psycho.c revision 1.34 1 /* $NetBSD: psycho.c,v 1.34 2001/07/20 00:07:13 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 #undef DEBUG
39 #define DEBUG
40
41 #ifdef DEBUG
42 #define PDB_PROM 0x01
43 #define PDB_BUSMAP 0x02
44 #define PDB_INTR 0x04
45 int psycho_debug = 0x0;
46 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0)
47 #else
48 #define DPRINTF(l, s)
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/device.h>
53 #include <sys/errno.h>
54 #include <sys/extent.h>
55 #include <sys/malloc.h>
56 #include <sys/systm.h>
57 #include <sys/time.h>
58 #include <sys/reboot.h>
59
60 #define _SPARC_BUS_DMA_PRIVATE
61 #include <machine/bus.h>
62 #include <machine/autoconf.h>
63 #include <machine/psl.h>
64
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67
68 #include <sparc64/dev/iommureg.h>
69 #include <sparc64/dev/iommuvar.h>
70 #include <sparc64/dev/psychoreg.h>
71 #include <sparc64/dev/psychovar.h>
72 #include <sparc64/sparc64/cache.h>
73
74 #include "ioconf.h"
75
76 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
77 pci_chipset_tag_t));
78 static void psycho_get_bus_range __P((int, int *));
79 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
80 static void psycho_set_intr __P((struct psycho_softc *, int, void *,
81 u_int64_t *, u_int64_t *));
82
83 /* Interrupt handlers */
84 static int psycho_ue __P((void *));
85 static int psycho_ce __P((void *));
86 static int psycho_bus_a __P((void *));
87 static int psycho_bus_b __P((void *));
88 static int psycho_powerfail __P((void *));
89 static int psycho_wakeup __P((void *));
90
91
92 /* IOMMU support */
93 static void psycho_iommu_init __P((struct psycho_softc *, int));
94
95 /*
96 * bus space and bus dma support for UltraSPARC `psycho'. note that most
97 * of the bus dma support is provided by the iommu dvma controller.
98 */
99 static int psycho_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
100 int, bus_space_handle_t *));
101 static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
102 bus_size_t, int, vaddr_t,
103 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 #define ROM_PCI_NAME "pci"
173 #define ROM_SABRE_MODEL "SUNW,sabre"
174 #define ROM_SIMBA_MODEL "SUNW,simba"
175 #define ROM_PSYCHO_MODEL "SUNW,psycho"
176
177 static int
178 psycho_match(parent, match, aux)
179 struct device *parent;
180 struct cfdata *match;
181 void *aux;
182 {
183 struct mainbus_attach_args *ma = aux;
184 char *model = getpropstring(ma->ma_node, "model");
185
186 /* match on a name of "pci" and a sabre or a psycho */
187 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0 &&
188 (strcmp(model, ROM_SABRE_MODEL) == 0 ||
189 strcmp(model, ROM_PSYCHO_MODEL) == 0))
190 return (1);
191
192 return (0);
193 }
194
195 /*
196 * SUNW,psycho initialisation ..
197 * - find the per-psycho registers
198 * - figure out the IGN.
199 * - find our partner psycho
200 * - configure ourselves
201 * - bus range, bus,
202 * - get interrupt-map and interrupt-map-mask
203 * - setup the chipsets.
204 * - if we're the first of the pair, initialise the IOMMU, otherwise
205 * just copy it's tags and addresses.
206 */
207 static void
208 psycho_attach(parent, self, aux)
209 struct device *parent, *self;
210 void *aux;
211 {
212 struct psycho_softc *sc = (struct psycho_softc *)self;
213 struct psycho_softc *osc = NULL;
214 struct psycho_pbm *pp;
215 struct pcibus_attach_args pba;
216 struct mainbus_attach_args *ma = aux;
217 bus_space_handle_t bh;
218 u_int64_t csr;
219 int psycho_br[2], n;
220 struct pci_ctl *pci_ctl;
221 char *model = getpropstring(ma->ma_node, "model");
222
223 printf("\n");
224
225 sc->sc_node = ma->ma_node;
226 sc->sc_bustag = ma->ma_bustag;
227 sc->sc_dmatag = ma->ma_dmatag;
228
229 /*
230 * call the model-specific initialisation routine.
231 */
232
233 if (strcmp(model, ROM_SABRE_MODEL) == 0)
234 sc->sc_mode = PSYCHO_MODE_SABRE;
235 else if (strcmp(model, ROM_PSYCHO_MODEL) == 0)
236 sc->sc_mode = PSYCHO_MODE_PSYCHO;
237 else
238 panic("psycho_attach: unknown model %s?", model);
239
240 /*
241 * The psycho gets three register banks:
242 * (0) per-PBM configuration and status registers
243 * (1) per-PBM PCI configuration space, containing only the
244 * PBM 256-byte PCI header
245 * (2) the shared psycho configuration registers (struct psychoreg)
246 *
247 * XXX use the prom address for the psycho registers? we do so far.
248 */
249
250 /* Register layouts are different. stuupid. */
251 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
252 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
253
254 if (ma->ma_naddress > 2) {
255 sc->sc_regs = (struct psychoreg *)
256 (u_long)ma->ma_address[2];
257 pci_ctl = (struct pci_ctl *)
258 (u_long)ma->ma_address[0];
259 } else if (ma->ma_nreg > 2) {
260 bus_space_handle_t handle;
261
262 /* We need to map this in ourselves. */
263 if (bus_space_map2(sc->sc_bustag, 0,
264 ma->ma_reg[2].ur_paddr,
265 ma->ma_reg[2].ur_len, 0, NULL, &handle))
266 panic("psycho_attach: cannot map regs");
267 sc->sc_regs = (struct psychoreg *)(u_long)handle;
268
269 if (bus_space_map2(sc->sc_bustag, 0,
270 ma->ma_reg[0].ur_paddr,
271 ma->ma_reg[0].ur_len, 0, NULL, &handle))
272 panic("psycho_attach: cannot map ctl");
273 /* XXX -- this is lost but never unmapped */
274 pci_ctl = (struct pci_ctl *)(u_long)handle;
275
276 } else
277 panic("psycho_attach: %d not enough registers",
278 ma->ma_nreg);
279 } else {
280 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
281
282 if (ma->ma_naddress) {
283 sc->sc_regs = (struct psychoreg *)
284 (u_long)ma->ma_address[0];
285 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
286 } else if (ma->ma_nreg) {
287 bus_space_handle_t handle;
288
289 /* We need to map this in ourselves. */
290 if (bus_space_map2(sc->sc_bustag, 0,
291 ma->ma_reg[0].ur_paddr,
292 ma->ma_reg[0].ur_len, 0, NULL, &handle))
293 panic("psycho_attach: cannot map regs");
294 sc->sc_regs = (struct psychoreg *)(u_long)handle;
295 pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
296 } else
297 panic("psycho_attach: %d not enough registers",
298 ma->ma_nreg);
299 }
300
301 csr = sc->sc_regs->psy_csr;
302 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
303 if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
304 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
305
306 printf("%s: impl %d, version %d: ign %x ",
307 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
308 sc->sc_ign);
309 /*
310 * Match other psycho's that are already configured against
311 * the base physical address. This will be the same for a
312 * pair of devices that share register space.
313 */
314 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
315
316 struct psycho_softc *asc =
317 (struct psycho_softc *)psycho_cd.cd_devs[n];
318
319 if (asc == NULL || asc == sc)
320 /* This entry is not there or it is me */
321 continue;
322
323 if (asc->sc_basepaddr != sc->sc_basepaddr)
324 /* This is an unrelated psycho */
325 continue;
326
327 /* Found partner */
328 osc = asc;
329 break;
330 }
331
332
333 /* Oh, dear. OK, lets get started */
334
335 /*
336 * Setup the PCI control register
337 */
338 csr = bus_space_read_8(sc->sc_bustag,
339 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
340 csr |= PCICTL_MRLM |
341 PCICTL_ARB_PARK |
342 PCICTL_ERRINTEN |
343 PCICTL_4ENABLE;
344 csr &= ~(PCICTL_SERR |
345 PCICTL_CPU_PRIO |
346 PCICTL_ARB_PRIO |
347 PCICTL_RTRYWAIT);
348 bus_space_write_8(sc->sc_bustag,
349 (bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
350
351
352 /*
353 * Allocate our psycho_pbm
354 */
355 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
356 if (pp == NULL)
357 panic("could not allocate psycho pbm");
358
359 memset(pp, 0, sizeof *pp);
360
361 pp->pp_sc = sc;
362
363 /* grab the psycho ranges */
364 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
365
366 /* get the bus-range for the psycho */
367 psycho_get_bus_range(sc->sc_node, psycho_br);
368
369 pba.pba_bus = psycho_br[0];
370
371 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
372 printf("; PCI bus %d", psycho_br[0]);
373
374 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
375
376 /* allocate our tags */
377 pp->pp_memt = psycho_alloc_mem_tag(pp);
378 pp->pp_iot = psycho_alloc_io_tag(pp);
379 pp->pp_dmat = psycho_alloc_dma_tag(pp);
380 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
381 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
382
383 /* allocate a chipset for this */
384 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
385
386 /* setup the rest of the psycho pbm */
387 pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
388
389 printf("\n");
390
391 /*
392 * And finally, if we're a sabre or the first of a pair of psycho's to
393 * arrive here, start up the IOMMU and get a config space tag.
394 */
395 if (osc == NULL) {
396
397 /*
398 * Establish handlers for interesting interrupts....
399 *
400 * XXX We need to remember these and remove this to support
401 * hotplug on the UPA/FHC bus.
402 *
403 * XXX Not all controllers have these, but installing them
404 * is better than trying to sort through this mess.
405 */
406 psycho_set_intr(sc, 15, psycho_ue,
407 &sc->sc_regs->ue_int_map,
408 &sc->sc_regs->ue_clr_int);
409 psycho_set_intr(sc, 1, psycho_ce,
410 &sc->sc_regs->ce_int_map,
411 &sc->sc_regs->ce_clr_int);
412 psycho_set_intr(sc, 15, psycho_bus_a,
413 &sc->sc_regs->pciaerr_int_map,
414 &sc->sc_regs->pciaerr_clr_int);
415 psycho_set_intr(sc, 15, psycho_bus_b,
416 &sc->sc_regs->pciberr_int_map,
417 &sc->sc_regs->pciberr_clr_int);
418 psycho_set_intr(sc, 15, psycho_powerfail,
419 &sc->sc_regs->power_int_map,
420 &sc->sc_regs->power_clr_int);
421 psycho_set_intr(sc, 1, psycho_wakeup,
422 &sc->sc_regs->pwrmgt_int_map,
423 &sc->sc_regs->pwrmgt_clr_int);
424
425 /*
426 * Setup IOMMU and PCI configuration if we're the first
427 * of a pair of psycho's to arrive here.
428 *
429 * We should calculate a TSB size based on amount of RAM
430 * and number of bus controllers and number an type of
431 * child devices.
432 *
433 * For the moment, 32KB should be more than enough.
434 */
435 psycho_iommu_init(sc, 2);
436
437 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
438 if (bus_space_map2(sc->sc_bustag,
439 PCI_CONFIG_BUS_SPACE,
440 sc->sc_basepaddr + 0x01000000,
441 0x0100000,
442 0,
443 0,
444 &bh))
445 panic("could not map psycho PCI configuration space");
446 sc->sc_configaddr = (off_t)bh;
447 } else {
448 /* Just copy IOMMU state, config tag and address */
449 sc->sc_is = osc->sc_is;
450 sc->sc_configtag = osc->sc_configtag;
451 sc->sc_configaddr = osc->sc_configaddr;
452 }
453
454 /*
455 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
456 */
457 pba.pba_busname = "pci";
458 pba.pba_flags = sc->sc_psycho_this->pp_flags;
459 pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
460 pba.pba_iot = sc->sc_psycho_this->pp_iot;
461 pba.pba_memt = sc->sc_psycho_this->pp_memt;
462
463 config_found(self, &pba, psycho_print);
464 }
465
466 static int
467 psycho_print(aux, p)
468 void *aux;
469 const char *p;
470 {
471
472 if (p == NULL)
473 return (UNCONF);
474 return (QUIET);
475 }
476
477 static void
478 psycho_set_intr(sc, ipl, handler, mapper, clearer)
479 struct psycho_softc *sc;
480 int ipl;
481 void *handler;
482 u_int64_t *mapper;
483 u_int64_t *clearer;
484 {
485 struct intrhand *ih;
486
487 ih = (struct intrhand *)malloc(sizeof(struct intrhand),
488 M_DEVBUF, M_NOWAIT);
489 ih->ih_arg = sc;
490 ih->ih_map = mapper;
491 ih->ih_clr = clearer;
492 ih->ih_fun = handler;
493 ih->ih_pil = (1<<ipl);
494 ih->ih_number = INTVEC(*(ih->ih_map));
495 intr_establish(ipl, ih);
496 *(ih->ih_map) |= INTMAP_V;
497 }
498
499 /*
500 * PCI bus support
501 */
502
503 /*
504 * allocate a PCI chipset tag and set it's cookie.
505 */
506 static pci_chipset_tag_t
507 psycho_alloc_chipset(pp, node, pc)
508 struct psycho_pbm *pp;
509 int node;
510 pci_chipset_tag_t pc;
511 {
512 pci_chipset_tag_t npc;
513
514 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
515 if (npc == NULL)
516 panic("could not allocate pci_chipset_tag_t");
517 memcpy(npc, pc, sizeof *pc);
518 npc->cookie = pp;
519 npc->rootnode = node;
520 npc->curnode = node;
521
522 return (npc);
523 }
524
525 /*
526 * grovel the OBP for various psycho properties
527 */
528 static void
529 psycho_get_bus_range(node, brp)
530 int node;
531 int *brp;
532 {
533 int n;
534
535 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
536 panic("could not get psycho bus-range");
537 if (n != 2)
538 panic("broken psycho bus-range");
539 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
540 }
541
542 static void
543 psycho_get_ranges(node, rp, np)
544 int node;
545 struct psycho_ranges **rp;
546 int *np;
547 {
548
549 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
550 panic("could not get psycho ranges");
551 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
552 }
553
554 /*
555 * Interrupt handlers.
556 */
557
558 static int
559 psycho_ue(arg)
560 void *arg;
561 {
562 struct psycho_softc *sc = (struct psycho_softc *)arg;
563 struct psychoreg *regs = sc->sc_regs;
564
565 /*
566 * It's uncorrectable. Dump the regs and panic.
567 */
568
569 panic("%s: uncorrectable DMA error AFAR %llx AFSR %llx\n",
570 sc->sc_dev.dv_xname,
571 (long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
572 return (1);
573 }
574 static int
575 psycho_ce(arg)
576 void *arg;
577 {
578 struct psycho_softc *sc = (struct psycho_softc *)arg;
579 struct psychoreg *regs = sc->sc_regs;
580
581 /*
582 * It's correctable. Dump the regs and continue.
583 */
584
585 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
586 sc->sc_dev.dv_xname,
587 (long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
588 return (1);
589 }
590 static int
591 psycho_bus_a(arg)
592 void *arg;
593 {
594 struct psycho_softc *sc = (struct psycho_softc *)arg;
595 struct psychoreg *regs = sc->sc_regs;
596
597 /*
598 * It's uncorrectable. Dump the regs and panic.
599 */
600
601 panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
602 sc->sc_dev.dv_xname,
603 (long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
604 return (1);
605 }
606 static int
607 psycho_bus_b(arg)
608 void *arg;
609 {
610 struct psycho_softc *sc = (struct psycho_softc *)arg;
611 struct psychoreg *regs = sc->sc_regs;
612
613 /*
614 * It's uncorrectable. Dump the regs and panic.
615 */
616
617 panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
618 sc->sc_dev.dv_xname,
619 (long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
620 return (1);
621 }
622 static int
623 psycho_powerfail(arg)
624 void *arg;
625 {
626
627 /*
628 * We lost power. Try to shut down NOW.
629 */
630 printf("Power Failure Detected: Shutting down NOW.\n");
631 cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
632 return (1);
633 }
634 static
635 int psycho_wakeup(arg)
636 void *arg;
637 {
638 struct psycho_softc *sc = (struct psycho_softc *)arg;
639
640 /*
641 * Gee, we don't really have a framework to deal with this
642 * properly.
643 */
644 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname);
645 return (1);
646 }
647
648
649
650 /*
651 * initialise the IOMMU..
652 */
653 void
654 psycho_iommu_init(sc, tsbsize)
655 struct psycho_softc *sc;
656 int tsbsize;
657 {
658 char *name;
659 struct iommu_state *is;
660 u_int32_t iobase = -1;
661 int *vdma = NULL;
662 int nitem;
663
664 is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
665 if (is == NULL)
666 panic("psycho_iommu_init: malloc is");
667
668 sc->sc_is = is;
669
670 /* punch in our copies */
671 is->is_bustag = sc->sc_bustag;
672 is->is_iommu = &sc->sc_regs->psy_iommu;
673
674 if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
675 is->is_sb = 0;
676 else
677 is->is_sb = &sc->sc_regs->psy_iommu_strbuf;
678
679 /*
680 * Separate the men from the boys. Get the `virtual-dma'
681 * property for sabre and use that to make sure the damn
682 * iommu works.
683 *
684 * We could query the `#virtual-dma-size-cells' and
685 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
686 */
687 if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
688 (void **)&vdma)) {
689 /* Damn. Gotta use these values. */
690 iobase = vdma[0];
691 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break
692 switch (vdma[1]) {
693 TSBCASE(1); TSBCASE(2); TSBCASE(3);
694 TSBCASE(4); TSBCASE(5); TSBCASE(6);
695 default:
696 printf("bogus tsb size %x, using 7\n", vdma[1]);
697 TSBCASE(7);
698 }
699 #undef TSBCASE
700 }
701
702 /* give us a nice name.. */
703 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
704 if (name == 0)
705 panic("couldn't malloc iommu name");
706 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
707
708 iommu_init(name, is, tsbsize, iobase);
709 }
710
711 /*
712 * below here is bus space and bus dma support
713 */
714 bus_space_tag_t
715 psycho_alloc_bus_tag(pp, type)
716 struct psycho_pbm *pp;
717 int type;
718 {
719 struct psycho_softc *sc = pp->pp_sc;
720 bus_space_tag_t bt;
721
722 bt = (bus_space_tag_t)
723 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
724 if (bt == NULL)
725 panic("could not allocate psycho bus tag");
726
727 bzero(bt, sizeof *bt);
728 bt->cookie = pp;
729 bt->parent = sc->sc_bustag;
730 bt->type = type;
731 bt->sparc_bus_map = _psycho_bus_map;
732 bt->sparc_bus_mmap = psycho_bus_mmap;
733 bt->sparc_intr_establish = psycho_intr_establish;
734 return (bt);
735 }
736
737 bus_dma_tag_t
738 psycho_alloc_dma_tag(pp)
739 struct psycho_pbm *pp;
740 {
741 struct psycho_softc *sc = pp->pp_sc;
742 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
743
744 dt = (bus_dma_tag_t)
745 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
746 if (dt == NULL)
747 panic("could not allocate psycho dma tag");
748
749 bzero(dt, sizeof *dt);
750 dt->_cookie = pp;
751 dt->_parent = pdt;
752 #define PCOPY(x) dt->x = pdt->x
753 PCOPY(_dmamap_create);
754 PCOPY(_dmamap_destroy);
755 dt->_dmamap_load = psycho_dmamap_load;
756 PCOPY(_dmamap_load_mbuf);
757 PCOPY(_dmamap_load_uio);
758 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
759 dt->_dmamap_unload = psycho_dmamap_unload;
760 dt->_dmamap_sync = psycho_dmamap_sync;
761 dt->_dmamem_alloc = psycho_dmamem_alloc;
762 dt->_dmamem_free = psycho_dmamem_free;
763 dt->_dmamem_map = psycho_dmamem_map;
764 dt->_dmamem_unmap = psycho_dmamem_unmap;
765 PCOPY(_dmamem_mmap);
766 #undef PCOPY
767 return (dt);
768 }
769
770 /*
771 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
772 * PCI physical addresses.
773 */
774
775 static int get_childspace __P((int));
776
777 static int
778 get_childspace(type)
779 int type;
780 {
781 int ss;
782
783 switch (type) {
784 case PCI_CONFIG_BUS_SPACE:
785 ss = 0x00;
786 break;
787 case PCI_IO_BUS_SPACE:
788 ss = 0x01;
789 break;
790 case PCI_MEMORY_BUS_SPACE:
791 ss = 0x02;
792 break;
793 #if 0
794 /* we don't do 64 bit memory space */
795 case PCI_MEMORY64_BUS_SPACE:
796 ss = 0x03;
797 break;
798 #endif
799 default:
800 panic("get_childspace: unknown bus type");
801 }
802
803 return (ss);
804 }
805
806 static int
807 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
808 bus_space_tag_t t;
809 bus_type_t btype;
810 bus_addr_t offset;
811 bus_size_t size;
812 int flags;
813 vaddr_t vaddr;
814 bus_space_handle_t *hp;
815 {
816 struct psycho_pbm *pp = t->cookie;
817 struct psycho_softc *sc = pp->pp_sc;
818 int i, ss;
819
820 DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, (unsigned long long)offset, (unsigned long long)size, flags,
821 (void *)vaddr));
822
823 ss = get_childspace(t->type);
824 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
825
826 for (i = 0; i < pp->pp_nrange; i++) {
827 bus_addr_t paddr;
828
829 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
830 continue;
831
832 paddr = pp->pp_range[i].phys_lo + offset;
833 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
834 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
835 (long)ss, (long)offset,
836 (unsigned long long)paddr));
837 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
838 size, flags, vaddr, hp));
839 }
840 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
841 return (EINVAL);
842 }
843
844 static int
845 psycho_bus_mmap(t, btype, paddr, flags, hp)
846 bus_space_tag_t t;
847 bus_type_t btype;
848 bus_addr_t paddr;
849 int flags;
850 bus_space_handle_t *hp;
851 {
852 bus_addr_t offset = paddr;
853 struct psycho_pbm *pp = t->cookie;
854 struct psycho_softc *sc = pp->pp_sc;
855 int i, ss;
856
857 ss = get_childspace(t->type);
858
859 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, (unsigned long long)paddr));
860
861 for (i = 0; i < pp->pp_nrange; i++) {
862 bus_addr_t paddr;
863
864 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
865 continue;
866
867 paddr = pp->pp_range[i].phys_lo + offset;
868 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
869 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
870 (long)ss, (long)offset,
871 (unsigned long long)paddr));
872 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
873 flags, hp));
874 }
875
876 return (-1);
877 }
878
879
880 /*
881 * install an interrupt handler for a PCI device
882 */
883 void *
884 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
885 bus_space_tag_t t;
886 int ihandle;
887 int level;
888 int flags;
889 int (*handler) __P((void *));
890 void *arg;
891 {
892 struct psycho_pbm *pp = t->cookie;
893 struct psycho_softc *sc = pp->pp_sc;
894 struct intrhand *ih;
895 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
896 int64_t intrmap = 0;
897 int ino;
898 long vec = INTVEC(ihandle);
899
900 ih = (struct intrhand *)
901 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
902 if (ih == NULL)
903 return (NULL);
904
905 /*
906 * Hunt through all the interrupt mapping regs to look for our
907 * interrupt vector.
908 *
909 * XXX We only compare INOs rather than IGNs since the firmware may
910 * not provide the IGN and the IGN is constant for all device on that
911 * PCI controller. This could cause problems for the FFB/external
912 * interrupt which has a full vector that can be set arbitrarily.
913 */
914
915
916 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
917 ino = INTINO(vec);
918 DPRINTF(PDB_INTR, (" ino %x", ino));
919
920 /* If the device didn't ask for an IPL, use the one encoded. */
921 if (level == IPL_NONE) level = INTLEV(vec);
922 /* If it still has no level, print a warning and assign IPL 2 */
923 if (level == IPL_NONE) {
924 printf("ERROR: no IPL, setting IPL 2.\n");
925 level = 2;
926 }
927
928 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
929
930 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
931 (long)ino, intrlev[ino]));
932
933 /* Hunt thru obio first */
934 for (intrmapptr = &sc->sc_regs->scsi_int_map,
935 intrclrptr = &sc->sc_regs->scsi_clr_int;
936 intrmapptr <= &sc->sc_regs->ffb1_int_map;
937 intrmapptr++, intrclrptr++) {
938 if (INTINO(*intrmapptr) == ino)
939 goto found;
940 }
941
942 /* Now do PCI interrupts */
943 for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
944 intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
945 intrmapptr <= &sc->sc_regs->pcib_slot3_int;
946 intrmapptr++, intrclrptr += 4) {
947 if (((*intrmapptr ^ vec) & 0x3c) == 0) {
948 intrclrptr += vec & 0x3;
949 goto found;
950 }
951 }
952 printf("Cannot find interrupt vector %lx\n", vec);
953 return (NULL);
954
955 found:
956 /* Register the map and clear intr registers */
957 ih->ih_map = intrmapptr;
958 ih->ih_clr = intrclrptr;
959 }
960 #ifdef NOT_DEBUG
961 if (psycho_debug & PDB_INTR) {
962 long i;
963
964 for (i = 0; i < 500000000; i++)
965 continue;
966 }
967 #endif
968
969 ih->ih_fun = handler;
970 ih->ih_arg = arg;
971 ih->ih_pil = level;
972 ih->ih_number = ino | sc->sc_ign;
973
974 DPRINTF(PDB_INTR, (
975 "; installing handler %p arg %p with ino %u pil %u\n",
976 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
977
978 intr_establish(ih->ih_pil, ih);
979
980 /*
981 * Enable the interrupt now we have the handler installed.
982 * Read the current value as we can't change it besides the
983 * valid bit so so make sure only this bit is changed.
984 *
985 * XXXX --- we really should use bus_space for this.
986 */
987 if (intrmapptr) {
988 intrmap = *intrmapptr;
989 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
990 (unsigned long long)intrmap));
991
992 /* Enable the interrupt */
993 intrmap |= INTMAP_V;
994 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
995 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
996 (unsigned long long)intrmap));
997 *intrmapptr = intrmap;
998 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
999 (unsigned long long)(intrmap = *intrmapptr)));
1000 }
1001 return (ih);
1002 }
1003
1004 /*
1005 * hooks into the iommu dvma calls.
1006 */
1007 int
1008 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1009 bus_dma_tag_t t;
1010 bus_dmamap_t map;
1011 void *buf;
1012 bus_size_t buflen;
1013 struct proc *p;
1014 int flags;
1015 {
1016 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1017 struct psycho_softc *sc = pp->pp_sc;
1018
1019 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1020 }
1021
1022 void
1023 psycho_dmamap_unload(t, map)
1024 bus_dma_tag_t t;
1025 bus_dmamap_t map;
1026 {
1027 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1028 struct psycho_softc *sc = pp->pp_sc;
1029
1030 iommu_dvmamap_unload(t, sc->sc_is, map);
1031 }
1032
1033 int
1034 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1035 bus_dma_tag_t t;
1036 bus_dmamap_t map;
1037 bus_dma_segment_t *segs;
1038 int nsegs;
1039 bus_size_t size;
1040 int flags;
1041 {
1042 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1043 struct psycho_softc *sc = pp->pp_sc;
1044
1045 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1046 }
1047
1048 void
1049 psycho_dmamap_sync(t, map, offset, len, ops)
1050 bus_dma_tag_t t;
1051 bus_dmamap_t map;
1052 bus_addr_t offset;
1053 bus_size_t len;
1054 int ops;
1055 {
1056 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1057 struct psycho_softc *sc = pp->pp_sc;
1058
1059 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1060 /* Flush the CPU then the IOMMU */
1061 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1062 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1063 }
1064 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1065 /* Flush the IOMMU then the CPU */
1066 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1067 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1068 }
1069
1070 }
1071
1072 int
1073 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1074 bus_dma_tag_t t;
1075 bus_size_t size;
1076 bus_size_t alignment;
1077 bus_size_t boundary;
1078 bus_dma_segment_t *segs;
1079 int nsegs;
1080 int *rsegs;
1081 int flags;
1082 {
1083 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1084 struct psycho_softc *sc = pp->pp_sc;
1085
1086 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1087 segs, nsegs, rsegs, flags));
1088 }
1089
1090 void
1091 psycho_dmamem_free(t, segs, nsegs)
1092 bus_dma_tag_t t;
1093 bus_dma_segment_t *segs;
1094 int nsegs;
1095 {
1096 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1097 struct psycho_softc *sc = pp->pp_sc;
1098
1099 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1100 }
1101
1102 int
1103 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1104 bus_dma_tag_t t;
1105 bus_dma_segment_t *segs;
1106 int nsegs;
1107 size_t size;
1108 caddr_t *kvap;
1109 int flags;
1110 {
1111 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1112 struct psycho_softc *sc = pp->pp_sc;
1113
1114 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1115 }
1116
1117 void
1118 psycho_dmamem_unmap(t, kva, size)
1119 bus_dma_tag_t t;
1120 caddr_t kva;
1121 size_t size;
1122 {
1123 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1124 struct psycho_softc *sc = pp->pp_sc;
1125
1126 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1127 }
1128