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