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