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