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