18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Created by:	Nicolas Pitre, March 2012
68c2ecf20Sopenharmony_ci * Copyright:	(C) 2012-2013  Linaro Limited
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/atomic.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
148c2ecf20Sopenharmony_ci#include <uapi/linux/sched/types.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/cpu_pm.h>
178c2ecf20Sopenharmony_ci#include <linux/cpu.h>
188c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
198c2ecf20Sopenharmony_ci#include <linux/kthread.h>
208c2ecf20Sopenharmony_ci#include <linux/wait.h>
218c2ecf20Sopenharmony_ci#include <linux/time.h>
228c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
238c2ecf20Sopenharmony_ci#include <linux/hrtimer.h>
248c2ecf20Sopenharmony_ci#include <linux/tick.h>
258c2ecf20Sopenharmony_ci#include <linux/notifier.h>
268c2ecf20Sopenharmony_ci#include <linux/mm.h>
278c2ecf20Sopenharmony_ci#include <linux/mutex.h>
288c2ecf20Sopenharmony_ci#include <linux/smp.h>
298c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
308c2ecf20Sopenharmony_ci#include <linux/string.h>
318c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
328c2ecf20Sopenharmony_ci#include <linux/irqchip/arm-gic.h>
338c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <asm/smp_plat.h>
368c2ecf20Sopenharmony_ci#include <asm/cputype.h>
378c2ecf20Sopenharmony_ci#include <asm/suspend.h>
388c2ecf20Sopenharmony_ci#include <asm/mcpm.h>
398c2ecf20Sopenharmony_ci#include <asm/bL_switcher.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS
428c2ecf20Sopenharmony_ci#include <trace/events/power_cpu_migrate.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
478c2ecf20Sopenharmony_ci * __attribute_const__ and we don't want the compiler to assume any
488c2ecf20Sopenharmony_ci * constness here as the value _does_ change along some code paths.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int read_mpidr(void)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	unsigned int id;
548c2ecf20Sopenharmony_ci	asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
558c2ecf20Sopenharmony_ci	return id & MPIDR_HWID_BITMASK;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * bL switcher core code.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void bL_do_switch(void *_arg)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	unsigned ib_mpidr, ib_cpu, ib_cluster;
658c2ecf20Sopenharmony_ci	long volatile handshake, **handshake_ptr = _arg;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	pr_debug("%s\n", __func__);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	ib_mpidr = cpu_logical_map(smp_processor_id());
708c2ecf20Sopenharmony_ci	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
718c2ecf20Sopenharmony_ci	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* Advertise our handshake location */
748c2ecf20Sopenharmony_ci	if (handshake_ptr) {
758c2ecf20Sopenharmony_ci		handshake = 0;
768c2ecf20Sopenharmony_ci		*handshake_ptr = &handshake;
778c2ecf20Sopenharmony_ci	} else
788c2ecf20Sopenharmony_ci		handshake = -1;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Our state has been saved at this point.  Let's release our
828c2ecf20Sopenharmony_ci	 * inbound CPU.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
858c2ecf20Sopenharmony_ci	sev();
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/*
888c2ecf20Sopenharmony_ci	 * From this point, we must assume that our counterpart CPU might
898c2ecf20Sopenharmony_ci	 * have taken over in its parallel world already, as if execution
908c2ecf20Sopenharmony_ci	 * just returned from cpu_suspend().  It is therefore important to
918c2ecf20Sopenharmony_ci	 * be very careful not to make any change the other guy is not
928c2ecf20Sopenharmony_ci	 * expecting.  This is why we need stack isolation.
938c2ecf20Sopenharmony_ci	 *
948c2ecf20Sopenharmony_ci	 * Fancy under cover tasks could be performed here.  For now
958c2ecf20Sopenharmony_ci	 * we have none.
968c2ecf20Sopenharmony_ci	 */
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/*
998c2ecf20Sopenharmony_ci	 * Let's wait until our inbound is alive.
1008c2ecf20Sopenharmony_ci	 */
1018c2ecf20Sopenharmony_ci	while (!handshake) {
1028c2ecf20Sopenharmony_ci		wfe();
1038c2ecf20Sopenharmony_ci		smp_mb();
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Let's put ourself down. */
1078c2ecf20Sopenharmony_ci	mcpm_cpu_power_down();
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* should never get here */
1108c2ecf20Sopenharmony_ci	BUG();
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/*
1148c2ecf20Sopenharmony_ci * Stack isolation.  To ensure 'current' remains valid, we just use another
1158c2ecf20Sopenharmony_ci * piece of our thread's stack space which should be fairly lightly used.
1168c2ecf20Sopenharmony_ci * The selected area starts just above the thread_info structure located
1178c2ecf20Sopenharmony_ci * at the very bottom of the stack, aligned to a cache line, and indexed
1188c2ecf20Sopenharmony_ci * with the cluster number.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_ci#define STACK_SIZE 512
1218c2ecf20Sopenharmony_ciextern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
1228c2ecf20Sopenharmony_cistatic int bL_switchpoint(unsigned long _arg)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	unsigned int mpidr = read_mpidr();
1258c2ecf20Sopenharmony_ci	unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
1268c2ecf20Sopenharmony_ci	void *stack = current_thread_info() + 1;
1278c2ecf20Sopenharmony_ci	stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
1288c2ecf20Sopenharmony_ci	stack += clusterid * STACK_SIZE + STACK_SIZE;
1298c2ecf20Sopenharmony_ci	call_with_stack(bL_do_switch, (void *)_arg, stack);
1308c2ecf20Sopenharmony_ci	BUG();
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*
1348c2ecf20Sopenharmony_ci * Generic switcher interface
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
1388c2ecf20Sopenharmony_cistatic int bL_switcher_cpu_pairing[NR_CPUS];
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/*
1418c2ecf20Sopenharmony_ci * bL_switch_to - Switch to a specific cluster for the current CPU
1428c2ecf20Sopenharmony_ci * @new_cluster_id: the ID of the cluster to switch to.
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * This function must be called on the CPU to be switched.
1458c2ecf20Sopenharmony_ci * Returns 0 on success, else a negative status code.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistatic int bL_switch_to(unsigned int new_cluster_id)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	unsigned int mpidr, this_cpu, that_cpu;
1508c2ecf20Sopenharmony_ci	unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
1518c2ecf20Sopenharmony_ci	struct completion inbound_alive;
1528c2ecf20Sopenharmony_ci	long volatile *handshake_ptr;
1538c2ecf20Sopenharmony_ci	int ipi_nr, ret;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	this_cpu = smp_processor_id();
1568c2ecf20Sopenharmony_ci	ob_mpidr = read_mpidr();
1578c2ecf20Sopenharmony_ci	ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
1588c2ecf20Sopenharmony_ci	ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
1598c2ecf20Sopenharmony_ci	BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if (new_cluster_id == ob_cluster)
1628c2ecf20Sopenharmony_ci		return 0;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	that_cpu = bL_switcher_cpu_pairing[this_cpu];
1658c2ecf20Sopenharmony_ci	ib_mpidr = cpu_logical_map(that_cpu);
1668c2ecf20Sopenharmony_ci	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
1678c2ecf20Sopenharmony_ci	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
1708c2ecf20Sopenharmony_ci		 this_cpu, ob_mpidr, ib_mpidr);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	this_cpu = smp_processor_id();
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* Close the gate for our entry vectors */
1758c2ecf20Sopenharmony_ci	mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
1768c2ecf20Sopenharmony_ci	mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* Install our "inbound alive" notifier. */
1798c2ecf20Sopenharmony_ci	init_completion(&inbound_alive);
1808c2ecf20Sopenharmony_ci	ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
1818c2ecf20Sopenharmony_ci	ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
1828c2ecf20Sopenharmony_ci	mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/*
1858c2ecf20Sopenharmony_ci	 * Let's wake up the inbound CPU now in case it requires some delay
1868c2ecf20Sopenharmony_ci	 * to come online, but leave it gated in our entry vector code.
1878c2ecf20Sopenharmony_ci	 */
1888c2ecf20Sopenharmony_ci	ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
1898c2ecf20Sopenharmony_ci	if (ret) {
1908c2ecf20Sopenharmony_ci		pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
1918c2ecf20Sopenharmony_ci		return ret;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/*
1958c2ecf20Sopenharmony_ci	 * Raise a SGI on the inbound CPU to make sure it doesn't stall
1968c2ecf20Sopenharmony_ci	 * in a possible WFI, such as in bL_power_down().
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/*
2018c2ecf20Sopenharmony_ci	 * Wait for the inbound to come up.  This allows for other
2028c2ecf20Sopenharmony_ci	 * tasks to be scheduled in the mean time.
2038c2ecf20Sopenharmony_ci	 */
2048c2ecf20Sopenharmony_ci	wait_for_completion(&inbound_alive);
2058c2ecf20Sopenharmony_ci	mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * From this point we are entering the switch critical zone
2098c2ecf20Sopenharmony_ci	 * and can't take any interrupts anymore.
2108c2ecf20Sopenharmony_ci	 */
2118c2ecf20Sopenharmony_ci	local_irq_disable();
2128c2ecf20Sopenharmony_ci	local_fiq_disable();
2138c2ecf20Sopenharmony_ci	trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* redirect GIC's SGIs to our counterpart */
2168c2ecf20Sopenharmony_ci	gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	tick_suspend_local();
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	ret = cpu_pm_enter();
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/* we can not tolerate errors at this point */
2238c2ecf20Sopenharmony_ci	if (ret)
2248c2ecf20Sopenharmony_ci		panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* Swap the physical CPUs in the logical map for this logical CPU. */
2278c2ecf20Sopenharmony_ci	cpu_logical_map(this_cpu) = ib_mpidr;
2288c2ecf20Sopenharmony_ci	cpu_logical_map(that_cpu) = ob_mpidr;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* Let's do the actual CPU switch. */
2318c2ecf20Sopenharmony_ci	ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
2328c2ecf20Sopenharmony_ci	if (ret > 0)
2338c2ecf20Sopenharmony_ci		panic("%s: cpu_suspend() returned %d\n", __func__, ret);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* We are executing on the inbound CPU at this point */
2368c2ecf20Sopenharmony_ci	mpidr = read_mpidr();
2378c2ecf20Sopenharmony_ci	pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
2388c2ecf20Sopenharmony_ci	BUG_ON(mpidr != ib_mpidr);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	mcpm_cpu_powered_up();
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	ret = cpu_pm_exit();
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	tick_resume_local();
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
2478c2ecf20Sopenharmony_ci	local_fiq_enable();
2488c2ecf20Sopenharmony_ci	local_irq_enable();
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	*handshake_ptr = 1;
2518c2ecf20Sopenharmony_ci	dsb_sev();
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (ret)
2548c2ecf20Sopenharmony_ci		pr_err("%s exiting with error %d\n", __func__, ret);
2558c2ecf20Sopenharmony_ci	return ret;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistruct bL_thread {
2598c2ecf20Sopenharmony_ci	spinlock_t lock;
2608c2ecf20Sopenharmony_ci	struct task_struct *task;
2618c2ecf20Sopenharmony_ci	wait_queue_head_t wq;
2628c2ecf20Sopenharmony_ci	int wanted_cluster;
2638c2ecf20Sopenharmony_ci	struct completion started;
2648c2ecf20Sopenharmony_ci	bL_switch_completion_handler completer;
2658c2ecf20Sopenharmony_ci	void *completer_cookie;
2668c2ecf20Sopenharmony_ci};
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic struct bL_thread bL_threads[NR_CPUS];
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int bL_switcher_thread(void *arg)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct bL_thread *t = arg;
2738c2ecf20Sopenharmony_ci	int cluster;
2748c2ecf20Sopenharmony_ci	bL_switch_completion_handler completer;
2758c2ecf20Sopenharmony_ci	void *completer_cookie;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	sched_set_fifo_low(current);
2788c2ecf20Sopenharmony_ci	complete(&t->started);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	do {
2818c2ecf20Sopenharmony_ci		if (signal_pending(current))
2828c2ecf20Sopenharmony_ci			flush_signals(current);
2838c2ecf20Sopenharmony_ci		wait_event_interruptible(t->wq,
2848c2ecf20Sopenharmony_ci				t->wanted_cluster != -1 ||
2858c2ecf20Sopenharmony_ci				kthread_should_stop());
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		spin_lock(&t->lock);
2888c2ecf20Sopenharmony_ci		cluster = t->wanted_cluster;
2898c2ecf20Sopenharmony_ci		completer = t->completer;
2908c2ecf20Sopenharmony_ci		completer_cookie = t->completer_cookie;
2918c2ecf20Sopenharmony_ci		t->wanted_cluster = -1;
2928c2ecf20Sopenharmony_ci		t->completer = NULL;
2938c2ecf20Sopenharmony_ci		spin_unlock(&t->lock);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		if (cluster != -1) {
2968c2ecf20Sopenharmony_ci			bL_switch_to(cluster);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci			if (completer)
2998c2ecf20Sopenharmony_ci				completer(completer_cookie);
3008c2ecf20Sopenharmony_ci		}
3018c2ecf20Sopenharmony_ci	} while (!kthread_should_stop());
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return 0;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct task_struct *task;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	task = kthread_create_on_node(bL_switcher_thread, arg,
3118c2ecf20Sopenharmony_ci				      cpu_to_node(cpu), "kswitcher_%d", cpu);
3128c2ecf20Sopenharmony_ci	if (!IS_ERR(task)) {
3138c2ecf20Sopenharmony_ci		kthread_bind(task, cpu);
3148c2ecf20Sopenharmony_ci		wake_up_process(task);
3158c2ecf20Sopenharmony_ci	} else
3168c2ecf20Sopenharmony_ci		pr_err("%s failed for CPU %d\n", __func__, cpu);
3178c2ecf20Sopenharmony_ci	return task;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci/*
3218c2ecf20Sopenharmony_ci * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
3228c2ecf20Sopenharmony_ci *      with completion notification via a callback
3238c2ecf20Sopenharmony_ci *
3248c2ecf20Sopenharmony_ci * @cpu: the CPU to switch
3258c2ecf20Sopenharmony_ci * @new_cluster_id: the ID of the cluster to switch to.
3268c2ecf20Sopenharmony_ci * @completer: switch completion callback.  if non-NULL,
3278c2ecf20Sopenharmony_ci *	@completer(@completer_cookie) will be called on completion of
3288c2ecf20Sopenharmony_ci *	the switch, in non-atomic context.
3298c2ecf20Sopenharmony_ci * @completer_cookie: opaque context argument for @completer.
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * This function causes a cluster switch on the given CPU by waking up
3328c2ecf20Sopenharmony_ci * the appropriate switcher thread.  This function may or may not return
3338c2ecf20Sopenharmony_ci * before the switch has occurred.
3348c2ecf20Sopenharmony_ci *
3358c2ecf20Sopenharmony_ci * If a @completer callback function is supplied, it will be called when
3368c2ecf20Sopenharmony_ci * the switch is complete.  This can be used to determine asynchronously
3378c2ecf20Sopenharmony_ci * when the switch is complete, regardless of when bL_switch_request()
3388c2ecf20Sopenharmony_ci * returns.  When @completer is supplied, no new switch request is permitted
3398c2ecf20Sopenharmony_ci * for the affected CPU until after the switch is complete, and @completer
3408c2ecf20Sopenharmony_ci * has returned.
3418c2ecf20Sopenharmony_ci */
3428c2ecf20Sopenharmony_ciint bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
3438c2ecf20Sopenharmony_ci			 bL_switch_completion_handler completer,
3448c2ecf20Sopenharmony_ci			 void *completer_cookie)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct bL_thread *t;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (cpu >= ARRAY_SIZE(bL_threads)) {
3498c2ecf20Sopenharmony_ci		pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
3508c2ecf20Sopenharmony_ci		return -EINVAL;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	t = &bL_threads[cpu];
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (IS_ERR(t->task))
3568c2ecf20Sopenharmony_ci		return PTR_ERR(t->task);
3578c2ecf20Sopenharmony_ci	if (!t->task)
3588c2ecf20Sopenharmony_ci		return -ESRCH;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	spin_lock(&t->lock);
3618c2ecf20Sopenharmony_ci	if (t->completer) {
3628c2ecf20Sopenharmony_ci		spin_unlock(&t->lock);
3638c2ecf20Sopenharmony_ci		return -EBUSY;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci	t->completer = completer;
3668c2ecf20Sopenharmony_ci	t->completer_cookie = completer_cookie;
3678c2ecf20Sopenharmony_ci	t->wanted_cluster = new_cluster_id;
3688c2ecf20Sopenharmony_ci	spin_unlock(&t->lock);
3698c2ecf20Sopenharmony_ci	wake_up(&t->wq);
3708c2ecf20Sopenharmony_ci	return 0;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switch_request_cb);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/*
3758c2ecf20Sopenharmony_ci * Activation and configuration code.
3768c2ecf20Sopenharmony_ci */
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(bL_switcher_activation_lock);
3798c2ecf20Sopenharmony_cistatic BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
3808c2ecf20Sopenharmony_cistatic unsigned int bL_switcher_active;
3818c2ecf20Sopenharmony_cistatic unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
3828c2ecf20Sopenharmony_cistatic cpumask_t bL_switcher_removed_logical_cpus;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ciint bL_switcher_register_notifier(struct notifier_block *nb)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	return blocking_notifier_chain_register(&bL_activation_notifier, nb);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ciint bL_switcher_unregister_notifier(struct notifier_block *nb)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic int bL_activation_notify(unsigned long val)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	int ret;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
4018c2ecf20Sopenharmony_ci	if (ret & NOTIFY_STOP_MASK)
4028c2ecf20Sopenharmony_ci		pr_err("%s: notifier chain failed with status 0x%x\n",
4038c2ecf20Sopenharmony_ci			__func__, ret);
4048c2ecf20Sopenharmony_ci	return notifier_to_errno(ret);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic void bL_switcher_restore_cpus(void)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	int i;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
4128c2ecf20Sopenharmony_ci		struct device *cpu_dev = get_cpu_device(i);
4138c2ecf20Sopenharmony_ci		int ret = device_online(cpu_dev);
4148c2ecf20Sopenharmony_ci		if (ret)
4158c2ecf20Sopenharmony_ci			dev_err(cpu_dev, "switcher: unable to restore CPU\n");
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic int bL_switcher_halve_cpus(void)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	int i, j, cluster_0, gic_id, ret;
4228c2ecf20Sopenharmony_ci	unsigned int cpu, cluster, mask;
4238c2ecf20Sopenharmony_ci	cpumask_t available_cpus;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/* First pass to validate what we have */
4268c2ecf20Sopenharmony_ci	mask = 0;
4278c2ecf20Sopenharmony_ci	for_each_online_cpu(i) {
4288c2ecf20Sopenharmony_ci		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
4298c2ecf20Sopenharmony_ci		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
4308c2ecf20Sopenharmony_ci		if (cluster >= 2) {
4318c2ecf20Sopenharmony_ci			pr_err("%s: only dual cluster systems are supported\n", __func__);
4328c2ecf20Sopenharmony_ci			return -EINVAL;
4338c2ecf20Sopenharmony_ci		}
4348c2ecf20Sopenharmony_ci		if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
4358c2ecf20Sopenharmony_ci			return -EINVAL;
4368c2ecf20Sopenharmony_ci		mask |= (1 << cluster);
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci	if (mask != 3) {
4398c2ecf20Sopenharmony_ci		pr_err("%s: no CPU pairing possible\n", __func__);
4408c2ecf20Sopenharmony_ci		return -EINVAL;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/*
4448c2ecf20Sopenharmony_ci	 * Now let's do the pairing.  We match each CPU with another CPU
4458c2ecf20Sopenharmony_ci	 * from a different cluster.  To get a uniform scheduling behavior
4468c2ecf20Sopenharmony_ci	 * without fiddling with CPU topology and compute capacity data,
4478c2ecf20Sopenharmony_ci	 * we'll use logical CPUs initially belonging to the same cluster.
4488c2ecf20Sopenharmony_ci	 */
4498c2ecf20Sopenharmony_ci	memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
4508c2ecf20Sopenharmony_ci	cpumask_copy(&available_cpus, cpu_online_mask);
4518c2ecf20Sopenharmony_ci	cluster_0 = -1;
4528c2ecf20Sopenharmony_ci	for_each_cpu(i, &available_cpus) {
4538c2ecf20Sopenharmony_ci		int match = -1;
4548c2ecf20Sopenharmony_ci		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
4558c2ecf20Sopenharmony_ci		if (cluster_0 == -1)
4568c2ecf20Sopenharmony_ci			cluster_0 = cluster;
4578c2ecf20Sopenharmony_ci		if (cluster != cluster_0)
4588c2ecf20Sopenharmony_ci			continue;
4598c2ecf20Sopenharmony_ci		cpumask_clear_cpu(i, &available_cpus);
4608c2ecf20Sopenharmony_ci		for_each_cpu(j, &available_cpus) {
4618c2ecf20Sopenharmony_ci			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
4628c2ecf20Sopenharmony_ci			/*
4638c2ecf20Sopenharmony_ci			 * Let's remember the last match to create "odd"
4648c2ecf20Sopenharmony_ci			 * pairings on purpose in order for other code not
4658c2ecf20Sopenharmony_ci			 * to assume any relation between physical and
4668c2ecf20Sopenharmony_ci			 * logical CPU numbers.
4678c2ecf20Sopenharmony_ci			 */
4688c2ecf20Sopenharmony_ci			if (cluster != cluster_0)
4698c2ecf20Sopenharmony_ci				match = j;
4708c2ecf20Sopenharmony_ci		}
4718c2ecf20Sopenharmony_ci		if (match != -1) {
4728c2ecf20Sopenharmony_ci			bL_switcher_cpu_pairing[i] = match;
4738c2ecf20Sopenharmony_ci			cpumask_clear_cpu(match, &available_cpus);
4748c2ecf20Sopenharmony_ci			pr_info("CPU%d paired with CPU%d\n", i, match);
4758c2ecf20Sopenharmony_ci		}
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/*
4798c2ecf20Sopenharmony_ci	 * Now we disable the unwanted CPUs i.e. everything that has no
4808c2ecf20Sopenharmony_ci	 * pairing information (that includes the pairing counterparts).
4818c2ecf20Sopenharmony_ci	 */
4828c2ecf20Sopenharmony_ci	cpumask_clear(&bL_switcher_removed_logical_cpus);
4838c2ecf20Sopenharmony_ci	for_each_online_cpu(i) {
4848c2ecf20Sopenharmony_ci		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
4858c2ecf20Sopenharmony_ci		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci		/* Let's take note of the GIC ID for this CPU */
4888c2ecf20Sopenharmony_ci		gic_id = gic_get_cpu_id(i);
4898c2ecf20Sopenharmony_ci		if (gic_id < 0) {
4908c2ecf20Sopenharmony_ci			pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
4918c2ecf20Sopenharmony_ci			bL_switcher_restore_cpus();
4928c2ecf20Sopenharmony_ci			return -EINVAL;
4938c2ecf20Sopenharmony_ci		}
4948c2ecf20Sopenharmony_ci		bL_gic_id[cpu][cluster] = gic_id;
4958c2ecf20Sopenharmony_ci		pr_info("GIC ID for CPU %u cluster %u is %u\n",
4968c2ecf20Sopenharmony_ci			cpu, cluster, gic_id);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		if (bL_switcher_cpu_pairing[i] != -1) {
4998c2ecf20Sopenharmony_ci			bL_switcher_cpu_original_cluster[i] = cluster;
5008c2ecf20Sopenharmony_ci			continue;
5018c2ecf20Sopenharmony_ci		}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci		ret = device_offline(get_cpu_device(i));
5048c2ecf20Sopenharmony_ci		if (ret) {
5058c2ecf20Sopenharmony_ci			bL_switcher_restore_cpus();
5068c2ecf20Sopenharmony_ci			return ret;
5078c2ecf20Sopenharmony_ci		}
5088c2ecf20Sopenharmony_ci		cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	return 0;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/* Determine the logical CPU a given physical CPU is grouped on. */
5158c2ecf20Sopenharmony_ciint bL_switcher_get_logical_index(u32 mpidr)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	int cpu;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (!bL_switcher_active)
5208c2ecf20Sopenharmony_ci		return -EUNATCH;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	mpidr &= MPIDR_HWID_BITMASK;
5238c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu) {
5248c2ecf20Sopenharmony_ci		int pairing = bL_switcher_cpu_pairing[cpu];
5258c2ecf20Sopenharmony_ci		if (pairing == -1)
5268c2ecf20Sopenharmony_ci			continue;
5278c2ecf20Sopenharmony_ci		if ((mpidr == cpu_logical_map(cpu)) ||
5288c2ecf20Sopenharmony_ci		    (mpidr == cpu_logical_map(pairing)))
5298c2ecf20Sopenharmony_ci			return cpu;
5308c2ecf20Sopenharmony_ci	}
5318c2ecf20Sopenharmony_ci	return -EINVAL;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistatic void bL_switcher_trace_trigger_cpu(void *__always_unused info)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ciint bL_switcher_trace_trigger(void)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	preempt_disable();
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	bL_switcher_trace_trigger_cpu(NULL);
5448c2ecf20Sopenharmony_ci	smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	preempt_enable();
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	return 0;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic int bL_switcher_enable(void)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	int cpu, ret;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	mutex_lock(&bL_switcher_activation_lock);
5578c2ecf20Sopenharmony_ci	lock_device_hotplug();
5588c2ecf20Sopenharmony_ci	if (bL_switcher_active) {
5598c2ecf20Sopenharmony_ci		unlock_device_hotplug();
5608c2ecf20Sopenharmony_ci		mutex_unlock(&bL_switcher_activation_lock);
5618c2ecf20Sopenharmony_ci		return 0;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	pr_info("big.LITTLE switcher initializing\n");
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
5678c2ecf20Sopenharmony_ci	if (ret)
5688c2ecf20Sopenharmony_ci		goto error;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	ret = bL_switcher_halve_cpus();
5718c2ecf20Sopenharmony_ci	if (ret)
5728c2ecf20Sopenharmony_ci		goto error;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	bL_switcher_trace_trigger();
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu) {
5778c2ecf20Sopenharmony_ci		struct bL_thread *t = &bL_threads[cpu];
5788c2ecf20Sopenharmony_ci		spin_lock_init(&t->lock);
5798c2ecf20Sopenharmony_ci		init_waitqueue_head(&t->wq);
5808c2ecf20Sopenharmony_ci		init_completion(&t->started);
5818c2ecf20Sopenharmony_ci		t->wanted_cluster = -1;
5828c2ecf20Sopenharmony_ci		t->task = bL_switcher_thread_create(cpu, t);
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	bL_switcher_active = 1;
5868c2ecf20Sopenharmony_ci	bL_activation_notify(BL_NOTIFY_POST_ENABLE);
5878c2ecf20Sopenharmony_ci	pr_info("big.LITTLE switcher initialized\n");
5888c2ecf20Sopenharmony_ci	goto out;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cierror:
5918c2ecf20Sopenharmony_ci	pr_warn("big.LITTLE switcher initialization failed\n");
5928c2ecf20Sopenharmony_ci	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ciout:
5958c2ecf20Sopenharmony_ci	unlock_device_hotplug();
5968c2ecf20Sopenharmony_ci	mutex_unlock(&bL_switcher_activation_lock);
5978c2ecf20Sopenharmony_ci	return ret;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSFS
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic void bL_switcher_disable(void)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	unsigned int cpu, cluster;
6058c2ecf20Sopenharmony_ci	struct bL_thread *t;
6068c2ecf20Sopenharmony_ci	struct task_struct *task;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	mutex_lock(&bL_switcher_activation_lock);
6098c2ecf20Sopenharmony_ci	lock_device_hotplug();
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (!bL_switcher_active)
6128c2ecf20Sopenharmony_ci		goto out;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
6158c2ecf20Sopenharmony_ci		bL_activation_notify(BL_NOTIFY_POST_ENABLE);
6168c2ecf20Sopenharmony_ci		goto out;
6178c2ecf20Sopenharmony_ci	}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	bL_switcher_active = 0;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	/*
6228c2ecf20Sopenharmony_ci	 * To deactivate the switcher, we must shut down the switcher
6238c2ecf20Sopenharmony_ci	 * threads to prevent any other requests from being accepted.
6248c2ecf20Sopenharmony_ci	 * Then, if the final cluster for given logical CPU is not the
6258c2ecf20Sopenharmony_ci	 * same as the original one, we'll recreate a switcher thread
6268c2ecf20Sopenharmony_ci	 * just for the purpose of switching the CPU back without any
6278c2ecf20Sopenharmony_ci	 * possibility for interference from external requests.
6288c2ecf20Sopenharmony_ci	 */
6298c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu) {
6308c2ecf20Sopenharmony_ci		t = &bL_threads[cpu];
6318c2ecf20Sopenharmony_ci		task = t->task;
6328c2ecf20Sopenharmony_ci		t->task = NULL;
6338c2ecf20Sopenharmony_ci		if (!task || IS_ERR(task))
6348c2ecf20Sopenharmony_ci			continue;
6358c2ecf20Sopenharmony_ci		kthread_stop(task);
6368c2ecf20Sopenharmony_ci		/* no more switch may happen on this CPU at this point */
6378c2ecf20Sopenharmony_ci		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6388c2ecf20Sopenharmony_ci		if (cluster == bL_switcher_cpu_original_cluster[cpu])
6398c2ecf20Sopenharmony_ci			continue;
6408c2ecf20Sopenharmony_ci		init_completion(&t->started);
6418c2ecf20Sopenharmony_ci		t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
6428c2ecf20Sopenharmony_ci		task = bL_switcher_thread_create(cpu, t);
6438c2ecf20Sopenharmony_ci		if (!IS_ERR(task)) {
6448c2ecf20Sopenharmony_ci			wait_for_completion(&t->started);
6458c2ecf20Sopenharmony_ci			kthread_stop(task);
6468c2ecf20Sopenharmony_ci			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
6478c2ecf20Sopenharmony_ci			if (cluster == bL_switcher_cpu_original_cluster[cpu])
6488c2ecf20Sopenharmony_ci				continue;
6498c2ecf20Sopenharmony_ci		}
6508c2ecf20Sopenharmony_ci		/* If execution gets here, we're in trouble. */
6518c2ecf20Sopenharmony_ci		pr_crit("%s: unable to restore original cluster for CPU %d\n",
6528c2ecf20Sopenharmony_ci			__func__, cpu);
6538c2ecf20Sopenharmony_ci		pr_crit("%s: CPU %d can't be restored\n",
6548c2ecf20Sopenharmony_ci			__func__, bL_switcher_cpu_pairing[cpu]);
6558c2ecf20Sopenharmony_ci		cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
6568c2ecf20Sopenharmony_ci				  &bL_switcher_removed_logical_cpus);
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	bL_switcher_restore_cpus();
6608c2ecf20Sopenharmony_ci	bL_switcher_trace_trigger();
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ciout:
6658c2ecf20Sopenharmony_ci	unlock_device_hotplug();
6668c2ecf20Sopenharmony_ci	mutex_unlock(&bL_switcher_activation_lock);
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic ssize_t bL_switcher_active_show(struct kobject *kobj,
6708c2ecf20Sopenharmony_ci		struct kobj_attribute *attr, char *buf)
6718c2ecf20Sopenharmony_ci{
6728c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", bL_switcher_active);
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic ssize_t bL_switcher_active_store(struct kobject *kobj,
6768c2ecf20Sopenharmony_ci		struct kobj_attribute *attr, const char *buf, size_t count)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	int ret;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	switch (buf[0]) {
6818c2ecf20Sopenharmony_ci	case '0':
6828c2ecf20Sopenharmony_ci		bL_switcher_disable();
6838c2ecf20Sopenharmony_ci		ret = 0;
6848c2ecf20Sopenharmony_ci		break;
6858c2ecf20Sopenharmony_ci	case '1':
6868c2ecf20Sopenharmony_ci		ret = bL_switcher_enable();
6878c2ecf20Sopenharmony_ci		break;
6888c2ecf20Sopenharmony_ci	default:
6898c2ecf20Sopenharmony_ci		ret = -EINVAL;
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	return (ret >= 0) ? count : ret;
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
6968c2ecf20Sopenharmony_ci		struct kobj_attribute *attr, const char *buf, size_t count)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	int ret = bL_switcher_trace_trigger();
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	return ret ? ret : count;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic struct kobj_attribute bL_switcher_active_attr =
7048c2ecf20Sopenharmony_ci	__ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_cistatic struct kobj_attribute bL_switcher_trace_trigger_attr =
7078c2ecf20Sopenharmony_ci	__ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic struct attribute *bL_switcher_attrs[] = {
7108c2ecf20Sopenharmony_ci	&bL_switcher_active_attr.attr,
7118c2ecf20Sopenharmony_ci	&bL_switcher_trace_trigger_attr.attr,
7128c2ecf20Sopenharmony_ci	NULL,
7138c2ecf20Sopenharmony_ci};
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic struct attribute_group bL_switcher_attr_group = {
7168c2ecf20Sopenharmony_ci	.attrs = bL_switcher_attrs,
7178c2ecf20Sopenharmony_ci};
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic struct kobject *bL_switcher_kobj;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_cistatic int __init bL_switcher_sysfs_init(void)
7228c2ecf20Sopenharmony_ci{
7238c2ecf20Sopenharmony_ci	int ret;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
7268c2ecf20Sopenharmony_ci	if (!bL_switcher_kobj)
7278c2ecf20Sopenharmony_ci		return -ENOMEM;
7288c2ecf20Sopenharmony_ci	ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
7298c2ecf20Sopenharmony_ci	if (ret)
7308c2ecf20Sopenharmony_ci		kobject_put(bL_switcher_kobj);
7318c2ecf20Sopenharmony_ci	return ret;
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci#endif  /* CONFIG_SYSFS */
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cibool bL_switcher_get_enabled(void)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	mutex_lock(&bL_switcher_activation_lock);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	return bL_switcher_active;
7418c2ecf20Sopenharmony_ci}
7428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_civoid bL_switcher_put_enabled(void)
7458c2ecf20Sopenharmony_ci{
7468c2ecf20Sopenharmony_ci	mutex_unlock(&bL_switcher_activation_lock);
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci/*
7518c2ecf20Sopenharmony_ci * Veto any CPU hotplug operation on those CPUs we've removed
7528c2ecf20Sopenharmony_ci * while the switcher is active.
7538c2ecf20Sopenharmony_ci * We're just not ready to deal with that given the trickery involved.
7548c2ecf20Sopenharmony_ci */
7558c2ecf20Sopenharmony_cistatic int bL_switcher_cpu_pre(unsigned int cpu)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	int pairing;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	if (!bL_switcher_active)
7608c2ecf20Sopenharmony_ci		return 0;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	pairing = bL_switcher_cpu_pairing[cpu];
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	if (pairing == -1)
7658c2ecf20Sopenharmony_ci		return -EINVAL;
7668c2ecf20Sopenharmony_ci	return 0;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic bool no_bL_switcher;
7708c2ecf20Sopenharmony_cicore_param(no_bL_switcher, no_bL_switcher, bool, 0644);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic int __init bL_switcher_init(void)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	int ret;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	if (!mcpm_is_available())
7778c2ecf20Sopenharmony_ci		return -ENODEV;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	cpuhp_setup_state_nocalls(CPUHP_ARM_BL_PREPARE, "arm/bl:prepare",
7808c2ecf20Sopenharmony_ci				  bL_switcher_cpu_pre, NULL);
7818c2ecf20Sopenharmony_ci	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/bl:predown",
7828c2ecf20Sopenharmony_ci					NULL, bL_switcher_cpu_pre);
7838c2ecf20Sopenharmony_ci	if (ret < 0) {
7848c2ecf20Sopenharmony_ci		cpuhp_remove_state_nocalls(CPUHP_ARM_BL_PREPARE);
7858c2ecf20Sopenharmony_ci		pr_err("bL_switcher: Failed to allocate a hotplug state\n");
7868c2ecf20Sopenharmony_ci		return ret;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci	if (!no_bL_switcher) {
7898c2ecf20Sopenharmony_ci		ret = bL_switcher_enable();
7908c2ecf20Sopenharmony_ci		if (ret)
7918c2ecf20Sopenharmony_ci			return ret;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSFS
7958c2ecf20Sopenharmony_ci	ret = bL_switcher_sysfs_init();
7968c2ecf20Sopenharmony_ci	if (ret)
7978c2ecf20Sopenharmony_ci		pr_err("%s: unable to create sysfs entry\n", __func__);
7988c2ecf20Sopenharmony_ci#endif
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	return 0;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cilate_initcall(bL_switcher_init);
804