cpuidle.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_driver();
  60. int i, dead_state = -1;
  61. int power_usage = -1;
  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 +=
  93. (unsigned long long)dev->last_residency;
  94. dev->states_usage[entered_state].usage++;
  95. } else {
  96. dev->last_residency = 0;
  97. }
  98. return entered_state;
  99. }
  100. /**
  101. * cpuidle_idle_call - the main idle loop
  102. *
  103. * NOTE: no locks or semaphores should be used here
  104. * return non-zero on failure
  105. */
  106. int cpuidle_idle_call(void)
  107. {
  108. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  109. struct cpuidle_driver *drv = cpuidle_get_driver();
  110. int next_state, entered_state;
  111. if (off)
  112. return -ENODEV;
  113. if (!initialized)
  114. return -ENODEV;
  115. /* check if the device is ready */
  116. if (!dev || !dev->enabled)
  117. return -EBUSY;
  118. /* ask the governor for the next state */
  119. next_state = cpuidle_curr_governor->select(drv, dev);
  120. if (need_resched()) {
  121. local_irq_enable();
  122. return 0;
  123. }
  124. trace_power_start_rcuidle(POWER_CSTATE, next_state, dev->cpu);
  125. trace_cpu_idle_rcuidle(next_state, 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. trace_power_end_rcuidle(dev->cpu);
  132. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  133. /* give the governor an opportunity to reflect on the outcome */
  134. if (cpuidle_curr_governor->reflect)
  135. cpuidle_curr_governor->reflect(dev, entered_state);
  136. return 0;
  137. }
  138. /**
  139. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  140. */
  141. void cpuidle_install_idle_handler(void)
  142. {
  143. if (enabled_devices) {
  144. /* Make sure all changes finished before we switch to new idle */
  145. smp_wmb();
  146. initialized = 1;
  147. }
  148. }
  149. /**
  150. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  151. */
  152. void cpuidle_uninstall_idle_handler(void)
  153. {
  154. if (enabled_devices) {
  155. initialized = 0;
  156. kick_all_cpus_sync();
  157. }
  158. }
  159. /**
  160. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  161. */
  162. void cpuidle_pause_and_lock(void)
  163. {
  164. mutex_lock(&cpuidle_lock);
  165. cpuidle_uninstall_idle_handler();
  166. }
  167. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  168. /**
  169. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  170. */
  171. void cpuidle_resume_and_unlock(void)
  172. {
  173. cpuidle_install_idle_handler();
  174. mutex_unlock(&cpuidle_lock);
  175. }
  176. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  177. /* Currently used in suspend/resume path to suspend cpuidle */
  178. void cpuidle_pause(void)
  179. {
  180. mutex_lock(&cpuidle_lock);
  181. cpuidle_uninstall_idle_handler();
  182. mutex_unlock(&cpuidle_lock);
  183. }
  184. /* Currently used in suspend/resume path to resume cpuidle */
  185. void cpuidle_resume(void)
  186. {
  187. mutex_lock(&cpuidle_lock);
  188. cpuidle_install_idle_handler();
  189. mutex_unlock(&cpuidle_lock);
  190. }
  191. /**
  192. * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
  193. * @dev: pointer to a valid cpuidle_device object
  194. * @drv: pointer to a valid cpuidle_driver object
  195. * @index: index of the target cpuidle state.
  196. */
  197. int cpuidle_wrap_enter(struct cpuidle_device *dev,
  198. struct cpuidle_driver *drv, int index,
  199. int (*enter)(struct cpuidle_device *dev,
  200. struct cpuidle_driver *drv, int index))
  201. {
  202. ktime_t time_start, time_end;
  203. s64 diff;
  204. time_start = ktime_get();
  205. index = enter(dev, drv, index);
  206. time_end = ktime_get();
  207. local_irq_enable();
  208. diff = ktime_to_us(ktime_sub(time_end, time_start));
  209. if (diff > INT_MAX)
  210. diff = INT_MAX;
  211. dev->last_residency = (int) diff;
  212. return index;
  213. }
  214. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  215. static int poll_idle(struct cpuidle_device *dev,
  216. struct cpuidle_driver *drv, int index)
  217. {
  218. ktime_t t1, t2;
  219. s64 diff;
  220. t1 = ktime_get();
  221. local_irq_enable();
  222. while (!need_resched())
  223. cpu_relax();
  224. t2 = ktime_get();
  225. diff = ktime_to_us(ktime_sub(t2, t1));
  226. if (diff > INT_MAX)
  227. diff = INT_MAX;
  228. dev->last_residency = (int) diff;
  229. return index;
  230. }
  231. static void poll_idle_init(struct cpuidle_driver *drv)
  232. {
  233. struct cpuidle_state *state = &drv->states[0];
  234. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  235. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  236. state->exit_latency = 0;
  237. state->target_residency = 0;
  238. state->power_usage = -1;
  239. state->flags = 0;
  240. state->enter = poll_idle;
  241. state->disabled = false;
  242. }
  243. #else
  244. static void poll_idle_init(struct cpuidle_driver *drv) {}
  245. #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
  246. /**
  247. * cpuidle_enable_device - enables idle PM for a CPU
  248. * @dev: the CPU
  249. *
  250. * This function must be called between cpuidle_pause_and_lock and
  251. * cpuidle_resume_and_unlock when used externally.
  252. */
  253. int cpuidle_enable_device(struct cpuidle_device *dev)
  254. {
  255. int ret, i;
  256. struct cpuidle_driver *drv = cpuidle_get_driver();
  257. if (!dev)
  258. return -EINVAL;
  259. if (dev->enabled)
  260. return 0;
  261. if (!drv || !cpuidle_curr_governor)
  262. return -EIO;
  263. if (!dev->state_count)
  264. dev->state_count = drv->state_count;
  265. if (dev->registered == 0) {
  266. ret = __cpuidle_register_device(dev);
  267. if (ret)
  268. return ret;
  269. }
  270. cpuidle_enter_ops = drv->en_core_tk_irqen ?
  271. cpuidle_enter_tk : cpuidle_enter;
  272. poll_idle_init(drv);
  273. if ((ret = cpuidle_add_state_sysfs(dev)))
  274. return ret;
  275. if (cpuidle_curr_governor->enable &&
  276. (ret = cpuidle_curr_governor->enable(drv, dev)))
  277. goto fail_sysfs;
  278. for (i = 0; i < dev->state_count; i++) {
  279. dev->states_usage[i].usage = 0;
  280. dev->states_usage[i].time = 0;
  281. }
  282. dev->last_residency = 0;
  283. smp_wmb();
  284. dev->enabled = 1;
  285. enabled_devices++;
  286. return 0;
  287. fail_sysfs:
  288. cpuidle_remove_state_sysfs(dev);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  292. /**
  293. * cpuidle_disable_device - disables idle PM for a CPU
  294. * @dev: the CPU
  295. *
  296. * This function must be called between cpuidle_pause_and_lock and
  297. * cpuidle_resume_and_unlock when used externally.
  298. */
  299. void cpuidle_disable_device(struct cpuidle_device *dev)
  300. {
  301. if (!dev->enabled)
  302. return;
  303. if (!cpuidle_get_driver() || !cpuidle_curr_governor)
  304. return;
  305. dev->enabled = 0;
  306. if (cpuidle_curr_governor->disable)
  307. cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
  308. cpuidle_remove_state_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 device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
  323. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  324. if (!try_module_get(cpuidle_driver->owner))
  325. return -EINVAL;
  326. init_completion(&dev->kobj_unregister);
  327. per_cpu(cpuidle_devices, dev->cpu) = dev;
  328. list_add(&dev->device_list, &cpuidle_detected_devices);
  329. ret = cpuidle_add_sysfs(cpu_dev);
  330. if (ret)
  331. goto err_sysfs;
  332. ret = cpuidle_coupled_register_device(dev);
  333. if (ret)
  334. goto err_coupled;
  335. dev->registered = 1;
  336. return 0;
  337. err_coupled:
  338. cpuidle_remove_sysfs(cpu_dev);
  339. wait_for_completion(&dev->kobj_unregister);
  340. err_sysfs:
  341. list_del(&dev->device_list);
  342. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  343. module_put(cpuidle_driver->owner);
  344. return ret;
  345. }
  346. /**
  347. * cpuidle_register_device - registers a CPU's idle PM feature
  348. * @dev: the cpu
  349. */
  350. int cpuidle_register_device(struct cpuidle_device *dev)
  351. {
  352. int ret;
  353. if (!dev)
  354. return -EINVAL;
  355. mutex_lock(&cpuidle_lock);
  356. if ((ret = __cpuidle_register_device(dev))) {
  357. mutex_unlock(&cpuidle_lock);
  358. return ret;
  359. }
  360. cpuidle_enable_device(dev);
  361. cpuidle_install_idle_handler();
  362. mutex_unlock(&cpuidle_lock);
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  366. /**
  367. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  368. * @dev: the cpu
  369. */
  370. void cpuidle_unregister_device(struct cpuidle_device *dev)
  371. {
  372. struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
  373. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  374. if (dev->registered == 0)
  375. return;
  376. cpuidle_pause_and_lock();
  377. cpuidle_disable_device(dev);
  378. cpuidle_remove_sysfs(cpu_dev);
  379. list_del(&dev->device_list);
  380. wait_for_completion(&dev->kobj_unregister);
  381. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  382. cpuidle_coupled_unregister_device(dev);
  383. cpuidle_resume_and_unlock();
  384. module_put(cpuidle_driver->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);