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