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.16.2.2 2003/01/23 00:55:27 scottl 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>
41 #include <vm/vm_page.h>
43 /* XXX needed for to access pmap to convert per-proc virtual to physical */
46 #include <vm/vm_map.h>
48 #include <machine/md_var.h>
50 #define MAX_BPAGES 1024
60 bus_dma_filter_t *filter;
68 bus_dma_segment_t *segments;
69 struct bounce_zone *bounce_zone;
73 * bus_dma_tag private flags
75 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
76 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
79 vm_offset_t vaddr; /* kva of bounce buffer */
80 bus_addr_t busaddr; /* Physical address */
81 vm_offset_t datavaddr; /* kva of client data */
82 bus_size_t datacount; /* client data count */
83 STAILQ_ENTRY(bounce_page) links;
87 STAILQ_ENTRY(bounce_zone) links;
88 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
101 struct sysctl_ctx_list sysctl_ctx;
102 struct sysctl_oid *sysctl_tree;
105 static int busdma_zonecount;
106 static STAILQ_HEAD(, bounce_zone) bounce_zone_list =
107 STAILQ_HEAD_INITIALIZER(bounce_zone_list);
109 int busdma_swi_pending;
110 static int total_bounce_pages;
111 static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
114 struct bp_list bpages;
118 void *buf; /* unmapped buffer pointer */
119 bus_size_t buflen; /* unmapped buffer length */
120 bus_dmamap_callback_t *callback;
122 STAILQ_ENTRY(bus_dmamap) links;
125 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist =
126 STAILQ_HEAD_INITIALIZER(bounce_map_waitinglist);
128 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist =
129 STAILQ_HEAD_INITIALIZER(bounce_map_callbacklist);
131 static struct bus_dmamap nobounce_dmamap;
133 static int alloc_bounce_zone(bus_dma_tag_t);
134 static int alloc_bounce_pages(bus_dma_tag_t, u_int);
135 static int reserve_bounce_pages(bus_dma_tag_t, bus_dmamap_t, int);
136 static bus_addr_t add_bounce_page(bus_dma_tag_t, bus_dmamap_t,
137 vm_offset_t, bus_size_t);
138 static void free_bounce_page(bus_dma_tag_t, struct bounce_page *);
140 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
141 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bounce_pages,
142 0, "Total bounce pages");
145 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
151 if (paddr > dmat->lowaddr
152 && paddr <= dmat->highaddr
153 && (dmat->filter == NULL
154 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
158 } while (retval == 0 && dmat != NULL);
163 * Allocate a device specific dma_tag.
166 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
167 bus_size_t boundary, bus_addr_t lowaddr,
168 bus_addr_t highaddr, bus_dma_filter_t *filter,
169 void *filterarg, bus_size_t maxsize, int nsegments,
170 bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
172 bus_dma_tag_t newtag;
178 /* Return a NULL tag on failure */
181 newtag = kmalloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT);
183 newtag->parent = parent;
184 newtag->alignment = alignment;
185 newtag->boundary = boundary;
186 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
187 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
188 newtag->filter = filter;
189 newtag->filterarg = filterarg;
190 newtag->maxsize = maxsize;
191 newtag->nsegments = nsegments;
192 newtag->maxsegsz = maxsegsz;
193 newtag->flags = flags;
194 newtag->ref_count = 1; /* Count ourself */
195 newtag->map_count = 0;
196 newtag->segments = NULL;
197 newtag->bounce_zone = NULL;
199 /* Take into account any restrictions imposed by our parent tag */
200 if (parent != NULL) {
201 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
202 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
204 if (newtag->boundary == 0) {
205 newtag->boundary = parent->boundary;
206 } else if (parent->boundary != 0) {
207 newtag->boundary = MIN(parent->boundary,
212 newtag->alignment = MAX(parent->alignment, newtag->alignment);
215 if (newtag->filter == NULL) {
217 * Short circuit looking at our parent directly
218 * since we have encapsulated all of its information
220 newtag->filter = parent->filter;
221 newtag->filterarg = parent->filterarg;
222 newtag->parent = parent->parent;
224 if (newtag->parent != NULL)
228 if (newtag->lowaddr < ptoa(Maxmem))
229 newtag->flags |= BUS_DMA_COULD_BOUNCE;
231 if ((newtag->flags & BUS_DMA_COULD_BOUNCE) &&
232 (flags & BUS_DMA_ALLOCNOW) != 0) {
233 struct bounce_zone *bz;
237 if (lowaddr > bounce_lowaddr) {
239 * Go through the pool and kill any pages
240 * that don't reside below lowaddr.
242 panic("bus_dma_tag_create: page reallocation "
246 error = alloc_bounce_zone(newtag);
249 bz = newtag->bounce_zone;
251 if (ptoa(bz->total_bpages) < maxsize) {
254 pages = atop(maxsize) - bz->total_bpages;
256 /* Add pages to our bounce pool */
257 if (alloc_bounce_pages(newtag, pages) < pages)
260 /* Performed initial allocation */
261 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
265 kfree(newtag, M_DEVBUF);
272 bus_dma_tag_destroy(bus_dma_tag_t dmat)
275 if (dmat->map_count != 0)
278 while (dmat != NULL) {
279 bus_dma_tag_t parent;
281 parent = dmat->parent;
283 if (dmat->ref_count == 0) {
284 if (dmat->segments != NULL)
285 kfree(dmat->segments, M_DEVBUF);
286 kfree(dmat, M_DEVBUF);
288 * Last reference count, so
289 * release our reference
290 * count on our parent.
301 * Allocate a handle for mapping from kva/uva/physical
302 * address space into bus device space.
305 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
311 if (dmat->segments == NULL) {
312 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
313 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
314 dmat->nsegments, M_DEVBUF, M_INTWAIT);
317 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
318 struct bounce_zone *bz;
323 if (dmat->bounce_zone == NULL) {
324 error = alloc_bounce_zone(dmat);
328 bz = dmat->bounce_zone;
330 *mapp = kmalloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT | M_ZERO);
332 /* Initialize the new map */
333 STAILQ_INIT(&((*mapp)->bpages));
335 * Attempt to add pages to our pool on a per-instance
336 * basis up to a sane limit.
338 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
339 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
340 || (dmat->map_count > 0
341 && bz->total_bpages < maxpages)) {
344 if (dmat->lowaddr > bounce_lowaddr) {
346 * Go through the pool and kill any pages
347 * that don't reside below lowaddr.
349 panic("bus_dmamap_create: page reallocation "
353 pages = MAX(atop(dmat->maxsize), 1);
354 pages = MIN(maxpages - bz->total_bpages, pages);
355 pages = MAX(pages, 1);
356 if (alloc_bounce_pages(dmat, pages) < pages)
359 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
361 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
375 * Destroy a handle for mapping from kva/uva/physical
376 * address space into bus device space.
379 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
382 if (STAILQ_FIRST(&map->bpages) != NULL)
384 kfree(map, M_DEVBUF);
391 * Allocate a piece of memory that can be efficiently mapped into
392 * bus device space based on the constraints lited in the dma tag.
394 * mapp is degenerate. By definition this allocation should not require
395 * bounce buffers so do not allocate a dma map.
398 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
403 /* If we succeed, no mapping/bouncing will be required */
406 if (dmat->segments == NULL) {
407 KKASSERT(dmat->nsegments < 16384);
408 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
409 dmat->nsegments, M_DEVBUF, M_INTWAIT);
412 if (flags & BUS_DMA_NOWAIT)
416 if (flags & BUS_DMA_ZERO)
419 if ((dmat->maxsize <= PAGE_SIZE) &&
420 dmat->lowaddr >= ptoa(Maxmem)) {
421 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
423 * XXX Check whether the allocation crossed a page boundary
424 * and retry with power-of-2 alignment in that case.
426 if ((((intptr_t)*vaddr) & PAGE_MASK) !=
427 (((intptr_t)*vaddr + dmat->maxsize) & PAGE_MASK)) {
430 kfree(*vaddr, M_DEVBUF);
431 /* XXX check for overflow? */
432 for (size = 1; size <= dmat->maxsize; size <<= 1)
434 *vaddr = kmalloc(size, M_DEVBUF, mflags);
438 * XXX Use Contigmalloc until it is merged into this facility
439 * and handles multi-seg allocations. Nobody is doing
440 * multi-seg allocations yet though.
442 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
443 0ul, dmat->lowaddr, dmat->alignment, dmat->boundary);
451 * Free a piece of memory and it's allociated dmamap, that was allocated
452 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
455 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
458 * dmamem does not need to be bounced, so the map should be
462 panic("bus_dmamem_free: Invalid map freed\n");
463 if ((dmat->maxsize <= PAGE_SIZE) &&
464 dmat->lowaddr >= ptoa(Maxmem))
465 kfree(vaddr, M_DEVBUF);
467 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
470 static __inline vm_paddr_t
471 _bus_dma_extract(pmap_t pmap, vm_offset_t vaddr)
474 return pmap_extract(pmap, vaddr);
476 return pmap_kextract(vaddr);
480 * Utility function to load a linear buffer. lastaddrp holds state
481 * between invocations (for multiple-buffer loads). segp contains
482 * the segment following the starting one on entrace, and the ending
483 * segment on exit. first indicates if this is the first invocation
487 _bus_dmamap_load_buffer2(bus_dma_tag_t dmat,
489 void *buf, bus_size_t buflen,
492 vm_paddr_t *lastpaddrp,
497 vm_paddr_t paddr, nextpaddr;
498 bus_dma_segment_t *sg;
503 map = &nobounce_dmamap;
506 * If we are being called during a callback, pagesneeded will
507 * be non-zero, so we can avoid doing the work twice.
509 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) &&
510 map != &nobounce_dmamap && map->pagesneeded == 0) {
511 vm_offset_t vendaddr;
514 * Count the number of bounce pages
515 * needed in order to complete this transfer
517 vaddr = (vm_offset_t)buf;
518 vendaddr = (vm_offset_t)buf + buflen;
520 while (vaddr < vendaddr) {
521 paddr = _bus_dma_extract(pmap, vaddr);
522 if (run_filter(dmat, paddr) != 0)
524 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
528 /* Reserve Necessary Bounce Pages */
529 if (map->pagesneeded != 0) {
531 if (flags & BUS_DMA_NOWAIT) {
532 if (reserve_bounce_pages(dmat, map, 0) != 0) {
537 if (reserve_bounce_pages(dmat, map, 1) != 0) {
538 /* Queue us for resources */
541 map->buflen = buflen;
543 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
547 return (EINPROGRESS);
553 KKASSERT(*segp >= 1);
555 sg = &dmat->segments[seg - 1];
557 vaddr = (vm_offset_t)buf;
558 nextpaddr = *lastpaddrp;
559 bmask = ~(dmat->boundary - 1); /* note: will be 0 if boundary is 0 */
561 /* force at least one segment */
568 paddr = _bus_dma_extract(pmap, vaddr);
569 size = PAGE_SIZE - (paddr & PAGE_MASK);
572 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
574 * note: this paddr has the same in-page offset
575 * as vaddr and thus the paddr above, so the
576 * size does not have to be recalculated
578 paddr = add_bounce_page(dmat, map, vaddr, size);
582 * Fill in the bus_dma_segment
588 } else if (paddr == nextpaddr) {
593 if (seg > dmat->nsegments)
598 nextpaddr = paddr + size;
601 * Handle maxsegsz and boundary issues with a nested loop
607 * Limit to the boundary and maximum segment size
609 if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
610 tmpsize = dmat->boundary -
611 (sg->ds_addr & ~bmask);
612 if (tmpsize > dmat->maxsegsz)
613 tmpsize = dmat->maxsegsz;
614 KKASSERT(tmpsize < sg->ds_len);
615 } else if (sg->ds_len > dmat->maxsegsz) {
616 tmpsize = dmat->maxsegsz;
622 * Futz, split the data into a new segment.
624 if (seg >= dmat->nsegments)
626 sg[1].ds_len = sg[0].ds_len - tmpsize;
627 sg[1].ds_addr = sg[0].ds_addr + tmpsize;
628 sg[0].ds_len = tmpsize;
638 } while (buflen > 0);
641 kprintf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
647 *lastpaddrp = nextpaddr;
653 * Map the buffer buf into bus space using the dmamap map.
656 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
657 bus_size_t buflen, bus_dmamap_callback_t *callback,
658 void *callback_arg, int flags)
660 vm_paddr_t lastaddr = 0;
661 int error, nsegs = 1;
666 * Follow old semantics. Once all of the callers are fixed,
667 * we should get rid of these internal flag "adjustment".
669 flags &= ~BUS_DMA_NOWAIT;
670 flags |= BUS_DMA_WAITOK;
672 map->callback = callback;
673 map->callback_arg = callback_arg;
676 error = _bus_dmamap_load_buffer2(dmat, map,
677 buf, buflen, NULL, flags, &lastaddr, &nsegs, 1);
678 if (error == EINPROGRESS)
681 callback(callback_arg, dmat->segments, nsegs, error);
686 * Like _bus_dmamap_load(), but for mbufs.
689 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
691 bus_dmamap_callback2_t *callback, void *callback_arg,
700 * Follow old semantics. Once all of the callers are fixed,
701 * we should get rid of these internal flag "adjustment".
703 flags &= ~BUS_DMA_WAITOK;
704 flags |= BUS_DMA_NOWAIT;
706 if (m0->m_pkthdr.len <= dmat->maxsize) {
708 vm_paddr_t lastaddr = 0;
713 for (m = m0; m != NULL && error == 0; m = m->m_next) {
717 error = _bus_dmamap_load_buffer2(dmat, map,
719 NULL, flags, &lastaddr,
729 /* force "no valid mappings" in callback */
730 callback(callback_arg, dmat->segments, 0, 0, error);
732 callback(callback_arg, dmat->segments, nsegs,
733 m0->m_pkthdr.len, error);
739 * Like _bus_dmamap_load(), but for uios.
742 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
744 bus_dmamap_callback2_t *callback, void *callback_arg,
748 int nsegs, error, first, i;
755 * Follow old semantics. Once all of the callers are fixed,
756 * we should get rid of these internal flag "adjustment".
758 flags &= ~BUS_DMA_WAITOK;
759 flags |= BUS_DMA_NOWAIT;
761 resid = uio->uio_resid;
764 if (uio->uio_segflg == UIO_USERSPACE) {
768 KASSERT(td != NULL && td->td_proc != NULL,
769 ("bus_dmamap_load_uio: USERSPACE but no proc"));
770 pmap = vmspace_pmap(td->td_proc->p_vmspace);
779 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
781 * Now at the first iovec to load. Load each iovec
782 * until we have exhausted the residual count.
785 resid < iov[i].iov_len ? resid : iov[i].iov_len;
786 caddr_t addr = (caddr_t) iov[i].iov_base;
788 error = _bus_dmamap_load_buffer2(dmat, map, addr, minlen,
789 pmap, flags, &lastaddr, &nsegs, first);
796 /* force "no valid mappings" in callback */
797 callback(callback_arg, dmat->segments, 0, 0, error);
799 callback(callback_arg, dmat->segments, nsegs,
800 uio->uio_resid, error);
806 * Release the mapping held by map.
809 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
811 struct bounce_page *bpage;
813 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
814 STAILQ_REMOVE_HEAD(&map->bpages, links);
815 free_bounce_page(dmat, bpage);
820 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
822 struct bounce_page *bpage;
824 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
826 * Handle data bouncing. We might also
827 * want to add support for invalidating
828 * the caches on broken hardware
831 case BUS_DMASYNC_PREWRITE:
832 while (bpage != NULL) {
833 bcopy((void *)bpage->datavaddr,
834 (void *)bpage->vaddr,
836 bpage = STAILQ_NEXT(bpage, links);
838 dmat->bounce_zone->total_bounced++;
841 case BUS_DMASYNC_POSTREAD:
842 while (bpage != NULL) {
843 bcopy((void *)bpage->vaddr,
844 (void *)bpage->datavaddr,
846 bpage = STAILQ_NEXT(bpage, links);
848 dmat->bounce_zone->total_bounced++;
851 case BUS_DMASYNC_PREREAD:
852 case BUS_DMASYNC_POSTWRITE:
860 alloc_bounce_zone(bus_dma_tag_t dmat)
862 struct bounce_zone *bz;
864 KASSERT(dmat->bounce_zone == NULL,
865 ("bounce zone was already assigned\n"));
867 /* Check to see if we already have a suitable zone */
868 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
869 if (dmat->alignment <= bz->alignment &&
870 dmat->boundary <= bz->boundary &&
871 dmat->lowaddr >= bz->lowaddr) {
872 dmat->bounce_zone = bz;
877 bz = kmalloc(sizeof(*bz), M_DEVBUF, M_INTWAIT | M_ZERO);
879 STAILQ_INIT(&bz->bounce_page_list);
881 bz->reserved_bpages = 0;
882 bz->active_bpages = 0;
883 bz->lowaddr = dmat->lowaddr;
884 bz->alignment = dmat->alignment;
885 bz->boundary = dmat->boundary;
886 ksnprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
888 ksnprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
889 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
890 dmat->bounce_zone = bz;
892 sysctl_ctx_init(&bz->sysctl_ctx);
893 bz->sysctl_tree = SYSCTL_ADD_NODE(&bz->sysctl_ctx,
894 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
896 if (bz->sysctl_tree == NULL) {
897 sysctl_ctx_free(&bz->sysctl_ctx);
898 return 0; /* XXX error code? */
901 SYSCTL_ADD_INT(&bz->sysctl_ctx,
902 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
903 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
904 "Total bounce pages");
905 SYSCTL_ADD_INT(&bz->sysctl_ctx,
906 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
907 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
908 "Free bounce pages");
909 SYSCTL_ADD_INT(&bz->sysctl_ctx,
910 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
911 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
912 "Reserved bounce pages");
913 SYSCTL_ADD_INT(&bz->sysctl_ctx,
914 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
915 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
916 "Active bounce pages");
917 SYSCTL_ADD_INT(&bz->sysctl_ctx,
918 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
919 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
920 "Total bounce requests");
921 SYSCTL_ADD_INT(&bz->sysctl_ctx,
922 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
923 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
924 "Total bounce requests that were deferred");
925 SYSCTL_ADD_INT(&bz->sysctl_ctx,
926 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
927 "reserve_failed", CTLFLAG_RD, &bz->reserve_failed, 0,
928 "Total bounce page reservations that were failed");
929 SYSCTL_ADD_STRING(&bz->sysctl_ctx,
930 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
931 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
932 SYSCTL_ADD_INT(&bz->sysctl_ctx,
933 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
934 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
935 SYSCTL_ADD_INT(&bz->sysctl_ctx,
936 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
937 "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
943 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
945 struct bounce_zone *bz = dmat->bounce_zone;
948 while (numpages > 0) {
949 struct bounce_page *bpage;
951 bpage = kmalloc(sizeof(*bpage), M_DEVBUF, M_INTWAIT | M_ZERO);
953 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
958 if (bpage->vaddr == 0) {
959 kfree(bpage, M_DEVBUF);
962 bpage->busaddr = pmap_kextract(bpage->vaddr);
965 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
966 total_bounce_pages++;
978 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
980 struct bounce_zone *bz = dmat->bounce_zone;
983 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
984 if (!commit && map->pagesneeded > (map->pagesreserved + pages)) {
985 bz->reserve_failed++;
986 return (map->pagesneeded - (map->pagesreserved + pages));
988 bz->free_bpages -= pages;
989 bz->reserved_bpages += pages;
990 map->pagesreserved += pages;
991 pages = map->pagesneeded - map->pagesreserved;
997 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1000 struct bounce_zone *bz = dmat->bounce_zone;
1001 struct bounce_page *bpage;
1003 if (map->pagesneeded == 0)
1004 panic("add_bounce_page: map doesn't need any pages");
1007 if (map->pagesreserved == 0)
1008 panic("add_bounce_page: map doesn't need any pages");
1009 map->pagesreserved--;
1012 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1014 panic("add_bounce_page: free page list is empty");
1016 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1017 bz->reserved_bpages--;
1018 bz->active_bpages++;
1021 bpage->datavaddr = vaddr;
1022 bpage->datacount = size;
1023 STAILQ_INSERT_TAIL(&map->bpages, bpage, links);
1024 return bpage->busaddr;
1028 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1030 struct bounce_zone *bz = dmat->bounce_zone;
1031 struct bus_dmamap *map;
1033 bpage->datavaddr = 0;
1034 bpage->datacount = 0;
1037 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1039 bz->active_bpages--;
1040 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1041 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1042 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1043 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1045 busdma_swi_pending = 1;
1046 bz->total_deferred++;
1056 struct bus_dmamap *map;
1059 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1060 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1062 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1063 map->callback, map->callback_arg, /*flags*/0);