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