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