vsbus.c revision 1.57 1 /* $NetBSD: vsbus.c,v 1.57 2009/10/26 19:16:58 cegger 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.57 2009/10/26 19:16:58 cegger 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/user.h>
46 #include <sys/device.h>
47 #include <sys/disklabel.h>
48 #include <sys/syslog.h>
49 #include <sys/stat.h>
50
51 #include <uvm/uvm_extern.h>
52
53 #define _VAX_BUS_DMA_PRIVATE
54 #include <machine/bus.h>
55 #include <machine/pte.h>
56 #include <machine/sid.h>
57 #include <machine/scb.h>
58 #include <machine/cpu.h>
59 #include <machine/trap.h>
60 #include <machine/nexus.h>
61
62 #include <machine/uvax.h>
63 #include <machine/ka410.h>
64 #include <machine/ka420.h>
65 #include <machine/ka43.h>
66
67 #include <machine/mainbus.h>
68 #include <machine/vsbus.h>
69
70 #include "ioconf.h"
71 #include "locators.h"
72 #include "opt_cputype.h"
73
74 static int vsbus_match(device_t, cfdata_t, void *);
75 static void vsbus_attach(device_t, device_t, void *);
76 static int vsbus_print(void *, const char *);
77 static int vsbus_search(device_t, cfdata_t, const int *, void *);
78
79 static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
80
81 CFATTACH_DECL_NEW(vsbus, sizeof(struct vsbus_softc),
82 vsbus_match, vsbus_attach, NULL, NULL);
83
84 static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
85 ._dmamap_create = _bus_dmamap_create,
86 ._dmamap_destroy = _bus_dmamap_destroy,
87 ._dmamap_load = _bus_dmamap_load,
88 ._dmamap_load_mbuf = _bus_dmamap_load_mbuf,
89 ._dmamap_load_uio = _bus_dmamap_load_uio,
90 ._dmamap_load_raw = _bus_dmamap_load_raw,
91 ._dmamap_unload = _bus_dmamap_unload,
92 ._dmamap_sync = _bus_dmamap_sync,
93 ._dmamem_alloc = _bus_dmamem_alloc,
94 ._dmamem_free = _bus_dmamem_free,
95 ._dmamem_map = _bus_dmamem_map,
96 ._dmamem_unmap = _bus_dmamem_unmap,
97 ._dmamem_mmap = _bus_dmamem_mmap,
98 };
99
100 int
101 vsbus_print(void *aux, const char *name)
102 {
103 struct vsbus_attach_args * const va = aux;
104
105 aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
106 va->va_cvec & 511, va->va_br, va->va_maskno - 1);
107 return UNCONF;
108 }
109
110 int
111 vsbus_match(device_t parent, cfdata_t cf, void *aux)
112 {
113 struct mainbus_attach_args * const ma = aux;
114
115 return !strcmp("vsbus", ma->ma_type);
116 }
117
118 void
119 vsbus_attach(device_t parent, device_t self, void *aux)
120 {
121 struct mainbus_attach_args * const ma = aux;
122 struct vsbus_softc *sc = device_private(self);
123 int dbase, dsize;
124
125 aprint_normal("\n");
126
127 sc->sc_dev = self;
128 sc->sc_iot = ma->ma_iot;
129 sc->sc_dmatag = vsbus_bus_dma_tag;
130
131 switch (vax_boardtype) {
132 #if VAX49 || VAX53
133 case VAX_BTYP_53:
134 case VAX_BTYP_49:
135 sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
136 sc->sc_intreq = (char *)sc->sc_vsregs + 12;
137 sc->sc_intclr = (char *)sc->sc_vsregs + 12;
138 sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
139 vsbus_dma_init(sc, 8192);
140 break;
141 #endif
142
143 #if VAX46 || VAX48
144 case VAX_BTYP_48:
145 case VAX_BTYP_46:
146 sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
147 sc->sc_intreq = (char *)sc->sc_vsregs + 15;
148 sc->sc_intclr = (char *)sc->sc_vsregs + 15;
149 sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
150 vsbus_dma_init(sc, 32768);
151 #endif
152
153 default:
154 sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
155 sc->sc_intreq = (char *)sc->sc_vsregs + 15;
156 sc->sc_intclr = (char *)sc->sc_vsregs + 15;
157 sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
158 if (vax_boardtype == VAX_BTYP_410) {
159 dbase = KA410_DMA_BASE;
160 dsize = KA410_DMA_SIZE;
161 } else {
162 dbase = KA420_DMA_BASE;
163 dsize = KA420_DMA_SIZE;
164 *(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
165 }
166 sc->sc_dmasize = dsize;
167 sc->sc_dmaaddr = uvm_km_alloc(kernel_map, dsize, 0,
168 UVM_KMF_VAONLY);
169 ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
170 break;
171 }
172
173 SIMPLEQ_INIT(&vsbus_dma);
174 /*
175 * First: find which interrupts we won't care about.
176 * There are interrupts that interrupt on a periodic basic
177 * that we don't want to interfere with the rest of the
178 * interrupt probing.
179 */
180 *sc->sc_intmsk = 0;
181 *sc->sc_intclr = 0xff;
182 DELAY(1000000); /* Wait a second */
183 sc->sc_mask = *sc->sc_intreq;
184 aprint_normal_dev(self, "interrupt mask %x\n", sc->sc_mask);
185 /*
186 * now check for all possible devices on this "bus"
187 */
188 config_search_ia(vsbus_search, self, "vsbus", NULL);
189
190 /* Autoconfig finished, enable interrupts */
191 *sc->sc_intmsk = ~sc->sc_mask;
192 }
193
194 int
195 vsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
196 {
197 struct vsbus_softc *sc = device_private(parent);
198 struct vsbus_attach_args va;
199 int i, vec, br;
200 u_char c;
201
202 va.va_paddr = cf->cf_loc[VSBUSCF_CSR];
203 va.va_addr = vax_map_physmem(va.va_paddr, 1);
204 va.va_dmat = &sc->sc_dmatag;
205 va.va_memt = sc->sc_iot;
206
207 *sc->sc_intmsk = 0;
208 *sc->sc_intclr = 0xff;
209 scb_vecref(0, 0); /* Clear vector ref */
210
211 i = config_match(parent, cf, &va);
212 vax_unmap_physmem(va.va_addr, 1);
213 c = *sc->sc_intreq & ~sc->sc_mask;
214 if (i == 0)
215 goto forgetit;
216 if (i > 10)
217 c = sc->sc_mask; /* Fooling interrupt */
218 else if (c == 0)
219 goto forgetit;
220
221 *sc->sc_intmsk = c;
222 DELAY(100);
223 *sc->sc_intmsk = 0;
224 va.va_maskno = ffs((u_int)c);
225 i = scb_vecref(&vec, &br);
226 if (i == 0)
227 goto fail;
228 if (vec == 0)
229 goto fail;
230
231 va.va_br = br;
232 va.va_cvec = vec;
233 va.va_dmaaddr = sc->sc_dmaaddr;
234 va.va_dmasize = sc->sc_dmasize;
235 *sc->sc_intmsk = c; /* Allow interrupts during attach */
236 config_attach(parent, cf, &va, vsbus_print);
237 *sc->sc_intmsk = 0;
238 return 0;
239
240 fail:
241 printf("%s%d at %s csr 0x%x %s\n",
242 cf->cf_name, cf->cf_unit, device_xname(parent),
243 cf->cf_loc[VSBUSCF_CSR], (i ? "zero vector" : "didn't interrupt"));
244 forgetit:
245 return 0;
246 }
247
248 /*
249 * Sets a new interrupt mask. Returns the old one.
250 * Works like spl functions.
251 */
252 unsigned char
253 vsbus_setmask(int mask)
254 {
255 struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
256 unsigned char ch;
257
258 if (sc == NULL)
259 return 0;
260
261 ch = *sc->sc_intmsk;
262 *sc->sc_intmsk = mask;
263 return ch;
264 }
265
266 /*
267 * Clears the interrupts in mask.
268 */
269 void
270 vsbus_clrintr(int mask)
271 {
272 struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
273 if (sc == NULL)
274 return;
275
276 *sc->sc_intclr = mask;
277 }
278
279 /*
280 * Copy data from/to a user process' space from the DMA area.
281 * Use the physical memory directly.
282 */
283 void
284 vsbus_copytoproc(struct proc *p, void *fromv, void *tov, int len)
285 {
286 char *from = fromv, *to = tov;
287 struct pte *pte;
288 paddr_t pa;
289
290 if ((vaddr_t)to & KERNBASE) { /* In kernel space */
291 memcpy(to, from, len);
292 return;
293 }
294
295 #ifdef DIAGNOSTIC
296 if (p == NULL)
297 panic("vsbus_copytoproc: no proc");
298 #endif
299
300 if ((vaddr_t)to & 0x40000000)
301 pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)to & ~0x40000000)];
302 else
303 pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)to)];
304 if ((vaddr_t)to & PGOFSET) {
305 int cz = round_page((vaddr_t)to) - (vaddr_t)to;
306
307 pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
308 memcpy((void *)pa, from, min(cz, len));
309 from += cz;
310 to += cz;
311 len -= cz;
312 pte += 8; /* XXX */
313 }
314 while (len > 0) {
315 pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
316 memcpy((void *)pa, from, min(PAGE_SIZE, len));
317 from += PAGE_SIZE;
318 to += PAGE_SIZE;
319 len -= PAGE_SIZE;
320 pte += 8; /* XXX */
321 }
322 }
323
324 void
325 vsbus_copyfromproc(struct proc *p, void *fromv, void *tov, int len)
326 {
327 char *from = fromv, *to = tov;
328 struct pte *pte;
329 paddr_t pa;
330
331 if ((vaddr_t)from & KERNBASE) { /* In kernel space */
332 memcpy(to, from, len);
333 return;
334 }
335
336 #ifdef DIAGNOSTIC
337 if (p == NULL)
338 panic("vsbus_copyfromproc: no proc");
339 #endif
340
341 if ((vaddr_t)from & 0x40000000)
342 pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)from & ~0x40000000)];
343 else
344 pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)from)];
345 if ((vaddr_t)from & PGOFSET) {
346 int cz = round_page((vaddr_t)from) - (vaddr_t)from;
347
348 pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
349 memcpy(to, (void *)pa, min(cz, len));
350 from += cz;
351 to += cz;
352 len -= cz;
353 pte += 8; /* XXX */
354 }
355 while (len > 0) {
356 pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
357 memcpy(to, (void *)pa, min(PAGE_SIZE, len));
358 from += PAGE_SIZE;
359 to += PAGE_SIZE;
360 len -= PAGE_SIZE;
361 pte += 8; /* XXX */
362 }
363 }
364
365 /*
366 * There can only be one user of the DMA area on VS2k/VS3100 at one
367 * time, so keep track of it here.
368 */
369 static int vsbus_active = 0;
370
371 void
372 vsbus_dma_start(struct vsbus_dma *vd)
373 {
374
375 SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
376
377 if (vsbus_active == 0)
378 vsbus_dma_intr();
379 }
380
381 void
382 vsbus_dma_intr(void)
383 {
384 struct vsbus_dma *vd;
385
386 vd = SIMPLEQ_FIRST(&vsbus_dma);
387 if (vd == NULL) {
388 vsbus_active = 0;
389 return;
390 }
391 vsbus_active = 1;
392 SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
393 (*vd->vd_go)(vd->vd_arg);
394 }
395
396