cpuidle.c 12 KB

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