xref: /freebsd/sys/x86/x86/busdma_machdep.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_acpi.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/lock.h>
43 #include <sys/memdesc.h>
44 #include <sys/mutex.h>
45 #include <sys/uio.h>
46 #include <sys/vmmeter.h>
47 #include <vm/vm.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>
52 #include <vm/pmap.h>
53 #include <machine/bus.h>
54 #include <x86/include/busdma_impl.h>
55 
56 /*
57  * Return true if a match is made.
58  *
59  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
60  *
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.
63  */
64 int
65 bus_dma_run_filter(struct bus_dma_tag_common *tc, vm_paddr_t paddr)
66 {
67 	int retval;
68 
69 	retval = 0;
70 	do {
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))
76 			retval = 1;
77 
78 		tc = tc->parent;
79 	} while (retval == 0 && tc != NULL);
80 	return (retval);
81 }
82 
83 int
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)
89 {
90 	void *newtag;
91 	struct bus_dma_tag_common *common;
92 
93 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
94 	/* Basic sanity checking */
95 	if (boundary != 0 && boundary < maxsegsz)
96 		maxsegsz = boundary;
97 	if (maxsegsz == 0)
98 		return (EINVAL);
99 	/* Return a NULL tag on failure */
100 	*dmat = NULL;
101 
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);
106 		return (ENOMEM);
107 	}
108 
109 	common = newtag;
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;
126 	} else {
127 		common->lockfunc = _busdma_dflt_lock;
128 		common->lockfuncarg = NULL;
129 	}
130 
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,
140 			    common->boundary);
141 		}
142 		if (common->filter == NULL) {
143 			/*
144 			 * Short circuit looking at our parent directly
145 			 * since we have encapsulated all of its information
146 			 */
147 			common->filter = parent->filter;
148 			common->filterarg = parent->filterarg;
149 			common->parent = parent->parent;
150 		}
151 		common->domain = parent->domain;
152 		atomic_add_int(&parent->ref_count, 1);
153 	}
154 	common->domain = vm_phys_domain_match(common->domain, 0ul,
155 	    common->lowaddr);
156 	*dmat = common;
157 	return (0);
158 }
159 
160 int
161 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
162 {
163 	struct bus_dma_tag_common *tc;
164 
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)
169 		return (0);
170 	tc->domain = domain;
171 	return (tc->impl->tag_set_domain(dmat));
172 }
173 
174 /*
175  * Allocate a device specific dma_tag.
176  */
177 int
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)
183 {
184 	struct bus_dma_tag_common *tc;
185 	int error;
186 
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);
191 	} else {
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);
196 	}
197 	return (error);
198 }
199 
200 void
201 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
202 {
203 	struct bus_dma_tag_common *common;
204 
205 	if (t == NULL || dmat == NULL)
206 		return;
207 
208 	common = (struct bus_dma_tag_common *)dmat;
209 
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;
221 }
222 
223 int
224 bus_dma_tag_destroy(bus_dma_tag_t dmat)
225 {
226 	struct bus_dma_tag_common *tc;
227 
228 	tc = (struct bus_dma_tag_common *)dmat;
229 	return (tc->impl->tag_destroy(dmat));
230 }
231