1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/cpumask.h>
16#include <linux/delay.h>
17#include <linux/errno.h>
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/jiffies.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/sched.h>
24#include <linux/smp.h>
25
26#include <asm/cacheflush.h>
27#include <asm/smp.h>
28#include <asm/smp_plat.h>
29#include <asm/smp_scu.h>
30
31
32#define CORTEX_A9_SCU_SIZE 0x58
33
34#define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC
35#define BOOT_ADDR_CPUID_MASK 0x3
36
37
38#define OF_SECONDARY_BOOT "secondary-boot-reg"
39#define MPIDR_CPUID_BITMASK 0x3
40
41
42
43
44
45
46
47
48
49
50
51static int __init scu_a9_enable(void)
52{
53 unsigned long config_base;
54 void __iomem *scu_base;
55
56 if (!scu_a9_has_base()) {
57 pr_err("no configuration base address register!\n");
58 return -ENXIO;
59 }
60
61
62 config_base = scu_a9_get_base();
63 if (!config_base) {
64 pr_err("hardware reports only one core\n");
65 return -ENOENT;
66 }
67
68 scu_base = ioremap((phys_addr_t)config_base, CORTEX_A9_SCU_SIZE);
69 if (!scu_base) {
70 pr_err("failed to remap config base (%lu/%u) for SCU\n",
71 config_base, CORTEX_A9_SCU_SIZE);
72 return -ENOMEM;
73 }
74
75 scu_enable(scu_base);
76
77 iounmap(scu_base);
78
79 return 0;
80}
81
82static u32 secondary_boot_addr_for(unsigned int cpu)
83{
84 u32 secondary_boot_addr = 0;
85 struct device_node *cpu_node = of_get_cpu_node(cpu, NULL);
86
87 if (!cpu_node) {
88 pr_err("Failed to find device tree node for CPU%u\n", cpu);
89 return 0;
90 }
91
92 if (of_property_read_u32(cpu_node,
93 OF_SECONDARY_BOOT,
94 &secondary_boot_addr))
95 pr_err("required secondary boot register not specified for CPU%u\n",
96 cpu);
97
98 of_node_put(cpu_node);
99
100 return secondary_boot_addr;
101}
102
103static int nsp_write_lut(unsigned int cpu)
104{
105 void __iomem *sku_rom_lut;
106 phys_addr_t secondary_startup_phy;
107 const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
108
109 if (!secondary_boot_addr)
110 return -EINVAL;
111
112 sku_rom_lut = ioremap_nocache((phys_addr_t)secondary_boot_addr,
113 sizeof(phys_addr_t));
114 if (!sku_rom_lut) {
115 pr_warn("unable to ioremap SKU-ROM LUT register for cpu %u\n", cpu);
116 return -ENOMEM;
117 }
118
119 secondary_startup_phy = virt_to_phys(secondary_startup);
120 BUG_ON(secondary_startup_phy > (phys_addr_t)U32_MAX);
121
122 writel_relaxed(secondary_startup_phy, sku_rom_lut);
123
124
125 smp_wmb();
126
127 iounmap(sku_rom_lut);
128
129 return 0;
130}
131
132static void __init bcm_smp_prepare_cpus(unsigned int max_cpus)
133{
134 const cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
135
136
137 if (scu_a9_enable()) {
138
139 pr_warn("failed to enable A9 SCU - disabling SMP\n");
140 init_cpu_present(&only_cpu_0);
141 }
142}
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162static int kona_boot_secondary(unsigned int cpu, struct task_struct *idle)
163{
164 void __iomem *boot_reg;
165 phys_addr_t boot_func;
166 u64 start_clock;
167 u32 cpu_id;
168 u32 boot_val;
169 bool timeout = false;
170 const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
171
172 cpu_id = cpu_logical_map(cpu);
173 if (cpu_id & ~BOOT_ADDR_CPUID_MASK) {
174 pr_err("bad cpu id (%u > %u)\n", cpu_id, BOOT_ADDR_CPUID_MASK);
175 return -EINVAL;
176 }
177
178 if (!secondary_boot_addr)
179 return -EINVAL;
180
181 boot_reg = ioremap_nocache((phys_addr_t)secondary_boot_addr,
182 sizeof(phys_addr_t));
183 if (!boot_reg) {
184 pr_err("unable to map boot register for cpu %u\n", cpu_id);
185 return -ENOMEM;
186 }
187
188
189
190
191
192 boot_func = virt_to_phys(secondary_startup);
193 BUG_ON(boot_func & BOOT_ADDR_CPUID_MASK);
194 BUG_ON(boot_func > (phys_addr_t)U32_MAX);
195
196
197 boot_val = (u32)boot_func | cpu_id;
198 writel_relaxed(boot_val, boot_reg);
199
200 sev();
201
202
203 start_clock = local_clock();
204 while (!timeout && readl_relaxed(boot_reg) == boot_val)
205 timeout = local_clock() - start_clock > SECONDARY_TIMEOUT_NS;
206
207 iounmap(boot_reg);
208
209 if (!timeout)
210 return 0;
211
212 pr_err("timeout waiting for cpu %u to start\n", cpu_id);
213
214 return -ENXIO;
215}
216
217
218#define CDC_CMD 6
219#define CDC_CMD_OFFSET 0
220#define CDC_CMD_REG(cpu) (CDC_CMD_OFFSET + 4*(cpu))
221
222
223
224
225
226
227static int bcm23550_boot_secondary(unsigned int cpu, struct task_struct *idle)
228{
229 void __iomem *cdc_base;
230 struct device_node *dn;
231 char *name;
232 int ret;
233
234
235
236
237 name = "brcm,bcm23550-cdc";
238 dn = of_find_compatible_node(NULL, NULL, name);
239 if (!dn) {
240 pr_err("unable to find cdc node\n");
241 return -ENODEV;
242 }
243
244 cdc_base = of_iomap(dn, 0);
245 of_node_put(dn);
246
247 if (!cdc_base) {
248 pr_err("unable to remap cdc base register\n");
249 return -ENOMEM;
250 }
251
252
253 ret = kona_boot_secondary(cpu, idle);
254 if (ret)
255 goto out;
256
257
258
259
260 writel_relaxed(CDC_CMD, cdc_base + CDC_CMD_REG(cpu));
261
262out:
263 iounmap(cdc_base);
264
265 return ret;
266}
267
268static int nsp_boot_secondary(unsigned int cpu, struct task_struct *idle)
269{
270 int ret;
271
272
273
274
275
276 ret = nsp_write_lut(cpu);
277 if (ret) {
278 pr_err("unable to write startup addr to SKU ROM LUT\n");
279 goto out;
280 }
281
282
283 arch_send_wakeup_ipi_mask(cpumask_of(cpu));
284
285out:
286 return ret;
287}
288
289static const struct smp_operations kona_smp_ops __initconst = {
290 .smp_prepare_cpus = bcm_smp_prepare_cpus,
291 .smp_boot_secondary = kona_boot_secondary,
292};
293CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx, "brcm,bcm11351-cpu-method",
294 &kona_smp_ops);
295
296static const struct smp_operations bcm23550_smp_ops __initconst = {
297 .smp_boot_secondary = bcm23550_boot_secondary,
298};
299CPU_METHOD_OF_DECLARE(bcm_smp_bcm23550, "brcm,bcm23550",
300 &bcm23550_smp_ops);
301
302static const struct smp_operations nsp_smp_ops __initconst = {
303 .smp_prepare_cpus = bcm_smp_prepare_cpus,
304 .smp_boot_secondary = nsp_boot_secondary,
305};
306CPU_METHOD_OF_DECLARE(bcm_smp_nsp, "brcm,bcm-nsp-smp", &nsp_smp_ops);
307