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