2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * From amd64/busdma_machdep.c, r204214
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
43 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
52 #include <machine/atomic.h>
53 #include <machine/bus.h>
54 #include <machine/md_var.h>
56 #define MAX_BPAGES 8192
66 bus_dma_filter_t *filter;
74 bus_dma_lock_t *lockfunc;
76 bus_dma_segment_t *segments;
77 struct bounce_zone *bounce_zone;
81 vm_offset_t vaddr; /* kva of bounce buffer */
82 bus_addr_t busaddr; /* Physical address */
83 vm_offset_t datavaddr; /* kva of client data */
84 bus_size_t datacount; /* client data count */
85 STAILQ_ENTRY(bounce_page) links;
88 int busdma_swi_pending;
91 STAILQ_ENTRY(bounce_zone) links;
92 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
100 bus_size_t alignment;
104 struct sysctl_ctx_list sysctl_tree;
105 struct sysctl_oid *sysctl_tree_top;
108 static struct mtx bounce_lock;
109 static int total_bpages;
110 static int busdma_zonecount;
111 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
113 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
114 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
115 "Total bounce pages");
118 struct bp_list bpages;
122 void *buf; /* unmapped buffer pointer */
123 bus_size_t buflen; /* unmapped buffer length */
124 bus_dmamap_callback_t *callback;
126 STAILQ_ENTRY(bus_dmamap) links;
129 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
130 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
131 static struct bus_dmamap nobounce_dmamap;
133 static void init_bounce_pages(void *dummy);
134 static int alloc_bounce_zone(bus_dma_tag_t dmat);
135 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
136 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
138 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
139 vm_offset_t vaddr, bus_size_t size);
140 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
141 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
144 * Return true if a match is made.
146 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
148 * If paddr is within the bounds of the dma tag then call the filter callback
149 * to check for a match, if there is no filter callback then assume a match.
152 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
159 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
160 || ((paddr & (dmat->alignment - 1)) != 0))
161 && (dmat->filter == NULL
162 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
166 } while (retval == 0 && dmat != NULL);
171 * Convenience function for manipulating driver locks from busdma (during
172 * busdma_swi, for example). Drivers that don't provide their own locks
173 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
174 * non-mutex locking scheme don't have to use this at all.
177 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
181 dmtx = (struct mtx *)arg;
190 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
195 * dflt_lock should never get called. It gets put into the dma tag when
196 * lockfunc == NULL, which is only valid if the maps that are associated
197 * with the tag are meant to never be defered.
198 * XXX Should have a way to identify which driver is responsible here.
201 dflt_lock(void *arg, bus_dma_lock_op_t op)
203 panic("driver error: busdma dflt_lock called");
206 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
207 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
209 * Allocate a device specific dma_tag.
212 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
213 bus_size_t boundary, bus_addr_t lowaddr,
214 bus_addr_t highaddr, bus_dma_filter_t *filter,
215 void *filterarg, bus_size_t maxsize, int nsegments,
216 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
217 void *lockfuncarg, bus_dma_tag_t *dmat)
219 bus_dma_tag_t newtag;
222 /* Basic sanity checking */
223 if (boundary != 0 && boundary < maxsegsz)
230 /* Return a NULL tag on failure */
233 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
235 if (newtag == NULL) {
236 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
237 __func__, newtag, 0, error);
241 newtag->parent = parent;
242 newtag->alignment = alignment;
243 newtag->boundary = boundary;
244 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
245 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
246 newtag->filter = filter;
247 newtag->filterarg = filterarg;
248 newtag->maxsize = maxsize;
249 newtag->nsegments = nsegments;
250 newtag->maxsegsz = maxsegsz;
251 newtag->flags = flags;
252 newtag->ref_count = 1; /* Count ourself */
253 newtag->map_count = 0;
254 if (lockfunc != NULL) {
255 newtag->lockfunc = lockfunc;
256 newtag->lockfuncarg = lockfuncarg;
258 newtag->lockfunc = dflt_lock;
259 newtag->lockfuncarg = NULL;
261 newtag->segments = NULL;
263 /* Take into account any restrictions imposed by our parent tag */
264 if (parent != NULL) {
265 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
266 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
267 if (newtag->boundary == 0)
268 newtag->boundary = parent->boundary;
269 else if (parent->boundary != 0)
270 newtag->boundary = MIN(parent->boundary,
272 if (newtag->filter == NULL) {
274 * Short circuit looking at our parent directly
275 * since we have encapsulated all of its information
277 newtag->filter = parent->filter;
278 newtag->filterarg = parent->filterarg;
279 newtag->parent = parent->parent;
281 if (newtag->parent != NULL)
282 atomic_add_int(&parent->ref_count, 1);
285 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
286 || newtag->alignment > 1)
287 newtag->flags |= BUS_DMA_COULD_BOUNCE;
289 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
290 (flags & BUS_DMA_ALLOCNOW) != 0) {
291 struct bounce_zone *bz;
295 if ((error = alloc_bounce_zone(newtag)) != 0) {
296 free(newtag, M_DEVBUF);
299 bz = newtag->bounce_zone;
301 if (ptoa(bz->total_bpages) < maxsize) {
304 pages = atop(maxsize) - bz->total_bpages;
306 /* Add pages to our bounce pool */
307 if (alloc_bounce_pages(newtag, pages) < pages)
310 /* Performed initial allocation */
311 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
315 free(newtag, M_DEVBUF);
319 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
320 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
325 bus_dma_tag_destroy(bus_dma_tag_t dmat)
327 bus_dma_tag_t dmat_copy;
335 if (dmat->map_count != 0) {
340 while (dmat != NULL) {
341 bus_dma_tag_t parent;
343 parent = dmat->parent;
344 atomic_subtract_int(&dmat->ref_count, 1);
345 if (dmat->ref_count == 0) {
346 if (dmat->segments != NULL)
347 free(dmat->segments, M_DEVBUF);
348 free(dmat, M_DEVBUF);
350 * Last reference count, so
351 * release our reference
352 * count on our parent.
360 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
365 * Allocate a handle for mapping from kva/uva/physical
366 * address space into bus device space.
369 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
375 if (dmat->segments == NULL) {
376 dmat->segments = (bus_dma_segment_t *)malloc(
377 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
379 if (dmat->segments == NULL) {
380 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
381 __func__, dmat, ENOMEM);
387 * Bouncing might be required if the driver asks for an active
388 * exclusion region, a data alignment that is stricter than 1, and/or
389 * an active address boundary.
391 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
394 struct bounce_zone *bz;
397 if (dmat->bounce_zone == NULL) {
398 if ((error = alloc_bounce_zone(dmat)) != 0)
401 bz = dmat->bounce_zone;
403 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
406 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
407 __func__, dmat, ENOMEM);
411 /* Initialize the new map */
412 STAILQ_INIT(&((*mapp)->bpages));
415 * Attempt to add pages to our pool on a per-instance
416 * basis up to a sane limit.
418 if (dmat->alignment > 1)
419 maxpages = MAX_BPAGES;
421 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
422 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
423 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
426 pages = MAX(atop(dmat->maxsize), 1);
427 pages = MIN(maxpages - bz->total_bpages, pages);
428 pages = MAX(pages, 1);
429 if (alloc_bounce_pages(dmat, pages) < pages)
432 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
434 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
445 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
446 __func__, dmat, dmat->flags, error);
451 * Destroy a handle for mapping from kva/uva/physical
452 * address space into bus device space.
455 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
457 if (map != NULL && map != &nobounce_dmamap) {
458 if (STAILQ_FIRST(&map->bpages) != NULL) {
459 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
460 __func__, dmat, EBUSY);
463 if (dmat->bounce_zone)
464 dmat->bounce_zone->map_count--;
468 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
474 * Allocate a piece of memory that can be efficiently mapped into
475 * bus device space based on the constraints lited in the dma tag.
476 * A dmamap to for use with dmamap_load is also allocated.
479 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
484 if (flags & BUS_DMA_NOWAIT)
489 /* If we succeed, no mapping/bouncing will be required */
492 if (dmat->segments == NULL) {
493 dmat->segments = (bus_dma_segment_t *)malloc(
494 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
496 if (dmat->segments == NULL) {
497 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
498 __func__, dmat, dmat->flags, ENOMEM);
502 if (flags & BUS_DMA_ZERO)
507 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
508 * alignment guarantees of malloc need to be nailed down, and the
509 * code below should be rewritten to take that into account.
511 * In the meantime, we'll warn the user if malloc gets it wrong.
513 if ((dmat->maxsize <= PAGE_SIZE) &&
514 (dmat->alignment < dmat->maxsize) &&
515 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
516 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
519 * XXX Use Contigmalloc until it is merged into this facility
520 * and handles multi-seg allocations. Nobody is doing
521 * multi-seg allocations yet though.
522 * XXX Certain AGP hardware does.
524 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
525 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
528 if (*vaddr == NULL) {
529 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
530 __func__, dmat, dmat->flags, ENOMEM);
532 } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
533 printf("bus_dmamem_alloc failed to align memory properly.\n");
536 if (flags & BUS_DMA_NOCACHE)
537 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
540 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
541 __func__, dmat, dmat->flags, 0);
546 * Free a piece of memory and it's allociated dmamap, that was allocated
547 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
550 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
553 * dmamem does not need to be bounced, so the map should be
557 panic("bus_dmamem_free: Invalid map freed\n");
559 pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
561 if ((dmat->maxsize <= PAGE_SIZE) &&
562 (dmat->alignment < dmat->maxsize) &&
563 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
564 free(vaddr, M_DEVBUF);
566 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
568 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
572 * Utility function to load a linear buffer. lastaddrp holds state
573 * between invocations (for multiple-buffer loads). segp contains
574 * the starting segment on entrance, and the ending segment on exit.
575 * first indicates if this is the first invocation of this function.
578 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
580 void *buf, bus_size_t buflen,
583 bus_addr_t *lastaddrp,
584 bus_dma_segment_t *segs,
589 bus_addr_t curaddr, lastaddr, baddr, bmask;
595 map = &nobounce_dmamap;
597 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
598 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
599 vm_offset_t vendaddr;
601 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
602 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
603 dmat->boundary, dmat->alignment);
604 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
605 map, &nobounce_dmamap, map->pagesneeded);
607 * Count the number of bounce pages
608 * needed in order to complete this transfer
610 vaddr = (vm_offset_t)buf;
611 vendaddr = (vm_offset_t)buf + buflen;
613 while (vaddr < vendaddr) {
616 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
618 paddr = pmap_extract(pmap, vaddr);
620 paddr = pmap_kextract(vaddr);
621 if (run_filter(dmat, paddr) != 0) {
622 sg_len = roundup2(sg_len, dmat->alignment);
627 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
630 /* Reserve Necessary Bounce Pages */
631 if (map->pagesneeded != 0) {
632 mtx_lock(&bounce_lock);
633 if (flags & BUS_DMA_NOWAIT) {
634 if (reserve_bounce_pages(dmat, map, 0) != 0) {
635 mtx_unlock(&bounce_lock);
639 if (reserve_bounce_pages(dmat, map, 1) != 0) {
640 /* Queue us for resources */
643 map->buflen = buflen;
644 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
646 mtx_unlock(&bounce_lock);
647 return (EINPROGRESS);
650 mtx_unlock(&bounce_lock);
653 vaddr = (vm_offset_t)buf;
654 lastaddr = *lastaddrp;
655 bmask = ~(dmat->boundary - 1);
657 for (seg = *segp; buflen > 0 ; ) {
658 bus_size_t max_sgsize;
661 * Get the physical address for this segment.
664 curaddr = pmap_extract(pmap, vaddr);
666 curaddr = pmap_kextract(vaddr);
669 * Compute the segment size, and adjust counts.
671 max_sgsize = MIN(buflen, dmat->maxsegsz);
672 sgsize = PAGE_SIZE - ((vm_offset_t)curaddr & PAGE_MASK);
673 if (map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
674 sgsize = roundup2(sgsize, dmat->alignment);
675 sgsize = MIN(sgsize, max_sgsize);
676 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
678 sgsize = MIN(sgsize, max_sgsize);
682 * Make sure we don't cross any boundaries.
684 if (dmat->boundary > 0) {
685 baddr = (curaddr + dmat->boundary) & bmask;
686 if (sgsize > (baddr - curaddr))
687 sgsize = (baddr - curaddr);
691 * Insert chunk into a segment, coalescing with
692 * previous segment if possible.
695 segs[seg].ds_addr = curaddr;
696 segs[seg].ds_len = sgsize;
699 if (curaddr == lastaddr &&
700 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
701 (dmat->boundary == 0 ||
702 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
703 segs[seg].ds_len += sgsize;
705 if (++seg >= dmat->nsegments)
707 segs[seg].ds_addr = curaddr;
708 segs[seg].ds_len = sgsize;
712 lastaddr = curaddr + sgsize;
718 *lastaddrp = lastaddr;
723 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
727 * Map the buffer buf into bus space using the dmamap map.
730 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
731 bus_size_t buflen, bus_dmamap_callback_t *callback,
732 void *callback_arg, int flags)
734 bus_addr_t lastaddr = 0;
735 int error, nsegs = 0;
738 flags |= BUS_DMA_WAITOK;
739 map->callback = callback;
740 map->callback_arg = callback_arg;
743 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
744 &lastaddr, dmat->segments, &nsegs, 1);
746 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
747 __func__, dmat, dmat->flags, error, nsegs + 1);
749 if (error == EINPROGRESS) {
754 (*callback)(callback_arg, dmat->segments, 0, error);
756 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
759 * Return ENOMEM to the caller so that it can pass it up the stack.
760 * This error only happens when NOWAIT is set, so deferal is disabled.
770 * Like _bus_dmamap_load(), but for mbufs.
773 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
775 bus_dmamap_callback2_t *callback, void *callback_arg,
782 flags |= BUS_DMA_NOWAIT;
785 if (m0->m_pkthdr.len <= dmat->maxsize) {
787 bus_addr_t lastaddr = 0;
790 for (m = m0; m != NULL && error == 0; m = m->m_next) {
792 error = _bus_dmamap_load_buffer(dmat, map,
794 NULL, flags, &lastaddr,
795 dmat->segments, &nsegs, first);
804 /* force "no valid mappings" in callback */
805 (*callback)(callback_arg, dmat->segments, 0, 0, error);
807 (*callback)(callback_arg, dmat->segments,
808 nsegs+1, m0->m_pkthdr.len, error);
810 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
811 __func__, dmat, dmat->flags, error, nsegs + 1);
816 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
817 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
824 flags |= BUS_DMA_NOWAIT;
827 if (m0->m_pkthdr.len <= dmat->maxsize) {
829 bus_addr_t lastaddr = 0;
832 for (m = m0; m != NULL && error == 0; m = m->m_next) {
834 error = _bus_dmamap_load_buffer(dmat, map,
836 NULL, flags, &lastaddr,
845 /* XXX FIXME: Having to increment nsegs is really annoying */
847 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
848 __func__, dmat, dmat->flags, error, *nsegs);
853 * Like _bus_dmamap_load(), but for uios.
856 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
858 bus_dmamap_callback2_t *callback, void *callback_arg,
861 bus_addr_t lastaddr = 0;
862 int nsegs, error, first, i;
867 flags |= BUS_DMA_NOWAIT;
868 resid = uio->uio_resid;
871 if (uio->uio_segflg == UIO_USERSPACE) {
872 KASSERT(uio->uio_td != NULL,
873 ("bus_dmamap_load_uio: USERSPACE but no proc"));
874 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
881 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
883 * Now at the first iovec to load. Load each iovec
884 * until we have exhausted the residual count.
887 resid < iov[i].iov_len ? resid : iov[i].iov_len;
888 caddr_t addr = (caddr_t) iov[i].iov_base;
891 error = _bus_dmamap_load_buffer(dmat, map,
892 addr, minlen, pmap, flags, &lastaddr,
893 dmat->segments, &nsegs, first);
901 /* force "no valid mappings" in callback */
902 (*callback)(callback_arg, dmat->segments, 0, 0, error);
904 (*callback)(callback_arg, dmat->segments,
905 nsegs+1, uio->uio_resid, error);
907 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
908 __func__, dmat, dmat->flags, error, nsegs + 1);
913 * Release the mapping held by map.
916 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
918 struct bounce_page *bpage;
920 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
921 STAILQ_REMOVE_HEAD(&map->bpages, links);
922 free_bounce_page(dmat, bpage);
927 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
929 struct bounce_page *bpage;
931 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
933 * Handle data bouncing. We might also
934 * want to add support for invalidating
935 * the caches on broken hardware
937 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
938 "performing bounce", __func__, op, dmat, dmat->flags);
940 if (op & BUS_DMASYNC_PREWRITE) {
941 while (bpage != NULL) {
942 bcopy((void *)bpage->datavaddr,
943 (void *)bpage->vaddr,
945 bpage = STAILQ_NEXT(bpage, links);
947 dmat->bounce_zone->total_bounced++;
950 if (op & BUS_DMASYNC_POSTREAD) {
951 while (bpage != NULL) {
952 bcopy((void *)bpage->vaddr,
953 (void *)bpage->datavaddr,
955 bpage = STAILQ_NEXT(bpage, links);
957 dmat->bounce_zone->total_bounced++;
963 init_bounce_pages(void *dummy __unused)
967 STAILQ_INIT(&bounce_zone_list);
968 STAILQ_INIT(&bounce_map_waitinglist);
969 STAILQ_INIT(&bounce_map_callbacklist);
970 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
972 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
974 static struct sysctl_ctx_list *
975 busdma_sysctl_tree(struct bounce_zone *bz)
977 return (&bz->sysctl_tree);
980 static struct sysctl_oid *
981 busdma_sysctl_tree_top(struct bounce_zone *bz)
983 return (bz->sysctl_tree_top);
987 alloc_bounce_zone(bus_dma_tag_t dmat)
989 struct bounce_zone *bz;
991 /* Check to see if we already have a suitable zone */
992 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
993 if ((dmat->alignment <= bz->alignment)
994 && (dmat->lowaddr >= bz->lowaddr)) {
995 dmat->bounce_zone = bz;
1000 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1001 M_NOWAIT | M_ZERO)) == NULL)
1004 STAILQ_INIT(&bz->bounce_page_list);
1005 bz->free_bpages = 0;
1006 bz->reserved_bpages = 0;
1007 bz->active_bpages = 0;
1008 bz->lowaddr = dmat->lowaddr;
1009 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1011 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1013 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1014 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1015 dmat->bounce_zone = bz;
1017 sysctl_ctx_init(&bz->sysctl_tree);
1018 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1019 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1021 if (bz->sysctl_tree_top == NULL) {
1022 sysctl_ctx_free(&bz->sysctl_tree);
1023 return (0); /* XXX error code? */
1026 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1027 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1028 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1029 "Total bounce pages");
1030 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1031 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1032 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1033 "Free bounce pages");
1034 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1035 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1036 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1037 "Reserved bounce pages");
1038 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1039 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1040 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1041 "Active bounce pages");
1042 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1043 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1044 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1045 "Total bounce requests");
1046 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1047 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1048 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1049 "Total bounce requests that were deferred");
1050 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1051 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1052 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1053 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1054 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1055 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1061 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1063 struct bounce_zone *bz;
1066 bz = dmat->bounce_zone;
1068 while (numpages > 0) {
1069 struct bounce_page *bpage;
1071 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1076 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1081 if (bpage->vaddr == 0) {
1082 free(bpage, M_DEVBUF);
1085 bpage->busaddr = pmap_kextract(bpage->vaddr);
1086 mtx_lock(&bounce_lock);
1087 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1091 mtx_unlock(&bounce_lock);
1099 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1101 struct bounce_zone *bz;
1104 mtx_assert(&bounce_lock, MA_OWNED);
1105 bz = dmat->bounce_zone;
1106 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1107 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1108 return (map->pagesneeded - (map->pagesreserved + pages));
1109 bz->free_bpages -= pages;
1110 bz->reserved_bpages += pages;
1111 map->pagesreserved += pages;
1112 pages = map->pagesneeded - map->pagesreserved;
1118 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1121 struct bounce_zone *bz;
1122 struct bounce_page *bpage;
1124 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1125 KASSERT(map != NULL && map != &nobounce_dmamap,
1126 ("add_bounce_page: bad map %p", map));
1128 bz = dmat->bounce_zone;
1129 if (map->pagesneeded == 0)
1130 panic("add_bounce_page: map doesn't need any pages");
1133 if (map->pagesreserved == 0)
1134 panic("add_bounce_page: map doesn't need any pages");
1135 map->pagesreserved--;
1137 mtx_lock(&bounce_lock);
1138 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1140 panic("add_bounce_page: free page list is empty");
1142 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1143 bz->reserved_bpages--;
1144 bz->active_bpages++;
1145 mtx_unlock(&bounce_lock);
1147 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1148 /* Page offset needs to be preserved. */
1149 bpage->vaddr |= vaddr & PAGE_MASK;
1150 bpage->busaddr |= vaddr & PAGE_MASK;
1152 bpage->datavaddr = vaddr;
1153 bpage->datacount = size;
1154 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1155 return (bpage->busaddr);
1159 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1161 struct bus_dmamap *map;
1162 struct bounce_zone *bz;
1164 bz = dmat->bounce_zone;
1165 bpage->datavaddr = 0;
1166 bpage->datacount = 0;
1167 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1169 * Reset the bounce page to start at offset 0. Other uses
1170 * of this bounce page may need to store a full page of
1171 * data and/or assume it starts on a page boundary.
1173 bpage->vaddr &= ~PAGE_MASK;
1174 bpage->busaddr &= ~PAGE_MASK;
1177 mtx_lock(&bounce_lock);
1178 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1180 bz->active_bpages--;
1181 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1182 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1183 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1184 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1186 busdma_swi_pending = 1;
1187 bz->total_deferred++;
1188 swi_sched(vm_ih, 0);
1191 mtx_unlock(&bounce_lock);
1198 struct bus_dmamap *map;
1200 mtx_lock(&bounce_lock);
1201 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1202 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1203 mtx_unlock(&bounce_lock);
1205 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1206 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1207 map->callback, map->callback_arg, /*flags*/0);
1208 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1209 mtx_lock(&bounce_lock);
1211 mtx_unlock(&bounce_lock);