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

Cache object: 82fd938b6bd61307659b10ea761b69b0


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