nexus.c revision 221218
1178172Simp/*- 2178172Simp * Copyright 1998 Massachusetts Institute of Technology 3178172Simp * 4178172Simp * Permission to use, copy, modify, and distribute this software and 5178172Simp * its documentation for any purpose and without fee is hereby 6178172Simp * granted, provided that both the above copyright notice and this 7178172Simp * permission notice appear in all copies, that both the above 8178172Simp * copyright notice and this permission notice appear in all 9178172Simp * supporting documentation, and that the name of M.I.T. not be used 10178172Simp * in advertising or publicity pertaining to distribution of the 11178172Simp * software without specific, written prior permission. M.I.T. makes 12178172Simp * no representations about the suitability of this software for any 13178172Simp * purpose. It is provided "as is" without express or implied 14178172Simp * warranty. 15178172Simp * 16178172Simp * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17178172Simp * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18178172Simp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19178172Simp * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20178172Simp * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21178172Simp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22178172Simp * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23178172Simp * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24178172Simp * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25178172Simp * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26178172Simp * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27178172Simp * SUCH DAMAGE. 28178172Simp * 29178172Simp */ 30178172Simp 31178172Simp/* 32178172Simp * This code implements a `root nexus' for MIPS Architecture 33178172Simp * machines. The function of the root nexus is to serve as an 34178172Simp * attachment point for both processors and buses, and to manage 35178172Simp * resources which are common to all of them. In particular, 36178172Simp * this code implements the core resource managers for interrupt 37178172Simp * requests and memory address space. 38178172Simp */ 39178172Simp 40178172Simp#include <sys/cdefs.h> 41178172Simp__FBSDID("$FreeBSD: head/sys/mips/mips/nexus.c 221218 2011-04-29 18:41:21Z jhb $"); 42178172Simp 43178172Simp#include <sys/param.h> 44178172Simp#include <sys/systm.h> 45178172Simp#include <sys/bus.h> 46178172Simp#include <sys/kernel.h> 47178172Simp#include <sys/malloc.h> 48178172Simp#include <sys/module.h> 49178172Simp#include <sys/rman.h> 50178172Simp#include <sys/interrupt.h> 51178172Simp 52178172Simp#include <vm/vm.h> 53178172Simp#include <vm/pmap.h> 54178172Simp 55178172Simp#include <machine/bus.h> 56178172Simp#include <machine/intr_machdep.h> 57178172Simp#include <machine/pmap.h> 58178172Simp#include <machine/resource.h> 59178172Simp#include <machine/vmparam.h> 60178172Simp 61202046Simp#undef NEXUS_DEBUG 62187238Sgonzo#ifdef NEXUS_DEBUG 63187238Sgonzo#define dprintf printf 64187238Sgonzo#else 65187238Sgonzo#define dprintf(x, arg...) 66187238Sgonzo#endif /* NEXUS_DEBUG */ 67187238Sgonzo 68178172Simpstatic MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 69178172Simp 70178172Simpstruct nexus_device { 71178172Simp struct resource_list nx_resources; 72178172Simp}; 73178172Simp 74178172Simp#define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 75178172Simp#define NUM_MIPS_IRQS 6 76178172Simp 77178172Simpstatic struct rman irq_rman; 78178172Simpstatic struct rman mem_rman; 79178172Simp 80178172Simpstatic struct resource * 81178172Simp nexus_alloc_resource(device_t, device_t, int, int *, u_long, 82178172Simp u_long, u_long, u_int); 83178172Simpstatic int nexus_activate_resource(device_t, device_t, int, int, 84178172Simp struct resource *); 85212413Savgstatic device_t nexus_add_child(device_t, u_int, const char *, int); 86178172Simpstatic int nexus_attach(device_t); 87178172Simpstatic int nexus_deactivate_resource(device_t, device_t, int, int, 88178172Simp struct resource *); 89178172Simpstatic void nexus_delete_resource(device_t, device_t, int, int); 90178172Simpstatic struct resource_list * 91178172Simp nexus_get_reslist(device_t, device_t); 92178172Simpstatic int nexus_get_resource(device_t, device_t, int, int, u_long *, 93178172Simp u_long *); 94178172Simpstatic void nexus_hinted_child(device_t, const char *, int); 95178172Simpstatic int nexus_print_child(device_t, device_t); 96178172Simpstatic int nexus_print_all_resources(device_t dev); 97178172Simpstatic int nexus_probe(device_t); 98178172Simpstatic int nexus_release_resource(device_t, device_t, int, int, 99178172Simp struct resource *); 100178172Simpstatic int nexus_set_resource(device_t, device_t, int, int, u_long, 101178172Simp u_long); 102178172Simpstatic int nexus_setup_intr(device_t dev, device_t child, 103178172Simp struct resource *res, int flags, driver_filter_t *filt, 104178172Simp driver_intr_t *intr, void *arg, void **cookiep); 105178172Simpstatic int nexus_teardown_intr(device_t, device_t, struct resource *, 106178172Simp void *); 107178172Simp 108178172Simpstatic device_method_t nexus_methods[] = { 109178172Simp /* Device interface */ 110178172Simp DEVMETHOD(device_probe, nexus_probe), 111178172Simp DEVMETHOD(device_attach, nexus_attach), 112178172Simp 113178172Simp /* Bus interface */ 114178172Simp DEVMETHOD(bus_add_child, nexus_add_child), 115178172Simp DEVMETHOD(bus_activate_resource,nexus_activate_resource), 116178172Simp DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 117178172Simp DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 118178172Simp DEVMETHOD(bus_delete_resource, nexus_delete_resource), 119178172Simp DEVMETHOD(bus_get_resource, nexus_get_resource), 120178172Simp DEVMETHOD(bus_get_resource_list, nexus_get_reslist), 121178172Simp DEVMETHOD(bus_hinted_child, nexus_hinted_child), 122178172Simp DEVMETHOD(bus_print_child, nexus_print_child), 123178172Simp DEVMETHOD(bus_release_resource, nexus_release_resource), 124178172Simp DEVMETHOD(bus_set_resource, nexus_set_resource), 125178172Simp DEVMETHOD(bus_setup_intr, nexus_setup_intr), 126178172Simp DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 127178172Simp 128178172Simp { 0, 0 } 129178172Simp}; 130178172Simp 131178172Simpstatic driver_t nexus_driver = { 132178172Simp "nexus", 133178172Simp nexus_methods, 134178172Simp 1 /* no softc */ 135178172Simp}; 136178172Simpstatic devclass_t nexus_devclass; 137178172Simp 138178172Simpstatic int 139178172Simpnexus_probe(device_t dev) 140178172Simp{ 141178172Simp 142178172Simp device_set_desc(dev, "MIPS32 root nexus"); 143178172Simp 144178172Simp irq_rman.rm_start = 0; 145178172Simp irq_rman.rm_end = NUM_MIPS_IRQS - 1; 146178172Simp irq_rman.rm_type = RMAN_ARRAY; 147178172Simp irq_rman.rm_descr = "Hardware IRQs"; 148178172Simp if (rman_init(&irq_rman) != 0 || 149178172Simp rman_manage_region(&irq_rman, 0, NUM_MIPS_IRQS - 1) != 0) { 150178172Simp panic("%s: irq_rman", __func__); 151178172Simp } 152178172Simp 153178172Simp mem_rman.rm_start = 0; 154221218Sjhb mem_rman.rm_end = ~0ul; 155178172Simp mem_rman.rm_type = RMAN_ARRAY; 156178172Simp mem_rman.rm_descr = "Memory addresses"; 157178172Simp if (rman_init(&mem_rman) != 0 || 158178172Simp rman_manage_region(&mem_rman, 0, ~0) != 0) { 159178172Simp panic("%s: mem_rman", __func__); 160178172Simp } 161178172Simp 162178172Simp return (0); 163178172Simp} 164178172Simp 165178172Simpstatic int 166178172Simpnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 167178172Simp driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) 168178172Simp{ 169206717Sjmallett register_t s; 170178172Simp int irq; 171178172Simp 172206717Sjmallett s = intr_disable(); 173178172Simp irq = rman_get_start(res); 174206717Sjmallett if (irq >= NUM_MIPS_IRQS) { 175206717Sjmallett intr_restore(s); 176178172Simp return (0); 177206717Sjmallett } 178178172Simp 179178172Simp cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg, 180178172Simp irq, flags, cookiep); 181206717Sjmallett intr_restore(s); 182178172Simp return (0); 183178172Simp} 184178172Simp 185178172Simpstatic int 186178172Simpnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 187178172Simp{ 188178172Simp 189178172Simp printf("Unimplemented %s at %s:%d\n", __func__, __FILE__, __LINE__); 190178172Simp return (0); 191178172Simp} 192178172Simp 193178172Simpstatic int 194178172Simpnexus_attach(device_t dev) 195178172Simp{ 196178172Simp 197178172Simp bus_generic_probe(dev); 198178172Simp bus_enumerate_hinted_children(dev); 199178172Simp bus_generic_attach(dev); 200178172Simp 201178172Simp return (0); 202178172Simp} 203178172Simp 204178172Simpstatic int 205178172Simpnexus_print_child(device_t bus, device_t child) 206178172Simp{ 207178172Simp int retval = 0; 208178172Simp 209178172Simp retval += bus_print_child_header(bus, child); 210178172Simp retval += nexus_print_all_resources(child); 211178172Simp if (device_get_flags(child)) 212178172Simp retval += printf(" flags %#x", device_get_flags(child)); 213178172Simp retval += printf(" on %s\n", device_get_nameunit(bus)); 214178172Simp 215178172Simp return (retval); 216178172Simp} 217178172Simp 218178172Simpstatic int 219178172Simpnexus_print_all_resources(device_t dev) 220178172Simp{ 221178172Simp struct nexus_device *ndev = DEVTONX(dev); 222178172Simp struct resource_list *rl = &ndev->nx_resources; 223178172Simp int retval = 0; 224178172Simp 225178172Simp if (STAILQ_FIRST(rl)) 226178172Simp retval += printf(" at"); 227178172Simp 228178172Simp retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 229178172Simp retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 230178172Simp 231178172Simp return (retval); 232178172Simp} 233178172Simp 234178172Simpstatic void 235178172Simpnexus_hinted_child(device_t bus, const char *dname, int dunit) 236178172Simp{ 237178172Simp device_t child; 238178172Simp long maddr; 239178172Simp int msize; 240178172Simp int result; 241202046Simp int irq; 242202046Simp int mem_hints_count; 243178172Simp 244178172Simp child = BUS_ADD_CHILD(bus, 0, dname, dunit); 245202046Simp if (child == NULL) 246202046Simp return; 247178172Simp 248178172Simp /* 249178172Simp * Set hard-wired resources for hinted child using 250178172Simp * specific RIDs. 251178172Simp */ 252202046Simp mem_hints_count = 0; 253202046Simp if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 254202046Simp mem_hints_count++; 255202046Simp if (resource_int_value(dname, dunit, "msize", &msize) == 0) 256202046Simp mem_hints_count++; 257178172Simp 258202046Simp /* check if all info for mem resource has been provided */ 259202046Simp if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 260202046Simp printf("Either maddr or msize hint is missing for %s%d\n", 261202046Simp dname, dunit); 262202046Simp } 263202046Simp else if (mem_hints_count) { 264202046Simp dprintf("%s: discovered hinted child %s at maddr %p(%d)\n", 265202046Simp __func__, device_get_nameunit(child), 266202046Simp (void *)(intptr_t)maddr, msize); 267178172Simp 268202046Simp result = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, 269202046Simp msize); 270202046Simp if (result != 0) { 271202046Simp device_printf(bus, 272202046Simp "warning: bus_set_resource() failed\n"); 273202046Simp } 274178172Simp } 275202046Simp 276202046Simp if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 277202046Simp result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 278202046Simp if (result != 0) 279202046Simp device_printf(bus, 280202046Simp "warning: bus_set_resource() failed\n"); 281202046Simp } 282178172Simp} 283178172Simp 284178172Simpstatic device_t 285212413Savgnexus_add_child(device_t bus, u_int order, const char *name, int unit) 286178172Simp{ 287178172Simp device_t child; 288178172Simp struct nexus_device *ndev; 289178172Simp 290178172Simp ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 291178172Simp if (!ndev) 292178172Simp return (0); 293178172Simp resource_list_init(&ndev->nx_resources); 294178172Simp 295178172Simp child = device_add_child_ordered(bus, order, name, unit); 296202046Simp if (child == NULL) { 297202046Simp device_printf(bus, "failed to add child: %s%d\n", name, unit); 298202046Simp return (0); 299202046Simp } 300178172Simp 301178172Simp /* should we free this in nexus_child_detached? */ 302178172Simp device_set_ivars(child, ndev); 303178172Simp 304178172Simp return (child); 305178172Simp} 306178172Simp 307178172Simp/* 308178172Simp * Allocate a resource on behalf of child. NB: child is usually going to be a 309178172Simp * child of one of our descendants, not a direct child of nexus0. 310178172Simp * (Exceptions include footbridge.) 311178172Simp */ 312178172Simpstatic struct resource * 313178172Simpnexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 314178172Simp u_long start, u_long end, u_long count, u_int flags) 315178172Simp{ 316178172Simp struct nexus_device *ndev = DEVTONX(child); 317178172Simp struct resource *rv; 318178172Simp struct resource_list_entry *rle; 319178172Simp struct rman *rm; 320178172Simp int isdefault, needactivate, passthrough; 321178172Simp 322187238Sgonzo dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %ld, %d)\n", 323178172Simp __func__, bus, child, type, rid, (void *)(intptr_t)start, 324178172Simp (void *)(intptr_t)end, count, flags); 325187238Sgonzo dprintf("%s: requested rid is %d\n", __func__, *rid); 326178172Simp 327178172Simp isdefault = (start == 0UL && end == ~0UL && count == 1); 328178172Simp needactivate = flags & RF_ACTIVE; 329178172Simp passthrough = (device_get_parent(child) != bus); 330178172Simp rle = NULL; 331178172Simp 332178172Simp /* 333178172Simp * If this is an allocation of the "default" range for a given RID, 334178172Simp * and we know what the resources for this device are (ie. they aren't 335178172Simp * maintained by a child bus), then work out the start/end values. 336178172Simp */ 337178172Simp if (isdefault) { 338178172Simp rle = resource_list_find(&ndev->nx_resources, type, *rid); 339178172Simp if (rle == NULL) 340178172Simp return (NULL); 341178172Simp if (rle->res != NULL) { 342178172Simp panic("%s: resource entry is busy", __func__); 343178172Simp } 344178172Simp start = rle->start; 345178172Simp end = rle->end; 346178172Simp count = rle->count; 347178172Simp } 348178172Simp 349178172Simp switch (type) { 350178172Simp case SYS_RES_IRQ: 351178172Simp rm = &irq_rman; 352178172Simp break; 353178172Simp case SYS_RES_MEMORY: 354178172Simp rm = &mem_rman; 355178172Simp break; 356178172Simp default: 357178172Simp printf("%s: unknown resource type %d\n", __func__, type); 358178172Simp return (0); 359178172Simp } 360178172Simp 361178172Simp rv = rman_reserve_resource(rm, start, end, count, flags, child); 362178172Simp if (rv == 0) { 363202046Simp printf("%s: could not reserve resource for %s\n", __func__, 364202046Simp device_get_nameunit(child)); 365178172Simp return (0); 366178172Simp } 367178172Simp 368178172Simp rman_set_rid(rv, *rid); 369178172Simp 370178172Simp if (needactivate) { 371178172Simp if (bus_activate_resource(child, type, *rid, rv)) { 372178172Simp printf("%s: could not activate resource\n", __func__); 373178172Simp rman_release_resource(rv); 374178172Simp return (0); 375178172Simp } 376178172Simp } 377178172Simp 378178172Simp return (rv); 379178172Simp} 380178172Simp 381178172Simpstatic int 382178172Simpnexus_activate_resource(device_t bus, device_t child, int type, int rid, 383178172Simp struct resource *r) 384178172Simp{ 385203796Sneel void *vaddr; 386203796Sneel u_int32_t paddr, psize; 387203796Sneel 388178172Simp /* 389178172Simp * If this is a memory resource, track the direct mapping 390178172Simp * in the uncached MIPS KSEG1 segment. 391178172Simp */ 392203796Sneel if (type == SYS_RES_MEMORY) { 393202046Simp paddr = rman_get_start(r); 394202046Simp psize = rman_get_size(r); 395203796Sneel vaddr = pmap_mapdev(paddr, psize); 396178172Simp 397178172Simp rman_set_virtual(r, vaddr); 398202046Simp rman_set_bustag(r, mips_bus_space_generic); 399202046Simp rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr); 400178172Simp } 401178172Simp 402178172Simp return (rman_activate_resource(r)); 403178172Simp} 404178172Simp 405178172Simpstatic struct resource_list * 406178172Simpnexus_get_reslist(device_t dev, device_t child) 407178172Simp{ 408178172Simp struct nexus_device *ndev = DEVTONX(child); 409178172Simp 410178172Simp return (&ndev->nx_resources); 411178172Simp} 412178172Simp 413178172Simpstatic int 414178172Simpnexus_set_resource(device_t dev, device_t child, int type, int rid, 415178172Simp u_long start, u_long count) 416178172Simp{ 417178172Simp struct nexus_device *ndev = DEVTONX(child); 418178172Simp struct resource_list *rl = &ndev->nx_resources; 419178172Simp struct resource_list_entry *rle; 420178172Simp 421187238Sgonzo dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 422178172Simp __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 423178172Simp 424178172Simp rle = resource_list_add(rl, type, rid, start, start + count - 1, 425178172Simp count); 426178172Simp if (rle == NULL) 427178172Simp return (ENXIO); 428178172Simp 429178172Simp return (0); 430178172Simp} 431178172Simp 432178172Simpstatic int 433178172Simpnexus_get_resource(device_t dev, device_t child, int type, int rid, 434178172Simp u_long *startp, u_long *countp) 435178172Simp{ 436178172Simp struct nexus_device *ndev = DEVTONX(child); 437178172Simp struct resource_list *rl = &ndev->nx_resources; 438178172Simp struct resource_list_entry *rle; 439178172Simp 440178172Simp rle = resource_list_find(rl, type, rid); 441178172Simp if (!rle) 442178172Simp return(ENOENT); 443178172Simp if (startp) 444178172Simp *startp = rle->start; 445178172Simp if (countp) 446178172Simp *countp = rle->count; 447178172Simp return (0); 448178172Simp} 449178172Simp 450178172Simpstatic void 451178172Simpnexus_delete_resource(device_t dev, device_t child, int type, int rid) 452178172Simp{ 453178172Simp struct nexus_device *ndev = DEVTONX(child); 454178172Simp struct resource_list *rl = &ndev->nx_resources; 455178172Simp 456187238Sgonzo dprintf("%s: entry\n", __func__); 457178172Simp 458178172Simp resource_list_delete(rl, type, rid); 459178172Simp} 460178172Simp 461178172Simpstatic int 462178172Simpnexus_release_resource(device_t bus, device_t child, int type, int rid, 463178172Simp struct resource *r) 464178172Simp{ 465187238Sgonzo dprintf("%s: entry\n", __func__); 466178172Simp 467178172Simp if (rman_get_flags(r) & RF_ACTIVE) { 468178172Simp int error = bus_deactivate_resource(child, type, rid, r); 469178172Simp if (error) 470178172Simp return error; 471178172Simp } 472178172Simp 473178172Simp return (rman_release_resource(r)); 474178172Simp} 475178172Simp 476178172Simpstatic int 477178172Simpnexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 478178172Simp struct resource *r) 479178172Simp{ 480202046Simp vm_offset_t va; 481202046Simp 482203796Sneel if (type == SYS_RES_MEMORY) { 483202046Simp va = (vm_offset_t)rman_get_virtual(r); 484202046Simp pmap_unmapdev(va, rman_get_size(r)); 485202046Simp } 486178172Simp 487178172Simp return (rman_deactivate_resource(r)); 488178172Simp} 489178172Simp 490178172SimpDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 491