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