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.11 2004/10/26 04:22:32 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
37 #include <vm/vm_page.h>
39 /* XXX needed for to access pmap to convert per-proc virtual to physical */
42 #include <vm/vm_map.h>
44 #include <machine/bus.h>
45 #include <machine/md_var.h>
47 #define MAX_BPAGES 128
55 bus_dma_filter_t *filter;
63 bus_dma_segment_t *segments;
67 vm_offset_t vaddr; /* kva of bounce buffer */
68 bus_addr_t busaddr; /* Physical address */
69 vm_offset_t datavaddr; /* kva of client data */
70 bus_size_t datacount; /* client data count */
71 STAILQ_ENTRY(bounce_page) links;
74 int busdma_swi_pending;
76 static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
77 static int free_bpages;
78 static int reserved_bpages;
79 static int active_bpages;
80 static int total_bpages;
81 static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
84 struct bp_list bpages;
88 void *buf; /* unmapped buffer pointer */
89 bus_size_t buflen; /* unmapped buffer length */
90 bus_dmamap_callback_t *callback;
92 STAILQ_ENTRY(bus_dmamap) links;
95 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
96 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
97 static struct bus_dmamap nobounce_dmamap;
99 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
100 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map);
101 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
102 vm_offset_t vaddr, bus_size_t size);
103 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
104 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
107 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
113 if (paddr > dmat->lowaddr
114 && paddr <= dmat->highaddr
115 && (dmat->filter == NULL
116 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
120 } while (retval == 0 && dmat != NULL);
124 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
126 * Allocate a device specific dma_tag.
129 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
130 bus_size_t boundary, bus_addr_t lowaddr,
131 bus_addr_t highaddr, bus_dma_filter_t *filter,
132 void *filterarg, bus_size_t maxsize, int nsegments,
133 bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
135 bus_dma_tag_t newtag;
138 /* Return a NULL tag on failure */
141 newtag = malloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT);
143 newtag->parent = parent;
144 newtag->alignment = alignment;
145 newtag->boundary = boundary;
146 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
147 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
148 newtag->filter = filter;
149 newtag->filterarg = filterarg;
150 newtag->maxsize = maxsize;
151 newtag->nsegments = nsegments;
152 newtag->maxsegsz = maxsegsz;
153 newtag->flags = flags;
154 newtag->ref_count = 1; /* Count ourself */
155 newtag->map_count = 0;
156 newtag->segments = NULL;
158 /* Take into account any restrictions imposed by our parent tag */
159 if (parent != NULL) {
160 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
161 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
163 * XXX Not really correct??? Probably need to honor boundary
164 * all the way up the inheritence chain.
166 newtag->boundary = MAX(parent->boundary, newtag->boundary);
167 if (newtag->filter == NULL) {
169 * Short circuit looking at our parent directly
170 * since we have encapsulated all of its information
172 newtag->filter = parent->filter;
173 newtag->filterarg = parent->filterarg;
174 newtag->parent = parent->parent;
176 if (newtag->parent != NULL) {
181 if (newtag->lowaddr < ptoa(Maxmem) &&
182 (flags & BUS_DMA_ALLOCNOW) != 0) {
185 if (lowaddr > bounce_lowaddr) {
187 * Go through the pool and kill any pages
188 * that don't reside below lowaddr.
190 panic("bus_dma_tag_create: page reallocation "
193 if (ptoa(total_bpages) < maxsize) {
196 pages = atop(maxsize) - total_bpages;
198 /* Add pages to our bounce pool */
199 if (alloc_bounce_pages(newtag, pages) < pages)
202 /* Performed initial allocation */
203 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
207 free(newtag, M_DEVBUF);
215 bus_dma_tag_destroy(bus_dma_tag_t dmat)
219 if (dmat->map_count != 0)
222 while (dmat != NULL) {
223 bus_dma_tag_t parent;
225 parent = dmat->parent;
227 if (dmat->ref_count == 0) {
228 if (dmat->segments != NULL)
229 free(dmat->segments, M_DEVBUF);
230 free(dmat, M_DEVBUF);
232 * Last reference count, so
233 * release our reference
234 * count on our parent.
245 * Allocate a handle for mapping from kva/uva/physical
246 * address space into bus device space.
249 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
255 if (dmat->segments == NULL) {
256 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
257 dmat->segments = malloc(sizeof(bus_dma_segment_t) *
258 dmat->nsegments, M_DEVBUF, M_INTWAIT);
261 if (dmat->lowaddr < ptoa(Maxmem)) {
265 *mapp = malloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT);
269 /* Initialize the new map */
270 bzero(*mapp, sizeof(**mapp));
271 STAILQ_INIT(&((*mapp)->bpages));
274 * Attempt to add pages to our pool on a per-instance
275 * basis up to a sane limit.
277 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
278 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
279 || (dmat->map_count > 0
280 && total_bpages < maxpages)) {
283 if (dmat->lowaddr > bounce_lowaddr) {
285 * Go through the pool and kill any pages
286 * that don't reside below lowaddr.
288 panic("bus_dmamap_create: page reallocation "
291 pages = atop(dmat->maxsize);
292 pages = MIN(maxpages - total_bpages, pages);
293 error = alloc_bounce_pages(dmat, pages);
295 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
297 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
311 * Destroy a handle for mapping from kva/uva/physical
312 * address space into bus device space.
315 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
318 if (STAILQ_FIRST(&map->bpages) != NULL)
328 * Allocate a piece of memory that can be efficiently mapped into
329 * bus device space based on the constraints lited in the dma tag.
331 * mapp is degenerate. By definition this allocation should not require
332 * bounce buffers so do not allocate a dma map.
335 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
339 /* If we succeed, no mapping/bouncing will be required */
342 if (dmat->segments == NULL) {
343 KKASSERT(dmat->nsegments < 16384);
344 dmat->segments = malloc(sizeof(bus_dma_segment_t) *
345 dmat->nsegments, M_DEVBUF, M_INTWAIT);
348 if (flags & BUS_DMA_NOWAIT)
352 if (flags & BUS_DMA_ZERO)
355 if ((dmat->maxsize <= PAGE_SIZE) &&
356 dmat->lowaddr >= ptoa(Maxmem)) {
357 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
359 * XXX Check whether the allocation crossed a page boundary
360 * and retry with power-of-2 alignment in that case.
362 if ((((intptr_t)*vaddr) & PAGE_MASK) !=
363 (((intptr_t)*vaddr + dmat->maxsize) & PAGE_MASK)) {
365 free(*vaddr, M_DEVBUF);
366 /* XXX check for overflow? */
367 for (size = 1; size <= dmat->maxsize; size <<= 1)
369 *vaddr = malloc(size, M_DEVBUF, mflags);
373 * XXX Use Contigmalloc until it is merged into this facility
374 * and handles multi-seg allocations. Nobody is doing
375 * multi-seg allocations yet though.
377 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
378 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
387 * Free a piece of memory and it's allociated dmamap, that was allocated
388 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
391 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
394 * dmamem does not need to be bounced, so the map should be
398 panic("bus_dmamem_free: Invalid map freed\n");
399 if ((dmat->maxsize <= PAGE_SIZE) &&
400 dmat->lowaddr >= ptoa(Maxmem))
401 free(vaddr, M_DEVBUF);
403 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
406 #define BUS_DMAMAP_NSEGS ((BUS_SPACE_MAXSIZE / PAGE_SIZE) + 1)
409 * Map the buffer buf into bus space using the dmamap map.
412 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
413 bus_size_t buflen, bus_dmamap_callback_t *callback,
414 void *callback_arg, int flags)
418 bus_dma_segment_t *sg;
421 vm_paddr_t nextpaddr;
424 map = &nobounce_dmamap;
428 * If we are being called during a callback, pagesneeded will
429 * be non-zero, so we can avoid doing the work twice.
431 if (dmat->lowaddr < ptoa(Maxmem) &&
432 map->pagesneeded == 0) {
433 vm_offset_t vendaddr;
436 * Count the number of bounce pages
437 * needed in order to complete this transfer
439 vaddr = trunc_page((vm_offset_t)buf);
440 vendaddr = (vm_offset_t)buf + buflen;
442 while (vaddr < vendaddr) {
443 paddr = pmap_kextract(vaddr);
444 if (run_filter(dmat, paddr) != 0) {
452 /* Reserve Necessary Bounce Pages */
453 if (map->pagesneeded != 0) {
457 if (reserve_bounce_pages(dmat, map) != 0) {
459 /* Queue us for resources */
462 map->buflen = buflen;
463 map->callback = callback;
464 map->callback_arg = callback_arg;
466 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
469 return (EINPROGRESS);
474 vaddr = (vm_offset_t)buf;
483 paddr = pmap_kextract(vaddr);
484 size = PAGE_SIZE - (paddr & PAGE_MASK);
488 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
489 paddr = add_bounce_page(dmat, map, vaddr, size);
492 if (sg->ds_len == 0) {
495 } else if (paddr == nextpaddr) {
498 /* Go to the next segment */
501 if (seg > dmat->nsegments)
507 nextpaddr = paddr + size;
509 } while (buflen > 0);
512 printf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
517 (*callback)(callback_arg, dmat->segments, seg, error);
523 * Utility function to load a linear buffer. lastaddrp holds state
524 * between invocations (for multiple-buffer loads). segp contains
525 * the starting segment on entrace, and the ending segment on exit.
526 * first indicates if this is the first invocation of this function.
529 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
530 void *buf, bus_size_t buflen,
533 vm_offset_t *lastaddrp,
537 bus_dma_segment_t *segs;
539 bus_addr_t curaddr, lastaddr, baddr, bmask;
540 vm_offset_t vaddr = (vm_offset_t)buf;
544 if (td->td_proc != NULL)
545 pmap = vmspace_pmap(td->td_proc->p_vmspace);
549 segs = dmat->segments;
550 lastaddr = *lastaddrp;
551 bmask = ~(dmat->boundary - 1);
553 for (seg = *segp; buflen > 0 ; ) {
555 * Get the physical address for this segment.
558 curaddr = pmap_extract(pmap, vaddr);
560 curaddr = pmap_kextract(vaddr);
563 * Compute the segment size, and adjust counts.
565 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
570 * Make sure we don't cross any boundaries.
572 if (dmat->boundary > 0) {
573 baddr = (curaddr + dmat->boundary) & bmask;
574 if (sgsize > (baddr - curaddr))
575 sgsize = (baddr - curaddr);
579 * Insert chunk into a segment, coalescing with
580 * previous segment if possible.
583 segs[seg].ds_addr = curaddr;
584 segs[seg].ds_len = sgsize;
587 if (curaddr == lastaddr &&
588 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
589 (dmat->boundary == 0 ||
590 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
591 segs[seg].ds_len += sgsize;
593 if (++seg >= dmat->nsegments)
595 segs[seg].ds_addr = curaddr;
596 segs[seg].ds_len = sgsize;
600 lastaddr = curaddr + sgsize;
606 *lastaddrp = lastaddr;
611 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
615 * Like _bus_dmamap_load(), but for mbufs.
618 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
620 bus_dmamap_callback2_t *callback, void *callback_arg,
625 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
626 ("bus_dmamap_load_mbuf: No support for bounce pages!"));
627 KASSERT(m0->m_flags & M_PKTHDR,
628 ("bus_dmamap_load_mbuf: no packet header"));
632 if (m0->m_pkthdr.len <= dmat->maxsize) {
634 vm_offset_t lastaddr = 0;
637 for (m = m0; m != NULL && error == 0; m = m->m_next) {
638 error = _bus_dmamap_load_buffer(dmat,
640 curthread, flags, &lastaddr,
649 /* force "no valid mappings" in callback */
650 (*callback)(callback_arg, dmat->segments, 0, 0, error);
652 (*callback)(callback_arg, dmat->segments,
653 nsegs+1, m0->m_pkthdr.len, error);
659 * Like _bus_dmamap_load(), but for uios.
662 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
664 bus_dmamap_callback2_t *callback, void *callback_arg,
667 vm_offset_t lastaddr;
668 int nsegs, error, first, i;
671 struct thread *td = NULL;
673 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
674 ("bus_dmamap_load_uio: No support for bounce pages!"));
676 resid = uio->uio_resid;
679 if (uio->uio_segflg == UIO_USERSPACE) {
681 KASSERT(td != NULL && td->td_proc != NULL,
682 ("bus_dmamap_load_uio: USERSPACE but no proc"));
688 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
690 * Now at the first iovec to load. Load each iovec
691 * until we have exhausted the residual count.
694 resid < iov[i].iov_len ? resid : iov[i].iov_len;
695 caddr_t addr = (caddr_t) iov[i].iov_base;
697 error = _bus_dmamap_load_buffer(dmat,
699 td, flags, &lastaddr, &nsegs, first);
706 /* force "no valid mappings" in callback */
707 (*callback)(callback_arg, dmat->segments, 0, 0, error);
709 (*callback)(callback_arg, dmat->segments,
710 nsegs+1, uio->uio_resid, error);
716 * Release the mapping held by map.
719 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
721 struct bounce_page *bpage;
723 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
724 STAILQ_REMOVE_HEAD(&map->bpages, links);
725 free_bounce_page(dmat, bpage);
730 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
732 struct bounce_page *bpage;
734 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
737 * Handle data bouncing. We might also
738 * want to add support for invalidating
739 * the caches on broken hardware
742 case BUS_DMASYNC_PREWRITE:
743 while (bpage != NULL) {
744 bcopy((void *)bpage->datavaddr,
745 (void *)bpage->vaddr,
747 bpage = STAILQ_NEXT(bpage, links);
751 case BUS_DMASYNC_POSTREAD:
752 while (bpage != NULL) {
753 bcopy((void *)bpage->vaddr,
754 (void *)bpage->datavaddr,
756 bpage = STAILQ_NEXT(bpage, links);
759 case BUS_DMASYNC_PREREAD:
760 case BUS_DMASYNC_POSTWRITE:
768 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
773 if (total_bpages == 0) {
774 STAILQ_INIT(&bounce_page_list);
775 STAILQ_INIT(&bounce_map_waitinglist);
776 STAILQ_INIT(&bounce_map_callbacklist);
779 while (numpages > 0) {
780 struct bounce_page *bpage;
783 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
788 bzero(bpage, sizeof(*bpage));
789 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
794 if (bpage->vaddr == NULL) {
795 free(bpage, M_DEVBUF);
798 bpage->busaddr = pmap_kextract(bpage->vaddr);
800 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
811 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
815 pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
816 free_bpages -= pages;
817 reserved_bpages += pages;
818 map->pagesreserved += pages;
819 pages = map->pagesneeded - map->pagesreserved;
825 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
829 struct bounce_page *bpage;
831 if (map->pagesneeded == 0)
832 panic("add_bounce_page: map doesn't need any pages");
835 if (map->pagesreserved == 0)
836 panic("add_bounce_page: map doesn't need any pages");
837 map->pagesreserved--;
840 bpage = STAILQ_FIRST(&bounce_page_list);
842 panic("add_bounce_page: free page list is empty");
844 STAILQ_REMOVE_HEAD(&bounce_page_list, links);
849 bpage->datavaddr = vaddr;
850 bpage->datacount = size;
851 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
852 return (bpage->busaddr);
856 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
859 struct bus_dmamap *map;
861 bpage->datavaddr = 0;
862 bpage->datacount = 0;
865 STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
868 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
869 if (reserve_bounce_pages(map->dmat, map) == 0) {
870 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
871 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
873 busdma_swi_pending = 1;
884 struct bus_dmamap *map;
887 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
888 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
890 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
891 map->callback, map->callback_arg, /*flags*/0);