psycho.c revision 1.12 1 /* $NetBSD: psycho.c,v 1.12 2000/05/24 20:27:52 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 * PCI support for UltraSPARC `psycho'
35 */
36
37 #undef DEBUG
38 #define DEBUG
39
40 #ifdef DEBUG
41 #define PDB_PROM 0x01
42 #define PDB_IOMMU 0x02
43 #define PDB_BUSMAP 0x04
44 #define PDB_BUSDMA 0x08
45 #define PDB_INTR 0x10
46 int psycho_debug = 0x0;
47 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0)
48 #else
49 #define DPRINTF(l, s)
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/device.h>
54 #include <sys/errno.h>
55 #include <sys/extent.h>
56 #include <sys/malloc.h>
57 #include <sys/systm.h>
58 #include <sys/time.h>
59
60 #include <vm/vm.h>
61 #include <vm/vm_kern.h>
62
63 #define _SPARC_BUS_DMA_PRIVATE
64 #include <machine/bus.h>
65 #include <machine/autoconf.h>
66
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/pcireg.h>
69
70 #include <sparc64/dev/iommureg.h>
71 #include <sparc64/dev/iommuvar.h>
72 #include <sparc64/dev/psychoreg.h>
73 #include <sparc64/dev/psychovar.h>
74 #include <sparc64/sparc64/cache.h>
75
76 #include "ioconf.h"
77
78 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
79 pci_chipset_tag_t));
80 static void psycho_get_bus_range __P((int, int *));
81 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
82 static void psycho_get_registers __P((int, struct psycho_registers **, int *));
83 static void psycho_get_intmap __P((int, struct psycho_interrupt_map **, int *));
84 static void psycho_get_intmapmask __P((int, struct psycho_interrupt_map_mask *));
85
86 /* IOMMU support */
87 static void psycho_iommu_init __P((struct psycho_softc *));
88
89 /*
90 * bus space and bus dma support for UltraSPARC `psycho'. note that most
91 * of the bus dma support is provided by the iommu dvma controller.
92 */
93 static int psycho_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
94 int, bus_space_handle_t *));
95 static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
96 bus_size_t, int, vaddr_t,
97 bus_space_handle_t *));
98 static void *psycho_intr_establish __P((bus_space_tag_t, int, int,
99 int (*) __P((void *)), void *));
100
101 static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
102 bus_size_t, struct proc *, int));
103 static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
104 static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
105 bus_dma_segment_t *, int, bus_size_t, int));
106 static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
107 bus_size_t, int));
108 int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
109 bus_dma_segment_t *, int, int *, int));
110 void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
111 int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
112 caddr_t *, int));
113 void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
114
115 /* base pci_chipset */
116 extern struct sparc_pci_chipset _sparc_pci_chipset;
117
118 /*
119 * autoconfiguration
120 */
121 static int psycho_match __P((struct device *, struct cfdata *, void *));
122 static void psycho_attach __P((struct device *, struct device *, void *));
123 static int psycho_print __P((void *aux, const char *p));
124
125 static void sabre_init __P((struct psycho_softc *, struct pcibus_attach_args *));
126 static void psycho_init __P((struct psycho_softc *, struct pcibus_attach_args *));
127
128 struct cfattach psycho_ca = {
129 sizeof(struct psycho_softc), psycho_match, psycho_attach
130 };
131
132 /*
133 * "sabre" is the UltraSPARC IIi onboard PCI interface, normally connected to
134 * an APB (advanced PCI bridge), which was designed specifically for the IIi.
135 * the APB appears as two "simba"'s underneath the sabre. real devices
136 * typically appear on the "simba"'s only.
137 *
138 * a pair of "psycho"s sit on the mainbus and have real devices attached to
139 * them. they implemented in the U2P (UPA to PCI). these two devices share
140 * register space and as such need to be configured together, even though the
141 * autoconfiguration will attach them separately.
142 *
143 * each of these appears as two usable PCI busses, though the sabre itself
144 * takes pci0 in this case, leaving real devices on pci1 and pci2. there can
145 * be multiple pairs of psycho's, however, in multi-board machines.
146 */
147 #define ROM_PCI_NAME "pci"
148 #define ROM_SABRE_MODEL "SUNW,sabre"
149 #define ROM_SIMBA_MODEL "SUNW,simba"
150 #define ROM_PSYCHO_MODEL "SUNW,psycho"
151
152 static int
153 psycho_match(parent, match, aux)
154 struct device *parent;
155 struct cfdata *match;
156 void *aux;
157 {
158 struct mainbus_attach_args *ma = aux;
159 char *model = getpropstring(ma->ma_node, "model");
160
161 /* match on a name of "pci" and a sabre or a psycho */
162 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0 &&
163 (strcmp(model, ROM_SABRE_MODEL) == 0 ||
164 strcmp(model, ROM_PSYCHO_MODEL) == 0))
165 return (1);
166
167 return (0);
168 }
169
170 static void
171 psycho_attach(parent, self, aux)
172 struct device *parent, *self;
173 void *aux;
174 {
175 struct psycho_softc *sc = (struct psycho_softc *)self;
176 struct pcibus_attach_args pba;
177 struct mainbus_attach_args *ma = aux;
178 char *model = getpropstring(ma->ma_node, "model");
179
180 printf("\n");
181
182 sc->sc_node = ma->ma_node;
183 sc->sc_bustag = ma->ma_bustag;
184 sc->sc_dmatag = ma->ma_dmatag;
185
186 /*
187 * pull in all the information about the psycho as we can.
188 */
189
190 /*
191 * XXX use the prom address for the psycho registers? we do so far.
192 */
193 sc->sc_regs = (struct psychoreg *)(u_long)ma->ma_address[0];
194 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
195
196 /*
197 * call the model-specific initialisation routine.
198 */
199 if (strcmp(model, ROM_SABRE_MODEL) == 0)
200 sabre_init(sc, &pba);
201 else if (strcmp(model, ROM_PSYCHO_MODEL) == 0)
202 psycho_init(sc, &pba);
203 #ifdef DIAGNOSTIC
204 else
205 panic("psycho_attach: unknown model %s?", model);
206 #endif
207
208 /*
209 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
210 */
211 pba.pba_busname = "pci";
212 pba.pba_flags = sc->sc_psycho_this->pp_flags;
213 pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
214 pba.pba_iot = sc->sc_psycho_this->pp_iot;
215 pba.pba_memt = sc->sc_psycho_this->pp_memt;
216
217 config_found(self, &pba, psycho_print);
218 }
219
220 static int
221 psycho_print(aux, p)
222 void *aux;
223 const char *p;
224 {
225
226 if (p == NULL)
227 return (UNCONF);
228 return (QUIET);
229 }
230
231 /*
232 * SUNW,sabre initialisation ..
233 * - get the sabre's ranges. this are used for both simba's.
234 * - find the two SUNW,simba's underneath (a and b)
235 * - work out which simba is which via the bus-range property
236 * - get each simba's interrupt-map and interrupt-map-mask.
237 * - turn on the iommu
238 */
239 static void
240 sabre_init(sc, pba)
241 struct psycho_softc *sc;
242 struct pcibus_attach_args *pba;
243 {
244 struct psycho_pbm *pp;
245 bus_space_handle_t bh;
246 u_int64_t csr;
247 int node;
248 int sabre_br[2], simba_br[2];
249
250 /* who? said a voice, incredulous */
251 sc->sc_mode = PSYCHO_MODE_SABRE;
252 printf("sabre: ");
253
254 /* setup the PCI control register; there is only one for the sabre */
255 csr = bus_space_read_8(sc->sc_bustag, (bus_space_handle_t)(u_long)
256 &sc->sc_regs->psy_pcictl[0].pci_csr, 0);
257 csr |= PCICTL_MRLM |
258 PCICTL_ARB_PARK |
259 PCICTL_ERRINTEN |
260 PCICTL_4ENABLE;
261 csr &= ~(PCICTL_SERR |
262 PCICTL_CPU_PRIO |
263 PCICTL_ARB_PRIO |
264 PCICTL_RTRYWAIT);
265 bus_space_write_8(sc->sc_bustag, &sc->sc_regs->psy_pcictl[0].pci_csr,
266 0, csr);
267
268 /* allocate a pair of psycho_pbm's for our simba's */
269 sc->sc_sabre = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
270 sc->sc_simba_a = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
271 sc->sc_simba_b = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
272 if (sc->sc_sabre == NULL || sc->sc_simba_a == NULL ||
273 sc->sc_simba_b == NULL)
274 panic("could not allocate simba pbm's");
275
276 memset(sc->sc_sabre, 0, sizeof *pp);
277 memset(sc->sc_simba_a, 0, sizeof *pp);
278 memset(sc->sc_simba_b, 0, sizeof *pp);
279
280 /* grab the sabre ranges; use them for both simba's */
281 psycho_get_ranges(sc->sc_node, &sc->sc_sabre->pp_range,
282 &sc->sc_sabre->pp_nrange);
283 sc->sc_simba_b->pp_range = sc->sc_simba_a->pp_range =
284 sc->sc_sabre->pp_range;
285 sc->sc_simba_b->pp_nrange = sc->sc_simba_a->pp_nrange =
286 sc->sc_sabre->pp_nrange;
287
288 /* get the bus-range for the sabre. we expect 0..2 */
289 psycho_get_bus_range(sc->sc_node, sabre_br);
290
291 pba->pba_bus = sabre_br[0];
292
293 printf("bus range %u to %u", sabre_br[0], sabre_br[1]);
294
295 for (node = firstchild(sc->sc_node); node; node = nextsibling(node)) {
296 char *name = getpropstring(node, "name");
297 char *model, who;
298 struct psycho_registers *regs;
299 int nregs, fn;
300
301 if (strcmp(name, ROM_PCI_NAME) != 0)
302 continue;
303
304 model = getpropstring(node, "model");
305 if (strcmp(model, ROM_SIMBA_MODEL) != 0)
306 continue;
307
308 psycho_get_bus_range(node, simba_br);
309 psycho_get_registers(node, ®s, &nregs);
310
311 fn = TAG2FN(regs->phys_hi);
312 switch (fn) {
313 case 0:
314 pp = sc->sc_simba_a;
315 who = 'a';
316 pp->pp_regs = regs;
317 pp->pp_nregs = nregs;
318 break;
319 case 1:
320 pp = sc->sc_simba_b;
321 who = 'b';
322 pp->pp_regs = regs;
323 pp->pp_nregs = nregs;
324 break;
325 default:
326 panic("illegal simba funcion %d\n");
327 }
328 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
329 /* link us in .. */
330 pp->pp_sc = sc;
331
332 printf("; simba %c, PCI bus %d", who, simba_br[0]);
333
334 /* grab the simba registers, interrupt map and map mask */
335 psycho_get_intmap(node, &pp->pp_intmap, &pp->pp_nintmap);
336 psycho_get_intmapmask(node, &pp->pp_intmapmask);
337
338 /* allocate our tags */
339 pp->pp_memt = psycho_alloc_mem_tag(pp);
340 pp->pp_iot = psycho_alloc_io_tag(pp);
341 pp->pp_dmat = psycho_alloc_dma_tag(pp);
342 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
343 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
344
345 /* allocate a chipset for this */
346 pp->pp_pc = psycho_alloc_chipset(pp, node, &_sparc_pci_chipset);
347 pp->pp_pc->busno = pp->pp_bus = simba_br[0];
348 }
349
350 /* setup the rest of the sabre pbm */
351 pp = sc->sc_sabre;
352 pp->pp_sc = sc;
353 pp->pp_memt = sc->sc_psycho_this->pp_memt;
354 pp->pp_iot = sc->sc_psycho_this->pp_iot;
355 pp->pp_dmat = sc->sc_psycho_this->pp_dmat;
356 pp->pp_flags = sc->sc_psycho_this->pp_flags;
357 pp->pp_intmap = NULL;
358 pp->pp_regs = NULL;
359 pp->pp_pcictl = sc->sc_psycho_this->pp_pcictl;
360 pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
361 sc->sc_psycho_this->pp_pc);
362
363 printf("\n");
364
365 /* and finally start up the IOMMU ... */
366 psycho_iommu_init(sc);
367
368 /*
369 * get us a config space tag, and punch in the physical address
370 * of the PCI configuration space. note that we use unmapped
371 * access to PCI configuration space, relying on the bus space
372 * macros to provide the proper ASI based on the bus tag.
373 */
374 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_simba_a);
375 if (bus_space_map2(sc->sc_bustag,
376 PCI_CONFIG_BUS_SPACE,
377 sc->sc_basepaddr + 0x01000000,
378 0x0100000,
379 0,
380 0,
381 &bh))
382 panic("could not map sabre PCI configuration space");
383 sc->sc_configaddr = bh;
384 }
385
386 /*
387 * SUNW,psycho initialisation ..
388 * - XXX what do we do here?
389 *
390 * i think that an attaching psycho should here find it's partner psycho
391 * and if they haven't been attached yet, allocate both psycho_pbm's and
392 * fill them both in here, and when the partner attaches, there is little
393 * to do... perhaps keep a static array of what psycho have been found so
394 * far (or perhaps those that have not yet been finished). .mrg.
395 * note that the partner can be found via matching `ranges' properties.
396 */
397 static void
398 psycho_init(sc, pba)
399 struct psycho_softc *sc;
400 struct pcibus_attach_args *pba;
401 {
402 struct psycho_softc *osc = NULL;
403 struct psycho_pbm *pp;
404 bus_space_handle_t bh;
405 u_int64_t csr;
406 int psycho_br[2], n;
407 char who;
408
409 printf("psycho: ");
410
411 /*
412 * OK, so the deal here is:
413 * - given our base register address, search our sibling
414 * devices for a match.
415 * - if we find a match, we are attaching an almost
416 * already setup PCI bus, the partner already done.
417 * - otherwise, we are doing the hard slog.
418 */
419 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
420
421 osc = (struct psycho_softc *)&psycho_cd.cd_devs[n];
422
423 /*
424 * I am not myself.
425 */
426 if (osc == sc || osc->sc_regs != sc->sc_regs)
427 continue;
428
429 /*
430 * OK, so we found a matching regs that wasn't me,
431 * so that means my IOMMU is setup.
432 */
433
434 /* who? said a voice, incredulous */
435 sc->sc_mode = PSYCHO_MODE_PSYCHO_B; /* XXX */
436 who = 'b';
437 break;
438 }
439
440 if (sc->sc_mode != PSYCHO_MODE_PSYCHO_B) {
441 sc->sc_mode = PSYCHO_MODE_PSYCHO_A; /* XXX */
442 who = 'a';
443 }
444
445 /* Oh, dear. OK, lets get started */
446
447 /* XXX: check this is OK for real psycho */
448 /* setup the PCI control register */
449 csr = bus_space_read_8(sc->sc_bustag, (bus_space_handle_t)(u_long)&sc->sc_regs->psy_pcictl[0].pci_csr, 0);
450 csr |= PCICTL_MRLM |
451 PCICTL_ARB_PARK |
452 PCICTL_ERRINTEN |
453 PCICTL_4ENABLE;
454 csr &= ~(PCICTL_SERR |
455 PCICTL_CPU_PRIO |
456 PCICTL_ARB_PRIO |
457 PCICTL_RTRYWAIT);
458 bus_space_write_8(sc->sc_bustag, &sc->sc_regs->psy_pcictl[0].pci_csr, 0, csr);
459
460 /* allocate our psycho_pbm */
461 sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
462 if (sc->sc_psycho_this == NULL)
463 panic("could not allocate psycho pbm");
464 if (osc) {
465 sc->sc_psycho_other = osc->sc_psycho_this;
466 osc->sc_psycho_other = sc->sc_psycho_this;
467 }
468
469 memset(sc->sc_psycho_this, 0, sizeof *pp);
470
471 /* grab the psycho ranges */
472 psycho_get_ranges(sc->sc_node, &sc->sc_psycho_this->pp_range,
473 &sc->sc_psycho_this->pp_nrange);
474
475 /* get the bus-range for the psycho */
476 psycho_get_bus_range(sc->sc_node, psycho_br);
477
478 pba->pba_bus = psycho_br[0];
479
480 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
481 printf("; simba %c, PCI bus %d", who, psycho_br[0]);
482
483 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
484
485 /* grab the psycho registers, interrupt map and map mask */
486 psycho_get_registers(sc->sc_node, &pp->pp_regs, &pp->pp_nregs);
487 psycho_get_intmap(sc->sc_node, &pp->pp_intmap, &pp->pp_nintmap);
488 psycho_get_intmapmask(sc->sc_node, &pp->pp_intmapmask);
489
490 /* allocate our tags */
491 pp->pp_memt = psycho_alloc_mem_tag(pp);
492 pp->pp_iot = psycho_alloc_io_tag(pp);
493 pp->pp_dmat = psycho_alloc_dma_tag(pp);
494 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
495 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
496
497 /* allocate a chipset for this */
498 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
499
500 /* setup the rest of the psycho pbm */
501 pp->pp_sc = sc;
502 pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
503 sc->sc_psycho_this->pp_pc);
504
505 printf("\n");
506
507 /*
508 * and finally, if we a a psycho A, start up the IOMMU and
509 * get us a config space tag, and punch in the physical address
510 * of the PCI configuration space. note that we use unmapped
511 * access to PCI configuration space, relying on the bus space
512 * macros to provide the proper ASI based on the bus tag.
513 */
514 if (sc->sc_mode == PSYCHO_MODE_PSYCHO_A) {
515 psycho_iommu_init(sc);
516
517 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
518 if (bus_space_map2(sc->sc_bustag,
519 PCI_CONFIG_BUS_SPACE,
520 sc->sc_basepaddr + 0x01000000,
521 0x0100000,
522 0,
523 0,
524 &bh))
525 panic("could not map sabre PCI configuration space");
526 sc->sc_configaddr = (paddr_t)bh;
527 } else {
528 /* for psycho B, we just copy the config tag and address */
529 sc->sc_configtag = osc->sc_configtag;
530 sc->sc_configaddr = osc->sc_configaddr;
531 }
532 }
533
534 /*
535 * PCI bus support
536 */
537
538 /*
539 * allocate a PCI chipset tag and set it's cookie.
540 */
541 static pci_chipset_tag_t
542 psycho_alloc_chipset(pp, node, pc)
543 struct psycho_pbm *pp;
544 int node;
545 pci_chipset_tag_t pc;
546 {
547 pci_chipset_tag_t npc;
548
549 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
550 if (npc == NULL)
551 panic("could not allocate pci_chipset_tag_t");
552 memcpy(npc, pc, sizeof *pc);
553 npc->cookie = pp;
554 npc->node = node;
555
556 return (npc);
557 }
558
559 /*
560 * grovel the OBP for various psycho properties
561 */
562 static void
563 psycho_get_bus_range(node, brp)
564 int node;
565 int *brp;
566 {
567 int n;
568
569 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
570 panic("could not get psycho bus-range");
571 if (n != 2)
572 panic("broken psycho bus-range");
573 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
574 }
575
576 static void
577 psycho_get_ranges(node, rp, np)
578 int node;
579 struct psycho_ranges **rp;
580 int *np;
581 {
582
583 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
584 panic("could not get psycho ranges");
585 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
586 }
587
588 static void
589 psycho_get_registers(node, rp, np)
590 int node;
591 struct psycho_registers **rp;
592 int *np;
593 {
594
595 if (getprop(node, "reg", sizeof(**rp), np, (void **)rp))
596 panic("could not get psycho registers");
597 DPRINTF(PDB_PROM, ("psycho debug: got `reg' for node %08x: %d entries\n", node, *np));
598 }
599
600 static void
601 psycho_get_intmap(node, imp, np)
602 int node;
603 struct psycho_interrupt_map **imp;
604 int *np;
605 {
606
607 if (getprop(node, "interrupt-map", sizeof(**imp), np, (void **)imp))
608 panic("could not get psycho interrupt-map");
609 DPRINTF(PDB_PROM, ("psycho debug: got `interupt-map' for node %08x\n", node));
610 }
611
612 static void
613 psycho_get_intmapmask(node, immp)
614 int node;
615 struct psycho_interrupt_map_mask *immp;
616 {
617 int n;
618
619 if (getprop(node, "interrupt-map-mask", sizeof(*immp), &n,
620 (void **)&immp))
621 panic("could not get psycho interrupt-map-mask");
622 if (n != 1)
623 panic("broken psycho interrupt-map-mask");
624 DPRINTF(PDB_PROM, ("psycho debug: got `interrupt-map-mask' for node %08x\n", node));
625 }
626
627 /*
628 * initialise the IOMMU..
629 */
630 void
631 psycho_iommu_init(sc)
632 struct psycho_softc *sc;
633 {
634 char *name;
635
636 /* punch in our copies */
637 sc->sc_is.is_bustag = sc->sc_bustag;
638 sc->sc_is.is_iommu = &sc->sc_regs->psy_iommu;
639 /* IIi does not have streaming buffers */
640 if (sc->sc_mode != PSYCHO_MODE_SABRE)
641 sc->sc_is.is_sb = &sc->sc_regs->psy_iommu_strbuf;
642 else
643 sc->sc_is.is_sb = 0;
644
645 /* give us a nice name.. */
646 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
647 if (name == 0)
648 panic("couldn't malloc iommu name");
649 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
650
651 /* XXX XXX XXX FIX ME tsbsize XXX XXX XXX */
652 iommu_init(name, &sc->sc_is, 0);
653 }
654
655 /*
656 * below here is bus space and bus dma support
657 */
658 bus_space_tag_t
659 psycho_alloc_bus_tag(pp, type)
660 struct psycho_pbm *pp;
661 int type;
662 {
663 struct psycho_softc *sc = pp->pp_sc;
664 bus_space_tag_t bt;
665
666 bt = (bus_space_tag_t)
667 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
668 if (bt == NULL)
669 panic("could not allocate psycho bus tag");
670
671 bzero(bt, sizeof *bt);
672 bt->cookie = pp;
673 bt->parent = sc->sc_bustag;
674 bt->type = type;
675 bt->sparc_bus_map = _psycho_bus_map;
676 bt->sparc_bus_mmap = psycho_bus_mmap;
677 bt->sparc_intr_establish = psycho_intr_establish;
678 return (bt);
679 }
680
681 bus_dma_tag_t
682 psycho_alloc_dma_tag(pp)
683 struct psycho_pbm *pp;
684 {
685 struct psycho_softc *sc = pp->pp_sc;
686 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
687
688 dt = (bus_dma_tag_t)
689 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
690 if (dt == NULL)
691 panic("could not allocate psycho dma tag");
692
693 bzero(dt, sizeof *dt);
694 dt->_cookie = pp;
695 dt->_parent = pdt;
696 #define PCOPY(x) dt->x = pdt->x
697 PCOPY(_dmamap_create);
698 PCOPY(_dmamap_destroy);
699 dt->_dmamap_load = psycho_dmamap_load;
700 PCOPY(_dmamap_load_mbuf);
701 PCOPY(_dmamap_load_uio);
702 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
703 dt->_dmamap_unload = psycho_dmamap_unload;
704 dt->_dmamap_sync = psycho_dmamap_sync;
705 dt->_dmamem_alloc = psycho_dmamem_alloc;
706 dt->_dmamem_free = psycho_dmamem_free;
707 dt->_dmamem_map = psycho_dmamem_map;
708 dt->_dmamem_unmap = psycho_dmamem_unmap;
709 PCOPY(_dmamem_mmap);
710 #undef PCOPY
711 return (dt);
712 }
713
714 /*
715 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
716 * PCI physical addresses.
717 */
718
719 static int get_childspace __P((int));
720
721 static int
722 get_childspace(type)
723 int type;
724 {
725 int ss;
726
727 switch (type) {
728 case PCI_CONFIG_BUS_SPACE:
729 ss = 0x00;
730 break;
731 case PCI_IO_BUS_SPACE:
732 ss = 0x01;
733 break;
734 case PCI_MEMORY_BUS_SPACE:
735 ss = 0x02;
736 break;
737 #if 0
738 /* we don't do 64 bit memory space */
739 case PCI_MEMORY64_BUS_SPACE:
740 ss = 0x03;
741 break;
742 #endif
743 default:
744 panic("get_childspace: unknown bus type");
745 }
746
747 return (ss);
748 }
749
750 static int
751 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
752 bus_space_tag_t t;
753 bus_type_t btype;
754 bus_addr_t offset;
755 bus_size_t size;
756 int flags;
757 vaddr_t vaddr;
758 bus_space_handle_t *hp;
759 {
760 struct psycho_pbm *pp = t->cookie;
761 struct psycho_softc *sc = pp->pp_sc;
762 int i, ss;
763
764 DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, offset, size, flags, vaddr));
765
766 ss = get_childspace(t->type);
767 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
768
769
770 for (i = 0; i < pp->pp_nrange; i++) {
771 bus_addr_t paddr;
772
773 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
774 continue;
775
776 paddr = pp->pp_range[i].phys_lo + offset;
777 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
778 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
779 (long)ss, (long)offset, paddr));
780 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
781 size, flags, vaddr, hp));
782 }
783 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
784 return (EINVAL);
785 }
786
787 static int
788 psycho_bus_mmap(t, btype, paddr, flags, hp)
789 bus_space_tag_t t;
790 bus_type_t btype;
791 bus_addr_t paddr;
792 int flags;
793 bus_space_handle_t *hp;
794 {
795 bus_addr_t offset = paddr;
796 struct psycho_pbm *pp = t->cookie;
797 struct psycho_softc *sc = pp->pp_sc;
798 int i, ss;
799
800 ss = get_childspace(t->type);
801
802 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, paddr));
803
804 for (i = 0; i < pp->pp_nrange; i++) {
805 bus_addr_t paddr;
806
807 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
808 continue;
809
810 paddr = pp->pp_range[i].phys_lo + offset;
811 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
812 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
813 (long)ss, (long)offset, paddr));
814 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
815 flags, hp));
816 }
817
818 return (-1);
819 }
820
821 /*
822 * interrupt mapping. this tells what sparc ipl any given ino runs at.
823 */
824 static int pci_ino_to_ipl_table[] = {
825 0, 0, 0, 0, /* PCI A, Slot 0, INTA#/B#/C#/D# */
826 0, 0, 0, 0, /* PCI A, Slot 1, INTA#/B#/C#/D# */
827 0, 0, 0, 0, /* PCI A, Slot 2, INTA#/B#/C#/D# (unavailable) */
828 0, 0, 0, 0, /* PCI A, Slot 3, INTA#/B#/C#/D# (unavailable) */
829 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
830 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
831 0, 0, 0, 0, /* PCI B, Slot 2, INTA#/B#/C#/D# */
832 0, 0, 0, 0, /* PCI B, Slot 3, INTA#/B#/C#/D# */
833 4, /* SCSI */
834 6, /* Ethernet */
835 3, /* Parallel */
836 9, /* Audio Record */
837 9, /* Audio Playback */
838 14, /* Power Fail */
839 4, /* Keyboard/Mouse/Serial */
840 8, /* Floppy */
841 14, /* Thermal Warning */
842 12, /* Keyboard */
843 12, /* Mouse */
844 12, /* Serial */
845 0, /* Reserved */
846 0, /* Reserved */
847 14, /* Uncorrectable ECC error */
848 14, /* Correctable ECC error */
849 14, /* PCI A bus error */
850 14, /* PCI B bus error */
851 14, /* power management */
852 };
853
854 #ifdef NOT_DEBUG
855 static struct psycho_pbm *ppbm;
856 #endif
857
858 int
859 psycho_intr_map(tag, pin, line, ihp)
860 pcitag_t tag;
861 int pin;
862 int line;
863 pci_intr_handle_t *ihp;
864 {
865
866 if (line < 0 || line > 0x32)
867 panic("psycho_intr_map: line line < 0 || line > 0x32");
868
869 /* UltraSPARC IIi does not use this register, but we have set it */
870 (*ihp) = line;
871 return (0);
872 }
873
874 /*
875 * install an interrupt handler for a PCI device
876 */
877 void *
878 psycho_intr_establish(t, level, flags, handler, arg)
879 bus_space_tag_t t;
880 int level;
881 int flags;
882 int (*handler) __P((void *));
883 void *arg;
884 {
885 struct psycho_pbm *pp = t->cookie;
886 struct psycho_softc *sc = pp->pp_sc;
887 struct intrhand *ih;
888 int ino;
889 long vec = level;
890
891 #ifdef NOT_DEBUG
892 if (!ppbm)
893 ppbm = pp;
894 #endif
895 ih = (struct intrhand *)
896 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
897 if (ih == NULL)
898 return (NULL);
899
900 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: level %x", level));
901 ino = INTINO(vec);
902 DPRINTF(PDB_INTR, (" ino %x", ino));
903 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
904 volatile int64_t *intrmapptr, *intrclrptr;
905 int64_t intrmap = 0;
906 int i;
907
908 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %lx\nHunting for IRQ...\n",
909 (long)ino, intrlev[ino]));
910 if ((ino & INTMAP_OBIO) == 0) {
911 /*
912 * there are only 8 PCI interrupt INO's available
913 */
914 i = INTPCIINOX(vec);
915
916 intrmapptr = &((&sc->sc_regs->pcia_slot0_int)[i]);
917 intrclrptr = &sc->sc_regs->pcia0_clr_int[i<<2];
918
919 DPRINTF(PDB_INTR, ("- turning on PCI intr %d", i));
920 } else {
921 /*
922 * there are INTPCI_MAXOBINO (0x16) OBIO interrupts
923 * available here (i think).
924 */
925 i = INTPCIOBINOX(vec);
926 if (i > INTPCI_MAXOBINO)
927 panic("ino %d", vec);
928
929 intrmapptr = &((&sc->sc_regs->scsi_int_map)[i]);
930 intrclrptr = &((&sc->sc_regs->scsi_clr_int)[i]);
931
932 DPRINTF(PDB_INTR, ("- turning on OBIO intr %d", i));
933 }
934
935 /* Register the map and clear intr registers */
936 ih->ih_map = intrmapptr;
937 ih->ih_clr = intrclrptr;
938
939 /*
940 * Read the current value as we can't change it besides the
941 * valid bit so so make sure only this bit is changed.
942 */
943 intrmap = *intrmapptr;
944 DPRINTF(PDB_INTR, ("; read intrmap = %016qx", intrmap));
945
946 /* Enable the interrupt */
947 intrmap |= INTMAP_V;
948 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
949 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", intrmap));
950 *intrmapptr = intrmap;
951 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
952 (intrmap = *intrmapptr)));
953 }
954 #ifdef NOT_DEBUG
955 if (psycho_debug & PDB_INTR) {
956 long i;
957
958 for (i = 0; i < 500000000; i++)
959 continue;
960 }
961 #endif
962
963 ih->ih_fun = handler;
964 ih->ih_arg = arg;
965 ih->ih_number = ino | 0x7c0;
966 ih->ih_pil = pci_ino_to_ipl_table[ino];
967 DPRINTF(PDB_INTR, ("; installing handler %p with ino %u pil %u\n",
968 handler, (u_int)ino, (u_int)ih->ih_pil));
969 intr_establish(ih->ih_pil, ih);
970 return (ih);
971 }
972
973 /*
974 * hooks into the iommu dvma calls.
975 */
976 int
977 psycho_dmamap_load(t, map, buf, buflen, p, flags)
978 bus_dma_tag_t t;
979 bus_dmamap_t map;
980 void *buf;
981 bus_size_t buflen;
982 struct proc *p;
983 int flags;
984 {
985 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
986 struct psycho_softc *sc = pp->pp_sc;
987
988 return (iommu_dvmamap_load(t, &sc->sc_is, map, buf, buflen, p, flags));
989 }
990
991 void
992 psycho_dmamap_unload(t, map)
993 bus_dma_tag_t t;
994 bus_dmamap_t map;
995 {
996 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
997 struct psycho_softc *sc = pp->pp_sc;
998
999 iommu_dvmamap_unload(t, &sc->sc_is, map);
1000 }
1001
1002 int
1003 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1004 bus_dma_tag_t t;
1005 bus_dmamap_t map;
1006 bus_dma_segment_t *segs;
1007 int nsegs;
1008 bus_size_t size;
1009 int flags;
1010 {
1011 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1012 struct psycho_softc *sc = pp->pp_sc;
1013
1014 return (iommu_dvmamap_load_raw(t, &sc->sc_is, map, segs, nsegs, size, flags));
1015 }
1016
1017 void
1018 psycho_dmamap_sync(t, map, offset, len, ops)
1019 bus_dma_tag_t t;
1020 bus_dmamap_t map;
1021 bus_addr_t offset;
1022 bus_size_t len;
1023 int ops;
1024 {
1025 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1026 struct psycho_softc *sc = pp->pp_sc;
1027
1028 iommu_dvmamap_sync(t, &sc->sc_is, map, offset, len, ops);
1029 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1030 }
1031
1032 int
1033 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1034 bus_dma_tag_t t;
1035 bus_size_t size;
1036 bus_size_t alignment;
1037 bus_size_t boundary;
1038 bus_dma_segment_t *segs;
1039 int nsegs;
1040 int *rsegs;
1041 int flags;
1042 {
1043 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1044 struct psycho_softc *sc = pp->pp_sc;
1045
1046 return (iommu_dvmamem_alloc(t, &sc->sc_is, size, alignment, boundary,
1047 segs, nsegs, rsegs, flags));
1048 }
1049
1050 void
1051 psycho_dmamem_free(t, segs, nsegs)
1052 bus_dma_tag_t t;
1053 bus_dma_segment_t *segs;
1054 int nsegs;
1055 {
1056 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1057 struct psycho_softc *sc = pp->pp_sc;
1058
1059 iommu_dvmamem_free(t, &sc->sc_is, segs, nsegs);
1060 }
1061
1062 int
1063 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1064 bus_dma_tag_t t;
1065 bus_dma_segment_t *segs;
1066 int nsegs;
1067 size_t size;
1068 caddr_t *kvap;
1069 int flags;
1070 {
1071 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1072 struct psycho_softc *sc = pp->pp_sc;
1073
1074 return (iommu_dvmamem_map(t, &sc->sc_is, segs, nsegs, size, kvap, flags));
1075 }
1076
1077 void
1078 psycho_dmamem_unmap(t, kva, size)
1079 bus_dma_tag_t t;
1080 caddr_t kva;
1081 size_t size;
1082 {
1083 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1084 struct psycho_softc *sc = pp->pp_sc;
1085
1086 iommu_dvmamem_unmap(t, &sc->sc_is, kva, size);
1087 }
1088
1089 #ifdef NOT_DEBUG
1090 void
1091 psycho_print_intr_state(void)
1092 {
1093 pcitag_t tag;
1094 bus_space_handle_t bh;
1095 u_int64_t data, diag;
1096 struct psycho_softc *sc = ppbm->pp_sc;
1097
1098 if (!ppbm) {
1099 printf("psycho_print_intr_state: no ppbm configured\n");
1100 return;
1101 }
1102 printf("psycho_print_intr_state: ");
1103
1104 bh = sc->sc_basepaddr;
1105 bh = (bus_space_handle_t)(u_long)sc->sc_regs;
1106 diag = bus_space_read_8(sc->sc_configtag, bh, 0xa800);
1107 printf("all PCI diags is %qx\n", diag);
1108 #if 0
1109 for (tag = 0xc00; tag < 0xc40; tag += 0x8) {
1110 data = bus_space_read_8(sc->sc_configtag, bh, tag);
1111
1112 printf(" - PCI slot at %qx reads as %qx", bh + tag, data);
1113 printf(": diag %x\n", (int)(diag & 0xff));
1114 diag >>= 8;
1115 }
1116 #endif
1117
1118 diag = bus_space_read_8(sc->sc_configtag, bh, 0xa808);
1119 printf("\t\tall OBIO diags is %qx\n", diag);
1120 #define START_TAG 0x1000 /* 0x1000 */
1121 #define END_TAG 0x1018 /* 0x1088 */
1122 for (tag = START_TAG; tag < END_TAG; tag += 0x8) {
1123 data = bus_space_read_8(sc->sc_configtag, bh + tag, 0);
1124
1125 printf(" - OBIO slot at %qx reads as %qx", bh + tag, data);
1126 printf(": diag %x\n", (int)(diag & 0x3));
1127 diag >>= 2;
1128 }
1129 }
1130 #endif
1131