nexus.c revision 265960
1/*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. 4 * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>. 5 * All rights reserved. 6 * 7 * Permission to use, copy, modify, and distribute this software and 8 * its documentation for any purpose and without fee is hereby 9 * granted, provided that both the above copyright notice and this 10 * permission notice appear in all copies, that both the above 11 * copyright notice and this permission notice appear in all 12 * supporting documentation, and that the name of M.I.T. not be used 13 * in advertising or publicity pertaining to distribution of the 14 * software without specific, written prior permission. M.I.T. makes 15 * no representations about the suitability of this software for any 16 * purpose. It is provided "as is" without express or implied 17 * warranty. 18 * 19 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 20 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 23 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: stable/10/sys/powerpc/powerpc/nexus.c 265960 2014-05-13 17:18:48Z ian $"); 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/bus.h> 41#include <sys/kernel.h> 42#include <sys/malloc.h> 43#include <sys/module.h> 44#include <sys/pcpu.h> 45#include <sys/rman.h> 46 47#include <vm/vm.h> 48#include <vm/pmap.h> 49 50#include <dev/ofw/ofw_bus.h> 51#include <dev/ofw/ofw_bus_subr.h> 52#include <dev/ofw/openfirm.h> 53 54#include <machine/bus.h> 55#include <machine/intr_machdep.h> 56#include <machine/resource.h> 57 58/* 59 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 60 * hang from the Open Firmware root node and adds them as devices to this bus 61 * (except some special nodes which are excluded) so that drivers can be 62 * attached to them. 63 * 64 * Additionally, interrupt setup/teardown and some resource management are 65 * done at this level. 66 * 67 * Maybe this code should get into dev/ofw to some extent, as some of it should 68 * work for all Open Firmware based machines... 69 */ 70 71struct nexus_devinfo { 72 struct ofw_bus_devinfo ndi_obdinfo; 73 struct resource_list ndi_rl; 74}; 75 76struct nexus_softc { 77 uint32_t acells, scells; 78 struct rman sc_intr_rman; 79 struct rman sc_mem_rman; 80}; 81 82static device_probe_t nexus_probe; 83static device_attach_t nexus_attach; 84static bus_print_child_t nexus_print_child; 85static bus_add_child_t nexus_add_child; 86static bus_probe_nomatch_t nexus_probe_nomatch; 87static bus_setup_intr_t nexus_setup_intr; 88static bus_teardown_intr_t nexus_teardown_intr; 89static bus_alloc_resource_t nexus_alloc_resource; 90static bus_activate_resource_t nexus_activate_resource; 91static bus_deactivate_resource_t nexus_deactivate_resource; 92static bus_adjust_resource_t nexus_adjust_resource; 93static bus_release_resource_t nexus_release_resource; 94static bus_get_resource_list_t nexus_get_resource_list; 95#ifdef SMP 96static bus_bind_intr_t nexus_bind_intr; 97#endif 98static bus_config_intr_t nexus_config_intr; 99static ofw_bus_get_devinfo_t nexus_get_devinfo; 100 101static int nexus_inlist(const char *, const char *const *); 102static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t); 103static void nexus_destroy_dinfo(struct nexus_devinfo *); 104static int nexus_print_res(struct nexus_devinfo *); 105 106static device_method_t nexus_methods[] = { 107 /* Device interface */ 108 DEVMETHOD(device_probe, nexus_probe), 109 DEVMETHOD(device_attach, nexus_attach), 110 DEVMETHOD(device_detach, bus_generic_detach), 111 DEVMETHOD(device_shutdown, bus_generic_shutdown), 112 DEVMETHOD(device_suspend, bus_generic_suspend), 113 DEVMETHOD(device_resume, bus_generic_resume), 114 115 /* Bus interface */ 116 DEVMETHOD(bus_print_child, nexus_print_child), 117 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 118 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 119 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 120 DEVMETHOD(bus_add_child, nexus_add_child), 121 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 122 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 123 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 124 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 125 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource), 126 DEVMETHOD(bus_release_resource, nexus_release_resource), 127 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 128 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 129 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 130 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 131 DEVMETHOD(bus_get_resource_list, nexus_get_resource_list), 132#ifdef SMP 133 DEVMETHOD(bus_bind_intr, nexus_bind_intr), 134#endif 135 DEVMETHOD(bus_config_intr, nexus_config_intr), 136 137 /* ofw_bus interface */ 138 DEVMETHOD(ofw_bus_get_devinfo, nexus_get_devinfo), 139 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 140 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 141 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 142 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 143 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 144 145 DEVMETHOD_END 146}; 147 148static devclass_t nexus_devclass; 149 150DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, sizeof(struct nexus_softc)); 151EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0, 152 BUS_PASS_BUS); 153MODULE_VERSION(nexus, 1); 154 155static const char *const nexus_excl_name[] = { 156 "FJSV,system", 157 "aliases", 158 "associations", 159 "chosen", 160 "cmp", 161 "counter-timer", /* No separate device; handled by psycho/sbus */ 162 "failsafe", 163 "memory", 164 "openprom", 165 "options", 166 "packages", 167 "physical-memory", 168 "rsc", 169 "sgcn", 170 "todsg", 171 "virtual-memory", 172 NULL 173}; 174 175static const char *const nexus_excl_type[] = { 176 "core", 177 "cpu", 178 NULL 179}; 180 181extern struct bus_space_tag nexus_bustag; 182extern struct bus_dma_tag nexus_dmatag; 183 184static int 185nexus_inlist(const char *name, const char *const *list) 186{ 187 int i; 188 189 if (name == NULL) 190 return (0); 191 for (i = 0; list[i] != NULL; i++) 192 if (strcmp(name, list[i]) == 0) 193 return (1); 194 return (0); 195} 196 197#define NEXUS_EXCLUDED(name, type) \ 198 (nexus_inlist((name), nexus_excl_name) || \ 199 ((type) != NULL && nexus_inlist((type), nexus_excl_type))) 200 201static int 202nexus_probe(device_t dev) 203{ 204 205 /* Nexus does always match. */ 206 device_set_desc(dev, "Open Firmware Nexus device"); 207 return (0); 208} 209 210static int 211nexus_attach(device_t dev) 212{ 213 struct nexus_devinfo *ndi; 214 struct nexus_softc *sc; 215 device_t cdev; 216 phandle_t node; 217 218 sc = device_get_softc(dev); 219 220 if (strcmp(device_get_name(device_get_parent(dev)), "root") == 0) { 221 node = OF_peer(0); 222 223 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 224 sc->sc_intr_rman.rm_descr = "Interrupts"; 225 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 226 sc->sc_mem_rman.rm_descr = "Device Memory"; 227 if (rman_init(&sc->sc_intr_rman) != 0 || 228 rman_init(&sc->sc_mem_rman) != 0 || 229 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 || 230 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) 231 != 0) 232 panic("%s: failed to set up rmans.", __func__); 233 } else 234 node = ofw_bus_get_node(dev); 235 236 /* 237 * Allow devices to identify. 238 */ 239 bus_generic_probe(dev); 240 241 /* 242 * If no Open Firmware, bail early 243 */ 244 if (node == -1) 245 return (bus_generic_attach(dev)); 246 247 /* 248 * Some important numbers 249 */ 250 sc->acells = 2; 251 OF_getprop(node, "#address-cells", &sc->acells, sizeof(sc->acells)); 252 sc->scells = 1; 253 OF_getprop(node, "#size-cells", &sc->scells, sizeof(sc->scells)); 254 255 /* 256 * Now walk the OFW tree and attach top-level devices. 257 */ 258 for (node = OF_child(node); node > 0; node = OF_peer(node)) { 259 if ((ndi = nexus_setup_dinfo(dev, node)) == NULL) 260 continue; 261 cdev = device_add_child(dev, NULL, -1); 262 if (cdev == NULL) { 263 device_printf(dev, "<%s>: device_add_child failed\n", 264 ndi->ndi_obdinfo.obd_name); 265 nexus_destroy_dinfo(ndi); 266 continue; 267 } 268 device_set_ivars(cdev, ndi); 269 } 270 return (bus_generic_attach(dev)); 271} 272 273static device_t 274nexus_add_child(device_t dev, u_int order, const char *name, int unit) 275{ 276 device_t cdev; 277 struct nexus_devinfo *ndi; 278 279 cdev = device_add_child_ordered(dev, order, name, unit); 280 if (cdev == NULL) 281 return (NULL); 282 283 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 284 ndi->ndi_obdinfo.obd_node = -1; 285 resource_list_init(&ndi->ndi_rl); 286 device_set_ivars(cdev, ndi); 287 288 return (cdev); 289} 290 291static int 292nexus_print_child(device_t bus, device_t child) 293{ 294 int rv; 295 296 rv = bus_print_child_header(bus, child); 297 rv += nexus_print_res(device_get_ivars(child)); 298 rv += bus_print_child_footer(bus, child); 299 return (rv); 300} 301 302static void 303nexus_probe_nomatch(device_t bus, device_t child) 304{ 305 const char *name, *type; 306 307 if (!bootverbose) 308 return; 309 310 name = ofw_bus_get_name(child); 311 type = ofw_bus_get_type(child); 312 313 device_printf(bus, "<%s>", 314 name != NULL ? name : "unknown"); 315 nexus_print_res(device_get_ivars(child)); 316 printf(" type %s (no driver attached)\n", 317 type != NULL ? type : "unknown"); 318} 319 320static int 321nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r, 322 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, 323 void **cookiep) 324{ 325 int error; 326 327 if (r == NULL) 328 panic("%s: NULL interrupt resource!", __func__); 329 330 if ((rman_get_flags(r) & RF_SHAREABLE) == 0) 331 flags |= INTR_EXCL; 332 333 /* We depend here on rman_activate_resource() being idempotent. */ 334 error = rman_activate_resource(r); 335 if (error) 336 return (error); 337 338 error = powerpc_setup_intr(device_get_nameunit(child), 339 rman_get_start(r), filt, intr, arg, flags, cookiep); 340 341 return (error); 342} 343 344static int 345nexus_teardown_intr(device_t bus __unused, device_t child __unused, 346 struct resource *r, void *ih) 347{ 348 349 if (r == NULL) 350 return (EINVAL); 351 352 return (powerpc_teardown_intr(ih)); 353} 354 355#ifdef SMP 356static int 357nexus_bind_intr(device_t bus __unused, device_t child __unused, 358 struct resource *r, int cpu) 359{ 360 361 return (powerpc_bind_intr(rman_get_start(r), cpu)); 362} 363#endif 364 365static int 366nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 367 enum intr_polarity pol) 368{ 369 370 return (powerpc_config_intr(irq, trig, pol)); 371} 372 373static struct resource * 374nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 375 u_long start, u_long end, u_long count, u_int flags) 376{ 377 struct nexus_softc *sc; 378 struct rman *rm; 379 struct resource *rv; 380 struct resource_list_entry *rle; 381 device_t nexus; 382 int isdefault, passthrough; 383 384 isdefault = (start == 0UL && end == ~0UL); 385 passthrough = (device_get_parent(child) != bus); 386 nexus = bus; 387 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 388 nexus = device_get_parent(nexus); 389 sc = device_get_softc(nexus); 390 rle = NULL; 391 392 if (!passthrough && isdefault) { 393 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 394 type, *rid); 395 if (rle == NULL) 396 return (NULL); 397 if (rle->res != NULL) 398 panic("%s: resource entry is busy", __func__); 399 start = rle->start; 400 count = ulmax(count, rle->count); 401 end = ulmax(rle->end, start + count - 1); 402 } 403 404 switch (type) { 405 case SYS_RES_IRQ: 406 rm = &sc->sc_intr_rman; 407 break; 408 case SYS_RES_MEMORY: 409 rm = &sc->sc_mem_rman; 410 break; 411 default: 412 return (NULL); 413 } 414 415 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 416 child); 417 if (rv == NULL) 418 return (NULL); 419 rman_set_rid(rv, *rid); 420 421 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, 422 *rid, rv) != 0) { 423 rman_release_resource(rv); 424 return (NULL); 425 } 426 427 if (!passthrough && rle != NULL) { 428 rle->res = rv; 429 rle->start = rman_get_start(rv); 430 rle->end = rman_get_end(rv); 431 rle->count = rle->end - rle->start + 1; 432 } 433 434 return (rv); 435} 436 437static int 438nexus_activate_resource(device_t bus __unused, device_t child __unused, 439 int type, int rid __unused, struct resource *r) 440{ 441 442 if (type == SYS_RES_MEMORY) { 443 vm_offset_t start; 444 void *p; 445 446 start = (vm_offset_t) rman_get_start(r); 447 if (bootverbose) 448 printf("nexus mapdev: start %zx, len %ld\n", start, 449 rman_get_size(r)); 450 451 p = pmap_mapdev(start, (vm_size_t) rman_get_size(r)); 452 if (p == NULL) 453 return (ENOMEM); 454 rman_set_virtual(r, p); 455 rman_set_bustag(r, &bs_be_tag); 456 rman_set_bushandle(r, (u_long)p); 457 } 458 return (rman_activate_resource(r)); 459} 460 461static int 462nexus_deactivate_resource(device_t bus __unused, device_t child __unused, 463 int type __unused, int rid __unused, struct resource *r) 464{ 465 466 /* 467 * If this is a memory resource, unmap it. 468 */ 469 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 470 bus_size_t psize; 471 472 psize = rman_get_size(r); 473 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize); 474 } 475 476 return (rman_deactivate_resource(r)); 477} 478 479static int 480nexus_adjust_resource(device_t bus, device_t child __unused, int type, 481 struct resource *r, u_long start, u_long end) 482{ 483 struct nexus_softc *sc; 484 struct rman *rm; 485 device_t nexus; 486 487 nexus = bus; 488 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 489 nexus = device_get_parent(nexus); 490 sc = device_get_softc(nexus); 491 switch (type) { 492 case SYS_RES_IRQ: 493 rm = &sc->sc_intr_rman; 494 break; 495 case SYS_RES_MEMORY: 496 rm = &sc->sc_mem_rman; 497 break; 498 default: 499 return (EINVAL); 500 } 501 if (rm == NULL) 502 return (ENXIO); 503 if (rman_is_region_manager(r, rm) == 0) 504 return (EINVAL); 505 return (rman_adjust_resource(r, start, end)); 506} 507 508static int 509nexus_release_resource(device_t bus __unused, device_t child, int type, 510 int rid, struct resource *r) 511{ 512 int error; 513 514 if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 515 error = bus_deactivate_resource(child, type, rid, r); 516 if (error) 517 return (error); 518 } 519 return (rman_release_resource(r)); 520} 521 522static struct resource_list * 523nexus_get_resource_list(device_t bus __unused, device_t child) 524{ 525 struct nexus_devinfo *ndi; 526 527 ndi = device_get_ivars(child); 528 return (&ndi->ndi_rl); 529} 530 531static const struct ofw_bus_devinfo * 532nexus_get_devinfo(device_t bus __unused, device_t child) 533{ 534 struct nexus_devinfo *ndi; 535 536 ndi = device_get_ivars(child); 537 return (&ndi->ndi_obdinfo); 538} 539 540static struct nexus_devinfo * 541nexus_setup_dinfo(device_t dev, phandle_t node) 542{ 543 struct nexus_softc *sc; 544 struct nexus_devinfo *ndi; 545 uint32_t *reg, *intr, icells; 546 uint64_t phys, size; 547 phandle_t iparent; 548 int i, j; 549 int nintr; 550 int nreg; 551 552 sc = device_get_softc(dev); 553 554 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 555 if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) { 556 free(ndi, M_DEVBUF); 557 return (NULL); 558 } 559 if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name, 560 ndi->ndi_obdinfo.obd_type)) { 561 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 562 free(ndi, M_DEVBUF); 563 return (NULL); 564 } 565 566 resource_list_init(&ndi->ndi_rl); 567 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 568 if (nreg == -1) 569 nreg = 0; 570 if (nreg % (sc->acells + sc->scells) != 0) { 571 if (bootverbose) 572 device_printf(dev, "Malformed reg property on <%s>\n", 573 ndi->ndi_obdinfo.obd_name); 574 nreg = 0; 575 } 576 577 for (i = 0; i < nreg; i += sc->acells + sc->scells) { 578 phys = size = 0; 579 for (j = 0; j < sc->acells; j++) { 580 phys <<= 32; 581 phys |= reg[i + j]; 582 } 583 for (j = 0; j < sc->scells; j++) { 584 size <<= 32; 585 size |= reg[i + sc->acells + j]; 586 } 587 /* Skip the dummy reg property of glue devices like ssm(4). */ 588 if (size != 0) 589 resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, i, 590 phys, phys + size - 1, size); 591 } 592 free(reg, M_OFWPROP); 593 594 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 595 (void **)&intr); 596 if (nintr > 0) { 597 iparent = 0; 598 OF_searchprop(node, "interrupt-parent", &iparent, 599 sizeof(iparent)); 600 OF_searchprop(iparent, "#interrupt-cells", &icells, 601 sizeof(icells)); 602 for (i = 0; i < nintr; i+= icells) { 603 intr[i] = MAP_IRQ(iparent, intr[i]); 604 resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, i, intr[i], 605 intr[i], 1); 606 if (icells > 1) { 607 powerpc_config_intr(intr[i], (intr[i+1] & 1) ? 608 INTR_TRIGGER_LEVEL : INTR_TRIGGER_EDGE, 609 INTR_POLARITY_LOW); 610 } 611 } 612 free(intr, M_OFWPROP); 613 } 614 615 return (ndi); 616} 617 618static void 619nexus_destroy_dinfo(struct nexus_devinfo *ndi) 620{ 621 622 resource_list_free(&ndi->ndi_rl); 623 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 624 free(ndi, M_DEVBUF); 625} 626 627static int 628nexus_print_res(struct nexus_devinfo *ndi) 629{ 630 int rv; 631 632 rv = 0; 633 rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY, 634 "%#lx"); 635 rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ, 636 "%ld"); 637 return (rv); 638} 639 640