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