blob: 727968d6a3e56a60005e1b129dda51505da573ff [file] [log] [blame]
Chen-Yu Tsai745373e2018-01-17 16:46:47 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018 Chen-Yu Tsai
4 *
5 * Chen-Yu Tsai <wens@csie.org>
6 *
7 * arch/arm/mach-sunxi/mc_smp.c
8 *
9 * Based on Allwinner code, arch/arm/mach-exynos/mcpm-exynos.c, and
10 * arch/arm/mach-hisi/platmcpm.c
11 * Cluster cache enable trampoline code adapted from MCPM framework
12 */
13
14#include <linux/arm-cci.h>
15#include <linux/cpu_pm.h>
16#include <linux/delay.h>
17#include <linux/io.h>
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +080018#include <linux/iopoll.h>
19#include <linux/irqchip/arm-gic.h>
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080020#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_device.h>
23#include <linux/smp.h>
24
25#include <asm/cacheflush.h>
26#include <asm/cp15.h>
27#include <asm/cputype.h>
28#include <asm/idmap.h>
29#include <asm/smp_plat.h>
30#include <asm/suspend.h>
31
32#define SUNXI_CPUS_PER_CLUSTER 4
33#define SUNXI_NR_CLUSTERS 2
34
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +080035#define POLL_USEC 100
36#define TIMEOUT_USEC 100000
37
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080038#define CPUCFG_CX_CTRL_REG0(c) (0x10 * (c))
39#define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(n) BIT(n)
40#define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL 0xf
41#define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7 BIT(4)
42#define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15 BIT(0)
43#define CPUCFG_CX_CTRL_REG1(c) (0x10 * (c) + 0x4)
44#define CPUCFG_CX_CTRL_REG1_ACINACTM BIT(0)
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +080045#define CPUCFG_CX_STATUS(c) (0x30 + 0x4 * (c))
46#define CPUCFG_CX_STATUS_STANDBYWFI(n) BIT(16 + (n))
47#define CPUCFG_CX_STATUS_STANDBYWFIL2 BIT(0)
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080048#define CPUCFG_CX_RST_CTRL(c) (0x80 + 0x4 * (c))
49#define CPUCFG_CX_RST_CTRL_DBG_SOC_RST BIT(24)
50#define CPUCFG_CX_RST_CTRL_ETM_RST(n) BIT(20 + (n))
51#define CPUCFG_CX_RST_CTRL_ETM_RST_ALL (0xf << 20)
52#define CPUCFG_CX_RST_CTRL_DBG_RST(n) BIT(16 + (n))
53#define CPUCFG_CX_RST_CTRL_DBG_RST_ALL (0xf << 16)
54#define CPUCFG_CX_RST_CTRL_H_RST BIT(12)
55#define CPUCFG_CX_RST_CTRL_L2_RST BIT(8)
56#define CPUCFG_CX_RST_CTRL_CX_RST(n) BIT(4 + (n))
57#define CPUCFG_CX_RST_CTRL_CORE_RST(n) BIT(n)
58
59#define PRCM_CPU_PO_RST_CTRL(c) (0x4 + 0x4 * (c))
60#define PRCM_CPU_PO_RST_CTRL_CORE(n) BIT(n)
61#define PRCM_CPU_PO_RST_CTRL_CORE_ALL 0xf
62#define PRCM_PWROFF_GATING_REG(c) (0x100 + 0x4 * (c))
63#define PRCM_PWROFF_GATING_REG_CLUSTER BIT(4)
64#define PRCM_PWROFF_GATING_REG_CORE(n) BIT(n)
65#define PRCM_PWR_SWITCH_REG(c, cpu) (0x140 + 0x10 * (c) + 0x4 * (cpu))
66#define PRCM_CPU_SOFT_ENTRY_REG 0x164
67
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +080068#define CPU0_SUPPORT_HOTPLUG_MAGIC0 0xFA50392F
69#define CPU0_SUPPORT_HOTPLUG_MAGIC1 0x790DCA3A
70
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080071static void __iomem *cpucfg_base;
72static void __iomem *prcm_base;
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +080073static void __iomem *sram_b_smp_base;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080074
Mylène Josseranddff052c2018-05-04 21:05:35 +020075extern void sunxi_mc_smp_secondary_startup(void);
76extern void sunxi_mc_smp_resume(void);
77
Chen-Yu Tsai745373e2018-01-17 16:46:47 +080078static bool sunxi_core_is_cortex_a15(unsigned int core, unsigned int cluster)
79{
80 struct device_node *node;
81 int cpu = cluster * SUNXI_CPUS_PER_CLUSTER + core;
82
83 node = of_cpu_device_node_get(cpu);
84
85 /* In case of_cpu_device_node_get fails */
86 if (!node)
87 node = of_get_cpu_node(cpu, NULL);
88
89 if (!node) {
90 /*
91 * There's no point in returning an error, since we
92 * would be mid way in a core or cluster power sequence.
93 */
94 pr_err("%s: Couldn't get CPU cluster %u core %u device node\n",
95 __func__, cluster, core);
96
97 return false;
98 }
99
100 return of_device_is_compatible(node, "arm,cortex-a15");
101}
102
103static int sunxi_cpu_power_switch_set(unsigned int cpu, unsigned int cluster,
104 bool enable)
105{
106 u32 reg;
107
108 /* control sequence from Allwinner A80 user manual v1.2 PRCM section */
109 reg = readl(prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
110 if (enable) {
111 if (reg == 0x00) {
112 pr_debug("power clamp for cluster %u cpu %u already open\n",
113 cluster, cpu);
114 return 0;
115 }
116
117 writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
118 udelay(10);
119 writel(0xfe, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
120 udelay(10);
121 writel(0xf8, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
122 udelay(10);
123 writel(0xf0, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
124 udelay(10);
125 writel(0x00, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
126 udelay(10);
127 } else {
128 writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
129 udelay(10);
130 }
131
132 return 0;
133}
134
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800135static void sunxi_cpu0_hotplug_support_set(bool enable)
136{
137 if (enable) {
138 writel(CPU0_SUPPORT_HOTPLUG_MAGIC0, sram_b_smp_base);
139 writel(CPU0_SUPPORT_HOTPLUG_MAGIC1, sram_b_smp_base + 0x4);
140 } else {
141 writel(0x0, sram_b_smp_base);
142 writel(0x0, sram_b_smp_base + 0x4);
143 }
144}
145
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800146static int sunxi_cpu_powerup(unsigned int cpu, unsigned int cluster)
147{
148 u32 reg;
149
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +0800150 pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800151 if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
152 return -EINVAL;
153
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800154 /* Set hotplug support magic flags for cpu0 */
155 if (cluster == 0 && cpu == 0)
156 sunxi_cpu0_hotplug_support_set(true);
157
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800158 /* assert processor power-on reset */
159 reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
160 reg &= ~PRCM_CPU_PO_RST_CTRL_CORE(cpu);
161 writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
162
163 /* Cortex-A7: hold L1 reset disable signal low */
164 if (!sunxi_core_is_cortex_a15(cpu, cluster)) {
165 reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
166 reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(cpu);
167 writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
168 }
169
170 /* assert processor related resets */
171 reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
172 reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
173
174 /*
175 * Allwinner code also asserts resets for NEON on A15. According
176 * to ARM manuals, asserting power-on reset is sufficient.
177 */
178 if (!sunxi_core_is_cortex_a15(cpu, cluster))
179 reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
180
181 writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
182
183 /* open power switch */
184 sunxi_cpu_power_switch_set(cpu, cluster, true);
185
186 /* clear processor power gate */
187 reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
188 reg &= ~PRCM_PWROFF_GATING_REG_CORE(cpu);
189 writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
190 udelay(20);
191
192 /* de-assert processor power-on reset */
193 reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
194 reg |= PRCM_CPU_PO_RST_CTRL_CORE(cpu);
195 writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
196
197 /* de-assert all processor resets */
198 reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
199 reg |= CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
200 reg |= CPUCFG_CX_RST_CTRL_CORE_RST(cpu);
201 if (!sunxi_core_is_cortex_a15(cpu, cluster))
202 reg |= CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
203 else
204 reg |= CPUCFG_CX_RST_CTRL_CX_RST(cpu); /* NEON */
205 writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
206
207 return 0;
208}
209
210static int sunxi_cluster_powerup(unsigned int cluster)
211{
212 u32 reg;
213
214 pr_debug("%s: cluster %u\n", __func__, cluster);
215 if (cluster >= SUNXI_NR_CLUSTERS)
216 return -EINVAL;
217
218 /* assert ACINACTM */
219 reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
220 reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
221 writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
222
223 /* assert cluster processor power-on resets */
224 reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
225 reg &= ~PRCM_CPU_PO_RST_CTRL_CORE_ALL;
226 writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
227
228 /* assert cluster resets */
229 reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
230 reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
231 reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST_ALL;
232 reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
233 reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
234
235 /*
236 * Allwinner code also asserts resets for NEON on A15. According
237 * to ARM manuals, asserting power-on reset is sufficient.
238 */
239 if (!sunxi_core_is_cortex_a15(0, cluster))
240 reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST_ALL;
241
242 writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
243
244 /* hold L1/L2 reset disable signals low */
245 reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
246 if (sunxi_core_is_cortex_a15(0, cluster)) {
247 /* Cortex-A15: hold L2RSTDISABLE low */
248 reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15;
249 } else {
250 /* Cortex-A7: hold L1RSTDISABLE and L2RSTDISABLE low */
251 reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL;
252 reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7;
253 }
254 writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
255
256 /* clear cluster power gate */
257 reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
258 reg &= ~PRCM_PWROFF_GATING_REG_CLUSTER;
259 writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
260 udelay(20);
261
262 /* de-assert cluster resets */
263 reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
264 reg |= CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
265 reg |= CPUCFG_CX_RST_CTRL_H_RST;
266 reg |= CPUCFG_CX_RST_CTRL_L2_RST;
267 writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
268
269 /* de-assert ACINACTM */
270 reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
271 reg &= ~CPUCFG_CX_CTRL_REG1_ACINACTM;
272 writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
273
274 return 0;
275}
276
277/*
278 * This bit is shared between the initial nocache_trampoline call to
279 * enable CCI-400 and proper cluster cache disable before power down.
280 */
281static void sunxi_cluster_cache_disable_without_axi(void)
282{
283 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
284 /*
285 * On the Cortex-A15 we need to disable
286 * L2 prefetching before flushing the cache.
287 */
288 asm volatile(
289 "mcr p15, 1, %0, c15, c0, 3\n"
290 "isb\n"
291 "dsb"
292 : : "r" (0x400));
293 }
294
295 /* Flush all cache levels for this cluster. */
296 v7_exit_coherency_flush(all);
297
298 /*
299 * Disable cluster-level coherency by masking
300 * incoming snoops and DVM messages:
301 */
302 cci_disable_port_by_cpu(read_cpuid_mpidr());
303}
304
305static int sunxi_mc_smp_cpu_table[SUNXI_NR_CLUSTERS][SUNXI_CPUS_PER_CLUSTER];
Mylène Josseranddff052c2018-05-04 21:05:35 +0200306int sunxi_mc_smp_first_comer;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800307
308static DEFINE_SPINLOCK(boot_lock);
309
310static bool sunxi_mc_smp_cluster_is_down(unsigned int cluster)
311{
312 int i;
313
314 for (i = 0; i < SUNXI_CPUS_PER_CLUSTER; i++)
315 if (sunxi_mc_smp_cpu_table[cluster][i])
316 return false;
317 return true;
318}
319
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800320static void sunxi_mc_smp_secondary_init(unsigned int cpu)
321{
322 /* Clear hotplug support magic flags for cpu0 */
323 if (cpu == 0)
324 sunxi_cpu0_hotplug_support_set(false);
325}
326
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800327static int sunxi_mc_smp_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
328{
329 unsigned int mpidr, cpu, cluster;
330
331 mpidr = cpu_logical_map(l_cpu);
332 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
333 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
334
335 if (!cpucfg_base)
336 return -ENODEV;
337 if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER)
338 return -EINVAL;
339
340 spin_lock_irq(&boot_lock);
341
342 if (sunxi_mc_smp_cpu_table[cluster][cpu])
343 goto out;
344
345 if (sunxi_mc_smp_cluster_is_down(cluster)) {
346 sunxi_mc_smp_first_comer = true;
347 sunxi_cluster_powerup(cluster);
348 } else {
349 sunxi_mc_smp_first_comer = false;
350 }
351
352 /* This is read by incoming CPUs with their cache and MMU disabled */
353 sync_cache_w(&sunxi_mc_smp_first_comer);
354 sunxi_cpu_powerup(cpu, cluster);
355
356out:
357 sunxi_mc_smp_cpu_table[cluster][cpu]++;
358 spin_unlock_irq(&boot_lock);
359
360 return 0;
361}
362
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +0800363#ifdef CONFIG_HOTPLUG_CPU
364static void sunxi_cluster_cache_disable(void)
365{
366 unsigned int cluster = MPIDR_AFFINITY_LEVEL(read_cpuid_mpidr(), 1);
367 u32 reg;
368
369 pr_debug("%s: cluster %u\n", __func__, cluster);
370
371 sunxi_cluster_cache_disable_without_axi();
372
373 /* last man standing, assert ACINACTM */
374 reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
375 reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
376 writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
377}
378
379static void sunxi_mc_smp_cpu_die(unsigned int l_cpu)
380{
381 unsigned int mpidr, cpu, cluster;
382 bool last_man;
383
384 mpidr = cpu_logical_map(l_cpu);
385 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
386 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
387 pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
388
389 spin_lock(&boot_lock);
390 sunxi_mc_smp_cpu_table[cluster][cpu]--;
391 if (sunxi_mc_smp_cpu_table[cluster][cpu] == 1) {
392 /* A power_up request went ahead of us. */
393 pr_debug("%s: aborting due to a power up request\n",
394 __func__);
395 spin_unlock(&boot_lock);
396 return;
397 } else if (sunxi_mc_smp_cpu_table[cluster][cpu] > 1) {
398 pr_err("Cluster %d CPU%d boots multiple times\n",
399 cluster, cpu);
400 BUG();
401 }
402
403 last_man = sunxi_mc_smp_cluster_is_down(cluster);
404 spin_unlock(&boot_lock);
405
406 gic_cpu_if_down(0);
407 if (last_man)
408 sunxi_cluster_cache_disable();
409 else
410 v7_exit_coherency_flush(louis);
411
412 for (;;)
413 wfi();
414}
415
416static int sunxi_cpu_powerdown(unsigned int cpu, unsigned int cluster)
417{
418 u32 reg;
419
420 pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
421 if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
422 return -EINVAL;
423
424 /* gate processor power */
425 reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
426 reg |= PRCM_PWROFF_GATING_REG_CORE(cpu);
427 writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
428 udelay(20);
429
430 /* close power switch */
431 sunxi_cpu_power_switch_set(cpu, cluster, false);
432
433 return 0;
434}
435
436static int sunxi_cluster_powerdown(unsigned int cluster)
437{
438 u32 reg;
439
440 pr_debug("%s: cluster %u\n", __func__, cluster);
441 if (cluster >= SUNXI_NR_CLUSTERS)
442 return -EINVAL;
443
444 /* assert cluster resets or system will hang */
445 pr_debug("%s: assert cluster reset\n", __func__);
446 reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
447 reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
448 reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
449 reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
450 writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
451
452 /* gate cluster power */
453 pr_debug("%s: gate cluster power\n", __func__);
454 reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
455 reg |= PRCM_PWROFF_GATING_REG_CLUSTER;
456 writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
457 udelay(20);
458
459 return 0;
460}
461
462static int sunxi_mc_smp_cpu_kill(unsigned int l_cpu)
463{
464 unsigned int mpidr, cpu, cluster;
465 unsigned int tries, count;
466 int ret = 0;
467 u32 reg;
468
469 mpidr = cpu_logical_map(l_cpu);
470 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
471 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
472
473 /* This should never happen */
474 if (WARN_ON(cluster >= SUNXI_NR_CLUSTERS ||
475 cpu >= SUNXI_CPUS_PER_CLUSTER))
476 return 0;
477
478 /* wait for CPU core to die and enter WFI */
479 count = TIMEOUT_USEC / POLL_USEC;
480 spin_lock_irq(&boot_lock);
481 for (tries = 0; tries < count; tries++) {
482 spin_unlock_irq(&boot_lock);
483 usleep_range(POLL_USEC / 2, POLL_USEC);
484 spin_lock_irq(&boot_lock);
485
486 /*
487 * If the user turns off a bunch of cores at the same
488 * time, the kernel might call cpu_kill before some of
489 * them are ready. This is because boot_lock serializes
490 * both cpu_die and cpu_kill callbacks. Either one could
491 * run first. We should wait for cpu_die to complete.
492 */
493 if (sunxi_mc_smp_cpu_table[cluster][cpu])
494 continue;
495
496 reg = readl(cpucfg_base + CPUCFG_CX_STATUS(cluster));
497 if (reg & CPUCFG_CX_STATUS_STANDBYWFI(cpu))
498 break;
499 }
500
501 if (tries >= count) {
502 ret = ETIMEDOUT;
503 goto out;
504 }
505
506 /* power down CPU core */
507 sunxi_cpu_powerdown(cpu, cluster);
508
509 if (!sunxi_mc_smp_cluster_is_down(cluster))
510 goto out;
511
512 /* wait for cluster L2 WFI */
513 ret = readl_poll_timeout(cpucfg_base + CPUCFG_CX_STATUS(cluster), reg,
514 reg & CPUCFG_CX_STATUS_STANDBYWFIL2,
515 POLL_USEC, TIMEOUT_USEC);
516 if (ret) {
517 /*
518 * Ignore timeout on the cluster. Leaving the cluster on
519 * will not affect system execution, just use a bit more
520 * power. But returning an error here will only confuse
521 * the user as the CPU has already been shutdown.
522 */
523 ret = 0;
524 goto out;
525 }
526
527 /* Power down cluster */
528 sunxi_cluster_powerdown(cluster);
529
530out:
531 spin_unlock_irq(&boot_lock);
532 pr_debug("%s: cluster %u cpu %u powerdown: %d\n",
533 __func__, cluster, cpu, ret);
534 return !ret;
535}
536
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800537static bool sunxi_mc_smp_cpu_can_disable(unsigned int __unused)
538{
539 return true;
540}
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +0800541#endif
542
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800543static const struct smp_operations sunxi_mc_smp_smp_ops __initconst = {
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800544 .smp_secondary_init = sunxi_mc_smp_secondary_init,
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800545 .smp_boot_secondary = sunxi_mc_smp_boot_secondary,
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +0800546#ifdef CONFIG_HOTPLUG_CPU
547 .cpu_die = sunxi_mc_smp_cpu_die,
548 .cpu_kill = sunxi_mc_smp_cpu_kill,
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800549 .cpu_can_disable = sunxi_mc_smp_cpu_can_disable,
Chen-Yu Tsai7cbea632018-01-17 16:46:51 +0800550#endif
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800551};
552
553static bool __init sunxi_mc_smp_cpu_table_init(void)
554{
555 unsigned int mpidr, cpu, cluster;
556
557 mpidr = read_cpuid_mpidr();
558 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
559 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
560
561 if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER) {
562 pr_err("%s: boot CPU is out of bounds!\n", __func__);
563 return false;
564 }
565 sunxi_mc_smp_cpu_table[cluster][cpu] = 1;
566 return true;
567}
568
569/*
570 * Adapted from arch/arm/common/mc_smp_entry.c
571 *
572 * We need the trampoline code to enable CCI-400 on the first cluster
573 */
574typedef typeof(cpu_reset) phys_reset_t;
575
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800576static int __init nocache_trampoline(unsigned long __unused)
577{
578 phys_reset_t phys_reset;
579
580 setup_mm_for_reboot();
581 sunxi_cluster_cache_disable_without_axi();
582
583 phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
584 phys_reset(__pa_symbol(sunxi_mc_smp_resume), false);
585 BUG();
586}
587
Chen-Yu Tsaic161dae2018-03-08 23:00:09 +0800588static int __init sunxi_mc_smp_loopback(void)
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800589{
590 int ret;
591
592 /*
593 * We're going to soft-restart the current CPU through the
594 * low-level MCPM code by leveraging the suspend/resume
595 * infrastructure. Let's play it safe by using cpu_pm_enter()
596 * in case the CPU init code path resets the VFP or similar.
597 */
598 sunxi_mc_smp_first_comer = true;
599 local_irq_disable();
600 local_fiq_disable();
601 ret = cpu_pm_enter();
602 if (!ret) {
603 ret = cpu_suspend(0, nocache_trampoline);
604 cpu_pm_exit();
605 }
606 local_fiq_enable();
607 local_irq_enable();
608 sunxi_mc_smp_first_comer = false;
609
610 return ret;
611}
612
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800613/*
614 * This holds any device nodes that we requested resources for,
615 * so that we may easily release resources in the error path.
616 */
617struct sunxi_mc_smp_nodes {
618 struct device_node *prcm_node;
619 struct device_node *cpucfg_node;
620 struct device_node *sram_node;
621};
622
623/* This structure holds SoC-specific bits tied to an enable-method string. */
624struct sunxi_mc_smp_data {
625 const char *enable_method;
626 int (*get_smp_nodes)(struct sunxi_mc_smp_nodes *nodes);
627};
628
629static void __init sunxi_mc_smp_put_nodes(struct sunxi_mc_smp_nodes *nodes)
630{
631 of_node_put(nodes->prcm_node);
632 of_node_put(nodes->cpucfg_node);
633 of_node_put(nodes->sram_node);
634 memset(nodes, 0, sizeof(*nodes));
635}
636
637static int __init sun9i_a80_get_smp_nodes(struct sunxi_mc_smp_nodes *nodes)
638{
639 nodes->prcm_node = of_find_compatible_node(NULL, NULL,
640 "allwinner,sun9i-a80-prcm");
641 if (!nodes->prcm_node) {
642 pr_err("%s: PRCM not available\n", __func__);
643 return -ENODEV;
644 }
645
646 nodes->cpucfg_node = of_find_compatible_node(NULL, NULL,
647 "allwinner,sun9i-a80-cpucfg");
648 if (!nodes->cpucfg_node) {
649 pr_err("%s: CPUCFG not available\n", __func__);
650 return -ENODEV;
651 }
652
653 nodes->sram_node = of_find_compatible_node(NULL, NULL,
654 "allwinner,sun9i-a80-smp-sram");
655 if (!nodes->sram_node) {
656 pr_err("%s: Secure SRAM not available\n", __func__);
657 return -ENODEV;
658 }
659
660 return 0;
661}
662
663static const struct sunxi_mc_smp_data sunxi_mc_smp_data[] __initconst = {
664 {
665 .enable_method = "allwinner,sun9i-a80-smp",
666 .get_smp_nodes = sun9i_a80_get_smp_nodes,
667 },
668};
669
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800670static int __init sunxi_mc_smp_init(void)
671{
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800672 struct sunxi_mc_smp_nodes nodes = { 0 };
673 struct device_node *node;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800674 struct resource res;
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800675 int i, ret;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800676
Chen-Yu Tsai85917432018-03-08 23:00:10 +0800677 /*
678 * Don't bother checking the "cpus" node, as an enable-method
679 * property in that node is undocumented.
680 */
681 node = of_cpu_device_node_get(0);
682 if (!node)
683 return -ENODEV;
684
685 /*
686 * We can't actually use the enable-method magic in the kernel.
687 * Our loopback / trampoline code uses the CPU suspend framework,
688 * which requires the identity mapping be available. It would not
689 * yet be available if we used the .init_cpus or .prepare_cpus
690 * callbacks in smp_operations, which we would use if we were to
691 * use CPU_METHOD_OF_DECLARE
692 */
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800693 for (i = 0; i < ARRAY_SIZE(sunxi_mc_smp_data); i++) {
694 ret = of_property_match_string(node, "enable-method",
695 sunxi_mc_smp_data[i].enable_method);
696 if (!ret)
697 break;
698 }
699
Chen-Yu Tsai85917432018-03-08 23:00:10 +0800700 of_node_put(node);
701 if (ret)
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800702 return -ENODEV;
703
704 if (!sunxi_mc_smp_cpu_table_init())
705 return -EINVAL;
706
707 if (!cci_probed()) {
708 pr_err("%s: CCI-400 not available\n", __func__);
709 return -ENODEV;
710 }
711
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800712 /* Get needed device tree nodes */
713 ret = sunxi_mc_smp_data[i].get_smp_nodes(&nodes);
714 if (ret)
715 goto err_put_nodes;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800716
717 /*
718 * Unfortunately we can not request the I/O region for the PRCM.
719 * It is shared with the PRCM clock.
720 */
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800721 prcm_base = of_iomap(nodes.prcm_node, 0);
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800722 if (!prcm_base) {
723 pr_err("%s: failed to map PRCM registers\n", __func__);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800724 ret = -ENOMEM;
725 goto err_put_nodes;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800726 }
727
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800728 cpucfg_base = of_io_request_and_map(nodes.cpucfg_node, 0,
729 "sunxi-mc-smp");
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800730 if (IS_ERR(cpucfg_base)) {
731 ret = PTR_ERR(cpucfg_base);
732 pr_err("%s: failed to map CPUCFG registers: %d\n",
733 __func__, ret);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800734 goto err_unmap_prcm;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800735 }
736
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800737 sram_b_smp_base = of_io_request_and_map(nodes.sram_node, 0,
738 "sunxi-mc-smp");
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800739 if (IS_ERR(sram_b_smp_base)) {
740 ret = PTR_ERR(sram_b_smp_base);
741 pr_err("%s: failed to map secure SRAM\n", __func__);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800742 goto err_unmap_release_cpucfg;
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800743 }
744
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800745 /* Configure CCI-400 for boot cluster */
Chen-Yu Tsaic161dae2018-03-08 23:00:09 +0800746 ret = sunxi_mc_smp_loopback();
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800747 if (ret) {
748 pr_err("%s: failed to configure boot cluster: %d\n",
749 __func__, ret);
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800750 goto err_unmap_release_secure_sram;
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800751 }
752
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800753 /* We don't need the device nodes anymore */
754 sunxi_mc_smp_put_nodes(&nodes);
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800755
756 /* Set the hardware entry point address */
757 writel(__pa_symbol(sunxi_mc_smp_secondary_startup),
758 prcm_base + PRCM_CPU_SOFT_ENTRY_REG);
759
760 /* Actually enable multi cluster SMP */
761 smp_set_ops(&sunxi_mc_smp_smp_ops);
762
763 pr_info("sunxi multi cluster SMP support installed\n");
764
765 return 0;
766
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800767err_unmap_release_secure_sram:
768 iounmap(sram_b_smp_base);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800769 of_address_to_resource(nodes.sram_node, 0, &res);
Chen-Yu Tsai8eaa0642018-01-17 16:46:53 +0800770 release_mem_region(res.start, resource_size(&res));
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800771err_unmap_release_cpucfg:
772 iounmap(cpucfg_base);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800773 of_address_to_resource(nodes.cpucfg_node, 0, &res);
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800774 release_mem_region(res.start, resource_size(&res));
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800775err_unmap_prcm:
776 iounmap(prcm_base);
Chen-Yu Tsai99e3a1e62018-03-08 23:00:12 +0800777err_put_nodes:
778 sunxi_mc_smp_put_nodes(&nodes);
Chen-Yu Tsai745373e2018-01-17 16:46:47 +0800779 return ret;
780}
781
782early_initcall(sunxi_mc_smp_init);