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