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