2 * Copyright (c) 2002 Peter Grehan
3 * Copyright (c) 1997, 1998 Justin T. Gibbs.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * From i386/busdma_machdep.c,v 1.26 2002/04/19 22:58:09 alfred
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * MacPPC bus dma support routines
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/interrupt.h>
44 #include <sys/mutex.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
52 #include <machine/atomic.h>
53 #include <machine/bus.h>
54 #include <machine/cpufunc.h>
62 bus_dma_filter_t *filter;
70 bus_dma_lock_t *lockfunc;
76 void *buf; /* unmapped buffer pointer */
77 bus_size_t buflen; /* unmapped buffer length */
78 bus_dmamap_callback_t *callback;
83 * Convenience function for manipulating driver locks from busdma (during
84 * busdma_swi, for example). Drivers that don't provide their own locks
85 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
86 * non-mutex locking scheme don't have to use this at all.
89 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
93 dmtx = (struct mtx *)arg;
102 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
107 * dflt_lock should never get called. It gets put into the dma tag when
108 * lockfunc == NULL, which is only valid if the maps that are associated
109 * with the tag are meant to never be defered.
110 * XXX Should have a way to identify which driver is responsible here.
113 dflt_lock(void *arg, bus_dma_lock_op_t op)
116 panic("driver error: busdma dflt_lock called");
118 printf("DRIVER_ERROR: busdma dflt_lock called\n");
123 * Allocate a device specific dma_tag.
126 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
127 bus_size_t boundary, bus_addr_t lowaddr,
128 bus_addr_t highaddr, bus_dma_filter_t *filter,
129 void *filterarg, bus_size_t maxsize, int nsegments,
130 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
131 void *lockfuncarg, bus_dma_tag_t *dmat)
133 bus_dma_tag_t newtag;
136 /* Return a NULL tag on failure */
139 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
143 newtag->parent = parent;
144 newtag->alignment = alignment;
145 newtag->boundary = boundary;
146 newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
147 newtag->highaddr = trunc_page((vm_offset_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 if (lockfunc != NULL) {
157 newtag->lockfunc = lockfunc;
158 newtag->lockfuncarg = lockfuncarg;
160 newtag->lockfunc = dflt_lock;
161 newtag->lockfuncarg = NULL;
165 * Take into account any restrictions imposed by our parent tag
167 if (parent != NULL) {
168 newtag->lowaddr = min(parent->lowaddr, newtag->lowaddr);
169 newtag->highaddr = max(parent->highaddr, newtag->highaddr);
170 if (newtag->boundary == 0)
171 newtag->boundary = parent->boundary;
172 else if (parent->boundary != 0)
173 newtag->boundary = MIN(parent->boundary,
175 if (newtag->filter == NULL) {
177 * Short circuit looking at our parent directly
178 * since we have encapsulated all of its information
180 newtag->filter = parent->filter;
181 newtag->filterarg = parent->filterarg;
182 newtag->parent = parent->parent;
184 if (newtag->parent != NULL)
185 atomic_add_int(&parent->ref_count, 1);
193 bus_dma_tag_destroy(bus_dma_tag_t dmat)
197 if (dmat->map_count != 0)
200 while (dmat != NULL) {
201 bus_dma_tag_t parent;
203 parent = dmat->parent;
204 atomic_subtract_int(&dmat->ref_count, 1);
205 if (dmat->ref_count == 0) {
206 free(dmat, M_DEVBUF);
208 * Last reference count, so
209 * release our reference
210 * count on our parent.
221 * Allocate a handle for mapping from kva/uva/physical
222 * address space into bus device space.
225 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
234 * Destroy a handle for mapping from kva/uva/physical
235 * address space into bus device space.
238 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
241 panic("dmamap_destroy: NULL?\n");
248 * Allocate a piece of memory that can be efficiently mapped into
249 * bus device space based on the constraints lited in the dma tag.
250 * A dmamap to for use with dmamap_load is also allocated.
253 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
258 if (flags & BUS_DMA_NOWAIT)
262 if (flags & BUS_DMA_ZERO)
269 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
270 * alignment guarantees of malloc need to be nailed down, and the
271 * code below should be rewritten to take that into account.
273 * In the meantime, we'll return an error if malloc gets it wrong.
275 if (dmat->maxsize <= PAGE_SIZE &&
276 dmat->alignment < dmat->maxsize) {
277 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
280 * XXX Use Contigmalloc until it is merged into this facility
281 * and handles multi-seg allocations. Nobody is doing
282 * multi-seg allocations yet though.
284 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
285 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
292 if ((uintptr_t)*vaddr % dmat->alignment)
293 printf("XXX: %s: alignment not respected!\n", __func__);
299 * Free a piece of memory and it's allocated dmamap, that was allocated
300 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
303 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
306 panic("bus_dmamem_free: Invalid map freed\n");
307 if (dmat->maxsize <= PAGE_SIZE &&
308 dmat->alignment < dmat->maxsize)
309 free(vaddr, M_DEVBUF);
311 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
315 * Utility function to load a linear buffer. lastaddrp holds state
316 * between invocations (for multiple-buffer loads). segp contains
317 * the starting segment on entrance, and the ending segment on exit.
318 * first indicates if this is the first invocation of this function.
321 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t segs[],
322 void *buf, bus_size_t buflen, struct thread *td,
323 int flags, vm_offset_t *lastaddrp, int *segp,
327 bus_addr_t curaddr, lastaddr, baddr, bmask;
328 vm_offset_t vaddr = (vm_offset_t)buf;
333 pmap = vmspace_pmap(td->td_proc->p_vmspace);
337 lastaddr = *lastaddrp;
338 bmask = ~(dmat->boundary - 1);
340 for (seg = *segp; buflen > 0 ; ) {
342 * Get the physical address for this segment.
345 curaddr = pmap_extract(pmap, vaddr);
347 curaddr = pmap_kextract(vaddr);
350 * Compute the segment size, and adjust counts.
352 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
353 if (sgsize > dmat->maxsegsz)
354 sgsize = dmat->maxsegsz;
359 * Make sure we don't cross any boundaries.
361 if (dmat->boundary > 0) {
362 baddr = (curaddr + dmat->boundary) & bmask;
363 if (sgsize > (baddr - curaddr))
364 sgsize = (baddr - curaddr);
368 * Insert chunk into a segment, coalescing with
369 * the previous segment if possible.
372 segs[seg].ds_addr = curaddr;
373 segs[seg].ds_len = sgsize;
376 if (curaddr == lastaddr &&
377 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
378 (dmat->boundary == 0 ||
379 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
380 segs[seg].ds_len += sgsize;
382 if (++seg >= dmat->nsegments)
384 segs[seg].ds_addr = curaddr;
385 segs[seg].ds_len = sgsize;
389 lastaddr = curaddr + sgsize;
395 *lastaddrp = lastaddr;
400 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
404 * Map the buffer buf into bus space using the dmamap map.
407 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
408 bus_size_t buflen, bus_dmamap_callback_t *callback,
409 void *callback_arg, int flags)
411 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
412 bus_dma_segment_t dm_segments[dmat->nsegments];
414 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
416 vm_offset_t lastaddr;
420 panic("bus_dmamap_load: Invalid map\n");
422 lastaddr = (vm_offset_t)0;
424 error = bus_dmamap_load_buffer(dmat, dm_segments, buf, buflen,
425 NULL, flags, &lastaddr, &nsegs, 1);
428 (*callback)(callback_arg, dm_segments, nsegs + 1, 0);
430 (*callback)(callback_arg, NULL, 0, error);
436 * Like bus_dmamap_load(), but for mbufs.
439 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
440 bus_dmamap_callback2_t *callback, void *callback_arg,
443 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
444 bus_dma_segment_t dm_segments[dmat->nsegments];
446 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
448 int nsegs = 0, error = 0;
452 if (m0->m_pkthdr.len <= dmat->maxsize) {
454 vm_offset_t lastaddr = 0;
457 for (m = m0; m != NULL && error == 0; m = m->m_next) {
459 error = bus_dmamap_load_buffer(dmat,
460 dm_segments, m->m_data, m->m_len, NULL,
461 flags, &lastaddr, &nsegs, first);
471 * force "no valid mappings" on error in callback.
473 (*callback)(callback_arg, dm_segments, 0, 0, error);
475 (*callback)(callback_arg, dm_segments, nsegs+1,
476 m0->m_pkthdr.len, error);
482 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
483 bus_dma_segment_t *segs, int *nsegs, int flags)
491 if (m0->m_pkthdr.len <= dmat->maxsize) {
493 vm_offset_t lastaddr = 0;
496 for (m = m0; m != NULL && error == 0; m = m->m_next) {
498 error = bus_dmamap_load_buffer(dmat,
499 segs, m->m_data, m->m_len, NULL,
500 flags, &lastaddr, nsegs, first);
513 * Like bus_dmamap_load(), but for uios.
516 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
517 bus_dmamap_callback2_t *callback, void *callback_arg,
520 vm_offset_t lastaddr;
521 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
522 bus_dma_segment_t dm_segments[dmat->nsegments];
524 bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
526 int nsegs, i, error, first;
529 struct thread *td = NULL;
531 resid = uio->uio_resid;
534 if (uio->uio_segflg == UIO_USERSPACE) {
537 ("bus_dmamap_load_uio: USERSPACE but no proc"));
542 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
544 * Now at the first iovec to load. Load each iovec
545 * until we have exhausted the residual count.
548 resid < iov[i].iov_len ? resid : iov[i].iov_len;
549 caddr_t addr = (caddr_t) iov[i].iov_base;
552 error = bus_dmamap_load_buffer(dmat, dm_segments, addr,
553 minlen, td, flags, &lastaddr, &nsegs, first);
563 * force "no valid mappings" on error in callback.
565 (*callback)(callback_arg, dm_segments, 0, 0, error);
567 (*callback)(callback_arg, dm_segments, nsegs+1,
568 uio->uio_resid, error);
575 * Release the mapping held by map. A no-op on PowerPC.
578 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
585 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)