cpuidle.c 19 KB

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