cpuidle.c 9.1 KB

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