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