xref: /OK3568_Linux_fs/kernel/arch/arm/mach-bcm/platsmp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2014-2015 Broadcom Corporation
4*4882a593Smuzhiyun  * Copyright 2014 Linaro Limited
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/cpumask.h>
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/io.h>
12*4882a593Smuzhiyun #include <linux/irqchip/irq-bcm2836.h>
13*4882a593Smuzhiyun #include <linux/jiffies.h>
14*4882a593Smuzhiyun #include <linux/of.h>
15*4882a593Smuzhiyun #include <linux/of_address.h>
16*4882a593Smuzhiyun #include <linux/sched.h>
17*4882a593Smuzhiyun #include <linux/sched/clock.h>
18*4882a593Smuzhiyun #include <linux/smp.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <asm/cacheflush.h>
21*4882a593Smuzhiyun #include <asm/smp.h>
22*4882a593Smuzhiyun #include <asm/smp_plat.h>
23*4882a593Smuzhiyun #include <asm/smp_scu.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include "platsmp.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* Size of mapped Cortex A9 SCU address space */
28*4882a593Smuzhiyun #define CORTEX_A9_SCU_SIZE	0x58
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define SECONDARY_TIMEOUT_NS	NSEC_PER_MSEC	/* 1 msec (in nanoseconds) */
31*4882a593Smuzhiyun #define BOOT_ADDR_CPUID_MASK	0x3
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* Name of device node property defining secondary boot register location */
34*4882a593Smuzhiyun #define OF_SECONDARY_BOOT	"secondary-boot-reg"
35*4882a593Smuzhiyun #define MPIDR_CPUID_BITMASK	0x3
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * Enable the Cortex A9 Snoop Control Unit
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * By the time this is called we already know there are multiple
41*4882a593Smuzhiyun  * cores present.  We assume we're running on a Cortex A9 processor,
42*4882a593Smuzhiyun  * so any trouble getting the base address register or getting the
43*4882a593Smuzhiyun  * SCU base is a problem.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * Return 0 if successful or an error code otherwise.
46*4882a593Smuzhiyun  */
scu_a9_enable(void)47*4882a593Smuzhiyun static int __init scu_a9_enable(void)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	unsigned long config_base;
50*4882a593Smuzhiyun 	void __iomem *scu_base;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (!scu_a9_has_base()) {
53*4882a593Smuzhiyun 		pr_err("no configuration base address register!\n");
54*4882a593Smuzhiyun 		return -ENXIO;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* Config base address register value is zero for uniprocessor */
58*4882a593Smuzhiyun 	config_base = scu_a9_get_base();
59*4882a593Smuzhiyun 	if (!config_base) {
60*4882a593Smuzhiyun 		pr_err("hardware reports only one core\n");
61*4882a593Smuzhiyun 		return -ENOENT;
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	scu_base = ioremap((phys_addr_t)config_base, CORTEX_A9_SCU_SIZE);
65*4882a593Smuzhiyun 	if (!scu_base) {
66*4882a593Smuzhiyun 		pr_err("failed to remap config base (%lu/%u) for SCU\n",
67*4882a593Smuzhiyun 			config_base, CORTEX_A9_SCU_SIZE);
68*4882a593Smuzhiyun 		return -ENOMEM;
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	scu_enable(scu_base);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	iounmap(scu_base);	/* That's the last we'll need of this */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return 0;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
secondary_boot_addr_for(unsigned int cpu)78*4882a593Smuzhiyun static u32 secondary_boot_addr_for(unsigned int cpu)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	u32 secondary_boot_addr = 0;
81*4882a593Smuzhiyun 	struct device_node *cpu_node = of_get_cpu_node(cpu, NULL);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun         if (!cpu_node) {
84*4882a593Smuzhiyun 		pr_err("Failed to find device tree node for CPU%u\n", cpu);
85*4882a593Smuzhiyun 		return 0;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (of_property_read_u32(cpu_node,
89*4882a593Smuzhiyun 				 OF_SECONDARY_BOOT,
90*4882a593Smuzhiyun 				 &secondary_boot_addr))
91*4882a593Smuzhiyun 		pr_err("required secondary boot register not specified for CPU%u\n",
92*4882a593Smuzhiyun 			cpu);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	of_node_put(cpu_node);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return secondary_boot_addr;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
nsp_write_lut(unsigned int cpu)99*4882a593Smuzhiyun static int nsp_write_lut(unsigned int cpu)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	void __iomem *sku_rom_lut;
102*4882a593Smuzhiyun 	phys_addr_t secondary_startup_phy;
103*4882a593Smuzhiyun 	const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (!secondary_boot_addr)
106*4882a593Smuzhiyun 		return -EINVAL;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	sku_rom_lut = ioremap((phys_addr_t)secondary_boot_addr,
109*4882a593Smuzhiyun 				      sizeof(phys_addr_t));
110*4882a593Smuzhiyun 	if (!sku_rom_lut) {
111*4882a593Smuzhiyun 		pr_warn("unable to ioremap SKU-ROM LUT register for cpu %u\n", cpu);
112*4882a593Smuzhiyun 		return -ENOMEM;
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	secondary_startup_phy = __pa_symbol(secondary_startup);
116*4882a593Smuzhiyun 	BUG_ON(secondary_startup_phy > (phys_addr_t)U32_MAX);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	writel_relaxed(secondary_startup_phy, sku_rom_lut);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* Ensure the write is visible to the secondary core */
121*4882a593Smuzhiyun 	smp_wmb();
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	iounmap(sku_rom_lut);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
bcm_smp_prepare_cpus(unsigned int max_cpus)128*4882a593Smuzhiyun static void __init bcm_smp_prepare_cpus(unsigned int max_cpus)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	const cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* Enable the SCU on Cortex A9 based SoCs */
133*4882a593Smuzhiyun 	if (scu_a9_enable()) {
134*4882a593Smuzhiyun 		/* Update the CPU present map to reflect uniprocessor mode */
135*4882a593Smuzhiyun 		pr_warn("failed to enable A9 SCU - disabling SMP\n");
136*4882a593Smuzhiyun 		init_cpu_present(&only_cpu_0);
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun  * The ROM code has the secondary cores looping, waiting for an event.
142*4882a593Smuzhiyun  * When an event occurs each core examines the bottom two bits of the
143*4882a593Smuzhiyun  * secondary boot register.  When a core finds those bits contain its
144*4882a593Smuzhiyun  * own core id, it performs initialization, including computing its boot
145*4882a593Smuzhiyun  * address by clearing the boot register value's bottom two bits.  The
146*4882a593Smuzhiyun  * core signals that it is beginning its execution by writing its boot
147*4882a593Smuzhiyun  * address back to the secondary boot register, and finally jumps to
148*4882a593Smuzhiyun  * that address.
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * So to start a core executing we need to:
151*4882a593Smuzhiyun  * - Encode the (hardware) CPU id with the bottom bits of the secondary
152*4882a593Smuzhiyun  *   start address.
153*4882a593Smuzhiyun  * - Write that value into the secondary boot register.
154*4882a593Smuzhiyun  * - Generate an event to wake up the secondary CPU(s).
155*4882a593Smuzhiyun  * - Wait for the secondary boot register to be re-written, which
156*4882a593Smuzhiyun  *   indicates the secondary core has started.
157*4882a593Smuzhiyun  */
kona_boot_secondary(unsigned int cpu,struct task_struct * idle)158*4882a593Smuzhiyun static int kona_boot_secondary(unsigned int cpu, struct task_struct *idle)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	void __iomem *boot_reg;
161*4882a593Smuzhiyun 	phys_addr_t boot_func;
162*4882a593Smuzhiyun 	u64 start_clock;
163*4882a593Smuzhiyun 	u32 cpu_id;
164*4882a593Smuzhiyun 	u32 boot_val;
165*4882a593Smuzhiyun 	bool timeout = false;
166*4882a593Smuzhiyun 	const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	cpu_id = cpu_logical_map(cpu);
169*4882a593Smuzhiyun 	if (cpu_id & ~BOOT_ADDR_CPUID_MASK) {
170*4882a593Smuzhiyun 		pr_err("bad cpu id (%u > %u)\n", cpu_id, BOOT_ADDR_CPUID_MASK);
171*4882a593Smuzhiyun 		return -EINVAL;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	if (!secondary_boot_addr)
175*4882a593Smuzhiyun 		return -EINVAL;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	boot_reg = ioremap((phys_addr_t)secondary_boot_addr,
178*4882a593Smuzhiyun 				   sizeof(phys_addr_t));
179*4882a593Smuzhiyun 	if (!boot_reg) {
180*4882a593Smuzhiyun 		pr_err("unable to map boot register for cpu %u\n", cpu_id);
181*4882a593Smuzhiyun 		return -ENOMEM;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/*
185*4882a593Smuzhiyun 	 * Secondary cores will start in secondary_startup(),
186*4882a593Smuzhiyun 	 * defined in "arch/arm/kernel/head.S"
187*4882a593Smuzhiyun 	 */
188*4882a593Smuzhiyun 	boot_func = __pa_symbol(secondary_startup);
189*4882a593Smuzhiyun 	BUG_ON(boot_func & BOOT_ADDR_CPUID_MASK);
190*4882a593Smuzhiyun 	BUG_ON(boot_func > (phys_addr_t)U32_MAX);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* The core to start is encoded in the low bits */
193*4882a593Smuzhiyun 	boot_val = (u32)boot_func | cpu_id;
194*4882a593Smuzhiyun 	writel_relaxed(boot_val, boot_reg);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	sev();
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/* The low bits will be cleared once the core has started */
199*4882a593Smuzhiyun 	start_clock = local_clock();
200*4882a593Smuzhiyun 	while (!timeout && readl_relaxed(boot_reg) == boot_val)
201*4882a593Smuzhiyun 		timeout = local_clock() - start_clock > SECONDARY_TIMEOUT_NS;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	iounmap(boot_reg);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (!timeout)
206*4882a593Smuzhiyun 		return 0;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	pr_err("timeout waiting for cpu %u to start\n", cpu_id);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return -ENXIO;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /* Cluster Dormant Control command to bring CPU into a running state */
214*4882a593Smuzhiyun #define CDC_CMD			6
215*4882a593Smuzhiyun #define CDC_CMD_OFFSET		0
216*4882a593Smuzhiyun #define CDC_CMD_REG(cpu)	(CDC_CMD_OFFSET + 4*(cpu))
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun  * BCM23550 has a Cluster Dormant Control block that keeps the core in
220*4882a593Smuzhiyun  * idle state. A command needs to be sent to the block to bring the CPU
221*4882a593Smuzhiyun  * into running state.
222*4882a593Smuzhiyun  */
bcm23550_boot_secondary(unsigned int cpu,struct task_struct * idle)223*4882a593Smuzhiyun static int bcm23550_boot_secondary(unsigned int cpu, struct task_struct *idle)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	void __iomem *cdc_base;
226*4882a593Smuzhiyun 	struct device_node *dn;
227*4882a593Smuzhiyun 	char *name;
228*4882a593Smuzhiyun 	int ret;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* Make sure a CDC node exists before booting the
231*4882a593Smuzhiyun 	 * secondary core.
232*4882a593Smuzhiyun 	 */
233*4882a593Smuzhiyun 	name = "brcm,bcm23550-cdc";
234*4882a593Smuzhiyun 	dn = of_find_compatible_node(NULL, NULL, name);
235*4882a593Smuzhiyun 	if (!dn) {
236*4882a593Smuzhiyun 		pr_err("unable to find cdc node\n");
237*4882a593Smuzhiyun 		return -ENODEV;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	cdc_base = of_iomap(dn, 0);
241*4882a593Smuzhiyun 	of_node_put(dn);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (!cdc_base) {
244*4882a593Smuzhiyun 		pr_err("unable to remap cdc base register\n");
245*4882a593Smuzhiyun 		return -ENOMEM;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Boot the secondary core */
249*4882a593Smuzhiyun 	ret = kona_boot_secondary(cpu, idle);
250*4882a593Smuzhiyun 	if (ret)
251*4882a593Smuzhiyun 		goto out;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/* Bring this CPU to RUN state so that nIRQ nFIQ
254*4882a593Smuzhiyun 	 * signals are unblocked.
255*4882a593Smuzhiyun 	 */
256*4882a593Smuzhiyun 	writel_relaxed(CDC_CMD, cdc_base + CDC_CMD_REG(cpu));
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun out:
259*4882a593Smuzhiyun 	iounmap(cdc_base);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	return ret;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
nsp_boot_secondary(unsigned int cpu,struct task_struct * idle)264*4882a593Smuzhiyun static int nsp_boot_secondary(unsigned int cpu, struct task_struct *idle)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	int ret;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/*
269*4882a593Smuzhiyun 	 * After wake up, secondary core branches to the startup
270*4882a593Smuzhiyun 	 * address programmed at SKU ROM LUT location.
271*4882a593Smuzhiyun 	 */
272*4882a593Smuzhiyun 	ret = nsp_write_lut(cpu);
273*4882a593Smuzhiyun 	if (ret) {
274*4882a593Smuzhiyun 		pr_err("unable to write startup addr to SKU ROM LUT\n");
275*4882a593Smuzhiyun 		goto out;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/* Send a CPU wakeup interrupt to the secondary core */
279*4882a593Smuzhiyun 	arch_send_wakeup_ipi_mask(cpumask_of(cpu));
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun out:
282*4882a593Smuzhiyun 	return ret;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
bcm2836_boot_secondary(unsigned int cpu,struct task_struct * idle)285*4882a593Smuzhiyun static int bcm2836_boot_secondary(unsigned int cpu, struct task_struct *idle)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	void __iomem *intc_base;
288*4882a593Smuzhiyun 	struct device_node *dn;
289*4882a593Smuzhiyun 	char *name;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	name = "brcm,bcm2836-l1-intc";
292*4882a593Smuzhiyun 	dn = of_find_compatible_node(NULL, NULL, name);
293*4882a593Smuzhiyun 	if (!dn) {
294*4882a593Smuzhiyun 		pr_err("unable to find intc node\n");
295*4882a593Smuzhiyun 		return -ENODEV;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	intc_base = of_iomap(dn, 0);
299*4882a593Smuzhiyun 	of_node_put(dn);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	if (!intc_base) {
302*4882a593Smuzhiyun 		pr_err("unable to remap intc base register\n");
303*4882a593Smuzhiyun 		return -ENOMEM;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	writel(virt_to_phys(secondary_startup),
307*4882a593Smuzhiyun 	       intc_base + LOCAL_MAILBOX3_SET0 + 16 * cpu);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	dsb(sy);
310*4882a593Smuzhiyun 	sev();
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	iounmap(intc_base);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun static const struct smp_operations kona_smp_ops __initconst = {
318*4882a593Smuzhiyun 	.smp_prepare_cpus	= bcm_smp_prepare_cpus,
319*4882a593Smuzhiyun 	.smp_boot_secondary	= kona_boot_secondary,
320*4882a593Smuzhiyun };
321*4882a593Smuzhiyun CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx, "brcm,bcm11351-cpu-method",
322*4882a593Smuzhiyun 			&kona_smp_ops);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun static const struct smp_operations bcm23550_smp_ops __initconst = {
325*4882a593Smuzhiyun 	.smp_boot_secondary	= bcm23550_boot_secondary,
326*4882a593Smuzhiyun };
327*4882a593Smuzhiyun CPU_METHOD_OF_DECLARE(bcm_smp_bcm23550, "brcm,bcm23550",
328*4882a593Smuzhiyun 			&bcm23550_smp_ops);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun static const struct smp_operations nsp_smp_ops __initconst = {
331*4882a593Smuzhiyun 	.smp_prepare_cpus	= bcm_smp_prepare_cpus,
332*4882a593Smuzhiyun 	.smp_boot_secondary	= nsp_boot_secondary,
333*4882a593Smuzhiyun };
334*4882a593Smuzhiyun CPU_METHOD_OF_DECLARE(bcm_smp_nsp, "brcm,bcm-nsp-smp", &nsp_smp_ops);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun const struct smp_operations bcm2836_smp_ops __initconst = {
337*4882a593Smuzhiyun 	.smp_boot_secondary	= bcm2836_boot_secondary,
338*4882a593Smuzhiyun };
339*4882a593Smuzhiyun CPU_METHOD_OF_DECLARE(bcm_smp_bcm2836, "brcm,bcm2836-smp", &bcm2836_smp_ops);
340