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