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