cpuidle.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 <linux/suspend.h>
  22. #include <linux/tick.h>
  23. #include <trace/events/power.h>
  24. #include "cpuidle.h"
  25. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  26. DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  27. DEFINE_MUTEX(cpuidle_lock);
  28. LIST_HEAD(cpuidle_detected_devices);
  29. static int enabled_devices;
  30. static int off __read_mostly;
  31. static int initialized __read_mostly;
  32. int cpuidle_disabled(void)
  33. {
  34. return off;
  35. }
  36. void disable_cpuidle(void)
  37. {
  38. off = 1;
  39. }
  40. bool cpuidle_not_available(struct cpuidle_driver *drv,
  41. struct cpuidle_device *dev)
  42. {
  43. return off || !initialized || !drv || !dev || !dev->enabled;
  44. }
  45. /**
  46. * cpuidle_play_dead - cpu off-lining
  47. *
  48. * Returns in case of an error or no driver
  49. */
  50. int cpuidle_play_dead(void)
  51. {
  52. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  53. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  54. int i;
  55. if (!drv)
  56. return -ENODEV;
  57. /* Find lowest-power state that supports long-term idle */
  58. for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
  59. if (drv->states[i].enter_dead)
  60. return drv->states[i].enter_dead(dev, i);
  61. return -ENODEV;
  62. }
  63. static int find_deepest_state(struct cpuidle_driver *drv,
  64. struct cpuidle_device *dev, bool freeze)
  65. {
  66. unsigned int latency_req = 0;
  67. int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
  68. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
  69. struct cpuidle_state *s = &drv->states[i];
  70. struct cpuidle_state_usage *su = &dev->states_usage[i];
  71. if (s->disabled || su->disable || s->exit_latency <= latency_req
  72. || (freeze && !s->enter_freeze))
  73. continue;
  74. latency_req = s->exit_latency;
  75. ret = i;
  76. }
  77. return ret;
  78. }
  79. /**
  80. * cpuidle_find_deepest_state - Find the deepest available idle state.
  81. * @drv: cpuidle driver for the given CPU.
  82. * @dev: cpuidle device for the given CPU.
  83. */
  84. int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  85. struct cpuidle_device *dev)
  86. {
  87. return find_deepest_state(drv, dev, false);
  88. }
  89. static void enter_freeze_proper(struct cpuidle_driver *drv,
  90. struct cpuidle_device *dev, int index)
  91. {
  92. tick_freeze();
  93. /*
  94. * The state used here cannot be a "coupled" one, because the "coupled"
  95. * cpuidle mechanism enables interrupts and doing that with timekeeping
  96. * suspended is generally unsafe.
  97. */
  98. drv->states[index].enter_freeze(dev, drv, index);
  99. WARN_ON(!irqs_disabled());
  100. /*
  101. * timekeeping_resume() that will be called by tick_unfreeze() for the
  102. * last CPU executing it calls functions containing RCU read-side
  103. * critical sections, so tell RCU about that.
  104. */
  105. RCU_NONIDLE(tick_unfreeze());
  106. }
  107. /**
  108. * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
  109. * @drv: cpuidle driver for the given CPU.
  110. * @dev: cpuidle device for the given CPU.
  111. *
  112. * If there are states with the ->enter_freeze callback, find the deepest of
  113. * them and enter it with frozen tick.
  114. */
  115. int cpuidle_enter_freeze(struct cpuidle_driver *drv, struct cpuidle_device *dev)
  116. {
  117. int index;
  118. /*
  119. * Find the deepest state with ->enter_freeze present, which guarantees
  120. * that interrupts won't be enabled when it exits and allows the tick to
  121. * be frozen safely.
  122. */
  123. index = find_deepest_state(drv, dev, true);
  124. if (index >= 0)
  125. enter_freeze_proper(drv, dev, index);
  126. return index;
  127. }
  128. /**
  129. * cpuidle_enter_state - enter the state and update stats
  130. * @dev: cpuidle device for this cpu
  131. * @drv: cpuidle driver for this cpu
  132. * @next_state: index into drv->states of the state to enter
  133. */
  134. int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
  135. int index)
  136. {
  137. int entered_state;
  138. struct cpuidle_state *target_state = &drv->states[index];
  139. bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
  140. ktime_t time_start, time_end;
  141. s64 diff;
  142. /*
  143. * Tell the time framework to switch to a broadcast timer because our
  144. * local timer will be shut down. If a local timer is used from another
  145. * CPU as a broadcast timer, this call may fail if it is not available.
  146. */
  147. if (broadcast && tick_broadcast_enter())
  148. return -EBUSY;
  149. trace_cpu_idle_rcuidle(index, dev->cpu);
  150. time_start = ktime_get();
  151. entered_state = target_state->enter(dev, drv, index);
  152. time_end = ktime_get();
  153. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  154. if (broadcast) {
  155. if (WARN_ON_ONCE(!irqs_disabled()))
  156. local_irq_disable();
  157. tick_broadcast_exit();
  158. }
  159. if (!cpuidle_state_is_coupled(dev, drv, entered_state))
  160. local_irq_enable();
  161. diff = ktime_to_us(ktime_sub(time_end, time_start));
  162. if (diff > INT_MAX)
  163. diff = INT_MAX;
  164. dev->last_residency = (int) diff;
  165. if (entered_state >= 0) {
  166. /* Update cpuidle counters */
  167. /* This can be moved to within driver enter routine
  168. * but that results in multiple copies of same code.
  169. */
  170. dev->states_usage[entered_state].time += dev->last_residency;
  171. dev->states_usage[entered_state].usage++;
  172. } else {
  173. dev->last_residency = 0;
  174. }
  175. return entered_state;
  176. }
  177. /**
  178. * cpuidle_select - ask the cpuidle framework to choose an idle state
  179. *
  180. * @drv: the cpuidle driver
  181. * @dev: the cpuidle device
  182. *
  183. * Returns the index of the idle state.
  184. */
  185. int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
  186. {
  187. return cpuidle_curr_governor->select(drv, dev);
  188. }
  189. /**
  190. * cpuidle_enter - enter into the specified idle state
  191. *
  192. * @drv: the cpuidle driver tied with the cpu
  193. * @dev: the cpuidle device
  194. * @index: the index in the idle state table
  195. *
  196. * Returns the index in the idle state, < 0 in case of error.
  197. * The error code depends on the backend driver
  198. */
  199. int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  200. int index)
  201. {
  202. if (cpuidle_state_is_coupled(dev, drv, index))
  203. return cpuidle_enter_state_coupled(dev, drv, index);
  204. return cpuidle_enter_state(dev, drv, index);
  205. }
  206. /**
  207. * cpuidle_reflect - tell the underlying governor what was the state
  208. * we were in
  209. *
  210. * @dev : the cpuidle device
  211. * @index: the index in the idle state table
  212. *
  213. */
  214. void cpuidle_reflect(struct cpuidle_device *dev, int index)
  215. {
  216. if (cpuidle_curr_governor->reflect)
  217. cpuidle_curr_governor->reflect(dev, index);
  218. }
  219. /**
  220. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  221. */
  222. void cpuidle_install_idle_handler(void)
  223. {
  224. if (enabled_devices) {
  225. /* Make sure all changes finished before we switch to new idle */
  226. smp_wmb();
  227. initialized = 1;
  228. }
  229. }
  230. /**
  231. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  232. */
  233. void cpuidle_uninstall_idle_handler(void)
  234. {
  235. if (enabled_devices) {
  236. initialized = 0;
  237. wake_up_all_idle_cpus();
  238. }
  239. /*
  240. * Make sure external observers (such as the scheduler)
  241. * are done looking at pointed idle states.
  242. */
  243. synchronize_rcu();
  244. }
  245. /**
  246. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  247. */
  248. void cpuidle_pause_and_lock(void)
  249. {
  250. mutex_lock(&cpuidle_lock);
  251. cpuidle_uninstall_idle_handler();
  252. }
  253. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  254. /**
  255. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  256. */
  257. void cpuidle_resume_and_unlock(void)
  258. {
  259. cpuidle_install_idle_handler();
  260. mutex_unlock(&cpuidle_lock);
  261. }
  262. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  263. /* Currently used in suspend/resume path to suspend cpuidle */
  264. void cpuidle_pause(void)
  265. {
  266. mutex_lock(&cpuidle_lock);
  267. cpuidle_uninstall_idle_handler();
  268. mutex_unlock(&cpuidle_lock);
  269. }
  270. /* Currently used in suspend/resume path to resume cpuidle */
  271. void cpuidle_resume(void)
  272. {
  273. mutex_lock(&cpuidle_lock);
  274. cpuidle_install_idle_handler();
  275. mutex_unlock(&cpuidle_lock);
  276. }
  277. /**
  278. * cpuidle_enable_device - enables idle PM for a CPU
  279. * @dev: the CPU
  280. *
  281. * This function must be called between cpuidle_pause_and_lock and
  282. * cpuidle_resume_and_unlock when used externally.
  283. */
  284. int cpuidle_enable_device(struct cpuidle_device *dev)
  285. {
  286. int ret;
  287. struct cpuidle_driver *drv;
  288. if (!dev)
  289. return -EINVAL;
  290. if (dev->enabled)
  291. return 0;
  292. drv = cpuidle_get_cpu_driver(dev);
  293. if (!drv || !cpuidle_curr_governor)
  294. return -EIO;
  295. if (!dev->registered)
  296. return -EINVAL;
  297. ret = cpuidle_add_device_sysfs(dev);
  298. if (ret)
  299. return ret;
  300. if (cpuidle_curr_governor->enable &&
  301. (ret = cpuidle_curr_governor->enable(drv, dev)))
  302. goto fail_sysfs;
  303. smp_wmb();
  304. dev->enabled = 1;
  305. enabled_devices++;
  306. return 0;
  307. fail_sysfs:
  308. cpuidle_remove_device_sysfs(dev);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  312. /**
  313. * cpuidle_disable_device - disables idle PM for a CPU
  314. * @dev: the CPU
  315. *
  316. * This function must be called between cpuidle_pause_and_lock and
  317. * cpuidle_resume_and_unlock when used externally.
  318. */
  319. void cpuidle_disable_device(struct cpuidle_device *dev)
  320. {
  321. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  322. if (!dev || !dev->enabled)
  323. return;
  324. if (!drv || !cpuidle_curr_governor)
  325. return;
  326. dev->enabled = 0;
  327. if (cpuidle_curr_governor->disable)
  328. cpuidle_curr_governor->disable(drv, dev);
  329. cpuidle_remove_device_sysfs(dev);
  330. enabled_devices--;
  331. }
  332. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  333. static void __cpuidle_unregister_device(struct cpuidle_device *dev)
  334. {
  335. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  336. list_del(&dev->device_list);
  337. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  338. module_put(drv->owner);
  339. }
  340. static void __cpuidle_device_init(struct cpuidle_device *dev)
  341. {
  342. memset(dev->states_usage, 0, sizeof(dev->states_usage));
  343. dev->last_residency = 0;
  344. }
  345. /**
  346. * __cpuidle_register_device - internal register function called before register
  347. * and enable routines
  348. * @dev: the cpu
  349. *
  350. * cpuidle_lock mutex must be held before this is called
  351. */
  352. static int __cpuidle_register_device(struct cpuidle_device *dev)
  353. {
  354. int ret;
  355. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  356. if (!try_module_get(drv->owner))
  357. return -EINVAL;
  358. per_cpu(cpuidle_devices, dev->cpu) = dev;
  359. list_add(&dev->device_list, &cpuidle_detected_devices);
  360. ret = cpuidle_coupled_register_device(dev);
  361. if (ret)
  362. __cpuidle_unregister_device(dev);
  363. else
  364. dev->registered = 1;
  365. return ret;
  366. }
  367. /**
  368. * cpuidle_register_device - registers a CPU's idle PM feature
  369. * @dev: the cpu
  370. */
  371. int cpuidle_register_device(struct cpuidle_device *dev)
  372. {
  373. int ret = -EBUSY;
  374. if (!dev)
  375. return -EINVAL;
  376. mutex_lock(&cpuidle_lock);
  377. if (dev->registered)
  378. goto out_unlock;
  379. __cpuidle_device_init(dev);
  380. ret = __cpuidle_register_device(dev);
  381. if (ret)
  382. goto out_unlock;
  383. ret = cpuidle_add_sysfs(dev);
  384. if (ret)
  385. goto out_unregister;
  386. ret = cpuidle_enable_device(dev);
  387. if (ret)
  388. goto out_sysfs;
  389. cpuidle_install_idle_handler();
  390. out_unlock:
  391. mutex_unlock(&cpuidle_lock);
  392. return ret;
  393. out_sysfs:
  394. cpuidle_remove_sysfs(dev);
  395. out_unregister:
  396. __cpuidle_unregister_device(dev);
  397. goto out_unlock;
  398. }
  399. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  400. /**
  401. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  402. * @dev: the cpu
  403. */
  404. void cpuidle_unregister_device(struct cpuidle_device *dev)
  405. {
  406. if (!dev || dev->registered == 0)
  407. return;
  408. cpuidle_pause_and_lock();
  409. cpuidle_disable_device(dev);
  410. cpuidle_remove_sysfs(dev);
  411. __cpuidle_unregister_device(dev);
  412. cpuidle_coupled_unregister_device(dev);
  413. cpuidle_resume_and_unlock();
  414. }
  415. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  416. /**
  417. * cpuidle_unregister: unregister a driver and the devices. This function
  418. * can be used only if the driver has been previously registered through
  419. * the cpuidle_register function.
  420. *
  421. * @drv: a valid pointer to a struct cpuidle_driver
  422. */
  423. void cpuidle_unregister(struct cpuidle_driver *drv)
  424. {
  425. int cpu;
  426. struct cpuidle_device *device;
  427. for_each_cpu(cpu, drv->cpumask) {
  428. device = &per_cpu(cpuidle_dev, cpu);
  429. cpuidle_unregister_device(device);
  430. }
  431. cpuidle_unregister_driver(drv);
  432. }
  433. EXPORT_SYMBOL_GPL(cpuidle_unregister);
  434. /**
  435. * cpuidle_register: registers the driver and the cpu devices with the
  436. * coupled_cpus passed as parameter. This function is used for all common
  437. * initialization pattern there are in the arch specific drivers. The
  438. * devices is globally defined in this file.
  439. *
  440. * @drv : a valid pointer to a struct cpuidle_driver
  441. * @coupled_cpus: a cpumask for the coupled states
  442. *
  443. * Returns 0 on success, < 0 otherwise
  444. */
  445. int cpuidle_register(struct cpuidle_driver *drv,
  446. const struct cpumask *const coupled_cpus)
  447. {
  448. int ret, cpu;
  449. struct cpuidle_device *device;
  450. ret = cpuidle_register_driver(drv);
  451. if (ret) {
  452. pr_err("failed to register cpuidle driver\n");
  453. return ret;
  454. }
  455. for_each_cpu(cpu, drv->cpumask) {
  456. device = &per_cpu(cpuidle_dev, cpu);
  457. device->cpu = cpu;
  458. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  459. /*
  460. * On multiplatform for ARM, the coupled idle states could be
  461. * enabled in the kernel even if the cpuidle driver does not
  462. * use it. Note, coupled_cpus is a struct copy.
  463. */
  464. if (coupled_cpus)
  465. device->coupled_cpus = *coupled_cpus;
  466. #endif
  467. ret = cpuidle_register_device(device);
  468. if (!ret)
  469. continue;
  470. pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
  471. cpuidle_unregister(drv);
  472. break;
  473. }
  474. return ret;
  475. }
  476. EXPORT_SYMBOL_GPL(cpuidle_register);
  477. #ifdef CONFIG_SMP
  478. /*
  479. * This function gets called when a part of the kernel has a new latency
  480. * requirement. This means we need to get all processors out of their C-state,
  481. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  482. * wakes them all right up.
  483. */
  484. static int cpuidle_latency_notify(struct notifier_block *b,
  485. unsigned long l, void *v)
  486. {
  487. wake_up_all_idle_cpus();
  488. return NOTIFY_OK;
  489. }
  490. static struct notifier_block cpuidle_latency_notifier = {
  491. .notifier_call = cpuidle_latency_notify,
  492. };
  493. static inline void latency_notifier_init(struct notifier_block *n)
  494. {
  495. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  496. }
  497. #else /* CONFIG_SMP */
  498. #define latency_notifier_init(x) do { } while (0)
  499. #endif /* CONFIG_SMP */
  500. /**
  501. * cpuidle_init - core initializer
  502. */
  503. static int __init cpuidle_init(void)
  504. {
  505. int ret;
  506. if (cpuidle_disabled())
  507. return -ENODEV;
  508. ret = cpuidle_add_interface(cpu_subsys.dev_root);
  509. if (ret)
  510. return ret;
  511. latency_notifier_init(&cpuidle_latency_notifier);
  512. return 0;
  513. }
  514. module_param(off, int, 0444);
  515. core_initcall(cpuidle_init);