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