cpu_fdt.c revision 1.12 1 1.12 ryo /* $NetBSD: cpu_fdt.c,v 1.12 2018/09/10 11:05:12 ryo Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.12 ryo #include "opt_multiprocessor.h"
30 1.12 ryo #include "psci_fdt.h"
31 1.12 ryo
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.12 ryo __KERNEL_RCSID(0, "$NetBSD: cpu_fdt.c,v 1.12 2018/09/10 11:05:12 ryo Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.12 ryo #include <sys/atomic.h>
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.4 skrll #include <sys/lwp.h>
40 1.1 jmcneill #include <sys/systm.h>
41 1.1 jmcneill #include <sys/kernel.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill
45 1.5 ryo #include <arm/armreg.h>
46 1.1 jmcneill #include <arm/cpu.h>
47 1.5 ryo #include <arm/cpufunc.h>
48 1.12 ryo #include <arm/locore.h>
49 1.12 ryo
50 1.12 ryo #include <arm/arm/psci.h>
51 1.12 ryo #include <arm/fdt/arm_fdtvar.h>
52 1.12 ryo #include <arm/fdt/psci_fdtvar.h>
53 1.12 ryo
54 1.12 ryo #include <uvm/uvm_extern.h>
55 1.1 jmcneill
56 1.1 jmcneill static int cpu_fdt_match(device_t, cfdata_t, void *);
57 1.1 jmcneill static void cpu_fdt_attach(device_t, device_t, void *);
58 1.1 jmcneill
59 1.3 jmcneill enum cpu_fdt_type {
60 1.3 jmcneill ARM_CPU_UP = 1,
61 1.3 jmcneill ARM_CPU_ARMV7,
62 1.3 jmcneill ARM_CPU_ARMV8,
63 1.3 jmcneill };
64 1.3 jmcneill
65 1.1 jmcneill struct cpu_fdt_softc {
66 1.1 jmcneill device_t sc_dev;
67 1.1 jmcneill int sc_phandle;
68 1.1 jmcneill };
69 1.1 jmcneill
70 1.3 jmcneill static const struct of_compat_data compat_data[] = {
71 1.3 jmcneill { "arm,arm1176jzf-s", ARM_CPU_UP },
72 1.3 jmcneill
73 1.6 jakllsch { "arm,arm-v7", ARM_CPU_ARMV7 },
74 1.3 jmcneill { "arm,cortex-a5", ARM_CPU_ARMV7 },
75 1.3 jmcneill { "arm,cortex-a7", ARM_CPU_ARMV7 },
76 1.3 jmcneill { "arm,cortex-a8", ARM_CPU_ARMV7 },
77 1.3 jmcneill { "arm,cortex-a9", ARM_CPU_ARMV7 },
78 1.3 jmcneill { "arm,cortex-a12", ARM_CPU_ARMV7 },
79 1.3 jmcneill { "arm,cortex-a15", ARM_CPU_ARMV7 },
80 1.3 jmcneill { "arm,cortex-a17", ARM_CPU_ARMV7 },
81 1.3 jmcneill
82 1.11 jmcneill { "arm,armv8", ARM_CPU_ARMV8 },
83 1.3 jmcneill { "arm,cortex-a53", ARM_CPU_ARMV8 },
84 1.3 jmcneill { "arm,cortex-a57", ARM_CPU_ARMV8 },
85 1.3 jmcneill { "arm,cortex-a72", ARM_CPU_ARMV8 },
86 1.3 jmcneill { "arm,cortex-a73", ARM_CPU_ARMV8 },
87 1.7 jmcneill
88 1.3 jmcneill { NULL }
89 1.3 jmcneill };
90 1.3 jmcneill
91 1.1 jmcneill CFATTACH_DECL_NEW(cpu_fdt, sizeof(struct cpu_fdt_softc),
92 1.1 jmcneill cpu_fdt_match, cpu_fdt_attach, NULL, NULL);
93 1.1 jmcneill
94 1.1 jmcneill static int
95 1.1 jmcneill cpu_fdt_match(device_t parent, cfdata_t cf, void *aux)
96 1.1 jmcneill {
97 1.1 jmcneill struct fdt_attach_args * const faa = aux;
98 1.3 jmcneill const int phandle = faa->faa_phandle;
99 1.3 jmcneill enum cpu_fdt_type type;
100 1.2 jmcneill int is_compatible;
101 1.2 jmcneill bus_addr_t mpidr;
102 1.2 jmcneill
103 1.3 jmcneill is_compatible = of_match_compat_data(phandle, compat_data);
104 1.2 jmcneill if (!is_compatible)
105 1.2 jmcneill return 0;
106 1.2 jmcneill
107 1.3 jmcneill type = of_search_compatible(phandle, compat_data)->data;
108 1.3 jmcneill switch (type) {
109 1.3 jmcneill case ARM_CPU_ARMV7:
110 1.3 jmcneill case ARM_CPU_ARMV8:
111 1.3 jmcneill if (fdtbus_get_reg(phandle, 0, &mpidr, NULL) != 0)
112 1.3 jmcneill return 0;
113 1.5 ryo
114 1.9 ryo #ifndef __aarch64__
115 1.9 ryo /* XXX NetBSD/arm requires all CPUs to be in the same cluster */
116 1.5 ryo const u_int bp_clid = cpu_clusterid();
117 1.5 ryo const u_int clid = __SHIFTOUT(mpidr, MPIDR_AFF1);
118 1.5 ryo
119 1.3 jmcneill if (bp_clid != clid)
120 1.3 jmcneill return 0;
121 1.9 ryo #endif
122 1.3 jmcneill break;
123 1.3 jmcneill default:
124 1.3 jmcneill break;
125 1.3 jmcneill }
126 1.1 jmcneill
127 1.2 jmcneill return is_compatible;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static void
131 1.1 jmcneill cpu_fdt_attach(device_t parent, device_t self, void *aux)
132 1.1 jmcneill {
133 1.1 jmcneill struct cpu_fdt_softc * const sc = device_private(self);
134 1.1 jmcneill struct fdt_attach_args * const faa = aux;
135 1.3 jmcneill const int phandle = faa->faa_phandle;
136 1.3 jmcneill enum cpu_fdt_type type;
137 1.1 jmcneill bus_addr_t mpidr;
138 1.3 jmcneill cpuid_t cpuid;
139 1.1 jmcneill
140 1.1 jmcneill sc->sc_dev = self;
141 1.3 jmcneill sc->sc_phandle = phandle;
142 1.3 jmcneill
143 1.3 jmcneill type = of_search_compatible(phandle, compat_data)->data;
144 1.1 jmcneill
145 1.3 jmcneill switch (type) {
146 1.3 jmcneill case ARM_CPU_ARMV7:
147 1.3 jmcneill case ARM_CPU_ARMV8:
148 1.3 jmcneill if (fdtbus_get_reg(phandle, 0, &mpidr, NULL) != 0) {
149 1.3 jmcneill aprint_error(": missing 'reg' property\n");
150 1.3 jmcneill return;
151 1.3 jmcneill }
152 1.9 ryo #ifndef __aarch64__
153 1.9 ryo mpidr = __SHIFTOUT(mpidr, MPIDR_AFF0);
154 1.9 ryo #endif
155 1.9 ryo cpuid = mpidr;
156 1.3 jmcneill break;
157 1.3 jmcneill default:
158 1.3 jmcneill cpuid = 0;
159 1.3 jmcneill break;
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.2 jmcneill /* Attach the CPU */
163 1.3 jmcneill cpu_attach(self, cpuid);
164 1.8 jmcneill
165 1.8 jmcneill /* Attach CPU frequency scaling provider */
166 1.8 jmcneill config_found(self, faa, NULL);
167 1.1 jmcneill }
168 1.12 ryo
169 1.12 ryo #ifdef MULTIPROCESSOR
170 1.12 ryo static register_t
171 1.12 ryo cpu_fdt_mpstart_pa(void)
172 1.12 ryo {
173 1.12 ryo #ifdef __aarch64__
174 1.12 ryo extern void aarch64_mpstart(void);
175 1.12 ryo return (register_t)aarch64_kern_vtophys((vaddr_t)aarch64_mpstart);
176 1.12 ryo #else
177 1.12 ryo extern void cortex_mpstart(void);
178 1.12 ryo return (register_t)cortex_mpstart;
179 1.12 ryo #endif
180 1.12 ryo }
181 1.12 ryo
182 1.12 ryo static int
183 1.12 ryo spintable_cpu_on(u_int cpuindex, paddr_t entry_point_address, paddr_t cpu_release_addr)
184 1.12 ryo {
185 1.12 ryo /*
186 1.12 ryo * we need devmap for cpu-release-addr in advance.
187 1.12 ryo * __HAVE_MM_MD_DIRECT_MAPPED_PHYS nor pmap didn't work at this point.
188 1.12 ryo */
189 1.12 ryo if (pmap_devmap_find_pa(cpu_release_addr, sizeof(paddr_t)) == NULL) {
190 1.12 ryo aprint_error("%s: devmap for cpu-release-addr"
191 1.12 ryo " 0x%08"PRIxPADDR" required\n", __func__, cpu_release_addr);
192 1.12 ryo return -1;
193 1.12 ryo } else {
194 1.12 ryo extern struct bus_space arm_generic_bs_tag;
195 1.12 ryo bus_space_handle_t ioh;
196 1.12 ryo
197 1.12 ryo bus_space_map(&arm_generic_bs_tag, cpu_release_addr,
198 1.12 ryo sizeof(paddr_t), 0, &ioh);
199 1.12 ryo bus_space_write_4(&arm_generic_bs_tag, ioh, 0,
200 1.12 ryo entry_point_address);
201 1.12 ryo bus_space_unmap(&arm_generic_bs_tag, ioh, sizeof(paddr_t));
202 1.12 ryo }
203 1.12 ryo
204 1.12 ryo return 0;
205 1.12 ryo }
206 1.12 ryo #endif /* MULTIPROCESSOR */
207 1.12 ryo
208 1.12 ryo
209 1.12 ryo void
210 1.12 ryo arm_fdt_cpu_bootstrap(void)
211 1.12 ryo {
212 1.12 ryo #ifdef MULTIPROCESSOR
213 1.12 ryo uint64_t mpidr, bp_mpidr;
214 1.12 ryo u_int cpuindex;
215 1.12 ryo int child, ret;
216 1.12 ryo const char *devtype, *method;
217 1.12 ryo
218 1.12 ryo const int cpus = OF_finddevice("/cpus");
219 1.12 ryo if (cpus == -1) {
220 1.12 ryo aprint_error("%s: no /cpus node found\n", __func__);
221 1.12 ryo arm_cpu_max = 1;
222 1.12 ryo return;
223 1.12 ryo }
224 1.12 ryo
225 1.12 ryo /* Count CPUs */
226 1.12 ryo arm_cpu_max = 0;
227 1.12 ryo for (child = OF_child(cpus); child; child = OF_peer(child))
228 1.12 ryo if (fdtbus_status_okay(child) && ((devtype =
229 1.12 ryo fdtbus_get_string(child, "device_type")) != NULL) &&
230 1.12 ryo (strcmp(devtype, "cpu") == 0))
231 1.12 ryo arm_cpu_max++;
232 1.12 ryo
233 1.12 ryo #if NPSCI_FDT > 0
234 1.12 ryo if (psci_fdt_preinit() != 0)
235 1.12 ryo return;
236 1.12 ryo #endif
237 1.12 ryo
238 1.12 ryo /* MPIDR affinity levels of boot processor. */
239 1.12 ryo bp_mpidr = cpu_mpidr_aff_read();
240 1.12 ryo
241 1.12 ryo /* Boot APs */
242 1.12 ryo uint32_t started = 0;
243 1.12 ryo cpuindex = 1;
244 1.12 ryo for (child = OF_child(cpus); child; child = OF_peer(child)) {
245 1.12 ryo if (!fdtbus_status_okay(child))
246 1.12 ryo continue;
247 1.12 ryo if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
248 1.12 ryo continue;
249 1.12 ryo if (mpidr == bp_mpidr)
250 1.12 ryo continue; /* BP already started */
251 1.12 ryo
252 1.12 ryo #ifdef __arm__
253 1.12 ryo /* XXX NetBSD/arm requires all CPUs to be in the same cluster */
254 1.12 ryo if ((mpidr & ~MPIDR_AFF0) != (bp_mpidr & ~MPIDR_AFF0))
255 1.12 ryo continue;
256 1.12 ryo #endif
257 1.12 ryo
258 1.12 ryo #ifdef __aarch64__
259 1.12 ryo KASSERT(cpuindex < MAXCPUS);
260 1.12 ryo cpu_mpidr[cpuindex] = mpidr;
261 1.12 ryo cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex], sizeof(cpu_mpidr[cpuindex]));
262 1.12 ryo #endif
263 1.12 ryo
264 1.12 ryo method = fdtbus_get_string(child, "enable-method");
265 1.12 ryo if (method == NULL)
266 1.12 ryo continue;
267 1.12 ryo
268 1.12 ryo if (strcmp(method, "spin-table") == 0) {
269 1.12 ryo uint64_t data;
270 1.12 ryo paddr_t cpu_release_addr;
271 1.12 ryo
272 1.12 ryo if (OF_getprop(child, "cpu-release-addr", &data,
273 1.12 ryo sizeof(data)) != sizeof(data))
274 1.12 ryo continue;
275 1.12 ryo
276 1.12 ryo cpu_release_addr = (paddr_t)be64toh(data);
277 1.12 ryo ret = spintable_cpu_on(mpidr, cpu_fdt_mpstart_pa(), cpu_release_addr);
278 1.12 ryo if (ret != 0)
279 1.12 ryo continue;
280 1.12 ryo
281 1.12 ryo #if NPSCI_FDT > 0
282 1.12 ryo } else if (strcmp(method, "psci") == 0) {
283 1.12 ryo ret = psci_cpu_on(mpidr, cpu_fdt_mpstart_pa(), 0);
284 1.12 ryo if (ret != PSCI_SUCCESS)
285 1.12 ryo continue;
286 1.12 ryo #endif
287 1.12 ryo } else {
288 1.12 ryo aprint_error("%s: %s: unsupported method\n", __func__, method);
289 1.12 ryo continue;
290 1.12 ryo }
291 1.12 ryo
292 1.12 ryo started |= __BIT(cpuindex);
293 1.12 ryo cpuindex++;
294 1.12 ryo }
295 1.12 ryo
296 1.12 ryo /* Wake up AP in case firmware has placed it in WFE state */
297 1.12 ryo __asm __volatile("sev" ::: "memory");
298 1.12 ryo
299 1.12 ryo /* Wait for APs to start */
300 1.12 ryo for (u_int i = 0x10000000; i > 0; i--) {
301 1.12 ryo membar_consumer();
302 1.12 ryo if (arm_cpu_hatched == started)
303 1.12 ryo break;
304 1.12 ryo }
305 1.12 ryo #endif /* MULTIPROCESSOR */
306 1.12 ryo }
307