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
28 * From amd64/busdma_machdep.c, r204214
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
43 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_map.h>
54 #include <machine/atomic.h>
55 #include <machine/bus.h>
56 #include <machine/cpufunc.h>
57 #include <machine/md_var.h>
61 #define MAX_BPAGES MIN(8192, physmem/40)
71 bus_dma_filter_t *filter;
79 bus_dma_lock_t *lockfunc;
81 struct bounce_zone *bounce_zone;
87 vm_offset_t vaddr; /* kva of bounce buffer */
88 bus_addr_t busaddr; /* Physical address */
89 vm_offset_t datavaddr; /* kva of client data */
90 bus_size_t datacount; /* client data count */
91 STAILQ_ENTRY(bounce_page) links;
94 int busdma_swi_pending;
97 STAILQ_ENTRY(bounce_zone) links;
98 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
106 bus_size_t alignment;
110 struct sysctl_ctx_list sysctl_tree;
111 struct sysctl_oid *sysctl_tree_top;
114 static struct mtx bounce_lock;
115 static int total_bpages;
116 static int busdma_zonecount;
117 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
119 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
120 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
121 "Total bounce pages");
124 struct bp_list bpages;
128 void *buf; /* unmapped buffer pointer */
129 bus_size_t buflen; /* unmapped buffer length */
130 bus_dma_segment_t *segments;
132 bus_dmamap_callback_t *callback;
134 STAILQ_ENTRY(bus_dmamap) links;
138 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
139 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
141 static void init_bounce_pages(void *dummy);
142 static int alloc_bounce_zone(bus_dma_tag_t dmat);
143 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
144 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
146 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
147 vm_offset_t vaddr, bus_size_t size);
148 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
149 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
152 * Return true if a match is made.
154 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
156 * If paddr is within the bounds of the dma tag then call the filter callback
157 * to check for a match, if there is no filter callback then assume a match.
160 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
167 if (dmat->filter == NULL && dmat->iommu == NULL &&
168 paddr > dmat->lowaddr && paddr <= dmat->highaddr)
170 if (dmat->filter == NULL &&
171 (paddr & (dmat->alignment - 1)) != 0)
173 if (dmat->filter != NULL &&
174 (*dmat->filter)(dmat->filterarg, paddr) != 0)
178 } while (retval == 0 && dmat != NULL);
183 * Convenience function for manipulating driver locks from busdma (during
184 * busdma_swi, for example). Drivers that don't provide their own locks
185 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
186 * non-mutex locking scheme don't have to use this at all.
189 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
193 dmtx = (struct mtx *)arg;
202 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
207 * dflt_lock should never get called. It gets put into the dma tag when
208 * lockfunc == NULL, which is only valid if the maps that are associated
209 * with the tag are meant to never be defered.
210 * XXX Should have a way to identify which driver is responsible here.
213 dflt_lock(void *arg, bus_dma_lock_op_t op)
215 panic("driver error: busdma dflt_lock called");
218 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
219 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
221 * Allocate a device specific dma_tag.
224 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
225 bus_addr_t boundary, bus_addr_t lowaddr,
226 bus_addr_t highaddr, bus_dma_filter_t *filter,
227 void *filterarg, bus_size_t maxsize, int nsegments,
228 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
229 void *lockfuncarg, bus_dma_tag_t *dmat)
231 bus_dma_tag_t newtag;
234 /* Basic sanity checking */
235 if (boundary != 0 && boundary < maxsegsz)
242 /* Return a NULL tag on failure */
245 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
247 if (newtag == NULL) {
248 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
249 __func__, newtag, 0, error);
253 newtag->parent = parent;
254 newtag->alignment = alignment;
255 newtag->boundary = boundary;
256 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
257 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
258 newtag->filter = filter;
259 newtag->filterarg = filterarg;
260 newtag->maxsize = maxsize;
261 newtag->nsegments = nsegments;
262 newtag->maxsegsz = maxsegsz;
263 newtag->flags = flags;
264 newtag->ref_count = 1; /* Count ourself */
265 newtag->map_count = 0;
266 if (lockfunc != NULL) {
267 newtag->lockfunc = lockfunc;
268 newtag->lockfuncarg = lockfuncarg;
270 newtag->lockfunc = dflt_lock;
271 newtag->lockfuncarg = NULL;
274 /* Take into account any restrictions imposed by our parent tag */
275 if (parent != NULL) {
276 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
277 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
278 if (newtag->boundary == 0)
279 newtag->boundary = parent->boundary;
280 else if (parent->boundary != 0)
281 newtag->boundary = MIN(parent->boundary,
283 if (newtag->filter == NULL) {
285 * Short circuit looking at our parent directly
286 * since we have encapsulated all of its information
288 newtag->filter = parent->filter;
289 newtag->filterarg = parent->filterarg;
290 newtag->parent = parent->parent;
292 if (newtag->parent != NULL)
293 atomic_add_int(&parent->ref_count, 1);
294 newtag->iommu = parent->iommu;
295 newtag->iommu_cookie = parent->iommu_cookie;
298 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem) && newtag->iommu == NULL)
299 newtag->flags |= BUS_DMA_COULD_BOUNCE;
301 if (newtag->alignment > 1)
302 newtag->flags |= BUS_DMA_COULD_BOUNCE;
304 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
305 (flags & BUS_DMA_ALLOCNOW) != 0) {
306 struct bounce_zone *bz;
310 if ((error = alloc_bounce_zone(newtag)) != 0) {
311 free(newtag, M_DEVBUF);
314 bz = newtag->bounce_zone;
316 if (ptoa(bz->total_bpages) < maxsize) {
319 pages = atop(maxsize) - bz->total_bpages;
321 /* Add pages to our bounce pool */
322 if (alloc_bounce_pages(newtag, pages) < pages)
325 /* Performed initial allocation */
326 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
330 free(newtag, M_DEVBUF);
334 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
335 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
340 bus_dma_tag_destroy(bus_dma_tag_t dmat)
342 bus_dma_tag_t dmat_copy;
350 if (dmat->map_count != 0) {
355 while (dmat != NULL) {
356 bus_dma_tag_t parent;
358 parent = dmat->parent;
359 atomic_subtract_int(&dmat->ref_count, 1);
360 if (dmat->ref_count == 0) {
361 free(dmat, M_DEVBUF);
363 * Last reference count, so
364 * release our reference
365 * count on our parent.
373 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
378 * Allocate a handle for mapping from kva/uva/physical
379 * address space into bus device space.
382 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
388 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
391 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
392 __func__, dmat, ENOMEM);
398 * Bouncing might be required if the driver asks for an active
399 * exclusion region, a data alignment that is stricter than 1, and/or
400 * an active address boundary.
402 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
405 struct bounce_zone *bz;
408 if (dmat->bounce_zone == NULL) {
409 if ((error = alloc_bounce_zone(dmat)) != 0)
412 bz = dmat->bounce_zone;
414 /* Initialize the new map */
415 STAILQ_INIT(&((*mapp)->bpages));
418 * Attempt to add pages to our pool on a per-instance
419 * basis up to a sane limit.
421 if (dmat->alignment > 1)
422 maxpages = MAX_BPAGES;
424 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
425 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
426 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
429 pages = MAX(atop(dmat->maxsize), 1);
430 pages = MIN(maxpages - bz->total_bpages, pages);
431 pages = MAX(pages, 1);
432 if (alloc_bounce_pages(dmat, pages) < pages)
435 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
437 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
446 (*mapp)->segments = (bus_dma_segment_t *)malloc(
447 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
449 if ((*mapp)->segments == NULL) {
450 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
451 __func__, dmat, ENOMEM);
457 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
458 __func__, dmat, dmat->flags, error);
463 * Destroy a handle for mapping from kva/uva/physical
464 * address space into bus device space.
467 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
469 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
470 if (STAILQ_FIRST(&map->bpages) != NULL) {
471 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
472 __func__, dmat, EBUSY);
475 if (dmat->bounce_zone)
476 dmat->bounce_zone->map_count--;
478 free(map->segments, M_DEVBUF);
481 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
487 * Allocate a piece of memory that can be efficiently mapped into
488 * bus device space based on the constraints lited in the dma tag.
489 * A dmamap to for use with dmamap_load is also allocated.
492 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
498 if (flags & BUS_DMA_NOWAIT)
503 bus_dmamap_create(dmat, flags, mapp);
505 if (flags & BUS_DMA_ZERO)
508 if (flags & BUS_DMA_NOCACHE)
509 attr = VM_MEMATTR_UNCACHEABLE;
512 attr = VM_MEMATTR_DEFAULT;
516 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
517 * alignment guarantees of malloc need to be nailed down, and the
518 * code below should be rewritten to take that into account.
520 * In the meantime, we'll warn the user if malloc gets it wrong.
522 if ((dmat->maxsize <= PAGE_SIZE) &&
523 (dmat->alignment < dmat->maxsize) &&
524 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
525 attr == VM_MEMATTR_DEFAULT) {
526 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
529 * XXX Use Contigmalloc until it is merged into this facility
530 * and handles multi-seg allocations. Nobody is doing
531 * multi-seg allocations yet though.
532 * XXX Certain AGP hardware does.
534 *vaddr = (void *)kmem_alloc_contig(kernel_map, dmat->maxsize,
535 mflags, 0ul, dmat->lowaddr, dmat->alignment ?
536 dmat->alignment : 1ul, dmat->boundary, attr);
537 (*mapp)->contigalloc = 1;
539 if (*vaddr == NULL) {
540 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
541 __func__, dmat, dmat->flags, ENOMEM);
543 } else if (vtophys(*vaddr) & (dmat->alignment - 1)) {
544 printf("bus_dmamem_alloc failed to align memory properly.\n");
546 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
547 __func__, dmat, dmat->flags, 0);
552 * Free a piece of memory and it's allociated dmamap, that was allocated
553 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
556 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
559 if (!map->contigalloc)
560 free(vaddr, M_DEVBUF);
562 kmem_free(kernel_map, (vm_offset_t)vaddr, dmat->maxsize);
563 bus_dmamap_destroy(dmat, map);
564 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
568 * Utility function to load a linear buffer. lastaddrp holds state
569 * between invocations (for multiple-buffer loads). segp contains
570 * the starting segment on entrance, and the ending segment on exit.
571 * first indicates if this is the first invocation of this function.
574 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
576 void *buf, bus_size_t buflen,
579 bus_addr_t *lastaddrp,
580 bus_dma_segment_t *segs,
585 bus_addr_t curaddr, lastaddr, baddr, bmask;
590 if (map->pagesneeded == 0 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
591 vm_offset_t vendaddr;
593 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
594 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
595 dmat->boundary, dmat->alignment);
596 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d", map, map->pagesneeded);
598 * Count the number of bounce pages
599 * needed in order to complete this transfer
601 vaddr = (vm_offset_t)buf;
602 vendaddr = (vm_offset_t)buf + buflen;
604 while (vaddr < vendaddr) {
607 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
609 paddr = pmap_extract(pmap, vaddr);
611 paddr = pmap_kextract(vaddr);
612 if (run_filter(dmat, paddr) != 0) {
613 sg_len = roundup2(sg_len, dmat->alignment);
618 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
621 /* Reserve Necessary Bounce Pages */
622 if (map->pagesneeded != 0) {
623 mtx_lock(&bounce_lock);
624 if (flags & BUS_DMA_NOWAIT) {
625 if (reserve_bounce_pages(dmat, map, 0) != 0) {
626 mtx_unlock(&bounce_lock);
630 if (reserve_bounce_pages(dmat, map, 1) != 0) {
631 /* Queue us for resources */
634 map->buflen = buflen;
635 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
637 mtx_unlock(&bounce_lock);
638 return (EINPROGRESS);
641 mtx_unlock(&bounce_lock);
644 vaddr = (vm_offset_t)buf;
645 lastaddr = *lastaddrp;
646 bmask = ~(dmat->boundary - 1);
648 for (seg = *segp; buflen > 0 ; ) {
649 bus_size_t max_sgsize;
652 * Get the physical address for this segment.
655 curaddr = pmap_extract(pmap, vaddr);
657 curaddr = pmap_kextract(vaddr);
660 * Compute the segment size, and adjust counts.
662 max_sgsize = MIN(buflen, dmat->maxsegsz);
663 sgsize = PAGE_SIZE - ((vm_offset_t)curaddr & PAGE_MASK);
664 if (map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
665 sgsize = roundup2(sgsize, dmat->alignment);
666 sgsize = MIN(sgsize, max_sgsize);
667 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
669 sgsize = MIN(sgsize, max_sgsize);
673 * Make sure we don't cross any boundaries.
675 if (dmat->boundary > 0) {
676 baddr = (curaddr + dmat->boundary) & bmask;
677 if (sgsize > (baddr - curaddr))
678 sgsize = (baddr - curaddr);
682 * Insert chunk into a segment, coalescing with
683 * previous segment if possible.
686 segs[seg].ds_addr = curaddr;
687 segs[seg].ds_len = sgsize;
690 if (curaddr == lastaddr &&
691 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
692 (dmat->boundary == 0 ||
693 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
694 segs[seg].ds_len += sgsize;
696 if (++seg >= dmat->nsegments)
698 segs[seg].ds_addr = curaddr;
699 segs[seg].ds_len = sgsize;
703 lastaddr = curaddr + sgsize;
709 *lastaddrp = lastaddr;
714 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
718 * Map the buffer buf into bus space using the dmamap map.
721 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
722 bus_size_t buflen, bus_dmamap_callback_t *callback,
723 void *callback_arg, int flags)
725 bus_addr_t lastaddr = 0;
728 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
729 flags |= BUS_DMA_WAITOK;
730 map->callback = callback;
731 map->callback_arg = callback_arg;
735 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
736 &lastaddr, map->segments, &map->nsegs, 1);
739 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
740 __func__, dmat, dmat->flags, error, map->nsegs);
742 if (error == EINPROGRESS) {
746 if (dmat->iommu != NULL)
747 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
748 dmat->highaddr, dmat->alignment, dmat->boundary,
752 (*callback)(callback_arg, map->segments, 0, error);
754 (*callback)(callback_arg, map->segments, map->nsegs, 0);
757 * Return ENOMEM to the caller so that it can pass it up the stack.
758 * This error only happens when NOWAIT is set, so deferal is disabled.
768 * Like _bus_dmamap_load(), but for mbufs.
771 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
773 bus_dmamap_callback2_t *callback, void *callback_arg,
780 flags |= BUS_DMA_NOWAIT;
783 if (m0->m_pkthdr.len <= dmat->maxsize) {
785 bus_addr_t lastaddr = 0;
788 for (m = m0; m != NULL && error == 0; m = m->m_next) {
790 error = _bus_dmamap_load_buffer(dmat, map,
792 NULL, flags, &lastaddr,
793 map->segments, &map->nsegs, first);
802 if (dmat->iommu != NULL)
803 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
804 dmat->highaddr, dmat->alignment, dmat->boundary,
808 /* force "no valid mappings" in callback */
809 (*callback)(callback_arg, map->segments, 0, 0, error);
811 (*callback)(callback_arg, map->segments,
812 map->nsegs, m0->m_pkthdr.len, error);
814 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
815 __func__, dmat, dmat->flags, error, map->nsegs);
820 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
821 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
828 flags |= BUS_DMA_NOWAIT;
831 if (m0->m_pkthdr.len <= dmat->maxsize) {
833 bus_addr_t lastaddr = 0;
836 for (m = m0; m != NULL && error == 0; m = m->m_next) {
838 error = _bus_dmamap_load_buffer(dmat, map,
840 NULL, flags, &lastaddr,
849 /* XXX FIXME: Having to increment nsegs is really annoying */
852 if (dmat->iommu != NULL)
853 IOMMU_MAP(dmat->iommu, segs, nsegs, dmat->lowaddr,
854 dmat->highaddr, dmat->alignment, dmat->boundary,
858 memcpy(map->segments, segs, map->nsegs*sizeof(segs[0]));
860 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
861 __func__, dmat, dmat->flags, error, *nsegs);
866 * Like _bus_dmamap_load(), but for uios.
869 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
871 bus_dmamap_callback2_t *callback, void *callback_arg,
874 bus_addr_t lastaddr = 0;
880 flags |= BUS_DMA_NOWAIT;
881 resid = uio->uio_resid;
884 if (uio->uio_segflg == UIO_USERSPACE) {
885 KASSERT(uio->uio_td != NULL,
886 ("bus_dmamap_load_uio: USERSPACE but no proc"));
887 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
894 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
896 * Now at the first iovec to load. Load each iovec
897 * until we have exhausted the residual count.
900 resid < iov[i].iov_len ? resid : iov[i].iov_len;
901 caddr_t addr = (caddr_t) iov[i].iov_base;
904 error = _bus_dmamap_load_buffer(dmat, map,
905 addr, minlen, pmap, flags, &lastaddr,
906 map->segments, &map->nsegs, first);
914 if (dmat->iommu != NULL)
915 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
916 dmat->highaddr, dmat->alignment, dmat->boundary,
920 /* force "no valid mappings" in callback */
921 (*callback)(callback_arg, map->segments, 0, 0, error);
923 (*callback)(callback_arg, map->segments,
924 map->nsegs, uio->uio_resid, error);
926 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
927 __func__, dmat, dmat->flags, error, map->nsegs);
932 * Release the mapping held by map.
935 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
937 struct bounce_page *bpage;
940 IOMMU_UNMAP(dmat->iommu, map->segments, map->nsegs, dmat->iommu_cookie);
944 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
945 STAILQ_REMOVE_HEAD(&map->bpages, links);
946 free_bounce_page(dmat, bpage);
951 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
953 struct bounce_page *bpage;
955 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
957 * Handle data bouncing. We might also
958 * want to add support for invalidating
959 * the caches on broken hardware
961 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
962 "performing bounce", __func__, op, dmat, dmat->flags);
964 if (op & BUS_DMASYNC_PREWRITE) {
965 while (bpage != NULL) {
966 bcopy((void *)bpage->datavaddr,
967 (void *)bpage->vaddr,
969 bpage = STAILQ_NEXT(bpage, links);
971 dmat->bounce_zone->total_bounced++;
974 if (op & BUS_DMASYNC_POSTREAD) {
975 while (bpage != NULL) {
976 bcopy((void *)bpage->vaddr,
977 (void *)bpage->datavaddr,
979 bpage = STAILQ_NEXT(bpage, links);
981 dmat->bounce_zone->total_bounced++;
989 init_bounce_pages(void *dummy __unused)
993 STAILQ_INIT(&bounce_zone_list);
994 STAILQ_INIT(&bounce_map_waitinglist);
995 STAILQ_INIT(&bounce_map_callbacklist);
996 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
998 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1000 static struct sysctl_ctx_list *
1001 busdma_sysctl_tree(struct bounce_zone *bz)
1003 return (&bz->sysctl_tree);
1006 static struct sysctl_oid *
1007 busdma_sysctl_tree_top(struct bounce_zone *bz)
1009 return (bz->sysctl_tree_top);
1013 alloc_bounce_zone(bus_dma_tag_t dmat)
1015 struct bounce_zone *bz;
1017 /* Check to see if we already have a suitable zone */
1018 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1019 if ((dmat->alignment <= bz->alignment)
1020 && (dmat->lowaddr >= bz->lowaddr)) {
1021 dmat->bounce_zone = bz;
1026 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1027 M_NOWAIT | M_ZERO)) == NULL)
1030 STAILQ_INIT(&bz->bounce_page_list);
1031 bz->free_bpages = 0;
1032 bz->reserved_bpages = 0;
1033 bz->active_bpages = 0;
1034 bz->lowaddr = dmat->lowaddr;
1035 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1037 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1039 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1040 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1041 dmat->bounce_zone = bz;
1043 sysctl_ctx_init(&bz->sysctl_tree);
1044 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1045 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1047 if (bz->sysctl_tree_top == NULL) {
1048 sysctl_ctx_free(&bz->sysctl_tree);
1049 return (0); /* XXX error code? */
1052 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1053 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1054 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1055 "Total bounce pages");
1056 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1057 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1058 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1059 "Free bounce pages");
1060 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1061 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1062 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1063 "Reserved bounce pages");
1064 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1065 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1066 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1067 "Active bounce pages");
1068 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1069 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1070 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1071 "Total bounce requests");
1072 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1073 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1074 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1075 "Total bounce requests that were deferred");
1076 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1077 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1078 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1079 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1080 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1081 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1087 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1089 struct bounce_zone *bz;
1092 bz = dmat->bounce_zone;
1094 while (numpages > 0) {
1095 struct bounce_page *bpage;
1097 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1102 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1107 if (bpage->vaddr == 0) {
1108 free(bpage, M_DEVBUF);
1111 bpage->busaddr = pmap_kextract(bpage->vaddr);
1112 mtx_lock(&bounce_lock);
1113 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1117 mtx_unlock(&bounce_lock);
1125 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1127 struct bounce_zone *bz;
1130 mtx_assert(&bounce_lock, MA_OWNED);
1131 bz = dmat->bounce_zone;
1132 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1133 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1134 return (map->pagesneeded - (map->pagesreserved + pages));
1135 bz->free_bpages -= pages;
1136 bz->reserved_bpages += pages;
1137 map->pagesreserved += pages;
1138 pages = map->pagesneeded - map->pagesreserved;
1144 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1147 struct bounce_zone *bz;
1148 struct bounce_page *bpage;
1150 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1152 bz = dmat->bounce_zone;
1153 if (map->pagesneeded == 0)
1154 panic("add_bounce_page: map doesn't need any pages");
1157 if (map->pagesreserved == 0)
1158 panic("add_bounce_page: map doesn't need any pages");
1159 map->pagesreserved--;
1161 mtx_lock(&bounce_lock);
1162 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1164 panic("add_bounce_page: free page list is empty");
1166 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1167 bz->reserved_bpages--;
1168 bz->active_bpages++;
1169 mtx_unlock(&bounce_lock);
1171 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1172 /* Page offset needs to be preserved. */
1173 bpage->vaddr |= vaddr & PAGE_MASK;
1174 bpage->busaddr |= vaddr & PAGE_MASK;
1176 bpage->datavaddr = vaddr;
1177 bpage->datacount = size;
1178 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1179 return (bpage->busaddr);
1183 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1185 struct bus_dmamap *map;
1186 struct bounce_zone *bz;
1188 bz = dmat->bounce_zone;
1189 bpage->datavaddr = 0;
1190 bpage->datacount = 0;
1191 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1193 * Reset the bounce page to start at offset 0. Other uses
1194 * of this bounce page may need to store a full page of
1195 * data and/or assume it starts on a page boundary.
1197 bpage->vaddr &= ~PAGE_MASK;
1198 bpage->busaddr &= ~PAGE_MASK;
1201 mtx_lock(&bounce_lock);
1202 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1204 bz->active_bpages--;
1205 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1206 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1207 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1208 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1210 busdma_swi_pending = 1;
1211 bz->total_deferred++;
1212 swi_sched(vm_ih, 0);
1215 mtx_unlock(&bounce_lock);
1222 struct bus_dmamap *map;
1224 mtx_lock(&bounce_lock);
1225 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1226 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1227 mtx_unlock(&bounce_lock);
1229 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1230 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1231 map->callback, map->callback_arg, /*flags*/0);
1232 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1233 mtx_lock(&bounce_lock);
1235 mtx_unlock(&bounce_lock);
1239 bus_dma_tag_set_iommu(bus_dma_tag_t tag, struct device *iommu, void *cookie)
1242 tag->iommu_cookie = cookie;