The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/busdma_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 0dab658facdcf703d102288c697834e1


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