2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 2015-2016 The FreeBSD Foundation
6 * Portions of this software were developed by Andrew Turner
7 * under sponsorship of the FreeBSD Foundation.
9 * Portions of this software were developed by Semihalf
10 * under sponsorship of the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification, immediately at the beginning of the file.
18 * 2. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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 <machine/bus_dma_impl.h>
59 #define MAX_BPAGES 4096
62 BF_COULD_BOUNCE = 0x01,
63 BF_MIN_ALLOC_COMP = 0x02,
72 struct bus_dma_tag_common common;
75 bus_dma_segment_t *segments;
76 struct bounce_zone *bounce_zone;
79 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
83 vm_offset_t vaddr; /* kva of client data */
84 bus_addr_t paddr; /* physical address */
85 vm_page_t pages; /* starting page of client data */
86 bus_size_t datacount; /* client data count */
90 STAILQ_HEAD(, bounce_page) bpages;
95 bus_dmamap_callback_t *callback;
97 __sbintime_t queued_time;
98 STAILQ_ENTRY(bus_dmamap) links;
100 #define DMAMAP_COULD_BOUNCE (1 << 0)
101 #define DMAMAP_FROM_DMAMEM (1 << 1)
103 struct sync_list slist[];
106 static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
107 pmap_t pmap, void *buf, bus_size_t buflen, int flags);
108 static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
109 vm_paddr_t buf, bus_size_t buflen, int flags);
111 static MALLOC_DEFINE(M_BUSDMA, "busdma", "busdma metadata");
113 #define dmat_alignment(dmat) ((dmat)->common.alignment)
114 #define dmat_flags(dmat) ((dmat)->common.flags)
115 #define dmat_highaddr(dmat) ((dmat)->common.highaddr)
116 #define dmat_lowaddr(dmat) ((dmat)->common.lowaddr)
117 #define dmat_lockfunc(dmat) ((dmat)->common.lockfunc)
118 #define dmat_lockfuncarg(dmat) ((dmat)->common.lockfuncarg)
120 #include "../../kern/subr_busdma_bounce.c"
123 * Allocate a device specific dma_tag.
126 bounce_bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
127 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
128 bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
129 bus_dma_lock_t *lockfunc, void *lockfuncarg, bus_dma_tag_t *dmat)
131 bus_dma_tag_t newtag;
135 error = common_bus_dma_tag_create(parent != NULL ? &parent->common :
136 NULL, alignment, boundary, lowaddr, highaddr, maxsize, nsegments,
137 maxsegsz, flags, lockfunc, lockfuncarg,
138 sizeof (struct bus_dma_tag), (void **)&newtag);
142 newtag->common.impl = &bus_dma_bounce_impl;
143 newtag->map_count = 0;
144 newtag->segments = NULL;
146 if ((flags & BUS_DMA_COHERENT) != 0)
147 newtag->bounce_flags |= BF_COHERENT;
149 if (parent != NULL) {
150 if ((parent->bounce_flags & BF_COULD_BOUNCE) != 0)
151 newtag->bounce_flags |= BF_COULD_BOUNCE;
153 /* Copy some flags from the parent */
154 newtag->bounce_flags |= parent->bounce_flags & BF_COHERENT;
157 if (newtag->common.lowaddr < ptoa((vm_paddr_t)Maxmem) ||
158 newtag->common.alignment > 1)
159 newtag->bounce_flags |= BF_COULD_BOUNCE;
161 if (((newtag->bounce_flags & BF_COULD_BOUNCE) != 0) &&
162 (flags & BUS_DMA_ALLOCNOW) != 0) {
163 struct bounce_zone *bz;
166 if ((error = alloc_bounce_zone(newtag)) != 0) {
167 free(newtag, M_DEVBUF);
170 bz = newtag->bounce_zone;
172 if (ptoa(bz->total_bpages) < maxsize) {
175 pages = atop(round_page(maxsize)) - bz->total_bpages;
177 /* Add pages to our bounce pool */
178 if (alloc_bounce_pages(newtag, pages) < pages)
181 /* Performed initial allocation */
182 newtag->bounce_flags |= BF_MIN_ALLOC_COMP;
187 free(newtag, M_DEVBUF);
190 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
191 __func__, newtag, (newtag != NULL ? newtag->common.flags : 0),
197 bounce_bus_dma_tag_destroy(bus_dma_tag_t dmat)
202 if (dmat->map_count != 0) {
206 if (dmat->segments != NULL)
207 free(dmat->segments, M_DEVBUF);
208 free(dmat, M_DEVBUF);
211 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat, error);
216 alloc_dmamap(bus_dma_tag_t dmat, int flags)
221 mapsize = sizeof(*map);
222 mapsize += sizeof(struct sync_list) * dmat->common.nsegments;
223 map = malloc(mapsize, M_DEVBUF, flags | M_ZERO);
227 /* Initialize the new map */
228 STAILQ_INIT(&map->bpages);
234 * Allocate a handle for mapping from kva/uva/physical
235 * address space into bus device space.
238 bounce_bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
240 struct bounce_zone *bz;
241 int error, maxpages, pages;
245 if (dmat->segments == NULL) {
246 dmat->segments = (bus_dma_segment_t *)malloc(
247 sizeof(bus_dma_segment_t) * dmat->common.nsegments,
249 if (dmat->segments == NULL) {
250 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
251 __func__, dmat, ENOMEM);
256 *mapp = alloc_dmamap(dmat, M_NOWAIT);
258 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
259 __func__, dmat, ENOMEM);
264 * Bouncing might be required if the driver asks for an active
265 * exclusion region, a data alignment that is stricter than 1, and/or
266 * an active address boundary.
268 if (dmat->bounce_flags & BF_COULD_BOUNCE) {
270 if (dmat->bounce_zone == NULL) {
271 if ((error = alloc_bounce_zone(dmat)) != 0) {
272 free(*mapp, M_DEVBUF);
276 bz = dmat->bounce_zone;
278 (*mapp)->flags = DMAMAP_COULD_BOUNCE;
281 * Attempt to add pages to our pool on a per-instance
282 * basis up to a sane limit.
284 if (dmat->common.alignment > 1)
285 maxpages = MAX_BPAGES;
287 maxpages = MIN(MAX_BPAGES, Maxmem -
288 atop(dmat->common.lowaddr));
289 if ((dmat->bounce_flags & BF_MIN_ALLOC_COMP) == 0 ||
290 (bz->map_count > 0 && bz->total_bpages < maxpages)) {
291 pages = MAX(atop(dmat->common.maxsize), 1);
292 pages = MIN(maxpages - bz->total_bpages, pages);
293 pages = MAX(pages, 1);
294 if (alloc_bounce_pages(dmat, pages) < pages)
296 if ((dmat->bounce_flags & BF_MIN_ALLOC_COMP)
299 dmat->bounce_flags |=
310 free(*mapp, M_DEVBUF);
311 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
312 __func__, dmat, dmat->common.flags, error);
317 * Destroy a handle for mapping from kva/uva/physical
318 * address space into bus device space.
321 bounce_bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
324 /* Check we are destroying the correct map type */
325 if ((map->flags & DMAMAP_FROM_DMAMEM) != 0)
326 panic("bounce_bus_dmamap_destroy: Invalid map freed\n");
328 if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) {
329 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, EBUSY);
332 if (dmat->bounce_zone) {
333 KASSERT((map->flags & DMAMAP_COULD_BOUNCE) != 0,
334 ("%s: Bounce zone when cannot bounce", __func__));
335 dmat->bounce_zone->map_count--;
339 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
344 * Allocate a piece of memory that can be efficiently mapped into
345 * bus device space based on the constraints lited in the dma tag.
346 * A dmamap to for use with dmamap_load is also allocated.
349 bounce_bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
354 * This bus_dma implementation requires IO-Coherent architecutre.
355 * If IO-Coherency is not guaranteed, the BUS_DMA_COHERENT flag has
356 * to be implented using non-cacheable memory.
362 if (flags & BUS_DMA_NOWAIT)
367 if (dmat->segments == NULL) {
368 dmat->segments = (bus_dma_segment_t *)malloc(
369 sizeof(bus_dma_segment_t) * dmat->common.nsegments,
371 if (dmat->segments == NULL) {
372 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
373 __func__, dmat, dmat->common.flags, ENOMEM);
377 if (flags & BUS_DMA_ZERO)
379 if (flags & BUS_DMA_NOCACHE)
380 attr = VM_MEMATTR_UNCACHEABLE;
381 else if ((flags & BUS_DMA_COHERENT) != 0 &&
382 (dmat->bounce_flags & BF_COHERENT) == 0)
384 * If we have a non-coherent tag, and are trying to allocate
385 * a coherent block of memory it needs to be uncached.
387 attr = VM_MEMATTR_UNCACHEABLE;
389 attr = VM_MEMATTR_DEFAULT;
392 * Create the map, but don't set the could bounce flag as
393 * this allocation should never bounce;
395 *mapp = alloc_dmamap(dmat, mflags);
397 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
398 __func__, dmat, dmat->common.flags, ENOMEM);
401 (*mapp)->flags = DMAMAP_FROM_DMAMEM;
404 * Allocate the buffer from the malloc(9) allocator if...
405 * - It's small enough to fit into a single power of two sized bucket.
406 * - The alignment is less than or equal to the maximum size
407 * - The low address requirement is fulfilled.
408 * else allocate non-contiguous pages if...
409 * - The page count that could get allocated doesn't exceed
410 * nsegments also when the maximum segment size is less
412 * - The alignment constraint isn't larger than a page boundary.
413 * - There are no boundary-crossing constraints.
414 * else allocate a block of contiguous pages because one or more of the
415 * constraints is something that only the contig allocator can fulfill.
417 * NOTE: The (dmat->common.alignment <= dmat->maxsize) check
418 * below is just a quick hack. The exact alignment guarantees
419 * of malloc(9) need to be nailed down, and the code below
420 * should be rewritten to take that into account.
422 * In the meantime warn the user if malloc gets it wrong.
424 if ((dmat->common.maxsize <= PAGE_SIZE) &&
425 (dmat->common.alignment <= dmat->common.maxsize) &&
426 dmat->common.lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
427 attr == VM_MEMATTR_DEFAULT) {
428 *vaddr = malloc(dmat->common.maxsize, M_DEVBUF, mflags);
429 } else if (dmat->common.nsegments >=
430 howmany(dmat->common.maxsize, MIN(dmat->common.maxsegsz, PAGE_SIZE)) &&
431 dmat->common.alignment <= PAGE_SIZE &&
432 (dmat->common.boundary % PAGE_SIZE) == 0) {
433 /* Page-based multi-segment allocations allowed */
434 *vaddr = kmem_alloc_attr(dmat->common.maxsize, mflags,
435 0ul, dmat->common.lowaddr, attr);
436 dmat->bounce_flags |= BF_KMEM_ALLOC;
438 *vaddr = kmem_alloc_contig(dmat->common.maxsize, mflags,
439 0ul, dmat->common.lowaddr, dmat->common.alignment != 0 ?
440 dmat->common.alignment : 1ul, dmat->common.boundary, attr);
441 dmat->bounce_flags |= BF_KMEM_ALLOC;
443 if (*vaddr == NULL) {
444 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
445 __func__, dmat, dmat->common.flags, ENOMEM);
446 free(*mapp, M_DEVBUF);
448 } else if (!vm_addr_align_ok(vtophys(*vaddr), dmat->common.alignment)) {
449 printf("bus_dmamem_alloc failed to align memory properly.\n");
452 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
453 __func__, dmat, dmat->common.flags, 0);
458 * Free a piece of memory and it's allociated dmamap, that was allocated
459 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
462 bounce_bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
466 * Check the map came from bounce_bus_dmamem_alloc, so the map
467 * should be NULL and the BF_KMEM_ALLOC flag cleared if malloc()
468 * was used and set if kmem_alloc_contig() was used.
470 if ((map->flags & DMAMAP_FROM_DMAMEM) == 0)
471 panic("bus_dmamem_free: Invalid map freed\n");
472 if ((dmat->bounce_flags & BF_KMEM_ALLOC) == 0)
473 free(vaddr, M_DEVBUF);
475 kmem_free(vaddr, dmat->common.maxsize);
478 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat,
483 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
484 bus_size_t buflen, int flags)
489 if ((map->flags & DMAMAP_COULD_BOUNCE) != 0 && map->pagesneeded == 0) {
491 * Count the number of bounce pages
492 * needed in order to complete this transfer
495 while (buflen != 0) {
496 sgsize = MIN(buflen, dmat->common.maxsegsz);
497 if (addr_needs_bounce(dmat, curaddr)) {
499 PAGE_SIZE - (curaddr & PAGE_MASK));
505 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
510 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
511 void *buf, bus_size_t buflen, int flags)
514 vm_offset_t vendaddr;
518 if ((map->flags & DMAMAP_COULD_BOUNCE) != 0 && map->pagesneeded == 0) {
519 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
520 "alignment= %d", dmat->common.lowaddr,
521 ptoa((vm_paddr_t)Maxmem),
522 dmat->common.boundary, dmat->common.alignment);
523 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d", map,
526 * Count the number of bounce pages
527 * needed in order to complete this transfer
529 vaddr = (vm_offset_t)buf;
530 vendaddr = (vm_offset_t)buf + buflen;
532 while (vaddr < vendaddr) {
533 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
534 if (pmap == kernel_pmap)
535 paddr = pmap_kextract(vaddr);
537 paddr = pmap_extract(pmap, vaddr);
538 if (addr_needs_bounce(dmat, paddr)) {
539 sg_len = roundup2(sg_len,
540 dmat->common.alignment);
545 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
550 * Add a single contiguous physical range to the segment list.
553 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
554 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
559 * Make sure we don't cross any boundaries.
561 if (!vm_addr_bound_ok(curaddr, sgsize, dmat->common.boundary))
562 sgsize = roundup2(curaddr, dmat->common.boundary) - curaddr;
565 * Insert chunk into a segment, coalescing with
566 * previous segment if possible.
571 segs[seg].ds_addr = curaddr;
572 segs[seg].ds_len = sgsize;
574 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
575 (segs[seg].ds_len + sgsize) <= dmat->common.maxsegsz &&
576 vm_addr_bound_ok(segs[seg].ds_addr,
577 segs[seg].ds_len + sgsize, dmat->common.boundary))
578 segs[seg].ds_len += sgsize;
580 if (++seg >= dmat->common.nsegments)
582 segs[seg].ds_addr = curaddr;
583 segs[seg].ds_len = sgsize;
591 * Utility function to load a physical buffer. segp contains
592 * the starting segment on entrace, and the ending segment on exit.
595 bounce_bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
596 vm_paddr_t buf, bus_size_t buflen, int flags, bus_dma_segment_t *segs,
599 struct sync_list *sl;
601 bus_addr_t curaddr, sl_end;
605 segs = dmat->segments;
607 if ((dmat->bounce_flags & BF_COULD_BOUNCE) != 0) {
608 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
609 if (map->pagesneeded != 0) {
610 error = _bus_dmamap_reserve_pages(dmat, map, flags);
616 sl = map->slist + map->sync_count - 1;
621 sgsize = MIN(buflen, dmat->common.maxsegsz);
622 if (((dmat->bounce_flags & BF_COULD_BOUNCE) != 0) &&
623 map->pagesneeded != 0 &&
624 addr_needs_bounce(dmat, curaddr)) {
625 sgsize = MIN(sgsize, PAGE_SIZE - (curaddr & PAGE_MASK));
626 curaddr = add_bounce_page(dmat, map, 0, curaddr,
628 } else if ((dmat->bounce_flags & BF_COHERENT) == 0) {
629 if (map->sync_count > 0)
630 sl_end = sl->paddr + sl->datacount;
632 if (map->sync_count == 0 || curaddr != sl_end) {
633 if (++map->sync_count > dmat->common.nsegments)
638 sl->datacount = sgsize;
639 sl->pages = PHYS_TO_VM_PAGE(curaddr);
640 KASSERT(sl->pages != NULL,
641 ("%s: page at PA:0x%08lx is not in "
642 "vm_page_array", __func__, curaddr));
644 sl->datacount += sgsize;
646 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
657 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
661 * Utility function to load a linear buffer. segp contains
662 * the starting segment on entrace, and the ending segment on exit.
665 bounce_bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
666 bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
669 struct sync_list *sl;
670 bus_size_t sgsize, max_sgsize;
671 bus_addr_t curaddr, sl_pend;
672 vm_offset_t kvaddr, vaddr, sl_vend;
676 segs = dmat->segments;
678 if ((dmat->bounce_flags & BF_COULD_BOUNCE) != 0) {
679 _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
680 if (map->pagesneeded != 0) {
681 error = _bus_dmamap_reserve_pages(dmat, map, flags);
687 sl = map->slist + map->sync_count - 1;
688 vaddr = (vm_offset_t)buf;
694 * Get the physical address for this segment.
696 if (pmap == kernel_pmap) {
697 curaddr = pmap_kextract(vaddr);
700 curaddr = pmap_extract(pmap, vaddr);
705 * Compute the segment size, and adjust counts.
707 max_sgsize = MIN(buflen, dmat->common.maxsegsz);
708 sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
709 if (((dmat->bounce_flags & BF_COULD_BOUNCE) != 0) &&
710 map->pagesneeded != 0 &&
711 addr_needs_bounce(dmat, curaddr)) {
712 sgsize = roundup2(sgsize, dmat->common.alignment);
713 sgsize = MIN(sgsize, max_sgsize);
714 curaddr = add_bounce_page(dmat, map, kvaddr, curaddr,
716 } else if ((dmat->bounce_flags & BF_COHERENT) == 0) {
717 sgsize = MIN(sgsize, max_sgsize);
718 if (map->sync_count > 0) {
719 sl_pend = sl->paddr + sl->datacount;
720 sl_vend = sl->vaddr + sl->datacount;
723 if (map->sync_count == 0 ||
724 (kvaddr != 0 && kvaddr != sl_vend) ||
725 (curaddr != sl_pend)) {
726 if (++map->sync_count > dmat->common.nsegments)
734 sl->pages = PHYS_TO_VM_PAGE(curaddr);
735 KASSERT(sl->pages != NULL,
736 ("%s: page at PA:0x%08lx is not "
737 "in vm_page_array", __func__,
740 sl->datacount = sgsize;
742 sl->datacount += sgsize;
744 sgsize = MIN(sgsize, max_sgsize);
746 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
758 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
762 bounce_bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
763 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
766 if ((map->flags & DMAMAP_COULD_BOUNCE) == 0)
770 map->callback = callback;
771 map->callback_arg = callback_arg;
774 static bus_dma_segment_t *
775 bounce_bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
776 bus_dma_segment_t *segs, int nsegs, int error)
780 segs = dmat->segments;
785 * Release the mapping held by map.
788 bounce_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
790 free_bounce_pages(dmat, map);
795 dma_preread_safe(vm_offset_t va, vm_size_t size)
798 * Write back any partial cachelines immediately before and
799 * after the DMA region.
801 if (va & (dcache_line_size - 1))
802 cpu_dcache_wb_range(va, 1);
803 if ((va + size) & (dcache_line_size - 1))
804 cpu_dcache_wb_range(va + size, 1);
806 cpu_dcache_inv_range(va, size);
810 dma_dcache_sync(struct sync_list *sl, bus_dmasync_op_t op)
812 uint32_t len, offset;
815 vm_offset_t va, tempva;
818 offset = sl->paddr & PAGE_MASK;
820 size = sl->datacount;
823 for ( ; size != 0; size -= len, pa += len, offset = 0, ++m) {
825 if (sl->vaddr == 0) {
826 len = min(PAGE_SIZE - offset, size);
827 tempva = pmap_quick_enter_page(m);
828 va = tempva | offset;
829 KASSERT(pa == (VM_PAGE_TO_PHYS(m) | offset),
830 ("unexpected vm_page_t phys: 0x%16lx != 0x%16lx",
831 VM_PAGE_TO_PHYS(m) | offset, pa));
838 case BUS_DMASYNC_PREWRITE:
839 case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD:
840 cpu_dcache_wb_range(va, len);
842 case BUS_DMASYNC_PREREAD:
844 * An mbuf may start in the middle of a cacheline. There
845 * will be no cpu writes to the beginning of that line
846 * (which contains the mbuf header) while dma is in
847 * progress. Handle that case by doing a writeback of
848 * just the first cacheline before invalidating the
849 * overall buffer. Any mbuf in a chain may have this
850 * misalignment. Buffers which are not mbufs bounce if
851 * they are not aligned to a cacheline.
853 dma_preread_safe(va, len);
855 case BUS_DMASYNC_POSTREAD:
856 case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE:
857 cpu_dcache_inv_range(va, len);
860 panic("unsupported combination of sync operations: "
865 pmap_quick_remove_page(tempva);
870 bounce_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map,
873 struct bounce_page *bpage;
874 struct sync_list *sl, *end;
875 vm_offset_t datavaddr, tempvaddr;
877 if (op == BUS_DMASYNC_POSTWRITE)
880 if ((op & BUS_DMASYNC_POSTREAD) != 0) {
882 * Wait for any DMA operations to complete before the bcopy.
887 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
888 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
889 "performing bounce", __func__, dmat, dmat->common.flags,
892 if ((op & BUS_DMASYNC_PREWRITE) != 0) {
893 while (bpage != NULL) {
895 datavaddr = bpage->datavaddr;
896 if (datavaddr == 0) {
897 tempvaddr = pmap_quick_enter_page(
899 datavaddr = tempvaddr | bpage->dataoffs;
902 bcopy((void *)datavaddr,
903 (void *)bpage->vaddr, bpage->datacount);
905 pmap_quick_remove_page(tempvaddr);
906 if ((dmat->bounce_flags & BF_COHERENT) == 0)
907 cpu_dcache_wb_range(bpage->vaddr,
909 bpage = STAILQ_NEXT(bpage, links);
911 dmat->bounce_zone->total_bounced++;
912 } else if ((op & BUS_DMASYNC_PREREAD) != 0) {
913 while (bpage != NULL) {
914 if ((dmat->bounce_flags & BF_COHERENT) == 0)
915 cpu_dcache_wbinv_range(bpage->vaddr,
917 bpage = STAILQ_NEXT(bpage, links);
921 if ((op & BUS_DMASYNC_POSTREAD) != 0) {
922 while (bpage != NULL) {
923 if ((dmat->bounce_flags & BF_COHERENT) == 0)
924 cpu_dcache_inv_range(bpage->vaddr,
927 datavaddr = bpage->datavaddr;
928 if (datavaddr == 0) {
929 tempvaddr = pmap_quick_enter_page(
931 datavaddr = tempvaddr | bpage->dataoffs;
934 bcopy((void *)bpage->vaddr,
935 (void *)datavaddr, bpage->datacount);
938 pmap_quick_remove_page(tempvaddr);
939 bpage = STAILQ_NEXT(bpage, links);
941 dmat->bounce_zone->total_bounced++;
946 * Cache maintenance for normal (non-COHERENT non-bounce) buffers.
948 if (map->sync_count != 0) {
950 end = &map->slist[map->sync_count];
951 CTR3(KTR_BUSDMA, "%s: tag %p op 0x%x "
952 "performing sync", __func__, dmat, op);
954 for ( ; sl != end; ++sl)
955 dma_dcache_sync(sl, op);
958 if ((op & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) != 0) {
960 * Wait for the bcopy to complete before any DMA operations.
966 struct bus_dma_impl bus_dma_bounce_impl = {
967 .tag_create = bounce_bus_dma_tag_create,
968 .tag_destroy = bounce_bus_dma_tag_destroy,
969 .map_create = bounce_bus_dmamap_create,
970 .map_destroy = bounce_bus_dmamap_destroy,
971 .mem_alloc = bounce_bus_dmamem_alloc,
972 .mem_free = bounce_bus_dmamem_free,
973 .load_phys = bounce_bus_dmamap_load_phys,
974 .load_buffer = bounce_bus_dmamap_load_buffer,
975 .load_ma = bus_dmamap_load_ma_triv,
976 .map_waitok = bounce_bus_dmamap_waitok,
977 .map_complete = bounce_bus_dmamap_complete,
978 .map_unload = bounce_bus_dmamap_unload,
979 .map_sync = bounce_bus_dmamap_sync