xref: /netbsd-src/sys/arch/atari/isa/isa_dma.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: isa_dma.c,v 1.11 2012/02/13 19:59:59 jdc 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 2012/02/13 19:59:59 jdc 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 ||
285 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
286 		return (error);
287 
288 	/*
289 	 * First attempt failed; bounce it.
290 	 */
291 
292 	STAT_INCR(isa_dma_stats_bounces);
293 
294 	/*
295 	 * Allocate bounce pages, if necessary.
296 	 */
297 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
298 		error = _isa_dma_alloc_bouncebuf(t, map, buflen, flags);
299 		if (error)
300 			return (error);
301 	}
302 
303 	/*
304 	 * Cache a pointer to the caller's buffer and load the DMA map
305 	 * with the bounce buffer.
306 	 */
307 	cookie->id_origbuf = buf;
308 	cookie->id_origbuflen = buflen;
309 	cookie->id_buftype = ID_BUFTYPE_LINEAR;
310 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
311 	    p, flags);
312 	if (error) {
313 		/*
314 		 * Free the bounce pages, unless our resources
315 		 * are reserved for our exclusive use.
316 		 */
317 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
318 			_isa_dma_free_bouncebuf(t, map);
319 		return (error);
320 	}
321 
322 	/* ...so _isa_bus_dmamap_sync() knows we're bouncing */
323 	cookie->id_flags |= ID_IS_BOUNCING;
324 	return (0);
325 }
326 
327 /*
328  * Like _isa_bus_dmamap_load(), but for mbufs.
329  */
330 int
331 _isa_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
332     int flags)
333 {
334 	struct atari_isa_dma_cookie *cookie = map->_dm_cookie;
335 	int error;
336 
337 	/*
338 	 * Make sure on error condition we return "no valid mappings."
339 	 */
340 	map->dm_mapsize = 0;
341 	map->dm_nsegs = 0;
342 
343 #ifdef DIAGNOSTIC
344 	if ((m0->m_flags & M_PKTHDR) == 0)
345 		panic("_isa_bus_dmamap_load_mbuf: no packet header");
346 #endif
347 
348 	if (m0->m_pkthdr.len > map->_dm_size)
349 		return (EINVAL);
350 
351 	/*
352 	 * Try to load the map the normal way.  If this errors out,
353 	 * and we can bounce, we will.
354 	 */
355 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
356 	if (error == 0 ||
357 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
358 		return (error);
359 
360 	/*
361 	 * First attempt failed; bounce it.
362 	 */
363 
364 	STAT_INCR(isa_dma_stats_bounces);
365 
366 	/*
367 	 * Allocate bounce pages, if necessary.
368 	 */
369 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
370 		error = _isa_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
371 		    flags);
372 		if (error)
373 			return (error);
374 	}
375 
376 	/*
377 	 * Cache a pointer to the caller's buffer and load the DMA map
378 	 * with the bounce buffer.
379 	 */
380 	cookie->id_origbuf = m0;
381 	cookie->id_origbuflen = m0->m_pkthdr.len;	/* not really used */
382 	cookie->id_buftype = ID_BUFTYPE_MBUF;
383 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
384 	    m0->m_pkthdr.len, NULL, flags);
385 	if (error) {
386 		/*
387 		 * Free the bounce pages, unless our resources
388 		 * are reserved for our exclusive use.
389 		 */
390 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
391 			_isa_dma_free_bouncebuf(t, map);
392 		return (error);
393 	}
394 
395 	/* ...so _isa_bus_dmamap_sync() knows we're bouncing */
396 	cookie->id_flags |= ID_IS_BOUNCING;
397 	return (0);
398 }
399 
400 /*
401  * Like _isa_bus_dmamap_load(), but for uios.
402  */
403 int
404 _isa_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
405 {
406 
407 	panic("_isa_bus_dmamap_load_uio: not implemented");
408 }
409 
410 /*
411  * Like _isa_bus_dmamap_load(), but for raw memory allocated with
412  * bus_dmamem_alloc().
413  */
414 int
415 _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)
416 {
417 
418 	panic("_isa_bus_dmamap_load_raw: not implemented");
419 }
420 
421 /*
422  * Unload an ISA DMA map.
423  */
424 void
425 _isa_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
426 {
427 	struct atari_isa_dma_cookie *cookie = map->_dm_cookie;
428 
429 	/*
430 	 * If we have bounce pages, free them, unless they're
431 	 * reserved for our exclusive use.
432 	 */
433 	if ((cookie->id_flags & ID_HAS_BOUNCE) &&
434 	    (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
435 		_isa_dma_free_bouncebuf(t, map);
436 
437 	cookie->id_flags &= ~ID_IS_BOUNCING;
438 	cookie->id_buftype = ID_BUFTYPE_INVALID;
439 
440 	/*
441 	 * Do the generic bits of the unload.
442 	 */
443 	_bus_dmamap_unload(t, map);
444 }
445 
446 /*
447  * Synchronize an ISA DMA map.
448  */
449 void
450 _isa_bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops)
451 {
452 	struct atari_isa_dma_cookie *cookie = map->_dm_cookie;
453 
454 	/*
455 	 * Mixing PRE and POST operations is not allowed.
456 	 */
457 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
458 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
459 		panic("_isa_bus_dmamap_sync: mix PRE and POST");
460 
461 #ifdef DIAGNOSTIC
462 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
463 		if (offset >= map->dm_mapsize)
464 			panic("_isa_bus_dmamap_sync: bad offset");
465 		if (len == 0 || (offset + len) > map->dm_mapsize)
466 			panic("_isa_bus_dmamap_sync: bad length");
467 	}
468 #endif
469 
470 	/*
471 	 * If we're not bouncing, just return; nothing to do.
472 	 */
473 	if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
474 		return;
475 
476 	switch (cookie->id_buftype) {
477 	case ID_BUFTYPE_LINEAR:
478 		/*
479 		 * Nothing to do for pre-read.
480 		 */
481 
482 		if (ops & BUS_DMASYNC_PREWRITE) {
483 			/*
484 			 * Copy the caller's buffer to the bounce buffer.
485 			 */
486 			memcpy((char *)cookie->id_bouncebuf + offset,
487 			    (char *)cookie->id_origbuf + offset, len);
488 		}
489 
490 		if (ops & BUS_DMASYNC_POSTREAD) {
491 			/*
492 			 * Copy the bounce buffer to the caller's buffer.
493 			 */
494 			memcpy((char *)cookie->id_origbuf + offset,
495 			    (char *)cookie->id_bouncebuf + offset, len);
496 		}
497 
498 		/*
499 		 * Nothing to do for post-write.
500 		 */
501 		break;
502 
503 	case ID_BUFTYPE_MBUF:
504 	    {
505 		struct mbuf *m, *m0 = cookie->id_origbuf;
506 		bus_size_t minlen, moff;
507 
508 		/*
509 		 * Nothing to do for pre-read.
510 		 */
511 
512 		if (ops & BUS_DMASYNC_PREWRITE) {
513 			/*
514 			 * Copy the caller's buffer to the bounce buffer.
515 			 */
516 			m_copydata(m0, offset, len,
517 			    (char *)cookie->id_bouncebuf + offset);
518 		}
519 
520 		if (ops & BUS_DMASYNC_POSTREAD) {
521 			/*
522 			 * Copy the bounce buffer to the caller's buffer.
523 			 */
524 			for (moff = offset, m = m0; m != NULL && len != 0;
525 			     m = m->m_next) {
526 				/* Find the beginning mbuf. */
527 				if (moff >= m->m_len) {
528 					moff -= m->m_len;
529 					continue;
530 				}
531 
532 				/*
533 				 * Now at the first mbuf to sync; nail
534 				 * each one until we have exhausted the
535 				 * length.
536 				 */
537 				minlen = len < m->m_len - moff ?
538 				    len : m->m_len - moff;
539 
540 				memcpy(mtod(m, char *) + moff,
541 				    (char *)cookie->id_bouncebuf + offset,
542 				    minlen);
543 
544 				moff = 0;
545 				len -= minlen;
546 				offset += minlen;
547 			}
548 		}
549 
550 		/*
551 		 * Nothing to do for post-write.
552 		 */
553 		break;
554 	    }
555 
556 	case ID_BUFTYPE_UIO:
557 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_UIO");
558 		break;
559 
560 	case ID_BUFTYPE_RAW:
561 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_RAW");
562 		break;
563 
564 	case ID_BUFTYPE_INVALID:
565 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_INVALID");
566 		break;
567 
568 	default:
569 		printf("unknown buffer type %d\n", cookie->id_buftype);
570 		panic("_isa_bus_dmamap_sync");
571 	}
572 }
573 
574 /*
575  * Allocate memory safe for ISA DMA.
576  */
577 int
578 _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)
579 {
580 	paddr_t high;
581 
582 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
583 		high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
584 	else
585 		high = trunc_page(avail_end);
586 
587 	return (bus_dmamem_alloc_range(t, size, alignment, boundary,
588 	    segs, nsegs, rsegs, flags, 0, high));
589 }
590 
591 /**********************************************************************
592  * ISA DMA utility functions
593  **********************************************************************/
594 
595 int
596 _isa_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t size, int flags)
597 {
598 	struct atari_isa_dma_cookie *cookie = map->_dm_cookie;
599 	int error = 0;
600 
601 	cookie->id_bouncebuflen = round_page(size);
602 	error = _isa_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
603 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
604 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
605 	if (error)
606 		goto out;
607 	error = bus_dmamem_map(t, cookie->id_bouncesegs,
608 	    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
609 	    (void **)&cookie->id_bouncebuf, flags);
610 
611  out:
612 	if (error) {
613 		bus_dmamem_free(t, cookie->id_bouncesegs,
614 		    cookie->id_nbouncesegs);
615 		cookie->id_bouncebuflen = 0;
616 		cookie->id_nbouncesegs = 0;
617 	} else {
618 		cookie->id_flags |= ID_HAS_BOUNCE;
619 		STAT_INCR(isa_dma_stats_nbouncebufs);
620 	}
621 
622 	return (error);
623 }
624 
625 void
626 _isa_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
627 {
628 	struct atari_isa_dma_cookie *cookie = map->_dm_cookie;
629 
630 	STAT_DECR(isa_dma_stats_nbouncebufs);
631 
632 	bus_dmamem_unmap(t, cookie->id_bouncebuf,
633 	    cookie->id_bouncebuflen);
634 	bus_dmamem_free(t, cookie->id_bouncesegs,
635 	    cookie->id_nbouncesegs);
636 	cookie->id_bouncebuflen = 0;
637 	cookie->id_nbouncesegs = 0;
638 	cookie->id_flags &= ~ID_HAS_BOUNCE;
639 }
640