cpuidle.c 14 KB

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