cpuidle.c 9.5 KB

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