cpuidle.c 8.1 KB

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