Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.1
      1 /*	$NetBSD: sbus.c,v 1.1 1998/06/20 04:58:51 eeh 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  * Sbus stuff.
     85  */
     86 
     87 #include <sys/param.h>
     88 #include <sys/malloc.h>
     89 #include <sys/systm.h>
     90 #include <sys/device.h>
     91 #include <vm/vm.h>
     92 
     93 #include <machine/bus.h>
     94 #include <sparc64/dev/sbusreg.h>
     95 #include <sparc64/dev/sbusvar.h>
     96 #include <sparc64/sparc64/asm.h>
     97 
     98 #include <machine/autoconf.h>
     99 #include <machine/ctlreg.h>
    100 #include <machine/cpu.h>
    101 
    102 /* XXXXX -- Needed to allow dvma_mapin to work -- need to switch to bus_dma_* */
    103 struct sbus_softc *sbus0;
    104 
    105 #ifdef DEBUG
    106 #define SDB_DVMA	0x1
    107 #define SDB_INTR	0x2
    108 int sbusdebug = 0;
    109 #endif
    110 
    111 void sbusreset __P((int));
    112 int sbus_flush __P((struct sbus_softc *));
    113 
    114 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
    115 static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
    116 static int sbus_get_intr __P((struct sbus_softc *, int, int *));
    117 static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    118 			      int, bus_space_handle_t *));
    119 static int _sbus_bus_map __P((
    120 		bus_space_tag_t,
    121 		bus_type_t,
    122 		bus_addr_t,		/*offset*/
    123 		bus_size_t,		/*size*/
    124 		int,			/*flags*/
    125 		vm_offset_t,		/*preferred virtual address */
    126 		bus_space_handle_t *));
    127 static void *sbus_intr_establish __P((
    128 		bus_space_tag_t,
    129 		int,			/*level*/
    130 		int,			/*flags*/
    131 		int (*) __P((void *)),	/*handler*/
    132 		void *));		/*handler arg*/
    133 
    134 
    135 /* autoconfiguration driver */
    136 int	sbus_match __P((struct device *, struct cfdata *, void *));
    137 void	sbus_attach __P((struct device *, struct device *, void *));
    138 
    139 
    140 struct cfattach sbus_ca = {
    141 	sizeof(struct sbus_softc), sbus_match, sbus_attach
    142 };
    143 
    144 extern struct cfdriver sbus_cd;
    145 
    146 /*
    147  * DVMA routines
    148  */
    149 void sbus_enter __P((struct sbus_softc *, vm_offset_t, int64_t));
    150 void sbus_remove __P((struct sbus_softc *, vm_offset_t, u_int));
    151 int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    152 			  bus_size_t, struct proc *, int));
    153 void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    154 void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    155 			   bus_size_t, int));
    156 int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    157 			   bus_size_t alignment, bus_size_t boundary,
    158 			   bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    159 void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    160 			   int nsegs));
    161 
    162 
    163 /*
    164  * Child devices receive the Sbus interrupt level in their attach
    165  * arguments. We translate these to CPU IPLs using the following
    166  * tables. Note: obio bus interrupt levels are identical to the
    167  * processor IPL.
    168  *
    169  * The second set of tables is used when the Sbus interrupt level
    170  * cannot be had from the PROM as an `interrupt' property. We then
    171  * fall back on the `intr' property which contains the CPU IPL.
    172  */
    173 
    174 /* Translate Sbus interrupt level to processor IPL */
    175 static int intr_sbus2ipl_4c[] = {
    176 	0, 1, 2, 3, 5, 7, 8, 9
    177 };
    178 static int intr_sbus2ipl_4m[] = {
    179 	0, 2, 3, 5, 7, 9, 11, 13
    180 };
    181 
    182 /*
    183  * This value is or'ed into the attach args' interrupt level cookie
    184  * if the interrupt level comes from an `intr' property, i.e. it is
    185  * not an Sbus interrupt level.
    186  */
    187 #define SBUS_INTR_COMPAT	0x80000000
    188 
    189 
    190 /*
    191  * Print the location of some sbus-attached device (called just
    192  * before attaching that device).  If `sbus' is not NULL, the
    193  * device was found but not configured; print the sbus as well.
    194  * Return UNCONF (config_find ignores this if the device was configured).
    195  */
    196 int
    197 sbus_print(args, busname)
    198 	void *args;
    199 	const char *busname;
    200 {
    201 	struct sbus_attach_args *sa = args;
    202 
    203 	if (busname)
    204 		printf("%s at %s", sa->sa_name, busname);
    205 	printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
    206 	if (sa->sa_pri) {
    207 		int level = sa->sa_pri;
    208 
    209 		printf(" vector %x ipl %d", (int)INTVEC(level), (int)INTLEV(level));
    210 	}
    211 	return (UNCONF);
    212 }
    213 
    214 int
    215 sbus_match(parent, cf, aux)
    216 	struct device *parent;
    217 	struct cfdata *cf;
    218 	void *aux;
    219 {
    220 	struct mainbus_attach_args *ma = aux;
    221 
    222 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    223 }
    224 
    225 /*
    226  * Attach an Sbus.
    227  */
    228 void
    229 sbus_attach(parent, self, aux)
    230 	struct device *parent;
    231 	struct device *self;
    232 	void *aux;
    233 {
    234 	struct sbus_softc *sc = sbus0 = (struct sbus_softc *)self;
    235 	struct mainbus_attach_args *ma = aux;
    236 	int node = ma->ma_node;
    237 
    238 	int node0, error;
    239 	bus_space_tag_t sbt;
    240 	struct sbus_attach_args sa;
    241 	char *busname = "sbus";
    242 	struct bootpath *bp = ma->ma_bp;
    243 
    244 
    245 	sc->sc_bustag = ma->ma_bustag;
    246 	sc->sc_dmatag = ma->ma_dmatag;
    247 	sc->sc_sysio = (struct sysioreg*) ma->ma_address[0];	/* Use prom mapping for sysio. */
    248 	sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;		/* Find interrupt group no */
    249 
    250 	/* Setup interrupt translation tables */
    251 	sc->sc_intr2ipl = CPU_ISSUN4C
    252 				? intr_sbus2ipl_4c
    253 				: intr_sbus2ipl_4m;
    254 
    255 	/*
    256 	 * Record clock frequency for synchronous SCSI.
    257 	 * IS THIS THE CORRECT DEFAULT??
    258 	 */
    259 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    260 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    261 
    262 	sbt = sbus_alloc_bustag(sc);
    263 	sc->sc_dmatag = sbus_alloc_dmatag(sc);
    264 
    265 	/*
    266 	 * Get the SBus burst transfer size if burst transfers are supported
    267 	 */
    268 	sc->sc_burst = getpropint(node, "burst-sizes", 0);
    269 
    270 	/* Propagate bootpath */
    271 	if (bp != NULL && strcmp(bp->name, busname) == 0)
    272 		bp++;
    273 	else
    274 		bp = NULL;
    275 
    276 	/*
    277 	 * Collect address translations from the OBP.
    278 	 */
    279 	error = getpropA(node, "ranges", sizeof(struct sbus_range),
    280 			 &sc->sc_nrange, (void **)&sc->sc_range);
    281 	switch (error) {
    282 	case 0:
    283 		break;
    284 #if 0
    285 	case ENOENT:
    286 		/* Fall back to our own `range' construction */
    287 		sc->sc_range = sbus_translations;
    288 		sc->sc_nrange =
    289 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
    290 		break;
    291 #endif
    292 	default:
    293 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    294 	}
    295 
    296 
    297 	/*
    298 	 * Setup the iommu.
    299 	 *
    300 	 * The sun4u iommu is part of the SBUS controller so we will
    301 	 * deal with it here.  We could try to fake a device node so
    302 	 * we can eventually share it with the PCI bus run by psyco,
    303 	 * but I don't want to get into that sort of cruft.
    304 	 */
    305 
    306 	/*
    307 	 * All IOMMUs will share the same TSB which is allocated in pmap_bootstrap.
    308 	 *
    309 	 * This makes device management easier.
    310 	 */
    311 	{
    312 		extern int64_t		*iotsb;
    313 		extern vm_offset_t	iotsbp;
    314 		extern int		iotsbsize;
    315 
    316 		sc->sc_tsbsize = iotsbsize;
    317 		sc->sc_tsb = iotsb;
    318 		sc->sc_ptsb = iotsbp;
    319 	}
    320 #if 0
    321 	/* Need to do 64-bit stores */
    322 	sc->sc_sysio->sys_iommu.iommu_cr = (IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN);
    323 	sc->sc_sysio->sys_iommu.iommu_tsb = sc->sc_ptsb;
    324 #else
    325 	stxa(&sc->sc_sysio->sys_iommu.iommu_cr,ASI_NUCLEUS,(IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
    326 	stxa(&sc->sc_sysio->sys_iommu.iommu_tsb,ASI_NUCLEUS,sc->sc_ptsb);
    327 #endif
    328 #ifdef DEBUG
    329 	if (sbusdebug & SDB_DVMA)
    330 	{
    331 		/* Probe the iommu */
    332 		int64_t cr, tsb;
    333 
    334 		printf("iommu regs at: cr=%x tsb=%x flush=%x\n", &sc->sc_sysio->sys_iommu.iommu_cr,
    335 		       &sc->sc_sysio->sys_iommu.iommu_tsb, &sc->sc_sysio->sys_iommu.iommu_flush);
    336 		cr = sc->sc_sysio->sys_iommu.iommu_cr;
    337 		tsb = sc->sc_sysio->sys_iommu.iommu_tsb;
    338 		printf("iommu cr=%x:%x tsb=%x:%x\n", (long)(cr>>32), (long)cr, (long)(tsb>>32), (long)tsb);
    339 		printf("sysio base %p phys %p TSB base %p phys %p",
    340 		       (long)sc->sc_sysio, (long)pmap_extract(pmap_kernel(), (vm_offset_t)sc->sc_sysio),
    341 		       (long)sc->sc_tsb, (long)sc->sc_ptsb);
    342 		delay(1000000); /* 1 s */
    343 	}
    344 #endif
    345 
    346 	/*
    347 	 * Initialize streaming buffer.
    348 	 */
    349 	sc->sc_flushpa = pmap_extract(pmap_kernel(), &sc->sc_flush);
    350 #if 0
    351 	sc->sc_sysio->sys_strbuf.strbuf_ctl = STRBUF_EN; /* Enable diagnostics mode? */
    352 #else
    353 	stxa(&sc->sc_sysio->sys_strbuf.strbuf_ctl,ASI_NUCLEUS,STRBUF_EN);
    354 #endif
    355 
    356 	/*
    357 	 * Loop through ROM children, fixing any relative addresses
    358 	 * and then configuring each device.
    359 	 * `specials' is an array of device names that are treated
    360 	 * specially:
    361 	 */
    362 	node0 = firstchild(node);
    363 	for (node = node0; node; node = nextsibling(node)) {
    364 		char *name = getpropstring(node, "name");
    365 
    366 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    367 					   node, bp, &sa) != 0) {
    368 			printf("sbus_attach: %s: incomplete\n", name);
    369 			continue;
    370 		}
    371 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    372 	}
    373 }
    374 
    375 int
    376 sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
    377 	struct sbus_softc	*sc;
    378 	bus_space_tag_t		bustag;
    379 	bus_dma_tag_t		dmatag;
    380 	int			node;
    381 	struct bootpath		*bp;
    382 	struct sbus_attach_args	*sa;
    383 {
    384 	struct	sbus_reg romreg;
    385 	int	base;
    386 	int	error;
    387 
    388 	bzero(sa, sizeof(struct sbus_attach_args));
    389 	sa->sa_name = getpropstring(node, "name");
    390 	sa->sa_bustag = bustag;
    391 	sa->sa_dmatag = dmatag;
    392 	sa->sa_node = node;
    393 	sa->sa_bp = bp;
    394 
    395 	if ((error = getprop_reg1(node, &romreg)) != 0)
    396 		return (error);
    397 
    398 	/* We pass only the first "reg" property */
    399 	base = (int)romreg.rr_paddr;
    400 	if (SBUS_ABS(base)) {
    401 		sa->sa_slot = SBUS_ABS_TO_SLOT(base);
    402 		sa->sa_offset = SBUS_ABS_TO_OFFSET(base);
    403 	} else {
    404 		sa->sa_slot = romreg.rr_iospace;
    405 		sa->sa_offset = base;
    406 	}
    407 	sa->sa_size = romreg.rr_len;
    408 
    409 	if ((error = sbus_get_intr(sc, node, &sa->sa_pri)) != 0)
    410 		return (error);
    411 
    412 	if ((error = getprop_address1(node, &sa->sa_promvaddr)) != 0)
    413 		return (error);
    414 
    415 	return (0);
    416 }
    417 
    418 int
    419 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
    420 	bus_space_tag_t t;
    421 	bus_type_t btype;
    422 	bus_addr_t offset;
    423 	bus_size_t size;
    424 	int	flags;
    425 	vm_offset_t vaddr;
    426 	bus_space_handle_t *hp;
    427 {
    428 	struct sbus_softc *sc = t->cookie;
    429 	int64_t slot = btype;
    430 	int i;
    431 
    432 	for (i = 0; i < sc->sc_nrange; i++) {
    433 		bus_addr_t paddr;
    434 
    435 		if (sc->sc_range[i].cspace != slot)
    436 			continue;
    437 
    438 		/* We've found the connection to the parent bus */
    439 		paddr = sc->sc_range[i].poffset + offset;
    440 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    441 #ifdef DEBUG
    442 		if (sbusdebug & SDB_DVMA)
    443 			printf("\n_sbus_bus_map: mapping paddr slot %x offset %x:%x poffset %x paddr %x:%x\n",
    444 			       (int)slot, (int)(offset>>32), (int)offset, (int)sc->sc_range[i].poffset, (int)(paddr>>32), (int)paddr);
    445 #endif
    446 		return (bus_space_map2(sc->sc_bustag, 0, paddr,
    447 					size, flags, vaddr, hp));
    448 	}
    449 
    450 	return (EINVAL);
    451 }
    452 
    453 int
    454 sbus_bus_mmap(t, btype, paddr, flags, hp)
    455 	bus_space_tag_t t;
    456 	bus_type_t btype;
    457 	bus_addr_t paddr;
    458 	int flags;
    459 	bus_space_handle_t *hp;
    460 {
    461 	bus_addr_t offset = paddr;
    462 	int slot = (paddr>>32);
    463 	struct sbus_softc *sc = t->cookie;
    464 	int i;
    465 
    466 	for (i = 0; i < sc->sc_nrange; i++) {
    467 		bus_addr_t paddr;
    468 
    469 		if (sc->sc_range[i].cspace != slot)
    470 			continue;
    471 
    472 		paddr = sc->sc_range[i].poffset + offset;
    473 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    474 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    475 				       flags, hp));
    476 	}
    477 
    478 	return (-1);
    479 }
    480 
    481 
    482 /*
    483  * Each attached device calls sbus_establish after it initializes
    484  * its sbusdev portion.
    485  */
    486 void
    487 sbus_establish(sd, dev)
    488 	register struct sbusdev *sd;
    489 	register struct device *dev;
    490 {
    491 	register struct sbus_softc *sc;
    492 	register struct device *curdev;
    493 
    494 	/*
    495 	 * We have to look for the sbus by name, since it is not necessarily
    496 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    497 	 * We don't just use the device structure of the above-attached
    498 	 * sbus, since we might (in the future) support multiple sbus's.
    499 	 */
    500 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    501 		if (!curdev || !curdev->dv_xname)
    502 			panic("sbus_establish: can't find sbus parent for %s",
    503 			      sd->sd_dev->dv_xname
    504 					? sd->sd_dev->dv_xname
    505 					: "<unknown>" );
    506 
    507 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    508 			break;
    509 	}
    510 	sc = (struct sbus_softc *) curdev;
    511 
    512 	sd->sd_dev = dev;
    513 	sd->sd_bchain = sc->sc_sbdev;
    514 	sc->sc_sbdev = sd;
    515 }
    516 
    517 /*
    518  * Reset the given sbus. (???)
    519  */
    520 void
    521 sbusreset(sbus)
    522 	int sbus;
    523 {
    524 	register struct sbusdev *sd;
    525 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    526 	struct device *dev;
    527 
    528 	printf("reset %s:", sc->sc_dev.dv_xname);
    529 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    530 		if (sd->sd_reset) {
    531 			dev = sd->sd_dev;
    532 			(*sd->sd_reset)(dev);
    533 			printf(" %s", dev->dv_xname);
    534 		}
    535 	}
    536 #if 0
    537 	/* Reload iommu regs */
    538 	sc->sc_sysio->sys_iommu.iommu_cr = (IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN);
    539 	sc->sc_sysio->sys_iommu.iommu_tsb = sc->sc_ptsb;
    540 	sc->sc_sysio->sys_strbuf.strbuf_ctl = STRBUF_EN; /* Enable diagnostics mode? */
    541 #else
    542 	/* Reload iommu regs */
    543 	stxa(&sc->sc_sysio->sys_iommu.iommu_cr,ASI_NUCLEUS,(IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
    544 	stxa(&sc->sc_sysio->sys_iommu.iommu_tsb,ASI_NUCLEUS,sc->sc_ptsb);
    545 	stxa(&sc->sc_sysio->sys_strbuf.strbuf_ctl,ASI_NUCLEUS,STRBUF_EN);
    546 #endif
    547 }
    548 
    549 /*
    550  * Here are the iommu control routines.
    551  */
    552 void
    553 sbus_enter(sc, va, pa)
    554 	struct sbus_softc *sc;
    555 	vm_offset_t va;
    556 	int64_t pa;
    557 {
    558 	int64_t tte;
    559 
    560 #ifdef DIAGNOSTIC
    561 	if (va < sc->sc_dvmabase)
    562 		panic("sbus_enter: va 0x%x not in DVMA space",va);
    563 #endif
    564 
    565 #ifdef 1
    566 	/* Streaming */
    567 	tte = MAKEIOTTE(pa, 1, 1, 1);
    568 #else
    569 	/* Consistent */
    570 	tte = MAKEIOTTE(pa, 1, 1, 0);
    571 #endif
    572 
    573 	/* Is the streamcache flush really needed? */
    574 #if 0
    575 	sc->sc_sysio->sys_strbuf.strbuf_pgflush = va;
    576 #else
    577 	stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
    578 #endif
    579 	sbus_flush(sc);
    580 	sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)] = tte;
    581 #if 0
    582 	sc->sc_sysio->sys_iommu.iommu_flush = va;
    583 #else
    584 	stxa(&sc->sc_sysio->sys_iommu.iommu_flush,ASI_NUCLEUS,va);
    585 #endif
    586 #ifdef DEBUG
    587 	if (sbusdebug & SDB_DVMA)
    588 		printf("sbus_enter: va %x pa %x:%x TSB[%x]@%p=%x:%x\n",
    589 		       va, (int)(pa>>32), (int)pa, IOTSBSLOT(va,sc->sc_tsbsize),
    590 		       &sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)],
    591 		       (int)(tte>>32), (int)tte);
    592 #endif
    593 }
    594 
    595 /*
    596  * sbus_clear: clears mappings created by sbus_enter
    597  *
    598  * Only demap from IOMMU if flag is set.
    599  */
    600 void
    601 sbus_remove(sc, va, len)
    602 	struct sbus_softc *sc;
    603 	register vm_offset_t va;
    604 	register u_int len;
    605 {
    606 
    607 #ifdef DIAGNOSTIC
    608 	if (va < sc->sc_dvmabase)
    609 		panic("sbus_remove: va 0x%x not in DVMA space", va);
    610 #endif
    611 
    612 	while (len > 0) {
    613 
    614 		/*
    615 		 * Streaming buffer flushes:
    616 		 *
    617 		 *   1 Tell strbuf to flush by storing va to strbuf_pgflush
    618 		 * If we're not on a cache line boundary (64-bits):
    619 		 *   2 Store 0 in flag
    620 		 *   3 Store pointer to flag in flushsync
    621 		 *   4 wait till flushsync becomes 0x1
    622 		 *
    623 		 * If it takes more than .5 sec, something went wrong.
    624 		 */
    625 #if 0
    626 		sc->sc_sysio->sys_strbuf.strbuf_pgflush = va;
    627 #else
    628 		stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
    629 #endif
    630 		if (len <= NBPG) {
    631 			sbus_flush(sc);
    632 		}
    633 #ifdef DEBUG
    634 		if (sbusdebug & SDB_DVMA)
    635 			printf("sbus_remove: flushed va %p TSB[%x]@%p=%x:%x\n",
    636 			       (int)va, (int)IOTSBSLOT(va,sc->sc_tsbsize),
    637 			       (int)&sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)],
    638 			       (int)((sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)])>>32),
    639 			       (int)(sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)]));
    640 #endif
    641 		sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)] = 0;
    642 #if 0
    643 		sc->sc_sysio->sys_iommu.iommu_flush = va;
    644 #else
    645 		stxa(&sc->sc_sysio->sys_iommu.iommu_flush, ASI_NUCLEUS, va);
    646 #endif
    647 		len -= NBPG;
    648 		va += NBPG;
    649 	}
    650 }
    651 
    652 int
    653 sbus_flush(sc)
    654 	struct sbus_softc *sc;
    655 {
    656 	extern u_int64_t cpu_clockrate;
    657 	u_int64_t flushtimeout;
    658 
    659 	sc->sc_flush = 0;
    660 	/*
    661 	 * KLUGE ALERT KLUGE ALERT
    662 	 *
    663 	 * In order not to bother with pmap_extract() to do the vtop
    664 	 * translation, flushdone is a static variable that resides in
    665 	 * the kernel's 4MB locked TTE.  This means that this routine
    666 	 * is NOT re-entrant.  Since we're single-threaded and poll
    667 	 * on this value, this is currently not a problem.
    668 	 */
    669 #ifdef NOTDEF_DEBUG
    670 	if (sbusdebug & SDB_DVMA)
    671 		printf("sbus_remove: flush = %x at va = %x pa = %x:%x\n",
    672 		       (int)sc->sc_flush, (int)&sc->sc_flush,
    673 		       (int)(sc->sc_flushpa>>32), (int)sc->sc_flushpa);
    674 #endif
    675 #if 0
    676 	sc->sc_sysio->sys_strbuf.strbuf_flushsync = sc->sc_flushpa;
    677 #else
    678 	stxa(&sc->sc_sysio->sys_strbuf.strbuf_flushsync, ASI_NUCLEUS, sc->sc_flushpa);
    679 #endif
    680 	membar_sync();
    681 	flushtimeout = tick() + cpu_clockrate/2; /* .5 sec after *now* */
    682 	while( !sc->sc_flush && flushtimeout > tick()) membar_sync();
    683 #ifdef DIAGNOSTIC
    684 	if( !sc->sc_flush )
    685 		printf("sbus_remove: flush timeout %x at %x:%x\n", (int)sc->sc_flush,
    686 		       (int)(sc->sc_flushpa>>32), (int)sc->sc_flushpa); /* panic? */
    687 #endif
    688 	return (sc->sc_flush);
    689 }
    690 /*
    691  * Get interrupt attributes for an Sbus device.
    692  */
    693 int
    694 sbus_get_intr(sc, node, ip)
    695 	struct sbus_softc *sc;
    696 	int node;
    697 	int *ip;
    698 {
    699 	struct sbus_intr *rip;
    700 	int *ipl;
    701 	int n;
    702 	char buf[32];
    703 
    704 	/*
    705 	 * The `interrupts' property contains the Sbus interrupt level.
    706 	 */
    707 	ipl = NULL;
    708 	if (getpropA(node, "interrupts", sizeof(int), &n, (void **)&ipl) == 0) {
    709 		/* Now things get ugly.  We need to take this value which is
    710 		 * the interrupt vector number and encode the IPL into it
    711 		 * somehow. Luckily, the interrupt vector has lots of free
    712 		 * space and we can easily stuff the IPL in there for a while.
    713 		 */
    714 		getpropstringA(node, "device_type", buf);
    715 		for (n=0; intrmap[n].in_class; n++) {
    716 			if (strcmp(intrmap[n].in_class, buf) == 0) {
    717 				ipl[0] |= INTLEVENCODE(intrmap[n].in_lev);
    718 				break;
    719 			}
    720 		}
    721 		*ip = ipl[0];
    722 		free(ipl, M_DEVBUF);
    723 		return (0);
    724 	}
    725 
    726 	/* We really don't support the following */
    727 /*	printf("\nWARNING: sbus_get_intr() \"interrupts\" not found -- using \"intr\"\n"); */
    728 /* And some devices don't even have interrupts */
    729 	/*
    730 	 * Fall back on `intr' property.
    731 	 */
    732 	rip = NULL;
    733 	switch (getpropA(node, "intr", sizeof(*rip), &n, (void **)&rip)) {
    734 	case 0:
    735 		getpropstringA(node, "device_type", buf);
    736 		for (n=0; intrmap[n].in_class; n++) {
    737 			if (strcmp(intrmap[n].in_class, buf) == 0) {
    738 				rip[0].int_pri |= INTLEVENCODE(intrmap[n].in_lev);
    739 				break;
    740 			}
    741 		}
    742 		*ip = rip[0].int_pri;
    743 		free(rip, M_DEVBUF);
    744 		return (0);
    745 	case ENOENT:
    746 		*ip = 0;
    747 		return (0);
    748 	}
    749 
    750 	return (-1);
    751 }
    752 
    753 
    754 /*
    755  * Install an interrupt handler for an Sbus device.
    756  */
    757 void *
    758 sbus_intr_establish(t, level, flags, handler, arg)
    759 	bus_space_tag_t t;
    760 	int level;
    761 	int flags;
    762 	int (*handler) __P((void *));
    763 	void *arg;
    764 {
    765 	struct sbus_softc *sc = t->cookie;
    766 	struct intrhand *ih;
    767 	int ipl;
    768 
    769 	ih = (struct intrhand *)
    770 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    771 	if (ih == NULL)
    772 		return (NULL);
    773 
    774 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    775 		ipl = level;
    776 	else if ((level & SBUS_INTR_COMPAT) != 0)
    777 		ipl = level & ~SBUS_INTR_COMPAT;
    778 	else {
    779 		/* Decode and remove IPL */
    780 		ipl = INTLEV(level);
    781 		level = INTVEC(level);
    782 #ifdef DEBUG
    783 		if (sbusdebug & SDB_INTR) {
    784 			printf("\nsbus: intr[%d]%x: %x\n", ipl, level,
    785 			       intrlev[level]);
    786 			printf("Hunting for IRQ...\n");
    787 		}
    788 #endif
    789 		if ((level & INTMAP_OBIO) == 0) {
    790 			/* We're in an SBUS slot */
    791 			/* Register the map and clear intr registers */
    792 #ifdef DEBUG
    793 			if (sbusdebug & SDB_INTR) {
    794 				int64_t *intrptr = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(level)];
    795 				int64_t intrmap = *intrptr;
    796 
    797 				printf("Found SBUS %x IRQ as %x:%x in slot %d\n",
    798 				       level, (int)(intrmap>>32), (int)intrmap,
    799 				       INTSLOT(level));
    800 			}
    801 #endif
    802 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(level)];
    803 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[INTVEC(level)];
    804 			/* Enable the interrupt */
    805 			level |= INTMAP_V;
    806 			stxa(ih->ih_map, ASI_NUCLEUS, level);
    807 		} else {
    808 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    809 			int64_t intrmap = 0;
    810 			int i;
    811 
    812 			/* Insert IGN */
    813 			level |= sc->sc_ign;
    814 			for (i=0;
    815 			     &intrptr[i] <= (int64_t *)&sc->sc_sysio->reserved_int_map &&
    816 				     INTVEC(intrmap=intrptr[i]) != INTVEC(level);
    817 			     i++);
    818 			if (INTVEC(intrmap) == INTVEC(level)) {
    819 #ifdef DEBUG
    820 				if (sbusdebug & SDB_INTR)
    821 					printf("Found OBIO %x IRQ as %x:%x in slot %d\n",
    822 					       level, (int)(intrmap>>32), (int)intrmap,
    823 					       i);
    824 #endif
    825 				/* Register the map and clear intr registers */
    826 				ih->ih_map = &intrptr[i];
    827 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    828 				ih->ih_clr = &intrptr[i];
    829 				/* Enable the interrupt */
    830 				intrmap |= INTMAP_V;
    831 				stxa(ih->ih_map, ASI_NUCLEUS, intrmap);
    832 			} else panic("IRQ not found!");
    833 		}
    834 	}
    835 #ifdef DEBUG
    836 	if (sbusdebug & SDB_INTR) { int i; for (i=0; i<140000000; i++); }
    837 #endif
    838 
    839 	ih->ih_fun = handler;
    840 	ih->ih_arg = arg;
    841 	ih->ih_number = level;
    842 	ih->ih_pil = (1<<ipl);
    843 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
    844 		intr_fasttrap(ipl, (void (*)__P((void)))handler);
    845 	else
    846 		intr_establish(ipl, ih);
    847 	return (ih);
    848 }
    849 
    850 static bus_space_tag_t
    851 sbus_alloc_bustag(sc)
    852 	struct sbus_softc *sc;
    853 {
    854 	bus_space_tag_t sbt;
    855 
    856 	sbt = (bus_space_tag_t)
    857 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    858 	if (sbt == NULL)
    859 		return (NULL);
    860 
    861 	bzero(sbt, sizeof *sbt);
    862 	sbt->cookie = sc;
    863 	sbt->parent = sc->sc_bustag;
    864 	sbt->sparc_bus_map = _sbus_bus_map;
    865 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    866 	sbt->sparc_intr_establish = sbus_intr_establish;
    867 	return (sbt);
    868 }
    869 
    870 
    871 static bus_dma_tag_t
    872 sbus_alloc_dmatag(sc)
    873 	struct sbus_softc *sc;
    874 {
    875 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    876 
    877 	sdt = (bus_dma_tag_t)
    878 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    879 	if (sdt == NULL)
    880 		/* Panic? */
    881 		return (psdt);
    882 
    883 	sdt->_cookie = sc;
    884 	sdt->_parent = psdt;
    885 #define PCOPY(x)	sdt->x = psdt->x
    886 	PCOPY(_dmamap_create);
    887 	PCOPY(_dmamap_destroy);
    888 	sdt->_dmamap_load = sbus_dmamap_load;
    889 	PCOPY(_dmamap_load_mbuf);
    890 	PCOPY(_dmamap_load_uio);
    891 	PCOPY(_dmamap_load_raw);
    892 	sdt->_dmamap_unload = sbus_dmamap_unload;
    893 	sdt->_dmamap_sync = sbus_dmamap_sync;
    894 	sdt->_dmamem_alloc = sbus_dmamem_alloc;
    895 	sdt->_dmamem_free = sbus_dmamem_free;
    896 	PCOPY(_dmamem_map);
    897 	PCOPY(_dmamem_unmap);
    898 	PCOPY(_dmamem_mmap);
    899 #undef	PCOPY
    900 	sc->sc_dmatag = sdt;
    901 	return (sdt);
    902 }
    903 
    904 int
    905 sbus_dmamap_load(t, map, buf, buflen, p, flags)
    906 	bus_dma_tag_t t;
    907 	bus_dmamap_t map;
    908 	void *buf;
    909 	bus_size_t buflen;
    910 	struct proc *p;
    911 	int flags;
    912 {
    913 	int err;
    914 	bus_size_t sgsize;
    915 	bus_addr_t dvmaddr, curaddr;
    916 	caddr_t vaddr = buf;
    917 	pmap_t pmap;
    918 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    919 
    920 	if ((err = bus_dmamap_load(t->_parent, map, buf, buflen, p, flags)))
    921 		return (err);
    922 
    923 	if (p != NULL)
    924 		pmap = p->p_vmspace->vm_map.pmap;
    925 	else
    926 		pmap = pmap_kernel();
    927 
    928 	dvmaddr = map->dm_segs[0].ds_addr;
    929 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    930 	for (; buflen > 0; ) {
    931 		/*
    932 		 * Get the physical address for this page.
    933 		 */
    934 		if ((curaddr = (bus_addr_t)pmap_extract(pmap, (vm_offset_t)vaddr)) == NULL) {
    935 			bus_dmamap_unload(t, map);
    936 			return (-1);
    937 		}
    938 
    939 		/*
    940 		 * Compute the segment size, and adjust counts.
    941 		 */
    942 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    943 		if (buflen < sgsize)
    944 			sgsize = buflen;
    945 
    946 		sbus_enter(sc, dvmaddr, curaddr & ~(NBPG-1));
    947 
    948 		dvmaddr += PAGE_SIZE;
    949 		vaddr += sgsize;
    950 		buflen -= sgsize;
    951 	}
    952 	return (0);
    953 }
    954 
    955 void
    956 sbus_dmamap_unload(t, map)
    957 	bus_dma_tag_t t;
    958 	bus_dmamap_t map;
    959 {
    960 	bus_addr_t addr;
    961 	bus_size_t len;
    962 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    963 
    964 	if (map->dm_nsegs != 1)
    965 		panic("_sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
    966 
    967 	addr = map->dm_segs[0].ds_addr;
    968 	len = map->dm_segs[0].ds_len;
    969 
    970 	sbus_remove(sc, addr, len);
    971 	bus_dmamap_unload(t->_parent, map);
    972 }
    973 
    974 
    975 void
    976 sbus_dmamap_sync(t, map, offset, len, ops)
    977 	bus_dma_tag_t t;
    978 	bus_dmamap_t map;
    979 	bus_addr_t offset;
    980 	bus_size_t len;
    981 	int ops;
    982 {
    983 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    984 	vm_offset_t va = map->dm_segs[0].ds_addr + offset;
    985 
    986 
    987 	/*
    988 	 * We only support one DMA segment; supporting more makes this code
    989          * too unweildy.
    990 	 */
    991 
    992 	if (ops&BUS_DMASYNC_PREREAD)
    993 		/* Nothing to do */;
    994 	if (ops&BUS_DMASYNC_POSTREAD) {
    995 		/*
    996 		 * We should sync the IOMMU streaming caches here first.
    997 		 */
    998 		while (len > 0) {
    999 
   1000 			/*
   1001 			 * Streaming buffer flushes:
   1002 			 *
   1003 			 *   1 Tell strbuf to flush by storing va to strbuf_pgflush
   1004 			 * If we're not on a cache line boundary (64-bits):
   1005 			 *   2 Store 0 in flag
   1006 			 *   3 Store pointer to flag in flushsync
   1007 			 *   4 wait till flushsync becomes 0x1
   1008 			 *
   1009 			 * If it takes more than .5 sec, something went wrong.
   1010 			 */
   1011 #if 0
   1012 			sc->sc_sysio->sys_strbuf.strbuf_pgflush = va;
   1013 #else
   1014 			stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
   1015 #endif
   1016 			if (len <= NBPG) {
   1017 				sbus_flush(sc);
   1018 			}
   1019 			len -= NBPG;
   1020 			va += NBPG;
   1021 		}
   1022 	}
   1023 	if (ops&BUS_DMASYNC_PREWRITE)
   1024 		/* Nothing to do */;
   1025 	if (ops&BUS_DMASYNC_POSTWRITE)
   1026 		/* Nothing to do */;
   1027 	bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1028 }
   1029 
   1030 int
   1031 sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
   1032 	bus_dma_tag_t t;
   1033 	bus_size_t size, alignment, boundary;
   1034 	bus_dma_segment_t *segs;
   1035 	int nsegs;
   1036 	int *rsegs;
   1037 	int flags;
   1038 {
   1039 	vm_offset_t curaddr;
   1040 	bus_addr_t dvmaddr;
   1041 	vm_page_t m;
   1042 	struct pglist *mlist;
   1043 	int error;
   1044 	int n;
   1045 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
   1046 
   1047 	if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
   1048 				     boundary, segs, nsegs, rsegs, flags)))
   1049 		return (error);
   1050 
   1051 	for (n=0; n<*rsegs; n++) {
   1052 		dvmaddr = segs[n].ds_addr;
   1053 		size = segs[n].ds_len;
   1054 		mlist = segs[n]._ds_mlist;
   1055 
   1056 		/* Map memory into DVMA space */
   1057 		for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
   1058 			curaddr = VM_PAGE_TO_PHYS(m);
   1059 			sbus_enter(sc, dvmaddr, curaddr);
   1060 			dvmaddr += PAGE_SIZE;
   1061 		}
   1062 	}
   1063 	return (0);
   1064 }
   1065 
   1066 void
   1067 sbus_dmamem_free(t, segs, nsegs)
   1068 	bus_dma_tag_t t;
   1069 	bus_dma_segment_t *segs;
   1070 	int nsegs;
   1071 {
   1072 	bus_addr_t addr;
   1073 	bus_size_t len;
   1074 	int n;
   1075 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
   1076 
   1077 
   1078 	for (n=0; n<nsegs; n++) {
   1079 		addr = segs[n].ds_addr;
   1080 		len = segs[n].ds_len;
   1081 		sbus_remove(sc, addr, len);
   1082 	}
   1083 	bus_dmamem_free(t->_parent, segs, nsegs);
   1084 }
   1085 
   1086