psycho.c revision 1.37 1 /* $NetBSD: psycho.c,v 1.37 2001/09/24 23:49:32 eeh Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include "opt_ddb.h"
32
33 /*
34 * Support for `psycho' and `psycho+' UPA to PCI bridge and
35 * UltraSPARC IIi and IIe `sabre' PCI controllers.
36 */
37
38 #undef DEBUG
39 #define DEBUG
40
41 #ifdef DEBUG
42 #define PDB_PROM 0x01
43 #define PDB_BUSMAP 0x02
44 #define PDB_INTR 0x04
45 int psycho_debug = 0x0;
46 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0)
47 #else
48 #define DPRINTF(l, s)
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/device.h>
53 #include <sys/errno.h>
54 #include <sys/extent.h>
55 #include <sys/malloc.h>
56 #include <sys/systm.h>
57 #include <sys/time.h>
58 #include <sys/reboot.h>
59
60 #define _SPARC_BUS_DMA_PRIVATE
61 #include <machine/bus.h>
62 #include <machine/autoconf.h>
63 #include <machine/psl.h>
64
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67
68 #include <sparc64/dev/iommureg.h>
69 #include <sparc64/dev/iommuvar.h>
70 #include <sparc64/dev/psychoreg.h>
71 #include <sparc64/dev/psychovar.h>
72 #include <sparc64/sparc64/cache.h>
73
74 #include "ioconf.h"
75
76 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
77 pci_chipset_tag_t));
78 static void psycho_get_bus_range __P((int, int *));
79 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
80 static void psycho_set_intr __P((struct psycho_softc *, int, void *,
81 u_int64_t *, u_int64_t *));
82
83 /* Interrupt handlers */
84 static int psycho_ue __P((void *));
85 static int psycho_ce __P((void *));
86 static int psycho_bus_a __P((void *));
87 static int psycho_bus_b __P((void *));
88 static int psycho_powerfail __P((void *));
89 static int psycho_wakeup __P((void *));
90
91
92 /* IOMMU support */
93 static void psycho_iommu_init __P((struct psycho_softc *, int));
94
95 /*
96 * bus space and bus dma support for UltraSPARC `psycho'. note that most
97 * of the bus dma support is provided by the iommu dvma controller.
98 */
99 static paddr_t psycho_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, int, int));
100 static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
101 bus_size_t, int, vaddr_t,
102 bus_space_handle_t *));
103 static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
104 int (*) __P((void *)), void *));
105
106 static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
107 bus_size_t, struct proc *, int));
108 static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
109 static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
110 bus_dma_segment_t *, int, bus_size_t, int));
111 static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
112 bus_size_t, int));
113 int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
114 bus_dma_segment_t *, int, int *, int));
115 void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
116 int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
117 caddr_t *, int));
118 void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
119
120 /* base pci_chipset */
121 extern struct sparc_pci_chipset _sparc_pci_chipset;
122
123 /*
124 * autoconfiguration
125 */
126 static int psycho_match __P((struct device *, struct cfdata *, void *));
127 static void psycho_attach __P((struct device *, struct device *, void *));
128 static int psycho_print __P((void *aux, const char *p));
129
130 struct cfattach psycho_ca = {
131 sizeof(struct psycho_softc), psycho_match, psycho_attach
132 };
133
134 /*
135 * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge. It manages a
136 * single PCI bus and does not have a streaming buffer. It often has an APB
137 * (advanced PCI bridge) connected to it, which was designed specifically for
138 * the IIi. The APB let's the IIi handle two independednt PCI buses, and
139 * appears as two "simba"'s underneath the sabre.
140 *
141 * "psycho" and "psycho+" is a dual UPA to PCI bridge. It sits on the UPA bus
142 * and manages two PCI buses. "psycho" has two 64-bit 33MHz buses, while
143 * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus. You
144 * will usually find a "psycho+" since I don't think the original "psycho"
145 * ever shipped, and if it did it would be in the U30.
146 *
147 * Each "psycho" PCI bus appears as a separate OFW node, but since they are
148 * both part of the same IC, they only have a single register space. As such,
149 * they need to be configured together, even though the autoconfiguration will
150 * attach them separately.
151 *
152 * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
153 * as pci1 and pci2, although they have been implemented with other PCI bus
154 * numbers on some machines.
155 *
156 * On UltraII machines, there can be any number of "psycho+" ICs, each
157 * providing two PCI buses.
158 *
159 *
160 * XXXX The psycho/sabre node has an `interrupts' attribute. They contain
161 * the values of the following interrupts in this order:
162 *
163 * PCI Bus Error (30)
164 * DMA UE (2e)
165 * DMA CE (2f)
166 * Power Fail (25)
167 *
168 * We really should attach handlers for each.
169 *
170 */
171
172 #define ROM_PCI_NAME "pci"
173
174 struct psycho_names {
175 char *p_name;
176 int p_type;
177 } psycho_names[] = {
178 { "SUNW,psycho", PSYCHO_MODE_PSYCHO },
179 { "pci108e,8000", PSYCHO_MODE_PSYCHO },
180 { "SUNW,sabre", PSYCHO_MODE_SABRE },
181 { "pci108e,a000", PSYCHO_MODE_SABRE },
182 { "pci108e,a001", PSYCHO_MODE_SABRE },
183 { NULL, 0 }
184 };
185
186 static int
187 psycho_match(parent, match, aux)
188 struct device *parent;
189 struct cfdata *match;
190 void *aux;
191 {
192 struct mainbus_attach_args *ma = aux;
193 char *model = getpropstring(ma->ma_node, "model");
194 int i;
195
196 /* match on a name of "pci" and a sabre or a psycho */
197 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
198 for (i=0; psycho_names[i].p_name; i++)
199 if (strcmp(model, psycho_names[i].p_name) == 0)
200 return (1);
201
202 model = getpropstring(ma->ma_node, "compatible");
203 for (i=0; psycho_names[i].p_name; i++)
204 if (strcmp(model, psycho_names[i].p_name) == 0)
205 return (1);
206 }
207 return (0);
208 }
209
210 /*
211 * SUNW,psycho initialisation ..
212 * - find the per-psycho registers
213 * - figure out the IGN.
214 * - find our partner psycho
215 * - configure ourselves
216 * - bus range, bus,
217 * - get interrupt-map and interrupt-map-mask
218 * - setup the chipsets.
219 * - if we're the first of the pair, initialise the IOMMU, otherwise
220 * just copy it's tags and addresses.
221 */
222 static void
223 psycho_attach(parent, self, aux)
224 struct device *parent, *self;
225 void *aux;
226 {
227 struct psycho_softc *sc = (struct psycho_softc *)self;
228 struct psycho_softc *osc = NULL;
229 struct psycho_pbm *pp;
230 struct pcibus_attach_args pba;
231 struct mainbus_attach_args *ma = aux;
232 bus_space_handle_t bh;
233 u_int64_t csr;
234 int psycho_br[2], n, i;
235 struct pci_ctl *pci_ctl;
236 char *model = getpropstring(ma->ma_node, "model");
237
238 printf("\n");
239
240 sc->sc_node = ma->ma_node;
241 sc->sc_bustag = ma->ma_bustag;
242 sc->sc_dmatag = ma->ma_dmatag;
243
244 /*
245 * call the model-specific initialisation routine.
246 */
247 for (i=0; psycho_names[i].p_name; i++)
248 if (strcmp(model, psycho_names[i].p_name) == 0) {
249 sc->sc_mode = psycho_names[i].p_type;
250 goto found;
251 }
252
253 model = getpropstring(ma->ma_node, "compatible");
254 for (i=0; psycho_names[i].p_name; i++)
255 if (strcmp(model, psycho_names[i].p_name) == 0) {
256 sc->sc_mode = psycho_names[i].p_type;
257 goto found;
258 }
259
260 panic("unknown psycho model %s", model);
261 found:
262
263 /*
264 * The psycho gets three register banks:
265 * (0) per-PBM configuration and status registers
266 * (1) per-PBM PCI configuration space, containing only the
267 * PBM 256-byte PCI header
268 * (2) the shared psycho configuration registers (struct psychoreg)
269 *
270 * XXX use the prom address for the psycho registers? we do so far.
271 */
272
273 /* Register layouts are different. stuupid. */
274 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
275 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
276
277 if (ma->ma_naddress > 2) {
278 sc->sc_regs = (struct psychoreg *)
279 (u_long)ma->ma_address[2];
280 pci_ctl = (struct pci_ctl *)
281 (u_long)ma->ma_address[0];
282 } else if (ma->ma_nreg > 2) {
283 bus_space_handle_t handle;
284
285 /* We need to map this in ourselves. */
286 if (bus_space_map2(sc->sc_bustag, 0,
287 ma->ma_reg[2].ur_paddr,
288 ma->ma_reg[2].ur_len, 0, NULL, &handle))
289 panic("psycho_attach: cannot map regs");
290 sc->sc_regs = (struct psychoreg *)(u_long)handle;
291
292 if (bus_space_map2(sc->sc_bustag, 0,
293 ma->ma_reg[0].ur_paddr,
294 ma->ma_reg[0].ur_len, 0, NULL, &handle))
295 panic("psycho_attach: cannot map ctl");
296 /* XXX -- this is lost but never unmapped */
297 pci_ctl = (struct pci_ctl *)(u_long)handle;
298
299 } else
300 panic("psycho_attach: %d not enough registers",
301 ma->ma_nreg);
302 } else {
303 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
304
305 if (ma->ma_naddress) {
306 sc->sc_regs = (struct psychoreg *)
307 (u_long)ma->ma_address[0];
308 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
309 } else if (ma->ma_nreg) {
310 bus_space_handle_t handle;
311
312 /* We need to map this in ourselves. */
313 if (bus_space_map2(sc->sc_bustag, 0,
314 ma->ma_reg[0].ur_paddr,
315 ma->ma_reg[0].ur_len, 0, NULL, &handle))
316 panic("psycho_attach: cannot map regs");
317 sc->sc_regs = (struct psychoreg *)(u_long)handle;
318 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
319 } else
320 panic("psycho_attach: %d not enough registers",
321 ma->ma_nreg);
322 }
323
324 csr = sc->sc_regs->psy_csr;
325 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
326 if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
327 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
328
329 printf("%s: impl %d, version %d: ign %x ",
330 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
331 sc->sc_ign);
332 /*
333 * Match other psycho's that are already configured against
334 * the base physical address. This will be the same for a
335 * pair of devices that share register space.
336 */
337 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
338
339 struct psycho_softc *asc =
340 (struct psycho_softc *)psycho_cd.cd_devs[n];
341
342 if (asc == NULL || asc == sc)
343 /* This entry is not there or it is me */
344 continue;
345
346 if (asc->sc_basepaddr != sc->sc_basepaddr)
347 /* This is an unrelated psycho */
348 continue;
349
350 /* Found partner */
351 osc = asc;
352 break;
353 }
354
355
356 /* Oh, dear. OK, lets get started */
357
358 /*
359 * Setup the PCI control register
360 */
361 csr = bus_space_read_8(sc->sc_bustag,
362 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
363 csr |= PCICTL_MRLM |
364 PCICTL_ARB_PARK |
365 PCICTL_ERRINTEN |
366 PCICTL_4ENABLE;
367 csr &= ~(PCICTL_SERR |
368 PCICTL_CPU_PRIO |
369 PCICTL_ARB_PRIO |
370 PCICTL_RTRYWAIT);
371 bus_space_write_8(sc->sc_bustag,
372 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
373
374
375 /*
376 * Allocate our psycho_pbm
377 */
378 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
379 if (pp == NULL)
380 panic("could not allocate psycho pbm");
381
382 memset(pp, 0, sizeof *pp);
383
384 pp->pp_sc = sc;
385
386 /* grab the psycho ranges */
387 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
388
389 /* get the bus-range for the psycho */
390 psycho_get_bus_range(sc->sc_node, psycho_br);
391
392 pba.pba_bus = psycho_br[0];
393
394 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
395 printf("; PCI bus %d", psycho_br[0]);
396
397 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
398
399 /* allocate our tags */
400 pp->pp_memt = psycho_alloc_mem_tag(pp);
401 pp->pp_iot = psycho_alloc_io_tag(pp);
402 pp->pp_dmat = psycho_alloc_dma_tag(pp);
403 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
404 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
405
406 /* allocate a chipset for this */
407 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
408
409 /* setup the rest of the psycho pbm */
410 pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
411
412 printf("\n");
413
414 /*
415 * And finally, if we're a sabre or the first of a pair of psycho's to
416 * arrive here, start up the IOMMU and get a config space tag.
417 */
418 if (osc == NULL) {
419
420 /*
421 * Establish handlers for interesting interrupts....
422 *
423 * XXX We need to remember these and remove this to support
424 * hotplug on the UPA/FHC bus.
425 *
426 * XXX Not all controllers have these, but installing them
427 * is better than trying to sort through this mess.
428 */
429 psycho_set_intr(sc, 15, psycho_ue,
430 &sc->sc_regs->ue_int_map,
431 &sc->sc_regs->ue_clr_int);
432 psycho_set_intr(sc, 1, psycho_ce,
433 &sc->sc_regs->ce_int_map,
434 &sc->sc_regs->ce_clr_int);
435 psycho_set_intr(sc, 15, psycho_bus_a,
436 &sc->sc_regs->pciaerr_int_map,
437 &sc->sc_regs->pciaerr_clr_int);
438 psycho_set_intr(sc, 15, psycho_bus_b,
439 &sc->sc_regs->pciberr_int_map,
440 &sc->sc_regs->pciberr_clr_int);
441 psycho_set_intr(sc, 15, psycho_powerfail,
442 &sc->sc_regs->power_int_map,
443 &sc->sc_regs->power_clr_int);
444 psycho_set_intr(sc, 1, psycho_wakeup,
445 &sc->sc_regs->pwrmgt_int_map,
446 &sc->sc_regs->pwrmgt_clr_int);
447
448 /*
449 * Setup IOMMU and PCI configuration if we're the first
450 * of a pair of psycho's to arrive here.
451 *
452 * We should calculate a TSB size based on amount of RAM
453 * and number of bus controllers and number an type of
454 * child devices.
455 *
456 * For the moment, 32KB should be more than enough.
457 */
458 psycho_iommu_init(sc, 2);
459
460 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
461 if (bus_space_map2(sc->sc_bustag,
462 PCI_CONFIG_BUS_SPACE,
463 sc->sc_basepaddr + 0x01000000,
464 0x0100000,
465 0,
466 0,
467 &bh))
468 panic("could not map psycho PCI configuration space");
469 sc->sc_configaddr = (off_t)bh;
470 } else {
471 /* Just copy IOMMU state, config tag and address */
472 sc->sc_is = osc->sc_is;
473 sc->sc_configtag = osc->sc_configtag;
474 sc->sc_configaddr = osc->sc_configaddr;
475 }
476
477 /*
478 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
479 */
480 pba.pba_busname = "pci";
481 pba.pba_flags = sc->sc_psycho_this->pp_flags;
482 pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
483 pba.pba_iot = sc->sc_psycho_this->pp_iot;
484 pba.pba_memt = sc->sc_psycho_this->pp_memt;
485
486 config_found(self, &pba, psycho_print);
487 }
488
489 static int
490 psycho_print(aux, p)
491 void *aux;
492 const char *p;
493 {
494
495 if (p == NULL)
496 return (UNCONF);
497 return (QUIET);
498 }
499
500 static void
501 psycho_set_intr(sc, ipl, handler, mapper, clearer)
502 struct psycho_softc *sc;
503 int ipl;
504 void *handler;
505 u_int64_t *mapper;
506 u_int64_t *clearer;
507 {
508 struct intrhand *ih;
509
510 ih = (struct intrhand *)malloc(sizeof(struct intrhand),
511 M_DEVBUF, M_NOWAIT);
512 ih->ih_arg = sc;
513 ih->ih_map = mapper;
514 ih->ih_clr = clearer;
515 ih->ih_fun = handler;
516 ih->ih_pil = (1<<ipl);
517 ih->ih_number = INTVEC(*(ih->ih_map));
518 intr_establish(ipl, ih);
519 *(ih->ih_map) |= INTMAP_V;
520 }
521
522 /*
523 * PCI bus support
524 */
525
526 /*
527 * allocate a PCI chipset tag and set it's cookie.
528 */
529 static pci_chipset_tag_t
530 psycho_alloc_chipset(pp, node, pc)
531 struct psycho_pbm *pp;
532 int node;
533 pci_chipset_tag_t pc;
534 {
535 pci_chipset_tag_t npc;
536
537 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
538 if (npc == NULL)
539 panic("could not allocate pci_chipset_tag_t");
540 memcpy(npc, pc, sizeof *pc);
541 npc->cookie = pp;
542 npc->rootnode = node;
543 npc->curnode = node;
544
545 return (npc);
546 }
547
548 /*
549 * grovel the OBP for various psycho properties
550 */
551 static void
552 psycho_get_bus_range(node, brp)
553 int node;
554 int *brp;
555 {
556 int n;
557
558 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
559 panic("could not get psycho bus-range");
560 if (n != 2)
561 panic("broken psycho bus-range");
562 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
563 }
564
565 static void
566 psycho_get_ranges(node, rp, np)
567 int node;
568 struct psycho_ranges **rp;
569 int *np;
570 {
571
572 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
573 panic("could not get psycho ranges");
574 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
575 }
576
577 /*
578 * Interrupt handlers.
579 */
580
581 static int
582 psycho_ue(arg)
583 void *arg;
584 {
585 struct psycho_softc *sc = (struct psycho_softc *)arg;
586 struct psychoreg *regs = sc->sc_regs;
587 long long afsr = regs->psy_ue_afsr;
588 long long afar = regs->psy_ue_afar;
589 char bits[128];
590
591 /*
592 * It's uncorrectable. Dump the regs and panic.
593 */
594 panic("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s",
595 sc->sc_dev.dv_xname, afar,
596 (long long)iommu_extract(sc->sc_is, (vaddr_t)afar), afsr,
597 bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
598 bits, sizeof(bits)));
599 return (1);
600 }
601 static int
602 psycho_ce(arg)
603 void *arg;
604 {
605 struct psycho_softc *sc = (struct psycho_softc *)arg;
606 struct psychoreg *regs = sc->sc_regs;
607
608 /*
609 * It's correctable. Dump the regs and continue.
610 */
611
612 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
613 sc->sc_dev.dv_xname,
614 (long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
615 return (1);
616 }
617 static int
618 psycho_bus_a(arg)
619 void *arg;
620 {
621 struct psycho_softc *sc = (struct psycho_softc *)arg;
622 struct psychoreg *regs = sc->sc_regs;
623
624 /*
625 * It's uncorrectable. Dump the regs and panic.
626 */
627
628 panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
629 sc->sc_dev.dv_xname,
630 (long long)regs->psy_pcictl[0].pci_afar,
631 (long long)regs->psy_pcictl[0].pci_afsr);
632 return (1);
633 }
634 static int
635 psycho_bus_b(arg)
636 void *arg;
637 {
638 struct psycho_softc *sc = (struct psycho_softc *)arg;
639 struct psychoreg *regs = sc->sc_regs;
640
641 /*
642 * It's uncorrectable. Dump the regs and panic.
643 */
644
645 panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
646 sc->sc_dev.dv_xname,
647 (long long)regs->psy_pcictl[0].pci_afar,
648 (long long)regs->psy_pcictl[0].pci_afsr);
649 return (1);
650 }
651 static int
652 psycho_powerfail(arg)
653 void *arg;
654 {
655
656 /*
657 * We lost power. Try to shut down NOW.
658 */
659 printf("Power Failure Detected: Shutting down NOW.\n");
660 cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
661 return (1);
662 }
663 static
664 int psycho_wakeup(arg)
665 void *arg;
666 {
667 struct psycho_softc *sc = (struct psycho_softc *)arg;
668
669 /*
670 * Gee, we don't really have a framework to deal with this
671 * properly.
672 */
673 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname);
674 return (1);
675 }
676
677
678
679 /*
680 * initialise the IOMMU..
681 */
682 void
683 psycho_iommu_init(sc, tsbsize)
684 struct psycho_softc *sc;
685 int tsbsize;
686 {
687 char *name;
688 struct iommu_state *is;
689 u_int32_t iobase = -1;
690 int *vdma = NULL;
691 int nitem;
692
693 is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
694 if (is == NULL)
695 panic("psycho_iommu_init: malloc is");
696
697 sc->sc_is = is;
698
699 /* punch in our copies */
700 is->is_bustag = sc->sc_bustag;
701 is->is_iommu = &sc->sc_regs->psy_iommu;
702
703 if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
704 is->is_sb = 0;
705 else
706 is->is_sb = &sc->sc_regs->psy_iommu_strbuf;
707
708 /*
709 * Separate the men from the boys. Get the `virtual-dma'
710 * property for sabre and use that to make sure the damn
711 * iommu works.
712 *
713 * We could query the `#virtual-dma-size-cells' and
714 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
715 */
716 if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
717 (void **)&vdma)) {
718 /* Damn. Gotta use these values. */
719 iobase = vdma[0];
720 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break
721 switch (vdma[1]) {
722 TSBCASE(1); TSBCASE(2); TSBCASE(3);
723 TSBCASE(4); TSBCASE(5); TSBCASE(6);
724 default:
725 printf("bogus tsb size %x, using 7\n", vdma[1]);
726 TSBCASE(7);
727 }
728 #undef TSBCASE
729 }
730
731 /* give us a nice name.. */
732 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
733 if (name == 0)
734 panic("couldn't malloc iommu name");
735 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
736
737 iommu_init(name, is, tsbsize, iobase);
738 }
739
740 /*
741 * below here is bus space and bus dma support
742 */
743 bus_space_tag_t
744 psycho_alloc_bus_tag(pp, type)
745 struct psycho_pbm *pp;
746 int type;
747 {
748 struct psycho_softc *sc = pp->pp_sc;
749 bus_space_tag_t bt;
750
751 bt = (bus_space_tag_t)
752 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
753 if (bt == NULL)
754 panic("could not allocate psycho bus tag");
755
756 bzero(bt, sizeof *bt);
757 bt->cookie = pp;
758 bt->parent = sc->sc_bustag;
759 bt->type = type;
760 bt->sparc_bus_map = _psycho_bus_map;
761 bt->sparc_bus_mmap = psycho_bus_mmap;
762 bt->sparc_intr_establish = psycho_intr_establish;
763 return (bt);
764 }
765
766 bus_dma_tag_t
767 psycho_alloc_dma_tag(pp)
768 struct psycho_pbm *pp;
769 {
770 struct psycho_softc *sc = pp->pp_sc;
771 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
772
773 dt = (bus_dma_tag_t)
774 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
775 if (dt == NULL)
776 panic("could not allocate psycho dma tag");
777
778 bzero(dt, sizeof *dt);
779 dt->_cookie = pp;
780 dt->_parent = pdt;
781 #define PCOPY(x) dt->x = pdt->x
782 PCOPY(_dmamap_create);
783 PCOPY(_dmamap_destroy);
784 dt->_dmamap_load = psycho_dmamap_load;
785 PCOPY(_dmamap_load_mbuf);
786 PCOPY(_dmamap_load_uio);
787 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
788 dt->_dmamap_unload = psycho_dmamap_unload;
789 dt->_dmamap_sync = psycho_dmamap_sync;
790 dt->_dmamem_alloc = psycho_dmamem_alloc;
791 dt->_dmamem_free = psycho_dmamem_free;
792 dt->_dmamem_map = psycho_dmamem_map;
793 dt->_dmamem_unmap = psycho_dmamem_unmap;
794 PCOPY(_dmamem_mmap);
795 #undef PCOPY
796 return (dt);
797 }
798
799 /*
800 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
801 * PCI physical addresses.
802 */
803
804 static int get_childspace __P((int));
805
806 static int
807 get_childspace(type)
808 int type;
809 {
810 int ss;
811
812 switch (type) {
813 case PCI_CONFIG_BUS_SPACE:
814 ss = 0x00;
815 break;
816 case PCI_IO_BUS_SPACE:
817 ss = 0x01;
818 break;
819 case PCI_MEMORY_BUS_SPACE:
820 ss = 0x02;
821 break;
822 #if 0
823 /* we don't do 64 bit memory space */
824 case PCI_MEMORY64_BUS_SPACE:
825 ss = 0x03;
826 break;
827 #endif
828 default:
829 panic("get_childspace: unknown bus type");
830 }
831
832 return (ss);
833 }
834
835 static int
836 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
837 bus_space_tag_t t;
838 bus_type_t btype;
839 bus_addr_t offset;
840 bus_size_t size;
841 int flags;
842 vaddr_t vaddr;
843 bus_space_handle_t *hp;
844 {
845 struct psycho_pbm *pp = t->cookie;
846 struct psycho_softc *sc = pp->pp_sc;
847 int i, ss;
848
849 DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, (unsigned long long)offset, (unsigned long long)size, flags,
850 (void *)vaddr));
851
852 ss = get_childspace(t->type);
853 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
854
855 for (i = 0; i < pp->pp_nrange; i++) {
856 bus_addr_t paddr;
857
858 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
859 continue;
860
861 paddr = pp->pp_range[i].phys_lo + offset;
862 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
863 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
864 (long)ss, (long)offset,
865 (unsigned long long)paddr));
866 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
867 size, flags, vaddr, hp));
868 }
869 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
870 return (EINVAL);
871 }
872
873 static paddr_t
874 psycho_bus_mmap(t, paddr, off, prot, flags)
875 bus_space_tag_t t;
876 bus_addr_t paddr;
877 off_t off;
878 int prot;
879 int flags;
880 {
881 bus_addr_t offset = paddr;
882 struct psycho_pbm *pp = t->cookie;
883 struct psycho_softc *sc = pp->pp_sc;
884 int i, ss;
885
886 ss = get_childspace(t->type);
887
888 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
889 prot, flags, (unsigned long long)paddr));
890
891 for (i = 0; i < pp->pp_nrange; i++) {
892 bus_addr_t paddr;
893
894 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
895 continue;
896
897 paddr = pp->pp_range[i].phys_lo + offset;
898 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
899 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
900 "space %lx offset %lx paddr %qx\n",
901 (long)ss, (long)offset,
902 (unsigned long long)paddr));
903 return (bus_space_mmap(sc->sc_bustag, paddr, off,
904 prot, flags));
905 }
906
907 return (-1);
908 }
909
910
911 /*
912 * install an interrupt handler for a PCI device
913 */
914 void *
915 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
916 bus_space_tag_t t;
917 int ihandle;
918 int level;
919 int flags;
920 int (*handler) __P((void *));
921 void *arg;
922 {
923 struct psycho_pbm *pp = t->cookie;
924 struct psycho_softc *sc = pp->pp_sc;
925 struct intrhand *ih;
926 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
927 int64_t intrmap = 0;
928 int ino;
929 long vec = INTVEC(ihandle);
930
931 ih = (struct intrhand *)
932 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
933 if (ih == NULL)
934 return (NULL);
935
936 /*
937 * Hunt through all the interrupt mapping regs to look for our
938 * interrupt vector.
939 *
940 * XXX We only compare INOs rather than IGNs since the firmware may
941 * not provide the IGN and the IGN is constant for all device on that
942 * PCI controller. This could cause problems for the FFB/external
943 * interrupt which has a full vector that can be set arbitrarily.
944 */
945
946
947 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
948 ino = INTINO(vec);
949 DPRINTF(PDB_INTR, (" ino %x", ino));
950
951 /* If the device didn't ask for an IPL, use the one encoded. */
952 if (level == IPL_NONE) level = INTLEV(vec);
953 /* If it still has no level, print a warning and assign IPL 2 */
954 if (level == IPL_NONE) {
955 printf("ERROR: no IPL, setting IPL 2.\n");
956 level = 2;
957 }
958
959 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
960
961 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
962 (long)ino, intrlev[ino]));
963
964 /* Hunt thru obio first */
965 for (intrmapptr = &sc->sc_regs->scsi_int_map,
966 intrclrptr = &sc->sc_regs->scsi_clr_int;
967 intrmapptr <= &sc->sc_regs->ffb1_int_map;
968 intrmapptr++, intrclrptr++) {
969 if (INTINO(*intrmapptr) == ino)
970 goto found;
971 }
972
973 /* Now do PCI interrupts */
974 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
975 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
976 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
977 intrmapptr++, intrclrptr += 4) {
978 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
979 intrclrptr += vec & 0x3;
980 goto found;
981 }
982 }
983 printf("Cannot find interrupt vector %lx\n", vec);
984 return (NULL);
985
986 found:
987 /* Register the map and clear intr registers */
988 ih->ih_map = intrmapptr;
989 ih->ih_clr = intrclrptr;
990 }
991 #ifdef NOT_DEBUG
992 if (psycho_debug & PDB_INTR) {
993 long i;
994
995 for (i = 0; i < 500000000; i++)
996 continue;
997 }
998 #endif
999
1000 ih->ih_fun = handler;
1001 ih->ih_arg = arg;
1002 ih->ih_pil = level;
1003 ih->ih_number = ino | sc->sc_ign;
1004
1005 DPRINTF(PDB_INTR, (
1006 "; installing handler %p arg %p with ino %u pil %u\n",
1007 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1008
1009 intr_establish(ih->ih_pil, ih);
1010
1011 /*
1012 * Enable the interrupt now we have the handler installed.
1013 * Read the current value as we can't change it besides the
1014 * valid bit so so make sure only this bit is changed.
1015 *
1016 * XXXX --- we really should use bus_space for this.
1017 */
1018 if (intrmapptr) {
1019 intrmap = *intrmapptr;
1020 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1021 (unsigned long long)intrmap));
1022
1023 /* Enable the interrupt */
1024 intrmap |= INTMAP_V;
1025 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1026 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1027 (unsigned long long)intrmap));
1028 *intrmapptr = intrmap;
1029 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1030 (unsigned long long)(intrmap = *intrmapptr)));
1031 }
1032 return (ih);
1033 }
1034
1035 /*
1036 * hooks into the iommu dvma calls.
1037 */
1038 int
1039 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1040 bus_dma_tag_t t;
1041 bus_dmamap_t map;
1042 void *buf;
1043 bus_size_t buflen;
1044 struct proc *p;
1045 int flags;
1046 {
1047 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1048 struct psycho_softc *sc = pp->pp_sc;
1049
1050 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1051 }
1052
1053 void
1054 psycho_dmamap_unload(t, map)
1055 bus_dma_tag_t t;
1056 bus_dmamap_t map;
1057 {
1058 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1059 struct psycho_softc *sc = pp->pp_sc;
1060
1061 iommu_dvmamap_unload(t, sc->sc_is, map);
1062 }
1063
1064 int
1065 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1066 bus_dma_tag_t t;
1067 bus_dmamap_t map;
1068 bus_dma_segment_t *segs;
1069 int nsegs;
1070 bus_size_t size;
1071 int flags;
1072 {
1073 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1074 struct psycho_softc *sc = pp->pp_sc;
1075
1076 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1077 }
1078
1079 void
1080 psycho_dmamap_sync(t, map, offset, len, ops)
1081 bus_dma_tag_t t;
1082 bus_dmamap_t map;
1083 bus_addr_t offset;
1084 bus_size_t len;
1085 int ops;
1086 {
1087 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1088 struct psycho_softc *sc = pp->pp_sc;
1089
1090 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1091 /* Flush the CPU then the IOMMU */
1092 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1093 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1094 }
1095 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1096 /* Flush the IOMMU then the CPU */
1097 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1098 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1099 }
1100
1101 }
1102
1103 int
1104 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1105 bus_dma_tag_t t;
1106 bus_size_t size;
1107 bus_size_t alignment;
1108 bus_size_t boundary;
1109 bus_dma_segment_t *segs;
1110 int nsegs;
1111 int *rsegs;
1112 int flags;
1113 {
1114 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1115 struct psycho_softc *sc = pp->pp_sc;
1116
1117 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1118 segs, nsegs, rsegs, flags));
1119 }
1120
1121 void
1122 psycho_dmamem_free(t, segs, nsegs)
1123 bus_dma_tag_t t;
1124 bus_dma_segment_t *segs;
1125 int nsegs;
1126 {
1127 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1128 struct psycho_softc *sc = pp->pp_sc;
1129
1130 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1131 }
1132
1133 int
1134 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1135 bus_dma_tag_t t;
1136 bus_dma_segment_t *segs;
1137 int nsegs;
1138 size_t size;
1139 caddr_t *kvap;
1140 int flags;
1141 {
1142 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1143 struct psycho_softc *sc = pp->pp_sc;
1144
1145 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1146 }
1147
1148 void
1149 psycho_dmamem_unmap(t, kva, size)
1150 bus_dma_tag_t t;
1151 caddr_t kva;
1152 size_t size;
1153 {
1154 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1155 struct psycho_softc *sc = pp->pp_sc;
1156
1157 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1158 }
1159