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