The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/mips/mips/nexus.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 
   31 /*
   32  * This code implements a `root nexus' for MIPS Architecture
   33  * machines.  The function of the root nexus is to serve as an
   34  * attachment point for both processors and buses, and to manage
   35  * resources which are common to all of them.  In particular,
   36  * this code implements the core resource managers for interrupt
   37  * requests and memory address space.
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD: releng/10.0/sys/mips/mips/nexus.c 234525 2012-04-21 04:17:30Z adrian $");
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/bus.h>
   46 #include <sys/kernel.h>
   47 #include <sys/malloc.h>
   48 #include <sys/module.h>
   49 #include <sys/rman.h>
   50 #include <sys/interrupt.h>
   51 
   52 #include <vm/vm.h>
   53 #include <vm/pmap.h>
   54 
   55 #include <machine/bus.h>
   56 #include <machine/intr_machdep.h>
   57 #include <machine/pmap.h>
   58 #include <machine/resource.h>
   59 #include <machine/vmparam.h>
   60 
   61 #undef NEXUS_DEBUG
   62 #ifdef NEXUS_DEBUG
   63 #define dprintf printf
   64 #else 
   65 #define dprintf(x, arg...)
   66 #endif  /* NEXUS_DEBUG */
   67 
   68 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
   69 
   70 struct nexus_device {
   71         struct resource_list    nx_resources;
   72 };
   73 
   74 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
   75 #define NUM_MIPS_IRQS   6
   76 
   77 static struct rman irq_rman;
   78 static struct rman mem_rman;
   79 
   80 static struct resource *
   81                 nexus_alloc_resource(device_t, device_t, int, int *, u_long,
   82                     u_long, u_long, u_int);
   83 static int      nexus_activate_resource(device_t, device_t, int, int,
   84                     struct resource *);
   85 static device_t nexus_add_child(device_t, u_int, const char *, int);
   86 static int      nexus_attach(device_t);
   87 static int      nexus_deactivate_resource(device_t, device_t, int, int,
   88                     struct resource *);
   89 static void     nexus_delete_resource(device_t, device_t, int, int);
   90 static struct resource_list *
   91                 nexus_get_reslist(device_t, device_t);
   92 static int      nexus_get_resource(device_t, device_t, int, int, u_long *,
   93                     u_long *);
   94 static void     nexus_hinted_child(device_t, const char *, int);
   95 static int      nexus_print_child(device_t, device_t);
   96 static int      nexus_print_all_resources(device_t dev);
   97 static int      nexus_probe(device_t);
   98 static int      nexus_release_resource(device_t, device_t, int, int,
   99                     struct resource *);
  100 static int      nexus_set_resource(device_t, device_t, int, int, u_long,
  101                     u_long);
  102 static int      nexus_setup_intr(device_t dev, device_t child,
  103                     struct resource *res, int flags, driver_filter_t *filt,
  104                     driver_intr_t *intr, void *arg, void **cookiep);
  105 static int      nexus_teardown_intr(device_t, device_t, struct resource *,
  106                     void *);
  107 
  108 static device_method_t nexus_methods[] = {
  109         /* Device interface */
  110         DEVMETHOD(device_probe,         nexus_probe),
  111         DEVMETHOD(device_attach,        nexus_attach),
  112 
  113         /* Bus interface */
  114         DEVMETHOD(bus_add_child,        nexus_add_child),
  115         DEVMETHOD(bus_activate_resource,nexus_activate_resource),
  116         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
  117         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
  118         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
  119         DEVMETHOD(bus_get_resource,     nexus_get_resource),
  120         DEVMETHOD(bus_get_resource_list,        nexus_get_reslist),
  121         DEVMETHOD(bus_hinted_child,     nexus_hinted_child),
  122         DEVMETHOD(bus_print_child,      nexus_print_child),
  123         DEVMETHOD(bus_release_resource, nexus_release_resource),
  124         DEVMETHOD(bus_set_resource,     nexus_set_resource),
  125         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
  126         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
  127 
  128         { 0, 0 }
  129 };
  130 
  131 static driver_t nexus_driver = {
  132         "nexus",
  133         nexus_methods,
  134         1                       /* no softc */
  135 };
  136 static devclass_t nexus_devclass;
  137 
  138 static int
  139 nexus_probe(device_t dev)
  140 {
  141 
  142         device_set_desc(dev, "MIPS32 root nexus");
  143 
  144         irq_rman.rm_start = 0;
  145         irq_rman.rm_end = NUM_MIPS_IRQS - 1;
  146         irq_rman.rm_type = RMAN_ARRAY;
  147         irq_rman.rm_descr = "Hardware IRQs";
  148         if (rman_init(&irq_rman) != 0 ||
  149             rman_manage_region(&irq_rman, 0, NUM_MIPS_IRQS - 1) != 0) {
  150                 panic("%s: irq_rman", __func__);
  151         }
  152 
  153         mem_rman.rm_start = 0;
  154         mem_rman.rm_end = ~0ul;
  155         mem_rman.rm_type = RMAN_ARRAY;
  156         mem_rman.rm_descr = "Memory addresses";
  157         if (rman_init(&mem_rman) != 0 ||
  158             rman_manage_region(&mem_rman, 0, ~0) != 0) {
  159                 panic("%s: mem_rman", __func__);
  160         }
  161 
  162         return (0);
  163 }
  164 
  165 static int
  166 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
  167     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
  168 {
  169         register_t s;
  170         int irq;
  171 
  172         s = intr_disable();
  173         irq = rman_get_start(res);
  174         if (irq >= NUM_MIPS_IRQS) {
  175                 intr_restore(s);
  176                 return (0);
  177         }
  178 
  179         cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg,
  180             irq, flags, cookiep);
  181         intr_restore(s);
  182         return (0);
  183 }
  184 
  185 static int
  186 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
  187 {
  188 
  189         printf("Unimplemented %s at %s:%d\n", __func__, __FILE__, __LINE__);
  190         return (0);
  191 }
  192 
  193 static int
  194 nexus_attach(device_t dev)
  195 {
  196 
  197         bus_generic_probe(dev);
  198         bus_enumerate_hinted_children(dev);
  199         bus_generic_attach(dev);
  200 
  201         return (0);
  202 }
  203 
  204 static int
  205 nexus_print_child(device_t bus, device_t child)
  206 {
  207         int retval = 0;
  208 
  209         retval += bus_print_child_header(bus, child);
  210         retval += nexus_print_all_resources(child);
  211         if (device_get_flags(child))
  212                 retval += printf(" flags %#x", device_get_flags(child));
  213         retval += printf(" on %s\n", device_get_nameunit(bus));
  214 
  215         return (retval);
  216 }
  217 
  218 static int
  219 nexus_print_all_resources(device_t dev)
  220 {
  221         struct nexus_device *ndev = DEVTONX(dev);
  222         struct resource_list *rl = &ndev->nx_resources;
  223         int retval = 0;
  224 
  225         if (STAILQ_FIRST(rl))
  226                 retval += printf(" at");
  227 
  228         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
  229         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
  230 
  231         return (retval);
  232 }
  233 
  234 static void
  235 nexus_hinted_child(device_t bus, const char *dname, int dunit)
  236 {
  237         device_t child;
  238         long    maddr;
  239         int     msize;
  240         int     order;
  241         int     result;
  242         int     irq;
  243         int     mem_hints_count;
  244 
  245         if ((resource_int_value(dname, dunit, "order", &order)) != 0)
  246                 order = 1000;
  247         child = BUS_ADD_CHILD(bus, order, dname, dunit);
  248         if (child == NULL)
  249                 return;
  250 
  251         /*
  252          * Set hard-wired resources for hinted child using
  253          * specific RIDs.
  254          */
  255         mem_hints_count = 0;
  256         if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
  257                 mem_hints_count++;
  258         if (resource_int_value(dname, dunit, "msize", &msize) == 0)
  259                 mem_hints_count++;
  260 
  261         /* check if all info for mem resource has been provided */
  262         if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
  263                 printf("Either maddr or msize hint is missing for %s%d\n",
  264                     dname, dunit);
  265         } 
  266         else if (mem_hints_count) {
  267                 dprintf("%s: discovered hinted child %s at maddr %p(%d)\n",
  268                     __func__, device_get_nameunit(child),
  269                     (void *)(intptr_t)maddr, msize);
  270 
  271                 result = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, 
  272                     msize);
  273                 if (result != 0) {
  274                         device_printf(bus, 
  275                             "warning: bus_set_resource() failed\n");
  276                 }
  277         }
  278 
  279         if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
  280                 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
  281                 if (result != 0)
  282                         device_printf(bus,
  283                             "warning: bus_set_resource() failed\n");
  284         }
  285 }
  286 
  287 static device_t
  288 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
  289 {
  290         device_t        child;
  291         struct nexus_device *ndev;
  292 
  293         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
  294         if (!ndev)
  295                 return (0);
  296         resource_list_init(&ndev->nx_resources);
  297 
  298         child = device_add_child_ordered(bus, order, name, unit);
  299         if (child == NULL) {
  300                 device_printf(bus, "failed to add child: %s%d\n", name, unit);
  301                 return (0);
  302         }
  303 
  304         /* should we free this in nexus_child_detached? */
  305         device_set_ivars(child, ndev);
  306 
  307         return (child);
  308 }
  309 
  310 /*
  311  * Allocate a resource on behalf of child.  NB: child is usually going to be a
  312  * child of one of our descendants, not a direct child of nexus0.
  313  * (Exceptions include footbridge.)
  314  */
  315 static struct resource *
  316 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
  317         u_long start, u_long end, u_long count, u_int flags)
  318 {
  319         struct nexus_device             *ndev = DEVTONX(child);
  320         struct resource                 *rv;
  321         struct resource_list_entry      *rle;
  322         struct rman                     *rm;
  323         int                              isdefault, needactivate, passthrough;
  324 
  325         dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %ld, %d)\n",
  326             __func__, bus, child, type, rid, (void *)(intptr_t)start,
  327             (void *)(intptr_t)end, count, flags);
  328         dprintf("%s: requested rid is %d\n", __func__, *rid);
  329 
  330         isdefault = (start == 0UL && end == ~0UL && count == 1);
  331         needactivate = flags & RF_ACTIVE;
  332         passthrough = (device_get_parent(child) != bus);
  333         rle = NULL;
  334 
  335         /*
  336          * If this is an allocation of the "default" range for a given RID,
  337          * and we know what the resources for this device are (ie. they aren't
  338          * maintained by a child bus), then work out the start/end values.
  339          */
  340         if (isdefault) {
  341                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
  342                 if (rle == NULL)
  343                         return (NULL);
  344                 if (rle->res != NULL) {
  345                         panic("%s: resource entry is busy", __func__);
  346                 }
  347                 start = rle->start;
  348                 end = rle->end;
  349                 count = rle->count;
  350         }
  351 
  352         switch (type) {
  353         case SYS_RES_IRQ:
  354                 rm = &irq_rman;
  355                 break;
  356         case SYS_RES_MEMORY:
  357                 rm = &mem_rman;
  358                 break;
  359         default:
  360                 printf("%s: unknown resource type %d\n", __func__, type);
  361                 return (0);
  362         }
  363 
  364         rv = rman_reserve_resource(rm, start, end, count, flags, child);
  365         if (rv == 0) {
  366                 printf("%s: could not reserve resource for %s\n", __func__,
  367                     device_get_nameunit(child));
  368                 return (0);
  369         }
  370 
  371         rman_set_rid(rv, *rid);
  372 
  373         if (needactivate) {
  374                 if (bus_activate_resource(child, type, *rid, rv)) {
  375                         printf("%s: could not activate resource\n", __func__);
  376                         rman_release_resource(rv);
  377                         return (0);
  378                 }
  379         }
  380 
  381         return (rv);
  382 }
  383 
  384 static int
  385 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
  386     struct resource *r)
  387 {
  388         void *vaddr;
  389         vm_paddr_t paddr;
  390         vm_size_t psize;
  391 
  392         /*
  393          * If this is a memory resource, use pmap_mapdev to map it.
  394          */
  395         if (type == SYS_RES_MEMORY) {
  396                 paddr = rman_get_start(r);
  397                 psize = rman_get_size(r);
  398                 vaddr = pmap_mapdev(paddr, psize);
  399 
  400                 rman_set_virtual(r, vaddr);
  401                 rman_set_bustag(r, mips_bus_space_generic);
  402                 rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr);
  403         }
  404 
  405         return (rman_activate_resource(r));
  406 }
  407 
  408 static struct resource_list *
  409 nexus_get_reslist(device_t dev, device_t child)
  410 {
  411         struct nexus_device *ndev = DEVTONX(child);
  412 
  413         return (&ndev->nx_resources);
  414 }
  415 
  416 static int
  417 nexus_set_resource(device_t dev, device_t child, int type, int rid,
  418     u_long start, u_long count)
  419 {
  420         struct nexus_device             *ndev = DEVTONX(child);
  421         struct resource_list            *rl = &ndev->nx_resources;
  422         struct resource_list_entry      *rle;
  423 
  424         dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
  425             __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
  426 
  427         rle = resource_list_add(rl, type, rid, start, start + count - 1,
  428             count);
  429         if (rle == NULL)
  430                 return (ENXIO);
  431 
  432         return (0);
  433 }
  434 
  435 static int
  436 nexus_get_resource(device_t dev, device_t child, int type, int rid,
  437     u_long *startp, u_long *countp)
  438 {
  439         struct nexus_device             *ndev = DEVTONX(child);
  440         struct resource_list            *rl = &ndev->nx_resources;
  441         struct resource_list_entry      *rle;
  442 
  443         rle = resource_list_find(rl, type, rid);
  444         if (!rle)
  445                 return(ENOENT);
  446         if (startp)
  447                 *startp = rle->start;
  448         if (countp)
  449                 *countp = rle->count;
  450         return (0);
  451 }
  452 
  453 static void
  454 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
  455 {
  456         struct nexus_device     *ndev = DEVTONX(child);
  457         struct resource_list    *rl = &ndev->nx_resources;
  458 
  459         dprintf("%s: entry\n", __func__);
  460 
  461         resource_list_delete(rl, type, rid);
  462 }
  463 
  464 static int
  465 nexus_release_resource(device_t bus, device_t child, int type, int rid,
  466                        struct resource *r)
  467 {
  468         dprintf("%s: entry\n", __func__);
  469 
  470         if (rman_get_flags(r) & RF_ACTIVE) {
  471                 int error = bus_deactivate_resource(child, type, rid, r);
  472                 if (error)
  473                         return error;
  474         }
  475 
  476         return (rman_release_resource(r));
  477 }
  478 
  479 static int
  480 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
  481                           struct resource *r)
  482 {
  483         vm_offset_t va;
  484         
  485         if (type == SYS_RES_MEMORY) {
  486                 va = (vm_offset_t)rman_get_virtual(r);
  487                 pmap_unmapdev(va, rman_get_size(r));
  488         }
  489 
  490         return (rman_deactivate_resource(r));
  491 }
  492 
  493 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);

Cache object: 000f7765b6eb78efd949f10312a74bfe


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.