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));