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$"); 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 61266000Sian#include "opt_platform.h" 62266000Sian 63202046Simp#undef NEXUS_DEBUG 64187238Sgonzo#ifdef NEXUS_DEBUG 65187238Sgonzo#define dprintf printf 66187238Sgonzo#else 67187238Sgonzo#define dprintf(x, arg...) 68187238Sgonzo#endif /* NEXUS_DEBUG */ 69187238Sgonzo 70266000Sian#define NUM_MIPS_IRQS 6 71266000Sian 72178172Simpstatic MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 73178172Simp 74178172Simpstruct nexus_device { 75178172Simp struct resource_list nx_resources; 76178172Simp}; 77178172Simp 78178172Simp#define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 79178172Simp 80178172Simpstatic struct rman irq_rman; 81178172Simpstatic struct rman mem_rman; 82178172Simp 83178172Simpstatic struct resource * 84178172Simp nexus_alloc_resource(device_t, device_t, int, int *, u_long, 85178172Simp u_long, u_long, u_int); 86212413Savgstatic device_t nexus_add_child(device_t, u_int, const char *, int); 87178172Simpstatic int nexus_attach(device_t); 88178172Simpstatic void nexus_delete_resource(device_t, device_t, int, int); 89178172Simpstatic struct resource_list * 90178172Simp nexus_get_reslist(device_t, device_t); 91178172Simpstatic int nexus_get_resource(device_t, device_t, int, int, u_long *, 92178172Simp u_long *); 93178172Simpstatic int nexus_print_child(device_t, device_t); 94178172Simpstatic int nexus_print_all_resources(device_t dev); 95178172Simpstatic int nexus_probe(device_t); 96178172Simpstatic int nexus_release_resource(device_t, device_t, int, int, 97178172Simp struct resource *); 98178172Simpstatic int nexus_set_resource(device_t, device_t, int, int, u_long, 99178172Simp u_long); 100266000Sianstatic int nexus_activate_resource(device_t, device_t, int, int, 101266000Sian struct resource *); 102266000Sianstatic int nexus_deactivate_resource(device_t, device_t, int, int, 103266000Sian struct resource *); 104266000Sianstatic void nexus_hinted_child(device_t, const char *, int); 105178172Simpstatic int nexus_setup_intr(device_t dev, device_t child, 106178172Simp struct resource *res, int flags, driver_filter_t *filt, 107178172Simp driver_intr_t *intr, void *arg, void **cookiep); 108178172Simpstatic int nexus_teardown_intr(device_t, device_t, struct resource *, 109178172Simp void *); 110178172Simp 111178172Simpstatic device_method_t nexus_methods[] = { 112178172Simp /* Device interface */ 113178172Simp DEVMETHOD(device_probe, nexus_probe), 114178172Simp DEVMETHOD(device_attach, nexus_attach), 115178172Simp 116178172Simp /* Bus interface */ 117178172Simp DEVMETHOD(bus_add_child, nexus_add_child), 118178172Simp DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 119178172Simp DEVMETHOD(bus_delete_resource, nexus_delete_resource), 120178172Simp DEVMETHOD(bus_get_resource, nexus_get_resource), 121178172Simp DEVMETHOD(bus_get_resource_list, nexus_get_reslist), 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), 127266000Sian DEVMETHOD(bus_activate_resource,nexus_activate_resource), 128266000Sian DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 129266000Sian DEVMETHOD(bus_hinted_child, nexus_hinted_child), 130178172Simp 131178172Simp { 0, 0 } 132178172Simp}; 133178172Simp 134178172Simpstatic driver_t nexus_driver = { 135178172Simp "nexus", 136178172Simp nexus_methods, 137178172Simp 1 /* no softc */ 138178172Simp}; 139178172Simpstatic devclass_t nexus_devclass; 140178172Simp 141178172Simpstatic int 142178172Simpnexus_probe(device_t dev) 143178172Simp{ 144178172Simp 145178172Simp device_set_desc(dev, "MIPS32 root nexus"); 146178172Simp 147178172Simp irq_rman.rm_start = 0; 148178172Simp irq_rman.rm_end = NUM_MIPS_IRQS - 1; 149178172Simp irq_rman.rm_type = RMAN_ARRAY; 150178172Simp irq_rman.rm_descr = "Hardware IRQs"; 151178172Simp if (rman_init(&irq_rman) != 0 || 152178172Simp rman_manage_region(&irq_rman, 0, NUM_MIPS_IRQS - 1) != 0) { 153178172Simp panic("%s: irq_rman", __func__); 154178172Simp } 155178172Simp 156178172Simp mem_rman.rm_start = 0; 157221218Sjhb mem_rman.rm_end = ~0ul; 158178172Simp mem_rman.rm_type = RMAN_ARRAY; 159178172Simp mem_rman.rm_descr = "Memory addresses"; 160178172Simp if (rman_init(&mem_rman) != 0 || 161178172Simp rman_manage_region(&mem_rman, 0, ~0) != 0) { 162178172Simp panic("%s: mem_rman", __func__); 163178172Simp } 164178172Simp 165178172Simp return (0); 166178172Simp} 167178172Simp 168178172Simpstatic int 169178172Simpnexus_attach(device_t dev) 170178172Simp{ 171178172Simp 172178172Simp bus_generic_probe(dev); 173178172Simp bus_enumerate_hinted_children(dev); 174178172Simp bus_generic_attach(dev); 175178172Simp 176178172Simp return (0); 177178172Simp} 178178172Simp 179178172Simpstatic int 180178172Simpnexus_print_child(device_t bus, device_t child) 181178172Simp{ 182178172Simp int retval = 0; 183178172Simp 184178172Simp retval += bus_print_child_header(bus, child); 185178172Simp retval += nexus_print_all_resources(child); 186178172Simp if (device_get_flags(child)) 187178172Simp retval += printf(" flags %#x", device_get_flags(child)); 188178172Simp retval += printf(" on %s\n", device_get_nameunit(bus)); 189178172Simp 190178172Simp return (retval); 191178172Simp} 192178172Simp 193178172Simpstatic int 194178172Simpnexus_print_all_resources(device_t dev) 195178172Simp{ 196178172Simp struct nexus_device *ndev = DEVTONX(dev); 197178172Simp struct resource_list *rl = &ndev->nx_resources; 198178172Simp int retval = 0; 199178172Simp 200178172Simp if (STAILQ_FIRST(rl)) 201178172Simp retval += printf(" at"); 202178172Simp 203178172Simp retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 204178172Simp retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 205178172Simp 206178172Simp return (retval); 207178172Simp} 208178172Simp 209178172Simpstatic device_t 210212413Savgnexus_add_child(device_t bus, u_int order, const char *name, int unit) 211178172Simp{ 212178172Simp device_t child; 213178172Simp struct nexus_device *ndev; 214178172Simp 215178172Simp ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 216178172Simp if (!ndev) 217178172Simp return (0); 218178172Simp resource_list_init(&ndev->nx_resources); 219178172Simp 220178172Simp child = device_add_child_ordered(bus, order, name, unit); 221202046Simp if (child == NULL) { 222202046Simp device_printf(bus, "failed to add child: %s%d\n", name, unit); 223202046Simp return (0); 224202046Simp } 225178172Simp 226178172Simp /* should we free this in nexus_child_detached? */ 227178172Simp device_set_ivars(child, ndev); 228178172Simp 229178172Simp return (child); 230178172Simp} 231178172Simp 232178172Simp/* 233178172Simp * Allocate a resource on behalf of child. NB: child is usually going to be a 234178172Simp * child of one of our descendants, not a direct child of nexus0. 235178172Simp * (Exceptions include footbridge.) 236178172Simp */ 237178172Simpstatic struct resource * 238178172Simpnexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 239178172Simp u_long start, u_long end, u_long count, u_int flags) 240178172Simp{ 241178172Simp struct nexus_device *ndev = DEVTONX(child); 242178172Simp struct resource *rv; 243178172Simp struct resource_list_entry *rle; 244178172Simp struct rman *rm; 245178172Simp int isdefault, needactivate, passthrough; 246178172Simp 247187238Sgonzo dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %ld, %d)\n", 248178172Simp __func__, bus, child, type, rid, (void *)(intptr_t)start, 249178172Simp (void *)(intptr_t)end, count, flags); 250187238Sgonzo dprintf("%s: requested rid is %d\n", __func__, *rid); 251178172Simp 252178172Simp isdefault = (start == 0UL && end == ~0UL && count == 1); 253178172Simp needactivate = flags & RF_ACTIVE; 254178172Simp passthrough = (device_get_parent(child) != bus); 255178172Simp rle = NULL; 256178172Simp 257178172Simp /* 258178172Simp * If this is an allocation of the "default" range for a given RID, 259178172Simp * and we know what the resources for this device are (ie. they aren't 260178172Simp * maintained by a child bus), then work out the start/end values. 261178172Simp */ 262178172Simp if (isdefault) { 263178172Simp rle = resource_list_find(&ndev->nx_resources, type, *rid); 264178172Simp if (rle == NULL) 265178172Simp return (NULL); 266178172Simp if (rle->res != NULL) { 267178172Simp panic("%s: resource entry is busy", __func__); 268178172Simp } 269178172Simp start = rle->start; 270178172Simp end = rle->end; 271178172Simp count = rle->count; 272178172Simp } 273178172Simp 274178172Simp switch (type) { 275178172Simp case SYS_RES_IRQ: 276178172Simp rm = &irq_rman; 277178172Simp break; 278178172Simp case SYS_RES_MEMORY: 279178172Simp rm = &mem_rman; 280178172Simp break; 281178172Simp default: 282178172Simp printf("%s: unknown resource type %d\n", __func__, type); 283178172Simp return (0); 284178172Simp } 285178172Simp 286178172Simp rv = rman_reserve_resource(rm, start, end, count, flags, child); 287178172Simp if (rv == 0) { 288202046Simp printf("%s: could not reserve resource for %s\n", __func__, 289202046Simp device_get_nameunit(child)); 290178172Simp return (0); 291178172Simp } 292178172Simp 293178172Simp rman_set_rid(rv, *rid); 294178172Simp 295178172Simp if (needactivate) { 296178172Simp if (bus_activate_resource(child, type, *rid, rv)) { 297178172Simp printf("%s: could not activate resource\n", __func__); 298178172Simp rman_release_resource(rv); 299178172Simp return (0); 300178172Simp } 301178172Simp } 302178172Simp 303178172Simp return (rv); 304178172Simp} 305178172Simp 306178172Simpstatic struct resource_list * 307178172Simpnexus_get_reslist(device_t dev, device_t child) 308178172Simp{ 309178172Simp struct nexus_device *ndev = DEVTONX(child); 310178172Simp 311178172Simp return (&ndev->nx_resources); 312178172Simp} 313178172Simp 314178172Simpstatic int 315178172Simpnexus_set_resource(device_t dev, device_t child, int type, int rid, 316178172Simp u_long start, u_long count) 317178172Simp{ 318178172Simp struct nexus_device *ndev = DEVTONX(child); 319178172Simp struct resource_list *rl = &ndev->nx_resources; 320178172Simp struct resource_list_entry *rle; 321178172Simp 322187238Sgonzo dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 323178172Simp __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 324178172Simp 325178172Simp rle = resource_list_add(rl, type, rid, start, start + count - 1, 326178172Simp count); 327178172Simp if (rle == NULL) 328178172Simp return (ENXIO); 329178172Simp 330178172Simp return (0); 331178172Simp} 332178172Simp 333178172Simpstatic int 334178172Simpnexus_get_resource(device_t dev, device_t child, int type, int rid, 335178172Simp u_long *startp, u_long *countp) 336178172Simp{ 337178172Simp struct nexus_device *ndev = DEVTONX(child); 338178172Simp struct resource_list *rl = &ndev->nx_resources; 339178172Simp struct resource_list_entry *rle; 340178172Simp 341178172Simp rle = resource_list_find(rl, type, rid); 342178172Simp if (!rle) 343178172Simp return(ENOENT); 344178172Simp if (startp) 345178172Simp *startp = rle->start; 346178172Simp if (countp) 347178172Simp *countp = rle->count; 348178172Simp return (0); 349178172Simp} 350178172Simp 351178172Simpstatic void 352178172Simpnexus_delete_resource(device_t dev, device_t child, int type, int rid) 353178172Simp{ 354178172Simp struct nexus_device *ndev = DEVTONX(child); 355178172Simp struct resource_list *rl = &ndev->nx_resources; 356178172Simp 357187238Sgonzo dprintf("%s: entry\n", __func__); 358178172Simp 359178172Simp resource_list_delete(rl, type, rid); 360178172Simp} 361178172Simp 362178172Simpstatic int 363178172Simpnexus_release_resource(device_t bus, device_t child, int type, int rid, 364178172Simp struct resource *r) 365178172Simp{ 366187238Sgonzo dprintf("%s: entry\n", __func__); 367178172Simp 368178172Simp if (rman_get_flags(r) & RF_ACTIVE) { 369178172Simp int error = bus_deactivate_resource(child, type, rid, r); 370178172Simp if (error) 371178172Simp return error; 372178172Simp } 373178172Simp 374178172Simp return (rman_release_resource(r)); 375178172Simp} 376178172Simp 377178172Simpstatic int 378266000Siannexus_activate_resource(device_t bus, device_t child, int type, int rid, 379266000Sian struct resource *r) 380266000Sian{ 381266000Sian void *vaddr; 382266000Sian vm_paddr_t paddr; 383266000Sian vm_size_t psize; 384266000Sian 385266000Sian /* 386266000Sian * If this is a memory resource, use pmap_mapdev to map it. 387266000Sian */ 388266000Sian if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 389266000Sian paddr = rman_get_start(r); 390266000Sian psize = rman_get_size(r); 391266000Sian vaddr = pmap_mapdev(paddr, psize); 392266000Sian 393266000Sian rman_set_virtual(r, vaddr); 394266000Sian rman_set_bustag(r, mips_bus_space_generic); 395266000Sian rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr); 396266000Sian } 397266000Sian 398266000Sian return (rman_activate_resource(r)); 399266000Sian} 400266000Sian 401266000Sianstatic int 402178172Simpnexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 403178172Simp struct resource *r) 404178172Simp{ 405202046Simp vm_offset_t va; 406202046Simp 407203796Sneel if (type == SYS_RES_MEMORY) { 408202046Simp va = (vm_offset_t)rman_get_virtual(r); 409202046Simp pmap_unmapdev(va, rman_get_size(r)); 410202046Simp } 411178172Simp 412178172Simp return (rman_deactivate_resource(r)); 413178172Simp} 414178172Simp 415266000Sianstatic int 416266000Siannexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 417266000Sian driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) 418266000Sian{ 419266000Sian register_t s; 420266000Sian int irq; 421266000Sian 422266000Sian s = intr_disable(); 423266000Sian irq = rman_get_start(res); 424266000Sian if (irq >= NUM_MIPS_IRQS) { 425266000Sian intr_restore(s); 426266000Sian return (0); 427266000Sian } 428266000Sian 429266000Sian cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg, 430266000Sian irq, flags, cookiep); 431266000Sian intr_restore(s); 432266000Sian return (0); 433266000Sian} 434266000Sian 435266000Sianstatic int 436266000Siannexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 437266000Sian{ 438266000Sian 439266000Sian printf("Unimplemented %s at %s:%d\n", __func__, __FILE__, __LINE__); 440266000Sian return (0); 441266000Sian} 442266000Sian 443266000Sianstatic void 444266000Siannexus_hinted_child(device_t bus, const char *dname, int dunit) 445266000Sian{ 446266000Sian device_t child; 447266000Sian long maddr; 448266000Sian int msize; 449266000Sian int order; 450266000Sian int result; 451266000Sian int irq; 452266000Sian int mem_hints_count; 453266000Sian 454266000Sian if ((resource_int_value(dname, dunit, "order", &order)) != 0) 455266000Sian order = 1000; 456266000Sian child = BUS_ADD_CHILD(bus, order, dname, dunit); 457266000Sian if (child == NULL) 458266000Sian return; 459266000Sian 460266000Sian /* 461266000Sian * Set hard-wired resources for hinted child using 462266000Sian * specific RIDs. 463266000Sian */ 464266000Sian mem_hints_count = 0; 465266000Sian if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 466266000Sian mem_hints_count++; 467266000Sian if (resource_int_value(dname, dunit, "msize", &msize) == 0) 468266000Sian mem_hints_count++; 469266000Sian 470266000Sian /* check if all info for mem resource has been provided */ 471266000Sian if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 472266000Sian printf("Either maddr or msize hint is missing for %s%d\n", 473266000Sian dname, dunit); 474266000Sian } 475266000Sian else if (mem_hints_count) { 476266000Sian dprintf("%s: discovered hinted child %s at maddr %p(%d)\n", 477266000Sian __func__, device_get_nameunit(child), 478266000Sian (void *)(intptr_t)maddr, msize); 479266000Sian 480266000Sian result = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, 481266000Sian msize); 482266000Sian if (result != 0) { 483266000Sian device_printf(bus, 484266000Sian "warning: bus_set_resource() failed\n"); 485266000Sian } 486266000Sian } 487266000Sian 488266000Sian if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 489266000Sian result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 490266000Sian if (result != 0) 491266000Sian device_printf(bus, 492266000Sian "warning: bus_set_resource() failed\n"); 493266000Sian } 494266000Sian} 495266000Sian 496178172SimpDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 497