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