cpuidle.c 6.3 KB

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