sbus.c revision 166034
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>. 7690618Stmm * All rights reserved. 7790618Stmm * 7890618Stmm * Redistribution and use in source and binary forms, with or without 7990618Stmm * modification, are permitted provided that the following conditions 8090618Stmm * are met: 8190618Stmm * 1. Redistributions of source code must retain the above copyright 8290618Stmm * notice, this list of conditions and the following disclaimer. 8390618Stmm * 8490618Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 8590618Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 8690618Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 8790618Stmm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 8890618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 8990618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 9090618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 9190618Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 9290618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 9390618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 9490618Stmm * SUCH DAMAGE. 9590618Stmm * 9690618Stmm * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93 9790618Stmm * from: NetBSD: sbus.c,v 1.46 2001/10/07 20:30:41 eeh Exp 9890618Stmm */ 9990618Stmm 100145185Smarius#include <sys/cdefs.h> 101145185Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sbus/sbus.c 166034 2007-01-15 22:37:59Z marius $"); 102145185Smarius 10390618Stmm/* 104145185Smarius * SBus support. 10590618Stmm */ 106131376Smarius 10790618Stmm#include <sys/param.h> 10890618Stmm#include <sys/systm.h> 10990618Stmm#include <sys/bus.h> 11090618Stmm#include <sys/kernel.h> 11190618Stmm#include <sys/malloc.h> 112130068Sphk#include <sys/module.h> 113107477Stmm#include <sys/pcpu.h> 11490618Stmm#include <sys/reboot.h> 11590618Stmm 116133589Smarius#include <dev/ofw/ofw_bus.h> 117152684Smarius#include <dev/ofw/ofw_bus_subr.h> 118119338Simp#include <dev/ofw/openfirm.h> 11990618Stmm 12090618Stmm#include <machine/bus.h> 121116541Stmm#include <machine/bus_private.h> 12290618Stmm#include <machine/iommureg.h> 12390618Stmm#include <machine/bus_common.h> 12490618Stmm#include <machine/intr_machdep.h> 12590618Stmm#include <machine/nexusvar.h> 12690618Stmm#include <machine/ofw_upa.h> 12790618Stmm#include <machine/resource.h> 12890618Stmm 12990618Stmm#include <sys/rman.h> 13090618Stmm 13190618Stmm#include <machine/iommuvar.h> 13290618Stmm 13390618Stmm#include <sparc64/sbus/ofw_sbus.h> 13490618Stmm#include <sparc64/sbus/sbusreg.h> 13590618Stmm#include <sparc64/sbus/sbusvar.h> 13690618Stmm 13790618Stmmstruct sbus_devinfo { 13890618Stmm int sdi_burstsz; 139145186Smarius int sdi_clockfreq; 14090618Stmm int sdi_slot; 14190618Stmm 142152684Smarius struct ofw_bus_devinfo sdi_obdinfo; 14390618Stmm struct resource_list sdi_rl; 14490618Stmm}; 14590618Stmm 14690618Stmm/* Range descriptor, allocated for each sc_range. */ 14790618Stmmstruct sbus_rd { 14890618Stmm bus_addr_t rd_poffset; 14990618Stmm bus_addr_t rd_pend; 15090618Stmm int rd_slot; 15190618Stmm bus_addr_t rd_coffset; 15290618Stmm bus_addr_t rd_cend; 15390618Stmm struct rman rd_rman; 15490618Stmm bus_space_handle_t rd_bushandle; 15590618Stmm struct resource *rd_res; 15690618Stmm}; 15790618Stmm 15890618Stmmstruct sbus_softc { 15990618Stmm bus_space_tag_t sc_bustag; 16090618Stmm bus_space_handle_t sc_bushandle; 16190618Stmm bus_dma_tag_t sc_dmatag; 16290618Stmm bus_dma_tag_t sc_cdmatag; 16390618Stmm bus_space_tag_t sc_cbustag; 16490618Stmm int sc_clockfreq; /* clock frequency (in Hz) */ 16590618Stmm struct upa_regs *sc_reg; 16690618Stmm int sc_nreg; 16790618Stmm int sc_nrange; 16890618Stmm struct sbus_rd *sc_rd; 169145185Smarius int sc_burst; /* burst transfer sizes supp. */ 17090618Stmm 17190618Stmm struct resource *sc_sysio_res; 172145185Smarius int sc_ign; /* IGN for this sysio */ 173145185Smarius struct iommu_state sc_is; /* IOMMU state (iommuvar.h) */ 17490618Stmm 17590618Stmm struct resource *sc_ot_ires; 17690618Stmm void *sc_ot_ihand; 17790618Stmm struct resource *sc_pf_ires; 17890618Stmm void *sc_pf_ihand; 17990618Stmm}; 18090618Stmm 18190618Stmmstruct sbus_clr { 18290618Stmm struct sbus_softc *scl_sc; 183145185Smarius bus_addr_t scl_clr; /* clear register */ 184145185Smarius driver_intr_t *scl_handler; /* handler to call */ 185145185Smarius void *scl_arg; /* argument for the handler */ 186145185Smarius void *scl_cookie; /* parent bus int. cookie */ 18790618Stmm}; 18890618Stmm 18990618Stmm#define SYSIO_READ8(sc, off) \ 19090618Stmm bus_space_read_8((sc)->sc_bustag, (sc)->sc_bushandle, (off)) 19190618Stmm#define SYSIO_WRITE8(sc, off, v) \ 19290618Stmm bus_space_write_8((sc)->sc_bustag, (sc)->sc_bushandle, (off), (v)) 19390618Stmm 194145185Smariusstatic device_probe_t sbus_probe; 195145186Smariusstatic device_attach_t sbus_attach; 196145185Smariusstatic bus_print_child_t sbus_print_child; 197145185Smariusstatic bus_probe_nomatch_t sbus_probe_nomatch; 198145185Smariusstatic bus_read_ivar_t sbus_read_ivar; 199145185Smariusstatic bus_get_resource_list_t sbus_get_resource_list; 200145185Smariusstatic bus_setup_intr_t sbus_setup_intr; 201145185Smariusstatic bus_teardown_intr_t sbus_teardown_intr; 202145185Smariusstatic bus_alloc_resource_t sbus_alloc_resource; 203145185Smariusstatic bus_release_resource_t sbus_release_resource; 204145185Smariusstatic bus_activate_resource_t sbus_activate_resource; 205145185Smariusstatic bus_deactivate_resource_t sbus_deactivate_resource; 206152684Smariusstatic ofw_bus_get_devinfo_t sbus_get_devinfo; 20790618Stmm 208146391Smariusstatic int sbus_inlist(const char *, const char **); 209152684Smariusstatic struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *, 210152684Smarius phandle_t); 211152684Smariusstatic void sbus_destroy_dinfo(struct sbus_devinfo *); 21290618Stmmstatic void sbus_intr_stub(void *); 21390618Stmmstatic bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *); 21490618Stmmstatic void sbus_overtemp(void *); 21590618Stmmstatic void sbus_pwrfail(void *); 216152684Smariusstatic int sbus_print_res(struct sbus_devinfo *); 21790618Stmm 21890618Stmmstatic device_method_t sbus_methods[] = { 21990618Stmm /* Device interface */ 22090618Stmm DEVMETHOD(device_probe, sbus_probe), 221145186Smarius DEVMETHOD(device_attach, sbus_attach), 222154870Smarius DEVMETHOD(device_shutdown, bus_generic_shutdown), 223154870Smarius DEVMETHOD(device_suspend, bus_generic_suspend), 224154870Smarius DEVMETHOD(device_resume, bus_generic_resume), 22590618Stmm 22690618Stmm /* Bus interface */ 22790618Stmm DEVMETHOD(bus_print_child, sbus_print_child), 22890618Stmm DEVMETHOD(bus_probe_nomatch, sbus_probe_nomatch), 22990618Stmm DEVMETHOD(bus_read_ivar, sbus_read_ivar), 23090618Stmm DEVMETHOD(bus_setup_intr, sbus_setup_intr), 23190618Stmm DEVMETHOD(bus_teardown_intr, sbus_teardown_intr), 23290618Stmm DEVMETHOD(bus_alloc_resource, sbus_alloc_resource), 23390618Stmm DEVMETHOD(bus_activate_resource, sbus_activate_resource), 23490618Stmm DEVMETHOD(bus_deactivate_resource, sbus_deactivate_resource), 23590618Stmm DEVMETHOD(bus_release_resource, sbus_release_resource), 23690618Stmm DEVMETHOD(bus_get_resource_list, sbus_get_resource_list), 23790618Stmm DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 23890618Stmm 239133589Smarius /* ofw_bus interface */ 240152684Smarius DEVMETHOD(ofw_bus_get_devinfo, sbus_get_devinfo), 241152684Smarius DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 242152684Smarius DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 243152684Smarius DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 244152684Smarius DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 245152684Smarius DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 246133589Smarius 24790618Stmm { 0, 0 } 24890618Stmm}; 24990618Stmm 25090618Stmmstatic driver_t sbus_driver = { 25190618Stmm "sbus", 25290618Stmm sbus_methods, 25390618Stmm sizeof(struct sbus_softc), 25490618Stmm}; 25590618Stmm 25690618Stmmstatic devclass_t sbus_devclass; 25790618Stmm 25890618StmmDRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0); 25990618Stmm 26090618Stmm#define OFW_SBUS_TYPE "sbus" 26190618Stmm#define OFW_SBUS_NAME "sbus" 26290618Stmm 263146391Smariusstatic const char *sbus_order_first[] = { 264146391Smarius "auxio", 265146391Smarius "dma", 266146391Smarius NULL 267146391Smarius}; 268146391Smarius 26990618Stmmstatic int 270146391Smariussbus_inlist(const char *name, const char **list) 271146391Smarius{ 272146391Smarius int i; 273146391Smarius 274146391Smarius if (name == NULL) 275146391Smarius return (0); 276146391Smarius for (i = 0; list[i] != NULL; i++) { 277146391Smarius if (strcmp(name, list[i]) == 0) 278146391Smarius return (1); 279146391Smarius } 280146391Smarius return (0); 281146391Smarius} 282146391Smarius 283146391Smariusstatic int 28490618Stmmsbus_probe(device_t dev) 28590618Stmm{ 286145186Smarius char *t; 287145186Smarius 288145186Smarius t = nexus_get_device_type(dev); 289145186Smarius if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) && 290145186Smarius strcmp(nexus_get_name(dev), OFW_SBUS_NAME) != 0) 291145186Smarius return (ENXIO); 292145186Smarius device_set_desc(dev, "U2S UPA-SBus bridge"); 293145186Smarius return (0); 294145186Smarius} 295145186Smarius 296145186Smariusstatic int 297145186Smariussbus_attach(device_t dev) 298145186Smarius{ 299145185Smarius struct sbus_softc *sc; 30090618Stmm struct sbus_devinfo *sdi; 30190618Stmm struct sbus_ranges *range; 30290618Stmm struct resource *res; 30390618Stmm device_t cdev; 30490618Stmm bus_addr_t phys; 30590618Stmm bus_size_t size; 306152684Smarius char *name; 307145185Smarius phandle_t child, node; 30890618Stmm u_int64_t mr; 30990618Stmm int intr, clock, rid, vec, i; 31090618Stmm 311145185Smarius sc = device_get_softc(dev); 312145185Smarius node = nexus_get_node(dev); 313145185Smarius 31490618Stmm if ((sc->sc_nreg = OF_getprop_alloc(node, "reg", sizeof(*sc->sc_reg), 31590618Stmm (void **)&sc->sc_reg)) == -1) { 316145185Smarius panic("%s: error getting reg property", __func__); 31790618Stmm } 31890618Stmm if (sc->sc_nreg < 1) 319145185Smarius panic("%s: bogus properties", __func__); 32090618Stmm phys = UPA_REG_PHYS(&sc->sc_reg[0]); 32190618Stmm size = UPA_REG_SIZE(&sc->sc_reg[0]); 32290618Stmm rid = 0; 32390618Stmm sc->sc_sysio_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, phys, 32490618Stmm phys + size - 1, size, RF_ACTIVE); 32590618Stmm if (sc->sc_sysio_res == NULL || 32690618Stmm rman_get_start(sc->sc_sysio_res) != phys) 327145185Smarius panic("%s: cannot allocate device memory", __func__); 32890618Stmm sc->sc_bustag = rman_get_bustag(sc->sc_sysio_res); 32990618Stmm sc->sc_bushandle = rman_get_bushandle(sc->sc_sysio_res); 33090618Stmm 33190618Stmm if (OF_getprop(node, "interrupts", &intr, sizeof(intr)) == -1) 332145185Smarius panic("%s: cannot get IGN", __func__); 333146391Smarius sc->sc_ign = (intr & INTMAP_IGN_MASK) >> INTMAP_IGN_SHIFT; 33490618Stmm sc->sc_cbustag = sbus_alloc_bustag(sc); 33590618Stmm 33690618Stmm /* 33790618Stmm * Record clock frequency for synchronous SCSI. 33890618Stmm * IS THIS THE CORRECT DEFAULT?? 33990618Stmm */ 34090618Stmm if (OF_getprop(node, "clock-frequency", &clock, sizeof(clock)) == -1) 34190618Stmm clock = 25000000; 34290618Stmm sc->sc_clockfreq = clock; 34390618Stmm clock /= 1000; 34490618Stmm device_printf(dev, "clock %d.%03d MHz\n", clock / 1000, clock % 1000); 34590618Stmm 34690618Stmm /* 34790618Stmm * Collect address translations from the OBP. 34890618Stmm */ 34990618Stmm if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges", 35090618Stmm sizeof(*range), (void **)&range)) == -1) { 351145185Smarius panic("%s: error getting ranges property", __func__); 35290618Stmm } 35390618Stmm sc->sc_rd = (struct sbus_rd *)malloc(sizeof(*sc->sc_rd) * sc->sc_nrange, 35490618Stmm M_DEVBUF, M_NOWAIT); 35590618Stmm if (sc->sc_rd == NULL) 356145185Smarius panic("%s: cannot allocate rmans", __func__); 35790618Stmm /* 35890618Stmm * Preallocate all space that the SBus bridge decodes, so that nothing 35990618Stmm * else gets in the way; set up rmans etc. 36090618Stmm */ 36190618Stmm for (i = 0; i < sc->sc_nrange; i++) { 36290618Stmm phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32); 36390618Stmm size = range[i].size; 36490618Stmm sc->sc_rd[i].rd_slot = range[i].cspace; 36590618Stmm sc->sc_rd[i].rd_coffset = range[i].coffset; 36690618Stmm sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size; 36790618Stmm rid = 0; 36890618Stmm if ((res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, phys, 36990618Stmm phys + size - 1, size, RF_ACTIVE)) == NULL) 370145185Smarius panic("%s: cannot allocate decoded range", __func__); 37190618Stmm sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res); 37290618Stmm sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY; 37390618Stmm sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory"; 37490618Stmm if (rman_init(&sc->sc_rd[i].rd_rman) != 0 || 37590618Stmm rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0) 376145185Smarius panic("%s: failed to set up memory rman", __func__); 37790618Stmm sc->sc_rd[i].rd_poffset = phys; 37890618Stmm sc->sc_rd[i].rd_pend = phys + size; 37990618Stmm sc->sc_rd[i].rd_res = res; 38090618Stmm } 38190618Stmm free(range, M_OFWPROP); 38290618Stmm 38390618Stmm /* 38490618Stmm * Get the SBus burst transfer size if burst transfers are supported. 38590618Stmm * XXX: is the default correct? 38690618Stmm */ 38790618Stmm if (OF_getprop(node, "burst-sizes", &sc->sc_burst, 38890618Stmm sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0) 38990618Stmm sc->sc_burst = SBUS_BURST_DEF; 39090618Stmm 39190618Stmm /* initalise the IOMMU */ 39290618Stmm 39390618Stmm /* punch in our copies */ 39490618Stmm sc->sc_is.is_bustag = sc->sc_bustag; 39590618Stmm sc->sc_is.is_bushandle = sc->sc_bushandle; 39690618Stmm sc->sc_is.is_iommu = SBR_IOMMU; 39790618Stmm sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG; 39890618Stmm sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG; 39990618Stmm sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG; 40090618Stmm sc->sc_is.is_dva = SBR_IOMMU_SVADIAG; 40190618Stmm sc->sc_is.is_dtcmp = 0; 40290618Stmm sc->sc_is.is_sb[0] = SBR_STRBUF; 403123865Sobrien sc->sc_is.is_sb[1] = 0; 40490618Stmm 40590618Stmm /* give us a nice name.. */ 40690618Stmm name = (char *)malloc(32, M_DEVBUF, M_NOWAIT); 407145185Smarius if (name == NULL) 408145185Smarius panic("%s: cannot malloc iommu name", __func__); 40990618Stmm snprintf(name, 32, "%s dvma", device_get_name(dev)); 41090618Stmm 411100188Stmm /* 412100188Stmm * Note: the SBus IOMMU ignores the high bits of an address, so a NULL 413100188Stmm * DMA pointer will be translated by the first page of the IOTSB. 414100188Stmm * To detect bugs we'll allocate and ignore the first entry. 415100188Stmm */ 416114484Stmm iommu_init(name, &sc->sc_is, 3, -1, 1); 41790618Stmm 418116213Stmm /* Create the DMA tag. */ 419116213Stmm sc->sc_dmatag = nexus_get_dmatag(dev); 420116213Stmm if (bus_dma_tag_create(sc->sc_dmatag, 8, 1, 0, 0x3ffffffff, NULL, NULL, 421117126Sscottl 0x3ffffffff, 0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_cdmatag) != 0) 422145185Smarius panic("%s: bus_dma_tag_create failed", __func__); 423116213Stmm /* Customize the tag. */ 424116213Stmm sc->sc_cdmatag->dt_cookie = &sc->sc_is; 425116541Stmm sc->sc_cdmatag->dt_mt = &iommu_dma_methods; 426116213Stmm /* XXX: register as root dma tag (kludge). */ 427116213Stmm sparc64_root_dma_tag = sc->sc_cdmatag; 428116213Stmm 429145185Smarius /* Enable the over-temperature and power-fail interrupts. */ 43090618Stmm rid = 0; 43190618Stmm mr = SYSIO_READ8(sc, SBR_THERM_INT_MAP); 43290618Stmm vec = INTVEC(mr); 433166034Smarius sc->sc_ot_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, vec, 434166034Smarius vec, 1, RF_ACTIVE); 435166034Smarius if (sc->sc_ot_ires == NULL || 436166034Smarius bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC | INTR_FAST, 437166034Smarius sbus_overtemp, sc, &sc->sc_ot_ihand) != 0) 438166034Smarius panic("%s: failed to set up temperature interrupt", __func__); 439107477Stmm SYSIO_WRITE8(sc, SBR_THERM_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid))); 44090618Stmm rid = 0; 44190618Stmm mr = SYSIO_READ8(sc, SBR_POWER_INT_MAP); 44290618Stmm vec = INTVEC(mr); 443166034Smarius sc->sc_pf_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, vec, 444166034Smarius vec, 1, RF_ACTIVE); 445166034Smarius if (sc->sc_pf_ires == NULL || 446166034Smarius bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC | INTR_FAST, 447166034Smarius sbus_pwrfail, sc, &sc->sc_pf_ihand) != 0) 448166034Smarius panic("%s: failed to set up power fail interrupt", __func__); 449107477Stmm SYSIO_WRITE8(sc, SBR_POWER_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid))); 45090618Stmm 45190618Stmm /* Initialize the counter-timer. */ 45290618Stmm sparc64_counter_init(sc->sc_bustag, sc->sc_bushandle, SBR_TC0); 45390618Stmm 45490618Stmm /* 45590618Stmm * Loop through ROM children, fixing any relative addresses 45690618Stmm * and then configuring each device. 45790618Stmm */ 45890618Stmm for (child = OF_child(node); child != 0; child = OF_peer(child)) { 459152684Smarius if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL) 46090618Stmm continue; 461146391Smarius /* 462146391Smarius * For devices where there are variants that are actually 463146391Smarius * split into two SBus devices (as opposed to the first 464146391Smarius * half of the device being a SBus device and the second 465146391Smarius * half hanging off of the first one) like 'auxio' and 466146391Smarius * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device 467146391Smarius * which is a prerequisite to the driver attaching to the 468146391Smarius * second one with a lower order. Saves us from dealing 469146391Smarius * with different probe orders in the respective device 470146391Smarius * drivers which generally is more hackish. 471146391Smarius */ 472146391Smarius cdev = device_add_child_ordered(dev, (OF_child(child) == 0 && 473152684Smarius sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ? 474152684Smarius SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1); 475152684Smarius if (cdev == NULL) { 476152684Smarius device_printf(dev, 477152684Smarius "<%s>: device_add_child_ordered failed\n", 478152684Smarius sdi->sdi_obdinfo.obd_name); 479152684Smarius sbus_destroy_dinfo(sdi); 480152684Smarius continue; 481152684Smarius } 48290618Stmm device_set_ivars(cdev, sdi); 48390618Stmm } 484145186Smarius return (bus_generic_attach(dev)); 48590618Stmm} 48690618Stmm 48790618Stmmstatic struct sbus_devinfo * 488152684Smariussbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node) 48990618Stmm{ 49090618Stmm struct sbus_devinfo *sdi; 49190618Stmm struct sbus_regs *reg; 49290618Stmm u_int32_t base, iv, *intr; 49390618Stmm int i, nreg, nintr, slot, rslot; 49490618Stmm 495111119Simp sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK); 496152684Smarius if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) { 497152684Smarius free(sdi, M_DEVBUF); 49890618Stmm return (NULL); 499152684Smarius } 50090618Stmm resource_list_init(&sdi->sdi_rl); 50190618Stmm slot = -1; 50290618Stmm nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 50390618Stmm if (nreg == -1) { 504152684Smarius if (sdi->sdi_obdinfo.obd_type == NULL || 505152684Smarius strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) { 506152684Smarius device_printf(dev, "<%s>: incomplete\n", 507152684Smarius sdi->sdi_obdinfo.obd_name); 508152684Smarius goto fail; 50990618Stmm } 51090618Stmm } else { 51190618Stmm for (i = 0; i < nreg; i++) { 51290618Stmm base = reg[i].sbr_offset; 51390618Stmm if (SBUS_ABS(base)) { 51490618Stmm rslot = SBUS_ABS_TO_SLOT(base); 51590618Stmm base = SBUS_ABS_TO_OFFSET(base); 51690618Stmm } else 51790618Stmm rslot = reg[i].sbr_slot; 518152684Smarius if (slot != -1 && slot != rslot) { 519152684Smarius device_printf(dev, "<%s>: multiple slots\n", 520152684Smarius sdi->sdi_obdinfo.obd_name); 521152684Smarius free(reg, M_OFWPROP); 522152684Smarius goto fail; 523152684Smarius } 52490618Stmm slot = rslot; 52590618Stmm 52690618Stmm resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i, 52790618Stmm base, base + reg[i].sbr_size, reg[i].sbr_size); 52890618Stmm } 52990618Stmm free(reg, M_OFWPROP); 53090618Stmm } 53190618Stmm sdi->sdi_slot = slot; 53290618Stmm 53390618Stmm /* 534145185Smarius * The `interrupts' property contains the SBus interrupt level. 53590618Stmm */ 536145185Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 537145185Smarius (void **)&intr); 53890618Stmm if (nintr != -1) { 53990618Stmm for (i = 0; i < nintr; i++) { 54090618Stmm iv = intr[i]; 54190618Stmm /* 542145185Smarius * SBus card devices need the slot number encoded into 54390618Stmm * the vector as this is generally not done. 54490618Stmm */ 545107477Stmm if ((iv & INTMAP_OBIO_MASK) == 0) 54690618Stmm iv |= slot << 3; 54790618Stmm /* Set the ign as appropriate. */ 548146391Smarius iv |= sc->sc_ign << INTMAP_IGN_SHIFT; 54990618Stmm resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i, 55090618Stmm iv, iv, 1); 55190618Stmm } 55290618Stmm free(intr, M_OFWPROP); 55390618Stmm } 55490618Stmm if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz, 55590618Stmm sizeof(sdi->sdi_burstsz)) == -1) 55690618Stmm sdi->sdi_burstsz = sc->sc_burst; 55790618Stmm else 55890618Stmm sdi->sdi_burstsz &= sc->sc_burst; 559145186Smarius if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq, 560145186Smarius sizeof(sdi->sdi_clockfreq)) == -1) 561145186Smarius sdi->sdi_clockfreq = sc->sc_clockfreq; 56290618Stmm 56390618Stmm return (sdi); 564152684Smarius 565152684Smariusfail: 566152684Smarius sbus_destroy_dinfo(sdi); 567152684Smarius return (NULL); 56890618Stmm} 56990618Stmm 57090618Stmmstatic void 57190618Stmmsbus_destroy_dinfo(struct sbus_devinfo *dinfo) 57290618Stmm{ 57390618Stmm 57490618Stmm resource_list_free(&dinfo->sdi_rl); 575152684Smarius ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo); 57690618Stmm free(dinfo, M_DEVBUF); 57790618Stmm} 57890618Stmm 57990618Stmmstatic int 58090618Stmmsbus_print_child(device_t dev, device_t child) 58190618Stmm{ 58290618Stmm int rv; 58390618Stmm 58490618Stmm rv = bus_print_child_header(dev, child); 585152684Smarius rv += sbus_print_res(device_get_ivars(child)); 58690618Stmm rv += bus_print_child_footer(dev, child); 58790618Stmm return (rv); 58890618Stmm} 58990618Stmm 59090618Stmmstatic void 59190618Stmmsbus_probe_nomatch(device_t dev, device_t child) 59290618Stmm{ 593152684Smarius const char *type; 59490618Stmm 595152684Smarius device_printf(dev, "<%s>", ofw_bus_get_name(child)); 596152684Smarius sbus_print_res(device_get_ivars(child)); 597152684Smarius type = ofw_bus_get_type(child); 598145186Smarius printf(" type %s (no driver attached)\n", 599152684Smarius type != NULL ? type : "unknown"); 60090618Stmm} 60190618Stmm 60290618Stmmstatic int 60390618Stmmsbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 60490618Stmm{ 605146391Smarius struct sbus_softc *sc; 60690618Stmm struct sbus_devinfo *dinfo; 60790618Stmm 608146391Smarius sc = device_get_softc(dev); 60990618Stmm if ((dinfo = device_get_ivars(child)) == NULL) 61090618Stmm return (ENOENT); 61190618Stmm switch (which) { 61290618Stmm case SBUS_IVAR_BURSTSZ: 61390618Stmm *result = dinfo->sdi_burstsz; 61490618Stmm break; 61590618Stmm case SBUS_IVAR_CLOCKFREQ: 616145186Smarius *result = dinfo->sdi_clockfreq; 61790618Stmm break; 618146391Smarius case SBUS_IVAR_IGN: 619146391Smarius *result = sc->sc_ign; 620146391Smarius break; 62190618Stmm case SBUS_IVAR_SLOT: 62290618Stmm *result = dinfo->sdi_slot; 62390618Stmm break; 62490618Stmm default: 62590618Stmm return (ENOENT); 62690618Stmm } 627146391Smarius return (0); 62890618Stmm} 62990618Stmm 63090618Stmmstatic struct resource_list * 63190618Stmmsbus_get_resource_list(device_t dev, device_t child) 63290618Stmm{ 63390618Stmm struct sbus_devinfo *sdi; 63490618Stmm 63590618Stmm sdi = device_get_ivars(child); 63690618Stmm return (&sdi->sdi_rl); 63790618Stmm} 63890618Stmm 63990618Stmm/* Write to the correct clr register, and call the actual handler. */ 64090618Stmmstatic void 64190618Stmmsbus_intr_stub(void *arg) 64290618Stmm{ 64390618Stmm struct sbus_clr *scl; 64490618Stmm 64590618Stmm scl = (struct sbus_clr *)arg; 64690618Stmm scl->scl_handler(scl->scl_arg); 64790618Stmm SYSIO_WRITE8(scl->scl_sc, scl->scl_clr, 0); 64890618Stmm} 64990618Stmm 65090618Stmmstatic int 651145185Smariussbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags, 652145185Smarius driver_intr_t *intr, void *arg, void **cookiep) 65390618Stmm{ 65490618Stmm struct sbus_softc *sc; 65590618Stmm struct sbus_clr *scl; 65690618Stmm bus_addr_t intrmapptr, intrclrptr, intrptr; 65790618Stmm u_int64_t intrmap; 65890618Stmm u_int32_t inr, slot; 65990618Stmm int error, i; 660159413Smarius long vec; 66190618Stmm 662145185Smarius sc = device_get_softc(dev); 66390618Stmm scl = (struct sbus_clr *)malloc(sizeof(*scl), M_DEVBUF, M_NOWAIT); 66490618Stmm if (scl == NULL) 665159413Smarius return (ENOMEM); 66690618Stmm intrptr = intrmapptr = intrclrptr = 0; 66790618Stmm intrmap = 0; 668159413Smarius vec = rman_get_start(ires); 669107474Stmm inr = INTVEC(vec); 670107477Stmm if ((inr & INTMAP_OBIO_MASK) == 0) { 671107474Stmm /* 672145185Smarius * We're in an SBus slot, register the map and clear 673107474Stmm * intr registers. 674107474Stmm */ 675107474Stmm slot = INTSLOT(vec); 676107474Stmm intrmapptr = SBR_SLOT0_INT_MAP + slot * 8; 677107474Stmm intrclrptr = SBR_SLOT0_INT_CLR + 678107474Stmm (slot * 8 * 8) + (INTPRI(vec) * 8); 679107474Stmm /* Enable the interrupt, insert IGN. */ 680146391Smarius intrmap = inr | (sc->sc_ign << INTMAP_IGN_SHIFT); 681107474Stmm } else { 682107474Stmm intrptr = SBR_SCSI_INT_MAP; 683107474Stmm /* Insert IGN */ 684146391Smarius inr |= sc->sc_ign << INTMAP_IGN_SHIFT; 685107474Stmm for (i = 0; intrptr <= SBR_RESERVED_INT_MAP && 686159413Smarius INTVEC(intrmap = SYSIO_READ8(sc, intrptr)) != inr; 687159413Smarius intrptr += 8, i++) 688107474Stmm ; 689159413Smarius if (INTVEC(intrmap) == inr) { 690107474Stmm /* Register the map and clear intr registers */ 691107474Stmm intrmapptr = intrptr; 692107474Stmm intrclrptr = SBR_SCSI_INT_CLR + i * 8; 693107474Stmm /* Enable the interrupt */ 694107474Stmm } else 695145185Smarius panic("%s: IRQ not found!", __func__); 696107474Stmm } 69790618Stmm 69890618Stmm scl->scl_sc = sc; 69990618Stmm scl->scl_arg = arg; 70090618Stmm scl->scl_handler = intr; 70190618Stmm scl->scl_clr = intrclrptr; 70290618Stmm /* Disable the interrupt while we fiddle with it */ 703159413Smarius SYSIO_WRITE8(sc, intrmapptr, intrmap & ~INTMAP_V); 70490618Stmm error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, 70590618Stmm sbus_intr_stub, scl, cookiep); 70690618Stmm if (error != 0) { 70790618Stmm free(scl, M_DEVBUF); 70890618Stmm return (error); 70990618Stmm } 71090618Stmm scl->scl_cookie = *cookiep; 71190618Stmm *cookiep = scl; 71290618Stmm 71390618Stmm /* 71490618Stmm * Clear the interrupt, it might have been triggered before it was 71590618Stmm * set up. 71690618Stmm */ 717107474Stmm SYSIO_WRITE8(sc, intrclrptr, 0); 71890618Stmm /* 719107477Stmm * Enable the interrupt and program the target module now we have the 720107477Stmm * handler installed. 72190618Stmm */ 722107477Stmm SYSIO_WRITE8(sc, intrmapptr, INTMAP_ENABLE(intrmap, PCPU_GET(mid))); 72390618Stmm return (error); 72490618Stmm} 72590618Stmm 72690618Stmmstatic int 727152684Smariussbus_teardown_intr(device_t dev, device_t child, struct resource *vec, 728152684Smarius void *cookie) 72990618Stmm{ 73090618Stmm struct sbus_clr *scl; 73190618Stmm int error; 73290618Stmm 73390618Stmm scl = (struct sbus_clr *)cookie; 73490618Stmm error = BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec, 73590618Stmm scl->scl_cookie); 73690618Stmm /* 737145185Smarius * Don't disable the interrupt for now, so that stray interrupts get 73890618Stmm * detected... 73990618Stmm */ 74090618Stmm if (error != 0) 74190618Stmm free(scl, M_DEVBUF); 74290618Stmm return (error); 74390618Stmm} 74490618Stmm 74590618Stmmstatic struct resource * 74690618Stmmsbus_alloc_resource(device_t bus, device_t child, int type, int *rid, 74790618Stmm u_long start, u_long end, u_long count, u_int flags) 74890618Stmm{ 74990618Stmm struct sbus_softc *sc; 75090618Stmm struct rman *rm; 75190618Stmm struct resource *rv; 75290618Stmm struct resource_list *rl; 75390618Stmm struct resource_list_entry *rle; 754145186Smarius device_t schild; 75590618Stmm bus_space_handle_t bh; 75690618Stmm bus_addr_t toffs; 75790618Stmm bus_size_t tend; 758145186Smarius int i, slot; 759145186Smarius int isdefault, needactivate, passthrough; 76090618Stmm 761145185Smarius isdefault = (start == 0UL && end == ~0UL); 762145185Smarius needactivate = flags & RF_ACTIVE; 763145186Smarius passthrough = (device_get_parent(child) != bus); 764145186Smarius rle = NULL; 765145186Smarius sc = device_get_softc(bus); 766145186Smarius rl = BUS_GET_RESOURCE_LIST(bus, child); 76790618Stmm switch (type) { 76890618Stmm case SYS_RES_IRQ: 769145186Smarius return (resource_list_alloc(rl, bus, child, type, rid, start, 770145186Smarius end, count, flags)); 77190618Stmm case SYS_RES_MEMORY: 772145186Smarius if (!passthrough) { 773145186Smarius rle = resource_list_find(rl, type, *rid); 774145186Smarius if (rle == NULL) 775145186Smarius return (NULL); 776145186Smarius if (rle->res != NULL) 777145186Smarius panic("%s: resource entry is busy", __func__); 778145186Smarius if (isdefault) { 779145186Smarius start = rle->start; 780145186Smarius count = ulmax(count, rle->count); 781145186Smarius end = ulmax(rle->end, start + count - 1); 782145186Smarius } 783145186Smarius } 78490618Stmm rm = NULL; 78590618Stmm bh = toffs = tend = 0; 786145186Smarius schild = child; 787145186Smarius while (device_get_parent(schild) != bus) 788145186Smarius schild = device_get_parent(child); 789145186Smarius slot = sbus_get_slot(schild); 79090618Stmm for (i = 0; i < sc->sc_nrange; i++) { 791145186Smarius if (sc->sc_rd[i].rd_slot != slot || 79290618Stmm start < sc->sc_rd[i].rd_coffset || 79390618Stmm start > sc->sc_rd[i].rd_cend) 79490618Stmm continue; 79590618Stmm /* Disallow cross-range allocations. */ 79690618Stmm if (end > sc->sc_rd[i].rd_cend) 79790618Stmm return (NULL); 79890618Stmm /* We've found the connection to the parent bus */ 79990618Stmm toffs = start - sc->sc_rd[i].rd_coffset; 80090618Stmm tend = end - sc->sc_rd[i].rd_coffset; 80190618Stmm rm = &sc->sc_rd[i].rd_rman; 80290618Stmm bh = sc->sc_rd[i].rd_bushandle; 803159413Smarius break; 80490618Stmm } 805159413Smarius if (rm == NULL) 80690618Stmm return (NULL); 80790618Stmm flags &= ~RF_ACTIVE; 80890618Stmm rv = rman_reserve_resource(rm, toffs, tend, count, flags, 80990618Stmm child); 81090618Stmm if (rv == NULL) 81190618Stmm return (NULL); 812157896Simp rman_set_rid(rv, *rid); 81390618Stmm rman_set_bustag(rv, sc->sc_cbustag); 81490618Stmm rman_set_bushandle(rv, bh + rman_get_start(rv)); 81590618Stmm if (needactivate) { 81690618Stmm if (bus_activate_resource(child, type, *rid, rv)) { 81790618Stmm rman_release_resource(rv); 81890618Stmm return (NULL); 81990618Stmm } 82090618Stmm } 821145186Smarius if (!passthrough) 822145186Smarius rle->res = rv; 823145186Smarius return (rv); 82490618Stmm default: 82590618Stmm return (NULL); 82690618Stmm } 82790618Stmm} 82890618Stmm 82990618Stmmstatic int 83090618Stmmsbus_activate_resource(device_t bus, device_t child, int type, int rid, 83190618Stmm struct resource *r) 83290618Stmm{ 833159413Smarius void *p; 834159413Smarius int error; 83590618Stmm 836108798Stmm if (type == SYS_RES_IRQ) { 837108798Stmm return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), 838108798Stmm child, type, rid, r)); 839108798Stmm } 840159413Smarius if (type == SYS_RES_MEMORY) { 841159413Smarius /* 842159413Smarius * Need to memory-map the device space, as some drivers depend 843159413Smarius * on the virtual address being set and useable. 844159413Smarius */ 845159413Smarius error = sparc64_bus_mem_map(rman_get_bustag(r), 846159413Smarius rman_get_bushandle(r), rman_get_size(r), 0, 0, &p); 847159413Smarius if (error != 0) 848159413Smarius return (error); 849159413Smarius rman_set_virtual(r, p); 850159413Smarius } 85190618Stmm return (rman_activate_resource(r)); 85290618Stmm} 85390618Stmm 85490618Stmmstatic int 85590618Stmmsbus_deactivate_resource(device_t bus, device_t child, int type, int rid, 85690618Stmm struct resource *r) 85790618Stmm{ 85890618Stmm 859108798Stmm if (type == SYS_RES_IRQ) { 860108798Stmm return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), 861108798Stmm child, type, rid, r)); 862108798Stmm } 863159413Smarius if (type == SYS_RES_MEMORY) { 864159413Smarius sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r)); 865159413Smarius rman_set_virtual(r, NULL); 866159413Smarius } 86790618Stmm return (rman_deactivate_resource(r)); 86890618Stmm} 86990618Stmm 87090618Stmmstatic int 87190618Stmmsbus_release_resource(device_t bus, device_t child, int type, int rid, 87290618Stmm struct resource *r) 87390618Stmm{ 874145186Smarius struct resource_list *rl; 875108798Stmm struct resource_list_entry *rle; 876145186Smarius int error, passthrough; 87790618Stmm 878145186Smarius passthrough = (device_get_parent(child) != bus); 879145186Smarius rl = BUS_GET_RESOURCE_LIST(bus, child); 88090618Stmm if (type == SYS_RES_IRQ) 881145186Smarius return (resource_list_release(rl, bus, child, type, rid, r)); 882145186Smarius if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 883145186Smarius error = bus_deactivate_resource(child, type, rid, r); 884108798Stmm if (error != 0) 885108798Stmm return (error); 88690618Stmm } 887145186Smarius error = rman_release_resource(r); 888145186Smarius if (error != 0 || passthrough) 889108798Stmm return (error); 890145186Smarius rle = resource_list_find(rl, type, rid); 891108798Stmm if (rle == NULL) 892145185Smarius panic("%s: cannot find resource", __func__); 893108798Stmm if (rle->res == NULL) 894145185Smarius panic("%s: resource entry is not busy", __func__); 895108798Stmm rle->res = NULL; 896108798Stmm return (0); 89790618Stmm} 89890618Stmm 899152684Smariusstatic const struct ofw_bus_devinfo * 900152684Smariussbus_get_devinfo(device_t bus, device_t child) 901152684Smarius{ 902152684Smarius struct sbus_devinfo *sdi; 903152684Smarius 904152684Smarius sdi = device_get_ivars(child); 905152684Smarius return (&sdi->sdi_obdinfo); 906152684Smarius} 907152684Smarius 90890618Stmm/* 90990618Stmm * Handle an overtemp situation. 91090618Stmm * 91190618Stmm * SPARCs have temperature sensors which generate interrupts 91290618Stmm * if the machine's temperature exceeds a certain threshold. 91390618Stmm * This handles the interrupt and powers off the machine. 91490618Stmm * The same needs to be done to PCI controller drivers. 91590618Stmm */ 91690618Stmmstatic void 91790618Stmmsbus_overtemp(void *arg) 91890618Stmm{ 91990618Stmm 92090618Stmm printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n"); 92190618Stmm shutdown_nice(RB_POWEROFF); 92290618Stmm} 92390618Stmm 92490618Stmm/* Try to shut down in time in case of power failure. */ 92590618Stmmstatic void 92690618Stmmsbus_pwrfail(void *arg) 92790618Stmm{ 92890618Stmm 92990618Stmm printf("Power failure detected\nShutting down NOW.\n"); 93090618Stmm shutdown_nice(0); 93190618Stmm} 93290618Stmm 93390618Stmmstatic bus_space_tag_t 93490618Stmmsbus_alloc_bustag(struct sbus_softc *sc) 93590618Stmm{ 93690618Stmm bus_space_tag_t sbt; 93790618Stmm 93890618Stmm sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF, 93990618Stmm M_NOWAIT | M_ZERO); 94090618Stmm if (sbt == NULL) 941145185Smarius panic("%s: out of memory", __func__); 94290618Stmm 943108815Stmm sbt->bst_cookie = sc; 944108815Stmm sbt->bst_parent = sc->sc_bustag; 945108815Stmm sbt->bst_type = SBUS_BUS_SPACE; 94690618Stmm return (sbt); 94790618Stmm} 948133589Smarius 949152684Smariusstatic int 950152684Smariussbus_print_res(struct sbus_devinfo *sdi) 951133589Smarius{ 952152684Smarius int rv; 953133589Smarius 954152684Smarius rv = 0; 955152684Smarius rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY, 956152684Smarius "%#lx"); 957152684Smarius rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ, 958152684Smarius "%ld"); 959152684Smarius return (rv); 960133589Smarius} 961