Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.13
      1  1.13  mrg /*	$NetBSD: iommu.c,v 1.13 2000/06/18 07:05:09 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.4  mrg #include "opt_ddb.h"
    116   1.4  mrg 
    117   1.1  mrg #include <sys/param.h>
    118   1.1  mrg #include <sys/extent.h>
    119   1.1  mrg #include <sys/malloc.h>
    120   1.1  mrg #include <sys/systm.h>
    121   1.1  mrg #include <sys/device.h>
    122   1.1  mrg #include <vm/vm.h>
    123   1.8  mrg #include <vm/vm_kern.h>
    124   1.1  mrg 
    125   1.1  mrg #include <machine/bus.h>
    126   1.7  mrg #include <sparc64/sparc64/cache.h>
    127   1.1  mrg #include <sparc64/sparc64/vaddrs.h>
    128   1.1  mrg #include <sparc64/dev/iommureg.h>
    129   1.1  mrg #include <sparc64/dev/iommuvar.h>
    130   1.1  mrg 
    131   1.1  mrg #include <machine/autoconf.h>
    132   1.1  mrg #include <machine/ctlreg.h>
    133   1.1  mrg #include <machine/cpu.h>
    134   1.1  mrg 
    135   1.1  mrg #ifdef DEBUG
    136   1.1  mrg #define IDB_DVMA	0x1
    137  1.10  mrg int iommudebug = 0x0;
    138   1.4  mrg #define DPRINTF(l, s)   do { if (iommudebug & l) printf s; } while (0)
    139   1.4  mrg #else
    140   1.4  mrg #define DPRINTF(l, s)
    141   1.1  mrg #endif
    142   1.1  mrg 
    143  1.11  eeh 
    144   1.1  mrg /*
    145   1.1  mrg  * initialise the UltraSPARC IOMMU (SBUS or PCI):
    146   1.1  mrg  *	- allocate and setup the iotsb.
    147   1.1  mrg  *	- enable the IOMMU
    148   1.7  mrg  *	- initialise the streaming buffers (if they exist)
    149   1.1  mrg  *	- create a private DVMA map.
    150   1.1  mrg  */
    151   1.1  mrg void
    152   1.1  mrg iommu_init(name, is, tsbsize)
    153   1.1  mrg 	char *name;
    154   1.1  mrg 	struct iommu_state *is;
    155   1.1  mrg 	int tsbsize;
    156   1.1  mrg {
    157  1.11  eeh 	psize_t size;
    158  1.11  eeh 	vaddr_t va;
    159  1.11  eeh 	paddr_t pa;
    160  1.11  eeh 	vm_page_t m;
    161  1.11  eeh 	struct pglist mlist;
    162   1.1  mrg 
    163   1.1  mrg 	/*
    164   1.1  mrg 	 * Setup the iommu.
    165   1.1  mrg 	 *
    166   1.7  mrg 	 * The sun4u iommu is part of the SBUS or PCI controller so we
    167   1.7  mrg 	 * will deal with it here..
    168   1.1  mrg 	 *
    169   1.2  eeh 	 * The IOMMU address space always ends at 0xffffe000, but the starting
    170   1.2  eeh 	 * address depends on the size of the map.  The map size is 1024 * 2 ^
    171   1.2  eeh 	 * is->is_tsbsize entries, where each entry is 8 bytes.  The start of
    172   1.2  eeh 	 * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
    173   1.2  eeh 	 *
    174   1.2  eeh 	 * Note: the stupid IOMMU ignores the high bits of an address, so a
    175   1.2  eeh 	 * NULL DMA pointer will be translated by the first page of the IOTSB.
    176   1.2  eeh 	 * To trap bugs we'll skip the first entry in the IOTSB.
    177   1.2  eeh 	 */
    178  1.11  eeh 	is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
    179  1.11  eeh 	is->is_tsbsize = tsbsize;
    180   1.2  eeh 	is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
    181  1.11  eeh 
    182  1.11  eeh 	/*
    183  1.11  eeh 	 * Allocate memory for I/O pagetables.
    184  1.11  eeh 	 * This takes 64K of contiguous physical memory to map 64M of
    185  1.11  eeh 	 * DVMA space (starting at IOMMU_DVMA_BASE).
    186  1.11  eeh 	 * The table must be aligned on a (-IOMMU_DVMA_BASE/pagesize)
    187  1.11  eeh 	 * boundary (i.e. 64K for 64M of DVMA space).
    188  1.11  eeh 	 */
    189  1.11  eeh 
    190  1.11  eeh 	size = NBPG<<(is->is_tsbsize);
    191  1.11  eeh 	TAILQ_INIT(&mlist);
    192  1.11  eeh 	if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
    193  1.11  eeh 		(paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
    194  1.11  eeh 		panic("iommu_init: no memory");
    195  1.11  eeh 
    196  1.11  eeh 	va = uvm_km_valloc(kernel_map, size);
    197  1.11  eeh 	if (va == 0)
    198  1.11  eeh 		panic("iommu_init: no memory");
    199  1.11  eeh 	is->is_tsb = (int64_t *)va;
    200  1.11  eeh 
    201  1.11  eeh 	m = TAILQ_FIRST(&mlist);
    202  1.11  eeh 	is->is_ptsb = VM_PAGE_TO_PHYS(m);
    203  1.11  eeh 
    204  1.11  eeh 	/* Map the pages */
    205  1.11  eeh 	for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
    206  1.11  eeh 		pa = VM_PAGE_TO_PHYS(m);
    207  1.11  eeh 		pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
    208  1.11  eeh 			VM_PROT_READ|VM_PROT_WRITE,
    209  1.11  eeh 			VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    210  1.11  eeh 		va += NBPG;
    211  1.11  eeh 	}
    212  1.11  eeh 	bzero(is->is_tsb, size);
    213   1.1  mrg 
    214   1.1  mrg #ifdef DEBUG
    215   1.1  mrg 	if (iommudebug & IDB_DVMA)
    216   1.1  mrg 	{
    217   1.1  mrg 		/* Probe the iommu */
    218   1.1  mrg 		struct iommureg *regs = is->is_iommu;
    219   1.1  mrg 		int64_t cr, tsb;
    220   1.1  mrg 
    221   1.1  mrg 		printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", &regs->iommu_cr,
    222   1.1  mrg 		       &regs->iommu_tsb, &regs->iommu_flush);
    223   1.1  mrg 		cr = regs->iommu_cr;
    224   1.1  mrg 		tsb = regs->iommu_tsb;
    225  1.11  eeh 		printf("iommu cr=%qx tsb=%qx\n", cr, tsb);
    226  1.11  eeh 		printf("TSB base %p phys %qx\n", (void *)is->is_tsb, (u_int64_t)is->is_ptsb);
    227   1.1  mrg 		delay(1000000); /* 1 s */
    228   1.1  mrg 	}
    229   1.1  mrg #endif
    230   1.1  mrg 
    231   1.1  mrg 	/*
    232   1.8  mrg 	 * Initialize streaming buffer, if it is there.
    233   1.1  mrg 	 */
    234   1.8  mrg 	if (is->is_sb)
    235   1.8  mrg 		(void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
    236   1.8  mrg 		    (paddr_t *)&is->is_flushpa);
    237   1.1  mrg 
    238   1.1  mrg 	/*
    239   1.1  mrg 	 * now actually start up the IOMMU
    240   1.1  mrg 	 */
    241   1.1  mrg 	iommu_reset(is);
    242   1.1  mrg 
    243   1.1  mrg 	/*
    244   1.1  mrg 	 * Now all the hardware's working we need to allocate a dvma map.
    245   1.1  mrg 	 */
    246  1.11  eeh 	printf("DVMA map: %x to %x\n",
    247  1.11  eeh 		(unsigned int)is->is_dvmabase,
    248  1.11  eeh 		(unsigned int)IOTSB_VEND);
    249   1.1  mrg 	is->is_dvmamap = extent_create(name,
    250   1.2  eeh 				       is->is_dvmabase, IOTSB_VEND,
    251   1.1  mrg 				       M_DEVBUF, 0, 0, EX_NOWAIT);
    252   1.1  mrg }
    253   1.1  mrg 
    254   1.8  mrg /*
    255   1.8  mrg  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
    256   1.8  mrg  * detected that already and disabled them.  If not, we will notice that
    257   1.8  mrg  * they aren't there when the STRBUF_EN bit does not remain.
    258   1.8  mrg  */
    259   1.1  mrg void
    260   1.1  mrg iommu_reset(is)
    261   1.1  mrg 	struct iommu_state *is;
    262   1.1  mrg {
    263   1.1  mrg 
    264   1.1  mrg 	/* Need to do 64-bit stores */
    265   1.1  mrg 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
    266  1.11  eeh 	/* Enable IOMMU in diagnostic mode */
    267  1.11  eeh 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0,
    268  1.11  eeh 		is->is_cr|IOMMUCR_DE);
    269  1.11  eeh 
    270   1.5  mrg 
    271   1.7  mrg 	if (!is->is_sb)
    272   1.7  mrg 		return;
    273   1.7  mrg 
    274   1.1  mrg 	/* Enable diagnostics mode? */
    275   1.1  mrg 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
    276   1.5  mrg 
    277   1.5  mrg 	/* No streaming buffers? Disable them */
    278   1.7  mrg 	if (bus_space_read_8(is->is_bustag,
    279   1.7  mrg 	    (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl, 0) == 0)
    280   1.5  mrg 		is->is_sb = 0;
    281   1.2  eeh }
    282   1.2  eeh 
    283   1.2  eeh /*
    284   1.2  eeh  * Here are the iommu control routines.
    285   1.2  eeh  */
    286   1.2  eeh void
    287   1.2  eeh iommu_enter(is, va, pa, flags)
    288   1.2  eeh 	struct iommu_state *is;
    289   1.2  eeh 	vaddr_t va;
    290   1.2  eeh 	int64_t pa;
    291   1.2  eeh 	int flags;
    292   1.2  eeh {
    293   1.2  eeh 	int64_t tte;
    294   1.2  eeh 
    295   1.2  eeh #ifdef DIAGNOSTIC
    296   1.2  eeh 	if (va < is->is_dvmabase)
    297  1.13  mrg 		panic("iommu_enter: va %#lx not in DVMA space", va);
    298   1.2  eeh #endif
    299   1.2  eeh 
    300   1.2  eeh 	tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
    301   1.2  eeh 			!(flags&BUS_DMA_COHERENT));
    302   1.2  eeh 
    303   1.2  eeh 	/* Is the streamcache flush really needed? */
    304   1.5  mrg 	if (is->is_sb) {
    305   1.5  mrg 		bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush, 0,
    306   1.5  mrg 		    va);
    307   1.5  mrg 		iommu_flush(is);
    308   1.5  mrg 	}
    309   1.4  mrg 	DPRINTF(IDB_DVMA, ("Clearing TSB slot %d for va %p\n",
    310   1.4  mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), va));
    311   1.2  eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    312   1.2  eeh 	bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush,
    313   1.2  eeh 			  0, va);
    314   1.4  mrg 	DPRINTF(IDB_DVMA, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    315   1.2  eeh 		       va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
    316   1.2  eeh 		       &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    317   1.4  mrg 		       (long)tte));
    318   1.2  eeh }
    319   1.2  eeh 
    320   1.2  eeh /*
    321   1.2  eeh  * iommu_remove: removes mappings created by iommu_enter
    322   1.2  eeh  *
    323   1.2  eeh  * Only demap from IOMMU if flag is set.
    324   1.8  mrg  *
    325   1.8  mrg  * XXX: this function needs better internal error checking.
    326   1.2  eeh  */
    327   1.2  eeh void
    328   1.2  eeh iommu_remove(is, va, len)
    329   1.2  eeh 	struct iommu_state *is;
    330   1.2  eeh 	vaddr_t va;
    331   1.2  eeh 	size_t len;
    332   1.2  eeh {
    333   1.2  eeh 
    334   1.2  eeh #ifdef DIAGNOSTIC
    335   1.2  eeh 	if (va < is->is_dvmabase)
    336   1.4  mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (long)va);
    337   1.2  eeh 	if ((long)(va + len) < (long)va)
    338   1.4  mrg 		panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
    339   1.2  eeh 		      (long) va, (long) len);
    340   1.2  eeh 	if (len & ~0xfffffff)
    341   1.4  mrg 		panic("iommu_remove: rediculous len 0x%lx", (long)len);
    342   1.2  eeh #endif
    343   1.2  eeh 
    344   1.2  eeh 	va = trunc_page(va);
    345   1.8  mrg 	DPRINTF(IDB_DVMA, ("iommu_remove: va %lx TSB[%lx]@%p\n",
    346   1.8  mrg 	    va, IOTSBSLOT(va,is->is_tsbsize),
    347   1.8  mrg 	    &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
    348   1.2  eeh 	while (len > 0) {
    349   1.8  mrg 		DPRINTF(IDB_DVMA, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
    350   1.8  mrg 		    (int)IOTSBSLOT(va,is->is_tsbsize), va, (u_long)len));
    351   1.5  mrg 		if (is->is_sb) {
    352   1.5  mrg 			DPRINTF(IDB_DVMA, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    353   1.2  eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    354   1.2  eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    355   1.2  eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    356   1.4  mrg 			       (u_long)len));
    357   1.5  mrg 			bus_space_write_8(is->is_bustag,
    358   1.5  mrg 			    &is->is_sb->strbuf_pgflush, 0, va);
    359  1.10  mrg 			if (len <= NBPG)
    360   1.5  mrg 				iommu_flush(is);
    361   1.5  mrg 			DPRINTF(IDB_DVMA, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    362   1.2  eeh 			       (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    363   1.2  eeh 			       (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    364   1.2  eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    365   1.4  mrg 			       (u_long)len));
    366  1.10  mrg 		} else
    367  1.10  mrg 			membar_sync();	/* XXX */
    368  1.10  mrg 
    369  1.10  mrg 		if (len <= NBPG)
    370  1.10  mrg 			len = 0;
    371  1.10  mrg 		else
    372   1.8  mrg 			len -= NBPG;
    373   1.8  mrg 
    374   1.2  eeh 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
    375   1.2  eeh 		bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush, 0, va);
    376   1.2  eeh 		va += NBPG;
    377   1.2  eeh 	}
    378   1.2  eeh }
    379   1.2  eeh 
    380   1.2  eeh int
    381   1.2  eeh iommu_flush(is)
    382   1.2  eeh 	struct iommu_state *is;
    383   1.2  eeh {
    384   1.2  eeh 	struct timeval cur, flushtimeout;
    385   1.2  eeh 
    386   1.2  eeh #define BUMPTIME(t, usec) { \
    387   1.2  eeh 	register volatile struct timeval *tp = (t); \
    388   1.2  eeh 	register long us; \
    389   1.2  eeh  \
    390   1.2  eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    391   1.2  eeh 	if (us >= 1000000) { \
    392   1.2  eeh 		tp->tv_usec = us - 1000000; \
    393   1.2  eeh 		tp->tv_sec++; \
    394   1.2  eeh 	} \
    395   1.2  eeh }
    396   1.5  mrg 
    397   1.5  mrg 	if (!is->is_sb)
    398   1.5  mrg 		return (0);
    399   1.7  mrg 
    400   1.7  mrg 	/*
    401   1.7  mrg 	 * Streaming buffer flushes:
    402   1.7  mrg 	 *
    403   1.7  mrg 	 *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
    404   1.7  mrg 	 *     we're not on a cache line boundary (64-bits):
    405   1.7  mrg 	 *   2 Store 0 in flag
    406   1.7  mrg 	 *   3 Store pointer to flag in flushsync
    407   1.7  mrg 	 *   4 wait till flushsync becomes 0x1
    408   1.7  mrg 	 *
    409   1.7  mrg 	 * If it takes more than .5 sec, something
    410   1.7  mrg 	 * went wrong.
    411   1.7  mrg 	 */
    412   1.2  eeh 
    413   1.2  eeh 	is->is_flush = 0;
    414   1.2  eeh 	membar_sync();
    415   1.2  eeh 	bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
    416   1.2  eeh 	membar_sync();
    417   1.2  eeh 
    418   1.2  eeh 	microtime(&flushtimeout);
    419   1.2  eeh 	cur = flushtimeout;
    420   1.2  eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    421   1.2  eeh 
    422   1.4  mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
    423   1.2  eeh 		       (long)is->is_flush, (long)&is->is_flush,
    424   1.2  eeh 		       (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
    425   1.4  mrg 		       flushtimeout.tv_sec, flushtimeout.tv_usec));
    426   1.2  eeh 	/* Bypass non-coherent D$ */
    427   1.2  eeh 	while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
    428   1.2  eeh 	       ((cur.tv_sec <= flushtimeout.tv_sec) &&
    429   1.2  eeh 		(cur.tv_usec <= flushtimeout.tv_usec)))
    430   1.2  eeh 		microtime(&cur);
    431   1.2  eeh 
    432   1.2  eeh #ifdef DIAGNOSTIC
    433   1.2  eeh 	if (!is->is_flush) {
    434   1.4  mrg 		printf("iommu_flush: flush timeout %p at %p\n", (long)is->is_flush,
    435   1.2  eeh 		       (long)is->is_flushpa); /* panic? */
    436   1.2  eeh #ifdef DDB
    437   1.2  eeh 		Debugger();
    438   1.2  eeh #endif
    439   1.2  eeh 	}
    440   1.2  eeh #endif
    441   1.4  mrg 	DPRINTF(IDB_DVMA, ("iommu_flush: flushed\n"));
    442   1.2  eeh 	return (is->is_flush);
    443   1.7  mrg }
    444   1.7  mrg 
    445   1.7  mrg /*
    446   1.7  mrg  * IOMMU DVMA operations, common to SBUS and PCI.
    447   1.7  mrg  */
    448   1.7  mrg int
    449   1.7  mrg iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
    450   1.7  mrg 	bus_dma_tag_t t;
    451   1.7  mrg 	struct iommu_state *is;
    452   1.7  mrg 	bus_dmamap_t map;
    453   1.7  mrg 	void *buf;
    454   1.7  mrg 	bus_size_t buflen;
    455   1.7  mrg 	struct proc *p;
    456   1.7  mrg 	int flags;
    457   1.7  mrg {
    458   1.7  mrg 	int s;
    459   1.7  mrg 	int err;
    460   1.7  mrg 	bus_size_t sgsize;
    461   1.7  mrg 	paddr_t curaddr;
    462   1.7  mrg 	u_long dvmaddr;
    463   1.7  mrg 	vaddr_t vaddr = (vaddr_t)buf;
    464   1.7  mrg 	pmap_t pmap;
    465   1.7  mrg 
    466   1.7  mrg 	if (map->dm_nsegs) {
    467   1.7  mrg 		/* Already in use?? */
    468   1.7  mrg #ifdef DIAGNOSTIC
    469   1.7  mrg 		printf("iommu_dvmamap_load: map still in use\n");
    470   1.7  mrg #endif
    471   1.7  mrg 		bus_dmamap_unload(t, map);
    472   1.7  mrg 	}
    473   1.7  mrg 	/*
    474   1.7  mrg 	 * Make sure that on error condition we return "no valid mappings".
    475   1.7  mrg 	 */
    476   1.7  mrg 	map->dm_nsegs = 0;
    477   1.7  mrg 
    478   1.7  mrg 	if (buflen > map->_dm_size) {
    479   1.7  mrg 		DPRINTF(IDB_DVMA,
    480   1.7  mrg 		    ("iommu_dvmamap_load(): error %d > %d -- "
    481   1.7  mrg 		     "map size exceeded!\n", buflen, map->_dm_size));
    482   1.7  mrg 		return (EINVAL);
    483   1.7  mrg 	}
    484   1.7  mrg 
    485  1.10  mrg #if 1
    486   1.7  mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    487  1.10  mrg #else
    488  1.10  mrg 	sgsize = buflen + ((int)vaddr & PGOFSET);
    489  1.10  mrg #endif
    490   1.7  mrg 	/*
    491   1.7  mrg 	 * XXX Need to implement "don't dma across this boundry".
    492   1.7  mrg 	 */
    493   1.7  mrg 	s = splhigh();
    494   1.7  mrg 	err = extent_alloc(is->is_dvmamap, sgsize, NBPG,
    495   1.7  mrg 	    map->_dm_boundary, EX_NOWAIT, (u_long *)&dvmaddr);
    496   1.7  mrg 	splx(s);
    497   1.7  mrg 
    498   1.7  mrg #ifdef DEBUG
    499  1.11  eeh 	if (err || (dvmaddr == (bus_addr_t)-1))
    500   1.7  mrg 	{
    501   1.7  mrg 		printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
    502   1.7  mrg 		    sgsize, flags);
    503   1.7  mrg 		Debugger();
    504   1.7  mrg 	}
    505   1.7  mrg #endif
    506  1.11  eeh 	if (err != 0)
    507  1.11  eeh 		return (err);
    508  1.11  eeh 
    509   1.7  mrg 	if (dvmaddr == (bus_addr_t)-1)
    510   1.7  mrg 		return (ENOMEM);
    511   1.7  mrg 
    512   1.7  mrg 	/*
    513   1.7  mrg 	 * We always use just one segment.
    514   1.7  mrg 	 */
    515   1.7  mrg 	map->dm_mapsize = buflen;
    516   1.7  mrg 	map->dm_nsegs = 1;
    517   1.7  mrg 	map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
    518   1.7  mrg 	map->dm_segs[0].ds_len = sgsize;
    519   1.7  mrg 
    520   1.7  mrg 	if (p != NULL)
    521   1.7  mrg 		pmap = p->p_vmspace->vm_map.pmap;
    522   1.7  mrg 	else
    523   1.7  mrg 		pmap = pmap_kernel();
    524   1.7  mrg 
    525   1.7  mrg 	dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
    526   1.7  mrg 	for (; buflen > 0; ) {
    527   1.7  mrg 		/*
    528   1.7  mrg 		 * Get the physical address for this page.
    529   1.7  mrg 		 */
    530   1.7  mrg 		if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
    531   1.7  mrg 			bus_dmamap_unload(t, map);
    532   1.7  mrg 			return (-1);
    533   1.7  mrg 		}
    534   1.7  mrg 
    535   1.7  mrg 		/*
    536   1.7  mrg 		 * Compute the segment size, and adjust counts.
    537   1.7  mrg 		 */
    538   1.7  mrg 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    539   1.7  mrg 		if (buflen < sgsize)
    540   1.7  mrg 			sgsize = buflen;
    541   1.7  mrg 
    542   1.7  mrg 		DPRINTF(IDB_DVMA,
    543  1.11  eeh 		    ("iommu_dvmamap_load: map %p loading va %p dva %lx at pa %lx\n",
    544  1.11  eeh 		    map, (void *)vaddr, (long)dvmaddr, (long)(curaddr&~(NBPG-1))));
    545   1.7  mrg 		iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
    546   1.7  mrg 		    flags);
    547   1.7  mrg 
    548   1.7  mrg 		dvmaddr += PAGE_SIZE;
    549   1.7  mrg 		vaddr += sgsize;
    550   1.7  mrg 		buflen -= sgsize;
    551   1.7  mrg 	}
    552   1.7  mrg 	return (0);
    553   1.7  mrg }
    554   1.7  mrg 
    555   1.7  mrg 
    556   1.7  mrg void
    557   1.7  mrg iommu_dvmamap_unload(t, is, map)
    558   1.7  mrg 	bus_dma_tag_t t;
    559   1.7  mrg 	struct iommu_state *is;
    560   1.7  mrg 	bus_dmamap_t map;
    561   1.7  mrg {
    562   1.7  mrg 	vaddr_t addr;
    563  1.10  mrg 	size_t len;
    564   1.7  mrg 	int error, s;
    565   1.7  mrg 	bus_addr_t dvmaddr;
    566   1.7  mrg 	bus_size_t sgsize;
    567   1.7  mrg 
    568   1.7  mrg 	if (map->dm_nsegs != 1)
    569   1.7  mrg 		panic("iommu_dvmamap_unload: nsegs = %d", map->dm_nsegs);
    570   1.7  mrg 
    571   1.7  mrg 	addr = trunc_page(map->dm_segs[0].ds_addr);
    572   1.7  mrg 	len = map->dm_segs[0].ds_len;
    573   1.7  mrg 
    574   1.7  mrg 	DPRINTF(IDB_DVMA,
    575   1.7  mrg 	    ("iommu_dvmamap_unload: map %p removing va %lx size %lx\n",
    576   1.7  mrg 	    map, (long)addr, (long)len));
    577   1.7  mrg 	iommu_remove(is, addr, len);
    578   1.7  mrg 	dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
    579   1.7  mrg 	sgsize = map->dm_segs[0].ds_len;
    580   1.7  mrg 
    581   1.7  mrg 	/* Mark the mappings as invalid. */
    582   1.7  mrg 	map->dm_mapsize = 0;
    583   1.7  mrg 	map->dm_nsegs = 0;
    584   1.7  mrg 
    585   1.7  mrg 	s = splhigh();
    586   1.7  mrg 	error = extent_free(is->is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
    587   1.7  mrg 	splx(s);
    588   1.7  mrg 	if (error != 0)
    589   1.7  mrg 		printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
    590   1.7  mrg 	cache_flush((caddr_t)(u_long)dvmaddr, (u_int)sgsize);
    591   1.9  eeh }
    592   1.9  eeh 
    593   1.9  eeh 
    594   1.9  eeh int
    595   1.9  eeh iommu_dvmamap_load_raw(t, is, map, segs, nsegs, size, flags)
    596   1.9  eeh 	bus_dma_tag_t t;
    597   1.9  eeh 	struct iommu_state *is;
    598   1.9  eeh 	bus_dmamap_t map;
    599   1.9  eeh 	bus_dma_segment_t *segs;
    600   1.9  eeh 	int nsegs;
    601   1.9  eeh 	bus_size_t size;
    602   1.9  eeh 	int flags;
    603   1.9  eeh {
    604   1.9  eeh 	vm_page_t m;
    605   1.9  eeh 	int s;
    606   1.9  eeh 	int err;
    607   1.9  eeh 	bus_size_t sgsize;
    608   1.9  eeh 	paddr_t pa;
    609   1.9  eeh 	u_long boundary;
    610   1.9  eeh 	u_long dvmaddr;
    611   1.9  eeh 	struct pglist *mlist;
    612   1.9  eeh 	int pagesz = PAGE_SIZE;
    613   1.9  eeh 
    614   1.9  eeh 	if (map->dm_nsegs) {
    615   1.9  eeh 		/* Already in use?? */
    616   1.9  eeh #ifdef DIAGNOSTIC
    617   1.9  eeh 		printf("iommu_dvmamap_load_raw: map still in use\n");
    618   1.9  eeh #endif
    619   1.9  eeh 		bus_dmamap_unload(t, map);
    620   1.9  eeh 	}
    621   1.9  eeh 	/*
    622   1.9  eeh 	 * Make sure that on error condition we return "no valid mappings".
    623   1.9  eeh 	 */
    624   1.9  eeh 	map->dm_nsegs = 0;
    625   1.9  eeh #ifdef DIAGNOSTIC
    626   1.9  eeh 	/* XXX - unhelpful since we can't reset these in map_unload() */
    627  1.10  mrg 	if (segs[0].ds_addr != 0)
    628  1.10  mrg 		panic("iommu_dvmamap_load_raw: segment already loaded: "
    629  1.10  mrg 			"addr %#llx, size %#llx",
    630  1.10  mrg 			(u_int64_t)segs[0].ds_addr, (u_int64_t)segs[0].ds_len);
    631  1.10  mrg 	if (segs[0].ds_len != size)
    632  1.10  mrg 		panic("iommu_dvmamap_load_raw: segment size changed: "
    633  1.10  mrg 			"ds_len %#llx size %#llx", segs[0].ds_len, size);
    634   1.9  eeh #endif
    635   1.9  eeh 	sgsize = round_page(size);
    636   1.9  eeh 
    637   1.9  eeh 	/*
    638   1.9  eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    639   1.9  eeh 	 * over boundary in the map.
    640   1.9  eeh 	 */
    641   1.9  eeh 	if ((boundary = segs[0]._ds_boundary) == 0)
    642   1.9  eeh 		boundary = map->_dm_boundary;
    643   1.9  eeh 
    644   1.9  eeh 	s = splhigh();
    645   1.9  eeh 	err = extent_alloc(is->is_dvmamap, sgsize, NBPG, boundary,
    646   1.9  eeh 			   (flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT,
    647   1.9  eeh 			   (u_long *)&dvmaddr);
    648   1.9  eeh 	splx(s);
    649   1.9  eeh 
    650   1.9  eeh 	if (err != 0)
    651   1.9  eeh 		return (err);
    652   1.9  eeh 
    653   1.9  eeh #ifdef DEBUG
    654   1.9  eeh 	if (dvmaddr == (bus_addr_t)-1)
    655   1.9  eeh 	{
    656   1.9  eeh 		printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
    657   1.9  eeh 		    sgsize, flags);
    658   1.9  eeh 		Debugger();
    659   1.9  eeh 	}
    660   1.9  eeh #endif
    661   1.9  eeh 	if (dvmaddr == (bus_addr_t)-1)
    662   1.9  eeh 		return (ENOMEM);
    663   1.9  eeh 
    664   1.9  eeh 	/*
    665   1.9  eeh 	 * We always use just one segment.
    666   1.9  eeh 	 */
    667   1.9  eeh 	map->dm_mapsize = size;
    668   1.9  eeh 	map->dm_nsegs = 1;
    669   1.9  eeh 	map->dm_segs[0].ds_addr = dvmaddr;
    670   1.9  eeh 	map->dm_segs[0].ds_len = size;
    671   1.9  eeh 
    672   1.9  eeh 	mlist = segs[0]._ds_mlist;
    673   1.9  eeh 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    674   1.9  eeh 		if (sgsize == 0)
    675   1.9  eeh 			panic("iommu_dmamap_load_raw: size botch");
    676   1.9  eeh 		pa = VM_PAGE_TO_PHYS(m);
    677   1.9  eeh 
    678   1.9  eeh 		DPRINTF(IDB_DVMA,
    679   1.9  eeh 		    ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
    680   1.9  eeh 		    map, (long)dvmaddr, (long)(pa)));
    681   1.9  eeh 		iommu_enter(is, dvmaddr, pa, flags);
    682   1.9  eeh 
    683   1.9  eeh 		dvmaddr += pagesz;
    684   1.9  eeh 		sgsize -= pagesz;
    685   1.9  eeh 	}
    686   1.9  eeh 	return (0);
    687   1.7  mrg }
    688   1.7  mrg 
    689   1.7  mrg void
    690   1.7  mrg iommu_dvmamap_sync(t, is, map, offset, len, ops)
    691   1.7  mrg 	bus_dma_tag_t t;
    692   1.7  mrg 	struct iommu_state *is;
    693   1.7  mrg 	bus_dmamap_t map;
    694   1.7  mrg 	bus_addr_t offset;
    695   1.7  mrg 	bus_size_t len;
    696   1.7  mrg 	int ops;
    697   1.7  mrg {
    698   1.7  mrg 	vaddr_t va = map->dm_segs[0].ds_addr + offset;
    699   1.7  mrg 
    700   1.7  mrg 	/*
    701   1.7  mrg 	 * We only support one DMA segment; supporting more makes this code
    702   1.7  mrg          * too unweildy.
    703   1.7  mrg 	 */
    704   1.7  mrg 
    705   1.7  mrg 	if (ops & BUS_DMASYNC_PREREAD) {
    706   1.7  mrg 		DPRINTF(IDB_DVMA,
    707   1.7  mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    708   1.7  mrg 		     "BUS_DMASYNC_PREREAD\n", (long)va, (u_long)len));
    709   1.7  mrg 
    710   1.7  mrg 		/* Nothing to do */;
    711   1.7  mrg 	}
    712   1.7  mrg 	if (ops & BUS_DMASYNC_POSTREAD) {
    713   1.7  mrg 		DPRINTF(IDB_DVMA,
    714   1.7  mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    715   1.7  mrg 		     "BUS_DMASYNC_POSTREAD\n", (long)va, (u_long)len));
    716   1.7  mrg 		/* if we have a streaming buffer, flush it here first */
    717   1.7  mrg 		if (is->is_sb)
    718   1.7  mrg 			while (len > 0) {
    719   1.7  mrg 				DPRINTF(IDB_DVMA,
    720   1.7  mrg 				    ("iommu_dvmamap_sync: flushing va %p, %lu "
    721   1.7  mrg 				     "bytes left\n", (long)va, (u_long)len));
    722   1.7  mrg 				bus_space_write_8(is->is_bustag,
    723   1.7  mrg 				    &is->is_sb->strbuf_pgflush, 0, va);
    724   1.7  mrg 				if (len <= NBPG) {
    725   1.7  mrg 					iommu_flush(is);
    726   1.7  mrg 					len = 0;
    727   1.7  mrg 				} else
    728   1.7  mrg 					len -= NBPG;
    729   1.7  mrg 				va += NBPG;
    730   1.7  mrg 			}
    731   1.7  mrg 	}
    732   1.7  mrg 	if (ops & BUS_DMASYNC_PREWRITE) {
    733   1.7  mrg 		DPRINTF(IDB_DVMA,
    734   1.7  mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    735   1.7  mrg 		     "BUS_DMASYNC_PREWRITE\n", (long)va, (u_long)len));
    736   1.7  mrg 		/* Nothing to do */;
    737   1.7  mrg 	}
    738   1.7  mrg 	if (ops & BUS_DMASYNC_POSTWRITE) {
    739   1.7  mrg 		DPRINTF(IDB_DVMA,
    740   1.7  mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    741   1.7  mrg 		     "BUS_DMASYNC_POSTWRITE\n", (long)va, (u_long)len));
    742   1.7  mrg 		/* Nothing to do */;
    743   1.7  mrg 	}
    744   1.7  mrg }
    745   1.7  mrg 
    746   1.7  mrg int
    747   1.7  mrg iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
    748   1.7  mrg 	bus_dma_tag_t t;
    749   1.7  mrg 	struct iommu_state *is;
    750   1.7  mrg 	bus_size_t size, alignment, boundary;
    751   1.7  mrg 	bus_dma_segment_t *segs;
    752   1.7  mrg 	int nsegs;
    753   1.7  mrg 	int *rsegs;
    754   1.7  mrg 	int flags;
    755   1.7  mrg {
    756   1.7  mrg 
    757   1.7  mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_alloc: sz %qx align %qx bound %qx "
    758   1.8  mrg 	   "segp %p flags %d\n", size, alignment, boundary, segs, flags));
    759   1.7  mrg 	return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
    760   1.7  mrg 	    segs, nsegs, rsegs, flags));
    761   1.7  mrg }
    762   1.7  mrg 
    763   1.7  mrg void
    764   1.7  mrg iommu_dvmamem_free(t, is, segs, nsegs)
    765   1.7  mrg 	bus_dma_tag_t t;
    766   1.7  mrg 	struct iommu_state *is;
    767   1.7  mrg 	bus_dma_segment_t *segs;
    768   1.7  mrg 	int nsegs;
    769   1.7  mrg {
    770   1.7  mrg 
    771   1.7  mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
    772   1.7  mrg 	    segs, nsegs));
    773   1.7  mrg 	bus_dmamem_free(t->_parent, segs, nsegs);
    774   1.7  mrg }
    775   1.7  mrg 
    776   1.7  mrg /*
    777   1.7  mrg  * Map the DVMA mappings into the kernel pmap.
    778   1.7  mrg  * Check the flags to see whether we're streaming or coherent.
    779   1.7  mrg  */
    780   1.7  mrg int
    781   1.7  mrg iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
    782   1.7  mrg 	bus_dma_tag_t t;
    783   1.7  mrg 	struct iommu_state *is;
    784   1.7  mrg 	bus_dma_segment_t *segs;
    785   1.7  mrg 	int nsegs;
    786   1.7  mrg 	size_t size;
    787   1.7  mrg 	caddr_t *kvap;
    788   1.7  mrg 	int flags;
    789   1.7  mrg {
    790   1.7  mrg 	vm_page_t m;
    791   1.7  mrg 	vaddr_t va;
    792   1.7  mrg 	bus_addr_t addr;
    793   1.7  mrg 	struct pglist *mlist;
    794   1.8  mrg 	int cbit;
    795   1.7  mrg 
    796   1.7  mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
    797   1.7  mrg 	    segs, nsegs, size));
    798   1.7  mrg 
    799   1.7  mrg 	/*
    800   1.8  mrg 	 * Allocate some space in the kernel map, and then map these pages
    801   1.8  mrg 	 * into this space.
    802   1.7  mrg 	 */
    803   1.8  mrg 	size = round_page(size);
    804   1.8  mrg 	va = uvm_km_valloc(kernel_map, size);
    805   1.8  mrg 	if (va == 0)
    806   1.8  mrg 		return (ENOMEM);
    807   1.7  mrg 
    808   1.8  mrg 	*kvap = (caddr_t)va;
    809   1.7  mrg 
    810   1.7  mrg 	/*
    811   1.7  mrg 	 * digest flags:
    812   1.7  mrg 	 */
    813   1.7  mrg 	cbit = 0;
    814   1.7  mrg 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
    815   1.7  mrg 		cbit |= PMAP_NVC;
    816   1.7  mrg 	if (flags & BUS_DMA_NOCACHE)	/* sideffects */
    817   1.7  mrg 		cbit |= PMAP_NC;
    818   1.7  mrg 
    819   1.7  mrg 	/*
    820   1.8  mrg 	 * Now take this and map it into the CPU.
    821   1.7  mrg 	 */
    822   1.7  mrg 	mlist = segs[0]._ds_mlist;
    823   1.7  mrg 	for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
    824   1.8  mrg #ifdef DIAGNOSTIC
    825   1.7  mrg 		if (size == 0)
    826   1.7  mrg 			panic("iommu_dvmamem_map: size botch");
    827   1.8  mrg #endif
    828   1.7  mrg 		addr = VM_PAGE_TO_PHYS(m);
    829   1.7  mrg 		DPRINTF(IDB_DVMA, ("iommu_dvmamem_map: "
    830   1.7  mrg 		    "mapping va %lx at %qx\n", va, addr | cbit));
    831   1.7  mrg 		pmap_enter(pmap_kernel(), va, addr | cbit,
    832   1.7  mrg 		    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
    833   1.7  mrg 		va += PAGE_SIZE;
    834   1.7  mrg 		size -= PAGE_SIZE;
    835   1.7  mrg 	}
    836   1.7  mrg 
    837   1.7  mrg 	return (0);
    838   1.7  mrg }
    839   1.7  mrg 
    840   1.7  mrg /*
    841   1.7  mrg  * Unmap DVMA mappings from kernel
    842   1.7  mrg  */
    843   1.7  mrg void
    844   1.7  mrg iommu_dvmamem_unmap(t, is, kva, size)
    845   1.7  mrg 	bus_dma_tag_t t;
    846   1.7  mrg 	struct iommu_state *is;
    847   1.7  mrg 	caddr_t kva;
    848   1.7  mrg 	size_t size;
    849   1.7  mrg {
    850   1.7  mrg 
    851   1.7  mrg 	DPRINTF(IDB_DVMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
    852   1.7  mrg 	    kva, size));
    853   1.7  mrg 
    854   1.7  mrg #ifdef DIAGNOSTIC
    855   1.7  mrg 	if ((u_long)kva & PGOFSET)
    856   1.7  mrg 		panic("iommu_dvmamem_unmap");
    857   1.7  mrg #endif
    858   1.7  mrg 
    859   1.7  mrg 	size = round_page(size);
    860   1.7  mrg 	pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
    861   1.8  mrg #if 0
    862   1.8  mrg 	/*
    863   1.8  mrg 	 * XXX ? is this necessary? i think so and i think other
    864   1.8  mrg 	 * implementations are missing it.
    865   1.8  mrg 	 */
    866   1.8  mrg 	uvm_km_free(kernel_map, (vaddr_t)kva, size);
    867   1.8  mrg #endif
    868   1.1  mrg }
    869