psycho.c revision 1.43 1 /* $NetBSD: psycho.c,v 1.43 2002/03/08 06:03:50 chs 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 #ifdef DDB
647 Debugger();
648 #endif
649 regs->psy_ue_afar = 0;
650 regs->psy_ue_afsr = 0;
651 return (1);
652 }
653 static int
654 psycho_ce(arg)
655 void *arg;
656 {
657 struct psycho_softc *sc = (struct psycho_softc *)arg;
658 struct psychoreg *regs = sc->sc_regs;
659
660 /*
661 * It's correctable. Dump the regs and continue.
662 */
663
664 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
665 sc->sc_dev.dv_xname,
666 (long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
667 return (1);
668 }
669 static int
670 psycho_bus_a(arg)
671 void *arg;
672 {
673 struct psycho_softc *sc = (struct psycho_softc *)arg;
674 struct psychoreg *regs = sc->sc_regs;
675
676 /*
677 * It's uncorrectable. Dump the regs and panic.
678 */
679
680 panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
681 sc->sc_dev.dv_xname,
682 (long long)regs->psy_pcictl[0].pci_afar,
683 (long long)regs->psy_pcictl[0].pci_afsr);
684 return (1);
685 }
686 static int
687 psycho_bus_b(arg)
688 void *arg;
689 {
690 struct psycho_softc *sc = (struct psycho_softc *)arg;
691 struct psychoreg *regs = sc->sc_regs;
692
693 /*
694 * It's uncorrectable. Dump the regs and panic.
695 */
696
697 panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
698 sc->sc_dev.dv_xname,
699 (long long)regs->psy_pcictl[0].pci_afar,
700 (long long)regs->psy_pcictl[0].pci_afsr);
701 return (1);
702 }
703 static int
704 psycho_powerfail(arg)
705 void *arg;
706 {
707
708 /*
709 * We lost power. Try to shut down NOW.
710 */
711 printf("Power Failure Detected: Shutting down NOW.\n");
712 cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
713 return (1);
714 }
715 static
716 int psycho_wakeup(arg)
717 void *arg;
718 {
719 struct psycho_softc *sc = (struct psycho_softc *)arg;
720
721 /*
722 * Gee, we don't really have a framework to deal with this
723 * properly.
724 */
725 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname);
726 return (1);
727 }
728
729
730
731 /*
732 * initialise the IOMMU..
733 */
734 void
735 psycho_iommu_init(sc, tsbsize)
736 struct psycho_softc *sc;
737 int tsbsize;
738 {
739 char *name;
740 struct iommu_state *is = sc->sc_is;
741 u_int32_t iobase = -1;
742 int *vdma = NULL;
743 int nitem;
744
745 /* punch in our copies */
746 is->is_bustag = sc->sc_bustag;
747 is->is_iommu = &sc->sc_regs->psy_iommu;
748
749 /*
750 * Separate the men from the boys. Get the `virtual-dma'
751 * property for sabre and use that to make sure the damn
752 * iommu works.
753 *
754 * We could query the `#virtual-dma-size-cells' and
755 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
756 */
757 if (!PROM_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
758 (void **)&vdma)) {
759 /* Damn. Gotta use these values. */
760 iobase = vdma[0];
761 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break
762 switch (vdma[1]) {
763 TSBCASE(1); TSBCASE(2); TSBCASE(3);
764 TSBCASE(4); TSBCASE(5); TSBCASE(6);
765 default:
766 printf("bogus tsb size %x, using 7\n", vdma[1]);
767 TSBCASE(7);
768 }
769 #undef TSBCASE
770 }
771
772 /* give us a nice name.. */
773 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
774 if (name == 0)
775 panic("couldn't malloc iommu name");
776 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
777
778 iommu_init(name, is, tsbsize, iobase);
779 }
780
781 /*
782 * below here is bus space and bus dma support
783 */
784 bus_space_tag_t
785 psycho_alloc_bus_tag(pp, type)
786 struct psycho_pbm *pp;
787 int type;
788 {
789 struct psycho_softc *sc = pp->pp_sc;
790 bus_space_tag_t bt;
791
792 bt = (bus_space_tag_t)
793 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
794 if (bt == NULL)
795 panic("could not allocate psycho bus tag");
796
797 bzero(bt, sizeof *bt);
798 bt->cookie = pp;
799 bt->parent = sc->sc_bustag;
800 bt->type = type;
801 bt->sparc_bus_map = _psycho_bus_map;
802 bt->sparc_bus_mmap = psycho_bus_mmap;
803 bt->sparc_intr_establish = psycho_intr_establish;
804 return (bt);
805 }
806
807 bus_dma_tag_t
808 psycho_alloc_dma_tag(pp)
809 struct psycho_pbm *pp;
810 {
811 struct psycho_softc *sc = pp->pp_sc;
812 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
813
814 dt = (bus_dma_tag_t)
815 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
816 if (dt == NULL)
817 panic("could not allocate psycho dma tag");
818
819 bzero(dt, sizeof *dt);
820 dt->_cookie = pp;
821 dt->_parent = pdt;
822 #define PCOPY(x) dt->x = pdt->x
823 PCOPY(_dmamap_create);
824 PCOPY(_dmamap_destroy);
825 dt->_dmamap_load = psycho_dmamap_load;
826 PCOPY(_dmamap_load_mbuf);
827 PCOPY(_dmamap_load_uio);
828 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
829 dt->_dmamap_unload = psycho_dmamap_unload;
830 dt->_dmamap_sync = psycho_dmamap_sync;
831 dt->_dmamem_alloc = psycho_dmamem_alloc;
832 dt->_dmamem_free = psycho_dmamem_free;
833 dt->_dmamem_map = psycho_dmamem_map;
834 dt->_dmamem_unmap = psycho_dmamem_unmap;
835 PCOPY(_dmamem_mmap);
836 #undef PCOPY
837 return (dt);
838 }
839
840 /*
841 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
842 * PCI physical addresses.
843 */
844
845 static int get_childspace __P((int));
846
847 static int
848 get_childspace(type)
849 int type;
850 {
851 int ss;
852
853 switch (type) {
854 case PCI_CONFIG_BUS_SPACE:
855 ss = 0x00;
856 break;
857 case PCI_IO_BUS_SPACE:
858 ss = 0x01;
859 break;
860 case PCI_MEMORY_BUS_SPACE:
861 ss = 0x02;
862 break;
863 #if 0
864 /* we don't do 64 bit memory space */
865 case PCI_MEMORY64_BUS_SPACE:
866 ss = 0x03;
867 break;
868 #endif
869 default:
870 panic("get_childspace: unknown bus type");
871 }
872
873 return (ss);
874 }
875
876 static int
877 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
878 bus_space_tag_t t;
879 bus_type_t btype;
880 bus_addr_t offset;
881 bus_size_t size;
882 int flags;
883 vaddr_t vaddr;
884 bus_space_handle_t *hp;
885 {
886 struct psycho_pbm *pp = t->cookie;
887 struct psycho_softc *sc = pp->pp_sc;
888 int i, ss;
889
890 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,
891 (void *)vaddr));
892
893 ss = get_childspace(t->type);
894 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
895
896 for (i = 0; i < pp->pp_nrange; i++) {
897 bus_addr_t paddr;
898
899 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
900 continue;
901
902 paddr = pp->pp_range[i].phys_lo + offset;
903 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
904 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
905 (long)ss, (long)offset,
906 (unsigned long long)paddr));
907 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
908 size, flags, vaddr, hp));
909 }
910 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
911 return (EINVAL);
912 }
913
914 static paddr_t
915 psycho_bus_mmap(t, paddr, off, prot, flags)
916 bus_space_tag_t t;
917 bus_addr_t paddr;
918 off_t off;
919 int prot;
920 int flags;
921 {
922 bus_addr_t offset = paddr;
923 struct psycho_pbm *pp = t->cookie;
924 struct psycho_softc *sc = pp->pp_sc;
925 int i, ss;
926
927 ss = get_childspace(t->type);
928
929 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
930 prot, flags, (unsigned long long)paddr));
931
932 for (i = 0; i < pp->pp_nrange; i++) {
933 bus_addr_t paddr;
934
935 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
936 continue;
937
938 paddr = pp->pp_range[i].phys_lo + offset;
939 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
940 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
941 "space %lx offset %lx paddr %qx\n",
942 (long)ss, (long)offset,
943 (unsigned long long)paddr));
944 return (bus_space_mmap(sc->sc_bustag, paddr, off,
945 prot, flags));
946 }
947
948 return (-1);
949 }
950
951
952 /*
953 * install an interrupt handler for a PCI device
954 */
955 void *
956 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
957 bus_space_tag_t t;
958 int ihandle;
959 int level;
960 int flags;
961 int (*handler) __P((void *));
962 void *arg;
963 {
964 struct psycho_pbm *pp = t->cookie;
965 struct psycho_softc *sc = pp->pp_sc;
966 struct intrhand *ih;
967 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
968 int64_t intrmap = 0;
969 int ino;
970 long vec = INTVEC(ihandle);
971
972 ih = (struct intrhand *)
973 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
974 if (ih == NULL)
975 return (NULL);
976
977 /*
978 * Hunt through all the interrupt mapping regs to look for our
979 * interrupt vector.
980 *
981 * XXX We only compare INOs rather than IGNs since the firmware may
982 * not provide the IGN and the IGN is constant for all device on that
983 * PCI controller. This could cause problems for the FFB/external
984 * interrupt which has a full vector that can be set arbitrarily.
985 */
986
987
988 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
989 ino = INTINO(vec);
990 DPRINTF(PDB_INTR, (" ino %x", ino));
991
992 /* If the device didn't ask for an IPL, use the one encoded. */
993 if (level == IPL_NONE) level = INTLEV(vec);
994 /* If it still has no level, print a warning and assign IPL 2 */
995 if (level == IPL_NONE) {
996 printf("ERROR: no IPL, setting IPL 2.\n");
997 level = 2;
998 }
999
1000 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
1001
1002 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1003 (long)ino, intrlev[ino]));
1004
1005 /* Hunt thru obio first */
1006 for (intrmapptr = &sc->sc_regs->scsi_int_map,
1007 intrclrptr = &sc->sc_regs->scsi_clr_int;
1008 intrmapptr <= &sc->sc_regs->ffb1_int_map;
1009 intrmapptr++, intrclrptr++) {
1010 if (INTINO(*intrmapptr) == ino)
1011 goto found;
1012 }
1013
1014 /* Now do PCI interrupts */
1015 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1016 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1017 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1018 intrmapptr++, intrclrptr += 4) {
1019 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1020 intrclrptr += vec & 0x3;
1021 goto found;
1022 }
1023 }
1024 printf("Cannot find interrupt vector %lx\n", vec);
1025 return (NULL);
1026
1027 found:
1028 /* Register the map and clear intr registers */
1029 ih->ih_map = intrmapptr;
1030 ih->ih_clr = intrclrptr;
1031 }
1032 #ifdef NOT_DEBUG
1033 if (psycho_debug & PDB_INTR) {
1034 long i;
1035
1036 for (i = 0; i < 500000000; i++)
1037 continue;
1038 }
1039 #endif
1040
1041 ih->ih_fun = handler;
1042 ih->ih_arg = arg;
1043 ih->ih_pil = level;
1044 ih->ih_number = ino | sc->sc_ign;
1045
1046 DPRINTF(PDB_INTR, (
1047 "; installing handler %p arg %p with ino %u pil %u\n",
1048 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1049
1050 intr_establish(ih->ih_pil, ih);
1051
1052 /*
1053 * Enable the interrupt now we have the handler installed.
1054 * Read the current value as we can't change it besides the
1055 * valid bit so so make sure only this bit is changed.
1056 *
1057 * XXXX --- we really should use bus_space for this.
1058 */
1059 if (intrmapptr) {
1060 intrmap = *intrmapptr;
1061 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1062 (unsigned long long)intrmap));
1063
1064 /* Enable the interrupt */
1065 intrmap |= INTMAP_V;
1066 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1067 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1068 (unsigned long long)intrmap));
1069 *intrmapptr = intrmap;
1070 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1071 (unsigned long long)(intrmap = *intrmapptr)));
1072 }
1073 return (ih);
1074 }
1075
1076 /*
1077 * hooks into the iommu dvma calls.
1078 */
1079 int
1080 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1081 bus_dma_tag_t t;
1082 bus_dmamap_t map;
1083 void *buf;
1084 bus_size_t buflen;
1085 struct proc *p;
1086 int flags;
1087 {
1088 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1089 struct psycho_softc *sc = pp->pp_sc;
1090
1091 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1092 }
1093
1094 void
1095 psycho_dmamap_unload(t, map)
1096 bus_dma_tag_t t;
1097 bus_dmamap_t map;
1098 {
1099 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1100 struct psycho_softc *sc = pp->pp_sc;
1101
1102 iommu_dvmamap_unload(t, sc->sc_is, map);
1103 }
1104
1105 int
1106 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1107 bus_dma_tag_t t;
1108 bus_dmamap_t map;
1109 bus_dma_segment_t *segs;
1110 int nsegs;
1111 bus_size_t size;
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_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1118 }
1119
1120 void
1121 psycho_dmamap_sync(t, map, offset, len, ops)
1122 bus_dma_tag_t t;
1123 bus_dmamap_t map;
1124 bus_addr_t offset;
1125 bus_size_t len;
1126 int ops;
1127 {
1128 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1129 struct psycho_softc *sc = pp->pp_sc;
1130
1131 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1132 /* Flush the CPU then the IOMMU */
1133 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1134 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1135 }
1136 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1137 /* Flush the IOMMU then the CPU */
1138 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1139 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1140 }
1141
1142 }
1143
1144 int
1145 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1146 bus_dma_tag_t t;
1147 bus_size_t size;
1148 bus_size_t alignment;
1149 bus_size_t boundary;
1150 bus_dma_segment_t *segs;
1151 int nsegs;
1152 int *rsegs;
1153 int flags;
1154 {
1155 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1156 struct psycho_softc *sc = pp->pp_sc;
1157
1158 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1159 segs, nsegs, rsegs, flags));
1160 }
1161
1162 void
1163 psycho_dmamem_free(t, segs, nsegs)
1164 bus_dma_tag_t t;
1165 bus_dma_segment_t *segs;
1166 int nsegs;
1167 {
1168 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1169 struct psycho_softc *sc = pp->pp_sc;
1170
1171 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1172 }
1173
1174 int
1175 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1176 bus_dma_tag_t t;
1177 bus_dma_segment_t *segs;
1178 int nsegs;
1179 size_t size;
1180 caddr_t *kvap;
1181 int flags;
1182 {
1183 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1184 struct psycho_softc *sc = pp->pp_sc;
1185
1186 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1187 }
1188
1189 void
1190 psycho_dmamem_unmap(t, kva, size)
1191 bus_dma_tag_t t;
1192 caddr_t kva;
1193 size_t size;
1194 {
1195 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1196 struct psycho_softc *sc = pp->pp_sc;
1197
1198 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1199 }
1200