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