sbus.c revision 1.21 1 /* $NetBSD: sbus.c,v 1.21 1999/11/13 00:32:16 thorpej 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/dev/iommureg.h>
122 #include <sparc64/dev/iommuvar.h>
123 #include <sparc64/dev/sbusreg.h>
124 #include <dev/sbus/sbusvar.h>
125
126 #include <machine/autoconf.h>
127 #include <machine/ctlreg.h>
128 #include <machine/cpu.h>
129 #include <machine/sparc64.h>
130
131 #ifdef DEBUG
132 #define SDB_DVMA 0x1
133 #define SDB_INTR 0x2
134 int sbusdebug = 0;
135 #endif
136
137 void sbusreset __P((int));
138
139 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
140 static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
141 static int sbus_get_intr __P((struct sbus_softc *, int,
142 struct sbus_intr **, int *));
143 static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
144 int, bus_space_handle_t *));
145 static int _sbus_bus_map __P((
146 bus_space_tag_t,
147 bus_type_t,
148 bus_addr_t, /*offset*/
149 bus_size_t, /*size*/
150 int, /*flags*/
151 vaddr_t, /*preferred virtual address */
152 bus_space_handle_t *));
153 static void *sbus_intr_establish __P((
154 bus_space_tag_t,
155 int, /*level*/
156 int, /*flags*/
157 int (*) __P((void *)), /*handler*/
158 void *)); /*handler arg*/
159
160
161 /* autoconfiguration driver */
162 int sbus_match __P((struct device *, struct cfdata *, void *));
163 void sbus_attach __P((struct device *, struct device *, void *));
164
165
166 struct cfattach sbus_ca = {
167 sizeof(struct sbus_softc), sbus_match, sbus_attach
168 };
169
170 extern struct cfdriver sbus_cd;
171
172 /*
173 * DVMA routines
174 */
175 int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
176 bus_size_t, struct proc *, int));
177 void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
178 void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
179 bus_size_t, int));
180 int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
181 bus_size_t alignment, bus_size_t boundary,
182 bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
183 void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
184 int nsegs));
185 int sbus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
186 int nsegs, size_t size, caddr_t *kvap, int flags));
187 void sbus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
188 size_t size));
189
190
191 /*
192 * Child devices receive the Sbus interrupt level in their attach
193 * arguments. We translate these to CPU IPLs using the following
194 * tables. Note: obio bus interrupt levels are identical to the
195 * processor IPL.
196 *
197 * The second set of tables is used when the Sbus interrupt level
198 * cannot be had from the PROM as an `interrupt' property. We then
199 * fall back on the `intr' property which contains the CPU IPL.
200 */
201
202 /* Translate Sbus interrupt level to processor IPL */
203 static int intr_sbus2ipl_4c[] = {
204 0, 1, 2, 3, 5, 7, 8, 9
205 };
206 static int intr_sbus2ipl_4m[] = {
207 0, 2, 3, 5, 7, 9, 11, 13
208 };
209
210 /*
211 * This value is or'ed into the attach args' interrupt level cookie
212 * if the interrupt level comes from an `intr' property, i.e. it is
213 * not an Sbus interrupt level.
214 */
215 #define SBUS_INTR_COMPAT 0x80000000
216
217
218 /*
219 * Print the location of some sbus-attached device (called just
220 * before attaching that device). If `sbus' is not NULL, the
221 * device was found but not configured; print the sbus as well.
222 * Return UNCONF (config_find ignores this if the device was configured).
223 */
224 int
225 sbus_print(args, busname)
226 void *args;
227 const char *busname;
228 {
229 struct sbus_attach_args *sa = args;
230 int i;
231
232 if (busname)
233 printf("%s at %s", sa->sa_name, busname);
234 printf(" slot %ld offset 0x%lx", (long)sa->sa_slot,
235 (u_long)sa->sa_offset);
236 for (i=0; i<sa->sa_nintr; i++) {
237 struct sbus_intr *sbi = &sa->sa_intr[i];
238
239 printf(" vector %lx ipl %ld",
240 (u_long)sbi->sbi_vec,
241 (long)INTLEV(sbi->sbi_pri));
242 }
243 return (UNCONF);
244 }
245
246 int
247 sbus_match(parent, cf, aux)
248 struct device *parent;
249 struct cfdata *cf;
250 void *aux;
251 {
252 struct mainbus_attach_args *ma = aux;
253
254 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
255 }
256
257 /*
258 * Attach an Sbus.
259 */
260 void
261 sbus_attach(parent, self, aux)
262 struct device *parent;
263 struct device *self;
264 void *aux;
265 {
266 struct sbus_softc *sc = (struct sbus_softc *)self;
267 struct mainbus_attach_args *ma = aux;
268 int node = ma->ma_node;
269
270 int node0, error;
271 bus_space_tag_t sbt;
272 struct sbus_attach_args sa;
273 char *busname = "sbus";
274 struct bootpath *bp = ma->ma_bp;
275
276
277 sc->sc_bustag = ma->ma_bustag;
278 sc->sc_dmatag = ma->ma_dmatag;
279 sc->sc_sysio = (struct sysioreg*)(u_long)ma->ma_address[0]; /* Use prom mapping for sysio. */
280 sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN; /* Find interrupt group no */
281
282 /* Setup interrupt translation tables */
283 sc->sc_intr2ipl = CPU_ISSUN4C
284 ? intr_sbus2ipl_4c
285 : intr_sbus2ipl_4m;
286
287 /*
288 * Record clock frequency for synchronous SCSI.
289 * IS THIS THE CORRECT DEFAULT??
290 */
291 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
292 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
293
294 sbt = sbus_alloc_bustag(sc);
295 sc->sc_dmatag = sbus_alloc_dmatag(sc);
296
297 /*
298 * Get the SBus burst transfer size if burst transfers are supported
299 */
300 sc->sc_burst = getpropint(node, "burst-sizes", 0);
301
302 /* Propagate bootpath */
303 if (bp != NULL && strcmp(bp->name, busname) == 0)
304 bp++;
305 else
306 bp = NULL;
307
308 /*
309 * Collect address translations from the OBP.
310 */
311 error = getprop(node, "ranges", sizeof(struct sbus_range),
312 &sc->sc_nrange, (void **)&sc->sc_range);
313 if (error)
314 panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
315
316 /* initailise the IOMMU */
317
318 /* punch in our copies */
319 sc->sc_is.is_bustag = sc->sc_bustag;
320 sc->sc_is.is_iommu = &sc->sc_sysio->sys_iommu;
321 sc->sc_is.is_sb = &sc->sc_sysio->sys_strbuf;
322
323 /* XXX should have instance number */
324 iommu_init("SBus dvma", &sc->sc_is, 0);
325
326 /*
327 * Loop through ROM children, fixing any relative addresses
328 * and then configuring each device.
329 * `specials' is an array of device names that are treated
330 * specially:
331 */
332 node0 = firstchild(node);
333 for (node = node0; node; node = nextsibling(node)) {
334 char *name = getpropstring(node, "name");
335
336 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
337 node, bp, &sa) != 0) {
338 printf("sbus_attach: %s: incomplete\n", name);
339 continue;
340 }
341 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
342 sbus_destroy_attach_args(&sa);
343 }
344 }
345
346 int
347 sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
348 struct sbus_softc *sc;
349 bus_space_tag_t bustag;
350 bus_dma_tag_t dmatag;
351 int node;
352 struct bootpath *bp;
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 sa->sa_bp = bp;
370
371 error = getprop(node, "reg", sizeof(struct sbus_reg),
372 &sa->sa_nreg, (void **)&sa->sa_reg);
373 if (error != 0) {
374 char buf[32];
375 if (error != ENOENT ||
376 !node_has_property(node, "device_type") ||
377 strcmp(getpropstringA(node, "device_type", buf),
378 "hierarchical") != 0)
379 return (error);
380 }
381 for (n = 0; n < sa->sa_nreg; n++) {
382 /* Convert to relative addressing, if necessary */
383 u_int32_t base = sa->sa_reg[n].sbr_offset;
384 if (SBUS_ABS(base)) {
385 sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
386 sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
387 }
388 }
389
390 if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 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 #ifdef DEBUG
445 if (sbusdebug & SDB_DVMA)
446 printf("\n_sbus_bus_map: mapping paddr slot %lx offset %lx poffset %lx paddr %lx\n",
447 (long)slot, (long)offset, (long)sc->sc_range[i].poffset, (long)paddr);
448 #endif
449 return (bus_space_map2(sc->sc_bustag, 0, paddr,
450 size, flags, vaddr, hp));
451 }
452
453 return (EINVAL);
454 }
455
456 int
457 sbus_bus_mmap(t, btype, paddr, flags, hp)
458 bus_space_tag_t t;
459 bus_type_t btype;
460 bus_addr_t paddr;
461 int flags;
462 bus_space_handle_t *hp;
463 {
464 bus_addr_t offset = paddr;
465 int slot = (paddr>>32);
466 struct sbus_softc *sc = t->cookie;
467 int i;
468
469 for (i = 0; i < sc->sc_nrange; i++) {
470 bus_addr_t paddr;
471
472 if (sc->sc_range[i].cspace != slot)
473 continue;
474
475 paddr = sc->sc_range[i].poffset + offset;
476 paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
477 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
478 flags, hp));
479 }
480
481 return (-1);
482 }
483
484
485 /*
486 * Each attached device calls sbus_establish after it initializes
487 * its sbusdev portion.
488 */
489 void
490 sbus_establish(sd, dev)
491 register struct sbusdev *sd;
492 register struct device *dev;
493 {
494 register struct sbus_softc *sc;
495 register struct device *curdev;
496
497 /*
498 * We have to look for the sbus by name, since it is not necessarily
499 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
500 * We don't just use the device structure of the above-attached
501 * sbus, since we might (in the future) support multiple sbus's.
502 */
503 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
504 if (!curdev || !curdev->dv_xname)
505 panic("sbus_establish: can't find sbus parent for %s",
506 sd->sd_dev->dv_xname
507 ? sd->sd_dev->dv_xname
508 : "<unknown>" );
509
510 if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
511 break;
512 }
513 sc = (struct sbus_softc *) curdev;
514
515 sd->sd_dev = dev;
516 sd->sd_bchain = sc->sc_sbdev;
517 sc->sc_sbdev = sd;
518 }
519
520 /*
521 * Reset the given sbus. (???)
522 */
523 void
524 sbusreset(sbus)
525 int sbus;
526 {
527 register struct sbusdev *sd;
528 struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
529 struct device *dev;
530
531 printf("reset %s:", sc->sc_dev.dv_xname);
532 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
533 if (sd->sd_reset) {
534 dev = sd->sd_dev;
535 (*sd->sd_reset)(dev);
536 printf(" %s", dev->dv_xname);
537 }
538 }
539 /* Reload iommu regs */
540 iommu_reset(&sc->sc_is);
541 }
542
543 /*
544 * Get interrupt attributes for an Sbus device.
545 */
546 int
547 sbus_get_intr(sc, node, ipp, np)
548 struct sbus_softc *sc;
549 int node;
550 struct sbus_intr **ipp;
551 int *np;
552 {
553 int *ipl;
554 int i, n, error;
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 /* Change format to an `struct sbus_intr' array */
563 struct sbus_intr *ip;
564 /* Default to interrupt level 2 -- otherwise unused */
565 int pri = INTLEVENCODE(2);
566 ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
567 if (ip == NULL)
568 return (ENOMEM);
569 /* Now things get ugly. We need to take this value which is
570 * the interrupt vector number and encode the IPL into it
571 * somehow. Luckily, the interrupt vector has lots of free
572 * space and we can easily stuff the IPL in there for a while.
573 */
574 getpropstringA(node, "device_type", buf);
575 if (!buf[0]) {
576 getpropstringA(node, "name", buf);
577 }
578 for (i=0; intrmap[i].in_class; i++) {
579 if (strcmp(intrmap[i].in_class, buf) == 0) {
580 pri = INTLEVENCODE(intrmap[i].in_lev);
581 break;
582 }
583 }
584 for (n = 0; n < *np; n++) {
585 /*
586 * We encode vector and priority into sbi_pri so we
587 * can pass them as a unit. This will go away if
588 * sbus_establish ever takes an sbus_intr instead
589 * of an integer level.
590 * Stuff the real vector in sbi_vec.
591 */
592 ip[n].sbi_pri = pri|ipl[n];
593 ip[n].sbi_vec = ipl[n];
594 }
595 free(ipl, M_DEVBUF);
596 *ipp = ip;
597 return (0);
598 }
599
600 /* We really don't support the following */
601 /* printf("\nWARNING: sbus_get_intr() \"interrupts\" not found -- using \"intr\"\n"); */
602 /* And some devices don't even have interrupts */
603 /*
604 * Fall back on `intr' property.
605 */
606 *ipp = NULL;
607 error = getprop(node, "intr", sizeof(struct sbus_intr),
608 np, (void **)ipp);
609 switch (error) {
610 case 0:
611 for (n = *np; n-- > 0;) {
612 /*
613 * Move the interrupt vector into place.
614 * We could remap the level, but the SBUS priorities
615 * are probably good enough.
616 */
617 (*ipp)[n].sbi_vec = (*ipp)[n].sbi_pri;
618 (*ipp)[n].sbi_pri |= INTLEVENCODE((*ipp)[n].sbi_pri);
619 }
620 break;
621 case ENOENT:
622 error = 0;
623 break;
624 }
625
626 return (error);
627 }
628
629
630 /*
631 * Install an interrupt handler for an Sbus device.
632 */
633 void *
634 sbus_intr_establish(t, level, flags, handler, arg)
635 bus_space_tag_t t;
636 int level;
637 int flags;
638 int (*handler) __P((void *));
639 void *arg;
640 {
641 struct sbus_softc *sc = t->cookie;
642 struct intrhand *ih;
643 int ipl;
644 long vec = level;
645
646 ih = (struct intrhand *)
647 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
648 if (ih == NULL)
649 return (NULL);
650
651 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
652 ipl = vec;
653 else if ((vec & SBUS_INTR_COMPAT) != 0)
654 ipl = vec & ~SBUS_INTR_COMPAT;
655 else {
656 /* Decode and remove IPL */
657 ipl = INTLEV(vec);
658 vec = INTVEC(vec);
659 #ifdef DEBUG
660 if (sbusdebug & SDB_INTR) {
661 printf("\nsbus: intr[%ld]%lx: %lx\n", (long)ipl, (long)vec,
662 intrlev[vec]);
663 printf("Hunting for IRQ...\n");
664 }
665 #endif
666 if ((vec & INTMAP_OBIO) == 0) {
667 /* We're in an SBUS slot */
668 /* Register the map and clear intr registers */
669 #ifdef DEBUG
670 if (sbusdebug & SDB_INTR) {
671 int64_t *intrptr = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
672 int64_t intrmap = *intrptr;
673
674 printf("Found SBUS %lx IRQ as %llx in slot %ld\n",
675 (long)vec, (long)intrmap,
676 (long)INTSLOT(vec));
677 }
678 #endif
679 ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
680 ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[INTVEC(vec)];
681 /* Enable the interrupt */
682 vec |= INTMAP_V;
683 /* Insert IGN */
684 vec |= sc->sc_ign;
685 bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, vec);
686 } else {
687 int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
688 int64_t intrmap = 0;
689 int i;
690
691 /* Insert IGN */
692 vec |= sc->sc_ign;
693 for (i=0;
694 &intrptr[i] <= (int64_t *)&sc->sc_sysio->reserved_int_map &&
695 INTVEC(intrmap=intrptr[i]) != INTVEC(vec);
696 i++);
697 if (INTVEC(intrmap) == INTVEC(vec)) {
698 #ifdef DEBUG
699 if (sbusdebug & SDB_INTR)
700 printf("Found OBIO %lx IRQ as %lx in slot %d\n",
701 vec, (long)intrmap, i);
702 #endif
703 /* Register the map and clear intr registers */
704 ih->ih_map = &intrptr[i];
705 intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
706 ih->ih_clr = &intrptr[i];
707 /* Enable the interrupt */
708 intrmap |= INTMAP_V;
709 bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, (u_long)intrmap);
710 } else panic("IRQ not found!");
711 }
712 }
713 #ifdef DEBUG
714 if (sbusdebug & SDB_INTR) { long i; for (i=0; i<1400000000; i++); }
715 #endif
716
717 ih->ih_fun = handler;
718 ih->ih_arg = arg;
719 ih->ih_number = vec;
720 ih->ih_pil = (1<<ipl);
721 intr_establish(ipl, ih);
722 return (ih);
723 }
724
725 static bus_space_tag_t
726 sbus_alloc_bustag(sc)
727 struct sbus_softc *sc;
728 {
729 bus_space_tag_t sbt;
730
731 sbt = (bus_space_tag_t)
732 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
733 if (sbt == NULL)
734 return (NULL);
735
736 bzero(sbt, sizeof *sbt);
737 sbt->cookie = sc;
738 sbt->parent = sc->sc_bustag;
739 sbt->type = SBUS_BUS_SPACE;
740 sbt->sparc_bus_map = _sbus_bus_map;
741 sbt->sparc_bus_mmap = sbus_bus_mmap;
742 sbt->sparc_intr_establish = sbus_intr_establish;
743 return (sbt);
744 }
745
746
747 static bus_dma_tag_t
748 sbus_alloc_dmatag(sc)
749 struct sbus_softc *sc;
750 {
751 bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
752
753 sdt = (bus_dma_tag_t)
754 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
755 if (sdt == NULL)
756 /* Panic? */
757 return (psdt);
758
759 sdt->_cookie = sc;
760 sdt->_parent = psdt;
761 #define PCOPY(x) sdt->x = psdt->x
762 PCOPY(_dmamap_create);
763 PCOPY(_dmamap_destroy);
764 sdt->_dmamap_load = sbus_dmamap_load;
765 PCOPY(_dmamap_load_mbuf);
766 PCOPY(_dmamap_load_uio);
767 PCOPY(_dmamap_load_raw);
768 sdt->_dmamap_unload = sbus_dmamap_unload;
769 sdt->_dmamap_sync = sbus_dmamap_sync;
770 sdt->_dmamem_alloc = sbus_dmamem_alloc;
771 sdt->_dmamem_free = sbus_dmamem_free;
772 sdt->_dmamem_map = sbus_dmamem_map;
773 sdt->_dmamem_unmap = sbus_dmamem_unmap;
774 PCOPY(_dmamem_mmap);
775 #undef PCOPY
776 sc->sc_dmatag = sdt;
777 return (sdt);
778 }
779
780 int
781 sbus_dmamap_load(t, map, buf, buflen, p, flags)
782 bus_dma_tag_t t;
783 bus_dmamap_t map;
784 void *buf;
785 bus_size_t buflen;
786 struct proc *p;
787 int flags;
788 {
789 int err, s;
790 bus_size_t sgsize;
791 paddr_t curaddr;
792 u_long dvmaddr;
793 vaddr_t vaddr = (vaddr_t)buf;
794 pmap_t pmap;
795 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
796
797 if (map->dm_nsegs) {
798 /* Already in use?? */
799 #ifdef DIAGNOSTIC
800 printf("sbus_dmamap_load: map still in use\n");
801 #endif
802 bus_dmamap_unload(t, map);
803 }
804
805 /*
806 * Make sure that on error condition we return "no valid mappings".
807 */
808 map->dm_nsegs = 0;
809
810 if (buflen > map->_dm_size)
811 #ifdef DEBUG
812 {
813 printf("sbus_dmamap_load(): error %d > %d -- map size exceeded!\n", buflen, map->_dm_size);
814 Debugger();
815 return (EINVAL);
816 }
817 #else
818 return (EINVAL);
819 #endif
820 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
821
822 /*
823 * XXX Need to implement "don't dma across this boundry".
824 */
825
826 s = splhigh();
827 err = extent_alloc(sc->sc_is.is_dvmamap, sgsize, NBPG,
828 map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
829 splx(s);
830
831 if (err != 0)
832 return (err);
833
834 #ifdef DEBUG
835 if (dvmaddr == (bus_addr_t)-1)
836 {
837 printf("sbus_dmamap_load(): dvmamap_alloc(%d, %x) failed!\n", sgsize, flags);
838 Debugger();
839 }
840 #endif
841 if (dvmaddr == (bus_addr_t)-1)
842 return (ENOMEM);
843
844 /*
845 * We always use just one segment.
846 */
847 map->dm_mapsize = buflen;
848 map->dm_nsegs = 1;
849 map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
850 map->dm_segs[0].ds_len = sgsize;
851
852 if (p != NULL)
853 pmap = p->p_vmspace->vm_map.pmap;
854 else
855 pmap = pmap_kernel();
856
857 dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
858 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
859 for (; buflen > 0; ) {
860 /*
861 * Get the physical address for this page.
862 */
863 if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
864 bus_dmamap_unload(t, map);
865 return (-1);
866 }
867
868 /*
869 * Compute the segment size, and adjust counts.
870 */
871 sgsize = NBPG - ((u_long)vaddr & PGOFSET);
872 if (buflen < sgsize)
873 sgsize = buflen;
874
875 #ifdef DEBUG
876 if (sbusdebug & SDB_DVMA)
877 printf("sbus_dmamap_load: map %p loading va %lx at pa %lx\n",
878 map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
879 #endif
880 iommu_enter(&sc->sc_is, trunc_page(dvmaddr), trunc_page(curaddr), flags);
881
882 dvmaddr += PAGE_SIZE;
883 vaddr += sgsize;
884 buflen -= sgsize;
885 }
886 return (0);
887 }
888
889 void
890 sbus_dmamap_unload(t, map)
891 bus_dma_tag_t t;
892 bus_dmamap_t map;
893 {
894 vaddr_t addr;
895 int len, error, s;
896 bus_addr_t dvmaddr;
897 bus_size_t sgsize;
898 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
899
900 if (map->dm_nsegs != 1)
901 panic("sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
902
903 addr = trunc_page(map->dm_segs[0].ds_addr);
904 len = map->dm_segs[0].ds_len;
905
906 #ifdef DEBUG
907 if (sbusdebug & SDB_DVMA)
908 printf("sbus_dmamap_unload: map %p removing va %lx size %lx\n",
909 map, (long)addr, (long)len);
910 #endif
911 iommu_remove(&sc->sc_is, addr, len);
912 dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
913 sgsize = map->dm_segs[0].ds_len;
914
915 /* Mark the mappings as invalid. */
916 map->dm_mapsize = 0;
917 map->dm_nsegs = 0;
918
919 /* Unmapping is bus dependent */
920 s = splhigh();
921 error = extent_free(sc->sc_is.is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
922 splx(s);
923 if (error != 0)
924 printf("warning: %ld of DVMA space lost\n", (long)sgsize);
925
926 cache_flush((caddr_t)dvmaddr, (u_int) sgsize);
927 }
928
929
930 void
931 sbus_dmamap_sync(t, map, offset, len, ops)
932 bus_dma_tag_t t;
933 bus_dmamap_t map;
934 bus_addr_t offset;
935 bus_size_t len;
936 int ops;
937 {
938 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
939 vaddr_t va = map->dm_segs[0].ds_addr + offset;
940
941 /*
942 * We only support one DMA segment; supporting more makes this code
943 * too unweildy.
944 */
945
946 if (ops&BUS_DMASYNC_PREREAD) {
947 #ifdef DEBUG
948 if (sbusdebug & SDB_DVMA)
949 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREREAD\n",
950 (long)va, (u_long)len);
951 #endif
952
953 /* Nothing to do */;
954 }
955 if (ops&BUS_DMASYNC_POSTREAD) {
956 /*
957 * We should sync the IOMMU streaming caches here first.
958 */
959 #ifdef DEBUG
960 if (sbusdebug & SDB_DVMA)
961 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTREAD\n",
962 (long)va, (u_long)len);
963 #endif
964 while (len > 0) {
965
966 /*
967 * Streaming buffer flushes:
968 *
969 * 1 Tell strbuf to flush by storing va to strbuf_pgflush
970 * If we're not on a cache line boundary (64-bits):
971 * 2 Store 0 in flag
972 * 3 Store pointer to flag in flushsync
973 * 4 wait till flushsync becomes 0x1
974 *
975 * If it takes more than .5 sec, something went wrong.
976 */
977 #ifdef DEBUG
978 if (sbusdebug & SDB_DVMA)
979 printf("sbus_dmamap_sync: flushing va %p, %lu bytes left\n",
980 (long)va, (u_long)len);
981 #endif
982 bus_space_write_8(sc->sc_bustag, &sc->sc_is.is_sb->strbuf_pgflush, 0, va);
983 if (len <= NBPG) {
984 iommu_flush(&sc->sc_is);
985 len = 0;
986 } else
987 len -= NBPG;
988 va += NBPG;
989 }
990 }
991 if (ops&BUS_DMASYNC_PREWRITE) {
992 #ifdef DEBUG
993 if (sbusdebug & SDB_DVMA)
994 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREWRITE\n",
995 (long)va, (u_long)len);
996 #endif
997 /* Nothing to do */;
998 }
999 if (ops&BUS_DMASYNC_POSTWRITE) {
1000 #ifdef DEBUG
1001 if (sbusdebug & SDB_DVMA)
1002 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTWRITE\n",
1003 (long)va, (u_long)len);
1004 #endif
1005 /* Nothing to do */;
1006 }
1007 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1008 }
1009
1010
1011 /*
1012 * Take memory allocated by our parent bus and generate DVMA mappings for it.
1013 */
1014 int
1015 sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1016 bus_dma_tag_t t;
1017 bus_size_t size, alignment, boundary;
1018 bus_dma_segment_t *segs;
1019 int nsegs;
1020 int *rsegs;
1021 int flags;
1022 {
1023 paddr_t curaddr;
1024 u_long dvmaddr;
1025 vm_page_t m;
1026 struct pglist *mlist;
1027 int error;
1028 int n, s;
1029 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1030
1031 if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
1032 boundary, segs, nsegs, rsegs, flags)))
1033 return (error);
1034
1035 /*
1036 * Allocate a DVMA mapping for our new memory.
1037 */
1038 for (n = 0; n < *rsegs; n++) {
1039 #if 1
1040 s = splhigh();
1041 if (extent_alloc(sc->sc_is.is_dvmamap, segs[0].ds_len, alignment,
1042 boundary, EX_NOWAIT, (u_long *)&dvmaddr)) {
1043 splx(s);
1044 /* Free what we got and exit */
1045 bus_dmamem_free(t->_parent, segs, nsegs);
1046 return (ENOMEM);
1047 }
1048 splx(s);
1049 #else
1050 dvmaddr = dvmamap_alloc(segs[0].ds_len, flags);
1051 if (dvmaddr == (bus_addr_t)-1) {
1052 /* Free what we got and exit */
1053 bus_dmamem_free(t->_parent, segs, nsegs);
1054 return (ENOMEM);
1055 }
1056 #endif
1057 segs[n].ds_addr = dvmaddr;
1058 size = segs[n].ds_len;
1059 mlist = segs[n]._ds_mlist;
1060
1061 /* Map memory into DVMA space */
1062 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1063 curaddr = VM_PAGE_TO_PHYS(m);
1064 #ifdef DEBUG
1065 if (sbusdebug & SDB_DVMA)
1066 printf("sbus_dmamem_alloc: map %p loading va %lx at pa %lx\n",
1067 (long)m, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
1068 #endif
1069 iommu_enter(&sc->sc_is, dvmaddr, curaddr, flags);
1070 dvmaddr += PAGE_SIZE;
1071 }
1072 }
1073 return (0);
1074 }
1075
1076 void
1077 sbus_dmamem_free(t, segs, nsegs)
1078 bus_dma_tag_t t;
1079 bus_dma_segment_t *segs;
1080 int nsegs;
1081 {
1082 vaddr_t addr;
1083 int len;
1084 int n, s, error;
1085 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1086
1087
1088 for (n=0; n<nsegs; n++) {
1089 addr = segs[n].ds_addr;
1090 len = segs[n].ds_len;
1091 iommu_remove(&sc->sc_is, addr, len);
1092 #if 1
1093 s = splhigh();
1094 error = extent_free(sc->sc_is.is_dvmamap, addr, len, EX_NOWAIT);
1095 splx(s);
1096 if (error != 0)
1097 printf("warning: %ld of DVMA space lost\n", (long)len);
1098 #else
1099 dvmamap_free(addr, len);
1100 #endif
1101 }
1102 bus_dmamem_free(t->_parent, segs, nsegs);
1103 }
1104
1105 /*
1106 * Map the DVMA mappings into the kernel pmap.
1107 * Check the flags to see whether we're streaming or coherent.
1108 */
1109 int
1110 sbus_dmamem_map(t, segs, nsegs, size, kvap, flags)
1111 bus_dma_tag_t t;
1112 bus_dma_segment_t *segs;
1113 int nsegs;
1114 size_t size;
1115 caddr_t *kvap;
1116 int flags;
1117 {
1118 vm_page_t m;
1119 vaddr_t va;
1120 bus_addr_t addr;
1121 struct pglist *mlist;
1122 int cbit;
1123
1124 /*
1125 * digest flags:
1126 */
1127 cbit = 0;
1128 if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1129 cbit |= PMAP_NVC;
1130 if (flags & BUS_DMA_NOCACHE) /* sideffects */
1131 cbit |= PMAP_NC;
1132 /*
1133 * Now take this and map it into the CPU since it should already
1134 * be in the the IOMMU.
1135 */
1136 *kvap = (caddr_t)va = segs[0].ds_addr;
1137 mlist = segs[0]._ds_mlist;
1138 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1139
1140 if (size == 0)
1141 panic("_bus_dmamem_map: size botch");
1142
1143 addr = VM_PAGE_TO_PHYS(m);
1144 pmap_enter(pmap_kernel(), va, addr | cbit,
1145 VM_PROT_READ | VM_PROT_WRITE,
1146 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1147 va += PAGE_SIZE;
1148 size -= PAGE_SIZE;
1149 }
1150
1151 return (0);
1152 }
1153
1154 /*
1155 * Unmap DVMA mappings from kernel
1156 */
1157 void
1158 sbus_dmamem_unmap(t, kva, size)
1159 bus_dma_tag_t t;
1160 caddr_t kva;
1161 size_t size;
1162 {
1163
1164 #ifdef DIAGNOSTIC
1165 if ((u_long)kva & PGOFSET)
1166 panic("_bus_dmamem_unmap");
1167 #endif
1168
1169 size = round_page(size);
1170 pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1171 }
1172