nexus.c revision 296250
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: head/sys/sparc64/sparc64/nexus.c 296250 2016-03-01 02:59:06Z jhibbits $"); 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 46#include <dev/ofw/ofw_bus.h> 47#include <dev/ofw/ofw_bus_subr.h> 48#include <dev/ofw/openfirm.h> 49 50#include <machine/bus.h> 51#include <machine/bus_common.h> 52#include <machine/intr_machdep.h> 53#include <machine/nexusvar.h> 54#include <machine/ofw_nexus.h> 55#include <machine/resource.h> 56#include <machine/ver.h> 57 58#include <sys/rman.h> 59 60/* 61 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 62 * hang from the Open Firmware root node and adds them as devices to this bus 63 * (except some special nodes which are excluded) so that drivers can be 64 * attached to them. 65 * 66 * Additionally, interrupt setup/teardown and some resource management are 67 * done at this level. 68 * 69 * Maybe this code should get into dev/ofw to some extent, as some of it should 70 * work for all Open Firmware based machines... 71 */ 72 73struct nexus_devinfo { 74 struct ofw_bus_devinfo ndi_obdinfo; 75 struct resource_list ndi_rl; 76}; 77 78struct nexus_softc { 79 struct rman sc_intr_rman; 80 struct rman sc_mem_rman; 81}; 82 83static device_probe_t nexus_probe; 84static device_attach_t nexus_attach; 85static bus_print_child_t nexus_print_child; 86static bus_add_child_t nexus_add_child; 87static bus_probe_nomatch_t nexus_probe_nomatch; 88static bus_setup_intr_t nexus_setup_intr; 89static bus_teardown_intr_t nexus_teardown_intr; 90static bus_alloc_resource_t nexus_alloc_resource; 91static bus_activate_resource_t nexus_activate_resource; 92static bus_deactivate_resource_t nexus_deactivate_resource; 93static bus_adjust_resource_t nexus_adjust_resource; 94static bus_release_resource_t nexus_release_resource; 95static bus_get_resource_list_t nexus_get_resource_list; 96#ifdef SMP 97static bus_bind_intr_t nexus_bind_intr; 98#endif 99static bus_describe_intr_t nexus_describe_intr; 100static bus_get_dma_tag_t nexus_get_dma_tag; 101static bus_get_bus_tag_t nexus_get_bus_tag; 102static ofw_bus_get_devinfo_t nexus_get_devinfo; 103 104static int nexus_inlist(const char *, const char *const *); 105static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t); 106static void nexus_destroy_dinfo(struct nexus_devinfo *); 107static int nexus_print_res(struct nexus_devinfo *); 108 109static device_method_t nexus_methods[] = { 110 /* Device interface */ 111 DEVMETHOD(device_probe, nexus_probe), 112 DEVMETHOD(device_attach, nexus_attach), 113 DEVMETHOD(device_detach, bus_generic_detach), 114 DEVMETHOD(device_shutdown, bus_generic_shutdown), 115 DEVMETHOD(device_suspend, bus_generic_suspend), 116 DEVMETHOD(device_resume, bus_generic_resume), 117 118 /* Bus interface */ 119 DEVMETHOD(bus_print_child, nexus_print_child), 120 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 121 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 122 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 123 DEVMETHOD(bus_add_child, nexus_add_child), 124 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 125 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 126 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 127 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource), 128 DEVMETHOD(bus_release_resource, nexus_release_resource), 129 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 130 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 131 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 132 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 133 DEVMETHOD(bus_get_resource_list, nexus_get_resource_list), 134#ifdef SMP 135 DEVMETHOD(bus_bind_intr, nexus_bind_intr), 136#endif 137 DEVMETHOD(bus_describe_intr, nexus_describe_intr), 138 DEVMETHOD(bus_get_dma_tag, nexus_get_dma_tag), 139 DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag), 140 141 /* ofw_bus interface */ 142 DEVMETHOD(ofw_bus_get_devinfo, nexus_get_devinfo), 143 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 144 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 145 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 146 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 147 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 148 149 DEVMETHOD_END 150}; 151 152static devclass_t nexus_devclass; 153 154DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, sizeof(struct nexus_softc)); 155EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0, 156 BUS_PASS_BUS); 157MODULE_VERSION(nexus, 1); 158 159static const char *const nexus_excl_name[] = { 160 "FJSV,system", 161 "aliases", 162 "associations", 163 "chosen", 164 "cmp", 165 "counter-timer", /* No separate device; handled by psycho/sbus */ 166 "failsafe", 167 "memory", 168 "openprom", 169 "options", 170 "packages", 171 "physical-memory", 172 "rsc", 173 "sgcn", 174 "todsg", 175 "virtual-memory", 176 NULL 177}; 178 179static const char *const nexus_excl_type[] = { 180 "core", 181 "cpu", 182 NULL 183}; 184 185extern struct bus_space_tag nexus_bustag; 186extern struct bus_dma_tag nexus_dmatag; 187 188static int 189nexus_inlist(const char *name, const char *const *list) 190{ 191 int i; 192 193 if (name == NULL) 194 return (0); 195 for (i = 0; list[i] != NULL; i++) 196 if (strcmp(name, list[i]) == 0) 197 return (1); 198 return (0); 199} 200 201#define NEXUS_EXCLUDED(name, type) \ 202 (nexus_inlist((name), nexus_excl_name) || \ 203 ((type) != NULL && nexus_inlist((type), nexus_excl_type))) 204 205static int 206nexus_probe(device_t dev) 207{ 208 209 /* Nexus does always match. */ 210 device_set_desc(dev, "Open Firmware Nexus device"); 211 return (0); 212} 213 214static int 215nexus_attach(device_t dev) 216{ 217 struct nexus_devinfo *ndi; 218 struct nexus_softc *sc; 219 device_t cdev; 220 phandle_t node; 221 222 if (strcmp(device_get_name(device_get_parent(dev)), "root") == 0) { 223 node = OF_peer(0); 224 if (node == -1) 225 panic("%s: OF_peer failed.", __func__); 226 227 sc = device_get_softc(dev); 228 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 229 sc->sc_intr_rman.rm_descr = "Interrupts"; 230 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 231 sc->sc_mem_rman.rm_descr = "Device Memory"; 232 if (rman_init(&sc->sc_intr_rman) != 0 || 233 rman_init(&sc->sc_mem_rman) != 0 || 234 rman_manage_region(&sc->sc_intr_rman, 0, 235 IV_MAX - 1) != 0 || 236 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0) 237 panic("%s: failed to set up rmans.", __func__); 238 } else 239 node = ofw_bus_get_node(dev); 240 241 /* 242 * Allow devices to identify. 243 */ 244 bus_generic_probe(dev); 245 246 /* 247 * Now walk the OFW tree and attach top-level devices. 248 */ 249 for (node = OF_child(node); node > 0; node = OF_peer(node)) { 250 if ((ndi = nexus_setup_dinfo(dev, node)) == NULL) 251 continue; 252 cdev = device_add_child(dev, NULL, -1); 253 if (cdev == NULL) { 254 device_printf(dev, "<%s>: device_add_child failed\n", 255 ndi->ndi_obdinfo.obd_name); 256 nexus_destroy_dinfo(ndi); 257 continue; 258 } 259 device_set_ivars(cdev, ndi); 260 } 261 return (bus_generic_attach(dev)); 262} 263 264static device_t 265nexus_add_child(device_t dev, u_int order, const char *name, int unit) 266{ 267 device_t cdev; 268 struct nexus_devinfo *ndi; 269 270 cdev = device_add_child_ordered(dev, order, name, unit); 271 if (cdev == NULL) 272 return (NULL); 273 274 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 275 ndi->ndi_obdinfo.obd_node = -1; 276 ndi->ndi_obdinfo.obd_name = strdup(name, M_OFWPROP); 277 resource_list_init(&ndi->ndi_rl); 278 device_set_ivars(cdev, ndi); 279 280 return (cdev); 281} 282 283static int 284nexus_print_child(device_t bus, device_t child) 285{ 286 int rv; 287 288 rv = bus_print_child_header(bus, child); 289 rv += nexus_print_res(device_get_ivars(child)); 290 rv += bus_print_child_footer(bus, child); 291 return (rv); 292} 293 294static void 295nexus_probe_nomatch(device_t bus, device_t child) 296{ 297 const char *type; 298 299 device_printf(bus, "<%s>", ofw_bus_get_name(child)); 300 nexus_print_res(device_get_ivars(child)); 301 type = ofw_bus_get_type(child); 302 printf(" type %s (no driver attached)\n", 303 type != NULL ? type : "unknown"); 304} 305 306static int 307nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r, 308 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, 309 void **cookiep) 310{ 311 int error; 312 313 if (r == NULL) 314 panic("%s: NULL interrupt resource!", __func__); 315 316 if ((rman_get_flags(r) & RF_SHAREABLE) == 0) 317 flags |= INTR_EXCL; 318 319 /* We depend here on rman_activate_resource() being idempotent. */ 320 error = rman_activate_resource(r); 321 if (error) 322 return (error); 323 324 error = inthand_add(device_get_nameunit(child), rman_get_start(r), 325 filt, intr, arg, flags, cookiep); 326 327 /* 328 * XXX in case of the AFB/FFB interrupt and a Psycho, Sabre or U2S 329 * bridge enable the interrupt in the respective bridge. 330 */ 331 332 return (error); 333} 334 335static int 336nexus_teardown_intr(device_t bus __unused, device_t child __unused, 337 struct resource *r, void *ih) 338{ 339 340 inthand_remove(rman_get_start(r), ih); 341 return (0); 342} 343 344#ifdef SMP 345static int 346nexus_bind_intr(device_t bus __unused, device_t child __unused, 347 struct resource *r, int cpu) 348{ 349 350 return (intr_bind(rman_get_start(r), cpu)); 351} 352#endif 353 354static int 355nexus_describe_intr(device_t bus __unused, device_t child __unused, 356 struct resource *r, void *cookie, const char *descr) 357{ 358 359 return (intr_describe(rman_get_start(r), cookie, descr)); 360} 361 362static struct resource * 363nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 364 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 365{ 366 struct nexus_softc *sc; 367 struct rman *rm; 368 struct resource *rv; 369 struct resource_list_entry *rle; 370 device_t nexus; 371 int isdefault, passthrough; 372 373 isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 374 passthrough = (device_get_parent(child) != bus); 375 nexus = bus; 376 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 377 nexus = device_get_parent(nexus); 378 sc = device_get_softc(nexus); 379 rle = NULL; 380 381 if (!passthrough) { 382 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 383 type, *rid); 384 if (rle == NULL) 385 return (NULL); 386 if (rle->res != NULL) 387 panic("%s: resource entry is busy", __func__); 388 if (isdefault) { 389 start = rle->start; 390 count = ulmax(count, rle->count); 391 end = ulmax(rle->end, start + count - 1); 392 } 393 } 394 395 switch (type) { 396 case SYS_RES_IRQ: 397 rm = &sc->sc_intr_rman; 398 break; 399 case SYS_RES_MEMORY: 400 rm = &sc->sc_mem_rman; 401 break; 402 default: 403 return (NULL); 404 } 405 406 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 407 child); 408 if (rv == NULL) 409 return (NULL); 410 rman_set_rid(rv, *rid); 411 412 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, 413 *rid, rv) != 0) { 414 rman_release_resource(rv); 415 return (NULL); 416 } 417 418 if (!passthrough) { 419 rle->res = rv; 420 rle->start = rman_get_start(rv); 421 rle->end = rman_get_end(rv); 422 rle->count = rle->end - rle->start + 1; 423 } 424 425 return (rv); 426} 427 428static int 429nexus_activate_resource(device_t bus __unused, device_t child __unused, 430 int type, int rid __unused, struct resource *r) 431{ 432 433 if (type == SYS_RES_MEMORY) { 434 rman_set_bustag(r, &nexus_bustag); 435 rman_set_bushandle(r, rman_get_start(r)); 436 } 437 return (rman_activate_resource(r)); 438} 439 440static int 441nexus_deactivate_resource(device_t bus __unused, device_t child __unused, 442 int type __unused, int rid __unused, struct resource *r) 443{ 444 445 return (rman_deactivate_resource(r)); 446} 447 448static int 449nexus_adjust_resource(device_t bus, device_t child __unused, int type, 450 struct resource *r, rman_res_t start, rman_res_t end) 451{ 452 struct nexus_softc *sc; 453 struct rman *rm; 454 device_t nexus; 455 456 nexus = bus; 457 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 458 nexus = device_get_parent(nexus); 459 sc = device_get_softc(nexus); 460 switch (type) { 461 case SYS_RES_IRQ: 462 rm = &sc->sc_intr_rman; 463 break; 464 case SYS_RES_MEMORY: 465 rm = &sc->sc_mem_rman; 466 break; 467 default: 468 return (EINVAL); 469 } 470 if (rm == NULL) 471 return (ENXIO); 472 if (rman_is_region_manager(r, rm) == 0) 473 return (EINVAL); 474 return (rman_adjust_resource(r, start, end)); 475} 476 477static int 478nexus_release_resource(device_t bus __unused, device_t child, int type, 479 int rid, struct resource *r) 480{ 481 int error; 482 483 if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 484 error = bus_deactivate_resource(child, type, rid, r); 485 if (error) 486 return (error); 487 } 488 return (rman_release_resource(r)); 489} 490 491static struct resource_list * 492nexus_get_resource_list(device_t bus __unused, device_t child) 493{ 494 struct nexus_devinfo *ndi; 495 496 ndi = device_get_ivars(child); 497 return (&ndi->ndi_rl); 498} 499 500static bus_dma_tag_t 501nexus_get_dma_tag(device_t bus __unused, device_t child __unused) 502{ 503 504 return (&nexus_dmatag); 505} 506 507static bus_space_tag_t 508nexus_get_bus_tag(device_t bus __unused, device_t child __unused) 509{ 510 511 return (&nexus_bustag); 512} 513 514static const struct ofw_bus_devinfo * 515nexus_get_devinfo(device_t bus __unused, device_t child) 516{ 517 struct nexus_devinfo *ndi; 518 519 ndi = device_get_ivars(child); 520 return (&ndi->ndi_obdinfo); 521} 522 523static struct nexus_devinfo * 524nexus_setup_dinfo(device_t dev, phandle_t node) 525{ 526 struct nexus_devinfo *ndi; 527 struct nexus_regs *reg; 528 bus_addr_t phys; 529 bus_size_t size; 530 uint32_t ign; 531 uint32_t *intr; 532 int i; 533 int nintr; 534 int nreg; 535 536 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 537 if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) { 538 free(ndi, M_DEVBUF); 539 return (NULL); 540 } 541 if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name, 542 ndi->ndi_obdinfo.obd_type)) { 543 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 544 free(ndi, M_DEVBUF); 545 return (NULL); 546 } 547 resource_list_init(&ndi->ndi_rl); 548 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 549 if (nreg == -1) { 550 device_printf(dev, "<%s>: incomplete\n", 551 ndi->ndi_obdinfo.obd_name); 552 goto fail; 553 } 554 for (i = 0; i < nreg; i++) { 555 phys = NEXUS_REG_PHYS(®[i]); 556 size = NEXUS_REG_SIZE(®[i]); 557 /* Skip the dummy reg property of glue devices like ssm(4). */ 558 if (size != 0) 559 resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, i, 560 phys, phys + size - 1, size); 561 } 562 free(reg, M_OFWPROP); 563 564 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 565 (void **)&intr); 566 if (nintr > 0) { 567 if (OF_getprop(node, PCPU_GET(impl) < CPU_IMPL_ULTRASPARCIII ? 568 "upa-portid" : "portid", &ign, sizeof(ign)) <= 0) { 569 device_printf(dev, "<%s>: could not determine portid\n", 570 ndi->ndi_obdinfo.obd_name); 571 free(intr, M_OFWPROP); 572 goto fail; 573 } 574 575 /* XXX 7-bit MID on Starfire */ 576 ign = (ign << INTMAP_IGN_SHIFT) & INTMAP_IGN_MASK; 577 for (i = 0; i < nintr; i++) { 578 intr[i] |= ign; 579 resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, i, intr[i], 580 intr[i], 1); 581 } 582 free(intr, M_OFWPROP); 583 } 584 585 return (ndi); 586 587 fail: 588 nexus_destroy_dinfo(ndi); 589 return (NULL); 590} 591 592static void 593nexus_destroy_dinfo(struct nexus_devinfo *ndi) 594{ 595 596 resource_list_free(&ndi->ndi_rl); 597 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 598 free(ndi, M_DEVBUF); 599} 600 601static int 602nexus_print_res(struct nexus_devinfo *ndi) 603{ 604 int rv; 605 606 rv = 0; 607 rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY, 608 "%#lx"); 609 rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ, 610 "%ld"); 611 return (rv); 612} 613