cpuidle.c 8.5 KB

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