xref: /netbsd-src/sys/arch/vax/vsa/vsbus.c (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 /*	$NetBSD: vsbus.c,v 1.58 2009/11/21 04:45:39 rmind Exp $ */
2 /*
3  * Copyright (c) 1996, 1999 Ludd, University of Lule}, Sweden.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Ludd by Bertram Barth.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed at Ludd, University of
19  *	Lule}, Sweden and its contributors.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: vsbus.c,v 1.58 2009/11/21 04:45:39 rmind Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/proc.h>
45 #include <sys/device.h>
46 #include <sys/disklabel.h>
47 #include <sys/syslog.h>
48 #include <sys/stat.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #define _VAX_BUS_DMA_PRIVATE
53 #include <machine/bus.h>
54 #include <machine/pte.h>
55 #include <machine/sid.h>
56 #include <machine/scb.h>
57 #include <machine/cpu.h>
58 #include <machine/trap.h>
59 #include <machine/nexus.h>
60 
61 #include <machine/uvax.h>
62 #include <machine/ka410.h>
63 #include <machine/ka420.h>
64 #include <machine/ka43.h>
65 
66 #include <machine/mainbus.h>
67 #include <machine/vsbus.h>
68 
69 #include "ioconf.h"
70 #include "locators.h"
71 #include "opt_cputype.h"
72 
73 static int	vsbus_match(device_t, cfdata_t, void *);
74 static void	vsbus_attach(device_t, device_t, void *);
75 static int	vsbus_print(void *, const char *);
76 static int	vsbus_search(device_t, cfdata_t, const int *, void *);
77 
78 static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
79 
80 CFATTACH_DECL_NEW(vsbus, sizeof(struct vsbus_softc),
81     vsbus_match, vsbus_attach, NULL, NULL);
82 
83 static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
84 	._dmamap_create		= _bus_dmamap_create,
85 	._dmamap_destroy	= _bus_dmamap_destroy,
86 	._dmamap_load		= _bus_dmamap_load,
87 	._dmamap_load_mbuf	= _bus_dmamap_load_mbuf,
88 	._dmamap_load_uio	= _bus_dmamap_load_uio,
89 	._dmamap_load_raw	= _bus_dmamap_load_raw,
90 	._dmamap_unload		= _bus_dmamap_unload,
91 	._dmamap_sync		= _bus_dmamap_sync,
92 	._dmamem_alloc		= _bus_dmamem_alloc,
93 	._dmamem_free		= _bus_dmamem_free,
94 	._dmamem_map		= _bus_dmamem_map,
95 	._dmamem_unmap		= _bus_dmamem_unmap,
96 	._dmamem_mmap		= _bus_dmamem_mmap,
97 };
98 
99 int
100 vsbus_print(void *aux, const char *name)
101 {
102 	struct vsbus_attach_args * const va = aux;
103 
104 	aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
105 	    va->va_cvec & 511, va->va_br, va->va_maskno - 1);
106 	return UNCONF;
107 }
108 
109 int
110 vsbus_match(device_t parent, cfdata_t cf, void *aux)
111 {
112 	struct mainbus_attach_args * const ma = aux;
113 
114 	return !strcmp("vsbus", ma->ma_type);
115 }
116 
117 void
118 vsbus_attach(device_t parent, device_t self, void *aux)
119 {
120 	struct mainbus_attach_args * const ma = aux;
121 	struct vsbus_softc *sc = device_private(self);
122 	int dbase, dsize;
123 
124 	aprint_normal("\n");
125 
126 	sc->sc_dev = self;
127 	sc->sc_iot = ma->ma_iot;
128 	sc->sc_dmatag = vsbus_bus_dma_tag;
129 
130 	switch (vax_boardtype) {
131 #if VAX49 || VAX53
132 	case VAX_BTYP_53:
133 	case VAX_BTYP_49:
134 		sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
135 		sc->sc_intreq = (char *)sc->sc_vsregs + 12;
136 		sc->sc_intclr = (char *)sc->sc_vsregs + 12;
137 		sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
138 		vsbus_dma_init(sc, 8192);
139 		break;
140 #endif
141 
142 #if VAX46 || VAX48
143 	case VAX_BTYP_48:
144 	case VAX_BTYP_46:
145 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
146 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
147 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
148 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
149 		vsbus_dma_init(sc, 32768);
150 #endif
151 
152 	default:
153 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
154 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
155 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
156 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
157 		if (vax_boardtype == VAX_BTYP_410) {
158 			dbase = KA410_DMA_BASE;
159 			dsize = KA410_DMA_SIZE;
160 		} else {
161 			dbase = KA420_DMA_BASE;
162 			dsize = KA420_DMA_SIZE;
163 			*(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
164 		}
165 		sc->sc_dmasize = dsize;
166 		sc->sc_dmaaddr = uvm_km_alloc(kernel_map, dsize, 0,
167 		    UVM_KMF_VAONLY);
168 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
169 		break;
170 	}
171 
172 	SIMPLEQ_INIT(&vsbus_dma);
173 	/*
174 	 * First: find which interrupts we won't care about.
175 	 * There are interrupts that interrupt on a periodic basic
176 	 * that we don't want to interfere with the rest of the
177 	 * interrupt probing.
178 	 */
179 	*sc->sc_intmsk = 0;
180 	*sc->sc_intclr = 0xff;
181 	DELAY(1000000); /* Wait a second */
182 	sc->sc_mask = *sc->sc_intreq;
183 	aprint_normal_dev(self, "interrupt mask %x\n", sc->sc_mask);
184 	/*
185 	 * now check for all possible devices on this "bus"
186 	 */
187 	config_search_ia(vsbus_search, self, "vsbus", NULL);
188 
189 	/* Autoconfig finished, enable interrupts */
190 	*sc->sc_intmsk = ~sc->sc_mask;
191 }
192 
193 int
194 vsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
195 {
196 	struct vsbus_softc *sc = device_private(parent);
197 	struct vsbus_attach_args va;
198 	int i, vec, br;
199 	u_char c;
200 
201 	va.va_paddr = cf->cf_loc[VSBUSCF_CSR];
202 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
203 	va.va_dmat = &sc->sc_dmatag;
204 	va.va_memt = sc->sc_iot;
205 
206 	*sc->sc_intmsk = 0;
207 	*sc->sc_intclr = 0xff;
208 	scb_vecref(0, 0); /* Clear vector ref */
209 
210 	i = config_match(parent, cf, &va);
211 	vax_unmap_physmem(va.va_addr, 1);
212 	c = *sc->sc_intreq & ~sc->sc_mask;
213 	if (i == 0)
214 		goto forgetit;
215 	if (i > 10)
216 		c = sc->sc_mask; /* Fooling interrupt */
217 	else if (c == 0)
218 		goto forgetit;
219 
220 	*sc->sc_intmsk = c;
221 	DELAY(100);
222 	*sc->sc_intmsk = 0;
223 	va.va_maskno = ffs((u_int)c);
224 	i = scb_vecref(&vec, &br);
225 	if (i == 0)
226 		goto fail;
227 	if (vec == 0)
228 		goto fail;
229 
230 	va.va_br = br;
231 	va.va_cvec = vec;
232 	va.va_dmaaddr = sc->sc_dmaaddr;
233 	va.va_dmasize = sc->sc_dmasize;
234 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
235 	config_attach(parent, cf, &va, vsbus_print);
236 	*sc->sc_intmsk = 0;
237 	return 0;
238 
239 fail:
240 	printf("%s%d at %s csr 0x%x %s\n",
241 	    cf->cf_name, cf->cf_unit, device_xname(parent),
242 	    cf->cf_loc[VSBUSCF_CSR], (i ? "zero vector" : "didn't interrupt"));
243 forgetit:
244 	return 0;
245 }
246 
247 /*
248  * Sets a new interrupt mask. Returns the old one.
249  * Works like spl functions.
250  */
251 unsigned char
252 vsbus_setmask(int mask)
253 {
254 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
255 	unsigned char ch;
256 
257 	if (sc == NULL)
258 		return 0;
259 
260 	ch = *sc->sc_intmsk;
261 	*sc->sc_intmsk = mask;
262 	return ch;
263 }
264 
265 /*
266  * Clears the interrupts in mask.
267  */
268 void
269 vsbus_clrintr(int mask)
270 {
271 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
272 	if (sc == NULL)
273 		return;
274 
275 	*sc->sc_intclr = mask;
276 }
277 
278 /*
279  * Copy data from/to a user process' space from the DMA area.
280  * Use the physical memory directly.
281  */
282 void
283 vsbus_copytoproc(struct proc *p, void *fromv, void *tov, int len)
284 {
285 	char *from = fromv, *to = tov;
286 	struct pte *pte;
287 	paddr_t pa;
288 
289 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
290 		memcpy(to, from, len);
291 		return;
292 	}
293 
294 #ifdef DIAGNOSTIC
295 	if (p == NULL)
296 		panic("vsbus_copytoproc: no proc");
297 #endif
298 
299 	if ((vaddr_t)to & 0x40000000)
300 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)to & ~0x40000000)];
301 	else
302 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)to)];
303 	if ((vaddr_t)to & PGOFSET) {
304 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
305 
306 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
307 		memcpy((void *)pa, from, min(cz, len));
308 		from += cz;
309 		to += cz;
310 		len -= cz;
311 		pte += 8; /* XXX */
312 	}
313 	while (len > 0) {
314 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
315 		memcpy((void *)pa, from, min(PAGE_SIZE, len));
316 		from += PAGE_SIZE;
317 		to += PAGE_SIZE;
318 		len -= PAGE_SIZE;
319 		pte += 8; /* XXX */
320 	}
321 }
322 
323 void
324 vsbus_copyfromproc(struct proc *p, void *fromv, void *tov, int len)
325 {
326 	char *from = fromv, *to = tov;
327 	struct pte *pte;
328 	paddr_t pa;
329 
330 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
331 		memcpy(to, from, len);
332 		return;
333 	}
334 
335 #ifdef DIAGNOSTIC
336 	if (p == NULL)
337 		panic("vsbus_copyfromproc: no proc");
338 #endif
339 
340 	if ((vaddr_t)from & 0x40000000)
341 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)from & ~0x40000000)];
342 	else
343 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)from)];
344 	if ((vaddr_t)from & PGOFSET) {
345 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
346 
347 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
348 		memcpy(to, (void *)pa, min(cz, len));
349 		from += cz;
350 		to += cz;
351 		len -= cz;
352 		pte += 8; /* XXX */
353 	}
354 	while (len > 0) {
355 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
356 		memcpy(to,  (void *)pa, min(PAGE_SIZE, len));
357 		from += PAGE_SIZE;
358 		to += PAGE_SIZE;
359 		len -= PAGE_SIZE;
360 		pte += 8; /* XXX */
361 	}
362 }
363 
364 /*
365  * There can only be one user of the DMA area on VS2k/VS3100 at one
366  * time, so keep track of it here.
367  */
368 static int vsbus_active = 0;
369 
370 void
371 vsbus_dma_start(struct vsbus_dma *vd)
372 {
373 
374 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
375 
376 	if (vsbus_active == 0)
377 		vsbus_dma_intr();
378 }
379 
380 void
381 vsbus_dma_intr(void)
382 {
383 	struct vsbus_dma *vd;
384 
385 	vd = SIMPLEQ_FIRST(&vsbus_dma);
386 	if (vd == NULL) {
387 		vsbus_active = 0;
388 		return;
389 	}
390 	vsbus_active = 1;
391 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
392 	(*vd->vd_go)(vd->vd_arg);
393 }
394 
395