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_extern.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_map.h>
50 #include <machine/atomic.h>
51 #include <machine/bus.h>
52 #include <machine/md_var.h>
53 #include <machine/specialreg.h>
56 #define MAX_BPAGES 512
58 #define MAX_BPAGES 8192
60 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
61 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
71 bus_dma_filter_t *filter;
79 bus_dma_lock_t *lockfunc;
81 bus_dma_segment_t *segments;
82 struct bounce_zone *bounce_zone;
86 vm_offset_t vaddr; /* kva of bounce buffer */
87 bus_addr_t busaddr; /* Physical address */
88 vm_offset_t datavaddr; /* kva of client data */
89 bus_size_t datacount; /* client data count */
90 STAILQ_ENTRY(bounce_page) links;
93 int busdma_swi_pending;
96 STAILQ_ENTRY(bounce_zone) links;
97 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
105 bus_size_t alignment;
109 struct sysctl_ctx_list sysctl_tree;
110 struct sysctl_oid *sysctl_tree_top;
113 static struct mtx bounce_lock;
114 static int total_bpages;
115 static int busdma_zonecount;
116 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
118 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
119 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
120 "Total bounce pages");
123 struct bp_list bpages;
127 void *buf; /* unmapped buffer pointer */
128 bus_size_t buflen; /* unmapped buffer length */
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;
136 static struct bus_dmamap nobounce_dmamap, contig_dmamap;
138 static void init_bounce_pages(void *dummy);
139 static int alloc_bounce_zone(bus_dma_tag_t dmat);
140 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
141 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
143 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
144 vm_offset_t vaddr, bus_size_t size);
145 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
146 int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
147 int _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
148 void *buf, bus_size_t buflen, int flags);
152 #define pmap_kextract pmap_kextract_ma
156 * Return true if a match is made.
158 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
160 * If paddr is within the bounds of the dma tag then call the filter callback
161 * to check for a match, if there is no filter callback then assume a match.
164 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
171 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
172 || ((paddr & (dmat->alignment - 1)) != 0))
173 && (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");
219 * Allocate a device specific dma_tag.
222 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
223 bus_size_t boundary, bus_addr_t lowaddr,
224 bus_addr_t highaddr, bus_dma_filter_t *filter,
225 void *filterarg, bus_size_t maxsize, int nsegments,
226 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
227 void *lockfuncarg, bus_dma_tag_t *dmat)
229 bus_dma_tag_t newtag;
232 /* Always enforce at least a 4GB (2GB for PAE) boundary. */
233 #if defined(__amd64__)
234 if (boundary == 0 || boundary > ((bus_addr_t)1 << 32))
235 boundary = (bus_size_t)1 << 32;
237 if (boundary == 0 || boundary > ((bus_addr_t)1 << 31))
238 boundary = (bus_size_t)1 << 31;
240 /* Basic sanity checking */
241 if (boundary != 0 && boundary < maxsegsz)
248 /* Return a NULL tag on failure */
251 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
253 if (newtag == NULL) {
254 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
255 __func__, newtag, 0, error);
259 newtag->parent = parent;
260 newtag->alignment = alignment;
261 newtag->boundary = boundary;
262 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
263 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
264 newtag->filter = filter;
265 newtag->filterarg = filterarg;
266 newtag->maxsize = maxsize;
267 newtag->nsegments = nsegments;
268 newtag->maxsegsz = maxsegsz;
269 newtag->flags = flags;
270 newtag->ref_count = 1; /* Count ourself */
271 newtag->map_count = 0;
272 if (lockfunc != NULL) {
273 newtag->lockfunc = lockfunc;
274 newtag->lockfuncarg = lockfuncarg;
276 newtag->lockfunc = dflt_lock;
277 newtag->lockfuncarg = NULL;
279 newtag->segments = NULL;
281 /* Take into account any restrictions imposed by our parent tag */
282 if (parent != NULL) {
283 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
284 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
285 if (newtag->boundary == 0)
286 newtag->boundary = parent->boundary;
287 else if (parent->boundary != 0)
288 newtag->boundary = MIN(parent->boundary,
290 if ((newtag->filter != NULL) ||
291 ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
292 newtag->flags |= BUS_DMA_COULD_BOUNCE;
293 if (newtag->filter == NULL) {
295 * Short circuit looking at our parent directly
296 * since we have encapsulated all of its information
298 newtag->filter = parent->filter;
299 newtag->filterarg = parent->filterarg;
300 newtag->parent = parent->parent;
302 if (newtag->parent != NULL)
303 atomic_add_int(&parent->ref_count, 1);
306 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
307 || newtag->alignment > 1)
308 newtag->flags |= BUS_DMA_COULD_BOUNCE;
310 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
311 (flags & BUS_DMA_ALLOCNOW) != 0) {
312 struct bounce_zone *bz;
316 if ((error = alloc_bounce_zone(newtag)) != 0) {
317 free(newtag, M_DEVBUF);
320 bz = newtag->bounce_zone;
322 if (ptoa(bz->total_bpages) < maxsize) {
325 pages = atop(maxsize) - bz->total_bpages;
327 /* Add pages to our bounce pool */
328 if (alloc_bounce_pages(newtag, pages) < pages)
331 /* Performed initial allocation */
332 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
336 free(newtag, M_DEVBUF);
340 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
341 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
346 bus_dma_tag_destroy(bus_dma_tag_t dmat)
348 bus_dma_tag_t dmat_copy;
356 if (dmat->map_count != 0) {
361 while (dmat != NULL) {
362 bus_dma_tag_t parent;
364 parent = dmat->parent;
365 atomic_subtract_int(&dmat->ref_count, 1);
366 if (dmat->ref_count == 0) {
367 if (dmat->segments != NULL)
368 free(dmat->segments, M_DEVBUF);
369 free(dmat, M_DEVBUF);
371 * Last reference count, so
372 * release our reference
373 * count on our parent.
381 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
386 * Allocate a handle for mapping from kva/uva/physical
387 * address space into bus device space.
390 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
396 if (dmat->segments == NULL) {
397 dmat->segments = (bus_dma_segment_t *)malloc(
398 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
400 if (dmat->segments == NULL) {
401 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
402 __func__, dmat, ENOMEM);
408 * Bouncing might be required if the driver asks for an active
409 * exclusion region, a data alignment that is stricter than 1, and/or
410 * an active address boundary.
412 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
415 struct bounce_zone *bz;
418 if (dmat->bounce_zone == NULL) {
419 if ((error = alloc_bounce_zone(dmat)) != 0)
422 bz = dmat->bounce_zone;
424 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
427 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
428 __func__, dmat, ENOMEM);
432 /* Initialize the new map */
433 STAILQ_INIT(&((*mapp)->bpages));
436 * Attempt to add pages to our pool on a per-instance
437 * basis up to a sane limit.
439 if (dmat->alignment > 1)
440 maxpages = MAX_BPAGES;
442 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
443 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
444 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
447 pages = MAX(atop(dmat->maxsize), 1);
448 pages = MIN(maxpages - bz->total_bpages, pages);
449 pages = MAX(pages, 1);
450 if (alloc_bounce_pages(dmat, pages) < pages)
453 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
455 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
466 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
467 __func__, dmat, dmat->flags, error);
472 * Destroy a handle for mapping from kva/uva/physical
473 * address space into bus device space.
476 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
478 if (map != NULL && map != &nobounce_dmamap && map != &contig_dmamap) {
479 if (STAILQ_FIRST(&map->bpages) != NULL) {
480 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
481 __func__, dmat, EBUSY);
484 if (dmat->bounce_zone)
485 dmat->bounce_zone->map_count--;
489 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
495 * Allocate a piece of memory that can be efficiently mapped into
496 * bus device space based on the constraints lited in the dma tag.
497 * A dmamap to for use with dmamap_load is also allocated.
500 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
506 if (flags & BUS_DMA_NOWAIT)
511 /* If we succeed, no mapping/bouncing will be required */
514 if (dmat->segments == NULL) {
515 dmat->segments = (bus_dma_segment_t *)malloc(
516 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
518 if (dmat->segments == NULL) {
519 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
520 __func__, dmat, dmat->flags, ENOMEM);
524 if (flags & BUS_DMA_ZERO)
526 if (flags & BUS_DMA_NOCACHE)
527 attr = VM_MEMATTR_UNCACHEABLE;
529 attr = VM_MEMATTR_DEFAULT;
533 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
534 * alignment guarantees of malloc need to be nailed down, and the
535 * code below should be rewritten to take that into account.
537 * In the meantime, we'll warn the user if malloc gets it wrong.
539 if ((dmat->maxsize <= PAGE_SIZE) &&
540 (dmat->alignment < dmat->maxsize) &&
541 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
542 attr == VM_MEMATTR_DEFAULT) {
543 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
544 } else if (dmat->nsegments >= btoc(dmat->maxsize) &&
545 dmat->alignment <= PAGE_SIZE &&
546 (dmat->boundary == 0 || dmat->boundary >= dmat->lowaddr)) {
547 /* Page-based multi-segment allocations allowed */
548 *vaddr = (void *)kmem_alloc_attr(kernel_map, dmat->maxsize,
549 mflags, 0ul, dmat->lowaddr, attr);
550 *mapp = &contig_dmamap;
552 *vaddr = (void *)kmem_alloc_contig(kernel_map, dmat->maxsize,
553 mflags, 0ul, dmat->lowaddr, dmat->alignment ?
554 dmat->alignment : 1ul, dmat->boundary, attr);
555 *mapp = &contig_dmamap;
557 if (*vaddr == NULL) {
558 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
559 __func__, dmat, dmat->flags, ENOMEM);
561 } else if (vtophys(*vaddr) & (dmat->alignment - 1)) {
562 printf("bus_dmamem_alloc failed to align memory properly.\n");
564 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
565 __func__, dmat, dmat->flags, 0);
570 * Free a piece of memory and it's allociated dmamap, that was allocated
571 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
574 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
577 * dmamem does not need to be bounced, so the map should be
578 * NULL if malloc() was used and contig_dmamap if
579 * kmem_alloc_contig() was used.
581 if (!(map == NULL || map == &contig_dmamap))
582 panic("bus_dmamem_free: Invalid map freed\n");
584 free(vaddr, M_DEVBUF);
586 kmem_free(kernel_map, (vm_offset_t)vaddr, dmat->maxsize);
587 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
591 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
592 void *buf, bus_size_t buflen, int flags)
595 vm_offset_t vendaddr;
598 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
599 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
600 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
601 dmat->boundary, dmat->alignment);
602 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
603 map, &nobounce_dmamap, map->pagesneeded);
605 * Count the number of bounce pages
606 * needed in order to complete this transfer
608 vaddr = (vm_offset_t)buf;
609 vendaddr = (vm_offset_t)buf + buflen;
611 while (vaddr < vendaddr) {
614 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
616 paddr = pmap_extract(pmap, vaddr);
618 paddr = pmap_kextract(vaddr);
619 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
620 run_filter(dmat, paddr) != 0) {
621 sg_len = roundup2(sg_len, dmat->alignment);
626 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
629 /* Reserve Necessary Bounce Pages */
630 if (map->pagesneeded != 0) {
631 mtx_lock(&bounce_lock);
632 if (flags & BUS_DMA_NOWAIT) {
633 if (reserve_bounce_pages(dmat, map, 0) != 0) {
634 mtx_unlock(&bounce_lock);
638 if (reserve_bounce_pages(dmat, map, 1) != 0) {
639 /* Queue us for resources */
642 map->buflen = buflen;
643 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
645 mtx_unlock(&bounce_lock);
646 return (EINPROGRESS);
649 mtx_unlock(&bounce_lock);
656 * Utility function to load a linear buffer. lastaddrp holds state
657 * between invocations (for multiple-buffer loads). segp contains
658 * the starting segment on entrace, and the ending segment on exit.
659 * first indicates if this is the first invocation of this function.
662 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
664 void *buf, bus_size_t buflen,
667 bus_addr_t *lastaddrp,
668 bus_dma_segment_t *segs,
673 bus_addr_t curaddr, lastaddr, baddr, bmask;
677 if (map == NULL || map == &contig_dmamap)
678 map = &nobounce_dmamap;
680 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
681 error = _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
686 vaddr = (vm_offset_t)buf;
687 lastaddr = *lastaddrp;
688 bmask = ~(dmat->boundary - 1);
690 for (seg = *segp; buflen > 0 ; ) {
691 bus_size_t max_sgsize;
694 * Get the physical address for this segment.
697 curaddr = pmap_extract(pmap, vaddr);
699 curaddr = pmap_kextract(vaddr);
702 * Compute the segment size, and adjust counts.
704 max_sgsize = MIN(buflen, dmat->maxsegsz);
705 sgsize = PAGE_SIZE - ((vm_offset_t)curaddr & PAGE_MASK);
706 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
707 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
708 sgsize = roundup2(sgsize, dmat->alignment);
709 sgsize = MIN(sgsize, max_sgsize);
710 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
712 sgsize = MIN(sgsize, max_sgsize);
716 * Make sure we don't cross any boundaries.
718 if (dmat->boundary > 0) {
719 baddr = (curaddr + dmat->boundary) & bmask;
720 if (sgsize > (baddr - curaddr))
721 sgsize = (baddr - curaddr);
725 * Insert chunk into a segment, coalescing with
726 * previous segment if possible.
729 segs[seg].ds_addr = curaddr;
730 segs[seg].ds_len = sgsize;
733 if (curaddr == lastaddr &&
734 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
735 (dmat->boundary == 0 ||
736 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
737 segs[seg].ds_len += sgsize;
739 if (++seg >= dmat->nsegments)
741 segs[seg].ds_addr = curaddr;
742 segs[seg].ds_len = sgsize;
746 lastaddr = curaddr + sgsize;
752 *lastaddrp = lastaddr;
757 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
761 * Map the buffer buf into bus space using the dmamap map.
764 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
765 bus_size_t buflen, bus_dmamap_callback_t *callback,
766 void *callback_arg, int flags)
768 bus_addr_t lastaddr = 0;
769 int error, nsegs = 0;
772 flags |= BUS_DMA_WAITOK;
773 map->callback = callback;
774 map->callback_arg = callback_arg;
777 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
778 &lastaddr, dmat->segments, &nsegs, 1);
780 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
781 __func__, dmat, dmat->flags, error, nsegs + 1);
783 if (error == EINPROGRESS) {
788 (*callback)(callback_arg, dmat->segments, 0, error);
790 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
793 * Return ENOMEM to the caller so that it can pass it up the stack.
794 * This error only happens when NOWAIT is set, so deferal is disabled.
804 * Like _bus_dmamap_load(), but for mbufs.
807 _bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
808 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
815 flags |= BUS_DMA_NOWAIT;
818 if (m0->m_pkthdr.len <= dmat->maxsize) {
820 bus_addr_t lastaddr = 0;
823 for (m = m0; m != NULL && error == 0; m = m->m_next) {
825 error = _bus_dmamap_load_buffer(dmat, map,
827 NULL, flags, &lastaddr,
836 /* XXX FIXME: Having to increment nsegs is really annoying */
838 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
839 __func__, dmat, dmat->flags, error, *nsegs);
844 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
846 bus_dmamap_callback2_t *callback, void *callback_arg,
851 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, dmat->segments, &nsegs,
855 /* force "no valid mappings" in callback */
856 (*callback)(callback_arg, dmat->segments, 0, 0, error);
858 (*callback)(callback_arg, dmat->segments,
859 nsegs, m0->m_pkthdr.len, error);
861 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
862 __func__, dmat, dmat->flags, error, nsegs);
867 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
868 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
871 return (_bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags));
875 * Like _bus_dmamap_load(), but for uios.
878 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
880 bus_dmamap_callback2_t *callback, void *callback_arg,
883 bus_addr_t lastaddr = 0;
884 int nsegs, error, first, i;
889 flags |= BUS_DMA_NOWAIT;
890 resid = uio->uio_resid;
893 if (uio->uio_segflg == UIO_USERSPACE) {
894 KASSERT(uio->uio_td != NULL,
895 ("bus_dmamap_load_uio: USERSPACE but no proc"));
896 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
903 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
905 * Now at the first iovec to load. Load each iovec
906 * until we have exhausted the residual count.
909 resid < iov[i].iov_len ? resid : iov[i].iov_len;
910 caddr_t addr = (caddr_t) iov[i].iov_base;
913 error = _bus_dmamap_load_buffer(dmat, map,
914 addr, minlen, pmap, flags, &lastaddr,
915 dmat->segments, &nsegs, first);
923 /* force "no valid mappings" in callback */
924 (*callback)(callback_arg, dmat->segments, 0, 0, error);
926 (*callback)(callback_arg, dmat->segments,
927 nsegs+1, uio->uio_resid, error);
929 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
930 __func__, dmat, dmat->flags, error, nsegs + 1);
935 * Release the mapping held by map.
938 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
940 struct bounce_page *bpage;
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);
1008 #if defined(__amd64__) || defined(PAE)
1009 #define SYSCTL_ADD_BUS_SIZE_T SYSCTL_ADD_UQUAD
1011 #define SYSCTL_ADD_BUS_SIZE_T(ctx, parent, nbr, name, flag, ptr, desc) \
1012 SYSCTL_ADD_UINT(ctx, parent, nbr, name, flag, ptr, 0, desc)
1016 alloc_bounce_zone(bus_dma_tag_t dmat)
1018 struct bounce_zone *bz;
1020 /* Check to see if we already have a suitable zone */
1021 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1022 if ((dmat->alignment <= bz->alignment)
1023 && (dmat->lowaddr >= bz->lowaddr)) {
1024 dmat->bounce_zone = bz;
1029 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1030 M_NOWAIT | M_ZERO)) == NULL)
1033 STAILQ_INIT(&bz->bounce_page_list);
1034 bz->free_bpages = 0;
1035 bz->reserved_bpages = 0;
1036 bz->active_bpages = 0;
1037 bz->lowaddr = dmat->lowaddr;
1038 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1040 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1042 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1043 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1044 dmat->bounce_zone = bz;
1046 sysctl_ctx_init(&bz->sysctl_tree);
1047 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1048 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1050 if (bz->sysctl_tree_top == NULL) {
1051 sysctl_ctx_free(&bz->sysctl_tree);
1052 return (0); /* XXX error code? */
1055 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1056 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1057 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1058 "Total bounce pages");
1059 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1060 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1061 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1062 "Free bounce pages");
1063 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1064 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1065 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1066 "Reserved bounce pages");
1067 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1068 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1069 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1070 "Active bounce pages");
1071 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1072 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1073 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1074 "Total bounce requests");
1075 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1076 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1077 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1078 "Total bounce requests that were deferred");
1079 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1080 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1081 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1082 SYSCTL_ADD_BUS_SIZE_T(busdma_sysctl_tree(bz),
1083 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1084 "alignment", CTLFLAG_RD, &bz->alignment, "");
1090 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1092 struct bounce_zone *bz;
1095 bz = dmat->bounce_zone;
1097 while (numpages > 0) {
1098 struct bounce_page *bpage;
1100 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1105 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1110 if (bpage->vaddr == 0) {
1111 free(bpage, M_DEVBUF);
1114 bpage->busaddr = pmap_kextract(bpage->vaddr);
1115 mtx_lock(&bounce_lock);
1116 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1120 mtx_unlock(&bounce_lock);
1128 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1130 struct bounce_zone *bz;
1133 mtx_assert(&bounce_lock, MA_OWNED);
1134 bz = dmat->bounce_zone;
1135 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1136 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1137 return (map->pagesneeded - (map->pagesreserved + pages));
1138 bz->free_bpages -= pages;
1139 bz->reserved_bpages += pages;
1140 map->pagesreserved += pages;
1141 pages = map->pagesneeded - map->pagesreserved;
1147 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1150 struct bounce_zone *bz;
1151 struct bounce_page *bpage;
1153 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1154 KASSERT(map != NULL && map != &nobounce_dmamap && map != &contig_dmamap,
1155 ("add_bounce_page: bad map %p", map));
1157 bz = dmat->bounce_zone;
1158 if (map->pagesneeded == 0)
1159 panic("add_bounce_page: map doesn't need any pages");
1162 if (map->pagesreserved == 0)
1163 panic("add_bounce_page: map doesn't need any pages");
1164 map->pagesreserved--;
1166 mtx_lock(&bounce_lock);
1167 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1169 panic("add_bounce_page: free page list is empty");
1171 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1172 bz->reserved_bpages--;
1173 bz->active_bpages++;
1174 mtx_unlock(&bounce_lock);
1176 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1177 /* Page offset needs to be preserved. */
1178 bpage->vaddr |= vaddr & PAGE_MASK;
1179 bpage->busaddr |= vaddr & PAGE_MASK;
1181 bpage->datavaddr = vaddr;
1182 bpage->datacount = size;
1183 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1184 return (bpage->busaddr);
1188 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1190 struct bus_dmamap *map;
1191 struct bounce_zone *bz;
1193 bz = dmat->bounce_zone;
1194 bpage->datavaddr = 0;
1195 bpage->datacount = 0;
1196 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1198 * Reset the bounce page to start at offset 0. Other uses
1199 * of this bounce page may need to store a full page of
1200 * data and/or assume it starts on a page boundary.
1202 bpage->vaddr &= ~PAGE_MASK;
1203 bpage->busaddr &= ~PAGE_MASK;
1206 mtx_lock(&bounce_lock);
1207 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1209 bz->active_bpages--;
1210 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1211 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1212 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1213 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1215 busdma_swi_pending = 1;
1216 bz->total_deferred++;
1217 swi_sched(vm_ih, 0);
1220 mtx_unlock(&bounce_lock);
1227 struct bus_dmamap *map;
1229 mtx_lock(&bounce_lock);
1230 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1231 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1232 mtx_unlock(&bounce_lock);
1234 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1235 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1236 map->callback, map->callback_arg, /*flags*/0);
1237 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1238 mtx_lock(&bounce_lock);
1240 mtx_unlock(&bounce_lock);