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

Cache object: c16ccd247184b80b0970115ce4fb106c


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