xref: /freebsd/sys/x86/x86/busdma_machdep.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5  * Copyright (c) 2013 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_acpi.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/ktr.h>
44 #include <sys/lock.h>
45 #include <sys/memdesc.h>
46 #include <sys/mutex.h>
47 #include <sys/uio.h>
48 #include <sys/vmmeter.h>
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_phys.h>
54 #include <vm/pmap.h>
55 #include <machine/bus.h>
56 #include <x86/include/busdma_impl.h>
57 
58 /*
59  * Convenience function for manipulating driver locks from busdma (during
60  * busdma_swi, for example).  Drivers that don't provide their own locks
61  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
62  * non-mutex locking scheme don't have to use this at all.
63  */
64 void
65 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
66 {
67 	struct mtx *dmtx;
68 
69 	dmtx = (struct mtx *)arg;
70 	switch (op) {
71 	case BUS_DMA_LOCK:
72 		mtx_lock(dmtx);
73 		break;
74 	case BUS_DMA_UNLOCK:
75 		mtx_unlock(dmtx);
76 		break;
77 	default:
78 		panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
79 	}
80 }
81 
82 /*
83  * dflt_lock should never get called.  It gets put into the dma tag when
84  * lockfunc == NULL, which is only valid if the maps that are associated
85  * with the tag are meant to never be defered.
86  * XXX Should have a way to identify which driver is responsible here.
87  */
88 void
89 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
90 {
91 
92 	panic("driver error: busdma dflt_lock called");
93 }
94 
95 /*
96  * Return true if a match is made.
97  *
98  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
99  *
100  * If paddr is within the bounds of the dma tag then call the filter callback
101  * to check for a match, if there is no filter callback then assume a match.
102  */
103 int
104 bus_dma_run_filter(struct bus_dma_tag_common *tc, vm_paddr_t paddr)
105 {
106 	int retval;
107 
108 	retval = 0;
109 	do {
110 		if ((paddr >= BUS_SPACE_MAXADDR ||
111 		    (paddr > tc->lowaddr && paddr <= tc->highaddr) ||
112 		    (paddr & (tc->alignment - 1)) != 0) &&
113 		    (tc->filter == NULL ||
114 		    (*tc->filter)(tc->filterarg, paddr) != 0))
115 			retval = 1;
116 
117 		tc = tc->parent;
118 	} while (retval == 0 && tc != NULL);
119 	return (retval);
120 }
121 
122 int
123 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
124     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
125     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
126     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
127     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
128 {
129 	void *newtag;
130 	struct bus_dma_tag_common *common;
131 
132 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
133 	/* Basic sanity checking */
134 	if (boundary != 0 && boundary < maxsegsz)
135 		maxsegsz = boundary;
136 	if (maxsegsz == 0)
137 		return (EINVAL);
138 	/* Return a NULL tag on failure */
139 	*dmat = NULL;
140 
141 	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
142 	if (newtag == NULL) {
143 		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
144 		    __func__, newtag, 0, ENOMEM);
145 		return (ENOMEM);
146 	}
147 
148 	common = newtag;
149 	common->impl = &bus_dma_bounce_impl;
150 	common->parent = parent;
151 	common->alignment = alignment;
152 	common->boundary = boundary;
153 	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
154 	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
155 	common->filter = filter;
156 	common->filterarg = filterarg;
157 	common->maxsize = maxsize;
158 	common->nsegments = nsegments;
159 	common->maxsegsz = maxsegsz;
160 	common->flags = flags;
161 	common->ref_count = 1; /* Count ourself */
162 	if (lockfunc != NULL) {
163 		common->lockfunc = lockfunc;
164 		common->lockfuncarg = lockfuncarg;
165 	} else {
166 		common->lockfunc = bus_dma_dflt_lock;
167 		common->lockfuncarg = NULL;
168 	}
169 
170 	/* Take into account any restrictions imposed by our parent tag */
171 	if (parent != NULL) {
172 		common->impl = parent->impl;
173 		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
174 		common->highaddr = MAX(parent->highaddr, common->highaddr);
175 		if (common->boundary == 0)
176 			common->boundary = parent->boundary;
177 		else if (parent->boundary != 0) {
178 			common->boundary = MIN(parent->boundary,
179 			    common->boundary);
180 		}
181 		if (common->filter == NULL) {
182 			/*
183 			 * Short circuit looking at our parent directly
184 			 * since we have encapsulated all of its information
185 			 */
186 			common->filter = parent->filter;
187 			common->filterarg = parent->filterarg;
188 			common->parent = parent->parent;
189 		}
190 		common->domain = parent->domain;
191 		atomic_add_int(&parent->ref_count, 1);
192 	}
193 	common->domain = vm_phys_domain_match(common->domain, 0ul,
194 	    common->lowaddr);
195 	*dmat = common;
196 	return (0);
197 }
198 
199 int
200 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
201 {
202 	struct bus_dma_tag_common *tc;
203 
204 	tc = (struct bus_dma_tag_common *)dmat;
205 	domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
206 	/* Only call the callback if it changes. */
207 	if (domain == tc->domain)
208 		return (0);
209 	tc->domain = domain;
210 	return (tc->impl->tag_set_domain(dmat));
211 }
212 
213 /*
214  * Allocate a device specific dma_tag.
215  */
216 int
217 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
218     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
219     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
220     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
221     void *lockfuncarg, bus_dma_tag_t *dmat)
222 {
223 	struct bus_dma_tag_common *tc;
224 	int error;
225 
226 	if (parent == NULL) {
227 		error = bus_dma_bounce_impl.tag_create(parent, alignment,
228 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
229 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
230 	} else {
231 		tc = (struct bus_dma_tag_common *)parent;
232 		error = tc->impl->tag_create(parent, alignment,
233 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
234 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
235 	}
236 	return (error);
237 }
238 
239 void
240 bus_dma_template_init(bus_dma_tag_template_t *t, bus_dma_tag_t parent)
241 {
242 
243 	if (t == NULL)
244 		return;
245 
246 	t->parent = parent;
247 	t->alignment = 1;
248 	t->boundary = 0;
249 	t->lowaddr = t->highaddr = BUS_SPACE_MAXADDR;
250 	t->maxsize = t->maxsegsize = BUS_SPACE_MAXSIZE;
251 	t->nsegments = BUS_SPACE_UNRESTRICTED;
252 	t->lockfunc = NULL;
253 	t->lockfuncarg = NULL;
254 	t->flags = 0;
255 }
256 
257 int
258 bus_dma_template_tag(bus_dma_tag_template_t *t, bus_dma_tag_t *dmat)
259 {
260 
261 	if (t == NULL || dmat == NULL)
262 		return (EINVAL);
263 
264 	return (bus_dma_tag_create(t->parent, t->alignment, t->boundary,
265 	    t->lowaddr, t->highaddr, NULL, NULL, t->maxsize,
266 	    t->nsegments, t->maxsegsize, t->flags, t->lockfunc, t->lockfuncarg,
267 	    dmat));
268 }
269 
270 void
271 bus_dma_template_clone(bus_dma_tag_template_t *t, bus_dma_tag_t dmat)
272 {
273 	struct bus_dma_tag_common *common;
274 
275 	if (t == NULL || dmat == NULL)
276 		return;
277 
278 	common = (struct bus_dma_tag_common *)dmat;
279 
280 	t->parent = (bus_dma_tag_t)common->parent;
281 	t->alignment = common->alignment;
282 	t->boundary = common->boundary;
283 	t->lowaddr = common->lowaddr;
284 	t->highaddr = common->highaddr;
285 	t->maxsize = common->maxsize;
286 	t->nsegments = common->nsegments;
287 	t->maxsegsize = common->maxsegsz;
288 	t->flags = common->flags;
289 	t->lockfunc = common->lockfunc;
290 	t->lockfuncarg = common->lockfuncarg;
291 }
292 
293 int
294 bus_dma_tag_destroy(bus_dma_tag_t dmat)
295 {
296 	struct bus_dma_tag_common *tc;
297 
298 	tc = (struct bus_dma_tag_common *)dmat;
299 	return (tc->impl->tag_destroy(dmat));
300 }
301 
302 #ifndef ACPI_DMAR
303 bool
304 bus_dma_dmar_set_buswide(device_t dev)
305 {
306 	return (false);
307 }
308 
309 int
310 bus_dma_dmar_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,
311     vm_paddr_t start, vm_size_t length, int flags)
312 {
313 	return (0);
314 }
315 #endif
316