Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.83
      1  1.83  nakayama /*	$NetBSD: sbus.c,v 1.83 2008/10/18 03:31:10 nakayama Exp $ */
      2   1.1       eeh 
      3  1.50       eeh /*
      4  1.50       eeh  * Copyright (c) 1999-2002 Eduardo Horvath
      5   1.1       eeh  * All rights reserved.
      6   1.1       eeh  *
      7   1.1       eeh  * Redistribution and use in source and binary forms, with or without
      8   1.1       eeh  * modification, are permitted provided that the following conditions
      9   1.1       eeh  * are met:
     10   1.1       eeh  * 1. Redistributions of source code must retain the above copyright
     11   1.1       eeh  *    notice, this list of conditions and the following disclaimer.
     12   1.1       eeh  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       eeh  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       eeh  *    documentation and/or other materials provided with the distribution.
     15  1.50       eeh  * 3. The name of the author may not be used to endorse or promote products
     16  1.50       eeh  *    derived from this software without specific prior written permission.
     17  1.18       eeh  *
     18  1.50       eeh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.50       eeh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.50       eeh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.50       eeh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.50       eeh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.50       eeh  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.50       eeh  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.50       eeh  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.50       eeh  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.18       eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.18       eeh  * SUCH DAMAGE.
     29  1.18       eeh  */
     30  1.18       eeh 
     31  1.18       eeh 
     32  1.18       eeh /*
     33   1.1       eeh  * Sbus stuff.
     34   1.1       eeh  */
     35  1.61     lukem 
     36  1.61     lukem #include <sys/cdefs.h>
     37  1.83  nakayama __KERNEL_RCSID(0, "$NetBSD: sbus.c,v 1.83 2008/10/18 03:31:10 nakayama Exp $");
     38  1.61     lukem 
     39   1.8       eeh #include "opt_ddb.h"
     40   1.1       eeh 
     41   1.1       eeh #include <sys/param.h>
     42  1.12       eeh #include <sys/extent.h>
     43   1.1       eeh #include <sys/malloc.h>
     44   1.1       eeh #include <sys/systm.h>
     45   1.1       eeh #include <sys/device.h>
     46  1.40       eeh #include <sys/reboot.h>
     47   1.1       eeh 
     48   1.1       eeh #include <machine/bus.h>
     49  1.50       eeh #include <machine/openfirm.h>
     50  1.50       eeh 
     51  1.13       mrg #include <sparc64/dev/iommureg.h>
     52  1.17       mrg #include <sparc64/dev/iommuvar.h>
     53   1.1       eeh #include <sparc64/dev/sbusreg.h>
     54   1.7        pk #include <dev/sbus/sbusvar.h>
     55   1.1       eeh 
     56  1.59   thorpej #include <uvm/uvm_extern.h>
     57  1.44       eeh 
     58   1.1       eeh #include <machine/autoconf.h>
     59   1.1       eeh #include <machine/cpu.h>
     60   1.8       eeh #include <machine/sparc64.h>
     61   1.1       eeh 
     62   1.1       eeh #ifdef DEBUG
     63   1.1       eeh #define SDB_DVMA	0x1
     64   1.1       eeh #define SDB_INTR	0x2
     65  1.27       mrg int sbus_debug = 0;
     66  1.27       mrg #define DPRINTF(l, s)   do { if (sbus_debug & l) printf s; } while (0)
     67  1.27       mrg #else
     68  1.27       mrg #define DPRINTF(l, s)
     69   1.1       eeh #endif
     70   1.1       eeh 
     71  1.75       cdi void sbusreset(int);
     72   1.1       eeh 
     73  1.75       cdi static bus_dma_tag_t sbus_alloc_dmatag(struct sbus_softc *);
     74  1.75       cdi static int sbus_get_intr(struct sbus_softc *, int, struct openprom_intr **,
     75  1.75       cdi 	int *, int);
     76  1.75       cdi static int sbus_overtemp(void *);
     77  1.75       cdi static int _sbus_bus_map(
     78   1.1       eeh 		bus_space_tag_t,
     79   1.1       eeh 		bus_addr_t,		/*offset*/
     80   1.1       eeh 		bus_size_t,		/*size*/
     81   1.1       eeh 		int,			/*flags*/
     82  1.47       eeh 		vaddr_t,			/* XXX unused -- compat w/sparc */
     83  1.75       cdi 		bus_space_handle_t *);
     84  1.75       cdi static void *sbus_intr_establish(
     85   1.1       eeh 		bus_space_tag_t,
     86  1.75       cdi 		int,			/*`device class' priority*/
     87  1.35        pk 		int,			/*Sbus interrupt level*/
     88  1.75       cdi 		int (*)(void *),	/*handler*/
     89  1.56        pk 		void *,			/*handler arg*/
     90  1.75       cdi 		void (*)(void));	/*optional fast trap*/
     91   1.1       eeh 
     92   1.1       eeh 
     93   1.1       eeh /* autoconfiguration driver */
     94  1.75       cdi int	sbus_match(struct device *, struct cfdata *, void *);
     95  1.75       cdi void	sbus_attach(struct device *, struct device *, void *);
     96   1.1       eeh 
     97   1.1       eeh 
     98  1.54   thorpej CFATTACH_DECL(sbus, sizeof(struct sbus_softc),
     99  1.55   thorpej     sbus_match, sbus_attach, NULL, NULL);
    100   1.1       eeh 
    101   1.1       eeh extern struct cfdriver sbus_cd;
    102   1.1       eeh 
    103   1.1       eeh /*
    104   1.1       eeh  * DVMA routines
    105   1.1       eeh  */
    106  1.83  nakayama static int sbus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
    107  1.83  nakayama 	bus_size_t, int, bus_dmamap_t *);
    108   1.1       eeh 
    109   1.1       eeh /*
    110   1.1       eeh  * Child devices receive the Sbus interrupt level in their attach
    111   1.1       eeh  * arguments. We translate these to CPU IPLs using the following
    112   1.1       eeh  * tables. Note: obio bus interrupt levels are identical to the
    113   1.1       eeh  * processor IPL.
    114   1.1       eeh  *
    115   1.1       eeh  * The second set of tables is used when the Sbus interrupt level
    116   1.1       eeh  * cannot be had from the PROM as an `interrupt' property. We then
    117   1.1       eeh  * fall back on the `intr' property which contains the CPU IPL.
    118   1.1       eeh  */
    119   1.1       eeh 
    120   1.1       eeh /*
    121   1.1       eeh  * This value is or'ed into the attach args' interrupt level cookie
    122   1.1       eeh  * if the interrupt level comes from an `intr' property, i.e. it is
    123   1.1       eeh  * not an Sbus interrupt level.
    124   1.1       eeh  */
    125   1.1       eeh #define SBUS_INTR_COMPAT	0x80000000
    126   1.1       eeh 
    127   1.1       eeh 
    128   1.1       eeh /*
    129   1.1       eeh  * Print the location of some sbus-attached device (called just
    130   1.1       eeh  * before attaching that device).  If `sbus' is not NULL, the
    131   1.1       eeh  * device was found but not configured; print the sbus as well.
    132   1.1       eeh  * Return UNCONF (config_find ignores this if the device was configured).
    133   1.1       eeh  */
    134   1.1       eeh int
    135  1.76       cdi sbus_print(void *args, const char *busname)
    136   1.1       eeh {
    137   1.1       eeh 	struct sbus_attach_args *sa = args;
    138   1.3       eeh 	int i;
    139   1.1       eeh 
    140   1.1       eeh 	if (busname)
    141  1.58   thorpej 		aprint_normal("%s at %s", sa->sa_name, busname);
    142  1.58   thorpej 	aprint_normal(" slot %ld offset 0x%lx", (long)sa->sa_slot,
    143   1.8       eeh 	       (u_long)sa->sa_offset);
    144  1.22       mrg 	for (i = 0; i < sa->sa_nintr; i++) {
    145  1.51   thorpej 		struct openprom_intr *sbi = &sa->sa_intr[i];
    146   1.1       eeh 
    147  1.58   thorpej 		aprint_normal(" vector %lx ipl %ld",
    148  1.51   thorpej 		       (u_long)sbi->oi_vec,
    149  1.51   thorpej 		       (long)INTLEV(sbi->oi_pri));
    150   1.1       eeh 	}
    151   1.1       eeh 	return (UNCONF);
    152   1.1       eeh }
    153   1.1       eeh 
    154   1.1       eeh int
    155  1.75       cdi sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    156   1.1       eeh {
    157   1.1       eeh 	struct mainbus_attach_args *ma = aux;
    158   1.1       eeh 
    159  1.52   thorpej 	return (strcmp(cf->cf_name, ma->ma_name) == 0);
    160   1.1       eeh }
    161   1.1       eeh 
    162   1.1       eeh /*
    163   1.1       eeh  * Attach an Sbus.
    164   1.1       eeh  */
    165   1.1       eeh void
    166  1.75       cdi sbus_attach(struct device *parent, struct device *self, void *aux)
    167   1.1       eeh {
    168  1.81    cegger 	struct sbus_softc *sc = device_private(self);
    169   1.1       eeh 	struct mainbus_attach_args *ma = aux;
    170  1.40       eeh 	struct intrhand *ih;
    171  1.40       eeh 	int ipl;
    172  1.27       mrg 	char *name;
    173   1.1       eeh 	int node = ma->ma_node;
    174   1.1       eeh 	int node0, error;
    175   1.1       eeh 	bus_space_tag_t sbt;
    176   1.1       eeh 	struct sbus_attach_args sa;
    177   1.1       eeh 
    178   1.1       eeh 	sc->sc_bustag = ma->ma_bustag;
    179   1.1       eeh 	sc->sc_dmatag = ma->ma_dmatag;
    180  1.48       eeh 	sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;
    181   1.1       eeh 
    182  1.48       eeh 	/* XXXX Use sysio PROM mappings for interrupt vector regs. */
    183  1.48       eeh 	sparc_promaddr_to_handle(sc->sc_bustag,	ma->ma_address[0], &sc->sc_bh);
    184  1.48       eeh 	sc->sc_sysio = (struct sysioreg *)bus_space_vaddr(sc->sc_bustag,
    185  1.49       eeh 		sc->sc_bh);
    186  1.48       eeh 
    187  1.48       eeh #ifdef _LP64
    188  1.48       eeh 	/*
    189  1.48       eeh 	 * 32-bit kernels use virtual addresses for bus space operations
    190  1.48       eeh 	 * so we may as well use the prom VA.
    191  1.48       eeh 	 *
    192  1.48       eeh 	 * 64-bit kernels use physical addresses for bus space operations
    193  1.48       eeh 	 * so mapping this in again will reduce TLB thrashing.
    194  1.48       eeh 	 */
    195  1.48       eeh 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr,
    196  1.48       eeh 		ma->ma_reg[0].ur_len, 0, &sc->sc_bh) != 0) {
    197  1.79    cegger 		aprint_error_dev(self, "cannot map registers\n");
    198  1.48       eeh 		return;
    199  1.48       eeh 	}
    200  1.48       eeh #endif
    201   1.1       eeh 
    202   1.1       eeh 	/*
    203   1.1       eeh 	 * Record clock frequency for synchronous SCSI.
    204   1.1       eeh 	 * IS THIS THE CORRECT DEFAULT??
    205   1.1       eeh 	 */
    206  1.67        pk 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency",
    207  1.48       eeh 		25*1000*1000);
    208   1.1       eeh 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    209   1.1       eeh 
    210  1.69        pk 	sbt = bus_space_tag_alloc(sc->sc_bustag, sc);
    211  1.69        pk 	sbt->type = SBUS_BUS_SPACE;
    212  1.69        pk 	sbt->sparc_bus_map = _sbus_bus_map;
    213  1.69        pk 	sbt->sparc_intr_establish = sbus_intr_establish;
    214  1.69        pk 
    215   1.1       eeh 	sc->sc_dmatag = sbus_alloc_dmatag(sc);
    216   1.1       eeh 
    217   1.1       eeh 	/*
    218   1.1       eeh 	 * Get the SBus burst transfer size if burst transfers are supported
    219   1.1       eeh 	 */
    220  1.67        pk 	sc->sc_burst = prom_getpropint(node, "burst-sizes", 0);
    221   1.1       eeh 
    222   1.1       eeh 	/*
    223   1.1       eeh 	 * Collect address translations from the OBP.
    224   1.1       eeh 	 */
    225  1.67        pk 	error = prom_getprop(node, "ranges", sizeof(struct openprom_range),
    226  1.69        pk 			 &sbt->nranges, &sbt->ranges);
    227  1.16       eeh 	if (error)
    228  1.79    cegger 		panic("%s: error getting ranges property", device_xname(&sc->sc_dev));
    229   1.1       eeh 
    230  1.48       eeh 	/* initialize the IOMMU */
    231  1.17       mrg 
    232  1.17       mrg 	/* punch in our copies */
    233  1.17       mrg 	sc->sc_is.is_bustag = sc->sc_bustag;
    234  1.48       eeh 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    235  1.48       eeh 		(vaddr_t)&((struct sysioreg *)NULL)->sys_iommu,
    236  1.48       eeh 		sizeof (struct iommureg), &sc->sc_is.is_iommu);
    237  1.50       eeh 
    238  1.50       eeh 	/* initialize our strbuf_ctl */
    239  1.50       eeh 	sc->sc_is.is_sb[0] = &sc->sc_sb;
    240  1.50       eeh 	sc->sc_sb.sb_is = &sc->sc_is;
    241  1.48       eeh 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    242  1.48       eeh 		(vaddr_t)&((struct sysioreg *)NULL)->sys_strbuf,
    243  1.50       eeh 		sizeof (struct iommu_strbuf), &sc->sc_sb.sb_sb);
    244  1.50       eeh 	/* Point sb_flush to our flush buffer. */
    245  1.50       eeh 	sc->sc_sb.sb_flush = &sc->sc_flush;
    246  1.16       eeh 
    247  1.27       mrg 	/* give us a nice name.. */
    248  1.27       mrg 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    249  1.27       mrg 	if (name == 0)
    250  1.27       mrg 		panic("couldn't malloc iommu name");
    251  1.79    cegger 	snprintf(name, 32, "%s dvma", device_xname(&sc->sc_dev));
    252  1.27       mrg 
    253  1.43       eeh 	iommu_init(name, &sc->sc_is, 0, -1);
    254  1.12       eeh 
    255  1.40       eeh 	/* Enable the over temp intr */
    256  1.40       eeh 	ih = (struct intrhand *)
    257  1.40       eeh 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    258  1.40       eeh 	ih->ih_map = &sc->sc_sysio->therm_int_map;
    259  1.40       eeh 	ih->ih_clr = NULL; /* &sc->sc_sysio->therm_clr_int; */
    260  1.40       eeh 	ih->ih_fun = sbus_overtemp;
    261  1.40       eeh 	ipl = 1;
    262  1.40       eeh 	ih->ih_pil = (1<<ipl);
    263  1.40       eeh 	ih->ih_number = INTVEC(*(ih->ih_map));
    264  1.80    martin 	intr_establish(ipl, true, ih);
    265  1.74    martin 	*(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
    266  1.40       eeh 
    267  1.42       mrg 	/*
    268  1.42       mrg 	 * Note: the stupid SBUS IOMMU ignores the high bits of an address, so a
    269  1.42       mrg 	 * NULL DMA pointer will be translated by the first page of the IOTSB.
    270  1.42       mrg 	 * To avoid bugs we'll alloc and ignore the first entry in the IOTSB.
    271  1.42       mrg 	 */
    272  1.42       mrg 	{
    273  1.42       mrg 		u_long dummy;
    274  1.42       mrg 
    275  1.42       mrg 		if (extent_alloc_subregion(sc->sc_is.is_dvmamap,
    276  1.59   thorpej 		    sc->sc_is.is_dvmabase, sc->sc_is.is_dvmabase + PAGE_SIZE,
    277  1.59   thorpej 		    PAGE_SIZE, PAGE_SIZE, 0, EX_NOWAIT|EX_BOUNDZERO,
    278  1.59   thorpej 		    (u_long *)&dummy) != 0)
    279  1.42       mrg 			panic("sbus iommu: can't toss first dvma page");
    280  1.42       mrg 	}
    281  1.42       mrg 
    282  1.12       eeh 	/*
    283   1.1       eeh 	 * Loop through ROM children, fixing any relative addresses
    284   1.1       eeh 	 * and then configuring each device.
    285   1.1       eeh 	 * `specials' is an array of device names that are treated
    286   1.1       eeh 	 * specially:
    287   1.1       eeh 	 */
    288  1.50       eeh 	node0 = OF_child(node);
    289  1.50       eeh 	for (node = node0; node; node = OF_peer(node)) {
    290  1.72  christos 		char *name1 = prom_getpropstring(node, "name");
    291   1.1       eeh 
    292   1.1       eeh 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    293  1.23        pk 					   node, &sa) != 0) {
    294  1.72  christos 			printf("sbus_attach: %s: incomplete\n", name1);
    295   1.1       eeh 			continue;
    296   1.1       eeh 		}
    297   1.1       eeh 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    298   1.3       eeh 		sbus_destroy_attach_args(&sa);
    299   1.1       eeh 	}
    300   1.1       eeh }
    301   1.1       eeh 
    302   1.1       eeh int
    303  1.76       cdi sbus_setup_attach_args(struct sbus_softc *sc, bus_space_tag_t bustag,
    304  1.76       cdi 	bus_dma_tag_t dmatag, int node, struct sbus_attach_args	*sa)
    305   1.1       eeh {
    306  1.51   thorpej 	/*struct	openprom_addr sbusreg;*/
    307   1.3       eeh 	/*int	base;*/
    308   1.1       eeh 	int	error;
    309   1.3       eeh 	int n;
    310   1.1       eeh 
    311  1.65    martin 	memset(sa, 0, sizeof(struct sbus_attach_args));
    312  1.68    martin 	n = 0;
    313  1.67        pk 	error = prom_getprop(node, "name", 1, &n, &sa->sa_name);
    314   1.3       eeh 	if (error != 0)
    315   1.3       eeh 		return (error);
    316   1.3       eeh 	sa->sa_name[n] = '\0';
    317   1.3       eeh 
    318   1.1       eeh 	sa->sa_bustag = bustag;
    319   1.1       eeh 	sa->sa_dmatag = dmatag;
    320   1.1       eeh 	sa->sa_node = node;
    321  1.37       eeh 	sa->sa_frequency = sc->sc_clockfreq;
    322   1.1       eeh 
    323  1.67        pk 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
    324  1.62       mrg 			 &sa->sa_nreg, &sa->sa_reg);
    325   1.3       eeh 	if (error != 0) {
    326   1.3       eeh 		char buf[32];
    327   1.3       eeh 		if (error != ENOENT ||
    328   1.3       eeh 		    !node_has_property(node, "device_type") ||
    329  1.67        pk 		    strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
    330   1.3       eeh 			   "hierarchical") != 0)
    331   1.3       eeh 			return (error);
    332   1.3       eeh 	}
    333   1.3       eeh 	for (n = 0; n < sa->sa_nreg; n++) {
    334   1.3       eeh 		/* Convert to relative addressing, if necessary */
    335  1.76       cdi 		uint32_t base = sa->sa_reg[n].oa_base;
    336   1.3       eeh 		if (SBUS_ABS(base)) {
    337  1.51   thorpej 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
    338  1.51   thorpej 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
    339   1.3       eeh 		}
    340   1.1       eeh 	}
    341   1.1       eeh 
    342  1.22       mrg 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr,
    343  1.22       mrg 	    sa->sa_slot)) != 0)
    344   1.1       eeh 		return (error);
    345   1.1       eeh 
    346  1.76       cdi 	error = prom_getprop(node, "address", sizeof(uint32_t),
    347  1.62       mrg 			 &sa->sa_npromvaddrs, &sa->sa_promvaddrs);
    348   1.3       eeh 	if (error != 0 && error != ENOENT)
    349   1.1       eeh 		return (error);
    350   1.1       eeh 
    351   1.1       eeh 	return (0);
    352   1.1       eeh }
    353   1.1       eeh 
    354   1.3       eeh void
    355  1.76       cdi sbus_destroy_attach_args(struct sbus_attach_args *sa)
    356   1.3       eeh {
    357   1.3       eeh 	if (sa->sa_name != NULL)
    358   1.3       eeh 		free(sa->sa_name, M_DEVBUF);
    359   1.3       eeh 
    360   1.3       eeh 	if (sa->sa_nreg != 0)
    361   1.3       eeh 		free(sa->sa_reg, M_DEVBUF);
    362   1.3       eeh 
    363   1.3       eeh 	if (sa->sa_intr)
    364   1.3       eeh 		free(sa->sa_intr, M_DEVBUF);
    365   1.3       eeh 
    366   1.3       eeh 	if (sa->sa_promvaddrs)
    367   1.8       eeh 		free((void *)sa->sa_promvaddrs, M_DEVBUF);
    368   1.3       eeh 
    369  1.65    martin 	memset(sa, 0, sizeof(struct sbus_attach_args)); /*DEBUG*/
    370   1.3       eeh }
    371   1.3       eeh 
    372   1.3       eeh 
    373   1.1       eeh int
    374  1.75       cdi _sbus_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags,
    375  1.75       cdi 	vaddr_t v, bus_space_handle_t *hp)
    376   1.1       eeh {
    377  1.69        pk 	int error;
    378   1.1       eeh 
    379  1.70        pk 	if (t->ranges != NULL) {
    380  1.70        pk 		if ((error = bus_space_translate_address_generic(
    381  1.70        pk 				t->ranges, t->nranges, &addr)) != 0)
    382  1.70        pk 			return (error);
    383  1.70        pk 	}
    384   1.1       eeh 
    385  1.69        pk 	return (bus_space_map(t->parent, addr, size, flags, hp));
    386   1.1       eeh }
    387   1.1       eeh 
    388  1.44       eeh 
    389  1.44       eeh bus_addr_t
    390  1.76       cdi sbus_bus_addr(bus_space_tag_t t, u_int btype, u_int offset)
    391  1.44       eeh {
    392  1.44       eeh 	int slot = btype;
    393  1.69        pk 	struct openprom_range *rp;
    394  1.44       eeh 	int i;
    395  1.44       eeh 
    396  1.69        pk 	for (i = 0; i < t->nranges; i++) {
    397  1.69        pk 		rp = &t->ranges[i];
    398  1.69        pk 		if (rp->or_child_space != slot)
    399  1.44       eeh 			continue;
    400  1.44       eeh 
    401  1.71       chs 		return BUS_ADDR(rp->or_parent_space,
    402  1.71       chs 				rp->or_parent_base + offset);
    403   1.1       eeh 	}
    404   1.1       eeh 
    405  1.69        pk 	return (0);
    406   1.1       eeh }
    407   1.1       eeh 
    408   1.1       eeh 
    409   1.1       eeh /*
    410   1.1       eeh  * Each attached device calls sbus_establish after it initializes
    411   1.1       eeh  * its sbusdev portion.
    412   1.1       eeh  */
    413   1.1       eeh void
    414  1.76       cdi sbus_establish(register struct sbusdev *sd, register struct device *dev)
    415   1.1       eeh {
    416   1.1       eeh 	register struct sbus_softc *sc;
    417   1.1       eeh 	register struct device *curdev;
    418   1.1       eeh 
    419   1.1       eeh 	/*
    420   1.1       eeh 	 * We have to look for the sbus by name, since it is not necessarily
    421   1.1       eeh 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    422   1.1       eeh 	 * We don't just use the device structure of the above-attached
    423   1.1       eeh 	 * sbus, since we might (in the future) support multiple sbus's.
    424   1.1       eeh 	 */
    425  1.77   thorpej 	for (curdev = device_parent(dev); ; curdev = device_parent(curdev)) {
    426  1.79    cegger 		if (!curdev || !device_xname(curdev))
    427   1.1       eeh 			panic("sbus_establish: can't find sbus parent for %s",
    428  1.79    cegger 			      device_xname(sd->sd_dev)
    429  1.79    cegger 					? device_xname(sd->sd_dev)
    430   1.1       eeh 					: "<unknown>" );
    431   1.1       eeh 
    432  1.79    cegger 		if (strncmp(device_xname(curdev), "sbus", 4) == 0)
    433   1.1       eeh 			break;
    434   1.1       eeh 	}
    435   1.1       eeh 	sc = (struct sbus_softc *) curdev;
    436   1.1       eeh 
    437   1.1       eeh 	sd->sd_dev = dev;
    438   1.1       eeh 	sd->sd_bchain = sc->sc_sbdev;
    439   1.1       eeh 	sc->sc_sbdev = sd;
    440   1.1       eeh }
    441   1.1       eeh 
    442   1.1       eeh /*
    443  1.33       mrg  * Reset the given sbus.
    444   1.1       eeh  */
    445   1.1       eeh void
    446  1.75       cdi sbusreset(int sbus)
    447   1.1       eeh {
    448   1.1       eeh 	register struct sbusdev *sd;
    449  1.81    cegger 	struct sbus_softc *sc = device_lookup_private(&sbus_cd, sbus);
    450   1.1       eeh 	struct device *dev;
    451   1.1       eeh 
    452  1.79    cegger 	printf("reset %s:", device_xname(&sc->sc_dev));
    453   1.1       eeh 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    454   1.1       eeh 		if (sd->sd_reset) {
    455   1.1       eeh 			dev = sd->sd_dev;
    456   1.1       eeh 			(*sd->sd_reset)(dev);
    457  1.79    cegger 			printf(" %s", device_xname(dev));
    458   1.1       eeh 		}
    459   1.1       eeh 	}
    460   1.1       eeh 	/* Reload iommu regs */
    461  1.17       mrg 	iommu_reset(&sc->sc_is);
    462  1.40       eeh }
    463  1.40       eeh 
    464  1.40       eeh /*
    465  1.40       eeh  * Handle an overtemp situation.
    466  1.41   hubertf  *
    467  1.41   hubertf  * SPARCs have temperature sensors which generate interrupts
    468  1.41   hubertf  * if the machine's temperature exceeds a certain threshold.
    469  1.41   hubertf  * This handles the interrupt and powers off the machine.
    470  1.41   hubertf  * The same needs to be done to PCI controller drivers.
    471  1.40       eeh  */
    472  1.40       eeh int
    473  1.75       cdi sbus_overtemp(void *arg)
    474  1.40       eeh {
    475  1.40       eeh 	/* Should try a clean shutdown first */
    476  1.41   hubertf 	printf("DANGER: OVER TEMPERATURE detected\nShutting down...\n");
    477  1.40       eeh 	delay(20);
    478  1.40       eeh 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
    479   1.1       eeh }
    480   1.1       eeh 
    481   1.1       eeh /*
    482   1.1       eeh  * Get interrupt attributes for an Sbus device.
    483   1.1       eeh  */
    484   1.1       eeh int
    485  1.75       cdi sbus_get_intr(struct sbus_softc *sc, int node, struct openprom_intr **ipp,
    486  1.75       cdi 	int *np, int slot)
    487   1.1       eeh {
    488   1.1       eeh 	int *ipl;
    489  1.22       mrg 	int n, i;
    490   1.1       eeh 	char buf[32];
    491   1.1       eeh 
    492   1.1       eeh 	/*
    493   1.1       eeh 	 * The `interrupts' property contains the Sbus interrupt level.
    494   1.1       eeh 	 */
    495   1.1       eeh 	ipl = NULL;
    496  1.67        pk 	if (prom_getprop(node, "interrupts", sizeof(int), np, &ipl) == 0) {
    497  1.51   thorpej 		struct openprom_intr *ip;
    498  1.22       mrg 		int pri;
    499  1.22       mrg 
    500  1.10       eeh 		/* Default to interrupt level 2 -- otherwise unused */
    501  1.22       mrg 		pri = INTLEVENCODE(2);
    502  1.22       mrg 
    503  1.22       mrg 		/* Change format to an `struct sbus_intr' array */
    504  1.51   thorpej 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
    505  1.51   thorpej 		    M_NOWAIT);
    506   1.3       eeh 		if (ip == NULL)
    507   1.3       eeh 			return (ENOMEM);
    508  1.22       mrg 
    509  1.22       mrg 		/*
    510  1.22       mrg 		 * Now things get ugly.  We need to take this value which is
    511   1.1       eeh 		 * the interrupt vector number and encode the IPL into it
    512   1.1       eeh 		 * somehow. Luckily, the interrupt vector has lots of free
    513  1.22       mrg 		 * space and we can easily stuff the IPL in there for a while.
    514   1.1       eeh 		 */
    515  1.67        pk 		prom_getpropstringA(node, "device_type", buf, sizeof buf);
    516  1.66        pk 		if (buf[0] == '\0')
    517  1.67        pk 			prom_getpropstringA(node, "name", buf, sizeof buf);
    518  1.22       mrg 
    519  1.22       mrg 		for (i = 0; intrmap[i].in_class; i++)
    520   1.3       eeh 			if (strcmp(intrmap[i].in_class, buf) == 0) {
    521   1.3       eeh 				pri = INTLEVENCODE(intrmap[i].in_lev);
    522   1.1       eeh 				break;
    523   1.1       eeh 			}
    524  1.22       mrg 
    525  1.22       mrg 		/*
    526  1.22       mrg 		 * Sbus card devices need the slot number encoded into
    527  1.22       mrg 		 * the vector as this is generally not done.
    528  1.22       mrg 		 */
    529  1.22       mrg 		if ((ipl[0] & INTMAP_OBIO) == 0)
    530  1.22       mrg 			pri |= slot << 3;
    531  1.22       mrg 
    532   1.3       eeh 		for (n = 0; n < *np; n++) {
    533   1.3       eeh 			/*
    534   1.3       eeh 			 * We encode vector and priority into sbi_pri so we
    535   1.3       eeh 			 * can pass them as a unit.  This will go away if
    536   1.3       eeh 			 * sbus_establish ever takes an sbus_intr instead
    537   1.3       eeh 			 * of an integer level.
    538   1.3       eeh 			 * Stuff the real vector in sbi_vec.
    539   1.3       eeh 			 */
    540  1.22       mrg 
    541  1.51   thorpej 			ip[n].oi_pri = pri|ipl[n];
    542  1.51   thorpej 			ip[n].oi_vec = ipl[n];
    543   1.3       eeh 		}
    544   1.1       eeh 		free(ipl, M_DEVBUF);
    545   1.3       eeh 		*ipp = ip;
    546   1.1       eeh 	}
    547   1.1       eeh 
    548  1.22       mrg 	return (0);
    549   1.1       eeh }
    550   1.1       eeh 
    551   1.1       eeh 
    552   1.1       eeh /*
    553   1.1       eeh  * Install an interrupt handler for an Sbus device.
    554   1.1       eeh  */
    555   1.1       eeh void *
    556  1.75       cdi sbus_intr_establish(bus_space_tag_t t, int pri, int level,
    557  1.75       cdi 	int (*handler)(void *), void *arg, void (*fastvec)(void))
    558   1.1       eeh {
    559   1.1       eeh 	struct sbus_softc *sc = t->cookie;
    560   1.1       eeh 	struct intrhand *ih;
    561   1.1       eeh 	int ipl;
    562  1.35        pk 	long vec = pri;
    563   1.1       eeh 
    564   1.1       eeh 	ih = (struct intrhand *)
    565   1.1       eeh 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    566   1.1       eeh 	if (ih == NULL)
    567   1.1       eeh 		return (NULL);
    568   1.1       eeh 
    569  1.56        pk 	if ((vec & SBUS_INTR_COMPAT) != 0)
    570   1.8       eeh 		ipl = vec & ~SBUS_INTR_COMPAT;
    571   1.1       eeh 	else {
    572   1.1       eeh 		/* Decode and remove IPL */
    573   1.8       eeh 		ipl = INTLEV(vec);
    574   1.8       eeh 		vec = INTVEC(vec);
    575  1.27       mrg 		DPRINTF(SDB_INTR,
    576  1.27       mrg 		    ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
    577  1.39       mrg 		    (long)ipl, (long)vec, (u_long)intrlev[vec]));
    578   1.8       eeh 		if ((vec & INTMAP_OBIO) == 0) {
    579   1.1       eeh 			/* We're in an SBUS slot */
    580   1.1       eeh 			/* Register the map and clear intr registers */
    581  1.22       mrg 
    582  1.35        pk 			int slot = INTSLOT(pri);
    583  1.22       mrg 
    584  1.22       mrg 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
    585  1.22       mrg 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
    586   1.1       eeh #ifdef DEBUG
    587  1.27       mrg 			if (sbus_debug & SDB_INTR) {
    588  1.72  christos 				int64_t imap = *ih->ih_map;
    589   1.1       eeh 
    590  1.36       mrg 				printf("SBUS %lx IRQ as %llx in slot %d\n",
    591  1.72  christos 				       (long)vec, (long long)imap, slot);
    592  1.36       mrg 				printf("\tmap addr %p clr addr %p\n",
    593  1.36       mrg 				    ih->ih_map, ih->ih_clr);
    594   1.1       eeh 			}
    595   1.1       eeh #endif
    596   1.1       eeh 			/* Enable the interrupt */
    597  1.63    petrov 			vec |= INTMAP_V | sc->sc_ign |
    598  1.63    petrov 				(CPU_UPAID << INTMAP_TID_SHIFT);
    599  1.48       eeh 			*(ih->ih_map) = vec;
    600   1.1       eeh 		} else {
    601   1.1       eeh 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    602  1.72  christos 			int64_t imap = 0;
    603   1.1       eeh 			int i;
    604   1.1       eeh 
    605   1.1       eeh 			/* Insert IGN */
    606   1.8       eeh 			vec |= sc->sc_ign;
    607  1.22       mrg 			for (i = 0; &intrptr[i] <=
    608  1.22       mrg 			    (int64_t *)&sc->sc_sysio->reserved_int_map &&
    609  1.72  christos 			    INTVEC(imap = intrptr[i]) != INTVEC(vec); i++)
    610  1.22       mrg 				;
    611  1.72  christos 			if (INTVEC(imap) == INTVEC(vec)) {
    612  1.27       mrg 				DPRINTF(SDB_INTR,
    613  1.36       mrg 				    ("OBIO %lx IRQ as %lx in slot %d\n",
    614  1.72  christos 				    vec, (long)imap, i));
    615   1.1       eeh 				/* Register the map and clear intr registers */
    616   1.1       eeh 				ih->ih_map = &intrptr[i];
    617   1.1       eeh 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    618   1.1       eeh 				ih->ih_clr = &intrptr[i];
    619   1.1       eeh 				/* Enable the interrupt */
    620  1.74    martin 				imap |= INTMAP_V
    621  1.74    martin 				    |(CPU_UPAID << INTMAP_TID_SHIFT);
    622  1.48       eeh 				/* XXXX */
    623  1.72  christos 				*(ih->ih_map) = imap;
    624  1.27       mrg 			} else
    625  1.27       mrg 				panic("IRQ not found!");
    626   1.1       eeh 		}
    627   1.1       eeh 	}
    628   1.1       eeh #ifdef DEBUG
    629  1.27       mrg 	if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
    630   1.1       eeh #endif
    631   1.1       eeh 
    632   1.1       eeh 	ih->ih_fun = handler;
    633   1.1       eeh 	ih->ih_arg = arg;
    634   1.8       eeh 	ih->ih_number = vec;
    635   1.1       eeh 	ih->ih_pil = (1<<ipl);
    636  1.80    martin 	intr_establish(ipl, level != IPL_VM, ih);
    637   1.1       eeh 	return (ih);
    638   1.1       eeh }
    639   1.1       eeh 
    640   1.1       eeh static bus_dma_tag_t
    641  1.75       cdi sbus_alloc_dmatag(struct sbus_softc *sc)
    642   1.1       eeh {
    643   1.1       eeh 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    644   1.1       eeh 
    645   1.1       eeh 	sdt = (bus_dma_tag_t)
    646   1.1       eeh 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    647   1.1       eeh 	if (sdt == NULL)
    648   1.1       eeh 		/* Panic? */
    649   1.1       eeh 		return (psdt);
    650   1.1       eeh 
    651   1.1       eeh 	sdt->_cookie = sc;
    652   1.1       eeh 	sdt->_parent = psdt;
    653   1.1       eeh #define PCOPY(x)	sdt->x = psdt->x
    654  1.83  nakayama 	sdt->_dmamap_create = sbus_dmamap_create;
    655   1.1       eeh 	PCOPY(_dmamap_destroy);
    656  1.83  nakayama 	sdt->_dmamap_load = iommu_dvmamap_load;
    657   1.1       eeh 	PCOPY(_dmamap_load_mbuf);
    658   1.1       eeh 	PCOPY(_dmamap_load_uio);
    659  1.83  nakayama 	sdt->_dmamap_load_raw = iommu_dvmamap_load_raw;
    660  1.83  nakayama 	sdt->_dmamap_unload = iommu_dvmamap_unload;
    661  1.83  nakayama 	sdt->_dmamap_sync = iommu_dvmamap_sync;
    662  1.83  nakayama 	sdt->_dmamem_alloc = iommu_dvmamem_alloc;
    663  1.83  nakayama 	sdt->_dmamem_free = iommu_dvmamem_free;
    664  1.83  nakayama 	sdt->_dmamem_map = iommu_dvmamem_map;
    665  1.83  nakayama 	sdt->_dmamem_unmap = iommu_dvmamem_unmap;
    666   1.1       eeh 	PCOPY(_dmamem_mmap);
    667   1.1       eeh #undef	PCOPY
    668   1.1       eeh 	sc->sc_dmatag = sdt;
    669   1.1       eeh 	return (sdt);
    670   1.1       eeh }
    671   1.1       eeh 
    672  1.83  nakayama static int
    673  1.83  nakayama sbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    674  1.83  nakayama 	bus_size_t maxsegsz, bus_size_t boundary, int flags,
    675  1.83  nakayama 	bus_dmamap_t *dmamp)
    676   1.1       eeh {
    677  1.83  nakayama 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    678  1.83  nakayama 	int error;
    679  1.28       mrg 
    680  1.83  nakayama 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
    681  1.83  nakayama 				  boundary, flags, dmamp);
    682  1.83  nakayama 	if (error == 0)
    683  1.83  nakayama 		(*dmamp)->_dm_cookie = &sc->sc_sb;
    684  1.83  nakayama 	return error;
    685   1.2       eeh }
    686