cpuidle.c 18 KB

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