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