Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.19
      1  1.19      eeh /*	$NetBSD: sbus.c,v 1.19 1999/06/20 00:51:30 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.18      eeh  * Copyright (c) 1999 Eduardo Horvath
     85  1.18      eeh  *
     86  1.18      eeh  * Redistribution and use in source and binary forms, with or without
     87  1.18      eeh  * modification, are permitted provided that the following conditions
     88  1.18      eeh  * are met:
     89  1.18      eeh  * 1. Redistributions of source code must retain the above copyright
     90  1.18      eeh  *    notice, this list of conditions and the following disclaimer.
     91  1.18      eeh  *
     92  1.18      eeh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     93  1.18      eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     94  1.18      eeh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     95  1.18      eeh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     96  1.18      eeh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     97  1.18      eeh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     98  1.18      eeh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  1.18      eeh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    100  1.18      eeh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    101  1.18      eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    102  1.18      eeh  * SUCH DAMAGE.
    103  1.18      eeh  *
    104  1.18      eeh  */
    105  1.18      eeh 
    106  1.18      eeh 
    107  1.18      eeh /*
    108   1.1      eeh  * Sbus stuff.
    109   1.1      eeh  */
    110   1.8      eeh #include "opt_ddb.h"
    111   1.1      eeh 
    112   1.1      eeh #include <sys/param.h>
    113  1.12      eeh #include <sys/extent.h>
    114   1.1      eeh #include <sys/malloc.h>
    115   1.1      eeh #include <sys/systm.h>
    116   1.1      eeh #include <sys/device.h>
    117   1.1      eeh #include <vm/vm.h>
    118   1.1      eeh 
    119   1.1      eeh #include <machine/bus.h>
    120   1.2      eeh #include <sparc64/sparc64/vaddrs.h>
    121  1.13      mrg #include <sparc64/dev/iommureg.h>
    122  1.17      mrg #include <sparc64/dev/iommuvar.h>
    123   1.1      eeh #include <sparc64/dev/sbusreg.h>
    124   1.7       pk #include <dev/sbus/sbusvar.h>
    125   1.1      eeh 
    126   1.1      eeh #include <machine/autoconf.h>
    127   1.1      eeh #include <machine/ctlreg.h>
    128   1.1      eeh #include <machine/cpu.h>
    129   1.8      eeh #include <machine/sparc64.h>
    130   1.1      eeh 
    131   1.1      eeh #ifdef DEBUG
    132   1.1      eeh #define SDB_DVMA	0x1
    133   1.1      eeh #define SDB_INTR	0x2
    134   1.1      eeh int sbusdebug = 0;
    135   1.1      eeh #endif
    136   1.1      eeh 
    137   1.1      eeh void sbusreset __P((int));
    138   1.1      eeh 
    139   1.1      eeh static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
    140   1.1      eeh static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
    141   1.3      eeh static int sbus_get_intr __P((struct sbus_softc *, int,
    142   1.3      eeh 			      struct sbus_intr **, int *));
    143   1.1      eeh static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    144   1.1      eeh 			      int, bus_space_handle_t *));
    145   1.1      eeh static int _sbus_bus_map __P((
    146   1.1      eeh 		bus_space_tag_t,
    147   1.1      eeh 		bus_type_t,
    148   1.1      eeh 		bus_addr_t,		/*offset*/
    149   1.1      eeh 		bus_size_t,		/*size*/
    150   1.1      eeh 		int,			/*flags*/
    151   1.3      eeh 		vaddr_t,		/*preferred virtual address */
    152   1.1      eeh 		bus_space_handle_t *));
    153   1.1      eeh static void *sbus_intr_establish __P((
    154   1.1      eeh 		bus_space_tag_t,
    155   1.1      eeh 		int,			/*level*/
    156   1.1      eeh 		int,			/*flags*/
    157   1.1      eeh 		int (*) __P((void *)),	/*handler*/
    158   1.1      eeh 		void *));		/*handler arg*/
    159   1.1      eeh 
    160   1.1      eeh 
    161   1.1      eeh /* autoconfiguration driver */
    162   1.1      eeh int	sbus_match __P((struct device *, struct cfdata *, void *));
    163   1.1      eeh void	sbus_attach __P((struct device *, struct device *, void *));
    164   1.1      eeh 
    165   1.1      eeh 
    166   1.1      eeh struct cfattach sbus_ca = {
    167   1.1      eeh 	sizeof(struct sbus_softc), sbus_match, sbus_attach
    168   1.1      eeh };
    169   1.1      eeh 
    170   1.1      eeh extern struct cfdriver sbus_cd;
    171   1.1      eeh 
    172   1.1      eeh /*
    173   1.1      eeh  * DVMA routines
    174   1.1      eeh  */
    175   1.1      eeh int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    176   1.1      eeh 			  bus_size_t, struct proc *, int));
    177   1.1      eeh void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    178   1.1      eeh void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    179   1.1      eeh 			   bus_size_t, int));
    180   1.1      eeh int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    181   1.1      eeh 			   bus_size_t alignment, bus_size_t boundary,
    182   1.1      eeh 			   bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    183   1.1      eeh void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    184   1.1      eeh 			   int nsegs));
    185   1.2      eeh int sbus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    186   1.2      eeh 			 int nsegs, size_t size, caddr_t *kvap, int flags));
    187   1.2      eeh void sbus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
    188   1.2      eeh 			    size_t size));
    189   1.1      eeh 
    190   1.1      eeh 
    191   1.1      eeh /*
    192   1.1      eeh  * Child devices receive the Sbus interrupt level in their attach
    193   1.1      eeh  * arguments. We translate these to CPU IPLs using the following
    194   1.1      eeh  * tables. Note: obio bus interrupt levels are identical to the
    195   1.1      eeh  * processor IPL.
    196   1.1      eeh  *
    197   1.1      eeh  * The second set of tables is used when the Sbus interrupt level
    198   1.1      eeh  * cannot be had from the PROM as an `interrupt' property. We then
    199   1.1      eeh  * fall back on the `intr' property which contains the CPU IPL.
    200   1.1      eeh  */
    201   1.1      eeh 
    202   1.1      eeh /* Translate Sbus interrupt level to processor IPL */
    203   1.1      eeh static int intr_sbus2ipl_4c[] = {
    204   1.1      eeh 	0, 1, 2, 3, 5, 7, 8, 9
    205   1.1      eeh };
    206   1.1      eeh static int intr_sbus2ipl_4m[] = {
    207   1.1      eeh 	0, 2, 3, 5, 7, 9, 11, 13
    208   1.1      eeh };
    209   1.1      eeh 
    210   1.1      eeh /*
    211   1.1      eeh  * This value is or'ed into the attach args' interrupt level cookie
    212   1.1      eeh  * if the interrupt level comes from an `intr' property, i.e. it is
    213   1.1      eeh  * not an Sbus interrupt level.
    214   1.1      eeh  */
    215   1.1      eeh #define SBUS_INTR_COMPAT	0x80000000
    216   1.1      eeh 
    217   1.1      eeh 
    218   1.1      eeh /*
    219   1.1      eeh  * Print the location of some sbus-attached device (called just
    220   1.1      eeh  * before attaching that device).  If `sbus' is not NULL, the
    221   1.1      eeh  * device was found but not configured; print the sbus as well.
    222   1.1      eeh  * Return UNCONF (config_find ignores this if the device was configured).
    223   1.1      eeh  */
    224   1.1      eeh int
    225   1.1      eeh sbus_print(args, busname)
    226   1.1      eeh 	void *args;
    227   1.1      eeh 	const char *busname;
    228   1.1      eeh {
    229   1.1      eeh 	struct sbus_attach_args *sa = args;
    230   1.3      eeh 	int i;
    231   1.1      eeh 
    232   1.1      eeh 	if (busname)
    233   1.1      eeh 		printf("%s at %s", sa->sa_name, busname);
    234   1.8      eeh 	printf(" slot %ld offset 0x%lx", (long)sa->sa_slot,
    235   1.8      eeh 	       (u_long)sa->sa_offset);
    236   1.3      eeh 	for (i=0; i<sa->sa_nintr; i++) {
    237   1.3      eeh 		struct sbus_intr *sbi = &sa->sa_intr[i];
    238   1.1      eeh 
    239   1.8      eeh 		printf(" vector %lx ipl %ld",
    240   1.8      eeh 		       (u_long)sbi->sbi_vec,
    241   1.8      eeh 		       (long)INTLEV(sbi->sbi_pri));
    242   1.1      eeh 	}
    243   1.1      eeh 	return (UNCONF);
    244   1.1      eeh }
    245   1.1      eeh 
    246   1.1      eeh int
    247   1.1      eeh sbus_match(parent, cf, aux)
    248   1.1      eeh 	struct device *parent;
    249   1.1      eeh 	struct cfdata *cf;
    250   1.1      eeh 	void *aux;
    251   1.1      eeh {
    252   1.1      eeh 	struct mainbus_attach_args *ma = aux;
    253   1.1      eeh 
    254   1.1      eeh 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    255   1.1      eeh }
    256   1.1      eeh 
    257   1.1      eeh /*
    258   1.1      eeh  * Attach an Sbus.
    259   1.1      eeh  */
    260   1.1      eeh void
    261   1.1      eeh sbus_attach(parent, self, aux)
    262   1.1      eeh 	struct device *parent;
    263   1.1      eeh 	struct device *self;
    264   1.1      eeh 	void *aux;
    265   1.1      eeh {
    266   1.9      eeh 	struct sbus_softc *sc = (struct sbus_softc *)self;
    267   1.1      eeh 	struct mainbus_attach_args *ma = aux;
    268   1.1      eeh 	int node = ma->ma_node;
    269   1.1      eeh 
    270   1.1      eeh 	int node0, error;
    271   1.1      eeh 	bus_space_tag_t sbt;
    272   1.1      eeh 	struct sbus_attach_args sa;
    273   1.1      eeh 	char *busname = "sbus";
    274   1.1      eeh 	struct bootpath *bp = ma->ma_bp;
    275   1.1      eeh 
    276   1.1      eeh 
    277   1.1      eeh 	sc->sc_bustag = ma->ma_bustag;
    278   1.1      eeh 	sc->sc_dmatag = ma->ma_dmatag;
    279   1.8      eeh 	sc->sc_sysio = (struct sysioreg*)(u_long)ma->ma_address[0];	/* Use prom mapping for sysio. */
    280   1.1      eeh 	sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;		/* Find interrupt group no */
    281   1.1      eeh 
    282   1.1      eeh 	/* Setup interrupt translation tables */
    283   1.1      eeh 	sc->sc_intr2ipl = CPU_ISSUN4C
    284   1.1      eeh 				? intr_sbus2ipl_4c
    285   1.1      eeh 				: intr_sbus2ipl_4m;
    286   1.1      eeh 
    287   1.1      eeh 	/*
    288   1.1      eeh 	 * Record clock frequency for synchronous SCSI.
    289   1.1      eeh 	 * IS THIS THE CORRECT DEFAULT??
    290   1.1      eeh 	 */
    291   1.1      eeh 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    292   1.1      eeh 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    293   1.1      eeh 
    294   1.1      eeh 	sbt = sbus_alloc_bustag(sc);
    295   1.1      eeh 	sc->sc_dmatag = sbus_alloc_dmatag(sc);
    296   1.1      eeh 
    297   1.1      eeh 	/*
    298   1.1      eeh 	 * Get the SBus burst transfer size if burst transfers are supported
    299   1.1      eeh 	 */
    300   1.1      eeh 	sc->sc_burst = getpropint(node, "burst-sizes", 0);
    301   1.1      eeh 
    302   1.1      eeh 	/* Propagate bootpath */
    303   1.1      eeh 	if (bp != NULL && strcmp(bp->name, busname) == 0)
    304   1.1      eeh 		bp++;
    305   1.1      eeh 	else
    306   1.1      eeh 		bp = NULL;
    307   1.1      eeh 
    308   1.1      eeh 	/*
    309   1.1      eeh 	 * Collect address translations from the OBP.
    310   1.1      eeh 	 */
    311   1.6       pk 	error = getprop(node, "ranges", sizeof(struct sbus_range),
    312   1.1      eeh 			 &sc->sc_nrange, (void **)&sc->sc_range);
    313  1.16      eeh 	if (error)
    314   1.1      eeh 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    315   1.1      eeh 
    316  1.17      mrg 	/* initailise the IOMMU */
    317  1.17      mrg 
    318  1.17      mrg 	/* punch in our copies */
    319  1.17      mrg 	sc->sc_is.is_bustag = sc->sc_bustag;
    320  1.17      mrg 	sc->sc_is.is_iommu = &sc->sc_sysio->sys_iommu;
    321  1.17      mrg 	sc->sc_is.is_sb = &sc->sc_sysio->sys_strbuf;
    322  1.16      eeh 
    323   1.1      eeh #ifdef DEBUG
    324   1.1      eeh 	if (sbusdebug & SDB_DVMA)
    325  1.17      mrg 		printf("sysio base %p phys %p\n",
    326  1.17      mrg 		       (long)sc->sc_sysio, (long)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_sysio));
    327   1.1      eeh #endif
    328   1.1      eeh 
    329  1.17      mrg 	/* XXX should have instance number */
    330  1.17      mrg 	iommu_init("SBus dvma", &sc->sc_is, 0);
    331  1.12      eeh 
    332  1.12      eeh 	/*
    333   1.1      eeh 	 * Loop through ROM children, fixing any relative addresses
    334   1.1      eeh 	 * and then configuring each device.
    335   1.1      eeh 	 * `specials' is an array of device names that are treated
    336   1.1      eeh 	 * specially:
    337   1.1      eeh 	 */
    338   1.1      eeh 	node0 = firstchild(node);
    339   1.1      eeh 	for (node = node0; node; node = nextsibling(node)) {
    340   1.1      eeh 		char *name = getpropstring(node, "name");
    341   1.1      eeh 
    342   1.1      eeh 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    343   1.1      eeh 					   node, bp, &sa) != 0) {
    344   1.1      eeh 			printf("sbus_attach: %s: incomplete\n", name);
    345   1.1      eeh 			continue;
    346   1.1      eeh 		}
    347   1.1      eeh 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    348   1.3      eeh 		sbus_destroy_attach_args(&sa);
    349   1.1      eeh 	}
    350   1.1      eeh }
    351   1.1      eeh 
    352   1.1      eeh int
    353   1.1      eeh sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
    354   1.1      eeh 	struct sbus_softc	*sc;
    355   1.1      eeh 	bus_space_tag_t		bustag;
    356   1.1      eeh 	bus_dma_tag_t		dmatag;
    357   1.1      eeh 	int			node;
    358   1.1      eeh 	struct bootpath		*bp;
    359   1.1      eeh 	struct sbus_attach_args	*sa;
    360   1.1      eeh {
    361   1.3      eeh 	/*struct	sbus_reg sbusreg;*/
    362   1.3      eeh 	/*int	base;*/
    363   1.1      eeh 	int	error;
    364   1.3      eeh 	int n;
    365   1.1      eeh 
    366   1.1      eeh 	bzero(sa, sizeof(struct sbus_attach_args));
    367   1.6       pk 	error = getprop(node, "name", 1, &n, (void **)&sa->sa_name);
    368   1.3      eeh 	if (error != 0)
    369   1.3      eeh 		return (error);
    370   1.3      eeh 	sa->sa_name[n] = '\0';
    371   1.3      eeh 
    372   1.1      eeh 	sa->sa_bustag = bustag;
    373   1.1      eeh 	sa->sa_dmatag = dmatag;
    374   1.1      eeh 	sa->sa_node = node;
    375   1.1      eeh 	sa->sa_bp = bp;
    376   1.1      eeh 
    377   1.6       pk 	error = getprop(node, "reg", sizeof(struct sbus_reg),
    378   1.3      eeh 			 &sa->sa_nreg, (void **)&sa->sa_reg);
    379   1.3      eeh 	if (error != 0) {
    380   1.3      eeh 		char buf[32];
    381   1.3      eeh 		if (error != ENOENT ||
    382   1.3      eeh 		    !node_has_property(node, "device_type") ||
    383   1.3      eeh 		    strcmp(getpropstringA(node, "device_type", buf),
    384   1.3      eeh 			   "hierarchical") != 0)
    385   1.3      eeh 			return (error);
    386   1.3      eeh 	}
    387   1.3      eeh 	for (n = 0; n < sa->sa_nreg; n++) {
    388   1.3      eeh 		/* Convert to relative addressing, if necessary */
    389   1.3      eeh 		u_int32_t base = sa->sa_reg[n].sbr_offset;
    390   1.3      eeh 		if (SBUS_ABS(base)) {
    391   1.3      eeh 			sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
    392   1.3      eeh 			sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
    393   1.3      eeh 		}
    394   1.1      eeh 	}
    395   1.1      eeh 
    396   1.3      eeh 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
    397   1.1      eeh 		return (error);
    398   1.1      eeh 
    399   1.6       pk 	error = getprop(node, "address", sizeof(u_int32_t),
    400   1.3      eeh 			 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
    401   1.3      eeh 	if (error != 0 && error != ENOENT)
    402   1.1      eeh 		return (error);
    403   1.1      eeh 
    404   1.1      eeh 	return (0);
    405   1.1      eeh }
    406   1.1      eeh 
    407   1.3      eeh void
    408   1.3      eeh sbus_destroy_attach_args(sa)
    409   1.3      eeh 	struct sbus_attach_args	*sa;
    410   1.3      eeh {
    411   1.3      eeh 	if (sa->sa_name != NULL)
    412   1.3      eeh 		free(sa->sa_name, M_DEVBUF);
    413   1.3      eeh 
    414   1.3      eeh 	if (sa->sa_nreg != 0)
    415   1.3      eeh 		free(sa->sa_reg, M_DEVBUF);
    416   1.3      eeh 
    417   1.3      eeh 	if (sa->sa_intr)
    418   1.3      eeh 		free(sa->sa_intr, M_DEVBUF);
    419   1.3      eeh 
    420   1.3      eeh 	if (sa->sa_promvaddrs)
    421   1.8      eeh 		free((void *)sa->sa_promvaddrs, M_DEVBUF);
    422   1.3      eeh 
    423   1.3      eeh 	bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
    424   1.3      eeh }
    425   1.3      eeh 
    426   1.3      eeh 
    427   1.1      eeh int
    428   1.1      eeh _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
    429   1.1      eeh 	bus_space_tag_t t;
    430   1.1      eeh 	bus_type_t btype;
    431   1.1      eeh 	bus_addr_t offset;
    432   1.1      eeh 	bus_size_t size;
    433   1.1      eeh 	int	flags;
    434   1.3      eeh 	vaddr_t vaddr;
    435   1.1      eeh 	bus_space_handle_t *hp;
    436   1.1      eeh {
    437   1.1      eeh 	struct sbus_softc *sc = t->cookie;
    438   1.1      eeh 	int64_t slot = btype;
    439   1.1      eeh 	int i;
    440   1.1      eeh 
    441   1.1      eeh 	for (i = 0; i < sc->sc_nrange; i++) {
    442   1.1      eeh 		bus_addr_t paddr;
    443   1.1      eeh 
    444   1.1      eeh 		if (sc->sc_range[i].cspace != slot)
    445   1.1      eeh 			continue;
    446   1.1      eeh 
    447   1.1      eeh 		/* We've found the connection to the parent bus */
    448   1.1      eeh 		paddr = sc->sc_range[i].poffset + offset;
    449   1.1      eeh 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    450   1.1      eeh #ifdef DEBUG
    451   1.1      eeh 		if (sbusdebug & SDB_DVMA)
    452   1.8      eeh 			printf("\n_sbus_bus_map: mapping paddr slot %lx offset %lx poffset %lx paddr %lx\n",
    453   1.8      eeh 			       (long)slot, (long)offset, (long)sc->sc_range[i].poffset, (long)paddr);
    454   1.1      eeh #endif
    455   1.1      eeh 		return (bus_space_map2(sc->sc_bustag, 0, paddr,
    456   1.1      eeh 					size, flags, vaddr, hp));
    457   1.1      eeh 	}
    458   1.1      eeh 
    459   1.1      eeh 	return (EINVAL);
    460   1.1      eeh }
    461   1.1      eeh 
    462   1.1      eeh int
    463   1.1      eeh sbus_bus_mmap(t, btype, paddr, flags, hp)
    464   1.1      eeh 	bus_space_tag_t t;
    465   1.1      eeh 	bus_type_t btype;
    466   1.1      eeh 	bus_addr_t paddr;
    467   1.1      eeh 	int flags;
    468   1.1      eeh 	bus_space_handle_t *hp;
    469   1.1      eeh {
    470   1.1      eeh 	bus_addr_t offset = paddr;
    471   1.1      eeh 	int slot = (paddr>>32);
    472   1.1      eeh 	struct sbus_softc *sc = t->cookie;
    473   1.1      eeh 	int i;
    474   1.1      eeh 
    475   1.1      eeh 	for (i = 0; i < sc->sc_nrange; i++) {
    476   1.1      eeh 		bus_addr_t paddr;
    477   1.1      eeh 
    478   1.1      eeh 		if (sc->sc_range[i].cspace != slot)
    479   1.1      eeh 			continue;
    480   1.1      eeh 
    481   1.1      eeh 		paddr = sc->sc_range[i].poffset + offset;
    482   1.1      eeh 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
    483   1.1      eeh 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    484   1.1      eeh 				       flags, hp));
    485   1.1      eeh 	}
    486   1.1      eeh 
    487   1.1      eeh 	return (-1);
    488   1.1      eeh }
    489   1.1      eeh 
    490   1.1      eeh 
    491   1.1      eeh /*
    492   1.1      eeh  * Each attached device calls sbus_establish after it initializes
    493   1.1      eeh  * its sbusdev portion.
    494   1.1      eeh  */
    495   1.1      eeh void
    496   1.1      eeh sbus_establish(sd, dev)
    497   1.1      eeh 	register struct sbusdev *sd;
    498   1.1      eeh 	register struct device *dev;
    499   1.1      eeh {
    500   1.1      eeh 	register struct sbus_softc *sc;
    501   1.1      eeh 	register struct device *curdev;
    502   1.1      eeh 
    503   1.1      eeh 	/*
    504   1.1      eeh 	 * We have to look for the sbus by name, since it is not necessarily
    505   1.1      eeh 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    506   1.1      eeh 	 * We don't just use the device structure of the above-attached
    507   1.1      eeh 	 * sbus, since we might (in the future) support multiple sbus's.
    508   1.1      eeh 	 */
    509   1.1      eeh 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    510   1.1      eeh 		if (!curdev || !curdev->dv_xname)
    511   1.1      eeh 			panic("sbus_establish: can't find sbus parent for %s",
    512   1.1      eeh 			      sd->sd_dev->dv_xname
    513   1.1      eeh 					? sd->sd_dev->dv_xname
    514   1.1      eeh 					: "<unknown>" );
    515   1.1      eeh 
    516   1.1      eeh 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    517   1.1      eeh 			break;
    518   1.1      eeh 	}
    519   1.1      eeh 	sc = (struct sbus_softc *) curdev;
    520   1.1      eeh 
    521   1.1      eeh 	sd->sd_dev = dev;
    522   1.1      eeh 	sd->sd_bchain = sc->sc_sbdev;
    523   1.1      eeh 	sc->sc_sbdev = sd;
    524   1.1      eeh }
    525   1.1      eeh 
    526   1.1      eeh /*
    527   1.1      eeh  * Reset the given sbus. (???)
    528   1.1      eeh  */
    529   1.1      eeh void
    530   1.1      eeh sbusreset(sbus)
    531   1.1      eeh 	int sbus;
    532   1.1      eeh {
    533   1.1      eeh 	register struct sbusdev *sd;
    534   1.1      eeh 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    535   1.1      eeh 	struct device *dev;
    536   1.1      eeh 
    537   1.1      eeh 	printf("reset %s:", sc->sc_dev.dv_xname);
    538   1.1      eeh 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    539   1.1      eeh 		if (sd->sd_reset) {
    540   1.1      eeh 			dev = sd->sd_dev;
    541   1.1      eeh 			(*sd->sd_reset)(dev);
    542   1.1      eeh 			printf(" %s", dev->dv_xname);
    543   1.1      eeh 		}
    544   1.1      eeh 	}
    545   1.1      eeh 	/* Reload iommu regs */
    546  1.17      mrg 	iommu_reset(&sc->sc_is);
    547   1.1      eeh }
    548   1.1      eeh 
    549   1.1      eeh /*
    550   1.1      eeh  * Get interrupt attributes for an Sbus device.
    551   1.1      eeh  */
    552   1.1      eeh int
    553   1.3      eeh sbus_get_intr(sc, node, ipp, np)
    554   1.1      eeh 	struct sbus_softc *sc;
    555   1.1      eeh 	int node;
    556   1.3      eeh 	struct sbus_intr **ipp;
    557   1.3      eeh 	int *np;
    558   1.1      eeh {
    559   1.1      eeh 	int *ipl;
    560   1.3      eeh 	int i, n, error;
    561   1.1      eeh 	char buf[32];
    562   1.1      eeh 
    563   1.1      eeh 	/*
    564   1.1      eeh 	 * The `interrupts' property contains the Sbus interrupt level.
    565   1.1      eeh 	 */
    566   1.1      eeh 	ipl = NULL;
    567   1.6       pk 	if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
    568   1.3      eeh 		/* Change format to an `struct sbus_intr' array */
    569   1.3      eeh 		struct sbus_intr *ip;
    570  1.10      eeh 		/* Default to interrupt level 2 -- otherwise unused */
    571  1.10      eeh 		int pri = INTLEVENCODE(2);
    572   1.3      eeh 		ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
    573   1.3      eeh 		if (ip == NULL)
    574   1.3      eeh 			return (ENOMEM);
    575   1.1      eeh 		/* Now things get ugly.  We need to take this value which is
    576   1.1      eeh 		 * the interrupt vector number and encode the IPL into it
    577   1.1      eeh 		 * somehow. Luckily, the interrupt vector has lots of free
    578   1.1      eeh 		 * space and we can easily stuff the IPL in there for a while.
    579   1.1      eeh 		 */
    580   1.1      eeh 		getpropstringA(node, "device_type", buf);
    581  1.10      eeh 		if (!buf[0]) {
    582  1.10      eeh 			getpropstringA(node, "name", buf);
    583  1.10      eeh 		}
    584   1.3      eeh 		for (i=0; intrmap[i].in_class; i++) {
    585   1.3      eeh 			if (strcmp(intrmap[i].in_class, buf) == 0) {
    586   1.3      eeh 				pri = INTLEVENCODE(intrmap[i].in_lev);
    587   1.1      eeh 				break;
    588   1.1      eeh 			}
    589   1.1      eeh 		}
    590   1.3      eeh 		for (n = 0; n < *np; n++) {
    591   1.3      eeh 			/*
    592   1.3      eeh 			 * We encode vector and priority into sbi_pri so we
    593   1.3      eeh 			 * can pass them as a unit.  This will go away if
    594   1.3      eeh 			 * sbus_establish ever takes an sbus_intr instead
    595   1.3      eeh 			 * of an integer level.
    596   1.3      eeh 			 * Stuff the real vector in sbi_vec.
    597   1.3      eeh 			 */
    598   1.3      eeh 			ip[n].sbi_pri = pri|ipl[n];
    599   1.3      eeh 			ip[n].sbi_vec = ipl[n];
    600   1.3      eeh 		}
    601   1.1      eeh 		free(ipl, M_DEVBUF);
    602   1.3      eeh 		*ipp = ip;
    603   1.1      eeh 		return (0);
    604   1.1      eeh 	}
    605   1.1      eeh 
    606   1.1      eeh 	/* We really don't support the following */
    607   1.1      eeh /*	printf("\nWARNING: sbus_get_intr() \"interrupts\" not found -- using \"intr\"\n"); */
    608   1.1      eeh /* And some devices don't even have interrupts */
    609   1.1      eeh 	/*
    610   1.1      eeh 	 * Fall back on `intr' property.
    611   1.1      eeh 	 */
    612   1.3      eeh 	*ipp = NULL;
    613   1.6       pk 	error = getprop(node, "intr", sizeof(struct sbus_intr),
    614   1.3      eeh 			 np, (void **)ipp);
    615   1.3      eeh 	switch (error) {
    616   1.1      eeh 	case 0:
    617   1.3      eeh 		for (n = *np; n-- > 0;) {
    618   1.3      eeh 			/*
    619   1.3      eeh 			 * Move the interrupt vector into place.
    620   1.3      eeh 			 * We could remap the level, but the SBUS priorities
    621   1.3      eeh 			 * are probably good enough.
    622   1.3      eeh 			 */
    623   1.3      eeh 			(*ipp)[n].sbi_vec = (*ipp)[n].sbi_pri;
    624   1.3      eeh 			(*ipp)[n].sbi_pri |= INTLEVENCODE((*ipp)[n].sbi_pri);
    625   1.1      eeh 		}
    626   1.3      eeh 		break;
    627   1.1      eeh 	case ENOENT:
    628   1.3      eeh 		error = 0;
    629   1.3      eeh 		break;
    630   1.1      eeh 	}
    631   1.1      eeh 
    632   1.3      eeh 	return (error);
    633   1.1      eeh }
    634   1.1      eeh 
    635   1.1      eeh 
    636   1.1      eeh /*
    637   1.1      eeh  * Install an interrupt handler for an Sbus device.
    638   1.1      eeh  */
    639   1.1      eeh void *
    640   1.1      eeh sbus_intr_establish(t, level, flags, handler, arg)
    641   1.1      eeh 	bus_space_tag_t t;
    642   1.1      eeh 	int level;
    643   1.1      eeh 	int flags;
    644   1.1      eeh 	int (*handler) __P((void *));
    645   1.1      eeh 	void *arg;
    646   1.1      eeh {
    647   1.1      eeh 	struct sbus_softc *sc = t->cookie;
    648   1.1      eeh 	struct intrhand *ih;
    649   1.1      eeh 	int ipl;
    650   1.8      eeh 	long vec = level;
    651   1.1      eeh 
    652   1.1      eeh 	ih = (struct intrhand *)
    653   1.1      eeh 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    654   1.1      eeh 	if (ih == NULL)
    655   1.1      eeh 		return (NULL);
    656   1.1      eeh 
    657   1.1      eeh 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    658   1.8      eeh 		ipl = vec;
    659   1.8      eeh 	else if ((vec & SBUS_INTR_COMPAT) != 0)
    660   1.8      eeh 		ipl = vec & ~SBUS_INTR_COMPAT;
    661   1.1      eeh 	else {
    662   1.1      eeh 		/* Decode and remove IPL */
    663   1.8      eeh 		ipl = INTLEV(vec);
    664   1.8      eeh 		vec = INTVEC(vec);
    665   1.1      eeh #ifdef DEBUG
    666   1.1      eeh 		if (sbusdebug & SDB_INTR) {
    667   1.8      eeh 			printf("\nsbus: intr[%ld]%lx: %lx\n", (long)ipl, (long)vec,
    668   1.8      eeh 			       intrlev[vec]);
    669   1.1      eeh 			printf("Hunting for IRQ...\n");
    670   1.1      eeh 		}
    671   1.1      eeh #endif
    672   1.8      eeh 		if ((vec & INTMAP_OBIO) == 0) {
    673   1.1      eeh 			/* We're in an SBUS slot */
    674   1.1      eeh 			/* Register the map and clear intr registers */
    675   1.1      eeh #ifdef DEBUG
    676   1.1      eeh 			if (sbusdebug & SDB_INTR) {
    677   1.8      eeh 				int64_t *intrptr = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
    678   1.1      eeh 				int64_t intrmap = *intrptr;
    679   1.1      eeh 
    680   1.8      eeh 				printf("Found SBUS %lx IRQ as %llx in slot %ld\n",
    681   1.8      eeh 				       (long)vec, (long)intrmap,
    682   1.8      eeh 				       (long)INTSLOT(vec));
    683   1.1      eeh 			}
    684   1.1      eeh #endif
    685   1.8      eeh 			ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
    686   1.8      eeh 			ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[INTVEC(vec)];
    687   1.1      eeh 			/* Enable the interrupt */
    688   1.8      eeh 			vec |= INTMAP_V;
    689   1.9      eeh 			/* Insert IGN */
    690   1.9      eeh 			vec |= sc->sc_ign;
    691   1.8      eeh 			bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, vec);
    692   1.1      eeh 		} else {
    693   1.1      eeh 			int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
    694   1.1      eeh 			int64_t intrmap = 0;
    695   1.1      eeh 			int i;
    696   1.1      eeh 
    697   1.1      eeh 			/* Insert IGN */
    698   1.8      eeh 			vec |= sc->sc_ign;
    699   1.1      eeh 			for (i=0;
    700   1.1      eeh 			     &intrptr[i] <= (int64_t *)&sc->sc_sysio->reserved_int_map &&
    701   1.8      eeh 				     INTVEC(intrmap=intrptr[i]) != INTVEC(vec);
    702   1.1      eeh 			     i++);
    703   1.8      eeh 			if (INTVEC(intrmap) == INTVEC(vec)) {
    704   1.1      eeh #ifdef DEBUG
    705   1.1      eeh 				if (sbusdebug & SDB_INTR)
    706   1.8      eeh 					printf("Found OBIO %lx IRQ as %lx in slot %d\n",
    707   1.8      eeh 					       vec, (long)intrmap, i);
    708   1.1      eeh #endif
    709   1.1      eeh 				/* Register the map and clear intr registers */
    710   1.1      eeh 				ih->ih_map = &intrptr[i];
    711   1.1      eeh 				intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
    712   1.1      eeh 				ih->ih_clr = &intrptr[i];
    713   1.1      eeh 				/* Enable the interrupt */
    714   1.1      eeh 				intrmap |= INTMAP_V;
    715   1.8      eeh 				bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, (u_long)intrmap);
    716   1.1      eeh 			} else panic("IRQ not found!");
    717   1.1      eeh 		}
    718   1.1      eeh 	}
    719   1.1      eeh #ifdef DEBUG
    720   1.8      eeh 	if (sbusdebug & SDB_INTR) { long i; for (i=0; i<1400000000; i++); }
    721   1.1      eeh #endif
    722   1.1      eeh 
    723   1.1      eeh 	ih->ih_fun = handler;
    724   1.1      eeh 	ih->ih_arg = arg;
    725   1.8      eeh 	ih->ih_number = vec;
    726   1.1      eeh 	ih->ih_pil = (1<<ipl);
    727  1.18      eeh 	intr_establish(ipl, ih);
    728   1.1      eeh 	return (ih);
    729   1.1      eeh }
    730   1.1      eeh 
    731   1.1      eeh static bus_space_tag_t
    732   1.1      eeh sbus_alloc_bustag(sc)
    733   1.1      eeh 	struct sbus_softc *sc;
    734   1.1      eeh {
    735   1.1      eeh 	bus_space_tag_t sbt;
    736   1.1      eeh 
    737   1.1      eeh 	sbt = (bus_space_tag_t)
    738   1.1      eeh 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    739   1.1      eeh 	if (sbt == NULL)
    740   1.1      eeh 		return (NULL);
    741   1.1      eeh 
    742   1.1      eeh 	bzero(sbt, sizeof *sbt);
    743   1.1      eeh 	sbt->cookie = sc;
    744   1.1      eeh 	sbt->parent = sc->sc_bustag;
    745  1.12      eeh 	sbt->type = SBUS_BUS_SPACE;
    746   1.1      eeh 	sbt->sparc_bus_map = _sbus_bus_map;
    747   1.1      eeh 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    748   1.1      eeh 	sbt->sparc_intr_establish = sbus_intr_establish;
    749   1.1      eeh 	return (sbt);
    750   1.1      eeh }
    751   1.1      eeh 
    752   1.1      eeh 
    753   1.1      eeh static bus_dma_tag_t
    754   1.1      eeh sbus_alloc_dmatag(sc)
    755   1.1      eeh 	struct sbus_softc *sc;
    756   1.1      eeh {
    757   1.1      eeh 	bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
    758   1.1      eeh 
    759   1.1      eeh 	sdt = (bus_dma_tag_t)
    760   1.1      eeh 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    761   1.1      eeh 	if (sdt == NULL)
    762   1.1      eeh 		/* Panic? */
    763   1.1      eeh 		return (psdt);
    764   1.1      eeh 
    765   1.1      eeh 	sdt->_cookie = sc;
    766   1.1      eeh 	sdt->_parent = psdt;
    767   1.1      eeh #define PCOPY(x)	sdt->x = psdt->x
    768   1.1      eeh 	PCOPY(_dmamap_create);
    769   1.1      eeh 	PCOPY(_dmamap_destroy);
    770   1.1      eeh 	sdt->_dmamap_load = sbus_dmamap_load;
    771   1.1      eeh 	PCOPY(_dmamap_load_mbuf);
    772   1.1      eeh 	PCOPY(_dmamap_load_uio);
    773   1.1      eeh 	PCOPY(_dmamap_load_raw);
    774   1.1      eeh 	sdt->_dmamap_unload = sbus_dmamap_unload;
    775   1.1      eeh 	sdt->_dmamap_sync = sbus_dmamap_sync;
    776   1.1      eeh 	sdt->_dmamem_alloc = sbus_dmamem_alloc;
    777   1.1      eeh 	sdt->_dmamem_free = sbus_dmamem_free;
    778   1.2      eeh 	sdt->_dmamem_map = sbus_dmamem_map;
    779   1.2      eeh 	sdt->_dmamem_unmap = sbus_dmamem_unmap;
    780   1.1      eeh 	PCOPY(_dmamem_mmap);
    781   1.1      eeh #undef	PCOPY
    782   1.1      eeh 	sc->sc_dmatag = sdt;
    783   1.1      eeh 	return (sdt);
    784   1.1      eeh }
    785   1.1      eeh 
    786   1.1      eeh int
    787   1.1      eeh sbus_dmamap_load(t, map, buf, buflen, p, flags)
    788   1.1      eeh 	bus_dma_tag_t t;
    789   1.1      eeh 	bus_dmamap_t map;
    790   1.1      eeh 	void *buf;
    791   1.1      eeh 	bus_size_t buflen;
    792   1.1      eeh 	struct proc *p;
    793   1.1      eeh 	int flags;
    794   1.1      eeh {
    795  1.12      eeh 	int err, s;
    796   1.1      eeh 	bus_size_t sgsize;
    797   1.3      eeh 	paddr_t curaddr;
    798  1.12      eeh 	u_long dvmaddr;
    799  1.12      eeh 	vaddr_t vaddr = (vaddr_t)buf;
    800   1.1      eeh 	pmap_t pmap;
    801   1.1      eeh 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    802   1.1      eeh 
    803   1.2      eeh 	if (map->dm_nsegs) {
    804   1.2      eeh 		/* Already in use?? */
    805   1.2      eeh #ifdef DIAGNOSTIC
    806   1.2      eeh 		printf("sbus_dmamap_load: map still in use\n");
    807   1.2      eeh #endif
    808   1.2      eeh 		bus_dmamap_unload(t, map);
    809   1.2      eeh 	}
    810  1.19      eeh 
    811  1.12      eeh 	/*
    812  1.12      eeh 	 * Make sure that on error condition we return "no valid mappings".
    813  1.12      eeh 	 */
    814  1.12      eeh 	map->dm_nsegs = 0;
    815  1.12      eeh 
    816  1.12      eeh 	if (buflen > map->_dm_size)
    817  1.12      eeh #ifdef DEBUG
    818  1.12      eeh 	{
    819  1.16      eeh 		printf("sbus_dmamap_load(): error %d > %d -- map size exceeded!\n", buflen, map->_dm_size);
    820  1.12      eeh 		Debugger();
    821  1.12      eeh 		return (EINVAL);
    822  1.12      eeh 	}
    823  1.12      eeh #else
    824  1.12      eeh 		return (EINVAL);
    825  1.12      eeh #endif
    826  1.12      eeh 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    827  1.12      eeh 
    828  1.12      eeh 	/*
    829  1.12      eeh 	 * XXX Need to implement "don't dma across this boundry".
    830  1.12      eeh 	 */
    831  1.12      eeh 
    832  1.12      eeh 	s = splhigh();
    833  1.17      mrg 	err = extent_alloc(sc->sc_is.is_dvmamap, sgsize, NBPG,
    834  1.12      eeh 			     map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
    835  1.12      eeh 	splx(s);
    836  1.12      eeh 
    837  1.12      eeh 	if (err != 0)
    838  1.12      eeh 		return (err);
    839  1.12      eeh 
    840  1.12      eeh #ifdef DEBUG
    841  1.12      eeh 	if (dvmaddr == (bus_addr_t)-1)
    842  1.12      eeh 	{
    843  1.16      eeh 		printf("sbus_dmamap_load(): dvmamap_alloc(%d, %x) failed!\n", sgsize, flags);
    844  1.12      eeh 		Debugger();
    845  1.12      eeh 	}
    846  1.12      eeh #endif
    847  1.12      eeh 	if (dvmaddr == (bus_addr_t)-1)
    848  1.12      eeh 		return (ENOMEM);
    849  1.12      eeh 
    850  1.12      eeh 	/*
    851  1.12      eeh 	 * We always use just one segment.
    852  1.12      eeh 	 */
    853  1.12      eeh 	map->dm_mapsize = buflen;
    854  1.12      eeh 	map->dm_nsegs = 1;
    855  1.12      eeh 	map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
    856  1.12      eeh 	map->dm_segs[0].ds_len = sgsize;
    857  1.12      eeh 
    858   1.1      eeh 	if (p != NULL)
    859   1.1      eeh 		pmap = p->p_vmspace->vm_map.pmap;
    860   1.1      eeh 	else
    861   1.1      eeh 		pmap = pmap_kernel();
    862   1.1      eeh 
    863   1.2      eeh 	dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
    864   1.1      eeh 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    865   1.1      eeh 	for (; buflen > 0; ) {
    866   1.1      eeh 		/*
    867   1.1      eeh 		 * Get the physical address for this page.
    868   1.1      eeh 		 */
    869   1.3      eeh 		if ((curaddr = (bus_addr_t)pmap_extract(pmap, (vaddr_t)vaddr)) == NULL) {
    870   1.1      eeh 			bus_dmamap_unload(t, map);
    871   1.1      eeh 			return (-1);
    872   1.1      eeh 		}
    873   1.1      eeh 
    874   1.1      eeh 		/*
    875   1.1      eeh 		 * Compute the segment size, and adjust counts.
    876   1.1      eeh 		 */
    877   1.1      eeh 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    878   1.1      eeh 		if (buflen < sgsize)
    879   1.1      eeh 			sgsize = buflen;
    880   1.1      eeh 
    881   1.2      eeh #ifdef DEBUG
    882   1.2      eeh 		if (sbusdebug & SDB_DVMA)
    883   1.8      eeh 			printf("sbus_dmamap_load: map %p loading va %lx at pa %lx\n",
    884   1.8      eeh 			       map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
    885   1.2      eeh #endif
    886  1.19      eeh 		iommu_enter(&sc->sc_is, trunc_page(dvmaddr), trunc_page(curaddr), flags);
    887   1.1      eeh 
    888   1.1      eeh 		dvmaddr += PAGE_SIZE;
    889   1.1      eeh 		vaddr += sgsize;
    890   1.1      eeh 		buflen -= sgsize;
    891   1.1      eeh 	}
    892   1.1      eeh 	return (0);
    893   1.1      eeh }
    894   1.1      eeh 
    895   1.1      eeh void
    896   1.1      eeh sbus_dmamap_unload(t, map)
    897   1.1      eeh 	bus_dma_tag_t t;
    898   1.1      eeh 	bus_dmamap_t map;
    899   1.1      eeh {
    900   1.3      eeh 	vaddr_t addr;
    901  1.12      eeh 	int len, error, s;
    902  1.12      eeh 	bus_addr_t dvmaddr;
    903  1.12      eeh 	bus_size_t sgsize;
    904   1.1      eeh 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    905   1.1      eeh 
    906   1.1      eeh 	if (map->dm_nsegs != 1)
    907  1.16      eeh 		panic("sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
    908   1.1      eeh 
    909   1.2      eeh 	addr = trunc_page(map->dm_segs[0].ds_addr);
    910   1.1      eeh 	len = map->dm_segs[0].ds_len;
    911   1.1      eeh 
    912   1.2      eeh #ifdef DEBUG
    913   1.2      eeh 	if (sbusdebug & SDB_DVMA)
    914   1.8      eeh 		printf("sbus_dmamap_unload: map %p removing va %lx size %lx\n",
    915   1.8      eeh 		       map, (long)addr, (long)len);
    916   1.2      eeh #endif
    917  1.19      eeh 	iommu_remove(&sc->sc_is, addr, len);
    918  1.12      eeh 	dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
    919  1.12      eeh 	sgsize = map->dm_segs[0].ds_len;
    920  1.12      eeh 
    921  1.12      eeh 	/* Mark the mappings as invalid. */
    922  1.12      eeh 	map->dm_mapsize = 0;
    923  1.12      eeh 	map->dm_nsegs = 0;
    924  1.12      eeh 
    925  1.12      eeh 	/* Unmapping is bus dependent */
    926  1.12      eeh 	s = splhigh();
    927  1.17      mrg 	error = extent_free(sc->sc_is.is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
    928  1.12      eeh 	splx(s);
    929  1.12      eeh 	if (error != 0)
    930  1.12      eeh 		printf("warning: %ld of DVMA space lost\n", (long)sgsize);
    931  1.12      eeh 
    932  1.12      eeh 	cache_flush((caddr_t)dvmaddr, (u_int) sgsize);
    933   1.1      eeh }
    934   1.1      eeh 
    935   1.1      eeh 
    936   1.1      eeh void
    937   1.1      eeh sbus_dmamap_sync(t, map, offset, len, ops)
    938   1.1      eeh 	bus_dma_tag_t t;
    939   1.1      eeh 	bus_dmamap_t map;
    940   1.1      eeh 	bus_addr_t offset;
    941   1.1      eeh 	bus_size_t len;
    942   1.1      eeh 	int ops;
    943   1.1      eeh {
    944   1.1      eeh 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
    945   1.3      eeh 	vaddr_t va = map->dm_segs[0].ds_addr + offset;
    946   1.1      eeh 
    947   1.1      eeh 	/*
    948   1.1      eeh 	 * We only support one DMA segment; supporting more makes this code
    949   1.1      eeh          * too unweildy.
    950   1.1      eeh 	 */
    951   1.1      eeh 
    952   1.8      eeh 	if (ops&BUS_DMASYNC_PREREAD) {
    953   1.8      eeh #ifdef DEBUG
    954   1.8      eeh 		if (sbusdebug & SDB_DVMA)
    955   1.8      eeh 			printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREREAD\n",
    956   1.8      eeh 			       (long)va, (u_long)len);
    957   1.8      eeh #endif
    958   1.8      eeh 
    959   1.1      eeh 		/* Nothing to do */;
    960   1.8      eeh 	}
    961   1.1      eeh 	if (ops&BUS_DMASYNC_POSTREAD) {
    962   1.1      eeh 		/*
    963   1.1      eeh 		 * We should sync the IOMMU streaming caches here first.
    964   1.1      eeh 		 */
    965   1.8      eeh #ifdef DEBUG
    966   1.8      eeh 		if (sbusdebug & SDB_DVMA)
    967   1.8      eeh 			printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTREAD\n",
    968   1.8      eeh 			       (long)va, (u_long)len);
    969   1.8      eeh #endif
    970   1.1      eeh 		while (len > 0) {
    971   1.1      eeh 
    972   1.1      eeh 			/*
    973   1.1      eeh 			 * Streaming buffer flushes:
    974   1.1      eeh 			 *
    975   1.1      eeh 			 *   1 Tell strbuf to flush by storing va to strbuf_pgflush
    976   1.1      eeh 			 * If we're not on a cache line boundary (64-bits):
    977   1.1      eeh 			 *   2 Store 0 in flag
    978   1.1      eeh 			 *   3 Store pointer to flag in flushsync
    979   1.1      eeh 			 *   4 wait till flushsync becomes 0x1
    980   1.1      eeh 			 *
    981   1.1      eeh 			 * If it takes more than .5 sec, something went wrong.
    982   1.1      eeh 			 */
    983   1.8      eeh #ifdef DEBUG
    984   1.8      eeh 			if (sbusdebug & SDB_DVMA)
    985   1.8      eeh 				printf("sbus_dmamap_sync: flushing va %p, %lu bytes left\n",
    986   1.8      eeh 				       (long)va, (u_long)len);
    987   1.8      eeh #endif
    988  1.17      mrg 			bus_space_write_8(sc->sc_bustag, &sc->sc_is.is_sb->strbuf_pgflush, 0, va);
    989   1.1      eeh 			if (len <= NBPG) {
    990  1.19      eeh 				iommu_flush(&sc->sc_is);
    991   1.8      eeh 				len = 0;
    992   1.8      eeh 			} else
    993   1.8      eeh 				len -= NBPG;
    994   1.1      eeh 			va += NBPG;
    995   1.1      eeh 		}
    996   1.1      eeh 	}
    997   1.8      eeh 	if (ops&BUS_DMASYNC_PREWRITE) {
    998   1.8      eeh #ifdef DEBUG
    999   1.8      eeh 		if (sbusdebug & SDB_DVMA)
   1000   1.8      eeh 			printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREWRITE\n",
   1001   1.8      eeh 			       (long)va, (u_long)len);
   1002   1.8      eeh #endif
   1003   1.1      eeh 		/* Nothing to do */;
   1004   1.8      eeh 	}
   1005   1.8      eeh 	if (ops&BUS_DMASYNC_POSTWRITE) {
   1006   1.8      eeh #ifdef DEBUG
   1007   1.8      eeh 		if (sbusdebug & SDB_DVMA)
   1008   1.8      eeh 			printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTWRITE\n",
   1009   1.8      eeh 			       (long)va, (u_long)len);
   1010   1.8      eeh #endif
   1011   1.8      eeh 		/* Nothing to do */;
   1012   1.8      eeh 	}
   1013   1.1      eeh 	bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1014   1.1      eeh }
   1015   1.1      eeh 
   1016   1.8      eeh 
   1017   1.8      eeh /*
   1018   1.8      eeh  * Take memory allocated by our parent bus and generate DVMA mappings for it.
   1019   1.8      eeh  */
   1020   1.1      eeh int
   1021   1.1      eeh sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
   1022   1.1      eeh 	bus_dma_tag_t t;
   1023   1.1      eeh 	bus_size_t size, alignment, boundary;
   1024   1.1      eeh 	bus_dma_segment_t *segs;
   1025   1.1      eeh 	int nsegs;
   1026   1.1      eeh 	int *rsegs;
   1027   1.1      eeh 	int flags;
   1028   1.1      eeh {
   1029   1.3      eeh 	paddr_t curaddr;
   1030  1.12      eeh 	u_long dvmaddr;
   1031   1.1      eeh 	vm_page_t m;
   1032   1.1      eeh 	struct pglist *mlist;
   1033   1.1      eeh 	int error;
   1034  1.12      eeh 	int n, s;
   1035   1.1      eeh 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
   1036   1.1      eeh 
   1037   1.1      eeh 	if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
   1038   1.1      eeh 				     boundary, segs, nsegs, rsegs, flags)))
   1039   1.1      eeh 		return (error);
   1040   1.1      eeh 
   1041   1.8      eeh 	/*
   1042   1.8      eeh 	 * Allocate a DVMA mapping for our new memory.
   1043   1.8      eeh 	 */
   1044  1.17      mrg 	for (n = 0; n < *rsegs; n++) {
   1045  1.12      eeh #if 1
   1046  1.12      eeh 		s = splhigh();
   1047  1.17      mrg 		if (extent_alloc(sc->sc_is.is_dvmamap, segs[0].ds_len, alignment,
   1048  1.12      eeh 				 boundary, EX_NOWAIT, (u_long *)&dvmaddr)) {
   1049  1.12      eeh 			splx(s);
   1050  1.12      eeh 				/* Free what we got and exit */
   1051  1.12      eeh 			bus_dmamem_free(t->_parent, segs, nsegs);
   1052  1.12      eeh 			return (ENOMEM);
   1053  1.12      eeh 		}
   1054  1.12      eeh 		splx(s);
   1055  1.12      eeh #else
   1056   1.8      eeh 		dvmaddr = dvmamap_alloc(segs[0].ds_len, flags);
   1057   1.8      eeh 		if (dvmaddr == (bus_addr_t)-1) {
   1058   1.8      eeh 			/* Free what we got and exit */
   1059   1.8      eeh 			bus_dmamem_free(t->_parent, segs, nsegs);
   1060   1.8      eeh 			return (ENOMEM);
   1061   1.8      eeh 		}
   1062  1.12      eeh #endif
   1063   1.8      eeh 		segs[n].ds_addr = dvmaddr;
   1064   1.1      eeh 		size = segs[n].ds_len;
   1065   1.1      eeh 		mlist = segs[n]._ds_mlist;
   1066   1.1      eeh 
   1067   1.1      eeh 		/* Map memory into DVMA space */
   1068   1.1      eeh 		for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
   1069   1.1      eeh 			curaddr = VM_PAGE_TO_PHYS(m);
   1070  1.12      eeh #ifdef DEBUG
   1071  1.12      eeh 			if (sbusdebug & SDB_DVMA)
   1072  1.12      eeh 				printf("sbus_dmamem_alloc: map %p loading va %lx at pa %lx\n",
   1073  1.12      eeh 				       (long)m, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
   1074  1.12      eeh #endif
   1075  1.19      eeh 			iommu_enter(&sc->sc_is, dvmaddr, curaddr, flags);
   1076   1.1      eeh 			dvmaddr += PAGE_SIZE;
   1077   1.1      eeh 		}
   1078   1.1      eeh 	}
   1079   1.1      eeh 	return (0);
   1080   1.1      eeh }
   1081   1.1      eeh 
   1082   1.1      eeh void
   1083   1.1      eeh sbus_dmamem_free(t, segs, nsegs)
   1084   1.1      eeh 	bus_dma_tag_t t;
   1085   1.1      eeh 	bus_dma_segment_t *segs;
   1086   1.1      eeh 	int nsegs;
   1087   1.1      eeh {
   1088   1.3      eeh 	vaddr_t addr;
   1089   1.2      eeh 	int len;
   1090  1.12      eeh 	int n, s, error;
   1091   1.1      eeh 	struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
   1092   1.1      eeh 
   1093   1.1      eeh 
   1094   1.1      eeh 	for (n=0; n<nsegs; n++) {
   1095   1.1      eeh 		addr = segs[n].ds_addr;
   1096   1.1      eeh 		len = segs[n].ds_len;
   1097  1.19      eeh 		iommu_remove(&sc->sc_is, addr, len);
   1098  1.12      eeh #if 1
   1099  1.12      eeh 		s = splhigh();
   1100  1.17      mrg 		error = extent_free(sc->sc_is.is_dvmamap, addr, len, EX_NOWAIT);
   1101  1.12      eeh 		splx(s);
   1102  1.12      eeh 		if (error != 0)
   1103  1.12      eeh 			printf("warning: %ld of DVMA space lost\n", (long)len);
   1104  1.12      eeh #else
   1105   1.8      eeh 		dvmamap_free(addr, len);
   1106  1.12      eeh #endif
   1107   1.1      eeh 	}
   1108   1.1      eeh 	bus_dmamem_free(t->_parent, segs, nsegs);
   1109   1.1      eeh }
   1110   1.1      eeh 
   1111   1.2      eeh /*
   1112   1.8      eeh  * Map the DVMA mappings into the kernel pmap.
   1113   1.2      eeh  * Check the flags to see whether we're streaming or coherent.
   1114   1.2      eeh  */
   1115   1.2      eeh int
   1116   1.2      eeh sbus_dmamem_map(t, segs, nsegs, size, kvap, flags)
   1117   1.2      eeh 	bus_dma_tag_t t;
   1118   1.2      eeh 	bus_dma_segment_t *segs;
   1119   1.2      eeh 	int nsegs;
   1120   1.2      eeh 	size_t size;
   1121   1.2      eeh 	caddr_t *kvap;
   1122   1.2      eeh 	int flags;
   1123   1.2      eeh {
   1124   1.2      eeh 	vm_page_t m;
   1125   1.3      eeh 	vaddr_t va;
   1126   1.2      eeh 	bus_addr_t addr;
   1127   1.2      eeh 	struct pglist *mlist;
   1128   1.3      eeh 	int cbit;
   1129   1.2      eeh 
   1130   1.2      eeh 	/*
   1131   1.2      eeh 	 * digest flags:
   1132   1.2      eeh 	 */
   1133   1.2      eeh 	cbit = 0;
   1134   1.2      eeh 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
   1135   1.2      eeh 		cbit |= PMAP_NVC;
   1136   1.3      eeh 	if (flags & BUS_DMA_NOCACHE)	/* sideffects */
   1137   1.2      eeh 		cbit |= PMAP_NC;
   1138   1.2      eeh 	/*
   1139   1.8      eeh 	 * Now take this and map it into the CPU since it should already
   1140   1.8      eeh 	 * be in the the IOMMU.
   1141   1.2      eeh 	 */
   1142   1.8      eeh 	*kvap = (caddr_t)va = segs[0].ds_addr;
   1143   1.2      eeh 	mlist = segs[0]._ds_mlist;
   1144   1.2      eeh 	for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
   1145   1.2      eeh 
   1146   1.2      eeh 		if (size == 0)
   1147   1.2      eeh 			panic("_bus_dmamem_map: size botch");
   1148   1.2      eeh 
   1149   1.2      eeh 		addr = VM_PAGE_TO_PHYS(m);
   1150   1.2      eeh 		pmap_enter(pmap_kernel(), va, addr | cbit,
   1151  1.14  thorpej 		    VM_PROT_READ | VM_PROT_WRITE, TRUE,
   1152  1.14  thorpej 		    VM_PROT_READ | VM_PROT_WRITE);
   1153   1.2      eeh 		va += PAGE_SIZE;
   1154   1.2      eeh 		size -= PAGE_SIZE;
   1155   1.2      eeh 	}
   1156   1.2      eeh 
   1157   1.2      eeh 	return (0);
   1158   1.2      eeh }
   1159   1.2      eeh 
   1160   1.2      eeh /*
   1161   1.8      eeh  * Unmap DVMA mappings from kernel
   1162   1.2      eeh  */
   1163   1.2      eeh void
   1164   1.2      eeh sbus_dmamem_unmap(t, kva, size)
   1165   1.2      eeh 	bus_dma_tag_t t;
   1166   1.2      eeh 	caddr_t kva;
   1167   1.2      eeh 	size_t size;
   1168   1.2      eeh {
   1169   1.2      eeh 
   1170   1.2      eeh #ifdef DIAGNOSTIC
   1171   1.2      eeh 	if ((u_long)kva & PGOFSET)
   1172   1.2      eeh 		panic("_bus_dmamem_unmap");
   1173   1.2      eeh #endif
   1174   1.2      eeh 
   1175   1.2      eeh 	size = round_page(size);
   1176   1.8      eeh 	pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
   1177   1.2      eeh }
   1178