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