cpuidle.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "cpuidle.h"
  19. DEFINE_PER_CPU(struct cpuidle_device *, 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. cpuidle_kick_cpus();
  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. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  152. static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
  153. {
  154. ktime_t t1, t2;
  155. s64 diff;
  156. int ret;
  157. t1 = ktime_get();
  158. local_irq_enable();
  159. while (!need_resched())
  160. cpu_relax();
  161. t2 = ktime_get();
  162. diff = ktime_to_us(ktime_sub(t2, t1));
  163. if (diff > INT_MAX)
  164. diff = INT_MAX;
  165. ret = (int) diff;
  166. return ret;
  167. }
  168. static void poll_idle_init(struct cpuidle_device *dev)
  169. {
  170. struct cpuidle_state *state = &dev->states[0];
  171. cpuidle_set_statedata(state, NULL);
  172. snprintf(state->name, CPUIDLE_NAME_LEN, "C0 (poll idle)");
  173. state->exit_latency = 0;
  174. state->target_residency = 0;
  175. state->power_usage = -1;
  176. state->flags = CPUIDLE_FLAG_POLL | CPUIDLE_FLAG_TIME_VALID;
  177. state->enter = poll_idle;
  178. }
  179. #else
  180. static void poll_idle_init(struct cpuidle_device *dev) {}
  181. #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
  182. /**
  183. * cpuidle_register_device - registers a CPU's idle PM feature
  184. * @dev: the cpu
  185. */
  186. int cpuidle_register_device(struct cpuidle_device *dev)
  187. {
  188. int ret;
  189. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  190. if (!sys_dev)
  191. return -EINVAL;
  192. if (!try_module_get(cpuidle_curr_driver->owner))
  193. return -EINVAL;
  194. init_completion(&dev->kobj_unregister);
  195. mutex_lock(&cpuidle_lock);
  196. poll_idle_init(dev);
  197. per_cpu(cpuidle_devices, dev->cpu) = dev;
  198. list_add(&dev->device_list, &cpuidle_detected_devices);
  199. if ((ret = cpuidle_add_sysfs(sys_dev))) {
  200. mutex_unlock(&cpuidle_lock);
  201. module_put(cpuidle_curr_driver->owner);
  202. return ret;
  203. }
  204. cpuidle_enable_device(dev);
  205. cpuidle_install_idle_handler();
  206. mutex_unlock(&cpuidle_lock);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  210. /**
  211. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  212. * @dev: the cpu
  213. */
  214. void cpuidle_unregister_device(struct cpuidle_device *dev)
  215. {
  216. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  217. cpuidle_pause_and_lock();
  218. cpuidle_disable_device(dev);
  219. cpuidle_remove_sysfs(sys_dev);
  220. list_del(&dev->device_list);
  221. wait_for_completion(&dev->kobj_unregister);
  222. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  223. cpuidle_resume_and_unlock();
  224. module_put(cpuidle_curr_driver->owner);
  225. }
  226. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  227. #ifdef CONFIG_SMP
  228. static void smp_callback(void *v)
  229. {
  230. /* we already woke the CPU up, nothing more to do */
  231. }
  232. /*
  233. * This function gets called when a part of the kernel has a new latency
  234. * requirement. This means we need to get all processors out of their C-state,
  235. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  236. * wakes them all right up.
  237. */
  238. static int cpuidle_latency_notify(struct notifier_block *b,
  239. unsigned long l, void *v)
  240. {
  241. smp_call_function(smp_callback, NULL, 0, 1);
  242. return NOTIFY_OK;
  243. }
  244. static struct notifier_block cpuidle_latency_notifier = {
  245. .notifier_call = cpuidle_latency_notify,
  246. };
  247. static inline void latency_notifier_init(struct notifier_block *n)
  248. {
  249. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  250. }
  251. #else /* CONFIG_SMP */
  252. #define latency_notifier_init(x) do { } while (0)
  253. #endif /* CONFIG_SMP */
  254. /**
  255. * cpuidle_init - core initializer
  256. */
  257. static int __init cpuidle_init(void)
  258. {
  259. int ret;
  260. pm_idle_old = pm_idle;
  261. ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
  262. if (ret)
  263. return ret;
  264. latency_notifier_init(&cpuidle_latency_notifier);
  265. return 0;
  266. }
  267. core_initcall(cpuidle_init);