2 * Copyright (c) 2004 Olivier Houchard
3 * Copyright (c) 2002 Peter Grehan
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * From i386/busdma_machdep.c,v 1.26 2002/04/19 22:58:09 alfred
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * MacPPC bus dma support routines
38 #define _ARM32_BUS_DMA_PRIVATE
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/interrupt.h>
46 #include <sys/mutex.h>
50 #include <sys/kernel.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_map.h>
56 #include <machine/atomic.h>
57 #include <machine/bus.h>
58 #include <machine/cpufunc.h>
66 bus_dma_filter_t *filter;
74 bus_dma_lock_t *lockfunc;
77 * DMA range for this tag. If the page doesn't fall within
78 * one of these ranges, an error is returned. The caller
79 * may then decide what to do with the transfer. If the
80 * range pointer is NULL, it is ignored.
82 struct arm32_dma_range *ranges;
86 #define DMAMAP_LINEAR 0x1
87 #define DMAMAP_MBUF 0x2
88 #define DMAMAP_UIO 0x4
89 #define DMAMAP_ALLOCATED 0x10
90 #define DMAMAP_TYPE_MASK (DMAMAP_LINEAR|DMAMAP_MBUF|DMAMAP_UIO)
91 #define DMAMAP_COHERENT 0x8
96 TAILQ_ENTRY(bus_dmamap) freelist;
100 static TAILQ_HEAD(,bus_dmamap) dmamap_freelist =
101 TAILQ_HEAD_INITIALIZER(dmamap_freelist);
103 #define BUSDMA_STATIC_MAPS 500
104 static struct bus_dmamap map_pool[BUSDMA_STATIC_MAPS];
106 static struct mtx busdma_mtx;
108 MTX_SYSINIT(busdma_mtx, &busdma_mtx, "busdma lock", MTX_DEF);
111 arm_dmamap_freelist_init(void *dummy)
115 for (i = 0; i < BUSDMA_STATIC_MAPS; i++)
116 TAILQ_INSERT_HEAD(&dmamap_freelist, &map_pool[i], freelist);
119 SYSINIT(busdma, SI_SUB_VM, SI_ORDER_ANY, arm_dmamap_freelist_init, NULL);
122 * Check to see if the specified page is in an allowed DMA range.
126 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
127 bus_dmamap_t map, void *buf, bus_size_t buflen, struct pmap *pmap,
128 int flags, vm_offset_t *lastaddrp, int *segp);
130 static __inline struct arm32_dma_range *
131 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
134 struct arm32_dma_range *dr;
137 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
138 if (curaddr >= dr->dr_sysbase &&
139 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
146 * Convenience function for manipulating driver locks from busdma (during
147 * busdma_swi, for example). Drivers that don't provide their own locks
148 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
149 * non-mutex locking scheme don't have to use this at all.
152 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
156 dmtx = (struct mtx *)arg;
165 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
170 * dflt_lock should never get called. It gets put into the dma tag when
171 * lockfunc == NULL, which is only valid if the maps that are associated
172 * with the tag are meant to never be defered.
173 * XXX Should have a way to identify which driver is responsible here.
176 dflt_lock(void *arg, bus_dma_lock_op_t op)
179 panic("driver error: busdma dflt_lock called");
181 printf("DRIVER_ERROR: busdma dflt_lock called\n");
185 static __inline bus_dmamap_t
186 _busdma_alloc_dmamap(void)
190 mtx_lock(&busdma_mtx);
191 map = TAILQ_FIRST(&dmamap_freelist);
193 TAILQ_REMOVE(&dmamap_freelist, map, freelist);
194 mtx_unlock(&busdma_mtx);
196 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
198 map->flags = DMAMAP_ALLOCATED;
205 _busdma_free_dmamap(bus_dmamap_t map)
207 if (map->flags & DMAMAP_ALLOCATED)
210 mtx_lock(&busdma_mtx);
211 TAILQ_INSERT_HEAD(&dmamap_freelist, map, freelist);
212 mtx_unlock(&busdma_mtx);
217 * Allocate a device specific dma_tag.
222 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
223 bus_size_t boundary, bus_addr_t lowaddr,
224 bus_addr_t highaddr, bus_dma_filter_t *filter,
225 void *filterarg, bus_size_t maxsize, int nsegments,
226 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
227 void *lockfuncarg, bus_dma_tag_t *dmat)
229 bus_dma_tag_t newtag;
231 /* Return a NULL tag on failure */
234 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
235 if (newtag == NULL) {
236 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
237 __func__, newtag, 0, error);
241 newtag->parent = parent;
242 newtag->alignment = alignment;
243 newtag->boundary = boundary;
244 newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
245 newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
246 newtag->filter = filter;
247 newtag->filterarg = filterarg;
248 newtag->maxsize = maxsize;
249 newtag->nsegments = nsegments;
250 newtag->maxsegsz = maxsegsz;
251 newtag->flags = flags;
252 newtag->ref_count = 1; /* Count ourself */
253 newtag->map_count = 0;
254 newtag->ranges = bus_dma_get_range();
255 newtag->_nranges = bus_dma_get_range_nb();
256 if (lockfunc != NULL) {
257 newtag->lockfunc = lockfunc;
258 newtag->lockfuncarg = lockfuncarg;
260 newtag->lockfunc = dflt_lock;
261 newtag->lockfuncarg = NULL;
264 * Take into account any restrictions imposed by our parent tag
266 if (parent != NULL) {
267 newtag->lowaddr = min(parent->lowaddr, newtag->lowaddr);
268 newtag->highaddr = max(parent->highaddr, newtag->highaddr);
269 if (newtag->boundary == 0)
270 newtag->boundary = parent->boundary;
271 else if (parent->boundary != 0)
272 newtag->boundary = min(parent->boundary,
274 if (newtag->filter == NULL) {
276 * Short circuit looking at our parent directly
277 * since we have encapsulated all of its information
279 newtag->filter = parent->filter;
280 newtag->filterarg = parent->filterarg;
281 newtag->parent = parent->parent;
283 if (newtag->parent != NULL)
284 atomic_add_int(&parent->ref_count, 1);
288 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
289 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
295 bus_dma_tag_destroy(bus_dma_tag_t dmat)
298 bus_dma_tag_t dmat_copy = dmat;
303 if (dmat->map_count != 0)
306 while (dmat != NULL) {
307 bus_dma_tag_t parent;
309 parent = dmat->parent;
310 atomic_subtract_int(&dmat->ref_count, 1);
311 if (dmat->ref_count == 0) {
312 free(dmat, M_DEVBUF);
314 * Last reference count, so
315 * release our reference
316 * count on our parent.
323 CTR2(KTR_BUSDMA, "%s tag %p", __func__, dmat_copy);
329 * Allocate a handle for mapping from kva/uva/physical
330 * address space into bus device space.
333 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
340 newmap = _busdma_alloc_dmamap();
341 if (newmap == NULL) {
342 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
349 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
350 __func__, dmat, dmat->flags, error);
356 * Destroy a handle for mapping from kva/uva/physical
357 * address space into bus device space.
360 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
363 _busdma_free_dmamap(map);
365 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
370 * Allocate a piece of memory that can be efficiently mapped into
371 * bus device space based on the constraints lited in the dma tag.
372 * A dmamap to for use with dmamap_load is also allocated.
375 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
378 bus_dmamap_t newmap = NULL;
382 if (flags & BUS_DMA_NOWAIT)
386 if (flags & BUS_DMA_ZERO)
389 newmap = _busdma_alloc_dmamap();
390 if (newmap == NULL) {
391 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
392 __func__, dmat, dmat->flags, ENOMEM);
399 if (dmat->maxsize <= PAGE_SIZE) {
400 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
403 * XXX Use Contigmalloc until it is merged into this facility
404 * and handles multi-seg allocations. Nobody is doing
405 * multi-seg allocations yet though.
407 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
408 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
411 if (*vaddr == NULL) {
412 if (newmap != NULL) {
413 _busdma_free_dmamap(newmap);
423 * Free a piece of memory and it's allocated dmamap, that was allocated
424 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
427 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
429 if (dmat->maxsize <= PAGE_SIZE)
430 free(vaddr, M_DEVBUF);
432 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
435 _busdma_free_dmamap(map);
436 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
440 * Utility function to load a linear buffer. lastaddrp holds state
441 * between invocations (for multiple-buffer loads). segp contains
442 * the starting segment on entrance, and the ending segment on exit.
443 * first indicates if this is the first invocation of this function.
446 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
447 bus_dmamap_t map, void *buf, bus_size_t buflen, struct pmap *pmap,
448 int flags, vm_offset_t *lastaddrp, int *segp)
451 bus_addr_t curaddr, lastaddr, baddr, bmask;
452 vm_offset_t vaddr = (vm_offset_t)buf;
459 lastaddr = *lastaddrp;
460 bmask = ~(dmat->boundary - 1);
462 CTR3(KTR_BUSDMA, "lowaddr= %d boundary= %d, "
463 "alignment= %d", dmat->lowaddr, dmat->boundary, dmat->alignment);
465 for (seg = *segp; buflen > 0 ; ) {
467 * Get the physical address for this segment.
469 * XXX Don't support checking for coherent mappings
470 * XXX in user address space.
472 if (__predict_true(pmap == pmap_kernel())) {
473 (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
474 if (__predict_false(pmap_pde_section(pde))) {
475 curaddr = (*pde & L1_S_FRAME) |
476 (vaddr & L1_S_OFFSET);
477 if (*pde & L1_S_CACHE_MASK) {
483 KASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV,
485 if (__predict_false((pte & L2_TYPE_MASK)
487 curaddr = (pte & L2_L_FRAME) |
488 (vaddr & L2_L_OFFSET);
489 if (pte & L2_L_CACHE_MASK) {
495 curaddr = (pte & L2_S_FRAME) |
496 (vaddr & L2_S_OFFSET);
497 if (pte & L2_S_CACHE_MASK) {
504 curaddr = pmap_extract(pmap, vaddr);
505 map->flags &= ~DMAMAP_COHERENT;
509 struct arm32_dma_range *dr;
511 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
516 * In a valid DMA range. Translate the physical
517 * memory address to an address in the DMA window.
519 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
523 * Compute the segment size, and adjust counts.
525 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
530 * Make sure we don't cross any boundaries.
532 if (dmat->boundary > 0) {
533 baddr = (curaddr + dmat->boundary) & bmask;
534 if (sgsize > (baddr - curaddr))
535 sgsize = (baddr - curaddr);
539 * Insert chunk into a segment, coalescing with
540 * the previous segment if possible.
542 if (seg >= 0 && curaddr == lastaddr &&
543 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
544 (dmat->boundary == 0 ||
545 (segs[seg].ds_addr & bmask) ==
546 (curaddr & bmask))) {
547 segs[seg].ds_len += sgsize;
550 if (++seg >= dmat->nsegments)
552 segs[seg].ds_addr = curaddr;
553 segs[seg].ds_len = sgsize;
558 lastaddr = curaddr + sgsize;
564 *lastaddrp = lastaddr;
570 error = EFBIG; /* XXX better return value here? */
575 * Map the buffer buf into bus space using the dmamap map.
578 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
579 bus_size_t buflen, bus_dmamap_callback_t *callback,
580 void *callback_arg, int flags)
582 vm_offset_t lastaddr = 0;
583 int error, nsegs = -1;
584 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
585 bus_dma_segment_t dm_segments[dmat->nsegments];
587 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
590 KASSERT(dmat != NULL, ("dmatag is NULL"));
591 KASSERT(map != NULL, ("dmamap is NULL"));
592 map->flags &= ~DMAMAP_TYPE_MASK;
593 map->flags |= DMAMAP_LINEAR|DMAMAP_COHERENT;
596 error = bus_dmamap_load_buffer(dmat,
597 dm_segments, map, buf, buflen, kernel_pmap,
598 flags, &lastaddr, &nsegs);
600 (*callback)(callback_arg, NULL, 0, error);
602 (*callback)(callback_arg, dm_segments, nsegs + 1, error);
604 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
605 __func__, dmat, dmat->flags, nsegs + 1, error);
611 * Like bus_dmamap_load(), but for mbufs.
614 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
615 bus_dmamap_callback2_t *callback, void *callback_arg,
618 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
619 bus_dma_segment_t dm_segments[dmat->nsegments];
621 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
623 int nsegs = -1, error = 0;
627 map->flags &= ~DMAMAP_TYPE_MASK;
628 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
631 if (m0->m_pkthdr.len <= dmat->maxsize) {
632 vm_offset_t lastaddr = 0;
635 for (m = m0; m != NULL && error == 0; m = m->m_next) {
637 error = bus_dmamap_load_buffer(dmat,
638 dm_segments, map, m->m_data, m->m_len,
639 pmap_kernel(), flags, &lastaddr, &nsegs);
640 map->len += m->m_len;
649 * force "no valid mappings" on error in callback.
651 (*callback)(callback_arg, dm_segments, 0, 0, error);
653 (*callback)(callback_arg, dm_segments, nsegs + 1,
654 m0->m_pkthdr.len, error);
656 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
657 __func__, dmat, dmat->flags, error, nsegs + 1);
663 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
664 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
670 flags |= BUS_DMA_NOWAIT;
672 map->flags &= ~DMAMAP_TYPE_MASK;
673 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
676 if (m0->m_pkthdr.len <= dmat->maxsize) {
677 vm_offset_t lastaddr = 0;
680 for (m = m0; m != NULL && error == 0; m = m->m_next) {
682 error = bus_dmamap_load_buffer(dmat, segs, map,
684 pmap_kernel(), flags, &lastaddr,
686 map->len += m->m_len;
693 /* XXX FIXME: Having to increment nsegs is really annoying */
695 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
696 __func__, dmat, dmat->flags, error, *nsegs);
701 * Like bus_dmamap_load(), but for uios.
704 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
705 bus_dmamap_callback2_t *callback, void *callback_arg,
708 vm_offset_t lastaddr;
709 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
710 bus_dma_segment_t dm_segments[dmat->nsegments];
712 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
719 resid = uio->uio_resid;
721 map->flags &= ~DMAMAP_TYPE_MASK;
722 map->flags |= DMAMAP_UIO|DMAMAP_COHERENT;
726 if (uio->uio_segflg == UIO_USERSPACE) {
727 KASSERT(uio->uio_td != NULL,
728 ("bus_dmamap_load_uio: USERSPACE but no proc"));
729 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
735 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
737 * Now at the first iovec to load. Load each iovec
738 * until we have exhausted the residual count.
741 resid < iov[i].iov_len ? resid : iov[i].iov_len;
742 caddr_t addr = (caddr_t) iov[i].iov_base;
745 error = bus_dmamap_load_buffer(dmat, dm_segments, map,
746 addr, minlen, pmap, flags, &lastaddr, &nsegs);
755 * force "no valid mappings" on error in callback.
757 (*callback)(callback_arg, dm_segments, 0, 0, error);
759 (*callback)(callback_arg, dm_segments, nsegs+1,
760 uio->uio_resid, error);
763 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
764 __func__, dmat, dmat->flags, error, nsegs + 1);
769 * Release the mapping held by map.
772 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
774 map->flags &= ~DMAMAP_TYPE_MASK;
779 bus_dmamap_sync_buf(void *buf, int len, bus_dmasync_op_t op)
782 if (op & BUS_DMASYNC_PREWRITE)
783 cpu_dcache_wb_range((vm_offset_t)buf, len);
784 if (op & BUS_DMASYNC_POSTREAD) {
785 if ((((vm_offset_t)buf | len) & arm_dcache_align_mask) == 0)
786 cpu_dcache_inv_range((vm_offset_t)buf, len);
788 cpu_dcache_wbinv_range((vm_offset_t)buf, len);
794 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
801 if (!(op & (BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTREAD)))
803 if (map->flags & DMAMAP_COHERENT)
805 if ((op && BUS_DMASYNC_POSTREAD) && (map->len >= 2 * PAGE_SIZE)) {
806 cpu_dcache_wbinv_all();
809 CTR3(KTR_BUSDMA, "%s: op %x flags %x", __func__, op, map->flags);
810 switch(map->flags & DMAMAP_TYPE_MASK) {
812 bus_dmamap_sync_buf(map->buffer, map->len, op);
818 bus_dmamap_sync_buf(m->m_data, m->m_len, op);
825 resid = uio->uio_resid;
826 for (int i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
827 bus_size_t minlen = resid < iov[i].iov_len ? resid :
830 bus_dmamap_sync_buf(iov[i].iov_base, minlen,
839 cpu_drain_writebuf();