sbus.c revision 171730
190618Stmm/*-
290618Stmm * Copyright (c) 1998 The NetBSD Foundation, Inc.
390618Stmm * All rights reserved.
490618Stmm *
590618Stmm * This code is derived from software contributed to The NetBSD Foundation
690618Stmm * by Paul Kranenburg.
790618Stmm *
890618Stmm * Redistribution and use in source and binary forms, with or without
990618Stmm * modification, are permitted provided that the following conditions
1090618Stmm * are met:
1190618Stmm * 1. Redistributions of source code must retain the above copyright
1290618Stmm *    notice, this list of conditions and the following disclaimer.
1390618Stmm * 2. Redistributions in binary form must reproduce the above copyright
1490618Stmm *    notice, this list of conditions and the following disclaimer in the
1590618Stmm *    documentation and/or other materials provided with the distribution.
1690618Stmm * 3. All advertising materials mentioning features or use of this software
1790618Stmm *    must display the following acknowledgement:
1890618Stmm *        This product includes software developed by the NetBSD
1990618Stmm *        Foundation, Inc. and its contributors.
2090618Stmm * 4. Neither the name of The NetBSD Foundation nor the names of its
2190618Stmm *    contributors may be used to endorse or promote products derived
2290618Stmm *    from this software without specific prior written permission.
2390618Stmm *
2490618Stmm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2590618Stmm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2690618Stmm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2790618Stmm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2890618Stmm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2990618Stmm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3090618Stmm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3190618Stmm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3290618Stmm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3390618Stmm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3490618Stmm * POSSIBILITY OF SUCH DAMAGE.
3590618Stmm */
36139825Simp/*-
3790618Stmm * Copyright (c) 1992, 1993
3890618Stmm *	The Regents of the University of California.  All rights reserved.
3990618Stmm *
4090618Stmm * This software was developed by the Computer Systems Engineering group
4190618Stmm * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
4290618Stmm * contributed to Berkeley.
4390618Stmm *
4490618Stmm * All advertising materials mentioning features or use of this software
4590618Stmm * must display the following acknowledgement:
4690618Stmm *	This product includes software developed by the University of
4790618Stmm *	California, Lawrence Berkeley Laboratory.
4890618Stmm *
4990618Stmm * Redistribution and use in source and binary forms, with or without
5090618Stmm * modification, are permitted provided that the following conditions
5190618Stmm * are met:
5290618Stmm * 1. Redistributions of source code must retain the above copyright
5390618Stmm *    notice, this list of conditions and the following disclaimer.
5490618Stmm * 2. Redistributions in binary form must reproduce the above copyright
5590618Stmm *    notice, this list of conditions and the following disclaimer in the
5690618Stmm *    documentation and/or other materials provided with the distribution.
5790618Stmm * 4. Neither the name of the University nor the names of its contributors
5890618Stmm *    may be used to endorse or promote products derived from this software
5990618Stmm *    without specific prior written permission.
6090618Stmm *
6190618Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6290618Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6390618Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6490618Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6590618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6690618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6790618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6890618Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6990618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
7090618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7190618Stmm * SUCH DAMAGE.
7290618Stmm */
73139825Simp/*-
7490618Stmm * Copyright (c) 1999 Eduardo Horvath
7590618Stmm * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
76167308Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
7790618Stmm * All rights reserved.
7890618Stmm *
7990618Stmm * Redistribution and use in source and binary forms, with or without
8090618Stmm * modification, are permitted provided that the following conditions
8190618Stmm * are met:
8290618Stmm * 1. Redistributions of source code must retain the above copyright
8390618Stmm *    notice, this list of conditions and the following disclaimer.
8490618Stmm *
8590618Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
8690618Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8790618Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8890618Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
8990618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9090618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9190618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9290618Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9390618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9490618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9590618Stmm * SUCH DAMAGE.
9690618Stmm *
9790618Stmm *	from: @(#)sbus.c	8.1 (Berkeley) 6/11/93
9890618Stmm *	from: NetBSD: sbus.c,v 1.46 2001/10/07 20:30:41 eeh Exp
9990618Stmm */
10090618Stmm
101145185Smarius#include <sys/cdefs.h>
102145185Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sbus/sbus.c 171730 2007-08-05 11:56:44Z marius $");
103145185Smarius
10490618Stmm/*
105145185Smarius * SBus support.
10690618Stmm */
107131376Smarius
10890618Stmm#include <sys/param.h>
10990618Stmm#include <sys/systm.h>
11090618Stmm#include <sys/bus.h>
11190618Stmm#include <sys/kernel.h>
11290618Stmm#include <sys/malloc.h>
113130068Sphk#include <sys/module.h>
114107477Stmm#include <sys/pcpu.h>
115171730Smarius#include <sys/queue.h>
11690618Stmm#include <sys/reboot.h>
117171730Smarius#include <sys/rman.h>
11890618Stmm
119133589Smarius#include <dev/ofw/ofw_bus.h>
120152684Smarius#include <dev/ofw/ofw_bus_subr.h>
121119338Simp#include <dev/ofw/openfirm.h>
12290618Stmm
12390618Stmm#include <machine/bus.h>
124171730Smarius#include <machine/bus_common.h>
125116541Stmm#include <machine/bus_private.h>
12690618Stmm#include <machine/iommureg.h>
127171730Smarius#include <machine/iommuvar.h>
12890618Stmm#include <machine/resource.h>
12990618Stmm
13090618Stmm#include <sparc64/sbus/ofw_sbus.h>
13190618Stmm#include <sparc64/sbus/sbusreg.h>
13290618Stmm#include <sparc64/sbus/sbusvar.h>
13390618Stmm
13490618Stmmstruct sbus_devinfo {
13590618Stmm	int			sdi_burstsz;
136145186Smarius	int			sdi_clockfreq;
13790618Stmm	int			sdi_slot;
13890618Stmm
139152684Smarius	struct ofw_bus_devinfo	sdi_obdinfo;
14090618Stmm	struct resource_list	sdi_rl;
14190618Stmm};
14290618Stmm
14390618Stmm/* Range descriptor, allocated for each sc_range. */
14490618Stmmstruct sbus_rd {
14590618Stmm	bus_addr_t		rd_poffset;
14690618Stmm	bus_addr_t		rd_pend;
14790618Stmm	int			rd_slot;
14890618Stmm	bus_addr_t		rd_coffset;
14990618Stmm	bus_addr_t		rd_cend;
15090618Stmm	struct rman		rd_rman;
15190618Stmm	bus_space_handle_t	rd_bushandle;
15290618Stmm	struct resource		*rd_res;
15390618Stmm};
15490618Stmm
15590618Stmmstruct sbus_softc {
15690618Stmm	bus_dma_tag_t		sc_cdmatag;
15790618Stmm	bus_space_tag_t		sc_cbustag;
15890618Stmm	int			sc_clockfreq;	/* clock frequency (in Hz) */
15990618Stmm	int			sc_nrange;
16090618Stmm	struct sbus_rd		*sc_rd;
161145185Smarius	int			sc_burst;	/* burst transfer sizes supp. */
16290618Stmm
16390618Stmm	struct resource		*sc_sysio_res;
164145185Smarius	int			sc_ign;		/* IGN for this sysio */
165145185Smarius	struct iommu_state	sc_is;		/* IOMMU state (iommuvar.h) */
16690618Stmm
16790618Stmm	struct resource		*sc_ot_ires;
16890618Stmm	void			*sc_ot_ihand;
16990618Stmm	struct resource		*sc_pf_ires;
17090618Stmm	void			*sc_pf_ihand;
17190618Stmm};
17290618Stmm
17390618Stmmstruct sbus_clr {
17490618Stmm	struct sbus_softc	*scl_sc;
175145185Smarius	bus_addr_t		scl_clr;	/* clear register */
176170852Smarius	driver_filter_t		*scl_filter;	/* filter to call */
177170387Spiso	driver_intr_t		*scl_handler;	/* handler to call */
178145185Smarius	void			*scl_arg;	/* argument for the handler */
179145185Smarius	void			*scl_cookie;	/* parent bus int. cookie */
18090618Stmm};
18190618Stmm
18290618Stmm#define	SYSIO_READ8(sc, off) \
183170852Smarius	bus_read_8((sc)->sc_sysio_res, (off))
18490618Stmm#define	SYSIO_WRITE8(sc, off, v) \
185170852Smarius	bus_write_8((sc)->sc_sysio_res, (off), (v))
18690618Stmm
187145185Smariusstatic device_probe_t sbus_probe;
188145186Smariusstatic device_attach_t sbus_attach;
189145185Smariusstatic bus_print_child_t sbus_print_child;
190145185Smariusstatic bus_probe_nomatch_t sbus_probe_nomatch;
191145185Smariusstatic bus_read_ivar_t sbus_read_ivar;
192145185Smariusstatic bus_get_resource_list_t sbus_get_resource_list;
193145185Smariusstatic bus_setup_intr_t sbus_setup_intr;
194145185Smariusstatic bus_teardown_intr_t sbus_teardown_intr;
195145185Smariusstatic bus_alloc_resource_t sbus_alloc_resource;
196145185Smariusstatic bus_release_resource_t sbus_release_resource;
197145185Smariusstatic bus_activate_resource_t sbus_activate_resource;
198145185Smariusstatic bus_deactivate_resource_t sbus_deactivate_resource;
199167308Smariusstatic bus_get_dma_tag_t sbus_get_dma_tag;
200152684Smariusstatic ofw_bus_get_devinfo_t sbus_get_devinfo;
20190618Stmm
202146391Smariusstatic int sbus_inlist(const char *, const char **);
203152684Smariusstatic struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *,
204152684Smarius    phandle_t);
205152684Smariusstatic void sbus_destroy_dinfo(struct sbus_devinfo *);
206170387Spisostatic driver_filter_t sbus_filter_stub;
207170387Spisostatic driver_intr_t sbus_intr_stub;
20890618Stmmstatic bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *);
209170852Smariusstatic driver_intr_t sbus_overtemp;
210170852Smariusstatic driver_intr_t sbus_pwrfail;
211152684Smariusstatic int sbus_print_res(struct sbus_devinfo *);
21290618Stmm
21390618Stmmstatic device_method_t sbus_methods[] = {
21490618Stmm	/* Device interface */
21590618Stmm	DEVMETHOD(device_probe,		sbus_probe),
216145186Smarius	DEVMETHOD(device_attach,	sbus_attach),
217154870Smarius	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
218154870Smarius	DEVMETHOD(device_suspend,	bus_generic_suspend),
219154870Smarius	DEVMETHOD(device_resume,	bus_generic_resume),
22090618Stmm
22190618Stmm	/* Bus interface */
22290618Stmm	DEVMETHOD(bus_print_child,	sbus_print_child),
22390618Stmm	DEVMETHOD(bus_probe_nomatch,	sbus_probe_nomatch),
22490618Stmm	DEVMETHOD(bus_read_ivar,	sbus_read_ivar),
22590618Stmm	DEVMETHOD(bus_setup_intr, 	sbus_setup_intr),
22690618Stmm	DEVMETHOD(bus_teardown_intr,	sbus_teardown_intr),
22790618Stmm	DEVMETHOD(bus_alloc_resource,	sbus_alloc_resource),
22890618Stmm	DEVMETHOD(bus_activate_resource,	sbus_activate_resource),
22990618Stmm	DEVMETHOD(bus_deactivate_resource,	sbus_deactivate_resource),
23090618Stmm	DEVMETHOD(bus_release_resource,	sbus_release_resource),
23190618Stmm	DEVMETHOD(bus_get_resource_list, sbus_get_resource_list),
23290618Stmm	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
233167308Smarius	DEVMETHOD(bus_get_dma_tag,	sbus_get_dma_tag),
23490618Stmm
235133589Smarius	/* ofw_bus interface */
236152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	sbus_get_devinfo),
237152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
238152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
239152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
240152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
241152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
242133589Smarius
24390618Stmm	{ 0, 0 }
24490618Stmm};
24590618Stmm
24690618Stmmstatic driver_t sbus_driver = {
24790618Stmm	"sbus",
24890618Stmm	sbus_methods,
24990618Stmm	sizeof(struct sbus_softc),
25090618Stmm};
25190618Stmm
25290618Stmmstatic devclass_t sbus_devclass;
25390618Stmm
25490618StmmDRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0);
25590618Stmm
25690618Stmm#define	OFW_SBUS_TYPE	"sbus"
25790618Stmm#define	OFW_SBUS_NAME	"sbus"
25890618Stmm
259146391Smariusstatic const char *sbus_order_first[] = {
260146391Smarius	"auxio",
261146391Smarius	"dma",
262146391Smarius	NULL
263146391Smarius};
264146391Smarius
26590618Stmmstatic int
266146391Smariussbus_inlist(const char *name, const char **list)
267146391Smarius{
268146391Smarius	int i;
269146391Smarius
270146391Smarius	if (name == NULL)
271146391Smarius		return (0);
272146391Smarius	for (i = 0; list[i] != NULL; i++) {
273146391Smarius		if (strcmp(name, list[i]) == 0)
274146391Smarius			return (1);
275146391Smarius	}
276146391Smarius	return (0);
277146391Smarius}
278146391Smarius
279146391Smariusstatic int
28090618Stmmsbus_probe(device_t dev)
28190618Stmm{
282167308Smarius	const char *t;
283145186Smarius
284167308Smarius	t = ofw_bus_get_type(dev);
285145186Smarius	if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) &&
286167308Smarius	    strcmp(ofw_bus_get_name(dev), OFW_SBUS_NAME) != 0)
287145186Smarius		return (ENXIO);
288145186Smarius	device_set_desc(dev, "U2S UPA-SBus bridge");
289145186Smarius	return (0);
290145186Smarius}
291145186Smarius
292145186Smariusstatic int
293145186Smariussbus_attach(device_t dev)
294145186Smarius{
295145185Smarius	struct sbus_softc *sc;
29690618Stmm	struct sbus_devinfo *sdi;
29790618Stmm	struct sbus_ranges *range;
29890618Stmm	struct resource *res;
299167308Smarius	struct resource_list *rl;
30090618Stmm	device_t cdev;
30190618Stmm	bus_addr_t phys;
30290618Stmm	bus_size_t size;
303152684Smarius	char *name;
304145185Smarius	phandle_t child, node;
30590618Stmm	u_int64_t mr;
306167308Smarius	int clock, i, intr, rid;
30790618Stmm
308145185Smarius	sc = device_get_softc(dev);
309167308Smarius	node = ofw_bus_get_node(dev);
310145185Smarius
31190618Stmm	rid = 0;
312167308Smarius	sc->sc_sysio_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
313167308Smarius	    RF_ACTIVE);
314167308Smarius	if (sc->sc_sysio_res == NULL)
315145185Smarius		panic("%s: cannot allocate device memory", __func__);
31690618Stmm
31790618Stmm	if (OF_getprop(node, "interrupts", &intr, sizeof(intr)) == -1)
318145185Smarius		panic("%s: cannot get IGN", __func__);
319146391Smarius	sc->sc_ign = (intr & INTMAP_IGN_MASK) >> INTMAP_IGN_SHIFT;
32090618Stmm	sc->sc_cbustag = sbus_alloc_bustag(sc);
32190618Stmm
32290618Stmm	/*
32390618Stmm	 * Record clock frequency for synchronous SCSI.
32490618Stmm	 * IS THIS THE CORRECT DEFAULT??
32590618Stmm	 */
32690618Stmm	if (OF_getprop(node, "clock-frequency", &clock, sizeof(clock)) == -1)
32790618Stmm		clock = 25000000;
32890618Stmm	sc->sc_clockfreq = clock;
32990618Stmm	clock /= 1000;
33090618Stmm	device_printf(dev, "clock %d.%03d MHz\n", clock / 1000, clock % 1000);
33190618Stmm
33290618Stmm	/*
33390618Stmm	 * Collect address translations from the OBP.
33490618Stmm	 */
33590618Stmm	if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges",
33690618Stmm	    sizeof(*range), (void **)&range)) == -1) {
337145185Smarius		panic("%s: error getting ranges property", __func__);
33890618Stmm	}
33990618Stmm	sc->sc_rd = (struct sbus_rd *)malloc(sizeof(*sc->sc_rd) * sc->sc_nrange,
34090618Stmm	    M_DEVBUF, M_NOWAIT);
34190618Stmm	if (sc->sc_rd == NULL)
342145185Smarius		panic("%s: cannot allocate rmans", __func__);
34390618Stmm	/*
34490618Stmm	 * Preallocate all space that the SBus bridge decodes, so that nothing
34590618Stmm	 * else gets in the way; set up rmans etc.
34690618Stmm	 */
347167308Smarius	rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
34890618Stmm	for (i = 0; i < sc->sc_nrange; i++) {
34990618Stmm		phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32);
35090618Stmm		size = range[i].size;
35190618Stmm		sc->sc_rd[i].rd_slot = range[i].cspace;
35290618Stmm		sc->sc_rd[i].rd_coffset = range[i].coffset;
35390618Stmm		sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size;
354167308Smarius		rid = resource_list_add_next(rl, SYS_RES_MEMORY, phys,
355167308Smarius		    phys + size - 1, size);
356167308Smarius		if ((res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
357167308Smarius		    RF_ACTIVE)) == NULL)
358145185Smarius			panic("%s: cannot allocate decoded range", __func__);
35990618Stmm		sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res);
36090618Stmm		sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY;
36190618Stmm		sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory";
36290618Stmm		if (rman_init(&sc->sc_rd[i].rd_rman) != 0 ||
36390618Stmm		    rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0)
364145185Smarius			panic("%s: failed to set up memory rman", __func__);
36590618Stmm		sc->sc_rd[i].rd_poffset = phys;
36690618Stmm		sc->sc_rd[i].rd_pend = phys + size;
36790618Stmm		sc->sc_rd[i].rd_res = res;
36890618Stmm	}
36990618Stmm	free(range, M_OFWPROP);
37090618Stmm
37190618Stmm	/*
37290618Stmm	 * Get the SBus burst transfer size if burst transfers are supported.
37390618Stmm	 * XXX: is the default correct?
37490618Stmm	 */
37590618Stmm	if (OF_getprop(node, "burst-sizes", &sc->sc_burst,
37690618Stmm	    sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0)
37790618Stmm		sc->sc_burst = SBUS_BURST_DEF;
37890618Stmm
37990618Stmm	/* initalise the IOMMU */
38090618Stmm
38190618Stmm	/* punch in our copies */
382171730Smarius	sc->sc_is.is_pmaxaddr = IOMMU_MAXADDR(SBUS_IOMMU_BITS);
383170852Smarius	sc->sc_is.is_bustag = rman_get_bustag(sc->sc_sysio_res);
384170852Smarius	sc->sc_is.is_bushandle = rman_get_bushandle(sc->sc_sysio_res);
38590618Stmm	sc->sc_is.is_iommu = SBR_IOMMU;
38690618Stmm	sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG;
38790618Stmm	sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG;
38890618Stmm	sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG;
38990618Stmm	sc->sc_is.is_dva = SBR_IOMMU_SVADIAG;
39090618Stmm	sc->sc_is.is_dtcmp = 0;
39190618Stmm	sc->sc_is.is_sb[0] = SBR_STRBUF;
392123865Sobrien	sc->sc_is.is_sb[1] = 0;
39390618Stmm
39490618Stmm	/* give us a nice name.. */
39590618Stmm	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
396145185Smarius	if (name == NULL)
397145185Smarius		panic("%s: cannot malloc iommu name", __func__);
39890618Stmm	snprintf(name, 32, "%s dvma", device_get_name(dev));
39990618Stmm
400100188Stmm	/*
401100188Stmm	 * Note: the SBus IOMMU ignores the high bits of an address, so a NULL
402100188Stmm	 * DMA pointer will be translated by the first page of the IOTSB.
403100188Stmm	 * To detect bugs we'll allocate and ignore the first entry.
404100188Stmm	 */
405114484Stmm	iommu_init(name, &sc->sc_is, 3, -1, 1);
40690618Stmm
407116213Stmm	/* Create the DMA tag. */
408171730Smarius	if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
409171730Smarius	    sc->sc_is.is_pmaxaddr, ~0, NULL, NULL, sc->sc_is.is_pmaxaddr,
410171730Smarius	    0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_cdmatag) != 0)
411145185Smarius		panic("%s: bus_dma_tag_create failed", __func__);
412116213Stmm	/* Customize the tag. */
413116213Stmm	sc->sc_cdmatag->dt_cookie = &sc->sc_is;
414116541Stmm	sc->sc_cdmatag->dt_mt = &iommu_dma_methods;
415116213Stmm
416145185Smarius	/* Enable the over-temperature and power-fail interrupts. */
417167308Smarius	rid = 4;
41890618Stmm	mr = SYSIO_READ8(sc, SBR_THERM_INT_MAP);
419167308Smarius	sc->sc_ot_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
420167308Smarius	    RF_ACTIVE);
421166034Smarius	if (sc->sc_ot_ires == NULL ||
422167308Smarius	    rman_get_start(sc->sc_ot_ires) != INTVEC(mr) ||
423166901Spiso	    bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC,
424170852Smarius	    NULL, sbus_overtemp, sc, &sc->sc_ot_ihand) != 0)
425166034Smarius		panic("%s: failed to set up temperature interrupt", __func__);
426107477Stmm	SYSIO_WRITE8(sc, SBR_THERM_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
427167308Smarius	rid = 3;
42890618Stmm	mr = SYSIO_READ8(sc, SBR_POWER_INT_MAP);
429167308Smarius	sc->sc_pf_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
430167308Smarius	    RF_ACTIVE);
431166034Smarius	if (sc->sc_pf_ires == NULL ||
432167308Smarius	    rman_get_start(sc->sc_pf_ires) != INTVEC(mr) ||
433166901Spiso	    bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC,
434170852Smarius	    NULL, sbus_pwrfail, sc, &sc->sc_pf_ihand) != 0)
435166034Smarius		panic("%s: failed to set up power fail interrupt", __func__);
436107477Stmm	SYSIO_WRITE8(sc, SBR_POWER_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
43790618Stmm
43890618Stmm	/* Initialize the counter-timer. */
439170852Smarius	sparc64_counter_init(rman_get_bustag(sc->sc_sysio_res),
440170852Smarius	    rman_get_bushandle(sc->sc_sysio_res), SBR_TC0);
44190618Stmm
44290618Stmm	/*
44390618Stmm	 * Loop through ROM children, fixing any relative addresses
44490618Stmm	 * and then configuring each device.
44590618Stmm	 */
44690618Stmm	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
447152684Smarius		if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL)
44890618Stmm			continue;
449146391Smarius		/*
450146391Smarius		 * For devices where there are variants that are actually
451146391Smarius		 * split into two SBus devices (as opposed to the first
452146391Smarius		 * half of the device being a SBus device and the second
453146391Smarius		 * half hanging off of the first one) like 'auxio' and
454146391Smarius		 * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device
455146391Smarius		 * which is a prerequisite to the driver attaching to the
456146391Smarius		 * second one with a lower order. Saves us from dealing
457146391Smarius		 * with different probe orders in the respective device
458146391Smarius		 * drivers which generally is more hackish.
459146391Smarius		 */
460146391Smarius		cdev = device_add_child_ordered(dev, (OF_child(child) == 0 &&
461152684Smarius		    sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ?
462152684Smarius		    SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1);
463152684Smarius		if (cdev == NULL) {
464152684Smarius			device_printf(dev,
465152684Smarius			    "<%s>: device_add_child_ordered failed\n",
466152684Smarius			    sdi->sdi_obdinfo.obd_name);
467152684Smarius			sbus_destroy_dinfo(sdi);
468152684Smarius			continue;
469152684Smarius		}
47090618Stmm		device_set_ivars(cdev, sdi);
47190618Stmm	}
472145186Smarius	return (bus_generic_attach(dev));
47390618Stmm}
47490618Stmm
47590618Stmmstatic struct sbus_devinfo *
476152684Smariussbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node)
47790618Stmm{
47890618Stmm	struct sbus_devinfo *sdi;
47990618Stmm	struct sbus_regs *reg;
48090618Stmm	u_int32_t base, iv, *intr;
48190618Stmm	int i, nreg, nintr, slot, rslot;
48290618Stmm
483111119Simp	sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK);
484152684Smarius	if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) {
485152684Smarius		free(sdi, M_DEVBUF);
48690618Stmm		return (NULL);
487152684Smarius	}
48890618Stmm	resource_list_init(&sdi->sdi_rl);
48990618Stmm	slot = -1;
49090618Stmm	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
49190618Stmm	if (nreg == -1) {
492152684Smarius		if (sdi->sdi_obdinfo.obd_type == NULL ||
493152684Smarius		    strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) {
494152684Smarius			device_printf(dev, "<%s>: incomplete\n",
495152684Smarius			    sdi->sdi_obdinfo.obd_name);
496152684Smarius			goto fail;
49790618Stmm		}
49890618Stmm	} else {
49990618Stmm		for (i = 0; i < nreg; i++) {
50090618Stmm			base = reg[i].sbr_offset;
50190618Stmm			if (SBUS_ABS(base)) {
50290618Stmm				rslot = SBUS_ABS_TO_SLOT(base);
50390618Stmm				base = SBUS_ABS_TO_OFFSET(base);
50490618Stmm			} else
50590618Stmm				rslot = reg[i].sbr_slot;
506152684Smarius			if (slot != -1 && slot != rslot) {
507152684Smarius				device_printf(dev, "<%s>: multiple slots\n",
508152684Smarius				    sdi->sdi_obdinfo.obd_name);
509152684Smarius				free(reg, M_OFWPROP);
510152684Smarius				goto fail;
511152684Smarius			}
51290618Stmm			slot = rslot;
51390618Stmm
51490618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i,
51590618Stmm			    base, base + reg[i].sbr_size, reg[i].sbr_size);
51690618Stmm		}
51790618Stmm		free(reg, M_OFWPROP);
51890618Stmm	}
51990618Stmm	sdi->sdi_slot = slot;
52090618Stmm
52190618Stmm	/*
522145185Smarius	 * The `interrupts' property contains the SBus interrupt level.
52390618Stmm	 */
524145185Smarius	nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
525145185Smarius	    (void **)&intr);
52690618Stmm	if (nintr != -1) {
52790618Stmm		for (i = 0; i < nintr; i++) {
52890618Stmm			iv = intr[i];
52990618Stmm			/*
530145185Smarius			 * SBus card devices need the slot number encoded into
53190618Stmm			 * the vector as this is generally not done.
53290618Stmm			 */
533107477Stmm			if ((iv & INTMAP_OBIO_MASK) == 0)
53490618Stmm				iv |= slot << 3;
53590618Stmm			/* Set the ign as appropriate. */
536146391Smarius			iv |= sc->sc_ign << INTMAP_IGN_SHIFT;
53790618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i,
53890618Stmm			    iv, iv, 1);
53990618Stmm		}
54090618Stmm		free(intr, M_OFWPROP);
54190618Stmm	}
54290618Stmm	if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz,
54390618Stmm	    sizeof(sdi->sdi_burstsz)) == -1)
54490618Stmm		sdi->sdi_burstsz = sc->sc_burst;
54590618Stmm	else
54690618Stmm		sdi->sdi_burstsz &= sc->sc_burst;
547145186Smarius	if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq,
548145186Smarius	    sizeof(sdi->sdi_clockfreq)) == -1)
549145186Smarius		sdi->sdi_clockfreq = sc->sc_clockfreq;
55090618Stmm
55190618Stmm	return (sdi);
552152684Smarius
553152684Smariusfail:
554152684Smarius	sbus_destroy_dinfo(sdi);
555152684Smarius	return (NULL);
55690618Stmm}
55790618Stmm
55890618Stmmstatic void
55990618Stmmsbus_destroy_dinfo(struct sbus_devinfo *dinfo)
56090618Stmm{
56190618Stmm
56290618Stmm	resource_list_free(&dinfo->sdi_rl);
563152684Smarius	ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo);
56490618Stmm	free(dinfo, M_DEVBUF);
56590618Stmm}
56690618Stmm
56790618Stmmstatic int
56890618Stmmsbus_print_child(device_t dev, device_t child)
56990618Stmm{
57090618Stmm	int rv;
57190618Stmm
57290618Stmm	rv = bus_print_child_header(dev, child);
573152684Smarius	rv += sbus_print_res(device_get_ivars(child));
57490618Stmm	rv += bus_print_child_footer(dev, child);
57590618Stmm	return (rv);
57690618Stmm}
57790618Stmm
57890618Stmmstatic void
57990618Stmmsbus_probe_nomatch(device_t dev, device_t child)
58090618Stmm{
581152684Smarius	const char *type;
58290618Stmm
583152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
584152684Smarius	sbus_print_res(device_get_ivars(child));
585152684Smarius	type = ofw_bus_get_type(child);
586145186Smarius	printf(" type %s (no driver attached)\n",
587152684Smarius	    type != NULL ? type : "unknown");
58890618Stmm}
58990618Stmm
59090618Stmmstatic int
59190618Stmmsbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
59290618Stmm{
593146391Smarius	struct sbus_softc *sc;
59490618Stmm	struct sbus_devinfo *dinfo;
59590618Stmm
596146391Smarius	sc = device_get_softc(dev);
59790618Stmm	if ((dinfo = device_get_ivars(child)) == NULL)
59890618Stmm		return (ENOENT);
59990618Stmm	switch (which) {
60090618Stmm	case SBUS_IVAR_BURSTSZ:
60190618Stmm		*result = dinfo->sdi_burstsz;
60290618Stmm		break;
60390618Stmm	case SBUS_IVAR_CLOCKFREQ:
604145186Smarius		*result = dinfo->sdi_clockfreq;
60590618Stmm		break;
606146391Smarius	case SBUS_IVAR_IGN:
607146391Smarius		*result = sc->sc_ign;
608146391Smarius		break;
60990618Stmm	case SBUS_IVAR_SLOT:
61090618Stmm		*result = dinfo->sdi_slot;
61190618Stmm		break;
61290618Stmm	default:
61390618Stmm		return (ENOENT);
61490618Stmm	}
615146391Smarius	return (0);
61690618Stmm}
61790618Stmm
61890618Stmmstatic struct resource_list *
61990618Stmmsbus_get_resource_list(device_t dev, device_t child)
62090618Stmm{
62190618Stmm	struct sbus_devinfo *sdi;
62290618Stmm
62390618Stmm	sdi = device_get_ivars(child);
62490618Stmm	return (&sdi->sdi_rl);
62590618Stmm}
62690618Stmm
627166901Spisostatic int
628170387Spisosbus_filter_stub(void *arg)
629170387Spiso{
630170387Spiso	struct sbus_clr *scl;
631170387Spiso	int res;
632170387Spiso
633170387Spiso	scl = (struct sbus_clr *)arg;
634170387Spiso	if (scl->scl_filter != NULL) {
635170387Spiso		res = scl->scl_filter(scl->scl_arg);
636170387Spiso		SYSIO_WRITE8(scl->scl_sc, scl->scl_clr, 0);
637170387Spiso	} else
638170387Spiso		res = FILTER_SCHEDULE_THREAD;
639170387Spiso	return (res);
640170387Spiso}
641170387Spiso
642170387Spisostatic void
64390618Stmmsbus_intr_stub(void *arg)
64490618Stmm{
64590618Stmm	struct sbus_clr *scl;
64690618Stmm
64790618Stmm	scl = (struct sbus_clr *)arg;
64890618Stmm	scl->scl_handler(scl->scl_arg);
649170387Spiso	if (scl->scl_filter == NULL)
650170387Spiso		SYSIO_WRITE8(scl->scl_sc, scl->scl_clr, 0);
65190618Stmm}
65290618Stmm
65390618Stmmstatic int
654145185Smariussbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
655166901Spiso    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
65690618Stmm{
65790618Stmm	struct sbus_softc *sc;
65890618Stmm	struct sbus_clr *scl;
65990618Stmm	bus_addr_t intrmapptr, intrclrptr, intrptr;
66090618Stmm	u_int64_t intrmap;
66190618Stmm	u_int32_t inr, slot;
66290618Stmm	int error, i;
663159413Smarius	long vec;
66490618Stmm
665166901Spiso	if (filt != NULL && intr != NULL)
666166901Spiso		return (EINVAL);
667145185Smarius	sc = device_get_softc(dev);
66890618Stmm	scl = (struct sbus_clr *)malloc(sizeof(*scl), M_DEVBUF, M_NOWAIT);
66990618Stmm	if (scl == NULL)
670159413Smarius		return (ENOMEM);
67190618Stmm	intrptr = intrmapptr = intrclrptr = 0;
67290618Stmm	intrmap = 0;
673159413Smarius	vec = rman_get_start(ires);
674107474Stmm	inr = INTVEC(vec);
675107477Stmm	if ((inr & INTMAP_OBIO_MASK) == 0) {
676107474Stmm		/*
677145185Smarius		 * We're in an SBus slot, register the map and clear
678107474Stmm		 * intr registers.
679107474Stmm		 */
680107474Stmm		slot = INTSLOT(vec);
681107474Stmm		intrmapptr = SBR_SLOT0_INT_MAP + slot * 8;
682107474Stmm		intrclrptr = SBR_SLOT0_INT_CLR +
683107474Stmm		    (slot * 8 * 8) + (INTPRI(vec) * 8);
684107474Stmm		/* Enable the interrupt, insert IGN. */
685146391Smarius		intrmap = inr | (sc->sc_ign << INTMAP_IGN_SHIFT);
686107474Stmm	} else {
687107474Stmm		intrptr = SBR_SCSI_INT_MAP;
688107474Stmm		/* Insert IGN */
689146391Smarius		inr |= sc->sc_ign << INTMAP_IGN_SHIFT;
690107474Stmm		for (i = 0; intrptr <= SBR_RESERVED_INT_MAP &&
691159413Smarius		    INTVEC(intrmap = SYSIO_READ8(sc, intrptr)) != inr;
692159413Smarius		    intrptr += 8, i++)
693107474Stmm			;
694159413Smarius		if (INTVEC(intrmap) == inr) {
695107474Stmm			/* Register the map and clear intr registers */
696107474Stmm			intrmapptr = intrptr;
697107474Stmm			intrclrptr = SBR_SCSI_INT_CLR + i * 8;
698107474Stmm			/* Enable the interrupt */
699107474Stmm		} else
700145185Smarius			panic("%s: IRQ not found!", __func__);
701107474Stmm	}
70290618Stmm
70390618Stmm	scl->scl_sc = sc;
70490618Stmm	scl->scl_arg = arg;
705170387Spiso	scl->scl_filter = filt;
706170387Spiso	scl->scl_handler = intr;
70790618Stmm	scl->scl_clr = intrclrptr;
70890618Stmm	/* Disable the interrupt while we fiddle with it */
709159413Smarius	SYSIO_WRITE8(sc, intrmapptr, intrmap & ~INTMAP_V);
710170387Spiso	error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
711170387Spiso	    sbus_filter_stub, sbus_intr_stub, scl, cookiep);
71290618Stmm	if (error != 0) {
71390618Stmm		free(scl, M_DEVBUF);
71490618Stmm		return (error);
71590618Stmm	}
71690618Stmm	scl->scl_cookie = *cookiep;
71790618Stmm	*cookiep = scl;
71890618Stmm
71990618Stmm	/*
72090618Stmm	 * Clear the interrupt, it might have been triggered before it was
72190618Stmm	 * set up.
72290618Stmm	 */
723107474Stmm	SYSIO_WRITE8(sc, intrclrptr, 0);
72490618Stmm	/*
725107477Stmm	 * Enable the interrupt and program the target module now we have the
726107477Stmm	 * handler installed.
72790618Stmm	 */
728107477Stmm	SYSIO_WRITE8(sc, intrmapptr, INTMAP_ENABLE(intrmap, PCPU_GET(mid)));
72990618Stmm	return (error);
73090618Stmm}
73190618Stmm
73290618Stmmstatic int
733152684Smariussbus_teardown_intr(device_t dev, device_t child, struct resource *vec,
734152684Smarius    void *cookie)
73590618Stmm{
73690618Stmm	struct sbus_clr *scl;
73790618Stmm	int error;
73890618Stmm
73990618Stmm	scl = (struct sbus_clr *)cookie;
74090618Stmm	error = BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec,
74190618Stmm	    scl->scl_cookie);
74290618Stmm	/*
743145185Smarius	 * Don't disable the interrupt for now, so that stray interrupts get
74490618Stmm	 * detected...
74590618Stmm	 */
74690618Stmm	if (error != 0)
74790618Stmm		free(scl, M_DEVBUF);
74890618Stmm	return (error);
74990618Stmm}
75090618Stmm
75190618Stmmstatic struct resource *
75290618Stmmsbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
75390618Stmm    u_long start, u_long end, u_long count, u_int flags)
75490618Stmm{
75590618Stmm	struct sbus_softc *sc;
75690618Stmm	struct rman *rm;
75790618Stmm	struct resource *rv;
75890618Stmm	struct resource_list *rl;
75990618Stmm	struct resource_list_entry *rle;
760145186Smarius	device_t schild;
76190618Stmm	bus_space_handle_t bh;
76290618Stmm	bus_addr_t toffs;
76390618Stmm	bus_size_t tend;
764145186Smarius	int i, slot;
765145186Smarius	int isdefault, needactivate, passthrough;
76690618Stmm
767145185Smarius	isdefault = (start == 0UL && end == ~0UL);
768145185Smarius	needactivate = flags & RF_ACTIVE;
769145186Smarius	passthrough = (device_get_parent(child) != bus);
770145186Smarius	rle = NULL;
771145186Smarius	sc = device_get_softc(bus);
772145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
77390618Stmm	switch (type) {
77490618Stmm	case SYS_RES_IRQ:
775145186Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
776145186Smarius		    end, count, flags));
77790618Stmm	case SYS_RES_MEMORY:
778145186Smarius		if (!passthrough) {
779145186Smarius			rle = resource_list_find(rl, type, *rid);
780145186Smarius			if (rle == NULL)
781145186Smarius				return (NULL);
782145186Smarius			if (rle->res != NULL)
783145186Smarius				panic("%s: resource entry is busy", __func__);
784145186Smarius			if (isdefault) {
785145186Smarius				start = rle->start;
786145186Smarius				count = ulmax(count, rle->count);
787145186Smarius				end = ulmax(rle->end, start + count - 1);
788145186Smarius			}
789145186Smarius		}
79090618Stmm		rm = NULL;
79190618Stmm		bh = toffs = tend = 0;
792145186Smarius		schild = child;
793145186Smarius		while (device_get_parent(schild) != bus)
794145186Smarius			schild = device_get_parent(child);
795145186Smarius		slot = sbus_get_slot(schild);
79690618Stmm		for (i = 0; i < sc->sc_nrange; i++) {
797145186Smarius			if (sc->sc_rd[i].rd_slot != slot ||
79890618Stmm			    start < sc->sc_rd[i].rd_coffset ||
79990618Stmm			    start > sc->sc_rd[i].rd_cend)
80090618Stmm				continue;
80190618Stmm			/* Disallow cross-range allocations. */
80290618Stmm			if (end > sc->sc_rd[i].rd_cend)
80390618Stmm				return (NULL);
80490618Stmm			/* We've found the connection to the parent bus */
80590618Stmm			toffs = start - sc->sc_rd[i].rd_coffset;
80690618Stmm			tend = end - sc->sc_rd[i].rd_coffset;
80790618Stmm			rm = &sc->sc_rd[i].rd_rman;
80890618Stmm			bh = sc->sc_rd[i].rd_bushandle;
809159413Smarius			break;
81090618Stmm		}
811159413Smarius		if (rm == NULL)
81290618Stmm			return (NULL);
81390618Stmm		flags &= ~RF_ACTIVE;
81490618Stmm		rv = rman_reserve_resource(rm, toffs, tend, count, flags,
81590618Stmm		    child);
81690618Stmm		if (rv == NULL)
81790618Stmm			return (NULL);
818157896Simp		rman_set_rid(rv, *rid);
81990618Stmm		rman_set_bustag(rv, sc->sc_cbustag);
82090618Stmm		rman_set_bushandle(rv, bh + rman_get_start(rv));
82190618Stmm		if (needactivate) {
82290618Stmm			if (bus_activate_resource(child, type, *rid, rv)) {
82390618Stmm				rman_release_resource(rv);
82490618Stmm				return (NULL);
82590618Stmm			}
82690618Stmm		}
827145186Smarius		if (!passthrough)
828145186Smarius			rle->res = rv;
829145186Smarius		return (rv);
83090618Stmm	default:
83190618Stmm		return (NULL);
83290618Stmm	}
83390618Stmm}
83490618Stmm
83590618Stmmstatic int
83690618Stmmsbus_activate_resource(device_t bus, device_t child, int type, int rid,
83790618Stmm    struct resource *r)
83890618Stmm{
839159413Smarius	void *p;
840159413Smarius	int error;
84190618Stmm
842108798Stmm	if (type == SYS_RES_IRQ) {
843108798Stmm		return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
844108798Stmm		    child, type, rid, r));
845108798Stmm	}
846159413Smarius	if (type == SYS_RES_MEMORY) {
847159413Smarius		/*
848159413Smarius		 * Need to memory-map the device space, as some drivers depend
849159413Smarius		 * on the virtual address being set and useable.
850159413Smarius		 */
851159413Smarius		error = sparc64_bus_mem_map(rman_get_bustag(r),
852159413Smarius		    rman_get_bushandle(r), rman_get_size(r), 0, 0, &p);
853159413Smarius		if (error != 0)
854159413Smarius			return (error);
855159413Smarius		rman_set_virtual(r, p);
856159413Smarius	}
85790618Stmm	return (rman_activate_resource(r));
85890618Stmm}
85990618Stmm
86090618Stmmstatic int
86190618Stmmsbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
86290618Stmm    struct resource *r)
86390618Stmm{
86490618Stmm
865108798Stmm	if (type == SYS_RES_IRQ) {
866108798Stmm		return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus),
867108798Stmm		    child, type, rid, r));
868108798Stmm	}
869159413Smarius	if (type == SYS_RES_MEMORY) {
870159413Smarius		sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r));
871159413Smarius		rman_set_virtual(r, NULL);
872159413Smarius	}
87390618Stmm	return (rman_deactivate_resource(r));
87490618Stmm}
87590618Stmm
87690618Stmmstatic int
87790618Stmmsbus_release_resource(device_t bus, device_t child, int type, int rid,
87890618Stmm    struct resource *r)
87990618Stmm{
880145186Smarius	struct resource_list *rl;
881108798Stmm	struct resource_list_entry *rle;
882145186Smarius	int error, passthrough;
88390618Stmm
884145186Smarius	passthrough = (device_get_parent(child) != bus);
885145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
88690618Stmm	if (type == SYS_RES_IRQ)
887145186Smarius		return (resource_list_release(rl, bus, child, type, rid, r));
888145186Smarius	if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
889145186Smarius		error = bus_deactivate_resource(child, type, rid, r);
890108798Stmm		if (error != 0)
891108798Stmm			return (error);
89290618Stmm	}
893145186Smarius	error = rman_release_resource(r);
894145186Smarius	if (error != 0 || passthrough)
895108798Stmm		return (error);
896145186Smarius	rle = resource_list_find(rl, type, rid);
897108798Stmm	if (rle == NULL)
898145185Smarius		panic("%s: cannot find resource", __func__);
899108798Stmm	if (rle->res == NULL)
900145185Smarius		panic("%s: resource entry is not busy", __func__);
901108798Stmm	rle->res = NULL;
902108798Stmm	return (0);
90390618Stmm}
90490618Stmm
905167308Smariusstatic bus_dma_tag_t
906167308Smariussbus_get_dma_tag(device_t bus, device_t child)
907167308Smarius{
908167308Smarius	struct sbus_softc *sc;
909167308Smarius
910167308Smarius	sc = device_get_softc(bus);
911167308Smarius	return (sc->sc_cdmatag);
912167308Smarius}
913167308Smarius
914152684Smariusstatic const struct ofw_bus_devinfo *
915152684Smariussbus_get_devinfo(device_t bus, device_t child)
916152684Smarius{
917152684Smarius	struct sbus_devinfo *sdi;
918152684Smarius
919152684Smarius	sdi = device_get_ivars(child);
920152684Smarius	return (&sdi->sdi_obdinfo);
921152684Smarius}
922152684Smarius
92390618Stmm/*
92490618Stmm * Handle an overtemp situation.
92590618Stmm *
92690618Stmm * SPARCs have temperature sensors which generate interrupts
92790618Stmm * if the machine's temperature exceeds a certain threshold.
92890618Stmm * This handles the interrupt and powers off the machine.
92990618Stmm * The same needs to be done to PCI controller drivers.
93090618Stmm */
931170852Smariusstatic void
93290618Stmmsbus_overtemp(void *arg)
93390618Stmm{
93490618Stmm
93590618Stmm	printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n");
93690618Stmm	shutdown_nice(RB_POWEROFF);
93790618Stmm}
93890618Stmm
93990618Stmm/* Try to shut down in time in case of power failure. */
940170852Smariusstatic void
94190618Stmmsbus_pwrfail(void *arg)
94290618Stmm{
94390618Stmm
94490618Stmm	printf("Power failure detected\nShutting down NOW.\n");
94590618Stmm	shutdown_nice(0);
94690618Stmm}
94790618Stmm
94890618Stmmstatic bus_space_tag_t
94990618Stmmsbus_alloc_bustag(struct sbus_softc *sc)
95090618Stmm{
95190618Stmm	bus_space_tag_t sbt;
95290618Stmm
95390618Stmm	sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
95490618Stmm	    M_NOWAIT | M_ZERO);
95590618Stmm	if (sbt == NULL)
956145185Smarius		panic("%s: out of memory", __func__);
95790618Stmm
958108815Stmm	sbt->bst_cookie = sc;
959170852Smarius	sbt->bst_parent = rman_get_bustag(sc->sc_sysio_res);
960108815Stmm	sbt->bst_type = SBUS_BUS_SPACE;
96190618Stmm	return (sbt);
96290618Stmm}
963133589Smarius
964152684Smariusstatic int
965152684Smariussbus_print_res(struct sbus_devinfo *sdi)
966133589Smarius{
967152684Smarius	int rv;
968133589Smarius
969152684Smarius	rv = 0;
970152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY,
971152684Smarius	    "%#lx");
972152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ,
973152684Smarius	    "%ld");
974152684Smarius	return (rv);
975133589Smarius}
976