psycho.c revision 1.44 1 /* $NetBSD: psycho.c,v 1.44 2002/03/15 07:06:24 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 #ifdef DEBUG
39 #define PDB_PROM 0x01
40 #define PDB_BUSMAP 0x02
41 #define PDB_INTR 0x04
42 int psycho_debug = 0x0;
43 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0)
44 #else
45 #define DPRINTF(l, s)
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/device.h>
50 #include <sys/errno.h>
51 #include <sys/extent.h>
52 #include <sys/malloc.h>
53 #include <sys/systm.h>
54 #include <sys/time.h>
55 #include <sys/reboot.h>
56
57 #define _SPARC_BUS_DMA_PRIVATE
58 #include <machine/bus.h>
59 #include <machine/autoconf.h>
60 #include <machine/psl.h>
61
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcireg.h>
64
65 #include <sparc64/dev/iommureg.h>
66 #include <sparc64/dev/iommuvar.h>
67 #include <sparc64/dev/psychoreg.h>
68 #include <sparc64/dev/psychovar.h>
69 #include <sparc64/sparc64/cache.h>
70
71 #include "ioconf.h"
72
73 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
74 pci_chipset_tag_t));
75 static void psycho_get_bus_range __P((int, int *));
76 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
77 static void psycho_set_intr __P((struct psycho_softc *, int, void *,
78 u_int64_t *, u_int64_t *));
79
80 /* Interrupt handlers */
81 static int psycho_ue __P((void *));
82 static int psycho_ce __P((void *));
83 static int psycho_bus_a __P((void *));
84 static int psycho_bus_b __P((void *));
85 static int psycho_powerfail __P((void *));
86 static int psycho_wakeup __P((void *));
87
88
89 /* IOMMU support */
90 static void psycho_iommu_init __P((struct psycho_softc *, int));
91
92 /*
93 * bus space and bus dma support for UltraSPARC `psycho'. note that most
94 * of the bus dma support is provided by the iommu dvma controller.
95 */
96 static paddr_t psycho_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
97 int, int));
98 static int _psycho_bus_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
99 vaddr_t, bus_space_handle_t *));
100 static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
101 int (*) __P((void *)), void *));
102
103 static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
104 bus_size_t, struct proc *, int));
105 static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
106 static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
107 bus_dma_segment_t *, int, bus_size_t, int));
108 static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
109 bus_size_t, int));
110 int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
111 bus_dma_segment_t *, int, int *, int));
112 void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
113 int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
114 caddr_t *, int));
115 void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
116
117 /* base pci_chipset */
118 extern struct sparc_pci_chipset _sparc_pci_chipset;
119
120 /*
121 * autoconfiguration
122 */
123 static int psycho_match __P((struct device *, struct cfdata *, void *));
124 static void psycho_attach __P((struct device *, struct device *, void *));
125 static int psycho_print __P((void *aux, const char *p));
126
127 struct cfattach psycho_ca = {
128 sizeof(struct psycho_softc), psycho_match, psycho_attach
129 };
130
131 /*
132 * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge. It manages a
133 * single PCI bus and does not have a streaming buffer. It often has an APB
134 * (advanced PCI bridge) connected to it, which was designed specifically for
135 * the IIi. The APB let's the IIi handle two independednt PCI buses, and
136 * appears as two "simba"'s underneath the sabre.
137 *
138 * "psycho" and "psycho+" is a dual UPA to PCI bridge. It sits on the UPA bus
139 * and manages two PCI buses. "psycho" has two 64-bit 33MHz buses, while
140 * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus. You
141 * will usually find a "psycho+" since I don't think the original "psycho"
142 * ever shipped, and if it did it would be in the U30.
143 *
144 * Each "psycho" PCI bus appears as a separate OFW node, but since they are
145 * both part of the same IC, they only have a single register space. As such,
146 * they need to be configured together, even though the autoconfiguration will
147 * attach them separately.
148 *
149 * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
150 * as pci1 and pci2, although they have been implemented with other PCI bus
151 * numbers on some machines.
152 *
153 * On UltraII machines, there can be any number of "psycho+" ICs, each
154 * providing two PCI buses.
155 *
156 *
157 * XXXX The psycho/sabre node has an `interrupts' attribute. They contain
158 * the values of the following interrupts in this order:
159 *
160 * PCI Bus Error (30)
161 * DMA UE (2e)
162 * DMA CE (2f)
163 * Power Fail (25)
164 *
165 * We really should attach handlers for each.
166 *
167 */
168
169 #define ROM_PCI_NAME "pci"
170
171 struct psycho_names {
172 char *p_name;
173 int p_type;
174 } psycho_names[] = {
175 { "SUNW,psycho", PSYCHO_MODE_PSYCHO },
176 { "pci108e,8000", PSYCHO_MODE_PSYCHO },
177 { "SUNW,sabre", PSYCHO_MODE_SABRE },
178 { "pci108e,a000", PSYCHO_MODE_SABRE },
179 { "pci108e,a001", PSYCHO_MODE_SABRE },
180 { NULL, 0 }
181 };
182
183 static int
184 psycho_match(parent, match, aux)
185 struct device *parent;
186 struct cfdata *match;
187 void *aux;
188 {
189 struct mainbus_attach_args *ma = aux;
190 char *model = PROM_getpropstring(ma->ma_node, "model");
191 int i;
192
193 /* match on a name of "pci" and a sabre or a psycho */
194 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
195 for (i=0; psycho_names[i].p_name; i++)
196 if (strcmp(model, psycho_names[i].p_name) == 0)
197 return (1);
198
199 model = PROM_getpropstring(ma->ma_node, "compatible");
200 for (i=0; psycho_names[i].p_name; i++)
201 if (strcmp(model, psycho_names[i].p_name) == 0)
202 return (1);
203 }
204 return (0);
205 }
206
207 /*
208 * SUNW,psycho initialisation ..
209 * - find the per-psycho registers
210 * - figure out the IGN.
211 * - find our partner psycho
212 * - configure ourselves
213 * - bus range, bus,
214 * - get interrupt-map and interrupt-map-mask
215 * - setup the chipsets.
216 * - if we're the first of the pair, initialise the IOMMU, otherwise
217 * just copy it's tags and addresses.
218 */
219 static void
220 psycho_attach(parent, self, aux)
221 struct device *parent, *self;
222 void *aux;
223 {
224 struct psycho_softc *sc = (struct psycho_softc *)self;
225 struct psycho_softc *osc = NULL;
226 struct psycho_pbm *pp;
227 struct pcibus_attach_args pba;
228 struct mainbus_attach_args *ma = aux;
229 bus_space_handle_t bh;
230 u_int64_t csr;
231 int psycho_br[2], n, i;
232 struct pci_ctl *pci_ctl;
233 char *model = PROM_getpropstring(ma->ma_node, "model");
234
235 printf("\n");
236
237 sc->sc_node = ma->ma_node;
238 sc->sc_bustag = ma->ma_bustag;
239 sc->sc_dmatag = ma->ma_dmatag;
240
241 /*
242 * call the model-specific initialisation routine.
243 */
244 for (i=0; psycho_names[i].p_name; i++)
245 if (strcmp(model, psycho_names[i].p_name) == 0) {
246 sc->sc_mode = psycho_names[i].p_type;
247 goto found;
248 }
249
250 model = PROM_getpropstring(ma->ma_node, "compatible");
251 for (i=0; psycho_names[i].p_name; i++)
252 if (strcmp(model, psycho_names[i].p_name) == 0) {
253 sc->sc_mode = psycho_names[i].p_type;
254 goto found;
255 }
256
257 panic("unknown psycho model %s", model);
258 found:
259
260 /*
261 * The psycho gets three register banks:
262 * (0) per-PBM configuration and status registers
263 * (1) per-PBM PCI configuration space, containing only the
264 * PBM 256-byte PCI header
265 * (2) the shared psycho configuration registers (struct psychoreg)
266 *
267 * XXX use the prom address for the psycho registers? we do so far.
268 */
269
270 /* Register layouts are different. stuupid. */
271 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
272 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
273
274 if (ma->ma_naddress > 2) {
275 sc->sc_regs = (struct psychoreg *)
276 (u_long)ma->ma_address[2];
277 pci_ctl = (struct pci_ctl *)
278 (u_long)ma->ma_address[0];
279 } else if (ma->ma_nreg > 2) {
280 bus_space_handle_t handle;
281
282 /* We need to map this in ourselves. */
283 if (bus_space_map(sc->sc_bustag,
284 ma->ma_reg[2].ur_paddr,
285 ma->ma_reg[2].ur_len, 0, &handle))
286 panic("psycho_attach: cannot map regs");
287 sc->sc_regs = (struct psychoreg *)(u_long)handle;
288
289 if (bus_space_map(sc->sc_bustag,
290 ma->ma_reg[0].ur_paddr,
291 ma->ma_reg[0].ur_len, 0, &handle))
292 panic("psycho_attach: cannot map ctl");
293 /* XXX -- this is lost but never unmapped */
294 pci_ctl = (struct pci_ctl *)(u_long)handle;
295
296 } else
297 panic("psycho_attach: %d not enough registers",
298 ma->ma_nreg);
299 } else {
300 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
301
302 if (ma->ma_naddress) {
303 sc->sc_regs = (struct psychoreg *)
304 (u_long)ma->ma_address[0];
305 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
306 } else if (ma->ma_nreg) {
307 bus_space_handle_t handle;
308
309 /* We need to map this in ourselves. */
310 if (bus_space_map(sc->sc_bustag,
311 ma->ma_reg[0].ur_paddr,
312 ma->ma_reg[0].ur_len, 0, &handle))
313 panic("psycho_attach: cannot map regs");
314 sc->sc_regs = (struct psychoreg *)(u_long)handle;
315 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
316 } else
317 panic("psycho_attach: %d not enough registers",
318 ma->ma_nreg);
319 }
320
321 csr = sc->sc_regs->psy_csr;
322 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
323 if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
324 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
325
326 printf("%s: impl %d, version %d: ign %x ",
327 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
328 sc->sc_ign);
329 /*
330 * Match other psycho's that are already configured against
331 * the base physical address. This will be the same for a
332 * pair of devices that share register space.
333 */
334 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
335
336 struct psycho_softc *asc =
337 (struct psycho_softc *)psycho_cd.cd_devs[n];
338
339 if (asc == NULL || asc == sc)
340 /* This entry is not there or it is me */
341 continue;
342
343 if (asc->sc_basepaddr != sc->sc_basepaddr)
344 /* This is an unrelated psycho */
345 continue;
346
347 /* Found partner */
348 osc = asc;
349 break;
350 }
351
352
353 /* Oh, dear. OK, lets get started */
354
355 /*
356 * Setup the PCI control register
357 */
358 csr = bus_space_read_8(sc->sc_bustag,
359 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
360 csr |= PCICTL_MRLM |
361 PCICTL_ARB_PARK |
362 PCICTL_ERRINTEN |
363 PCICTL_4ENABLE;
364 csr &= ~(PCICTL_SERR |
365 PCICTL_CPU_PRIO |
366 PCICTL_ARB_PRIO |
367 PCICTL_RTRYWAIT);
368 bus_space_write_8(sc->sc_bustag,
369 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
370
371
372 /*
373 * Allocate our psycho_pbm
374 */
375 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
376 if (pp == NULL)
377 panic("could not allocate psycho pbm");
378
379 memset(pp, 0, sizeof *pp);
380
381 pp->pp_sc = sc;
382
383 /* grab the psycho ranges */
384 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
385
386 /* get the bus-range for the psycho */
387 psycho_get_bus_range(sc->sc_node, psycho_br);
388
389 pba.pba_bus = psycho_br[0];
390
391 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
392 printf("; PCI bus %d", psycho_br[0]);
393
394 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
395
396 /* allocate our tags */
397 pp->pp_memt = psycho_alloc_mem_tag(pp);
398 pp->pp_iot = psycho_alloc_io_tag(pp);
399 pp->pp_dmat = psycho_alloc_dma_tag(pp);
400 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
401 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
402
403 /* allocate a chipset for this */
404 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
405
406 /* setup the rest of the psycho pbm */
407 pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
408
409 printf("\n");
410
411 /*
412 * And finally, if we're a sabre or the first of a pair of psycho's to
413 * arrive here, start up the IOMMU and get a config space tag.
414 */
415 if (osc == NULL) {
416 uint64_t timeo;
417
418 /*
419 * Establish handlers for interesting interrupts....
420 *
421 * XXX We need to remember these and remove this to support
422 * hotplug on the UPA/FHC bus.
423 *
424 * XXX Not all controllers have these, but installing them
425 * is better than trying to sort through this mess.
426 */
427 psycho_set_intr(sc, 15, psycho_ue,
428 &sc->sc_regs->ue_int_map,
429 &sc->sc_regs->ue_clr_int);
430 psycho_set_intr(sc, 1, psycho_ce,
431 &sc->sc_regs->ce_int_map,
432 &sc->sc_regs->ce_clr_int);
433 psycho_set_intr(sc, 15, psycho_bus_a,
434 &sc->sc_regs->pciaerr_int_map,
435 &sc->sc_regs->pciaerr_clr_int);
436 psycho_set_intr(sc, 15, psycho_bus_b,
437 &sc->sc_regs->pciberr_int_map,
438 &sc->sc_regs->pciberr_clr_int);
439 psycho_set_intr(sc, 15, psycho_powerfail,
440 &sc->sc_regs->power_int_map,
441 &sc->sc_regs->power_clr_int);
442 psycho_set_intr(sc, 1, psycho_wakeup,
443 &sc->sc_regs->pwrmgt_int_map,
444 &sc->sc_regs->pwrmgt_clr_int);
445
446
447 /*
448 * Apparently a number of machines with psycho and psycho+
449 * controllers have interrupt latency issues. We'll try
450 * setting the interrupt retry timeout to 0xff which gives us
451 * a retry of 3-6 usec (which is what sysio is set to) for the
452 * moment, which seems to help alleviate this problem.
453 */
454 timeo = bus_space_read_8(sc->sc_bustag,
455 (bus_space_handle_t)
456 (u_long)&sc->sc_regs->intr_retry_timer, 0);
457 if (timeo > 0xfff) {
458 #ifdef DEBUG
459 printf("decreasing interrupt retry timeout "
460 "from %lx to 0xff\n", (long)timeo);
461 #endif
462 bus_space_write_8(sc->sc_bustag,
463 (bus_space_handle_t)
464 (u_long)&sc->sc_regs->intr_retry_timer, 0,
465 0xff);
466 }
467
468 /*
469 * Setup IOMMU and PCI configuration if we're the first
470 * of a pair of psycho's to arrive here.
471 *
472 * We should calculate a TSB size based on amount of RAM
473 * and number of bus controllers and number an type of
474 * child devices.
475 *
476 * For the moment, 32KB should be more than enough.
477 */
478 sc->sc_is = malloc(sizeof(struct iommu_state),
479 M_DEVBUF, M_NOWAIT);
480 if (sc->sc_is == NULL)
481 panic("psycho_attach: malloc iommu_state");
482
483
484 sc->sc_is->is_sb[0] = 0;
485 sc->sc_is->is_sb[1] = 0;
486 if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
487 sc->sc_is->is_sb[0] = &pci_ctl->pci_strbuf;
488
489 psycho_iommu_init(sc, 2);
490
491 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
492
493 /*
494 * XXX This is a really ugly hack because PCI config space
495 * is explicitly handled with unmapped accesses.
496 */
497 i = sc->sc_bustag->type;
498 sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE;
499 if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000,
500 0x0100000, 0, &bh))
501 panic("could not map psycho PCI configuration space");
502 sc->sc_bustag->type = i;
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, offset, size, flags, unused, hp)
878 bus_space_tag_t t;
879 bus_addr_t offset;
880 bus_size_t size;
881 int flags;
882 vaddr_t unused;
883 bus_space_handle_t *hp;
884 {
885 struct psycho_pbm *pp = t->cookie;
886 struct psycho_softc *sc = pp->pp_sc;
887 int i, ss;
888
889 DPRINTF(PDB_BUSMAP,
890 ("_psycho_bus_map: type %d off %qx sz %qx flags %d",
891 t->type, (unsigned long long)offset,
892 (unsigned long long)size, flags));
893
894 ss = get_childspace(t->type);
895 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
896
897 for (i = 0; i < pp->pp_nrange; i++) {
898 bus_addr_t paddr;
899
900 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
901 continue;
902
903 paddr = pp->pp_range[i].phys_lo + offset;
904 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
905 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
906 (long)ss, (long)offset,
907 (unsigned long long)paddr));
908 return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
909 flags, 0, hp));
910 }
911 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
912 return (EINVAL);
913 }
914
915 static paddr_t
916 psycho_bus_mmap(t, paddr, off, prot, flags)
917 bus_space_tag_t t;
918 bus_addr_t paddr;
919 off_t off;
920 int prot;
921 int flags;
922 {
923 bus_addr_t offset = paddr;
924 struct psycho_pbm *pp = t->cookie;
925 struct psycho_softc *sc = pp->pp_sc;
926 int i, ss;
927
928 ss = get_childspace(t->type);
929
930 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
931 prot, flags, (unsigned long long)paddr));
932
933 for (i = 0; i < pp->pp_nrange; i++) {
934 bus_addr_t paddr;
935
936 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
937 continue;
938
939 paddr = pp->pp_range[i].phys_lo + offset;
940 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
941 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
942 "space %lx offset %lx paddr %qx\n",
943 (long)ss, (long)offset,
944 (unsigned long long)paddr));
945 return (bus_space_mmap(sc->sc_bustag, paddr, off,
946 prot, flags));
947 }
948
949 return (-1);
950 }
951
952
953 /*
954 * install an interrupt handler for a PCI device
955 */
956 void *
957 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
958 bus_space_tag_t t;
959 int ihandle;
960 int level;
961 int flags;
962 int (*handler) __P((void *));
963 void *arg;
964 {
965 struct psycho_pbm *pp = t->cookie;
966 struct psycho_softc *sc = pp->pp_sc;
967 struct intrhand *ih;
968 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
969 int64_t intrmap = 0;
970 int ino;
971 long vec = INTVEC(ihandle);
972
973 ih = (struct intrhand *)
974 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
975 if (ih == NULL)
976 return (NULL);
977
978 /*
979 * Hunt through all the interrupt mapping regs to look for our
980 * interrupt vector.
981 *
982 * XXX We only compare INOs rather than IGNs since the firmware may
983 * not provide the IGN and the IGN is constant for all device on that
984 * PCI controller. This could cause problems for the FFB/external
985 * interrupt which has a full vector that can be set arbitrarily.
986 */
987
988
989 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
990 ino = INTINO(vec);
991 DPRINTF(PDB_INTR, (" ino %x", ino));
992
993 /* If the device didn't ask for an IPL, use the one encoded. */
994 if (level == IPL_NONE) level = INTLEV(vec);
995 /* If it still has no level, print a warning and assign IPL 2 */
996 if (level == IPL_NONE) {
997 printf("ERROR: no IPL, setting IPL 2.\n");
998 level = 2;
999 }
1000
1001 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
1002
1003 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1004 (long)ino, intrlev[ino]));
1005
1006 /* Hunt thru obio first */
1007 for (intrmapptr = &sc->sc_regs->scsi_int_map,
1008 intrclrptr = &sc->sc_regs->scsi_clr_int;
1009 intrmapptr <= &sc->sc_regs->ffb1_int_map;
1010 intrmapptr++, intrclrptr++) {
1011 if (INTINO(*intrmapptr) == ino)
1012 goto found;
1013 }
1014
1015 /* Now do PCI interrupts */
1016 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1017 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1018 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1019 intrmapptr++, intrclrptr += 4) {
1020 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1021 intrclrptr += vec & 0x3;
1022 goto found;
1023 }
1024 }
1025 printf("Cannot find interrupt vector %lx\n", vec);
1026 return (NULL);
1027
1028 found:
1029 /* Register the map and clear intr registers */
1030 ih->ih_map = intrmapptr;
1031 ih->ih_clr = intrclrptr;
1032 }
1033 #ifdef NOT_DEBUG
1034 if (psycho_debug & PDB_INTR) {
1035 long i;
1036
1037 for (i = 0; i < 500000000; i++)
1038 continue;
1039 }
1040 #endif
1041
1042 ih->ih_fun = handler;
1043 ih->ih_arg = arg;
1044 ih->ih_pil = level;
1045 ih->ih_number = ino | sc->sc_ign;
1046
1047 DPRINTF(PDB_INTR, (
1048 "; installing handler %p arg %p with ino %u pil %u\n",
1049 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1050
1051 intr_establish(ih->ih_pil, ih);
1052
1053 /*
1054 * Enable the interrupt now we have the handler installed.
1055 * Read the current value as we can't change it besides the
1056 * valid bit so so make sure only this bit is changed.
1057 *
1058 * XXXX --- we really should use bus_space for this.
1059 */
1060 if (intrmapptr) {
1061 intrmap = *intrmapptr;
1062 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1063 (unsigned long long)intrmap));
1064
1065 /* Enable the interrupt */
1066 intrmap |= INTMAP_V;
1067 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1068 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1069 (unsigned long long)intrmap));
1070 *intrmapptr = intrmap;
1071 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1072 (unsigned long long)(intrmap = *intrmapptr)));
1073 }
1074 return (ih);
1075 }
1076
1077 /*
1078 * hooks into the iommu dvma calls.
1079 */
1080 int
1081 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1082 bus_dma_tag_t t;
1083 bus_dmamap_t map;
1084 void *buf;
1085 bus_size_t buflen;
1086 struct proc *p;
1087 int flags;
1088 {
1089 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1090 struct psycho_softc *sc = pp->pp_sc;
1091
1092 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1093 }
1094
1095 void
1096 psycho_dmamap_unload(t, map)
1097 bus_dma_tag_t t;
1098 bus_dmamap_t map;
1099 {
1100 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1101 struct psycho_softc *sc = pp->pp_sc;
1102
1103 iommu_dvmamap_unload(t, sc->sc_is, map);
1104 }
1105
1106 int
1107 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1108 bus_dma_tag_t t;
1109 bus_dmamap_t map;
1110 bus_dma_segment_t *segs;
1111 int nsegs;
1112 bus_size_t size;
1113 int flags;
1114 {
1115 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1116 struct psycho_softc *sc = pp->pp_sc;
1117
1118 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1119 }
1120
1121 void
1122 psycho_dmamap_sync(t, map, offset, len, ops)
1123 bus_dma_tag_t t;
1124 bus_dmamap_t map;
1125 bus_addr_t offset;
1126 bus_size_t len;
1127 int ops;
1128 {
1129 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1130 struct psycho_softc *sc = pp->pp_sc;
1131
1132 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1133 /* Flush the CPU then the IOMMU */
1134 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1135 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1136 }
1137 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1138 /* Flush the IOMMU then the CPU */
1139 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1140 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1141 }
1142
1143 }
1144
1145 int
1146 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1147 bus_dma_tag_t t;
1148 bus_size_t size;
1149 bus_size_t alignment;
1150 bus_size_t boundary;
1151 bus_dma_segment_t *segs;
1152 int nsegs;
1153 int *rsegs;
1154 int flags;
1155 {
1156 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1157 struct psycho_softc *sc = pp->pp_sc;
1158
1159 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1160 segs, nsegs, rsegs, flags));
1161 }
1162
1163 void
1164 psycho_dmamem_free(t, segs, nsegs)
1165 bus_dma_tag_t t;
1166 bus_dma_segment_t *segs;
1167 int nsegs;
1168 {
1169 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1170 struct psycho_softc *sc = pp->pp_sc;
1171
1172 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1173 }
1174
1175 int
1176 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1177 bus_dma_tag_t t;
1178 bus_dma_segment_t *segs;
1179 int nsegs;
1180 size_t size;
1181 caddr_t *kvap;
1182 int flags;
1183 {
1184 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1185 struct psycho_softc *sc = pp->pp_sc;
1186
1187 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1188 }
1189
1190 void
1191 psycho_dmamem_unmap(t, kva, size)
1192 bus_dma_tag_t t;
1193 caddr_t kva;
1194 size_t size;
1195 {
1196 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1197 struct psycho_softc *sc = pp->pp_sc;
1198
1199 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1200 }
1201