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/i386/i386/Attic/busdma_machdep.c,v 1.9 2004/04/19 13:37:43 joerg 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 = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
145 newtag->parent = parent;
146 newtag->alignment = alignment;
147 newtag->boundary = boundary;
148 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
149 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
150 newtag->filter = filter;
151 newtag->filterarg = filterarg;
152 newtag->maxsize = maxsize;
153 newtag->nsegments = nsegments;
154 newtag->maxsegsz = maxsegsz;
155 newtag->flags = flags;
156 newtag->ref_count = 1; /* Count ourself */
157 newtag->map_count = 0;
158 newtag->segments = malloc(sizeof(bus_dma_segment_t) * newtag->nsegments,
159 M_DEVBUF, M_INTWAIT);
160 if (newtag->segments == NULL) {
161 free(newtag, M_DEVBUF);
165 /* Take into account any restrictions imposed by our parent tag */
166 if (parent != NULL) {
167 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
168 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
170 * XXX Not really correct??? Probably need to honor boundary
171 * all the way up the inheritence chain.
173 newtag->boundary = MAX(parent->boundary, newtag->boundary);
174 if (newtag->filter == NULL) {
176 * Short circuit looking at our parent directly
177 * since we have encapsulated all of its information
179 newtag->filter = parent->filter;
180 newtag->filterarg = parent->filterarg;
181 newtag->parent = parent->parent;
183 if (newtag->parent != NULL) {
188 if (newtag->lowaddr < ptoa(Maxmem) &&
189 (flags & BUS_DMA_ALLOCNOW) != 0) {
192 if (lowaddr > bounce_lowaddr) {
194 * Go through the pool and kill any pages
195 * that don't reside below lowaddr.
197 panic("bus_dma_tag_create: page reallocation "
200 if (ptoa(total_bpages) < maxsize) {
203 pages = atop(maxsize) - total_bpages;
205 /* Add pages to our bounce pool */
206 if (alloc_bounce_pages(newtag, pages) < pages)
209 /* Performed initial allocation */
210 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
214 free(newtag, M_DEVBUF);
222 bus_dma_tag_destroy(bus_dma_tag_t dmat)
226 if (dmat->map_count != 0)
229 while (dmat != NULL) {
230 bus_dma_tag_t parent;
232 parent = dmat->parent;
234 if (dmat->ref_count == 0) {
235 if (dmat->segments != NULL)
236 free(dmat->segments, M_DEVBUF);
237 free(dmat, M_DEVBUF);
239 * Last reference count, so
240 * release our reference
241 * count on our parent.
252 * Allocate a handle for mapping from kva/uva/physical
253 * address space into bus device space.
256 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
262 if (dmat->lowaddr < ptoa(Maxmem)) {
266 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
271 /* Initialize the new map */
272 bzero(*mapp, sizeof(**mapp));
273 STAILQ_INIT(&((*mapp)->bpages));
276 * Attempt to add pages to our pool on a per-instance
277 * basis up to a sane limit.
279 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
280 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
281 || (dmat->map_count > 0
282 && total_bpages < maxpages)) {
285 if (dmat->lowaddr > bounce_lowaddr) {
287 * Go through the pool and kill any pages
288 * that don't reside below lowaddr.
290 panic("bus_dmamap_create: page reallocation "
293 pages = atop(dmat->maxsize);
294 pages = MIN(maxpages - total_bpages, pages);
295 error = alloc_bounce_pages(dmat, pages);
297 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
299 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
313 * Destroy a handle for mapping from kva/uva/physical
314 * address space into bus device space.
317 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
320 if (STAILQ_FIRST(&map->bpages) != NULL)
330 * Allocate a piece of memory that can be efficiently mapped into
331 * bus device space based on the constraints lited in the dma tag.
332 * A dmamap to for use with dmamap_load is also allocated.
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 (flags & BUS_DMA_NOWAIT)
346 if (flags & BUS_DMA_ZERO)
349 if ((dmat->maxsize <= PAGE_SIZE) &&
350 dmat->lowaddr >= ptoa(Maxmem)) {
351 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
353 * XXX Check wether the allocation crossed a page boundary
354 * and retry with power-of-2 alignment in that case.
356 if ((((intptr_t)*vaddr) & PAGE_MASK) !=
357 (((intptr_t)*vaddr + dmat->maxsize) & PAGE_MASK)) {
359 free(*vaddr, M_DEVBUF);
360 /* XXX check for overflow? */
361 for (size = 1; size <= dmat->maxsize; size <<= 1)
363 *vaddr = malloc(size, M_DEVBUF, mflags);
367 * XXX Use Contigmalloc until it is merged into this facility
368 * and handles multi-seg allocations. Nobody is doing
369 * multi-seg allocations yet though.
371 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
372 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
381 * Free a piece of memory and it's allociated dmamap, that was allocated
382 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
385 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
388 * dmamem does not need to be bounced, so the map should be
392 panic("bus_dmamem_free: Invalid map freed\n");
393 if ((dmat->maxsize <= PAGE_SIZE) &&
394 dmat->lowaddr >= ptoa(Maxmem))
395 free(vaddr, M_DEVBUF);
397 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
400 #define BUS_DMAMAP_NSEGS ((BUS_SPACE_MAXSIZE / PAGE_SIZE) + 1)
403 * Map the buffer buf into bus space using the dmamap map.
406 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
407 bus_size_t buflen, bus_dmamap_callback_t *callback,
408 void *callback_arg, int flags)
412 bus_dma_segment_t *sg;
415 vm_paddr_t nextpaddr;
418 map = &nobounce_dmamap;
422 * If we are being called during a callback, pagesneeded will
423 * be non-zero, so we can avoid doing the work twice.
425 if (dmat->lowaddr < ptoa(Maxmem) &&
426 map->pagesneeded == 0) {
427 vm_offset_t vendaddr;
430 * Count the number of bounce pages
431 * needed in order to complete this transfer
433 vaddr = trunc_page((vm_offset_t)buf);
434 vendaddr = (vm_offset_t)buf + buflen;
436 while (vaddr < vendaddr) {
437 paddr = pmap_kextract(vaddr);
438 if (run_filter(dmat, paddr) != 0) {
446 /* Reserve Necessary Bounce Pages */
447 if (map->pagesneeded != 0) {
451 if (reserve_bounce_pages(dmat, map) != 0) {
453 /* Queue us for resources */
456 map->buflen = buflen;
457 map->callback = callback;
458 map->callback_arg = callback_arg;
460 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
463 return (EINPROGRESS);
468 vaddr = (vm_offset_t)buf;
477 paddr = pmap_kextract(vaddr);
478 size = PAGE_SIZE - (paddr & PAGE_MASK);
482 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
483 paddr = add_bounce_page(dmat, map, vaddr, size);
486 if (sg->ds_len == 0) {
489 } else if (paddr == nextpaddr) {
492 /* Go to the next segment */
495 if (seg > dmat->nsegments)
501 nextpaddr = paddr + size;
503 } while (buflen > 0);
506 printf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
511 (*callback)(callback_arg, dmat->segments, seg, error);
517 * Utility function to load a linear buffer. lastaddrp holds state
518 * between invocations (for multiple-buffer loads). segp contains
519 * the starting segment on entrace, and the ending segment on exit.
520 * first indicates if this is the first invocation of this function.
523 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
524 void *buf, bus_size_t buflen,
527 vm_offset_t *lastaddrp,
531 bus_dma_segment_t *segs;
533 bus_addr_t curaddr, lastaddr, baddr, bmask;
534 vm_offset_t vaddr = (vm_offset_t)buf;
538 if (td->td_proc != NULL)
539 pmap = vmspace_pmap(td->td_proc->p_vmspace);
543 segs = dmat->segments;
544 lastaddr = *lastaddrp;
545 bmask = ~(dmat->boundary - 1);
547 for (seg = *segp; buflen > 0 ; ) {
549 * Get the physical address for this segment.
552 curaddr = pmap_extract(pmap, vaddr);
554 curaddr = pmap_kextract(vaddr);
557 * Compute the segment size, and adjust counts.
559 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
564 * Make sure we don't cross any boundaries.
566 if (dmat->boundary > 0) {
567 baddr = (curaddr + dmat->boundary) & bmask;
568 if (sgsize > (baddr - curaddr))
569 sgsize = (baddr - curaddr);
573 * Insert chunk into a segment, coalescing with
574 * previous segment if possible.
577 segs[seg].ds_addr = curaddr;
578 segs[seg].ds_len = sgsize;
581 if (curaddr == lastaddr &&
582 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
583 (dmat->boundary == 0 ||
584 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
585 segs[seg].ds_len += sgsize;
587 if (++seg >= dmat->nsegments)
589 segs[seg].ds_addr = curaddr;
590 segs[seg].ds_len = sgsize;
594 lastaddr = curaddr + sgsize;
600 *lastaddrp = lastaddr;
605 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
609 * Like _bus_dmamap_load(), but for mbufs.
612 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
614 bus_dmamap_callback2_t *callback, void *callback_arg,
619 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
620 ("bus_dmamap_load_mbuf: No support for bounce pages!"));
621 KASSERT(m0->m_flags & M_PKTHDR,
622 ("bus_dmamap_load_mbuf: no packet header"));
626 if (m0->m_pkthdr.len <= dmat->maxsize) {
628 vm_offset_t lastaddr = 0;
631 for (m = m0; m != NULL && error == 0; m = m->m_next) {
632 error = _bus_dmamap_load_buffer(dmat,
634 curthread, flags, &lastaddr,
643 /* force "no valid mappings" in callback */
644 (*callback)(callback_arg, dmat->segments, 0, 0, error);
646 (*callback)(callback_arg, dmat->segments,
647 nsegs+1, m0->m_pkthdr.len, error);
653 * Like _bus_dmamap_load(), but for uios.
656 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
658 bus_dmamap_callback2_t *callback, void *callback_arg,
661 vm_offset_t lastaddr;
662 int nsegs, error, first, i;
665 struct thread *td = NULL;
667 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
668 ("bus_dmamap_load_uio: No support for bounce pages!"));
670 resid = uio->uio_resid;
673 if (uio->uio_segflg == UIO_USERSPACE) {
675 KASSERT(td != NULL && td->td_proc != NULL,
676 ("bus_dmamap_load_uio: USERSPACE but no proc"));
682 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
684 * Now at the first iovec to load. Load each iovec
685 * until we have exhausted the residual count.
688 resid < iov[i].iov_len ? resid : iov[i].iov_len;
689 caddr_t addr = (caddr_t) iov[i].iov_base;
691 error = _bus_dmamap_load_buffer(dmat,
693 td, flags, &lastaddr, &nsegs, first);
700 /* force "no valid mappings" in callback */
701 (*callback)(callback_arg, dmat->segments, 0, 0, error);
703 (*callback)(callback_arg, dmat->segments,
704 nsegs+1, uio->uio_resid, error);
710 * Release the mapping held by map.
713 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
715 struct bounce_page *bpage;
717 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
718 STAILQ_REMOVE_HEAD(&map->bpages, links);
719 free_bounce_page(dmat, bpage);
724 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
726 struct bounce_page *bpage;
728 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
731 * Handle data bouncing. We might also
732 * want to add support for invalidating
733 * the caches on broken hardware
736 case BUS_DMASYNC_PREWRITE:
737 while (bpage != NULL) {
738 bcopy((void *)bpage->datavaddr,
739 (void *)bpage->vaddr,
741 bpage = STAILQ_NEXT(bpage, links);
745 case BUS_DMASYNC_POSTREAD:
746 while (bpage != NULL) {
747 bcopy((void *)bpage->vaddr,
748 (void *)bpage->datavaddr,
750 bpage = STAILQ_NEXT(bpage, links);
753 case BUS_DMASYNC_PREREAD:
754 case BUS_DMASYNC_POSTWRITE:
762 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
767 if (total_bpages == 0) {
768 STAILQ_INIT(&bounce_page_list);
769 STAILQ_INIT(&bounce_map_waitinglist);
770 STAILQ_INIT(&bounce_map_callbacklist);
773 while (numpages > 0) {
774 struct bounce_page *bpage;
777 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
782 bzero(bpage, sizeof(*bpage));
783 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
788 if (bpage->vaddr == NULL) {
789 free(bpage, M_DEVBUF);
792 bpage->busaddr = pmap_kextract(bpage->vaddr);
794 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
805 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
809 pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
810 free_bpages -= pages;
811 reserved_bpages += pages;
812 map->pagesreserved += pages;
813 pages = map->pagesneeded - map->pagesreserved;
819 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
823 struct bounce_page *bpage;
825 if (map->pagesneeded == 0)
826 panic("add_bounce_page: map doesn't need any pages");
829 if (map->pagesreserved == 0)
830 panic("add_bounce_page: map doesn't need any pages");
831 map->pagesreserved--;
834 bpage = STAILQ_FIRST(&bounce_page_list);
836 panic("add_bounce_page: free page list is empty");
838 STAILQ_REMOVE_HEAD(&bounce_page_list, links);
843 bpage->datavaddr = vaddr;
844 bpage->datacount = size;
845 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
846 return (bpage->busaddr);
850 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
853 struct bus_dmamap *map;
855 bpage->datavaddr = 0;
856 bpage->datacount = 0;
859 STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
862 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
863 if (reserve_bounce_pages(map->dmat, map) == 0) {
864 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
865 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
867 busdma_swi_pending = 1;
878 struct bus_dmamap *map;
881 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
882 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
884 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
885 map->callback, map->callback_arg, /*flags*/0);