2 * Copyright (c) 1997, 1998 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/malloc.h>
34 #include <sys/interrupt.h>
35 #include <sys/kernel.h>
39 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_map.h>
48 #include <machine/atomic.h>
49 #include <machine/bus.h>
50 #include <machine/md_var.h>
52 #define MAX_BPAGES 8192
62 bus_dma_filter_t *filter;
70 bus_dma_lock_t *lockfunc;
72 bus_dma_segment_t *segments;
73 struct bounce_zone *bounce_zone;
77 vm_offset_t vaddr; /* kva of bounce buffer */
78 bus_addr_t busaddr; /* Physical address */
79 vm_offset_t datavaddr; /* kva of client data */
80 bus_size_t datacount; /* client data count */
81 STAILQ_ENTRY(bounce_page) links;
84 int busdma_swi_pending;
87 STAILQ_ENTRY(bounce_zone) links;
88 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
100 struct sysctl_ctx_list sysctl_tree;
101 struct sysctl_oid *sysctl_tree_top;
104 static struct mtx bounce_lock;
105 static int total_bpages;
106 static int busdma_zonecount;
107 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
109 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
110 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
111 "Total bounce pages");
114 struct bp_list bpages;
118 void *buf; /* unmapped buffer pointer */
119 bus_size_t buflen; /* unmapped buffer length */
120 bus_dmamap_callback_t *callback;
122 STAILQ_ENTRY(bus_dmamap) links;
125 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
126 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
127 static struct bus_dmamap nobounce_dmamap;
129 static void init_bounce_pages(void *dummy);
130 static int alloc_bounce_zone(bus_dma_tag_t dmat);
131 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
132 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
134 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
135 vm_offset_t vaddr, bus_size_t size);
136 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
137 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
140 * Return true if a match is made.
142 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
144 * If paddr is within the bounds of the dma tag then call the filter callback
145 * to check for a match, if there is no filter callback then assume a match.
148 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
155 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
156 || ((paddr & (dmat->alignment - 1)) != 0))
157 && (dmat->filter == NULL
158 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
162 } while (retval == 0 && dmat != NULL);
167 * Convenience function for manipulating driver locks from busdma (during
168 * busdma_swi, for example). Drivers that don't provide their own locks
169 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
170 * non-mutex locking scheme don't have to use this at all.
173 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
177 dmtx = (struct mtx *)arg;
186 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
191 * dflt_lock should never get called. It gets put into the dma tag when
192 * lockfunc == NULL, which is only valid if the maps that are associated
193 * with the tag are meant to never be defered.
194 * XXX Should have a way to identify which driver is responsible here.
197 dflt_lock(void *arg, bus_dma_lock_op_t op)
199 panic("driver error: busdma dflt_lock called");
202 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
203 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
205 * Allocate a device specific dma_tag.
208 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
209 bus_size_t boundary, bus_addr_t lowaddr,
210 bus_addr_t highaddr, bus_dma_filter_t *filter,
211 void *filterarg, bus_size_t maxsize, int nsegments,
212 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
213 void *lockfuncarg, bus_dma_tag_t *dmat)
215 bus_dma_tag_t newtag;
218 /* Basic sanity checking */
219 if (boundary != 0 && boundary < maxsegsz)
222 /* Return a NULL tag on failure */
225 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
227 if (newtag == NULL) {
228 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
229 __func__, newtag, 0, error);
233 newtag->parent = parent;
234 newtag->alignment = alignment;
235 newtag->boundary = boundary;
236 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
237 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
239 newtag->filter = filter;
240 newtag->filterarg = filterarg;
241 newtag->maxsize = maxsize;
242 newtag->nsegments = nsegments;
243 newtag->maxsegsz = maxsegsz;
244 newtag->flags = flags;
245 newtag->ref_count = 1; /* Count ourself */
246 newtag->map_count = 0;
247 if (lockfunc != NULL) {
248 newtag->lockfunc = lockfunc;
249 newtag->lockfuncarg = lockfuncarg;
251 newtag->lockfunc = dflt_lock;
252 newtag->lockfuncarg = NULL;
254 newtag->segments = NULL;
256 /* Take into account any restrictions imposed by our parent tag */
257 if (parent != NULL) {
258 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
259 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
260 if (newtag->boundary == 0)
261 newtag->boundary = parent->boundary;
262 else if (parent->boundary != 0)
263 newtag->boundary = MIN(parent->boundary,
265 if (newtag->filter == NULL) {
267 * Short circuit looking at our parent directly
268 * since we have encapsulated all of its information
270 newtag->filter = parent->filter;
271 newtag->filterarg = parent->filterarg;
272 newtag->parent = parent->parent;
274 if (newtag->parent != NULL)
275 atomic_add_int(&parent->ref_count, 1);
278 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
279 || newtag->alignment > 1)
280 newtag->flags |= BUS_DMA_COULD_BOUNCE;
282 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
283 (flags & BUS_DMA_ALLOCNOW) != 0) {
284 struct bounce_zone *bz;
288 if ((error = alloc_bounce_zone(newtag)) != 0) {
289 free(newtag, M_DEVBUF);
292 bz = newtag->bounce_zone;
294 if (ptoa(bz->total_bpages) < maxsize) {
297 pages = atop(maxsize) - bz->total_bpages;
299 /* Add pages to our bounce pool */
300 if (alloc_bounce_pages(newtag, pages) < pages)
303 /* Performed initial allocation */
304 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
308 free(newtag, M_DEVBUF);
312 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
313 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
318 bus_dma_tag_destroy(bus_dma_tag_t dmat)
320 bus_dma_tag_t dmat_copy;
328 if (dmat->map_count != 0) {
333 while (dmat != NULL) {
334 bus_dma_tag_t parent;
336 parent = dmat->parent;
337 atomic_subtract_int(&dmat->ref_count, 1);
338 if (dmat->ref_count == 0) {
339 if (dmat->segments != NULL)
340 free(dmat->segments, M_DEVBUF);
341 free(dmat, M_DEVBUF);
343 * Last reference count, so
344 * release our reference
345 * count on our parent.
353 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
358 * Allocate a handle for mapping from kva/uva/physical
359 * address space into bus device space.
362 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
368 if (dmat->segments == NULL) {
369 dmat->segments = (bus_dma_segment_t *)malloc(
370 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
372 if (dmat->segments == NULL) {
373 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
374 __func__, dmat, ENOMEM);
380 * Bouncing might be required if the driver asks for an active
381 * exclusion region, a data alignment that is stricter than 1, and/or
382 * an active address boundary.
384 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
387 struct bounce_zone *bz;
390 if (dmat->bounce_zone == NULL) {
391 if ((error = alloc_bounce_zone(dmat)) != 0)
394 bz = dmat->bounce_zone;
396 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
399 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
400 __func__, dmat, ENOMEM);
404 /* Initialize the new map */
405 STAILQ_INIT(&((*mapp)->bpages));
408 * Attempt to add pages to our pool on a per-instance
409 * basis up to a sane limit.
411 if (dmat->alignment > 1)
412 maxpages = MAX_BPAGES;
414 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
415 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
416 || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
419 pages = MAX(atop(dmat->maxsize), 1);
420 pages = MIN(maxpages - bz->total_bpages, pages);
421 pages = MAX(pages, 1);
422 if (alloc_bounce_pages(dmat, pages) < pages)
425 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
427 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
437 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
438 __func__, dmat, dmat->flags, error);
443 * Destroy a handle for mapping from kva/uva/physical
444 * address space into bus device space.
447 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
449 if (map != NULL && map != &nobounce_dmamap) {
450 if (STAILQ_FIRST(&map->bpages) != NULL) {
451 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
452 __func__, dmat, EBUSY);
458 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
464 * Allocate a piece of memory that can be efficiently mapped into
465 * bus device space based on the constraints lited in the dma tag.
466 * A dmamap to for use with dmamap_load is also allocated.
469 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
474 if (flags & BUS_DMA_NOWAIT)
478 if (flags & BUS_DMA_ZERO)
481 /* If we succeed, no mapping/bouncing will be required */
484 if (dmat->segments == NULL) {
485 dmat->segments = (bus_dma_segment_t *)malloc(
486 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
488 if (dmat->segments == NULL) {
489 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
490 __func__, dmat, dmat->flags, ENOMEM);
495 if ((dmat->maxsize <= PAGE_SIZE) &&
496 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
497 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
500 * XXX Use Contigmalloc until it is merged into this facility
501 * and handles multi-seg allocations. Nobody is doing
502 * multi-seg allocations yet though.
503 * XXX Certain AGP hardware does.
505 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
506 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
509 if (*vaddr == NULL) {
510 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
511 __func__, dmat, dmat->flags, ENOMEM);
514 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
515 __func__, dmat, dmat->flags, ENOMEM);
520 * Free a piece of memory and it's allociated dmamap, that was allocated
521 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
524 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
527 * dmamem does not need to be bounced, so the map should be
531 panic("bus_dmamem_free: Invalid map freed\n");
532 if ((dmat->maxsize <= PAGE_SIZE)
533 && dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
534 free(vaddr, M_DEVBUF);
536 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
538 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
542 * Utility function to load a linear buffer. lastaddrp holds state
543 * between invocations (for multiple-buffer loads). segp contains
544 * the starting segment on entrace, and the ending segment on exit.
545 * first indicates if this is the first invocation of this function.
548 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
550 void *buf, bus_size_t buflen,
553 bus_addr_t *lastaddrp,
554 bus_dma_segment_t *segs,
559 bus_addr_t curaddr, lastaddr, baddr, bmask;
566 map = &nobounce_dmamap;
568 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
569 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
570 vm_offset_t vendaddr;
572 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
573 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
574 dmat->boundary, dmat->alignment);
575 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
576 map, &nobounce_dmamap, map->pagesneeded);
578 * Count the number of bounce pages
579 * needed in order to complete this transfer
581 vaddr = trunc_page((vm_offset_t)buf);
582 vendaddr = (vm_offset_t)buf + buflen;
584 while (vaddr < vendaddr) {
585 paddr = pmap_kextract(vaddr);
586 if (run_filter(dmat, paddr) != 0) {
592 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
595 /* Reserve Necessary Bounce Pages */
596 if (map->pagesneeded != 0) {
597 mtx_lock(&bounce_lock);
598 if (flags & BUS_DMA_NOWAIT) {
599 if (reserve_bounce_pages(dmat, map, 0) != 0) {
600 mtx_unlock(&bounce_lock);
604 if (reserve_bounce_pages(dmat, map, 1) != 0) {
605 /* Queue us for resources */
608 map->buflen = buflen;
609 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
611 mtx_unlock(&bounce_lock);
612 return (EINPROGRESS);
615 mtx_unlock(&bounce_lock);
618 vaddr = (vm_offset_t)buf;
619 lastaddr = *lastaddrp;
620 bmask = ~(dmat->boundary - 1);
622 for (seg = *segp; buflen > 0 ; ) {
624 * Get the physical address for this segment.
627 curaddr = pmap_extract(pmap, vaddr);
629 curaddr = pmap_kextract(vaddr);
632 * Compute the segment size, and adjust counts.
634 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
639 * Make sure we don't cross any boundaries.
641 if (dmat->boundary > 0) {
642 baddr = (curaddr + dmat->boundary) & bmask;
643 if (sgsize > (baddr - curaddr))
644 sgsize = (baddr - curaddr);
647 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
648 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
651 * Insert chunk into a segment, coalescing with
652 * previous segment if possible.
655 segs[seg].ds_addr = curaddr;
656 segs[seg].ds_len = sgsize;
659 if (needbounce == 0 && curaddr == lastaddr &&
660 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
661 (dmat->boundary == 0 ||
662 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
663 segs[seg].ds_len += sgsize;
665 if (++seg >= dmat->nsegments)
667 segs[seg].ds_addr = curaddr;
668 segs[seg].ds_len = sgsize;
672 lastaddr = curaddr + sgsize;
678 *lastaddrp = lastaddr;
683 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
687 * Map the buffer buf into bus space using the dmamap map.
690 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
691 bus_size_t buflen, bus_dmamap_callback_t *callback,
692 void *callback_arg, int flags)
694 bus_addr_t lastaddr = 0;
695 int error, nsegs = 0;
698 flags |= BUS_DMA_WAITOK;
699 map->callback = callback;
700 map->callback_arg = callback_arg;
703 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
704 &lastaddr, dmat->segments, &nsegs, 1);
706 if (error == EINPROGRESS) {
707 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
708 __func__, dmat, dmat->flags, error);
713 (*callback)(callback_arg, dmat->segments, 0, error);
715 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
717 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error 0 nsegs %d",
718 __func__, dmat, dmat->flags, nsegs + 1);
724 * Like _bus_dmamap_load(), but for mbufs.
727 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
729 bus_dmamap_callback2_t *callback, void *callback_arg,
736 flags |= BUS_DMA_NOWAIT;
739 if (m0->m_pkthdr.len <= dmat->maxsize) {
741 bus_addr_t lastaddr = 0;
744 for (m = m0; m != NULL && error == 0; m = m->m_next) {
746 error = _bus_dmamap_load_buffer(dmat, map,
748 NULL, flags, &lastaddr,
749 dmat->segments, &nsegs, first);
758 /* force "no valid mappings" in callback */
759 (*callback)(callback_arg, dmat->segments, 0, 0, error);
761 (*callback)(callback_arg, dmat->segments,
762 nsegs+1, m0->m_pkthdr.len, error);
764 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
765 __func__, dmat, dmat->flags, error, nsegs + 1);
770 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
771 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
778 flags |= BUS_DMA_NOWAIT;
781 if (m0->m_pkthdr.len <= dmat->maxsize) {
783 bus_addr_t lastaddr = 0;
786 for (m = m0; m != NULL && error == 0; m = m->m_next) {
788 error = _bus_dmamap_load_buffer(dmat, map,
790 NULL, flags, &lastaddr,
799 /* XXX FIXME: Having to increment nsegs is really annoying */
801 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
802 __func__, dmat, dmat->flags, error, *nsegs);
807 * Like _bus_dmamap_load(), but for uios.
810 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
812 bus_dmamap_callback2_t *callback, void *callback_arg,
816 int nsegs, error, first, i;
821 flags |= BUS_DMA_NOWAIT;
822 resid = uio->uio_resid;
825 if (uio->uio_segflg == UIO_USERSPACE) {
826 KASSERT(uio->uio_td != NULL,
827 ("bus_dmamap_load_uio: USERSPACE but no proc"));
828 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
835 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
837 * Now at the first iovec to load. Load each iovec
838 * until we have exhausted the residual count.
841 resid < iov[i].iov_len ? resid : iov[i].iov_len;
842 caddr_t addr = (caddr_t) iov[i].iov_base;
845 error = _bus_dmamap_load_buffer(dmat, map,
846 addr, minlen, pmap, flags, &lastaddr,
847 dmat->segments, &nsegs, first);
855 /* force "no valid mappings" in callback */
856 (*callback)(callback_arg, dmat->segments, 0, 0, error);
858 (*callback)(callback_arg, dmat->segments,
859 nsegs+1, uio->uio_resid, error);
861 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
862 __func__, dmat, dmat->flags, error, nsegs + 1);
867 * Release the mapping held by map.
870 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
872 struct bounce_page *bpage;
874 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
875 STAILQ_REMOVE_HEAD(&map->bpages, links);
876 free_bounce_page(dmat, bpage);
881 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
883 struct bounce_page *bpage;
885 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
887 * Handle data bouncing. We might also
888 * want to add support for invalidating
889 * the caches on broken hardware
891 dmat->bounce_zone->total_bounced++;
892 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
893 "performing bounce", __func__, op, dmat, dmat->flags);
895 if (op & BUS_DMASYNC_PREWRITE) {
896 while (bpage != NULL) {
897 bcopy((void *)bpage->datavaddr,
898 (void *)bpage->vaddr,
900 bpage = STAILQ_NEXT(bpage, links);
904 if (op & BUS_DMASYNC_POSTREAD) {
905 while (bpage != NULL) {
906 bcopy((void *)bpage->vaddr,
907 (void *)bpage->datavaddr,
909 bpage = STAILQ_NEXT(bpage, links);
916 init_bounce_pages(void *dummy __unused)
920 STAILQ_INIT(&bounce_zone_list);
921 STAILQ_INIT(&bounce_map_waitinglist);
922 STAILQ_INIT(&bounce_map_callbacklist);
923 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
925 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
927 static struct sysctl_ctx_list *
928 busdma_sysctl_tree(struct bounce_zone *bz)
930 return (&bz->sysctl_tree);
933 static struct sysctl_oid *
934 busdma_sysctl_tree_top(struct bounce_zone *bz)
936 return (bz->sysctl_tree_top);
940 alloc_bounce_zone(bus_dma_tag_t dmat)
942 struct bounce_zone *bz;
944 /* Check to see if we already have a suitable zone */
945 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
946 if ((dmat->alignment <= bz->alignment)
947 && (dmat->boundary <= bz->boundary)
948 && (dmat->lowaddr >= bz->lowaddr)) {
949 dmat->bounce_zone = bz;
954 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
955 M_NOWAIT | M_ZERO)) == NULL)
958 STAILQ_INIT(&bz->bounce_page_list);
960 bz->reserved_bpages = 0;
961 bz->active_bpages = 0;
962 bz->lowaddr = dmat->lowaddr;
963 bz->alignment = dmat->alignment;
964 bz->boundary = dmat->boundary;
965 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
967 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
968 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
969 dmat->bounce_zone = bz;
971 sysctl_ctx_init(&bz->sysctl_tree);
972 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
973 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
975 if (bz->sysctl_tree_top == NULL) {
976 sysctl_ctx_free(&bz->sysctl_tree);
977 return (0); /* XXX error code? */
980 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
981 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
982 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
983 "Total bounce pages");
984 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
985 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
986 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
987 "Free bounce pages");
988 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
989 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
990 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
991 "Reserved bounce pages");
992 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
993 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
994 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
995 "Active bounce pages");
996 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
997 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
998 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
999 "Total bounce requests");
1000 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1001 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1002 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1003 "Total bounce requests that were deferred");
1004 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1005 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1006 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1007 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1008 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1009 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1010 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1011 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1012 "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
1018 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1020 struct bounce_zone *bz;
1023 bz = dmat->bounce_zone;
1025 while (numpages > 0) {
1026 struct bounce_page *bpage;
1028 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1033 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1038 if (bpage->vaddr == 0) {
1039 free(bpage, M_DEVBUF);
1042 bpage->busaddr = pmap_kextract(bpage->vaddr);
1043 mtx_lock(&bounce_lock);
1044 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1048 mtx_unlock(&bounce_lock);
1056 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1058 struct bounce_zone *bz;
1061 mtx_assert(&bounce_lock, MA_OWNED);
1062 bz = dmat->bounce_zone;
1063 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1064 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1065 return (map->pagesneeded - (map->pagesreserved + pages));
1066 bz->free_bpages -= pages;
1067 bz->reserved_bpages += pages;
1068 map->pagesreserved += pages;
1069 pages = map->pagesneeded - map->pagesreserved;
1075 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1078 struct bounce_zone *bz;
1079 struct bounce_page *bpage;
1081 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1082 KASSERT(map != NULL && map != &nobounce_dmamap,
1083 ("add_bounce_page: bad map %p", map));
1085 bz = dmat->bounce_zone;
1086 if (map->pagesneeded == 0)
1087 panic("add_bounce_page: map doesn't need any pages");
1090 if (map->pagesreserved == 0)
1091 panic("add_bounce_page: map doesn't need any pages");
1092 map->pagesreserved--;
1094 mtx_lock(&bounce_lock);
1095 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1097 panic("add_bounce_page: free page list is empty");
1099 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1100 bz->reserved_bpages--;
1101 bz->active_bpages++;
1102 mtx_unlock(&bounce_lock);
1104 bpage->datavaddr = vaddr;
1105 bpage->datacount = size;
1106 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1107 return (bpage->busaddr);
1111 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1113 struct bus_dmamap *map;
1114 struct bounce_zone *bz;
1116 bz = dmat->bounce_zone;
1117 bpage->datavaddr = 0;
1118 bpage->datacount = 0;
1120 mtx_lock(&bounce_lock);
1121 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1123 bz->active_bpages--;
1124 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1125 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1126 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1127 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1129 busdma_swi_pending = 1;
1130 bz->total_deferred++;
1131 swi_sched(vm_ih, 0);
1134 mtx_unlock(&bounce_lock);
1141 struct bus_dmamap *map;
1143 mtx_lock(&bounce_lock);
1144 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1145 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1146 mtx_unlock(&bounce_lock);
1148 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1149 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1150 map->callback, map->callback_arg, /*flags*/0);
1151 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1152 mtx_lock(&bounce_lock);
1154 mtx_unlock(&bounce_lock);