psycho.c revision 225931
1/*-
2 * Copyright (c) 1999, 2000 Matthew R. Green
3 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>
4 * Copyright (c) 2005 - 2006 Marius Strobl <marius@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 *	from: NetBSD: psycho.c,v 1.39 2001/10/07 20:30:41 eeh Exp
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/sparc64/pci/psycho.c 225931 2011-10-02 23:22:38Z marius $");
35
36/*
37 * Support for `Hummingbird' (UltraSPARC IIe), `Psycho' and `Psycho+'
38 * (UltraSPARC II) and `Sabre' (UltraSPARC IIi) UPA to PCI bridges.
39 */
40
41#include "opt_ofw_pci.h"
42#include "opt_psycho.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/bus.h>
47#include <sys/endian.h>
48#include <sys/kdb.h>
49#include <sys/kernel.h>
50#include <sys/lock.h>
51#include <sys/malloc.h>
52#include <sys/module.h>
53#include <sys/mutex.h>
54#include <sys/pcpu.h>
55#include <sys/reboot.h>
56#include <sys/rman.h>
57
58#include <dev/ofw/ofw_bus.h>
59#include <dev/ofw/ofw_pci.h>
60#include <dev/ofw/openfirm.h>
61
62#include <machine/bus.h>
63#include <machine/bus_common.h>
64#include <machine/bus_private.h>
65#include <machine/iommureg.h>
66#include <machine/iommuvar.h>
67#include <machine/resource.h>
68#include <machine/ver.h>
69
70#include <dev/pci/pcireg.h>
71#include <dev/pci/pcivar.h>
72
73#include <sparc64/pci/ofw_pci.h>
74#include <sparc64/pci/psychoreg.h>
75#include <sparc64/pci/psychovar.h>
76
77#include "pcib_if.h"
78
79static const struct psycho_desc *psycho_find_desc(const struct psycho_desc *,
80    const char *);
81static const struct psycho_desc *psycho_get_desc(device_t);
82static void psycho_set_intr(struct psycho_softc *, u_int, bus_addr_t,
83    driver_filter_t, driver_intr_t);
84static int psycho_find_intrmap(struct psycho_softc *, u_int, bus_addr_t *,
85    bus_addr_t *, u_long *);
86static void sabre_dmamap_sync(bus_dma_tag_t dt, bus_dmamap_t map,
87    bus_dmasync_op_t op);
88static void psycho_intr_enable(void *);
89static void psycho_intr_disable(void *);
90static void psycho_intr_assign(void *);
91static void psycho_intr_clear(void *);
92
93/* Interrupt handlers */
94static driver_filter_t psycho_ue;
95static driver_filter_t psycho_ce;
96static driver_filter_t psycho_pci_bus;
97static driver_filter_t psycho_powerfail;
98static driver_intr_t psycho_overtemp;
99#ifdef PSYCHO_MAP_WAKEUP
100static driver_filter_t psycho_wakeup;
101#endif
102
103/* IOMMU support */
104static void psycho_iommu_init(struct psycho_softc *, int, uint32_t);
105
106/*
107 * Methods
108 */
109static device_probe_t psycho_probe;
110static device_attach_t psycho_attach;
111static bus_read_ivar_t psycho_read_ivar;
112static bus_setup_intr_t psycho_setup_intr;
113static bus_alloc_resource_t psycho_alloc_resource;
114static bus_activate_resource_t psycho_activate_resource;
115static bus_adjust_resource_t psycho_adjust_resource;
116static bus_get_dma_tag_t psycho_get_dma_tag;
117static pcib_maxslots_t psycho_maxslots;
118static pcib_read_config_t psycho_read_config;
119static pcib_write_config_t psycho_write_config;
120static pcib_route_interrupt_t psycho_route_interrupt;
121static ofw_bus_get_node_t psycho_get_node;
122static ofw_pci_setup_device_t psycho_setup_device;
123
124static device_method_t psycho_methods[] = {
125	/* Device interface */
126	DEVMETHOD(device_probe,		psycho_probe),
127	DEVMETHOD(device_attach,	psycho_attach),
128	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
129	DEVMETHOD(device_suspend,	bus_generic_suspend),
130	DEVMETHOD(device_resume,	bus_generic_resume),
131
132	/* Bus interface */
133	DEVMETHOD(bus_print_child,	bus_generic_print_child),
134	DEVMETHOD(bus_read_ivar,	psycho_read_ivar),
135	DEVMETHOD(bus_setup_intr,	psycho_setup_intr),
136	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
137	DEVMETHOD(bus_alloc_resource,	psycho_alloc_resource),
138	DEVMETHOD(bus_activate_resource, psycho_activate_resource),
139	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
140	DEVMETHOD(bus_adjust_resource,	psycho_adjust_resource),
141	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
142	DEVMETHOD(bus_describe_intr,	bus_generic_describe_intr),
143	DEVMETHOD(bus_get_dma_tag,	psycho_get_dma_tag),
144
145	/* pcib interface */
146	DEVMETHOD(pcib_maxslots,	psycho_maxslots),
147	DEVMETHOD(pcib_read_config,	psycho_read_config),
148	DEVMETHOD(pcib_write_config,	psycho_write_config),
149	DEVMETHOD(pcib_route_interrupt,	psycho_route_interrupt),
150
151	/* ofw_bus interface */
152	DEVMETHOD(ofw_bus_get_node,	psycho_get_node),
153
154	/* ofw_pci interface */
155	DEVMETHOD(ofw_pci_setup_device,	psycho_setup_device),
156
157	KOBJMETHOD_END
158};
159
160static devclass_t psycho_devclass;
161
162DEFINE_CLASS_0(pcib, psycho_driver, psycho_methods,
163    sizeof(struct psycho_softc));
164EARLY_DRIVER_MODULE(psycho, nexus, psycho_driver, psycho_devclass, 0, 0,
165    BUS_PASS_BUS);
166
167static SLIST_HEAD(, psycho_softc) psycho_softcs =
168    SLIST_HEAD_INITIALIZER(psycho_softcs);
169
170static const struct intr_controller psycho_ic = {
171	psycho_intr_enable,
172	psycho_intr_disable,
173	psycho_intr_assign,
174	psycho_intr_clear
175};
176
177struct psycho_icarg {
178	struct psycho_softc	*pica_sc;
179	bus_addr_t		pica_map;
180	bus_addr_t		pica_clr;
181};
182
183#define	PSYCHO_READ8(sc, off)						\
184	bus_read_8((sc)->sc_mem_res, (off))
185#define	PSYCHO_WRITE8(sc, off, v)					\
186	bus_write_8((sc)->sc_mem_res, (off), (v))
187#define	PCICTL_READ8(sc, off)						\
188	PSYCHO_READ8((sc), (sc)->sc_pcictl + (off))
189#define	PCICTL_WRITE8(sc, off, v)					\
190	PSYCHO_WRITE8((sc), (sc)->sc_pcictl + (off), (v))
191
192/*
193 * "Sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
194 * single PCI bus and does not have a streaming buffer.  It often has an APB
195 * (advanced PCI bridge) connected to it, which was designed specifically for
196 * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
197 * appears as two "Simba"'s underneath the Sabre.
198 *
199 * "Hummingbird" is the UltraSPARC IIe onboard UPA to PCI bridge. It's
200 * basically the same as Sabre but without an APB underneath it.
201 *
202 * "Psycho" and "Psycho+" are dual UPA to PCI bridges.  They sit on the UPA
203 * bus and manage two PCI buses.  "Psycho" has two 64-bit 33MHz buses, while
204 * "Psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus.  You
205 * will usually find a "Psycho+" since I don't think the original "Psycho"
206 * ever shipped, and if it did it would be in the U30.
207 *
208 * Each "Psycho" PCI bus appears as a separate OFW node, but since they are
209 * both part of the same IC, they only have a single register space.  As such,
210 * they need to be configured together, even though the autoconfiguration will
211 * attach them separately.
212 *
213 * On UltraIIi machines, "Sabre" itself usually takes pci0, with "Simba" often
214 * as pci1 and pci2, although they have been implemented with other PCI bus
215 * numbers on some machines.
216 *
217 * On UltraII machines, there can be any number of "Psycho+" ICs, each
218 * providing two PCI buses.
219 */
220
221struct psycho_desc {
222	const char	*pd_string;
223	int		pd_mode;
224	const char	*pd_name;
225};
226
227static const struct psycho_desc const psycho_compats[] = {
228	{ "pci108e,8000", PSYCHO_MODE_PSYCHO,	"Psycho compatible" },
229	{ "pci108e,a000", PSYCHO_MODE_SABRE,	"Sabre compatible" },
230	{ "pci108e,a001", PSYCHO_MODE_SABRE,	"Hummingbird compatible" },
231	{ NULL,		  0,			NULL }
232};
233
234static const struct psycho_desc const psycho_models[] = {
235	{ "SUNW,psycho",  PSYCHO_MODE_PSYCHO,	"Psycho" },
236	{ "SUNW,sabre",   PSYCHO_MODE_SABRE,	"Sabre" },
237	{ NULL,		  0,			NULL }
238};
239
240static const struct psycho_desc *
241psycho_find_desc(const struct psycho_desc *table, const char *string)
242{
243	const struct psycho_desc *desc;
244
245	if (string == NULL)
246		return (NULL);
247	for (desc = table; desc->pd_string != NULL; desc++)
248		if (strcmp(desc->pd_string, string) == 0)
249			return (desc);
250	return (NULL);
251}
252
253static const struct psycho_desc *
254psycho_get_desc(device_t dev)
255{
256	const struct psycho_desc *rv;
257
258	rv = psycho_find_desc(psycho_models, ofw_bus_get_model(dev));
259	if (rv == NULL)
260		rv = psycho_find_desc(psycho_compats,
261		    ofw_bus_get_compat(dev));
262	return (rv);
263}
264
265static int
266psycho_probe(device_t dev)
267{
268	const char *dtype;
269
270	dtype = ofw_bus_get_type(dev);
271	if (dtype != NULL && strcmp(dtype, OFW_TYPE_PCI) == 0 &&
272	    psycho_get_desc(dev) != NULL) {
273		device_set_desc(dev, "U2P UPA-PCI bridge");
274		return (0);
275	}
276	return (ENXIO);
277}
278
279static int
280psycho_attach(device_t dev)
281{
282	struct psycho_icarg *pica;
283	struct psycho_softc *asc, *sc, *osc;
284	struct ofw_pci_ranges *range;
285	const struct psycho_desc *desc;
286	bus_addr_t intrclr, intrmap;
287	uint64_t csr, dr;
288	phandle_t node;
289	uint32_t dvmabase, prop, prop_array[2];
290	u_int rerun, ver;
291	int i, j;
292
293	node = ofw_bus_get_node(dev);
294	sc = device_get_softc(dev);
295	desc = psycho_get_desc(dev);
296
297	sc->sc_node = node;
298	sc->sc_dev = dev;
299	sc->sc_mode = desc->pd_mode;
300
301	/*
302	 * The Psycho gets three register banks:
303	 * (0) per-PBM configuration and status registers
304	 * (1) per-PBM PCI configuration space, containing only the
305	 *     PBM 256-byte PCI header
306	 * (2) the shared Psycho configuration registers
307	 */
308	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
309		i = 2;
310		sc->sc_pcictl =
311		    bus_get_resource_start(dev, SYS_RES_MEMORY, 0) -
312		    bus_get_resource_start(dev, SYS_RES_MEMORY, 2);
313		switch (sc->sc_pcictl) {
314		case PSR_PCICTL0:
315			sc->sc_half = 0;
316			break;
317		case PSR_PCICTL1:
318			sc->sc_half = 1;
319			break;
320		default:
321			panic("%s: bogus PCI control register location",
322			    __func__);
323			/* NOTREACHED */
324		}
325	} else {
326		i = 0;
327		sc->sc_pcictl = PSR_PCICTL0;
328		sc->sc_half = 0;
329	}
330	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
331	    (sc->sc_mode == PSYCHO_MODE_PSYCHO ? RF_SHAREABLE : 0) |
332	    RF_ACTIVE);
333	if (sc->sc_mem_res == NULL)
334		panic("%s: could not allocate registers", __func__);
335
336	/*
337	 * Match other Psychos that are already configured against
338	 * the base physical address.  This will be the same for a
339	 * pair of devices that share register space.
340	 */
341	osc = NULL;
342	SLIST_FOREACH(asc, &psycho_softcs, sc_link) {
343		if (rman_get_start(asc->sc_mem_res) ==
344		    rman_get_start(sc->sc_mem_res)) {
345			/* Found partner. */
346			osc = asc;
347			break;
348		}
349	}
350	if (osc == NULL) {
351		sc->sc_mtx = malloc(sizeof(*sc->sc_mtx), M_DEVBUF,
352		    M_NOWAIT | M_ZERO);
353		if (sc->sc_mtx == NULL)
354			panic("%s: could not malloc mutex", __func__);
355		mtx_init(sc->sc_mtx, "pcib_mtx", NULL, MTX_SPIN);
356	} else {
357		if (sc->sc_mode != PSYCHO_MODE_PSYCHO)
358			panic("%s: no partner expected", __func__);
359		if (mtx_initialized(osc->sc_mtx) == 0)
360			panic("%s: mutex not initialized", __func__);
361		sc->sc_mtx = osc->sc_mtx;
362	}
363
364	csr = PSYCHO_READ8(sc, PSR_CS);
365	ver = PSYCHO_GCSR_VERS(csr);
366	sc->sc_ign = 0x1f; /* Hummingbird/Sabre IGN is always 0x1f. */
367	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
368		sc->sc_ign = PSYCHO_GCSR_IGN(csr);
369	if (OF_getprop(node, "clock-frequency", &prop, sizeof(prop)) == -1)
370		prop = 33000000;
371
372	device_printf(dev,
373	    "%s, impl %d, version %d, IGN %#x, bus %c, %dMHz\n",
374	    desc->pd_name, (u_int)PSYCHO_GCSR_IMPL(csr), ver, sc->sc_ign,
375	    'A' + sc->sc_half, prop / 1000 / 1000);
376
377	/* Set up the PCI control and PCI diagnostic registers. */
378
379	csr = PCICTL_READ8(sc, PCR_CS);
380	csr &= ~PCICTL_ARB_PARK;
381	if (OF_getproplen(node, "no-bus-parking") < 0)
382		csr |= PCICTL_ARB_PARK;
383
384	/* Workarounds for version specific bugs. */
385	dr = PCICTL_READ8(sc, PCR_DIAG);
386	switch (ver) {
387	case 0:
388		dr |= DIAG_RTRY_DIS;
389		dr &= ~DIAG_DWSYNC_DIS;
390		rerun = 0;
391		break;
392	case 1:
393		csr &= ~PCICTL_ARB_PARK;
394		dr |= DIAG_RTRY_DIS | DIAG_DWSYNC_DIS;
395		rerun = 0;
396		break;
397	default:
398		dr |= DIAG_DWSYNC_DIS;
399		dr &= ~DIAG_RTRY_DIS;
400		rerun = 1;
401		break;
402	}
403
404	csr |= PCICTL_ERRINTEN | PCICTL_ARB_4;
405	csr &= ~(PCICTL_SBHINTEN | PCICTL_WAKEUPEN);
406#ifdef PSYCHO_DEBUG
407	device_printf(dev, "PCI CSR 0x%016llx -> 0x%016llx\n",
408	    (unsigned long long)PCICTL_READ8(sc, PCR_CS),
409	    (unsigned long long)csr);
410#endif
411	PCICTL_WRITE8(sc, PCR_CS, csr);
412
413	dr &= ~DIAG_ISYNC_DIS;
414#ifdef PSYCHO_DEBUG
415	device_printf(dev, "PCI DR 0x%016llx -> 0x%016llx\n",
416	    (unsigned long long)PCICTL_READ8(sc, PCR_DIAG),
417	    (unsigned long long)dr);
418#endif
419	PCICTL_WRITE8(sc, PCR_DIAG, dr);
420
421	if (sc->sc_mode == PSYCHO_MODE_SABRE) {
422		/* Use the PROM preset for now. */
423		csr = PCICTL_READ8(sc, PCR_TAS);
424		if (csr == 0)
425			panic("%s: Hummingbird/Sabre TAS not initialized.",
426			    __func__);
427		dvmabase = (ffs(csr) - 1) << PCITAS_ADDR_SHIFT;
428	} else
429		dvmabase = -1;
430
431	/* Initialize memory and I/O rmans. */
432	sc->sc_pci_io_rman.rm_type = RMAN_ARRAY;
433	sc->sc_pci_io_rman.rm_descr = "Psycho PCI I/O Ports";
434	if (rman_init(&sc->sc_pci_io_rman) != 0 ||
435	    rman_manage_region(&sc->sc_pci_io_rman, 0, PSYCHO_IO_SIZE) != 0)
436		panic("%s: failed to set up I/O rman", __func__);
437	sc->sc_pci_mem_rman.rm_type = RMAN_ARRAY;
438	sc->sc_pci_mem_rman.rm_descr = "Psycho PCI Memory";
439	if (rman_init(&sc->sc_pci_mem_rman) != 0 ||
440	    rman_manage_region(&sc->sc_pci_mem_rman, 0, PSYCHO_MEM_SIZE) != 0)
441		panic("%s: failed to set up memory rman", __func__);
442
443	i = OF_getprop_alloc(node, "ranges", sizeof(*range), (void **)&range);
444	/*
445	 * Make sure that the expected ranges are present.  The
446	 * OFW_PCI_CS_MEM64 one is not currently used though.
447	 */
448	if (i != PSYCHO_NRANGE)
449		panic("%s: unsupported number of ranges", __func__);
450	/*
451	 * Find the addresses of the various bus spaces.
452	 * There should not be multiple ones of one kind.
453	 * The physical start addresses of the ranges are the configuration,
454	 * memory and I/O handles.
455	 */
456	for (i = 0; i < PSYCHO_NRANGE; i++) {
457		j = OFW_PCI_RANGE_CS(&range[i]);
458		if (sc->sc_pci_bh[j] != 0)
459			panic("%s: duplicate range for space %d",
460			    __func__, j);
461		sc->sc_pci_bh[j] = OFW_PCI_RANGE_PHYS(&range[i]);
462	}
463	free(range, M_OFWPROP);
464
465	/* Register the softc, this is needed for paired Psychos. */
466	SLIST_INSERT_HEAD(&psycho_softcs, sc, sc_link);
467
468	/*
469	 * If we're a Hummingbird/Sabre or the first of a pair of Psychos
470	 * to arrive here, do the interrupt setup and start up the IOMMU.
471	 */
472	if (osc == NULL) {
473		/*
474		 * Hunt through all the interrupt mapping regs and register
475		 * our interrupt controller for the corresponding interrupt
476		 * vectors.  We do this early in order to be able to catch
477		 * stray interrupts.
478		 */
479		for (i = 0; i <= PSYCHO_MAX_INO; i++) {
480			if (psycho_find_intrmap(sc, i, &intrmap, &intrclr,
481			    NULL) == 0)
482				continue;
483			pica = malloc(sizeof(*pica), M_DEVBUF, M_NOWAIT);
484			if (pica == NULL)
485				panic("%s: could not allocate interrupt "
486				    "controller argument", __func__);
487			pica->pica_sc = sc;
488			pica->pica_map = intrmap;
489			pica->pica_clr = intrclr;
490#ifdef PSYCHO_DEBUG
491			/*
492			 * Enable all interrupts and clear all interrupt
493			 * states.  This aids the debugging of interrupt
494			 * routing problems.
495			 */
496			device_printf(dev,
497			    "intr map (INO %d, %s) %#lx: %#lx, clr: %#lx\n",
498			    i, intrmap <= PSR_PCIB3_INT_MAP ? "PCI" : "OBIO",
499			    (u_long)intrmap, (u_long)PSYCHO_READ8(sc,
500			    intrmap), (u_long)intrclr);
501			PSYCHO_WRITE8(sc, intrmap, INTMAP_VEC(sc->sc_ign, i));
502			PSYCHO_WRITE8(sc, intrclr, INTCLR_IDLE);
503			PSYCHO_WRITE8(sc, intrmap,
504			    INTMAP_ENABLE(INTMAP_VEC(sc->sc_ign, i),
505			    PCPU_GET(mid)));
506#endif
507			j = intr_controller_register(INTMAP_VEC(sc->sc_ign,
508			    i), &psycho_ic, pica);
509			if (j != 0)
510				device_printf(dev, "could not register "
511				    "interrupt controller for INO %d (%d)\n",
512				    i, j);
513		}
514
515		if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
516			sparc64_counter_init(device_get_nameunit(dev),
517			    rman_get_bustag(sc->sc_mem_res),
518			    rman_get_bushandle(sc->sc_mem_res), PSR_TC0);
519
520		/*
521		 * Set up IOMMU and PCI configuration if we're the first
522		 * of a pair of Psychos to arrive here or a Hummingbird
523		 * or Sabre.
524		 *
525		 * We should calculate a TSB size based on amount of RAM
526		 * and number of bus controllers and number and type of
527		 * child devices.
528		 *
529		 * For the moment, 32KB should be more than enough.
530		 */
531		sc->sc_is = malloc(sizeof(*sc->sc_is), M_DEVBUF, M_NOWAIT |
532		    M_ZERO);
533		if (sc->sc_is == NULL)
534			panic("%s: could not malloc IOMMU state", __func__);
535		sc->sc_is->is_flags = IOMMU_PRESERVE_PROM;
536		if (sc->sc_mode == PSYCHO_MODE_SABRE) {
537			sc->sc_dma_methods =
538			    malloc(sizeof(*sc->sc_dma_methods), M_DEVBUF,
539			    M_NOWAIT);
540			if (sc->sc_dma_methods == NULL)
541				panic("%s: could not malloc DMA methods",
542				    __func__);
543			memcpy(sc->sc_dma_methods, &iommu_dma_methods,
544			    sizeof(*sc->sc_dma_methods));
545			sc->sc_dma_methods->dm_dmamap_sync =
546			    sabre_dmamap_sync;
547			sc->sc_is->is_pmaxaddr =
548			    IOMMU_MAXADDR(SABRE_IOMMU_BITS);
549		} else {
550			sc->sc_dma_methods = &iommu_dma_methods;
551			sc->sc_is->is_pmaxaddr =
552			    IOMMU_MAXADDR(PSYCHO_IOMMU_BITS);
553		}
554		sc->sc_is->is_sb[0] = sc->sc_is->is_sb[1] = 0;
555		if (OF_getproplen(node, "no-streaming-cache") < 0)
556			sc->sc_is->is_sb[0] = sc->sc_pcictl + PCR_STRBUF;
557		sc->sc_is->is_flags |= (rerun != 1) ? IOMMU_RERUN_DISABLE : 0;
558		psycho_iommu_init(sc, 3, dvmabase);
559	} else {
560		/* Just copy IOMMU state, config tag and address. */
561		sc->sc_dma_methods = &iommu_dma_methods;
562		sc->sc_is = osc->sc_is;
563		if (OF_getproplen(node, "no-streaming-cache") < 0)
564			sc->sc_is->is_sb[1] = sc->sc_pcictl + PCR_STRBUF;
565		iommu_reset(sc->sc_is);
566	}
567
568	/* Allocate our tags. */
569	sc->sc_pci_iot = sparc64_alloc_bus_tag(NULL, rman_get_bustag(
570	    sc->sc_mem_res), PCI_IO_BUS_SPACE, NULL);
571	if (sc->sc_pci_iot == NULL)
572		panic("%s: could not allocate PCI I/O tag", __func__);
573	sc->sc_pci_cfgt = sparc64_alloc_bus_tag(NULL, rman_get_bustag(
574	    sc->sc_mem_res), PCI_CONFIG_BUS_SPACE, NULL);
575	if (sc->sc_pci_cfgt == NULL)
576		panic("%s: could not allocate PCI configuration space tag",
577		    __func__);
578	if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
579	    sc->sc_is->is_pmaxaddr, ~0, NULL, NULL, sc->sc_is->is_pmaxaddr,
580	    0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_pci_dmat) != 0)
581		panic("%s: could not create PCI DMA tag", __func__);
582	/* Customize the tag. */
583	sc->sc_pci_dmat->dt_cookie = sc->sc_is;
584	sc->sc_pci_dmat->dt_mt = sc->sc_dma_methods;
585
586	i = OF_getprop(node, "bus-range", (void *)prop_array,
587	    sizeof(prop_array));
588	if (i == -1)
589		panic("%s: could not get bus-range", __func__);
590	if (i != sizeof(prop_array))
591		panic("%s: broken bus-range (%d)", __func__, i);
592	sc->sc_pci_secbus = prop_array[0];
593	sc->sc_pci_subbus = prop_array[1];
594	if (bootverbose)
595		device_printf(dev, "bus range %u to %u; PCI bus %d\n",
596		    sc->sc_pci_secbus, sc->sc_pci_subbus, sc->sc_pci_secbus);
597
598	/* Clear any pending PCI error bits. */
599	PCIB_WRITE_CONFIG(dev, sc->sc_pci_secbus, PCS_DEVICE, PCS_FUNC,
600	    PCIR_STATUS, PCIB_READ_CONFIG(dev, sc->sc_pci_secbus,
601	    PCS_DEVICE, PCS_FUNC, PCIR_STATUS, 2), 2);
602	PCICTL_WRITE8(sc, PCR_CS, PCICTL_READ8(sc, PCR_CS));
603	PCICTL_WRITE8(sc, PCR_AFS, PCICTL_READ8(sc, PCR_AFS));
604
605	if (osc == NULL) {
606		/*
607		 * Establish handlers for interesting interrupts...
608		 *
609		 * XXX We need to remember these and remove this to support
610		 * hotplug on the UPA/FHC bus.
611		 *
612		 * XXX Not all controllers have these, but installing them
613		 * is better than trying to sort through this mess.
614		 */
615		psycho_set_intr(sc, 1, PSR_UE_INT_MAP, psycho_ue, NULL);
616		psycho_set_intr(sc, 2, PSR_CE_INT_MAP, psycho_ce, NULL);
617#ifdef DEBUGGER_ON_POWERFAIL
618		psycho_set_intr(sc, 3, PSR_POWER_INT_MAP, psycho_powerfail,
619		    NULL);
620#else
621		psycho_set_intr(sc, 3, PSR_POWER_INT_MAP, NULL,
622		    (driver_intr_t *)psycho_powerfail);
623#endif
624		if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
625			/*
626			 * Hummingbirds/Sabres do not have the following two
627			 * interrupts.
628			 */
629
630			/*
631			 * The spare hardware interrupt is used for the
632			 * over-temperature interrupt.
633			 */
634			psycho_set_intr(sc, 4, PSR_SPARE_INT_MAP,
635			    NULL, psycho_overtemp);
636#ifdef PSYCHO_MAP_WAKEUP
637			/*
638			 * psycho_wakeup() doesn't do anything useful right
639			 * now.
640			 */
641			psycho_set_intr(sc, 5, PSR_PWRMGT_INT_MAP,
642			    psycho_wakeup, NULL);
643#endif /* PSYCHO_MAP_WAKEUP */
644		}
645	}
646	/*
647	 * Register a PCI bus error interrupt handler according to which
648	 * half this is.  Hummingbird/Sabre don't have a PCI bus B error
649	 * interrupt but they are also only used for PCI bus A.
650	 */
651	psycho_set_intr(sc, 0, sc->sc_half == 0 ? PSR_PCIAERR_INT_MAP :
652	    PSR_PCIBERR_INT_MAP, psycho_pci_bus, NULL);
653
654	/*
655	 * Set the latency timer register as this isn't always done by the
656	 * firmware.
657	 */
658	PCIB_WRITE_CONFIG(dev, sc->sc_pci_secbus, PCS_DEVICE, PCS_FUNC,
659	    PCIR_LATTIMER, OFW_PCI_LATENCY, 1);
660
661	for (i = PCIR_VENDOR; i < PCIR_STATUS; i += sizeof(uint16_t))
662		le16enc(&sc->sc_pci_hpbcfg[i], bus_space_read_2(
663		    sc->sc_pci_cfgt, sc->sc_pci_bh[OFW_PCI_CS_CONFIG],
664		    PSYCHO_CONF_OFF(sc->sc_pci_secbus, PCS_DEVICE,
665		    PCS_FUNC, i)));
666	for (i = PCIR_REVID; i <= PCIR_BIST; i += sizeof(uint8_t))
667		sc->sc_pci_hpbcfg[i] = bus_space_read_1(sc->sc_pci_cfgt,
668		    sc->sc_pci_bh[OFW_PCI_CS_CONFIG], PSYCHO_CONF_OFF(
669		    sc->sc_pci_secbus, PCS_DEVICE, PCS_FUNC, i));
670
671	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(ofw_pci_intr_t));
672	/*
673	 * On E250 the interrupt map entry for the EBus bridge is wrong,
674	 * causing incorrect interrupts to be assigned to some devices on
675	 * the EBus.  Work around it by changing our copy of the interrupt
676	 * map mask to perform a full comparison of the INO.  That way
677	 * the interrupt map entry for the EBus bridge won't match at all
678	 * and the INOs specified in the "interrupts" properties of the
679	 * EBus devices will be used directly instead.
680	 */
681	if (strcmp(sparc64_model, "SUNW,Ultra-250") == 0 &&
682	    sc->sc_pci_iinfo.opi_imapmsk != NULL)
683		*(ofw_pci_intr_t *)(&sc->sc_pci_iinfo.opi_imapmsk[
684		    sc->sc_pci_iinfo.opi_addrc]) = INTMAP_INO_MASK;
685
686	device_add_child(dev, "pci", -1);
687	return (bus_generic_attach(dev));
688}
689
690static void
691psycho_set_intr(struct psycho_softc *sc, u_int index, bus_addr_t intrmap,
692    driver_filter_t filt, driver_intr_t intr)
693{
694	u_long vec;
695	int rid;
696
697	rid = index;
698	sc->sc_irq_res[index] = bus_alloc_resource_any(sc->sc_dev,
699	    SYS_RES_IRQ, &rid, RF_ACTIVE);
700	if (sc->sc_irq_res[index] == NULL && intrmap >= PSR_POWER_INT_MAP) {
701		/*
702		 * These interrupts aren't mandatory and not available
703		 * with all controllers (not even Psychos).
704		 */
705		return;
706	}
707	if (sc->sc_irq_res[index] == NULL ||
708	    INTIGN(vec = rman_get_start(sc->sc_irq_res[index])) !=
709	    sc->sc_ign ||
710	    INTVEC(PSYCHO_READ8(sc, intrmap)) != vec ||
711	    intr_vectors[vec].iv_ic != &psycho_ic ||
712	    bus_setup_intr(sc->sc_dev, sc->sc_irq_res[index],
713	    INTR_TYPE_MISC | INTR_BRIDGE, filt, intr, sc,
714	    &sc->sc_ihand[index]) != 0)
715		panic("%s: failed to set up interrupt %d", __func__, index);
716}
717
718static int
719psycho_find_intrmap(struct psycho_softc *sc, u_int ino,
720    bus_addr_t *intrmapptr, bus_addr_t *intrclrptr, bus_addr_t *intrdiagptr)
721{
722	bus_addr_t intrclr, intrmap;
723	uint64_t diag;
724	int found;
725
726	/*
727	 * XXX we only compare INOs rather than INRs since the firmware may
728	 * not provide the IGN and the IGN is constant for all devices on
729	 * that PCI controller.
730	 * This could cause problems for the FFB/external interrupt which
731	 * has a full vector that can be set arbitrarily.
732	 */
733
734	if (ino > PSYCHO_MAX_INO) {
735		device_printf(sc->sc_dev, "out of range INO %d requested\n",
736		    ino);
737		return (0);
738	}
739
740	found = 0;
741	/* Hunt through OBIO first. */
742	diag = PSYCHO_READ8(sc, PSR_OBIO_INT_DIAG);
743	for (intrmap = PSR_SCSI_INT_MAP, intrclr = PSR_SCSI_INT_CLR;
744	    intrmap <= PSR_PWRMGT_INT_MAP; intrmap += 8, intrclr += 8,
745	    diag >>= 2) {
746		if (sc->sc_mode == PSYCHO_MODE_SABRE &&
747		    (intrmap == PSR_TIMER0_INT_MAP ||
748		    intrmap == PSR_TIMER1_INT_MAP ||
749		    intrmap == PSR_PCIBERR_INT_MAP ||
750		    intrmap == PSR_PWRMGT_INT_MAP))
751			continue;
752		if (INTINO(PSYCHO_READ8(sc, intrmap)) == ino) {
753			diag &= 2;
754			found = 1;
755			break;
756		}
757	}
758
759	if (!found) {
760		diag = PSYCHO_READ8(sc, PSR_PCI_INT_DIAG);
761		/* Now do PCI interrupts. */
762		for (intrmap = PSR_PCIA0_INT_MAP, intrclr = PSR_PCIA0_INT_CLR;
763		    intrmap <= PSR_PCIB3_INT_MAP; intrmap += 8, intrclr += 32,
764		    diag >>= 8) {
765			if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
766			    (intrmap == PSR_PCIA2_INT_MAP ||
767			    intrmap == PSR_PCIA3_INT_MAP))
768				continue;
769			if (((PSYCHO_READ8(sc, intrmap) ^ ino) & 0x3c) == 0) {
770				intrclr += 8 * (ino & 3);
771				diag = (diag >> ((ino & 3) * 2)) & 2;
772				found = 1;
773				break;
774			}
775		}
776	}
777	if (intrmapptr != NULL)
778		*intrmapptr = intrmap;
779	if (intrclrptr != NULL)
780		*intrclrptr = intrclr;
781	if (intrdiagptr != NULL)
782		*intrdiagptr = diag;
783	return (found);
784}
785
786/*
787 * Interrupt handlers
788 */
789static int
790psycho_ue(void *arg)
791{
792	struct psycho_softc *sc = arg;
793	uint64_t afar, afsr;
794
795	afar = PSYCHO_READ8(sc, PSR_UE_AFA);
796	afsr = PSYCHO_READ8(sc, PSR_UE_AFS);
797	/*
798	 * On the UltraSPARC-IIi/IIe, IOMMU misses/protection faults cause
799	 * the AFAR to be set to the physical address of the TTE entry that
800	 * was invalid/write protected.  Call into the IOMMU code to have
801	 * them decoded to virtual I/O addresses.
802	 */
803	if ((afsr & UEAFSR_P_DTE) != 0)
804		iommu_decode_fault(sc->sc_is, afar);
805	panic("%s: uncorrectable DMA error AFAR %#lx AFSR %#lx",
806	    device_get_nameunit(sc->sc_dev), (u_long)afar, (u_long)afsr);
807	return (FILTER_HANDLED);
808}
809
810static int
811psycho_ce(void *arg)
812{
813	struct psycho_softc *sc = arg;
814	uint64_t afar, afsr;
815
816	mtx_lock_spin(sc->sc_mtx);
817	afar = PSYCHO_READ8(sc, PSR_CE_AFA);
818	afsr = PSYCHO_READ8(sc, PSR_CE_AFS);
819	device_printf(sc->sc_dev, "correctable DMA error AFAR %#lx "
820	    "AFSR %#lx\n", (u_long)afar, (u_long)afsr);
821	/* Clear the error bits that we caught. */
822	PSYCHO_WRITE8(sc, PSR_CE_AFS, afsr);
823	mtx_unlock_spin(sc->sc_mtx);
824	return (FILTER_HANDLED);
825}
826
827static int
828psycho_pci_bus(void *arg)
829{
830	struct psycho_softc *sc = arg;
831	uint64_t afar, afsr;
832
833	afar = PCICTL_READ8(sc, PCR_AFA);
834	afsr = PCICTL_READ8(sc, PCR_AFS);
835	panic("%s: PCI bus %c error AFAR %#lx AFSR %#lx",
836	    device_get_nameunit(sc->sc_dev), 'A' + sc->sc_half, (u_long)afar,
837	    (u_long)afsr);
838	return (FILTER_HANDLED);
839}
840
841static int
842psycho_powerfail(void *arg)
843{
844#ifdef DEBUGGER_ON_POWERFAIL
845	struct psycho_softc *sc = arg;
846
847	kdb_enter(KDB_WHY_POWERFAIL, "powerfail");
848#else
849	static int shutdown;
850
851	/* As the interrupt is cleared we may be called multiple times. */
852	if (shutdown != 0)
853		return (FILTER_HANDLED);
854	shutdown++;
855	printf("Power Failure Detected: Shutting down NOW.\n");
856	shutdown_nice(0);
857#endif
858	return (FILTER_HANDLED);
859}
860
861static void
862psycho_overtemp(void *arg)
863{
864	static int shutdown;
865
866	/* As the interrupt is cleared we may be called multiple times. */
867	if (shutdown != 0)
868		return;
869	shutdown++;
870	printf("DANGER: OVER TEMPERATURE detected.\nShutting down NOW.\n");
871	shutdown_nice(RB_POWEROFF);
872}
873
874#ifdef PSYCHO_MAP_WAKEUP
875static int
876psycho_wakeup(void *arg)
877{
878	struct psycho_softc *sc = arg;
879
880	/* We don't really have a framework to deal with this properly. */
881	device_printf(sc->sc_dev, "power management wakeup\n");
882	return (FILTER_HANDLED);
883}
884#endif /* PSYCHO_MAP_WAKEUP */
885
886static void
887psycho_iommu_init(struct psycho_softc *sc, int tsbsize, uint32_t dvmabase)
888{
889	struct iommu_state *is = sc->sc_is;
890
891	/* Punch in our copies. */
892	is->is_bustag = rman_get_bustag(sc->sc_mem_res);
893	is->is_bushandle = rman_get_bushandle(sc->sc_mem_res);
894	is->is_iommu = PSR_IOMMU;
895	is->is_dtag = PSR_IOMMU_TLB_TAG_DIAG;
896	is->is_ddram = PSR_IOMMU_TLB_DATA_DIAG;
897	is->is_dqueue = PSR_IOMMU_QUEUE_DIAG;
898	is->is_dva = PSR_IOMMU_SVADIAG;
899	is->is_dtcmp = PSR_IOMMU_TLB_CMP_DIAG;
900
901	iommu_init(device_get_nameunit(sc->sc_dev), is, tsbsize, dvmabase, 0);
902}
903
904static int
905psycho_maxslots(device_t dev)
906{
907
908	/* XXX: is this correct? */
909	return (PCI_SLOTMAX);
910}
911
912static uint32_t
913psycho_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
914    int width)
915{
916	struct psycho_softc *sc;
917	bus_space_handle_t bh;
918	u_long offset = 0;
919	uint8_t byte;
920	uint16_t shrt;
921	uint32_t r, wrd;
922	int i;
923
924	sc = device_get_softc(dev);
925	if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
926	    slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > PCI_REGMAX)
927		return (-1);
928
929	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
930
931	/*
932	 * The Hummingbird and Sabre bridges are picky in that they
933	 * only allow their config space to be accessed using the
934	 * "native" width of the respective register being accessed
935	 * and return semi-random other content of their config space
936	 * otherwise.  Given that the PCI specs don't say anything
937	 * about such a (unusual) limitation and lots of stuff expects
938	 * to be able to access the contents of the config space at
939	 * any width we allow just that.  We do this by using a copy
940	 * of the header of the bridge (the rest is all zero anyway)
941	 * read during attach (expect for PCIR_STATUS) in order to
942	 * simplify things.
943	 * The Psycho bridges contain a dupe of their header at 0x80
944	 * which we nullify that way also.
945	 */
946	if (bus == sc->sc_pci_secbus && slot == PCS_DEVICE &&
947	    func == PCS_FUNC) {
948		if (offset % width != 0)
949			return (-1);
950
951		if (reg >= sizeof(sc->sc_pci_hpbcfg))
952			return (0);
953
954		if ((reg < PCIR_STATUS && reg + width > PCIR_STATUS) ||
955		    reg == PCIR_STATUS || reg == PCIR_STATUS + 1)
956			le16enc(&sc->sc_pci_hpbcfg[PCIR_STATUS],
957			    bus_space_read_2(sc->sc_pci_cfgt, bh,
958			    PSYCHO_CONF_OFF(sc->sc_pci_secbus,
959			    PCS_DEVICE, PCS_FUNC, PCIR_STATUS)));
960
961		switch (width) {
962		case 1:
963			return (sc->sc_pci_hpbcfg[reg]);
964		case 2:
965			return (le16dec(&sc->sc_pci_hpbcfg[reg]));
966		case 4:
967			return (le32dec(&sc->sc_pci_hpbcfg[reg]));
968		}
969	}
970
971	offset = PSYCHO_CONF_OFF(bus, slot, func, reg);
972	switch (width) {
973	case 1:
974		i = bus_space_peek_1(sc->sc_pci_cfgt, bh, offset, &byte);
975		r = byte;
976		break;
977	case 2:
978		i = bus_space_peek_2(sc->sc_pci_cfgt, bh, offset, &shrt);
979		r = shrt;
980		break;
981	case 4:
982		i = bus_space_peek_4(sc->sc_pci_cfgt, bh, offset, &wrd);
983		r = wrd;
984		break;
985	default:
986		panic("%s: bad width", __func__);
987		/* NOTREACHED */
988	}
989
990	if (i) {
991#ifdef PSYCHO_DEBUG
992		printf("%s: read data error reading: %d.%d.%d: 0x%x\n",
993		    __func__, bus, slot, func, reg);
994#endif
995		r = -1;
996	}
997	return (r);
998}
999
1000static void
1001psycho_write_config(device_t dev, u_int bus, u_int slot, u_int func,
1002    u_int reg, uint32_t val, int width)
1003{
1004	struct psycho_softc *sc;
1005	bus_space_handle_t bh;
1006	u_long offset = 0;
1007
1008	sc = device_get_softc(dev);
1009	if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
1010	    slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > PCI_REGMAX)
1011		return;
1012
1013	offset = PSYCHO_CONF_OFF(bus, slot, func, reg);
1014	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
1015	switch (width) {
1016	case 1:
1017		bus_space_write_1(sc->sc_pci_cfgt, bh, offset, val);
1018		break;
1019	case 2:
1020		bus_space_write_2(sc->sc_pci_cfgt, bh, offset, val);
1021		break;
1022	case 4:
1023		bus_space_write_4(sc->sc_pci_cfgt, bh, offset, val);
1024		break;
1025	default:
1026		panic("%s: bad width", __func__);
1027		/* NOTREACHED */
1028	}
1029}
1030
1031static int
1032psycho_route_interrupt(device_t bridge, device_t dev, int pin)
1033{
1034	struct psycho_softc *sc;
1035	struct ofw_pci_register reg;
1036	bus_addr_t intrmap;
1037	ofw_pci_intr_t pintr, mintr;
1038	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
1039
1040	sc = device_get_softc(bridge);
1041	pintr = pin;
1042	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo,
1043	    &reg, sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
1044	    NULL, maskbuf))
1045		return (mintr);
1046	/*
1047	 * If this is outside of the range for an intpin, it's likely a full
1048	 * INO, and no mapping is required at all; this happens on the U30,
1049	 * where there's no interrupt map at the Psycho node.  Fortunately,
1050	 * there seem to be no INOs in the intpin range on this boxen, so
1051	 * this easy heuristics will do.
1052	 */
1053	if (pin > 4)
1054		return (pin);
1055	/*
1056	 * Guess the INO; we always assume that this is a non-OBIO
1057	 * device, and that pin is a "real" intpin number.  Determine
1058	 * the mapping register to be used by the slot number.
1059	 * We only need to do this on E450s, it seems; here, the slot numbers
1060	 * for bus A are one-based, while those for bus B seemingly have an
1061	 * offset of 2 (hence the factor of 3 below).
1062	 */
1063	intrmap = PSR_PCIA0_INT_MAP +
1064	    8 * (pci_get_slot(dev) - 1 + 3 * sc->sc_half);
1065	mintr = INTINO(PSYCHO_READ8(sc, intrmap)) + pin - 1;
1066	device_printf(bridge,
1067	    "guessing interrupt %d for device %d.%d pin %d\n",
1068	    (int)mintr, pci_get_slot(dev), pci_get_function(dev), pin);
1069	return (mintr);
1070}
1071
1072static int
1073psycho_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1074{
1075	struct psycho_softc *sc;
1076
1077	sc = device_get_softc(dev);
1078	switch (which) {
1079	case PCIB_IVAR_DOMAIN:
1080		*result = device_get_unit(dev);
1081		return (0);
1082	case PCIB_IVAR_BUS:
1083		*result = sc->sc_pci_secbus;
1084		return (0);
1085	}
1086	return (ENOENT);
1087}
1088
1089static void
1090sabre_dmamap_sync(bus_dma_tag_t dt, bus_dmamap_t map, bus_dmasync_op_t op)
1091{
1092	struct iommu_state *is = dt->dt_cookie;
1093
1094	if ((map->dm_flags & DMF_LOADED) == 0)
1095		return;
1096
1097	if ((op & BUS_DMASYNC_POSTREAD) != 0)
1098		(void)bus_space_read_8(is->is_bustag, is->is_bushandle,
1099		    PSR_DMA_WRITE_SYNC);
1100
1101	if ((op & BUS_DMASYNC_PREWRITE) != 0)
1102		membar(Sync);
1103}
1104
1105static void
1106psycho_intr_enable(void *arg)
1107{
1108	struct intr_vector *iv = arg;
1109	struct psycho_icarg *pica = iv->iv_icarg;
1110
1111	PSYCHO_WRITE8(pica->pica_sc, pica->pica_map,
1112	    INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
1113}
1114
1115static void
1116psycho_intr_disable(void *arg)
1117{
1118	struct intr_vector *iv = arg;
1119	struct psycho_icarg *pica = iv->iv_icarg;
1120
1121	PSYCHO_WRITE8(pica->pica_sc, pica->pica_map, iv->iv_vec);
1122}
1123
1124static void
1125psycho_intr_assign(void *arg)
1126{
1127	struct intr_vector *iv = arg;
1128	struct psycho_icarg *pica = iv->iv_icarg;
1129
1130	PSYCHO_WRITE8(pica->pica_sc, pica->pica_map, INTMAP_TID(
1131	    PSYCHO_READ8(pica->pica_sc, pica->pica_map), iv->iv_mid));
1132}
1133
1134static void
1135psycho_intr_clear(void *arg)
1136{
1137	struct intr_vector *iv = arg;
1138	struct psycho_icarg *pica = iv->iv_icarg;
1139
1140	PSYCHO_WRITE8(pica->pica_sc, pica->pica_clr, INTCLR_IDLE);
1141}
1142
1143static int
1144psycho_setup_intr(device_t dev, device_t child, struct resource *ires,
1145    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1146    void **cookiep)
1147{
1148	struct psycho_softc *sc;
1149	u_long vec;
1150
1151	sc = device_get_softc(dev);
1152	/*
1153	 * Make sure the vector is fully specified and we registered
1154	 * our interrupt controller for it.
1155	 */
1156	vec = rman_get_start(ires);
1157	if (INTIGN(vec) != sc->sc_ign ||
1158	    intr_vectors[vec].iv_ic != &psycho_ic) {
1159		device_printf(dev, "invalid interrupt vector 0x%lx\n", vec);
1160		return (EINVAL);
1161	}
1162	return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
1163	    arg, cookiep));
1164}
1165
1166static struct resource *
1167psycho_alloc_resource(device_t bus, device_t child, int type, int *rid,
1168    u_long start, u_long end, u_long count, u_int flags)
1169{
1170	struct psycho_softc *sc;
1171	struct resource *rv;
1172	struct rman *rm;
1173
1174	sc = device_get_softc(bus);
1175	switch (type) {
1176	case SYS_RES_IRQ:
1177		/*
1178		 * XXX: Don't accept blank ranges for now, only single
1179		 * interrupts.  The other case should not happen with
1180		 * the MI PCI code...
1181		 * XXX: This may return a resource that is out of the
1182		 * range that was specified.  Is this correct...?
1183		 */
1184		if (start != end)
1185			panic("%s: XXX: interrupt range", __func__);
1186		start = end = INTMAP_VEC(sc->sc_ign, end);
1187		return (bus_generic_alloc_resource(bus, child, type, rid,
1188		     start, end, count, flags));
1189	case SYS_RES_MEMORY:
1190		rm = &sc->sc_pci_mem_rman;
1191		break;
1192	case SYS_RES_IOPORT:
1193		rm = &sc->sc_pci_io_rman;
1194		break;
1195	default:
1196		return (NULL);
1197	}
1198
1199	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1200	    child);
1201	if (rv == NULL)
1202		return (NULL);
1203	rman_set_rid(rv, *rid);
1204
1205	if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
1206	    *rid, rv) != 0) {
1207		rman_release_resource(rv);
1208		return (NULL);
1209	}
1210	return (rv);
1211}
1212
1213static int
1214psycho_activate_resource(device_t bus, device_t child, int type, int rid,
1215    struct resource *r)
1216{
1217	struct psycho_softc *sc;
1218	struct bus_space_tag *tag;
1219
1220	sc = device_get_softc(bus);
1221	switch (type) {
1222	case SYS_RES_IRQ:
1223		return (bus_generic_activate_resource(bus, child, type, rid,
1224		    r));
1225	case SYS_RES_MEMORY:
1226		tag = sparc64_alloc_bus_tag(r, rman_get_bustag(
1227		    sc->sc_mem_res), PCI_MEMORY_BUS_SPACE, NULL);
1228		if (tag == NULL)
1229			return (ENOMEM);
1230		rman_set_bustag(r, tag);
1231		rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_MEM32] +
1232		    rman_get_start(r));
1233		break;
1234	case SYS_RES_IOPORT:
1235		rman_set_bustag(r, sc->sc_pci_iot);
1236		rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_IO] +
1237		    rman_get_start(r));
1238		break;
1239	}
1240	return (rman_activate_resource(r));
1241}
1242
1243static int
1244psycho_adjust_resource(device_t bus, device_t child, int type,
1245    struct resource *r, u_long start, u_long end)
1246{
1247	struct psycho_softc *sc;
1248	struct rman *rm;
1249
1250	sc = device_get_softc(bus);
1251	switch (type) {
1252	case SYS_RES_IRQ:
1253		return (bus_generic_adjust_resource(bus, child, type, r,
1254		    start, end));
1255	case SYS_RES_MEMORY:
1256		rm = &sc->sc_pci_mem_rman;
1257		break;
1258	case SYS_RES_IOPORT:
1259		rm = &sc->sc_pci_io_rman;
1260		break;
1261	default:
1262		return (EINVAL);
1263	}
1264	if (rman_is_region_manager(r, rm) == 0)
1265		return (EINVAL);
1266	return (rman_adjust_resource(r, start, end));
1267}
1268
1269static bus_dma_tag_t
1270psycho_get_dma_tag(device_t bus, device_t child __unused)
1271{
1272	struct psycho_softc *sc;
1273
1274	sc = device_get_softc(bus);
1275	return (sc->sc_pci_dmat);
1276}
1277
1278static phandle_t
1279psycho_get_node(device_t bus, device_t child __unused)
1280{
1281	struct psycho_softc *sc;
1282
1283	sc = device_get_softc(bus);
1284	/* We only have one child, the PCI bus, which needs our own node. */
1285	return (sc->sc_node);
1286}
1287
1288static void
1289psycho_setup_device(device_t bus, device_t child)
1290{
1291	struct psycho_softc *sc;
1292	uint32_t rev;
1293
1294	sc = device_get_softc(bus);
1295	/*
1296	 * Revision 0 EBus bridges have a bug which prevents them from
1297	 * working when bus parking is enabled.
1298	 */
1299	if ((strcmp(ofw_bus_get_name(child), "ebus") == 0 ||
1300	    strcmp(ofw_bus_get_name(child), "pci108e,1000") == 0) &&
1301	    OF_getprop(ofw_bus_get_node(child), "revision-id", &rev,
1302	    sizeof(rev)) > 0 && rev == 0)
1303		PCICTL_WRITE8(sc, PCR_CS, PCICTL_READ8(sc, PCR_CS) &
1304		    ~PCICTL_ARB_PARK);
1305}
1306