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/amd64/amd64/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 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/5.3/sys/amd64/amd64/nexus.c 134567 2004-08-31 05:26:37Z njl $");
   32 
   33 /*
   34  * This code implements a `root nexus' for Intel Architecture
   35  * machines.  The function of the root nexus is to serve as an
   36  * attachment point for both processors and buses, and to manage
   37  * resources which are common to all of them.  In particular,
   38  * this code implements the core resource managers for interrupt
   39  * requests, DMA requests (which rightfully should be a part of the
   40  * ISA code but it's easier to do it here for now), I/O port addresses,
   41  * and I/O memory address space.
   42  */
   43 
   44 #define __RMAN_RESOURCE_VISIBLE
   45 #include "opt_isa.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/bus.h>
   50 #include <sys/kernel.h>
   51 #include <sys/malloc.h>
   52 #include <sys/module.h>
   53 #include <machine/bus.h>
   54 #include <machine/intr_machdep.h>
   55 #include <sys/rman.h>
   56 #include <sys/interrupt.h>
   57 
   58 #include <machine/vmparam.h>
   59 #include <vm/vm.h>
   60 #include <vm/pmap.h>
   61 #include <machine/pmap.h>
   62 
   63 #include <machine/resource.h>
   64 
   65 #ifdef DEV_ISA
   66 #include <isa/isavar.h>
   67 #include <amd64/isa/isa.h>
   68 #endif
   69 #include <sys/rtprio.h>
   70 
   71 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
   72 struct nexus_device {
   73         struct resource_list    nx_resources;
   74 };
   75 
   76 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
   77 
   78 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
   79 
   80 static  int nexus_probe(device_t);
   81 static  int nexus_attach(device_t);
   82 static  int nexus_print_all_resources(device_t dev);
   83 static  int nexus_print_child(device_t, device_t);
   84 static device_t nexus_add_child(device_t bus, int order, const char *name,
   85                                 int unit);
   86 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
   87                                               u_long, u_long, u_long, u_int);
   88 static  int nexus_config_intr(device_t, int, enum intr_trigger,
   89                               enum intr_polarity);
   90 static  int nexus_activate_resource(device_t, device_t, int, int,
   91                                     struct resource *);
   92 static  int nexus_deactivate_resource(device_t, device_t, int, int,
   93                                       struct resource *);
   94 static  int nexus_release_resource(device_t, device_t, int, int,
   95                                    struct resource *);
   96 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
   97                              void (*)(void *), void *, void **);
   98 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
   99                                 void *);
  100 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
  101 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
  102 static  int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
  103 static void nexus_delete_resource(device_t, device_t, int, int);
  104 
  105 static device_method_t nexus_methods[] = {
  106         /* Device interface */
  107         DEVMETHOD(device_probe,         nexus_probe),
  108         DEVMETHOD(device_attach,        nexus_attach),
  109         DEVMETHOD(device_detach,        bus_generic_detach),
  110         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  111         DEVMETHOD(device_suspend,       bus_generic_suspend),
  112         DEVMETHOD(device_resume,        bus_generic_resume),
  113 
  114         /* Bus interface */
  115         DEVMETHOD(bus_print_child,      nexus_print_child),
  116         DEVMETHOD(bus_add_child,        nexus_add_child),
  117         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
  118         DEVMETHOD(bus_release_resource, nexus_release_resource),
  119         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
  120         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
  121         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
  122         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
  123         DEVMETHOD(bus_config_intr,      nexus_config_intr),
  124         DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
  125         DEVMETHOD(bus_set_resource,     nexus_set_resource),
  126         DEVMETHOD(bus_get_resource,     nexus_get_resource),
  127         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
  128 
  129         { 0, 0 }
  130 };
  131 
  132 static driver_t nexus_driver = {
  133         "nexus",
  134         nexus_methods,
  135         1,                      /* no softc */
  136 };
  137 static devclass_t nexus_devclass;
  138 
  139 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
  140 
  141 static int
  142 nexus_probe(device_t dev)
  143 {
  144         int irq, last;
  145 
  146         device_quiet(dev);      /* suppress attach message for neatness */
  147 
  148         /* 
  149          * XXX working notes:
  150          *
  151          * - IRQ resource creation should be moved to the PIC/APIC driver.
  152          * - DRQ resource creation should be moved to the DMAC driver.
  153          * - The above should be sorted to probe earlier than any child busses.
  154          *
  155          * - Leave I/O and memory creation here, as child probes may need them.
  156          *   (especially eg. ACPI)
  157          */
  158 
  159         /*
  160          * IRQ's are on the mainboard on old systems, but on the ISA part
  161          * of PCI->ISA bridges.  There would be multiple sets of IRQs on
  162          * multi-ISA-bus systems.  PCI interrupts are routed to the ISA
  163          * component, so in a way, PCI can be a partial child of an ISA bus(!).
  164          * APIC interrupts are global though.
  165          */
  166         irq_rman.rm_start = 0;
  167         irq_rman.rm_type = RMAN_ARRAY;
  168         irq_rman.rm_descr = "Interrupt request lines";
  169         irq_rman.rm_end = NUM_IO_INTS - 1;
  170         if (rman_init(&irq_rman))
  171                 panic("nexus_probe irq_rman");
  172 
  173         /*
  174          * We search for regions of existing IRQs and add those to the IRQ
  175          * resource manager.
  176          */
  177         last = -1;
  178         for (irq = 0; irq < NUM_IO_INTS; irq++)
  179                 if (intr_lookup_source(irq) != NULL) {
  180                         if (last == -1)
  181                                 last = irq;
  182                 } else if (last != -1) {
  183                         if (rman_manage_region(&irq_rman, last, irq - 1) != 0)
  184                                 panic("nexus_probe irq_rman add");
  185                         last = -1;
  186                 }
  187         if (last != -1 && rman_manage_region(&irq_rman, last, irq - 1) != 0)
  188                 panic("nexus_probe irq_rman add");
  189 
  190         /*
  191          * ISA DMA on PCI systems is implemented in the ISA part of each
  192          * PCI->ISA bridge and the channels can be duplicated if there are
  193          * multiple bridges.  (eg: laptops with docking stations)
  194          */
  195         drq_rman.rm_start = 0;
  196         drq_rman.rm_end = 7;
  197         drq_rman.rm_type = RMAN_ARRAY;
  198         drq_rman.rm_descr = "DMA request lines";
  199         /* XXX drq 0 not available on some machines */
  200         if (rman_init(&drq_rman)
  201             || rman_manage_region(&drq_rman,
  202                                   drq_rman.rm_start, drq_rman.rm_end))
  203                 panic("nexus_probe drq_rman");
  204 
  205         /*
  206          * However, IO ports and Memory truely are global at this level,
  207          * as are APIC interrupts (however many IO APICS there turn out
  208          * to be on large systems..)
  209          */
  210         port_rman.rm_start = 0;
  211         port_rman.rm_end = 0xffff;
  212         port_rman.rm_type = RMAN_ARRAY;
  213         port_rman.rm_descr = "I/O ports";
  214         if (rman_init(&port_rman)
  215             || rman_manage_region(&port_rman, 0, 0xffff))
  216                 panic("nexus_probe port_rman");
  217 
  218         mem_rman.rm_start = 0;
  219         mem_rman.rm_end = ~0u;
  220         mem_rman.rm_type = RMAN_ARRAY;
  221         mem_rman.rm_descr = "I/O memory addresses";
  222         if (rman_init(&mem_rman)
  223             || rman_manage_region(&mem_rman, 0, ~0))
  224                 panic("nexus_probe mem_rman");
  225 
  226         return 0;
  227 }
  228 
  229 static int
  230 nexus_attach(device_t dev)
  231 {
  232 
  233         bus_generic_probe(dev);
  234         bus_generic_attach(dev);
  235         return 0;
  236 }
  237 
  238 static int
  239 nexus_print_all_resources(device_t dev)
  240 {
  241         struct  nexus_device *ndev = DEVTONX(dev);
  242         struct resource_list *rl = &ndev->nx_resources;
  243         int retval = 0;
  244 
  245         if (SLIST_FIRST(rl))
  246                 retval += printf(" at");
  247         
  248         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
  249         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
  250         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
  251 
  252         return retval;
  253 }
  254 
  255 static int
  256 nexus_print_child(device_t bus, device_t child)
  257 {
  258         int retval = 0;
  259 
  260         retval += bus_print_child_header(bus, child);
  261         retval += nexus_print_all_resources(child);
  262         retval += printf(" on motherboard\n");  /* XXX "motherboard", ick */
  263 
  264         return (retval);
  265 }
  266 
  267 static device_t
  268 nexus_add_child(device_t bus, int order, const char *name, int unit)
  269 {
  270         device_t                child;
  271         struct nexus_device     *ndev;
  272 
  273         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
  274         if (!ndev)
  275                 return(0);
  276         resource_list_init(&ndev->nx_resources);
  277 
  278         child = device_add_child_ordered(bus, order, name, unit); 
  279 
  280         /* should we free this in nexus_child_detached? */
  281         device_set_ivars(child, ndev);
  282 
  283         return(child);
  284 }
  285 
  286 /*
  287  * Allocate a resource on behalf of child.  NB: child is usually going to be a
  288  * child of one of our descendants, not a direct child of nexus0.
  289  */
  290 static struct resource *
  291 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
  292                      u_long start, u_long end, u_long count, u_int flags)
  293 {
  294         struct nexus_device *ndev = DEVTONX(child);
  295         struct  resource *rv;
  296         struct resource_list_entry *rle;
  297         struct  rman *rm;
  298         int needactivate = flags & RF_ACTIVE;
  299 
  300         /*
  301          * If this is an allocation of the "default" range for a given RID, and
  302          * we know what the resources for this device are (ie. they aren't maintained
  303          * by a child bus), then work out the start/end values.
  304          */
  305         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
  306                 if (ndev == NULL)
  307                         return(NULL);
  308                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
  309                 if (rle == NULL)
  310                         return(NULL);
  311                 start = rle->start;
  312                 end = rle->end;
  313                 count = rle->count;
  314         }
  315 
  316         flags &= ~RF_ACTIVE;
  317 
  318         switch (type) {
  319         case SYS_RES_IRQ:
  320                 rm = &irq_rman;
  321                 break;
  322 
  323         case SYS_RES_DRQ:
  324                 rm = &drq_rman;
  325                 break;
  326 
  327         case SYS_RES_IOPORT:
  328                 rm = &port_rman;
  329                 break;
  330 
  331         case SYS_RES_MEMORY:
  332                 rm = &mem_rman;
  333                 break;
  334 
  335         default:
  336                 return 0;
  337         }
  338 
  339         rv = rman_reserve_resource(rm, start, end, count, flags, child);
  340         if (rv == 0)
  341                 return 0;
  342 
  343         if (type == SYS_RES_MEMORY) {
  344                 rman_set_bustag(rv, AMD64_BUS_SPACE_MEM);
  345         } else if (type == SYS_RES_IOPORT) {
  346                 rman_set_bustag(rv, AMD64_BUS_SPACE_IO);
  347                 rman_set_bushandle(rv, rman_get_start(rv));
  348         }
  349 
  350         if (needactivate) {
  351                 if (bus_activate_resource(child, type, *rid, rv)) {
  352                         rman_release_resource(rv);
  353                         return 0;
  354                 }
  355         }
  356         
  357         return rv;
  358 }
  359 
  360 static int
  361 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
  362                         struct resource *r)
  363 {
  364 
  365         /*
  366          * If this is a memory resource, map it into the kernel.
  367          */
  368         if (rman_get_bustag(r) == AMD64_BUS_SPACE_MEM) {
  369                 caddr_t vaddr = 0;
  370 
  371                 if (rman_get_end(r) < 1024 * 1024) {
  372                         /*
  373                          * The first 1Mb is mapped at KERNBASE.
  374                          */
  375                         vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
  376                 } else {
  377                         u_int64_t paddr;
  378                         u_int64_t psize;
  379                         u_int32_t poffs;
  380 
  381                         paddr = rman_get_start(r);
  382                         psize = rman_get_size(r);
  383 
  384                         poffs = paddr - trunc_page(paddr);
  385                         vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
  386                 }
  387                 rman_set_virtual(r, vaddr);
  388                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
  389         }
  390         return (rman_activate_resource(r));
  391 }
  392 
  393 static int
  394 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
  395                           struct resource *r)
  396 {
  397         /*
  398          * If this is a memory resource, unmap it.
  399          */
  400         if ((rman_get_bustag(r) == AMD64_BUS_SPACE_MEM) &&
  401             (rman_get_end(r) >= 1024 * 1024)) {
  402                 u_int32_t psize;
  403 
  404                 psize = rman_get_size(r);
  405                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
  406         }
  407                 
  408         return (rman_deactivate_resource(r));
  409 }
  410 
  411 static int
  412 nexus_release_resource(device_t bus, device_t child, int type, int rid,
  413                        struct resource *r)
  414 {
  415         if (rman_get_flags(r) & RF_ACTIVE) {
  416                 int error = bus_deactivate_resource(child, type, rid, r);
  417                 if (error)
  418                         return error;
  419         }
  420         return (rman_release_resource(r));
  421 }
  422 
  423 /*
  424  * Currently this uses the really grody interface from kern/kern_intr.c
  425  * (which really doesn't belong in kern/anything.c).  Eventually, all of
  426  * the code in kern_intr.c and machdep_intr.c should get moved here, since
  427  * this is going to be the official interface.
  428  */
  429 static int
  430 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
  431                  int flags, void (*ihand)(void *), void *arg, void **cookiep)
  432 {
  433         int             error;
  434 
  435         /* somebody tried to setup an irq that failed to allocate! */
  436         if (irq == NULL)
  437                 panic("nexus_setup_intr: NULL irq resource!");
  438 
  439         *cookiep = 0;
  440         if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
  441                 flags |= INTR_EXCL;
  442 
  443         /*
  444          * We depend here on rman_activate_resource() being idempotent.
  445          */
  446         error = rman_activate_resource(irq);
  447         if (error)
  448                 return (error);
  449 
  450         error = intr_add_handler(device_get_nameunit(child),
  451             rman_get_start(irq), ihand, arg, flags, cookiep);
  452 
  453         return (error);
  454 }
  455 
  456 static int
  457 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
  458 {
  459         return (intr_remove_handler(ih));
  460 }
  461 
  462 static int
  463 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
  464     enum intr_polarity pol)
  465 {
  466         return (intr_config_intr(irq, trig, pol));
  467 }
  468 
  469 static struct resource_list *
  470 nexus_get_reslist(device_t dev, device_t child)
  471 {
  472         struct nexus_device *ndev = DEVTONX(child);
  473 
  474         return (&ndev->nx_resources);
  475 }
  476 
  477 static int
  478 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
  479 {
  480         struct nexus_device     *ndev = DEVTONX(child);
  481         struct resource_list    *rl = &ndev->nx_resources;
  482 
  483         /* XXX this should return a success/failure indicator */
  484         resource_list_add(rl, type, rid, start, start + count - 1, count);
  485         return(0);
  486 }
  487 
  488 static int
  489 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
  490 {
  491         struct nexus_device     *ndev = DEVTONX(child);
  492         struct resource_list    *rl = &ndev->nx_resources;
  493         struct resource_list_entry *rle;
  494 
  495         rle = resource_list_find(rl, type, rid);
  496         if (!rle)
  497                 return(ENOENT);
  498         if (startp)
  499                 *startp = rle->start;
  500         if (countp)
  501                 *countp = rle->count;
  502         return(0);
  503 }
  504 
  505 static void
  506 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
  507 {
  508         struct nexus_device     *ndev = DEVTONX(child);
  509         struct resource_list    *rl = &ndev->nx_resources;
  510 
  511         resource_list_delete(rl, type, rid);
  512 }
  513 
  514 #ifdef DEV_ISA
  515 /*
  516  * Placeholder which claims PnP 'devices' which describe system 
  517  * resources.
  518  */
  519 static struct isa_pnp_id sysresource_ids[] = {
  520         { 0x010cd041 /* PNP0c01 */, "System Memory" },
  521         { 0x020cd041 /* PNP0c02 */, "System Resource" },
  522         { 0 }
  523 };
  524 
  525 static int
  526 sysresource_probe(device_t dev)
  527 {
  528         int     result;
  529         
  530         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
  531                 device_quiet(dev);
  532         }
  533         return(result);
  534 }
  535 
  536 static int
  537 sysresource_attach(device_t dev)
  538 {
  539         return(0);
  540 }
  541 
  542 static device_method_t sysresource_methods[] = {
  543         /* Device interface */
  544         DEVMETHOD(device_probe,         sysresource_probe),
  545         DEVMETHOD(device_attach,        sysresource_attach),
  546         DEVMETHOD(device_detach,        bus_generic_detach),
  547         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  548         DEVMETHOD(device_suspend,       bus_generic_suspend),
  549         DEVMETHOD(device_resume,        bus_generic_resume),
  550         { 0, 0 }
  551 };
  552 
  553 static driver_t sysresource_driver = {
  554         "sysresource",
  555         sysresource_methods,
  556         1,              /* no softc */
  557 };
  558 
  559 static devclass_t sysresource_devclass;
  560 
  561 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
  562 #endif /* DEV_ISA */

Cache object: d637ee5b1a7e21ae87f2f8971d71f4a2


[ 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.