cpuidle.c 8.2 KB

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