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