cpuidle.c 9.0 KB

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