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