2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
5 * Copyright (c) 2013 The FreeBSD Foundation
8 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "opt_iommu.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/kernel.h>
46 #include <sys/memdesc.h>
47 #include <sys/mutex.h>
49 #include <sys/vmmeter.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_phys.h>
56 #include <machine/bus.h>
57 #include <x86/include/busdma_impl.h>
60 * Convenience function for manipulating driver locks from busdma (during
61 * busdma_swi, for example). Drivers that don't provide their own locks
62 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
63 * non-mutex locking scheme don't have to use this at all.
66 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
70 dmtx = (struct mtx *)arg;
79 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
84 * dflt_lock should never get called. It gets put into the dma tag when
85 * lockfunc == NULL, which is only valid if the maps that are associated
86 * with the tag are meant to never be defered.
87 * XXX Should have a way to identify which driver is responsible here.
90 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
93 panic("driver error: busdma dflt_lock called");
97 * Return true if a match is made.
99 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
101 * If paddr is within the bounds of the dma tag then call the filter callback
102 * to check for a match, if there is no filter callback then assume a match.
105 bus_dma_run_filter(struct bus_dma_tag_common *tc, vm_paddr_t paddr)
111 if ((paddr >= BUS_SPACE_MAXADDR ||
112 (paddr > tc->lowaddr && paddr <= tc->highaddr) ||
113 (paddr & (tc->alignment - 1)) != 0) &&
114 (tc->filter == NULL ||
115 (*tc->filter)(tc->filterarg, paddr) != 0))
119 } while (retval == 0 && tc != NULL);
124 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
125 bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
126 bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
127 bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
128 bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
131 struct bus_dma_tag_common *common;
133 KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
134 /* Basic sanity checking */
135 if (boundary != 0 && boundary < maxsegsz)
139 /* Return a NULL tag on failure */
142 newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
143 if (newtag == NULL) {
144 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
145 __func__, newtag, 0, ENOMEM);
150 common->impl = &bus_dma_bounce_impl;
151 common->parent = parent;
152 common->alignment = alignment;
153 common->boundary = boundary;
154 common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
155 common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
156 common->filter = filter;
157 common->filterarg = filterarg;
158 common->maxsize = maxsize;
159 common->nsegments = nsegments;
160 common->maxsegsz = maxsegsz;
161 common->flags = flags;
162 common->ref_count = 1; /* Count ourself */
163 if (lockfunc != NULL) {
164 common->lockfunc = lockfunc;
165 common->lockfuncarg = lockfuncarg;
167 common->lockfunc = bus_dma_dflt_lock;
168 common->lockfuncarg = NULL;
171 /* Take into account any restrictions imposed by our parent tag */
172 if (parent != NULL) {
173 common->impl = parent->impl;
174 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
175 common->highaddr = MAX(parent->highaddr, common->highaddr);
176 if (common->boundary == 0)
177 common->boundary = parent->boundary;
178 else if (parent->boundary != 0) {
179 common->boundary = MIN(parent->boundary,
182 if (common->filter == NULL) {
184 * Short circuit looking at our parent directly
185 * since we have encapsulated all of its information
187 common->filter = parent->filter;
188 common->filterarg = parent->filterarg;
189 common->parent = parent->parent;
191 common->domain = parent->domain;
192 atomic_add_int(&parent->ref_count, 1);
194 common->domain = vm_phys_domain_match(common->domain, 0ul,
201 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
203 struct bus_dma_tag_common *tc;
205 tc = (struct bus_dma_tag_common *)dmat;
206 domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
207 /* Only call the callback if it changes. */
208 if (domain == tc->domain)
211 return (tc->impl->tag_set_domain(dmat));
215 * Allocate a device specific dma_tag.
218 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
219 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
220 bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
221 int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
222 void *lockfuncarg, bus_dma_tag_t *dmat)
224 struct bus_dma_tag_common *tc;
227 if (parent == NULL) {
228 error = bus_dma_bounce_impl.tag_create(parent, alignment,
229 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
230 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
232 tc = (struct bus_dma_tag_common *)parent;
233 error = tc->impl->tag_create(parent, alignment,
234 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
235 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
241 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
243 struct bus_dma_tag_common *common;
245 if (t == NULL || dmat == NULL)
248 common = (struct bus_dma_tag_common *)dmat;
250 t->parent = (bus_dma_tag_t)common->parent;
251 t->alignment = common->alignment;
252 t->boundary = common->boundary;
253 t->lowaddr = common->lowaddr;
254 t->highaddr = common->highaddr;
255 t->maxsize = common->maxsize;
256 t->nsegments = common->nsegments;
257 t->maxsegsize = common->maxsegsz;
258 t->flags = common->flags;
259 t->lockfunc = common->lockfunc;
260 t->lockfuncarg = common->lockfuncarg;
264 bus_dma_tag_destroy(bus_dma_tag_t dmat)
266 struct bus_dma_tag_common *tc;
268 tc = (struct bus_dma_tag_common *)dmat;
269 return (tc->impl->tag_destroy(dmat));
273 bool bus_dma_iommu_set_buswide(device_t dev);
274 int bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,
275 vm_paddr_t start, vm_size_t length, int flags);
278 bus_dma_iommu_set_buswide(device_t dev)
284 bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,
285 vm_paddr_t start, vm_size_t length, int flags)