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