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