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  * $FreeBSD$
   27  */
   28 
   29 #include <sys/param.h>
   30 #include <sys/systm.h>
   31 #include <sys/malloc.h>
   32 #include <sys/mbuf.h>
   33 #include <sys/uio.h>
   34 
   35 #include <vm/vm.h>
   36 #include <vm/vm_page.h>
   37 
   38 /* XXX needed for to access pmap to convert per-proc virtual to physical */
   39 #include <sys/proc.h>
   40 #include <sys/lock.h>
   41 #include <vm/vm_map.h>
   42 
   43 #include <machine/bus.h>
   44 #include <machine/md_var.h>
   45 
   46 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
   47 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
   48 #define MAX_BPAGES 128
   49 
   50 struct bus_dma_tag {
   51         bus_dma_tag_t     parent;
   52         bus_size_t        alignment;
   53         bus_size_t        boundary;
   54         bus_addr_t        lowaddr;
   55         bus_addr_t        highaddr;
   56         bus_dma_filter_t *filter;
   57         void             *filterarg;
   58         bus_size_t        maxsize;
   59         u_int             nsegments;
   60         bus_size_t        maxsegsz;
   61         int               flags;
   62         int               ref_count;
   63         int               map_count;
   64 };
   65 
   66 struct bounce_page {
   67         vm_offset_t     vaddr;          /* kva of bounce buffer */
   68         bus_addr_t      busaddr;        /* Physical address */
   69         vm_offset_t     datavaddr;      /* kva of client data */
   70         bus_size_t      datacount;      /* client data count */
   71         STAILQ_ENTRY(bounce_page) links;
   72 };
   73 
   74 int busdma_swi_pending;
   75 
   76 static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
   77 static int free_bpages;
   78 static int reserved_bpages;
   79 static int active_bpages;
   80 static int total_bpages;
   81 static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
   82 
   83 struct bus_dmamap {
   84         struct bp_list         bpages;
   85         int                    pagesneeded;
   86         int                    pagesreserved;
   87         bus_dma_tag_t          dmat;
   88         void                  *buf;             /* unmapped buffer pointer */
   89         bus_size_t             buflen;          /* unmapped buffer length */
   90         bus_dmamap_callback_t *callback;
   91         void                  *callback_arg;
   92         STAILQ_ENTRY(bus_dmamap) links;
   93 };
   94 
   95 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
   96 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
   97 static struct bus_dmamap nobounce_dmamap;
   98 
   99 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
  100 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map);
  101 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
  102                                    vm_offset_t vaddr, bus_size_t size);
  103 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
  104 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
  105 
  106 static __inline int
  107 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
  108 {
  109         int retval;
  110 
  111         retval = 0;
  112         do {
  113                 if (paddr > dmat->lowaddr
  114                  && paddr <= dmat->highaddr
  115                  && (dmat->filter == NULL
  116                   || (*dmat->filter)(dmat->filterarg, paddr) != 0))
  117                         retval = 1;
  118 
  119                 dmat = dmat->parent;            
  120         } while (retval == 0 && dmat != NULL);
  121         return (retval);
  122 }
  123 
  124 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
  125 /*
  126  * Allocate a device specific dma_tag.
  127  */
  128 int
  129 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
  130                    bus_size_t boundary, bus_addr_t lowaddr,
  131                    bus_addr_t highaddr, bus_dma_filter_t *filter,
  132                    void *filterarg, bus_size_t maxsize, int nsegments,
  133                    bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
  134 {
  135         bus_dma_tag_t newtag;
  136         int error = 0;
  137 
  138         /* Return a NULL tag on failure */
  139         *dmat = NULL;
  140 
  141         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
  142         if (newtag == NULL)
  143                 return (ENOMEM);
  144 
  145         newtag->parent = parent;
  146         newtag->alignment = alignment;
  147         newtag->boundary = boundary;
  148         newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
  149         newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
  150         newtag->filter = filter;
  151         newtag->filterarg = filterarg;
  152         newtag->maxsize = maxsize;
  153         newtag->nsegments = nsegments;
  154         newtag->maxsegsz = maxsegsz;
  155         newtag->flags = flags;
  156         newtag->ref_count = 1; /* Count ourself */
  157         newtag->map_count = 0;
  158         
  159         /* Take into account any restrictions imposed by our parent tag */
  160         if (parent != NULL) {
  161                 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
  162                 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
  163                 /*
  164                  * XXX Not really correct??? Probably need to honor boundary
  165                  *     all the way up the inheritence chain.
  166                  */
  167                 newtag->boundary = MAX(parent->boundary, newtag->boundary);
  168                 if (newtag->filter == NULL) {
  169                         /*
  170                          * Short circuit looking at our parent directly
  171                          * since we have encapsulated all of its information
  172                          */
  173                         newtag->filter = parent->filter;
  174                         newtag->filterarg = parent->filterarg;
  175                         newtag->parent = parent->parent;
  176                 }
  177                 if (newtag->parent != NULL) {
  178                         parent->ref_count++;
  179                 }
  180         }
  181         
  182         if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem) &&
  183             (flags & BUS_DMA_ALLOCNOW) != 0) {
  184                 /* Must bounce */
  185 
  186                 if (lowaddr > bounce_lowaddr) {
  187                         /*
  188                          * Go through the pool and kill any pages
  189                          * that don't reside below lowaddr.
  190                          */
  191                         panic("bus_dma_tag_create: page reallocation "
  192                               "not implemented");
  193                 }
  194                 if (ptoa(total_bpages) < maxsize) {
  195                         int pages;
  196 
  197                         pages = atop(maxsize) - total_bpages;
  198 
  199                         /* Add pages to our bounce pool */
  200                         if (alloc_bounce_pages(newtag, pages) < pages)
  201                                 error = ENOMEM;
  202                 }
  203                 /* Performed initial allocation */
  204                 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
  205         }
  206         
  207         if (error != 0) {
  208                 free(newtag, M_DEVBUF);
  209         } else {
  210                 *dmat = newtag;
  211         }
  212         return (error);
  213 }
  214 
  215 int
  216 bus_dma_tag_destroy(bus_dma_tag_t dmat)
  217 {
  218         if (dmat != NULL) {
  219 
  220                 if (dmat->map_count != 0)
  221                         return (EBUSY);
  222 
  223                 while (dmat != NULL) {
  224                         bus_dma_tag_t parent;
  225 
  226                         parent = dmat->parent;
  227                         dmat->ref_count--;
  228                         if (dmat->ref_count == 0) {
  229                                 free(dmat, M_DEVBUF);
  230                                 /*
  231                                  * Last reference count, so
  232                                  * release our reference
  233                                  * count on our parent.
  234                                  */
  235                                 dmat = parent;
  236                         } else
  237                                 dmat = NULL;
  238                 }
  239         }
  240         return (0);
  241 }
  242 
  243 /*
  244  * Allocate a handle for mapping from kva/uva/physical
  245  * address space into bus device space.
  246  */
  247 int
  248 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
  249 {
  250         int error;
  251 
  252         error = 0;
  253 
  254         if (dmat->lowaddr < ptoa((vm_paddr_t)Maxmem)) {
  255                 /* Must bounce */
  256                 int maxpages;
  257 
  258                 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
  259                                              M_NOWAIT);
  260                 if (*mapp == NULL) {
  261                         return (ENOMEM);
  262                 } else {
  263                         /* Initialize the new map */
  264                         bzero(*mapp, sizeof(**mapp));
  265                         STAILQ_INIT(&((*mapp)->bpages));
  266                 }
  267                 /*
  268                  * Attempt to add pages to our pool on a per-instance
  269                  * basis up to a sane limit.
  270                  */
  271                 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
  272                 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
  273                  || (dmat->map_count > 0
  274                   && total_bpages < maxpages)) {
  275                         int pages;
  276 
  277                         if (dmat->lowaddr > bounce_lowaddr) {
  278                                 /*
  279                                  * Go through the pool and kill any pages
  280                                  * that don't reside below lowaddr.
  281                                  */
  282                                 panic("bus_dmamap_create: page reallocation "
  283                                       "not implemented");
  284                         }
  285                         pages = atop(dmat->maxsize);
  286                         pages = MIN(maxpages - total_bpages, pages);
  287                         if (alloc_bounce_pages(dmat, pages) < pages)
  288                                 error = ENOMEM;
  289 
  290                         if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
  291                                 if (error == 0)
  292                                         dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
  293                         } else {
  294                                 error = 0;
  295                         }
  296                 }
  297         } else {
  298                 *mapp = NULL;
  299         }
  300         if (error == 0)
  301                 dmat->map_count++;
  302         return (error);
  303 }
  304 
  305 /*
  306  * Destroy a handle for mapping from kva/uva/physical
  307  * address space into bus device space.
  308  */
  309 int
  310 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
  311 {
  312         if (map != NULL) {
  313                 if (STAILQ_FIRST(&map->bpages) != NULL)
  314                         return (EBUSY);
  315                 free(map, M_DEVBUF);
  316         }
  317         dmat->map_count--;
  318         return (0);
  319 }
  320 
  321 
  322 /*
  323  * Allocate a piece of memory that can be efficiently mapped into
  324  * bus device space based on the constraints lited in the dma tag.
  325  * A dmamap to for use with dmamap_load is also allocated.
  326  */
  327 int
  328 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
  329                  bus_dmamap_t *mapp)
  330 {
  331         int mflags;
  332 
  333         if (flags & BUS_DMA_NOWAIT)
  334                 mflags = M_NOWAIT;
  335         else
  336                 mflags = M_WAITOK;
  337         if (flags & BUS_DMA_ZERO)
  338                 mflags |= M_ZERO;
  339 
  340         /* If we succeed, no mapping/bouncing will be required */
  341         *mapp = NULL;
  342 
  343         if ((dmat->maxsize <= PAGE_SIZE) &&
  344             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
  345                 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
  346         } else {
  347                 /*
  348                  * XXX Use Contigmalloc until it is merged into this facility
  349                  *     and handles multi-seg allocations.  Nobody is doing
  350                  *     multi-seg allocations yet though.
  351                  */
  352                 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
  353                     0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
  354                     dmat->boundary);
  355         }
  356         if (*vaddr == NULL)
  357                 return (ENOMEM);
  358         return (0);
  359 }
  360 
  361 /*
  362  * Free a piece of memory and it's allociated dmamap, that was allocated
  363  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
  364  */
  365 void
  366 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
  367 {
  368         /*
  369          * dmamem does not need to be bounced, so the map should be
  370          * NULL
  371          */
  372         if (map != NULL)
  373                 panic("bus_dmamem_free: Invalid map freed\n");
  374         if ((dmat->maxsize <= PAGE_SIZE) &&
  375             dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
  376                 free(vaddr, M_DEVBUF);
  377         else
  378                 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
  379 }
  380 
  381 #define BUS_DMAMAP_NSEGS ((BUS_SPACE_MAXSIZE / PAGE_SIZE) + 1)
  382 
  383 /*
  384  * Map the buffer buf into bus space using the dmamap map.
  385  */
  386 int
  387 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
  388                 bus_size_t buflen, bus_dmamap_callback_t *callback,
  389                 void *callback_arg, int flags)
  390 {
  391         vm_offset_t             vaddr;
  392         vm_paddr_t              paddr;
  393 #ifdef __GNUC__
  394         bus_dma_segment_t       dm_segments[dmat->nsegments];
  395 #else
  396         bus_dma_segment_t       dm_segments[BUS_DMAMAP_NSEGS];
  397 #endif
  398         bus_dma_segment_t      *sg;
  399         int                     seg;
  400         int                     error;
  401         vm_paddr_t              nextpaddr;
  402 
  403         if (map == NULL)
  404                 map = &nobounce_dmamap;
  405 
  406         error = 0;
  407         /*
  408          * If we are being called during a callback, pagesneeded will
  409          * be non-zero, so we can avoid doing the work twice.
  410          */
  411         if (dmat->lowaddr < ptoa((vm_paddr_t)Maxmem) &&
  412             map->pagesneeded == 0) {
  413                 vm_offset_t     vendaddr;
  414 
  415                 /*
  416                  * Count the number of bounce pages
  417                  * needed in order to complete this transfer
  418                  */
  419                 vaddr = trunc_page((vm_offset_t)buf);
  420                 vendaddr = (vm_offset_t)buf + buflen;
  421 
  422                 while (vaddr < vendaddr) {
  423                         paddr = pmap_kextract(vaddr);
  424                         if (run_filter(dmat, paddr) != 0) {
  425 
  426                                 map->pagesneeded++;
  427                         }
  428                         vaddr += PAGE_SIZE;
  429                 }
  430         }
  431 
  432         /* Reserve Necessary Bounce Pages */
  433         if (map->pagesneeded != 0) {
  434                 int s;
  435 
  436                 s = splhigh();
  437                 if (reserve_bounce_pages(dmat, map) != 0) {
  438 
  439                         /* Queue us for resources */
  440                         map->dmat = dmat;
  441                         map->buf = buf;
  442                         map->buflen = buflen;
  443                         map->callback = callback;
  444                         map->callback_arg = callback_arg;
  445 
  446                         STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
  447                         splx(s);
  448 
  449                         return (EINPROGRESS);
  450                 }
  451                 splx(s);
  452         }
  453 
  454         vaddr = (vm_offset_t)buf;
  455         sg = &dm_segments[0];
  456         seg = 1;
  457         sg->ds_len = 0;
  458 
  459         nextpaddr = 0;
  460         do {
  461                 bus_size_t      size;
  462 
  463                 paddr = pmap_kextract(vaddr);
  464                 size = PAGE_SIZE - (paddr & PAGE_MASK);
  465                 if (size > buflen)
  466                         size = buflen;
  467 
  468                 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
  469                         paddr = add_bounce_page(dmat, map, vaddr, size);
  470                 }
  471 
  472                 if (sg->ds_len == 0) {
  473                         sg->ds_addr = paddr;
  474                         sg->ds_len = size;
  475                 } else if (paddr == nextpaddr) {
  476                         sg->ds_len += size;
  477                 } else {
  478                         /* Go to the next segment */
  479                         sg++;
  480                         seg++;
  481                         if (seg > dmat->nsegments)
  482                                 break;
  483                         sg->ds_addr = paddr;
  484                         sg->ds_len = size;
  485                 }
  486                 vaddr += size;
  487                 nextpaddr = paddr + size;
  488                 buflen -= size;
  489 
  490         } while (buflen > 0);
  491 
  492         if (buflen != 0) {
  493                 printf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
  494                        (u_long)buflen);
  495                 error = EFBIG;
  496         }
  497 
  498         (*callback)(callback_arg, dm_segments, seg, error);
  499 
  500         return (0);
  501 }
  502 
  503 /*
  504  * Utility function to load a linear buffer.  lastaddrp holds state
  505  * between invocations (for multiple-buffer loads).  segp contains
  506  * the starting segment on entrace, and the ending segment on exit.
  507  * first indicates if this is the first invocation of this function.
  508  */
  509 static int
  510 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
  511                         bus_dma_segment_t segs[],
  512                         void *buf, bus_size_t buflen,
  513                         struct proc *p,
  514                         int flags,
  515                         vm_offset_t *lastaddrp,
  516                         int *segp,
  517                         int first)
  518 {
  519         bus_size_t sgsize;
  520         bus_addr_t curaddr, lastaddr, baddr, bmask;
  521         vm_offset_t vaddr = (vm_offset_t)buf;
  522         int seg;
  523         pmap_t pmap;
  524 
  525         if (p != NULL)
  526                 pmap = vmspace_pmap(p->p_vmspace);
  527         else
  528                 pmap = NULL;
  529 
  530         lastaddr = *lastaddrp;
  531         bmask  = ~(dmat->boundary - 1);
  532 
  533         for (seg = *segp; buflen > 0 ; ) {
  534                 /*
  535                  * Get the physical address for this segment.
  536                  */
  537                 if (pmap)
  538                         curaddr = pmap_extract(pmap, vaddr);
  539                 else
  540                         curaddr = pmap_kextract(vaddr);
  541 
  542                 /*
  543                  * Compute the segment size, and adjust counts.
  544                  */
  545                 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
  546                 if (buflen < sgsize)
  547                         sgsize = buflen;
  548 
  549                 /*
  550                  * Make sure we don't cross any boundaries.
  551                  */
  552                 if (dmat->boundary > 0) {
  553                         baddr = (curaddr + dmat->boundary) & bmask;
  554                         if (sgsize > (baddr - curaddr))
  555                                 sgsize = (baddr - curaddr);
  556                 }
  557 
  558                 /*
  559                  * Insert chunk into a segment, coalescing with
  560                  * previous segment if possible.
  561                  */
  562                 if (first) {
  563                         segs[seg].ds_addr = curaddr;
  564                         segs[seg].ds_len = sgsize;
  565                         first = 0;
  566                 } else {
  567                         if (curaddr == lastaddr &&
  568                             (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
  569                             (dmat->boundary == 0 ||
  570                              (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
  571                                 segs[seg].ds_len += sgsize;
  572                         else {
  573                                 if (++seg >= dmat->nsegments)
  574                                         break;
  575                                 segs[seg].ds_addr = curaddr;
  576                                 segs[seg].ds_len = sgsize;
  577                         }
  578                 }
  579 
  580                 lastaddr = curaddr + sgsize;
  581                 vaddr += sgsize;
  582                 buflen -= sgsize;
  583         }
  584 
  585         *segp = seg;
  586         *lastaddrp = lastaddr;
  587 
  588         /*
  589          * Did we fit?
  590          */
  591         return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
  592 }
  593 
  594 /*
  595  * Like _bus_dmamap_load(), but for mbufs.
  596  */
  597 int
  598 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
  599                      struct mbuf *m0,
  600                      bus_dmamap_callback2_t *callback, void *callback_arg,
  601                      int flags)
  602 {
  603 #ifdef __GNUC__
  604         bus_dma_segment_t dm_segments[dmat->nsegments];
  605 #else
  606         bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
  607 #endif
  608         int nsegs, error;
  609 
  610         KASSERT(dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem) || map != NULL,
  611                 ("bus_dmamap_load_mbuf: No support for bounce pages!"));
  612         KASSERT(m0->m_flags & M_PKTHDR,
  613                 ("bus_dmamap_load_mbuf: no packet header"));
  614 
  615         nsegs = 0;
  616         error = 0;
  617         if (m0->m_pkthdr.len <= dmat->maxsize) {
  618                 int first = 1;
  619                 vm_offset_t lastaddr = 0;
  620                 struct mbuf *m;
  621 
  622                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
  623                         error = _bus_dmamap_load_buffer(dmat,
  624                                         dm_segments,
  625                                         m->m_data, m->m_len,
  626                                         NULL, flags, &lastaddr, &nsegs, first);
  627                         first = 0;
  628                 }
  629         } else {
  630                 error = EINVAL;
  631         }
  632 
  633         if (error) {
  634                 /* force "no valid mappings" in callback */
  635                 (*callback)(callback_arg, dm_segments, 0, 0, error);
  636         } else {
  637                 (*callback)(callback_arg, dm_segments,
  638                             nsegs+1, m0->m_pkthdr.len, error);
  639         }
  640         return (error);
  641 }
  642 
  643 /*
  644  * Like _bus_dmamap_load(), but for uios.
  645  */
  646 int
  647 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
  648                     struct uio *uio,
  649                     bus_dmamap_callback2_t *callback, void *callback_arg,
  650                     int flags)
  651 {
  652         vm_offset_t lastaddr;
  653 #ifdef __GNUC__
  654         bus_dma_segment_t dm_segments[dmat->nsegments];
  655 #else
  656         bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
  657 #endif
  658         int nsegs, error, first, i;
  659         bus_size_t resid;
  660         struct iovec *iov;
  661         struct proc *p = NULL;
  662 
  663         KASSERT(dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem) || map != NULL,
  664                 ("bus_dmamap_load_uio: No support for bounce pages!"));
  665 
  666         resid = uio->uio_resid;
  667         iov = uio->uio_iov;
  668 
  669         if (uio->uio_segflg == UIO_USERSPACE) {
  670                 p = uio->uio_procp;
  671                 KASSERT(p != NULL,
  672                         ("bus_dmamap_load_uio: USERSPACE but no proc"));
  673         }
  674 
  675         nsegs = 0;
  676         error = 0;
  677         first = 1;
  678         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
  679                 /*
  680                  * Now at the first iovec to load.  Load each iovec
  681                  * until we have exhausted the residual count.
  682                  */
  683                 bus_size_t minlen =
  684                         resid < iov[i].iov_len ? resid : iov[i].iov_len;
  685                 caddr_t addr = (caddr_t) iov[i].iov_base;
  686 
  687                 error = _bus_dmamap_load_buffer(dmat,
  688                                 dm_segments,
  689                                 addr, minlen,
  690                                 p, flags, &lastaddr, &nsegs, first);
  691                 first = 0;
  692 
  693                 resid -= minlen;
  694         }
  695 
  696         if (error) {
  697                 /* force "no valid mappings" in callback */
  698                 (*callback)(callback_arg, dm_segments, 0, 0, error);
  699         } else {
  700                 (*callback)(callback_arg, dm_segments,
  701                             nsegs+1, uio->uio_resid, error);
  702         }
  703         return (error);
  704 }
  705 
  706 /*
  707  * Release the mapping held by map.
  708  */
  709 void
  710 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
  711 {
  712         struct bounce_page *bpage;
  713 
  714         while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  715                 STAILQ_REMOVE_HEAD(&map->bpages, links);
  716                 free_bounce_page(dmat, bpage);
  717         }
  718 }
  719 
  720 void
  721 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
  722 {
  723         struct bounce_page *bpage;
  724 
  725         if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
  726                 
  727                 /*
  728                  * Handle data bouncing.  We might also
  729                  * want to add support for invalidating
  730                  * the caches on broken hardware
  731                  */
  732                 switch (op) {
  733                 case BUS_DMASYNC_PREWRITE:
  734                         while (bpage != NULL) {
  735                                 bcopy((void *)bpage->datavaddr,
  736                                       (void *)bpage->vaddr,
  737                                       bpage->datacount);
  738                                 bpage = STAILQ_NEXT(bpage, links);
  739                         }
  740                         break;
  741 
  742                 case BUS_DMASYNC_POSTREAD:
  743                         while (bpage != NULL) {
  744                                 bcopy((void *)bpage->vaddr,
  745                                       (void *)bpage->datavaddr,
  746                                       bpage->datacount);
  747                                 bpage = STAILQ_NEXT(bpage, links);
  748                         }
  749                         break;
  750                 case BUS_DMASYNC_PREREAD:
  751                 case BUS_DMASYNC_POSTWRITE:
  752                         /* No-ops */
  753                         break;
  754                 }
  755         }
  756 }
  757 
  758 static int
  759 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
  760 {
  761         int count;
  762 
  763         count = 0;
  764         if (total_bpages == 0) {
  765                 STAILQ_INIT(&bounce_page_list);
  766                 STAILQ_INIT(&bounce_map_waitinglist);
  767                 STAILQ_INIT(&bounce_map_callbacklist);
  768         }
  769         
  770         while (numpages > 0) {
  771                 struct bounce_page *bpage;
  772                 int s;
  773 
  774                 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
  775                                                      M_NOWAIT);
  776 
  777                 if (bpage == NULL)
  778                         break;
  779                 bzero(bpage, sizeof(*bpage));
  780                 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
  781                                                          M_NOWAIT, 0ul,
  782                                                          dmat->lowaddr,
  783                                                          PAGE_SIZE,
  784                                                          0);
  785                 if (bpage->vaddr == NULL) {
  786                         free(bpage, M_DEVBUF);
  787                         break;
  788                 }
  789                 bpage->busaddr = pmap_kextract(bpage->vaddr);
  790                 s = splhigh();
  791                 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
  792                 total_bpages++;
  793                 free_bpages++;
  794                 splx(s);
  795                 count++;
  796                 numpages--;
  797         }
  798         return (count);
  799 }
  800 
  801 static int
  802 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
  803 {
  804         int pages;
  805 
  806         pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
  807         free_bpages -= pages;
  808         reserved_bpages += pages;
  809         map->pagesreserved += pages;
  810         pages = map->pagesneeded - map->pagesreserved;
  811 
  812         return (pages);
  813 }
  814 
  815 static bus_addr_t
  816 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
  817                 bus_size_t size)
  818 {
  819         int s;
  820         struct bounce_page *bpage;
  821 
  822         if (map->pagesneeded == 0)
  823                 panic("add_bounce_page: map doesn't need any pages");
  824         map->pagesneeded--;
  825 
  826         if (map->pagesreserved == 0)
  827                 panic("add_bounce_page: map doesn't need any pages");
  828         map->pagesreserved--;
  829 
  830         s = splhigh();
  831         bpage = STAILQ_FIRST(&bounce_page_list);
  832         if (bpage == NULL)
  833                 panic("add_bounce_page: free page list is empty");
  834 
  835         STAILQ_REMOVE_HEAD(&bounce_page_list, links);
  836         reserved_bpages--;
  837         active_bpages++;
  838         splx(s);
  839 
  840         bpage->datavaddr = vaddr;
  841         bpage->datacount = size;
  842         STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
  843         return (bpage->busaddr);
  844 }
  845 
  846 static void
  847 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
  848 {
  849         int s;
  850         struct bus_dmamap *map;
  851 
  852         bpage->datavaddr = 0;
  853         bpage->datacount = 0;
  854 
  855         s = splhigh();
  856         STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
  857         free_bpages++;
  858         active_bpages--;
  859         if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
  860                 if (reserve_bounce_pages(map->dmat, map) == 0) {
  861                         STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
  862                         STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
  863                                            map, links);
  864                         busdma_swi_pending = 1;
  865                         setsoftvm();
  866                 }
  867         }
  868         splx(s);
  869 }
  870 
  871 void
  872 busdma_swi()
  873 {
  874         int s;
  875         struct bus_dmamap *map;
  876 
  877         s = splhigh();
  878         while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
  879                 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
  880                 splx(s);
  881                 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
  882                                 map->callback, map->callback_arg, /*flags*/0);
  883                 s = splhigh();
  884         }
  885         splx(s);
  886 }

Cache object: d8889088883e362c1ecb2e6afe7c6a5d


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