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
26 * $FreeBSD: src/sys/i386/i386/busdma_machdep.c,v 1.94 2008/08/15 20:51:31 kmacy Exp $
27 * $DragonFly: src/sys/platform/pc32/i386/busdma_machdep.c,v 1.23 2008/06/05 18:06:32 swildner Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
35 #include <sys/thread2.h>
36 #include <sys/bus_dma.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <sys/spinlock2.h>
43 #include <vm/vm_page.h>
45 /* XXX needed for to access pmap to convert per-proc virtual to physical */
48 #include <vm/vm_map.h>
50 #include <machine/md_var.h>
52 #define MAX_BPAGES 1024
63 bus_dma_filter_t *filter;
71 bus_dma_segment_t *segments;
72 struct bounce_zone *bounce_zone;
76 * bus_dma_tag private flags
78 #define BUS_DMA_BOUNCE_ALIGN BUS_DMA_BUS2
79 #define BUS_DMA_BOUNCE_LOWADDR BUS_DMA_BUS3
80 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
82 #define BUS_DMA_COULD_BOUNCE (BUS_DMA_BOUNCE_LOWADDR | BUS_DMA_BOUNCE_ALIGN)
84 #define BUS_DMAMEM_KMALLOC(dmat) \
85 ((dmat)->maxsize <= PAGE_SIZE && \
86 (dmat)->alignment <= PAGE_SIZE && \
87 (dmat)->lowaddr >= ptoa(Maxmem))
90 vm_offset_t vaddr; /* kva of bounce buffer */
91 bus_addr_t busaddr; /* Physical address */
92 vm_offset_t datavaddr; /* kva of client data */
93 bus_size_t datacount; /* client data count */
94 STAILQ_ENTRY(bounce_page) links;
98 STAILQ_ENTRY(bounce_zone) links;
99 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
100 STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
102 struct spinlock spin;
113 bus_size_t alignment;
117 struct sysctl_ctx_list sysctl_ctx;
118 struct sysctl_oid *sysctl_tree;
122 #define BZ_LOCK(bz) spin_lock_wr(&(bz)->spin)
123 #define BZ_UNLOCK(bz) spin_unlock_wr(&(bz)->spin)
125 #define BZ_LOCK(bz) crit_enter()
126 #define BZ_UNLOCK(bz) crit_exit()
129 static struct lwkt_token bounce_zone_tok =
130 LWKT_TOKEN_INITIALIZER(bounce_zone_tok);
131 static int busdma_zonecount;
132 static STAILQ_HEAD(, bounce_zone) bounce_zone_list =
133 STAILQ_HEAD_INITIALIZER(bounce_zone_list);
135 int busdma_swi_pending;
136 static int total_bounce_pages;
137 static int max_bounce_pages = MAX_BPAGES;
138 static int bounce_alignment = 1; /* XXX temporary */
140 TUNABLE_INT("hw.busdma.max_bpages", &max_bounce_pages);
141 TUNABLE_INT("hw.busdma.bounce_alignment", &bounce_alignment);
144 struct bp_list bpages;
148 void *buf; /* unmapped buffer pointer */
149 bus_size_t buflen; /* unmapped buffer length */
150 bus_dmamap_callback_t *callback;
152 STAILQ_ENTRY(bus_dmamap) links;
155 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist =
156 STAILQ_HEAD_INITIALIZER(bounce_map_callbacklist);
158 static struct bus_dmamap nobounce_dmamap;
160 static int alloc_bounce_zone(bus_dma_tag_t);
161 static int alloc_bounce_pages(bus_dma_tag_t, u_int, int);
162 static int reserve_bounce_pages(bus_dma_tag_t, bus_dmamap_t, int);
163 static void return_bounce_pages(bus_dma_tag_t, bus_dmamap_t);
164 static bus_addr_t add_bounce_page(bus_dma_tag_t, bus_dmamap_t,
165 vm_offset_t, bus_size_t);
166 static void free_bounce_page(bus_dma_tag_t, struct bounce_page *);
168 static bus_dmamap_t get_map_waiting(bus_dma_tag_t);
169 static void add_map_callback(bus_dmamap_t);
171 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
172 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bounce_pages,
173 0, "Total bounce pages");
174 SYSCTL_INT(_hw_busdma, OID_AUTO, max_bpages, CTLFLAG_RD, &max_bounce_pages,
175 0, "Max bounce pages per bounce zone");
176 SYSCTL_INT(_hw_busdma, OID_AUTO, bounce_alignment, CTLFLAG_RD,
177 &bounce_alignment, 0, "Obey alignment constraint");
180 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
186 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr) ||
187 (bounce_alignment && (paddr & (dmat->alignment - 1)) != 0))
188 && (dmat->filter == NULL ||
189 dmat->filter(dmat->filterarg, paddr) != 0))
193 } while (retval == 0 && dmat != NULL);
198 * Allocate a device specific dma_tag.
201 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
202 bus_size_t boundary, bus_addr_t lowaddr,
203 bus_addr_t highaddr, bus_dma_filter_t *filter,
204 void *filterarg, bus_size_t maxsize, int nsegments,
205 bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
207 bus_dma_tag_t newtag;
216 if (alignment & (alignment - 1))
217 panic("alignment must be power of 2\n");
220 if (boundary & (boundary - 1))
221 panic("boundary must be power of 2\n");
222 if (boundary < maxsegsz) {
223 kprintf("boundary < maxsegsz:\n");
229 /* Return a NULL tag on failure */
232 newtag = kmalloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT);
234 newtag->parent = parent;
235 newtag->alignment = alignment;
236 newtag->boundary = boundary;
237 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
238 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
239 newtag->filter = filter;
240 newtag->filterarg = filterarg;
241 newtag->maxsize = maxsize;
242 newtag->nsegments = nsegments;
243 newtag->maxsegsz = maxsegsz;
244 newtag->flags = flags;
245 newtag->ref_count = 1; /* Count ourself */
246 newtag->map_count = 0;
247 newtag->segments = NULL;
248 newtag->bounce_zone = NULL;
250 /* Take into account any restrictions imposed by our parent tag */
251 if (parent != NULL) {
252 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
253 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
255 if (newtag->boundary == 0) {
256 newtag->boundary = parent->boundary;
257 } else if (parent->boundary != 0) {
258 newtag->boundary = MIN(parent->boundary,
263 newtag->alignment = MAX(parent->alignment, newtag->alignment);
266 if (newtag->filter == NULL) {
268 * Short circuit looking at our parent directly
269 * since we have encapsulated all of its information
271 newtag->filter = parent->filter;
272 newtag->filterarg = parent->filterarg;
273 newtag->parent = parent->parent;
275 if (newtag->parent != NULL)
279 if (newtag->lowaddr < ptoa(Maxmem))
280 newtag->flags |= BUS_DMA_BOUNCE_LOWADDR;
281 if (bounce_alignment && newtag->alignment > 1 &&
282 !(newtag->flags & BUS_DMA_ALIGNED))
283 newtag->flags |= BUS_DMA_BOUNCE_ALIGN;
285 if ((newtag->flags & BUS_DMA_COULD_BOUNCE) &&
286 (flags & BUS_DMA_ALLOCNOW) != 0) {
287 struct bounce_zone *bz;
291 error = alloc_bounce_zone(newtag);
294 bz = newtag->bounce_zone;
296 if (ptoa(bz->total_bpages) < maxsize) {
299 if (flags & BUS_DMA_ONEBPAGE) {
302 pages = atop(round_page(maxsize)) -
304 pages = MAX(pages, 1);
307 /* Add pages to our bounce pool */
308 if (alloc_bounce_pages(newtag, pages, flags) < pages)
311 /* Performed initial allocation */
312 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
317 kfree(newtag, M_DEVBUF);
324 bus_dma_tag_destroy(bus_dma_tag_t dmat)
327 if (dmat->map_count != 0)
330 while (dmat != NULL) {
331 bus_dma_tag_t parent;
333 parent = dmat->parent;
335 if (dmat->ref_count == 0) {
336 if (dmat->segments != NULL)
337 kfree(dmat->segments, M_DEVBUF);
338 kfree(dmat, M_DEVBUF);
340 * Last reference count, so
341 * release our reference
342 * count on our parent.
353 * Allocate a handle for mapping from kva/uva/physical
354 * address space into bus device space.
357 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
363 if (dmat->segments == NULL) {
364 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
365 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
366 dmat->nsegments, M_DEVBUF, M_INTWAIT);
369 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
370 struct bounce_zone *bz;
375 if (dmat->bounce_zone == NULL) {
376 error = alloc_bounce_zone(dmat);
380 bz = dmat->bounce_zone;
382 *mapp = kmalloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT | M_ZERO);
384 /* Initialize the new map */
385 STAILQ_INIT(&((*mapp)->bpages));
388 * Attempt to add pages to our pool on a per-instance
389 * basis up to a sane limit.
391 if (dmat->flags & BUS_DMA_BOUNCE_ALIGN) {
392 maxpages = max_bounce_pages;
394 maxpages = MIN(max_bounce_pages,
395 Maxmem - atop(dmat->lowaddr));
397 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
398 || (dmat->map_count > 0
399 && bz->total_bpages < maxpages)) {
402 if (flags & BUS_DMA_ONEBPAGE) {
405 pages = atop(round_page(dmat->maxsize));
406 pages = MIN(maxpages - bz->total_bpages, pages);
407 pages = MAX(pages, 1);
409 if (alloc_bounce_pages(dmat, pages, flags) < pages)
412 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
414 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
428 * Destroy a handle for mapping from kva/uva/physical
429 * address space into bus device space.
432 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
435 if (STAILQ_FIRST(&map->bpages) != NULL)
437 kfree(map, M_DEVBUF);
443 static __inline bus_size_t
444 check_kmalloc(bus_dma_tag_t dmat, const void *vaddr0, int verify)
446 bus_size_t maxsize = 0;
447 uintptr_t vaddr = (uintptr_t)vaddr0;
449 if ((vaddr ^ (vaddr + dmat->maxsize - 1)) & ~PAGE_MASK) {
450 kprintf("boundary check failed\n");
452 print_backtrace(); /* XXX panic */
453 maxsize = dmat->maxsize;
455 if (vaddr & (dmat->alignment - 1)) {
456 kprintf("alignment check failed\n");
458 print_backtrace(); /* XXX panic */
459 if (dmat->maxsize < dmat->alignment)
460 maxsize = dmat->alignment;
462 maxsize = dmat->maxsize;
468 * Allocate a piece of memory that can be efficiently mapped into
469 * bus device space based on the constraints lited in the dma tag.
471 * mapp is degenerate. By definition this allocation should not require
472 * bounce buffers so do not allocate a dma map.
475 bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
480 /* If we succeed, no mapping/bouncing will be required */
483 if (dmat->segments == NULL) {
484 KKASSERT(dmat->nsegments < 16384);
485 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
486 dmat->nsegments, M_DEVBUF, M_INTWAIT);
489 if (flags & BUS_DMA_NOWAIT)
493 if (flags & BUS_DMA_ZERO)
496 if (BUS_DMAMEM_KMALLOC(dmat)) {
499 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
503 * Check whether the allocation
504 * - crossed a page boundary
506 * Retry with power-of-2 alignment in the above cases.
508 maxsize = check_kmalloc(dmat, *vaddr, 0);
512 kfree(*vaddr, M_DEVBUF);
513 /* XXX check for overflow? */
514 for (size = 1; size <= maxsize; size <<= 1)
516 *vaddr = kmalloc(size, M_DEVBUF, mflags);
517 check_kmalloc(dmat, *vaddr, 1);
521 * XXX Use Contigmalloc until it is merged into this facility
522 * and handles multi-seg allocations. Nobody is doing
523 * multi-seg allocations yet though.
525 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
526 0ul, dmat->lowaddr, dmat->alignment, dmat->boundary);
534 * Free a piece of memory and it's allociated dmamap, that was allocated
535 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
538 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
541 * dmamem does not need to be bounced, so the map should be
545 panic("bus_dmamem_free: Invalid map freed\n");
546 if (BUS_DMAMEM_KMALLOC(dmat))
547 kfree(vaddr, M_DEVBUF);
549 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
552 static __inline vm_paddr_t
553 _bus_dma_extract(pmap_t pmap, vm_offset_t vaddr)
556 return pmap_extract(pmap, vaddr);
558 return pmap_kextract(vaddr);
562 * Utility function to load a linear buffer. lastaddrp holds state
563 * between invocations (for multiple-buffer loads). segp contains
564 * the segment following the starting one on entrace, and the ending
565 * segment on exit. first indicates if this is the first invocation
569 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
571 void *buf, bus_size_t buflen,
572 bus_dma_segment_t *segments,
576 vm_paddr_t *lastpaddrp,
581 vm_paddr_t paddr, nextpaddr;
582 bus_dma_segment_t *sg;
587 map = &nobounce_dmamap;
590 if (dmat->flags & BUS_DMA_ALIGNED)
591 KKASSERT(((uintptr_t)buf & (dmat->alignment - 1)) == 0);
595 * If we are being called during a callback, pagesneeded will
596 * be non-zero, so we can avoid doing the work twice.
598 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) &&
599 map != &nobounce_dmamap && map->pagesneeded == 0) {
600 vm_offset_t vendaddr;
603 * Count the number of bounce pages
604 * needed in order to complete this transfer
606 vaddr = (vm_offset_t)buf;
607 vendaddr = (vm_offset_t)buf + buflen;
609 while (vaddr < vendaddr) {
610 paddr = _bus_dma_extract(pmap, vaddr);
611 if (run_filter(dmat, paddr) != 0)
613 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
617 /* Reserve Necessary Bounce Pages */
618 if (map->pagesneeded != 0) {
619 struct bounce_zone *bz;
621 bz = dmat->bounce_zone;
623 if (flags & BUS_DMA_NOWAIT) {
624 if (reserve_bounce_pages(dmat, map, 0) != 0) {
630 if (reserve_bounce_pages(dmat, map, 1) != 0) {
631 /* Queue us for resources */
634 map->buflen = buflen;
637 &dmat->bounce_zone->bounce_map_waitinglist,
641 return (EINPROGRESS);
647 KKASSERT(*segp >= 1 && *segp <= nsegments);
649 sg = &segments[seg - 1];
651 vaddr = (vm_offset_t)buf;
652 nextpaddr = *lastpaddrp;
653 bmask = ~(dmat->boundary - 1); /* note: will be 0 if boundary is 0 */
655 /* force at least one segment */
662 paddr = _bus_dma_extract(pmap, vaddr);
663 size = PAGE_SIZE - (paddr & PAGE_MASK);
666 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
668 * note: this paddr has the same in-page offset
669 * as vaddr and thus the paddr above, so the
670 * size does not have to be recalculated
672 paddr = add_bounce_page(dmat, map, vaddr, size);
676 * Fill in the bus_dma_segment
682 } else if (paddr == nextpaddr) {
692 nextpaddr = paddr + size;
695 * Handle maxsegsz and boundary issues with a nested loop
701 * Limit to the boundary and maximum segment size
703 if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
704 tmpsize = dmat->boundary -
705 (sg->ds_addr & ~bmask);
706 if (tmpsize > dmat->maxsegsz)
707 tmpsize = dmat->maxsegsz;
708 KKASSERT(tmpsize < sg->ds_len);
709 } else if (sg->ds_len > dmat->maxsegsz) {
710 tmpsize = dmat->maxsegsz;
716 * Futz, split the data into a new segment.
718 if (seg >= nsegments)
720 sg[1].ds_len = sg[0].ds_len - tmpsize;
721 sg[1].ds_addr = sg[0].ds_addr + tmpsize;
722 sg[0].ds_len = tmpsize;
732 } while (buflen > 0);
738 *lastpaddrp = nextpaddr;
741 if (error && (dmat->flags & BUS_DMA_COULD_BOUNCE) &&
742 map != &nobounce_dmamap) {
743 _bus_dmamap_unload(dmat, map);
744 return_bounce_pages(dmat, map);
750 * Map the buffer buf into bus space using the dmamap map.
753 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
754 bus_size_t buflen, bus_dmamap_callback_t *callback,
755 void *callback_arg, int flags)
757 vm_paddr_t lastaddr = 0;
758 int error, nsegs = 1;
763 * Follow old semantics. Once all of the callers are fixed,
764 * we should get rid of these internal flag "adjustment".
766 flags &= ~BUS_DMA_NOWAIT;
767 flags |= BUS_DMA_WAITOK;
769 map->callback = callback;
770 map->callback_arg = callback_arg;
773 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen,
774 dmat->segments, dmat->nsegments,
775 NULL, flags, &lastaddr, &nsegs, 1);
776 if (error == EINPROGRESS)
779 callback(callback_arg, dmat->segments, nsegs, error);
784 * Like _bus_dmamap_load(), but for mbufs.
787 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
789 bus_dmamap_callback2_t *callback, void *callback_arg,
796 * Follow old semantics. Once all of the callers are fixed,
797 * we should get rid of these internal flag "adjustment".
799 flags &= ~BUS_DMA_WAITOK;
800 flags |= BUS_DMA_NOWAIT;
802 error = bus_dmamap_load_mbuf_segment(dmat, map, m0,
803 dmat->segments, dmat->nsegments, &nsegs, flags);
805 /* force "no valid mappings" in callback */
806 callback(callback_arg, dmat->segments, 0, 0, error);
808 callback(callback_arg, dmat->segments, nsegs,
809 m0->m_pkthdr.len, error);
815 bus_dmamap_load_mbuf_segment(bus_dma_tag_t dmat, bus_dmamap_t map,
817 bus_dma_segment_t *segs, int maxsegs,
818 int *nsegs, int flags)
824 KASSERT(maxsegs >= 1, ("invalid maxsegs %d\n", maxsegs));
825 KASSERT(maxsegs <= dmat->nsegments,
826 ("%d too many segments, dmat only support %d segments\n",
827 maxsegs, dmat->nsegments));
828 KASSERT(flags & BUS_DMA_NOWAIT,
829 ("only BUS_DMA_NOWAIT is supported\n"));
831 if (m0->m_pkthdr.len <= dmat->maxsize) {
833 vm_paddr_t lastaddr = 0;
838 for (m = m0; m != NULL && error == 0; m = m->m_next) {
842 error = _bus_dmamap_load_buffer(dmat, map,
845 NULL, flags, &lastaddr,
847 if (error == ENOMEM && !first) {
849 * Out of bounce pages due to too many
850 * fragments in the mbuf chain; return
859 KKASSERT(*nsegs <= maxsegs && *nsegs >= 1);
865 KKASSERT(error != EINPROGRESS);
870 * Like _bus_dmamap_load(), but for uios.
873 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
875 bus_dmamap_callback2_t *callback, void *callback_arg,
879 int nsegs, error, first, i;
886 * Follow old semantics. Once all of the callers are fixed,
887 * we should get rid of these internal flag "adjustment".
889 flags &= ~BUS_DMA_WAITOK;
890 flags |= BUS_DMA_NOWAIT;
892 resid = uio->uio_resid;
895 if (uio->uio_segflg == UIO_USERSPACE) {
899 KASSERT(td != NULL && td->td_proc != NULL,
900 ("bus_dmamap_load_uio: USERSPACE but no proc"));
901 pmap = vmspace_pmap(td->td_proc->p_vmspace);
910 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
912 * Now at the first iovec to load. Load each iovec
913 * until we have exhausted the residual count.
916 resid < iov[i].iov_len ? resid : iov[i].iov_len;
917 caddr_t addr = (caddr_t) iov[i].iov_base;
919 error = _bus_dmamap_load_buffer(dmat, map, addr, minlen,
920 dmat->segments, dmat->nsegments,
921 pmap, flags, &lastaddr, &nsegs, first);
928 /* force "no valid mappings" in callback */
929 callback(callback_arg, dmat->segments, 0, 0, error);
931 callback(callback_arg, dmat->segments, nsegs,
932 uio->uio_resid, error);
938 * Release the mapping held by map.
941 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
943 struct bounce_page *bpage;
945 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
946 STAILQ_REMOVE_HEAD(&map->bpages, links);
947 free_bounce_page(dmat, bpage);
952 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
954 struct bounce_page *bpage;
956 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
958 * Handle data bouncing. We might also
959 * want to add support for invalidating
960 * the caches on broken hardware
963 case BUS_DMASYNC_PREWRITE:
964 while (bpage != NULL) {
965 bcopy((void *)bpage->datavaddr,
966 (void *)bpage->vaddr,
968 bpage = STAILQ_NEXT(bpage, links);
970 dmat->bounce_zone->total_bounced++;
973 case BUS_DMASYNC_POSTREAD:
974 while (bpage != NULL) {
975 bcopy((void *)bpage->vaddr,
976 (void *)bpage->datavaddr,
978 bpage = STAILQ_NEXT(bpage, links);
980 dmat->bounce_zone->total_bounced++;
983 case BUS_DMASYNC_PREREAD:
984 case BUS_DMASYNC_POSTWRITE:
992 alloc_bounce_zone(bus_dma_tag_t dmat)
994 struct bounce_zone *bz, *new_bz;
997 KASSERT(dmat->bounce_zone == NULL,
998 ("bounce zone was already assigned\n"));
1000 new_bz = kmalloc(sizeof(*new_bz), M_DEVBUF, M_INTWAIT | M_ZERO);
1002 lwkt_gettoken(&ref, &bounce_zone_tok);
1004 /* Check to see if we already have a suitable zone */
1005 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1006 if (dmat->alignment <= bz->alignment &&
1007 dmat->lowaddr >= bz->lowaddr) {
1008 lwkt_reltoken(&ref);
1010 dmat->bounce_zone = bz;
1011 kfree(new_bz, M_DEVBUF);
1018 spin_init(&bz->spin);
1020 STAILQ_INIT(&bz->bounce_page_list);
1021 STAILQ_INIT(&bz->bounce_map_waitinglist);
1022 bz->free_bpages = 0;
1023 bz->reserved_bpages = 0;
1024 bz->active_bpages = 0;
1025 bz->lowaddr = dmat->lowaddr;
1026 bz->alignment = round_page(dmat->alignment);
1027 ksnprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1029 ksnprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1030 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1032 lwkt_reltoken(&ref);
1034 dmat->bounce_zone = bz;
1036 sysctl_ctx_init(&bz->sysctl_ctx);
1037 bz->sysctl_tree = SYSCTL_ADD_NODE(&bz->sysctl_ctx,
1038 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1040 if (bz->sysctl_tree == NULL) {
1041 sysctl_ctx_free(&bz->sysctl_ctx);
1042 return 0; /* XXX error code? */
1045 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1046 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1047 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1048 "Total bounce pages");
1049 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1050 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1051 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1052 "Free bounce pages");
1053 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1054 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1055 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1056 "Reserved bounce pages");
1057 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1058 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1059 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1060 "Active bounce pages");
1061 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1062 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1063 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1064 "Total bounce requests");
1065 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1066 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1067 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1068 "Total bounce requests that were deferred");
1069 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1070 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1071 "reserve_failed", CTLFLAG_RD, &bz->reserve_failed, 0,
1072 "Total bounce page reservations that were failed");
1073 SYSCTL_ADD_STRING(&bz->sysctl_ctx,
1074 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1075 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1076 SYSCTL_ADD_INT(&bz->sysctl_ctx,
1077 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1078 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1084 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages, int flags)
1086 struct bounce_zone *bz = dmat->bounce_zone;
1087 int count = 0, mflags;
1089 if (flags & BUS_DMA_NOWAIT)
1094 while (numpages > 0) {
1095 struct bounce_page *bpage;
1097 bpage = kmalloc(sizeof(*bpage), M_DEVBUF, M_INTWAIT | M_ZERO);
1099 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1103 if (bpage->vaddr == 0) {
1104 kfree(bpage, M_DEVBUF);
1107 bpage->busaddr = pmap_kextract(bpage->vaddr);
1110 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1111 total_bounce_pages++;
1122 /* Assume caller holds bounce zone spinlock */
1124 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1126 struct bounce_zone *bz = dmat->bounce_zone;
1129 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1130 if (!commit && map->pagesneeded > (map->pagesreserved + pages)) {
1131 bz->reserve_failed++;
1132 return (map->pagesneeded - (map->pagesreserved + pages));
1135 bz->free_bpages -= pages;
1137 bz->reserved_bpages += pages;
1138 KKASSERT(bz->reserved_bpages <= bz->total_bpages);
1140 map->pagesreserved += pages;
1141 pages = map->pagesneeded - map->pagesreserved;
1147 return_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
1149 struct bounce_zone *bz = dmat->bounce_zone;
1150 int reserved = map->pagesreserved;
1151 bus_dmamap_t wait_map;
1153 map->pagesreserved = 0;
1154 map->pagesneeded = 0;
1161 bz->free_bpages += reserved;
1162 KKASSERT(bz->free_bpages <= bz->total_bpages);
1164 KKASSERT(bz->reserved_bpages >= reserved);
1165 bz->reserved_bpages -= reserved;
1167 wait_map = get_map_waiting(dmat);
1171 if (wait_map != NULL)
1172 add_map_callback(map);
1176 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1179 struct bounce_zone *bz = dmat->bounce_zone;
1180 struct bounce_page *bpage;
1182 KASSERT(map->pagesneeded > 0, ("map doesn't need any pages"));
1185 KASSERT(map->pagesreserved > 0, ("map doesn't reserve any pages"));
1186 map->pagesreserved--;
1190 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1191 KASSERT(bpage != NULL, ("free page list is empty"));
1192 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1194 KKASSERT(bz->reserved_bpages > 0);
1195 bz->reserved_bpages--;
1197 bz->active_bpages++;
1198 KKASSERT(bz->active_bpages <= bz->total_bpages);
1202 bpage->datavaddr = vaddr;
1203 bpage->datacount = size;
1204 STAILQ_INSERT_TAIL(&map->bpages, bpage, links);
1205 return bpage->busaddr;
1209 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1211 struct bounce_zone *bz = dmat->bounce_zone;
1214 bpage->datavaddr = 0;
1215 bpage->datacount = 0;
1219 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1222 KKASSERT(bz->free_bpages <= bz->total_bpages);
1224 KKASSERT(bz->active_bpages > 0);
1225 bz->active_bpages--;
1227 map = get_map_waiting(dmat);
1232 add_map_callback(map);
1235 /* Assume caller holds bounce zone spinlock */
1237 get_map_waiting(bus_dma_tag_t dmat)
1239 struct bounce_zone *bz = dmat->bounce_zone;
1242 map = STAILQ_FIRST(&bz->bounce_map_waitinglist);
1244 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1245 STAILQ_REMOVE_HEAD(&bz->bounce_map_waitinglist, links);
1246 bz->total_deferred++;
1255 add_map_callback(bus_dmamap_t map)
1258 /* XXX callbacklist is not MPSAFE */
1261 STAILQ_INSERT_TAIL(&bounce_map_callbacklist, map, links);
1262 busdma_swi_pending = 1;
1267 panic("%s uncoded\n", __func__);
1278 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1279 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1281 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1282 map->callback, map->callback_arg, /*flags*/0);