cpuidle.c 11 KB

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