xref: /openbmc/linux/arch/arm/common/bL_switcher.c (revision 6b7437aed1568076cefa4d42747b1515dcb848db)
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>
261c33be57SNicolas Pitre #include <linux/string.h>
27*6b7437aeSNicolas Pitre #include <linux/sysfs.h>
281c33be57SNicolas Pitre #include <linux/irqchip/arm-gic.h>
291c33be57SNicolas Pitre 
301c33be57SNicolas Pitre #include <asm/smp_plat.h>
311c33be57SNicolas Pitre #include <asm/suspend.h>
321c33be57SNicolas Pitre #include <asm/mcpm.h>
331c33be57SNicolas Pitre #include <asm/bL_switcher.h>
341c33be57SNicolas Pitre 
351c33be57SNicolas Pitre 
361c33be57SNicolas Pitre /*
371c33be57SNicolas Pitre  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
381c33be57SNicolas Pitre  * __attribute_const__ and we don't want the compiler to assume any
391c33be57SNicolas Pitre  * constness here as the value _does_ change along some code paths.
401c33be57SNicolas Pitre  */
411c33be57SNicolas Pitre 
421c33be57SNicolas Pitre static int read_mpidr(void)
431c33be57SNicolas Pitre {
441c33be57SNicolas Pitre 	unsigned int id;
451c33be57SNicolas Pitre 	asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
461c33be57SNicolas Pitre 	return id & MPIDR_HWID_BITMASK;
471c33be57SNicolas Pitre }
481c33be57SNicolas Pitre 
491c33be57SNicolas Pitre /*
501c33be57SNicolas Pitre  * bL switcher core code.
511c33be57SNicolas Pitre  */
521c33be57SNicolas Pitre 
531c33be57SNicolas Pitre static void bL_do_switch(void *_unused)
541c33be57SNicolas Pitre {
551c33be57SNicolas Pitre 	unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
561c33be57SNicolas Pitre 
571c33be57SNicolas Pitre 	pr_debug("%s\n", __func__);
581c33be57SNicolas Pitre 
591c33be57SNicolas Pitre 	mpidr = read_mpidr();
601c33be57SNicolas Pitre 	cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
611c33be57SNicolas Pitre 	clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
621c33be57SNicolas Pitre 	ob_cluster = clusterid;
631c33be57SNicolas Pitre 	ib_cluster = clusterid ^ 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 	 */
691c33be57SNicolas Pitre 	mcpm_set_entry_vector(cpuid, 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];
115ed96762eSNicolas Pitre 
1161c33be57SNicolas Pitre /*
1171c33be57SNicolas Pitre  * bL_switch_to - Switch to a specific cluster for the current CPU
1181c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
1191c33be57SNicolas Pitre  *
1201c33be57SNicolas Pitre  * This function must be called on the CPU to be switched.
1211c33be57SNicolas Pitre  * Returns 0 on success, else a negative status code.
1221c33be57SNicolas Pitre  */
1231c33be57SNicolas Pitre static int bL_switch_to(unsigned int new_cluster_id)
1241c33be57SNicolas Pitre {
1251c33be57SNicolas Pitre 	unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
1263f09d479SLorenzo Pieralisi 	struct tick_device *tdev;
1273f09d479SLorenzo Pieralisi 	enum clock_event_mode tdev_mode;
1281c33be57SNicolas Pitre 	int ret;
1291c33be57SNicolas Pitre 
1301c33be57SNicolas Pitre 	mpidr = read_mpidr();
1311c33be57SNicolas Pitre 	cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
1321c33be57SNicolas Pitre 	clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
1331c33be57SNicolas Pitre 	ob_cluster = clusterid;
1341c33be57SNicolas Pitre 	ib_cluster = clusterid ^ 1;
1351c33be57SNicolas Pitre 
1361c33be57SNicolas Pitre 	if (new_cluster_id == clusterid)
1371c33be57SNicolas Pitre 		return 0;
1381c33be57SNicolas Pitre 
1391c33be57SNicolas Pitre 	pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
1401c33be57SNicolas Pitre 
1411c33be57SNicolas Pitre 	/* Close the gate for our entry vectors */
1421c33be57SNicolas Pitre 	mcpm_set_entry_vector(cpuid, ob_cluster, NULL);
1431c33be57SNicolas Pitre 	mcpm_set_entry_vector(cpuid, ib_cluster, NULL);
1441c33be57SNicolas Pitre 
1451c33be57SNicolas Pitre 	/*
1461c33be57SNicolas Pitre 	 * Let's wake up the inbound CPU now in case it requires some delay
1471c33be57SNicolas Pitre 	 * to come online, but leave it gated in our entry vector code.
1481c33be57SNicolas Pitre 	 */
1491c33be57SNicolas Pitre 	ret = mcpm_cpu_power_up(cpuid, ib_cluster);
1501c33be57SNicolas Pitre 	if (ret) {
1511c33be57SNicolas Pitre 		pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
1521c33be57SNicolas Pitre 		return ret;
1531c33be57SNicolas Pitre 	}
1541c33be57SNicolas Pitre 
1551c33be57SNicolas Pitre 	/*
1561c33be57SNicolas Pitre 	 * From this point we are entering the switch critical zone
1571c33be57SNicolas Pitre 	 * and can't take any interrupts anymore.
1581c33be57SNicolas Pitre 	 */
1591c33be57SNicolas Pitre 	local_irq_disable();
1601c33be57SNicolas Pitre 	local_fiq_disable();
1611c33be57SNicolas Pitre 
1621c33be57SNicolas Pitre 	this_cpu = smp_processor_id();
1631c33be57SNicolas Pitre 
1641c33be57SNicolas Pitre 	/* redirect GIC's SGIs to our counterpart */
165ed96762eSNicolas Pitre 	gic_migrate_target(bL_gic_id[cpuid][ib_cluster]);
1661c33be57SNicolas Pitre 
1671c33be57SNicolas Pitre 	/*
1681c33be57SNicolas Pitre 	 * Raise a SGI on the inbound CPU to make sure it doesn't stall
1691c33be57SNicolas Pitre 	 * in a possible WFI, such as in mcpm_power_down().
1701c33be57SNicolas Pitre 	 */
1711c33be57SNicolas Pitre 	arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
1721c33be57SNicolas Pitre 
1733f09d479SLorenzo Pieralisi 	tdev = tick_get_device(this_cpu);
1743f09d479SLorenzo Pieralisi 	if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
1753f09d479SLorenzo Pieralisi 		tdev = NULL;
1763f09d479SLorenzo Pieralisi 	if (tdev) {
1773f09d479SLorenzo Pieralisi 		tdev_mode = tdev->evtdev->mode;
1783f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
1793f09d479SLorenzo Pieralisi 	}
1803f09d479SLorenzo Pieralisi 
1811c33be57SNicolas Pitre 	ret = cpu_pm_enter();
1821c33be57SNicolas Pitre 
1831c33be57SNicolas Pitre 	/* we can not tolerate errors at this point */
1841c33be57SNicolas Pitre 	if (ret)
1851c33be57SNicolas Pitre 		panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
1861c33be57SNicolas Pitre 
1871c33be57SNicolas Pitre 	/* Flip the cluster in the CPU logical map for this CPU. */
1881c33be57SNicolas Pitre 	cpu_logical_map(this_cpu) ^= (1 << 8);
1891c33be57SNicolas Pitre 
1901c33be57SNicolas Pitre 	/* Let's do the actual CPU switch. */
1911c33be57SNicolas Pitre 	ret = cpu_suspend(0, bL_switchpoint);
1921c33be57SNicolas Pitre 	if (ret > 0)
1931c33be57SNicolas Pitre 		panic("%s: cpu_suspend() returned %d\n", __func__, ret);
1941c33be57SNicolas Pitre 
1951c33be57SNicolas Pitre 	/* We are executing on the inbound CPU at this point */
1961c33be57SNicolas Pitre 	mpidr = read_mpidr();
1971c33be57SNicolas Pitre 	cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
1981c33be57SNicolas Pitre 	clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
1991c33be57SNicolas Pitre 	pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid);
2001c33be57SNicolas Pitre 	BUG_ON(clusterid != ib_cluster);
2011c33be57SNicolas Pitre 
2021c33be57SNicolas Pitre 	mcpm_cpu_powered_up();
2031c33be57SNicolas Pitre 
2041c33be57SNicolas Pitre 	ret = cpu_pm_exit();
2051c33be57SNicolas Pitre 
2063f09d479SLorenzo Pieralisi 	if (tdev) {
2073f09d479SLorenzo Pieralisi 		clockevents_set_mode(tdev->evtdev, tdev_mode);
2083f09d479SLorenzo Pieralisi 		clockevents_program_event(tdev->evtdev,
2093f09d479SLorenzo Pieralisi 					  tdev->evtdev->next_event, 1);
2103f09d479SLorenzo Pieralisi 	}
2113f09d479SLorenzo Pieralisi 
2121c33be57SNicolas Pitre 	local_fiq_enable();
2131c33be57SNicolas Pitre 	local_irq_enable();
2141c33be57SNicolas Pitre 
2151c33be57SNicolas Pitre 	if (ret)
2161c33be57SNicolas Pitre 		pr_err("%s exiting with error %d\n", __func__, ret);
2171c33be57SNicolas Pitre 	return ret;
2181c33be57SNicolas Pitre }
2191c33be57SNicolas Pitre 
22071ce1deeSNicolas Pitre struct bL_thread {
22171ce1deeSNicolas Pitre 	struct task_struct *task;
22271ce1deeSNicolas Pitre 	wait_queue_head_t wq;
22371ce1deeSNicolas Pitre 	int wanted_cluster;
224*6b7437aeSNicolas Pitre 	struct completion started;
2251c33be57SNicolas Pitre };
2261c33be57SNicolas Pitre 
22771ce1deeSNicolas Pitre static struct bL_thread bL_threads[NR_CPUS];
22871ce1deeSNicolas Pitre 
22971ce1deeSNicolas Pitre static int bL_switcher_thread(void *arg)
2301c33be57SNicolas Pitre {
23171ce1deeSNicolas Pitre 	struct bL_thread *t = arg;
23271ce1deeSNicolas Pitre 	struct sched_param param = { .sched_priority = 1 };
23371ce1deeSNicolas Pitre 	int cluster;
23471ce1deeSNicolas Pitre 
23571ce1deeSNicolas Pitre 	sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
236*6b7437aeSNicolas Pitre 	complete(&t->started);
23771ce1deeSNicolas Pitre 
23871ce1deeSNicolas Pitre 	do {
23971ce1deeSNicolas Pitre 		if (signal_pending(current))
24071ce1deeSNicolas Pitre 			flush_signals(current);
24171ce1deeSNicolas Pitre 		wait_event_interruptible(t->wq,
24271ce1deeSNicolas Pitre 				t->wanted_cluster != -1 ||
24371ce1deeSNicolas Pitre 				kthread_should_stop());
24471ce1deeSNicolas Pitre 		cluster = xchg(&t->wanted_cluster, -1);
24571ce1deeSNicolas Pitre 		if (cluster != -1)
24671ce1deeSNicolas Pitre 			bL_switch_to(cluster);
24771ce1deeSNicolas Pitre 	} while (!kthread_should_stop());
24871ce1deeSNicolas Pitre 
24971ce1deeSNicolas Pitre 	return 0;
25071ce1deeSNicolas Pitre }
25171ce1deeSNicolas Pitre 
252*6b7437aeSNicolas Pitre static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
25371ce1deeSNicolas Pitre {
25471ce1deeSNicolas Pitre 	struct task_struct *task;
25571ce1deeSNicolas Pitre 
25671ce1deeSNicolas Pitre 	task = kthread_create_on_node(bL_switcher_thread, arg,
25771ce1deeSNicolas Pitre 				      cpu_to_node(cpu), "kswitcher_%d", cpu);
25871ce1deeSNicolas Pitre 	if (!IS_ERR(task)) {
25971ce1deeSNicolas Pitre 		kthread_bind(task, cpu);
26071ce1deeSNicolas Pitre 		wake_up_process(task);
26171ce1deeSNicolas Pitre 	} else
26271ce1deeSNicolas Pitre 		pr_err("%s failed for CPU %d\n", __func__, cpu);
26371ce1deeSNicolas Pitre 	return task;
2641c33be57SNicolas Pitre }
2651c33be57SNicolas Pitre 
2661c33be57SNicolas Pitre /*
2671c33be57SNicolas Pitre  * bL_switch_request - Switch to a specific cluster for the given CPU
2681c33be57SNicolas Pitre  *
2691c33be57SNicolas Pitre  * @cpu: the CPU to switch
2701c33be57SNicolas Pitre  * @new_cluster_id: the ID of the cluster to switch to.
2711c33be57SNicolas Pitre  *
27271ce1deeSNicolas Pitre  * This function causes a cluster switch on the given CPU by waking up
27371ce1deeSNicolas Pitre  * the appropriate switcher thread.  This function may or may not return
27471ce1deeSNicolas Pitre  * before the switch has occurred.
2751c33be57SNicolas Pitre  */
27671ce1deeSNicolas Pitre int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
2771c33be57SNicolas Pitre {
27871ce1deeSNicolas Pitre 	struct bL_thread *t;
2791c33be57SNicolas Pitre 
28071ce1deeSNicolas Pitre 	if (cpu >= ARRAY_SIZE(bL_threads)) {
28171ce1deeSNicolas Pitre 		pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
28271ce1deeSNicolas Pitre 		return -EINVAL;
2831c33be57SNicolas Pitre 	}
2841c33be57SNicolas Pitre 
28571ce1deeSNicolas Pitre 	t = &bL_threads[cpu];
28671ce1deeSNicolas Pitre 	if (IS_ERR(t->task))
28771ce1deeSNicolas Pitre 		return PTR_ERR(t->task);
28871ce1deeSNicolas Pitre 	if (!t->task)
28971ce1deeSNicolas Pitre 		return -ESRCH;
29071ce1deeSNicolas Pitre 
29171ce1deeSNicolas Pitre 	t->wanted_cluster = new_cluster_id;
29271ce1deeSNicolas Pitre 	wake_up(&t->wq);
29371ce1deeSNicolas Pitre 	return 0;
2941c33be57SNicolas Pitre }
2951c33be57SNicolas Pitre EXPORT_SYMBOL_GPL(bL_switch_request);
29671ce1deeSNicolas Pitre 
2979797a0e9SNicolas Pitre /*
2989797a0e9SNicolas Pitre  * Activation and configuration code.
2999797a0e9SNicolas Pitre  */
3009797a0e9SNicolas Pitre 
301*6b7437aeSNicolas Pitre static unsigned int bL_switcher_active;
302*6b7437aeSNicolas Pitre static unsigned int bL_switcher_cpu_original_cluster[MAX_CPUS_PER_CLUSTER];
3039797a0e9SNicolas Pitre static cpumask_t bL_switcher_removed_logical_cpus;
3049797a0e9SNicolas Pitre 
305*6b7437aeSNicolas Pitre static void bL_switcher_restore_cpus(void)
3069797a0e9SNicolas Pitre {
3079797a0e9SNicolas Pitre 	int i;
3089797a0e9SNicolas Pitre 
3099797a0e9SNicolas Pitre 	for_each_cpu(i, &bL_switcher_removed_logical_cpus)
3109797a0e9SNicolas Pitre 		cpu_up(i);
3119797a0e9SNicolas Pitre }
3129797a0e9SNicolas Pitre 
313*6b7437aeSNicolas Pitre static int bL_switcher_halve_cpus(void)
3149797a0e9SNicolas Pitre {
3159797a0e9SNicolas Pitre 	int cpu, cluster, i, ret;
3169797a0e9SNicolas Pitre 	cpumask_t cluster_mask[2], common_mask;
3179797a0e9SNicolas Pitre 
3189797a0e9SNicolas Pitre 	cpumask_clear(&bL_switcher_removed_logical_cpus);
3199797a0e9SNicolas Pitre 	cpumask_clear(&cluster_mask[0]);
3209797a0e9SNicolas Pitre 	cpumask_clear(&cluster_mask[1]);
3219797a0e9SNicolas Pitre 
3229797a0e9SNicolas Pitre 	for_each_online_cpu(i) {
3239797a0e9SNicolas Pitre 		cpu = cpu_logical_map(i) & 0xff;
3249797a0e9SNicolas Pitre 		cluster = (cpu_logical_map(i) >> 8) & 0xff;
3259797a0e9SNicolas Pitre 		if (cluster >= 2) {
3269797a0e9SNicolas Pitre 			pr_err("%s: only dual cluster systems are supported\n", __func__);
3279797a0e9SNicolas Pitre 			return -EINVAL;
3289797a0e9SNicolas Pitre 		}
3299797a0e9SNicolas Pitre 		cpumask_set_cpu(cpu, &cluster_mask[cluster]);
3309797a0e9SNicolas Pitre 	}
3319797a0e9SNicolas Pitre 
3329797a0e9SNicolas Pitre 	if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
3339797a0e9SNicolas Pitre 		pr_err("%s: no common set of CPUs\n", __func__);
3349797a0e9SNicolas Pitre 		return -EINVAL;
3359797a0e9SNicolas Pitre 	}
3369797a0e9SNicolas Pitre 
3379797a0e9SNicolas Pitre 	for_each_online_cpu(i) {
3389797a0e9SNicolas Pitre 		cpu = cpu_logical_map(i) & 0xff;
3399797a0e9SNicolas Pitre 		cluster = (cpu_logical_map(i) >> 8) & 0xff;
3409797a0e9SNicolas Pitre 
3419797a0e9SNicolas Pitre 		if (cpumask_test_cpu(cpu, &common_mask)) {
342ed96762eSNicolas Pitre 			/* Let's take note of the GIC ID for this CPU */
343ed96762eSNicolas Pitre 			int gic_id = gic_get_cpu_id(i);
344ed96762eSNicolas Pitre 			if (gic_id < 0) {
345ed96762eSNicolas Pitre 				pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
346ed96762eSNicolas Pitre 				return -EINVAL;
347ed96762eSNicolas Pitre 			}
348ed96762eSNicolas Pitre 			bL_gic_id[cpu][cluster] = gic_id;
349ed96762eSNicolas Pitre 			pr_info("GIC ID for CPU %u cluster %u is %u\n",
350ed96762eSNicolas Pitre 				cpu, cluster, gic_id);
351ed96762eSNicolas Pitre 
3529797a0e9SNicolas Pitre 			/*
3539797a0e9SNicolas Pitre 			 * We keep only those logical CPUs which number
3549797a0e9SNicolas Pitre 			 * is equal to their physical CPU number. This is
3559797a0e9SNicolas Pitre 			 * not perfect but good enough for now.
3569797a0e9SNicolas Pitre 			 */
357*6b7437aeSNicolas Pitre 			if (cpu == i) {
358*6b7437aeSNicolas Pitre 				bL_switcher_cpu_original_cluster[cpu] = cluster;
3599797a0e9SNicolas Pitre 				continue;
3609797a0e9SNicolas Pitre 			}
361*6b7437aeSNicolas Pitre 		}
3629797a0e9SNicolas Pitre 
3639797a0e9SNicolas Pitre 		ret = cpu_down(i);
3649797a0e9SNicolas Pitre 		if (ret) {
3659797a0e9SNicolas Pitre 			bL_switcher_restore_cpus();
3669797a0e9SNicolas Pitre 			return ret;
3679797a0e9SNicolas Pitre 		}
3689797a0e9SNicolas Pitre 		cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
3699797a0e9SNicolas Pitre 	}
3709797a0e9SNicolas Pitre 
3719797a0e9SNicolas Pitre 	return 0;
3729797a0e9SNicolas Pitre }
3739797a0e9SNicolas Pitre 
374*6b7437aeSNicolas Pitre static int bL_switcher_enable(void)
37571ce1deeSNicolas Pitre {
3769797a0e9SNicolas Pitre 	int cpu, ret;
37771ce1deeSNicolas Pitre 
378*6b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
379*6b7437aeSNicolas Pitre 	if (bL_switcher_active) {
380*6b7437aeSNicolas Pitre 		cpu_hotplug_driver_unlock();
381*6b7437aeSNicolas Pitre 		return 0;
3829797a0e9SNicolas Pitre 	}
3839797a0e9SNicolas Pitre 
384*6b7437aeSNicolas Pitre 	pr_info("big.LITTLE switcher initializing\n");
385*6b7437aeSNicolas Pitre 
3869797a0e9SNicolas Pitre 	ret = bL_switcher_halve_cpus();
3879797a0e9SNicolas Pitre 	if (ret) {
3889797a0e9SNicolas Pitre 		cpu_hotplug_driver_unlock();
3899797a0e9SNicolas Pitre 		return ret;
3909797a0e9SNicolas Pitre 	}
3919797a0e9SNicolas Pitre 
39271ce1deeSNicolas Pitre 	for_each_online_cpu(cpu) {
39371ce1deeSNicolas Pitre 		struct bL_thread *t = &bL_threads[cpu];
39471ce1deeSNicolas Pitre 		init_waitqueue_head(&t->wq);
395*6b7437aeSNicolas Pitre 		init_completion(&t->started);
39671ce1deeSNicolas Pitre 		t->wanted_cluster = -1;
39771ce1deeSNicolas Pitre 		t->task = bL_switcher_thread_create(cpu, t);
39871ce1deeSNicolas Pitre 	}
399*6b7437aeSNicolas Pitre 
400*6b7437aeSNicolas Pitre 	bL_switcher_active = 1;
4019797a0e9SNicolas Pitre 	cpu_hotplug_driver_unlock();
40271ce1deeSNicolas Pitre 
40371ce1deeSNicolas Pitre 	pr_info("big.LITTLE switcher initialized\n");
40471ce1deeSNicolas Pitre 	return 0;
40571ce1deeSNicolas Pitre }
40671ce1deeSNicolas Pitre 
407*6b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
408*6b7437aeSNicolas Pitre 
409*6b7437aeSNicolas Pitre static void bL_switcher_disable(void)
410*6b7437aeSNicolas Pitre {
411*6b7437aeSNicolas Pitre 	unsigned int cpu, cluster, i;
412*6b7437aeSNicolas Pitre 	struct bL_thread *t;
413*6b7437aeSNicolas Pitre 	struct task_struct *task;
414*6b7437aeSNicolas Pitre 
415*6b7437aeSNicolas Pitre 	cpu_hotplug_driver_lock();
416*6b7437aeSNicolas Pitre 	if (!bL_switcher_active) {
417*6b7437aeSNicolas Pitre 		cpu_hotplug_driver_unlock();
418*6b7437aeSNicolas Pitre 		return;
419*6b7437aeSNicolas Pitre 	}
420*6b7437aeSNicolas Pitre 	bL_switcher_active = 0;
421*6b7437aeSNicolas Pitre 
422*6b7437aeSNicolas Pitre 	/*
423*6b7437aeSNicolas Pitre 	 * To deactivate the switcher, we must shut down the switcher
424*6b7437aeSNicolas Pitre 	 * threads to prevent any other requests from being accepted.
425*6b7437aeSNicolas Pitre 	 * Then, if the final cluster for given logical CPU is not the
426*6b7437aeSNicolas Pitre 	 * same as the original one, we'll recreate a switcher thread
427*6b7437aeSNicolas Pitre 	 * just for the purpose of switching the CPU back without any
428*6b7437aeSNicolas Pitre 	 * possibility for interference from external requests.
429*6b7437aeSNicolas Pitre 	 */
430*6b7437aeSNicolas Pitre 	for_each_online_cpu(cpu) {
431*6b7437aeSNicolas Pitre 		BUG_ON(cpu != (cpu_logical_map(cpu) & 0xff));
432*6b7437aeSNicolas Pitre 		t = &bL_threads[cpu];
433*6b7437aeSNicolas Pitre 		task = t->task;
434*6b7437aeSNicolas Pitre 		t->task = NULL;
435*6b7437aeSNicolas Pitre 		if (!task || IS_ERR(task))
436*6b7437aeSNicolas Pitre 			continue;
437*6b7437aeSNicolas Pitre 		kthread_stop(task);
438*6b7437aeSNicolas Pitre 		/* no more switch may happen on this CPU at this point */
439*6b7437aeSNicolas Pitre 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
440*6b7437aeSNicolas Pitre 		if (cluster == bL_switcher_cpu_original_cluster[cpu])
441*6b7437aeSNicolas Pitre 			continue;
442*6b7437aeSNicolas Pitre 		init_completion(&t->started);
443*6b7437aeSNicolas Pitre 		t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
444*6b7437aeSNicolas Pitre 		task = bL_switcher_thread_create(cpu, t);
445*6b7437aeSNicolas Pitre 		if (!IS_ERR(task)) {
446*6b7437aeSNicolas Pitre 			wait_for_completion(&t->started);
447*6b7437aeSNicolas Pitre 			kthread_stop(task);
448*6b7437aeSNicolas Pitre 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
449*6b7437aeSNicolas Pitre 			if (cluster == bL_switcher_cpu_original_cluster[cpu])
450*6b7437aeSNicolas Pitre 				continue;
451*6b7437aeSNicolas Pitre 		}
452*6b7437aeSNicolas Pitre 		/* If execution gets here, we're in trouble. */
453*6b7437aeSNicolas Pitre 		pr_crit("%s: unable to restore original cluster for CPU %d\n",
454*6b7437aeSNicolas Pitre 			__func__, cpu);
455*6b7437aeSNicolas Pitre 		for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
456*6b7437aeSNicolas Pitre 			if ((cpu_logical_map(i) & 0xff) != cpu)
457*6b7437aeSNicolas Pitre 				continue;
458*6b7437aeSNicolas Pitre 			pr_crit("%s: CPU %d can't be restored\n",
459*6b7437aeSNicolas Pitre 				__func__, i);
460*6b7437aeSNicolas Pitre 			cpumask_clear_cpu(i, &bL_switcher_removed_logical_cpus);
461*6b7437aeSNicolas Pitre 			break;
462*6b7437aeSNicolas Pitre 		}
463*6b7437aeSNicolas Pitre 	}
464*6b7437aeSNicolas Pitre 
465*6b7437aeSNicolas Pitre 	bL_switcher_restore_cpus();
466*6b7437aeSNicolas Pitre 	cpu_hotplug_driver_unlock();
467*6b7437aeSNicolas Pitre }
468*6b7437aeSNicolas Pitre 
469*6b7437aeSNicolas Pitre static ssize_t bL_switcher_active_show(struct kobject *kobj,
470*6b7437aeSNicolas Pitre 		struct kobj_attribute *attr, char *buf)
471*6b7437aeSNicolas Pitre {
472*6b7437aeSNicolas Pitre 	return sprintf(buf, "%u\n", bL_switcher_active);
473*6b7437aeSNicolas Pitre }
474*6b7437aeSNicolas Pitre 
475*6b7437aeSNicolas Pitre static ssize_t bL_switcher_active_store(struct kobject *kobj,
476*6b7437aeSNicolas Pitre 		struct kobj_attribute *attr, const char *buf, size_t count)
477*6b7437aeSNicolas Pitre {
478*6b7437aeSNicolas Pitre 	int ret;
479*6b7437aeSNicolas Pitre 
480*6b7437aeSNicolas Pitre 	switch (buf[0]) {
481*6b7437aeSNicolas Pitre 	case '0':
482*6b7437aeSNicolas Pitre 		bL_switcher_disable();
483*6b7437aeSNicolas Pitre 		ret = 0;
484*6b7437aeSNicolas Pitre 		break;
485*6b7437aeSNicolas Pitre 	case '1':
486*6b7437aeSNicolas Pitre 		ret = bL_switcher_enable();
487*6b7437aeSNicolas Pitre 		break;
488*6b7437aeSNicolas Pitre 	default:
489*6b7437aeSNicolas Pitre 		ret = -EINVAL;
490*6b7437aeSNicolas Pitre 	}
491*6b7437aeSNicolas Pitre 
492*6b7437aeSNicolas Pitre 	return (ret >= 0) ? count : ret;
493*6b7437aeSNicolas Pitre }
494*6b7437aeSNicolas Pitre 
495*6b7437aeSNicolas Pitre static struct kobj_attribute bL_switcher_active_attr =
496*6b7437aeSNicolas Pitre 	__ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
497*6b7437aeSNicolas Pitre 
498*6b7437aeSNicolas Pitre static struct attribute *bL_switcher_attrs[] = {
499*6b7437aeSNicolas Pitre 	&bL_switcher_active_attr.attr,
500*6b7437aeSNicolas Pitre 	NULL,
501*6b7437aeSNicolas Pitre };
502*6b7437aeSNicolas Pitre 
503*6b7437aeSNicolas Pitre static struct attribute_group bL_switcher_attr_group = {
504*6b7437aeSNicolas Pitre 	.attrs = bL_switcher_attrs,
505*6b7437aeSNicolas Pitre };
506*6b7437aeSNicolas Pitre 
507*6b7437aeSNicolas Pitre static struct kobject *bL_switcher_kobj;
508*6b7437aeSNicolas Pitre 
509*6b7437aeSNicolas Pitre static int __init bL_switcher_sysfs_init(void)
510*6b7437aeSNicolas Pitre {
511*6b7437aeSNicolas Pitre 	int ret;
512*6b7437aeSNicolas Pitre 
513*6b7437aeSNicolas Pitre 	bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
514*6b7437aeSNicolas Pitre 	if (!bL_switcher_kobj)
515*6b7437aeSNicolas Pitre 		return -ENOMEM;
516*6b7437aeSNicolas Pitre 	ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
517*6b7437aeSNicolas Pitre 	if (ret)
518*6b7437aeSNicolas Pitre 		kobject_put(bL_switcher_kobj);
519*6b7437aeSNicolas Pitre 	return ret;
520*6b7437aeSNicolas Pitre }
521*6b7437aeSNicolas Pitre 
522*6b7437aeSNicolas Pitre #endif  /* CONFIG_SYSFS */
523*6b7437aeSNicolas Pitre 
524*6b7437aeSNicolas Pitre static int __init bL_switcher_init(void)
525*6b7437aeSNicolas Pitre {
526*6b7437aeSNicolas Pitre 	int ret;
527*6b7437aeSNicolas Pitre 
528*6b7437aeSNicolas Pitre 	if (MAX_NR_CLUSTERS != 2) {
529*6b7437aeSNicolas Pitre 		pr_err("%s: only dual cluster systems are supported\n", __func__);
530*6b7437aeSNicolas Pitre 		return -EINVAL;
531*6b7437aeSNicolas Pitre 	}
532*6b7437aeSNicolas Pitre 
533*6b7437aeSNicolas Pitre 	ret = bL_switcher_enable();
534*6b7437aeSNicolas Pitre 	if (ret)
535*6b7437aeSNicolas Pitre 		return ret;
536*6b7437aeSNicolas Pitre 
537*6b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS
538*6b7437aeSNicolas Pitre 	ret = bL_switcher_sysfs_init();
539*6b7437aeSNicolas Pitre 	if (ret)
540*6b7437aeSNicolas Pitre 		pr_err("%s: unable to create sysfs entry\n", __func__);
541*6b7437aeSNicolas Pitre #endif
542*6b7437aeSNicolas Pitre 
543*6b7437aeSNicolas Pitre 	return 0;
544*6b7437aeSNicolas Pitre }
545*6b7437aeSNicolas Pitre 
54671ce1deeSNicolas Pitre late_initcall(bL_switcher_init);
547