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