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/memdesc.h>
40 #include <sys/mutex.h>
41 #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_addr_t dataaddr; /* client physical address */
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;
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_addr_t addr,
146 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
147 int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
148 static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
149 pmap_t pmap, void *buf, bus_size_t buflen,
151 static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
152 vm_paddr_t buf, bus_size_t buflen,
154 static int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
159 #define pmap_kextract pmap_kextract_ma
163 * Return true if a match is made.
165 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
167 * If paddr is within the bounds of the dma tag then call the filter callback
168 * to check for a match, if there is no filter callback then assume a match.
171 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
178 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
179 || ((paddr & (dmat->alignment - 1)) != 0))
180 && (dmat->filter == NULL
181 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
185 } while (retval == 0 && dmat != NULL);
190 * Convenience function for manipulating driver locks from busdma (during
191 * busdma_swi, for example). Drivers that don't provide their own locks
192 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
193 * non-mutex locking scheme don't have to use this at all.
196 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
200 dmtx = (struct mtx *)arg;
209 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
214 * dflt_lock should never get called. It gets put into the dma tag when
215 * lockfunc == NULL, which is only valid if the maps that are associated
216 * with the tag are meant to never be defered.
217 * XXX Should have a way to identify which driver is responsible here.
220 dflt_lock(void *arg, bus_dma_lock_op_t op)
222 panic("driver error: busdma dflt_lock called");
226 * Allocate a device specific dma_tag.
229 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
230 bus_addr_t boundary, bus_addr_t lowaddr,
231 bus_addr_t highaddr, bus_dma_filter_t *filter,
232 void *filterarg, bus_size_t maxsize, int nsegments,
233 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
234 void *lockfuncarg, bus_dma_tag_t *dmat)
236 bus_dma_tag_t newtag;
239 /* Basic sanity checking */
240 if (boundary != 0 && boundary < maxsegsz)
247 /* Return a NULL tag on failure */
250 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
252 if (newtag == NULL) {
253 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
254 __func__, newtag, 0, ENOMEM);
258 newtag->parent = parent;
259 newtag->alignment = alignment;
260 newtag->boundary = boundary;
261 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
262 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
263 newtag->filter = filter;
264 newtag->filterarg = filterarg;
265 newtag->maxsize = maxsize;
266 newtag->nsegments = nsegments;
267 newtag->maxsegsz = maxsegsz;
268 newtag->flags = flags;
269 newtag->ref_count = 1; /* Count ourself */
270 newtag->map_count = 0;
271 if (lockfunc != NULL) {
272 newtag->lockfunc = lockfunc;
273 newtag->lockfuncarg = lockfuncarg;
275 newtag->lockfunc = dflt_lock;
276 newtag->lockfuncarg = NULL;
278 newtag->segments = NULL;
280 /* Take into account any restrictions imposed by our parent tag */
281 if (parent != NULL) {
282 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
283 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
284 if (newtag->boundary == 0)
285 newtag->boundary = parent->boundary;
286 else if (parent->boundary != 0)
287 newtag->boundary = MIN(parent->boundary,
289 if ((newtag->filter != NULL) ||
290 ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
291 newtag->flags |= BUS_DMA_COULD_BOUNCE;
292 if (newtag->filter == NULL) {
294 * Short circuit looking at our parent directly
295 * since we have encapsulated all of its information
297 newtag->filter = parent->filter;
298 newtag->filterarg = parent->filterarg;
299 newtag->parent = parent->parent;
301 if (newtag->parent != NULL)
302 atomic_add_int(&parent->ref_count, 1);
305 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
306 || newtag->alignment > 1)
307 newtag->flags |= BUS_DMA_COULD_BOUNCE;
309 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
310 (flags & BUS_DMA_ALLOCNOW) != 0) {
311 struct bounce_zone *bz;
315 if ((error = alloc_bounce_zone(newtag)) != 0) {
316 free(newtag, M_DEVBUF);
319 bz = newtag->bounce_zone;
321 if (ptoa(bz->total_bpages) < maxsize) {
324 pages = atop(maxsize) - bz->total_bpages;
326 /* Add pages to our bounce pool */
327 if (alloc_bounce_pages(newtag, pages) < pages)
330 /* Performed initial allocation */
331 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
335 free(newtag, M_DEVBUF);
339 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
340 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
345 bus_dma_tag_destroy(bus_dma_tag_t dmat)
347 bus_dma_tag_t dmat_copy;
355 if (dmat->map_count != 0) {
360 while (dmat != NULL) {
361 bus_dma_tag_t parent;
363 parent = dmat->parent;
364 atomic_subtract_int(&dmat->ref_count, 1);
365 if (dmat->ref_count == 0) {
366 if (dmat->segments != NULL)
367 free(dmat->segments, M_DEVBUF);
368 free(dmat, M_DEVBUF);
370 * Last reference count, so
371 * release our reference
372 * count on our parent.
380 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
385 * Allocate a handle for mapping from kva/uva/physical
386 * address space into bus device space.
389 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
395 if (dmat->segments == NULL) {
396 dmat->segments = (bus_dma_segment_t *)malloc(
397 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
399 if (dmat->segments == NULL) {
400 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
401 __func__, dmat, ENOMEM);
407 * Bouncing might be required if the driver asks for an active
408 * exclusion region, a data alignment that is stricter than 1, and/or
409 * an active address boundary.
411 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
414 struct bounce_zone *bz;
417 if (dmat->bounce_zone == NULL) {
418 if ((error = alloc_bounce_zone(dmat)) != 0)
421 bz = dmat->bounce_zone;
423 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
426 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
427 __func__, dmat, ENOMEM);
431 /* Initialize the new map */
432 STAILQ_INIT(&((*mapp)->bpages));
435 * Attempt to add pages to our pool on a per-instance
436 * basis up to a sane limit.
438 if (dmat->alignment > 1)
439 maxpages = MAX_BPAGES;
441 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
442 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
443 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
446 pages = MAX(atop(dmat->maxsize), 1);
447 pages = MIN(maxpages - bz->total_bpages, pages);
448 pages = MAX(pages, 1);
449 if (alloc_bounce_pages(dmat, pages) < pages)
452 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
454 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
465 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
466 __func__, dmat, dmat->flags, error);
471 * Destroy a handle for mapping from kva/uva/physical
472 * address space into bus device space.
475 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
477 if (map != NULL && map != &nobounce_dmamap && map != &contig_dmamap) {
478 if (STAILQ_FIRST(&map->bpages) != NULL) {
479 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
480 __func__, dmat, EBUSY);
483 if (dmat->bounce_zone)
484 dmat->bounce_zone->map_count--;
488 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
494 * Allocate a piece of memory that can be efficiently mapped into
495 * bus device space based on the constraints lited in the dma tag.
496 * A dmamap to for use with dmamap_load is also allocated.
499 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
505 if (flags & BUS_DMA_NOWAIT)
510 /* If we succeed, no mapping/bouncing will be required */
513 if (dmat->segments == NULL) {
514 dmat->segments = (bus_dma_segment_t *)malloc(
515 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
517 if (dmat->segments == NULL) {
518 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
519 __func__, dmat, dmat->flags, ENOMEM);
523 if (flags & BUS_DMA_ZERO)
525 if (flags & BUS_DMA_NOCACHE)
526 attr = VM_MEMATTR_UNCACHEABLE;
528 attr = VM_MEMATTR_DEFAULT;
532 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
533 * alignment guarantees of malloc need to be nailed down, and the
534 * code below should be rewritten to take that into account.
536 * In the meantime, we'll warn the user if malloc gets it wrong.
538 if ((dmat->maxsize <= PAGE_SIZE) &&
539 (dmat->alignment < dmat->maxsize) &&
540 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
541 attr == VM_MEMATTR_DEFAULT) {
542 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
543 } else if (dmat->nsegments >= btoc(dmat->maxsize) &&
544 dmat->alignment <= PAGE_SIZE &&
545 (dmat->boundary == 0 || dmat->boundary >= dmat->lowaddr)) {
546 /* Page-based multi-segment allocations allowed */
547 *vaddr = (void *)kmem_alloc_attr(kernel_arena, dmat->maxsize,
548 mflags, 0ul, dmat->lowaddr, attr);
549 *mapp = &contig_dmamap;
551 *vaddr = (void *)kmem_alloc_contig(kernel_arena, dmat->maxsize,
552 mflags, 0ul, dmat->lowaddr, dmat->alignment ?
553 dmat->alignment : 1ul, dmat->boundary, attr);
554 *mapp = &contig_dmamap;
556 if (*vaddr == NULL) {
557 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
558 __func__, dmat, dmat->flags, ENOMEM);
560 } else if (vtophys(*vaddr) & (dmat->alignment - 1)) {
561 printf("bus_dmamem_alloc failed to align memory properly.\n");
563 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
564 __func__, dmat, dmat->flags, 0);
569 * Free a piece of memory and it's allociated dmamap, that was allocated
570 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
573 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
576 * dmamem does not need to be bounced, so the map should be
577 * NULL if malloc() was used and contig_dmamap if
578 * kmem_alloc_contig() was used.
580 if (!(map == NULL || map == &contig_dmamap))
581 panic("bus_dmamem_free: Invalid map freed\n");
583 free(vaddr, M_DEVBUF);
585 kmem_free(kernel_arena, (vm_offset_t)vaddr, dmat->maxsize);
586 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
590 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
591 bus_size_t buflen, int flags)
596 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
598 * Count the number of bounce pages
599 * needed in order to complete this transfer
602 while (buflen != 0) {
603 sgsize = MIN(buflen, dmat->maxsegsz);
604 if (run_filter(dmat, curaddr)) {
605 sgsize = MIN(sgsize, PAGE_SIZE);
611 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
616 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
617 void *buf, bus_size_t buflen, int flags)
620 vm_offset_t vendaddr;
623 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
624 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
625 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
626 dmat->boundary, dmat->alignment);
627 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
628 map, &nobounce_dmamap, map->pagesneeded);
630 * Count the number of bounce pages
631 * needed in order to complete this transfer
633 vaddr = (vm_offset_t)buf;
634 vendaddr = (vm_offset_t)buf + buflen;
636 while (vaddr < vendaddr) {
639 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
640 if (pmap == kernel_pmap)
641 paddr = pmap_kextract(vaddr);
643 paddr = pmap_extract(pmap, vaddr);
644 if (run_filter(dmat, paddr) != 0) {
645 sg_len = roundup2(sg_len, dmat->alignment);
650 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
655 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
658 /* Reserve Necessary Bounce Pages */
659 mtx_lock(&bounce_lock);
660 if (flags & BUS_DMA_NOWAIT) {
661 if (reserve_bounce_pages(dmat, map, 0) != 0) {
662 mtx_unlock(&bounce_lock);
666 if (reserve_bounce_pages(dmat, map, 1) != 0) {
667 /* Queue us for resources */
668 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
669 mtx_unlock(&bounce_lock);
670 return (EINPROGRESS);
673 mtx_unlock(&bounce_lock);
679 * Add a single contiguous physical range to the segment list.
682 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
683 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
685 bus_addr_t baddr, bmask;
689 * Make sure we don't cross any boundaries.
691 bmask = ~(dmat->boundary - 1);
692 if (dmat->boundary > 0) {
693 baddr = (curaddr + dmat->boundary) & bmask;
694 if (sgsize > (baddr - curaddr))
695 sgsize = (baddr - curaddr);
699 * Insert chunk into a segment, coalescing with
700 * previous segment if possible.
705 segs[seg].ds_addr = curaddr;
706 segs[seg].ds_len = sgsize;
708 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
709 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
710 (dmat->boundary == 0 ||
711 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
712 segs[seg].ds_len += sgsize;
714 if (++seg >= dmat->nsegments)
716 segs[seg].ds_addr = curaddr;
717 segs[seg].ds_len = sgsize;
725 * Utility function to load a physical buffer. segp contains
726 * the starting segment on entrace, and the ending segment on exit.
729 _bus_dmamap_load_phys(bus_dma_tag_t dmat,
731 vm_paddr_t buf, bus_size_t buflen,
733 bus_dma_segment_t *segs,
740 if (map == NULL || map == &contig_dmamap)
741 map = &nobounce_dmamap;
744 segs = dmat->segments;
746 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
747 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
748 if (map->pagesneeded != 0) {
749 error = _bus_dmamap_reserve_pages(dmat, map, flags);
757 sgsize = MIN(buflen, dmat->maxsegsz);
758 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
759 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
760 sgsize = MIN(sgsize, PAGE_SIZE);
761 curaddr = add_bounce_page(dmat, map, 0, curaddr,
764 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
775 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
779 * Utility function to load a linear buffer. segp contains
780 * the starting segment on entrace, and the ending segment on exit.
783 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
785 void *buf, bus_size_t buflen,
788 bus_dma_segment_t *segs,
796 if (map == NULL || map == &contig_dmamap)
797 map = &nobounce_dmamap;
800 segs = dmat->segments;
802 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
803 _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
804 if (map->pagesneeded != 0) {
805 error = _bus_dmamap_reserve_pages(dmat, map, flags);
811 vaddr = (vm_offset_t)buf;
814 bus_size_t max_sgsize;
817 * Get the physical address for this segment.
819 if (pmap == kernel_pmap)
820 curaddr = pmap_kextract(vaddr);
822 curaddr = pmap_extract(pmap, vaddr);
825 * Compute the segment size, and adjust counts.
827 max_sgsize = MIN(buflen, dmat->maxsegsz);
828 sgsize = PAGE_SIZE - ((vm_offset_t)curaddr & PAGE_MASK);
829 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
830 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
831 sgsize = roundup2(sgsize, dmat->alignment);
832 sgsize = MIN(sgsize, max_sgsize);
833 curaddr = add_bounce_page(dmat, map, vaddr, curaddr,
836 sgsize = MIN(sgsize, max_sgsize);
838 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
849 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
853 __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
854 struct memdesc *mem, bus_dmamap_callback_t *callback,
860 map->callback = callback;
861 map->callback_arg = callback_arg;
866 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
867 bus_dma_segment_t *segs, int nsegs, int error)
871 segs = dmat->segments;
876 * Release the mapping held by map.
879 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
881 struct bounce_page *bpage;
883 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
884 STAILQ_REMOVE_HEAD(&map->bpages, links);
885 free_bounce_page(dmat, bpage);
890 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
892 struct bounce_page *bpage;
894 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
896 * Handle data bouncing. We might also
897 * want to add support for invalidating
898 * the caches on broken hardware
900 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
901 "performing bounce", __func__, dmat, dmat->flags, op);
903 if (op & BUS_DMASYNC_PREWRITE) {
904 while (bpage != NULL) {
905 if (bpage->datavaddr != 0)
906 bcopy((void *)bpage->datavaddr,
907 (void *)bpage->vaddr,
910 physcopyout(bpage->dataaddr,
911 (void *)bpage->vaddr,
913 bpage = STAILQ_NEXT(bpage, links);
915 dmat->bounce_zone->total_bounced++;
918 if (op & BUS_DMASYNC_POSTREAD) {
919 while (bpage != NULL) {
920 if (bpage->datavaddr != 0)
921 bcopy((void *)bpage->vaddr,
922 (void *)bpage->datavaddr,
925 physcopyin((void *)bpage->vaddr,
928 bpage = STAILQ_NEXT(bpage, links);
930 dmat->bounce_zone->total_bounced++;
936 init_bounce_pages(void *dummy __unused)
940 STAILQ_INIT(&bounce_zone_list);
941 STAILQ_INIT(&bounce_map_waitinglist);
942 STAILQ_INIT(&bounce_map_callbacklist);
943 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
945 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
947 static struct sysctl_ctx_list *
948 busdma_sysctl_tree(struct bounce_zone *bz)
950 return (&bz->sysctl_tree);
953 static struct sysctl_oid *
954 busdma_sysctl_tree_top(struct bounce_zone *bz)
956 return (bz->sysctl_tree_top);
959 #if defined(__amd64__) || defined(PAE)
960 #define SYSCTL_ADD_BUS_SIZE_T SYSCTL_ADD_UQUAD
962 #define SYSCTL_ADD_BUS_SIZE_T(ctx, parent, nbr, name, flag, ptr, desc) \
963 SYSCTL_ADD_UINT(ctx, parent, nbr, name, flag, ptr, 0, desc)
967 alloc_bounce_zone(bus_dma_tag_t dmat)
969 struct bounce_zone *bz;
971 /* Check to see if we already have a suitable zone */
972 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
973 if ((dmat->alignment <= bz->alignment)
974 && (dmat->lowaddr >= bz->lowaddr)) {
975 dmat->bounce_zone = bz;
980 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
981 M_NOWAIT | M_ZERO)) == NULL)
984 STAILQ_INIT(&bz->bounce_page_list);
986 bz->reserved_bpages = 0;
987 bz->active_bpages = 0;
988 bz->lowaddr = dmat->lowaddr;
989 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
991 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
993 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
994 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
995 dmat->bounce_zone = bz;
997 sysctl_ctx_init(&bz->sysctl_tree);
998 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
999 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1001 if (bz->sysctl_tree_top == NULL) {
1002 sysctl_ctx_free(&bz->sysctl_tree);
1003 return (0); /* XXX error code? */
1006 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1007 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1008 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1009 "Total bounce pages");
1010 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1011 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1012 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1013 "Free bounce pages");
1014 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1015 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1016 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1017 "Reserved bounce pages");
1018 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1019 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1020 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1021 "Active bounce pages");
1022 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1023 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1024 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1025 "Total bounce requests");
1026 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1027 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1028 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1029 "Total bounce requests that were deferred");
1030 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1031 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1032 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1033 SYSCTL_ADD_BUS_SIZE_T(busdma_sysctl_tree(bz),
1034 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1035 "alignment", CTLFLAG_RD, &bz->alignment, "");
1041 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1043 struct bounce_zone *bz;
1046 bz = dmat->bounce_zone;
1048 while (numpages > 0) {
1049 struct bounce_page *bpage;
1051 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1056 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1061 if (bpage->vaddr == 0) {
1062 free(bpage, M_DEVBUF);
1065 bpage->busaddr = pmap_kextract(bpage->vaddr);
1066 mtx_lock(&bounce_lock);
1067 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1071 mtx_unlock(&bounce_lock);
1079 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1081 struct bounce_zone *bz;
1084 mtx_assert(&bounce_lock, MA_OWNED);
1085 bz = dmat->bounce_zone;
1086 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1087 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1088 return (map->pagesneeded - (map->pagesreserved + pages));
1089 bz->free_bpages -= pages;
1090 bz->reserved_bpages += pages;
1091 map->pagesreserved += pages;
1092 pages = map->pagesneeded - map->pagesreserved;
1098 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1099 bus_addr_t addr, bus_size_t size)
1101 struct bounce_zone *bz;
1102 struct bounce_page *bpage;
1104 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1105 KASSERT(map != NULL && map != &nobounce_dmamap && map != &contig_dmamap,
1106 ("add_bounce_page: bad map %p", map));
1108 bz = dmat->bounce_zone;
1109 if (map->pagesneeded == 0)
1110 panic("add_bounce_page: map doesn't need any pages");
1113 if (map->pagesreserved == 0)
1114 panic("add_bounce_page: map doesn't need any pages");
1115 map->pagesreserved--;
1117 mtx_lock(&bounce_lock);
1118 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1120 panic("add_bounce_page: free page list is empty");
1122 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1123 bz->reserved_bpages--;
1124 bz->active_bpages++;
1125 mtx_unlock(&bounce_lock);
1127 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1128 /* Page offset needs to be preserved. */
1129 bpage->vaddr |= vaddr & PAGE_MASK;
1130 bpage->busaddr |= vaddr & PAGE_MASK;
1132 bpage->datavaddr = vaddr;
1133 bpage->dataaddr = addr;
1134 bpage->datacount = size;
1135 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1136 return (bpage->busaddr);
1140 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1142 struct bus_dmamap *map;
1143 struct bounce_zone *bz;
1145 bz = dmat->bounce_zone;
1146 bpage->datavaddr = 0;
1147 bpage->datacount = 0;
1148 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1150 * Reset the bounce page to start at offset 0. Other uses
1151 * of this bounce page may need to store a full page of
1152 * data and/or assume it starts on a page boundary.
1154 bpage->vaddr &= ~PAGE_MASK;
1155 bpage->busaddr &= ~PAGE_MASK;
1158 mtx_lock(&bounce_lock);
1159 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1161 bz->active_bpages--;
1162 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1163 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1164 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1165 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1167 busdma_swi_pending = 1;
1168 bz->total_deferred++;
1169 swi_sched(vm_ih, 0);
1172 mtx_unlock(&bounce_lock);
1179 struct bus_dmamap *map;
1181 mtx_lock(&bounce_lock);
1182 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1183 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1184 mtx_unlock(&bounce_lock);
1186 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1187 bus_dmamap_load_mem(map->dmat, map, &map->mem,
1188 map->callback, map->callback_arg,
1190 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1191 mtx_lock(&bounce_lock);
1193 mtx_unlock(&bounce_lock);