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

Cache object: 268bacdda57c561e14e1fbb870e4a9f4


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