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