intio.c revision 1.4 1 /* $NetBSD: intio.c,v 1.4 1999/03/22 03:21:35 minoura Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1998 NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * NetBSD/x68k internal I/O virtual bus.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/extent.h>
45 #include <vm/vm.h>
46
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/frame.h>
50
51 #include <arch/x68k/dev/intiovar.h>
52 #include <arch/x68k/dev/mfp.h>
53
54
55 /*
56 * bus_space(9) interface
57 */
58 static int intio_bus_space_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int, bus_space_handle_t *));
59 static void intio_bus_space_unmap __P((bus_space_tag_t, bus_space_handle_t, bus_size_t));
60 static int intio_bus_space_subregion __P((bus_space_tag_t, bus_space_handle_t, bus_size_t, bus_size_t, bus_space_handle_t *));
61
62 static struct x68k_bus_space intio_bus = {
63 #if 0
64 X68K_INTIO_BUS,
65 #endif
66 intio_bus_space_map, intio_bus_space_unmap, intio_bus_space_subregion,
67 x68k_bus_space_alloc, x68k_bus_space_free,
68 #if 0
69 x68k_bus_space_barrier,
70 #endif
71
72 0
73 };
74
75 /*
76 * bus_dma(9) interface
77 */
78 #define INTIO_DMA_BOUNCE_THRESHOLD (16 * 1024 * 1024)
79 int _intio_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
80 bus_size_t, bus_size_t, int, bus_dmamap_t *));
81 void _intio_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
82 int _intio_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
83 bus_size_t, struct proc *, int));
84 int _intio_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
85 struct mbuf *, int));
86 int _intio_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
87 struct uio *, int));
88 int _intio_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
89 bus_dma_segment_t *, int, bus_size_t, int));
90 void _intio_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
91 void _intio_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
92 bus_addr_t, bus_size_t, int));
93
94 int _intio_bus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t,
95 bus_size_t, bus_dma_segment_t *, int, int *, int));
96
97 int _intio_dma_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
98 bus_size_t, int));
99 void _intio_dma_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
100
101 struct x68k_bus_dma intio_bus_dma = {
102 INTIO_DMA_BOUNCE_THRESHOLD,
103 _intio_bus_dmamap_create,
104 _intio_bus_dmamap_destroy,
105 _intio_bus_dmamap_load,
106 _intio_bus_dmamap_load_mbuf,
107 _intio_bus_dmamap_load_uio,
108 _intio_bus_dmamap_load_raw,
109 _intio_bus_dmamap_unload,
110 _intio_bus_dmamap_sync,
111 _intio_bus_dmamem_alloc,
112 x68k_bus_dmamem_free,
113 x68k_bus_dmamem_map,
114 x68k_bus_dmamem_unmap,
115 x68k_bus_dmamem_mmap,
116 };
117
118 /*
119 * autoconf stuff
120 */
121 static int intio_match __P((struct device *, struct cfdata *, void *));
122 static void intio_attach __P((struct device *, struct device *, void *));
123 static int intio_search __P((struct device *, struct cfdata *cf, void *));
124 static int intio_print __P((void *, const char *));
125 static void intio_alloc_system_ports __P((struct intio_softc*));
126
127 struct cfattach intio_ca = {
128 sizeof(struct intio_softc), intio_match, intio_attach
129 };
130
131 static struct intio_interrupt_vector {
132 intio_intr_handler_t iiv_handler;
133 void *iiv_arg;
134 int iiv_intrcntoff;
135 } iiv[256] = {0,};
136
137 extern struct cfdriver intio_cd;
138
139 /* used in console initialization */
140 extern int x68k_realconfig;
141 int x68k_config_found __P((struct cfdata *, struct device *,
142 void *, cfprint_t));
143 static struct cfdata *cfdata_intiobus = NULL;
144
145 /* other static functions */
146 static int scan_intrnames __P((const char *));
147 #ifdef DEBUG
148 int intio_debug = 0;
149 #endif
150
151 static int
152 intio_match(parent, cf, aux)
153 struct device *parent;
154 struct cfdata *cf;
155 void *aux; /* NULL */
156 {
157 if (strcmp(aux, intio_cd.cd_name) != 0)
158 return (0);
159 if (cf->cf_unit != 0)
160 return (0);
161 if (x68k_realconfig == 0)
162 cfdata_intiobus = cf; /* XXX */
163
164 return (1);
165 }
166
167
168 /* used in console initialization: configure only MFP */
169 static struct intio_attach_args initial_ia = {
170 &intio_bus,
171 0/*XXX*/,
172
173 "mfp", /* ia_name */
174 MFP_ADDR, /* ia_addr */
175 MFP_INTR, /* ia_intr */
176 -1 /* ia_dma */
177 -1, /* ia_dmaintr */
178 };
179
180 static void
181 intio_attach(parent, self, aux)
182 struct device *parent, *self;
183 void *aux; /* NULL */
184 {
185 struct intio_softc *sc = (struct intio_softc *)self;
186 struct intio_attach_args ia;
187
188 if (self == NULL) {
189 /* console only init */
190 x68k_config_found(cfdata_intiobus, NULL, &initial_ia, NULL);
191 return;
192 }
193
194 printf (" mapped at %8p\n", intiobase);
195
196 sc->sc_map = extent_create("intiomap",
197 PHYS_INTIODEV,
198 PHYS_INTIODEV + 0x400000,
199 M_DEVBUF, NULL, NULL, EX_NOWAIT);
200 intio_alloc_system_ports (sc);
201
202 sc->sc_bst = &intio_bus;
203 sc->sc_bst->x68k_bus_device = self;
204 sc->sc_dmat = &intio_bus_dma;
205 sc->sc_dmac = 0;
206
207 bzero(iiv, sizeof (struct intio_interrupt_vector) * 256);
208
209 ia.ia_bst = sc->sc_bst;
210 ia.ia_dmat = sc->sc_dmat;
211
212 config_search (intio_search, self, &ia);
213 }
214
215 static int
216 intio_search(parent, cf, aux)
217 struct device *parent;
218 struct cfdata *cf;
219 void *aux;
220 {
221 struct intio_attach_args *ia = aux;
222 struct intio_softc *sc = (struct intio_softc *)parent;
223
224 ia->ia_bst = sc->sc_bst;
225 ia->ia_dmat = sc->sc_dmat;
226 ia->ia_name = cf->cf_driver->cd_name;
227 ia->ia_addr = cf->cf_addr;
228 ia->ia_intr = cf->cf_intr;
229 ia->ia_dma = cf->cf_dma;
230 ia->ia_dmaintr = cf->cf_dmaintr;
231
232 if ((*cf->cf_attach->ca_match)(parent, cf, ia) > 0)
233 config_attach(parent, cf, ia, intio_print);
234
235 return (0);
236 }
237
238 static int
239 intio_print(aux, name)
240 void *aux;
241 const char *name;
242 {
243 struct intio_attach_args *ia = aux;
244
245 /* if (ia->ia_addr > 0) */
246 printf (" addr 0x%06x", ia->ia_addr);
247 if (ia->ia_intr > 0)
248 printf (" intr 0x%02x", ia->ia_intr);
249 if (ia->ia_dma >= 0) {
250 printf (" using DMA ch%d", ia->ia_dma);
251 if (ia->ia_dmaintr > 0)
252 printf (" intr 0x%02x and 0x%02x",
253 ia->ia_dmaintr, ia->ia_dmaintr+1);
254 }
255
256 return (QUIET);
257 }
258
259 /*
260 * intio memory map manager
261 */
262
263 int
264 intio_map_allocate_region(parent, ia, flag)
265 struct device *parent;
266 struct intio_attach_args *ia;
267 enum intio_map_flag flag; /* INTIO_MAP_TESTONLY or INTIO_MAP_ALLOCATE */
268 {
269 struct intio_softc *sc = (struct intio_softc*) parent;
270 struct extent *map = sc->sc_map;
271 int r;
272
273 r = extent_alloc_region (map, ia->ia_addr, ia->ia_size, 0);
274 #ifdef DEBUG
275 if (intio_debug)
276 extent_print (map);
277 #endif
278 if (r == 0) {
279 if (flag != INTIO_MAP_ALLOCATE)
280 extent_free (map, ia->ia_addr, ia->ia_size, 0);
281 return 0;
282 }
283
284 return -1;
285 }
286
287 int
288 intio_map_free_region(parent, ia)
289 struct device *parent;
290 struct intio_attach_args *ia;
291 {
292 struct intio_softc *sc = (struct intio_softc*) parent;
293 struct extent *map = sc->sc_map;
294
295 extent_free (map, ia->ia_addr, ia->ia_size, 0);
296 #ifdef DEBUG
297 if (intio_debug)
298 extent_print (map);
299 #endif
300 return 0;
301 }
302
303 void
304 intio_alloc_system_ports(sc)
305 struct intio_softc *sc;
306 {
307 extent_alloc_region (sc->sc_map, INTIO_SYSPORT, 16, 0);
308 extent_alloc_region (sc->sc_map, INTIO_SICILIAN, 0x2000, 0);
309 }
310
311
312 /*
313 * intio bus space stuff.
314 */
315 static int
316 intio_bus_space_map(t, bpa, size, flags, bshp)
317 bus_space_tag_t t;
318 bus_addr_t bpa;
319 bus_size_t size;
320 int flags;
321 bus_space_handle_t *bshp;
322 {
323 /*
324 * Intio bus is mapped permanently.
325 */
326 *bshp = (bus_space_handle_t)
327 ((u_int) bpa - PHYS_INTIODEV + intiobase);
328 /*
329 * Some devices are mapped on odd addresses only.
330 */
331 if (flags & BUS_SPACE_MAP_SHIFTED)
332 *bshp += 0x80000001;
333
334 return (0);
335 }
336
337 static void
338 intio_bus_space_unmap(t, bsh, size)
339 bus_space_tag_t t;
340 bus_space_handle_t bsh;
341 bus_size_t size;
342 {
343 return;
344 }
345
346 static int
347 intio_bus_space_subregion(t, bsh, offset, size, nbshp)
348 bus_space_tag_t t;
349 bus_space_handle_t bsh;
350 bus_size_t offset, size;
351 bus_space_handle_t *nbshp;
352 {
353
354 *nbshp = bsh + offset;
355 return (0);
356 }
357
358
359 /*
360 * interrupt handler
361 */
362 int
363 intio_intr_establish (vector, name, handler, arg)
364 int vector;
365 const char *name; /* XXX */
366 intio_intr_handler_t handler;
367 void *arg;
368 {
369 if (vector < 16)
370 panic ("Invalid interrupt vector");
371 if (iiv[vector].iiv_handler)
372 return EBUSY;
373 iiv[vector].iiv_handler = handler;
374 iiv[vector].iiv_arg = arg;
375 iiv[vector].iiv_intrcntoff = scan_intrnames(name);
376
377 return 0;
378 }
379
380 static int
381 scan_intrnames (name)
382 const char *name;
383 {
384 extern char intrnames[];
385 extern char eintrnames[];
386 int r = 0;
387 char *p = &intrnames[0];
388
389 for (;;) {
390 if (*p == 0) { /* new intr */
391 if (p + strlen(name) >= eintrnames)
392 panic ("Interrupt statics buffer overrun.");
393 strcpy (p, name);
394 break;
395 }
396 if (strcmp(p, name) == 0)
397 break;
398 r++;
399 while (*p++ != 0);
400 }
401
402 return r;
403 }
404
405 int
406 intio_intr_disestablish (vector, arg)
407 int vector;
408 void *arg;
409 {
410 if (iiv[vector].iiv_handler == 0 || iiv[vector].iiv_arg != arg)
411 return EINVAL;
412 iiv[vector].iiv_handler = 0;
413 iiv[vector].iiv_arg = 0;
414
415 return 0;
416 }
417
418 int
419 intio_intr (frame)
420 struct frame *frame;
421 {
422 int vector = frame->f_vector / 4;
423 extern int intrcnt[];
424
425 #if 0 /* this is not correct now */
426 /* CAUTION: HERE WE ARE IN SPLHIGH() */
427 /* LOWER TO APPROPRIATE IPL AT VERY FIRST IN THE HANDLER!! */
428 #endif
429 if (iiv[vector].iiv_handler == 0) {
430 printf ("Stray interrupt: %d type %x\n", vector, frame->f_format);
431 return 0;
432 }
433
434 intrcnt[iiv[vector].iiv_intrcntoff]++;
435
436 return (*(iiv[vector].iiv_handler)) (iiv[vector].iiv_arg);
437 }
438
439 /*
440 * Intio I/O controler interrupt
441 */
442 static u_int8_t intio_ivec = 0;
443
444 void
445 intio_set_ivec (vec)
446 int vec;
447 {
448 vec &= 0xfc;
449
450 if (intio_ivec && intio_ivec != (vec & 0xfc))
451 panic ("Wrong interrupt vector for Sicilian.");
452
453 intio_ivec = vec;
454 intio_set_sicilian_ivec(vec);
455 }
456
457
458 /*
459 * intio bus dma stuff. stolen from arch/i386/isa/isa_machdep.c
460 */
461
462 /*
463 * Create an INTIO DMA map.
464 */
465 int
466 _intio_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
467 bus_dma_tag_t t;
468 bus_size_t size;
469 int nsegments;
470 bus_size_t maxsegsz;
471 bus_size_t boundary;
472 int flags;
473 bus_dmamap_t *dmamp;
474 {
475 struct intio_dma_cookie *cookie;
476 bus_dmamap_t map;
477 int error, cookieflags;
478 void *cookiestore;
479 size_t cookiesize;
480 extern paddr_t avail_end;
481
482 /* Call common function to create the basic map. */
483 error = x68k_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
484 flags, dmamp);
485 if (error)
486 return (error);
487
488 map = *dmamp;
489 map->x68k_dm_cookie = NULL;
490
491 cookiesize = sizeof(struct intio_dma_cookie);
492
493 /*
494 * INTIO only has 24-bits of address space. This means
495 * we can't DMA to pages over 16M. In order to DMA to
496 * arbitrary buffers, we use "bounce buffers" - pages
497 * in memory below the 16M boundary. On DMA reads,
498 * DMA happens to the bounce buffers, and is copied into
499 * the caller's buffer. On writes, data is copied into
500 * but bounce buffer, and the DMA happens from those
501 * pages. To software using the DMA mapping interface,
502 * this looks simply like a data cache.
503 *
504 * If we have more than 16M of RAM in the system, we may
505 * need bounce buffers. We check and remember that here.
506 *
507 * ...or, there is an opposite case. The most segments
508 * a transfer will require is (maxxfer / NBPG) + 1. If
509 * the caller can't handle that many segments (e.g. the
510 * DMAC), we may have to bounce it as well.
511 */
512 if (avail_end <= t->_bounce_thresh)
513 /* Bouncing not necessary due to memory size. */
514 map->x68k_dm_bounce_thresh = 0;
515 cookieflags = 0;
516 if (map->x68k_dm_bounce_thresh != 0 ||
517 ((map->x68k_dm_size / NBPG) + 1) > map->x68k_dm_segcnt) {
518 cookieflags |= ID_MIGHT_NEED_BOUNCE;
519 cookiesize += (sizeof(bus_dma_segment_t) * map->x68k_dm_segcnt);
520 }
521
522 /*
523 * Allocate our cookie.
524 */
525 if ((cookiestore = malloc(cookiesize, M_DMAMAP,
526 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
527 error = ENOMEM;
528 goto out;
529 }
530 memset(cookiestore, 0, cookiesize);
531 cookie = (struct intio_dma_cookie *)cookiestore;
532 cookie->id_flags = cookieflags;
533 map->x68k_dm_cookie = cookie;
534
535 if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
536 /*
537 * Allocate the bounce pages now if the caller
538 * wishes us to do so.
539 */
540 if ((flags & BUS_DMA_ALLOCNOW) == 0)
541 goto out;
542
543 error = _intio_dma_alloc_bouncebuf(t, map, size, flags);
544 }
545
546 out:
547 if (error) {
548 if (map->x68k_dm_cookie != NULL)
549 free(map->x68k_dm_cookie, M_DMAMAP);
550 x68k_bus_dmamap_destroy(t, map);
551 }
552 return (error);
553 }
554
555 /*
556 * Destroy an INTIO DMA map.
557 */
558 void
559 _intio_bus_dmamap_destroy(t, map)
560 bus_dma_tag_t t;
561 bus_dmamap_t map;
562 {
563 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
564
565 /*
566 * Free any bounce pages this map might hold.
567 */
568 if (cookie->id_flags & ID_HAS_BOUNCE)
569 _intio_dma_free_bouncebuf(t, map);
570
571 free(cookie, M_DMAMAP);
572 x68k_bus_dmamap_destroy(t, map);
573 }
574
575 /*
576 * Load an INTIO DMA map with a linear buffer.
577 */
578 int
579 _intio_bus_dmamap_load(t, map, buf, buflen, p, flags)
580 bus_dma_tag_t t;
581 bus_dmamap_t map;
582 void *buf;
583 bus_size_t buflen;
584 struct proc *p;
585 int flags;
586 {
587 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
588 int error;
589
590 /*
591 * Make sure that on error condition we return "no valid mappings."
592 */
593 map->dm_mapsize = 0;
594 map->dm_nsegs = 0;
595
596 /*
597 * Try to load the map the normal way. If this errors out,
598 * and we can bounce, we will.
599 */
600 error = x68k_bus_dmamap_load(t, map, buf, buflen, p, flags);
601 if (error == 0 ||
602 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
603 return (error);
604
605 /*
606 * Allocate bounce pages, if necessary.
607 */
608 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
609 error = _intio_dma_alloc_bouncebuf(t, map, buflen, flags);
610 if (error)
611 return (error);
612 }
613
614 /*
615 * Cache a pointer to the caller's buffer and load the DMA map
616 * with the bounce buffer.
617 */
618 cookie->id_origbuf = buf;
619 cookie->id_origbuflen = buflen;
620 cookie->id_buftype = ID_BUFTYPE_LINEAR;
621 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
622 p, flags);
623 if (error) {
624 /*
625 * Free the bounce pages, unless our resources
626 * are reserved for our exclusive use.
627 */
628 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
629 _intio_dma_free_bouncebuf(t, map);
630 return (error);
631 }
632
633 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
634 cookie->id_flags |= ID_IS_BOUNCING;
635 return (0);
636 }
637
638 /*
639 * Like _intio_bus_dmamap_load(), but for mbufs.
640 */
641 int
642 _intio_bus_dmamap_load_mbuf(t, map, m0, flags)
643 bus_dma_tag_t t;
644 bus_dmamap_t map;
645 struct mbuf *m0;
646 int flags;
647 {
648 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
649 int error;
650
651 /*
652 * Make sure on error condition we return "no valid mappings."
653 */
654 map->dm_mapsize = 0;
655 map->dm_nsegs = 0;
656
657 #ifdef DIAGNOSTIC
658 if ((m0->m_flags & M_PKTHDR) == 0)
659 panic("_intio_bus_dmamap_load_mbuf: no packet header");
660 #endif
661
662 if (m0->m_pkthdr.len > map->x68k_dm_size)
663 return (EINVAL);
664
665 /*
666 * Try to load the map the normal way. If this errors out,
667 * and we can bounce, we will.
668 */
669 error = x68k_bus_dmamap_load_mbuf(t, map, m0, flags);
670 if (error == 0 ||
671 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
672 return (error);
673
674 /*
675 * Allocate bounce pages, if necessary.
676 */
677 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
678 error = _intio_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
679 flags);
680 if (error)
681 return (error);
682 }
683
684 /*
685 * Cache a pointer to the caller's buffer and load the DMA map
686 * with the bounce buffer.
687 */
688 cookie->id_origbuf = m0;
689 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
690 cookie->id_buftype = ID_BUFTYPE_MBUF;
691 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf,
692 m0->m_pkthdr.len, NULL, flags);
693 if (error) {
694 /*
695 * Free the bounce pages, unless our resources
696 * are reserved for our exclusive use.
697 */
698 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
699 _intio_dma_free_bouncebuf(t, map);
700 return (error);
701 }
702
703 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
704 cookie->id_flags |= ID_IS_BOUNCING;
705 return (0);
706 }
707
708 /*
709 * Like _intio_bus_dmamap_load(), but for uios.
710 */
711 int
712 _intio_bus_dmamap_load_uio(t, map, uio, flags)
713 bus_dma_tag_t t;
714 bus_dmamap_t map;
715 struct uio *uio;
716 int flags;
717 {
718 panic("_intio_bus_dmamap_load_uio: not implemented");
719 }
720
721 /*
722 * Like _intio_bus_dmamap_load(), but for raw memory allocated with
723 * bus_dmamem_alloc().
724 */
725 int
726 _intio_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
727 bus_dma_tag_t t;
728 bus_dmamap_t map;
729 bus_dma_segment_t *segs;
730 int nsegs;
731 bus_size_t size;
732 int flags;
733 {
734
735 panic("_intio_bus_dmamap_load_raw: not implemented");
736 }
737
738 /*
739 * Unload an INTIO DMA map.
740 */
741 void
742 _intio_bus_dmamap_unload(t, map)
743 bus_dma_tag_t t;
744 bus_dmamap_t map;
745 {
746 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
747
748 /*
749 * If we have bounce pages, free them, unless they're
750 * reserved for our exclusive use.
751 */
752 if ((cookie->id_flags & ID_HAS_BOUNCE) &&
753 (map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
754 _intio_dma_free_bouncebuf(t, map);
755
756 cookie->id_flags &= ~ID_IS_BOUNCING;
757 cookie->id_buftype = ID_BUFTYPE_INVALID;
758
759 /*
760 * Do the generic bits of the unload.
761 */
762 x68k_bus_dmamap_unload(t, map);
763 }
764
765 /*
766 * Synchronize an INTIO DMA map.
767 */
768 void
769 _intio_bus_dmamap_sync(t, map, offset, len, ops)
770 bus_dma_tag_t t;
771 bus_dmamap_t map;
772 bus_addr_t offset;
773 bus_size_t len;
774 int ops;
775 {
776 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
777
778 /*
779 * Mixing PRE and POST operations is not allowed.
780 */
781 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
782 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
783 panic("_intio_bus_dmamap_sync: mix PRE and POST");
784
785 #ifdef DIAGNOSTIC
786 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
787 if (offset >= map->dm_mapsize)
788 panic("_intio_bus_dmamap_sync: bad offset");
789 if (len == 0 || (offset + len) > map->dm_mapsize)
790 panic("_intio_bus_dmamap_sync: bad length");
791 }
792 #endif
793
794 /*
795 * If we're not bouncing, just return; nothing to do.
796 */
797 if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
798 return;
799
800 switch (cookie->id_buftype) {
801 case ID_BUFTYPE_LINEAR:
802 /*
803 * Nothing to do for pre-read.
804 */
805
806 if (ops & BUS_DMASYNC_PREWRITE) {
807 /*
808 * Copy the caller's buffer to the bounce buffer.
809 */
810 memcpy((char *)cookie->id_bouncebuf + offset,
811 (char *)cookie->id_origbuf + offset, len);
812 }
813
814 if (ops & BUS_DMASYNC_POSTREAD) {
815 /*
816 * Copy the bounce buffer to the caller's buffer.
817 */
818 memcpy((char *)cookie->id_origbuf + offset,
819 (char *)cookie->id_bouncebuf + offset, len);
820 }
821
822 /*
823 * Nothing to do for post-write.
824 */
825 break;
826
827 case ID_BUFTYPE_MBUF:
828 {
829 struct mbuf *m, *m0 = cookie->id_origbuf;
830 bus_size_t minlen, moff;
831
832 /*
833 * Nothing to do for pre-read.
834 */
835
836 if (ops & BUS_DMASYNC_PREWRITE) {
837 /*
838 * Copy the caller's buffer to the bounce buffer.
839 */
840 m_copydata(m0, offset, len,
841 (char *)cookie->id_bouncebuf + offset);
842 }
843
844 if (ops & BUS_DMASYNC_POSTREAD) {
845 /*
846 * Copy the bounce buffer to the caller's buffer.
847 */
848 for (moff = offset, m = m0; m != NULL && len != 0;
849 m = m->m_next) {
850 /* Find the beginning mbuf. */
851 if (moff >= m->m_len) {
852 moff -= m->m_len;
853 continue;
854 }
855
856 /*
857 * Now at the first mbuf to sync; nail
858 * each one until we have exhausted the
859 * length.
860 */
861 minlen = len < m->m_len - moff ?
862 len : m->m_len - moff;
863
864 memcpy(mtod(m, caddr_t) + moff,
865 (char *)cookie->id_bouncebuf + offset,
866 minlen);
867
868 moff = 0;
869 len -= minlen;
870 offset += minlen;
871 }
872 }
873
874 /*
875 * Nothing to do for post-write.
876 */
877 break;
878 }
879
880 case ID_BUFTYPE_UIO:
881 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_UIO");
882 break;
883
884 case ID_BUFTYPE_RAW:
885 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_RAW");
886 break;
887
888 case ID_BUFTYPE_INVALID:
889 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_INVALID");
890 break;
891
892 default:
893 printf("unknown buffer type %d\n", cookie->id_buftype);
894 panic("_intio_bus_dmamap_sync");
895 }
896 }
897
898 /*
899 * Allocate memory safe for INTIO DMA.
900 */
901 int
902 _intio_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
903 bus_dma_tag_t t;
904 bus_size_t size, alignment, boundary;
905 bus_dma_segment_t *segs;
906 int nsegs;
907 int *rsegs;
908 int flags;
909 {
910 paddr_t high;
911 extern paddr_t avail_end;
912
913 if (avail_end > INTIO_DMA_BOUNCE_THRESHOLD)
914 high = trunc_page(INTIO_DMA_BOUNCE_THRESHOLD);
915 else
916 high = trunc_page(avail_end);
917
918 return (x68k_bus_dmamem_alloc_range(t, size, alignment, boundary,
919 segs, nsegs, rsegs, flags, 0, high));
920 }
921
922 /**********************************************************************
923 * INTIO DMA utility functions
924 **********************************************************************/
925
926 int
927 _intio_dma_alloc_bouncebuf(t, map, size, flags)
928 bus_dma_tag_t t;
929 bus_dmamap_t map;
930 bus_size_t size;
931 int flags;
932 {
933 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
934 int error = 0;
935
936 cookie->id_bouncebuflen = round_page(size);
937 error = _intio_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
938 NBPG, map->x68k_dm_boundary, cookie->id_bouncesegs,
939 map->x68k_dm_segcnt, &cookie->id_nbouncesegs, flags);
940 if (error)
941 goto out;
942 error = x68k_bus_dmamem_map(t, cookie->id_bouncesegs,
943 cookie->id_nbouncesegs, cookie->id_bouncebuflen,
944 (caddr_t *)&cookie->id_bouncebuf, flags);
945
946 out:
947 if (error) {
948 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
949 cookie->id_nbouncesegs);
950 cookie->id_bouncebuflen = 0;
951 cookie->id_nbouncesegs = 0;
952 } else {
953 cookie->id_flags |= ID_HAS_BOUNCE;
954 }
955
956 return (error);
957 }
958
959 void
960 _intio_dma_free_bouncebuf(t, map)
961 bus_dma_tag_t t;
962 bus_dmamap_t map;
963 {
964 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
965
966 x68k_bus_dmamem_unmap(t, cookie->id_bouncebuf,
967 cookie->id_bouncebuflen);
968 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
969 cookie->id_nbouncesegs);
970 cookie->id_bouncebuflen = 0;
971 cookie->id_nbouncesegs = 0;
972 cookie->id_flags &= ~ID_HAS_BOUNCE;
973 }
974