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