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