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