1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21c33be57SNicolas Pitre /*
31c33be57SNicolas Pitre * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
41c33be57SNicolas Pitre *
51c33be57SNicolas Pitre * Created by: Nicolas Pitre, March 2012
61c33be57SNicolas Pitre * Copyright: (C) 2012-2013 Linaro Limited
71c33be57SNicolas Pitre */
81c33be57SNicolas Pitre
90577fee2SDave Martin #include <linux/atomic.h>
101c33be57SNicolas Pitre #include <linux/init.h>
111c33be57SNicolas Pitre #include <linux/kernel.h>
121c33be57SNicolas Pitre #include <linux/module.h>
13174cd4b1SIngo Molnar #include <linux/sched/signal.h>
14ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
151c33be57SNicolas Pitre #include <linux/interrupt.h>
161c33be57SNicolas Pitre #include <linux/cpu_pm.h>
1771ce1deeSNicolas Pitre #include <linux/cpu.h>
183f09d479SLorenzo Pieralisi #include <linux/cpumask.h>
1971ce1deeSNicolas Pitre #include <linux/kthread.h>
2071ce1deeSNicolas Pitre #include <linux/wait.h>
211bfbddb6SDave Martin #include <linux/time.h>
223f09d479SLorenzo Pieralisi #include <linux/clockchips.h>
233f09d479SLorenzo Pieralisi #include <linux/hrtimer.h>
243f09d479SLorenzo Pieralisi #include <linux/tick.h>
25491990e2SDave Martin #include <linux/notifier.h>
261c33be57SNicolas Pitre #include <linux/mm.h>
27c0f43751SDave Martin #include <linux/mutex.h>
28b09bbe5bSDave Martin #include <linux/smp.h>
290577fee2SDave Martin #include <linux/spinlock.h>
301c33be57SNicolas Pitre #include <linux/string.h>
316b7437aeSNicolas Pitre #include <linux/sysfs.h>
321c33be57SNicolas Pitre #include <linux/irqchip/arm-gic.h>
33c4821c05SNicolas Pitre #include <linux/moduleparam.h>
341c33be57SNicolas Pitre
351c33be57SNicolas Pitre #include <asm/smp_plat.h>
361bfbddb6SDave Martin #include <asm/cputype.h>
371c33be57SNicolas Pitre #include <asm/suspend.h>
381c33be57SNicolas Pitre #include <asm/mcpm.h>
391c33be57SNicolas Pitre #include <asm/bL_switcher.h>
401c33be57SNicolas Pitre
411bfbddb6SDave Martin #define CREATE_TRACE_POINTS
421bfbddb6SDave Martin #include <trace/events/power_cpu_migrate.h>
431bfbddb6SDave Martin
441c33be57SNicolas Pitre
451c33be57SNicolas Pitre /*
461c33be57SNicolas Pitre * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
471c33be57SNicolas Pitre * __attribute_const__ and we don't want the compiler to assume any
481c33be57SNicolas Pitre * constness here as the value _does_ change along some code paths.
491c33be57SNicolas Pitre */
501c33be57SNicolas Pitre
read_mpidr(void)511c33be57SNicolas Pitre static int read_mpidr(void)
521c33be57SNicolas Pitre {
531c33be57SNicolas Pitre unsigned int id;
541c33be57SNicolas Pitre asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
551c33be57SNicolas Pitre return id & MPIDR_HWID_BITMASK;
561c33be57SNicolas Pitre }
571c33be57SNicolas Pitre
581c33be57SNicolas Pitre /*
591c33be57SNicolas Pitre * bL switcher core code.
601c33be57SNicolas Pitre */
611c33be57SNicolas Pitre
bL_do_switch(void * _arg)62108a9640SNicolas Pitre static void bL_do_switch(void *_arg)
631c33be57SNicolas Pitre {
6438c35d4fSNicolas Pitre unsigned ib_mpidr, ib_cpu, ib_cluster;
65108a9640SNicolas Pitre long volatile handshake, **handshake_ptr = _arg;
661c33be57SNicolas Pitre
671c33be57SNicolas Pitre pr_debug("%s\n", __func__);
681c33be57SNicolas Pitre
6938c35d4fSNicolas Pitre ib_mpidr = cpu_logical_map(smp_processor_id());
7038c35d4fSNicolas Pitre ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
7138c35d4fSNicolas Pitre ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
721c33be57SNicolas Pitre
73108a9640SNicolas Pitre /* Advertise our handshake location */
74108a9640SNicolas Pitre if (handshake_ptr) {
75108a9640SNicolas Pitre handshake = 0;
76108a9640SNicolas Pitre *handshake_ptr = &handshake;
77108a9640SNicolas Pitre } else
78108a9640SNicolas Pitre handshake = -1;
79108a9640SNicolas Pitre
801c33be57SNicolas Pitre /*
811c33be57SNicolas Pitre * Our state has been saved at this point. Let's release our
821c33be57SNicolas Pitre * inbound CPU.
831c33be57SNicolas Pitre */
8438c35d4fSNicolas Pitre mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
851c33be57SNicolas Pitre sev();
861c33be57SNicolas Pitre
871c33be57SNicolas Pitre /*
881c33be57SNicolas Pitre * From this point, we must assume that our counterpart CPU might
891c33be57SNicolas Pitre * have taken over in its parallel world already, as if execution
901c33be57SNicolas Pitre * just returned from cpu_suspend(). It is therefore important to
911c33be57SNicolas Pitre * be very careful not to make any change the other guy is not
921c33be57SNicolas Pitre * expecting. This is why we need stack isolation.
931c33be57SNicolas Pitre *
941c33be57SNicolas Pitre * Fancy under cover tasks could be performed here. For now
951c33be57SNicolas Pitre * we have none.
961c33be57SNicolas Pitre */
971c33be57SNicolas Pitre
98108a9640SNicolas Pitre /*
99108a9640SNicolas Pitre * Let's wait until our inbound is alive.
100108a9640SNicolas Pitre */
101108a9640SNicolas Pitre while (!handshake) {
102108a9640SNicolas Pitre wfe();
103108a9640SNicolas Pitre smp_mb();
104108a9640SNicolas Pitre }
105108a9640SNicolas Pitre
1061c33be57SNicolas Pitre /* Let's put ourself down. */
1071c33be57SNicolas Pitre mcpm_cpu_power_down();
1081c33be57SNicolas Pitre
1091c33be57SNicolas Pitre /* should never get here */
1101c33be57SNicolas Pitre BUG();
1111c33be57SNicolas Pitre }
1121c33be57SNicolas Pitre
1131c33be57SNicolas Pitre /*
114c052de26SNicolas Pitre * Stack isolation. To ensure 'current' remains valid, we just use another
115c052de26SNicolas Pitre * piece of our thread's stack space which should be fairly lightly used.
116c052de26SNicolas Pitre * The selected area starts just above the thread_info structure located
117c052de26SNicolas Pitre * at the very bottom of the stack, aligned to a cache line, and indexed
118c052de26SNicolas Pitre * with the cluster number.
1191c33be57SNicolas Pitre */
120c052de26SNicolas Pitre #define STACK_SIZE 512
1211c33be57SNicolas Pitre extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
bL_switchpoint(unsigned long _arg)1221c33be57SNicolas Pitre static int bL_switchpoint(unsigned long _arg)
1231c33be57SNicolas Pitre {
1241c33be57SNicolas Pitre unsigned int mpidr = read_mpidr();
1251c33be57SNicolas Pitre unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
126c052de26SNicolas Pitre void *stack = current_thread_info() + 1;
1271c33be57SNicolas Pitre stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
128c052de26SNicolas Pitre stack += clusterid * STACK_SIZE + STACK_SIZE;
1291c33be57SNicolas Pitre call_with_stack(bL_do_switch, (void *)_arg, stack);
1301c33be57SNicolas Pitre BUG();
1311c33be57SNicolas Pitre }
1321c33be57SNicolas Pitre
1331c33be57SNicolas Pitre /*
1341c33be57SNicolas Pitre * Generic switcher interface
1351c33be57SNicolas Pitre */
1361c33be57SNicolas Pitre
137ed96762eSNicolas Pitre static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
13838c35d4fSNicolas Pitre static int bL_switcher_cpu_pairing[NR_CPUS];
139ed96762eSNicolas Pitre
1401c33be57SNicolas Pitre /*
1411c33be57SNicolas Pitre * bL_switch_to - Switch to a specific cluster for the current CPU
1421c33be57SNicolas Pitre * @new_cluster_id: the ID of the cluster to switch to.
1431c33be57SNicolas Pitre *
1441c33be57SNicolas Pitre * This function must be called on the CPU to be switched.
1451c33be57SNicolas Pitre * Returns 0 on success, else a negative status code.
1461c33be57SNicolas Pitre */
bL_switch_to(unsigned int new_cluster_id)1471c33be57SNicolas Pitre static int bL_switch_to(unsigned int new_cluster_id)
1481c33be57SNicolas Pitre {
14938c35d4fSNicolas Pitre unsigned int mpidr, this_cpu, that_cpu;
15038c35d4fSNicolas Pitre unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
1516137eba6SNicolas Pitre struct completion inbound_alive;
152108a9640SNicolas Pitre long volatile *handshake_ptr;
1536137eba6SNicolas Pitre int ipi_nr, ret;
1541c33be57SNicolas Pitre
15538c35d4fSNicolas Pitre this_cpu = smp_processor_id();
15638c35d4fSNicolas Pitre ob_mpidr = read_mpidr();
15738c35d4fSNicolas Pitre ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
15838c35d4fSNicolas Pitre ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
15938c35d4fSNicolas Pitre BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
1601c33be57SNicolas Pitre
16138c35d4fSNicolas Pitre if (new_cluster_id == ob_cluster)
1621c33be57SNicolas Pitre return 0;
1631c33be57SNicolas Pitre
16438c35d4fSNicolas Pitre that_cpu = bL_switcher_cpu_pairing[this_cpu];
16538c35d4fSNicolas Pitre ib_mpidr = cpu_logical_map(that_cpu);
16638c35d4fSNicolas Pitre ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
16738c35d4fSNicolas Pitre ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
16838c35d4fSNicolas Pitre
16938c35d4fSNicolas Pitre pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
17038c35d4fSNicolas Pitre this_cpu, ob_mpidr, ib_mpidr);
1711c33be57SNicolas Pitre
1726137eba6SNicolas Pitre this_cpu = smp_processor_id();
1736137eba6SNicolas Pitre
1741c33be57SNicolas Pitre /* Close the gate for our entry vectors */
17538c35d4fSNicolas Pitre mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
17638c35d4fSNicolas Pitre mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
1771c33be57SNicolas Pitre
1786137eba6SNicolas Pitre /* Install our "inbound alive" notifier. */
1796137eba6SNicolas Pitre init_completion(&inbound_alive);
1806137eba6SNicolas Pitre ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
1816137eba6SNicolas Pitre ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
1826137eba6SNicolas Pitre mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
1836137eba6SNicolas Pitre
1841c33be57SNicolas Pitre /*
1851c33be57SNicolas Pitre * Let's wake up the inbound CPU now in case it requires some delay
1861c33be57SNicolas Pitre * to come online, but leave it gated in our entry vector code.
1871c33be57SNicolas Pitre */
18838c35d4fSNicolas Pitre ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
1891c33be57SNicolas Pitre if (ret) {
1901c33be57SNicolas Pitre pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
1911c33be57SNicolas Pitre return ret;
1921c33be57SNicolas Pitre }
1931c33be57SNicolas Pitre
1941c33be57SNicolas Pitre /*
1956137eba6SNicolas Pitre * Raise a SGI on the inbound CPU to make sure it doesn't stall
1966137eba6SNicolas Pitre * in a possible WFI, such as in bL_power_down().
1976137eba6SNicolas Pitre */
1986137eba6SNicolas Pitre gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
1996137eba6SNicolas Pitre
2006137eba6SNicolas Pitre /*
2016137eba6SNicolas Pitre * Wait for the inbound to come up. This allows for other
2026137eba6SNicolas Pitre * tasks to be scheduled in the mean time.
2036137eba6SNicolas Pitre */
2046137eba6SNicolas Pitre wait_for_completion(&inbound_alive);
2056137eba6SNicolas Pitre mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
2066137eba6SNicolas Pitre
2076137eba6SNicolas Pitre /*
2081c33be57SNicolas Pitre * From this point we are entering the switch critical zone
2091c33be57SNicolas Pitre * and can't take any interrupts anymore.
2101c33be57SNicolas Pitre */
2111c33be57SNicolas Pitre local_irq_disable();
2121c33be57SNicolas Pitre local_fiq_disable();
21341fa4215SThomas Gleixner trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
2141c33be57SNicolas Pitre
2151c33be57SNicolas Pitre /* redirect GIC's SGIs to our counterpart */
21638c35d4fSNicolas Pitre gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
2171c33be57SNicolas Pitre
2187270d11cSThomas Gleixner tick_suspend_local();
2193f09d479SLorenzo Pieralisi
2201c33be57SNicolas Pitre ret = cpu_pm_enter();
2211c33be57SNicolas Pitre
2221c33be57SNicolas Pitre /* we can not tolerate errors at this point */
2231c33be57SNicolas Pitre if (ret)
2241c33be57SNicolas Pitre panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
2251c33be57SNicolas Pitre
22638c35d4fSNicolas Pitre /* Swap the physical CPUs in the logical map for this logical CPU. */
22738c35d4fSNicolas Pitre cpu_logical_map(this_cpu) = ib_mpidr;
22838c35d4fSNicolas Pitre cpu_logical_map(that_cpu) = ob_mpidr;
2291c33be57SNicolas Pitre
2301c33be57SNicolas Pitre /* Let's do the actual CPU switch. */
231108a9640SNicolas Pitre ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
2321c33be57SNicolas Pitre if (ret > 0)
2331c33be57SNicolas Pitre panic("%s: cpu_suspend() returned %d\n", __func__, ret);
2341c33be57SNicolas Pitre
2351c33be57SNicolas Pitre /* We are executing on the inbound CPU at this point */
2361c33be57SNicolas Pitre mpidr = read_mpidr();
23738c35d4fSNicolas Pitre pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
23838c35d4fSNicolas Pitre BUG_ON(mpidr != ib_mpidr);
2391c33be57SNicolas Pitre
2401c33be57SNicolas Pitre mcpm_cpu_powered_up();
2411c33be57SNicolas Pitre
2421c33be57SNicolas Pitre ret = cpu_pm_exit();
2431c33be57SNicolas Pitre
2447270d11cSThomas Gleixner tick_resume_local();
2453f09d479SLorenzo Pieralisi
24641fa4215SThomas Gleixner trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
2471c33be57SNicolas Pitre local_fiq_enable();
2481c33be57SNicolas Pitre local_irq_enable();
2491c33be57SNicolas Pitre
250108a9640SNicolas Pitre *handshake_ptr = 1;
251108a9640SNicolas Pitre dsb_sev();
252108a9640SNicolas Pitre
2531c33be57SNicolas Pitre if (ret)
2541c33be57SNicolas Pitre pr_err("%s exiting with error %d\n", __func__, ret);
2551c33be57SNicolas Pitre return ret;
2561c33be57SNicolas Pitre }
2571c33be57SNicolas Pitre
25871ce1deeSNicolas Pitre struct bL_thread {
2590577fee2SDave Martin spinlock_t lock;
26071ce1deeSNicolas Pitre struct task_struct *task;
26171ce1deeSNicolas Pitre wait_queue_head_t wq;
26271ce1deeSNicolas Pitre int wanted_cluster;
2636b7437aeSNicolas Pitre struct completion started;
2640577fee2SDave Martin bL_switch_completion_handler completer;
2650577fee2SDave Martin void *completer_cookie;
2661c33be57SNicolas Pitre };
2671c33be57SNicolas Pitre
26871ce1deeSNicolas Pitre static struct bL_thread bL_threads[NR_CPUS];
26971ce1deeSNicolas Pitre
bL_switcher_thread(void * arg)27071ce1deeSNicolas Pitre static int bL_switcher_thread(void *arg)
2711c33be57SNicolas Pitre {
27271ce1deeSNicolas Pitre struct bL_thread *t = arg;
27371ce1deeSNicolas Pitre int cluster;
2740577fee2SDave Martin bL_switch_completion_handler completer;
2750577fee2SDave Martin void *completer_cookie;
27671ce1deeSNicolas Pitre
277*0030c1d4SPeter Zijlstra sched_set_fifo_low(current);
2786b7437aeSNicolas Pitre complete(&t->started);
27971ce1deeSNicolas Pitre
28071ce1deeSNicolas Pitre do {
28171ce1deeSNicolas Pitre if (signal_pending(current))
28271ce1deeSNicolas Pitre flush_signals(current);
28371ce1deeSNicolas Pitre wait_event_interruptible(t->wq,
28471ce1deeSNicolas Pitre t->wanted_cluster != -1 ||
28571ce1deeSNicolas Pitre kthread_should_stop());
2860577fee2SDave Martin
2870577fee2SDave Martin spin_lock(&t->lock);
2880577fee2SDave Martin cluster = t->wanted_cluster;
2890577fee2SDave Martin completer = t->completer;
2900577fee2SDave Martin completer_cookie = t->completer_cookie;
2910577fee2SDave Martin t->wanted_cluster = -1;
2920577fee2SDave Martin t->completer = NULL;
2930577fee2SDave Martin spin_unlock(&t->lock);
2940577fee2SDave Martin
2950577fee2SDave Martin if (cluster != -1) {
29671ce1deeSNicolas Pitre bL_switch_to(cluster);
2970577fee2SDave Martin
2980577fee2SDave Martin if (completer)
2990577fee2SDave Martin completer(completer_cookie);
3000577fee2SDave Martin }
30171ce1deeSNicolas Pitre } while (!kthread_should_stop());
30271ce1deeSNicolas Pitre
30371ce1deeSNicolas Pitre return 0;
30471ce1deeSNicolas Pitre }
30571ce1deeSNicolas Pitre
bL_switcher_thread_create(int cpu,void * arg)3066b7437aeSNicolas Pitre static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
30771ce1deeSNicolas Pitre {
30871ce1deeSNicolas Pitre struct task_struct *task;
30971ce1deeSNicolas Pitre
31071ce1deeSNicolas Pitre task = kthread_create_on_node(bL_switcher_thread, arg,
31171ce1deeSNicolas Pitre cpu_to_node(cpu), "kswitcher_%d", cpu);
31271ce1deeSNicolas Pitre if (!IS_ERR(task)) {
31371ce1deeSNicolas Pitre kthread_bind(task, cpu);
31471ce1deeSNicolas Pitre wake_up_process(task);
31571ce1deeSNicolas Pitre } else
31671ce1deeSNicolas Pitre pr_err("%s failed for CPU %d\n", __func__, cpu);
31771ce1deeSNicolas Pitre return task;
3181c33be57SNicolas Pitre }
3191c33be57SNicolas Pitre
3201c33be57SNicolas Pitre /*
3210577fee2SDave Martin * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
3220577fee2SDave Martin * with completion notification via a callback
3231c33be57SNicolas Pitre *
3241c33be57SNicolas Pitre * @cpu: the CPU to switch
3251c33be57SNicolas Pitre * @new_cluster_id: the ID of the cluster to switch to.
3260577fee2SDave Martin * @completer: switch completion callback. if non-NULL,
3270577fee2SDave Martin * @completer(@completer_cookie) will be called on completion of
3280577fee2SDave Martin * the switch, in non-atomic context.
3290577fee2SDave Martin * @completer_cookie: opaque context argument for @completer.
3301c33be57SNicolas Pitre *
33171ce1deeSNicolas Pitre * This function causes a cluster switch on the given CPU by waking up
33271ce1deeSNicolas Pitre * the appropriate switcher thread. This function may or may not return
33371ce1deeSNicolas Pitre * before the switch has occurred.
3340577fee2SDave Martin *
3350577fee2SDave Martin * If a @completer callback function is supplied, it will be called when
3360577fee2SDave Martin * the switch is complete. This can be used to determine asynchronously
3370577fee2SDave Martin * when the switch is complete, regardless of when bL_switch_request()
3380577fee2SDave Martin * returns. When @completer is supplied, no new switch request is permitted
3390577fee2SDave Martin * for the affected CPU until after the switch is complete, and @completer
3400577fee2SDave Martin * has returned.
3411c33be57SNicolas Pitre */
bL_switch_request_cb(unsigned int cpu,unsigned int new_cluster_id,bL_switch_completion_handler completer,void * completer_cookie)3420577fee2SDave Martin int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
3430577fee2SDave Martin bL_switch_completion_handler completer,
3440577fee2SDave Martin void *completer_cookie)
3451c33be57SNicolas Pitre {
34671ce1deeSNicolas Pitre struct bL_thread *t;
3471c33be57SNicolas Pitre
34871ce1deeSNicolas Pitre if (cpu >= ARRAY_SIZE(bL_threads)) {
34971ce1deeSNicolas Pitre pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
35071ce1deeSNicolas Pitre return -EINVAL;
3511c33be57SNicolas Pitre }
3521c33be57SNicolas Pitre
35371ce1deeSNicolas Pitre t = &bL_threads[cpu];
3540577fee2SDave Martin
35571ce1deeSNicolas Pitre if (IS_ERR(t->task))
35671ce1deeSNicolas Pitre return PTR_ERR(t->task);
35771ce1deeSNicolas Pitre if (!t->task)
35871ce1deeSNicolas Pitre return -ESRCH;
35971ce1deeSNicolas Pitre
3600577fee2SDave Martin spin_lock(&t->lock);
3610577fee2SDave Martin if (t->completer) {
3620577fee2SDave Martin spin_unlock(&t->lock);
3630577fee2SDave Martin return -EBUSY;
3640577fee2SDave Martin }
3650577fee2SDave Martin t->completer = completer;
3660577fee2SDave Martin t->completer_cookie = completer_cookie;
36771ce1deeSNicolas Pitre t->wanted_cluster = new_cluster_id;
3680577fee2SDave Martin spin_unlock(&t->lock);
36971ce1deeSNicolas Pitre wake_up(&t->wq);
37071ce1deeSNicolas Pitre return 0;
3711c33be57SNicolas Pitre }
3720577fee2SDave Martin EXPORT_SYMBOL_GPL(bL_switch_request_cb);
37371ce1deeSNicolas Pitre
3749797a0e9SNicolas Pitre /*
3759797a0e9SNicolas Pitre * Activation and configuration code.
3769797a0e9SNicolas Pitre */
3779797a0e9SNicolas Pitre
378c0f43751SDave Martin static DEFINE_MUTEX(bL_switcher_activation_lock);
379491990e2SDave Martin static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
3806b7437aeSNicolas Pitre static unsigned int bL_switcher_active;
38138c35d4fSNicolas Pitre static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
3829797a0e9SNicolas Pitre static cpumask_t bL_switcher_removed_logical_cpus;
3839797a0e9SNicolas Pitre
bL_switcher_register_notifier(struct notifier_block * nb)384491990e2SDave Martin int bL_switcher_register_notifier(struct notifier_block *nb)
385491990e2SDave Martin {
386491990e2SDave Martin return blocking_notifier_chain_register(&bL_activation_notifier, nb);
387491990e2SDave Martin }
388491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
389491990e2SDave Martin
bL_switcher_unregister_notifier(struct notifier_block * nb)390491990e2SDave Martin int bL_switcher_unregister_notifier(struct notifier_block *nb)
391491990e2SDave Martin {
392491990e2SDave Martin return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
393491990e2SDave Martin }
394491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
395491990e2SDave Martin
bL_activation_notify(unsigned long val)396491990e2SDave Martin static int bL_activation_notify(unsigned long val)
397491990e2SDave Martin {
398491990e2SDave Martin int ret;
399491990e2SDave Martin
400491990e2SDave Martin ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
401491990e2SDave Martin if (ret & NOTIFY_STOP_MASK)
402491990e2SDave Martin pr_err("%s: notifier chain failed with status 0x%x\n",
403491990e2SDave Martin __func__, ret);
404491990e2SDave Martin return notifier_to_errno(ret);
405491990e2SDave Martin }
406491990e2SDave Martin
bL_switcher_restore_cpus(void)4076b7437aeSNicolas Pitre static void bL_switcher_restore_cpus(void)
4089797a0e9SNicolas Pitre {
4099797a0e9SNicolas Pitre int i;
4109797a0e9SNicolas Pitre
4113f8517e7SNicolas Pitre for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
4123f8517e7SNicolas Pitre struct device *cpu_dev = get_cpu_device(i);
4133f8517e7SNicolas Pitre int ret = device_online(cpu_dev);
4143f8517e7SNicolas Pitre if (ret)
4153f8517e7SNicolas Pitre dev_err(cpu_dev, "switcher: unable to restore CPU\n");
4163f8517e7SNicolas Pitre }
4179797a0e9SNicolas Pitre }
4189797a0e9SNicolas Pitre
bL_switcher_halve_cpus(void)4196b7437aeSNicolas Pitre static int bL_switcher_halve_cpus(void)
4209797a0e9SNicolas Pitre {
42138c35d4fSNicolas Pitre int i, j, cluster_0, gic_id, ret;
42238c35d4fSNicolas Pitre unsigned int cpu, cluster, mask;
42338c35d4fSNicolas Pitre cpumask_t available_cpus;
4249797a0e9SNicolas Pitre
42538c35d4fSNicolas Pitre /* First pass to validate what we have */
42638c35d4fSNicolas Pitre mask = 0;
4279797a0e9SNicolas Pitre for_each_online_cpu(i) {
42838c35d4fSNicolas Pitre cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
42938c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
4309797a0e9SNicolas Pitre if (cluster >= 2) {
4319797a0e9SNicolas Pitre pr_err("%s: only dual cluster systems are supported\n", __func__);
4329797a0e9SNicolas Pitre return -EINVAL;
4339797a0e9SNicolas Pitre }
43438c35d4fSNicolas Pitre if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
43538c35d4fSNicolas Pitre return -EINVAL;
43638c35d4fSNicolas Pitre mask |= (1 << cluster);
4379797a0e9SNicolas Pitre }
43838c35d4fSNicolas Pitre if (mask != 3) {
43938c35d4fSNicolas Pitre pr_err("%s: no CPU pairing possible\n", __func__);
4409797a0e9SNicolas Pitre return -EINVAL;
4419797a0e9SNicolas Pitre }
4429797a0e9SNicolas Pitre
44338c35d4fSNicolas Pitre /*
44438c35d4fSNicolas Pitre * Now let's do the pairing. We match each CPU with another CPU
44538c35d4fSNicolas Pitre * from a different cluster. To get a uniform scheduling behavior
44638c35d4fSNicolas Pitre * without fiddling with CPU topology and compute capacity data,
44738c35d4fSNicolas Pitre * we'll use logical CPUs initially belonging to the same cluster.
44838c35d4fSNicolas Pitre */
44938c35d4fSNicolas Pitre memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
45038c35d4fSNicolas Pitre cpumask_copy(&available_cpus, cpu_online_mask);
45138c35d4fSNicolas Pitre cluster_0 = -1;
45238c35d4fSNicolas Pitre for_each_cpu(i, &available_cpus) {
45338c35d4fSNicolas Pitre int match = -1;
45438c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
45538c35d4fSNicolas Pitre if (cluster_0 == -1)
45638c35d4fSNicolas Pitre cluster_0 = cluster;
45738c35d4fSNicolas Pitre if (cluster != cluster_0)
45838c35d4fSNicolas Pitre continue;
45938c35d4fSNicolas Pitre cpumask_clear_cpu(i, &available_cpus);
46038c35d4fSNicolas Pitre for_each_cpu(j, &available_cpus) {
46138c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
46238c35d4fSNicolas Pitre /*
46338c35d4fSNicolas Pitre * Let's remember the last match to create "odd"
46438c35d4fSNicolas Pitre * pairings on purpose in order for other code not
46538c35d4fSNicolas Pitre * to assume any relation between physical and
46638c35d4fSNicolas Pitre * logical CPU numbers.
46738c35d4fSNicolas Pitre */
46838c35d4fSNicolas Pitre if (cluster != cluster_0)
46938c35d4fSNicolas Pitre match = j;
47038c35d4fSNicolas Pitre }
47138c35d4fSNicolas Pitre if (match != -1) {
47238c35d4fSNicolas Pitre bL_switcher_cpu_pairing[i] = match;
47338c35d4fSNicolas Pitre cpumask_clear_cpu(match, &available_cpus);
47438c35d4fSNicolas Pitre pr_info("CPU%d paired with CPU%d\n", i, match);
47538c35d4fSNicolas Pitre }
47638c35d4fSNicolas Pitre }
4779797a0e9SNicolas Pitre
47838c35d4fSNicolas Pitre /*
47938c35d4fSNicolas Pitre * Now we disable the unwanted CPUs i.e. everything that has no
48038c35d4fSNicolas Pitre * pairing information (that includes the pairing counterparts).
48138c35d4fSNicolas Pitre */
48238c35d4fSNicolas Pitre cpumask_clear(&bL_switcher_removed_logical_cpus);
48338c35d4fSNicolas Pitre for_each_online_cpu(i) {
48438c35d4fSNicolas Pitre cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
48538c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
48638c35d4fSNicolas Pitre
487ed96762eSNicolas Pitre /* Let's take note of the GIC ID for this CPU */
48838c35d4fSNicolas Pitre gic_id = gic_get_cpu_id(i);
489ed96762eSNicolas Pitre if (gic_id < 0) {
490ed96762eSNicolas Pitre pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
49138c35d4fSNicolas Pitre bL_switcher_restore_cpus();
492ed96762eSNicolas Pitre return -EINVAL;
493ed96762eSNicolas Pitre }
494ed96762eSNicolas Pitre bL_gic_id[cpu][cluster] = gic_id;
495ed96762eSNicolas Pitre pr_info("GIC ID for CPU %u cluster %u is %u\n",
496ed96762eSNicolas Pitre cpu, cluster, gic_id);
497ed96762eSNicolas Pitre
49838c35d4fSNicolas Pitre if (bL_switcher_cpu_pairing[i] != -1) {
49938c35d4fSNicolas Pitre bL_switcher_cpu_original_cluster[i] = cluster;
5009797a0e9SNicolas Pitre continue;
5019797a0e9SNicolas Pitre }
5029797a0e9SNicolas Pitre
5033f8517e7SNicolas Pitre ret = device_offline(get_cpu_device(i));
5049797a0e9SNicolas Pitre if (ret) {
5059797a0e9SNicolas Pitre bL_switcher_restore_cpus();
5069797a0e9SNicolas Pitre return ret;
5079797a0e9SNicolas Pitre }
5089797a0e9SNicolas Pitre cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
5099797a0e9SNicolas Pitre }
5109797a0e9SNicolas Pitre
5119797a0e9SNicolas Pitre return 0;
5129797a0e9SNicolas Pitre }
5139797a0e9SNicolas Pitre
514d08e2e09SDave Martin /* Determine the logical CPU a given physical CPU is grouped on. */
bL_switcher_get_logical_index(u32 mpidr)515d08e2e09SDave Martin int bL_switcher_get_logical_index(u32 mpidr)
516d08e2e09SDave Martin {
517d08e2e09SDave Martin int cpu;
518d08e2e09SDave Martin
519d08e2e09SDave Martin if (!bL_switcher_active)
520d08e2e09SDave Martin return -EUNATCH;
521d08e2e09SDave Martin
522d08e2e09SDave Martin mpidr &= MPIDR_HWID_BITMASK;
523d08e2e09SDave Martin for_each_online_cpu(cpu) {
524d08e2e09SDave Martin int pairing = bL_switcher_cpu_pairing[cpu];
525d08e2e09SDave Martin if (pairing == -1)
526d08e2e09SDave Martin continue;
527d08e2e09SDave Martin if ((mpidr == cpu_logical_map(cpu)) ||
528d08e2e09SDave Martin (mpidr == cpu_logical_map(pairing)))
529d08e2e09SDave Martin return cpu;
530d08e2e09SDave Martin }
531d08e2e09SDave Martin return -EINVAL;
532d08e2e09SDave Martin }
533d08e2e09SDave Martin
bL_switcher_trace_trigger_cpu(void * __always_unused info)534b09bbe5bSDave Martin static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
535b09bbe5bSDave Martin {
53641fa4215SThomas Gleixner trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
537b09bbe5bSDave Martin }
538b09bbe5bSDave Martin
bL_switcher_trace_trigger(void)53929064b88SDave Martin int bL_switcher_trace_trigger(void)
540b09bbe5bSDave Martin {
541b09bbe5bSDave Martin preempt_disable();
542b09bbe5bSDave Martin
543b09bbe5bSDave Martin bL_switcher_trace_trigger_cpu(NULL);
544caa75932SNadav Amit smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
545b09bbe5bSDave Martin
546b09bbe5bSDave Martin preempt_enable();
547b09bbe5bSDave Martin
548caa75932SNadav Amit return 0;
549b09bbe5bSDave Martin }
55029064b88SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
551b09bbe5bSDave Martin
bL_switcher_enable(void)5526b7437aeSNicolas Pitre static int bL_switcher_enable(void)
55371ce1deeSNicolas Pitre {
5549797a0e9SNicolas Pitre int cpu, ret;
55571ce1deeSNicolas Pitre
556c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock);
557b0ced9d2STushar Behera lock_device_hotplug();
5586b7437aeSNicolas Pitre if (bL_switcher_active) {
559b0ced9d2STushar Behera unlock_device_hotplug();
560c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock);
5616b7437aeSNicolas Pitre return 0;
5629797a0e9SNicolas Pitre }
5639797a0e9SNicolas Pitre
5646b7437aeSNicolas Pitre pr_info("big.LITTLE switcher initializing\n");
5656b7437aeSNicolas Pitre
566491990e2SDave Martin ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
567491990e2SDave Martin if (ret)
568491990e2SDave Martin goto error;
569491990e2SDave Martin
5709797a0e9SNicolas Pitre ret = bL_switcher_halve_cpus();
571491990e2SDave Martin if (ret)
572491990e2SDave Martin goto error;
5739797a0e9SNicolas Pitre
574b09bbe5bSDave Martin bL_switcher_trace_trigger();
575b09bbe5bSDave Martin
57671ce1deeSNicolas Pitre for_each_online_cpu(cpu) {
57771ce1deeSNicolas Pitre struct bL_thread *t = &bL_threads[cpu];
5780577fee2SDave Martin spin_lock_init(&t->lock);
57971ce1deeSNicolas Pitre init_waitqueue_head(&t->wq);
5806b7437aeSNicolas Pitre init_completion(&t->started);
58171ce1deeSNicolas Pitre t->wanted_cluster = -1;
58271ce1deeSNicolas Pitre t->task = bL_switcher_thread_create(cpu, t);
58371ce1deeSNicolas Pitre }
5846b7437aeSNicolas Pitre
5856b7437aeSNicolas Pitre bL_switcher_active = 1;
586491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_ENABLE);
58771ce1deeSNicolas Pitre pr_info("big.LITTLE switcher initialized\n");
588491990e2SDave Martin goto out;
589c0f43751SDave Martin
590491990e2SDave Martin error:
591491990e2SDave Martin pr_warn("big.LITTLE switcher initialization failed\n");
592491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_DISABLE);
593491990e2SDave Martin
594491990e2SDave Martin out:
595b0ced9d2STushar Behera unlock_device_hotplug();
596c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock);
597491990e2SDave Martin return ret;
59871ce1deeSNicolas Pitre }
59971ce1deeSNicolas Pitre
6006b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
6016b7437aeSNicolas Pitre
bL_switcher_disable(void)6026b7437aeSNicolas Pitre static void bL_switcher_disable(void)
6036b7437aeSNicolas Pitre {
60438c35d4fSNicolas Pitre unsigned int cpu, cluster;
6056b7437aeSNicolas Pitre struct bL_thread *t;
6066b7437aeSNicolas Pitre struct task_struct *task;
6076b7437aeSNicolas Pitre
608c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock);
609b0ced9d2STushar Behera lock_device_hotplug();
610491990e2SDave Martin
611491990e2SDave Martin if (!bL_switcher_active)
612491990e2SDave Martin goto out;
613491990e2SDave Martin
614491990e2SDave Martin if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
615491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_ENABLE);
616491990e2SDave Martin goto out;
6176b7437aeSNicolas Pitre }
618491990e2SDave Martin
6196b7437aeSNicolas Pitre bL_switcher_active = 0;
6206b7437aeSNicolas Pitre
6216b7437aeSNicolas Pitre /*
6226b7437aeSNicolas Pitre * To deactivate the switcher, we must shut down the switcher
6236b7437aeSNicolas Pitre * threads to prevent any other requests from being accepted.
6246b7437aeSNicolas Pitre * Then, if the final cluster for given logical CPU is not the
6256b7437aeSNicolas Pitre * same as the original one, we'll recreate a switcher thread
6266b7437aeSNicolas Pitre * just for the purpose of switching the CPU back without any
6276b7437aeSNicolas Pitre * possibility for interference from external requests.
6286b7437aeSNicolas Pitre */
6296b7437aeSNicolas Pitre for_each_online_cpu(cpu) {
6306b7437aeSNicolas Pitre t = &bL_threads[cpu];
6316b7437aeSNicolas Pitre task = t->task;
6326b7437aeSNicolas Pitre t->task = NULL;
6336b7437aeSNicolas Pitre if (!task || IS_ERR(task))
6346b7437aeSNicolas Pitre continue;
6356b7437aeSNicolas Pitre kthread_stop(task);
6366b7437aeSNicolas Pitre /* no more switch may happen on this CPU at this point */
6376b7437aeSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6386b7437aeSNicolas Pitre if (cluster == bL_switcher_cpu_original_cluster[cpu])
6396b7437aeSNicolas Pitre continue;
6406b7437aeSNicolas Pitre init_completion(&t->started);
6416b7437aeSNicolas Pitre t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
6426b7437aeSNicolas Pitre task = bL_switcher_thread_create(cpu, t);
6436b7437aeSNicolas Pitre if (!IS_ERR(task)) {
6446b7437aeSNicolas Pitre wait_for_completion(&t->started);
6456b7437aeSNicolas Pitre kthread_stop(task);
6466b7437aeSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6476b7437aeSNicolas Pitre if (cluster == bL_switcher_cpu_original_cluster[cpu])
6486b7437aeSNicolas Pitre continue;
6496b7437aeSNicolas Pitre }
6506b7437aeSNicolas Pitre /* If execution gets here, we're in trouble. */
6516b7437aeSNicolas Pitre pr_crit("%s: unable to restore original cluster for CPU %d\n",
6526b7437aeSNicolas Pitre __func__, cpu);
6536b7437aeSNicolas Pitre pr_crit("%s: CPU %d can't be restored\n",
65438c35d4fSNicolas Pitre __func__, bL_switcher_cpu_pairing[cpu]);
65538c35d4fSNicolas Pitre cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
65638c35d4fSNicolas Pitre &bL_switcher_removed_logical_cpus);
6576b7437aeSNicolas Pitre }
6586b7437aeSNicolas Pitre
6596b7437aeSNicolas Pitre bL_switcher_restore_cpus();
660b09bbe5bSDave Martin bL_switcher_trace_trigger();
661b09bbe5bSDave Martin
662491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_DISABLE);
663491990e2SDave Martin
664491990e2SDave Martin out:
665b0ced9d2STushar Behera unlock_device_hotplug();
666c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock);
6676b7437aeSNicolas Pitre }
6686b7437aeSNicolas Pitre
bL_switcher_active_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)6696b7437aeSNicolas Pitre static ssize_t bL_switcher_active_show(struct kobject *kobj,
6706b7437aeSNicolas Pitre struct kobj_attribute *attr, char *buf)
6716b7437aeSNicolas Pitre {
6726b7437aeSNicolas Pitre return sprintf(buf, "%u\n", bL_switcher_active);
6736b7437aeSNicolas Pitre }
6746b7437aeSNicolas Pitre
bL_switcher_active_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)6756b7437aeSNicolas Pitre static ssize_t bL_switcher_active_store(struct kobject *kobj,
6766b7437aeSNicolas Pitre struct kobj_attribute *attr, const char *buf, size_t count)
6776b7437aeSNicolas Pitre {
6786b7437aeSNicolas Pitre int ret;
6796b7437aeSNicolas Pitre
6806b7437aeSNicolas Pitre switch (buf[0]) {
6816b7437aeSNicolas Pitre case '0':
6826b7437aeSNicolas Pitre bL_switcher_disable();
6836b7437aeSNicolas Pitre ret = 0;
6846b7437aeSNicolas Pitre break;
6856b7437aeSNicolas Pitre case '1':
6866b7437aeSNicolas Pitre ret = bL_switcher_enable();
6876b7437aeSNicolas Pitre break;
6886b7437aeSNicolas Pitre default:
6896b7437aeSNicolas Pitre ret = -EINVAL;
6906b7437aeSNicolas Pitre }
6916b7437aeSNicolas Pitre
6926b7437aeSNicolas Pitre return (ret >= 0) ? count : ret;
6936b7437aeSNicolas Pitre }
6946b7437aeSNicolas Pitre
bL_switcher_trace_trigger_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)695b09bbe5bSDave Martin static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
696b09bbe5bSDave Martin struct kobj_attribute *attr, const char *buf, size_t count)
697b09bbe5bSDave Martin {
698b09bbe5bSDave Martin int ret = bL_switcher_trace_trigger();
699b09bbe5bSDave Martin
700b09bbe5bSDave Martin return ret ? ret : count;
701b09bbe5bSDave Martin }
702b09bbe5bSDave Martin
7036b7437aeSNicolas Pitre static struct kobj_attribute bL_switcher_active_attr =
7046b7437aeSNicolas Pitre __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
7056b7437aeSNicolas Pitre
706b09bbe5bSDave Martin static struct kobj_attribute bL_switcher_trace_trigger_attr =
707b09bbe5bSDave Martin __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
708b09bbe5bSDave Martin
7096b7437aeSNicolas Pitre static struct attribute *bL_switcher_attrs[] = {
7106b7437aeSNicolas Pitre &bL_switcher_active_attr.attr,
711b09bbe5bSDave Martin &bL_switcher_trace_trigger_attr.attr,
7126b7437aeSNicolas Pitre NULL,
7136b7437aeSNicolas Pitre };
7146b7437aeSNicolas Pitre
7156b7437aeSNicolas Pitre static struct attribute_group bL_switcher_attr_group = {
7166b7437aeSNicolas Pitre .attrs = bL_switcher_attrs,
7176b7437aeSNicolas Pitre };
7186b7437aeSNicolas Pitre
7196b7437aeSNicolas Pitre static struct kobject *bL_switcher_kobj;
7206b7437aeSNicolas Pitre
bL_switcher_sysfs_init(void)7216b7437aeSNicolas Pitre static int __init bL_switcher_sysfs_init(void)
7226b7437aeSNicolas Pitre {
7236b7437aeSNicolas Pitre int ret;
7246b7437aeSNicolas Pitre
7256b7437aeSNicolas Pitre bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
7266b7437aeSNicolas Pitre if (!bL_switcher_kobj)
7276b7437aeSNicolas Pitre return -ENOMEM;
7286b7437aeSNicolas Pitre ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
7296b7437aeSNicolas Pitre if (ret)
7306b7437aeSNicolas Pitre kobject_put(bL_switcher_kobj);
7316b7437aeSNicolas Pitre return ret;
7326b7437aeSNicolas Pitre }
7336b7437aeSNicolas Pitre
7346b7437aeSNicolas Pitre #endif /* CONFIG_SYSFS */
7356b7437aeSNicolas Pitre
bL_switcher_get_enabled(void)736c0f43751SDave Martin bool bL_switcher_get_enabled(void)
737c0f43751SDave Martin {
738c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock);
739c0f43751SDave Martin
740c0f43751SDave Martin return bL_switcher_active;
741c0f43751SDave Martin }
742c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
743c0f43751SDave Martin
bL_switcher_put_enabled(void)744c0f43751SDave Martin void bL_switcher_put_enabled(void)
745c0f43751SDave Martin {
746c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock);
747c0f43751SDave Martin }
748c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
749c0f43751SDave Martin
75027261435SNicolas Pitre /*
75127261435SNicolas Pitre * Veto any CPU hotplug operation on those CPUs we've removed
75227261435SNicolas Pitre * while the switcher is active.
75327261435SNicolas Pitre * We're just not ready to deal with that given the trickery involved.
75427261435SNicolas Pitre */
bL_switcher_cpu_pre(unsigned int cpu)755a3c9b14fSSebastian Andrzej Siewior static int bL_switcher_cpu_pre(unsigned int cpu)
75627261435SNicolas Pitre {
757a3c9b14fSSebastian Andrzej Siewior int pairing;
758a3c9b14fSSebastian Andrzej Siewior
759a3c9b14fSSebastian Andrzej Siewior if (!bL_switcher_active)
760a3c9b14fSSebastian Andrzej Siewior return 0;
761a3c9b14fSSebastian Andrzej Siewior
762a3c9b14fSSebastian Andrzej Siewior pairing = bL_switcher_cpu_pairing[cpu];
763a3c9b14fSSebastian Andrzej Siewior
76427261435SNicolas Pitre if (pairing == -1)
765a3c9b14fSSebastian Andrzej Siewior return -EINVAL;
766a3c9b14fSSebastian Andrzej Siewior return 0;
76727261435SNicolas Pitre }
76827261435SNicolas Pitre
769c4821c05SNicolas Pitre static bool no_bL_switcher;
770c4821c05SNicolas Pitre core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
771c4821c05SNicolas Pitre
bL_switcher_init(void)7726b7437aeSNicolas Pitre static int __init bL_switcher_init(void)
7736b7437aeSNicolas Pitre {
7746b7437aeSNicolas Pitre int ret;
7756b7437aeSNicolas Pitre
7764530e4b6SNicolas Pitre if (!mcpm_is_available())
7774530e4b6SNicolas Pitre return -ENODEV;
7786b7437aeSNicolas Pitre
779a3c9b14fSSebastian Andrzej Siewior cpuhp_setup_state_nocalls(CPUHP_ARM_BL_PREPARE, "arm/bl:prepare",
780a3c9b14fSSebastian Andrzej Siewior bL_switcher_cpu_pre, NULL);
781a3c9b14fSSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/bl:predown",
782a3c9b14fSSebastian Andrzej Siewior NULL, bL_switcher_cpu_pre);
783a3c9b14fSSebastian Andrzej Siewior if (ret < 0) {
784a3c9b14fSSebastian Andrzej Siewior cpuhp_remove_state_nocalls(CPUHP_ARM_BL_PREPARE);
785a3c9b14fSSebastian Andrzej Siewior pr_err("bL_switcher: Failed to allocate a hotplug state\n");
786a3c9b14fSSebastian Andrzej Siewior return ret;
787a3c9b14fSSebastian Andrzej Siewior }
788c4821c05SNicolas Pitre if (!no_bL_switcher) {
7896b7437aeSNicolas Pitre ret = bL_switcher_enable();
7906b7437aeSNicolas Pitre if (ret)
7916b7437aeSNicolas Pitre return ret;
792c4821c05SNicolas Pitre }
7936b7437aeSNicolas Pitre
7946b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
7956b7437aeSNicolas Pitre ret = bL_switcher_sysfs_init();
7966b7437aeSNicolas Pitre if (ret)
7976b7437aeSNicolas Pitre pr_err("%s: unable to create sysfs entry\n", __func__);
7986b7437aeSNicolas Pitre #endif
7996b7437aeSNicolas Pitre
8006b7437aeSNicolas Pitre return 0;
8016b7437aeSNicolas Pitre }
8026b7437aeSNicolas Pitre
80371ce1deeSNicolas Pitre late_initcall(bL_switcher_init);
804