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