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