cpuidle.c 16 KB

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