2 * Copyright (c) 1999, 2000 Matthew R. Green
3 * Copyright (c) 2001-2003 Thomas Moestl
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright (c) 1998 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
65 * Copyright (c) 1992, 1993
66 * The Regents of the University of California. All rights reserved.
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.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
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.
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
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
101 #include <sys/cdefs.h>
102 __FBSDID("$FreeBSD$");
105 * UltraSPARC IOMMU support; used by both the PCI and SBus code.
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
119 * - When running out of DVMA space, return EINPROGRESS in the non-
120 * BUS_DMA_NOWAIT case and delay the callback until sufficient space
122 * - Use the streaming cache unless BUS_DMA_COHERENT is specified.
125 #include "opt_iommu.h"
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>
139 #include <vm/vm_map.h>
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>
147 #include <sys/rman.h>
149 #include <machine/iommuvar.h>
154 #define IOMMU_MAX_PRE (32 * 1024)
155 #define IOMMU_MAX_PRE_SEG 3
157 /* Threshold for using the streaming buffer */
158 #define IOMMU_STREAM_THRESH 128
160 MALLOC_DEFINE(M_IOMMU, "dvmamem", "IOMMU DVMA Buffers");
162 static int iommu_strbuf_flush_sync(struct iommu_state *);
164 static void iommu_diag(struct iommu_state *, vm_offset_t va);
170 #define IOMMU_READ8(is, reg, off) \
171 bus_space_read_8((is)->is_bustag, (is)->is_bushandle, \
173 #define IOMMU_WRITE8(is, reg, off, v) \
174 bus_space_write_8((is)->is_bustag, (is)->is_bushandle, \
175 (is)->reg + (off), (v))
177 #define IOMMU_HAS_SB(is) \
178 ((is)->is_sb[0] != 0 || (is)->is_sb[1] != 0)
181 * Always overallocate one page; this is needed to handle alignment of the
182 * buffer, so it makes sense using a lazy allocation scheme.
184 #define IOMMU_SIZE_ROUNDUP(sz) \
185 (round_io_page(sz) + IO_PAGE_SIZE)
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)]
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)
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)
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)
210 /* Flush a page from the TLB. No locking required, since this is atomic. */
212 iommu_tlb_flush(struct iommu_state *is, bus_addr_t va)
215 IOMMU_WRITE8(is, is_iommu, IMR_FLUSH, va);
219 * Flush a page from the streaming buffer. No locking required, since this
223 iommu_strbuf_flushpg(struct iommu_state *is, bus_addr_t va)
227 for (i = 0; i < 2; i++)
228 if (is->is_sb[i] != 0)
229 IOMMU_WRITE8(is, is_sb[i], ISR_PGFLUSH, va);
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.
238 iommu_strbuf_flush(struct iommu_state *is, bus_addr_t va)
241 iommu_strbuf_flushpg(is, va);
244 /* Synchronize all outstanding flush operations. */
246 iommu_strbuf_sync(struct iommu_state *is)
250 iommu_strbuf_flush_sync(is);
253 /* LRU queue handling for lazy resource allocation. */
255 iommu_map_insq(struct iommu_state *is, bus_dmamap_t map)
259 if (!SLIST_EMPTY(&map->dm_reslist)) {
261 TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
262 TAILQ_INSERT_TAIL(&is->is_maplruq, map, dm_maplruq);
268 iommu_map_remq(struct iommu_state *is, bus_dmamap_t map)
273 TAILQ_REMOVE(&is->is_maplruq, map, dm_maplruq);
278 * initialise the UltraSPARC IOMMU (PCI or SBus):
279 * - allocate and setup the iotsb.
281 * - initialise the streaming buffers (if they exist)
282 * - create a private DVMA map.
285 iommu_init(const char *name, struct iommu_state *is, int tsbsize,
286 uint32_t iovabase, int resvpg)
296 * The sun4u iommu is part of the PCI or SBus controller so we
297 * will deal with it here..
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)).
304 is->is_cr = (tsbsize << IOMMUCR_TSBSZ_SHIFT) | IOMMUCR_EN;
305 is->is_tsbsize = tsbsize;
306 is->is_dvmabase = iovabase;
308 is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
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" : "");
317 * Set up resource mamangement.
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);
331 * Allocate memory for I/O page tables. They need to be
332 * physically contiguous.
334 is->is_tsb = contigmalloc(size, M_DEVBUF, M_NOWAIT, 0, ~0UL,
337 panic("%s: contigmalloc failed", __func__);
338 is->is_ptsb = pmap_kextract((vm_offset_t)is->is_tsb);
339 bzero(is->is_tsb, size);
342 * Initialize streaming buffer, if it is there.
344 if (IOMMU_HAS_SB(is)) {
346 * Find two 64-byte blocks in is_flush that are aligned on
347 * a 64-byte boundary for flushing.
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);
358 * Now actually start up the IOMMU.
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.
369 iommu_reset(struct iommu_state *is)
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);
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 ?
383 /* No streaming buffers? Disable them. */
384 if ((IOMMU_READ8(is, is_sb[i], ISR_CTL) &
392 * Enter a mapping into the TSB. No locking required, since each TSB slot is
393 * uniquely assigned to a single map.
396 iommu_enter(struct iommu_state *is, vm_offset_t va, vm_paddr_t pa,
397 int stream, int flags)
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));
406 tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
407 !(flags & BUS_DMA_NOCACHE), stream);
409 IOMMU_SET_TTE(is, va, tte);
410 iommu_tlb_flush(is, va);
419 * Remove mappings created by iommu_enter(). Flush the streaming buffer,
420 * but do not synchronize it. Returns whether a streaming buffer flush
424 iommu_remove(struct iommu_state *is, vm_offset_t va, vm_size_t len)
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));
437 va = trunc_io_page(va);
439 if ((IOMMU_GET_TTE(is, va) & IOTTE_STREAM) != 0) {
441 iommu_strbuf_flush(is, va);
443 len -= ulmin(len, IO_PAGE_SIZE);
444 IOMMU_SET_TTE(is, va, 0);
445 iommu_tlb_flush(is, va);
451 /* Decode an IOMMU fault for host bridge error handlers. */
453 iommu_decode_fault(struct iommu_state *is, vm_offset_t phys)
458 idx = phys - is->is_ptsb;
459 if (phys < is->is_ptsb ||
460 idx > (PAGE_SIZE << is->is_tsbsize))
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);
468 * A barrier operation which makes sure that all previous streaming buffer
469 * flushes complete before it returns.
472 iommu_strbuf_flush_sync(struct iommu_state *is)
474 struct timeval cur, end;
478 if (!IOMMU_HAS_SB(is))
482 * Streaming buffer flushes:
484 * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
485 * we're not on a cache line boundary (64-bits):
487 * 3 Store pointer to flag in flushsync
488 * 4 wait till flushsync becomes 0x1
490 * If it takes more than .5 sec, something went wrong.
492 *is->is_flushva[0] = 1;
493 *is->is_flushva[1] = 1;
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,
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.
510 end.tv_usec = 500000;
511 timevaladd(&end, &cur);
513 while ((!*is->is_flushva[0] || !*is->is_flushva[1]) &&
514 timevalcmp(&cur, &end, <=))
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]);
525 /* Determine whether we may enable streaming on a mapping. */
527 iommu_use_streaming(struct iommu_state *is, bus_dmamap_t map, bus_size_t size)
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.
536 return (size >= IOMMU_STREAM_THRESH && IOMMU_HAS_SB(is) &&
537 (map->dm_flags & DMF_COHERENT) == 0);
544 * Allocate DVMA virtual memory for a map. The map may not be on a queue,
545 * so that it can be freely modified.
548 iommu_dvma_valloc(bus_dma_tag_t t, struct iommu_state *is, bus_dmamap_t map,
551 struct resource *res;
552 struct bus_dmamap_res *bdr;
553 bus_size_t align, sgsize;
555 KASSERT(!map->dm_onq, ("%s: map on queue!", __func__));
556 if ((bdr = malloc(sizeof(*bdr), M_IOMMU, M_NOWAIT)) == NULL)
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
562 * Alignment to a page boundary is always enforced.
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);
579 SLIST_INSERT_HEAD(&map->dm_reslist, bdr, dr_link);
583 /* Unload the map and mark all resources as unused, but do not free them. */
585 iommu_dvmamap_vunload(struct iommu_state *is, bus_dmamap_t map)
587 struct bus_dmamap_res *r;
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);
596 iommu_strbuf_sync(is);
599 /* Free a DVMA virtual memory resource. */
601 iommu_dvma_vfree_res(bus_dmamap_t map, struct bus_dmamap_res *r)
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);
611 /* Free all DVMA virtual memory for a map. */
613 iommu_dvma_vfree(struct iommu_state *is, bus_dmamap_t map)
617 iommu_map_remq(is, map);
618 iommu_dvmamap_vunload(is, map);
620 while (!SLIST_EMPTY(&map->dm_reslist))
621 iommu_dvma_vfree_res(map, SLIST_FIRST(&map->dm_reslist));
624 /* Prune a map, freeing all unused DVMA resources. */
626 iommu_dvma_vprune(struct iommu_state *is, bus_dmamap_t map)
628 struct bus_dmamap_res *r, *n;
629 bus_size_t freed = 0;
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);
639 if (SLIST_EMPTY(&map->dm_reslist))
640 iommu_map_remq(is, map);
645 * Try to find a suitably-sized (and if requested, -aligned) slab of DVMA
646 * memory with IO page offset voffs.
649 iommu_dvma_vfindseg(bus_dmamap_t map, vm_offset_t voffs, bus_size_t size,
652 struct bus_dmamap_res *r;
653 bus_addr_t dvmaddr, dvmend;
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;
661 dvmend = dvmaddr + size;
662 if (dvmend <= BDR_END(r)) {
663 r->dr_used = dvmend - BDR_START(r);
671 * Try to find or allocate a slab of DVMA space; see above.
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)
677 bus_dmamap_t tm, last;
678 bus_addr_t dvmaddr, freed;
679 int error, complete = 0;
681 dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
683 /* Need to allocate. */
685 while ((error = iommu_dvma_valloc(dt, is, map,
686 voffs + size)) == ENOMEM && !complete) {
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.
697 last = TAILQ_LAST(&is->is_maplruq, iommu_maplruq_head);
699 tm = TAILQ_FIRST(&is->is_maplruq);
700 complete = tm == last;
703 freed += iommu_dvma_vprune(is, tm);
704 /* Move to the end. */
705 iommu_map_insq(is, tm);
706 } while (freed < size && !complete);
711 dvmaddr = iommu_dvma_vfindseg(map, voffs, size, amask);
712 KASSERT(dvmaddr != 0, ("%s: allocation failed unexpectedly!",
720 iommu_dvmamem_alloc(bus_dma_tag_t dt, void **vaddr, int flags,
723 struct iommu_state *is = dt->dt_cookie;
727 * XXX: This will break for 32 bit transfers on machines with more
728 * than is->is_pmaxaddr memory.
730 if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
733 if ((flags & BUS_DMA_NOWAIT) != 0)
737 if ((flags & BUS_DMA_ZERO) != 0)
740 if ((*vaddr = malloc(dt->dt_maxsize, M_IOMMU, mflags)) == NULL) {
742 sparc64_dma_free_map(dt, *mapp);
745 if ((flags & BUS_DMA_COHERENT) != 0)
746 (*mapp)->dm_flags |= DMF_COHERENT;
748 * Try to preallocate DVMA space. If this fails, it is retried at
751 iommu_dvma_valloc(dt, is, *mapp, IOMMU_SIZE_ROUNDUP(dt->dt_maxsize));
753 iommu_map_insq(is, *mapp);
759 iommu_dvmamem_free(bus_dma_tag_t dt, void *vaddr, bus_dmamap_t map)
761 struct iommu_state *is = dt->dt_cookie;
763 iommu_dvma_vfree(is, map);
764 sparc64_dma_free_map(dt, map);
765 free(vaddr, M_IOMMU);
769 iommu_dvmamap_create(bus_dma_tag_t dt, int flags, bus_dmamap_t *mapp)
771 struct iommu_state *is = dt->dt_cookie;
772 bus_size_t totsz, presz, currsz;
773 int error, i, maxpre;
775 if ((error = sparc64_dma_alloc_map(dt, mapp)) != 0)
777 if ((flags & BUS_DMA_COHERENT) != 0)
778 (*mapp)->dm_flags |= DMF_COHERENT;
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.
788 totsz = ulmin(IOMMU_SIZE_ROUNDUP(dt->dt_maxsize), IOMMU_MAX_PRE);
789 error = iommu_dvma_valloc(dt, is, *mapp, totsz);
793 * Try to be smart about preallocating some additional segments if
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,
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);
809 iommu_map_insq(is, *mapp);
815 iommu_dvmamap_destroy(bus_dma_tag_t dt, bus_dmamap_t map)
817 struct iommu_state *is = dt->dt_cookie;
819 iommu_dvma_vfree(is, map);
820 sparc64_dma_free_map(dt, map);
825 * IOMMU DVMA operations, common to PCI and SBus
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)
832 bus_addr_t amask, dvmaddr;
833 bus_size_t sgsize, esize;
834 vm_offset_t vaddr, voffs;
837 int error, firstpg, sgcnt;
839 KASSERT(buflen != 0, ("%s: buflen == 0!", __func__));
840 if (buflen > dt->dt_maxsize)
844 pmap = vmspace_pmap(td->td_proc->p_vmspace);
846 vaddr = (vm_offset_t)buf;
847 voffs = vaddr & IO_PAGE_MASK;
848 amask = align ? dt->dt_alignment - 1 : 0;
850 /* Try to find a slab that is large enough. */
851 error = iommu_dvma_vallocseg(dt, is, map, voffs, buflen, amask,
858 map->dm_flags &= ~DMF_STREAMED;
859 map->dm_flags |= iommu_use_streaming(is, map, buflen) != 0 ?
861 for (; buflen > 0; ) {
863 * Get the physical address for this page.
866 curaddr = pmap_extract(pmap, vaddr);
868 curaddr = pmap_kextract(vaddr);
871 * Compute the segment size, and adjust counts.
873 sgsize = IO_PAGE_SIZE - ((u_long)vaddr & IO_PAGE_MASK);
880 iommu_enter(is, trunc_io_page(dvmaddr), trunc_io_page(curaddr),
881 (map->dm_flags & DMF_STREAMED) != 0, flags);
884 * Chop the chunk up into segments of at most maxsegsz, but try
885 * to fill each segment as well as possible.
888 esize = ulmin(sgsize,
889 dt->dt_maxsegsz - segs[sgcnt].ds_len);
890 segs[sgcnt].ds_len += esize;
896 if (sgcnt >= dt->dt_nsegments)
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 >=
906 esize = ulmin(sgsize, dt->dt_maxsegsz);
907 segs[sgcnt].ds_addr = dvmaddr;
908 segs[sgcnt].ds_len = esize;
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,
924 struct iommu_state *is = dt->dt_cookie;
927 if ((map->dm_flags & DMF_LOADED) != 0) {
929 printf("%s: map still in use\n", __func__);
931 bus_dmamap_unload(dt, map);
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.
940 iommu_map_remq(is, map);
943 error = iommu_dvmamap_load_buffer(dt, is, map, buf, buflen, NULL,
944 flags, dt->dt_segments, &seg, 1);
947 iommu_map_insq(is, map);
949 iommu_dvmamap_vunload(is, map);
951 (*cb)(cba, dt->dt_segments, 0, error);
954 map->dm_flags |= DMF_LOADED;
955 (*cb)(cba, dt->dt_segments, seg + 1, 0);
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)
965 struct iommu_state *is = dt->dt_cookie;
967 int error = 0, first = 1, nsegs = -1;
971 if ((map->dm_flags & DMF_LOADED) != 0) {
973 printf("%s: map still in use\n", __func__);
975 bus_dmamap_unload(dt, map);
979 iommu_map_remq(is, map);
982 if (m0->m_pkthdr.len <= dt->dt_maxsize) {
983 for (m = m0; m != NULL && error == 0; m = m->m_next) {
986 error = iommu_dvmamap_load_buffer(dt, is, map,
987 m->m_data, m->m_len, NULL, flags, dt->dt_segments,
995 iommu_map_insq(is, map);
997 iommu_dvmamap_vunload(is, map);
999 /* force "no valid mappings" in callback */
1000 (*cb)(cba, dt->dt_segments, 0, 0, error);
1003 map->dm_flags |= DMF_LOADED;
1004 (*cb)(cba, dt->dt_segments, nsegs + 1, m0->m_pkthdr.len, 0);
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)
1013 struct iommu_state *is = dt->dt_cookie;
1015 int error = 0, first = 1;
1020 if ((map->dm_flags & DMF_LOADED) != 0) {
1022 printf("%s: map still in use\n", __func__);
1024 bus_dmamap_unload(dt, map);
1028 iommu_map_remq(is, map);
1031 if (m0->m_pkthdr.len <= dt->dt_maxsize) {
1032 for (m = m0; m != NULL && error == 0; m = m->m_next) {
1035 error = iommu_dvmamap_load_buffer(dt, is, map,
1036 m->m_data, m->m_len, NULL, flags, segs,
1044 iommu_map_insq(is, map);
1046 iommu_dvmamap_vunload(is, map);
1050 map->dm_flags |= DMF_LOADED;
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)
1060 struct iommu_state *is = dt->dt_cookie;
1062 struct thread *td = NULL;
1063 bus_size_t minlen, resid;
1064 int nsegs = -1, error = 0, first = 1, i;
1066 if ((map->dm_flags & DMF_LOADED) != 0) {
1068 printf("%s: map still in use\n", __func__);
1070 bus_dmamap_unload(dt, map);
1074 iommu_map_remq(is, map);
1077 resid = uio->uio_resid;
1080 if (uio->uio_segflg == UIO_USERSPACE) {
1083 ("%s: USERSPACE but no proc", __func__));
1086 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
1088 * Now at the first iovec to load. Load each iovec
1089 * until we have exhausted the residual count.
1091 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
1095 error = iommu_dvmamap_load_buffer(dt, is, map,
1096 iov[i].iov_base, minlen, td, flags, dt->dt_segments,
1104 iommu_map_insq(is, map);
1106 iommu_dvmamap_vunload(is, map);
1108 /* force "no valid mappings" in callback */
1109 (*cb)(cba, dt->dt_segments, 0, 0, error);
1112 map->dm_flags |= DMF_LOADED;
1113 (*cb)(cba, dt->dt_segments, nsegs + 1, uio->uio_resid, 0);
1119 iommu_dvmamap_unload(bus_dma_tag_t dt, bus_dmamap_t map)
1121 struct iommu_state *is = dt->dt_cookie;
1123 if ((map->dm_flags & DMF_LOADED) == 0)
1126 iommu_dvmamap_vunload(is, map);
1127 iommu_map_insq(is, map);
1129 map->dm_flags &= ~DMF_LOADED;
1133 iommu_dvmamap_sync(bus_dma_tag_t dt, bus_dmamap_t map, bus_dmasync_op_t op)
1135 struct iommu_state *is = dt->dt_cookie;
1136 struct bus_dmamap_res *r;
1141 if ((map->dm_flags & DMF_LOADED) == 0)
1143 /* XXX This is probably bogus. */
1144 if ((op & BUS_DMASYNC_PREREAD) != 0)
1146 if ((map->dm_flags & DMF_STREAMED) != 0 &&
1147 ((op & BUS_DMASYNC_POSTREAD) != 0 ||
1148 (op & BUS_DMASYNC_PREWRITE) != 0)) {
1150 SLIST_FOREACH(r, &map->dm_reslist, dr_link) {
1151 va = (vm_offset_t)BDR_START(r);
1154 * If we have a streaming buffer, flush it here
1158 if ((IOMMU_GET_TTE(is, va) &
1159 IOTTE_STREAM) != 0) {
1161 iommu_strbuf_flush(is, va);
1163 len -= ulmin(len, IO_PAGE_SIZE);
1168 iommu_strbuf_sync(is);
1171 if ((op & BUS_DMASYNC_PREWRITE) != 0)
1178 * Perform an IOMMU diagnostic access and print the tag belonging to va.
1181 iommu_diag(struct iommu_state *is, vm_offset_t va)
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));
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);
1207 #endif /* IOMMU_DIAG */
1209 struct bus_dma_methods iommu_dma_methods = {
1210 iommu_dvmamap_create,
1211 iommu_dvmamap_destroy,
1213 iommu_dvmamap_load_mbuf,
1214 iommu_dvmamap_load_mbuf_sg,
1215 iommu_dvmamap_load_uio,
1216 iommu_dvmamap_unload,
1218 iommu_dvmamem_alloc,