cpuidle.c 15 KB

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