Home | History | Annotate | Line # | Download | only in arm32
bus_dma.c revision 1.41
      1 /*	$NetBSD: bus_dma.c,v 1.41 2004/11/28 17:36:27 thorpej 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.41 2004/11/28 17:36:27 thorpej 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_valloc(kernel_map, size);
    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 	uvm_km_free(kernel_map, (vaddr_t)kva, size);
    898 }
    899 
    900 /*
    901  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    902  * bus-specific DMA mmap(2)'ing functions.
    903  */
    904 paddr_t
    905 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    906     off_t off, int prot, int flags)
    907 {
    908 	int i;
    909 
    910 	for (i = 0; i < nsegs; i++) {
    911 #ifdef DIAGNOSTIC
    912 		if (off & PGOFSET)
    913 			panic("_bus_dmamem_mmap: offset unaligned");
    914 		if (segs[i].ds_addr & PGOFSET)
    915 			panic("_bus_dmamem_mmap: segment unaligned");
    916 		if (segs[i].ds_len & PGOFSET)
    917 			panic("_bus_dmamem_mmap: segment size not multiple"
    918 			    " of page size");
    919 #endif	/* DIAGNOSTIC */
    920 		if (off >= segs[i].ds_len) {
    921 			off -= segs[i].ds_len;
    922 			continue;
    923 		}
    924 
    925 		return (arm_btop((u_long)segs[i].ds_addr + off));
    926 	}
    927 
    928 	/* Page not found. */
    929 	return (-1);
    930 }
    931 
    932 /**********************************************************************
    933  * DMA utility functions
    934  **********************************************************************/
    935 
    936 /*
    937  * Utility function to load a linear buffer.  lastaddrp holds state
    938  * between invocations (for multiple-buffer loads).  segp contains
    939  * the starting segment on entrace, and the ending segment on exit.
    940  * first indicates if this is the first invocation of this function.
    941  */
    942 int
    943 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    944     bus_size_t buflen, struct proc *p, int flags)
    945 {
    946 	bus_size_t sgsize;
    947 	bus_addr_t curaddr;
    948 	vaddr_t vaddr = (vaddr_t)buf;
    949 	pd_entry_t *pde;
    950 	pt_entry_t pte;
    951 	int error;
    952 	pmap_t pmap;
    953 	pt_entry_t *ptep;
    954 
    955 #ifdef DEBUG_DMA
    956 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
    957 	    buf, buflen, flags);
    958 #endif	/* DEBUG_DMA */
    959 
    960 	if (p != NULL)
    961 		pmap = p->p_vmspace->vm_map.pmap;
    962 	else
    963 		pmap = pmap_kernel();
    964 
    965 	while (buflen > 0) {
    966 		/*
    967 		 * Get the physical address for this segment.
    968 		 *
    969 		 * XXX Don't support checking for coherent mappings
    970 		 * XXX in user address space.
    971 		 */
    972 		if (__predict_true(pmap == pmap_kernel())) {
    973 			(void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
    974 			if (__predict_false(pmap_pde_section(pde))) {
    975 				curaddr = (*pde & L1_S_FRAME) |
    976 				    (vaddr & L1_S_OFFSET);
    977 				if (*pde & L1_S_CACHE_MASK) {
    978 					map->_dm_flags &=
    979 					    ~ARM32_DMAMAP_COHERENT;
    980 				}
    981 			} else {
    982 				pte = *ptep;
    983 				KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
    984 				if (__predict_false((pte & L2_TYPE_MASK)
    985 						    == L2_TYPE_L)) {
    986 					curaddr = (pte & L2_L_FRAME) |
    987 					    (vaddr & L2_L_OFFSET);
    988 					if (pte & L2_L_CACHE_MASK) {
    989 						map->_dm_flags &=
    990 						    ~ARM32_DMAMAP_COHERENT;
    991 					}
    992 				} else {
    993 					curaddr = (pte & L2_S_FRAME) |
    994 					    (vaddr & L2_S_OFFSET);
    995 					if (pte & L2_S_CACHE_MASK) {
    996 						map->_dm_flags &=
    997 						    ~ARM32_DMAMAP_COHERENT;
    998 					}
    999 				}
   1000 			}
   1001 		} else {
   1002 			(void) pmap_extract(pmap, vaddr, &curaddr);
   1003 			map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
   1004 		}
   1005 
   1006 		/*
   1007 		 * Compute the segment size, and adjust counts.
   1008 		 */
   1009 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
   1010 		if (buflen < sgsize)
   1011 			sgsize = buflen;
   1012 
   1013 		error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize);
   1014 		if (error)
   1015 			return (error);
   1016 
   1017 		vaddr += sgsize;
   1018 		buflen -= sgsize;
   1019 	}
   1020 
   1021 	return (0);
   1022 }
   1023 
   1024 /*
   1025  * Allocate physical memory from the given physical address range.
   1026  * Called by DMA-safe memory allocation methods.
   1027  */
   1028 int
   1029 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1030     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1031     int flags, paddr_t low, paddr_t high)
   1032 {
   1033 	paddr_t curaddr, lastaddr;
   1034 	struct vm_page *m;
   1035 	struct pglist mlist;
   1036 	int curseg, error;
   1037 
   1038 #ifdef DEBUG_DMA
   1039 	printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
   1040 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
   1041 #endif	/* DEBUG_DMA */
   1042 
   1043 	/* Always round the size. */
   1044 	size = round_page(size);
   1045 
   1046 	/*
   1047 	 * Allocate pages from the VM system.
   1048 	 */
   1049 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
   1050 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
   1051 	if (error)
   1052 		return (error);
   1053 
   1054 	/*
   1055 	 * Compute the location, size, and number of segments actually
   1056 	 * returned by the VM code.
   1057 	 */
   1058 	m = mlist.tqh_first;
   1059 	curseg = 0;
   1060 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
   1061 	segs[curseg].ds_len = PAGE_SIZE;
   1062 #ifdef DEBUG_DMA
   1063 		printf("alloc: page %lx\n", lastaddr);
   1064 #endif	/* DEBUG_DMA */
   1065 	m = m->pageq.tqe_next;
   1066 
   1067 	for (; m != NULL; m = m->pageq.tqe_next) {
   1068 		curaddr = VM_PAGE_TO_PHYS(m);
   1069 #ifdef DIAGNOSTIC
   1070 		if (curaddr < low || curaddr >= high) {
   1071 			printf("uvm_pglistalloc returned non-sensical"
   1072 			    " address 0x%lx\n", curaddr);
   1073 			panic("_bus_dmamem_alloc_range");
   1074 		}
   1075 #endif	/* DIAGNOSTIC */
   1076 #ifdef DEBUG_DMA
   1077 		printf("alloc: page %lx\n", curaddr);
   1078 #endif	/* DEBUG_DMA */
   1079 		if (curaddr == (lastaddr + PAGE_SIZE))
   1080 			segs[curseg].ds_len += PAGE_SIZE;
   1081 		else {
   1082 			curseg++;
   1083 			segs[curseg].ds_addr = curaddr;
   1084 			segs[curseg].ds_len = PAGE_SIZE;
   1085 		}
   1086 		lastaddr = curaddr;
   1087 	}
   1088 
   1089 	*rsegs = curseg + 1;
   1090 
   1091 	return (0);
   1092 }
   1093 
   1094 /*
   1095  * Check if a memory region intersects with a DMA range, and return the
   1096  * page-rounded intersection if it does.
   1097  */
   1098 int
   1099 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
   1100     paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
   1101 {
   1102 	struct arm32_dma_range *dr;
   1103 	int i;
   1104 
   1105 	if (ranges == NULL)
   1106 		return (0);
   1107 
   1108 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
   1109 		if (dr->dr_sysbase <= pa &&
   1110 		    pa < (dr->dr_sysbase + dr->dr_len)) {
   1111 			/*
   1112 			 * Beginning of region intersects with this range.
   1113 			 */
   1114 			*pap = trunc_page(pa);
   1115 			*sizep = round_page(min(pa + size,
   1116 			    dr->dr_sysbase + dr->dr_len) - pa);
   1117 			return (1);
   1118 		}
   1119 		if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
   1120 			/*
   1121 			 * End of region intersects with this range.
   1122 			 */
   1123 			*pap = trunc_page(dr->dr_sysbase);
   1124 			*sizep = round_page(min((pa + size) - dr->dr_sysbase,
   1125 			    dr->dr_len));
   1126 			return (1);
   1127 		}
   1128 	}
   1129 
   1130 	/* No intersection found. */
   1131 	return (0);
   1132 }
   1133