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