xref: /openbmc/linux/arch/arm/common/bL_switcher.c (revision c0f4375146a738bae23e48fa8b5383abf02177cb)
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 
121c33be57SNicolas Pitre #include <linux/init.h>
131c33be57SNicolas Pitre #include <linux/kernel.h>
141c33be57SNicolas Pitre #include <linux/module.h>
151c33be57SNicolas Pitre #include <linux/sched.h>
161c33be57SNicolas Pitre #include <linux/interrupt.h>
171c33be57SNicolas Pitre #include <linux/cpu_pm.h>
1871ce1deeSNicolas Pitre #include <linux/cpu.h>
193f09d479SLorenzo Pieralisi #include <linux/cpumask.h>
2071ce1deeSNicolas Pitre #include <linux/kthread.h>
2171ce1deeSNicolas Pitre #include <linux/wait.h>
223f09d479SLorenzo Pieralisi #include <linux/clockchips.h>
233f09d479SLorenzo Pieralisi #include <linux/hrtimer.h>
243f09d479SLorenzo Pieralisi #include <linux/tick.h>
251c33be57SNicolas Pitre #include <linux/mm.h>
26*c0f43751SDave Martin #include <linux/mutex.h>
271c33be57SNicolas Pitre #include <linux/string.h>
286b7437aeSNicolas Pitre #include <linux/sysfs.h>
291c33be57SNicolas Pitre #include <linux/irqchip/arm-gic.h>
30c4821c05SNicolas Pitre #include <linux/moduleparam.h>
311c33be57SNicolas Pitre 
321c33be57SNicolas Pitre #include <asm/smp_plat.h>
331c33be57SNicolas Pitre #include <asm/suspend.h>
341c33be57SNicolas Pitre #include <asm/mcpm.h>
351c33be57SNicolas Pitre #include <asm/bL_switcher.h>
361c33be57SNicolas Pitre 
371c33be57SNicolas Pitre 
381c33be57SNicolas Pitre /*
391c33be57SNicolas Pitre  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
401c33be57SNicolas Pitre  * __attribute_const__ and we don't want the compiler to assume any
411c33be57SNicolas Pitre  * constness here as the value _does_ change along some code paths.
421c33be57SNicolas Pitre  */
431c33be57SNicolas Pitre 
441c33be57SNicolas Pitre static int read_mpidr(void)
451c33be57SNicolas Pitre {
461c33be57SNicolas Pitre 	unsigned int id;
471c33be57SNicolas Pitre 	asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
481c33be57SNicolas Pitre 	return id & MPIDR_HWID_BITMASK;
491c33be57SNicolas Pitre }
501c33be57SNicolas Pitre 
511c33be57SNicolas Pitre /*
521c33be57SNicolas Pitre  * bL switcher core code.
531c33be57SNicolas Pitre  */
541c33be57SNicolas Pitre 
551c33be57SNicolas Pitre static void bL_do_switch(void *_unused)
561c33be57SNicolas Pitre {
5738c35d4fSNicolas Pitre 	unsigned ib_mpidr, ib_cpu, ib_cluster;
581c33be57SNicolas Pitre 
591c33be57SNicolas Pitre 	pr_debug("%s\n", __func__);
601c33be57SNicolas Pitre 
6138c35d4fSNicolas Pitre 	ib_mpidr = cpu_logical_map(smp_processor_id());
6238c35d4fSNicolas Pitre 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
6338c35d4fSNicolas Pitre 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
641c33be57SNicolas Pitre 
651c33be57SNicolas Pitre 	/*
661c33be57SNicolas Pitre 	 * Our state has been saved at this point.  Let's release our
671c33be57SNicolas Pitre 	 * inbound CPU.
681c33be57SNicolas Pitre 	 */
6938c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
701c33be57SNicolas Pitre 	sev();
711c33be57SNicolas Pitre 
721c33be57SNicolas Pitre 	/*
731c33be57SNicolas Pitre 	 * From this point, we must assume that our counterpart CPU might
741c33be57SNicolas Pitre 	 * have taken over in its parallel world already, as if execution
751c33be57SNicolas Pitre 	 * just returned from cpu_suspend().  It is therefore important to
761c33be57SNicolas Pitre 	 * be very careful not to make any change the other guy is not
771c33be57SNicolas Pitre 	 * expecting.  This is why we need stack isolation.
781c33be57SNicolas Pitre 	 *
791c33be57SNicolas Pitre 	 * Fancy under cover tasks could be performed here.  For now
801c33be57SNicolas Pitre 	 * we have none.
811c33be57SNicolas Pitre 	 */
821c33be57SNicolas Pitre 
831c33be57SNicolas Pitre 	/* Let's put ourself down. */
841c33be57SNicolas Pitre 	mcpm_cpu_power_down();
851c33be57SNicolas Pitre 
861c33be57SNicolas Pitre 	/* should never get here */
871c33be57SNicolas Pitre 	BUG();
881c33be57SNicolas Pitre }
891c33be57SNicolas Pitre 
901c33be57SNicolas Pitre /*
91c052de26SNicolas Pitre  * Stack isolation.  To ensure 'current' remains valid, we just use another
92c052de26SNicolas Pitre  * piece of our thread's stack space which should be fairly lightly used.
93c052de26SNicolas Pitre  * The selected area starts just above the thread_info structure located
94c052de26SNicolas Pitre  * at the very bottom of the stack, aligned to a cache line, and indexed
95c052de26SNicolas Pitre  * with the cluster number.
961c33be57SNicolas Pitre  */
97c052de26SNicolas Pitre #define STACK_SIZE 512
981c33be57SNicolas Pitre extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
991c33be57SNicolas Pitre static int bL_switchpoint(unsigned long _arg)
1001c33be57SNicolas Pitre {
1011c33be57SNicolas Pitre 	unsigned int mpidr = read_mpidr();
1021c33be57SNicolas Pitre 	unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
103c052de26SNicolas Pitre 	void *stack = current_thread_info() + 1;
1041c33be57SNicolas Pitre 	stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
105c052de26SNicolas Pitre 	stack += clusterid * STACK_SIZE + STACK_SIZE;
1061c33be57SNicolas Pitre 	call_with_stack(bL_do_switch, (void *)_arg, stack);
1071c33be57SNicolas Pitre 	BUG();
1081c33be57SNicolas Pitre }
1091c33be57SNicolas Pitre 
1101c33be57SNicolas Pitre /*
1111c33be57SNicolas Pitre  * Generic switcher interface
1121c33be57SNicolas Pitre  */
1131c33be57SNicolas Pitre 
114ed96762eSNicolas Pitre static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
11538c35d4fSNicolas Pitre static int bL_switcher_cpu_pairing[NR_CPUS];
116ed96762eSNicolas Pitre 
1171c33be57SNicolas Pitre /*
1181c33be57SNicolas Pitre  * bL_switch_to - Switch to a specific cluster for the current CPU
1191c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
1201c33be57SNicolas Pitre  *
1211c33be57SNicolas Pitre  * This function must be called on the CPU to be switched.
1221c33be57SNicolas Pitre  * Returns 0 on success, else a negative status code.
1231c33be57SNicolas Pitre  */
1241c33be57SNicolas Pitre static int bL_switch_to(unsigned int new_cluster_id)
1251c33be57SNicolas Pitre {
12638c35d4fSNicolas Pitre 	unsigned int mpidr, this_cpu, that_cpu;
12738c35d4fSNicolas Pitre 	unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
1283f09d479SLorenzo Pieralisi 	struct tick_device *tdev;
1293f09d479SLorenzo Pieralisi 	enum clock_event_mode tdev_mode;
1301c33be57SNicolas Pitre 	int ret;
1311c33be57SNicolas Pitre 
13238c35d4fSNicolas Pitre 	this_cpu = smp_processor_id();
13338c35d4fSNicolas Pitre 	ob_mpidr = read_mpidr();
13438c35d4fSNicolas Pitre 	ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
13538c35d4fSNicolas Pitre 	ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
13638c35d4fSNicolas Pitre 	BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
1371c33be57SNicolas Pitre 
13838c35d4fSNicolas Pitre 	if (new_cluster_id == ob_cluster)
1391c33be57SNicolas Pitre 		return 0;
1401c33be57SNicolas Pitre 
14138c35d4fSNicolas Pitre 	that_cpu = bL_switcher_cpu_pairing[this_cpu];
14238c35d4fSNicolas Pitre 	ib_mpidr = cpu_logical_map(that_cpu);
14338c35d4fSNicolas Pitre 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
14438c35d4fSNicolas Pitre 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
14538c35d4fSNicolas Pitre 
14638c35d4fSNicolas Pitre 	pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
14738c35d4fSNicolas Pitre 		 this_cpu, ob_mpidr, ib_mpidr);
1481c33be57SNicolas Pitre 
1491c33be57SNicolas Pitre 	/* Close the gate for our entry vectors */
15038c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
15138c35d4fSNicolas Pitre 	mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
1521c33be57SNicolas Pitre 
1531c33be57SNicolas Pitre 	/*
1541c33be57SNicolas Pitre 	 * Let's wake up the inbound CPU now in case it requires some delay
1551c33be57SNicolas Pitre 	 * to come online, but leave it gated in our entry vector code.
1561c33be57SNicolas Pitre 	 */
15738c35d4fSNicolas Pitre 	ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
1581c33be57SNicolas Pitre 	if (ret) {
1591c33be57SNicolas Pitre 		pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
1601c33be57SNicolas Pitre 		return ret;
1611c33be57SNicolas Pitre 	}
1621c33be57SNicolas Pitre 
1631c33be57SNicolas Pitre 	/*
1641c33be57SNicolas Pitre 	 * From this point we are entering the switch critical zone
1651c33be57SNicolas Pitre 	 * and can't take any interrupts anymore.
1661c33be57SNicolas Pitre 	 */
1671c33be57SNicolas Pitre 	local_irq_disable();
1681c33be57SNicolas Pitre 	local_fiq_disable();
1691c33be57SNicolas Pitre 
1701c33be57SNicolas Pitre 	/* redirect GIC's SGIs to our counterpart */
17138c35d4fSNicolas Pitre 	gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
1721c33be57SNicolas Pitre 
1731c33be57SNicolas Pitre 	/*
1741c33be57SNicolas Pitre 	 * Raise a SGI on the inbound CPU to make sure it doesn't stall
1751c33be57SNicolas Pitre 	 * in a possible WFI, such as in mcpm_power_down().
1761c33be57SNicolas Pitre 	 */
1771c33be57SNicolas Pitre 	arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
1781c33be57SNicolas Pitre 
1793f09d479SLorenzo Pieralisi 	tdev = tick_get_device(this_cpu);
1803f09d479SLorenzo Pieralisi 	if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
1813f09d479SLorenzo Pieralisi 		tdev = NULL;
1823f09d479SLorenzo Pieralisi 	if (tdev) {
1833f09d479SLorenzo Pieralisi 		tdev_mode = tdev->evtdev->mode;
1843f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
1853f09d479SLorenzo Pieralisi 	}
1863f09d479SLorenzo Pieralisi 
1871c33be57SNicolas Pitre 	ret = cpu_pm_enter();
1881c33be57SNicolas Pitre 
1891c33be57SNicolas Pitre 	/* we can not tolerate errors at this point */
1901c33be57SNicolas Pitre 	if (ret)
1911c33be57SNicolas Pitre 		panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
1921c33be57SNicolas Pitre 
19338c35d4fSNicolas Pitre 	/* Swap the physical CPUs in the logical map for this logical CPU. */
19438c35d4fSNicolas Pitre 	cpu_logical_map(this_cpu) = ib_mpidr;
19538c35d4fSNicolas Pitre 	cpu_logical_map(that_cpu) = ob_mpidr;
1961c33be57SNicolas Pitre 
1971c33be57SNicolas Pitre 	/* Let's do the actual CPU switch. */
1981c33be57SNicolas Pitre 	ret = cpu_suspend(0, bL_switchpoint);
1991c33be57SNicolas Pitre 	if (ret > 0)
2001c33be57SNicolas Pitre 		panic("%s: cpu_suspend() returned %d\n", __func__, ret);
2011c33be57SNicolas Pitre 
2021c33be57SNicolas Pitre 	/* We are executing on the inbound CPU at this point */
2031c33be57SNicolas Pitre 	mpidr = read_mpidr();
20438c35d4fSNicolas Pitre 	pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
20538c35d4fSNicolas Pitre 	BUG_ON(mpidr != ib_mpidr);
2061c33be57SNicolas Pitre 
2071c33be57SNicolas Pitre 	mcpm_cpu_powered_up();
2081c33be57SNicolas Pitre 
2091c33be57SNicolas Pitre 	ret = cpu_pm_exit();
2101c33be57SNicolas Pitre 
2113f09d479SLorenzo Pieralisi 	if (tdev) {
2123f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, tdev_mode);
2133f09d479SLorenzo Pieralisi 		clockevents_program_event(tdev->evtdev,
2143f09d479SLorenzo Pieralisi 					  tdev->evtdev->next_event, 1);
2153f09d479SLorenzo Pieralisi 	}
2163f09d479SLorenzo Pieralisi 
2171c33be57SNicolas Pitre 	local_fiq_enable();
2181c33be57SNicolas Pitre 	local_irq_enable();
2191c33be57SNicolas Pitre 
2201c33be57SNicolas Pitre 	if (ret)
2211c33be57SNicolas Pitre 		pr_err("%s exiting with error %d\n", __func__, ret);
2221c33be57SNicolas Pitre 	return ret;
2231c33be57SNicolas Pitre }
2241c33be57SNicolas Pitre 
22571ce1deeSNicolas Pitre struct bL_thread {
22671ce1deeSNicolas Pitre 	struct task_struct *task;
22771ce1deeSNicolas Pitre 	wait_queue_head_t wq;
22871ce1deeSNicolas Pitre 	int wanted_cluster;
2296b7437aeSNicolas Pitre 	struct completion started;
2301c33be57SNicolas Pitre };
2311c33be57SNicolas Pitre 
23271ce1deeSNicolas Pitre static struct bL_thread bL_threads[NR_CPUS];
23371ce1deeSNicolas Pitre 
23471ce1deeSNicolas Pitre static int bL_switcher_thread(void *arg)
2351c33be57SNicolas Pitre {
23671ce1deeSNicolas Pitre 	struct bL_thread *t = arg;
23771ce1deeSNicolas Pitre 	struct sched_param param = { .sched_priority = 1 };
23871ce1deeSNicolas Pitre 	int cluster;
23971ce1deeSNicolas Pitre 
24071ce1deeSNicolas Pitre 	sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
2416b7437aeSNicolas Pitre 	complete(&t->started);
24271ce1deeSNicolas Pitre 
24371ce1deeSNicolas Pitre 	do {
24471ce1deeSNicolas Pitre 		if (signal_pending(current))
24571ce1deeSNicolas Pitre 			flush_signals(current);
24671ce1deeSNicolas Pitre 		wait_event_interruptible(t->wq,
24771ce1deeSNicolas Pitre 				t->wanted_cluster != -1 ||
24871ce1deeSNicolas Pitre 				kthread_should_stop());
24971ce1deeSNicolas Pitre 		cluster = xchg(&t->wanted_cluster, -1);
25071ce1deeSNicolas Pitre 		if (cluster != -1)
25171ce1deeSNicolas Pitre 			bL_switch_to(cluster);
25271ce1deeSNicolas Pitre 	} while (!kthread_should_stop());
25371ce1deeSNicolas Pitre 
25471ce1deeSNicolas Pitre 	return 0;
25571ce1deeSNicolas Pitre }
25671ce1deeSNicolas Pitre 
2576b7437aeSNicolas Pitre static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
25871ce1deeSNicolas Pitre {
25971ce1deeSNicolas Pitre 	struct task_struct *task;
26071ce1deeSNicolas Pitre 
26171ce1deeSNicolas Pitre 	task = kthread_create_on_node(bL_switcher_thread, arg,
26271ce1deeSNicolas Pitre 				      cpu_to_node(cpu), "kswitcher_%d", cpu);
26371ce1deeSNicolas Pitre 	if (!IS_ERR(task)) {
26471ce1deeSNicolas Pitre 		kthread_bind(task, cpu);
26571ce1deeSNicolas Pitre 		wake_up_process(task);
26671ce1deeSNicolas Pitre 	} else
26771ce1deeSNicolas Pitre 		pr_err("%s failed for CPU %d\n", __func__, cpu);
26871ce1deeSNicolas Pitre 	return task;
2691c33be57SNicolas Pitre }
2701c33be57SNicolas Pitre 
2711c33be57SNicolas Pitre /*
2721c33be57SNicolas Pitre  * bL_switch_request - Switch to a specific cluster for the given CPU
2731c33be57SNicolas Pitre  *
2741c33be57SNicolas Pitre  * @cpu: the CPU to switch
2751c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
2761c33be57SNicolas Pitre  *
27771ce1deeSNicolas Pitre  * This function causes a cluster switch on the given CPU by waking up
27871ce1deeSNicolas Pitre  * the appropriate switcher thread.  This function may or may not return
27971ce1deeSNicolas Pitre  * before the switch has occurred.
2801c33be57SNicolas Pitre  */
28171ce1deeSNicolas Pitre int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
2821c33be57SNicolas Pitre {
28371ce1deeSNicolas Pitre 	struct bL_thread *t;
2841c33be57SNicolas Pitre 
28571ce1deeSNicolas Pitre 	if (cpu >= ARRAY_SIZE(bL_threads)) {
28671ce1deeSNicolas Pitre 		pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
28771ce1deeSNicolas Pitre 		return -EINVAL;
2881c33be57SNicolas Pitre 	}
2891c33be57SNicolas Pitre 
29071ce1deeSNicolas Pitre 	t = &bL_threads[cpu];
29171ce1deeSNicolas Pitre 	if (IS_ERR(t->task))
29271ce1deeSNicolas Pitre 		return PTR_ERR(t->task);
29371ce1deeSNicolas Pitre 	if (!t->task)
29471ce1deeSNicolas Pitre 		return -ESRCH;
29571ce1deeSNicolas Pitre 
29671ce1deeSNicolas Pitre 	t->wanted_cluster = new_cluster_id;
29771ce1deeSNicolas Pitre 	wake_up(&t->wq);
29871ce1deeSNicolas Pitre 	return 0;
2991c33be57SNicolas Pitre }
3001c33be57SNicolas Pitre EXPORT_SYMBOL_GPL(bL_switch_request);
30171ce1deeSNicolas Pitre 
3029797a0e9SNicolas Pitre /*
3039797a0e9SNicolas Pitre  * Activation and configuration code.
3049797a0e9SNicolas Pitre  */
3059797a0e9SNicolas Pitre 
306*c0f43751SDave Martin static DEFINE_MUTEX(bL_switcher_activation_lock);
3076b7437aeSNicolas Pitre static unsigned int bL_switcher_active;
30838c35d4fSNicolas Pitre static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
3099797a0e9SNicolas Pitre static cpumask_t bL_switcher_removed_logical_cpus;
3109797a0e9SNicolas Pitre 
3116b7437aeSNicolas Pitre static void bL_switcher_restore_cpus(void)
3129797a0e9SNicolas Pitre {
3139797a0e9SNicolas Pitre 	int i;
3149797a0e9SNicolas Pitre 
3159797a0e9SNicolas Pitre 	for_each_cpu(i, &bL_switcher_removed_logical_cpus)
3169797a0e9SNicolas Pitre 		cpu_up(i);
3179797a0e9SNicolas Pitre }
3189797a0e9SNicolas Pitre 
3196b7437aeSNicolas Pitre static int bL_switcher_halve_cpus(void)
3209797a0e9SNicolas Pitre {
32138c35d4fSNicolas Pitre 	int i, j, cluster_0, gic_id, ret;
32238c35d4fSNicolas Pitre 	unsigned int cpu, cluster, mask;
32338c35d4fSNicolas Pitre 	cpumask_t available_cpus;
3249797a0e9SNicolas Pitre 
32538c35d4fSNicolas Pitre 	/* First pass to validate what we have */
32638c35d4fSNicolas Pitre 	mask = 0;
3279797a0e9SNicolas Pitre 	for_each_online_cpu(i) {
32838c35d4fSNicolas Pitre 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
32938c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
3309797a0e9SNicolas Pitre 		if (cluster >= 2) {
3319797a0e9SNicolas Pitre 			pr_err("%s: only dual cluster systems are supported\n", __func__);
3329797a0e9SNicolas Pitre 			return -EINVAL;
3339797a0e9SNicolas Pitre 		}
33438c35d4fSNicolas Pitre 		if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
33538c35d4fSNicolas Pitre 			return -EINVAL;
33638c35d4fSNicolas Pitre 		mask |= (1 << cluster);
3379797a0e9SNicolas Pitre 	}
33838c35d4fSNicolas Pitre 	if (mask != 3) {
33938c35d4fSNicolas Pitre 		pr_err("%s: no CPU pairing possible\n", __func__);
3409797a0e9SNicolas Pitre 		return -EINVAL;
3419797a0e9SNicolas Pitre 	}
3429797a0e9SNicolas Pitre 
34338c35d4fSNicolas Pitre 	/*
34438c35d4fSNicolas Pitre 	 * Now let's do the pairing.  We match each CPU with another CPU
34538c35d4fSNicolas Pitre 	 * from a different cluster.  To get a uniform scheduling behavior
34638c35d4fSNicolas Pitre 	 * without fiddling with CPU topology and compute capacity data,
34738c35d4fSNicolas Pitre 	 * we'll use logical CPUs initially belonging to the same cluster.
34838c35d4fSNicolas Pitre 	 */
34938c35d4fSNicolas Pitre 	memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
35038c35d4fSNicolas Pitre 	cpumask_copy(&available_cpus, cpu_online_mask);
35138c35d4fSNicolas Pitre 	cluster_0 = -1;
35238c35d4fSNicolas Pitre 	for_each_cpu(i, &available_cpus) {
35338c35d4fSNicolas Pitre 		int match = -1;
35438c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
35538c35d4fSNicolas Pitre 		if (cluster_0 == -1)
35638c35d4fSNicolas Pitre 			cluster_0 = cluster;
35738c35d4fSNicolas Pitre 		if (cluster != cluster_0)
35838c35d4fSNicolas Pitre 			continue;
35938c35d4fSNicolas Pitre 		cpumask_clear_cpu(i, &available_cpus);
36038c35d4fSNicolas Pitre 		for_each_cpu(j, &available_cpus) {
36138c35d4fSNicolas Pitre 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
36238c35d4fSNicolas Pitre 			/*
36338c35d4fSNicolas Pitre 			 * Let's remember the last match to create "odd"
36438c35d4fSNicolas Pitre 			 * pairings on purpose in order for other code not
36538c35d4fSNicolas Pitre 			 * to assume any relation between physical and
36638c35d4fSNicolas Pitre 			 * logical CPU numbers.
36738c35d4fSNicolas Pitre 			 */
36838c35d4fSNicolas Pitre 			if (cluster != cluster_0)
36938c35d4fSNicolas Pitre 				match = j;
37038c35d4fSNicolas Pitre 		}
37138c35d4fSNicolas Pitre 		if (match != -1) {
37238c35d4fSNicolas Pitre 			bL_switcher_cpu_pairing[i] = match;
37338c35d4fSNicolas Pitre 			cpumask_clear_cpu(match, &available_cpus);
37438c35d4fSNicolas Pitre 			pr_info("CPU%d paired with CPU%d\n", i, match);
37538c35d4fSNicolas Pitre 		}
37638c35d4fSNicolas Pitre 	}
3779797a0e9SNicolas Pitre 
37838c35d4fSNicolas Pitre 	/*
37938c35d4fSNicolas Pitre 	 * Now we disable the unwanted CPUs i.e. everything that has no
38038c35d4fSNicolas Pitre 	 * pairing information (that includes the pairing counterparts).
38138c35d4fSNicolas Pitre 	 */
38238c35d4fSNicolas Pitre 	cpumask_clear(&bL_switcher_removed_logical_cpus);
38338c35d4fSNicolas Pitre 	for_each_online_cpu(i) {
38438c35d4fSNicolas Pitre 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
38538c35d4fSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
38638c35d4fSNicolas Pitre 
387ed96762eSNicolas Pitre 		/* Let's take note of the GIC ID for this CPU */
38838c35d4fSNicolas Pitre 		gic_id = gic_get_cpu_id(i);
389ed96762eSNicolas Pitre 		if (gic_id < 0) {
390ed96762eSNicolas Pitre 			pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
39138c35d4fSNicolas Pitre 			bL_switcher_restore_cpus();
392ed96762eSNicolas Pitre 			return -EINVAL;
393ed96762eSNicolas Pitre 		}
394ed96762eSNicolas Pitre 		bL_gic_id[cpu][cluster] = gic_id;
395ed96762eSNicolas Pitre 		pr_info("GIC ID for CPU %u cluster %u is %u\n",
396ed96762eSNicolas Pitre 			cpu, cluster, gic_id);
397ed96762eSNicolas Pitre 
39838c35d4fSNicolas Pitre 		if (bL_switcher_cpu_pairing[i] != -1) {
39938c35d4fSNicolas Pitre 			bL_switcher_cpu_original_cluster[i] = cluster;
4009797a0e9SNicolas Pitre 			continue;
4019797a0e9SNicolas Pitre 		}
4029797a0e9SNicolas Pitre 
4039797a0e9SNicolas Pitre 		ret = cpu_down(i);
4049797a0e9SNicolas Pitre 		if (ret) {
4059797a0e9SNicolas Pitre 			bL_switcher_restore_cpus();
4069797a0e9SNicolas Pitre 			return ret;
4079797a0e9SNicolas Pitre 		}
4089797a0e9SNicolas Pitre 		cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
4099797a0e9SNicolas Pitre 	}
4109797a0e9SNicolas Pitre 
4119797a0e9SNicolas Pitre 	return 0;
4129797a0e9SNicolas Pitre }
4139797a0e9SNicolas Pitre 
4146b7437aeSNicolas Pitre static int bL_switcher_enable(void)
41571ce1deeSNicolas Pitre {
4169797a0e9SNicolas Pitre 	int cpu, ret;
41771ce1deeSNicolas Pitre 
418*c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
4196b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
4206b7437aeSNicolas Pitre 	if (bL_switcher_active) {
4216b7437aeSNicolas Pitre 		cpu_hotplug_driver_unlock();
422*c0f43751SDave Martin 		mutex_unlock(&bL_switcher_activation_lock);
4236b7437aeSNicolas Pitre 		return 0;
4249797a0e9SNicolas Pitre 	}
4259797a0e9SNicolas Pitre 
4266b7437aeSNicolas Pitre 	pr_info("big.LITTLE switcher initializing\n");
4276b7437aeSNicolas Pitre 
4289797a0e9SNicolas Pitre 	ret = bL_switcher_halve_cpus();
4299797a0e9SNicolas Pitre 	if (ret) {
4309797a0e9SNicolas Pitre 		cpu_hotplug_driver_unlock();
431*c0f43751SDave Martin 		mutex_unlock(&bL_switcher_activation_lock);
4329797a0e9SNicolas Pitre 		return ret;
4339797a0e9SNicolas Pitre 	}
4349797a0e9SNicolas Pitre 
43571ce1deeSNicolas Pitre 	for_each_online_cpu(cpu) {
43671ce1deeSNicolas Pitre 		struct bL_thread *t = &bL_threads[cpu];
43771ce1deeSNicolas Pitre 		init_waitqueue_head(&t->wq);
4386b7437aeSNicolas Pitre 		init_completion(&t->started);
43971ce1deeSNicolas Pitre 		t->wanted_cluster = -1;
44071ce1deeSNicolas Pitre 		t->task = bL_switcher_thread_create(cpu, t);
44171ce1deeSNicolas Pitre 	}
4426b7437aeSNicolas Pitre 
4436b7437aeSNicolas Pitre 	bL_switcher_active = 1;
44471ce1deeSNicolas Pitre 	pr_info("big.LITTLE switcher initialized\n");
445*c0f43751SDave Martin 
446*c0f43751SDave Martin 	cpu_hotplug_driver_unlock();
447*c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
44871ce1deeSNicolas Pitre 	return 0;
44971ce1deeSNicolas Pitre }
45071ce1deeSNicolas Pitre 
4516b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
4526b7437aeSNicolas Pitre 
4536b7437aeSNicolas Pitre static void bL_switcher_disable(void)
4546b7437aeSNicolas Pitre {
45538c35d4fSNicolas Pitre 	unsigned int cpu, cluster;
4566b7437aeSNicolas Pitre 	struct bL_thread *t;
4576b7437aeSNicolas Pitre 	struct task_struct *task;
4586b7437aeSNicolas Pitre 
459*c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
4606b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
4616b7437aeSNicolas Pitre 	if (!bL_switcher_active) {
4626b7437aeSNicolas Pitre 		cpu_hotplug_driver_unlock();
463*c0f43751SDave Martin 		mutex_unlock(&bL_switcher_activation_lock);
4646b7437aeSNicolas Pitre 		return;
4656b7437aeSNicolas Pitre 	}
4666b7437aeSNicolas Pitre 	bL_switcher_active = 0;
4676b7437aeSNicolas Pitre 
4686b7437aeSNicolas Pitre 	/*
4696b7437aeSNicolas Pitre 	 * To deactivate the switcher, we must shut down the switcher
4706b7437aeSNicolas Pitre 	 * threads to prevent any other requests from being accepted.
4716b7437aeSNicolas Pitre 	 * Then, if the final cluster for given logical CPU is not the
4726b7437aeSNicolas Pitre 	 * same as the original one, we'll recreate a switcher thread
4736b7437aeSNicolas Pitre 	 * just for the purpose of switching the CPU back without any
4746b7437aeSNicolas Pitre 	 * possibility for interference from external requests.
4756b7437aeSNicolas Pitre 	 */
4766b7437aeSNicolas Pitre 	for_each_online_cpu(cpu) {
4776b7437aeSNicolas Pitre 		t = &bL_threads[cpu];
4786b7437aeSNicolas Pitre 		task = t->task;
4796b7437aeSNicolas Pitre 		t->task = NULL;
4806b7437aeSNicolas Pitre 		if (!task || IS_ERR(task))
4816b7437aeSNicolas Pitre 			continue;
4826b7437aeSNicolas Pitre 		kthread_stop(task);
4836b7437aeSNicolas Pitre 		/* no more switch may happen on this CPU at this point */
4846b7437aeSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
4856b7437aeSNicolas Pitre 		if (cluster == bL_switcher_cpu_original_cluster[cpu])
4866b7437aeSNicolas Pitre 			continue;
4876b7437aeSNicolas Pitre 		init_completion(&t->started);
4886b7437aeSNicolas Pitre 		t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
4896b7437aeSNicolas Pitre 		task = bL_switcher_thread_create(cpu, t);
4906b7437aeSNicolas Pitre 		if (!IS_ERR(task)) {
4916b7437aeSNicolas Pitre 			wait_for_completion(&t->started);
4926b7437aeSNicolas Pitre 			kthread_stop(task);
4936b7437aeSNicolas Pitre 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
4946b7437aeSNicolas Pitre 			if (cluster == bL_switcher_cpu_original_cluster[cpu])
4956b7437aeSNicolas Pitre 				continue;
4966b7437aeSNicolas Pitre 		}
4976b7437aeSNicolas Pitre 		/* If execution gets here, we're in trouble. */
4986b7437aeSNicolas Pitre 		pr_crit("%s: unable to restore original cluster for CPU %d\n",
4996b7437aeSNicolas Pitre 			__func__, cpu);
5006b7437aeSNicolas Pitre 		pr_crit("%s: CPU %d can't be restored\n",
50138c35d4fSNicolas Pitre 			__func__, bL_switcher_cpu_pairing[cpu]);
50238c35d4fSNicolas Pitre 		cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
50338c35d4fSNicolas Pitre 				  &bL_switcher_removed_logical_cpus);
5046b7437aeSNicolas Pitre 	}
5056b7437aeSNicolas Pitre 
5066b7437aeSNicolas Pitre 	bL_switcher_restore_cpus();
5076b7437aeSNicolas Pitre 	cpu_hotplug_driver_unlock();
508*c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
5096b7437aeSNicolas Pitre }
5106b7437aeSNicolas Pitre 
5116b7437aeSNicolas Pitre static ssize_t bL_switcher_active_show(struct kobject *kobj,
5126b7437aeSNicolas Pitre 		struct kobj_attribute *attr, char *buf)
5136b7437aeSNicolas Pitre {
5146b7437aeSNicolas Pitre 	return sprintf(buf, "%u\n", bL_switcher_active);
5156b7437aeSNicolas Pitre }
5166b7437aeSNicolas Pitre 
5176b7437aeSNicolas Pitre static ssize_t bL_switcher_active_store(struct kobject *kobj,
5186b7437aeSNicolas Pitre 		struct kobj_attribute *attr, const char *buf, size_t count)
5196b7437aeSNicolas Pitre {
5206b7437aeSNicolas Pitre 	int ret;
5216b7437aeSNicolas Pitre 
5226b7437aeSNicolas Pitre 	switch (buf[0]) {
5236b7437aeSNicolas Pitre 	case '0':
5246b7437aeSNicolas Pitre 		bL_switcher_disable();
5256b7437aeSNicolas Pitre 		ret = 0;
5266b7437aeSNicolas Pitre 		break;
5276b7437aeSNicolas Pitre 	case '1':
5286b7437aeSNicolas Pitre 		ret = bL_switcher_enable();
5296b7437aeSNicolas Pitre 		break;
5306b7437aeSNicolas Pitre 	default:
5316b7437aeSNicolas Pitre 		ret = -EINVAL;
5326b7437aeSNicolas Pitre 	}
5336b7437aeSNicolas Pitre 
5346b7437aeSNicolas Pitre 	return (ret >= 0) ? count : ret;
5356b7437aeSNicolas Pitre }
5366b7437aeSNicolas Pitre 
5376b7437aeSNicolas Pitre static struct kobj_attribute bL_switcher_active_attr =
5386b7437aeSNicolas Pitre 	__ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
5396b7437aeSNicolas Pitre 
5406b7437aeSNicolas Pitre static struct attribute *bL_switcher_attrs[] = {
5416b7437aeSNicolas Pitre 	&bL_switcher_active_attr.attr,
5426b7437aeSNicolas Pitre 	NULL,
5436b7437aeSNicolas Pitre };
5446b7437aeSNicolas Pitre 
5456b7437aeSNicolas Pitre static struct attribute_group bL_switcher_attr_group = {
5466b7437aeSNicolas Pitre 	.attrs = bL_switcher_attrs,
5476b7437aeSNicolas Pitre };
5486b7437aeSNicolas Pitre 
5496b7437aeSNicolas Pitre static struct kobject *bL_switcher_kobj;
5506b7437aeSNicolas Pitre 
5516b7437aeSNicolas Pitre static int __init bL_switcher_sysfs_init(void)
5526b7437aeSNicolas Pitre {
5536b7437aeSNicolas Pitre 	int ret;
5546b7437aeSNicolas Pitre 
5556b7437aeSNicolas Pitre 	bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
5566b7437aeSNicolas Pitre 	if (!bL_switcher_kobj)
5576b7437aeSNicolas Pitre 		return -ENOMEM;
5586b7437aeSNicolas Pitre 	ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
5596b7437aeSNicolas Pitre 	if (ret)
5606b7437aeSNicolas Pitre 		kobject_put(bL_switcher_kobj);
5616b7437aeSNicolas Pitre 	return ret;
5626b7437aeSNicolas Pitre }
5636b7437aeSNicolas Pitre 
5646b7437aeSNicolas Pitre #endif  /* CONFIG_SYSFS */
5656b7437aeSNicolas Pitre 
566*c0f43751SDave Martin bool bL_switcher_get_enabled(void)
567*c0f43751SDave Martin {
568*c0f43751SDave Martin 	mutex_lock(&bL_switcher_activation_lock);
569*c0f43751SDave Martin 
570*c0f43751SDave Martin 	return bL_switcher_active;
571*c0f43751SDave Martin }
572*c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
573*c0f43751SDave Martin 
574*c0f43751SDave Martin void bL_switcher_put_enabled(void)
575*c0f43751SDave Martin {
576*c0f43751SDave Martin 	mutex_unlock(&bL_switcher_activation_lock);
577*c0f43751SDave Martin }
578*c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
579*c0f43751SDave Martin 
58027261435SNicolas Pitre /*
58127261435SNicolas Pitre  * Veto any CPU hotplug operation on those CPUs we've removed
58227261435SNicolas Pitre  * while the switcher is active.
58327261435SNicolas Pitre  * We're just not ready to deal with that given the trickery involved.
58427261435SNicolas Pitre  */
58527261435SNicolas Pitre static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
58627261435SNicolas Pitre 					unsigned long action, void *hcpu)
58727261435SNicolas Pitre {
58827261435SNicolas Pitre 	if (bL_switcher_active) {
58927261435SNicolas Pitre 		int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
59027261435SNicolas Pitre 		switch (action & 0xf) {
59127261435SNicolas Pitre 		case CPU_UP_PREPARE:
59227261435SNicolas Pitre 		case CPU_DOWN_PREPARE:
59327261435SNicolas Pitre 			if (pairing == -1)
59427261435SNicolas Pitre 				return NOTIFY_BAD;
59527261435SNicolas Pitre 		}
59627261435SNicolas Pitre 	}
59727261435SNicolas Pitre 	return NOTIFY_DONE;
59827261435SNicolas Pitre }
59927261435SNicolas Pitre 
600c4821c05SNicolas Pitre static bool no_bL_switcher;
601c4821c05SNicolas Pitre core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
602c4821c05SNicolas Pitre 
6036b7437aeSNicolas Pitre static int __init bL_switcher_init(void)
6046b7437aeSNicolas Pitre {
6056b7437aeSNicolas Pitre 	int ret;
6066b7437aeSNicolas Pitre 
6076b7437aeSNicolas Pitre 	if (MAX_NR_CLUSTERS != 2) {
6086b7437aeSNicolas Pitre 		pr_err("%s: only dual cluster systems are supported\n", __func__);
6096b7437aeSNicolas Pitre 		return -EINVAL;
6106b7437aeSNicolas Pitre 	}
6116b7437aeSNicolas Pitre 
61227261435SNicolas Pitre 	cpu_notifier(bL_switcher_hotplug_callback, 0);
61327261435SNicolas Pitre 
614c4821c05SNicolas Pitre 	if (!no_bL_switcher) {
6156b7437aeSNicolas Pitre 		ret = bL_switcher_enable();
6166b7437aeSNicolas Pitre 		if (ret)
6176b7437aeSNicolas Pitre 			return ret;
618c4821c05SNicolas Pitre 	}
6196b7437aeSNicolas Pitre 
6206b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
6216b7437aeSNicolas Pitre 	ret = bL_switcher_sysfs_init();
6226b7437aeSNicolas Pitre 	if (ret)
6236b7437aeSNicolas Pitre 		pr_err("%s: unable to create sysfs entry\n", __func__);
6246b7437aeSNicolas Pitre #endif
6256b7437aeSNicolas Pitre 
6266b7437aeSNicolas Pitre 	return 0;
6276b7437aeSNicolas Pitre }
6286b7437aeSNicolas Pitre 
62971ce1deeSNicolas Pitre late_initcall(bL_switcher_init);
630