cpuidle.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/latency.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpuidle.h>
  17. #include "cpuidle.h"
  18. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  19. EXPORT_PER_CPU_SYMBOL_GPL(cpuidle_devices);
  20. DEFINE_MUTEX(cpuidle_lock);
  21. LIST_HEAD(cpuidle_detected_devices);
  22. static void (*pm_idle_old)(void);
  23. static int enabled_devices;
  24. /**
  25. * cpuidle_idle_call - the main idle loop
  26. *
  27. * NOTE: no locks or semaphores should be used here
  28. */
  29. static void cpuidle_idle_call(void)
  30. {
  31. struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
  32. struct cpuidle_state *target_state;
  33. int next_state;
  34. /* check if the device is ready */
  35. if (!dev || !dev->enabled) {
  36. if (pm_idle_old)
  37. pm_idle_old();
  38. else
  39. local_irq_enable();
  40. return;
  41. }
  42. /* ask the governor for the next state */
  43. next_state = cpuidle_curr_governor->select(dev);
  44. if (need_resched())
  45. return;
  46. target_state = &dev->states[next_state];
  47. /* enter the state and update stats */
  48. dev->last_residency = target_state->enter(dev, target_state);
  49. dev->last_state = target_state;
  50. target_state->time += dev->last_residency;
  51. target_state->usage++;
  52. /* give the governor an opportunity to reflect on the outcome */
  53. if (cpuidle_curr_governor->reflect)
  54. cpuidle_curr_governor->reflect(dev);
  55. }
  56. /**
  57. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  58. */
  59. void cpuidle_install_idle_handler(void)
  60. {
  61. if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
  62. /* Make sure all changes finished before we switch to new idle */
  63. smp_wmb();
  64. pm_idle = cpuidle_idle_call;
  65. }
  66. }
  67. /**
  68. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  69. */
  70. void cpuidle_uninstall_idle_handler(void)
  71. {
  72. if (enabled_devices && (pm_idle != pm_idle_old)) {
  73. pm_idle = pm_idle_old;
  74. cpu_idle_wait();
  75. }
  76. }
  77. /**
  78. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  79. */
  80. void cpuidle_pause_and_lock(void)
  81. {
  82. mutex_lock(&cpuidle_lock);
  83. cpuidle_uninstall_idle_handler();
  84. }
  85. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  86. /**
  87. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  88. */
  89. void cpuidle_resume_and_unlock(void)
  90. {
  91. cpuidle_install_idle_handler();
  92. mutex_unlock(&cpuidle_lock);
  93. }
  94. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  95. /**
  96. * cpuidle_enable_device - enables idle PM for a CPU
  97. * @dev: the CPU
  98. *
  99. * This function must be called between cpuidle_pause_and_lock and
  100. * cpuidle_resume_and_unlock when used externally.
  101. */
  102. int cpuidle_enable_device(struct cpuidle_device *dev)
  103. {
  104. int ret, i;
  105. if (dev->enabled)
  106. return 0;
  107. if (!cpuidle_curr_driver || !cpuidle_curr_governor)
  108. return -EIO;
  109. if (!dev->state_count)
  110. return -EINVAL;
  111. if ((ret = cpuidle_add_state_sysfs(dev)))
  112. return ret;
  113. if (cpuidle_curr_governor->enable &&
  114. (ret = cpuidle_curr_governor->enable(dev)))
  115. goto fail_sysfs;
  116. for (i = 0; i < dev->state_count; i++) {
  117. dev->states[i].usage = 0;
  118. dev->states[i].time = 0;
  119. }
  120. dev->last_residency = 0;
  121. dev->last_state = NULL;
  122. smp_wmb();
  123. dev->enabled = 1;
  124. enabled_devices++;
  125. return 0;
  126. fail_sysfs:
  127. cpuidle_remove_state_sysfs(dev);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  131. /**
  132. * cpuidle_disable_device - disables idle PM for a CPU
  133. * @dev: the CPU
  134. *
  135. * This function must be called between cpuidle_pause_and_lock and
  136. * cpuidle_resume_and_unlock when used externally.
  137. */
  138. void cpuidle_disable_device(struct cpuidle_device *dev)
  139. {
  140. if (!dev->enabled)
  141. return;
  142. if (!cpuidle_curr_driver || !cpuidle_curr_governor)
  143. return;
  144. dev->enabled = 0;
  145. if (cpuidle_curr_governor->disable)
  146. cpuidle_curr_governor->disable(dev);
  147. cpuidle_remove_state_sysfs(dev);
  148. enabled_devices--;
  149. }
  150. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  151. /**
  152. * cpuidle_register_device - registers a CPU's idle PM feature
  153. * @dev: the cpu
  154. */
  155. int cpuidle_register_device(struct cpuidle_device *dev)
  156. {
  157. int ret;
  158. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  159. if (!sys_dev)
  160. return -EINVAL;
  161. if (!try_module_get(cpuidle_curr_driver->owner))
  162. return -EINVAL;
  163. init_completion(&dev->kobj_unregister);
  164. mutex_lock(&cpuidle_lock);
  165. per_cpu(cpuidle_devices, dev->cpu) = dev;
  166. list_add(&dev->device_list, &cpuidle_detected_devices);
  167. if ((ret = cpuidle_add_sysfs(sys_dev))) {
  168. mutex_unlock(&cpuidle_lock);
  169. module_put(cpuidle_curr_driver->owner);
  170. return ret;
  171. }
  172. cpuidle_enable_device(dev);
  173. cpuidle_install_idle_handler();
  174. mutex_unlock(&cpuidle_lock);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  178. /**
  179. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  180. * @dev: the cpu
  181. */
  182. void cpuidle_unregister_device(struct cpuidle_device *dev)
  183. {
  184. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  185. cpuidle_pause_and_lock();
  186. cpuidle_disable_device(dev);
  187. cpuidle_remove_sysfs(sys_dev);
  188. list_del(&dev->device_list);
  189. wait_for_completion(&dev->kobj_unregister);
  190. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  191. cpuidle_resume_and_unlock();
  192. module_put(cpuidle_curr_driver->owner);
  193. }
  194. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  195. #ifdef CONFIG_SMP
  196. static void smp_callback(void *v)
  197. {
  198. /* we already woke the CPU up, nothing more to do */
  199. }
  200. /*
  201. * This function gets called when a part of the kernel has a new latency
  202. * requirement. This means we need to get all processors out of their C-state,
  203. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  204. * wakes them all right up.
  205. */
  206. static int cpuidle_latency_notify(struct notifier_block *b,
  207. unsigned long l, void *v)
  208. {
  209. smp_call_function(smp_callback, NULL, 0, 1);
  210. return NOTIFY_OK;
  211. }
  212. static struct notifier_block cpuidle_latency_notifier = {
  213. .notifier_call = cpuidle_latency_notify,
  214. };
  215. #define latency_notifier_init(x) do { register_latency_notifier(x); } while (0)
  216. #else /* CONFIG_SMP */
  217. #define latency_notifier_init(x) do { } while (0)
  218. #endif /* CONFIG_SMP */
  219. /**
  220. * cpuidle_init - core initializer
  221. */
  222. static int __init cpuidle_init(void)
  223. {
  224. int ret;
  225. pm_idle_old = pm_idle;
  226. ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
  227. if (ret)
  228. return ret;
  229. latency_notifier_init(&cpuidle_latency_notifier);
  230. return 0;
  231. }
  232. core_initcall(cpuidle_init);