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