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>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
36 #include <sys/mutex.h>
38 #include <sys/interrupt.h>
41 #include <sys/sysctl.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_map.h>
47 #include <machine/atomic.h>
48 #include <machine/bus.h>
49 #include <machine/md_var.h>
51 #define MAX_BPAGES 256
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_swi_pending;
82 static struct mtx bounce_lock;
83 static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
84 static int free_bpages;
85 static int reserved_bpages;
86 static int active_bpages;
87 static int total_bpages;
88 static int total_bounced;
89 static int total_deferred;
91 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
92 SYSCTL_INT(_hw_busdma, OID_AUTO, free_bpages, CTLFLAG_RD, &free_bpages, 0,
94 SYSCTL_INT(_hw_busdma, OID_AUTO, reserved_bpages, CTLFLAG_RD, &reserved_bpages,
95 0, "Reserved bounce pages");
96 SYSCTL_INT(_hw_busdma, OID_AUTO, active_bpages, CTLFLAG_RD, &active_bpages, 0,
97 "Active bounce pages");
98 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
99 "Total bounce pages");
100 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bounced, CTLFLAG_RD, &total_bounced, 0,
101 "Total bounce requests");
102 SYSCTL_INT(_hw_busdma, OID_AUTO, total_deferred, CTLFLAG_RD, &total_deferred, 0,
103 "Total bounce requests that were deferred");
106 struct bp_list bpages;
110 void *buf; /* unmapped buffer pointer */
111 bus_size_t buflen; /* unmapped buffer length */
112 bus_dmamap_callback_t *callback;
114 STAILQ_ENTRY(bus_dmamap) links;
117 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
118 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
119 static struct bus_dmamap nobounce_dmamap;
121 static void init_bounce_pages(void *dummy);
122 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
123 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
125 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
126 vm_offset_t vaddr, bus_size_t size);
127 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
128 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr,
132 * Return true if a match is made.
134 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
136 * If paddr is within the bounds of the dma tag then call the filter callback
137 * to check for a match, if there is no filter callback then assume a match.
140 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr, bus_size_t len)
146 bndy = dmat->boundary;
149 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
150 || ((paddr & (dmat->alignment - 1)) != 0)
151 || ((paddr & bndy) != ((paddr + len) & bndy)))
152 && (dmat->filter == NULL
153 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
157 } while (retval == 0 && dmat != NULL);
162 * Convenience function for manipulating driver locks from busdma (during
163 * busdma_swi, for example). Drivers that don't provide their own locks
164 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
165 * non-mutex locking scheme don't have to use this at all.
168 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
172 dmtx = (struct mtx *)arg;
181 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
186 * dflt_lock should never get called. It gets put into the dma tag when
187 * lockfunc == NULL, which is only valid if the maps that are associated
188 * with the tag are meant to never be defered.
189 * XXX Should have a way to identify which driver is responsible here.
192 dflt_lock(void *arg, bus_dma_lock_op_t op)
194 panic("driver error: busdma dflt_lock called");
197 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
199 * Allocate a device specific dma_tag.
202 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
203 bus_size_t boundary, bus_addr_t lowaddr,
204 bus_addr_t highaddr, bus_dma_filter_t *filter,
205 void *filterarg, bus_size_t maxsize, int nsegments,
206 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
207 void *lockfuncarg, bus_dma_tag_t *dmat)
209 bus_dma_tag_t newtag;
212 /* Basic sanity checking */
213 if (boundary != 0 && boundary < maxsegsz)
216 /* Return a NULL tag on failure */
219 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
223 newtag->parent = parent;
224 newtag->alignment = alignment;
225 newtag->boundary = boundary;
226 newtag->lowaddr = trunc_page(lowaddr) + (PAGE_SIZE - 1);
227 newtag->highaddr = trunc_page(highaddr) + (PAGE_SIZE - 1);
228 newtag->filter = filter;
229 newtag->filterarg = filterarg;
230 newtag->maxsize = maxsize;
231 newtag->nsegments = nsegments;
232 newtag->maxsegsz = maxsegsz;
233 newtag->flags = flags;
234 newtag->ref_count = 1; /* Count ourself */
235 newtag->map_count = 0;
236 if (lockfunc != NULL) {
237 newtag->lockfunc = lockfunc;
238 newtag->lockfuncarg = lockfuncarg;
240 newtag->lockfunc = dflt_lock;
241 newtag->lockfuncarg = NULL;
243 newtag->segments = NULL;
245 /* Take into account any restrictions imposed by our parent tag */
246 if (parent != NULL) {
247 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
248 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
249 if (newtag->boundary == 0)
250 newtag->boundary = parent->boundary;
251 else if (parent->boundary != 0)
252 newtag->boundary = MIN(parent->boundary,
254 if (newtag->filter == NULL) {
256 * Short circuit looking at our parent directly
257 * since we have encapsulated all of its information
259 newtag->filter = parent->filter;
260 newtag->filterarg = parent->filterarg;
261 newtag->parent = parent->parent;
263 if (newtag->parent != NULL)
264 atomic_add_int(&parent->ref_count, 1);
267 if (newtag->lowaddr < ptoa(Maxmem) && (flags & BUS_DMA_ALLOCNOW) != 0) {
270 if (ptoa(total_bpages) < maxsize) {
273 pages = atop(maxsize) - total_bpages;
275 /* Add pages to our bounce pool */
276 if (alloc_bounce_pages(newtag, pages) < pages)
279 /* Performed initial allocation */
280 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
284 free(newtag, M_DEVBUF);
292 bus_dma_tag_destroy(bus_dma_tag_t dmat)
296 if (dmat->map_count != 0)
299 while (dmat != NULL) {
300 bus_dma_tag_t parent;
302 parent = dmat->parent;
303 atomic_subtract_int(&dmat->ref_count, 1);
304 if (dmat->ref_count == 0) {
305 if (dmat->segments != NULL)
306 free(dmat->segments, M_DEVBUF);
307 free(dmat, M_DEVBUF);
309 * Last reference count, so
310 * release our reference
311 * count on our parent.
322 * Allocate a handle for mapping from kva/uva/physical
323 * address space into bus device space.
326 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
332 if (dmat->segments == NULL) {
333 dmat->segments = (bus_dma_segment_t *)malloc(
334 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
336 if (dmat->segments == NULL)
341 * Bouncing might be required if the driver asks for an active
342 * exclusion region, a data alignment that is stricter than 1, and/or
343 * an active address boundary.
345 if (dmat->lowaddr < ptoa(Maxmem)) {
349 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
354 /* Initialize the new map */
355 STAILQ_INIT(&((*mapp)->bpages));
358 * Attempt to add pages to our pool on a per-instance
359 * basis up to a sane limit.
361 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
362 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
363 || (dmat->map_count > 0 && total_bpages < maxpages)) {
366 pages = MAX(atop(dmat->maxsize), 1);
367 pages = MIN(maxpages - total_bpages, pages);
368 if (alloc_bounce_pages(dmat, pages) < pages)
371 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
373 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
387 * Destroy a handle for mapping from kva/uva/physical
388 * address space into bus device space.
391 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
394 if (map != NULL && map != &nobounce_dmamap) {
395 if (STAILQ_FIRST(&map->bpages) != NULL)
405 * Allocate a piece of memory that can be efficiently mapped into
406 * bus device space based on the constraints lited in the dma tag.
407 * A dmamap to for use with dmamap_load is also allocated.
410 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
415 if (flags & BUS_DMA_NOWAIT)
419 if (flags & BUS_DMA_ZERO)
422 /* If we succeed, no mapping/bouncing will be required */
425 if (dmat->segments == NULL) {
426 dmat->segments = (bus_dma_segment_t *)malloc(
427 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
429 if (dmat->segments == NULL)
435 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
436 * alignment guarantees of malloc need to be nailed down, and the
437 * code below should be rewritten to take that into account.
439 * In the meantime, we'll warn the user if malloc gets it wrong.
441 if ((dmat->maxsize <= PAGE_SIZE) &&
442 (dmat->alignment < dmat->maxsize) &&
443 dmat->lowaddr >= ptoa(Maxmem)) {
444 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
447 * XXX Use Contigmalloc until it is merged into this facility
448 * and handles multi-seg allocations. Nobody is doing
449 * multi-seg allocations yet though.
450 * XXX Certain AGP hardware does.
452 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
453 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
458 else if ((uintptr_t)*vaddr & (dmat->alignment - 1))
459 printf("bus_dmamem_alloc failed to align memory properly.\n");
464 * Free a piece of memory and it's allociated dmamap, that was allocated
465 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
468 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
471 * dmamem does not need to be bounced, so the map should be
475 panic("bus_dmamem_free: Invalid map freed\n");
476 if ((dmat->maxsize <= PAGE_SIZE) &&
477 (dmat->alignment < dmat->maxsize) &&
478 dmat->lowaddr >= ptoa(Maxmem))
479 free(vaddr, M_DEVBUF);
481 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
486 * Utility function to load a linear buffer. lastaddrp holds state
487 * between invocations (for multiple-buffer loads). segp contains
488 * the starting segment on entrace, and the ending segment on exit.
489 * first indicates if this is the first invocation of this function.
492 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
494 void *buf, bus_size_t buflen,
497 bus_addr_t *lastaddrp,
498 bus_dma_segment_t *segs,
503 bus_addr_t curaddr, lastaddr, baddr, bmask;
510 map = &nobounce_dmamap;
513 pmap = vmspace_pmap(td->td_proc->p_vmspace);
517 if ((dmat->lowaddr < ptoa(Maxmem) || dmat->boundary > 0 ||
518 dmat->alignment > 1) && map != &nobounce_dmamap &&
519 map->pagesneeded == 0) {
520 vm_offset_t vendaddr;
523 * Count the number of bounce pages
524 * needed in order to complete this transfer
526 vaddr = trunc_page((vm_offset_t)buf);
527 vendaddr = (vm_offset_t)buf + buflen;
529 while (vaddr < vendaddr) {
530 paddr = pmap_kextract(vaddr);
531 if (run_filter(dmat, paddr, 0) != 0)
537 vaddr = (vm_offset_t)buf;
539 /* Reserve Necessary Bounce Pages */
540 if (map->pagesneeded != 0) {
541 mtx_lock(&bounce_lock);
542 if (flags & BUS_DMA_NOWAIT) {
543 if (reserve_bounce_pages(dmat, map, 0) != 0) {
544 mtx_unlock(&bounce_lock);
548 if (reserve_bounce_pages(dmat, map, 1) != 0) {
549 /* Queue us for resources */
552 map->buflen = buflen;
553 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
555 mtx_unlock(&bounce_lock);
556 return (EINPROGRESS);
559 mtx_unlock(&bounce_lock);
562 lastaddr = *lastaddrp;
563 bmask = ~(dmat->boundary - 1);
565 for (seg = *segp; buflen > 0 ; ) {
567 * Get the physical address for this segment.
570 curaddr = pmap_extract(pmap, vaddr);
572 curaddr = pmap_kextract(vaddr);
575 * Compute the segment size, and adjust counts.
577 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
578 if (sgsize > dmat->maxsegsz)
579 sgsize = dmat->maxsegsz;
584 * Make sure we don't cross any boundaries.
586 if (dmat->boundary > 0) {
587 baddr = (curaddr + dmat->boundary) & bmask;
588 if (sgsize > (baddr - curaddr))
589 sgsize = (baddr - curaddr);
592 if (map->pagesneeded != 0 && run_filter(dmat, curaddr, sgsize))
593 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
596 * Insert chunk into a segment, coalescing with
597 * previous segment if possible.
600 segs[seg].ds_addr = curaddr;
601 segs[seg].ds_len = sgsize;
604 if (curaddr == lastaddr &&
605 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
606 (dmat->boundary == 0 ||
607 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
608 segs[seg].ds_len += sgsize;
610 if (++seg >= dmat->nsegments)
612 segs[seg].ds_addr = curaddr;
613 segs[seg].ds_len = sgsize;
617 lastaddr = curaddr + sgsize;
623 *lastaddrp = lastaddr;
628 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
632 * Map the buffer buf into bus space using the dmamap map.
635 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
636 bus_size_t buflen, bus_dmamap_callback_t *callback,
637 void *callback_arg, int flags)
639 bus_addr_t lastaddr = 0;
640 int error, nsegs = 0;
643 flags |= BUS_DMA_WAITOK;
644 map->callback = callback;
645 map->callback_arg = callback_arg;
648 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
649 &lastaddr, dmat->segments, &nsegs, 1);
651 if (error == EINPROGRESS)
655 (*callback)(callback_arg, dmat->segments, 0, error);
657 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
663 * Like _bus_dmamap_load(), but for mbufs.
666 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
668 bus_dmamap_callback2_t *callback, void *callback_arg,
675 flags |= BUS_DMA_NOWAIT;
678 if (m0->m_pkthdr.len <= dmat->maxsize) {
680 bus_addr_t lastaddr = 0;
683 for (m = m0; m != NULL && error == 0; m = m->m_next) {
685 error = _bus_dmamap_load_buffer(dmat, map,
687 NULL, flags, &lastaddr,
688 dmat->segments, &nsegs, first);
697 /* force "no valid mappings" in callback */
698 (*callback)(callback_arg, dmat->segments, 0, 0, error);
700 (*callback)(callback_arg, dmat->segments, nsegs + 1,
701 m0->m_pkthdr.len, error);
707 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
708 struct mbuf *m0, bus_dma_segment_t *segs,
709 int *nsegs, int flags)
715 flags |= BUS_DMA_NOWAIT;
718 if (m0->m_pkthdr.len <= dmat->maxsize) {
720 bus_addr_t lastaddr = 0;
723 for (m = m0; m != NULL && error == 0; m = m->m_next) {
725 error = _bus_dmamap_load_buffer(dmat, map,
727 NULL, flags, &lastaddr,
741 * Like _bus_dmamap_load(), but for uios.
744 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
746 bus_dmamap_callback2_t *callback, void *callback_arg,
750 int nsegs, error, first, i;
753 struct thread *td = NULL;
755 flags |= BUS_DMA_NOWAIT;
756 resid = uio->uio_resid;
759 if (uio->uio_segflg == UIO_USERSPACE) {
762 ("bus_dmamap_load_uio: USERSPACE but no proc"));
768 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
770 * Now at the first iovec to load. Load each iovec
771 * until we have exhausted the residual count.
774 resid < iov[i].iov_len ? resid : iov[i].iov_len;
775 caddr_t addr = (caddr_t) iov[i].iov_base;
778 error = _bus_dmamap_load_buffer(dmat, map, addr,
779 minlen, td, flags, &lastaddr, dmat->segments,
788 /* force "no valid mappings" in callback */
789 (*callback)(callback_arg, dmat->segments, 0, 0, error);
791 (*callback)(callback_arg, dmat->segments, nsegs + 1,
792 uio->uio_resid, error);
798 * Release the mapping held by map.
801 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
803 struct bounce_page *bpage;
805 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
806 STAILQ_REMOVE_HEAD(&map->bpages, links);
807 free_bounce_page(dmat, bpage);
812 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
814 struct bounce_page *bpage;
816 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
818 * Handle data bouncing. We might also
819 * want to add support for invalidating
820 * the caches on broken hardware
823 if (op & BUS_DMASYNC_PREWRITE) {
824 while (bpage != NULL) {
825 bcopy((void *)bpage->datavaddr,
826 (void *)bpage->vaddr,
828 bpage = STAILQ_NEXT(bpage, links);
833 if (op & BUS_DMASYNC_POSTREAD) {
834 while (bpage != NULL) {
835 bcopy((void *)bpage->vaddr,
836 (void *)bpage->datavaddr,
838 bpage = STAILQ_NEXT(bpage, links);
846 init_bounce_pages(void *dummy __unused)
853 STAILQ_INIT(&bounce_page_list);
854 STAILQ_INIT(&bounce_map_waitinglist);
855 STAILQ_INIT(&bounce_map_callbacklist);
856 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
858 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
861 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
866 while (numpages > 0) {
867 struct bounce_page *bpage;
869 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
874 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
879 if (bpage->vaddr == 0) {
880 free(bpage, M_DEVBUF);
883 bpage->busaddr = pmap_kextract(bpage->vaddr);
884 mtx_lock(&bounce_lock);
885 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
888 mtx_unlock(&bounce_lock);
896 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
900 mtx_assert(&bounce_lock, MA_OWNED);
901 pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
902 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
903 return (map->pagesneeded - (map->pagesreserved + pages));
904 free_bpages -= pages;
905 reserved_bpages += pages;
906 map->pagesreserved += pages;
907 pages = map->pagesneeded - map->pagesreserved;
913 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
916 struct bounce_page *bpage;
918 KASSERT(map != NULL && map != &nobounce_dmamap,
919 ("add_bounce_page: bad map %p", map));
921 if (map->pagesneeded == 0)
922 panic("add_bounce_page: map doesn't need any pages");
925 if (map->pagesreserved == 0)
926 panic("add_bounce_page: map doesn't need any pages");
927 map->pagesreserved--;
929 mtx_lock(&bounce_lock);
930 bpage = STAILQ_FIRST(&bounce_page_list);
932 panic("add_bounce_page: free page list is empty");
934 STAILQ_REMOVE_HEAD(&bounce_page_list, links);
937 mtx_unlock(&bounce_lock);
939 bpage->datavaddr = vaddr;
940 bpage->datacount = size;
941 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
942 return (bpage->busaddr);
946 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
948 struct bus_dmamap *map;
950 bpage->datavaddr = 0;
951 bpage->datacount = 0;
953 mtx_lock(&bounce_lock);
954 STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
957 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
958 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
959 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
960 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
962 busdma_swi_pending = 1;
967 mtx_unlock(&bounce_lock);
974 struct bus_dmamap *map;
976 mtx_lock(&bounce_lock);
977 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
978 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
979 mtx_unlock(&bounce_lock);
981 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
982 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
983 map->callback, map->callback_arg, /*flags*/0);
984 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
985 mtx_lock(&bounce_lock);
987 mtx_unlock(&bounce_lock);