cpuidle.c 16 KB

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