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