2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #define RB_AUGMENT(entry) iommu_gas_augment_entry(entry)
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
46 #include <sys/rwlock.h>
47 #include <sys/memdesc.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
64 #include <dev/iommu/iommu.h>
65 #include <dev/iommu/iommu_gas.h>
66 #include <dev/iommu/iommu_msi.h>
67 #include <machine/atomic.h>
68 #include <machine/bus.h>
69 #include <machine/md_var.h>
70 #include <machine/iommu.h>
71 #include <dev/iommu/busdma_iommu.h>
74 * Guest Address Space management.
77 static uma_zone_t iommu_map_entry_zone;
80 static int iommu_check_free;
87 iommu_map_entry_zone = uma_zcreate("IOMMU_MAP_ENTRY",
88 sizeof(struct iommu_map_entry), NULL, NULL,
89 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NODUMP);
91 SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
93 struct iommu_map_entry *
94 iommu_gas_alloc_entry(struct iommu_domain *domain, u_int flags)
96 struct iommu_map_entry *res;
98 KASSERT((flags & ~(IOMMU_PGF_WAITOK)) == 0,
99 ("unsupported flags %x", flags));
101 res = uma_zalloc(iommu_map_entry_zone, ((flags & IOMMU_PGF_WAITOK) !=
102 0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
104 res->domain = domain;
105 atomic_add_int(&domain->entries_cnt, 1);
111 iommu_gas_free_entry(struct iommu_domain *domain, struct iommu_map_entry *entry)
114 KASSERT(domain == entry->domain,
115 ("mismatched free domain %p entry %p entry->domain %p", domain,
116 entry, entry->domain));
117 atomic_subtract_int(&domain->entries_cnt, 1);
118 uma_zfree(iommu_map_entry_zone, entry);
122 iommu_gas_cmp_entries(struct iommu_map_entry *a, struct iommu_map_entry *b)
125 /* Last entry have zero size, so <= */
126 KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
127 a, (uintmax_t)a->start, (uintmax_t)a->end));
128 KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
129 b, (uintmax_t)b->start, (uintmax_t)b->end));
130 KASSERT(a->end <= b->start || b->end <= a->start ||
131 a->end == a->start || b->end == b->start,
132 ("overlapping entries %p (%jx, %jx) %p (%jx, %jx)",
133 a, (uintmax_t)a->start, (uintmax_t)a->end,
134 b, (uintmax_t)b->start, (uintmax_t)b->end));
138 else if (b->end < a->end)
144 iommu_gas_augment_entry(struct iommu_map_entry *entry)
146 struct iommu_map_entry *child;
147 iommu_gaddr_t free_down;
150 if ((child = RB_LEFT(entry, rb_entry)) != NULL) {
151 free_down = MAX(free_down, child->free_down);
152 free_down = MAX(free_down, entry->start - child->last);
153 entry->first = child->first;
155 entry->first = entry->start;
157 if ((child = RB_RIGHT(entry, rb_entry)) != NULL) {
158 free_down = MAX(free_down, child->free_down);
159 free_down = MAX(free_down, child->first - entry->end);
160 entry->last = child->last;
162 entry->last = entry->end;
163 entry->free_down = free_down;
166 RB_GENERATE(iommu_gas_entries_tree, iommu_map_entry, rb_entry,
167 iommu_gas_cmp_entries);
171 iommu_gas_check_free(struct iommu_domain *domain)
173 struct iommu_map_entry *entry, *l, *r;
176 RB_FOREACH(entry, iommu_gas_entries_tree, &domain->rb_root) {
177 KASSERT(domain == entry->domain,
178 ("mismatched free domain %p entry %p entry->domain %p",
179 domain, entry, entry->domain));
180 l = RB_LEFT(entry, rb_entry);
181 r = RB_RIGHT(entry, rb_entry);
184 v = MAX(v, l->free_down);
185 v = MAX(v, entry->start - l->last);
188 v = MAX(v, r->free_down);
189 v = MAX(v, r->first - entry->end);
191 MPASS(entry->free_down == v);
197 iommu_gas_rb_insert(struct iommu_domain *domain, struct iommu_map_entry *entry)
199 struct iommu_map_entry *found;
201 found = RB_INSERT(iommu_gas_entries_tree,
202 &domain->rb_root, entry);
203 return (found == NULL);
207 iommu_gas_rb_remove(struct iommu_domain *domain, struct iommu_map_entry *entry)
210 RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
213 struct iommu_domain *
214 iommu_get_ctx_domain(struct iommu_ctx *ctx)
217 return (ctx->domain);
221 iommu_gas_init_domain(struct iommu_domain *domain)
223 struct iommu_map_entry *begin, *end;
225 begin = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
226 end = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
228 IOMMU_DOMAIN_LOCK(domain);
229 KASSERT(domain->entries_cnt == 2, ("dirty domain %p", domain));
230 KASSERT(RB_EMPTY(&domain->rb_root),
231 ("non-empty entries %p", domain));
234 begin->end = IOMMU_PAGE_SIZE;
235 begin->flags = IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED;
236 iommu_gas_rb_insert(domain, begin);
238 end->start = domain->end;
239 end->end = domain->end;
240 end->flags = IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED;
241 iommu_gas_rb_insert(domain, end);
243 domain->first_place = begin;
244 domain->last_place = end;
245 domain->flags |= IOMMU_DOMAIN_GAS_INITED;
246 IOMMU_DOMAIN_UNLOCK(domain);
250 iommu_gas_fini_domain(struct iommu_domain *domain)
252 struct iommu_map_entry *entry, *entry1;
254 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
255 KASSERT(domain->entries_cnt == 2,
256 ("domain still in use %p", domain));
258 entry = RB_MIN(iommu_gas_entries_tree, &domain->rb_root);
259 KASSERT(entry->start == 0, ("start entry start %p", domain));
260 KASSERT(entry->end == IOMMU_PAGE_SIZE, ("start entry end %p", domain));
261 KASSERT(entry->flags ==
262 (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
263 ("start entry flags %p", domain));
264 RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
265 iommu_gas_free_entry(domain, entry);
267 entry = RB_MAX(iommu_gas_entries_tree, &domain->rb_root);
268 KASSERT(entry->start == domain->end, ("end entry start %p", domain));
269 KASSERT(entry->end == domain->end, ("end entry end %p", domain));
270 KASSERT(entry->flags ==
271 (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
272 ("end entry flags %p", domain));
273 RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
274 iommu_gas_free_entry(domain, entry);
276 RB_FOREACH_SAFE(entry, iommu_gas_entries_tree, &domain->rb_root,
278 KASSERT((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0,
279 ("non-RMRR entry left %p", domain));
280 RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root,
282 iommu_gas_free_entry(domain, entry);
286 struct iommu_gas_match_args {
287 struct iommu_domain *domain;
290 const struct bus_dma_tag_common *common;
292 struct iommu_map_entry *entry;
296 * The interval [beg, end) is a free interval between two iommu_map_entries.
297 * maxaddr is an upper bound on addresses that can be allocated. Try to
298 * allocate space in the free interval, subject to the conditions expressed
299 * by a, and return 'true' if and only if the allocation attempt succeeds.
302 iommu_gas_match_one(struct iommu_gas_match_args *a, iommu_gaddr_t beg,
303 iommu_gaddr_t end, iommu_gaddr_t maxaddr)
305 iommu_gaddr_t bs, start;
307 a->entry->start = roundup2(beg + IOMMU_PAGE_SIZE,
308 a->common->alignment);
309 if (a->entry->start + a->size > maxaddr)
312 /* IOMMU_PAGE_SIZE to create gap after new entry. */
313 if (a->entry->start < beg + IOMMU_PAGE_SIZE ||
314 a->entry->start + a->size + a->offset + IOMMU_PAGE_SIZE > end)
317 /* No boundary crossing. */
318 if (iommu_test_boundary(a->entry->start + a->offset, a->size,
319 a->common->boundary))
323 * The start + offset to start + offset + size region crosses
324 * the boundary. Check if there is enough space after the
325 * next boundary after the beg.
327 bs = rounddown2(a->entry->start + a->offset + a->common->boundary,
328 a->common->boundary);
329 start = roundup2(bs, a->common->alignment);
330 /* IOMMU_PAGE_SIZE to create gap after new entry. */
331 if (start + a->offset + a->size + IOMMU_PAGE_SIZE <= end &&
332 start + a->offset + a->size <= maxaddr &&
333 iommu_test_boundary(start + a->offset, a->size,
334 a->common->boundary)) {
335 a->entry->start = start;
340 * Not enough space to align at the requested boundary, or
341 * boundary is smaller than the size, but allowed to split.
342 * We already checked that start + size does not overlap maxaddr.
344 * XXXKIB. It is possible that bs is exactly at the start of
345 * the next entry, then we do not have gap. Ignore for now.
347 if ((a->gas_flags & IOMMU_MF_CANSPLIT) != 0) {
348 a->size = bs - a->entry->start;
356 iommu_gas_match_insert(struct iommu_gas_match_args *a)
361 * The prev->end is always aligned on the page size, which
362 * causes page alignment for the entry->start too. The size
363 * is checked to be multiple of the page size.
365 * The page sized gap is created between consequent
366 * allocations to ensure that out-of-bounds accesses fault.
368 a->entry->end = a->entry->start + a->size;
370 found = iommu_gas_rb_insert(a->domain, a->entry);
371 KASSERT(found, ("found dup %p start %jx size %jx",
372 a->domain, (uintmax_t)a->entry->start, (uintmax_t)a->size));
373 a->entry->flags = IOMMU_MAP_ENTRY_MAP;
377 iommu_gas_lowermatch(struct iommu_gas_match_args *a, struct iommu_map_entry *entry)
379 struct iommu_map_entry *child;
381 child = RB_RIGHT(entry, rb_entry);
382 if (child != NULL && entry->end < a->common->lowaddr &&
383 iommu_gas_match_one(a, entry->end, child->first,
384 a->common->lowaddr)) {
385 iommu_gas_match_insert(a);
388 if (entry->free_down < a->size + a->offset + IOMMU_PAGE_SIZE)
390 if (entry->first >= a->common->lowaddr)
392 child = RB_LEFT(entry, rb_entry);
393 if (child != NULL && 0 == iommu_gas_lowermatch(a, child))
395 if (child != NULL && child->last < a->common->lowaddr &&
396 iommu_gas_match_one(a, child->last, entry->start,
397 a->common->lowaddr)) {
398 iommu_gas_match_insert(a);
401 child = RB_RIGHT(entry, rb_entry);
402 if (child != NULL && 0 == iommu_gas_lowermatch(a, child))
408 iommu_gas_uppermatch(struct iommu_gas_match_args *a, struct iommu_map_entry *entry)
410 struct iommu_map_entry *child;
412 if (entry->free_down < a->size + a->offset + IOMMU_PAGE_SIZE)
414 if (entry->last < a->common->highaddr)
416 child = RB_LEFT(entry, rb_entry);
417 if (child != NULL && 0 == iommu_gas_uppermatch(a, child))
419 if (child != NULL && child->last >= a->common->highaddr &&
420 iommu_gas_match_one(a, child->last, entry->start,
422 iommu_gas_match_insert(a);
425 child = RB_RIGHT(entry, rb_entry);
426 if (child != NULL && entry->end >= a->common->highaddr &&
427 iommu_gas_match_one(a, entry->end, child->first,
429 iommu_gas_match_insert(a);
432 if (child != NULL && 0 == iommu_gas_uppermatch(a, child))
438 iommu_gas_find_space(struct iommu_domain *domain,
439 const struct bus_dma_tag_common *common, iommu_gaddr_t size,
440 int offset, u_int flags, struct iommu_map_entry *entry)
442 struct iommu_gas_match_args a;
445 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
446 KASSERT(entry->flags == 0, ("dirty entry %p %p", domain, entry));
447 KASSERT((size & IOMMU_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
456 /* Handle lower region. */
457 if (common->lowaddr > 0) {
458 error = iommu_gas_lowermatch(&a,
459 RB_ROOT(&domain->rb_root));
462 KASSERT(error == ENOMEM,
463 ("error %d from iommu_gas_lowermatch", error));
465 /* Handle upper region. */
466 if (common->highaddr >= domain->end)
468 error = iommu_gas_uppermatch(&a, RB_ROOT(&domain->rb_root));
469 KASSERT(error == ENOMEM,
470 ("error %d from iommu_gas_uppermatch", error));
475 iommu_gas_alloc_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
478 struct iommu_map_entry *next, *prev;
481 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
483 if ((entry->start & IOMMU_PAGE_MASK) != 0 ||
484 (entry->end & IOMMU_PAGE_MASK) != 0)
486 if (entry->start >= entry->end)
488 if (entry->end >= domain->end)
491 next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, entry);
492 KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
493 (uintmax_t)entry->start));
494 prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
495 /* prev could be NULL */
498 * Adapt to broken BIOSes which specify overlapping RMRR
501 * XXXKIB: this does not handle a case when prev or next
502 * entries are completely covered by the current one, which
505 if (prev != NULL && prev->end > entry->start &&
506 (prev->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
507 if ((flags & IOMMU_MF_RMRR) == 0 ||
508 (prev->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
510 entry->start = prev->end;
512 if (next->start < entry->end &&
513 (next->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
514 if ((flags & IOMMU_MF_RMRR) == 0 ||
515 (next->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
517 entry->end = next->start;
519 if (entry->end == entry->start)
522 if (prev != NULL && prev->end > entry->start) {
523 /* This assumes that prev is the placeholder entry. */
524 iommu_gas_rb_remove(domain, prev);
527 if (next->start < entry->end) {
528 iommu_gas_rb_remove(domain, next);
532 found = iommu_gas_rb_insert(domain, entry);
533 KASSERT(found, ("found RMRR dup %p start %jx end %jx",
534 domain, (uintmax_t)entry->start, (uintmax_t)entry->end));
535 if ((flags & IOMMU_MF_RMRR) != 0)
536 entry->flags = IOMMU_MAP_ENTRY_RMRR;
539 struct iommu_map_entry *ip, *in;
540 ip = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
541 in = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
542 KASSERT(prev == NULL || ip == prev,
543 ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
544 entry, entry->start, entry->end, prev,
545 prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
546 ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
547 KASSERT(next == NULL || in == next,
548 ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
549 entry, entry->start, entry->end, next,
550 next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
551 in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
558 iommu_gas_free_space(struct iommu_domain *domain, struct iommu_map_entry *entry)
561 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
562 KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
563 IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_MAP,
564 ("permanent entry %p %p", domain, entry));
566 iommu_gas_rb_remove(domain, entry);
567 entry->flags &= ~IOMMU_MAP_ENTRY_MAP;
569 if (iommu_check_free)
570 iommu_gas_check_free(domain);
575 iommu_gas_free_region(struct iommu_domain *domain, struct iommu_map_entry *entry)
577 struct iommu_map_entry *next, *prev;
579 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
580 KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
581 IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_RMRR,
582 ("non-RMRR entry %p %p", domain, entry));
584 prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
585 next = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
586 iommu_gas_rb_remove(domain, entry);
587 entry->flags &= ~IOMMU_MAP_ENTRY_RMRR;
590 iommu_gas_rb_insert(domain, domain->first_place);
592 iommu_gas_rb_insert(domain, domain->last_place);
596 iommu_gas_map(struct iommu_domain *domain,
597 const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
598 u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res)
600 struct iommu_map_entry *entry;
603 KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_CANSPLIT)) == 0,
604 ("invalid flags 0x%x", flags));
606 entry = iommu_gas_alloc_entry(domain,
607 (flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0);
610 IOMMU_DOMAIN_LOCK(domain);
611 error = iommu_gas_find_space(domain, common, size, offset, flags,
613 if (error == ENOMEM) {
614 IOMMU_DOMAIN_UNLOCK(domain);
615 iommu_gas_free_entry(domain, entry);
619 if (iommu_check_free)
620 iommu_gas_check_free(domain);
623 ("unexpected error %d from iommu_gas_find_entry", error));
624 KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
625 (uintmax_t)entry->end, (uintmax_t)domain->end));
626 entry->flags |= eflags;
627 IOMMU_DOMAIN_UNLOCK(domain);
629 error = domain->ops->map(domain, entry->start,
630 entry->end - entry->start, ma, eflags,
631 ((flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0));
632 if (error == ENOMEM) {
633 iommu_domain_unload_entry(entry, true);
637 ("unexpected error %d from domain_map_buf", error));
644 iommu_gas_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
645 u_int eflags, u_int flags, vm_page_t *ma)
650 KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
651 entry, entry->flags));
652 KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_RMRR)) == 0,
653 ("invalid flags 0x%x", flags));
655 start = entry->start;
656 IOMMU_DOMAIN_LOCK(domain);
657 error = iommu_gas_alloc_region(domain, entry, flags);
659 IOMMU_DOMAIN_UNLOCK(domain);
662 entry->flags |= eflags;
663 IOMMU_DOMAIN_UNLOCK(domain);
664 if (entry->end == entry->start)
667 error = domain->ops->map(domain, entry->start,
668 entry->end - entry->start, ma + OFF_TO_IDX(start - entry->start),
669 eflags, ((flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0));
670 if (error == ENOMEM) {
671 iommu_domain_unload_entry(entry, false);
675 ("unexpected error %d from domain_map_buf", error));
681 iommu_gas_reserve_region_locked(struct iommu_domain *domain,
682 iommu_gaddr_t start, iommu_gaddr_t end, struct iommu_map_entry *entry)
686 IOMMU_DOMAIN_ASSERT_LOCKED(domain);
688 entry->start = start;
690 error = iommu_gas_alloc_region(domain, entry, IOMMU_MF_CANWAIT);
692 entry->flags |= IOMMU_MAP_ENTRY_UNMAPPED;
697 iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
698 iommu_gaddr_t end, struct iommu_map_entry **entry0)
700 struct iommu_map_entry *entry;
703 entry = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
704 IOMMU_DOMAIN_LOCK(domain);
705 error = iommu_gas_reserve_region_locked(domain, start, end, entry);
706 IOMMU_DOMAIN_UNLOCK(domain);
708 iommu_gas_free_entry(domain, entry);
709 else if (entry0 != NULL)
715 * As in iommu_gas_reserve_region, reserve [start, end), but allow for existing
719 iommu_gas_reserve_region_extend(struct iommu_domain *domain,
720 iommu_gaddr_t start, iommu_gaddr_t end)
722 struct iommu_map_entry *entry, *next, *prev, key = {};
723 iommu_gaddr_t entry_start, entry_end;
728 end = ummin(end, domain->end);
729 while (start < end) {
730 /* Preallocate an entry. */
732 entry = iommu_gas_alloc_entry(domain,
734 /* Calculate the free region from here to the next entry. */
735 key.start = key.end = start;
736 IOMMU_DOMAIN_LOCK(domain);
737 next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, &key);
738 KASSERT(next != NULL, ("domain %p with end %#jx has no entry "
739 "after %#jx", domain, (uintmax_t)domain->end,
741 entry_end = ummin(end, next->start);
742 prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
744 entry_start = ummax(start, prev->end);
748 /* Reserve the region if non-empty. */
749 if (entry_start != entry_end) {
750 error = iommu_gas_reserve_region_locked(domain,
751 entry_start, entry_end, entry);
756 IOMMU_DOMAIN_UNLOCK(domain);
758 /* Release a preallocated entry if it was not used. */
760 iommu_gas_free_entry(domain, entry);
764 struct iommu_map_entry *
765 iommu_map_alloc_entry(struct iommu_domain *domain, u_int flags)
767 struct iommu_map_entry *res;
769 res = iommu_gas_alloc_entry(domain, flags);
775 iommu_map_free_entry(struct iommu_domain *domain, struct iommu_map_entry *entry)
778 iommu_gas_free_entry(domain, entry);
782 iommu_map(struct iommu_domain *domain,
783 const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
784 u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res)
788 error = iommu_gas_map(domain, common, size, offset, eflags, flags,
795 iommu_unmap_msi(struct iommu_ctx *ctx)
797 struct iommu_map_entry *entry;
798 struct iommu_domain *domain;
800 domain = ctx->domain;
801 entry = domain->msi_entry;
805 domain->ops->unmap(domain, entry->start, entry->end -
806 entry->start, IOMMU_PGF_WAITOK);
808 IOMMU_DOMAIN_LOCK(domain);
809 iommu_gas_free_space(domain, entry);
810 IOMMU_DOMAIN_UNLOCK(domain);
812 iommu_gas_free_entry(domain, entry);
814 domain->msi_entry = NULL;
815 domain->msi_base = 0;
816 domain->msi_phys = 0;
820 iommu_map_msi(struct iommu_ctx *ctx, iommu_gaddr_t size, int offset,
821 u_int eflags, u_int flags, vm_page_t *ma)
823 struct iommu_domain *domain;
824 struct iommu_map_entry *entry;
828 domain = ctx->domain;
830 /* Check if there is already an MSI page allocated */
831 IOMMU_DOMAIN_LOCK(domain);
832 entry = domain->msi_entry;
833 IOMMU_DOMAIN_UNLOCK(domain);
836 error = iommu_gas_map(domain, &ctx->tag->common, size, offset,
837 eflags, flags, ma, &entry);
838 IOMMU_DOMAIN_LOCK(domain);
840 if (domain->msi_entry == NULL) {
841 MPASS(domain->msi_base == 0);
842 MPASS(domain->msi_phys == 0);
844 domain->msi_entry = entry;
845 domain->msi_base = entry->start;
846 domain->msi_phys = VM_PAGE_TO_PHYS(ma[0]);
849 * We lost the race and already have an
850 * MSI page allocated. Free the unneeded entry.
852 iommu_gas_free_entry(domain, entry);
854 } else if (domain->msi_entry != NULL) {
856 * The allocation failed, but another succeeded.
857 * Return success as there is a valid MSI page.
861 IOMMU_DOMAIN_UNLOCK(domain);
868 iommu_translate_msi(struct iommu_domain *domain, uint64_t *addr)
871 *addr = (*addr - domain->msi_phys) + domain->msi_base;
873 KASSERT(*addr >= domain->msi_entry->start,
874 ("%s: Address is below the MSI entry start address (%jx < %jx)",
875 __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->start));
877 KASSERT(*addr + sizeof(*addr) <= domain->msi_entry->end,
878 ("%s: Address is above the MSI entry end address (%jx < %jx)",
879 __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->end));
883 iommu_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
884 u_int eflags, u_int flags, vm_page_t *ma)
888 error = iommu_gas_map_region(domain, entry, eflags, flags, ma);
893 SYSCTL_NODE(_hw, OID_AUTO, iommu, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, "");
896 SYSCTL_INT(_hw_iommu, OID_AUTO, check_free, CTLFLAG_RWTUN,
897 &iommu_check_free, 0,
898 "Check the GPA RBtree for free_down and free_after validity");