Home | History | Annotate | Line # | Download | only in fdt
arm_fdt.c revision 1.17
      1 /* $NetBSD: arm_fdt.c,v 1.17 2021/08/07 16:18:43 thorpej 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.17 2021/08/07 16:18:43 thorpej 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 static void	arm_fdt_irq_default_handler(void *);
     60 
     61 #ifdef EFI_RUNTIME
     62 static void	arm_fdt_efi_init(device_t);
     63 static int	arm_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
     64 static int	arm_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
     65 
     66 static struct todr_chip_handle efi_todr;
     67 #endif
     68 
     69 CFATTACH_DECL_NEW(arm_fdt, 0,
     70     arm_fdt_match, arm_fdt_attach, NULL, NULL);
     71 
     72 struct arm_fdt_cpu_hatch_cb {
     73 	TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
     74 	void (*cb)(void *, struct cpu_info *);
     75 	void *priv;
     76 };
     77 
     78 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
     79     TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
     80 
     81 static void (*_arm_fdt_irq_handler)(void *) = arm_fdt_irq_default_handler;
     82 static void (*_arm_fdt_timer_init)(void) = NULL;
     83 
     84 int
     85 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
     86 {
     87 	return 1;
     88 }
     89 
     90 void
     91 arm_fdt_attach(device_t parent, device_t self, void *aux)
     92 {
     93 	const struct arm_platform *plat = arm_fdt_platform();
     94 	struct fdt_attach_args faa;
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal("\n");
     98 
     99 #ifdef EFI_RUNTIME
    100 	arm_fdt_efi_init(self);
    101 #endif
    102 
    103 	plat->ap_init_attach_args(&faa);
    104 	faa.faa_name = "";
    105 	faa.faa_phandle = OF_peer(0);
    106 
    107 	config_found(self, &faa, NULL, CFARGS_NONE);
    108 }
    109 
    110 const struct arm_platform *
    111 arm_fdt_platform(void)
    112 {
    113 	static const struct arm_platform_info *booted_platform = NULL;
    114 	__link_set_decl(arm_platforms, struct arm_platform_info);
    115 	struct arm_platform_info * const *info;
    116 
    117 	if (booted_platform == NULL) {
    118 		const struct arm_platform_info *best_info = NULL;
    119 		const int phandle = OF_peer(0);
    120 		int match, best_match = 0;
    121 
    122 		__link_set_foreach(info, arm_platforms) {
    123 			const struct device_compatible_entry compat_data[] = {
    124 				{ .compat = (*info)->api_compat },
    125 				DEVICE_COMPAT_EOL
    126 			};
    127 
    128 			match = of_compatible_match(phandle, compat_data);
    129 			if (match > best_match) {
    130 				best_match = match;
    131 				best_info = *info;
    132 			}
    133 		}
    134 
    135 		booted_platform = best_info;
    136 	}
    137 
    138 	/*
    139 	 * No SoC specific platform was found. Try to find a generic
    140 	 * platform definition and use that if available.
    141 	 */
    142 	if (booted_platform == NULL) {
    143 		__link_set_foreach(info, arm_platforms) {
    144 			if (strcmp((*info)->api_compat, ARM_PLATFORM_DEFAULT) == 0) {
    145 				booted_platform = *info;
    146 				break;
    147 			}
    148 		}
    149 	}
    150 
    151 	return booted_platform == NULL ? NULL : booted_platform->api_ops;
    152 }
    153 
    154 void
    155 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
    156 {
    157 	struct arm_fdt_cpu_hatch_cb *c;
    158 
    159 	c = kmem_alloc(sizeof(*c), KM_SLEEP);
    160 	c->priv = priv;
    161 	c->cb = cb;
    162 	TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
    163 }
    164 
    165 void
    166 arm_fdt_cpu_hatch(struct cpu_info *ci)
    167 {
    168 	struct arm_fdt_cpu_hatch_cb *c;
    169 
    170 	TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
    171 		c->cb(c->priv, ci);
    172 }
    173 
    174 static void
    175 arm_fdt_irq_default_handler(void *frame)
    176 {
    177 	panic("missing interrupt controller driver");
    178 }
    179 
    180 void
    181 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
    182 {
    183 	KASSERT(_arm_fdt_irq_handler == arm_fdt_irq_default_handler);
    184 	_arm_fdt_irq_handler = irq_handler;
    185 }
    186 
    187 void
    188 arm_fdt_irq_handler(void *tf)
    189 {
    190 	_arm_fdt_irq_handler(tf);
    191 }
    192 
    193 void
    194 arm_fdt_timer_register(void (*timerfn)(void))
    195 {
    196 	if (_arm_fdt_timer_init != NULL) {
    197 #ifdef DIAGNOSTIC
    198 		aprint_verbose("%s: timer already registered\n", __func__);
    199 #endif
    200 		return;
    201 	}
    202 	_arm_fdt_timer_init = timerfn;
    203 }
    204 
    205 void
    206 arm_fdt_memory_dump(paddr_t pa)
    207 {
    208 	const struct arm_platform *plat = arm_fdt_platform();
    209 	struct fdt_attach_args faa;
    210 	bus_space_tag_t bst;
    211 	bus_space_handle_t bsh;
    212 
    213 	plat->ap_init_attach_args(&faa);
    214 
    215 	bst = faa.faa_bst;
    216 	bus_space_map(bst, pa, 0x100, 0, &bsh);
    217 
    218 	for (int i = 0; i < 0x100; i += 0x10) {
    219 		printf("%" PRIxPTR ": %08x %08x %08x %08x\n",
    220 		    (uintptr_t)(pa + i),
    221 		    bus_space_read_4(bst, bsh, i + 0),
    222 		    bus_space_read_4(bst, bsh, i + 4),
    223 		    bus_space_read_4(bst, bsh, i + 8),
    224 		    bus_space_read_4(bst, bsh, i + 12));
    225 	}
    226 }
    227 
    228 #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
    229 void
    230 cpu_initclocks(void)
    231 {
    232 	if (_arm_fdt_timer_init == NULL)
    233 		panic("cpu_initclocks: no timer registered");
    234 	_arm_fdt_timer_init();
    235 }
    236 #endif
    237 
    238 void
    239 arm_fdt_module_init(void)
    240 {
    241 #ifdef MODULAR
    242 	const int chosen = OF_finddevice("/chosen");
    243 	const char *module_name;
    244 	const uint64_t *data;
    245 	u_int index;
    246 	paddr_t pa;
    247 	vaddr_t va;
    248 	int len;
    249 
    250 	if (chosen == -1)
    251 		return;
    252 
    253 	data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
    254 	if (data == NULL)
    255 		return;
    256 
    257 	for (index = 0; index < len / 16; index++, data += 2) {
    258 		module_name = fdtbus_get_string_index(chosen,
    259 		    "netbsd,module-names", index);
    260 		if (module_name == NULL)
    261 			break;
    262 
    263 		const paddr_t startpa = (paddr_t)be64dec(data + 0);
    264 		const size_t size = (size_t)be64dec(data + 1);
    265 		const paddr_t endpa = round_page(startpa + size);
    266 
    267 		const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
    268 		    0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    269 		if (startva == 0) {
    270 			printf("ERROR: Cannot allocate VA for module %s\n",
    271 			    module_name);
    272 			continue;
    273 		}
    274 
    275 		for (pa = startpa, va = startva;
    276 		     pa < endpa;
    277 		     pa += PAGE_SIZE, va += PAGE_SIZE) {
    278 			pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
    279 		}
    280 		pmap_update(pmap_kernel());
    281 
    282 		module_prime(module_name, (void *)(uintptr_t)startva, size);
    283 	}
    284 #endif /* !MODULAR */
    285 }
    286 
    287 #ifdef EFI_RUNTIME
    288 static void
    289 arm_fdt_efi_init(device_t dev)
    290 {
    291 	uint64_t efi_system_table;
    292 	struct efi_tm tm;
    293 	int error;
    294 
    295 	const int chosen = OF_finddevice("/chosen");
    296 	if (chosen < 0)
    297 		return;
    298 
    299 	if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
    300 		return;
    301 
    302 	error = arm_efirt_init(efi_system_table);
    303 	if (error)
    304 		return;
    305 
    306 	aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
    307 
    308 	if (arm_efirt_gettime(&tm) == 0) {
    309 		aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
    310 		efi_todr.cookie = NULL;
    311 		efi_todr.todr_gettime_ymdhms = arm_fdt_efi_rtc_gettime;
    312 		efi_todr.todr_settime_ymdhms = arm_fdt_efi_rtc_settime;
    313 		todr_attach(&efi_todr);
    314 	}
    315 }
    316 
    317 static int
    318 arm_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    319 {
    320 	struct efi_tm tm;
    321 	int error;
    322 
    323 	error = arm_efirt_gettime(&tm);
    324 	if (error)
    325 		return error;
    326 
    327 	dt->dt_year = tm.tm_year;
    328 	dt->dt_mon = tm.tm_mon;
    329 	dt->dt_day = tm.tm_mday;
    330 	dt->dt_wday = 0;
    331 	dt->dt_hour = tm.tm_hour;
    332 	dt->dt_min = tm.tm_min;
    333 	dt->dt_sec = tm.tm_sec;
    334 
    335 	return 0;
    336 }
    337 
    338 static int
    339 arm_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    340 {
    341 	struct efi_tm tm;
    342 
    343 	memset(&tm, 0, sizeof(tm));
    344 	tm.tm_year = dt->dt_year;
    345 	tm.tm_mon = dt->dt_mon;
    346 	tm.tm_mday = dt->dt_day;
    347 	tm.tm_hour = dt->dt_hour;
    348 	tm.tm_min = dt->dt_min;
    349 	tm.tm_sec = dt->dt_sec;
    350 
    351 	return arm_efirt_settime(&tm);
    352 }
    353 #endif
    354