Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Created by:	Nicolas Pitre, March 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright:	(C) 2012-2013  Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <uapi/linux/sched/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/cpu_pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/irqchip/arm-gic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <asm/smp_plat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <asm/cputype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/mcpm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <asm/bL_switcher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <trace/events/power_cpu_migrate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * __attribute_const__ and we don't want the compiler to assume any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * constness here as the value _does_ change along some code paths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int read_mpidr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return id & MPIDR_HWID_BITMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * bL switcher core code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void bL_do_switch(void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned ib_mpidr, ib_cpu, ib_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	long volatile handshake, **handshake_ptr = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	pr_debug("%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	ib_mpidr = cpu_logical_map(smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	/* Advertise our handshake location */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (handshake_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		handshake = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		*handshake_ptr = &handshake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		handshake = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * Our state has been saved at this point.  Let's release our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * inbound CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	sev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 * From this point, we must assume that our counterpart CPU might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * have taken over in its parallel world already, as if execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * just returned from cpu_suspend().  It is therefore important to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * be very careful not to make any change the other guy is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * expecting.  This is why we need stack isolation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * Fancy under cover tasks could be performed here.  For now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * we have none.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * Let's wait until our inbound is alive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	while (!handshake) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		wfe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* Let's put ourself down. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	mcpm_cpu_power_down();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/* should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * Stack isolation.  To ensure 'current' remains valid, we just use another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * piece of our thread's stack space which should be fairly lightly used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * The selected area starts just above the thread_info structure located
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * at the very bottom of the stack, aligned to a cache line, and indexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * with the cluster number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define STACK_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int bL_switchpoint(unsigned long _arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	unsigned int mpidr = read_mpidr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	void *stack = current_thread_info() + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	stack += clusterid * STACK_SIZE + STACK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	call_with_stack(bL_do_switch, (void *)_arg, stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * Generic switcher interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int bL_switcher_cpu_pairing[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * bL_switch_to - Switch to a specific cluster for the current CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * @new_cluster_id: the ID of the cluster to switch to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * This function must be called on the CPU to be switched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * Returns 0 on success, else a negative status code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int bL_switch_to(unsigned int new_cluster_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	unsigned int mpidr, this_cpu, that_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct completion inbound_alive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	long volatile *handshake_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int ipi_nr, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	this_cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ob_mpidr = read_mpidr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (new_cluster_id == ob_cluster)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	that_cpu = bL_switcher_cpu_pairing[this_cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	ib_mpidr = cpu_logical_map(that_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		 this_cpu, ob_mpidr, ib_mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	this_cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	/* Close the gate for our entry vectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* Install our "inbound alive" notifier. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	init_completion(&inbound_alive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * Let's wake up the inbound CPU now in case it requires some delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * to come online, but leave it gated in our entry vector code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * Raise a SGI on the inbound CPU to make sure it doesn't stall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 * in a possible WFI, such as in bL_power_down().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * Wait for the inbound to come up.  This allows for other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * tasks to be scheduled in the mean time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	wait_for_completion(&inbound_alive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * From this point we are entering the switch critical zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * and can't take any interrupts anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	local_fiq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	/* redirect GIC's SGIs to our counterpart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	tick_suspend_local();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	ret = cpu_pm_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* we can not tolerate errors at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/* Swap the physical CPUs in the logical map for this logical CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	cpu_logical_map(this_cpu) = ib_mpidr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	cpu_logical_map(that_cpu) = ob_mpidr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	/* Let's do the actual CPU switch. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		panic("%s: cpu_suspend() returned %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* We are executing on the inbound CPU at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	mpidr = read_mpidr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	BUG_ON(mpidr != ib_mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	mcpm_cpu_powered_up();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	ret = cpu_pm_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	tick_resume_local();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	local_fiq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	*handshake_ptr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	dsb_sev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		pr_err("%s exiting with error %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct bL_thread {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	wait_queue_head_t wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int wanted_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct completion started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	bL_switch_completion_handler completer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	void *completer_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static struct bL_thread bL_threads[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int bL_switcher_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct bL_thread *t = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	int cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	bL_switch_completion_handler completer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	void *completer_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	sched_set_fifo_low(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	complete(&t->started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			flush_signals(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		wait_event_interruptible(t->wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				t->wanted_cluster != -1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				kthread_should_stop());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		spin_lock(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		cluster = t->wanted_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		completer = t->completer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		completer_cookie = t->completer_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		t->wanted_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		t->completer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		spin_unlock(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		if (cluster != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			bL_switch_to(cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			if (completer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				completer(completer_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	} while (!kthread_should_stop());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	task = kthread_create_on_node(bL_switcher_thread, arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				      cpu_to_node(cpu), "kswitcher_%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!IS_ERR(task)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		kthread_bind(task, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		wake_up_process(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		pr_err("%s failed for CPU %d\n", __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  *      with completion notification via a callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * @cpu: the CPU to switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * @new_cluster_id: the ID of the cluster to switch to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * @completer: switch completion callback.  if non-NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  *	@completer(@completer_cookie) will be called on completion of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  *	the switch, in non-atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * @completer_cookie: opaque context argument for @completer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * This function causes a cluster switch on the given CPU by waking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * the appropriate switcher thread.  This function may or may not return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  * before the switch has occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * If a @completer callback function is supplied, it will be called when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * the switch is complete.  This can be used to determine asynchronously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * when the switch is complete, regardless of when bL_switch_request()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * returns.  When @completer is supplied, no new switch request is permitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * for the affected CPU until after the switch is complete, and @completer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * has returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			 bL_switch_completion_handler completer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			 void *completer_cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct bL_thread *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (cpu >= ARRAY_SIZE(bL_threads)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	t = &bL_threads[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (IS_ERR(t->task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return PTR_ERR(t->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (!t->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	spin_lock(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (t->completer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		spin_unlock(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	t->completer = completer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	t->completer_cookie = completer_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	t->wanted_cluster = new_cluster_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	spin_unlock(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	wake_up(&t->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) EXPORT_SYMBOL_GPL(bL_switch_request_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * Activation and configuration code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static DEFINE_MUTEX(bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static unsigned int bL_switcher_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static cpumask_t bL_switcher_removed_logical_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int bL_switcher_register_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return blocking_notifier_chain_register(&bL_activation_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int bL_switcher_unregister_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static int bL_activation_notify(unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (ret & NOTIFY_STOP_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		pr_err("%s: notifier chain failed with status 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			__func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return notifier_to_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static void bL_switcher_restore_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		struct device *cpu_dev = get_cpu_device(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		int ret = device_online(cpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			dev_err(cpu_dev, "switcher: unable to restore CPU\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int bL_switcher_halve_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	int i, j, cluster_0, gic_id, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	unsigned int cpu, cluster, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	cpumask_t available_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/* First pass to validate what we have */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	for_each_online_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		if (cluster >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			pr_err("%s: only dual cluster systems are supported\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		mask |= (1 << cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (mask != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		pr_err("%s: no CPU pairing possible\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 * Now let's do the pairing.  We match each CPU with another CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * from a different cluster.  To get a uniform scheduling behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 * without fiddling with CPU topology and compute capacity data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * we'll use logical CPUs initially belonging to the same cluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	cpumask_copy(&available_cpus, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	cluster_0 = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	for_each_cpu(i, &available_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		int match = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (cluster_0 == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			cluster_0 = cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		if (cluster != cluster_0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		cpumask_clear_cpu(i, &available_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		for_each_cpu(j, &available_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			 * Let's remember the last match to create "odd"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			 * pairings on purpose in order for other code not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			 * to assume any relation between physical and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			 * logical CPU numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			if (cluster != cluster_0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 				match = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		if (match != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			bL_switcher_cpu_pairing[i] = match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			cpumask_clear_cpu(match, &available_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			pr_info("CPU%d paired with CPU%d\n", i, match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	 * Now we disable the unwanted CPUs i.e. everything that has no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	 * pairing information (that includes the pairing counterparts).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	cpumask_clear(&bL_switcher_removed_logical_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	for_each_online_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		/* Let's take note of the GIC ID for this CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		gic_id = gic_get_cpu_id(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		if (gic_id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			bL_switcher_restore_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		bL_gic_id[cpu][cluster] = gic_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		pr_info("GIC ID for CPU %u cluster %u is %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			cpu, cluster, gic_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (bL_switcher_cpu_pairing[i] != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			bL_switcher_cpu_original_cluster[i] = cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		ret = device_offline(get_cpu_device(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			bL_switcher_restore_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Determine the logical CPU a given physical CPU is grouped on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int bL_switcher_get_logical_index(u32 mpidr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (!bL_switcher_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		return -EUNATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	mpidr &= MPIDR_HWID_BITMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		int pairing = bL_switcher_cpu_pairing[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		if (pairing == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if ((mpidr == cpu_logical_map(cpu)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		    (mpidr == cpu_logical_map(pairing)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			return cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int bL_switcher_trace_trigger(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	bL_switcher_trace_trigger_cpu(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static int bL_switcher_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	mutex_lock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	lock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (bL_switcher_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		mutex_unlock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	pr_info("big.LITTLE switcher initializing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	ret = bL_switcher_halve_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	bL_switcher_trace_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		struct bL_thread *t = &bL_threads[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		spin_lock_init(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		init_waitqueue_head(&t->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		init_completion(&t->started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		t->wanted_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		t->task = bL_switcher_thread_create(cpu, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	bL_switcher_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	bL_activation_notify(BL_NOTIFY_POST_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	pr_info("big.LITTLE switcher initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	pr_warn("big.LITTLE switcher initialization failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	mutex_unlock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static void bL_switcher_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	unsigned int cpu, cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	struct bL_thread *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	mutex_lock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	lock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (!bL_switcher_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		bL_activation_notify(BL_NOTIFY_POST_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	bL_switcher_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	 * To deactivate the switcher, we must shut down the switcher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	 * threads to prevent any other requests from being accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	 * Then, if the final cluster for given logical CPU is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	 * same as the original one, we'll recreate a switcher thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	 * just for the purpose of switching the CPU back without any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	 * possibility for interference from external requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		t = &bL_threads[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		task = t->task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		t->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		if (!task || IS_ERR(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		kthread_stop(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		/* no more switch may happen on this CPU at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		if (cluster == bL_switcher_cpu_original_cluster[cpu])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		init_completion(&t->started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		task = bL_switcher_thread_create(cpu, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		if (!IS_ERR(task)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			wait_for_completion(&t->started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			kthread_stop(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 			cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			if (cluster == bL_switcher_cpu_original_cluster[cpu])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		/* If execution gets here, we're in trouble. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		pr_crit("%s: unable to restore original cluster for CPU %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			__func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		pr_crit("%s: CPU %d can't be restored\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			__func__, bL_switcher_cpu_pairing[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 				  &bL_switcher_removed_logical_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	bL_switcher_restore_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	bL_switcher_trace_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	bL_activation_notify(BL_NOTIFY_POST_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	mutex_unlock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static ssize_t bL_switcher_active_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	return sprintf(buf, "%u\n", bL_switcher_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static ssize_t bL_switcher_active_store(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		struct kobj_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	switch (buf[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	case '0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		bL_switcher_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		ret = bL_switcher_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	return (ret >= 0) ? count : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		struct kobj_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	int ret = bL_switcher_trace_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static struct kobj_attribute bL_switcher_active_attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	__ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static struct kobj_attribute bL_switcher_trace_trigger_attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	__ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static struct attribute *bL_switcher_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	&bL_switcher_active_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	&bL_switcher_trace_trigger_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static struct attribute_group bL_switcher_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	.attrs = bL_switcher_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static struct kobject *bL_switcher_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int __init bL_switcher_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (!bL_switcher_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		kobject_put(bL_switcher_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) #endif  /* CONFIG_SYSFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) bool bL_switcher_get_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	mutex_lock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	return bL_switcher_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) void bL_switcher_put_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	mutex_unlock(&bL_switcher_activation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)  * Veto any CPU hotplug operation on those CPUs we've removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)  * while the switcher is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)  * We're just not ready to deal with that given the trickery involved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static int bL_switcher_cpu_pre(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	int pairing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (!bL_switcher_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	pairing = bL_switcher_cpu_pairing[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	if (pairing == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static bool no_bL_switcher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) static int __init bL_switcher_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	if (!mcpm_is_available())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	cpuhp_setup_state_nocalls(CPUHP_ARM_BL_PREPARE, "arm/bl:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 				  bL_switcher_cpu_pre, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/bl:predown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 					NULL, bL_switcher_cpu_pre);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		cpuhp_remove_state_nocalls(CPUHP_ARM_BL_PREPARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		pr_err("bL_switcher: Failed to allocate a hotplug state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	if (!no_bL_switcher) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		ret = bL_switcher_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	ret = bL_switcher_sysfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		pr_err("%s: unable to create sysfs entry\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) late_initcall(bL_switcher_init);