Home | History | Annotate | Line # | Download | only in isa
isadma_bounce.c revision 1.1
      1 /* $NetBSD: isadma_bounce.c,v 1.1 2000/06/01 03:55:45 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 2000 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/syslog.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/proc.h>
     46 #include <sys/mbuf.h>
     47 
     48 #define _ALPHA_BUS_DMA_PRIVATE
     49 #include <machine/bus.h>
     50 
     51 #include <dev/isa/isareg.h>
     52 #include <dev/isa/isavar.h>
     53 
     54 #include <vm/vm.h>
     55 
     56 extern	paddr_t avail_end;
     57 
     58 /*
     59  * ISA can only DMA to 0-16M.
     60  */
     61 #define	ISA_DMA_BOUNCE_THRESHOLD	(16 * 1024 * 1024)
     62 
     63 /*
     64  * Cookie used by bouncing ISA DMA.  A pointer to one of these is stashed
     65  * in the DMA map.
     66  */
     67 struct isadma_bounce_cookie {
     68 	int	id_flags;		/* flags; see below */
     69 
     70 	/*
     71 	 * Information about the original buffer used during
     72 	 * DMA map syncs.  Note that origbuflen is only used
     73 	 * for ID_BUFTYPE_LINEAR.
     74 	 */
     75 	void	*id_origbuf;		/* pointer to orig buffer if
     76 					   bouncing */
     77 	bus_size_t id_origbuflen;	/* ...and size */
     78 	int	id_buftype;		/* type of buffer */
     79 
     80 	void	*id_bouncebuf;		/* pointer to the bounce buffer */
     81 	bus_size_t id_bouncebuflen;	/* ...and size */
     82 	int	id_nbouncesegs;		/* number of valid bounce segs */
     83 	bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer
     84 					       physical memory segments */
     85 };
     86 
     87 /* id_flags */
     88 #define	ID_MIGHT_NEED_BOUNCE	0x01	/* map could need bounce buffers */
     89 #define	ID_HAS_BOUNCE		0x02	/* map currently has bounce buffers */
     90 #define	ID_IS_BOUNCING		0x04	/* map is bouncing current xfer */
     91 
     92 /* id_buftype */
     93 #define	ID_BUFTYPE_INVALID	0
     94 #define	ID_BUFTYPE_LINEAR	1
     95 #define	ID_BUFTYPE_MBUF		2
     96 #define	ID_BUFTYPE_UIO		3
     97 #define	ID_BUFTYPE_RAW		4
     98 
     99 int	isadma_bounce_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
    100 	    bus_size_t, int));
    101 void	isadma_bounce_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
    102 
    103 /*
    104  * Create an ISA DMA map.
    105  */
    106 int
    107 isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    108     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    109 {
    110 	struct isadma_bounce_cookie *cookie;
    111 	bus_dmamap_t map;
    112 	int error, cookieflags;
    113 	void *cookiestore;
    114 	size_t cookiesize;
    115 
    116 	/* Call common function to create the basic map. */
    117 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    118 	    flags, dmamp);
    119 	if (error)
    120 		return (error);
    121 
    122 	map = *dmamp;
    123 	map->_dm_cookie = NULL;
    124 
    125 	cookiesize = sizeof(*cookie);
    126 
    127 	/*
    128 	 * ISA only has 24-bits of address space.  This means
    129 	 * we can't DMA to pages over 16M.  In order to DMA to
    130 	 * arbitrary buffers, we use "bounce buffers" - pages
    131 	 * in memory below the 16M boundary.  On DMA reads,
    132 	 * DMA happens to the bounce buffers, and is copied into
    133 	 * the caller's buffer.  On writes, data is copied into
    134 	 * but bounce buffer, and the DMA happens from those
    135 	 * pages.  To software using the DMA mapping interface,
    136 	 * this looks simply like a data cache.
    137 	 *
    138 	 * If we have more than 16M of RAM in the system, we may
    139 	 * need bounce buffers.  We check and remember that here.
    140 	 *
    141 	 * ...or, there is an opposite case.  The most segments
    142 	 * a transfer will require is (maxxfer / PAGE_SIZE) + 1.  If
    143 	 * the caller can't handle that many segments (e.g. the
    144 	 * ISA DMA controller), we may have to bounce it as well.
    145 	 */
    146 	cookieflags = 0;
    147 	if (avail_end > (t->_wbase + t->_wsize) ||
    148 	    ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
    149 		cookieflags |= ID_MIGHT_NEED_BOUNCE;
    150 		cookiesize += (sizeof(bus_dma_segment_t) *
    151 		    (map->_dm_segcnt - 1));
    152 	}
    153 
    154 	/*
    155 	 * Allocate our cookie.
    156 	 */
    157 	if ((cookiestore = malloc(cookiesize, M_DMAMAP,
    158 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
    159 		error = ENOMEM;
    160 		goto out;
    161 	}
    162 	memset(cookiestore, 0, cookiesize);
    163 	cookie = (struct isadma_bounce_cookie *)cookiestore;
    164 	cookie->id_flags = cookieflags;
    165 	map->_dm_cookie = cookie;
    166 
    167 	if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
    168 		/*
    169 		 * Allocate the bounce pages now if the caller
    170 		 * wishes us to do so.
    171 		 */
    172 		if ((flags & BUS_DMA_ALLOCNOW) == 0)
    173 			goto out;
    174 
    175 		error = isadma_bounce_alloc_bouncebuf(t, map, size, flags);
    176 	}
    177 
    178  out:
    179 	if (error) {
    180 		if (map->_dm_cookie != NULL)
    181 			free(map->_dm_cookie, M_DMAMAP);
    182 		_bus_dmamap_destroy(t, map);
    183 	}
    184 	return (error);
    185 }
    186 
    187 /*
    188  * Destroy an ISA DMA map.
    189  */
    190 void
    191 isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    192 {
    193 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    194 
    195 	/*
    196 	 * Free any bounce pages this map might hold.
    197 	 */
    198 	if (cookie->id_flags & ID_HAS_BOUNCE)
    199 		isadma_bounce_free_bouncebuf(t, map);
    200 
    201 	free(cookie, M_DMAMAP);
    202 	_bus_dmamap_destroy(t, map);
    203 }
    204 
    205 /*
    206  * Load an ISA DMA map with a linear buffer.
    207  */
    208 int
    209 isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    210     size_t buflen, struct proc *p, int flags)
    211 {
    212 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    213 	int error;
    214 
    215 	/*
    216 	 * Make sure that on error condition we return "no valid mappings."
    217 	 */
    218 	map->dm_mapsize = 0;
    219 	map->dm_nsegs = 0;
    220 
    221 	/*
    222 	 * Try to load the map the normal way.  If this errors out,
    223 	 * and we can bounce, we will.
    224 	 */
    225 	error = _bus_dmamap_load_direct(t, map, buf, buflen, p, flags);
    226 	if (error == 0 ||
    227 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    228 		return (error);
    229 
    230 	/*
    231 	 * First attempt failed; bounce it.
    232 	 */
    233 
    234 	/*
    235 	 * Allocate bounce pages, if necessary.
    236 	 */
    237 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    238 		error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags);
    239 		if (error)
    240 			return (error);
    241 	}
    242 
    243 	/*
    244 	 * Cache a pointer to the caller's buffer and load the DMA map
    245 	 * with the bounce buffer.
    246 	 */
    247 	cookie->id_origbuf = buf;
    248 	cookie->id_origbuflen = buflen;
    249 	cookie->id_buftype = ID_BUFTYPE_LINEAR;
    250 	error = _bus_dmamap_load_direct(t, map, cookie->id_bouncebuf, buflen,
    251 	    p, flags);
    252 	if (error) {
    253 		/*
    254 		 * Free the bounce pages, unless our resources
    255 		 * are reserved for our exclusive use.
    256 		 */
    257 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    258 			isadma_bounce_free_bouncebuf(t, map);
    259 		return (error);
    260 	}
    261 
    262 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    263 	cookie->id_flags |= ID_IS_BOUNCING;
    264 	return (0);
    265 }
    266 
    267 /*
    268  * Like isadma_bounce_dmamap_load(), but for mbufs.
    269  */
    270 int
    271 isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map,
    272     struct mbuf *m0, int flags)
    273 {
    274 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    275 	int error;
    276 
    277 	/*
    278 	 * Make sure on error condition we return "no valid mappings."
    279 	 */
    280 	map->dm_mapsize = 0;
    281 	map->dm_nsegs = 0;
    282 
    283 #ifdef DIAGNOSTIC
    284 	if ((m0->m_flags & M_PKTHDR) == 0)
    285 		panic("isadma_bounce_dmamap_load_mbuf: no packet header");
    286 #endif
    287 
    288 	if (m0->m_pkthdr.len > map->_dm_size)
    289 		return (EINVAL);
    290 
    291 	/*
    292 	 * Try to load the map the normal way.  If this errors out,
    293 	 * and we can bounce, we will.
    294 	 */
    295 	error = _bus_dmamap_load_mbuf_direct(t, map, m0, flags);
    296 	if (error == 0 ||
    297 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    298 		return (error);
    299 
    300 	/*
    301 	 * First attempt failed; bounce it.
    302 	 */
    303 
    304 	/*
    305 	 * Allocate bounce pages, if necessary.
    306 	 */
    307 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    308 		error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
    309 		    flags);
    310 		if (error)
    311 			return (error);
    312 	}
    313 
    314 	/*
    315 	 * Cache a pointer to the caller's buffer and load the DMA map
    316 	 * with the bounce buffer.
    317 	 */
    318 	cookie->id_origbuf = m0;
    319 	cookie->id_origbuflen = m0->m_pkthdr.len;	/* not really used */
    320 	cookie->id_buftype = ID_BUFTYPE_MBUF;
    321 	error = _bus_dmamap_load_direct(t, map, cookie->id_bouncebuf,
    322 	    m0->m_pkthdr.len, NULL, flags);
    323 	if (error) {
    324 		/*
    325 		 * Free the bounce pages, unless our resources
    326 		 * are reserved for our exclusive use.
    327 		 */
    328 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    329 			isadma_bounce_free_bouncebuf(t, map);
    330 		return (error);
    331 	}
    332 
    333 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    334 	cookie->id_flags |= ID_IS_BOUNCING;
    335 	return (0);
    336 }
    337 
    338 /*
    339  * Like isadma_bounce_dmamap_load(), but for uios.
    340  */
    341 int
    342 isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map,
    343     struct uio *uio, int flags)
    344 {
    345 
    346 	panic("isadma_bounce_dmamap_load_uio: not implemented");
    347 }
    348 
    349 /*
    350  * Like isadma_bounce_dmamap_load(), but for raw memory allocated with
    351  * bus_dmamem_alloc().
    352  */
    353 int
    354 isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    355     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    356 {
    357 
    358 	panic("isadma_bounce_dmamap_load_raw: not implemented");
    359 }
    360 
    361 /*
    362  * Unload an ISA DMA map.
    363  */
    364 void
    365 isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    366 {
    367 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    368 
    369 	/*
    370 	 * If we have bounce pages, free them, unless they're
    371 	 * reserved for our exclusive use.
    372 	 */
    373 	if ((cookie->id_flags & ID_HAS_BOUNCE) &&
    374 	    (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    375 		isadma_bounce_free_bouncebuf(t, map);
    376 
    377 	cookie->id_flags &= ~ID_IS_BOUNCING;
    378 	cookie->id_buftype = ID_BUFTYPE_INVALID;
    379 
    380 	/*
    381 	 * Do the generic bits of the unload.
    382 	 */
    383 	_bus_dmamap_unload(t, map);
    384 }
    385 
    386 /*
    387  * Synchronize an ISA DMA map.
    388  */
    389 void
    390 isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    391     bus_size_t len, int ops)
    392 {
    393 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    394 
    395 	/*
    396 	 * Mixing PRE and POST operations is not allowed.
    397 	 */
    398 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    399 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    400 		panic("isadma_bounce_dmamap_sync: mix PRE and POST");
    401 
    402 #ifdef DIAGNOSTIC
    403 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    404 		if (offset >= map->dm_mapsize)
    405 			panic("isadma_bounce_dmamap_sync: bad offset");
    406 		if (len == 0 || (offset + len) > map->dm_mapsize)
    407 			panic("isadma_bounce_dmamap_sync: bad length");
    408 	}
    409 #endif
    410 
    411 	/*
    412 	 * If we're not bouncing, just drain the write buffer
    413 	 * and return.
    414 	 */
    415 	if ((cookie->id_flags & ID_IS_BOUNCING) == 0) {
    416 		alpha_mb();
    417 		return;
    418 	}
    419 
    420 	switch (cookie->id_buftype) {
    421 	case ID_BUFTYPE_LINEAR:
    422 		/*
    423 		 * Nothing to do for pre-read.
    424 		 */
    425 
    426 		if (ops & BUS_DMASYNC_PREWRITE) {
    427 			/*
    428 			 * Copy the caller's buffer to the bounce buffer.
    429 			 */
    430 			memcpy((char *)cookie->id_bouncebuf + offset,
    431 			    (char *)cookie->id_origbuf + offset, len);
    432 		}
    433 
    434 		if (ops & BUS_DMASYNC_POSTREAD) {
    435 			/*
    436 			 * Copy the bounce buffer to the caller's buffer.
    437 			 */
    438 			memcpy((char *)cookie->id_origbuf + offset,
    439 			    (char *)cookie->id_bouncebuf + offset, len);
    440 		}
    441 
    442 		/*
    443 		 * Nothing to do for post-write.
    444 		 */
    445 		break;
    446 
    447 	case ID_BUFTYPE_MBUF:
    448 	    {
    449 		struct mbuf *m, *m0 = cookie->id_origbuf;
    450 		bus_size_t minlen, moff;
    451 
    452 		/*
    453 		 * Nothing to do for pre-read.
    454 		 */
    455 
    456 		if (ops & BUS_DMASYNC_PREWRITE) {
    457 			/*
    458 			 * Copy the caller's buffer to the bounce buffer.
    459 			 */
    460 			m_copydata(m0, offset, len,
    461 			    (char *)cookie->id_bouncebuf + offset);
    462 		}
    463 
    464 		if (ops & BUS_DMASYNC_POSTREAD) {
    465 			/*
    466 			 * Copy the bounce buffer to the caller's buffer.
    467 			 */
    468 			for (moff = offset, m = m0; m != NULL && len != 0;
    469 			     m = m->m_next) {
    470 				/* Find the beginning mbuf. */
    471 				if (moff >= m->m_len) {
    472 					moff -= m->m_len;
    473 					continue;
    474 				}
    475 
    476 				/*
    477 				 * Now at the first mbuf to sync; nail
    478 				 * each one until we have exhausted the
    479 				 * length.
    480 				 */
    481 				minlen = len < m->m_len - moff ?
    482 				    len : m->m_len - moff;
    483 
    484 				memcpy(mtod(m, caddr_t) + moff,
    485 				    (char *)cookie->id_bouncebuf + offset,
    486 				    minlen);
    487 
    488 				moff = 0;
    489 				len -= minlen;
    490 				offset += minlen;
    491 			}
    492 		}
    493 
    494 		/*
    495 		 * Nothing to do for post-write.
    496 		 */
    497 		break;
    498 	    }
    499 
    500 	case ID_BUFTYPE_UIO:
    501 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO");
    502 		break;
    503 
    504 	case ID_BUFTYPE_RAW:
    505 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW");
    506 		break;
    507 
    508 	case ID_BUFTYPE_INVALID:
    509 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID");
    510 		break;
    511 
    512 	default:
    513 		printf("unknown buffer type %d\n", cookie->id_buftype);
    514 		panic("isadma_bounce_dmamap_sync");
    515 	}
    516 
    517 	/* Drain the write buffer. */
    518 	alpha_mb();
    519 }
    520 
    521 /*
    522  * Allocate memory safe for ISA DMA.
    523  */
    524 int
    525 isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size,
    526     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
    527     int nsegs, int *rsegs, int flags)
    528 {
    529 	paddr_t high;
    530 
    531 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
    532 		high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
    533 	else
    534 		high = trunc_page(avail_end);
    535 
    536 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    537 	    segs, nsegs, rsegs, flags, 0, high));
    538 }
    539 
    540 /**********************************************************************
    541  * ISA DMA utility functions
    542  **********************************************************************/
    543 
    544 int
    545 isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
    546     bus_size_t size, int flags)
    547 {
    548 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    549 	int error = 0;
    550 
    551 	cookie->id_bouncebuflen = round_page(size);
    552 	error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen,
    553 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
    554 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
    555 	if (error)
    556 		goto out;
    557 	error = _bus_dmamem_map(t, cookie->id_bouncesegs,
    558 	    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
    559 	    (caddr_t *)&cookie->id_bouncebuf, flags);
    560 
    561  out:
    562 	if (error) {
    563 		_bus_dmamem_free(t, cookie->id_bouncesegs,
    564 		    cookie->id_nbouncesegs);
    565 		cookie->id_bouncebuflen = 0;
    566 		cookie->id_nbouncesegs = 0;
    567 	} else
    568 		cookie->id_flags |= ID_HAS_BOUNCE;
    569 
    570 	return (error);
    571 }
    572 
    573 void
    574 isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
    575 {
    576 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    577 
    578 	_bus_dmamem_unmap(t, cookie->id_bouncebuf,
    579 	    cookie->id_bouncebuflen);
    580 	_bus_dmamem_free(t, cookie->id_bouncesegs,
    581 	    cookie->id_nbouncesegs);
    582 	cookie->id_bouncebuflen = 0;
    583 	cookie->id_nbouncesegs = 0;
    584 	cookie->id_flags &= ~ID_HAS_BOUNCE;
    585 }
    586