cpuidle.c 15 KB

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