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