psycho.c revision 1.9 1 /* $NetBSD: psycho.c,v 1.9 2000/05/17 02:31:12 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)&sc->sc_regs->psy_pcictl[0].pci_csr, 0);
256 csr |= PCICTL_MRLM |
257 PCICTL_ARB_PARK |
258 PCICTL_ERRINTEN |
259 PCICTL_4ENABLE;
260 csr &= ~(PCICTL_SERR |
261 PCICTL_CPU_PRIO |
262 PCICTL_ARB_PRIO |
263 PCICTL_RTRYWAIT);
264 bus_space_write_8(sc->sc_bustag, &sc->sc_regs->psy_pcictl[0].pci_csr, 0, csr);
265
266 /* allocate a pair of psycho_pbm's for our simba's */
267 sc->sc_sabre = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
268 sc->sc_simba_a = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
269 sc->sc_simba_b = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
270 if (sc->sc_sabre == NULL || sc->sc_simba_a == NULL ||
271 sc->sc_simba_b == NULL)
272 panic("could not allocate simba pbm's");
273
274 memset(sc->sc_sabre, 0, sizeof *pp);
275 memset(sc->sc_simba_a, 0, sizeof *pp);
276 memset(sc->sc_simba_b, 0, sizeof *pp);
277
278 /* grab the sabre ranges; use them for both simba's */
279 psycho_get_ranges(sc->sc_node, &sc->sc_sabre->pp_range,
280 &sc->sc_sabre->pp_nrange);
281 sc->sc_simba_b->pp_range = sc->sc_simba_a->pp_range =
282 sc->sc_sabre->pp_range;
283 sc->sc_simba_b->pp_nrange = sc->sc_simba_a->pp_nrange =
284 sc->sc_sabre->pp_nrange;
285
286 /* get the bus-range for the sabre. we expect 0..2 */
287 psycho_get_bus_range(sc->sc_node, sabre_br);
288
289 pba->pba_bus = sabre_br[0];
290
291 printf("bus range %u to %u", sabre_br[0], sabre_br[1]);
292
293 for (node = firstchild(sc->sc_node); node; node = nextsibling(node)) {
294 char *name = getpropstring(node, "name");
295 char *model, who;
296
297 if (strcmp(name, ROM_PCI_NAME) != 0)
298 continue;
299
300 model = getpropstring(node, "model");
301 if (strcmp(model, ROM_SIMBA_MODEL) != 0)
302 continue;
303
304 psycho_get_bus_range(node, simba_br);
305
306 if (simba_br[0] == 1) { /* PCI B */
307 pp = sc->sc_simba_b;
308 who = 'b';
309 } else { /* PCI A */
310 pp = sc->sc_simba_a;
311 who = 'a';
312 }
313 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
314 /* link us in .. */
315 pp->pp_sc = sc;
316
317 printf("; simba %c, PCI bus %d", who, simba_br[0]);
318
319 /* grab the simba registers, interrupt map and map mask */
320 psycho_get_registers(node, &pp->pp_regs, &pp->pp_nregs);
321 psycho_get_intmap(node, &pp->pp_intmap, &pp->pp_nintmap);
322 psycho_get_intmapmask(node, &pp->pp_intmapmask);
323
324 /* allocate our tags */
325 pp->pp_memt = psycho_alloc_mem_tag(pp);
326 pp->pp_iot = psycho_alloc_io_tag(pp);
327 pp->pp_dmat = psycho_alloc_dma_tag(pp);
328 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
329 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
330
331 /* allocate a chipset for this */
332 pp->pp_pc = psycho_alloc_chipset(pp, node, &_sparc_pci_chipset);
333 }
334
335 /* setup the rest of the sabre pbm */
336 pp = sc->sc_sabre;
337 pp->pp_sc = sc;
338 pp->pp_memt = sc->sc_psycho_this->pp_memt;
339 pp->pp_iot = sc->sc_psycho_this->pp_iot;
340 pp->pp_dmat = sc->sc_psycho_this->pp_dmat;
341 pp->pp_flags = sc->sc_psycho_this->pp_flags;
342 pp->pp_intmap = NULL;
343 pp->pp_regs = NULL;
344 pp->pp_pcictl = sc->sc_psycho_this->pp_pcictl;
345 pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
346 sc->sc_psycho_this->pp_pc);
347
348 printf("\n");
349
350 /* and finally start up the IOMMU ... */
351 psycho_iommu_init(sc);
352
353 /*
354 * get us a config space tag, and punch in the physical address
355 * of the PCI configuration space. note that we use unmapped
356 * access to PCI configuration space, relying on the bus space
357 * macros to provide the proper ASI based on the bus tag.
358 */
359 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_simba_a);
360 if (bus_space_map2(sc->sc_bustag,
361 PCI_CONFIG_BUS_SPACE,
362 sc->sc_basepaddr + 0x01000000,
363 0x0100000,
364 0,
365 0,
366 &bh))
367 panic("could not map sabre PCI configuration space");
368 sc->sc_configaddr = (paddr_t)bh;
369 }
370
371 /*
372 * SUNW,psycho initialisation ..
373 * - XXX what do we do here?
374 *
375 * i think that an attaching psycho should here find it's partner psycho
376 * and if they haven't been attached yet, allocate both psycho_pbm's and
377 * fill them both in here, and when the partner attaches, there is little
378 * to do... perhaps keep a static array of what psycho have been found so
379 * far (or perhaps those that have not yet been finished). .mrg.
380 * note that the partner can be found via matching `ranges' properties.
381 */
382 static void
383 psycho_init(sc, pba)
384 struct psycho_softc *sc;
385 struct pcibus_attach_args *pba;
386 {
387 struct psycho_softc *osc = NULL;
388 struct psycho_pbm *pp;
389 bus_space_handle_t bh;
390 u_int64_t csr;
391 int psycho_br[2], n;
392 char who;
393
394 printf("psycho: ");
395
396 /*
397 * OK, so the deal here is:
398 * - given our base register address, search our sibling
399 * devices for a match.
400 * - if we find a match, we are attaching an almost
401 * already setup PCI bus, the partner already done.
402 * - otherwise, we are doing the hard slog.
403 */
404 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
405
406 osc = (struct psycho_softc *)&psycho_cd.cd_devs[n];
407
408 /*
409 * I am not myself.
410 */
411 if (osc == sc || osc->sc_regs != sc->sc_regs)
412 continue;
413
414 /*
415 * OK, so we found a matching regs that wasn't me,
416 * so that means my IOMMU is setup.
417 */
418
419 /* who? said a voice, incredulous */
420 sc->sc_mode = PSYCHO_MODE_PSYCHO_B; /* XXX */
421 who = 'b';
422 break;
423 }
424
425 if (sc->sc_mode != PSYCHO_MODE_PSYCHO_B) {
426 sc->sc_mode = PSYCHO_MODE_PSYCHO_A; /* XXX */
427 who = 'a';
428 }
429
430 /* Oh, dear. OK, lets get started */
431
432 /* XXX: check this is OK for real psycho */
433 /* setup the PCI control register */
434 csr = bus_space_read_8(sc->sc_bustag, (bus_space_handle_t)(u_long)&sc->sc_regs->psy_pcictl[0].pci_csr, 0);
435 csr |= PCICTL_MRLM |
436 PCICTL_ARB_PARK |
437 PCICTL_ERRINTEN |
438 PCICTL_4ENABLE;
439 csr &= ~(PCICTL_SERR |
440 PCICTL_CPU_PRIO |
441 PCICTL_ARB_PRIO |
442 PCICTL_RTRYWAIT);
443 bus_space_write_8(sc->sc_bustag, &sc->sc_regs->psy_pcictl[0].pci_csr, 0, csr);
444
445 /* allocate our psycho_pbm */
446 sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
447 if (sc->sc_psycho_this == NULL)
448 panic("could not allocate psycho pbm");
449 if (osc) {
450 sc->sc_psycho_other = osc->sc_psycho_this;
451 osc->sc_psycho_other = sc->sc_psycho_this;
452 }
453
454 memset(sc->sc_psycho_this, 0, sizeof *pp);
455
456 /* grab the psycho ranges */
457 psycho_get_ranges(sc->sc_node, &sc->sc_psycho_this->pp_range,
458 &sc->sc_psycho_this->pp_nrange);
459
460 /* get the bus-range for the psycho */
461 psycho_get_bus_range(sc->sc_node, psycho_br);
462
463 pba->pba_bus = psycho_br[0];
464
465 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
466 printf("; simba %c, PCI bus %d", who, psycho_br[0]);
467
468 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
469
470 /* grab the psycho registers, interrupt map and map mask */
471 psycho_get_registers(sc->sc_node, &pp->pp_regs, &pp->pp_nregs);
472 psycho_get_intmap(sc->sc_node, &pp->pp_intmap, &pp->pp_nintmap);
473 psycho_get_intmapmask(sc->sc_node, &pp->pp_intmapmask);
474
475 /* allocate our tags */
476 pp->pp_memt = psycho_alloc_mem_tag(pp);
477 pp->pp_iot = psycho_alloc_io_tag(pp);
478 pp->pp_dmat = psycho_alloc_dma_tag(pp);
479 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
480 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
481
482 /* allocate a chipset for this */
483 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
484
485 /* setup the rest of the psycho pbm */
486 pp->pp_sc = sc;
487 pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
488 sc->sc_psycho_this->pp_pc);
489
490 printf("\n");
491
492 /*
493 * and finally, if we a a psycho A, start up the IOMMU and
494 * get us a config space tag, and punch in the physical address
495 * of the PCI configuration space. note that we use unmapped
496 * access to PCI configuration space, relying on the bus space
497 * macros to provide the proper ASI based on the bus tag.
498 */
499 if (sc->sc_mode == PSYCHO_MODE_PSYCHO_A) {
500 psycho_iommu_init(sc);
501
502 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
503 if (bus_space_map2(sc->sc_bustag,
504 PCI_CONFIG_BUS_SPACE,
505 sc->sc_basepaddr + 0x01000000,
506 0x0100000,
507 0,
508 0,
509 &bh))
510 panic("could not map sabre PCI configuration space");
511 sc->sc_configaddr = (paddr_t)bh;
512 } else {
513 /* for psycho B, we just copy the config tag and address */
514 sc->sc_configtag = osc->sc_configtag;
515 sc->sc_configaddr = osc->sc_configaddr;
516 }
517 }
518
519 /*
520 * PCI bus support
521 */
522
523 /*
524 * allocate a PCI chipset tag and set it's cookie.
525 */
526 static pci_chipset_tag_t
527 psycho_alloc_chipset(pp, node, pc)
528 struct psycho_pbm *pp;
529 int node;
530 pci_chipset_tag_t pc;
531 {
532 pci_chipset_tag_t npc;
533
534 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
535 if (npc == NULL)
536 panic("could not allocate pci_chipset_tag_t");
537 memcpy(npc, pc, sizeof *pc);
538 npc->cookie = pp;
539 npc->node = node;
540
541 return (npc);
542 }
543
544 /*
545 * grovel the OBP for various psycho properties
546 */
547 static void
548 psycho_get_bus_range(node, brp)
549 int node;
550 int *brp;
551 {
552 int n;
553
554 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
555 panic("could not get psycho bus-range");
556 if (n != 2)
557 panic("broken psycho bus-range");
558 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
559 }
560
561 static void
562 psycho_get_ranges(node, rp, np)
563 int node;
564 struct psycho_ranges **rp;
565 int *np;
566 {
567
568 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
569 panic("could not get psycho ranges");
570 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
571 }
572
573 static void
574 psycho_get_registers(node, rp, np)
575 int node;
576 struct psycho_registers **rp;
577 int *np;
578 {
579
580 if (getprop(node, "reg", sizeof(**rp), np, (void **)rp))
581 panic("could not get psycho registers");
582 DPRINTF(PDB_PROM, ("psycho debug: got `reg' for node %08x: %d entries\n", node, *np));
583 }
584
585 static void
586 psycho_get_intmap(node, imp, np)
587 int node;
588 struct psycho_interrupt_map **imp;
589 int *np;
590 {
591
592 if (getprop(node, "interrupt-map", sizeof(**imp), np, (void **)imp))
593 panic("could not get psycho interrupt-map");
594 DPRINTF(PDB_PROM, ("psycho debug: got `interupt-map' for node %08x\n", node));
595 }
596
597 static void
598 psycho_get_intmapmask(node, immp)
599 int node;
600 struct psycho_interrupt_map_mask *immp;
601 {
602 int n;
603
604 if (getprop(node, "interrupt-map-mask", sizeof(*immp), &n,
605 (void **)&immp))
606 panic("could not get psycho interrupt-map-mask");
607 if (n != 1)
608 panic("broken psycho interrupt-map-mask");
609 DPRINTF(PDB_PROM, ("psycho debug: got `interrupt-map-mask' for node %08x\n", node));
610 }
611
612 /*
613 * initialise the IOMMU..
614 */
615 void
616 psycho_iommu_init(sc)
617 struct psycho_softc *sc;
618 {
619 char *name;
620
621 /* punch in our copies */
622 sc->sc_is.is_bustag = sc->sc_bustag;
623 sc->sc_is.is_iommu = &sc->sc_regs->psy_iommu;
624 /* IIi does not have streaming buffers */
625 if (sc->sc_mode != PSYCHO_MODE_SABRE)
626 sc->sc_is.is_sb = &sc->sc_regs->psy_iommu_strbuf;
627 else
628 sc->sc_is.is_sb = 0;
629
630 /* give us a nice name.. */
631 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
632 if (name == 0)
633 panic("couldn't malloc iommu name");
634 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
635
636 /* XXX XXX XXX FIX ME tsbsize XXX XXX XXX */
637 iommu_init(name, &sc->sc_is, 0);
638 }
639
640 /*
641 * below here is bus space and bus dma support
642 */
643 bus_space_tag_t
644 psycho_alloc_bus_tag(pp, type)
645 struct psycho_pbm *pp;
646 int type;
647 {
648 struct psycho_softc *sc = pp->pp_sc;
649 bus_space_tag_t bt;
650
651 bt = (bus_space_tag_t)
652 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
653 if (bt == NULL)
654 panic("could not allocate psycho bus tag");
655
656 bzero(bt, sizeof *bt);
657 bt->cookie = pp;
658 bt->parent = sc->sc_bustag;
659 bt->type = type;
660 bt->sparc_bus_map = _psycho_bus_map;
661 bt->sparc_bus_mmap = psycho_bus_mmap;
662 bt->sparc_intr_establish = psycho_intr_establish;
663 return (bt);
664 }
665
666 bus_dma_tag_t
667 psycho_alloc_dma_tag(pp)
668 struct psycho_pbm *pp;
669 {
670 struct psycho_softc *sc = pp->pp_sc;
671 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
672
673 dt = (bus_dma_tag_t)
674 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
675 if (dt == NULL)
676 panic("could not allocate psycho dma tag");
677
678 bzero(dt, sizeof *dt);
679 dt->_cookie = pp;
680 dt->_parent = pdt;
681 #define PCOPY(x) dt->x = pdt->x
682 PCOPY(_dmamap_create);
683 PCOPY(_dmamap_destroy);
684 dt->_dmamap_load = psycho_dmamap_load;
685 PCOPY(_dmamap_load_mbuf);
686 PCOPY(_dmamap_load_uio);
687 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
688 dt->_dmamap_unload = psycho_dmamap_unload;
689 dt->_dmamap_sync = psycho_dmamap_sync;
690 dt->_dmamem_alloc = psycho_dmamem_alloc;
691 dt->_dmamem_free = psycho_dmamem_free;
692 dt->_dmamem_map = psycho_dmamem_map;
693 dt->_dmamem_unmap = psycho_dmamem_unmap;
694 PCOPY(_dmamem_mmap);
695 #undef PCOPY
696 return (dt);
697 }
698
699 /*
700 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
701 * PCI physical addresses.
702 */
703
704 static int get_childspace __P((int));
705
706 static int
707 get_childspace(type)
708 int type;
709 {
710 int ss;
711
712 switch (type) {
713 case PCI_CONFIG_BUS_SPACE:
714 ss = 0x00;
715 break;
716 case PCI_IO_BUS_SPACE:
717 ss = 0x01;
718 break;
719 case PCI_MEMORY_BUS_SPACE:
720 ss = 0x02;
721 break;
722 #if 0
723 /* we don't do 64 bit memory space */
724 case PCI_MEMORY64_BUS_SPACE:
725 ss = 0x03;
726 break;
727 #endif
728 default:
729 panic("get_childspace: unknown bus type");
730 }
731
732 return (ss);
733 }
734
735 static int
736 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
737 bus_space_tag_t t;
738 bus_type_t btype;
739 bus_addr_t offset;
740 bus_size_t size;
741 int flags;
742 vaddr_t vaddr;
743 bus_space_handle_t *hp;
744 {
745 struct psycho_pbm *pp = t->cookie;
746 struct psycho_softc *sc = pp->pp_sc;
747 int i, ss;
748
749 DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, offset, size, flags, vaddr));
750
751 ss = get_childspace(t->type);
752 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
753
754
755 for (i = 0; i < pp->pp_nrange; i++) {
756 bus_addr_t paddr;
757
758 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
759 continue;
760
761 paddr = pp->pp_range[i].phys_lo + offset;
762 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
763 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
764 (long)ss, (long)offset, paddr));
765 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
766 size, flags, vaddr, hp));
767 }
768 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
769 return (EINVAL);
770 }
771
772 static int
773 psycho_bus_mmap(t, btype, paddr, flags, hp)
774 bus_space_tag_t t;
775 bus_type_t btype;
776 bus_addr_t paddr;
777 int flags;
778 bus_space_handle_t *hp;
779 {
780 bus_addr_t offset = paddr;
781 struct psycho_pbm *pp = t->cookie;
782 struct psycho_softc *sc = pp->pp_sc;
783 int i, ss;
784
785 ss = get_childspace(t->type);
786
787 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, paddr));
788
789 for (i = 0; i < pp->pp_nrange; i++) {
790 bus_addr_t paddr;
791
792 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
793 continue;
794
795 paddr = pp->pp_range[i].phys_lo + offset;
796 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
797 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
798 (long)ss, (long)offset, paddr));
799 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
800 flags, hp));
801 }
802
803 return (-1);
804 }
805
806 /*
807 * interrupt mapping. this tells what sparc ipl any given ino runs at.
808 */
809 static int pci_ino_to_ipl_table[] = {
810 0, 0, 0, 0, /* PCI A, Slot 0, INTA#/B#/C#/D# */
811 0, 0, 0, 0, /* PCI A, Slot 1, INTA#/B#/C#/D# */
812 0, 0, 0, 0, /* PCI A, Slot 2, INTA#/B#/C#/D# (unavailable) */
813 0, 0, 0, 0, /* PCI A, Slot 3, INTA#/B#/C#/D# (unavailable) */
814 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
815 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
816 0, 0, 0, 0, /* PCI B, Slot 2, INTA#/B#/C#/D# */
817 0, 0, 0, 0, /* PCI B, Slot 3, INTA#/B#/C#/D# */
818 4, /* SCSI */
819 6, /* Ethernet */
820 3, /* Parallel */
821 9, /* Audio Record */
822 9, /* Audio Playback */
823 14, /* Power Fail */
824 4, /* Keyboard/Mouse/Serial */
825 8, /* Floppy */
826 14, /* Thermal Warning */
827 12, /* Keyboard */
828 12, /* Mouse */
829 12, /* Serial */
830 0, /* Reserved */
831 0, /* Reserved */
832 14, /* Uncorrectable ECC error */
833 14, /* Correctable ECC error */
834 14, /* PCI A bus error */
835 14, /* PCI B bus error */
836 14, /* power management */
837 };
838
839 int
840 psycho_intr_map(tag, pin, line, ihp)
841 pcitag_t tag;
842 int pin;
843 int line;
844 pci_intr_handle_t *ihp;
845 {
846
847 if (line < 0 || line > 0x32)
848 panic("psycho_intr_map: line line < 0 || line > 0x32");
849
850 /* UltraSPARC IIi does not use this register, but we have set it */
851 (*ihp) = line;
852 return (0);
853 }
854
855 /*
856 * install an interrupt handler for a PCI device
857 */
858 void *
859 psycho_intr_establish(t, level, flags, handler, arg)
860 bus_space_tag_t t;
861 int level;
862 int flags;
863 int (*handler) __P((void *));
864 void *arg;
865 {
866 struct psycho_pbm *pp = t->cookie;
867 struct psycho_softc *sc = pp->pp_sc;
868 struct intrhand *ih;
869 int ino;
870 long vec = level;
871
872 ih = (struct intrhand *)
873 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
874 if (ih == NULL)
875 return (NULL);
876
877 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: level %x", level));
878 ino = INTINO(vec);
879 DPRINTF(PDB_INTR, (" ino %x", ino));
880 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
881 volatile int64_t *intrmapptr, *intrclrptr;
882 int64_t intrmap = 0;
883 int i;
884
885 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %lx\nHunting for IRQ...\n",
886 (long)ino, intrlev[ino]));
887 if ((ino & INTMAP_OBIO) == 0) {
888 /*
889 * there are only 8 PCI interrupt INO's available
890 */
891 i = INTPCIINOX(vec);
892
893 intrmapptr = &((&sc->sc_regs->pcia_slot0_int)[i]);
894 intrclrptr = &sc->sc_regs->pcia0_clr_int[i<<2];
895
896 DPRINTF(PDB_INTR, ("- turning on PCI intr %d", i));
897 } else {
898 /*
899 * there are INTPCI_MAXOBINO (0x16) OBIO interrupts
900 * available here (i think).
901 */
902 i = INTPCIOBINOX(vec);
903 if (i > INTPCI_MAXOBINO)
904 panic("ino %d", vec);
905
906 intrmapptr = &((&sc->sc_regs->scsi_int_map)[i]);
907 intrclrptr = &((&sc->sc_regs->scsi_clr_int)[i]);
908
909 DPRINTF(PDB_INTR, ("- turning on OBIO intr %d", i));
910 }
911
912 /* Register the map and clear intr registers */
913 ih->ih_map = intrmapptr;
914 ih->ih_clr = intrclrptr;
915
916 /*
917 * Read the current value as we can't change it besides the
918 * valid bit so so make sure only this bit is changed.
919 */
920 intrmap = *intrmapptr;
921 DPRINTF(PDB_INTR, ("; read intrmap = %016qx", intrmap));
922
923 /* Enable the interrupt */
924 intrmap |= INTMAP_V;
925 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
926 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", intrmap));
927 *intrmapptr = intrmap;
928 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
929 (intrmap = *intrmapptr)));
930 }
931 #ifdef DEBUG
932 if (psycho_debug & PDB_INTR) {
933 long i;
934
935 for (i = 0; i < 500000000; i++)
936 continue;
937 }
938 #endif
939
940 ih->ih_fun = handler;
941 ih->ih_arg = arg;
942 ih->ih_number = ino | 0x7c0;
943 ih->ih_pil = pci_ino_to_ipl_table[ino];
944 DPRINTF(PDB_INTR, ("; installing handler %p with ino %u pil %u\n",
945 handler, (u_int)ino, (u_int)ih->ih_pil));
946 intr_establish(ih->ih_pil, ih);
947 return (ih);
948 }
949
950 /*
951 * hooks into the iommu dvma calls.
952 */
953 int
954 psycho_dmamap_load(t, map, buf, buflen, p, flags)
955 bus_dma_tag_t t;
956 bus_dmamap_t map;
957 void *buf;
958 bus_size_t buflen;
959 struct proc *p;
960 int flags;
961 {
962 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
963 struct psycho_softc *sc = pp->pp_sc;
964
965 return (iommu_dvmamap_load(t, &sc->sc_is, map, buf, buflen, p, flags));
966 }
967
968 void
969 psycho_dmamap_unload(t, map)
970 bus_dma_tag_t t;
971 bus_dmamap_t map;
972 {
973 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
974 struct psycho_softc *sc = pp->pp_sc;
975
976 iommu_dvmamap_unload(t, &sc->sc_is, map);
977 }
978
979 int
980 psycho_dmamap_load_raw(tag, map, segs, nsegs, size, flags)
981 bus_dma_tag_t tag;
982 bus_dmamap_t map;
983 bus_dma_segment_t *segs;
984 int nsegs;
985 bus_size_t size;
986 int flags;
987 {
988 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
989 struct psycho_softc *sc = pp->pp_sc;
990
991 return (iommu_dvmamap_load_raw(tag, &sc->sc_is, segs, nsegs, size, flags));
992 }
993
994 void
995 psycho_dmamap_sync(t, map, offset, len, ops)
996 bus_dma_tag_t t;
997 bus_dmamap_t map;
998 bus_addr_t offset;
999 bus_size_t len;
1000 int ops;
1001 {
1002 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1003 struct psycho_softc *sc = pp->pp_sc;
1004
1005 iommu_dvmamap_sync(t, &sc->sc_is, map, offset, len, ops);
1006 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1007 }
1008
1009 int
1010 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1011 bus_dma_tag_t t;
1012 bus_size_t size;
1013 bus_size_t alignment;
1014 bus_size_t boundary;
1015 bus_dma_segment_t *segs;
1016 int nsegs;
1017 int *rsegs;
1018 int flags;
1019 {
1020 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1021 struct psycho_softc *sc = pp->pp_sc;
1022
1023 return (iommu_dvmamem_alloc(t, &sc->sc_is, size, alignment, boundary,
1024 segs, nsegs, rsegs, flags));
1025 }
1026
1027 void
1028 psycho_dmamem_free(t, segs, nsegs)
1029 bus_dma_tag_t t;
1030 bus_dma_segment_t *segs;
1031 int nsegs;
1032 {
1033 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1034 struct psycho_softc *sc = pp->pp_sc;
1035
1036 iommu_dvmamem_free(t, &sc->sc_is, segs, nsegs);
1037 }
1038
1039 int
1040 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1041 bus_dma_tag_t t;
1042 bus_dma_segment_t *segs;
1043 int nsegs;
1044 size_t size;
1045 caddr_t *kvap;
1046 int flags;
1047 {
1048 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1049 struct psycho_softc *sc = pp->pp_sc;
1050
1051 return (iommu_dvmamem_map(t, &sc->sc_is, segs, nsegs, size, kvap, flags));
1052 }
1053
1054 void
1055 psycho_dmamem_unmap(t, kva, size)
1056 bus_dma_tag_t t;
1057 caddr_t kva;
1058 size_t size;
1059 {
1060 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1061 struct psycho_softc *sc = pp->pp_sc;
1062
1063 iommu_dvmamem_unmap(t, &sc->sc_is, kva, size);
1064 }
1065