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