ofwbus.c revision 257016
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/dev/ofw/ofw_nexus.c 257016 2013-10-23 20:00:14Z nwhitehorn $"); 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/ofw_nexus.h> 53#include <dev/ofw/openfirm.h> 54 55#include <machine/bus.h> 56#include <machine/intr_machdep.h> 57#include <machine/resource.h> 58 59/* 60 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 61 * hang from the Open Firmware root node and adds them as devices to this bus 62 * (except some special nodes which are excluded) so that drivers can be 63 * attached to them. 64 * 65 * Additionally, interrupt setup/teardown and some resource management are 66 * done at this level. 67 * 68 */ 69 70struct nexus_devinfo { 71 struct ofw_bus_devinfo ndi_obdinfo; 72 struct resource_list ndi_rl; 73}; 74 75static device_probe_t nexus_probe; 76static device_attach_t nexus_attach; 77static bus_print_child_t nexus_print_child; 78static bus_add_child_t nexus_add_child; 79static bus_probe_nomatch_t nexus_probe_nomatch; 80static bus_alloc_resource_t nexus_alloc_resource; 81static bus_adjust_resource_t nexus_adjust_resource; 82static bus_release_resource_t nexus_release_resource; 83static bus_get_resource_list_t nexus_get_resource_list; 84static ofw_bus_get_devinfo_t nexus_get_devinfo; 85 86static int nexus_inlist(const char *, const char *const *); 87static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t); 88static void nexus_destroy_dinfo(struct nexus_devinfo *); 89static int nexus_print_res(struct nexus_devinfo *); 90 91static device_method_t nexus_methods[] = { 92 /* Device interface */ 93 DEVMETHOD(device_probe, nexus_probe), 94 DEVMETHOD(device_attach, nexus_attach), 95 DEVMETHOD(device_detach, bus_generic_detach), 96 DEVMETHOD(device_shutdown, bus_generic_shutdown), 97 DEVMETHOD(device_suspend, bus_generic_suspend), 98 DEVMETHOD(device_resume, bus_generic_resume), 99 100 /* Bus interface */ 101 DEVMETHOD(bus_print_child, nexus_print_child), 102 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 103 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 104 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 105 DEVMETHOD(bus_add_child, nexus_add_child), 106 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 107 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 108 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource), 109 DEVMETHOD(bus_release_resource, nexus_release_resource), 110 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 111 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 112 DEVMETHOD(bus_get_resource_list, nexus_get_resource_list), 113 114 /* ofw_bus interface */ 115 DEVMETHOD(ofw_bus_get_devinfo, nexus_get_devinfo), 116 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 117 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 118 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 119 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 120 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 121 122 DEVMETHOD_END 123}; 124 125DEFINE_CLASS_0(ofw_nexus, ofw_nexus_driver, nexus_methods, 126 sizeof(struct ofw_nexus_softc)); 127MODULE_VERSION(ofw_nexus, 1); 128 129static const char *const nexus_excl_name[] = { 130 "FJSV,system", 131 "aliases", 132 "associations", 133 "chosen", 134 "cmp", 135 "counter-timer", /* No separate device; handled by psycho/sbus */ 136 "failsafe", 137 "memory", 138 "openprom", 139 "options", 140 "packages", 141 "physical-memory", 142 "rsc", 143 "sgcn", 144 "todsg", 145 "virtual-memory", 146 NULL 147}; 148 149static const char *const nexus_excl_type[] = { 150 "core", 151 "cpu", 152 NULL 153}; 154 155static int 156nexus_inlist(const char *name, const char *const *list) 157{ 158 int i; 159 160 if (name == NULL) 161 return (0); 162 for (i = 0; list[i] != NULL; i++) 163 if (strcmp(name, list[i]) == 0) 164 return (1); 165 return (0); 166} 167 168#define NEXUS_EXCLUDED(name, type) \ 169 (nexus_inlist((name), nexus_excl_name) || \ 170 ((type) != NULL && nexus_inlist((type), nexus_excl_type))) 171 172static int 173nexus_probe(device_t dev) 174{ 175 176 /* Nexus does always match. */ 177 device_set_desc(dev, "Open Firmware Nexus device"); 178 return (0); 179} 180 181static int 182nexus_attach(device_t dev) 183{ 184 struct nexus_devinfo *ndi; 185 struct ofw_nexus_softc *sc; 186 device_t cdev; 187 phandle_t node; 188 189 sc = device_get_softc(dev); 190 191 if (strcmp(device_get_name(device_get_parent(dev)), "root") == 0) { 192 node = OF_peer(0); 193 194 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 195 sc->sc_intr_rman.rm_descr = "Interrupts"; 196 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 197 sc->sc_mem_rman.rm_descr = "Device Memory"; 198 if (rman_init(&sc->sc_intr_rman) != 0 || 199 rman_init(&sc->sc_mem_rman) != 0 || 200 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 || 201 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) 202 != 0) 203 panic("%s: failed to set up rmans.", __func__); 204 } else 205 node = ofw_bus_get_node(dev); 206 207 /* 208 * Allow devices to identify. 209 */ 210 bus_generic_probe(dev); 211 212 /* 213 * If no Open Firmware, bail early 214 */ 215 if (node == -1) 216 return (bus_generic_attach(dev)); 217 218 /* 219 * Some important numbers 220 */ 221 sc->acells = 2; 222 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells)); 223 sc->scells = 1; 224 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells)); 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, u_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 resource_list_init(&ndi->ndi_rl); 257 device_set_ivars(cdev, ndi); 258 259 return (cdev); 260} 261 262static int 263nexus_print_child(device_t bus, device_t child) 264{ 265 int rv; 266 267 rv = bus_print_child_header(bus, child); 268 rv += nexus_print_res(device_get_ivars(child)); 269 rv += bus_print_child_footer(bus, child); 270 return (rv); 271} 272 273static void 274nexus_probe_nomatch(device_t bus, device_t child) 275{ 276 const char *name, *type; 277 278 if (!bootverbose) 279 return; 280 281 name = ofw_bus_get_name(child); 282 type = ofw_bus_get_type(child); 283 284 device_printf(bus, "<%s>", 285 name != NULL ? name : "unknown"); 286 nexus_print_res(device_get_ivars(child)); 287 printf(" type %s (no driver attached)\n", 288 type != NULL ? type : "unknown"); 289} 290 291static struct resource * 292nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 293 u_long start, u_long end, u_long count, u_int flags) 294{ 295 struct ofw_nexus_softc *sc; 296 struct rman *rm; 297 struct resource *rv; 298 struct resource_list_entry *rle; 299 device_t nexus; 300 int isdefault, passthrough; 301 302 isdefault = (start == 0UL && end == ~0UL); 303 passthrough = (device_get_parent(child) != bus); 304 nexus = bus; 305 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 306 nexus = device_get_parent(nexus); 307 sc = device_get_softc(nexus); 308 rle = NULL; 309 310 if (!passthrough && isdefault) { 311 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 312 type, *rid); 313 if (rle == NULL) 314 return (NULL); 315 if (rle->res != NULL) 316 panic("%s: resource entry is busy", __func__); 317 start = rle->start; 318 count = ulmax(count, rle->count); 319 end = ulmax(rle->end, start + count - 1); 320 } 321 322 switch (type) { 323 case SYS_RES_IRQ: 324 rm = &sc->sc_intr_rman; 325 break; 326 case SYS_RES_MEMORY: 327 rm = &sc->sc_mem_rman; 328 break; 329 default: 330 return (NULL); 331 } 332 333 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 334 child); 335 if (rv == NULL) 336 return (NULL); 337 rman_set_rid(rv, *rid); 338 339 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, 340 *rid, rv) != 0) { 341 rman_release_resource(rv); 342 return (NULL); 343 } 344 345 if (!passthrough && rle != NULL) { 346 rle->res = rv; 347 rle->start = rman_get_start(rv); 348 rle->end = rman_get_end(rv); 349 rle->count = rle->end - rle->start + 1; 350 } 351 352 return (rv); 353} 354 355static int 356nexus_adjust_resource(device_t bus, device_t child __unused, int type, 357 struct resource *r, u_long start, u_long end) 358{ 359 struct ofw_nexus_softc *sc; 360 struct rman *rm; 361 device_t nexus; 362 363 nexus = bus; 364 while (strcmp(device_get_name(device_get_parent(nexus)), "root") != 0) 365 nexus = device_get_parent(nexus); 366 sc = device_get_softc(nexus); 367 switch (type) { 368 case SYS_RES_IRQ: 369 rm = &sc->sc_intr_rman; 370 break; 371 case SYS_RES_MEMORY: 372 rm = &sc->sc_mem_rman; 373 break; 374 default: 375 return (EINVAL); 376 } 377 if (rm == NULL) 378 return (ENXIO); 379 if (rman_is_region_manager(r, rm) == 0) 380 return (EINVAL); 381 return (rman_adjust_resource(r, start, end)); 382} 383 384static int 385nexus_release_resource(device_t bus __unused, device_t child, int type, 386 int rid, struct resource *r) 387{ 388 int error; 389 390 if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 391 error = bus_deactivate_resource(child, type, rid, r); 392 if (error) 393 return (error); 394 } 395 return (rman_release_resource(r)); 396} 397 398static struct resource_list * 399nexus_get_resource_list(device_t bus __unused, device_t child) 400{ 401 struct nexus_devinfo *ndi; 402 403 ndi = device_get_ivars(child); 404 return (&ndi->ndi_rl); 405} 406 407static const struct ofw_bus_devinfo * 408nexus_get_devinfo(device_t bus __unused, device_t child) 409{ 410 struct nexus_devinfo *ndi; 411 412 ndi = device_get_ivars(child); 413 return (&ndi->ndi_obdinfo); 414} 415 416static struct nexus_devinfo * 417nexus_setup_dinfo(device_t dev, phandle_t node) 418{ 419 struct ofw_nexus_softc *sc; 420 struct nexus_devinfo *ndi; 421 uint32_t *reg, *intr, icells; 422 uint64_t phys, size; 423 phandle_t iparent; 424 int i, j; 425 int nintr; 426 int nreg; 427 428 sc = device_get_softc(dev); 429 430 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 431 if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) { 432 free(ndi, M_DEVBUF); 433 return (NULL); 434 } 435 if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name, 436 ndi->ndi_obdinfo.obd_type)) { 437 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 438 free(ndi, M_DEVBUF); 439 return (NULL); 440 } 441 442 resource_list_init(&ndi->ndi_rl); 443 nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®); 444 if (nreg == -1) 445 nreg = 0; 446 if (nreg % (sc->acells + sc->scells) != 0) { 447 if (bootverbose) 448 device_printf(dev, "Malformed reg property on <%s>\n", 449 ndi->ndi_obdinfo.obd_name); 450 nreg = 0; 451 } 452 453 for (i = 0; i < nreg; i += sc->acells + sc->scells) { 454 phys = size = 0; 455 for (j = 0; j < sc->acells; j++) { 456 phys <<= 32; 457 phys |= reg[i + j]; 458 } 459 for (j = 0; j < sc->scells; j++) { 460 size <<= 32; 461 size |= reg[i + sc->acells + j]; 462 } 463 /* Skip the dummy reg property of glue devices like ssm(4). */ 464 if (size != 0) 465 resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, i, 466 phys, phys + size - 1, size); 467 } 468 free(reg, M_OFWPROP); 469 470 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr), 471 (void **)&intr); 472 if (nintr > 0) { 473 iparent = 0; 474 OF_searchprop(node, "interrupt-parent", &iparent, 475 sizeof(iparent)); 476 OF_searchprop(iparent, "#interrupt-cells", &icells, 477 sizeof(icells)); 478 for (i = 0; i < nintr; i+= icells) { 479 intr[i] = ofw_bus_map_intr(dev, iparent, intr[i]); 480 resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, i, intr[i], 481 intr[i], 1); 482 if (icells > 1) 483 ofw_bus_config_intr(dev, intr[i], intr[i+1]); 484 } 485 free(intr, M_OFWPROP); 486 } 487 488 return (ndi); 489} 490 491static void 492nexus_destroy_dinfo(struct nexus_devinfo *ndi) 493{ 494 495 resource_list_free(&ndi->ndi_rl); 496 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo); 497 free(ndi, M_DEVBUF); 498} 499 500static int 501nexus_print_res(struct nexus_devinfo *ndi) 502{ 503 int rv; 504 505 rv = 0; 506 rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY, 507 "%#lx"); 508 rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ, 509 "%ld"); 510 return (rv); 511} 512 513