xref: /openbmc/linux/drivers/cpuidle/coupled.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24126c019SColin Cross /*
34126c019SColin Cross  * coupled.c - helper functions to enter the same idle state on multiple cpus
44126c019SColin Cross  *
54126c019SColin Cross  * Copyright (c) 2011 Google, Inc.
64126c019SColin Cross  *
74126c019SColin Cross  * Author: Colin Cross <ccross@android.com>
84126c019SColin Cross  */
94126c019SColin Cross 
104126c019SColin Cross #include <linux/kernel.h>
114126c019SColin Cross #include <linux/cpu.h>
124126c019SColin Cross #include <linux/cpuidle.h>
134126c019SColin Cross #include <linux/mutex.h>
144126c019SColin Cross #include <linux/sched.h>
154126c019SColin Cross #include <linux/slab.h>
164126c019SColin Cross #include <linux/spinlock.h>
174126c019SColin Cross 
184126c019SColin Cross #include "cpuidle.h"
194126c019SColin Cross 
204126c019SColin Cross /**
214126c019SColin Cross  * DOC: Coupled cpuidle states
224126c019SColin Cross  *
234126c019SColin Cross  * On some ARM SMP SoCs (OMAP4460, Tegra 2, and probably more), the
244126c019SColin Cross  * cpus cannot be independently powered down, either due to
254126c019SColin Cross  * sequencing restrictions (on Tegra 2, cpu 0 must be the last to
264126c019SColin Cross  * power down), or due to HW bugs (on OMAP4460, a cpu powering up
274126c019SColin Cross  * will corrupt the gic state unless the other cpu runs a work
284126c019SColin Cross  * around).  Each cpu has a power state that it can enter without
294126c019SColin Cross  * coordinating with the other cpu (usually Wait For Interrupt, or
304126c019SColin Cross  * WFI), and one or more "coupled" power states that affect blocks
314126c019SColin Cross  * shared between the cpus (L2 cache, interrupt controller, and
324126c019SColin Cross  * sometimes the whole SoC).  Entering a coupled power state must
334126c019SColin Cross  * be tightly controlled on both cpus.
344126c019SColin Cross  *
354126c019SColin Cross  * This file implements a solution, where each cpu will wait in the
364126c019SColin Cross  * WFI state until all cpus are ready to enter a coupled state, at
374126c019SColin Cross  * which point the coupled state function will be called on all
384126c019SColin Cross  * cpus at approximately the same time.
394126c019SColin Cross  *
404126c019SColin Cross  * Once all cpus are ready to enter idle, they are woken by an smp
414126c019SColin Cross  * cross call.  At this point, there is a chance that one of the
424126c019SColin Cross  * cpus will find work to do, and choose not to enter idle.  A
434126c019SColin Cross  * final pass is needed to guarantee that all cpus will call the
444126c019SColin Cross  * power state enter function at the same time.  During this pass,
454126c019SColin Cross  * each cpu will increment the ready counter, and continue once the
464126c019SColin Cross  * ready counter matches the number of online coupled cpus.  If any
474126c019SColin Cross  * cpu exits idle, the other cpus will decrement their counter and
484126c019SColin Cross  * retry.
494126c019SColin Cross  *
504126c019SColin Cross  * requested_state stores the deepest coupled idle state each cpu
514126c019SColin Cross  * is ready for.  It is assumed that the states are indexed from
524126c019SColin Cross  * shallowest (highest power, lowest exit latency) to deepest
534126c019SColin Cross  * (lowest power, highest exit latency).  The requested_state
544126c019SColin Cross  * variable is not locked.  It is only written from the cpu that
554126c019SColin Cross  * it stores (or by the on/offlining cpu if that cpu is offline),
564126c019SColin Cross  * and only read after all the cpus are ready for the coupled idle
57*ea374718SJason Wang  * state are no longer updating it.
584126c019SColin Cross  *
594126c019SColin Cross  * Three atomic counters are used.  alive_count tracks the number
604126c019SColin Cross  * of cpus in the coupled set that are currently or soon will be
614126c019SColin Cross  * online.  waiting_count tracks the number of cpus that are in
624126c019SColin Cross  * the waiting loop, in the ready loop, or in the coupled idle state.
634126c019SColin Cross  * ready_count tracks the number of cpus that are in the ready loop
644126c019SColin Cross  * or in the coupled idle state.
654126c019SColin Cross  *
664126c019SColin Cross  * To use coupled cpuidle states, a cpuidle driver must:
674126c019SColin Cross  *
684126c019SColin Cross  *    Set struct cpuidle_device.coupled_cpus to the mask of all
694126c019SColin Cross  *    coupled cpus, usually the same as cpu_possible_mask if all cpus
704126c019SColin Cross  *    are part of the same cluster.  The coupled_cpus mask must be
714126c019SColin Cross  *    set in the struct cpuidle_device for each cpu.
724126c019SColin Cross  *
734126c019SColin Cross  *    Set struct cpuidle_device.safe_state to a state that is not a
744126c019SColin Cross  *    coupled state.  This is usually WFI.
754126c019SColin Cross  *
764126c019SColin Cross  *    Set CPUIDLE_FLAG_COUPLED in struct cpuidle_state.flags for each
774126c019SColin Cross  *    state that affects multiple cpus.
784126c019SColin Cross  *
794126c019SColin Cross  *    Provide a struct cpuidle_state.enter function for each state
804126c019SColin Cross  *    that affects multiple cpus.  This function is guaranteed to be
814126c019SColin Cross  *    called on all cpus at approximately the same time.  The driver
824126c019SColin Cross  *    should ensure that the cpus all abort together if any cpu tries
834126c019SColin Cross  *    to abort once the function is called.  The function should return
844126c019SColin Cross  *    with interrupts still disabled.
854126c019SColin Cross  */
864126c019SColin Cross 
874126c019SColin Cross /**
884126c019SColin Cross  * struct cpuidle_coupled - data for set of cpus that share a coupled idle state
894126c019SColin Cross  * @coupled_cpus: mask of cpus that are part of the coupled set
904126c019SColin Cross  * @requested_state: array of requested states for cpus in the coupled set
914126c019SColin Cross  * @ready_waiting_counts: combined count of cpus  in ready or waiting loops
9232014c86SBenjamin Gaignard  * @abort_barrier: synchronisation point for abort cases
934126c019SColin Cross  * @online_count: count of cpus that are online
944126c019SColin Cross  * @refcnt: reference count of cpuidle devices that are using this struct
954126c019SColin Cross  * @prevent: flag to prevent coupled idle while a cpu is hotplugging
964126c019SColin Cross  */
974126c019SColin Cross struct cpuidle_coupled {
984126c019SColin Cross 	cpumask_t coupled_cpus;
994126c019SColin Cross 	int requested_state[NR_CPUS];
1004126c019SColin Cross 	atomic_t ready_waiting_counts;
101f983827bSColin Cross 	atomic_t abort_barrier;
1024126c019SColin Cross 	int online_count;
1034126c019SColin Cross 	int refcnt;
1044126c019SColin Cross 	int prevent;
1054126c019SColin Cross };
1064126c019SColin Cross 
1074126c019SColin Cross #define WAITING_BITS 16
1084126c019SColin Cross #define MAX_WAITING_CPUS (1 << WAITING_BITS)
1094126c019SColin Cross #define WAITING_MASK (MAX_WAITING_CPUS - 1)
1104126c019SColin Cross #define READY_MASK (~WAITING_MASK)
1114126c019SColin Cross 
1124126c019SColin Cross #define CPUIDLE_COUPLED_NOT_IDLE	(-1)
1134126c019SColin Cross 
114966a9671SYing Huang static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb);
1154126c019SColin Cross 
1164126c019SColin Cross /*
117f983827bSColin Cross  * The cpuidle_coupled_poke_pending mask is used to avoid calling
118966a9671SYing Huang  * __smp_call_function_single with the per cpu call_single_data_t struct already
1194126c019SColin Cross  * in use.  This prevents a deadlock where two cpus are waiting for each others
120966a9671SYing Huang  * call_single_data_t struct to be available
1214126c019SColin Cross  */
122f983827bSColin Cross static cpumask_t cpuidle_coupled_poke_pending;
123f983827bSColin Cross 
124f983827bSColin Cross /*
125f983827bSColin Cross  * The cpuidle_coupled_poked mask is used to ensure that each cpu has been poked
126f983827bSColin Cross  * once to minimize entering the ready loop with a poke pending, which would
127f983827bSColin Cross  * require aborting and retrying.
128f983827bSColin Cross  */
129f983827bSColin Cross static cpumask_t cpuidle_coupled_poked;
1304126c019SColin Cross 
1314126c019SColin Cross /**
13220ff51a3SColin Cross  * cpuidle_coupled_parallel_barrier - synchronize all online coupled cpus
13320ff51a3SColin Cross  * @dev: cpuidle_device of the calling cpu
13420ff51a3SColin Cross  * @a:   atomic variable to hold the barrier
13520ff51a3SColin Cross  *
13620ff51a3SColin Cross  * No caller to this function will return from this function until all online
13720ff51a3SColin Cross  * cpus in the same coupled group have called this function.  Once any caller
13820ff51a3SColin Cross  * has returned from this function, the barrier is immediately available for
13920ff51a3SColin Cross  * reuse.
14020ff51a3SColin Cross  *
141caf4a36eSViresh Kumar  * The atomic variable must be initialized to 0 before any cpu calls
14220ff51a3SColin Cross  * this function, will be reset to 0 before any cpu returns from this function.
14320ff51a3SColin Cross  *
14420ff51a3SColin Cross  * Must only be called from within a coupled idle state handler
14520ff51a3SColin Cross  * (state.enter when state.flags has CPUIDLE_FLAG_COUPLED set).
14620ff51a3SColin Cross  *
14720ff51a3SColin Cross  * Provides full smp barrier semantics before and after calling.
14820ff51a3SColin Cross  */
cpuidle_coupled_parallel_barrier(struct cpuidle_device * dev,atomic_t * a)14920ff51a3SColin Cross void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
15020ff51a3SColin Cross {
15120ff51a3SColin Cross 	int n = dev->coupled->online_count;
15220ff51a3SColin Cross 
1534e857c58SPeter Zijlstra 	smp_mb__before_atomic();
15420ff51a3SColin Cross 	atomic_inc(a);
15520ff51a3SColin Cross 
15620ff51a3SColin Cross 	while (atomic_read(a) < n)
15720ff51a3SColin Cross 		cpu_relax();
15820ff51a3SColin Cross 
15920ff51a3SColin Cross 	if (atomic_inc_return(a) == n * 2) {
16020ff51a3SColin Cross 		atomic_set(a, 0);
16120ff51a3SColin Cross 		return;
16220ff51a3SColin Cross 	}
16320ff51a3SColin Cross 
16420ff51a3SColin Cross 	while (atomic_read(a) > n)
16520ff51a3SColin Cross 		cpu_relax();
16620ff51a3SColin Cross }
16720ff51a3SColin Cross 
16820ff51a3SColin Cross /**
1694126c019SColin Cross  * cpuidle_state_is_coupled - check if a state is part of a coupled set
1704126c019SColin Cross  * @drv: struct cpuidle_driver for the platform
1714126c019SColin Cross  * @state: index of the target state in drv->states
1724126c019SColin Cross  *
1734126c019SColin Cross  * Returns true if the target state is coupled with cpus besides this one
1744126c019SColin Cross  */
cpuidle_state_is_coupled(struct cpuidle_driver * drv,int state)1754c1ed5a6SXunlei Pang bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)
1764126c019SColin Cross {
1774126c019SColin Cross 	return drv->states[state].flags & CPUIDLE_FLAG_COUPLED;
1784126c019SColin Cross }
1794126c019SColin Cross 
1804126c019SColin Cross /**
181abceaa9cSXunlei Pang  * cpuidle_coupled_state_verify - check if the coupled states are correctly set.
182abceaa9cSXunlei Pang  * @drv: struct cpuidle_driver for the platform
183abceaa9cSXunlei Pang  *
184abceaa9cSXunlei Pang  * Returns 0 for valid state values, a negative error code otherwise:
185abceaa9cSXunlei Pang  *  * -EINVAL if any coupled state(safe_state_index) is wrongly set.
186abceaa9cSXunlei Pang  */
cpuidle_coupled_state_verify(struct cpuidle_driver * drv)187abceaa9cSXunlei Pang int cpuidle_coupled_state_verify(struct cpuidle_driver *drv)
188abceaa9cSXunlei Pang {
189abceaa9cSXunlei Pang 	int i;
190abceaa9cSXunlei Pang 
191abceaa9cSXunlei Pang 	for (i = drv->state_count - 1; i >= 0; i--) {
192abceaa9cSXunlei Pang 		if (cpuidle_state_is_coupled(drv, i) &&
193abceaa9cSXunlei Pang 		    (drv->safe_state_index == i ||
194abceaa9cSXunlei Pang 		     drv->safe_state_index < 0 ||
195abceaa9cSXunlei Pang 		     drv->safe_state_index >= drv->state_count))
196abceaa9cSXunlei Pang 			return -EINVAL;
197abceaa9cSXunlei Pang 	}
198abceaa9cSXunlei Pang 
199abceaa9cSXunlei Pang 	return 0;
200abceaa9cSXunlei Pang }
201abceaa9cSXunlei Pang 
202abceaa9cSXunlei Pang /**
2034126c019SColin Cross  * cpuidle_coupled_set_ready - mark a cpu as ready
2044126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2054126c019SColin Cross  */
cpuidle_coupled_set_ready(struct cpuidle_coupled * coupled)2064126c019SColin Cross static inline void cpuidle_coupled_set_ready(struct cpuidle_coupled *coupled)
2074126c019SColin Cross {
2084126c019SColin Cross 	atomic_add(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
2094126c019SColin Cross }
2104126c019SColin Cross 
2114126c019SColin Cross /**
2124126c019SColin Cross  * cpuidle_coupled_set_not_ready - mark a cpu as not ready
2134126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2144126c019SColin Cross  *
2154126c019SColin Cross  * Decrements the ready counter, unless the ready (and thus the waiting) counter
2164126c019SColin Cross  * is equal to the number of online cpus.  Prevents a race where one cpu
2174126c019SColin Cross  * decrements the waiting counter and then re-increments it just before another
2184126c019SColin Cross  * cpu has decremented its ready counter, leading to the ready counter going
2194126c019SColin Cross  * down from the number of online cpus without going through the coupled idle
2204126c019SColin Cross  * state.
2214126c019SColin Cross  *
2224126c019SColin Cross  * Returns 0 if the counter was decremented successfully, -EINVAL if the ready
2234126c019SColin Cross  * counter was equal to the number of online cpus.
2244126c019SColin Cross  */
2254126c019SColin Cross static
cpuidle_coupled_set_not_ready(struct cpuidle_coupled * coupled)2264126c019SColin Cross inline int cpuidle_coupled_set_not_ready(struct cpuidle_coupled *coupled)
2274126c019SColin Cross {
2284126c019SColin Cross 	int all;
2294126c019SColin Cross 	int ret;
2304126c019SColin Cross 
23192638e2fSSivaram Nair 	all = coupled->online_count | (coupled->online_count << WAITING_BITS);
2324126c019SColin Cross 	ret = atomic_add_unless(&coupled->ready_waiting_counts,
2334126c019SColin Cross 		-MAX_WAITING_CPUS, all);
2344126c019SColin Cross 
2354126c019SColin Cross 	return ret ? 0 : -EINVAL;
2364126c019SColin Cross }
2374126c019SColin Cross 
2384126c019SColin Cross /**
2394126c019SColin Cross  * cpuidle_coupled_no_cpus_ready - check if no cpus in a coupled set are ready
2404126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2414126c019SColin Cross  *
2424126c019SColin Cross  * Returns true if all of the cpus in a coupled set are out of the ready loop.
2434126c019SColin Cross  */
cpuidle_coupled_no_cpus_ready(struct cpuidle_coupled * coupled)2444126c019SColin Cross static inline int cpuidle_coupled_no_cpus_ready(struct cpuidle_coupled *coupled)
2454126c019SColin Cross {
2464126c019SColin Cross 	int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
2474126c019SColin Cross 	return r == 0;
2484126c019SColin Cross }
2494126c019SColin Cross 
2504126c019SColin Cross /**
2514126c019SColin Cross  * cpuidle_coupled_cpus_ready - check if all cpus in a coupled set are ready
2524126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2534126c019SColin Cross  *
2544126c019SColin Cross  * Returns true if all cpus coupled to this target state are in the ready loop
2554126c019SColin Cross  */
cpuidle_coupled_cpus_ready(struct cpuidle_coupled * coupled)2564126c019SColin Cross static inline bool cpuidle_coupled_cpus_ready(struct cpuidle_coupled *coupled)
2574126c019SColin Cross {
2584126c019SColin Cross 	int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
2594126c019SColin Cross 	return r == coupled->online_count;
2604126c019SColin Cross }
2614126c019SColin Cross 
2624126c019SColin Cross /**
2634126c019SColin Cross  * cpuidle_coupled_cpus_waiting - check if all cpus in a coupled set are waiting
2644126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2654126c019SColin Cross  *
2664126c019SColin Cross  * Returns true if all cpus coupled to this target state are in the wait loop
2674126c019SColin Cross  */
cpuidle_coupled_cpus_waiting(struct cpuidle_coupled * coupled)2684126c019SColin Cross static inline bool cpuidle_coupled_cpus_waiting(struct cpuidle_coupled *coupled)
2694126c019SColin Cross {
2704126c019SColin Cross 	int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
2714126c019SColin Cross 	return w == coupled->online_count;
2724126c019SColin Cross }
2734126c019SColin Cross 
2744126c019SColin Cross /**
2754126c019SColin Cross  * cpuidle_coupled_no_cpus_waiting - check if no cpus in coupled set are waiting
2764126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2774126c019SColin Cross  *
2784126c019SColin Cross  * Returns true if all of the cpus in a coupled set are out of the waiting loop.
2794126c019SColin Cross  */
cpuidle_coupled_no_cpus_waiting(struct cpuidle_coupled * coupled)2804126c019SColin Cross static inline int cpuidle_coupled_no_cpus_waiting(struct cpuidle_coupled *coupled)
2814126c019SColin Cross {
2824126c019SColin Cross 	int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
2834126c019SColin Cross 	return w == 0;
2844126c019SColin Cross }
2854126c019SColin Cross 
2864126c019SColin Cross /**
2874126c019SColin Cross  * cpuidle_coupled_get_state - determine the deepest idle state
2884126c019SColin Cross  * @dev: struct cpuidle_device for this cpu
2894126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
2904126c019SColin Cross  *
2914126c019SColin Cross  * Returns the deepest idle state that all coupled cpus can enter
2924126c019SColin Cross  */
cpuidle_coupled_get_state(struct cpuidle_device * dev,struct cpuidle_coupled * coupled)2934126c019SColin Cross static inline int cpuidle_coupled_get_state(struct cpuidle_device *dev,
2944126c019SColin Cross 		struct cpuidle_coupled *coupled)
2954126c019SColin Cross {
2964126c019SColin Cross 	int i;
2974126c019SColin Cross 	int state = INT_MAX;
2984126c019SColin Cross 
2994126c019SColin Cross 	/*
3004126c019SColin Cross 	 * Read barrier ensures that read of requested_state is ordered after
3014126c019SColin Cross 	 * reads of ready_count.  Matches the write barriers
3024126c019SColin Cross 	 * cpuidle_set_state_waiting.
3034126c019SColin Cross 	 */
3044126c019SColin Cross 	smp_rmb();
3054126c019SColin Cross 
306f9b531feSRusty Russell 	for_each_cpu(i, &coupled->coupled_cpus)
3074126c019SColin Cross 		if (cpu_online(i) && coupled->requested_state[i] < state)
3084126c019SColin Cross 			state = coupled->requested_state[i];
3094126c019SColin Cross 
3104126c019SColin Cross 	return state;
3114126c019SColin Cross }
3124126c019SColin Cross 
cpuidle_coupled_handle_poke(void * info)313f983827bSColin Cross static void cpuidle_coupled_handle_poke(void *info)
3144126c019SColin Cross {
3154126c019SColin Cross 	int cpu = (unsigned long)info;
316f983827bSColin Cross 	cpumask_set_cpu(cpu, &cpuidle_coupled_poked);
317f983827bSColin Cross 	cpumask_clear_cpu(cpu, &cpuidle_coupled_poke_pending);
3184126c019SColin Cross }
3194126c019SColin Cross 
3204126c019SColin Cross /**
3214126c019SColin Cross  * cpuidle_coupled_poke - wake up a cpu that may be waiting
3224126c019SColin Cross  * @cpu: target cpu
3234126c019SColin Cross  *
3244126c019SColin Cross  * Ensures that the target cpu exits it's waiting idle state (if it is in it)
3254126c019SColin Cross  * and will see updates to waiting_count before it re-enters it's waiting idle
3264126c019SColin Cross  * state.
3274126c019SColin Cross  *
3284126c019SColin Cross  * If cpuidle_coupled_poked_mask is already set for the target cpu, that cpu
3294126c019SColin Cross  * either has or will soon have a pending IPI that will wake it out of idle,
3304126c019SColin Cross  * or it is currently processing the IPI and is not in idle.
3314126c019SColin Cross  */
cpuidle_coupled_poke(int cpu)3324126c019SColin Cross static void cpuidle_coupled_poke(int cpu)
3334126c019SColin Cross {
334966a9671SYing Huang 	call_single_data_t *csd = &per_cpu(cpuidle_coupled_poke_cb, cpu);
3354126c019SColin Cross 
336f983827bSColin Cross 	if (!cpumask_test_and_set_cpu(cpu, &cpuidle_coupled_poke_pending))
337c46fff2aSFrederic Weisbecker 		smp_call_function_single_async(cpu, csd);
3384126c019SColin Cross }
3394126c019SColin Cross 
3404126c019SColin Cross /**
3414126c019SColin Cross  * cpuidle_coupled_poke_others - wake up all other cpus that may be waiting
34232014c86SBenjamin Gaignard  * @this_cpu: target cpu
3434126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
3444126c019SColin Cross  *
3454126c019SColin Cross  * Calls cpuidle_coupled_poke on all other online cpus.
3464126c019SColin Cross  */
cpuidle_coupled_poke_others(int this_cpu,struct cpuidle_coupled * coupled)3474126c019SColin Cross static void cpuidle_coupled_poke_others(int this_cpu,
3484126c019SColin Cross 		struct cpuidle_coupled *coupled)
3494126c019SColin Cross {
3504126c019SColin Cross 	int cpu;
3514126c019SColin Cross 
352f9b531feSRusty Russell 	for_each_cpu(cpu, &coupled->coupled_cpus)
3534126c019SColin Cross 		if (cpu != this_cpu && cpu_online(cpu))
3544126c019SColin Cross 			cpuidle_coupled_poke(cpu);
3554126c019SColin Cross }
3564126c019SColin Cross 
3574126c019SColin Cross /**
3584126c019SColin Cross  * cpuidle_coupled_set_waiting - mark this cpu as in the wait loop
35932014c86SBenjamin Gaignard  * @cpu: target cpu
3604126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
3614126c019SColin Cross  * @next_state: the index in drv->states of the requested state for this cpu
3624126c019SColin Cross  *
363f983827bSColin Cross  * Updates the requested idle state for the specified cpuidle device.
364f983827bSColin Cross  * Returns the number of waiting cpus.
3654126c019SColin Cross  */
cpuidle_coupled_set_waiting(int cpu,struct cpuidle_coupled * coupled,int next_state)366f983827bSColin Cross static int cpuidle_coupled_set_waiting(int cpu,
3674126c019SColin Cross 		struct cpuidle_coupled *coupled, int next_state)
3684126c019SColin Cross {
3694126c019SColin Cross 	coupled->requested_state[cpu] = next_state;
3704126c019SColin Cross 
3714126c019SColin Cross 	/*
3724126c019SColin Cross 	 * The atomic_inc_return provides a write barrier to order the write
3734126c019SColin Cross 	 * to requested_state with the later write that increments ready_count.
3744126c019SColin Cross 	 */
375f983827bSColin Cross 	return atomic_inc_return(&coupled->ready_waiting_counts) & WAITING_MASK;
3764126c019SColin Cross }
3774126c019SColin Cross 
3784126c019SColin Cross /**
3794126c019SColin Cross  * cpuidle_coupled_set_not_waiting - mark this cpu as leaving the wait loop
38032014c86SBenjamin Gaignard  * @cpu: target cpu
3814126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
3824126c019SColin Cross  *
3834126c019SColin Cross  * Removes the requested idle state for the specified cpuidle device.
3844126c019SColin Cross  */
cpuidle_coupled_set_not_waiting(int cpu,struct cpuidle_coupled * coupled)3854126c019SColin Cross static void cpuidle_coupled_set_not_waiting(int cpu,
3864126c019SColin Cross 		struct cpuidle_coupled *coupled)
3874126c019SColin Cross {
3884126c019SColin Cross 	/*
3894126c019SColin Cross 	 * Decrementing waiting count can race with incrementing it in
3904126c019SColin Cross 	 * cpuidle_coupled_set_waiting, but that's OK.  Worst case, some
3914126c019SColin Cross 	 * cpus will increment ready_count and then spin until they
3924126c019SColin Cross 	 * notice that this cpu has cleared it's requested_state.
3934126c019SColin Cross 	 */
3944126c019SColin Cross 	atomic_dec(&coupled->ready_waiting_counts);
3954126c019SColin Cross 
3964126c019SColin Cross 	coupled->requested_state[cpu] = CPUIDLE_COUPLED_NOT_IDLE;
3974126c019SColin Cross }
3984126c019SColin Cross 
3994126c019SColin Cross /**
4004126c019SColin Cross  * cpuidle_coupled_set_done - mark this cpu as leaving the ready loop
4014126c019SColin Cross  * @cpu: the current cpu
4024126c019SColin Cross  * @coupled: the struct coupled that contains the current cpu
4034126c019SColin Cross  *
4044126c019SColin Cross  * Marks this cpu as no longer in the ready and waiting loops.  Decrements
4054126c019SColin Cross  * the waiting count first to prevent another cpu looping back in and seeing
4064126c019SColin Cross  * this cpu as waiting just before it exits idle.
4074126c019SColin Cross  */
cpuidle_coupled_set_done(int cpu,struct cpuidle_coupled * coupled)4084126c019SColin Cross static void cpuidle_coupled_set_done(int cpu, struct cpuidle_coupled *coupled)
4094126c019SColin Cross {
4104126c019SColin Cross 	cpuidle_coupled_set_not_waiting(cpu, coupled);
4114126c019SColin Cross 	atomic_sub(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
4124126c019SColin Cross }
4134126c019SColin Cross 
4144126c019SColin Cross /**
4154126c019SColin Cross  * cpuidle_coupled_clear_pokes - spin until the poke interrupt is processed
41632014c86SBenjamin Gaignard  * @cpu: this cpu
4174126c019SColin Cross  *
4184126c019SColin Cross  * Turns on interrupts and spins until any outstanding poke interrupts have
4194126c019SColin Cross  * been processed and the poke bit has been cleared.
4204126c019SColin Cross  *
4214126c019SColin Cross  * Other interrupts may also be processed while interrupts are enabled, so
4229e19b73cSColin Cross  * need_resched() must be tested after this function returns to make sure
4234126c019SColin Cross  * the interrupt didn't schedule work that should take the cpu out of idle.
4244126c019SColin Cross  *
4259e19b73cSColin Cross  * Returns 0 if no poke was pending, 1 if a poke was cleared.
4264126c019SColin Cross  */
cpuidle_coupled_clear_pokes(int cpu)4274126c019SColin Cross static int cpuidle_coupled_clear_pokes(int cpu)
4284126c019SColin Cross {
4299e19b73cSColin Cross 	if (!cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending))
4309e19b73cSColin Cross 		return 0;
4319e19b73cSColin Cross 
4324126c019SColin Cross 	local_irq_enable();
433f983827bSColin Cross 	while (cpumask_test_cpu(cpu, &cpuidle_coupled_poke_pending))
4344126c019SColin Cross 		cpu_relax();
4354126c019SColin Cross 	local_irq_disable();
4364126c019SColin Cross 
4379e19b73cSColin Cross 	return 1;
4384126c019SColin Cross }
4394126c019SColin Cross 
cpuidle_coupled_any_pokes_pending(struct cpuidle_coupled * coupled)440f983827bSColin Cross static bool cpuidle_coupled_any_pokes_pending(struct cpuidle_coupled *coupled)
441f983827bSColin Cross {
442f983827bSColin Cross 	cpumask_t cpus;
443f983827bSColin Cross 	int ret;
444f983827bSColin Cross 
445f983827bSColin Cross 	cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus);
446f983827bSColin Cross 	ret = cpumask_and(&cpus, &cpuidle_coupled_poke_pending, &cpus);
447f983827bSColin Cross 
448f983827bSColin Cross 	return ret;
449f983827bSColin Cross }
450f983827bSColin Cross 
4514126c019SColin Cross /**
4524126c019SColin Cross  * cpuidle_enter_state_coupled - attempt to enter a state with coupled cpus
4534126c019SColin Cross  * @dev: struct cpuidle_device for the current cpu
4544126c019SColin Cross  * @drv: struct cpuidle_driver for the platform
4554126c019SColin Cross  * @next_state: index of the requested state in drv->states
4564126c019SColin Cross  *
4574126c019SColin Cross  * Coordinate with coupled cpus to enter the target state.  This is a two
4584126c019SColin Cross  * stage process.  In the first stage, the cpus are operating independently,
4594126c019SColin Cross  * and may call into cpuidle_enter_state_coupled at completely different times.
4604126c019SColin Cross  * To save as much power as possible, the first cpus to call this function will
4614126c019SColin Cross  * go to an intermediate state (the cpuidle_device's safe state), and wait for
4624126c019SColin Cross  * all the other cpus to call this function.  Once all coupled cpus are idle,
4634126c019SColin Cross  * the second stage will start.  Each coupled cpu will spin until all cpus have
4644126c019SColin Cross  * guaranteed that they will call the target_state.
4654126c019SColin Cross  *
4664126c019SColin Cross  * This function must be called with interrupts disabled.  It may enable
4674126c019SColin Cross  * interrupts while preparing for idle, and it will always return with
4684126c019SColin Cross  * interrupts enabled.
4694126c019SColin Cross  */
cpuidle_enter_state_coupled(struct cpuidle_device * dev,struct cpuidle_driver * drv,int next_state)4704126c019SColin Cross int cpuidle_enter_state_coupled(struct cpuidle_device *dev,
4714126c019SColin Cross 		struct cpuidle_driver *drv, int next_state)
4724126c019SColin Cross {
4734126c019SColin Cross 	int entered_state = -1;
4744126c019SColin Cross 	struct cpuidle_coupled *coupled = dev->coupled;
475f983827bSColin Cross 	int w;
4764126c019SColin Cross 
4774126c019SColin Cross 	if (!coupled)
4784126c019SColin Cross 		return -EINVAL;
4794126c019SColin Cross 
4804126c019SColin Cross 	while (coupled->prevent) {
4819e19b73cSColin Cross 		cpuidle_coupled_clear_pokes(dev->cpu);
4829e19b73cSColin Cross 		if (need_resched()) {
4834126c019SColin Cross 			local_irq_enable();
4844126c019SColin Cross 			return entered_state;
4854126c019SColin Cross 		}
4864126c019SColin Cross 		entered_state = cpuidle_enter_state(dev, drv,
487ba6a860dSXunlei Pang 			drv->safe_state_index);
48859e99856SColin Cross 		local_irq_disable();
4894126c019SColin Cross 	}
4904126c019SColin Cross 
4914126c019SColin Cross 	/* Read barrier ensures online_count is read after prevent is cleared */
4924126c019SColin Cross 	smp_rmb();
4934126c019SColin Cross 
494f983827bSColin Cross reset:
495f983827bSColin Cross 	cpumask_clear_cpu(dev->cpu, &cpuidle_coupled_poked);
496f983827bSColin Cross 
497f983827bSColin Cross 	w = cpuidle_coupled_set_waiting(dev->cpu, coupled, next_state);
498f983827bSColin Cross 	/*
499f983827bSColin Cross 	 * If this is the last cpu to enter the waiting state, poke
500f983827bSColin Cross 	 * all the other cpus out of their waiting state so they can
501f983827bSColin Cross 	 * enter a deeper state.  This can race with one of the cpus
502f983827bSColin Cross 	 * exiting the waiting state due to an interrupt and
503f983827bSColin Cross 	 * decrementing waiting_count, see comment below.
504f983827bSColin Cross 	 */
505f983827bSColin Cross 	if (w == coupled->online_count) {
506f983827bSColin Cross 		cpumask_set_cpu(dev->cpu, &cpuidle_coupled_poked);
507f983827bSColin Cross 		cpuidle_coupled_poke_others(dev->cpu, coupled);
508f983827bSColin Cross 	}
5094126c019SColin Cross 
5104126c019SColin Cross retry:
5114126c019SColin Cross 	/*
5124126c019SColin Cross 	 * Wait for all coupled cpus to be idle, using the deepest state
513f983827bSColin Cross 	 * allowed for a single cpu.  If this was not the poking cpu, wait
514f983827bSColin Cross 	 * for at least one poke before leaving to avoid a race where
515f983827bSColin Cross 	 * two cpus could arrive at the waiting loop at the same time,
516f983827bSColin Cross 	 * but the first of the two to arrive could skip the loop without
517f983827bSColin Cross 	 * processing the pokes from the last to arrive.
5184126c019SColin Cross 	 */
519f983827bSColin Cross 	while (!cpuidle_coupled_cpus_waiting(coupled) ||
520f983827bSColin Cross 			!cpumask_test_cpu(dev->cpu, &cpuidle_coupled_poked)) {
5219e19b73cSColin Cross 		if (cpuidle_coupled_clear_pokes(dev->cpu))
5229e19b73cSColin Cross 			continue;
5239e19b73cSColin Cross 
5249e19b73cSColin Cross 		if (need_resched()) {
5254126c019SColin Cross 			cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
5264126c019SColin Cross 			goto out;
5274126c019SColin Cross 		}
5284126c019SColin Cross 
5294126c019SColin Cross 		if (coupled->prevent) {
5304126c019SColin Cross 			cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
5314126c019SColin Cross 			goto out;
5324126c019SColin Cross 		}
5334126c019SColin Cross 
5344126c019SColin Cross 		entered_state = cpuidle_enter_state(dev, drv,
535ba6a860dSXunlei Pang 			drv->safe_state_index);
53659e99856SColin Cross 		local_irq_disable();
5374126c019SColin Cross 	}
5384126c019SColin Cross 
5399e19b73cSColin Cross 	cpuidle_coupled_clear_pokes(dev->cpu);
5409e19b73cSColin Cross 	if (need_resched()) {
5414126c019SColin Cross 		cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
5424126c019SColin Cross 		goto out;
5434126c019SColin Cross 	}
5444126c019SColin Cross 
5454126c019SColin Cross 	/*
546f983827bSColin Cross 	 * Make sure final poke status for this cpu is visible before setting
547f983827bSColin Cross 	 * cpu as ready.
548f983827bSColin Cross 	 */
549f983827bSColin Cross 	smp_wmb();
550f983827bSColin Cross 
551f983827bSColin Cross 	/*
5524126c019SColin Cross 	 * All coupled cpus are probably idle.  There is a small chance that
5534126c019SColin Cross 	 * one of the other cpus just became active.  Increment the ready count,
5544126c019SColin Cross 	 * and spin until all coupled cpus have incremented the counter. Once a
5554126c019SColin Cross 	 * cpu has incremented the ready counter, it cannot abort idle and must
5564126c019SColin Cross 	 * spin until either all cpus have incremented the ready counter, or
5574126c019SColin Cross 	 * another cpu leaves idle and decrements the waiting counter.
5584126c019SColin Cross 	 */
5594126c019SColin Cross 
5604126c019SColin Cross 	cpuidle_coupled_set_ready(coupled);
5614126c019SColin Cross 	while (!cpuidle_coupled_cpus_ready(coupled)) {
5624126c019SColin Cross 		/* Check if any other cpus bailed out of idle. */
5634126c019SColin Cross 		if (!cpuidle_coupled_cpus_waiting(coupled))
5644126c019SColin Cross 			if (!cpuidle_coupled_set_not_ready(coupled))
5654126c019SColin Cross 				goto retry;
5664126c019SColin Cross 
5674126c019SColin Cross 		cpu_relax();
5684126c019SColin Cross 	}
5694126c019SColin Cross 
570f983827bSColin Cross 	/*
571f983827bSColin Cross 	 * Make sure read of all cpus ready is done before reading pending pokes
572f983827bSColin Cross 	 */
573f983827bSColin Cross 	smp_rmb();
574f983827bSColin Cross 
575f983827bSColin Cross 	/*
576f983827bSColin Cross 	 * There is a small chance that a cpu left and reentered idle after this
577f983827bSColin Cross 	 * cpu saw that all cpus were waiting.  The cpu that reentered idle will
578f983827bSColin Cross 	 * have sent this cpu a poke, which will still be pending after the
579f983827bSColin Cross 	 * ready loop.  The pending interrupt may be lost by the interrupt
580f983827bSColin Cross 	 * controller when entering the deep idle state.  It's not possible to
581f983827bSColin Cross 	 * clear a pending interrupt without turning interrupts on and handling
582f983827bSColin Cross 	 * it, and it's too late to turn on interrupts here, so reset the
583f983827bSColin Cross 	 * coupled idle state of all cpus and retry.
584f983827bSColin Cross 	 */
585f983827bSColin Cross 	if (cpuidle_coupled_any_pokes_pending(coupled)) {
586f983827bSColin Cross 		cpuidle_coupled_set_done(dev->cpu, coupled);
587f983827bSColin Cross 		/* Wait for all cpus to see the pending pokes */
588f983827bSColin Cross 		cpuidle_coupled_parallel_barrier(dev, &coupled->abort_barrier);
589f983827bSColin Cross 		goto reset;
590f983827bSColin Cross 	}
591f983827bSColin Cross 
5924126c019SColin Cross 	/* all cpus have acked the coupled state */
5934126c019SColin Cross 	next_state = cpuidle_coupled_get_state(dev, coupled);
5944126c019SColin Cross 
5954126c019SColin Cross 	entered_state = cpuidle_enter_state(dev, drv, next_state);
5964126c019SColin Cross 
5974126c019SColin Cross 	cpuidle_coupled_set_done(dev->cpu, coupled);
5984126c019SColin Cross 
5994126c019SColin Cross out:
6004126c019SColin Cross 	/*
6014126c019SColin Cross 	 * Normal cpuidle states are expected to return with irqs enabled.
6024126c019SColin Cross 	 * That leads to an inefficiency where a cpu receiving an interrupt
6034126c019SColin Cross 	 * that brings it out of idle will process that interrupt before
6044126c019SColin Cross 	 * exiting the idle enter function and decrementing ready_count.  All
6054126c019SColin Cross 	 * other cpus will need to spin waiting for the cpu that is processing
6064126c019SColin Cross 	 * the interrupt.  If the driver returns with interrupts disabled,
6074126c019SColin Cross 	 * all other cpus will loop back into the safe idle state instead of
6084126c019SColin Cross 	 * spinning, saving power.
6094126c019SColin Cross 	 *
6104126c019SColin Cross 	 * Calling local_irq_enable here allows coupled states to return with
6114126c019SColin Cross 	 * interrupts disabled, but won't cause problems for drivers that
6124126c019SColin Cross 	 * exit with interrupts enabled.
6134126c019SColin Cross 	 */
6144126c019SColin Cross 	local_irq_enable();
6154126c019SColin Cross 
6164126c019SColin Cross 	/*
6174126c019SColin Cross 	 * Wait until all coupled cpus have exited idle.  There is no risk that
6184126c019SColin Cross 	 * a cpu exits and re-enters the ready state because this cpu has
6194126c019SColin Cross 	 * already decremented its waiting_count.
6204126c019SColin Cross 	 */
6214126c019SColin Cross 	while (!cpuidle_coupled_no_cpus_ready(coupled))
6224126c019SColin Cross 		cpu_relax();
6234126c019SColin Cross 
6244126c019SColin Cross 	return entered_state;
6254126c019SColin Cross }
6264126c019SColin Cross 
cpuidle_coupled_update_online_cpus(struct cpuidle_coupled * coupled)6274126c019SColin Cross static void cpuidle_coupled_update_online_cpus(struct cpuidle_coupled *coupled)
6284126c019SColin Cross {
6294126c019SColin Cross 	cpumask_t cpus;
6304126c019SColin Cross 	cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus);
6314126c019SColin Cross 	coupled->online_count = cpumask_weight(&cpus);
6324126c019SColin Cross }
6334126c019SColin Cross 
6344126c019SColin Cross /**
6354126c019SColin Cross  * cpuidle_coupled_register_device - register a coupled cpuidle device
6364126c019SColin Cross  * @dev: struct cpuidle_device for the current cpu
6374126c019SColin Cross  *
6384126c019SColin Cross  * Called from cpuidle_register_device to handle coupled idle init.  Finds the
6394126c019SColin Cross  * cpuidle_coupled struct for this set of coupled cpus, or creates one if none
6404126c019SColin Cross  * exists yet.
6414126c019SColin Cross  */
cpuidle_coupled_register_device(struct cpuidle_device * dev)6424126c019SColin Cross int cpuidle_coupled_register_device(struct cpuidle_device *dev)
6434126c019SColin Cross {
6444126c019SColin Cross 	int cpu;
6454126c019SColin Cross 	struct cpuidle_device *other_dev;
646966a9671SYing Huang 	call_single_data_t *csd;
6474126c019SColin Cross 	struct cpuidle_coupled *coupled;
6484126c019SColin Cross 
6494126c019SColin Cross 	if (cpumask_empty(&dev->coupled_cpus))
6504126c019SColin Cross 		return 0;
6514126c019SColin Cross 
652f9b531feSRusty Russell 	for_each_cpu(cpu, &dev->coupled_cpus) {
6534126c019SColin Cross 		other_dev = per_cpu(cpuidle_devices, cpu);
6544126c019SColin Cross 		if (other_dev && other_dev->coupled) {
6554126c019SColin Cross 			coupled = other_dev->coupled;
6564126c019SColin Cross 			goto have_coupled;
6574126c019SColin Cross 		}
6584126c019SColin Cross 	}
6594126c019SColin Cross 
6604126c019SColin Cross 	/* No existing coupled info found, create a new one */
6614126c019SColin Cross 	coupled = kzalloc(sizeof(struct cpuidle_coupled), GFP_KERNEL);
6624126c019SColin Cross 	if (!coupled)
6634126c019SColin Cross 		return -ENOMEM;
6644126c019SColin Cross 
6654126c019SColin Cross 	coupled->coupled_cpus = dev->coupled_cpus;
6664126c019SColin Cross 
6674126c019SColin Cross have_coupled:
6684126c019SColin Cross 	dev->coupled = coupled;
6694126c019SColin Cross 	if (WARN_ON(!cpumask_equal(&dev->coupled_cpus, &coupled->coupled_cpus)))
6704126c019SColin Cross 		coupled->prevent++;
6714126c019SColin Cross 
6724126c019SColin Cross 	cpuidle_coupled_update_online_cpus(coupled);
6734126c019SColin Cross 
6744126c019SColin Cross 	coupled->refcnt++;
6754126c019SColin Cross 
6764126c019SColin Cross 	csd = &per_cpu(cpuidle_coupled_poke_cb, dev->cpu);
677545b8c8dSPeter Zijlstra 	INIT_CSD(csd, cpuidle_coupled_handle_poke, (void *)(unsigned long)dev->cpu);
6784126c019SColin Cross 
6794126c019SColin Cross 	return 0;
6804126c019SColin Cross }
6814126c019SColin Cross 
6824126c019SColin Cross /**
6834126c019SColin Cross  * cpuidle_coupled_unregister_device - unregister a coupled cpuidle device
6844126c019SColin Cross  * @dev: struct cpuidle_device for the current cpu
6854126c019SColin Cross  *
6864126c019SColin Cross  * Called from cpuidle_unregister_device to tear down coupled idle.  Removes the
6874126c019SColin Cross  * cpu from the coupled idle set, and frees the cpuidle_coupled_info struct if
6884126c019SColin Cross  * this was the last cpu in the set.
6894126c019SColin Cross  */
cpuidle_coupled_unregister_device(struct cpuidle_device * dev)6904126c019SColin Cross void cpuidle_coupled_unregister_device(struct cpuidle_device *dev)
6914126c019SColin Cross {
6924126c019SColin Cross 	struct cpuidle_coupled *coupled = dev->coupled;
6934126c019SColin Cross 
6944126c019SColin Cross 	if (cpumask_empty(&dev->coupled_cpus))
6954126c019SColin Cross 		return;
6964126c019SColin Cross 
6974126c019SColin Cross 	if (--coupled->refcnt)
6984126c019SColin Cross 		kfree(coupled);
6994126c019SColin Cross 	dev->coupled = NULL;
7004126c019SColin Cross }
7014126c019SColin Cross 
7024126c019SColin Cross /**
7034126c019SColin Cross  * cpuidle_coupled_prevent_idle - prevent cpus from entering a coupled state
7044126c019SColin Cross  * @coupled: the struct coupled that contains the cpu that is changing state
7054126c019SColin Cross  *
7064126c019SColin Cross  * Disables coupled cpuidle on a coupled set of cpus.  Used to ensure that
7074126c019SColin Cross  * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
7084126c019SColin Cross  */
cpuidle_coupled_prevent_idle(struct cpuidle_coupled * coupled)7094126c019SColin Cross static void cpuidle_coupled_prevent_idle(struct cpuidle_coupled *coupled)
7104126c019SColin Cross {
7114126c019SColin Cross 	int cpu = get_cpu();
7124126c019SColin Cross 
7134126c019SColin Cross 	/* Force all cpus out of the waiting loop. */
7144126c019SColin Cross 	coupled->prevent++;
7154126c019SColin Cross 	cpuidle_coupled_poke_others(cpu, coupled);
7164126c019SColin Cross 	put_cpu();
7174126c019SColin Cross 	while (!cpuidle_coupled_no_cpus_waiting(coupled))
7184126c019SColin Cross 		cpu_relax();
7194126c019SColin Cross }
7204126c019SColin Cross 
7214126c019SColin Cross /**
7224126c019SColin Cross  * cpuidle_coupled_allow_idle - allows cpus to enter a coupled state
7234126c019SColin Cross  * @coupled: the struct coupled that contains the cpu that is changing state
7244126c019SColin Cross  *
7254126c019SColin Cross  * Enables coupled cpuidle on a coupled set of cpus.  Used to ensure that
7264126c019SColin Cross  * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
7274126c019SColin Cross  */
cpuidle_coupled_allow_idle(struct cpuidle_coupled * coupled)7284126c019SColin Cross static void cpuidle_coupled_allow_idle(struct cpuidle_coupled *coupled)
7294126c019SColin Cross {
7304126c019SColin Cross 	int cpu = get_cpu();
7314126c019SColin Cross 
7324126c019SColin Cross 	/*
7334126c019SColin Cross 	 * Write barrier ensures readers see the new online_count when they
7344126c019SColin Cross 	 * see prevent == 0.
7354126c019SColin Cross 	 */
7364126c019SColin Cross 	smp_wmb();
7374126c019SColin Cross 	coupled->prevent--;
7384126c019SColin Cross 	/* Force cpus out of the prevent loop. */
7394126c019SColin Cross 	cpuidle_coupled_poke_others(cpu, coupled);
7404126c019SColin Cross 	put_cpu();
7414126c019SColin Cross }
7424126c019SColin Cross 
coupled_cpu_online(unsigned int cpu)743dfc616d8SSebastian Andrzej Siewior static int coupled_cpu_online(unsigned int cpu)
7444126c019SColin Cross {
7454126c019SColin Cross 	struct cpuidle_device *dev;
7464126c019SColin Cross 
7474126c019SColin Cross 	mutex_lock(&cpuidle_lock);
7484126c019SColin Cross 
7494126c019SColin Cross 	dev = per_cpu(cpuidle_devices, cpu);
750dfc616d8SSebastian Andrzej Siewior 	if (dev && dev->coupled) {
7514126c019SColin Cross 		cpuidle_coupled_update_online_cpus(dev->coupled);
7524126c019SColin Cross 		cpuidle_coupled_allow_idle(dev->coupled);
7534126c019SColin Cross 	}
7544126c019SColin Cross 
7554126c019SColin Cross 	mutex_unlock(&cpuidle_lock);
756dfc616d8SSebastian Andrzej Siewior 	return 0;
7574126c019SColin Cross }
7584126c019SColin Cross 
coupled_cpu_up_prepare(unsigned int cpu)759dfc616d8SSebastian Andrzej Siewior static int coupled_cpu_up_prepare(unsigned int cpu)
760dfc616d8SSebastian Andrzej Siewior {
761dfc616d8SSebastian Andrzej Siewior 	struct cpuidle_device *dev;
762dfc616d8SSebastian Andrzej Siewior 
763dfc616d8SSebastian Andrzej Siewior 	mutex_lock(&cpuidle_lock);
764dfc616d8SSebastian Andrzej Siewior 
765dfc616d8SSebastian Andrzej Siewior 	dev = per_cpu(cpuidle_devices, cpu);
766dfc616d8SSebastian Andrzej Siewior 	if (dev && dev->coupled)
767dfc616d8SSebastian Andrzej Siewior 		cpuidle_coupled_prevent_idle(dev->coupled);
768dfc616d8SSebastian Andrzej Siewior 
769dfc616d8SSebastian Andrzej Siewior 	mutex_unlock(&cpuidle_lock);
770dfc616d8SSebastian Andrzej Siewior 	return 0;
771dfc616d8SSebastian Andrzej Siewior }
7724126c019SColin Cross 
cpuidle_coupled_init(void)7734126c019SColin Cross static int __init cpuidle_coupled_init(void)
7744126c019SColin Cross {
775dfc616d8SSebastian Andrzej Siewior 	int ret;
776dfc616d8SSebastian Andrzej Siewior 
777dfc616d8SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE,
778dfc616d8SSebastian Andrzej Siewior 					"cpuidle/coupled:prepare",
779dfc616d8SSebastian Andrzej Siewior 					coupled_cpu_up_prepare,
780dfc616d8SSebastian Andrzej Siewior 					coupled_cpu_online);
781dfc616d8SSebastian Andrzej Siewior 	if (ret)
782dfc616d8SSebastian Andrzej Siewior 		return ret;
783dfc616d8SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
784dfc616d8SSebastian Andrzej Siewior 					"cpuidle/coupled:online",
785dfc616d8SSebastian Andrzej Siewior 					coupled_cpu_online,
786dfc616d8SSebastian Andrzej Siewior 					coupled_cpu_up_prepare);
787dfc616d8SSebastian Andrzej Siewior 	if (ret < 0)
788dfc616d8SSebastian Andrzej Siewior 		cpuhp_remove_state_nocalls(CPUHP_CPUIDLE_COUPLED_PREPARE);
789dfc616d8SSebastian Andrzej Siewior 	return ret;
7904126c019SColin Cross }
7914126c019SColin Cross core_initcall(cpuidle_coupled_init);
792