cpuidle.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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/percpu-defs.h"
  11. #include <linux/clockchips.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mutex.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/clock.h>
  16. #include <linux/sched/idle.h>
  17. #include <linux/notifier.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cpuidle.h>
  21. #include <linux/ktime.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/module.h>
  24. #include <linux/suspend.h>
  25. #include <linux/tick.h>
  26. #include <linux/mmu_context.h>
  27. #include <linux/context_tracking.h>
  28. #include <trace/events/power.h>
  29. #include "cpuidle.h"
  30. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  31. DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  32. DEFINE_MUTEX(cpuidle_lock);
  33. LIST_HEAD(cpuidle_detected_devices);
  34. static int enabled_devices;
  35. static int off __read_mostly;
  36. static int initialized __read_mostly;
  37. int cpuidle_disabled(void)
  38. {
  39. return off;
  40. }
  41. void disable_cpuidle(void)
  42. {
  43. off = 1;
  44. }
  45. bool cpuidle_not_available(struct cpuidle_driver *drv,
  46. struct cpuidle_device *dev)
  47. {
  48. return off || !initialized || !drv || !dev || !dev->enabled;
  49. }
  50. /**
  51. * cpuidle_play_dead - cpu off-lining
  52. *
  53. * Returns in case of an error or no driver
  54. */
  55. int cpuidle_play_dead(void)
  56. {
  57. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  58. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  59. int i;
  60. if (!drv)
  61. return -ENODEV;
  62. for (i = drv->state_count - 1; i >= 0; i--) {
  63. if (drv->states[i].enter_dead)
  64. drv->states[i].enter_dead(dev, i);
  65. }
  66. /*
  67. * If :enter_dead() is successful, it will never return, so reaching
  68. * here means that all of them failed above or were not present.
  69. */
  70. return -ENODEV;
  71. }
  72. static int find_deepest_state(struct cpuidle_driver *drv,
  73. struct cpuidle_device *dev,
  74. u64 max_latency_ns,
  75. unsigned int forbidden_flags,
  76. bool s2idle)
  77. {
  78. u64 latency_req = 0;
  79. int i, ret = 0;
  80. for (i = 1; i < drv->state_count; i++) {
  81. struct cpuidle_state *s = &drv->states[i];
  82. if (dev->states_usage[i].disable ||
  83. s->exit_latency_ns <= latency_req ||
  84. s->exit_latency_ns > max_latency_ns ||
  85. (s->flags & forbidden_flags) ||
  86. (s2idle && !s->enter_s2idle))
  87. continue;
  88. latency_req = s->exit_latency_ns;
  89. ret = i;
  90. }
  91. return ret;
  92. }
  93. /**
  94. * cpuidle_use_deepest_state - Set/unset governor override mode.
  95. * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
  96. *
  97. * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
  98. * state with exit latency within @latency_limit_ns (override governors going
  99. * forward), or do not override governors if it is zero.
  100. */
  101. void cpuidle_use_deepest_state(u64 latency_limit_ns)
  102. {
  103. struct cpuidle_device *dev;
  104. preempt_disable();
  105. dev = cpuidle_get_device();
  106. if (dev)
  107. dev->forced_idle_latency_limit_ns = latency_limit_ns;
  108. preempt_enable();
  109. }
  110. /**
  111. * cpuidle_find_deepest_state - Find the deepest available idle state.
  112. * @drv: cpuidle driver for the given CPU.
  113. * @dev: cpuidle device for the given CPU.
  114. * @latency_limit_ns: Idle state exit latency limit
  115. *
  116. * Return: the index of the deepest available idle state.
  117. */
  118. int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  119. struct cpuidle_device *dev,
  120. u64 latency_limit_ns)
  121. {
  122. return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
  123. }
  124. #ifdef CONFIG_SUSPEND
  125. static noinstr void enter_s2idle_proper(struct cpuidle_driver *drv,
  126. struct cpuidle_device *dev, int index)
  127. {
  128. struct cpuidle_state *target_state = &drv->states[index];
  129. ktime_t time_start, time_end;
  130. instrumentation_begin();
  131. time_start = ns_to_ktime(local_clock_noinstr());
  132. tick_freeze();
  133. /*
  134. * The state used here cannot be a "coupled" one, because the "coupled"
  135. * cpuidle mechanism enables interrupts and doing that with timekeeping
  136. * suspended is generally unsafe.
  137. */
  138. stop_critical_timings();
  139. if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
  140. ct_cpuidle_enter();
  141. /* Annotate away the indirect call */
  142. instrumentation_begin();
  143. }
  144. target_state->enter_s2idle(dev, drv, index);
  145. if (WARN_ON_ONCE(!irqs_disabled()))
  146. raw_local_irq_disable();
  147. if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
  148. instrumentation_end();
  149. ct_cpuidle_exit();
  150. }
  151. tick_unfreeze();
  152. start_critical_timings();
  153. time_end = ns_to_ktime(local_clock_noinstr());
  154. dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
  155. dev->states_usage[index].s2idle_usage++;
  156. instrumentation_end();
  157. }
  158. /**
  159. * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
  160. * @drv: cpuidle driver for the given CPU.
  161. * @dev: cpuidle device for the given CPU.
  162. * @latency_limit_ns: Idle state exit latency limit
  163. *
  164. * If there are states with the ->enter_s2idle callback, find the deepest of
  165. * them and enter it with frozen tick.
  166. */
  167. int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  168. u64 latency_limit_ns)
  169. {
  170. int index;
  171. /*
  172. * Find the deepest state with ->enter_s2idle present that meets the
  173. * specified latency limit, which guarantees that interrupts won't be
  174. * enabled when it exits and allows the tick to be frozen safely.
  175. */
  176. index = find_deepest_state(drv, dev, latency_limit_ns, 0, true);
  177. if (index > 0) {
  178. enter_s2idle_proper(drv, dev, index);
  179. local_irq_enable();
  180. }
  181. return index;
  182. }
  183. #endif /* CONFIG_SUSPEND */
  184. /**
  185. * cpuidle_enter_state - enter the state and update stats
  186. * @dev: cpuidle device for this cpu
  187. * @drv: cpuidle driver for this cpu
  188. * @index: index into the states table in @drv of the state to enter
  189. */
  190. noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
  191. struct cpuidle_driver *drv,
  192. int index)
  193. {
  194. int entered_state;
  195. struct cpuidle_state *target_state = &drv->states[index];
  196. bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
  197. ktime_t time_start, time_end;
  198. instrumentation_begin();
  199. /*
  200. * Tell the time framework to switch to a broadcast timer because our
  201. * local timer will be shut down. If a local timer is used from another
  202. * CPU as a broadcast timer, this call may fail if it is not available.
  203. */
  204. if (broadcast && tick_broadcast_enter()) {
  205. index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
  206. CPUIDLE_FLAG_TIMER_STOP, false);
  207. target_state = &drv->states[index];
  208. broadcast = false;
  209. }
  210. if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  211. leave_mm();
  212. /* Take note of the planned idle state. */
  213. sched_idle_set_state(target_state);
  214. trace_cpu_idle(index, dev->cpu);
  215. time_start = ns_to_ktime(local_clock_noinstr());
  216. stop_critical_timings();
  217. if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
  218. ct_cpuidle_enter();
  219. /* Annotate away the indirect call */
  220. instrumentation_begin();
  221. }
  222. /*
  223. * NOTE!!
  224. *
  225. * For cpuidle_state::enter() methods that do *NOT* set
  226. * CPUIDLE_FLAG_RCU_IDLE RCU will be disabled here and these functions
  227. * must be marked either noinstr or __cpuidle.
  228. *
  229. * For cpuidle_state::enter() methods that *DO* set
  230. * CPUIDLE_FLAG_RCU_IDLE this isn't required, but they must mark the
  231. * function calling ct_cpuidle_enter() as noinstr/__cpuidle and all
  232. * functions called within the RCU-idle region.
  233. */
  234. entered_state = target_state->enter(dev, drv, index);
  235. if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter))
  236. raw_local_irq_disable();
  237. if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
  238. instrumentation_end();
  239. ct_cpuidle_exit();
  240. }
  241. start_critical_timings();
  242. sched_clock_idle_wakeup_event();
  243. time_end = ns_to_ktime(local_clock_noinstr());
  244. trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
  245. /* The cpu is no longer idle or about to enter idle. */
  246. sched_idle_set_state(NULL);
  247. if (broadcast)
  248. tick_broadcast_exit();
  249. if (!cpuidle_state_is_coupled(drv, index))
  250. local_irq_enable();
  251. if (entered_state >= 0) {
  252. s64 diff, delay = drv->states[entered_state].exit_latency_ns;
  253. int i;
  254. /*
  255. * Update cpuidle counters
  256. * This can be moved to within driver enter routine,
  257. * but that results in multiple copies of same code.
  258. */
  259. diff = ktime_sub(time_end, time_start);
  260. dev->last_residency_ns = diff;
  261. dev->states_usage[entered_state].time_ns += diff;
  262. dev->states_usage[entered_state].usage++;
  263. if (diff < drv->states[entered_state].target_residency_ns) {
  264. for (i = entered_state - 1; i >= 0; i--) {
  265. if (dev->states_usage[i].disable)
  266. continue;
  267. /* Shallower states are enabled, so update. */
  268. dev->states_usage[entered_state].above++;
  269. trace_cpu_idle_miss(dev->cpu, entered_state, false);
  270. break;
  271. }
  272. } else if (diff > delay) {
  273. for (i = entered_state + 1; i < drv->state_count; i++) {
  274. if (dev->states_usage[i].disable)
  275. continue;
  276. /*
  277. * Update if a deeper state would have been a
  278. * better match for the observed idle duration.
  279. */
  280. if (diff - delay >= drv->states[i].target_residency_ns) {
  281. dev->states_usage[entered_state].below++;
  282. trace_cpu_idle_miss(dev->cpu, entered_state, true);
  283. }
  284. break;
  285. }
  286. }
  287. } else {
  288. dev->last_residency_ns = 0;
  289. dev->states_usage[index].rejected++;
  290. }
  291. instrumentation_end();
  292. return entered_state;
  293. }
  294. /**
  295. * cpuidle_select - ask the cpuidle framework to choose an idle state
  296. *
  297. * @drv: the cpuidle driver
  298. * @dev: the cpuidle device
  299. * @stop_tick: indication on whether or not to stop the tick
  300. *
  301. * Returns the index of the idle state. The return value must not be negative.
  302. *
  303. * The memory location pointed to by @stop_tick is expected to be written the
  304. * 'false' boolean value if the scheduler tick should not be stopped before
  305. * entering the returned state.
  306. */
  307. int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  308. bool *stop_tick)
  309. {
  310. return cpuidle_curr_governor->select(drv, dev, stop_tick);
  311. }
  312. /**
  313. * cpuidle_enter - enter into the specified idle state
  314. *
  315. * @drv: the cpuidle driver tied with the cpu
  316. * @dev: the cpuidle device
  317. * @index: the index in the idle state table
  318. *
  319. * Returns the index in the idle state, < 0 in case of error.
  320. * The error code depends on the backend driver
  321. */
  322. int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
  323. int index)
  324. {
  325. int ret = 0;
  326. /*
  327. * Store the next hrtimer, which becomes either next tick or the next
  328. * timer event, whatever expires first. Additionally, to make this data
  329. * useful for consumers outside cpuidle, we rely on that the governor's
  330. * ->select() callback have decided, whether to stop the tick or not.
  331. */
  332. WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
  333. if (cpuidle_state_is_coupled(drv, index))
  334. ret = cpuidle_enter_state_coupled(dev, drv, index);
  335. else
  336. ret = cpuidle_enter_state(dev, drv, index);
  337. WRITE_ONCE(dev->next_hrtimer, 0);
  338. return ret;
  339. }
  340. /**
  341. * cpuidle_reflect - tell the underlying governor what was the state
  342. * we were in
  343. *
  344. * @dev : the cpuidle device
  345. * @index: the index in the idle state table
  346. *
  347. */
  348. void cpuidle_reflect(struct cpuidle_device *dev, int index)
  349. {
  350. if (cpuidle_curr_governor->reflect && index >= 0)
  351. cpuidle_curr_governor->reflect(dev, index);
  352. }
  353. /*
  354. * Min polling interval of 10usec is a guess. It is assuming that
  355. * for most users, the time for a single ping-pong workload like
  356. * perf bench pipe would generally complete within 10usec but
  357. * this is hardware dependent. Actual time can be estimated with
  358. *
  359. * perf bench sched pipe -l 10000
  360. *
  361. * Run multiple times to avoid cpufreq effects.
  362. */
  363. #define CPUIDLE_POLL_MIN 10000
  364. #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
  365. /**
  366. * cpuidle_poll_time - return amount of time to poll for,
  367. * governors can override dev->poll_limit_ns if necessary
  368. *
  369. * @drv: the cpuidle driver tied with the cpu
  370. * @dev: the cpuidle device
  371. *
  372. */
  373. __cpuidle u64 cpuidle_poll_time(struct cpuidle_driver *drv,
  374. struct cpuidle_device *dev)
  375. {
  376. int i;
  377. u64 limit_ns;
  378. BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
  379. if (dev->poll_limit_ns)
  380. return dev->poll_limit_ns;
  381. limit_ns = CPUIDLE_POLL_MAX;
  382. for (i = 1; i < drv->state_count; i++) {
  383. u64 state_limit;
  384. if (dev->states_usage[i].disable)
  385. continue;
  386. state_limit = drv->states[i].target_residency_ns;
  387. if (state_limit < CPUIDLE_POLL_MIN)
  388. continue;
  389. limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
  390. break;
  391. }
  392. dev->poll_limit_ns = limit_ns;
  393. return dev->poll_limit_ns;
  394. }
  395. /**
  396. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  397. */
  398. void cpuidle_install_idle_handler(void)
  399. {
  400. if (enabled_devices) {
  401. /* Make sure all changes finished before we switch to new idle */
  402. smp_wmb();
  403. initialized = 1;
  404. }
  405. }
  406. /**
  407. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  408. */
  409. void cpuidle_uninstall_idle_handler(void)
  410. {
  411. if (enabled_devices) {
  412. initialized = 0;
  413. wake_up_all_idle_cpus();
  414. }
  415. /*
  416. * Make sure external observers (such as the scheduler)
  417. * are done looking at pointed idle states.
  418. */
  419. synchronize_rcu();
  420. }
  421. /**
  422. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  423. */
  424. void cpuidle_pause_and_lock(void)
  425. {
  426. mutex_lock(&cpuidle_lock);
  427. cpuidle_uninstall_idle_handler();
  428. }
  429. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  430. /**
  431. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  432. */
  433. void cpuidle_resume_and_unlock(void)
  434. {
  435. cpuidle_install_idle_handler();
  436. mutex_unlock(&cpuidle_lock);
  437. }
  438. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  439. /* Currently used in suspend/resume path to suspend cpuidle */
  440. void cpuidle_pause(void)
  441. {
  442. mutex_lock(&cpuidle_lock);
  443. cpuidle_uninstall_idle_handler();
  444. mutex_unlock(&cpuidle_lock);
  445. }
  446. /* Currently used in suspend/resume path to resume cpuidle */
  447. void cpuidle_resume(void)
  448. {
  449. mutex_lock(&cpuidle_lock);
  450. cpuidle_install_idle_handler();
  451. mutex_unlock(&cpuidle_lock);
  452. }
  453. /**
  454. * cpuidle_enable_device - enables idle PM for a CPU
  455. * @dev: the CPU
  456. *
  457. * This function must be called between cpuidle_pause_and_lock and
  458. * cpuidle_resume_and_unlock when used externally.
  459. */
  460. int cpuidle_enable_device(struct cpuidle_device *dev)
  461. {
  462. int ret;
  463. struct cpuidle_driver *drv;
  464. if (!dev)
  465. return -EINVAL;
  466. if (dev->enabled)
  467. return 0;
  468. if (!cpuidle_curr_governor)
  469. return -EIO;
  470. drv = cpuidle_get_cpu_driver(dev);
  471. if (!drv)
  472. return -EIO;
  473. if (!dev->registered)
  474. return -EINVAL;
  475. ret = cpuidle_add_device_sysfs(dev);
  476. if (ret)
  477. return ret;
  478. if (cpuidle_curr_governor->enable) {
  479. ret = cpuidle_curr_governor->enable(drv, dev);
  480. if (ret)
  481. goto fail_sysfs;
  482. }
  483. smp_wmb();
  484. dev->enabled = 1;
  485. enabled_devices++;
  486. return 0;
  487. fail_sysfs:
  488. cpuidle_remove_device_sysfs(dev);
  489. return ret;
  490. }
  491. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  492. /**
  493. * cpuidle_disable_device - disables idle PM for a CPU
  494. * @dev: the CPU
  495. *
  496. * This function must be called between cpuidle_pause_and_lock and
  497. * cpuidle_resume_and_unlock when used externally.
  498. */
  499. void cpuidle_disable_device(struct cpuidle_device *dev)
  500. {
  501. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  502. if (!dev || !dev->enabled)
  503. return;
  504. if (!drv || !cpuidle_curr_governor)
  505. return;
  506. dev->enabled = 0;
  507. if (cpuidle_curr_governor->disable)
  508. cpuidle_curr_governor->disable(drv, dev);
  509. cpuidle_remove_device_sysfs(dev);
  510. enabled_devices--;
  511. }
  512. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  513. static void __cpuidle_unregister_device(struct cpuidle_device *dev)
  514. {
  515. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  516. list_del(&dev->device_list);
  517. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  518. module_put(drv->owner);
  519. dev->registered = 0;
  520. }
  521. static void __cpuidle_device_init(struct cpuidle_device *dev)
  522. {
  523. memset(dev->states_usage, 0, sizeof(dev->states_usage));
  524. dev->last_residency_ns = 0;
  525. dev->next_hrtimer = 0;
  526. }
  527. /**
  528. * __cpuidle_register_device - internal register function called before register
  529. * and enable routines
  530. * @dev: the cpu
  531. *
  532. * cpuidle_lock mutex must be held before this is called
  533. */
  534. static int __cpuidle_register_device(struct cpuidle_device *dev)
  535. {
  536. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  537. unsigned int cpu = dev->cpu;
  538. int i, ret;
  539. if (per_cpu(cpuidle_devices, cpu)) {
  540. pr_info("CPU%d: cpuidle device already registered\n", cpu);
  541. return -EEXIST;
  542. }
  543. if (!try_module_get(drv->owner))
  544. return -EINVAL;
  545. for (i = 0; i < drv->state_count; i++) {
  546. if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
  547. dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
  548. if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
  549. dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
  550. }
  551. per_cpu(cpuidle_devices, cpu) = dev;
  552. list_add(&dev->device_list, &cpuidle_detected_devices);
  553. ret = cpuidle_coupled_register_device(dev);
  554. if (ret)
  555. __cpuidle_unregister_device(dev);
  556. else
  557. dev->registered = 1;
  558. return ret;
  559. }
  560. /**
  561. * cpuidle_register_device - registers a CPU's idle PM feature
  562. * @dev: the cpu
  563. */
  564. int cpuidle_register_device(struct cpuidle_device *dev)
  565. {
  566. int ret = -EBUSY;
  567. if (!dev)
  568. return -EINVAL;
  569. mutex_lock(&cpuidle_lock);
  570. if (dev->registered)
  571. goto out_unlock;
  572. __cpuidle_device_init(dev);
  573. ret = __cpuidle_register_device(dev);
  574. if (ret)
  575. goto out_unlock;
  576. ret = cpuidle_add_sysfs(dev);
  577. if (ret)
  578. goto out_unregister;
  579. ret = cpuidle_enable_device(dev);
  580. if (ret)
  581. goto out_sysfs;
  582. cpuidle_install_idle_handler();
  583. out_unlock:
  584. mutex_unlock(&cpuidle_lock);
  585. return ret;
  586. out_sysfs:
  587. cpuidle_remove_sysfs(dev);
  588. out_unregister:
  589. __cpuidle_unregister_device(dev);
  590. goto out_unlock;
  591. }
  592. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  593. /**
  594. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  595. * @dev: the cpu
  596. */
  597. void cpuidle_unregister_device(struct cpuidle_device *dev)
  598. {
  599. if (!dev || dev->registered == 0)
  600. return;
  601. cpuidle_pause_and_lock();
  602. cpuidle_disable_device(dev);
  603. cpuidle_remove_sysfs(dev);
  604. __cpuidle_unregister_device(dev);
  605. cpuidle_coupled_unregister_device(dev);
  606. cpuidle_resume_and_unlock();
  607. }
  608. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  609. /**
  610. * cpuidle_unregister: unregister a driver and the devices. This function
  611. * can be used only if the driver has been previously registered through
  612. * the cpuidle_register function.
  613. *
  614. * @drv: a valid pointer to a struct cpuidle_driver
  615. */
  616. void cpuidle_unregister(struct cpuidle_driver *drv)
  617. {
  618. int cpu;
  619. struct cpuidle_device *device;
  620. for_each_cpu(cpu, drv->cpumask) {
  621. device = &per_cpu(cpuidle_dev, cpu);
  622. cpuidle_unregister_device(device);
  623. }
  624. cpuidle_unregister_driver(drv);
  625. }
  626. EXPORT_SYMBOL_GPL(cpuidle_unregister);
  627. /**
  628. * cpuidle_register: registers the driver and the cpu devices with the
  629. * coupled_cpus passed as parameter. This function is used for all common
  630. * initialization pattern there are in the arch specific drivers. The
  631. * devices is globally defined in this file.
  632. *
  633. * @drv : a valid pointer to a struct cpuidle_driver
  634. * @coupled_cpus: a cpumask for the coupled states
  635. *
  636. * Returns 0 on success, < 0 otherwise
  637. */
  638. int cpuidle_register(struct cpuidle_driver *drv,
  639. const struct cpumask *const coupled_cpus)
  640. {
  641. int ret, cpu;
  642. struct cpuidle_device *device;
  643. ret = cpuidle_register_driver(drv);
  644. if (ret) {
  645. pr_err("failed to register cpuidle driver\n");
  646. return ret;
  647. }
  648. for_each_cpu(cpu, drv->cpumask) {
  649. device = &per_cpu(cpuidle_dev, cpu);
  650. device->cpu = cpu;
  651. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  652. /*
  653. * On multiplatform for ARM, the coupled idle states could be
  654. * enabled in the kernel even if the cpuidle driver does not
  655. * use it. Note, coupled_cpus is a struct copy.
  656. */
  657. if (coupled_cpus)
  658. device->coupled_cpus = *coupled_cpus;
  659. #endif
  660. ret = cpuidle_register_device(device);
  661. if (!ret)
  662. continue;
  663. pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
  664. cpuidle_unregister(drv);
  665. break;
  666. }
  667. return ret;
  668. }
  669. EXPORT_SYMBOL_GPL(cpuidle_register);
  670. /**
  671. * cpuidle_init - core initializer
  672. */
  673. static int __init cpuidle_init(void)
  674. {
  675. if (cpuidle_disabled())
  676. return -ENODEV;
  677. return cpuidle_add_interface();
  678. }
  679. module_param(off, int, 0444);
  680. module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
  681. core_initcall(cpuidle_init);