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