ofwbus.c revision 209486
1/*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29/*- 30 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 54 * 55 * $FreeBSD: head/sys/powerpc/aim/nexus.c 209486 2010-06-23 22:33:03Z nwhitehorn $ 56 */ 57#include "opt_psim.h" 58 59#include <sys/param.h> 60#include <sys/systm.h> 61#include <sys/module.h> 62#include <sys/bus.h> 63#include <sys/cons.h> 64#include <sys/kernel.h> 65#include <sys/malloc.h> 66 67#include <dev/ofw/openfirm.h> 68 69#include <machine/bus.h> 70#include <machine/frame.h> 71#include <machine/intr_machdep.h> 72#include <machine/resource.h> 73 74#include <sys/rman.h> 75 76#include "ofw_bus_if.h" 77#include "pic_if.h" 78 79/* 80 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 81 * exist in Open Firmware and adds them as devices to this bus so that drivers 82 * can be attached to them. 83 * 84 * Maybe this code should get into dev/ofw to some extent, as some of it should 85 * work for all Open Firmware based machines... 86 */ 87 88static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information"); 89 90enum nexus_ivars { 91 NEXUS_IVAR_NODE, 92 NEXUS_IVAR_NAME, 93 NEXUS_IVAR_DEVICE_TYPE, 94 NEXUS_IVAR_COMPATIBLE, 95}; 96 97struct nexus_devinfo { 98 phandle_t ndi_node; 99 /* Some common properties. */ 100 const char *ndi_name; 101 const char *ndi_device_type; 102 const char *ndi_compatible; 103}; 104 105struct nexus_softc { 106 struct rman sc_rman; 107}; 108 109/* 110 * Device interface 111 */ 112static int nexus_probe(device_t); 113static int nexus_attach(device_t); 114 115/* 116 * Bus interface 117 */ 118static device_t nexus_add_child(device_t, int, const char *, int); 119static void nexus_probe_nomatch(device_t, device_t); 120static int nexus_read_ivar(device_t, device_t, int, uintptr_t *); 121static int nexus_write_ivar(device_t, device_t, int, uintptr_t); 122#ifdef SMP 123static int nexus_bind_intr(device_t dev, device_t child, 124 struct resource *irq, int cpu); 125#endif 126static int nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 127 enum intr_polarity pol); 128static int nexus_setup_intr(device_t, device_t, struct resource *, int, 129 driver_filter_t *, driver_intr_t *, void *, void **); 130static int nexus_teardown_intr(device_t, device_t, struct resource *, 131 void *); 132static struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 133 u_long, u_long, u_long, u_int); 134static int nexus_activate_resource(device_t, device_t, int, int, 135 struct resource *); 136static int nexus_deactivate_resource(device_t, device_t, int, int, 137 struct resource *); 138static int nexus_release_resource(device_t, device_t, int, int, 139 struct resource *); 140 141/* 142 * OFW bus interface. 143 */ 144static phandle_t nexus_ofw_get_node(device_t, device_t); 145static const char *nexus_ofw_get_name(device_t, device_t); 146static const char *nexus_ofw_get_type(device_t, device_t); 147static const char *nexus_ofw_get_compat(device_t, device_t); 148 149/* 150 * Local routines 151 */ 152static device_t nexus_device_from_node(device_t, phandle_t); 153 154static device_method_t nexus_methods[] = { 155 /* Device interface */ 156 DEVMETHOD(device_probe, nexus_probe), 157 DEVMETHOD(device_attach, nexus_attach), 158 DEVMETHOD(device_detach, bus_generic_detach), 159 DEVMETHOD(device_shutdown, bus_generic_shutdown), 160 DEVMETHOD(device_suspend, bus_generic_suspend), 161 DEVMETHOD(device_resume, bus_generic_resume), 162 163 /* Bus interface. Resource management is business of the children... */ 164 DEVMETHOD(bus_add_child, nexus_add_child), 165 DEVMETHOD(bus_print_child, bus_generic_print_child), 166 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 167 DEVMETHOD(bus_read_ivar, nexus_read_ivar), 168 DEVMETHOD(bus_write_ivar, nexus_write_ivar), 169 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 170 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 171#ifdef SMP 172 DEVMETHOD(bus_bind_intr, nexus_bind_intr), 173#endif 174 DEVMETHOD(bus_config_intr, nexus_config_intr), 175 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 176 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 177 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 178 DEVMETHOD(bus_release_resource, nexus_release_resource), 179 180 /* OFW bus interface */ 181 DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node), 182 DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name), 183 DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type), 184 DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat), 185 186 { 0, 0 } 187}; 188 189static driver_t nexus_driver = { 190 "nexus", 191 nexus_methods, 192 sizeof(struct nexus_softc), 193}; 194 195static devclass_t nexus_devclass; 196 197DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 198 199static int 200nexus_probe(device_t dev) 201{ 202 bus_generic_probe(dev); 203 device_set_desc(dev, "Open Firmware Nexus device"); 204 return (0); 205} 206 207static int 208nexus_attach(device_t dev) 209{ 210 phandle_t root; 211 phandle_t child; 212 struct nexus_softc *sc; 213 u_long start, end; 214 215 if ((root = OF_peer(0)) == -1) 216 panic("nexus_probe: OF_peer failed."); 217 218 sc = device_get_softc(dev); 219 220 start = 0; 221 end = MAX_PICS*INTR_VECTORS - 1; 222 223 sc->sc_rman.rm_start = start; 224 sc->sc_rman.rm_end = end; 225 sc->sc_rman.rm_type = RMAN_ARRAY; 226 sc->sc_rman.rm_descr = "Interrupt request lines"; 227 if (rman_init(&sc->sc_rman) || 228 rman_manage_region(&sc->sc_rman, start, end)) 229 panic("nexus_probe IRQ rman"); 230 231 /* 232 * Now walk the OFW tree to locate top-level devices 233 */ 234 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 235 if (child == -1) 236 panic("nexus_probe(): OF_child failed."); 237 (void)nexus_device_from_node(dev, child); 238 239 } 240 241 return (bus_generic_attach(dev)); 242} 243 244static void 245nexus_probe_nomatch(device_t dev, device_t child) 246{ 247 char *name, *type; 248 249 if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME, 250 (uintptr_t *)&name) != 0 || 251 BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE, 252 (uintptr_t *)&type) != 0) 253 return; 254 255 if (type == NULL) 256 type = "(unknown)"; 257 258 if (bootverbose) 259 device_printf(dev, "<%s>, type %s (no driver attached)\n", 260 name, type); 261} 262 263static device_t 264nexus_add_child(device_t dev, int order, const char *name, int unit) 265{ 266 device_t child; 267 struct nexus_devinfo *dinfo; 268 269 child = device_add_child_ordered(dev, order, name, unit); 270 if (child == NULL) 271 return (NULL); 272 273 dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO); 274 if (dinfo == NULL) 275 return (NULL); 276 277 dinfo->ndi_node = -1; 278 dinfo->ndi_name = name; 279 device_set_ivars(child, dinfo); 280 281 return (child); 282} 283 284 285static int 286nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 287{ 288 struct nexus_devinfo *dinfo; 289 290 if ((dinfo = device_get_ivars(child)) == 0) 291 return (ENOENT); 292 switch (which) { 293 case NEXUS_IVAR_NODE: 294 *result = dinfo->ndi_node; 295 break; 296 case NEXUS_IVAR_NAME: 297 *result = (uintptr_t)dinfo->ndi_name; 298 break; 299 case NEXUS_IVAR_DEVICE_TYPE: 300 *result = (uintptr_t)dinfo->ndi_device_type; 301 break; 302 case NEXUS_IVAR_COMPATIBLE: 303 *result = (uintptr_t)dinfo->ndi_compatible; 304 break; 305 default: 306 return (ENOENT); 307 } 308 return 0; 309} 310 311static int 312nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 313{ 314 struct nexus_devinfo *dinfo; 315 316 if ((dinfo = device_get_ivars(child)) == 0) 317 return (ENOENT); 318 319 switch (which) { 320 case NEXUS_IVAR_NAME: 321 return (EINVAL); 322 323 /* Identified devices may want to set these */ 324 case NEXUS_IVAR_NODE: 325 dinfo->ndi_node = (phandle_t)value; 326 break; 327 328 case NEXUS_IVAR_DEVICE_TYPE: 329 dinfo->ndi_device_type = (char *)value; 330 break; 331 332 default: 333 return (ENOENT); 334 } 335 return 0; 336} 337 338static int 339nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 340 driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep) 341{ 342 driver_t *driver; 343 int error; 344 345 /* somebody tried to setup an irq that failed to allocate! */ 346 if (res == NULL) 347 panic("nexus_setup_intr: NULL irq resource!"); 348 349 *cookiep = 0; 350 if ((rman_get_flags(res) & RF_SHAREABLE) == 0) 351 flags |= INTR_EXCL; 352 353 driver = device_get_driver(child); 354 355 /* 356 * We depend here on rman_activate_resource() being idempotent. 357 */ 358 error = rman_activate_resource(res); 359 if (error) 360 return (error); 361 362 error = powerpc_setup_intr(device_get_nameunit(child), 363 rman_get_start(res), filter, ihand, arg, flags, cookiep); 364 365 return (error); 366} 367 368static int 369nexus_teardown_intr(device_t dev, device_t child, struct resource *res, 370 void *cookie) 371{ 372 373 return (powerpc_teardown_intr(cookie)); 374} 375 376#ifdef SMP 377static int 378nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu) 379{ 380 381 return (powerpc_bind_intr(rman_get_start(irq), cpu)); 382} 383#endif 384 385static int 386nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 387 enum intr_polarity pol) 388{ 389 390 return (powerpc_config_intr(irq, trig, pol)); 391} 392 393/* 394 * Allocate resources at the behest of a child. This only handles interrupts, 395 * since I/O resources are handled by child busses. 396 */ 397static struct resource * 398nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 399 u_long start, u_long end, u_long count, u_int flags) 400{ 401 struct nexus_softc *sc; 402 struct resource *rv; 403 404 if (type != SYS_RES_IRQ) { 405 device_printf(bus, "unknown resource request from %s\n", 406 device_get_nameunit(child)); 407 return (NULL); 408 } 409 410 if (count == 0 || start + count - 1 != end) { 411 device_printf(bus, "invalid IRQ allocation from %s\n", 412 device_get_nameunit(child)); 413 return (NULL); 414 } 415 416 sc = device_get_softc(bus); 417 418 rv = rman_reserve_resource(&sc->sc_rman, start, end, count, 419 flags, child); 420 if (rv == NULL) { 421 device_printf(bus, "IRQ allocation failed for %s\n", 422 device_get_nameunit(child)); 423 } else 424 rman_set_rid(rv, *rid); 425 426 return (rv); 427} 428 429static int 430nexus_activate_resource(device_t bus, device_t child, int type, int rid, 431 struct resource *res) 432{ 433 434 /* Not much to be done yet... */ 435 return (rman_activate_resource(res)); 436} 437 438static int 439nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 440 struct resource *res) 441{ 442 443 /* Not much to be done yet... */ 444 return (rman_deactivate_resource(res)); 445} 446 447static int 448nexus_release_resource(device_t bus, device_t child, int type, int rid, 449 struct resource *res) 450{ 451 452 if (type != SYS_RES_IRQ) { 453 device_printf(bus, "unknown resource request from %s\n", 454 device_get_nameunit(child)); 455 return (EINVAL); 456 } 457 458 return (rman_release_resource(res)); 459} 460 461static device_t 462nexus_device_from_node(device_t parent, phandle_t node) 463{ 464 device_t cdev; 465 struct nexus_devinfo *dinfo; 466 char *name, *type, *compatible; 467 468 OF_getprop_alloc(node, "name", 1, (void **)&name); 469 OF_getprop_alloc(node, "device_type", 1, (void **)&type); 470 OF_getprop_alloc(node, "compatible", 1, (void **)&compatible); 471 cdev = device_add_child(parent, NULL, -1); 472 if (cdev != NULL) { 473 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK); 474 dinfo->ndi_node = node; 475 dinfo->ndi_name = name; 476 dinfo->ndi_device_type = type; 477 dinfo->ndi_compatible = compatible; 478 device_set_ivars(cdev, dinfo); 479 } else 480 free(name, M_OFWPROP); 481 482 return (cdev); 483} 484 485static const char * 486nexus_ofw_get_name(device_t bus, device_t dev) 487{ 488 struct nexus_devinfo *dinfo; 489 490 if ((dinfo = device_get_ivars(dev)) == NULL) 491 return (NULL); 492 493 return (dinfo->ndi_name); 494} 495 496static phandle_t 497nexus_ofw_get_node(device_t bus, device_t dev) 498{ 499 struct nexus_devinfo *dinfo; 500 501 if ((dinfo = device_get_ivars(dev)) == NULL) 502 return (0); 503 504 return (dinfo->ndi_node); 505} 506 507static const char * 508nexus_ofw_get_type(device_t bus, device_t dev) 509{ 510 struct nexus_devinfo *dinfo; 511 512 if ((dinfo = device_get_ivars(dev)) == NULL) 513 return (NULL); 514 515 return (dinfo->ndi_device_type); 516} 517 518static const char * 519nexus_ofw_get_compat(device_t bus, device_t dev) 520{ 521 struct nexus_devinfo *dinfo; 522 523 if ((dinfo = device_get_ivars(dev)) == NULL) 524 return (NULL); 525 526 return (dinfo->ndi_compatible); 527} 528 529