schizo.c revision 186290
1193323Sed/*-
2193323Sed * Copyright (c) 1999, 2000 Matthew R. Green
3193323Sed * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>
4193323Sed * Copyright (c) 2005, 2007, 2008 by Marius Strobl <marius@FreeBSD.org>
5193323Sed * All rights reserved.
6193323Sed *
7193323Sed * Redistribution and use in source and binary forms, with or without
8193323Sed * modification, are permitted provided that the following conditions
9193323Sed * are met:
10193323Sed * 1. Redistributions of source code must retain the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer.
12193323Sed * 2. Redistributions in binary form must reproduce the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer in the
14193323Sed *    documentation and/or other materials provided with the distribution.
15193323Sed * 3. The name of the author may not be used to endorse or promote products
16193323Sed *    derived from this software without specific prior written permission.
17193323Sed *
18193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19193323Sed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21193323Sed * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22193323Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23193323Sed * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24193323Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25193323Sed * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26193323Sed * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28193323Sed * SUCH DAMAGE.
29193323Sed *
30193323Sed *	from: NetBSD: psycho.c,v 1.39 2001/10/07 20:30:41 eeh Exp
31249423Sdim *	from: FreeBSD: psycho.c 183152 2008-09-18 19:45:22Z marius
32249423Sdim */
33249423Sdim
34234353Sdim#include <sys/cdefs.h>
35249423Sdim__FBSDID("$FreeBSD: head/sys/sparc64/pci/schizo.c 186290 2008-12-18 18:29:15Z marius $");
36207618Srdivacky
37193323Sed/*
38193323Sed * Driver for `Schizo' Fireplane/Safari to PCI 2.1 and `Tomatillo' JBus to
39218893Sdim * PCI 2.2 bridges
40249423Sdim */
41249423Sdim
42249423Sdim#include "opt_ofw_pci.h"
43249423Sdim#include "opt_schizo.h"
44249423Sdim
45193323Sed#include <sys/param.h>
46193323Sed#include <sys/systm.h>
47198090Srdivacky#include <sys/bus.h>
48249423Sdim#include <sys/kernel.h>
49249423Sdim#include <sys/lock.h>
50249423Sdim#include <sys/malloc.h>
51193323Sed#include <sys/module.h>
52234353Sdim#include <sys/mutex.h>
53193323Sed#include <sys/pcpu.h>
54193323Sed#include <sys/rman.h>
55193323Sed#include <sys/time.h>
56193323Sed#include <sys/timetc.h>
57193323Sed
58193323Sed#include <dev/ofw/ofw_bus.h>
59193323Sed#include <dev/ofw/ofw_pci.h>
60193323Sed#include <dev/ofw/openfirm.h>
61234353Sdim
62193323Sed#include <machine/bus.h>
63234353Sdim#include <machine/bus_common.h>
64198090Srdivacky#include <machine/bus_private.h>
65193323Sed#include <machine/fsr.h>
66193323Sed#include <machine/iommureg.h>
67234353Sdim#include <machine/iommuvar.h>
68234353Sdim#include <machine/resource.h>
69193323Sed
70234353Sdim#include <dev/pci/pcireg.h>
71234353Sdim#include <dev/pci/pcivar.h>
72234353Sdim
73234353Sdim#include <sparc64/pci/ofw_pci.h>
74234353Sdim#include <sparc64/pci/schizoreg.h>
75234353Sdim#include <sparc64/pci/schizovar.h>
76234353Sdim
77234353Sdim#include "pcib_if.h"
78234353Sdim
79234353Sdimstatic const struct schizo_desc *schizo_get_desc(device_t);
80234353Sdimstatic void schizo_set_intr(struct schizo_softc *, u_int, u_int,
81234353Sdim    driver_filter_t);
82234353Sdimstatic driver_filter_t schizo_dma_sync_stub;
83234353Sdimstatic driver_filter_t ichip_dma_sync_stub;
84234353Sdimstatic void schizo_intr_enable(void *);
85234353Sdimstatic void schizo_intr_disable(void *);
86234353Sdimstatic void schizo_intr_assign(void *);
87234353Sdimstatic void schizo_intr_clear(void *);
88234353Sdimstatic int schizo_intr_register(struct schizo_softc *sc, u_int ino);
89234353Sdimstatic int schizo_get_intrmap(struct schizo_softc *, u_int,
90263508Sdim    bus_addr_t *, bus_addr_t *);
91263508Sdimstatic bus_space_tag_t schizo_alloc_bus_tag(struct schizo_softc *, int);
92234353Sdimstatic timecounter_get_t schizo_get_timecount;
93234353Sdim
94234353Sdim/* Interrupt handlers */
95234353Sdimstatic driver_filter_t schizo_pci_bus;
96234353Sdimstatic driver_filter_t schizo_ue;
97234353Sdimstatic driver_filter_t schizo_ce;
98234353Sdimstatic driver_filter_t schizo_host_bus;
99263508Sdimstatic driver_filter_t schizo_cdma;
100234353Sdim
101234353Sdim/* IOMMU support */
102234353Sdimstatic void schizo_iommu_init(struct schizo_softc *, int, uint32_t);
103234353Sdim
104234353Sdim/*
105263508Sdim * Methods
106234353Sdim */
107234353Sdimstatic device_probe_t schizo_probe;
108263508Sdimstatic device_attach_t schizo_attach;
109234353Sdimstatic bus_read_ivar_t schizo_read_ivar;
110234353Sdimstatic bus_setup_intr_t schizo_setup_intr;
111234353Sdimstatic bus_teardown_intr_t schizo_teardown_intr;
112234353Sdimstatic bus_alloc_resource_t schizo_alloc_resource;
113263508Sdimstatic bus_activate_resource_t schizo_activate_resource;
114234353Sdimstatic bus_deactivate_resource_t schizo_deactivate_resource;
115234353Sdimstatic bus_release_resource_t schizo_release_resource;
116263508Sdimstatic bus_get_dma_tag_t schizo_get_dma_tag;
117234353Sdimstatic pcib_maxslots_t schizo_maxslots;
118234353Sdimstatic pcib_read_config_t schizo_read_config;
119234353Sdimstatic pcib_write_config_t schizo_write_config;
120234353Sdimstatic pcib_route_interrupt_t schizo_route_interrupt;
121263508Sdimstatic ofw_bus_get_node_t schizo_get_node;
122263508Sdim
123234353Sdimstatic device_method_t schizo_methods[] = {
124234353Sdim	/* Device interface */
125198090Srdivacky	DEVMETHOD(device_probe,		schizo_probe),
126193323Sed	DEVMETHOD(device_attach,	schizo_attach),
127193323Sed	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
128193323Sed	DEVMETHOD(device_suspend,	bus_generic_suspend),
129193323Sed	DEVMETHOD(device_resume,	bus_generic_resume),
130193323Sed
131193323Sed	/* Bus interface */
132234353Sdim	DEVMETHOD(bus_print_child,	bus_generic_print_child),
133234353Sdim	DEVMETHOD(bus_read_ivar,	schizo_read_ivar),
134234353Sdim	DEVMETHOD(bus_setup_intr,	schizo_setup_intr),
135193323Sed	DEVMETHOD(bus_teardown_intr,	schizo_teardown_intr),
136193323Sed	DEVMETHOD(bus_alloc_resource,	schizo_alloc_resource),
137193323Sed	DEVMETHOD(bus_activate_resource,	schizo_activate_resource),
138193323Sed	DEVMETHOD(bus_deactivate_resource,	schizo_deactivate_resource),
139193323Sed	DEVMETHOD(bus_release_resource,	schizo_release_resource),
140193323Sed	DEVMETHOD(bus_get_dma_tag,	schizo_get_dma_tag),
141193323Sed
142234353Sdim	/* pcib interface */
143193323Sed	DEVMETHOD(pcib_maxslots,	schizo_maxslots),
144234353Sdim	DEVMETHOD(pcib_read_config,	schizo_read_config),
145193323Sed	DEVMETHOD(pcib_write_config,	schizo_write_config),
146193323Sed	DEVMETHOD(pcib_route_interrupt,	schizo_route_interrupt),
147193323Sed
148193323Sed	/* ofw_bus interface */
149193323Sed	DEVMETHOD(ofw_bus_get_node,	schizo_get_node),
150193323Sed
151193323Sed	{ 0, 0 }
152234353Sdim};
153234353Sdim
154263508Sdimstatic devclass_t schizo_devclass;
155263508Sdim
156218893SdimDEFINE_CLASS_0(pcib, schizo_driver, schizo_methods,
157218893Sdim    sizeof(struct schizo_softc));
158193323SedDRIVER_MODULE(schizo, nexus, schizo_driver, schizo_devclass, 0, 0);
159193323Sed
160193323Sedstatic SLIST_HEAD(, schizo_softc) schizo_softcs =
161193323Sed    SLIST_HEAD_INITIALIZER(schizo_softcs);
162193323Sed
163212904Sdimstatic const struct intr_controller schizo_ic = {
164193323Sed	schizo_intr_enable,
165193323Sed	schizo_intr_disable,
166193323Sed	schizo_intr_assign,
167193323Sed	schizo_intr_clear
168193323Sed};
169193323Sed
170193323Sedstruct schizo_icarg {
171193323Sed	struct schizo_softc	*sica_sc;
172193323Sed	bus_addr_t		sica_map;
173218893Sdim	bus_addr_t		sica_clr;
174249423Sdim};
175193323Sed
176193323Sedstruct schizo_dma_sync {
177193323Sed	struct schizo_softc	*sds_sc;
178193323Sed	driver_filter_t		*sds_handler;
179198090Srdivacky	void			*sds_arg;
180234353Sdim	void			*sds_cookie;
181198090Srdivacky	uint64_t		sds_syncval;
182198090Srdivacky	device_t		sds_ppb;	/* farest PCI-PCI bridge */
183193323Sed	uint8_t			sds_bus;	/* bus of farest PCI device */
184193323Sed	uint8_t			sds_slot;	/* slot of farest PCI device */
185193323Sed	uint8_t			sds_func;	/* func. of farest PCI device */
186193323Sed};
187193323Sed
188193323Sed#define	SCHIZO_PERF_CNT_QLTY	100
189193323Sed
190193323Sed#define	SCHIZO_SPC_READ_8(spc, sc, offs) \
191193323Sed	bus_read_8((sc)->sc_mem_res[(spc)], (offs))
192193323Sed#define	SCHIZO_SPC_WRITE_8(spc, sc, offs, v) \
193193323Sed	bus_write_8((sc)->sc_mem_res[(spc)], (offs), (v))
194193323Sed
195193323Sed#define	SCHIZO_PCI_READ_8(sc, offs) \
196193323Sed	SCHIZO_SPC_READ_8(STX_PCI, (sc), (offs))
197193323Sed#define	SCHIZO_PCI_WRITE_8(sc, offs, v) \
198193323Sed	SCHIZO_SPC_WRITE_8(STX_PCI, (sc), (offs), (v))
199263508Sdim#define	SCHIZO_CTRL_READ_8(sc, offs) \
200193323Sed	SCHIZO_SPC_READ_8(STX_CTRL, (sc), (offs))
201193323Sed#define	SCHIZO_CTRL_WRITE_8(sc, offs, v) \
202193323Sed	SCHIZO_SPC_WRITE_8(STX_CTRL, (sc), (offs), (v))
203193323Sed#define	SCHIZO_PCICFG_READ_8(sc, offs) \
204193323Sed	SCHIZO_SPC_READ_8(STX_PCICFG, (sc), (offs))
205193323Sed#define	SCHIZO_PCICFG_WRITE_8(sc, offs, v) \
206193323Sed	SCHIZO_SPC_WRITE_8(STX_PCICFG, (sc), (offs), (v))
207193323Sed#define	SCHIZO_ICON_READ_8(sc, offs) \
208193323Sed	SCHIZO_SPC_READ_8(STX_ICON, (sc), (offs))
209193323Sed#define	SCHIZO_ICON_WRITE_8(sc, offs, v) \
210234353Sdim	SCHIZO_SPC_WRITE_8(STX_ICON, (sc), (offs), (v))
211193323Sed
212193323Sed#define	OFW_PCI_TYPE		"pci"
213193323Sed
214193323Sedstruct schizo_desc {
215193323Sed	const char	*sd_string;
216193323Sed	int		sd_mode;
217193323Sed	const char	*sd_name;
218193323Sed};
219193323Sed
220193323Sedstatic const struct schizo_desc const schizo_compats[] = {
221234353Sdim	{ "pci108e,8001",	SCHIZO_MODE_SCZ,	"Schizo" },
222234353Sdim	{ "pci108e,a801",	SCHIZO_MODE_TOM,	"Tomatillo" },
223234353Sdim	{ NULL,			0,			NULL }
224249423Sdim};
225234353Sdim
226263508Sdimstatic const struct schizo_desc *
227263508Sdimschizo_get_desc(device_t dev)
228263508Sdim{
229234353Sdim	const struct schizo_desc *desc;
230234353Sdim	const char *compat;
231263508Sdim
232234353Sdim	compat = ofw_bus_get_compat(dev);
233263508Sdim	if (compat == NULL)
234234353Sdim		return (NULL);
235234353Sdim	for (desc = schizo_compats; desc->sd_string != NULL; desc++)
236234353Sdim		if (strcmp(desc->sd_string, compat) == 0)
237234353Sdim			return (desc);
238234353Sdim	return (NULL);
239234353Sdim}
240234353Sdim
241234353Sdimstatic int
242234353Sdimschizo_probe(device_t dev)
243234353Sdim{
244234353Sdim	const char *dtype;
245263508Sdim
246234353Sdim	dtype = ofw_bus_get_type(dev);
247249423Sdim	if (dtype != NULL && strcmp(dtype, OFW_PCI_TYPE) == 0 &&
248234353Sdim	    schizo_get_desc(dev) != NULL) {
249234353Sdim		device_set_desc(dev, "Sun Host-PCI bridge");
250234353Sdim		return (0);
251234353Sdim	}
252249423Sdim	return (ENXIO);
253249423Sdim}
254249423Sdim
255263508Sdimstatic int
256263508Sdimschizo_attach(device_t dev)
257249423Sdim{
258249423Sdim	struct ofw_pci_ranges *range;
259234353Sdim	const struct schizo_desc *desc;
260234353Sdim	struct schizo_softc *asc, *sc, *osc;
261234353Sdim	struct timecounter *tc;
262234353Sdim	uint64_t ino_bitmap, reg;
263263508Sdim	phandle_t node;
264263508Sdim	uint32_t prop, prop_array[2];
265234353Sdim	int i, mode, n, nrange, rid, tsbsize;
266234353Sdim
267234353Sdim	sc = device_get_softc(dev);
268234353Sdim	node = ofw_bus_get_node(dev);
269234353Sdim	desc = schizo_get_desc(dev);
270234353Sdim	mode = desc->sd_mode;
271234353Sdim
272234353Sdim	sc->sc_dev = dev;
273234353Sdim	sc->sc_node = node;
274234353Sdim	sc->sc_mode = mode;
275234353Sdim	sc->sc_flags = 0;
276263508Sdim
277234353Sdim	/*
278234353Sdim	 * The Schizo has three register banks:
279234353Sdim	 * (0) per-PBM PCI configuration and status registers, but for bus B
280234353Sdim	 *     shared with the UPA64s interrupt mapping register banks
281263508Sdim	 * (1) shared Schizo controller configuration and status registers
282234353Sdim	 * (2) per-PBM PCI configuration space
283234353Sdim	 *
284234353Sdim	 * The Tomatillo has four register banks:
285234353Sdim	 * (0) per-PBM PCI configuration and status registers
286263508Sdim	 * (1) per-PBM Tomatillo controller configuration registers, but on
287263508Sdim	 *     machines having the `jbusppm' device shared with its Estar
288234353Sdim	 *     register bank for bus A
289234353Sdim	 * (2) per-PBM PCI configuration space
290234353Sdim	 * (3) per-PBM interrupt concentrator registers
291234353Sdim	 */
292234353Sdim	sc->sc_half = (bus_get_resource_start(dev, SYS_RES_MEMORY, STX_PCI) >>
293263508Sdim	    20) & 1;
294234353Sdim	for (n = 0; n < (mode == SCHIZO_MODE_SCZ ? SCZ_NREG : TOM_NREG);
295234353Sdim	    n++) {
296234353Sdim		rid = n;
297234353Sdim		sc->sc_mem_res[n] = bus_alloc_resource_any(dev,
298263508Sdim		    SYS_RES_MEMORY, &rid,
299234353Sdim		    (((mode == SCHIZO_MODE_SCZ && ((sc->sc_half == 1 &&
300234353Sdim		    n == STX_PCI) || n == STX_CTRL)) ||
301234353Sdim		    (mode == SCHIZO_MODE_TOM && sc->sc_half == 0 &&
302234353Sdim		    n == STX_CTRL)) ? RF_SHAREABLE : 0) | RF_ACTIVE);
303234353Sdim		if (sc->sc_mem_res[n] == NULL)
304234353Sdim			panic("%s: could not allocate register bank %d",
305263508Sdim			    __func__, n);
306263508Sdim	}
307234353Sdim
308263508Sdim	/*
309263508Sdim	 * Match other Schizos that are already configured against
310263508Sdim	 * the controller base physical address.  This will be the
311234353Sdim	 * same for a pair of devices that share register space.
312234353Sdim	 */
313234353Sdim	osc = NULL;
314234353Sdim	SLIST_FOREACH(asc, &schizo_softcs, sc_link) {
315234353Sdim		if (rman_get_start(asc->sc_mem_res[STX_CTRL]) ==
316234353Sdim		    rman_get_start(sc->sc_mem_res[STX_CTRL])) {
317234353Sdim			/* Found partner. */
318234353Sdim			osc = asc;
319234353Sdim			break;
320234353Sdim		}
321234353Sdim	}
322234353Sdim	if (osc == NULL) {
323234353Sdim		sc->sc_mtx = malloc(sizeof(*sc->sc_mtx), M_DEVBUF,
324234353Sdim		    M_NOWAIT | M_ZERO);
325263508Sdim		if (sc->sc_mtx == NULL)
326263508Sdim			panic("%s: could not malloc mutex", __func__);
327263508Sdim		mtx_init(sc->sc_mtx, "pcib_mtx", NULL, MTX_SPIN);
328234353Sdim	} else {
329234353Sdim		if (sc->sc_mode != SCHIZO_MODE_SCZ)
330234353Sdim			panic("%s: no partner expected", __func__);
331234353Sdim		if (mtx_initialized(osc->sc_mtx) == 0)
332234353Sdim			panic("%s: mutex not initialized", __func__);
333234353Sdim		sc->sc_mtx = osc->sc_mtx;
334193323Sed	}
335218893Sdim
336218893Sdim	if (OF_getprop(node, "portid", &sc->sc_ign, sizeof(sc->sc_ign)) == -1)
337249423Sdim		panic("%s: could not determine IGN", __func__);
338218893Sdim	if (OF_getprop(node, "version#", &sc->sc_ver, sizeof(sc->sc_ver)) == -1)
339218893Sdim		panic("%s: could not determine version", __func__);
340218893Sdim	if (OF_getprop(node, "clock-frequency", &prop, sizeof(prop)) == -1)
341218893Sdim		prop = 33000000;
342218893Sdim
343193323Sed	device_printf(dev, "%s, version %d, IGN %#x, bus %c, %dMHz\n",
344234353Sdim	    desc->sd_name, sc->sc_ver, sc->sc_ign, 'A' + sc->sc_half,
345234353Sdim	    prop / 1000 / 1000);
346193323Sed
347193323Sed	/* Set up the PCI interrupt retry timer. */
348193323Sed#ifdef SCHIZO_DEBUG
349193323Sed	device_printf(dev, "PCI IRT 0x%016llx\n", (unsigned long long)
350193323Sed	    SCHIZO_PCI_READ_8(sc, STX_PCI_INTR_RETRY_TIM));
351193323Sed#endif
352234353Sdim	SCHIZO_PCI_WRITE_8(sc, STX_PCI_INTR_RETRY_TIM, 5);
353234353Sdim
354234353Sdim	/* Set up the PCI control register. */
355234353Sdim	reg = SCHIZO_PCI_READ_8(sc, STX_PCI_CTRL);
356203954Srdivacky	reg |= STX_PCI_CTRL_MMU_IEN | STX_PCI_CTRL_SBH_IEN |
357204642Srdivacky	    STX_PCI_CTRL_ERR_IEN | STX_PCI_CTRL_ARB_MASK;
358203954Srdivacky	reg &= ~(TOM_PCI_CTRL_DTO_IEN | STX_PCI_CTRL_ARB_PARK);
359203954Srdivacky	if (OF_getproplen(node, "no-bus-parking") < 0)
360193323Sed		reg |= STX_PCI_CTRL_ARB_PARK;
361193323Sed	if (mode == SCHIZO_MODE_TOM) {
362193323Sed		reg |= TOM_PCI_CTRL_PRM | TOM_PCI_CTRL_PRO | TOM_PCI_CTRL_PRL;
363193323Sed		if (sc->sc_ver <= 1)	/* revision <= 2.0 */
364193323Sed			reg |= TOM_PCI_CTRL_DTO_IEN;
365198090Srdivacky		else
366198090Srdivacky			reg |= STX_PCI_CTRL_PTO;
367198090Srdivacky	}
368198090Srdivacky#ifdef SCHIZO_DEBUG
369193323Sed	device_printf(dev, "PCI CSR 0x%016llx -> 0x%016llx\n",
370193323Sed	    (unsigned long long)SCHIZO_PCI_READ_8(sc, STX_PCI_CTRL),
371193323Sed	    (unsigned long long)reg);
372193323Sed#endif
373193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_CTRL, reg);
374193323Sed
375193323Sed	/* Set up the PCI diagnostic register. */
376193323Sed	reg = SCHIZO_PCI_READ_8(sc, STX_PCI_DIAG);
377193323Sed	reg &= ~(SCZ_PCI_DIAG_RTRYARB_DIS | STX_PCI_DIAG_RETRY_DIS |
378193323Sed	    STX_PCI_DIAG_INTRSYNC_DIS);
379193323Sed#ifdef SCHIZO_DEBUG
380234353Sdim	device_printf(dev, "PCI DR 0x%016llx -> 0x%016llx\n",
381193323Sed	    (unsigned long long)SCHIZO_PCI_READ_8(sc, STX_PCI_DIAG),
382193323Sed	    (unsigned long long)reg);
383193323Sed#endif
384193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_DIAG, reg);
385193323Sed
386193323Sed	/*
387193323Sed	 * On Tomatillo clear the I/O prefetch lengths (workaround for a
388193323Sed	 * Jalapeno bug).
389193323Sed	 */
390193323Sed	if (mode == SCHIZO_MODE_TOM)
391193323Sed		SCHIZO_PCI_WRITE_8(sc, TOM_PCI_IOC_CSR, TOM_PCI_IOC_PW |
392205218Srdivacky		    (1 << TOM_PCI_IOC_PREF_OFF_SHIFT) | TOM_PCI_IOC_CPRM |
393193323Sed		    TOM_PCI_IOC_CPRO | TOM_PCI_IOC_CPRL);
394193323Sed
395193323Sed	/*
396193323Sed	 * Hunt through all the interrupt mapping regs and register
397193323Sed	 * the interrupt controller for our interrupt vectors.  We do
398193323Sed	 * this early in order to be able to catch stray interrupts.
399193323Sed	 * This is complicated by the fact that a pair of Schizo PBMs
400193323Sed	 * shares one IGN.
401193323Sed	 */
402193323Sed	n = OF_getprop(node, "ino-bitmap", (void *)prop_array,
403193323Sed	    sizeof(prop_array));
404193323Sed	if (n == -1)
405234353Sdim		panic("%s: could not get ino-bitmap", __func__);
406193323Sed	ino_bitmap = ((uint64_t)prop_array[1] << 32) | prop_array[0];
407193323Sed	for (n = 0; n <= STX_MAX_INO; n++) {
408193323Sed		if ((ino_bitmap & (1ULL << n)) == 0)
409193323Sed			continue;
410234353Sdim		if (n == STX_FB0_INO || n == STX_FB1_INO)
411234353Sdim			/* Leave for upa(4). */
412234353Sdim			continue;
413234353Sdim		i = schizo_intr_register(sc, n);
414234353Sdim		if (i != 0)
415234353Sdim			device_printf(dev, "could not register interrupt "
416234353Sdim			    "controller for INO %d (%d)\n", n, i);
417234353Sdim	}
418234353Sdim
419234353Sdim	/*
420234353Sdim	 * Setup Safari/JBus performance counter 0 in bus cycle counting
421234353Sdim	 * mode as timecounter.  Unfortunately, this is broken with at
422234353Sdim	 * least the version 4 Tomatillos found in Fire V120 and Blade
423234353Sdim	 * 1500, which apparently actually count some different event at
424234353Sdim	 * ~0.5 and 3MHz respectively instead (also when running in full
425234353Sdim	 * power mode).  Besides, one counter seems to be shared by a
426234353Sdim	 * "pair" of Tomatillos, too.
427234353Sdim	 */
428249423Sdim	if (sc->sc_half == 0) {
429234353Sdim		SCHIZO_CTRL_WRITE_8(sc, STX_CTRL_PERF,
430234353Sdim		    (STX_CTRL_PERF_DIS << STX_CTRL_PERF_CNT1_SHIFT) |
431193323Sed		    (STX_CTRL_PERF_BUSCYC << STX_CTRL_PERF_CNT0_SHIFT));
432193323Sed		tc = malloc(sizeof(*tc), M_DEVBUF, M_NOWAIT | M_ZERO);
433193323Sed		if (tc == NULL)
434234353Sdim			panic("%s: could not malloc timecounter", __func__);
435206274Srdivacky		tc->tc_get_timecount = schizo_get_timecount;
436193323Sed		tc->tc_poll_pps = NULL;
437193323Sed		tc->tc_counter_mask = STX_CTRL_PERF_CNT_MASK;
438193323Sed		if (OF_getprop(OF_peer(0), "clock-frequency", &prop,
439193323Sed		    sizeof(prop)) == -1)
440193323Sed			panic("%s: could not determine clock frequency",
441193323Sed			    __func__);
442193323Sed		tc->tc_frequency = prop;
443234353Sdim		tc->tc_name = strdup(device_get_nameunit(dev), M_DEVBUF);
444193323Sed		if (mode == SCHIZO_MODE_SCZ)
445234353Sdim			tc->tc_quality = SCHIZO_PERF_CNT_QLTY;
446198090Srdivacky		else
447193323Sed			tc->tc_quality = -SCHIZO_PERF_CNT_QLTY;
448193323Sed		tc->tc_priv = sc;
449193323Sed		tc_init(tc);
450234353Sdim	}
451193323Sed
452234353Sdim	/* Set up the IOMMU.  Both Schizo and Tomatillo have one per PBM. */
453193323Sed	sc->sc_is.is_pmaxaddr = IOMMU_MAXADDR(STX_IOMMU_BITS);
454234353Sdim	sc->sc_is.is_sb[0] = 0;
455234353Sdim	sc->sc_is.is_sb[1] = 0;
456193323Sed#ifdef notyet
457193323Sed	if (OF_getproplen(node, "no-streaming-cache") < 0)
458223017Sdim		sc->sc_is.is_sb[0] = STX_PCI_STRBUF;
459263508Sdim#endif
460234353Sdim
461193323Sed#define	TSBCASE(x)							\
462234353Sdim	case (IOTSB_BASESZ << (x)) << (IO_PAGE_SHIFT - IOTTE_SHIFT):	\
463234353Sdim		tsbsize = (x);						\
464234353Sdim		break;							\
465234353Sdim
466263508Sdim	n = OF_getprop(node, "virtual-dma", (void *)prop_array,
467234353Sdim	    sizeof(prop_array));
468234353Sdim	if (n == -1 || n != sizeof(prop_array))
469234353Sdim		schizo_iommu_init(sc, 7, -1);
470234353Sdim	else {
471234353Sdim		switch (prop_array[1]) {
472234353Sdim		TSBCASE(1);
473234353Sdim		TSBCASE(2);
474193323Sed		TSBCASE(3);
475193323Sed		TSBCASE(4);
476193323Sed		TSBCASE(5);
477193323Sed		TSBCASE(6);
478193323Sed		TSBCASE(7);
479193323Sed		TSBCASE(8);
480193323Sed		default:
481193323Sed			panic("%s: unsupported DVMA size 0x%x",
482234353Sdim			    __func__, prop_array[1]);
483193323Sed			/* NOTREACHED */
484234353Sdim		}
485193323Sed		schizo_iommu_init(sc, tsbsize, prop_array[0]);
486193323Sed	}
487234353Sdim
488193323Sed#undef TSBCASE
489234353Sdim
490198090Srdivacky	/* Initialize memory and I/O rmans. */
491193323Sed	sc->sc_pci_io_rman.rm_type = RMAN_ARRAY;
492193323Sed	sc->sc_pci_io_rman.rm_descr = "Schizo PCI I/O Ports";
493193323Sed	if (rman_init(&sc->sc_pci_io_rman) != 0 ||
494193323Sed	    rman_manage_region(&sc->sc_pci_io_rman, 0, STX_IO_SIZE) != 0)
495193323Sed		panic("%s: failed to set up I/O rman", __func__);
496193323Sed	sc->sc_pci_mem_rman.rm_type = RMAN_ARRAY;
497193323Sed	sc->sc_pci_mem_rman.rm_descr = "Schizo PCI Memory";
498193323Sed	if (rman_init(&sc->sc_pci_mem_rman) != 0 ||
499212904Sdim	    rman_manage_region(&sc->sc_pci_mem_rman, 0, STX_MEM_SIZE) != 0)
500212904Sdim		panic("%s: failed to set up memory rman", __func__);
501193323Sed
502212904Sdim	nrange = OF_getprop_alloc(node, "ranges", sizeof(*range),
503193323Sed	    (void **)&range);
504193323Sed	/*
505193323Sed	 * Make sure that the expected ranges are present.  The
506193323Sed	 * OFW_PCI_CS_MEM64 one is not currently used though.
507193323Sed	 */
508193323Sed	if (nrange != STX_NRANGE)
509234353Sdim		panic("%s: unsupported number of ranges", __func__);
510234353Sdim	/*
511212904Sdim	 * Find the addresses of the various bus spaces.
512263508Sdim	 * There should not be multiple ones of one kind.
513263508Sdim	 * The physical start addresses of the ranges are the configuration,
514193323Sed	 * memory and I/O handles.
515193323Sed	 */
516193323Sed	for (n = 0; n < STX_NRANGE; n++) {
517193323Sed		i = OFW_PCI_RANGE_CS(&range[n]);
518193323Sed		if (sc->sc_pci_bh[i] != 0)
519193323Sed			panic("%s: duplicate range for space %d", __func__, i);
520234353Sdim		sc->sc_pci_bh[i] = OFW_PCI_RANGE_PHYS(&range[n]);
521193323Sed	}
522193323Sed	free(range, M_OFWPROP);
523193323Sed
524193323Sed	/* Register the softc, this is needed for paired Schizos. */
525193323Sed	SLIST_INSERT_HEAD(&schizo_softcs, sc, sc_link);
526193323Sed
527193323Sed	/* Allocate our tags. */
528193323Sed	sc->sc_pci_memt = schizo_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
529193323Sed	sc->sc_pci_iot = schizo_alloc_bus_tag(sc, PCI_IO_BUS_SPACE);
530193323Sed	sc->sc_pci_cfgt = schizo_alloc_bus_tag(sc, PCI_CONFIG_BUS_SPACE);
531193323Sed	if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
532193323Sed	    sc->sc_is.is_pmaxaddr, ~0, NULL, NULL, sc->sc_is.is_pmaxaddr,
533234353Sdim	    0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_pci_dmat) != 0)
534193323Sed		panic("%s: bus_dma_tag_create failed", __func__);
535193323Sed	/* Customize the tag. */
536193323Sed	sc->sc_pci_dmat->dt_cookie = &sc->sc_is;
537193323Sed	sc->sc_pci_dmat->dt_mt = &iommu_dma_methods;
538234353Sdim
539193323Sed	/*
540193323Sed	 * Get the bus range from the firmware.
541193323Sed	 * NB: Tomatillos don't support PCI bus reenumeration.
542212904Sdim	 */
543193323Sed	n = OF_getprop(node, "bus-range", (void *)prop_array,
544193323Sed	    sizeof(prop_array));
545193323Sed	if (n == -1)
546193323Sed		panic("%s: could not get bus-range", __func__);
547193323Sed	if (n != sizeof(prop_array))
548193323Sed		panic("%s: broken bus-range (%d)", __func__, n);
549193323Sed	if (bootverbose)
550193323Sed		device_printf(dev, "bus range %u to %u; PCI bus %d\n",
551193323Sed		    prop_array[0], prop_array[1], prop_array[0]);
552193323Sed	sc->sc_pci_secbus = prop_array[0];
553193323Sed
554193323Sed	/* Clear any pending PCI error bits. */
555193323Sed	PCIB_WRITE_CONFIG(dev, sc->sc_pci_secbus, STX_CS_DEVICE, STX_CS_FUNC,
556193323Sed	    PCIR_STATUS, PCIB_READ_CONFIG(dev, sc->sc_pci_secbus,
557193323Sed	    STX_CS_DEVICE, STX_CS_FUNC, PCIR_STATUS, 2), 2);
558193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_CTRL,
559193323Sed	    SCHIZO_PCI_READ_8(sc, STX_PCI_CTRL));
560193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_AFSR,
561193323Sed	    SCHIZO_PCI_READ_8(sc, STX_PCI_AFSR));
562193323Sed
563193323Sed	/*
564193323Sed	 * Establish handlers for interesting interrupts...
565198090Srdivacky	 * Someone at Sun clearly was smoking crack; with Schizos PCI
566234353Sdim	 * bus error interrupts for one PBM can be routed to the other
567193323Sed	 * PBM though we obviously need to use the softc of the former
568193323Sed	 * as the argument for the interrupt handler and the softc of
569193323Sed	 * the latter as the argument for the interrupt controller.
570193323Sed	 */
571193323Sed	if (sc->sc_half == 0) {
572193323Sed		if ((ino_bitmap & (1ULL << STX_PCIERR_A_INO)) != 0 ||
573234353Sdim		    (osc != NULL && ((struct schizo_icarg *)intr_vectors[
574234353Sdim		    INTMAP_VEC(sc->sc_ign, STX_PCIERR_A_INO)].iv_icarg)->
575234353Sdim		    sica_sc == osc))
576193323Sed			/*
577193323Sed			 * We are the driver for PBM A and either also
578234353Sdim			 * registered the interrupt controller for us or
579193323Sed			 * the driver for PBM B has probed first and
580198090Srdivacky			 * registered it for us.
581234353Sdim			 */
582193323Sed			schizo_set_intr(sc, 0, STX_PCIERR_A_INO,
583198090Srdivacky			    schizo_pci_bus);
584193323Sed		if ((ino_bitmap & (1ULL << STX_PCIERR_B_INO)) != 0 &&
585193323Sed		    osc != NULL)
586193323Sed			/*
587193323Sed			 * We are the driver for PBM A but registered
588234353Sdim			 * the interrupt controller for PBM B, i.e. the
589193323Sed			 * driver for PBM B attached first but couldn't
590234353Sdim			 * set up a handler for PBM B.
591193323Sed			 */
592234353Sdim			schizo_set_intr(osc, 0, STX_PCIERR_B_INO,
593234353Sdim			    schizo_pci_bus);
594234353Sdim	} else {
595234353Sdim		if ((ino_bitmap & (1ULL << STX_PCIERR_B_INO)) != 0 ||
596234353Sdim		    (osc != NULL && ((struct schizo_icarg *)intr_vectors[
597263508Sdim		    INTMAP_VEC(sc->sc_ign, STX_PCIERR_B_INO)].iv_icarg)->
598234353Sdim		    sica_sc == osc))
599234353Sdim			/*
600193323Sed			 * We are the driver for PBM B and either also
601263508Sdim			 * registered the interrupt controller for us or
602234353Sdim			 * the driver for PBM A has probed first and
603234353Sdim			 * registered it for us.
604234353Sdim			 */
605234353Sdim			schizo_set_intr(sc, 0, STX_PCIERR_B_INO,
606234353Sdim			    schizo_pci_bus);
607234353Sdim		if ((ino_bitmap & (1ULL << STX_PCIERR_A_INO)) != 0 &&
608234353Sdim		    osc != NULL)
609193323Sed			/*
610193323Sed			 * We are the driver for PBM B but registered
611234353Sdim			 * the interrupt controller for PBM A, i.e. the
612193323Sed			 * driver for PBM A attached first but couldn't
613193323Sed			 * set up a handler for PBM A.
614193323Sed			 */
615193323Sed			schizo_set_intr(osc, 0, STX_PCIERR_A_INO,
616193323Sed			    schizo_pci_bus);
617193323Sed	}
618234353Sdim	if ((ino_bitmap & (1ULL << STX_UE_INO)) != 0)
619193323Sed		schizo_set_intr(sc, 1, STX_UE_INO, schizo_ue);
620234353Sdim	if ((ino_bitmap & (1ULL << STX_CE_INO)) != 0)
621193323Sed		schizo_set_intr(sc, 2, STX_CE_INO, schizo_ce);
622193323Sed	if ((ino_bitmap & (1ULL << STX_BUS_INO)) != 0)
623193323Sed		schizo_set_intr(sc, 3, STX_BUS_INO, schizo_host_bus);
624193323Sed
625193323Sed	/*
626193323Sed	 * According to the Schizo Errata I-13, consistent DMA flushing/
627193323Sed	 * syncing is FUBAR in version < 5 (i.e. revision < 2.3) bridges,
628193323Sed	 * so we can't use it and need to live with the consequences.
629193323Sed	 * With Schizo version >= 5, CDMA flushing/syncing is usable
630193323Sed	 * but requires the the workaround described in Schizo Errata
631193323Sed	 * I-23.  With Tomatillo and XMITS, CDMA flushing/syncing works
632193323Sed	 * as expected, Tomatillo version <= 4 (i.e. revision <= 2.3)
633193323Sed	 * bridges additionally require a block store after a write to
634193323Sed	 * TOMXMS_PCI_DMA_SYNC_PEND though.
635203954Srdivacky	 */
636200581Srdivacky	if ((sc->sc_mode == SCHIZO_MODE_SCZ && sc->sc_ver >= 5) ||
637206124Srdivacky	    sc->sc_mode == SCHIZO_MODE_TOM || sc->sc_mode == SCHIZO_MODE_XMS) {
638206124Srdivacky		sc->sc_flags |= SCHIZO_FLAGS_CDMA;
639234982Sdim		if (sc->sc_mode == SCHIZO_MODE_SCZ) {
640206124Srdivacky			n = STX_CDMA_A_INO + sc->sc_half;
641206124Srdivacky			if (bus_set_resource(dev, SYS_RES_IRQ, 5,
642206124Srdivacky			    INTMAP_VEC(sc->sc_ign, n), 1) != 0)
643206124Srdivacky				panic("%s: failed to add CDMA interrupt",
644206124Srdivacky				    __func__);
645206124Srdivacky			i = schizo_intr_register(sc, n);
646193323Sed			if (i != 0)
647206124Srdivacky				panic("%s: could not register interrupt "
648193323Sed				    "controller for CDMA (%d)", __func__, i);
649206124Srdivacky			(void)schizo_get_intrmap(sc, n, NULL,
650243830Sdim			   &sc->sc_cdma_clr);
651249423Sdim			sc->sc_cdma_state = SCHIZO_CDMA_STATE_DONE;
652249423Sdim			schizo_set_intr(sc, 5, n, schizo_cdma);
653206124Srdivacky		}
654193323Sed		if (sc->sc_mode == SCHIZO_MODE_TOM && sc->sc_ver <= 4)
655206124Srdivacky			sc->sc_flags |= SCHIZO_FLAGS_BSWAR;
656193323Sed	}
657193323Sed
658193323Sed	/*
659193323Sed	 * Set the latency timer register as this isn't always done by the
660193323Sed	 * firmware.
661218893Sdim	 */
662193323Sed	PCIB_WRITE_CONFIG(dev, sc->sc_pci_secbus, STX_CS_DEVICE, STX_CS_FUNC,
663193323Sed	    PCIR_LATTIMER, OFW_PCI_LATENCY, 1);
664193323Sed
665193323Sed	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(ofw_pci_intr_t));
666193323Sed
667193323Sed	device_add_child(dev, "pci", -1);
668193323Sed	return (bus_generic_attach(dev));
669193323Sed}
670193323Sed
671193323Sedstatic void
672193323Sedschizo_set_intr(struct schizo_softc *sc, u_int index, u_int ino,
673193323Sed    driver_filter_t handler)
674193323Sed{
675193323Sed	u_long vec;
676193323Sed	int rid;
677193323Sed
678193323Sed	rid = index;
679193323Sed	sc->sc_irq_res[index] = bus_alloc_resource_any(sc->sc_dev, SYS_RES_IRQ,
680193323Sed	    &rid, RF_ACTIVE);
681193323Sed	if (sc->sc_irq_res[index] == NULL ||
682193323Sed	    INTIGN(vec = rman_get_start(sc->sc_irq_res[index])) != sc->sc_ign ||
683193323Sed	    INTINO(vec) != ino ||
684193323Sed	    intr_vectors[vec].iv_ic != &schizo_ic ||
685193323Sed	    bus_setup_intr(sc->sc_dev, sc->sc_irq_res[index],
686193323Sed	    INTR_TYPE_MISC | INTR_FAST, handler, NULL, sc,
687193323Sed	    &sc->sc_ihand[index]) != 0)
688193323Sed		panic("%s: failed to set up interrupt %d", __func__, index);
689198090Srdivacky}
690198090Srdivacky
691226633Sdimstatic int
692198090Srdivackyschizo_intr_register(struct schizo_softc *sc, u_int ino)
693193323Sed{
694193323Sed	struct schizo_icarg *sica;
695193323Sed	bus_addr_t intrclr, intrmap;
696193323Sed	int error;
697198090Srdivacky
698198090Srdivacky	if (schizo_get_intrmap(sc, ino, &intrmap, &intrclr) == 0)
699198090Srdivacky		return (ENXIO);
700198090Srdivacky	sica = malloc(sizeof(*sica), M_DEVBUF, M_NOWAIT);
701234982Sdim	if (sica == NULL)
702234982Sdim		return (ENOMEM);
703193323Sed	sica->sica_sc = sc;
704193323Sed	sica->sica_map = intrmap;
705193323Sed	sica->sica_clr = intrclr;
706193323Sed#ifdef SCHIZO_DEBUG
707234353Sdim	device_printf(sc->sc_dev, "intr map (INO %d) %#lx: %#lx, clr: %#lx\n",
708193323Sed	    ino, (u_long)intrmap, (u_long)SCHIZO_PCI_READ_8(sc, intrmap),
709193323Sed	    (u_long)intrclr);
710234353Sdim#endif
711234353Sdim	error = (intr_controller_register(INTMAP_VEC(sc->sc_ign, ino),
712193323Sed	    &schizo_ic, sica));
713202375Srdivacky	if (error != 0)
714198090Srdivacky		free(sica, M_DEVBUF);
715198090Srdivacky	return (error);
716198090Srdivacky}
717234353Sdim
718193323Sedstatic int
719193323Sedschizo_get_intrmap(struct schizo_softc *sc, u_int ino, bus_addr_t *intrmapptr,
720193323Sed    bus_addr_t *intrclrptr)
721193323Sed{
722193323Sed	bus_addr_t intrclr, intrmap;
723193323Sed	uint64_t mr;
724193323Sed
725193323Sed	/*
726234353Sdim	 * XXX we only look for INOs rather than INRs since the firmware
727193323Sed	 * may not provide the IGN and the IGN is constant for all devices
728193323Sed	 * on that PCI controller.
729193323Sed	 */
730193323Sed
731193323Sed	if (ino > STX_MAX_INO) {
732193323Sed		device_printf(sc->sc_dev, "out of range INO %d requested\n",
733234353Sdim		    ino);
734234353Sdim		return (0);
735193323Sed	}
736234353Sdim
737193323Sed	intrmap = STX_PCI_IMAP_BASE + (ino << 3);
738193323Sed	intrclr = STX_PCI_ICLR_BASE + (ino << 3);
739193323Sed	mr = SCHIZO_PCI_READ_8(sc, intrmap);
740193323Sed	if (INTINO(mr) != ino) {
741193323Sed		device_printf(sc->sc_dev,
742193323Sed		    "interrupt map entry does not match INO (%d != %d)\n",
743234353Sdim		    (int)INTINO(mr), ino);
744193323Sed		return (0);
745193323Sed	}
746193323Sed
747193323Sed	if (intrmapptr != NULL)
748193323Sed		*intrmapptr = intrmap;
749193323Sed	if (intrclrptr != NULL)
750193323Sed		*intrclrptr = intrclr;
751193323Sed	return (1);
752193323Sed}
753234353Sdim
754263508Sdim/*
755193323Sed * Interrupt handlers
756193323Sed */
757193323Sedstatic int
758198090Srdivackyschizo_pci_bus(void *arg)
759198090Srdivacky{
760226633Sdim	struct schizo_softc *sc = arg;
761223017Sdim	uint64_t afar, afsr, csr, iommu;
762223017Sdim	uint32_t status;
763226633Sdim
764234353Sdim	afar = SCHIZO_PCI_READ_8(sc, STX_PCI_AFAR);
765226633Sdim	afsr = SCHIZO_PCI_READ_8(sc, STX_PCI_AFSR);
766226633Sdim	csr = SCHIZO_PCI_READ_8(sc, STX_PCI_CTRL);
767226633Sdim	iommu = SCHIZO_PCI_READ_8(sc, STX_PCI_IOMMU);
768226633Sdim	status = PCIB_READ_CONFIG(sc->sc_dev, sc->sc_pci_secbus,
769226633Sdim	    STX_CS_DEVICE, STX_CS_FUNC, PCIR_STATUS, 2);
770193323Sed	if ((csr & STX_PCI_CTRL_MMU_ERR) != 0) {
771193323Sed		if ((iommu & TOM_PCI_IOMMU_ERR) == 0)
772193323Sed			goto clear_error;
773234353Sdim
774234353Sdim		/* These are non-fatal if target abort was signaled. */
775193323Sed		if ((status & PCIM_STATUS_STABORT) != 0 &&
776234353Sdim		    ((iommu & TOM_PCI_IOMMU_ERRMASK) ==
777193323Sed		    TOM_PCI_IOMMU_INVALID_ERR ||
778193323Sed		    (iommu & TOM_PCI_IOMMU_ERR_ILLTSBTBW) != 0 ||
779202375Srdivacky		    (iommu & TOM_PCI_IOMMU_ERR_BAD_VA) != 0)) {
780198090Srdivacky			SCHIZO_PCI_WRITE_8(sc, STX_PCI_IOMMU, iommu);
781198090Srdivacky			goto clear_error;
782198090Srdivacky		}
783193323Sed	}
784218893Sdim
785218893Sdim	panic("%s: PCI bus %c error AFAR %#llx AFSR %#llx PCI CSR %#llx "
786218893Sdim	    "IOMMU %#llx STATUS %#llx", device_get_name(sc->sc_dev),
787193323Sed	    'A' + sc->sc_half, (unsigned long long)afar,
788193323Sed	    (unsigned long long)afsr, (unsigned long long)csr,
789193323Sed	    (unsigned long long)iommu, (unsigned long long)status);
790193323Sed
791193323Sed clear_error:
792193323Sed	if (bootverbose)
793193323Sed		device_printf(sc->sc_dev,
794193323Sed		    "PCI bus %c error AFAR %#llx AFSR %#llx PCI CSR %#llx "
795193323Sed		    "STATUS %#llx", 'A' + sc->sc_half,
796193323Sed		    (unsigned long long)afar, (unsigned long long)afsr,
797193323Sed		    (unsigned long long)csr, (unsigned long long)status);
798193323Sed	/* Clear the error bits that we caught. */
799193323Sed	PCIB_WRITE_CONFIG(sc->sc_dev, sc->sc_pci_secbus, STX_CS_DEVICE,
800193323Sed	    STX_CS_FUNC, PCIR_STATUS, status, 2);
801193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_CTRL, csr);
802193323Sed	SCHIZO_PCI_WRITE_8(sc, STX_PCI_AFSR, afsr);
803193323Sed	return (FILTER_HANDLED);
804193323Sed}
805193323Sed
806193323Sedstatic int
807193323Sedschizo_ue(void *arg)
808193323Sed{
809193323Sed	struct schizo_softc *sc = arg;
810193323Sed	uint64_t afar, afsr;
811193323Sed	int i;
812193323Sed
813193323Sed	mtx_lock_spin(sc->sc_mtx);
814193323Sed	afar = SCHIZO_CTRL_READ_8(sc, STX_CTRL_UE_AFAR);
815193323Sed	for (i = 0; i < 1000; i++)
816218893Sdim		if (((afsr = SCHIZO_CTRL_READ_8(sc, STX_CTRL_UE_AFSR)) &
817193323Sed		    STX_CTRL_CE_AFSR_ERRPNDG) == 0)
818210299Sed			break;
819234353Sdim	mtx_unlock_spin(sc->sc_mtx);
820206274Srdivacky	panic("%s: uncorrectable DMA error AFAR %#llx AFSR %#llx",
821210299Sed	    device_get_name(sc->sc_dev), (unsigned long long)afar,
822206274Srdivacky	    (unsigned long long)afsr);
823193323Sed	return (FILTER_HANDLED);
824193323Sed}
825193323Sed
826193323Sedstatic int
827206274Srdivackyschizo_ce(void *arg)
828193323Sed{
829193323Sed	struct schizo_softc *sc = arg;
830193323Sed	uint64_t afar, afsr;
831210299Sed	int i;
832234353Sdim
833234353Sdim	mtx_lock_spin(sc->sc_mtx);
834234353Sdim	afar = SCHIZO_CTRL_READ_8(sc, STX_CTRL_CE_AFAR);
835234353Sdim	for (i = 0; i < 1000; i++)
836234353Sdim		if (((afsr = SCHIZO_CTRL_READ_8(sc, STX_CTRL_UE_AFSR)) &
837193323Sed		    STX_CTRL_CE_AFSR_ERRPNDG) == 0)
838193323Sed			break;
839193323Sed	device_printf(sc->sc_dev,
840193323Sed	    "correctable DMA error AFAR %#llx AFSR %#llx\n",
841193323Sed	    (unsigned long long)afar, (unsigned long long)afsr);
842193323Sed	/* Clear the error bits that we caught. */
843226633Sdim	SCHIZO_CTRL_WRITE_8(sc, STX_CTRL_UE_AFSR, afsr);
844193323Sed	mtx_unlock_spin(sc->sc_mtx);
845210299Sed	return (FILTER_HANDLED);
846193323Sed}
847193323Sed
848193323Sedstatic int
849234353Sdimschizo_host_bus(void *arg)
850193323Sed{
851193323Sed	struct schizo_softc *sc = arg;
852193323Sed	uint64_t errlog;
853193323Sed
854193323Sed	errlog = SCHIZO_CTRL_READ_8(sc, STX_CTRL_BUS_ERRLOG);
855193323Sed	panic("%s: %s error %#llx", device_get_name(sc->sc_dev),
856263508Sdim	    sc->sc_mode == SCHIZO_MODE_TOM ? "JBus" : "Safari",
857263508Sdim	    (unsigned long long)errlog);
858193323Sed	return (FILTER_HANDLED);
859218893Sdim}
860210299Sed
861193323Sedstatic int
862193323Sedschizo_cdma(void *arg)
863226633Sdim{
864226633Sdim	struct schizo_softc *sc = arg;
865263508Sdim
866263508Sdim	atomic_store_rel_32(&sc->sc_cdma_state, SCHIZO_CDMA_STATE_DONE);
867226633Sdim	return (FILTER_HANDLED);
868226633Sdim}
869226633Sdim
870226633Sdimstatic void
871226633Sdimschizo_iommu_init(struct schizo_softc *sc, int tsbsize, uint32_t dvmabase)
872226633Sdim{
873226633Sdim
874226633Sdim	/* Punch in our copies. */
875226633Sdim	sc->sc_is.is_bustag = rman_get_bustag(sc->sc_mem_res[STX_PCI]);
876193323Sed	sc->sc_is.is_bushandle = rman_get_bushandle(sc->sc_mem_res[STX_PCI]);
877193323Sed	sc->sc_is.is_iommu = STX_PCI_IOMMU;
878193323Sed	sc->sc_is.is_dtag = STX_PCI_IOMMU_TLB_TAG_DIAG;
879193323Sed	sc->sc_is.is_ddram = STX_PCI_IOMMU_TLB_DATA_DIAG;
880193323Sed	sc->sc_is.is_dqueue = STX_PCI_IOMMU_QUEUE_DIAG;
881193323Sed	sc->sc_is.is_dva = STX_PCI_IOMMU_SVADIAG;
882218893Sdim	sc->sc_is.is_dtcmp = STX_PCI_IOMMU_TLB_CMP_DIAG;
883234353Sdim
884193323Sed	iommu_init(device_get_nameunit(sc->sc_dev), &sc->sc_is, tsbsize,
885193323Sed	    dvmabase, 0);
886193323Sed}
887193323Sed
888193323Sedstatic int
889193323Sedschizo_maxslots(device_t dev)
890193323Sed{
891193323Sed	struct schizo_softc *sc;
892193323Sed
893193323Sed	sc = device_get_softc(dev);
894193323Sed	if (sc->sc_mode == SCHIZO_MODE_SCZ)
895193323Sed		return (sc->sc_half == 0 ? 4 : 6);
896193323Sed
897207618Srdivacky	/* XXX: is this correct? */
898207618Srdivacky	return (PCI_SLOTMAX);
899207618Srdivacky}
900207618Srdivacky
901207618Srdivackystatic uint32_t
902234353Sdimschizo_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
903193323Sed    int width)
904193323Sed{
905206274Srdivacky	struct schizo_softc *sc;
906207618Srdivacky	bus_space_handle_t bh;
907193323Sed	u_long offset = 0;
908207618Srdivacky	uint32_t r, wrd;
909207618Srdivacky	int i;
910207618Srdivacky	uint16_t shrt;
911207618Srdivacky	uint8_t byte;
912207618Srdivacky
913193323Sed	sc = device_get_softc(dev);
914193323Sed
915193323Sed	/*
916193323Sed	 * The Schizo bridges contain a dupe of their header at 0x80.
917234353Sdim	 */
918193323Sed	if (sc->sc_mode == SCHIZO_MODE_SCZ && bus == sc->sc_pci_secbus &&
919243830Sdim	    slot == STX_CS_DEVICE && func == STX_CS_FUNC &&
920243830Sdim	    reg + width > 0x80)
921243830Sdim		return (0);
922193323Sed
923193323Sed	offset = STX_CONF_OFF(bus, slot, func, reg);
924193323Sed	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
925193323Sed	switch (width) {
926234353Sdim	case 1:
927193323Sed		i = bus_space_peek_1(sc->sc_pci_cfgt, bh, offset, &byte);
928193323Sed		r = byte;
929202375Srdivacky		break;
930193323Sed	case 2:
931193323Sed		i = bus_space_peek_2(sc->sc_pci_cfgt, bh, offset, &shrt);
932193323Sed		r = shrt;
933193323Sed		break;
934193323Sed	case 4:
935193323Sed		i = bus_space_peek_4(sc->sc_pci_cfgt, bh, offset, &wrd);
936193323Sed		r = wrd;
937193323Sed		break;
938193323Sed	default:
939193323Sed		panic("%s: bad width", __func__);
940193323Sed		/* NOTREACHED */
941193323Sed	}
942193323Sed
943193323Sed	if (i) {
944193323Sed#ifdef SCHIZO_DEBUG
945193323Sed		printf("%s: read data error reading: %d.%d.%d: 0x%x\n",
946193323Sed		    __func__, bus, slot, func, reg);
947193323Sed#endif
948193323Sed		r = -1;
949193323Sed	}
950193323Sed	return (r);
951193323Sed}
952193323Sed
953193323Sedstatic void
954193323Sedschizo_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
955193323Sed    uint32_t val, int width)
956193323Sed{
957193323Sed	struct schizo_softc *sc;
958193323Sed	bus_space_handle_t bh;
959193323Sed	u_long offset = 0;
960193323Sed
961193323Sed	sc = device_get_softc(dev);
962193323Sed	offset = STX_CONF_OFF(bus, slot, func, reg);
963193323Sed	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
964193323Sed	switch (width) {
965234353Sdim	case 1:
966193323Sed		bus_space_write_1(sc->sc_pci_cfgt, bh, offset, val);
967193323Sed		break;
968193323Sed	case 2:
969193323Sed		bus_space_write_2(sc->sc_pci_cfgt, bh, offset, val);
970193323Sed		break;
971193323Sed	case 4:
972234353Sdim		bus_space_write_4(sc->sc_pci_cfgt, bh, offset, val);
973193323Sed		break;
974193323Sed	default:
975193323Sed		panic("%s: bad width", __func__);
976198090Srdivacky		/* NOTREACHED */
977193323Sed	}
978193323Sed}
979193323Sed
980198090Srdivackystatic int
981203954Srdivackyschizo_route_interrupt(device_t bridge, device_t dev, int pin)
982193323Sed{
983193323Sed	struct schizo_softc *sc;
984193323Sed	struct ofw_pci_register reg;
985193323Sed	ofw_pci_intr_t pintr, mintr;
986234353Sdim	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
987193323Sed
988234353Sdim	sc = device_get_softc(bridge);
989223017Sdim	pintr = pin;
990223017Sdim	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, &reg,
991223017Sdim	    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr), maskbuf))
992223017Sdim		return (mintr);
993223017Sdim
994223017Sdim	device_printf(bridge, "could not route pin %d for device %d.%d\n",
995223017Sdim	    pin, pci_get_slot(dev), pci_get_function(dev));
996234353Sdim	return (PCI_INVALID_IRQ);
997263508Sdim}
998263508Sdim
999234353Sdimstatic int
1000234353Sdimschizo_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1001206274Srdivacky{
1002206274Srdivacky	struct schizo_softc *sc;
1003206274Srdivacky
1004234353Sdim	sc = device_get_softc(dev);
1005206274Srdivacky	switch (which) {
1006206274Srdivacky	case PCIB_IVAR_DOMAIN:
1007206274Srdivacky		*result = device_get_unit(dev);
1008223017Sdim		return (0);
1009223017Sdim	case PCIB_IVAR_BUS:
1010223017Sdim		*result = sc->sc_pci_secbus;
1011223017Sdim		return (0);
1012206274Srdivacky	}
1013193323Sed	return (ENOENT);
1014206274Srdivacky}
1015193323Sed
1016234353Sdimstatic int
1017206274Srdivackyschizo_dma_sync_stub(void *arg)
1018206274Srdivacky{
1019206274Srdivacky	struct timeval cur, end;
1020206274Srdivacky	struct schizo_dma_sync *sds = arg;
1021206274Srdivacky	struct schizo_softc *sc = sds->sds_sc;
1022234353Sdim	uint32_t state;
1023234353Sdim
1024234353Sdim	(void)PCIB_READ_CONFIG(sds->sds_ppb, sds->sds_bus, sds->sds_slot,
1025234353Sdim	    sds->sds_func, PCIR_VENDOR, 2);
1026234353Sdim	for (; atomic_cmpset_acq_32(&sc->sc_cdma_state, SCHIZO_CDMA_STATE_DONE,
1027234353Sdim	    SCHIZO_CDMA_STATE_PENDING) == 0;)
1028206274Srdivacky		;
1029206274Srdivacky	SCHIZO_PCI_WRITE_8(sc, sc->sc_cdma_clr, 1);
1030223017Sdim	microuptime(&cur);
1031223017Sdim	end.tv_sec = 1;
1032234353Sdim	end.tv_usec = 0;
1033223017Sdim	timevaladd(&end, &cur);
1034234353Sdim	for (; (state = atomic_load_32(&sc->sc_cdma_state)) !=
1035234353Sdim	    SCHIZO_CDMA_STATE_DONE && timevalcmp(&cur, &end, <=);)
1036234353Sdim		microuptime(&cur);
1037223017Sdim	if (state != SCHIZO_CDMA_STATE_DONE)
1038223017Sdim		panic("%s: DMA does not sync", __func__);
1039223017Sdim	return (sds->sds_handler(sds->sds_arg));
1040223017Sdim}
1041223017Sdim
1042223017Sdim#define	VIS_BLOCKSIZE	64
1043223017Sdim
1044206274Srdivackystatic int
1045206274Srdivackyichip_dma_sync_stub(void *arg)
1046206274Srdivacky{
1047206274Srdivacky	static u_char buf[VIS_BLOCKSIZE] __aligned(VIS_BLOCKSIZE);
1048223017Sdim	struct timeval cur, end;
1049206274Srdivacky	struct schizo_dma_sync *sds = arg;
1050206274Srdivacky	struct schizo_softc *sc = sds->sds_sc;
1051206274Srdivacky	register_t reg, s;
1052234353Sdim
1053206274Srdivacky	(void)PCIB_READ_CONFIG(sds->sds_ppb, sds->sds_bus, sds->sds_slot,
1054206274Srdivacky	    sds->sds_func, PCIR_VENDOR, 2);
1055206274Srdivacky	SCHIZO_PCI_WRITE_8(sc, TOMXMS_PCI_DMA_SYNC_PEND, sds->sds_syncval);
1056206274Srdivacky	microuptime(&cur);
1057206274Srdivacky	end.tv_sec = 1;
1058206274Srdivacky	end.tv_usec = 0;
1059206274Srdivacky	timevaladd(&end, &cur);
1060206274Srdivacky	for (; ((reg = SCHIZO_PCI_READ_8(sc, TOMXMS_PCI_DMA_SYNC_PEND)) &
1061206274Srdivacky	    sds->sds_syncval) != 0 && timevalcmp(&cur, &end, <=);)
1062206274Srdivacky		microuptime(&cur);
1063206274Srdivacky	if ((reg & sds->sds_syncval) != 0)
1064206274Srdivacky		panic("%s: DMA does not sync", __func__);
1065206274Srdivacky
1066206274Srdivacky	if ((sc->sc_flags & SCHIZO_FLAGS_BSWAR) != 0) {
1067206274Srdivacky		s = intr_disable();
1068206274Srdivacky		reg = rd(fprs);
1069206274Srdivacky		wr(fprs, reg | FPRS_FEF, 0);
1070206274Srdivacky		__asm __volatile("stda %%f0, [%0] %1"
1071206274Srdivacky		    : : "r" (buf), "n" (ASI_BLK_COMMIT_S));
1072206274Srdivacky		membar(Sync);
1073206274Srdivacky		wr(fprs, reg, 0);
1074206274Srdivacky		intr_restore(s);
1075193323Sed	}
1076234353Sdim	return (sds->sds_handler(sds->sds_arg));
1077193323Sed}
1078193323Sed
1079193323Sedstatic void
1080198090Srdivackyschizo_intr_enable(void *arg)
1081193323Sed{
1082193323Sed	struct intr_vector *iv = arg;
1083193323Sed	struct schizo_icarg *sica = iv->iv_icarg;
1084193323Sed
1085193323Sed	SCHIZO_PCI_WRITE_8(sica->sica_sc, sica->sica_map,
1086193323Sed	    INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
1087193323Sed}
1088193323Sed
1089193323Sedstatic void
1090193323Sedschizo_intr_disable(void *arg)
1091193323Sed{
1092193323Sed	struct intr_vector *iv = arg;
1093218893Sdim	struct schizo_icarg *sica = iv->iv_icarg;
1094193323Sed
1095193323Sed	SCHIZO_PCI_WRITE_8(sica->sica_sc, sica->sica_map, iv->iv_vec);
1096202375Srdivacky}
1097234353Sdim
1098193323Sedstatic void
1099193323Sedschizo_intr_assign(void *arg)
1100193323Sed{
1101193323Sed	struct intr_vector *iv = arg;
1102193323Sed	struct schizo_icarg *sica = iv->iv_icarg;
1103193323Sed
1104193323Sed	SCHIZO_PCI_WRITE_8(sica->sica_sc, sica->sica_map, INTMAP_TID(
1105193323Sed	    SCHIZO_PCI_READ_8(sica->sica_sc, sica->sica_map), iv->iv_mid));
1106193323Sed}
1107193323Sed
1108218893Sdimstatic void
1109207618Srdivackyschizo_intr_clear(void *arg)
1110207618Srdivacky{
1111239462Sdim	struct intr_vector *iv = arg;
1112239462Sdim	struct schizo_icarg *sica = iv->iv_icarg;
1113218893Sdim
1114218893Sdim	SCHIZO_PCI_WRITE_8(sica->sica_sc, sica->sica_clr, 0);
1115218893Sdim}
1116218893Sdim
1117218893Sdimstatic int
1118193323Sedschizo_setup_intr(device_t dev, device_t child, struct resource *ires,
1119207618Srdivacky    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1120193323Sed    void **cookiep)
1121193323Sed{
1122193323Sed	devclass_t pci_devclass;
1123193323Sed	device_t cdev, pdev, pcidev;
1124193323Sed	struct schizo_dma_sync *sds;
1125193323Sed	struct schizo_softc *sc;
1126193323Sed	u_long vec;
1127193323Sed	int error, found;
1128193323Sed
1129234353Sdim	sc = device_get_softc(dev);
1130198090Srdivacky	/*
1131234353Sdim	 * Make sure the vector is fully specified.
1132193323Sed	 */
1133193323Sed	vec = rman_get_start(ires);
1134193323Sed	if (INTIGN(vec) != sc->sc_ign) {
1135234353Sdim		device_printf(dev, "invalid interrupt vector 0x%lx\n", vec);
1136224145Sdim		return (EINVAL);
1137224145Sdim	}
1138224145Sdim
1139234353Sdim	if (intr_vectors[vec].iv_ic == &schizo_ic) {
1140193323Sed		/*
1141193323Sed		 * Ensure we use the right softc in case the interrupt
1142193323Sed		 * is routed to our companion PBM for some odd reason.
1143193323Sed		 */
1144193323Sed		sc = ((struct schizo_icarg *)intr_vectors[vec].iv_icarg)->
1145193323Sed		    sica_sc;
1146234353Sdim	} else if (intr_vectors[vec].iv_ic == NULL) {
1147193323Sed		/*
1148193323Sed		 * Work around broken firmware which misses entries in
1149193323Sed		 * the ino-bitmap.
1150193323Sed		 */
1151193323Sed		error = schizo_intr_register(sc, INTINO(vec));
1152207618Srdivacky		if (error != 0) {
1153206274Srdivacky			device_printf(dev, "could not register interrupt "
1154234353Sdim			    "controller for vector 0x%lx (%d)\n", vec, error);
1155207618Srdivacky			return (error);
1156193323Sed		}
1157193323Sed		device_printf(dev, "belatedly registered as interrupt "
1158193323Sed		    "controller for vector 0x%lx\n", vec);
1159	} else {
1160		device_printf(dev,
1161		    "invalid interrupt controller for vector 0x%lx\n", vec);
1162		return (EINVAL);
1163	}
1164
1165	/*
1166	 * Install a a wrapper for CDMA flushing/syncing for devices
1167	 * behind PCI-PCI bridges if possible.
1168	 */
1169	pcidev = NULL;
1170	found = 0;
1171	pci_devclass = devclass_find("pci");
1172	for (cdev = child; cdev != dev; cdev = pdev) {
1173		pdev = device_get_parent(cdev);
1174		if (pcidev == NULL) {
1175			if (device_get_devclass(pdev) != pci_devclass)
1176				continue;
1177			pcidev = cdev;
1178			continue;
1179		}
1180		if (pci_get_class(cdev) == PCIC_BRIDGE &&
1181		    pci_get_subclass(cdev) == PCIS_BRIDGE_PCI)
1182			found = 1;
1183	}
1184	if ((sc->sc_flags & SCHIZO_FLAGS_CDMA) != 0) {
1185		sds = malloc(sizeof(*sds), M_DEVBUF, M_NOWAIT | M_ZERO);
1186		if (sds == NULL)
1187			return (ENOMEM);
1188		if (found != 0 && pcidev != NULL) {
1189			sds->sds_sc = sc;
1190			sds->sds_arg = arg;
1191			sds->sds_ppb =
1192			    device_get_parent(device_get_parent(pcidev));
1193			sds->sds_bus = pci_get_bus(pcidev);
1194			sds->sds_slot = pci_get_slot(pcidev);
1195			sds->sds_func = pci_get_function(pcidev);
1196			sds->sds_syncval = 1ULL << INTINO(vec);
1197			if (bootverbose)
1198				device_printf(dev, "installed DMA sync "
1199				    "wrapper for device %d.%d on bus %d\n",
1200				    sds->sds_slot, sds->sds_func,
1201				    sds->sds_bus);
1202
1203#define	DMA_SYNC_STUB							\
1204	(sc->sc_mode == SCHIZO_MODE_SCZ ? schizo_dma_sync_stub :	\
1205	ichip_dma_sync_stub)
1206
1207			if (intr == NULL) {
1208				sds->sds_handler = filt;
1209				error = bus_generic_setup_intr(dev, child,
1210				    ires, flags, DMA_SYNC_STUB, intr, sds,
1211				    cookiep);
1212			} else {
1213				sds->sds_handler = (driver_filter_t *)intr;
1214				error = bus_generic_setup_intr(dev, child,
1215				    ires, flags, filt, (driver_intr_t *)
1216				    DMA_SYNC_STUB, sds, cookiep);
1217			}
1218
1219#undef DMA_SYNC_STUB
1220
1221		} else
1222			error = bus_generic_setup_intr(dev, child, ires,
1223			    flags, filt, intr, arg, cookiep);
1224		if (error != 0) {
1225			free(sds, M_DEVBUF);
1226			return (error);
1227		}
1228		sds->sds_cookie = *cookiep;
1229		*cookiep = sds;
1230		return (error);
1231	} else if (found != 0)
1232		device_printf(dev, "WARNING: using devices behind PCI-PCI "
1233		    "bridges may cause data corruption\n");
1234	return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
1235	    arg, cookiep));
1236}
1237
1238static int
1239schizo_teardown_intr(device_t dev, device_t child, struct resource *vec,
1240    void *cookie)
1241{
1242	struct schizo_dma_sync *sds;
1243	struct schizo_softc *sc;
1244	int error;
1245
1246	sc = device_get_softc(dev);
1247	if ((sc->sc_flags & SCHIZO_FLAGS_CDMA) != 0) {
1248		sds = cookie;
1249		error = bus_generic_teardown_intr(dev, child, vec,
1250		    sds->sds_cookie);
1251		if (error == 0)
1252			free(sds, M_DEVBUF);
1253		return (error);
1254	}
1255	return (bus_generic_teardown_intr(dev, child, vec, cookie));
1256}
1257
1258static struct resource *
1259schizo_alloc_resource(device_t bus, device_t child, int type, int *rid,
1260    u_long start, u_long end, u_long count, u_int flags)
1261{
1262	struct schizo_softc *sc;
1263	struct resource *rv;
1264	struct rman *rm;
1265	bus_space_tag_t bt;
1266	bus_space_handle_t bh;
1267	int needactivate = flags & RF_ACTIVE;
1268
1269	flags &= ~RF_ACTIVE;
1270
1271	sc = device_get_softc(bus);
1272	if (type == SYS_RES_IRQ) {
1273		/*
1274		 * XXX: Don't accept blank ranges for now, only single
1275		 * interrupts.  The other case should not happen with
1276		 * the MI PCI code...
1277		 * XXX: This may return a resource that is out of the
1278		 * range that was specified.  Is this correct...?
1279		 */
1280		if (start != end)
1281			panic("%s: XXX: interrupt range", __func__);
1282		start = end = INTMAP_VEC(sc->sc_ign, end);
1283		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
1284		    rid, start, end, count, flags));
1285	}
1286	switch (type) {
1287	case SYS_RES_MEMORY:
1288		rm = &sc->sc_pci_mem_rman;
1289		bt = sc->sc_pci_memt;
1290		bh = sc->sc_pci_bh[OFW_PCI_CS_MEM32];
1291		break;
1292	case SYS_RES_IOPORT:
1293		rm = &sc->sc_pci_io_rman;
1294		bt = sc->sc_pci_iot;
1295		bh = sc->sc_pci_bh[OFW_PCI_CS_IO];
1296		break;
1297	default:
1298		return (NULL);
1299		/* NOTREACHED */
1300	}
1301
1302	rv = rman_reserve_resource(rm, start, end, count, flags, child);
1303	if (rv == NULL)
1304		return (NULL);
1305	rman_set_rid(rv, *rid);
1306	bh += rman_get_start(rv);
1307	rman_set_bustag(rv, bt);
1308	rman_set_bushandle(rv, bh);
1309
1310	if (needactivate) {
1311		if (bus_activate_resource(child, type, *rid, rv)) {
1312			rman_release_resource(rv);
1313			return (NULL);
1314		}
1315	}
1316	return (rv);
1317}
1318
1319static int
1320schizo_activate_resource(device_t bus, device_t child, int type, int rid,
1321    struct resource *r)
1322{
1323	void *p;
1324	int error;
1325
1326	if (type == SYS_RES_IRQ)
1327		return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
1328		    type, rid, r));
1329	if (type == SYS_RES_MEMORY) {
1330		/*
1331		 * Need to memory-map the device space, as some drivers
1332		 * depend on the virtual address being set and usable.
1333		 */
1334		error = sparc64_bus_mem_map(rman_get_bustag(r),
1335		    rman_get_bushandle(r), rman_get_size(r), 0, 0, &p);
1336		if (error != 0)
1337			return (error);
1338		rman_set_virtual(r, p);
1339	}
1340	return (rman_activate_resource(r));
1341}
1342
1343static int
1344schizo_deactivate_resource(device_t bus, device_t child, int type, int rid,
1345    struct resource *r)
1346{
1347
1348	if (type == SYS_RES_IRQ)
1349		return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
1350		    type, rid, r));
1351	if (type == SYS_RES_MEMORY) {
1352		sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r));
1353		rman_set_virtual(r, NULL);
1354	}
1355	return (rman_deactivate_resource(r));
1356}
1357
1358static int
1359schizo_release_resource(device_t bus, device_t child, int type, int rid,
1360    struct resource *r)
1361{
1362	int error;
1363
1364	if (type == SYS_RES_IRQ)
1365		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1366		    type, rid, r));
1367	if (rman_get_flags(r) & RF_ACTIVE) {
1368		error = bus_deactivate_resource(child, type, rid, r);
1369		if (error)
1370			return (error);
1371	}
1372	return (rman_release_resource(r));
1373}
1374
1375static bus_dma_tag_t
1376schizo_get_dma_tag(device_t bus, device_t child)
1377{
1378	struct schizo_softc *sc;
1379
1380	sc = device_get_softc(bus);
1381	return (sc->sc_pci_dmat);
1382}
1383
1384static phandle_t
1385schizo_get_node(device_t bus, device_t dev)
1386{
1387	struct schizo_softc *sc;
1388
1389	sc = device_get_softc(bus);
1390	/* We only have one child, the PCI bus, which needs our own node. */
1391	return (sc->sc_node);
1392}
1393
1394static bus_space_tag_t
1395schizo_alloc_bus_tag(struct schizo_softc *sc, int type)
1396{
1397	bus_space_tag_t bt;
1398
1399	bt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
1400	    M_NOWAIT | M_ZERO);
1401	if (bt == NULL)
1402		panic("%s: out of memory", __func__);
1403
1404	bt->bst_cookie = sc;
1405	bt->bst_parent = rman_get_bustag(sc->sc_mem_res[STX_PCI]);
1406	bt->bst_type = type;
1407	return (bt);
1408}
1409
1410static u_int
1411schizo_get_timecount(struct timecounter *tc)
1412{
1413	struct schizo_softc *sc;
1414
1415	sc = tc->tc_priv;
1416	return (SCHIZO_CTRL_READ_8(sc, STX_CTRL_PERF_CNT) &
1417	    (STX_CTRL_PERF_CNT_MASK << STX_CTRL_PERF_CNT_CNT0_SHIFT));
1418}
1419