1 /* $OpenBSD: mainbus.c,v 1.19 2020/08/26 03:29:05 visa Exp $ */ 2 /* 3 * Copyright (c) 2016 Patrick Wildt <patrick@blueri.se> 4 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/device.h> 23 #include <sys/malloc.h> 24 25 #include <machine/fdt.h> 26 #include <dev/ofw/openfirm.h> 27 #include <dev/ofw/fdt.h> 28 #include <dev/ofw/ofw_thermal.h> 29 30 #include <arm64/arm64/arm64var.h> 31 #include <arm64/dev/mainbus.h> 32 33 int mainbus_match(struct device *, void *, void *); 34 void mainbus_attach(struct device *, struct device *, void *); 35 36 void mainbus_attach_node(struct device *, int, cfmatch_t); 37 int mainbus_match_status(struct device *, void *, void *); 38 void mainbus_attach_cpus(struct device *, cfmatch_t); 39 int mainbus_match_primary(struct device *, void *, void *); 40 int mainbus_match_secondary(struct device *, void *, void *); 41 void mainbus_attach_psci(struct device *); 42 void mainbus_attach_efi(struct device *); 43 void mainbus_attach_apm(struct device *); 44 void mainbus_attach_framebuffer(struct device *); 45 46 struct mainbus_softc { 47 struct device sc_dev; 48 int sc_node; 49 bus_space_tag_t sc_iot; 50 bus_dma_tag_t sc_dmat; 51 int sc_acells; 52 int sc_scells; 53 int *sc_ranges; 54 int sc_rangeslen; 55 int sc_early; 56 int sc_early_nodes[64]; 57 }; 58 59 struct cfattach mainbus_ca = { 60 sizeof(struct mainbus_softc), mainbus_match, mainbus_attach, NULL, 61 config_activate_children 62 }; 63 64 struct cfdriver mainbus_cd = { 65 NULL, "mainbus", DV_DULL 66 }; 67 68 struct machine_bus_dma_tag mainbus_dma_tag = { 69 NULL, 70 0, 71 _dmamap_create, 72 _dmamap_destroy, 73 _dmamap_load, 74 _dmamap_load_mbuf, 75 _dmamap_load_uio, 76 _dmamap_load_raw, 77 _dmamap_load_buffer, 78 _dmamap_unload, 79 _dmamap_sync, 80 _dmamem_alloc, 81 _dmamem_free, 82 _dmamem_map, 83 _dmamem_unmap, 84 _dmamem_mmap, 85 }; 86 87 /* 88 * Mainbus takes care of FDT and non-FDT machines, so we 89 * always attach. 90 */ 91 int 92 mainbus_match(struct device *parent, void *cfdata, void *aux) 93 { 94 return (1); 95 } 96 97 void agtimer_init(void); 98 99 void 100 mainbus_attach(struct device *parent, struct device *self, void *aux) 101 { 102 struct mainbus_softc *sc = (struct mainbus_softc *)self; 103 char prop[128]; 104 int node, len; 105 106 arm_intr_init_fdt(); 107 agtimer_init(); 108 109 sc->sc_node = OF_peer(0); 110 sc->sc_iot = &arm64_bs_tag; 111 sc->sc_dmat = &mainbus_dma_tag; 112 sc->sc_acells = OF_getpropint(OF_peer(0), "#address-cells", 1); 113 sc->sc_scells = OF_getpropint(OF_peer(0), "#size-cells", 1); 114 115 len = OF_getprop(sc->sc_node, "model", prop, sizeof(prop)); 116 if (len > 0) { 117 printf(": %s\n", prop); 118 hw_prod = malloc(len, M_DEVBUF, M_NOWAIT); 119 if (hw_prod) 120 strlcpy(hw_prod, prop, len); 121 } else 122 printf(": unknown model\n"); 123 124 len = OF_getprop(sc->sc_node, "serial-number", prop, sizeof(prop)); 125 if (len > 0) { 126 hw_serial = malloc(len, M_DEVBUF, M_NOWAIT); 127 if (hw_serial) 128 strlcpy(hw_serial, prop, len); 129 } 130 131 mainbus_attach_psci(self); 132 133 /* Attach primary CPU first. */ 134 mainbus_attach_cpus(self, mainbus_match_primary); 135 136 /* Attach secondary CPUs. */ 137 mainbus_attach_cpus(self, mainbus_match_secondary); 138 139 mainbus_attach_efi(self); 140 141 sc->sc_rangeslen = OF_getproplen(OF_peer(0), "ranges"); 142 if (sc->sc_rangeslen > 0 && !(sc->sc_rangeslen % sizeof(uint32_t))) { 143 sc->sc_ranges = malloc(sc->sc_rangeslen, M_TEMP, M_WAITOK); 144 OF_getpropintarray(OF_peer(0), "ranges", sc->sc_ranges, 145 sc->sc_rangeslen); 146 } 147 148 mainbus_attach_apm(self); 149 150 /* Scan the whole tree. */ 151 sc->sc_early = 1; 152 for (node = OF_child(sc->sc_node); node != 0; node = OF_peer(node)) 153 mainbus_attach_node(self, node, NULL); 154 155 sc->sc_early = 0; 156 for (node = OF_child(sc->sc_node); node != 0; node = OF_peer(node)) 157 mainbus_attach_node(self, node, NULL); 158 159 mainbus_attach_framebuffer(self); 160 161 thermal_init(); 162 } 163 164 int 165 mainbus_print(void *aux, const char *pnp) 166 { 167 struct fdt_attach_args *fa = aux; 168 char buf[32]; 169 170 if (!pnp) 171 return (QUIET); 172 173 if (OF_getprop(fa->fa_node, "status", buf, sizeof(buf)) > 0 && 174 strcmp(buf, "disabled") == 0) 175 return (QUIET); 176 177 if (OF_getprop(fa->fa_node, "name", buf, sizeof(buf)) > 0) { 178 buf[sizeof(buf) - 1] = 0; 179 if (strcmp(buf, "aliases") == 0 || 180 strcmp(buf, "chosen") == 0 || 181 strcmp(buf, "cpus") == 0 || 182 strcmp(buf, "memory") == 0 || 183 strcmp(buf, "reserved-memory") == 0 || 184 strcmp(buf, "thermal-zones") == 0 || 185 strncmp(buf, "__", 2) == 0) 186 return (QUIET); 187 printf("\"%s\"", buf); 188 } else 189 printf("node %u", fa->fa_node); 190 191 printf(" at %s", pnp); 192 193 return (UNCONF); 194 } 195 196 /* 197 * Look for a driver that wants to be attached to this node. 198 */ 199 void 200 mainbus_attach_node(struct device *self, int node, cfmatch_t submatch) 201 { 202 struct mainbus_softc *sc = (struct mainbus_softc *)self; 203 struct fdt_attach_args fa; 204 int i, len, line; 205 uint32_t *cell, *reg; 206 struct device *child; 207 cfprint_t print = NULL; 208 209 /* Skip if already attached early. */ 210 for (i = 0; i < nitems(sc->sc_early_nodes); i++) { 211 if (sc->sc_early_nodes[i] == node) 212 return; 213 if (sc->sc_early_nodes[i] == 0) 214 break; 215 } 216 217 memset(&fa, 0, sizeof(fa)); 218 fa.fa_name = ""; 219 fa.fa_node = node; 220 fa.fa_iot = sc->sc_iot; 221 fa.fa_dmat = sc->sc_dmat; 222 fa.fa_acells = sc->sc_acells; 223 fa.fa_scells = sc->sc_scells; 224 225 len = OF_getproplen(node, "reg"); 226 line = (sc->sc_acells + sc->sc_scells) * sizeof(uint32_t); 227 if (len > 0 && (len % line) == 0) { 228 reg = malloc(len, M_TEMP, M_WAITOK); 229 OF_getpropintarray(node, "reg", reg, len); 230 231 fa.fa_reg = malloc((len / line) * sizeof(struct fdt_reg), 232 M_DEVBUF, M_WAITOK); 233 fa.fa_nreg = (len / line); 234 235 for (i = 0, cell = reg; i < len / line; i++) { 236 if (sc->sc_acells >= 1) 237 fa.fa_reg[i].addr = cell[0]; 238 if (sc->sc_acells == 2) { 239 fa.fa_reg[i].addr <<= 32; 240 fa.fa_reg[i].addr |= cell[1]; 241 } 242 cell += sc->sc_acells; 243 if (sc->sc_scells >= 1) 244 fa.fa_reg[i].size = cell[0]; 245 if (sc->sc_scells == 2) { 246 fa.fa_reg[i].size <<= 32; 247 fa.fa_reg[i].size |= cell[1]; 248 } 249 cell += sc->sc_scells; 250 } 251 252 free(reg, M_TEMP, len); 253 } 254 255 len = OF_getproplen(node, "interrupts"); 256 if (len > 0 && (len % sizeof(uint32_t)) == 0) { 257 fa.fa_intr = malloc(len, M_DEVBUF, M_WAITOK); 258 fa.fa_nintr = len / sizeof(uint32_t); 259 260 OF_getpropintarray(node, "interrupts", fa.fa_intr, len); 261 } 262 263 if (OF_getproplen(node, "dma-coherent") >= 0) { 264 fa.fa_dmat = malloc(sizeof(*sc->sc_dmat), 265 M_DEVBUF, M_WAITOK | M_ZERO); 266 memcpy(fa.fa_dmat, sc->sc_dmat, sizeof(*sc->sc_dmat)); 267 fa.fa_dmat->_flags |= BUS_DMA_COHERENT; 268 } 269 270 if (submatch == NULL && sc->sc_early == 0) 271 print = mainbus_print; 272 if (submatch == NULL) 273 submatch = mainbus_match_status; 274 275 child = config_found_sm(self, &fa, print, submatch); 276 277 /* Record nodes that we attach early. */ 278 if (child && sc->sc_early) { 279 for (i = 0; i < nitems(sc->sc_early_nodes); i++) { 280 if (sc->sc_early_nodes[i] != 0) 281 continue; 282 sc->sc_early_nodes[i] = node; 283 break; 284 } 285 } 286 287 free(fa.fa_reg, M_DEVBUF, fa.fa_nreg * sizeof(struct fdt_reg)); 288 free(fa.fa_intr, M_DEVBUF, fa.fa_nintr * sizeof(uint32_t)); 289 } 290 291 int 292 mainbus_match_status(struct device *parent, void *match, void *aux) 293 { 294 struct mainbus_softc *sc = (struct mainbus_softc *)parent; 295 struct fdt_attach_args *fa = aux; 296 struct cfdata *cf = match; 297 char buf[32]; 298 299 if (OF_getprop(fa->fa_node, "status", buf, sizeof(buf)) > 0 && 300 strcmp(buf, "disabled") == 0) 301 return 0; 302 303 if (cf->cf_loc[0] == sc->sc_early) 304 return (*cf->cf_attach->ca_match)(parent, match, aux); 305 306 return 0; 307 } 308 309 void 310 mainbus_attach_cpus(struct device *self, cfmatch_t match) 311 { 312 struct mainbus_softc *sc = (struct mainbus_softc *)self; 313 int node = OF_finddevice("/cpus"); 314 int acells, scells; 315 char buf[32]; 316 317 if (node == -1) 318 return; 319 320 acells = sc->sc_acells; 321 scells = sc->sc_scells; 322 sc->sc_acells = OF_getpropint(node, "#address-cells", 2); 323 sc->sc_scells = OF_getpropint(node, "#size-cells", 0); 324 325 ncpusfound = 0; 326 for (node = OF_child(node); node != 0; node = OF_peer(node)) { 327 if (OF_getprop(node, "device_type", buf, sizeof(buf)) > 0 && 328 strcmp(buf, "cpu") == 0) 329 ncpusfound++; 330 331 mainbus_attach_node(self, node, match); 332 } 333 334 sc->sc_acells = acells; 335 sc->sc_scells = scells; 336 } 337 338 int 339 mainbus_match_primary(struct device *parent, void *match, void *aux) 340 { 341 struct fdt_attach_args *fa = aux; 342 struct cfdata *cf = match; 343 uint64_t mpidr = READ_SPECIALREG(mpidr_el1); 344 345 if (fa->fa_nreg < 1 || fa->fa_reg[0].addr != (mpidr & MPIDR_AFF)) 346 return 0; 347 348 return (*cf->cf_attach->ca_match)(parent, match, aux); 349 } 350 351 int 352 mainbus_match_secondary(struct device *parent, void *match, void *aux) 353 { 354 struct fdt_attach_args *fa = aux; 355 struct cfdata *cf = match; 356 uint64_t mpidr = READ_SPECIALREG(mpidr_el1); 357 358 if (fa->fa_nreg < 1 || fa->fa_reg[0].addr == (mpidr & MPIDR_AFF)) 359 return 0; 360 361 return (*cf->cf_attach->ca_match)(parent, match, aux); 362 } 363 364 void 365 mainbus_attach_psci(struct device *self) 366 { 367 struct mainbus_softc *sc = (struct mainbus_softc *)self; 368 int node = OF_finddevice("/psci"); 369 370 if (node == -1) 371 return; 372 373 sc->sc_early = 1; 374 mainbus_attach_node(self, node, NULL); 375 sc->sc_early = 0; 376 } 377 378 void 379 mainbus_attach_efi(struct device *self) 380 { 381 struct mainbus_softc *sc = (struct mainbus_softc *)self; 382 struct fdt_attach_args fa; 383 int node = OF_finddevice("/chosen"); 384 385 if (node == -1 || 386 OF_getproplen(node, "openbsd,uefi-system-table") <= 0) 387 return; 388 389 memset(&fa, 0, sizeof(fa)); 390 fa.fa_name = "efi"; 391 fa.fa_iot = sc->sc_iot; 392 fa.fa_dmat = sc->sc_dmat; 393 config_found(self, &fa, NULL); 394 } 395 396 void 397 mainbus_attach_apm(struct device *self) 398 { 399 struct fdt_attach_args fa; 400 401 memset(&fa, 0, sizeof(fa)); 402 fa.fa_name = "apm"; 403 404 config_found(self, &fa, NULL); 405 } 406 407 void 408 mainbus_attach_framebuffer(struct device *self) 409 { 410 int node = OF_finddevice("/chosen"); 411 412 if (node == -1) 413 return; 414 415 for (node = OF_child(node); node != 0; node = OF_peer(node)) 416 mainbus_attach_node(self, node, NULL); 417 } 418