sbus.c revision 1.26 1 /* $NetBSD: sbus.c,v 1.26 2000/04/10 13:34:18 pk 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 #ifdef DDB
795 Debugger();
796 #endif
797 return (EINVAL);
798 }
799 #else
800 return (EINVAL);
801 #endif
802 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
803
804 /*
805 * XXX Need to implement "don't dma across this boundry".
806 */
807
808 s = splhigh();
809 err = extent_alloc(sc->sc_is.is_dvmamap, sgsize, NBPG,
810 map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
811 splx(s);
812
813 if (err != 0)
814 return (err);
815
816 #ifdef DEBUG
817 if (dvmaddr == (bus_addr_t)-1)
818 {
819 printf("sbus_dmamap_load(): dvmamap_alloc(%d, %x) failed!\n", sgsize, flags);
820 #ifdef DDB
821 Debugger();
822 #endif
823 }
824 #endif
825 if (dvmaddr == (bus_addr_t)-1)
826 return (ENOMEM);
827
828 /*
829 * We always use just one segment.
830 */
831 map->dm_mapsize = buflen;
832 map->dm_nsegs = 1;
833 map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
834 map->dm_segs[0].ds_len = sgsize;
835
836 if (p != NULL)
837 pmap = p->p_vmspace->vm_map.pmap;
838 else
839 pmap = pmap_kernel();
840
841 dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
842 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
843 for (; buflen > 0; ) {
844 /*
845 * Get the physical address for this page.
846 */
847 if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
848 bus_dmamap_unload(t, map);
849 return (-1);
850 }
851
852 /*
853 * Compute the segment size, and adjust counts.
854 */
855 sgsize = NBPG - ((u_long)vaddr & PGOFSET);
856 if (buflen < sgsize)
857 sgsize = buflen;
858
859 #ifdef DEBUG
860 if (sbusdebug & SDB_DVMA)
861 printf("sbus_dmamap_load: map %p loading va %lx at pa %lx\n",
862 map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
863 #endif
864 iommu_enter(&sc->sc_is, trunc_page(dvmaddr), trunc_page(curaddr), flags);
865
866 dvmaddr += PAGE_SIZE;
867 vaddr += sgsize;
868 buflen -= sgsize;
869 }
870 return (0);
871 }
872
873 void
874 sbus_dmamap_unload(t, map)
875 bus_dma_tag_t t;
876 bus_dmamap_t map;
877 {
878 vaddr_t addr;
879 int len, error, s;
880 bus_addr_t dvmaddr;
881 bus_size_t sgsize;
882 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
883
884 if (map->dm_nsegs != 1)
885 panic("sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
886
887 addr = trunc_page(map->dm_segs[0].ds_addr);
888 len = map->dm_segs[0].ds_len;
889
890 #ifdef DEBUG
891 if (sbusdebug & SDB_DVMA)
892 printf("sbus_dmamap_unload: map %p removing va %lx size %lx\n",
893 map, (long)addr, (long)len);
894 #endif
895 iommu_remove(&sc->sc_is, addr, len);
896 dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
897 sgsize = map->dm_segs[0].ds_len;
898
899 /* Mark the mappings as invalid. */
900 map->dm_mapsize = 0;
901 map->dm_nsegs = 0;
902
903 /* Unmapping is bus dependent */
904 s = splhigh();
905 error = extent_free(sc->sc_is.is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
906 splx(s);
907 if (error != 0)
908 printf("warning: %ld of DVMA space lost\n", (long)sgsize);
909
910 cache_flush((caddr_t)(u_long)dvmaddr, (u_int)sgsize);
911 }
912
913 void
914 sbus_dmamap_sync(t, map, offset, len, ops)
915 bus_dma_tag_t t;
916 bus_dmamap_t map;
917 bus_addr_t offset;
918 bus_size_t len;
919 int ops;
920 {
921 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
922 vaddr_t va = map->dm_segs[0].ds_addr + offset;
923
924 /*
925 * We only support one DMA segment; supporting more makes this code
926 * too unweildy.
927 */
928
929 if (ops&BUS_DMASYNC_PREREAD) {
930 #ifdef DEBUG
931 if (sbusdebug & SDB_DVMA)
932 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREREAD\n",
933 (long)va, (u_long)len);
934 #endif
935
936 /* Nothing to do */;
937 }
938 if (ops&BUS_DMASYNC_POSTREAD) {
939 /*
940 * We should sync the IOMMU streaming caches here first.
941 */
942 #ifdef DEBUG
943 if (sbusdebug & SDB_DVMA)
944 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTREAD\n",
945 (long)va, (u_long)len);
946 #endif
947 while (len > 0) {
948
949 /*
950 * Streaming buffer flushes:
951 *
952 * 1 Tell strbuf to flush by storing va to strbuf_pgflush
953 * If we're not on a cache line boundary (64-bits):
954 * 2 Store 0 in flag
955 * 3 Store pointer to flag in flushsync
956 * 4 wait till flushsync becomes 0x1
957 *
958 * If it takes more than .5 sec, something went wrong.
959 */
960 #ifdef DEBUG
961 if (sbusdebug & SDB_DVMA)
962 printf("sbus_dmamap_sync: flushing va %p, %lu bytes left\n",
963 (long)va, (u_long)len);
964 #endif
965 bus_space_write_8(sc->sc_bustag, &sc->sc_is.is_sb->strbuf_pgflush, 0, va);
966 if (len <= NBPG) {
967 iommu_flush(&sc->sc_is);
968 len = 0;
969 } else
970 len -= NBPG;
971 va += NBPG;
972 }
973 }
974 if (ops&BUS_DMASYNC_PREWRITE) {
975 #ifdef DEBUG
976 if (sbusdebug & SDB_DVMA)
977 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREWRITE\n",
978 (long)va, (u_long)len);
979 #endif
980 /* Nothing to do */;
981 }
982 if (ops&BUS_DMASYNC_POSTWRITE) {
983 #ifdef DEBUG
984 if (sbusdebug & SDB_DVMA)
985 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTWRITE\n",
986 (long)va, (u_long)len);
987 #endif
988 /* Nothing to do */;
989 }
990 bus_dmamap_sync(t->_parent, map, offset, len, ops);
991 }
992
993
994 /*
995 * Take memory allocated by our parent bus and generate DVMA mappings for it.
996 */
997 int
998 sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
999 bus_dma_tag_t t;
1000 bus_size_t size, alignment, boundary;
1001 bus_dma_segment_t *segs;
1002 int nsegs;
1003 int *rsegs;
1004 int flags;
1005 {
1006 paddr_t curaddr;
1007 u_long dvmaddr;
1008 vm_page_t m;
1009 struct pglist *mlist;
1010 int error;
1011 int n, s;
1012 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1013
1014 if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
1015 boundary, segs, nsegs, rsegs, flags)))
1016 return (error);
1017
1018 /*
1019 * Allocate a DVMA mapping for our new memory.
1020 */
1021 for (n = 0; n < *rsegs; n++) {
1022 #if 1
1023 s = splhigh();
1024 if (extent_alloc(sc->sc_is.is_dvmamap, segs[0].ds_len, alignment,
1025 boundary, EX_NOWAIT, &dvmaddr)) {
1026 splx(s);
1027 /* Free what we got and exit */
1028 bus_dmamem_free(t->_parent, segs, nsegs);
1029 return (ENOMEM);
1030 }
1031 splx(s);
1032 #else
1033 dvmaddr = dvmamap_alloc(segs[0].ds_len, flags);
1034 if (dvmaddr == (bus_addr_t)-1) {
1035 /* Free what we got and exit */
1036 bus_dmamem_free(t->_parent, segs, nsegs);
1037 return (ENOMEM);
1038 }
1039 #endif
1040 segs[n].ds_addr = dvmaddr;
1041 size = segs[n].ds_len;
1042 mlist = segs[n]._ds_mlist;
1043
1044 /* Map memory into DVMA space */
1045 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1046 curaddr = VM_PAGE_TO_PHYS(m);
1047 #ifdef DEBUG
1048 if (sbusdebug & SDB_DVMA)
1049 printf("sbus_dmamem_alloc: map %p loading va %lx at pa %lx\n",
1050 (long)m, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
1051 #endif
1052 iommu_enter(&sc->sc_is, dvmaddr, curaddr, flags);
1053 dvmaddr += PAGE_SIZE;
1054 }
1055 }
1056 return (0);
1057 }
1058
1059 void
1060 sbus_dmamem_free(t, segs, nsegs)
1061 bus_dma_tag_t t;
1062 bus_dma_segment_t *segs;
1063 int nsegs;
1064 {
1065 vaddr_t addr;
1066 int len;
1067 int n, s, error;
1068 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1069
1070
1071 for (n=0; n<nsegs; n++) {
1072 addr = segs[n].ds_addr;
1073 len = segs[n].ds_len;
1074 iommu_remove(&sc->sc_is, addr, len);
1075 #if 1
1076 s = splhigh();
1077 error = extent_free(sc->sc_is.is_dvmamap, addr, len, EX_NOWAIT);
1078 splx(s);
1079 if (error != 0)
1080 printf("warning: %ld of DVMA space lost\n", (long)len);
1081 #else
1082 dvmamap_free(addr, len);
1083 #endif
1084 }
1085 bus_dmamem_free(t->_parent, segs, nsegs);
1086 }
1087
1088 /*
1089 * Map the DVMA mappings into the kernel pmap.
1090 * Check the flags to see whether we're streaming or coherent.
1091 */
1092 int
1093 sbus_dmamem_map(t, segs, nsegs, size, kvap, flags)
1094 bus_dma_tag_t t;
1095 bus_dma_segment_t *segs;
1096 int nsegs;
1097 size_t size;
1098 caddr_t *kvap;
1099 int flags;
1100 {
1101 vm_page_t m;
1102 vaddr_t va;
1103 bus_addr_t addr;
1104 struct pglist *mlist;
1105 int cbit;
1106
1107 /*
1108 * digest flags:
1109 */
1110 cbit = 0;
1111 if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1112 cbit |= PMAP_NVC;
1113 if (flags & BUS_DMA_NOCACHE) /* sideffects */
1114 cbit |= PMAP_NC;
1115 /*
1116 * Now take this and map it into the CPU since it should already
1117 * be in the IOMMU.
1118 */
1119 *kvap = (caddr_t)va = segs[0].ds_addr;
1120 mlist = segs[0]._ds_mlist;
1121 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1122
1123 if (size == 0)
1124 panic("_bus_dmamem_map: size botch");
1125
1126 addr = VM_PAGE_TO_PHYS(m);
1127 pmap_enter(pmap_kernel(), va, addr | cbit,
1128 VM_PROT_READ | VM_PROT_WRITE,
1129 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1130 va += PAGE_SIZE;
1131 size -= PAGE_SIZE;
1132 }
1133
1134 return (0);
1135 }
1136
1137 /*
1138 * Unmap DVMA mappings from kernel
1139 */
1140 void
1141 sbus_dmamem_unmap(t, kva, size)
1142 bus_dma_tag_t t;
1143 caddr_t kva;
1144 size_t size;
1145 {
1146
1147 #ifdef DIAGNOSTIC
1148 if ((u_long)kva & PGOFSET)
1149 panic("_bus_dmamem_unmap");
1150 #endif
1151
1152 size = round_page(size);
1153 pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1154 }
1155