intio.c revision 1.1.2.5 1 /* $NetBSD: intio.c,v 1.1.2.5 1999/02/02 23:44: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 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 return (0);
337 }
338
339 static void
340 intio_bus_space_unmap(t, bsh, size)
341 bus_space_tag_t t;
342 bus_space_handle_t bsh;
343 bus_size_t size;
344 {
345 return;
346 }
347
348 static int
349 intio_bus_space_subregion(t, bsh, offset, size, nbshp)
350 bus_space_tag_t t;
351 bus_space_handle_t bsh;
352 bus_size_t offset, size;
353 bus_space_handle_t *nbshp;
354 {
355
356 *nbshp = bsh + offset;
357 return (0);
358 }
359
360
361 /*
362 * interrupt handler
363 */
364 int
365 intio_intr_establish (vector, name, handler, arg)
366 int vector;
367 const char *name; /* XXX */
368 intio_intr_handler_t handler;
369 void *arg;
370 {
371 if (vector < 16)
372 panic ("Invalid interrupt vector");
373 if (iiv[vector].iiv_handler)
374 return EBUSY;
375 iiv[vector].iiv_handler = handler;
376 iiv[vector].iiv_arg = arg;
377
378 return 0;
379 }
380
381 int
382 intio_intr_disestablish (vector, arg)
383 int vector;
384 void *arg;
385 {
386 if (iiv[vector].iiv_handler == 0 || iiv[vector].iiv_arg != arg)
387 return EINVAL;
388 iiv[vector].iiv_handler = 0;
389 iiv[vector].iiv_arg = 0;
390
391 return 0;
392 }
393
394 int
395 intio_intr (frame)
396 struct frame *frame;
397 {
398 int vector = frame->f_vector / 4;
399
400 /* CAUTION: HERE WE ARE IN SPLHIGH() */
401 /* LOWER TO APPROPRIATE IPL AT VERY FIRST IN THE HANDLER!! */
402
403 if (iiv[vector].iiv_handler == 0) {
404 printf ("Stray interrupt: %d type %x\n", vector, frame->f_format);
405 return 0;
406 }
407
408 /* TODO: update the interrupt statics. */
409
410 return (*(iiv[vector].iiv_handler)) (iiv[vector].iiv_arg);
411 }
412
413 /*
414 * Intio I/O controler interrupt
415 */
416 static intio_ivec = 0;
417 void
418 intio_set_ivec (vec)
419 int vec;
420 {
421 vec &= 0xfc;
422
423 if (intio_ivec && intio_ivec != (vec & 0xfc))
424 panic ("Wrong interrupt vector for Sicilian.");
425
426 intio_ivec = vec;
427 intio_set_sicilian_ivec(vec);
428 }
429
430
431 /*
432 * intio bus dma stuff. stolen from arch/i386/isa/isa_machdep.c
433 */
434
435 /*
436 * Create an INTIO DMA map.
437 */
438 int
439 _intio_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
440 bus_dma_tag_t t;
441 bus_size_t size;
442 int nsegments;
443 bus_size_t maxsegsz;
444 bus_size_t boundary;
445 int flags;
446 bus_dmamap_t *dmamp;
447 {
448 struct intio_dma_cookie *cookie;
449 bus_dmamap_t map;
450 int error, cookieflags;
451 void *cookiestore;
452 size_t cookiesize;
453 extern paddr_t avail_end;
454
455 /* Call common function to create the basic map. */
456 error = x68k_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
457 flags, dmamp);
458 if (error)
459 return (error);
460
461 map = *dmamp;
462 map->x68k_dm_cookie = NULL;
463
464 cookiesize = sizeof(struct intio_dma_cookie);
465
466 /*
467 * INTIO only has 24-bits of address space. This means
468 * we can't DMA to pages over 16M. In order to DMA to
469 * arbitrary buffers, we use "bounce buffers" - pages
470 * in memory below the 16M boundary. On DMA reads,
471 * DMA happens to the bounce buffers, and is copied into
472 * the caller's buffer. On writes, data is copied into
473 * but bounce buffer, and the DMA happens from those
474 * pages. To software using the DMA mapping interface,
475 * this looks simply like a data cache.
476 *
477 * If we have more than 16M of RAM in the system, we may
478 * need bounce buffers. We check and remember that here.
479 *
480 * ...or, there is an opposite case. The most segments
481 * a transfer will require is (maxxfer / NBPG) + 1. If
482 * the caller can't handle that many segments (e.g. the
483 * DMAC), we may have to bounce it as well.
484 */
485 if (avail_end <= t->_bounce_thresh)
486 /* Bouncing not necessary due to memory size. */
487 map->x68k_dm_bounce_thresh = 0;
488 cookieflags = 0;
489 if (map->x68k_dm_bounce_thresh != 0 ||
490 ((map->x68k_dm_size / NBPG) + 1) > map->x68k_dm_segcnt) {
491 cookieflags |= ID_MIGHT_NEED_BOUNCE;
492 cookiesize += (sizeof(bus_dma_segment_t) * map->x68k_dm_segcnt);
493 }
494
495 /*
496 * Allocate our cookie.
497 */
498 if ((cookiestore = malloc(cookiesize, M_DMAMAP,
499 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
500 error = ENOMEM;
501 goto out;
502 }
503 memset(cookiestore, 0, cookiesize);
504 cookie = (struct intio_dma_cookie *)cookiestore;
505 cookie->id_flags = cookieflags;
506 map->x68k_dm_cookie = cookie;
507
508 if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
509 /*
510 * Allocate the bounce pages now if the caller
511 * wishes us to do so.
512 */
513 if ((flags & BUS_DMA_ALLOCNOW) == 0)
514 goto out;
515
516 error = _intio_dma_alloc_bouncebuf(t, map, size, flags);
517 }
518
519 out:
520 if (error) {
521 if (map->x68k_dm_cookie != NULL)
522 free(map->x68k_dm_cookie, M_DMAMAP);
523 x68k_bus_dmamap_destroy(t, map);
524 }
525 return (error);
526 }
527
528 /*
529 * Destroy an INTIO DMA map.
530 */
531 void
532 _intio_bus_dmamap_destroy(t, map)
533 bus_dma_tag_t t;
534 bus_dmamap_t map;
535 {
536 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
537
538 /*
539 * Free any bounce pages this map might hold.
540 */
541 if (cookie->id_flags & ID_HAS_BOUNCE)
542 _intio_dma_free_bouncebuf(t, map);
543
544 free(cookie, M_DMAMAP);
545 x68k_bus_dmamap_destroy(t, map);
546 }
547
548 /*
549 * Load an INTIO DMA map with a linear buffer.
550 */
551 int
552 _intio_bus_dmamap_load(t, map, buf, buflen, p, flags)
553 bus_dma_tag_t t;
554 bus_dmamap_t map;
555 void *buf;
556 bus_size_t buflen;
557 struct proc *p;
558 int flags;
559 {
560 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
561 int error;
562
563 /*
564 * Make sure that on error condition we return "no valid mappings."
565 */
566 map->dm_mapsize = 0;
567 map->dm_nsegs = 0;
568
569 /*
570 * Try to load the map the normal way. If this errors out,
571 * and we can bounce, we will.
572 */
573 error = x68k_bus_dmamap_load(t, map, buf, buflen, p, flags);
574 if (error == 0 ||
575 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
576 return (error);
577
578 /*
579 * Allocate bounce pages, if necessary.
580 */
581 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
582 error = _intio_dma_alloc_bouncebuf(t, map, buflen, flags);
583 if (error)
584 return (error);
585 }
586
587 /*
588 * Cache a pointer to the caller's buffer and load the DMA map
589 * with the bounce buffer.
590 */
591 cookie->id_origbuf = buf;
592 cookie->id_origbuflen = buflen;
593 cookie->id_buftype = ID_BUFTYPE_LINEAR;
594 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
595 p, flags);
596 if (error) {
597 /*
598 * Free the bounce pages, unless our resources
599 * are reserved for our exclusive use.
600 */
601 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
602 _intio_dma_free_bouncebuf(t, map);
603 return (error);
604 }
605
606 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
607 cookie->id_flags |= ID_IS_BOUNCING;
608 return (0);
609 }
610
611 /*
612 * Like _intio_bus_dmamap_load(), but for mbufs.
613 */
614 int
615 _intio_bus_dmamap_load_mbuf(t, map, m0, flags)
616 bus_dma_tag_t t;
617 bus_dmamap_t map;
618 struct mbuf *m0;
619 int flags;
620 {
621 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
622 int error;
623
624 /*
625 * Make sure on error condition we return "no valid mappings."
626 */
627 map->dm_mapsize = 0;
628 map->dm_nsegs = 0;
629
630 #ifdef DIAGNOSTIC
631 if ((m0->m_flags & M_PKTHDR) == 0)
632 panic("_intio_bus_dmamap_load_mbuf: no packet header");
633 #endif
634
635 if (m0->m_pkthdr.len > map->x68k_dm_size)
636 return (EINVAL);
637
638 /*
639 * Try to load the map the normal way. If this errors out,
640 * and we can bounce, we will.
641 */
642 error = x68k_bus_dmamap_load_mbuf(t, map, m0, flags);
643 if (error == 0 ||
644 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
645 return (error);
646
647 /*
648 * Allocate bounce pages, if necessary.
649 */
650 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
651 error = _intio_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
652 flags);
653 if (error)
654 return (error);
655 }
656
657 /*
658 * Cache a pointer to the caller's buffer and load the DMA map
659 * with the bounce buffer.
660 */
661 cookie->id_origbuf = m0;
662 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
663 cookie->id_buftype = ID_BUFTYPE_MBUF;
664 error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf,
665 m0->m_pkthdr.len, NULL, flags);
666 if (error) {
667 /*
668 * Free the bounce pages, unless our resources
669 * are reserved for our exclusive use.
670 */
671 if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
672 _intio_dma_free_bouncebuf(t, map);
673 return (error);
674 }
675
676 /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
677 cookie->id_flags |= ID_IS_BOUNCING;
678 return (0);
679 }
680
681 /*
682 * Like _intio_bus_dmamap_load(), but for uios.
683 */
684 int
685 _intio_bus_dmamap_load_uio(t, map, uio, flags)
686 bus_dma_tag_t t;
687 bus_dmamap_t map;
688 struct uio *uio;
689 int flags;
690 {
691 panic("_intio_bus_dmamap_load_uio: not implemented");
692 }
693
694 /*
695 * Like _intio_bus_dmamap_load(), but for raw memory allocated with
696 * bus_dmamem_alloc().
697 */
698 int
699 _intio_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
700 bus_dma_tag_t t;
701 bus_dmamap_t map;
702 bus_dma_segment_t *segs;
703 int nsegs;
704 bus_size_t size;
705 int flags;
706 {
707
708 panic("_intio_bus_dmamap_load_raw: not implemented");
709 }
710
711 /*
712 * Unload an INTIO DMA map.
713 */
714 void
715 _intio_bus_dmamap_unload(t, map)
716 bus_dma_tag_t t;
717 bus_dmamap_t map;
718 {
719 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
720
721 /*
722 * If we have bounce pages, free them, unless they're
723 * reserved for our exclusive use.
724 */
725 if ((cookie->id_flags & ID_HAS_BOUNCE) &&
726 (map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
727 _intio_dma_free_bouncebuf(t, map);
728
729 cookie->id_flags &= ~ID_IS_BOUNCING;
730 cookie->id_buftype = ID_BUFTYPE_INVALID;
731
732 /*
733 * Do the generic bits of the unload.
734 */
735 x68k_bus_dmamap_unload(t, map);
736 }
737
738 /*
739 * Synchronize an INTIO DMA map.
740 */
741 void
742 _intio_bus_dmamap_sync(t, map, offset, len, ops)
743 bus_dma_tag_t t;
744 bus_dmamap_t map;
745 bus_addr_t offset;
746 bus_size_t len;
747 int ops;
748 {
749 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
750
751 /*
752 * Mixing PRE and POST operations is not allowed.
753 */
754 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
755 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
756 panic("_intio_bus_dmamap_sync: mix PRE and POST");
757
758 #ifdef DIAGNOSTIC
759 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
760 if (offset >= map->dm_mapsize)
761 panic("_intio_bus_dmamap_sync: bad offset");
762 if (len == 0 || (offset + len) > map->dm_mapsize)
763 panic("_intio_bus_dmamap_sync: bad length");
764 }
765 #endif
766
767 /*
768 * If we're not bouncing, just return; nothing to do.
769 */
770 if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
771 return;
772
773 switch (cookie->id_buftype) {
774 case ID_BUFTYPE_LINEAR:
775 /*
776 * Nothing to do for pre-read.
777 */
778
779 if (ops & BUS_DMASYNC_PREWRITE) {
780 /*
781 * Copy the caller's buffer to the bounce buffer.
782 */
783 memcpy((char *)cookie->id_bouncebuf + offset,
784 (char *)cookie->id_origbuf + offset, len);
785 }
786
787 if (ops & BUS_DMASYNC_POSTREAD) {
788 /*
789 * Copy the bounce buffer to the caller's buffer.
790 */
791 memcpy((char *)cookie->id_origbuf + offset,
792 (char *)cookie->id_bouncebuf + offset, len);
793 }
794
795 /*
796 * Nothing to do for post-write.
797 */
798 break;
799
800 case ID_BUFTYPE_MBUF:
801 {
802 struct mbuf *m, *m0 = cookie->id_origbuf;
803 bus_size_t minlen, moff;
804
805 /*
806 * Nothing to do for pre-read.
807 */
808
809 if (ops & BUS_DMASYNC_PREWRITE) {
810 /*
811 * Copy the caller's buffer to the bounce buffer.
812 */
813 m_copydata(m0, offset, len,
814 (char *)cookie->id_bouncebuf + offset);
815 }
816
817 if (ops & BUS_DMASYNC_POSTREAD) {
818 /*
819 * Copy the bounce buffer to the caller's buffer.
820 */
821 for (moff = offset, m = m0; m != NULL && len != 0;
822 m = m->m_next) {
823 /* Find the beginning mbuf. */
824 if (moff >= m->m_len) {
825 moff -= m->m_len;
826 continue;
827 }
828
829 /*
830 * Now at the first mbuf to sync; nail
831 * each one until we have exhausted the
832 * length.
833 */
834 minlen = len < m->m_len - moff ?
835 len : m->m_len - moff;
836
837 memcpy(mtod(m, caddr_t) + moff,
838 (char *)cookie->id_bouncebuf + offset,
839 minlen);
840
841 moff = 0;
842 len -= minlen;
843 offset += minlen;
844 }
845 }
846
847 /*
848 * Nothing to do for post-write.
849 */
850 break;
851 }
852
853 case ID_BUFTYPE_UIO:
854 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_UIO");
855 break;
856
857 case ID_BUFTYPE_RAW:
858 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_RAW");
859 break;
860
861 case ID_BUFTYPE_INVALID:
862 panic("_intio_bus_dmamap_sync: ID_BUFTYPE_INVALID");
863 break;
864
865 default:
866 printf("unknown buffer type %d\n", cookie->id_buftype);
867 panic("_intio_bus_dmamap_sync");
868 }
869 }
870
871 /*
872 * Allocate memory safe for INTIO DMA.
873 */
874 int
875 _intio_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
876 bus_dma_tag_t t;
877 bus_size_t size, alignment, boundary;
878 bus_dma_segment_t *segs;
879 int nsegs;
880 int *rsegs;
881 int flags;
882 {
883 paddr_t high;
884 extern paddr_t avail_end;
885
886 if (avail_end > INTIO_DMA_BOUNCE_THRESHOLD)
887 high = trunc_page(INTIO_DMA_BOUNCE_THRESHOLD);
888 else
889 high = trunc_page(avail_end);
890
891 return (x68k_bus_dmamem_alloc_range(t, size, alignment, boundary,
892 segs, nsegs, rsegs, flags, 0, high));
893 }
894
895 /**********************************************************************
896 * INTIO DMA utility functions
897 **********************************************************************/
898
899 int
900 _intio_dma_alloc_bouncebuf(t, map, size, flags)
901 bus_dma_tag_t t;
902 bus_dmamap_t map;
903 bus_size_t size;
904 int flags;
905 {
906 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
907 int error = 0;
908
909 cookie->id_bouncebuflen = round_page(size);
910 error = _intio_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
911 NBPG, map->x68k_dm_boundary, cookie->id_bouncesegs,
912 map->x68k_dm_segcnt, &cookie->id_nbouncesegs, flags);
913 if (error)
914 goto out;
915 error = x68k_bus_dmamem_map(t, cookie->id_bouncesegs,
916 cookie->id_nbouncesegs, cookie->id_bouncebuflen,
917 (caddr_t *)&cookie->id_bouncebuf, flags);
918
919 out:
920 if (error) {
921 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
922 cookie->id_nbouncesegs);
923 cookie->id_bouncebuflen = 0;
924 cookie->id_nbouncesegs = 0;
925 } else {
926 cookie->id_flags |= ID_HAS_BOUNCE;
927 }
928
929 return (error);
930 }
931
932 void
933 _intio_dma_free_bouncebuf(t, map)
934 bus_dma_tag_t t;
935 bus_dmamap_t map;
936 {
937 struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
938
939 x68k_bus_dmamem_unmap(t, cookie->id_bouncebuf,
940 cookie->id_bouncebuflen);
941 x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
942 cookie->id_nbouncesegs);
943 cookie->id_bouncebuflen = 0;
944 cookie->id_nbouncesegs = 0;
945 cookie->id_flags &= ~ID_HAS_BOUNCE;
946 }
947