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/busdma_machdep.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 (c) 1997, 1998 Justin T. Gibbs.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions, and the following disclaimer,
   10  *    without modification, immediately at the beginning of the file.
   11  * 2. The name of the author may not be used to endorse or promote products
   12  *    derived from this software without specific prior written permission.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/7.4/sys/amd64/amd64/busdma_machdep.c 200289 2009-12-09 08:16:12Z scottl $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/malloc.h>
   33 #include <sys/bus.h>
   34 #include <sys/interrupt.h>
   35 #include <sys/kernel.h>
   36 #include <sys/ktr.h>
   37 #include <sys/lock.h>
   38 #include <sys/proc.h>
   39 #include <sys/mutex.h>
   40 #include <sys/mbuf.h>
   41 #include <sys/uio.h>
   42 #include <sys/sysctl.h>
   43 
   44 #include <vm/vm.h>
   45 #include <vm/vm_page.h>
   46 #include <vm/vm_map.h>
   47 
   48 #include <machine/atomic.h>
   49 #include <machine/bus.h>
   50 #include <machine/md_var.h>
   51 #include <machine/specialreg.h>
   52 
   53 #define MAX_BPAGES 8192
   54 
   55 struct bounce_zone;
   56 
   57 struct bus_dma_tag {
   58         bus_dma_tag_t     parent;
   59         bus_size_t        alignment;
   60         bus_size_t        boundary;
   61         bus_addr_t        lowaddr;
   62         bus_addr_t        highaddr;
   63         bus_dma_filter_t *filter;
   64         void             *filterarg;
   65         bus_size_t        maxsize;
   66         u_int             nsegments;
   67         bus_size_t        maxsegsz;
   68         int               flags;
   69         int               ref_count;
   70         int               map_count;
   71         bus_dma_lock_t   *lockfunc;
   72         void             *lockfuncarg;
   73         bus_dma_segment_t *segments;
   74         struct bounce_zone *bounce_zone;
   75 };
   76 
   77 struct bounce_page {
   78         vm_offset_t     vaddr;          /* kva of bounce buffer */
   79         bus_addr_t      busaddr;        /* Physical address */
   80         vm_offset_t     datavaddr;      /* kva of client data */
   81         bus_size_t      datacount;      /* client data count */
   82         STAILQ_ENTRY(bounce_page) links;
   83 };
   84 
   85 int busdma_swi_pending;
   86 
   87 struct bounce_zone {
   88         STAILQ_ENTRY(bounce_zone) links;
   89         STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
   90         int             total_bpages;
   91         int             free_bpages;
   92         int             reserved_bpages;
   93         int             active_bpages;
   94         int             total_bounced;
   95         int             total_deferred;
   96         bus_size_t      alignment;
   97         bus_addr_t      lowaddr;
   98         char            zoneid[8];
   99         char            lowaddrid[20];
  100         struct sysctl_ctx_list sysctl_tree;
  101         struct sysctl_oid *sysctl_tree_top;
  102 };
  103 
  104 static struct mtx bounce_lock;
  105 static int total_bpages;
  106 static int busdma_zonecount;
  107 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
  108 
  109 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
  110 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
  111            "Total bounce pages");
  112 
  113 struct bus_dmamap {
  114         struct bp_list         bpages;
  115         int                    pagesneeded;
  116         int                    pagesreserved;
  117         bus_dma_tag_t          dmat;
  118         void                  *buf;             /* unmapped buffer pointer */
  119         bus_size_t             buflen;          /* unmapped buffer length */
  120         bus_dmamap_callback_t *callback;
  121         void                  *callback_arg;
  122         STAILQ_ENTRY(bus_dmamap) links;
  123 };
  124 
  125 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
  126 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
  127 static struct bus_dmamap nobounce_dmamap;
  128 
  129 static void init_bounce_pages(void *dummy);
  130 static int alloc_bounce_zone(bus_dma_tag_t dmat);
  131 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
  132 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
  133                                 int commit);
  134 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
  135                                    vm_offset_t vaddr, bus_size_t size);
  136 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
  137 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
  138 
  139 /*
  140  * Return true if a match is made.
  141  *
  142  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
  143  *
  144  * If paddr is within the bounds of the dma tag then call the filter callback
  145  * to check for a match, if there is no filter callback then assume a match.
  146  */
  147 static __inline int
  148 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
  149 {
  150         int retval;
  151 
  152         retval = 0;
  153 
  154         do {
  155                 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
  156                  || ((paddr & (dmat->alignment - 1)) != 0))
  157                  && (dmat->filter == NULL
  158                   || (*dmat->filter)(dmat->filterarg, paddr) != 0))
  159                         retval = 1;
  160 
  161                 dmat = dmat->parent;            
  162         } while (retval == 0 && dmat != NULL);
  163         return (retval);
  164 }
  165 
  166 /*
  167  * Convenience function for manipulating driver locks from busdma (during
  168  * busdma_swi, for example).  Drivers that don't provide their own locks
  169  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
  170  * non-mutex locking scheme don't have to use this at all.
  171  */
  172 void
  173 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
  174 {
  175         struct mtx *dmtx;
  176 
  177         dmtx = (struct mtx *)arg;
  178         switch (op) {
  179         case BUS_DMA_LOCK:
  180                 mtx_lock(dmtx);
  181                 break;
  182         case BUS_DMA_UNLOCK:
  183                 mtx_unlock(dmtx);
  184                 break;
  185         default:
  186                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
  187         }
  188 }
  189 
  190 /*
  191  * dflt_lock should never get called.  It gets put into the dma tag when
  192  * lockfunc == NULL, which is only valid if the maps that are associated
  193  * with the tag are meant to never be defered.
  194  * XXX Should have a way to identify which driver is responsible here.
  195  */
  196 static void
  197 dflt_lock(void *arg, bus_dma_lock_op_t op)
  198 {
  199         panic("driver error: busdma dflt_lock called");
  200 }
  201 
  202 #define BUS_DMA_COULD_BOUNCE    BUS_DMA_BUS3
  203 #define BUS_DMA_MIN_ALLOC_COMP  BUS_DMA_BUS4
  204 /*
  205  * Allocate a device specific dma_tag.
  206  */
  207 int
  208 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
  209                    bus_size_t boundary, bus_addr_t lowaddr,
  210                    bus_addr_t highaddr, bus_dma_filter_t *filter,
  211                    void *filterarg, bus_size_t maxsize, int nsegments,
  212                    bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
  213                    void *lockfuncarg, bus_dma_tag_t *dmat)
  214 {
  215         bus_dma_tag_t newtag;
  216         int error = 0;
  217 
  218         /* Basic sanity checking */
  219         if (boundary != 0 && boundary < maxsegsz)
  220                 maxsegsz = boundary;
  221 
  222         if (maxsegsz == 0) {
  223                 return (EINVAL);
  224         }
  225 
  226         /* Return a NULL tag on failure */
  227         *dmat = NULL;
  228 
  229         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
  230             M_ZERO | M_NOWAIT);
  231         if (newtag == NULL) {
  232                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
  233                     __func__, newtag, 0, error);
  234                 return (ENOMEM);
  235         }
  236 
  237         newtag->parent = parent;
  238         newtag->alignment = alignment;
  239         newtag->boundary = boundary;
  240         newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
  241         newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
  242             (PAGE_SIZE - 1);
  243         newtag->filter = filter;
  244         newtag->filterarg = filterarg;
  245         newtag->maxsize = maxsize;
  246         newtag->nsegments = nsegments;
  247         newtag->maxsegsz = maxsegsz;
  248         newtag->flags = flags;
  249         newtag->ref_count = 1; /* Count ourself */
  250         newtag->map_count = 0;
  251         if (lockfunc != NULL) {
  252                 newtag->lockfunc = lockfunc;
  253                 newtag->lockfuncarg = lockfuncarg;
  254         } else {
  255                 newtag->lockfunc = dflt_lock;
  256                 newtag->lockfuncarg = NULL;
  257         }
  258         newtag->segments = NULL;
  259 
  260         /* Take into account any restrictions imposed by our parent tag */
  261         if (parent != NULL) {
  262                 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
  263                 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
  264                 if (newtag->boundary == 0)
  265                         newtag->boundary = parent->boundary;
  266                 else if (parent->boundary != 0)
  267                         newtag->boundary = MIN(parent->boundary,
  268                                                newtag->boundary);
  269                 if (newtag->filter == NULL) {
  270                         /*
  271                          * Short circuit looking at our parent directly
  272                          * since we have encapsulated all of its information
  273                          */
  274                         newtag->filter = parent->filter;
  275                         newtag->filterarg = parent->filterarg;
  276                         newtag->parent = parent->parent;
  277                 }
  278                 if (newtag->parent != NULL)
  279                         atomic_add_int(&parent->ref_count, 1);
  280         }
  281 
  282         if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
  283          || newtag->alignment > 1)
  284                 newtag->flags |= BUS_DMA_COULD_BOUNCE;
  285 
  286         if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
  287             (flags & BUS_DMA_ALLOCNOW) != 0) {
  288                 struct bounce_zone *bz;
  289 
  290                 /* Must bounce */
  291 
  292                 if ((error = alloc_bounce_zone(newtag)) != 0) {
  293                         free(newtag, M_DEVBUF);
  294                         return (error);
  295                 }
  296                 bz = newtag->bounce_zone;
  297 
  298                 if (ptoa(bz->total_bpages) < maxsize) {
  299                         int pages;
  300 
  301                         pages = atop(maxsize) - bz->total_bpages;
  302 
  303                         /* Add pages to our bounce pool */
  304                         if (alloc_bounce_pages(newtag, pages) < pages)
  305                                 error = ENOMEM;
  306                 }
  307                 /* Performed initial allocation */
  308                 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
  309         }
  310         
  311         if (error != 0) {
  312                 free(newtag, M_DEVBUF);
  313         } else {
  314                 *dmat = newtag;
  315         }
  316         CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
  317             __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
  318         return (error);
  319 }
  320 
  321 int
  322 bus_dma_tag_destroy(bus_dma_tag_t dmat)
  323 {
  324         bus_dma_tag_t dmat_copy;
  325         int error;
  326 
  327         error = 0;
  328         dmat_copy = dmat;
  329 
  330         if (dmat != NULL) {
  331 
  332                 if (dmat->map_count != 0) {
  333                         error = EBUSY;
  334                         goto out;
  335                 }
  336 
  337                 while (dmat != NULL) {
  338                         bus_dma_tag_t parent;
  339 
  340                         parent = dmat->parent;
  341                         atomic_subtract_int(&dmat->ref_count, 1);
  342                         if (dmat->ref_count == 0) {
  343                                 if (dmat->segments != NULL)
  344                                         free(dmat->segments, M_DEVBUF);
  345                                 free(dmat, M_DEVBUF);
  346                                 /*
  347                                  * Last reference count, so
  348                                  * release our reference
  349                                  * count on our parent.
  350                                  */
  351                                 dmat = parent;
  352                         } else
  353                                 dmat = NULL;
  354                 }
  355         }
  356 out:
  357         CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
  358         return (error);
  359 }
  360 
  361 /*
  362  * Allocate a handle for mapping from kva/uva/physical
  363  * address space into bus device space.
  364  */
  365 int
  366 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
  367 {
  368         int error;
  369 
  370         error = 0;
  371 
  372         if (dmat->segments == NULL) {
  373                 dmat->segments = (bus_dma_segment_t *)malloc(
  374                     sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
  375                     M_NOWAIT);
  376                 if (dmat->segments == NULL) {
  377                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  378                             __func__, dmat, ENOMEM);
  379                         return (ENOMEM);
  380                 }
  381         }
  382 
  383         /*
  384          * Bouncing might be required if the driver asks for an active
  385          * exclusion region, a data alignment that is stricter than 1, and/or
  386          * an active address boundary.
  387          */
  388         if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
  389 
  390                 /* Must bounce */
  391                 struct bounce_zone *bz;
  392                 int maxpages;
  393 
  394                 if (dmat->bounce_zone == NULL) {
  395                         if ((error = alloc_bounce_zone(dmat)) != 0)
  396                                 return (error);
  397                 }
  398                 bz = dmat->bounce_zone;
  399 
  400                 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
  401                                              M_NOWAIT | M_ZERO);
  402                 if (*mapp == NULL) {
  403                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  404                             __func__, dmat, ENOMEM);
  405                         return (ENOMEM);
  406                 }
  407 
  408                 /* Initialize the new map */
  409                 STAILQ_INIT(&((*mapp)->bpages));
  410 
  411                 /*
  412                  * Attempt to add pages to our pool on a per-instance
  413                  * basis up to a sane limit.
  414                  */
  415                 if (dmat->alignment > 1)
  416                         maxpages = MAX_BPAGES;
  417                 else
  418                         maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
  419                 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
  420                  || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
  421                         int pages;
  422 
  423                         pages = MAX(atop(dmat->maxsize), 1);
  424                         pages = MIN(maxpages - bz->total_bpages, pages);
  425                         pages = MAX(pages, 1);
  426                         if (alloc_bounce_pages(dmat, pages) < pages)
  427                                 error = ENOMEM;
  428 
  429                         if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
  430                                 if (error == 0)
  431                                         dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
  432                         } else {
  433                                 error = 0;
  434                         }
  435                 }
  436         } else {
  437                 *mapp = NULL;
  438         }
  439         if (error == 0)
  440                 dmat->map_count++;
  441         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  442             __func__, dmat, dmat->flags, error);
  443         return (error);
  444 }
  445 
  446 /*
  447  * Destroy a handle for mapping from kva/uva/physical
  448  * address space into bus device space.
  449  */
  450 int
  451 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
  452 {
  453         if (map != NULL && map != &nobounce_dmamap) {
  454                 if (STAILQ_FIRST(&map->bpages) != NULL) {
  455                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  456                             __func__, dmat, EBUSY);
  457                         return (EBUSY);
  458                 }
  459                 free(map, M_DEVBUF);
  460         }
  461         dmat->map_count--;
  462         CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
  463         return (0);
  464 }
  465 
  466 
  467 /*
  468  * Allocate a piece of memory that can be efficiently mapped into
  469  * bus device space based on the constraints lited in the dma tag.
  470  * A dmamap to for use with dmamap_load is also allocated.
  471  */
  472 int
  473 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
  474                  bus_dmamap_t *mapp)
  475 {
  476         int mflags;
  477 
  478         if (flags & BUS_DMA_NOWAIT)
  479                 mflags = M_NOWAIT;
  480         else
  481                 mflags = M_WAITOK;
  482         if (flags & BUS_DMA_ZERO)
  483                 mflags |= M_ZERO;
  484 
  485         /* If we succeed, no mapping/bouncing will be required */
  486         *mapp = NULL;
  487 
  488         if (dmat->segments == NULL) {
  489                 dmat->segments = (bus_dma_segment_t *)malloc(
  490                     sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
  491                     M_NOWAIT);
  492                 if (dmat->segments == NULL) {
  493                         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  494                             __func__, dmat, dmat->flags, ENOMEM);
  495                         return (ENOMEM);
  496                 }
  497         }
  498 
  499         /* 
  500          * XXX:
  501          * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
  502          * alignment guarantees of malloc need to be nailed down, and the
  503          * code below should be rewritten to take that into account.
  504          *
  505          * In the meantime, we'll warn the user if malloc gets it wrong.
  506          */
  507         if ((dmat->maxsize <= PAGE_SIZE) &&
  508            (dmat->alignment < dmat->maxsize) &&
  509             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
  510                 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
  511         } else {
  512                 /*
  513                  * XXX Use Contigmalloc until it is merged into this facility
  514                  *     and handles multi-seg allocations.  Nobody is doing
  515                  *     multi-seg allocations yet though.
  516                  * XXX Certain AGP hardware does.
  517                  */
  518                 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
  519                     0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
  520                     dmat->boundary);
  521         }
  522         if (*vaddr == NULL) {
  523                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  524                     __func__, dmat, dmat->flags, ENOMEM);
  525                 return (ENOMEM);
  526         } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
  527                 printf("bus_dmamem_alloc failed to align memory properly.\n");
  528         }
  529         if (flags & BUS_DMA_NOCACHE)
  530                 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
  531                     PAT_UNCACHEABLE);
  532         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  533             __func__, dmat, dmat->flags, ENOMEM);
  534         return (0);
  535 }
  536 
  537 /*
  538  * Free a piece of memory and it's allociated dmamap, that was allocated
  539  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
  540  */
  541 void
  542 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
  543 {
  544         /*
  545          * dmamem does not need to be bounced, so the map should be
  546          * NULL
  547          */
  548         if (map != NULL)
  549                 panic("bus_dmamem_free: Invalid map freed\n");
  550         pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
  551         if ((dmat->maxsize <= PAGE_SIZE) &&
  552            (dmat->alignment < dmat->maxsize) &&
  553             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
  554                 free(vaddr, M_DEVBUF);
  555         else {
  556                 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
  557         }
  558         CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
  559 }
  560 
  561 /*
  562  * Utility function to load a linear buffer.  lastaddrp holds state
  563  * between invocations (for multiple-buffer loads).  segp contains
  564  * the starting segment on entrace, and the ending segment on exit.
  565  * first indicates if this is the first invocation of this function.
  566  */
  567 static __inline int
  568 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
  569                         bus_dmamap_t map,
  570                         void *buf, bus_size_t buflen,
  571                         pmap_t pmap,
  572                         int flags,
  573                         bus_addr_t *lastaddrp,
  574                         bus_dma_segment_t *segs,
  575                         int *segp,
  576                         int first)
  577 {
  578         bus_size_t sgsize;
  579         bus_addr_t curaddr, lastaddr, baddr, bmask;
  580         vm_offset_t vaddr;
  581         bus_addr_t paddr;
  582         int seg;
  583 
  584         if (map == NULL)
  585                 map = &nobounce_dmamap;
  586 
  587         if ((map != &nobounce_dmamap && map->pagesneeded == 0) 
  588          && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
  589                 vm_offset_t     vendaddr;
  590 
  591                 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
  592                     "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
  593                     dmat->boundary, dmat->alignment);
  594                 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
  595                     map, &nobounce_dmamap, map->pagesneeded);
  596                 /*
  597                  * Count the number of bounce pages
  598                  * needed in order to complete this transfer
  599                  */
  600                 vaddr = (vm_offset_t)buf;
  601                 vendaddr = (vm_offset_t)buf + buflen;
  602 
  603                 while (vaddr < vendaddr) {
  604                         if (pmap)
  605                                 paddr = pmap_extract(pmap, vaddr);
  606                         else
  607                                 paddr = pmap_kextract(vaddr);
  608                         if (run_filter(dmat, paddr) != 0)
  609                                 map->pagesneeded++;
  610                         vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
  611                 }
  612                 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
  613         }
  614 
  615         /* Reserve Necessary Bounce Pages */
  616         if (map->pagesneeded != 0) {
  617                 mtx_lock(&bounce_lock);
  618                 if (flags & BUS_DMA_NOWAIT) {
  619                         if (reserve_bounce_pages(dmat, map, 0) != 0) {
  620                                 mtx_unlock(&bounce_lock);
  621                                 return (ENOMEM);
  622                         }
  623                 } else {
  624                         if (reserve_bounce_pages(dmat, map, 1) != 0) {
  625                                 /* Queue us for resources */
  626                                 map->dmat = dmat;
  627                                 map->buf = buf;
  628                                 map->buflen = buflen;
  629                                 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
  630                                     map, links);
  631                                 mtx_unlock(&bounce_lock);
  632                                 return (EINPROGRESS);
  633                         }
  634                 }
  635                 mtx_unlock(&bounce_lock);
  636         }
  637 
  638         vaddr = (vm_offset_t)buf;
  639         lastaddr = *lastaddrp;
  640         bmask = ~(dmat->boundary - 1);
  641 
  642         for (seg = *segp; buflen > 0 ; ) {
  643                 /*
  644                  * Get the physical address for this segment.
  645                  */
  646                 if (pmap)
  647                         curaddr = pmap_extract(pmap, vaddr);
  648                 else
  649                         curaddr = pmap_kextract(vaddr);
  650 
  651                 /*
  652                  * Compute the segment size, and adjust counts.
  653                  */
  654                 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
  655                 if (sgsize > dmat->maxsegsz)
  656                         sgsize = dmat->maxsegsz;
  657                 if (buflen < sgsize)
  658                         sgsize = buflen;
  659 
  660                 /*
  661                  * Make sure we don't cross any boundaries.
  662                  */
  663                 if (dmat->boundary > 0) {
  664                         baddr = (curaddr + dmat->boundary) & bmask;
  665                         if (sgsize > (baddr - curaddr))
  666                                 sgsize = (baddr - curaddr);
  667                 }
  668 
  669                 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
  670                         curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
  671 
  672                 /*
  673                  * Insert chunk into a segment, coalescing with
  674                  * previous segment if possible.
  675                  */
  676                 if (first) {
  677                         segs[seg].ds_addr = curaddr;
  678                         segs[seg].ds_len = sgsize;
  679                         first = 0;
  680                 } else {
  681                         if (curaddr == lastaddr &&
  682                             (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
  683                             (dmat->boundary == 0 ||
  684                              (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
  685                                 segs[seg].ds_len += sgsize;
  686                         else {
  687                                 if (++seg >= dmat->nsegments)
  688                                         break;
  689                                 segs[seg].ds_addr = curaddr;
  690                                 segs[seg].ds_len = sgsize;
  691                         }
  692                 }
  693 
  694                 lastaddr = curaddr + sgsize;
  695                 vaddr += sgsize;
  696                 buflen -= sgsize;
  697         }
  698 
  699         *segp = seg;
  700         *lastaddrp = lastaddr;
  701 
  702         /*
  703          * Did we fit?
  704          */
  705         return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
  706 }
  707 
  708 /*
  709  * Map the buffer buf into bus space using the dmamap map.
  710  */
  711 int
  712 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
  713                 bus_size_t buflen, bus_dmamap_callback_t *callback,
  714                 void *callback_arg, int flags)
  715 {
  716         bus_addr_t              lastaddr = 0;
  717         int                     error, nsegs = 0;
  718 
  719         if (map != NULL) {
  720                 flags |= BUS_DMA_WAITOK;
  721                 map->callback = callback;
  722                 map->callback_arg = callback_arg;
  723         }
  724 
  725         error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
  726              &lastaddr, dmat->segments, &nsegs, 1);
  727 
  728         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  729             __func__, dmat, dmat->flags, error, nsegs + 1);
  730 
  731         if (error == EINPROGRESS) {
  732                 return (error);
  733         }
  734 
  735         if (error)
  736                 (*callback)(callback_arg, dmat->segments, 0, error);
  737         else
  738                 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
  739 
  740         /*
  741          * Return ENOMEM to the caller so that it can pass it up the stack.
  742          * This error only happens when NOWAIT is set, so deferal is disabled.
  743          */
  744         if (error == ENOMEM)
  745                 return (error);
  746 
  747         return (0);
  748 }
  749 
  750 
  751 /*
  752  * Like _bus_dmamap_load(), but for mbufs.
  753  */
  754 int
  755 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
  756                      struct mbuf *m0,
  757                      bus_dmamap_callback2_t *callback, void *callback_arg,
  758                      int flags)
  759 {
  760         int nsegs, error;
  761 
  762         M_ASSERTPKTHDR(m0);
  763 
  764         flags |= BUS_DMA_NOWAIT;
  765         nsegs = 0;
  766         error = 0;
  767         if (m0->m_pkthdr.len <= dmat->maxsize) {
  768                 int first = 1;
  769                 bus_addr_t lastaddr = 0;
  770                 struct mbuf *m;
  771 
  772                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
  773                         if (m->m_len > 0) {
  774                                 error = _bus_dmamap_load_buffer(dmat, map,
  775                                                 m->m_data, m->m_len,
  776                                                 NULL, flags, &lastaddr,
  777                                                 dmat->segments, &nsegs, first);
  778                                 first = 0;
  779                         }
  780                 }
  781         } else {
  782                 error = EINVAL;
  783         }
  784 
  785         if (error) {
  786                 /* force "no valid mappings" in callback */
  787                 (*callback)(callback_arg, dmat->segments, 0, 0, error);
  788         } else {
  789                 (*callback)(callback_arg, dmat->segments,
  790                             nsegs+1, m0->m_pkthdr.len, error);
  791         }
  792         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  793             __func__, dmat, dmat->flags, error, nsegs + 1);
  794         return (error);
  795 }
  796 
  797 int
  798 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
  799                         struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
  800                         int flags)
  801 {
  802         int error;
  803 
  804         M_ASSERTPKTHDR(m0);
  805 
  806         flags |= BUS_DMA_NOWAIT;
  807         *nsegs = 0;
  808         error = 0;
  809         if (m0->m_pkthdr.len <= dmat->maxsize) {
  810                 int first = 1;
  811                 bus_addr_t lastaddr = 0;
  812                 struct mbuf *m;
  813 
  814                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
  815                         if (m->m_len > 0) {
  816                                 error = _bus_dmamap_load_buffer(dmat, map,
  817                                                 m->m_data, m->m_len,
  818                                                 NULL, flags, &lastaddr,
  819                                                 segs, nsegs, first);
  820                                 first = 0;
  821                         }
  822                 }
  823         } else {
  824                 error = EINVAL;
  825         }
  826 
  827         /* XXX FIXME: Having to increment nsegs is really annoying */
  828         ++*nsegs;
  829         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  830             __func__, dmat, dmat->flags, error, *nsegs);
  831         return (error);
  832 }
  833 
  834 /*
  835  * Like _bus_dmamap_load(), but for uios.
  836  */
  837 int
  838 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
  839                     struct uio *uio,
  840                     bus_dmamap_callback2_t *callback, void *callback_arg,
  841                     int flags)
  842 {
  843         bus_addr_t lastaddr = 0;
  844         int nsegs, error, first, i;
  845         bus_size_t resid;
  846         struct iovec *iov;
  847         pmap_t pmap;
  848 
  849         flags |= BUS_DMA_NOWAIT;
  850         resid = uio->uio_resid;
  851         iov = uio->uio_iov;
  852 
  853         if (uio->uio_segflg == UIO_USERSPACE) {
  854                 KASSERT(uio->uio_td != NULL,
  855                         ("bus_dmamap_load_uio: USERSPACE but no proc"));
  856                 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
  857         } else
  858                 pmap = NULL;
  859 
  860         nsegs = 0;
  861         error = 0;
  862         first = 1;
  863         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
  864                 /*
  865                  * Now at the first iovec to load.  Load each iovec
  866                  * until we have exhausted the residual count.
  867                  */
  868                 bus_size_t minlen =
  869                         resid < iov[i].iov_len ? resid : iov[i].iov_len;
  870                 caddr_t addr = (caddr_t) iov[i].iov_base;
  871 
  872                 if (minlen > 0) {
  873                         error = _bus_dmamap_load_buffer(dmat, map,
  874                                         addr, minlen, pmap, flags, &lastaddr,
  875                                         dmat->segments, &nsegs, first);
  876                         first = 0;
  877 
  878                         resid -= minlen;
  879                 }
  880         }
  881 
  882         if (error) {
  883                 /* force "no valid mappings" in callback */
  884                 (*callback)(callback_arg, dmat->segments, 0, 0, error);
  885         } else {
  886                 (*callback)(callback_arg, dmat->segments,
  887                             nsegs+1, uio->uio_resid, error);
  888         }
  889         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  890             __func__, dmat, dmat->flags, error, nsegs + 1);
  891         return (error);
  892 }
  893 
  894 /*
  895  * Release the mapping held by map.
  896  */
  897 void
  898 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
  899 {
  900         struct bounce_page *bpage;
  901 
  902         while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  903                 STAILQ_REMOVE_HEAD(&map->bpages, links);
  904                 free_bounce_page(dmat, bpage);
  905         }
  906 }
  907 
  908 void
  909 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
  910 {
  911         struct bounce_page *bpage;
  912 
  913         if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  914                 /*
  915                  * Handle data bouncing.  We might also
  916                  * want to add support for invalidating
  917                  * the caches on broken hardware
  918                  */
  919                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
  920                     "performing bounce", __func__, op, dmat, dmat->flags);
  921 
  922                 if (op & BUS_DMASYNC_PREWRITE) {
  923                         while (bpage != NULL) {
  924                                 bcopy((void *)bpage->datavaddr,
  925                                       (void *)bpage->vaddr,
  926                                       bpage->datacount);
  927                                 bpage = STAILQ_NEXT(bpage, links);
  928                         }
  929                         dmat->bounce_zone->total_bounced++;
  930                 }
  931 
  932                 if (op & BUS_DMASYNC_POSTREAD) {
  933                         while (bpage != NULL) {
  934                                 bcopy((void *)bpage->vaddr,
  935                                       (void *)bpage->datavaddr,
  936                                       bpage->datacount);
  937                                 bpage = STAILQ_NEXT(bpage, links);
  938                         }
  939                         dmat->bounce_zone->total_bounced++;
  940                 }
  941         }
  942 }
  943 
  944 static void
  945 init_bounce_pages(void *dummy __unused)
  946 {
  947 
  948         total_bpages = 0;
  949         STAILQ_INIT(&bounce_zone_list);
  950         STAILQ_INIT(&bounce_map_waitinglist);
  951         STAILQ_INIT(&bounce_map_callbacklist);
  952         mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
  953 }
  954 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
  955 
  956 static struct sysctl_ctx_list *
  957 busdma_sysctl_tree(struct bounce_zone *bz)
  958 {
  959         return (&bz->sysctl_tree);
  960 }
  961 
  962 static struct sysctl_oid *
  963 busdma_sysctl_tree_top(struct bounce_zone *bz)
  964 {
  965         return (bz->sysctl_tree_top);
  966 }
  967 
  968 static int
  969 alloc_bounce_zone(bus_dma_tag_t dmat)
  970 {
  971         struct bounce_zone *bz;
  972 
  973         /* Check to see if we already have a suitable zone */
  974         STAILQ_FOREACH(bz, &bounce_zone_list, links) {
  975                 if ((dmat->alignment <= bz->alignment)
  976                  && (dmat->lowaddr >= bz->lowaddr)) {
  977                         dmat->bounce_zone = bz;
  978                         return (0);
  979                 }
  980         }
  981 
  982         if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
  983             M_NOWAIT | M_ZERO)) == NULL)
  984                 return (ENOMEM);
  985 
  986         STAILQ_INIT(&bz->bounce_page_list);
  987         bz->free_bpages = 0;
  988         bz->reserved_bpages = 0;
  989         bz->active_bpages = 0;
  990         bz->lowaddr = dmat->lowaddr;
  991         bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
  992         snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
  993         busdma_zonecount++;
  994         snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
  995         STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
  996         dmat->bounce_zone = bz;
  997 
  998         sysctl_ctx_init(&bz->sysctl_tree);
  999         bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
 1000             SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
 1001             CTLFLAG_RD, 0, "");
 1002         if (bz->sysctl_tree_top == NULL) {
 1003                 sysctl_ctx_free(&bz->sysctl_tree);
 1004                 return (0);     /* XXX error code? */
 1005         }
 1006 
 1007         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1008             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1009             "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
 1010             "Total bounce pages");
 1011         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1012             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1013             "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
 1014             "Free bounce pages");
 1015         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1016             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1017             "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
 1018             "Reserved bounce pages");
 1019         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1020             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1021             "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
 1022             "Active bounce pages");
 1023         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1024             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1025             "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
 1026             "Total bounce requests");
 1027         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1028             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1029             "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
 1030             "Total bounce requests that were deferred");
 1031         SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
 1032             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1033             "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
 1034         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1035             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1036             "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
 1037 
 1038         return (0);
 1039 }
 1040 
 1041 static int
 1042 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
 1043 {
 1044         struct bounce_zone *bz;
 1045         int count;
 1046 
 1047         bz = dmat->bounce_zone;
 1048         count = 0;
 1049         while (numpages > 0) {
 1050                 struct bounce_page *bpage;
 1051 
 1052                 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
 1053                                                      M_NOWAIT | M_ZERO);
 1054 
 1055                 if (bpage == NULL)
 1056                         break;
 1057                 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
 1058                                                          M_NOWAIT, 0ul,
 1059                                                          bz->lowaddr,
 1060                                                          PAGE_SIZE,
 1061                                                          0);
 1062                 if (bpage->vaddr == 0) {
 1063                         free(bpage, M_DEVBUF);
 1064                         break;
 1065                 }
 1066                 bpage->busaddr = pmap_kextract(bpage->vaddr);
 1067                 mtx_lock(&bounce_lock);
 1068                 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
 1069                 total_bpages++;
 1070                 bz->total_bpages++;
 1071                 bz->free_bpages++;
 1072                 mtx_unlock(&bounce_lock);
 1073                 count++;
 1074                 numpages--;
 1075         }
 1076         return (count);
 1077 }
 1078 
 1079 static int
 1080 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
 1081 {
 1082         struct bounce_zone *bz;
 1083         int pages;
 1084 
 1085         mtx_assert(&bounce_lock, MA_OWNED);
 1086         bz = dmat->bounce_zone;
 1087         pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
 1088         if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
 1089                 return (map->pagesneeded - (map->pagesreserved + pages));
 1090         bz->free_bpages -= pages;
 1091         bz->reserved_bpages += pages;
 1092         map->pagesreserved += pages;
 1093         pages = map->pagesneeded - map->pagesreserved;
 1094 
 1095         return (pages);
 1096 }
 1097 
 1098 static bus_addr_t
 1099 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
 1100                 bus_size_t size)
 1101 {
 1102         struct bounce_zone *bz;
 1103         struct bounce_page *bpage;
 1104 
 1105         KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
 1106         KASSERT(map != NULL && map != &nobounce_dmamap,
 1107             ("add_bounce_page: bad map %p", map));
 1108 
 1109         bz = dmat->bounce_zone;
 1110         if (map->pagesneeded == 0)
 1111                 panic("add_bounce_page: map doesn't need any pages");
 1112         map->pagesneeded--;
 1113 
 1114         if (map->pagesreserved == 0)
 1115                 panic("add_bounce_page: map doesn't need any pages");
 1116         map->pagesreserved--;
 1117 
 1118         mtx_lock(&bounce_lock);
 1119         bpage = STAILQ_FIRST(&bz->bounce_page_list);
 1120         if (bpage == NULL)
 1121                 panic("add_bounce_page: free page list is empty");
 1122 
 1123         STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
 1124         bz->reserved_bpages--;
 1125         bz->active_bpages++;
 1126         mtx_unlock(&bounce_lock);
 1127 
 1128         bpage->datavaddr = vaddr;
 1129         bpage->datacount = size;
 1130         STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
 1131         return (bpage->busaddr);
 1132 }
 1133 
 1134 static void
 1135 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
 1136 {
 1137         struct bus_dmamap *map;
 1138         struct bounce_zone *bz;
 1139 
 1140         bz = dmat->bounce_zone;
 1141         bpage->datavaddr = 0;
 1142         bpage->datacount = 0;
 1143 
 1144         mtx_lock(&bounce_lock);
 1145         STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
 1146         bz->free_bpages++;
 1147         bz->active_bpages--;
 1148         if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
 1149                 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
 1150                         STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
 1151                         STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
 1152                                            map, links);
 1153                         busdma_swi_pending = 1;
 1154                         bz->total_deferred++;
 1155                         swi_sched(vm_ih, 0);
 1156                 }
 1157         }
 1158         mtx_unlock(&bounce_lock);
 1159 }
 1160 
 1161 void
 1162 busdma_swi(void)
 1163 {
 1164         bus_dma_tag_t dmat;
 1165         struct bus_dmamap *map;
 1166 
 1167         mtx_lock(&bounce_lock);
 1168         while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
 1169                 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
 1170                 mtx_unlock(&bounce_lock);
 1171                 dmat = map->dmat;
 1172                 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
 1173                 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
 1174                                 map->callback, map->callback_arg, /*flags*/0);
 1175                 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
 1176                 mtx_lock(&bounce_lock);
 1177         }
 1178         mtx_unlock(&bounce_lock);
 1179 }

Cache object: b5117226f282474dae42aaa85c0f8cd2


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