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