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