cpuidle.c 16 KB

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