coupled.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * coupled.c - helper functions to enter the same idle state on multiple cpus
  3. *
  4. * Copyright (c) 2011 Google, Inc.
  5. *
  6. * Author: Colin Cross <ccross@android.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cpuidle.h>
  21. #include <linux/mutex.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include "cpuidle.h"
  26. /**
  27. * DOC: Coupled cpuidle states
  28. *
  29. * On some ARM SMP SoCs (OMAP4460, Tegra 2, and probably more), the
  30. * cpus cannot be independently powered down, either due to
  31. * sequencing restrictions (on Tegra 2, cpu 0 must be the last to
  32. * power down), or due to HW bugs (on OMAP4460, a cpu powering up
  33. * will corrupt the gic state unless the other cpu runs a work
  34. * around). Each cpu has a power state that it can enter without
  35. * coordinating with the other cpu (usually Wait For Interrupt, or
  36. * WFI), and one or more "coupled" power states that affect blocks
  37. * shared between the cpus (L2 cache, interrupt controller, and
  38. * sometimes the whole SoC). Entering a coupled power state must
  39. * be tightly controlled on both cpus.
  40. *
  41. * This file implements a solution, where each cpu will wait in the
  42. * WFI state until all cpus are ready to enter a coupled state, at
  43. * which point the coupled state function will be called on all
  44. * cpus at approximately the same time.
  45. *
  46. * Once all cpus are ready to enter idle, they are woken by an smp
  47. * cross call. At this point, there is a chance that one of the
  48. * cpus will find work to do, and choose not to enter idle. A
  49. * final pass is needed to guarantee that all cpus will call the
  50. * power state enter function at the same time. During this pass,
  51. * each cpu will increment the ready counter, and continue once the
  52. * ready counter matches the number of online coupled cpus. If any
  53. * cpu exits idle, the other cpus will decrement their counter and
  54. * retry.
  55. *
  56. * requested_state stores the deepest coupled idle state each cpu
  57. * is ready for. It is assumed that the states are indexed from
  58. * shallowest (highest power, lowest exit latency) to deepest
  59. * (lowest power, highest exit latency). The requested_state
  60. * variable is not locked. It is only written from the cpu that
  61. * it stores (or by the on/offlining cpu if that cpu is offline),
  62. * and only read after all the cpus are ready for the coupled idle
  63. * state are are no longer updating it.
  64. *
  65. * Three atomic counters are used. alive_count tracks the number
  66. * of cpus in the coupled set that are currently or soon will be
  67. * online. waiting_count tracks the number of cpus that are in
  68. * the waiting loop, in the ready loop, or in the coupled idle state.
  69. * ready_count tracks the number of cpus that are in the ready loop
  70. * or in the coupled idle state.
  71. *
  72. * To use coupled cpuidle states, a cpuidle driver must:
  73. *
  74. * Set struct cpuidle_device.coupled_cpus to the mask of all
  75. * coupled cpus, usually the same as cpu_possible_mask if all cpus
  76. * are part of the same cluster. The coupled_cpus mask must be
  77. * set in the struct cpuidle_device for each cpu.
  78. *
  79. * Set struct cpuidle_device.safe_state to a state that is not a
  80. * coupled state. This is usually WFI.
  81. *
  82. * Set CPUIDLE_FLAG_COUPLED in struct cpuidle_state.flags for each
  83. * state that affects multiple cpus.
  84. *
  85. * Provide a struct cpuidle_state.enter function for each state
  86. * that affects multiple cpus. This function is guaranteed to be
  87. * called on all cpus at approximately the same time. The driver
  88. * should ensure that the cpus all abort together if any cpu tries
  89. * to abort once the function is called. The function should return
  90. * with interrupts still disabled.
  91. */
  92. /**
  93. * struct cpuidle_coupled - data for set of cpus that share a coupled idle state
  94. * @coupled_cpus: mask of cpus that are part of the coupled set
  95. * @requested_state: array of requested states for cpus in the coupled set
  96. * @ready_waiting_counts: combined count of cpus in ready or waiting loops
  97. * @online_count: count of cpus that are online
  98. * @refcnt: reference count of cpuidle devices that are using this struct
  99. * @prevent: flag to prevent coupled idle while a cpu is hotplugging
  100. */
  101. struct cpuidle_coupled {
  102. cpumask_t coupled_cpus;
  103. int requested_state[NR_CPUS];
  104. atomic_t ready_waiting_counts;
  105. atomic_t abort_barrier;
  106. int online_count;
  107. int refcnt;
  108. int prevent;
  109. };
  110. #define WAITING_BITS 16
  111. #define MAX_WAITING_CPUS (1 << WAITING_BITS)
  112. #define WAITING_MASK (MAX_WAITING_CPUS - 1)
  113. #define READY_MASK (~WAITING_MASK)
  114. #define CPUIDLE_COUPLED_NOT_IDLE (-1)
  115. static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
  116. /*
  117. * The cpuidle_coupled_poke_pending mask is used to avoid calling
  118. * __smp_call_function_single with the per cpu call_single_data struct already
  119. * in use. This prevents a deadlock where two cpus are waiting for each others
  120. * call_single_data struct to be available
  121. */
  122. static cpumask_t cpuidle_coupled_poke_pending;
  123. /*
  124. * The cpuidle_coupled_poked mask is used to ensure that each cpu has been poked
  125. * once to minimize entering the ready loop with a poke pending, which would
  126. * require aborting and retrying.
  127. */
  128. static cpumask_t cpuidle_coupled_poked;
  129. /**
  130. * cpuidle_coupled_parallel_barrier - synchronize all online coupled cpus
  131. * @dev: cpuidle_device of the calling cpu
  132. * @a: atomic variable to hold the barrier
  133. *
  134. * No caller to this function will return from this function until all online
  135. * cpus in the same coupled group have called this function. Once any caller
  136. * has returned from this function, the barrier is immediately available for
  137. * reuse.
  138. *
  139. * The atomic variable must be initialized to 0 before any cpu calls
  140. * this function, will be reset to 0 before any cpu returns from this function.
  141. *
  142. * Must only be called from within a coupled idle state handler
  143. * (state.enter when state.flags has CPUIDLE_FLAG_COUPLED set).
  144. *
  145. * Provides full smp barrier semantics before and after calling.
  146. */
  147. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  148. {
  149. int n = dev->coupled->online_count;
  150. smp_mb__before_atomic();
  151. atomic_inc(a);
  152. while (atomic_read(a) < n)
  153. cpu_relax();
  154. if (atomic_inc_return(a) == n * 2) {
  155. atomic_set(a, 0);
  156. return;
  157. }
  158. while (atomic_read(a) > n)
  159. cpu_relax();
  160. }
  161. /**
  162. * cpuidle_state_is_coupled - check if a state is part of a coupled set
  163. * @drv: struct cpuidle_driver for the platform
  164. * @state: index of the target state in drv->states
  165. *
  166. * Returns true if the target state is coupled with cpus besides this one
  167. */
  168. bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)
  169. {
  170. return drv->states[state].flags & CPUIDLE_FLAG_COUPLED;
  171. }
  172. /**
  173. * cpuidle_coupled_state_verify - check if the coupled states are correctly set.
  174. * @drv: struct cpuidle_driver for the platform
  175. *
  176. * Returns 0 for valid state values, a negative error code otherwise:
  177. * * -EINVAL if any coupled state(safe_state_index) is wrongly set.
  178. */
  179. int cpuidle_coupled_state_verify(struct cpuidle_driver *drv)
  180. {
  181. int i;
  182. for (i = drv->state_count - 1; i >= 0; i--) {
  183. if (cpuidle_state_is_coupled(drv, i) &&
  184. (drv->safe_state_index == i ||
  185. drv->safe_state_index < 0 ||
  186. drv->safe_state_index >= drv->state_count))
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * cpuidle_coupled_set_ready - mark a cpu as ready
  193. * @coupled: the struct coupled that contains the current cpu
  194. */
  195. static inline void cpuidle_coupled_set_ready(struct cpuidle_coupled *coupled)
  196. {
  197. atomic_add(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
  198. }
  199. /**
  200. * cpuidle_coupled_set_not_ready - mark a cpu as not ready
  201. * @coupled: the struct coupled that contains the current cpu
  202. *
  203. * Decrements the ready counter, unless the ready (and thus the waiting) counter
  204. * is equal to the number of online cpus. Prevents a race where one cpu
  205. * decrements the waiting counter and then re-increments it just before another
  206. * cpu has decremented its ready counter, leading to the ready counter going
  207. * down from the number of online cpus without going through the coupled idle
  208. * state.
  209. *
  210. * Returns 0 if the counter was decremented successfully, -EINVAL if the ready
  211. * counter was equal to the number of online cpus.
  212. */
  213. static
  214. inline int cpuidle_coupled_set_not_ready(struct cpuidle_coupled *coupled)
  215. {
  216. int all;
  217. int ret;
  218. all = coupled->online_count | (coupled->online_count << WAITING_BITS);
  219. ret = atomic_add_unless(&coupled->ready_waiting_counts,
  220. -MAX_WAITING_CPUS, all);
  221. return ret ? 0 : -EINVAL;
  222. }
  223. /**
  224. * cpuidle_coupled_no_cpus_ready - check if no cpus in a coupled set are ready
  225. * @coupled: the struct coupled that contains the current cpu
  226. *
  227. * Returns true if all of the cpus in a coupled set are out of the ready loop.
  228. */
  229. static inline int cpuidle_coupled_no_cpus_ready(struct cpuidle_coupled *coupled)
  230. {
  231. int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
  232. return r == 0;
  233. }
  234. /**
  235. * cpuidle_coupled_cpus_ready - check if all cpus in a coupled set are ready
  236. * @coupled: the struct coupled that contains the current cpu
  237. *
  238. * Returns true if all cpus coupled to this target state are in the ready loop
  239. */
  240. static inline bool cpuidle_coupled_cpus_ready(struct cpuidle_coupled *coupled)
  241. {
  242. int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
  243. return r == coupled->online_count;
  244. }
  245. /**
  246. * cpuidle_coupled_cpus_waiting - check if all cpus in a coupled set are waiting
  247. * @coupled: the struct coupled that contains the current cpu
  248. *
  249. * Returns true if all cpus coupled to this target state are in the wait loop
  250. */
  251. static inline bool cpuidle_coupled_cpus_waiting(struct cpuidle_coupled *coupled)
  252. {
  253. int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
  254. return w == coupled->online_count;
  255. }
  256. /**
  257. * cpuidle_coupled_no_cpus_waiting - check if no cpus in coupled set are waiting
  258. * @coupled: the struct coupled that contains the current cpu
  259. *
  260. * Returns true if all of the cpus in a coupled set are out of the waiting loop.
  261. */
  262. static inline int cpuidle_coupled_no_cpus_waiting(struct cpuidle_coupled *coupled)
  263. {
  264. int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
  265. return w == 0;
  266. }
  267. /**
  268. * cpuidle_coupled_get_state - determine the deepest idle state
  269. * @dev: struct cpuidle_device for this cpu
  270. * @coupled: the struct coupled that contains the current cpu
  271. *
  272. * Returns the deepest idle state that all coupled cpus can enter
  273. */
  274. static inline int cpuidle_coupled_get_state(struct cpuidle_device *dev,
  275. struct cpuidle_coupled *coupled)
  276. {
  277. int i;
  278. int state = INT_MAX;
  279. /*
  280. * Read barrier ensures that read of requested_state is ordered after
  281. * reads of ready_count. Matches the write barriers
  282. * cpuidle_set_state_waiting.
  283. */
  284. smp_rmb();
  285. for_each_cpu(i, &coupled->coupled_cpus)
  286. if (cpu_online(i) && coupled->requested_state[i] < state)
  287. state = coupled->requested_state[i];
  288. return state;
  289. }
  290. static void cpuidle_coupled_handle_poke(void *info)
  291. {
  292. int cpu = (unsigned long)info;
  293. cpumask_set_cpu(cpu, &cpuidle_coupled_poked);
  294. cpumask_clear_cpu(cpu, &cpuidle_coupled_poke_pending);
  295. }
  296. /**
  297. * cpuidle_coupled_poke - wake up a cpu that may be waiting
  298. * @cpu: target cpu
  299. *
  300. * Ensures that the target cpu exits it's waiting idle state (if it is in it)
  301. * and will see updates to waiting_count before it re-enters it's waiting idle
  302. * state.
  303. *
  304. * If cpuidle_coupled_poked_mask is already set for the target cpu, that cpu
  305. * either has or will soon have a pending IPI that will wake it out of idle,
  306. * or it is currently processing the IPI and is not in idle.
  307. */
  308. static void cpuidle_coupled_poke(int cpu)
  309. {
  310. struct call_single_data *csd = &per_cpu(cpuidle_coupled_poke_cb, cpu);
  311. if (!cpumask_test_and_set_cpu(cpu, &cpuidle_coupled_poke_pending))
  312. smp_call_function_single_async(cpu, csd);
  313. }
  314. /**
  315. * cpuidle_coupled_poke_others - wake up all other cpus that may be waiting
  316. * @dev: struct cpuidle_device for this cpu
  317. * @coupled: the struct coupled that contains the current cpu
  318. *
  319. * Calls cpuidle_coupled_poke on all other online cpus.
  320. */
  321. static void cpuidle_coupled_poke_others(int this_cpu,
  322. struct cpuidle_coupled *coupled)
  323. {
  324. int cpu;
  325. for_each_cpu(cpu, &coupled->coupled_cpus)
  326. if (cpu != this_cpu && cpu_online(cpu))
  327. cpuidle_coupled_poke(cpu);
  328. }
  329. /**
  330. * cpuidle_coupled_set_waiting - mark this cpu as in the wait loop
  331. * @dev: struct cpuidle_device for this cpu
  332. * @coupled: the struct coupled that contains the current cpu
  333. * @next_state: the index in drv->states of the requested state for this cpu
  334. *
  335. * Updates the requested idle state for the specified cpuidle device.
  336. * Returns the number of waiting cpus.
  337. */
  338. static int cpuidle_coupled_set_waiting(int cpu,
  339. struct cpuidle_coupled *coupled, int next_state)
  340. {
  341. coupled->requested_state[cpu] = next_state;
  342. /*
  343. * The atomic_inc_return provides a write barrier to order the write
  344. * to requested_state with the later write that increments ready_count.
  345. */
  346. return atomic_inc_return(&coupled->ready_waiting_counts) & WAITING_MASK;
  347. }
  348. /**
  349. * cpuidle_coupled_set_not_waiting - mark this cpu as leaving the wait loop
  350. * @dev: struct cpuidle_device for this cpu
  351. * @coupled: the struct coupled that contains the current cpu
  352. *
  353. * Removes the requested idle state for the specified cpuidle device.
  354. */
  355. static void cpuidle_coupled_set_not_waiting(int cpu,
  356. struct cpuidle_coupled *coupled)
  357. {
  358. /*
  359. * Decrementing waiting count can race with incrementing it in
  360. * cpuidle_coupled_set_waiting, but that's OK. Worst case, some
  361. * cpus will increment ready_count and then spin until they
  362. * notice that this cpu has cleared it's requested_state.
  363. */
  364. atomic_dec(&coupled->ready_waiting_counts);
  365. coupled->requested_state[cpu] = CPUIDLE_COUPLED_NOT_IDLE;
  366. }
  367. /**
  368. * cpuidle_coupled_set_done - mark this cpu as leaving the ready loop
  369. * @cpu: the current cpu
  370. * @coupled: the struct coupled that contains the current cpu
  371. *
  372. * Marks this cpu as no longer in the ready and waiting loops. Decrements
  373. * the waiting count first to prevent another cpu looping back in and seeing
  374. * this cpu as waiting just before it exits idle.
  375. */
  376. static void cpuidle_coupled_set_done(int cpu, struct cpuidle_coupled *coupled)
  377. {
  378. cpuidle_coupled_set_not_waiting(cpu, coupled);
  379. atomic_sub(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
  380. }
  381. /**
  382. * cpuidle_coupled_clear_pokes - spin until the poke interrupt is processed
  383. * @cpu - this cpu
  384. *
  385. * Turns on interrupts and spins until any outstanding poke interrupts have
  386. * been processed and the poke bit has been cleared.
  387. *
  388. * Other interrupts may also be processed while interrupts are enabled, so
  389. * need_resched() must be tested after this function returns to make sure
  390. * the interrupt didn't schedule work that should take the cpu out of idle.
  391. *
  392. * Returns 0 if no poke was pending, 1 if a poke was cleared.
  393. */
  394. static int cpuidle_coupled_clear_pokes(int cpu)
  395. {
  396. if (!cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending))
  397. return 0;
  398. local_irq_enable();
  399. while (cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending))
  400. cpu_relax();
  401. local_irq_disable();
  402. return 1;
  403. }
  404. static bool cpuidle_coupled_any_pokes_pending(struct cpuidle_coupled *coupled)
  405. {
  406. cpumask_t cpus;
  407. int ret;
  408. cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus);
  409. ret = cpumask_and(&cpus, &cpuidle_coupled_poke_pending, &cpus);
  410. return ret;
  411. }
  412. /**
  413. * cpuidle_enter_state_coupled - attempt to enter a state with coupled cpus
  414. * @dev: struct cpuidle_device for the current cpu
  415. * @drv: struct cpuidle_driver for the platform
  416. * @next_state: index of the requested state in drv->states
  417. *
  418. * Coordinate with coupled cpus to enter the target state. This is a two
  419. * stage process. In the first stage, the cpus are operating independently,
  420. * and may call into cpuidle_enter_state_coupled at completely different times.
  421. * To save as much power as possible, the first cpus to call this function will
  422. * go to an intermediate state (the cpuidle_device's safe state), and wait for
  423. * all the other cpus to call this function. Once all coupled cpus are idle,
  424. * the second stage will start. Each coupled cpu will spin until all cpus have
  425. * guaranteed that they will call the target_state.
  426. *
  427. * This function must be called with interrupts disabled. It may enable
  428. * interrupts while preparing for idle, and it will always return with
  429. * interrupts enabled.
  430. */
  431. int cpuidle_enter_state_coupled(struct cpuidle_device *dev,
  432. struct cpuidle_driver *drv, int next_state)
  433. {
  434. int entered_state = -1;
  435. struct cpuidle_coupled *coupled = dev->coupled;
  436. int w;
  437. if (!coupled)
  438. return -EINVAL;
  439. while (coupled->prevent) {
  440. cpuidle_coupled_clear_pokes(dev->cpu);
  441. if (need_resched()) {
  442. local_irq_enable();
  443. return entered_state;
  444. }
  445. entered_state = cpuidle_enter_state(dev, drv,
  446. drv->safe_state_index);
  447. local_irq_disable();
  448. }
  449. /* Read barrier ensures online_count is read after prevent is cleared */
  450. smp_rmb();
  451. reset:
  452. cpumask_clear_cpu(dev->cpu, &cpuidle_coupled_poked);
  453. w = cpuidle_coupled_set_waiting(dev->cpu, coupled, next_state);
  454. /*
  455. * If this is the last cpu to enter the waiting state, poke
  456. * all the other cpus out of their waiting state so they can
  457. * enter a deeper state. This can race with one of the cpus
  458. * exiting the waiting state due to an interrupt and
  459. * decrementing waiting_count, see comment below.
  460. */
  461. if (w == coupled->online_count) {
  462. cpumask_set_cpu(dev->cpu, &cpuidle_coupled_poked);
  463. cpuidle_coupled_poke_others(dev->cpu, coupled);
  464. }
  465. retry:
  466. /*
  467. * Wait for all coupled cpus to be idle, using the deepest state
  468. * allowed for a single cpu. If this was not the poking cpu, wait
  469. * for at least one poke before leaving to avoid a race where
  470. * two cpus could arrive at the waiting loop at the same time,
  471. * but the first of the two to arrive could skip the loop without
  472. * processing the pokes from the last to arrive.
  473. */
  474. while (!cpuidle_coupled_cpus_waiting(coupled) ||
  475. !cpumask_test_cpu(dev->cpu, &cpuidle_coupled_poked)) {
  476. if (cpuidle_coupled_clear_pokes(dev->cpu))
  477. continue;
  478. if (need_resched()) {
  479. cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
  480. goto out;
  481. }
  482. if (coupled->prevent) {
  483. cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
  484. goto out;
  485. }
  486. entered_state = cpuidle_enter_state(dev, drv,
  487. drv->safe_state_index);
  488. local_irq_disable();
  489. }
  490. cpuidle_coupled_clear_pokes(dev->cpu);
  491. if (need_resched()) {
  492. cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
  493. goto out;
  494. }
  495. /*
  496. * Make sure final poke status for this cpu is visible before setting
  497. * cpu as ready.
  498. */
  499. smp_wmb();
  500. /*
  501. * All coupled cpus are probably idle. There is a small chance that
  502. * one of the other cpus just became active. Increment the ready count,
  503. * and spin until all coupled cpus have incremented the counter. Once a
  504. * cpu has incremented the ready counter, it cannot abort idle and must
  505. * spin until either all cpus have incremented the ready counter, or
  506. * another cpu leaves idle and decrements the waiting counter.
  507. */
  508. cpuidle_coupled_set_ready(coupled);
  509. while (!cpuidle_coupled_cpus_ready(coupled)) {
  510. /* Check if any other cpus bailed out of idle. */
  511. if (!cpuidle_coupled_cpus_waiting(coupled))
  512. if (!cpuidle_coupled_set_not_ready(coupled))
  513. goto retry;
  514. cpu_relax();
  515. }
  516. /*
  517. * Make sure read of all cpus ready is done before reading pending pokes
  518. */
  519. smp_rmb();
  520. /*
  521. * There is a small chance that a cpu left and reentered idle after this
  522. * cpu saw that all cpus were waiting. The cpu that reentered idle will
  523. * have sent this cpu a poke, which will still be pending after the
  524. * ready loop. The pending interrupt may be lost by the interrupt
  525. * controller when entering the deep idle state. It's not possible to
  526. * clear a pending interrupt without turning interrupts on and handling
  527. * it, and it's too late to turn on interrupts here, so reset the
  528. * coupled idle state of all cpus and retry.
  529. */
  530. if (cpuidle_coupled_any_pokes_pending(coupled)) {
  531. cpuidle_coupled_set_done(dev->cpu, coupled);
  532. /* Wait for all cpus to see the pending pokes */
  533. cpuidle_coupled_parallel_barrier(dev, &coupled->abort_barrier);
  534. goto reset;
  535. }
  536. /* all cpus have acked the coupled state */
  537. next_state = cpuidle_coupled_get_state(dev, coupled);
  538. entered_state = cpuidle_enter_state(dev, drv, next_state);
  539. cpuidle_coupled_set_done(dev->cpu, coupled);
  540. out:
  541. /*
  542. * Normal cpuidle states are expected to return with irqs enabled.
  543. * That leads to an inefficiency where a cpu receiving an interrupt
  544. * that brings it out of idle will process that interrupt before
  545. * exiting the idle enter function and decrementing ready_count. All
  546. * other cpus will need to spin waiting for the cpu that is processing
  547. * the interrupt. If the driver returns with interrupts disabled,
  548. * all other cpus will loop back into the safe idle state instead of
  549. * spinning, saving power.
  550. *
  551. * Calling local_irq_enable here allows coupled states to return with
  552. * interrupts disabled, but won't cause problems for drivers that
  553. * exit with interrupts enabled.
  554. */
  555. local_irq_enable();
  556. /*
  557. * Wait until all coupled cpus have exited idle. There is no risk that
  558. * a cpu exits and re-enters the ready state because this cpu has
  559. * already decremented its waiting_count.
  560. */
  561. while (!cpuidle_coupled_no_cpus_ready(coupled))
  562. cpu_relax();
  563. return entered_state;
  564. }
  565. static void cpuidle_coupled_update_online_cpus(struct cpuidle_coupled *coupled)
  566. {
  567. cpumask_t cpus;
  568. cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus);
  569. coupled->online_count = cpumask_weight(&cpus);
  570. }
  571. /**
  572. * cpuidle_coupled_register_device - register a coupled cpuidle device
  573. * @dev: struct cpuidle_device for the current cpu
  574. *
  575. * Called from cpuidle_register_device to handle coupled idle init. Finds the
  576. * cpuidle_coupled struct for this set of coupled cpus, or creates one if none
  577. * exists yet.
  578. */
  579. int cpuidle_coupled_register_device(struct cpuidle_device *dev)
  580. {
  581. int cpu;
  582. struct cpuidle_device *other_dev;
  583. struct call_single_data *csd;
  584. struct cpuidle_coupled *coupled;
  585. if (cpumask_empty(&dev->coupled_cpus))
  586. return 0;
  587. for_each_cpu(cpu, &dev->coupled_cpus) {
  588. other_dev = per_cpu(cpuidle_devices, cpu);
  589. if (other_dev && other_dev->coupled) {
  590. coupled = other_dev->coupled;
  591. goto have_coupled;
  592. }
  593. }
  594. /* No existing coupled info found, create a new one */
  595. coupled = kzalloc(sizeof(struct cpuidle_coupled), GFP_KERNEL);
  596. if (!coupled)
  597. return -ENOMEM;
  598. coupled->coupled_cpus = dev->coupled_cpus;
  599. have_coupled:
  600. dev->coupled = coupled;
  601. if (WARN_ON(!cpumask_equal(&dev->coupled_cpus, &coupled->coupled_cpus)))
  602. coupled->prevent++;
  603. cpuidle_coupled_update_online_cpus(coupled);
  604. coupled->refcnt++;
  605. csd = &per_cpu(cpuidle_coupled_poke_cb, dev->cpu);
  606. csd->func = cpuidle_coupled_handle_poke;
  607. csd->info = (void *)(unsigned long)dev->cpu;
  608. return 0;
  609. }
  610. /**
  611. * cpuidle_coupled_unregister_device - unregister a coupled cpuidle device
  612. * @dev: struct cpuidle_device for the current cpu
  613. *
  614. * Called from cpuidle_unregister_device to tear down coupled idle. Removes the
  615. * cpu from the coupled idle set, and frees the cpuidle_coupled_info struct if
  616. * this was the last cpu in the set.
  617. */
  618. void cpuidle_coupled_unregister_device(struct cpuidle_device *dev)
  619. {
  620. struct cpuidle_coupled *coupled = dev->coupled;
  621. if (cpumask_empty(&dev->coupled_cpus))
  622. return;
  623. if (--coupled->refcnt)
  624. kfree(coupled);
  625. dev->coupled = NULL;
  626. }
  627. /**
  628. * cpuidle_coupled_prevent_idle - prevent cpus from entering a coupled state
  629. * @coupled: the struct coupled that contains the cpu that is changing state
  630. *
  631. * Disables coupled cpuidle on a coupled set of cpus. Used to ensure that
  632. * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
  633. */
  634. static void cpuidle_coupled_prevent_idle(struct cpuidle_coupled *coupled)
  635. {
  636. int cpu = get_cpu();
  637. /* Force all cpus out of the waiting loop. */
  638. coupled->prevent++;
  639. cpuidle_coupled_poke_others(cpu, coupled);
  640. put_cpu();
  641. while (!cpuidle_coupled_no_cpus_waiting(coupled))
  642. cpu_relax();
  643. }
  644. /**
  645. * cpuidle_coupled_allow_idle - allows cpus to enter a coupled state
  646. * @coupled: the struct coupled that contains the cpu that is changing state
  647. *
  648. * Enables coupled cpuidle on a coupled set of cpus. Used to ensure that
  649. * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
  650. */
  651. static void cpuidle_coupled_allow_idle(struct cpuidle_coupled *coupled)
  652. {
  653. int cpu = get_cpu();
  654. /*
  655. * Write barrier ensures readers see the new online_count when they
  656. * see prevent == 0.
  657. */
  658. smp_wmb();
  659. coupled->prevent--;
  660. /* Force cpus out of the prevent loop. */
  661. cpuidle_coupled_poke_others(cpu, coupled);
  662. put_cpu();
  663. }
  664. static int coupled_cpu_online(unsigned int cpu)
  665. {
  666. struct cpuidle_device *dev;
  667. mutex_lock(&cpuidle_lock);
  668. dev = per_cpu(cpuidle_devices, cpu);
  669. if (dev && dev->coupled) {
  670. cpuidle_coupled_update_online_cpus(dev->coupled);
  671. cpuidle_coupled_allow_idle(dev->coupled);
  672. }
  673. mutex_unlock(&cpuidle_lock);
  674. return 0;
  675. }
  676. static int coupled_cpu_up_prepare(unsigned int cpu)
  677. {
  678. struct cpuidle_device *dev;
  679. mutex_lock(&cpuidle_lock);
  680. dev = per_cpu(cpuidle_devices, cpu);
  681. if (dev && dev->coupled)
  682. cpuidle_coupled_prevent_idle(dev->coupled);
  683. mutex_unlock(&cpuidle_lock);
  684. return 0;
  685. }
  686. static int __init cpuidle_coupled_init(void)
  687. {
  688. int ret;
  689. ret = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE,
  690. "cpuidle/coupled:prepare",
  691. coupled_cpu_up_prepare,
  692. coupled_cpu_online);
  693. if (ret)
  694. return ret;
  695. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  696. "cpuidle/coupled:online",
  697. coupled_cpu_online,
  698. coupled_cpu_up_prepare);
  699. if (ret < 0)
  700. cpuhp_remove_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE);
  701. return ret;
  702. }
  703. core_initcall(cpuidle_coupled_init);