arm_fdt.c revision 1.13 1 /* $NetBSD: arm_fdt.c,v 1.13 2020/12/17 08:47:18 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_arm_timer.h"
30 #include "opt_efi.h"
31 #include "opt_modular.h"
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.13 2020/12/17 08:47:18 skrll Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/cpu.h>
39 #include <sys/device.h>
40 #include <sys/kmem.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/fdt/fdtvar.h>
47 #include <dev/ofw/openfirm.h>
48
49 #include <arm/fdt/arm_fdtvar.h>
50
51 #ifdef EFI_RUNTIME
52 #include <arm/arm/efi_runtime.h>
53 #include <dev/clock_subr.h>
54 #endif
55
56 static int arm_fdt_match(device_t, cfdata_t, void *);
57 static void arm_fdt_attach(device_t, device_t, void *);
58
59 #ifdef EFI_RUNTIME
60 static void arm_fdt_efi_init(device_t);
61 static int arm_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
62 static int arm_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
63
64 static struct todr_chip_handle efi_todr;
65 #endif
66
67 CFATTACH_DECL_NEW(arm_fdt, 0,
68 arm_fdt_match, arm_fdt_attach, NULL, NULL);
69
70 struct arm_fdt_cpu_hatch_cb {
71 TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
72 void (*cb)(void *, struct cpu_info *);
73 void *priv;
74 };
75
76 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
77 TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
78
79 static void (*_arm_fdt_irq_handler)(void *) = NULL;
80 static void (*_arm_fdt_timer_init)(void) = NULL;
81
82 int
83 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 return 1;
86 }
87
88 void
89 arm_fdt_attach(device_t parent, device_t self, void *aux)
90 {
91 const struct arm_platform *plat = arm_fdt_platform();
92 struct fdt_attach_args faa;
93
94 aprint_naive("\n");
95 aprint_normal("\n");
96
97 #ifdef EFI_RUNTIME
98 arm_fdt_efi_init(self);
99 #endif
100
101 plat->ap_init_attach_args(&faa);
102 faa.faa_name = "";
103 faa.faa_phandle = OF_peer(0);
104
105 config_found(self, &faa, NULL);
106 }
107
108 const struct arm_platform *
109 arm_fdt_platform(void)
110 {
111 static const struct arm_platform_info *booted_platform = NULL;
112 __link_set_decl(arm_platforms, struct arm_platform_info);
113 struct arm_platform_info * const *info;
114
115 if (booted_platform == NULL) {
116 const struct arm_platform_info *best_info = NULL;
117 const int phandle = OF_peer(0);
118 int match, best_match = 0;
119
120 __link_set_foreach(info, arm_platforms) {
121 const char * const compat[] = { (*info)->api_compat, NULL };
122 match = of_match_compatible(phandle, compat);
123 if (match > best_match) {
124 best_match = match;
125 best_info = *info;
126 }
127 }
128
129 booted_platform = best_info;
130 }
131
132 /*
133 * No SoC specific platform was found. Try to find a generic
134 * platform definition and use that if available.
135 */
136 if (booted_platform == NULL) {
137 __link_set_foreach(info, arm_platforms) {
138 if (strcmp((*info)->api_compat, ARM_PLATFORM_DEFAULT) == 0) {
139 booted_platform = *info;
140 break;
141 }
142 }
143 }
144
145 return booted_platform == NULL ? NULL : booted_platform->api_ops;
146 }
147
148 void
149 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
150 {
151 struct arm_fdt_cpu_hatch_cb *c;
152
153 c = kmem_alloc(sizeof(*c), KM_SLEEP);
154 c->priv = priv;
155 c->cb = cb;
156 TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
157 }
158
159 void
160 arm_fdt_cpu_hatch(struct cpu_info *ci)
161 {
162 struct arm_fdt_cpu_hatch_cb *c;
163
164 TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
165 c->cb(c->priv, ci);
166 }
167
168 void
169 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
170 {
171 KASSERT(_arm_fdt_irq_handler == NULL);
172 _arm_fdt_irq_handler = irq_handler;
173 }
174
175 void
176 arm_fdt_irq_handler(void *tf)
177 {
178 _arm_fdt_irq_handler(tf);
179 }
180
181 void
182 arm_fdt_timer_register(void (*timerfn)(void))
183 {
184 if (_arm_fdt_timer_init != NULL) {
185 #ifdef DIAGNOSTIC
186 aprint_verbose("%s: timer already registered\n", __func__);
187 #endif
188 return;
189 }
190 _arm_fdt_timer_init = timerfn;
191 }
192
193 void
194 arm_fdt_memory_dump(paddr_t pa)
195 {
196 const struct arm_platform *plat = arm_fdt_platform();
197 struct fdt_attach_args faa;
198 bus_space_tag_t bst;
199 bus_space_handle_t bsh;
200
201 plat->ap_init_attach_args(&faa);
202
203 bst = faa.faa_bst;
204 bus_space_map(bst, pa, 0x100, 0, &bsh);
205
206 for (int i = 0; i < 0x100; i += 0x10) {
207 printf("%" PRIxPTR ": %08x %08x %08x %08x\n",
208 (uintptr_t)(pa + i),
209 bus_space_read_4(bst, bsh, i + 0),
210 bus_space_read_4(bst, bsh, i + 4),
211 bus_space_read_4(bst, bsh, i + 8),
212 bus_space_read_4(bst, bsh, i + 12));
213 }
214 }
215
216 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
217 void
218 cpu_initclocks(void)
219 {
220 if (_arm_fdt_timer_init == NULL)
221 panic("cpu_initclocks: no timer registered");
222 _arm_fdt_timer_init();
223 }
224 #endif
225
226 void
227 arm_fdt_module_init(void)
228 {
229 #ifdef MODULAR
230 const int chosen = OF_finddevice("/chosen");
231 const char *module_name;
232 const uint64_t *data;
233 u_int index;
234 paddr_t pa;
235 vaddr_t va;
236 int len;
237
238 if (chosen == -1)
239 return;
240
241 data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
242 if (data == NULL)
243 return;
244
245 for (index = 0; index < len / 16; index++, data += 2) {
246 module_name = fdtbus_get_string_index(chosen,
247 "netbsd,module-names", index);
248 if (module_name == NULL)
249 break;
250
251 const paddr_t startpa = (paddr_t)be64dec(data + 0);
252 const size_t size = (size_t)be64dec(data + 1);
253 const paddr_t endpa = round_page(startpa + size);
254
255 const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
256 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
257 if (startva == 0) {
258 printf("ERROR: Cannot allocate VA for module %s\n",
259 module_name);
260 continue;
261 }
262
263 for (pa = startpa, va = startva;
264 pa < endpa;
265 pa += PAGE_SIZE, va += PAGE_SIZE) {
266 pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
267 }
268 pmap_update(pmap_kernel());
269
270 module_prime(module_name, (void *)(uintptr_t)startva, size);
271 }
272 #endif /* !MODULAR */
273 }
274
275 #ifdef EFI_RUNTIME
276 static void
277 arm_fdt_efi_init(device_t dev)
278 {
279 uint64_t efi_system_table;
280 struct efi_tm tm;
281 int error;
282
283 const int chosen = OF_finddevice("/chosen");
284 if (chosen < 0)
285 return;
286
287 if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
288 return;
289
290 error = arm_efirt_init(efi_system_table);
291 if (error)
292 return;
293
294 aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
295
296 if (arm_efirt_gettime(&tm) == 0) {
297 aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
298 efi_todr.cookie = NULL;
299 efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime;
300 efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime;
301 todr_attach(&efi_todr);
302 }
303 }
304
305 static int
306 arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
307 {
308 struct efi_tm tm;
309 int error;
310
311 error = arm_efirt_gettime(&tm);
312 if (error)
313 return error;
314
315 dt->dt_year = tm.tm_year;
316 dt->dt_mon = tm.tm_mon;
317 dt->dt_day = tm.tm_mday;
318 dt->dt_wday = 0;
319 dt->dt_hour = tm.tm_hour;
320 dt->dt_min = tm.tm_min;
321 dt->dt_sec = tm.tm_sec;
322
323 return 0;
324 }
325
326 static int
327 arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
328 {
329 struct efi_tm tm;
330
331 memset(&tm, 0, sizeof(tm));
332 tm.tm_year = dt->dt_year;
333 tm.tm_mon = dt->dt_mon;
334 tm.tm_mday = dt->dt_day;
335 tm.tm_hour = dt->dt_hour;
336 tm.tm_min = dt->dt_min;
337 tm.tm_sec = dt->dt_sec;
338
339 return arm_efirt_settime(&tm);
340 }
341 #endif
342