cpuidle.c 14 KB

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