sbus.c revision 1.27 1 1.27 mrg /* $NetBSD: sbus.c,v 1.27 2000/04/22 12:36:29 mrg Exp $ */
2 1.1 eeh
3 1.1 eeh /*-
4 1.1 eeh * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 eeh * All rights reserved.
6 1.1 eeh *
7 1.1 eeh * This code is derived from software contributed to The NetBSD Foundation
8 1.1 eeh * by Paul Kranenburg.
9 1.1 eeh *
10 1.1 eeh * Redistribution and use in source and binary forms, with or without
11 1.1 eeh * modification, are permitted provided that the following conditions
12 1.1 eeh * are met:
13 1.1 eeh * 1. Redistributions of source code must retain the above copyright
14 1.1 eeh * notice, this list of conditions and the following disclaimer.
15 1.1 eeh * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 eeh * notice, this list of conditions and the following disclaimer in the
17 1.1 eeh * documentation and/or other materials provided with the distribution.
18 1.1 eeh * 3. All advertising materials mentioning features or use of this software
19 1.1 eeh * must display the following acknowledgement:
20 1.1 eeh * This product includes software developed by the NetBSD
21 1.1 eeh * Foundation, Inc. and its contributors.
22 1.1 eeh * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 eeh * contributors may be used to endorse or promote products derived
24 1.1 eeh * from this software without specific prior written permission.
25 1.1 eeh *
26 1.1 eeh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 eeh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 eeh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 eeh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 eeh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 eeh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 eeh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 eeh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 eeh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 eeh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 eeh * POSSIBILITY OF SUCH DAMAGE.
37 1.1 eeh */
38 1.1 eeh
39 1.1 eeh /*
40 1.1 eeh * Copyright (c) 1992, 1993
41 1.1 eeh * The Regents of the University of California. All rights reserved.
42 1.1 eeh *
43 1.1 eeh * This software was developed by the Computer Systems Engineering group
44 1.1 eeh * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 1.1 eeh * contributed to Berkeley.
46 1.1 eeh *
47 1.1 eeh * All advertising materials mentioning features or use of this software
48 1.1 eeh * must display the following acknowledgement:
49 1.1 eeh * This product includes software developed by the University of
50 1.1 eeh * California, Lawrence Berkeley Laboratory.
51 1.1 eeh *
52 1.1 eeh * Redistribution and use in source and binary forms, with or without
53 1.1 eeh * modification, are permitted provided that the following conditions
54 1.1 eeh * are met:
55 1.1 eeh * 1. Redistributions of source code must retain the above copyright
56 1.1 eeh * notice, this list of conditions and the following disclaimer.
57 1.1 eeh * 2. Redistributions in binary form must reproduce the above copyright
58 1.1 eeh * notice, this list of conditions and the following disclaimer in the
59 1.1 eeh * documentation and/or other materials provided with the distribution.
60 1.1 eeh * 3. All advertising materials mentioning features or use of this software
61 1.1 eeh * must display the following acknowledgement:
62 1.1 eeh * This product includes software developed by the University of
63 1.1 eeh * California, Berkeley and its contributors.
64 1.1 eeh * 4. Neither the name of the University nor the names of its contributors
65 1.1 eeh * may be used to endorse or promote products derived from this software
66 1.1 eeh * without specific prior written permission.
67 1.1 eeh *
68 1.1 eeh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 1.1 eeh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 1.1 eeh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 1.1 eeh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 1.1 eeh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 1.1 eeh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 1.1 eeh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 1.1 eeh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 1.1 eeh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 1.1 eeh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 1.1 eeh * SUCH DAMAGE.
79 1.1 eeh *
80 1.1 eeh * @(#)sbus.c 8.1 (Berkeley) 6/11/93
81 1.1 eeh */
82 1.1 eeh
83 1.1 eeh /*
84 1.18 eeh * Copyright (c) 1999 Eduardo Horvath
85 1.18 eeh *
86 1.18 eeh * Redistribution and use in source and binary forms, with or without
87 1.18 eeh * modification, are permitted provided that the following conditions
88 1.18 eeh * are met:
89 1.18 eeh * 1. Redistributions of source code must retain the above copyright
90 1.18 eeh * notice, this list of conditions and the following disclaimer.
91 1.18 eeh *
92 1.18 eeh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
93 1.18 eeh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 1.18 eeh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95 1.18 eeh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
96 1.18 eeh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97 1.18 eeh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98 1.18 eeh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 1.18 eeh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100 1.18 eeh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101 1.18 eeh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102 1.18 eeh * SUCH DAMAGE.
103 1.18 eeh *
104 1.18 eeh */
105 1.18 eeh
106 1.18 eeh
107 1.18 eeh /*
108 1.1 eeh * Sbus stuff.
109 1.1 eeh */
110 1.8 eeh #include "opt_ddb.h"
111 1.1 eeh
112 1.1 eeh #include <sys/param.h>
113 1.12 eeh #include <sys/extent.h>
114 1.1 eeh #include <sys/malloc.h>
115 1.1 eeh #include <sys/systm.h>
116 1.1 eeh #include <sys/device.h>
117 1.1 eeh #include <vm/vm.h>
118 1.1 eeh
119 1.1 eeh #include <machine/bus.h>
120 1.2 eeh #include <sparc64/sparc64/vaddrs.h>
121 1.25 mrg #include <sparc64/sparc64/cache.h>
122 1.13 mrg #include <sparc64/dev/iommureg.h>
123 1.17 mrg #include <sparc64/dev/iommuvar.h>
124 1.1 eeh #include <sparc64/dev/sbusreg.h>
125 1.7 pk #include <dev/sbus/sbusvar.h>
126 1.1 eeh
127 1.1 eeh #include <machine/autoconf.h>
128 1.1 eeh #include <machine/ctlreg.h>
129 1.1 eeh #include <machine/cpu.h>
130 1.8 eeh #include <machine/sparc64.h>
131 1.1 eeh
132 1.1 eeh #ifdef DEBUG
133 1.1 eeh #define SDB_DVMA 0x1
134 1.1 eeh #define SDB_INTR 0x2
135 1.27 mrg int sbus_debug = 0;
136 1.27 mrg #define DPRINTF(l, s) do { if (sbus_debug & l) printf s; } while (0)
137 1.27 mrg #else
138 1.27 mrg #define DPRINTF(l, s)
139 1.1 eeh #endif
140 1.1 eeh
141 1.1 eeh void sbusreset __P((int));
142 1.1 eeh
143 1.1 eeh static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
144 1.1 eeh static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
145 1.3 eeh static int sbus_get_intr __P((struct sbus_softc *, int,
146 1.22 mrg struct sbus_intr **, int *, int));
147 1.1 eeh static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
148 1.1 eeh int, bus_space_handle_t *));
149 1.1 eeh static int _sbus_bus_map __P((
150 1.1 eeh bus_space_tag_t,
151 1.1 eeh bus_type_t,
152 1.1 eeh bus_addr_t, /*offset*/
153 1.1 eeh bus_size_t, /*size*/
154 1.1 eeh int, /*flags*/
155 1.3 eeh vaddr_t, /*preferred virtual address */
156 1.1 eeh bus_space_handle_t *));
157 1.1 eeh static void *sbus_intr_establish __P((
158 1.1 eeh bus_space_tag_t,
159 1.1 eeh int, /*level*/
160 1.1 eeh int, /*flags*/
161 1.1 eeh int (*) __P((void *)), /*handler*/
162 1.1 eeh void *)); /*handler arg*/
163 1.1 eeh
164 1.1 eeh
165 1.1 eeh /* autoconfiguration driver */
166 1.1 eeh int sbus_match __P((struct device *, struct cfdata *, void *));
167 1.1 eeh void sbus_attach __P((struct device *, struct device *, void *));
168 1.1 eeh
169 1.1 eeh
170 1.1 eeh struct cfattach sbus_ca = {
171 1.1 eeh sizeof(struct sbus_softc), sbus_match, sbus_attach
172 1.1 eeh };
173 1.1 eeh
174 1.1 eeh extern struct cfdriver sbus_cd;
175 1.1 eeh
176 1.1 eeh /*
177 1.1 eeh * DVMA routines
178 1.1 eeh */
179 1.1 eeh int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
180 1.1 eeh bus_size_t, struct proc *, int));
181 1.1 eeh void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
182 1.1 eeh void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
183 1.1 eeh bus_size_t, int));
184 1.1 eeh int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
185 1.1 eeh bus_size_t alignment, bus_size_t boundary,
186 1.1 eeh bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
187 1.1 eeh void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
188 1.1 eeh int nsegs));
189 1.2 eeh int sbus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
190 1.2 eeh int nsegs, size_t size, caddr_t *kvap, int flags));
191 1.2 eeh void sbus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
192 1.2 eeh size_t size));
193 1.1 eeh
194 1.1 eeh
195 1.1 eeh /*
196 1.1 eeh * Child devices receive the Sbus interrupt level in their attach
197 1.1 eeh * arguments. We translate these to CPU IPLs using the following
198 1.1 eeh * tables. Note: obio bus interrupt levels are identical to the
199 1.1 eeh * processor IPL.
200 1.1 eeh *
201 1.1 eeh * The second set of tables is used when the Sbus interrupt level
202 1.1 eeh * cannot be had from the PROM as an `interrupt' property. We then
203 1.1 eeh * fall back on the `intr' property which contains the CPU IPL.
204 1.1 eeh */
205 1.1 eeh
206 1.1 eeh /* Translate Sbus interrupt level to processor IPL */
207 1.1 eeh static int intr_sbus2ipl_4c[] = {
208 1.1 eeh 0, 1, 2, 3, 5, 7, 8, 9
209 1.1 eeh };
210 1.1 eeh static int intr_sbus2ipl_4m[] = {
211 1.1 eeh 0, 2, 3, 5, 7, 9, 11, 13
212 1.1 eeh };
213 1.1 eeh
214 1.1 eeh /*
215 1.1 eeh * This value is or'ed into the attach args' interrupt level cookie
216 1.1 eeh * if the interrupt level comes from an `intr' property, i.e. it is
217 1.1 eeh * not an Sbus interrupt level.
218 1.1 eeh */
219 1.1 eeh #define SBUS_INTR_COMPAT 0x80000000
220 1.1 eeh
221 1.1 eeh
222 1.1 eeh /*
223 1.1 eeh * Print the location of some sbus-attached device (called just
224 1.1 eeh * before attaching that device). If `sbus' is not NULL, the
225 1.1 eeh * device was found but not configured; print the sbus as well.
226 1.1 eeh * Return UNCONF (config_find ignores this if the device was configured).
227 1.1 eeh */
228 1.1 eeh int
229 1.1 eeh sbus_print(args, busname)
230 1.1 eeh void *args;
231 1.1 eeh const char *busname;
232 1.1 eeh {
233 1.1 eeh struct sbus_attach_args *sa = args;
234 1.3 eeh int i;
235 1.1 eeh
236 1.1 eeh if (busname)
237 1.1 eeh printf("%s at %s", sa->sa_name, busname);
238 1.8 eeh printf(" slot %ld offset 0x%lx", (long)sa->sa_slot,
239 1.8 eeh (u_long)sa->sa_offset);
240 1.22 mrg for (i = 0; i < sa->sa_nintr; i++) {
241 1.3 eeh struct sbus_intr *sbi = &sa->sa_intr[i];
242 1.1 eeh
243 1.8 eeh printf(" vector %lx ipl %ld",
244 1.8 eeh (u_long)sbi->sbi_vec,
245 1.8 eeh (long)INTLEV(sbi->sbi_pri));
246 1.1 eeh }
247 1.1 eeh return (UNCONF);
248 1.1 eeh }
249 1.1 eeh
250 1.1 eeh int
251 1.1 eeh sbus_match(parent, cf, aux)
252 1.1 eeh struct device *parent;
253 1.1 eeh struct cfdata *cf;
254 1.1 eeh void *aux;
255 1.1 eeh {
256 1.1 eeh struct mainbus_attach_args *ma = aux;
257 1.1 eeh
258 1.1 eeh return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
259 1.1 eeh }
260 1.1 eeh
261 1.1 eeh /*
262 1.1 eeh * Attach an Sbus.
263 1.1 eeh */
264 1.1 eeh void
265 1.1 eeh sbus_attach(parent, self, aux)
266 1.1 eeh struct device *parent;
267 1.1 eeh struct device *self;
268 1.1 eeh void *aux;
269 1.1 eeh {
270 1.9 eeh struct sbus_softc *sc = (struct sbus_softc *)self;
271 1.1 eeh struct mainbus_attach_args *ma = aux;
272 1.27 mrg char *name;
273 1.1 eeh int node = ma->ma_node;
274 1.1 eeh
275 1.1 eeh int node0, error;
276 1.1 eeh bus_space_tag_t sbt;
277 1.1 eeh struct sbus_attach_args sa;
278 1.1 eeh
279 1.1 eeh sc->sc_bustag = ma->ma_bustag;
280 1.1 eeh sc->sc_dmatag = ma->ma_dmatag;
281 1.8 eeh sc->sc_sysio = (struct sysioreg*)(u_long)ma->ma_address[0]; /* Use prom mapping for sysio. */
282 1.1 eeh sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN; /* Find interrupt group no */
283 1.1 eeh
284 1.1 eeh /* Setup interrupt translation tables */
285 1.1 eeh sc->sc_intr2ipl = CPU_ISSUN4C
286 1.1 eeh ? intr_sbus2ipl_4c
287 1.1 eeh : intr_sbus2ipl_4m;
288 1.1 eeh
289 1.1 eeh /*
290 1.1 eeh * Record clock frequency for synchronous SCSI.
291 1.1 eeh * IS THIS THE CORRECT DEFAULT??
292 1.1 eeh */
293 1.1 eeh sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
294 1.1 eeh printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
295 1.1 eeh
296 1.1 eeh sbt = sbus_alloc_bustag(sc);
297 1.1 eeh sc->sc_dmatag = sbus_alloc_dmatag(sc);
298 1.1 eeh
299 1.1 eeh /*
300 1.1 eeh * Get the SBus burst transfer size if burst transfers are supported
301 1.1 eeh */
302 1.1 eeh sc->sc_burst = getpropint(node, "burst-sizes", 0);
303 1.1 eeh
304 1.1 eeh /*
305 1.1 eeh * Collect address translations from the OBP.
306 1.1 eeh */
307 1.6 pk error = getprop(node, "ranges", sizeof(struct sbus_range),
308 1.1 eeh &sc->sc_nrange, (void **)&sc->sc_range);
309 1.16 eeh if (error)
310 1.1 eeh panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
311 1.1 eeh
312 1.17 mrg /* initailise the IOMMU */
313 1.17 mrg
314 1.17 mrg /* punch in our copies */
315 1.17 mrg sc->sc_is.is_bustag = sc->sc_bustag;
316 1.17 mrg sc->sc_is.is_iommu = &sc->sc_sysio->sys_iommu;
317 1.17 mrg sc->sc_is.is_sb = &sc->sc_sysio->sys_strbuf;
318 1.16 eeh
319 1.27 mrg /* give us a nice name.. */
320 1.27 mrg name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
321 1.27 mrg if (name == 0)
322 1.27 mrg panic("couldn't malloc iommu name");
323 1.27 mrg snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
324 1.27 mrg
325 1.27 mrg iommu_init(name, &sc->sc_is, 0);
326 1.12 eeh
327 1.12 eeh /*
328 1.1 eeh * Loop through ROM children, fixing any relative addresses
329 1.1 eeh * and then configuring each device.
330 1.1 eeh * `specials' is an array of device names that are treated
331 1.1 eeh * specially:
332 1.1 eeh */
333 1.1 eeh node0 = firstchild(node);
334 1.1 eeh for (node = node0; node; node = nextsibling(node)) {
335 1.1 eeh char *name = getpropstring(node, "name");
336 1.1 eeh
337 1.1 eeh if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
338 1.23 pk node, &sa) != 0) {
339 1.1 eeh printf("sbus_attach: %s: incomplete\n", name);
340 1.1 eeh continue;
341 1.1 eeh }
342 1.1 eeh (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
343 1.3 eeh sbus_destroy_attach_args(&sa);
344 1.1 eeh }
345 1.1 eeh }
346 1.1 eeh
347 1.1 eeh int
348 1.23 pk sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
349 1.1 eeh struct sbus_softc *sc;
350 1.1 eeh bus_space_tag_t bustag;
351 1.1 eeh bus_dma_tag_t dmatag;
352 1.1 eeh int node;
353 1.1 eeh struct sbus_attach_args *sa;
354 1.1 eeh {
355 1.3 eeh /*struct sbus_reg sbusreg;*/
356 1.3 eeh /*int base;*/
357 1.1 eeh int error;
358 1.3 eeh int n;
359 1.1 eeh
360 1.1 eeh bzero(sa, sizeof(struct sbus_attach_args));
361 1.6 pk error = getprop(node, "name", 1, &n, (void **)&sa->sa_name);
362 1.3 eeh if (error != 0)
363 1.3 eeh return (error);
364 1.3 eeh sa->sa_name[n] = '\0';
365 1.3 eeh
366 1.1 eeh sa->sa_bustag = bustag;
367 1.1 eeh sa->sa_dmatag = dmatag;
368 1.1 eeh sa->sa_node = node;
369 1.1 eeh
370 1.6 pk error = getprop(node, "reg", sizeof(struct sbus_reg),
371 1.3 eeh &sa->sa_nreg, (void **)&sa->sa_reg);
372 1.3 eeh if (error != 0) {
373 1.3 eeh char buf[32];
374 1.3 eeh if (error != ENOENT ||
375 1.3 eeh !node_has_property(node, "device_type") ||
376 1.3 eeh strcmp(getpropstringA(node, "device_type", buf),
377 1.3 eeh "hierarchical") != 0)
378 1.3 eeh return (error);
379 1.3 eeh }
380 1.3 eeh for (n = 0; n < sa->sa_nreg; n++) {
381 1.3 eeh /* Convert to relative addressing, if necessary */
382 1.3 eeh u_int32_t base = sa->sa_reg[n].sbr_offset;
383 1.3 eeh if (SBUS_ABS(base)) {
384 1.3 eeh sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
385 1.3 eeh sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
386 1.3 eeh }
387 1.1 eeh }
388 1.1 eeh
389 1.22 mrg if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr,
390 1.22 mrg sa->sa_slot)) != 0)
391 1.1 eeh return (error);
392 1.1 eeh
393 1.6 pk error = getprop(node, "address", sizeof(u_int32_t),
394 1.3 eeh &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
395 1.3 eeh if (error != 0 && error != ENOENT)
396 1.1 eeh return (error);
397 1.1 eeh
398 1.1 eeh return (0);
399 1.1 eeh }
400 1.1 eeh
401 1.3 eeh void
402 1.3 eeh sbus_destroy_attach_args(sa)
403 1.3 eeh struct sbus_attach_args *sa;
404 1.3 eeh {
405 1.3 eeh if (sa->sa_name != NULL)
406 1.3 eeh free(sa->sa_name, M_DEVBUF);
407 1.3 eeh
408 1.3 eeh if (sa->sa_nreg != 0)
409 1.3 eeh free(sa->sa_reg, M_DEVBUF);
410 1.3 eeh
411 1.3 eeh if (sa->sa_intr)
412 1.3 eeh free(sa->sa_intr, M_DEVBUF);
413 1.3 eeh
414 1.3 eeh if (sa->sa_promvaddrs)
415 1.8 eeh free((void *)sa->sa_promvaddrs, M_DEVBUF);
416 1.3 eeh
417 1.27 mrg bzero(sa, sizeof(struct sbus_attach_args)); /*DEBUG*/
418 1.3 eeh }
419 1.3 eeh
420 1.3 eeh
421 1.1 eeh int
422 1.1 eeh _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
423 1.1 eeh bus_space_tag_t t;
424 1.1 eeh bus_type_t btype;
425 1.1 eeh bus_addr_t offset;
426 1.1 eeh bus_size_t size;
427 1.1 eeh int flags;
428 1.3 eeh vaddr_t vaddr;
429 1.1 eeh bus_space_handle_t *hp;
430 1.1 eeh {
431 1.1 eeh struct sbus_softc *sc = t->cookie;
432 1.1 eeh int64_t slot = btype;
433 1.1 eeh int i;
434 1.1 eeh
435 1.1 eeh for (i = 0; i < sc->sc_nrange; i++) {
436 1.1 eeh bus_addr_t paddr;
437 1.1 eeh
438 1.1 eeh if (sc->sc_range[i].cspace != slot)
439 1.1 eeh continue;
440 1.1 eeh
441 1.1 eeh /* We've found the connection to the parent bus */
442 1.1 eeh paddr = sc->sc_range[i].poffset + offset;
443 1.1 eeh paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
444 1.27 mrg DPRINTF(SDB_DVMA,
445 1.27 mrg ("\n_sbus_bus_map: mapping paddr slot %lx offset %lx poffset %lx paddr %lx\n",
446 1.27 mrg (long)slot, (long)offset, (long)sc->sc_range[i].poffset,
447 1.27 mrg (long)paddr));
448 1.1 eeh return (bus_space_map2(sc->sc_bustag, 0, paddr,
449 1.1 eeh size, flags, vaddr, hp));
450 1.1 eeh }
451 1.1 eeh
452 1.1 eeh return (EINVAL);
453 1.1 eeh }
454 1.1 eeh
455 1.1 eeh int
456 1.1 eeh sbus_bus_mmap(t, btype, paddr, flags, hp)
457 1.1 eeh bus_space_tag_t t;
458 1.1 eeh bus_type_t btype;
459 1.1 eeh bus_addr_t paddr;
460 1.1 eeh int flags;
461 1.1 eeh bus_space_handle_t *hp;
462 1.1 eeh {
463 1.1 eeh bus_addr_t offset = paddr;
464 1.1 eeh int slot = (paddr>>32);
465 1.1 eeh struct sbus_softc *sc = t->cookie;
466 1.1 eeh int i;
467 1.1 eeh
468 1.1 eeh for (i = 0; i < sc->sc_nrange; i++) {
469 1.1 eeh bus_addr_t paddr;
470 1.1 eeh
471 1.1 eeh if (sc->sc_range[i].cspace != slot)
472 1.1 eeh continue;
473 1.1 eeh
474 1.1 eeh paddr = sc->sc_range[i].poffset + offset;
475 1.1 eeh paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
476 1.1 eeh return (bus_space_mmap(sc->sc_bustag, 0, paddr,
477 1.1 eeh flags, hp));
478 1.1 eeh }
479 1.1 eeh
480 1.1 eeh return (-1);
481 1.1 eeh }
482 1.1 eeh
483 1.1 eeh
484 1.1 eeh /*
485 1.1 eeh * Each attached device calls sbus_establish after it initializes
486 1.1 eeh * its sbusdev portion.
487 1.1 eeh */
488 1.1 eeh void
489 1.1 eeh sbus_establish(sd, dev)
490 1.1 eeh register struct sbusdev *sd;
491 1.1 eeh register struct device *dev;
492 1.1 eeh {
493 1.1 eeh register struct sbus_softc *sc;
494 1.1 eeh register struct device *curdev;
495 1.1 eeh
496 1.1 eeh /*
497 1.1 eeh * We have to look for the sbus by name, since it is not necessarily
498 1.1 eeh * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
499 1.1 eeh * We don't just use the device structure of the above-attached
500 1.1 eeh * sbus, since we might (in the future) support multiple sbus's.
501 1.1 eeh */
502 1.1 eeh for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
503 1.1 eeh if (!curdev || !curdev->dv_xname)
504 1.1 eeh panic("sbus_establish: can't find sbus parent for %s",
505 1.1 eeh sd->sd_dev->dv_xname
506 1.1 eeh ? sd->sd_dev->dv_xname
507 1.1 eeh : "<unknown>" );
508 1.1 eeh
509 1.1 eeh if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
510 1.1 eeh break;
511 1.1 eeh }
512 1.1 eeh sc = (struct sbus_softc *) curdev;
513 1.1 eeh
514 1.1 eeh sd->sd_dev = dev;
515 1.1 eeh sd->sd_bchain = sc->sc_sbdev;
516 1.1 eeh sc->sc_sbdev = sd;
517 1.1 eeh }
518 1.1 eeh
519 1.1 eeh /*
520 1.1 eeh * Reset the given sbus. (???)
521 1.1 eeh */
522 1.1 eeh void
523 1.1 eeh sbusreset(sbus)
524 1.1 eeh int sbus;
525 1.1 eeh {
526 1.1 eeh register struct sbusdev *sd;
527 1.1 eeh struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
528 1.1 eeh struct device *dev;
529 1.1 eeh
530 1.1 eeh printf("reset %s:", sc->sc_dev.dv_xname);
531 1.1 eeh for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
532 1.1 eeh if (sd->sd_reset) {
533 1.1 eeh dev = sd->sd_dev;
534 1.1 eeh (*sd->sd_reset)(dev);
535 1.1 eeh printf(" %s", dev->dv_xname);
536 1.1 eeh }
537 1.1 eeh }
538 1.1 eeh /* Reload iommu regs */
539 1.17 mrg iommu_reset(&sc->sc_is);
540 1.1 eeh }
541 1.1 eeh
542 1.1 eeh /*
543 1.1 eeh * Get interrupt attributes for an Sbus device.
544 1.1 eeh */
545 1.1 eeh int
546 1.22 mrg sbus_get_intr(sc, node, ipp, np, slot)
547 1.1 eeh struct sbus_softc *sc;
548 1.1 eeh int node;
549 1.3 eeh struct sbus_intr **ipp;
550 1.3 eeh int *np;
551 1.22 mrg int slot;
552 1.1 eeh {
553 1.1 eeh int *ipl;
554 1.22 mrg int n, i;
555 1.1 eeh char buf[32];
556 1.1 eeh
557 1.1 eeh /*
558 1.1 eeh * The `interrupts' property contains the Sbus interrupt level.
559 1.1 eeh */
560 1.1 eeh ipl = NULL;
561 1.6 pk if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
562 1.3 eeh struct sbus_intr *ip;
563 1.22 mrg int pri;
564 1.22 mrg
565 1.10 eeh /* Default to interrupt level 2 -- otherwise unused */
566 1.22 mrg pri = INTLEVENCODE(2);
567 1.22 mrg
568 1.22 mrg /* Change format to an `struct sbus_intr' array */
569 1.3 eeh ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
570 1.3 eeh if (ip == NULL)
571 1.3 eeh return (ENOMEM);
572 1.22 mrg
573 1.22 mrg /*
574 1.22 mrg * Now things get ugly. We need to take this value which is
575 1.1 eeh * the interrupt vector number and encode the IPL into it
576 1.1 eeh * somehow. Luckily, the interrupt vector has lots of free
577 1.22 mrg * space and we can easily stuff the IPL in there for a while.
578 1.1 eeh */
579 1.1 eeh getpropstringA(node, "device_type", buf);
580 1.22 mrg if (!buf[0])
581 1.10 eeh getpropstringA(node, "name", buf);
582 1.22 mrg
583 1.22 mrg for (i = 0; intrmap[i].in_class; i++)
584 1.3 eeh if (strcmp(intrmap[i].in_class, buf) == 0) {
585 1.3 eeh pri = INTLEVENCODE(intrmap[i].in_lev);
586 1.1 eeh break;
587 1.1 eeh }
588 1.22 mrg
589 1.22 mrg /*
590 1.22 mrg * Sbus card devices need the slot number encoded into
591 1.22 mrg * the vector as this is generally not done.
592 1.22 mrg */
593 1.22 mrg if ((ipl[0] & INTMAP_OBIO) == 0)
594 1.22 mrg pri |= slot << 3;
595 1.22 mrg
596 1.3 eeh for (n = 0; n < *np; n++) {
597 1.3 eeh /*
598 1.3 eeh * We encode vector and priority into sbi_pri so we
599 1.3 eeh * can pass them as a unit. This will go away if
600 1.3 eeh * sbus_establish ever takes an sbus_intr instead
601 1.3 eeh * of an integer level.
602 1.3 eeh * Stuff the real vector in sbi_vec.
603 1.3 eeh */
604 1.22 mrg
605 1.3 eeh ip[n].sbi_pri = pri|ipl[n];
606 1.3 eeh ip[n].sbi_vec = ipl[n];
607 1.3 eeh }
608 1.1 eeh free(ipl, M_DEVBUF);
609 1.3 eeh *ipp = ip;
610 1.1 eeh }
611 1.1 eeh
612 1.22 mrg return (0);
613 1.1 eeh }
614 1.1 eeh
615 1.1 eeh
616 1.1 eeh /*
617 1.1 eeh * Install an interrupt handler for an Sbus device.
618 1.1 eeh */
619 1.1 eeh void *
620 1.1 eeh sbus_intr_establish(t, level, flags, handler, arg)
621 1.1 eeh bus_space_tag_t t;
622 1.1 eeh int level;
623 1.1 eeh int flags;
624 1.1 eeh int (*handler) __P((void *));
625 1.1 eeh void *arg;
626 1.1 eeh {
627 1.1 eeh struct sbus_softc *sc = t->cookie;
628 1.1 eeh struct intrhand *ih;
629 1.1 eeh int ipl;
630 1.8 eeh long vec = level;
631 1.1 eeh
632 1.1 eeh ih = (struct intrhand *)
633 1.1 eeh malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
634 1.1 eeh if (ih == NULL)
635 1.1 eeh return (NULL);
636 1.1 eeh
637 1.1 eeh if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
638 1.8 eeh ipl = vec;
639 1.8 eeh else if ((vec & SBUS_INTR_COMPAT) != 0)
640 1.8 eeh ipl = vec & ~SBUS_INTR_COMPAT;
641 1.1 eeh else {
642 1.1 eeh /* Decode and remove IPL */
643 1.8 eeh ipl = INTLEV(vec);
644 1.8 eeh vec = INTVEC(vec);
645 1.27 mrg DPRINTF(SDB_INTR,
646 1.27 mrg ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
647 1.27 mrg (long)ipl, (long)vec, intrlev[vec]));
648 1.8 eeh if ((vec & INTMAP_OBIO) == 0) {
649 1.1 eeh /* We're in an SBUS slot */
650 1.1 eeh /* Register the map and clear intr registers */
651 1.22 mrg
652 1.22 mrg int slot = INTSLOT(level);
653 1.22 mrg
654 1.22 mrg ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
655 1.22 mrg ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
656 1.1 eeh #ifdef DEBUG
657 1.27 mrg if (sbus_debug & SDB_INTR) {
658 1.22 mrg int64_t intrmap = *ih->ih_map;
659 1.1 eeh
660 1.22 mrg printf("Found SBUS %lx IRQ as %llx in slot %d\n",
661 1.22 mrg (long)vec, (long long)intrmap, slot);
662 1.22 mrg printf("\tmap addr %p clr addr %p\n", ih->ih_map, ih->ih_clr);
663 1.1 eeh }
664 1.1 eeh #endif
665 1.1 eeh /* Enable the interrupt */
666 1.8 eeh vec |= INTMAP_V;
667 1.9 eeh /* Insert IGN */
668 1.9 eeh vec |= sc->sc_ign;
669 1.8 eeh bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, vec);
670 1.1 eeh } else {
671 1.1 eeh int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
672 1.1 eeh int64_t intrmap = 0;
673 1.1 eeh int i;
674 1.1 eeh
675 1.1 eeh /* Insert IGN */
676 1.8 eeh vec |= sc->sc_ign;
677 1.22 mrg for (i = 0; &intrptr[i] <=
678 1.22 mrg (int64_t *)&sc->sc_sysio->reserved_int_map &&
679 1.22 mrg INTVEC(intrmap = intrptr[i]) != INTVEC(vec); i++)
680 1.22 mrg ;
681 1.8 eeh if (INTVEC(intrmap) == INTVEC(vec)) {
682 1.27 mrg DPRINTF(SDB_INTR,
683 1.27 mrg ("Found OBIO %lx IRQ as %lx in slot %d\n",
684 1.27 mrg vec, (long)intrmap, i));
685 1.1 eeh /* Register the map and clear intr registers */
686 1.1 eeh ih->ih_map = &intrptr[i];
687 1.1 eeh intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
688 1.1 eeh ih->ih_clr = &intrptr[i];
689 1.1 eeh /* Enable the interrupt */
690 1.1 eeh intrmap |= INTMAP_V;
691 1.8 eeh bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, (u_long)intrmap);
692 1.27 mrg } else
693 1.27 mrg panic("IRQ not found!");
694 1.1 eeh }
695 1.1 eeh }
696 1.1 eeh #ifdef DEBUG
697 1.27 mrg if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
698 1.1 eeh #endif
699 1.1 eeh
700 1.1 eeh ih->ih_fun = handler;
701 1.1 eeh ih->ih_arg = arg;
702 1.8 eeh ih->ih_number = vec;
703 1.1 eeh ih->ih_pil = (1<<ipl);
704 1.18 eeh intr_establish(ipl, ih);
705 1.1 eeh return (ih);
706 1.1 eeh }
707 1.1 eeh
708 1.1 eeh static bus_space_tag_t
709 1.1 eeh sbus_alloc_bustag(sc)
710 1.1 eeh struct sbus_softc *sc;
711 1.1 eeh {
712 1.1 eeh bus_space_tag_t sbt;
713 1.1 eeh
714 1.1 eeh sbt = (bus_space_tag_t)
715 1.1 eeh malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
716 1.1 eeh if (sbt == NULL)
717 1.1 eeh return (NULL);
718 1.1 eeh
719 1.1 eeh bzero(sbt, sizeof *sbt);
720 1.1 eeh sbt->cookie = sc;
721 1.1 eeh sbt->parent = sc->sc_bustag;
722 1.12 eeh sbt->type = SBUS_BUS_SPACE;
723 1.1 eeh sbt->sparc_bus_map = _sbus_bus_map;
724 1.1 eeh sbt->sparc_bus_mmap = sbus_bus_mmap;
725 1.1 eeh sbt->sparc_intr_establish = sbus_intr_establish;
726 1.1 eeh return (sbt);
727 1.1 eeh }
728 1.1 eeh
729 1.1 eeh
730 1.1 eeh static bus_dma_tag_t
731 1.1 eeh sbus_alloc_dmatag(sc)
732 1.1 eeh struct sbus_softc *sc;
733 1.1 eeh {
734 1.1 eeh bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
735 1.1 eeh
736 1.1 eeh sdt = (bus_dma_tag_t)
737 1.1 eeh malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
738 1.1 eeh if (sdt == NULL)
739 1.1 eeh /* Panic? */
740 1.1 eeh return (psdt);
741 1.1 eeh
742 1.1 eeh sdt->_cookie = sc;
743 1.1 eeh sdt->_parent = psdt;
744 1.1 eeh #define PCOPY(x) sdt->x = psdt->x
745 1.1 eeh PCOPY(_dmamap_create);
746 1.1 eeh PCOPY(_dmamap_destroy);
747 1.1 eeh sdt->_dmamap_load = sbus_dmamap_load;
748 1.1 eeh PCOPY(_dmamap_load_mbuf);
749 1.1 eeh PCOPY(_dmamap_load_uio);
750 1.1 eeh PCOPY(_dmamap_load_raw);
751 1.1 eeh sdt->_dmamap_unload = sbus_dmamap_unload;
752 1.1 eeh sdt->_dmamap_sync = sbus_dmamap_sync;
753 1.1 eeh sdt->_dmamem_alloc = sbus_dmamem_alloc;
754 1.1 eeh sdt->_dmamem_free = sbus_dmamem_free;
755 1.2 eeh sdt->_dmamem_map = sbus_dmamem_map;
756 1.2 eeh sdt->_dmamem_unmap = sbus_dmamem_unmap;
757 1.1 eeh PCOPY(_dmamem_mmap);
758 1.1 eeh #undef PCOPY
759 1.1 eeh sc->sc_dmatag = sdt;
760 1.1 eeh return (sdt);
761 1.1 eeh }
762 1.1 eeh
763 1.1 eeh int
764 1.1 eeh sbus_dmamap_load(t, map, buf, buflen, p, flags)
765 1.1 eeh bus_dma_tag_t t;
766 1.1 eeh bus_dmamap_t map;
767 1.1 eeh void *buf;
768 1.1 eeh bus_size_t buflen;
769 1.1 eeh struct proc *p;
770 1.1 eeh int flags;
771 1.1 eeh {
772 1.12 eeh int err, s;
773 1.1 eeh bus_size_t sgsize;
774 1.3 eeh paddr_t curaddr;
775 1.12 eeh u_long dvmaddr;
776 1.12 eeh vaddr_t vaddr = (vaddr_t)buf;
777 1.1 eeh pmap_t pmap;
778 1.1 eeh struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
779 1.1 eeh
780 1.2 eeh if (map->dm_nsegs) {
781 1.2 eeh /* Already in use?? */
782 1.2 eeh #ifdef DIAGNOSTIC
783 1.2 eeh printf("sbus_dmamap_load: map still in use\n");
784 1.2 eeh #endif
785 1.2 eeh bus_dmamap_unload(t, map);
786 1.2 eeh }
787 1.19 eeh
788 1.12 eeh /*
789 1.12 eeh * Make sure that on error condition we return "no valid mappings".
790 1.12 eeh */
791 1.12 eeh map->dm_nsegs = 0;
792 1.12 eeh
793 1.12 eeh if (buflen > map->_dm_size)
794 1.12 eeh #ifdef DEBUG
795 1.12 eeh {
796 1.16 eeh printf("sbus_dmamap_load(): error %d > %d -- map size exceeded!\n", buflen, map->_dm_size);
797 1.26 pk #ifdef DDB
798 1.12 eeh Debugger();
799 1.26 pk #endif
800 1.12 eeh return (EINVAL);
801 1.12 eeh }
802 1.12 eeh #else
803 1.12 eeh return (EINVAL);
804 1.12 eeh #endif
805 1.12 eeh sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
806 1.12 eeh
807 1.12 eeh /*
808 1.12 eeh * XXX Need to implement "don't dma across this boundry".
809 1.12 eeh */
810 1.12 eeh
811 1.12 eeh s = splhigh();
812 1.17 mrg err = extent_alloc(sc->sc_is.is_dvmamap, sgsize, NBPG,
813 1.12 eeh map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
814 1.12 eeh splx(s);
815 1.12 eeh
816 1.12 eeh if (err != 0)
817 1.12 eeh return (err);
818 1.12 eeh
819 1.12 eeh #ifdef DEBUG
820 1.12 eeh if (dvmaddr == (bus_addr_t)-1)
821 1.12 eeh {
822 1.27 mrg printf("sbus_dmamap_load(): extent_alloc(%d, %x) failed!\n", sgsize, flags);
823 1.26 pk #ifdef DDB
824 1.12 eeh Debugger();
825 1.26 pk #endif
826 1.12 eeh }
827 1.12 eeh #endif
828 1.12 eeh if (dvmaddr == (bus_addr_t)-1)
829 1.12 eeh return (ENOMEM);
830 1.12 eeh
831 1.12 eeh /*
832 1.12 eeh * We always use just one segment.
833 1.12 eeh */
834 1.12 eeh map->dm_mapsize = buflen;
835 1.12 eeh map->dm_nsegs = 1;
836 1.12 eeh map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
837 1.12 eeh map->dm_segs[0].ds_len = sgsize;
838 1.12 eeh
839 1.1 eeh if (p != NULL)
840 1.1 eeh pmap = p->p_vmspace->vm_map.pmap;
841 1.1 eeh else
842 1.1 eeh pmap = pmap_kernel();
843 1.1 eeh
844 1.2 eeh dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
845 1.1 eeh sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
846 1.1 eeh for (; buflen > 0; ) {
847 1.1 eeh /*
848 1.1 eeh * Get the physical address for this page.
849 1.1 eeh */
850 1.20 thorpej if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
851 1.1 eeh bus_dmamap_unload(t, map);
852 1.1 eeh return (-1);
853 1.1 eeh }
854 1.1 eeh
855 1.1 eeh /*
856 1.1 eeh * Compute the segment size, and adjust counts.
857 1.1 eeh */
858 1.1 eeh sgsize = NBPG - ((u_long)vaddr & PGOFSET);
859 1.1 eeh if (buflen < sgsize)
860 1.1 eeh sgsize = buflen;
861 1.1 eeh
862 1.27 mrg DPRINTF(SDB_DVMA,
863 1.27 mrg ("sbus_dmamap_load: map %p loading va %lx at pa %lx\n",
864 1.27 mrg map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1))));
865 1.19 eeh iommu_enter(&sc->sc_is, trunc_page(dvmaddr), trunc_page(curaddr), flags);
866 1.1 eeh
867 1.1 eeh dvmaddr += PAGE_SIZE;
868 1.1 eeh vaddr += sgsize;
869 1.1 eeh buflen -= sgsize;
870 1.1 eeh }
871 1.1 eeh return (0);
872 1.1 eeh }
873 1.1 eeh
874 1.1 eeh void
875 1.1 eeh sbus_dmamap_unload(t, map)
876 1.1 eeh bus_dma_tag_t t;
877 1.1 eeh bus_dmamap_t map;
878 1.1 eeh {
879 1.3 eeh vaddr_t addr;
880 1.12 eeh int len, error, s;
881 1.12 eeh bus_addr_t dvmaddr;
882 1.12 eeh bus_size_t sgsize;
883 1.1 eeh struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
884 1.1 eeh
885 1.1 eeh if (map->dm_nsegs != 1)
886 1.16 eeh panic("sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
887 1.1 eeh
888 1.2 eeh addr = trunc_page(map->dm_segs[0].ds_addr);
889 1.1 eeh len = map->dm_segs[0].ds_len;
890 1.1 eeh
891 1.27 mrg DPRINTF(SDB_DVMA,
892 1.27 mrg ("sbus_dmamap_unload: map %p removing va %lx size %lx\n",
893 1.27 mrg map, (long)addr, (long)len));
894 1.19 eeh iommu_remove(&sc->sc_is, addr, len);
895 1.12 eeh dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
896 1.12 eeh sgsize = map->dm_segs[0].ds_len;
897 1.12 eeh
898 1.12 eeh /* Mark the mappings as invalid. */
899 1.12 eeh map->dm_mapsize = 0;
900 1.12 eeh map->dm_nsegs = 0;
901 1.12 eeh
902 1.12 eeh /* Unmapping is bus dependent */
903 1.12 eeh s = splhigh();
904 1.17 mrg error = extent_free(sc->sc_is.is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
905 1.12 eeh splx(s);
906 1.12 eeh if (error != 0)
907 1.12 eeh printf("warning: %ld of DVMA space lost\n", (long)sgsize);
908 1.12 eeh
909 1.25 mrg cache_flush((caddr_t)(u_long)dvmaddr, (u_int)sgsize);
910 1.1 eeh }
911 1.1 eeh
912 1.1 eeh void
913 1.1 eeh sbus_dmamap_sync(t, map, offset, len, ops)
914 1.1 eeh bus_dma_tag_t t;
915 1.1 eeh bus_dmamap_t map;
916 1.1 eeh bus_addr_t offset;
917 1.1 eeh bus_size_t len;
918 1.1 eeh int ops;
919 1.1 eeh {
920 1.1 eeh struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
921 1.3 eeh vaddr_t va = map->dm_segs[0].ds_addr + offset;
922 1.1 eeh
923 1.1 eeh /*
924 1.1 eeh * We only support one DMA segment; supporting more makes this code
925 1.1 eeh * too unweildy.
926 1.1 eeh */
927 1.1 eeh
928 1.27 mrg if (ops & BUS_DMASYNC_PREREAD) {
929 1.27 mrg DPRINTF(SDB_DVMA,
930 1.27 mrg ("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREREAD\n",
931 1.27 mrg (long)va, (u_long)len));
932 1.8 eeh
933 1.1 eeh /* Nothing to do */;
934 1.8 eeh }
935 1.27 mrg if (ops & BUS_DMASYNC_POSTREAD) {
936 1.1 eeh /*
937 1.1 eeh * We should sync the IOMMU streaming caches here first.
938 1.1 eeh */
939 1.27 mrg DPRINTF(SDB_DVMA,
940 1.27 mrg ("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTREAD\n",
941 1.27 mrg (long)va, (u_long)len));
942 1.1 eeh while (len > 0) {
943 1.27 mrg DPRINTF(SDB_DVMA,
944 1.27 mrg ("sbus_dmamap_sync: flushing va %p, %lu bytes left\n",
945 1.27 mrg (long)va, (u_long)len));
946 1.17 mrg bus_space_write_8(sc->sc_bustag, &sc->sc_is.is_sb->strbuf_pgflush, 0, va);
947 1.1 eeh if (len <= NBPG) {
948 1.19 eeh iommu_flush(&sc->sc_is);
949 1.8 eeh len = 0;
950 1.8 eeh } else
951 1.8 eeh len -= NBPG;
952 1.1 eeh va += NBPG;
953 1.1 eeh }
954 1.1 eeh }
955 1.27 mrg if (ops & BUS_DMASYNC_PREWRITE) {
956 1.27 mrg DPRINTF(SDB_DVMA,
957 1.27 mrg ("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREWRITE\n",
958 1.27 mrg (long)va, (u_long)len));
959 1.1 eeh /* Nothing to do */;
960 1.8 eeh }
961 1.27 mrg if (ops & BUS_DMASYNC_POSTWRITE) {
962 1.27 mrg DPRINTF(SDB_DVMA,
963 1.27 mrg ("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTWRITE\n",
964 1.27 mrg (long)va, (u_long)len));
965 1.8 eeh /* Nothing to do */;
966 1.8 eeh }
967 1.1 eeh bus_dmamap_sync(t->_parent, map, offset, len, ops);
968 1.1 eeh }
969 1.1 eeh
970 1.8 eeh
971 1.8 eeh /*
972 1.8 eeh * Take memory allocated by our parent bus and generate DVMA mappings for it.
973 1.8 eeh */
974 1.1 eeh int
975 1.1 eeh sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
976 1.1 eeh bus_dma_tag_t t;
977 1.1 eeh bus_size_t size, alignment, boundary;
978 1.1 eeh bus_dma_segment_t *segs;
979 1.1 eeh int nsegs;
980 1.1 eeh int *rsegs;
981 1.1 eeh int flags;
982 1.1 eeh {
983 1.3 eeh paddr_t curaddr;
984 1.12 eeh u_long dvmaddr;
985 1.1 eeh vm_page_t m;
986 1.1 eeh struct pglist *mlist;
987 1.1 eeh int error;
988 1.12 eeh int n, s;
989 1.1 eeh struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
990 1.1 eeh
991 1.1 eeh if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
992 1.1 eeh boundary, segs, nsegs, rsegs, flags)))
993 1.1 eeh return (error);
994 1.1 eeh
995 1.8 eeh /*
996 1.8 eeh * Allocate a DVMA mapping for our new memory.
997 1.8 eeh */
998 1.17 mrg for (n = 0; n < *rsegs; n++) {
999 1.12 eeh s = splhigh();
1000 1.17 mrg if (extent_alloc(sc->sc_is.is_dvmamap, segs[0].ds_len, alignment,
1001 1.25 mrg boundary, EX_NOWAIT, &dvmaddr)) {
1002 1.12 eeh splx(s);
1003 1.12 eeh /* Free what we got and exit */
1004 1.12 eeh bus_dmamem_free(t->_parent, segs, nsegs);
1005 1.12 eeh return (ENOMEM);
1006 1.12 eeh }
1007 1.12 eeh splx(s);
1008 1.8 eeh segs[n].ds_addr = dvmaddr;
1009 1.1 eeh size = segs[n].ds_len;
1010 1.1 eeh mlist = segs[n]._ds_mlist;
1011 1.1 eeh
1012 1.1 eeh /* Map memory into DVMA space */
1013 1.1 eeh for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1014 1.1 eeh curaddr = VM_PAGE_TO_PHYS(m);
1015 1.27 mrg DPRINTF(SDB_DVMA,
1016 1.27 mrg ("sbus_dmamem_alloc: map %p loading va %lx at pa %lx\n",
1017 1.27 mrg (long)m, (long)dvmaddr,
1018 1.27 mrg (long)(curaddr & ~(NBPG-1))));
1019 1.19 eeh iommu_enter(&sc->sc_is, dvmaddr, curaddr, flags);
1020 1.1 eeh dvmaddr += PAGE_SIZE;
1021 1.1 eeh }
1022 1.1 eeh }
1023 1.1 eeh return (0);
1024 1.1 eeh }
1025 1.1 eeh
1026 1.1 eeh void
1027 1.1 eeh sbus_dmamem_free(t, segs, nsegs)
1028 1.1 eeh bus_dma_tag_t t;
1029 1.1 eeh bus_dma_segment_t *segs;
1030 1.1 eeh int nsegs;
1031 1.1 eeh {
1032 1.3 eeh vaddr_t addr;
1033 1.2 eeh int len;
1034 1.12 eeh int n, s, error;
1035 1.1 eeh struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1036 1.1 eeh
1037 1.1 eeh
1038 1.27 mrg for (n = 0; n < nsegs; n++) {
1039 1.1 eeh addr = segs[n].ds_addr;
1040 1.1 eeh len = segs[n].ds_len;
1041 1.19 eeh iommu_remove(&sc->sc_is, addr, len);
1042 1.12 eeh s = splhigh();
1043 1.17 mrg error = extent_free(sc->sc_is.is_dvmamap, addr, len, EX_NOWAIT);
1044 1.12 eeh splx(s);
1045 1.12 eeh if (error != 0)
1046 1.12 eeh printf("warning: %ld of DVMA space lost\n", (long)len);
1047 1.1 eeh }
1048 1.1 eeh bus_dmamem_free(t->_parent, segs, nsegs);
1049 1.1 eeh }
1050 1.1 eeh
1051 1.2 eeh /*
1052 1.8 eeh * Map the DVMA mappings into the kernel pmap.
1053 1.2 eeh * Check the flags to see whether we're streaming or coherent.
1054 1.2 eeh */
1055 1.2 eeh int
1056 1.2 eeh sbus_dmamem_map(t, segs, nsegs, size, kvap, flags)
1057 1.2 eeh bus_dma_tag_t t;
1058 1.2 eeh bus_dma_segment_t *segs;
1059 1.2 eeh int nsegs;
1060 1.2 eeh size_t size;
1061 1.2 eeh caddr_t *kvap;
1062 1.2 eeh int flags;
1063 1.2 eeh {
1064 1.2 eeh vm_page_t m;
1065 1.3 eeh vaddr_t va;
1066 1.2 eeh bus_addr_t addr;
1067 1.2 eeh struct pglist *mlist;
1068 1.3 eeh int cbit;
1069 1.2 eeh
1070 1.2 eeh /*
1071 1.2 eeh * digest flags:
1072 1.2 eeh */
1073 1.2 eeh cbit = 0;
1074 1.2 eeh if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1075 1.2 eeh cbit |= PMAP_NVC;
1076 1.3 eeh if (flags & BUS_DMA_NOCACHE) /* sideffects */
1077 1.2 eeh cbit |= PMAP_NC;
1078 1.2 eeh /*
1079 1.8 eeh * Now take this and map it into the CPU since it should already
1080 1.24 soren * be in the IOMMU.
1081 1.2 eeh */
1082 1.8 eeh *kvap = (caddr_t)va = segs[0].ds_addr;
1083 1.2 eeh mlist = segs[0]._ds_mlist;
1084 1.2 eeh for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1085 1.2 eeh if (size == 0)
1086 1.2 eeh panic("_bus_dmamem_map: size botch");
1087 1.2 eeh
1088 1.2 eeh addr = VM_PAGE_TO_PHYS(m);
1089 1.2 eeh pmap_enter(pmap_kernel(), va, addr | cbit,
1090 1.21 thorpej VM_PROT_READ | VM_PROT_WRITE,
1091 1.21 thorpej VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1092 1.2 eeh va += PAGE_SIZE;
1093 1.2 eeh size -= PAGE_SIZE;
1094 1.2 eeh }
1095 1.2 eeh
1096 1.2 eeh return (0);
1097 1.2 eeh }
1098 1.2 eeh
1099 1.2 eeh /*
1100 1.8 eeh * Unmap DVMA mappings from kernel
1101 1.2 eeh */
1102 1.2 eeh void
1103 1.2 eeh sbus_dmamem_unmap(t, kva, size)
1104 1.2 eeh bus_dma_tag_t t;
1105 1.2 eeh caddr_t kva;
1106 1.2 eeh size_t size;
1107 1.2 eeh {
1108 1.2 eeh
1109 1.2 eeh #ifdef DIAGNOSTIC
1110 1.2 eeh if ((u_long)kva & PGOFSET)
1111 1.2 eeh panic("_bus_dmamem_unmap");
1112 1.2 eeh #endif
1113 1.2 eeh
1114 1.2 eeh size = round_page(size);
1115 1.8 eeh pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1116 1.2 eeh }
1117