Home | History | Annotate | Line # | Download | only in isa
isadma_bounce.c revision 1.2
      1 /* $NetBSD: isadma_bounce.c,v 1.2 2000/06/20 08:26:55 soda Exp $ */
      2 /* NetBSD: isadma_bounce.c,v 1.2 2000/06/01 05:49:36 thorpej Exp  */
      3 
      4 /*-
      5  * Copyright (c) 1996, 1997, 1998, 2000 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  * NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     42 
     43 __KERNEL_RCSID(0, "$NetBSD: isadma_bounce.c,v 1.2 2000/06/20 08:26:55 soda Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/syslog.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <sys/proc.h>
     51 #include <sys/mbuf.h>
     52 
     53 #define _ARC_BUS_DMA_PRIVATE
     54 #include <machine/bus.h>
     55 
     56 #include <dev/isa/isareg.h>
     57 #include <dev/isa/isavar.h>
     58 
     59 #include <vm/vm.h>
     60 
     61 extern paddr_t avail_end;	/* from pmap.c */
     62 
     63 /*
     64  * ISA can only DMA to 0-16M.
     65  */
     66 #define	ISA_DMA_BOUNCE_THRESHOLD	(16 * 1024 * 1024)
     67 
     68 /*
     69  * Cookie used by bouncing ISA DMA.  A pointer to one of these is stashed
     70  * in the DMA map.
     71  */
     72 struct isadma_bounce_cookie {
     73 	int	id_flags;		/* flags; see below */
     74 
     75 	/*
     76 	 * Information about the original buffer used during
     77 	 * DMA map syncs.  Note that origbuflen is only used
     78 	 * for ID_BUFTYPE_LINEAR.
     79 	 */
     80 	void	*id_origbuf;		/* pointer to orig buffer if
     81 					   bouncing */
     82 	bus_size_t id_origbuflen;	/* ...and size */
     83 	int	id_buftype;		/* type of buffer */
     84 
     85 	void	*id_bouncebuf;		/* pointer to the bounce buffer */
     86 	bus_size_t id_bouncebuflen;	/* ...and size */
     87 	int	id_nbouncesegs;		/* number of valid bounce segs */
     88 	bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer
     89 					       physical memory segments */
     90 };
     91 
     92 /* id_flags */
     93 #define	ID_MIGHT_NEED_BOUNCE	0x01	/* map could need bounce buffers */
     94 #define	ID_HAS_BOUNCE		0x02	/* map currently has bounce buffers */
     95 #define	ID_IS_BOUNCING		0x04	/* map is bouncing current xfer */
     96 
     97 /* id_buftype */
     98 #define	ID_BUFTYPE_INVALID	0
     99 #define	ID_BUFTYPE_LINEAR	1
    100 #define	ID_BUFTYPE_MBUF		2
    101 #define	ID_BUFTYPE_UIO		3
    102 #define	ID_BUFTYPE_RAW		4
    103 
    104 int	isadma_bounce_dmamap_create(bus_dma_tag_t, bus_size_t, int,
    105 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
    106 void	isadma_bounce_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
    107 int	isadma_bounce_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
    108 	    bus_size_t, struct proc *, int);
    109 int	isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
    110 	    struct mbuf *, int);
    111 int	isadma_bounce_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
    112 	    struct uio *, int);
    113 int	isadma_bounce_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
    114 	    bus_dma_segment_t *, int, bus_size_t, int);
    115 void	isadma_bounce_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
    116 void	isadma_bounce_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
    117 	    bus_addr_t, bus_size_t, int);
    118 int	isadma_bounce_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
    119 	    bus_size_t, bus_dma_segment_t *, int, int *, int);
    120 
    121 static int isadma_bounce_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
    122 	    bus_size_t, int));
    123 static void isadma_bounce_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
    124 
    125 void
    126 isadma_bounce_tag_init(t)
    127 	bus_dma_tag_t t;
    128 {
    129 	/*
    130 	 * Initialize the DMA tag used for ISA DMA.
    131 	 */
    132 
    133 	_bus_dma_tag_init(t);
    134 
    135 	t->_dmamap_create = isadma_bounce_dmamap_create;
    136 	t->_dmamap_destroy = isadma_bounce_dmamap_destroy;
    137 	t->_dmamap_load = isadma_bounce_dmamap_load;
    138 	t->_dmamap_load_mbuf = isadma_bounce_dmamap_load_mbuf;
    139 	t->_dmamap_load_uio = isadma_bounce_dmamap_load_uio;
    140 	t->_dmamap_load_raw = isadma_bounce_dmamap_load_raw;
    141 	t->_dmamap_unload = isadma_bounce_dmamap_unload;
    142 	t->_dmamap_sync = isadma_bounce_dmamap_sync;
    143 	t->_dmamem_alloc = isadma_bounce_dmamem_alloc;
    144 }
    145 
    146 /*
    147  * Create an ISA DMA map.
    148  */
    149 int
    150 isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    151     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    152 {
    153 	struct isadma_bounce_cookie *cookie;
    154 	bus_dmamap_t map;
    155 	int error, cookieflags;
    156 	void *cookiestore;
    157 	size_t cookiesize;
    158 
    159 	/* Call common function to create the basic map. */
    160 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    161 	    flags, dmamp);
    162 	if (error)
    163 		return (error);
    164 
    165 	map = *dmamp;
    166 	map->_dm_cookie = NULL;
    167 
    168 	cookiesize = sizeof(*cookie);
    169 
    170 	/*
    171 	 * ISA only has 24-bits of address space.  This means
    172 	 * we can't DMA to pages over 16M.  In order to DMA to
    173 	 * arbitrary buffers, we use "bounce buffers" - pages
    174 	 * in memory below the 16M boundary.  On DMA reads,
    175 	 * DMA happens to the bounce buffers, and is copied into
    176 	 * the caller's buffer.  On writes, data is copied into
    177 	 * but bounce buffer, and the DMA happens from those
    178 	 * pages.  To software using the DMA mapping interface,
    179 	 * this looks simply like a data cache.
    180 	 *
    181 	 * If we have more than 16M of RAM in the system, we may
    182 	 * need bounce buffers.  We check and remember that here.
    183 	 *
    184 	 * ...or, there is an opposite case.  The most segments
    185 	 * a transfer will require is (maxxfer / PAGE_SIZE) + 1.  If
    186 	 * the caller can't handle that many segments (e.g. the
    187 	 * ISA DMA controller), we may have to bounce it as well.
    188 	 */
    189 	cookieflags = 0;
    190 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD ||
    191 	    ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
    192 		cookieflags |= ID_MIGHT_NEED_BOUNCE;
    193 		cookiesize += (sizeof(bus_dma_segment_t) *
    194 		    (map->_dm_segcnt - 1));
    195 	}
    196 
    197 	/*
    198 	 * Allocate our cookie.
    199 	 */
    200 	if ((cookiestore = malloc(cookiesize, M_DMAMAP,
    201 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
    202 		error = ENOMEM;
    203 		goto out;
    204 	}
    205 	memset(cookiestore, 0, cookiesize);
    206 	cookie = (struct isadma_bounce_cookie *)cookiestore;
    207 	cookie->id_flags = cookieflags;
    208 	map->_dm_cookie = cookie;
    209 
    210 	if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
    211 		/*
    212 		 * Allocate the bounce pages now if the caller
    213 		 * wishes us to do so.
    214 		 */
    215 		if ((flags & BUS_DMA_ALLOCNOW) == 0)
    216 			goto out;
    217 
    218 		error = isadma_bounce_alloc_bouncebuf(t, map, size, flags);
    219 	}
    220 
    221  out:
    222 	if (error) {
    223 		if (map->_dm_cookie != NULL)
    224 			free(map->_dm_cookie, M_DMAMAP);
    225 		_bus_dmamap_destroy(t, map);
    226 	}
    227 	return (error);
    228 }
    229 
    230 /*
    231  * Destroy an ISA DMA map.
    232  */
    233 void
    234 isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    235 {
    236 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    237 
    238 	/*
    239 	 * Free any bounce pages this map might hold.
    240 	 */
    241 	if (cookie->id_flags & ID_HAS_BOUNCE)
    242 		isadma_bounce_free_bouncebuf(t, map);
    243 
    244 	free(cookie, M_DMAMAP);
    245 	_bus_dmamap_destroy(t, map);
    246 }
    247 
    248 /*
    249  * Load an ISA DMA map with a linear buffer.
    250  */
    251 int
    252 isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    253     bus_size_t buflen, struct proc *p, int flags)
    254 {
    255 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    256 	int error;
    257 
    258 	/*
    259 	 * Make sure that on error condition we return "no valid mappings."
    260 	 */
    261 	map->dm_mapsize = 0;
    262 	map->dm_nsegs = 0;
    263 
    264 	/*
    265 	 * Try to load the map the normal way.  If this errors out,
    266 	 * and we can bounce, we will.
    267 	 */
    268 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    269 	if (error == 0 ||
    270 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    271 		return (error);
    272 
    273 	/*
    274 	 * First attempt failed; bounce it.
    275 	 */
    276 
    277 	/*
    278 	 * Allocate bounce pages, if necessary.
    279 	 */
    280 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    281 		error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags);
    282 		if (error)
    283 			return (error);
    284 	}
    285 
    286 	/*
    287 	 * Cache a pointer to the caller's buffer and load the DMA map
    288 	 * with the bounce buffer.
    289 	 */
    290 	cookie->id_origbuf = buf;
    291 	cookie->id_origbuflen = buflen;
    292 	cookie->id_buftype = ID_BUFTYPE_LINEAR;
    293 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
    294 	    p, flags);
    295 	if (error) {
    296 		/*
    297 		 * Free the bounce pages, unless our resources
    298 		 * are reserved for our exclusive use.
    299 		 */
    300 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    301 			isadma_bounce_free_bouncebuf(t, map);
    302 		return (error);
    303 	}
    304 
    305 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    306 	cookie->id_flags |= ID_IS_BOUNCING;
    307 	return (0);
    308 }
    309 
    310 /*
    311  * Like isadma_bounce_dmamap_load(), but for mbufs.
    312  */
    313 int
    314 isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map,
    315     struct mbuf *m0, int flags)
    316 {
    317 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    318 	int error;
    319 
    320 	/*
    321 	 * Make sure on error condition we return "no valid mappings."
    322 	 */
    323 	map->dm_mapsize = 0;
    324 	map->dm_nsegs = 0;
    325 
    326 #ifdef DIAGNOSTIC
    327 	if ((m0->m_flags & M_PKTHDR) == 0)
    328 		panic("isadma_bounce_dmamap_load_mbuf: no packet header");
    329 #endif
    330 
    331 	if (m0->m_pkthdr.len > map->_dm_size)
    332 		return (EINVAL);
    333 
    334 	/*
    335 	 * Try to load the map the normal way.  If this errors out,
    336 	 * and we can bounce, we will.
    337 	 */
    338 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
    339 	if (error == 0 ||
    340 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    341 		return (error);
    342 
    343 	/*
    344 	 * First attempt failed; bounce it.
    345 	 */
    346 
    347 	/*
    348 	 * Allocate bounce pages, if necessary.
    349 	 */
    350 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    351 		error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
    352 		    flags);
    353 		if (error)
    354 			return (error);
    355 	}
    356 
    357 	/*
    358 	 * Cache a pointer to the caller's buffer and load the DMA map
    359 	 * with the bounce buffer.
    360 	 */
    361 	cookie->id_origbuf = m0;
    362 	cookie->id_origbuflen = m0->m_pkthdr.len;	/* not really used */
    363 	cookie->id_buftype = ID_BUFTYPE_MBUF;
    364 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
    365 	    m0->m_pkthdr.len, NULL, flags);
    366 	if (error) {
    367 		/*
    368 		 * Free the bounce pages, unless our resources
    369 		 * are reserved for our exclusive use.
    370 		 */
    371 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    372 			isadma_bounce_free_bouncebuf(t, map);
    373 		return (error);
    374 	}
    375 
    376 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    377 	cookie->id_flags |= ID_IS_BOUNCING;
    378 	return (0);
    379 }
    380 
    381 /*
    382  * Like isadma_bounce_dmamap_load(), but for uios.
    383  */
    384 int
    385 isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map,
    386     struct uio *uio, int flags)
    387 {
    388 
    389 	panic("isadma_bounce_dmamap_load_uio: not implemented");
    390 }
    391 
    392 /*
    393  * Like isadma_bounce_dmamap_load(), but for raw memory allocated with
    394  * bus_dmamem_alloc().
    395  */
    396 int
    397 isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    398     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    399 {
    400 
    401 	panic("isadma_bounce_dmamap_load_raw: not implemented");
    402 }
    403 
    404 /*
    405  * Unload an ISA DMA map.
    406  */
    407 void
    408 isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    409 {
    410 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    411 
    412 	/*
    413 	 * If we have bounce pages, free them, unless they're
    414 	 * reserved for our exclusive use.
    415 	 */
    416 	if ((cookie->id_flags & ID_HAS_BOUNCE) &&
    417 	    (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    418 		isadma_bounce_free_bouncebuf(t, map);
    419 
    420 	cookie->id_flags &= ~ID_IS_BOUNCING;
    421 	cookie->id_buftype = ID_BUFTYPE_INVALID;
    422 
    423 	/*
    424 	 * Do the generic bits of the unload.
    425 	 */
    426 	_bus_dmamap_unload(t, map);
    427 }
    428 
    429 /*
    430  * Synchronize an ISA DMA map.
    431  */
    432 void
    433 isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    434     bus_size_t len, int ops)
    435 {
    436 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    437 	void (*sync)(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, bus_size_t, int);
    438 
    439 #if defined(MIPS1) && defined(MIPS3)
    440 	sync = (CPUISMIPS3) ? _mips3_bus_dmamap_sync : _mips1_bus_dmamap_sync;
    441 #elif defined(MIPS1)
    442 	sync = _mips1_bus_dmamap_sync;
    443 #elif defined(MIPS3)
    444 	sync = _mips3_bus_dmamap_sync;
    445 #else
    446 #error neither MIPS1 nor MIPS3 is defined
    447 #endif
    448 	/*
    449 	 * Mixing PRE and POST operations is not allowed.
    450 	 */
    451 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    452 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    453 		panic("isadma_bounce_dmamap_sync: mix PRE and POST");
    454 
    455 #ifdef DIAGNOSTIC
    456 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    457 		if (offset >= map->dm_mapsize)
    458 			panic("isadma_bounce_dmamap_sync: bad offset");
    459 		if (len == 0 || (offset + len) > map->dm_mapsize)
    460 			panic("isadma_bounce_dmamap_sync: bad length");
    461 	}
    462 #endif
    463 
    464 	/*
    465 	 * If we're not bouncing, just drain the write buffer
    466 	 * and flush cache.
    467 	 */
    468 	if ((cookie->id_flags & ID_IS_BOUNCING) == 0) {
    469 		((*sync)(t, map, offset, len, ops));
    470 		return;
    471 	}
    472 
    473 	/*
    474 	 * XXX
    475 	 * This should be needed in BUS_DMASYNC_POSTREAD case only,
    476 	 * if _mips3_bus_dmamap_sync() used "Hit_Invalidate on POSTREAD",
    477 	 * rather than "Hit_Write_Back_Invalidate on PREREAD".
    478 	 */
    479 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_POSTREAD))
    480 		((*sync)(t, map, offset, len, ops));
    481 
    482 	switch (cookie->id_buftype) {
    483 	case ID_BUFTYPE_LINEAR:
    484 		/*
    485 		 * Nothing to do for pre-read.
    486 		 */
    487 
    488 		if (ops & BUS_DMASYNC_PREWRITE) {
    489 			/*
    490 			 * Copy the caller's buffer to the bounce buffer.
    491 			 */
    492 			memcpy((char *)cookie->id_bouncebuf + offset,
    493 			    (char *)cookie->id_origbuf + offset, len);
    494 		}
    495 
    496 		if (ops & BUS_DMASYNC_POSTREAD) {
    497 			/*
    498 			 * Copy the bounce buffer to the caller's buffer.
    499 			 */
    500 			memcpy((char *)cookie->id_origbuf + offset,
    501 			    (char *)cookie->id_bouncebuf + offset, len);
    502 		}
    503 
    504 		/*
    505 		 * Nothing to do for post-write.
    506 		 */
    507 		break;
    508 
    509 	case ID_BUFTYPE_MBUF:
    510 	    {
    511 		struct mbuf *m, *m0 = cookie->id_origbuf;
    512 		bus_size_t minlen, moff;
    513 
    514 		/*
    515 		 * Nothing to do for pre-read.
    516 		 */
    517 
    518 		if (ops & BUS_DMASYNC_PREWRITE) {
    519 			/*
    520 			 * Copy the caller's buffer to the bounce buffer.
    521 			 */
    522 			m_copydata(m0, offset, len,
    523 			    (char *)cookie->id_bouncebuf + offset);
    524 		}
    525 
    526 		if (ops & BUS_DMASYNC_POSTREAD) {
    527 			/*
    528 			 * Copy the bounce buffer to the caller's buffer.
    529 			 */
    530 			for (moff = offset, m = m0; m != NULL && len != 0;
    531 			     m = m->m_next) {
    532 				/* Find the beginning mbuf. */
    533 				if (moff >= m->m_len) {
    534 					moff -= m->m_len;
    535 					continue;
    536 				}
    537 
    538 				/*
    539 				 * Now at the first mbuf to sync; nail
    540 				 * each one until we have exhausted the
    541 				 * length.
    542 				 */
    543 				minlen = len < m->m_len - moff ?
    544 				    len : m->m_len - moff;
    545 
    546 				memcpy(mtod(m, caddr_t) + moff,
    547 				    (char *)cookie->id_bouncebuf + offset,
    548 				    minlen);
    549 
    550 				moff = 0;
    551 				len -= minlen;
    552 				offset += minlen;
    553 			}
    554 		}
    555 
    556 		/*
    557 		 * Nothing to do for post-write.
    558 		 */
    559 		break;
    560 	    }
    561 
    562 	case ID_BUFTYPE_UIO:
    563 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO");
    564 		break;
    565 
    566 	case ID_BUFTYPE_RAW:
    567 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW");
    568 		break;
    569 
    570 	case ID_BUFTYPE_INVALID:
    571 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID");
    572 		break;
    573 
    574 	default:
    575 		printf("unknown buffer type %d\n", cookie->id_buftype);
    576 		panic("isadma_bounce_dmamap_sync");
    577 	}
    578 
    579 	if (ops & BUS_DMASYNC_PREWRITE)
    580 		((*sync)(t, map, offset, len, ops));
    581 }
    582 
    583 /*
    584  * Allocate memory safe for ISA DMA.
    585  */
    586 int
    587 isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size,
    588     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
    589     int nsegs, int *rsegs, int flags)
    590 {
    591 	paddr_t high;
    592 
    593 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
    594 		high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
    595 	else
    596 		high = trunc_page(avail_end);
    597 
    598 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    599 	    segs, nsegs, rsegs, flags, 0, high));
    600 }
    601 
    602 /**********************************************************************
    603  * ISA DMA utility functions
    604  **********************************************************************/
    605 
    606 static int
    607 isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
    608     bus_size_t size, int flags)
    609 {
    610 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    611 	int error = 0;
    612 
    613 	cookie->id_bouncebuflen = round_page(size);
    614 	error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen,
    615 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
    616 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
    617 	if (error)
    618 		goto out;
    619 	error = _bus_dmamem_map(t, cookie->id_bouncesegs,
    620 	    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
    621 	    (caddr_t *)&cookie->id_bouncebuf, flags);
    622 
    623  out:
    624 	if (error) {
    625 		_bus_dmamem_free(t, cookie->id_bouncesegs,
    626 		    cookie->id_nbouncesegs);
    627 		cookie->id_bouncebuflen = 0;
    628 		cookie->id_nbouncesegs = 0;
    629 	} else
    630 		cookie->id_flags |= ID_HAS_BOUNCE;
    631 
    632 	return (error);
    633 }
    634 
    635 static void
    636 isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
    637 {
    638 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    639 
    640 	_bus_dmamem_unmap(t, cookie->id_bouncebuf,
    641 	    cookie->id_bouncebuflen);
    642 	_bus_dmamem_free(t, cookie->id_bouncesegs,
    643 	    cookie->id_nbouncesegs);
    644 	cookie->id_bouncebuflen = 0;
    645 	cookie->id_nbouncesegs = 0;
    646 	cookie->id_flags &= ~ID_HAS_BOUNCE;
    647 }
    648