cpuidle.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * cpuidle.c - core cpuidle infrastructure
  3. *
  4. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  5. * Shaohua Li <shaohua.li@intel.com>
  6. * Adam Belay <abelay@novell.com>
  7. *
  8. * This code is licenced under the GPL.
  9. */
  10. #include <linux/clockchips.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mutex.h>
  13. #include <linux/sched.h>
  14. #include <linux/notifier.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpuidle.h>
  18. #include <linux/ktime.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/module.h>
  21. #include <linux/suspend.h>
  22. #include <linux/tick.h>
  23. #include <trace/events/power.h>
  24. #include "cpuidle.h"
  25. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  26. DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  27. DEFINE_MUTEX(cpuidle_lock);
  28. LIST_HEAD(cpuidle_detected_devices);
  29. static int enabled_devices;
  30. static int off __read_mostly;
  31. static int initialized __read_mostly;
  32. int cpuidle_disabled(void)
  33. {
  34. return off;
  35. }
  36. void disable_cpuidle(void)
  37. {
  38. off = 1;
  39. }
  40. bool cpuidle_not_available(struct cpuidle_driver *drv,
  41. struct cpuidle_device *dev)
  42. {
  43. return off || !initialized || !drv || !dev || !dev->enabled;
  44. }
  45. /**
  46. * cpuidle_play_dead - cpu off-lining
  47. *
  48. * Returns in case of an error or no driver
  49. */
  50. int cpuidle_play_dead(void)
  51. {
  52. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  53. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  54. int i;
  55. if (!drv)
  56. return -ENODEV;
  57. /* Find lowest-power state that supports long-term idle */
  58. for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
  59. if (drv->states[i].enter_dead)
  60. return drv->states[i].enter_dead(dev, i);
  61. return -ENODEV;
  62. }
  63. static int find_deepest_state(struct cpuidle_driver *drv,
  64. struct cpuidle_device *dev, bool freeze)
  65. {
  66. unsigned int latency_req = 0;
  67. int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
  68. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
  69. struct cpuidle_state *s = &drv->states[i];
  70. struct cpuidle_state_usage *su = &dev->states_usage[i];
  71. if (s->disabled || su->disable || s->exit_latency <= latency_req
  72. || (freeze && !s->enter_freeze))
  73. continue;
  74. latency_req = s->exit_latency;
  75. ret = i;
  76. }
  77. return ret;
  78. }
  79. /**
  80. * cpuidle_find_deepest_state - Find the deepest available idle state.
  81. * @drv: cpuidle driver for the given CPU.
  82. * @dev: cpuidle device for the given CPU.
  83. */
  84. int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  85. struct cpuidle_device *dev)
  86. {
  87. return find_deepest_state(drv, dev, false);
  88. }
  89. static void enter_freeze_proper(struct cpuidle_driver *drv,
  90. struct cpuidle_device *dev, int index)
  91. {
  92. tick_freeze();
  93. /*
  94. * The state used here cannot be a "coupled" one, because the "coupled"
  95. * cpuidle mechanism enables interrupts and doing that with timekeeping
  96. * suspended is generally unsafe.
  97. */
  98. drv->states[index].enter_freeze(dev, drv, index);
  99. WARN_ON(!irqs_disabled());
  100. /*
  101. * timekeeping_resume() that will be called by tick_unfreeze() for the
  102. * last CPU executing it calls functions containing RCU read-side
  103. * critical sections, so tell RCU about that.
  104. */
  105. RCU_NONIDLE(tick_unfreeze());
  106. }
  107. /**
  108. * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
  109. * @drv: cpuidle driver for the given CPU.
  110. * @dev: cpuidle device for the given CPU.
  111. *
  112. * If there are states with the ->enter_freeze callback, find the deepest of
  113. * them and enter it with frozen tick.
  114. */
  115. int cpuidle_enter_freeze(struct cpuidle_driver *drv, struct cpuidle_device *dev)
  116. {
  117. int index;
  118. /*
  119. * Find the deepest state with ->enter_freeze present, which guarantees
  120. * that interrupts won't be enabled when it exits and allows the tick to
  121. * be frozen safely.
  122. */
  123. index = find_deepest_state(drv, dev, true);
  124. if (index >= 0)
  125. enter_freeze_proper(drv, dev, index);
  126. return index;
  127. }
  128. /**
  129. * cpuidle_enter_state - enter the state and update stats
  130. * @dev: cpuidle device for this cpu
  131. * @drv: cpuidle driver for this cpu
  132. * @next_state: index into drv->states of the state to enter
  133. */
  134. int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
  135. int index)
  136. {
  137. int entered_state;
  138. struct cpuidle_state *target_state = &drv->states[index];
  139. ktime_t time_start, time_end;
  140. s64 diff;
  141. trace_cpu_idle_rcuidle(index, dev->cpu);
  142. time_start = ktime_get();
  143. entered_state = target_state->enter(dev, drv, index);
  144. time_end = ktime_get();
  145. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  146. if (!cpuidle_state_is_coupled(dev, drv, entered_state))
  147. local_irq_enable();
  148. diff = ktime_to_us(ktime_sub(time_end, time_start));
  149. if (diff > INT_MAX)
  150. diff = INT_MAX;
  151. dev->last_residency = (int) diff;
  152. if (entered_state >= 0) {
  153. /* Update cpuidle counters */
  154. /* This can be moved to within driver enter routine
  155. * but that results in multiple copies of same code.
  156. */
  157. dev->states_usage[entered_state].time += dev->last_residency;
  158. dev->states_usage[entered_state].usage++;
  159. } else {
  160. dev->last_residency = 0;
  161. }
  162. return entered_state;
  163. }
  164. /**
  165. * cpuidle_select - ask the cpuidle framework to choose an idle state
  166. *
  167. * @drv: the cpuidle driver
  168. * @dev: the cpuidle device
  169. *
  170. * Returns the index of the idle state.
  171. */
  172. int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
  173. {
  174. return cpuidle_curr_governor->select(drv, dev);
  175. }
  176. /**
  177. * cpuidle_enter - enter into the specified idle state
  178. *
  179. * @drv: the cpuidle driver tied with the cpu
  180. * @dev: the cpuidle device
  181. * @index: the index in the idle state table
  182. *
  183. * Returns the index in the idle state, < 0 in case of error.
  184. * The error code depends on the backend driver
  185. */
  186. int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  187. int index)
  188. {
  189. if (cpuidle_state_is_coupled(dev, drv, index))
  190. return cpuidle_enter_state_coupled(dev, drv, index);
  191. return cpuidle_enter_state(dev, drv, index);
  192. }
  193. /**
  194. * cpuidle_reflect - tell the underlying governor what was the state
  195. * we were in
  196. *
  197. * @dev : the cpuidle device
  198. * @index: the index in the idle state table
  199. *
  200. */
  201. void cpuidle_reflect(struct cpuidle_device *dev, int index)
  202. {
  203. if (cpuidle_curr_governor->reflect)
  204. cpuidle_curr_governor->reflect(dev, index);
  205. }
  206. /**
  207. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  208. */
  209. void cpuidle_install_idle_handler(void)
  210. {
  211. if (enabled_devices) {
  212. /* Make sure all changes finished before we switch to new idle */
  213. smp_wmb();
  214. initialized = 1;
  215. }
  216. }
  217. /**
  218. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  219. */
  220. void cpuidle_uninstall_idle_handler(void)
  221. {
  222. if (enabled_devices) {
  223. initialized = 0;
  224. wake_up_all_idle_cpus();
  225. }
  226. /*
  227. * Make sure external observers (such as the scheduler)
  228. * are done looking at pointed idle states.
  229. */
  230. synchronize_rcu();
  231. }
  232. /**
  233. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  234. */
  235. void cpuidle_pause_and_lock(void)
  236. {
  237. mutex_lock(&cpuidle_lock);
  238. cpuidle_uninstall_idle_handler();
  239. }
  240. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  241. /**
  242. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  243. */
  244. void cpuidle_resume_and_unlock(void)
  245. {
  246. cpuidle_install_idle_handler();
  247. mutex_unlock(&cpuidle_lock);
  248. }
  249. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  250. /* Currently used in suspend/resume path to suspend cpuidle */
  251. void cpuidle_pause(void)
  252. {
  253. mutex_lock(&cpuidle_lock);
  254. cpuidle_uninstall_idle_handler();
  255. mutex_unlock(&cpuidle_lock);
  256. }
  257. /* Currently used in suspend/resume path to resume cpuidle */
  258. void cpuidle_resume(void)
  259. {
  260. mutex_lock(&cpuidle_lock);
  261. cpuidle_install_idle_handler();
  262. mutex_unlock(&cpuidle_lock);
  263. }
  264. /**
  265. * cpuidle_enable_device - enables idle PM for a CPU
  266. * @dev: the CPU
  267. *
  268. * This function must be called between cpuidle_pause_and_lock and
  269. * cpuidle_resume_and_unlock when used externally.
  270. */
  271. int cpuidle_enable_device(struct cpuidle_device *dev)
  272. {
  273. int ret;
  274. struct cpuidle_driver *drv;
  275. if (!dev)
  276. return -EINVAL;
  277. if (dev->enabled)
  278. return 0;
  279. drv = cpuidle_get_cpu_driver(dev);
  280. if (!drv || !cpuidle_curr_governor)
  281. return -EIO;
  282. if (!dev->registered)
  283. return -EINVAL;
  284. if (!dev->state_count)
  285. dev->state_count = drv->state_count;
  286. ret = cpuidle_add_device_sysfs(dev);
  287. if (ret)
  288. return ret;
  289. if (cpuidle_curr_governor->enable &&
  290. (ret = cpuidle_curr_governor->enable(drv, dev)))
  291. goto fail_sysfs;
  292. smp_wmb();
  293. dev->enabled = 1;
  294. enabled_devices++;
  295. return 0;
  296. fail_sysfs:
  297. cpuidle_remove_device_sysfs(dev);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  301. /**
  302. * cpuidle_disable_device - disables idle PM for a CPU
  303. * @dev: the CPU
  304. *
  305. * This function must be called between cpuidle_pause_and_lock and
  306. * cpuidle_resume_and_unlock when used externally.
  307. */
  308. void cpuidle_disable_device(struct cpuidle_device *dev)
  309. {
  310. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  311. if (!dev || !dev->enabled)
  312. return;
  313. if (!drv || !cpuidle_curr_governor)
  314. return;
  315. dev->enabled = 0;
  316. if (cpuidle_curr_governor->disable)
  317. cpuidle_curr_governor->disable(drv, dev);
  318. cpuidle_remove_device_sysfs(dev);
  319. enabled_devices--;
  320. }
  321. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  322. static void __cpuidle_unregister_device(struct cpuidle_device *dev)
  323. {
  324. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  325. list_del(&dev->device_list);
  326. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  327. module_put(drv->owner);
  328. }
  329. static void __cpuidle_device_init(struct cpuidle_device *dev)
  330. {
  331. memset(dev->states_usage, 0, sizeof(dev->states_usage));
  332. dev->last_residency = 0;
  333. }
  334. /**
  335. * __cpuidle_register_device - internal register function called before register
  336. * and enable routines
  337. * @dev: the cpu
  338. *
  339. * cpuidle_lock mutex must be held before this is called
  340. */
  341. static int __cpuidle_register_device(struct cpuidle_device *dev)
  342. {
  343. int ret;
  344. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  345. if (!try_module_get(drv->owner))
  346. return -EINVAL;
  347. per_cpu(cpuidle_devices, dev->cpu) = dev;
  348. list_add(&dev->device_list, &cpuidle_detected_devices);
  349. ret = cpuidle_coupled_register_device(dev);
  350. if (ret)
  351. __cpuidle_unregister_device(dev);
  352. else
  353. dev->registered = 1;
  354. return ret;
  355. }
  356. /**
  357. * cpuidle_register_device - registers a CPU's idle PM feature
  358. * @dev: the cpu
  359. */
  360. int cpuidle_register_device(struct cpuidle_device *dev)
  361. {
  362. int ret = -EBUSY;
  363. if (!dev)
  364. return -EINVAL;
  365. mutex_lock(&cpuidle_lock);
  366. if (dev->registered)
  367. goto out_unlock;
  368. __cpuidle_device_init(dev);
  369. ret = __cpuidle_register_device(dev);
  370. if (ret)
  371. goto out_unlock;
  372. ret = cpuidle_add_sysfs(dev);
  373. if (ret)
  374. goto out_unregister;
  375. ret = cpuidle_enable_device(dev);
  376. if (ret)
  377. goto out_sysfs;
  378. cpuidle_install_idle_handler();
  379. out_unlock:
  380. mutex_unlock(&cpuidle_lock);
  381. return ret;
  382. out_sysfs:
  383. cpuidle_remove_sysfs(dev);
  384. out_unregister:
  385. __cpuidle_unregister_device(dev);
  386. goto out_unlock;
  387. }
  388. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  389. /**
  390. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  391. * @dev: the cpu
  392. */
  393. void cpuidle_unregister_device(struct cpuidle_device *dev)
  394. {
  395. if (!dev || dev->registered == 0)
  396. return;
  397. cpuidle_pause_and_lock();
  398. cpuidle_disable_device(dev);
  399. cpuidle_remove_sysfs(dev);
  400. __cpuidle_unregister_device(dev);
  401. cpuidle_coupled_unregister_device(dev);
  402. cpuidle_resume_and_unlock();
  403. }
  404. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  405. /**
  406. * cpuidle_unregister: unregister a driver and the devices. This function
  407. * can be used only if the driver has been previously registered through
  408. * the cpuidle_register function.
  409. *
  410. * @drv: a valid pointer to a struct cpuidle_driver
  411. */
  412. void cpuidle_unregister(struct cpuidle_driver *drv)
  413. {
  414. int cpu;
  415. struct cpuidle_device *device;
  416. for_each_cpu(cpu, drv->cpumask) {
  417. device = &per_cpu(cpuidle_dev, cpu);
  418. cpuidle_unregister_device(device);
  419. }
  420. cpuidle_unregister_driver(drv);
  421. }
  422. EXPORT_SYMBOL_GPL(cpuidle_unregister);
  423. /**
  424. * cpuidle_register: registers the driver and the cpu devices with the
  425. * coupled_cpus passed as parameter. This function is used for all common
  426. * initialization pattern there are in the arch specific drivers. The
  427. * devices is globally defined in this file.
  428. *
  429. * @drv : a valid pointer to a struct cpuidle_driver
  430. * @coupled_cpus: a cpumask for the coupled states
  431. *
  432. * Returns 0 on success, < 0 otherwise
  433. */
  434. int cpuidle_register(struct cpuidle_driver *drv,
  435. const struct cpumask *const coupled_cpus)
  436. {
  437. int ret, cpu;
  438. struct cpuidle_device *device;
  439. ret = cpuidle_register_driver(drv);
  440. if (ret) {
  441. pr_err("failed to register cpuidle driver\n");
  442. return ret;
  443. }
  444. for_each_cpu(cpu, drv->cpumask) {
  445. device = &per_cpu(cpuidle_dev, cpu);
  446. device->cpu = cpu;
  447. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  448. /*
  449. * On multiplatform for ARM, the coupled idle states could be
  450. * enabled in the kernel even if the cpuidle driver does not
  451. * use it. Note, coupled_cpus is a struct copy.
  452. */
  453. if (coupled_cpus)
  454. device->coupled_cpus = *coupled_cpus;
  455. #endif
  456. ret = cpuidle_register_device(device);
  457. if (!ret)
  458. continue;
  459. pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
  460. cpuidle_unregister(drv);
  461. break;
  462. }
  463. return ret;
  464. }
  465. EXPORT_SYMBOL_GPL(cpuidle_register);
  466. #ifdef CONFIG_SMP
  467. /*
  468. * This function gets called when a part of the kernel has a new latency
  469. * requirement. This means we need to get all processors out of their C-state,
  470. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  471. * wakes them all right up.
  472. */
  473. static int cpuidle_latency_notify(struct notifier_block *b,
  474. unsigned long l, void *v)
  475. {
  476. wake_up_all_idle_cpus();
  477. return NOTIFY_OK;
  478. }
  479. static struct notifier_block cpuidle_latency_notifier = {
  480. .notifier_call = cpuidle_latency_notify,
  481. };
  482. static inline void latency_notifier_init(struct notifier_block *n)
  483. {
  484. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  485. }
  486. #else /* CONFIG_SMP */
  487. #define latency_notifier_init(x) do { } while (0)
  488. #endif /* CONFIG_SMP */
  489. /**
  490. * cpuidle_init - core initializer
  491. */
  492. static int __init cpuidle_init(void)
  493. {
  494. int ret;
  495. if (cpuidle_disabled())
  496. return -ENODEV;
  497. ret = cpuidle_add_interface(cpu_subsys.dev_root);
  498. if (ret)
  499. return ret;
  500. latency_notifier_init(&cpuidle_latency_notifier);
  501. return 0;
  502. }
  503. module_param(off, int, 0444);
  504. core_initcall(cpuidle_init);