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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: d22e1839350eddd4d6be262a59fee752


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