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>
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_COULD_BOUNCE BUS_DMA_BUS3
79 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
82 vm_offset_t vaddr; /* kva of bounce buffer */
83 bus_addr_t busaddr; /* Physical address */
84 vm_offset_t datavaddr; /* kva of client data */
85 bus_size_t datacount; /* client data count */
86 STAILQ_ENTRY(bounce_page) links;
90 STAILQ_ENTRY(bounce_zone) links;
91 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
92 STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
105 bus_size_t alignment;
110 struct sysctl_ctx_list sysctl_ctx;
111 struct sysctl_oid *sysctl_tree;
115 #define BZ_LOCK(bz) spin_lock_wr(&(bz)->spin)
116 #define BZ_UNLOCK(bz) spin_unlock_wr(&(bz)->spin)
118 #define BZ_LOCK(bz) crit_enter()
119 #define BZ_UNLOCK(bz) crit_exit()
122 static struct lwkt_token bounce_zone_tok =
123 LWKT_TOKEN_INITIALIZER(bounce_zone_tok);
124 static int busdma_zonecount;
125 static STAILQ_HEAD(, bounce_zone) bounce_zone_list =
126 STAILQ_HEAD_INITIALIZER(bounce_zone_list);
128 int busdma_swi_pending;
129 static int total_bounce_pages;
130 static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
133 struct bp_list bpages;
137 void *buf; /* unmapped buffer pointer */
138 bus_size_t buflen; /* unmapped buffer length */
139 bus_dmamap_callback_t *callback;
141 STAILQ_ENTRY(bus_dmamap) links;
144 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist =
145 STAILQ_HEAD_INITIALIZER(bounce_map_callbacklist);
147 static struct bus_dmamap nobounce_dmamap;
149 static int alloc_bounce_zone(bus_dma_tag_t);
150 static int alloc_bounce_pages(bus_dma_tag_t, u_int);
151 static int reserve_bounce_pages(bus_dma_tag_t, bus_dmamap_t, int);
152 static void return_bounce_pages(bus_dma_tag_t, bus_dmamap_t);
153 static bus_addr_t add_bounce_page(bus_dma_tag_t, bus_dmamap_t,
154 vm_offset_t, bus_size_t);
155 static void free_bounce_page(bus_dma_tag_t, struct bounce_page *);
157 static bus_dmamap_t get_map_waiting(bus_dma_tag_t);
158 static void add_map_callback(bus_dmamap_t);
160 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
161 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bounce_pages,
162 0, "Total bounce pages");
165 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
171 if (paddr > dmat->lowaddr
172 && paddr <= dmat->highaddr
173 && (dmat->filter == NULL
174 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
178 } while (retval == 0 && dmat != NULL);
183 * Allocate a device specific dma_tag.
186 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
187 bus_size_t boundary, bus_addr_t lowaddr,
188 bus_addr_t highaddr, bus_dma_filter_t *filter,
189 void *filterarg, bus_size_t maxsize, int nsegments,
190 bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
192 bus_dma_tag_t newtag;
198 /* Return a NULL tag on failure */
201 newtag = kmalloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT);
203 newtag->parent = parent;
204 newtag->alignment = alignment;
205 newtag->boundary = boundary;
206 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
207 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
208 newtag->filter = filter;
209 newtag->filterarg = filterarg;
210 newtag->maxsize = maxsize;
211 newtag->nsegments = nsegments;
212 newtag->maxsegsz = maxsegsz;
213 newtag->flags = flags;
214 newtag->ref_count = 1; /* Count ourself */
215 newtag->map_count = 0;
216 newtag->segments = NULL;
217 newtag->bounce_zone = NULL;
219 /* Take into account any restrictions imposed by our parent tag */
220 if (parent != NULL) {
221 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
222 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
224 if (newtag->boundary == 0) {
225 newtag->boundary = parent->boundary;
226 } else if (parent->boundary != 0) {
227 newtag->boundary = MIN(parent->boundary,
232 newtag->alignment = MAX(parent->alignment, newtag->alignment);
235 if (newtag->filter == NULL) {
237 * Short circuit looking at our parent directly
238 * since we have encapsulated all of its information
240 newtag->filter = parent->filter;
241 newtag->filterarg = parent->filterarg;
242 newtag->parent = parent->parent;
244 if (newtag->parent != NULL)
248 if (newtag->lowaddr < ptoa(Maxmem))
249 newtag->flags |= BUS_DMA_COULD_BOUNCE;
251 if ((newtag->flags & BUS_DMA_COULD_BOUNCE) &&
252 (flags & BUS_DMA_ALLOCNOW) != 0) {
253 struct bounce_zone *bz;
257 if (lowaddr > bounce_lowaddr) {
259 * Go through the pool and kill any pages
260 * that don't reside below lowaddr.
262 panic("bus_dma_tag_create: page reallocation "
266 error = alloc_bounce_zone(newtag);
269 bz = newtag->bounce_zone;
271 if (ptoa(bz->total_bpages) < maxsize) {
274 pages = atop(maxsize) - bz->total_bpages;
276 /* Add pages to our bounce pool */
277 if (alloc_bounce_pages(newtag, pages) < pages)
280 /* Performed initial allocation */
281 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
285 kfree(newtag, M_DEVBUF);
292 bus_dma_tag_destroy(bus_dma_tag_t dmat)
295 if (dmat->map_count != 0)
298 while (dmat != NULL) {
299 bus_dma_tag_t parent;
301 parent = dmat->parent;
303 if (dmat->ref_count == 0) {
304 if (dmat->segments != NULL)
305 kfree(dmat->segments, M_DEVBUF);
306 kfree(dmat, M_DEVBUF);
308 * Last reference count, so
309 * release our reference
310 * count on our parent.
321 * Allocate a handle for mapping from kva/uva/physical
322 * address space into bus device space.
325 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
331 if (dmat->segments == NULL) {
332 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
333 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
334 dmat->nsegments, M_DEVBUF, M_INTWAIT);
337 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
338 struct bounce_zone *bz;
343 if (dmat->bounce_zone == NULL) {
344 error = alloc_bounce_zone(dmat);
348 bz = dmat->bounce_zone;
350 *mapp = kmalloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT | M_ZERO);
352 /* Initialize the new map */
353 STAILQ_INIT(&((*mapp)->bpages));
355 * Attempt to add pages to our pool on a per-instance
356 * basis up to a sane limit.
358 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
359 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
360 || (dmat->map_count > 0
361 && bz->total_bpages < maxpages)) {
364 if (dmat->lowaddr > bounce_lowaddr) {
366 * Go through the pool and kill any pages
367 * that don't reside below lowaddr.
369 panic("bus_dmamap_create: page reallocation "
373 pages = MAX(atop(dmat->maxsize), 1);
374 pages = MIN(maxpages - bz->total_bpages, pages);
375 pages = MAX(pages, 1);
376 if (alloc_bounce_pages(dmat, pages) < pages)
379 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
381 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
395 * Destroy a handle for mapping from kva/uva/physical
396 * address space into bus device space.
399 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
402 if (STAILQ_FIRST(&map->bpages) != NULL)
404 kfree(map, M_DEVBUF);
411 * Allocate a piece of memory that can be efficiently mapped into
412 * bus device space based on the constraints lited in the dma tag.
414 * mapp is degenerate. By definition this allocation should not require
415 * bounce buffers so do not allocate a dma map.
418 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
423 /* If we succeed, no mapping/bouncing will be required */
426 if (dmat->segments == NULL) {
427 KKASSERT(dmat->nsegments < 16384);
428 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
429 dmat->nsegments, M_DEVBUF, M_INTWAIT);
432 if (flags & BUS_DMA_NOWAIT)
436 if (flags & BUS_DMA_ZERO)
439 if ((dmat->maxsize <= PAGE_SIZE) &&
440 dmat->lowaddr >= ptoa(Maxmem)) {
441 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
443 * XXX Check whether the allocation crossed a page boundary
444 * and retry with power-of-2 alignment in that case.
446 if ((((intptr_t)*vaddr) & PAGE_MASK) !=
447 (((intptr_t)*vaddr + dmat->maxsize) & PAGE_MASK)) {
450 kfree(*vaddr, M_DEVBUF);
451 /* XXX check for overflow? */
452 for (size = 1; size <= dmat->maxsize; size <<= 1)
454 *vaddr = kmalloc(size, M_DEVBUF, mflags);
458 * XXX Use Contigmalloc until it is merged into this facility
459 * and handles multi-seg allocations. Nobody is doing
460 * multi-seg allocations yet though.
462 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
463 0ul, dmat->lowaddr, dmat->alignment, dmat->boundary);
471 * Free a piece of memory and it's allociated dmamap, that was allocated
472 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
475 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
478 * dmamem does not need to be bounced, so the map should be
482 panic("bus_dmamem_free: Invalid map freed\n");
483 if ((dmat->maxsize <= PAGE_SIZE) &&
484 dmat->lowaddr >= ptoa(Maxmem))
485 kfree(vaddr, M_DEVBUF);
487 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
490 static __inline vm_paddr_t
491 _bus_dma_extract(pmap_t pmap, vm_offset_t vaddr)
494 return pmap_extract(pmap, vaddr);
496 return pmap_kextract(vaddr);
500 * Utility function to load a linear buffer. lastaddrp holds state
501 * between invocations (for multiple-buffer loads). segp contains
502 * the segment following the starting one on entrace, and the ending
503 * segment on exit. first indicates if this is the first invocation
507 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
509 void *buf, bus_size_t buflen,
512 vm_paddr_t *lastpaddrp,
517 vm_paddr_t paddr, nextpaddr;
518 bus_dma_segment_t *sg;
523 map = &nobounce_dmamap;
526 * If we are being called during a callback, pagesneeded will
527 * be non-zero, so we can avoid doing the work twice.
529 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) &&
530 map != &nobounce_dmamap && map->pagesneeded == 0) {
531 vm_offset_t vendaddr;
534 * Count the number of bounce pages
535 * needed in order to complete this transfer
537 vaddr = (vm_offset_t)buf;
538 vendaddr = (vm_offset_t)buf + buflen;
540 while (vaddr < vendaddr) {
541 paddr = _bus_dma_extract(pmap, vaddr);
542 if (run_filter(dmat, paddr) != 0)
544 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
548 /* Reserve Necessary Bounce Pages */
549 if (map->pagesneeded != 0) {
550 struct bounce_zone *bz;
552 bz = dmat->bounce_zone;
554 if (flags & BUS_DMA_NOWAIT) {
555 if (reserve_bounce_pages(dmat, map, 0) != 0) {
560 if (reserve_bounce_pages(dmat, map, 1) != 0) {
561 /* Queue us for resources */
564 map->buflen = buflen;
567 &dmat->bounce_zone->bounce_map_waitinglist,
571 return (EINPROGRESS);
577 KKASSERT(*segp >= 1 && *segp <= dmat->nsegments);
579 sg = &dmat->segments[seg - 1];
581 vaddr = (vm_offset_t)buf;
582 nextpaddr = *lastpaddrp;
583 bmask = ~(dmat->boundary - 1); /* note: will be 0 if boundary is 0 */
585 /* force at least one segment */
592 paddr = _bus_dma_extract(pmap, vaddr);
593 size = PAGE_SIZE - (paddr & PAGE_MASK);
596 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
598 * note: this paddr has the same in-page offset
599 * as vaddr and thus the paddr above, so the
600 * size does not have to be recalculated
602 paddr = add_bounce_page(dmat, map, vaddr, size);
606 * Fill in the bus_dma_segment
612 } else if (paddr == nextpaddr) {
617 if (seg > dmat->nsegments)
622 nextpaddr = paddr + size;
625 * Handle maxsegsz and boundary issues with a nested loop
631 * Limit to the boundary and maximum segment size
633 if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
634 tmpsize = dmat->boundary -
635 (sg->ds_addr & ~bmask);
636 if (tmpsize > dmat->maxsegsz)
637 tmpsize = dmat->maxsegsz;
638 KKASSERT(tmpsize < sg->ds_len);
639 } else if (sg->ds_len > dmat->maxsegsz) {
640 tmpsize = dmat->maxsegsz;
646 * Futz, split the data into a new segment.
648 if (seg >= dmat->nsegments)
650 sg[1].ds_len = sg[0].ds_len - tmpsize;
651 sg[1].ds_addr = sg[0].ds_addr + tmpsize;
652 sg[0].ds_len = tmpsize;
662 } while (buflen > 0);
668 *lastpaddrp = nextpaddr;
670 if (error && (dmat->flags & BUS_DMA_COULD_BOUNCE) &&
671 map != &nobounce_dmamap) {
672 _bus_dmamap_unload(dmat, map);
673 return_bounce_pages(dmat, map);
679 * Map the buffer buf into bus space using the dmamap map.
682 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
683 bus_size_t buflen, bus_dmamap_callback_t *callback,
684 void *callback_arg, int flags)
686 vm_paddr_t lastaddr = 0;
687 int error, nsegs = 1;
692 * Follow old semantics. Once all of the callers are fixed,
693 * we should get rid of these internal flag "adjustment".
695 flags &= ~BUS_DMA_NOWAIT;
696 flags |= BUS_DMA_WAITOK;
698 map->callback = callback;
699 map->callback_arg = callback_arg;
702 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen,
703 NULL, flags, &lastaddr, &nsegs, 1);
704 if (error == EINPROGRESS)
707 callback(callback_arg, dmat->segments, nsegs, error);
712 * Like _bus_dmamap_load(), but for mbufs.
715 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
717 bus_dmamap_callback2_t *callback, void *callback_arg,
726 * Follow old semantics. Once all of the callers are fixed,
727 * we should get rid of these internal flag "adjustment".
729 flags &= ~BUS_DMA_WAITOK;
730 flags |= BUS_DMA_NOWAIT;
732 if (m0->m_pkthdr.len <= dmat->maxsize) {
734 vm_paddr_t lastaddr = 0;
739 for (m = m0; m != NULL && error == 0; m = m->m_next) {
743 error = _bus_dmamap_load_buffer(dmat, map,
745 NULL, flags, &lastaddr,
755 /* force "no valid mappings" in callback */
756 callback(callback_arg, dmat->segments, 0, 0, error);
758 callback(callback_arg, dmat->segments, nsegs,
759 m0->m_pkthdr.len, error);
765 * Like _bus_dmamap_load(), but for uios.
768 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
770 bus_dmamap_callback2_t *callback, void *callback_arg,
774 int nsegs, error, first, i;
781 * Follow old semantics. Once all of the callers are fixed,
782 * we should get rid of these internal flag "adjustment".
784 flags &= ~BUS_DMA_WAITOK;
785 flags |= BUS_DMA_NOWAIT;
787 resid = uio->uio_resid;
790 if (uio->uio_segflg == UIO_USERSPACE) {
794 KASSERT(td != NULL && td->td_proc != NULL,
795 ("bus_dmamap_load_uio: USERSPACE but no proc"));
796 pmap = vmspace_pmap(td->td_proc->p_vmspace);
805 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
807 * Now at the first iovec to load. Load each iovec
808 * until we have exhausted the residual count.
811 resid < iov[i].iov_len ? resid : iov[i].iov_len;
812 caddr_t addr = (caddr_t) iov[i].iov_base;
814 error = _bus_dmamap_load_buffer(dmat, map, addr, minlen,
815 pmap, flags, &lastaddr, &nsegs, first);
822 /* force "no valid mappings" in callback */
823 callback(callback_arg, dmat->segments, 0, 0, error);
825 callback(callback_arg, dmat->segments, nsegs,
826 uio->uio_resid, error);
832 * Release the mapping held by map.
835 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
837 struct bounce_page *bpage;
839 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
840 STAILQ_REMOVE_HEAD(&map->bpages, links);
841 free_bounce_page(dmat, bpage);
846 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
848 struct bounce_page *bpage;
850 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
852 * Handle data bouncing. We might also
853 * want to add support for invalidating
854 * the caches on broken hardware
857 case BUS_DMASYNC_PREWRITE:
858 while (bpage != NULL) {
859 bcopy((void *)bpage->datavaddr,
860 (void *)bpage->vaddr,
862 bpage = STAILQ_NEXT(bpage, links);
864 dmat->bounce_zone->total_bounced++;
867 case BUS_DMASYNC_POSTREAD:
868 while (bpage != NULL) {
869 bcopy((void *)bpage->vaddr,
870 (void *)bpage->datavaddr,
872 bpage = STAILQ_NEXT(bpage, links);
874 dmat->bounce_zone->total_bounced++;
877 case BUS_DMASYNC_PREREAD:
878 case BUS_DMASYNC_POSTWRITE:
886 alloc_bounce_zone(bus_dma_tag_t dmat)
888 struct bounce_zone *bz, *new_bz;
891 KASSERT(dmat->bounce_zone == NULL,
892 ("bounce zone was already assigned\n"));
894 new_bz = kmalloc(sizeof(*new_bz), M_DEVBUF, M_INTWAIT | M_ZERO);
896 lwkt_gettoken(&ref, &bounce_zone_tok);
898 /* Check to see if we already have a suitable zone */
899 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
900 if (dmat->alignment <= bz->alignment &&
901 dmat->boundary <= bz->boundary &&
902 dmat->lowaddr >= bz->lowaddr) {
905 dmat->bounce_zone = bz;
906 kfree(new_bz, M_DEVBUF);
913 spin_init(&bz->spin);
915 STAILQ_INIT(&bz->bounce_page_list);
916 STAILQ_INIT(&bz->bounce_map_waitinglist);
918 bz->reserved_bpages = 0;
919 bz->active_bpages = 0;
920 bz->lowaddr = dmat->lowaddr;
921 bz->alignment = dmat->alignment;
922 bz->boundary = dmat->boundary;
923 ksnprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
925 ksnprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
926 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
930 dmat->bounce_zone = bz;
932 sysctl_ctx_init(&bz->sysctl_ctx);
933 bz->sysctl_tree = SYSCTL_ADD_NODE(&bz->sysctl_ctx,
934 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
936 if (bz->sysctl_tree == NULL) {
937 sysctl_ctx_free(&bz->sysctl_ctx);
938 return 0; /* XXX error code? */
941 SYSCTL_ADD_INT(&bz->sysctl_ctx,
942 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
943 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
944 "Total bounce pages");
945 SYSCTL_ADD_INT(&bz->sysctl_ctx,
946 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
947 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
948 "Free bounce pages");
949 SYSCTL_ADD_INT(&bz->sysctl_ctx,
950 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
951 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
952 "Reserved bounce pages");
953 SYSCTL_ADD_INT(&bz->sysctl_ctx,
954 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
955 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
956 "Active bounce pages");
957 SYSCTL_ADD_INT(&bz->sysctl_ctx,
958 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
959 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
960 "Total bounce requests");
961 SYSCTL_ADD_INT(&bz->sysctl_ctx,
962 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
963 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
964 "Total bounce requests that were deferred");
965 SYSCTL_ADD_INT(&bz->sysctl_ctx,
966 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
967 "reserve_failed", CTLFLAG_RD, &bz->reserve_failed, 0,
968 "Total bounce page reservations that were failed");
969 SYSCTL_ADD_STRING(&bz->sysctl_ctx,
970 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
971 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
972 SYSCTL_ADD_INT(&bz->sysctl_ctx,
973 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
974 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
975 SYSCTL_ADD_INT(&bz->sysctl_ctx,
976 SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
977 "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
983 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
985 struct bounce_zone *bz = dmat->bounce_zone;
988 while (numpages > 0) {
989 struct bounce_page *bpage;
991 bpage = kmalloc(sizeof(*bpage), M_DEVBUF, M_INTWAIT | M_ZERO);
993 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
998 if (bpage->vaddr == 0) {
999 kfree(bpage, M_DEVBUF);
1002 bpage->busaddr = pmap_kextract(bpage->vaddr);
1005 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1006 total_bounce_pages++;
1017 /* Assume caller holds bounce zone spinlock */
1019 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1021 struct bounce_zone *bz = dmat->bounce_zone;
1024 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1025 if (!commit && map->pagesneeded > (map->pagesreserved + pages)) {
1026 bz->reserve_failed++;
1027 return (map->pagesneeded - (map->pagesreserved + pages));
1030 bz->free_bpages -= pages;
1032 bz->reserved_bpages += pages;
1033 KKASSERT(bz->reserved_bpages <= bz->total_bpages);
1035 map->pagesreserved += pages;
1036 pages = map->pagesneeded - map->pagesreserved;
1042 return_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
1044 struct bounce_zone *bz = dmat->bounce_zone;
1045 int reserved = map->pagesreserved;
1046 bus_dmamap_t wait_map;
1048 map->pagesreserved = 0;
1049 map->pagesneeded = 0;
1056 bz->free_bpages += reserved;
1057 KKASSERT(bz->free_bpages <= bz->total_bpages);
1059 KKASSERT(bz->reserved_bpages >= reserved);
1060 bz->reserved_bpages -= reserved;
1062 wait_map = get_map_waiting(dmat);
1066 if (wait_map != NULL)
1067 add_map_callback(map);
1071 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1074 struct bounce_zone *bz = dmat->bounce_zone;
1075 struct bounce_page *bpage;
1077 KASSERT(map->pagesneeded > 0, ("map doesn't need any pages"));
1080 KASSERT(map->pagesreserved > 0, ("map doesn't reserve any pages"));
1081 map->pagesreserved--;
1085 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1086 KASSERT(bpage != NULL, ("free page list is empty"));
1087 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1089 KKASSERT(bz->reserved_bpages > 0);
1090 bz->reserved_bpages--;
1092 bz->active_bpages++;
1093 KKASSERT(bz->active_bpages <= bz->total_bpages);
1097 bpage->datavaddr = vaddr;
1098 bpage->datacount = size;
1099 STAILQ_INSERT_TAIL(&map->bpages, bpage, links);
1100 return bpage->busaddr;
1104 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1106 struct bounce_zone *bz = dmat->bounce_zone;
1109 bpage->datavaddr = 0;
1110 bpage->datacount = 0;
1114 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1117 KKASSERT(bz->free_bpages <= bz->total_bpages);
1119 KKASSERT(bz->active_bpages > 0);
1120 bz->active_bpages--;
1122 map = get_map_waiting(dmat);
1127 add_map_callback(map);
1130 /* Assume caller holds bounce zone spinlock */
1132 get_map_waiting(bus_dma_tag_t dmat)
1134 struct bounce_zone *bz = dmat->bounce_zone;
1137 map = STAILQ_FIRST(&bz->bounce_map_waitinglist);
1139 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1140 STAILQ_REMOVE_HEAD(&bz->bounce_map_waitinglist, links);
1141 bz->total_deferred++;
1150 add_map_callback(bus_dmamap_t map)
1152 /* XXX callbacklist is not MPSAFE */
1155 STAILQ_INSERT_TAIL(&bounce_map_callbacklist, map, links);
1156 busdma_swi_pending = 1;
1168 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1169 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1171 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1172 map->callback, map->callback_arg, /*flags*/0);