isadma_machdep.c revision 1.10 1 /* $NetBSD: isadma_machdep.c,v 1.10 2007/03/04 10:21:26 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 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>
41 __KERNEL_RCSID(0, "$NetBSD: isadma_machdep.c,v 1.10 2007/03/04 10:21:26 christos Exp $");
42
43 #define ISA_DMA_STATS
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 _ARM32_BUS_DMA_PRIVATE
54 #include <machine/bus.h>
55
56 #include <dev/isa/isareg.h>
57 #include <dev/isa/isavar.h>
58
59 #include <uvm/uvm_extern.h>
60
61 /*
62 * ISA has a 24-bit address limitation, so at most it has a 16M
63 * DMA range. However, some platforms have a more limited range,
64 * e.g. the Shark NC. On these systems, we are provided with
65 * a set of DMA ranges. The pmap module is aware of these ranges
66 * and places DMA-safe memory for them onto an alternate free list
67 * so that they are protected from being used to service page faults,
68 * etc. (unless we've run out of memory elsewhere).
69 */
70 extern struct arm32_dma_range *shark_isa_dma_ranges;
71 extern int shark_isa_dma_nranges;
72
73 int _isa_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
74 bus_size_t, bus_size_t, int, bus_dmamap_t *));
75 void _isa_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
76 int _isa_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
77 bus_size_t, struct proc *, int));
78 int _isa_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
79 struct mbuf *, int));
80 int _isa_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
81 struct uio *, int));
82 int _isa_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
83 bus_dma_segment_t *, int, bus_size_t, int));
84 void _isa_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
85 void _isa_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
86 bus_addr_t, bus_size_t, int));
87
88 int _isa_bus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t,
89 bus_size_t, bus_dma_segment_t *, int, int *, int));
90
91 int _isa_dma_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
92 bus_size_t, int));
93 void _isa_dma_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
94
95 /*
96 * Entry points for ISA DMA. These are mostly wrappers around
97 * the generic functions that understand how to deal with bounce
98 * buffers, if necessary.
99 */
100 struct arm32_bus_dma_tag isa_bus_dma_tag = {
101 0, /* _ranges */
102 0, /* _nranges */
103 NULL, /* _cookie */
104 _isa_bus_dmamap_create,
105 _isa_bus_dmamap_destroy,
106 _isa_bus_dmamap_load,
107 _isa_bus_dmamap_load_mbuf,
108 _isa_bus_dmamap_load_uio,
109 _isa_bus_dmamap_load_raw,
110 _isa_bus_dmamap_unload,
111 _isa_bus_dmamap_sync, /* pre */
112 _isa_bus_dmamap_sync, /* post */
113 _isa_bus_dmamem_alloc,
114 _bus_dmamem_free,
115 _bus_dmamem_map,
116 _bus_dmamem_unmap,
117 _bus_dmamem_mmap,
118 };
119
120 /*
121 * Initialize ISA DMA.
122 */
123 void
124 isa_dma_init()
125 {
126
127 isa_bus_dma_tag._ranges = shark_isa_dma_ranges;
128 isa_bus_dma_tag._nranges = shark_isa_dma_nranges;
129 }
130
131 /**********************************************************************
132 * bus.h dma interface entry points
133 **********************************************************************/
134
135 #ifdef ISA_DMA_STATS
136 #define STAT_INCR(v) (v)++
137 #define STAT_DECR(v) do { \
138 if ((v) == 0) \
139 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
140 else \
141 (v)--; \
142 } while (0)
143 u_long isa_dma_stats_loads;
144 u_long isa_dma_stats_bounces;
145 u_long isa_dma_stats_nbouncebufs;
146 #else
147 #define STAT_INCR(v)
148 #define STAT_DECR(v)
149 #endif
150
151 /*
152 * Create an ISA DMA map.
153 */
154 int
155 _isa_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
156 bus_dma_tag_t t;
157 bus_size_t size;
158 int nsegments;
159 bus_size_t maxsegsz;
160 bus_size_t boundary;
161 int flags;
162 bus_dmamap_t *dmamp;
163 {
164 struct arm32_isa_dma_cookie *cookie;
165 bus_dmamap_t map;
166 int error, cookieflags;
167 void *cookiestore;
168 size_t cookiesize;
169
170 /* Call common function to create the basic map. */
171 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
172 flags, dmamp);
173 if (error)
174 return (error);
175
176 map = *dmamp;
177 map->_dm_cookie = NULL;
178
179 cookiesize = sizeof(struct arm32_isa_dma_cookie);
180
181 /*
182 * ISA only has 24-bits of address space. This means
183 * we can't DMA to pages over 16M. In order to DMA to
184 * arbitrary buffers, we use "bounce buffers" - pages
185 * in memory below the 16M boundary. On DMA reads,
186 * DMA happens to the bounce buffers, and is copied into
187 * the caller's buffer. On writes, data is copied into
188 * but bounce buffer, and the DMA happens from those
189 * pages. To software using the DMA mapping interface,
190 * this looks simply like a data cache.
191 *
192 * If we have more than 16M of RAM in the system, we may
193 * need bounce buffers. We check and remember that here.
194 *
195 * There are exceptions, however. VLB devices can do
196 * 32-bit DMA, and indicate that here.
197 *
198 * ...or, there is an opposite case. The most segments
199 * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If
200 * the caller can't handle that many segments (e.g. the
201 * ISA DMA controller), we may have to bounce it as well.
202 *
203 * Well, not really... see note above regarding DMA ranges.
204 * Because of the range issue on this platform, we just
205 * always "might bounce".
206 */
207 cookieflags = ID_MIGHT_NEED_BOUNCE;
208 cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
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 arm32_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(t, map)
248 bus_dma_tag_t t;
249 bus_dmamap_t map;
250 {
251 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
252
253 /*
254 * Free any bounce pages this map might hold.
255 */
256 if (cookie->id_flags & ID_HAS_BOUNCE)
257 _isa_dma_free_bouncebuf(t, map);
258
259 free(cookie, M_DMAMAP);
260 _bus_dmamap_destroy(t, map);
261 }
262
263 /*
264 * Load an ISA DMA map with a linear buffer.
265 */
266 int
267 _isa_bus_dmamap_load(t, map, buf, buflen, p, flags)
268 bus_dma_tag_t t;
269 bus_dmamap_t map;
270 void *buf;
271 bus_size_t buflen;
272 struct proc *p;
273 int flags;
274 {
275 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
276 int error;
277
278 STAT_INCR(isa_dma_stats_loads);
279
280 /*
281 * Make sure that on error condition we return "no valid mappings."
282 */
283 map->dm_mapsize = 0;
284 map->dm_nsegs = 0;
285
286 /*
287 * Try to load the map the normal way. If this errors out,
288 * and we can bounce, we will.
289 */
290 error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
291 if (error == 0 ||
292 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
293 return (error);
294
295 /*
296 * First attempt failed; bounce it.
297 */
298
299 STAT_INCR(isa_dma_stats_bounces);
300
301 /*
302 * Allocate bounce pages, if necessary.
303 */
304 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
305 error = _isa_dma_alloc_bouncebuf(t, map, buflen, flags);
306 if (error)
307 return (error);
308 }
309
310 /*
311 * Cache a pointer to the caller's buffer and load the DMA map
312 * with the bounce buffer.
313 */
314 cookie->id_origbuf = buf;
315 cookie->id_origbuflen = buflen;
316 cookie->id_buftype = ID_BUFTYPE_LINEAR;
317 error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
318 NULL, flags);
319 if (error) {
320 /*
321 * Free the bounce pages, unless our resources
322 * are reserved for our exclusive use.
323 */
324 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
325 _isa_dma_free_bouncebuf(t, map);
326 return (error);
327 }
328
329 /* ...so _isa_bus_dmamap_sync() knows we're bouncing */
330 cookie->id_flags |= ID_IS_BOUNCING;
331 return (0);
332 }
333
334 /*
335 * Like _isa_bus_dmamap_load(), but for mbufs.
336 */
337 int
338 _isa_bus_dmamap_load_mbuf(t, map, m0, flags)
339 bus_dma_tag_t t;
340 bus_dmamap_t map;
341 struct mbuf *m0;
342 int flags;
343 {
344 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
345 int error;
346
347 /*
348 * Make sure that on error condition we return "no valid mappings."
349 */
350 map->dm_mapsize = 0;
351 map->dm_nsegs = 0;
352
353 #ifdef DIAGNOSTIC
354 if ((m0->m_flags & M_PKTHDR) == 0)
355 panic("_isa_bus_dmamap_load_mbuf: no packet header");
356 #endif
357
358 if (m0->m_pkthdr.len > map->_dm_size)
359 return (EINVAL);
360
361 /*
362 * Try to load the map the normal way. If this errors out,
363 * and we can bounce, we will.
364 */
365 error = _bus_dmamap_load_mbuf(t, map, m0, flags);
366 if (error == 0 ||
367 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
368 return (error);
369
370 /*
371 * First attempt failed; bounce it.
372 */
373
374 STAT_INCR(isa_dma_stats_bounces);
375
376 /*
377 * Allocate bounce pages, if necessary.
378 */
379 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
380 error = _isa_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
381 flags);
382 if (error)
383 return (error);
384 }
385
386 /*
387 * Cache a pointer to the caller's buffer and load the DMA map
388 * with the bounce buffer.
389 */
390 cookie->id_origbuf = m0;
391 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
392 cookie->id_buftype = ID_BUFTYPE_MBUF;
393 error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
394 m0->m_pkthdr.len, NULL, flags);
395 if (error) {
396 /*
397 * Free the bounce pages, unless our resources
398 * are reserved for our exclusive use.
399 */
400 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
401 _isa_dma_free_bouncebuf(t, map);
402 return (error);
403 }
404
405 /* ...so _isa_bus_dmamap_sync() knows we're bouncing */
406 cookie->id_flags |= ID_IS_BOUNCING;
407 return (0);
408 }
409
410 /*
411 * Like _isa_bus_dmamap_load(), but for uios.
412 */
413 int
414 _isa_bus_dmamap_load_uio(t, map, uio, flags)
415 bus_dma_tag_t t;
416 bus_dmamap_t map;
417 struct uio *uio;
418 int flags;
419 {
420
421 panic("_isa_bus_dmamap_load_uio: not implemented");
422 }
423
424 /*
425 * Like _isa_bus_dmamap_load(), but for raw memory allocated with
426 * bus_dmamem_alloc().
427 */
428 int
429 _isa_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
430 bus_dma_tag_t t;
431 bus_dmamap_t map;
432 bus_dma_segment_t *segs;
433 int nsegs;
434 bus_size_t size;
435 int flags;
436 {
437
438 panic("_isa_bus_dmamap_load_raw: not implemented");
439 }
440
441 /*
442 * Unload an ISA DMA map.
443 */
444 void
445 _isa_bus_dmamap_unload(t, map)
446 bus_dma_tag_t t;
447 bus_dmamap_t map;
448 {
449 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
450
451 /*
452 * If we have bounce pages, free them, unless they're
453 * reserved for our exclusive use.
454 */
455 if ((cookie->id_flags & ID_HAS_BOUNCE) &&
456 (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
457 _isa_dma_free_bouncebuf(t, map);
458
459 cookie->id_flags &= ~ID_IS_BOUNCING;
460 cookie->id_buftype = ID_BUFTYPE_INVALID;
461
462 /*
463 * Do the generic bits of the unload.
464 */
465 _bus_dmamap_unload(t, map);
466 }
467
468 /*
469 * Synchronize an ISA DMA map.
470 */
471 void
472 _isa_bus_dmamap_sync(t, map, offset, len, ops)
473 bus_dma_tag_t t;
474 bus_dmamap_t map;
475 bus_addr_t offset;
476 bus_size_t len;
477 int ops;
478 {
479 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
480
481 /*
482 * Mixing PRE and POST operations is not allowed.
483 */
484 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
485 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
486 panic("_isa_bus_dmamap_sync: mix PRE and POST");
487
488 #ifdef DIAGNOSTIC
489 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
490 if (offset >= map->dm_mapsize)
491 panic("_isa_bus_dmamap_sync: bad offset");
492 if (len == 0 || (offset + len) > map->dm_mapsize)
493 panic("_isa_bus_dmamap_sync: bad length");
494 }
495 #endif
496
497 /*
498 * If we're not bouncing, just return; nothing to do.
499 */
500 if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
501 return;
502
503 switch (cookie->id_buftype) {
504 case ID_BUFTYPE_LINEAR:
505 /*
506 * Nothing to do for pre-read.
507 */
508
509 if (ops & BUS_DMASYNC_PREWRITE) {
510 /*
511 * Copy the caller's buffer to the bounce buffer.
512 */
513 memcpy((char *)cookie->id_bouncebuf + offset,
514 (char *)cookie->id_origbuf + offset, len);
515 }
516
517 if (ops & BUS_DMASYNC_POSTREAD) {
518 /*
519 * Copy the bounce buffer to the caller's buffer.
520 */
521 memcpy((char *)cookie->id_origbuf + offset,
522 (char *)cookie->id_bouncebuf + offset, len);
523 }
524
525 /*
526 * Nothing to do for post-write.
527 */
528 break;
529
530 case ID_BUFTYPE_MBUF:
531 {
532 struct mbuf *m, *m0 = cookie->id_origbuf;
533 bus_size_t minlen, moff;
534
535 /*
536 * Nothing to do for pre-read.
537 */
538
539 if (ops & BUS_DMASYNC_PREWRITE) {
540 /*
541 * Copy the caller's buffer to the bounce buffer.
542 */
543 m_copydata(m0, offset, len,
544 (char *)cookie->id_bouncebuf + offset);
545 }
546
547 if (ops & BUS_DMASYNC_POSTREAD) {
548 /*
549 * Copy the bounce buffer to the caller's buffer.
550 */
551 for (moff = offset, m = m0; m != NULL && len != 0;
552 m = m->m_next) {
553 /* Find the beginning mbuf. */
554 if (moff >= m->m_len) {
555 moff -= m->m_len;
556 continue;
557 }
558
559 /*
560 * Now at the first mbuf to sync; nail
561 * each one until we have exhausted the
562 * length.
563 */
564 minlen = len < m->m_len - moff ?
565 len : m->m_len - moff;
566
567 memcpy(mtod(m, char *) + moff,
568 (char *)cookie->id_bouncebuf + offset,
569 minlen);
570
571 moff = 0;
572 len -= minlen;
573 offset += minlen;
574 }
575 }
576
577 /*
578 * Nothing to do for post-write.
579 */
580 break;
581 }
582
583 case ID_BUFTYPE_UIO:
584 panic("_isa_bus_dmamap_sync: ID_BUFTYPE_UIO");
585 break;
586
587 case ID_BUFTYPE_RAW:
588 panic("_isa_bus_dmamap_sync: ID_BUFTYPE_RAW");
589 break;
590
591 case ID_BUFTYPE_INVALID:
592 panic("_isa_bus_dmamap_sync: ID_BUFTYPE_INVALID");
593 break;
594
595 default:
596 printf("unknown buffer type %d\n", cookie->id_buftype);
597 panic("_isa_bus_dmamap_sync");
598 }
599 }
600
601 /*
602 * Allocate memory safe for ISA DMA.
603 */
604 int
605 _isa_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
606 bus_dma_tag_t t;
607 bus_size_t size, alignment, boundary;
608 bus_dma_segment_t *segs;
609 int nsegs;
610 int *rsegs;
611 int flags;
612 {
613
614 if (t->_ranges == NULL)
615 return (ENOMEM);
616
617 /* _bus_dmamem_alloc() does the range checks for us. */
618 return (_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs,
619 rsegs, flags));
620 }
621
622 /**********************************************************************
623 * ISA DMA utility functions
624 **********************************************************************/
625
626 int
627 _isa_dma_alloc_bouncebuf(t, map, size, flags)
628 bus_dma_tag_t t;
629 bus_dmamap_t map;
630 bus_size_t size;
631 int flags;
632 {
633 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
634 int error = 0;
635
636 cookie->id_bouncebuflen = round_page(size);
637 error = _isa_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
638 PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
639 map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
640 if (error)
641 goto out;
642 error = _bus_dmamem_map(t, cookie->id_bouncesegs,
643 cookie->id_nbouncesegs, cookie->id_bouncebuflen,
644 (void **)&cookie->id_bouncebuf, flags);
645
646 out:
647 if (error) {
648 _bus_dmamem_free(t, cookie->id_bouncesegs,
649 cookie->id_nbouncesegs);
650 cookie->id_bouncebuflen = 0;
651 cookie->id_nbouncesegs = 0;
652 } else {
653 cookie->id_flags |= ID_HAS_BOUNCE;
654 STAT_INCR(isa_dma_stats_nbouncebufs);
655 }
656
657 return (error);
658 }
659
660 void
661 _isa_dma_free_bouncebuf(t, map)
662 bus_dma_tag_t t;
663 bus_dmamap_t map;
664 {
665 struct arm32_isa_dma_cookie *cookie = map->_dm_cookie;
666
667 STAT_DECR(isa_dma_stats_nbouncebufs);
668
669 _bus_dmamem_unmap(t, cookie->id_bouncebuf,
670 cookie->id_bouncebuflen);
671 _bus_dmamem_free(t, cookie->id_bouncesegs,
672 cookie->id_nbouncesegs);
673 cookie->id_bouncebuflen = 0;
674 cookie->id_nbouncesegs = 0;
675 cookie->id_flags &= ~ID_HAS_BOUNCE;
676 }
677