cpuidle.c 10 KB

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