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