xref: /NextBSD/sys/arm64/arm64/busdma_machdep.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * Copyright (c) 1997, 1998 Justin T. Gibbs.
3  * Copyright (c) 2013, 2015 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Portions of this software were developed by Semihalf
10  * under sponsorship of the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification, immediately at the beginning of the file.
18  * 2. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/ktr.h>
43 #include <sys/lock.h>
44 #include <sys/memdesc.h>
45 #include <sys/mutex.h>
46 #include <sys/uio.h>
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/pmap.h>
50 
51 #include <machine/bus.h>
52 #include <arm64/include/bus_dma_impl.h>
53 
54 /*
55  * Convenience function for manipulating driver locks from busdma (during
56  * busdma_swi, for example).  Drivers that don't provide their own locks
57  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
58  * non-mutex locking scheme don't have to use this at all.
59  */
60 void
busdma_lock_mutex(void * arg,bus_dma_lock_op_t op)61 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
62 {
63 	struct mtx *dmtx;
64 
65 	dmtx = (struct mtx *)arg;
66 	switch (op) {
67 	case BUS_DMA_LOCK:
68 		mtx_lock(dmtx);
69 		break;
70 	case BUS_DMA_UNLOCK:
71 		mtx_unlock(dmtx);
72 		break;
73 	default:
74 		panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
75 	}
76 }
77 
78 /*
79  * dflt_lock should never get called.  It gets put into the dma tag when
80  * lockfunc == NULL, which is only valid if the maps that are associated
81  * with the tag are meant to never be defered.
82  * XXX Should have a way to identify which driver is responsible here.
83  */
84 void
bus_dma_dflt_lock(void * arg,bus_dma_lock_op_t op)85 bus_dma_dflt_lock(void *arg, bus_dma_lock_op_t op)
86 {
87 
88 	panic("driver error: busdma dflt_lock called");
89 }
90 
91 /*
92  * Return true if a match is made.
93  *
94  * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
95  *
96  * If paddr is within the bounds of the dma tag then call the filter callback
97  * to check for a match, if there is no filter callback then assume a match.
98  */
99 int
bus_dma_run_filter(struct bus_dma_tag_common * tc,bus_addr_t paddr)100 bus_dma_run_filter(struct bus_dma_tag_common *tc, bus_addr_t paddr)
101 {
102 	int retval;
103 
104 	retval = 0;
105 	do {
106 		if (((paddr > tc->lowaddr && paddr <= tc->highaddr) ||
107 		    ((paddr & (tc->alignment - 1)) != 0)) &&
108 		    (tc->filter == NULL ||
109 		    (*tc->filter)(tc->filterarg, paddr) != 0))
110 			retval = 1;
111 
112 		tc = tc->parent;
113 	} while (retval == 0 && tc != NULL);
114 	return (retval);
115 }
116 
117 int
common_bus_dma_tag_create(struct bus_dma_tag_common * parent,bus_size_t alignment,bus_addr_t boundary,bus_addr_t lowaddr,bus_addr_t highaddr,bus_dma_filter_t * filter,void * filterarg,bus_size_t maxsize,int nsegments,bus_size_t maxsegsz,int flags,bus_dma_lock_t * lockfunc,void * lockfuncarg,size_t sz,void ** dmat)118 common_bus_dma_tag_create(struct bus_dma_tag_common *parent,
119     bus_size_t alignment, bus_addr_t boundary, bus_addr_t lowaddr,
120     bus_addr_t highaddr, bus_dma_filter_t *filter, void *filterarg,
121     bus_size_t maxsize, int nsegments, bus_size_t maxsegsz, int flags,
122     bus_dma_lock_t *lockfunc, void *lockfuncarg, size_t sz, void **dmat)
123 {
124 	void *newtag;
125 	struct bus_dma_tag_common *common;
126 
127 	KASSERT(sz >= sizeof(struct bus_dma_tag_common), ("sz"));
128 	/* Return a NULL tag on failure */
129 	*dmat = NULL;
130 	/* Basic sanity checking */
131 	if (boundary != 0 && boundary < maxsegsz)
132 		maxsegsz = boundary;
133 	if (maxsegsz == 0)
134 		return (EINVAL);
135 
136 	newtag = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
137 	if (newtag == NULL) {
138 		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
139 		    __func__, newtag, 0, ENOMEM);
140 		return (ENOMEM);
141 	}
142 
143 	common = newtag;
144 	common->impl = &bus_dma_bounce_impl;
145 	common->parent = parent;
146 	common->alignment = alignment;
147 	common->boundary = boundary;
148 	common->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
149 	common->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
150 	common->filter = filter;
151 	common->filterarg = filterarg;
152 	common->maxsize = maxsize;
153 	common->nsegments = nsegments;
154 	common->maxsegsz = maxsegsz;
155 	common->flags = flags;
156 	common->ref_count = 1; /* Count ourself */
157 	if (lockfunc != NULL) {
158 		common->lockfunc = lockfunc;
159 		common->lockfuncarg = lockfuncarg;
160 	} else {
161 		common->lockfunc = bus_dma_dflt_lock;
162 		common->lockfuncarg = NULL;
163 	}
164 
165 	/* Take into account any restrictions imposed by our parent tag */
166 	if (parent != NULL) {
167 		common->impl = parent->impl;
168 		common->lowaddr = MIN(parent->lowaddr, common->lowaddr);
169 		common->highaddr = MAX(parent->highaddr, common->highaddr);
170 		if (common->boundary == 0)
171 			common->boundary = parent->boundary;
172 		else if (parent->boundary != 0) {
173 			common->boundary = MIN(parent->boundary,
174 			    common->boundary);
175 		}
176 		if (common->filter == NULL) {
177 			/*
178 			 * Short circuit looking at our parent directly
179 			 * since we have encapsulated all of its information
180 			 */
181 			common->filter = parent->filter;
182 			common->filterarg = parent->filterarg;
183 			common->parent = parent->parent;
184 		}
185 		atomic_add_int(&parent->ref_count, 1);
186 	}
187 	*dmat = common;
188 	return (0);
189 }
190 
191 /*
192  * Allocate a device specific dma_tag.
193  */
194 int
bus_dma_tag_create(bus_dma_tag_t parent,bus_size_t alignment,bus_addr_t boundary,bus_addr_t lowaddr,bus_addr_t highaddr,bus_dma_filter_t * filter,void * filterarg,bus_size_t maxsize,int nsegments,bus_size_t maxsegsz,int flags,bus_dma_lock_t * lockfunc,void * lockfuncarg,bus_dma_tag_t * dmat)195 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
196     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
197     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
198     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
199     void *lockfuncarg, bus_dma_tag_t *dmat)
200 {
201 	struct bus_dma_tag_common *tc;
202 	int error;
203 
204 	if (parent == NULL) {
205 		error = bus_dma_bounce_impl.tag_create(parent, alignment,
206 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
207 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
208 	} else {
209 		tc = (struct bus_dma_tag_common *)parent;
210 		error = tc->impl->tag_create(parent, alignment,
211 		    boundary, lowaddr, highaddr, filter, filterarg, maxsize,
212 		    nsegments, maxsegsz, flags, lockfunc, lockfuncarg, dmat);
213 	}
214 	return (error);
215 }
216 
217 int
bus_dma_tag_destroy(bus_dma_tag_t dmat)218 bus_dma_tag_destroy(bus_dma_tag_t dmat)
219 {
220 	struct bus_dma_tag_common *tc;
221 
222 	tc = (struct bus_dma_tag_common *)dmat;
223 	return (tc->impl->tag_destroy(dmat));
224 }
225 
226 /*
227  * Allocate a handle for mapping from kva/uva/physical
228  * address space into bus device space.
229  */
230 int
bus_dmamap_create(bus_dma_tag_t dmat,int flags,bus_dmamap_t * mapp)231 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
232 {
233 	struct bus_dma_tag_common *tc;
234 
235 	tc = (struct bus_dma_tag_common *)dmat;
236 	return (tc->impl->map_create(dmat, flags, mapp));
237 }
238 
239 /*
240  * Destroy a handle for mapping from kva/uva/physical
241  * address space into bus device space.
242  */
243 int
bus_dmamap_destroy(bus_dma_tag_t dmat,bus_dmamap_t map)244 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
245 {
246 	struct bus_dma_tag_common *tc;
247 
248 	tc = (struct bus_dma_tag_common *)dmat;
249 	return (tc->impl->map_destroy(dmat, map));
250 }
251 
252 
253 /*
254  * Allocate a piece of memory that can be efficiently mapped into
255  * bus device space based on the constraints listed in the dma tag.
256  * A dmamap to for use with dmamap_load is also allocated.
257  */
258 int
bus_dmamem_alloc(bus_dma_tag_t dmat,void ** vaddr,int flags,bus_dmamap_t * mapp)259 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
260     bus_dmamap_t *mapp)
261 {
262 	struct bus_dma_tag_common *tc;
263 
264 	tc = (struct bus_dma_tag_common *)dmat;
265 	return (tc->impl->mem_alloc(dmat, vaddr, flags, mapp));
266 }
267 
268 /*
269  * Free a piece of memory and it's allociated dmamap, that was allocated
270  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
271  */
272 void
bus_dmamem_free(bus_dma_tag_t dmat,void * vaddr,bus_dmamap_t map)273 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
274 {
275 	struct bus_dma_tag_common *tc;
276 
277 	tc = (struct bus_dma_tag_common *)dmat;
278 	tc->impl->mem_free(dmat, vaddr, map);
279 }
280 
281 int
_bus_dmamap_load_phys(bus_dma_tag_t dmat,bus_dmamap_t map,vm_paddr_t buf,bus_size_t buflen,int flags,bus_dma_segment_t * segs,int * segp)282 _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
283     bus_size_t buflen, int flags, bus_dma_segment_t *segs, int *segp)
284 {
285 	struct bus_dma_tag_common *tc;
286 
287 	tc = (struct bus_dma_tag_common *)dmat;
288 	return (tc->impl->load_phys(dmat, map, buf, buflen, flags, segs,
289 	    segp));
290 }
291 
292 int
_bus_dmamap_load_ma(bus_dma_tag_t dmat,bus_dmamap_t map,struct vm_page ** ma,bus_size_t tlen,int ma_offs,int flags,bus_dma_segment_t * segs,int * segp)293 _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map, struct vm_page **ma,
294     bus_size_t tlen, int ma_offs, int flags, bus_dma_segment_t *segs,
295     int *segp)
296 {
297 	struct bus_dma_tag_common *tc;
298 
299 	tc = (struct bus_dma_tag_common *)dmat;
300 	return (tc->impl->load_ma(dmat, map, ma, tlen, ma_offs, flags,
301 	    segs, segp));
302 }
303 
304 int
_bus_dmamap_load_buffer(bus_dma_tag_t dmat,bus_dmamap_t map,void * buf,bus_size_t buflen,pmap_t pmap,int flags,bus_dma_segment_t * segs,int * segp)305 _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
306     bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
307     int *segp)
308 {
309 	struct bus_dma_tag_common *tc;
310 
311 	tc = (struct bus_dma_tag_common *)dmat;
312 	return (tc->impl->load_buffer(dmat, map, buf, buflen, pmap, flags, segs,
313 	    segp));
314 }
315 
316 void
__bus_dmamap_waitok(bus_dma_tag_t dmat,bus_dmamap_t map,struct memdesc * mem,bus_dmamap_callback_t * callback,void * callback_arg)317 __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
318     struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
319 {
320 	struct bus_dma_tag_common *tc;
321 
322 	tc = (struct bus_dma_tag_common *)dmat;
323 	tc->impl->map_waitok(dmat, map, mem, callback, callback_arg);
324 }
325 
326 bus_dma_segment_t *
_bus_dmamap_complete(bus_dma_tag_t dmat,bus_dmamap_t map,bus_dma_segment_t * segs,int nsegs,int error)327 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
328     bus_dma_segment_t *segs, int nsegs, int error)
329 {
330 	struct bus_dma_tag_common *tc;
331 
332 	tc = (struct bus_dma_tag_common *)dmat;
333 	return (tc->impl->map_complete(dmat, map, segs, nsegs, error));
334 }
335 
336 /*
337  * Release the mapping held by map.
338  */
339 void
_bus_dmamap_unload(bus_dma_tag_t dmat,bus_dmamap_t map)340 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
341 {
342 	struct bus_dma_tag_common *tc;
343 
344 	tc = (struct bus_dma_tag_common *)dmat;
345 	tc->impl->map_unload(dmat, map);
346 }
347 
348 void
_bus_dmamap_sync(bus_dma_tag_t dmat,bus_dmamap_t map,bus_dmasync_op_t op)349 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
350 {
351 	struct bus_dma_tag_common *tc;
352 
353 	tc = (struct bus_dma_tag_common *)dmat;
354 	tc->impl->map_sync(dmat, map, op);
355 }
356