cpuidle.c 19 KB

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