sbus.c revision 167308
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 167308 2007-03-07 21:13:51Z 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>
11590618Stmm#include <sys/reboot.h>
11690618Stmm
117133589Smarius#include <dev/ofw/ofw_bus.h>
118152684Smarius#include <dev/ofw/ofw_bus_subr.h>
119119338Simp#include <dev/ofw/openfirm.h>
12090618Stmm
12190618Stmm#include <machine/bus.h>
122116541Stmm#include <machine/bus_private.h>
12390618Stmm#include <machine/iommureg.h>
12490618Stmm#include <machine/bus_common.h>
12590618Stmm#include <machine/resource.h>
12690618Stmm
12790618Stmm#include <sys/rman.h>
12890618Stmm
12990618Stmm#include <machine/iommuvar.h>
13090618Stmm
13190618Stmm#include <sparc64/sbus/ofw_sbus.h>
13290618Stmm#include <sparc64/sbus/sbusreg.h>
13390618Stmm#include <sparc64/sbus/sbusvar.h>
13490618Stmm
13590618Stmmstruct sbus_devinfo {
13690618Stmm	int			sdi_burstsz;
137145186Smarius	int			sdi_clockfreq;
13890618Stmm	int			sdi_slot;
13990618Stmm
140152684Smarius	struct ofw_bus_devinfo	sdi_obdinfo;
14190618Stmm	struct resource_list	sdi_rl;
14290618Stmm};
14390618Stmm
14490618Stmm/* Range descriptor, allocated for each sc_range. */
14590618Stmmstruct sbus_rd {
14690618Stmm	bus_addr_t		rd_poffset;
14790618Stmm	bus_addr_t		rd_pend;
14890618Stmm	int			rd_slot;
14990618Stmm	bus_addr_t		rd_coffset;
15090618Stmm	bus_addr_t		rd_cend;
15190618Stmm	struct rman		rd_rman;
15290618Stmm	bus_space_handle_t	rd_bushandle;
15390618Stmm	struct resource		*rd_res;
15490618Stmm};
15590618Stmm
15690618Stmmstruct sbus_softc {
15790618Stmm	bus_space_tag_t		sc_bustag;
15890618Stmm	bus_space_handle_t	sc_bushandle;
15990618Stmm	bus_dma_tag_t		sc_cdmatag;
16090618Stmm	bus_space_tag_t		sc_cbustag;
16190618Stmm	int			sc_clockfreq;	/* clock frequency (in Hz) */
16290618Stmm	int			sc_nrange;
16390618Stmm	struct sbus_rd		*sc_rd;
164145185Smarius	int			sc_burst;	/* burst transfer sizes supp. */
16590618Stmm
16690618Stmm	struct resource		*sc_sysio_res;
167145185Smarius	int			sc_ign;		/* IGN for this sysio */
168145185Smarius	struct iommu_state	sc_is;		/* IOMMU state (iommuvar.h) */
16990618Stmm
17090618Stmm	struct resource		*sc_ot_ires;
17190618Stmm	void			*sc_ot_ihand;
17290618Stmm	struct resource		*sc_pf_ires;
17390618Stmm	void			*sc_pf_ihand;
17490618Stmm};
17590618Stmm
17690618Stmmstruct sbus_clr {
17790618Stmm	struct sbus_softc	*scl_sc;
178145185Smarius	bus_addr_t		scl_clr;	/* clear register */
179166901Spiso	driver_filter_t		*scl_handler;	/* handler to call */
180145185Smarius	void			*scl_arg;	/* argument for the handler */
181145185Smarius	void			*scl_cookie;	/* parent bus int. cookie */
18290618Stmm};
18390618Stmm
18490618Stmm#define	SYSIO_READ8(sc, off) \
18590618Stmm	bus_space_read_8((sc)->sc_bustag, (sc)->sc_bushandle, (off))
18690618Stmm#define	SYSIO_WRITE8(sc, off, v) \
18790618Stmm	bus_space_write_8((sc)->sc_bustag, (sc)->sc_bushandle, (off), (v))
18890618Stmm
189145185Smariusstatic device_probe_t sbus_probe;
190145186Smariusstatic device_attach_t sbus_attach;
191145185Smariusstatic bus_print_child_t sbus_print_child;
192145185Smariusstatic bus_probe_nomatch_t sbus_probe_nomatch;
193145185Smariusstatic bus_read_ivar_t sbus_read_ivar;
194145185Smariusstatic bus_get_resource_list_t sbus_get_resource_list;
195145185Smariusstatic bus_setup_intr_t sbus_setup_intr;
196145185Smariusstatic bus_teardown_intr_t sbus_teardown_intr;
197145185Smariusstatic bus_alloc_resource_t sbus_alloc_resource;
198145185Smariusstatic bus_release_resource_t sbus_release_resource;
199145185Smariusstatic bus_activate_resource_t sbus_activate_resource;
200145185Smariusstatic bus_deactivate_resource_t sbus_deactivate_resource;
201167308Smariusstatic bus_get_dma_tag_t sbus_get_dma_tag;
202152684Smariusstatic ofw_bus_get_devinfo_t sbus_get_devinfo;
20390618Stmm
204146391Smariusstatic int sbus_inlist(const char *, const char **);
205152684Smariusstatic struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *,
206152684Smarius    phandle_t);
207152684Smariusstatic void sbus_destroy_dinfo(struct sbus_devinfo *);
208167308Smariusstatic driver_filter_t sbus_intr_stub;
20990618Stmmstatic bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *);
210167308Smariusstatic driver_filter_t sbus_overtemp;
211167308Smariusstatic driver_filter_t sbus_pwrfail;
212152684Smariusstatic int sbus_print_res(struct sbus_devinfo *);
21390618Stmm
21490618Stmmstatic device_method_t sbus_methods[] = {
21590618Stmm	/* Device interface */
21690618Stmm	DEVMETHOD(device_probe,		sbus_probe),
217145186Smarius	DEVMETHOD(device_attach,	sbus_attach),
218154870Smarius	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
219154870Smarius	DEVMETHOD(device_suspend,	bus_generic_suspend),
220154870Smarius	DEVMETHOD(device_resume,	bus_generic_resume),
22190618Stmm
22290618Stmm	/* Bus interface */
22390618Stmm	DEVMETHOD(bus_print_child,	sbus_print_child),
22490618Stmm	DEVMETHOD(bus_probe_nomatch,	sbus_probe_nomatch),
22590618Stmm	DEVMETHOD(bus_read_ivar,	sbus_read_ivar),
22690618Stmm	DEVMETHOD(bus_setup_intr, 	sbus_setup_intr),
22790618Stmm	DEVMETHOD(bus_teardown_intr,	sbus_teardown_intr),
22890618Stmm	DEVMETHOD(bus_alloc_resource,	sbus_alloc_resource),
22990618Stmm	DEVMETHOD(bus_activate_resource,	sbus_activate_resource),
23090618Stmm	DEVMETHOD(bus_deactivate_resource,	sbus_deactivate_resource),
23190618Stmm	DEVMETHOD(bus_release_resource,	sbus_release_resource),
23290618Stmm	DEVMETHOD(bus_get_resource_list, sbus_get_resource_list),
23390618Stmm	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
234167308Smarius	DEVMETHOD(bus_get_dma_tag,	sbus_get_dma_tag),
23590618Stmm
236133589Smarius	/* ofw_bus interface */
237152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	sbus_get_devinfo),
238152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
239152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
240152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
241152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
242152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
243133589Smarius
24490618Stmm	{ 0, 0 }
24590618Stmm};
24690618Stmm
24790618Stmmstatic driver_t sbus_driver = {
24890618Stmm	"sbus",
24990618Stmm	sbus_methods,
25090618Stmm	sizeof(struct sbus_softc),
25190618Stmm};
25290618Stmm
25390618Stmmstatic devclass_t sbus_devclass;
25490618Stmm
25590618StmmDRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0);
25690618Stmm
25790618Stmm#define	OFW_SBUS_TYPE	"sbus"
25890618Stmm#define	OFW_SBUS_NAME	"sbus"
25990618Stmm
260146391Smariusstatic const char *sbus_order_first[] = {
261146391Smarius	"auxio",
262146391Smarius	"dma",
263146391Smarius	NULL
264146391Smarius};
265146391Smarius
26690618Stmmstatic int
267146391Smariussbus_inlist(const char *name, const char **list)
268146391Smarius{
269146391Smarius	int i;
270146391Smarius
271146391Smarius	if (name == NULL)
272146391Smarius		return (0);
273146391Smarius	for (i = 0; list[i] != NULL; i++) {
274146391Smarius		if (strcmp(name, list[i]) == 0)
275146391Smarius			return (1);
276146391Smarius	}
277146391Smarius	return (0);
278146391Smarius}
279146391Smarius
280146391Smariusstatic int
28190618Stmmsbus_probe(device_t dev)
28290618Stmm{
283167308Smarius	const char *t;
284145186Smarius
285167308Smarius	t = ofw_bus_get_type(dev);
286145186Smarius	if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) &&
287167308Smarius	    strcmp(ofw_bus_get_name(dev), OFW_SBUS_NAME) != 0)
288145186Smarius		return (ENXIO);
289145186Smarius	device_set_desc(dev, "U2S UPA-SBus bridge");
290145186Smarius	return (0);
291145186Smarius}
292145186Smarius
293145186Smariusstatic int
294145186Smariussbus_attach(device_t dev)
295145186Smarius{
296145185Smarius	struct sbus_softc *sc;
29790618Stmm	struct sbus_devinfo *sdi;
29890618Stmm	struct sbus_ranges *range;
29990618Stmm	struct resource *res;
300167308Smarius	struct resource_list *rl;
30190618Stmm	device_t cdev;
30290618Stmm	bus_addr_t phys;
30390618Stmm	bus_size_t size;
304152684Smarius	char *name;
305145185Smarius	phandle_t child, node;
30690618Stmm	u_int64_t mr;
307167308Smarius	int clock, i, intr, rid;
30890618Stmm
309145185Smarius	sc = device_get_softc(dev);
310167308Smarius	node = ofw_bus_get_node(dev);
311145185Smarius
31290618Stmm	rid = 0;
313167308Smarius	sc->sc_sysio_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
314167308Smarius	    RF_ACTIVE);
315167308Smarius	if (sc->sc_sysio_res == NULL)
316145185Smarius		panic("%s: cannot allocate device memory", __func__);
31790618Stmm	sc->sc_bustag = rman_get_bustag(sc->sc_sysio_res);
31890618Stmm	sc->sc_bushandle = rman_get_bushandle(sc->sc_sysio_res);
31990618Stmm
32090618Stmm	if (OF_getprop(node, "interrupts", &intr, sizeof(intr)) == -1)
321145185Smarius		panic("%s: cannot get IGN", __func__);
322146391Smarius	sc->sc_ign = (intr & INTMAP_IGN_MASK) >> INTMAP_IGN_SHIFT;
32390618Stmm	sc->sc_cbustag = sbus_alloc_bustag(sc);
32490618Stmm
32590618Stmm	/*
32690618Stmm	 * Record clock frequency for synchronous SCSI.
32790618Stmm	 * IS THIS THE CORRECT DEFAULT??
32890618Stmm	 */
32990618Stmm	if (OF_getprop(node, "clock-frequency", &clock, sizeof(clock)) == -1)
33090618Stmm		clock = 25000000;
33190618Stmm	sc->sc_clockfreq = clock;
33290618Stmm	clock /= 1000;
33390618Stmm	device_printf(dev, "clock %d.%03d MHz\n", clock / 1000, clock % 1000);
33490618Stmm
33590618Stmm	/*
33690618Stmm	 * Collect address translations from the OBP.
33790618Stmm	 */
33890618Stmm	if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges",
33990618Stmm	    sizeof(*range), (void **)&range)) == -1) {
340145185Smarius		panic("%s: error getting ranges property", __func__);
34190618Stmm	}
34290618Stmm	sc->sc_rd = (struct sbus_rd *)malloc(sizeof(*sc->sc_rd) * sc->sc_nrange,
34390618Stmm	    M_DEVBUF, M_NOWAIT);
34490618Stmm	if (sc->sc_rd == NULL)
345145185Smarius		panic("%s: cannot allocate rmans", __func__);
34690618Stmm	/*
34790618Stmm	 * Preallocate all space that the SBus bridge decodes, so that nothing
34890618Stmm	 * else gets in the way; set up rmans etc.
34990618Stmm	 */
350167308Smarius	rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
35190618Stmm	for (i = 0; i < sc->sc_nrange; i++) {
35290618Stmm		phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32);
35390618Stmm		size = range[i].size;
35490618Stmm		sc->sc_rd[i].rd_slot = range[i].cspace;
35590618Stmm		sc->sc_rd[i].rd_coffset = range[i].coffset;
35690618Stmm		sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size;
357167308Smarius		rid = resource_list_add_next(rl, SYS_RES_MEMORY, phys,
358167308Smarius		    phys + size - 1, size);
359167308Smarius		if ((res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
360167308Smarius		    RF_ACTIVE)) == NULL)
361145185Smarius			panic("%s: cannot allocate decoded range", __func__);
36290618Stmm		sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res);
36390618Stmm		sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY;
36490618Stmm		sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory";
36590618Stmm		if (rman_init(&sc->sc_rd[i].rd_rman) != 0 ||
36690618Stmm		    rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0)
367145185Smarius			panic("%s: failed to set up memory rman", __func__);
36890618Stmm		sc->sc_rd[i].rd_poffset = phys;
36990618Stmm		sc->sc_rd[i].rd_pend = phys + size;
37090618Stmm		sc->sc_rd[i].rd_res = res;
37190618Stmm	}
37290618Stmm	free(range, M_OFWPROP);
37390618Stmm
37490618Stmm	/*
37590618Stmm	 * Get the SBus burst transfer size if burst transfers are supported.
37690618Stmm	 * XXX: is the default correct?
37790618Stmm	 */
37890618Stmm	if (OF_getprop(node, "burst-sizes", &sc->sc_burst,
37990618Stmm	    sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0)
38090618Stmm		sc->sc_burst = SBUS_BURST_DEF;
38190618Stmm
38290618Stmm	/* initalise the IOMMU */
38390618Stmm
38490618Stmm	/* punch in our copies */
38590618Stmm	sc->sc_is.is_bustag = sc->sc_bustag;
38690618Stmm	sc->sc_is.is_bushandle = sc->sc_bushandle;
38790618Stmm	sc->sc_is.is_iommu = SBR_IOMMU;
38890618Stmm	sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG;
38990618Stmm	sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG;
39090618Stmm	sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG;
39190618Stmm	sc->sc_is.is_dva = SBR_IOMMU_SVADIAG;
39290618Stmm	sc->sc_is.is_dtcmp = 0;
39390618Stmm	sc->sc_is.is_sb[0] = SBR_STRBUF;
394123865Sobrien	sc->sc_is.is_sb[1] = 0;
39590618Stmm
39690618Stmm	/* give us a nice name.. */
39790618Stmm	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
398145185Smarius	if (name == NULL)
399145185Smarius		panic("%s: cannot malloc iommu name", __func__);
40090618Stmm	snprintf(name, 32, "%s dvma", device_get_name(dev));
40190618Stmm
402100188Stmm	/*
403100188Stmm	 * Note: the SBus IOMMU ignores the high bits of an address, so a NULL
404100188Stmm	 * DMA pointer will be translated by the first page of the IOTSB.
405100188Stmm	 * To detect bugs we'll allocate and ignore the first entry.
406100188Stmm	 */
407114484Stmm	iommu_init(name, &sc->sc_is, 3, -1, 1);
40890618Stmm
409116213Stmm	/* Create the DMA tag. */
410167308Smarius	if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0, IOMMU_MAXADDR, ~0,
411167308Smarius	    NULL, NULL, IOMMU_MAXADDR, 0xff, 0xffffffff, 0, NULL, NULL,
412167308Smarius	    &sc->sc_cdmatag) != 0)
413145185Smarius		panic("%s: bus_dma_tag_create failed", __func__);
414116213Stmm	/* Customize the tag. */
415116213Stmm	sc->sc_cdmatag->dt_cookie = &sc->sc_is;
416116541Stmm	sc->sc_cdmatag->dt_mt = &iommu_dma_methods;
417116213Stmm
418145185Smarius	/* Enable the over-temperature and power-fail interrupts. */
419167308Smarius	rid = 4;
42090618Stmm	mr = SYSIO_READ8(sc, SBR_THERM_INT_MAP);
421167308Smarius	sc->sc_ot_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
422167308Smarius	    RF_ACTIVE);
423166034Smarius	if (sc->sc_ot_ires == NULL ||
424167308Smarius	    rman_get_start(sc->sc_ot_ires) != INTVEC(mr) ||
425166901Spiso	    bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC,
426166901Spiso	    sbus_overtemp, NULL, sc, &sc->sc_ot_ihand) != 0)
427166034Smarius		panic("%s: failed to set up temperature interrupt", __func__);
428107477Stmm	SYSIO_WRITE8(sc, SBR_THERM_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
429167308Smarius	rid = 3;
43090618Stmm	mr = SYSIO_READ8(sc, SBR_POWER_INT_MAP);
431167308Smarius	sc->sc_pf_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
432167308Smarius	    RF_ACTIVE);
433166034Smarius	if (sc->sc_pf_ires == NULL ||
434167308Smarius	    rman_get_start(sc->sc_pf_ires) != INTVEC(mr) ||
435166901Spiso	    bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC,
436166901Spiso	    sbus_pwrfail, NULL, sc, &sc->sc_pf_ihand) != 0)
437166034Smarius		panic("%s: failed to set up power fail interrupt", __func__);
438107477Stmm	SYSIO_WRITE8(sc, SBR_POWER_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
43990618Stmm
44090618Stmm	/* Initialize the counter-timer. */
44190618Stmm	sparc64_counter_init(sc->sc_bustag, sc->sc_bushandle, SBR_TC0);
44290618Stmm
44390618Stmm	/*
44490618Stmm	 * Loop through ROM children, fixing any relative addresses
44590618Stmm	 * and then configuring each device.
44690618Stmm	 */
44790618Stmm	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
448152684Smarius		if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL)
44990618Stmm			continue;
450146391Smarius		/*
451146391Smarius		 * For devices where there are variants that are actually
452146391Smarius		 * split into two SBus devices (as opposed to the first
453146391Smarius		 * half of the device being a SBus device and the second
454146391Smarius		 * half hanging off of the first one) like 'auxio' and
455146391Smarius		 * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device
456146391Smarius		 * which is a prerequisite to the driver attaching to the
457146391Smarius		 * second one with a lower order. Saves us from dealing
458146391Smarius		 * with different probe orders in the respective device
459146391Smarius		 * drivers which generally is more hackish.
460146391Smarius		 */
461146391Smarius		cdev = device_add_child_ordered(dev, (OF_child(child) == 0 &&
462152684Smarius		    sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ?
463152684Smarius		    SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1);
464152684Smarius		if (cdev == NULL) {
465152684Smarius			device_printf(dev,
466152684Smarius			    "<%s>: device_add_child_ordered failed\n",
467152684Smarius			    sdi->sdi_obdinfo.obd_name);
468152684Smarius			sbus_destroy_dinfo(sdi);
469152684Smarius			continue;
470152684Smarius		}
47190618Stmm		device_set_ivars(cdev, sdi);
47290618Stmm	}
473145186Smarius	return (bus_generic_attach(dev));
47490618Stmm}
47590618Stmm
47690618Stmmstatic struct sbus_devinfo *
477152684Smariussbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node)
47890618Stmm{
47990618Stmm	struct sbus_devinfo *sdi;
48090618Stmm	struct sbus_regs *reg;
48190618Stmm	u_int32_t base, iv, *intr;
48290618Stmm	int i, nreg, nintr, slot, rslot;
48390618Stmm
484111119Simp	sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK);
485152684Smarius	if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) {
486152684Smarius		free(sdi, M_DEVBUF);
48790618Stmm		return (NULL);
488152684Smarius	}
48990618Stmm	resource_list_init(&sdi->sdi_rl);
49090618Stmm	slot = -1;
49190618Stmm	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
49290618Stmm	if (nreg == -1) {
493152684Smarius		if (sdi->sdi_obdinfo.obd_type == NULL ||
494152684Smarius		    strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) {
495152684Smarius			device_printf(dev, "<%s>: incomplete\n",
496152684Smarius			    sdi->sdi_obdinfo.obd_name);
497152684Smarius			goto fail;
49890618Stmm		}
49990618Stmm	} else {
50090618Stmm		for (i = 0; i < nreg; i++) {
50190618Stmm			base = reg[i].sbr_offset;
50290618Stmm			if (SBUS_ABS(base)) {
50390618Stmm				rslot = SBUS_ABS_TO_SLOT(base);
50490618Stmm				base = SBUS_ABS_TO_OFFSET(base);
50590618Stmm			} else
50690618Stmm				rslot = reg[i].sbr_slot;
507152684Smarius			if (slot != -1 && slot != rslot) {
508152684Smarius				device_printf(dev, "<%s>: multiple slots\n",
509152684Smarius				    sdi->sdi_obdinfo.obd_name);
510152684Smarius				free(reg, M_OFWPROP);
511152684Smarius				goto fail;
512152684Smarius			}
51390618Stmm			slot = rslot;
51490618Stmm
51590618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i,
51690618Stmm			    base, base + reg[i].sbr_size, reg[i].sbr_size);
51790618Stmm		}
51890618Stmm		free(reg, M_OFWPROP);
51990618Stmm	}
52090618Stmm	sdi->sdi_slot = slot;
52190618Stmm
52290618Stmm	/*
523145185Smarius	 * The `interrupts' property contains the SBus interrupt level.
52490618Stmm	 */
525145185Smarius	nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
526145185Smarius	    (void **)&intr);
52790618Stmm	if (nintr != -1) {
52890618Stmm		for (i = 0; i < nintr; i++) {
52990618Stmm			iv = intr[i];
53090618Stmm			/*
531145185Smarius			 * SBus card devices need the slot number encoded into
53290618Stmm			 * the vector as this is generally not done.
53390618Stmm			 */
534107477Stmm			if ((iv & INTMAP_OBIO_MASK) == 0)
53590618Stmm				iv |= slot << 3;
53690618Stmm			/* Set the ign as appropriate. */
537146391Smarius			iv |= sc->sc_ign << INTMAP_IGN_SHIFT;
53890618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i,
53990618Stmm			    iv, iv, 1);
54090618Stmm		}
54190618Stmm		free(intr, M_OFWPROP);
54290618Stmm	}
54390618Stmm	if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz,
54490618Stmm	    sizeof(sdi->sdi_burstsz)) == -1)
54590618Stmm		sdi->sdi_burstsz = sc->sc_burst;
54690618Stmm	else
54790618Stmm		sdi->sdi_burstsz &= sc->sc_burst;
548145186Smarius	if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq,
549145186Smarius	    sizeof(sdi->sdi_clockfreq)) == -1)
550145186Smarius		sdi->sdi_clockfreq = sc->sc_clockfreq;
55190618Stmm
55290618Stmm	return (sdi);
553152684Smarius
554152684Smariusfail:
555152684Smarius	sbus_destroy_dinfo(sdi);
556152684Smarius	return (NULL);
55790618Stmm}
55890618Stmm
55990618Stmmstatic void
56090618Stmmsbus_destroy_dinfo(struct sbus_devinfo *dinfo)
56190618Stmm{
56290618Stmm
56390618Stmm	resource_list_free(&dinfo->sdi_rl);
564152684Smarius	ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo);
56590618Stmm	free(dinfo, M_DEVBUF);
56690618Stmm}
56790618Stmm
56890618Stmmstatic int
56990618Stmmsbus_print_child(device_t dev, device_t child)
57090618Stmm{
57190618Stmm	int rv;
57290618Stmm
57390618Stmm	rv = bus_print_child_header(dev, child);
574152684Smarius	rv += sbus_print_res(device_get_ivars(child));
57590618Stmm	rv += bus_print_child_footer(dev, child);
57690618Stmm	return (rv);
57790618Stmm}
57890618Stmm
57990618Stmmstatic void
58090618Stmmsbus_probe_nomatch(device_t dev, device_t child)
58190618Stmm{
582152684Smarius	const char *type;
58390618Stmm
584152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
585152684Smarius	sbus_print_res(device_get_ivars(child));
586152684Smarius	type = ofw_bus_get_type(child);
587145186Smarius	printf(" type %s (no driver attached)\n",
588152684Smarius	    type != NULL ? type : "unknown");
58990618Stmm}
59090618Stmm
59190618Stmmstatic int
59290618Stmmsbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
59390618Stmm{
594146391Smarius	struct sbus_softc *sc;
59590618Stmm	struct sbus_devinfo *dinfo;
59690618Stmm
597146391Smarius	sc = device_get_softc(dev);
59890618Stmm	if ((dinfo = device_get_ivars(child)) == NULL)
59990618Stmm		return (ENOENT);
60090618Stmm	switch (which) {
60190618Stmm	case SBUS_IVAR_BURSTSZ:
60290618Stmm		*result = dinfo->sdi_burstsz;
60390618Stmm		break;
60490618Stmm	case SBUS_IVAR_CLOCKFREQ:
605145186Smarius		*result = dinfo->sdi_clockfreq;
60690618Stmm		break;
607146391Smarius	case SBUS_IVAR_IGN:
608146391Smarius		*result = sc->sc_ign;
609146391Smarius		break;
61090618Stmm	case SBUS_IVAR_SLOT:
61190618Stmm		*result = dinfo->sdi_slot;
61290618Stmm		break;
61390618Stmm	default:
61490618Stmm		return (ENOENT);
61590618Stmm	}
616146391Smarius	return (0);
61790618Stmm}
61890618Stmm
61990618Stmmstatic struct resource_list *
62090618Stmmsbus_get_resource_list(device_t dev, device_t child)
62190618Stmm{
62290618Stmm	struct sbus_devinfo *sdi;
62390618Stmm
62490618Stmm	sdi = device_get_ivars(child);
62590618Stmm	return (&sdi->sdi_rl);
62690618Stmm}
62790618Stmm
62890618Stmm/* Write to the correct clr register, and call the actual handler. */
629166901Spisostatic int
63090618Stmmsbus_intr_stub(void *arg)
63190618Stmm{
63290618Stmm	struct sbus_clr *scl;
63390618Stmm
63490618Stmm	scl = (struct sbus_clr *)arg;
63590618Stmm	scl->scl_handler(scl->scl_arg);
63690618Stmm	SYSIO_WRITE8(scl->scl_sc, scl->scl_clr, 0);
637166901Spiso	return (FILTER_HANDLED);
63890618Stmm}
63990618Stmm
64090618Stmmstatic int
641145185Smariussbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
642166901Spiso    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
64390618Stmm{
64490618Stmm	struct sbus_softc *sc;
64590618Stmm	struct sbus_clr *scl;
64690618Stmm	bus_addr_t intrmapptr, intrclrptr, intrptr;
64790618Stmm	u_int64_t intrmap;
64890618Stmm	u_int32_t inr, slot;
64990618Stmm	int error, i;
650159413Smarius	long vec;
65190618Stmm
652166901Spiso	if (filt != NULL && intr != NULL)
653166901Spiso		return (EINVAL);
654145185Smarius	sc = device_get_softc(dev);
65590618Stmm	scl = (struct sbus_clr *)malloc(sizeof(*scl), M_DEVBUF, M_NOWAIT);
65690618Stmm	if (scl == NULL)
657159413Smarius		return (ENOMEM);
65890618Stmm	intrptr = intrmapptr = intrclrptr = 0;
65990618Stmm	intrmap = 0;
660159413Smarius	vec = rman_get_start(ires);
661107474Stmm	inr = INTVEC(vec);
662107477Stmm	if ((inr & INTMAP_OBIO_MASK) == 0) {
663107474Stmm		/*
664145185Smarius		 * We're in an SBus slot, register the map and clear
665107474Stmm		 * intr registers.
666107474Stmm		 */
667107474Stmm		slot = INTSLOT(vec);
668107474Stmm		intrmapptr = SBR_SLOT0_INT_MAP + slot * 8;
669107474Stmm		intrclrptr = SBR_SLOT0_INT_CLR +
670107474Stmm		    (slot * 8 * 8) + (INTPRI(vec) * 8);
671107474Stmm		/* Enable the interrupt, insert IGN. */
672146391Smarius		intrmap = inr | (sc->sc_ign << INTMAP_IGN_SHIFT);
673107474Stmm	} else {
674107474Stmm		intrptr = SBR_SCSI_INT_MAP;
675107474Stmm		/* Insert IGN */
676146391Smarius		inr |= sc->sc_ign << INTMAP_IGN_SHIFT;
677107474Stmm		for (i = 0; intrptr <= SBR_RESERVED_INT_MAP &&
678159413Smarius		    INTVEC(intrmap = SYSIO_READ8(sc, intrptr)) != inr;
679159413Smarius		    intrptr += 8, i++)
680107474Stmm			;
681159413Smarius		if (INTVEC(intrmap) == inr) {
682107474Stmm			/* Register the map and clear intr registers */
683107474Stmm			intrmapptr = intrptr;
684107474Stmm			intrclrptr = SBR_SCSI_INT_CLR + i * 8;
685107474Stmm			/* Enable the interrupt */
686107474Stmm		} else
687145185Smarius			panic("%s: IRQ not found!", __func__);
688107474Stmm	}
68990618Stmm
69090618Stmm	scl->scl_sc = sc;
69190618Stmm	scl->scl_arg = arg;
692166901Spiso	scl->scl_handler = (filt != NULL) ? filt : (driver_filter_t *)intr;
69390618Stmm	scl->scl_clr = intrclrptr;
69490618Stmm	/* Disable the interrupt while we fiddle with it */
695159413Smarius	SYSIO_WRITE8(sc, intrmapptr, intrmap & ~INTMAP_V);
696166901Spiso	if (filt != NULL)
697166901Spiso		error = BUS_SETUP_INTR(device_get_parent(dev), child, ires,
698166901Spiso		    flags, sbus_intr_stub, NULL, scl, cookiep);
699166901Spiso	else
700166901Spiso		error = BUS_SETUP_INTR(device_get_parent(dev), child, ires,
701166901Spiso		    flags, NULL, (driver_intr_t *)sbus_intr_stub, scl,
702166901Spiso		    cookiep);
70390618Stmm	if (error != 0) {
70490618Stmm		free(scl, M_DEVBUF);
70590618Stmm		return (error);
70690618Stmm	}
70790618Stmm	scl->scl_cookie = *cookiep;
70890618Stmm	*cookiep = scl;
70990618Stmm
71090618Stmm	/*
71190618Stmm	 * Clear the interrupt, it might have been triggered before it was
71290618Stmm	 * set up.
71390618Stmm	 */
714107474Stmm	SYSIO_WRITE8(sc, intrclrptr, 0);
71590618Stmm	/*
716107477Stmm	 * Enable the interrupt and program the target module now we have the
717107477Stmm	 * handler installed.
71890618Stmm	 */
719107477Stmm	SYSIO_WRITE8(sc, intrmapptr, INTMAP_ENABLE(intrmap, PCPU_GET(mid)));
72090618Stmm	return (error);
72190618Stmm}
72290618Stmm
72390618Stmmstatic int
724152684Smariussbus_teardown_intr(device_t dev, device_t child, struct resource *vec,
725152684Smarius    void *cookie)
72690618Stmm{
72790618Stmm	struct sbus_clr *scl;
72890618Stmm	int error;
72990618Stmm
73090618Stmm	scl = (struct sbus_clr *)cookie;
73190618Stmm	error = BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec,
73290618Stmm	    scl->scl_cookie);
73390618Stmm	/*
734145185Smarius	 * Don't disable the interrupt for now, so that stray interrupts get
73590618Stmm	 * detected...
73690618Stmm	 */
73790618Stmm	if (error != 0)
73890618Stmm		free(scl, M_DEVBUF);
73990618Stmm	return (error);
74090618Stmm}
74190618Stmm
74290618Stmmstatic struct resource *
74390618Stmmsbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
74490618Stmm    u_long start, u_long end, u_long count, u_int flags)
74590618Stmm{
74690618Stmm	struct sbus_softc *sc;
74790618Stmm	struct rman *rm;
74890618Stmm	struct resource *rv;
74990618Stmm	struct resource_list *rl;
75090618Stmm	struct resource_list_entry *rle;
751145186Smarius	device_t schild;
75290618Stmm	bus_space_handle_t bh;
75390618Stmm	bus_addr_t toffs;
75490618Stmm	bus_size_t tend;
755145186Smarius	int i, slot;
756145186Smarius	int isdefault, needactivate, passthrough;
75790618Stmm
758145185Smarius	isdefault = (start == 0UL && end == ~0UL);
759145185Smarius	needactivate = flags & RF_ACTIVE;
760145186Smarius	passthrough = (device_get_parent(child) != bus);
761145186Smarius	rle = NULL;
762145186Smarius	sc = device_get_softc(bus);
763145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
76490618Stmm	switch (type) {
76590618Stmm	case SYS_RES_IRQ:
766145186Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
767145186Smarius		    end, count, flags));
76890618Stmm	case SYS_RES_MEMORY:
769145186Smarius		if (!passthrough) {
770145186Smarius			rle = resource_list_find(rl, type, *rid);
771145186Smarius			if (rle == NULL)
772145186Smarius				return (NULL);
773145186Smarius			if (rle->res != NULL)
774145186Smarius				panic("%s: resource entry is busy", __func__);
775145186Smarius			if (isdefault) {
776145186Smarius				start = rle->start;
777145186Smarius				count = ulmax(count, rle->count);
778145186Smarius				end = ulmax(rle->end, start + count - 1);
779145186Smarius			}
780145186Smarius		}
78190618Stmm		rm = NULL;
78290618Stmm		bh = toffs = tend = 0;
783145186Smarius		schild = child;
784145186Smarius		while (device_get_parent(schild) != bus)
785145186Smarius			schild = device_get_parent(child);
786145186Smarius		slot = sbus_get_slot(schild);
78790618Stmm		for (i = 0; i < sc->sc_nrange; i++) {
788145186Smarius			if (sc->sc_rd[i].rd_slot != slot ||
78990618Stmm			    start < sc->sc_rd[i].rd_coffset ||
79090618Stmm			    start > sc->sc_rd[i].rd_cend)
79190618Stmm				continue;
79290618Stmm			/* Disallow cross-range allocations. */
79390618Stmm			if (end > sc->sc_rd[i].rd_cend)
79490618Stmm				return (NULL);
79590618Stmm			/* We've found the connection to the parent bus */
79690618Stmm			toffs = start - sc->sc_rd[i].rd_coffset;
79790618Stmm			tend = end - sc->sc_rd[i].rd_coffset;
79890618Stmm			rm = &sc->sc_rd[i].rd_rman;
79990618Stmm			bh = sc->sc_rd[i].rd_bushandle;
800159413Smarius			break;
80190618Stmm		}
802159413Smarius		if (rm == NULL)
80390618Stmm			return (NULL);
80490618Stmm		flags &= ~RF_ACTIVE;
80590618Stmm		rv = rman_reserve_resource(rm, toffs, tend, count, flags,
80690618Stmm		    child);
80790618Stmm		if (rv == NULL)
80890618Stmm			return (NULL);
809157896Simp		rman_set_rid(rv, *rid);
81090618Stmm		rman_set_bustag(rv, sc->sc_cbustag);
81190618Stmm		rman_set_bushandle(rv, bh + rman_get_start(rv));
81290618Stmm		if (needactivate) {
81390618Stmm			if (bus_activate_resource(child, type, *rid, rv)) {
81490618Stmm				rman_release_resource(rv);
81590618Stmm				return (NULL);
81690618Stmm			}
81790618Stmm		}
818145186Smarius		if (!passthrough)
819145186Smarius			rle->res = rv;
820145186Smarius		return (rv);
82190618Stmm	default:
82290618Stmm		return (NULL);
82390618Stmm	}
82490618Stmm}
82590618Stmm
82690618Stmmstatic int
82790618Stmmsbus_activate_resource(device_t bus, device_t child, int type, int rid,
82890618Stmm    struct resource *r)
82990618Stmm{
830159413Smarius	void *p;
831159413Smarius	int error;
83290618Stmm
833108798Stmm	if (type == SYS_RES_IRQ) {
834108798Stmm		return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
835108798Stmm		    child, type, rid, r));
836108798Stmm	}
837159413Smarius	if (type == SYS_RES_MEMORY) {
838159413Smarius		/*
839159413Smarius		 * Need to memory-map the device space, as some drivers depend
840159413Smarius		 * on the virtual address being set and useable.
841159413Smarius		 */
842159413Smarius		error = sparc64_bus_mem_map(rman_get_bustag(r),
843159413Smarius		    rman_get_bushandle(r), rman_get_size(r), 0, 0, &p);
844159413Smarius		if (error != 0)
845159413Smarius			return (error);
846159413Smarius		rman_set_virtual(r, p);
847159413Smarius	}
84890618Stmm	return (rman_activate_resource(r));
84990618Stmm}
85090618Stmm
85190618Stmmstatic int
85290618Stmmsbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
85390618Stmm    struct resource *r)
85490618Stmm{
85590618Stmm
856108798Stmm	if (type == SYS_RES_IRQ) {
857108798Stmm		return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus),
858108798Stmm		    child, type, rid, r));
859108798Stmm	}
860159413Smarius	if (type == SYS_RES_MEMORY) {
861159413Smarius		sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r));
862159413Smarius		rman_set_virtual(r, NULL);
863159413Smarius	}
86490618Stmm	return (rman_deactivate_resource(r));
86590618Stmm}
86690618Stmm
86790618Stmmstatic int
86890618Stmmsbus_release_resource(device_t bus, device_t child, int type, int rid,
86990618Stmm    struct resource *r)
87090618Stmm{
871145186Smarius	struct resource_list *rl;
872108798Stmm	struct resource_list_entry *rle;
873145186Smarius	int error, passthrough;
87490618Stmm
875145186Smarius	passthrough = (device_get_parent(child) != bus);
876145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
87790618Stmm	if (type == SYS_RES_IRQ)
878145186Smarius		return (resource_list_release(rl, bus, child, type, rid, r));
879145186Smarius	if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
880145186Smarius		error = bus_deactivate_resource(child, type, rid, r);
881108798Stmm		if (error != 0)
882108798Stmm			return (error);
88390618Stmm	}
884145186Smarius	error = rman_release_resource(r);
885145186Smarius	if (error != 0 || passthrough)
886108798Stmm		return (error);
887145186Smarius	rle = resource_list_find(rl, type, rid);
888108798Stmm	if (rle == NULL)
889145185Smarius		panic("%s: cannot find resource", __func__);
890108798Stmm	if (rle->res == NULL)
891145185Smarius		panic("%s: resource entry is not busy", __func__);
892108798Stmm	rle->res = NULL;
893108798Stmm	return (0);
89490618Stmm}
89590618Stmm
896167308Smariusstatic bus_dma_tag_t
897167308Smariussbus_get_dma_tag(device_t bus, device_t child)
898167308Smarius{
899167308Smarius	struct sbus_softc *sc;
900167308Smarius
901167308Smarius	sc = device_get_softc(bus);
902167308Smarius	return (sc->sc_cdmatag);
903167308Smarius}
904167308Smarius
905152684Smariusstatic const struct ofw_bus_devinfo *
906152684Smariussbus_get_devinfo(device_t bus, device_t child)
907152684Smarius{
908152684Smarius	struct sbus_devinfo *sdi;
909152684Smarius
910152684Smarius	sdi = device_get_ivars(child);
911152684Smarius	return (&sdi->sdi_obdinfo);
912152684Smarius}
913152684Smarius
91490618Stmm/*
91590618Stmm * Handle an overtemp situation.
91690618Stmm *
91790618Stmm * SPARCs have temperature sensors which generate interrupts
91890618Stmm * if the machine's temperature exceeds a certain threshold.
91990618Stmm * This handles the interrupt and powers off the machine.
92090618Stmm * The same needs to be done to PCI controller drivers.
92190618Stmm */
922166901Spisostatic int
92390618Stmmsbus_overtemp(void *arg)
92490618Stmm{
92590618Stmm
92690618Stmm	printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n");
92790618Stmm	shutdown_nice(RB_POWEROFF);
928166901Spiso	return (FILTER_HANDLED);
92990618Stmm}
93090618Stmm
93190618Stmm/* Try to shut down in time in case of power failure. */
932166901Spisostatic int
93390618Stmmsbus_pwrfail(void *arg)
93490618Stmm{
93590618Stmm
93690618Stmm	printf("Power failure detected\nShutting down NOW.\n");
93790618Stmm	shutdown_nice(0);
938166901Spiso	return (FILTER_HANDLED);
93990618Stmm}
94090618Stmm
94190618Stmmstatic bus_space_tag_t
94290618Stmmsbus_alloc_bustag(struct sbus_softc *sc)
94390618Stmm{
94490618Stmm	bus_space_tag_t sbt;
94590618Stmm
94690618Stmm	sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
94790618Stmm	    M_NOWAIT | M_ZERO);
94890618Stmm	if (sbt == NULL)
949145185Smarius		panic("%s: out of memory", __func__);
95090618Stmm
951108815Stmm	sbt->bst_cookie = sc;
952108815Stmm	sbt->bst_parent = sc->sc_bustag;
953108815Stmm	sbt->bst_type = SBUS_BUS_SPACE;
95490618Stmm	return (sbt);
95590618Stmm}
956133589Smarius
957152684Smariusstatic int
958152684Smariussbus_print_res(struct sbus_devinfo *sdi)
959133589Smarius{
960152684Smarius	int rv;
961133589Smarius
962152684Smarius	rv = 0;
963152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY,
964152684Smarius	    "%#lx");
965152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ,
966152684Smarius	    "%ld");
967152684Smarius	return (rv);
968133589Smarius}
969