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