psycho.c revision 1.15 1 /* $NetBSD: psycho.c,v 1.15 2000/06/26 04:56:09 simonb 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 *, int));
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 unsigned 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 = NULL;
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
366 /*
367 * SABRE seems to be buggy. It only appears to work with 128K IOTSB.
368 * I have tried other sizes but they just don't seem to work. Maybe
369 * more testing is needed.
370 *
371 * The PROM reserves a certain amount of RAM for an IOTSB. The
372 * problem is that it's not necessarily the full 128K. So we'll free
373 * this space up and let iommu_init() allocate a full mapping.
374 *
375 * (Otherwise we would need to change the iommu code to handle a
376 * preallocated TSB that may not cover the entire DVMA address
377 * space...
378 *
379 * The information about this memory is shared between the
380 * `virtual-dma' property, which describes the base and size of the
381 * virtual region, and the IOMMU base address register which is the
382 * only known pointer to the RAM. To free up the memory you need to
383 * read the base addres register and then calculate the size by taking
384 * the virtual size and dividing it by 1K to get the size in bytes.
385 * This range can then be freed up by calling uvm_page_physload().
386 *
387 */
388
389 /* and finally start up the IOMMU ... */
390 psycho_iommu_init(sc, 7);
391
392 /*
393 * get us a config space tag, and punch in the physical address
394 * of the PCI configuration space. note that we use unmapped
395 * access to PCI configuration space, relying on the bus space
396 * macros to provide the proper ASI based on the bus tag.
397 */
398 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_simba_a);
399 if (bus_space_map2(sc->sc_bustag,
400 PCI_CONFIG_BUS_SPACE,
401 sc->sc_basepaddr + 0x01000000,
402 0x0100000,
403 0,
404 0,
405 &bh))
406 panic("could not map sabre PCI configuration space");
407 sc->sc_configaddr = bh;
408 }
409
410 /*
411 * SUNW,psycho initialisation ..
412 * - XXX what do we do here?
413 *
414 * i think that an attaching psycho should here find it's partner psycho
415 * and if they haven't been attached yet, allocate both psycho_pbm's and
416 * fill them both in here, and when the partner attaches, there is little
417 * to do... perhaps keep a static array of what psycho have been found so
418 * far (or perhaps those that have not yet been finished). .mrg.
419 * note that the partner can be found via matching `ranges' properties.
420 */
421 static void
422 psycho_init(sc, pba)
423 struct psycho_softc *sc;
424 struct pcibus_attach_args *pba;
425 {
426 struct psycho_softc *osc = NULL;
427 struct psycho_pbm *pp;
428 bus_space_handle_t bh;
429 u_int64_t csr;
430 int psycho_br[2], n;
431 char who;
432
433 printf("psycho: ");
434
435 /*
436 * OK, so the deal here is:
437 * - given our base register address, search our sibling
438 * devices for a match.
439 * - if we find a match, we are attaching an almost
440 * already setup PCI bus, the partner already done.
441 * - otherwise, we are doing the hard slog.
442 */
443 for (n = 0; n < psycho_cd.cd_ndevs; n++) {
444
445 osc = (struct psycho_softc *)&psycho_cd.cd_devs[n];
446
447 /*
448 * I am not myself.
449 */
450 if (osc == sc || osc->sc_regs != sc->sc_regs)
451 continue;
452
453 /*
454 * OK, so we found a matching regs that wasn't me,
455 * so that means my IOMMU is setup.
456 */
457
458 /* who? said a voice, incredulous */
459 sc->sc_mode = PSYCHO_MODE_PSYCHO_B; /* XXX */
460 who = 'b';
461 break;
462 }
463
464 if (sc->sc_mode != PSYCHO_MODE_PSYCHO_B) {
465 sc->sc_mode = PSYCHO_MODE_PSYCHO_A; /* XXX */
466 who = 'a';
467 }
468
469 /* Oh, dear. OK, lets get started */
470
471 /* XXX: check this is OK for real psycho */
472 /* setup the PCI control register */
473 csr = bus_space_read_8(sc->sc_bustag, (bus_space_handle_t)(u_long)&sc->sc_regs->psy_pcictl[0].pci_csr, 0);
474 csr |= PCICTL_MRLM |
475 PCICTL_ARB_PARK |
476 PCICTL_ERRINTEN |
477 PCICTL_4ENABLE;
478 csr &= ~(PCICTL_SERR |
479 PCICTL_CPU_PRIO |
480 PCICTL_ARB_PRIO |
481 PCICTL_RTRYWAIT);
482 bus_space_write_8(sc->sc_bustag, &sc->sc_regs->psy_pcictl[0].pci_csr, 0, csr);
483
484 /* allocate our psycho_pbm */
485 sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
486 if (sc->sc_psycho_this == NULL)
487 panic("could not allocate psycho pbm");
488 if (osc) {
489 sc->sc_psycho_other = osc->sc_psycho_this;
490 osc->sc_psycho_other = sc->sc_psycho_this;
491 }
492
493 memset(sc->sc_psycho_this, 0, sizeof *pp);
494
495 /* grab the psycho ranges */
496 psycho_get_ranges(sc->sc_node, &sc->sc_psycho_this->pp_range,
497 &sc->sc_psycho_this->pp_nrange);
498
499 /* get the bus-range for the psycho */
500 psycho_get_bus_range(sc->sc_node, psycho_br);
501
502 pba->pba_bus = psycho_br[0];
503
504 printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
505 printf("; simba %c, PCI bus %d", who, psycho_br[0]);
506
507 pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
508
509 /* grab the psycho registers, interrupt map and map mask */
510 psycho_get_registers(sc->sc_node, &pp->pp_regs, &pp->pp_nregs);
511 psycho_get_intmap(sc->sc_node, &pp->pp_intmap, &pp->pp_nintmap);
512 psycho_get_intmapmask(sc->sc_node, &pp->pp_intmapmask);
513
514 /* allocate our tags */
515 pp->pp_memt = psycho_alloc_mem_tag(pp);
516 pp->pp_iot = psycho_alloc_io_tag(pp);
517 pp->pp_dmat = psycho_alloc_dma_tag(pp);
518 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
519 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
520
521 /* allocate a chipset for this */
522 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
523
524 /* setup the rest of the psycho pbm */
525 pp->pp_sc = sc;
526 pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
527 sc->sc_psycho_this->pp_pc);
528
529 printf("\n");
530
531 /*
532 * and finally, if we a a psycho A, start up the IOMMU and
533 * get us a config space tag, and punch in the physical address
534 * of the PCI configuration space. note that we use unmapped
535 * access to PCI configuration space, relying on the bus space
536 * macros to provide the proper ASI based on the bus tag.
537 */
538 if (sc->sc_mode == PSYCHO_MODE_PSYCHO_A) {
539 /*
540 * We should calculate a TSB size based on amount of RAM
541 * and number of bus controllers.
542 *
543 * For the moment, 32KB should be more than enough.
544 */
545 psycho_iommu_init(sc, 2);
546
547 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
548 if (bus_space_map2(sc->sc_bustag,
549 PCI_CONFIG_BUS_SPACE,
550 sc->sc_basepaddr + 0x01000000,
551 0x0100000,
552 0,
553 0,
554 &bh))
555 panic("could not map sabre PCI configuration space");
556 sc->sc_configaddr = (off_t)bh;
557 } else {
558 /* for psycho B, we just copy the config tag and address */
559 sc->sc_configtag = osc->sc_configtag;
560 sc->sc_configaddr = osc->sc_configaddr;
561 }
562 }
563
564 /*
565 * PCI bus support
566 */
567
568 /*
569 * allocate a PCI chipset tag and set it's cookie.
570 */
571 static pci_chipset_tag_t
572 psycho_alloc_chipset(pp, node, pc)
573 struct psycho_pbm *pp;
574 int node;
575 pci_chipset_tag_t pc;
576 {
577 pci_chipset_tag_t npc;
578
579 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
580 if (npc == NULL)
581 panic("could not allocate pci_chipset_tag_t");
582 memcpy(npc, pc, sizeof *pc);
583 npc->cookie = pp;
584 npc->node = node;
585
586 return (npc);
587 }
588
589 /*
590 * grovel the OBP for various psycho properties
591 */
592 static void
593 psycho_get_bus_range(node, brp)
594 int node;
595 int *brp;
596 {
597 int n;
598
599 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
600 panic("could not get psycho bus-range");
601 if (n != 2)
602 panic("broken psycho bus-range");
603 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
604 }
605
606 static void
607 psycho_get_ranges(node, rp, np)
608 int node;
609 struct psycho_ranges **rp;
610 int *np;
611 {
612
613 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
614 panic("could not get psycho ranges");
615 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
616 }
617
618 static void
619 psycho_get_registers(node, rp, np)
620 int node;
621 struct psycho_registers **rp;
622 int *np;
623 {
624
625 if (getprop(node, "reg", sizeof(**rp), np, (void **)rp))
626 panic("could not get psycho registers");
627 DPRINTF(PDB_PROM, ("psycho debug: got `reg' for node %08x: %d entries\n", node, *np));
628 }
629
630 static void
631 psycho_get_intmap(node, imp, np)
632 int node;
633 struct psycho_interrupt_map **imp;
634 int *np;
635 {
636
637 if (getprop(node, "interrupt-map", sizeof(**imp), np, (void **)imp))
638 panic("could not get psycho interrupt-map");
639 DPRINTF(PDB_PROM, ("psycho debug: got `interupt-map' for node %08x\n", node));
640 }
641
642 static void
643 psycho_get_intmapmask(node, immp)
644 int node;
645 struct psycho_interrupt_map_mask *immp;
646 {
647 int n;
648
649 if (getprop(node, "interrupt-map-mask", sizeof(*immp), &n,
650 (void **)&immp))
651 panic("could not get psycho interrupt-map-mask");
652 if (n != 1)
653 panic("broken psycho interrupt-map-mask");
654 DPRINTF(PDB_PROM, ("psycho debug: got `interrupt-map-mask' for node %08x\n", node));
655 }
656
657 /*
658 * initialise the IOMMU..
659 */
660 void
661 psycho_iommu_init(sc, tsbsize)
662 struct psycho_softc *sc;
663 int tsbsize;
664 {
665 char *name;
666
667 /* punch in our copies */
668 sc->sc_is.is_bustag = sc->sc_bustag;
669 sc->sc_is.is_iommu = &sc->sc_regs->psy_iommu;
670
671 if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
672 sc->sc_is.is_sb = 0;
673 else
674 sc->sc_is.is_sb = &sc->sc_regs->psy_iommu_strbuf;
675
676 /* give us a nice name.. */
677 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
678 if (name == 0)
679 panic("couldn't malloc iommu name");
680 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
681
682 iommu_init(name, &sc->sc_is, tsbsize);
683 }
684
685 /*
686 * below here is bus space and bus dma support
687 */
688 bus_space_tag_t
689 psycho_alloc_bus_tag(pp, type)
690 struct psycho_pbm *pp;
691 int type;
692 {
693 struct psycho_softc *sc = pp->pp_sc;
694 bus_space_tag_t bt;
695
696 bt = (bus_space_tag_t)
697 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
698 if (bt == NULL)
699 panic("could not allocate psycho bus tag");
700
701 bzero(bt, sizeof *bt);
702 bt->cookie = pp;
703 bt->parent = sc->sc_bustag;
704 bt->type = type;
705 bt->sparc_bus_map = _psycho_bus_map;
706 bt->sparc_bus_mmap = psycho_bus_mmap;
707 bt->sparc_intr_establish = psycho_intr_establish;
708 return (bt);
709 }
710
711 bus_dma_tag_t
712 psycho_alloc_dma_tag(pp)
713 struct psycho_pbm *pp;
714 {
715 struct psycho_softc *sc = pp->pp_sc;
716 bus_dma_tag_t dt, pdt = sc->sc_dmatag;
717
718 dt = (bus_dma_tag_t)
719 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
720 if (dt == NULL)
721 panic("could not allocate psycho dma tag");
722
723 bzero(dt, sizeof *dt);
724 dt->_cookie = pp;
725 dt->_parent = pdt;
726 #define PCOPY(x) dt->x = pdt->x
727 PCOPY(_dmamap_create);
728 PCOPY(_dmamap_destroy);
729 dt->_dmamap_load = psycho_dmamap_load;
730 PCOPY(_dmamap_load_mbuf);
731 PCOPY(_dmamap_load_uio);
732 dt->_dmamap_load_raw = psycho_dmamap_load_raw;
733 dt->_dmamap_unload = psycho_dmamap_unload;
734 dt->_dmamap_sync = psycho_dmamap_sync;
735 dt->_dmamem_alloc = psycho_dmamem_alloc;
736 dt->_dmamem_free = psycho_dmamem_free;
737 dt->_dmamem_map = psycho_dmamem_map;
738 dt->_dmamem_unmap = psycho_dmamem_unmap;
739 PCOPY(_dmamem_mmap);
740 #undef PCOPY
741 return (dt);
742 }
743
744 /*
745 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about
746 * PCI physical addresses.
747 */
748
749 static int get_childspace __P((int));
750
751 static int
752 get_childspace(type)
753 int type;
754 {
755 int ss;
756
757 switch (type) {
758 case PCI_CONFIG_BUS_SPACE:
759 ss = 0x00;
760 break;
761 case PCI_IO_BUS_SPACE:
762 ss = 0x01;
763 break;
764 case PCI_MEMORY_BUS_SPACE:
765 ss = 0x02;
766 break;
767 #if 0
768 /* we don't do 64 bit memory space */
769 case PCI_MEMORY64_BUS_SPACE:
770 ss = 0x03;
771 break;
772 #endif
773 default:
774 panic("get_childspace: unknown bus type");
775 }
776
777 return (ss);
778 }
779
780 static int
781 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
782 bus_space_tag_t t;
783 bus_type_t btype;
784 bus_addr_t offset;
785 bus_size_t size;
786 int flags;
787 vaddr_t vaddr;
788 bus_space_handle_t *hp;
789 {
790 struct psycho_pbm *pp = t->cookie;
791 struct psycho_softc *sc = pp->pp_sc;
792 int i, ss;
793
794 DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, offset, size, flags, vaddr));
795
796 ss = get_childspace(t->type);
797 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
798
799
800 for (i = 0; i < pp->pp_nrange; i++) {
801 bus_addr_t paddr;
802
803 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
804 continue;
805
806 paddr = pp->pp_range[i].phys_lo + offset;
807 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
808 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
809 (long)ss, (long)offset, paddr));
810 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
811 size, flags, vaddr, hp));
812 }
813 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
814 return (EINVAL);
815 }
816
817 static int
818 psycho_bus_mmap(t, btype, paddr, flags, hp)
819 bus_space_tag_t t;
820 bus_type_t btype;
821 bus_addr_t paddr;
822 int flags;
823 bus_space_handle_t *hp;
824 {
825 bus_addr_t offset = paddr;
826 struct psycho_pbm *pp = t->cookie;
827 struct psycho_softc *sc = pp->pp_sc;
828 int i, ss;
829
830 ss = get_childspace(t->type);
831
832 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, paddr));
833
834 for (i = 0; i < pp->pp_nrange; i++) {
835 bus_addr_t paddr;
836
837 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
838 continue;
839
840 paddr = pp->pp_range[i].phys_lo + offset;
841 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
842 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
843 (long)ss, (long)offset, paddr));
844 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
845 flags, hp));
846 }
847
848 return (-1);
849 }
850
851 /*
852 * interrupt mapping. this tells what sparc ipl any given ino runs at.
853 */
854 static int pci_ino_to_ipl_table[] = {
855 0, 0, 0, 0, /* PCI A, Slot 0, INTA#/B#/C#/D# */
856 0, 0, 0, 0, /* PCI A, Slot 1, INTA#/B#/C#/D# */
857 0, 0, 0, 0, /* PCI A, Slot 2, INTA#/B#/C#/D# (unavailable) */
858 0, 0, 0, 0, /* PCI A, Slot 3, INTA#/B#/C#/D# (unavailable) */
859 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
860 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
861 0, 0, 0, 0, /* PCI B, Slot 2, INTA#/B#/C#/D# */
862 0, 0, 0, 0, /* PCI B, Slot 3, INTA#/B#/C#/D# */
863 4, /* SCSI */
864 6, /* Ethernet */
865 3, /* Parallel */
866 9, /* Audio Record */
867 9, /* Audio Playback */
868 14, /* Power Fail */
869 4, /* Keyboard/Mouse/Serial */
870 8, /* Floppy */
871 14, /* Thermal Warning */
872 12, /* Keyboard */
873 12, /* Mouse */
874 12, /* Serial */
875 0, /* Reserved */
876 0, /* Reserved */
877 14, /* Uncorrectable ECC error */
878 14, /* Correctable ECC error */
879 14, /* PCI A bus error */
880 14, /* PCI B bus error */
881 14, /* power management */
882 };
883
884 #ifdef NOT_DEBUG
885 static struct psycho_pbm *ppbm;
886 #endif
887
888 int
889 psycho_intr_map(tag, pin, line, ihp)
890 pcitag_t tag;
891 int pin;
892 int line;
893 pci_intr_handle_t *ihp;
894 {
895
896 if (line < 0 || line > 0x32)
897 panic("psycho_intr_map: line line < 0 || line > 0x32");
898
899 /* UltraSPARC IIi does not use this register, but we have set it */
900 (*ihp) = line;
901 return (0);
902 }
903
904 /*
905 * install an interrupt handler for a PCI device
906 */
907 void *
908 psycho_intr_establish(t, level, flags, handler, arg)
909 bus_space_tag_t t;
910 int level;
911 int flags;
912 int (*handler) __P((void *));
913 void *arg;
914 {
915 struct psycho_pbm *pp = t->cookie;
916 struct psycho_softc *sc = pp->pp_sc;
917 struct intrhand *ih;
918 int ino;
919 long vec = level;
920
921 #ifdef NOT_DEBUG
922 if (!ppbm)
923 ppbm = pp;
924 #endif
925 ih = (struct intrhand *)
926 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
927 if (ih == NULL)
928 return (NULL);
929
930 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: level %x", level));
931 ino = INTINO(vec);
932 DPRINTF(PDB_INTR, (" ino %x", ino));
933 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
934 volatile int64_t *intrmapptr, *intrclrptr;
935 int64_t intrmap = 0;
936 int i;
937
938 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %lx\nHunting for IRQ...\n",
939 (long)ino, intrlev[ino]));
940 if ((ino & INTMAP_OBIO) == 0) {
941 /*
942 * there are only 8 PCI interrupt INO's available
943 */
944 i = INTPCIINOX(vec);
945
946 intrmapptr = &((&sc->sc_regs->pcia_slot0_int)[i]);
947 intrclrptr = &sc->sc_regs->pcia0_clr_int[i<<2];
948
949 DPRINTF(PDB_INTR, ("- turning on PCI intr %d", i));
950 } else {
951 /*
952 * there are INTPCI_MAXOBINO (0x16) OBIO interrupts
953 * available here (i think).
954 */
955 i = INTPCIOBINOX(vec);
956 if (i > INTPCI_MAXOBINO)
957 panic("ino %d", vec);
958
959 intrmapptr = &((&sc->sc_regs->scsi_int_map)[i]);
960 intrclrptr = &((&sc->sc_regs->scsi_clr_int)[i]);
961
962 DPRINTF(PDB_INTR, ("- turning on OBIO intr %d", i));
963 }
964
965 /* Register the map and clear intr registers */
966 ih->ih_map = intrmapptr;
967 ih->ih_clr = intrclrptr;
968
969 /*
970 * Read the current value as we can't change it besides the
971 * valid bit so so make sure only this bit is changed.
972 */
973 intrmap = *intrmapptr;
974 DPRINTF(PDB_INTR, ("; read intrmap = %016qx", intrmap));
975
976 /* Enable the interrupt */
977 intrmap |= INTMAP_V;
978 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
979 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", intrmap));
980 *intrmapptr = intrmap;
981 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
982 (intrmap = *intrmapptr)));
983 }
984 #ifdef NOT_DEBUG
985 if (psycho_debug & PDB_INTR) {
986 long i;
987
988 for (i = 0; i < 500000000; i++)
989 continue;
990 }
991 #endif
992
993 ih->ih_fun = handler;
994 ih->ih_arg = arg;
995 ih->ih_number = ino | 0x7c0;
996 ih->ih_pil = pci_ino_to_ipl_table[ino];
997 DPRINTF(PDB_INTR, ("; installing handler %p with ino %u pil %u\n",
998 handler, (u_int)ino, (u_int)ih->ih_pil));
999 intr_establish(ih->ih_pil, ih);
1000 return (ih);
1001 }
1002
1003 /*
1004 * hooks into the iommu dvma calls.
1005 */
1006 int
1007 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1008 bus_dma_tag_t t;
1009 bus_dmamap_t map;
1010 void *buf;
1011 bus_size_t buflen;
1012 struct proc *p;
1013 int flags;
1014 {
1015 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1016 struct psycho_softc *sc = pp->pp_sc;
1017
1018 return (iommu_dvmamap_load(t, &sc->sc_is, map, buf, buflen, p, flags));
1019 }
1020
1021 void
1022 psycho_dmamap_unload(t, map)
1023 bus_dma_tag_t t;
1024 bus_dmamap_t map;
1025 {
1026 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1027 struct psycho_softc *sc = pp->pp_sc;
1028
1029 iommu_dvmamap_unload(t, &sc->sc_is, map);
1030 }
1031
1032 int
1033 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1034 bus_dma_tag_t t;
1035 bus_dmamap_t map;
1036 bus_dma_segment_t *segs;
1037 int nsegs;
1038 bus_size_t size;
1039 int flags;
1040 {
1041 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1042 struct psycho_softc *sc = pp->pp_sc;
1043
1044 return (iommu_dvmamap_load_raw(t, &sc->sc_is, map, segs, nsegs, size, flags));
1045 }
1046
1047 void
1048 psycho_dmamap_sync(t, map, offset, len, ops)
1049 bus_dma_tag_t t;
1050 bus_dmamap_t map;
1051 bus_addr_t offset;
1052 bus_size_t len;
1053 int ops;
1054 {
1055 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1056 struct psycho_softc *sc = pp->pp_sc;
1057
1058 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1059 /* Flush the CPU then the IOMMU */
1060 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1061 iommu_dvmamap_sync(t, &sc->sc_is, map, offset, len, ops);
1062 }
1063 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1064 /* Flush the IOMMU then the CPU */
1065 iommu_dvmamap_sync(t, &sc->sc_is, map, offset, len, ops);
1066 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1067 }
1068
1069 }
1070
1071 int
1072 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1073 bus_dma_tag_t t;
1074 bus_size_t size;
1075 bus_size_t alignment;
1076 bus_size_t boundary;
1077 bus_dma_segment_t *segs;
1078 int nsegs;
1079 int *rsegs;
1080 int flags;
1081 {
1082 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1083 struct psycho_softc *sc = pp->pp_sc;
1084
1085 return (iommu_dvmamem_alloc(t, &sc->sc_is, size, alignment, boundary,
1086 segs, nsegs, rsegs, flags));
1087 }
1088
1089 void
1090 psycho_dmamem_free(t, segs, nsegs)
1091 bus_dma_tag_t t;
1092 bus_dma_segment_t *segs;
1093 int nsegs;
1094 {
1095 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1096 struct psycho_softc *sc = pp->pp_sc;
1097
1098 iommu_dvmamem_free(t, &sc->sc_is, segs, nsegs);
1099 }
1100
1101 int
1102 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1103 bus_dma_tag_t t;
1104 bus_dma_segment_t *segs;
1105 int nsegs;
1106 size_t size;
1107 caddr_t *kvap;
1108 int flags;
1109 {
1110 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1111 struct psycho_softc *sc = pp->pp_sc;
1112
1113 return (iommu_dvmamem_map(t, &sc->sc_is, segs, nsegs, size, kvap, flags));
1114 }
1115
1116 void
1117 psycho_dmamem_unmap(t, kva, size)
1118 bus_dma_tag_t t;
1119 caddr_t kva;
1120 size_t size;
1121 {
1122 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1123 struct psycho_softc *sc = pp->pp_sc;
1124
1125 iommu_dvmamem_unmap(t, &sc->sc_is, kva, size);
1126 }
1127
1128 #ifdef NOT_DEBUG
1129 void
1130 psycho_print_intr_state(void)
1131 {
1132 pcitag_t tag;
1133 bus_space_handle_t bh;
1134 u_int64_t data, diag;
1135 struct psycho_softc *sc = ppbm->pp_sc;
1136
1137 if (!ppbm) {
1138 printf("psycho_print_intr_state: no ppbm configured\n");
1139 return;
1140 }
1141 printf("psycho_print_intr_state: ");
1142
1143 bh = sc->sc_basepaddr;
1144 bh = (bus_space_handle_t)(u_long)sc->sc_regs;
1145 diag = bus_space_read_8(sc->sc_configtag, bh, 0xa800);
1146 printf("all PCI diags is %qx\n", diag);
1147 #if 0
1148 for (tag = 0xc00; tag < 0xc40; tag += 0x8) {
1149 data = bus_space_read_8(sc->sc_configtag, bh, tag);
1150
1151 printf(" - PCI slot at %qx reads as %qx", bh + tag, data);
1152 printf(": diag %x\n", (int)(diag & 0xff));
1153 diag >>= 8;
1154 }
1155 #endif
1156
1157 diag = bus_space_read_8(sc->sc_configtag, bh, 0xa808);
1158 printf("\t\tall OBIO diags is %qx\n", diag);
1159 #define START_TAG 0x1000 /* 0x1000 */
1160 #define END_TAG 0x1018 /* 0x1088 */
1161 for (tag = START_TAG; tag < END_TAG; tag += 0x8) {
1162 data = bus_space_read_8(sc->sc_configtag, bh + tag, 0);
1163
1164 printf(" - OBIO slot at %qx reads as %qx", bh + tag, data);
1165 printf(": diag %x\n", (int)(diag & 0x3));
1166 diag >>= 2;
1167 }
1168 }
1169 #endif
1170