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