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