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>
33 #include <ddb/db_output.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
42 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
51 #include <machine/atomic.h>
52 #include <machine/bus.h>
53 #include <machine/md_var.h>
54 #include <machine/specialreg.h>
56 #define MAX_BPAGES 512
57 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
58 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
68 bus_dma_filter_t *filter;
76 bus_dma_lock_t *lockfunc;
78 bus_dma_segment_t *segments;
79 struct bounce_zone *bounce_zone;
83 vm_offset_t vaddr; /* kva of bounce buffer */
84 bus_addr_t busaddr; /* Physical address */
85 vm_offset_t datavaddr; /* kva of client data */
86 bus_size_t datacount; /* client data count */
87 STAILQ_ENTRY(bounce_page) links;
90 int busdma_swi_pending;
93 STAILQ_ENTRY(bounce_zone) links;
94 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
102 bus_size_t alignment;
106 struct sysctl_ctx_list sysctl_tree;
107 struct sysctl_oid *sysctl_tree_top;
110 static struct mtx bounce_lock;
111 static int total_bpages;
112 static int busdma_zonecount;
113 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
115 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
116 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
117 "Total bounce pages");
120 struct bp_list bpages;
124 void *buf; /* unmapped buffer pointer */
125 bus_size_t buflen; /* unmapped buffer length */
126 bus_dmamap_callback_t *callback;
128 STAILQ_ENTRY(bus_dmamap) links;
131 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
132 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
133 static struct bus_dmamap nobounce_dmamap;
135 static void init_bounce_pages(void *dummy);
136 static int alloc_bounce_zone(bus_dma_tag_t dmat);
137 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
138 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
140 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
141 vm_offset_t vaddr, bus_size_t size);
142 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
143 int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
144 int _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
145 void *buf, bus_size_t buflen, int flags);
149 #define pmap_kextract pmap_kextract_ma
153 * Return true if a match is made.
155 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
157 * If paddr is within the bounds of the dma tag then call the filter callback
158 * to check for a match, if there is no filter callback then assume a match.
161 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
168 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
169 || ((paddr & (dmat->alignment - 1)) != 0))
170 && (dmat->filter == NULL
171 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
175 } while (retval == 0 && dmat != NULL);
180 * Convenience function for manipulating driver locks from busdma (during
181 * busdma_swi, for example). Drivers that don't provide their own locks
182 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
183 * non-mutex locking scheme don't have to use this at all.
186 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
190 dmtx = (struct mtx *)arg;
199 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
204 * dflt_lock should never get called. It gets put into the dma tag when
205 * lockfunc == NULL, which is only valid if the maps that are associated
206 * with the tag are meant to never be defered.
207 * XXX Should have a way to identify which driver is responsible here.
210 dflt_lock(void *arg, bus_dma_lock_op_t op)
212 panic("driver error: busdma dflt_lock called");
216 * Allocate a device specific dma_tag.
219 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
220 bus_size_t boundary, bus_addr_t lowaddr,
221 bus_addr_t highaddr, bus_dma_filter_t *filter,
222 void *filterarg, bus_size_t maxsize, int nsegments,
223 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
224 void *lockfuncarg, bus_dma_tag_t *dmat)
226 bus_dma_tag_t newtag;
229 /* Basic sanity checking */
230 if (boundary != 0 && boundary < maxsegsz)
237 /* Return a NULL tag on failure */
240 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
242 if (newtag == NULL) {
243 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
244 __func__, newtag, 0, error);
248 newtag->parent = parent;
249 newtag->alignment = alignment;
250 newtag->boundary = boundary;
251 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
252 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
254 newtag->filter = filter;
255 newtag->filterarg = filterarg;
256 newtag->maxsize = maxsize;
257 newtag->nsegments = nsegments;
258 newtag->maxsegsz = maxsegsz;
259 newtag->flags = flags;
260 newtag->ref_count = 1; /* Count ourself */
261 newtag->map_count = 0;
262 if (lockfunc != NULL) {
263 newtag->lockfunc = lockfunc;
264 newtag->lockfuncarg = lockfuncarg;
266 newtag->lockfunc = dflt_lock;
267 newtag->lockfuncarg = NULL;
269 newtag->segments = NULL;
271 /* Take into account any restrictions imposed by our parent tag */
272 if (parent != NULL) {
273 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
274 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
275 if (newtag->boundary == 0)
276 newtag->boundary = parent->boundary;
277 else if (parent->boundary != 0)
278 newtag->boundary = MIN(parent->boundary,
280 if ((newtag->filter != NULL) ||
281 ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
282 newtag->flags |= BUS_DMA_COULD_BOUNCE;
283 if (newtag->filter == NULL) {
285 * Short circuit looking at our parent directly
286 * since we have encapsulated all of its information
288 newtag->filter = parent->filter;
289 newtag->filterarg = parent->filterarg;
290 newtag->parent = parent->parent;
292 if (newtag->parent != NULL)
293 atomic_add_int(&parent->ref_count, 1);
296 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
297 || newtag->alignment > 1)
298 newtag->flags |= BUS_DMA_COULD_BOUNCE;
300 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
301 (flags & BUS_DMA_ALLOCNOW) != 0) {
302 struct bounce_zone *bz;
306 if ((error = alloc_bounce_zone(newtag)) != 0) {
307 free(newtag, M_DEVBUF);
310 bz = newtag->bounce_zone;
312 if (ptoa(bz->total_bpages) < maxsize) {
315 pages = atop(maxsize) - bz->total_bpages;
317 /* Add pages to our bounce pool */
318 if (alloc_bounce_pages(newtag, pages) < pages)
321 /* Performed initial allocation */
322 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
326 free(newtag, M_DEVBUF);
330 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
331 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
336 bus_dma_tag_destroy(bus_dma_tag_t dmat)
338 bus_dma_tag_t dmat_copy;
346 if (dmat->map_count != 0) {
351 while (dmat != NULL) {
352 bus_dma_tag_t parent;
354 parent = dmat->parent;
355 atomic_subtract_int(&dmat->ref_count, 1);
356 if (dmat->ref_count == 0) {
357 if (dmat->segments != NULL)
358 free(dmat->segments, M_DEVBUF);
359 free(dmat, M_DEVBUF);
361 * Last reference count, so
362 * release our reference
363 * count on our parent.
371 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
376 * Allocate a handle for mapping from kva/uva/physical
377 * address space into bus device space.
380 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
386 if (dmat->segments == NULL) {
387 dmat->segments = (bus_dma_segment_t *)malloc(
388 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
390 if (dmat->segments == NULL) {
391 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
392 __func__, dmat, ENOMEM);
398 * Bouncing might be required if the driver asks for an active
399 * exclusion region, a data alignment that is stricter than 1, and/or
400 * an active address boundary.
402 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
405 struct bounce_zone *bz;
408 if (dmat->bounce_zone == NULL) {
409 if ((error = alloc_bounce_zone(dmat)) != 0)
412 bz = dmat->bounce_zone;
414 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
417 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
418 __func__, dmat, ENOMEM);
422 /* Initialize the new map */
423 STAILQ_INIT(&((*mapp)->bpages));
426 * Attempt to add pages to our pool on a per-instance
427 * basis up to a sane limit.
429 if (dmat->alignment > 1)
430 maxpages = MAX_BPAGES;
432 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
433 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
434 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
437 pages = MAX(atop(dmat->maxsize), 1);
438 pages = MIN(maxpages - bz->total_bpages, pages);
439 pages = MAX(pages, 1);
440 if (alloc_bounce_pages(dmat, pages) < pages)
443 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
445 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
456 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
457 __func__, dmat, dmat->flags, error);
462 * Destroy a handle for mapping from kva/uva/physical
463 * address space into bus device space.
466 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
468 if (map != NULL && map != &nobounce_dmamap) {
469 if (STAILQ_FIRST(&map->bpages) != NULL) {
470 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
471 __func__, dmat, EBUSY);
474 if (dmat->bounce_zone)
475 dmat->bounce_zone->map_count--;
479 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
485 * Allocate a piece of memory that can be efficiently mapped into
486 * bus device space based on the constraints lited in the dma tag.
487 * A dmamap to for use with dmamap_load is also allocated.
490 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
495 if (flags & BUS_DMA_NOWAIT)
500 /* If we succeed, no mapping/bouncing will be required */
503 if (dmat->segments == NULL) {
504 dmat->segments = (bus_dma_segment_t *)malloc(
505 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
507 if (dmat->segments == NULL) {
508 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
509 __func__, dmat, dmat->flags, ENOMEM);
513 if (flags & BUS_DMA_ZERO)
518 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
519 * alignment guarantees of malloc need to be nailed down, and the
520 * code below should be rewritten to take that into account.
522 * In the meantime, we'll warn the user if malloc gets it wrong.
524 if ((dmat->maxsize <= PAGE_SIZE) &&
525 (dmat->alignment < dmat->maxsize) &&
526 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
527 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
530 * XXX Use Contigmalloc until it is merged into this facility
531 * and handles multi-seg allocations. Nobody is doing
532 * multi-seg allocations yet though.
533 * XXX Certain AGP hardware does.
535 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
536 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
539 if (*vaddr == NULL) {
540 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
541 __func__, dmat, dmat->flags, ENOMEM);
543 } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
544 printf("bus_dmamem_alloc failed to align memory properly.\n");
546 if (flags & BUS_DMA_NOCACHE)
547 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
549 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
550 __func__, dmat, dmat->flags, 0);
555 * Free a piece of memory and it's allociated dmamap, that was allocated
556 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
559 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
562 * dmamem does not need to be bounced, so the map should be
566 panic("bus_dmamem_free: Invalid map freed\n");
567 pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
568 if ((dmat->maxsize <= PAGE_SIZE) &&
569 (dmat->alignment < dmat->maxsize) &&
570 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
571 free(vaddr, M_DEVBUF);
573 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
575 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
579 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
580 void *buf, bus_size_t buflen, int flags)
583 vm_offset_t vendaddr;
586 if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
587 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
588 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
589 dmat->boundary, dmat->alignment);
590 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
591 map, &nobounce_dmamap, map->pagesneeded);
593 * Count the number of bounce pages
594 * needed in order to complete this transfer
596 vaddr = (vm_offset_t)buf;
597 vendaddr = (vm_offset_t)buf + buflen;
599 while (vaddr < vendaddr) {
601 paddr = pmap_extract(pmap, vaddr);
603 paddr = pmap_kextract(vaddr);
604 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
605 run_filter(dmat, paddr) != 0) {
608 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
610 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
613 /* Reserve Necessary Bounce Pages */
614 if (map->pagesneeded != 0) {
615 mtx_lock(&bounce_lock);
616 if (flags & BUS_DMA_NOWAIT) {
617 if (reserve_bounce_pages(dmat, map, 0) != 0) {
618 mtx_unlock(&bounce_lock);
622 if (reserve_bounce_pages(dmat, map, 1) != 0) {
623 /* Queue us for resources */
626 map->buflen = buflen;
627 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
629 mtx_unlock(&bounce_lock);
630 return (EINPROGRESS);
633 mtx_unlock(&bounce_lock);
640 * Utility function to load a linear buffer. lastaddrp holds state
641 * between invocations (for multiple-buffer loads). segp contains
642 * the starting segment on entrace, and the ending segment on exit.
643 * first indicates if this is the first invocation of this function.
646 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
648 void *buf, bus_size_t buflen,
651 bus_addr_t *lastaddrp,
652 bus_dma_segment_t *segs,
657 bus_addr_t curaddr, lastaddr, baddr, bmask;
662 map = &nobounce_dmamap;
664 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
665 error = _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
670 vaddr = (vm_offset_t)buf;
671 lastaddr = *lastaddrp;
672 bmask = ~(dmat->boundary - 1);
674 for (seg = *segp; buflen > 0 ; ) {
676 * Get the physical address for this segment.
679 curaddr = pmap_extract(pmap, vaddr);
681 curaddr = pmap_kextract(vaddr);
684 * Compute the segment size, and adjust counts.
686 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
687 if (sgsize > dmat->maxsegsz)
688 sgsize = dmat->maxsegsz;
693 * Make sure we don't cross any boundaries.
695 if (dmat->boundary > 0) {
696 baddr = (curaddr + dmat->boundary) & bmask;
697 if (sgsize > (baddr - curaddr))
698 sgsize = (baddr - curaddr);
701 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
702 map->pagesneeded != 0 && run_filter(dmat, curaddr))
703 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
706 * Insert chunk into a segment, coalescing with
707 * previous segment if possible.
710 segs[seg].ds_addr = curaddr;
711 segs[seg].ds_len = sgsize;
714 if (curaddr == lastaddr &&
715 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
716 (dmat->boundary == 0 ||
717 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
718 segs[seg].ds_len += sgsize;
720 if (++seg >= dmat->nsegments)
722 segs[seg].ds_addr = curaddr;
723 segs[seg].ds_len = sgsize;
727 lastaddr = curaddr + sgsize;
733 *lastaddrp = lastaddr;
738 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
742 * Map the buffer buf into bus space using the dmamap map.
745 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
746 bus_size_t buflen, bus_dmamap_callback_t *callback,
747 void *callback_arg, int flags)
749 bus_addr_t lastaddr = 0;
750 int error, nsegs = 0;
753 flags |= BUS_DMA_WAITOK;
754 map->callback = callback;
755 map->callback_arg = callback_arg;
758 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
759 &lastaddr, dmat->segments, &nsegs, 1);
761 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
762 __func__, dmat, dmat->flags, error, nsegs + 1);
764 if (error == EINPROGRESS) {
769 (*callback)(callback_arg, dmat->segments, 0, error);
771 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
774 * Return ENOMEM to the caller so that it can pass it up the stack.
775 * This error only happens when NOWAIT is set, so deferal is disabled.
785 * Like _bus_dmamap_load(), but for mbufs.
788 _bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
789 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
796 flags |= BUS_DMA_NOWAIT;
799 if (m0->m_pkthdr.len <= dmat->maxsize) {
801 bus_addr_t lastaddr = 0;
804 for (m = m0; m != NULL && error == 0; m = m->m_next) {
806 error = _bus_dmamap_load_buffer(dmat, map,
808 NULL, flags, &lastaddr,
817 /* XXX FIXME: Having to increment nsegs is really annoying */
819 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
820 __func__, dmat, dmat->flags, error, *nsegs);
825 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
827 bus_dmamap_callback2_t *callback, void *callback_arg,
832 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, dmat->segments, &nsegs,
836 /* force "no valid mappings" in callback */
837 (*callback)(callback_arg, dmat->segments, 0, 0, error);
839 (*callback)(callback_arg, dmat->segments,
840 nsegs, m0->m_pkthdr.len, error);
842 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
843 __func__, dmat, dmat->flags, error, nsegs);
848 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
849 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
852 return (_bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags));
856 * Like _bus_dmamap_load(), but for uios.
859 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
861 bus_dmamap_callback2_t *callback, void *callback_arg,
865 int nsegs, error, first, i;
870 flags |= BUS_DMA_NOWAIT;
871 resid = uio->uio_resid;
874 if (uio->uio_segflg == UIO_USERSPACE) {
875 KASSERT(uio->uio_td != NULL,
876 ("bus_dmamap_load_uio: USERSPACE but no proc"));
877 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
884 lastaddr = (bus_addr_t) 0;
885 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
887 * Now at the first iovec to load. Load each iovec
888 * until we have exhausted the residual count.
891 resid < iov[i].iov_len ? resid : iov[i].iov_len;
892 caddr_t addr = (caddr_t) iov[i].iov_base;
895 error = _bus_dmamap_load_buffer(dmat, map,
896 addr, minlen, pmap, flags, &lastaddr,
897 dmat->segments, &nsegs, first);
905 /* force "no valid mappings" in callback */
906 (*callback)(callback_arg, dmat->segments, 0, 0, error);
908 (*callback)(callback_arg, dmat->segments,
909 nsegs+1, uio->uio_resid, error);
911 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
912 __func__, dmat, dmat->flags, error, nsegs + 1);
917 * Release the mapping held by map.
920 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
922 struct bounce_page *bpage;
924 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
925 STAILQ_REMOVE_HEAD(&map->bpages, links);
926 free_bounce_page(dmat, bpage);
931 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
933 struct bounce_page *bpage;
935 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
937 * Handle data bouncing. We might also
938 * want to add support for invalidating
939 * the caches on broken hardware
941 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
942 "performing bounce", __func__, op, dmat, dmat->flags);
944 if (op & BUS_DMASYNC_PREWRITE) {
945 while (bpage != NULL) {
946 bcopy((void *)bpage->datavaddr,
947 (void *)bpage->vaddr,
949 bpage = STAILQ_NEXT(bpage, links);
951 dmat->bounce_zone->total_bounced++;
954 if (op & BUS_DMASYNC_POSTREAD) {
955 while (bpage != NULL) {
956 bcopy((void *)bpage->vaddr,
957 (void *)bpage->datavaddr,
959 bpage = STAILQ_NEXT(bpage, links);
961 dmat->bounce_zone->total_bounced++;
967 init_bounce_pages(void *dummy __unused)
971 STAILQ_INIT(&bounce_zone_list);
972 STAILQ_INIT(&bounce_map_waitinglist);
973 STAILQ_INIT(&bounce_map_callbacklist);
974 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
976 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
978 static struct sysctl_ctx_list *
979 busdma_sysctl_tree(struct bounce_zone *bz)
981 return (&bz->sysctl_tree);
984 static struct sysctl_oid *
985 busdma_sysctl_tree_top(struct bounce_zone *bz)
987 return (bz->sysctl_tree_top);
991 alloc_bounce_zone(bus_dma_tag_t dmat)
993 struct bounce_zone *bz;
995 /* Check to see if we already have a suitable zone */
996 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
997 if ((dmat->alignment <= bz->alignment)
998 && (dmat->lowaddr >= bz->lowaddr)) {
999 dmat->bounce_zone = bz;
1004 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1005 M_NOWAIT | M_ZERO)) == NULL)
1008 STAILQ_INIT(&bz->bounce_page_list);
1009 bz->free_bpages = 0;
1010 bz->reserved_bpages = 0;
1011 bz->active_bpages = 0;
1012 bz->lowaddr = dmat->lowaddr;
1013 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1015 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1017 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1018 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1019 dmat->bounce_zone = bz;
1021 sysctl_ctx_init(&bz->sysctl_tree);
1022 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1023 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1025 if (bz->sysctl_tree_top == NULL) {
1026 sysctl_ctx_free(&bz->sysctl_tree);
1027 return (0); /* XXX error code? */
1030 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1031 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1032 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1033 "Total bounce pages");
1034 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1035 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1036 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1037 "Free bounce pages");
1038 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1039 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1040 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1041 "Reserved bounce pages");
1042 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1043 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1044 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1045 "Active bounce pages");
1046 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1047 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1048 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1049 "Total bounce requests");
1050 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1051 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1052 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1053 "Total bounce requests that were deferred");
1054 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1055 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1056 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1057 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1058 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1059 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1065 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1067 struct bounce_zone *bz;
1070 bz = dmat->bounce_zone;
1072 while (numpages > 0) {
1073 struct bounce_page *bpage;
1075 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1080 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1085 if (bpage->vaddr == 0) {
1086 free(bpage, M_DEVBUF);
1089 bpage->busaddr = pmap_kextract(bpage->vaddr);
1090 mtx_lock(&bounce_lock);
1091 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1095 mtx_unlock(&bounce_lock);
1103 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1105 struct bounce_zone *bz;
1108 mtx_assert(&bounce_lock, MA_OWNED);
1109 bz = dmat->bounce_zone;
1110 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1111 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1112 return (map->pagesneeded - (map->pagesreserved + pages));
1113 bz->free_bpages -= pages;
1114 bz->reserved_bpages += pages;
1115 map->pagesreserved += pages;
1116 pages = map->pagesneeded - map->pagesreserved;
1122 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1125 struct bounce_zone *bz;
1126 struct bounce_page *bpage;
1128 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1129 KASSERT(map != NULL && map != &nobounce_dmamap,
1130 ("add_bounce_page: bad map %p", map));
1132 bz = dmat->bounce_zone;
1133 if (map->pagesneeded == 0)
1134 panic("add_bounce_page: map doesn't need any pages");
1137 if (map->pagesreserved == 0)
1138 panic("add_bounce_page: map doesn't need any pages");
1139 map->pagesreserved--;
1141 mtx_lock(&bounce_lock);
1142 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1144 panic("add_bounce_page: free page list is empty");
1146 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1147 bz->reserved_bpages--;
1148 bz->active_bpages++;
1149 mtx_unlock(&bounce_lock);
1151 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1152 /* Page offset needs to be preserved. */
1153 bpage->vaddr |= vaddr & PAGE_MASK;
1154 bpage->busaddr |= vaddr & PAGE_MASK;
1156 bpage->datavaddr = vaddr;
1157 bpage->datacount = size;
1158 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1159 return (bpage->busaddr);
1163 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1165 struct bus_dmamap *map;
1166 struct bounce_zone *bz;
1168 bz = dmat->bounce_zone;
1169 bpage->datavaddr = 0;
1170 bpage->datacount = 0;
1171 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1173 * Reset the bounce page to start at offset 0. Other uses
1174 * of this bounce page may need to store a full page of
1175 * data and/or assume it starts on a page boundary.
1177 bpage->vaddr &= ~PAGE_MASK;
1178 bpage->busaddr &= ~PAGE_MASK;
1181 mtx_lock(&bounce_lock);
1182 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1184 bz->active_bpages--;
1185 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1186 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1187 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1188 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1190 busdma_swi_pending = 1;
1191 bz->total_deferred++;
1192 swi_sched(vm_ih, 0);
1195 mtx_unlock(&bounce_lock);
1202 struct bus_dmamap *map;
1204 mtx_lock(&bounce_lock);
1205 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1206 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1207 mtx_unlock(&bounce_lock);
1209 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1210 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1211 map->callback, map->callback_arg, /*flags*/0);
1212 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1213 mtx_lock(&bounce_lock);
1215 mtx_unlock(&bounce_lock);