Home | History | Annotate | Line # | Download | only in fdt
arm_fdt.c revision 1.22
      1 /* $NetBSD: arm_fdt.c,v 1.22 2025/01/30 11:09:53 jmcneill 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.22 2025/01/30 11:09:53 jmcneill 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 
     72 static const char * const ignore_efi_runtime_models[] = {
     73 	/* RTC calls do not work with current firmware. */
     74 	"Radxa Computer (Shenzhen) Co., Ltd. Radxa Orion O6",
     75 };
     76 #endif
     77 
     78 CFATTACH_DECL_NEW(arm_fdt, 0,
     79     arm_fdt_match, arm_fdt_attach, NULL, NULL);
     80 
     81 struct arm_fdt_cpu_hatch_cb {
     82 	TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
     83 	void (*cb)(void *, struct cpu_info *);
     84 	void *priv;
     85 };
     86 
     87 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
     88     TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
     89 
     90 static void (*_arm_fdt_irq_handler)(void *) = arm_fdt_irq_default_handler;
     91 static void (*_arm_fdt_fiq_handler)(void *) = arm_fdt_fiq_default_handler;
     92 static void (*_arm_fdt_timer_init)(void) = NULL;
     93 
     94 int
     95 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
     96 {
     97 	return 1;
     98 }
     99 
    100 void
    101 arm_fdt_attach(device_t parent, device_t self, void *aux)
    102 {
    103 	const struct fdt_platform *plat = fdt_platform_find();
    104 	struct fdt_attach_args faa;
    105 
    106 	aprint_naive("\n");
    107 	aprint_normal("\n");
    108 
    109 	DISABLE_INTERRUPT();
    110 
    111 #ifdef EFI_RUNTIME
    112 	arm_fdt_efi_init(self);
    113 #endif
    114 
    115 	plat->fp_init_attach_args(&faa);
    116 	faa.faa_name = "";
    117 	faa.faa_phandle = OF_peer(0);
    118 
    119 	config_found(self, &faa, NULL, CFARGS_NONE);
    120 }
    121 void
    122 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
    123 {
    124 	struct arm_fdt_cpu_hatch_cb *c;
    125 
    126 	c = kmem_alloc(sizeof(*c), KM_SLEEP);
    127 	c->priv = priv;
    128 	c->cb = cb;
    129 	TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
    130 }
    131 
    132 void
    133 arm_fdt_cpu_hatch(struct cpu_info *ci)
    134 {
    135 	struct arm_fdt_cpu_hatch_cb *c;
    136 
    137 	TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
    138 		c->cb(c->priv, ci);
    139 }
    140 
    141 static void
    142 arm_fdt_irq_default_handler(void *frame)
    143 {
    144 	panic("No IRQ handler installed");
    145 }
    146 
    147 static void
    148 arm_fdt_fiq_default_handler(void *frame)
    149 {
    150 	panic("No FIQ handler installed");
    151 }
    152 
    153 void
    154 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
    155 {
    156 	KASSERT(_arm_fdt_irq_handler == arm_fdt_irq_default_handler);
    157 	_arm_fdt_irq_handler = irq_handler;
    158 }
    159 
    160 void
    161 arm_fdt_fiq_set_handler(void (*fiq_handler)(void *))
    162 {
    163 	KASSERT(_arm_fdt_fiq_handler == arm_fdt_fiq_default_handler);
    164 	_arm_fdt_fiq_handler = fiq_handler;
    165 }
    166 
    167 void
    168 arm_fdt_irq_handler(void *tf)
    169 {
    170 	_arm_fdt_irq_handler(tf);
    171 }
    172 
    173 void
    174 arm_fdt_fiq_handler(void *tf)
    175 {
    176 	_arm_fdt_fiq_handler(tf);
    177 }
    178 
    179 void
    180 arm_fdt_timer_register(void (*timerfn)(void))
    181 {
    182 	if (_arm_fdt_timer_init != NULL) {
    183 #ifdef DIAGNOSTIC
    184 		aprint_verbose("%s: timer already registered\n", __func__);
    185 #endif
    186 		return;
    187 	}
    188 	_arm_fdt_timer_init = timerfn;
    189 }
    190 
    191 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
    192 void
    193 cpu_initclocks(void)
    194 {
    195 	if (_arm_fdt_timer_init == NULL)
    196 		panic("cpu_initclocks: no timer registered");
    197 	_arm_fdt_timer_init();
    198 	ENABLE_INTERRUPT();
    199 }
    200 #endif
    201 
    202 void
    203 arm_fdt_module_init(void)
    204 {
    205 #ifdef MODULAR
    206 	const int chosen = OF_finddevice("/chosen");
    207 	const char *module_name;
    208 	const uint64_t *data;
    209 	u_int index;
    210 	paddr_t pa;
    211 	vaddr_t va;
    212 	int len;
    213 
    214 	if (chosen == -1)
    215 		return;
    216 
    217 	data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
    218 	if (data == NULL)
    219 		return;
    220 
    221 	for (index = 0; index < len / 16; index++, data += 2) {
    222 		module_name = fdtbus_get_string_index(chosen,
    223 		    "netbsd,module-names", index);
    224 		if (module_name == NULL)
    225 			break;
    226 
    227 		const paddr_t startpa = (paddr_t)be64dec(data + 0);
    228 		const size_t size = (size_t)be64dec(data + 1);
    229 		const paddr_t endpa = round_page(startpa + size);
    230 
    231 		const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
    232 		    0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    233 		if (startva == 0) {
    234 			printf("ERROR: Cannot allocate VA for module %s\n",
    235 			    module_name);
    236 			continue;
    237 		}
    238 
    239 		for (pa = startpa, va = startva;
    240 		     pa < endpa;
    241 		     pa += PAGE_SIZE, va += PAGE_SIZE) {
    242 			pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
    243 		}
    244 		pmap_update(pmap_kernel());
    245 
    246 		module_prime(module_name, (void *)(uintptr_t)startva, size);
    247 	}
    248 #endif /* !MODULAR */
    249 }
    250 
    251 #ifdef EFI_RUNTIME
    252 static bool
    253 arm_fdi_efi_ignored(void)
    254 {
    255 	const int phandle = OF_peer(0);
    256 	const char *descr;
    257 	u_int n;
    258 
    259 	descr = fdtbus_get_string(phandle, "model");
    260 	if (descr == NULL) {
    261 		return false;
    262 	}
    263 
    264 	for (n = 0; n < __arraycount(ignore_efi_runtime_models); n++) {
    265 		if (strcmp(descr, ignore_efi_runtime_models[n]) == 0) {
    266 			return true;
    267 		}
    268 	}
    269 
    270 	return false;
    271 }
    272 
    273 static void
    274 arm_fdt_efi_init(device_t dev)
    275 {
    276 	uint64_t efi_system_table;
    277 	struct efi_tm tm;
    278 	int error;
    279 
    280 	const int chosen = OF_finddevice("/chosen");
    281 	if (chosen < 0)
    282 		return;
    283 
    284 	if (arm_fdi_efi_ignored()) {
    285 		aprint_debug_dev(dev, "EFI runtime services ignored on this platform\n");
    286 		return;
    287 	}
    288 
    289 	if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
    290 		return;
    291 
    292 	error = arm_efirt_init(efi_system_table);
    293 	if (error)
    294 		return;
    295 
    296 	aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
    297 
    298 	if (arm_efirt_gettime(&tm, NULL) == 0) {
    299 		aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
    300 		efi_todr.cookie = NULL;
    301 		efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime;
    302 		efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime;
    303 		todr_attach(&efi_todr);
    304 	}
    305 }
    306 
    307 static int
    308 arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    309 {
    310 	struct efi_tm tm;
    311 	efi_status status;
    312 
    313 	status = arm_efirt_gettime(&tm, NULL);
    314 	if (status != 0)
    315 		return EIO;
    316 
    317 	dt->dt_year = tm.tm_year;
    318 	dt->dt_mon = tm.tm_mon;
    319 	dt->dt_day = tm.tm_mday;
    320 	dt->dt_wday = 0;
    321 	dt->dt_hour = tm.tm_hour;
    322 	dt->dt_min = tm.tm_min;
    323 	dt->dt_sec = tm.tm_sec;
    324 
    325 	return 0;
    326 }
    327 
    328 static int
    329 arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    330 {
    331 	struct efi_tm tm;
    332 	efi_status status;
    333 
    334 	memset(&tm, 0, sizeof(tm));
    335 	tm.tm_year = dt->dt_year;
    336 	tm.tm_mon = dt->dt_mon;
    337 	tm.tm_mday = dt->dt_day;
    338 	tm.tm_hour = dt->dt_hour;
    339 	tm.tm_min = dt->dt_min;
    340 	tm.tm_sec = dt->dt_sec;
    341 
    342 	status = arm_efirt_settime(&tm);
    343 	if (status != 0)
    344 		return EIO;
    345 
    346 	return 0;
    347 }
    348 #endif
    349