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