xref: /netbsd-src/sys/arch/arm/fdt/arm_fdt.c (revision 8d564c5dcfeea024762586ce07de3c286d3d30e1)
1 /* $NetBSD: arm_fdt.c,v 1.21 2023/04/07 08:55:30 skrll Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared D. McNeill <jmcneill@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.21 2023/04/07 08:55:30 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 
48 #include <dev/ofw/openfirm.h>
49 
50 #include <arm/fdt/arm_fdtvar.h>
51 
52 #include <arm/locore.h>
53 
54 #ifdef EFI_RUNTIME
55 #include <arm/arm/efi_runtime.h>
56 #include <dev/clock_subr.h>
57 #endif
58 
59 static int	arm_fdt_match(device_t, cfdata_t, void *);
60 static void	arm_fdt_attach(device_t, device_t, void *);
61 
62 static void	arm_fdt_irq_default_handler(void *);
63 static void	arm_fdt_fiq_default_handler(void *);
64 
65 #ifdef EFI_RUNTIME
66 static void	arm_fdt_efi_init(device_t);
67 static int	arm_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
68 static int	arm_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
69 
70 static struct todr_chip_handle efi_todr;
71 #endif
72 
73 CFATTACH_DECL_NEW(arm_fdt, 0,
74     arm_fdt_match, arm_fdt_attach, NULL, NULL);
75 
76 struct arm_fdt_cpu_hatch_cb {
77 	TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
78 	void (*cb)(void *, struct cpu_info *);
79 	void *priv;
80 };
81 
82 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
83     TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
84 
85 static void (*_arm_fdt_irq_handler)(void *) = arm_fdt_irq_default_handler;
86 static void (*_arm_fdt_fiq_handler)(void *) = arm_fdt_fiq_default_handler;
87 static void (*_arm_fdt_timer_init)(void) = NULL;
88 
89 int
arm_fdt_match(device_t parent,cfdata_t cf,void * aux)90 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
91 {
92 	return 1;
93 }
94 
95 void
arm_fdt_attach(device_t parent,device_t self,void * aux)96 arm_fdt_attach(device_t parent, device_t self, void *aux)
97 {
98 	const struct fdt_platform *plat = fdt_platform_find();
99 	struct fdt_attach_args faa;
100 
101 	aprint_naive("\n");
102 	aprint_normal("\n");
103 
104 	DISABLE_INTERRUPT();
105 
106 #ifdef EFI_RUNTIME
107 	arm_fdt_efi_init(self);
108 #endif
109 
110 	plat->fp_init_attach_args(&faa);
111 	faa.faa_name = "";
112 	faa.faa_phandle = OF_peer(0);
113 
114 	config_found(self, &faa, NULL, CFARGS_NONE);
115 }
116 void
arm_fdt_cpu_hatch_register(void * priv,void (* cb)(void *,struct cpu_info *))117 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
118 {
119 	struct arm_fdt_cpu_hatch_cb *c;
120 
121 	c = kmem_alloc(sizeof(*c), KM_SLEEP);
122 	c->priv = priv;
123 	c->cb = cb;
124 	TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
125 }
126 
127 void
arm_fdt_cpu_hatch(struct cpu_info * ci)128 arm_fdt_cpu_hatch(struct cpu_info *ci)
129 {
130 	struct arm_fdt_cpu_hatch_cb *c;
131 
132 	TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
133 		c->cb(c->priv, ci);
134 }
135 
136 static void
arm_fdt_irq_default_handler(void * frame)137 arm_fdt_irq_default_handler(void *frame)
138 {
139 	panic("No IRQ handler installed");
140 }
141 
142 static void
arm_fdt_fiq_default_handler(void * frame)143 arm_fdt_fiq_default_handler(void *frame)
144 {
145 	panic("No FIQ handler installed");
146 }
147 
148 void
arm_fdt_irq_set_handler(void (* irq_handler)(void *))149 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
150 {
151 	KASSERT(_arm_fdt_irq_handler == arm_fdt_irq_default_handler);
152 	_arm_fdt_irq_handler = irq_handler;
153 }
154 
155 void
arm_fdt_fiq_set_handler(void (* fiq_handler)(void *))156 arm_fdt_fiq_set_handler(void (*fiq_handler)(void *))
157 {
158 	KASSERT(_arm_fdt_fiq_handler == arm_fdt_fiq_default_handler);
159 	_arm_fdt_fiq_handler = fiq_handler;
160 }
161 
162 void
arm_fdt_irq_handler(void * tf)163 arm_fdt_irq_handler(void *tf)
164 {
165 	_arm_fdt_irq_handler(tf);
166 }
167 
168 void
arm_fdt_fiq_handler(void * tf)169 arm_fdt_fiq_handler(void *tf)
170 {
171 	_arm_fdt_fiq_handler(tf);
172 }
173 
174 void
arm_fdt_timer_register(void (* timerfn)(void))175 arm_fdt_timer_register(void (*timerfn)(void))
176 {
177 	if (_arm_fdt_timer_init != NULL) {
178 #ifdef DIAGNOSTIC
179 		aprint_verbose("%s: timer already registered\n", __func__);
180 #endif
181 		return;
182 	}
183 	_arm_fdt_timer_init = timerfn;
184 }
185 
186 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
187 void
cpu_initclocks(void)188 cpu_initclocks(void)
189 {
190 	if (_arm_fdt_timer_init == NULL)
191 		panic("cpu_initclocks: no timer registered");
192 	_arm_fdt_timer_init();
193 	ENABLE_INTERRUPT();
194 }
195 #endif
196 
197 void
arm_fdt_module_init(void)198 arm_fdt_module_init(void)
199 {
200 #ifdef MODULAR
201 	const int chosen = OF_finddevice("/chosen");
202 	const char *module_name;
203 	const uint64_t *data;
204 	u_int index;
205 	paddr_t pa;
206 	vaddr_t va;
207 	int len;
208 
209 	if (chosen == -1)
210 		return;
211 
212 	data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
213 	if (data == NULL)
214 		return;
215 
216 	for (index = 0; index < len / 16; index++, data += 2) {
217 		module_name = fdtbus_get_string_index(chosen,
218 		    "netbsd,module-names", index);
219 		if (module_name == NULL)
220 			break;
221 
222 		const paddr_t startpa = (paddr_t)be64dec(data + 0);
223 		const size_t size = (size_t)be64dec(data + 1);
224 		const paddr_t endpa = round_page(startpa + size);
225 
226 		const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
227 		    0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
228 		if (startva == 0) {
229 			printf("ERROR: Cannot allocate VA for module %s\n",
230 			    module_name);
231 			continue;
232 		}
233 
234 		for (pa = startpa, va = startva;
235 		     pa < endpa;
236 		     pa += PAGE_SIZE, va += PAGE_SIZE) {
237 			pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
238 		}
239 		pmap_update(pmap_kernel());
240 
241 		module_prime(module_name, (void *)(uintptr_t)startva, size);
242 	}
243 #endif /* !MODULAR */
244 }
245 
246 #ifdef EFI_RUNTIME
247 static void
arm_fdt_efi_init(device_t dev)248 arm_fdt_efi_init(device_t dev)
249 {
250 	uint64_t efi_system_table;
251 	struct efi_tm tm;
252 	int error;
253 
254 	const int chosen = OF_finddevice("/chosen");
255 	if (chosen < 0)
256 		return;
257 
258 	if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
259 		return;
260 
261 	error = arm_efirt_init(efi_system_table);
262 	if (error)
263 		return;
264 
265 	aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
266 
267 	if (arm_efirt_gettime(&tm, NULL) == 0) {
268 		aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
269 		efi_todr.cookie = NULL;
270 		efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime;
271 		efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime;
272 		todr_attach(&efi_todr);
273 	}
274 }
275 
276 static int
arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch,struct clock_ymdhms * dt)277 arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
278 {
279 	struct efi_tm tm;
280 	efi_status status;
281 
282 	status = arm_efirt_gettime(&tm, NULL);
283 	if (status != 0)
284 		return EIO;
285 
286 	dt->dt_year = tm.tm_year;
287 	dt->dt_mon = tm.tm_mon;
288 	dt->dt_day = tm.tm_mday;
289 	dt->dt_wday = 0;
290 	dt->dt_hour = tm.tm_hour;
291 	dt->dt_min = tm.tm_min;
292 	dt->dt_sec = tm.tm_sec;
293 
294 	return 0;
295 }
296 
297 static int
arm_fdt_efi_rtc_settime(todr_chip_handle_t tch,struct clock_ymdhms * dt)298 arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
299 {
300 	struct efi_tm tm;
301 	efi_status status;
302 
303 	memset(&tm, 0, sizeof(tm));
304 	tm.tm_year = dt->dt_year;
305 	tm.tm_mon = dt->dt_mon;
306 	tm.tm_mday = dt->dt_day;
307 	tm.tm_hour = dt->dt_hour;
308 	tm.tm_min = dt->dt_min;
309 	tm.tm_sec = dt->dt_sec;
310 
311 	status = arm_efirt_settime(&tm);
312 	if (status != 0)
313 		return EIO;
314 
315 	return 0;
316 }
317 #endif
318