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