cpu_fdt.c revision 1.7.2.3 1 1.7.2.2 martin /* $NetBSD: cpu_fdt.c,v 1.7.2.3 2020/04/13 08:03:34 martin 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.7.2.1 christos #include "opt_multiprocessor.h"
30 1.7.2.1 christos #include "psci_fdt.h"
31 1.7.2.1 christos
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.7.2.2 martin __KERNEL_RCSID(0, "$NetBSD: cpu_fdt.c,v 1.7.2.3 2020/04/13 08:03:34 martin Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.7.2.1 christos #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.7.2.2 martin #include <arm/cpuvar.h>
49 1.7.2.1 christos #include <arm/locore.h>
50 1.7.2.1 christos
51 1.7.2.1 christos #include <arm/arm/psci.h>
52 1.7.2.1 christos #include <arm/fdt/arm_fdtvar.h>
53 1.7.2.1 christos #include <arm/fdt/psci_fdtvar.h>
54 1.7.2.1 christos
55 1.7.2.1 christos #include <uvm/uvm_extern.h>
56 1.1 jmcneill
57 1.1 jmcneill static int cpu_fdt_match(device_t, cfdata_t, void *);
58 1.1 jmcneill static void cpu_fdt_attach(device_t, device_t, void *);
59 1.1 jmcneill
60 1.1 jmcneill struct cpu_fdt_softc {
61 1.1 jmcneill device_t sc_dev;
62 1.1 jmcneill int sc_phandle;
63 1.1 jmcneill };
64 1.1 jmcneill
65 1.1 jmcneill CFATTACH_DECL_NEW(cpu_fdt, sizeof(struct cpu_fdt_softc),
66 1.1 jmcneill cpu_fdt_match, cpu_fdt_attach, NULL, NULL);
67 1.1 jmcneill
68 1.1 jmcneill static int
69 1.1 jmcneill cpu_fdt_match(device_t parent, cfdata_t cf, void *aux)
70 1.1 jmcneill {
71 1.1 jmcneill struct fdt_attach_args * const faa = aux;
72 1.3 jmcneill const int phandle = faa->faa_phandle;
73 1.7.2.2 martin const char *device_type;
74 1.2 jmcneill
75 1.7.2.2 martin device_type = fdtbus_get_string(phandle, "device_type");
76 1.2 jmcneill
77 1.7.2.2 martin return device_type != NULL && strcmp(device_type, "cpu") == 0;
78 1.1 jmcneill }
79 1.1 jmcneill
80 1.1 jmcneill static void
81 1.1 jmcneill cpu_fdt_attach(device_t parent, device_t self, void *aux)
82 1.1 jmcneill {
83 1.1 jmcneill struct cpu_fdt_softc * const sc = device_private(self);
84 1.1 jmcneill struct fdt_attach_args * const faa = aux;
85 1.3 jmcneill const int phandle = faa->faa_phandle;
86 1.7.2.2 martin bus_addr_t cpuid;
87 1.7.2.2 martin const uint32_t *cap_ptr;
88 1.7.2.2 martin int len;
89 1.1 jmcneill
90 1.1 jmcneill sc->sc_dev = self;
91 1.3 jmcneill sc->sc_phandle = phandle;
92 1.3 jmcneill
93 1.7.2.2 martin cap_ptr = fdtbus_get_prop(phandle, "capacity-dmips-mhz", &len);
94 1.7.2.2 martin if (cap_ptr && len == 4) {
95 1.7.2.2 martin prop_dictionary_t dict = device_properties(self);
96 1.7.2.2 martin uint32_t capacity_dmips_mhz = be32toh(*cap_ptr);
97 1.1 jmcneill
98 1.7.2.2 martin prop_dictionary_set_uint32(dict, "capacity_dmips_mhz",
99 1.7.2.2 martin capacity_dmips_mhz);
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.7.2.2 martin if (fdtbus_get_reg(phandle, 0, &cpuid, NULL) != 0)
103 1.7.2.2 martin cpuid = 0;
104 1.7.2.2 martin
105 1.2 jmcneill /* Attach the CPU */
106 1.3 jmcneill cpu_attach(self, cpuid);
107 1.7.2.1 christos
108 1.7.2.1 christos /* Attach CPU frequency scaling provider */
109 1.7.2.1 christos config_found(self, faa, NULL);
110 1.7.2.1 christos }
111 1.7.2.1 christos
112 1.7.2.1 christos #if defined(MULTIPROCESSOR) && (NPSCI_FDT > 0 || defined(__aarch64__))
113 1.7.2.1 christos static register_t
114 1.7.2.1 christos cpu_fdt_mpstart_pa(void)
115 1.7.2.1 christos {
116 1.7.2.1 christos bool ok __diagused;
117 1.7.2.1 christos paddr_t pa;
118 1.7.2.1 christos
119 1.7.2.1 christos ok = pmap_extract(pmap_kernel(), (vaddr_t)cpu_mpstart, &pa);
120 1.7.2.1 christos KASSERT(ok);
121 1.7.2.1 christos
122 1.7.2.1 christos return pa;
123 1.7.2.1 christos }
124 1.7.2.1 christos #endif
125 1.7.2.1 christos
126 1.7.2.1 christos #ifdef MULTIPROCESSOR
127 1.7.2.1 christos static bool
128 1.7.2.1 christos arm_fdt_cpu_okay(const int child)
129 1.7.2.1 christos {
130 1.7.2.1 christos const char *s;
131 1.7.2.1 christos
132 1.7.2.1 christos s = fdtbus_get_string(child, "device_type");
133 1.7.2.1 christos if (!s || strcmp(s, "cpu") != 0)
134 1.7.2.1 christos return false;
135 1.7.2.1 christos
136 1.7.2.1 christos s = fdtbus_get_string(child, "status");
137 1.7.2.1 christos if (s) {
138 1.7.2.1 christos if (strcmp(s, "okay") == 0)
139 1.7.2.1 christos return false;
140 1.7.2.1 christos if (strcmp(s, "disabled") == 0)
141 1.7.2.1 christos return of_hasprop(child, "enable-method");
142 1.7.2.1 christos return false;
143 1.7.2.1 christos } else {
144 1.7.2.1 christos return true;
145 1.7.2.1 christos }
146 1.7.2.1 christos }
147 1.7.2.1 christos #endif /* MULTIPROCESSOR */
148 1.7.2.1 christos
149 1.7.2.1 christos void
150 1.7.2.1 christos arm_fdt_cpu_bootstrap(void)
151 1.7.2.1 christos {
152 1.7.2.1 christos #ifdef MULTIPROCESSOR
153 1.7.2.1 christos uint64_t mpidr, bp_mpidr;
154 1.7.2.1 christos u_int cpuindex;
155 1.7.2.1 christos int child;
156 1.7.2.1 christos
157 1.7.2.1 christos const int cpus = OF_finddevice("/cpus");
158 1.7.2.1 christos if (cpus == -1) {
159 1.7.2.1 christos aprint_error("%s: no /cpus node found\n", __func__);
160 1.7.2.1 christos arm_cpu_max = 1;
161 1.7.2.1 christos return;
162 1.7.2.1 christos }
163 1.7.2.1 christos
164 1.7.2.1 christos /* Count CPUs */
165 1.7.2.1 christos arm_cpu_max = 0;
166 1.7.2.1 christos
167 1.7.2.1 christos /* MPIDR affinity levels of boot processor. */
168 1.7.2.1 christos bp_mpidr = cpu_mpidr_aff_read();
169 1.7.2.1 christos
170 1.7.2.1 christos /* Boot APs */
171 1.7.2.1 christos cpuindex = 1;
172 1.7.2.1 christos for (child = OF_child(cpus); child; child = OF_peer(child)) {
173 1.7.2.1 christos if (!arm_fdt_cpu_okay(child))
174 1.7.2.1 christos continue;
175 1.7.2.1 christos
176 1.7.2.1 christos arm_cpu_max++;
177 1.7.2.1 christos if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
178 1.7.2.1 christos continue;
179 1.7.2.1 christos if (mpidr == bp_mpidr)
180 1.7.2.1 christos continue; /* BP already started */
181 1.7.2.1 christos
182 1.7.2.1 christos KASSERT(cpuindex < MAXCPUS);
183 1.7.2.1 christos cpu_mpidr[cpuindex] = mpidr;
184 1.7.2.1 christos cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex],
185 1.7.2.1 christos sizeof(cpu_mpidr[cpuindex]));
186 1.7.2.1 christos
187 1.7.2.1 christos cpuindex++;
188 1.7.2.1 christos }
189 1.7.2.1 christos #endif
190 1.7.2.1 christos }
191 1.7.2.1 christos
192 1.7.2.1 christos #ifdef MULTIPROCESSOR
193 1.7.2.1 christos static struct arm_cpu_method *
194 1.7.2.1 christos arm_fdt_cpu_enable_method(int phandle)
195 1.7.2.1 christos {
196 1.7.2.1 christos const char *method;
197 1.7.2.1 christos
198 1.7.2.1 christos method = fdtbus_get_string(phandle, "enable-method");
199 1.7.2.1 christos if (method == NULL)
200 1.7.2.1 christos return NULL;
201 1.7.2.1 christos
202 1.7.2.1 christos __link_set_decl(arm_cpu_methods, struct arm_cpu_method);
203 1.7.2.1 christos struct arm_cpu_method * const *acmp;
204 1.7.2.1 christos __link_set_foreach(acmp, arm_cpu_methods) {
205 1.7.2.1 christos if (strcmp(method, (*acmp)->acm_compat) == 0)
206 1.7.2.1 christos return *acmp;
207 1.7.2.1 christos }
208 1.7.2.1 christos
209 1.7.2.1 christos return NULL;
210 1.7.2.1 christos }
211 1.7.2.1 christos
212 1.7.2.1 christos static int
213 1.7.2.1 christos arm_fdt_cpu_enable(int phandle, struct arm_cpu_method *acm)
214 1.7.2.1 christos {
215 1.7.2.1 christos return acm->acm_enable(phandle);
216 1.7.2.1 christos }
217 1.7.2.1 christos #endif
218 1.7.2.1 christos
219 1.7.2.1 christos int
220 1.7.2.1 christos arm_fdt_cpu_mpstart(void)
221 1.7.2.1 christos {
222 1.7.2.1 christos int ret = 0;
223 1.7.2.1 christos #ifdef MULTIPROCESSOR
224 1.7.2.1 christos uint64_t mpidr, bp_mpidr;
225 1.7.2.1 christos u_int cpuindex, i;
226 1.7.2.1 christos int child, error;
227 1.7.2.1 christos struct arm_cpu_method *acm;
228 1.7.2.1 christos
229 1.7.2.1 christos const int cpus = OF_finddevice("/cpus");
230 1.7.2.1 christos if (cpus == -1) {
231 1.7.2.1 christos aprint_error("%s: no /cpus node found\n", __func__);
232 1.7.2.1 christos return 0;
233 1.7.2.1 christos }
234 1.7.2.1 christos
235 1.7.2.1 christos /* MPIDR affinity levels of boot processor. */
236 1.7.2.1 christos bp_mpidr = cpu_mpidr_aff_read();
237 1.7.2.1 christos
238 1.7.2.1 christos /* Boot APs */
239 1.7.2.1 christos cpuindex = 1;
240 1.7.2.1 christos for (child = OF_child(cpus); child; child = OF_peer(child)) {
241 1.7.2.1 christos if (!arm_fdt_cpu_okay(child))
242 1.7.2.1 christos continue;
243 1.7.2.1 christos
244 1.7.2.1 christos if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
245 1.7.2.1 christos continue;
246 1.7.2.1 christos
247 1.7.2.1 christos if (mpidr == bp_mpidr)
248 1.7.2.1 christos continue; /* BP already started */
249 1.7.2.1 christos
250 1.7.2.1 christos acm = arm_fdt_cpu_enable_method(child);
251 1.7.2.1 christos if (acm == NULL)
252 1.7.2.1 christos acm = arm_fdt_cpu_enable_method(cpus);
253 1.7.2.1 christos if (acm == NULL)
254 1.7.2.1 christos continue;
255 1.7.2.1 christos
256 1.7.2.1 christos error = arm_fdt_cpu_enable(child, acm);
257 1.7.2.1 christos if (error != 0) {
258 1.7.2.2 martin aprint_error("%s: failed to enable CPU %#" PRIx64 "\n",
259 1.7.2.2 martin __func__, mpidr);
260 1.7.2.1 christos continue;
261 1.7.2.1 christos }
262 1.7.2.1 christos
263 1.7.2.1 christos /* Wake up AP in case firmware has placed it in WFE state */
264 1.7.2.1 christos __asm __volatile("sev" ::: "memory");
265 1.7.2.1 christos
266 1.7.2.1 christos /* Wait for AP to start */
267 1.7.2.1 christos for (i = 0x10000000; i > 0; i--) {
268 1.7.2.3 martin if (cpu_hatched_p(cpuindex))
269 1.7.2.1 christos break;
270 1.7.2.1 christos }
271 1.7.2.1 christos
272 1.7.2.1 christos if (i == 0) {
273 1.7.2.1 christos ret++;
274 1.7.2.1 christos aprint_error("cpu%d: WARNING: AP failed to start\n", cpuindex);
275 1.7.2.1 christos }
276 1.7.2.1 christos
277 1.7.2.1 christos cpuindex++;
278 1.7.2.1 christos }
279 1.7.2.1 christos #endif /* MULTIPROCESSOR */
280 1.7.2.1 christos return ret;
281 1.7.2.1 christos }
282 1.7.2.1 christos
283 1.7.2.1 christos static int
284 1.7.2.1 christos cpu_enable_nullop(int phandle)
285 1.7.2.1 christos {
286 1.7.2.1 christos return ENXIO;
287 1.7.2.1 christos }
288 1.7.2.1 christos ARM_CPU_METHOD(default, "", cpu_enable_nullop);
289 1.7.2.1 christos
290 1.7.2.1 christos #if defined(MULTIPROCESSOR) && NPSCI_FDT > 0
291 1.7.2.1 christos static int
292 1.7.2.1 christos cpu_enable_psci(int phandle)
293 1.7.2.1 christos {
294 1.7.2.1 christos static bool psci_probed, psci_p;
295 1.7.2.1 christos uint64_t mpidr;
296 1.7.2.1 christos int ret;
297 1.7.2.1 christos
298 1.7.2.1 christos if (!psci_probed) {
299 1.7.2.1 christos psci_probed = true;
300 1.7.2.1 christos psci_p = psci_fdt_preinit() == 0;
301 1.7.2.1 christos }
302 1.7.2.1 christos if (!psci_p)
303 1.7.2.1 christos return ENXIO;
304 1.7.2.1 christos
305 1.7.2.1 christos fdtbus_get_reg64(phandle, 0, &mpidr, NULL);
306 1.7.2.1 christos
307 1.7.2.3 martin #if !defined(AARCH64)
308 1.7.2.3 martin /*
309 1.7.2.3 martin * not necessary on AARCH64. beside there it hangs the system
310 1.7.2.3 martin * because cache ops are only functional after cpu_attach()
311 1.7.2.3 martin * was called.
312 1.7.2.3 martin */
313 1.7.2.3 martin cpu_dcache_wbinv_all();
314 1.7.2.3 martin #endif
315 1.7.2.1 christos ret = psci_cpu_on(mpidr, cpu_fdt_mpstart_pa(), 0);
316 1.7.2.1 christos if (ret != PSCI_SUCCESS)
317 1.7.2.1 christos return EIO;
318 1.7.2.1 christos
319 1.7.2.1 christos return 0;
320 1.7.2.1 christos }
321 1.7.2.1 christos ARM_CPU_METHOD(psci, "psci", cpu_enable_psci);
322 1.7.2.1 christos #endif
323 1.7.2.1 christos
324 1.7.2.1 christos #if defined(MULTIPROCESSOR) && defined(__aarch64__)
325 1.7.2.1 christos static int
326 1.7.2.1 christos spintable_cpu_on(u_int cpuindex, paddr_t entry_point_address, paddr_t cpu_release_addr)
327 1.7.2.1 christos {
328 1.7.2.1 christos /*
329 1.7.2.1 christos * we need devmap for cpu-release-addr in advance.
330 1.7.2.2 martin * __HAVE_MM_MD_DIRECT_MAPPED_PHYS nor pmap work at this point.
331 1.7.2.1 christos */
332 1.7.2.1 christos if (pmap_devmap_find_pa(cpu_release_addr, sizeof(paddr_t)) == NULL) {
333 1.7.2.1 christos aprint_error("%s: devmap for cpu-release-addr"
334 1.7.2.1 christos " 0x%08"PRIxPADDR" required\n", __func__, cpu_release_addr);
335 1.7.2.1 christos return -1;
336 1.7.2.1 christos } else {
337 1.7.2.1 christos extern struct bus_space arm_generic_bs_tag;
338 1.7.2.1 christos bus_space_handle_t ioh;
339 1.7.2.1 christos
340 1.7.2.1 christos bus_space_map(&arm_generic_bs_tag, cpu_release_addr,
341 1.7.2.1 christos sizeof(paddr_t), 0, &ioh);
342 1.7.2.1 christos bus_space_write_4(&arm_generic_bs_tag, ioh, 0,
343 1.7.2.1 christos entry_point_address);
344 1.7.2.1 christos bus_space_unmap(&arm_generic_bs_tag, ioh, sizeof(paddr_t));
345 1.7.2.1 christos }
346 1.7.2.1 christos
347 1.7.2.1 christos return 0;
348 1.7.2.1 christos }
349 1.7.2.1 christos
350 1.7.2.1 christos static int
351 1.7.2.1 christos cpu_enable_spin_table(int phandle)
352 1.7.2.1 christos {
353 1.7.2.1 christos uint64_t mpidr, addr;
354 1.7.2.1 christos int ret;
355 1.7.2.1 christos
356 1.7.2.1 christos fdtbus_get_reg64(phandle, 0, &mpidr, NULL);
357 1.7.2.1 christos
358 1.7.2.1 christos if (of_getprop_uint64(phandle, "cpu-release-addr", &addr) != 0)
359 1.7.2.1 christos return ENXIO;
360 1.7.2.1 christos
361 1.7.2.1 christos ret = spintable_cpu_on(mpidr, cpu_fdt_mpstart_pa(), (paddr_t)addr);
362 1.7.2.1 christos if (ret != 0)
363 1.7.2.1 christos return EIO;
364 1.7.2.1 christos
365 1.7.2.1 christos return 0;
366 1.1 jmcneill }
367 1.7.2.1 christos ARM_CPU_METHOD(spin_table, "spin-table", cpu_enable_spin_table);
368 1.7.2.1 christos #endif
369