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