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