cpuidle.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
  37. static void cpuidle_kick_cpus(void)
  38. {
  39. cpu_idle_wait();
  40. }
  41. #elif defined(CONFIG_SMP)
  42. # error "Arch needs cpu_idle_wait() equivalent here"
  43. #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
  44. static void cpuidle_kick_cpus(void) {}
  45. #endif
  46. static int __cpuidle_register_device(struct cpuidle_device *dev);
  47. /**
  48. * cpuidle_idle_call - the main idle loop
  49. *
  50. * NOTE: no locks or semaphores should be used here
  51. * return non-zero on failure
  52. */
  53. int cpuidle_idle_call(void)
  54. {
  55. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  56. struct cpuidle_state *target_state;
  57. int next_state;
  58. if (off)
  59. return -ENODEV;
  60. if (!initialized)
  61. return -ENODEV;
  62. /* check if the device is ready */
  63. if (!dev || !dev->enabled)
  64. return -EBUSY;
  65. #if 0
  66. /* shows regressions, re-enable for 2.6.29 */
  67. /*
  68. * run any timers that can be run now, at this point
  69. * before calculating the idle duration etc.
  70. */
  71. hrtimer_peek_ahead_timers();
  72. #endif
  73. /*
  74. * Call the device's prepare function before calling the
  75. * governor's select function. ->prepare gives the device's
  76. * cpuidle driver a chance to update any dynamic information
  77. * of its cpuidle states for the current idle period, e.g.
  78. * state availability, latencies, residencies, etc.
  79. */
  80. if (dev->prepare)
  81. dev->prepare(dev);
  82. /* ask the governor for the next state */
  83. next_state = cpuidle_curr_governor->select(dev);
  84. if (need_resched()) {
  85. local_irq_enable();
  86. return 0;
  87. }
  88. target_state = &dev->states[next_state];
  89. /* enter the state and update stats */
  90. dev->last_state = target_state;
  91. trace_power_start(POWER_CSTATE, next_state, dev->cpu);
  92. trace_cpu_idle(next_state, dev->cpu);
  93. dev->last_residency = target_state->enter(dev, target_state);
  94. trace_power_end(dev->cpu);
  95. trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
  96. if (dev->last_state)
  97. target_state = dev->last_state;
  98. target_state->time += (unsigned long long)dev->last_residency;
  99. target_state->usage++;
  100. /* give the governor an opportunity to reflect on the outcome */
  101. if (cpuidle_curr_governor->reflect)
  102. cpuidle_curr_governor->reflect(dev);
  103. return 0;
  104. }
  105. /**
  106. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  107. */
  108. void cpuidle_install_idle_handler(void)
  109. {
  110. if (enabled_devices) {
  111. /* Make sure all changes finished before we switch to new idle */
  112. smp_wmb();
  113. initialized = 1;
  114. }
  115. }
  116. /**
  117. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  118. */
  119. void cpuidle_uninstall_idle_handler(void)
  120. {
  121. if (enabled_devices) {
  122. initialized = 0;
  123. cpuidle_kick_cpus();
  124. }
  125. }
  126. /**
  127. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  128. */
  129. void cpuidle_pause_and_lock(void)
  130. {
  131. mutex_lock(&cpuidle_lock);
  132. cpuidle_uninstall_idle_handler();
  133. }
  134. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  135. /**
  136. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  137. */
  138. void cpuidle_resume_and_unlock(void)
  139. {
  140. cpuidle_install_idle_handler();
  141. mutex_unlock(&cpuidle_lock);
  142. }
  143. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  144. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  145. static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
  146. {
  147. ktime_t t1, t2;
  148. s64 diff;
  149. int ret;
  150. t1 = ktime_get();
  151. local_irq_enable();
  152. while (!need_resched())
  153. cpu_relax();
  154. t2 = ktime_get();
  155. diff = ktime_to_us(ktime_sub(t2, t1));
  156. if (diff > INT_MAX)
  157. diff = INT_MAX;
  158. ret = (int) diff;
  159. return ret;
  160. }
  161. static void poll_idle_init(struct cpuidle_device *dev)
  162. {
  163. struct cpuidle_state *state = &dev->states[0];
  164. cpuidle_set_statedata(state, NULL);
  165. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  166. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  167. state->exit_latency = 0;
  168. state->target_residency = 0;
  169. state->power_usage = -1;
  170. state->flags = 0;
  171. state->enter = poll_idle;
  172. }
  173. #else
  174. static void poll_idle_init(struct cpuidle_device *dev) {}
  175. #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
  176. /**
  177. * cpuidle_enable_device - enables idle PM for a CPU
  178. * @dev: the CPU
  179. *
  180. * This function must be called between cpuidle_pause_and_lock and
  181. * cpuidle_resume_and_unlock when used externally.
  182. */
  183. int cpuidle_enable_device(struct cpuidle_device *dev)
  184. {
  185. int ret, i;
  186. if (dev->enabled)
  187. return 0;
  188. if (!cpuidle_get_driver() || !cpuidle_curr_governor)
  189. return -EIO;
  190. if (!dev->state_count)
  191. return -EINVAL;
  192. if (dev->registered == 0) {
  193. ret = __cpuidle_register_device(dev);
  194. if (ret)
  195. return ret;
  196. }
  197. poll_idle_init(dev);
  198. if ((ret = cpuidle_add_state_sysfs(dev)))
  199. return ret;
  200. if (cpuidle_curr_governor->enable &&
  201. (ret = cpuidle_curr_governor->enable(dev)))
  202. goto fail_sysfs;
  203. for (i = 0; i < dev->state_count; i++) {
  204. dev->states[i].usage = 0;
  205. dev->states[i].time = 0;
  206. }
  207. dev->last_residency = 0;
  208. dev->last_state = NULL;
  209. smp_wmb();
  210. dev->enabled = 1;
  211. enabled_devices++;
  212. return 0;
  213. fail_sysfs:
  214. cpuidle_remove_state_sysfs(dev);
  215. return ret;
  216. }
  217. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  218. /**
  219. * cpuidle_disable_device - disables idle PM for a CPU
  220. * @dev: the CPU
  221. *
  222. * This function must be called between cpuidle_pause_and_lock and
  223. * cpuidle_resume_and_unlock when used externally.
  224. */
  225. void cpuidle_disable_device(struct cpuidle_device *dev)
  226. {
  227. if (!dev->enabled)
  228. return;
  229. if (!cpuidle_get_driver() || !cpuidle_curr_governor)
  230. return;
  231. dev->enabled = 0;
  232. if (cpuidle_curr_governor->disable)
  233. cpuidle_curr_governor->disable(dev);
  234. cpuidle_remove_state_sysfs(dev);
  235. enabled_devices--;
  236. }
  237. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  238. /**
  239. * __cpuidle_register_device - internal register function called before register
  240. * and enable routines
  241. * @dev: the cpu
  242. *
  243. * cpuidle_lock mutex must be held before this is called
  244. */
  245. static int __cpuidle_register_device(struct cpuidle_device *dev)
  246. {
  247. int ret;
  248. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  249. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  250. if (!sys_dev)
  251. return -EINVAL;
  252. if (!try_module_get(cpuidle_driver->owner))
  253. return -EINVAL;
  254. init_completion(&dev->kobj_unregister);
  255. /*
  256. * cpuidle driver should set the dev->power_specified bit
  257. * before registering the device if the driver provides
  258. * power_usage numbers.
  259. *
  260. * For those devices whose ->power_specified is not set,
  261. * we fill in power_usage with decreasing values as the
  262. * cpuidle code has an implicit assumption that state Cn
  263. * uses less power than C(n-1).
  264. *
  265. * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
  266. * an power value of -1. So we use -2, -3, etc, for other
  267. * c-states.
  268. */
  269. if (!dev->power_specified) {
  270. int i;
  271. for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
  272. dev->states[i].power_usage = -1 - i;
  273. }
  274. per_cpu(cpuidle_devices, dev->cpu) = dev;
  275. list_add(&dev->device_list, &cpuidle_detected_devices);
  276. if ((ret = cpuidle_add_sysfs(sys_dev))) {
  277. module_put(cpuidle_driver->owner);
  278. return ret;
  279. }
  280. dev->registered = 1;
  281. return 0;
  282. }
  283. /**
  284. * cpuidle_register_device - registers a CPU's idle PM feature
  285. * @dev: the cpu
  286. */
  287. int cpuidle_register_device(struct cpuidle_device *dev)
  288. {
  289. int ret;
  290. mutex_lock(&cpuidle_lock);
  291. if ((ret = __cpuidle_register_device(dev))) {
  292. mutex_unlock(&cpuidle_lock);
  293. return ret;
  294. }
  295. cpuidle_enable_device(dev);
  296. cpuidle_install_idle_handler();
  297. mutex_unlock(&cpuidle_lock);
  298. return 0;
  299. }
  300. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  301. /**
  302. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  303. * @dev: the cpu
  304. */
  305. void cpuidle_unregister_device(struct cpuidle_device *dev)
  306. {
  307. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  308. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  309. if (dev->registered == 0)
  310. return;
  311. cpuidle_pause_and_lock();
  312. cpuidle_disable_device(dev);
  313. cpuidle_remove_sysfs(sys_dev);
  314. list_del(&dev->device_list);
  315. wait_for_completion(&dev->kobj_unregister);
  316. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  317. cpuidle_resume_and_unlock();
  318. module_put(cpuidle_driver->owner);
  319. }
  320. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  321. #ifdef CONFIG_SMP
  322. static void smp_callback(void *v)
  323. {
  324. /* we already woke the CPU up, nothing more to do */
  325. }
  326. /*
  327. * This function gets called when a part of the kernel has a new latency
  328. * requirement. This means we need to get all processors out of their C-state,
  329. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  330. * wakes them all right up.
  331. */
  332. static int cpuidle_latency_notify(struct notifier_block *b,
  333. unsigned long l, void *v)
  334. {
  335. smp_call_function(smp_callback, NULL, 1);
  336. return NOTIFY_OK;
  337. }
  338. static struct notifier_block cpuidle_latency_notifier = {
  339. .notifier_call = cpuidle_latency_notify,
  340. };
  341. static inline void latency_notifier_init(struct notifier_block *n)
  342. {
  343. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  344. }
  345. #else /* CONFIG_SMP */
  346. #define latency_notifier_init(x) do { } while (0)
  347. #endif /* CONFIG_SMP */
  348. /**
  349. * cpuidle_init - core initializer
  350. */
  351. static int __init cpuidle_init(void)
  352. {
  353. int ret;
  354. if (cpuidle_disabled())
  355. return -ENODEV;
  356. ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
  357. if (ret)
  358. return ret;
  359. latency_notifier_init(&cpuidle_latency_notifier);
  360. return 0;
  361. }
  362. module_param(off, int, 0444);
  363. core_initcall(cpuidle_init);