Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.4
      1  1.4      mrg /*	$NetBSD: iommu.c,v 1.4 2000/04/05 05:59:03 mrg Exp $	*/
      2  1.1      mrg 
      3  1.1      mrg /*-
      4  1.1      mrg  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1      mrg  * All rights reserved.
      6  1.1      mrg  *
      7  1.1      mrg  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1      mrg  * by Paul Kranenburg.
      9  1.1      mrg  *
     10  1.1      mrg  * Redistribution and use in source and binary forms, with or without
     11  1.1      mrg  * modification, are permitted provided that the following conditions
     12  1.1      mrg  * are met:
     13  1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     14  1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     15  1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      mrg  *    documentation and/or other materials provided with the distribution.
     18  1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     19  1.1      mrg  *    must display the following acknowledgement:
     20  1.1      mrg  *        This product includes software developed by the NetBSD
     21  1.1      mrg  *        Foundation, Inc. and its contributors.
     22  1.1      mrg  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1      mrg  *    contributors may be used to endorse or promote products derived
     24  1.1      mrg  *    from this software without specific prior written permission.
     25  1.1      mrg  *
     26  1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1      mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1      mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1      mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1      mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1      mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1      mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1      mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1      mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1      mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1      mrg  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1      mrg  */
     38  1.1      mrg 
     39  1.1      mrg /*
     40  1.1      mrg  * Copyright (c) 1992, 1993
     41  1.1      mrg  *	The Regents of the University of California.  All rights reserved.
     42  1.1      mrg  *
     43  1.1      mrg  * This software was developed by the Computer Systems Engineering group
     44  1.1      mrg  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     45  1.1      mrg  * contributed to Berkeley.
     46  1.1      mrg  *
     47  1.1      mrg  * All advertising materials mentioning features or use of this software
     48  1.1      mrg  * must display the following acknowledgement:
     49  1.1      mrg  *	This product includes software developed by the University of
     50  1.1      mrg  *	California, Lawrence Berkeley Laboratory.
     51  1.1      mrg  *
     52  1.1      mrg  * Redistribution and use in source and binary forms, with or without
     53  1.1      mrg  * modification, are permitted provided that the following conditions
     54  1.1      mrg  * are met:
     55  1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     56  1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     57  1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     58  1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     59  1.1      mrg  *    documentation and/or other materials provided with the distribution.
     60  1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     61  1.1      mrg  *    must display the following acknowledgement:
     62  1.1      mrg  *	This product includes software developed by the University of
     63  1.1      mrg  *	California, Berkeley and its contributors.
     64  1.1      mrg  * 4. Neither the name of the University nor the names of its contributors
     65  1.1      mrg  *    may be used to endorse or promote products derived from this software
     66  1.1      mrg  *    without specific prior written permission.
     67  1.1      mrg  *
     68  1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     69  1.1      mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     70  1.1      mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     71  1.1      mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     72  1.1      mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     73  1.1      mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     74  1.1      mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     75  1.1      mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     76  1.1      mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     77  1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     78  1.1      mrg  * SUCH DAMAGE.
     79  1.1      mrg  *
     80  1.1      mrg  *	from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
     81  1.1      mrg  *	from: @(#)sbus.c	8.1 (Berkeley) 6/11/93
     82  1.1      mrg  */
     83  1.1      mrg 
     84  1.4      mrg #include "opt_ddb.h"
     85  1.4      mrg 
     86  1.1      mrg #include <sys/param.h>
     87  1.1      mrg #include <sys/extent.h>
     88  1.1      mrg #include <sys/malloc.h>
     89  1.1      mrg #include <sys/systm.h>
     90  1.1      mrg #include <sys/device.h>
     91  1.1      mrg #include <vm/vm.h>
     92  1.1      mrg 
     93  1.1      mrg #include <machine/bus.h>
     94  1.1      mrg #include <sparc64/sparc64/vaddrs.h>
     95  1.1      mrg #include <sparc64/dev/iommureg.h>
     96  1.1      mrg #include <sparc64/dev/iommuvar.h>
     97  1.1      mrg 
     98  1.1      mrg #include <machine/autoconf.h>
     99  1.1      mrg #include <machine/ctlreg.h>
    100  1.1      mrg #include <machine/cpu.h>
    101  1.1      mrg 
    102  1.1      mrg #ifdef DEBUG
    103  1.1      mrg #define IDB_DVMA	0x1
    104  1.1      mrg #define IDB_INTR	0x2
    105  1.1      mrg int iommudebug = 0;
    106  1.4      mrg #define DPRINTF(l, s)   do { if (iommudebug & l) printf s; } while (0)
    107  1.4      mrg #else
    108  1.4      mrg #define DPRINTF(l, s)
    109  1.1      mrg #endif
    110  1.1      mrg 
    111  1.1      mrg /*
    112  1.1      mrg  * initialise the UltraSPARC IOMMU (SBUS or PCI):
    113  1.1      mrg  *	- allocate and setup the iotsb.
    114  1.1      mrg  *	- enable the IOMMU
    115  1.1      mrg  *	- initialise the streaming buffers
    116  1.1      mrg  *	- create a private DVMA map.
    117  1.1      mrg  *
    118  1.1      mrg  */
    119  1.1      mrg void
    120  1.1      mrg iommu_init(name, is, tsbsize)
    121  1.1      mrg 	char *name;
    122  1.1      mrg 	struct iommu_state *is;
    123  1.1      mrg 	int tsbsize;
    124  1.1      mrg {
    125  1.1      mrg 
    126  1.1      mrg 	/*
    127  1.1      mrg 	 * Setup the iommu.
    128  1.1      mrg 	 *
    129  1.1      mrg 	 * The sun4u iommu is part of the SBUS controller so we will
    130  1.1      mrg 	 * deal with it here.  We could try to fake a device node so
    131  1.1      mrg 	 * we can eventually share it with the PCI bus run by psycho,
    132  1.1      mrg 	 * but I don't want to get into that sort of cruft.
    133  1.1      mrg 	 *
    134  1.1      mrg 	 * First we need to allocate a IOTSB.  Problem is that the IOMMU
    135  1.1      mrg 	 * can only access the IOTSB by physical address, so all the
    136  1.1      mrg 	 * pages must be contiguous.  Luckily, the smallest IOTSB size
    137  1.1      mrg 	 * is one 8K page.
    138  1.1      mrg 	 */
    139  1.1      mrg 	if (tsbsize != 0)
    140  1.1      mrg 		panic("tsbsize != 0; FIX ME");	/* XXX */
    141  1.1      mrg 
    142  1.1      mrg 	/* we want 8K pages */
    143  1.1      mrg 	is->is_cr = IOMMUCR_8KPG | IOMMUCR_EN;
    144  1.2      eeh 	/*
    145  1.2      eeh 	 *
    146  1.2      eeh 	 * The IOMMU address space always ends at 0xffffe000, but the starting
    147  1.2      eeh 	 * address depends on the size of the map.  The map size is 1024 * 2 ^
    148  1.2      eeh 	 * is->is_tsbsize entries, where each entry is 8 bytes.  The start of
    149  1.2      eeh 	 * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
    150  1.2      eeh 	 *
    151  1.2      eeh 	 * Note: the stupid IOMMU ignores the high bits of an address, so a
    152  1.2      eeh 	 * NULL DMA pointer will be translated by the first page of the IOTSB.
    153  1.2      eeh 	 * To trap bugs we'll skip the first entry in the IOTSB.
    154  1.2      eeh 	 */
    155  1.2      eeh 	is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
    156  1.1      mrg 	is->is_tsbsize = tsbsize;
    157  1.1      mrg 	is->is_tsb = malloc(NBPG, M_DMAMAP, M_WAITOK);	/* XXX */
    158  1.3  thorpej 	(void) pmap_extract(pmap_kernel(), (vaddr_t)is->is_tsb,
    159  1.3  thorpej 	    (paddr_t *)&is->is_ptsb);
    160  1.1      mrg 
    161  1.1      mrg #ifdef DEBUG
    162  1.1      mrg 	if (iommudebug & IDB_DVMA)
    163  1.1      mrg 	{
    164  1.1      mrg 		/* Probe the iommu */
    165  1.1      mrg 		struct iommureg *regs = is->is_iommu;
    166  1.1      mrg 		int64_t cr, tsb;
    167  1.1      mrg 
    168  1.1      mrg 		printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", &regs->iommu_cr,
    169  1.1      mrg 		       &regs->iommu_tsb, &regs->iommu_flush);
    170  1.1      mrg 		cr = regs->iommu_cr;
    171  1.1      mrg 		tsb = regs->iommu_tsb;
    172  1.1      mrg 		printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
    173  1.1      mrg 		printf("TSB base %p phys %p\n", (long)is->is_tsb, (long)is->is_ptsb);
    174  1.1      mrg 		delay(1000000); /* 1 s */
    175  1.1      mrg 	}
    176  1.1      mrg #endif
    177  1.1      mrg 
    178  1.1      mrg 	/*
    179  1.1      mrg 	 * Initialize streaming buffer.
    180  1.1      mrg 	 */
    181  1.3  thorpej 	(void) pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
    182  1.3  thorpej 	    (paddr_t *)&is->is_flushpa);
    183  1.1      mrg 
    184  1.1      mrg 	/*
    185  1.1      mrg 	 * now actually start up the IOMMU
    186  1.1      mrg 	 */
    187  1.1      mrg 	iommu_reset(is);
    188  1.1      mrg 
    189  1.1      mrg 	/*
    190  1.1      mrg 	 * Now all the hardware's working we need to allocate a dvma map.
    191  1.1      mrg 	 */
    192  1.1      mrg 	is->is_dvmamap = extent_create(name,
    193  1.2      eeh 				       is->is_dvmabase, IOTSB_VEND,
    194  1.1      mrg 				       M_DEVBUF, 0, 0, EX_NOWAIT);
    195  1.1      mrg }
    196  1.1      mrg 
    197  1.1      mrg void
    198  1.1      mrg iommu_reset(is)
    199  1.1      mrg 	struct iommu_state *is;
    200  1.1      mrg {
    201  1.1      mrg 
    202  1.1      mrg 	/* Need to do 64-bit stores */
    203  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0, is->is_cr);
    204  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
    205  1.1      mrg 	/* Enable diagnostics mode? */
    206  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
    207  1.2      eeh }
    208  1.2      eeh 
    209  1.2      eeh /*
    210  1.2      eeh  * Here are the iommu control routines.
    211  1.2      eeh  */
    212  1.2      eeh void
    213  1.2      eeh iommu_enter(is, va, pa, flags)
    214  1.2      eeh 	struct iommu_state *is;
    215  1.2      eeh 	vaddr_t va;
    216  1.2      eeh 	int64_t pa;
    217  1.2      eeh 	int flags;
    218  1.2      eeh {
    219  1.2      eeh 	int64_t tte;
    220  1.2      eeh 
    221  1.2      eeh #ifdef DIAGNOSTIC
    222  1.2      eeh 	if (va < is->is_dvmabase)
    223  1.4      mrg 		panic("iommu_enter: va 0x%lx not in DVMA space",va);
    224  1.2      eeh #endif
    225  1.2      eeh 
    226  1.2      eeh 	tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
    227  1.2      eeh 			!(flags&BUS_DMA_COHERENT));
    228  1.2      eeh 
    229  1.2      eeh 	/* Is the streamcache flush really needed? */
    230  1.2      eeh 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush,
    231  1.2      eeh 			  0, va);
    232  1.2      eeh 	iommu_flush(is);
    233  1.4      mrg 	DPRINTF(IDB_DVMA, ("Clearing TSB slot %d for va %p\n",
    234  1.4      mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), va));
    235  1.2      eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    236  1.2      eeh 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush,
    237  1.2      eeh 			  0, va);
    238  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    239  1.2      eeh 		       va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
    240  1.2      eeh 		       &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    241  1.4      mrg 		       (long)tte));
    242  1.2      eeh }
    243  1.2      eeh 
    244  1.2      eeh /*
    245  1.2      eeh  * iommu_remove: removes mappings created by iommu_enter
    246  1.2      eeh  *
    247  1.2      eeh  * Only demap from IOMMU if flag is set.
    248  1.2      eeh  */
    249  1.2      eeh void
    250  1.2      eeh iommu_remove(is, va, len)
    251  1.2      eeh 	struct iommu_state *is;
    252  1.2      eeh 	vaddr_t va;
    253  1.2      eeh 	size_t len;
    254  1.2      eeh {
    255  1.2      eeh 
    256  1.2      eeh #ifdef DIAGNOSTIC
    257  1.2      eeh 	if (va < is->is_dvmabase)
    258  1.4      mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (long)va);
    259  1.2      eeh 	if ((long)(va + len) < (long)va)
    260  1.4      mrg 		panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
    261  1.2      eeh 		      (long) va, (long) len);
    262  1.2      eeh 	if (len & ~0xfffffff)
    263  1.4      mrg 		panic("iommu_remove: rediculous len 0x%lx", (long)len);
    264  1.2      eeh #endif
    265  1.2      eeh 
    266  1.2      eeh 	va = trunc_page(va);
    267  1.2      eeh 	while (len > 0) {
    268  1.2      eeh 
    269  1.2      eeh 		/*
    270  1.2      eeh 		 * Streaming buffer flushes:
    271  1.2      eeh 		 *
    272  1.2      eeh 		 *   1 Tell strbuf to flush by storing va to strbuf_pgflush
    273  1.2      eeh 		 * If we're not on a cache line boundary (64-bits):
    274  1.2      eeh 		 *   2 Store 0 in flag
    275  1.2      eeh 		 *   3 Store pointer to flag in flushsync
    276  1.2      eeh 		 *   4 wait till flushsync becomes 0x1
    277  1.2      eeh 		 *
    278  1.2      eeh 		 * If it takes more than .5 sec, something went wrong.
    279  1.2      eeh 		 */
    280  1.4      mrg 		DPRINTF(IDB_DVMA, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    281  1.2      eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    282  1.2      eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    283  1.2      eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    284  1.4      mrg 			       (u_long)len));
    285  1.2      eeh 		bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush, 0, va);
    286  1.2      eeh 		if (len <= NBPG) {
    287  1.2      eeh 			iommu_flush(is);
    288  1.2      eeh 			len = 0;
    289  1.2      eeh 		} else len -= NBPG;
    290  1.4      mrg 		DPRINTF(IDB_DVMA, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    291  1.2      eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    292  1.2      eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    293  1.2      eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    294  1.4      mrg 			       (u_long)len));
    295  1.2      eeh 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
    296  1.2      eeh 		bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush, 0, va);
    297  1.2      eeh 		va += NBPG;
    298  1.2      eeh 	}
    299  1.2      eeh }
    300  1.2      eeh 
    301  1.2      eeh int
    302  1.2      eeh iommu_flush(is)
    303  1.2      eeh 	struct iommu_state *is;
    304  1.2      eeh {
    305  1.2      eeh 	struct timeval cur, flushtimeout;
    306  1.2      eeh 
    307  1.2      eeh #define BUMPTIME(t, usec) { \
    308  1.2      eeh 	register volatile struct timeval *tp = (t); \
    309  1.2      eeh 	register long us; \
    310  1.2      eeh  \
    311  1.2      eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    312  1.2      eeh 	if (us >= 1000000) { \
    313  1.2      eeh 		tp->tv_usec = us - 1000000; \
    314  1.2      eeh 		tp->tv_sec++; \
    315  1.2      eeh 	} \
    316  1.2      eeh }
    317  1.2      eeh 
    318  1.2      eeh 	is->is_flush = 0;
    319  1.2      eeh 	membar_sync();
    320  1.2      eeh 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
    321  1.2      eeh 	membar_sync();
    322  1.2      eeh 
    323  1.2      eeh 	microtime(&flushtimeout);
    324  1.2      eeh 	cur = flushtimeout;
    325  1.2      eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    326  1.2      eeh 
    327  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
    328  1.2      eeh 		       (long)is->is_flush, (long)&is->is_flush,
    329  1.2      eeh 		       (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
    330  1.4      mrg 		       flushtimeout.tv_sec, flushtimeout.tv_usec));
    331  1.2      eeh 	/* Bypass non-coherent D$ */
    332  1.2      eeh 	while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
    333  1.2      eeh 	       ((cur.tv_sec <= flushtimeout.tv_sec) &&
    334  1.2      eeh 		(cur.tv_usec <= flushtimeout.tv_usec)))
    335  1.2      eeh 		microtime(&cur);
    336  1.2      eeh 
    337  1.2      eeh #ifdef DIAGNOSTIC
    338  1.2      eeh 	if (!is->is_flush) {
    339  1.4      mrg 		printf("iommu_flush: flush timeout %p at %p\n", (long)is->is_flush,
    340  1.2      eeh 		       (long)is->is_flushpa); /* panic? */
    341  1.2      eeh #ifdef DDB
    342  1.2      eeh 		Debugger();
    343  1.2      eeh #endif
    344  1.2      eeh 	}
    345  1.2      eeh #endif
    346  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flushed\n"));
    347  1.2      eeh 	return (is->is_flush);
    348  1.1      mrg }
    349