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