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