cpuidle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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->registered)
  241. return -EINVAL;
  242. if (!dev->state_count)
  243. dev->state_count = drv->state_count;
  244. poll_idle_init(drv);
  245. ret = cpuidle_add_device_sysfs(dev);
  246. if (ret)
  247. return ret;
  248. if (cpuidle_curr_governor->enable &&
  249. (ret = cpuidle_curr_governor->enable(drv, dev)))
  250. goto fail_sysfs;
  251. for (i = 0; i < dev->state_count; i++) {
  252. dev->states_usage[i].usage = 0;
  253. dev->states_usage[i].time = 0;
  254. }
  255. dev->last_residency = 0;
  256. smp_wmb();
  257. dev->enabled = 1;
  258. enabled_devices++;
  259. return 0;
  260. fail_sysfs:
  261. cpuidle_remove_device_sysfs(dev);
  262. return ret;
  263. }
  264. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  265. /**
  266. * cpuidle_disable_device - disables idle PM for a CPU
  267. * @dev: the CPU
  268. *
  269. * This function must be called between cpuidle_pause_and_lock and
  270. * cpuidle_resume_and_unlock when used externally.
  271. */
  272. void cpuidle_disable_device(struct cpuidle_device *dev)
  273. {
  274. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  275. if (!dev || !dev->enabled)
  276. return;
  277. if (!drv || !cpuidle_curr_governor)
  278. return;
  279. dev->enabled = 0;
  280. if (cpuidle_curr_governor->disable)
  281. cpuidle_curr_governor->disable(drv, dev);
  282. cpuidle_remove_device_sysfs(dev);
  283. enabled_devices--;
  284. }
  285. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  286. /**
  287. * __cpuidle_register_device - internal register function called before register
  288. * and enable routines
  289. * @dev: the cpu
  290. *
  291. * cpuidle_lock mutex must be held before this is called
  292. */
  293. static int __cpuidle_register_device(struct cpuidle_device *dev)
  294. {
  295. int ret;
  296. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  297. if (!try_module_get(drv->owner))
  298. return -EINVAL;
  299. per_cpu(cpuidle_devices, dev->cpu) = dev;
  300. list_add(&dev->device_list, &cpuidle_detected_devices);
  301. ret = cpuidle_add_sysfs(dev);
  302. if (ret)
  303. goto err_sysfs;
  304. ret = cpuidle_coupled_register_device(dev);
  305. if (ret)
  306. goto err_coupled;
  307. dev->registered = 1;
  308. return 0;
  309. err_coupled:
  310. cpuidle_remove_sysfs(dev);
  311. err_sysfs:
  312. list_del(&dev->device_list);
  313. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  314. module_put(drv->owner);
  315. return ret;
  316. }
  317. /**
  318. * cpuidle_register_device - registers a CPU's idle PM feature
  319. * @dev: the cpu
  320. */
  321. int cpuidle_register_device(struct cpuidle_device *dev)
  322. {
  323. int ret;
  324. if (!dev)
  325. return -EINVAL;
  326. mutex_lock(&cpuidle_lock);
  327. if ((ret = __cpuidle_register_device(dev))) {
  328. mutex_unlock(&cpuidle_lock);
  329. return ret;
  330. }
  331. ret = cpuidle_enable_device(dev);
  332. if (ret) {
  333. mutex_unlock(&cpuidle_lock);
  334. return ret;
  335. }
  336. cpuidle_install_idle_handler();
  337. mutex_unlock(&cpuidle_lock);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  341. /**
  342. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  343. * @dev: the cpu
  344. */
  345. void cpuidle_unregister_device(struct cpuidle_device *dev)
  346. {
  347. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  348. if (dev->registered == 0)
  349. return;
  350. cpuidle_pause_and_lock();
  351. cpuidle_disable_device(dev);
  352. cpuidle_remove_sysfs(dev);
  353. list_del(&dev->device_list);
  354. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  355. cpuidle_coupled_unregister_device(dev);
  356. cpuidle_resume_and_unlock();
  357. module_put(drv->owner);
  358. }
  359. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  360. /**
  361. * cpuidle_unregister: unregister a driver and the devices. This function
  362. * can be used only if the driver has been previously registered through
  363. * the cpuidle_register function.
  364. *
  365. * @drv: a valid pointer to a struct cpuidle_driver
  366. */
  367. void cpuidle_unregister(struct cpuidle_driver *drv)
  368. {
  369. int cpu;
  370. struct cpuidle_device *device;
  371. for_each_cpu(cpu, drv->cpumask) {
  372. device = &per_cpu(cpuidle_dev, cpu);
  373. cpuidle_unregister_device(device);
  374. }
  375. cpuidle_unregister_driver(drv);
  376. }
  377. EXPORT_SYMBOL_GPL(cpuidle_unregister);
  378. /**
  379. * cpuidle_register: registers the driver and the cpu devices with the
  380. * coupled_cpus passed as parameter. This function is used for all common
  381. * initialization pattern there are in the arch specific drivers. The
  382. * devices is globally defined in this file.
  383. *
  384. * @drv : a valid pointer to a struct cpuidle_driver
  385. * @coupled_cpus: a cpumask for the coupled states
  386. *
  387. * Returns 0 on success, < 0 otherwise
  388. */
  389. int cpuidle_register(struct cpuidle_driver *drv,
  390. const struct cpumask *const coupled_cpus)
  391. {
  392. int ret, cpu;
  393. struct cpuidle_device *device;
  394. ret = cpuidle_register_driver(drv);
  395. if (ret) {
  396. pr_err("failed to register cpuidle driver\n");
  397. return ret;
  398. }
  399. for_each_cpu(cpu, drv->cpumask) {
  400. device = &per_cpu(cpuidle_dev, cpu);
  401. device->cpu = cpu;
  402. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  403. /*
  404. * On multiplatform for ARM, the coupled idle states could
  405. * enabled in the kernel even if the cpuidle driver does not
  406. * use it. Note, coupled_cpus is a struct copy.
  407. */
  408. if (coupled_cpus)
  409. device->coupled_cpus = *coupled_cpus;
  410. #endif
  411. ret = cpuidle_register_device(device);
  412. if (!ret)
  413. continue;
  414. pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
  415. cpuidle_unregister(drv);
  416. break;
  417. }
  418. return ret;
  419. }
  420. EXPORT_SYMBOL_GPL(cpuidle_register);
  421. #ifdef CONFIG_SMP
  422. static void smp_callback(void *v)
  423. {
  424. /* we already woke the CPU up, nothing more to do */
  425. }
  426. /*
  427. * This function gets called when a part of the kernel has a new latency
  428. * requirement. This means we need to get all processors out of their C-state,
  429. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  430. * wakes them all right up.
  431. */
  432. static int cpuidle_latency_notify(struct notifier_block *b,
  433. unsigned long l, void *v)
  434. {
  435. smp_call_function(smp_callback, NULL, 1);
  436. return NOTIFY_OK;
  437. }
  438. static struct notifier_block cpuidle_latency_notifier = {
  439. .notifier_call = cpuidle_latency_notify,
  440. };
  441. static inline void latency_notifier_init(struct notifier_block *n)
  442. {
  443. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  444. }
  445. #else /* CONFIG_SMP */
  446. #define latency_notifier_init(x) do { } while (0)
  447. #endif /* CONFIG_SMP */
  448. /**
  449. * cpuidle_init - core initializer
  450. */
  451. static int __init cpuidle_init(void)
  452. {
  453. int ret;
  454. if (cpuidle_disabled())
  455. return -ENODEV;
  456. ret = cpuidle_add_interface(cpu_subsys.dev_root);
  457. if (ret)
  458. return ret;
  459. latency_notifier_init(&cpuidle_latency_notifier);
  460. return 0;
  461. }
  462. module_param(off, int, 0444);
  463. core_initcall(cpuidle_init);