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