cpuidle.c 11 KB

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