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