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/sched.h>
23 #include <linux/smp.h>
25 #include <asm/cacheflush.h>
27 #include <asm/smp_plat.h>
28 #include <asm/smp_scu.h>
30 /* Size of mapped Cortex A9 SCU address space */
31 #define CORTEX_A9_SCU_SIZE 0x58
33 #define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC /* 1 msec (in nanoseconds) */
34 #define BOOT_ADDR_CPUID_MASK 0x3
36 /* Name of device node property defining secondary boot register location */
37 #define OF_SECONDARY_BOOT "secondary-boot-reg"
38 #define MPIDR_CPUID_BITMASK 0x3
40 /* I/O address of register used to coordinate secondary core startup */
41 static u32 secondary_boot_addr
;
44 * Enable the Cortex A9 Snoop Control Unit
46 * By the time this is called we already know there are multiple
47 * cores present. We assume we're running on a Cortex A9 processor,
48 * so any trouble getting the base address register or getting the
49 * SCU base is a problem.
51 * Return 0 if successful or an error code otherwise.
53 static int __init
scu_a9_enable(void)
55 unsigned long config_base
;
56 void __iomem
*scu_base
;
58 if (!scu_a9_has_base()) {
59 pr_err("no configuration base address register!\n");
63 /* Config base address register value is zero for uniprocessor */
64 config_base
= scu_a9_get_base();
66 pr_err("hardware reports only one core\n");
70 scu_base
= ioremap((phys_addr_t
)config_base
, CORTEX_A9_SCU_SIZE
);
72 pr_err("failed to remap config base (%lu/%u) for SCU\n",
73 config_base
, CORTEX_A9_SCU_SIZE
);
79 iounmap(scu_base
); /* That's the last we'll need of this */
84 static int nsp_write_lut(void)
86 void __iomem
*sku_rom_lut
;
87 phys_addr_t secondary_startup_phy
;
89 if (!secondary_boot_addr
) {
90 pr_warn("required secondary boot register not specified\n");
94 sku_rom_lut
= ioremap_nocache((phys_addr_t
)secondary_boot_addr
,
95 sizeof(secondary_boot_addr
));
97 pr_warn("unable to ioremap SKU-ROM LUT register\n");
101 secondary_startup_phy
= virt_to_phys(secondary_startup
);
102 BUG_ON(secondary_startup_phy
> (phys_addr_t
)U32_MAX
);
104 writel_relaxed(secondary_startup_phy
, sku_rom_lut
);
106 /* Ensure the write is visible to the secondary core */
109 iounmap(sku_rom_lut
);
114 static void __init
bcm_smp_prepare_cpus(unsigned int max_cpus
)
116 static cpumask_t only_cpu_0
= { CPU_BITS_CPU0
};
117 struct device_node
*cpus_node
= NULL
;
118 struct device_node
*cpu_node
= NULL
;
122 * This function is only called via smp_ops->smp_prepare_cpu().
123 * That only happens if a "/cpus" device tree node exists
124 * and has an "enable-method" property that selects the SMP
125 * operations defined herein.
127 cpus_node
= of_find_node_by_path("/cpus");
131 for_each_child_of_node(cpus_node
, cpu_node
) {
134 if (of_node_cmp(cpu_node
->type
, "cpu"))
137 if (of_property_read_u32(cpu_node
, "reg", &cpuid
)) {
138 pr_debug("%s: missing reg property\n",
139 cpu_node
->full_name
);
145 * "secondary-boot-reg" property should be defined only
148 if ((cpuid
& MPIDR_CPUID_BITMASK
) == 1) {
150 * Our secondary enable method requires a
151 * "secondary-boot-reg" property to specify a register
152 * address used to request the ROM code boot a secondary
153 * core. If we have any trouble getting this we fall
154 * back to uniprocessor mode.
156 if (of_property_read_u32(cpu_node
,
158 &secondary_boot_addr
)) {
159 pr_warn("%s: no" OF_SECONDARY_BOOT
"property\n",
168 * Enable the SCU on Cortex A9 based SoCs. If -ENOENT is
169 * returned, the SoC reported a uniprocessor configuration.
170 * We bail on any other error.
172 ret
= scu_a9_enable();
174 of_node_put(cpu_node
);
175 of_node_put(cpus_node
);
178 /* Update the CPU present map to reflect uniprocessor mode */
179 pr_warn("disabling SMP\n");
180 init_cpu_present(&only_cpu_0
);
185 * The ROM code has the secondary cores looping, waiting for an event.
186 * When an event occurs each core examines the bottom two bits of the
187 * secondary boot register. When a core finds those bits contain its
188 * own core id, it performs initialization, including computing its boot
189 * address by clearing the boot register value's bottom two bits. The
190 * core signals that it is beginning its execution by writing its boot
191 * address back to the secondary boot register, and finally jumps to
194 * So to start a core executing we need to:
195 * - Encode the (hardware) CPU id with the bottom bits of the secondary
197 * - Write that value into the secondary boot register.
198 * - Generate an event to wake up the secondary CPU(s).
199 * - Wait for the secondary boot register to be re-written, which
200 * indicates the secondary core has started.
202 static int kona_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
204 void __iomem
*boot_reg
;
205 phys_addr_t boot_func
;
209 bool timeout
= false;
211 cpu_id
= cpu_logical_map(cpu
);
212 if (cpu_id
& ~BOOT_ADDR_CPUID_MASK
) {
213 pr_err("bad cpu id (%u > %u)\n", cpu_id
, BOOT_ADDR_CPUID_MASK
);
217 if (!secondary_boot_addr
) {
218 pr_err("required secondary boot register not specified\n");
222 boot_reg
= ioremap_nocache(
223 (phys_addr_t
)secondary_boot_addr
, sizeof(u32
));
225 pr_err("unable to map boot register for cpu %u\n", cpu_id
);
230 * Secondary cores will start in secondary_startup(),
231 * defined in "arch/arm/kernel/head.S"
233 boot_func
= virt_to_phys(secondary_startup
);
234 BUG_ON(boot_func
& BOOT_ADDR_CPUID_MASK
);
235 BUG_ON(boot_func
> (phys_addr_t
)U32_MAX
);
237 /* The core to start is encoded in the low bits */
238 boot_val
= (u32
)boot_func
| cpu_id
;
239 writel_relaxed(boot_val
, boot_reg
);
243 /* The low bits will be cleared once the core has started */
244 start_clock
= local_clock();
245 while (!timeout
&& readl_relaxed(boot_reg
) == boot_val
)
246 timeout
= local_clock() - start_clock
> SECONDARY_TIMEOUT_NS
;
253 pr_err("timeout waiting for cpu %u to start\n", cpu_id
);
258 static int nsp_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
263 * After wake up, secondary core branches to the startup
264 * address programmed at SKU ROM LUT location.
266 ret
= nsp_write_lut();
268 pr_err("unable to write startup addr to SKU ROM LUT\n");
272 /* Send a CPU wakeup interrupt to the secondary core */
273 arch_send_wakeup_ipi_mask(cpumask_of(cpu
));
279 static const struct smp_operations bcm_smp_ops __initconst
= {
280 .smp_prepare_cpus
= bcm_smp_prepare_cpus
,
281 .smp_boot_secondary
= kona_boot_secondary
,
283 CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx
, "brcm,bcm11351-cpu-method",
286 static const struct smp_operations nsp_smp_ops __initconst
= {
287 .smp_prepare_cpus
= bcm_smp_prepare_cpus
,
288 .smp_boot_secondary
= nsp_boot_secondary
,
290 CPU_METHOD_OF_DECLARE(bcm_smp_nsp
, "brcm,bcm-nsp-smp", &nsp_smp_ops
);