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


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

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

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

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

Cache object: 62e11b6425de42209e48a3d1bf5d3d66


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