psycho.c revision 1.45 1 /* $NetBSD: psycho.c,v 1.45 2002/03/20 18:54:47 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 bus_space_handle_t 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 * Identify the device.
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
268 /* Register layouts are different. stuupid. */
269 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
270 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
271
272 if (ma->ma_naddress > 2) {
273 sparc_promaddr_to_handle(sc->sc_bustag,
274 ma->ma_address[2], &sc->sc_bh);
275 sparc_promaddr_to_handle(sc->sc_bustag,
276 ma->ma_address[0], &pci_ctl);
277
278 sc->sc_regs = (struct psychoreg *)
279 bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
280 } else if (ma->ma_nreg > 2) {
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, BUS_SPACE_MAP_LINEAR,
286 &sc->sc_bh))
287 panic("psycho_attach: cannot map regs");
288 sc->sc_regs = (struct psychoreg *)
289 bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
290
291 if (bus_space_map(sc->sc_bustag,
292 ma->ma_reg[0].ur_paddr,
293 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
294 &pci_ctl))
295 panic("psycho_attach: cannot map ctl");
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 sparc_promaddr_to_handle(sc->sc_bustag,
304 ma->ma_address[0], &sc->sc_bh);
305 sc->sc_regs = (struct psychoreg *)
306 bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
307 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
308 offsetof(struct psychoreg, psy_pcictl),
309 sizeof(struct pci_ctl), &pci_ctl);
310 } else if (ma->ma_nreg) {
311
312 /* We need to map this in ourselves. */
313 if (bus_space_map(sc->sc_bustag,
314 ma->ma_reg[0].ur_paddr,
315 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR,
316 &sc->sc_bh))
317 panic("psycho_attach: cannot map regs");
318 sc->sc_regs = (struct psychoreg *)
319 bus_space_vaddr(sc->sc_bustag, sc->sc_bh);
320
321 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
322 offsetof(struct psychoreg, psy_pcictl),
323 sizeof(struct pci_ctl), &pci_ctl);
324 } else
325 panic("psycho_attach: %d not enough registers",
326 ma->ma_nreg);
327 }
328
329
330 csr = bus_space_read_8(sc->sc_bustag, sc->sc_bh,
331 offsetof(struct psychoreg, psy_csr));
332 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
333 if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
334 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
335
336 printf("%s: impl %d, version %d: ign %x ",
337 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
338 sc->sc_ign);
339 /*
340 * Match other psycho's that are already configured against
341 * the base physical address. This will be the same for a
342 * pair of devices that share register space.
343 */
344 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
345
346 struct psycho_softc *asc =
347 (struct psycho_softc *)psycho_cd.cd_devs[n];
348
349 if (asc == NULL || asc == sc)
350 /* This entry is not there or it is me */
351 continue;
352
353 if (asc->sc_basepaddr != sc->sc_basepaddr)
354 /* This is an unrelated psycho */
355 continue;
356
357 /* Found partner */
358 osc = asc;
359 break;
360 }
361
362
363 /* Oh, dear. OK, lets get started */
364
365 /*
366 * Setup the PCI control register
367 */
368 csr = bus_space_read_8(sc->sc_bustag, pci_ctl,
369 offsetof(struct pci_ctl, pci_csr));
370 csr |= PCICTL_MRLM |
371 PCICTL_ARB_PARK |
372 PCICTL_ERRINTEN |
373 PCICTL_4ENABLE;
374 csr &= ~(PCICTL_SERR |
375 PCICTL_CPU_PRIO |
376 PCICTL_ARB_PRIO |
377 PCICTL_RTRYWAIT);
378 bus_space_write_8(sc->sc_bustag, pci_ctl,
379 offsetof(struct pci_ctl, pci_csr), csr);
380
381
382 /*
383 * Allocate our psycho_pbm
384 */
385 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
386 if (pp == NULL)
387 panic("could not allocate psycho pbm");
388
389 memset(pp, 0, sizeof *pp);
390
391 pp->pp_sc = sc;
392
393 /* grab the psycho ranges */
394 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
395
396 /* get the bus-range for the psycho */
397 psycho_get_bus_range(sc->sc_node, psycho_br);
398
399 pba.pba_bus = psycho_br[0];
400
401 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
402 printf("; PCI bus %d", psycho_br[0]);
403
404 pp->pp_pcictl = pci_ctl;
405
406 /* allocate our tags */
407 pp->pp_memt = psycho_alloc_mem_tag(pp);
408 pp->pp_iot = psycho_alloc_io_tag(pp);
409 pp->pp_dmat = psycho_alloc_dma_tag(pp);
410 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
411 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
412
413 /* allocate a chipset for this */
414 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
415
416 /* setup the rest of the psycho pbm */
417 pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
418
419 printf("\n");
420
421 /*
422 * And finally, if we're a sabre or the first of a pair of psycho's to
423 * arrive here, start up the IOMMU and get a config space tag.
424 */
425 if (osc == NULL) {
426 uint64_t timeo;
427
428 /*
429 * Establish handlers for interesting interrupts....
430 *
431 * XXX We need to remember these and remove this to support
432 * hotplug on the UPA/FHC bus.
433 *
434 * XXX Not all controllers have these, but installing them
435 * is better than trying to sort through this mess.
436 */
437 psycho_set_intr(sc, 15, psycho_ue,
438 &sc->sc_regs->ue_int_map,
439 &sc->sc_regs->ue_clr_int);
440 psycho_set_intr(sc, 1, psycho_ce,
441 &sc->sc_regs->ce_int_map,
442 &sc->sc_regs->ce_clr_int);
443 psycho_set_intr(sc, 15, psycho_bus_a,
444 &sc->sc_regs->pciaerr_int_map,
445 &sc->sc_regs->pciaerr_clr_int);
446 psycho_set_intr(sc, 15, psycho_bus_b,
447 &sc->sc_regs->pciberr_int_map,
448 &sc->sc_regs->pciberr_clr_int);
449 psycho_set_intr(sc, 15, psycho_powerfail,
450 &sc->sc_regs->power_int_map,
451 &sc->sc_regs->power_clr_int);
452 psycho_set_intr(sc, 1, psycho_wakeup,
453 &sc->sc_regs->pwrmgt_int_map,
454 &sc->sc_regs->pwrmgt_clr_int);
455
456
457 /*
458 * Apparently a number of machines with psycho and psycho+
459 * controllers have interrupt latency issues. We'll try
460 * setting the interrupt retry timeout to 0xff which gives us
461 * a retry of 3-6 usec (which is what sysio is set to) for the
462 * moment, which seems to help alleviate this problem.
463 */
464 timeo = sc->sc_regs->intr_retry_timer;
465 if (timeo > 0xfff) {
466 #ifdef DEBUG
467 printf("decreasing interrupt retry timeout "
468 "from %lx to 0xff\n", (long)timeo);
469 #endif
470 sc->sc_regs->intr_retry_timer = 0xff;
471 }
472
473 /*
474 * Setup IOMMU and PCI configuration if we're the first
475 * of a pair of psycho's to arrive here.
476 *
477 * We should calculate a TSB size based on amount of RAM
478 * and number of bus controllers and number an type of
479 * child devices.
480 *
481 * For the moment, 32KB should be more than enough.
482 */
483 sc->sc_is = malloc(sizeof(struct iommu_state),
484 M_DEVBUF, M_NOWAIT);
485 if (sc->sc_is == NULL)
486 panic("psycho_attach: malloc iommu_state");
487 sc->sc_is->is_sbvalid[0] = sc->sc_is->is_sbvalid[1] = 0;
488
489
490 if (PROM_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
491 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
492 offsetof(struct pci_ctl, pci_strbuf),
493 sizeof (struct iommu_strbuf),
494 &sc->sc_is->is_sb[0]);
495 sc->sc_is->is_sbvalid[0] = 1;
496 }
497
498 psycho_iommu_init(sc, 2);
499
500 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
501
502 /*
503 * XXX This is a really ugly hack because PCI config space
504 * is explicitly handled with unmapped accesses.
505 */
506 i = sc->sc_bustag->type;
507 sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE;
508 if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000,
509 0x0100000, 0, &bh))
510 panic("could not map psycho PCI configuration space");
511 sc->sc_bustag->type = i;
512 sc->sc_configaddr = bh;
513 } else {
514 /* Just copy IOMMU state, config tag and address */
515 sc->sc_is = osc->sc_is;
516 sc->sc_configtag = osc->sc_configtag;
517 sc->sc_configaddr = osc->sc_configaddr;
518
519 if (PROM_getproplen(sc->sc_node, "no-streaming-cache") < 0) {
520 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
521 offsetof(struct pci_ctl, pci_strbuf),
522 sizeof (struct iommu_strbuf),
523 &sc->sc_is->is_sb[1]);
524 sc->sc_is->is_sbvalid[1] = 1;
525 }
526 iommu_reset(sc->sc_is);
527 }
528
529 /*
530 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
531 */
532 pba.pba_busname = "pci";
533 pba.pba_flags = sc->sc_psycho_this->pp_flags;
534 pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
535 pba.pba_iot = sc->sc_psycho_this->pp_iot;
536 pba.pba_memt = sc->sc_psycho_this->pp_memt;
537
538 config_found(self, &pba, psycho_print);
539 }
540
541 static int
542 psycho_print(aux, p)
543 void *aux;
544 const char *p;
545 {
546
547 if (p == NULL)
548 return (UNCONF);
549 return (QUIET);
550 }
551
552 static void
553 psycho_set_intr(sc, ipl, handler, mapper, clearer)
554 struct psycho_softc *sc;
555 int ipl;
556 void *handler;
557 u_int64_t *mapper;
558 u_int64_t *clearer;
559 {
560 struct intrhand *ih;
561
562 ih = (struct intrhand *)malloc(sizeof(struct intrhand),
563 M_DEVBUF, M_NOWAIT);
564 ih->ih_arg = sc;
565 ih->ih_map = mapper;
566 ih->ih_clr = clearer;
567 ih->ih_fun = handler;
568 ih->ih_pil = (1<<ipl);
569 ih->ih_number = INTVEC(*(ih->ih_map));
570 intr_establish(ipl, ih);
571 *(ih->ih_map) |= INTMAP_V;
572 }
573
574 /*
575 * PCI bus support
576 */
577
578 /*
579 * allocate a PCI chipset tag and set it's cookie.
580 */
581 static pci_chipset_tag_t
582 psycho_alloc_chipset(pp, node, pc)
583 struct psycho_pbm *pp;
584 int node;
585 pci_chipset_tag_t pc;
586 {
587 pci_chipset_tag_t npc;
588
589 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
590 if (npc == NULL)
591 panic("could not allocate pci_chipset_tag_t");
592 memcpy(npc, pc, sizeof *pc);
593 npc->cookie = pp;
594 npc->rootnode = node;
595 npc->curnode = node;
596
597 return (npc);
598 }
599
600 /*
601 * grovel the OBP for various psycho properties
602 */
603 static void
604 psycho_get_bus_range(node, brp)
605 int node;
606 int *brp;
607 {
608 int n;
609
610 if (PROM_getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
611 panic("could not get psycho bus-range");
612 if (n != 2)
613 panic("broken psycho bus-range");
614 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
615 }
616
617 static void
618 psycho_get_ranges(node, rp, np)
619 int node;
620 struct psycho_ranges **rp;
621 int *np;
622 {
623
624 if (PROM_getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
625 panic("could not get psycho ranges");
626 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
627 }
628
629 /*
630 * Interrupt handlers.
631 */
632
633 static int
634 psycho_ue(arg)
635 void *arg;
636 {
637 struct psycho_softc *sc = (struct psycho_softc *)arg;
638 struct psychoreg *regs = sc->sc_regs;
639 long long afsr = regs->psy_ue_afsr;
640 long long afar = regs->psy_ue_afar;
641 long size = NBPG<<(sc->sc_is->is_tsbsize);
642 struct iommu_state *is = sc->sc_is;
643 char bits[128];
644
645 /*
646 * It's uncorrectable. Dump the regs and panic.
647 */
648 printf("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s",
649 sc->sc_dev.dv_xname, afar,
650 (long long)iommu_extract(is, (vaddr_t)afar), afsr,
651 bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
652 bits, sizeof(bits)));
653
654 /* Sometimes the AFAR points to an IOTSB entry */
655 if (afar >= is->is_ptsb && afar < is->is_ptsb + size) {
656 printf("IOVA %llx IOTTE %llx\n",
657 (long long)((afar - is->is_ptsb) * NBPG + is->is_dvmabase),
658 (long long)ldxa(afar, ASI_PHYS_CACHED));
659 }
660 #ifdef DDB
661 Debugger();
662 #endif
663 regs->psy_ue_afar = 0;
664 regs->psy_ue_afsr = 0;
665 return (1);
666 }
667 static int
668 psycho_ce(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 correctable. Dump the regs and continue.
676 */
677
678 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
679 sc->sc_dev.dv_xname,
680 (long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
681 return (1);
682 }
683 static int
684 psycho_bus_a(arg)
685 void *arg;
686 {
687 struct psycho_softc *sc = (struct psycho_softc *)arg;
688 struct psychoreg *regs = sc->sc_regs;
689
690 /*
691 * It's uncorrectable. Dump the regs and panic.
692 */
693
694 panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
695 sc->sc_dev.dv_xname,
696 (long long)regs->psy_pcictl[0].pci_afar,
697 (long long)regs->psy_pcictl[0].pci_afsr);
698 return (1);
699 }
700 static int
701 psycho_bus_b(arg)
702 void *arg;
703 {
704 struct psycho_softc *sc = (struct psycho_softc *)arg;
705 struct psychoreg *regs = sc->sc_regs;
706
707 /*
708 * It's uncorrectable. Dump the regs and panic.
709 */
710
711 panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
712 sc->sc_dev.dv_xname,
713 (long long)regs->psy_pcictl[0].pci_afar,
714 (long long)regs->psy_pcictl[0].pci_afsr);
715 return (1);
716 }
717 static int
718 psycho_powerfail(arg)
719 void *arg;
720 {
721
722 /*
723 * We lost power. Try to shut down NOW.
724 */
725 printf("Power Failure Detected: Shutting down NOW.\n");
726 cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
727 return (1);
728 }
729 static
730 int psycho_wakeup(arg)
731 void *arg;
732 {
733 struct psycho_softc *sc = (struct psycho_softc *)arg;
734
735 /*
736 * Gee, we don't really have a framework to deal with this
737 * properly.
738 */
739 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname);
740 return (1);
741 }
742
743
744
745 /*
746 * initialise the IOMMU..
747 */
748 void
749 psycho_iommu_init(sc, tsbsize)
750 struct psycho_softc *sc;
751 int tsbsize;
752 {
753 char *name;
754 struct iommu_state *is = sc->sc_is;
755 u_int32_t iobase = -1;
756 int *vdma = NULL;
757 int nitem;
758
759 /* punch in our copies */
760 is->is_bustag = sc->sc_bustag;
761 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
762 offsetof(struct psychoreg, psy_iommu),
763 sizeof (struct iommureg),
764 &is->is_iommu);
765
766 /*
767 * Separate the men from the boys. Get the `virtual-dma'
768 * property for sabre and use that to make sure the damn
769 * iommu works.
770 *
771 * We could query the `#virtual-dma-size-cells' and
772 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
773 */
774 if (!PROM_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
775 (void **)&vdma)) {
776 /* Damn. Gotta use these values. */
777 iobase = vdma[0];
778 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break
779 switch (vdma[1]) {
780 TSBCASE(1); TSBCASE(2); TSBCASE(3);
781 TSBCASE(4); TSBCASE(5); TSBCASE(6);
782 default:
783 printf("bogus tsb size %x, using 7\n", vdma[1]);
784 TSBCASE(7);
785 }
786 #undef TSBCASE
787 }
788
789 /* give us a nice name.. */
790 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
791 if (name == 0)
792 panic("couldn't malloc iommu name");
793 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
794
795 iommu_init(name, is, tsbsize, iobase);
796 }
797
798 /*
799 * below here is bus space and bus dma support
800 */
801 bus_space_tag_t
802 psycho_alloc_bus_tag(pp, type)
803 struct psycho_pbm *pp;
804 int type;
805 {
806 struct psycho_softc *sc = pp->pp_sc;
807 bus_space_tag_t bt;
808
809 bt = (bus_space_tag_t)
810 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
811 if (bt == NULL)
812 panic("could not allocate psycho bus tag");
813
814 bzero(bt, sizeof *bt);
815 bt->cookie = pp;
816 bt->parent = sc->sc_bustag;
817 bt->type = type;
818 bt->sparc_bus_map = _psycho_bus_map;
819 bt->sparc_bus_mmap = psycho_bus_mmap;
820 bt->sparc_intr_establish = psycho_intr_establish;
821 return (bt);
822 }
823
824 bus_dma_tag_t
825 psycho_alloc_dma_tag(pp)
826 struct psycho_pbm *pp;
827 {
828 struct psycho_softc *sc = pp->pp_sc;
829 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
830
831 dt = (bus_dma_tag_t)
832 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
833 if (dt == NULL)
834 panic("could not allocate psycho dma tag");
835
836 bzero(dt, sizeof *dt);
837 dt->_cookie = pp;
838 dt->_parent = pdt;
839 #define PCOPY(x) dt->x = pdt->x
840 PCOPY(_dmamap_create);
841 PCOPY(_dmamap_destroy);
842 dt->_dmamap_load = psycho_dmamap_load;
843 PCOPY(_dmamap_load_mbuf);
844 PCOPY(_dmamap_load_uio);
845 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
846 dt->_dmamap_unload = psycho_dmamap_unload;
847 dt->_dmamap_sync = psycho_dmamap_sync;
848 dt->_dmamem_alloc = psycho_dmamem_alloc;
849 dt->_dmamem_free = psycho_dmamem_free;
850 dt->_dmamem_map = psycho_dmamem_map;
851 dt->_dmamem_unmap = psycho_dmamem_unmap;
852 PCOPY(_dmamem_mmap);
853 #undef PCOPY
854 return (dt);
855 }
856
857 /*
858 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
859 * PCI physical addresses.
860 */
861
862 static int get_childspace __P((int));
863
864 static int
865 get_childspace(type)
866 int type;
867 {
868 int ss;
869
870 switch (type) {
871 case PCI_CONFIG_BUS_SPACE:
872 ss = 0x00;
873 break;
874 case PCI_IO_BUS_SPACE:
875 ss = 0x01;
876 break;
877 case PCI_MEMORY_BUS_SPACE:
878 ss = 0x02;
879 break;
880 #if 0
881 /* we don't do 64 bit memory space */
882 case PCI_MEMORY64_BUS_SPACE:
883 ss = 0x03;
884 break;
885 #endif
886 default:
887 panic("get_childspace: unknown bus type");
888 }
889
890 return (ss);
891 }
892
893 static int
894 _psycho_bus_map(t, offset, size, flags, unused, hp)
895 bus_space_tag_t t;
896 bus_addr_t offset;
897 bus_size_t size;
898 int flags;
899 vaddr_t unused;
900 bus_space_handle_t *hp;
901 {
902 struct psycho_pbm *pp = t->cookie;
903 struct psycho_softc *sc = pp->pp_sc;
904 int i, ss;
905
906 DPRINTF(PDB_BUSMAP,
907 ("_psycho_bus_map: type %d off %qx sz %qx flags %d",
908 t->type, (unsigned long long)offset,
909 (unsigned long long)size, flags));
910
911 ss = get_childspace(t->type);
912 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
913
914 for (i = 0; i < pp->pp_nrange; i++) {
915 bus_addr_t paddr;
916
917 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
918 continue;
919
920 paddr = pp->pp_range[i].phys_lo + offset;
921 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
922 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
923 (long)ss, (long)offset,
924 (unsigned long long)paddr));
925 return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
926 flags, 0, hp));
927 }
928 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
929 return (EINVAL);
930 }
931
932 static paddr_t
933 psycho_bus_mmap(t, paddr, off, prot, flags)
934 bus_space_tag_t t;
935 bus_addr_t paddr;
936 off_t off;
937 int prot;
938 int flags;
939 {
940 bus_addr_t offset = paddr;
941 struct psycho_pbm *pp = t->cookie;
942 struct psycho_softc *sc = pp->pp_sc;
943 int i, ss;
944
945 ss = get_childspace(t->type);
946
947 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
948 prot, flags, (unsigned long long)paddr));
949
950 for (i = 0; i < pp->pp_nrange; i++) {
951 bus_addr_t paddr;
952
953 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
954 continue;
955
956 paddr = pp->pp_range[i].phys_lo + offset;
957 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
958 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
959 "space %lx offset %lx paddr %qx\n",
960 (long)ss, (long)offset,
961 (unsigned long long)paddr));
962 return (bus_space_mmap(sc->sc_bustag, paddr, off,
963 prot, flags));
964 }
965
966 return (-1);
967 }
968
969
970 /*
971 * install an interrupt handler for a PCI device
972 */
973 void *
974 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
975 bus_space_tag_t t;
976 int ihandle;
977 int level;
978 int flags;
979 int (*handler) __P((void *));
980 void *arg;
981 {
982 struct psycho_pbm *pp = t->cookie;
983 struct psycho_softc *sc = pp->pp_sc;
984 struct intrhand *ih;
985 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
986 int64_t intrmap = 0;
987 int ino;
988 long vec = INTVEC(ihandle);
989
990 ih = (struct intrhand *)
991 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
992 if (ih == NULL)
993 return (NULL);
994
995 /*
996 * Hunt through all the interrupt mapping regs to look for our
997 * interrupt vector.
998 *
999 * XXX We only compare INOs rather than IGNs since the firmware may
1000 * not provide the IGN and the IGN is constant for all device on that
1001 * PCI controller. This could cause problems for the FFB/external
1002 * interrupt which has a full vector that can be set arbitrarily.
1003 */
1004
1005
1006 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
1007 ino = INTINO(vec);
1008 DPRINTF(PDB_INTR, (" ino %x", ino));
1009
1010 /* If the device didn't ask for an IPL, use the one encoded. */
1011 if (level == IPL_NONE) level = INTLEV(vec);
1012 /* If it still has no level, print a warning and assign IPL 2 */
1013 if (level == IPL_NONE) {
1014 printf("ERROR: no IPL, setting IPL 2.\n");
1015 level = 2;
1016 }
1017
1018 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
1019
1020 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1021 (long)ino, intrlev[ino]));
1022
1023 /* Hunt thru obio first */
1024 for (intrmapptr = &sc->sc_regs->scsi_int_map,
1025 intrclrptr = &sc->sc_regs->scsi_clr_int;
1026 intrmapptr <= &sc->sc_regs->ffb1_int_map;
1027 intrmapptr++, intrclrptr++) {
1028 if (INTINO(*intrmapptr) == ino)
1029 goto found;
1030 }
1031
1032 /* Now do PCI interrupts */
1033 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1034 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1035 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1036 intrmapptr++, intrclrptr += 4) {
1037 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1038 intrclrptr += vec & 0x3;
1039 goto found;
1040 }
1041 }
1042 printf("Cannot find interrupt vector %lx\n", vec);
1043 return (NULL);
1044
1045 found:
1046 /* Register the map and clear intr registers */
1047 ih->ih_map = intrmapptr;
1048 ih->ih_clr = intrclrptr;
1049 }
1050 #ifdef NOT_DEBUG
1051 if (psycho_debug & PDB_INTR) {
1052 long i;
1053
1054 for (i = 0; i < 500000000; i++)
1055 continue;
1056 }
1057 #endif
1058
1059 ih->ih_fun = handler;
1060 ih->ih_arg = arg;
1061 ih->ih_pil = level;
1062 ih->ih_number = ino | sc->sc_ign;
1063
1064 DPRINTF(PDB_INTR, (
1065 "; installing handler %p arg %p with ino %u pil %u\n",
1066 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1067
1068 intr_establish(ih->ih_pil, ih);
1069
1070 /*
1071 * Enable the interrupt now we have the handler installed.
1072 * Read the current value as we can't change it besides the
1073 * valid bit so so make sure only this bit is changed.
1074 *
1075 * XXXX --- we really should use bus_space for this.
1076 */
1077 if (intrmapptr) {
1078 intrmap = *intrmapptr;
1079 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1080 (unsigned long long)intrmap));
1081
1082 /* Enable the interrupt */
1083 intrmap |= INTMAP_V;
1084 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1085 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1086 (unsigned long long)intrmap));
1087 *intrmapptr = intrmap;
1088 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1089 (unsigned long long)(intrmap = *intrmapptr)));
1090 }
1091 return (ih);
1092 }
1093
1094 /*
1095 * hooks into the iommu dvma calls.
1096 */
1097 int
1098 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1099 bus_dma_tag_t t;
1100 bus_dmamap_t map;
1101 void *buf;
1102 bus_size_t buflen;
1103 struct proc *p;
1104 int flags;
1105 {
1106 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1107 struct psycho_softc *sc = pp->pp_sc;
1108
1109 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1110 }
1111
1112 void
1113 psycho_dmamap_unload(t, map)
1114 bus_dma_tag_t t;
1115 bus_dmamap_t map;
1116 {
1117 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1118 struct psycho_softc *sc = pp->pp_sc;
1119
1120 iommu_dvmamap_unload(t, sc->sc_is, map);
1121 }
1122
1123 int
1124 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1125 bus_dma_tag_t t;
1126 bus_dmamap_t map;
1127 bus_dma_segment_t *segs;
1128 int nsegs;
1129 bus_size_t size;
1130 int flags;
1131 {
1132 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1133 struct psycho_softc *sc = pp->pp_sc;
1134
1135 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1136 }
1137
1138 void
1139 psycho_dmamap_sync(t, map, offset, len, ops)
1140 bus_dma_tag_t t;
1141 bus_dmamap_t map;
1142 bus_addr_t offset;
1143 bus_size_t len;
1144 int ops;
1145 {
1146 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1147 struct psycho_softc *sc = pp->pp_sc;
1148
1149 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1150 /* Flush the CPU then the IOMMU */
1151 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1152 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1153 }
1154 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1155 /* Flush the IOMMU then the CPU */
1156 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1157 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1158 }
1159
1160 }
1161
1162 int
1163 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1164 bus_dma_tag_t t;
1165 bus_size_t size;
1166 bus_size_t alignment;
1167 bus_size_t boundary;
1168 bus_dma_segment_t *segs;
1169 int nsegs;
1170 int *rsegs;
1171 int flags;
1172 {
1173 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1174 struct psycho_softc *sc = pp->pp_sc;
1175
1176 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1177 segs, nsegs, rsegs, flags));
1178 }
1179
1180 void
1181 psycho_dmamem_free(t, segs, nsegs)
1182 bus_dma_tag_t t;
1183 bus_dma_segment_t *segs;
1184 int nsegs;
1185 {
1186 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1187 struct psycho_softc *sc = pp->pp_sc;
1188
1189 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1190 }
1191
1192 int
1193 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1194 bus_dma_tag_t t;
1195 bus_dma_segment_t *segs;
1196 int nsegs;
1197 size_t size;
1198 caddr_t *kvap;
1199 int flags;
1200 {
1201 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1202 struct psycho_softc *sc = pp->pp_sc;
1203
1204 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1205 }
1206
1207 void
1208 psycho_dmamem_unmap(t, kva, size)
1209 bus_dma_tag_t t;
1210 caddr_t kva;
1211 size_t size;
1212 {
1213 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1214 struct psycho_softc *sc = pp->pp_sc;
1215
1216 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1217 }
1218