Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.35.2.5
      1  1.35.2.5  jdolecek /*	$NetBSD: iommu.c,v 1.35.2.5 2002/03/16 15:59:56 jdolecek Exp $	*/
      2       1.7       mrg 
      3       1.7       mrg /*
      4  1.35.2.5  jdolecek  * Copyright (c) 2001, 2002 Eduardo Horvath
      5       1.7       mrg  * Copyright (c) 1999, 2000 Matthew R. Green
      6       1.7       mrg  * All rights reserved.
      7       1.7       mrg  *
      8       1.7       mrg  * Redistribution and use in source and binary forms, with or without
      9       1.7       mrg  * modification, are permitted provided that the following conditions
     10       1.7       mrg  * are met:
     11       1.7       mrg  * 1. Redistributions of source code must retain the above copyright
     12       1.7       mrg  *    notice, this list of conditions and the following disclaimer.
     13       1.7       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.7       mrg  *    notice, this list of conditions and the following disclaimer in the
     15       1.7       mrg  *    documentation and/or other materials provided with the distribution.
     16       1.7       mrg  * 3. The name of the author may not be used to endorse or promote products
     17       1.7       mrg  *    derived from this software without specific prior written permission.
     18       1.7       mrg  *
     19       1.7       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.7       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.7       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.7       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.7       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24       1.7       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25       1.7       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26       1.7       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27       1.7       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.7       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.7       mrg  * SUCH DAMAGE.
     30       1.7       mrg  */
     31       1.1       mrg 
     32       1.7       mrg /*
     33       1.7       mrg  * UltraSPARC IOMMU support; used by both the sbus and pci code.
     34       1.7       mrg  */
     35       1.4       mrg #include "opt_ddb.h"
     36       1.4       mrg 
     37       1.1       mrg #include <sys/param.h>
     38       1.1       mrg #include <sys/extent.h>
     39       1.1       mrg #include <sys/malloc.h>
     40       1.1       mrg #include <sys/systm.h>
     41       1.1       mrg #include <sys/device.h>
     42  1.35.2.4   thorpej #include <sys/proc.h>
     43      1.18       mrg 
     44      1.18       mrg #include <uvm/uvm_extern.h>
     45       1.1       mrg 
     46       1.1       mrg #include <machine/bus.h>
     47       1.7       mrg #include <sparc64/sparc64/cache.h>
     48       1.1       mrg #include <sparc64/dev/iommureg.h>
     49       1.1       mrg #include <sparc64/dev/iommuvar.h>
     50       1.1       mrg 
     51       1.1       mrg #include <machine/autoconf.h>
     52       1.1       mrg #include <machine/cpu.h>
     53       1.1       mrg 
     54       1.1       mrg #ifdef DEBUG
     55      1.22       mrg #define IDB_BUSDMA	0x1
     56      1.22       mrg #define IDB_IOMMU	0x2
     57      1.22       mrg #define IDB_INFO	0x4
     58  1.35.2.1     lukem #define	IDB_SYNC	0x8
     59      1.10       mrg int iommudebug = 0x0;
     60       1.4       mrg #define DPRINTF(l, s)   do { if (iommudebug & l) printf s; } while (0)
     61       1.4       mrg #else
     62       1.4       mrg #define DPRINTF(l, s)
     63       1.1       mrg #endif
     64       1.1       mrg 
     65  1.35.2.4   thorpej #define iommu_strbuf_flush(i,v) do {				\
     66  1.35.2.4   thorpej 	if ((i)->is_sb[0])					\
     67  1.35.2.4   thorpej 		bus_space_write_8((i)->is_bustag,		\
     68  1.35.2.4   thorpej 			(bus_space_handle_t)(u_long)		\
     69  1.35.2.4   thorpej 			&(i)->is_sb[0]->strbuf_pgflush,		\
     70  1.35.2.4   thorpej 			0, (v));				\
     71  1.35.2.4   thorpej 	if ((i)->is_sb[1])					\
     72  1.35.2.4   thorpej 		bus_space_write_8((i)->is_bustag,		\
     73  1.35.2.4   thorpej 			(bus_space_handle_t)(u_long)		\
     74  1.35.2.4   thorpej 			&(i)->is_sb[1]->strbuf_pgflush,		\
     75  1.35.2.4   thorpej 			0, (v));				\
     76  1.35.2.4   thorpej 	} while (0)
     77  1.35.2.4   thorpej 
     78      1.31       eeh static	int iommu_strbuf_flush_done __P((struct iommu_state *));
     79      1.11       eeh 
     80       1.1       mrg /*
     81       1.1       mrg  * initialise the UltraSPARC IOMMU (SBUS or PCI):
     82       1.1       mrg  *	- allocate and setup the iotsb.
     83       1.1       mrg  *	- enable the IOMMU
     84       1.7       mrg  *	- initialise the streaming buffers (if they exist)
     85       1.1       mrg  *	- create a private DVMA map.
     86       1.1       mrg  */
     87       1.1       mrg void
     88  1.35.2.1     lukem iommu_init(name, is, tsbsize, iovabase)
     89       1.1       mrg 	char *name;
     90       1.1       mrg 	struct iommu_state *is;
     91       1.1       mrg 	int tsbsize;
     92  1.35.2.1     lukem 	u_int32_t iovabase;
     93       1.1       mrg {
     94      1.11       eeh 	psize_t size;
     95      1.11       eeh 	vaddr_t va;
     96      1.11       eeh 	paddr_t pa;
     97      1.35       chs 	struct vm_page *m;
     98      1.11       eeh 	struct pglist mlist;
     99       1.1       mrg 
    100       1.1       mrg 	/*
    101       1.1       mrg 	 * Setup the iommu.
    102       1.1       mrg 	 *
    103  1.35.2.5  jdolecek 	 * The sun4u iommu is part of the SBUS or PCI controller so we will
    104  1.35.2.5  jdolecek 	 * deal with it here..
    105       1.1       mrg 	 *
    106  1.35.2.5  jdolecek 	 * For sysio and psycho/psycho+ the IOMMU address space always ends at
    107  1.35.2.5  jdolecek 	 * 0xffffe000, but the starting address depends on the size of the
    108  1.35.2.5  jdolecek 	 * map.  The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
    109  1.35.2.5  jdolecek 	 * entry is 8 bytes.  The start of the map can be calculated by
    110  1.35.2.5  jdolecek 	 * (0xffffe000 << (8 + is->is_tsbsize)).
    111  1.35.2.5  jdolecek 	 *
    112  1.35.2.5  jdolecek 	 * But sabre and hummingbird use a different scheme that seems to
    113  1.35.2.5  jdolecek 	 * be hard-wired, so we read the start and size from the PROM and
    114  1.35.2.5  jdolecek 	 * just use those values.
    115       1.2       eeh 	 */
    116      1.11       eeh 	is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
    117      1.11       eeh 	is->is_tsbsize = tsbsize;
    118  1.35.2.5  jdolecek 	if (iovabase == -1) {
    119  1.35.2.5  jdolecek 		is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
    120  1.35.2.5  jdolecek 		is->is_dvmaend = IOTSB_VEND;
    121  1.35.2.5  jdolecek 	} else {
    122  1.35.2.5  jdolecek 		is->is_dvmabase = iovabase;
    123  1.35.2.5  jdolecek 		is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize);
    124  1.35.2.5  jdolecek 	}
    125      1.11       eeh 
    126      1.11       eeh 	/*
    127      1.15       eeh 	 * Allocate memory for I/O pagetables.  They need to be physically
    128      1.15       eeh 	 * contiguous.
    129      1.11       eeh 	 */
    130      1.11       eeh 
    131      1.11       eeh 	size = NBPG<<(is->is_tsbsize);
    132      1.11       eeh 	TAILQ_INIT(&mlist);
    133      1.11       eeh 	if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
    134      1.11       eeh 		(paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
    135      1.11       eeh 		panic("iommu_init: no memory");
    136      1.11       eeh 
    137      1.11       eeh 	va = uvm_km_valloc(kernel_map, size);
    138      1.11       eeh 	if (va == 0)
    139      1.11       eeh 		panic("iommu_init: no memory");
    140      1.11       eeh 	is->is_tsb = (int64_t *)va;
    141      1.11       eeh 
    142      1.11       eeh 	m = TAILQ_FIRST(&mlist);
    143      1.11       eeh 	is->is_ptsb = VM_PAGE_TO_PHYS(m);
    144      1.11       eeh 
    145      1.11       eeh 	/* Map the pages */
    146      1.11       eeh 	for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
    147      1.11       eeh 		pa = VM_PAGE_TO_PHYS(m);
    148      1.11       eeh 		pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
    149      1.11       eeh 			VM_PROT_READ|VM_PROT_WRITE,
    150      1.11       eeh 			VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    151      1.11       eeh 		va += NBPG;
    152      1.11       eeh 	}
    153  1.35.2.3   thorpej 	pmap_update(pmap_kernel());
    154      1.11       eeh 	bzero(is->is_tsb, size);
    155       1.1       mrg 
    156       1.1       mrg #ifdef DEBUG
    157      1.22       mrg 	if (iommudebug & IDB_INFO)
    158       1.1       mrg 	{
    159       1.1       mrg 		/* Probe the iommu */
    160       1.1       mrg 		struct iommureg *regs = is->is_iommu;
    161       1.1       mrg 
    162      1.25       mrg 		printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
    163      1.25       mrg 		    (u_long)&regs->iommu_cr,
    164      1.25       mrg 		    (u_long)&regs->iommu_tsb,
    165      1.25       mrg 		    (u_long)&regs->iommu_flush);
    166      1.25       mrg 		printf("iommu cr=%llx tsb=%llx\n", (unsigned long long)regs->iommu_cr, (unsigned long long)regs->iommu_tsb);
    167      1.25       mrg 		printf("TSB base %p phys %llx\n", (void *)is->is_tsb, (unsigned long long)is->is_ptsb);
    168       1.1       mrg 		delay(1000000); /* 1 s */
    169       1.1       mrg 	}
    170       1.1       mrg #endif
    171       1.1       mrg 
    172       1.1       mrg 	/*
    173       1.8       mrg 	 * Initialize streaming buffer, if it is there.
    174       1.1       mrg 	 */
    175  1.35.2.4   thorpej 	if (is->is_sb[0] || is->is_sb[1])
    176  1.35.2.4   thorpej 		(void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush[0],
    177       1.8       mrg 		    (paddr_t *)&is->is_flushpa);
    178       1.1       mrg 
    179       1.1       mrg 	/*
    180       1.1       mrg 	 * now actually start up the IOMMU
    181       1.1       mrg 	 */
    182       1.1       mrg 	iommu_reset(is);
    183       1.1       mrg 
    184       1.1       mrg 	/*
    185       1.1       mrg 	 * Now all the hardware's working we need to allocate a dvma map.
    186       1.1       mrg 	 */
    187      1.11       eeh 	printf("DVMA map: %x to %x\n",
    188      1.11       eeh 		(unsigned int)is->is_dvmabase,
    189  1.35.2.5  jdolecek 		(unsigned int)is->is_dvmaend);
    190  1.35.2.5  jdolecek 	printf("IOTSB: %llx to %llx\n",
    191  1.35.2.5  jdolecek 		(unsigned long long)is->is_ptsb,
    192  1.35.2.5  jdolecek 		(unsigned long long)(is->is_ptsb + size));
    193       1.1       mrg 	is->is_dvmamap = extent_create(name,
    194  1.35.2.5  jdolecek 				       is->is_dvmabase, is->is_dvmaend - NBPG,
    195       1.1       mrg 				       M_DEVBUF, 0, 0, EX_NOWAIT);
    196       1.1       mrg }
    197       1.1       mrg 
    198       1.8       mrg /*
    199       1.8       mrg  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
    200       1.8       mrg  * detected that already and disabled them.  If not, we will notice that
    201       1.8       mrg  * they aren't there when the STRBUF_EN bit does not remain.
    202       1.8       mrg  */
    203       1.1       mrg void
    204       1.1       mrg iommu_reset(is)
    205       1.1       mrg 	struct iommu_state *is;
    206       1.1       mrg {
    207  1.35.2.5  jdolecek 	struct iommu_strbuf *sb;
    208  1.35.2.5  jdolecek 	int i;
    209       1.1       mrg 
    210       1.1       mrg 	/* Need to do 64-bit stores */
    211      1.21       eeh 	bus_space_write_8(is->is_bustag,
    212  1.35.2.4   thorpej 		(bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
    213  1.35.2.4   thorpej 		0, is->is_ptsb);
    214      1.11       eeh 	/* Enable IOMMU in diagnostic mode */
    215      1.21       eeh 	bus_space_write_8(is->is_bustag,
    216  1.35.2.4   thorpej 		(bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr,
    217  1.35.2.4   thorpej 		0, is->is_cr|IOMMUCR_DE);
    218       1.5       mrg 
    219  1.35.2.5  jdolecek 	for (i=0; i<2; i++) {
    220  1.35.2.5  jdolecek 		if ((sb = is->is_sb[i]) != NULL) {
    221  1.35.2.4   thorpej 
    222  1.35.2.5  jdolecek 			/* Enable diagnostics mode? */
    223  1.35.2.5  jdolecek 			bus_space_write_8(is->is_bustag,
    224  1.35.2.5  jdolecek 				(bus_space_handle_t)(u_long)&sb->strbuf_ctl,
    225  1.35.2.5  jdolecek 				0, STRBUF_EN);
    226  1.35.2.5  jdolecek 
    227  1.35.2.5  jdolecek 			/* No streaming buffers? Disable them */
    228  1.35.2.5  jdolecek 			if (bus_space_read_8(is->is_bustag,
    229  1.35.2.5  jdolecek 				(bus_space_handle_t)(u_long)&sb->strbuf_ctl,
    230  1.35.2.5  jdolecek 				0) == 0)
    231  1.35.2.5  jdolecek 				is->is_sb[i] = 0;
    232  1.35.2.5  jdolecek 		}
    233  1.35.2.4   thorpej 	}
    234       1.2       eeh }
    235       1.2       eeh 
    236       1.2       eeh /*
    237       1.2       eeh  * Here are the iommu control routines.
    238       1.2       eeh  */
    239       1.2       eeh void
    240       1.2       eeh iommu_enter(is, va, pa, flags)
    241       1.2       eeh 	struct iommu_state *is;
    242       1.2       eeh 	vaddr_t va;
    243       1.2       eeh 	int64_t pa;
    244       1.2       eeh 	int flags;
    245       1.2       eeh {
    246       1.2       eeh 	int64_t tte;
    247       1.2       eeh 
    248       1.2       eeh #ifdef DIAGNOSTIC
    249  1.35.2.5  jdolecek 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    250      1.13       mrg 		panic("iommu_enter: va %#lx not in DVMA space", va);
    251       1.2       eeh #endif
    252       1.2       eeh 
    253       1.2       eeh 	tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
    254      1.32   thorpej 			(flags&BUS_DMA_STREAMING));
    255  1.35.2.5  jdolecek tte |= (flags & 0xff000LL)<<(4*8);/* DEBUG */
    256       1.2       eeh 
    257       1.2       eeh 	/* Is the streamcache flush really needed? */
    258  1.35.2.4   thorpej 	if (is->is_sb[0] || is->is_sb[1]) {
    259      1.31       eeh 		iommu_strbuf_flush(is, va);
    260      1.31       eeh 		iommu_strbuf_flush_done(is);
    261       1.5       mrg 	}
    262      1.22       mrg 	DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
    263      1.25       mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
    264       1.2       eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    265      1.21       eeh 	bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
    266      1.21       eeh 			  &is->is_iommu->iommu_flush, 0, va);
    267      1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    268      1.25       mrg 		       va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
    269      1.25       mrg 		       (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    270      1.25       mrg 		       (u_long)tte));
    271       1.2       eeh }
    272       1.2       eeh 
    273  1.35.2.4   thorpej 
    274  1.35.2.4   thorpej /*
    275  1.35.2.4   thorpej  * Find the value of a DVMA address (debug routine).
    276  1.35.2.4   thorpej  */
    277  1.35.2.4   thorpej paddr_t
    278  1.35.2.4   thorpej iommu_extract(is, dva)
    279  1.35.2.4   thorpej 	struct iommu_state *is;
    280  1.35.2.4   thorpej 	vaddr_t dva;
    281  1.35.2.4   thorpej {
    282  1.35.2.4   thorpej 	int64_t tte = 0;
    283  1.35.2.4   thorpej 
    284  1.35.2.5  jdolecek 	if (dva >= is->is_dvmabase && dva < is->is_dvmaend)
    285  1.35.2.4   thorpej 		tte = is->is_tsb[IOTSBSLOT(dva,is->is_tsbsize)];
    286  1.35.2.4   thorpej 
    287  1.35.2.4   thorpej 	if ((tte&IOTTE_V) == 0)
    288  1.35.2.4   thorpej 		return ((paddr_t)-1L);
    289  1.35.2.4   thorpej 	return (tte&IOTTE_PAMASK);
    290  1.35.2.4   thorpej }
    291  1.35.2.4   thorpej 
    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.35.2.5  jdolecek 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    308      1.25       mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (u_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.25       mrg 		panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
    314       1.2       eeh #endif
    315       1.2       eeh 
    316       1.2       eeh 	va = trunc_page(va);
    317      1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
    318      1.25       mrg 	    va, (u_long)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.22       mrg 		DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
    322      1.25       mrg 		    (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, (u_long)len));
    323  1.35.2.4   thorpej 		if (is->is_sb[0] || is->is_sb[0]) {
    324      1.22       mrg 			DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    325      1.25       mrg 			       (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    326      1.25       mrg 			       (void *)(u_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.31       eeh 			iommu_strbuf_flush(is, va);
    330      1.10       mrg 			if (len <= NBPG)
    331      1.31       eeh 				iommu_strbuf_flush_done(is);
    332      1.22       mrg 			DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
    333      1.25       mrg 			       (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
    334      1.25       mrg 			       (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    335       1.2       eeh 			       (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
    336       1.4       mrg 			       (u_long)len));
    337  1.35.2.1     lukem 		}
    338      1.10       mrg 
    339      1.10       mrg 		if (len <= NBPG)
    340      1.10       mrg 			len = 0;
    341      1.10       mrg 		else
    342       1.8       mrg 			len -= NBPG;
    343       1.8       mrg 
    344  1.35.2.5  jdolecek 		/* XXX Zero-ing the entry would not require RMW */
    345  1.35.2.5  jdolecek 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
    346      1.21       eeh 		bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
    347      1.21       eeh 				  &is->is_iommu->iommu_flush, 0, va);
    348       1.2       eeh 		va += NBPG;
    349       1.2       eeh 	}
    350       1.2       eeh }
    351       1.2       eeh 
    352      1.14       mrg static int
    353      1.31       eeh iommu_strbuf_flush_done(is)
    354       1.2       eeh 	struct iommu_state *is;
    355       1.2       eeh {
    356       1.2       eeh 	struct timeval cur, flushtimeout;
    357       1.2       eeh 
    358       1.2       eeh #define BUMPTIME(t, usec) { \
    359       1.2       eeh 	register volatile struct timeval *tp = (t); \
    360       1.2       eeh 	register long us; \
    361       1.2       eeh  \
    362       1.2       eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    363       1.2       eeh 	if (us >= 1000000) { \
    364       1.2       eeh 		tp->tv_usec = us - 1000000; \
    365       1.2       eeh 		tp->tv_sec++; \
    366       1.2       eeh 	} \
    367       1.2       eeh }
    368       1.5       mrg 
    369  1.35.2.4   thorpej 	if (!is->is_sb[0] && !is->is_sb[1])
    370       1.5       mrg 		return (0);
    371       1.7       mrg 
    372       1.7       mrg 	/*
    373       1.7       mrg 	 * Streaming buffer flushes:
    374       1.7       mrg 	 *
    375       1.7       mrg 	 *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
    376       1.7       mrg 	 *     we're not on a cache line boundary (64-bits):
    377       1.7       mrg 	 *   2 Store 0 in flag
    378       1.7       mrg 	 *   3 Store pointer to flag in flushsync
    379       1.7       mrg 	 *   4 wait till flushsync becomes 0x1
    380       1.7       mrg 	 *
    381       1.7       mrg 	 * If it takes more than .5 sec, something
    382       1.7       mrg 	 * went wrong.
    383       1.7       mrg 	 */
    384       1.2       eeh 
    385  1.35.2.4   thorpej 	is->is_flush[0] = 1;
    386  1.35.2.4   thorpej 	is->is_flush[1] = 1;
    387  1.35.2.4   thorpej 	if (is->is_sb[0]) {
    388  1.35.2.4   thorpej 		is->is_flush[0] = 0;
    389  1.35.2.4   thorpej 		bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
    390  1.35.2.4   thorpej 			&is->is_sb[0]->strbuf_flushsync, 0, is->is_flushpa);
    391  1.35.2.4   thorpej 	}
    392  1.35.2.4   thorpej 	if (is->is_sb[1]) {
    393  1.35.2.4   thorpej 		is->is_flush[0] = 1;
    394  1.35.2.4   thorpej 		bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
    395  1.35.2.4   thorpej 			&is->is_sb[1]->strbuf_flushsync, 0, is->is_flushpa + 8);
    396  1.35.2.4   thorpej 	}
    397       1.2       eeh 
    398       1.2       eeh 	microtime(&flushtimeout);
    399       1.2       eeh 	cur = flushtimeout;
    400       1.2       eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    401       1.2       eeh 
    402  1.35.2.4   thorpej 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx,%lx "
    403  1.35.2.4   thorpej 		"at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
    404  1.35.2.4   thorpej 		(long)is->is_flush[0], (long)is->is_flush[1],
    405  1.35.2.4   thorpej 		(long)&is->is_flush[0], (long)is->is_flushpa,
    406  1.35.2.4   thorpej 		cur.tv_sec, cur.tv_usec,
    407  1.35.2.4   thorpej 		flushtimeout.tv_sec, flushtimeout.tv_usec));
    408  1.35.2.4   thorpej 
    409       1.2       eeh 	/* Bypass non-coherent D$ */
    410  1.35.2.4   thorpej 	while ((!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
    411  1.35.2.4   thorpej 		!ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) &&
    412  1.35.2.4   thorpej 		((cur.tv_sec <= flushtimeout.tv_sec) &&
    413  1.35.2.4   thorpej 			(cur.tv_usec <= flushtimeout.tv_usec)))
    414       1.2       eeh 		microtime(&cur);
    415       1.2       eeh 
    416       1.2       eeh #ifdef DIAGNOSTIC
    417  1.35.2.4   thorpej 	if (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) ||
    418  1.35.2.4   thorpej 	    !ldxa(is->is_flushpa + 8, ASI_PHYS_CACHED)) {
    419  1.35.2.4   thorpej 		printf("iommu_strbuf_flush_done: flush timeout %p,%p at %p\n",
    420  1.35.2.4   thorpej 			(void *)(u_long)is->is_flush[0],
    421  1.35.2.4   thorpej 			(void *)(u_long)is->is_flush[1],
    422  1.35.2.4   thorpej 			(void *)(u_long)is->is_flushpa); /* panic? */
    423       1.2       eeh #ifdef DDB
    424       1.2       eeh 		Debugger();
    425       1.2       eeh #endif
    426       1.2       eeh 	}
    427       1.2       eeh #endif
    428      1.31       eeh 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
    429  1.35.2.4   thorpej 	return (is->is_flush[0] && is->is_flush[1]);
    430       1.7       mrg }
    431       1.7       mrg 
    432       1.7       mrg /*
    433       1.7       mrg  * IOMMU DVMA operations, common to SBUS and PCI.
    434       1.7       mrg  */
    435       1.7       mrg int
    436       1.7       mrg iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
    437       1.7       mrg 	bus_dma_tag_t t;
    438       1.7       mrg 	struct iommu_state *is;
    439       1.7       mrg 	bus_dmamap_t map;
    440       1.7       mrg 	void *buf;
    441       1.7       mrg 	bus_size_t buflen;
    442       1.7       mrg 	struct proc *p;
    443       1.7       mrg 	int flags;
    444       1.7       mrg {
    445       1.7       mrg 	int s;
    446       1.7       mrg 	int err;
    447       1.7       mrg 	bus_size_t sgsize;
    448       1.7       mrg 	paddr_t curaddr;
    449  1.35.2.4   thorpej 	u_long dvmaddr, sgstart, sgend;
    450      1.21       eeh 	bus_size_t align, boundary;
    451       1.7       mrg 	vaddr_t vaddr = (vaddr_t)buf;
    452  1.35.2.4   thorpej 	int seg;
    453       1.7       mrg 	pmap_t pmap;
    454       1.7       mrg 
    455       1.7       mrg 	if (map->dm_nsegs) {
    456       1.7       mrg 		/* Already in use?? */
    457       1.7       mrg #ifdef DIAGNOSTIC
    458       1.7       mrg 		printf("iommu_dvmamap_load: map still in use\n");
    459       1.7       mrg #endif
    460       1.7       mrg 		bus_dmamap_unload(t, map);
    461       1.7       mrg 	}
    462       1.7       mrg 	/*
    463       1.7       mrg 	 * Make sure that on error condition we return "no valid mappings".
    464       1.7       mrg 	 */
    465       1.7       mrg 	map->dm_nsegs = 0;
    466       1.7       mrg 
    467       1.7       mrg 	if (buflen > map->_dm_size) {
    468      1.22       mrg 		DPRINTF(IDB_BUSDMA,
    469       1.7       mrg 		    ("iommu_dvmamap_load(): error %d > %d -- "
    470      1.25       mrg 		     "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
    471       1.7       mrg 		return (EINVAL);
    472       1.7       mrg 	}
    473       1.7       mrg 
    474       1.7       mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    475      1.20       mrg 
    476       1.7       mrg 	/*
    477      1.21       eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    478      1.21       eeh 	 * over boundary in the map.
    479       1.7       mrg 	 */
    480      1.21       eeh 	if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
    481      1.21       eeh 		boundary = map->_dm_boundary;
    482      1.21       eeh 	align = max(map->dm_segs[0]._ds_align, NBPG);
    483       1.7       mrg 	s = splhigh();
    484  1.35.2.4   thorpej 	/*
    485  1.35.2.4   thorpej 	 * If our segment size is larger than the boundary we need to
    486  1.35.2.4   thorpej 	 * split the transfer up int little pieces ourselves.
    487  1.35.2.4   thorpej 	 */
    488  1.35.2.4   thorpej 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    489  1.35.2.4   thorpej 		(sgsize > boundary) ? 0 : boundary,
    490  1.35.2.4   thorpej 		EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
    491       1.7       mrg 	splx(s);
    492       1.7       mrg 
    493       1.7       mrg #ifdef DEBUG
    494      1.11       eeh 	if (err || (dvmaddr == (bus_addr_t)-1))
    495       1.7       mrg 	{
    496       1.7       mrg 		printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
    497      1.25       mrg 		    (int)sgsize, flags);
    498  1.35.2.4   thorpej #ifdef DDB
    499       1.7       mrg 		Debugger();
    500  1.35.2.4   thorpej #endif
    501       1.7       mrg 	}
    502       1.7       mrg #endif
    503      1.11       eeh 	if (err != 0)
    504      1.11       eeh 		return (err);
    505      1.11       eeh 
    506       1.7       mrg 	if (dvmaddr == (bus_addr_t)-1)
    507       1.7       mrg 		return (ENOMEM);
    508       1.7       mrg 
    509  1.35.2.4   thorpej 	/* Set the active DVMA map */
    510  1.35.2.4   thorpej 	map->_dm_dvmastart = dvmaddr;
    511  1.35.2.4   thorpej 	map->_dm_dvmasize = sgsize;
    512  1.35.2.4   thorpej 
    513  1.35.2.4   thorpej 	/*
    514  1.35.2.4   thorpej 	 * Now split the DVMA range into segments, not crossing
    515  1.35.2.4   thorpej 	 * the boundary.
    516  1.35.2.4   thorpej 	 */
    517  1.35.2.4   thorpej 	seg = 0;
    518  1.35.2.4   thorpej 	sgstart = dvmaddr + (vaddr & PGOFSET);
    519  1.35.2.4   thorpej 	sgend = sgstart + buflen - 1;
    520  1.35.2.4   thorpej 	map->dm_segs[seg].ds_addr = sgstart;
    521  1.35.2.4   thorpej 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary-1 %lx "
    522  1.35.2.4   thorpej 		"~(boundary-1) %lx\n", boundary, (boundary-1), ~(boundary-1)));
    523  1.35.2.4   thorpej 	while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
    524  1.35.2.4   thorpej 		/* Oops.  We crossed a boundary.  Split the xfer. */
    525  1.35.2.4   thorpej 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    526  1.35.2.4   thorpej 			"seg %d start %lx size %lx\n", seg,
    527  1.35.2.5  jdolecek 			(long)map->dm_segs[seg].ds_addr,
    528  1.35.2.5  jdolecek 			map->dm_segs[seg].ds_len));
    529  1.35.2.5  jdolecek 		map->dm_segs[seg].ds_len =
    530  1.35.2.5  jdolecek 		    boundary - (sgstart & (boundary - 1));
    531  1.35.2.4   thorpej 		if (++seg > map->_dm_segcnt) {
    532  1.35.2.4   thorpej 			/* Too many segments.  Fail the operation. */
    533  1.35.2.4   thorpej 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    534  1.35.2.4   thorpej 				"too many segments %d\n", seg));
    535  1.35.2.4   thorpej 			s = splhigh();
    536  1.35.2.4   thorpej 			/* How can this fail?  And if it does what can we do? */
    537  1.35.2.4   thorpej 			err = extent_free(is->is_dvmamap,
    538  1.35.2.4   thorpej 				dvmaddr, sgsize, EX_NOWAIT);
    539  1.35.2.4   thorpej 			map->_dm_dvmastart = 0;
    540  1.35.2.4   thorpej 			map->_dm_dvmasize = 0;
    541  1.35.2.4   thorpej 			splx(s);
    542  1.35.2.4   thorpej 			return (E2BIG);
    543  1.35.2.4   thorpej 		}
    544  1.35.2.4   thorpej 		sgstart = roundup(sgstart, boundary);
    545  1.35.2.4   thorpej 		map->dm_segs[seg].ds_addr = sgstart;
    546  1.35.2.4   thorpej 	}
    547  1.35.2.4   thorpej 	map->dm_segs[seg].ds_len = sgend - sgstart + 1;
    548  1.35.2.4   thorpej 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    549  1.35.2.4   thorpej 		"seg %d start %lx size %lx\n", seg,
    550  1.35.2.5  jdolecek 		(long)map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
    551  1.35.2.4   thorpej 	map->dm_nsegs = seg+1;
    552       1.7       mrg 	map->dm_mapsize = buflen;
    553       1.7       mrg 
    554       1.7       mrg 	if (p != NULL)
    555       1.7       mrg 		pmap = p->p_vmspace->vm_map.pmap;
    556       1.7       mrg 	else
    557       1.7       mrg 		pmap = pmap_kernel();
    558       1.7       mrg 
    559       1.7       mrg 	for (; buflen > 0; ) {
    560       1.7       mrg 		/*
    561       1.7       mrg 		 * Get the physical address for this page.
    562       1.7       mrg 		 */
    563       1.7       mrg 		if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
    564       1.7       mrg 			bus_dmamap_unload(t, map);
    565       1.7       mrg 			return (-1);
    566       1.7       mrg 		}
    567       1.7       mrg 
    568       1.7       mrg 		/*
    569       1.7       mrg 		 * Compute the segment size, and adjust counts.
    570       1.7       mrg 		 */
    571       1.7       mrg 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    572       1.7       mrg 		if (buflen < sgsize)
    573       1.7       mrg 			sgsize = buflen;
    574       1.7       mrg 
    575      1.22       mrg 		DPRINTF(IDB_BUSDMA,
    576  1.35.2.1     lukem 		    ("iommu_dvmamap_load: map %p loading va %p "
    577  1.35.2.1     lukem 			    "dva %lx at pa %lx\n",
    578  1.35.2.1     lukem 			    map, (void *)vaddr, (long)dvmaddr,
    579  1.35.2.1     lukem 			    (long)(curaddr&~(NBPG-1))));
    580       1.7       mrg 		iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
    581  1.35.2.5  jdolecek 		    flags|0x4000);
    582       1.7       mrg 
    583       1.7       mrg 		dvmaddr += PAGE_SIZE;
    584       1.7       mrg 		vaddr += sgsize;
    585       1.7       mrg 		buflen -= sgsize;
    586       1.7       mrg 	}
    587  1.35.2.5  jdolecek #ifdef DIAGNOSTIC
    588  1.35.2.5  jdolecek 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    589  1.35.2.5  jdolecek 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    590  1.35.2.5  jdolecek 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    591  1.35.2.5  jdolecek 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    592  1.35.2.5  jdolecek 				seg, (long)map->dm_segs[seg].ds_addr,
    593  1.35.2.5  jdolecek 				is->is_dvmabase, is->is_dvmaend);
    594  1.35.2.5  jdolecek 			Debugger();
    595  1.35.2.5  jdolecek 		}
    596  1.35.2.5  jdolecek 	}
    597  1.35.2.5  jdolecek #endif
    598       1.7       mrg 	return (0);
    599       1.7       mrg }
    600       1.7       mrg 
    601       1.7       mrg 
    602       1.7       mrg void
    603       1.7       mrg iommu_dvmamap_unload(t, is, map)
    604       1.7       mrg 	bus_dma_tag_t t;
    605       1.7       mrg 	struct iommu_state *is;
    606       1.7       mrg 	bus_dmamap_t map;
    607       1.7       mrg {
    608  1.35.2.4   thorpej 	int error, s;
    609       1.7       mrg 	bus_size_t sgsize;
    610       1.7       mrg 
    611  1.35.2.4   thorpej 	/* Flush the iommu */
    612  1.35.2.4   thorpej #ifdef DEBUG
    613  1.35.2.4   thorpej 	if (!map->_dm_dvmastart) {
    614  1.35.2.4   thorpej 		printf("iommu_dvmamap_unload: No dvmastart is zero\n");
    615  1.35.2.4   thorpej #ifdef DDB
    616  1.35.2.4   thorpej 		Debugger();
    617  1.35.2.4   thorpej #endif
    618      1.26    martin 	}
    619  1.35.2.4   thorpej #endif
    620  1.35.2.4   thorpej 	iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
    621  1.35.2.4   thorpej 
    622      1.23       eeh 	/* Flush the caches */
    623      1.23       eeh 	bus_dmamap_unload(t->_parent, map);
    624      1.23       eeh 
    625       1.7       mrg 	/* Mark the mappings as invalid. */
    626       1.7       mrg 	map->dm_mapsize = 0;
    627       1.7       mrg 	map->dm_nsegs = 0;
    628       1.7       mrg 
    629       1.7       mrg 	s = splhigh();
    630  1.35.2.4   thorpej 	error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
    631  1.35.2.4   thorpej 		map->_dm_dvmasize, EX_NOWAIT);
    632  1.35.2.4   thorpej 	map->_dm_dvmastart = 0;
    633  1.35.2.4   thorpej 	map->_dm_dvmasize = 0;
    634       1.7       mrg 	splx(s);
    635       1.7       mrg 	if (error != 0)
    636       1.7       mrg 		printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
    637  1.35.2.4   thorpej 
    638  1.35.2.4   thorpej 	/* Clear the map */
    639       1.9       eeh }
    640       1.9       eeh 
    641       1.9       eeh 
    642       1.9       eeh int
    643      1.22       mrg iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
    644       1.9       eeh 	bus_dma_tag_t t;
    645       1.9       eeh 	struct iommu_state *is;
    646       1.9       eeh 	bus_dmamap_t map;
    647       1.9       eeh 	bus_dma_segment_t *segs;
    648       1.9       eeh 	int nsegs;
    649      1.22       mrg 	int flags;
    650       1.9       eeh 	bus_size_t size;
    651       1.9       eeh {
    652      1.35       chs 	struct vm_page *m;
    653  1.35.2.4   thorpej 	int i, j, s;
    654      1.26    martin 	int left;
    655       1.9       eeh 	int err;
    656       1.9       eeh 	bus_size_t sgsize;
    657       1.9       eeh 	paddr_t pa;
    658      1.21       eeh 	bus_size_t boundary, align;
    659  1.35.2.4   thorpej 	u_long dvmaddr, sgstart, sgend;
    660       1.9       eeh 	struct pglist *mlist;
    661       1.9       eeh 	int pagesz = PAGE_SIZE;
    662  1.35.2.5  jdolecek 	int npg = 0; /* DEBUG */
    663       1.9       eeh 
    664       1.9       eeh 	if (map->dm_nsegs) {
    665       1.9       eeh 		/* Already in use?? */
    666       1.9       eeh #ifdef DIAGNOSTIC
    667       1.9       eeh 		printf("iommu_dvmamap_load_raw: map still in use\n");
    668       1.9       eeh #endif
    669       1.9       eeh 		bus_dmamap_unload(t, map);
    670       1.9       eeh 	}
    671  1.35.2.4   thorpej 
    672  1.35.2.4   thorpej 	/*
    673  1.35.2.4   thorpej 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    674  1.35.2.4   thorpej 	 * over boundary in the map.
    675  1.35.2.4   thorpej 	 */
    676  1.35.2.4   thorpej 	if ((boundary = segs[0]._ds_boundary) == 0)
    677  1.35.2.4   thorpej 		boundary = map->_dm_boundary;
    678  1.35.2.4   thorpej 
    679  1.35.2.5  jdolecek 	align = max(segs[0]._ds_align, pagesz);
    680  1.35.2.4   thorpej 
    681       1.9       eeh 	/*
    682       1.9       eeh 	 * Make sure that on error condition we return "no valid mappings".
    683       1.9       eeh 	 */
    684       1.9       eeh 	map->dm_nsegs = 0;
    685      1.26    martin 	/* Count up the total number of pages we need */
    686      1.26    martin 	pa = segs[0].ds_addr;
    687      1.26    martin 	sgsize = 0;
    688  1.35.2.4   thorpej 	left = size;
    689  1.35.2.4   thorpej 	for (i=0; left && i<nsegs; i++) {
    690      1.26    martin 		if (round_page(pa) != round_page(segs[i].ds_addr))
    691      1.26    martin 			sgsize = round_page(sgsize);
    692  1.35.2.4   thorpej 		sgsize += min(left, segs[i].ds_len);
    693  1.35.2.4   thorpej 		left -= segs[i].ds_len;
    694      1.26    martin 		pa = segs[i].ds_addr + segs[i].ds_len;
    695      1.26    martin 	}
    696      1.26    martin 	sgsize = round_page(sgsize);
    697       1.9       eeh 
    698       1.9       eeh 	s = splhigh();
    699  1.35.2.4   thorpej 	/*
    700  1.35.2.4   thorpej 	 * If our segment size is larger than the boundary we need to
    701  1.35.2.5  jdolecek 	 * split the transfer up into little pieces ourselves.
    702  1.35.2.4   thorpej 	 */
    703  1.35.2.4   thorpej 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    704  1.35.2.4   thorpej 		(sgsize > boundary) ? 0 : boundary,
    705  1.35.2.4   thorpej 		((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
    706  1.35.2.4   thorpej 		EX_BOUNDZERO, (u_long *)&dvmaddr);
    707       1.9       eeh 	splx(s);
    708       1.9       eeh 
    709       1.9       eeh 	if (err != 0)
    710       1.9       eeh 		return (err);
    711       1.9       eeh 
    712       1.9       eeh #ifdef DEBUG
    713       1.9       eeh 	if (dvmaddr == (bus_addr_t)-1)
    714       1.9       eeh 	{
    715       1.9       eeh 		printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
    716      1.25       mrg 		    (int)sgsize, flags);
    717       1.9       eeh 		Debugger();
    718       1.9       eeh 	}
    719       1.9       eeh #endif
    720       1.9       eeh 	if (dvmaddr == (bus_addr_t)-1)
    721       1.9       eeh 		return (ENOMEM);
    722       1.9       eeh 
    723  1.35.2.4   thorpej 	/* Set the active DVMA map */
    724  1.35.2.4   thorpej 	map->_dm_dvmastart = dvmaddr;
    725  1.35.2.4   thorpej 	map->_dm_dvmasize = sgsize;
    726  1.35.2.4   thorpej 
    727      1.26    martin 	if ((mlist = segs[0]._ds_mlist) == NULL) {
    728      1.26    martin 		u_long prev_va = NULL;
    729  1.35.2.5  jdolecek 		paddr_t prev_pa = 0;
    730  1.35.2.5  jdolecek 		int end = 0, offset;
    731  1.35.2.5  jdolecek 
    732      1.26    martin 		/*
    733  1.35.2.5  jdolecek 		 * This segs is made up of individual physical
    734  1.35.2.5  jdolecek 		 *  segments, probably by _bus_dmamap_load_uio() or
    735      1.26    martin 		 * _bus_dmamap_load_mbuf().  Ignore the mlist and
    736  1.35.2.5  jdolecek 		 * load each one individually.
    737      1.26    martin 		 */
    738  1.35.2.4   thorpej 		map->dm_mapsize = size;
    739      1.26    martin 
    740  1.35.2.5  jdolecek 		j = 0;
    741  1.35.2.5  jdolecek 		for (i = 0; i < nsegs ; i++) {
    742  1.35.2.4   thorpej 
    743  1.35.2.5  jdolecek 			pa = segs[i].ds_addr;
    744  1.35.2.5  jdolecek 			offset = (pa & PGOFSET);
    745  1.35.2.5  jdolecek 			pa = trunc_page(pa);
    746  1.35.2.5  jdolecek 			dvmaddr = trunc_page(dvmaddr);
    747  1.35.2.5  jdolecek 			left = min(size, segs[i].ds_len);
    748  1.35.2.5  jdolecek 
    749  1.35.2.5  jdolecek 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
    750  1.35.2.5  jdolecek 				"physseg %d start %lx size %lx\n", i,
    751  1.35.2.5  jdolecek 				(long)segs[i].ds_addr, segs[i].ds_len));
    752  1.35.2.5  jdolecek 
    753  1.35.2.5  jdolecek 			if ((pa == prev_pa) &&
    754  1.35.2.5  jdolecek 				((offset != 0) || (end != offset))) {
    755  1.35.2.5  jdolecek 				/* We can re-use this mapping */
    756  1.35.2.5  jdolecek #ifdef DEBUG
    757  1.35.2.5  jdolecek if (iommudebug & 0x10) printf("reusing dva %lx prev %lx pa %lx prev %lx\n",
    758  1.35.2.5  jdolecek 	dvmaddr, prev_va, pa, prev_pa);
    759  1.35.2.5  jdolecek #endif
    760  1.35.2.5  jdolecek 				dvmaddr = prev_va;
    761  1.35.2.5  jdolecek 			}
    762  1.35.2.4   thorpej 
    763  1.35.2.5  jdolecek 			sgstart = dvmaddr + offset;
    764  1.35.2.5  jdolecek 			sgend = sgstart + left - 1;
    765  1.35.2.4   thorpej 
    766  1.35.2.5  jdolecek 			/* Are the segments virtually adjacent? */
    767  1.35.2.5  jdolecek 			if ((j > 0) && (end == offset) &&
    768  1.35.2.5  jdolecek 				((offset == 0) || (pa == prev_pa))) {
    769  1.35.2.5  jdolecek 				/* Just append to the previous segment. */
    770  1.35.2.5  jdolecek #ifdef DEBUG
    771  1.35.2.5  jdolecek if (iommudebug & 0x10) {
    772  1.35.2.5  jdolecek printf("appending: offset %x pa %lx prev %lx dva %lx prev %lx\n",
    773  1.35.2.5  jdolecek 	offset, pa, prev_pa, dvmaddr, prev_va);
    774  1.35.2.5  jdolecek }
    775  1.35.2.5  jdolecek #endif
    776  1.35.2.4   thorpej 
    777  1.35.2.5  jdolecek 				map->dm_segs[--j].ds_len += left;
    778  1.35.2.5  jdolecek 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    779  1.35.2.5  jdolecek 					"appending seg %d start %lx size %lx\n", j,
    780  1.35.2.5  jdolecek 					(long)map->dm_segs[j].ds_addr,
    781  1.35.2.5  jdolecek 					map->dm_segs[j].ds_len));
    782  1.35.2.5  jdolecek 			} else {
    783  1.35.2.5  jdolecek 				map->dm_segs[j].ds_addr = sgstart;
    784  1.35.2.5  jdolecek 				map->dm_segs[j].ds_len = left;
    785  1.35.2.5  jdolecek 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    786  1.35.2.5  jdolecek 					"seg %d start %lx size %lx\n", j,
    787  1.35.2.5  jdolecek 					(long)map->dm_segs[j].ds_addr,
    788  1.35.2.5  jdolecek 					map->dm_segs[j].ds_len));
    789  1.35.2.4   thorpej 			}
    790  1.35.2.5  jdolecek 			end = (offset + left) & PGOFSET;
    791  1.35.2.4   thorpej 
    792  1.35.2.4   thorpej 			/* Check for boundary issues */
    793  1.35.2.4   thorpej 			while ((sgstart & ~(boundary - 1)) !=
    794  1.35.2.4   thorpej 				(sgend & ~(boundary - 1))) {
    795  1.35.2.4   thorpej 				/* Need a new segment. */
    796  1.35.2.4   thorpej 				map->dm_segs[j].ds_len =
    797  1.35.2.4   thorpej 					sgstart & (boundary - 1);
    798  1.35.2.4   thorpej 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    799  1.35.2.4   thorpej 					"seg %d start %lx size %lx\n", j,
    800  1.35.2.5  jdolecek 					(long)map->dm_segs[j].ds_addr,
    801  1.35.2.4   thorpej 					map->dm_segs[j].ds_len));
    802  1.35.2.4   thorpej 				if (++j > map->_dm_segcnt) {
    803  1.35.2.4   thorpej 					iommu_dvmamap_unload(t, is, map);
    804  1.35.2.4   thorpej 					return (E2BIG);
    805  1.35.2.4   thorpej 				}
    806  1.35.2.4   thorpej 				sgstart = roundup(sgstart, boundary);
    807  1.35.2.4   thorpej 				map->dm_segs[j].ds_addr = sgstart;
    808  1.35.2.4   thorpej 				map->dm_segs[j].ds_len = sgend - sgstart + 1;
    809  1.35.2.4   thorpej 			}
    810  1.35.2.4   thorpej 
    811      1.26    martin 			if (sgsize == 0)
    812      1.26    martin 				panic("iommu_dmamap_load_raw: size botch");
    813  1.35.2.4   thorpej 
    814  1.35.2.5  jdolecek 			/* Now map a series of pages. */
    815  1.35.2.5  jdolecek 			while (dvmaddr < sgend) {
    816  1.35.2.5  jdolecek 				DPRINTF(IDB_BUSDMA,
    817  1.35.2.5  jdolecek 					("iommu_dvmamap_load_raw: map %p "
    818  1.35.2.5  jdolecek 						"loading va %lx at pa %lx\n",
    819  1.35.2.5  jdolecek 						map, (long)dvmaddr,
    820  1.35.2.5  jdolecek 						(long)(pa)));
    821  1.35.2.5  jdolecek 				/* Enter it if we haven't before. */
    822  1.35.2.5  jdolecek 				if (prev_va != dvmaddr)
    823  1.35.2.5  jdolecek #ifdef DEBUG
    824  1.35.2.5  jdolecek { if (iommudebug & 0x10) printf("seg %d:%d entering dvma %lx, prev %lx pa %lx\n", i,j, dvmaddr, prev_va, pa);
    825  1.35.2.5  jdolecek #endif
    826  1.35.2.5  jdolecek 					iommu_enter(is, prev_va = dvmaddr,
    827  1.35.2.5  jdolecek 						prev_pa = pa,
    828  1.35.2.5  jdolecek 						flags|(++npg<<12));
    829  1.35.2.5  jdolecek #ifdef DEBUG
    830  1.35.2.5  jdolecek } else if (iommudebug & 0x10) printf("seg %d:%d skipping dvma %lx, prev %lx\n", i,j, dvmaddr, prev_va);
    831  1.35.2.5  jdolecek #endif
    832  1.35.2.5  jdolecek 
    833  1.35.2.5  jdolecek 				dvmaddr += pagesz;
    834  1.35.2.5  jdolecek 				pa += pagesz;
    835  1.35.2.5  jdolecek 			}
    836  1.35.2.5  jdolecek 
    837  1.35.2.5  jdolecek 			size -= left;
    838  1.35.2.5  jdolecek 			++j;
    839      1.27    martin 		}
    840  1.35.2.5  jdolecek 
    841  1.35.2.5  jdolecek 		map->dm_nsegs = j;
    842  1.35.2.5  jdolecek #ifdef DIAGNOSTIC
    843  1.35.2.5  jdolecek 		{ int seg;
    844  1.35.2.5  jdolecek 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    845  1.35.2.5  jdolecek 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    846  1.35.2.5  jdolecek 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    847  1.35.2.5  jdolecek 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    848  1.35.2.5  jdolecek 				seg, (long)map->dm_segs[seg].ds_addr,
    849  1.35.2.5  jdolecek 				is->is_dvmabase, is->is_dvmaend);
    850  1.35.2.5  jdolecek 			Debugger();
    851  1.35.2.5  jdolecek 		}
    852  1.35.2.5  jdolecek 	}
    853  1.35.2.5  jdolecek 		}
    854  1.35.2.5  jdolecek #endif
    855      1.26    martin 		return (0);
    856      1.26    martin 	}
    857       1.9       eeh 	/*
    858  1.35.2.4   thorpej 	 * This was allocated with bus_dmamem_alloc.
    859  1.35.2.4   thorpej 	 * The pages are on an `mlist'.
    860       1.9       eeh 	 */
    861       1.9       eeh 	map->dm_mapsize = size;
    862      1.26    martin 	i = 0;
    863  1.35.2.4   thorpej 	sgstart = dvmaddr;
    864  1.35.2.4   thorpej 	sgend = sgstart + size - 1;
    865  1.35.2.4   thorpej 	map->dm_segs[i].ds_addr = sgstart;
    866  1.35.2.4   thorpej 	while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
    867  1.35.2.4   thorpej 		/* Oops.  We crossed a boundary.  Split the xfer. */
    868  1.35.2.4   thorpej 		map->dm_segs[i].ds_len = sgstart & (boundary - 1);
    869  1.35.2.4   thorpej 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    870  1.35.2.4   thorpej 			"seg %d start %lx size %lx\n", i,
    871  1.35.2.5  jdolecek 			(long)map->dm_segs[i].ds_addr,
    872  1.35.2.4   thorpej 			map->dm_segs[i].ds_len));
    873  1.35.2.4   thorpej 		if (++i > map->_dm_segcnt) {
    874  1.35.2.4   thorpej 			/* Too many segments.  Fail the operation. */
    875  1.35.2.4   thorpej 			s = splhigh();
    876  1.35.2.4   thorpej 			/* How can this fail?  And if it does what can we do? */
    877  1.35.2.4   thorpej 			err = extent_free(is->is_dvmamap,
    878  1.35.2.4   thorpej 				dvmaddr, sgsize, EX_NOWAIT);
    879  1.35.2.4   thorpej 			map->_dm_dvmastart = 0;
    880  1.35.2.4   thorpej 			map->_dm_dvmasize = 0;
    881  1.35.2.4   thorpej 			splx(s);
    882  1.35.2.4   thorpej 			return (E2BIG);
    883  1.35.2.4   thorpej 		}
    884  1.35.2.4   thorpej 		sgstart = roundup(sgstart, boundary);
    885  1.35.2.4   thorpej 		map->dm_segs[i].ds_addr = sgstart;
    886  1.35.2.4   thorpej 	}
    887  1.35.2.4   thorpej 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    888  1.35.2.4   thorpej 			"seg %d start %lx size %lx\n", i,
    889  1.35.2.5  jdolecek 			(long)map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len));
    890  1.35.2.4   thorpej 	map->dm_segs[i].ds_len = sgend - sgstart + 1;
    891       1.9       eeh 
    892       1.9       eeh 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    893       1.9       eeh 		if (sgsize == 0)
    894       1.9       eeh 			panic("iommu_dmamap_load_raw: size botch");
    895       1.9       eeh 		pa = VM_PAGE_TO_PHYS(m);
    896       1.9       eeh 
    897      1.22       mrg 		DPRINTF(IDB_BUSDMA,
    898       1.9       eeh 		    ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
    899       1.9       eeh 		    map, (long)dvmaddr, (long)(pa)));
    900  1.35.2.5  jdolecek 		iommu_enter(is, dvmaddr, pa, flags|0x8000);
    901       1.9       eeh 
    902       1.9       eeh 		dvmaddr += pagesz;
    903       1.9       eeh 		sgsize -= pagesz;
    904       1.9       eeh 	}
    905  1.35.2.4   thorpej 	map->dm_mapsize = size;
    906  1.35.2.4   thorpej 	map->dm_nsegs = i+1;
    907  1.35.2.5  jdolecek #ifdef DIAGNOSTIC
    908  1.35.2.5  jdolecek 	{ int seg;
    909  1.35.2.5  jdolecek 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    910  1.35.2.5  jdolecek 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    911  1.35.2.5  jdolecek 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    912  1.35.2.5  jdolecek 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    913  1.35.2.5  jdolecek 				seg, (long)map->dm_segs[seg].ds_addr,
    914  1.35.2.5  jdolecek 				is->is_dvmabase, is->is_dvmaend);
    915  1.35.2.5  jdolecek 			Debugger();
    916  1.35.2.5  jdolecek 		}
    917  1.35.2.5  jdolecek 	}
    918  1.35.2.5  jdolecek 	}
    919  1.35.2.5  jdolecek #endif
    920       1.9       eeh 	return (0);
    921       1.7       mrg }
    922       1.7       mrg 
    923       1.7       mrg void
    924       1.7       mrg iommu_dvmamap_sync(t, is, map, offset, len, ops)
    925       1.7       mrg 	bus_dma_tag_t t;
    926       1.7       mrg 	struct iommu_state *is;
    927       1.7       mrg 	bus_dmamap_t map;
    928       1.7       mrg 	bus_addr_t offset;
    929       1.7       mrg 	bus_size_t len;
    930       1.7       mrg 	int ops;
    931       1.7       mrg {
    932       1.7       mrg 	vaddr_t va = map->dm_segs[0].ds_addr + offset;
    933       1.7       mrg 
    934       1.7       mrg 	/*
    935       1.7       mrg 	 * We only support one DMA segment; supporting more makes this code
    936       1.7       mrg          * too unweildy.
    937       1.7       mrg 	 */
    938       1.7       mrg 
    939       1.7       mrg 	if (ops & BUS_DMASYNC_PREREAD) {
    940  1.35.2.1     lukem 		DPRINTF(IDB_SYNC,
    941       1.7       mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    942      1.25       mrg 		     "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
    943       1.7       mrg 
    944       1.7       mrg 		/* Nothing to do */;
    945       1.7       mrg 	}
    946       1.7       mrg 	if (ops & BUS_DMASYNC_POSTREAD) {
    947  1.35.2.1     lukem 		DPRINTF(IDB_SYNC,
    948       1.7       mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    949      1.25       mrg 		     "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
    950       1.7       mrg 		/* if we have a streaming buffer, flush it here first */
    951  1.35.2.4   thorpej 		if (is->is_sb[0] || is->is_sb[1])
    952       1.7       mrg 			while (len > 0) {
    953      1.22       mrg 				DPRINTF(IDB_BUSDMA,
    954       1.7       mrg 				    ("iommu_dvmamap_sync: flushing va %p, %lu "
    955      1.25       mrg 				     "bytes left\n", (void *)(u_long)va, (u_long)len));
    956      1.31       eeh 				iommu_strbuf_flush(is, va);
    957       1.7       mrg 				if (len <= NBPG) {
    958      1.31       eeh 					iommu_strbuf_flush_done(is);
    959       1.7       mrg 					len = 0;
    960       1.7       mrg 				} else
    961       1.7       mrg 					len -= NBPG;
    962       1.7       mrg 				va += NBPG;
    963       1.7       mrg 			}
    964       1.7       mrg 	}
    965       1.7       mrg 	if (ops & BUS_DMASYNC_PREWRITE) {
    966  1.35.2.1     lukem 		DPRINTF(IDB_SYNC,
    967       1.7       mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    968      1.25       mrg 		     "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
    969      1.31       eeh 		/* if we have a streaming buffer, flush it here first */
    970  1.35.2.4   thorpej 		if (is->is_sb[0] || is->is_sb[1])
    971      1.31       eeh 			while (len > 0) {
    972      1.31       eeh 				DPRINTF(IDB_BUSDMA,
    973      1.31       eeh 				    ("iommu_dvmamap_sync: flushing va %p, %lu "
    974      1.31       eeh 				     "bytes left\n", (void *)(u_long)va, (u_long)len));
    975      1.31       eeh 				iommu_strbuf_flush(is, va);
    976      1.31       eeh 				if (len <= NBPG) {
    977      1.31       eeh 					iommu_strbuf_flush_done(is);
    978      1.31       eeh 					len = 0;
    979      1.31       eeh 				} else
    980      1.31       eeh 					len -= NBPG;
    981      1.31       eeh 				va += NBPG;
    982      1.31       eeh 			}
    983       1.7       mrg 	}
    984       1.7       mrg 	if (ops & BUS_DMASYNC_POSTWRITE) {
    985  1.35.2.1     lukem 		DPRINTF(IDB_SYNC,
    986       1.7       mrg 		    ("iommu_dvmamap_sync: syncing va %p len %lu "
    987      1.25       mrg 		     "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
    988       1.7       mrg 		/* Nothing to do */;
    989       1.7       mrg 	}
    990       1.7       mrg }
    991       1.7       mrg 
    992       1.7       mrg int
    993       1.7       mrg iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
    994       1.7       mrg 	bus_dma_tag_t t;
    995       1.7       mrg 	struct iommu_state *is;
    996       1.7       mrg 	bus_size_t size, alignment, boundary;
    997       1.7       mrg 	bus_dma_segment_t *segs;
    998       1.7       mrg 	int nsegs;
    999       1.7       mrg 	int *rsegs;
   1000       1.7       mrg 	int flags;
   1001       1.7       mrg {
   1002       1.7       mrg 
   1003      1.25       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
   1004      1.25       mrg 	   "segp %p flags %d\n", (unsigned long long)size,
   1005      1.25       mrg 	   (unsigned long long)alignment, (unsigned long long)boundary,
   1006      1.25       mrg 	   segs, flags));
   1007       1.7       mrg 	return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
   1008      1.21       eeh 	    segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
   1009       1.7       mrg }
   1010       1.7       mrg 
   1011       1.7       mrg void
   1012       1.7       mrg iommu_dvmamem_free(t, is, segs, nsegs)
   1013       1.7       mrg 	bus_dma_tag_t t;
   1014       1.7       mrg 	struct iommu_state *is;
   1015       1.7       mrg 	bus_dma_segment_t *segs;
   1016       1.7       mrg 	int nsegs;
   1017       1.7       mrg {
   1018       1.7       mrg 
   1019      1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
   1020       1.7       mrg 	    segs, nsegs));
   1021       1.7       mrg 	bus_dmamem_free(t->_parent, segs, nsegs);
   1022       1.7       mrg }
   1023       1.7       mrg 
   1024       1.7       mrg /*
   1025       1.7       mrg  * Map the DVMA mappings into the kernel pmap.
   1026       1.7       mrg  * Check the flags to see whether we're streaming or coherent.
   1027       1.7       mrg  */
   1028       1.7       mrg int
   1029       1.7       mrg iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
   1030       1.7       mrg 	bus_dma_tag_t t;
   1031       1.7       mrg 	struct iommu_state *is;
   1032       1.7       mrg 	bus_dma_segment_t *segs;
   1033       1.7       mrg 	int nsegs;
   1034       1.7       mrg 	size_t size;
   1035       1.7       mrg 	caddr_t *kvap;
   1036       1.7       mrg 	int flags;
   1037       1.7       mrg {
   1038      1.35       chs 	struct vm_page *m;
   1039       1.7       mrg 	vaddr_t va;
   1040       1.7       mrg 	bus_addr_t addr;
   1041       1.7       mrg 	struct pglist *mlist;
   1042       1.8       mrg 	int cbit;
   1043       1.7       mrg 
   1044      1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
   1045       1.7       mrg 	    segs, nsegs, size));
   1046       1.7       mrg 
   1047       1.7       mrg 	/*
   1048       1.8       mrg 	 * Allocate some space in the kernel map, and then map these pages
   1049       1.8       mrg 	 * into this space.
   1050       1.7       mrg 	 */
   1051       1.8       mrg 	size = round_page(size);
   1052       1.8       mrg 	va = uvm_km_valloc(kernel_map, size);
   1053       1.8       mrg 	if (va == 0)
   1054       1.8       mrg 		return (ENOMEM);
   1055       1.7       mrg 
   1056       1.8       mrg 	*kvap = (caddr_t)va;
   1057       1.7       mrg 
   1058       1.7       mrg 	/*
   1059       1.7       mrg 	 * digest flags:
   1060       1.7       mrg 	 */
   1061       1.7       mrg 	cbit = 0;
   1062       1.7       mrg 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
   1063       1.7       mrg 		cbit |= PMAP_NVC;
   1064       1.7       mrg 	if (flags & BUS_DMA_NOCACHE)	/* sideffects */
   1065       1.7       mrg 		cbit |= PMAP_NC;
   1066       1.7       mrg 
   1067       1.7       mrg 	/*
   1068       1.8       mrg 	 * Now take this and map it into the CPU.
   1069       1.7       mrg 	 */
   1070       1.7       mrg 	mlist = segs[0]._ds_mlist;
   1071       1.7       mrg 	for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
   1072       1.8       mrg #ifdef DIAGNOSTIC
   1073       1.7       mrg 		if (size == 0)
   1074       1.7       mrg 			panic("iommu_dvmamem_map: size botch");
   1075       1.8       mrg #endif
   1076       1.7       mrg 		addr = VM_PAGE_TO_PHYS(m);
   1077      1.22       mrg 		DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
   1078      1.25       mrg 		    "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
   1079       1.7       mrg 		pmap_enter(pmap_kernel(), va, addr | cbit,
   1080      1.24       eeh 		    VM_PROT_READ | VM_PROT_WRITE,
   1081      1.24       eeh 		    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
   1082       1.7       mrg 		va += PAGE_SIZE;
   1083       1.7       mrg 		size -= PAGE_SIZE;
   1084       1.7       mrg 	}
   1085  1.35.2.3   thorpej 	pmap_update(pmap_kernel());
   1086       1.7       mrg 
   1087       1.7       mrg 	return (0);
   1088       1.7       mrg }
   1089       1.7       mrg 
   1090       1.7       mrg /*
   1091       1.7       mrg  * Unmap DVMA mappings from kernel
   1092       1.7       mrg  */
   1093       1.7       mrg void
   1094       1.7       mrg iommu_dvmamem_unmap(t, is, kva, size)
   1095       1.7       mrg 	bus_dma_tag_t t;
   1096       1.7       mrg 	struct iommu_state *is;
   1097       1.7       mrg 	caddr_t kva;
   1098       1.7       mrg 	size_t size;
   1099       1.7       mrg {
   1100       1.7       mrg 
   1101      1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
   1102       1.7       mrg 	    kva, size));
   1103       1.7       mrg 
   1104       1.7       mrg #ifdef DIAGNOSTIC
   1105       1.7       mrg 	if ((u_long)kva & PGOFSET)
   1106       1.7       mrg 		panic("iommu_dvmamem_unmap");
   1107       1.7       mrg #endif
   1108       1.7       mrg 
   1109       1.7       mrg 	size = round_page(size);
   1110       1.7       mrg 	pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
   1111  1.35.2.3   thorpej 	pmap_update(pmap_kernel());
   1112       1.8       mrg #if 0
   1113       1.8       mrg 	/*
   1114       1.8       mrg 	 * XXX ? is this necessary? i think so and i think other
   1115       1.8       mrg 	 * implementations are missing it.
   1116       1.8       mrg 	 */
   1117       1.8       mrg 	uvm_km_free(kernel_map, (vaddr_t)kva, size);
   1118       1.8       mrg #endif
   1119       1.1       mrg }
   1120