1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013-2014 Linaro Ltd.
4 * Copyright (c) 2013-2014 HiSilicon Limited.
6 #include <linux/init.h>
8 #include <linux/delay.h>
10 #include <linux/memblock.h>
11 #include <linux/of_address.h>
13 #include <asm/cputype.h>
15 #include <asm/cacheflush.h>
17 #include <asm/smp_plat.h>
21 /* bits definition in SC_CPU_RESET_REQ[x]/SC_CPU_RESET_DREQ[x]
22 * 1 -- unreset; 0 -- reset
24 #define CORE_RESET_BIT(x) (1 << x)
25 #define NEON_RESET_BIT(x) (1 << (x + 4))
26 #define CORE_DEBUG_RESET_BIT(x) (1 << (x + 9))
27 #define CLUSTER_L2_RESET_BIT (1 << 8)
28 #define CLUSTER_DEBUG_RESET_BIT (1 << 13)
31 * bits definition in SC_CPU_RESET_STATUS[x]
32 * 1 -- reset status; 0 -- unreset status
34 #define CORE_RESET_STATUS(x) (1 << x)
35 #define NEON_RESET_STATUS(x) (1 << (x + 4))
36 #define CORE_DEBUG_RESET_STATUS(x) (1 << (x + 9))
37 #define CLUSTER_L2_RESET_STATUS (1 << 8)
38 #define CLUSTER_DEBUG_RESET_STATUS (1 << 13)
39 #define CORE_WFI_STATUS(x) (1 << (x + 16))
40 #define CORE_WFE_STATUS(x) (1 << (x + 20))
41 #define CORE_DEBUG_ACK(x) (1 << (x + 24))
43 #define SC_CPU_RESET_REQ(x) (0x520 + (x << 3)) /* reset */
44 #define SC_CPU_RESET_DREQ(x) (0x524 + (x << 3)) /* unreset */
45 #define SC_CPU_RESET_STATUS(x) (0x1520 + (x << 3))
47 #define FAB_SF_MODE 0x0c
48 #define FAB_SF_INVLD 0x10
50 /* bits definition in FB_SF_INVLD */
51 #define FB_SF_INVLD_START (1 << 8)
53 #define HIP04_MAX_CLUSTERS 4
54 #define HIP04_MAX_CPUS_PER_CLUSTER 4
57 #define TIMEOUT_MSEC 1000
59 static void __iomem *sysctrl, *fabric;
60 static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
61 static DEFINE_SPINLOCK(boot_lock);
62 static u32 fabric_phys_addr;
64 * [0]: bootwrapper physical address
65 * [1]: bootwrapper size
66 * [2]: relocation address
67 * [3]: relocation size
69 static u32 hip04_boot_method[4];
71 static bool hip04_cluster_is_down(unsigned int cluster)
75 for (i = 0; i < HIP04_MAX_CPUS_PER_CLUSTER; i++)
76 if (hip04_cpu_table[cluster][i])
81 static void hip04_set_snoop_filter(unsigned int cluster, unsigned int on)
87 data = readl_relaxed(fabric + FAB_SF_MODE);
91 data &= ~(1 << cluster);
92 writel_relaxed(data, fabric + FAB_SF_MODE);
95 } while (data != readl_relaxed(fabric + FAB_SF_MODE));
98 static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
100 unsigned int mpidr, cpu, cluster;
102 void __iomem *sys_dreq, *sys_status;
104 mpidr = cpu_logical_map(l_cpu);
105 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
106 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
110 if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
113 spin_lock_irq(&boot_lock);
115 if (hip04_cpu_table[cluster][cpu])
118 sys_dreq = sysctrl + SC_CPU_RESET_DREQ(cluster);
119 sys_status = sysctrl + SC_CPU_RESET_STATUS(cluster);
120 if (hip04_cluster_is_down(cluster)) {
121 data = CLUSTER_DEBUG_RESET_BIT;
122 writel_relaxed(data, sys_dreq);
125 data = readl_relaxed(sys_status);
126 } while (data & CLUSTER_DEBUG_RESET_STATUS);
127 hip04_set_snoop_filter(cluster, 1);
130 data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
131 CORE_DEBUG_RESET_BIT(cpu);
132 writel_relaxed(data, sys_dreq);
135 } while (data == readl_relaxed(sys_status));
138 * We may fail to power up core again without this delay.
139 * It's not mentioned in document. It's found by test.
143 arch_send_wakeup_ipi_mask(cpumask_of(l_cpu));
146 hip04_cpu_table[cluster][cpu]++;
147 spin_unlock_irq(&boot_lock);
152 #ifdef CONFIG_HOTPLUG_CPU
153 static void hip04_cpu_die(unsigned int l_cpu)
155 unsigned int mpidr, cpu, cluster;
158 mpidr = cpu_logical_map(l_cpu);
159 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
160 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
162 spin_lock(&boot_lock);
163 hip04_cpu_table[cluster][cpu]--;
164 if (hip04_cpu_table[cluster][cpu] == 1) {
165 /* A power_up request went ahead of us. */
166 spin_unlock(&boot_lock);
168 } else if (hip04_cpu_table[cluster][cpu] > 1) {
169 pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
173 last_man = hip04_cluster_is_down(cluster);
174 spin_unlock(&boot_lock);
176 /* Since it's Cortex A15, disable L2 prefetching. */
178 "mcr p15, 1, %0, c15, c0, 3 \n\t"
182 v7_exit_coherency_flush(all);
184 v7_exit_coherency_flush(louis);
191 static int hip04_cpu_kill(unsigned int l_cpu)
193 unsigned int mpidr, cpu, cluster;
194 unsigned int data, tries, count;
196 mpidr = cpu_logical_map(l_cpu);
197 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
198 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
199 BUG_ON(cluster >= HIP04_MAX_CLUSTERS ||
200 cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
202 count = TIMEOUT_MSEC / POLL_MSEC;
203 spin_lock_irq(&boot_lock);
204 for (tries = 0; tries < count; tries++) {
205 if (hip04_cpu_table[cluster][cpu])
208 data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
209 if (data & CORE_WFI_STATUS(cpu))
211 spin_unlock_irq(&boot_lock);
212 /* Wait for clean L2 when the whole cluster is down. */
214 spin_lock_irq(&boot_lock);
218 data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
219 CORE_DEBUG_RESET_BIT(cpu);
220 writel_relaxed(data, sysctrl + SC_CPU_RESET_REQ(cluster));
221 for (tries = 0; tries < count; tries++) {
223 data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
224 if (data & CORE_RESET_STATUS(cpu))
229 if (hip04_cluster_is_down(cluster))
230 hip04_set_snoop_filter(cluster, 0);
231 spin_unlock_irq(&boot_lock);
234 spin_unlock_irq(&boot_lock);
239 static const struct smp_operations hip04_smp_ops __initconst = {
240 .smp_boot_secondary = hip04_boot_secondary,
241 #ifdef CONFIG_HOTPLUG_CPU
242 .cpu_die = hip04_cpu_die,
243 .cpu_kill = hip04_cpu_kill,
247 static bool __init hip04_cpu_table_init(void)
249 unsigned int mpidr, cpu, cluster;
251 mpidr = read_cpuid_mpidr();
252 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
253 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
255 if (cluster >= HIP04_MAX_CLUSTERS ||
256 cpu >= HIP04_MAX_CPUS_PER_CLUSTER) {
257 pr_err("%s: boot CPU is out of bound!\n", __func__);
260 hip04_set_snoop_filter(cluster, 1);
261 hip04_cpu_table[cluster][cpu] = 1;
265 static int __init hip04_smp_init(void)
267 struct device_node *np, *np_sctl, *np_fab;
268 struct resource fab_res;
269 void __iomem *relocation;
272 np = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-bootwrapper");
275 ret = of_property_read_u32_array(np, "boot-method",
276 &hip04_boot_method[0], 4);
281 np_sctl = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
284 np_fab = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-fabric");
288 ret = memblock_reserve(hip04_boot_method[0], hip04_boot_method[1]);
292 relocation = ioremap(hip04_boot_method[2], hip04_boot_method[3]);
294 pr_err("failed to map relocation space\n");
298 sysctrl = of_iomap(np_sctl, 0);
300 pr_err("failed to get sysctrl base\n");
304 ret = of_address_to_resource(np_fab, 0, &fab_res);
306 pr_err("failed to get fabric base phys\n");
309 fabric_phys_addr = fab_res.start;
310 sync_cache_w(&fabric_phys_addr);
311 fabric = of_iomap(np_fab, 0);
313 pr_err("failed to get fabric base\n");
318 if (!hip04_cpu_table_init()) {
324 * Fill the instruction address that is used after secondary core
327 writel_relaxed(hip04_boot_method[0], relocation);
328 writel_relaxed(0xa5a5a5a5, relocation + 4); /* magic number */
329 writel_relaxed(__pa_symbol(secondary_startup), relocation + 8);
330 writel_relaxed(0, relocation + 12);
333 smp_set_ops(&hip04_smp_ops);
342 memblock_phys_free(hip04_boot_method[0], hip04_boot_method[1]);
346 early_initcall(hip04_smp_init);