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);
351 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
352 __func__, dmat, dmat->flags, error);
358 * Destroy a handle for mapping from kva/uva/physical
359 * address space into bus device space.
362 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
365 _busdma_free_dmamap(map);
367 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
372 * Allocate a piece of memory that can be efficiently mapped into
373 * bus device space based on the constraints lited in the dma tag.
374 * A dmamap to for use with dmamap_load is also allocated.
377 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
380 bus_dmamap_t newmap = NULL;
384 if (flags & BUS_DMA_NOWAIT)
388 if (flags & BUS_DMA_ZERO)
391 newmap = _busdma_alloc_dmamap();
392 if (newmap == NULL) {
393 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
394 __func__, dmat, dmat->flags, ENOMEM);
401 if (dmat->maxsize <= PAGE_SIZE) {
402 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
405 * XXX Use Contigmalloc until it is merged into this facility
406 * and handles multi-seg allocations. Nobody is doing
407 * multi-seg allocations yet though.
409 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
410 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
413 if (*vaddr == NULL) {
414 if (newmap != NULL) {
415 _busdma_free_dmamap(newmap);
421 if (flags & BUS_DMA_COHERENT) {
422 void *tmpaddr = arm_remap_nocache(
423 (void *)((vm_offset_t)*vaddr &~ PAGE_MASK),
424 dmat->maxsize + ((vm_offset_t)*vaddr & PAGE_MASK));
427 tmpaddr = (void *)((vm_offset_t)(tmpaddr) +
428 ((vm_offset_t)*vaddr & PAGE_MASK));
429 newmap->origbuffer = *vaddr;
430 newmap->allocbuffer = tmpaddr;
431 cpu_idcache_wbinv_range((vm_offset_t)*vaddr,
435 newmap->origbuffer = newmap->allocbuffer = NULL;
437 newmap->origbuffer = newmap->allocbuffer = NULL;
442 * Free a piece of memory and it's allocated dmamap, that was allocated
443 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
446 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
448 if (map->allocbuffer) {
449 KASSERT(map->allocbuffer == vaddr,
450 ("Trying to freeing the wrong DMA buffer"));
451 vaddr = map->origbuffer;
452 arm_unmap_nocache(map->allocbuffer, dmat->maxsize);
454 if (dmat->maxsize <= PAGE_SIZE)
455 free(vaddr, M_DEVBUF);
457 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
460 _busdma_free_dmamap(map);
461 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
465 * Utility function to load a linear buffer. lastaddrp holds state
466 * between invocations (for multiple-buffer loads). segp contains
467 * the starting segment on entrance, and the ending segment on exit.
468 * first indicates if this is the first invocation of this function.
471 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
472 bus_dmamap_t map, void *buf, bus_size_t buflen, struct pmap *pmap,
473 int flags, vm_offset_t *lastaddrp, int *segp)
476 bus_addr_t curaddr, lastaddr, baddr, bmask;
477 vm_offset_t vaddr = (vm_offset_t)buf;
484 lastaddr = *lastaddrp;
485 bmask = ~(dmat->boundary - 1);
487 CTR3(KTR_BUSDMA, "lowaddr= %d boundary= %d, "
488 "alignment= %d", dmat->lowaddr, dmat->boundary, dmat->alignment);
490 for (seg = *segp; buflen > 0 ; ) {
492 * Get the physical address for this segment.
494 * XXX Don't support checking for coherent mappings
495 * XXX in user address space.
497 if (__predict_true(pmap == pmap_kernel())) {
498 (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
499 if (__predict_false(pmap_pde_section(pde))) {
500 curaddr = (*pde & L1_S_FRAME) |
501 (vaddr & L1_S_OFFSET);
502 if (*pde & L1_S_CACHE_MASK) {
508 KASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV,
510 if (__predict_false((pte & L2_TYPE_MASK)
512 curaddr = (pte & L2_L_FRAME) |
513 (vaddr & L2_L_OFFSET);
514 if (pte & L2_L_CACHE_MASK) {
520 curaddr = (pte & L2_S_FRAME) |
521 (vaddr & L2_S_OFFSET);
522 if (pte & L2_S_CACHE_MASK) {
529 curaddr = pmap_extract(pmap, vaddr);
530 map->flags &= ~DMAMAP_COHERENT;
534 struct arm32_dma_range *dr;
536 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
541 * In a valid DMA range. Translate the physical
542 * memory address to an address in the DMA window.
544 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
548 * Compute the segment size, and adjust counts.
550 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
555 * Make sure we don't cross any boundaries.
557 if (dmat->boundary > 0) {
558 baddr = (curaddr + dmat->boundary) & bmask;
559 if (sgsize > (baddr - curaddr))
560 sgsize = (baddr - curaddr);
564 * Insert chunk into a segment, coalescing with
565 * the previous segment if possible.
567 if (seg >= 0 && curaddr == lastaddr &&
568 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
569 (dmat->boundary == 0 ||
570 (segs[seg].ds_addr & bmask) ==
571 (curaddr & bmask))) {
572 segs[seg].ds_len += sgsize;
575 if (++seg >= dmat->nsegments)
577 segs[seg].ds_addr = curaddr;
578 segs[seg].ds_len = sgsize;
583 lastaddr = curaddr + sgsize;
589 *lastaddrp = lastaddr;
595 error = EFBIG; /* XXX better return value here? */
600 * Map the buffer buf into bus space using the dmamap map.
603 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
604 bus_size_t buflen, bus_dmamap_callback_t *callback,
605 void *callback_arg, int flags)
607 vm_offset_t lastaddr = 0;
608 int error, nsegs = -1;
609 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
610 bus_dma_segment_t dm_segments[dmat->nsegments];
612 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
615 KASSERT(dmat != NULL, ("dmatag is NULL"));
616 KASSERT(map != NULL, ("dmamap is NULL"));
617 map->flags &= ~DMAMAP_TYPE_MASK;
618 map->flags |= DMAMAP_LINEAR|DMAMAP_COHERENT;
621 error = bus_dmamap_load_buffer(dmat,
622 dm_segments, map, buf, buflen, kernel_pmap,
623 flags, &lastaddr, &nsegs);
625 (*callback)(callback_arg, NULL, 0, error);
627 (*callback)(callback_arg, dm_segments, nsegs + 1, error);
629 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
630 __func__, dmat, dmat->flags, nsegs + 1, error);
636 * Like bus_dmamap_load(), but for mbufs.
639 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
640 bus_dmamap_callback2_t *callback, void *callback_arg,
643 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
644 bus_dma_segment_t dm_segments[dmat->nsegments];
646 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
648 int nsegs = -1, error = 0;
652 map->flags &= ~DMAMAP_TYPE_MASK;
653 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
656 if (m0->m_pkthdr.len <= dmat->maxsize) {
657 vm_offset_t lastaddr = 0;
660 for (m = m0; m != NULL && error == 0; m = m->m_next) {
662 error = bus_dmamap_load_buffer(dmat,
663 dm_segments, map, m->m_data, m->m_len,
664 pmap_kernel(), flags, &lastaddr, &nsegs);
665 map->len += m->m_len;
674 * force "no valid mappings" on error in callback.
676 (*callback)(callback_arg, dm_segments, 0, 0, error);
678 (*callback)(callback_arg, dm_segments, nsegs + 1,
679 m0->m_pkthdr.len, error);
681 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
682 __func__, dmat, dmat->flags, error, nsegs + 1);
688 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
689 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
695 flags |= BUS_DMA_NOWAIT;
697 map->flags &= ~DMAMAP_TYPE_MASK;
698 map->flags |= DMAMAP_MBUF | DMAMAP_COHERENT;
701 if (m0->m_pkthdr.len <= dmat->maxsize) {
702 vm_offset_t lastaddr = 0;
705 for (m = m0; m != NULL && error == 0; m = m->m_next) {
707 error = bus_dmamap_load_buffer(dmat, segs, map,
709 pmap_kernel(), flags, &lastaddr,
711 map->len += m->m_len;
718 /* XXX FIXME: Having to increment nsegs is really annoying */
720 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
721 __func__, dmat, dmat->flags, error, *nsegs);
726 * Like bus_dmamap_load(), but for uios.
729 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
730 bus_dmamap_callback2_t *callback, void *callback_arg,
733 vm_offset_t lastaddr;
734 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
735 bus_dma_segment_t dm_segments[dmat->nsegments];
737 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
744 resid = uio->uio_resid;
746 map->flags &= ~DMAMAP_TYPE_MASK;
747 map->flags |= DMAMAP_UIO|DMAMAP_COHERENT;
751 if (uio->uio_segflg == UIO_USERSPACE) {
752 KASSERT(uio->uio_td != NULL,
753 ("bus_dmamap_load_uio: USERSPACE but no proc"));
754 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
760 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
762 * Now at the first iovec to load. Load each iovec
763 * until we have exhausted the residual count.
766 resid < iov[i].iov_len ? resid : iov[i].iov_len;
767 caddr_t addr = (caddr_t) iov[i].iov_base;
770 error = bus_dmamap_load_buffer(dmat, dm_segments, map,
771 addr, minlen, pmap, flags, &lastaddr, &nsegs);
780 * force "no valid mappings" on error in callback.
782 (*callback)(callback_arg, dm_segments, 0, 0, error);
784 (*callback)(callback_arg, dm_segments, nsegs+1,
785 uio->uio_resid, error);
788 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
789 __func__, dmat, dmat->flags, error, nsegs + 1);
794 * Release the mapping held by map.
797 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
799 map->flags &= ~DMAMAP_TYPE_MASK;
804 bus_dmamap_sync_buf(void *buf, int len, bus_dmasync_op_t op)
807 if (op & BUS_DMASYNC_PREWRITE)
808 cpu_dcache_wb_range((vm_offset_t)buf, len);
809 if (op & BUS_DMASYNC_POSTREAD) {
810 if ((((vm_offset_t)buf | len) & arm_dcache_align_mask) == 0)
811 cpu_dcache_inv_range((vm_offset_t)buf, len);
813 cpu_dcache_wbinv_range((vm_offset_t)buf, len);
819 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
826 if (!(op & (BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTREAD)))
828 if (map->flags & DMAMAP_COHERENT)
830 if ((op && BUS_DMASYNC_POSTREAD) && (map->len >= 2 * PAGE_SIZE)) {
831 cpu_dcache_wbinv_all();
834 CTR3(KTR_BUSDMA, "%s: op %x flags %x", __func__, op, map->flags);
835 switch(map->flags & DMAMAP_TYPE_MASK) {
837 bus_dmamap_sync_buf(map->buffer, map->len, op);
843 bus_dmamap_sync_buf(m->m_data, m->m_len, op);
850 resid = uio->uio_resid;
851 for (int i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
852 bus_size_t minlen = resid < iov[i].iov_len ? resid :
855 bus_dmamap_sync_buf(iov[i].iov_base, minlen,
864 cpu_drain_writebuf();