cpuidle.c 12 KB

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