cpuidle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <trace/events/power.h>
  22. #include "cpuidle.h"
  23. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  24. DEFINE_MUTEX(cpuidle_lock);
  25. LIST_HEAD(cpuidle_detected_devices);
  26. static int enabled_devices;
  27. static int off __read_mostly;
  28. static int initialized __read_mostly;
  29. int cpuidle_disabled(void)
  30. {
  31. return off;
  32. }
  33. void disable_cpuidle(void)
  34. {
  35. off = 1;
  36. }
  37. static int __cpuidle_register_device(struct cpuidle_device *dev);
  38. /**
  39. * cpuidle_play_dead - cpu off-lining
  40. *
  41. * Returns in case of an error or no driver
  42. */
  43. int cpuidle_play_dead(void)
  44. {
  45. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  46. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  47. int i;
  48. if (!drv)
  49. return -ENODEV;
  50. /* Find lowest-power state that supports long-term idle */
  51. for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
  52. if (drv->states[i].enter_dead)
  53. return drv->states[i].enter_dead(dev, i);
  54. return -ENODEV;
  55. }
  56. /**
  57. * cpuidle_enter_state - enter the state and update stats
  58. * @dev: cpuidle device for this cpu
  59. * @drv: cpuidle driver for this cpu
  60. * @next_state: index into drv->states of the state to enter
  61. */
  62. int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
  63. int index)
  64. {
  65. int entered_state;
  66. struct cpuidle_state *target_state = &drv->states[index];
  67. ktime_t time_start, time_end;
  68. s64 diff;
  69. time_start = ktime_get();
  70. entered_state = target_state->enter(dev, drv, index);
  71. time_end = ktime_get();
  72. local_irq_enable();
  73. diff = ktime_to_us(ktime_sub(time_end, time_start));
  74. if (diff > INT_MAX)
  75. diff = INT_MAX;
  76. dev->last_residency = (int) diff;
  77. if (entered_state >= 0) {
  78. /* Update cpuidle counters */
  79. /* This can be moved to within driver enter routine
  80. * but that results in multiple copies of same code.
  81. */
  82. dev->states_usage[entered_state].time += dev->last_residency;
  83. dev->states_usage[entered_state].usage++;
  84. } else {
  85. dev->last_residency = 0;
  86. }
  87. return entered_state;
  88. }
  89. /**
  90. * cpuidle_idle_call - the main idle loop
  91. *
  92. * NOTE: no locks or semaphores should be used here
  93. * return non-zero on failure
  94. */
  95. int cpuidle_idle_call(void)
  96. {
  97. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  98. struct cpuidle_driver *drv;
  99. int next_state, entered_state;
  100. if (off)
  101. return -ENODEV;
  102. if (!initialized)
  103. return -ENODEV;
  104. /* check if the device is ready */
  105. if (!dev || !dev->enabled)
  106. return -EBUSY;
  107. drv = cpuidle_get_cpu_driver(dev);
  108. /* ask the governor for the next state */
  109. next_state = cpuidle_curr_governor->select(drv, dev);
  110. if (need_resched()) {
  111. dev->last_residency = 0;
  112. /* give the governor an opportunity to reflect on the outcome */
  113. if (cpuidle_curr_governor->reflect)
  114. cpuidle_curr_governor->reflect(dev, next_state);
  115. local_irq_enable();
  116. return 0;
  117. }
  118. trace_cpu_idle_rcuidle(next_state, dev->cpu);
  119. if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
  120. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
  121. &dev->cpu);
  122. if (cpuidle_state_is_coupled(dev, drv, next_state))
  123. entered_state = cpuidle_enter_state_coupled(dev, drv,
  124. next_state);
  125. else
  126. entered_state = cpuidle_enter_state(dev, drv, next_state);
  127. if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
  128. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
  129. &dev->cpu);
  130. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  131. /* give the governor an opportunity to reflect on the outcome */
  132. if (cpuidle_curr_governor->reflect)
  133. cpuidle_curr_governor->reflect(dev, entered_state);
  134. return 0;
  135. }
  136. /**
  137. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  138. */
  139. void cpuidle_install_idle_handler(void)
  140. {
  141. if (enabled_devices) {
  142. /* Make sure all changes finished before we switch to new idle */
  143. smp_wmb();
  144. initialized = 1;
  145. }
  146. }
  147. /**
  148. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  149. */
  150. void cpuidle_uninstall_idle_handler(void)
  151. {
  152. if (enabled_devices) {
  153. initialized = 0;
  154. kick_all_cpus_sync();
  155. }
  156. }
  157. /**
  158. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  159. */
  160. void cpuidle_pause_and_lock(void)
  161. {
  162. mutex_lock(&cpuidle_lock);
  163. cpuidle_uninstall_idle_handler();
  164. }
  165. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  166. /**
  167. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  168. */
  169. void cpuidle_resume_and_unlock(void)
  170. {
  171. cpuidle_install_idle_handler();
  172. mutex_unlock(&cpuidle_lock);
  173. }
  174. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  175. /* Currently used in suspend/resume path to suspend cpuidle */
  176. void cpuidle_pause(void)
  177. {
  178. mutex_lock(&cpuidle_lock);
  179. cpuidle_uninstall_idle_handler();
  180. mutex_unlock(&cpuidle_lock);
  181. }
  182. /* Currently used in suspend/resume path to resume cpuidle */
  183. void cpuidle_resume(void)
  184. {
  185. mutex_lock(&cpuidle_lock);
  186. cpuidle_install_idle_handler();
  187. mutex_unlock(&cpuidle_lock);
  188. }
  189. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  190. static int poll_idle(struct cpuidle_device *dev,
  191. struct cpuidle_driver *drv, int index)
  192. {
  193. ktime_t t1, t2;
  194. s64 diff;
  195. t1 = ktime_get();
  196. local_irq_enable();
  197. while (!need_resched())
  198. cpu_relax();
  199. t2 = ktime_get();
  200. diff = ktime_to_us(ktime_sub(t2, t1));
  201. if (diff > INT_MAX)
  202. diff = INT_MAX;
  203. dev->last_residency = (int) diff;
  204. return index;
  205. }
  206. static void poll_idle_init(struct cpuidle_driver *drv)
  207. {
  208. struct cpuidle_state *state = &drv->states[0];
  209. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  210. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  211. state->exit_latency = 0;
  212. state->target_residency = 0;
  213. state->power_usage = -1;
  214. state->flags = 0;
  215. state->enter = poll_idle;
  216. state->disabled = false;
  217. }
  218. #else
  219. static void poll_idle_init(struct cpuidle_driver *drv) {}
  220. #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
  221. /**
  222. * cpuidle_enable_device - enables idle PM for a CPU
  223. * @dev: the CPU
  224. *
  225. * This function must be called between cpuidle_pause_and_lock and
  226. * cpuidle_resume_and_unlock when used externally.
  227. */
  228. int cpuidle_enable_device(struct cpuidle_device *dev)
  229. {
  230. int ret, i;
  231. struct cpuidle_driver *drv;
  232. if (!dev)
  233. return -EINVAL;
  234. if (dev->enabled)
  235. return 0;
  236. drv = cpuidle_get_cpu_driver(dev);
  237. if (!drv || !cpuidle_curr_governor)
  238. return -EIO;
  239. if (!dev->state_count)
  240. dev->state_count = drv->state_count;
  241. if (dev->registered == 0) {
  242. ret = __cpuidle_register_device(dev);
  243. if (ret)
  244. return ret;
  245. }
  246. poll_idle_init(drv);
  247. ret = cpuidle_add_device_sysfs(dev);
  248. if (ret)
  249. return ret;
  250. if (cpuidle_curr_governor->enable &&
  251. (ret = cpuidle_curr_governor->enable(drv, dev)))
  252. goto fail_sysfs;
  253. for (i = 0; i < dev->state_count; i++) {
  254. dev->states_usage[i].usage = 0;
  255. dev->states_usage[i].time = 0;
  256. }
  257. dev->last_residency = 0;
  258. smp_wmb();
  259. dev->enabled = 1;
  260. enabled_devices++;
  261. return 0;
  262. fail_sysfs:
  263. cpuidle_remove_device_sysfs(dev);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  267. /**
  268. * cpuidle_disable_device - disables idle PM for a CPU
  269. * @dev: the CPU
  270. *
  271. * This function must be called between cpuidle_pause_and_lock and
  272. * cpuidle_resume_and_unlock when used externally.
  273. */
  274. void cpuidle_disable_device(struct cpuidle_device *dev)
  275. {
  276. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  277. if (!dev || !dev->enabled)
  278. return;
  279. if (!drv || !cpuidle_curr_governor)
  280. return;
  281. dev->enabled = 0;
  282. if (cpuidle_curr_governor->disable)
  283. cpuidle_curr_governor->disable(drv, dev);
  284. cpuidle_remove_device_sysfs(dev);
  285. enabled_devices--;
  286. }
  287. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  288. /**
  289. * __cpuidle_register_device - internal register function called before register
  290. * and enable routines
  291. * @dev: the cpu
  292. *
  293. * cpuidle_lock mutex must be held before this is called
  294. */
  295. static int __cpuidle_register_device(struct cpuidle_device *dev)
  296. {
  297. int ret;
  298. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  299. if (!try_module_get(drv->owner))
  300. return -EINVAL;
  301. per_cpu(cpuidle_devices, dev->cpu) = dev;
  302. list_add(&dev->device_list, &cpuidle_detected_devices);
  303. ret = cpuidle_add_sysfs(dev);
  304. if (ret)
  305. goto err_sysfs;
  306. ret = cpuidle_coupled_register_device(dev);
  307. if (ret)
  308. goto err_coupled;
  309. dev->registered = 1;
  310. return 0;
  311. err_coupled:
  312. cpuidle_remove_sysfs(dev);
  313. err_sysfs:
  314. list_del(&dev->device_list);
  315. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  316. module_put(drv->owner);
  317. return ret;
  318. }
  319. /**
  320. * cpuidle_register_device - registers a CPU's idle PM feature
  321. * @dev: the cpu
  322. */
  323. int cpuidle_register_device(struct cpuidle_device *dev)
  324. {
  325. int ret;
  326. if (!dev)
  327. return -EINVAL;
  328. mutex_lock(&cpuidle_lock);
  329. if ((ret = __cpuidle_register_device(dev))) {
  330. mutex_unlock(&cpuidle_lock);
  331. return ret;
  332. }
  333. cpuidle_enable_device(dev);
  334. cpuidle_install_idle_handler();
  335. mutex_unlock(&cpuidle_lock);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  339. /**
  340. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  341. * @dev: the cpu
  342. */
  343. void cpuidle_unregister_device(struct cpuidle_device *dev)
  344. {
  345. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  346. if (dev->registered == 0)
  347. return;
  348. cpuidle_pause_and_lock();
  349. cpuidle_disable_device(dev);
  350. cpuidle_remove_sysfs(dev);
  351. list_del(&dev->device_list);
  352. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  353. cpuidle_coupled_unregister_device(dev);
  354. cpuidle_resume_and_unlock();
  355. module_put(drv->owner);
  356. }
  357. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  358. #ifdef CONFIG_SMP
  359. static void smp_callback(void *v)
  360. {
  361. /* we already woke the CPU up, nothing more to do */
  362. }
  363. /*
  364. * This function gets called when a part of the kernel has a new latency
  365. * requirement. This means we need to get all processors out of their C-state,
  366. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  367. * wakes them all right up.
  368. */
  369. static int cpuidle_latency_notify(struct notifier_block *b,
  370. unsigned long l, void *v)
  371. {
  372. smp_call_function(smp_callback, NULL, 1);
  373. return NOTIFY_OK;
  374. }
  375. static struct notifier_block cpuidle_latency_notifier = {
  376. .notifier_call = cpuidle_latency_notify,
  377. };
  378. static inline void latency_notifier_init(struct notifier_block *n)
  379. {
  380. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  381. }
  382. #else /* CONFIG_SMP */
  383. #define latency_notifier_init(x) do { } while (0)
  384. #endif /* CONFIG_SMP */
  385. /**
  386. * cpuidle_init - core initializer
  387. */
  388. static int __init cpuidle_init(void)
  389. {
  390. int ret;
  391. if (cpuidle_disabled())
  392. return -ENODEV;
  393. ret = cpuidle_add_interface(cpu_subsys.dev_root);
  394. if (ret)
  395. return ret;
  396. latency_notifier_init(&cpuidle_latency_notifier);
  397. return 0;
  398. }
  399. module_param(off, int, 0444);
  400. core_initcall(cpuidle_init);