intio.c revision 1.6 1 /* $NetBSD: intio.c,v 1.6 1999/05/05 13:41:44 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 0x30, /* ia_size */
176 MFP_INTR, /* ia_intr */
177 -1 /* ia_dma */
178 -1, /* ia_dmaintr */
179 };
180
181 static void
182 intio_attach(parent, self, aux)
183 struct device *parent, *self;
184 void *aux; /* NULL */
185 {
186 struct intio_softc *sc = (struct intio_softc *)self;
187 struct intio_attach_args ia;
188
189 if (self == NULL) {
190 /* console only init */
191 x68k_config_found(cfdata_intiobus, NULL, &initial_ia, NULL);
192 return;
193 }
194
195 printf (" mapped at %8p\n", intiobase);
196
197 sc->sc_map = extent_create("intiomap",
198 PHYS_INTIODEV,
199 PHYS_INTIODEV + 0x400000,
200 M_DEVBUF, NULL, NULL, EX_NOWAIT);
201 intio_alloc_system_ports (sc);
202
203 sc->sc_bst = &intio_bus;
204 sc->sc_bst->x68k_bus_device = self;
205 sc->sc_dmat = &intio_bus_dma;
206 sc->sc_dmac = 0;
207
208 bzero(iiv, sizeof (struct intio_interrupt_vector) * 256);
209
210 ia.ia_bst = sc->sc_bst;
211 ia.ia_dmat = sc->sc_dmat;
212
213 config_search (intio_search, self, &ia);
214 }
215
216 static int
217 intio_search(parent, cf, aux)
218 struct device *parent;
219 struct cfdata *cf;
220 void *aux;
221 {
222 struct intio_attach_args *ia = aux;
223 struct intio_softc *sc = (struct intio_softc *)parent;
224
225 ia->ia_bst = sc->sc_bst;
226 ia->ia_dmat = sc->sc_dmat;
227 ia->ia_name = cf->cf_driver->cd_name;
228 ia->ia_addr = cf->cf_addr;
229 ia->ia_intr = cf->cf_intr;
230 ia->ia_dma = cf->cf_dma;
231 ia->ia_dmaintr = cf->cf_dmaintr;
232
233 if ((*cf->cf_attach->ca_match)(parent, cf, ia) > 0)
234 config_attach(parent, cf, ia, intio_print);
235
236 return (0);
237 }
238
239 static int
240 intio_print(aux, name)
241 void *aux;
242 const char *name;
243 {
244 struct intio_attach_args *ia = aux;
245
246 /* if (ia->ia_addr > 0) */
247 printf (" addr 0x%06x", ia->ia_addr);
248 if (ia->ia_intr > 0)
249 printf (" intr 0x%02x", ia->ia_intr);
250 if (ia->ia_dma >= 0) {
251 printf (" using DMA ch%d", ia->ia_dma);
252 if (ia->ia_dmaintr > 0)
253 printf (" intr 0x%02x and 0x%02x",
254 ia->ia_dmaintr, ia->ia_dmaintr+1);
255 }
256
257 return (QUIET);
258 }
259
260 /*
261 * intio memory map manager
262 */
263
264 int
265 intio_map_allocate_region(parent, ia, flag)
266 struct device *parent;
267 struct intio_attach_args *ia;
268 enum intio_map_flag flag; /* INTIO_MAP_TESTONLY or INTIO_MAP_ALLOCATE */
269 {
270 struct intio_softc *sc = (struct intio_softc*) parent;
271 struct extent *map = sc->sc_map;
272 int r;
273
274 r = extent_alloc_region (map, ia->ia_addr, ia->ia_size, 0);
275 #ifdef DEBUG
276 if (intio_debug)
277 extent_print (map);
278 #endif
279 if (r == 0) {
280 if (flag != INTIO_MAP_ALLOCATE)
281 extent_free (map, ia->ia_addr, ia->ia_size, 0);
282 return 0;
283 }
284
285 return -1;
286 }
287
288 int
289 intio_map_free_region(parent, ia)
290 struct device *parent;
291 struct intio_attach_args *ia;
292 {
293 struct intio_softc *sc = (struct intio_softc*) parent;
294 struct extent *map = sc->sc_map;
295
296 extent_free (map, ia->ia_addr, ia->ia_size, 0);
297 #ifdef DEBUG
298 if (intio_debug)
299 extent_print (map);
300 #endif
301 return 0;
302 }
303
304 void
305 intio_alloc_system_ports(sc)
306 struct intio_softc *sc;
307 {
308 extent_alloc_region (sc->sc_map, INTIO_SYSPORT, 16, 0);
309 extent_alloc_region (sc->sc_map, INTIO_SICILIAN, 0x2000, 0);
310 }
311
312
313 /*
314 * intio bus space stuff.
315 */
316 static int
317 intio_bus_space_map(t, bpa, size, flags, bshp)
318 bus_space_tag_t t;
319 bus_addr_t bpa;
320 bus_size_t size;
321 int flags;
322 bus_space_handle_t *bshp;
323 {
324 /*
325 * Intio bus is mapped permanently.
326 */
327 *bshp = (bus_space_handle_t)
328 ((u_int) bpa - PHYS_INTIODEV + intiobase);
329 /*
330 * Some devices are mapped on odd addresses only.
331 */
332 if (flags & BUS_SPACE_MAP_SHIFTED)
333 *bshp += 0x80000001;
334
335 return (0);
336 }
337
338 static void
339 intio_bus_space_unmap(t, bsh, size)
340 bus_space_tag_t t;
341 bus_space_handle_t bsh;
342 bus_size_t size;
343 {
344 return;
345 }
346
347 static int
348 intio_bus_space_subregion(t, bsh, offset, size, nbshp)
349 bus_space_tag_t t;
350 bus_space_handle_t bsh;
351 bus_size_t offset, size;
352 bus_space_handle_t *nbshp;
353 {
354
355 *nbshp = bsh + offset;
356 return (0);
357 }
358
359
360 /*
361 * interrupt handler
362 */
363 int
364 intio_intr_establish (vector, name, handler, arg)
365 int vector;
366 const char *name; /* XXX */
367 intio_intr_handler_t handler;
368 void *arg;
369 {
370 if (vector < 16)
371 panic ("Invalid interrupt vector");
372 if (iiv[vector].iiv_handler)
373 return EBUSY;
374 iiv[vector].iiv_handler = handler;
375 iiv[vector].iiv_arg = arg;
376 iiv[vector].iiv_intrcntoff = scan_intrnames(name);
377
378 return 0;
379 }
380
381 static int
382 scan_intrnames (name)
383 const char *name;
384 {
385 extern char intrnames[];
386 extern char eintrnames[];
387 int r = 0;
388 char *p = &intrnames[0];
389
390 for (;;) {
391 if (*p == 0) { /* new intr */
392 if (p + strlen(name) >= eintrnames)
393 panic ("Interrupt statics buffer overrun.");
394 strcpy (p, name);
395 break;
396 }
397 if (strcmp(p, name) == 0)
398 break;
399 r++;
400 while (*p++ != 0);
401 }
402
403 return r;
404 }
405
406 int
407 intio_intr_disestablish (vector, arg)
408 int vector;
409 void *arg;
410 {
411 if (iiv[vector].iiv_handler == 0 || iiv[vector].iiv_arg != arg)
412 return EINVAL;
413 iiv[vector].iiv_handler = 0;
414 iiv[vector].iiv_arg = 0;
415
416 return 0;
417 }
418
419 int
420 intio_intr (frame)
421 struct frame *frame;
422 {
423 int vector = frame->f_vector / 4;
424 extern int intrcnt[];
425
426 #if 0 /* this is not correct now */
427 /* CAUTION: HERE WE ARE IN SPLHIGH() */
428 /* LOWER TO APPROPRIATE IPL AT VERY FIRST IN THE HANDLER!! */
429 #endif
430 if (iiv[vector].iiv_handler == 0) {
431 printf ("Stray interrupt: %d type %x\n", vector, frame->f_format);
432 return 0;
433 }
434
435 intrcnt[iiv[vector].iiv_intrcntoff]++;
436
437 return (*(iiv[vector].iiv_handler)) (iiv[vector].iiv_arg);
438 }
439
440 /*
441 * Intio I/O controler interrupt
442 */
443 static u_int8_t intio_ivec = 0;
444
445 void
446 intio_set_ivec (vec)
447 int vec;
448 {
449 vec &= 0xfc;
450
451 if (intio_ivec && intio_ivec != (vec & 0xfc))
452 panic ("Wrong interrupt vector for Sicilian.");
453
454 intio_ivec = vec;
455 intio_set_sicilian_ivec(vec);
456 }
457
458
459 /*
460 * intio bus dma stuff. stolen from arch/i386/isa/isa_machdep.c
461 */
462
463 /*
464 * Create an INTIO DMA map.
465 */
466 int
467 _intio_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
468 bus_dma_tag_t t;
469 bus_size_t size;
470 int nsegments;
471 bus_size_t maxsegsz;
472 bus_size_t boundary;
473 int flags;
474 bus_dmamap_t *dmamp;
475 {
476 struct intio_dma_cookie *cookie;
477 bus_dmamap_t map;
478 int error, cookieflags;
479 void *cookiestore;
480 size_t cookiesize;
481 extern paddr_t avail_end;
482
483 /* Call common function to create the basic map. */
484 error = x68k_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
485 flags, dmamp);
486 if (error)
487 return (error);
488
489 map = *dmamp;
490 map->x68k_dm_cookie = NULL;
491
492 cookiesize = sizeof(struct intio_dma_cookie);
493
494 /*
495 * INTIO only has 24-bits of address space. This means
496 * we can't DMA to pages over 16M. In order to DMA to
497 * arbitrary buffers, we use "bounce buffers" - pages
498 * in memory below the 16M boundary. On DMA reads,
499 * DMA happens to the bounce buffers, and is copied into
500 * the caller's buffer. On writes, data is copied into
501 * but bounce buffer, and the DMA happens from those
502 * pages. To software using the DMA mapping interface,
503 * this looks simply like a data cache.
504 *
505 * If we have more than 16M of RAM in the system, we may
506 * need bounce buffers. We check and remember that here.
507 *
508 * ...or, there is an opposite case. The most segments
509 * a transfer will require is (maxxfer / NBPG) + 1. If
510 * the caller can't handle that many segments (e.g. the
511 * DMAC), we may have to bounce it as well.
512 */
513 if (avail_end <= t->_bounce_thresh)
514 /* Bouncing not necessary due to memory size. */
515 map->x68k_dm_bounce_thresh = 0;
516 cookieflags = 0;
517 if (map->x68k_dm_bounce_thresh != 0 ||
518 ((map->x68k_dm_size / NBPG) + 1) > map->x68k_dm_segcnt) {
519 cookieflags |= ID_MIGHT_NEED_BOUNCE;
520 cookiesize += (sizeof(bus_dma_segment_t) * map->x68k_dm_segcnt);
521 }
522
523 /*
524 * Allocate our cookie.
525 */
526 if ((cookiestore = malloc(cookiesize, M_DMAMAP,
527 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
528 error = ENOMEM;
529 goto out;
530 }
531 memset(cookiestore, 0, cookiesize);
532 cookie = (struct intio_dma_cookie *)cookiestore;
533 cookie->id_flags = cookieflags;
534 map->x68k_dm_cookie = cookie;
535
536 if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
537 /*
538 * Allocate the bounce pages now if the caller
539 * wishes us to do so.
540 */
541 if ((flags & BUS_DMA_ALLOCNOW) == 0)
542 goto out;
543
544 error = _intio_dma_alloc_bouncebuf(t, map, size, flags);
545 }
546
547 out:
548 if (error) {
549 if (map->x68k_dm_cookie != NULL)
550 free(map->x68k_dm_cookie, M_DMAMAP);
551 x68k_bus_dmamap_destroy(t, map);
552 }
553 return (error);
554 }
555
556 /*
557 * Destroy an INTIO DMA map.
558 */
559 void
560 _intio_bus_dmamap_destroy(t, map)
561 bus_dma_tag_t t;
562 bus_dmamap_t map;
563 {
564 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
565
566 /*
567 * Free any bounce pages this map might hold.
568 */
569 if (cookie->id_flags & ID_HAS_BOUNCE)
570 _intio_dma_free_bouncebuf(t, map);
571
572 free(cookie, M_DMAMAP);
573 x68k_bus_dmamap_destroy(t, map);
574 }
575
576 /*
577 * Load an INTIO DMA map with a linear buffer.
578 */
579 int
580 _intio_bus_dmamap_load(t, map, buf, buflen, p, flags)
581 bus_dma_tag_t t;
582 bus_dmamap_t map;
583 void *buf;
584 bus_size_t buflen;
585 struct proc *p;
586 int flags;
587 {
588 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
589 int error;
590
591 /*
592 * Make sure that on error condition we return "no valid mappings."
593 */
594 map->dm_mapsize = 0;
595 map->dm_nsegs = 0;
596
597 /*
598 * Try to load the map the normal way. If this errors out,
599 * and we can bounce, we will.
600 */
601 error = x68k_bus_dmamap_load(t, map, buf, buflen, p, flags);
602 if (error == 0 ||
603 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
604 return (error);
605
606 /*
607 * Allocate bounce pages, if necessary.
608 */
609 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
610 error = _intio_dma_alloc_bouncebuf(t, map, buflen, flags);
611 if (error)
612 return (error);
613 }
614
615 /*
616 * Cache a pointer to the caller's buffer and load the DMA map
617 * with the bounce buffer.
618 */
619 cookie->id_origbuf = buf;
620 cookie->id_origbuflen = buflen;
621 cookie->id_buftype = ID_BUFTYPE_LINEAR;
622 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
623 p, flags);
624 if (error) {
625 /*
626 * Free the bounce pages, unless our resources
627 * are reserved for our exclusive use.
628 */
629 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
630 _intio_dma_free_bouncebuf(t, map);
631 return (error);
632 }
633
634 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
635 cookie->id_flags |= ID_IS_BOUNCING;
636 return (0);
637 }
638
639 /*
640 * Like _intio_bus_dmamap_load(), but for mbufs.
641 */
642 int
643 _intio_bus_dmamap_load_mbuf(t, map, m0, flags)
644 bus_dma_tag_t t;
645 bus_dmamap_t map;
646 struct mbuf *m0;
647 int flags;
648 {
649 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
650 int error;
651
652 /*
653 * Make sure on error condition we return "no valid mappings."
654 */
655 map->dm_mapsize = 0;
656 map->dm_nsegs = 0;
657
658 #ifdef DIAGNOSTIC
659 if ((m0->m_flags & M_PKTHDR) == 0)
660 panic("_intio_bus_dmamap_load_mbuf: no packet header");
661 #endif
662
663 if (m0->m_pkthdr.len > map->x68k_dm_size)
664 return (EINVAL);
665
666 /*
667 * Try to load the map the normal way. If this errors out,
668 * and we can bounce, we will.
669 */
670 error = x68k_bus_dmamap_load_mbuf(t, map, m0, flags);
671 if (error == 0 ||
672 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
673 return (error);
674
675 /*
676 * Allocate bounce pages, if necessary.
677 */
678 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
679 error = _intio_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
680 flags);
681 if (error)
682 return (error);
683 }
684
685 /*
686 * Cache a pointer to the caller's buffer and load the DMA map
687 * with the bounce buffer.
688 */
689 cookie->id_origbuf = m0;
690 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
691 cookie->id_buftype = ID_BUFTYPE_MBUF;
692 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf,
693 m0->m_pkthdr.len, NULL, flags);
694 if (error) {
695 /*
696 * Free the bounce pages, unless our resources
697 * are reserved for our exclusive use.
698 */
699 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
700 _intio_dma_free_bouncebuf(t, map);
701 return (error);
702 }
703
704 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
705 cookie->id_flags |= ID_IS_BOUNCING;
706 return (0);
707 }
708
709 /*
710 * Like _intio_bus_dmamap_load(), but for uios.
711 */
712 int
713 _intio_bus_dmamap_load_uio(t, map, uio, flags)
714 bus_dma_tag_t t;
715 bus_dmamap_t map;
716 struct uio *uio;
717 int flags;
718 {
719 panic("_intio_bus_dmamap_load_uio: not implemented");
720 }
721
722 /*
723 * Like _intio_bus_dmamap_load(), but for raw memory allocated with
724 * bus_dmamem_alloc().
725 */
726 int
727 _intio_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
728 bus_dma_tag_t t;
729 bus_dmamap_t map;
730 bus_dma_segment_t *segs;
731 int nsegs;
732 bus_size_t size;
733 int flags;
734 {
735
736 panic("_intio_bus_dmamap_load_raw: not implemented");
737 }
738
739 /*
740 * Unload an INTIO DMA map.
741 */
742 void
743 _intio_bus_dmamap_unload(t, map)
744 bus_dma_tag_t t;
745 bus_dmamap_t map;
746 {
747 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
748
749 /*
750 * If we have bounce pages, free them, unless they're
751 * reserved for our exclusive use.
752 */
753 if ((cookie->id_flags & ID_HAS_BOUNCE) &&
754 (map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
755 _intio_dma_free_bouncebuf(t, map);
756
757 cookie->id_flags &= ~ID_IS_BOUNCING;
758 cookie->id_buftype = ID_BUFTYPE_INVALID;
759
760 /*
761 * Do the generic bits of the unload.
762 */
763 x68k_bus_dmamap_unload(t, map);
764 }
765
766 /*
767 * Synchronize an INTIO DMA map.
768 */
769 void
770 _intio_bus_dmamap_sync(t, map, offset, len, ops)
771 bus_dma_tag_t t;
772 bus_dmamap_t map;
773 bus_addr_t offset;
774 bus_size_t len;
775 int ops;
776 {
777 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
778
779 /*
780 * Mixing PRE and POST operations is not allowed.
781 */
782 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
783 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
784 panic("_intio_bus_dmamap_sync: mix PRE and POST");
785
786 #ifdef DIAGNOSTIC
787 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
788 if (offset >= map->dm_mapsize)
789 panic("_intio_bus_dmamap_sync: bad offset");
790 if (len == 0 || (offset + len) > map->dm_mapsize)
791 panic("_intio_bus_dmamap_sync: bad length");
792 }
793 #endif
794
795 /*
796 * If we're not bouncing, just return; nothing to do.
797 */
798 if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
799 return;
800
801 switch (cookie->id_buftype) {
802 case ID_BUFTYPE_LINEAR:
803 /*
804 * Nothing to do for pre-read.
805 */
806
807 if (ops & BUS_DMASYNC_PREWRITE) {
808 /*
809 * Copy the caller's buffer to the bounce buffer.
810 */
811 memcpy((char *)cookie->id_bouncebuf + offset,
812 (char *)cookie->id_origbuf + offset, len);
813 }
814
815 if (ops & BUS_DMASYNC_POSTREAD) {
816 /*
817 * Copy the bounce buffer to the caller's buffer.
818 */
819 memcpy((char *)cookie->id_origbuf + offset,
820 (char *)cookie->id_bouncebuf + offset, len);
821 }
822
823 /*
824 * Nothing to do for post-write.
825 */
826 break;
827
828 case ID_BUFTYPE_MBUF:
829 {
830 struct mbuf *m, *m0 = cookie->id_origbuf;
831 bus_size_t minlen, moff;
832
833 /*
834 * Nothing to do for pre-read.
835 */
836
837 if (ops & BUS_DMASYNC_PREWRITE) {
838 /*
839 * Copy the caller's buffer to the bounce buffer.
840 */
841 m_copydata(m0, offset, len,
842 (char *)cookie->id_bouncebuf + offset);
843 }
844
845 if (ops & BUS_DMASYNC_POSTREAD) {
846 /*
847 * Copy the bounce buffer to the caller's buffer.
848 */
849 for (moff = offset, m = m0; m != NULL && len != 0;
850 m = m->m_next) {
851 /* Find the beginning mbuf. */
852 if (moff >= m->m_len) {
853 moff -= m->m_len;
854 continue;
855 }
856
857 /*
858 * Now at the first mbuf to sync; nail
859 * each one until we have exhausted the
860 * length.
861 */
862 minlen = len < m->m_len - moff ?
863 len : m->m_len - moff;
864
865 memcpy(mtod(m, caddr_t) + moff,
866 (char *)cookie->id_bouncebuf + offset,
867 minlen);
868
869 moff = 0;
870 len -= minlen;
871 offset += minlen;
872 }
873 }
874
875 /*
876 * Nothing to do for post-write.
877 */
878 break;
879 }
880
881 case ID_BUFTYPE_UIO:
882 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_UIO");
883 break;
884
885 case ID_BUFTYPE_RAW:
886 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_RAW");
887 break;
888
889 case ID_BUFTYPE_INVALID:
890 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_INVALID");
891 break;
892
893 default:
894 printf("unknown buffer type %d\n", cookie->id_buftype);
895 panic("_intio_bus_dmamap_sync");
896 }
897 }
898
899 /*
900 * Allocate memory safe for INTIO DMA.
901 */
902 int
903 _intio_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
904 bus_dma_tag_t t;
905 bus_size_t size, alignment, boundary;
906 bus_dma_segment_t *segs;
907 int nsegs;
908 int *rsegs;
909 int flags;
910 {
911 paddr_t high;
912 extern paddr_t avail_end;
913
914 if (avail_end > INTIO_DMA_BOUNCE_THRESHOLD)
915 high = trunc_page(INTIO_DMA_BOUNCE_THRESHOLD);
916 else
917 high = trunc_page(avail_end);
918
919 return (x68k_bus_dmamem_alloc_range(t, size, alignment, boundary,
920 segs, nsegs, rsegs, flags, 0, high));
921 }
922
923 /**********************************************************************
924 * INTIO DMA utility functions
925 **********************************************************************/
926
927 int
928 _intio_dma_alloc_bouncebuf(t, map, size, flags)
929 bus_dma_tag_t t;
930 bus_dmamap_t map;
931 bus_size_t size;
932 int flags;
933 {
934 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
935 int error = 0;
936
937 cookie->id_bouncebuflen = round_page(size);
938 error = _intio_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
939 NBPG, map->x68k_dm_boundary, cookie->id_bouncesegs,
940 map->x68k_dm_segcnt, &cookie->id_nbouncesegs, flags);
941 if (error)
942 goto out;
943 error = x68k_bus_dmamem_map(t, cookie->id_bouncesegs,
944 cookie->id_nbouncesegs, cookie->id_bouncebuflen,
945 (caddr_t *)&cookie->id_bouncebuf, flags);
946
947 out:
948 if (error) {
949 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
950 cookie->id_nbouncesegs);
951 cookie->id_bouncebuflen = 0;
952 cookie->id_nbouncesegs = 0;
953 } else {
954 cookie->id_flags |= ID_HAS_BOUNCE;
955 }
956
957 return (error);
958 }
959
960 void
961 _intio_dma_free_bouncebuf(t, map)
962 bus_dma_tag_t t;
963 bus_dmamap_t map;
964 {
965 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
966
967 x68k_bus_dmamem_unmap(t, cookie->id_bouncebuf,
968 cookie->id_bouncebuflen);
969 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
970 cookie->id_nbouncesegs);
971 cookie->id_bouncebuflen = 0;
972 cookie->id_nbouncesegs = 0;
973 cookie->id_flags &= ~ID_HAS_BOUNCE;
974 }
975