2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * Copyright (c) 2013 The FreeBSD Foundation
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/memdesc.h>
42 #include <sys/mutex.h>
45 #include <vm/vm_extern.h>
47 #include <machine/bus.h>
48 #include <x86/include/busdma_impl.h>
51 * Convenience function for manipulating driver locks from busdma (during
52 * busdma_swi, for example). Drivers that don't provide their own locks
53 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
54 * non-mutex locking scheme don't have to use this at all.
57 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
61 dmtx = (struct mtx *)arg;
70 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
75 * dflt_lock should never get called. It gets put into the dma tag when
76 * lockfunc == NULL, which is only valid if the maps that are associated
77 * with the tag are meant to never be defered.
78 * XXX Should have a way to identify which driver is responsible here.
81 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
84 panic("driver error: busdma dflt_lock called");
88 * Return true if a match is made.
90 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
92 * If paddr is within the bounds of the dma tag then call the filter callback
93 * to check for a match, if there is no filter callback then assume a match.
96 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
102 if (((paddr > tc->lowaddr && paddr <= tc->highaddr) ||
103 ((paddr & (tc->alignment - 1)) != 0)) &&
104 (tc->filter == NULL ||
105 (*tc->filter)(tc->filterarg, paddr) != 0))
109 } while (retval == 0 && tc != NULL);
114 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
115 bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
116 bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
117 bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
118 bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
121 struct bus_dma_tag_common *common;
123 KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
124 /* Basic sanity checking */
125 if (boundary != 0 && boundary < maxsegsz)
129 /* Return a NULL tag on failure */
132 newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
133 if (newtag == NULL) {
134 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
135 __func__, newtag, 0, ENOMEM);
140 common->impl = &bus_dma_bounce_impl;
141 common->parent = parent;
142 common->alignment = alignment;
143 common->boundary = boundary;
144 common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
145 common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
146 common->filter = filter;
147 common->filterarg = filterarg;
148 common->maxsize = maxsize;
149 common->nsegments = nsegments;
150 common->maxsegsz = maxsegsz;
151 common->flags = flags;
152 common->ref_count = 1; /* Count ourself */
153 if (lockfunc != NULL) {
154 common->lockfunc = lockfunc;
155 common->lockfuncarg = lockfuncarg;
157 common->lockfunc = bus_dma_dflt_lock;
158 common->lockfuncarg = NULL;
161 /* Take into account any restrictions imposed by our parent tag */
162 if (parent != NULL) {
163 common->impl = parent->impl;
164 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
165 common->highaddr = MAX(parent->highaddr, common->highaddr);
166 if (common->boundary == 0)
167 common->boundary = parent->boundary;
168 else if (parent->boundary != 0) {
169 common->boundary = MIN(parent->boundary,
172 if (common->filter == NULL) {
174 * Short circuit looking at our parent directly
175 * since we have encapsulated all of its information
177 common->filter = parent->filter;
178 common->filterarg = parent->filterarg;
179 common->parent = parent->parent;
181 atomic_add_int(&parent->ref_count, 1);
188 * Allocate a device specific dma_tag.
191 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
192 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
193 bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
194 int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
195 void *lockfuncarg, bus_dma_tag_t *dmat)
197 struct bus_dma_tag_common *tc;
200 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s", __func__);
202 if (parent == NULL) {
203 error = bus_dma_bounce_impl.tag_create(parent, alignment,
204 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
205 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
207 tc = (struct bus_dma_tag_common *)parent;
208 error = tc->impl->tag_create(parent, alignment,
209 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
210 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
216 bus_dma_tag_destroy(bus_dma_tag_t dmat)
218 struct bus_dma_tag_common *tc;
220 tc = (struct bus_dma_tag_common *)dmat;
221 return (tc->impl->tag_destroy(dmat));
225 * Allocate a handle for mapping from kva/uva/physical
226 * address space into bus device space.
229 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
231 struct bus_dma_tag_common *tc;
233 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s", __func__);
235 tc = (struct bus_dma_tag_common *)dmat;
236 return (tc->impl->map_create(dmat, flags, mapp));
240 * Destroy a handle for mapping from kva/uva/physical
241 * address space into bus device space.
244 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
246 struct bus_dma_tag_common *tc;
248 tc = (struct bus_dma_tag_common *)dmat;
249 return (tc->impl->map_destroy(dmat, map));
254 * Allocate a piece of memory that can be efficiently mapped into
255 * bus device space based on the constraints lited in the dma tag.
256 * A dmamap to for use with dmamap_load is also allocated.
259 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
262 struct bus_dma_tag_common *tc;
264 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s", __func__);
266 tc = (struct bus_dma_tag_common *)dmat;
267 return (tc->impl->mem_alloc(dmat, vaddr, flags, mapp));
271 * Free a piece of memory and it's allociated dmamap, that was allocated
272 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
275 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
277 struct bus_dma_tag_common *tc;
279 tc = (struct bus_dma_tag_common *)dmat;
280 tc->impl->mem_free(dmat, vaddr, map);
284 * Utility function to load a physical buffer. segp contains
285 * the starting segment on entrace, and the ending segment on exit.
288 _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
289 bus_size_t buflen, int flags, bus_dma_segment_t *segs, int *segp)
291 struct bus_dma_tag_common *tc;
293 tc = (struct bus_dma_tag_common *)dmat;
294 return (tc->impl->load_phys(dmat, map, buf, buflen, flags, segs,
299 _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map, struct vm_page **ma,
300 bus_size_t tlen, int ma_offs, int flags, bus_dma_segment_t *segs,
303 struct bus_dma_tag_common *tc;
305 tc = (struct bus_dma_tag_common *)dmat;
306 return (tc->impl->load_ma(dmat, map, ma, tlen, ma_offs, flags,
311 * Utility function to load a linear buffer. segp contains
312 * the starting segment on entrace, and the ending segment on exit.
315 _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
316 bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
319 struct bus_dma_tag_common *tc;
321 tc = (struct bus_dma_tag_common *)dmat;
322 return (tc->impl->load_buffer(dmat, map, buf, buflen, pmap, flags, segs,
327 __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
328 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
330 struct bus_dma_tag_common *tc;
332 tc = (struct bus_dma_tag_common *)dmat;
333 tc->impl->map_waitok(dmat, map, mem, callback, callback_arg);
337 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
338 bus_dma_segment_t *segs, int nsegs, int error)
340 struct bus_dma_tag_common *tc;
342 tc = (struct bus_dma_tag_common *)dmat;
343 return (tc->impl->map_complete(dmat, map, segs, nsegs, error));
347 * Release the mapping held by map.
350 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
352 struct bus_dma_tag_common *tc;
354 tc = (struct bus_dma_tag_common *)dmat;
355 tc->impl->map_unload(dmat, map);
359 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
361 struct bus_dma_tag_common *tc;
363 tc = (struct bus_dma_tag_common *)dmat;
364 tc->impl->map_sync(dmat, map, op);