![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 /* 0003 * coupled.c - helper functions to enter the same idle state on multiple cpus 0004 * 0005 * Copyright (c) 2011 Google, Inc. 0006 * 0007 * Author: Colin Cross <ccross@android.com> 0008 */ 0009 0010 #include <linux/kernel.h> 0011 #include <linux/cpu.h> 0012 #include <linux/cpuidle.h> 0013 #include <linux/mutex.h> 0014 #include <linux/sched.h> 0015 #include <linux/slab.h> 0016 #include <linux/spinlock.h> 0017 0018 #include "cpuidle.h" 0019 0020 /** 0021 * DOC: Coupled cpuidle states 0022 * 0023 * On some ARM SMP SoCs (OMAP4460, Tegra 2, and probably more), the 0024 * cpus cannot be independently powered down, either due to 0025 * sequencing restrictions (on Tegra 2, cpu 0 must be the last to 0026 * power down), or due to HW bugs (on OMAP4460, a cpu powering up 0027 * will corrupt the gic state unless the other cpu runs a work 0028 * around). Each cpu has a power state that it can enter without 0029 * coordinating with the other cpu (usually Wait For Interrupt, or 0030 * WFI), and one or more "coupled" power states that affect blocks 0031 * shared between the cpus (L2 cache, interrupt controller, and 0032 * sometimes the whole SoC). Entering a coupled power state must 0033 * be tightly controlled on both cpus. 0034 * 0035 * This file implements a solution, where each cpu will wait in the 0036 * WFI state until all cpus are ready to enter a coupled state, at 0037 * which point the coupled state function will be called on all 0038 * cpus at approximately the same time. 0039 * 0040 * Once all cpus are ready to enter idle, they are woken by an smp 0041 * cross call. At this point, there is a chance that one of the 0042 * cpus will find work to do, and choose not to enter idle. A 0043 * final pass is needed to guarantee that all cpus will call the 0044 * power state enter function at the same time. During this pass, 0045 * each cpu will increment the ready counter, and continue once the 0046 * ready counter matches the number of online coupled cpus. If any 0047 * cpu exits idle, the other cpus will decrement their counter and 0048 * retry. 0049 * 0050 * requested_state stores the deepest coupled idle state each cpu 0051 * is ready for. It is assumed that the states are indexed from 0052 * shallowest (highest power, lowest exit latency) to deepest 0053 * (lowest power, highest exit latency). The requested_state 0054 * variable is not locked. It is only written from the cpu that 0055 * it stores (or by the on/offlining cpu if that cpu is offline), 0056 * and only read after all the cpus are ready for the coupled idle 0057 * state are are no longer updating it. 0058 * 0059 * Three atomic counters are used. alive_count tracks the number 0060 * of cpus in the coupled set that are currently or soon will be 0061 * online. waiting_count tracks the number of cpus that are in 0062 * the waiting loop, in the ready loop, or in the coupled idle state. 0063 * ready_count tracks the number of cpus that are in the ready loop 0064 * or in the coupled idle state. 0065 * 0066 * To use coupled cpuidle states, a cpuidle driver must: 0067 * 0068 * Set struct cpuidle_device.coupled_cpus to the mask of all 0069 * coupled cpus, usually the same as cpu_possible_mask if all cpus 0070 * are part of the same cluster. The coupled_cpus mask must be 0071 * set in the struct cpuidle_device for each cpu. 0072 * 0073 * Set struct cpuidle_device.safe_state to a state that is not a 0074 * coupled state. This is usually WFI. 0075 * 0076 * Set CPUIDLE_FLAG_COUPLED in struct cpuidle_state.flags for each 0077 * state that affects multiple cpus. 0078 * 0079 * Provide a struct cpuidle_state.enter function for each state 0080 * that affects multiple cpus. This function is guaranteed to be 0081 * called on all cpus at approximately the same time. The driver 0082 * should ensure that the cpus all abort together if any cpu tries 0083 * to abort once the function is called. The function should return 0084 * with interrupts still disabled. 0085 */ 0086 0087 /** 0088 * struct cpuidle_coupled - data for set of cpus that share a coupled idle state 0089 * @coupled_cpus: mask of cpus that are part of the coupled set 0090 * @requested_state: array of requested states for cpus in the coupled set 0091 * @ready_waiting_counts: combined count of cpus in ready or waiting loops 0092 * @abort_barrier: synchronisation point for abort cases 0093 * @online_count: count of cpus that are online 0094 * @refcnt: reference count of cpuidle devices that are using this struct 0095 * @prevent: flag to prevent coupled idle while a cpu is hotplugging 0096 */ 0097 struct cpuidle_coupled { 0098 cpumask_t coupled_cpus; 0099 int requested_state[NR_CPUS]; 0100 atomic_t ready_waiting_counts; 0101 atomic_t abort_barrier; 0102 int online_count; 0103 int refcnt; 0104 int prevent; 0105 }; 0106 0107 #define WAITING_BITS 16 0108 #define MAX_WAITING_CPUS (1 << WAITING_BITS) 0109 #define WAITING_MASK (MAX_WAITING_CPUS - 1) 0110 #define READY_MASK (~WAITING_MASK) 0111 0112 #define CPUIDLE_COUPLED_NOT_IDLE (-1) 0113 0114 static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb); 0115 0116 /* 0117 * The cpuidle_coupled_poke_pending mask is used to avoid calling 0118 * __smp_call_function_single with the per cpu call_single_data_t struct already 0119 * in use. This prevents a deadlock where two cpus are waiting for each others 0120 * call_single_data_t struct to be available 0121 */ 0122 static cpumask_t cpuidle_coupled_poke_pending; 0123 0124 /* 0125 * The cpuidle_coupled_poked mask is used to ensure that each cpu has been poked 0126 * once to minimize entering the ready loop with a poke pending, which would 0127 * require aborting and retrying. 0128 */ 0129 static cpumask_t cpuidle_coupled_poked; 0130 0131 /** 0132 * cpuidle_coupled_parallel_barrier - synchronize all online coupled cpus 0133 * @dev: cpuidle_device of the calling cpu 0134 * @a: atomic variable to hold the barrier 0135 * 0136 * No caller to this function will return from this function until all online 0137 * cpus in the same coupled group have called this function. Once any caller 0138 * has returned from this function, the barrier is immediately available for 0139 * reuse. 0140 * 0141 * The atomic variable must be initialized to 0 before any cpu calls 0142 * this function, will be reset to 0 before any cpu returns from this function. 0143 * 0144 * Must only be called from within a coupled idle state handler 0145 * (state.enter when state.flags has CPUIDLE_FLAG_COUPLED set). 0146 * 0147 * Provides full smp barrier semantics before and after calling. 0148 */ 0149 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a) 0150 { 0151 int n = dev->coupled->online_count; 0152 0153 smp_mb__before_atomic(); 0154 atomic_inc(a); 0155 0156 while (atomic_read(a) < n) 0157 cpu_relax(); 0158 0159 if (atomic_inc_return(a) == n * 2) { 0160 atomic_set(a, 0); 0161 return; 0162 } 0163 0164 while (atomic_read(a) > n) 0165 cpu_relax(); 0166 } 0167 0168 /** 0169 * cpuidle_state_is_coupled - check if a state is part of a coupled set 0170 * @drv: struct cpuidle_driver for the platform 0171 * @state: index of the target state in drv->states 0172 * 0173 * Returns true if the target state is coupled with cpus besides this one 0174 */ 0175 bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) 0176 { 0177 return drv->states[state].flags & CPUIDLE_FLAG_COUPLED; 0178 } 0179 0180 /** 0181 * cpuidle_coupled_state_verify - check if the coupled states are correctly set. 0182 * @drv: struct cpuidle_driver for the platform 0183 * 0184 * Returns 0 for valid state values, a negative error code otherwise: 0185 * * -EINVAL if any coupled state(safe_state_index) is wrongly set. 0186 */ 0187 int cpuidle_coupled_state_verify(struct cpuidle_driver *drv) 0188 { 0189 int i; 0190 0191 for (i = drv->state_count - 1; i >= 0; i--) { 0192 if (cpuidle_state_is_coupled(drv, i) && 0193 (drv->safe_state_index == i || 0194 drv->safe_state_index < 0 || 0195 drv->safe_state_index >= drv->state_count)) 0196 return -EINVAL; 0197 } 0198 0199 return 0; 0200 } 0201 0202 /** 0203 * cpuidle_coupled_set_ready - mark a cpu as ready 0204 * @coupled: the struct coupled that contains the current cpu 0205 */ 0206 static inline void cpuidle_coupled_set_ready(struct cpuidle_coupled *coupled) 0207 { 0208 atomic_add(MAX_WAITING_CPUS, &coupled->ready_waiting_counts); 0209 } 0210 0211 /** 0212 * cpuidle_coupled_set_not_ready - mark a cpu as not ready 0213 * @coupled: the struct coupled that contains the current cpu 0214 * 0215 * Decrements the ready counter, unless the ready (and thus the waiting) counter 0216 * is equal to the number of online cpus. Prevents a race where one cpu 0217 * decrements the waiting counter and then re-increments it just before another 0218 * cpu has decremented its ready counter, leading to the ready counter going 0219 * down from the number of online cpus without going through the coupled idle 0220 * state. 0221 * 0222 * Returns 0 if the counter was decremented successfully, -EINVAL if the ready 0223 * counter was equal to the number of online cpus. 0224 */ 0225 static 0226 inline int cpuidle_coupled_set_not_ready(struct cpuidle_coupled *coupled) 0227 { 0228 int all; 0229 int ret; 0230 0231 all = coupled->online_count | (coupled->online_count << WAITING_BITS); 0232 ret = atomic_add_unless(&coupled->ready_waiting_counts, 0233 -MAX_WAITING_CPUS, all); 0234 0235 return ret ? 0 : -EINVAL; 0236 } 0237 0238 /** 0239 * cpuidle_coupled_no_cpus_ready - check if no cpus in a coupled set are ready 0240 * @coupled: the struct coupled that contains the current cpu 0241 * 0242 * Returns true if all of the cpus in a coupled set are out of the ready loop. 0243 */ 0244 static inline int cpuidle_coupled_no_cpus_ready(struct cpuidle_coupled *coupled) 0245 { 0246 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS; 0247 return r == 0; 0248 } 0249 0250 /** 0251 * cpuidle_coupled_cpus_ready - check if all cpus in a coupled set are ready 0252 * @coupled: the struct coupled that contains the current cpu 0253 * 0254 * Returns true if all cpus coupled to this target state are in the ready loop 0255 */ 0256 static inline bool cpuidle_coupled_cpus_ready(struct cpuidle_coupled *coupled) 0257 { 0258 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS; 0259 return r == coupled->online_count; 0260 } 0261 0262 /** 0263 * cpuidle_coupled_cpus_waiting - check if all cpus in a coupled set are waiting 0264 * @coupled: the struct coupled that contains the current cpu 0265 * 0266 * Returns true if all cpus coupled to this target state are in the wait loop 0267 */ 0268 static inline bool cpuidle_coupled_cpus_waiting(struct cpuidle_coupled *coupled) 0269 { 0270 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK; 0271 return w == coupled->online_count; 0272 } 0273 0274 /** 0275 * cpuidle_coupled_no_cpus_waiting - check if no cpus in coupled set are waiting 0276 * @coupled: the struct coupled that contains the current cpu 0277 * 0278 * Returns true if all of the cpus in a coupled set are out of the waiting loop. 0279 */ 0280 static inline int cpuidle_coupled_no_cpus_waiting(struct cpuidle_coupled *coupled) 0281 { 0282 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK; 0283 return w == 0; 0284 } 0285 0286 /** 0287 * cpuidle_coupled_get_state - determine the deepest idle state 0288 * @dev: struct cpuidle_device for this cpu 0289 * @coupled: the struct coupled that contains the current cpu 0290 * 0291 * Returns the deepest idle state that all coupled cpus can enter 0292 */ 0293 static inline int cpuidle_coupled_get_state(struct cpuidle_device *dev, 0294 struct cpuidle_coupled *coupled) 0295 { 0296 int i; 0297 int state = INT_MAX; 0298 0299 /* 0300 * Read barrier ensures that read of requested_state is ordered after 0301 * reads of ready_count. Matches the write barriers 0302 * cpuidle_set_state_waiting. 0303 */ 0304 smp_rmb(); 0305 0306 for_each_cpu(i, &coupled->coupled_cpus) 0307 if (cpu_online(i) && coupled->requested_state[i] < state) 0308 state = coupled->requested_state[i]; 0309 0310 return state; 0311 } 0312 0313 static void cpuidle_coupled_handle_poke(void *info) 0314 { 0315 int cpu = (unsigned long)info; 0316 cpumask_set_cpu(cpu, &cpuidle_coupled_poked); 0317 cpumask_clear_cpu(cpu, &cpuidle_coupled_poke_pending); 0318 } 0319 0320 /** 0321 * cpuidle_coupled_poke - wake up a cpu that may be waiting 0322 * @cpu: target cpu 0323 * 0324 * Ensures that the target cpu exits it's waiting idle state (if it is in it) 0325 * and will see updates to waiting_count before it re-enters it's waiting idle 0326 * state. 0327 * 0328 * If cpuidle_coupled_poked_mask is already set for the target cpu, that cpu 0329 * either has or will soon have a pending IPI that will wake it out of idle, 0330 * or it is currently processing the IPI and is not in idle. 0331 */ 0332 static void cpuidle_coupled_poke(int cpu) 0333 { 0334 call_single_data_t *csd = &per_cpu(cpuidle_coupled_poke_cb, cpu); 0335 0336 if (!cpumask_test_and_set_cpu(cpu, &cpuidle_coupled_poke_pending)) 0337 smp_call_function_single_async(cpu, csd); 0338 } 0339 0340 /** 0341 * cpuidle_coupled_poke_others - wake up all other cpus that may be waiting 0342 * @this_cpu: target cpu 0343 * @coupled: the struct coupled that contains the current cpu 0344 * 0345 * Calls cpuidle_coupled_poke on all other online cpus. 0346 */ 0347 static void cpuidle_coupled_poke_others(int this_cpu, 0348 struct cpuidle_coupled *coupled) 0349 { 0350 int cpu; 0351 0352 for_each_cpu(cpu, &coupled->coupled_cpus) 0353 if (cpu != this_cpu && cpu_online(cpu)) 0354 cpuidle_coupled_poke(cpu); 0355 } 0356 0357 /** 0358 * cpuidle_coupled_set_waiting - mark this cpu as in the wait loop 0359 * @cpu: target cpu 0360 * @coupled: the struct coupled that contains the current cpu 0361 * @next_state: the index in drv->states of the requested state for this cpu 0362 * 0363 * Updates the requested idle state for the specified cpuidle device. 0364 * Returns the number of waiting cpus. 0365 */ 0366 static int cpuidle_coupled_set_waiting(int cpu, 0367 struct cpuidle_coupled *coupled, int next_state) 0368 { 0369 coupled->requested_state[cpu] = next_state; 0370 0371 /* 0372 * The atomic_inc_return provides a write barrier to order the write 0373 * to requested_state with the later write that increments ready_count. 0374 */ 0375 return atomic_inc_return(&coupled->ready_waiting_counts) & WAITING_MASK; 0376 } 0377 0378 /** 0379 * cpuidle_coupled_set_not_waiting - mark this cpu as leaving the wait loop 0380 * @cpu: target cpu 0381 * @coupled: the struct coupled that contains the current cpu 0382 * 0383 * Removes the requested idle state for the specified cpuidle device. 0384 */ 0385 static void cpuidle_coupled_set_not_waiting(int cpu, 0386 struct cpuidle_coupled *coupled) 0387 { 0388 /* 0389 * Decrementing waiting count can race with incrementing it in 0390 * cpuidle_coupled_set_waiting, but that's OK. Worst case, some 0391 * cpus will increment ready_count and then spin until they 0392 * notice that this cpu has cleared it's requested_state. 0393 */ 0394 atomic_dec(&coupled->ready_waiting_counts); 0395 0396 coupled->requested_state[cpu] = CPUIDLE_COUPLED_NOT_IDLE; 0397 } 0398 0399 /** 0400 * cpuidle_coupled_set_done - mark this cpu as leaving the ready loop 0401 * @cpu: the current cpu 0402 * @coupled: the struct coupled that contains the current cpu 0403 * 0404 * Marks this cpu as no longer in the ready and waiting loops. Decrements 0405 * the waiting count first to prevent another cpu looping back in and seeing 0406 * this cpu as waiting just before it exits idle. 0407 */ 0408 static void cpuidle_coupled_set_done(int cpu, struct cpuidle_coupled *coupled) 0409 { 0410 cpuidle_coupled_set_not_waiting(cpu, coupled); 0411 atomic_sub(MAX_WAITING_CPUS, &coupled->ready_waiting_counts); 0412 } 0413 0414 /** 0415 * cpuidle_coupled_clear_pokes - spin until the poke interrupt is processed 0416 * @cpu: this cpu 0417 * 0418 * Turns on interrupts and spins until any outstanding poke interrupts have 0419 * been processed and the poke bit has been cleared. 0420 * 0421 * Other interrupts may also be processed while interrupts are enabled, so 0422 * need_resched() must be tested after this function returns to make sure 0423 * the interrupt didn't schedule work that should take the cpu out of idle. 0424 * 0425 * Returns 0 if no poke was pending, 1 if a poke was cleared. 0426 */ 0427 static int cpuidle_coupled_clear_pokes(int cpu) 0428 { 0429 if (!cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending)) 0430 return 0; 0431 0432 local_irq_enable(); 0433 while (cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending)) 0434 cpu_relax(); 0435 local_irq_disable(); 0436 0437 return 1; 0438 } 0439 0440 static bool cpuidle_coupled_any_pokes_pending(struct cpuidle_coupled *coupled) 0441 { 0442 cpumask_t cpus; 0443 int ret; 0444 0445 cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus); 0446 ret = cpumask_and(&cpus, &cpuidle_coupled_poke_pending, &cpus); 0447 0448 return ret; 0449 } 0450 0451 /** 0452 * cpuidle_enter_state_coupled - attempt to enter a state with coupled cpus 0453 * @dev: struct cpuidle_device for the current cpu 0454 * @drv: struct cpuidle_driver for the platform 0455 * @next_state: index of the requested state in drv->states 0456 * 0457 * Coordinate with coupled cpus to enter the target state. This is a two 0458 * stage process. In the first stage, the cpus are operating independently, 0459 * and may call into cpuidle_enter_state_coupled at completely different times. 0460 * To save as much power as possible, the first cpus to call this function will 0461 * go to an intermediate state (the cpuidle_device's safe state), and wait for 0462 * all the other cpus to call this function. Once all coupled cpus are idle, 0463 * the second stage will start. Each coupled cpu will spin until all cpus have 0464 * guaranteed that they will call the target_state. 0465 * 0466 * This function must be called with interrupts disabled. It may enable 0467 * interrupts while preparing for idle, and it will always return with 0468 * interrupts enabled. 0469 */ 0470 int cpuidle_enter_state_coupled(struct cpuidle_device *dev, 0471 struct cpuidle_driver *drv, int next_state) 0472 { 0473 int entered_state = -1; 0474 struct cpuidle_coupled *coupled = dev->coupled; 0475 int w; 0476 0477 if (!coupled) 0478 return -EINVAL; 0479 0480 while (coupled->prevent) { 0481 cpuidle_coupled_clear_pokes(dev->cpu); 0482 if (need_resched()) { 0483 local_irq_enable(); 0484 return entered_state; 0485 } 0486 entered_state = cpuidle_enter_state(dev, drv, 0487 drv->safe_state_index); 0488 local_irq_disable(); 0489 } 0490 0491 /* Read barrier ensures online_count is read after prevent is cleared */ 0492 smp_rmb(); 0493 0494 reset: 0495 cpumask_clear_cpu(dev->cpu, &cpuidle_coupled_poked); 0496 0497 w = cpuidle_coupled_set_waiting(dev->cpu, coupled, next_state); 0498 /* 0499 * If this is the last cpu to enter the waiting state, poke 0500 * all the other cpus out of their waiting state so they can 0501 * enter a deeper state. This can race with one of the cpus 0502 * exiting the waiting state due to an interrupt and 0503 * decrementing waiting_count, see comment below. 0504 */ 0505 if (w == coupled->online_count) { 0506 cpumask_set_cpu(dev->cpu, &cpuidle_coupled_poked); 0507 cpuidle_coupled_poke_others(dev->cpu, coupled); 0508 } 0509 0510 retry: 0511 /* 0512 * Wait for all coupled cpus to be idle, using the deepest state 0513 * allowed for a single cpu. If this was not the poking cpu, wait 0514 * for at least one poke before leaving to avoid a race where 0515 * two cpus could arrive at the waiting loop at the same time, 0516 * but the first of the two to arrive could skip the loop without 0517 * processing the pokes from the last to arrive. 0518 */ 0519 while (!cpuidle_coupled_cpus_waiting(coupled) || 0520 !cpumask_test_cpu(dev->cpu, &cpuidle_coupled_poked)) { 0521 if (cpuidle_coupled_clear_pokes(dev->cpu)) 0522 continue; 0523 0524 if (need_resched()) { 0525 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); 0526 goto out; 0527 } 0528 0529 if (coupled->prevent) { 0530 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); 0531 goto out; 0532 } 0533 0534 entered_state = cpuidle_enter_state(dev, drv, 0535 drv->safe_state_index); 0536 local_irq_disable(); 0537 } 0538 0539 cpuidle_coupled_clear_pokes(dev->cpu); 0540 if (need_resched()) { 0541 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); 0542 goto out; 0543 } 0544 0545 /* 0546 * Make sure final poke status for this cpu is visible before setting 0547 * cpu as ready. 0548 */ 0549 smp_wmb(); 0550 0551 /* 0552 * All coupled cpus are probably idle. There is a small chance that 0553 * one of the other cpus just became active. Increment the ready count, 0554 * and spin until all coupled cpus have incremented the counter. Once a 0555 * cpu has incremented the ready counter, it cannot abort idle and must 0556 * spin until either all cpus have incremented the ready counter, or 0557 * another cpu leaves idle and decrements the waiting counter. 0558 */ 0559 0560 cpuidle_coupled_set_ready(coupled); 0561 while (!cpuidle_coupled_cpus_ready(coupled)) { 0562 /* Check if any other cpus bailed out of idle. */ 0563 if (!cpuidle_coupled_cpus_waiting(coupled)) 0564 if (!cpuidle_coupled_set_not_ready(coupled)) 0565 goto retry; 0566 0567 cpu_relax(); 0568 } 0569 0570 /* 0571 * Make sure read of all cpus ready is done before reading pending pokes 0572 */ 0573 smp_rmb(); 0574 0575 /* 0576 * There is a small chance that a cpu left and reentered idle after this 0577 * cpu saw that all cpus were waiting. The cpu that reentered idle will 0578 * have sent this cpu a poke, which will still be pending after the 0579 * ready loop. The pending interrupt may be lost by the interrupt 0580 * controller when entering the deep idle state. It's not possible to 0581 * clear a pending interrupt without turning interrupts on and handling 0582 * it, and it's too late to turn on interrupts here, so reset the 0583 * coupled idle state of all cpus and retry. 0584 */ 0585 if (cpuidle_coupled_any_pokes_pending(coupled)) { 0586 cpuidle_coupled_set_done(dev->cpu, coupled); 0587 /* Wait for all cpus to see the pending pokes */ 0588 cpuidle_coupled_parallel_barrier(dev, &coupled->abort_barrier); 0589 goto reset; 0590 } 0591 0592 /* all cpus have acked the coupled state */ 0593 next_state = cpuidle_coupled_get_state(dev, coupled); 0594 0595 entered_state = cpuidle_enter_state(dev, drv, next_state); 0596 0597 cpuidle_coupled_set_done(dev->cpu, coupled); 0598 0599 out: 0600 /* 0601 * Normal cpuidle states are expected to return with irqs enabled. 0602 * That leads to an inefficiency where a cpu receiving an interrupt 0603 * that brings it out of idle will process that interrupt before 0604 * exiting the idle enter function and decrementing ready_count. All 0605 * other cpus will need to spin waiting for the cpu that is processing 0606 * the interrupt. If the driver returns with interrupts disabled, 0607 * all other cpus will loop back into the safe idle state instead of 0608 * spinning, saving power. 0609 * 0610 * Calling local_irq_enable here allows coupled states to return with 0611 * interrupts disabled, but won't cause problems for drivers that 0612 * exit with interrupts enabled. 0613 */ 0614 local_irq_enable(); 0615 0616 /* 0617 * Wait until all coupled cpus have exited idle. There is no risk that 0618 * a cpu exits and re-enters the ready state because this cpu has 0619 * already decremented its waiting_count. 0620 */ 0621 while (!cpuidle_coupled_no_cpus_ready(coupled)) 0622 cpu_relax(); 0623 0624 return entered_state; 0625 } 0626 0627 static void cpuidle_coupled_update_online_cpus(struct cpuidle_coupled *coupled) 0628 { 0629 cpumask_t cpus; 0630 cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus); 0631 coupled->online_count = cpumask_weight(&cpus); 0632 } 0633 0634 /** 0635 * cpuidle_coupled_register_device - register a coupled cpuidle device 0636 * @dev: struct cpuidle_device for the current cpu 0637 * 0638 * Called from cpuidle_register_device to handle coupled idle init. Finds the 0639 * cpuidle_coupled struct for this set of coupled cpus, or creates one if none 0640 * exists yet. 0641 */ 0642 int cpuidle_coupled_register_device(struct cpuidle_device *dev) 0643 { 0644 int cpu; 0645 struct cpuidle_device *other_dev; 0646 call_single_data_t *csd; 0647 struct cpuidle_coupled *coupled; 0648 0649 if (cpumask_empty(&dev->coupled_cpus)) 0650 return 0; 0651 0652 for_each_cpu(cpu, &dev->coupled_cpus) { 0653 other_dev = per_cpu(cpuidle_devices, cpu); 0654 if (other_dev && other_dev->coupled) { 0655 coupled = other_dev->coupled; 0656 goto have_coupled; 0657 } 0658 } 0659 0660 /* No existing coupled info found, create a new one */ 0661 coupled = kzalloc(sizeof(struct cpuidle_coupled), GFP_KERNEL); 0662 if (!coupled) 0663 return -ENOMEM; 0664 0665 coupled->coupled_cpus = dev->coupled_cpus; 0666 0667 have_coupled: 0668 dev->coupled = coupled; 0669 if (WARN_ON(!cpumask_equal(&dev->coupled_cpus, &coupled->coupled_cpus))) 0670 coupled->prevent++; 0671 0672 cpuidle_coupled_update_online_cpus(coupled); 0673 0674 coupled->refcnt++; 0675 0676 csd = &per_cpu(cpuidle_coupled_poke_cb, dev->cpu); 0677 INIT_CSD(csd, cpuidle_coupled_handle_poke, (void *)(unsigned long)dev->cpu); 0678 0679 return 0; 0680 } 0681 0682 /** 0683 * cpuidle_coupled_unregister_device - unregister a coupled cpuidle device 0684 * @dev: struct cpuidle_device for the current cpu 0685 * 0686 * Called from cpuidle_unregister_device to tear down coupled idle. Removes the 0687 * cpu from the coupled idle set, and frees the cpuidle_coupled_info struct if 0688 * this was the last cpu in the set. 0689 */ 0690 void cpuidle_coupled_unregister_device(struct cpuidle_device *dev) 0691 { 0692 struct cpuidle_coupled *coupled = dev->coupled; 0693 0694 if (cpumask_empty(&dev->coupled_cpus)) 0695 return; 0696 0697 if (--coupled->refcnt) 0698 kfree(coupled); 0699 dev->coupled = NULL; 0700 } 0701 0702 /** 0703 * cpuidle_coupled_prevent_idle - prevent cpus from entering a coupled state 0704 * @coupled: the struct coupled that contains the cpu that is changing state 0705 * 0706 * Disables coupled cpuidle on a coupled set of cpus. Used to ensure that 0707 * cpu_online_mask doesn't change while cpus are coordinating coupled idle. 0708 */ 0709 static void cpuidle_coupled_prevent_idle(struct cpuidle_coupled *coupled) 0710 { 0711 int cpu = get_cpu(); 0712 0713 /* Force all cpus out of the waiting loop. */ 0714 coupled->prevent++; 0715 cpuidle_coupled_poke_others(cpu, coupled); 0716 put_cpu(); 0717 while (!cpuidle_coupled_no_cpus_waiting(coupled)) 0718 cpu_relax(); 0719 } 0720 0721 /** 0722 * cpuidle_coupled_allow_idle - allows cpus to enter a coupled state 0723 * @coupled: the struct coupled that contains the cpu that is changing state 0724 * 0725 * Enables coupled cpuidle on a coupled set of cpus. Used to ensure that 0726 * cpu_online_mask doesn't change while cpus are coordinating coupled idle. 0727 */ 0728 static void cpuidle_coupled_allow_idle(struct cpuidle_coupled *coupled) 0729 { 0730 int cpu = get_cpu(); 0731 0732 /* 0733 * Write barrier ensures readers see the new online_count when they 0734 * see prevent == 0. 0735 */ 0736 smp_wmb(); 0737 coupled->prevent--; 0738 /* Force cpus out of the prevent loop. */ 0739 cpuidle_coupled_poke_others(cpu, coupled); 0740 put_cpu(); 0741 } 0742 0743 static int coupled_cpu_online(unsigned int cpu) 0744 { 0745 struct cpuidle_device *dev; 0746 0747 mutex_lock(&cpuidle_lock); 0748 0749 dev = per_cpu(cpuidle_devices, cpu); 0750 if (dev && dev->coupled) { 0751 cpuidle_coupled_update_online_cpus(dev->coupled); 0752 cpuidle_coupled_allow_idle(dev->coupled); 0753 } 0754 0755 mutex_unlock(&cpuidle_lock); 0756 return 0; 0757 } 0758 0759 static int coupled_cpu_up_prepare(unsigned int cpu) 0760 { 0761 struct cpuidle_device *dev; 0762 0763 mutex_lock(&cpuidle_lock); 0764 0765 dev = per_cpu(cpuidle_devices, cpu); 0766 if (dev && dev->coupled) 0767 cpuidle_coupled_prevent_idle(dev->coupled); 0768 0769 mutex_unlock(&cpuidle_lock); 0770 return 0; 0771 } 0772 0773 static int __init cpuidle_coupled_init(void) 0774 { 0775 int ret; 0776 0777 ret = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE, 0778 "cpuidle/coupled:prepare", 0779 coupled_cpu_up_prepare, 0780 coupled_cpu_online); 0781 if (ret) 0782 return ret; 0783 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 0784 "cpuidle/coupled:online", 0785 coupled_cpu_online, 0786 coupled_cpu_up_prepare); 0787 if (ret < 0) 0788 cpuhp_remove_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE); 0789 return ret; 0790 } 0791 core_initcall(cpuidle_coupled_init);
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |