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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
34 #include <sys/interrupt.h>
35 #include <sys/kernel.h>
39 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_map.h>
48 #include <machine/atomic.h>
49 #include <machine/bus.h>
50 #include <machine/md_var.h>
51 #include <machine/specialreg.h>
53 #define MAX_BPAGES 8192
63 bus_dma_filter_t *filter;
71 bus_dma_lock_t *lockfunc;
73 bus_dma_segment_t *segments;
74 struct bounce_zone *bounce_zone;
78 vm_offset_t vaddr; /* kva of bounce buffer */
79 bus_addr_t busaddr; /* Physical address */
80 vm_offset_t datavaddr; /* kva of client data */
81 bus_size_t datacount; /* client data count */
82 STAILQ_ENTRY(bounce_page) links;
85 int busdma_swi_pending;
88 STAILQ_ENTRY(bounce_zone) links;
89 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
101 struct sysctl_ctx_list sysctl_tree;
102 struct sysctl_oid *sysctl_tree_top;
105 static struct mtx bounce_lock;
106 static int total_bpages;
107 static int busdma_zonecount;
108 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
110 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
111 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
112 "Total bounce pages");
115 struct bp_list bpages;
119 void *buf; /* unmapped buffer pointer */
120 bus_size_t buflen; /* unmapped buffer length */
121 bus_dmamap_callback_t *callback;
123 STAILQ_ENTRY(bus_dmamap) links;
126 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
127 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
128 static struct bus_dmamap nobounce_dmamap;
130 static void init_bounce_pages(void *dummy);
131 static int alloc_bounce_zone(bus_dma_tag_t dmat);
132 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
133 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
135 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
136 vm_offset_t vaddr, bus_size_t size);
137 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
138 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
141 * Return true if a match is made.
143 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
145 * If paddr is within the bounds of the dma tag then call the filter callback
146 * to check for a match, if there is no filter callback then assume a match.
149 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
156 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
157 || ((paddr & (dmat->alignment - 1)) != 0))
158 && (dmat->filter == NULL
159 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
163 } while (retval == 0 && dmat != NULL);
168 * Convenience function for manipulating driver locks from busdma (during
169 * busdma_swi, for example). Drivers that don't provide their own locks
170 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
171 * non-mutex locking scheme don't have to use this at all.
174 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
178 dmtx = (struct mtx *)arg;
187 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
192 * dflt_lock should never get called. It gets put into the dma tag when
193 * lockfunc == NULL, which is only valid if the maps that are associated
194 * with the tag are meant to never be defered.
195 * XXX Should have a way to identify which driver is responsible here.
198 dflt_lock(void *arg, bus_dma_lock_op_t op)
200 panic("driver error: busdma dflt_lock called");
203 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
204 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
206 * Allocate a device specific dma_tag.
209 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
210 bus_size_t boundary, bus_addr_t lowaddr,
211 bus_addr_t highaddr, bus_dma_filter_t *filter,
212 void *filterarg, bus_size_t maxsize, int nsegments,
213 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
214 void *lockfuncarg, bus_dma_tag_t *dmat)
216 bus_dma_tag_t newtag;
219 /* Basic sanity checking */
220 if (boundary != 0 && boundary < maxsegsz)
227 /* Return a NULL tag on failure */
230 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
232 if (newtag == NULL) {
233 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
234 __func__, newtag, 0, error);
238 newtag->parent = parent;
239 newtag->alignment = alignment;
240 newtag->boundary = boundary;
241 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
242 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
244 newtag->filter = filter;
245 newtag->filterarg = filterarg;
246 newtag->maxsize = maxsize;
247 newtag->nsegments = nsegments;
248 newtag->maxsegsz = maxsegsz;
249 newtag->flags = flags;
250 newtag->ref_count = 1; /* Count ourself */
251 newtag->map_count = 0;
252 if (lockfunc != NULL) {
253 newtag->lockfunc = lockfunc;
254 newtag->lockfuncarg = lockfuncarg;
256 newtag->lockfunc = dflt_lock;
257 newtag->lockfuncarg = NULL;
259 newtag->segments = NULL;
261 /* Take into account any restrictions imposed by our parent tag */
262 if (parent != NULL) {
263 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
264 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
265 if (newtag->boundary == 0)
266 newtag->boundary = parent->boundary;
267 else if (parent->boundary != 0)
268 newtag->boundary = MIN(parent->boundary,
270 if (newtag->filter == NULL) {
272 * Short circuit looking at our parent directly
273 * since we have encapsulated all of its information
275 newtag->filter = parent->filter;
276 newtag->filterarg = parent->filterarg;
277 newtag->parent = parent->parent;
279 if (newtag->parent != NULL)
280 atomic_add_int(&parent->ref_count, 1);
283 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
284 || newtag->alignment > 1)
285 newtag->flags |= BUS_DMA_COULD_BOUNCE;
287 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
288 (flags & BUS_DMA_ALLOCNOW) != 0) {
289 struct bounce_zone *bz;
293 if ((error = alloc_bounce_zone(newtag)) != 0) {
294 free(newtag, M_DEVBUF);
297 bz = newtag->bounce_zone;
299 if (ptoa(bz->total_bpages) < maxsize) {
302 pages = atop(maxsize) - bz->total_bpages;
304 /* Add pages to our bounce pool */
305 if (alloc_bounce_pages(newtag, pages) < pages)
308 /* Performed initial allocation */
309 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
313 free(newtag, M_DEVBUF);
317 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
318 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
323 bus_dma_tag_destroy(bus_dma_tag_t dmat)
325 bus_dma_tag_t dmat_copy;
333 if (dmat->map_count != 0) {
338 while (dmat != NULL) {
339 bus_dma_tag_t parent;
341 parent = dmat->parent;
342 atomic_subtract_int(&dmat->ref_count, 1);
343 if (dmat->ref_count == 0) {
344 if (dmat->segments != NULL)
345 free(dmat->segments, M_DEVBUF);
346 free(dmat, M_DEVBUF);
348 * Last reference count, so
349 * release our reference
350 * count on our parent.
358 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
363 * Allocate a handle for mapping from kva/uva/physical
364 * address space into bus device space.
367 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
373 if (dmat->segments == NULL) {
374 dmat->segments = (bus_dma_segment_t *)malloc(
375 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
377 if (dmat->segments == NULL) {
378 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
379 __func__, dmat, ENOMEM);
385 * Bouncing might be required if the driver asks for an active
386 * exclusion region, a data alignment that is stricter than 1, and/or
387 * an active address boundary.
389 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
392 struct bounce_zone *bz;
395 if (dmat->bounce_zone == NULL) {
396 if ((error = alloc_bounce_zone(dmat)) != 0)
399 bz = dmat->bounce_zone;
401 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
404 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
405 __func__, dmat, ENOMEM);
409 /* Initialize the new map */
410 STAILQ_INIT(&((*mapp)->bpages));
413 * Attempt to add pages to our pool on a per-instance
414 * basis up to a sane limit.
416 if (dmat->alignment > 1)
417 maxpages = MAX_BPAGES;
419 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
420 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
421 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
424 pages = MAX(atop(dmat->maxsize), 1);
425 pages = MIN(maxpages - bz->total_bpages, pages);
426 pages = MAX(pages, 1);
427 if (alloc_bounce_pages(dmat, pages) < pages)
430 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
432 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
443 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
444 __func__, dmat, dmat->flags, error);
449 * Destroy a handle for mapping from kva/uva/physical
450 * address space into bus device space.
453 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
455 if (map != NULL && map != &nobounce_dmamap) {
456 if (STAILQ_FIRST(&map->bpages) != NULL) {
457 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
458 __func__, dmat, EBUSY);
461 if (dmat->bounce_zone)
462 dmat->bounce_zone->map_count--;
466 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
472 * Allocate a piece of memory that can be efficiently mapped into
473 * bus device space based on the constraints lited in the dma tag.
474 * A dmamap to for use with dmamap_load is also allocated.
477 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
482 if (flags & BUS_DMA_NOWAIT)
487 /* If we succeed, no mapping/bouncing will be required */
490 if (dmat->segments == NULL) {
491 dmat->segments = (bus_dma_segment_t *)malloc(
492 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
494 if (dmat->segments == NULL) {
495 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
496 __func__, dmat, dmat->flags, ENOMEM);
500 if (flags & BUS_DMA_ZERO)
505 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
506 * alignment guarantees of malloc need to be nailed down, and the
507 * code below should be rewritten to take that into account.
509 * In the meantime, we'll warn the user if malloc gets it wrong.
511 if ((dmat->maxsize <= PAGE_SIZE) &&
512 (dmat->alignment < dmat->maxsize) &&
513 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
514 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
517 * XXX Use Contigmalloc until it is merged into this facility
518 * and handles multi-seg allocations. Nobody is doing
519 * multi-seg allocations yet though.
520 * XXX Certain AGP hardware does.
522 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
523 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
526 if (*vaddr == NULL) {
527 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
528 __func__, dmat, dmat->flags, ENOMEM);
530 } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
531 printf("bus_dmamem_alloc failed to align memory properly.\n");
533 if (flags & BUS_DMA_NOCACHE)
534 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
536 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
537 __func__, dmat, dmat->flags, 0);
542 * Free a piece of memory and it's allociated dmamap, that was allocated
543 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
546 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
549 * dmamem does not need to be bounced, so the map should be
553 panic("bus_dmamem_free: Invalid map freed\n");
554 pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
555 if ((dmat->maxsize <= PAGE_SIZE) &&
556 (dmat->alignment < dmat->maxsize) &&
557 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
558 free(vaddr, M_DEVBUF);
560 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
562 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
566 * Utility function to load a linear buffer. lastaddrp holds state
567 * between invocations (for multiple-buffer loads). segp contains
568 * the starting segment on entrace, and the ending segment on exit.
569 * first indicates if this is the first invocation of this function.
572 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
574 void *buf, bus_size_t buflen,
577 bus_addr_t *lastaddrp,
578 bus_dma_segment_t *segs,
583 bus_addr_t curaddr, lastaddr, baddr, bmask;
589 map = &nobounce_dmamap;
591 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
592 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
593 vm_offset_t vendaddr;
595 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
596 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
597 dmat->boundary, dmat->alignment);
598 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
599 map, &nobounce_dmamap, map->pagesneeded);
601 * Count the number of bounce pages
602 * needed in order to complete this transfer
604 vaddr = (vm_offset_t)buf;
605 vendaddr = (vm_offset_t)buf + buflen;
607 while (vaddr < vendaddr) {
609 paddr = pmap_extract(pmap, vaddr);
611 paddr = pmap_kextract(vaddr);
612 if (run_filter(dmat, paddr) != 0)
614 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
616 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
619 /* Reserve Necessary Bounce Pages */
620 if (map->pagesneeded != 0) {
621 mtx_lock(&bounce_lock);
622 if (flags & BUS_DMA_NOWAIT) {
623 if (reserve_bounce_pages(dmat, map, 0) != 0) {
624 mtx_unlock(&bounce_lock);
628 if (reserve_bounce_pages(dmat, map, 1) != 0) {
629 /* Queue us for resources */
632 map->buflen = buflen;
633 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
635 mtx_unlock(&bounce_lock);
636 return (EINPROGRESS);
639 mtx_unlock(&bounce_lock);
642 vaddr = (vm_offset_t)buf;
643 lastaddr = *lastaddrp;
644 bmask = ~(dmat->boundary - 1);
646 for (seg = *segp; buflen > 0 ; ) {
648 * Get the physical address for this segment.
651 curaddr = pmap_extract(pmap, vaddr);
653 curaddr = pmap_kextract(vaddr);
656 * Compute the segment size, and adjust counts.
658 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
659 if (sgsize > dmat->maxsegsz)
660 sgsize = dmat->maxsegsz;
665 * Make sure we don't cross any boundaries.
667 if (dmat->boundary > 0) {
668 baddr = (curaddr + dmat->boundary) & bmask;
669 if (sgsize > (baddr - curaddr))
670 sgsize = (baddr - curaddr);
673 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
674 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
677 * Insert chunk into a segment, coalescing with
678 * previous segment if possible.
681 segs[seg].ds_addr = curaddr;
682 segs[seg].ds_len = sgsize;
685 if (curaddr == lastaddr &&
686 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
687 (dmat->boundary == 0 ||
688 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
689 segs[seg].ds_len += sgsize;
691 if (++seg >= dmat->nsegments)
693 segs[seg].ds_addr = curaddr;
694 segs[seg].ds_len = sgsize;
698 lastaddr = curaddr + sgsize;
704 *lastaddrp = lastaddr;
709 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
713 * Map the buffer buf into bus space using the dmamap map.
716 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
717 bus_size_t buflen, bus_dmamap_callback_t *callback,
718 void *callback_arg, int flags)
720 bus_addr_t lastaddr = 0;
721 int error, nsegs = 0;
724 flags |= BUS_DMA_WAITOK;
725 map->callback = callback;
726 map->callback_arg = callback_arg;
729 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
730 &lastaddr, dmat->segments, &nsegs, 1);
732 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
733 __func__, dmat, dmat->flags, error, nsegs + 1);
735 if (error == EINPROGRESS) {
740 (*callback)(callback_arg, dmat->segments, 0, error);
742 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
745 * Return ENOMEM to the caller so that it can pass it up the stack.
746 * This error only happens when NOWAIT is set, so deferal is disabled.
756 * Like _bus_dmamap_load(), but for mbufs.
759 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
761 bus_dmamap_callback2_t *callback, void *callback_arg,
768 flags |= BUS_DMA_NOWAIT;
771 if (m0->m_pkthdr.len <= dmat->maxsize) {
773 bus_addr_t lastaddr = 0;
776 for (m = m0; m != NULL && error == 0; m = m->m_next) {
778 error = _bus_dmamap_load_buffer(dmat, map,
780 NULL, flags, &lastaddr,
781 dmat->segments, &nsegs, first);
790 /* force "no valid mappings" in callback */
791 (*callback)(callback_arg, dmat->segments, 0, 0, error);
793 (*callback)(callback_arg, dmat->segments,
794 nsegs+1, m0->m_pkthdr.len, error);
796 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
797 __func__, dmat, dmat->flags, error, nsegs + 1);
802 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
803 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
810 flags |= BUS_DMA_NOWAIT;
813 if (m0->m_pkthdr.len <= dmat->maxsize) {
815 bus_addr_t lastaddr = 0;
818 for (m = m0; m != NULL && error == 0; m = m->m_next) {
820 error = _bus_dmamap_load_buffer(dmat, map,
822 NULL, flags, &lastaddr,
831 /* XXX FIXME: Having to increment nsegs is really annoying */
833 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
834 __func__, dmat, dmat->flags, error, *nsegs);
839 * Like _bus_dmamap_load(), but for uios.
842 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
844 bus_dmamap_callback2_t *callback, void *callback_arg,
847 bus_addr_t lastaddr = 0;
848 int nsegs, error, first, i;
853 flags |= BUS_DMA_NOWAIT;
854 resid = uio->uio_resid;
857 if (uio->uio_segflg == UIO_USERSPACE) {
858 KASSERT(uio->uio_td != NULL,
859 ("bus_dmamap_load_uio: USERSPACE but no proc"));
860 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
867 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
869 * Now at the first iovec to load. Load each iovec
870 * until we have exhausted the residual count.
873 resid < iov[i].iov_len ? resid : iov[i].iov_len;
874 caddr_t addr = (caddr_t) iov[i].iov_base;
877 error = _bus_dmamap_load_buffer(dmat, map,
878 addr, minlen, pmap, flags, &lastaddr,
879 dmat->segments, &nsegs, first);
887 /* force "no valid mappings" in callback */
888 (*callback)(callback_arg, dmat->segments, 0, 0, error);
890 (*callback)(callback_arg, dmat->segments,
891 nsegs+1, uio->uio_resid, error);
893 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
894 __func__, dmat, dmat->flags, error, nsegs + 1);
899 * Release the mapping held by map.
902 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
904 struct bounce_page *bpage;
906 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
907 STAILQ_REMOVE_HEAD(&map->bpages, links);
908 free_bounce_page(dmat, bpage);
913 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
915 struct bounce_page *bpage;
917 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
919 * Handle data bouncing. We might also
920 * want to add support for invalidating
921 * the caches on broken hardware
923 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
924 "performing bounce", __func__, op, dmat, dmat->flags);
926 if (op & BUS_DMASYNC_PREWRITE) {
927 while (bpage != NULL) {
928 bcopy((void *)bpage->datavaddr,
929 (void *)bpage->vaddr,
931 bpage = STAILQ_NEXT(bpage, links);
933 dmat->bounce_zone->total_bounced++;
936 if (op & BUS_DMASYNC_POSTREAD) {
937 while (bpage != NULL) {
938 bcopy((void *)bpage->vaddr,
939 (void *)bpage->datavaddr,
941 bpage = STAILQ_NEXT(bpage, links);
943 dmat->bounce_zone->total_bounced++;
949 init_bounce_pages(void *dummy __unused)
953 STAILQ_INIT(&bounce_zone_list);
954 STAILQ_INIT(&bounce_map_waitinglist);
955 STAILQ_INIT(&bounce_map_callbacklist);
956 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
958 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
960 static struct sysctl_ctx_list *
961 busdma_sysctl_tree(struct bounce_zone *bz)
963 return (&bz->sysctl_tree);
966 static struct sysctl_oid *
967 busdma_sysctl_tree_top(struct bounce_zone *bz)
969 return (bz->sysctl_tree_top);
973 alloc_bounce_zone(bus_dma_tag_t dmat)
975 struct bounce_zone *bz;
977 /* Check to see if we already have a suitable zone */
978 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
979 if ((dmat->alignment <= bz->alignment)
980 && (dmat->lowaddr >= bz->lowaddr)) {
981 dmat->bounce_zone = bz;
986 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
987 M_NOWAIT | M_ZERO)) == NULL)
990 STAILQ_INIT(&bz->bounce_page_list);
992 bz->reserved_bpages = 0;
993 bz->active_bpages = 0;
994 bz->lowaddr = dmat->lowaddr;
995 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
997 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
999 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1000 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1001 dmat->bounce_zone = bz;
1003 sysctl_ctx_init(&bz->sysctl_tree);
1004 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1005 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1007 if (bz->sysctl_tree_top == NULL) {
1008 sysctl_ctx_free(&bz->sysctl_tree);
1009 return (0); /* XXX error code? */
1012 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1013 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1014 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1015 "Total bounce pages");
1016 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1017 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1018 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1019 "Free bounce pages");
1020 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1021 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1022 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1023 "Reserved bounce pages");
1024 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1025 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1026 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1027 "Active bounce pages");
1028 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1029 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1030 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1031 "Total bounce requests");
1032 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1033 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1034 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1035 "Total bounce requests that were deferred");
1036 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1037 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1038 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1039 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1040 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1041 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1047 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1049 struct bounce_zone *bz;
1052 bz = dmat->bounce_zone;
1054 while (numpages > 0) {
1055 struct bounce_page *bpage;
1057 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1062 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1067 if (bpage->vaddr == 0) {
1068 free(bpage, M_DEVBUF);
1071 bpage->busaddr = pmap_kextract(bpage->vaddr);
1072 mtx_lock(&bounce_lock);
1073 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1077 mtx_unlock(&bounce_lock);
1085 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1087 struct bounce_zone *bz;
1090 mtx_assert(&bounce_lock, MA_OWNED);
1091 bz = dmat->bounce_zone;
1092 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1093 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1094 return (map->pagesneeded - (map->pagesreserved + pages));
1095 bz->free_bpages -= pages;
1096 bz->reserved_bpages += pages;
1097 map->pagesreserved += pages;
1098 pages = map->pagesneeded - map->pagesreserved;
1104 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1107 struct bounce_zone *bz;
1108 struct bounce_page *bpage;
1110 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1111 KASSERT(map != NULL && map != &nobounce_dmamap,
1112 ("add_bounce_page: bad map %p", map));
1114 bz = dmat->bounce_zone;
1115 if (map->pagesneeded == 0)
1116 panic("add_bounce_page: map doesn't need any pages");
1119 if (map->pagesreserved == 0)
1120 panic("add_bounce_page: map doesn't need any pages");
1121 map->pagesreserved--;
1123 mtx_lock(&bounce_lock);
1124 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1126 panic("add_bounce_page: free page list is empty");
1128 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1129 bz->reserved_bpages--;
1130 bz->active_bpages++;
1131 mtx_unlock(&bounce_lock);
1133 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1134 /* Page offset needs to be preserved. */
1135 bpage->vaddr |= vaddr & PAGE_MASK;
1136 bpage->busaddr |= vaddr & PAGE_MASK;
1138 bpage->datavaddr = vaddr;
1139 bpage->datacount = size;
1140 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1141 return (bpage->busaddr);
1145 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1147 struct bus_dmamap *map;
1148 struct bounce_zone *bz;
1150 bz = dmat->bounce_zone;
1151 bpage->datavaddr = 0;
1152 bpage->datacount = 0;
1153 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1155 * Reset the bounce page to start at offset 0. Other uses
1156 * of this bounce page may need to store a full page of
1157 * data and/or assume it starts on a page boundary.
1159 bpage->vaddr &= ~PAGE_MASK;
1160 bpage->busaddr &= ~PAGE_MASK;
1163 mtx_lock(&bounce_lock);
1164 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1166 bz->active_bpages--;
1167 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1168 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1169 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1170 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1172 busdma_swi_pending = 1;
1173 bz->total_deferred++;
1174 swi_sched(vm_ih, 0);
1177 mtx_unlock(&bounce_lock);
1184 struct bus_dmamap *map;
1186 mtx_lock(&bounce_lock);
1187 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1188 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1189 mtx_unlock(&bounce_lock);
1191 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1192 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1193 map->callback, map->callback_arg, /*flags*/0);
1194 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1195 mtx_lock(&bounce_lock);
1197 mtx_unlock(&bounce_lock);