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