Home | History | Annotate | Line # | Download | only in arm32
bus_dma.c revision 1.42.4.1
      1 /*	$NetBSD: bus_dma.c,v 1.42.4.1 2005/01/28 10:33:58 yamt 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #define _ARM32_BUS_DMA_PRIVATE
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.42.4.1 2005/01/28 10:33:58 yamt Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/proc.h>
     49 #include <sys/buf.h>
     50 #include <sys/reboot.h>
     51 #include <sys/conf.h>
     52 #include <sys/file.h>
     53 #include <sys/malloc.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/vnode.h>
     56 #include <sys/device.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #include <machine/bus.h>
     61 #include <machine/cpu.h>
     62 
     63 #include <arm/cpufunc.h>
     64 
     65 int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
     66 	    bus_size_t, struct proc *, int);
     67 struct arm32_dma_range *_bus_dma_inrange(struct arm32_dma_range *,
     68 	    int, bus_addr_t);
     69 
     70 /*
     71  * Check to see if the specified page is in an allowed DMA range.
     72  */
     73 __inline struct arm32_dma_range *
     74 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
     75     bus_addr_t curaddr)
     76 {
     77 	struct arm32_dma_range *dr;
     78 	int i;
     79 
     80 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
     81 		if (curaddr >= dr->dr_sysbase &&
     82 		    round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
     83 			return (dr);
     84 	}
     85 
     86 	return (NULL);
     87 }
     88 
     89 /*
     90  * Common function to load the specified physical address into the
     91  * DMA map, coalescing segments and boundary checking as necessary.
     92  */
     93 static int
     94 _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
     95     bus_addr_t paddr, bus_size_t size)
     96 {
     97 	bus_dma_segment_t * const segs = map->dm_segs;
     98 	int nseg = map->dm_nsegs;
     99 	bus_addr_t lastaddr = 0xdead;	/* XXX gcc */
    100 	bus_addr_t bmask = ~(map->_dm_boundary - 1);
    101 	bus_addr_t curaddr;
    102 	bus_size_t sgsize;
    103 
    104 	if (nseg > 0)
    105 		lastaddr = segs[nseg-1].ds_addr + segs[nseg-1].ds_len;
    106  again:
    107 	sgsize = size;
    108 
    109 	/* Make sure we're in an allowed DMA range. */
    110 	if (t->_ranges != NULL) {
    111 		/* XXX cache last result? */
    112 		const struct arm32_dma_range * const dr =
    113 		    _bus_dma_inrange(t->_ranges, t->_nranges, paddr);
    114 		if (dr == NULL)
    115 			return (EINVAL);
    116 
    117 		/*
    118 		 * In a valid DMA range.  Translate the physical
    119 		 * memory address to an address in the DMA window.
    120 		 */
    121 		curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
    122 	} else
    123 		curaddr = paddr;
    124 
    125 	/*
    126 	 * Make sure we don't cross any boundaries.
    127 	 */
    128 	if (map->_dm_boundary > 0) {
    129 		bus_addr_t baddr;	/* next boundary address */
    130 
    131 		baddr = (curaddr + map->_dm_boundary) & bmask;
    132 		if (sgsize > (baddr - curaddr))
    133 			sgsize = (baddr - curaddr);
    134 	}
    135 
    136 	/*
    137 	 * Insert chunk into a segment, coalescing with the
    138 	 * previous segment if possible.
    139 	 */
    140 	if (nseg > 0 && curaddr == lastaddr &&
    141 	    segs[nseg-1].ds_len + sgsize <= map->_dm_maxsegsz &&
    142 	    (map->_dm_boundary == 0 ||
    143 	     (segs[nseg-1].ds_addr & bmask) == (curaddr & bmask))) {
    144 	     	/* coalesce */
    145 		segs[nseg-1].ds_len += sgsize;
    146 	} else if (nseg >= map->_dm_segcnt) {
    147 		return (EFBIG);
    148 	} else {
    149 		/* new segment */
    150 		segs[nseg].ds_addr = curaddr;
    151 		segs[nseg].ds_len = sgsize;
    152 		nseg++;
    153 	}
    154 
    155 	lastaddr = curaddr + sgsize;
    156 
    157 	paddr += sgsize;
    158 	size -= sgsize;
    159 	if (size > 0)
    160 		goto again;
    161 
    162 	map->dm_nsegs = nseg;
    163 	return (0);
    164 }
    165 
    166 /*
    167  * Common function for DMA map creation.  May be called by bus-specific
    168  * DMA map creation functions.
    169  */
    170 int
    171 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    172     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    173 {
    174 	struct arm32_bus_dmamap *map;
    175 	void *mapstore;
    176 	size_t mapsize;
    177 
    178 #ifdef DEBUG_DMA
    179 	printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
    180 	    t, size, nsegments, maxsegsz, boundary, flags);
    181 #endif	/* DEBUG_DMA */
    182 
    183 	/*
    184 	 * Allocate and initialize the DMA map.  The end of the map
    185 	 * is a variable-sized array of segments, so we allocate enough
    186 	 * room for them in one shot.
    187 	 *
    188 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
    189 	 * of ALLOCNOW notifies others that we've reserved these resources,
    190 	 * and they are not to be freed.
    191 	 *
    192 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
    193 	 * the (nsegments - 1).
    194 	 */
    195 	mapsize = sizeof(struct arm32_bus_dmamap) +
    196 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
    197 	if ((mapstore = malloc(mapsize, M_DMAMAP,
    198 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
    199 		return (ENOMEM);
    200 
    201 	memset(mapstore, 0, mapsize);
    202 	map = (struct arm32_bus_dmamap *)mapstore;
    203 	map->_dm_size = size;
    204 	map->_dm_segcnt = nsegments;
    205 	map->_dm_maxsegsz = maxsegsz;
    206 	map->_dm_boundary = boundary;
    207 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    208 	map->_dm_origbuf = NULL;
    209 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    210 	map->_dm_proc = NULL;
    211 	map->dm_mapsize = 0;		/* no valid mappings */
    212 	map->dm_nsegs = 0;
    213 
    214 	*dmamp = map;
    215 #ifdef DEBUG_DMA
    216 	printf("dmamap_create:map=%p\n", map);
    217 #endif	/* DEBUG_DMA */
    218 	return (0);
    219 }
    220 
    221 /*
    222  * Common function for DMA map destruction.  May be called by bus-specific
    223  * DMA map destruction functions.
    224  */
    225 void
    226 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    227 {
    228 
    229 #ifdef DEBUG_DMA
    230 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
    231 #endif	/* DEBUG_DMA */
    232 
    233 	/*
    234 	 * Explicit unload.
    235 	 */
    236 	map->dm_mapsize = 0;
    237 	map->dm_nsegs = 0;
    238 	map->_dm_origbuf = NULL;
    239 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    240 	map->_dm_proc = NULL;
    241 
    242 	free(map, M_DMAMAP);
    243 }
    244 
    245 /*
    246  * Common function for loading a DMA map with a linear buffer.  May
    247  * be called by bus-specific DMA map load functions.
    248  */
    249 int
    250 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    251     bus_size_t buflen, struct proc *p, int flags)
    252 {
    253 	int error;
    254 
    255 #ifdef DEBUG_DMA
    256 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
    257 	    t, map, buf, buflen, p, flags);
    258 #endif	/* DEBUG_DMA */
    259 
    260 	/*
    261 	 * Make sure that on error condition we return "no valid mappings".
    262 	 */
    263 	map->dm_mapsize = 0;
    264 	map->dm_nsegs = 0;
    265 
    266 	if (buflen > map->_dm_size)
    267 		return (EINVAL);
    268 
    269 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    270 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
    271 
    272 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags);
    273 	if (error == 0) {
    274 		map->dm_mapsize = buflen;
    275 		map->_dm_origbuf = buf;
    276 		map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
    277 		map->_dm_proc = p;
    278 	}
    279 #ifdef DEBUG_DMA
    280 	printf("dmamap_load: error=%d\n", error);
    281 #endif	/* DEBUG_DMA */
    282 	return (error);
    283 }
    284 
    285 /*
    286  * Like _bus_dmamap_load(), but for mbufs.
    287  */
    288 int
    289 _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    290     int flags)
    291 {
    292 	int error;
    293 	struct mbuf *m;
    294 
    295 #ifdef DEBUG_DMA
    296 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
    297 	    t, map, m0, flags);
    298 #endif	/* DEBUG_DMA */
    299 
    300 	/*
    301 	 * Make sure that on error condition we return "no valid mappings."
    302 	 */
    303 	map->dm_mapsize = 0;
    304 	map->dm_nsegs = 0;
    305 
    306 #ifdef DIAGNOSTIC
    307 	if ((m0->m_flags & M_PKTHDR) == 0)
    308 		panic("_bus_dmamap_load_mbuf: no packet header");
    309 #endif	/* DIAGNOSTIC */
    310 
    311 	if (m0->m_pkthdr.len > map->_dm_size)
    312 		return (EINVAL);
    313 
    314 	/*
    315 	 * Mbuf chains should almost never have coherent (i.e.
    316 	 * un-cached) mappings, so clear that flag now.
    317 	 */
    318 	map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
    319 
    320 	error = 0;
    321 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    322 		int offset;
    323 		int remainbytes;
    324 		const struct vm_page * const *pgs;
    325 		paddr_t paddr;
    326 		int size;
    327 
    328 		if (m->m_len == 0)
    329 			continue;
    330 		switch (m->m_flags & (M_EXT|M_CLUSTER|M_EXT_PAGES)) {
    331 		case M_EXT|M_CLUSTER:
    332 			/* XXX KDASSERT */
    333 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
    334 			paddr = m->m_ext.ext_paddr +
    335 			    (m->m_data - m->m_ext.ext_buf);
    336 			size = m->m_len;
    337 			error = _bus_dmamap_load_paddr(t, map, paddr, size);
    338 			break;
    339 
    340 		case M_EXT|M_EXT_PAGES:
    341 			KASSERT(m->m_ext.ext_buf <= m->m_data);
    342 			KASSERT(m->m_data <=
    343 			    m->m_ext.ext_buf + m->m_ext.ext_size);
    344 
    345 			offset = (vaddr_t)m->m_data -
    346 			    trunc_page((vaddr_t)m->m_ext.ext_buf);
    347 			remainbytes = m->m_len;
    348 
    349 			/* skip uninteresting pages */
    350 			pgs = (const struct vm_page * const *)
    351 			    m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
    352 
    353 			offset &= PAGE_MASK;	/* offset in the first page */
    354 
    355 			/* load each page */
    356 			while (remainbytes > 0) {
    357 				const struct vm_page *pg;
    358 
    359 				size = MIN(remainbytes, PAGE_SIZE - offset);
    360 
    361 				pg = *pgs++;
    362 				KASSERT(pg);
    363 				paddr = VM_PAGE_TO_PHYS(pg) + offset;
    364 
    365 				error = _bus_dmamap_load_paddr(t, map,
    366 				    paddr, size);
    367 				if (error)
    368 					break;
    369 				offset = 0;
    370 				remainbytes -= size;
    371 			}
    372 			break;
    373 
    374 		case 0:
    375 			paddr = m->m_paddr + M_BUFOFFSET(m) +
    376 			    (m->m_data - M_BUFADDR(m));
    377 			size = m->m_len;
    378 			error = _bus_dmamap_load_paddr(t, map, paddr, size);
    379 			break;
    380 
    381 		default:
    382 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
    383 			    m->m_len, NULL, flags);
    384 		}
    385 	}
    386 	if (error == 0) {
    387 		map->dm_mapsize = m0->m_pkthdr.len;
    388 		map->_dm_origbuf = m0;
    389 		map->_dm_buftype = ARM32_BUFTYPE_MBUF;
    390 		map->_dm_proc = NULL;	/* always kernel */
    391 	}
    392 #ifdef DEBUG_DMA
    393 	printf("dmamap_load_mbuf: error=%d\n", error);
    394 #endif	/* DEBUG_DMA */
    395 	return (error);
    396 }
    397 
    398 /*
    399  * Like _bus_dmamap_load(), but for uios.
    400  */
    401 int
    402 _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    403     int flags)
    404 {
    405 	int i, error;
    406 	bus_size_t minlen, resid;
    407 	struct proc *p = NULL;
    408 	struct iovec *iov;
    409 	caddr_t addr;
    410 
    411 	/*
    412 	 * Make sure that on error condition we return "no valid mappings."
    413 	 */
    414 	map->dm_mapsize = 0;
    415 	map->dm_nsegs = 0;
    416 
    417 	resid = uio->uio_resid;
    418 	iov = uio->uio_iov;
    419 
    420 	if (uio->uio_segflg == UIO_USERSPACE) {
    421 		p = uio->uio_procp;
    422 #ifdef DIAGNOSTIC
    423 		if (p == NULL)
    424 			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
    425 #endif
    426 	}
    427 
    428 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    429 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
    430 
    431 	error = 0;
    432 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    433 		/*
    434 		 * Now at the first iovec to load.  Load each iovec
    435 		 * until we have exhausted the residual count.
    436 		 */
    437 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    438 		addr = (caddr_t)iov[i].iov_base;
    439 
    440 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
    441 		    p, flags);
    442 
    443 		resid -= minlen;
    444 	}
    445 	if (error == 0) {
    446 		map->dm_mapsize = uio->uio_resid;
    447 		map->_dm_origbuf = uio;
    448 		map->_dm_buftype = ARM32_BUFTYPE_UIO;
    449 		map->_dm_proc = p;
    450 	}
    451 	return (error);
    452 }
    453 
    454 /*
    455  * Like _bus_dmamap_load(), but for raw memory allocated with
    456  * bus_dmamem_alloc().
    457  */
    458 int
    459 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    460     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    461 {
    462 
    463 	panic("_bus_dmamap_load_raw: not implemented");
    464 }
    465 
    466 /*
    467  * Common function for unloading a DMA map.  May be called by
    468  * bus-specific DMA map unload functions.
    469  */
    470 void
    471 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    472 {
    473 
    474 #ifdef DEBUG_DMA
    475 	printf("dmamap_unload: t=%p map=%p\n", t, map);
    476 #endif	/* DEBUG_DMA */
    477 
    478 	/*
    479 	 * No resources to free; just mark the mappings as
    480 	 * invalid.
    481 	 */
    482 	map->dm_mapsize = 0;
    483 	map->dm_nsegs = 0;
    484 	map->_dm_origbuf = NULL;
    485 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    486 	map->_dm_proc = NULL;
    487 }
    488 
    489 static __inline void
    490 _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    491     bus_size_t len, int ops)
    492 {
    493 	vaddr_t addr = (vaddr_t) map->_dm_origbuf;
    494 
    495 	addr += offset;
    496 
    497 	switch (ops) {
    498 	case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    499 		cpu_dcache_wbinv_range(addr, len);
    500 		break;
    501 
    502 	case BUS_DMASYNC_PREREAD:
    503 		if (((addr | len) & arm_dcache_align_mask) == 0)
    504 			cpu_dcache_inv_range(addr, len);
    505 		else
    506 			cpu_dcache_wbinv_range(addr, len);
    507 		break;
    508 
    509 	case BUS_DMASYNC_PREWRITE:
    510 		cpu_dcache_wb_range(addr, len);
    511 		break;
    512 	}
    513 }
    514 
    515 static __inline void
    516 _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    517     bus_size_t len, int ops)
    518 {
    519 	struct mbuf *m, *m0 = map->_dm_origbuf;
    520 	bus_size_t minlen, moff;
    521 	vaddr_t maddr;
    522 
    523 	for (moff = offset, m = m0; m != NULL && len != 0;
    524 	     m = m->m_next) {
    525 		/* Find the beginning mbuf. */
    526 		if (moff >= m->m_len) {
    527 			moff -= m->m_len;
    528 			continue;
    529 		}
    530 
    531 		/*
    532 		 * Now at the first mbuf to sync; nail each one until
    533 		 * we have exhausted the length.
    534 		 */
    535 		minlen = m->m_len - moff;
    536 		if (len < minlen)
    537 			minlen = len;
    538 
    539 		maddr = mtod(m, vaddr_t);
    540 		maddr += moff;
    541 
    542 		/*
    543 		 * We can save a lot of work here if we know the mapping
    544 		 * is read-only at the MMU:
    545 		 *
    546 		 * If a mapping is read-only, no dirty cache blocks will
    547 		 * exist for it.  If a writable mapping was made read-only,
    548 		 * we know any dirty cache lines for the range will have
    549 		 * been cleaned for us already.  Therefore, if the upper
    550 		 * layer can tell us we have a read-only mapping, we can
    551 		 * skip all cache cleaning.
    552 		 *
    553 		 * NOTE: This only works if we know the pmap cleans pages
    554 		 * before making a read-write -> read-only transition.  If
    555 		 * this ever becomes non-true (e.g. Physically Indexed
    556 		 * cache), this will have to be revisited.
    557 		 */
    558 		switch (ops) {
    559 		case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    560 			if (! M_ROMAP(m)) {
    561 				cpu_dcache_wbinv_range(maddr, minlen);
    562 				break;
    563 			}
    564 			/* else FALLTHROUGH */
    565 
    566 		case BUS_DMASYNC_PREREAD:
    567 			if (((maddr | minlen) & arm_dcache_align_mask) == 0)
    568 				cpu_dcache_inv_range(maddr, minlen);
    569 			else
    570 				cpu_dcache_wbinv_range(maddr, minlen);
    571 			break;
    572 
    573 		case BUS_DMASYNC_PREWRITE:
    574 			if (! M_ROMAP(m))
    575 				cpu_dcache_wb_range(maddr, minlen);
    576 			break;
    577 		}
    578 		moff = 0;
    579 		len -= minlen;
    580 	}
    581 }
    582 
    583 static __inline void
    584 _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    585     bus_size_t len, int ops)
    586 {
    587 	struct uio *uio = map->_dm_origbuf;
    588 	struct iovec *iov;
    589 	bus_size_t minlen, ioff;
    590 	vaddr_t addr;
    591 
    592 	for (iov = uio->uio_iov, ioff = offset; len != 0; iov++) {
    593 		/* Find the beginning iovec. */
    594 		if (ioff >= iov->iov_len) {
    595 			ioff -= iov->iov_len;
    596 			continue;
    597 		}
    598 
    599 		/*
    600 		 * Now at the first iovec to sync; nail each one until
    601 		 * we have exhausted the length.
    602 		 */
    603 		minlen = iov->iov_len - ioff;
    604 		if (len < minlen)
    605 			minlen = len;
    606 
    607 		addr = (vaddr_t) iov->iov_base;
    608 		addr += ioff;
    609 
    610 		switch (ops) {
    611 		case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    612 			cpu_dcache_wbinv_range(addr, minlen);
    613 			break;
    614 
    615 		case BUS_DMASYNC_PREREAD:
    616 			if (((addr | minlen) & arm_dcache_align_mask) == 0)
    617 				cpu_dcache_inv_range(addr, minlen);
    618 			else
    619 				cpu_dcache_wbinv_range(addr, minlen);
    620 			break;
    621 
    622 		case BUS_DMASYNC_PREWRITE:
    623 			cpu_dcache_wb_range(addr, minlen);
    624 			break;
    625 		}
    626 		ioff = 0;
    627 		len -= minlen;
    628 	}
    629 }
    630 
    631 /*
    632  * Common function for DMA map synchronization.  May be called
    633  * by bus-specific DMA map synchronization functions.
    634  *
    635  * This version works for the Virtually Indexed Virtually Tagged
    636  * cache found on 32-bit ARM processors.
    637  *
    638  * XXX Should have separate versions for write-through vs.
    639  * XXX write-back caches.  We currently assume write-back
    640  * XXX here, which is not as efficient as it could be for
    641  * XXX the write-through case.
    642  */
    643 void
    644 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    645     bus_size_t len, int ops)
    646 {
    647 
    648 #ifdef DEBUG_DMA
    649 	printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
    650 	    t, map, offset, len, ops);
    651 #endif	/* DEBUG_DMA */
    652 
    653 	/*
    654 	 * Mixing of PRE and POST operations is not allowed.
    655 	 */
    656 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    657 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    658 		panic("_bus_dmamap_sync: mix PRE and POST");
    659 
    660 #ifdef DIAGNOSTIC
    661 	if (offset >= map->dm_mapsize)
    662 		panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
    663 		    offset, map->dm_mapsize);
    664 	if (len == 0 || (offset + len) > map->dm_mapsize)
    665 		panic("_bus_dmamap_sync: bad length");
    666 #endif
    667 
    668 	/*
    669 	 * For a virtually-indexed write-back cache, we need
    670 	 * to do the following things:
    671 	 *
    672 	 *	PREREAD -- Invalidate the D-cache.  We do this
    673 	 *	here in case a write-back is required by the back-end.
    674 	 *
    675 	 *	PREWRITE -- Write-back the D-cache.  Note that if
    676 	 *	we are doing a PREREAD|PREWRITE, we can collapse
    677 	 *	the whole thing into a single Wb-Inv.
    678 	 *
    679 	 *	POSTREAD -- Nothing.
    680 	 *
    681 	 *	POSTWRITE -- Nothing.
    682 	 */
    683 
    684 	ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    685 	if (ops == 0)
    686 		return;
    687 
    688 	/* Skip cache frobbing if mapping was COHERENT. */
    689 	if (map->_dm_flags & ARM32_DMAMAP_COHERENT) {
    690 		/* Drain the write buffer. */
    691 		cpu_drain_writebuf();
    692 		return;
    693 	}
    694 
    695 	/*
    696 	 * If the mapping belongs to a non-kernel vmspace, and the
    697 	 * vmspace has not been active since the last time a full
    698 	 * cache flush was performed, we don't need to do anything.
    699 	 */
    700 	if (__predict_false(map->_dm_proc != NULL &&
    701 	    map->_dm_proc->p_vmspace->vm_map.pmap->pm_cstate.cs_cache_d == 0))
    702 		return;
    703 
    704 	switch (map->_dm_buftype) {
    705 	case ARM32_BUFTYPE_LINEAR:
    706 		_bus_dmamap_sync_linear(t, map, offset, len, ops);
    707 		break;
    708 
    709 	case ARM32_BUFTYPE_MBUF:
    710 		_bus_dmamap_sync_mbuf(t, map, offset, len, ops);
    711 		break;
    712 
    713 	case ARM32_BUFTYPE_UIO:
    714 		_bus_dmamap_sync_uio(t, map, offset, len, ops);
    715 		break;
    716 
    717 	case ARM32_BUFTYPE_RAW:
    718 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
    719 		break;
    720 
    721 	case ARM32_BUFTYPE_INVALID:
    722 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
    723 		break;
    724 
    725 	default:
    726 		printf("unknown buffer type %d\n", map->_dm_buftype);
    727 		panic("_bus_dmamap_sync");
    728 	}
    729 
    730 	/* Drain the write buffer. */
    731 	cpu_drain_writebuf();
    732 }
    733 
    734 /*
    735  * Common function for DMA-safe memory allocation.  May be called
    736  * by bus-specific DMA memory allocation functions.
    737  */
    738 
    739 extern paddr_t physical_start;
    740 extern paddr_t physical_end;
    741 
    742 int
    743 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    744     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    745     int flags)
    746 {
    747 	struct arm32_dma_range *dr;
    748 	int error, i;
    749 
    750 #ifdef DEBUG_DMA
    751 	printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
    752 	    "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
    753 	    boundary, segs, nsegs, rsegs, flags);
    754 #endif
    755 
    756 	if ((dr = t->_ranges) != NULL) {
    757 		error = ENOMEM;
    758 		for (i = 0; i < t->_nranges; i++, dr++) {
    759 			if (dr->dr_len == 0)
    760 				continue;
    761 			error = _bus_dmamem_alloc_range(t, size, alignment,
    762 			    boundary, segs, nsegs, rsegs, flags,
    763 			    trunc_page(dr->dr_sysbase),
    764 			    trunc_page(dr->dr_sysbase + dr->dr_len));
    765 			if (error == 0)
    766 				break;
    767 		}
    768 	} else {
    769 		error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
    770 		    segs, nsegs, rsegs, flags, trunc_page(physical_start),
    771 		    trunc_page(physical_end));
    772 	}
    773 
    774 #ifdef DEBUG_DMA
    775 	printf("dmamem_alloc: =%d\n", error);
    776 #endif
    777 
    778 	return(error);
    779 }
    780 
    781 /*
    782  * Common function for freeing DMA-safe memory.  May be called by
    783  * bus-specific DMA memory free functions.
    784  */
    785 void
    786 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    787 {
    788 	struct vm_page *m;
    789 	bus_addr_t addr;
    790 	struct pglist mlist;
    791 	int curseg;
    792 
    793 #ifdef DEBUG_DMA
    794 	printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
    795 #endif	/* DEBUG_DMA */
    796 
    797 	/*
    798 	 * Build a list of pages to free back to the VM system.
    799 	 */
    800 	TAILQ_INIT(&mlist);
    801 	for (curseg = 0; curseg < nsegs; curseg++) {
    802 		for (addr = segs[curseg].ds_addr;
    803 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    804 		    addr += PAGE_SIZE) {
    805 			m = PHYS_TO_VM_PAGE(addr);
    806 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    807 		}
    808 	}
    809 	uvm_pglistfree(&mlist);
    810 }
    811 
    812 /*
    813  * Common function for mapping DMA-safe memory.  May be called by
    814  * bus-specific DMA memory map functions.
    815  */
    816 int
    817 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    818     size_t size, caddr_t *kvap, int flags)
    819 {
    820 	vaddr_t va;
    821 	bus_addr_t addr;
    822 	int curseg;
    823 	pt_entry_t *ptep/*, pte*/;
    824 
    825 #ifdef DEBUG_DMA
    826 	printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
    827 	    segs, nsegs, (unsigned long)size, flags);
    828 #endif	/* DEBUG_DMA */
    829 
    830 	size = round_page(size);
    831 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
    832 
    833 	if (va == 0)
    834 		return (ENOMEM);
    835 
    836 	*kvap = (caddr_t)va;
    837 
    838 	for (curseg = 0; curseg < nsegs; curseg++) {
    839 		for (addr = segs[curseg].ds_addr;
    840 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    841 		    addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    842 #ifdef DEBUG_DMA
    843 			printf("wiring p%lx to v%lx", addr, va);
    844 #endif	/* DEBUG_DMA */
    845 			if (size == 0)
    846 				panic("_bus_dmamem_map: size botch");
    847 			pmap_enter(pmap_kernel(), va, addr,
    848 			    VM_PROT_READ | VM_PROT_WRITE,
    849 			    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
    850 			/*
    851 			 * If the memory must remain coherent with the
    852 			 * cache then we must make the memory uncacheable
    853 			 * in order to maintain virtual cache coherency.
    854 			 * We must also guarantee the cache does not already
    855 			 * contain the virtal addresses we are making
    856 			 * uncacheable.
    857 			 */
    858 			if (flags & BUS_DMA_COHERENT) {
    859 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
    860 				cpu_drain_writebuf();
    861 				ptep = vtopte(va);
    862 				*ptep &= ~L2_S_CACHE_MASK;
    863 				PTE_SYNC(ptep);
    864 				tlb_flush();
    865 			}
    866 #ifdef DEBUG_DMA
    867 			ptep = vtopte(va);
    868 			printf(" pte=v%p *pte=%x\n", ptep, *ptep);
    869 #endif	/* DEBUG_DMA */
    870 		}
    871 	}
    872 	pmap_update(pmap_kernel());
    873 #ifdef DEBUG_DMA
    874 	printf("dmamem_map: =%p\n", *kvap);
    875 #endif	/* DEBUG_DMA */
    876 	return (0);
    877 }
    878 
    879 /*
    880  * Common function for unmapping DMA-safe memory.  May be called by
    881  * bus-specific DMA memory unmapping functions.
    882  */
    883 void
    884 _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    885 {
    886 
    887 #ifdef DEBUG_DMA
    888 	printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
    889 	    (unsigned long)size);
    890 #endif	/* DEBUG_DMA */
    891 #ifdef DIAGNOSTIC
    892 	if ((u_long)kva & PGOFSET)
    893 		panic("_bus_dmamem_unmap");
    894 #endif	/* DIAGNOSTIC */
    895 
    896 	size = round_page(size);
    897 	pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
    898 	pmap_update(pmap_kernel());
    899 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
    900 }
    901 
    902 /*
    903  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    904  * bus-specific DMA mmap(2)'ing functions.
    905  */
    906 paddr_t
    907 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    908     off_t off, int prot, int flags)
    909 {
    910 	int i;
    911 
    912 	for (i = 0; i < nsegs; i++) {
    913 #ifdef DIAGNOSTIC
    914 		if (off & PGOFSET)
    915 			panic("_bus_dmamem_mmap: offset unaligned");
    916 		if (segs[i].ds_addr & PGOFSET)
    917 			panic("_bus_dmamem_mmap: segment unaligned");
    918 		if (segs[i].ds_len & PGOFSET)
    919 			panic("_bus_dmamem_mmap: segment size not multiple"
    920 			    " of page size");
    921 #endif	/* DIAGNOSTIC */
    922 		if (off >= segs[i].ds_len) {
    923 			off -= segs[i].ds_len;
    924 			continue;
    925 		}
    926 
    927 		return (arm_btop((u_long)segs[i].ds_addr + off));
    928 	}
    929 
    930 	/* Page not found. */
    931 	return (-1);
    932 }
    933 
    934 /**********************************************************************
    935  * DMA utility functions
    936  **********************************************************************/
    937 
    938 /*
    939  * Utility function to load a linear buffer.  lastaddrp holds state
    940  * between invocations (for multiple-buffer loads).  segp contains
    941  * the starting segment on entrace, and the ending segment on exit.
    942  * first indicates if this is the first invocation of this function.
    943  */
    944 int
    945 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    946     bus_size_t buflen, struct proc *p, int flags)
    947 {
    948 	bus_size_t sgsize;
    949 	bus_addr_t curaddr;
    950 	vaddr_t vaddr = (vaddr_t)buf;
    951 	pd_entry_t *pde;
    952 	pt_entry_t pte;
    953 	int error;
    954 	pmap_t pmap;
    955 	pt_entry_t *ptep;
    956 
    957 #ifdef DEBUG_DMA
    958 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
    959 	    buf, buflen, flags);
    960 #endif	/* DEBUG_DMA */
    961 
    962 	if (p != NULL)
    963 		pmap = p->p_vmspace->vm_map.pmap;
    964 	else
    965 		pmap = pmap_kernel();
    966 
    967 	while (buflen > 0) {
    968 		/*
    969 		 * Get the physical address for this segment.
    970 		 *
    971 		 * XXX Don't support checking for coherent mappings
    972 		 * XXX in user address space.
    973 		 */
    974 		if (__predict_true(pmap == pmap_kernel())) {
    975 			(void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
    976 			if (__predict_false(pmap_pde_section(pde))) {
    977 				curaddr = (*pde & L1_S_FRAME) |
    978 				    (vaddr & L1_S_OFFSET);
    979 				if (*pde & L1_S_CACHE_MASK) {
    980 					map->_dm_flags &=
    981 					    ~ARM32_DMAMAP_COHERENT;
    982 				}
    983 			} else {
    984 				pte = *ptep;
    985 				KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
    986 				if (__predict_false((pte & L2_TYPE_MASK)
    987 						    == L2_TYPE_L)) {
    988 					curaddr = (pte & L2_L_FRAME) |
    989 					    (vaddr & L2_L_OFFSET);
    990 					if (pte & L2_L_CACHE_MASK) {
    991 						map->_dm_flags &=
    992 						    ~ARM32_DMAMAP_COHERENT;
    993 					}
    994 				} else {
    995 					curaddr = (pte & L2_S_FRAME) |
    996 					    (vaddr & L2_S_OFFSET);
    997 					if (pte & L2_S_CACHE_MASK) {
    998 						map->_dm_flags &=
    999 						    ~ARM32_DMAMAP_COHERENT;
   1000 					}
   1001 				}
   1002 			}
   1003 		} else {
   1004 			(void) pmap_extract(pmap, vaddr, &curaddr);
   1005 			map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
   1006 		}
   1007 
   1008 		/*
   1009 		 * Compute the segment size, and adjust counts.
   1010 		 */
   1011 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
   1012 		if (buflen < sgsize)
   1013 			sgsize = buflen;
   1014 
   1015 		error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize);
   1016 		if (error)
   1017 			return (error);
   1018 
   1019 		vaddr += sgsize;
   1020 		buflen -= sgsize;
   1021 	}
   1022 
   1023 	return (0);
   1024 }
   1025 
   1026 /*
   1027  * Allocate physical memory from the given physical address range.
   1028  * Called by DMA-safe memory allocation methods.
   1029  */
   1030 int
   1031 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1032     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1033     int flags, paddr_t low, paddr_t high)
   1034 {
   1035 	paddr_t curaddr, lastaddr;
   1036 	struct vm_page *m;
   1037 	struct pglist mlist;
   1038 	int curseg, error;
   1039 
   1040 #ifdef DEBUG_DMA
   1041 	printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
   1042 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
   1043 #endif	/* DEBUG_DMA */
   1044 
   1045 	/* Always round the size. */
   1046 	size = round_page(size);
   1047 
   1048 	/*
   1049 	 * Allocate pages from the VM system.
   1050 	 */
   1051 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
   1052 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
   1053 	if (error)
   1054 		return (error);
   1055 
   1056 	/*
   1057 	 * Compute the location, size, and number of segments actually
   1058 	 * returned by the VM code.
   1059 	 */
   1060 	m = TAILQ_FIRST(&mlist);
   1061 	curseg = 0;
   1062 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
   1063 	segs[curseg].ds_len = PAGE_SIZE;
   1064 #ifdef DEBUG_DMA
   1065 		printf("alloc: page %lx\n", lastaddr);
   1066 #endif	/* DEBUG_DMA */
   1067 	m = TAILQ_NEXT(m, pageq);
   1068 
   1069 	for (; m != NULL; m = TAILQ_NEXT(m, pageq)) {
   1070 		curaddr = VM_PAGE_TO_PHYS(m);
   1071 #ifdef DIAGNOSTIC
   1072 		if (curaddr < low || curaddr >= high) {
   1073 			printf("uvm_pglistalloc returned non-sensical"
   1074 			    " address 0x%lx\n", curaddr);
   1075 			panic("_bus_dmamem_alloc_range");
   1076 		}
   1077 #endif	/* DIAGNOSTIC */
   1078 #ifdef DEBUG_DMA
   1079 		printf("alloc: page %lx\n", curaddr);
   1080 #endif	/* DEBUG_DMA */
   1081 		if (curaddr == (lastaddr + PAGE_SIZE))
   1082 			segs[curseg].ds_len += PAGE_SIZE;
   1083 		else {
   1084 			curseg++;
   1085 			segs[curseg].ds_addr = curaddr;
   1086 			segs[curseg].ds_len = PAGE_SIZE;
   1087 		}
   1088 		lastaddr = curaddr;
   1089 	}
   1090 
   1091 	*rsegs = curseg + 1;
   1092 
   1093 	return (0);
   1094 }
   1095 
   1096 /*
   1097  * Check if a memory region intersects with a DMA range, and return the
   1098  * page-rounded intersection if it does.
   1099  */
   1100 int
   1101 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
   1102     paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
   1103 {
   1104 	struct arm32_dma_range *dr;
   1105 	int i;
   1106 
   1107 	if (ranges == NULL)
   1108 		return (0);
   1109 
   1110 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
   1111 		if (dr->dr_sysbase <= pa &&
   1112 		    pa < (dr->dr_sysbase + dr->dr_len)) {
   1113 			/*
   1114 			 * Beginning of region intersects with this range.
   1115 			 */
   1116 			*pap = trunc_page(pa);
   1117 			*sizep = round_page(min(pa + size,
   1118 			    dr->dr_sysbase + dr->dr_len) - pa);
   1119 			return (1);
   1120 		}
   1121 		if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
   1122 			/*
   1123 			 * End of region intersects with this range.
   1124 			 */
   1125 			*pap = trunc_page(dr->dr_sysbase);
   1126 			*sizep = round_page(min((pa + size) - dr->dr_sysbase,
   1127 			    dr->dr_len));
   1128 			return (1);
   1129 		}
   1130 	}
   1131 
   1132 	/* No intersection found. */
   1133 	return (0);
   1134 }
   1135