2 * SPDX-License-Identifier: BSD-2-Clause
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>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/kernel.h>
43 #include <sys/memdesc.h>
44 #include <sys/mutex.h>
46 #include <sys/vmmeter.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_phys.h>
53 #include <machine/bus.h>
54 #include <x86/include/busdma_impl.h>
57 * Return true if a match is made.
59 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
61 * If paddr is within the bounds of the dma tag then call the filter callback
62 * to check for a match, if there is no filter callback then assume a match.
65 bus_dma_run_filter(struct bus_dma_tag_common *tc, vm_paddr_t paddr)
71 if ((paddr >= BUS_SPACE_MAXADDR ||
72 (paddr > tc->lowaddr && paddr <= tc->highaddr) ||
73 !vm_addr_align_ok(paddr, tc->alignment)) &&
74 (tc->filter == NULL ||
75 (*tc->filter)(tc->filterarg, paddr) != 0))
79 } while (retval == 0 && tc != NULL);
84 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
85 bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
86 bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
87 bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
88 bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
91 struct bus_dma_tag_common *common;
93 KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
94 /* Basic sanity checking */
95 if (boundary != 0 && boundary < maxsegsz)
99 /* Return a NULL tag on failure */
102 newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
103 if (newtag == NULL) {
104 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
105 __func__, newtag, 0, ENOMEM);
110 common->impl = &bus_dma_bounce_impl;
111 common->parent = parent;
112 common->alignment = alignment;
113 common->boundary = boundary;
114 common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
115 common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
116 common->filter = filter;
117 common->filterarg = filterarg;
118 common->maxsize = maxsize;
119 common->nsegments = nsegments;
120 common->maxsegsz = maxsegsz;
121 common->flags = flags;
122 common->ref_count = 1; /* Count ourself */
123 if (lockfunc != NULL) {
124 common->lockfunc = lockfunc;
125 common->lockfuncarg = lockfuncarg;
127 common->lockfunc = _busdma_dflt_lock;
128 common->lockfuncarg = NULL;
131 /* Take into account any restrictions imposed by our parent tag */
132 if (parent != NULL) {
133 common->impl = parent->impl;
134 common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
135 common->highaddr = MAX(parent->highaddr, common->highaddr);
136 if (common->boundary == 0)
137 common->boundary = parent->boundary;
138 else if (parent->boundary != 0) {
139 common->boundary = MIN(parent->boundary,
142 if (common->filter == NULL) {
144 * Short circuit looking at our parent directly
145 * since we have encapsulated all of its information
147 common->filter = parent->filter;
148 common->filterarg = parent->filterarg;
149 common->parent = parent->parent;
151 common->domain = parent->domain;
152 atomic_add_int(&parent->ref_count, 1);
154 common->domain = vm_phys_domain_match(common->domain, 0ul,
161 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
163 struct bus_dma_tag_common *tc;
165 tc = (struct bus_dma_tag_common *)dmat;
166 domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
167 /* Only call the callback if it changes. */
168 if (domain == tc->domain)
171 return (tc->impl->tag_set_domain(dmat));
175 * Allocate a device specific dma_tag.
178 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
179 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
180 bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
181 int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
182 void *lockfuncarg, bus_dma_tag_t *dmat)
184 struct bus_dma_tag_common *tc;
187 if (parent == NULL) {
188 error = bus_dma_bounce_impl.tag_create(parent, alignment,
189 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
190 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
192 tc = (struct bus_dma_tag_common *)parent;
193 error = tc->impl->tag_create(parent, alignment,
194 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
195 nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
201 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
203 struct bus_dma_tag_common *common;
205 if (t == NULL || dmat == NULL)
208 common = (struct bus_dma_tag_common *)dmat;
210 t->parent = (bus_dma_tag_t)common->parent;
211 t->alignment = common->alignment;
212 t->boundary = common->boundary;
213 t->lowaddr = common->lowaddr;
214 t->highaddr = common->highaddr;
215 t->maxsize = common->maxsize;
216 t->nsegments = common->nsegments;
217 t->maxsegsize = common->maxsegsz;
218 t->flags = common->flags;
219 t->lockfunc = common->lockfunc;
220 t->lockfuncarg = common->lockfuncarg;
224 bus_dma_tag_destroy(bus_dma_tag_t dmat)
226 struct bus_dma_tag_common *tc;
228 tc = (struct bus_dma_tag_common *)dmat;
229 return (tc->impl->tag_destroy(dmat));