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