xref: /openbmc/linux/arch/arm/common/bL_switcher.c (revision 1bfbddb6f3a0dbb8c3996d1c4d4911d695737c15)
11c33be57SNicolas Pitre /*
21c33be57SNicolas Pitre  * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
31c33be57SNicolas Pitre  *
41c33be57SNicolas Pitre  * Created by:	Nicolas Pitre, March 2012
51c33be57SNicolas Pitre  * Copyright:	(C) 2012-2013  Linaro Limited
61c33be57SNicolas Pitre  *
71c33be57SNicolas Pitre  * This program is free software; you can redistribute it and/or modify
81c33be57SNicolas Pitre  * it under the terms of the GNU General Public License version 2 as
91c33be57SNicolas Pitre  * published by the Free Software Foundation.
101c33be57SNicolas Pitre  */
111c33be57SNicolas Pitre 
120577fee2SDave Martin #include <linux/atomic.h>
131c33be57SNicolas Pitre #include <linux/init.h>
141c33be57SNicolas Pitre #include <linux/kernel.h>
151c33be57SNicolas Pitre #include <linux/module.h>
161c33be57SNicolas Pitre #include <linux/sched.h>
171c33be57SNicolas Pitre #include <linux/interrupt.h>
181c33be57SNicolas Pitre #include <linux/cpu_pm.h>
1971ce1deeSNicolas Pitre #include <linux/cpu.h>
203f09d479SLorenzo Pieralisi #include <linux/cpumask.h>
2171ce1deeSNicolas Pitre #include <linux/kthread.h>
2271ce1deeSNicolas Pitre #include <linux/wait.h>
23*1bfbddb6SDave Martin #include <linux/time.h>
243f09d479SLorenzo Pieralisi #include <linux/clockchips.h>
253f09d479SLorenzo Pieralisi #include <linux/hrtimer.h>
263f09d479SLorenzo Pieralisi #include <linux/tick.h>
27491990e2SDave Martin #include <linux/notifier.h>
281c33be57SNicolas Pitre #include <linux/mm.h>
29c0f43751SDave Martin #include <linux/mutex.h>
300577fee2SDave Martin #include <linux/spinlock.h>
311c33be57SNicolas Pitre #include <linux/string.h>
326b7437aeSNicolas Pitre #include <linux/sysfs.h>
331c33be57SNicolas Pitre #include <linux/irqchip/arm-gic.h>
34c4821c05SNicolas Pitre #include <linux/moduleparam.h>
351c33be57SNicolas Pitre 
361c33be57SNicolas Pitre #include <asm/smp_plat.h>
37*1bfbddb6SDave Martin #include <asm/cputype.h>
381c33be57SNicolas Pitre #include <asm/suspend.h>
391c33be57SNicolas Pitre #include <asm/mcpm.h>
401c33be57SNicolas Pitre #include <asm/bL_switcher.h>
411c33be57SNicolas Pitre 
42*1bfbddb6SDave Martin #define CREATE_TRACE_POINTS
43*1bfbddb6SDave Martin #include <trace/events/power_cpu_migrate.h>
44*1bfbddb6SDave Martin 
451c33be57SNicolas Pitre 
461c33be57SNicolas Pitre /*
471c33be57SNicolas Pitre  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
481c33be57SNicolas Pitre  * __attribute_const__ and we don't want the compiler to assume any
491c33be57SNicolas Pitre  * constness here as the value _does_ change along some code paths.
501c33be57SNicolas Pitre  */
511c33be57SNicolas Pitre 
521c33be57SNicolas Pitre static int read_mpidr(void)
531c33be57SNicolas Pitre {
541c33be57SNicolas Pitre 	unsigned int id;
551c33be57SNicolas Pitre 	asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
561c33be57SNicolas Pitre 	return id & MPIDR_HWID_BITMASK;
571c33be57SNicolas Pitre }
581c33be57SNicolas Pitre 
591c33be57SNicolas Pitre /*
60*1bfbddb6SDave Martin  * Get a global nanosecond time stamp for tracing.
61*1bfbddb6SDave Martin  */
62*1bfbddb6SDave Martin static s64 get_ns(void)
63*1bfbddb6SDave Martin {
64*1bfbddb6SDave Martin 	struct timespec ts;
65*1bfbddb6SDave Martin 	getnstimeofday(&ts);
66*1bfbddb6SDave Martin 	return timespec_to_ns(&ts);
67*1bfbddb6SDave Martin }
68*1bfbddb6SDave Martin 
69*1bfbddb6SDave Martin /*
701c33be57SNicolas Pitre  * bL switcher core code.
711c33be57SNicolas Pitre  */
721c33be57SNicolas Pitre 
73108a9640SNicolas Pitre static void bL_do_switch(void *_arg)
741c33be57SNicolas Pitre {
7538c35d4fSNicolas Pitre 	unsigned ib_mpidr, ib_cpu, ib_cluster;
76108a9640SNicolas Pitre 	long volatile handshake, **handshake_ptr = _arg;
771c33be57SNicolas Pitre 
781c33be57SNicolas Pitre 	pr_debug("%s\n", __func__);
791c33be57SNicolas Pitre 
8038c35d4fSNicolas Pitre 	ib_mpidr = cpu_logical_map(smp_processor_id());
8138c35d4fSNicolas Pitre 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
8238c35d4fSNicolas Pitre 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
831c33be57SNicolas Pitre 
84108a9640SNicolas Pitre 	/* Advertise our handshake location */
85108a9640SNicolas Pitre 	if (handshake_ptr) {
86108a9640SNicolas Pitre 		handshake = 0;
87108a9640SNicolas Pitre 		*handshake_ptr = &handshake;
88108a9640SNicolas Pitre 	} else
89108a9640SNicolas Pitre 		handshake = -1;
90108a9640SNicolas Pitre 
911c33be57SNicolas Pitre 	/*
921c33be57SNicolas Pitre 	 * Our state has been saved at this point.  Let's release our
931c33be57SNicolas Pitre 	 * inbound CPU.
941c33be57SNicolas Pitre 	 */
9538c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
961c33be57SNicolas Pitre 	sev();
971c33be57SNicolas Pitre 
981c33be57SNicolas Pitre 	/*
991c33be57SNicolas Pitre 	 * From this point, we must assume that our counterpart CPU might
1001c33be57SNicolas Pitre 	 * have taken over in its parallel world already, as if execution
1011c33be57SNicolas Pitre 	 * just returned from cpu_suspend().  It is therefore important to
1021c33be57SNicolas Pitre 	 * be very careful not to make any change the other guy is not
1031c33be57SNicolas Pitre 	 * expecting.  This is why we need stack isolation.
1041c33be57SNicolas Pitre 	 *
1051c33be57SNicolas Pitre 	 * Fancy under cover tasks could be performed here.  For now
1061c33be57SNicolas Pitre 	 * we have none.
1071c33be57SNicolas Pitre 	 */
1081c33be57SNicolas Pitre 
109108a9640SNicolas Pitre 	/*
110108a9640SNicolas Pitre 	 * Let's wait until our inbound is alive.
111108a9640SNicolas Pitre 	 */
112108a9640SNicolas Pitre 	while (!handshake) {
113108a9640SNicolas Pitre 		wfe();
114108a9640SNicolas Pitre 		smp_mb();
115108a9640SNicolas Pitre 	}
116108a9640SNicolas Pitre 
1171c33be57SNicolas Pitre 	/* Let's put ourself down. */
1181c33be57SNicolas Pitre 	mcpm_cpu_power_down();
1191c33be57SNicolas Pitre 
1201c33be57SNicolas Pitre 	/* should never get here */
1211c33be57SNicolas Pitre 	BUG();
1221c33be57SNicolas Pitre }
1231c33be57SNicolas Pitre 
1241c33be57SNicolas Pitre /*
125c052de26SNicolas Pitre  * Stack isolation.  To ensure 'current' remains valid, we just use another
126c052de26SNicolas Pitre  * piece of our thread's stack space which should be fairly lightly used.
127c052de26SNicolas Pitre  * The selected area starts just above the thread_info structure located
128c052de26SNicolas Pitre  * at the very bottom of the stack, aligned to a cache line, and indexed
129c052de26SNicolas Pitre  * with the cluster number.
1301c33be57SNicolas Pitre  */
131c052de26SNicolas Pitre #define STACK_SIZE 512
1321c33be57SNicolas Pitre extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
1331c33be57SNicolas Pitre static int bL_switchpoint(unsigned long _arg)
1341c33be57SNicolas Pitre {
1351c33be57SNicolas Pitre 	unsigned int mpidr = read_mpidr();
1361c33be57SNicolas Pitre 	unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
137c052de26SNicolas Pitre 	void *stack = current_thread_info() + 1;
1381c33be57SNicolas Pitre 	stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
139c052de26SNicolas Pitre 	stack += clusterid * STACK_SIZE + STACK_SIZE;
1401c33be57SNicolas Pitre 	call_with_stack(bL_do_switch, (void *)_arg, stack);
1411c33be57SNicolas Pitre 	BUG();
1421c33be57SNicolas Pitre }
1431c33be57SNicolas Pitre 
1441c33be57SNicolas Pitre /*
1451c33be57SNicolas Pitre  * Generic switcher interface
1461c33be57SNicolas Pitre  */
1471c33be57SNicolas Pitre 
148ed96762eSNicolas Pitre static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
14938c35d4fSNicolas Pitre static int bL_switcher_cpu_pairing[NR_CPUS];
150ed96762eSNicolas Pitre 
1511c33be57SNicolas Pitre /*
1521c33be57SNicolas Pitre  * bL_switch_to - Switch to a specific cluster for the current CPU
1531c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
1541c33be57SNicolas Pitre  *
1551c33be57SNicolas Pitre  * This function must be called on the CPU to be switched.
1561c33be57SNicolas Pitre  * Returns 0 on success, else a negative status code.
1571c33be57SNicolas Pitre  */
1581c33be57SNicolas Pitre static int bL_switch_to(unsigned int new_cluster_id)
1591c33be57SNicolas Pitre {
16038c35d4fSNicolas Pitre 	unsigned int mpidr, this_cpu, that_cpu;
16138c35d4fSNicolas Pitre 	unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
1626137eba6SNicolas Pitre 	struct completion inbound_alive;
1633f09d479SLorenzo Pieralisi 	struct tick_device *tdev;
1643f09d479SLorenzo Pieralisi 	enum clock_event_mode tdev_mode;
165108a9640SNicolas Pitre 	long volatile *handshake_ptr;
1666137eba6SNicolas Pitre 	int ipi_nr, ret;
1671c33be57SNicolas Pitre 
16838c35d4fSNicolas Pitre 	this_cpu = smp_processor_id();
16938c35d4fSNicolas Pitre 	ob_mpidr = read_mpidr();
17038c35d4fSNicolas Pitre 	ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
17138c35d4fSNicolas Pitre 	ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
17238c35d4fSNicolas Pitre 	BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
1731c33be57SNicolas Pitre 
17438c35d4fSNicolas Pitre 	if (new_cluster_id == ob_cluster)
1751c33be57SNicolas Pitre 		return 0;
1761c33be57SNicolas Pitre 
17738c35d4fSNicolas Pitre 	that_cpu = bL_switcher_cpu_pairing[this_cpu];
17838c35d4fSNicolas Pitre 	ib_mpidr = cpu_logical_map(that_cpu);
17938c35d4fSNicolas Pitre 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
18038c35d4fSNicolas Pitre 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
18138c35d4fSNicolas Pitre 
18238c35d4fSNicolas Pitre 	pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
18338c35d4fSNicolas Pitre 		 this_cpu, ob_mpidr, ib_mpidr);
1841c33be57SNicolas Pitre 
1856137eba6SNicolas Pitre 	this_cpu = smp_processor_id();
1866137eba6SNicolas Pitre 
1871c33be57SNicolas Pitre 	/* Close the gate for our entry vectors */
18838c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
18938c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
1901c33be57SNicolas Pitre 
1916137eba6SNicolas Pitre 	/* Install our "inbound alive" notifier. */
1926137eba6SNicolas Pitre 	init_completion(&inbound_alive);
1936137eba6SNicolas Pitre 	ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
1946137eba6SNicolas Pitre 	ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
1956137eba6SNicolas Pitre 	mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
1966137eba6SNicolas Pitre 
1971c33be57SNicolas Pitre 	/*
1981c33be57SNicolas Pitre 	 * Let's wake up the inbound CPU now in case it requires some delay
1991c33be57SNicolas Pitre 	 * to come online, but leave it gated in our entry vector code.
2001c33be57SNicolas Pitre 	 */
20138c35d4fSNicolas Pitre 	ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
2021c33be57SNicolas Pitre 	if (ret) {
2031c33be57SNicolas Pitre 		pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
2041c33be57SNicolas Pitre 		return ret;
2051c33be57SNicolas Pitre 	}
2061c33be57SNicolas Pitre 
2071c33be57SNicolas Pitre 	/*
2086137eba6SNicolas Pitre 	 * Raise a SGI on the inbound CPU to make sure it doesn't stall
2096137eba6SNicolas Pitre 	 * in a possible WFI, such as in bL_power_down().
2106137eba6SNicolas Pitre 	 */
2116137eba6SNicolas Pitre 	gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
2126137eba6SNicolas Pitre 
2136137eba6SNicolas Pitre 	/*
2146137eba6SNicolas Pitre 	 * Wait for the inbound to come up.  This allows for other
2156137eba6SNicolas Pitre 	 * tasks to be scheduled in the mean time.
2166137eba6SNicolas Pitre 	 */
2176137eba6SNicolas Pitre 	wait_for_completion(&inbound_alive);
2186137eba6SNicolas Pitre 	mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
2196137eba6SNicolas Pitre 
2206137eba6SNicolas Pitre 	/*
2211c33be57SNicolas Pitre 	 * From this point we are entering the switch critical zone
2221c33be57SNicolas Pitre 	 * and can't take any interrupts anymore.
2231c33be57SNicolas Pitre 	 */
2241c33be57SNicolas Pitre 	local_irq_disable();
2251c33be57SNicolas Pitre 	local_fiq_disable();
226*1bfbddb6SDave Martin 	trace_cpu_migrate_begin(get_ns(), ob_mpidr);
2271c33be57SNicolas Pitre 
2281c33be57SNicolas Pitre 	/* redirect GIC's SGIs to our counterpart */
22938c35d4fSNicolas Pitre 	gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
2301c33be57SNicolas Pitre 
2313f09d479SLorenzo Pieralisi 	tdev = tick_get_device(this_cpu);
2323f09d479SLorenzo Pieralisi 	if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
2333f09d479SLorenzo Pieralisi 		tdev = NULL;
2343f09d479SLorenzo Pieralisi 	if (tdev) {
2353f09d479SLorenzo Pieralisi 		tdev_mode = tdev->evtdev->mode;
2363f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
2373f09d479SLorenzo Pieralisi 	}
2383f09d479SLorenzo Pieralisi 
2391c33be57SNicolas Pitre 	ret = cpu_pm_enter();
2401c33be57SNicolas Pitre 
2411c33be57SNicolas Pitre 	/* we can not tolerate errors at this point */
2421c33be57SNicolas Pitre 	if (ret)
2431c33be57SNicolas Pitre 		panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
2441c33be57SNicolas Pitre 
24538c35d4fSNicolas Pitre 	/* Swap the physical CPUs in the logical map for this logical CPU. */
24638c35d4fSNicolas Pitre 	cpu_logical_map(this_cpu) = ib_mpidr;
24738c35d4fSNicolas Pitre 	cpu_logical_map(that_cpu) = ob_mpidr;
2481c33be57SNicolas Pitre 
2491c33be57SNicolas Pitre 	/* Let's do the actual CPU switch. */
250108a9640SNicolas Pitre 	ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
2511c33be57SNicolas Pitre 	if (ret > 0)
2521c33be57SNicolas Pitre 		panic("%s: cpu_suspend() returned %d\n", __func__, ret);
2531c33be57SNicolas Pitre 
2541c33be57SNicolas Pitre 	/* We are executing on the inbound CPU at this point */
2551c33be57SNicolas Pitre 	mpidr = read_mpidr();
25638c35d4fSNicolas Pitre 	pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
25738c35d4fSNicolas Pitre 	BUG_ON(mpidr != ib_mpidr);
2581c33be57SNicolas Pitre 
2591c33be57SNicolas Pitre 	mcpm_cpu_powered_up();
2601c33be57SNicolas Pitre 
2611c33be57SNicolas Pitre 	ret = cpu_pm_exit();
2621c33be57SNicolas Pitre 
2633f09d479SLorenzo Pieralisi 	if (tdev) {
2643f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, tdev_mode);
2653f09d479SLorenzo Pieralisi 		clockevents_program_event(tdev->evtdev,
2663f09d479SLorenzo Pieralisi 					  tdev->evtdev->next_event, 1);
2673f09d479SLorenzo Pieralisi 	}
2683f09d479SLorenzo Pieralisi 
269*1bfbddb6SDave Martin 	trace_cpu_migrate_finish(get_ns(), ib_mpidr);
2701c33be57SNicolas Pitre 	local_fiq_enable();
2711c33be57SNicolas Pitre 	local_irq_enable();
2721c33be57SNicolas Pitre 
273108a9640SNicolas Pitre 	*handshake_ptr = 1;
274108a9640SNicolas Pitre 	dsb_sev();
275108a9640SNicolas Pitre 
2761c33be57SNicolas Pitre 	if (ret)
2771c33be57SNicolas Pitre 		pr_err("%s exiting with error %d\n", __func__, ret);
2781c33be57SNicolas Pitre 	return ret;
2791c33be57SNicolas Pitre }
2801c33be57SNicolas Pitre 
28171ce1deeSNicolas Pitre struct bL_thread {
2820577fee2SDave Martin 	spinlock_t lock;
28371ce1deeSNicolas Pitre 	struct task_struct *task;
28471ce1deeSNicolas Pitre 	wait_queue_head_t wq;
28571ce1deeSNicolas Pitre 	int wanted_cluster;
2866b7437aeSNicolas Pitre 	struct completion started;
2870577fee2SDave Martin 	bL_switch_completion_handler completer;
2880577fee2SDave Martin 	void *completer_cookie;
2891c33be57SNicolas Pitre };
2901c33be57SNicolas Pitre 
29171ce1deeSNicolas Pitre static struct bL_thread bL_threads[NR_CPUS];
29271ce1deeSNicolas Pitre 
29371ce1deeSNicolas Pitre static int bL_switcher_thread(void *arg)
2941c33be57SNicolas Pitre {
29571ce1deeSNicolas Pitre 	struct bL_thread *t = arg;
29671ce1deeSNicolas Pitre 	struct sched_param param = { .sched_priority = 1 };
29771ce1deeSNicolas Pitre 	int cluster;
2980577fee2SDave Martin 	bL_switch_completion_handler completer;
2990577fee2SDave Martin 	void *completer_cookie;
30071ce1deeSNicolas Pitre 
30171ce1deeSNicolas Pitre 	sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
3026b7437aeSNicolas Pitre 	complete(&t->started);
30371ce1deeSNicolas Pitre 
30471ce1deeSNicolas Pitre 	do {
30571ce1deeSNicolas Pitre 		if (signal_pending(current))
30671ce1deeSNicolas Pitre 			flush_signals(current);
30771ce1deeSNicolas Pitre 		wait_event_interruptible(t->wq,
30871ce1deeSNicolas Pitre 				t->wanted_cluster != -1 ||
30971ce1deeSNicolas Pitre 				kthread_should_stop());
3100577fee2SDave Martin 
3110577fee2SDave Martin 		spin_lock(&t->lock);
3120577fee2SDave Martin 		cluster = t->wanted_cluster;
3130577fee2SDave Martin 		completer = t->completer;
3140577fee2SDave Martin 		completer_cookie = t->completer_cookie;
3150577fee2SDave Martin 		t->wanted_cluster = -1;
3160577fee2SDave Martin 		t->completer = NULL;
3170577fee2SDave Martin 		spin_unlock(&t->lock);
3180577fee2SDave Martin 
3190577fee2SDave Martin 		if (cluster != -1) {
32071ce1deeSNicolas Pitre 			bL_switch_to(cluster);
3210577fee2SDave Martin 
3220577fee2SDave Martin 			if (completer)
3230577fee2SDave Martin 				completer(completer_cookie);
3240577fee2SDave Martin 		}
32571ce1deeSNicolas Pitre 	} while (!kthread_should_stop());
32671ce1deeSNicolas Pitre 
32771ce1deeSNicolas Pitre 	return 0;
32871ce1deeSNicolas Pitre }
32971ce1deeSNicolas Pitre 
3306b7437aeSNicolas Pitre static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
33171ce1deeSNicolas Pitre {
33271ce1deeSNicolas Pitre 	struct task_struct *task;
33371ce1deeSNicolas Pitre 
33471ce1deeSNicolas Pitre 	task = kthread_create_on_node(bL_switcher_thread, arg,
33571ce1deeSNicolas Pitre 				      cpu_to_node(cpu), "kswitcher_%d", cpu);
33671ce1deeSNicolas Pitre 	if (!IS_ERR(task)) {
33771ce1deeSNicolas Pitre 		kthread_bind(task, cpu);
33871ce1deeSNicolas Pitre 		wake_up_process(task);
33971ce1deeSNicolas Pitre 	} else
34071ce1deeSNicolas Pitre 		pr_err("%s failed for CPU %d\n", __func__, cpu);
34171ce1deeSNicolas Pitre 	return task;
3421c33be57SNicolas Pitre }
3431c33be57SNicolas Pitre 
3441c33be57SNicolas Pitre /*
3450577fee2SDave Martin  * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
3460577fee2SDave Martin  *      with completion notification via a callback
3471c33be57SNicolas Pitre  *
3481c33be57SNicolas Pitre  * @cpu: the CPU to switch
3491c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
3500577fee2SDave Martin  * @completer: switch completion callback.  if non-NULL,
3510577fee2SDave Martin  *	@completer(@completer_cookie) will be called on completion of
3520577fee2SDave Martin  *	the switch, in non-atomic context.
3530577fee2SDave Martin  * @completer_cookie: opaque context argument for @completer.
3541c33be57SNicolas Pitre  *
35571ce1deeSNicolas Pitre  * This function causes a cluster switch on the given CPU by waking up
35671ce1deeSNicolas Pitre  * the appropriate switcher thread.  This function may or may not return
35771ce1deeSNicolas Pitre  * before the switch has occurred.
3580577fee2SDave Martin  *
3590577fee2SDave Martin  * If a @completer callback function is supplied, it will be called when
3600577fee2SDave Martin  * the switch is complete.  This can be used to determine asynchronously
3610577fee2SDave Martin  * when the switch is complete, regardless of when bL_switch_request()
3620577fee2SDave Martin  * returns.  When @completer is supplied, no new switch request is permitted
3630577fee2SDave Martin  * for the affected CPU until after the switch is complete, and @completer
3640577fee2SDave Martin  * has returned.
3651c33be57SNicolas Pitre  */
3660577fee2SDave Martin int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
3670577fee2SDave Martin 			 bL_switch_completion_handler completer,
3680577fee2SDave Martin 			 void *completer_cookie)
3691c33be57SNicolas Pitre {
37071ce1deeSNicolas Pitre 	struct bL_thread *t;
3711c33be57SNicolas Pitre 
37271ce1deeSNicolas Pitre 	if (cpu >= ARRAY_SIZE(bL_threads)) {
37371ce1deeSNicolas Pitre 		pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
37471ce1deeSNicolas Pitre 		return -EINVAL;
3751c33be57SNicolas Pitre 	}
3761c33be57SNicolas Pitre 
37771ce1deeSNicolas Pitre 	t = &bL_threads[cpu];
3780577fee2SDave Martin 
37971ce1deeSNicolas Pitre 	if (IS_ERR(t->task))
38071ce1deeSNicolas Pitre 		return PTR_ERR(t->task);
38171ce1deeSNicolas Pitre 	if (!t->task)
38271ce1deeSNicolas Pitre 		return -ESRCH;
38371ce1deeSNicolas Pitre 
3840577fee2SDave Martin 	spin_lock(&t->lock);
3850577fee2SDave Martin 	if (t->completer) {
3860577fee2SDave Martin 		spin_unlock(&t->lock);
3870577fee2SDave Martin 		return -EBUSY;
3880577fee2SDave Martin 	}
3890577fee2SDave Martin 	t->completer = completer;
3900577fee2SDave Martin 	t->completer_cookie = completer_cookie;
39171ce1deeSNicolas Pitre 	t->wanted_cluster = new_cluster_id;
3920577fee2SDave Martin 	spin_unlock(&t->lock);
39371ce1deeSNicolas Pitre 	wake_up(&t->wq);
39471ce1deeSNicolas Pitre 	return 0;
3951c33be57SNicolas Pitre }
3960577fee2SDave Martin EXPORT_SYMBOL_GPL(bL_switch_request_cb);
39771ce1deeSNicolas Pitre 
3989797a0e9SNicolas Pitre /*
3999797a0e9SNicolas Pitre  * Activation and configuration code.
4009797a0e9SNicolas Pitre  */
4019797a0e9SNicolas Pitre 
402c0f43751SDave Martin static DEFINE_MUTEX(bL_switcher_activation_lock);
403491990e2SDave Martin static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
4046b7437aeSNicolas Pitre static unsigned int bL_switcher_active;
40538c35d4fSNicolas Pitre static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
4069797a0e9SNicolas Pitre static cpumask_t bL_switcher_removed_logical_cpus;
4079797a0e9SNicolas Pitre 
408491990e2SDave Martin int bL_switcher_register_notifier(struct notifier_block *nb)
409491990e2SDave Martin {
410491990e2SDave Martin 	return blocking_notifier_chain_register(&bL_activation_notifier, nb);
411491990e2SDave Martin }
412491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
413491990e2SDave Martin 
414491990e2SDave Martin int bL_switcher_unregister_notifier(struct notifier_block *nb)
415491990e2SDave Martin {
416491990e2SDave Martin 	return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
417491990e2SDave Martin }
418491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
419491990e2SDave Martin 
420491990e2SDave Martin static int bL_activation_notify(unsigned long val)
421491990e2SDave Martin {
422491990e2SDave Martin 	int ret;
423491990e2SDave Martin 
424491990e2SDave Martin 	ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
425491990e2SDave Martin 	if (ret & NOTIFY_STOP_MASK)
426491990e2SDave Martin 		pr_err("%s: notifier chain failed with status 0x%x\n",
427491990e2SDave Martin 			__func__, ret);
428491990e2SDave Martin 	return notifier_to_errno(ret);
429491990e2SDave Martin }
430491990e2SDave Martin 
4316b7437aeSNicolas Pitre static void bL_switcher_restore_cpus(void)
4329797a0e9SNicolas Pitre {
4339797a0e9SNicolas Pitre 	int i;
4349797a0e9SNicolas Pitre 
4359797a0e9SNicolas Pitre 	for_each_cpu(i, &bL_switcher_removed_logical_cpus)
4369797a0e9SNicolas Pitre 		cpu_up(i);
4379797a0e9SNicolas Pitre }
4389797a0e9SNicolas Pitre 
4396b7437aeSNicolas Pitre static int bL_switcher_halve_cpus(void)
4409797a0e9SNicolas Pitre {
44138c35d4fSNicolas Pitre 	int i, j, cluster_0, gic_id, ret;
44238c35d4fSNicolas Pitre 	unsigned int cpu, cluster, mask;
44338c35d4fSNicolas Pitre 	cpumask_t available_cpus;
4449797a0e9SNicolas Pitre 
44538c35d4fSNicolas Pitre 	/* First pass to validate what we have */
44638c35d4fSNicolas Pitre 	mask = 0;
4479797a0e9SNicolas Pitre 	for_each_online_cpu(i) {
44838c35d4fSNicolas Pitre 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
44938c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
4509797a0e9SNicolas Pitre 		if (cluster >= 2) {
4519797a0e9SNicolas Pitre 			pr_err("%s: only dual cluster systems are supported\n", __func__);
4529797a0e9SNicolas Pitre 			return -EINVAL;
4539797a0e9SNicolas Pitre 		}
45438c35d4fSNicolas Pitre 		if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
45538c35d4fSNicolas Pitre 			return -EINVAL;
45638c35d4fSNicolas Pitre 		mask |= (1 << cluster);
4579797a0e9SNicolas Pitre 	}
45838c35d4fSNicolas Pitre 	if (mask != 3) {
45938c35d4fSNicolas Pitre 		pr_err("%s: no CPU pairing possible\n", __func__);
4609797a0e9SNicolas Pitre 		return -EINVAL;
4619797a0e9SNicolas Pitre 	}
4629797a0e9SNicolas Pitre 
46338c35d4fSNicolas Pitre 	/*
46438c35d4fSNicolas Pitre 	 * Now let's do the pairing.  We match each CPU with another CPU
46538c35d4fSNicolas Pitre 	 * from a different cluster.  To get a uniform scheduling behavior
46638c35d4fSNicolas Pitre 	 * without fiddling with CPU topology and compute capacity data,
46738c35d4fSNicolas Pitre 	 * we'll use logical CPUs initially belonging to the same cluster.
46838c35d4fSNicolas Pitre 	 */
46938c35d4fSNicolas Pitre 	memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
47038c35d4fSNicolas Pitre 	cpumask_copy(&available_cpus, cpu_online_mask);
47138c35d4fSNicolas Pitre 	cluster_0 = -1;
47238c35d4fSNicolas Pitre 	for_each_cpu(i, &available_cpus) {
47338c35d4fSNicolas Pitre 		int match = -1;
47438c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
47538c35d4fSNicolas Pitre 		if (cluster_0 == -1)
47638c35d4fSNicolas Pitre 			cluster_0 = cluster;
47738c35d4fSNicolas Pitre 		if (cluster != cluster_0)
47838c35d4fSNicolas Pitre 			continue;
47938c35d4fSNicolas Pitre 		cpumask_clear_cpu(i, &available_cpus);
48038c35d4fSNicolas Pitre 		for_each_cpu(j, &available_cpus) {
48138c35d4fSNicolas Pitre 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
48238c35d4fSNicolas Pitre 			/*
48338c35d4fSNicolas Pitre 			 * Let's remember the last match to create "odd"
48438c35d4fSNicolas Pitre 			 * pairings on purpose in order for other code not
48538c35d4fSNicolas Pitre 			 * to assume any relation between physical and
48638c35d4fSNicolas Pitre 			 * logical CPU numbers.
48738c35d4fSNicolas Pitre 			 */
48838c35d4fSNicolas Pitre 			if (cluster != cluster_0)
48938c35d4fSNicolas Pitre 				match = j;
49038c35d4fSNicolas Pitre 		}
49138c35d4fSNicolas Pitre 		if (match != -1) {
49238c35d4fSNicolas Pitre 			bL_switcher_cpu_pairing[i] = match;
49338c35d4fSNicolas Pitre 			cpumask_clear_cpu(match, &available_cpus);
49438c35d4fSNicolas Pitre 			pr_info("CPU%d paired with CPU%d\n", i, match);
49538c35d4fSNicolas Pitre 		}
49638c35d4fSNicolas Pitre 	}
4979797a0e9SNicolas Pitre 
49838c35d4fSNicolas Pitre 	/*
49938c35d4fSNicolas Pitre 	 * Now we disable the unwanted CPUs i.e. everything that has no
50038c35d4fSNicolas Pitre 	 * pairing information (that includes the pairing counterparts).
50138c35d4fSNicolas Pitre 	 */
50238c35d4fSNicolas Pitre 	cpumask_clear(&bL_switcher_removed_logical_cpus);
50338c35d4fSNicolas Pitre 	for_each_online_cpu(i) {
50438c35d4fSNicolas Pitre 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
50538c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
50638c35d4fSNicolas Pitre 
507ed96762eSNicolas Pitre 		/* Let's take note of the GIC ID for this CPU */
50838c35d4fSNicolas Pitre 		gic_id = gic_get_cpu_id(i);
509ed96762eSNicolas Pitre 		if (gic_id < 0) {
510ed96762eSNicolas Pitre 			pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
51138c35d4fSNicolas Pitre 			bL_switcher_restore_cpus();
512ed96762eSNicolas Pitre 			return -EINVAL;
513ed96762eSNicolas Pitre 		}
514ed96762eSNicolas Pitre 		bL_gic_id[cpu][cluster] = gic_id;
515ed96762eSNicolas Pitre 		pr_info("GIC ID for CPU %u cluster %u is %u\n",
516ed96762eSNicolas Pitre 			cpu, cluster, gic_id);
517ed96762eSNicolas Pitre 
51838c35d4fSNicolas Pitre 		if (bL_switcher_cpu_pairing[i] != -1) {
51938c35d4fSNicolas Pitre 			bL_switcher_cpu_original_cluster[i] = cluster;
5209797a0e9SNicolas Pitre 			continue;
5219797a0e9SNicolas Pitre 		}
5229797a0e9SNicolas Pitre 
5239797a0e9SNicolas Pitre 		ret = cpu_down(i);
5249797a0e9SNicolas Pitre 		if (ret) {
5259797a0e9SNicolas Pitre 			bL_switcher_restore_cpus();
5269797a0e9SNicolas Pitre 			return ret;
5279797a0e9SNicolas Pitre 		}
5289797a0e9SNicolas Pitre 		cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
5299797a0e9SNicolas Pitre 	}
5309797a0e9SNicolas Pitre 
5319797a0e9SNicolas Pitre 	return 0;
5329797a0e9SNicolas Pitre }
5339797a0e9SNicolas Pitre 
5346b7437aeSNicolas Pitre static int bL_switcher_enable(void)
53571ce1deeSNicolas Pitre {
5369797a0e9SNicolas Pitre 	int cpu, ret;
53771ce1deeSNicolas Pitre 
538c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
5396b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
5406b7437aeSNicolas Pitre 	if (bL_switcher_active) {
5416b7437aeSNicolas Pitre 		cpu_hotplug_driver_unlock();
542c0f43751SDave Martin 		mutex_unlock(&bL_switcher_activation_lock);
5436b7437aeSNicolas Pitre 		return 0;
5449797a0e9SNicolas Pitre 	}
5459797a0e9SNicolas Pitre 
5466b7437aeSNicolas Pitre 	pr_info("big.LITTLE switcher initializing\n");
5476b7437aeSNicolas Pitre 
548491990e2SDave Martin 	ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
549491990e2SDave Martin 	if (ret)
550491990e2SDave Martin 		goto error;
551491990e2SDave Martin 
5529797a0e9SNicolas Pitre 	ret = bL_switcher_halve_cpus();
553491990e2SDave Martin 	if (ret)
554491990e2SDave Martin 		goto error;
5559797a0e9SNicolas Pitre 
55671ce1deeSNicolas Pitre 	for_each_online_cpu(cpu) {
55771ce1deeSNicolas Pitre 		struct bL_thread *t = &bL_threads[cpu];
5580577fee2SDave Martin 		spin_lock_init(&t->lock);
55971ce1deeSNicolas Pitre 		init_waitqueue_head(&t->wq);
5606b7437aeSNicolas Pitre 		init_completion(&t->started);
56171ce1deeSNicolas Pitre 		t->wanted_cluster = -1;
56271ce1deeSNicolas Pitre 		t->task = bL_switcher_thread_create(cpu, t);
56371ce1deeSNicolas Pitre 	}
5646b7437aeSNicolas Pitre 
5656b7437aeSNicolas Pitre 	bL_switcher_active = 1;
566491990e2SDave Martin 	bL_activation_notify(BL_NOTIFY_POST_ENABLE);
56771ce1deeSNicolas Pitre 	pr_info("big.LITTLE switcher initialized\n");
568491990e2SDave Martin 	goto out;
569c0f43751SDave Martin 
570491990e2SDave Martin error:
571491990e2SDave Martin 	pr_warn("big.LITTLE switcher initialization failed\n");
572491990e2SDave Martin 	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
573491990e2SDave Martin 
574491990e2SDave Martin out:
575c0f43751SDave Martin 	cpu_hotplug_driver_unlock();
576c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
577491990e2SDave Martin 	return ret;
57871ce1deeSNicolas Pitre }
57971ce1deeSNicolas Pitre 
5806b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
5816b7437aeSNicolas Pitre 
5826b7437aeSNicolas Pitre static void bL_switcher_disable(void)
5836b7437aeSNicolas Pitre {
58438c35d4fSNicolas Pitre 	unsigned int cpu, cluster;
5856b7437aeSNicolas Pitre 	struct bL_thread *t;
5866b7437aeSNicolas Pitre 	struct task_struct *task;
5876b7437aeSNicolas Pitre 
588c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
5896b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
590491990e2SDave Martin 
591491990e2SDave Martin 	if (!bL_switcher_active)
592491990e2SDave Martin 		goto out;
593491990e2SDave Martin 
594491990e2SDave Martin 	if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
595491990e2SDave Martin 		bL_activation_notify(BL_NOTIFY_POST_ENABLE);
596491990e2SDave Martin 		goto out;
5976b7437aeSNicolas Pitre 	}
598491990e2SDave Martin 
5996b7437aeSNicolas Pitre 	bL_switcher_active = 0;
6006b7437aeSNicolas Pitre 
6016b7437aeSNicolas Pitre 	/*
6026b7437aeSNicolas Pitre 	 * To deactivate the switcher, we must shut down the switcher
6036b7437aeSNicolas Pitre 	 * threads to prevent any other requests from being accepted.
6046b7437aeSNicolas Pitre 	 * Then, if the final cluster for given logical CPU is not the
6056b7437aeSNicolas Pitre 	 * same as the original one, we'll recreate a switcher thread
6066b7437aeSNicolas Pitre 	 * just for the purpose of switching the CPU back without any
6076b7437aeSNicolas Pitre 	 * possibility for interference from external requests.
6086b7437aeSNicolas Pitre 	 */
6096b7437aeSNicolas Pitre 	for_each_online_cpu(cpu) {
6106b7437aeSNicolas Pitre 		t = &bL_threads[cpu];
6116b7437aeSNicolas Pitre 		task = t->task;
6126b7437aeSNicolas Pitre 		t->task = NULL;
6136b7437aeSNicolas Pitre 		if (!task || IS_ERR(task))
6146b7437aeSNicolas Pitre 			continue;
6156b7437aeSNicolas Pitre 		kthread_stop(task);
6166b7437aeSNicolas Pitre 		/* no more switch may happen on this CPU at this point */
6176b7437aeSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6186b7437aeSNicolas Pitre 		if (cluster == bL_switcher_cpu_original_cluster[cpu])
6196b7437aeSNicolas Pitre 			continue;
6206b7437aeSNicolas Pitre 		init_completion(&t->started);
6216b7437aeSNicolas Pitre 		t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
6226b7437aeSNicolas Pitre 		task = bL_switcher_thread_create(cpu, t);
6236b7437aeSNicolas Pitre 		if (!IS_ERR(task)) {
6246b7437aeSNicolas Pitre 			wait_for_completion(&t->started);
6256b7437aeSNicolas Pitre 			kthread_stop(task);
6266b7437aeSNicolas Pitre 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6276b7437aeSNicolas Pitre 			if (cluster == bL_switcher_cpu_original_cluster[cpu])
6286b7437aeSNicolas Pitre 				continue;
6296b7437aeSNicolas Pitre 		}
6306b7437aeSNicolas Pitre 		/* If execution gets here, we're in trouble. */
6316b7437aeSNicolas Pitre 		pr_crit("%s: unable to restore original cluster for CPU %d\n",
6326b7437aeSNicolas Pitre 			__func__, cpu);
6336b7437aeSNicolas Pitre 		pr_crit("%s: CPU %d can't be restored\n",
63438c35d4fSNicolas Pitre 			__func__, bL_switcher_cpu_pairing[cpu]);
63538c35d4fSNicolas Pitre 		cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
63638c35d4fSNicolas Pitre 				  &bL_switcher_removed_logical_cpus);
6376b7437aeSNicolas Pitre 	}
6386b7437aeSNicolas Pitre 
6396b7437aeSNicolas Pitre 	bL_switcher_restore_cpus();
640491990e2SDave Martin 	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
641491990e2SDave Martin 
642491990e2SDave Martin out:
6436b7437aeSNicolas Pitre 	cpu_hotplug_driver_unlock();
644c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
6456b7437aeSNicolas Pitre }
6466b7437aeSNicolas Pitre 
6476b7437aeSNicolas Pitre static ssize_t bL_switcher_active_show(struct kobject *kobj,
6486b7437aeSNicolas Pitre 		struct kobj_attribute *attr, char *buf)
6496b7437aeSNicolas Pitre {
6506b7437aeSNicolas Pitre 	return sprintf(buf, "%u\n", bL_switcher_active);
6516b7437aeSNicolas Pitre }
6526b7437aeSNicolas Pitre 
6536b7437aeSNicolas Pitre static ssize_t bL_switcher_active_store(struct kobject *kobj,
6546b7437aeSNicolas Pitre 		struct kobj_attribute *attr, const char *buf, size_t count)
6556b7437aeSNicolas Pitre {
6566b7437aeSNicolas Pitre 	int ret;
6576b7437aeSNicolas Pitre 
6586b7437aeSNicolas Pitre 	switch (buf[0]) {
6596b7437aeSNicolas Pitre 	case '0':
6606b7437aeSNicolas Pitre 		bL_switcher_disable();
6616b7437aeSNicolas Pitre 		ret = 0;
6626b7437aeSNicolas Pitre 		break;
6636b7437aeSNicolas Pitre 	case '1':
6646b7437aeSNicolas Pitre 		ret = bL_switcher_enable();
6656b7437aeSNicolas Pitre 		break;
6666b7437aeSNicolas Pitre 	default:
6676b7437aeSNicolas Pitre 		ret = -EINVAL;
6686b7437aeSNicolas Pitre 	}
6696b7437aeSNicolas Pitre 
6706b7437aeSNicolas Pitre 	return (ret >= 0) ? count : ret;
6716b7437aeSNicolas Pitre }
6726b7437aeSNicolas Pitre 
6736b7437aeSNicolas Pitre static struct kobj_attribute bL_switcher_active_attr =
6746b7437aeSNicolas Pitre 	__ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
6756b7437aeSNicolas Pitre 
6766b7437aeSNicolas Pitre static struct attribute *bL_switcher_attrs[] = {
6776b7437aeSNicolas Pitre 	&bL_switcher_active_attr.attr,
6786b7437aeSNicolas Pitre 	NULL,
6796b7437aeSNicolas Pitre };
6806b7437aeSNicolas Pitre 
6816b7437aeSNicolas Pitre static struct attribute_group bL_switcher_attr_group = {
6826b7437aeSNicolas Pitre 	.attrs = bL_switcher_attrs,
6836b7437aeSNicolas Pitre };
6846b7437aeSNicolas Pitre 
6856b7437aeSNicolas Pitre static struct kobject *bL_switcher_kobj;
6866b7437aeSNicolas Pitre 
6876b7437aeSNicolas Pitre static int __init bL_switcher_sysfs_init(void)
6886b7437aeSNicolas Pitre {
6896b7437aeSNicolas Pitre 	int ret;
6906b7437aeSNicolas Pitre 
6916b7437aeSNicolas Pitre 	bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
6926b7437aeSNicolas Pitre 	if (!bL_switcher_kobj)
6936b7437aeSNicolas Pitre 		return -ENOMEM;
6946b7437aeSNicolas Pitre 	ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
6956b7437aeSNicolas Pitre 	if (ret)
6966b7437aeSNicolas Pitre 		kobject_put(bL_switcher_kobj);
6976b7437aeSNicolas Pitre 	return ret;
6986b7437aeSNicolas Pitre }
6996b7437aeSNicolas Pitre 
7006b7437aeSNicolas Pitre #endif  /* CONFIG_SYSFS */
7016b7437aeSNicolas Pitre 
702c0f43751SDave Martin bool bL_switcher_get_enabled(void)
703c0f43751SDave Martin {
704c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
705c0f43751SDave Martin 
706c0f43751SDave Martin 	return bL_switcher_active;
707c0f43751SDave Martin }
708c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
709c0f43751SDave Martin 
710c0f43751SDave Martin void bL_switcher_put_enabled(void)
711c0f43751SDave Martin {
712c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
713c0f43751SDave Martin }
714c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
715c0f43751SDave Martin 
71627261435SNicolas Pitre /*
71727261435SNicolas Pitre  * Veto any CPU hotplug operation on those CPUs we've removed
71827261435SNicolas Pitre  * while the switcher is active.
71927261435SNicolas Pitre  * We're just not ready to deal with that given the trickery involved.
72027261435SNicolas Pitre  */
72127261435SNicolas Pitre static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
72227261435SNicolas Pitre 					unsigned long action, void *hcpu)
72327261435SNicolas Pitre {
72427261435SNicolas Pitre 	if (bL_switcher_active) {
72527261435SNicolas Pitre 		int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
72627261435SNicolas Pitre 		switch (action & 0xf) {
72727261435SNicolas Pitre 		case CPU_UP_PREPARE:
72827261435SNicolas Pitre 		case CPU_DOWN_PREPARE:
72927261435SNicolas Pitre 			if (pairing == -1)
73027261435SNicolas Pitre 				return NOTIFY_BAD;
73127261435SNicolas Pitre 		}
73227261435SNicolas Pitre 	}
73327261435SNicolas Pitre 	return NOTIFY_DONE;
73427261435SNicolas Pitre }
73527261435SNicolas Pitre 
736c4821c05SNicolas Pitre static bool no_bL_switcher;
737c4821c05SNicolas Pitre core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
738c4821c05SNicolas Pitre 
7396b7437aeSNicolas Pitre static int __init bL_switcher_init(void)
7406b7437aeSNicolas Pitre {
7416b7437aeSNicolas Pitre 	int ret;
7426b7437aeSNicolas Pitre 
7436b7437aeSNicolas Pitre 	if (MAX_NR_CLUSTERS != 2) {
7446b7437aeSNicolas Pitre 		pr_err("%s: only dual cluster systems are supported\n", __func__);
7456b7437aeSNicolas Pitre 		return -EINVAL;
7466b7437aeSNicolas Pitre 	}
7476b7437aeSNicolas Pitre 
74827261435SNicolas Pitre 	cpu_notifier(bL_switcher_hotplug_callback, 0);
74927261435SNicolas Pitre 
750c4821c05SNicolas Pitre 	if (!no_bL_switcher) {
7516b7437aeSNicolas Pitre 		ret = bL_switcher_enable();
7526b7437aeSNicolas Pitre 		if (ret)
7536b7437aeSNicolas Pitre 			return ret;
754c4821c05SNicolas Pitre 	}
7556b7437aeSNicolas Pitre 
7566b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
7576b7437aeSNicolas Pitre 	ret = bL_switcher_sysfs_init();
7586b7437aeSNicolas Pitre 	if (ret)
7596b7437aeSNicolas Pitre 		pr_err("%s: unable to create sysfs entry\n", __func__);
7606b7437aeSNicolas Pitre #endif
7616b7437aeSNicolas Pitre 
7626b7437aeSNicolas Pitre 	return 0;
7636b7437aeSNicolas Pitre }
7646b7437aeSNicolas Pitre 
76571ce1deeSNicolas Pitre late_initcall(bL_switcher_init);
766