2 * Copyright (c) 1997 Justin T. Gibbs.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_map.h>
46 #include <machine/atomic.h>
47 #include <machine/bus.h>
48 #include <machine/sgmap.h>
49 #include <machine/md_var.h>
51 #define MAX_BPAGES 128
59 bus_dma_filter_t *filter;
67 bus_dma_lock_t *lockfunc;
69 bus_dma_segment_t *segments;
73 vm_offset_t vaddr; /* kva of bounce buffer */
74 bus_addr_t busaddr; /* Physical address */
75 vm_offset_t datavaddr; /* kva of client data */
76 bus_size_t datacount; /* client data count */
77 STAILQ_ENTRY(bounce_page) links;
80 int busdma_pyxis_bug, busdma_swi_pending;
81 TUNABLE_INT("machdep.busdma_pyxis_bug", &busdma_pyxis_bug);
83 static struct mtx bounce_lock;
84 static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
85 static int free_bpages;
86 static int reserved_bpages;
87 static int active_bpages;
88 static int total_bpages;
91 struct bp_list bpages;
95 void *buf; /* unmapped buffer pointer */
96 bus_size_t buflen; /* unmapped buffer length */
97 vm_offset_t busaddress; /* address in bus space */
98 bus_dmamap_callback_t *callback;
100 void *sgmaphandle; /* handle into sgmap */
101 STAILQ_ENTRY(bus_dmamap) links;
104 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
105 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
106 static struct bus_dmamap nobounce_dmamap;
108 static void init_bounce_pages(void *dummy);
109 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
110 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
112 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
113 vm_offset_t vaddr, bus_size_t size);
114 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
115 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
118 * Return true if a match is made.
120 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
122 * If paddr is within the bounds of the dma tag then call the filter callback
123 * to check for a match, if there is no filter callback then assume a match.
126 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
132 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
133 || ((paddr & (dmat->alignment - 1)) != 0))
134 && (dmat->filter == NULL
135 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
139 } while (retval == 0 && dmat != NULL);
144 * Convenience function for manipulating driver locks from busdma (during
145 * busdma_swi, for example). Drivers that don't provide their own locks
146 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
147 * non-mutex locking scheme don't have to use this at all.
150 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
154 dmtx = (struct mtx *)arg;
163 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
168 * dflt_lock should never get called. It gets put into the dma tag when
169 * lockfunc == NULL, which is only valid if the maps that are associated
170 * with the tag are meant to never be defered.
171 * XXX Should have a way to identify which driver is responsible here.
174 dflt_lock(void *arg, bus_dma_lock_op_t op)
177 panic("driver error: busdma dflt_lock called");
179 printf("DRIVER_ERROR: busdma dflt_lock called\n");
183 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
184 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
186 * Allocate a device specific dma_tag.
189 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
190 bus_size_t boundary, bus_addr_t lowaddr,
191 bus_addr_t highaddr, bus_dma_filter_t *filter,
192 void *filterarg, bus_size_t maxsize, int nsegments,
193 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
194 void *lockfuncarg, bus_dma_tag_t *dmat)
196 bus_dma_tag_t newtag;
199 /* Basic sanity checking */
200 if (boundary != 0 && boundary < maxsegsz)
203 /* Return a NULL tag on failure */
206 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
211 newtag->parent = parent;
212 newtag->alignment = alignment;
213 newtag->boundary = boundary;
214 newtag->lowaddr = trunc_page(lowaddr) + (PAGE_SIZE - 1);
215 newtag->highaddr = trunc_page(highaddr) + (PAGE_SIZE - 1);
216 newtag->filter = filter;
217 newtag->filterarg = filterarg;
218 newtag->maxsize = maxsize;
219 newtag->nsegments = nsegments;
220 newtag->maxsegsz = maxsegsz;
221 newtag->flags = flags;
222 newtag->ref_count = 1; /* Count ourself */
223 newtag->map_count = 0;
224 if (lockfunc != NULL) {
225 newtag->lockfunc = lockfunc;
226 newtag->lockfuncarg = lockfuncarg;
228 newtag->lockfunc = dflt_lock;
229 newtag->lockfuncarg = NULL;
231 newtag->segments = NULL;
233 /* Take into account any restrictions imposed by our parent tag */
234 if (parent != NULL) {
235 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
236 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
237 if (newtag->boundary == 0)
238 newtag->boundary = parent->boundary;
239 else if (parent->boundary != 0)
240 newtag->boundary = MIN(parent->boundary,
242 if (newtag->filter == NULL) {
244 * Short circuit looking at our parent directly
245 * since we have encapsulated all of its information
247 newtag->filter = parent->filter;
248 newtag->filterarg = parent->filterarg;
249 newtag->parent = parent->parent;
251 if (newtag->parent != NULL)
252 atomic_add_int(&parent->ref_count, 1);
255 if (newtag->lowaddr < ptoa(Maxmem) || newtag->alignment > 1)
256 newtag->flags |= BUS_DMA_COULD_BOUNCE;
258 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
259 (flags & BUS_DMA_ALLOCNOW) != 0) {
262 if (ptoa(total_bpages) < maxsize) {
265 pages = atop(maxsize) - total_bpages;
267 /* Add pages to our bounce pool */
268 if (alloc_bounce_pages(newtag, pages) < pages)
271 /* Performed initial allocation */
272 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
276 free(newtag, M_DEVBUF);
284 bus_dma_tag_destroy(bus_dma_tag_t dmat)
288 if (dmat->map_count != 0)
291 while (dmat != NULL) {
292 bus_dma_tag_t parent;
294 parent = dmat->parent;
295 atomic_subtract_int(&dmat->ref_count, 1);
296 if (dmat->ref_count == 0) {
297 if (dmat->segments != NULL)
298 free(dmat->segments, M_DEVBUF);
299 free(dmat, M_DEVBUF);
301 * Last reference count, so
302 * release our reference
303 * count on our parent.
314 * Allocate a handle for mapping from kva/uva/physical
315 * address space into bus device space.
318 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
324 if ((dmat->flags & BUS_DMA_ISA) && chipset.sgmap != NULL) {
326 map = (bus_dmamap_t)malloc(sizeof(*map), M_DEVBUF,
332 sgmap_alloc_region(chipset.sgmap,
341 if (dmat->segments == NULL) {
342 dmat->segments = (bus_dma_segment_t *)malloc(
343 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
345 if (dmat->segments == NULL)
350 * Bouncing might be required if the driver asks for an active
351 * exclusion region, a data alignment that is stricter than 1, and/or
352 * an active address boundary.
354 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
359 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
364 /* Initialize the new map */
365 STAILQ_INIT(&((*mapp)->bpages));
368 * Attempt to add pages to our pool on a per-instance
369 * basis up to a sane limit.
371 if (dmat->alignment > 1)
372 maxpages = MAX_BPAGES;
374 maxpages = MIN(MAX_BPAGES,
375 Maxmem - atop(dmat->lowaddr));
376 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
377 || (dmat->map_count > 0 && total_bpages < maxpages)) {
380 pages = MAX(atop(dmat->maxsize), 1);
381 pages = MIN(maxpages - total_bpages, pages);
382 pages = MAX(pages, 1);
383 if (alloc_bounce_pages(dmat, pages) < pages)
386 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
388 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
394 *mapp = &nobounce_dmamap;
402 * Destroy a handle for mapping from kva/uva/physical
403 * address space into bus device space.
406 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
408 if ((dmat->flags & BUS_DMA_ISA) && chipset.sgmap != NULL) {
409 sgmap_free_region(chipset.sgmap, map->sgmaphandle);
412 if (map != NULL && map != &nobounce_dmamap) {
413 if (STAILQ_FIRST(&map->bpages) != NULL)
423 * Allocate a piece of memory that can be efficiently mapped into
424 * bus device space based on the constraints lited in the dma tag.
425 * A dmamap to for use with dmamap_load is also allocated.
428 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
433 if (flags & BUS_DMA_NOWAIT)
437 if (flags & BUS_DMA_ZERO)
440 /* If we succeed, no mapping/bouncing will be required */
441 *mapp = &nobounce_dmamap;
443 if (dmat->segments == NULL) {
444 dmat->segments = (bus_dma_segment_t *)malloc(
445 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
447 if (dmat->segments == NULL)
451 if ((dmat->maxsize <= PAGE_SIZE) &&
452 (dmat->alignment < dmat->maxsize) &&
453 dmat->lowaddr >= ptoa(Maxmem)) {
454 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
457 * XXX Use Contigmalloc until it is merged into this facility
458 * and handles multi-seg allocations. Nobody is doing
459 * multi-seg allocations yet though.
461 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
462 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
471 * Free a piece of memory and it's allociated dmamap, that was allocated
472 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
475 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
478 * dmamem does not need to be bounced, so the map should be
481 if (map != &nobounce_dmamap)
482 panic("bus_dmamem_free: Invalid map freed\n");
483 if ((dmat->maxsize <= PAGE_SIZE) &&
484 (dmat->alignment < dmat->maxsize) &&
485 dmat->lowaddr >= ptoa(Maxmem))
486 free(vaddr, M_DEVBUF);
488 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
492 #define BUS_DMAMAP_NSEGS ((64 * 1024 / PAGE_SIZE) + 1)
494 vm_offset_t alpha_XXX_dmamap_or = 1024UL*1024UL*1024UL; /*XXX */
497 * Utility function to load a linear buffer. lastaddrp holds state
498 * between invocations (for multiple-buffer loads). segp contains
499 * the starting segment on entrace, and the ending segment on exit.
500 * first indicates if this is the first invocation of this function.
503 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
505 void *buf, bus_size_t buflen,
508 bus_addr_t *lastaddrp,
509 bus_dma_segment_t *segs,
514 bus_addr_t curaddr, lastaddr, baddr, bmask;
522 * Enforce a boundary of 8k for buffers that aren't allocated
523 * via bus_dmamem_alloc() on systems with the Pyxis pass 1 DMA
524 * bug. This is somewhat gross.
526 boundary = dmat->boundary;
527 maxsegsz = dmat->maxsegsz;
528 if (busdma_pyxis_bug && map != &nobounce_dmamap) {
529 if (boundary == 0 || boundary > 8192)
531 if (boundary < maxsegsz)
536 * If we are being called during a callback, pagesneeded will
537 * be non-zero, so we can avoid doing the work twice.
539 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
540 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
541 vm_offset_t vendaddr;
544 * Count the number of bounce pages
545 * needed in order to complete this transfer
547 vaddr = trunc_page(buf);
548 vendaddr = (vm_offset_t)buf + buflen;
550 while (vaddr < vendaddr) {
552 paddr = pmap_extract(pmap, vaddr);
554 paddr = pmap_kextract(vaddr);
555 if (run_filter(dmat, paddr) != 0)
561 /* Reserve Necessary Bounce Pages */
562 if (map->pagesneeded != 0) {
563 mtx_lock(&bounce_lock);
564 if (flags & BUS_DMA_NOWAIT) {
565 if (reserve_bounce_pages(dmat, map, 0) != 0) {
566 mtx_unlock(&bounce_lock);
570 if (reserve_bounce_pages(dmat, map, 1) != 0) {
571 /* Queue us for resources */
574 map->buflen = buflen;
575 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
577 mtx_unlock(&bounce_lock);
578 return (EINPROGRESS);
581 mtx_unlock(&bounce_lock);
584 vaddr = (vm_offset_t)buf;
585 lastaddr = *lastaddrp;
586 bmask = ~(boundary - 1);
588 for (seg = *segp; buflen > 0 ; ) {
590 * Get the physical address for this segment.
593 curaddr = pmap_extract(pmap, vaddr);
595 curaddr = pmap_kextract(vaddr);
598 * Compute the segment size, and adjust counts.
600 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
605 * Make sure we don't cross any boundaries.
608 baddr = (curaddr + boundary) & bmask;
609 if (sgsize > (baddr - curaddr))
610 sgsize = (baddr - curaddr);
613 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
614 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
617 * Convert the physical address into an IO address.
619 curaddr |= alpha_XXX_dmamap_or;
622 * Insert chunk into a segment, coalescing with
623 * previous segment if possible.
626 segs[seg].ds_addr = curaddr;
627 segs[seg].ds_len = sgsize;
630 if (curaddr == lastaddr &&
631 (segs[seg].ds_len + sgsize) <= maxsegsz &&
633 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
634 segs[seg].ds_len += sgsize;
636 if (++seg >= dmat->nsegments)
638 segs[seg].ds_addr = curaddr;
639 segs[seg].ds_len = sgsize;
643 lastaddr = curaddr + sgsize;
649 *lastaddrp = lastaddr;
654 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
658 * Map the buffer buf into bus space using the dmamap map.
661 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
662 bus_size_t buflen, bus_dmamap_callback_t *callback,
663 void *callback_arg, int flags)
665 bus_addr_t lastaddr = 0;
666 int error, nsegs = 0;
668 if ((dmat->flags & BUS_DMA_ISA) && chipset.sgmap != NULL) {
669 bus_dma_segment_t segment;
673 * For ISA dma, we use the chipset's scatter-gather
674 * map to map the tranfer into the ISA reachable range
675 * of the bus address space.
677 vaddr = trunc_page((vm_offset_t) buf);
678 dmat->segments = &segment;
679 dmat->segments[0].ds_addr =
680 map->busaddress + (vm_offset_t) buf - vaddr;
681 dmat->segments[0].ds_len = buflen;
682 buflen = round_page((vm_offset_t) buf + buflen) - vaddr;
683 sgmap_load_region(chipset.sgmap,
687 map->buflen = buflen;
688 (*callback)(callback_arg, dmat->segments, 1, 0);
689 dmat->segments = NULL;
694 if (map != &nobounce_dmamap) {
695 flags |= BUS_DMA_WAITOK;
696 map->callback = callback;
697 map->callback_arg = callback_arg;
700 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
701 &lastaddr, dmat->segments, &nsegs, 1);
703 if (error == EINPROGRESS) {
708 (*callback)(callback_arg, dmat->segments, 0, error);
710 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
713 * Return ENOMEM to the caller so that it can pass it up the stack.
714 * This error only happens when NOWAIT is set, so deferal is disabled.
723 * Like _bus_dmamap_load(), but for mbufs.
726 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
728 bus_dmamap_callback2_t *callback, void *callback_arg,
735 flags |= BUS_DMA_NOWAIT;
738 if (m0->m_pkthdr.len <= dmat->maxsize) {
740 bus_addr_t lastaddr = 0;
743 for (m = m0; m != NULL && error == 0; m = m->m_next) {
745 error = _bus_dmamap_load_buffer(dmat, map,
747 NULL, flags, &lastaddr,
748 dmat->segments, &nsegs, first);
757 /* force "no valid mappings" in callback */
758 (*callback)(callback_arg, dmat->segments, 0, 0, error);
760 (*callback)(callback_arg, dmat->segments,
761 nsegs+1, m0->m_pkthdr.len, error);
767 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
768 struct mbuf *m0, bus_dma_segment_t *segs,
769 int *nsegs, int flags)
775 flags |= BUS_DMA_NOWAIT;
778 if (m0->m_pkthdr.len <= dmat->maxsize) {
780 bus_addr_t lastaddr = 0;
783 for (m = m0; m != NULL && error == 0; m = m->m_next) {
785 error = _bus_dmamap_load_buffer(dmat, map,
787 NULL, flags, &lastaddr,
801 * Like _bus_dmamap_load(), but for uios.
804 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
806 bus_dmamap_callback2_t *callback, void *callback_arg,
810 int nsegs, error, first, i;
815 flags |= BUS_DMA_NOWAIT;
816 resid = uio->uio_resid;
819 if (uio->uio_segflg == UIO_USERSPACE) {
820 KASSERT(uio->uio_td != NULL,
821 ("bus_dmamap_load_uio: USERSPACE but no proc"));
822 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
829 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
831 * Now at the first iovec to load. Load each iovec
832 * until we have exhausted the residual count.
835 resid < iov[i].iov_len ? resid : iov[i].iov_len;
836 caddr_t addr = (caddr_t) iov[i].iov_base;
839 error = _bus_dmamap_load_buffer(dmat, map,
840 addr, minlen, pmap, flags, &lastaddr,
841 dmat->segments, &nsegs, first);
849 /* force "no valid mappings" in callback */
850 (*callback)(callback_arg, dmat->segments, 0, 0, error);
852 (*callback)(callback_arg, dmat->segments,
853 nsegs+1, uio->uio_resid, error);
859 * Release the mapping held by map.
862 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
864 struct bounce_page *bpage;
866 if ((dmat->flags & BUS_DMA_ISA) && chipset.sgmap != NULL) {
867 sgmap_unload_region(chipset.sgmap,
873 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
874 STAILQ_REMOVE_HEAD(&map->bpages, links);
875 free_bounce_page(dmat, bpage);
880 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
882 struct bounce_page *bpage;
884 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
886 * Handle data bouncing. We might also
887 * want to add support for invalidating
888 * the caches on broken hardware
890 if (op & BUS_DMASYNC_PREWRITE) {
891 while (bpage != NULL) {
892 bcopy((void *)bpage->datavaddr,
893 (void *)bpage->vaddr,
895 bpage = STAILQ_NEXT(bpage, links);
899 if (op & BUS_DMASYNC_POSTREAD) {
900 while (bpage != NULL) {
901 bcopy((void *)bpage->vaddr,
902 (void *)bpage->datavaddr,
904 bpage = STAILQ_NEXT(bpage, links);
909 /* Ensure any pending writes have drained. */
910 if (op & (BUS_DMASYNC_PREWRITE))
915 init_bounce_pages(void *dummy __unused)
922 STAILQ_INIT(&bounce_page_list);
923 STAILQ_INIT(&bounce_map_waitinglist);
924 STAILQ_INIT(&bounce_map_callbacklist);
925 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
927 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
930 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
935 while (numpages > 0) {
936 struct bounce_page *bpage;
938 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
943 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
948 if (bpage->vaddr == 0) {
949 free(bpage, M_DEVBUF);
952 bpage->busaddr = pmap_kextract(bpage->vaddr);
953 mtx_lock(&bounce_lock);
954 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
957 mtx_unlock(&bounce_lock);
965 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
969 mtx_assert(&bounce_lock, MA_OWNED);
970 pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
971 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
972 return (map->pagesneeded - (map->pagesreserved + pages));
973 free_bpages -= pages;
974 reserved_bpages += pages;
975 map->pagesreserved += pages;
976 pages = map->pagesneeded - map->pagesreserved;
982 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
985 struct bounce_page *bpage;
987 if (map->pagesneeded == 0)
988 panic("add_bounce_page: map doesn't need any pages");
991 if (map->pagesreserved == 0)
992 panic("add_bounce_page: map doesn't need any pages");
993 map->pagesreserved--;
995 mtx_lock(&bounce_lock);
996 bpage = STAILQ_FIRST(&bounce_page_list);
998 panic("add_bounce_page: free page list is empty");
1000 STAILQ_REMOVE_HEAD(&bounce_page_list, links);
1003 mtx_unlock(&bounce_lock);
1005 bpage->datavaddr = vaddr;
1006 bpage->datacount = size;
1007 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1008 return (bpage->busaddr);
1012 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1014 struct bus_dmamap *map;
1016 bpage->datavaddr = 0;
1017 bpage->datacount = 0;
1019 mtx_lock(&bounce_lock);
1020 STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
1023 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1024 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1025 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1026 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1028 busdma_swi_pending = 1;
1029 swi_sched(vm_ih, 0);
1032 mtx_unlock(&bounce_lock);
1039 struct bus_dmamap *map;
1041 mtx_lock(&bounce_lock);
1042 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1043 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1044 mtx_unlock(&bounce_lock);
1046 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1047 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1048 map->callback, map->callback_arg, /*flags*/0);
1049 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1050 mtx_lock(&bounce_lock);
1052 mtx_unlock(&bounce_lock);