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$");
   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         /* Always enforce at least a 4GB boundary. */
  219         if (boundary == 0 || boundary > ((bus_addr_t)1 << 32))
  220                 boundary = (bus_size_t)1 << 32;
  221 
  222         /* Basic sanity checking */
  223         if (boundary != 0 && boundary < maxsegsz)
  224                 maxsegsz = boundary;
  225 
  226         if (maxsegsz == 0) {
  227                 return (EINVAL);
  228         }
  229 
  230         /* Return a NULL tag on failure */
  231         *dmat = NULL;
  232 
  233         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
  234             M_ZERO | M_NOWAIT);
  235         if (newtag == NULL) {
  236                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
  237                     __func__, newtag, 0, error);
  238                 return (ENOMEM);
  239         }
  240 
  241         newtag->parent = parent;
  242         newtag->alignment = alignment;
  243         newtag->boundary = boundary;
  244         newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
  245         newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
  246             (PAGE_SIZE - 1);
  247         newtag->filter = filter;
  248         newtag->filterarg = filterarg;
  249         newtag->maxsize = maxsize;
  250         newtag->nsegments = nsegments;
  251         newtag->maxsegsz = maxsegsz;
  252         newtag->flags = flags;
  253         newtag->ref_count = 1; /* Count ourself */
  254         newtag->map_count = 0;
  255         if (lockfunc != NULL) {
  256                 newtag->lockfunc = lockfunc;
  257                 newtag->lockfuncarg = lockfuncarg;
  258         } else {
  259                 newtag->lockfunc = dflt_lock;
  260                 newtag->lockfuncarg = NULL;
  261         }
  262         newtag->segments = NULL;
  263 
  264         /* Take into account any restrictions imposed by our parent tag */
  265         if (parent != NULL) {
  266                 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
  267                 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
  268                 if (newtag->boundary == 0)
  269                         newtag->boundary = parent->boundary;
  270                 else if (parent->boundary != 0)
  271                         newtag->boundary = MIN(parent->boundary,
  272                                                newtag->boundary);
  273                 if (newtag->filter == NULL) {
  274                         /*
  275                          * Short circuit looking at our parent directly
  276                          * since we have encapsulated all of its information
  277                          */
  278                         newtag->filter = parent->filter;
  279                         newtag->filterarg = parent->filterarg;
  280                         newtag->parent = parent->parent;
  281                 }
  282                 if (newtag->parent != NULL)
  283                         atomic_add_int(&parent->ref_count, 1);
  284         }
  285 
  286         if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
  287          || newtag->alignment > 1)
  288                 newtag->flags |= BUS_DMA_COULD_BOUNCE;
  289 
  290         if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
  291             (flags & BUS_DMA_ALLOCNOW) != 0) {
  292                 struct bounce_zone *bz;
  293 
  294                 /* Must bounce */
  295 
  296                 if ((error = alloc_bounce_zone(newtag)) != 0) {
  297                         free(newtag, M_DEVBUF);
  298                         return (error);
  299                 }
  300                 bz = newtag->bounce_zone;
  301 
  302                 if (ptoa(bz->total_bpages) < maxsize) {
  303                         int pages;
  304 
  305                         pages = atop(maxsize) - bz->total_bpages;
  306 
  307                         /* Add pages to our bounce pool */
  308                         if (alloc_bounce_pages(newtag, pages) < pages)
  309                                 error = ENOMEM;
  310                 }
  311                 /* Performed initial allocation */
  312                 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
  313         }
  314         
  315         if (error != 0) {
  316                 free(newtag, M_DEVBUF);
  317         } else {
  318                 *dmat = newtag;
  319         }
  320         CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
  321             __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
  322         return (error);
  323 }
  324 
  325 int
  326 bus_dma_tag_destroy(bus_dma_tag_t dmat)
  327 {
  328         bus_dma_tag_t dmat_copy;
  329         int error;
  330 
  331         error = 0;
  332         dmat_copy = dmat;
  333 
  334         if (dmat != NULL) {
  335 
  336                 if (dmat->map_count != 0) {
  337                         error = EBUSY;
  338                         goto out;
  339                 }
  340 
  341                 while (dmat != NULL) {
  342                         bus_dma_tag_t parent;
  343 
  344                         parent = dmat->parent;
  345                         atomic_subtract_int(&dmat->ref_count, 1);
  346                         if (dmat->ref_count == 0) {
  347                                 if (dmat->segments != NULL)
  348                                         free(dmat->segments, M_DEVBUF);
  349                                 free(dmat, M_DEVBUF);
  350                                 /*
  351                                  * Last reference count, so
  352                                  * release our reference
  353                                  * count on our parent.
  354                                  */
  355                                 dmat = parent;
  356                         } else
  357                                 dmat = NULL;
  358                 }
  359         }
  360 out:
  361         CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
  362         return (error);
  363 }
  364 
  365 /*
  366  * Allocate a handle for mapping from kva/uva/physical
  367  * address space into bus device space.
  368  */
  369 int
  370 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
  371 {
  372         int error;
  373 
  374         error = 0;
  375 
  376         if (dmat->segments == NULL) {
  377                 dmat->segments = (bus_dma_segment_t *)malloc(
  378                     sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
  379                     M_NOWAIT);
  380                 if (dmat->segments == NULL) {
  381                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  382                             __func__, dmat, ENOMEM);
  383                         return (ENOMEM);
  384                 }
  385         }
  386 
  387         /*
  388          * Bouncing might be required if the driver asks for an active
  389          * exclusion region, a data alignment that is stricter than 1, and/or
  390          * an active address boundary.
  391          */
  392         if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
  393 
  394                 /* Must bounce */
  395                 struct bounce_zone *bz;
  396                 int maxpages;
  397 
  398                 if (dmat->bounce_zone == NULL) {
  399                         if ((error = alloc_bounce_zone(dmat)) != 0)
  400                                 return (error);
  401                 }
  402                 bz = dmat->bounce_zone;
  403 
  404                 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
  405                                              M_NOWAIT | M_ZERO);
  406                 if (*mapp == NULL) {
  407                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  408                             __func__, dmat, ENOMEM);
  409                         return (ENOMEM);
  410                 }
  411 
  412                 /* Initialize the new map */
  413                 STAILQ_INIT(&((*mapp)->bpages));
  414 
  415                 /*
  416                  * Attempt to add pages to our pool on a per-instance
  417                  * basis up to a sane limit.
  418                  */
  419                 if (dmat->alignment > 1)
  420                         maxpages = MAX_BPAGES;
  421                 else
  422                         maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
  423                 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
  424                  || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
  425                         int pages;
  426 
  427                         pages = MAX(atop(dmat->maxsize), 1);
  428                         pages = MIN(maxpages - bz->total_bpages, pages);
  429                         pages = MAX(pages, 1);
  430                         if (alloc_bounce_pages(dmat, pages) < pages)
  431                                 error = ENOMEM;
  432 
  433                         if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
  434                                 if (error == 0)
  435                                         dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
  436                         } else {
  437                                 error = 0;
  438                         }
  439                 }
  440         } else {
  441                 *mapp = NULL;
  442         }
  443         if (error == 0)
  444                 dmat->map_count++;
  445         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  446             __func__, dmat, dmat->flags, error);
  447         return (error);
  448 }
  449 
  450 /*
  451  * Destroy a handle for mapping from kva/uva/physical
  452  * address space into bus device space.
  453  */
  454 int
  455 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
  456 {
  457         if (map != NULL && map != &nobounce_dmamap) {
  458                 if (STAILQ_FIRST(&map->bpages) != NULL) {
  459                         CTR3(KTR_BUSDMA, "%s: tag %p error %d",
  460                             __func__, dmat, EBUSY);
  461                         return (EBUSY);
  462                 }
  463                 free(map, M_DEVBUF);
  464         }
  465         dmat->map_count--;
  466         CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
  467         return (0);
  468 }
  469 
  470 
  471 /*
  472  * Allocate a piece of memory that can be efficiently mapped into
  473  * bus device space based on the constraints lited in the dma tag.
  474  * A dmamap to for use with dmamap_load is also allocated.
  475  */
  476 int
  477 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
  478                  bus_dmamap_t *mapp)
  479 {
  480         int mflags;
  481 
  482         if (flags & BUS_DMA_NOWAIT)
  483                 mflags = M_NOWAIT;
  484         else
  485                 mflags = M_WAITOK;
  486         if (flags & BUS_DMA_ZERO)
  487                 mflags |= M_ZERO;
  488 
  489         /* If we succeed, no mapping/bouncing will be required */
  490         *mapp = NULL;
  491 
  492         if (dmat->segments == NULL) {
  493                 dmat->segments = (bus_dma_segment_t *)malloc(
  494                     sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
  495                     M_NOWAIT);
  496                 if (dmat->segments == NULL) {
  497                         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  498                             __func__, dmat, dmat->flags, ENOMEM);
  499                         return (ENOMEM);
  500                 }
  501         }
  502 
  503         /* 
  504          * XXX:
  505          * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
  506          * alignment guarantees of malloc need to be nailed down, and the
  507          * code below should be rewritten to take that into account.
  508          *
  509          * In the meantime, we'll warn the user if malloc gets it wrong.
  510          */
  511         if ((dmat->maxsize <= PAGE_SIZE) &&
  512            (dmat->alignment < dmat->maxsize) &&
  513             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
  514                 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
  515         } else {
  516                 /*
  517                  * XXX Use Contigmalloc until it is merged into this facility
  518                  *     and handles multi-seg allocations.  Nobody is doing
  519                  *     multi-seg allocations yet though.
  520                  * XXX Certain AGP hardware does.
  521                  */
  522                 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
  523                     0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
  524                     dmat->boundary);
  525         }
  526         if (*vaddr == NULL) {
  527                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  528                     __func__, dmat, dmat->flags, ENOMEM);
  529                 return (ENOMEM);
  530         } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
  531                 printf("bus_dmamem_alloc failed to align memory properly.\n");
  532         }
  533         if (flags & BUS_DMA_NOCACHE)
  534                 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
  535                     PAT_UNCACHEABLE);
  536         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
  537             __func__, dmat, dmat->flags, ENOMEM);
  538         return (0);
  539 }
  540 
  541 /*
  542  * Free a piece of memory and it's allociated dmamap, that was allocated
  543  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
  544  */
  545 void
  546 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
  547 {
  548         /*
  549          * dmamem does not need to be bounced, so the map should be
  550          * NULL
  551          */
  552         if (map != NULL)
  553                 panic("bus_dmamem_free: Invalid map freed\n");
  554         pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
  555         if ((dmat->maxsize <= PAGE_SIZE) &&
  556            (dmat->alignment < dmat->maxsize) &&
  557             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
  558                 free(vaddr, M_DEVBUF);
  559         else {
  560                 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
  561         }
  562         CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
  563 }
  564 
  565 /*
  566  * Utility function to load a linear buffer.  lastaddrp holds state
  567  * between invocations (for multiple-buffer loads).  segp contains
  568  * the starting segment on entrace, and the ending segment on exit.
  569  * first indicates if this is the first invocation of this function.
  570  */
  571 static __inline int
  572 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
  573                         bus_dmamap_t map,
  574                         void *buf, bus_size_t buflen,
  575                         pmap_t pmap,
  576                         int flags,
  577                         bus_addr_t *lastaddrp,
  578                         bus_dma_segment_t *segs,
  579                         int *segp,
  580                         int first)
  581 {
  582         bus_size_t sgsize;
  583         bus_addr_t curaddr, lastaddr, baddr, bmask;
  584         vm_offset_t vaddr;
  585         bus_addr_t paddr;
  586         int seg;
  587 
  588         if (map == NULL)
  589                 map = &nobounce_dmamap;
  590 
  591         if ((map != &nobounce_dmamap && map->pagesneeded == 0) 
  592          && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
  593                 vm_offset_t     vendaddr;
  594 
  595                 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
  596                     "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
  597                     dmat->boundary, dmat->alignment);
  598                 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
  599                     map, &nobounce_dmamap, map->pagesneeded);
  600                 /*
  601                  * Count the number of bounce pages
  602                  * needed in order to complete this transfer
  603                  */
  604                 vaddr = (vm_offset_t)buf;
  605                 vendaddr = (vm_offset_t)buf + buflen;
  606 
  607                 while (vaddr < vendaddr) {
  608                         if (pmap)
  609                                 paddr = pmap_extract(pmap, vaddr);
  610                         else
  611                                 paddr = pmap_kextract(vaddr);
  612                         if (run_filter(dmat, paddr) != 0)
  613                                 map->pagesneeded++;
  614                         vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
  615                 }
  616                 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
  617         }
  618 
  619         /* Reserve Necessary Bounce Pages */
  620         if (map->pagesneeded != 0) {
  621                 mtx_lock(&bounce_lock);
  622                 if (flags & BUS_DMA_NOWAIT) {
  623                         if (reserve_bounce_pages(dmat, map, 0) != 0) {
  624                                 mtx_unlock(&bounce_lock);
  625                                 return (ENOMEM);
  626                         }
  627                 } else {
  628                         if (reserve_bounce_pages(dmat, map, 1) != 0) {
  629                                 /* Queue us for resources */
  630                                 map->dmat = dmat;
  631                                 map->buf = buf;
  632                                 map->buflen = buflen;
  633                                 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
  634                                     map, links);
  635                                 mtx_unlock(&bounce_lock);
  636                                 return (EINPROGRESS);
  637                         }
  638                 }
  639                 mtx_unlock(&bounce_lock);
  640         }
  641 
  642         vaddr = (vm_offset_t)buf;
  643         lastaddr = *lastaddrp;
  644         bmask = ~(dmat->boundary - 1);
  645 
  646         for (seg = *segp; buflen > 0 ; ) {
  647                 /*
  648                  * Get the physical address for this segment.
  649                  */
  650                 if (pmap)
  651                         curaddr = pmap_extract(pmap, vaddr);
  652                 else
  653                         curaddr = pmap_kextract(vaddr);
  654 
  655                 /*
  656                  * Compute the segment size, and adjust counts.
  657                  */
  658                 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
  659                 if (sgsize > dmat->maxsegsz)
  660                         sgsize = dmat->maxsegsz;
  661                 if (buflen < sgsize)
  662                         sgsize = buflen;
  663 
  664                 /*
  665                  * Make sure we don't cross any boundaries.
  666                  */
  667                 if (dmat->boundary > 0) {
  668                         baddr = (curaddr + dmat->boundary) & bmask;
  669                         if (sgsize > (baddr - curaddr))
  670                                 sgsize = (baddr - curaddr);
  671                 }
  672 
  673                 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
  674                         curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
  675 
  676                 /*
  677                  * Insert chunk into a segment, coalescing with
  678                  * previous segment if possible.
  679                  */
  680                 if (first) {
  681                         segs[seg].ds_addr = curaddr;
  682                         segs[seg].ds_len = sgsize;
  683                         first = 0;
  684                 } else {
  685                         if (curaddr == lastaddr &&
  686                             (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
  687                             (dmat->boundary == 0 ||
  688                              (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
  689                                 segs[seg].ds_len += sgsize;
  690                         else {
  691                                 if (++seg >= dmat->nsegments)
  692                                         break;
  693                                 segs[seg].ds_addr = curaddr;
  694                                 segs[seg].ds_len = sgsize;
  695                         }
  696                 }
  697 
  698                 lastaddr = curaddr + sgsize;
  699                 vaddr += sgsize;
  700                 buflen -= sgsize;
  701         }
  702 
  703         *segp = seg;
  704         *lastaddrp = lastaddr;
  705 
  706         /*
  707          * Did we fit?
  708          */
  709         return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
  710 }
  711 
  712 /*
  713  * Map the buffer buf into bus space using the dmamap map.
  714  */
  715 int
  716 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
  717                 bus_size_t buflen, bus_dmamap_callback_t *callback,
  718                 void *callback_arg, int flags)
  719 {
  720         bus_addr_t              lastaddr = 0;
  721         int                     error, nsegs = 0;
  722 
  723         if (map != NULL) {
  724                 flags |= BUS_DMA_WAITOK;
  725                 map->callback = callback;
  726                 map->callback_arg = callback_arg;
  727         }
  728 
  729         error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
  730              &lastaddr, dmat->segments, &nsegs, 1);
  731 
  732         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  733             __func__, dmat, dmat->flags, error, nsegs + 1);
  734 
  735         if (error == EINPROGRESS) {
  736                 return (error);
  737         }
  738 
  739         if (error)
  740                 (*callback)(callback_arg, dmat->segments, 0, error);
  741         else
  742                 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
  743 
  744         /*
  745          * Return ENOMEM to the caller so that it can pass it up the stack.
  746          * This error only happens when NOWAIT is set, so deferal is disabled.
  747          */
  748         if (error == ENOMEM)
  749                 return (error);
  750 
  751         return (0);
  752 }
  753 
  754 
  755 /*
  756  * Like _bus_dmamap_load(), but for mbufs.
  757  */
  758 int
  759 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
  760                      struct mbuf *m0,
  761                      bus_dmamap_callback2_t *callback, void *callback_arg,
  762                      int flags)
  763 {
  764         int nsegs, error;
  765 
  766         M_ASSERTPKTHDR(m0);
  767 
  768         flags |= BUS_DMA_NOWAIT;
  769         nsegs = 0;
  770         error = 0;
  771         if (m0->m_pkthdr.len <= dmat->maxsize) {
  772                 int first = 1;
  773                 bus_addr_t lastaddr = 0;
  774                 struct mbuf *m;
  775 
  776                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
  777                         if (m->m_len > 0) {
  778                                 error = _bus_dmamap_load_buffer(dmat, map,
  779                                                 m->m_data, m->m_len,
  780                                                 NULL, flags, &lastaddr,
  781                                                 dmat->segments, &nsegs, first);
  782                                 first = 0;
  783                         }
  784                 }
  785         } else {
  786                 error = EINVAL;
  787         }
  788 
  789         if (error) {
  790                 /* force "no valid mappings" in callback */
  791                 (*callback)(callback_arg, dmat->segments, 0, 0, error);
  792         } else {
  793                 (*callback)(callback_arg, dmat->segments,
  794                             nsegs+1, m0->m_pkthdr.len, error);
  795         }
  796         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  797             __func__, dmat, dmat->flags, error, nsegs + 1);
  798         return (error);
  799 }
  800 
  801 int
  802 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
  803                         struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
  804                         int flags)
  805 {
  806         int error;
  807 
  808         M_ASSERTPKTHDR(m0);
  809 
  810         flags |= BUS_DMA_NOWAIT;
  811         *nsegs = 0;
  812         error = 0;
  813         if (m0->m_pkthdr.len <= dmat->maxsize) {
  814                 int first = 1;
  815                 bus_addr_t lastaddr = 0;
  816                 struct mbuf *m;
  817 
  818                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
  819                         if (m->m_len > 0) {
  820                                 error = _bus_dmamap_load_buffer(dmat, map,
  821                                                 m->m_data, m->m_len,
  822                                                 NULL, flags, &lastaddr,
  823                                                 segs, nsegs, first);
  824                                 first = 0;
  825                         }
  826                 }
  827         } else {
  828                 error = EINVAL;
  829         }
  830 
  831         /* XXX FIXME: Having to increment nsegs is really annoying */
  832         ++*nsegs;
  833         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  834             __func__, dmat, dmat->flags, error, *nsegs);
  835         return (error);
  836 }
  837 
  838 /*
  839  * Like _bus_dmamap_load(), but for uios.
  840  */
  841 int
  842 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
  843                     struct uio *uio,
  844                     bus_dmamap_callback2_t *callback, void *callback_arg,
  845                     int flags)
  846 {
  847         bus_addr_t lastaddr = 0;
  848         int nsegs, error, first, i;
  849         bus_size_t resid;
  850         struct iovec *iov;
  851         pmap_t pmap;
  852 
  853         flags |= BUS_DMA_NOWAIT;
  854         resid = uio->uio_resid;
  855         iov = uio->uio_iov;
  856 
  857         if (uio->uio_segflg == UIO_USERSPACE) {
  858                 KASSERT(uio->uio_td != NULL,
  859                         ("bus_dmamap_load_uio: USERSPACE but no proc"));
  860                 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
  861         } else
  862                 pmap = NULL;
  863 
  864         nsegs = 0;
  865         error = 0;
  866         first = 1;
  867         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
  868                 /*
  869                  * Now at the first iovec to load.  Load each iovec
  870                  * until we have exhausted the residual count.
  871                  */
  872                 bus_size_t minlen =
  873                         resid < iov[i].iov_len ? resid : iov[i].iov_len;
  874                 caddr_t addr = (caddr_t) iov[i].iov_base;
  875 
  876                 if (minlen > 0) {
  877                         error = _bus_dmamap_load_buffer(dmat, map,
  878                                         addr, minlen, pmap, flags, &lastaddr,
  879                                         dmat->segments, &nsegs, first);
  880                         first = 0;
  881 
  882                         resid -= minlen;
  883                 }
  884         }
  885 
  886         if (error) {
  887                 /* force "no valid mappings" in callback */
  888                 (*callback)(callback_arg, dmat->segments, 0, 0, error);
  889         } else {
  890                 (*callback)(callback_arg, dmat->segments,
  891                             nsegs+1, uio->uio_resid, error);
  892         }
  893         CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
  894             __func__, dmat, dmat->flags, error, nsegs + 1);
  895         return (error);
  896 }
  897 
  898 /*
  899  * Release the mapping held by map.
  900  */
  901 void
  902 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
  903 {
  904         struct bounce_page *bpage;
  905 
  906         while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  907                 STAILQ_REMOVE_HEAD(&map->bpages, links);
  908                 free_bounce_page(dmat, bpage);
  909         }
  910 }
  911 
  912 void
  913 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
  914 {
  915         struct bounce_page *bpage;
  916 
  917         if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  918                 /*
  919                  * Handle data bouncing.  We might also
  920                  * want to add support for invalidating
  921                  * the caches on broken hardware
  922                  */
  923                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
  924                     "performing bounce", __func__, op, dmat, dmat->flags);
  925 
  926                 if (op & BUS_DMASYNC_PREWRITE) {
  927                         while (bpage != NULL) {
  928                                 bcopy((void *)bpage->datavaddr,
  929                                       (void *)bpage->vaddr,
  930                                       bpage->datacount);
  931                                 bpage = STAILQ_NEXT(bpage, links);
  932                         }
  933                         dmat->bounce_zone->total_bounced++;
  934                 }
  935 
  936                 if (op & BUS_DMASYNC_POSTREAD) {
  937                         while (bpage != NULL) {
  938                                 bcopy((void *)bpage->vaddr,
  939                                       (void *)bpage->datavaddr,
  940                                       bpage->datacount);
  941                                 bpage = STAILQ_NEXT(bpage, links);
  942                         }
  943                         dmat->bounce_zone->total_bounced++;
  944                 }
  945         }
  946 }
  947 
  948 static void
  949 init_bounce_pages(void *dummy __unused)
  950 {
  951 
  952         total_bpages = 0;
  953         STAILQ_INIT(&bounce_zone_list);
  954         STAILQ_INIT(&bounce_map_waitinglist);
  955         STAILQ_INIT(&bounce_map_callbacklist);
  956         mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
  957 }
  958 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
  959 
  960 static struct sysctl_ctx_list *
  961 busdma_sysctl_tree(struct bounce_zone *bz)
  962 {
  963         return (&bz->sysctl_tree);
  964 }
  965 
  966 static struct sysctl_oid *
  967 busdma_sysctl_tree_top(struct bounce_zone *bz)
  968 {
  969         return (bz->sysctl_tree_top);
  970 }
  971 
  972 static int
  973 alloc_bounce_zone(bus_dma_tag_t dmat)
  974 {
  975         struct bounce_zone *bz;
  976 
  977         /* Check to see if we already have a suitable zone */
  978         STAILQ_FOREACH(bz, &bounce_zone_list, links) {
  979                 if ((dmat->alignment <= bz->alignment)
  980                  && (dmat->lowaddr >= bz->lowaddr)) {
  981                         dmat->bounce_zone = bz;
  982                         return (0);
  983                 }
  984         }
  985 
  986         if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
  987             M_NOWAIT | M_ZERO)) == NULL)
  988                 return (ENOMEM);
  989 
  990         STAILQ_INIT(&bz->bounce_page_list);
  991         bz->free_bpages = 0;
  992         bz->reserved_bpages = 0;
  993         bz->active_bpages = 0;
  994         bz->lowaddr = dmat->lowaddr;
  995         bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
  996         snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
  997         busdma_zonecount++;
  998         snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
  999         STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
 1000         dmat->bounce_zone = bz;
 1001 
 1002         sysctl_ctx_init(&bz->sysctl_tree);
 1003         bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
 1004             SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
 1005             CTLFLAG_RD, 0, "");
 1006         if (bz->sysctl_tree_top == NULL) {
 1007                 sysctl_ctx_free(&bz->sysctl_tree);
 1008                 return (0);     /* XXX error code? */
 1009         }
 1010 
 1011         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1012             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1013             "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
 1014             "Total bounce pages");
 1015         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1016             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1017             "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
 1018             "Free bounce pages");
 1019         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1020             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1021             "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
 1022             "Reserved bounce pages");
 1023         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1024             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1025             "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
 1026             "Active bounce pages");
 1027         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1028             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1029             "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
 1030             "Total bounce requests");
 1031         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1032             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1033             "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
 1034             "Total bounce requests that were deferred");
 1035         SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
 1036             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1037             "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
 1038         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
 1039             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
 1040             "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
 1041 
 1042         return (0);
 1043 }
 1044 
 1045 static int
 1046 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
 1047 {
 1048         struct bounce_zone *bz;
 1049         int count;
 1050 
 1051         bz = dmat->bounce_zone;
 1052         count = 0;
 1053         while (numpages > 0) {
 1054                 struct bounce_page *bpage;
 1055 
 1056                 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
 1057                                                      M_NOWAIT | M_ZERO);
 1058 
 1059                 if (bpage == NULL)
 1060                         break;
 1061                 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
 1062                                                          M_NOWAIT, 0ul,
 1063                                                          bz->lowaddr,
 1064                                                          PAGE_SIZE,
 1065                                                          0);
 1066                 if (bpage->vaddr == 0) {
 1067                         free(bpage, M_DEVBUF);
 1068                         break;
 1069                 }
 1070                 bpage->busaddr = pmap_kextract(bpage->vaddr);
 1071                 mtx_lock(&bounce_lock);
 1072                 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
 1073                 total_bpages++;
 1074                 bz->total_bpages++;
 1075                 bz->free_bpages++;
 1076                 mtx_unlock(&bounce_lock);
 1077                 count++;
 1078                 numpages--;
 1079         }
 1080         return (count);
 1081 }
 1082 
 1083 static int
 1084 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
 1085 {
 1086         struct bounce_zone *bz;
 1087         int pages;
 1088 
 1089         mtx_assert(&bounce_lock, MA_OWNED);
 1090         bz = dmat->bounce_zone;
 1091         pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
 1092         if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
 1093                 return (map->pagesneeded - (map->pagesreserved + pages));
 1094         bz->free_bpages -= pages;
 1095         bz->reserved_bpages += pages;
 1096         map->pagesreserved += pages;
 1097         pages = map->pagesneeded - map->pagesreserved;
 1098 
 1099         return (pages);
 1100 }
 1101 
 1102 static bus_addr_t
 1103 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
 1104                 bus_size_t size)
 1105 {
 1106         struct bounce_zone *bz;
 1107         struct bounce_page *bpage;
 1108 
 1109         KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
 1110         KASSERT(map != NULL && map != &nobounce_dmamap,
 1111             ("add_bounce_page: bad map %p", map));
 1112 
 1113         bz = dmat->bounce_zone;
 1114         if (map->pagesneeded == 0)
 1115                 panic("add_bounce_page: map doesn't need any pages");
 1116         map->pagesneeded--;
 1117 
 1118         if (map->pagesreserved == 0)
 1119                 panic("add_bounce_page: map doesn't need any pages");
 1120         map->pagesreserved--;
 1121 
 1122         mtx_lock(&bounce_lock);
 1123         bpage = STAILQ_FIRST(&bz->bounce_page_list);
 1124         if (bpage == NULL)
 1125                 panic("add_bounce_page: free page list is empty");
 1126 
 1127         STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
 1128         bz->reserved_bpages--;
 1129         bz->active_bpages++;
 1130         mtx_unlock(&bounce_lock);
 1131 
 1132         bpage->datavaddr = vaddr;
 1133         bpage->datacount = size;
 1134         STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
 1135         return (bpage->busaddr);
 1136 }
 1137 
 1138 static void
 1139 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
 1140 {
 1141         struct bus_dmamap *map;
 1142         struct bounce_zone *bz;
 1143 
 1144         bz = dmat->bounce_zone;
 1145         bpage->datavaddr = 0;
 1146         bpage->datacount = 0;
 1147 
 1148         mtx_lock(&bounce_lock);
 1149         STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
 1150         bz->free_bpages++;
 1151         bz->active_bpages--;
 1152         if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
 1153                 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
 1154                         STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
 1155                         STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
 1156                                            map, links);
 1157                         busdma_swi_pending = 1;
 1158                         bz->total_deferred++;
 1159                         swi_sched(vm_ih, 0);
 1160                 }
 1161         }
 1162         mtx_unlock(&bounce_lock);
 1163 }
 1164 
 1165 void
 1166 busdma_swi(void)
 1167 {
 1168         bus_dma_tag_t dmat;
 1169         struct bus_dmamap *map;
 1170 
 1171         mtx_lock(&bounce_lock);
 1172         while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
 1173                 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
 1174                 mtx_unlock(&bounce_lock);
 1175                 dmat = map->dmat;
 1176                 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
 1177                 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
 1178                                 map->callback, map->callback_arg, /*flags*/0);
 1179                 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
 1180                 mtx_lock(&bounce_lock);
 1181         }
 1182         mtx_unlock(&bounce_lock);
 1183 }

Cache object: db52a4753dbb95e32249755d3a10d88d


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