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