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_page.h>
50 #include <vm/vm_map.h>
52 #include <machine/atomic.h>
53 #include <machine/bus.h>
54 #include <machine/md_var.h>
58 #define MAX_BPAGES MIN(8192, physmem/40)
68 bus_dma_filter_t *filter;
76 bus_dma_lock_t *lockfunc;
78 struct bounce_zone *bounce_zone;
84 vm_offset_t vaddr; /* kva of bounce buffer */
85 bus_addr_t busaddr; /* Physical address */
86 vm_offset_t datavaddr; /* kva of client data */
87 bus_size_t datacount; /* client data count */
88 STAILQ_ENTRY(bounce_page) links;
91 int busdma_swi_pending;
94 STAILQ_ENTRY(bounce_zone) links;
95 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
103 bus_size_t alignment;
107 struct sysctl_ctx_list sysctl_tree;
108 struct sysctl_oid *sysctl_tree_top;
111 static struct mtx bounce_lock;
112 static int total_bpages;
113 static int busdma_zonecount;
114 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
116 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
117 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
118 "Total bounce pages");
121 struct bp_list bpages;
125 void *buf; /* unmapped buffer pointer */
126 bus_size_t buflen; /* unmapped buffer length */
127 bus_dma_segment_t *segments;
129 bus_dmamap_callback_t *callback;
131 STAILQ_ENTRY(bus_dmamap) links;
134 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
135 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
137 static void init_bounce_pages(void *dummy);
138 static int alloc_bounce_zone(bus_dma_tag_t dmat);
139 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
140 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
142 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
143 vm_offset_t vaddr, bus_size_t size);
144 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
145 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
148 * Return true if a match is made.
150 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
152 * If paddr is within the bounds of the dma tag then call the filter callback
153 * to check for a match, if there is no filter callback then assume a match.
156 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
163 if (dmat->filter == NULL && dmat->iommu == NULL &&
164 paddr > dmat->lowaddr && paddr <= dmat->highaddr)
166 if (dmat->filter == NULL &&
167 (paddr & (dmat->alignment - 1)) != 0)
169 if (dmat->filter != NULL &&
170 (*dmat->filter)(dmat->filterarg, paddr) != 0)
174 } while (retval == 0 && dmat != NULL);
179 * Convenience function for manipulating driver locks from busdma (during
180 * busdma_swi, for example). Drivers that don't provide their own locks
181 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
182 * non-mutex locking scheme don't have to use this at all.
185 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
189 dmtx = (struct mtx *)arg;
198 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
203 * dflt_lock should never get called. It gets put into the dma tag when
204 * lockfunc == NULL, which is only valid if the maps that are associated
205 * with the tag are meant to never be defered.
206 * XXX Should have a way to identify which driver is responsible here.
209 dflt_lock(void *arg, bus_dma_lock_op_t op)
211 panic("driver error: busdma dflt_lock called");
214 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
215 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
217 * Allocate a device specific dma_tag.
220 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
221 bus_size_t boundary, bus_addr_t lowaddr,
222 bus_addr_t highaddr, bus_dma_filter_t *filter,
223 void *filterarg, bus_size_t maxsize, int nsegments,
224 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
225 void *lockfuncarg, bus_dma_tag_t *dmat)
227 bus_dma_tag_t newtag;
230 /* Basic sanity checking */
231 if (boundary != 0 && boundary < maxsegsz)
238 /* Return a NULL tag on failure */
241 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
243 if (newtag == NULL) {
244 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
245 __func__, newtag, 0, error);
249 newtag->parent = parent;
250 newtag->alignment = alignment;
251 newtag->boundary = boundary;
252 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
253 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
254 newtag->filter = filter;
255 newtag->filterarg = filterarg;
256 newtag->maxsize = maxsize;
257 newtag->nsegments = nsegments;
258 newtag->maxsegsz = maxsegsz;
259 newtag->flags = flags;
260 newtag->ref_count = 1; /* Count ourself */
261 newtag->map_count = 0;
262 if (lockfunc != NULL) {
263 newtag->lockfunc = lockfunc;
264 newtag->lockfuncarg = lockfuncarg;
266 newtag->lockfunc = dflt_lock;
267 newtag->lockfuncarg = NULL;
270 /* Take into account any restrictions imposed by our parent tag */
271 if (parent != NULL) {
272 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
273 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
274 if (newtag->boundary == 0)
275 newtag->boundary = parent->boundary;
276 else if (parent->boundary != 0)
277 newtag->boundary = MIN(parent->boundary,
279 if (newtag->filter == NULL) {
281 * Short circuit looking at our parent directly
282 * since we have encapsulated all of its information
284 newtag->filter = parent->filter;
285 newtag->filterarg = parent->filterarg;
286 newtag->parent = parent->parent;
288 if (newtag->parent != NULL)
289 atomic_add_int(&parent->ref_count, 1);
290 newtag->iommu = parent->iommu;
291 newtag->iommu_cookie = parent->iommu_cookie;
294 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem) && newtag->iommu == NULL)
295 newtag->flags |= BUS_DMA_COULD_BOUNCE;
297 if (newtag->alignment > 1)
298 newtag->flags |= BUS_DMA_COULD_BOUNCE;
300 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
301 (flags & BUS_DMA_ALLOCNOW) != 0) {
302 struct bounce_zone *bz;
306 if ((error = alloc_bounce_zone(newtag)) != 0) {
307 free(newtag, M_DEVBUF);
310 bz = newtag->bounce_zone;
312 if (ptoa(bz->total_bpages) < maxsize) {
315 pages = atop(maxsize) - bz->total_bpages;
317 /* Add pages to our bounce pool */
318 if (alloc_bounce_pages(newtag, pages) < pages)
321 /* Performed initial allocation */
322 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
326 free(newtag, M_DEVBUF);
330 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
331 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
336 bus_dma_tag_destroy(bus_dma_tag_t dmat)
338 bus_dma_tag_t dmat_copy;
346 if (dmat->map_count != 0) {
351 while (dmat != NULL) {
352 bus_dma_tag_t parent;
354 parent = dmat->parent;
355 atomic_subtract_int(&dmat->ref_count, 1);
356 if (dmat->ref_count == 0) {
357 free(dmat, M_DEVBUF);
359 * Last reference count, so
360 * release our reference
361 * count on our parent.
369 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
374 * Allocate a handle for mapping from kva/uva/physical
375 * address space into bus device space.
378 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
384 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
387 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
388 __func__, dmat, ENOMEM);
394 * Bouncing might be required if the driver asks for an active
395 * exclusion region, a data alignment that is stricter than 1, and/or
396 * an active address boundary.
398 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
401 struct bounce_zone *bz;
404 if (dmat->bounce_zone == NULL) {
405 if ((error = alloc_bounce_zone(dmat)) != 0)
408 bz = dmat->bounce_zone;
410 /* Initialize the new map */
411 STAILQ_INIT(&((*mapp)->bpages));
414 * Attempt to add pages to our pool on a per-instance
415 * basis up to a sane limit.
417 if (dmat->alignment > 1)
418 maxpages = MAX_BPAGES;
420 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
421 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
422 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
425 pages = MAX(atop(dmat->maxsize), 1);
426 pages = MIN(maxpages - bz->total_bpages, pages);
427 pages = MAX(pages, 1);
428 if (alloc_bounce_pages(dmat, pages) < pages)
431 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
433 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
442 (*mapp)->segments = (bus_dma_segment_t *)malloc(
443 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
445 if ((*mapp)->segments == NULL) {
446 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
447 __func__, dmat, ENOMEM);
453 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
454 __func__, dmat, dmat->flags, error);
459 * Destroy a handle for mapping from kva/uva/physical
460 * address space into bus device space.
463 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
465 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
466 if (STAILQ_FIRST(&map->bpages) != NULL) {
467 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
468 __func__, dmat, EBUSY);
471 if (dmat->bounce_zone)
472 dmat->bounce_zone->map_count--;
474 free(map->segments, M_DEVBUF);
477 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
483 * Allocate a piece of memory that can be efficiently mapped into
484 * bus device space based on the constraints lited in the dma tag.
485 * A dmamap to for use with dmamap_load is also allocated.
488 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
493 if (flags & BUS_DMA_NOWAIT)
498 bus_dmamap_create(dmat, flags, mapp);
500 if (flags & BUS_DMA_ZERO)
505 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
506 * alignment guarantees of malloc need to be nailed down, and the
507 * code below should be rewritten to take that into account.
509 * In the meantime, we'll warn the user if malloc gets it wrong.
511 if ((dmat->maxsize <= PAGE_SIZE) &&
512 (dmat->alignment < dmat->maxsize) &&
513 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
514 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
517 * XXX Use Contigmalloc until it is merged into this facility
518 * and handles multi-seg allocations. Nobody is doing
519 * multi-seg allocations yet though.
520 * XXX Certain AGP hardware does.
522 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
523 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
526 if (*vaddr == NULL) {
527 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
528 __func__, dmat, dmat->flags, ENOMEM);
530 } else if (vtophys(*vaddr) & (dmat->alignment - 1)) {
531 printf("bus_dmamem_alloc failed to align memory properly.\n");
534 if (flags & BUS_DMA_NOCACHE)
535 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
536 VM_MEMATTR_UNCACHEABLE);
538 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
539 __func__, dmat, dmat->flags, 0);
544 * Free a piece of memory and it's allociated dmamap, that was allocated
545 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
548 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
550 bus_dmamap_destroy(dmat, map);
553 pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, VM_MEMATTR_DEFAULT);
555 if ((dmat->maxsize <= PAGE_SIZE) &&
556 (dmat->alignment < dmat->maxsize) &&
557 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
558 free(vaddr, M_DEVBUF);
560 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
562 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
566 * Utility function to load a linear buffer. lastaddrp holds state
567 * between invocations (for multiple-buffer loads). segp contains
568 * the starting segment on entrance, and the ending segment on exit.
569 * first indicates if this is the first invocation of this function.
572 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
574 void *buf, bus_size_t buflen,
577 bus_addr_t *lastaddrp,
578 bus_dma_segment_t *segs,
583 bus_addr_t curaddr, lastaddr, baddr, bmask;
588 if (map->pagesneeded == 0 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
589 vm_offset_t vendaddr;
591 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
592 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
593 dmat->boundary, dmat->alignment);
594 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d", map, map->pagesneeded);
596 * Count the number of bounce pages
597 * needed in order to complete this transfer
599 vaddr = (vm_offset_t)buf;
600 vendaddr = (vm_offset_t)buf + buflen;
602 while (vaddr < vendaddr) {
605 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
607 paddr = pmap_extract(pmap, vaddr);
609 paddr = pmap_kextract(vaddr);
610 if (run_filter(dmat, paddr) != 0) {
611 sg_len = roundup2(sg_len, dmat->alignment);
616 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
619 /* Reserve Necessary Bounce Pages */
620 if (map->pagesneeded != 0) {
621 mtx_lock(&bounce_lock);
622 if (flags & BUS_DMA_NOWAIT) {
623 if (reserve_bounce_pages(dmat, map, 0) != 0) {
624 mtx_unlock(&bounce_lock);
628 if (reserve_bounce_pages(dmat, map, 1) != 0) {
629 /* Queue us for resources */
632 map->buflen = buflen;
633 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
635 mtx_unlock(&bounce_lock);
636 return (EINPROGRESS);
639 mtx_unlock(&bounce_lock);
642 vaddr = (vm_offset_t)buf;
643 lastaddr = *lastaddrp;
644 bmask = ~(dmat->boundary - 1);
646 for (seg = *segp; buflen > 0 ; ) {
647 bus_size_t max_sgsize;
650 * Get the physical address for this segment.
653 curaddr = pmap_extract(pmap, vaddr);
655 curaddr = pmap_kextract(vaddr);
658 * Compute the segment size, and adjust counts.
660 max_sgsize = MIN(buflen, dmat->maxsegsz);
661 sgsize = PAGE_SIZE - ((vm_offset_t)curaddr & PAGE_MASK);
662 if (map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
663 sgsize = roundup2(sgsize, dmat->alignment);
664 sgsize = MIN(sgsize, max_sgsize);
665 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
667 sgsize = MIN(sgsize, max_sgsize);
671 * Make sure we don't cross any boundaries.
673 if (dmat->boundary > 0) {
674 baddr = (curaddr + dmat->boundary) & bmask;
675 if (sgsize > (baddr - curaddr))
676 sgsize = (baddr - curaddr);
680 * Insert chunk into a segment, coalescing with
681 * previous segment if possible.
684 segs[seg].ds_addr = curaddr;
685 segs[seg].ds_len = sgsize;
688 if (curaddr == lastaddr &&
689 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
690 (dmat->boundary == 0 ||
691 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
692 segs[seg].ds_len += sgsize;
694 if (++seg >= dmat->nsegments)
696 segs[seg].ds_addr = curaddr;
697 segs[seg].ds_len = sgsize;
701 lastaddr = curaddr + sgsize;
707 *lastaddrp = lastaddr;
712 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
716 * Map the buffer buf into bus space using the dmamap map.
719 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
720 bus_size_t buflen, bus_dmamap_callback_t *callback,
721 void *callback_arg, int flags)
723 bus_addr_t lastaddr = 0;
726 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
727 flags |= BUS_DMA_WAITOK;
728 map->callback = callback;
729 map->callback_arg = callback_arg;
733 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
734 &lastaddr, map->segments, &map->nsegs, 1);
737 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
738 __func__, dmat, dmat->flags, error, map->nsegs);
740 if (error == EINPROGRESS) {
744 if (dmat->iommu != NULL)
745 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
746 dmat->highaddr, dmat->alignment, dmat->boundary,
750 (*callback)(callback_arg, map->segments, 0, error);
752 (*callback)(callback_arg, map->segments, map->nsegs, 0);
755 * Return ENOMEM to the caller so that it can pass it up the stack.
756 * This error only happens when NOWAIT is set, so deferal is disabled.
766 * Like _bus_dmamap_load(), but for mbufs.
769 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
771 bus_dmamap_callback2_t *callback, void *callback_arg,
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,
791 map->segments, &map->nsegs, first);
800 if (dmat->iommu != NULL)
801 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
802 dmat->highaddr, dmat->alignment, dmat->boundary,
806 /* force "no valid mappings" in callback */
807 (*callback)(callback_arg, map->segments, 0, 0, error);
809 (*callback)(callback_arg, map->segments,
810 map->nsegs, m0->m_pkthdr.len, error);
812 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
813 __func__, dmat, dmat->flags, error, map->nsegs);
818 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
819 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
826 flags |= BUS_DMA_NOWAIT;
829 if (m0->m_pkthdr.len <= dmat->maxsize) {
831 bus_addr_t lastaddr = 0;
834 for (m = m0; m != NULL && error == 0; m = m->m_next) {
836 error = _bus_dmamap_load_buffer(dmat, map,
838 NULL, flags, &lastaddr,
847 /* XXX FIXME: Having to increment nsegs is really annoying */
850 if (dmat->iommu != NULL)
851 IOMMU_MAP(dmat->iommu, segs, nsegs, dmat->lowaddr,
852 dmat->highaddr, dmat->alignment, dmat->boundary,
856 memcpy(map->segments, segs, map->nsegs*sizeof(segs[0]));
858 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
859 __func__, dmat, dmat->flags, error, *nsegs);
864 * Like _bus_dmamap_load(), but for uios.
867 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
869 bus_dmamap_callback2_t *callback, void *callback_arg,
872 bus_addr_t lastaddr = 0;
878 flags |= BUS_DMA_NOWAIT;
879 resid = uio->uio_resid;
882 if (uio->uio_segflg == UIO_USERSPACE) {
883 KASSERT(uio->uio_td != NULL,
884 ("bus_dmamap_load_uio: USERSPACE but no proc"));
885 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
892 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
894 * Now at the first iovec to load. Load each iovec
895 * until we have exhausted the residual count.
898 resid < iov[i].iov_len ? resid : iov[i].iov_len;
899 caddr_t addr = (caddr_t) iov[i].iov_base;
902 error = _bus_dmamap_load_buffer(dmat, map,
903 addr, minlen, pmap, flags, &lastaddr,
904 map->segments, &map->nsegs, first);
912 if (dmat->iommu != NULL)
913 IOMMU_MAP(dmat->iommu, map->segments, &map->nsegs, dmat->lowaddr,
914 dmat->highaddr, dmat->alignment, dmat->boundary,
918 /* force "no valid mappings" in callback */
919 (*callback)(callback_arg, map->segments, 0, 0, error);
921 (*callback)(callback_arg, map->segments,
922 map->nsegs, uio->uio_resid, error);
924 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
925 __func__, dmat, dmat->flags, error, map->nsegs);
930 * Release the mapping held by map.
933 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
935 struct bounce_page *bpage;
938 IOMMU_UNMAP(dmat->iommu, map->segments, map->nsegs, dmat->iommu_cookie);
942 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
943 STAILQ_REMOVE_HEAD(&map->bpages, links);
944 free_bounce_page(dmat, bpage);
949 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
951 struct bounce_page *bpage;
953 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
955 * Handle data bouncing. We might also
956 * want to add support for invalidating
957 * the caches on broken hardware
959 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
960 "performing bounce", __func__, op, dmat, dmat->flags);
962 if (op & BUS_DMASYNC_PREWRITE) {
963 while (bpage != NULL) {
964 bcopy((void *)bpage->datavaddr,
965 (void *)bpage->vaddr,
967 bpage = STAILQ_NEXT(bpage, links);
969 dmat->bounce_zone->total_bounced++;
972 if (op & BUS_DMASYNC_POSTREAD) {
973 while (bpage != NULL) {
974 bcopy((void *)bpage->vaddr,
975 (void *)bpage->datavaddr,
977 bpage = STAILQ_NEXT(bpage, links);
979 dmat->bounce_zone->total_bounced++;
985 init_bounce_pages(void *dummy __unused)
989 STAILQ_INIT(&bounce_zone_list);
990 STAILQ_INIT(&bounce_map_waitinglist);
991 STAILQ_INIT(&bounce_map_callbacklist);
992 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
994 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
996 static struct sysctl_ctx_list *
997 busdma_sysctl_tree(struct bounce_zone *bz)
999 return (&bz->sysctl_tree);
1002 static struct sysctl_oid *
1003 busdma_sysctl_tree_top(struct bounce_zone *bz)
1005 return (bz->sysctl_tree_top);
1009 alloc_bounce_zone(bus_dma_tag_t dmat)
1011 struct bounce_zone *bz;
1013 /* Check to see if we already have a suitable zone */
1014 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1015 if ((dmat->alignment <= bz->alignment)
1016 && (dmat->lowaddr >= bz->lowaddr)) {
1017 dmat->bounce_zone = bz;
1022 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1023 M_NOWAIT | M_ZERO)) == NULL)
1026 STAILQ_INIT(&bz->bounce_page_list);
1027 bz->free_bpages = 0;
1028 bz->reserved_bpages = 0;
1029 bz->active_bpages = 0;
1030 bz->lowaddr = dmat->lowaddr;
1031 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1033 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1035 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1036 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1037 dmat->bounce_zone = bz;
1039 sysctl_ctx_init(&bz->sysctl_tree);
1040 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1041 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1043 if (bz->sysctl_tree_top == NULL) {
1044 sysctl_ctx_free(&bz->sysctl_tree);
1045 return (0); /* XXX error code? */
1048 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1049 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1050 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1051 "Total bounce pages");
1052 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1053 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1054 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1055 "Free bounce pages");
1056 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1057 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1058 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1059 "Reserved bounce pages");
1060 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1061 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1062 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1063 "Active bounce pages");
1064 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1065 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1066 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1067 "Total bounce requests");
1068 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1069 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1070 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1071 "Total bounce requests that were deferred");
1072 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1073 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1074 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1075 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1076 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1077 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1083 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1085 struct bounce_zone *bz;
1088 bz = dmat->bounce_zone;
1090 while (numpages > 0) {
1091 struct bounce_page *bpage;
1093 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1098 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1103 if (bpage->vaddr == 0) {
1104 free(bpage, M_DEVBUF);
1107 bpage->busaddr = pmap_kextract(bpage->vaddr);
1108 mtx_lock(&bounce_lock);
1109 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1113 mtx_unlock(&bounce_lock);
1121 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1123 struct bounce_zone *bz;
1126 mtx_assert(&bounce_lock, MA_OWNED);
1127 bz = dmat->bounce_zone;
1128 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1129 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1130 return (map->pagesneeded - (map->pagesreserved + pages));
1131 bz->free_bpages -= pages;
1132 bz->reserved_bpages += pages;
1133 map->pagesreserved += pages;
1134 pages = map->pagesneeded - map->pagesreserved;
1140 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1143 struct bounce_zone *bz;
1144 struct bounce_page *bpage;
1146 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1148 bz = dmat->bounce_zone;
1149 if (map->pagesneeded == 0)
1150 panic("add_bounce_page: map doesn't need any pages");
1153 if (map->pagesreserved == 0)
1154 panic("add_bounce_page: map doesn't need any pages");
1155 map->pagesreserved--;
1157 mtx_lock(&bounce_lock);
1158 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1160 panic("add_bounce_page: free page list is empty");
1162 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1163 bz->reserved_bpages--;
1164 bz->active_bpages++;
1165 mtx_unlock(&bounce_lock);
1167 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1168 /* Page offset needs to be preserved. */
1169 bpage->vaddr |= vaddr & PAGE_MASK;
1170 bpage->busaddr |= vaddr & PAGE_MASK;
1172 bpage->datavaddr = vaddr;
1173 bpage->datacount = size;
1174 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1175 return (bpage->busaddr);
1179 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1181 struct bus_dmamap *map;
1182 struct bounce_zone *bz;
1184 bz = dmat->bounce_zone;
1185 bpage->datavaddr = 0;
1186 bpage->datacount = 0;
1187 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1189 * Reset the bounce page to start at offset 0. Other uses
1190 * of this bounce page may need to store a full page of
1191 * data and/or assume it starts on a page boundary.
1193 bpage->vaddr &= ~PAGE_MASK;
1194 bpage->busaddr &= ~PAGE_MASK;
1197 mtx_lock(&bounce_lock);
1198 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1200 bz->active_bpages--;
1201 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1202 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1203 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1204 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1206 busdma_swi_pending = 1;
1207 bz->total_deferred++;
1208 swi_sched(vm_ih, 0);
1211 mtx_unlock(&bounce_lock);
1218 struct bus_dmamap *map;
1220 mtx_lock(&bounce_lock);
1221 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1222 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1223 mtx_unlock(&bounce_lock);
1225 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1226 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1227 map->callback, map->callback_arg, /*flags*/0);
1228 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1229 mtx_lock(&bounce_lock);
1231 mtx_unlock(&bounce_lock);
1235 bus_dma_tag_set_iommu(bus_dma_tag_t tag, struct device *iommu, void *cookie)
1238 tag->iommu_cookie = cookie;