cpuidle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 <trace/events/power.h>
  22. #include "cpuidle.h"
  23. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  24. DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  25. DEFINE_MUTEX(cpuidle_lock);
  26. LIST_HEAD(cpuidle_detected_devices);
  27. static int enabled_devices;
  28. static int off __read_mostly;
  29. static int initialized __read_mostly;
  30. static bool use_deepest_state __read_mostly;
  31. int cpuidle_disabled(void)
  32. {
  33. return off;
  34. }
  35. void disable_cpuidle(void)
  36. {
  37. off = 1;
  38. }
  39. /**
  40. * cpuidle_play_dead - cpu off-lining
  41. *
  42. * Returns in case of an error or no driver
  43. */
  44. int cpuidle_play_dead(void)
  45. {
  46. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  47. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  48. int i;
  49. if (!drv)
  50. return -ENODEV;
  51. /* Find lowest-power state that supports long-term idle */
  52. for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
  53. if (drv->states[i].enter_dead)
  54. return drv->states[i].enter_dead(dev, i);
  55. return -ENODEV;
  56. }
  57. /**
  58. * cpuidle_use_deepest_state - Enable/disable the "deepest idle" mode.
  59. * @enable: Whether enable or disable the feature.
  60. *
  61. * If the "deepest idle" mode is enabled, cpuidle will ignore the governor and
  62. * always use the state with the greatest exit latency (out of the states that
  63. * are not disabled).
  64. *
  65. * This function can only be called after cpuidle_pause() to avoid races.
  66. */
  67. void cpuidle_use_deepest_state(bool enable)
  68. {
  69. use_deepest_state = enable;
  70. }
  71. /**
  72. * cpuidle_find_deepest_state - Find the state of the greatest exit latency.
  73. * @drv: cpuidle driver for a given CPU.
  74. * @dev: cpuidle device for a given CPU.
  75. */
  76. static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  77. struct cpuidle_device *dev)
  78. {
  79. unsigned int latency_req = 0;
  80. int i, ret = CPUIDLE_DRIVER_STATE_START - 1;
  81. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
  82. struct cpuidle_state *s = &drv->states[i];
  83. struct cpuidle_state_usage *su = &dev->states_usage[i];
  84. if (s->disabled || su->disable || s->exit_latency <= latency_req)
  85. continue;
  86. latency_req = s->exit_latency;
  87. ret = i;
  88. }
  89. return ret;
  90. }
  91. /**
  92. * cpuidle_enter_state - enter the state and update stats
  93. * @dev: cpuidle device for this cpu
  94. * @drv: cpuidle driver for this cpu
  95. * @next_state: index into drv->states of the state to enter
  96. */
  97. int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
  98. int index)
  99. {
  100. int entered_state;
  101. struct cpuidle_state *target_state = &drv->states[index];
  102. ktime_t time_start, time_end;
  103. s64 diff;
  104. trace_cpu_idle_rcuidle(index, dev->cpu);
  105. time_start = ktime_get();
  106. entered_state = target_state->enter(dev, drv, index);
  107. time_end = ktime_get();
  108. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  109. if (!cpuidle_state_is_coupled(dev, drv, entered_state))
  110. local_irq_enable();
  111. diff = ktime_to_us(ktime_sub(time_end, time_start));
  112. if (diff > INT_MAX)
  113. diff = INT_MAX;
  114. dev->last_residency = (int) diff;
  115. if (entered_state >= 0) {
  116. /* Update cpuidle counters */
  117. /* This can be moved to within driver enter routine
  118. * but that results in multiple copies of same code.
  119. */
  120. dev->states_usage[entered_state].time += dev->last_residency;
  121. dev->states_usage[entered_state].usage++;
  122. } else {
  123. dev->last_residency = 0;
  124. }
  125. return entered_state;
  126. }
  127. /**
  128. * cpuidle_select - ask the cpuidle framework to choose an idle state
  129. *
  130. * @drv: the cpuidle driver
  131. * @dev: the cpuidle device
  132. *
  133. * Returns the index of the idle state.
  134. */
  135. int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
  136. {
  137. if (off || !initialized)
  138. return -ENODEV;
  139. if (!drv || !dev || !dev->enabled)
  140. return -EBUSY;
  141. if (unlikely(use_deepest_state))
  142. return cpuidle_find_deepest_state(drv, dev);
  143. return cpuidle_curr_governor->select(drv, dev);
  144. }
  145. /**
  146. * cpuidle_enter - enter into the specified idle state
  147. *
  148. * @drv: the cpuidle driver tied with the cpu
  149. * @dev: the cpuidle device
  150. * @index: the index in the idle state table
  151. *
  152. * Returns the index in the idle state, < 0 in case of error.
  153. * The error code depends on the backend driver
  154. */
  155. int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  156. int index)
  157. {
  158. if (cpuidle_state_is_coupled(dev, drv, index))
  159. return cpuidle_enter_state_coupled(dev, drv, index);
  160. return cpuidle_enter_state(dev, drv, index);
  161. }
  162. /**
  163. * cpuidle_reflect - tell the underlying governor what was the state
  164. * we were in
  165. *
  166. * @dev : the cpuidle device
  167. * @index: the index in the idle state table
  168. *
  169. */
  170. void cpuidle_reflect(struct cpuidle_device *dev, int index)
  171. {
  172. if (cpuidle_curr_governor->reflect && !unlikely(use_deepest_state))
  173. cpuidle_curr_governor->reflect(dev, index);
  174. }
  175. /**
  176. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  177. */
  178. void cpuidle_install_idle_handler(void)
  179. {
  180. if (enabled_devices) {
  181. /* Make sure all changes finished before we switch to new idle */
  182. smp_wmb();
  183. initialized = 1;
  184. }
  185. }
  186. /**
  187. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  188. */
  189. void cpuidle_uninstall_idle_handler(void)
  190. {
  191. if (enabled_devices) {
  192. initialized = 0;
  193. wake_up_all_idle_cpus();
  194. }
  195. /*
  196. * Make sure external observers (such as the scheduler)
  197. * are done looking at pointed idle states.
  198. */
  199. synchronize_rcu();
  200. }
  201. /**
  202. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  203. */
  204. void cpuidle_pause_and_lock(void)
  205. {
  206. mutex_lock(&cpuidle_lock);
  207. cpuidle_uninstall_idle_handler();
  208. }
  209. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  210. /**
  211. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  212. */
  213. void cpuidle_resume_and_unlock(void)
  214. {
  215. cpuidle_install_idle_handler();
  216. mutex_unlock(&cpuidle_lock);
  217. }
  218. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  219. /* Currently used in suspend/resume path to suspend cpuidle */
  220. void cpuidle_pause(void)
  221. {
  222. mutex_lock(&cpuidle_lock);
  223. cpuidle_uninstall_idle_handler();
  224. mutex_unlock(&cpuidle_lock);
  225. }
  226. /* Currently used in suspend/resume path to resume cpuidle */
  227. void cpuidle_resume(void)
  228. {
  229. mutex_lock(&cpuidle_lock);
  230. cpuidle_install_idle_handler();
  231. mutex_unlock(&cpuidle_lock);
  232. }
  233. /**
  234. * cpuidle_enable_device - enables idle PM for a CPU
  235. * @dev: the CPU
  236. *
  237. * This function must be called between cpuidle_pause_and_lock and
  238. * cpuidle_resume_and_unlock when used externally.
  239. */
  240. int cpuidle_enable_device(struct cpuidle_device *dev)
  241. {
  242. int ret;
  243. struct cpuidle_driver *drv;
  244. if (!dev)
  245. return -EINVAL;
  246. if (dev->enabled)
  247. return 0;
  248. drv = cpuidle_get_cpu_driver(dev);
  249. if (!drv || !cpuidle_curr_governor)
  250. return -EIO;
  251. if (!dev->registered)
  252. return -EINVAL;
  253. if (!dev->state_count)
  254. dev->state_count = drv->state_count;
  255. ret = cpuidle_add_device_sysfs(dev);
  256. if (ret)
  257. return ret;
  258. if (cpuidle_curr_governor->enable &&
  259. (ret = cpuidle_curr_governor->enable(drv, dev)))
  260. goto fail_sysfs;
  261. smp_wmb();
  262. dev->enabled = 1;
  263. enabled_devices++;
  264. return 0;
  265. fail_sysfs:
  266. cpuidle_remove_device_sysfs(dev);
  267. return ret;
  268. }
  269. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  270. /**
  271. * cpuidle_disable_device - disables idle PM for a CPU
  272. * @dev: the CPU
  273. *
  274. * This function must be called between cpuidle_pause_and_lock and
  275. * cpuidle_resume_and_unlock when used externally.
  276. */
  277. void cpuidle_disable_device(struct cpuidle_device *dev)
  278. {
  279. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  280. if (!dev || !dev->enabled)
  281. return;
  282. if (!drv || !cpuidle_curr_governor)
  283. return;
  284. dev->enabled = 0;
  285. if (cpuidle_curr_governor->disable)
  286. cpuidle_curr_governor->disable(drv, dev);
  287. cpuidle_remove_device_sysfs(dev);
  288. enabled_devices--;
  289. }
  290. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  291. static void __cpuidle_unregister_device(struct cpuidle_device *dev)
  292. {
  293. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  294. list_del(&dev->device_list);
  295. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  296. module_put(drv->owner);
  297. }
  298. static void __cpuidle_device_init(struct cpuidle_device *dev)
  299. {
  300. memset(dev->states_usage, 0, sizeof(dev->states_usage));
  301. dev->last_residency = 0;
  302. }
  303. /**
  304. * __cpuidle_register_device - internal register function called before register
  305. * and enable routines
  306. * @dev: the cpu
  307. *
  308. * cpuidle_lock mutex must be held before this is called
  309. */
  310. static int __cpuidle_register_device(struct cpuidle_device *dev)
  311. {
  312. int ret;
  313. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  314. if (!try_module_get(drv->owner))
  315. return -EINVAL;
  316. per_cpu(cpuidle_devices, dev->cpu) = dev;
  317. list_add(&dev->device_list, &cpuidle_detected_devices);
  318. ret = cpuidle_coupled_register_device(dev);
  319. if (ret)
  320. __cpuidle_unregister_device(dev);
  321. else
  322. dev->registered = 1;
  323. return ret;
  324. }
  325. /**
  326. * cpuidle_register_device - registers a CPU's idle PM feature
  327. * @dev: the cpu
  328. */
  329. int cpuidle_register_device(struct cpuidle_device *dev)
  330. {
  331. int ret = -EBUSY;
  332. if (!dev)
  333. return -EINVAL;
  334. mutex_lock(&cpuidle_lock);
  335. if (dev->registered)
  336. goto out_unlock;
  337. __cpuidle_device_init(dev);
  338. ret = __cpuidle_register_device(dev);
  339. if (ret)
  340. goto out_unlock;
  341. ret = cpuidle_add_sysfs(dev);
  342. if (ret)
  343. goto out_unregister;
  344. ret = cpuidle_enable_device(dev);
  345. if (ret)
  346. goto out_sysfs;
  347. cpuidle_install_idle_handler();
  348. out_unlock:
  349. mutex_unlock(&cpuidle_lock);
  350. return ret;
  351. out_sysfs:
  352. cpuidle_remove_sysfs(dev);
  353. out_unregister:
  354. __cpuidle_unregister_device(dev);
  355. goto out_unlock;
  356. }
  357. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  358. /**
  359. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  360. * @dev: the cpu
  361. */
  362. void cpuidle_unregister_device(struct cpuidle_device *dev)
  363. {
  364. if (!dev || dev->registered == 0)
  365. return;
  366. cpuidle_pause_and_lock();
  367. cpuidle_disable_device(dev);
  368. cpuidle_remove_sysfs(dev);
  369. __cpuidle_unregister_device(dev);
  370. cpuidle_coupled_unregister_device(dev);
  371. cpuidle_resume_and_unlock();
  372. }
  373. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  374. /**
  375. * cpuidle_unregister: unregister a driver and the devices. This function
  376. * can be used only if the driver has been previously registered through
  377. * the cpuidle_register function.
  378. *
  379. * @drv: a valid pointer to a struct cpuidle_driver
  380. */
  381. void cpuidle_unregister(struct cpuidle_driver *drv)
  382. {
  383. int cpu;
  384. struct cpuidle_device *device;
  385. for_each_cpu(cpu, drv->cpumask) {
  386. device = &per_cpu(cpuidle_dev, cpu);
  387. cpuidle_unregister_device(device);
  388. }
  389. cpuidle_unregister_driver(drv);
  390. }
  391. EXPORT_SYMBOL_GPL(cpuidle_unregister);
  392. /**
  393. * cpuidle_register: registers the driver and the cpu devices with the
  394. * coupled_cpus passed as parameter. This function is used for all common
  395. * initialization pattern there are in the arch specific drivers. The
  396. * devices is globally defined in this file.
  397. *
  398. * @drv : a valid pointer to a struct cpuidle_driver
  399. * @coupled_cpus: a cpumask for the coupled states
  400. *
  401. * Returns 0 on success, < 0 otherwise
  402. */
  403. int cpuidle_register(struct cpuidle_driver *drv,
  404. const struct cpumask *const coupled_cpus)
  405. {
  406. int ret, cpu;
  407. struct cpuidle_device *device;
  408. ret = cpuidle_register_driver(drv);
  409. if (ret) {
  410. pr_err("failed to register cpuidle driver\n");
  411. return ret;
  412. }
  413. for_each_cpu(cpu, drv->cpumask) {
  414. device = &per_cpu(cpuidle_dev, cpu);
  415. device->cpu = cpu;
  416. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  417. /*
  418. * On multiplatform for ARM, the coupled idle states could be
  419. * enabled in the kernel even if the cpuidle driver does not
  420. * use it. Note, coupled_cpus is a struct copy.
  421. */
  422. if (coupled_cpus)
  423. device->coupled_cpus = *coupled_cpus;
  424. #endif
  425. ret = cpuidle_register_device(device);
  426. if (!ret)
  427. continue;
  428. pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
  429. cpuidle_unregister(drv);
  430. break;
  431. }
  432. return ret;
  433. }
  434. EXPORT_SYMBOL_GPL(cpuidle_register);
  435. #ifdef CONFIG_SMP
  436. /*
  437. * This function gets called when a part of the kernel has a new latency
  438. * requirement. This means we need to get all processors out of their C-state,
  439. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  440. * wakes them all right up.
  441. */
  442. static int cpuidle_latency_notify(struct notifier_block *b,
  443. unsigned long l, void *v)
  444. {
  445. wake_up_all_idle_cpus();
  446. return NOTIFY_OK;
  447. }
  448. static struct notifier_block cpuidle_latency_notifier = {
  449. .notifier_call = cpuidle_latency_notify,
  450. };
  451. static inline void latency_notifier_init(struct notifier_block *n)
  452. {
  453. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  454. }
  455. #else /* CONFIG_SMP */
  456. #define latency_notifier_init(x) do { } while (0)
  457. #endif /* CONFIG_SMP */
  458. /**
  459. * cpuidle_init - core initializer
  460. */
  461. static int __init cpuidle_init(void)
  462. {
  463. int ret;
  464. if (cpuidle_disabled())
  465. return -ENODEV;
  466. ret = cpuidle_add_interface(cpu_subsys.dev_root);
  467. if (ret)
  468. return ret;
  469. latency_notifier_init(&cpuidle_latency_notifier);
  470. return 0;
  471. }
  472. module_param(off, int, 0444);
  473. core_initcall(cpuidle_init);