Lines Matching +full:loop +full:- +full:powered
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * coupled.c - helper functions to enter the same idle state on multiple cpus
24 * cpus cannot be independently powered down, either due to
62 * the waiting loop, in the ready loop, or in the coupled idle state.
63 * ready_count tracks the number of cpus that are in the ready loop
88 * struct cpuidle_coupled - data for set of cpus that share a coupled idle state
109 #define WAITING_MASK (MAX_WAITING_CPUS - 1)
112 #define CPUIDLE_COUPLED_NOT_IDLE (-1)
126 * once to minimize entering the ready loop with a poke pending, which would
132 * cpuidle_coupled_parallel_barrier - synchronize all online coupled cpus
151 int n = dev->coupled->online_count; in cpuidle_coupled_parallel_barrier()
169 * cpuidle_state_is_coupled - check if a state is part of a coupled set
171 * @state: index of the target state in drv->states
177 return drv->states[state].flags & CPUIDLE_FLAG_COUPLED; in cpuidle_state_is_coupled()
181 * cpuidle_coupled_state_verify - check if the coupled states are correctly set.
185 * * -EINVAL if any coupled state(safe_state_index) is wrongly set.
191 for (i = drv->state_count - 1; i >= 0; i--) { in cpuidle_coupled_state_verify()
193 (drv->safe_state_index == i || in cpuidle_coupled_state_verify()
194 drv->safe_state_index < 0 || in cpuidle_coupled_state_verify()
195 drv->safe_state_index >= drv->state_count)) in cpuidle_coupled_state_verify()
196 return -EINVAL; in cpuidle_coupled_state_verify()
203 * cpuidle_coupled_set_ready - mark a cpu as ready
208 atomic_add(MAX_WAITING_CPUS, &coupled->ready_waiting_counts); in cpuidle_coupled_set_ready()
212 * cpuidle_coupled_set_not_ready - mark a cpu as not ready
217 * decrements the waiting counter and then re-increments it just before another
222 * Returns 0 if the counter was decremented successfully, -EINVAL if the ready
231 all = coupled->online_count | (coupled->online_count << WAITING_BITS); in cpuidle_coupled_set_not_ready()
232 ret = atomic_add_unless(&coupled->ready_waiting_counts, in cpuidle_coupled_set_not_ready()
233 -MAX_WAITING_CPUS, all); in cpuidle_coupled_set_not_ready()
235 return ret ? 0 : -EINVAL; in cpuidle_coupled_set_not_ready()
239 * cpuidle_coupled_no_cpus_ready - check if no cpus in a coupled set are ready
242 * Returns true if all of the cpus in a coupled set are out of the ready loop.
246 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS; in cpuidle_coupled_no_cpus_ready()
251 * cpuidle_coupled_cpus_ready - check if all cpus in a coupled set are ready
254 * Returns true if all cpus coupled to this target state are in the ready loop
258 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS; in cpuidle_coupled_cpus_ready()
259 return r == coupled->online_count; in cpuidle_coupled_cpus_ready()
263 * cpuidle_coupled_cpus_waiting - check if all cpus in a coupled set are waiting
266 * Returns true if all cpus coupled to this target state are in the wait loop
270 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK; in cpuidle_coupled_cpus_waiting()
271 return w == coupled->online_count; in cpuidle_coupled_cpus_waiting()
275 * cpuidle_coupled_no_cpus_waiting - check if no cpus in coupled set are waiting
278 * Returns true if all of the cpus in a coupled set are out of the waiting loop.
282 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK; in cpuidle_coupled_no_cpus_waiting()
287 * cpuidle_coupled_get_state - determine the deepest idle state
306 for_each_cpu(i, &coupled->coupled_cpus) in cpuidle_coupled_get_state()
307 if (cpu_online(i) && coupled->requested_state[i] < state) in cpuidle_coupled_get_state()
308 state = coupled->requested_state[i]; in cpuidle_coupled_get_state()
321 * cpuidle_coupled_poke - wake up a cpu that may be waiting
325 * and will see updates to waiting_count before it re-enters it's waiting idle
341 * cpuidle_coupled_poke_others - wake up all other cpus that may be waiting
352 for_each_cpu(cpu, &coupled->coupled_cpus) in cpuidle_coupled_poke_others()
358 * cpuidle_coupled_set_waiting - mark this cpu as in the wait loop
361 * @next_state: the index in drv->states of the requested state for this cpu
369 coupled->requested_state[cpu] = next_state; in cpuidle_coupled_set_waiting()
375 return atomic_inc_return(&coupled->ready_waiting_counts) & WAITING_MASK; in cpuidle_coupled_set_waiting()
379 * cpuidle_coupled_set_not_waiting - mark this cpu as leaving the wait loop
394 atomic_dec(&coupled->ready_waiting_counts); in cpuidle_coupled_set_not_waiting()
396 coupled->requested_state[cpu] = CPUIDLE_COUPLED_NOT_IDLE; in cpuidle_coupled_set_not_waiting()
400 * cpuidle_coupled_set_done - mark this cpu as leaving the ready loop
411 atomic_sub(MAX_WAITING_CPUS, &coupled->ready_waiting_counts); in cpuidle_coupled_set_done()
415 * cpuidle_coupled_clear_pokes - spin until the poke interrupt is processed
445 cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus); in cpuidle_coupled_any_pokes_pending()
452 * cpuidle_enter_state_coupled - attempt to enter a state with coupled cpus
455 * @next_state: index of the requested state in drv->states
473 int entered_state = -1; in cpuidle_enter_state_coupled()
474 struct cpuidle_coupled *coupled = dev->coupled; in cpuidle_enter_state_coupled()
478 return -EINVAL; in cpuidle_enter_state_coupled()
480 while (coupled->prevent) { in cpuidle_enter_state_coupled()
481 cpuidle_coupled_clear_pokes(dev->cpu); in cpuidle_enter_state_coupled()
487 drv->safe_state_index); in cpuidle_enter_state_coupled()
495 cpumask_clear_cpu(dev->cpu, &cpuidle_coupled_poked); in cpuidle_enter_state_coupled()
497 w = cpuidle_coupled_set_waiting(dev->cpu, coupled, next_state); in cpuidle_enter_state_coupled()
505 if (w == coupled->online_count) { in cpuidle_enter_state_coupled()
506 cpumask_set_cpu(dev->cpu, &cpuidle_coupled_poked); in cpuidle_enter_state_coupled()
507 cpuidle_coupled_poke_others(dev->cpu, coupled); in cpuidle_enter_state_coupled()
515 * two cpus could arrive at the waiting loop at the same time, in cpuidle_enter_state_coupled()
516 * but the first of the two to arrive could skip the loop without in cpuidle_enter_state_coupled()
520 !cpumask_test_cpu(dev->cpu, &cpuidle_coupled_poked)) { in cpuidle_enter_state_coupled()
521 if (cpuidle_coupled_clear_pokes(dev->cpu)) in cpuidle_enter_state_coupled()
525 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); in cpuidle_enter_state_coupled()
529 if (coupled->prevent) { in cpuidle_enter_state_coupled()
530 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); in cpuidle_enter_state_coupled()
535 drv->safe_state_index); in cpuidle_enter_state_coupled()
539 cpuidle_coupled_clear_pokes(dev->cpu); in cpuidle_enter_state_coupled()
541 cpuidle_coupled_set_not_waiting(dev->cpu, coupled); in cpuidle_enter_state_coupled()
579 * ready loop. The pending interrupt may be lost by the interrupt in cpuidle_enter_state_coupled()
586 cpuidle_coupled_set_done(dev->cpu, coupled); in cpuidle_enter_state_coupled()
588 cpuidle_coupled_parallel_barrier(dev, &coupled->abort_barrier); in cpuidle_enter_state_coupled()
597 cpuidle_coupled_set_done(dev->cpu, coupled); in cpuidle_enter_state_coupled()
607 * all other cpus will loop back into the safe idle state instead of in cpuidle_enter_state_coupled()
618 * a cpu exits and re-enters the ready state because this cpu has in cpuidle_enter_state_coupled()
630 cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus); in cpuidle_coupled_update_online_cpus()
631 coupled->online_count = cpumask_weight(&cpus); in cpuidle_coupled_update_online_cpus()
635 * cpuidle_coupled_register_device - register a coupled cpuidle device
649 if (cpumask_empty(&dev->coupled_cpus)) in cpuidle_coupled_register_device()
652 for_each_cpu(cpu, &dev->coupled_cpus) { in cpuidle_coupled_register_device()
654 if (other_dev && other_dev->coupled) { in cpuidle_coupled_register_device()
655 coupled = other_dev->coupled; in cpuidle_coupled_register_device()
663 return -ENOMEM; in cpuidle_coupled_register_device()
665 coupled->coupled_cpus = dev->coupled_cpus; in cpuidle_coupled_register_device()
668 dev->coupled = coupled; in cpuidle_coupled_register_device()
669 if (WARN_ON(!cpumask_equal(&dev->coupled_cpus, &coupled->coupled_cpus))) in cpuidle_coupled_register_device()
670 coupled->prevent++; in cpuidle_coupled_register_device()
674 coupled->refcnt++; in cpuidle_coupled_register_device()
676 csd = &per_cpu(cpuidle_coupled_poke_cb, dev->cpu); in cpuidle_coupled_register_device()
677 INIT_CSD(csd, cpuidle_coupled_handle_poke, (void *)(unsigned long)dev->cpu); in cpuidle_coupled_register_device()
683 * cpuidle_coupled_unregister_device - unregister a coupled cpuidle device
692 struct cpuidle_coupled *coupled = dev->coupled; in cpuidle_coupled_unregister_device()
694 if (cpumask_empty(&dev->coupled_cpus)) in cpuidle_coupled_unregister_device()
697 if (--coupled->refcnt) in cpuidle_coupled_unregister_device()
699 dev->coupled = NULL; in cpuidle_coupled_unregister_device()
703 * cpuidle_coupled_prevent_idle - prevent cpus from entering a coupled state
713 /* Force all cpus out of the waiting loop. */ in cpuidle_coupled_prevent_idle()
714 coupled->prevent++; in cpuidle_coupled_prevent_idle()
722 * cpuidle_coupled_allow_idle - allows cpus to enter a coupled state
737 coupled->prevent--; in cpuidle_coupled_allow_idle()
738 /* Force cpus out of the prevent loop. */ in cpuidle_coupled_allow_idle()
750 if (dev && dev->coupled) { in coupled_cpu_online()
751 cpuidle_coupled_update_online_cpus(dev->coupled); in coupled_cpu_online()
752 cpuidle_coupled_allow_idle(dev->coupled); in coupled_cpu_online()
766 if (dev && dev->coupled) in coupled_cpu_up_prepare()
767 cpuidle_coupled_prevent_idle(dev->coupled); in coupled_cpu_up_prepare()