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