Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.8
      1  1.8      mrg /*	$NetBSD: iommu.c,v 1.8 2000/04/25 14:59:38 mrg Exp $	*/
      2  1.7      mrg 
      3  1.7      mrg /*
      4  1.7      mrg  * Copyright (c) 1999, 2000 Matthew R. Green
      5  1.7      mrg  * All rights reserved.
      6  1.7      mrg  *
      7  1.7      mrg  * Redistribution and use in source and binary forms, with or without
      8  1.7      mrg  * modification, are permitted provided that the following conditions
      9  1.7      mrg  * are met:
     10  1.7      mrg  * 1. Redistributions of source code must retain the above copyright
     11  1.7      mrg  *    notice, this list of conditions and the following disclaimer.
     12  1.7      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.7      mrg  *    notice, this list of conditions and the following disclaimer in the
     14  1.7      mrg  *    documentation and/or other materials provided with the distribution.
     15  1.7      mrg  * 3. The name of the author may not be used to endorse or promote products
     16  1.7      mrg  *    derived from this software without specific prior written permission.
     17  1.7      mrg  *
     18  1.7      mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.7      mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.7      mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.7      mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.7      mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.7      mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.7      mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.7      mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.7      mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.7      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.7      mrg  * SUCH DAMAGE.
     29  1.7      mrg  */
     30  1.1      mrg 
     31  1.1      mrg /*-
     32  1.1      mrg  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     33  1.1      mrg  * All rights reserved.
     34  1.1      mrg  *
     35  1.1      mrg  * This code is derived from software contributed to The NetBSD Foundation
     36  1.1      mrg  * by Paul Kranenburg.
     37  1.1      mrg  *
     38  1.1      mrg  * Redistribution and use in source and binary forms, with or without
     39  1.1      mrg  * modification, are permitted provided that the following conditions
     40  1.1      mrg  * are met:
     41  1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     42  1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     43  1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     45  1.1      mrg  *    documentation and/or other materials provided with the distribution.
     46  1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     47  1.1      mrg  *    must display the following acknowledgement:
     48  1.1      mrg  *        This product includes software developed by the NetBSD
     49  1.1      mrg  *        Foundation, Inc. and its contributors.
     50  1.1      mrg  * 4. Neither the name of The NetBSD Foundation nor the names of its
     51  1.1      mrg  *    contributors may be used to endorse or promote products derived
     52  1.1      mrg  *    from this software without specific prior written permission.
     53  1.1      mrg  *
     54  1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     55  1.1      mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  1.1      mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  1.1      mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     58  1.1      mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  1.1      mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  1.1      mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  1.1      mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  1.1      mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  1.1      mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  1.1      mrg  * POSSIBILITY OF SUCH DAMAGE.
     65  1.1      mrg  */
     66  1.1      mrg 
     67  1.1      mrg /*
     68  1.1      mrg  * Copyright (c) 1992, 1993
     69  1.1      mrg  *	The Regents of the University of California.  All rights reserved.
     70  1.1      mrg  *
     71  1.1      mrg  * This software was developed by the Computer Systems Engineering group
     72  1.1      mrg  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     73  1.1      mrg  * contributed to Berkeley.
     74  1.1      mrg  *
     75  1.1      mrg  * All advertising materials mentioning features or use of this software
     76  1.1      mrg  * must display the following acknowledgement:
     77  1.1      mrg  *	This product includes software developed by the University of
     78  1.1      mrg  *	California, Lawrence Berkeley Laboratory.
     79  1.1      mrg  *
     80  1.1      mrg  * Redistribution and use in source and binary forms, with or without
     81  1.1      mrg  * modification, are permitted provided that the following conditions
     82  1.1      mrg  * are met:
     83  1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     84  1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     85  1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     86  1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     87  1.1      mrg  *    documentation and/or other materials provided with the distribution.
     88  1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     89  1.1      mrg  *    must display the following acknowledgement:
     90  1.1      mrg  *	This product includes software developed by the University of
     91  1.1      mrg  *	California, Berkeley and its contributors.
     92  1.1      mrg  * 4. Neither the name of the University nor the names of its contributors
     93  1.1      mrg  *    may be used to endorse or promote products derived from this software
     94  1.1      mrg  *    without specific prior written permission.
     95  1.1      mrg  *
     96  1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     97  1.1      mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     98  1.1      mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     99  1.1      mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    100  1.1      mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    101  1.1      mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    102  1.1      mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    103  1.1      mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    104  1.1      mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    105  1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    106  1.1      mrg  * SUCH DAMAGE.
    107  1.1      mrg  *
    108  1.1      mrg  *	from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
    109  1.1      mrg  *	from: @(#)sbus.c	8.1 (Berkeley) 6/11/93
    110  1.1      mrg  */
    111  1.1      mrg 
    112  1.7      mrg /*
    113  1.7      mrg  * UltraSPARC IOMMU support; used by both the sbus and pci code.
    114  1.7      mrg  */
    115  1.7      mrg 
    116  1.4      mrg #include "opt_ddb.h"
    117  1.4      mrg 
    118  1.1      mrg #include <sys/param.h>
    119  1.1      mrg #include <sys/extent.h>
    120  1.1      mrg #include <sys/malloc.h>
    121  1.1      mrg #include <sys/systm.h>
    122  1.1      mrg #include <sys/device.h>
    123  1.1      mrg #include <vm/vm.h>
    124  1.8      mrg #include <vm/vm_kern.h>
    125  1.1      mrg 
    126  1.1      mrg #include <machine/bus.h>
    127  1.7      mrg #include <sparc64/sparc64/cache.h>
    128  1.1      mrg #include <sparc64/sparc64/vaddrs.h>
    129  1.1      mrg #include <sparc64/dev/iommureg.h>
    130  1.1      mrg #include <sparc64/dev/iommuvar.h>
    131  1.1      mrg 
    132  1.1      mrg #include <machine/autoconf.h>
    133  1.1      mrg #include <machine/ctlreg.h>
    134  1.1      mrg #include <machine/cpu.h>
    135  1.1      mrg 
    136  1.1      mrg #ifdef DEBUG
    137  1.1      mrg #define IDB_DVMA	0x1
    138  1.1      mrg #define IDB_INTR	0x2
    139  1.7      mrg int iommudebug = 0x3;
    140  1.4      mrg #define DPRINTF(l, s)   do { if (iommudebug & l) printf s; } while (0)
    141  1.4      mrg #else
    142  1.4      mrg #define DPRINTF(l, s)
    143  1.1      mrg #endif
    144  1.1      mrg 
    145  1.1      mrg /*
    146  1.1      mrg  * initialise the UltraSPARC IOMMU (SBUS or PCI):
    147  1.1      mrg  *	- allocate and setup the iotsb.
    148  1.1      mrg  *	- enable the IOMMU
    149  1.7      mrg  *	- initialise the streaming buffers (if they exist)
    150  1.1      mrg  *	- create a private DVMA map.
    151  1.1      mrg  */
    152  1.1      mrg void
    153  1.1      mrg iommu_init(name, is, tsbsize)
    154  1.1      mrg 	char *name;
    155  1.1      mrg 	struct iommu_state *is;
    156  1.1      mrg 	int tsbsize;
    157  1.1      mrg {
    158  1.1      mrg 
    159  1.1      mrg 	/*
    160  1.1      mrg 	 * Setup the iommu.
    161  1.1      mrg 	 *
    162  1.7      mrg 	 * The sun4u iommu is part of the SBUS or PCI controller so we
    163  1.7      mrg 	 * will deal with it here..
    164  1.1      mrg 	 *
    165  1.1      mrg 	 * First we need to allocate a IOTSB.  Problem is that the IOMMU
    166  1.1      mrg 	 * can only access the IOTSB by physical address, so all the
    167  1.1      mrg 	 * pages must be contiguous.  Luckily, the smallest IOTSB size
    168  1.1      mrg 	 * is one 8K page.
    169  1.1      mrg 	 */
    170  1.1      mrg 	if (tsbsize != 0)
    171  1.1      mrg 		panic("tsbsize != 0; FIX ME");	/* XXX */
    172  1.1      mrg 
    173  1.1      mrg 	/* we want 8K pages */
    174  1.1      mrg 	is->is_cr = IOMMUCR_8KPG | IOMMUCR_EN;
    175  1.2      eeh 	/*
    176  1.2      eeh 	 *
    177  1.2      eeh 	 * The IOMMU address space always ends at 0xffffe000, but the starting
    178  1.2      eeh 	 * address depends on the size of the map.  The map size is 1024 * 2 ^
    179  1.2      eeh 	 * is->is_tsbsize entries, where each entry is 8 bytes.  The start of
    180  1.2      eeh 	 * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
    181  1.2      eeh 	 *
    182  1.2      eeh 	 * Note: the stupid IOMMU ignores the high bits of an address, so a
    183  1.2      eeh 	 * NULL DMA pointer will be translated by the first page of the IOTSB.
    184  1.2      eeh 	 * To trap bugs we'll skip the first entry in the IOTSB.
    185  1.2      eeh 	 */
    186  1.2      eeh 	is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
    187  1.1      mrg 	is->is_tsbsize = tsbsize;
    188  1.1      mrg 	is->is_tsb = malloc(NBPG, M_DMAMAP, M_WAITOK);	/* XXX */
    189  1.3  thorpej 	(void) pmap_extract(pmap_kernel(), (vaddr_t)is->is_tsb,
    190  1.3  thorpej 	    (paddr_t *)&is->is_ptsb);
    191  1.1      mrg 
    192  1.1      mrg #ifdef DEBUG
    193  1.1      mrg 	if (iommudebug & IDB_DVMA)
    194  1.1      mrg 	{
    195  1.1      mrg 		/* Probe the iommu */
    196  1.1      mrg 		struct iommureg *regs = is->is_iommu;
    197  1.1      mrg 		int64_t cr, tsb;
    198  1.1      mrg 
    199  1.1      mrg 		printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", &regs->iommu_cr,
    200  1.1      mrg 		       &regs->iommu_tsb, &regs->iommu_flush);
    201  1.1      mrg 		cr = regs->iommu_cr;
    202  1.1      mrg 		tsb = regs->iommu_tsb;
    203  1.1      mrg 		printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
    204  1.1      mrg 		printf("TSB base %p phys %p\n", (long)is->is_tsb, (long)is->is_ptsb);
    205  1.1      mrg 		delay(1000000); /* 1 s */
    206  1.1      mrg 	}
    207  1.1      mrg #endif
    208  1.1      mrg 
    209  1.1      mrg 	/*
    210  1.8      mrg 	 * Initialize streaming buffer, if it is there.
    211  1.1      mrg 	 */
    212  1.8      mrg 	if (is->is_sb)
    213  1.8      mrg 		(void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
    214  1.8      mrg 		    (paddr_t *)&is->is_flushpa);
    215  1.1      mrg 
    216  1.1      mrg 	/*
    217  1.1      mrg 	 * now actually start up the IOMMU
    218  1.1      mrg 	 */
    219  1.1      mrg 	iommu_reset(is);
    220  1.1      mrg 
    221  1.1      mrg 	/*
    222  1.1      mrg 	 * Now all the hardware's working we need to allocate a dvma map.
    223  1.1      mrg 	 */
    224  1.1      mrg 	is->is_dvmamap = extent_create(name,
    225  1.2      eeh 				       is->is_dvmabase, IOTSB_VEND,
    226  1.1      mrg 				       M_DEVBUF, 0, 0, EX_NOWAIT);
    227  1.1      mrg }
    228  1.1      mrg 
    229  1.8      mrg /*
    230  1.8      mrg  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
    231  1.8      mrg  * detected that already and disabled them.  If not, we will notice that
    232  1.8      mrg  * they aren't there when the STRBUF_EN bit does not remain.
    233  1.8      mrg  */
    234  1.1      mrg void
    235  1.1      mrg iommu_reset(is)
    236  1.1      mrg 	struct iommu_state *is;
    237  1.1      mrg {
    238  1.1      mrg 
    239  1.1      mrg 	/* Need to do 64-bit stores */
    240  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0, is->is_cr);
    241  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
    242  1.5      mrg 
    243  1.7      mrg 	if (!is->is_sb)
    244  1.7      mrg 		return;
    245  1.7      mrg 
    246  1.1      mrg 	/* Enable diagnostics mode? */
    247  1.1      mrg 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
    248  1.5      mrg 
    249  1.5      mrg 	/* No streaming buffers? Disable them */
    250  1.7      mrg 	if (bus_space_read_8(is->is_bustag,
    251  1.7      mrg 	    (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl, 0) == 0)
    252  1.5      mrg 		is->is_sb = 0;
    253  1.2      eeh }
    254  1.2      eeh 
    255  1.2      eeh /*
    256  1.2      eeh  * Here are the iommu control routines.
    257  1.2      eeh  */
    258  1.2      eeh void
    259  1.2      eeh iommu_enter(is, va, pa, flags)
    260  1.2      eeh 	struct iommu_state *is;
    261  1.2      eeh 	vaddr_t va;
    262  1.2      eeh 	int64_t pa;
    263  1.2      eeh 	int flags;
    264  1.2      eeh {
    265  1.2      eeh 	int64_t tte;
    266  1.2      eeh 
    267  1.2      eeh #ifdef DIAGNOSTIC
    268  1.2      eeh 	if (va < is->is_dvmabase)
    269  1.4      mrg 		panic("iommu_enter: va 0x%lx not in DVMA space",va);
    270  1.2      eeh #endif
    271  1.2      eeh 
    272  1.2      eeh 	tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
    273  1.2      eeh 			!(flags&BUS_DMA_COHERENT));
    274  1.2      eeh 
    275  1.2      eeh 	/* Is the streamcache flush really needed? */
    276  1.5      mrg 	if (is->is_sb) {
    277  1.5      mrg 		bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush, 0,
    278  1.5      mrg 		    va);
    279  1.5      mrg 		iommu_flush(is);
    280  1.5      mrg 	}
    281  1.4      mrg 	DPRINTF(IDB_DVMA, ("Clearing TSB slot %d for va %p\n",
    282  1.4      mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), va));
    283  1.2      eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    284  1.2      eeh 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush,
    285  1.2      eeh 			  0, va);
    286  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    287  1.2      eeh 		       va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
    288  1.2      eeh 		       &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    289  1.4      mrg 		       (long)tte));
    290  1.2      eeh }
    291  1.2      eeh 
    292  1.2      eeh /*
    293  1.2      eeh  * iommu_remove: removes mappings created by iommu_enter
    294  1.2      eeh  *
    295  1.2      eeh  * Only demap from IOMMU if flag is set.
    296  1.8      mrg  *
    297  1.8      mrg  * XXX: this function needs better internal error checking.
    298  1.2      eeh  */
    299  1.2      eeh void
    300  1.2      eeh iommu_remove(is, va, len)
    301  1.2      eeh 	struct iommu_state *is;
    302  1.2      eeh 	vaddr_t va;
    303  1.2      eeh 	size_t len;
    304  1.2      eeh {
    305  1.2      eeh 
    306  1.2      eeh #ifdef DIAGNOSTIC
    307  1.2      eeh 	if (va < is->is_dvmabase)
    308  1.4      mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (long)va);
    309  1.2      eeh 	if ((long)(va + len) < (long)va)
    310  1.4      mrg 		panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
    311  1.2      eeh 		      (long) va, (long) len);
    312  1.2      eeh 	if (len & ~0xfffffff)
    313  1.4      mrg 		panic("iommu_remove: rediculous len 0x%lx", (long)len);
    314  1.2      eeh #endif
    315  1.2      eeh 
    316  1.2      eeh 	va = trunc_page(va);
    317  1.8      mrg 	DPRINTF(IDB_DVMA, ("iommu_remove: va %lx TSB[%lx]@%p\n",
    318  1.8      mrg 	    va, IOTSBSLOT(va,is->is_tsbsize),
    319  1.8      mrg 	    &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
    320  1.2      eeh 	while (len > 0) {
    321  1.8      mrg 		DPRINTF(IDB_DVMA, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
    322  1.8      mrg 		    (int)IOTSBSLOT(va,is->is_tsbsize), va, (u_long)len));
    323  1.5      mrg 		if (is->is_sb) {
    324  1.5      mrg 			DPRINTF(IDB_DVMA, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    325  1.2      eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    326  1.2      eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    327  1.2      eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    328  1.4      mrg 			       (u_long)len));
    329  1.5      mrg 			bus_space_write_8(is->is_bustag,
    330  1.5      mrg 			    &is->is_sb->strbuf_pgflush, 0, va);
    331  1.5      mrg 			if (len <= NBPG) {
    332  1.5      mrg 				iommu_flush(is);
    333  1.5      mrg 				len = 0;
    334  1.5      mrg 			} else len -= NBPG;
    335  1.5      mrg 			DPRINTF(IDB_DVMA, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    336  1.2      eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    337  1.2      eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    338  1.2      eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    339  1.4      mrg 			       (u_long)len));
    340  1.8      mrg 		} else {
    341  1.8      mrg 			len -= NBPG;
    342  1.8      mrg 			membar_sync();	/* XXX */
    343  1.5      mrg 		}
    344  1.8      mrg 
    345  1.2      eeh 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
    346  1.2      eeh 		bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush, 0, va);
    347  1.2      eeh 		va += NBPG;
    348  1.2      eeh 	}
    349  1.2      eeh }
    350  1.2      eeh 
    351  1.2      eeh int
    352  1.2      eeh iommu_flush(is)
    353  1.2      eeh 	struct iommu_state *is;
    354  1.2      eeh {
    355  1.2      eeh 	struct timeval cur, flushtimeout;
    356  1.2      eeh 
    357  1.2      eeh #define BUMPTIME(t, usec) { \
    358  1.2      eeh 	register volatile struct timeval *tp = (t); \
    359  1.2      eeh 	register long us; \
    360  1.2      eeh  \
    361  1.2      eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    362  1.2      eeh 	if (us >= 1000000) { \
    363  1.2      eeh 		tp->tv_usec = us - 1000000; \
    364  1.2      eeh 		tp->tv_sec++; \
    365  1.2      eeh 	} \
    366  1.2      eeh }
    367  1.5      mrg 
    368  1.5      mrg 	if (!is->is_sb)
    369  1.5      mrg 		return (0);
    370  1.7      mrg 
    371  1.7      mrg 	/*
    372  1.7      mrg 	 * Streaming buffer flushes:
    373  1.7      mrg 	 *
    374  1.7      mrg 	 *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
    375  1.7      mrg 	 *     we're not on a cache line boundary (64-bits):
    376  1.7      mrg 	 *   2 Store 0 in flag
    377  1.7      mrg 	 *   3 Store pointer to flag in flushsync
    378  1.7      mrg 	 *   4 wait till flushsync becomes 0x1
    379  1.7      mrg 	 *
    380  1.7      mrg 	 * If it takes more than .5 sec, something
    381  1.7      mrg 	 * went wrong.
    382  1.7      mrg 	 */
    383  1.2      eeh 
    384  1.2      eeh 	is->is_flush = 0;
    385  1.2      eeh 	membar_sync();
    386  1.2      eeh 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
    387  1.2      eeh 	membar_sync();
    388  1.2      eeh 
    389  1.2      eeh 	microtime(&flushtimeout);
    390  1.2      eeh 	cur = flushtimeout;
    391  1.2      eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    392  1.2      eeh 
    393  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
    394  1.2      eeh 		       (long)is->is_flush, (long)&is->is_flush,
    395  1.2      eeh 		       (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
    396  1.4      mrg 		       flushtimeout.tv_sec, flushtimeout.tv_usec));
    397  1.2      eeh 	/* Bypass non-coherent D$ */
    398  1.2      eeh 	while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
    399  1.2      eeh 	       ((cur.tv_sec <= flushtimeout.tv_sec) &&
    400  1.2      eeh 		(cur.tv_usec <= flushtimeout.tv_usec)))
    401  1.2      eeh 		microtime(&cur);
    402  1.2      eeh 
    403  1.2      eeh #ifdef DIAGNOSTIC
    404  1.2      eeh 	if (!is->is_flush) {
    405  1.4      mrg 		printf("iommu_flush: flush timeout %p at %p\n", (long)is->is_flush,
    406  1.2      eeh 		       (long)is->is_flushpa); /* panic? */
    407  1.2      eeh #ifdef DDB
    408  1.2      eeh 		Debugger();
    409  1.2      eeh #endif
    410  1.2      eeh 	}
    411  1.2      eeh #endif
    412  1.4      mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flushed\n"));
    413  1.2      eeh 	return (is->is_flush);
    414  1.7      mrg }
    415  1.7      mrg 
    416  1.7      mrg /*
    417  1.7      mrg  * IOMMU DVMA operations, common to SBUS and PCI.
    418  1.7      mrg  */
    419  1.7      mrg int
    420  1.7      mrg iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
    421  1.7      mrg 	bus_dma_tag_t t;
    422  1.7      mrg 	struct iommu_state *is;
    423  1.7      mrg 	bus_dmamap_t map;
    424  1.7      mrg 	void *buf;
    425  1.7      mrg 	bus_size_t buflen;
    426  1.7      mrg 	struct proc *p;
    427  1.7      mrg 	int flags;
    428  1.7      mrg {
    429  1.7      mrg 	int s;
    430  1.7      mrg 	int err;
    431  1.7      mrg 	bus_size_t sgsize;
    432  1.7      mrg 	paddr_t curaddr;
    433  1.7      mrg 	u_long dvmaddr;
    434  1.7      mrg 	vaddr_t vaddr = (vaddr_t)buf;
    435  1.7      mrg 	pmap_t pmap;
    436  1.7      mrg 
    437  1.7      mrg 	if (map->dm_nsegs) {
    438  1.7      mrg 		/* Already in use?? */
    439  1.7      mrg #ifdef DIAGNOSTIC
    440  1.7      mrg 		printf("iommu_dvmamap_load: map still in use\n");
    441  1.7      mrg #endif
    442  1.7      mrg 		bus_dmamap_unload(t, map);
    443  1.7      mrg 	}
    444  1.7      mrg 	/*
    445  1.7      mrg 	 * Make sure that on error condition we return "no valid mappings".
    446  1.7      mrg 	 */
    447  1.7      mrg 	map->dm_nsegs = 0;
    448  1.7      mrg 
    449  1.7      mrg 	if (buflen > map->_dm_size) {
    450  1.7      mrg 		DPRINTF(IDB_DVMA,
    451  1.7      mrg 		    ("iommu_dvmamap_load(): error %d > %d -- "
    452  1.7      mrg 		     "map size exceeded!\n", buflen, map->_dm_size));
    453  1.7      mrg 		return (EINVAL);
    454  1.7      mrg 	}
    455  1.7      mrg 
    456  1.7      mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    457  1.7      mrg 
    458  1.7      mrg 	/*
    459  1.7      mrg 	 * XXX Need to implement "don't dma across this boundry".
    460  1.7      mrg 	 */
    461  1.7      mrg 
    462  1.7      mrg 	s = splhigh();
    463  1.7      mrg 	err = extent_alloc(is->is_dvmamap, sgsize, NBPG,
    464  1.7      mrg 	    map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
    465  1.7      mrg 	splx(s);
    466  1.7      mrg 
    467  1.7      mrg 	if (err != 0)
    468  1.7      mrg 		return (err);
    469  1.7      mrg 
    470  1.7      mrg #ifdef DEBUG
    471  1.7      mrg 	if (dvmaddr == (bus_addr_t)-1)
    472  1.7      mrg 	{
    473  1.7      mrg 		printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
    474  1.7      mrg 		    sgsize, flags);
    475  1.7      mrg 		Debugger();
    476  1.7      mrg 	}
    477  1.7      mrg #endif
    478  1.7      mrg 	if (dvmaddr == (bus_addr_t)-1)
    479  1.7      mrg 		return (ENOMEM);
    480  1.7      mrg 
    481  1.7      mrg 	/*
    482  1.7      mrg 	 * We always use just one segment.
    483  1.7      mrg 	 */
    484  1.7      mrg 	map->dm_mapsize = buflen;
    485  1.7      mrg 	map->dm_nsegs = 1;
    486  1.7      mrg 	map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
    487  1.7      mrg 	map->dm_segs[0].ds_len = sgsize;
    488  1.7      mrg 
    489  1.7      mrg 	if (p != NULL)
    490  1.7      mrg 		pmap = p->p_vmspace->vm_map.pmap;
    491  1.7      mrg 	else
    492  1.7      mrg 		pmap = pmap_kernel();
    493  1.7      mrg 
    494  1.7      mrg 	dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
    495  1.7      mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    496  1.7      mrg 	for (; buflen > 0; ) {
    497  1.7      mrg 		/*
    498  1.7      mrg 		 * Get the physical address for this page.
    499  1.7      mrg 		 */
    500  1.7      mrg 		if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
    501  1.7      mrg 			bus_dmamap_unload(t, map);
    502  1.7      mrg 			return (-1);
    503  1.7      mrg 		}
    504  1.7      mrg 
    505  1.7      mrg 		/*
    506  1.7      mrg 		 * Compute the segment size, and adjust counts.
    507  1.7      mrg 		 */
    508  1.7      mrg 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    509  1.7      mrg 		if (buflen < sgsize)
    510  1.7      mrg 			sgsize = buflen;
    511  1.7      mrg 
    512  1.7      mrg 		DPRINTF(IDB_DVMA,
    513  1.7      mrg 		    ("iommu_dvmamap_load: map %p loading va %lx at pa %lx\n",
    514  1.7      mrg 		    map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1))));
    515  1.7      mrg 		iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
    516  1.7      mrg 		    flags);
    517  1.7      mrg 
    518  1.7      mrg 		dvmaddr += PAGE_SIZE;
    519  1.7      mrg 		vaddr += sgsize;
    520  1.7      mrg 		buflen -= sgsize;
    521  1.7      mrg 	}
    522  1.7      mrg 	return (0);
    523  1.7      mrg }
    524  1.7      mrg 
    525  1.7      mrg 
    526  1.7      mrg void
    527  1.7      mrg iommu_dvmamap_unload(t, is, map)
    528  1.7      mrg 	bus_dma_tag_t t;
    529  1.7      mrg 	struct iommu_state *is;
    530  1.7      mrg 	bus_dmamap_t map;
    531  1.7      mrg {
    532  1.7      mrg 	vaddr_t addr;
    533  1.7      mrg 	int len;
    534  1.7      mrg 	int error, s;
    535  1.7      mrg 	bus_addr_t dvmaddr;
    536  1.7      mrg 	bus_size_t sgsize;
    537  1.7      mrg 
    538  1.7      mrg 	if (map->dm_nsegs != 1)
    539  1.7      mrg 		panic("iommu_dvmamap_unload: nsegs = %d", map->dm_nsegs);
    540  1.7      mrg 
    541  1.7      mrg 	addr = trunc_page(map->dm_segs[0].ds_addr);
    542  1.7      mrg 	len = map->dm_segs[0].ds_len;
    543  1.7      mrg 
    544  1.7      mrg 	DPRINTF(IDB_DVMA,
    545  1.7      mrg 	    ("iommu_dvmamap_unload: map %p removing va %lx size %lx\n",
    546  1.7      mrg 	    map, (long)addr, (long)len));
    547  1.7      mrg 	iommu_remove(is, addr, len);
    548  1.7      mrg 	dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
    549  1.7      mrg 	sgsize = map->dm_segs[0].ds_len;
    550  1.7      mrg 
    551  1.7      mrg 	/* Mark the mappings as invalid. */
    552  1.7      mrg 	map->dm_mapsize = 0;
    553  1.7      mrg 	map->dm_nsegs = 0;
    554  1.7      mrg 
    555  1.7      mrg 	s = splhigh();
    556  1.7      mrg 	error = extent_free(is->is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
    557  1.7      mrg 	splx(s);
    558  1.7      mrg 	if (error != 0)
    559  1.7      mrg 		printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
    560  1.7      mrg 	cache_flush((caddr_t)(u_long)dvmaddr, (u_int)sgsize);
    561  1.7      mrg }
    562  1.7      mrg 
    563  1.7      mrg void
    564  1.7      mrg iommu_dvmamap_sync(t, is, map, offset, len, ops)
    565  1.7      mrg 	bus_dma_tag_t t;
    566  1.7      mrg 	struct iommu_state *is;
    567  1.7      mrg 	bus_dmamap_t map;
    568  1.7      mrg 	bus_addr_t offset;
    569  1.7      mrg 	bus_size_t len;
    570  1.7      mrg 	int ops;
    571  1.7      mrg {
    572  1.7      mrg 	vaddr_t va = map->dm_segs[0].ds_addr + offset;
    573  1.7      mrg 
    574  1.7      mrg 	/*
    575  1.7      mrg 	 * We only support one DMA segment; supporting more makes this code
    576  1.7      mrg          * too unweildy.
    577  1.7      mrg 	 */
    578  1.7      mrg 
    579  1.7      mrg 	if (ops & BUS_DMASYNC_PREREAD) {
    580  1.7      mrg 		DPRINTF(IDB_DVMA,
    581  1.7      mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    582  1.7      mrg 		     "BUS_DMASYNC_PREREAD\n", (long)va, (u_long)len));
    583  1.7      mrg 
    584  1.7      mrg 		/* Nothing to do */;
    585  1.7      mrg 	}
    586  1.7      mrg 	if (ops & BUS_DMASYNC_POSTREAD) {
    587  1.7      mrg 		DPRINTF(IDB_DVMA,
    588  1.7      mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    589  1.7      mrg 		     "BUS_DMASYNC_POSTREAD\n", (long)va, (u_long)len));
    590  1.7      mrg 		/* if we have a streaming buffer, flush it here first */
    591  1.7      mrg 		if (is->is_sb)
    592  1.7      mrg 			while (len > 0) {
    593  1.7      mrg 				DPRINTF(IDB_DVMA,
    594  1.7      mrg 				    ("iommu_dvmamap_sync: flushing va %p, %lu "
    595  1.7      mrg 				     "bytes left\n", (long)va, (u_long)len));
    596  1.7      mrg 				bus_space_write_8(is->is_bustag,
    597  1.7      mrg 				    &is->is_sb->strbuf_pgflush, 0, va);
    598  1.7      mrg 				if (len <= NBPG) {
    599  1.7      mrg 					iommu_flush(is);
    600  1.7      mrg 					len = 0;
    601  1.7      mrg 				} else
    602  1.7      mrg 					len -= NBPG;
    603  1.7      mrg 				va += NBPG;
    604  1.7      mrg 			}
    605  1.7      mrg 	}
    606  1.7      mrg 	if (ops & BUS_DMASYNC_PREWRITE) {
    607  1.7      mrg 		DPRINTF(IDB_DVMA,
    608  1.7      mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    609  1.7      mrg 		     "BUS_DMASYNC_PREWRITE\n", (long)va, (u_long)len));
    610  1.7      mrg 		/* Nothing to do */;
    611  1.7      mrg 	}
    612  1.7      mrg 	if (ops & BUS_DMASYNC_POSTWRITE) {
    613  1.7      mrg 		DPRINTF(IDB_DVMA,
    614  1.7      mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    615  1.7      mrg 		     "BUS_DMASYNC_POSTWRITE\n", (long)va, (u_long)len));
    616  1.7      mrg 		/* Nothing to do */;
    617  1.7      mrg 	}
    618  1.7      mrg }
    619  1.7      mrg 
    620  1.7      mrg int
    621  1.7      mrg iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
    622  1.7      mrg 	bus_dma_tag_t t;
    623  1.7      mrg 	struct iommu_state *is;
    624  1.7      mrg 	bus_size_t size, alignment, boundary;
    625  1.7      mrg 	bus_dma_segment_t *segs;
    626  1.7      mrg 	int nsegs;
    627  1.7      mrg 	int *rsegs;
    628  1.7      mrg 	int flags;
    629  1.7      mrg {
    630  1.7      mrg 
    631  1.7      mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_alloc: sz %qx align %qx bound %qx "
    632  1.8      mrg 	   "segp %p flags %d\n", size, alignment, boundary, segs, flags));
    633  1.7      mrg 	return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
    634  1.7      mrg 	    segs, nsegs, rsegs, flags));
    635  1.7      mrg }
    636  1.7      mrg 
    637  1.7      mrg void
    638  1.7      mrg iommu_dvmamem_free(t, is, segs, nsegs)
    639  1.7      mrg 	bus_dma_tag_t t;
    640  1.7      mrg 	struct iommu_state *is;
    641  1.7      mrg 	bus_dma_segment_t *segs;
    642  1.7      mrg 	int nsegs;
    643  1.7      mrg {
    644  1.7      mrg 
    645  1.7      mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
    646  1.7      mrg 	    segs, nsegs));
    647  1.7      mrg 	bus_dmamem_free(t->_parent, segs, nsegs);
    648  1.7      mrg }
    649  1.7      mrg 
    650  1.7      mrg /*
    651  1.7      mrg  * Map the DVMA mappings into the kernel pmap.
    652  1.7      mrg  * Check the flags to see whether we're streaming or coherent.
    653  1.7      mrg  */
    654  1.7      mrg int
    655  1.7      mrg iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
    656  1.7      mrg 	bus_dma_tag_t t;
    657  1.7      mrg 	struct iommu_state *is;
    658  1.7      mrg 	bus_dma_segment_t *segs;
    659  1.7      mrg 	int nsegs;
    660  1.7      mrg 	size_t size;
    661  1.7      mrg 	caddr_t *kvap;
    662  1.7      mrg 	int flags;
    663  1.7      mrg {
    664  1.7      mrg 	vm_page_t m;
    665  1.7      mrg 	vaddr_t va;
    666  1.7      mrg 	bus_addr_t addr;
    667  1.7      mrg 	struct pglist *mlist;
    668  1.8      mrg 	int cbit;
    669  1.7      mrg 
    670  1.7      mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
    671  1.7      mrg 	    segs, nsegs, size));
    672  1.7      mrg 
    673  1.7      mrg 	/*
    674  1.8      mrg 	 * Allocate some space in the kernel map, and then map these pages
    675  1.8      mrg 	 * into this space.
    676  1.7      mrg 	 */
    677  1.8      mrg 	size = round_page(size);
    678  1.8      mrg 	va = uvm_km_valloc(kernel_map, size);
    679  1.8      mrg 	if (va == 0)
    680  1.8      mrg 		return (ENOMEM);
    681  1.7      mrg 
    682  1.8      mrg 	*kvap = (caddr_t)va;
    683  1.7      mrg 
    684  1.7      mrg 	/*
    685  1.7      mrg 	 * digest flags:
    686  1.7      mrg 	 */
    687  1.7      mrg 	cbit = 0;
    688  1.7      mrg 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
    689  1.7      mrg 		cbit |= PMAP_NVC;
    690  1.7      mrg 	if (flags & BUS_DMA_NOCACHE)	/* sideffects */
    691  1.7      mrg 		cbit |= PMAP_NC;
    692  1.7      mrg 
    693  1.7      mrg 	/*
    694  1.8      mrg 	 * Now take this and map it into the CPU.
    695  1.7      mrg 	 */
    696  1.7      mrg 	mlist = segs[0]._ds_mlist;
    697  1.7      mrg 	for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
    698  1.8      mrg #ifdef DIAGNOSTIC
    699  1.7      mrg 		if (size == 0)
    700  1.7      mrg 			panic("iommu_dvmamem_map: size botch");
    701  1.8      mrg #endif
    702  1.7      mrg 		addr = VM_PAGE_TO_PHYS(m);
    703  1.7      mrg 		DPRINTF(IDB_DVMA, ("iommu_dvmamem_map: "
    704  1.7      mrg 		    "mapping va %lx at %qx\n", va, addr | cbit));
    705  1.7      mrg 		pmap_enter(pmap_kernel(), va, addr | cbit,
    706  1.7      mrg 		    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
    707  1.7      mrg 		va += PAGE_SIZE;
    708  1.7      mrg 		size -= PAGE_SIZE;
    709  1.7      mrg 	}
    710  1.7      mrg 
    711  1.7      mrg 	return (0);
    712  1.7      mrg }
    713  1.7      mrg 
    714  1.7      mrg /*
    715  1.7      mrg  * Unmap DVMA mappings from kernel
    716  1.7      mrg  */
    717  1.7      mrg void
    718  1.7      mrg iommu_dvmamem_unmap(t, is, kva, size)
    719  1.7      mrg 	bus_dma_tag_t t;
    720  1.7      mrg 	struct iommu_state *is;
    721  1.7      mrg 	caddr_t kva;
    722  1.7      mrg 	size_t size;
    723  1.7      mrg {
    724  1.7      mrg 
    725  1.7      mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
    726  1.7      mrg 	    kva, size));
    727  1.7      mrg 
    728  1.7      mrg #ifdef DIAGNOSTIC
    729  1.7      mrg 	if ((u_long)kva & PGOFSET)
    730  1.7      mrg 		panic("iommu_dvmamem_unmap");
    731  1.7      mrg #endif
    732  1.7      mrg 
    733  1.7      mrg 	size = round_page(size);
    734  1.7      mrg 	pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
    735  1.8      mrg #if 0
    736  1.8      mrg 	/*
    737  1.8      mrg 	 * XXX ? is this necessary? i think so and i think other
    738  1.8      mrg 	 * implementations are missing it.
    739  1.8      mrg 	 */
    740  1.8      mrg 	uvm_km_free(kernel_map, (vaddr_t)kva, size);
    741  1.8      mrg #endif
    742  1.1      mrg }
    743