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
98 TAILQ_ENTRY(bus_dmamap) freelist;
102 static TAILQ_HEAD(,bus_dmamap) dmamap_freelist =
103 TAILQ_HEAD_INITIALIZER(dmamap_freelist);
105 #define BUSDMA_STATIC_MAPS 500
106 static struct bus_dmamap map_pool[BUSDMA_STATIC_MAPS];
108 static struct mtx busdma_mtx;
110 MTX_SYSINIT(busdma_mtx, &busdma_mtx, "busdma lock", MTX_DEF);
113 arm_dmamap_freelist_init(void *dummy)
117 for (i = 0; i < BUSDMA_STATIC_MAPS; i++)
118 TAILQ_INSERT_HEAD(&dmamap_freelist, &map_pool[i], freelist);
121 SYSINIT(busdma, SI_SUB_VM, SI_ORDER_ANY, arm_dmamap_freelist_init, NULL);
124 * Check to see if the specified page is in an allowed DMA range.
128 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
129 bus_dmamap_t map, void *buf, bus_size_t buflen, struct pmap *pmap,
130 int flags, vm_offset_t *lastaddrp, int *segp);
132 static __inline struct arm32_dma_range *
133 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
136 struct arm32_dma_range *dr;
139 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
140 if (curaddr >= dr->dr_sysbase &&
141 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
148 * Convenience function for manipulating driver locks from busdma (during
149 * busdma_swi, for example). Drivers that don't provide their own locks
150 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
151 * non-mutex locking scheme don't have to use this at all.
154 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
158 dmtx = (struct mtx *)arg;
167 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
172 * dflt_lock should never get called. It gets put into the dma tag when
173 * lockfunc == NULL, which is only valid if the maps that are associated
174 * with the tag are meant to never be defered.
175 * XXX Should have a way to identify which driver is responsible here.
178 dflt_lock(void *arg, bus_dma_lock_op_t op)
181 panic("driver error: busdma dflt_lock called");
183 printf("DRIVER_ERROR: busdma dflt_lock called\n");
187 static __inline bus_dmamap_t
188 _busdma_alloc_dmamap(void)
192 mtx_lock(&busdma_mtx);
193 map = TAILQ_FIRST(&dmamap_freelist);
195 TAILQ_REMOVE(&dmamap_freelist, map, freelist);
196 mtx_unlock(&busdma_mtx);
198 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
200 map->flags = DMAMAP_ALLOCATED;
207 _busdma_free_dmamap(bus_dmamap_t map)
209 if (map->flags & DMAMAP_ALLOCATED)
212 mtx_lock(&busdma_mtx);
213 TAILQ_INSERT_HEAD(&dmamap_freelist, map, freelist);
214 mtx_unlock(&busdma_mtx);
219 * Allocate a device specific dma_tag.
224 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
225 bus_size_t boundary, bus_addr_t lowaddr,
226 bus_addr_t highaddr, bus_dma_filter_t *filter,
227 void *filterarg, bus_size_t maxsize, int nsegments,
228 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
229 void *lockfuncarg, bus_dma_tag_t *dmat)
231 bus_dma_tag_t newtag;
233 /* Return a NULL tag on failure */
236 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
237 if (newtag == NULL) {
238 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
239 __func__, newtag, 0, error);
243 newtag->parent = parent;
244 newtag->alignment = alignment;
245 newtag->boundary = boundary;
246 newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
247 newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
248 newtag->filter = filter;
249 newtag->filterarg = filterarg;
250 newtag->maxsize = maxsize;
251 newtag->nsegments = nsegments;
252 newtag->maxsegsz = maxsegsz;
253 newtag->flags = flags;
254 newtag->ref_count = 1; /* Count ourself */
255 newtag->map_count = 0;
256 newtag->ranges = bus_dma_get_range();
257 newtag->_nranges = bus_dma_get_range_nb();
258 if (lockfunc != NULL) {
259 newtag->lockfunc = lockfunc;
260 newtag->lockfuncarg = lockfuncarg;
262 newtag->lockfunc = dflt_lock;
263 newtag->lockfuncarg = NULL;
266 * Take into account any restrictions imposed by our parent tag
268 if (parent != NULL) {
269 newtag->lowaddr = min(parent->lowaddr, newtag->lowaddr);
270 newtag->highaddr = max(parent->highaddr, newtag->highaddr);
271 if (newtag->boundary == 0)
272 newtag->boundary = parent->boundary;
273 else if (parent->boundary != 0)
274 newtag->boundary = min(parent->boundary,
276 if (newtag->filter == NULL) {
278 * Short circuit looking at our parent directly
279 * since we have encapsulated all of its information
281 newtag->filter = parent->filter;
282 newtag->filterarg = parent->filterarg;
283 newtag->parent = parent->parent;
285 if (newtag->parent != NULL)
286 atomic_add_int(&parent->ref_count, 1);
290 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
291 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
297 bus_dma_tag_destroy(bus_dma_tag_t dmat)
300 bus_dma_tag_t dmat_copy = dmat;
305 if (dmat->map_count != 0)
308 while (dmat != NULL) {
309 bus_dma_tag_t parent;
311 parent = dmat->parent;
312 atomic_subtract_int(&dmat->ref_count, 1);
313 if (dmat->ref_count == 0) {
314 free(dmat, M_DEVBUF);
316 * Last reference count, so
317 * release our reference
318 * count on our parent.
325 CTR2(KTR_BUSDMA, "%s tag %p", __func__, dmat_copy);
331 * Allocate a handle for mapping from kva/uva/physical
332 * address space into bus device space.
335 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
342 newmap = _busdma_alloc_dmamap();
343 if (newmap == NULL) {
344 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
349 newmap->allocbuffer = NULL;
352 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
353 __func__, dmat, dmat->flags, error);
359 * Destroy a handle for mapping from kva/uva/physical
360 * address space into bus device space.
363 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
366 _busdma_free_dmamap(map);
368 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
373 * Allocate a piece of memory that can be efficiently mapped into
374 * bus device space based on the constraints lited in the dma tag.
375 * A dmamap to for use with dmamap_load is also allocated.
378 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
381 bus_dmamap_t newmap = NULL;
385 if (flags & BUS_DMA_NOWAIT)
389 if (flags & BUS_DMA_ZERO)
392 newmap = _busdma_alloc_dmamap();
393 if (newmap == NULL) {
394 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
395 __func__, dmat, dmat->flags, ENOMEM);
402 if (dmat->maxsize <= PAGE_SIZE) {
403 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
406 * XXX Use Contigmalloc until it is merged into this facility
407 * and handles multi-seg allocations. Nobody is doing
408 * multi-seg allocations yet though.
410 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
411 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
414 if (*vaddr == NULL) {
415 if (newmap != NULL) {
416 _busdma_free_dmamap(newmap);
422 if (flags & BUS_DMA_COHERENT) {
423 void *tmpaddr = arm_remap_nocache(
424 (void *)((vm_offset_t)*vaddr &~ PAGE_MASK),
425 dmat->maxsize + ((vm_offset_t)*vaddr & PAGE_MASK));
428 tmpaddr = (void *)((vm_offset_t)(tmpaddr) +
429 ((vm_offset_t)*vaddr & PAGE_MASK));
430 newmap->origbuffer = *vaddr;
431 newmap->allocbuffer = tmpaddr;
432 cpu_idcache_wbinv_range((vm_offset_t)*vaddr,
436 newmap->origbuffer = newmap->allocbuffer = NULL;
438 newmap->origbuffer = newmap->allocbuffer = NULL;
443 * Free a piece of memory and it's allocated dmamap, that was allocated
444 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
447 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
449 if (map->allocbuffer) {
450 KASSERT(map->allocbuffer == vaddr,
451 ("Trying to freeing the wrong DMA buffer"));
452 vaddr = map->origbuffer;
453 arm_unmap_nocache(map->allocbuffer, dmat->maxsize);
455 if (dmat->maxsize <= PAGE_SIZE)
456 free(vaddr, M_DEVBUF);
458 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
461 _busdma_free_dmamap(map);
462 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
466 * Utility function to load a linear buffer. lastaddrp holds state
467 * between invocations (for multiple-buffer loads). segp contains
468 * the starting segment on entrance, and the ending segment on exit.
469 * first indicates if this is the first invocation of this function.
472 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
473 bus_dmamap_t map, void *buf, bus_size_t buflen, struct pmap *pmap,
474 int flags, vm_offset_t *lastaddrp, int *segp)
477 bus_addr_t curaddr, lastaddr, baddr, bmask;
478 vm_offset_t vaddr = (vm_offset_t)buf;
485 lastaddr = *lastaddrp;
486 bmask = ~(dmat->boundary - 1);
488 CTR3(KTR_BUSDMA, "lowaddr= %d boundary= %d, "
489 "alignment= %d", dmat->lowaddr, dmat->boundary, dmat->alignment);
491 for (seg = *segp; buflen > 0 ; ) {
493 * Get the physical address for this segment.
495 * XXX Don't support checking for coherent mappings
496 * XXX in user address space.
498 if (__predict_true(pmap == pmap_kernel())) {
499 (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
500 if (__predict_false(pmap_pde_section(pde))) {
501 curaddr = (*pde & L1_S_FRAME) |
502 (vaddr & L1_S_OFFSET);
503 if (*pde & L1_S_CACHE_MASK) {
509 KASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV,
511 if (__predict_false((pte & L2_TYPE_MASK)
513 curaddr = (pte & L2_L_FRAME) |
514 (vaddr & L2_L_OFFSET);
515 if (pte & L2_L_CACHE_MASK) {
521 curaddr = (pte & L2_S_FRAME) |
522 (vaddr & L2_S_OFFSET);
523 if (pte & L2_S_CACHE_MASK) {
530 curaddr = pmap_extract(pmap, vaddr);
531 map->flags &= ~DMAMAP_COHERENT;
535 struct arm32_dma_range *dr;
537 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
542 * In a valid DMA range. Translate the physical
543 * memory address to an address in the DMA window.
545 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
549 * Compute the segment size, and adjust counts.
551 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
556 * Make sure we don't cross any boundaries.
558 if (dmat->boundary > 0) {
559 baddr = (curaddr + dmat->boundary) & bmask;
560 if (sgsize > (baddr - curaddr))
561 sgsize = (baddr - curaddr);
565 * Insert chunk into a segment, coalescing with
566 * the previous segment if possible.
568 if (seg >= 0 && curaddr == lastaddr &&
569 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
570 (dmat->boundary == 0 ||
571 (segs[seg].ds_addr & bmask) ==
572 (curaddr & bmask))) {
573 segs[seg].ds_len += sgsize;
576 if (++seg >= dmat->nsegments)
578 segs[seg].ds_addr = curaddr;
579 segs[seg].ds_len = sgsize;
584 lastaddr = curaddr + sgsize;
590 *lastaddrp = lastaddr;
596 error = EFBIG; /* XXX better return value here? */
601 * Map the buffer buf into bus space using the dmamap map.
604 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
605 bus_size_t buflen, bus_dmamap_callback_t *callback,
606 void *callback_arg, int flags)
608 vm_offset_t lastaddr = 0;
609 int error, nsegs = -1;
610 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
611 bus_dma_segment_t dm_segments[dmat->nsegments];
613 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
616 KASSERT(dmat != NULL, ("dmatag is NULL"));
617 KASSERT(map != NULL, ("dmamap is NULL"));
618 map->flags &= ~DMAMAP_TYPE_MASK;
619 map->flags |= DMAMAP_LINEAR|DMAMAP_COHERENT;
622 error = bus_dmamap_load_buffer(dmat,
623 dm_segments, map, buf, buflen, kernel_pmap,
624 flags, &lastaddr, &nsegs);
626 (*callback)(callback_arg, NULL, 0, error);
628 (*callback)(callback_arg, dm_segments, nsegs + 1, error);
630 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
631 __func__, dmat, dmat->flags, nsegs + 1, error);
637 * Like bus_dmamap_load(), but for mbufs.
640 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
641 bus_dmamap_callback2_t *callback, void *callback_arg,
644 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
645 bus_dma_segment_t dm_segments[dmat->nsegments];
647 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
649 int nsegs = -1, error = 0;
653 map->flags &= ~DMAMAP_TYPE_MASK;
654 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
657 if (m0->m_pkthdr.len <= dmat->maxsize) {
658 vm_offset_t lastaddr = 0;
661 for (m = m0; m != NULL && error == 0; m = m->m_next) {
663 error = bus_dmamap_load_buffer(dmat,
664 dm_segments, map, m->m_data, m->m_len,
665 pmap_kernel(), flags, &lastaddr, &nsegs);
666 map->len += m->m_len;
675 * force "no valid mappings" on error in callback.
677 (*callback)(callback_arg, dm_segments, 0, 0, error);
679 (*callback)(callback_arg, dm_segments, nsegs + 1,
680 m0->m_pkthdr.len, error);
682 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
683 __func__, dmat, dmat->flags, error, nsegs + 1);
689 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
690 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
696 flags |= BUS_DMA_NOWAIT;
698 map->flags &= ~DMAMAP_TYPE_MASK;
699 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
702 if (m0->m_pkthdr.len <= dmat->maxsize) {
703 vm_offset_t lastaddr = 0;
706 for (m = m0; m != NULL && error == 0; m = m->m_next) {
708 error = bus_dmamap_load_buffer(dmat, segs, map,
710 pmap_kernel(), flags, &lastaddr,
712 map->len += m->m_len;
719 /* XXX FIXME: Having to increment nsegs is really annoying */
721 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
722 __func__, dmat, dmat->flags, error, *nsegs);
727 * Like bus_dmamap_load(), but for uios.
730 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
731 bus_dmamap_callback2_t *callback, void *callback_arg,
734 vm_offset_t lastaddr;
735 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
736 bus_dma_segment_t dm_segments[dmat->nsegments];
738 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
745 resid = uio->uio_resid;
747 map->flags &= ~DMAMAP_TYPE_MASK;
748 map->flags |= DMAMAP_UIO|DMAMAP_COHERENT;
752 if (uio->uio_segflg == UIO_USERSPACE) {
753 KASSERT(uio->uio_td != NULL,
754 ("bus_dmamap_load_uio: USERSPACE but no proc"));
755 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
761 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
763 * Now at the first iovec to load. Load each iovec
764 * until we have exhausted the residual count.
767 resid < iov[i].iov_len ? resid : iov[i].iov_len;
768 caddr_t addr = (caddr_t) iov[i].iov_base;
771 error = bus_dmamap_load_buffer(dmat, dm_segments, map,
772 addr, minlen, pmap, flags, &lastaddr, &nsegs);
781 * force "no valid mappings" on error in callback.
783 (*callback)(callback_arg, dm_segments, 0, 0, error);
785 (*callback)(callback_arg, dm_segments, nsegs+1,
786 uio->uio_resid, error);
789 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
790 __func__, dmat, dmat->flags, error, nsegs + 1);
795 * Release the mapping held by map.
798 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
800 map->flags &= ~DMAMAP_TYPE_MASK;
805 bus_dmamap_sync_buf(void *buf, int len, bus_dmasync_op_t op)
808 if (op & BUS_DMASYNC_PREWRITE)
809 cpu_dcache_wb_range((vm_offset_t)buf, len);
810 if (op & BUS_DMASYNC_PREREAD) {
811 if ((vm_offset_t)buf & arm_dcache_align_mask)
812 cpu_dcache_wbinv_range((vm_offset_t)buf &
813 ~arm_dcache_align_mask, arm_dcache_align);
814 if (((vm_offset_t)buf + len) & arm_dcache_align_mask)
815 cpu_dcache_wbinv_range(((vm_offset_t)buf + len) &
816 ~arm_dcache_align_mask, arm_dcache_align);
818 if (op & BUS_DMASYNC_POSTREAD)
819 cpu_dcache_inv_range((vm_offset_t)buf, len);
823 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
830 if (op == BUS_DMASYNC_POSTWRITE)
832 if (map->flags & DMAMAP_COHERENT)
834 if ((op && BUS_DMASYNC_POSTREAD) && (map->len >= 2 * PAGE_SIZE)) {
835 cpu_dcache_wbinv_all();
838 CTR3(KTR_BUSDMA, "%s: op %x flags %x", __func__, op, map->flags);
839 switch(map->flags & DMAMAP_TYPE_MASK) {
841 bus_dmamap_sync_buf(map->buffer, map->len, op);
847 bus_dmamap_sync_buf(m->m_data, m->m_len, op);
854 resid = uio->uio_resid;
855 for (int i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
856 bus_size_t minlen = resid < iov[i].iov_len ? resid :
859 bus_dmamap_sync_buf(iov[i].iov_base, minlen,
868 cpu_drain_writebuf();