]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sparc64/iommu.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / sparc64 / iommu.c
1 /*-
2  * Copyright (c) 1999, 2000 Matthew R. Green
3  * Copyright (c) 2001-2003 Thomas Moestl
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright (c) 1998 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Paul Kranenburg.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *        This product includes software developed by the NetBSD
47  *        Foundation, Inc. and its contributors.
48  * 4. Neither the name of The NetBSD Foundation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64 /*-
65  * Copyright (c) 1992, 1993
66  *      The Regents of the University of California.  All rights reserved.
67  *
68  * This software was developed by the Computer Systems Engineering group
69  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
70  * contributed to Berkeley.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 4. Neither the name of the University nor the names of its contributors
81  *    may be used to endorse or promote products derived from this software
82  *    without specific prior written permission.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94  * SUCH DAMAGE.
95  *
96  *      from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
97  *      from: @(#)sbus.c        8.1 (Berkeley) 6/11/93
98  *      from: NetBSD: iommu.c,v 1.42 2001/08/06 22:02:58 eeh Exp
99  */
100
101 #include <sys/cdefs.h>
102 __FBSDID("$FreeBSD$");
103
104 /*
105  * UltraSPARC IOMMU support; used by both the PCI and SBus code.
106  *
107  * TODO:
108  * - Support sub-page boundaries.
109  * - Fix alignment handling for small allocations (the possible page offset
110  *   of malloc()ed memory is not handled at all).  Revise interaction of
111  *   alignment with the load_mbuf and load_uio functions.
112  * - Handle lowaddr and highaddr in some way, and try to work out a way
113  *   for filter callbacks to work.  Currently, only lowaddr is honored
114  *   in that no addresses above it are considered at all.
115  * - Implement BUS_DMA_ALLOCNOW in bus_dma_tag_create as far as possible.
116  * - Check the possible return values and callback error arguments;
117  *   the callback currently gets called in error conditions where it should
118  *   not be.
119  * - When running out of DVMA space, return EINPROGRESS in the non-
120  *   BUS_DMA_NOWAIT case and delay the callback until sufficient space
121  *   becomes available.
122  * - Use the streaming cache unless BUS_DMA_COHERENT is specified.
123  */
124
125 #include "opt_iommu.h"
126
127 #include <sys/param.h>
128 #include <sys/kernel.h>
129 #include <sys/lock.h>
130 #include <sys/malloc.h>
131 #include <sys/mbuf.h>
132 #include <sys/mutex.h>
133 #include <sys/proc.h>
134 #include <sys/systm.h>
135 #include <sys/uio.h>
136
137 #include <vm/vm.h>
138 #include <vm/pmap.h>
139 #include <vm/vm_map.h>
140
141 #include <machine/bus.h>
142 #include <machine/bus_private.h>
143 #include <machine/iommureg.h>
144 #include <machine/pmap.h>
145 #include <machine/resource.h>
146
147 #include <sys/rman.h>
148
149 #include <machine/iommuvar.h>
150
151 /*
152  * Tuning constants
153  */
154 #define IOMMU_MAX_PRE           (32 * 1024)
155 #define IOMMU_MAX_PRE_SEG       3
156
157 /* Threshold for using the streaming buffer */
158 #define IOMMU_STREAM_THRESH     128
159
160 MALLOC_DEFINE(M_IOMMU, "dvmamem", "IOMMU DVMA Buffers");
161
162 static  int iommu_strbuf_flush_sync(struct iommu_state *);
163 #ifdef IOMMU_DIAG
164 static  void iommu_diag(struct iommu_state *, vm_offset_t va);
165 #endif
166
167 /*
168  * Helpers
169  */
170 #define IOMMU_READ8(is, reg, off)                                       \
171         bus_space_read_8((is)->is_bustag, (is)->is_bushandle,           \
172             (is)->reg + (off))
173 #define IOMMU_WRITE8(is, reg, off, v)                                   \
174         bus_space_write_8((is)->is_bustag, (is)->is_bushandle,          \
175             (is)->reg + (off), (v))
176
177 #define IOMMU_HAS_SB(is)                                                \
178         ((is)->is_sb[0] != 0 || (is)->is_sb[1] != 0)
179
180 /*
181  * Always overallocate one page; this is needed to handle alignment of the
182  * buffer, so it makes sense using a lazy allocation scheme.
183  */
184 #define IOMMU_SIZE_ROUNDUP(sz)                                          \
185         (round_io_page(sz) + IO_PAGE_SIZE)
186
187 #define IOMMU_SET_TTE(is, va, tte)                                      \
188         ((is)->is_tsb[IOTSBSLOT(va)] = (tte))
189 #define IOMMU_GET_TTE(is, va)                                           \
190         (is)->is_tsb[IOTSBSLOT(va)]
191
192 /* Resource helpers */
193 #define IOMMU_RES_START(res)                                            \
194         ((bus_addr_t)rman_get_start(res) << IO_PAGE_SHIFT)
195 #define IOMMU_RES_END(res)                                              \
196         ((bus_addr_t)(rman_get_end(res) + 1) << IO_PAGE_SHIFT)
197 #define IOMMU_RES_SIZE(res)                                             \
198         ((bus_size_t)rman_get_size(res) << IO_PAGE_SHIFT)
199
200 /* Helpers for struct bus_dmamap_res */
201 #define BDR_START(r)    IOMMU_RES_START((r)->dr_res)
202 #define BDR_END(r)      IOMMU_RES_END((r)->dr_res)
203 #define BDR_SIZE(r)     IOMMU_RES_SIZE((r)->dr_res)
204
205 /* Locking macros */
206 #define IS_LOCK(is)     mtx_lock(&is->is_mtx)
207 #define IS_LOCK_ASSERT(is)      mtx_assert(&is->is_mtx, MA_OWNED)
208 #define IS_UNLOCK(is)   mtx_unlock(&is->is_mtx)
209
210 /* Flush a page from the TLB.  No locking required, since this is atomic. */
211 static __inline void
212 iommu_tlb_flush(struct iommu_state *is, bus_addr_t va)
213 {
214
215         IOMMU_WRITE8(is, is_iommu, IMR_FLUSH, va);
216 }
217
218 /*
219  * Flush a page from the streaming buffer.  No locking required, since this
220  * is atomic.
221  */
222 static __inline void
223 iommu_strbuf_flushpg(struct iommu_state *is, bus_addr_t va)
224 {
225         int i;
226
227         for (i = 0; i < 2; i++)
228                 if (is->is_sb[i] != 0)
229                         IOMMU_WRITE8(is, is_sb[i], ISR_PGFLUSH, va);
230 }
231
232 /*
233  * Flush an address from the streaming buffer(s); this is an asynchronous
234  * operation.  To make sure that it has completed, iommu_strbuf_sync() needs
235  * to be called.  No locking required.
236  */
237 static __inline void
238 iommu_strbuf_flush(struct iommu_state *is, bus_addr_t va)
239 {
240
241         iommu_strbuf_flushpg(is, va);
242 }
243
244 /* Synchronize all outstanding flush operations. */
245 static __inline void
246 iommu_strbuf_sync(struct iommu_state *is)
247 {
248
249         IS_LOCK_ASSERT(is);
250         iommu_strbuf_flush_sync(is);
251 }
252
253 /* LRU queue handling for lazy resource allocation. */
254 static __inline void
255 iommu_map_insq(struct iommu_state *is, bus_dmamap_t map)
256 {
257
258         IS_LOCK_ASSERT(is);
259         if (!SLIST_EMPTY(&map->dm_reslist)) {
260                 if (map->dm_onq)
261                         TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
262                 TAILQ_INSERT_TAIL(&is->is_maplruq, map, dm_maplruq);
263                 map->dm_onq = 1;
264         }
265 }
266
267 static __inline void
268 iommu_map_remq(struct iommu_state *is, bus_dmamap_t map)
269 {
270
271         IS_LOCK_ASSERT(is);
272         if (map->dm_onq)
273                 TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
274         map->dm_onq = 0;
275 }
276
277 /*
278  * initialise the UltraSPARC IOMMU (PCI or SBus):
279  *      - allocate and setup the iotsb.
280  *      - enable the IOMMU
281  *      - initialise the streaming buffers (if they exist)
282  *      - create a private DVMA map.
283  */
284 void
285 iommu_init(const char *name, struct iommu_state *is, int tsbsize,
286     uint32_t iovabase, int resvpg)
287 {
288         vm_size_t size;
289         vm_offset_t offs;
290         uint64_t end;
291         int i;
292
293         /*
294          * Setup the iommu.
295          *
296          * The sun4u iommu is part of the PCI or SBus controller so we
297          * will deal with it here..
298          *
299          * The IOMMU address space always ends at 0xffffe000, but the starting
300          * address depends on the size of the map.  The map size is 1024 * 2 ^
301          * is->is_tsbsize entries, where each entry is 8 bytes.  The start of
302          * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
303          */
304         is->is_cr = (tsbsize << IOMMUCR_TSBSZ_SHIFT) | IOMMUCR_EN;
305         is->is_tsbsize = tsbsize;
306         is->is_dvmabase = iovabase;
307         if (iovabase == -1)
308                 is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
309
310         size = IOTSB_BASESZ << is->is_tsbsize;
311         printf("%s: DVMA map: %#lx to %#lx%s\n", name,
312             is->is_dvmabase, is->is_dvmabase +
313             (size << (IO_PAGE_SHIFT - IOTTE_SHIFT)) - 1,
314             IOMMU_HAS_SB(is) ? ", streaming buffer" : "");
315
316         /*
317          * Set up resource mamangement.
318          */
319         mtx_init(&is->is_mtx, "iommu", NULL, MTX_DEF);
320         end = is->is_dvmabase + (size << (IO_PAGE_SHIFT - IOTTE_SHIFT));
321         is->is_dvma_rman.rm_type = RMAN_ARRAY;
322         is->is_dvma_rman.rm_descr = "DVMA Memory";
323         if (rman_init(&is->is_dvma_rman) != 0 ||
324             rman_manage_region(&is->is_dvma_rman,
325             (is->is_dvmabase >> IO_PAGE_SHIFT) + resvpg,
326             (end >> IO_PAGE_SHIFT) - 1) != 0)
327                 panic("%s: could not initialize DVMA rman", __func__);
328         TAILQ_INIT(&is->is_maplruq);
329
330         /*
331          * Allocate memory for I/O page tables.  They need to be
332          * physically contiguous.
333          */
334         is->is_tsb = contigmalloc(size, M_DEVBUF, M_NOWAIT, 0, ~0UL,
335             PAGE_SIZE, 0);
336         if (is->is_tsb == 0)
337                 panic("%s: contigmalloc failed", __func__);
338         is->is_ptsb = pmap_kextract((vm_offset_t)is->is_tsb);
339         bzero(is->is_tsb, size);
340
341         /*
342          * Initialize streaming buffer, if it is there.
343          */
344         if (IOMMU_HAS_SB(is)) {
345                 /*
346                  * Find two 64-byte blocks in is_flush that are aligned on
347                  * a 64-byte boundary for flushing.
348                  */
349                 offs = roundup2((vm_offset_t)is->is_flush,
350                     STRBUF_FLUSHSYNC_NBYTES);
351                 for (i = 0; i < 2; i++, offs += STRBUF_FLUSHSYNC_NBYTES) {
352                         is->is_flushva[i] = (int64_t *)offs;
353                         is->is_flushpa[i] = pmap_kextract(offs);
354                 }
355         }
356
357         /*
358          * Now actually start up the IOMMU.
359          */
360         iommu_reset(is);
361 }
362
363 /*
364  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
365  * detected that already and disabled them.  If not, we will notice that
366  * they aren't there when the STRBUF_EN bit does not remain.
367  */
368 void
369 iommu_reset(struct iommu_state *is)
370 {
371         int i;
372
373         IOMMU_WRITE8(is, is_iommu, IMR_TSB, is->is_ptsb);
374         /* Enable IOMMU in diagnostic mode */
375         IOMMU_WRITE8(is, is_iommu, IMR_CTL, is->is_cr | IOMMUCR_DE);
376
377         for (i = 0; i < 2; i++) {
378                 if (is->is_sb[i] != 0) {
379                         IOMMU_WRITE8(is, is_sb[i], ISR_CTL, STRBUF_EN |
380                             ((is->is_flags & IOMMU_RERUN_DISABLE) != 0 ?
381                             STRBUF_RR_DIS : 0));
382
383                         /* No streaming buffers?  Disable them. */
384                         if ((IOMMU_READ8(is, is_sb[i], ISR_CTL) &
385                             STRBUF_EN) == 0)
386                                 is->is_sb[i] = 0;
387                 }
388         }
389 }
390
391 /*
392  * Enter a mapping into the TSB.  No locking required, since each TSB slot is
393  * uniquely assigned to a single map.
394  */
395 static void
396 iommu_enter(struct iommu_state *is, vm_offset_t va, vm_paddr_t pa,
397     int stream, int flags)
398 {
399         int64_t tte;
400
401         KASSERT(va >= is->is_dvmabase,
402             ("%s: va %#lx not in DVMA space", __func__, va));
403         KASSERT(pa <= is->is_pmaxaddr,
404             ("%s: XXX: physical address too large (%#lx)", __func__, pa));
405
406         tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
407             !(flags & BUS_DMA_NOCACHE), stream);
408
409         IOMMU_SET_TTE(is, va, tte);
410         iommu_tlb_flush(is, va);
411 #ifdef IOMMU_DIAG
412         IS_LOCK(is);
413         iommu_diag(is, va);
414         IS_UNLOCK(is);
415 #endif
416 }
417
418 /*
419  * Remove mappings created by iommu_enter().  Flush the streaming buffer,
420  * but do not synchronize it.  Returns whether a streaming buffer flush
421  * was performed.
422  */
423 static int
424 iommu_remove(struct iommu_state *is, vm_offset_t va, vm_size_t len)
425 {
426         int streamed = 0;
427
428 #ifdef IOMMU_DIAG
429         iommu_diag(is, va);
430 #endif
431
432         KASSERT(va >= is->is_dvmabase,
433             ("%s: va 0x%lx not in DVMA space", __func__, (u_long)va));
434         KASSERT(va + len >= va,
435             ("%s: va 0x%lx + len 0x%lx wraps", __func__, (long)va, (long)len));
436
437         va = trunc_io_page(va);
438         while (len > 0) {
439                 if ((IOMMU_GET_TTE(is, va) & IOTTE_STREAM) != 0) {
440                         streamed = 1;
441                         iommu_strbuf_flush(is, va);
442                 }
443                 len -= ulmin(len, IO_PAGE_SIZE);
444                 IOMMU_SET_TTE(is, va, 0);
445                 iommu_tlb_flush(is, va);
446                 va += IO_PAGE_SIZE;
447         }
448         return (streamed);
449 }
450
451 /* Decode an IOMMU fault for host bridge error handlers. */
452 void
453 iommu_decode_fault(struct iommu_state *is, vm_offset_t phys)
454 {
455         bus_addr_t va;
456         long idx;
457
458         idx = phys - is->is_ptsb;
459         if (phys < is->is_ptsb ||
460             idx > (PAGE_SIZE << is->is_tsbsize))
461                 return;
462         va = is->is_dvmabase +
463             (((bus_addr_t)idx >> IOTTE_SHIFT) << IO_PAGE_SHIFT);
464         printf("IOMMU fault virtual address %#lx\n", (u_long)va);
465 }
466
467 /*
468  * A barrier operation which makes sure that all previous streaming buffer
469  * flushes complete before it returns.
470  */
471 static int
472 iommu_strbuf_flush_sync(struct iommu_state *is)
473 {
474         struct timeval cur, end;
475         int i;
476
477         IS_LOCK_ASSERT(is);
478         if (!IOMMU_HAS_SB(is))
479                 return (0);
480
481         /*
482          * Streaming buffer flushes:
483          *
484          *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
485          *     we're not on a cache line boundary (64-bits):
486          *   2 Store 0 in flag
487          *   3 Store pointer to flag in flushsync
488          *   4 wait till flushsync becomes 0x1
489          *
490          * If it takes more than .5 sec, something went wrong.
491          */
492         *is->is_flushva[0] = 1;
493         *is->is_flushva[1] = 1;
494         membar(StoreStore);
495         for (i = 0; i < 2; i++) {
496                 if (is->is_sb[i] != 0) {
497                         *is->is_flushva[i] = 0;
498                         IOMMU_WRITE8(is, is_sb[i], ISR_FLUSHSYNC,
499                             is->is_flushpa[i]);
500                 }
501         }
502
503         microuptime(&cur);
504         end.tv_sec = 0;
505         /*
506          * 0.5s is the recommended timeout from the U2S manual.  The actual
507          * time required should be smaller by at least a factor of 1000.
508          * We have no choice but to busy-wait.
509          */
510         end.tv_usec = 500000;
511         timevaladd(&end, &cur);
512
513         while ((!*is->is_flushva[0] || !*is->is_flushva[1]) &&
514             timevalcmp(&cur, &end, <=))
515                 microuptime(&cur);
516
517         if (!*is->is_flushva[0] || !*is->is_flushva[1]) {
518                 panic("%s: flush timeout %ld, %ld at %#lx", __func__,
519                     *is->is_flushva[0], *is->is_flushva[1], is->is_flushpa[0]);
520         }
521
522         return (1);
523 }
524
525 /* Determine whether we may enable streaming on a mapping. */
526 static __inline int
527 iommu_use_streaming(struct iommu_state *is, bus_dmamap_t map, bus_size_t size)
528 {
529
530         /*
531          * This cannot be enabled yet, as many driver are still missing
532          * bus_dmamap_sync() calls.  As soon as there is a BUS_DMA_STREAMING
533          * flag, this should be reenabled conditionally on it.
534          */
535 #ifdef notyet
536         return (size >= IOMMU_STREAM_THRESH && IOMMU_HAS_SB(is) &&
537             (map->dm_flags & DMF_COHERENT) == 0);
538 #else
539         return (0);
540 #endif
541 }
542
543 /*
544  * Allocate DVMA virtual memory for a map.  The map may not be on a queue,
545  * so that it can be freely modified.
546  */
547 static int
548 iommu_dvma_valloc(bus_dma_tag_t t, struct iommu_state *is, bus_dmamap_t map,
549     bus_size_t size)
550 {
551         struct resource *res;
552         struct bus_dmamap_res *bdr;
553         bus_size_t align, sgsize;
554
555         KASSERT(!map->dm_onq, ("%s: map on queue!", __func__));
556         if ((bdr = malloc(sizeof(*bdr), M_IOMMU, M_NOWAIT)) == NULL)
557                 return (EAGAIN);
558         /*
559          * If a boundary is specified, a map cannot be larger than it; however
560          * we do not clip currently, as that does not play well with the lazy
561          * allocation code.
562          * Alignment to a page boundary is always enforced.
563          */
564         align = (t->dt_alignment + IO_PAGE_MASK) >> IO_PAGE_SHIFT;
565         sgsize = round_io_page(size) >> IO_PAGE_SHIFT;
566         if (t->dt_boundary > 0 && t->dt_boundary < IO_PAGE_SIZE)
567                 panic("%s: illegal boundary specified", __func__);
568         res = rman_reserve_resource_bound(&is->is_dvma_rman, 0L,
569             t->dt_lowaddr >> IO_PAGE_SHIFT, sgsize,
570             t->dt_boundary >> IO_PAGE_SHIFT,
571             RF_ACTIVE | rman_make_alignment_flags(align), NULL);
572         if (res == NULL) {
573                 free(bdr, M_IOMMU);
574                 return (ENOMEM);
575         }
576
577         bdr->dr_res = res;
578         bdr->dr_used = 0;
579         SLIST_INSERT_HEAD(&map->dm_reslist, bdr, dr_link);
580         return (0);
581 }
582
583 /* Unload the map and mark all resources as unused, but do not free them. */
584 static void
585 iommu_dvmamap_vunload(struct iommu_state *is, bus_dmamap_t map)
586 {
587         struct bus_dmamap_res *r;
588         int streamed = 0;
589
590         IS_LOCK_ASSERT(is);     /* for iommu_strbuf_sync() below */
591         SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
592                 streamed |= iommu_remove(is, BDR_START(r), r->dr_used);
593                 r->dr_used = 0;
594         }
595         if (streamed)
596                 iommu_strbuf_sync(is);
597 }
598
599 /* Free a DVMA virtual memory resource. */
600 static __inline void
601 iommu_dvma_vfree_res(bus_dmamap_t map, struct bus_dmamap_res *r)
602 {
603
604         KASSERT(r->dr_used == 0, ("%s: resource busy!", __func__));
605         if (r->dr_res != NULL && rman_release_resource(r->dr_res) != 0)
606                 printf("warning: DVMA space lost\n");
607         SLIST_REMOVE(&map->dm_reslist, r, bus_dmamap_res, dr_link);
608         free(r, M_IOMMU);
609 }
610
611 /* Free all DVMA virtual memory for a map. */
612 static void
613 iommu_dvma_vfree(struct iommu_state *is, bus_dmamap_t map)
614 {
615
616         IS_LOCK(is);
617         iommu_map_remq(is, map);
618         iommu_dvmamap_vunload(is, map);
619         IS_UNLOCK(is);
620         while (!SLIST_EMPTY(&map->dm_reslist))
621                 iommu_dvma_vfree_res(map, SLIST_FIRST(&map->dm_reslist));
622 }
623
624 /* Prune a map, freeing all unused DVMA resources. */
625 static bus_size_t
626 iommu_dvma_vprune(struct iommu_state *is, bus_dmamap_t map)
627 {
628         struct bus_dmamap_res *r, *n;
629         bus_size_t freed = 0;
630
631         IS_LOCK_ASSERT(is);
632         for (r = SLIST_FIRST(&map->dm_reslist); r != NULL; r = n) {
633                 n = SLIST_NEXT(r, dr_link);
634                 if (r->dr_used == 0) {
635                         freed += BDR_SIZE(r);
636                         iommu_dvma_vfree_res(map, r);
637                 }
638         }
639         if (SLIST_EMPTY(&map->dm_reslist))
640                 iommu_map_remq(is, map);
641         return (freed);
642 }
643
644 /*
645  * Try to find a suitably-sized (and if requested, -aligned) slab of DVMA
646  * memory with IO page offset voffs.
647  */
648 static bus_addr_t
649 iommu_dvma_vfindseg(bus_dmamap_t map, vm_offset_t voffs, bus_size_t size,
650     bus_addr_t amask)
651 {
652         struct bus_dmamap_res *r;
653         bus_addr_t dvmaddr, dvmend;
654
655         KASSERT(!map->dm_onq, ("%s: map on queue!", __func__));
656         SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
657                 dvmaddr = round_io_page(BDR_START(r) + r->dr_used);
658                 /* Alignment can only work with voffs == 0. */
659                 dvmaddr = (dvmaddr + amask) & ~amask;
660                 dvmaddr += voffs;
661                 dvmend = dvmaddr + size;
662                 if (dvmend <= BDR_END(r)) {
663                         r->dr_used = dvmend - BDR_START(r);
664                         return (dvmaddr);
665                 }
666         }
667         return (0);
668 }
669
670 /*
671  * Try to find or allocate a slab of DVMA space; see above.
672  */
673 static int
674 iommu_dvma_vallocseg(bus_dma_tag_t dt, struct iommu_state *is, bus_dmamap_t map,
675     vm_offset_t voffs, bus_size_t size, bus_addr_t amask, bus_addr_t *addr)
676 {
677         bus_dmamap_t tm, last;
678         bus_addr_t dvmaddr, freed;
679         int error, complete = 0;
680
681         dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
682
683         /* Need to allocate. */
684         if (dvmaddr == 0) {
685                 while ((error = iommu_dvma_valloc(dt, is, map,
686                         voffs + size)) == ENOMEM && !complete) {
687                         /*
688                          * Free the allocated DVMA of a few maps until
689                          * the required size is reached. This is an
690                          * approximation to not have to call the allocation
691                          * function too often; most likely one free run
692                          * will not suffice if not one map was large enough
693                          * itself due to fragmentation.
694                          */
695                         IS_LOCK(is);
696                         freed = 0;
697                         last = TAILQ_LAST(&is->is_maplruq, iommu_maplruq_head);
698                         do {
699                                 tm = TAILQ_FIRST(&is->is_maplruq);
700                                 complete = tm == last;
701                                 if (tm == NULL)
702                                         break;
703                                 freed += iommu_dvma_vprune(is, tm);
704                                 /* Move to the end. */
705                                 iommu_map_insq(is, tm);
706                         } while (freed < size && !complete);
707                         IS_UNLOCK(is);
708                 }
709                 if (error != 0)
710                         return (error);
711                 dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
712                 KASSERT(dvmaddr != 0, ("%s: allocation failed unexpectedly!",
713                     __func__));
714         }
715         *addr = dvmaddr;
716         return (0);
717 }
718
719 static int
720 iommu_dvmamem_alloc(bus_dma_tag_t dt, void **vaddr, int flags,
721     bus_dmamap_t *mapp)
722 {
723         struct iommu_state *is = dt->dt_cookie;
724         int error, mflags;
725
726         /*
727          * XXX: This will break for 32 bit transfers on machines with more
728          * than is->is_pmaxaddr memory.
729          */
730         if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
731                 return (error);
732
733         if ((flags & BUS_DMA_NOWAIT) != 0)
734                 mflags = M_NOWAIT;
735         else
736                 mflags = M_WAITOK;
737         if ((flags & BUS_DMA_ZERO) != 0)
738                 mflags |= M_ZERO;
739
740         if ((*vaddr = malloc(dt->dt_maxsize, M_IOMMU, mflags)) == NULL) {
741                 error = ENOMEM;
742                 sparc64_dma_free_map(dt, *mapp);
743                 return (error);
744         }
745         if ((flags & BUS_DMA_COHERENT) != 0)
746                 (*mapp)->dm_flags |= DMF_COHERENT;
747         /*
748          * Try to preallocate DVMA space.  If this fails, it is retried at
749          * load time.
750          */
751         iommu_dvma_valloc(dt, is, *mapp, IOMMU_SIZE_ROUNDUP(dt->dt_maxsize));
752         IS_LOCK(is);
753         iommu_map_insq(is, *mapp);
754         IS_UNLOCK(is);
755         return (0);
756 }
757
758 static void
759 iommu_dvmamem_free(bus_dma_tag_t dt, void *vaddr, bus_dmamap_t map)
760 {
761         struct iommu_state *is = dt->dt_cookie;
762
763         iommu_dvma_vfree(is, map);
764         sparc64_dma_free_map(dt, map);
765         free(vaddr, M_IOMMU);
766 }
767
768 static int
769 iommu_dvmamap_create(bus_dma_tag_t dt, int flags, bus_dmamap_t *mapp)
770 {
771         struct iommu_state *is = dt->dt_cookie;
772         bus_size_t totsz, presz, currsz;
773         int error, i, maxpre;
774
775         if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
776                 return (error);
777         if ((flags & BUS_DMA_COHERENT) != 0)
778                 (*mapp)->dm_flags |= DMF_COHERENT;
779         /*
780          * Preallocate DVMA space; if this fails now, it is retried at load
781          * time.  Through bus_dmamap_load_mbuf() and bus_dmamap_load_uio(),
782          * it is possible to have multiple discontiguous segments in a single
783          * map, which is handled by allocating additional resources, instead
784          * of increasing the size, to avoid fragmentation.
785          * Clamp preallocation to IOMMU_MAX_PRE.  In some situations we can
786          * handle more; that case is handled by reallocating at map load time.
787          */
788         totsz = ulmin(IOMMU_SIZE_ROUNDUP(dt->dt_maxsize), IOMMU_MAX_PRE);
789         error = iommu_dvma_valloc(dt, is, *mapp, totsz);
790         if (error != 0)
791                 return (0);
792         /*
793          * Try to be smart about preallocating some additional segments if
794          * needed.
795          */
796         maxpre = imin(dt->dt_nsegments, IOMMU_MAX_PRE_SEG);
797         presz = dt->dt_maxsize / maxpre;
798         KASSERT(presz != 0, ("%s: bogus preallocation size , nsegments = %d, "
799             "maxpre = %d, maxsize = %lu", __func__, dt->dt_nsegments, maxpre,
800             dt->dt_maxsize));
801         for (i = 1; i < maxpre && totsz < IOMMU_MAX_PRE; i++) {
802                 currsz = round_io_page(ulmin(presz, IOMMU_MAX_PRE - totsz));
803                 error = iommu_dvma_valloc(dt, is, *mapp, currsz);
804                 if (error != 0)
805                         break;
806                 totsz += currsz;
807         }
808         IS_LOCK(is);
809         iommu_map_insq(is, *mapp);
810         IS_UNLOCK(is);
811         return (0);
812 }
813
814 static int
815 iommu_dvmamap_destroy(bus_dma_tag_t dt, bus_dmamap_t map)
816 {
817         struct iommu_state *is = dt->dt_cookie;
818
819         iommu_dvma_vfree(is, map);
820         sparc64_dma_free_map(dt, map);
821         return (0);
822 }
823
824 /*
825  * IOMMU DVMA operations, common to PCI and SBus
826  */
827 static int
828 iommu_dvmamap_load_buffer(bus_dma_tag_t dt, struct iommu_state *is,
829     bus_dmamap_t map, void *buf, bus_size_t buflen, struct thread *td,
830     int flags, bus_dma_segment_t *segs, int *segp, int align)
831 {
832         bus_addr_t amask, dvmaddr;
833         bus_size_t sgsize, esize;
834         vm_offset_t vaddr, voffs;
835         vm_paddr_t curaddr;
836         pmap_t pmap = NULL;
837         int error, firstpg, sgcnt;
838
839         KASSERT(buflen != 0, ("%s: buflen == 0!", __func__));
840         if (buflen > dt->dt_maxsize)
841                 return (EINVAL);
842
843         if (td != NULL)
844                 pmap = vmspace_pmap(td->td_proc->p_vmspace);
845
846         vaddr = (vm_offset_t)buf;
847         voffs = vaddr & IO_PAGE_MASK;
848         amask = align ? dt->dt_alignment - 1 : 0;
849
850         /* Try to find a slab that is large enough. */
851         error = iommu_dvma_vallocseg(dt, is, map, voffs, buflen, amask,
852             &dvmaddr);
853         if (error != 0)
854                 return (error);
855
856         sgcnt = *segp;
857         firstpg = 1;
858         map->dm_flags &= ~DMF_STREAMED;
859         map->dm_flags |= iommu_use_streaming(is, map, buflen) != 0 ?
860             DMF_STREAMED : 0;
861         for (; buflen > 0; ) {
862                 /*
863                  * Get the physical address for this page.
864                  */
865                 if (pmap != NULL)
866                         curaddr = pmap_extract(pmap, vaddr);
867                 else
868                         curaddr = pmap_kextract(vaddr);
869
870                 /*
871                  * Compute the segment size, and adjust counts.
872                  */
873                 sgsize = IO_PAGE_SIZE - ((u_long)vaddr & IO_PAGE_MASK);
874                 if (buflen < sgsize)
875                         sgsize = buflen;
876
877                 buflen -= sgsize;
878                 vaddr += sgsize;
879
880                 iommu_enter(is, trunc_io_page(dvmaddr), trunc_io_page(curaddr),
881                     (map->dm_flags & DMF_STREAMED) != 0, flags);
882
883                 /*
884                  * Chop the chunk up into segments of at most maxsegsz, but try
885                  * to fill each segment as well as possible.
886                  */
887                 if (!firstpg) {
888                         esize = ulmin(sgsize,
889                             dt->dt_maxsegsz - segs[sgcnt].ds_len);
890                         segs[sgcnt].ds_len += esize;
891                         sgsize -= esize;
892                         dvmaddr += esize;
893                 }
894                 while (sgsize > 0) {
895                         sgcnt++;
896                         if (sgcnt >= dt->dt_nsegments)
897                                 return (EFBIG);
898                         /*
899                          * No extra alignment here - the common practice in
900                          * the busdma code seems to be that only the first
901                          * segment needs to satisfy the alignment constraints
902                          * (and that only for bus_dmamem_alloc()ed maps).
903                          * It is assumed that such tags have maxsegsize >=
904                          * maxsize.
905                          */
906                         esize = ulmin(sgsize, dt->dt_maxsegsz);
907                         segs[sgcnt].ds_addr = dvmaddr;
908                         segs[sgcnt].ds_len = esize;
909                         sgsize -= esize;
910                         dvmaddr += esize;
911                 }
912
913                 firstpg = 0;
914         }
915         *segp = sgcnt;
916         return (0);
917 }
918
919 static int
920 iommu_dvmamap_load(bus_dma_tag_t dt, bus_dmamap_t map, void *buf,
921     bus_size_t buflen, bus_dmamap_callback_t *cb, void *cba,
922     int flags)
923 {
924         struct iommu_state *is = dt->dt_cookie;
925         int error, seg = -1;
926
927         if ((map->dm_flags & DMF_LOADED) != 0) {
928 #ifdef DIAGNOSTIC
929                 printf("%s: map still in use\n", __func__);
930 #endif
931                 bus_dmamap_unload(dt, map);
932         }
933
934         /*
935          * Make sure that the map is not on a queue so that the resource list
936          * may be safely accessed and modified without needing the lock to
937          * cover the whole operation.
938          */
939         IS_LOCK(is);
940         iommu_map_remq(is, map);
941         IS_UNLOCK(is);
942
943         error = iommu_dvmamap_load_buffer(dt, is, map, buf, buflen, NULL,
944             flags, dt->dt_segments, &seg, 1);
945
946         IS_LOCK(is);
947         iommu_map_insq(is, map);
948         if (error != 0) {
949                 iommu_dvmamap_vunload(is, map);
950                 IS_UNLOCK(is);
951                 (*cb)(cba, dt->dt_segments, 0, error);
952         } else {
953                 IS_UNLOCK(is);
954                 map->dm_flags |= DMF_LOADED;
955                 (*cb)(cba, dt->dt_segments, seg + 1, 0);
956         }
957
958         return (error);
959 }
960
961 static int
962 iommu_dvmamap_load_mbuf(bus_dma_tag_t dt, bus_dmamap_t map, struct mbuf *m0,
963     bus_dmamap_callback2_t *cb, void *cba, int flags)
964 {
965         struct iommu_state *is = dt->dt_cookie;
966         struct mbuf *m;
967         int error = 0, first = 1, nsegs = -1;
968
969         M_ASSERTPKTHDR(m0);
970
971         if ((map->dm_flags & DMF_LOADED) != 0) {
972 #ifdef DIAGNOSTIC
973                 printf("%s: map still in use\n", __func__);
974 #endif
975                 bus_dmamap_unload(dt, map);
976         }
977
978         IS_LOCK(is);
979         iommu_map_remq(is, map);
980         IS_UNLOCK(is);
981
982         if (m0->m_pkthdr.len <= dt->dt_maxsize) {
983                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
984                         if (m->m_len == 0)
985                                 continue;
986                         error = iommu_dvmamap_load_buffer(dt, is, map,
987                             m->m_data, m->m_len, NULL, flags, dt->dt_segments,
988                             &nsegs, first);
989                         first = 0;
990                 }
991         } else
992                 error = EINVAL;
993
994         IS_LOCK(is);
995         iommu_map_insq(is, map);
996         if (error != 0) {
997                 iommu_dvmamap_vunload(is, map);
998                 IS_UNLOCK(is);
999                 /* force "no valid mappings" in callback */
1000                 (*cb)(cba, dt->dt_segments, 0, 0, error);
1001         } else {
1002                 IS_UNLOCK(is);
1003                 map->dm_flags |= DMF_LOADED;
1004                 (*cb)(cba, dt->dt_segments, nsegs + 1, m0->m_pkthdr.len, 0);
1005         }
1006         return (error);
1007 }
1008
1009 static int
1010 iommu_dvmamap_load_mbuf_sg(bus_dma_tag_t dt, bus_dmamap_t map, struct mbuf *m0,
1011     bus_dma_segment_t *segs, int *nsegs, int flags)
1012 {
1013         struct iommu_state *is = dt->dt_cookie;
1014         struct mbuf *m;
1015         int error = 0, first = 1;
1016
1017         M_ASSERTPKTHDR(m0);
1018
1019         *nsegs = -1;
1020         if ((map->dm_flags & DMF_LOADED) != 0) {
1021 #ifdef DIAGNOSTIC
1022                 printf("%s: map still in use\n", __func__);
1023 #endif
1024                 bus_dmamap_unload(dt, map);
1025         }
1026
1027         IS_LOCK(is);
1028         iommu_map_remq(is, map);
1029         IS_UNLOCK(is);
1030
1031         if (m0->m_pkthdr.len <= dt->dt_maxsize) {
1032                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
1033                         if (m->m_len == 0)
1034                                 continue;
1035                         error = iommu_dvmamap_load_buffer(dt, is, map,
1036                             m->m_data, m->m_len, NULL, flags, segs,
1037                             nsegs, first);
1038                         first = 0;
1039                 }
1040         } else
1041                 error = EINVAL;
1042
1043         IS_LOCK(is);
1044         iommu_map_insq(is, map);
1045         if (error != 0) {
1046                 iommu_dvmamap_vunload(is, map);
1047                 IS_UNLOCK(is);
1048         } else {
1049                 IS_UNLOCK(is);
1050                 map->dm_flags |= DMF_LOADED;
1051                 ++*nsegs;
1052         }
1053         return (error);
1054 }
1055
1056 static int
1057 iommu_dvmamap_load_uio(bus_dma_tag_t dt, bus_dmamap_t map, struct uio *uio,
1058     bus_dmamap_callback2_t *cb,  void *cba, int flags)
1059 {
1060         struct iommu_state *is = dt->dt_cookie;
1061         struct iovec *iov;
1062         struct thread *td = NULL;
1063         bus_size_t minlen, resid;
1064         int nsegs = -1, error = 0, first = 1, i;
1065
1066         if ((map->dm_flags & DMF_LOADED) != 0) {
1067 #ifdef DIAGNOSTIC
1068                 printf("%s: map still in use\n", __func__);
1069 #endif
1070                 bus_dmamap_unload(dt, map);
1071         }
1072
1073         IS_LOCK(is);
1074         iommu_map_remq(is, map);
1075         IS_UNLOCK(is);
1076
1077         resid = uio->uio_resid;
1078         iov = uio->uio_iov;
1079
1080         if (uio->uio_segflg == UIO_USERSPACE) {
1081                 td = uio->uio_td;
1082                 KASSERT(td != NULL,
1083                     ("%s: USERSPACE but no proc", __func__));
1084         }
1085
1086         for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
1087                 /*
1088                  * Now at the first iovec to load.  Load each iovec
1089                  * until we have exhausted the residual count.
1090                  */
1091                 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
1092                 if (minlen == 0)
1093                         continue;
1094
1095                 error = iommu_dvmamap_load_buffer(dt, is, map,
1096                     iov[i].iov_base, minlen, td, flags, dt->dt_segments,
1097                     &nsegs, first);
1098                 first = 0;
1099
1100                 resid -= minlen;
1101         }
1102
1103         IS_LOCK(is);
1104         iommu_map_insq(is, map);
1105         if (error) {
1106                 iommu_dvmamap_vunload(is, map);
1107                 IS_UNLOCK(is);
1108                 /* force "no valid mappings" in callback */
1109                 (*cb)(cba, dt->dt_segments, 0, 0, error);
1110         } else {
1111                 IS_UNLOCK(is);
1112                 map->dm_flags |= DMF_LOADED;
1113                 (*cb)(cba, dt->dt_segments, nsegs + 1, uio->uio_resid, 0);
1114         }
1115         return (error);
1116 }
1117
1118 static void
1119 iommu_dvmamap_unload(bus_dma_tag_t dt, bus_dmamap_t map)
1120 {
1121         struct iommu_state *is = dt->dt_cookie;
1122
1123         if ((map->dm_flags & DMF_LOADED) == 0)
1124                 return;
1125         IS_LOCK(is);
1126         iommu_dvmamap_vunload(is, map);
1127         iommu_map_insq(is, map);
1128         IS_UNLOCK(is);
1129         map->dm_flags &= ~DMF_LOADED;
1130 }
1131
1132 static void
1133 iommu_dvmamap_sync(bus_dma_tag_t dt, bus_dmamap_t map, bus_dmasync_op_t op)
1134 {
1135         struct iommu_state *is = dt->dt_cookie;
1136         struct bus_dmamap_res *r;
1137         vm_offset_t va;
1138         vm_size_t len;
1139         int streamed = 0;
1140
1141         if ((map->dm_flags & DMF_LOADED) == 0)
1142                 return;
1143         /* XXX This is probably bogus. */
1144         if ((op & BUS_DMASYNC_PREREAD) != 0)
1145                 membar(Sync);
1146         if ((map->dm_flags & DMF_STREAMED) != 0 &&
1147             ((op & BUS_DMASYNC_POSTREAD) != 0 ||
1148             (op & BUS_DMASYNC_PREWRITE) != 0)) {
1149                 IS_LOCK(is);
1150                 SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
1151                         va = (vm_offset_t)BDR_START(r);
1152                         len = r->dr_used;
1153                         /*
1154                          * If we have a streaming buffer, flush it here
1155                          * first.
1156                          */
1157                         while (len > 0) {
1158                                 if ((IOMMU_GET_TTE(is, va) &
1159                                     IOTTE_STREAM) != 0) {
1160                                         streamed = 1;
1161                                         iommu_strbuf_flush(is, va);
1162                                 }
1163                                 len -= ulmin(len, IO_PAGE_SIZE);
1164                                 va += IO_PAGE_SIZE;
1165                         }
1166                 }
1167                 if (streamed)
1168                         iommu_strbuf_sync(is);
1169                 IS_UNLOCK(is);
1170         }
1171         if ((op & BUS_DMASYNC_PREWRITE) != 0)
1172                 membar(Sync);
1173 }
1174
1175 #ifdef IOMMU_DIAG
1176
1177 /*
1178  * Perform an IOMMU diagnostic access and print the tag belonging to va.
1179  */
1180 static void
1181 iommu_diag(struct iommu_state *is, vm_offset_t va)
1182 {
1183         int i;
1184         uint64_t data, tag;
1185
1186         IS_LOCK_ASSERT(is);
1187         IOMMU_WRITE8(is, is_dva, 0, trunc_io_page(va));
1188         membar(StoreStore | StoreLoad);
1189         printf("%s: tte entry %#lx", __func__, IOMMU_GET_TTE(is, va));
1190         if (is->is_dtcmp != 0) {
1191                 printf(", tag compare register is %#lx\n",
1192                     IOMMU_READ8(is, is_dtcmp, 0));
1193         } else
1194                 printf("\n");
1195         for (i = 0; i < 16; i++) {
1196                 tag = IOMMU_READ8(is, is_dtag, i * 8);
1197                 data = IOMMU_READ8(is, is_ddram, i * 8);
1198                 printf("%s: tag %d: %#lx, vpn %#lx, err %lx; "
1199                     "data %#lx, pa %#lx, v %d, c %d\n", __func__, i,
1200                     tag, (tag & IOMMU_DTAG_VPNMASK) << IOMMU_DTAG_VPNSHIFT,
1201                     (tag & IOMMU_DTAG_ERRMASK) >> IOMMU_DTAG_ERRSHIFT, data,
1202                     (data & IOMMU_DDATA_PGMASK) << IOMMU_DDATA_PGSHIFT,
1203                     (data & IOMMU_DDATA_V) != 0, (data & IOMMU_DDATA_C) != 0);
1204         }
1205 }
1206
1207 #endif /* IOMMU_DIAG */
1208
1209 struct bus_dma_methods iommu_dma_methods = {
1210         iommu_dvmamap_create,
1211         iommu_dvmamap_destroy,
1212         iommu_dvmamap_load,
1213         iommu_dvmamap_load_mbuf,
1214         iommu_dvmamap_load_mbuf_sg,
1215         iommu_dvmamap_load_uio,
1216         iommu_dvmamap_unload,
1217         iommu_dvmamap_sync,
1218         iommu_dvmamem_alloc,
1219         iommu_dvmamem_free,
1220 };