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