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