cpuidle.c 8.5 KB

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