nexus.c revision 90622
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/sparc64/sparc64/nexus.c 90622 2002-02-13 16:28:40Z tmm $ 56 */ 57 58#include <sys/param.h> 59#include <sys/systm.h> 60#include <sys/bus.h> 61#include <sys/cons.h> 62#include <sys/kernel.h> 63#include <sys/malloc.h> 64 65#include <dev/ofw/openfirm.h> 66 67#include <machine/bus.h> 68#include <machine/frame.h> 69#include <machine/intr_machdep.h> 70#include <machine/nexusvar.h> 71#include <machine/ofw_upa.h> 72#include <machine/resource.h> 73#include <machine/upa.h> 74 75#include <sys/rman.h> 76 77/* 78 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 79 * hang from the OpenFirmware root node and add them as devices to this bus 80 * (except some special nodes which are excluded) so that drivers can be 81 * attached to them. This saves lots of detection work. 82 * Usually, all devices and bridges that are attached to the UltraSparc UPA 83 * bus will show up here, plus some pseudo-nodes which are excluded. 84 * For now, the only node that gets used is probably the pci bus one. 85 * 86 * Additionally, interrupt setup/teardown and some resource management are 87 * done at this level. 88 * 89 * Maybe this code should get into dev/ofw to some extent, as some of it should 90 * work for all OpenFirmware based machines... 91 */ 92 93static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information"); 94 95struct nexus_devinfo { 96 phandle_t ndi_node; 97 /* Some common properties. */ 98 char *ndi_name; 99 char *ndi_device_type; 100 char *ndi_model; 101 struct upa_regs *ndi_reg; 102 int ndi_nreg; 103 u_int *ndi_interrupts; 104 int ndi_ninterrupts; 105 bus_space_tag_t ndi_bustag; 106 bus_dma_tag_t ndi_dmatag; 107}; 108 109struct nexus_softc { 110 struct rman sc_intr_rman; 111 struct rman sc_mem_rman; 112}; 113 114static int nexus_probe(device_t); 115static void nexus_probe_nomatch(device_t, device_t); 116static int nexus_read_ivar(device_t, device_t, int, uintptr_t *); 117static int nexus_write_ivar(device_t, device_t, int, uintptr_t); 118static int nexus_setup_intr(device_t, device_t, struct resource *, int, 119 driver_intr_t *, void *, void **); 120static int nexus_teardown_intr(device_t, device_t, struct resource *, 121 void *); 122static struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 123 u_long, u_long, u_long, u_int); 124static int nexus_activate_resource(device_t, device_t, int, int, 125 struct resource *); 126static int nexus_deactivate_resource(device_t, device_t, int, int, 127 struct resource *); 128static int nexus_release_resource(device_t, device_t, int, int, 129 struct resource *); 130 131static device_method_t nexus_methods[] = { 132 /* Device interface */ 133 DEVMETHOD(device_probe, nexus_probe), 134 DEVMETHOD(device_attach, bus_generic_attach), 135 DEVMETHOD(device_detach, bus_generic_detach), 136 DEVMETHOD(device_shutdown, bus_generic_shutdown), 137 DEVMETHOD(device_suspend, bus_generic_suspend), 138 DEVMETHOD(device_resume, bus_generic_resume), 139 140 /* Bus interface. Resource management is business of the children... */ 141 DEVMETHOD(bus_print_child, bus_generic_print_child), 142 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 143 DEVMETHOD(bus_read_ivar, nexus_read_ivar), 144 DEVMETHOD(bus_write_ivar, nexus_write_ivar), 145 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 146 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 147 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 148 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 149 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 150 DEVMETHOD(bus_release_resource, nexus_release_resource), 151 152 { 0, 0 } 153}; 154 155static driver_t nexus_driver = { 156 "nexus", 157 nexus_methods, 158 sizeof(struct nexus_softc), 159}; 160 161static devclass_t nexus_devclass; 162 163DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 164 165static char *nexus_excl_name[] = { 166 "aliases", 167 "chosen", 168 "counter-timer", /* No separate device; handled by psycho/sbus */ 169 "memory", 170 "openprom", 171 "options", 172 "packages", 173 "virtual-memory", 174 NULL 175}; 176 177static char *nexus_excl_type[] = { 178 "cpu", 179 NULL 180}; 181 182extern struct bus_space_tag nexus_bustag; 183extern struct bus_dma_tag nexus_dmatag; 184 185static int 186nexus_inlist(char *name, char *list[]) 187{ 188 int i; 189 190 for (i = 0; list[i] != NULL; i++) 191 if (strcmp(name, list[i]) == 0) 192 return (1); 193 return (0); 194} 195 196#define NEXUS_EXCLUDED(name, type) \ 197 (nexus_inlist((name), nexus_excl_name) || \ 198 ((type) != NULL && nexus_inlist((type), nexus_excl_type))) 199 200static int 201nexus_probe(device_t dev) 202{ 203 phandle_t root; 204 phandle_t child; 205 device_t cdev; 206 struct nexus_devinfo *dinfo; 207 struct nexus_softc *sc; 208 char *name, *type; 209 210 if ((root = OF_peer(0)) == -1) 211 panic("nexus_probe: OF_peer failed."); 212 213 sc = device_get_softc(dev); 214 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 215 sc->sc_intr_rman.rm_descr = "Interrupts"; 216 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 217 sc->sc_mem_rman.rm_descr = "UPA Device Memory"; 218 if (rman_init(&sc->sc_intr_rman) != 0 || 219 rman_init(&sc->sc_mem_rman) != 0 || 220 rman_manage_region(&sc->sc_intr_rman, 0, NIV - 1) != 0 || 221 rman_manage_region(&sc->sc_mem_rman, UPA_MEMSTART, UPA_MEMEND) != 0) 222 panic("nexus_probe: failed to set up rmans"); 223 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 224 if (child == -1) 225 panic("nexus_probe(): OF_child failed."); 226 if (OF_getprop_alloc(child, "name", 1, (void **)&name) == -1) 227 continue; 228 OF_getprop_alloc(child, "device_type", 1, (void **)&type); 229 if (NEXUS_EXCLUDED(name, type)) { 230 free(name, M_OFWPROP); 231 if (type != NULL) 232 free(type, M_OFWPROP); 233 continue; 234 } 235 cdev = device_add_child(dev, NULL, -1); 236 if (cdev != NULL) { 237 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK); 238 dinfo->ndi_node = child; 239 dinfo->ndi_name = name; 240 dinfo->ndi_device_type = type; 241 OF_getprop_alloc(child, "model", 1, 242 (void **)&dinfo->ndi_model); 243 dinfo->ndi_nreg = OF_getprop_alloc(child, "reg", 244 sizeof(*dinfo->ndi_reg), (void **)&dinfo->ndi_reg); 245 dinfo->ndi_ninterrupts = OF_getprop_alloc(child, 246 "interrupts", sizeof(*dinfo->ndi_interrupts), 247 (void **)&dinfo->ndi_interrupts); 248 dinfo->ndi_bustag = &nexus_bustag; 249 dinfo->ndi_dmatag = &nexus_dmatag; 250 device_set_ivars(cdev, dinfo); 251 } else 252 free(name, M_OFWPROP); 253 254 } 255 device_set_desc(dev, "OpenFirmware Nexus device"); 256 return (0); 257} 258 259static void 260nexus_probe_nomatch(device_t dev, device_t child) 261{ 262 char *name; 263 char *type; 264 265 if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME, 266 (uintptr_t *)&name) != 0 || 267 BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE, 268 (uintptr_t *)&type) != 0) 269 return; 270 271 if (type == NULL) 272 type = "(unknown)"; 273 device_printf(dev, "<%s>, type %s (no driver attached)\n", 274 name, type); 275} 276 277static int 278nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 279{ 280 struct nexus_devinfo *dinfo; 281 282 if ((dinfo = device_get_ivars(child)) == 0) 283 return (ENOENT); 284 switch (which) { 285 case NEXUS_IVAR_NODE: 286 *result = dinfo->ndi_node; 287 break; 288 case NEXUS_IVAR_NAME: 289 *result = (uintptr_t)dinfo->ndi_name; 290 break; 291 case NEXUS_IVAR_DEVICE_TYPE: 292 *result = (uintptr_t)dinfo->ndi_device_type; 293 break; 294 case NEXUS_IVAR_MODEL: 295 *result = (uintptr_t)dinfo->ndi_model; 296 break; 297 case NEXUS_IVAR_REG: 298 *result = (uintptr_t)dinfo->ndi_reg; 299 break; 300 case NEXUS_IVAR_NREG: 301 *result = dinfo->ndi_nreg; 302 break; 303 case NEXUS_IVAR_INTERRUPTS: 304 *result = (uintptr_t)dinfo->ndi_interrupts; 305 break; 306 case NEXUS_IVAR_NINTERRUPTS: 307 *result = dinfo->ndi_ninterrupts; 308 break; 309 case NEXUS_IVAR_DMATAG: 310 *result = (uintptr_t)dinfo->ndi_dmatag; 311 break; 312 default: 313 return (ENOENT); 314 } 315 return 0; 316} 317 318static int 319nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 320{ 321 struct nexus_devinfo *dinfo; 322 323 if ((dinfo = device_get_ivars(child)) == 0) 324 return (ENOENT); 325 326 switch (which) { 327 case NEXUS_IVAR_NODE: 328 case NEXUS_IVAR_NAME: 329 case NEXUS_IVAR_DEVICE_TYPE: 330 case NEXUS_IVAR_MODEL: 331 case NEXUS_IVAR_REG: 332 case NEXUS_IVAR_NREG: 333 case NEXUS_IVAR_INTERRUPTS: 334 case NEXUS_IVAR_NINTERRUPTS: 335 case NEXUS_IVAR_DMATAG: 336 return (EINVAL); 337 default: 338 return (ENOENT); 339 } 340 return 0; 341} 342 343static int 344nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 345 driver_intr_t *intr, void *arg, void **cookiep) 346{ 347 int error; 348 349 if (res == NULL) 350 panic("nexus_setup_intr: NULL interrupt resource!"); 351 352 if ((res->r_flags & RF_SHAREABLE) == 0) 353 flags |= INTR_EXCL; 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 = inthand_add(device_get_nameunit(child), res->r_start, 363 intr, arg, flags, cookiep); 364 365 return (error); 366} 367 368static int 369nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 370{ 371 inthand_remove(r->r_start, ih); 372 return (0); 373} 374 375/* 376 * Allocate resources at the behalf of a child. This only handles interrupts, 377 * since i/o resources are usually set up by the firmware, and thus need not 378 * be handled here. 379 */ 380static struct resource * 381nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 382 u_long start, u_long end, u_long count, u_int flags) 383{ 384 struct nexus_softc *sc = device_get_softc(bus); 385 struct resource *rv; 386 struct rman *rm; 387 int needactivate = flags & RF_ACTIVE; 388 389 flags &= ~RF_ACTIVE; 390 391 switch (type) { 392 case SYS_RES_IRQ: 393 rm = &sc->sc_intr_rman; 394 break; 395 case SYS_RES_MEMORY: 396 rm = &sc->sc_mem_rman; 397 break; 398 default: 399 return (NULL); 400 } 401 402 rv = rman_reserve_resource(rm, start, end, count, flags, child); 403 if (rv == NULL) 404 return (NULL); 405 if (type == SYS_RES_MEMORY) { 406 rman_set_bustag(rv, &nexus_bustag); 407 rman_set_bushandle(rv, rman_get_start(rv)); 408 } 409 410 if (needactivate) { 411 if (bus_activate_resource(child, type, *rid, rv)) { 412 rman_release_resource(rv); 413 return (NULL); 414 } 415 } 416 417 return (rv); 418} 419 420static int 421nexus_activate_resource(device_t bus, device_t child, int type, int rid, 422 struct resource *r) 423{ 424 425 /* Not much to be done yet... */ 426 return (rman_activate_resource(r)); 427} 428 429static int 430nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 431 struct resource *r) 432{ 433 434 /* Not much to be done yet... */ 435 return (rman_deactivate_resource(r)); 436} 437 438static int 439nexus_release_resource(device_t bus, device_t child, int type, int rid, 440 struct resource *r) 441{ 442 int error; 443 444 if (rman_get_flags(r) & RF_ACTIVE) { 445 error = bus_deactivate_resource(child, type, rid, r); 446 if (error) 447 return error; 448 } 449 return (rman_release_resource(r)); 450} 451