xref: /openbsd-src/sys/arch/sparc64/dev/psycho.c (revision 8550894424f8a4aa4aafb6cd57229dd6ed7cd9dd)
1 /*	$OpenBSD: psycho.c,v 1.81 2022/10/16 01:22:39 jsg 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 lets the IIi handle two independent 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(int node, int *brp)
725 {
726 	int n, error;
727 
728 	error = getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp);
729 	if (error)
730 		panic("could not get psycho bus-range, error %d", error);
731 	if (n != 2)
732 		panic("broken psycho bus-range");
733 	DPRINTF(PDB_PROM,
734 	    ("psycho debug: got `bus-range' for node %08x: %u - %u\n",
735 	    node, brp[0], brp[1]));
736 }
737 
738 void
739 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np)
740 {
741 
742 	if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
743 		panic("could not get psycho ranges");
744 	DPRINTF(PDB_PROM,
745 	    ("psycho debug: got `ranges' for node %08x: %d entries\n",
746 	    node, *np));
747 }
748 
749 /*
750  * Interrupt handlers.
751  */
752 
753 int
754 psycho_ue(void *arg)
755 {
756 	struct psycho_softc *sc = arg;
757 	unsigned long long afsr = psycho_psychoreg_read(sc, psy_ue_afsr);
758 	unsigned long long afar = psycho_psychoreg_read(sc, psy_ue_afar);
759 
760 	/*
761 	 * It's uncorrectable.  Dump the regs and panic.
762 	 */
763 	panic("%s: uncorrectable DMA error AFAR %llx (pa=%lx tte=%llx/%llx) "
764 	    "AFSR %llx", sc->sc_dev.dv_xname, afar,
765 	    iommu_extract(sc->sc_is, (vaddr_t)afar),
766 	    iommu_lookup_tte(sc->sc_is, (vaddr_t)afar),
767 	    iommu_fetch_tte(sc->sc_is, (paddr_t)afar),
768 	    afsr);
769 	return (1);
770 }
771 
772 int
773 psycho_ce(void *arg)
774 {
775 	struct psycho_softc *sc = arg;
776 	u_int64_t afar, afsr;
777 
778 	/*
779 	 * It's correctable.  Dump the regs and continue.
780 	 */
781 
782 	afar = psycho_psychoreg_read(sc, psy_ce_afar);
783 	afsr = psycho_psychoreg_read(sc, psy_ce_afsr);
784 
785 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
786 	    sc->sc_dev.dv_xname, afar, afsr);
787 
788 	/* Clear error. */
789 	psycho_psychoreg_write(sc, psy_ce_afsr,
790 	    afsr & (PSY_CEAFSR_PDRD | PSY_CEAFSR_PDWR |
791 	    PSY_CEAFSR_SDRD | PSY_CEAFSR_SDWR));
792 
793 	return (1);
794 }
795 
796 int
797 psycho_bus_error(struct psycho_softc *sc, int bus)
798 {
799 	u_int64_t afsr, afar, bits;
800 
801 	afar = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afar);
802 	afsr = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afsr);
803 
804 	bits = afsr & (PSY_PCIAFSR_PMA | PSY_PCIAFSR_PTA | PSY_PCIAFSR_PTRY |
805 	    PSY_PCIAFSR_PPERR | PSY_PCIAFSR_SMA | PSY_PCIAFSR_STA |
806 	    PSY_PCIAFSR_STRY | PSY_PCIAFSR_SPERR);
807 
808 	if (bits == 0)
809 		return (0);
810 
811 	/*
812 	 * It's uncorrectable.  Dump the regs and panic.
813 	 */
814 	printf("%s: PCI bus %c error AFAR %llx (pa=%llx) AFSR %llx\n",
815 	    sc->sc_dev.dv_xname, 'A' + bus, (long long)afar,
816 	    (long long)iommu_extract(sc->sc_is, (vaddr_t)afar),
817 	    (long long)afsr);
818 
819 	psycho_psychoreg_write(sc, psy_pcictl[bus].pci_afsr, bits);
820 	return (1);
821 }
822 
823 int
824 psycho_bus_a(void *arg)
825 {
826 	struct psycho_softc *sc = arg;
827 
828 	return (psycho_bus_error(sc, 0));
829 }
830 
831 int
832 psycho_bus_b(void *arg)
833 {
834 	struct psycho_softc *sc = arg;
835 
836 	return (psycho_bus_error(sc, 1));
837 }
838 
839 int
840 psycho_powerfail(void *arg)
841 {
842 	/*
843 	 * We lost power.  Try to shut down NOW.
844 	 */
845 	panic("Power Failure Detected");
846 	/* NOTREACHED */
847 	return (1);
848 }
849 
850 int
851 psycho_wakeup(void *arg)
852 {
853 	struct psycho_softc *sc = arg;
854 
855 	/*
856 	 * Gee, we don't really have a framework to deal with this
857 	 * properly.
858 	 */
859 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
860 	return (1);
861 }
862 
863 /*
864  * initialise the IOMMU..
865  */
866 void
867 psycho_iommu_init(struct psycho_softc *sc, int tsbsize)
868 {
869 	struct iommu_state *is = sc->sc_is;
870 	int *vdma = NULL, nitem;
871 	u_int32_t iobase = -1;
872 	char *name;
873 
874 	/* punch in our copies */
875 	is->is_bustag = sc->sc_bustag;
876 	bus_space_subregion(sc->sc_bustag, sc->sc_regsh,
877 	    offsetof(struct psychoreg, psy_iommu), sizeof(struct iommureg),
878 	    &is->is_iommu);
879 
880 	/*
881 	 * Separate the men from the boys.  If it has a `virtual-dma'
882 	 * property, use it.
883 	 */
884 	if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
885 	    (void **)&vdma)) {
886 		/* Damn.  Gotta use these values. */
887 		iobase = vdma[0];
888 #define	TSBCASE(x)	case 1 << ((x) + 23): tsbsize = (x); break
889 		switch (vdma[1]) {
890 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
891 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
892 		default:
893 			printf("bogus tsb size %x, using 7\n", vdma[1]);
894 			TSBCASE(7);
895 		}
896 #undef TSBCASE
897 		DPRINTF(PDB_CONF, ("psycho_iommu_init: iobase=0x%x\n", iobase));
898 		free(vdma, M_DEVBUF, 0);
899 	} else {
900 		DPRINTF(PDB_CONF, ("psycho_iommu_init: getprop failed, "
901 		    "iobase=0x%x, tsbsize=%d\n", iobase, tsbsize));
902 	}
903 
904 	/* give us a nice name.. */
905 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
906 	if (name == NULL)
907 		panic("couldn't malloc iommu name");
908 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
909 
910 	iommu_init(name, &iommu_hw_default, is, tsbsize, iobase);
911 }
912 
913 /*
914  * below here is bus space and bus dma support
915  */
916 
917 bus_space_tag_t
918 psycho_alloc_mem_tag(struct psycho_pbm *pp)
919 {
920 	return (psycho_alloc_bus_tag(pp, "mem",
921 	    0x02,	/* 32-bit mem space (where's the #define???) */
922 	    ASI_PRIMARY, ASI_PRIMARY_LITTLE));
923 }
924 
925 bus_space_tag_t
926 psycho_alloc_io_tag(struct psycho_pbm *pp)
927 {
928 	return (psycho_alloc_bus_tag(pp, "io",
929 	    0x01,	/* IO space (where's the #define???) */
930 	    ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED));
931 }
932 
933 bus_space_tag_t
934 psycho_alloc_config_tag(struct psycho_pbm *pp)
935 {
936 	return (psycho_alloc_bus_tag(pp, "cfg",
937 	    0x00,	/* Config space (where's the #define???) */
938 	    ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED));
939 }
940 
941 bus_space_tag_t
942 psycho_alloc_bus_tag(struct psycho_pbm *pp,
943     const char *name, int ss, int asi, int sasi)
944 {
945 	struct psycho_softc *sc = pp->pp_sc;
946 	struct sparc_bus_space_tag *bt;
947 
948 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
949 	if (bt == NULL)
950 		panic("could not allocate psycho bus tag");
951 
952 	snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d-%2.2x)",
953 	    sc->sc_dev.dv_xname, name, ss, asi);
954 
955 	bt->cookie = pp;
956 	bt->parent = sc->sc_bustag;
957 	bt->default_type = ss;
958 	bt->asi = asi;
959 	bt->sasi = sasi;
960 	bt->sparc_bus_map = psycho_bus_map;
961 	bt->sparc_bus_mmap = psycho_bus_mmap;
962 	bt->sparc_bus_addr = psycho_bus_addr;
963 	bt->sparc_intr_establish = psycho_intr_establish;
964 
965 	return (bt);
966 }
967 
968 bus_dma_tag_t
969 psycho_alloc_dma_tag(struct psycho_pbm *pp)
970 {
971 	struct psycho_softc *sc = pp->pp_sc;
972 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
973 
974 	dt = (bus_dma_tag_t)malloc(sizeof(struct sparc_bus_dma_tag),
975 	    M_DEVBUF, M_NOWAIT | M_ZERO);
976 	if (dt == NULL)
977 		panic("could not allocate psycho dma tag");
978 
979 	dt->_cookie = pp;
980 	dt->_parent = pdt;
981 	dt->_dmamap_create	= psycho_dmamap_create;
982 	dt->_dmamap_destroy	= iommu_dvmamap_destroy;
983 	dt->_dmamap_load	= iommu_dvmamap_load;
984 	dt->_dmamap_load_raw	= iommu_dvmamap_load_raw;
985 	dt->_dmamap_unload	= iommu_dvmamap_unload;
986 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
987 		dt->_dmamap_sync = iommu_dvmamap_sync;
988 	else
989 		dt->_dmamap_sync = psycho_sabre_dvmamap_sync;
990 	dt->_dmamem_alloc	= iommu_dvmamem_alloc;
991 	dt->_dmamem_free	= iommu_dvmamem_free;
992 
993 	return (dt);
994 }
995 
996 /*
997  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
998  * PCI physical addresses.
999  */
1000 
1001 int
1002 psycho_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t offset,
1003     bus_size_t size, int flags, bus_space_handle_t *hp)
1004 {
1005 	struct psycho_pbm *pp = t->cookie;
1006 	int i, ss;
1007 
1008 	DPRINTF(PDB_BUSMAP, ("\npsycho_bus_map: type %d off %llx sz %llx "
1009 	    "flags %d", t->default_type, (unsigned long long)offset,
1010 	    (unsigned long long)size, flags));
1011 
1012 	ss = t->default_type;
1013 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
1014 
1015 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
1016 		printf("\npsycho_bus_map: invalid parent");
1017 		return (EINVAL);
1018 	}
1019 
1020 	t = t->parent;
1021 
1022 	if (flags & BUS_SPACE_MAP_PROMADDRESS) {
1023 		return ((*t->sparc_bus_map)
1024 		    (t, t0, offset, size, flags, hp));
1025 	}
1026 
1027 	for (i = 0; i < pp->pp_nrange; i++) {
1028 		bus_addr_t paddr;
1029 
1030 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1031 			continue;
1032 
1033 		paddr = pp->pp_range[i].phys_lo + offset;
1034 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi) << 32;
1035 		DPRINTF(PDB_BUSMAP,
1036 		    ("\n_psycho_bus_map: mapping paddr space %lx offset %lx "
1037 			"paddr %llx",
1038 		    (long)ss, (long)offset,
1039 		    (unsigned long long)paddr));
1040 		return ((*t->sparc_bus_map)(t, t0, paddr, size, flags, hp));
1041 	}
1042 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
1043 	return (EINVAL);
1044 }
1045 
1046 paddr_t
1047 psycho_bus_mmap(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t paddr,
1048     off_t off, int prot, int flags)
1049 {
1050 	bus_addr_t offset = paddr;
1051 	struct psycho_pbm *pp = t->cookie;
1052 	int i, ss;
1053 
1054 	ss = t->default_type;
1055 
1056 	DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: prot %d flags %d pa %llx",
1057 	    prot, flags, (unsigned long long)paddr));
1058 
1059 	if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) {
1060 		printf("\npsycho_bus_mmap: invalid parent");
1061 		return (-1);
1062 	}
1063 
1064 	t = t->parent;
1065 
1066 	for (i = 0; i < pp->pp_nrange; i++) {
1067 		bus_addr_t paddr;
1068 
1069 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1070 			continue;
1071 
1072 		paddr = pp->pp_range[i].phys_lo + offset;
1073 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi) << 32;
1074 		DPRINTF(PDB_BUSMAP, ("\npsycho_bus_mmap: mapping paddr "
1075 		    "space %lx offset %lx paddr %llx",
1076 		    (long)ss, (long)offset,
1077 		    (unsigned long long)paddr));
1078 		return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags));
1079 	}
1080 
1081 	return (-1);
1082 }
1083 
1084 bus_addr_t
1085 psycho_bus_addr(bus_space_tag_t t, bus_space_tag_t t0, bus_space_handle_t h)
1086 {
1087 	struct psycho_pbm *pp = t->cookie;
1088 	bus_addr_t addr;
1089 	int i, ss;
1090 
1091 	ss = t->default_type;
1092 
1093 	if (t->parent == 0 || t->parent->sparc_bus_addr == 0) {
1094 		printf("\npsycho_bus_addr: invalid parent");
1095 		return (-1);
1096 	}
1097 
1098 	t = t->parent;
1099 
1100 	addr = ((*t->sparc_bus_addr)(t, t0, h));
1101 	if (addr == -1)
1102 		return (-1);
1103 
1104 	for (i = 0; i < pp->pp_nrange; i++) {
1105 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
1106 			continue;
1107 
1108 		return (BUS_ADDR_PADDR(addr) - pp->pp_range[i].phys_lo);
1109 	}
1110 
1111 	return (-1);
1112 }
1113 
1114 int
1115 psycho_conf_size(pci_chipset_tag_t pc, pcitag_t tag)
1116 {
1117 	return PCI_CONFIG_SPACE_SIZE;
1118 }
1119 
1120 pcireg_t
1121 psycho_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
1122 {
1123 	struct cpu_info *ci = curcpu();
1124 	pcireg_t val;
1125 	int s;
1126 
1127 	s = splhigh();
1128 	__membar("#Sync");
1129 	ci->ci_pci_probe = 1;
1130 	val = bus_space_read_4(pc->bustag, pc->bushandle,
1131 	    PCITAG_OFFSET(tag) + reg);
1132 	__membar("#Sync");
1133 	if (ci->ci_pci_fault)
1134 		val = 0xffffffff;
1135 	ci->ci_pci_probe = ci->ci_pci_fault = 0;
1136 	splx(s);
1137 
1138 	return (val);
1139 }
1140 
1141 void
1142 psycho_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
1143 {
1144         bus_space_write_4(pc->bustag, pc->bushandle,
1145 	    PCITAG_OFFSET(tag) + reg, data);
1146 }
1147 
1148 /*
1149  * Bus-specific interrupt mapping
1150  */
1151 int
1152 psycho_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
1153 {
1154 	struct psycho_pbm *pp = pa->pa_pc->cookie;
1155 	struct psycho_softc *sc = pp->pp_sc;
1156 	u_int dev;
1157 
1158 	if (*ihp != (pci_intr_handle_t)-1) {
1159 		*ihp |= sc->sc_ign;
1160 		return (0);
1161 	}
1162 
1163 	/*
1164 	 * We didn't find a PROM mapping for this interrupt.  Try to
1165 	 * construct one ourselves based on the swizzled interrupt pin
1166 	 * and the interrupt mapping for PCI slots documented in the
1167 	 * UltraSPARC-IIi User's Manual.
1168 	 */
1169 
1170 	if (pa->pa_intrpin == 0)
1171 		return (-1);
1172 
1173 	/*
1174 	 * This deserves some documentation.  Should anyone
1175 	 * have anything official looking, please speak up.
1176 	 */
1177 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
1178 	    pp->pp_id == PSYCHO_PBM_B)
1179 		dev = PCITAG_DEV(pa->pa_intrtag) - 2;
1180 	else
1181 		dev = PCITAG_DEV(pa->pa_intrtag) - 1;
1182 
1183 	*ihp = (pa->pa_intrpin - 1) & INTMAP_PCIINT;
1184 	*ihp |= ((pp->pp_id == PSYCHO_PBM_B) ? INTMAP_PCIBUS : 0);
1185 	*ihp |= (dev << 2) & INTMAP_PCISLOT;
1186 	*ihp |= sc->sc_ign;
1187 
1188 	return (0);
1189 }
1190 
1191 /*
1192  * install an interrupt handler for a PCI device
1193  */
1194 void *
1195 psycho_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
1196     int level, int flags, int (*handler)(void *), void *arg, const char *what)
1197 {
1198 	struct psycho_pbm *pp = t->cookie;
1199 	struct psycho_softc *sc = pp->pp_sc;
1200 	struct intrhand *ih;
1201 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
1202 	int64_t intrmap = 0;
1203 	int ino;
1204 	long vec = INTVEC(ihandle);
1205 
1206 	/*
1207 	 * Hunt through all the interrupt mapping regs to look for our
1208 	 * interrupt vector.
1209 	 *
1210 	 * XXX We only compare INOs rather than IGNs since the firmware may
1211 	 * not provide the IGN and the IGN is constant for all device on that
1212 	 * PCI controller.  This could cause problems for the FFB/external
1213 	 * interrupt which has a full vector that can be set arbitrarily.
1214 	 */
1215 
1216 	DPRINTF(PDB_INTR,
1217 	    ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
1218 	ino = INTINO(vec);
1219 	DPRINTF(PDB_INTR, (" ino %x", ino));
1220 
1221 	/* If the device didn't ask for an IPL, use the one encoded. */
1222 	if (level == IPL_NONE)
1223 		level = INTLEV(vec);
1224 	/* If it still has no level, print a warning and assign IPL 2 */
1225 	if (level == IPL_NONE) {
1226 		printf("ERROR: no IPL, setting IPL 2.\n");
1227 		level = 2;
1228 	}
1229 
1230 	if (flags & BUS_INTR_ESTABLISH_SOFTINTR)
1231 		goto found;
1232 
1233 	DPRINTF(PDB_INTR,
1234 	    ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
1235 	    (long)ino, intrlev[ino]));
1236 
1237 	/*
1238 	 * First look for PCI interrupts, otherwise the PCI A slot 0
1239 	 * INTA# interrupt might match an unused non-PCI (obio)
1240 	 * interrupt.
1241 	 */
1242 
1243 	for (intrmapptr = psycho_psychoreg_vaddr(sc, pcia_slot0_int),
1244 	    intrclrptr = psycho_psychoreg_vaddr(sc, pcia0_clr_int[0]);
1245 	    intrmapptr <= (volatile u_int64_t *)
1246 		psycho_psychoreg_vaddr(sc, pcib_slot3_int);
1247 	    intrmapptr++, intrclrptr += 4) {
1248 		/* Skip PCI-A Slot 2 and PCI-A Slot 3 on psycho's */
1249 		if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
1250 		    (intrmapptr ==
1251 			psycho_psychoreg_vaddr(sc, pcia_slot2_int) ||
1252 		    intrmapptr ==
1253 			psycho_psychoreg_vaddr(sc, pcia_slot3_int)))
1254 			continue;
1255 
1256 		if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1257 			intrclrptr += vec & 0x3;
1258 			goto found;
1259 		}
1260 	}
1261 
1262 	/* Now hunt through obio.  */
1263 	for (intrmapptr = psycho_psychoreg_vaddr(sc, scsi_int_map),
1264 	    intrclrptr = psycho_psychoreg_vaddr(sc, scsi_clr_int);
1265 	    intrmapptr < (volatile u_int64_t *)
1266 		psycho_psychoreg_vaddr(sc, ffb0_int_map);
1267 	    intrmapptr++, intrclrptr++) {
1268 		if (INTINO(*intrmapptr) == ino)
1269 			goto found;
1270 	}
1271 
1272 	printf("Cannot find interrupt vector %lx\n", vec);
1273 	return (NULL);
1274 
1275 found:
1276 	ih = bus_intr_allocate(t0, handler, arg, ino | sc->sc_ign, level,
1277 	    intrmapptr, intrclrptr, what);
1278 	if (ih == NULL) {
1279 		printf("Cannot allocate interrupt vector %lx\n", vec);
1280 		return (NULL);
1281 	}
1282 
1283 	DPRINTF(PDB_INTR, (
1284 	    "\ninstalling handler %p arg %p with number %x pil %u",
1285 	    ih->ih_fun, ih->ih_arg, ih->ih_number, ih->ih_pil));
1286 
1287 	if (flags & BUS_INTR_ESTABLISH_MPSAFE)
1288 		ih->ih_mpsafe = 1;
1289 
1290 	intr_establish(ih->ih_pil, ih);
1291 
1292 	/*
1293 	 * Enable the interrupt now we have the handler installed.
1294 	 * Read the current value as we can't change it besides the
1295 	 * valid bit so so make sure only this bit is changed.
1296 	 *
1297 	 * XXXX --- we really should use bus_space for this.
1298 	 */
1299 	if (intrmapptr) {
1300 		intrmap = *intrmapptr;
1301 		DPRINTF(PDB_INTR, ("; read intrmap = %016llx",
1302 			(unsigned long long)intrmap));
1303 
1304 		/* Enable the interrupt */
1305 		intrmap |= INTMAP_V;
1306 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1307 		DPRINTF(PDB_INTR, ("; writing intrmap = %016llx",
1308 			(unsigned long long)intrmap));
1309 		*intrmapptr = intrmap;
1310 		DPRINTF(PDB_INTR, ("; reread intrmap = %016llx",
1311 			(unsigned long long)(intrmap = *intrmapptr)));
1312 	}
1313 	return (ih);
1314 }
1315 
1316 /*
1317  * hooks into the iommu dvma calls.
1318  */
1319 int
1320 psycho_dmamap_create(bus_dma_tag_t t, bus_dma_tag_t t0, bus_size_t size,
1321     int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
1322     bus_dmamap_t *dmamp)
1323 {
1324 	struct psycho_pbm *pp = t->_cookie;
1325 
1326 	return (iommu_dvmamap_create(t, t0, &pp->pp_sb, size, nsegments,
1327 	    maxsegsz, boundary, flags, dmamp));
1328 }
1329 
1330 void
1331 psycho_sabre_dvmamap_sync(bus_dma_tag_t t, bus_dma_tag_t t0, bus_dmamap_t map,
1332     bus_size_t offset, bus_size_t len, int ops)
1333 {
1334 	struct psycho_pbm *pp = t->_cookie;
1335 	struct psycho_softc *sc = pp->pp_sc;
1336 
1337 	if (ops & BUS_DMASYNC_POSTREAD)
1338 		psycho_psychoreg_read(sc, pci_dma_write_sync);
1339 
1340 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE))
1341 		__membar("#MemIssue");
1342 }
1343 
1344 u_int
1345 stick_get_timecount(struct timecounter *tc)
1346 {
1347 	struct psycho_softc *sc = tc->tc_priv;
1348 
1349 	return psycho_psychoreg_read(sc, stick_reg_low);
1350 }
1351