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


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

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

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

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

Cache object: d27004636dfc1a7ea80a033ab7e93d01


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