xref: /netbsd-src/sys/arch/arm/arm32/bus_dma.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /*	$NetBSD: bus_dma.c,v 1.132 2021/02/07 10:47:40 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 1998, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
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  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #define _ARM32_BUS_DMA_PRIVATE
34 
35 #include "opt_arm_bus_space.h"
36 #include "opt_cputypes.h"
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.132 2021/02/07 10:47:40 skrll Exp $");
40 
41 #include <sys/param.h>
42 
43 #include <sys/bus.h>
44 #include <sys/cpu.h>
45 #include <sys/kmem.h>
46 #include <sys/mbuf.h>
47 
48 #include <uvm/uvm.h>
49 
50 #include <arm/cpuconf.h>
51 #include <arm/cpufunc.h>
52 
53 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
54 #include <dev/mm.h>
55 #endif
56 
57 #ifdef BUSDMA_COUNTERS
58 static struct evcnt bus_dma_creates =
59 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "creates");
60 static struct evcnt bus_dma_bounced_creates =
61 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced creates");
62 static struct evcnt bus_dma_loads =
63 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "loads");
64 static struct evcnt bus_dma_bounced_loads =
65 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced loads");
66 static struct evcnt bus_dma_coherent_loads =
67 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "coherent loads");
68 static struct evcnt bus_dma_read_bounces =
69 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "read bounces");
70 static struct evcnt bus_dma_write_bounces =
71 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "write bounces");
72 static struct evcnt bus_dma_bounced_unloads =
73 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced unloads");
74 static struct evcnt bus_dma_unloads =
75 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "unloads");
76 static struct evcnt bus_dma_bounced_destroys =
77 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced destroys");
78 static struct evcnt bus_dma_destroys =
79 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "destroys");
80 static struct evcnt bus_dma_sync_prereadwrite =
81 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prereadwrite");
82 static struct evcnt bus_dma_sync_preread_begin =
83 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread begin");
84 static struct evcnt bus_dma_sync_preread =
85 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread");
86 static struct evcnt bus_dma_sync_preread_tail =
87 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread tail");
88 static struct evcnt bus_dma_sync_prewrite =
89 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prewrite");
90 static struct evcnt bus_dma_sync_postread =
91 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postread");
92 static struct evcnt bus_dma_sync_postreadwrite =
93 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postreadwrite");
94 static struct evcnt bus_dma_sync_postwrite =
95 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postwrite");
96 
97 static struct evcnt bus_dma_sync_coherent_prereadwrite =
98 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent prereadwrite");
99 static struct evcnt bus_dma_sync_coherent_preread =
100 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent preread");
101 static struct evcnt bus_dma_sync_coherent_prewrite =
102 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent prewrite");
103 static struct evcnt bus_dma_sync_coherent_postread =
104 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent postread");
105 static struct evcnt bus_dma_sync_coherent_postreadwrite =
106 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent postreadwrite");
107 static struct evcnt bus_dma_sync_coherent_postwrite =
108 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync coherent postwrite");
109 
110 EVCNT_ATTACH_STATIC(bus_dma_creates);
111 EVCNT_ATTACH_STATIC(bus_dma_bounced_creates);
112 EVCNT_ATTACH_STATIC(bus_dma_loads);
113 EVCNT_ATTACH_STATIC(bus_dma_bounced_loads);
114 EVCNT_ATTACH_STATIC(bus_dma_coherent_loads);
115 EVCNT_ATTACH_STATIC(bus_dma_read_bounces);
116 EVCNT_ATTACH_STATIC(bus_dma_write_bounces);
117 EVCNT_ATTACH_STATIC(bus_dma_unloads);
118 EVCNT_ATTACH_STATIC(bus_dma_bounced_unloads);
119 EVCNT_ATTACH_STATIC(bus_dma_destroys);
120 EVCNT_ATTACH_STATIC(bus_dma_bounced_destroys);
121 EVCNT_ATTACH_STATIC(bus_dma_sync_prereadwrite);
122 EVCNT_ATTACH_STATIC(bus_dma_sync_preread_begin);
123 EVCNT_ATTACH_STATIC(bus_dma_sync_preread);
124 EVCNT_ATTACH_STATIC(bus_dma_sync_preread_tail);
125 EVCNT_ATTACH_STATIC(bus_dma_sync_prewrite);
126 EVCNT_ATTACH_STATIC(bus_dma_sync_postread);
127 EVCNT_ATTACH_STATIC(bus_dma_sync_postreadwrite);
128 EVCNT_ATTACH_STATIC(bus_dma_sync_postwrite);
129 
130 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_prereadwrite);
131 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_preread);
132 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_prewrite);
133 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_postread);
134 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_postreadwrite);
135 EVCNT_ATTACH_STATIC(bus_dma_sync_coherent_postwrite);
136 
137 #define	STAT_INCR(x)	(bus_dma_ ## x.ev_count++)
138 #else
139 #define	STAT_INCR(x)	__nothing
140 #endif
141 
142 int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
143 	    bus_size_t, struct vmspace *, int);
144 
145 /*
146  * Check to see if the specified page is in an allowed DMA range.
147  */
148 static inline struct arm32_dma_range *
149 _bus_dma_paddr_inrange(struct arm32_dma_range *ranges, int nranges,
150     bus_addr_t curaddr)
151 {
152 	struct arm32_dma_range *dr;
153 	int i;
154 
155 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
156 		if (curaddr >= dr->dr_sysbase &&
157 		    curaddr < (dr->dr_sysbase + dr->dr_len))
158 			return dr;
159 	}
160 
161 	return NULL;
162 }
163 
164 /*
165  * Check to see if the specified busaddr is in an allowed DMA range.
166  */
167 static inline paddr_t
168 _bus_dma_busaddr_to_paddr(bus_dma_tag_t t, bus_addr_t curaddr)
169 {
170 	struct arm32_dma_range *dr;
171 	u_int i;
172 
173 	if (t->_nranges == 0)
174 		return curaddr;
175 
176 	for (i = 0, dr = t->_ranges; i < t->_nranges; i++, dr++) {
177 		if (dr->dr_busbase <= curaddr
178 		    && curaddr < dr->dr_busbase + dr->dr_len)
179 			return curaddr - dr->dr_busbase + dr->dr_sysbase;
180 	}
181 	panic("%s: curaddr %#lx not in range", __func__, curaddr);
182 }
183 
184 /*
185  * Common function to load the specified physical address into the
186  * DMA map, coalescing segments and boundary checking as necessary.
187  */
188 static int
189 _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
190     bus_addr_t paddr, bus_size_t size, bool coherent)
191 {
192 	bus_dma_segment_t * const segs = map->dm_segs;
193 	int nseg = map->dm_nsegs;
194 	bus_addr_t lastaddr;
195 	bus_addr_t bmask = ~(map->_dm_boundary - 1);
196 	bus_addr_t curaddr;
197 	bus_size_t sgsize;
198 	uint32_t _ds_flags = coherent ? _BUS_DMAMAP_COHERENT : 0;
199 
200 	if (nseg > 0)
201 		lastaddr = segs[nseg - 1].ds_addr + segs[nseg - 1].ds_len;
202 	else
203 		lastaddr = 0xdead;
204 
205  again:
206 	sgsize = size;
207 
208 	/* Make sure we're in an allowed DMA range. */
209 	if (t->_ranges != NULL) {
210 		/* XXX cache last result? */
211 		const struct arm32_dma_range * const dr =
212 		    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, paddr);
213 		if (dr == NULL)
214 			return EINVAL;
215 
216 		/*
217 		 * If this region is coherent, mark the segment as coherent.
218 		 */
219 		_ds_flags |= dr->dr_flags & _BUS_DMAMAP_COHERENT;
220 
221 		/*
222 		 * In a valid DMA range.  Translate the physical
223 		 * memory address to an address in the DMA window.
224 		 */
225 		curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
226 #if 0
227 		printf("%p: %#lx: range %#lx/%#lx/%#lx/%#x: %#x <-- %#lx\n",
228 		    t, paddr, dr->dr_sysbase, dr->dr_busbase,
229 		    dr->dr_len, dr->dr_flags, _ds_flags, curaddr);
230 #endif
231 	} else
232 		curaddr = paddr;
233 
234 	/*
235 	 * Make sure we don't cross any boundaries.
236 	 */
237 	if (map->_dm_boundary > 0) {
238 		bus_addr_t baddr;	/* next boundary address */
239 
240 		baddr = (curaddr + map->_dm_boundary) & bmask;
241 		if (sgsize > (baddr - curaddr))
242 			sgsize = (baddr - curaddr);
243 	}
244 
245 	/*
246 	 * Insert chunk into a segment, coalescing with the
247 	 * previous segment if possible.
248 	 */
249 	if (nseg > 0 && curaddr == lastaddr &&
250 	    segs[nseg - 1].ds_len + sgsize <= map->dm_maxsegsz &&
251 	    ((segs[nseg - 1]._ds_flags ^ _ds_flags) & _BUS_DMAMAP_COHERENT) == 0 &&
252 	    (map->_dm_boundary == 0 ||
253 	     (segs[nseg - 1].ds_addr & bmask) == (curaddr & bmask))) {
254 	     	/* coalesce */
255 		segs[nseg - 1].ds_len += sgsize;
256 	} else if (nseg >= map->_dm_segcnt) {
257 		return EFBIG;
258 	} else {
259 		/* new segment */
260 		segs[nseg].ds_addr = curaddr;
261 		segs[nseg].ds_len = sgsize;
262 		segs[nseg]._ds_flags = _ds_flags;
263 		nseg++;
264 	}
265 
266 	lastaddr = curaddr + sgsize;
267 
268 	paddr += sgsize;
269 	size -= sgsize;
270 	if (size > 0)
271 		goto again;
272 
273 	map->_dm_flags &= (_ds_flags & _BUS_DMAMAP_COHERENT);
274 	map->dm_nsegs = nseg;
275 	return 0;
276 }
277 
278 static int _bus_dma_uiomove(void *buf, struct uio *uio, size_t n,
279 	    int direction);
280 
281 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
282 static int _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
283 	    bus_size_t size, int flags);
284 static void _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map);
285 
286 static int
287 _bus_dma_load_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
288 	size_t buflen, int buftype, int flags)
289 {
290 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
291 	struct vmspace * const vm = vmspace_kernel();
292 	int error;
293 
294 	KASSERT(cookie != NULL);
295 	KASSERT(cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE);
296 
297 	/*
298 	 * Allocate bounce pages, if necessary.
299 	 */
300 	if ((cookie->id_flags & _BUS_DMA_HAS_BOUNCE) == 0) {
301 		error = _bus_dma_alloc_bouncebuf(t, map, buflen, flags);
302 		if (error)
303 			return error;
304 	}
305 
306 	/*
307 	 * Cache a pointer to the caller's buffer and load the DMA map
308 	 * with the bounce buffer.
309 	 */
310 	cookie->id_origbuf = buf;
311 	cookie->id_origbuflen = buflen;
312 	error = _bus_dmamap_load_buffer(t, map, cookie->id_bouncebuf,
313 	    buflen, vm, flags);
314 	if (error)
315 		return error;
316 
317 	STAT_INCR(bounced_loads);
318 	map->dm_mapsize = buflen;
319 	map->_dm_vmspace = vm;
320 	map->_dm_buftype = buftype;
321 
322 	/* ...so _bus_dmamap_sync() knows we're bouncing */
323 	map->_dm_flags |= _BUS_DMAMAP_IS_BOUNCING;
324 	cookie->id_flags |= _BUS_DMA_IS_BOUNCING;
325 	return 0;
326 }
327 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
328 
329 /*
330  * Common function for DMA map creation.  May be called by bus-specific
331  * DMA map creation functions.
332  */
333 int
334 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
335     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
336 {
337 	struct arm32_bus_dmamap *map;
338 	void *mapstore;
339 	int error = 0;
340 
341 #ifdef DEBUG_DMA
342 	printf("dmamap_create: t=%p size=%#lx nseg=%#x msegsz=%#lx boundary=%#lx"
343 	    " flags=%#x\n", t, size, nsegments, maxsegsz, boundary, flags);
344 #endif	/* DEBUG_DMA */
345 
346 	/*
347 	 * Allocate and initialize the DMA map.  The end of the map
348 	 * is a variable-sized array of segments, so we allocate enough
349 	 * room for them in one shot.
350 	 *
351 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
352 	 * of ALLOCNOW notifies others that we've reserved these resources,
353 	 * and they are not to be freed.
354 	 *
355 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
356 	 * the (nsegments - 1).
357 	 */
358 	const size_t mapsize = sizeof(struct arm32_bus_dmamap) +
359 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
360 	const int zallocflags = (flags & BUS_DMA_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
361 	if ((mapstore = kmem_intr_zalloc(mapsize, zallocflags)) == NULL)
362 		return ENOMEM;
363 
364 	map = (struct arm32_bus_dmamap *)mapstore;
365 	map->_dm_size = size;
366 	map->_dm_segcnt = nsegments;
367 	map->_dm_maxmaxsegsz = maxsegsz;
368 	map->_dm_boundary = boundary;
369 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
370 	map->_dm_origbuf = NULL;
371 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
372 	map->_dm_vmspace = vmspace_kernel();
373 	map->_dm_cookie = NULL;
374 	map->dm_maxsegsz = maxsegsz;
375 	map->dm_mapsize = 0;		/* no valid mappings */
376 	map->dm_nsegs = 0;
377 
378 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
379 	struct arm32_bus_dma_cookie *cookie;
380 	int cookieflags;
381 	void *cookiestore;
382 
383 	cookieflags = 0;
384 
385 	if (t->_may_bounce != NULL) {
386 		error = (*t->_may_bounce)(t, map, flags, &cookieflags);
387 		if (error != 0)
388 			goto out;
389 	}
390 
391 	if (t->_ranges != NULL) {
392 		/*
393 		 * If ranges are defined, we may have to bounce. The only
394 		 * exception is if there is exactly one range that covers
395 		 * all of physical memory.
396 		 */
397 		switch (t->_nranges) {
398 		case 1:
399 			if (t->_ranges[0].dr_sysbase == 0 &&
400 			    t->_ranges[0].dr_len == UINTPTR_MAX) {
401 				break;
402 			}
403 			/* FALLTHROUGH */
404 		default:
405 			cookieflags |= _BUS_DMA_MIGHT_NEED_BOUNCE;
406 		}
407 	}
408 
409 	if ((cookieflags & _BUS_DMA_MIGHT_NEED_BOUNCE) == 0) {
410 		STAT_INCR(creates);
411 		*dmamp = map;
412 		return 0;
413 	}
414 
415 	const size_t cookiesize = sizeof(struct arm32_bus_dma_cookie) +
416 	    (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
417 
418 	/*
419 	 * Allocate our cookie.
420 	 */
421 	if ((cookiestore = kmem_intr_zalloc(cookiesize, zallocflags)) == NULL) {
422 		error = ENOMEM;
423 		goto out;
424 	}
425 	cookie = (struct arm32_bus_dma_cookie *)cookiestore;
426 	cookie->id_flags = cookieflags;
427 	map->_dm_cookie = cookie;
428 	STAT_INCR(bounced_creates);
429 
430 	error = _bus_dma_alloc_bouncebuf(t, map, size, flags);
431  out:
432 	if (error)
433 		_bus_dmamap_destroy(t, map);
434 	else
435 		*dmamp = map;
436 #else
437 	*dmamp = map;
438 	STAT_INCR(creates);
439 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
440 #ifdef DEBUG_DMA
441 	printf("dmamap_create:map=%p\n", map);
442 #endif	/* DEBUG_DMA */
443 	return error;
444 }
445 
446 /*
447  * Common function for DMA map destruction.  May be called by bus-specific
448  * DMA map destruction functions.
449  */
450 void
451 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
452 {
453 
454 #ifdef DEBUG_DMA
455 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
456 #endif	/* DEBUG_DMA */
457 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
458 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
459 
460 	/*
461 	 * Free any bounce pages this map might hold.
462 	 */
463 	if (cookie != NULL) {
464 		const size_t cookiesize = sizeof(struct arm32_bus_dma_cookie) +
465 		    (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
466 
467 		if (cookie->id_flags & _BUS_DMA_IS_BOUNCING)
468 			STAT_INCR(bounced_unloads);
469 		map->dm_nsegs = 0;
470 		if (cookie->id_flags & _BUS_DMA_HAS_BOUNCE)
471 			_bus_dma_free_bouncebuf(t, map);
472 		STAT_INCR(bounced_destroys);
473 		kmem_intr_free(cookie, cookiesize);
474 	} else
475 #endif
476 	STAT_INCR(destroys);
477 
478 	if (map->dm_nsegs > 0)
479 		STAT_INCR(unloads);
480 
481 	const size_t mapsize = sizeof(struct arm32_bus_dmamap) +
482 	    (sizeof(bus_dma_segment_t) * (map->_dm_segcnt - 1));
483 	kmem_intr_free(map, mapsize);
484 }
485 
486 /*
487  * Common function for loading a DMA map with a linear buffer.  May
488  * be called by bus-specific DMA map load functions.
489  */
490 int
491 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
492     bus_size_t buflen, struct proc *p, int flags)
493 {
494 	struct vmspace *vm;
495 	int error;
496 
497 #ifdef DEBUG_DMA
498 	printf("dmamap_load: t=%p map=%p buf=%p len=%#lx p=%p f=%#x\n",
499 	    t, map, buf, buflen, p, flags);
500 #endif	/* DEBUG_DMA */
501 
502 	if (map->dm_nsegs > 0) {
503 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
504 		struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
505 		if (cookie != NULL) {
506 			if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
507 				STAT_INCR(bounced_unloads);
508 				cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
509 				map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
510 			}
511 		} else
512 #endif
513 		STAT_INCR(unloads);
514 	}
515 
516 	/*
517 	 * Make sure that on error condition we return "no valid mappings".
518 	 */
519 	map->dm_mapsize = 0;
520 	map->dm_nsegs = 0;
521 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
522 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
523 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
524 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
525 
526 	if (buflen > map->_dm_size)
527 		return EINVAL;
528 
529 	if (p != NULL) {
530 		vm = p->p_vmspace;
531 	} else {
532 		vm = vmspace_kernel();
533 	}
534 
535 	/* _bus_dmamap_load_buffer() clears this if we're not... */
536 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
537 
538 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
539 	if (error == 0) {
540 		map->dm_mapsize = buflen;
541 		map->_dm_vmspace = vm;
542 		map->_dm_origbuf = buf;
543 		map->_dm_buftype = _BUS_DMA_BUFTYPE_LINEAR;
544 		if (map->_dm_flags & _BUS_DMAMAP_COHERENT) {
545 			STAT_INCR(coherent_loads);
546 		} else {
547 			STAT_INCR(loads);
548 		}
549 		return 0;
550 	}
551 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
552 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
553 	if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
554 		error = _bus_dma_load_bouncebuf(t, map, buf, buflen,
555 		    _BUS_DMA_BUFTYPE_LINEAR, flags);
556 	}
557 #endif
558 	return error;
559 }
560 
561 /*
562  * Like _bus_dmamap_load(), but for mbufs.
563  */
564 int
565 _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
566     int flags)
567 {
568 	struct mbuf *m;
569 	int error;
570 
571 #ifdef DEBUG_DMA
572 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%#x\n",
573 	    t, map, m0, flags);
574 #endif	/* DEBUG_DMA */
575 
576 	if (map->dm_nsegs > 0) {
577 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
578 		struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
579 		if (cookie != NULL) {
580 			if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
581 				STAT_INCR(bounced_unloads);
582 				cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
583 				map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
584 			}
585 		} else
586 #endif
587 		STAT_INCR(unloads);
588 	}
589 
590 	/*
591 	 * Make sure that on error condition we return "no valid mappings."
592 	 */
593 	map->dm_mapsize = 0;
594 	map->dm_nsegs = 0;
595 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
596 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
597 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
598 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
599 
600 	KASSERT(m0->m_flags & M_PKTHDR);
601 
602 	if (m0->m_pkthdr.len > map->_dm_size)
603 		return EINVAL;
604 
605 	/* _bus_dmamap_load_paddr() clears this if we're not... */
606 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
607 
608 	error = 0;
609 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
610 		int offset;
611 		int remainbytes;
612 		const struct vm_page * const *pgs;
613 		paddr_t paddr;
614 		int size;
615 
616 		if (m->m_len == 0)
617 			continue;
618 		/*
619 		 * Don't allow reads in read-only mbufs.
620 		 */
621 		if (M_ROMAP(m) && (flags & BUS_DMA_READ)) {
622 			error = EFAULT;
623 			break;
624 		}
625 		switch (m->m_flags & (M_EXT|M_EXT_CLUSTER|M_EXT_PAGES)) {
626 		case M_EXT|M_EXT_CLUSTER:
627 			/* XXX KDASSERT */
628 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
629 			paddr = m->m_ext.ext_paddr +
630 			    (m->m_data - m->m_ext.ext_buf);
631 			size = m->m_len;
632 			error = _bus_dmamap_load_paddr(t, map, paddr, size,
633 			    false);
634 			break;
635 
636 		case M_EXT|M_EXT_PAGES:
637 			KASSERT(m->m_ext.ext_buf <= m->m_data);
638 			KASSERT(m->m_data <=
639 			    m->m_ext.ext_buf + m->m_ext.ext_size);
640 
641 			offset = (vaddr_t)m->m_data -
642 			    trunc_page((vaddr_t)m->m_ext.ext_buf);
643 			remainbytes = m->m_len;
644 
645 			/* skip uninteresting pages */
646 			pgs = (const struct vm_page * const *)
647 			    m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
648 
649 			offset &= PAGE_MASK;	/* offset in the first page */
650 
651 			/* load each page */
652 			while (remainbytes > 0) {
653 				const struct vm_page *pg;
654 
655 				size = MIN(remainbytes, PAGE_SIZE - offset);
656 
657 				pg = *pgs++;
658 				KASSERT(pg);
659 				paddr = VM_PAGE_TO_PHYS(pg) + offset;
660 
661 				error = _bus_dmamap_load_paddr(t, map,
662 				    paddr, size, false);
663 				if (error)
664 					break;
665 				offset = 0;
666 				remainbytes -= size;
667 			}
668 			break;
669 
670 		case 0:
671 			paddr = m->m_paddr + M_BUFOFFSET(m) +
672 			    (m->m_data - M_BUFADDR(m));
673 			size = m->m_len;
674 			error = _bus_dmamap_load_paddr(t, map, paddr, size,
675 			    false);
676 			break;
677 
678 		default:
679 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
680 			    m->m_len, vmspace_kernel(), flags);
681 		}
682 	}
683 	if (error == 0) {
684 		map->dm_mapsize = m0->m_pkthdr.len;
685 		map->_dm_origbuf = m0;
686 		map->_dm_buftype = _BUS_DMA_BUFTYPE_MBUF;
687 		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
688 		if (map->_dm_flags & _BUS_DMAMAP_COHERENT) {
689 			STAT_INCR(coherent_loads);
690 		} else {
691 			STAT_INCR(loads);
692 		}
693 		return 0;
694 	}
695 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
696 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
697 	if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
698 		error = _bus_dma_load_bouncebuf(t, map, m0, m0->m_pkthdr.len,
699 		    _BUS_DMA_BUFTYPE_MBUF, flags);
700 	}
701 #endif
702 	return error;
703 }
704 
705 /*
706  * Like _bus_dmamap_load(), but for uios.
707  */
708 int
709 _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
710     int flags)
711 {
712 	bus_size_t minlen, resid;
713 	struct iovec *iov;
714 	void *addr;
715 	int i, error;
716 
717 	/*
718 	 * Make sure that on error condition we return "no valid mappings."
719 	 */
720 	map->dm_mapsize = 0;
721 	map->dm_nsegs = 0;
722 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
723 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
724 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
725 
726 	resid = uio->uio_resid;
727 	iov = uio->uio_iov;
728 
729 	/* _bus_dmamap_load_buffer() clears this if we're not... */
730 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
731 
732 	error = 0;
733 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
734 		/*
735 		 * Now at the first iovec to load.  Load each iovec
736 		 * until we have exhausted the residual count.
737 		 */
738 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
739 		addr = (void *)iov[i].iov_base;
740 
741 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
742 		    uio->uio_vmspace, flags);
743 
744 		resid -= minlen;
745 	}
746 	if (error == 0) {
747 		map->dm_mapsize = uio->uio_resid;
748 		map->_dm_origbuf = uio;
749 		map->_dm_buftype = _BUS_DMA_BUFTYPE_UIO;
750 		map->_dm_vmspace = uio->uio_vmspace;
751 		if (map->_dm_flags & _BUS_DMAMAP_COHERENT) {
752 			STAT_INCR(coherent_loads);
753 		} else {
754 			STAT_INCR(loads);
755 		}
756 	}
757 	return error;
758 }
759 
760 /*
761  * Like _bus_dmamap_load(), but for raw memory allocated with
762  * bus_dmamem_alloc().
763  */
764 int
765 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
766     bus_dma_segment_t *segs, int nsegs, bus_size_t size0, int flags)
767 {
768 
769 	bus_size_t size;
770 	int i, error = 0;
771 
772 	/*
773 	 * Make sure that on error conditions we return "no valid mappings."
774 	 */
775 	map->dm_mapsize = 0;
776 	map->dm_nsegs = 0;
777 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
778 
779 	if (size0 > map->_dm_size)
780 		return EINVAL;
781 
782 	for (i = 0, size = size0; i < nsegs && size > 0; i++) {
783 		bus_dma_segment_t *ds = &segs[i];
784 		bus_size_t sgsize;
785 
786 		sgsize = MIN(ds->ds_len, size);
787 		if (sgsize == 0)
788 			continue;
789 		const bool coherent =
790 		    (ds->_ds_flags & _BUS_DMAMAP_COHERENT) != 0;
791 		error = _bus_dmamap_load_paddr(t, map, ds->ds_addr,
792 		    sgsize, coherent);
793 		if (error != 0)
794 			break;
795 		size -= sgsize;
796 	}
797 
798 	if (error != 0) {
799 		map->dm_mapsize = 0;
800 		map->dm_nsegs = 0;
801 		return error;
802 	}
803 
804 	/* XXX TBD bounce */
805 
806 	map->dm_mapsize = size0;
807 	map->_dm_origbuf = NULL;
808 	map->_dm_buftype = _BUS_DMA_BUFTYPE_RAW;
809 	map->_dm_vmspace = NULL;
810 	return 0;
811 }
812 
813 /*
814  * Common function for unloading a DMA map.  May be called by
815  * bus-specific DMA map unload functions.
816  */
817 void
818 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
819 {
820 
821 #ifdef DEBUG_DMA
822 	printf("dmamap_unload: t=%p map=%p\n", t, map);
823 #endif	/* DEBUG_DMA */
824 
825 	/*
826 	 * No resources to free; just mark the mappings as
827 	 * invalid.
828 	 */
829 	map->dm_mapsize = 0;
830 	map->dm_nsegs = 0;
831 	map->_dm_origbuf = NULL;
832 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
833 	map->_dm_vmspace = NULL;
834 }
835 
836 static void
837 _bus_dmamap_sync_segment(vaddr_t va, paddr_t pa, vsize_t len, int ops,
838     bool readonly_p)
839 {
840 
841 #if defined(ARM_MMU_EXTENDED)
842 	/*
843 	 * No optimisations are available for readonly mbufs on armv6+, so
844 	 * assume it's not readonly from here on.
845 	 *
846  	 * See the comment in _bus_dmamap_sync_mbuf
847 	 */
848 	readonly_p = false;
849 #endif
850 
851 	KASSERTMSG((va & PAGE_MASK) == (pa & PAGE_MASK),
852 	    "va %#lx pa %#lx", va, pa);
853 #if 0
854 	printf("sync_segment: va=%#lx pa=%#lx len=%#lx ops=%#x ro=%d\n",
855 	    va, pa, len, ops, readonly_p);
856 #endif
857 
858 	switch (ops) {
859 	case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
860 		if (!readonly_p) {
861 			STAT_INCR(sync_prereadwrite);
862 			cpu_dcache_wbinv_range(va, len);
863 			cpu_sdcache_wbinv_range(va, pa, len);
864 			break;
865 		}
866 		/* FALLTHROUGH */
867 
868 	case BUS_DMASYNC_PREREAD: {
869 		const size_t line_size = arm_dcache_align;
870 		const size_t line_mask = arm_dcache_align_mask;
871 		vsize_t misalignment = va & line_mask;
872 		if (misalignment) {
873 			va -= misalignment;
874 			pa -= misalignment;
875 			len += misalignment;
876 			STAT_INCR(sync_preread_begin);
877 			cpu_dcache_wbinv_range(va, line_size);
878 			cpu_sdcache_wbinv_range(va, pa, line_size);
879 			if (len <= line_size)
880 				break;
881 			va += line_size;
882 			pa += line_size;
883 			len -= line_size;
884 		}
885 		misalignment = len & line_mask;
886 		len -= misalignment;
887 		if (len > 0) {
888 			STAT_INCR(sync_preread);
889 			cpu_dcache_inv_range(va, len);
890 			cpu_sdcache_inv_range(va, pa, len);
891 		}
892 		if (misalignment) {
893 			va += len;
894 			pa += len;
895 			STAT_INCR(sync_preread_tail);
896 			cpu_dcache_wbinv_range(va, line_size);
897 			cpu_sdcache_wbinv_range(va, pa, line_size);
898 		}
899 		break;
900 	}
901 
902 	case BUS_DMASYNC_PREWRITE:
903 		STAT_INCR(sync_prewrite);
904 		cpu_dcache_wb_range(va, len);
905 		cpu_sdcache_wb_range(va, pa, len);
906 		break;
907 
908 #if defined(CPU_CORTEX) || defined(CPU_ARMV8)
909 
910 	/*
911 	 * Cortex CPUs can do speculative loads so we need to clean the cache
912 	 * after a DMA read to deal with any speculatively loaded cache lines.
913 	 * Since these can't be dirty, we can just invalidate them and don't
914 	 * have to worry about having to write back their contents.
915 	 */
916 	case BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE:
917 		STAT_INCR(sync_postreadwrite);
918 		cpu_dcache_inv_range(va, len);
919 		cpu_sdcache_inv_range(va, pa, len);
920 		break;
921 
922 	case BUS_DMASYNC_POSTREAD:
923 		STAT_INCR(sync_postread);
924 		cpu_dcache_inv_range(va, len);
925 		cpu_sdcache_inv_range(va, pa, len);
926 		break;
927 #endif
928 	}
929 }
930 
931 static inline void
932 _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
933     bus_size_t len, int ops)
934 {
935 	bus_dma_segment_t *ds = map->dm_segs;
936 	vaddr_t va = (vaddr_t) map->_dm_origbuf;
937 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
938 	if (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING) {
939 		struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
940 		va = (vaddr_t) cookie->id_bouncebuf;
941 	}
942 #endif
943 
944 	while (len > 0) {
945 		while (offset >= ds->ds_len) {
946 			offset -= ds->ds_len;
947 			va += ds->ds_len;
948 			ds++;
949 		}
950 
951 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + offset);
952 		size_t seglen = uimin(len, ds->ds_len - offset);
953 
954 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
955 			_bus_dmamap_sync_segment(va + offset, pa, seglen, ops,
956 			    false);
957 
958 		offset += seglen;
959 		len -= seglen;
960 	}
961 }
962 
963 static inline void
964 _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t offset,
965     bus_size_t len, int ops)
966 {
967 	bus_dma_segment_t *ds = map->dm_segs;
968 	struct mbuf *m = map->_dm_origbuf;
969 	bus_size_t voff = offset;
970 	bus_size_t ds_off = offset;
971 
972 	while (len > 0) {
973 		/* Find the current dma segment */
974 		while (ds_off >= ds->ds_len) {
975 			ds_off -= ds->ds_len;
976 			ds++;
977 		}
978 		/* Find the current mbuf. */
979 		while (voff >= m->m_len) {
980 			voff -= m->m_len;
981 			m = m->m_next;
982 		}
983 
984 		/*
985 		 * Now at the first mbuf to sync; nail each one until
986 		 * we have exhausted the length.
987 		 */
988 		vsize_t seglen = uimin(len, uimin(m->m_len - voff, ds->ds_len - ds_off));
989 		vaddr_t va = mtod(m, vaddr_t) + voff;
990 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
991 
992 		/*
993 		 * We can save a lot of work here if we know the mapping
994 		 * is read-only at the MMU and we aren't using the armv6+
995 		 * MMU:
996 		 *
997 		 * If a mapping is read-only, no dirty cache blocks will
998 		 * exist for it.  If a writable mapping was made read-only,
999 		 * we know any dirty cache lines for the range will have
1000 		 * been cleaned for us already.  Therefore, if the upper
1001 		 * layer can tell us we have a read-only mapping, we can
1002 		 * skip all cache cleaning.
1003 		 *
1004 		 * NOTE: This only works if we know the pmap cleans pages
1005 		 * before making a read-write -> read-only transition.  If
1006 		 * this ever becomes non-true (e.g. Physically Indexed
1007 		 * cache), this will have to be revisited.
1008 		 */
1009 
1010 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0) {
1011 			/*
1012 			 * If we are doing preread (DMAing into the mbuf),
1013 			 * this mbuf better not be readonly,
1014 			 */
1015 			KASSERT(!(ops & BUS_DMASYNC_PREREAD) || !M_ROMAP(m));
1016 			_bus_dmamap_sync_segment(va, pa, seglen, ops,
1017 			    M_ROMAP(m));
1018 		}
1019 		voff += seglen;
1020 		ds_off += seglen;
1021 		len -= seglen;
1022 	}
1023 }
1024 
1025 static inline void
1026 _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
1027     bus_size_t len, int ops)
1028 {
1029 	bus_dma_segment_t *ds = map->dm_segs;
1030 	struct uio *uio = map->_dm_origbuf;
1031 	struct iovec *iov = uio->uio_iov;
1032 	bus_size_t voff = offset;
1033 	bus_size_t ds_off = offset;
1034 
1035 	while (len > 0) {
1036 		/* Find the current dma segment */
1037 		while (ds_off >= ds->ds_len) {
1038 			ds_off -= ds->ds_len;
1039 			ds++;
1040 		}
1041 
1042 		/* Find the current iovec. */
1043 		while (voff >= iov->iov_len) {
1044 			voff -= iov->iov_len;
1045 			iov++;
1046 		}
1047 
1048 		/*
1049 		 * Now at the first iovec to sync; nail each one until
1050 		 * we have exhausted the length.
1051 		 */
1052 		vsize_t seglen = uimin(len, uimin(iov->iov_len - voff, ds->ds_len - ds_off));
1053 		vaddr_t va = (vaddr_t) iov->iov_base + voff;
1054 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
1055 
1056 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
1057 			_bus_dmamap_sync_segment(va, pa, seglen, ops, false);
1058 
1059 		voff += seglen;
1060 		ds_off += seglen;
1061 		len -= seglen;
1062 	}
1063 }
1064 
1065 /*
1066  * Common function for DMA map synchronization.  May be called
1067  * by bus-specific DMA map synchronization functions.
1068  *
1069  * XXX Should have separate versions for write-through vs.
1070  * XXX write-back caches.  We currently assume write-back
1071  * XXX here, which is not as efficient as it could be for
1072  * XXX the write-through case.
1073  */
1074 void
1075 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
1076     bus_size_t len, int ops)
1077 {
1078 #ifdef DEBUG_DMA
1079 	printf("dmamap_sync: t=%p map=%p offset=%#lx len=%#lx ops=%#x\n",
1080 	    t, map, offset, len, ops);
1081 #endif	/* DEBUG_DMA */
1082 
1083 	/*
1084 	 * Mixing of PRE and POST operations is not allowed.
1085 	 */
1086 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
1087 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
1088 		panic("%s: mix PRE and POST", __func__);
1089 
1090 	KASSERTMSG(offset < map->dm_mapsize,
1091 	    "offset %lu mapsize %lu",
1092 	    offset, map->dm_mapsize);
1093 	KASSERTMSG(len > 0 && offset + len <= map->dm_mapsize,
1094 	    "len %lu offset %lu mapsize %lu",
1095 	    len, offset, map->dm_mapsize);
1096 
1097 	/*
1098 	 * For a virtually-indexed write-back cache, we need
1099 	 * to do the following things:
1100 	 *
1101 	 *	PREREAD -- Invalidate the D-cache.  We do this
1102 	 *	here in case a write-back is required by the back-end.
1103 	 *
1104 	 *	PREWRITE -- Write-back the D-cache.  Note that if
1105 	 *	we are doing a PREREAD|PREWRITE, we can collapse
1106 	 *	the whole thing into a single Wb-Inv.
1107 	 *
1108 	 *	POSTREAD -- Re-invalidate the D-cache in case speculative
1109 	 *	memory accesses caused cachelines to become valid with now
1110 	 *	invalid data.
1111 	 *
1112 	 *	POSTWRITE -- Nothing.
1113 	 */
1114 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1115 	const bool bouncing = (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING);
1116 #else
1117 	const bool bouncing = false;
1118 #endif
1119 
1120 	const int pre_ops = ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1121 #if defined(CPU_CORTEX) || defined(CPU_ARMV8)
1122 	const int post_ops = ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1123 #else
1124 	const int post_ops = 0;
1125 #endif
1126 	if (pre_ops == 0 && post_ops == 0)
1127 		return;
1128 
1129 	if (post_ops == BUS_DMASYNC_POSTWRITE) {
1130 		KASSERT(pre_ops == 0);
1131 		if ((map->_dm_flags & _BUS_DMAMAP_COHERENT)) {
1132 			STAT_INCR(sync_coherent_postwrite);
1133 		} else {
1134 			STAT_INCR(sync_postwrite);
1135 		}
1136 		return;
1137 	}
1138 
1139 	KASSERTMSG(bouncing || pre_ops != 0 || (post_ops & BUS_DMASYNC_POSTREAD),
1140 	    "pre_ops %#x post_ops %#x", pre_ops, post_ops);
1141 
1142 	if (bouncing && (ops & BUS_DMASYNC_PREWRITE)) {
1143 		struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
1144 		STAT_INCR(write_bounces);
1145 		char * const dataptr = (char *)cookie->id_bouncebuf + offset;
1146 		/*
1147 		 * Copy the caller's buffer to the bounce buffer.
1148 		 */
1149 		switch (map->_dm_buftype) {
1150 		case _BUS_DMA_BUFTYPE_LINEAR:
1151 			memcpy(dataptr, cookie->id_origlinearbuf + offset, len);
1152 			break;
1153 
1154 		case _BUS_DMA_BUFTYPE_MBUF:
1155 			m_copydata(cookie->id_origmbuf, offset, len, dataptr);
1156 			break;
1157 
1158 		case _BUS_DMA_BUFTYPE_UIO:
1159 			_bus_dma_uiomove(dataptr, cookie->id_origuio, len,
1160 			    UIO_WRITE);
1161 			break;
1162 
1163 #ifdef DIAGNOSTIC
1164 		case _BUS_DMA_BUFTYPE_RAW:
1165 			panic("%s:(pre): _BUS_DMA_BUFTYPE_RAW", __func__);
1166 			break;
1167 
1168 		case _BUS_DMA_BUFTYPE_INVALID:
1169 			panic("%s(pre): _BUS_DMA_BUFTYPE_INVALID", __func__);
1170 			break;
1171 
1172 		default:
1173 			panic("%s(pre): map %p: unknown buffer type %d\n",
1174 			    __func__, map, map->_dm_buftype);
1175 			break;
1176 #endif /* DIAGNOSTIC */
1177 		}
1178 	}
1179 
1180 	/* Skip cache frobbing if mapping was COHERENT */
1181 	if ((map->_dm_flags & _BUS_DMAMAP_COHERENT)) {
1182 		switch (ops) {
1183 		case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
1184 			STAT_INCR(sync_coherent_prereadwrite);
1185 			break;
1186 
1187 		case BUS_DMASYNC_PREREAD:
1188 			STAT_INCR(sync_coherent_preread);
1189 			break;
1190 
1191 		case BUS_DMASYNC_PREWRITE:
1192 			STAT_INCR(sync_coherent_prewrite);
1193 			break;
1194 
1195 		case BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE:
1196 			STAT_INCR(sync_coherent_postreadwrite);
1197 			break;
1198 
1199 		case BUS_DMASYNC_POSTREAD:
1200 			STAT_INCR(sync_coherent_postread);
1201 			break;
1202 
1203 		/* BUS_DMASYNC_POSTWRITE was aleady handled as a fastpath */
1204 		}
1205 		/*
1206 		 * Drain the write buffer of DMA operators.
1207 		 * 1) when cpu->device (prewrite)
1208 		 * 2) when device->cpu (postread)
1209 		 */
1210 		if ((pre_ops & BUS_DMASYNC_PREWRITE) || (post_ops & BUS_DMASYNC_POSTREAD))
1211 			cpu_drain_writebuf();
1212 
1213 		/*
1214 		 * Only thing left to do for COHERENT mapping is copy from bounce
1215 		 * in the POSTREAD case.
1216 		 */
1217 		if (bouncing && (post_ops & BUS_DMASYNC_POSTREAD))
1218 			goto bounce_it;
1219 
1220 		return;
1221 	}
1222 
1223 #if !defined(ARM_MMU_EXTENDED)
1224 	/*
1225 	 * If the mapping belongs to a non-kernel vmspace, and the
1226 	 * vmspace has not been active since the last time a full
1227 	 * cache flush was performed, we don't need to do anything.
1228 	 */
1229 	if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
1230 	    vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
1231 		return;
1232 #endif
1233 
1234 	int buftype = map->_dm_buftype;
1235 	if (bouncing) {
1236 		buftype = _BUS_DMA_BUFTYPE_LINEAR;
1237 	}
1238 
1239 	switch (buftype) {
1240 	case _BUS_DMA_BUFTYPE_LINEAR:
1241 	case _BUS_DMA_BUFTYPE_RAW:
1242 		_bus_dmamap_sync_linear(t, map, offset, len, ops);
1243 		break;
1244 
1245 	case _BUS_DMA_BUFTYPE_MBUF:
1246 		_bus_dmamap_sync_mbuf(t, map, offset, len, ops);
1247 		break;
1248 
1249 	case _BUS_DMA_BUFTYPE_UIO:
1250 		_bus_dmamap_sync_uio(t, map, offset, len, ops);
1251 		break;
1252 
1253 	case _BUS_DMA_BUFTYPE_INVALID:
1254 		panic("%s: _BUS_DMA_BUFTYPE_INVALID", __func__);
1255 		break;
1256 
1257 	default:
1258 		panic("%s: map %p: unknown buffer type %d\n", __func__, map,
1259 		    map->_dm_buftype);
1260 	}
1261 
1262 	/* Drain the write buffer. */
1263 	cpu_drain_writebuf();
1264 
1265 	if (!bouncing || (ops & BUS_DMASYNC_POSTREAD) == 0)
1266 		return;
1267 
1268   bounce_it:
1269 	STAT_INCR(read_bounces);
1270 
1271 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
1272 	char * const dataptr = (char *)cookie->id_bouncebuf + offset;
1273 	/*
1274 	 * Copy the bounce buffer to the caller's buffer.
1275 	 */
1276 	switch (map->_dm_buftype) {
1277 	case _BUS_DMA_BUFTYPE_LINEAR:
1278 		memcpy(cookie->id_origlinearbuf + offset, dataptr, len);
1279 		break;
1280 
1281 	case _BUS_DMA_BUFTYPE_MBUF:
1282 		m_copyback(cookie->id_origmbuf, offset, len, dataptr);
1283 		break;
1284 
1285 	case _BUS_DMA_BUFTYPE_UIO:
1286 		_bus_dma_uiomove(dataptr, cookie->id_origuio, len, UIO_READ);
1287 		break;
1288 
1289 #ifdef DIAGNOSTIC
1290 	case _BUS_DMA_BUFTYPE_RAW:
1291 		panic("%s(post): _BUS_DMA_BUFTYPE_RAW", __func__);
1292 		break;
1293 
1294 	case _BUS_DMA_BUFTYPE_INVALID:
1295 		panic("%s(post): _BUS_DMA_BUFTYPE_INVALID", __func__);
1296 		break;
1297 
1298 	default:
1299 		panic("%s(post): map %p: unknown buffer type %d\n", __func__,
1300 		    map, map->_dm_buftype);
1301 		break;
1302 #endif
1303 	}
1304 }
1305 
1306 /*
1307  * Common function for DMA-safe memory allocation.  May be called
1308  * by bus-specific DMA memory allocation functions.
1309  */
1310 
1311 extern paddr_t physical_start;
1312 extern paddr_t physical_end;
1313 
1314 int
1315 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1316     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1317     int flags)
1318 {
1319 	struct arm32_dma_range *dr;
1320 	int error, i;
1321 
1322 #ifdef DEBUG_DMA
1323 	printf("dmamem_alloc t=%p size=%#lx align=%#lx boundary=%#lx "
1324 	    "segs=%p nsegs=%#x rsegs=%p flags=%#x\n", t, size, alignment,
1325 	    boundary, segs, nsegs, rsegs, flags);
1326 #endif
1327 
1328 	if ((dr = t->_ranges) != NULL) {
1329 		error = ENOMEM;
1330 		for (i = 0; i < t->_nranges; i++, dr++) {
1331 			if (dr->dr_len == 0
1332 			    || (dr->dr_flags & _BUS_DMAMAP_NOALLOC))
1333 				continue;
1334 			error = _bus_dmamem_alloc_range(t, size, alignment,
1335 			    boundary, segs, nsegs, rsegs, flags,
1336 			    trunc_page(dr->dr_sysbase),
1337 			    trunc_page(dr->dr_sysbase + dr->dr_len));
1338 			if (error == 0)
1339 				break;
1340 		}
1341 	} else {
1342 		error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
1343 		    segs, nsegs, rsegs, flags, trunc_page(physical_start),
1344 		    trunc_page(physical_end));
1345 	}
1346 
1347 #ifdef DEBUG_DMA
1348 	printf("dmamem_alloc: =%d\n", error);
1349 #endif
1350 
1351 	return error;
1352 }
1353 
1354 /*
1355  * Common function for freeing DMA-safe memory.  May be called by
1356  * bus-specific DMA memory free functions.
1357  */
1358 void
1359 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
1360 {
1361 	struct vm_page *m;
1362 	bus_addr_t addr;
1363 	struct pglist mlist;
1364 	int curseg;
1365 
1366 #ifdef DEBUG_DMA
1367 	printf("dmamem_free: t=%p segs=%p nsegs=%#x\n", t, segs, nsegs);
1368 #endif	/* DEBUG_DMA */
1369 
1370 	/*
1371 	 * Build a list of pages to free back to the VM system.
1372 	 */
1373 	TAILQ_INIT(&mlist);
1374 	for (curseg = 0; curseg < nsegs; curseg++) {
1375 		for (addr = segs[curseg].ds_addr;
1376 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
1377 		    addr += PAGE_SIZE) {
1378 			m = PHYS_TO_VM_PAGE(addr);
1379 			TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
1380 		}
1381 	}
1382 	uvm_pglistfree(&mlist);
1383 }
1384 
1385 /*
1386  * Common function for mapping DMA-safe memory.  May be called by
1387  * bus-specific DMA memory map functions.
1388  */
1389 int
1390 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1391     size_t size, void **kvap, int flags)
1392 {
1393 	vaddr_t va;
1394 	paddr_t pa;
1395 	int curseg;
1396 	const uvm_flag_t kmflags = UVM_KMF_VAONLY
1397 	    | ((flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0);
1398 	vsize_t align = 0;
1399 
1400 #ifdef DEBUG_DMA
1401 	printf("dmamem_map: t=%p segs=%p nsegs=%#x size=%#lx flags=%#x\n", t,
1402 	    segs, nsegs, (unsigned long)size, flags);
1403 #endif	/* DEBUG_DMA */
1404 
1405 #ifdef PMAP_MAP_POOLPAGE
1406 	/*
1407 	 * If all of memory is mapped, and we are mapping a single physically
1408 	 * contiguous area then this area is already mapped.  Let's see if we
1409 	 * avoid having a separate mapping for it.
1410 	 */
1411 	if (nsegs == 1 && (flags & BUS_DMA_PREFETCHABLE) == 0) {
1412 		/*
1413 		 * If this is a non-COHERENT mapping, then the existing kernel
1414 		 * mapping is already compatible with it.
1415 		 */
1416 		bool direct_mapable = (flags & BUS_DMA_COHERENT) == 0;
1417 		pa = segs[0].ds_addr;
1418 
1419 		/*
1420 		 * This is a COHERENT mapping which, unless this address is in
1421 		 * a COHERENT dma range, will not be compatible.
1422 		 */
1423 		if (t->_ranges != NULL) {
1424 			const struct arm32_dma_range * const dr =
1425 			    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
1426 			if (dr != NULL
1427 			    && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
1428 				direct_mapable = true;
1429 			}
1430 		}
1431 
1432 #ifdef PMAP_NEED_ALLOC_POOLPAGE
1433 		/*
1434 		 * The page can only be direct mapped if was allocated out
1435 		 * of the arm poolpage vm freelist.
1436 		 */
1437 		uvm_physseg_t upm = uvm_physseg_find(atop(pa), NULL);
1438 		KASSERT(uvm_physseg_valid_p(upm));
1439 		if (direct_mapable) {
1440 			direct_mapable =
1441 			    (arm_poolpage_vmfreelist == uvm_physseg_get_free_list(upm));
1442 		}
1443 #endif
1444 
1445 		if (direct_mapable) {
1446 			*kvap = (void *)PMAP_MAP_POOLPAGE(pa);
1447 #ifdef DEBUG_DMA
1448 			printf("dmamem_map: =%p\n", *kvap);
1449 #endif	/* DEBUG_DMA */
1450 			return 0;
1451 		}
1452 	}
1453 #endif
1454 
1455 	size = round_page(size);
1456 
1457 #ifdef PMAP_MAPSIZE1
1458 	if (size >= PMAP_MAPSIZE1)
1459 		align = PMAP_MAPSIZE1;
1460 
1461 #ifdef PMAP_MAPSIZE2
1462 
1463 #if PMAP_MAPSIZE1 > PMAP_MAPSIZE2
1464 #error PMAP_MAPSIZE1 must be smaller than PMAP_MAPSIZE2
1465 #endif
1466 
1467 	if (size >= PMAP_MAPSIZE2)
1468 		align = PMAP_MAPSIZE2;
1469 
1470 #ifdef PMAP_MAPSIZE3
1471 
1472 #if PMAP_MAPSIZE2 > PMAP_MAPSIZE3
1473 #error PMAP_MAPSIZE2 must be smaller than PMAP_MAPSIZE3
1474 #endif
1475 
1476 	if (size >= PMAP_MAPSIZE3)
1477 		align = PMAP_MAPSIZE3;
1478 #endif
1479 #endif
1480 #endif
1481 
1482 	va = uvm_km_alloc(kernel_map, size, align, kmflags);
1483 	if (__predict_false(va == 0 && align > 0)) {
1484 		align = 0;
1485 		va = uvm_km_alloc(kernel_map, size, 0, kmflags);
1486 	}
1487 
1488 	if (va == 0)
1489 		return ENOMEM;
1490 
1491 	*kvap = (void *)va;
1492 
1493 	for (curseg = 0; curseg < nsegs; curseg++) {
1494 		for (pa = segs[curseg].ds_addr;
1495 		    pa < (segs[curseg].ds_addr + segs[curseg].ds_len);
1496 		    pa += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
1497 			bool uncached = (flags & BUS_DMA_COHERENT);
1498 			bool prefetchable = (flags & BUS_DMA_PREFETCHABLE);
1499 #ifdef DEBUG_DMA
1500 			printf("wiring P%#lx to V%#lx\n", pa, va);
1501 #endif	/* DEBUG_DMA */
1502 			if (size == 0)
1503 				panic("_bus_dmamem_map: size botch");
1504 
1505 			const struct arm32_dma_range * const dr =
1506 			    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
1507 			/*
1508 			 * If this dma region is coherent then there is
1509 			 * no need for an uncached mapping.
1510 			 */
1511 			if (dr != NULL
1512 			    && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
1513 				uncached = false;
1514 			}
1515 
1516 			u_int pmap_flags = PMAP_WIRED;
1517 			if (prefetchable)
1518 				pmap_flags |= PMAP_WRITE_COMBINE;
1519 			else if (uncached)
1520 				pmap_flags |= PMAP_NOCACHE;
1521 
1522 			pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE,
1523 			    pmap_flags);
1524 		}
1525 	}
1526 	pmap_update(pmap_kernel());
1527 #ifdef DEBUG_DMA
1528 	printf("dmamem_map: =%p\n", *kvap);
1529 #endif	/* DEBUG_DMA */
1530 	return 0;
1531 }
1532 
1533 /*
1534  * Common function for unmapping DMA-safe memory.  May be called by
1535  * bus-specific DMA memory unmapping functions.
1536  */
1537 void
1538 _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
1539 {
1540 
1541 #ifdef DEBUG_DMA
1542 	printf("dmamem_unmap: t=%p kva=%p size=%#zx\n", t, kva, size);
1543 #endif	/* DEBUG_DMA */
1544 	KASSERTMSG(((uintptr_t)kva & PAGE_MASK) == 0,
1545 	    "kva %p (%#"PRIxPTR")", kva, ((uintptr_t)kva & PAGE_MASK));
1546 
1547 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
1548 	/*
1549 	 * Check to see if this used direct mapped memory.  Get its physical
1550 	 * address and try to map it.  If the resultant matches the kva, then
1551 	 * it was and so we can just return since we have nothing to free up.
1552 	 */
1553 	paddr_t pa;
1554 	vaddr_t va;
1555 	(void)pmap_extract(pmap_kernel(), (vaddr_t)kva, &pa);
1556 	if (mm_md_direct_mapped_phys(pa, &va) && va == (vaddr_t)kva)
1557 		return;
1558 #endif
1559 
1560 	size = round_page(size);
1561 	pmap_kremove((vaddr_t)kva, size);
1562 	pmap_update(pmap_kernel());
1563 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
1564 }
1565 
1566 /*
1567  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
1568  * bus-specific DMA mmap(2)'ing functions.
1569  */
1570 paddr_t
1571 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1572     off_t off, int prot, int flags)
1573 {
1574 	paddr_t map_flags;
1575 	int i;
1576 
1577 	for (i = 0; i < nsegs; i++) {
1578 		KASSERTMSG((off & PAGE_MASK) == 0,
1579 		    "off %#jx (%#x)", (uintmax_t)off, (int)off & PAGE_MASK);
1580 		KASSERTMSG((segs[i].ds_addr & PAGE_MASK) == 0,
1581 		    "ds_addr %#lx (%#x)", segs[i].ds_addr,
1582 		    (int)segs[i].ds_addr & PAGE_MASK);
1583 		KASSERTMSG((segs[i].ds_len & PAGE_MASK) == 0,
1584 		    "ds_len %#lx (%#x)", segs[i].ds_addr,
1585 		    (int)segs[i].ds_addr & PAGE_MASK);
1586 		if (off >= segs[i].ds_len) {
1587 			off -= segs[i].ds_len;
1588 			continue;
1589 		}
1590 
1591 		map_flags = 0;
1592 		if (flags & BUS_DMA_PREFETCHABLE)
1593 			map_flags |= ARM_MMAP_WRITECOMBINE;
1594 
1595 		return arm_btop((u_long)segs[i].ds_addr + off) | map_flags;
1596 
1597 	}
1598 
1599 	/* Page not found. */
1600 	return -1;
1601 }
1602 
1603 /**********************************************************************
1604  * DMA utility functions
1605  **********************************************************************/
1606 
1607 /*
1608  * Utility function to load a linear buffer.  lastaddrp holds state
1609  * between invocations (for multiple-buffer loads).  segp contains
1610  * the starting segment on entrace, and the ending segment on exit.
1611  * first indicates if this is the first invocation of this function.
1612  */
1613 int
1614 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
1615     bus_size_t buflen, struct vmspace *vm, int flags)
1616 {
1617 	bus_size_t sgsize;
1618 	bus_addr_t curaddr;
1619 	vaddr_t vaddr = (vaddr_t)buf;
1620 	int error;
1621 	pmap_t pmap;
1622 
1623 #ifdef DEBUG_DMA
1624 	printf("_bus_dmamem_load_buffer(buf=%p, len=%#lx, flags=%#x)\n",
1625 	    buf, buflen, flags);
1626 #endif	/* DEBUG_DMA */
1627 
1628 	pmap = vm_map_pmap(&vm->vm_map);
1629 
1630 	while (buflen > 0) {
1631 		/*
1632 		 * Get the physical address for this segment.
1633 		 *
1634 		 */
1635 		bool coherent;
1636 		bool ok __diagused;
1637 		ok = pmap_extract_coherency(pmap, vaddr, &curaddr, &coherent);
1638 
1639 		/*
1640 		 * trying to bus_dmamap_load an unmapped buffer is a
1641 		 * programming error.
1642 		 */
1643 		KASSERT(ok);
1644 
1645 		KASSERTMSG((vaddr & PAGE_MASK) == (curaddr & PAGE_MASK),
1646 		    "va %#lx curaddr %#lx", vaddr, curaddr);
1647 
1648 		/*
1649 		 * Compute the segment size, and adjust counts.
1650 		 */
1651 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
1652 		if (buflen < sgsize)
1653 			sgsize = buflen;
1654 
1655 		error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize,
1656 		    coherent);
1657 		if (error)
1658 			return error;
1659 
1660 		vaddr += sgsize;
1661 		buflen -= sgsize;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
1667 /*
1668  * Allocate physical memory from the given physical address range.
1669  * Called by DMA-safe memory allocation methods.
1670  */
1671 int
1672 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1673     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1674     int flags, paddr_t low, paddr_t high)
1675 {
1676 	paddr_t curaddr, lastaddr;
1677 	struct vm_page *m;
1678 	struct pglist mlist;
1679 	int curseg, error;
1680 
1681 	KASSERTMSG(boundary == 0 || (boundary & (boundary - 1)) == 0,
1682 	    "invalid boundary %#lx", boundary);
1683 
1684 #ifdef DEBUG_DMA
1685 	printf("alloc_range: t=%p size=%#lx align=%#lx boundary=%#lx segs=%p nsegs=%#x rsegs=%p flags=%#x lo=%#lx hi=%#lx\n",
1686 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
1687 #endif	/* DEBUG_DMA */
1688 
1689 	/* Always round the size. */
1690 	size = round_page(size);
1691 
1692 	/*
1693 	 * We accept boundaries < size, splitting in multiple segments
1694 	 * if needed. uvm_pglistalloc does not, so compute an appropriate
1695 	 * boundary: next power of 2 >= size
1696 	 */
1697 	bus_size_t uboundary = boundary;
1698 	if (uboundary <= PAGE_SIZE) {
1699 		uboundary = 0;
1700 	} else {
1701 		while (uboundary < size) {
1702 			uboundary <<= 1;
1703 		}
1704 	}
1705 
1706 	/*
1707 	 * Allocate pages from the VM system.
1708 	 */
1709 	error = uvm_pglistalloc(size, low, high, alignment, uboundary,
1710 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
1711 	if (error)
1712 		return error;
1713 
1714 	/*
1715 	 * Compute the location, size, and number of segments actually
1716 	 * returned by the VM code.
1717 	 */
1718 	m = TAILQ_FIRST(&mlist);
1719 	curseg = 0;
1720 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
1721 	segs[curseg].ds_len = PAGE_SIZE;
1722 #ifdef DEBUG_DMA
1723 		printf("alloc: page %#lx\n", lastaddr);
1724 #endif	/* DEBUG_DMA */
1725 	m = TAILQ_NEXT(m, pageq.queue);
1726 
1727 	for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
1728 		curaddr = VM_PAGE_TO_PHYS(m);
1729 		KASSERTMSG(low <= curaddr && curaddr < high,
1730 		    "uvm_pglistalloc returned non-sensicaladdress %#lx "
1731 		    "(low=%#lx, high=%#lx\n", curaddr, low, high);
1732 #ifdef DEBUG_DMA
1733 		printf("alloc: page %#lx\n", curaddr);
1734 #endif	/* DEBUG_DMA */
1735 		if (curaddr == lastaddr + PAGE_SIZE
1736 		    && (lastaddr & boundary) == (curaddr & boundary))
1737 			segs[curseg].ds_len += PAGE_SIZE;
1738 		else {
1739 			curseg++;
1740 			if (curseg >= nsegs) {
1741 				uvm_pglistfree(&mlist);
1742 				return EFBIG;
1743 			}
1744 			segs[curseg].ds_addr = curaddr;
1745 			segs[curseg].ds_len = PAGE_SIZE;
1746 		}
1747 		lastaddr = curaddr;
1748 	}
1749 
1750 	*rsegs = curseg + 1;
1751 
1752 	return 0;
1753 }
1754 
1755 /*
1756  * Check if a memory region intersects with a DMA range, and return the
1757  * page-rounded intersection if it does.
1758  */
1759 int
1760 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
1761     paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
1762 {
1763 	struct arm32_dma_range *dr;
1764 	int i;
1765 
1766 	if (ranges == NULL)
1767 		return 0;
1768 
1769 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
1770 		if (dr->dr_sysbase <= pa &&
1771 		    pa < (dr->dr_sysbase + dr->dr_len)) {
1772 			/*
1773 			 * Beginning of region intersects with this range.
1774 			 */
1775 			*pap = trunc_page(pa);
1776 			*sizep = round_page(uimin(pa + size,
1777 			    dr->dr_sysbase + dr->dr_len) - pa);
1778 			return 1;
1779 		}
1780 		if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
1781 			/*
1782 			 * End of region intersects with this range.
1783 			 */
1784 			*pap = trunc_page(dr->dr_sysbase);
1785 			*sizep = round_page(uimin((pa + size) - dr->dr_sysbase,
1786 			    dr->dr_len));
1787 			return 1;
1788 		}
1789 	}
1790 
1791 	/* No intersection found. */
1792 	return 0;
1793 }
1794 
1795 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1796 static int
1797 _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
1798     bus_size_t size, int flags)
1799 {
1800 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
1801 	int error = 0;
1802 
1803 	KASSERT(cookie != NULL);
1804 
1805 	cookie->id_bouncebuflen = round_page(size);
1806 	error = _bus_dmamem_alloc(t, cookie->id_bouncebuflen,
1807 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
1808 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
1809 	if (error == 0) {
1810 		error = _bus_dmamem_map(t, cookie->id_bouncesegs,
1811 		    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
1812 		    (void **)&cookie->id_bouncebuf, flags);
1813 		if (error) {
1814 			_bus_dmamem_free(t, cookie->id_bouncesegs,
1815 			    cookie->id_nbouncesegs);
1816 			cookie->id_bouncebuflen = 0;
1817 			cookie->id_nbouncesegs = 0;
1818 		} else {
1819 			cookie->id_flags |= _BUS_DMA_HAS_BOUNCE;
1820 		}
1821 	} else {
1822 		cookie->id_bouncebuflen = 0;
1823 		cookie->id_nbouncesegs = 0;
1824 	}
1825 
1826 	return error;
1827 }
1828 
1829 static void
1830 _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
1831 {
1832 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
1833 
1834 	KASSERT(cookie != NULL);
1835 
1836 	_bus_dmamem_unmap(t, cookie->id_bouncebuf, cookie->id_bouncebuflen);
1837 	_bus_dmamem_free(t, cookie->id_bouncesegs, cookie->id_nbouncesegs);
1838 	cookie->id_bouncebuflen = 0;
1839 	cookie->id_nbouncesegs = 0;
1840 	cookie->id_flags &= ~_BUS_DMA_HAS_BOUNCE;
1841 }
1842 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1843 
1844 /*
1845  * This function does the same as uiomove, but takes an explicit
1846  * direction, and does not update the uio structure.
1847  */
1848 static int
1849 _bus_dma_uiomove(void *buf, struct uio *uio, size_t n, int direction)
1850 {
1851 	struct iovec *iov;
1852 	int error;
1853 	struct vmspace *vm;
1854 	char *cp;
1855 	size_t resid, cnt;
1856 	int i;
1857 
1858 	iov = uio->uio_iov;
1859 	vm = uio->uio_vmspace;
1860 	cp = buf;
1861 	resid = n;
1862 
1863 	for (i = 0; i < uio->uio_iovcnt && resid > 0; i++) {
1864 		iov = &uio->uio_iov[i];
1865 		if (iov->iov_len == 0)
1866 			continue;
1867 		cnt = MIN(resid, iov->iov_len);
1868 
1869 		if (!VMSPACE_IS_KERNEL_P(vm)) {
1870 			preempt_point();
1871 		}
1872 		if (direction == UIO_READ) {
1873 			error = copyout_vmspace(vm, cp, iov->iov_base, cnt);
1874 		} else {
1875 			error = copyin_vmspace(vm, iov->iov_base, cp, cnt);
1876 		}
1877 		if (error)
1878 			return error;
1879 		cp += cnt;
1880 		resid -= cnt;
1881 	}
1882 	return 0;
1883 }
1884 
1885 int
1886 _bus_dmatag_subregion(bus_dma_tag_t tag, bus_addr_t min_addr,
1887     bus_addr_t max_addr, bus_dma_tag_t *newtag, int flags)
1888 {
1889 	if (min_addr >= max_addr)
1890 		return EOPNOTSUPP;
1891 
1892 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1893 	struct arm32_dma_range *dr;
1894 	bool psubset = true;
1895 	size_t nranges = 0;
1896 	size_t i;
1897 	for (i = 0, dr = tag->_ranges; i < tag->_nranges; i++, dr++) {
1898 		/*
1899 		 * If the new {min,max}_addr are narrower than any of the
1900 		 * ranges in the parent tag then we need a new tag;
1901 		 * otherwise the parent tag is a subset of the new
1902 		 * range and can continue to be used.
1903 		 */
1904 		if (min_addr > dr->dr_sysbase
1905 		    || max_addr < dr->dr_sysbase + dr->dr_len - 1) {
1906 			psubset = false;
1907 		}
1908 		if (min_addr <= dr->dr_sysbase + dr->dr_len
1909 		    && max_addr >= dr->dr_sysbase) {
1910 			nranges++;
1911 		}
1912 	}
1913 	if (nranges == 0) {
1914 		nranges = 1;
1915 		psubset = false;
1916 	}
1917 	if (psubset) {
1918 		*newtag = tag;
1919 		/* if the tag must be freed, add a reference */
1920 		if (tag->_tag_needs_free)
1921 			(tag->_tag_needs_free)++;
1922 		return 0;
1923 	}
1924 
1925 	const size_t tagsize = sizeof(*tag) + nranges * sizeof(*dr);
1926 	if ((*newtag = kmem_intr_zalloc(tagsize,
1927 	    (flags & BUS_DMA_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)) == NULL)
1928 		return ENOMEM;
1929 
1930 	dr = (void *)(*newtag + 1);
1931 	**newtag = *tag;
1932 	(*newtag)->_tag_needs_free = 1;
1933 	(*newtag)->_ranges = dr;
1934 	(*newtag)->_nranges = nranges;
1935 
1936 	if (tag->_ranges == NULL) {
1937 		dr->dr_sysbase = min_addr;
1938 		dr->dr_busbase = min_addr;
1939 		dr->dr_len = max_addr + 1 - min_addr;
1940 	} else {
1941 		struct arm32_dma_range *pdr;
1942 
1943 		for (i = 0, pdr = tag->_ranges; i < tag->_nranges; i++, pdr++) {
1944 			KASSERT(nranges != 0);
1945 
1946 			if (min_addr > pdr->dr_sysbase + pdr->dr_len
1947 			    || max_addr < pdr->dr_sysbase) {
1948 				/*
1949 				 * this range doesn't overlap with new limits,
1950 				 * so skip.
1951 				 */
1952 				continue;
1953 			}
1954 			/*
1955 			 * Copy the range and adjust to fit within the new
1956 			 * limits
1957 			 */
1958 			dr[0] = pdr[0];
1959 			if (dr->dr_sysbase < min_addr) {
1960 				psize_t diff = min_addr - dr->dr_sysbase;
1961 				dr->dr_busbase += diff;
1962 				dr->dr_len -= diff;
1963 				dr->dr_sysbase += diff;
1964 			}
1965 			if (max_addr <= dr->dr_sysbase + dr->dr_len - 1) {
1966 				dr->dr_len = max_addr + 1 - dr->dr_sysbase;
1967 			}
1968 			dr++;
1969 			nranges--;
1970 		}
1971 	}
1972 
1973 	return 0;
1974 #else
1975 	return EOPNOTSUPP;
1976 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1977 }
1978 
1979 void
1980 _bus_dmatag_destroy(bus_dma_tag_t tag)
1981 {
1982 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1983 	switch (tag->_tag_needs_free) {
1984 	case 0:
1985 		break;				/* not allocated with kmem */
1986 	case 1: {
1987 		const size_t tagsize = sizeof(*tag)
1988 		    + tag->_nranges * sizeof(*tag->_ranges);
1989 		kmem_intr_free(tag, tagsize);	/* last reference to tag */
1990 		break;
1991 	}
1992 	default:
1993 		(tag->_tag_needs_free)--;	/* one less reference */
1994 	}
1995 #endif
1996 }
1997