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