Home | History | Annotate | Line # | Download | only in dev
      1  1.106   thorpej /*	$NetBSD: sbus.c,v 1.106 2023/12/02 21:02:53 thorpej 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.106   thorpej __KERNEL_RCSID(0, "$NetBSD: sbus.c,v 1.106 2023/12/02 21:02:53 thorpej 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.1       eeh #include <sys/malloc.h>
     43  1.100   thorpej #include <sys/kmem.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.105   thorpej #include <sys/vmem.h>
     48    1.1       eeh 
     49   1.88    dyoung #include <sys/bus.h>
     50   1.50       eeh #include <machine/openfirm.h>
     51   1.50       eeh 
     52   1.13       mrg #include <sparc64/dev/iommureg.h>
     53   1.17       mrg #include <sparc64/dev/iommuvar.h>
     54    1.1       eeh #include <sparc64/dev/sbusreg.h>
     55    1.7        pk #include <dev/sbus/sbusvar.h>
     56    1.1       eeh 
     57   1.59   thorpej #include <uvm/uvm_extern.h>
     58   1.44       eeh 
     59    1.1       eeh #include <machine/autoconf.h>
     60    1.1       eeh #include <machine/cpu.h>
     61    1.8       eeh #include <machine/sparc64.h>
     62    1.1       eeh 
     63    1.1       eeh #ifdef DEBUG
     64    1.1       eeh #define SDB_DVMA	0x1
     65    1.1       eeh #define SDB_INTR	0x2
     66   1.27       mrg int sbus_debug = 0;
     67   1.27       mrg #define DPRINTF(l, s)   do { if (sbus_debug & l) printf s; } while (0)
     68   1.27       mrg #else
     69   1.27       mrg #define DPRINTF(l, s)
     70    1.1       eeh #endif
     71    1.1       eeh 
     72   1.75       cdi void sbusreset(int);
     73    1.1       eeh 
     74   1.75       cdi static bus_dma_tag_t sbus_alloc_dmatag(struct sbus_softc *);
     75   1.75       cdi static int sbus_get_intr(struct sbus_softc *, int, struct openprom_intr **,
     76   1.75       cdi 	int *, int);
     77   1.75       cdi static int sbus_overtemp(void *);
     78   1.75       cdi static int _sbus_bus_map(
     79    1.1       eeh 		bus_space_tag_t,
     80    1.1       eeh 		bus_addr_t,		/*offset*/
     81    1.1       eeh 		bus_size_t,		/*size*/
     82    1.1       eeh 		int,			/*flags*/
     83   1.90       mrg 		vaddr_t,		/* XXX unused -- compat w/sparc */
     84   1.75       cdi 		bus_space_handle_t *);
     85   1.75       cdi static void *sbus_intr_establish(
     86    1.1       eeh 		bus_space_tag_t,
     87   1.75       cdi 		int,			/*`device class' priority*/
     88   1.35        pk 		int,			/*Sbus interrupt level*/
     89   1.75       cdi 		int (*)(void *),	/*handler*/
     90   1.56        pk 		void *,			/*handler arg*/
     91   1.75       cdi 		void (*)(void));	/*optional fast trap*/
     92    1.1       eeh 
     93    1.1       eeh 
     94    1.1       eeh /* autoconfiguration driver */
     95   1.84   tsutsui int	sbus_match(device_t, cfdata_t, void *);
     96   1.84   tsutsui void	sbus_attach(device_t, device_t, void *);
     97    1.1       eeh 
     98    1.1       eeh 
     99   1.84   tsutsui CFATTACH_DECL_NEW(sbus, sizeof(struct sbus_softc),
    100   1.55   thorpej     sbus_match, sbus_attach, NULL, NULL);
    101    1.1       eeh 
    102    1.1       eeh extern struct cfdriver sbus_cd;
    103    1.1       eeh 
    104    1.1       eeh /*
    105    1.1       eeh  * DVMA routines
    106    1.1       eeh  */
    107   1.83  nakayama static int sbus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
    108   1.83  nakayama 	bus_size_t, int, bus_dmamap_t *);
    109    1.1       eeh 
    110    1.1       eeh /*
    111    1.1       eeh  * Child devices receive the Sbus interrupt level in their attach
    112    1.1       eeh  * arguments. We translate these to CPU IPLs using the following
    113    1.1       eeh  * tables. Note: obio bus interrupt levels are identical to the
    114    1.1       eeh  * processor IPL.
    115    1.1       eeh  *
    116    1.1       eeh  * The second set of tables is used when the Sbus interrupt level
    117    1.1       eeh  * cannot be had from the PROM as an `interrupt' property. We then
    118    1.1       eeh  * fall back on the `intr' property which contains the CPU IPL.
    119    1.1       eeh  */
    120    1.1       eeh 
    121    1.1       eeh /*
    122    1.1       eeh  * This value is or'ed into the attach args' interrupt level cookie
    123    1.1       eeh  * if the interrupt level comes from an `intr' property, i.e. it is
    124    1.1       eeh  * not an Sbus interrupt level.
    125    1.1       eeh  */
    126    1.1       eeh #define SBUS_INTR_COMPAT	0x80000000
    127    1.1       eeh 
    128    1.1       eeh 
    129    1.1       eeh /*
    130    1.1       eeh  * Print the location of some sbus-attached device (called just
    131    1.1       eeh  * before attaching that device).  If `sbus' is not NULL, the
    132    1.1       eeh  * device was found but not configured; print the sbus as well.
    133    1.1       eeh  * Return UNCONF (config_find ignores this if the device was configured).
    134    1.1       eeh  */
    135    1.1       eeh int
    136   1.76       cdi sbus_print(void *args, const char *busname)
    137    1.1       eeh {
    138    1.1       eeh 	struct sbus_attach_args *sa = args;
    139    1.3       eeh 	int i;
    140    1.1       eeh 
    141    1.1       eeh 	if (busname)
    142   1.58   thorpej 		aprint_normal("%s at %s", sa->sa_name, busname);
    143   1.58   thorpej 	aprint_normal(" slot %ld offset 0x%lx", (long)sa->sa_slot,
    144    1.8       eeh 	       (u_long)sa->sa_offset);
    145   1.22       mrg 	for (i = 0; i < sa->sa_nintr; i++) {
    146   1.51   thorpej 		struct openprom_intr *sbi = &sa->sa_intr[i];
    147    1.1       eeh 
    148   1.58   thorpej 		aprint_normal(" vector %lx ipl %ld",
    149   1.51   thorpej 		       (u_long)sbi->oi_vec,
    150   1.51   thorpej 		       (long)INTLEV(sbi->oi_pri));
    151    1.1       eeh 	}
    152    1.1       eeh 	return (UNCONF);
    153    1.1       eeh }
    154    1.1       eeh 
    155    1.1       eeh int
    156   1.84   tsutsui sbus_match(device_t parent, cfdata_t cf, void *aux)
    157    1.1       eeh {
    158    1.1       eeh 	struct mainbus_attach_args *ma = aux;
    159    1.1       eeh 
    160   1.52   thorpej 	return (strcmp(cf->cf_name, ma->ma_name) == 0);
    161    1.1       eeh }
    162    1.1       eeh 
    163    1.1       eeh /*
    164    1.1       eeh  * Attach an Sbus.
    165    1.1       eeh  */
    166    1.1       eeh void
    167   1.84   tsutsui sbus_attach(device_t parent, device_t self, void *aux)
    168    1.1       eeh {
    169   1.81    cegger 	struct sbus_softc *sc = device_private(self);
    170    1.1       eeh 	struct mainbus_attach_args *ma = aux;
    171   1.40       eeh 	struct intrhand *ih;
    172   1.40       eeh 	int ipl;
    173   1.27       mrg 	char *name;
    174    1.1       eeh 	int node = ma->ma_node;
    175    1.1       eeh 	int node0, error;
    176    1.1       eeh 	bus_space_tag_t sbt;
    177    1.1       eeh 	struct sbus_attach_args sa;
    178    1.1       eeh 
    179   1.84   tsutsui 	sc->sc_dev = self;
    180    1.1       eeh 	sc->sc_bustag = ma->ma_bustag;
    181    1.1       eeh 	sc->sc_dmatag = ma->ma_dmatag;
    182   1.95   msaitoh 	sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;
    183    1.1       eeh 
    184   1.48       eeh 	/* XXXX Use sysio PROM mappings for interrupt vector regs. */
    185   1.48       eeh 	sparc_promaddr_to_handle(sc->sc_bustag,	ma->ma_address[0], &sc->sc_bh);
    186   1.48       eeh 	sc->sc_sysio = (struct sysioreg *)bus_space_vaddr(sc->sc_bustag,
    187   1.49       eeh 		sc->sc_bh);
    188   1.48       eeh 
    189   1.48       eeh #ifdef _LP64
    190   1.48       eeh 	/*
    191   1.48       eeh 	 * 32-bit kernels use virtual addresses for bus space operations
    192   1.48       eeh 	 * so we may as well use the prom VA.
    193   1.48       eeh 	 *
    194   1.48       eeh 	 * 64-bit kernels use physical addresses for bus space operations
    195   1.48       eeh 	 * so mapping this in again will reduce TLB thrashing.
    196   1.48       eeh 	 */
    197   1.48       eeh 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr,
    198   1.48       eeh 		ma->ma_reg[0].ur_len, 0, &sc->sc_bh) != 0) {
    199   1.79    cegger 		aprint_error_dev(self, "cannot map registers\n");
    200   1.48       eeh 		return;
    201   1.48       eeh 	}
    202   1.48       eeh #endif
    203    1.1       eeh 
    204    1.1       eeh 	/*
    205    1.1       eeh 	 * Record clock frequency for synchronous SCSI.
    206    1.1       eeh 	 * IS THIS THE CORRECT DEFAULT??
    207    1.1       eeh 	 */
    208   1.67        pk 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency",
    209   1.48       eeh 		25*1000*1000);
    210    1.1       eeh 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    211    1.1       eeh 
    212   1.69        pk 	sbt = bus_space_tag_alloc(sc->sc_bustag, sc);
    213   1.69        pk 	sbt->type = SBUS_BUS_SPACE;
    214   1.69        pk 	sbt->sparc_bus_map = _sbus_bus_map;
    215   1.69        pk 	sbt->sparc_intr_establish = sbus_intr_establish;
    216   1.69        pk 
    217    1.1       eeh 	sc->sc_dmatag = sbus_alloc_dmatag(sc);
    218    1.1       eeh 
    219    1.1       eeh 	/*
    220    1.1       eeh 	 * Get the SBus burst transfer size if burst transfers are supported
    221    1.1       eeh 	 */
    222   1.67        pk 	sc->sc_burst = prom_getpropint(node, "burst-sizes", 0);
    223    1.1       eeh 
    224    1.1       eeh 	/*
    225    1.1       eeh 	 * Collect address translations from the OBP.
    226    1.1       eeh 	 */
    227   1.67        pk 	error = prom_getprop(node, "ranges", sizeof(struct openprom_range),
    228   1.69        pk 			 &sbt->nranges, &sbt->ranges);
    229   1.16       eeh 	if (error)
    230   1.84   tsutsui 		panic("%s: error getting ranges property", device_xname(self));
    231    1.1       eeh 
    232   1.48       eeh 	/* initialize the IOMMU */
    233   1.17       mrg 
    234   1.17       mrg 	/* punch in our copies */
    235   1.17       mrg 	sc->sc_is.is_bustag = sc->sc_bustag;
    236   1.48       eeh 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    237   1.48       eeh 		(vaddr_t)&((struct sysioreg *)NULL)->sys_iommu,
    238   1.87       mrg 		sizeof (struct iommureg), &sc->sc_is.is_iommu);
    239   1.50       eeh 
    240   1.50       eeh 	/* initialize our strbuf_ctl */
    241   1.50       eeh 	sc->sc_is.is_sb[0] = &sc->sc_sb;
    242   1.50       eeh 	sc->sc_sb.sb_is = &sc->sc_is;
    243   1.48       eeh 	bus_space_subregion(sc->sc_bustag, sc->sc_bh,
    244   1.48       eeh 		(vaddr_t)&((struct sysioreg *)NULL)->sys_strbuf,
    245   1.50       eeh 		sizeof (struct iommu_strbuf), &sc->sc_sb.sb_sb);
    246   1.50       eeh 	/* Point sb_flush to our flush buffer. */
    247   1.50       eeh 	sc->sc_sb.sb_flush = &sc->sc_flush;
    248   1.16       eeh 
    249   1.27       mrg 	/* give us a nice name.. */
    250  1.100   thorpej 	name = kmem_asprintf("%s dvma", device_xname(self));
    251   1.27       mrg 
    252   1.43       eeh 	iommu_init(name, &sc->sc_is, 0, -1);
    253   1.12       eeh 
    254   1.40       eeh 	/* Enable the over temp intr */
    255   1.94     palle 	ih = intrhand_alloc();
    256   1.40       eeh 	ih->ih_map = &sc->sc_sysio->therm_int_map;
    257   1.40       eeh 	ih->ih_clr = NULL; /* &sc->sc_sysio->therm_clr_int; */
    258   1.40       eeh 	ih->ih_fun = sbus_overtemp;
    259   1.40       eeh 	ipl = 1;
    260   1.92  nakayama 	ih->ih_pil = ipl;
    261   1.40       eeh 	ih->ih_number = INTVEC(*(ih->ih_map));
    262   1.91       mrg 	ih->ih_pending = 0;
    263   1.80    martin 	intr_establish(ipl, true, ih);
    264   1.74    martin 	*(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
    265   1.40       eeh 
    266   1.42       mrg 	/*
    267   1.42       mrg 	 * Note: the stupid SBUS IOMMU ignores the high bits of an address, so a
    268   1.42       mrg 	 * NULL DMA pointer will be translated by the first page of the IOTSB.
    269   1.42       mrg 	 * To avoid bugs we'll alloc and ignore the first entry in the IOTSB.
    270   1.42       mrg 	 */
    271  1.106   thorpej 	if (vmem_xalloc_addr(sc->sc_is.is_dvmamap, sc->sc_is.is_dvmabase,
    272  1.106   thorpej 			    PAGE_SIZE, VM_NOSLEEP) != 0) {
    273  1.106   thorpej 		panic("sbus iommu: can't toss first dvma page");
    274   1.42       mrg 	}
    275   1.42       mrg 
    276   1.12       eeh 	/*
    277    1.1       eeh 	 * Loop through ROM children, fixing any relative addresses
    278    1.1       eeh 	 * and then configuring each device.
    279    1.1       eeh 	 * `specials' is an array of device names that are treated
    280    1.1       eeh 	 * specially:
    281    1.1       eeh 	 */
    282  1.104   thorpej 	devhandle_t selfh = device_handle(self);
    283   1.50       eeh 	node0 = OF_child(node);
    284   1.50       eeh 	for (node = node0; node; node = OF_peer(node)) {
    285   1.72  christos 		char *name1 = prom_getpropstring(node, "name");
    286    1.1       eeh 
    287    1.1       eeh 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    288   1.23        pk 					   node, &sa) != 0) {
    289   1.72  christos 			printf("sbus_attach: %s: incomplete\n", name1);
    290    1.1       eeh 			continue;
    291    1.1       eeh 		}
    292  1.102   thorpej 		(void) config_found(self, &sa, sbus_print,
    293  1.104   thorpej 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
    294    1.3       eeh 		sbus_destroy_attach_args(&sa);
    295    1.1       eeh 	}
    296    1.1       eeh }
    297    1.1       eeh 
    298    1.1       eeh int
    299   1.76       cdi sbus_setup_attach_args(struct sbus_softc *sc, bus_space_tag_t bustag,
    300   1.76       cdi 	bus_dma_tag_t dmatag, int node, struct sbus_attach_args	*sa)
    301    1.1       eeh {
    302   1.51   thorpej 	/*struct	openprom_addr sbusreg;*/
    303    1.3       eeh 	/*int	base;*/
    304    1.1       eeh 	int	error;
    305    1.3       eeh 	int n;
    306    1.1       eeh 
    307   1.65    martin 	memset(sa, 0, sizeof(struct sbus_attach_args));
    308   1.68    martin 	n = 0;
    309   1.67        pk 	error = prom_getprop(node, "name", 1, &n, &sa->sa_name);
    310    1.3       eeh 	if (error != 0)
    311    1.3       eeh 		return (error);
    312   1.93       mrg 	KASSERT(sa->sa_name[n-1] == '\0');
    313    1.3       eeh 
    314    1.1       eeh 	sa->sa_bustag = bustag;
    315    1.1       eeh 	sa->sa_dmatag = dmatag;
    316    1.1       eeh 	sa->sa_node = node;
    317   1.37       eeh 	sa->sa_frequency = sc->sc_clockfreq;
    318    1.1       eeh 
    319   1.67        pk 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
    320   1.62       mrg 			 &sa->sa_nreg, &sa->sa_reg);
    321    1.3       eeh 	if (error != 0) {
    322    1.3       eeh 		char buf[32];
    323    1.3       eeh 		if (error != ENOENT ||
    324    1.3       eeh 		    !node_has_property(node, "device_type") ||
    325   1.67        pk 		    strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
    326    1.3       eeh 			   "hierarchical") != 0)
    327    1.3       eeh 			return (error);
    328    1.3       eeh 	}
    329    1.3       eeh 	for (n = 0; n < sa->sa_nreg; n++) {
    330    1.3       eeh 		/* Convert to relative addressing, if necessary */
    331   1.76       cdi 		uint32_t base = sa->sa_reg[n].oa_base;
    332    1.3       eeh 		if (SBUS_ABS(base)) {
    333   1.51   thorpej 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
    334   1.51   thorpej 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
    335    1.3       eeh 		}
    336    1.1       eeh 	}
    337    1.1       eeh 
    338   1.22       mrg 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr,
    339   1.22       mrg 	    sa->sa_slot)) != 0)
    340    1.1       eeh 		return (error);
    341    1.1       eeh 
    342   1.76       cdi 	error = prom_getprop(node, "address", sizeof(uint32_t),
    343   1.62       mrg 			 &sa->sa_npromvaddrs, &sa->sa_promvaddrs);
    344    1.3       eeh 	if (error != 0 && error != ENOENT)
    345    1.1       eeh 		return (error);
    346    1.1       eeh 
    347    1.1       eeh 	return (0);
    348    1.1       eeh }
    349    1.1       eeh 
    350    1.3       eeh void
    351   1.76       cdi sbus_destroy_attach_args(struct sbus_attach_args *sa)
    352    1.3       eeh {
    353    1.3       eeh 	if (sa->sa_name != NULL)
    354    1.3       eeh 		free(sa->sa_name, M_DEVBUF);
    355    1.3       eeh 
    356    1.3       eeh 	if (sa->sa_nreg != 0)
    357    1.3       eeh 		free(sa->sa_reg, M_DEVBUF);
    358    1.3       eeh 
    359    1.3       eeh 	if (sa->sa_intr)
    360    1.3       eeh 		free(sa->sa_intr, M_DEVBUF);
    361    1.3       eeh 
    362    1.3       eeh 	if (sa->sa_promvaddrs)
    363    1.8       eeh 		free((void *)sa->sa_promvaddrs, M_DEVBUF);
    364    1.3       eeh 
    365   1.65    martin 	memset(sa, 0, sizeof(struct sbus_attach_args)); /*DEBUG*/
    366    1.3       eeh }
    367    1.3       eeh 
    368    1.3       eeh 
    369    1.1       eeh int
    370   1.75       cdi _sbus_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags,
    371   1.75       cdi 	vaddr_t v, bus_space_handle_t *hp)
    372    1.1       eeh {
    373   1.69        pk 	int error;
    374    1.1       eeh 
    375   1.70        pk 	if (t->ranges != NULL) {
    376   1.70        pk 		if ((error = bus_space_translate_address_generic(
    377   1.70        pk 				t->ranges, t->nranges, &addr)) != 0)
    378   1.70        pk 			return (error);
    379   1.70        pk 	}
    380    1.1       eeh 
    381   1.96  macallan 	/*
    382   1.96  macallan 	 * BUS_SPACE_MAP_PREFETCHABLE doesn't work right through sbus, so weed
    383   1.96  macallan 	 * it out for now until we know better
    384   1.96  macallan 	 */
    385   1.96  macallan 
    386   1.96  macallan 	flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
    387   1.96  macallan 
    388   1.69        pk 	return (bus_space_map(t->parent, addr, size, flags, hp));
    389    1.1       eeh }
    390    1.1       eeh 
    391   1.44       eeh 
    392   1.44       eeh bus_addr_t
    393   1.76       cdi sbus_bus_addr(bus_space_tag_t t, u_int btype, u_int offset)
    394   1.44       eeh {
    395   1.44       eeh 	int slot = btype;
    396   1.69        pk 	struct openprom_range *rp;
    397   1.44       eeh 	int i;
    398   1.44       eeh 
    399   1.69        pk 	for (i = 0; i < t->nranges; i++) {
    400   1.69        pk 		rp = &t->ranges[i];
    401   1.69        pk 		if (rp->or_child_space != slot)
    402   1.44       eeh 			continue;
    403   1.44       eeh 
    404   1.71       chs 		return BUS_ADDR(rp->or_parent_space,
    405   1.71       chs 				rp->or_parent_base + offset);
    406    1.1       eeh 	}
    407    1.1       eeh 
    408   1.69        pk 	return (0);
    409    1.1       eeh }
    410    1.1       eeh 
    411    1.1       eeh 
    412    1.1       eeh /*
    413   1.40       eeh  * Handle an overtemp situation.
    414   1.41   hubertf  *
    415   1.41   hubertf  * SPARCs have temperature sensors which generate interrupts
    416   1.41   hubertf  * if the machine's temperature exceeds a certain threshold.
    417   1.41   hubertf  * This handles the interrupt and powers off the machine.
    418   1.41   hubertf  * The same needs to be done to PCI controller drivers.
    419   1.40       eeh  */
    420   1.40       eeh int
    421   1.75       cdi sbus_overtemp(void *arg)
    422   1.40       eeh {
    423   1.40       eeh 	/* Should try a clean shutdown first */
    424   1.41   hubertf 	printf("DANGER: OVER TEMPERATURE detected\nShutting down...\n");
    425   1.40       eeh 	delay(20);
    426   1.98   thorpej 	kern_reboot(RB_POWERDOWN|RB_HALT, NULL);
    427    1.1       eeh }
    428    1.1       eeh 
    429    1.1       eeh /*
    430    1.1       eeh  * Get interrupt attributes for an Sbus device.
    431    1.1       eeh  */
    432    1.1       eeh int
    433   1.75       cdi sbus_get_intr(struct sbus_softc *sc, int node, struct openprom_intr **ipp,
    434   1.75       cdi 	int *np, int slot)
    435    1.1       eeh {
    436    1.1       eeh 	int *ipl;
    437   1.22       mrg 	int n, i;
    438    1.1       eeh 	char buf[32];
    439    1.1       eeh 
    440    1.1       eeh 	/*
    441    1.1       eeh 	 * The `interrupts' property contains the Sbus interrupt level.
    442    1.1       eeh 	 */
    443    1.1       eeh 	ipl = NULL;
    444   1.67        pk 	if (prom_getprop(node, "interrupts", sizeof(int), np, &ipl) == 0) {
    445   1.51   thorpej 		struct openprom_intr *ip;
    446   1.22       mrg 		int pri;
    447   1.22       mrg 
    448   1.10       eeh 		/* Default to interrupt level 2 -- otherwise unused */
    449   1.22       mrg 		pri = INTLEVENCODE(2);
    450   1.22       mrg 
    451   1.22       mrg 		/* Change format to an `struct sbus_intr' array */
    452   1.51   thorpej 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
    453   1.97       chs 		    M_WAITOK);
    454   1.22       mrg 
    455   1.22       mrg 		/*
    456   1.22       mrg 		 * Now things get ugly.  We need to take this value which is
    457    1.1       eeh 		 * the interrupt vector number and encode the IPL into it
    458    1.1       eeh 		 * somehow. Luckily, the interrupt vector has lots of free
    459   1.22       mrg 		 * space and we can easily stuff the IPL in there for a while.
    460    1.1       eeh 		 */
    461   1.67        pk 		prom_getpropstringA(node, "device_type", buf, sizeof buf);
    462   1.66        pk 		if (buf[0] == '\0')
    463   1.67        pk 			prom_getpropstringA(node, "name", buf, sizeof buf);
    464   1.22       mrg 
    465   1.22       mrg 		for (i = 0; intrmap[i].in_class; i++)
    466    1.3       eeh 			if (strcmp(intrmap[i].in_class, buf) == 0) {
    467    1.3       eeh 				pri = INTLEVENCODE(intrmap[i].in_lev);
    468    1.1       eeh 				break;
    469    1.1       eeh 			}
    470   1.22       mrg 
    471   1.22       mrg 		/*
    472   1.22       mrg 		 * Sbus card devices need the slot number encoded into
    473   1.22       mrg 		 * the vector as this is generally not done.
    474   1.22       mrg 		 */
    475   1.22       mrg 		if ((ipl[0] & INTMAP_OBIO) == 0)
    476   1.22       mrg 			pri |= slot << 3;
    477   1.22       mrg 
    478    1.3       eeh 		for (n = 0; n < *np; n++) {
    479    1.3       eeh 			/*
    480    1.3       eeh 			 * We encode vector and priority into sbi_pri so we
    481    1.3       eeh 			 * can pass them as a unit.  This will go away if
    482    1.3       eeh 			 * sbus_establish ever takes an sbus_intr instead
    483    1.3       eeh 			 * of an integer level.
    484    1.3       eeh 			 * Stuff the real vector in sbi_vec.
    485    1.3       eeh 			 */
    486   1.22       mrg 
    487   1.51   thorpej 			ip[n].oi_pri = pri|ipl[n];
    488   1.51   thorpej 			ip[n].oi_vec = ipl[n];
    489    1.3       eeh 		}
    490    1.1       eeh 		free(ipl, M_DEVBUF);
    491    1.3       eeh 		*ipp = ip;
    492    1.1       eeh 	}
    493    1.1       eeh 
    494   1.22       mrg 	return (0);
    495    1.1       eeh }
    496    1.1       eeh 
    497    1.1       eeh 
    498    1.1       eeh /*
    499    1.1       eeh  * Install an interrupt handler for an Sbus device.
    500    1.1       eeh  */
    501    1.1       eeh void *
    502   1.75       cdi sbus_intr_establish(bus_space_tag_t t, int pri, int level,
    503   1.75       cdi 	int (*handler)(void *), void *arg, void (*fastvec)(void))
    504    1.1       eeh {
    505    1.1       eeh 	struct sbus_softc *sc = t->cookie;
    506    1.1       eeh 	struct intrhand *ih;
    507    1.1       eeh 	int ipl;
    508   1.95   msaitoh 	long vec = pri;
    509    1.1       eeh 
    510   1.94     palle 	ih = intrhand_alloc();
    511    1.1       eeh 
    512   1.56        pk 	if ((vec & SBUS_INTR_COMPAT) != 0)
    513    1.8       eeh 		ipl = vec & ~SBUS_INTR_COMPAT;
    514    1.1       eeh 	else {
    515    1.1       eeh 		/* Decode and remove IPL */
    516    1.8       eeh 		ipl = INTLEV(vec);
    517    1.8       eeh 		vec = INTVEC(vec);
    518   1.27       mrg 		DPRINTF(SDB_INTR,
    519   1.27       mrg 		    ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
    520   1.39       mrg 		    (long)ipl, (long)vec, (u_long)intrlev[vec]));
    521    1.8       eeh 		if ((vec & INTMAP_OBIO) == 0) {
    522    1.1       eeh 			/* We're in an SBUS slot */
    523    1.1       eeh 			/* Register the map and clear intr registers */
    524   1.22       mrg 
    525   1.35        pk 			int slot = INTSLOT(pri);
    526   1.22       mrg 
    527   1.22       mrg 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
    528   1.22       mrg 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
    529    1.1       eeh #ifdef DEBUG
    530   1.27       mrg 			if (sbus_debug & SDB_INTR) {
    531   1.72  christos 				int64_t imap = *ih->ih_map;
    532    1.1       eeh 
    533   1.36       mrg 				printf("SBUS %lx IRQ as %llx in slot %d\n",
    534   1.72  christos 				       (long)vec, (long long)imap, slot);
    535   1.36       mrg 				printf("\tmap addr %p clr addr %p\n",
    536   1.36       mrg 				    ih->ih_map, ih->ih_clr);
    537    1.1       eeh 			}
    538    1.1       eeh #endif
    539    1.1       eeh 			/* Enable the interrupt */
    540   1.63    petrov 			vec |= INTMAP_V | sc->sc_ign |
    541   1.63    petrov 				(CPU_UPAID << INTMAP_TID_SHIFT);
    542   1.48       eeh 			*(ih->ih_map) = vec;
    543    1.1       eeh 		} else {
    544    1.1       eeh 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    545   1.72  christos 			int64_t imap = 0;
    546    1.1       eeh 			int i;
    547    1.1       eeh 
    548    1.1       eeh 			/* Insert IGN */
    549    1.8       eeh 			vec |= sc->sc_ign;
    550   1.22       mrg 			for (i = 0; &intrptr[i] <=
    551   1.22       mrg 			    (int64_t *)&sc->sc_sysio->reserved_int_map &&
    552   1.72  christos 			    INTVEC(imap = intrptr[i]) != INTVEC(vec); i++)
    553   1.22       mrg 				;
    554   1.72  christos 			if (INTVEC(imap) == INTVEC(vec)) {
    555   1.27       mrg 				DPRINTF(SDB_INTR,
    556   1.36       mrg 				    ("OBIO %lx IRQ as %lx in slot %d\n",
    557   1.72  christos 				    vec, (long)imap, i));
    558    1.1       eeh 				/* Register the map and clear intr registers */
    559    1.1       eeh 				ih->ih_map = &intrptr[i];
    560    1.1       eeh 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    561    1.1       eeh 				ih->ih_clr = &intrptr[i];
    562    1.1       eeh 				/* Enable the interrupt */
    563   1.74    martin 				imap |= INTMAP_V
    564   1.74    martin 				    |(CPU_UPAID << INTMAP_TID_SHIFT);
    565   1.48       eeh 				/* XXXX */
    566   1.72  christos 				*(ih->ih_map) = imap;
    567   1.27       mrg 			} else
    568   1.27       mrg 				panic("IRQ not found!");
    569    1.1       eeh 		}
    570    1.1       eeh 	}
    571    1.1       eeh #ifdef DEBUG
    572   1.27       mrg 	if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
    573    1.1       eeh #endif
    574    1.1       eeh 
    575    1.1       eeh 	ih->ih_fun = handler;
    576    1.1       eeh 	ih->ih_arg = arg;
    577    1.8       eeh 	ih->ih_number = vec;
    578   1.89  macallan 	ih->ih_ivec = 0;
    579   1.92  nakayama 	ih->ih_pil = ipl;
    580   1.91       mrg 	ih->ih_pending = 0;
    581   1.91       mrg 
    582   1.80    martin 	intr_establish(ipl, level != IPL_VM, ih);
    583    1.1       eeh 	return (ih);
    584    1.1       eeh }
    585    1.1       eeh 
    586    1.1       eeh static bus_dma_tag_t
    587   1.75       cdi sbus_alloc_dmatag(struct sbus_softc *sc)
    588    1.1       eeh {
    589    1.1       eeh 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    590    1.1       eeh 
    591  1.100   thorpej 	sdt = kmem_alloc(sizeof(*sdt), KM_SLEEP);
    592    1.1       eeh 	sdt->_cookie = sc;
    593    1.1       eeh 	sdt->_parent = psdt;
    594    1.1       eeh #define PCOPY(x)	sdt->x = psdt->x
    595   1.83  nakayama 	sdt->_dmamap_create = sbus_dmamap_create;
    596    1.1       eeh 	PCOPY(_dmamap_destroy);
    597   1.83  nakayama 	sdt->_dmamap_load = iommu_dvmamap_load;
    598    1.1       eeh 	PCOPY(_dmamap_load_mbuf);
    599    1.1       eeh 	PCOPY(_dmamap_load_uio);
    600   1.83  nakayama 	sdt->_dmamap_load_raw = iommu_dvmamap_load_raw;
    601   1.83  nakayama 	sdt->_dmamap_unload = iommu_dvmamap_unload;
    602   1.83  nakayama 	sdt->_dmamap_sync = iommu_dvmamap_sync;
    603   1.83  nakayama 	sdt->_dmamem_alloc = iommu_dvmamem_alloc;
    604   1.83  nakayama 	sdt->_dmamem_free = iommu_dvmamem_free;
    605   1.83  nakayama 	sdt->_dmamem_map = iommu_dvmamem_map;
    606   1.83  nakayama 	sdt->_dmamem_unmap = iommu_dvmamem_unmap;
    607    1.1       eeh 	PCOPY(_dmamem_mmap);
    608    1.1       eeh #undef	PCOPY
    609    1.1       eeh 	sc->sc_dmatag = sdt;
    610    1.1       eeh 	return (sdt);
    611    1.1       eeh }
    612    1.1       eeh 
    613   1.83  nakayama static int
    614   1.83  nakayama sbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    615   1.83  nakayama 	bus_size_t maxsegsz, bus_size_t boundary, int flags,
    616   1.83  nakayama 	bus_dmamap_t *dmamp)
    617    1.1       eeh {
    618   1.84   tsutsui 	struct sbus_softc *sc = t->_cookie;
    619   1.83  nakayama 	int error;
    620   1.28       mrg 
    621   1.83  nakayama 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
    622   1.83  nakayama 				  boundary, flags, dmamp);
    623   1.83  nakayama 	if (error == 0)
    624   1.83  nakayama 		(*dmamp)->_dm_cookie = &sc->sc_sb;
    625   1.83  nakayama 	return error;
    626    1.2       eeh }
    627