Home | History | Annotate | Line # | Download | only in sparc
iommu.c revision 1.2
      1  1.2  abrown /*	$NetBSD: iommu.c,v 1.2 1996/04/04 23:05:22 abrown Exp $ */
      2  1.1      pk 
      3  1.1      pk /*
      4  1.1      pk  * Copyright (c) 1996
      5  1.1      pk  * 	The President and Fellows of Harvard University. All rights reserved.
      6  1.1      pk  * Copyright (c) 1995 	Paul Kranenburg
      7  1.1      pk  *
      8  1.1      pk  * Redistribution and use in source and binary forms, with or without
      9  1.1      pk  * modification, are permitted provided that the following conditions
     10  1.1      pk  * are met:
     11  1.1      pk  * 1. Redistributions of source code must retain the above copyright
     12  1.1      pk  *    notice, this list of conditions and the following disclaimer.
     13  1.1      pk  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      pk  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      pk  *    documentation and/or other materials provided with the distribution.
     16  1.1      pk  * 3. All advertising materials mentioning features or use of this software
     17  1.1      pk  *    must display the following acknowledgement:
     18  1.1      pk  *	This product includes software developed by Aaron Brown and
     19  1.1      pk  *	Harvard University.
     20  1.1      pk  *	This product includes software developed by Paul Kranenburg.
     21  1.1      pk  * 4. Neither the name of the University nor the names of its contributors
     22  1.1      pk  *    may be used to endorse or promote products derived from this software
     23  1.1      pk  *    without specific prior written permission.
     24  1.1      pk  *
     25  1.1      pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1      pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1      pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1      pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1      pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1      pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1      pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1      pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1      pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1      pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1      pk  * SUCH DAMAGE.
     36  1.1      pk  *
     37  1.1      pk  */
     38  1.1      pk 
     39  1.1      pk #include <sys/param.h>
     40  1.1      pk #include <sys/systm.h>
     41  1.1      pk #include <sys/device.h>
     42  1.1      pk #include <vm/vm.h>
     43  1.1      pk 
     44  1.1      pk #include <machine/autoconf.h>
     45  1.1      pk #include <machine/ctlreg.h>
     46  1.1      pk #include <sparc/sparc/asm.h>
     47  1.1      pk #include <sparc/sparc/vaddrs.h>
     48  1.1      pk #include <sparc/sparc/iommureg.h>
     49  1.1      pk 
     50  1.1      pk struct iommu_softc {
     51  1.1      pk 	struct device	sc_dev;		/* base device */
     52  1.1      pk 	struct iommureg	*sc_reg;
     53  1.1      pk 	u_int		sc_pagesize;
     54  1.1      pk 	u_int		sc_range;
     55  1.1      pk 	u_int		sc_dvmabase;
     56  1.1      pk 	iopte_t		*sc_ptes;
     57  1.1      pk 	int		sc_hasiocache;
     58  1.1      pk };
     59  1.1      pk struct	iommu_softc *iommu_sc;/*XXX*/
     60  1.1      pk int	has_iocache;
     61  1.1      pk 
     62  1.1      pk 
     63  1.1      pk /* autoconfiguration driver */
     64  1.1      pk int	iommu_print __P((void *, char *));
     65  1.1      pk void	iommu_attach __P((struct device *, struct device *, void *));
     66  1.1      pk int	iommu_match __P((struct device *, void *, void *));
     67  1.1      pk 
     68  1.1      pk struct cfattach iommu_ca = {
     69  1.1      pk 	sizeof(struct iommu_softc), iommu_match, iommu_attach
     70  1.1      pk };
     71  1.1      pk 
     72  1.1      pk struct cfdriver iommu_cd = {
     73  1.1      pk 	NULL, "iommu", DV_DULL
     74  1.1      pk };
     75  1.1      pk 
     76  1.1      pk /*
     77  1.1      pk  * Print the location of some iommu-attached device (called just
     78  1.1      pk  * before attaching that device).  If `iommu' is not NULL, the
     79  1.1      pk  * device was found but not configured; print the iommu as well.
     80  1.1      pk  * Return UNCONF (config_find ignores this if the device was configured).
     81  1.1      pk  */
     82  1.1      pk int
     83  1.1      pk iommu_print(args, iommu)
     84  1.1      pk 	void *args;
     85  1.1      pk 	char *iommu;
     86  1.1      pk {
     87  1.1      pk 	register struct confargs *ca = args;
     88  1.1      pk 
     89  1.1      pk 	if (iommu)
     90  1.1      pk 		printf("%s at %s", ca->ca_ra.ra_name, iommu);
     91  1.1      pk 	return (UNCONF);
     92  1.1      pk }
     93  1.1      pk 
     94  1.1      pk int
     95  1.1      pk iommu_match(parent, vcf, aux)
     96  1.1      pk 	struct device *parent;
     97  1.1      pk 	void *vcf, *aux;
     98  1.1      pk {
     99  1.1      pk 	struct cfdata *cf = vcf;
    100  1.1      pk 	register struct confargs *ca = aux;
    101  1.1      pk 	register struct romaux *ra = &ca->ca_ra;
    102  1.1      pk 
    103  1.1      pk 	if (CPU_ISSUN4OR4C)
    104  1.1      pk 		return (0);
    105  1.1      pk 	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
    106  1.1      pk }
    107  1.1      pk 
    108  1.1      pk /*
    109  1.1      pk  * Attach the iommu.
    110  1.1      pk  */
    111  1.1      pk void
    112  1.1      pk iommu_attach(parent, self, aux)
    113  1.1      pk 	struct device *parent;
    114  1.1      pk 	struct device *self;
    115  1.1      pk 	void *aux;
    116  1.1      pk {
    117  1.1      pk 	register struct iommu_softc *sc = (struct iommu_softc *)self;
    118  1.1      pk 	struct confargs oca, *ca = aux;
    119  1.1      pk 	register struct romaux *ra = &ca->ca_ra;
    120  1.1      pk 	register int node;
    121  1.1      pk 	register char *name;
    122  1.1      pk 	register u_int pbase, pa;
    123  1.1      pk 	register int i, mmupcrsav, s, wierdviking = 0;
    124  1.1      pk 	register iopte_t *tpte_p;
    125  1.1      pk 	extern u_int *kernel_iopte_table;
    126  1.1      pk 	extern u_int kernel_iopte_table_pa;
    127  1.1      pk 
    128  1.1      pk /*XXX-GCC!*/mmupcrsav=0;
    129  1.1      pk 	iommu_sc = sc;
    130  1.1      pk 	/*
    131  1.1      pk 	 * XXX there is only one iommu, for now -- do not know how to
    132  1.1      pk 	 * address children on others
    133  1.1      pk 	 */
    134  1.1      pk 	if (sc->sc_dev.dv_unit > 0) {
    135  1.1      pk 		printf(" unsupported\n");
    136  1.1      pk 		return;
    137  1.1      pk 	}
    138  1.1      pk 	node = ra->ra_node;
    139  1.1      pk 
    140  1.1      pk #if 0
    141  1.1      pk 	if (ra->ra_vaddr)
    142  1.1      pk 		sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
    143  1.1      pk #else
    144  1.1      pk 	/*
    145  1.1      pk 	 * Map registers into our space. The PROM may have done this
    146  1.1      pk 	 * already, but I feel better if we have our own copy. Plus, the
    147  1.1      pk 	 * prom doesn't map the entire register set
    148  1.1      pk 	 *
    149  1.1      pk 	 * XXX struct iommureg is bigger than ra->ra_len; what are the
    150  1.1      pk 	 *     other fields for?
    151  1.1      pk 	 */
    152  1.1      pk 	sc->sc_reg = (struct iommureg *)
    153  1.1      pk 		mapdev(ra->ra_reg, 0, 0, ra->ra_len, ra->ra_iospace);
    154  1.1      pk #endif
    155  1.1      pk 
    156  1.1      pk 	sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
    157  1.1      pk 	has_iocache = sc->sc_hasiocache; /* Set global flag */
    158  1.1      pk 
    159  1.1      pk 	sc->sc_pagesize = getpropint(node, "page-size", NBPG),
    160  1.1      pk 	sc->sc_range = (1 << 24) <<
    161  1.1      pk 	    ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
    162  1.1      pk #if 0
    163  1.1      pk 	sc->sc_dvmabase = (0 - sc->sc_range);
    164  1.1      pk #endif
    165  1.1      pk 	pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
    166  1.1      pk 			(14 - IOMMU_BAR_IBASHFT);
    167  1.1      pk 
    168  1.1      pk 	/*
    169  1.1      pk 	 * Now we build our own copy of the IOMMU page tables. We need to
    170  1.1      pk 	 * do this since we're going to change the range to give us 64M of
    171  1.1      pk 	 * mappings, and thus we can move DVMA space down to 0xfd000000 to
    172  1.1      pk 	 * give us lots of space and to avoid bumping into the PROM, etc.
    173  1.1      pk 	 *
    174  1.1      pk 	 * XXX Note that this is rather messy.
    175  1.1      pk 	 */
    176  1.1      pk 	sc->sc_ptes = (iopte_t *) kernel_iopte_table;
    177  1.1      pk 
    178  1.1      pk 	/*
    179  1.1      pk 	 * Now discache the page tables so that the IOMMU sees our
    180  1.1      pk 	 * changes.
    181  1.1      pk 	 */
    182  1.1      pk 	kvm_uncache((caddr_t)sc->sc_ptes,
    183  1.1      pk 		(((0 - DVMA4M_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
    184  1.1      pk 
    185  1.1      pk 	/*
    186  1.1      pk 	 * Ok. We've got to read in the original table using MMU bypass,
    187  1.1      pk 	 * and copy all of its entries to the appropriate place in our
    188  1.1      pk 	 * new table, even if the sizes are different.
    189  1.1      pk 	 * This is pretty easy since we know DVMA ends at 0xffffffff.
    190  1.1      pk 	 *
    191  1.1      pk 	 * XXX: PGOFSET, NBPG assume same page size as SRMMU
    192  1.1      pk 	 */
    193  1.1      pk 	if ((getpsr() & 0x40000000) && (!(lda(SRMMU_PCR,ASI_SRMMU) & 0x800))) {
    194  1.1      pk 		wierdviking = 1;
    195  1.1      pk 		sta(SRMMU_PCR, ASI_SRMMU, 	/* set MMU AC bit */
    196  1.1      pk 		    ((mmupcrsav = lda(SRMMU_PCR,ASI_SRMMU)) | SRMMU_PCR_AC));
    197  1.1      pk 	}
    198  1.1      pk 
    199  1.1      pk 	for (tpte_p = &sc->sc_ptes[((0 - DVMA4M_BASE)/NBPG) - 1],
    200  1.1      pk 	     pa = (u_int)pbase - sizeof(iopte_t) +
    201  1.1      pk 		   ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
    202  1.1      pk 	     tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
    203  1.1      pk 	     tpte_p--, pa -= sizeof(iopte_t)) {
    204  1.1      pk 
    205  1.1      pk 		IOMMU_FLUSHPAGE(sc,
    206  1.1      pk 			        (tpte_p - &sc->sc_ptes[0])*NBPG + DVMA4M_BASE);
    207  1.1      pk 		*tpte_p = lda(pa, ASI_BYPASS);
    208  1.1      pk 	}
    209  1.1      pk 	if (wierdviking) {	/* restore mmu after bug-avoidance */
    210  1.1      pk 		sta(SRMMU_PCR, ASI_SRMMU, mmupcrsav);
    211  1.1      pk 	}
    212  1.1      pk 
    213  1.1      pk 	/*
    214  1.1      pk 	 * Now we can install our new pagetable into the IOMMU
    215  1.1      pk 	 */
    216  1.1      pk 	sc->sc_range = 0 - DVMA4M_BASE;
    217  1.1      pk 	sc->sc_dvmabase = DVMA4M_BASE;
    218  1.1      pk 
    219  1.1      pk 	/* calculate log2(sc->sc_range/16MB) */
    220  1.1      pk 	i = ffs(sc->sc_range/(1 << 24)) - 1;
    221  1.1      pk 	if ((1 << i) != (sc->sc_range/(1 << 24)))
    222  1.1      pk 		panic("bad iommu range: %d\n",i);
    223  1.1      pk 
    224  1.1      pk 	s = splhigh();
    225  1.1      pk 	IOMMU_FLUSHALL(sc);
    226  1.1      pk 
    227  1.1      pk 	sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
    228  1.1      pk 			  (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
    229  1.1      pk 	sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
    230  1.1      pk 
    231  1.1      pk 	IOMMU_FLUSHALL(sc);
    232  1.1      pk 	splx(s);
    233  1.1      pk 
    234  1.1      pk 	printf(": version %x/%x, page-size %d, range %dMB\n",
    235  1.1      pk 		(sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
    236  1.1      pk 		(sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
    237  1.1      pk 		sc->sc_pagesize,
    238  1.1      pk 		sc->sc_range >> 20);
    239  1.1      pk 
    240  1.1      pk 	/* Propagate bootpath */
    241  1.1      pk 	if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "iommu") == 0)
    242  1.1      pk 		oca.ca_ra.ra_bp = ra->ra_bp + 1;
    243  1.1      pk 	else
    244  1.1      pk 		oca.ca_ra.ra_bp = NULL;
    245  1.1      pk 
    246  1.1      pk 	/*
    247  1.1      pk 	 * Loop through ROM children (expect Sbus among them).
    248  1.1      pk 	 */
    249  1.1      pk 	for (node = firstchild(node); node; node = nextsibling(node)) {
    250  1.1      pk 		name = getpropstring(node, "name");
    251  1.1      pk 		if (!romprop(&oca.ca_ra, name, node))
    252  1.1      pk 			continue;
    253  1.1      pk 		oca.ca_bustype = BUS_MAIN; /* ??? */
    254  1.1      pk 		(void) config_found(&sc->sc_dev, (void *)&oca, iommu_print);
    255  1.1      pk 	}
    256  1.1      pk }
    257  1.1      pk 
    258  1.1      pk void
    259  1.1      pk iommu_enter(va, pa)
    260  1.1      pk 	u_int va, pa;
    261  1.1      pk {
    262  1.1      pk 	struct iommu_softc *sc = iommu_sc;
    263  1.1      pk 	int pte;
    264  1.1      pk 
    265  1.1      pk #ifdef DEBUG
    266  1.1      pk 	if (va < sc->sc_dvmabase)
    267  1.1      pk 		panic("iommu_enter: va 0x%x not in DVMA space",va);
    268  1.1      pk #endif
    269  1.1      pk 
    270  1.1      pk 	pte = atop(pa) << IOPTE_PPNSHFT;
    271  1.1      pk 	pte &= IOPTE_PPN;
    272  1.2  abrown 	pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
    273  1.1      pk 	sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
    274  1.1      pk 	IOMMU_FLUSHPAGE(sc, va);
    275  1.1      pk }
    276  1.1      pk 
    277  1.1      pk /*
    278  1.1      pk  * iommu_clear: clears mappings created by iommu_enter
    279  1.1      pk  */
    280  1.1      pk void
    281  1.1      pk iommu_remove(va, len)
    282  1.1      pk 	register u_int va, len;
    283  1.1      pk {
    284  1.1      pk 	register struct iommu_softc *sc = iommu_sc;
    285  1.1      pk 
    286  1.1      pk #ifdef DEBUG
    287  1.1      pk 	if (va < sc->sc_dvmabase)
    288  1.1      pk 		panic("iommu_enter: va 0x%x not in DVMA space", va);
    289  1.1      pk #endif
    290  1.1      pk 
    291  1.1      pk 	while (len > 0) {
    292  1.1      pk #ifdef notyet
    293  1.1      pk #ifdef DEBUG
    294  1.1      pk 		if ((sc->sc_ptes[atop(va - sc->sc_dvmabase)] & IOPTE_V) == 0)
    295  1.1      pk 			panic("iommu_clear: clearing invalid pte at va 0x%x",
    296  1.1      pk 				va);
    297  1.1      pk #endif
    298  1.1      pk #endif
    299  1.1      pk 		sc->sc_ptes[atop(va - sc->sc_dvmabase)] = 0;
    300  1.1      pk 		sta(sc->sc_ptes + atop(va - sc->sc_dvmabase), ASI_BYPASS, 0);
    301  1.1      pk 		IOMMU_FLUSHPAGE(sc, va);
    302  1.1      pk 		len -= sc->sc_pagesize;
    303  1.1      pk 		va += sc->sc_pagesize;
    304  1.1      pk 	}
    305  1.1      pk }
    306  1.1      pk 
    307  1.1      pk #if 0	/* These registers aren't there??? */
    308  1.1      pk void
    309  1.1      pk iommu_error()
    310  1.1      pk {
    311  1.1      pk 	struct iommu_softc *sc = X;
    312  1.1      pk 	struct iommureg *iop = sc->sc_reg;
    313  1.1      pk 
    314  1.1      pk 	printf("iommu: afsr %x, afar %x\n", iop->io_afsr, iop->io_afar);
    315  1.1      pk 	printf("iommu: mfsr %x, mfar %x\n", iop->io_mfsr, iop->io_mfar);
    316  1.1      pk }
    317  1.1      pk int
    318  1.1      pk iommu_alloc(va, len)
    319  1.1      pk 	u_int va, len;
    320  1.1      pk {
    321  1.1      pk 	struct iommu_softc *sc = X;
    322  1.1      pk 	int off, tva, pa, iovaddr, pte;
    323  1.1      pk 
    324  1.1      pk 	off = (int)va & PGOFSET;
    325  1.1      pk 	len = round_page(len + off);
    326  1.1      pk 	va -= off;
    327  1.1      pk 
    328  1.1      pk if ((int)sc->sc_dvmacur + len > 0)
    329  1.1      pk 	sc->sc_dvmacur = sc->sc_dvmabase;
    330  1.1      pk 
    331  1.1      pk 	iovaddr = tva = sc->sc_dvmacur;
    332  1.1      pk 	sc->sc_dvmacur += len;
    333  1.1      pk 	while (len) {
    334  1.1      pk 		pa = pmap_extract(pmap_kernel(), va);
    335  1.1      pk 
    336  1.1      pk #define IOMMU_PPNSHIFT	8
    337  1.1      pk #define IOMMU_V		0x00000002
    338  1.1      pk #define IOMMU_W		0x00000004
    339  1.1      pk 
    340  1.1      pk 		pte = atop(pa) << IOMMU_PPNSHIFT;
    341  1.1      pk 		pte |= IOMMU_V | IOMMU_W;
    342  1.1      pk 		sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
    343  1.1      pk 		sc->sc_reg->io_flushpage = tva;
    344  1.1      pk 		len -= NBPG;
    345  1.1      pk 		va += NBPG;
    346  1.1      pk 		tva += NBPG;
    347  1.1      pk 	}
    348  1.1      pk 	return iovaddr + off;
    349  1.1      pk }
    350  1.1      pk #endif
    351