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