cpuidle.c 13 KB

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