Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.41
      1 /*	$NetBSD: sbus.c,v 1.41 2001/03/16 10:47:25 hubertf Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This software was developed by the Computer Systems Engineering group
     44  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     45  * contributed to Berkeley.
     46  *
     47  * All advertising materials mentioning features or use of this software
     48  * must display the following acknowledgement:
     49  *	This product includes software developed by the University of
     50  *	California, Lawrence Berkeley Laboratory.
     51  *
     52  * Redistribution and use in source and binary forms, with or without
     53  * modification, are permitted provided that the following conditions
     54  * are met:
     55  * 1. Redistributions of source code must retain the above copyright
     56  *    notice, this list of conditions and the following disclaimer.
     57  * 2. Redistributions in binary form must reproduce the above copyright
     58  *    notice, this list of conditions and the following disclaimer in the
     59  *    documentation and/or other materials provided with the distribution.
     60  * 3. All advertising materials mentioning features or use of this software
     61  *    must display the following acknowledgement:
     62  *	This product includes software developed by the University of
     63  *	California, Berkeley and its contributors.
     64  * 4. Neither the name of the University nor the names of its contributors
     65  *    may be used to endorse or promote products derived from this software
     66  *    without specific prior written permission.
     67  *
     68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     78  * SUCH DAMAGE.
     79  *
     80  *	@(#)sbus.c	8.1 (Berkeley) 6/11/93
     81  */
     82 
     83 /*
     84  * Copyright (c) 1999 Eduardo Horvath
     85  *
     86  * Redistribution and use in source and binary forms, with or without
     87  * modification, are permitted provided that the following conditions
     88  * are met:
     89  * 1. Redistributions of source code must retain the above copyright
     90  *    notice, this list of conditions and the following disclaimer.
     91  *
     92  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     93  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     95  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     96  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     97  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     98  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    100  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    102  * SUCH DAMAGE.
    103  *
    104  */
    105 
    106 
    107 /*
    108  * Sbus stuff.
    109  */
    110 #include "opt_ddb.h"
    111 
    112 #include <sys/param.h>
    113 #include <sys/extent.h>
    114 #include <sys/malloc.h>
    115 #include <sys/systm.h>
    116 #include <sys/device.h>
    117 #include <sys/reboot.h>
    118 
    119 #include <machine/bus.h>
    120 #include <sparc64/sparc64/cache.h>
    121 #include <sparc64/dev/iommureg.h>
    122 #include <sparc64/dev/iommuvar.h>
    123 #include <sparc64/dev/sbusreg.h>
    124 #include <dev/sbus/sbusvar.h>
    125 
    126 #include <machine/autoconf.h>
    127 #include <machine/cpu.h>
    128 #include <machine/sparc64.h>
    129 
    130 #ifdef DEBUG
    131 #define SDB_DVMA	0x1
    132 #define SDB_INTR	0x2
    133 int sbus_debug = 0;
    134 #define DPRINTF(l, s)   do { if (sbus_debug & l) printf s; } while (0)
    135 #else
    136 #define DPRINTF(l, s)
    137 #endif
    138 
    139 void sbusreset __P((int));
    140 
    141 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
    142 static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
    143 static int sbus_get_intr __P((struct sbus_softc *, int,
    144 			      struct sbus_intr **, int *, int));
    145 static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    146 			      int, bus_space_handle_t *));
    147 static int sbus_overtemp __P((void *));
    148 static int _sbus_bus_map __P((
    149 		bus_space_tag_t,
    150 		bus_type_t,
    151 		bus_addr_t,		/*offset*/
    152 		bus_size_t,		/*size*/
    153 		int,			/*flags*/
    154 		vaddr_t,		/*preferred virtual address */
    155 		bus_space_handle_t *));
    156 static void *sbus_intr_establish __P((
    157 		bus_space_tag_t,
    158 		int,			/*Sbus interrupt level*/
    159 		int,			/*`device class' priority*/
    160 		int,			/*flags*/
    161 		int (*) __P((void *)),	/*handler*/
    162 		void *));		/*handler arg*/
    163 
    164 
    165 /* autoconfiguration driver */
    166 int	sbus_match __P((struct device *, struct cfdata *, void *));
    167 void	sbus_attach __P((struct device *, struct device *, void *));
    168 
    169 
    170 struct cfattach sbus_ca = {
    171 	sizeof(struct sbus_softc), sbus_match, sbus_attach
    172 };
    173 
    174 extern struct cfdriver sbus_cd;
    175 
    176 /*
    177  * DVMA routines
    178  */
    179 int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    180 			  bus_size_t, struct proc *, int));
    181 void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    182 int sbus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    183 		    bus_dma_segment_t *, int, bus_size_t, int));
    184 void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    185 			   bus_size_t, int));
    186 int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    187 			   bus_size_t alignment, bus_size_t boundary,
    188 			   bus_dma_segment_t *segs, int nsegs, int *rsegs,
    189 			   int flags));
    190 void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    191 			   int nsegs));
    192 int sbus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    193 			 int nsegs, size_t size, caddr_t *kvap, int flags));
    194 void sbus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
    195 			    size_t size));
    196 
    197 /*
    198  * Child devices receive the Sbus interrupt level in their attach
    199  * arguments. We translate these to CPU IPLs using the following
    200  * tables. Note: obio bus interrupt levels are identical to the
    201  * processor IPL.
    202  *
    203  * The second set of tables is used when the Sbus interrupt level
    204  * cannot be had from the PROM as an `interrupt' property. We then
    205  * fall back on the `intr' property which contains the CPU IPL.
    206  */
    207 
    208 /* Translate Sbus interrupt level to processor IPL */
    209 static int intr_sbus2ipl_4c[] = {
    210 	0, 1, 2, 3, 5, 7, 8, 9
    211 };
    212 static int intr_sbus2ipl_4m[] = {
    213 	0, 2, 3, 5, 7, 9, 11, 13
    214 };
    215 
    216 /*
    217  * This value is or'ed into the attach args' interrupt level cookie
    218  * if the interrupt level comes from an `intr' property, i.e. it is
    219  * not an Sbus interrupt level.
    220  */
    221 #define SBUS_INTR_COMPAT	0x80000000
    222 
    223 
    224 /*
    225  * Print the location of some sbus-attached device (called just
    226  * before attaching that device).  If `sbus' is not NULL, the
    227  * device was found but not configured; print the sbus as well.
    228  * Return UNCONF (config_find ignores this if the device was configured).
    229  */
    230 int
    231 sbus_print(args, busname)
    232 	void *args;
    233 	const char *busname;
    234 {
    235 	struct sbus_attach_args *sa = args;
    236 	int i;
    237 
    238 	if (busname)
    239 		printf("%s at %s", sa->sa_name, busname);
    240 	printf(" slot %ld offset 0x%lx", (long)sa->sa_slot,
    241 	       (u_long)sa->sa_offset);
    242 	for (i = 0; i < sa->sa_nintr; i++) {
    243 		struct sbus_intr *sbi = &sa->sa_intr[i];
    244 
    245 		printf(" vector %lx ipl %ld",
    246 		       (u_long)sbi->sbi_vec,
    247 		       (long)INTLEV(sbi->sbi_pri));
    248 	}
    249 	return (UNCONF);
    250 }
    251 
    252 int
    253 sbus_match(parent, cf, aux)
    254 	struct device *parent;
    255 	struct cfdata *cf;
    256 	void *aux;
    257 {
    258 	struct mainbus_attach_args *ma = aux;
    259 
    260 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    261 }
    262 
    263 /*
    264  * Attach an Sbus.
    265  */
    266 void
    267 sbus_attach(parent, self, aux)
    268 	struct device *parent;
    269 	struct device *self;
    270 	void *aux;
    271 {
    272 	struct sbus_softc *sc = (struct sbus_softc *)self;
    273 	struct mainbus_attach_args *ma = aux;
    274 	struct intrhand *ih;
    275 	int ipl;
    276 	char *name;
    277 	int node = ma->ma_node;
    278 
    279 	int node0, error;
    280 	bus_space_tag_t sbt;
    281 	struct sbus_attach_args sa;
    282 
    283 	sc->sc_bustag = ma->ma_bustag;
    284 	sc->sc_dmatag = ma->ma_dmatag;
    285 	sc->sc_sysio = (struct sysioreg*)(u_long)ma->ma_address[0];	/* Use prom mapping for sysio. */
    286 	sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;		/* Find interrupt group no */
    287 
    288 	/* Setup interrupt translation tables */
    289 	sc->sc_intr2ipl = CPU_ISSUN4C
    290 				? intr_sbus2ipl_4c
    291 				: intr_sbus2ipl_4m;
    292 
    293 	/*
    294 	 * Record clock frequency for synchronous SCSI.
    295 	 * IS THIS THE CORRECT DEFAULT??
    296 	 */
    297 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    298 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    299 
    300 	sbt = sbus_alloc_bustag(sc);
    301 	sc->sc_dmatag = sbus_alloc_dmatag(sc);
    302 
    303 	/*
    304 	 * Get the SBus burst transfer size if burst transfers are supported
    305 	 */
    306 	sc->sc_burst = getpropint(node, "burst-sizes", 0);
    307 
    308 	/*
    309 	 * Collect address translations from the OBP.
    310 	 */
    311 	error = getprop(node, "ranges", sizeof(struct sbus_range),
    312 			 &sc->sc_nrange, (void **)&sc->sc_range);
    313 	if (error)
    314 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    315 
    316 	/* initailise the IOMMU */
    317 
    318 	/* punch in our copies */
    319 	sc->sc_is.is_bustag = sc->sc_bustag;
    320 	sc->sc_is.is_iommu = &sc->sc_sysio->sys_iommu;
    321 	sc->sc_is.is_sb = &sc->sc_sysio->sys_strbuf;
    322 
    323 	/* give us a nice name.. */
    324 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    325 	if (name == 0)
    326 		panic("couldn't malloc iommu name");
    327 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
    328 
    329 	iommu_init(name, &sc->sc_is, 0);
    330 
    331 	/* Enable the over temp intr */
    332 	ih = (struct intrhand *)
    333 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    334 	ih->ih_map = &sc->sc_sysio->therm_int_map;
    335 	ih->ih_clr = NULL; /* &sc->sc_sysio->therm_clr_int; */
    336 	ih->ih_fun = sbus_overtemp;
    337 	ipl = 1;
    338 	ih->ih_pil = (1<<ipl);
    339 	ih->ih_number = INTVEC(*(ih->ih_map));
    340 	intr_establish(ipl, ih);
    341 	*(ih->ih_map) |= INTMAP_V;
    342 
    343 	/*
    344 	 * Loop through ROM children, fixing any relative addresses
    345 	 * and then configuring each device.
    346 	 * `specials' is an array of device names that are treated
    347 	 * specially:
    348 	 */
    349 	node0 = firstchild(node);
    350 	for (node = node0; node; node = nextsibling(node)) {
    351 		char *name = getpropstring(node, "name");
    352 
    353 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    354 					   node, &sa) != 0) {
    355 			printf("sbus_attach: %s: incomplete\n", name);
    356 			continue;
    357 		}
    358 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    359 		sbus_destroy_attach_args(&sa);
    360 	}
    361 }
    362 
    363 int
    364 sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
    365 	struct sbus_softc	*sc;
    366 	bus_space_tag_t		bustag;
    367 	bus_dma_tag_t		dmatag;
    368 	int			node;
    369 	struct sbus_attach_args	*sa;
    370 {
    371 	/*struct	sbus_reg sbusreg;*/
    372 	/*int	base;*/
    373 	int	error;
    374 	int n;
    375 
    376 	bzero(sa, sizeof(struct sbus_attach_args));
    377 	error = getprop(node, "name", 1, &n, (void **)&sa->sa_name);
    378 	if (error != 0)
    379 		return (error);
    380 	sa->sa_name[n] = '\0';
    381 
    382 	sa->sa_bustag = bustag;
    383 	sa->sa_dmatag = dmatag;
    384 	sa->sa_node = node;
    385 	sa->sa_frequency = sc->sc_clockfreq;
    386 
    387 	error = getprop(node, "reg", sizeof(struct sbus_reg),
    388 			 &sa->sa_nreg, (void **)&sa->sa_reg);
    389 	if (error != 0) {
    390 		char buf[32];
    391 		if (error != ENOENT ||
    392 		    !node_has_property(node, "device_type") ||
    393 		    strcmp(getpropstringA(node, "device_type", buf),
    394 			   "hierarchical") != 0)
    395 			return (error);
    396 	}
    397 	for (n = 0; n < sa->sa_nreg; n++) {
    398 		/* Convert to relative addressing, if necessary */
    399 		u_int32_t base = sa->sa_reg[n].sbr_offset;
    400 		if (SBUS_ABS(base)) {
    401 			sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
    402 			sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
    403 		}
    404 	}
    405 
    406 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr,
    407 	    sa->sa_slot)) != 0)
    408 		return (error);
    409 
    410 	error = getprop(node, "address", sizeof(u_int32_t),
    411 			 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
    412 	if (error != 0 && error != ENOENT)
    413 		return (error);
    414 
    415 	return (0);
    416 }
    417 
    418 void
    419 sbus_destroy_attach_args(sa)
    420 	struct sbus_attach_args	*sa;
    421 {
    422 	if (sa->sa_name != NULL)
    423 		free(sa->sa_name, M_DEVBUF);
    424 
    425 	if (sa->sa_nreg != 0)
    426 		free(sa->sa_reg, M_DEVBUF);
    427 
    428 	if (sa->sa_intr)
    429 		free(sa->sa_intr, M_DEVBUF);
    430 
    431 	if (sa->sa_promvaddrs)
    432 		free((void *)sa->sa_promvaddrs, M_DEVBUF);
    433 
    434 	bzero(sa, sizeof(struct sbus_attach_args)); /*DEBUG*/
    435 }
    436 
    437 
    438 int
    439 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
    440 	bus_space_tag_t t;
    441 	bus_type_t btype;
    442 	bus_addr_t offset;
    443 	bus_size_t size;
    444 	int	flags;
    445 	vaddr_t vaddr;
    446 	bus_space_handle_t *hp;
    447 {
    448 	struct sbus_softc *sc = t->cookie;
    449 	int64_t slot = btype;
    450 	int i;
    451 
    452 	for (i = 0; i < sc->sc_nrange; i++) {
    453 		bus_addr_t paddr;
    454 
    455 		if (sc->sc_range[i].cspace != slot)
    456 			continue;
    457 
    458 		/* We've found the connection to the parent bus */
    459 		paddr = sc->sc_range[i].poffset + offset;
    460 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    461 		DPRINTF(SDB_DVMA,
    462 ("\n_sbus_bus_map: mapping paddr slot %lx offset %lx poffset %lx paddr %lx\n",
    463 		    (long)slot, (long)offset, (long)sc->sc_range[i].poffset,
    464 		    (long)paddr));
    465 		return (bus_space_map2(sc->sc_bustag, 0, paddr,
    466 					size, flags, vaddr, hp));
    467 	}
    468 
    469 	return (EINVAL);
    470 }
    471 
    472 int
    473 sbus_bus_mmap(t, btype, paddr, flags, hp)
    474 	bus_space_tag_t t;
    475 	bus_type_t btype;
    476 	bus_addr_t paddr;
    477 	int flags;
    478 	bus_space_handle_t *hp;
    479 {
    480 	bus_addr_t offset = paddr;
    481 	int slot = btype;
    482 	struct sbus_softc *sc = t->cookie;
    483 	int i;
    484 
    485 	for (i = 0; i < sc->sc_nrange; i++) {
    486 		bus_addr_t paddr;
    487 
    488 		if (sc->sc_range[i].cspace != slot)
    489 			continue;
    490 
    491 		paddr = sc->sc_range[i].poffset + offset;
    492 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    493 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    494 				       flags, hp));
    495 	}
    496 
    497 	return (-1);
    498 }
    499 
    500 
    501 /*
    502  * Each attached device calls sbus_establish after it initializes
    503  * its sbusdev portion.
    504  */
    505 void
    506 sbus_establish(sd, dev)
    507 	register struct sbusdev *sd;
    508 	register struct device *dev;
    509 {
    510 	register struct sbus_softc *sc;
    511 	register struct device *curdev;
    512 
    513 	/*
    514 	 * We have to look for the sbus by name, since it is not necessarily
    515 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    516 	 * We don't just use the device structure of the above-attached
    517 	 * sbus, since we might (in the future) support multiple sbus's.
    518 	 */
    519 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    520 		if (!curdev || !curdev->dv_xname)
    521 			panic("sbus_establish: can't find sbus parent for %s",
    522 			      sd->sd_dev->dv_xname
    523 					? sd->sd_dev->dv_xname
    524 					: "<unknown>" );
    525 
    526 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    527 			break;
    528 	}
    529 	sc = (struct sbus_softc *) curdev;
    530 
    531 	sd->sd_dev = dev;
    532 	sd->sd_bchain = sc->sc_sbdev;
    533 	sc->sc_sbdev = sd;
    534 }
    535 
    536 /*
    537  * Reset the given sbus.
    538  */
    539 void
    540 sbusreset(sbus)
    541 	int sbus;
    542 {
    543 	register struct sbusdev *sd;
    544 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    545 	struct device *dev;
    546 
    547 	printf("reset %s:", sc->sc_dev.dv_xname);
    548 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    549 		if (sd->sd_reset) {
    550 			dev = sd->sd_dev;
    551 			(*sd->sd_reset)(dev);
    552 			printf(" %s", dev->dv_xname);
    553 		}
    554 	}
    555 	/* Reload iommu regs */
    556 	iommu_reset(&sc->sc_is);
    557 }
    558 
    559 /*
    560  * Handle an overtemp situation.
    561  *
    562  * SPARCs have temperature sensors which generate interrupts
    563  * if the machine's temperature exceeds a certain threshold.
    564  * This handles the interrupt and powers off the machine.
    565  * The same needs to be done to PCI controller drivers.
    566  */
    567 int
    568 sbus_overtemp(arg)
    569 	void *arg;
    570 {
    571 	/* Should try a clean shutdown first */
    572 	printf("DANGER: OVER TEMPERATURE detected\nShutting down...\n");
    573 	delay(20);
    574 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
    575 }
    576 
    577 /*
    578  * Get interrupt attributes for an Sbus device.
    579  */
    580 int
    581 sbus_get_intr(sc, node, ipp, np, slot)
    582 	struct sbus_softc *sc;
    583 	int node;
    584 	struct sbus_intr **ipp;
    585 	int *np;
    586 	int slot;
    587 {
    588 	int *ipl;
    589 	int n, i;
    590 	char buf[32];
    591 
    592 	/*
    593 	 * The `interrupts' property contains the Sbus interrupt level.
    594 	 */
    595 	ipl = NULL;
    596 	if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
    597 		struct sbus_intr *ip;
    598 		int pri;
    599 
    600 		/* Default to interrupt level 2 -- otherwise unused */
    601 		pri = INTLEVENCODE(2);
    602 
    603 		/* Change format to an `struct sbus_intr' array */
    604 		ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
    605 		if (ip == NULL)
    606 			return (ENOMEM);
    607 
    608 		/*
    609 		 * Now things get ugly.  We need to take this value which is
    610 		 * the interrupt vector number and encode the IPL into it
    611 		 * somehow. Luckily, the interrupt vector has lots of free
    612 		 * space and we can easily stuff the IPL in there for a while.
    613 		 */
    614 		getpropstringA(node, "device_type", buf);
    615 		if (!buf[0])
    616 			getpropstringA(node, "name", buf);
    617 
    618 		for (i = 0; intrmap[i].in_class; i++)
    619 			if (strcmp(intrmap[i].in_class, buf) == 0) {
    620 				pri = INTLEVENCODE(intrmap[i].in_lev);
    621 				break;
    622 			}
    623 
    624 		/*
    625 		 * Sbus card devices need the slot number encoded into
    626 		 * the vector as this is generally not done.
    627 		 */
    628 		if ((ipl[0] & INTMAP_OBIO) == 0)
    629 			pri |= slot << 3;
    630 
    631 		for (n = 0; n < *np; n++) {
    632 			/*
    633 			 * We encode vector and priority into sbi_pri so we
    634 			 * can pass them as a unit.  This will go away if
    635 			 * sbus_establish ever takes an sbus_intr instead
    636 			 * of an integer level.
    637 			 * Stuff the real vector in sbi_vec.
    638 			 */
    639 
    640 			ip[n].sbi_pri = pri|ipl[n];
    641 			ip[n].sbi_vec = ipl[n];
    642 		}
    643 		free(ipl, M_DEVBUF);
    644 		*ipp = ip;
    645 	}
    646 
    647 	return (0);
    648 }
    649 
    650 
    651 /*
    652  * Install an interrupt handler for an Sbus device.
    653  */
    654 void *
    655 sbus_intr_establish(t, pri, level, flags, handler, arg)
    656 	bus_space_tag_t t;
    657 	int pri;
    658 	int level;
    659 	int flags;
    660 	int (*handler) __P((void *));
    661 	void *arg;
    662 {
    663 	struct sbus_softc *sc = t->cookie;
    664 	struct intrhand *ih;
    665 	int ipl;
    666 	long vec = pri;
    667 
    668 	ih = (struct intrhand *)
    669 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    670 	if (ih == NULL)
    671 		return (NULL);
    672 
    673 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    674 		ipl = vec;
    675 	else if ((vec & SBUS_INTR_COMPAT) != 0)
    676 		ipl = vec & ~SBUS_INTR_COMPAT;
    677 	else {
    678 		/* Decode and remove IPL */
    679 		ipl = INTLEV(vec);
    680 		vec = INTVEC(vec);
    681 		DPRINTF(SDB_INTR,
    682 		    ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
    683 		    (long)ipl, (long)vec, (u_long)intrlev[vec]));
    684 		if ((vec & INTMAP_OBIO) == 0) {
    685 			/* We're in an SBUS slot */
    686 			/* Register the map and clear intr registers */
    687 
    688 			int slot = INTSLOT(pri);
    689 
    690 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
    691 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
    692 #ifdef DEBUG
    693 			if (sbus_debug & SDB_INTR) {
    694 				int64_t intrmap = *ih->ih_map;
    695 
    696 				printf("SBUS %lx IRQ as %llx in slot %d\n",
    697 				       (long)vec, (long long)intrmap, slot);
    698 				printf("\tmap addr %p clr addr %p\n",
    699 				    ih->ih_map, ih->ih_clr);
    700 			}
    701 #endif
    702 			/* Enable the interrupt */
    703 			vec |= INTMAP_V;
    704 			/* Insert IGN */
    705 			vec |= sc->sc_ign;
    706 			bus_space_write_8(sc->sc_bustag,
    707 			    (bus_space_handle_t)(u_long)ih->ih_map, 0, vec);
    708 		} else {
    709 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    710 			int64_t intrmap = 0;
    711 			int i;
    712 
    713 			/* Insert IGN */
    714 			vec |= sc->sc_ign;
    715 			for (i = 0; &intrptr[i] <=
    716 			    (int64_t *)&sc->sc_sysio->reserved_int_map &&
    717 			    INTVEC(intrmap = intrptr[i]) != INTVEC(vec); i++)
    718 				;
    719 			if (INTVEC(intrmap) == INTVEC(vec)) {
    720 				DPRINTF(SDB_INTR,
    721 				    ("OBIO %lx IRQ as %lx in slot %d\n",
    722 				    vec, (long)intrmap, i));
    723 				/* Register the map and clear intr registers */
    724 				ih->ih_map = &intrptr[i];
    725 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    726 				ih->ih_clr = &intrptr[i];
    727 				/* Enable the interrupt */
    728 				intrmap |= INTMAP_V;
    729 				bus_space_write_8(sc->sc_bustag,
    730 				    (bus_space_handle_t)(u_long)ih->ih_map, 0,
    731 				    (u_long)intrmap);
    732 			} else
    733 				panic("IRQ not found!");
    734 		}
    735 	}
    736 #ifdef DEBUG
    737 	if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
    738 #endif
    739 
    740 	ih->ih_fun = handler;
    741 	ih->ih_arg = arg;
    742 	ih->ih_number = vec;
    743 	ih->ih_pil = (1<<ipl);
    744 	intr_establish(ipl, ih);
    745 	return (ih);
    746 }
    747 
    748 static bus_space_tag_t
    749 sbus_alloc_bustag(sc)
    750 	struct sbus_softc *sc;
    751 {
    752 	bus_space_tag_t sbt;
    753 
    754 	sbt = (bus_space_tag_t)
    755 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    756 	if (sbt == NULL)
    757 		return (NULL);
    758 
    759 	bzero(sbt, sizeof *sbt);
    760 	sbt->cookie = sc;
    761 	sbt->parent = sc->sc_bustag;
    762 	sbt->type = SBUS_BUS_SPACE;
    763 	sbt->sparc_bus_map = _sbus_bus_map;
    764 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    765 	sbt->sparc_intr_establish = sbus_intr_establish;
    766 	return (sbt);
    767 }
    768 
    769 
    770 static bus_dma_tag_t
    771 sbus_alloc_dmatag(sc)
    772 	struct sbus_softc *sc;
    773 {
    774 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    775 
    776 	sdt = (bus_dma_tag_t)
    777 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    778 	if (sdt == NULL)
    779 		/* Panic? */
    780 		return (psdt);
    781 
    782 	sdt->_cookie = sc;
    783 	sdt->_parent = psdt;
    784 #define PCOPY(x)	sdt->x = psdt->x
    785 	PCOPY(_dmamap_create);
    786 	PCOPY(_dmamap_destroy);
    787 	sdt->_dmamap_load = sbus_dmamap_load;
    788 	PCOPY(_dmamap_load_mbuf);
    789 	PCOPY(_dmamap_load_uio);
    790 	sdt->_dmamap_load_raw = sbus_dmamap_load_raw;
    791 	sdt->_dmamap_unload = sbus_dmamap_unload;
    792 	sdt->_dmamap_sync = sbus_dmamap_sync;
    793 	sdt->_dmamem_alloc = sbus_dmamem_alloc;
    794 	sdt->_dmamem_free = sbus_dmamem_free;
    795 	sdt->_dmamem_map = sbus_dmamem_map;
    796 	sdt->_dmamem_unmap = sbus_dmamem_unmap;
    797 	PCOPY(_dmamem_mmap);
    798 #undef	PCOPY
    799 	sc->sc_dmatag = sdt;
    800 	return (sdt);
    801 }
    802 
    803 int
    804 sbus_dmamap_load(tag, map, buf, buflen, p, flags)
    805 	bus_dma_tag_t tag;
    806 	bus_dmamap_t map;
    807 	void *buf;
    808 	bus_size_t buflen;
    809 	struct proc *p;
    810 	int flags;
    811 {
    812 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    813 
    814 	return (iommu_dvmamap_load(tag, &sc->sc_is, map, buf, buflen, p, flags));
    815 }
    816 
    817 int
    818 sbus_dmamap_load_raw(tag, map, segs, nsegs, size, flags)
    819 	bus_dma_tag_t tag;
    820 	bus_dmamap_t map;
    821 	bus_dma_segment_t *segs;
    822 	int nsegs;
    823 	bus_size_t size;
    824 	int flags;
    825 {
    826 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    827 
    828 	return (iommu_dvmamap_load_raw(tag, &sc->sc_is, map, segs, nsegs, flags, size));
    829 }
    830 
    831 void
    832 sbus_dmamap_unload(tag, map)
    833 	bus_dma_tag_t tag;
    834 	bus_dmamap_t map;
    835 {
    836 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    837 
    838 	iommu_dvmamap_unload(tag, &sc->sc_is, map);
    839 }
    840 
    841 void
    842 sbus_dmamap_sync(tag, map, offset, len, ops)
    843 	bus_dma_tag_t tag;
    844 	bus_dmamap_t map;
    845 	bus_addr_t offset;
    846 	bus_size_t len;
    847 	int ops;
    848 {
    849 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    850 
    851 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
    852 		/* Flush the CPU then the IOMMU */
    853 		bus_dmamap_sync(tag->_parent, map, offset, len, ops);
    854 		iommu_dvmamap_sync(tag, &sc->sc_is, map, offset, len, ops);
    855 	}
    856 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
    857 		/* Flush the IOMMU then the CPU */
    858 		iommu_dvmamap_sync(tag, &sc->sc_is, map, offset, len, ops);
    859 		bus_dmamap_sync(tag->_parent, map, offset, len, ops);
    860 	}
    861 }
    862 
    863 int
    864 sbus_dmamem_alloc(tag, size, alignment, boundary, segs, nsegs, rsegs, flags)
    865 	bus_dma_tag_t tag;
    866 	bus_size_t size;
    867 	bus_size_t alignment;
    868 	bus_size_t boundary;
    869 	bus_dma_segment_t *segs;
    870 	int nsegs;
    871 	int *rsegs;
    872 	int flags;
    873 {
    874 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    875 
    876 	return (iommu_dvmamem_alloc(tag, &sc->sc_is, size, alignment, boundary,
    877 	    segs, nsegs, rsegs, flags));
    878 }
    879 
    880 void
    881 sbus_dmamem_free(tag, segs, nsegs)
    882 	bus_dma_tag_t tag;
    883 	bus_dma_segment_t *segs;
    884 	int nsegs;
    885 {
    886 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    887 
    888 	iommu_dvmamem_free(tag, &sc->sc_is, segs, nsegs);
    889 }
    890 
    891 int
    892 sbus_dmamem_map(tag, segs, nsegs, size, kvap, flags)
    893 	bus_dma_tag_t tag;
    894 	bus_dma_segment_t *segs;
    895 	int nsegs;
    896 	size_t size;
    897 	caddr_t *kvap;
    898 	int flags;
    899 {
    900 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    901 
    902 	return (iommu_dvmamem_map(tag, &sc->sc_is, segs, nsegs, size, kvap, flags));
    903 }
    904 
    905 void
    906 sbus_dmamem_unmap(tag, kva, size)
    907 	bus_dma_tag_t tag;
    908 	caddr_t kva;
    909 	size_t size;
    910 {
    911 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    912 
    913 	iommu_dvmamem_unmap(tag, &sc->sc_is, kva, size);
    914 }
    915