Home | History | Annotate | Line # | Download | only in common
bus_dma.c revision 1.58
      1 /* $NetBSD: bus_dma.c,v 1.58 2005/04/01 11:59:22 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 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/cdefs.h>			/* RCS ID & Copyright macro defns */
     41 
     42 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.58 2005/04/01 11:59:22 yamt Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/malloc.h>
     49 #include <sys/proc.h>
     50 #include <sys/mbuf.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #define _ALPHA_BUS_DMA_PRIVATE
     55 #include <machine/bus.h>
     56 #include <machine/intr.h>
     57 
     58 int	_bus_dmamap_load_buffer_direct(bus_dma_tag_t,
     59 	    bus_dmamap_t, void *, bus_size_t, struct proc *, int,
     60 	    paddr_t *, int *, int);
     61 
     62 extern paddr_t avail_start, avail_end;	/* from pmap.c */
     63 
     64 /*
     65  * Common function for DMA map creation.  May be called by bus-specific
     66  * DMA map creation functions.
     67  */
     68 int
     69 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
     70     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
     71 {
     72 	struct alpha_bus_dmamap *map;
     73 	void *mapstore;
     74 	size_t mapsize;
     75 
     76 	/*
     77 	 * Allocate and initialize the DMA map.  The end of the map
     78 	 * is a variable-sized array of segments, so we allocate enough
     79 	 * room for them in one shot.
     80 	 *
     81 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
     82 	 * of ALLOCNOW notifies others that we've reserved these resources,
     83 	 * and they are not to be freed.
     84 	 *
     85 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
     86 	 * the (nsegments - 1).
     87 	 */
     88 	mapsize = sizeof(struct alpha_bus_dmamap) +
     89 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
     90 	if ((mapstore = malloc(mapsize, M_DMAMAP,
     91 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
     92 		return (ENOMEM);
     93 
     94 	memset(mapstore, 0, mapsize);
     95 	map = (struct alpha_bus_dmamap *)mapstore;
     96 	map->_dm_size = size;
     97 	map->_dm_segcnt = nsegments;
     98 	map->_dm_maxmaxsegsz = maxsegsz;
     99 	if (t->_boundary != 0 && t->_boundary < boundary)
    100 		map->_dm_boundary = t->_boundary;
    101 	else
    102 		map->_dm_boundary = boundary;
    103 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    104 	map->dm_maxsegsz = maxsegsz;
    105 	map->dm_mapsize = 0;		/* no valid mappings */
    106 	map->dm_nsegs = 0;
    107 	map->_dm_window = NULL;
    108 
    109 	*dmamp = map;
    110 	return (0);
    111 }
    112 
    113 /*
    114  * Common function for DMA map destruction.  May be called by bus-specific
    115  * DMA map destruction functions.
    116  */
    117 void
    118 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    119 {
    120 
    121 	free(map, M_DMAMAP);
    122 }
    123 
    124 /*
    125  * Utility function to load a linear buffer.  lastaddrp holds state
    126  * between invocations (for multiple-buffer loads).  segp contains
    127  * the starting segment on entrance, and the ending segment on exit.
    128  * first indicates if this is the first invocation of this function.
    129  */
    130 int
    131 _bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map,
    132     void *buf, size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
    133     int *segp, int first)
    134 {
    135 	bus_size_t sgsize;
    136 	bus_addr_t curaddr, lastaddr, baddr, bmask;
    137 	vaddr_t vaddr = (vaddr_t)buf;
    138 	int seg;
    139 
    140 	lastaddr = *lastaddrp;
    141 	bmask = ~(map->_dm_boundary - 1);
    142 
    143 	for (seg = *segp; buflen > 0 ; ) {
    144 		/*
    145 		 * Get the physical address for this segment.
    146 		 */
    147 		if (p != NULL)
    148 			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
    149 			    vaddr, &curaddr);
    150 		else
    151 			curaddr = vtophys(vaddr);
    152 
    153 		/*
    154 		 * If we're beyond the current DMA window, indicate
    155 		 * that and try to fall back into SGMAPs.
    156 		 */
    157 		if (t->_wsize != 0 && curaddr >= t->_wsize)
    158 			return (EINVAL);
    159 
    160 		curaddr |= t->_wbase;
    161 
    162 		/*
    163 		 * Compute the segment size, and adjust counts.
    164 		 */
    165 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
    166 		if (buflen < sgsize)
    167 			sgsize = buflen;
    168 		if (map->dm_maxsegsz < sgsize)
    169 			sgsize = map->dm_maxsegsz;
    170 
    171 		/*
    172 		 * Make sure we don't cross any boundaries.
    173 		 */
    174 		if (map->_dm_boundary > 0) {
    175 			baddr = (curaddr + map->_dm_boundary) & bmask;
    176 			if (sgsize > (baddr - curaddr))
    177 				sgsize = (baddr - curaddr);
    178 		}
    179 
    180 		/*
    181 		 * Insert chunk into a segment, coalescing with
    182 		 * the previous segment if possible.
    183 		 */
    184 		if (first) {
    185 			map->dm_segs[seg].ds_addr = curaddr;
    186 			map->dm_segs[seg].ds_len = sgsize;
    187 			first = 0;
    188 		} else {
    189 			if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 &&
    190 			    curaddr == lastaddr &&
    191 			    (map->dm_segs[seg].ds_len + sgsize) <=
    192 			     map->dm_maxsegsz &&
    193 			    (map->_dm_boundary == 0 ||
    194 			     (map->dm_segs[seg].ds_addr & bmask) ==
    195 			     (curaddr & bmask)))
    196 				map->dm_segs[seg].ds_len += sgsize;
    197 			else {
    198 				if (++seg >= map->_dm_segcnt)
    199 					break;
    200 				map->dm_segs[seg].ds_addr = curaddr;
    201 				map->dm_segs[seg].ds_len = sgsize;
    202 			}
    203 		}
    204 
    205 		lastaddr = curaddr + sgsize;
    206 		vaddr += sgsize;
    207 		buflen -= sgsize;
    208 	}
    209 
    210 	*segp = seg;
    211 	*lastaddrp = lastaddr;
    212 
    213 	/*
    214 	 * Did we fit?
    215 	 */
    216 	if (buflen != 0) {
    217 		/*
    218 		 * If there is a chained window, we will automatically
    219 		 * fall back to it.
    220 		 */
    221 		return (EFBIG);		/* XXX better return value here? */
    222 	}
    223 
    224 	return (0);
    225 }
    226 
    227 /*
    228  * Common function for loading a direct-mapped DMA map with a linear
    229  * buffer.  Called by bus-specific DMA map load functions with the
    230  * OR value appropriate for indicating "direct-mapped" for that
    231  * chipset.
    232  */
    233 int
    234 _bus_dmamap_load_direct(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    235     bus_size_t buflen, struct proc *p, int flags)
    236 {
    237 	paddr_t lastaddr;
    238 	int seg, error;
    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 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    246 
    247 	if (buflen > map->_dm_size)
    248 		return (EINVAL);
    249 
    250 	seg = 0;
    251 	error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen,
    252 	    p, flags, &lastaddr, &seg, 1);
    253 	if (error == 0) {
    254 		map->dm_mapsize = buflen;
    255 		map->dm_nsegs = seg + 1;
    256 		map->_dm_window = t;
    257 	} else if (t->_next_window != NULL) {
    258 		/*
    259 		 * Give the next window a chance.
    260 		 */
    261 		error = bus_dmamap_load(t->_next_window, map, buf, buflen,
    262 		    p, flags);
    263 	}
    264 	return (error);
    265 }
    266 
    267 /*
    268  * Like _bus_dmamap_load_direct(), but for mbufs.
    269  */
    270 int
    271 _bus_dmamap_load_mbuf_direct(bus_dma_tag_t t, bus_dmamap_t map,
    272     struct mbuf *m0, int flags)
    273 {
    274 	paddr_t lastaddr;
    275 	int seg, error, first;
    276 	struct mbuf *m;
    277 
    278 	/*
    279 	 * Make sure that on error condition we return "no valid mappings."
    280 	 */
    281 	map->dm_mapsize = 0;
    282 	map->dm_nsegs = 0;
    283 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    284 
    285 #ifdef DIAGNOSTIC
    286 	if ((m0->m_flags & M_PKTHDR) == 0)
    287 		panic("_bus_dmamap_load_mbuf_direct: no packet header");
    288 #endif
    289 
    290 	if (m0->m_pkthdr.len > map->_dm_size)
    291 		return (EINVAL);
    292 
    293 	first = 1;
    294 	seg = 0;
    295 	error = 0;
    296 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    297 		if (m->m_len == 0)
    298 			continue;
    299 		/* XXX Could be better about coalescing. */
    300 		/* XXX Doesn't check boundaries. */
    301 		switch (m->m_flags & (M_EXT|M_EXT_CLUSTER)) {
    302 		case M_EXT|M_EXT_CLUSTER:
    303 			/* XXX KDASSERT */
    304 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
    305 			lastaddr = m->m_ext.ext_paddr +
    306 			    (m->m_data - m->m_ext.ext_buf);
    307  have_addr:
    308 			if (first == 0 &&
    309 			    ++seg >= map->_dm_segcnt) {
    310 				error = EFBIG;
    311 				break;
    312 			}
    313 
    314 			/*
    315 			 * If we're beyond the current DMA window, indicate
    316 			 * that and try to fall back into SGMAPs.
    317 			 */
    318 			if (t->_wsize != 0 && lastaddr >= t->_wsize) {
    319 				error = EINVAL;
    320 				break;
    321 			}
    322 			lastaddr |= t->_wbase;
    323 
    324 			map->dm_segs[seg].ds_addr = lastaddr;
    325 			map->dm_segs[seg].ds_len = m->m_len;
    326 			lastaddr += m->m_len;
    327 			break;
    328 
    329 		case 0:
    330 			lastaddr = m->m_paddr + M_BUFOFFSET(m) +
    331 			    (m->m_data - M_BUFADDR(m));
    332 			goto have_addr;
    333 
    334 		default:
    335 			error = _bus_dmamap_load_buffer_direct(t, map,
    336 			    m->m_data, m->m_len, NULL, flags, &lastaddr,
    337 			    &seg, first);
    338 		}
    339 		first = 0;
    340 	}
    341 	if (error == 0) {
    342 		map->dm_mapsize = m0->m_pkthdr.len;
    343 		map->dm_nsegs = seg + 1;
    344 		map->_dm_window = t;
    345 	} else if (t->_next_window != NULL) {
    346 		/*
    347 		 * Give the next window a chance.
    348 		 */
    349 		error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags);
    350 	}
    351 	return (error);
    352 }
    353 
    354 /*
    355  * Like _bus_dmamap_load_direct(), but for uios.
    356  */
    357 int
    358 _bus_dmamap_load_uio_direct(bus_dma_tag_t t, bus_dmamap_t map,
    359     struct uio *uio, int flags)
    360 {
    361 	paddr_t lastaddr;
    362 	int seg, i, error, first;
    363 	bus_size_t minlen, resid;
    364 	struct proc *p = NULL;
    365 	struct iovec *iov;
    366 	caddr_t addr;
    367 
    368 	/*
    369 	 * Make sure that on error condition we return "no valid mappings."
    370 	 */
    371 	map->dm_mapsize = 0;
    372 	map->dm_nsegs = 0;
    373 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    374 
    375 	resid = uio->uio_resid;
    376 	iov = uio->uio_iov;
    377 
    378 	if (uio->uio_segflg == UIO_USERSPACE) {
    379 		p = uio->uio_procp;
    380 #ifdef DIAGNOSTIC
    381 		if (p == NULL)
    382 			panic("_bus_dmamap_load_uio_direct: "
    383 			    "USERSPACE but no proc");
    384 #endif
    385 	}
    386 
    387 	first = 1;
    388 	seg = 0;
    389 	error = 0;
    390 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    391 		/*
    392 		 * Now at the first iovec to load.  Load each iovec
    393 		 * until we have exhausted the residual count.
    394 		 */
    395 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    396 		addr = (caddr_t)iov[i].iov_base;
    397 
    398 		error = _bus_dmamap_load_buffer_direct(t, map,
    399 		    addr, minlen, p, flags, &lastaddr, &seg, first);
    400 		first = 0;
    401 
    402 		resid -= minlen;
    403 	}
    404 	if (error == 0) {
    405 		map->dm_mapsize = uio->uio_resid;
    406 		map->dm_nsegs = seg + 1;
    407 		map->_dm_window = t;
    408 	} else if (t->_next_window != NULL) {
    409 		/*
    410 		 * Give the next window a chance.
    411 		 */
    412 		error = bus_dmamap_load_uio(t->_next_window, map, uio, flags);
    413 	}
    414 	return (error);
    415 }
    416 
    417 /*
    418  * Like _bus_dmamap_load_direct(), but for raw memory.
    419  */
    420 int
    421 _bus_dmamap_load_raw_direct(bus_dma_tag_t t, bus_dmamap_t map,
    422     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    423 {
    424 
    425 	panic("_bus_dmamap_load_raw_direct: not implemented");
    426 }
    427 
    428 /*
    429  * Common function for unloading a DMA map.  May be called by
    430  * chipset-specific DMA map unload functions.
    431  */
    432 void
    433 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    434 {
    435 
    436 	/*
    437 	 * No resources to free; just mark the mappings as
    438 	 * invalid.
    439 	 */
    440 	map->dm_maxsegsz = map->_dm_maxmaxsegsz;
    441 	map->dm_mapsize = 0;
    442 	map->dm_nsegs = 0;
    443 	map->_dm_window = NULL;
    444 }
    445 
    446 /*
    447  * Common function for DMA map synchronization.  May be called
    448  * by chipset-specific DMA map synchronization functions.
    449  */
    450 void
    451 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    452     bus_size_t len, int ops)
    453 {
    454 
    455 	/*
    456 	 * Flush the store buffer.
    457 	 */
    458 	alpha_mb();
    459 }
    460 
    461 /*
    462  * Common function for DMA-safe memory allocation.  May be called
    463  * by bus-specific DMA memory allocation functions.
    464  */
    465 int
    466 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    467     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    468     int flags)
    469 {
    470 
    471 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    472 	    segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
    473 }
    474 
    475 /*
    476  * Allocate physical memory from the given physical address range.
    477  * Called by DMA-safe memory allocation methods.
    478  */
    479 int
    480 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    481     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    482     int flags, paddr_t low, paddr_t high)
    483 {
    484 	paddr_t curaddr, lastaddr;
    485 	struct vm_page *m;
    486 	struct pglist mlist;
    487 	int curseg, error;
    488 
    489 	/* Always round the size. */
    490 	size = round_page(size);
    491 
    492 	/*
    493 	 * Allocate pages from the VM system.
    494 	 */
    495 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
    496 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    497 	if (error)
    498 		return (error);
    499 
    500 	/*
    501 	 * Compute the location, size, and number of segments actually
    502 	 * returned by the VM code.
    503 	 */
    504 	m = mlist.tqh_first;
    505 	curseg = 0;
    506 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    507 	segs[curseg].ds_len = PAGE_SIZE;
    508 	m = m->pageq.tqe_next;
    509 
    510 	for (; m != NULL; m = m->pageq.tqe_next) {
    511 		curaddr = VM_PAGE_TO_PHYS(m);
    512 #ifdef DIAGNOSTIC
    513 		if (curaddr < avail_start || curaddr >= high) {
    514 			printf("uvm_pglistalloc returned non-sensical"
    515 			    " address 0x%lx\n", curaddr);
    516 			panic("_bus_dmamem_alloc");
    517 		}
    518 #endif
    519 		if (curaddr == (lastaddr + PAGE_SIZE))
    520 			segs[curseg].ds_len += PAGE_SIZE;
    521 		else {
    522 			curseg++;
    523 			segs[curseg].ds_addr = curaddr;
    524 			segs[curseg].ds_len = PAGE_SIZE;
    525 		}
    526 		lastaddr = curaddr;
    527 	}
    528 
    529 	*rsegs = curseg + 1;
    530 
    531 	return (0);
    532 }
    533 
    534 /*
    535  * Common function for freeing DMA-safe memory.  May be called by
    536  * bus-specific DMA memory free functions.
    537  */
    538 void
    539 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    540 {
    541 	struct vm_page *m;
    542 	bus_addr_t addr;
    543 	struct pglist mlist;
    544 	int curseg;
    545 
    546 	/*
    547 	 * Build a list of pages to free back to the VM system.
    548 	 */
    549 	TAILQ_INIT(&mlist);
    550 	for (curseg = 0; curseg < nsegs; curseg++) {
    551 		for (addr = segs[curseg].ds_addr;
    552 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    553 		    addr += PAGE_SIZE) {
    554 			m = PHYS_TO_VM_PAGE(addr);
    555 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    556 		}
    557 	}
    558 
    559 	uvm_pglistfree(&mlist);
    560 }
    561 
    562 /*
    563  * Common function for mapping DMA-safe memory.  May be called by
    564  * bus-specific DMA memory map functions.
    565  */
    566 int
    567 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    568     size_t size, caddr_t *kvap, int flags)
    569 {
    570 	vaddr_t va;
    571 	bus_addr_t addr;
    572 	int curseg;
    573 
    574 	/*
    575 	 * If we're only mapping 1 segment, use K0SEG, to avoid
    576 	 * TLB thrashing.
    577 	 */
    578 	if (nsegs == 1) {
    579 		*kvap = (caddr_t)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
    580 		return (0);
    581 	}
    582 
    583 	size = round_page(size);
    584 
    585 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
    586 
    587 	if (va == 0)
    588 		return (ENOMEM);
    589 
    590 	*kvap = (caddr_t)va;
    591 
    592 	for (curseg = 0; curseg < nsegs; curseg++) {
    593 		for (addr = segs[curseg].ds_addr;
    594 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    595 		    addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    596 			if (size == 0)
    597 				panic("_bus_dmamem_map: size botch");
    598 			pmap_enter(pmap_kernel(), va, addr,
    599 			    VM_PROT_READ | VM_PROT_WRITE,
    600 			    PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    601 		}
    602 	}
    603 	pmap_update(pmap_kernel());
    604 
    605 	return (0);
    606 }
    607 
    608 /*
    609  * Common function for unmapping DMA-safe memory.  May be called by
    610  * bus-specific DMA memory unmapping functions.
    611  */
    612 void
    613 _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    614 {
    615 
    616 #ifdef DIAGNOSTIC
    617 	if ((u_long)kva & PGOFSET)
    618 		panic("_bus_dmamem_unmap");
    619 #endif
    620 
    621 	/*
    622 	 * Nothing to do if we mapped it with K0SEG.
    623 	 */
    624 	if (kva >= (caddr_t)ALPHA_K0SEG_BASE &&
    625 	    kva <= (caddr_t)ALPHA_K0SEG_END)
    626 		return;
    627 
    628 	size = round_page(size);
    629 	pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
    630 	pmap_update(pmap_kernel());
    631 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
    632 }
    633 
    634 /*
    635  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    636  * bus-specific DMA mmap(2)'ing functions.
    637  */
    638 paddr_t
    639 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    640     off_t off, int prot, int flags)
    641 {
    642 	int i;
    643 
    644 	for (i = 0; i < nsegs; i++) {
    645 #ifdef DIAGNOSTIC
    646 		if (off & PGOFSET)
    647 			panic("_bus_dmamem_mmap: offset unaligned");
    648 		if (segs[i].ds_addr & PGOFSET)
    649 			panic("_bus_dmamem_mmap: segment unaligned");
    650 		if (segs[i].ds_len & PGOFSET)
    651 			panic("_bus_dmamem_mmap: segment size not multiple"
    652 			    " of page size");
    653 #endif
    654 		if (off >= segs[i].ds_len) {
    655 			off -= segs[i].ds_len;
    656 			continue;
    657 		}
    658 
    659 		return (alpha_btop((caddr_t)segs[i].ds_addr + off));
    660 	}
    661 
    662 	/* Page not found. */
    663 	return (-1);
    664 }
    665