cpuidle.c 12 KB

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