2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 2015 The FreeBSD Foundation
6 * Portions of this software were developed by Semihalf
7 * under sponsorship of the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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/memdesc.h>
44 #include <sys/mutex.h>
45 #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/md_var.h>
57 #include <arm64/include/bus_dma_impl.h>
59 #define MAX_BPAGES 4096
62 BUS_DMA_COULD_BOUNCE = 0x01,
63 BUS_DMA_MIN_ALLOC_COMP = 0x02,
64 BUS_DMA_KMEM_ALLOC = 0x04,
70 struct bus_dma_tag_common common;
73 bus_dma_segment_t *segments;
74 struct bounce_zone *bounce_zone;
78 vm_offset_t vaddr; /* kva of bounce buffer */
79 bus_addr_t busaddr; /* Physical address */
80 vm_offset_t datavaddr; /* kva of client data */
81 vm_page_t datapage; /* physical page of client data */
82 vm_offset_t dataoffs; /* page offset of client data */
83 bus_size_t datacount; /* client data count */
84 STAILQ_ENTRY(bounce_page) links;
87 int busdma_swi_pending;
90 STAILQ_ENTRY(bounce_zone) links;
91 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
103 struct sysctl_ctx_list sysctl_tree;
104 struct sysctl_oid *sysctl_tree_top;
107 static struct mtx bounce_lock;
108 static int total_bpages;
109 static int busdma_zonecount;
110 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
112 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
113 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
114 "Total bounce pages");
117 struct bp_list bpages;
122 bus_dmamap_callback_t *callback;
124 STAILQ_ENTRY(bus_dmamap) links;
127 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
128 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
129 static struct bus_dmamap nobounce_dmamap;
131 static void init_bounce_pages(void *dummy);
132 static int alloc_bounce_zone(bus_dma_tag_t dmat);
133 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
134 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
136 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
137 vm_offset_t vaddr, bus_addr_t addr, bus_size_t size);
138 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
139 int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
140 static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
141 pmap_t pmap, void *buf, bus_size_t buflen, int flags);
142 static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
143 vm_paddr_t buf, bus_size_t buflen, int flags);
144 static int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
148 * Allocate a device specific dma_tag.
151 bounce_bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
152 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
153 bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
154 int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
155 void *lockfuncarg, bus_dma_tag_t *dmat)
157 bus_dma_tag_t newtag;
161 error = common_bus_dma_tag_create(parent != NULL ? &parent->common :
162 NULL, alignment, boundary, lowaddr, highaddr, filter, filterarg,
163 maxsize, nsegments, maxsegsz, flags, lockfunc, lockfuncarg,
164 sizeof (struct bus_dma_tag), (void **)&newtag);
168 newtag->common.impl = &bus_dma_bounce_impl;
169 newtag->map_count = 0;
170 newtag->segments = NULL;
172 if (parent != NULL && ((newtag->common.filter != NULL) ||
173 ((parent->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0)))
174 newtag->bounce_flags |= BUS_DMA_COULD_BOUNCE;
176 if (newtag->common.lowaddr < ptoa((vm_paddr_t)Maxmem) ||
177 newtag->common.alignment > 1)
178 newtag->bounce_flags |= BUS_DMA_COULD_BOUNCE;
180 if (((newtag->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0) &&
181 (flags & BUS_DMA_ALLOCNOW) != 0) {
182 struct bounce_zone *bz;
185 if ((error = alloc_bounce_zone(newtag)) != 0) {
186 free(newtag, M_DEVBUF);
189 bz = newtag->bounce_zone;
191 if (ptoa(bz->total_bpages) < maxsize) {
194 pages = atop(maxsize) - bz->total_bpages;
196 /* Add pages to our bounce pool */
197 if (alloc_bounce_pages(newtag, pages) < pages)
200 /* Performed initial allocation */
201 newtag->bounce_flags |= BUS_DMA_MIN_ALLOC_COMP;
206 free(newtag, M_DEVBUF);
209 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
210 __func__, newtag, (newtag != NULL ? newtag->common.flags : 0),
216 bounce_bus_dma_tag_destroy(bus_dma_tag_t dmat)
218 bus_dma_tag_t dmat_copy, parent;
225 if (dmat->map_count != 0) {
229 while (dmat != NULL) {
230 parent = (bus_dma_tag_t)dmat->common.parent;
231 atomic_subtract_int(&dmat->common.ref_count, 1);
232 if (dmat->common.ref_count == 0) {
233 if (dmat->segments != NULL)
234 free(dmat->segments, M_DEVBUF);
235 free(dmat, M_DEVBUF);
237 * Last reference count, so
238 * release our reference
239 * count on our parent.
247 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
252 * Allocate a handle for mapping from kva/uva/physical
253 * address space into bus device space.
256 bounce_bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
258 struct bounce_zone *bz;
259 int error, maxpages, pages;
263 if (dmat->segments == NULL) {
264 dmat->segments = (bus_dma_segment_t *)malloc(
265 sizeof(bus_dma_segment_t) * dmat->common.nsegments,
267 if (dmat->segments == NULL) {
268 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
269 __func__, dmat, ENOMEM);
275 * Bouncing might be required if the driver asks for an active
276 * exclusion region, a data alignment that is stricter than 1, and/or
277 * an active address boundary.
279 if (dmat->bounce_flags & BUS_DMA_COULD_BOUNCE) {
281 if (dmat->bounce_zone == NULL) {
282 if ((error = alloc_bounce_zone(dmat)) != 0)
285 bz = dmat->bounce_zone;
287 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
290 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
291 __func__, dmat, ENOMEM);
295 /* Initialize the new map */
296 STAILQ_INIT(&((*mapp)->bpages));
299 * Attempt to add pages to our pool on a per-instance
300 * basis up to a sane limit.
302 if (dmat->common.alignment > 1)
303 maxpages = MAX_BPAGES;
305 maxpages = MIN(MAX_BPAGES, Maxmem -
306 atop(dmat->common.lowaddr));
307 if ((dmat->bounce_flags & BUS_DMA_MIN_ALLOC_COMP) == 0 ||
308 (bz->map_count > 0 && bz->total_bpages < maxpages)) {
309 pages = MAX(atop(dmat->common.maxsize), 1);
310 pages = MIN(maxpages - bz->total_bpages, pages);
311 pages = MAX(pages, 1);
312 if (alloc_bounce_pages(dmat, pages) < pages)
314 if ((dmat->bounce_flags & BUS_DMA_MIN_ALLOC_COMP)
317 dmat->bounce_flags |=
318 BUS_DMA_MIN_ALLOC_COMP;
329 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
330 __func__, dmat, dmat->common.flags, error);
335 * Destroy a handle for mapping from kva/uva/physical
336 * address space into bus device space.
339 bounce_bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
342 if (map != NULL && map != &nobounce_dmamap) {
343 if (STAILQ_FIRST(&map->bpages) != NULL) {
344 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
345 __func__, dmat, EBUSY);
348 if (dmat->bounce_zone)
349 dmat->bounce_zone->map_count--;
353 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
359 * Allocate a piece of memory that can be efficiently mapped into
360 * bus device space based on the constraints lited in the dma tag.
361 * A dmamap to for use with dmamap_load is also allocated.
364 bounce_bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
369 * This bus_dma implementation requires IO-Coherent architecutre.
370 * If IO-Coherency is not guaranteed, the BUS_DMA_COHERENT flag has
371 * to be implented using non-cacheable memory.
377 if (flags & BUS_DMA_NOWAIT)
382 /* If we succeed, no mapping/bouncing will be required */
385 if (dmat->segments == NULL) {
386 dmat->segments = (bus_dma_segment_t *)malloc(
387 sizeof(bus_dma_segment_t) * dmat->common.nsegments,
389 if (dmat->segments == NULL) {
390 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
391 __func__, dmat, dmat->common.flags, ENOMEM);
395 if (flags & BUS_DMA_ZERO)
397 if (flags & BUS_DMA_NOCACHE)
398 attr = VM_MEMATTR_UNCACHEABLE;
400 attr = VM_MEMATTR_DEFAULT;
404 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
405 * alignment guarantees of malloc need to be nailed down, and the
406 * code below should be rewritten to take that into account.
408 * In the meantime, we'll warn the user if malloc gets it wrong.
410 if ((dmat->common.maxsize <= PAGE_SIZE) &&
411 (dmat->common.alignment < dmat->common.maxsize) &&
412 dmat->common.lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
413 attr == VM_MEMATTR_DEFAULT) {
414 *vaddr = malloc(dmat->common.maxsize, M_DEVBUF, mflags);
415 } else if (dmat->common.nsegments >= btoc(dmat->common.maxsize) &&
416 dmat->common.alignment <= PAGE_SIZE &&
417 (dmat->common.boundary == 0 ||
418 dmat->common.boundary >= dmat->common.lowaddr)) {
419 /* Page-based multi-segment allocations allowed */
420 *vaddr = (void *)kmem_alloc_attr(kernel_arena,
421 dmat->common.maxsize, mflags, 0ul, dmat->common.lowaddr,
423 dmat->bounce_flags |= BUS_DMA_KMEM_ALLOC;
425 *vaddr = (void *)kmem_alloc_contig(kernel_arena,
426 dmat->common.maxsize, mflags, 0ul, dmat->common.lowaddr,
427 dmat->common.alignment != 0 ? dmat->common.alignment : 1ul,
428 dmat->common.boundary, attr);
429 dmat->bounce_flags |= BUS_DMA_KMEM_ALLOC;
431 if (*vaddr == NULL) {
432 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
433 __func__, dmat, dmat->common.flags, ENOMEM);
435 } else if (vtophys(*vaddr) & (dmat->common.alignment - 1)) {
436 printf("bus_dmamem_alloc failed to align memory properly.\n");
438 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
439 __func__, dmat, dmat->common.flags, 0);
444 * Free a piece of memory and it's allociated dmamap, that was allocated
445 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
448 bounce_bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
451 * dmamem does not need to be bounced, so the map should be
452 * NULL and the BUS_DMA_KMEM_ALLOC flag cleared if malloc()
453 * was used and set if kmem_alloc_contig() was used.
456 panic("bus_dmamem_free: Invalid map freed\n");
457 if ((dmat->bounce_flags & BUS_DMA_KMEM_ALLOC) == 0)
458 free(vaddr, M_DEVBUF);
460 kmem_free(kernel_arena, (vm_offset_t)vaddr,
461 dmat->common.maxsize);
462 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat,
467 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
468 bus_size_t buflen, int flags)
473 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
475 * Count the number of bounce pages
476 * needed in order to complete this transfer
479 while (buflen != 0) {
480 sgsize = MIN(buflen, dmat->common.maxsegsz);
481 if (bus_dma_run_filter(&dmat->common, curaddr)) {
483 PAGE_SIZE - (curaddr & PAGE_MASK));
489 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
494 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
495 void *buf, bus_size_t buflen, int flags)
498 vm_offset_t vendaddr;
502 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
503 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
504 "alignment= %d", dmat->common.lowaddr,
505 ptoa((vm_paddr_t)Maxmem),
506 dmat->common.boundary, dmat->common.alignment);
507 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
508 map, &nobounce_dmamap, map->pagesneeded);
510 * Count the number of bounce pages
511 * needed in order to complete this transfer
513 vaddr = (vm_offset_t)buf;
514 vendaddr = (vm_offset_t)buf + buflen;
516 while (vaddr < vendaddr) {
517 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
518 if (pmap == kernel_pmap)
519 paddr = pmap_kextract(vaddr);
521 paddr = pmap_extract(pmap, vaddr);
522 if (bus_dma_run_filter(&dmat->common, paddr) != 0) {
523 sg_len = roundup2(sg_len,
524 dmat->common.alignment);
529 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
534 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
537 /* Reserve Necessary Bounce Pages */
538 mtx_lock(&bounce_lock);
539 if (flags & BUS_DMA_NOWAIT) {
540 if (reserve_bounce_pages(dmat, map, 0) != 0) {
541 mtx_unlock(&bounce_lock);
545 if (reserve_bounce_pages(dmat, map, 1) != 0) {
546 /* Queue us for resources */
547 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
548 mtx_unlock(&bounce_lock);
549 return (EINPROGRESS);
552 mtx_unlock(&bounce_lock);
558 * Add a single contiguous physical range to the segment list.
561 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
562 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
564 bus_addr_t baddr, bmask;
568 * Make sure we don't cross any boundaries.
570 bmask = ~(dmat->common.boundary - 1);
571 if (dmat->common.boundary > 0) {
572 baddr = (curaddr + dmat->common.boundary) & bmask;
573 if (sgsize > (baddr - curaddr))
574 sgsize = (baddr - curaddr);
578 * Insert chunk into a segment, coalescing with
579 * previous segment if possible.
584 segs[seg].ds_addr = curaddr;
585 segs[seg].ds_len = sgsize;
587 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
588 (segs[seg].ds_len + sgsize) <= dmat->common.maxsegsz &&
589 (dmat->common.boundary == 0 ||
590 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
591 segs[seg].ds_len += sgsize;
593 if (++seg >= dmat->common.nsegments)
595 segs[seg].ds_addr = curaddr;
596 segs[seg].ds_len = sgsize;
604 * Utility function to load a physical buffer. segp contains
605 * the starting segment on entrace, and the ending segment on exit.
608 bounce_bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
609 vm_paddr_t buf, bus_size_t buflen, int flags, bus_dma_segment_t *segs,
617 map = &nobounce_dmamap;
620 segs = dmat->segments;
622 if ((dmat->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0) {
623 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
624 if (map->pagesneeded != 0) {
625 error = _bus_dmamap_reserve_pages(dmat, map, flags);
633 sgsize = MIN(buflen, dmat->common.maxsegsz);
634 if (((dmat->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0) &&
635 map->pagesneeded != 0 &&
636 bus_dma_run_filter(&dmat->common, curaddr)) {
637 sgsize = MIN(sgsize, PAGE_SIZE - (curaddr & PAGE_MASK));
638 curaddr = add_bounce_page(dmat, map, 0, curaddr,
641 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
652 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
656 * Utility function to load a linear buffer. segp contains
657 * the starting segment on entrace, and the ending segment on exit.
660 bounce_bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
661 bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
664 bus_size_t sgsize, max_sgsize;
666 vm_offset_t kvaddr, vaddr;
670 map = &nobounce_dmamap;
673 segs = dmat->segments;
675 if ((dmat->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0) {
676 _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
677 if (map->pagesneeded != 0) {
678 error = _bus_dmamap_reserve_pages(dmat, map, flags);
684 vaddr = (vm_offset_t)buf;
687 * Get the physical address for this segment.
689 if (pmap == kernel_pmap) {
690 curaddr = pmap_kextract(vaddr);
693 curaddr = pmap_extract(pmap, vaddr);
698 * Compute the segment size, and adjust counts.
700 max_sgsize = MIN(buflen, dmat->common.maxsegsz);
701 sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
702 if (((dmat->bounce_flags & BUS_DMA_COULD_BOUNCE) != 0) &&
703 map->pagesneeded != 0 &&
704 bus_dma_run_filter(&dmat->common, curaddr)) {
705 sgsize = roundup2(sgsize, dmat->common.alignment);
706 sgsize = MIN(sgsize, max_sgsize);
707 curaddr = add_bounce_page(dmat, map, kvaddr, curaddr,
710 sgsize = MIN(sgsize, max_sgsize);
712 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
723 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
727 bounce_bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
728 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
735 map->callback = callback;
736 map->callback_arg = callback_arg;
739 static bus_dma_segment_t *
740 bounce_bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
741 bus_dma_segment_t *segs, int nsegs, int error)
745 segs = dmat->segments;
750 * Release the mapping held by map.
753 bounce_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
755 struct bounce_page *bpage;
757 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
758 STAILQ_REMOVE_HEAD(&map->bpages, links);
759 free_bounce_page(dmat, bpage);
764 bounce_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map,
767 struct bounce_page *bpage;
768 vm_offset_t datavaddr, tempvaddr;
770 if ((bpage = STAILQ_FIRST(&map->bpages)) == NULL)
775 * This bus_dma implementation requires IO-Coherent architecutre.
776 * If IO-Coherency is not guaranteed, cache operations have to be
777 * added to this function.
780 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
781 "performing bounce", __func__, dmat, dmat->common.flags, op);
783 if ((op & BUS_DMASYNC_PREWRITE) != 0) {
784 while (bpage != NULL) {
786 datavaddr = bpage->datavaddr;
787 if (datavaddr == 0) {
789 pmap_quick_enter_page(bpage->datapage);
790 datavaddr = tempvaddr | bpage->dataoffs;
793 bcopy((void *)datavaddr,
794 (void *)bpage->vaddr, bpage->datacount);
797 pmap_quick_remove_page(tempvaddr);
798 bpage = STAILQ_NEXT(bpage, links);
800 dmat->bounce_zone->total_bounced++;
803 if ((op & BUS_DMASYNC_POSTREAD) != 0) {
804 while (bpage != NULL) {
806 datavaddr = bpage->datavaddr;
807 if (datavaddr == 0) {
809 pmap_quick_enter_page(bpage->datapage);
810 datavaddr = tempvaddr | bpage->dataoffs;
813 bcopy((void *)bpage->vaddr,
814 (void *)datavaddr, bpage->datacount);
817 pmap_quick_remove_page(tempvaddr);
818 bpage = STAILQ_NEXT(bpage, links);
820 dmat->bounce_zone->total_bounced++;
825 init_bounce_pages(void *dummy __unused)
829 STAILQ_INIT(&bounce_zone_list);
830 STAILQ_INIT(&bounce_map_waitinglist);
831 STAILQ_INIT(&bounce_map_callbacklist);
832 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
834 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
836 static struct sysctl_ctx_list *
837 busdma_sysctl_tree(struct bounce_zone *bz)
839 return (&bz->sysctl_tree);
842 static struct sysctl_oid *
843 busdma_sysctl_tree_top(struct bounce_zone *bz)
845 return (bz->sysctl_tree_top);
849 alloc_bounce_zone(bus_dma_tag_t dmat)
851 struct bounce_zone *bz;
853 /* Check to see if we already have a suitable zone */
854 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
855 if ((dmat->common.alignment <= bz->alignment) &&
856 (dmat->common.lowaddr >= bz->lowaddr)) {
857 dmat->bounce_zone = bz;
862 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
863 M_NOWAIT | M_ZERO)) == NULL)
866 STAILQ_INIT(&bz->bounce_page_list);
868 bz->reserved_bpages = 0;
869 bz->active_bpages = 0;
870 bz->lowaddr = dmat->common.lowaddr;
871 bz->alignment = MAX(dmat->common.alignment, PAGE_SIZE);
873 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
875 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
876 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
877 dmat->bounce_zone = bz;
879 sysctl_ctx_init(&bz->sysctl_tree);
880 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
881 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
883 if (bz->sysctl_tree_top == NULL) {
884 sysctl_ctx_free(&bz->sysctl_tree);
885 return (0); /* XXX error code? */
888 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
889 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
890 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
891 "Total bounce pages");
892 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
893 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
894 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
895 "Free bounce pages");
896 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
897 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
898 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
899 "Reserved bounce pages");
900 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
901 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
902 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
903 "Active bounce pages");
904 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
905 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
906 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
907 "Total bounce requests");
908 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
909 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
910 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
911 "Total bounce requests that were deferred");
912 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
913 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
914 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
915 SYSCTL_ADD_UAUTO(busdma_sysctl_tree(bz),
916 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
917 "alignment", CTLFLAG_RD, &bz->alignment, "");
923 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
925 struct bounce_zone *bz;
928 bz = dmat->bounce_zone;
930 while (numpages > 0) {
931 struct bounce_page *bpage;
933 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
938 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
939 M_NOWAIT, 0ul, bz->lowaddr, PAGE_SIZE, 0);
940 if (bpage->vaddr == 0) {
941 free(bpage, M_DEVBUF);
944 bpage->busaddr = pmap_kextract(bpage->vaddr);
945 mtx_lock(&bounce_lock);
946 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
950 mtx_unlock(&bounce_lock);
958 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
960 struct bounce_zone *bz;
963 mtx_assert(&bounce_lock, MA_OWNED);
964 bz = dmat->bounce_zone;
965 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
966 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
967 return (map->pagesneeded - (map->pagesreserved + pages));
968 bz->free_bpages -= pages;
969 bz->reserved_bpages += pages;
970 map->pagesreserved += pages;
971 pages = map->pagesneeded - map->pagesreserved;
977 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
978 bus_addr_t addr, bus_size_t size)
980 struct bounce_zone *bz;
981 struct bounce_page *bpage;
983 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
984 KASSERT(map != NULL && map != &nobounce_dmamap,
985 ("add_bounce_page: bad map %p", map));
987 bz = dmat->bounce_zone;
988 if (map->pagesneeded == 0)
989 panic("add_bounce_page: map doesn't need any pages");
992 if (map->pagesreserved == 0)
993 panic("add_bounce_page: map doesn't need any pages");
994 map->pagesreserved--;
996 mtx_lock(&bounce_lock);
997 bpage = STAILQ_FIRST(&bz->bounce_page_list);
999 panic("add_bounce_page: free page list is empty");
1001 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1002 bz->reserved_bpages--;
1003 bz->active_bpages++;
1004 mtx_unlock(&bounce_lock);
1006 if (dmat->common.flags & BUS_DMA_KEEP_PG_OFFSET) {
1007 /* Page offset needs to be preserved. */
1008 bpage->vaddr |= addr & PAGE_MASK;
1009 bpage->busaddr |= addr & PAGE_MASK;
1011 bpage->datavaddr = vaddr;
1012 /* PHYS_TO_VM_PAGE() will truncate unaligned addresses. */
1013 bpage->datapage = PHYS_TO_VM_PAGE(addr);
1014 bpage->dataoffs = addr & PAGE_MASK;
1015 bpage->datacount = size;
1016 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1017 return (bpage->busaddr);
1021 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1023 struct bus_dmamap *map;
1024 struct bounce_zone *bz;
1026 bz = dmat->bounce_zone;
1027 bpage->datavaddr = 0;
1028 bpage->datacount = 0;
1029 if (dmat->common.flags & BUS_DMA_KEEP_PG_OFFSET) {
1031 * Reset the bounce page to start at offset 0. Other uses
1032 * of this bounce page may need to store a full page of
1033 * data and/or assume it starts on a page boundary.
1035 bpage->vaddr &= ~PAGE_MASK;
1036 bpage->busaddr &= ~PAGE_MASK;
1039 mtx_lock(&bounce_lock);
1040 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1042 bz->active_bpages--;
1043 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1044 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1045 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1046 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1048 busdma_swi_pending = 1;
1049 bz->total_deferred++;
1050 swi_sched(vm_ih, 0);
1053 mtx_unlock(&bounce_lock);
1060 struct bus_dmamap *map;
1062 mtx_lock(&bounce_lock);
1063 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1064 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1065 mtx_unlock(&bounce_lock);
1067 (dmat->common.lockfunc)(dmat->common.lockfuncarg, BUS_DMA_LOCK);
1068 bus_dmamap_load_mem(map->dmat, map, &map->mem,
1069 map->callback, map->callback_arg, BUS_DMA_WAITOK);
1070 (dmat->common.lockfunc)(dmat->common.lockfuncarg,
1072 mtx_lock(&bounce_lock);
1074 mtx_unlock(&bounce_lock);
1077 struct bus_dma_impl bus_dma_bounce_impl = {
1078 .tag_create = bounce_bus_dma_tag_create,
1079 .tag_destroy = bounce_bus_dma_tag_destroy,
1080 .map_create = bounce_bus_dmamap_create,
1081 .map_destroy = bounce_bus_dmamap_destroy,
1082 .mem_alloc = bounce_bus_dmamem_alloc,
1083 .mem_free = bounce_bus_dmamem_free,
1084 .load_phys = bounce_bus_dmamap_load_phys,
1085 .load_buffer = bounce_bus_dmamap_load_buffer,
1086 .load_ma = bus_dmamap_load_ma_triv,
1087 .map_waitok = bounce_bus_dmamap_waitok,
1088 .map_complete = bounce_bus_dmamap_complete,
1089 .map_unload = bounce_bus_dmamap_unload,
1090 .map_sync = bounce_bus_dmamap_sync