2 * Copyright (c) 2012 Ian Lepore
3 * Copyright (c) 2004 Olivier Houchard
4 * Copyright (c) 2002 Peter Grehan
5 * Copyright (c) 1997, 1998 Justin T. Gibbs.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * From i386/busdma_machdep.c,v 1.26 2002/04/19 22:58:09 alfred
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * ARM bus dma support routines.
38 * XXX Things to investigate / fix some day...
39 * - What is the earliest that this API can be called? Could there be any
40 * fallout from changing the SYSINIT() order from SI_SUB_VM to SI_SUB_KMEM?
41 * - The manpage mentions the BUS_DMA_NOWAIT flag only in the context of the
42 * bus_dmamap_load() function. This code has historically (and still does)
43 * honor it in bus_dmamem_alloc(). If we got rid of that we could lose some
44 * error checking because some resource management calls would become WAITOK
45 * and thus "cannot fail."
46 * - The decisions made by _bus_dma_can_bounce() should be made once, at tag
47 * creation time, and the result stored in the tag.
48 * - It should be possible to take some shortcuts when mapping a buffer we know
49 * came from the uma(9) allocators based on what we know about such buffers
50 * (aligned, contiguous, etc).
51 * - The allocation of bounce pages could probably be cleaned up, then we could
52 * retire arm_remap_nocache().
55 #define _ARM32_BUS_DMA_PRIVATE
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/malloc.h>
60 #include <sys/busdma_bufalloc.h>
61 #include <sys/interrupt.h>
64 #include <sys/memdesc.h>
65 #include <sys/mutex.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_map.h>
78 #include <machine/atomic.h>
79 #include <machine/bus.h>
80 #include <machine/cpufunc.h>
81 #include <machine/md_var.h>
84 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
85 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
95 bus_dma_filter_t *filter;
103 bus_dma_lock_t *lockfunc;
106 * DMA range for this tag. If the page doesn't fall within
107 * one of these ranges, an error is returned. The caller
108 * may then decide what to do with the transfer. If the
109 * range pointer is NULL, it is ignored.
111 struct arm32_dma_range *ranges;
113 struct bounce_zone *bounce_zone;
115 * Most tags need one or two segments, and can use the local tagsegs
116 * array. For tags with a larger limit, we'll allocate a bigger array
119 bus_dma_segment_t *segments;
120 bus_dma_segment_t tagsegs[2];
124 vm_offset_t vaddr; /* kva of bounce buffer */
125 vm_offset_t vaddr_nocache; /* kva of bounce buffer uncached */
126 bus_addr_t busaddr; /* Physical address */
127 vm_offset_t datavaddr; /* kva of client data */
128 bus_addr_t dataaddr; /* client physical address */
129 bus_size_t datacount; /* client data count */
130 STAILQ_ENTRY(bounce_page) links;
134 vm_offset_t vaddr; /* kva of bounce buffer */
135 bus_addr_t busaddr; /* Physical address */
136 bus_size_t datacount; /* client data count */
139 int busdma_swi_pending;
142 STAILQ_ENTRY(bounce_zone) links;
143 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
151 bus_size_t alignment;
155 struct sysctl_ctx_list sysctl_tree;
156 struct sysctl_oid *sysctl_tree_top;
159 static struct mtx bounce_lock;
160 static int total_bpages;
161 static int busdma_zonecount;
162 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
164 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
165 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
166 "Total bounce pages");
168 #define DMAMAP_COHERENT 0x8
169 #define DMAMAP_CACHE_ALIGNED 0x10
172 struct bp_list bpages;
178 STAILQ_ENTRY(bus_dmamap) links;
179 bus_dmamap_callback_t *callback;
182 struct sync_list *slist;
185 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
186 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
188 static struct mtx busdma_mtx;
190 MTX_SYSINIT(busdma_mtx, &busdma_mtx, "busdma lock", MTX_DEF);
192 static void init_bounce_pages(void *dummy);
193 static int alloc_bounce_zone(bus_dma_tag_t dmat);
194 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
195 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
197 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
198 vm_offset_t vaddr, bus_addr_t addr,
200 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
202 /* Default tag, as most drivers provide no parent tag. */
203 bus_dma_tag_t arm_root_dma_tag;
206 * ----------------------------------------------------------------------------
207 * Begin block of code useful to transplant to other implementations.
210 static uma_zone_t dmamap_zone; /* Cache of struct bus_dmamap items */
212 static busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */
213 static busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */
216 * This is the ctor function passed to uma_zcreate() for the pool of dma maps.
217 * It'll need platform-specific changes if this code is copied.
220 dmamap_ctor(void *mem, int size, void *arg, int flags)
225 map = (bus_dmamap_t)mem;
226 dmat = (bus_dma_tag_t)arg;
232 STAILQ_INIT(&map->bpages);
238 * This is the dtor function passed to uma_zcreate() for the pool of dma maps.
239 * It may need platform-specific changes if this code is copied .
242 dmamap_dtor(void *mem, int size, void *arg)
246 map = (bus_dmamap_t)mem;
248 map->dmat->map_count--;
252 busdma_init(void *dummy)
255 /* Create a cache of maps for bus_dmamap_create(). */
256 dmamap_zone = uma_zcreate("dma maps", sizeof(struct bus_dmamap),
257 dmamap_ctor, dmamap_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
259 /* Create a cache of buffers in standard (cacheable) memory. */
260 standard_allocator = busdma_bufalloc_create("buffer",
261 arm_dcache_align, /* minimum_alignment */
262 NULL, /* uma_alloc func */
263 NULL, /* uma_free func */
264 0); /* uma_zcreate_flags */
267 * Create a cache of buffers in uncacheable memory, to implement the
268 * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag.
270 coherent_allocator = busdma_bufalloc_create("coherent",
271 arm_dcache_align, /* minimum_alignment */
272 busdma_bufalloc_alloc_uncacheable,
273 busdma_bufalloc_free_uncacheable,
274 0); /* uma_zcreate_flags */
278 * This init historically used SI_SUB_VM, but now the init code requires
279 * malloc(9) using M_DEVBUF memory, which is set up later than SI_SUB_VM, by
280 * SI_SUB_KMEM and SI_ORDER_SECOND, so we'll go right after that by using
281 * SI_SUB_KMEM and SI_ORDER_THIRD.
283 SYSINIT(busdma, SI_SUB_KMEM, SI_ORDER_THIRD, busdma_init, NULL);
286 * End block of code useful to transplant to other implementations.
287 * ----------------------------------------------------------------------------
291 * Return true if a match is made.
293 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
295 * If paddr is within the bounds of the dma tag then call the filter callback
296 * to check for a match, if there is no filter callback then assume a match.
299 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
306 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
307 || ((paddr & (dmat->alignment - 1)) != 0))
308 && (dmat->filter == NULL
309 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
313 } while (retval == 0 && dmat != NULL);
318 * This routine checks the exclusion zone constraints from a tag against the
319 * physical RAM available on the machine. If a tag specifies an exclusion zone
320 * but there's no RAM in that zone, then we avoid allocating resources to bounce
321 * a request, and we can use any memory allocator (as opposed to needing
322 * kmem_alloc_contig() just because it can allocate pages in an address range).
324 * Most tags have BUS_SPACE_MAXADDR or BUS_SPACE_MAXADDR_32BIT (they are the
325 * same value on 32-bit architectures) as their lowaddr constraint, and we can't
326 * possibly have RAM at an address higher than the highest address we can
327 * express, so we take a fast out.
330 _bus_dma_can_bounce(vm_offset_t lowaddr, vm_offset_t highaddr)
334 if (lowaddr >= BUS_SPACE_MAXADDR)
337 for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) {
338 if ((lowaddr >= phys_avail[i] && lowaddr <= phys_avail[i + 1])
339 || (lowaddr < phys_avail[i] &&
340 highaddr > phys_avail[i]))
346 static __inline struct arm32_dma_range *
347 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
350 struct arm32_dma_range *dr;
353 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
354 if (curaddr >= dr->dr_sysbase &&
355 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
362 * Convenience function for manipulating driver locks from busdma (during
363 * busdma_swi, for example). Drivers that don't provide their own locks
364 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
365 * non-mutex locking scheme don't have to use this at all.
368 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
372 dmtx = (struct mtx *)arg;
381 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
386 * dflt_lock should never get called. It gets put into the dma tag when
387 * lockfunc == NULL, which is only valid if the maps that are associated
388 * with the tag are meant to never be defered.
389 * XXX Should have a way to identify which driver is responsible here.
392 dflt_lock(void *arg, bus_dma_lock_op_t op)
395 panic("driver error: busdma dflt_lock called");
397 printf("DRIVER_ERROR: busdma dflt_lock called\n");
402 * Allocate a device specific dma_tag.
407 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
408 bus_size_t boundary, bus_addr_t lowaddr,
409 bus_addr_t highaddr, bus_dma_filter_t *filter,
410 void *filterarg, bus_size_t maxsize, int nsegments,
411 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
412 void *lockfuncarg, bus_dma_tag_t *dmat)
414 bus_dma_tag_t newtag;
416 /* Return a NULL tag on failure */
419 parent = arm_root_dma_tag;
421 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
422 if (newtag == NULL) {
423 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
424 __func__, newtag, 0, error);
428 newtag->parent = parent;
429 newtag->alignment = alignment ? alignment : 1;
430 newtag->boundary = boundary;
431 newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
432 newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
433 newtag->filter = filter;
434 newtag->filterarg = filterarg;
435 newtag->maxsize = maxsize;
436 newtag->nsegments = nsegments;
437 newtag->maxsegsz = maxsegsz;
438 newtag->flags = flags;
439 newtag->ref_count = 1; /* Count ourself */
440 newtag->map_count = 0;
441 newtag->ranges = bus_dma_get_range();
442 newtag->_nranges = bus_dma_get_range_nb();
443 if (lockfunc != NULL) {
444 newtag->lockfunc = lockfunc;
445 newtag->lockfuncarg = lockfuncarg;
447 newtag->lockfunc = dflt_lock;
448 newtag->lockfuncarg = NULL;
451 * If all the segments we need fit into the local tagsegs array, set the
452 * pointer now. Otherwise NULL the pointer and an array of segments
453 * will be allocated later, on first use. We don't pre-allocate now
454 * because some tags exist just to pass contraints to children in the
455 * device hierarchy, and they tend to use BUS_SPACE_UNRESTRICTED and we
456 * sure don't want to try to allocate an array for that.
458 if (newtag->nsegments <= nitems(newtag->tagsegs))
459 newtag->segments = newtag->tagsegs;
461 newtag->segments = NULL;
463 * Take into account any restrictions imposed by our parent tag
465 if (parent != NULL) {
466 newtag->lowaddr = min(parent->lowaddr, newtag->lowaddr);
467 newtag->highaddr = max(parent->highaddr, newtag->highaddr);
468 if (newtag->boundary == 0)
469 newtag->boundary = parent->boundary;
470 else if (parent->boundary != 0)
471 newtag->boundary = min(parent->boundary,
473 if ((newtag->filter != NULL) ||
474 ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
475 newtag->flags |= BUS_DMA_COULD_BOUNCE;
476 if (newtag->filter == NULL) {
478 * Short circuit looking at our parent directly
479 * since we have encapsulated all of its information
481 newtag->filter = parent->filter;
482 newtag->filterarg = parent->filterarg;
483 newtag->parent = parent->parent;
485 if (newtag->parent != NULL)
486 atomic_add_int(&parent->ref_count, 1);
488 if (_bus_dma_can_bounce(newtag->lowaddr, newtag->highaddr)
489 || newtag->alignment > 1)
490 newtag->flags |= BUS_DMA_COULD_BOUNCE;
492 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
493 (flags & BUS_DMA_ALLOCNOW) != 0) {
494 struct bounce_zone *bz;
498 if ((error = alloc_bounce_zone(newtag)) != 0) {
499 free(newtag, M_DEVBUF);
502 bz = newtag->bounce_zone;
504 if (ptoa(bz->total_bpages) < maxsize) {
507 pages = atop(maxsize) - bz->total_bpages;
509 /* Add pages to our bounce pool */
510 if (alloc_bounce_pages(newtag, pages) < pages)
513 /* Performed initial allocation */
514 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
516 newtag->bounce_zone = NULL;
518 free(newtag, M_DEVBUF);
521 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
522 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
528 bus_dma_tag_destroy(bus_dma_tag_t dmat)
531 bus_dma_tag_t dmat_copy = dmat;
536 if (dmat->map_count != 0)
539 while (dmat != NULL) {
540 bus_dma_tag_t parent;
542 parent = dmat->parent;
543 atomic_subtract_int(&dmat->ref_count, 1);
544 if (dmat->ref_count == 0) {
545 if (dmat->segments != NULL &&
546 dmat->segments != dmat->tagsegs)
547 free(dmat->segments, M_DEVBUF);
548 free(dmat, M_DEVBUF);
550 * Last reference count, so
551 * release our reference
552 * count on our parent.
559 CTR2(KTR_BUSDMA, "%s tag %p", __func__, dmat_copy);
566 * Allocate a handle for mapping from kva/uva/physical
567 * address space into bus device space.
570 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
572 struct sync_list *slist;
576 slist = malloc(sizeof(*slist) * dmat->nsegments, M_DEVBUF, M_NOWAIT);
580 map = uma_zalloc_arg(dmamap_zone, dmat, M_NOWAIT);
583 free(slist, M_DEVBUF);
588 * If the tag's segments haven't been allocated yet we need to do it
589 * now, because we can't sleep for resources at map load time.
591 if (dmat->segments == NULL) {
592 dmat->segments = malloc(dmat->nsegments *
593 sizeof(*dmat->segments), M_DEVBUF, M_NOWAIT);
594 if (dmat->segments == NULL) {
595 free(slist, M_DEVBUF);
596 uma_zfree(dmamap_zone, map);
603 * Bouncing might be required if the driver asks for an active
604 * exclusion region, a data alignment that is stricter than 1, and/or
605 * an active address boundary.
607 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
610 struct bounce_zone *bz;
613 if (dmat->bounce_zone == NULL) {
614 if ((error = alloc_bounce_zone(dmat)) != 0) {
615 free(slist, M_DEVBUF);
616 uma_zfree(dmamap_zone, map);
621 bz = dmat->bounce_zone;
623 /* Initialize the new map */
624 STAILQ_INIT(&((*mapp)->bpages));
627 * Attempt to add pages to our pool on a per-instance
628 * basis up to a sane limit.
630 maxpages = MAX_BPAGES;
631 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
632 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
635 pages = MAX(atop(dmat->maxsize), 1);
636 pages = MIN(maxpages - bz->total_bpages, pages);
637 pages = MAX(pages, 1);
638 if (alloc_bounce_pages(dmat, pages) < pages)
641 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
643 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
652 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
653 __func__, dmat, dmat->flags, error);
659 * Destroy a handle for mapping from kva/uva/physical
660 * address space into bus device space.
663 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
666 if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) {
667 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
668 __func__, dmat, EBUSY);
671 free(map->slist, M_DEVBUF);
672 uma_zfree(dmamap_zone, map);
673 if (dmat->bounce_zone)
674 dmat->bounce_zone->map_count--;
675 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
680 * Allocate a piece of memory that can be efficiently mapped into bus device
681 * space based on the constraints listed in the dma tag. Returns a pointer to
682 * the allocated memory, and a pointer to an associated bus_dmamap.
685 bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddrp, int flags,
688 struct sync_list *slist;
690 struct busdma_bufzone *bufzone;
691 busdma_bufalloc_t ba;
694 vm_memattr_t memattr;
696 if (flags & BUS_DMA_NOWAIT)
701 * If the tag's segments haven't been allocated yet we need to do it
702 * now, because we can't sleep for resources at map load time.
704 if (dmat->segments == NULL)
705 dmat->segments = malloc(dmat->nsegments *
706 sizeof(*dmat->segments), M_DEVBUF, mflags);
708 slist = malloc(sizeof(*slist) * dmat->nsegments, M_DEVBUF, M_NOWAIT);
711 map = uma_zalloc_arg(dmamap_zone, dmat, mflags);
713 free(slist, M_DEVBUF);
716 if (flags & BUS_DMA_COHERENT) {
717 memattr = VM_MEMATTR_UNCACHEABLE;
718 ba = coherent_allocator;
719 map->flags |= DMAMAP_COHERENT;
721 memattr = VM_MEMATTR_DEFAULT;
722 ba = standard_allocator;
724 /* All buffers we allocate are cache-aligned. */
725 map->flags |= DMAMAP_CACHE_ALIGNED;
727 if (flags & BUS_DMA_ZERO)
731 * Try to find a bufzone in the allocator that holds a cache of buffers
732 * of the right size for this request. If the buffer is too big to be
733 * held in the allocator cache, this returns NULL.
735 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
738 * Allocate the buffer from the uma(9) allocator if...
739 * - It's small enough to be in the allocator (bufzone not NULL).
740 * - The alignment constraint isn't larger than the allocation size
741 * (the allocator aligns buffers to their size boundaries).
742 * - There's no need to handle lowaddr/highaddr exclusion zones.
743 * else allocate non-contiguous pages if...
744 * - The page count that could get allocated doesn't exceed nsegments.
745 * - The alignment constraint isn't larger than a page boundary.
746 * - There are no boundary-crossing constraints.
747 * else allocate a block of contiguous pages because one or more of the
748 * constraints is something that only the contig allocator can fulfill.
750 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
751 !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr)) {
752 vaddr = uma_zalloc(bufzone->umazone, mflags);
753 } else if (dmat->nsegments >= btoc(dmat->maxsize) &&
754 dmat->alignment <= PAGE_SIZE && dmat->boundary == 0) {
755 vaddr = (void *)kmem_alloc_attr(kernel_map, dmat->maxsize,
756 mflags, 0, dmat->lowaddr, memattr);
758 vaddr = (void *)kmem_alloc_contig(kernel_map, dmat->maxsize,
759 mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary,
763 free(slist, M_DEVBUF);
764 uma_zfree(dmamap_zone, map);
773 return (vaddr == NULL ? ENOMEM : 0);
777 * Free a piece of memory that was allocated via bus_dmamem_alloc, along with
778 * its associated map.
781 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
783 struct busdma_bufzone *bufzone;
784 busdma_bufalloc_t ba;
786 if (map->flags & DMAMAP_COHERENT)
787 ba = coherent_allocator;
789 ba = standard_allocator;
790 uma_zfree(dmamap_zone, map);
792 free(map->slist, M_DEVBUF);
793 /* Be careful not to access map from here on. */
795 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
797 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
798 !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr))
799 uma_zfree(bufzone->umazone, vaddr);
801 kmem_free(kernel_map, (vm_offset_t)vaddr, dmat->maxsize);
805 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
806 bus_size_t buflen, int flags)
811 if ((map->pagesneeded == 0)) {
812 CTR3(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d",
813 dmat->lowaddr, dmat->boundary, dmat->alignment);
814 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d",
815 map, map->pagesneeded);
817 * Count the number of bounce pages
818 * needed in order to complete this transfer
821 while (buflen != 0) {
822 sgsize = MIN(buflen, dmat->maxsegsz);
823 if (run_filter(dmat, curaddr) != 0) {
824 sgsize = MIN(sgsize, PAGE_SIZE);
830 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
835 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
836 void *buf, bus_size_t buflen, int flags)
839 vm_offset_t vendaddr;
842 if ((map->pagesneeded == 0)) {
843 CTR3(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d",
844 dmat->lowaddr, dmat->boundary, dmat->alignment);
845 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d",
846 map, map->pagesneeded);
848 * Count the number of bounce pages
849 * needed in order to complete this transfer
851 vaddr = trunc_page((vm_offset_t)buf);
852 vendaddr = (vm_offset_t)buf + buflen;
854 while (vaddr < vendaddr) {
855 if (__predict_true(pmap == kernel_pmap))
856 paddr = pmap_kextract(vaddr);
858 paddr = pmap_extract(pmap, vaddr);
859 if (run_filter(dmat, paddr) != 0)
863 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
868 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
871 /* Reserve Necessary Bounce Pages */
872 mtx_lock(&bounce_lock);
873 if (flags & BUS_DMA_NOWAIT) {
874 if (reserve_bounce_pages(dmat, map, 0) != 0) {
875 mtx_unlock(&bounce_lock);
879 if (reserve_bounce_pages(dmat, map, 1) != 0) {
880 /* Queue us for resources */
881 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
882 mtx_unlock(&bounce_lock);
883 return (EINPROGRESS);
886 mtx_unlock(&bounce_lock);
892 * Add a single contiguous physical range to the segment list.
895 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
896 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
898 bus_addr_t baddr, bmask;
902 * Make sure we don't cross any boundaries.
904 bmask = ~(dmat->boundary - 1);
905 if (dmat->boundary > 0) {
906 baddr = (curaddr + dmat->boundary) & bmask;
907 if (sgsize > (baddr - curaddr))
908 sgsize = (baddr - curaddr);
911 struct arm32_dma_range *dr;
913 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
918 * In a valid DMA range. Translate the physical
919 * memory address to an address in the DMA window.
921 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
927 * Insert chunk into a segment, coalescing with
928 * the previous segment if possible.
931 curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
932 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
933 (dmat->boundary == 0 ||
934 (segs[seg].ds_addr & bmask) == (curaddr & bmask))) {
935 segs[seg].ds_len += sgsize;
937 if (++seg >= dmat->nsegments)
939 segs[seg].ds_addr = curaddr;
940 segs[seg].ds_len = sgsize;
947 * Utility function to load a physical buffer. segp contains
948 * the starting segment on entrace, and the ending segment on exit.
951 _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
952 bus_size_t buflen, int flags, bus_dma_segment_t *segs, int *segp)
959 segs = dmat->segments;
961 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
962 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
963 if (map->pagesneeded != 0) {
964 error = _bus_dmamap_reserve_pages(dmat, map, flags);
972 sgsize = MIN(buflen, dmat->maxsegsz);
973 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
974 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
975 sgsize = MIN(sgsize, PAGE_SIZE);
976 curaddr = add_bounce_page(dmat, map, 0, curaddr,
979 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
991 _bus_dmamap_unload(dmat, map);
992 return (EFBIG); /* XXX better return value here? */
997 * Utility function to load a linear buffer. segp contains
998 * the starting segment on entrance, and the ending segment on exit.
1001 _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
1002 bus_size_t buflen, struct pmap *pmap, int flags, bus_dma_segment_t *segs,
1007 struct sync_list *sl;
1008 vm_offset_t vaddr = (vm_offset_t)buf;
1012 segs = dmat->segments;
1013 if ((flags & BUS_DMA_LOAD_MBUF) != 0)
1014 map->flags |= DMAMAP_CACHE_ALIGNED;
1016 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
1017 _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
1018 if (map->pagesneeded != 0) {
1019 error = _bus_dmamap_reserve_pages(dmat, map, flags);
1024 CTR3(KTR_BUSDMA, "lowaddr= %d boundary= %d, "
1025 "alignment= %d", dmat->lowaddr, dmat->boundary, dmat->alignment);
1027 while (buflen > 0) {
1029 * Get the physical address for this segment.
1031 if (__predict_true(pmap == kernel_pmap)) {
1032 curaddr = pmap_kextract(vaddr);
1034 curaddr = pmap_extract(pmap, vaddr);
1035 map->flags &= ~DMAMAP_COHERENT;
1039 * Compute the segment size, and adjust counts.
1041 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
1042 if (sgsize > dmat->maxsegsz)
1043 sgsize = dmat->maxsegsz;
1044 if (buflen < sgsize)
1047 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
1048 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
1049 curaddr = add_bounce_page(dmat, map, vaddr, curaddr,
1052 sl = &map->slist[map->sync_count - 1];
1053 if (map->sync_count == 0 ||
1054 vaddr != sl->vaddr + sl->datacount) {
1055 if (++map->sync_count > dmat->nsegments)
1059 sl->datacount = sgsize;
1060 sl->busaddr = curaddr;
1062 sl->datacount += sgsize;
1064 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1077 _bus_dmamap_unload(dmat, map);
1078 return (EFBIG); /* XXX better return value here? */
1084 __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
1085 struct memdesc *mem, bus_dmamap_callback_t *callback,
1089 KASSERT(dmat != NULL, ("dmatag is NULL"));
1090 KASSERT(map != NULL, ("dmamap is NULL"));
1092 map->callback = callback;
1093 map->callback_arg = callback_arg;
1097 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
1098 bus_dma_segment_t *segs, int nsegs, int error)
1102 segs = dmat->segments;
1107 * Release the mapping held by map.
1110 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1112 struct bounce_page *bpage;
1114 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1115 STAILQ_REMOVE_HEAD(&map->bpages, links);
1116 free_bounce_page(dmat, bpage);
1118 map->sync_count = 0;
1123 bus_dmamap_sync_buf(vm_offset_t buf, int len, bus_dmasync_op_t op,
1126 char _tmp_cl[arm_dcache_align], _tmp_clend[arm_dcache_align];
1130 if ((op & BUS_DMASYNC_PREWRITE) && !(op & BUS_DMASYNC_PREREAD)) {
1131 cpu_dcache_wb_range(buf, len);
1132 cpu_l2cache_wb_range(buf, len);
1136 * If the caller promises the buffer is properly aligned to a cache line
1137 * (even if the call parms make it look like it isn't) we can avoid
1138 * attempting to preserve the non-DMA part of the cache line in the
1139 * POSTREAD case, but we MUST still do a writeback in the PREREAD case.
1141 * This covers the case of mbufs, where we know how they're aligned and
1142 * know the CPU doesn't touch the header in front of the DMA data area
1143 * during the IO, but it may have touched it right before invoking the
1144 * sync, so a PREREAD writeback is required.
1146 * It also handles buffers we created in bus_dmamem_alloc(), which are
1147 * always aligned and padded to cache line size even if the IO length
1148 * isn't a multiple of cache line size. In this case the PREREAD
1149 * writeback probably isn't required, but it's harmless.
1151 partial = (((vm_offset_t)buf) | len) & arm_dcache_align_mask;
1153 if (op & BUS_DMASYNC_PREREAD) {
1154 if (!(op & BUS_DMASYNC_PREWRITE) && !partial) {
1155 cpu_dcache_inv_range(buf, len);
1156 cpu_l2cache_inv_range(buf, len);
1158 cpu_dcache_wbinv_range(buf, len);
1159 cpu_l2cache_wbinv_range(buf, len);
1162 if (op & BUS_DMASYNC_POSTREAD) {
1163 if (partial && !bufaligned) {
1165 if (buf & arm_dcache_align_mask)
1166 memcpy(_tmp_cl, (void *)(buf &
1167 ~arm_dcache_align_mask),
1168 buf & arm_dcache_align_mask);
1169 if ((buf + len) & arm_dcache_align_mask)
1171 (void *)(buf + len),
1173 ((buf + len) & arm_dcache_align_mask));
1175 cpu_dcache_inv_range(buf, len);
1176 cpu_l2cache_inv_range(buf, len);
1177 if (partial && !bufaligned) {
1178 if (buf & arm_dcache_align_mask)
1179 memcpy((void *)(buf &
1180 ~arm_dcache_align_mask), _tmp_cl,
1181 buf & arm_dcache_align_mask);
1182 if ((buf + len) & arm_dcache_align_mask)
1183 memcpy((void *)(buf + len),
1184 _tmp_clend, arm_dcache_align -
1185 ((buf + len) & arm_dcache_align_mask));
1192 _bus_dmamap_sync_bp(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1194 struct bounce_page *bpage;
1196 STAILQ_FOREACH(bpage, &map->bpages, links) {
1197 if (op & BUS_DMASYNC_PREWRITE) {
1198 if (bpage->datavaddr != 0)
1199 bcopy((void *)bpage->datavaddr,
1200 (void *)(bpage->vaddr_nocache != 0 ?
1201 bpage->vaddr_nocache :
1205 physcopyout(bpage->dataaddr,
1206 (void *)(bpage->vaddr_nocache != 0 ?
1207 bpage->vaddr_nocache :
1210 if (bpage->vaddr_nocache == 0) {
1211 cpu_dcache_wb_range(bpage->vaddr,
1213 cpu_l2cache_wb_range(bpage->vaddr,
1216 dmat->bounce_zone->total_bounced++;
1218 if (op & BUS_DMASYNC_POSTREAD) {
1219 if (bpage->vaddr_nocache == 0) {
1220 cpu_dcache_inv_range(bpage->vaddr,
1222 cpu_l2cache_inv_range(bpage->vaddr,
1225 if (bpage->datavaddr != 0)
1226 bcopy((void *)(bpage->vaddr_nocache != 0 ?
1227 bpage->vaddr_nocache : bpage->vaddr),
1228 (void *)bpage->datavaddr, bpage->datacount);
1230 physcopyin((void *)(bpage->vaddr_nocache != 0 ?
1231 bpage->vaddr_nocache : bpage->vaddr),
1232 bpage->dataaddr, bpage->datacount);
1233 dmat->bounce_zone->total_bounced++;
1239 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1241 struct sync_list *sl, *end;
1244 if (op == BUS_DMASYNC_POSTWRITE)
1246 if (map->flags & DMAMAP_COHERENT)
1248 if (STAILQ_FIRST(&map->bpages))
1249 _bus_dmamap_sync_bp(dmat, map, op);
1250 CTR3(KTR_BUSDMA, "%s: op %x flags %x", __func__, op, map->flags);
1251 bufaligned = (map->flags & DMAMAP_CACHE_ALIGNED);
1252 if (map->sync_count) {
1253 end = &map->slist[map->sync_count];
1254 for (sl = &map->slist[0]; sl != end; sl++)
1255 bus_dmamap_sync_buf(sl->vaddr, sl->datacount, op,
1261 cpu_drain_writebuf();
1265 init_bounce_pages(void *dummy __unused)
1269 STAILQ_INIT(&bounce_zone_list);
1270 STAILQ_INIT(&bounce_map_waitinglist);
1271 STAILQ_INIT(&bounce_map_callbacklist);
1272 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
1274 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1276 static struct sysctl_ctx_list *
1277 busdma_sysctl_tree(struct bounce_zone *bz)
1279 return (&bz->sysctl_tree);
1282 static struct sysctl_oid *
1283 busdma_sysctl_tree_top(struct bounce_zone *bz)
1285 return (bz->sysctl_tree_top);
1289 alloc_bounce_zone(bus_dma_tag_t dmat)
1291 struct bounce_zone *bz;
1293 /* Check to see if we already have a suitable zone */
1294 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1295 if ((dmat->alignment <= bz->alignment)
1296 && (dmat->lowaddr >= bz->lowaddr)) {
1297 dmat->bounce_zone = bz;
1302 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1303 M_NOWAIT | M_ZERO)) == NULL)
1306 STAILQ_INIT(&bz->bounce_page_list);
1307 bz->free_bpages = 0;
1308 bz->reserved_bpages = 0;
1309 bz->active_bpages = 0;
1310 bz->lowaddr = dmat->lowaddr;
1311 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1313 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1315 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1316 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1317 dmat->bounce_zone = bz;
1319 sysctl_ctx_init(&bz->sysctl_tree);
1320 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1321 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1323 if (bz->sysctl_tree_top == NULL) {
1324 sysctl_ctx_free(&bz->sysctl_tree);
1325 return (0); /* XXX error code? */
1328 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1329 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1330 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1331 "Total bounce pages");
1332 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1333 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1334 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1335 "Free bounce pages");
1336 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1337 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1338 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1339 "Reserved bounce pages");
1340 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1341 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1342 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1343 "Active bounce pages");
1344 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1345 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1346 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1347 "Total bounce requests");
1348 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1349 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1350 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1351 "Total bounce requests that were deferred");
1352 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1353 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1354 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1355 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1356 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1357 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1363 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1365 struct bounce_zone *bz;
1368 bz = dmat->bounce_zone;
1370 while (numpages > 0) {
1371 struct bounce_page *bpage;
1373 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1378 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1383 if (bpage->vaddr == 0) {
1384 free(bpage, M_DEVBUF);
1387 bpage->busaddr = pmap_kextract(bpage->vaddr);
1388 bpage->vaddr_nocache = (vm_offset_t)arm_remap_nocache(
1389 (void *)bpage->vaddr, PAGE_SIZE);
1390 mtx_lock(&bounce_lock);
1391 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1395 mtx_unlock(&bounce_lock);
1403 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1405 struct bounce_zone *bz;
1408 mtx_assert(&bounce_lock, MA_OWNED);
1409 bz = dmat->bounce_zone;
1410 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1411 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1412 return (map->pagesneeded - (map->pagesreserved + pages));
1413 bz->free_bpages -= pages;
1414 bz->reserved_bpages += pages;
1415 map->pagesreserved += pages;
1416 pages = map->pagesneeded - map->pagesreserved;
1422 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1423 bus_addr_t addr, bus_size_t size)
1425 struct bounce_zone *bz;
1426 struct bounce_page *bpage;
1428 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1429 KASSERT(map != NULL, ("add_bounce_page: bad map %p", map));
1431 bz = dmat->bounce_zone;
1432 if (map->pagesneeded == 0)
1433 panic("add_bounce_page: map doesn't need any pages");
1436 if (map->pagesreserved == 0)
1437 panic("add_bounce_page: map doesn't need any pages");
1438 map->pagesreserved--;
1440 mtx_lock(&bounce_lock);
1441 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1443 panic("add_bounce_page: free page list is empty");
1445 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1446 bz->reserved_bpages--;
1447 bz->active_bpages++;
1448 mtx_unlock(&bounce_lock);
1450 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1451 /* Page offset needs to be preserved. */
1452 bpage->vaddr |= vaddr & PAGE_MASK;
1453 bpage->busaddr |= vaddr & PAGE_MASK;
1455 bpage->datavaddr = vaddr;
1456 bpage->dataaddr = addr;
1457 bpage->datacount = size;
1458 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1459 return (bpage->busaddr);
1463 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1465 struct bus_dmamap *map;
1466 struct bounce_zone *bz;
1468 bz = dmat->bounce_zone;
1469 bpage->datavaddr = 0;
1470 bpage->datacount = 0;
1471 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1473 * Reset the bounce page to start at offset 0. Other uses
1474 * of this bounce page may need to store a full page of
1475 * data and/or assume it starts on a page boundary.
1477 bpage->vaddr &= ~PAGE_MASK;
1478 bpage->busaddr &= ~PAGE_MASK;
1481 mtx_lock(&bounce_lock);
1482 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1484 bz->active_bpages--;
1485 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1486 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1487 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1488 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1490 busdma_swi_pending = 1;
1491 bz->total_deferred++;
1492 swi_sched(vm_ih, 0);
1495 mtx_unlock(&bounce_lock);
1502 struct bus_dmamap *map;
1504 mtx_lock(&bounce_lock);
1505 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1506 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1507 mtx_unlock(&bounce_lock);
1509 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1510 bus_dmamap_load_mem(map->dmat, map, &map->mem,
1511 map->callback, map->callback_arg, BUS_DMA_WAITOK);
1512 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1513 mtx_lock(&bounce_lock);
1515 mtx_unlock(&bounce_lock);