2 * Copyright (c) 2013-2014 Linaro Ltd.
3 * Copyright (c) 2013-2014 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/delay.h>
13 #include <linux/memblock.h>
14 #include <linux/of_address.h>
16 #include <asm/cputype.h>
18 #include <asm/cacheflush.h>
20 #include <asm/smp_plat.h>
24 /* bits definition in SC_CPU_RESET_REQ[x]/SC_CPU_RESET_DREQ[x]
25 * 1 -- unreset; 0 -- reset
27 #define CORE_RESET_BIT(x) (1 << x)
28 #define NEON_RESET_BIT(x) (1 << (x + 4))
29 #define CORE_DEBUG_RESET_BIT(x) (1 << (x + 9))
30 #define CLUSTER_L2_RESET_BIT (1 << 8)
31 #define CLUSTER_DEBUG_RESET_BIT (1 << 13)
34 * bits definition in SC_CPU_RESET_STATUS[x]
35 * 1 -- reset status; 0 -- unreset status
37 #define CORE_RESET_STATUS(x) (1 << x)
38 #define NEON_RESET_STATUS(x) (1 << (x + 4))
39 #define CORE_DEBUG_RESET_STATUS(x) (1 << (x + 9))
40 #define CLUSTER_L2_RESET_STATUS (1 << 8)
41 #define CLUSTER_DEBUG_RESET_STATUS (1 << 13)
42 #define CORE_WFI_STATUS(x) (1 << (x + 16))
43 #define CORE_WFE_STATUS(x) (1 << (x + 20))
44 #define CORE_DEBUG_ACK(x) (1 << (x + 24))
46 #define SC_CPU_RESET_REQ(x) (0x520 + (x << 3)) /* reset */
47 #define SC_CPU_RESET_DREQ(x) (0x524 + (x << 3)) /* unreset */
48 #define SC_CPU_RESET_STATUS(x) (0x1520 + (x << 3))
50 #define FAB_SF_MODE 0x0c
51 #define FAB_SF_INVLD 0x10
53 /* bits definition in FB_SF_INVLD */
54 #define FB_SF_INVLD_START (1 << 8)
56 #define HIP04_MAX_CLUSTERS 4
57 #define HIP04_MAX_CPUS_PER_CLUSTER 4
60 #define TIMEOUT_MSEC 1000
62 static void __iomem *sysctrl, *fabric;
63 static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
64 static DEFINE_SPINLOCK(boot_lock);
65 static u32 fabric_phys_addr;
67 * [0]: bootwrapper physical address
68 * [1]: bootwrapper size
69 * [2]: relocation address
70 * [3]: relocation size
72 static u32 hip04_boot_method[4];
74 static bool hip04_cluster_is_down(unsigned int cluster)
78 for (i = 0; i < HIP04_MAX_CPUS_PER_CLUSTER; i++)
79 if (hip04_cpu_table[cluster][i])
84 static void hip04_set_snoop_filter(unsigned int cluster, unsigned int on)
90 data = readl_relaxed(fabric + FAB_SF_MODE);
94 data &= ~(1 << cluster);
95 writel_relaxed(data, fabric + FAB_SF_MODE);
98 } while (data != readl_relaxed(fabric + FAB_SF_MODE));
101 static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
103 unsigned int mpidr, cpu, cluster;
105 void __iomem *sys_dreq, *sys_status;
107 mpidr = cpu_logical_map(l_cpu);
108 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
109 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
113 if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
116 spin_lock_irq(&boot_lock);
118 if (hip04_cpu_table[cluster][cpu])
121 sys_dreq = sysctrl + SC_CPU_RESET_DREQ(cluster);
122 sys_status = sysctrl + SC_CPU_RESET_STATUS(cluster);
123 if (hip04_cluster_is_down(cluster)) {
124 data = CLUSTER_DEBUG_RESET_BIT;
125 writel_relaxed(data, sys_dreq);
128 data = readl_relaxed(sys_status);
129 } while (data & CLUSTER_DEBUG_RESET_STATUS);
130 hip04_set_snoop_filter(cluster, 1);
133 data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
134 CORE_DEBUG_RESET_BIT(cpu);
135 writel_relaxed(data, sys_dreq);
138 } while (data == readl_relaxed(sys_status));
141 * We may fail to power up core again without this delay.
142 * It's not mentioned in document. It's found by test.
146 arch_send_wakeup_ipi_mask(cpumask_of(l_cpu));
149 hip04_cpu_table[cluster][cpu]++;
150 spin_unlock_irq(&boot_lock);
155 #ifdef CONFIG_HOTPLUG_CPU
156 static void hip04_cpu_die(unsigned int l_cpu)
158 unsigned int mpidr, cpu, cluster;
161 mpidr = cpu_logical_map(l_cpu);
162 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
163 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
165 spin_lock(&boot_lock);
166 hip04_cpu_table[cluster][cpu]--;
167 if (hip04_cpu_table[cluster][cpu] == 1) {
168 /* A power_up request went ahead of us. */
169 spin_unlock(&boot_lock);
171 } else if (hip04_cpu_table[cluster][cpu] > 1) {
172 pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
176 last_man = hip04_cluster_is_down(cluster);
177 spin_unlock(&boot_lock);
179 /* Since it's Cortex A15, disable L2 prefetching. */
181 "mcr p15, 1, %0, c15, c0, 3 \n\t"
185 v7_exit_coherency_flush(all);
187 v7_exit_coherency_flush(louis);
194 static int hip04_cpu_kill(unsigned int l_cpu)
196 unsigned int mpidr, cpu, cluster;
197 unsigned int data, tries, count;
199 mpidr = cpu_logical_map(l_cpu);
200 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
201 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
202 BUG_ON(cluster >= HIP04_MAX_CLUSTERS ||
203 cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
205 count = TIMEOUT_MSEC / POLL_MSEC;
206 spin_lock_irq(&boot_lock);
207 for (tries = 0; tries < count; tries++) {
208 if (hip04_cpu_table[cluster][cpu])
211 data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
212 if (data & CORE_WFI_STATUS(cpu))
214 spin_unlock_irq(&boot_lock);
215 /* Wait for clean L2 when the whole cluster is down. */
217 spin_lock_irq(&boot_lock);
221 data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
222 CORE_DEBUG_RESET_BIT(cpu);
223 writel_relaxed(data, sysctrl + SC_CPU_RESET_REQ(cluster));
224 for (tries = 0; tries < count; tries++) {
226 data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
227 if (data & CORE_RESET_STATUS(cpu))
232 if (hip04_cluster_is_down(cluster))
233 hip04_set_snoop_filter(cluster, 0);
234 spin_unlock_irq(&boot_lock);
237 spin_unlock_irq(&boot_lock);
242 static const struct smp_operations hip04_smp_ops __initconst = {
243 .smp_boot_secondary = hip04_boot_secondary,
244 #ifdef CONFIG_HOTPLUG_CPU
245 .cpu_die = hip04_cpu_die,
246 .cpu_kill = hip04_cpu_kill,
250 static bool __init hip04_cpu_table_init(void)
252 unsigned int mpidr, cpu, cluster;
254 mpidr = read_cpuid_mpidr();
255 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
256 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
258 if (cluster >= HIP04_MAX_CLUSTERS ||
259 cpu >= HIP04_MAX_CPUS_PER_CLUSTER) {
260 pr_err("%s: boot CPU is out of bound!\n", __func__);
263 hip04_set_snoop_filter(cluster, 1);
264 hip04_cpu_table[cluster][cpu] = 1;
268 static int __init hip04_smp_init(void)
270 struct device_node *np, *np_sctl, *np_fab;
271 struct resource fab_res;
272 void __iomem *relocation;
275 np = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-bootwrapper");
278 ret = of_property_read_u32_array(np, "boot-method",
279 &hip04_boot_method[0], 4);
284 np_sctl = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
287 np_fab = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-fabric");
291 ret = memblock_reserve(hip04_boot_method[0], hip04_boot_method[1]);
295 relocation = ioremap(hip04_boot_method[2], hip04_boot_method[3]);
297 pr_err("failed to map relocation space\n");
301 sysctrl = of_iomap(np_sctl, 0);
303 pr_err("failed to get sysctrl base\n");
307 ret = of_address_to_resource(np_fab, 0, &fab_res);
309 pr_err("failed to get fabric base phys\n");
312 fabric_phys_addr = fab_res.start;
313 sync_cache_w(&fabric_phys_addr);
314 fabric = of_iomap(np_fab, 0);
316 pr_err("failed to get fabric base\n");
321 if (!hip04_cpu_table_init()) {
327 * Fill the instruction address that is used after secondary core
330 writel_relaxed(hip04_boot_method[0], relocation);
331 writel_relaxed(0xa5a5a5a5, relocation + 4); /* magic number */
332 writel_relaxed(__pa_symbol(secondary_startup), relocation + 8);
333 writel_relaxed(0, relocation + 12);
336 smp_set_ops(&hip04_smp_ops);
345 memblock_free(hip04_boot_method[0], hip04_boot_method[1]);
349 early_initcall(hip04_smp_init);