2 * Copyright (c) 1999, 2000 Matthew R. Green
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * from: NetBSD: iommu.c,v 1.82 2008/05/30 02:29:37 mrg Exp
29 * Copyright (c) 1999-2002 Eduardo Horvath
30 * Copyright (c) 2001-2003 Thomas Moestl
31 * Copyright (c) 2007, 2009 Marius Strobl <marius@FreeBSD.org>
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
50 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
52 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * from: NetBSD: sbus.c,v 1.50 2002/06/20 18:26:24 eeh Exp
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
64 * UltraSPARC IOMMU support; used by both the PCI and SBus code.
67 * - Support sub-page boundaries.
68 * - Fix alignment handling for small allocations (the possible page offset
69 * of malloc()ed memory is not handled at all). Revise interaction of
70 * alignment with the load_mbuf and load_uio functions.
71 * - Handle lowaddr and highaddr in some way, and try to work out a way
72 * for filter callbacks to work. Currently, only lowaddr is honored
73 * in that no addresses above it are considered at all.
74 * - Implement BUS_DMA_ALLOCNOW in bus_dma_tag_create as far as possible.
75 * - Check the possible return values and callback error arguments;
76 * the callback currently gets called in error conditions where it should
78 * - When running out of DVMA space, return EINPROGRESS in the non-
79 * BUS_DMA_NOWAIT case and delay the callback until sufficient space
83 #include "opt_iommu.h"
85 #include <sys/param.h>
86 #include <sys/kernel.h>
88 #include <sys/malloc.h>
90 #include <sys/mutex.h>
93 #include <sys/systm.h>
98 #include <vm/vm_map.h>
100 #include <machine/asi.h>
101 #include <machine/bus.h>
102 #include <machine/bus_private.h>
103 #include <machine/iommureg.h>
104 #include <machine/pmap.h>
105 #include <machine/resource.h>
106 #include <machine/ver.h>
108 #include <sys/rman.h>
110 #include <machine/iommuvar.h>
115 #define IOMMU_MAX_PRE (32 * 1024)
116 #define IOMMU_MAX_PRE_SEG 3
118 /* Threshold for using the streaming buffer */
119 #define IOMMU_STREAM_THRESH 128
121 static MALLOC_DEFINE(M_IOMMU, "dvmamem", "IOMMU DVMA Buffers");
123 static int iommu_strbuf_flush_sync(struct iommu_state *);
125 static void iommu_diag(struct iommu_state *, vm_offset_t va);
131 #define IOMMU_READ8(is, reg, off) \
132 bus_space_read_8((is)->is_bustag, (is)->is_bushandle, \
134 #define IOMMU_WRITE8(is, reg, off, v) \
135 bus_space_write_8((is)->is_bustag, (is)->is_bushandle, \
136 (is)->reg + (off), (v))
138 #define IOMMU_HAS_SB(is) \
139 ((is)->is_sb[0] != 0 || (is)->is_sb[1] != 0)
142 * Always overallocate one page; this is needed to handle alignment of the
143 * buffer, so it makes sense using a lazy allocation scheme.
145 #define IOMMU_SIZE_ROUNDUP(sz) \
146 (round_io_page(sz) + IO_PAGE_SIZE)
148 #define IOMMU_SET_TTE(is, va, tte) \
149 ((is)->is_tsb[IOTSBSLOT(va)] = (tte))
150 #define IOMMU_GET_TTE(is, va) \
151 (is)->is_tsb[IOTSBSLOT(va)]
153 /* Resource helpers */
154 #define IOMMU_RES_START(res) \
155 ((bus_addr_t)rman_get_start(res) << IO_PAGE_SHIFT)
156 #define IOMMU_RES_END(res) \
157 ((bus_addr_t)(rman_get_end(res) + 1) << IO_PAGE_SHIFT)
158 #define IOMMU_RES_SIZE(res) \
159 ((bus_size_t)rman_get_size(res) << IO_PAGE_SHIFT)
161 /* Helpers for struct bus_dmamap_res */
162 #define BDR_START(r) IOMMU_RES_START((r)->dr_res)
163 #define BDR_END(r) IOMMU_RES_END((r)->dr_res)
164 #define BDR_SIZE(r) IOMMU_RES_SIZE((r)->dr_res)
167 #define IS_LOCK(is) mtx_lock(&is->is_mtx)
168 #define IS_LOCK_ASSERT(is) mtx_assert(&is->is_mtx, MA_OWNED)
169 #define IS_UNLOCK(is) mtx_unlock(&is->is_mtx)
171 /* Flush a page from the TLB. No locking required, since this is atomic. */
173 iommu_tlb_flush(struct iommu_state *is, bus_addr_t va)
176 if ((is->is_flags & IOMMU_FIRE) != 0)
178 * Direct page flushing is not supported and also not
179 * necessary due to cache snooping.
182 IOMMU_WRITE8(is, is_iommu, IMR_FLUSH, va);
186 * Flush a page from the streaming buffer. No locking required, since this
190 iommu_strbuf_flushpg(struct iommu_state *is, bus_addr_t va)
194 for (i = 0; i < 2; i++)
195 if (is->is_sb[i] != 0)
196 IOMMU_WRITE8(is, is_sb[i], ISR_PGFLUSH, va);
200 * Flush an address from the streaming buffer(s); this is an asynchronous
201 * operation. To make sure that it has completed, iommu_strbuf_sync() needs
202 * to be called. No locking required.
205 iommu_strbuf_flush(struct iommu_state *is, bus_addr_t va)
208 iommu_strbuf_flushpg(is, va);
211 /* Synchronize all outstanding flush operations. */
213 iommu_strbuf_sync(struct iommu_state *is)
217 iommu_strbuf_flush_sync(is);
220 /* LRU queue handling for lazy resource allocation. */
222 iommu_map_insq(struct iommu_state *is, bus_dmamap_t map)
226 if (!SLIST_EMPTY(&map->dm_reslist)) {
228 TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
229 TAILQ_INSERT_TAIL(&is->is_maplruq, map, dm_maplruq);
235 iommu_map_remq(struct iommu_state *is, bus_dmamap_t map)
240 TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
245 * initialise the UltraSPARC IOMMU (PCI or SBus):
246 * - allocate and setup the iotsb.
248 * - initialise the streaming buffers (if they exist)
249 * - create a private DVMA map.
252 iommu_init(const char *name, struct iommu_state *is, u_int tsbsize,
253 uint32_t iovabase, u_int resvpg)
257 uint64_t end, obpmap, obpptsb, tte;
258 u_int maxtsbsize, obptsbentries, obptsbsize, slot, tsbentries;
264 * The sun4u IOMMU is part of the PCI or SBus controller so we
265 * will deal with it here..
267 * The IOMMU address space always ends at 0xffffe000, but the starting
268 * address depends on the size of the map. The map size is 1024 * 2 ^
269 * is->is_tsbsize entries, where each entry is 8 bytes. The start of
270 * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
272 if ((is->is_flags & IOMMU_FIRE) != 0) {
273 maxtsbsize = IOMMU_TSB512K;
275 * We enable bypass in order to be able to use a physical
276 * address for the event queue base.
278 is->is_cr = IOMMUCR_SE | IOMMUCR_CM_C_TLB_TBW | IOMMUCR_BE;
280 maxtsbsize = IOMMU_TSB128K;
281 is->is_cr = (tsbsize << IOMMUCR_TSBSZ_SHIFT) | IOMMUCR_DE;
283 if (tsbsize > maxtsbsize)
284 panic("%s: unsupported TSB size ", __func__);
285 tsbentries = IOMMU_TSBENTRIES(tsbsize);
286 is->is_cr |= IOMMUCR_EN;
287 is->is_tsbsize = tsbsize;
288 is->is_dvmabase = iovabase;
290 is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
292 size = IOTSB_BASESZ << is->is_tsbsize;
293 printf("%s: DVMA map: %#lx to %#lx %d entries%s\n", name,
294 is->is_dvmabase, is->is_dvmabase +
295 (size << (IO_PAGE_SHIFT - IOTTE_SHIFT)) - 1, tsbentries,
296 IOMMU_HAS_SB(is) ? ", streaming buffer" : "");
299 * Set up resource mamangement.
301 mtx_init(&is->is_mtx, "iommu", NULL, MTX_DEF);
302 end = is->is_dvmabase + (size << (IO_PAGE_SHIFT - IOTTE_SHIFT));
303 is->is_dvma_rman.rm_type = RMAN_ARRAY;
304 is->is_dvma_rman.rm_descr = "DVMA Memory";
305 if (rman_init(&is->is_dvma_rman) != 0 ||
306 rman_manage_region(&is->is_dvma_rman,
307 (is->is_dvmabase >> IO_PAGE_SHIFT) + resvpg,
308 (end >> IO_PAGE_SHIFT) - 1) != 0)
309 panic("%s: could not initialize DVMA rman", __func__);
310 TAILQ_INIT(&is->is_maplruq);
313 * Allocate memory for I/O page tables. They need to be
314 * physically contiguous.
316 is->is_tsb = contigmalloc(size, M_DEVBUF, M_NOWAIT, 0, ~0UL,
318 if (is->is_tsb == NULL)
319 panic("%s: contigmalloc failed", __func__);
320 is->is_ptsb = pmap_kextract((vm_offset_t)is->is_tsb);
321 bzero(is->is_tsb, size);
324 * Add the PROM mappings to the kernel IOTSB if desired.
325 * Note that the firmware of certain Darwin boards doesn't set
326 * the TSB size correctly.
328 if ((is->is_flags & IOMMU_FIRE) != 0)
329 obptsbsize = (IOMMU_READ8(is, is_iommu, IMR_TSB) &
330 IOMMUTB_TSBSZ_MASK) >> IOMMUTB_TSBSZ_SHIFT;
332 obptsbsize = (IOMMU_READ8(is, is_iommu, IMR_CTL) &
333 IOMMUCR_TSBSZ_MASK) >> IOMMUCR_TSBSZ_SHIFT;
334 obptsbentries = IOMMU_TSBENTRIES(obptsbsize);
336 printf("%s: PROM IOTSB size: %d (%d entries)\n", name,
337 obptsbsize, obptsbentries);
338 if ((is->is_flags & IOMMU_PRESERVE_PROM) != 0 &&
339 !(PCPU_GET(impl) == CPU_IMPL_ULTRASPARCIIi && obptsbsize == 7)) {
340 if (obptsbentries > tsbentries)
341 panic("%s: PROM IOTSB entries exceed kernel",
343 obpptsb = IOMMU_READ8(is, is_iommu, IMR_TSB) &
345 for (i = 0; i < obptsbentries; i++) {
346 tte = ldxa(obpptsb + i * 8, ASI_PHYS_USE_EC);
347 if ((tte & IOTTE_V) == 0)
349 slot = tsbentries - obptsbentries + i;
351 printf("%s: adding PROM IOTSB slot %d "
352 "(kernel slot %d) TTE: %#lx\n", name,
354 obpmap = (is->is_dvmabase + slot * IO_PAGE_SIZE) >>
356 if (rman_reserve_resource(&is->is_dvma_rman, obpmap,
357 obpmap, IO_PAGE_SIZE >> IO_PAGE_SHIFT, RF_ACTIVE,
359 panic("%s: could not reserve PROM IOTSB slot "
360 "%d (kernel slot %d)", __func__, i, slot);
361 is->is_tsb[slot] = tte;
366 * Initialize streaming buffer, if it is there.
368 if (IOMMU_HAS_SB(is)) {
370 * Find two 64-byte blocks in is_flush that are aligned on
371 * a 64-byte boundary for flushing.
373 offs = roundup2((vm_offset_t)is->is_flush,
374 STRBUF_FLUSHSYNC_NBYTES);
375 for (i = 0; i < 2; i++, offs += STRBUF_FLUSHSYNC_NBYTES) {
376 is->is_flushva[i] = (uint64_t *)offs;
377 is->is_flushpa[i] = pmap_kextract(offs);
382 * Now actually start up the IOMMU.
388 * Streaming buffers don't exist on the UltraSPARC IIi; we should have
389 * detected that already and disabled them. If not, we will notice that
390 * they aren't there when the STRBUF_EN bit does not remain.
393 iommu_reset(struct iommu_state *is)
399 if ((is->is_flags & IOMMU_FIRE) != 0) {
400 tsb |= is->is_tsbsize;
401 IOMMU_WRITE8(is, is_iommu, IMR_CACHE_INVAL, ~0ULL);
403 IOMMU_WRITE8(is, is_iommu, IMR_TSB, tsb);
404 IOMMU_WRITE8(is, is_iommu, IMR_CTL, is->is_cr);
406 for (i = 0; i < 2; i++) {
407 if (is->is_sb[i] != 0) {
408 IOMMU_WRITE8(is, is_sb[i], ISR_CTL, STRBUF_EN |
409 ((is->is_flags & IOMMU_RERUN_DISABLE) != 0 ?
412 /* No streaming buffers? Disable them. */
413 if ((IOMMU_READ8(is, is_sb[i], ISR_CTL) &
419 (void)IOMMU_READ8(is, is_iommu, IMR_CTL);
423 * Enter a mapping into the TSB. No locking required, since each TSB slot is
424 * uniquely assigned to a single map.
427 iommu_enter(struct iommu_state *is, vm_offset_t va, vm_paddr_t pa,
428 int stream, int flags)
432 KASSERT(va >= is->is_dvmabase,
433 ("%s: va %#lx not in DVMA space", __func__, va));
434 KASSERT(pa <= is->is_pmaxaddr,
435 ("%s: XXX: physical address too large (%#lx)", __func__, pa));
437 tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
438 !(flags & BUS_DMA_NOCACHE), stream);
440 IOMMU_SET_TTE(is, va, tte);
441 iommu_tlb_flush(is, va);
450 * Remove mappings created by iommu_enter(). Flush the streaming buffer,
451 * but do not synchronize it. Returns whether a streaming buffer flush
455 iommu_remove(struct iommu_state *is, vm_offset_t va, vm_size_t len)
457 int slot, streamed = 0;
463 KASSERT(va >= is->is_dvmabase,
464 ("%s: va 0x%lx not in DVMA space", __func__, (u_long)va));
465 KASSERT(va + len >= va,
466 ("%s: va 0x%lx + len 0x%lx wraps", __func__, (long)va, (long)len));
468 va = trunc_io_page(va);
470 if ((IOMMU_GET_TTE(is, va) & IOTTE_STREAM) != 0) {
472 iommu_strbuf_flush(is, va);
474 len -= ulmin(len, IO_PAGE_SIZE);
475 IOMMU_SET_TTE(is, va, 0);
476 iommu_tlb_flush(is, va);
477 if ((is->is_flags & IOMMU_FLUSH_CACHE) != 0) {
478 slot = IOTSBSLOT(va);
479 if (len <= IO_PAGE_SIZE || slot % 8 == 7)
480 IOMMU_WRITE8(is, is_iommu, IMR_CACHE_FLUSH,
481 is->is_ptsb + slot * 8);
488 /* Decode an IOMMU fault for host bridge error handlers. */
490 iommu_decode_fault(struct iommu_state *is, vm_offset_t phys)
495 idx = phys - is->is_ptsb;
496 if (phys < is->is_ptsb ||
497 idx > (PAGE_SIZE << is->is_tsbsize))
499 va = is->is_dvmabase +
500 (((bus_addr_t)idx >> IOTTE_SHIFT) << IO_PAGE_SHIFT);
501 printf("IOMMU fault virtual address %#lx\n", (u_long)va);
505 * A barrier operation which makes sure that all previous streaming buffer
506 * flushes complete before it returns.
509 iommu_strbuf_flush_sync(struct iommu_state *is)
511 struct timeval cur, end;
515 if (!IOMMU_HAS_SB(is))
519 * Streaming buffer flushes:
521 * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
522 * we're not on a cache line boundary (64-bits):
524 * 3 Store pointer to flag in flushsync
525 * 4 wait till flushsync becomes 0x1
527 * If it takes more than .5 sec, something went wrong.
529 *is->is_flushva[0] = 1;
530 *is->is_flushva[1] = 1;
532 for (i = 0; i < 2; i++) {
533 if (is->is_sb[i] != 0) {
534 *is->is_flushva[i] = 0;
535 IOMMU_WRITE8(is, is_sb[i], ISR_FLUSHSYNC,
543 * 0.5s is the recommended timeout from the U2S manual. The actual
544 * time required should be smaller by at least a factor of 1000.
545 * We have no choice but to busy-wait.
547 end.tv_usec = 500000;
548 timevaladd(&end, &cur);
550 while ((!*is->is_flushva[0] || !*is->is_flushva[1]) &&
551 timevalcmp(&cur, &end, <=))
554 if (!*is->is_flushva[0] || !*is->is_flushva[1]) {
555 panic("%s: flush timeout %ld, %ld at %#lx", __func__,
556 *is->is_flushva[0], *is->is_flushva[1], is->is_flushpa[0]);
562 /* Determine whether we may enable streaming on a mapping. */
564 iommu_use_streaming(struct iommu_state *is, bus_dmamap_t map, bus_size_t size)
567 return (size >= IOMMU_STREAM_THRESH && IOMMU_HAS_SB(is) &&
568 (map->dm_flags & DMF_COHERENT) == 0);
572 * Allocate DVMA virtual memory for a map. The map may not be on a queue,
573 * so that it can be freely modified.
576 iommu_dvma_valloc(bus_dma_tag_t t, struct iommu_state *is, bus_dmamap_t map,
579 struct resource *res;
580 struct bus_dmamap_res *bdr;
581 bus_size_t align, sgsize;
583 KASSERT(!map->dm_onq, ("%s: map on queue!", __func__));
584 if ((bdr = malloc(sizeof(*bdr), M_IOMMU, M_NOWAIT)) == NULL)
587 * If a boundary is specified, a map cannot be larger than it; however
588 * we do not clip currently, as that does not play well with the lazy
590 * Alignment to a page boundary is always enforced.
592 align = (t->dt_alignment + IO_PAGE_MASK) >> IO_PAGE_SHIFT;
593 sgsize = round_io_page(size) >> IO_PAGE_SHIFT;
594 if (t->dt_boundary > 0 && t->dt_boundary < IO_PAGE_SIZE)
595 panic("%s: illegal boundary specified", __func__);
596 res = rman_reserve_resource_bound(&is->is_dvma_rman, 0L,
597 t->dt_lowaddr >> IO_PAGE_SHIFT, sgsize,
598 t->dt_boundary >> IO_PAGE_SHIFT,
599 RF_ACTIVE | rman_make_alignment_flags(align), NULL);
607 SLIST_INSERT_HEAD(&map->dm_reslist, bdr, dr_link);
611 /* Unload the map and mark all resources as unused, but do not free them. */
613 iommu_dvmamap_vunload(struct iommu_state *is, bus_dmamap_t map)
615 struct bus_dmamap_res *r;
618 IS_LOCK_ASSERT(is); /* for iommu_strbuf_sync() below */
619 SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
620 streamed |= iommu_remove(is, BDR_START(r), r->dr_used);
624 iommu_strbuf_sync(is);
627 /* Free a DVMA virtual memory resource. */
629 iommu_dvma_vfree_res(bus_dmamap_t map, struct bus_dmamap_res *r)
632 KASSERT(r->dr_used == 0, ("%s: resource busy!", __func__));
633 if (r->dr_res != NULL && rman_release_resource(r->dr_res) != 0)
634 printf("warning: DVMA space lost\n");
635 SLIST_REMOVE(&map->dm_reslist, r, bus_dmamap_res, dr_link);
639 /* Free all DVMA virtual memory for a map. */
641 iommu_dvma_vfree(struct iommu_state *is, bus_dmamap_t map)
645 iommu_map_remq(is, map);
646 iommu_dvmamap_vunload(is, map);
648 while (!SLIST_EMPTY(&map->dm_reslist))
649 iommu_dvma_vfree_res(map, SLIST_FIRST(&map->dm_reslist));
652 /* Prune a map, freeing all unused DVMA resources. */
654 iommu_dvma_vprune(struct iommu_state *is, bus_dmamap_t map)
656 struct bus_dmamap_res *r, *n;
657 bus_size_t freed = 0;
660 for (r = SLIST_FIRST(&map->dm_reslist); r != NULL; r = n) {
661 n = SLIST_NEXT(r, dr_link);
662 if (r->dr_used == 0) {
663 freed += BDR_SIZE(r);
664 iommu_dvma_vfree_res(map, r);
667 if (SLIST_EMPTY(&map->dm_reslist))
668 iommu_map_remq(is, map);
673 * Try to find a suitably-sized (and if requested, -aligned) slab of DVMA
674 * memory with IO page offset voffs.
677 iommu_dvma_vfindseg(bus_dmamap_t map, vm_offset_t voffs, bus_size_t size,
680 struct bus_dmamap_res *r;
681 bus_addr_t dvmaddr, dvmend;
683 KASSERT(!map->dm_onq, ("%s: map on queue!", __func__));
684 SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
685 dvmaddr = round_io_page(BDR_START(r) + r->dr_used);
686 /* Alignment can only work with voffs == 0. */
687 dvmaddr = (dvmaddr + amask) & ~amask;
689 dvmend = dvmaddr + size;
690 if (dvmend <= BDR_END(r)) {
691 r->dr_used = dvmend - BDR_START(r);
699 * Try to find or allocate a slab of DVMA space; see above.
702 iommu_dvma_vallocseg(bus_dma_tag_t dt, struct iommu_state *is, bus_dmamap_t map,
703 vm_offset_t voffs, bus_size_t size, bus_addr_t amask, bus_addr_t *addr)
705 bus_dmamap_t tm, last;
706 bus_addr_t dvmaddr, freed;
707 int error, complete = 0;
709 dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
711 /* Need to allocate. */
713 while ((error = iommu_dvma_valloc(dt, is, map,
714 voffs + size)) == ENOMEM && !complete) {
716 * Free the allocated DVMA of a few maps until
717 * the required size is reached. This is an
718 * approximation to not have to call the allocation
719 * function too often; most likely one free run
720 * will not suffice if not one map was large enough
721 * itself due to fragmentation.
725 last = TAILQ_LAST(&is->is_maplruq, iommu_maplruq_head);
727 tm = TAILQ_FIRST(&is->is_maplruq);
728 complete = tm == last;
731 freed += iommu_dvma_vprune(is, tm);
732 /* Move to the end. */
733 iommu_map_insq(is, tm);
734 } while (freed < size && !complete);
739 dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
740 KASSERT(dvmaddr != 0, ("%s: allocation failed unexpectedly!",
748 iommu_dvmamem_alloc(bus_dma_tag_t dt, void **vaddr, int flags,
751 struct iommu_state *is = dt->dt_cookie;
755 * XXX: This will break for 32 bit transfers on machines with more
756 * than is->is_pmaxaddr memory.
758 if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
761 if ((flags & BUS_DMA_NOWAIT) != 0)
765 if ((flags & BUS_DMA_ZERO) != 0)
768 if ((*vaddr = malloc(dt->dt_maxsize, M_IOMMU, mflags)) == NULL) {
770 sparc64_dma_free_map(dt, *mapp);
773 if ((flags & BUS_DMA_COHERENT) != 0)
774 (*mapp)->dm_flags |= DMF_COHERENT;
776 * Try to preallocate DVMA space. If this fails, it is retried at
779 iommu_dvma_valloc(dt, is, *mapp, IOMMU_SIZE_ROUNDUP(dt->dt_maxsize));
781 iommu_map_insq(is, *mapp);
787 iommu_dvmamem_free(bus_dma_tag_t dt, void *vaddr, bus_dmamap_t map)
789 struct iommu_state *is = dt->dt_cookie;
791 iommu_dvma_vfree(is, map);
792 sparc64_dma_free_map(dt, map);
793 free(vaddr, M_IOMMU);
797 iommu_dvmamap_create(bus_dma_tag_t dt, int flags, bus_dmamap_t *mapp)
799 struct iommu_state *is = dt->dt_cookie;
800 bus_size_t totsz, presz, currsz;
801 int error, i, maxpre;
803 if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
805 if ((flags & BUS_DMA_COHERENT) != 0)
806 (*mapp)->dm_flags |= DMF_COHERENT;
808 * Preallocate DVMA space; if this fails now, it is retried at load
809 * time. Through bus_dmamap_load_mbuf() and bus_dmamap_load_uio(),
810 * it is possible to have multiple discontiguous segments in a single
811 * map, which is handled by allocating additional resources, instead
812 * of increasing the size, to avoid fragmentation.
813 * Clamp preallocation to IOMMU_MAX_PRE. In some situations we can
814 * handle more; that case is handled by reallocating at map load time.
816 totsz = ulmin(IOMMU_SIZE_ROUNDUP(dt->dt_maxsize), IOMMU_MAX_PRE);
817 error = iommu_dvma_valloc(dt, is, *mapp, totsz);
821 * Try to be smart about preallocating some additional segments if
824 maxpre = imin(dt->dt_nsegments, IOMMU_MAX_PRE_SEG);
825 presz = dt->dt_maxsize / maxpre;
826 for (i = 1; i < maxpre && totsz < IOMMU_MAX_PRE; i++) {
827 currsz = round_io_page(ulmin(presz, IOMMU_MAX_PRE - totsz));
828 error = iommu_dvma_valloc(dt, is, *mapp, currsz);
834 iommu_map_insq(is, *mapp);
840 iommu_dvmamap_destroy(bus_dma_tag_t dt, bus_dmamap_t map)
842 struct iommu_state *is = dt->dt_cookie;
844 iommu_dvma_vfree(is, map);
845 sparc64_dma_free_map(dt, map);
850 * Utility function to load a physical buffer. segp contains
851 * the starting segment on entrace, and the ending segment on exit.
854 iommu_dvmamap_load_phys(bus_dma_tag_t dt, bus_dmamap_t map, vm_paddr_t buf,
855 bus_size_t buflen, int flags, bus_dma_segment_t *segs, int *segp)
857 bus_addr_t amask, dvmaddr, dvmoffs;
858 bus_size_t sgsize, esize;
859 struct iommu_state *is;
862 int error, firstpg, sgcnt;
867 if ((map->dm_flags & DMF_LOADED) != 0) {
869 printf("%s: map still in use\n", __func__);
871 bus_dmamap_unload(dt, map);
875 * Make sure that the map is not on a queue so that the
876 * resource list may be safely accessed and modified without
877 * needing the lock to cover the whole operation.
880 iommu_map_remq(is, map);
883 amask = dt->dt_alignment - 1;
886 KASSERT(buflen != 0, ("%s: buflen == 0!", __func__));
887 if (buflen > dt->dt_maxsize)
891 segs = dt->dt_segments;
893 voffs = buf & IO_PAGE_MASK;
895 /* Try to find a slab that is large enough. */
896 error = iommu_dvma_vallocseg(dt, is, map, voffs, buflen, amask,
903 map->dm_flags &= ~DMF_STREAMED;
904 map->dm_flags |= iommu_use_streaming(is, map, buflen) != 0 ?
906 for (; buflen > 0; ) {
910 * Compute the segment size, and adjust counts.
912 sgsize = IO_PAGE_SIZE - ((u_long)buf & IO_PAGE_MASK);
919 dvmoffs = trunc_io_page(dvmaddr);
920 iommu_enter(is, dvmoffs, trunc_io_page(curaddr),
921 (map->dm_flags & DMF_STREAMED) != 0, flags);
922 if ((is->is_flags & IOMMU_FLUSH_CACHE) != 0) {
923 slot = IOTSBSLOT(dvmoffs);
924 if (buflen <= 0 || slot % 8 == 7)
925 IOMMU_WRITE8(is, is_iommu, IMR_CACHE_FLUSH,
926 is->is_ptsb + slot * 8);
930 * Chop the chunk up into segments of at most maxsegsz, but try
931 * to fill each segment as well as possible.
934 esize = ulmin(sgsize,
935 dt->dt_maxsegsz - segs[sgcnt].ds_len);
936 segs[sgcnt].ds_len += esize;
942 if (sgcnt >= dt->dt_nsegments)
945 * No extra alignment here - the common practice in
946 * the busdma code seems to be that only the first
947 * segment needs to satisfy the alignment constraints
948 * (and that only for bus_dmamem_alloc()ed maps).
949 * It is assumed that such tags have maxsegsize >=
952 esize = ulmin(sgsize, dt->dt_maxsegsz);
953 segs[sgcnt].ds_addr = dvmaddr;
954 segs[sgcnt].ds_len = esize;
966 * IOMMU DVMA operations, common to PCI and SBus
969 iommu_dvmamap_load_buffer(bus_dma_tag_t dt, bus_dmamap_t map, void *buf,
970 bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
973 bus_addr_t amask, dvmaddr, dvmoffs;
974 bus_size_t sgsize, esize;
975 struct iommu_state *is;
976 vm_offset_t vaddr, voffs;
978 int error, firstpg, sgcnt;
983 if ((map->dm_flags & DMF_LOADED) != 0) {
985 printf("%s: map still in use\n", __func__);
987 bus_dmamap_unload(dt, map);
991 * Make sure that the map is not on a queue so that the
992 * resource list may be safely accessed and modified without
993 * needing the lock to cover the whole operation.
996 iommu_map_remq(is, map);
999 amask = dt->dt_alignment - 1;
1002 KASSERT(buflen != 0, ("%s: buflen == 0!", __func__));
1003 if (buflen > dt->dt_maxsize)
1007 segs = dt->dt_segments;
1009 vaddr = (vm_offset_t)buf;
1010 voffs = vaddr & IO_PAGE_MASK;
1012 /* Try to find a slab that is large enough. */
1013 error = iommu_dvma_vallocseg(dt, is, map, voffs, buflen, amask,
1020 map->dm_flags &= ~DMF_STREAMED;
1021 map->dm_flags |= iommu_use_streaming(is, map, buflen) != 0 ?
1023 for (; buflen > 0; ) {
1025 * Get the physical address for this page.
1027 if (pmap == kernel_pmap)
1028 curaddr = pmap_kextract(vaddr);
1030 curaddr = pmap_extract(pmap, vaddr);
1033 * Compute the segment size, and adjust counts.
1035 sgsize = IO_PAGE_SIZE - ((u_long)vaddr & IO_PAGE_MASK);
1036 if (buflen < sgsize)
1042 dvmoffs = trunc_io_page(dvmaddr);
1043 iommu_enter(is, dvmoffs, trunc_io_page(curaddr),
1044 (map->dm_flags & DMF_STREAMED) != 0, flags);
1045 if ((is->is_flags & IOMMU_FLUSH_CACHE) != 0) {
1046 slot = IOTSBSLOT(dvmoffs);
1047 if (buflen <= 0 || slot % 8 == 7)
1048 IOMMU_WRITE8(is, is_iommu, IMR_CACHE_FLUSH,
1049 is->is_ptsb + slot * 8);
1053 * Chop the chunk up into segments of at most maxsegsz, but try
1054 * to fill each segment as well as possible.
1057 esize = ulmin(sgsize,
1058 dt->dt_maxsegsz - segs[sgcnt].ds_len);
1059 segs[sgcnt].ds_len += esize;
1063 while (sgsize > 0) {
1065 if (sgcnt >= dt->dt_nsegments)
1068 * No extra alignment here - the common practice in
1069 * the busdma code seems to be that only the first
1070 * segment needs to satisfy the alignment constraints
1071 * (and that only for bus_dmamem_alloc()ed maps).
1072 * It is assumed that such tags have maxsegsize >=
1075 esize = ulmin(sgsize, dt->dt_maxsegsz);
1076 segs[sgcnt].ds_addr = dvmaddr;
1077 segs[sgcnt].ds_len = esize;
1089 iommu_dvmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
1090 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
1094 static bus_dma_segment_t *
1095 iommu_dvmamap_complete(bus_dma_tag_t dt, bus_dmamap_t map,
1096 bus_dma_segment_t *segs, int nsegs, int error)
1098 struct iommu_state *is = dt->dt_cookie;
1101 iommu_map_insq(is, map);
1103 iommu_dvmamap_vunload(is, map);
1107 map->dm_flags |= DMF_LOADED;
1110 segs = dt->dt_segments;
1115 iommu_dvmamap_unload(bus_dma_tag_t dt, bus_dmamap_t map)
1117 struct iommu_state *is = dt->dt_cookie;
1119 if ((map->dm_flags & DMF_LOADED) == 0)
1122 iommu_dvmamap_vunload(is, map);
1123 iommu_map_insq(is, map);
1125 map->dm_flags &= ~DMF_LOADED;
1129 iommu_dvmamap_sync(bus_dma_tag_t dt, bus_dmamap_t map, bus_dmasync_op_t op)
1131 struct iommu_state *is = dt->dt_cookie;
1132 struct bus_dmamap_res *r;
1137 if ((map->dm_flags & DMF_LOADED) == 0)
1139 if ((map->dm_flags & DMF_STREAMED) != 0 &&
1140 ((op & BUS_DMASYNC_POSTREAD) != 0 ||
1141 (op & BUS_DMASYNC_PREWRITE) != 0)) {
1143 SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
1144 va = (vm_offset_t)BDR_START(r);
1147 * If we have a streaming buffer, flush it here
1151 if ((IOMMU_GET_TTE(is, va) &
1152 IOTTE_STREAM) != 0) {
1154 iommu_strbuf_flush(is, va);
1156 len -= ulmin(len, IO_PAGE_SIZE);
1161 iommu_strbuf_sync(is);
1164 if ((op & BUS_DMASYNC_PREWRITE) != 0)
1171 * Perform an IOMMU diagnostic access and print the tag belonging to va.
1174 iommu_diag(struct iommu_state *is, vm_offset_t va)
1179 if ((is->is_flags & IOMMU_FIRE) != 0)
1182 IOMMU_WRITE8(is, is_dva, 0, trunc_io_page(va));
1183 membar(StoreStore | StoreLoad);
1184 printf("%s: tte entry %#lx", __func__, IOMMU_GET_TTE(is, va));
1185 if (is->is_dtcmp != 0) {
1186 printf(", tag compare register is %#lx\n",
1187 IOMMU_READ8(is, is_dtcmp, 0));
1190 for (i = 0; i < 16; i++) {
1191 tag = IOMMU_READ8(is, is_dtag, i * 8);
1192 data = IOMMU_READ8(is, is_ddram, i * 8);
1193 printf("%s: tag %d: %#lx, vpn %#lx, err %lx; "
1194 "data %#lx, pa %#lx, v %d, c %d\n", __func__, i,
1195 tag, (tag & IOMMU_DTAG_VPNMASK) << IOMMU_DTAG_VPNSHIFT,
1196 (tag & IOMMU_DTAG_ERRMASK) >> IOMMU_DTAG_ERRSHIFT, data,
1197 (data & IOMMU_DDATA_PGMASK) << IOMMU_DDATA_PGSHIFT,
1198 (data & IOMMU_DDATA_V) != 0, (data & IOMMU_DDATA_C) != 0);
1202 #endif /* IOMMU_DIAG */
1204 struct bus_dma_methods iommu_dma_methods = {
1205 iommu_dvmamap_create,
1206 iommu_dvmamap_destroy,
1207 iommu_dvmamap_load_phys,
1208 iommu_dvmamap_load_buffer,
1209 iommu_dvmamap_waitok,
1210 iommu_dvmamap_complete,
1211 iommu_dvmamap_unload,
1213 iommu_dvmamem_alloc,