1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Created by: Nicolas Pitre, March 2012
6*4882a593Smuzhiyun * Copyright: (C) 2012-2013 Linaro Limited
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/atomic.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/sched/signal.h>
14*4882a593Smuzhiyun #include <uapi/linux/sched/types.h>
15*4882a593Smuzhiyun #include <linux/interrupt.h>
16*4882a593Smuzhiyun #include <linux/cpu_pm.h>
17*4882a593Smuzhiyun #include <linux/cpu.h>
18*4882a593Smuzhiyun #include <linux/cpumask.h>
19*4882a593Smuzhiyun #include <linux/kthread.h>
20*4882a593Smuzhiyun #include <linux/wait.h>
21*4882a593Smuzhiyun #include <linux/time.h>
22*4882a593Smuzhiyun #include <linux/clockchips.h>
23*4882a593Smuzhiyun #include <linux/hrtimer.h>
24*4882a593Smuzhiyun #include <linux/tick.h>
25*4882a593Smuzhiyun #include <linux/notifier.h>
26*4882a593Smuzhiyun #include <linux/mm.h>
27*4882a593Smuzhiyun #include <linux/mutex.h>
28*4882a593Smuzhiyun #include <linux/smp.h>
29*4882a593Smuzhiyun #include <linux/spinlock.h>
30*4882a593Smuzhiyun #include <linux/string.h>
31*4882a593Smuzhiyun #include <linux/sysfs.h>
32*4882a593Smuzhiyun #include <linux/irqchip/arm-gic.h>
33*4882a593Smuzhiyun #include <linux/moduleparam.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <asm/smp_plat.h>
36*4882a593Smuzhiyun #include <asm/cputype.h>
37*4882a593Smuzhiyun #include <asm/suspend.h>
38*4882a593Smuzhiyun #include <asm/mcpm.h>
39*4882a593Smuzhiyun #include <asm/bL_switcher.h>
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
42*4882a593Smuzhiyun #include <trace/events/power_cpu_migrate.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
47*4882a593Smuzhiyun * __attribute_const__ and we don't want the compiler to assume any
48*4882a593Smuzhiyun * constness here as the value _does_ change along some code paths.
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun
read_mpidr(void)51*4882a593Smuzhiyun static int read_mpidr(void)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun unsigned int id;
54*4882a593Smuzhiyun asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
55*4882a593Smuzhiyun return id & MPIDR_HWID_BITMASK;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * bL switcher core code.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun
bL_do_switch(void * _arg)62*4882a593Smuzhiyun static void bL_do_switch(void *_arg)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun unsigned ib_mpidr, ib_cpu, ib_cluster;
65*4882a593Smuzhiyun long volatile handshake, **handshake_ptr = _arg;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun pr_debug("%s\n", __func__);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun ib_mpidr = cpu_logical_map(smp_processor_id());
70*4882a593Smuzhiyun ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
71*4882a593Smuzhiyun ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Advertise our handshake location */
74*4882a593Smuzhiyun if (handshake_ptr) {
75*4882a593Smuzhiyun handshake = 0;
76*4882a593Smuzhiyun *handshake_ptr = &handshake;
77*4882a593Smuzhiyun } else
78*4882a593Smuzhiyun handshake = -1;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Our state has been saved at this point. Let's release our
82*4882a593Smuzhiyun * inbound CPU.
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
85*4882a593Smuzhiyun sev();
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun * From this point, we must assume that our counterpart CPU might
89*4882a593Smuzhiyun * have taken over in its parallel world already, as if execution
90*4882a593Smuzhiyun * just returned from cpu_suspend(). It is therefore important to
91*4882a593Smuzhiyun * be very careful not to make any change the other guy is not
92*4882a593Smuzhiyun * expecting. This is why we need stack isolation.
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * Fancy under cover tasks could be performed here. For now
95*4882a593Smuzhiyun * we have none.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * Let's wait until our inbound is alive.
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun while (!handshake) {
102*4882a593Smuzhiyun wfe();
103*4882a593Smuzhiyun smp_mb();
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Let's put ourself down. */
107*4882a593Smuzhiyun mcpm_cpu_power_down();
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* should never get here */
110*4882a593Smuzhiyun BUG();
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * Stack isolation. To ensure 'current' remains valid, we just use another
115*4882a593Smuzhiyun * piece of our thread's stack space which should be fairly lightly used.
116*4882a593Smuzhiyun * The selected area starts just above the thread_info structure located
117*4882a593Smuzhiyun * at the very bottom of the stack, aligned to a cache line, and indexed
118*4882a593Smuzhiyun * with the cluster number.
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun #define STACK_SIZE 512
121*4882a593Smuzhiyun extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
bL_switchpoint(unsigned long _arg)122*4882a593Smuzhiyun static int bL_switchpoint(unsigned long _arg)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun unsigned int mpidr = read_mpidr();
125*4882a593Smuzhiyun unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
126*4882a593Smuzhiyun void *stack = current_thread_info() + 1;
127*4882a593Smuzhiyun stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
128*4882a593Smuzhiyun stack += clusterid * STACK_SIZE + STACK_SIZE;
129*4882a593Smuzhiyun call_with_stack(bL_do_switch, (void *)_arg, stack);
130*4882a593Smuzhiyun BUG();
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun * Generic switcher interface
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
138*4882a593Smuzhiyun static int bL_switcher_cpu_pairing[NR_CPUS];
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * bL_switch_to - Switch to a specific cluster for the current CPU
142*4882a593Smuzhiyun * @new_cluster_id: the ID of the cluster to switch to.
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * This function must be called on the CPU to be switched.
145*4882a593Smuzhiyun * Returns 0 on success, else a negative status code.
146*4882a593Smuzhiyun */
bL_switch_to(unsigned int new_cluster_id)147*4882a593Smuzhiyun static int bL_switch_to(unsigned int new_cluster_id)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun unsigned int mpidr, this_cpu, that_cpu;
150*4882a593Smuzhiyun unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
151*4882a593Smuzhiyun struct completion inbound_alive;
152*4882a593Smuzhiyun long volatile *handshake_ptr;
153*4882a593Smuzhiyun int ipi_nr, ret;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun this_cpu = smp_processor_id();
156*4882a593Smuzhiyun ob_mpidr = read_mpidr();
157*4882a593Smuzhiyun ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
158*4882a593Smuzhiyun ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
159*4882a593Smuzhiyun BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (new_cluster_id == ob_cluster)
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun that_cpu = bL_switcher_cpu_pairing[this_cpu];
165*4882a593Smuzhiyun ib_mpidr = cpu_logical_map(that_cpu);
166*4882a593Smuzhiyun ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
167*4882a593Smuzhiyun ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
170*4882a593Smuzhiyun this_cpu, ob_mpidr, ib_mpidr);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun this_cpu = smp_processor_id();
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Close the gate for our entry vectors */
175*4882a593Smuzhiyun mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
176*4882a593Smuzhiyun mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* Install our "inbound alive" notifier. */
179*4882a593Smuzhiyun init_completion(&inbound_alive);
180*4882a593Smuzhiyun ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
181*4882a593Smuzhiyun ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
182*4882a593Smuzhiyun mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * Let's wake up the inbound CPU now in case it requires some delay
186*4882a593Smuzhiyun * to come online, but leave it gated in our entry vector code.
187*4882a593Smuzhiyun */
188*4882a593Smuzhiyun ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
189*4882a593Smuzhiyun if (ret) {
190*4882a593Smuzhiyun pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
191*4882a593Smuzhiyun return ret;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * Raise a SGI on the inbound CPU to make sure it doesn't stall
196*4882a593Smuzhiyun * in a possible WFI, such as in bL_power_down().
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * Wait for the inbound to come up. This allows for other
202*4882a593Smuzhiyun * tasks to be scheduled in the mean time.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun wait_for_completion(&inbound_alive);
205*4882a593Smuzhiyun mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * From this point we are entering the switch critical zone
209*4882a593Smuzhiyun * and can't take any interrupts anymore.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun local_irq_disable();
212*4882a593Smuzhiyun local_fiq_disable();
213*4882a593Smuzhiyun trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* redirect GIC's SGIs to our counterpart */
216*4882a593Smuzhiyun gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun tick_suspend_local();
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun ret = cpu_pm_enter();
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* we can not tolerate errors at this point */
223*4882a593Smuzhiyun if (ret)
224*4882a593Smuzhiyun panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* Swap the physical CPUs in the logical map for this logical CPU. */
227*4882a593Smuzhiyun cpu_logical_map(this_cpu) = ib_mpidr;
228*4882a593Smuzhiyun cpu_logical_map(that_cpu) = ob_mpidr;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* Let's do the actual CPU switch. */
231*4882a593Smuzhiyun ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
232*4882a593Smuzhiyun if (ret > 0)
233*4882a593Smuzhiyun panic("%s: cpu_suspend() returned %d\n", __func__, ret);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* We are executing on the inbound CPU at this point */
236*4882a593Smuzhiyun mpidr = read_mpidr();
237*4882a593Smuzhiyun pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
238*4882a593Smuzhiyun BUG_ON(mpidr != ib_mpidr);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun mcpm_cpu_powered_up();
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun ret = cpu_pm_exit();
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun tick_resume_local();
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
247*4882a593Smuzhiyun local_fiq_enable();
248*4882a593Smuzhiyun local_irq_enable();
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun *handshake_ptr = 1;
251*4882a593Smuzhiyun dsb_sev();
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if (ret)
254*4882a593Smuzhiyun pr_err("%s exiting with error %d\n", __func__, ret);
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun struct bL_thread {
259*4882a593Smuzhiyun spinlock_t lock;
260*4882a593Smuzhiyun struct task_struct *task;
261*4882a593Smuzhiyun wait_queue_head_t wq;
262*4882a593Smuzhiyun int wanted_cluster;
263*4882a593Smuzhiyun struct completion started;
264*4882a593Smuzhiyun bL_switch_completion_handler completer;
265*4882a593Smuzhiyun void *completer_cookie;
266*4882a593Smuzhiyun };
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun static struct bL_thread bL_threads[NR_CPUS];
269*4882a593Smuzhiyun
bL_switcher_thread(void * arg)270*4882a593Smuzhiyun static int bL_switcher_thread(void *arg)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun struct bL_thread *t = arg;
273*4882a593Smuzhiyun int cluster;
274*4882a593Smuzhiyun bL_switch_completion_handler completer;
275*4882a593Smuzhiyun void *completer_cookie;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun sched_set_fifo_low(current);
278*4882a593Smuzhiyun complete(&t->started);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun do {
281*4882a593Smuzhiyun if (signal_pending(current))
282*4882a593Smuzhiyun flush_signals(current);
283*4882a593Smuzhiyun wait_event_interruptible(t->wq,
284*4882a593Smuzhiyun t->wanted_cluster != -1 ||
285*4882a593Smuzhiyun kthread_should_stop());
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun spin_lock(&t->lock);
288*4882a593Smuzhiyun cluster = t->wanted_cluster;
289*4882a593Smuzhiyun completer = t->completer;
290*4882a593Smuzhiyun completer_cookie = t->completer_cookie;
291*4882a593Smuzhiyun t->wanted_cluster = -1;
292*4882a593Smuzhiyun t->completer = NULL;
293*4882a593Smuzhiyun spin_unlock(&t->lock);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun if (cluster != -1) {
296*4882a593Smuzhiyun bL_switch_to(cluster);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (completer)
299*4882a593Smuzhiyun completer(completer_cookie);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun } while (!kthread_should_stop());
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
bL_switcher_thread_create(int cpu,void * arg)306*4882a593Smuzhiyun static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct task_struct *task;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun task = kthread_create_on_node(bL_switcher_thread, arg,
311*4882a593Smuzhiyun cpu_to_node(cpu), "kswitcher_%d", cpu);
312*4882a593Smuzhiyun if (!IS_ERR(task)) {
313*4882a593Smuzhiyun kthread_bind(task, cpu);
314*4882a593Smuzhiyun wake_up_process(task);
315*4882a593Smuzhiyun } else
316*4882a593Smuzhiyun pr_err("%s failed for CPU %d\n", __func__, cpu);
317*4882a593Smuzhiyun return task;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
322*4882a593Smuzhiyun * with completion notification via a callback
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * @cpu: the CPU to switch
325*4882a593Smuzhiyun * @new_cluster_id: the ID of the cluster to switch to.
326*4882a593Smuzhiyun * @completer: switch completion callback. if non-NULL,
327*4882a593Smuzhiyun * @completer(@completer_cookie) will be called on completion of
328*4882a593Smuzhiyun * the switch, in non-atomic context.
329*4882a593Smuzhiyun * @completer_cookie: opaque context argument for @completer.
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * This function causes a cluster switch on the given CPU by waking up
332*4882a593Smuzhiyun * the appropriate switcher thread. This function may or may not return
333*4882a593Smuzhiyun * before the switch has occurred.
334*4882a593Smuzhiyun *
335*4882a593Smuzhiyun * If a @completer callback function is supplied, it will be called when
336*4882a593Smuzhiyun * the switch is complete. This can be used to determine asynchronously
337*4882a593Smuzhiyun * when the switch is complete, regardless of when bL_switch_request()
338*4882a593Smuzhiyun * returns. When @completer is supplied, no new switch request is permitted
339*4882a593Smuzhiyun * for the affected CPU until after the switch is complete, and @completer
340*4882a593Smuzhiyun * has returned.
341*4882a593Smuzhiyun */
bL_switch_request_cb(unsigned int cpu,unsigned int new_cluster_id,bL_switch_completion_handler completer,void * completer_cookie)342*4882a593Smuzhiyun int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
343*4882a593Smuzhiyun bL_switch_completion_handler completer,
344*4882a593Smuzhiyun void *completer_cookie)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun struct bL_thread *t;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (cpu >= ARRAY_SIZE(bL_threads)) {
349*4882a593Smuzhiyun pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
350*4882a593Smuzhiyun return -EINVAL;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun t = &bL_threads[cpu];
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun if (IS_ERR(t->task))
356*4882a593Smuzhiyun return PTR_ERR(t->task);
357*4882a593Smuzhiyun if (!t->task)
358*4882a593Smuzhiyun return -ESRCH;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun spin_lock(&t->lock);
361*4882a593Smuzhiyun if (t->completer) {
362*4882a593Smuzhiyun spin_unlock(&t->lock);
363*4882a593Smuzhiyun return -EBUSY;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun t->completer = completer;
366*4882a593Smuzhiyun t->completer_cookie = completer_cookie;
367*4882a593Smuzhiyun t->wanted_cluster = new_cluster_id;
368*4882a593Smuzhiyun spin_unlock(&t->lock);
369*4882a593Smuzhiyun wake_up(&t->wq);
370*4882a593Smuzhiyun return 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switch_request_cb);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /*
375*4882a593Smuzhiyun * Activation and configuration code.
376*4882a593Smuzhiyun */
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun static DEFINE_MUTEX(bL_switcher_activation_lock);
379*4882a593Smuzhiyun static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
380*4882a593Smuzhiyun static unsigned int bL_switcher_active;
381*4882a593Smuzhiyun static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
382*4882a593Smuzhiyun static cpumask_t bL_switcher_removed_logical_cpus;
383*4882a593Smuzhiyun
bL_switcher_register_notifier(struct notifier_block * nb)384*4882a593Smuzhiyun int bL_switcher_register_notifier(struct notifier_block *nb)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun return blocking_notifier_chain_register(&bL_activation_notifier, nb);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
389*4882a593Smuzhiyun
bL_switcher_unregister_notifier(struct notifier_block * nb)390*4882a593Smuzhiyun int bL_switcher_unregister_notifier(struct notifier_block *nb)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
395*4882a593Smuzhiyun
bL_activation_notify(unsigned long val)396*4882a593Smuzhiyun static int bL_activation_notify(unsigned long val)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun int ret;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
401*4882a593Smuzhiyun if (ret & NOTIFY_STOP_MASK)
402*4882a593Smuzhiyun pr_err("%s: notifier chain failed with status 0x%x\n",
403*4882a593Smuzhiyun __func__, ret);
404*4882a593Smuzhiyun return notifier_to_errno(ret);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
bL_switcher_restore_cpus(void)407*4882a593Smuzhiyun static void bL_switcher_restore_cpus(void)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun int i;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
412*4882a593Smuzhiyun struct device *cpu_dev = get_cpu_device(i);
413*4882a593Smuzhiyun int ret = device_online(cpu_dev);
414*4882a593Smuzhiyun if (ret)
415*4882a593Smuzhiyun dev_err(cpu_dev, "switcher: unable to restore CPU\n");
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
bL_switcher_halve_cpus(void)419*4882a593Smuzhiyun static int bL_switcher_halve_cpus(void)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun int i, j, cluster_0, gic_id, ret;
422*4882a593Smuzhiyun unsigned int cpu, cluster, mask;
423*4882a593Smuzhiyun cpumask_t available_cpus;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun /* First pass to validate what we have */
426*4882a593Smuzhiyun mask = 0;
427*4882a593Smuzhiyun for_each_online_cpu(i) {
428*4882a593Smuzhiyun cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
429*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
430*4882a593Smuzhiyun if (cluster >= 2) {
431*4882a593Smuzhiyun pr_err("%s: only dual cluster systems are supported\n", __func__);
432*4882a593Smuzhiyun return -EINVAL;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
435*4882a593Smuzhiyun return -EINVAL;
436*4882a593Smuzhiyun mask |= (1 << cluster);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun if (mask != 3) {
439*4882a593Smuzhiyun pr_err("%s: no CPU pairing possible\n", __func__);
440*4882a593Smuzhiyun return -EINVAL;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /*
444*4882a593Smuzhiyun * Now let's do the pairing. We match each CPU with another CPU
445*4882a593Smuzhiyun * from a different cluster. To get a uniform scheduling behavior
446*4882a593Smuzhiyun * without fiddling with CPU topology and compute capacity data,
447*4882a593Smuzhiyun * we'll use logical CPUs initially belonging to the same cluster.
448*4882a593Smuzhiyun */
449*4882a593Smuzhiyun memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
450*4882a593Smuzhiyun cpumask_copy(&available_cpus, cpu_online_mask);
451*4882a593Smuzhiyun cluster_0 = -1;
452*4882a593Smuzhiyun for_each_cpu(i, &available_cpus) {
453*4882a593Smuzhiyun int match = -1;
454*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
455*4882a593Smuzhiyun if (cluster_0 == -1)
456*4882a593Smuzhiyun cluster_0 = cluster;
457*4882a593Smuzhiyun if (cluster != cluster_0)
458*4882a593Smuzhiyun continue;
459*4882a593Smuzhiyun cpumask_clear_cpu(i, &available_cpus);
460*4882a593Smuzhiyun for_each_cpu(j, &available_cpus) {
461*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
462*4882a593Smuzhiyun /*
463*4882a593Smuzhiyun * Let's remember the last match to create "odd"
464*4882a593Smuzhiyun * pairings on purpose in order for other code not
465*4882a593Smuzhiyun * to assume any relation between physical and
466*4882a593Smuzhiyun * logical CPU numbers.
467*4882a593Smuzhiyun */
468*4882a593Smuzhiyun if (cluster != cluster_0)
469*4882a593Smuzhiyun match = j;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun if (match != -1) {
472*4882a593Smuzhiyun bL_switcher_cpu_pairing[i] = match;
473*4882a593Smuzhiyun cpumask_clear_cpu(match, &available_cpus);
474*4882a593Smuzhiyun pr_info("CPU%d paired with CPU%d\n", i, match);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /*
479*4882a593Smuzhiyun * Now we disable the unwanted CPUs i.e. everything that has no
480*4882a593Smuzhiyun * pairing information (that includes the pairing counterparts).
481*4882a593Smuzhiyun */
482*4882a593Smuzhiyun cpumask_clear(&bL_switcher_removed_logical_cpus);
483*4882a593Smuzhiyun for_each_online_cpu(i) {
484*4882a593Smuzhiyun cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
485*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* Let's take note of the GIC ID for this CPU */
488*4882a593Smuzhiyun gic_id = gic_get_cpu_id(i);
489*4882a593Smuzhiyun if (gic_id < 0) {
490*4882a593Smuzhiyun pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
491*4882a593Smuzhiyun bL_switcher_restore_cpus();
492*4882a593Smuzhiyun return -EINVAL;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun bL_gic_id[cpu][cluster] = gic_id;
495*4882a593Smuzhiyun pr_info("GIC ID for CPU %u cluster %u is %u\n",
496*4882a593Smuzhiyun cpu, cluster, gic_id);
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun if (bL_switcher_cpu_pairing[i] != -1) {
499*4882a593Smuzhiyun bL_switcher_cpu_original_cluster[i] = cluster;
500*4882a593Smuzhiyun continue;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun ret = device_offline(get_cpu_device(i));
504*4882a593Smuzhiyun if (ret) {
505*4882a593Smuzhiyun bL_switcher_restore_cpus();
506*4882a593Smuzhiyun return ret;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return 0;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /* Determine the logical CPU a given physical CPU is grouped on. */
bL_switcher_get_logical_index(u32 mpidr)515*4882a593Smuzhiyun int bL_switcher_get_logical_index(u32 mpidr)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun int cpu;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (!bL_switcher_active)
520*4882a593Smuzhiyun return -EUNATCH;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun mpidr &= MPIDR_HWID_BITMASK;
523*4882a593Smuzhiyun for_each_online_cpu(cpu) {
524*4882a593Smuzhiyun int pairing = bL_switcher_cpu_pairing[cpu];
525*4882a593Smuzhiyun if (pairing == -1)
526*4882a593Smuzhiyun continue;
527*4882a593Smuzhiyun if ((mpidr == cpu_logical_map(cpu)) ||
528*4882a593Smuzhiyun (mpidr == cpu_logical_map(pairing)))
529*4882a593Smuzhiyun return cpu;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun return -EINVAL;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
bL_switcher_trace_trigger_cpu(void * __always_unused info)534*4882a593Smuzhiyun static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun
bL_switcher_trace_trigger(void)539*4882a593Smuzhiyun int bL_switcher_trace_trigger(void)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun preempt_disable();
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun bL_switcher_trace_trigger_cpu(NULL);
544*4882a593Smuzhiyun smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun preempt_enable();
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun return 0;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
551*4882a593Smuzhiyun
bL_switcher_enable(void)552*4882a593Smuzhiyun static int bL_switcher_enable(void)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun int cpu, ret;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun mutex_lock(&bL_switcher_activation_lock);
557*4882a593Smuzhiyun lock_device_hotplug();
558*4882a593Smuzhiyun if (bL_switcher_active) {
559*4882a593Smuzhiyun unlock_device_hotplug();
560*4882a593Smuzhiyun mutex_unlock(&bL_switcher_activation_lock);
561*4882a593Smuzhiyun return 0;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun pr_info("big.LITTLE switcher initializing\n");
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
567*4882a593Smuzhiyun if (ret)
568*4882a593Smuzhiyun goto error;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun ret = bL_switcher_halve_cpus();
571*4882a593Smuzhiyun if (ret)
572*4882a593Smuzhiyun goto error;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun bL_switcher_trace_trigger();
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun for_each_online_cpu(cpu) {
577*4882a593Smuzhiyun struct bL_thread *t = &bL_threads[cpu];
578*4882a593Smuzhiyun spin_lock_init(&t->lock);
579*4882a593Smuzhiyun init_waitqueue_head(&t->wq);
580*4882a593Smuzhiyun init_completion(&t->started);
581*4882a593Smuzhiyun t->wanted_cluster = -1;
582*4882a593Smuzhiyun t->task = bL_switcher_thread_create(cpu, t);
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun bL_switcher_active = 1;
586*4882a593Smuzhiyun bL_activation_notify(BL_NOTIFY_POST_ENABLE);
587*4882a593Smuzhiyun pr_info("big.LITTLE switcher initialized\n");
588*4882a593Smuzhiyun goto out;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun error:
591*4882a593Smuzhiyun pr_warn("big.LITTLE switcher initialization failed\n");
592*4882a593Smuzhiyun bL_activation_notify(BL_NOTIFY_POST_DISABLE);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun out:
595*4882a593Smuzhiyun unlock_device_hotplug();
596*4882a593Smuzhiyun mutex_unlock(&bL_switcher_activation_lock);
597*4882a593Smuzhiyun return ret;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
601*4882a593Smuzhiyun
bL_switcher_disable(void)602*4882a593Smuzhiyun static void bL_switcher_disable(void)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun unsigned int cpu, cluster;
605*4882a593Smuzhiyun struct bL_thread *t;
606*4882a593Smuzhiyun struct task_struct *task;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun mutex_lock(&bL_switcher_activation_lock);
609*4882a593Smuzhiyun lock_device_hotplug();
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun if (!bL_switcher_active)
612*4882a593Smuzhiyun goto out;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
615*4882a593Smuzhiyun bL_activation_notify(BL_NOTIFY_POST_ENABLE);
616*4882a593Smuzhiyun goto out;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun bL_switcher_active = 0;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * To deactivate the switcher, we must shut down the switcher
623*4882a593Smuzhiyun * threads to prevent any other requests from being accepted.
624*4882a593Smuzhiyun * Then, if the final cluster for given logical CPU is not the
625*4882a593Smuzhiyun * same as the original one, we'll recreate a switcher thread
626*4882a593Smuzhiyun * just for the purpose of switching the CPU back without any
627*4882a593Smuzhiyun * possibility for interference from external requests.
628*4882a593Smuzhiyun */
629*4882a593Smuzhiyun for_each_online_cpu(cpu) {
630*4882a593Smuzhiyun t = &bL_threads[cpu];
631*4882a593Smuzhiyun task = t->task;
632*4882a593Smuzhiyun t->task = NULL;
633*4882a593Smuzhiyun if (!task || IS_ERR(task))
634*4882a593Smuzhiyun continue;
635*4882a593Smuzhiyun kthread_stop(task);
636*4882a593Smuzhiyun /* no more switch may happen on this CPU at this point */
637*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
638*4882a593Smuzhiyun if (cluster == bL_switcher_cpu_original_cluster[cpu])
639*4882a593Smuzhiyun continue;
640*4882a593Smuzhiyun init_completion(&t->started);
641*4882a593Smuzhiyun t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
642*4882a593Smuzhiyun task = bL_switcher_thread_create(cpu, t);
643*4882a593Smuzhiyun if (!IS_ERR(task)) {
644*4882a593Smuzhiyun wait_for_completion(&t->started);
645*4882a593Smuzhiyun kthread_stop(task);
646*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
647*4882a593Smuzhiyun if (cluster == bL_switcher_cpu_original_cluster[cpu])
648*4882a593Smuzhiyun continue;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun /* If execution gets here, we're in trouble. */
651*4882a593Smuzhiyun pr_crit("%s: unable to restore original cluster for CPU %d\n",
652*4882a593Smuzhiyun __func__, cpu);
653*4882a593Smuzhiyun pr_crit("%s: CPU %d can't be restored\n",
654*4882a593Smuzhiyun __func__, bL_switcher_cpu_pairing[cpu]);
655*4882a593Smuzhiyun cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
656*4882a593Smuzhiyun &bL_switcher_removed_logical_cpus);
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun bL_switcher_restore_cpus();
660*4882a593Smuzhiyun bL_switcher_trace_trigger();
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun bL_activation_notify(BL_NOTIFY_POST_DISABLE);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun out:
665*4882a593Smuzhiyun unlock_device_hotplug();
666*4882a593Smuzhiyun mutex_unlock(&bL_switcher_activation_lock);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
bL_switcher_active_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)669*4882a593Smuzhiyun static ssize_t bL_switcher_active_show(struct kobject *kobj,
670*4882a593Smuzhiyun struct kobj_attribute *attr, char *buf)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun return sprintf(buf, "%u\n", bL_switcher_active);
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun
bL_switcher_active_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)675*4882a593Smuzhiyun static ssize_t bL_switcher_active_store(struct kobject *kobj,
676*4882a593Smuzhiyun struct kobj_attribute *attr, const char *buf, size_t count)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun int ret;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun switch (buf[0]) {
681*4882a593Smuzhiyun case '0':
682*4882a593Smuzhiyun bL_switcher_disable();
683*4882a593Smuzhiyun ret = 0;
684*4882a593Smuzhiyun break;
685*4882a593Smuzhiyun case '1':
686*4882a593Smuzhiyun ret = bL_switcher_enable();
687*4882a593Smuzhiyun break;
688*4882a593Smuzhiyun default:
689*4882a593Smuzhiyun ret = -EINVAL;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun return (ret >= 0) ? count : ret;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun
bL_switcher_trace_trigger_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)695*4882a593Smuzhiyun static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
696*4882a593Smuzhiyun struct kobj_attribute *attr, const char *buf, size_t count)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun int ret = bL_switcher_trace_trigger();
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun return ret ? ret : count;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun static struct kobj_attribute bL_switcher_active_attr =
704*4882a593Smuzhiyun __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun static struct kobj_attribute bL_switcher_trace_trigger_attr =
707*4882a593Smuzhiyun __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun static struct attribute *bL_switcher_attrs[] = {
710*4882a593Smuzhiyun &bL_switcher_active_attr.attr,
711*4882a593Smuzhiyun &bL_switcher_trace_trigger_attr.attr,
712*4882a593Smuzhiyun NULL,
713*4882a593Smuzhiyun };
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun static struct attribute_group bL_switcher_attr_group = {
716*4882a593Smuzhiyun .attrs = bL_switcher_attrs,
717*4882a593Smuzhiyun };
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun static struct kobject *bL_switcher_kobj;
720*4882a593Smuzhiyun
bL_switcher_sysfs_init(void)721*4882a593Smuzhiyun static int __init bL_switcher_sysfs_init(void)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun int ret;
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
726*4882a593Smuzhiyun if (!bL_switcher_kobj)
727*4882a593Smuzhiyun return -ENOMEM;
728*4882a593Smuzhiyun ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
729*4882a593Smuzhiyun if (ret)
730*4882a593Smuzhiyun kobject_put(bL_switcher_kobj);
731*4882a593Smuzhiyun return ret;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun #endif /* CONFIG_SYSFS */
735*4882a593Smuzhiyun
bL_switcher_get_enabled(void)736*4882a593Smuzhiyun bool bL_switcher_get_enabled(void)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun mutex_lock(&bL_switcher_activation_lock);
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun return bL_switcher_active;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
743*4882a593Smuzhiyun
bL_switcher_put_enabled(void)744*4882a593Smuzhiyun void bL_switcher_put_enabled(void)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun mutex_unlock(&bL_switcher_activation_lock);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun /*
751*4882a593Smuzhiyun * Veto any CPU hotplug operation on those CPUs we've removed
752*4882a593Smuzhiyun * while the switcher is active.
753*4882a593Smuzhiyun * We're just not ready to deal with that given the trickery involved.
754*4882a593Smuzhiyun */
bL_switcher_cpu_pre(unsigned int cpu)755*4882a593Smuzhiyun static int bL_switcher_cpu_pre(unsigned int cpu)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun int pairing;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun if (!bL_switcher_active)
760*4882a593Smuzhiyun return 0;
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun pairing = bL_switcher_cpu_pairing[cpu];
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun if (pairing == -1)
765*4882a593Smuzhiyun return -EINVAL;
766*4882a593Smuzhiyun return 0;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun static bool no_bL_switcher;
770*4882a593Smuzhiyun core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
771*4882a593Smuzhiyun
bL_switcher_init(void)772*4882a593Smuzhiyun static int __init bL_switcher_init(void)
773*4882a593Smuzhiyun {
774*4882a593Smuzhiyun int ret;
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun if (!mcpm_is_available())
777*4882a593Smuzhiyun return -ENODEV;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun cpuhp_setup_state_nocalls(CPUHP_ARM_BL_PREPARE, "arm/bl:prepare",
780*4882a593Smuzhiyun bL_switcher_cpu_pre, NULL);
781*4882a593Smuzhiyun ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/bl:predown",
782*4882a593Smuzhiyun NULL, bL_switcher_cpu_pre);
783*4882a593Smuzhiyun if (ret < 0) {
784*4882a593Smuzhiyun cpuhp_remove_state_nocalls(CPUHP_ARM_BL_PREPARE);
785*4882a593Smuzhiyun pr_err("bL_switcher: Failed to allocate a hotplug state\n");
786*4882a593Smuzhiyun return ret;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun if (!no_bL_switcher) {
789*4882a593Smuzhiyun ret = bL_switcher_enable();
790*4882a593Smuzhiyun if (ret)
791*4882a593Smuzhiyun return ret;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
795*4882a593Smuzhiyun ret = bL_switcher_sysfs_init();
796*4882a593Smuzhiyun if (ret)
797*4882a593Smuzhiyun pr_err("%s: unable to create sysfs entry\n", __func__);
798*4882a593Smuzhiyun #endif
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun return 0;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun late_initcall(bL_switcher_init);
804