xref: /openbsd-src/sys/arch/sparc64/dev/psycho.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: psycho.c,v 1.66 2009/03/29 22:52:11 kettenis Exp $	*/
2 /*	$NetBSD: psycho.c,v 1.39 2001/10/07 20:30:41 eeh Exp $	*/
3 
4 /*
5  * Copyright (c) 1999, 2000 Matthew R. Green
6  * Copyright (c) 2003 Henric Jungheim
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Support for `psycho' and `psycho+' UPA to PCI bridge and
35  * UltraSPARC IIi and IIe `sabre' PCI controllers.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/errno.h>
41 #include <sys/extent.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #include <sys/time.h>
45 #include <sys/timetc.h>
46 #include <sys/reboot.h>
47 
48 #include <uvm/uvm_extern.h>
49 
50 #define _SPARC_BUS_DMA_PRIVATE
51 #include <machine/bus.h>
52 #include <machine/autoconf.h>
53 #include <machine/openfirm.h>
54 #include <machine/psl.h>
55 
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
58 
59 #include <sparc64/dev/iommureg.h>
60 #include <sparc64/dev/iommuvar.h>
61 #include <sparc64/dev/psychoreg.h>
62 #include <sparc64/dev/psychovar.h>
63 #include <sparc64/dev/starfire.h>
64 #include <sparc64/sparc64/cache.h>
65 
66 #ifdef DEBUG
67 #define PDB_PROM	0x01
68 #define PDB_BUSMAP	0x02
69 #define PDB_INTR	0x04
70 #define PDB_CONF	0x08
71 int psycho_debug = ~0;
72 #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
73 #else
74 #define DPRINTF(l, s)
75 #endif
76 
77 pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int,
78     pci_chipset_tag_t);
79 void psycho_get_bus_range(int, int *);
80 void psycho_get_ranges(int, struct psycho_ranges **, int *);
81 void psycho_set_intr(struct psycho_softc *, int, void *,
82     u_int64_t *, u_int64_t *, const char *);
83 bus_space_tag_t psycho_alloc_bus_tag(struct psycho_pbm *,
84     const char *, int, int, int);
85 
86 /* Interrupt handlers */
87 int psycho_ue(void *);
88 int psycho_ce(void *);
89 int psycho_bus_a(void *);
90 int psycho_bus_b(void *);
91 int psycho_bus_error(struct psycho_softc *, int);
92 int psycho_powerfail(void *);
93 int psycho_wakeup(void *);
94 
95 /* IOMMU support */
96 void psycho_iommu_init(struct psycho_softc *, int);
97 
98 /*
99  * bus space and bus dma support for UltraSPARC `psycho'.  note that most
100  * of the bus dma support is provided by the iommu dvma controller.
101  */
102 int psycho_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t,
103     bus_size_t, int, bus_space_handle_t *);
104 paddr_t psycho_bus_mmap(bus_space_tag_t, bus_space_tag_t, bus_addr_t, off_t,
105     int, int);
106 bus_addr_t psycho_bus_addr(bus_space_tag_t, bus_space_tag_t,
107     bus_space_handle_t);
108 void *psycho_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
109     int (*)(void *), void *, const char *);
110 
111 int psycho_dmamap_create(bus_dma_tag_t, bus_dma_tag_t, bus_size_t, int,
112     bus_size_t, bus_size_t, int, bus_dmamap_t *);
113 void psycho_sabre_dvmamap_sync(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t,
114     bus_size_t, bus_size_t, int);
115 void psycho_map_psycho(struct psycho_softc *, int, bus_addr_t, bus_size_t,
116     bus_addr_t, bus_size_t);
117 int psycho_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
118 void psycho_identify_pbm(struct psycho_softc *sc, struct psycho_pbm *pp,
119     struct pcibus_attach_args *pa);
120 
121 pcireg_t psycho_conf_read(pci_chipset_tag_t, pcitag_t, int);
122 void psycho_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
123 
124 /* base pci_chipset */
125 extern struct sparc_pci_chipset _sparc_pci_chipset;
126 
127 u_int stick_get_timecount(struct timecounter *);
128 
129 struct timecounter stick_timecounter = {
130 	stick_get_timecount, NULL, ~0u, 0, "stick", 1000, NULL
131 };
132 
133 /*
134  * autoconfiguration
135  */
136 int	psycho_match(struct device *, void *, void *);
137 void	psycho_attach(struct device *, struct device *, void *);
138 int	psycho_print(void *aux, const char *p);
139 
140 
141 struct cfattach psycho_ca = {
142         sizeof(struct psycho_softc), psycho_match, psycho_attach
143 };
144 
145 struct cfdriver psycho_cd = {
146 	NULL, "psycho", DV_DULL
147 };
148 
149 /*
150  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
151  * single PCI bus and does not have a streaming buffer.  It often has an APB
152  * (advanced PCI bridge) connected to it, which was designed specifically for
153  * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
154  * appears as two "simba"'s underneath the sabre.
155  *
156  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
157  * and manages two PCI buses.  "psycho" has two 64-bit 33MHz buses, while
158  * "psycho+" controls both a 64-bit 33MHz and a 64-bit 66MHz PCI bus.  You
159  * will usually find a "psycho+" since I don't think the original "psycho"
160  * ever shipped, and if it did it would be in the U30.
161  *
162  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
163  * both part of the same IC, they only have a single register space.  As such,
164  * they need to be configured together, even though the autoconfiguration will
165  * attach them separately.
166  *
167  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
168  * as pci1 and pci2, although they have been implemented with other PCI bus
169  * numbers on some machines.
170  *
171  * On UltraII machines, there can be any number of "psycho+" ICs, each
172  * providing two PCI buses.
173  *
174  *
175  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
176  * the values of the following interrupts in this order:
177  *
178  * PCI Bus Error	(30)
179  * DMA UE		(2e)
180  * DMA CE		(2f)
181  * Power Fail		(25)
182  *
183  * We really should attach handlers for each.
184  *
185  */
186 #define	ROM_PCI_NAME		"pci"
187 
188 struct psycho_type {
189 	char *p_name;
190 	int p_type;
191 } psycho_types[] = {
192 	{ "SUNW,psycho",        PSYCHO_MODE_PSYCHO      },
193 	{ "pci108e,8000",       PSYCHO_MODE_PSYCHO      },
194 	{ "SUNW,sabre",         PSYCHO_MODE_SABRE       },
195 	{ "pci108e,a000",       PSYCHO_MODE_SABRE       },
196 	{ "pci108e,a001",       PSYCHO_MODE_SABRE       },
197 	{ "pci10cf,138f",	PSYCHO_MODE_CMU_CH	},
198 	{ "pci10cf,1390",	PSYCHO_MODE_CMU_CH	},
199 	{ NULL, 0 }
200 };
201 
202 int
203 psycho_match(struct device *parent, void *match, void *aux)
204 {
205 	struct mainbus_attach_args *ma = aux;
206 	struct psycho_type *ptype;
207 	char *str;
208 
209 	/* match on a name of "pci" and a sabre or a psycho */
210 	if (strcmp(ma->ma_name, ROM_PCI_NAME) != 0)
211 		return (0);
212 
213 	for (ptype = psycho_types; ptype->p_name != NULL; ptype++) {
214 		str = getpropstring(ma->ma_node, "model");
215 		if (strcmp(str, ptype->p_name) == 0)
216 			return (1);
217 		str = getpropstring(ma->ma_node, "compatible");
218 		if (strcmp(str, ptype->p_name) == 0)
219 			return (1);
220 	}
221 	return (0);
222 }
223 
224 /*
225  * SUNW,psycho initialization ...
226  *	- find the per-psycho registers
227  *	- figure out the IGN.
228  *	- find our partner psycho
229  *	- configure ourselves
230  *	- bus range, bus,
231  *	- get interrupt-map and interrupt-map-mask
232  *	- setup the chipsets.
233  *	- if we're the first of the pair, initialise the IOMMU, otherwise
234  *	  just copy its tags and addresses.
235  */
236 void
237 psycho_attach(struct device *parent, struct device *self, void *aux)
238 {
239 	struct psycho_softc *sc = (struct psycho_softc *)self;
240 	struct psycho_softc *osc = NULL;
241 	struct psycho_pbm *pp;
242 	struct pcibus_attach_args pba;
243 	struct mainbus_attach_args *ma = aux;
244 	u_int64_t csr;
245 	int psycho_br[2], n;
246 	struct psycho_type *ptype;
247 	char buf[32];
248 	u_int stick_rate;
249 
250 	sc->sc_node = ma->ma_node;
251 	sc->sc_bustag = ma->ma_bustag;
252 	sc->sc_dmatag = ma->ma_dmatag;
253 
254 	/*
255 	 * call the model-specific initialization routine.
256 	 */
257 
258 	for (ptype = psycho_types; ptype->p_name != NULL; ptype++) {
259 		char *str;
260 
261 		str = getpropstring(ma->ma_node, "model");
262 		if (strcmp(str, ptype->p_name) == 0)
263 			break;
264 		str = getpropstring(ma->ma_node, "compatible");
265 		if (strcmp(str, ptype->p_name) == 0)
266 			break;
267 	}
268 	if (ptype->p_name == NULL)
269 		panic("psycho_attach: unknown model?");
270 	sc->sc_mode = ptype->p_type;
271 
272 	/*
273 	 * The psycho gets three register banks:
274 	 * (0) per-PBM configuration and status registers
275 	 * (1) per-PBM PCI configuration space, containing only the
276 	 *     PBM 256-byte PCI header
277 	 * (2) the shared psycho configuration registers (struct psychoreg)
278 	 *
279 	 * XXX use the prom address for the psycho registers?  we do so far.
280 	 */
281 
282 	/* Register layouts are different.  stuupid. */
283 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO ||
284 	    sc->sc_mode == PSYCHO_MODE_CMU_CH) {
285 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
286 
287 		if (ma->ma_naddress > 2) {
288 			psycho_map_psycho(sc, 0,
289 			    ma->ma_address[2], sizeof(struct psychoreg),
290 			    ma->ma_address[0], sizeof(struct pci_ctl));
291 		} else if (ma->ma_nreg > 2) {
292 			psycho_map_psycho(sc, 1,
293 			    ma->ma_reg[2].ur_paddr, ma->ma_reg[2].ur_len,
294 			    ma->ma_reg[0].ur_paddr, ma->ma_reg[0].ur_len);
295 		} else
296 			panic("psycho_attach: %d not enough registers",
297 			    ma->ma_nreg);
298 	} else {
299 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
300 
301 		if (ma->ma_naddress) {
302 			psycho_map_psycho(sc, 0,
303 			    ma->ma_address[0], sizeof(struct psychoreg),
304 			    ma->ma_address[0] +
305 				offsetof(struct psychoreg, psy_pcictl[0]),
306 			    sizeof(struct pci_ctl));
307 		} else if (ma->ma_nreg) {
308 			psycho_map_psycho(sc, 1,
309 			    ma->ma_reg[0].ur_paddr, ma->ma_reg[0].ur_len,
310 			    ma->ma_reg[0].ur_paddr +
311 				offsetof(struct psychoreg, psy_pcictl[0]),
312 			    sizeof(struct pci_ctl));
313 		} else
314 			panic("psycho_attach: %d not enough registers",
315 			    ma->ma_nreg);
316 	}
317 
318 	csr = psycho_psychoreg_read(sc, psy_csr);
319 	sc->sc_ign = INTMAP_IGN; /* APB IGN is always 0x1f << 6 = 0x7c */
320 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO ||
321 	    sc->sc_mode == PSYCHO_MODE_CMU_CH)
322 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
323 
324 	printf(": %s, impl %d, version %d, ign %x\n", ptype->p_name,
325 	    PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr), sc->sc_ign);
326 
327 	/*
328 	 * Match other psycho's that are already configured against
329 	 * the base physical address. This will be the same for a
330 	 * pair of devices that share register space.
331 	 */
332 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
333 		struct psycho_softc *asc =
334 		    (struct psycho_softc *)psycho_cd.cd_devs[n];
335 
336 		if (asc == NULL || asc == sc)
337 			/* This entry is not there or it is me */
338 			continue;
339 
340 		if (asc->sc_basepaddr != sc->sc_basepaddr)
341 			/* This is an unrelated psycho */
342 			continue;
343 
344 		/* Found partner */
345 		osc = asc;
346 		break;
347 	}
348 
349 	/* Oh, dear.  OK, lets get started */
350 
351 	/*
352 	 * Setup the PCI control register
353 	 */
354 	csr = psycho_pcictl_read(sc, pci_csr);
355 	csr |= PCICTL_MRLM | PCICTL_ARB_PARK | PCICTL_ERRINTEN |
356 	    PCICTL_4ENABLE;
357 	csr &= ~(PCICTL_SERR | PCICTL_CPU_PRIO | PCICTL_ARB_PRIO |
358 	    PCICTL_RTRYWAIT);
359 	psycho_pcictl_write(sc, pci_csr, csr);
360 
361 	/*
362 	 * Allocate our psycho_pbm
363 	 */
364 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF,
365 		M_NOWAIT | M_ZERO);
366 	if (pp == NULL)
367 		panic("could not allocate psycho pbm");
368 
369 	pp->pp_sc = sc;
370 
371 	/* grab the psycho ranges */
372 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
373 
374 	/* get the bus-range for the psycho */
375 	psycho_get_bus_range(sc->sc_node, psycho_br);
376 
377 	bzero(&pba, sizeof(pba));
378 	pba.pba_domain = pci_ndomains++;
379 	pba.pba_bus = psycho_br[0];
380 
381 	printf("%s: bus range %u-%u, PCI bus %d\n", sc->sc_dev.dv_xname,
382 	    psycho_br[0], psycho_br[1], psycho_br[0]);
383 
384 	pp->pp_pcictl = sc->sc_pcictl;
385 
386 	/* allocate our tags */
387 	pp->pp_memt = psycho_alloc_mem_tag(pp);
388 	pp->pp_iot = psycho_alloc_io_tag(pp);
389 	if (sc->sc_mode == PSYCHO_MODE_CMU_CH)
390 		pp->pp_dmat = ma->ma_dmatag;
391 	else
392 		pp->pp_dmat = psycho_alloc_dma_tag(pp);
393 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
394 	                (pp->pp_iot ? PCI_FLAGS_IO_ENABLED  : 0);
395 
396 	/* allocate a chipset for this */
397 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
398 
399 	/* setup the rest of the psycho pbm */
400 	pba.pba_pc = pp->pp_pc;
401 
402 	/*
403 	 * And finally, if we're a sabre or the first of a pair of psycho's to
404 	 * arrive here, start up the IOMMU and get a config space tag.
405 	 */
406 
407 	if (osc == NULL) {
408 		uint64_t timeo;
409 
410 		/* Initialize Starfire PC interrupt translation. */
411 		if (OF_getprop(findroot(), "name", buf, sizeof(buf)) > 0 &&
412 		    strcmp(buf, "SUNW,Ultra-Enterprise-10000") == 0)
413 			starfire_pc_ittrans_init(ma->ma_upaid);
414 
415 		/*
416 		 * Establish handlers for interesting interrupts....
417 		 *
418 		 * XXX We need to remember these and remove this to support
419 		 * hotplug on the UPA/FHC bus.
420 		 *
421 		 * XXX Not all controllers have these, but installing them
422 		 * is better than trying to sort through this mess.
423 		 */
424 		psycho_set_intr(sc, 15, psycho_ue,
425 		    psycho_psychoreg_vaddr(sc, ue_int_map),
426 		    psycho_psychoreg_vaddr(sc, ue_clr_int), "ue");
427 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO ||
428 		    sc->sc_mode == PSYCHO_MODE_SABRE) {
429 			psycho_set_intr(sc, 1, psycho_ce,
430 			    psycho_psychoreg_vaddr(sc, ce_int_map),
431 			    psycho_psychoreg_vaddr(sc, ce_clr_int), "ce");
432 			psycho_set_intr(sc, 15, psycho_bus_a,
433 			    psycho_psychoreg_vaddr(sc, pciaerr_int_map),
434 			    psycho_psychoreg_vaddr(sc, pciaerr_clr_int),
435 			    "bus_a");
436 		}
437 #if 0
438 		psycho_set_intr(sc, 15, psycho_powerfail,
439 		    psycho_psychoreg_vaddr(sc, power_int_map),
440 		    psycho_psychoreg_vaddr(sc, power_clr_int), "powerfail");
441 #endif
442 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO ||
443 		    sc->sc_mode == PSYCHO_MODE_CMU_CH) {
444 			psycho_set_intr(sc, 15, psycho_bus_b,
445 			    psycho_psychoreg_vaddr(sc, pciberr_int_map),
446 			    psycho_psychoreg_vaddr(sc, pciberr_clr_int),
447 			    "bus_b");
448 		}
449 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
450 			psycho_set_intr(sc, 1, psycho_wakeup,
451 			    psycho_psychoreg_vaddr(sc, pwrmgt_int_map),
452 			    psycho_psychoreg_vaddr(sc, pwrmgt_clr_int),
453 			    "wakeup");
454 		}
455 
456 		/*
457 		 * Apparently a number of machines with psycho and psycho+
458 		 * controllers have interrupt latency issues.  We'll try
459 		 * setting the interrupt retry timeout to 0xff which gives us
460 		 * a retry of 3-6 usec (which is what sysio is set to) for the
461 		 * moment, which seems to help alleviate this problem.
462 		 */
463 		timeo = psycho_psychoreg_read(sc, intr_retry_timer);
464 		if (timeo > 0xfff) {
465 #ifdef DEBUG
466 			printf("decreasing interrupt retry timeout "
467 			    "from %lx to 0xff\n", (long)timeo);
468 #endif
469 			psycho_psychoreg_write(sc, intr_retry_timer, 0xff);
470 		}
471 
472 		/*
473 		 * Setup IOMMU and PCI configuration if we're the first
474 		 * of a pair of psycho's to arrive here.
475 		 *
476 		 * We should calculate a TSB size based on the amount of RAM,
477 		 * number of bus controllers, and number and type of child
478 		 * devices.
479 		 *
480 		 * For the moment, 32KB should be more than enough.
481 		 */
482 		sc->sc_is = malloc(sizeof(struct iommu_state),
483 			M_DEVBUF, M_NOWAIT | M_ZERO);
484 		if (sc->sc_is == NULL)
485 			panic("psycho_attach: malloc iommu_state");
486 
487 		if (getproplen(sc->sc_node, "no-streaming-cache") < 0) {
488 			struct strbuf_ctl *sb = &pp->pp_sb;
489 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
490 
491 			/*
492 			 * Initialize the strbuf_ctl.
493 			 *
494 			 * The flush sync buffer must be 64-byte aligned.
495 			 */
496 
497 			sb->sb_flush = (void *)(va & ~0x3f);
498 
499 			sb->sb_bustag = sc->sc_bustag;
500 			if (bus_space_subregion(sc->sc_bustag, sc->sc_pcictl,
501 			    offsetof(struct pci_ctl, pci_strbuf),
502 			    sizeof(struct iommu_strbuf),
503 			    &sb->sb_sb)) {
504 				printf("STC0 subregion failed\n");
505 				sb->sb_flush = 0;
506 			}
507 		}
508 
509 		/* Point out iommu at the strbuf_ctl. */
510 		sc->sc_is->is_sb[0] = &pp->pp_sb;
511 
512 		/* CMU-CH doesn't have an IOMMU. */
513 		if (sc->sc_mode != PSYCHO_MODE_CMU_CH) {
514 			printf("%s: ", sc->sc_dev.dv_xname);
515 			psycho_iommu_init(sc, 2);
516 		}
517 
518 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
519 		if (bus_space_map(sc->sc_configtag,
520 		    sc->sc_basepaddr, 0x01000000, 0, &sc->sc_configaddr))
521 			panic("can't map psycho PCI configuration space");
522 	} else {
523 		/* Just copy IOMMU state, config tag and address */
524 		sc->sc_is = osc->sc_is;
525 		sc->sc_configtag = osc->sc_configtag;
526 		sc->sc_configaddr = osc->sc_configaddr;
527 
528 		if (getproplen(sc->sc_node, "no-streaming-cache") < 0) {
529 			struct strbuf_ctl *sb = &pp->pp_sb;
530 			vaddr_t va = (vaddr_t)&pp->pp_flush[0x40];
531 
532 			/*
533 			 * Initialize the strbuf_ctl.
534 			 *
535 			 * The flush sync buffer must be 64-byte aligned.
536 			 */
537 
538 			sb->sb_flush = (void *)(va & ~0x3f);
539 
540 			sb->sb_bustag = sc->sc_bustag;
541 			if (bus_space_subregion(sc->sc_bustag, sc->sc_pcictl,
542 			    offsetof(struct pci_ctl, pci_strbuf),
543 			    sizeof(struct iommu_strbuf),
544 			    &sb->sb_sb)) {
545 				printf("STC1 subregion failed\n");
546 				sb->sb_flush = 0;
547 			}
548 
549 			/* Point out iommu at the strbuf_ctl. */
550 			sc->sc_is->is_sb[1] = sb;
551 		}
552 
553 		/* Point out iommu at the strbuf_ctl. */
554 		sc->sc_is->is_sb[1] = &pp->pp_sb;
555 
556 		printf("%s: ", sc->sc_dev.dv_xname);
557 		printf("dvma map %x-%x", sc->sc_is->is_dvmabase,
558 		    sc->sc_is->is_dvmaend);
559 #ifdef DEBUG
560 		printf(", iotdb %llx-%llx",
561 		    (unsigned long long)sc->sc_is->is_ptsb,
562 		    (unsigned long long)(sc->sc_is->is_ptsb +
563 		    (PAGE_SIZE << sc->sc_is->is_tsbsize)));
564 #endif
565 		iommu_reset(sc->sc_is);
566 		printf("\n");
567 	}
568 
569 	/*
570 	 * The UltraSPARC IIe has new STICK logic that provides a
571 	 * timebase counter that doesn't scale with processor
572 	 * frequency.  Use it to provide a timecounter.
573 	 */
574 	stick_rate = getpropint(findroot(), "stick-frequency", 0);
575 	if (stick_rate > 0 && sc->sc_mode == PSYCHO_MODE_SABRE) {
576 		stick_timecounter.tc_frequency = stick_rate;
577 		stick_timecounter.tc_priv = sc;
578 		tc_init(&stick_timecounter);
579 	}
580 
581 	/*
582 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
583 	 */
584 	pba.pba_busname = "pci";
585 #if 0
586 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
587 #endif
588 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
589 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
590 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
591 	pba.pba_pc->bustag = sc->sc_configtag;
592 	pba.pba_pc->bushandle = sc->sc_configaddr;
593 	pba.pba_pc->conf_read = psycho_conf_read;
594 	pba.pba_pc->conf_write = psycho_conf_write;
595 	pba.pba_pc->intr_map = psycho_intr_map;
596 
597 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO ||
598 	    sc->sc_mode == PSYCHO_MODE_CMU_CH)
599 		psycho_identify_pbm(sc, pp, &pba);
600 	else
601 		pp->pp_id = PSYCHO_PBM_UNKNOWN;
602 
603 	config_found(self, &pba, psycho_print);
604 }
605 
606 void
607 psycho_identify_pbm(struct psycho_softc *sc, struct psycho_pbm *pp,
608     struct pcibus_attach_args *pa)
609 {
610 	vaddr_t pci_va = (vaddr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_pcictl);
611 	paddr_t pci_pa;
612 
613 	if (pmap_extract(pmap_kernel(), pci_va, &pci_pa) == 0)
614 	    pp->pp_id = PSYCHO_PBM_UNKNOWN;
615 	else switch(pci_pa & 0xffff) {
616 		case 0x2000:
617 			pp->pp_id = PSYCHO_PBM_A;
618 			break;
619 		case 0x4000:
620 			pp->pp_id = PSYCHO_PBM_B;
621 			break;
622 		default:
623 			pp->pp_id = PSYCHO_PBM_UNKNOWN;
624 			break;
625 	}
626 }
627 
628 void
629 psycho_map_psycho(struct psycho_softc* sc, int do_map, bus_addr_t reg_addr,
630     bus_size_t reg_size, bus_addr_t pci_addr, bus_size_t pci_size)
631 {
632 	if (do_map) {
633 		if (bus_space_map(sc->sc_bustag,
634 		    reg_addr, reg_size, 0, &sc->sc_regsh))
635 			panic("psycho_attach: cannot map regs");
636 
637 		if (pci_addr >= reg_addr &&
638 		    pci_addr + pci_size <= reg_addr + reg_size) {
639 			if (bus_space_subregion(sc->sc_bustag, sc->sc_regsh,
640 			    pci_addr - reg_addr, pci_size, &sc->sc_pcictl))
641 				panic("psycho_map_psycho: map ctl");
642 		}
643 		else if (bus_space_map(sc->sc_bustag, pci_addr, pci_size,
644 		    0, &sc->sc_pcictl))
645 			panic("psycho_map_psycho: cannot map pci");
646 	} else {
647 		if (bus_space_map(sc->sc_bustag, reg_addr, reg_size,
648 		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_regsh))
649 			panic("psycho_map_psycho: cannot map ctl");
650 		if (bus_space_map(sc->sc_bustag, pci_addr, pci_size,
651 		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_pcictl))
652 			panic("psycho_map_psycho: cannot map pci");
653 	}
654 }
655 
656 int
657 psycho_print(void *aux, const char *p)
658 {
659 	if (p == NULL)
660 		return (UNCONF);
661 	return (QUIET);
662 }
663 
664 void
665 psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler,
666     u_int64_t *mapper, u_int64_t *clearer, const char *suffix)
667 {
668 	struct intrhand *ih;
669 
670 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT | M_ZERO);
671 	if (ih == NULL)
672 		panic("couldn't malloc intrhand");
673 	ih->ih_arg = sc;
674 	ih->ih_map = mapper;
675 	ih->ih_clr = clearer;
676 	ih->ih_fun = handler;
677 	ih->ih_pil = (1 << ipl);
678 	ih->ih_number = INTVEC(*(ih->ih_map));
679 	snprintf(ih->ih_name, sizeof(ih->ih_name),
680 	    "%s:%s", sc->sc_dev.dv_xname, suffix);
681 
682 	DPRINTF(PDB_INTR, (
683 	    "\ninstalling handler %p arg %p for %s with number %x pil %u",
684 	    ih->ih_fun, ih->ih_arg, sc->sc_dev.dv_xname, ih->ih_number,
685 	    ih->ih_pil));
686 
687 	intr_establish(ipl, ih);
688 }
689 
690 /*
691  * PCI bus support
692  */
693 
694 /*
695  * allocate a PCI chipset tag and set its cookie.
696  */
697 pci_chipset_tag_t
698 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc)
699 {
700 	pci_chipset_tag_t npc;
701 
702 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
703 	if (npc == NULL)
704 		panic("could not allocate pci_chipset_tag_t");
705 	memcpy(npc, pc, sizeof *pc);
706 	npc->cookie = pp;
707 	npc->rootnode = node;
708 
709 	return (npc);
710 }
711 
712 /*
713  * grovel the OBP for various psycho properties
714  */
715 void
716 psycho_get_bus_range(node, brp)
717 	int node;
718 	int *brp;
719 {
720 	int n, error;
721 
722 	error = getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp);
723 	if (error)
724 		panic("could not get psycho bus-range, error %d", error);
725 	if (n != 2)
726 		panic("broken psycho bus-range");
727 	DPRINTF(PDB_PROM,
728 	    ("psycho debug: got `bus-range' for node %08x: %u - %u\n",
729 	    node, brp[0], brp[1]));
730 }
731 
732 void
733 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np)
734 {
735 
736 	if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
737 		panic("could not get psycho ranges");
738 	DPRINTF(PDB_PROM,
739 	    ("psycho debug: got `ranges' for node %08x: %d entries\n",
740 	    node, *np));
741 }
742 
743 /*
744  * Interrupt handlers.
745  */
746 
747 int
748 psycho_ue(void *arg)
749 {
750 	struct psycho_softc *sc = arg;
751 	unsigned long long afsr = psycho_psychoreg_read(sc, psy_ue_afsr);
752 	unsigned long long afar = psycho_psychoreg_read(sc, psy_ue_afar);
753 
754 	/*
755 	 * It's uncorrectable.  Dump the regs and panic.
756 	 */
757 	panic("%s: uncorrectable DMA error AFAR %llx (pa=%lx tte=%llx/%llx) "
758 	    "AFSR %llx", sc->sc_dev.dv_xname, afar,
759 	    iommu_extract(sc->sc_is, (vaddr_t)afar),
760 	    iommu_lookup_tte(sc->sc_is, (vaddr_t)afar),
761 	    iommu_fetch_tte(sc->sc_is, (paddr_t)afar),
762 	    afsr);
763 	return (1);
764 }
765 
766 int
767 psycho_ce(void *arg)
768 {
769 	struct psycho_softc *sc = arg;
770 	u_int64_t afar, afsr;
771 
772 	/*
773 	 * It's correctable.  Dump the regs and continue.
774 	 */
775 
776 	afar = psycho_psychoreg_read(sc, psy_ce_afar);
777 	afsr = psycho_psychoreg_read(sc, psy_ce_afsr);
778 
779 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
780 	    sc->sc_dev.dv_xname, afar, afsr);
781 
782 	/* Clear error. */
783 	psycho_psychoreg_write(sc, psy_ce_afsr,
784 	    afsr & (PSY_CEAFSR_PDRD | PSY_CEAFSR_PDWR |
785 	    PSY_CEAFSR_SDRD | PSY_CEAFSR_SDWR));
786 
787 	return (1);
788 }
789 
790 int
791 psycho_bus_error(struct psycho_softc *sc, int bus)
792 {
793 	u_int64_t afsr, afar, bits;
794 
795 	afar = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afar);
796 	afsr = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afsr);
797 
798 	bits = afsr & (PSY_PCIAFSR_PMA | PSY_PCIAFSR_PTA | PSY_PCIAFSR_PTRY |
799 	    PSY_PCIAFSR_PPERR | PSY_PCIAFSR_SMA | PSY_PCIAFSR_STA |
800 	    PSY_PCIAFSR_STRY | PSY_PCIAFSR_SPERR);
801 
802 	if (bits == 0)
803 		return (0);
804 
805 	/*
806 	 * It's uncorrectable.  Dump the regs and panic.
807 	 */
808 	printf("%s: PCI bus %c error AFAR %llx (pa=%llx) AFSR %llx\n",
809 	    sc->sc_dev.dv_xname, 'A' + bus, (long long)afar,
810 	    (long long)iommu_extract(sc->sc_is, (vaddr_t)afar),
811 	    (long long)afsr);
812 
813 	psycho_psychoreg_write(sc, psy_pcictl[bus].pci_afsr, bits);
814 	return (1);
815 }
816 
817 int
818 psycho_bus_a(void *arg)
819 {
820 	struct psycho_softc *sc = arg;
821 
822 	return (psycho_bus_error(sc, 0));
823 }
824 
825 int
826 psycho_bus_b(void *arg)
827 {
828 	struct psycho_softc *sc = arg;
829 
830 	return (psycho_bus_error(sc, 1));
831 }
832 
833 int
834 psycho_powerfail(void *arg)
835 {
836 	/*
837 	 * We lost power.  Try to shut down NOW.
838 	 */
839 	printf("Power Failure Detected: Shutting down NOW.\n");
840 	boot(RB_POWERDOWN|RB_HALT);
841 	return (1);
842 }
843 
844 int
845 psycho_wakeup(void *arg)
846 {
847 	struct psycho_softc *sc = arg;
848 
849 	/*
850 	 * Gee, we don't really have a framework to deal with this
851 	 * properly.
852 	 */
853 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
854 	return (1);
855 }
856 
857 /*
858  * initialise the IOMMU..
859  */
860 void
861 psycho_iommu_init(struct psycho_softc *sc, int tsbsize)
862 {
863 	struct iommu_state *is = sc->sc_is;
864 	int *vdma = NULL, nitem;
865 	u_int32_t iobase = -1;
866 	char *name;
867 
868 	/* punch in our copies */
869 	is->is_bustag = sc->sc_bustag;
870 	bus_space_subregion(sc->sc_bustag, sc->sc_regsh,
871 	    offsetof(struct psychoreg, psy_iommu), sizeof(struct iommureg),
872 	    &is->is_iommu);
873 
874 	/*
875 	 * Separate the men from the boys.  If it has a `virtual-dma'
876 	 * property, use it.
877 	 */
878 	if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
879 	    (void **)&vdma)) {
880 		/* Damn.  Gotta use these values. */
881 		iobase = vdma[0];
882 #define	TSBCASE(x)	case 1 << ((x) + 23): tsbsize = (x); break
883 		switch (vdma[1]) {
884 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
885 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
886 		default:
887 			printf("bogus tsb size %x, using 7\n", vdma[1]);
888 			TSBCASE(7);
889 		}
890 #undef TSBCASE
891 		DPRINTF(PDB_CONF, ("psycho_iommu_init: iobase=0x%x\n", iobase));
892 		free(vdma, M_DEVBUF);
893 	} else {
894 		DPRINTF(PDB_CONF, ("psycho_iommu_init: getprop failed, "
895 		    "iobase=0x%x, tsbsize=%d\n", iobase, tsbsize));
896 	}
897 
898 	/* give us a nice name.. */
899 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
900 	if (name == NULL)
901 		panic("couldn't malloc iommu name");
902 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
903 
904 	iommu_init(name, is, tsbsize, iobase);
905 }
906 
907 /*
908  * below here is bus space and bus dma support
909  */
910 
911 bus_space_tag_t
912 psycho_alloc_mem_tag(struct psycho_pbm *pp)
913 {
914 	return (psycho_alloc_bus_tag(pp, "mem",
915 	    0x02,	/* 32-bit mem space (where's the #define???) */
916 	    ASI_PRIMARY, ASI_PRIMARY_LITTLE));
917 }
918 
919 bus_space_tag_t
920 psycho_alloc_io_tag(struct psycho_pbm *pp)
921 {
922 	return (psycho_alloc_bus_tag(pp, "io",
923 	    0x01,	/* IO space (where's the #define???) */
924 	    ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED));
925 }
926 
927 bus_space_tag_t
928 psycho_alloc_config_tag(struct psycho_pbm *pp)
929 {
930 	return (psycho_alloc_bus_tag(pp, "cfg",
931 	    0x00,	/* Config space (where's the #define???) */
932 	    ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED));
933 }
934 
935 bus_space_tag_t
936 psycho_alloc_bus_tag(struct psycho_pbm *pp,
937     const char *name, int ss, int asi, int sasi)
938 {
939 	struct psycho_softc *sc = pp->pp_sc;
940 	struct sparc_bus_space_tag *bt;
941 
942 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
943 	if (bt == NULL)
944 		panic("could not allocate psycho bus tag");
945 
946 	snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d-%2.2x)",
947 	    sc->sc_dev.dv_xname, name, ss, asi);
948 
949 	bt->cookie = pp;
950 	bt->parent = sc->sc_bustag;
951 	bt->default_type = ss;
952 	bt->asi = asi;
953 	bt->sasi = sasi;
954 	bt->sparc_bus_map = psycho_bus_map;
955 	bt->sparc_bus_mmap = psycho_bus_mmap;
956 	bt->sparc_bus_addr = psycho_bus_addr;
957 	bt->sparc_intr_establish = psycho_intr_establish;
958 
959 	return (bt);
960 }
961 
962 bus_dma_tag_t
963 psycho_alloc_dma_tag(struct psycho_pbm *pp)
964 {
965 	struct psycho_softc *sc = pp->pp_sc;
966 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
967 
968 	dt = (bus_dma_tag_t)malloc(sizeof(struct sparc_bus_dma_tag),
969 	    M_DEVBUF, M_NOWAIT | M_ZERO);
970 	if (dt == NULL)
971 		panic("could not allocate psycho dma tag");
972 
973 	dt->_cookie = pp;
974 	dt->_parent = pdt;
975 	dt->_dmamap_create	= psycho_dmamap_create;
976 	dt->_dmamap_destroy	= iommu_dvmamap_destroy;
977 	dt->_dmamap_load	= iommu_dvmamap_load;
978 	dt->_dmamap_load_raw	= iommu_dvmamap_load_raw;
979 	dt->_dmamap_unload	= iommu_dvmamap_unload;
980 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
981 		dt->_dmamap_sync = iommu_dvmamap_sync;
982 	else
983 		dt->_dmamap_sync = psycho_sabre_dvmamap_sync;
984 	dt->_dmamem_alloc	= iommu_dvmamem_alloc;
985 	dt->_dmamem_free	= iommu_dvmamem_free;
986 
987 	return (dt);
988 }
989 
990 /*
991  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
992  * PCI physical addresses.
993  */
994 
995 int
996 psycho_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t offset,
997     bus_size_t size, int flags, bus_space_handle_t *hp)
998 {
999 	struct psycho_pbm *pp = t->cookie;
1000 	int i, ss;
1001 
1002 	DPRINTF(PDB_BUSMAP, ("\npsycho_bus_map: type %d off %qx sz %qx "
1003 	    "flags %d", t->default_type, (unsigned long long)offset,
1004 	    (unsigned long long)size, flags));
1005 
1006 	ss = t->default_type;
1007 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
1008 
1009 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
1010 		printf("\npsycho_bus_map: invalid parent");
1011 		return (EINVAL);
1012 	}
1013 
1014 	t = t->parent;
1015 
1016 	if (flags & BUS_SPACE_MAP_PROMADDRESS) {
1017 		return ((*t->sparc_bus_map)
1018 		    (t, t0, offset, size, flags, hp));
1019 	}
1020 
1021 	for (i = 0; i < pp->pp_nrange; i++) {
1022 		bus_addr_t paddr;
1023 
1024 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1025 			continue;
1026 
1027 		paddr = pp->pp_range[i].phys_lo + offset;
1028 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi << 32);
1029 		DPRINTF(PDB_BUSMAP,
1030 		    ("\n_psycho_bus_map: mapping paddr space %lx offset %lx "
1031 			"paddr %qx",
1032 		    (long)ss, (long)offset,
1033 		    (unsigned long long)paddr));
1034 		return ((*t->sparc_bus_map)(t, t0, paddr, size, flags, hp));
1035 	}
1036 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
1037 	return (EINVAL);
1038 }
1039 
1040 paddr_t
1041 psycho_bus_mmap(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t paddr,
1042     off_t off, int prot, int flags)
1043 {
1044 	bus_addr_t offset = paddr;
1045 	struct psycho_pbm *pp = t->cookie;
1046 	int i, ss;
1047 
1048 	ss = t->default_type;
1049 
1050 	DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: prot %d flags %d pa %qx",
1051 	    prot, flags, (unsigned long long)paddr));
1052 
1053 	if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) {
1054 		printf("\npsycho_bus_mmap: invalid parent");
1055 		return (-1);
1056 	}
1057 
1058 	t = t->parent;
1059 
1060 	for (i = 0; i < pp->pp_nrange; i++) {
1061 		bus_addr_t paddr;
1062 
1063 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1064 			continue;
1065 
1066 		paddr = pp->pp_range[i].phys_lo + offset;
1067 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi << 32);
1068 		DPRINTF(PDB_BUSMAP, ("\npsycho_bus_mmap: mapping paddr "
1069 		    "space %lx offset %lx paddr %qx",
1070 		    (long)ss, (long)offset,
1071 		    (unsigned long long)paddr));
1072 		return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags));
1073 	}
1074 
1075 	return (-1);
1076 }
1077 
1078 bus_addr_t
1079 psycho_bus_addr(bus_space_tag_t t, bus_space_tag_t t0, bus_space_handle_t h)
1080 {
1081 	struct psycho_pbm *pp = t->cookie;
1082 	bus_addr_t addr;
1083 	int i, ss;
1084 
1085 	ss = t->default_type;
1086 
1087 	if (t->parent == 0 || t->parent->sparc_bus_addr == 0) {
1088 		printf("\npsycho_bus_addr: invalid parent");
1089 		return (-1);
1090 	}
1091 
1092 	t = t->parent;
1093 
1094 	addr = ((*t->sparc_bus_addr)(t, t0, h));
1095 	if (addr == -1)
1096 		return (-1);
1097 
1098 	for (i = 0; i < pp->pp_nrange; i++) {
1099 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1100 			continue;
1101 
1102 		return (BUS_ADDR_PADDR(addr) - pp->pp_range[i].phys_lo);
1103 	}
1104 
1105 	return (-1);
1106 }
1107 
1108 
1109 pcireg_t
1110 psycho_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
1111 {
1112 	return (bus_space_read_4(pc->bustag, pc->bushandle,
1113 	    PCITAG_OFFSET(tag) + reg));
1114 }
1115 
1116 void
1117 psycho_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
1118 {
1119         bus_space_write_4(pc->bustag, pc->bushandle,
1120 	    PCITAG_OFFSET(tag) + reg, data);
1121 }
1122 
1123 /*
1124  * Bus-specific interrupt mapping
1125  */
1126 int
1127 psycho_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
1128 {
1129 	struct psycho_pbm *pp = pa->pa_pc->cookie;
1130 	struct psycho_softc *sc = pp->pp_sc;
1131 	u_int dev;
1132 
1133 	if (*ihp != (pci_intr_handle_t)-1) {
1134 		*ihp |= sc->sc_ign;
1135 		return (0);
1136 	}
1137 
1138 	/*
1139 	 * We didn't find a PROM mapping for this interrupt.  Try to
1140 	 * construct one ourselves based on the swizzled interrupt pin
1141 	 * and the interrupt mapping for PCI slots documented in the
1142 	 * UltraSPARC-IIi User's Manual.
1143 	 */
1144 
1145 	if (pa->pa_intrpin == 0)
1146 		return (-1);
1147 
1148 	/*
1149 	 * This deserves some documentation.  Should anyone
1150 	 * have anything official looking, please speak up.
1151 	 */
1152 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
1153 	    pp->pp_id == PSYCHO_PBM_B)
1154 		dev = PCITAG_DEV(pa->pa_intrtag) - 2;
1155 	else
1156 		dev = PCITAG_DEV(pa->pa_intrtag) - 1;
1157 
1158 	*ihp = (pa->pa_intrpin - 1) & INTMAP_PCIINT;
1159 	*ihp |= ((pp->pp_id == PSYCHO_PBM_B) ? INTMAP_PCIBUS : 0);
1160 	*ihp |= (dev << 2) & INTMAP_PCISLOT;
1161 	*ihp |= sc->sc_ign;
1162 
1163 	return (0);
1164 }
1165 
1166 /*
1167  * install an interrupt handler for a PCI device
1168  */
1169 void *
1170 psycho_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
1171     int level, int flags, int (*handler)(void *), void *arg, const char *what)
1172 {
1173 	struct psycho_pbm *pp = t->cookie;
1174 	struct psycho_softc *sc = pp->pp_sc;
1175 	struct intrhand *ih;
1176 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
1177 	int64_t intrmap = 0;
1178 	int ino;
1179 	long vec = INTVEC(ihandle);
1180 
1181 	/*
1182 	 * Hunt through all the interrupt mapping regs to look for our
1183 	 * interrupt vector.
1184 	 *
1185 	 * XXX We only compare INOs rather than IGNs since the firmware may
1186 	 * not provide the IGN and the IGN is constant for all device on that
1187 	 * PCI controller.  This could cause problems for the FFB/external
1188 	 * interrupt which has a full vector that can be set arbitrarily.
1189 	 */
1190 
1191 	DPRINTF(PDB_INTR,
1192 	    ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
1193 	ino = INTINO(vec);
1194 	DPRINTF(PDB_INTR, (" ino %x", ino));
1195 
1196 	/* If the device didn't ask for an IPL, use the one encoded. */
1197 	if (level == IPL_NONE)
1198 		level = INTLEV(vec);
1199 	/* If it still has no level, print a warning and assign IPL 2 */
1200 	if (level == IPL_NONE) {
1201 		printf("ERROR: no IPL, setting IPL 2.\n");
1202 		level = 2;
1203 	}
1204 
1205 	if (flags & BUS_INTR_ESTABLISH_SOFTINTR)
1206 		goto found;
1207 
1208 	DPRINTF(PDB_INTR,
1209 	    ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1210 	    (long)ino, intrlev[ino]));
1211 
1212 	/*
1213 	 * First look for PCI interrupts, otherwise the PCI A slot 0
1214 	 * INTA# interrupt might match an unused non-PCI (obio)
1215 	 * interrupt.
1216 	 */
1217 
1218 	for (intrmapptr = psycho_psychoreg_vaddr(sc, pcia_slot0_int),
1219 	    intrclrptr = psycho_psychoreg_vaddr(sc, pcia0_clr_int[0]);
1220 	    intrmapptr <= (volatile u_int64_t *)
1221 		psycho_psychoreg_vaddr(sc, pcib_slot3_int);
1222 	    intrmapptr++, intrclrptr += 4) {
1223 		/* Skip PCI-A Slot 2 and PCI-A Slot 3 on psycho's */
1224 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
1225 		    (intrmapptr ==
1226 			psycho_psychoreg_vaddr(sc, pcia_slot2_int) ||
1227 		    intrmapptr ==
1228 			psycho_psychoreg_vaddr(sc, pcia_slot3_int)))
1229 			continue;
1230 
1231 		if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1232 			intrclrptr += vec & 0x3;
1233 			goto found;
1234 		}
1235 	}
1236 
1237 	/* Now hunt through obio.  */
1238 	for (intrmapptr = psycho_psychoreg_vaddr(sc, scsi_int_map),
1239 	    intrclrptr = psycho_psychoreg_vaddr(sc, scsi_clr_int);
1240 	    intrmapptr < (volatile u_int64_t *)
1241 		psycho_psychoreg_vaddr(sc, ffb0_int_map);
1242 	    intrmapptr++, intrclrptr++) {
1243 		if (INTINO(*intrmapptr) == ino)
1244 			goto found;
1245 	}
1246 
1247 	printf("Cannot find interrupt vector %lx\n", vec);
1248 	return (NULL);
1249 
1250 found:
1251 	ih = bus_intr_allocate(t0, handler, arg, ino | sc->sc_ign, level,
1252 	    intrmapptr, intrclrptr, what);
1253 	if (ih == NULL) {
1254 		printf("Cannot allocate interrupt vector %lx\n", vec);
1255 		return (NULL);
1256 	}
1257 
1258 	DPRINTF(PDB_INTR, (
1259 	    "\ninstalling handler %p arg %p with number %x pil %u",
1260 	    ih->ih_fun, ih->ih_arg, ih->ih_number, ih->ih_pil));
1261 
1262 	intr_establish(ih->ih_pil, ih);
1263 
1264 	/*
1265 	 * Enable the interrupt now we have the handler installed.
1266 	 * Read the current value as we can't change it besides the
1267 	 * valid bit so so make sure only this bit is changed.
1268 	 *
1269 	 * XXXX --- we really should use bus_space for this.
1270 	 */
1271 	if (intrmapptr) {
1272 		intrmap = *intrmapptr;
1273 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1274 			(unsigned long long)intrmap));
1275 
1276 		/* Enable the interrupt */
1277 		intrmap |= INTMAP_V;
1278 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1279 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx",
1280 			(unsigned long long)intrmap));
1281 		*intrmapptr = intrmap;
1282 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1283 			(unsigned long long)(intrmap = *intrmapptr)));
1284 	}
1285 	return (ih);
1286 }
1287 
1288 /*
1289  * hooks into the iommu dvma calls.
1290  */
1291 int
1292 psycho_dmamap_create(bus_dma_tag_t t, bus_dma_tag_t t0, bus_size_t size,
1293     int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
1294     bus_dmamap_t *dmamp)
1295 {
1296 	struct psycho_pbm *pp = t->_cookie;
1297 
1298 	return (iommu_dvmamap_create(t, t0, &pp->pp_sb, size, nsegments,
1299 	    maxsegsz, boundary, flags, dmamp));
1300 }
1301 
1302 void
1303 psycho_sabre_dvmamap_sync(bus_dma_tag_t t, bus_dma_tag_t t0, bus_dmamap_t map,
1304     bus_size_t offset, bus_size_t len, int ops)
1305 {
1306 	struct psycho_pbm *pp = t->_cookie;
1307 	struct psycho_softc *sc = pp->pp_sc;
1308 
1309 	if (ops & BUS_DMASYNC_POSTREAD)
1310 		psycho_psychoreg_read(sc, pci_dma_write_sync);
1311 
1312 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE))
1313 		membar(MemIssue);
1314 }
1315 
1316 u_int
1317 stick_get_timecount(struct timecounter *tc)
1318 {
1319 	struct psycho_softc *sc = tc->tc_priv;
1320 
1321 	return psycho_psychoreg_read(sc, stick_reg_low);
1322 }
1323