xref: /freebsd/sys/x86/x86/busdma_machdep.c (revision b00ab754)
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 <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  * Convenience function for manipulating driver locks from busdma (during
58  * busdma_swi, for example).  Drivers that don't provide their own locks
59  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
60  * non-mutex locking scheme don't have to use this at all.
61  */
62 void
63 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
64 {
65 	struct mtx *dmtx;
66 
67 	dmtx = (struct mtx *)arg;
68 	switch (op) {
69 	case BUS_DMA_LOCK:
70 		mtx_lock(dmtx);
71 		break;
72 	case BUS_DMA_UNLOCK:
73 		mtx_unlock(dmtx);
74 		break;
75 	default:
76 		panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
77 	}
78 }
79 
80 /*
81  * dflt_lock should never get called.  It gets put into the dma tag when
82  * lockfunc == NULL, which is only valid if the maps that are associated
83  * with the tag are meant to never be defered.
84  * XXX Should have a way to identify which driver is responsible here.
85  */
86 void
87 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
88 {
89 
90 	panic("driver error: busdma dflt_lock called");
91 }
92 
93 /*
94  * Return true if a match is made.
95  *
96  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
97  *
98  * If paddr is within the bounds of the dma tag then call the filter callback
99  * to check for a match, if there is no filter callback then assume a match.
100  */
101 int
102 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
103 {
104 	int retval;
105 
106 	retval = 0;
107 	do {
108 		if (((paddr > tc->lowaddr && paddr <= tc->highaddr) ||
109 		    ((paddr & (tc->alignment - 1)) != 0)) &&
110 		    (tc->filter == NULL ||
111 		    (*tc->filter)(tc->filterarg, paddr) != 0))
112 			retval = 1;
113 
114 		tc = tc->parent;
115 	} while (retval == 0 && tc != NULL);
116 	return (retval);
117 }
118 
119 int
120 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
121     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
122     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
123     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
124     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
125 {
126 	void *newtag;
127 	struct bus_dma_tag_common *common;
128 
129 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
130 	/* Basic sanity checking */
131 	if (boundary != 0 && boundary < maxsegsz)
132 		maxsegsz = boundary;
133 	if (maxsegsz == 0)
134 		return (EINVAL);
135 	/* Return a NULL tag on failure */
136 	*dmat = NULL;
137 
138 	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
139 	if (newtag == NULL) {
140 		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
141 		    __func__, newtag, 0, ENOMEM);
142 		return (ENOMEM);
143 	}
144 
145 	common = newtag;
146 	common->impl = &bus_dma_bounce_impl;
147 	common->parent = parent;
148 	common->alignment = alignment;
149 	common->boundary = boundary;
150 	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
151 	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
152 	common->filter = filter;
153 	common->filterarg = filterarg;
154 	common->maxsize = maxsize;
155 	common->nsegments = nsegments;
156 	common->maxsegsz = maxsegsz;
157 	common->flags = flags;
158 	common->ref_count = 1; /* Count ourself */
159 	if (lockfunc != NULL) {
160 		common->lockfunc = lockfunc;
161 		common->lockfuncarg = lockfuncarg;
162 	} else {
163 		common->lockfunc = bus_dma_dflt_lock;
164 		common->lockfuncarg = NULL;
165 	}
166 
167 	/* Take into account any restrictions imposed by our parent tag */
168 	if (parent != NULL) {
169 		common->impl = parent->impl;
170 		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
171 		common->highaddr = MAX(parent->highaddr, common->highaddr);
172 		if (common->boundary == 0)
173 			common->boundary = parent->boundary;
174 		else if (parent->boundary != 0) {
175 			common->boundary = MIN(parent->boundary,
176 			    common->boundary);
177 		}
178 		if (common->filter == NULL) {
179 			/*
180 			 * Short circuit looking at our parent directly
181 			 * since we have encapsulated all of its information
182 			 */
183 			common->filter = parent->filter;
184 			common->filterarg = parent->filterarg;
185 			common->parent = parent->parent;
186 		}
187 		common->domain = parent->domain;
188 		atomic_add_int(&parent->ref_count, 1);
189 	}
190 	common->domain = vm_phys_domain_match(common->domain, 0ul,
191 	    common->lowaddr);
192 	*dmat = common;
193 	return (0);
194 }
195 
196 int
197 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
198 {
199 	struct bus_dma_tag_common *tc;
200 
201 	tc = (struct bus_dma_tag_common *)dmat;
202 	domain = vm_phys_domain_match(domain, 0ul, tc->lowaddr);
203 	/* Only call the callback if it changes. */
204 	if (domain == tc->domain)
205 		return (0);
206 	tc->domain = domain;
207 	return (tc->impl->tag_set_domain(dmat));
208 }
209 
210 /*
211  * Allocate a device specific dma_tag.
212  */
213 int
214 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
215     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
216     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
217     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
218     void *lockfuncarg, bus_dma_tag_t *dmat)
219 {
220 	struct bus_dma_tag_common *tc;
221 	int error;
222 
223 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s", __func__);
224 
225 	if (parent == NULL) {
226 		error = bus_dma_bounce_impl.tag_create(parent, alignment,
227 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
228 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
229 	} else {
230 		tc = (struct bus_dma_tag_common *)parent;
231 		error = tc->impl->tag_create(parent, alignment,
232 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
233 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
234 	}
235 	return (error);
236 }
237 
238 int
239 bus_dma_tag_destroy(bus_dma_tag_t dmat)
240 {
241 	struct bus_dma_tag_common *tc;
242 
243 	tc = (struct bus_dma_tag_common *)dmat;
244 	return (tc->impl->tag_destroy(dmat));
245 }
246 
247