Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.56
      1 /*	$NetBSD: sbus.c,v 1.56 2002/12/10 12:24:05 pk 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 openprom_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 		void (*) __P((void))));	/*optional fast trap*/
     90 
     91 
     92 /* autoconfiguration driver */
     93 int	sbus_match __P((struct device *, struct cfdata *, void *));
     94 void	sbus_attach __P((struct device *, struct device *, void *));
     95 
     96 
     97 CFATTACH_DECL(sbus, sizeof(struct sbus_softc),
     98     sbus_match, sbus_attach, NULL, NULL);
     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 openprom_intr *sbi = &sa->sa_intr[i];
    162 
    163 		printf(" vector %lx ipl %ld",
    164 		       (u_long)sbi->oi_vec,
    165 		       (long)INTLEV(sbi->oi_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_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 openprom_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	openprom_addr 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 openprom_addr),
    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].oa_base;
    356 		if (SBUS_ABS(base)) {
    357 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
    358 			sa->sa_reg[n].oa_base = 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].or_child_space != slot)
    412 			continue;
    413 
    414 		/* We've found the connection to the parent bus */
    415 		paddr = sc->sc_range[i].or_parent_base + offset;
    416 		paddr |= ((bus_addr_t)sc->sc_range[i].or_parent_space<<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,
    420 		    (long)sc->sc_range[i].or_parent_base,
    421 		    (long)paddr));
    422 		return (bus_space_map(sc->sc_bustag, paddr, size, flags, hp));
    423 	}
    424 
    425 	return (EINVAL);
    426 }
    427 
    428 
    429 bus_addr_t
    430 sbus_bus_addr(t, btype, offset)
    431 	bus_space_tag_t t;
    432 	u_int btype;
    433 	u_int offset;
    434 {
    435 	bus_addr_t baddr;
    436 	int slot = btype;
    437 	struct sbus_softc *sc = t->cookie;
    438 	int i;
    439 
    440 	for (i = 0; i < sc->sc_nrange; i++) {
    441 		if (sc->sc_range[i].or_child_space != slot)
    442 			continue;
    443 
    444 		baddr = sc->sc_range[i].or_parent_base + offset;
    445 		baddr |= ((bus_addr_t)sc->sc_range[i].or_parent_space<<32);
    446 	}
    447 
    448 	return (baddr);
    449 }
    450 
    451 
    452 /*
    453  * Each attached device calls sbus_establish after it initializes
    454  * its sbusdev portion.
    455  */
    456 void
    457 sbus_establish(sd, dev)
    458 	register struct sbusdev *sd;
    459 	register struct device *dev;
    460 {
    461 	register struct sbus_softc *sc;
    462 	register struct device *curdev;
    463 
    464 	/*
    465 	 * We have to look for the sbus by name, since it is not necessarily
    466 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    467 	 * We don't just use the device structure of the above-attached
    468 	 * sbus, since we might (in the future) support multiple sbus's.
    469 	 */
    470 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    471 		if (!curdev || !curdev->dv_xname)
    472 			panic("sbus_establish: can't find sbus parent for %s",
    473 			      sd->sd_dev->dv_xname
    474 					? sd->sd_dev->dv_xname
    475 					: "<unknown>" );
    476 
    477 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    478 			break;
    479 	}
    480 	sc = (struct sbus_softc *) curdev;
    481 
    482 	sd->sd_dev = dev;
    483 	sd->sd_bchain = sc->sc_sbdev;
    484 	sc->sc_sbdev = sd;
    485 }
    486 
    487 /*
    488  * Reset the given sbus.
    489  */
    490 void
    491 sbusreset(sbus)
    492 	int sbus;
    493 {
    494 	register struct sbusdev *sd;
    495 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    496 	struct device *dev;
    497 
    498 	printf("reset %s:", sc->sc_dev.dv_xname);
    499 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    500 		if (sd->sd_reset) {
    501 			dev = sd->sd_dev;
    502 			(*sd->sd_reset)(dev);
    503 			printf(" %s", dev->dv_xname);
    504 		}
    505 	}
    506 	/* Reload iommu regs */
    507 	iommu_reset(&sc->sc_is);
    508 }
    509 
    510 /*
    511  * Handle an overtemp situation.
    512  *
    513  * SPARCs have temperature sensors which generate interrupts
    514  * if the machine's temperature exceeds a certain threshold.
    515  * This handles the interrupt and powers off the machine.
    516  * The same needs to be done to PCI controller drivers.
    517  */
    518 int
    519 sbus_overtemp(arg)
    520 	void *arg;
    521 {
    522 	/* Should try a clean shutdown first */
    523 	printf("DANGER: OVER TEMPERATURE detected\nShutting down...\n");
    524 	delay(20);
    525 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
    526 }
    527 
    528 /*
    529  * Get interrupt attributes for an Sbus device.
    530  */
    531 int
    532 sbus_get_intr(sc, node, ipp, np, slot)
    533 	struct sbus_softc *sc;
    534 	int node;
    535 	struct openprom_intr **ipp;
    536 	int *np;
    537 	int slot;
    538 {
    539 	int *ipl;
    540 	int n, i;
    541 	char buf[32];
    542 
    543 	/*
    544 	 * The `interrupts' property contains the Sbus interrupt level.
    545 	 */
    546 	ipl = NULL;
    547 	if (PROM_getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
    548 		struct openprom_intr *ip;
    549 		int pri;
    550 
    551 		/* Default to interrupt level 2 -- otherwise unused */
    552 		pri = INTLEVENCODE(2);
    553 
    554 		/* Change format to an `struct sbus_intr' array */
    555 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
    556 		    M_NOWAIT);
    557 		if (ip == NULL)
    558 			return (ENOMEM);
    559 
    560 		/*
    561 		 * Now things get ugly.  We need to take this value which is
    562 		 * the interrupt vector number and encode the IPL into it
    563 		 * somehow. Luckily, the interrupt vector has lots of free
    564 		 * space and we can easily stuff the IPL in there for a while.
    565 		 */
    566 		PROM_getpropstringA(node, "device_type", buf);
    567 		if (!buf[0])
    568 			PROM_getpropstringA(node, "name", buf);
    569 
    570 		for (i = 0; intrmap[i].in_class; i++)
    571 			if (strcmp(intrmap[i].in_class, buf) == 0) {
    572 				pri = INTLEVENCODE(intrmap[i].in_lev);
    573 				break;
    574 			}
    575 
    576 		/*
    577 		 * Sbus card devices need the slot number encoded into
    578 		 * the vector as this is generally not done.
    579 		 */
    580 		if ((ipl[0] & INTMAP_OBIO) == 0)
    581 			pri |= slot << 3;
    582 
    583 		for (n = 0; n < *np; n++) {
    584 			/*
    585 			 * We encode vector and priority into sbi_pri so we
    586 			 * can pass them as a unit.  This will go away if
    587 			 * sbus_establish ever takes an sbus_intr instead
    588 			 * of an integer level.
    589 			 * Stuff the real vector in sbi_vec.
    590 			 */
    591 
    592 			ip[n].oi_pri = pri|ipl[n];
    593 			ip[n].oi_vec = ipl[n];
    594 		}
    595 		free(ipl, M_DEVBUF);
    596 		*ipp = ip;
    597 	}
    598 
    599 	return (0);
    600 }
    601 
    602 
    603 /*
    604  * Install an interrupt handler for an Sbus device.
    605  */
    606 void *
    607 sbus_intr_establish(t, pri, level, flags, handler, arg, fastvec)
    608 	bus_space_tag_t t;
    609 	int pri;
    610 	int level;
    611 	int flags;
    612 	int (*handler) __P((void *));
    613 	void *arg;
    614 	void (*fastvec) __P((void));	/* ignored */
    615 {
    616 	struct sbus_softc *sc = t->cookie;
    617 	struct intrhand *ih;
    618 	int ipl;
    619 	long vec = pri;
    620 
    621 	ih = (struct intrhand *)
    622 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    623 	if (ih == NULL)
    624 		return (NULL);
    625 
    626 	if ((vec & SBUS_INTR_COMPAT) != 0)
    627 		ipl = vec & ~SBUS_INTR_COMPAT;
    628 	else {
    629 		/* Decode and remove IPL */
    630 		ipl = INTLEV(vec);
    631 		vec = INTVEC(vec);
    632 		DPRINTF(SDB_INTR,
    633 		    ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
    634 		    (long)ipl, (long)vec, (u_long)intrlev[vec]));
    635 		if ((vec & INTMAP_OBIO) == 0) {
    636 			/* We're in an SBUS slot */
    637 			/* Register the map and clear intr registers */
    638 
    639 			int slot = INTSLOT(pri);
    640 
    641 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
    642 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
    643 #ifdef DEBUG
    644 			if (sbus_debug & SDB_INTR) {
    645 				int64_t intrmap = *ih->ih_map;
    646 
    647 				printf("SBUS %lx IRQ as %llx in slot %d\n",
    648 				       (long)vec, (long long)intrmap, slot);
    649 				printf("\tmap addr %p clr addr %p\n",
    650 				    ih->ih_map, ih->ih_clr);
    651 			}
    652 #endif
    653 			/* Enable the interrupt */
    654 			vec |= INTMAP_V;
    655 			/* Insert IGN */
    656 			vec |= sc->sc_ign;
    657 			/* XXXX */
    658 			*(ih->ih_map) = vec;
    659 		} else {
    660 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    661 			int64_t intrmap = 0;
    662 			int i;
    663 
    664 			/* Insert IGN */
    665 			vec |= sc->sc_ign;
    666 			for (i = 0; &intrptr[i] <=
    667 			    (int64_t *)&sc->sc_sysio->reserved_int_map &&
    668 			    INTVEC(intrmap = intrptr[i]) != INTVEC(vec); i++)
    669 				;
    670 			if (INTVEC(intrmap) == INTVEC(vec)) {
    671 				DPRINTF(SDB_INTR,
    672 				    ("OBIO %lx IRQ as %lx in slot %d\n",
    673 				    vec, (long)intrmap, i));
    674 				/* Register the map and clear intr registers */
    675 				ih->ih_map = &intrptr[i];
    676 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    677 				ih->ih_clr = &intrptr[i];
    678 				/* Enable the interrupt */
    679 				intrmap |= INTMAP_V;
    680 				/* XXXX */
    681 				*(ih->ih_map) = intrmap;
    682 			} else
    683 				panic("IRQ not found!");
    684 		}
    685 	}
    686 #ifdef DEBUG
    687 	if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
    688 #endif
    689 
    690 	ih->ih_fun = handler;
    691 	ih->ih_arg = arg;
    692 	ih->ih_number = vec;
    693 	ih->ih_pil = (1<<ipl);
    694 	intr_establish(ipl, ih);
    695 	return (ih);
    696 }
    697 
    698 static bus_space_tag_t
    699 sbus_alloc_bustag(sc)
    700 	struct sbus_softc *sc;
    701 {
    702 	bus_space_tag_t sbt;
    703 
    704 	sbt = (bus_space_tag_t)
    705 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    706 	if (sbt == NULL)
    707 		return (NULL);
    708 
    709 	bzero(sbt, sizeof *sbt);
    710 	sbt->cookie = sc;
    711 	sbt->parent = sc->sc_bustag;
    712 	sbt->type = SBUS_BUS_SPACE;
    713 	sbt->sparc_bus_map = _sbus_bus_map;
    714 	sbt->sparc_bus_mmap = sc->sc_bustag->sparc_bus_mmap;
    715 	sbt->sparc_intr_establish = sbus_intr_establish;
    716 	return (sbt);
    717 }
    718 
    719 
    720 static bus_dma_tag_t
    721 sbus_alloc_dmatag(sc)
    722 	struct sbus_softc *sc;
    723 {
    724 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    725 
    726 	sdt = (bus_dma_tag_t)
    727 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    728 	if (sdt == NULL)
    729 		/* Panic? */
    730 		return (psdt);
    731 
    732 	sdt->_cookie = sc;
    733 	sdt->_parent = psdt;
    734 #define PCOPY(x)	sdt->x = psdt->x
    735 	PCOPY(_dmamap_create);
    736 	PCOPY(_dmamap_destroy);
    737 	sdt->_dmamap_load = sbus_dmamap_load;
    738 	PCOPY(_dmamap_load_mbuf);
    739 	PCOPY(_dmamap_load_uio);
    740 	sdt->_dmamap_load_raw = sbus_dmamap_load_raw;
    741 	sdt->_dmamap_unload = sbus_dmamap_unload;
    742 	sdt->_dmamap_sync = sbus_dmamap_sync;
    743 	sdt->_dmamem_alloc = sbus_dmamem_alloc;
    744 	sdt->_dmamem_free = sbus_dmamem_free;
    745 	sdt->_dmamem_map = sbus_dmamem_map;
    746 	sdt->_dmamem_unmap = sbus_dmamem_unmap;
    747 	PCOPY(_dmamem_mmap);
    748 #undef	PCOPY
    749 	sc->sc_dmatag = sdt;
    750 	return (sdt);
    751 }
    752 
    753 int
    754 sbus_dmamap_load(tag, map, buf, buflen, p, flags)
    755 	bus_dma_tag_t tag;
    756 	bus_dmamap_t map;
    757 	void *buf;
    758 	bus_size_t buflen;
    759 	struct proc *p;
    760 	int flags;
    761 {
    762 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    763 
    764 	return (iommu_dvmamap_load(tag, &sc->sc_sb, map, buf, buflen, p, flags));
    765 }
    766 
    767 int
    768 sbus_dmamap_load_raw(tag, map, segs, nsegs, size, flags)
    769 	bus_dma_tag_t tag;
    770 	bus_dmamap_t map;
    771 	bus_dma_segment_t *segs;
    772 	int nsegs;
    773 	bus_size_t size;
    774 	int flags;
    775 {
    776 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    777 
    778 	return (iommu_dvmamap_load_raw(tag, &sc->sc_sb, map, segs, nsegs, flags, size));
    779 }
    780 
    781 void
    782 sbus_dmamap_unload(tag, map)
    783 	bus_dma_tag_t tag;
    784 	bus_dmamap_t map;
    785 {
    786 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    787 
    788 	iommu_dvmamap_unload(tag, &sc->sc_sb, map);
    789 }
    790 
    791 void
    792 sbus_dmamap_sync(tag, map, offset, len, ops)
    793 	bus_dma_tag_t tag;
    794 	bus_dmamap_t map;
    795 	bus_addr_t offset;
    796 	bus_size_t len;
    797 	int ops;
    798 {
    799 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    800 
    801 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
    802 		/* Flush the CPU then the IOMMU */
    803 		bus_dmamap_sync(tag->_parent, map, offset, len, ops);
    804 		iommu_dvmamap_sync(tag, &sc->sc_sb, map, offset, len, ops);
    805 	}
    806 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
    807 		/* Flush the IOMMU then the CPU */
    808 		iommu_dvmamap_sync(tag, &sc->sc_sb, map, offset, len, ops);
    809 		bus_dmamap_sync(tag->_parent, map, offset, len, ops);
    810 	}
    811 }
    812 
    813 int
    814 sbus_dmamem_alloc(tag, size, alignment, boundary, segs, nsegs, rsegs, flags)
    815 	bus_dma_tag_t tag;
    816 	bus_size_t size;
    817 	bus_size_t alignment;
    818 	bus_size_t boundary;
    819 	bus_dma_segment_t *segs;
    820 	int nsegs;
    821 	int *rsegs;
    822 	int flags;
    823 {
    824 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    825 
    826 	return (iommu_dvmamem_alloc(tag, &sc->sc_sb, size, alignment, boundary,
    827 	    segs, nsegs, rsegs, flags));
    828 }
    829 
    830 void
    831 sbus_dmamem_free(tag, segs, nsegs)
    832 	bus_dma_tag_t tag;
    833 	bus_dma_segment_t *segs;
    834 	int nsegs;
    835 {
    836 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    837 
    838 	iommu_dvmamem_free(tag, &sc->sc_sb, segs, nsegs);
    839 }
    840 
    841 int
    842 sbus_dmamem_map(tag, segs, nsegs, size, kvap, flags)
    843 	bus_dma_tag_t tag;
    844 	bus_dma_segment_t *segs;
    845 	int nsegs;
    846 	size_t size;
    847 	caddr_t *kvap;
    848 	int flags;
    849 {
    850 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    851 
    852 	return (iommu_dvmamem_map(tag, &sc->sc_sb, segs, nsegs, size, kvap, flags));
    853 }
    854 
    855 void
    856 sbus_dmamem_unmap(tag, kva, size)
    857 	bus_dma_tag_t tag;
    858 	caddr_t kva;
    859 	size_t size;
    860 {
    861 	struct sbus_softc *sc = (struct sbus_softc *)tag->_cookie;
    862 
    863 	iommu_dvmamem_unmap(tag, &sc->sc_sb, kva, size);
    864 }
    865