psycho.c revision 1.42 1 /* $NetBSD: psycho.c,v 1.42 2002/02/12 20:50:47 martin 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 = PROM_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 = PROM_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 = PROM_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 = PROM_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 uint64_t timeo;
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 /*
451 * Apparently a number of machines with psycho and psycho+
452 * controllers have interrupt latency issues. We'll try
453 * setting the interrupt retry timeout to 0xff which gives us
454 * a retry of 3-6 usec (which is what sysio is set to) for the
455 * moment, which seems to help alleviate this problem.
456 */
457 timeo = bus_space_read_8(sc->sc_bustag,
458 (bus_space_handle_t)
459 (u_long)&sc->sc_regs->intr_retry_timer, 0);
460 if (timeo > 0xfff) {
461 #ifdef DEBUG
462 printf("decreasing interrupt retry timeout "
463 "from %lx to 0xff\n", (long)timeo);
464 #endif
465 bus_space_write_8(sc->sc_bustag,
466 (bus_space_handle_t)
467 (u_long)&sc->sc_regs->intr_retry_timer, 0,
468 0xff);
469 }
470
471 /*
472 * Setup IOMMU and PCI configuration if we're the first
473 * of a pair of psycho's to arrive here.
474 *
475 * We should calculate a TSB size based on amount of RAM
476 * and number of bus controllers and number an type of
477 * child devices.
478 *
479 * For the moment, 32KB should be more than enough.
480 */
481 sc->sc_is = malloc(sizeof(struct iommu_state),
482 M_DEVBUF, M_NOWAIT);
483 if (sc->sc_is == NULL)
484 panic("psycho_attach: malloc iommu_state");
485
486
487 sc->sc_is->is_sb[0] = 0;
488 sc->sc_is->is_sb[1] = 0;
489 if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
490 sc->sc_is->is_sb[0] = &pci_ctl->pci_strbuf;
491
492 psycho_iommu_init(sc, 2);
493
494 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
495 if (bus_space_map2(sc->sc_bustag,
496 PCI_CONFIG_BUS_SPACE,
497 sc->sc_basepaddr + 0x01000000,
498 0x0100000,
499 0,
500 0,
501 &bh))
502 panic("could not map psycho PCI configuration space");
503 sc->sc_configaddr = (off_t)bh;
504 } else {
505 /* Just copy IOMMU state, config tag and address */
506 sc->sc_is = osc->sc_is;
507 sc->sc_configtag = osc->sc_configtag;
508 sc->sc_configaddr = osc->sc_configaddr;
509
510 if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
511 sc->sc_is->is_sb[1] = &pci_ctl->pci_strbuf;
512 iommu_reset(sc->sc_is);
513 }
514
515 /*
516 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
517 */
518 pba.pba_busname = "pci";
519 pba.pba_flags = sc->sc_psycho_this->pp_flags;
520 pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
521 pba.pba_iot = sc->sc_psycho_this->pp_iot;
522 pba.pba_memt = sc->sc_psycho_this->pp_memt;
523
524 config_found(self, &pba, psycho_print);
525 }
526
527 static int
528 psycho_print(aux, p)
529 void *aux;
530 const char *p;
531 {
532
533 if (p == NULL)
534 return (UNCONF);
535 return (QUIET);
536 }
537
538 static void
539 psycho_set_intr(sc, ipl, handler, mapper, clearer)
540 struct psycho_softc *sc;
541 int ipl;
542 void *handler;
543 u_int64_t *mapper;
544 u_int64_t *clearer;
545 {
546 struct intrhand *ih;
547
548 ih = (struct intrhand *)malloc(sizeof(struct intrhand),
549 M_DEVBUF, M_NOWAIT);
550 ih->ih_arg = sc;
551 ih->ih_map = mapper;
552 ih->ih_clr = clearer;
553 ih->ih_fun = handler;
554 ih->ih_pil = (1<<ipl);
555 ih->ih_number = INTVEC(*(ih->ih_map));
556 intr_establish(ipl, ih);
557 *(ih->ih_map) |= INTMAP_V;
558 }
559
560 /*
561 * PCI bus support
562 */
563
564 /*
565 * allocate a PCI chipset tag and set it's cookie.
566 */
567 static pci_chipset_tag_t
568 psycho_alloc_chipset(pp, node, pc)
569 struct psycho_pbm *pp;
570 int node;
571 pci_chipset_tag_t pc;
572 {
573 pci_chipset_tag_t npc;
574
575 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
576 if (npc == NULL)
577 panic("could not allocate pci_chipset_tag_t");
578 memcpy(npc, pc, sizeof *pc);
579 npc->cookie = pp;
580 npc->rootnode = node;
581 npc->curnode = node;
582
583 return (npc);
584 }
585
586 /*
587 * grovel the OBP for various psycho properties
588 */
589 static void
590 psycho_get_bus_range(node, brp)
591 int node;
592 int *brp;
593 {
594 int n;
595
596 if (PROM_getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
597 panic("could not get psycho bus-range");
598 if (n != 2)
599 panic("broken psycho bus-range");
600 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
601 }
602
603 static void
604 psycho_get_ranges(node, rp, np)
605 int node;
606 struct psycho_ranges **rp;
607 int *np;
608 {
609
610 if (PROM_getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
611 panic("could not get psycho ranges");
612 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
613 }
614
615 /*
616 * Interrupt handlers.
617 */
618
619 static int
620 psycho_ue(arg)
621 void *arg;
622 {
623 struct psycho_softc *sc = (struct psycho_softc *)arg;
624 struct psychoreg *regs = sc->sc_regs;
625 long long afsr = regs->psy_ue_afsr;
626 long long afar = regs->psy_ue_afar;
627 long size = NBPG<<(sc->sc_is->is_tsbsize);
628 struct iommu_state *is = sc->sc_is;
629 char bits[128];
630
631 /*
632 * It's uncorrectable. Dump the regs and panic.
633 */
634 printf("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s",
635 sc->sc_dev.dv_xname, afar,
636 (long long)iommu_extract(is, (vaddr_t)afar), afsr,
637 bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
638 bits, sizeof(bits)));
639
640 /* Sometimes the AFAR points to an IOTSB entry */
641 if (afar >= is->is_ptsb && afar < is->is_ptsb + size) {
642 printf("IOVA %llx IOTTE %llx\n",
643 (long long)((afar - is->is_ptsb) * NBPG + is->is_dvmabase),
644 (long long)ldxa(afar, ASI_PHYS_CACHED));
645 }
646 Debugger();
647 regs->psy_ue_afar = 0;
648 regs->psy_ue_afsr = 0;
649 return (1);
650 }
651 static int
652 psycho_ce(arg)
653 void *arg;
654 {
655 struct psycho_softc *sc = (struct psycho_softc *)arg;
656 struct psychoreg *regs = sc->sc_regs;
657
658 /*
659 * It's correctable. Dump the regs and continue.
660 */
661
662 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
663 sc->sc_dev.dv_xname,
664 (long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
665 return (1);
666 }
667 static int
668 psycho_bus_a(arg)
669 void *arg;
670 {
671 struct psycho_softc *sc = (struct psycho_softc *)arg;
672 struct psychoreg *regs = sc->sc_regs;
673
674 /*
675 * It's uncorrectable. Dump the regs and panic.
676 */
677
678 panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
679 sc->sc_dev.dv_xname,
680 (long long)regs->psy_pcictl[0].pci_afar,
681 (long long)regs->psy_pcictl[0].pci_afsr);
682 return (1);
683 }
684 static int
685 psycho_bus_b(arg)
686 void *arg;
687 {
688 struct psycho_softc *sc = (struct psycho_softc *)arg;
689 struct psychoreg *regs = sc->sc_regs;
690
691 /*
692 * It's uncorrectable. Dump the regs and panic.
693 */
694
695 panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
696 sc->sc_dev.dv_xname,
697 (long long)regs->psy_pcictl[0].pci_afar,
698 (long long)regs->psy_pcictl[0].pci_afsr);
699 return (1);
700 }
701 static int
702 psycho_powerfail(arg)
703 void *arg;
704 {
705
706 /*
707 * We lost power. Try to shut down NOW.
708 */
709 printf("Power Failure Detected: Shutting down NOW.\n");
710 cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
711 return (1);
712 }
713 static
714 int psycho_wakeup(arg)
715 void *arg;
716 {
717 struct psycho_softc *sc = (struct psycho_softc *)arg;
718
719 /*
720 * Gee, we don't really have a framework to deal with this
721 * properly.
722 */
723 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname);
724 return (1);
725 }
726
727
728
729 /*
730 * initialise the IOMMU..
731 */
732 void
733 psycho_iommu_init(sc, tsbsize)
734 struct psycho_softc *sc;
735 int tsbsize;
736 {
737 char *name;
738 struct iommu_state *is = sc->sc_is;
739 u_int32_t iobase = -1;
740 int *vdma = NULL;
741 int nitem;
742
743 /* punch in our copies */
744 is->is_bustag = sc->sc_bustag;
745 is->is_iommu = &sc->sc_regs->psy_iommu;
746
747 /*
748 * Separate the men from the boys. Get the `virtual-dma'
749 * property for sabre and use that to make sure the damn
750 * iommu works.
751 *
752 * We could query the `#virtual-dma-size-cells' and
753 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
754 */
755 if (!PROM_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
756 (void **)&vdma)) {
757 /* Damn. Gotta use these values. */
758 iobase = vdma[0];
759 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break
760 switch (vdma[1]) {
761 TSBCASE(1); TSBCASE(2); TSBCASE(3);
762 TSBCASE(4); TSBCASE(5); TSBCASE(6);
763 default:
764 printf("bogus tsb size %x, using 7\n", vdma[1]);
765 TSBCASE(7);
766 }
767 #undef TSBCASE
768 }
769
770 /* give us a nice name.. */
771 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
772 if (name == 0)
773 panic("couldn't malloc iommu name");
774 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
775
776 iommu_init(name, is, tsbsize, iobase);
777 }
778
779 /*
780 * below here is bus space and bus dma support
781 */
782 bus_space_tag_t
783 psycho_alloc_bus_tag(pp, type)
784 struct psycho_pbm *pp;
785 int type;
786 {
787 struct psycho_softc *sc = pp->pp_sc;
788 bus_space_tag_t bt;
789
790 bt = (bus_space_tag_t)
791 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
792 if (bt == NULL)
793 panic("could not allocate psycho bus tag");
794
795 bzero(bt, sizeof *bt);
796 bt->cookie = pp;
797 bt->parent = sc->sc_bustag;
798 bt->type = type;
799 bt->sparc_bus_map = _psycho_bus_map;
800 bt->sparc_bus_mmap = psycho_bus_mmap;
801 bt->sparc_intr_establish = psycho_intr_establish;
802 return (bt);
803 }
804
805 bus_dma_tag_t
806 psycho_alloc_dma_tag(pp)
807 struct psycho_pbm *pp;
808 {
809 struct psycho_softc *sc = pp->pp_sc;
810 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
811
812 dt = (bus_dma_tag_t)
813 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
814 if (dt == NULL)
815 panic("could not allocate psycho dma tag");
816
817 bzero(dt, sizeof *dt);
818 dt->_cookie = pp;
819 dt->_parent = pdt;
820 #define PCOPY(x) dt->x = pdt->x
821 PCOPY(_dmamap_create);
822 PCOPY(_dmamap_destroy);
823 dt->_dmamap_load = psycho_dmamap_load;
824 PCOPY(_dmamap_load_mbuf);
825 PCOPY(_dmamap_load_uio);
826 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
827 dt->_dmamap_unload = psycho_dmamap_unload;
828 dt->_dmamap_sync = psycho_dmamap_sync;
829 dt->_dmamem_alloc = psycho_dmamem_alloc;
830 dt->_dmamem_free = psycho_dmamem_free;
831 dt->_dmamem_map = psycho_dmamem_map;
832 dt->_dmamem_unmap = psycho_dmamem_unmap;
833 PCOPY(_dmamem_mmap);
834 #undef PCOPY
835 return (dt);
836 }
837
838 /*
839 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
840 * PCI physical addresses.
841 */
842
843 static int get_childspace __P((int));
844
845 static int
846 get_childspace(type)
847 int type;
848 {
849 int ss;
850
851 switch (type) {
852 case PCI_CONFIG_BUS_SPACE:
853 ss = 0x00;
854 break;
855 case PCI_IO_BUS_SPACE:
856 ss = 0x01;
857 break;
858 case PCI_MEMORY_BUS_SPACE:
859 ss = 0x02;
860 break;
861 #if 0
862 /* we don't do 64 bit memory space */
863 case PCI_MEMORY64_BUS_SPACE:
864 ss = 0x03;
865 break;
866 #endif
867 default:
868 panic("get_childspace: unknown bus type");
869 }
870
871 return (ss);
872 }
873
874 static int
875 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
876 bus_space_tag_t t;
877 bus_type_t btype;
878 bus_addr_t offset;
879 bus_size_t size;
880 int flags;
881 vaddr_t vaddr;
882 bus_space_handle_t *hp;
883 {
884 struct psycho_pbm *pp = t->cookie;
885 struct psycho_softc *sc = pp->pp_sc;
886 int i, ss;
887
888 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,
889 (void *)vaddr));
890
891 ss = get_childspace(t->type);
892 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
893
894 for (i = 0; i < pp->pp_nrange; i++) {
895 bus_addr_t paddr;
896
897 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
898 continue;
899
900 paddr = pp->pp_range[i].phys_lo + offset;
901 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
902 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
903 (long)ss, (long)offset,
904 (unsigned long long)paddr));
905 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
906 size, flags, vaddr, hp));
907 }
908 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
909 return (EINVAL);
910 }
911
912 static paddr_t
913 psycho_bus_mmap(t, paddr, off, prot, flags)
914 bus_space_tag_t t;
915 bus_addr_t paddr;
916 off_t off;
917 int prot;
918 int flags;
919 {
920 bus_addr_t offset = paddr;
921 struct psycho_pbm *pp = t->cookie;
922 struct psycho_softc *sc = pp->pp_sc;
923 int i, ss;
924
925 ss = get_childspace(t->type);
926
927 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
928 prot, flags, (unsigned long long)paddr));
929
930 for (i = 0; i < pp->pp_nrange; i++) {
931 bus_addr_t paddr;
932
933 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
934 continue;
935
936 paddr = pp->pp_range[i].phys_lo + offset;
937 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
938 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
939 "space %lx offset %lx paddr %qx\n",
940 (long)ss, (long)offset,
941 (unsigned long long)paddr));
942 return (bus_space_mmap(sc->sc_bustag, paddr, off,
943 prot, flags));
944 }
945
946 return (-1);
947 }
948
949
950 /*
951 * install an interrupt handler for a PCI device
952 */
953 void *
954 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
955 bus_space_tag_t t;
956 int ihandle;
957 int level;
958 int flags;
959 int (*handler) __P((void *));
960 void *arg;
961 {
962 struct psycho_pbm *pp = t->cookie;
963 struct psycho_softc *sc = pp->pp_sc;
964 struct intrhand *ih;
965 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
966 int64_t intrmap = 0;
967 int ino;
968 long vec = INTVEC(ihandle);
969
970 ih = (struct intrhand *)
971 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
972 if (ih == NULL)
973 return (NULL);
974
975 /*
976 * Hunt through all the interrupt mapping regs to look for our
977 * interrupt vector.
978 *
979 * XXX We only compare INOs rather than IGNs since the firmware may
980 * not provide the IGN and the IGN is constant for all device on that
981 * PCI controller. This could cause problems for the FFB/external
982 * interrupt which has a full vector that can be set arbitrarily.
983 */
984
985
986 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
987 ino = INTINO(vec);
988 DPRINTF(PDB_INTR, (" ino %x", ino));
989
990 /* If the device didn't ask for an IPL, use the one encoded. */
991 if (level == IPL_NONE) level = INTLEV(vec);
992 /* If it still has no level, print a warning and assign IPL 2 */
993 if (level == IPL_NONE) {
994 printf("ERROR: no IPL, setting IPL 2.\n");
995 level = 2;
996 }
997
998 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
999
1000 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1001 (long)ino, intrlev[ino]));
1002
1003 /* Hunt thru obio first */
1004 for (intrmapptr = &sc->sc_regs->scsi_int_map,
1005 intrclrptr = &sc->sc_regs->scsi_clr_int;
1006 intrmapptr <= &sc->sc_regs->ffb1_int_map;
1007 intrmapptr++, intrclrptr++) {
1008 if (INTINO(*intrmapptr) == ino)
1009 goto found;
1010 }
1011
1012 /* Now do PCI interrupts */
1013 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1014 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1015 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1016 intrmapptr++, intrclrptr += 4) {
1017 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1018 intrclrptr += vec & 0x3;
1019 goto found;
1020 }
1021 }
1022 printf("Cannot find interrupt vector %lx\n", vec);
1023 return (NULL);
1024
1025 found:
1026 /* Register the map and clear intr registers */
1027 ih->ih_map = intrmapptr;
1028 ih->ih_clr = intrclrptr;
1029 }
1030 #ifdef NOT_DEBUG
1031 if (psycho_debug & PDB_INTR) {
1032 long i;
1033
1034 for (i = 0; i < 500000000; i++)
1035 continue;
1036 }
1037 #endif
1038
1039 ih->ih_fun = handler;
1040 ih->ih_arg = arg;
1041 ih->ih_pil = level;
1042 ih->ih_number = ino | sc->sc_ign;
1043
1044 DPRINTF(PDB_INTR, (
1045 "; installing handler %p arg %p with ino %u pil %u\n",
1046 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1047
1048 intr_establish(ih->ih_pil, ih);
1049
1050 /*
1051 * Enable the interrupt now we have the handler installed.
1052 * Read the current value as we can't change it besides the
1053 * valid bit so so make sure only this bit is changed.
1054 *
1055 * XXXX --- we really should use bus_space for this.
1056 */
1057 if (intrmapptr) {
1058 intrmap = *intrmapptr;
1059 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1060 (unsigned long long)intrmap));
1061
1062 /* Enable the interrupt */
1063 intrmap |= INTMAP_V;
1064 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1065 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1066 (unsigned long long)intrmap));
1067 *intrmapptr = intrmap;
1068 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1069 (unsigned long long)(intrmap = *intrmapptr)));
1070 }
1071 return (ih);
1072 }
1073
1074 /*
1075 * hooks into the iommu dvma calls.
1076 */
1077 int
1078 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1079 bus_dma_tag_t t;
1080 bus_dmamap_t map;
1081 void *buf;
1082 bus_size_t buflen;
1083 struct proc *p;
1084 int flags;
1085 {
1086 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1087 struct psycho_softc *sc = pp->pp_sc;
1088
1089 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1090 }
1091
1092 void
1093 psycho_dmamap_unload(t, map)
1094 bus_dma_tag_t t;
1095 bus_dmamap_t map;
1096 {
1097 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1098 struct psycho_softc *sc = pp->pp_sc;
1099
1100 iommu_dvmamap_unload(t, sc->sc_is, map);
1101 }
1102
1103 int
1104 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1105 bus_dma_tag_t t;
1106 bus_dmamap_t map;
1107 bus_dma_segment_t *segs;
1108 int nsegs;
1109 bus_size_t size;
1110 int flags;
1111 {
1112 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1113 struct psycho_softc *sc = pp->pp_sc;
1114
1115 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1116 }
1117
1118 void
1119 psycho_dmamap_sync(t, map, offset, len, ops)
1120 bus_dma_tag_t t;
1121 bus_dmamap_t map;
1122 bus_addr_t offset;
1123 bus_size_t len;
1124 int ops;
1125 {
1126 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1127 struct psycho_softc *sc = pp->pp_sc;
1128
1129 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1130 /* Flush the CPU then the IOMMU */
1131 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1132 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1133 }
1134 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1135 /* Flush the IOMMU then the CPU */
1136 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1137 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1138 }
1139
1140 }
1141
1142 int
1143 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1144 bus_dma_tag_t t;
1145 bus_size_t size;
1146 bus_size_t alignment;
1147 bus_size_t boundary;
1148 bus_dma_segment_t *segs;
1149 int nsegs;
1150 int *rsegs;
1151 int flags;
1152 {
1153 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1154 struct psycho_softc *sc = pp->pp_sc;
1155
1156 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1157 segs, nsegs, rsegs, flags));
1158 }
1159
1160 void
1161 psycho_dmamem_free(t, segs, nsegs)
1162 bus_dma_tag_t t;
1163 bus_dma_segment_t *segs;
1164 int nsegs;
1165 {
1166 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1167 struct psycho_softc *sc = pp->pp_sc;
1168
1169 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1170 }
1171
1172 int
1173 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1174 bus_dma_tag_t t;
1175 bus_dma_segment_t *segs;
1176 int nsegs;
1177 size_t size;
1178 caddr_t *kvap;
1179 int flags;
1180 {
1181 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1182 struct psycho_softc *sc = pp->pp_sc;
1183
1184 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1185 }
1186
1187 void
1188 psycho_dmamem_unmap(t, kva, size)
1189 bus_dma_tag_t t;
1190 caddr_t kva;
1191 size_t size;
1192 {
1193 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1194 struct psycho_softc *sc = pp->pp_sc;
1195
1196 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1197 }
1198