2 * Copyright (c) 2012-2014 Ian Lepore
3 * Copyright (c) 2010 Mark Tinguely
4 * Copyright (c) 2004 Olivier Houchard
5 * Copyright (c) 2002 Peter Grehan
6 * Copyright (c) 1997, 1998 Justin T. Gibbs.
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
30 * From i386/busdma_machdep.c 191438 2009-04-23 20:24:19Z jhb
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #define _ARM32_BUS_DMA_PRIVATE
37 #include <sys/param.h>
40 #include <ddb/db_output.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/busdma_bufalloc.h>
45 #include <sys/counter.h>
46 #include <sys/interrupt.h>
47 #include <sys/kernel.h>
50 #include <sys/memdesc.h>
52 #include <sys/mutex.h>
53 #include <sys/sysctl.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_kern.h>
62 #include <machine/atomic.h>
63 #include <machine/bus.h>
64 #include <machine/cpufunc.h>
65 #include <machine/md_var.h>
68 #define MAX_DMA_SEGMENTS 4096
69 #define BUS_DMA_EXCL_BOUNCE BUS_DMA_BUS2
70 #define BUS_DMA_ALIGN_BOUNCE BUS_DMA_BUS3
71 #define BUS_DMA_COULD_BOUNCE (BUS_DMA_EXCL_BOUNCE | BUS_DMA_ALIGN_BOUNCE)
72 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
82 bus_dma_filter_t *filter;
90 bus_dma_lock_t *lockfunc;
92 struct bounce_zone *bounce_zone;
94 * DMA range for this tag. If the page doesn't fall within
95 * one of these ranges, an error is returned. The caller
96 * may then decide what to do with the transfer. If the
97 * range pointer is NULL, it is ignored.
99 struct arm32_dma_range *ranges;
104 vm_offset_t vaddr; /* kva of bounce buffer */
105 bus_addr_t busaddr; /* Physical address */
106 vm_offset_t datavaddr; /* kva of client data */
107 bus_addr_t dataaddr; /* client physical address */
108 bus_size_t datacount; /* client data count */
109 STAILQ_ENTRY(bounce_page) links;
113 vm_offset_t vaddr; /* kva of bounce buffer */
114 bus_addr_t busaddr; /* Physical address */
115 bus_size_t datacount; /* client data count */
118 int busdma_swi_pending;
121 STAILQ_ENTRY(bounce_zone) links;
122 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
130 bus_size_t alignment;
134 struct sysctl_ctx_list sysctl_tree;
135 struct sysctl_oid *sysctl_tree_top;
138 static struct mtx bounce_lock;
139 static int total_bpages;
140 static int busdma_zonecount;
141 static uint32_t tags_total;
142 static uint32_t maps_total;
143 static uint32_t maps_dmamem;
144 static uint32_t maps_coherent;
145 static counter_u64_t maploads_total;
146 static counter_u64_t maploads_bounced;
147 static counter_u64_t maploads_coherent;
148 static counter_u64_t maploads_dmamem;
149 static counter_u64_t maploads_mbuf;
150 static counter_u64_t maploads_physmem;
152 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
154 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
155 SYSCTL_UINT(_hw_busdma, OID_AUTO, tags_total, CTLFLAG_RD, &tags_total, 0,
156 "Number of active tags");
157 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_total, CTLFLAG_RD, &maps_total, 0,
158 "Number of active maps");
159 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_dmamem, CTLFLAG_RD, &maps_dmamem, 0,
160 "Number of active maps for bus_dmamem_alloc buffers");
161 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_coherent, CTLFLAG_RD, &maps_coherent, 0,
162 "Number of active maps with BUS_DMA_COHERENT flag set");
163 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_total, CTLFLAG_RD,
164 &maploads_total, "Number of load operations performed");
165 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_bounced, CTLFLAG_RD,
166 &maploads_bounced, "Number of load operations that used bounce buffers");
167 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_coherent, CTLFLAG_RD,
168 &maploads_dmamem, "Number of load operations on BUS_DMA_COHERENT memory");
169 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_dmamem, CTLFLAG_RD,
170 &maploads_dmamem, "Number of load operations on bus_dmamem_alloc buffers");
171 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_mbuf, CTLFLAG_RD,
172 &maploads_mbuf, "Number of load operations for mbufs");
173 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_physmem, CTLFLAG_RD,
174 &maploads_physmem, "Number of load operations on physical buffers");
175 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
176 "Total bounce pages");
179 struct bp_list bpages;
185 bus_dmamap_callback_t *callback;
188 #define DMAMAP_COHERENT (1 << 0)
189 #define DMAMAP_DMAMEM_ALLOC (1 << 1)
190 #define DMAMAP_MBUF (1 << 2)
191 STAILQ_ENTRY(bus_dmamap) links;
192 bus_dma_segment_t *segments;
194 struct sync_list slist[];
197 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
198 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
200 static void init_bounce_pages(void *dummy);
201 static int alloc_bounce_zone(bus_dma_tag_t dmat);
202 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
203 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
205 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
206 vm_offset_t vaddr, bus_addr_t addr,
208 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
209 static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
210 void *buf, bus_size_t buflen, int flags);
211 static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
212 vm_paddr_t buf, bus_size_t buflen, int flags);
213 static int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
216 static busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */
217 static busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */
219 busdma_init(void *dummy)
223 maploads_total = counter_u64_alloc(M_WAITOK);
224 maploads_bounced = counter_u64_alloc(M_WAITOK);
225 maploads_coherent = counter_u64_alloc(M_WAITOK);
226 maploads_dmamem = counter_u64_alloc(M_WAITOK);
227 maploads_mbuf = counter_u64_alloc(M_WAITOK);
228 maploads_physmem = counter_u64_alloc(M_WAITOK);
232 /* Create a cache of buffers in standard (cacheable) memory. */
233 standard_allocator = busdma_bufalloc_create("buffer",
234 arm_dcache_align, /* minimum_alignment */
235 NULL, /* uma_alloc func */
236 NULL, /* uma_free func */
237 uma_flags); /* uma_zcreate_flags */
241 * Force UMA zone to allocate service structures like
242 * slabs using own allocator. uma_debug code performs
243 * atomic ops on uma_slab_t fields and safety of this
244 * operation is not guaranteed for write-back caches
246 uma_flags = UMA_ZONE_OFFPAGE;
249 * Create a cache of buffers in uncacheable memory, to implement the
250 * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag.
252 coherent_allocator = busdma_bufalloc_create("coherent",
253 arm_dcache_align, /* minimum_alignment */
254 busdma_bufalloc_alloc_uncacheable,
255 busdma_bufalloc_free_uncacheable,
256 uma_flags); /* uma_zcreate_flags */
260 * This init historically used SI_SUB_VM, but now the init code requires
261 * malloc(9) using M_DEVBUF memory and the pcpu zones for counter(9), which get
262 * set up by SI_SUB_KMEM and SI_ORDER_LAST, so we'll go right after that by
263 * using SI_SUB_KMEM+1.
265 SYSINIT(busdma, SI_SUB_KMEM+1, SI_ORDER_FIRST, busdma_init, NULL);
268 * This routine checks the exclusion zone constraints from a tag against the
269 * physical RAM available on the machine. If a tag specifies an exclusion zone
270 * but there's no RAM in that zone, then we avoid allocating resources to bounce
271 * a request, and we can use any memory allocator (as opposed to needing
272 * kmem_alloc_contig() just because it can allocate pages in an address range).
274 * Most tags have BUS_SPACE_MAXADDR or BUS_SPACE_MAXADDR_32BIT (they are the
275 * same value on 32-bit architectures) as their lowaddr constraint, and we can't
276 * possibly have RAM at an address higher than the highest address we can
277 * express, so we take a fast out.
280 exclusion_bounce_check(vm_offset_t lowaddr, vm_offset_t highaddr)
284 if (lowaddr >= BUS_SPACE_MAXADDR)
287 for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) {
288 if ((lowaddr >= phys_avail[i] && lowaddr < phys_avail[i + 1]) ||
289 (lowaddr < phys_avail[i] && highaddr >= phys_avail[i]))
296 * Return true if the tag has an exclusion zone that could lead to bouncing.
299 exclusion_bounce(bus_dma_tag_t dmat)
302 return (dmat->flags & BUS_DMA_EXCL_BOUNCE);
306 * Return true if the given address does not fall on the alignment boundary.
309 alignment_bounce(bus_dma_tag_t dmat, bus_addr_t addr)
312 return (addr & (dmat->alignment - 1));
316 * Return true if the DMA should bounce because the start or end does not fall
317 * on a cacheline boundary (which would require a partial cacheline flush).
318 * COHERENT memory doesn't trigger cacheline flushes. Memory allocated by
319 * bus_dmamem_alloc() is always aligned to cacheline boundaries, and there's a
320 * strict rule that such memory cannot be accessed by the CPU while DMA is in
321 * progress (or by multiple DMA engines at once), so that it's always safe to do
322 * full cacheline flushes even if that affects memory outside the range of a
323 * given DMA operation that doesn't involve the full allocated buffer. If we're
324 * mapping an mbuf, that follows the same rules as a buffer we allocated.
327 cacheline_bounce(bus_dmamap_t map, bus_addr_t addr, bus_size_t size)
330 if (map->flags & (DMAMAP_DMAMEM_ALLOC | DMAMAP_COHERENT | DMAMAP_MBUF))
332 return ((addr | size) & arm_dcache_align_mask);
336 * Return true if we might need to bounce the DMA described by addr and size.
338 * This is used to quick-check whether we need to do the more expensive work of
339 * checking the DMA page-by-page looking for alignment and exclusion bounces.
341 * Note that the addr argument might be either virtual or physical. It doesn't
342 * matter because we only look at the low-order bits, which are the same in both
346 might_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t addr,
350 return ((dmat->flags & BUS_DMA_EXCL_BOUNCE) ||
351 alignment_bounce(dmat, addr) ||
352 cacheline_bounce(map, addr, size));
356 * Return true if we must bounce the DMA described by paddr and size.
358 * Bouncing can be triggered by DMA that doesn't begin and end on cacheline
359 * boundaries, or doesn't begin on an alignment boundary, or falls within the
360 * exclusion zone of any tag in the ancestry chain.
362 * For exclusions, walk the chain of tags comparing paddr to the exclusion zone
363 * within each tag. If the tag has a filter function, use it to decide whether
364 * the DMA needs to bounce, otherwise any DMA within the zone bounces.
367 must_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t paddr,
371 if (cacheline_bounce(map, paddr, size))
375 * The tag already contains ancestors' alignment restrictions so this
376 * check doesn't need to be inside the loop.
378 if (alignment_bounce(dmat, paddr))
382 * Even though each tag has an exclusion zone that is a superset of its
383 * own and all its ancestors' exclusions, the exclusion zone of each tag
384 * up the chain must be checked within the loop, because the busdma
385 * rules say the filter function is called only when the address lies
386 * within the low-highaddr range of the tag that filterfunc belongs to.
388 while (dmat != NULL && exclusion_bounce(dmat)) {
389 if ((paddr >= dmat->lowaddr && paddr <= dmat->highaddr) &&
390 (dmat->filter == NULL ||
391 dmat->filter(dmat->filterarg, paddr) != 0))
399 static __inline struct arm32_dma_range *
400 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
403 struct arm32_dma_range *dr;
406 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
407 if (curaddr >= dr->dr_sysbase &&
408 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
416 * Convenience function for manipulating driver locks from busdma (during
417 * busdma_swi, for example). Drivers that don't provide their own locks
418 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
419 * non-mutex locking scheme don't have to use this at all.
422 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
426 dmtx = (struct mtx *)arg;
435 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
440 * dflt_lock should never get called. It gets put into the dma tag when
441 * lockfunc == NULL, which is only valid if the maps that are associated
442 * with the tag are meant to never be defered.
443 * XXX Should have a way to identify which driver is responsible here.
446 dflt_lock(void *arg, bus_dma_lock_op_t op)
449 panic("driver error: busdma dflt_lock called");
453 * Allocate a device specific dma_tag.
456 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
457 bus_size_t boundary, bus_addr_t lowaddr,
458 bus_addr_t highaddr, bus_dma_filter_t *filter,
459 void *filterarg, bus_size_t maxsize, int nsegments,
460 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
461 void *lockfuncarg, bus_dma_tag_t *dmat)
463 bus_dma_tag_t newtag;
468 parent = arm_root_dma_tag;
471 /* Basic sanity checking. */
472 KASSERT(boundary == 0 || powerof2(boundary),
473 ("dma tag boundary %lu, must be a power of 2", boundary));
474 KASSERT(boundary == 0 || boundary >= maxsegsz,
475 ("dma tag boundary %lu is < maxsegsz %lu\n", boundary, maxsegsz));
476 KASSERT(alignment != 0 && powerof2(alignment),
477 ("dma tag alignment %lu, must be non-zero power of 2", alignment));
478 KASSERT(maxsegsz != 0, ("dma tag maxsegsz must not be zero"));
480 /* Return a NULL tag on failure */
483 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
485 if (newtag == NULL) {
486 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
487 __func__, newtag, 0, error);
491 newtag->parent = parent;
492 newtag->alignment = alignment;
493 newtag->boundary = boundary;
494 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
495 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
497 newtag->filter = filter;
498 newtag->filterarg = filterarg;
499 newtag->maxsize = maxsize;
500 newtag->nsegments = nsegments;
501 newtag->maxsegsz = maxsegsz;
502 newtag->flags = flags;
503 newtag->ref_count = 1; /* Count ourself */
504 newtag->map_count = 0;
505 newtag->ranges = bus_dma_get_range();
506 newtag->_nranges = bus_dma_get_range_nb();
507 if (lockfunc != NULL) {
508 newtag->lockfunc = lockfunc;
509 newtag->lockfuncarg = lockfuncarg;
511 newtag->lockfunc = dflt_lock;
512 newtag->lockfuncarg = NULL;
515 /* Take into account any restrictions imposed by our parent tag */
516 if (parent != NULL) {
517 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
518 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
519 newtag->alignment = MAX(parent->alignment, newtag->alignment);
520 newtag->flags |= parent->flags & BUS_DMA_COULD_BOUNCE;
521 if (newtag->boundary == 0)
522 newtag->boundary = parent->boundary;
523 else if (parent->boundary != 0)
524 newtag->boundary = MIN(parent->boundary,
526 if (newtag->filter == NULL) {
528 * Short circuit to looking at our parent directly
529 * since we have encapsulated all of its information
531 newtag->filter = parent->filter;
532 newtag->filterarg = parent->filterarg;
533 newtag->parent = parent->parent;
535 if (newtag->parent != NULL)
536 atomic_add_int(&parent->ref_count, 1);
539 if (exclusion_bounce_check(newtag->lowaddr, newtag->highaddr))
540 newtag->flags |= BUS_DMA_EXCL_BOUNCE;
541 if (alignment_bounce(newtag, 1))
542 newtag->flags |= BUS_DMA_ALIGN_BOUNCE;
545 * Any request can auto-bounce due to cacheline alignment, in addition
546 * to any alignment or boundary specifications in the tag, so if the
547 * ALLOCNOW flag is set, there's always work to do.
549 if ((flags & BUS_DMA_ALLOCNOW) != 0) {
550 struct bounce_zone *bz;
552 * Round size up to a full page, and add one more page because
553 * there can always be one more boundary crossing than the
554 * number of pages in a transfer.
556 maxsize = roundup2(maxsize, PAGE_SIZE) + PAGE_SIZE;
558 if ((error = alloc_bounce_zone(newtag)) != 0) {
559 free(newtag, M_DEVBUF);
562 bz = newtag->bounce_zone;
564 if (ptoa(bz->total_bpages) < maxsize) {
567 pages = atop(maxsize) - bz->total_bpages;
569 /* Add pages to our bounce pool */
570 if (alloc_bounce_pages(newtag, pages) < pages)
573 /* Performed initial allocation */
574 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
576 newtag->bounce_zone = NULL;
579 free(newtag, M_DEVBUF);
581 atomic_add_32(&tags_total, 1);
584 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
585 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
590 bus_dma_tag_destroy(bus_dma_tag_t dmat)
592 bus_dma_tag_t dmat_copy;
600 if (dmat->map_count != 0) {
605 while (dmat != NULL) {
606 bus_dma_tag_t parent;
608 parent = dmat->parent;
609 atomic_subtract_int(&dmat->ref_count, 1);
610 if (dmat->ref_count == 0) {
611 atomic_subtract_32(&tags_total, 1);
612 free(dmat, M_DEVBUF);
614 * Last reference count, so
615 * release our reference
616 * count on our parent.
624 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
628 static int allocate_bz_and_pages(bus_dma_tag_t dmat, bus_dmamap_t mapp)
630 struct bounce_zone *bz;
634 if (dmat->bounce_zone == NULL)
635 if ((error = alloc_bounce_zone(dmat)) != 0)
637 bz = dmat->bounce_zone;
638 /* Initialize the new map */
639 STAILQ_INIT(&(mapp->bpages));
642 * Attempt to add pages to our pool on a per-instance basis up to a sane
643 * limit. Even if the tag isn't flagged as COULD_BOUNCE due to
644 * alignment and boundary constraints, it could still auto-bounce due to
645 * cacheline alignment, which requires at most two bounce pages.
647 if (dmat->flags & BUS_DMA_COULD_BOUNCE)
648 maxpages = MAX_BPAGES;
650 maxpages = 2 * bz->map_count;
651 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 ||
652 (bz->map_count > 0 && bz->total_bpages < maxpages)) {
655 pages = atop(roundup2(dmat->maxsize, PAGE_SIZE)) + 1;
656 pages = MIN(maxpages - bz->total_bpages, pages);
657 pages = MAX(pages, 2);
658 if (alloc_bounce_pages(dmat, pages) < pages)
661 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0)
662 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
669 allocate_map(bus_dma_tag_t dmat, int mflags)
671 int mapsize, segsize;
675 * Allocate the map. The map structure ends with an embedded
676 * variable-sized array of sync_list structures. Following that
677 * we allocate enough extra space to hold the array of bus_dma_segments.
679 KASSERT(dmat->nsegments <= MAX_DMA_SEGMENTS,
680 ("cannot allocate %u dma segments (max is %u)",
681 dmat->nsegments, MAX_DMA_SEGMENTS));
682 segsize = sizeof(struct bus_dma_segment) * dmat->nsegments;
683 mapsize = sizeof(*map) + sizeof(struct sync_list) * dmat->nsegments;
684 map = malloc(mapsize + segsize, M_DEVBUF, mflags | M_ZERO);
686 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
689 map->segments = (bus_dma_segment_t *)((uintptr_t)map + mapsize);
694 * Allocate a handle for mapping from kva/uva/physical
695 * address space into bus device space.
698 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
703 *mapp = map = allocate_map(dmat, M_NOWAIT);
705 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
710 * Bouncing might be required if the driver asks for an exclusion
711 * region, a data alignment that is stricter than 1, or DMA that begins
712 * or ends with a partial cacheline. Whether bouncing will actually
713 * happen can't be known until mapping time, but we need to pre-allocate
714 * resources now because we might not be allowed to at mapping time.
716 error = allocate_bz_and_pages(dmat, map);
722 if (map->flags & DMAMAP_COHERENT)
723 atomic_add_32(&maps_coherent, 1);
724 atomic_add_32(&maps_total, 1);
731 * Destroy a handle for mapping from kva/uva/physical
732 * address space into bus device space.
735 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
737 if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) {
738 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
739 __func__, dmat, EBUSY);
742 if (dmat->bounce_zone)
743 dmat->bounce_zone->map_count--;
744 if (map->flags & DMAMAP_COHERENT)
745 atomic_subtract_32(&maps_coherent, 1);
746 atomic_subtract_32(&maps_total, 1);
749 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
755 * Allocate a piece of memory that can be efficiently mapped into
756 * bus device space based on the constraints lited in the dma tag.
757 * A dmamap to for use with dmamap_load is also allocated.
760 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
763 busdma_bufalloc_t ba;
764 struct busdma_bufzone *bufzone;
766 vm_memattr_t memattr;
769 if (flags & BUS_DMA_NOWAIT)
773 if (flags & BUS_DMA_ZERO)
776 *mapp = map = allocate_map(dmat, mflags);
778 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
779 __func__, dmat, dmat->flags, ENOMEM);
782 map->flags = DMAMAP_DMAMEM_ALLOC;
784 /* Choose a busdma buffer allocator based on memory type flags. */
785 if (flags & BUS_DMA_COHERENT) {
786 memattr = VM_MEMATTR_UNCACHEABLE;
787 ba = coherent_allocator;
788 map->flags |= DMAMAP_COHERENT;
790 memattr = VM_MEMATTR_DEFAULT;
791 ba = standard_allocator;
795 * Try to find a bufzone in the allocator that holds a cache of buffers
796 * of the right size for this request. If the buffer is too big to be
797 * held in the allocator cache, this returns NULL.
799 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
802 * Allocate the buffer from the uma(9) allocator if...
803 * - It's small enough to be in the allocator (bufzone not NULL).
804 * - The alignment constraint isn't larger than the allocation size
805 * (the allocator aligns buffers to their size boundaries).
806 * - There's no need to handle lowaddr/highaddr exclusion zones.
807 * else allocate non-contiguous pages if...
808 * - The page count that could get allocated doesn't exceed nsegments.
809 * - The alignment constraint isn't larger than a page boundary.
810 * - There are no boundary-crossing constraints.
811 * else allocate a block of contiguous pages because one or more of the
812 * constraints is something that only the contig allocator can fulfill.
814 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
815 !exclusion_bounce(dmat)) {
816 *vaddr = uma_zalloc(bufzone->umazone, mflags);
817 } else if (dmat->nsegments >= btoc(dmat->maxsize) &&
818 dmat->alignment <= PAGE_SIZE && dmat->boundary == 0) {
819 *vaddr = (void *)kmem_alloc_attr(kernel_arena, dmat->maxsize,
820 mflags, 0, dmat->lowaddr, memattr);
822 *vaddr = (void *)kmem_alloc_contig(kernel_arena, dmat->maxsize,
823 mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary,
828 if (*vaddr == NULL) {
829 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
830 __func__, dmat, dmat->flags, ENOMEM);
835 if (map->flags & DMAMAP_COHERENT)
836 atomic_add_32(&maps_coherent, 1);
837 atomic_add_32(&maps_dmamem, 1);
838 atomic_add_32(&maps_total, 1);
841 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
842 __func__, dmat, dmat->flags, 0);
847 * Free a piece of memory and it's allociated dmamap, that was allocated
848 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
851 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
853 struct busdma_bufzone *bufzone;
854 busdma_bufalloc_t ba;
856 if (map->flags & DMAMAP_COHERENT)
857 ba = coherent_allocator;
859 ba = standard_allocator;
861 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
863 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
864 !exclusion_bounce(dmat))
865 uma_zfree(bufzone->umazone, vaddr);
867 kmem_free(kernel_arena, (vm_offset_t)vaddr, dmat->maxsize);
870 if (map->flags & DMAMAP_COHERENT)
871 atomic_subtract_32(&maps_coherent, 1);
872 atomic_subtract_32(&maps_total, 1);
873 atomic_subtract_32(&maps_dmamem, 1);
875 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
879 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
880 bus_size_t buflen, int flags)
885 if (map->pagesneeded == 0) {
886 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d"
887 " map= %p, pagesneeded= %d",
888 dmat->lowaddr, dmat->boundary, dmat->alignment,
889 map, map->pagesneeded);
891 * Count the number of bounce pages
892 * needed in order to complete this transfer
895 while (buflen != 0) {
896 sgsize = MIN(buflen, dmat->maxsegsz);
897 if (must_bounce(dmat, map, curaddr, sgsize) != 0) {
898 sgsize = MIN(sgsize, PAGE_SIZE);
904 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded);
909 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
910 void *buf, bus_size_t buflen, int flags)
913 vm_offset_t vendaddr;
916 if (map->pagesneeded == 0) {
917 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d"
918 " map= %p, pagesneeded= %d",
919 dmat->lowaddr, dmat->boundary, dmat->alignment,
920 map, map->pagesneeded);
922 * Count the number of bounce pages
923 * needed in order to complete this transfer
925 vaddr = (vm_offset_t)buf;
926 vendaddr = (vm_offset_t)buf + buflen;
928 while (vaddr < vendaddr) {
929 if (__predict_true(map->pmap == kernel_pmap))
930 paddr = pmap_kextract(vaddr);
932 paddr = pmap_extract(map->pmap, vaddr);
933 if (must_bounce(dmat, map, paddr,
934 min(vendaddr - vaddr, (PAGE_SIZE - ((vm_offset_t)vaddr &
935 PAGE_MASK)))) != 0) {
938 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
941 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded);
946 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
949 /* Reserve Necessary Bounce Pages */
950 mtx_lock(&bounce_lock);
951 if (flags & BUS_DMA_NOWAIT) {
952 if (reserve_bounce_pages(dmat, map, 0) != 0) {
953 map->pagesneeded = 0;
954 mtx_unlock(&bounce_lock);
958 if (reserve_bounce_pages(dmat, map, 1) != 0) {
959 /* Queue us for resources */
960 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
961 mtx_unlock(&bounce_lock);
962 return (EINPROGRESS);
965 mtx_unlock(&bounce_lock);
971 * Add a single contiguous physical range to the segment list.
974 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
975 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
977 bus_addr_t baddr, bmask;
981 * Make sure we don't cross any boundaries.
983 bmask = ~(dmat->boundary - 1);
984 if (dmat->boundary > 0) {
985 baddr = (curaddr + dmat->boundary) & bmask;
986 if (sgsize > (baddr - curaddr))
987 sgsize = (baddr - curaddr);
991 struct arm32_dma_range *dr;
993 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
996 _bus_dmamap_unload(dmat, map);
1000 * In a valid DMA range. Translate the physical
1001 * memory address to an address in the DMA window.
1003 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
1007 * Insert chunk into a segment, coalescing with
1008 * previous segment if possible.
1013 segs[seg].ds_addr = curaddr;
1014 segs[seg].ds_len = sgsize;
1016 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
1017 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
1018 (dmat->boundary == 0 ||
1019 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
1020 segs[seg].ds_len += sgsize;
1022 if (++seg >= dmat->nsegments)
1024 segs[seg].ds_addr = curaddr;
1025 segs[seg].ds_len = sgsize;
1033 * Utility function to load a physical buffer. segp contains
1034 * the starting segment on entrace, and the ending segment on exit.
1037 _bus_dmamap_load_phys(bus_dma_tag_t dmat,
1039 vm_paddr_t buf, bus_size_t buflen,
1041 bus_dma_segment_t *segs,
1049 segs = map->segments;
1051 counter_u64_add(maploads_total, 1);
1052 counter_u64_add(maploads_physmem, 1);
1054 if (might_bounce(dmat, map, buflen, buflen)) {
1055 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
1056 if (map->pagesneeded != 0) {
1057 counter_u64_add(maploads_bounced, 1);
1058 error = _bus_dmamap_reserve_pages(dmat, map, flags);
1064 while (buflen > 0) {
1066 sgsize = MIN(buflen, dmat->maxsegsz);
1067 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr,
1069 sgsize = MIN(sgsize, PAGE_SIZE);
1070 curaddr = add_bounce_page(dmat, map, 0, curaddr,
1073 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1085 _bus_dmamap_unload(dmat, map);
1086 return (EFBIG); /* XXX better return value here? */
1092 _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map,
1093 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
1094 bus_dma_segment_t *segs, int *segp)
1097 return (bus_dmamap_load_ma_triv(dmat, map, ma, tlen, ma_offs, flags,
1102 * Utility function to load a linear buffer. segp contains
1103 * the starting segment on entrace, and the ending segment on exit.
1106 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
1108 void *buf, bus_size_t buflen,
1111 bus_dma_segment_t *segs,
1117 struct sync_list *sl;
1120 counter_u64_add(maploads_total, 1);
1121 if (map->flags & DMAMAP_COHERENT)
1122 counter_u64_add(maploads_coherent, 1);
1123 if (map->flags & DMAMAP_DMAMEM_ALLOC)
1124 counter_u64_add(maploads_dmamem, 1);
1127 segs = map->segments;
1129 if (flags & BUS_DMA_LOAD_MBUF) {
1130 counter_u64_add(maploads_mbuf, 1);
1131 map->flags |= DMAMAP_MBUF;
1136 if (might_bounce(dmat, map, (bus_addr_t)buf, buflen)) {
1137 _bus_dmamap_count_pages(dmat, map, buf, buflen, flags);
1138 if (map->pagesneeded != 0) {
1139 counter_u64_add(maploads_bounced, 1);
1140 error = _bus_dmamap_reserve_pages(dmat, map, flags);
1147 vaddr = (vm_offset_t)buf;
1149 while (buflen > 0) {
1151 * Get the physical address for this segment.
1153 if (__predict_true(map->pmap == kernel_pmap))
1154 curaddr = pmap_kextract(vaddr);
1156 curaddr = pmap_extract(map->pmap, vaddr);
1159 * Compute the segment size, and adjust counts.
1161 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
1162 if (sgsize > dmat->maxsegsz)
1163 sgsize = dmat->maxsegsz;
1164 if (buflen < sgsize)
1167 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr,
1169 curaddr = add_bounce_page(dmat, map, vaddr, curaddr,
1172 sl = &map->slist[map->sync_count - 1];
1173 if (map->sync_count == 0 ||
1175 curaddr != sl->busaddr + sl->datacount ||
1177 vaddr != sl->vaddr + sl->datacount) {
1178 if (++map->sync_count > dmat->nsegments)
1182 sl->datacount = sgsize;
1183 sl->busaddr = curaddr;
1185 sl->datacount += sgsize;
1187 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1200 _bus_dmamap_unload(dmat, map);
1201 return (EFBIG); /* XXX better return value here? */
1208 __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
1209 struct memdesc *mem, bus_dmamap_callback_t *callback,
1215 map->callback = callback;
1216 map->callback_arg = callback_arg;
1220 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
1221 bus_dma_segment_t *segs, int nsegs, int error)
1225 segs = map->segments;
1230 * Release the mapping held by map.
1233 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1235 struct bounce_page *bpage;
1236 struct bounce_zone *bz;
1238 if ((bz = dmat->bounce_zone) != NULL) {
1239 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1240 STAILQ_REMOVE_HEAD(&map->bpages, links);
1241 free_bounce_page(dmat, bpage);
1244 bz = dmat->bounce_zone;
1245 bz->free_bpages += map->pagesreserved;
1246 bz->reserved_bpages -= map->pagesreserved;
1247 map->pagesreserved = 0;
1248 map->pagesneeded = 0;
1250 map->sync_count = 0;
1251 map->flags &= ~DMAMAP_MBUF;
1254 #ifdef notyetbounceuser
1255 /* If busdma uses user pages, then the interrupt handler could
1256 * be use the kernel vm mapping. Both bounce pages and sync list
1257 * do not cross page boundaries.
1258 * Below is a rough sequence that a person would do to fix the
1259 * user page reference in the kernel vmspace. This would be
1260 * done in the dma post routine.
1263 _bus_dmamap_fix_user(vm_offset_t buf, bus_size_t len,
1264 pmap_t pmap, int op)
1271 * each synclist entry is contained within a single page.
1272 * this would be needed if BUS_DMASYNC_POSTxxxx was implemented
1274 curaddr = pmap_extract(pmap, buf);
1275 va = pmap_dma_map(curaddr);
1278 cpu_dcache_wb_range(va, sgsize);
1281 case SYNC_USER_COPYTO:
1282 bcopy((void *)va, (void *)bounce, sgsize);
1285 case SYNC_USER_COPYFROM:
1286 bcopy((void *) bounce, (void *)va, sgsize);
1298 #define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(pa, size)
1299 #define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(pa, size)
1300 #define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(pa, size)
1302 #define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(va, size)
1303 #define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(va, size)
1304 #define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(va, size)
1308 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1310 struct bounce_page *bpage;
1311 struct sync_list *sl, *end;
1313 * If the buffer was from user space, it is possible that this is not
1314 * the same vm map, especially on a POST operation. It's not clear that
1315 * dma on userland buffers can work at all right now. To be safe, until
1316 * we're able to test direct userland dma, panic on a map mismatch.
1318 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1319 if (!pmap_dmap_iscurrent(map->pmap))
1320 panic("_bus_dmamap_sync: wrong user map for bounce sync.");
1322 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1323 "performing bounce", __func__, dmat, dmat->flags, op);
1326 * For PREWRITE do a writeback. Clean the caches from the
1327 * innermost to the outermost levels.
1329 if (op & BUS_DMASYNC_PREWRITE) {
1330 while (bpage != NULL) {
1331 if (bpage->datavaddr != 0)
1332 bcopy((void *)bpage->datavaddr,
1333 (void *)bpage->vaddr,
1336 physcopyout(bpage->dataaddr,
1337 (void *)bpage->vaddr,
1339 cpu_dcache_wb_range((vm_offset_t)bpage->vaddr,
1341 l2cache_wb_range((vm_offset_t)bpage->vaddr,
1342 (vm_offset_t)bpage->busaddr,
1344 bpage = STAILQ_NEXT(bpage, links);
1346 dmat->bounce_zone->total_bounced++;
1350 * Do an invalidate for PREREAD unless a writeback was already
1351 * done above due to PREWRITE also being set. The reason for a
1352 * PREREAD invalidate is to prevent dirty lines currently in the
1353 * cache from being evicted during the DMA. If a writeback was
1354 * done due to PREWRITE also being set there will be no dirty
1355 * lines and the POSTREAD invalidate handles the rest. The
1356 * invalidate is done from the innermost to outermost level. If
1357 * L2 were done first, a dirty cacheline could be automatically
1358 * evicted from L1 before we invalidated it, re-dirtying the L2.
1360 if ((op & BUS_DMASYNC_PREREAD) && !(op & BUS_DMASYNC_PREWRITE)) {
1361 bpage = STAILQ_FIRST(&map->bpages);
1362 while (bpage != NULL) {
1363 cpu_dcache_inv_range((vm_offset_t)bpage->vaddr,
1365 l2cache_inv_range((vm_offset_t)bpage->vaddr,
1366 (vm_offset_t)bpage->busaddr,
1368 bpage = STAILQ_NEXT(bpage, links);
1373 * Re-invalidate the caches on a POSTREAD, even though they were
1374 * already invalidated at PREREAD time. Aggressive prefetching
1375 * due to accesses to other data near the dma buffer could have
1376 * brought buffer data into the caches which is now stale. The
1377 * caches are invalidated from the outermost to innermost; the
1378 * prefetches could be happening right now, and if L1 were
1379 * invalidated first, stale L2 data could be prefetched into L1.
1381 if (op & BUS_DMASYNC_POSTREAD) {
1382 while (bpage != NULL) {
1387 startv = bpage->vaddr &~ arm_dcache_align_mask;
1388 startp = bpage->busaddr &~ arm_dcache_align_mask;
1389 len = bpage->datacount;
1391 if (startv != bpage->vaddr)
1392 len += bpage->vaddr & arm_dcache_align_mask;
1393 if (len & arm_dcache_align_mask)
1395 (len & arm_dcache_align_mask)) +
1397 l2cache_inv_range(startv, startp, len);
1398 cpu_dcache_inv_range(startv, len);
1399 if (bpage->datavaddr != 0)
1400 bcopy((void *)bpage->vaddr,
1401 (void *)bpage->datavaddr,
1404 physcopyin((void *)bpage->vaddr,
1407 bpage = STAILQ_NEXT(bpage, links);
1409 dmat->bounce_zone->total_bounced++;
1414 * For COHERENT memory no cache maintenance is necessary, but ensure all
1415 * writes have reached memory for the PREWRITE case. No action is
1416 * needed for a PREREAD without PREWRITE also set, because that would
1417 * imply that the cpu had written to the COHERENT buffer and expected
1418 * the dma device to see that change, and by definition a PREWRITE sync
1419 * is required to make that happen.
1421 if (map->flags & DMAMAP_COHERENT) {
1422 if (op & BUS_DMASYNC_PREWRITE) {
1424 cpu_l2cache_drain_writebuf();
1430 * Cache maintenance for normal (non-COHERENT non-bounce) buffers. All
1431 * the comments about the sequences for flushing cache levels in the
1432 * bounce buffer code above apply here as well. In particular, the fact
1433 * that the sequence is inner-to-outer for PREREAD invalidation and
1434 * outer-to-inner for POSTREAD invalidation is not a mistake.
1436 if (map->sync_count != 0) {
1437 if (!pmap_dmap_iscurrent(map->pmap))
1438 panic("_bus_dmamap_sync: wrong user map for sync.");
1440 sl = &map->slist[0];
1441 end = &map->slist[map->sync_count];
1442 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1443 "performing sync", __func__, dmat, dmat->flags, op);
1446 case BUS_DMASYNC_PREWRITE:
1447 case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD:
1449 cpu_dcache_wb_range(sl->vaddr, sl->datacount);
1450 l2cache_wb_range(sl->vaddr, sl->busaddr,
1456 case BUS_DMASYNC_PREREAD:
1458 * An mbuf may start in the middle of a cacheline. There
1459 * will be no cpu writes to the beginning of that line
1460 * (which contains the mbuf header) while dma is in
1461 * progress. Handle that case by doing a writeback of
1462 * just the first cacheline before invalidating the
1463 * overall buffer. Any mbuf in a chain may have this
1464 * misalignment. Buffers which are not mbufs bounce if
1465 * they are not aligned to a cacheline.
1468 if (sl->vaddr & arm_dcache_align_mask) {
1469 KASSERT(map->flags & DMAMAP_MBUF,
1470 ("unaligned buffer is not an mbuf"));
1471 cpu_dcache_wb_range(sl->vaddr, 1);
1472 l2cache_wb_range(sl->vaddr,
1475 cpu_dcache_inv_range(sl->vaddr, sl->datacount);
1476 l2cache_inv_range(sl->vaddr, sl->busaddr,
1482 case BUS_DMASYNC_POSTWRITE:
1485 case BUS_DMASYNC_POSTREAD:
1486 case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE:
1488 l2cache_inv_range(sl->vaddr, sl->busaddr,
1490 cpu_dcache_inv_range(sl->vaddr, sl->datacount);
1496 panic("unsupported combination of sync operations: 0x%08x\n", op);
1503 init_bounce_pages(void *dummy __unused)
1507 STAILQ_INIT(&bounce_zone_list);
1508 STAILQ_INIT(&bounce_map_waitinglist);
1509 STAILQ_INIT(&bounce_map_callbacklist);
1510 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
1512 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1514 static struct sysctl_ctx_list *
1515 busdma_sysctl_tree(struct bounce_zone *bz)
1518 return (&bz->sysctl_tree);
1521 static struct sysctl_oid *
1522 busdma_sysctl_tree_top(struct bounce_zone *bz)
1525 return (bz->sysctl_tree_top);
1529 alloc_bounce_zone(bus_dma_tag_t dmat)
1531 struct bounce_zone *bz;
1533 /* Check to see if we already have a suitable zone */
1534 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1535 if ((dmat->alignment <= bz->alignment) &&
1536 (dmat->lowaddr >= bz->lowaddr)) {
1537 dmat->bounce_zone = bz;
1542 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1543 M_NOWAIT | M_ZERO)) == NULL)
1546 STAILQ_INIT(&bz->bounce_page_list);
1547 bz->free_bpages = 0;
1548 bz->reserved_bpages = 0;
1549 bz->active_bpages = 0;
1550 bz->lowaddr = dmat->lowaddr;
1551 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1553 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1555 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1556 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1557 dmat->bounce_zone = bz;
1559 sysctl_ctx_init(&bz->sysctl_tree);
1560 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1561 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1563 if (bz->sysctl_tree_top == NULL) {
1564 sysctl_ctx_free(&bz->sysctl_tree);
1565 return (0); /* XXX error code? */
1568 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1569 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1570 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1571 "Total bounce pages");
1572 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1573 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1574 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1575 "Free bounce pages");
1576 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1577 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1578 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1579 "Reserved bounce pages");
1580 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1581 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1582 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1583 "Active bounce pages");
1584 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1585 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1586 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1587 "Total bounce requests (pages bounced)");
1588 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1589 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1590 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1591 "Total bounce requests that were deferred");
1592 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1593 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1594 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1595 SYSCTL_ADD_ULONG(busdma_sysctl_tree(bz),
1596 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1597 "alignment", CTLFLAG_RD, &bz->alignment, "");
1603 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1605 struct bounce_zone *bz;
1608 bz = dmat->bounce_zone;
1610 while (numpages > 0) {
1611 struct bounce_page *bpage;
1613 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1618 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1619 M_NOWAIT, 0ul, bz->lowaddr, PAGE_SIZE, 0);
1620 if (bpage->vaddr == 0) {
1621 free(bpage, M_DEVBUF);
1624 bpage->busaddr = pmap_kextract(bpage->vaddr);
1625 mtx_lock(&bounce_lock);
1626 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1630 mtx_unlock(&bounce_lock);
1638 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1640 struct bounce_zone *bz;
1643 mtx_assert(&bounce_lock, MA_OWNED);
1644 bz = dmat->bounce_zone;
1645 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1646 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1647 return (map->pagesneeded - (map->pagesreserved + pages));
1648 bz->free_bpages -= pages;
1649 bz->reserved_bpages += pages;
1650 map->pagesreserved += pages;
1651 pages = map->pagesneeded - map->pagesreserved;
1657 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1658 bus_addr_t addr, bus_size_t size)
1660 struct bounce_zone *bz;
1661 struct bounce_page *bpage;
1663 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1664 KASSERT(map != NULL,
1665 ("add_bounce_page: bad map %p", map));
1667 bz = dmat->bounce_zone;
1668 if (map->pagesneeded == 0)
1669 panic("add_bounce_page: map doesn't need any pages");
1672 if (map->pagesreserved == 0)
1673 panic("add_bounce_page: map doesn't need any pages");
1674 map->pagesreserved--;
1676 mtx_lock(&bounce_lock);
1677 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1679 panic("add_bounce_page: free page list is empty");
1681 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1682 bz->reserved_bpages--;
1683 bz->active_bpages++;
1684 mtx_unlock(&bounce_lock);
1686 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1687 /* Page offset needs to be preserved. */
1688 bpage->vaddr |= addr & PAGE_MASK;
1689 bpage->busaddr |= addr & PAGE_MASK;
1691 bpage->datavaddr = vaddr;
1692 bpage->dataaddr = addr;
1693 bpage->datacount = size;
1694 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1695 return (bpage->busaddr);
1699 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1701 struct bus_dmamap *map;
1702 struct bounce_zone *bz;
1704 bz = dmat->bounce_zone;
1705 bpage->datavaddr = 0;
1706 bpage->datacount = 0;
1707 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1709 * Reset the bounce page to start at offset 0. Other uses
1710 * of this bounce page may need to store a full page of
1711 * data and/or assume it starts on a page boundary.
1713 bpage->vaddr &= ~PAGE_MASK;
1714 bpage->busaddr &= ~PAGE_MASK;
1717 mtx_lock(&bounce_lock);
1718 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1720 bz->active_bpages--;
1721 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1722 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1723 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1724 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1726 busdma_swi_pending = 1;
1727 bz->total_deferred++;
1728 swi_sched(vm_ih, 0);
1731 mtx_unlock(&bounce_lock);
1738 struct bus_dmamap *map;
1740 mtx_lock(&bounce_lock);
1741 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1742 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1743 mtx_unlock(&bounce_lock);
1745 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_LOCK);
1746 bus_dmamap_load_mem(map->dmat, map, &map->mem, map->callback,
1747 map->callback_arg, BUS_DMA_WAITOK);
1748 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1749 mtx_lock(&bounce_lock);
1751 mtx_unlock(&bounce_lock);