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