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