psycho.c revision 1.27 1 /* $NetBSD: psycho.c,v 1.27 2000/12/04 16:01:18 fvdl 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,
275 (bus_space_handle_t)&sc->sc_regs->psy_pcictl[0].pci_csr, 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", fn);
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, (unsigned long long)offset, (unsigned long long)size, flags,
825 (void *)vaddr));
826
827 ss = get_childspace(t->type);
828 DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
829
830
831 for (i = 0; i < pp->pp_nrange; i++) {
832 bus_addr_t paddr;
833
834 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
835 continue;
836
837 paddr = pp->pp_range[i].phys_lo + offset;
838 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
839 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
840 (long)ss, (long)offset,
841 (unsigned long long)paddr));
842 return (bus_space_map2(sc->sc_bustag, t->type, paddr,
843 size, flags, vaddr, hp));
844 }
845 DPRINTF(PDB_BUSMAP, (" FAILED\n"));
846 return (EINVAL);
847 }
848
849 static int
850 psycho_bus_mmap(t, btype, paddr, flags, hp)
851 bus_space_tag_t t;
852 bus_type_t btype;
853 bus_addr_t paddr;
854 int flags;
855 bus_space_handle_t *hp;
856 {
857 bus_addr_t offset = paddr;
858 struct psycho_pbm *pp = t->cookie;
859 struct psycho_softc *sc = pp->pp_sc;
860 int i, ss;
861
862 ss = get_childspace(t->type);
863
864 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, (unsigned long long)paddr));
865
866 for (i = 0; i < pp->pp_nrange; i++) {
867 bus_addr_t paddr;
868
869 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
870 continue;
871
872 paddr = pp->pp_range[i].phys_lo + offset;
873 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
874 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
875 (long)ss, (long)offset,
876 (unsigned long long)paddr));
877 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
878 flags, hp));
879 }
880
881 return (-1);
882 }
883
884 /*
885 * interrupt mapping. this tells what sparc ipl any given ino runs at.
886 */
887 static int pci_ino_to_ipl_table[] = {
888 0, 0, 0, 0, /* PCI A, Slot 0, INTA#/B#/C#/D# */
889 0, 0, 0, 0, /* PCI A, Slot 1, INTA#/B#/C#/D# */
890 0, 0, 0, 0, /* PCI A, Slot 2, INTA#/B#/C#/D# (unavailable) */
891 0, 0, 0, 0, /* PCI A, Slot 3, INTA#/B#/C#/D# (unavailable) */
892 0, 0, 0, 0, /* PCI B, Slot 0, INTA#/B#/C#/D# */
893 0, 0, 0, 0, /* PCI B, Slot 1, INTA#/B#/C#/D# */
894 0, 0, 0, 0, /* PCI B, Slot 2, INTA#/B#/C#/D# */
895 0, 0, 0, 0, /* PCI B, Slot 3, INTA#/B#/C#/D# */
896 PIL_SCSI, /* SCSI */
897 PIL_NET, /* Ethernet */
898 3, /* Parallel */
899 PIL_AUD, /* Audio Record */
900 PIL_AUD, /* Audio Playback */
901 14, /* Power Fail */
902 4, /* Keyboard/Mouse/Serial */
903 PIL_FD, /* Floppy */
904 14, /* Thermal Warning */
905 PIL_SER, /* Keyboard */
906 PIL_SER, /* Mouse */
907 PIL_SER, /* Serial */
908 0, /* Reserved */
909 0, /* Reserved */
910 14, /* Uncorrectable ECC error */
911 14, /* Correctable ECC error */
912 14, /* PCI A bus error */
913 14, /* PCI B bus error */
914 14, /* power management */
915 };
916
917 int
918 psycho_intr_map(tag, pin, line, ihp)
919 pcitag_t tag;
920 int pin;
921 int line;
922 pci_intr_handle_t *ihp;
923 {
924
925 if (line < 0 || line > 0x32)
926 panic("psycho_intr_map: line line < 0 || line > 0x32");
927
928 /* UltraSPARC IIi does not use this register, but we have set it */
929 (*ihp) = line;
930 return (0);
931 }
932
933 /*
934 * install an interrupt handler for a PCI device
935 */
936 void *
937 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
938 bus_space_tag_t t;
939 int ihandle;
940 int level;
941 int flags;
942 int (*handler) __P((void *));
943 void *arg;
944 {
945 struct psycho_pbm *pp = t->cookie;
946 struct psycho_softc *sc = pp->pp_sc;
947 struct intrhand *ih;
948 int ino;
949 long vec = ihandle;
950
951 ih = (struct intrhand *)
952 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
953 if (ih == NULL)
954 return (NULL);
955
956 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x", ihandle));
957 ino = INTINO(vec);
958 DPRINTF(PDB_INTR, (" ino %x", ino));
959 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
960 volatile int64_t *intrmapptr, *intrclrptr;
961 int64_t intrmap = 0;
962 int i;
963
964 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
965 (long)ino, intrlev[ino]));
966 if ((ino & INTMAP_OBIO) == 0) {
967 /*
968 * there are only 8 PCI interrupt INO's available
969 */
970 i = INTPCIINOX(vec);
971
972 intrmapptr = &((&sc->sc_regs->pcia_slot0_int)[i]);
973 intrclrptr = &sc->sc_regs->pcia0_clr_int[ino];
974
975 DPRINTF(PDB_INTR, ("- turning on PCI intr %d", i));
976 } else {
977 /*
978 * there are INTPCI_MAXOBINO (0x16) OBIO interrupts
979 * available here (i think).
980 */
981 i = INTPCIOBINOX(vec);
982 if (i > INTPCI_MAXOBINO)
983 panic("ino %ld", vec);
984
985 intrmapptr = &((&sc->sc_regs->scsi_int_map)[i]);
986 intrclrptr = &((&sc->sc_regs->scsi_clr_int)[i]);
987
988 DPRINTF(PDB_INTR, ("- turning on OBIO intr %d", i));
989 }
990
991 /* Register the map and clear intr registers */
992 ih->ih_map = intrmapptr;
993 ih->ih_clr = intrclrptr;
994
995 /*
996 * Read the current value as we can't change it besides the
997 * valid bit so so make sure only this bit is changed.
998 */
999 intrmap = *intrmapptr;
1000 DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1001 (unsigned long long)intrmap));
1002
1003 /* Enable the interrupt */
1004 intrmap |= INTMAP_V;
1005 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1006 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1007 (unsigned long long)intrmap));
1008 *intrmapptr = intrmap;
1009 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1010 (unsigned long long)(intrmap = *intrmapptr)));
1011 }
1012 #ifdef NOT_DEBUG
1013 if (psycho_debug & PDB_INTR) {
1014 long i;
1015
1016 for (i = 0; i < 500000000; i++)
1017 continue;
1018 }
1019 #endif
1020
1021 ih->ih_fun = handler;
1022 ih->ih_arg = arg;
1023 ih->ih_number = ino | sc->sc_ign;
1024 /*
1025 * If a `device class' level is specified, use it,
1026 * else get the PIL from a built-in table.
1027 */
1028 if (level != IPL_NONE)
1029 ih->ih_pil = level;
1030 else
1031 ih->ih_pil = pci_ino_to_ipl_table[ino];
1032
1033 DPRINTF(PDB_INTR, (
1034 "; installing handler %p arg %p with ino %u pil %u\n",
1035 handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1036
1037 intr_establish(ih->ih_pil, ih);
1038 return (ih);
1039 }
1040
1041 /*
1042 * hooks into the iommu dvma calls.
1043 */
1044 int
1045 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1046 bus_dma_tag_t t;
1047 bus_dmamap_t map;
1048 void *buf;
1049 bus_size_t buflen;
1050 struct proc *p;
1051 int flags;
1052 {
1053 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1054 struct psycho_softc *sc = pp->pp_sc;
1055
1056 return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1057 }
1058
1059 void
1060 psycho_dmamap_unload(t, map)
1061 bus_dma_tag_t t;
1062 bus_dmamap_t map;
1063 {
1064 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1065 struct psycho_softc *sc = pp->pp_sc;
1066
1067 iommu_dvmamap_unload(t, sc->sc_is, map);
1068 }
1069
1070 int
1071 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1072 bus_dma_tag_t t;
1073 bus_dmamap_t map;
1074 bus_dma_segment_t *segs;
1075 int nsegs;
1076 bus_size_t size;
1077 int flags;
1078 {
1079 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1080 struct psycho_softc *sc = pp->pp_sc;
1081
1082 return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1083 }
1084
1085 void
1086 psycho_dmamap_sync(t, map, offset, len, ops)
1087 bus_dma_tag_t t;
1088 bus_dmamap_t map;
1089 bus_addr_t offset;
1090 bus_size_t len;
1091 int ops;
1092 {
1093 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1094 struct psycho_softc *sc = pp->pp_sc;
1095
1096 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1097 /* Flush the CPU then the IOMMU */
1098 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1099 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1100 }
1101 if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1102 /* Flush the IOMMU then the CPU */
1103 iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1104 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1105 }
1106
1107 }
1108
1109 int
1110 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1111 bus_dma_tag_t t;
1112 bus_size_t size;
1113 bus_size_t alignment;
1114 bus_size_t boundary;
1115 bus_dma_segment_t *segs;
1116 int nsegs;
1117 int *rsegs;
1118 int flags;
1119 {
1120 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1121 struct psycho_softc *sc = pp->pp_sc;
1122
1123 return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1124 segs, nsegs, rsegs, flags));
1125 }
1126
1127 void
1128 psycho_dmamem_free(t, segs, nsegs)
1129 bus_dma_tag_t t;
1130 bus_dma_segment_t *segs;
1131 int nsegs;
1132 {
1133 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1134 struct psycho_softc *sc = pp->pp_sc;
1135
1136 iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1137 }
1138
1139 int
1140 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1141 bus_dma_tag_t t;
1142 bus_dma_segment_t *segs;
1143 int nsegs;
1144 size_t size;
1145 caddr_t *kvap;
1146 int flags;
1147 {
1148 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1149 struct psycho_softc *sc = pp->pp_sc;
1150
1151 return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1152 }
1153
1154 void
1155 psycho_dmamem_unmap(t, kva, size)
1156 bus_dma_tag_t t;
1157 caddr_t kva;
1158 size_t size;
1159 {
1160 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1161 struct psycho_softc *sc = pp->pp_sc;
1162
1163 iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1164 }
1165