2 * Copyright (C) 2014-2015 Broadcom Corporation
3 * Copyright 2014 Linaro Limited
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2.
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether express or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
20 #include <linux/jiffies.h>
22 #include <linux/of_address.h>
23 #include <linux/sched.h>
24 #include <linux/smp.h>
26 #include <asm/cacheflush.h>
28 #include <asm/smp_plat.h>
29 #include <asm/smp_scu.h>
31 /* Size of mapped Cortex A9 SCU address space */
32 #define CORTEX_A9_SCU_SIZE 0x58
34 #define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC /* 1 msec (in nanoseconds) */
35 #define BOOT_ADDR_CPUID_MASK 0x3
37 /* Name of device node property defining secondary boot register location */
38 #define OF_SECONDARY_BOOT "secondary-boot-reg"
39 #define MPIDR_CPUID_BITMASK 0x3
41 /* I/O address of register used to coordinate secondary core startup */
42 static u32 secondary_boot_addr
;
45 * Enable the Cortex A9 Snoop Control Unit
47 * By the time this is called we already know there are multiple
48 * cores present. We assume we're running on a Cortex A9 processor,
49 * so any trouble getting the base address register or getting the
50 * SCU base is a problem.
52 * Return 0 if successful or an error code otherwise.
54 static int __init
scu_a9_enable(void)
56 unsigned long config_base
;
57 void __iomem
*scu_base
;
59 if (!scu_a9_has_base()) {
60 pr_err("no configuration base address register!\n");
64 /* Config base address register value is zero for uniprocessor */
65 config_base
= scu_a9_get_base();
67 pr_err("hardware reports only one core\n");
71 scu_base
= ioremap((phys_addr_t
)config_base
, CORTEX_A9_SCU_SIZE
);
73 pr_err("failed to remap config base (%lu/%u) for SCU\n",
74 config_base
, CORTEX_A9_SCU_SIZE
);
80 iounmap(scu_base
); /* That's the last we'll need of this */
85 static int nsp_write_lut(void)
87 void __iomem
*sku_rom_lut
;
88 phys_addr_t secondary_startup_phy
;
90 if (!secondary_boot_addr
) {
91 pr_warn("required secondary boot register not specified\n");
95 sku_rom_lut
= ioremap_nocache((phys_addr_t
)secondary_boot_addr
,
96 sizeof(secondary_boot_addr
));
98 pr_warn("unable to ioremap SKU-ROM LUT register\n");
102 secondary_startup_phy
= virt_to_phys(secondary_startup
);
103 BUG_ON(secondary_startup_phy
> (phys_addr_t
)U32_MAX
);
105 writel_relaxed(secondary_startup_phy
, sku_rom_lut
);
107 /* Ensure the write is visible to the secondary core */
110 iounmap(sku_rom_lut
);
115 static void __init
bcm_smp_prepare_cpus(unsigned int max_cpus
)
117 static cpumask_t only_cpu_0
= { CPU_BITS_CPU0
};
118 struct device_node
*cpus_node
= NULL
;
119 struct device_node
*cpu_node
= NULL
;
123 * This function is only called via smp_ops->smp_prepare_cpu().
124 * That only happens if a "/cpus" device tree node exists
125 * and has an "enable-method" property that selects the SMP
126 * operations defined herein.
128 cpus_node
= of_find_node_by_path("/cpus");
132 for_each_child_of_node(cpus_node
, cpu_node
) {
135 if (of_node_cmp(cpu_node
->type
, "cpu"))
138 if (of_property_read_u32(cpu_node
, "reg", &cpuid
)) {
139 pr_debug("%s: missing reg property\n",
140 cpu_node
->full_name
);
146 * "secondary-boot-reg" property should be defined only
149 if ((cpuid
& MPIDR_CPUID_BITMASK
) == 1) {
151 * Our secondary enable method requires a
152 * "secondary-boot-reg" property to specify a register
153 * address used to request the ROM code boot a secondary
154 * core. If we have any trouble getting this we fall
155 * back to uniprocessor mode.
157 if (of_property_read_u32(cpu_node
,
159 &secondary_boot_addr
)) {
160 pr_warn("%s: no" OF_SECONDARY_BOOT
"property\n",
169 * Enable the SCU on Cortex A9 based SoCs. If -ENOENT is
170 * returned, the SoC reported a uniprocessor configuration.
171 * We bail on any other error.
173 ret
= scu_a9_enable();
175 of_node_put(cpu_node
);
176 of_node_put(cpus_node
);
179 /* Update the CPU present map to reflect uniprocessor mode */
180 pr_warn("disabling SMP\n");
181 init_cpu_present(&only_cpu_0
);
186 * The ROM code has the secondary cores looping, waiting for an event.
187 * When an event occurs each core examines the bottom two bits of the
188 * secondary boot register. When a core finds those bits contain its
189 * own core id, it performs initialization, including computing its boot
190 * address by clearing the boot register value's bottom two bits. The
191 * core signals that it is beginning its execution by writing its boot
192 * address back to the secondary boot register, and finally jumps to
195 * So to start a core executing we need to:
196 * - Encode the (hardware) CPU id with the bottom bits of the secondary
198 * - Write that value into the secondary boot register.
199 * - Generate an event to wake up the secondary CPU(s).
200 * - Wait for the secondary boot register to be re-written, which
201 * indicates the secondary core has started.
203 static int kona_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
205 void __iomem
*boot_reg
;
206 phys_addr_t boot_func
;
210 bool timeout
= false;
212 cpu_id
= cpu_logical_map(cpu
);
213 if (cpu_id
& ~BOOT_ADDR_CPUID_MASK
) {
214 pr_err("bad cpu id (%u > %u)\n", cpu_id
, BOOT_ADDR_CPUID_MASK
);
218 if (!secondary_boot_addr
) {
219 pr_err("required secondary boot register not specified\n");
223 boot_reg
= ioremap_nocache(
224 (phys_addr_t
)secondary_boot_addr
, sizeof(u32
));
226 pr_err("unable to map boot register for cpu %u\n", cpu_id
);
231 * Secondary cores will start in secondary_startup(),
232 * defined in "arch/arm/kernel/head.S"
234 boot_func
= virt_to_phys(secondary_startup
);
235 BUG_ON(boot_func
& BOOT_ADDR_CPUID_MASK
);
236 BUG_ON(boot_func
> (phys_addr_t
)U32_MAX
);
238 /* The core to start is encoded in the low bits */
239 boot_val
= (u32
)boot_func
| cpu_id
;
240 writel_relaxed(boot_val
, boot_reg
);
244 /* The low bits will be cleared once the core has started */
245 start_clock
= local_clock();
246 while (!timeout
&& readl_relaxed(boot_reg
) == boot_val
)
247 timeout
= local_clock() - start_clock
> SECONDARY_TIMEOUT_NS
;
254 pr_err("timeout waiting for cpu %u to start\n", cpu_id
);
259 /* Cluster Dormant Control command to bring CPU into a running state */
261 #define CDC_CMD_OFFSET 0
262 #define CDC_CMD_REG(cpu) (CDC_CMD_OFFSET + 4*(cpu))
265 * BCM23550 has a Cluster Dormant Control block that keeps the core in
266 * idle state. A command needs to be sent to the block to bring the CPU
267 * into running state.
269 static int bcm23550_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
271 void __iomem
*cdc_base
;
272 struct device_node
*dn
;
276 /* Make sure a CDC node exists before booting the
279 name
= "brcm,bcm23550-cdc";
280 dn
= of_find_compatible_node(NULL
, NULL
, name
);
282 pr_err("unable to find cdc node\n");
286 cdc_base
= of_iomap(dn
, 0);
290 pr_err("unable to remap cdc base register\n");
294 /* Boot the secondary core */
295 ret
= kona_boot_secondary(cpu
, idle
);
299 /* Bring this CPU to RUN state so that nIRQ nFIQ
300 * signals are unblocked.
302 writel_relaxed(CDC_CMD
, cdc_base
+ CDC_CMD_REG(cpu
));
310 static int nsp_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
315 * After wake up, secondary core branches to the startup
316 * address programmed at SKU ROM LUT location.
318 ret
= nsp_write_lut();
320 pr_err("unable to write startup addr to SKU ROM LUT\n");
324 /* Send a CPU wakeup interrupt to the secondary core */
325 arch_send_wakeup_ipi_mask(cpumask_of(cpu
));
331 static const struct smp_operations bcm_smp_ops __initconst
= {
332 .smp_prepare_cpus
= bcm_smp_prepare_cpus
,
333 .smp_boot_secondary
= kona_boot_secondary
,
335 CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx
, "brcm,bcm11351-cpu-method",
338 static const struct smp_operations bcm23550_smp_ops __initconst
= {
339 .smp_boot_secondary
= bcm23550_boot_secondary
,
341 CPU_METHOD_OF_DECLARE(bcm_smp_bcm23550
, "brcm,bcm23550",
344 static const struct smp_operations nsp_smp_ops __initconst
= {
345 .smp_prepare_cpus
= bcm_smp_prepare_cpus
,
346 .smp_boot_secondary
= nsp_boot_secondary
,
348 CPU_METHOD_OF_DECLARE(bcm_smp_nsp
, "brcm,bcm-nsp-smp", &nsp_smp_ops
);