cpuidle.c 12 KB

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