Home | History | Annotate | Line # | Download | only in dev
vme_machdep.c revision 1.29
      1 /*	$NetBSD: vme_machdep.c,v 1.29 2000/06/29 07:40:08 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/extent.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/errno.h>
     45 
     46 #include <sys/proc.h>
     47 #include <sys/user.h>
     48 #include <sys/syslog.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #define _SPARC_BUS_DMA_PRIVATE
     53 #include <machine/bus.h>
     54 #include <sparc/sparc/iommuvar.h>
     55 #include <machine/autoconf.h>
     56 #include <machine/oldmon.h>
     57 #include <machine/cpu.h>
     58 #include <machine/ctlreg.h>
     59 
     60 #include <dev/vme/vmereg.h>
     61 #include <dev/vme/vmevar.h>
     62 
     63 #include <sparc/sparc/asm.h>
     64 #include <sparc/sparc/vaddrs.h>
     65 #include <sparc/sparc/cpuvar.h>
     66 #include <sparc/dev/vmereg.h>
     67 
     68 struct sparcvme_softc {
     69 	struct device	 sc_dev;	/* base device */
     70 	bus_space_tag_t	 sc_bustag;
     71 	bus_dma_tag_t	 sc_dmatag;
     72 	struct vmebusreg *sc_reg; 	/* VME control registers */
     73 	struct vmebusvec *sc_vec;	/* VME interrupt vector */
     74 	struct rom_range *sc_range;	/* ROM range property */
     75 	int		 sc_nrange;
     76 	volatile u_int32_t *sc_ioctags;	/* VME IO-cache tag registers */
     77 	volatile u_int32_t *sc_iocflush;/* VME IO-cache flush registers */
     78 	int 		 (*sc_vmeintr) __P((void *));
     79 };
     80 struct  sparcvme_softc *sparcvme_sc;/*XXX*/
     81 
     82 /* autoconfiguration driver */
     83 static int	vmematch_iommu  __P((struct device *, struct cfdata *, void *));
     84 static void	vmeattach_iommu __P((struct device *, struct device *, void *));
     85 static int	vmematch_mainbus  __P((struct device *, struct cfdata *, void *));
     86 static void	vmeattach_mainbus __P((struct device *, struct device *, void *));
     87 #if defined(SUN4)
     88 int 		vmeintr4  __P((void *));
     89 #endif
     90 #if defined(SUN4M)
     91 int 		vmeintr4m __P((void *));
     92 static int	sparc_vme_error __P((void));
     93 #endif
     94 
     95 
     96 static int	sparc_vme_probe __P((void *, vme_addr_t, vme_size_t,
     97 				vme_am_t, vme_datasize_t,
     98 	int (*) __P((void *, bus_space_tag_t, bus_space_handle_t)), void *));
     99 static int	sparc_vme_map __P((void *, vme_addr_t, vme_size_t, vme_am_t,
    100 				   vme_datasize_t, vme_swap_t,
    101 				   bus_space_tag_t *, bus_space_handle_t *,
    102 				   vme_mapresc_t *));
    103 static void	sparc_vme_unmap __P((void *, vme_mapresc_t));
    104 static int	sparc_vme_intr_map __P((void *, int, int, vme_intr_handle_t *));
    105 static const struct evcnt *sparc_vme_intr_evcnt __P((void *,
    106 						     vme_intr_handle_t));
    107 static void *	sparc_vme_intr_establish __P((void *, vme_intr_handle_t, int,
    108 					      int (*) __P((void *)), void *));
    109 static void	sparc_vme_intr_disestablish __P((void *, void *));
    110 
    111 static int	vmebus_translate __P((struct sparcvme_softc *, vme_am_t,
    112 				      vme_addr_t, bus_type_t *, bus_addr_t *));
    113 #if defined(SUN4M)
    114 static void	sparc_vme_iommu_barrier __P(( bus_space_tag_t, bus_space_handle_t,
    115 					  bus_size_t, bus_size_t, int));
    116 
    117 #endif
    118 
    119 /*
    120  * DMA functions.
    121  */
    122 static void	sparc_vct_dmamap_destroy __P((void *, bus_dmamap_t));
    123 
    124 #if defined(SUN4)
    125 static int	sparc_vct4_dmamap_create __P((void *, vme_size_t, vme_am_t,
    126 		    vme_datasize_t, vme_swap_t, int, vme_size_t, vme_addr_t,
    127 		    int, bus_dmamap_t *));
    128 static int	sparc_vme4_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    129 		    bus_size_t, struct proc *, int));
    130 static void	sparc_vme4_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    131 static void	sparc_vme4_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
    132 		    bus_addr_t, bus_size_t, int));
    133 #endif
    134 
    135 #if defined(SUN4M)
    136 static int	sparc_vct_iommu_dmamap_create __P((void *, vme_size_t, vme_am_t,
    137 		    vme_datasize_t, vme_swap_t, int, vme_size_t, vme_addr_t,
    138 		    int, bus_dmamap_t *));
    139 static int	sparc_vme_iommu_dmamap_create __P((bus_dma_tag_t, bus_size_t,
    140 		    int, bus_size_t, bus_size_t, int, bus_dmamap_t *));
    141 
    142 static int	sparc_vme_iommu_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t,
    143 		    void *, bus_size_t, struct proc *, int));
    144 static void	sparc_vme_iommu_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    145 static void	sparc_vme_iommu_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
    146 		    bus_addr_t, bus_size_t, int));
    147 #endif
    148 
    149 static int	sparc_vme_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *,
    150 		    int, size_t, caddr_t *, int));
    151 #if 0
    152 static void	sparc_vme_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
    153 static void	sparc_vme_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
    154 static paddr_t	sparc_vme_dmamem_mmap __P((bus_dma_tag_t,
    155 		    bus_dma_segment_t *, int, off_t, int, int));
    156 #endif
    157 
    158 int sparc_vme_mmap_cookie __P((vme_addr_t, vme_am_t, bus_space_handle_t *));
    159 
    160 struct cfattach vme_mainbus_ca = {
    161 	sizeof(struct sparcvme_softc), vmematch_mainbus, vmeattach_mainbus
    162 };
    163 
    164 struct cfattach vme_iommu_ca = {
    165 	sizeof(struct sparcvme_softc), vmematch_iommu, vmeattach_iommu
    166 };
    167 
    168 int	(*vmeerr_handler) __P((void));
    169 
    170 #define VMEMOD_D32 0x40 /* ??? */
    171 
    172 /* If the PROM does not provide the `ranges' property, we make up our own */
    173 struct rom_range vmebus_translations[] = {
    174 #define _DS (VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA)
    175 	{ VME_AM_A16|_DS, 0, PMAP_VME16, 0xffff0000, 0 },
    176 	{ VME_AM_A24|_DS, 0, PMAP_VME16, 0xff000000, 0 },
    177 	{ VME_AM_A32|_DS, 0, PMAP_VME16, 0x00000000, 0 },
    178 	{ VME_AM_A16|VMEMOD_D32|_DS, 0, PMAP_VME32, 0xffff0000, 0 },
    179 	{ VME_AM_A24|VMEMOD_D32|_DS, 0, PMAP_VME32, 0xff000000, 0 },
    180 	{ VME_AM_A32|VMEMOD_D32|_DS, 0, PMAP_VME32, 0x00000000, 0 }
    181 #undef _DS
    182 };
    183 
    184 /*
    185  * The VME bus logic on sun4 machines maps DMA requests in the first MB
    186  * of VME space to the last MB of DVMA space. `vme_dvmamap' is used
    187  * for DVMA space allocations. The DMA addresses returned by
    188  * bus_dmamap_load*() must be relocated by -VME4_DVMA_BASE.
    189  */
    190 struct extent *vme_dvmamap;
    191 
    192 /*
    193  * The VME hardware on the sun4m IOMMU maps the first 8MB of 32-bit
    194  * VME space to the last 8MB of DVMA space and the first 1MB of
    195  * 24-bit VME space to the first 1MB of the last 8MB of DVMA space
    196  * (thus 24-bit VME space overlaps the first 1MB of of 32-bit space).
    197  * The following constants define subregions in the IOMMU DVMA map
    198  * for VME DVMA allocations.  The DMA addresses returned by
    199  * bus_dmamap_load*() must be relocated by -VME_IOMMU_DVMA_BASE.
    200  */
    201 #define VME_IOMMU_DVMA_BASE		0xff800000
    202 #define VME_IOMMU_DVMA_AM24_BASE	VME_IOMMU_DVMA_BASE
    203 #define VME_IOMMU_DVMA_AM24_END		0xff900000
    204 #define VME_IOMMU_DVMA_AM32_BASE	VME_IOMMU_DVMA_BASE
    205 #define VME_IOMMU_DVMA_AM32_END		IOMMU_DVMA_END
    206 
    207 struct sparc_bus_space_tag sparc_vme_bus_tag = {
    208 	NULL, /* cookie */
    209 	NULL, /* parent bus tag */
    210 	NULL, /* bus_map */
    211 	NULL, /* bus_unmap */
    212 	NULL, /* bus_subregion */
    213 	NULL  /* barrier */
    214 };
    215 
    216 struct vme_chipset_tag sparc_vme_chipset_tag = {
    217 	NULL,
    218 	sparc_vme_map,
    219 	sparc_vme_unmap,
    220 	sparc_vme_probe,
    221 	sparc_vme_intr_map,
    222 	sparc_vme_intr_evcnt,
    223 	sparc_vme_intr_establish,
    224 	sparc_vme_intr_disestablish,
    225 	0, 0, 0 /* bus specific DMA stuff */
    226 };
    227 
    228 
    229 #if defined(SUN4)
    230 struct sparc_bus_dma_tag sparc_vme4_dma_tag = {
    231 	NULL,	/* cookie */
    232 	_bus_dmamap_create,
    233 	_bus_dmamap_destroy,
    234 	sparc_vme4_dmamap_load,
    235 	_bus_dmamap_load_mbuf,
    236 	_bus_dmamap_load_uio,
    237 	_bus_dmamap_load_raw,
    238 	sparc_vme4_dmamap_unload,
    239 	sparc_vme4_dmamap_sync,
    240 
    241 	_bus_dmamem_alloc,
    242 	_bus_dmamem_free,
    243 	sparc_vme_dmamem_map,
    244 	_bus_dmamem_unmap,
    245 	_bus_dmamem_mmap
    246 };
    247 #endif
    248 
    249 #if defined(SUN4M)
    250 struct sparc_bus_dma_tag sparc_vme_iommu_dma_tag = {
    251 	NULL,	/* cookie */
    252 	sparc_vme_iommu_dmamap_create,
    253 	_bus_dmamap_destroy,
    254 	sparc_vme_iommu_dmamap_load,
    255 	_bus_dmamap_load_mbuf,
    256 	_bus_dmamap_load_uio,
    257 	_bus_dmamap_load_raw,
    258 	sparc_vme_iommu_dmamap_unload,
    259 	sparc_vme_iommu_dmamap_sync,
    260 
    261 	_bus_dmamem_alloc,
    262 	_bus_dmamem_free,
    263 	sparc_vme_dmamem_map,
    264 	_bus_dmamem_unmap,
    265 	_bus_dmamem_mmap
    266 };
    267 #endif
    268 
    269 
    270 int
    271 vmematch_mainbus(parent, cf, aux)
    272 	struct device *parent;
    273 	struct cfdata *cf;
    274 	void *aux;
    275 {
    276 	struct mainbus_attach_args *ma = aux;
    277 
    278 	if (!CPU_ISSUN4)
    279 		return (0);
    280 
    281 	return (strcmp("vme", ma->ma_name) == 0);
    282 }
    283 
    284 int
    285 vmematch_iommu(parent, cf, aux)
    286 	struct device *parent;
    287 	struct cfdata *cf;
    288 	void *aux;
    289 {
    290 	struct iommu_attach_args *ia = aux;
    291 
    292 	return (strcmp("vme", ia->iom_name) == 0);
    293 }
    294 
    295 
    296 void
    297 vmeattach_mainbus(parent, self, aux)
    298 	struct device *parent, *self;
    299 	void *aux;
    300 {
    301 #if defined(SUN4)
    302 	struct mainbus_attach_args *ma = aux;
    303 	struct sparcvme_softc *sc = (struct sparcvme_softc *)self;
    304 	struct vmebus_attach_args vba;
    305 
    306 	if (self->dv_unit > 0) {
    307 		printf(" unsupported\n");
    308 		return;
    309 	}
    310 
    311 	sc->sc_bustag = ma->ma_bustag;
    312 	sc->sc_dmatag = ma->ma_dmatag;
    313 
    314 	/* VME interrupt entry point */
    315 	sc->sc_vmeintr = vmeintr4;
    316 
    317 /*XXX*/	sparc_vme_chipset_tag.cookie = self;
    318 /*XXX*/	sparc_vme_chipset_tag.vct_dmamap_create = sparc_vct4_dmamap_create;
    319 /*XXX*/	sparc_vme_chipset_tag.vct_dmamap_destroy = sparc_vct_dmamap_destroy;
    320 /*XXX*/	sparc_vme4_dma_tag._cookie = self;
    321 
    322 #if 0
    323 	sparc_vme_bus_tag.parent = ma->ma_bustag;
    324 	vba.vba_bustag = &sparc_vme_bus_tag;
    325 #endif
    326 	vba.va_vct = &sparc_vme_chipset_tag;
    327 	vba.va_bdt = &sparc_vme4_dma_tag;
    328 	vba.va_slaveconfig = 0;
    329 
    330 	/* Fall back to our own `range' construction */
    331 	sc->sc_range = vmebus_translations;
    332 	sc->sc_nrange =
    333 		sizeof(vmebus_translations)/sizeof(vmebus_translations[0]);
    334 
    335 	vme_dvmamap = extent_create("vmedvma", VME4_DVMA_BASE, VME4_DVMA_END,
    336 				    M_DEVBUF, 0, 0, EX_NOWAIT);
    337 	if (vme_dvmamap == NULL)
    338 		panic("vme: unable to allocate DVMA map");
    339 
    340 	printf("\n");
    341 	(void)config_found(self, &vba, 0);
    342 
    343 #endif
    344 	return;
    345 }
    346 
    347 /* sun4m vmebus */
    348 void
    349 vmeattach_iommu(parent, self, aux)
    350 	struct device *parent, *self;
    351 	void *aux;
    352 {
    353 #if defined(SUN4M)
    354 	struct sparcvme_softc *sc = (struct sparcvme_softc *)self;
    355 	struct iommu_attach_args *ia = aux;
    356 	struct vmebus_attach_args vba;
    357 	bus_space_handle_t bh;
    358 	int node;
    359 	int cline;
    360 
    361 	if (self->dv_unit > 0) {
    362 		printf(" unsupported\n");
    363 		return;
    364 	}
    365 
    366 	sc->sc_bustag = ia->iom_bustag;
    367 	sc->sc_dmatag = ia->iom_dmatag;
    368 
    369 	/* VME interrupt entry point */
    370 	sc->sc_vmeintr = vmeintr4m;
    371 
    372 /*XXX*/	sparc_vme_chipset_tag.cookie = self;
    373 /*XXX*/	sparc_vme_chipset_tag.vct_dmamap_create = sparc_vct_iommu_dmamap_create;
    374 /*XXX*/	sparc_vme_chipset_tag.vct_dmamap_destroy = sparc_vct_dmamap_destroy;
    375 /*XXX*/	sparc_vme_iommu_dma_tag._cookie = self;
    376 	sparc_vme_bus_tag.sparc_bus_barrier = sparc_vme_iommu_barrier;
    377 
    378 #if 0
    379 	vba.vba_bustag = &sparc_vme_bus_tag;
    380 #endif
    381 	vba.va_vct = &sparc_vme_chipset_tag;
    382 	vba.va_bdt = &sparc_vme_iommu_dma_tag;
    383 	vba.va_slaveconfig = 0;
    384 
    385 	node = ia->iom_node;
    386 
    387 	/*
    388 	 * Map VME control space
    389 	 */
    390 	if (ia->iom_nreg < 2) {
    391 		printf("%s: only %d register sets\n", self->dv_xname,
    392 			ia->iom_nreg);
    393 		return;
    394 	}
    395 
    396 	if (bus_space_map2(ia->iom_bustag,
    397 			  (bus_type_t)ia->iom_reg[0].ior_iospace,
    398 			  (bus_addr_t)ia->iom_reg[0].ior_pa,
    399 			  (bus_size_t)ia->iom_reg[0].ior_size,
    400 			  BUS_SPACE_MAP_LINEAR,
    401 			  0, &bh) != 0) {
    402 		panic("%s: can't map vmebusreg", self->dv_xname);
    403 	}
    404 	sc->sc_reg = (struct vmebusreg *)bh;
    405 
    406 	if (bus_space_map2(ia->iom_bustag,
    407 			  (bus_type_t)ia->iom_reg[1].ior_iospace,
    408 			  (bus_addr_t)ia->iom_reg[1].ior_pa,
    409 			  (bus_size_t)ia->iom_reg[1].ior_size,
    410 			  BUS_SPACE_MAP_LINEAR,
    411 			  0, &bh) != 0) {
    412 		panic("%s: can't map vmebusvec", self->dv_xname);
    413 	}
    414 	sc->sc_vec = (struct vmebusvec *)bh;
    415 
    416 	/*
    417 	 * Map VME IO cache tags and flush control.
    418 	 */
    419 	if (bus_space_map2(ia->iom_bustag,
    420 			  (bus_type_t)ia->iom_reg[1].ior_iospace,
    421 			  (bus_addr_t)ia->iom_reg[1].ior_pa + VME_IOC_TAGOFFSET,
    422 			  VME_IOC_SIZE,
    423 			  BUS_SPACE_MAP_LINEAR,
    424 			  0, &bh) != 0) {
    425 		panic("%s: can't map IOC tags", self->dv_xname);
    426 	}
    427 	sc->sc_ioctags = (u_int32_t *)bh;
    428 
    429 	if (bus_space_map2(ia->iom_bustag,
    430 			  (bus_type_t)ia->iom_reg[1].ior_iospace,
    431 			  (bus_addr_t)ia->iom_reg[1].ior_pa+VME_IOC_FLUSHOFFSET,
    432 			  VME_IOC_SIZE,
    433 			  BUS_SPACE_MAP_LINEAR,
    434 			  0, &bh) != 0) {
    435 		panic("%s: can't map IOC flush registers", self->dv_xname);
    436 	}
    437 	sc->sc_iocflush = (u_int32_t *)bh;
    438 
    439 /*XXX*/	sparc_vme_bus_tag.cookie = sc->sc_reg;
    440 
    441 	/*
    442 	 * Get "range" property.
    443 	 */
    444 	if (getprop(node, "ranges", sizeof(struct rom_range),
    445 		    &sc->sc_nrange, (void **)&sc->sc_range) != 0) {
    446 		panic("%s: can't get ranges property", self->dv_xname);
    447 	}
    448 
    449 	sparcvme_sc = sc;
    450 	vmeerr_handler = sparc_vme_error;
    451 
    452 	/*
    453 	 * Invalidate all IO-cache entries.
    454 	 */
    455 	for (cline = VME_IOC_SIZE/VME_IOC_LINESZ; cline > 0;) {
    456 		sc->sc_ioctags[--cline] = 0;
    457 	}
    458 
    459 	/* Enable IO-cache */
    460 	sc->sc_reg->vmebus_cr |= VMEBUS_CR_C;
    461 
    462 	printf(": version 0x%x\n",
    463 	       sc->sc_reg->vmebus_cr & VMEBUS_CR_IMPL);
    464 
    465 	(void)config_found(self, &vba, 0);
    466 #endif
    467 }
    468 
    469 #if defined(SUN4M)
    470 static int
    471 sparc_vme_error()
    472 {
    473 	struct sparcvme_softc *sc = sparcvme_sc;
    474 	u_int32_t afsr, afpa;
    475 	char bits[64];
    476 
    477 	afsr = sc->sc_reg->vmebus_afsr;
    478 	afpa = sc->sc_reg->vmebus_afar;
    479 	printf("VME error:\n\tAFSR %s\n",
    480 		bitmask_snprintf(afsr, VMEBUS_AFSR_BITS, bits, sizeof(bits)));
    481 	printf("\taddress: 0x%x%x\n", afsr, afpa);
    482 	return (0);
    483 }
    484 #endif
    485 
    486 int
    487 vmebus_translate(sc, mod, addr, btp, bap)
    488 	struct sparcvme_softc *sc;
    489 	vme_am_t	mod;
    490 	vme_addr_t	addr;
    491 	bus_type_t	*btp;
    492 	bus_addr_t	*bap;
    493 {
    494 	int i;
    495 
    496 	for (i = 0; i < sc->sc_nrange; i++) {
    497 
    498 		if (sc->sc_range[i].cspace != mod)
    499 			continue;
    500 
    501 		/* We've found the connection to the parent bus */
    502 		*bap = sc->sc_range[i].poffset + addr;
    503 		*btp = sc->sc_range[i].pspace;
    504 		return (0);
    505 	}
    506 	return (ENOENT);
    507 }
    508 
    509 struct vmeprobe_myarg {
    510 	int (*cb) __P((void *, bus_space_tag_t, bus_space_handle_t));
    511 	void *cbarg;
    512 	bus_space_tag_t tag;
    513 	int res; /* backwards */
    514 };
    515 
    516 static int vmeprobe_mycb __P((void *, void *));
    517 static int
    518 vmeprobe_mycb(bh, arg)
    519 	void *bh, *arg;
    520 {
    521 	struct vmeprobe_myarg *a = arg;
    522 
    523 	a->res = (*a->cb)(a->cbarg, a->tag, (bus_space_handle_t)bh);
    524 	return (!a->res);
    525 }
    526 
    527 int
    528 sparc_vme_probe(cookie, addr, len, mod, datasize, callback, arg)
    529 	void *cookie;
    530 	vme_addr_t addr;
    531 	vme_size_t len;
    532 	vme_am_t mod;
    533 	vme_datasize_t datasize;
    534 	int (*callback) __P((void *, bus_space_tag_t, bus_space_handle_t));
    535 	void *arg;
    536 {
    537 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
    538 	bus_type_t iospace;
    539 	bus_addr_t paddr;
    540 	bus_size_t size;
    541 	struct vmeprobe_myarg myarg;
    542 	int res, i;
    543 
    544 	if (vmebus_translate(sc, mod, addr, &iospace, &paddr) != 0)
    545 		return (EINVAL);
    546 
    547 	size = (datasize == VME_D8 ? 1 : (datasize == VME_D16 ? 2 : 4));
    548 
    549 	if (callback) {
    550 		myarg.cb = callback;
    551 		myarg.cbarg = arg;
    552 		myarg.tag = sc->sc_bustag;
    553 		myarg.res = 0;
    554 		res = bus_space_probe(sc->sc_bustag, iospace, paddr, size, 0,
    555 				      0, vmeprobe_mycb, &myarg);
    556 		return (res ? 0 : (myarg.res ? myarg.res : EIO));
    557 	}
    558 
    559 	for (i = 0; i < len / size; i++) {
    560 		myarg.res = 0;
    561 		res = bus_space_probe(sc->sc_bustag, iospace, paddr, size, 0,
    562 				      0, 0, 0);
    563 		if (res == 0)
    564 			return (EIO);
    565 		paddr += size;
    566 	}
    567 	return (0);
    568 }
    569 
    570 int
    571 sparc_vme_map(cookie, addr, size, mod, datasize, swap, tp, hp, rp)
    572 	void *cookie;
    573 	vme_addr_t addr;
    574 	vme_size_t size;
    575 	vme_am_t mod;
    576 	vme_datasize_t datasize;
    577 	vme_swap_t swap;
    578 	bus_space_tag_t *tp;
    579 	bus_space_handle_t *hp;
    580 	vme_mapresc_t *rp;
    581 {
    582 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
    583 	bus_type_t iospace;
    584 	bus_addr_t paddr;
    585 	int error;
    586 
    587 	error = vmebus_translate(sc, mod, addr, &iospace, &paddr);
    588 	if (error != 0)
    589 		return (error);
    590 
    591 	*tp = sc->sc_bustag;
    592 	return (bus_space_map2(sc->sc_bustag, iospace, paddr, size, 0, 0, hp));
    593 }
    594 
    595 int
    596 sparc_vme_mmap_cookie(addr, mod, hp)
    597 	vme_addr_t addr;
    598 	vme_am_t mod;
    599 	bus_space_handle_t *hp;
    600 {
    601 	struct sparcvme_softc *sc = sparcvme_sc;
    602 	bus_type_t iospace;
    603 	bus_addr_t paddr;
    604 	int error;
    605 
    606 	error = vmebus_translate(sc, mod, addr, &iospace, &paddr);
    607 	if (error != 0)
    608 		return (error);
    609 
    610 	return (bus_space_mmap(sc->sc_bustag, iospace, paddr, 0, hp));
    611 }
    612 
    613 #if defined(SUN4M)
    614 void
    615 sparc_vme_iommu_barrier(t, h, offset, size, flags)
    616 	bus_space_tag_t t;
    617 	bus_space_handle_t h;
    618 	bus_size_t offset;
    619 	bus_size_t size;
    620 	int flags;
    621 {
    622 	struct vmebusreg *vbp = (struct vmebusreg *)t->cookie;
    623 
    624 	/* Read async fault status to flush write-buffers */
    625 	(*(volatile int *)&vbp->vmebus_afsr);
    626 }
    627 #endif
    628 
    629 
    630 
    631 /*
    632  * VME Interrupt Priority Level to sparc Processor Interrupt Level.
    633  */
    634 static int vme_ipl_to_pil[] = {
    635 	0,
    636 	2,
    637 	3,
    638 	5,
    639 	7,
    640 	9,
    641 	11,
    642 	13
    643 };
    644 
    645 
    646 /*
    647  * All VME device interrupts go through vmeintr(). This function reads
    648  * the VME vector from the bus, then dispatches the device interrupt
    649  * handler.  All handlers for devices that map to the same Processor
    650  * Interrupt Level (according to the table above) are on a linked list
    651  * of `sparc_vme_intr_handle' structures. The head of which is passed
    652  * down as the argument to `vmeintr(void *arg)'.
    653  */
    654 struct sparc_vme_intr_handle {
    655 	struct intrhand ih;
    656 	struct sparc_vme_intr_handle *next;
    657 	int	vec;		/* VME interrupt vector */
    658 	int	pri;		/* VME interrupt priority */
    659 	struct sparcvme_softc *sc;/*XXX*/
    660 };
    661 
    662 #if defined(SUN4)
    663 int
    664 vmeintr4(arg)
    665 	void *arg;
    666 {
    667 	struct sparc_vme_intr_handle *ihp = (vme_intr_handle_t)arg;
    668 	int level, vec;
    669 	int i = 0;
    670 
    671 	level = (ihp->pri << 1) | 1;
    672 
    673 	vec = ldcontrolb((caddr_t)(AC_VMEINTVEC | level));
    674 
    675 	if (vec == -1) {
    676 		printf("vme: spurious interrupt\n");
    677 		return 1; /* XXX - pretend we handled it, for now */
    678 	}
    679 
    680 	for (; ihp; ihp = ihp->next)
    681 		if (ihp->vec == vec && ihp->ih.ih_fun)
    682 			i += (ihp->ih.ih_fun)(ihp->ih.ih_arg);
    683 	return (i);
    684 }
    685 #endif
    686 
    687 #if defined(SUN4M)
    688 int
    689 vmeintr4m(arg)
    690 	void *arg;
    691 {
    692 	struct sparc_vme_intr_handle *ihp = (vme_intr_handle_t)arg;
    693 	int level, vec;
    694 	int i = 0;
    695 
    696 	level = (ihp->pri << 1) | 1;
    697 
    698 #if 0
    699 	int pending;
    700 
    701 	/* Flush VME <=> Sbus write buffers */
    702 	(*(volatile int *)&ihp->sc->sc_reg->vmebus_afsr);
    703 
    704 	pending = *((int*)ICR_SI_PEND);
    705 	if ((pending & SINTR_VME(ihp->pri)) == 0) {
    706 		printf("vmeintr: non pending at pri %x(p 0x%x)\n",
    707 			ihp->pri, pending);
    708 		return (0);
    709 	}
    710 #endif
    711 #if 0
    712 	/* Why gives this a bus timeout sometimes? */
    713 	vec = ihp->sc->sc_vec->vmebusvec[level];
    714 #else
    715 	/* so, arrange to catch the fault... */
    716 	{
    717 	extern struct user *proc0paddr;
    718 	extern int fkbyte __P((caddr_t, struct pcb *));
    719 	caddr_t addr = (caddr_t)&ihp->sc->sc_vec->vmebusvec[level];
    720 	struct pcb *xpcb;
    721 	u_long saveonfault;
    722 	int s;
    723 
    724 	s = splhigh();
    725 	if (curproc == NULL)
    726 		xpcb = (struct pcb *)proc0paddr;
    727 	else
    728 		xpcb = &curproc->p_addr->u_pcb;
    729 
    730 	saveonfault = (u_long)xpcb->pcb_onfault;
    731 	vec = fkbyte(addr, xpcb);
    732 	xpcb->pcb_onfault = (caddr_t)saveonfault;
    733 
    734 	splx(s);
    735 	}
    736 #endif
    737 
    738 	if (vec == -1) {
    739 		printf("vme: spurious interrupt: ");
    740 		printf("SI: 0x%x, VME AFSR: 0x%x, VME AFAR 0x%x\n",
    741 			*((int*)ICR_SI_PEND),
    742 			ihp->sc->sc_reg->vmebus_afsr,
    743 			ihp->sc->sc_reg->vmebus_afar);
    744 		return (1); /* XXX - pretend we handled it, for now */
    745 	}
    746 
    747 	for (; ihp; ihp = ihp->next)
    748 		if (ihp->vec == vec && ihp->ih.ih_fun)
    749 			i += (ihp->ih.ih_fun)(ihp->ih.ih_arg);
    750 	return (i);
    751 }
    752 #endif
    753 
    754 int
    755 sparc_vme_intr_map(cookie, level, vec, ihp)
    756 	void *cookie;
    757 	int level;
    758 	int vec;
    759 	vme_intr_handle_t *ihp;
    760 {
    761 	struct sparc_vme_intr_handle *ih;
    762 
    763 	ih = (vme_intr_handle_t)
    764 	    malloc(sizeof(struct sparc_vme_intr_handle), M_DEVBUF, M_NOWAIT);
    765 	ih->pri = level;
    766 	ih->vec = vec;
    767 	ih->sc = cookie;/*XXX*/
    768 	*ihp = ih;
    769 	return (0);
    770 }
    771 
    772 const struct evcnt *
    773 sparc_vme_intr_evcnt(cookie, vih)
    774 	void *cookie;
    775 	vme_intr_handle_t vih;
    776 {
    777 
    778 	/* XXX for now, no evcnt parent reported */
    779 	return NULL;
    780 }
    781 
    782 void *
    783 sparc_vme_intr_establish(cookie, vih, pri, func, arg)
    784 	void *cookie;
    785 	vme_intr_handle_t vih;
    786 	int pri;
    787 	int (*func) __P((void *));
    788 	void *arg;
    789 {
    790 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
    791 	struct sparc_vme_intr_handle *svih =
    792 			(struct sparc_vme_intr_handle *)vih;
    793 	struct intrhand *ih;
    794 	int level;
    795 
    796 	/* XXX pri == svih->pri ??? */
    797 
    798 	/* Translate VME priority to processor IPL */
    799 	level = vme_ipl_to_pil[svih->pri];
    800 
    801 	svih->ih.ih_fun = func;
    802 	svih->ih.ih_arg = arg;
    803 	svih->next = NULL;
    804 
    805 	/* ensure the interrupt subsystem will call us at this level */
    806 	for (ih = intrhand[level]; ih != NULL; ih = ih->ih_next)
    807 		if (ih->ih_fun == sc->sc_vmeintr)
    808 			break;
    809 
    810 	if (ih == NULL) {
    811 		ih = (struct intrhand *)
    812 			malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    813 		if (ih == NULL)
    814 			panic("vme_addirq");
    815 		bzero(ih, sizeof *ih);
    816 		ih->ih_fun = sc->sc_vmeintr;
    817 		ih->ih_arg = vih;
    818 		intr_establish(level, ih);
    819 	} else {
    820 		svih->next = (vme_intr_handle_t)ih->ih_arg;
    821 		ih->ih_arg = vih;
    822 	}
    823 	return (NULL);
    824 }
    825 
    826 void
    827 sparc_vme_unmap(cookie, resc)
    828 	void * cookie;
    829 	vme_mapresc_t resc;
    830 {
    831 	/* Not implemented */
    832 	panic("sparc_vme_unmap");
    833 }
    834 
    835 void
    836 sparc_vme_intr_disestablish(cookie, a)
    837 	void *cookie;
    838 	void *a;
    839 {
    840 	/* Not implemented */
    841 	panic("sparc_vme_intr_disestablish");
    842 }
    843 
    844 
    845 
    846 /*
    847  * VME DMA functions.
    848  */
    849 
    850 static void
    851 sparc_vct_dmamap_destroy(cookie, map)
    852 	void *cookie;
    853 	bus_dmamap_t map;
    854 {
    855 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
    856 	bus_dmamap_destroy(sc->sc_dmatag, map);
    857 }
    858 
    859 #if defined(SUN4)
    860 static int
    861 sparc_vct4_dmamap_create(cookie, size, am, datasize, swap, nsegments, maxsegsz,
    862 			  boundary, flags, dmamp)
    863 	void *cookie;
    864 	vme_size_t size;
    865 	vme_am_t am;
    866 	vme_datasize_t datasize;
    867 	vme_swap_t swap;
    868 	int nsegments;
    869 	vme_size_t maxsegsz;
    870 	vme_addr_t boundary;
    871 	int flags;
    872 	bus_dmamap_t *dmamp;
    873 {
    874 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
    875 
    876 	/* Allocate a base map through parent bus ops */
    877 	return (bus_dmamap_create(sc->sc_dmatag, size, nsegments, maxsegsz,
    878 				  boundary, flags, dmamp));
    879 }
    880 
    881 int
    882 sparc_vme4_dmamap_load(t, map, buf, buflen, p, flags)
    883 	bus_dma_tag_t t;
    884 	bus_dmamap_t map;
    885 	void *buf;
    886 	bus_size_t buflen;
    887 	struct proc *p;
    888 	int flags;
    889 {
    890 	bus_addr_t dva;
    891 	bus_size_t sgsize;
    892 	vaddr_t va, voff;
    893 	pmap_t pmap;
    894 	int pagesz = PAGE_SIZE;
    895 	int error;
    896 
    897 	cpuinfo.cache_flush(buf, buflen); /* XXX - move to bus_dma_sync */
    898 
    899 	va = (vaddr_t)buf;
    900 	voff = va & (pagesz - 1);
    901 	va &= -pagesz;
    902 
    903 	/*
    904 	 * Allocate an integral number of pages from DVMA space
    905 	 * covering the passed buffer.
    906 	 */
    907 	sgsize = (buflen + voff + pagesz - 1) & -pagesz;
    908 	error = extent_alloc(vme_dvmamap, sgsize, pagesz,
    909 			     map->_dm_boundary,
    910 			     (flags & BUS_DMA_NOWAIT) == 0
    911 					? EX_WAITOK
    912 					: EX_NOWAIT,
    913 			     (u_long *)&dva);
    914 	if (error != 0)
    915 		return (error);
    916 
    917 	map->dm_mapsize = buflen;
    918 	map->dm_nsegs = 1;
    919 	/* Adjust DVMA address to VME view */
    920 	map->dm_segs[0].ds_addr = dva + voff - VME4_DVMA_BASE;
    921 	map->dm_segs[0].ds_len = buflen;
    922 	map->dm_segs[0]._ds_sgsize = sgsize;
    923 
    924 	pmap = (p == NULL) ? pmap_kernel() : p->p_vmspace->vm_map.pmap;
    925 
    926 	for (; sgsize != 0; ) {
    927 		paddr_t pa;
    928 		/*
    929 		 * Get the physical address for this page.
    930 		 */
    931 		(void) pmap_extract(pmap, va, &pa);
    932 
    933 #ifdef notyet
    934 		if (have_iocache)
    935 			pa |= PG_IOC;
    936 #endif
    937 		pmap_enter(pmap_kernel(), dva,
    938 			   pa | PMAP_NC,
    939 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
    940 
    941 		dva += pagesz;
    942 		va += pagesz;
    943 		sgsize -= pagesz;
    944 	}
    945 
    946 	return (0);
    947 }
    948 
    949 void
    950 sparc_vme4_dmamap_unload(t, map)
    951 	bus_dma_tag_t t;
    952 	bus_dmamap_t map;
    953 {
    954 	bus_dma_segment_t *segs = map->dm_segs;
    955 	int nsegs = map->dm_nsegs;
    956 	bus_addr_t dva;
    957 	bus_size_t len;
    958 	int i, s, error;
    959 
    960 	for (i = 0; i < nsegs; i++) {
    961 		/* Go from VME to CPU view */
    962 		dva = segs[i].ds_addr + VME4_DVMA_BASE;
    963 		dva &= -PAGE_SIZE;
    964 		len = segs[i]._ds_sgsize;
    965 
    966 		/* Remove double-mapping in DVMA space */
    967 		pmap_remove(pmap_kernel(), dva, dva + len);
    968 
    969 		/* Release DVMA space */
    970 		s = splhigh();
    971 		error = extent_free(vme_dvmamap, dva, len, EX_NOWAIT);
    972 		splx(s);
    973 		if (error != 0)
    974 			printf("warning: %ld of DVMA space lost\n", len);
    975 	}
    976 
    977 	/* Mark the mappings as invalid. */
    978 	map->dm_mapsize = 0;
    979 	map->dm_nsegs = 0;
    980 }
    981 
    982 void
    983 sparc_vme4_dmamap_sync(t, map, offset, len, ops)
    984 	bus_dma_tag_t t;
    985 	bus_dmamap_t map;
    986 	bus_addr_t offset;
    987 	bus_size_t len;
    988 	int ops;
    989 {
    990 
    991 	/*
    992 	 * XXX Should perform cache flushes as necessary (e.g. 4/200 W/B).
    993 	 *     Currently the cache is flushed in bus_dma_load()...
    994 	 */
    995 }
    996 #endif /* SUN4 */
    997 
    998 #if defined(SUN4M)
    999 static int
   1000 sparc_vme_iommu_dmamap_create (t, size, nsegments, maxsegsz,
   1001 				boundary, flags, dmamp)
   1002 	bus_dma_tag_t t;
   1003 	bus_size_t size;
   1004 	int nsegments;
   1005 	bus_size_t maxsegsz;
   1006 	bus_size_t boundary;
   1007 	int flags;
   1008 	bus_dmamap_t *dmamp;
   1009 {
   1010 
   1011 	printf("sparc_vme_dmamap_create: please use `vme_dmamap_create'\n");
   1012 	return (EINVAL);
   1013 }
   1014 
   1015 static int
   1016 sparc_vct_iommu_dmamap_create(cookie, size, am, datasize, swap, nsegments,
   1017 			      maxsegsz, boundary, flags, dmamp)
   1018 	void *cookie;
   1019 	vme_size_t size;
   1020 	vme_am_t am;
   1021 	vme_datasize_t datasize;
   1022 	vme_swap_t swap;
   1023 	int nsegments;
   1024 	vme_size_t maxsegsz;
   1025 	vme_addr_t boundary;
   1026 	int flags;
   1027 	bus_dmamap_t *dmamp;
   1028 {
   1029 	struct sparcvme_softc *sc = (struct sparcvme_softc *)cookie;
   1030 	bus_dmamap_t map;
   1031 	int error;
   1032 
   1033 	/* Allocate a base map through parent bus ops */
   1034 	error = bus_dmamap_create(sc->sc_dmatag, size, nsegments, maxsegsz,
   1035 				  boundary, flags, &map);
   1036 	if (error != 0)
   1037 		return (error);
   1038 
   1039 	/*
   1040 	 * Each I/O cache line maps to a 8K section of VME DVMA space, so
   1041 	 * we must ensure that DVMA alloctions are always 8K aligned.
   1042 	 */
   1043 	map->_dm_align = VME_IOC_PAGESZ;
   1044 
   1045 	/* Set map region based on Address Modifier */
   1046 	switch ((am & VME_AM_ADRSIZEMASK)) {
   1047 	case VME_AM_A16:
   1048 	case VME_AM_A24:
   1049 		/* 1 MB of DVMA space */
   1050 		map->_dm_ex_start = VME_IOMMU_DVMA_AM24_BASE;
   1051 		map->_dm_ex_end   = VME_IOMMU_DVMA_AM24_END;
   1052 		break;
   1053 	case VME_AM_A32:
   1054 		/* 8 MB of DVMA space */
   1055 		map->_dm_ex_start = VME_IOMMU_DVMA_AM32_BASE;
   1056 		map->_dm_ex_end   = VME_IOMMU_DVMA_AM32_END;
   1057 		break;
   1058 	}
   1059 
   1060 	*dmamp = map;
   1061 	return (0);
   1062 }
   1063 
   1064 int
   1065 sparc_vme_iommu_dmamap_load(t, map, buf, buflen, p, flags)
   1066 	bus_dma_tag_t t;
   1067 	bus_dmamap_t map;
   1068 	void *buf;
   1069 	bus_size_t buflen;
   1070 	struct proc *p;
   1071 	int flags;
   1072 {
   1073 	struct sparcvme_softc	*sc = (struct sparcvme_softc *)t->_cookie;
   1074 	volatile u_int32_t	*ioctags;
   1075 	int			error;
   1076 
   1077 	/* Round request to a multiple of the I/O cache size */
   1078 	buflen = (buflen + VME_IOC_PAGESZ - 1) & -VME_IOC_PAGESZ;
   1079 	error = bus_dmamap_load(sc->sc_dmatag, map, buf, buflen, p, flags);
   1080 	if (error != 0)
   1081 		return (error);
   1082 
   1083 	/* Allocate I/O cache entries for this range */
   1084 	ioctags = sc->sc_ioctags + VME_IOC_LINE(map->dm_segs[0].ds_addr);
   1085 	while (buflen > 0) {
   1086 		*ioctags = VME_IOC_IC | VME_IOC_W;
   1087 		ioctags += VME_IOC_LINESZ/sizeof(*ioctags);
   1088 		buflen -= VME_IOC_PAGESZ;
   1089 	}
   1090 
   1091 	/*
   1092 	 * Adjust DVMA address to VME view.
   1093 	 * Note: the DVMA base address is the same for all
   1094 	 * VME address spaces.
   1095 	 */
   1096 	map->dm_segs[0].ds_addr -= VME_IOMMU_DVMA_BASE;
   1097 	return (0);
   1098 }
   1099 
   1100 
   1101 void
   1102 sparc_vme_iommu_dmamap_unload(t, map)
   1103 	bus_dma_tag_t t;
   1104 	bus_dmamap_t map;
   1105 {
   1106 	struct sparcvme_softc	*sc = (struct sparcvme_softc *)t->_cookie;
   1107 	volatile u_int32_t	*flushregs;
   1108 	int			len;
   1109 
   1110 	/* Go from VME to CPU view */
   1111 	map->dm_segs[0].ds_addr += VME_IOMMU_DVMA_BASE;
   1112 
   1113 	/* Flush VME I/O cache */
   1114 	len = map->dm_segs[0]._ds_sgsize;
   1115 	flushregs = sc->sc_iocflush + VME_IOC_LINE(map->dm_segs[0].ds_addr);
   1116 	while (len > 0) {
   1117 		*flushregs = 0;
   1118 		flushregs += VME_IOC_LINESZ/sizeof(*flushregs);
   1119 		len -= VME_IOC_PAGESZ;
   1120 	}
   1121 
   1122 	/*
   1123 	 * Start a read from `tag space' which will not complete until
   1124 	 * all cache flushes have finished
   1125 	 */
   1126 	(*sc->sc_ioctags);
   1127 
   1128 	bus_dmamap_unload(sc->sc_dmatag, map);
   1129 }
   1130 
   1131 void
   1132 sparc_vme_iommu_dmamap_sync(t, map, offset, len, ops)
   1133 	bus_dma_tag_t t;
   1134 	bus_dmamap_t map;
   1135 	bus_addr_t offset;
   1136 	bus_size_t len;
   1137 	int ops;
   1138 {
   1139 
   1140 	/*
   1141 	 * XXX Should perform cache flushes as necessary.
   1142 	 */
   1143 }
   1144 #endif /* SUN4M */
   1145 
   1146 int
   1147 sparc_vme_dmamem_map(t, segs, nsegs, size, kvap, flags)
   1148 	bus_dma_tag_t t;
   1149 	bus_dma_segment_t *segs;
   1150 	int nsegs;
   1151 	size_t size;
   1152 	caddr_t *kvap;
   1153 	int flags;
   1154 {
   1155 	struct sparcvme_softc	*sc = (struct sparcvme_softc *)t->_cookie;
   1156 
   1157 	return (bus_dmamem_map(sc->sc_dmatag, segs, nsegs, size, kvap, flags));
   1158 }
   1159