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

Cache object: f29084413e56cf0e40ea873b31d32fa7


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