xref: /freebsd/sys/dev/iommu/iommu_gas.c (revision c0047e7c)
13024e8afSRuslan Bukin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33024e8afSRuslan Bukin  *
43024e8afSRuslan Bukin  * Copyright (c) 2013 The FreeBSD Foundation
53024e8afSRuslan Bukin  *
63024e8afSRuslan Bukin  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
73024e8afSRuslan Bukin  * under sponsorship from the FreeBSD Foundation.
83024e8afSRuslan Bukin  *
93024e8afSRuslan Bukin  * Redistribution and use in source and binary forms, with or without
103024e8afSRuslan Bukin  * modification, are permitted provided that the following conditions
113024e8afSRuslan Bukin  * are met:
123024e8afSRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
133024e8afSRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
143024e8afSRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
153024e8afSRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
163024e8afSRuslan Bukin  *    documentation and/or other materials provided with the distribution.
173024e8afSRuslan Bukin  *
183024e8afSRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
193024e8afSRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203024e8afSRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
213024e8afSRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
223024e8afSRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
233024e8afSRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
243024e8afSRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
253024e8afSRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263024e8afSRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
273024e8afSRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
283024e8afSRuslan Bukin  * SUCH DAMAGE.
293024e8afSRuslan Bukin  */
303024e8afSRuslan Bukin 
31b16f993eSDoug Moore #define	RB_AUGMENT_CHECK(entry) iommu_gas_augment_entry(entry)
323024e8afSRuslan Bukin 
333024e8afSRuslan Bukin #include <sys/param.h>
343024e8afSRuslan Bukin #include <sys/systm.h>
353024e8afSRuslan Bukin #include <sys/malloc.h>
363024e8afSRuslan Bukin #include <sys/bus.h>
373024e8afSRuslan Bukin #include <sys/interrupt.h>
383024e8afSRuslan Bukin #include <sys/kernel.h>
393024e8afSRuslan Bukin #include <sys/ktr.h>
403024e8afSRuslan Bukin #include <sys/lock.h>
413024e8afSRuslan Bukin #include <sys/proc.h>
423024e8afSRuslan Bukin #include <sys/rwlock.h>
433024e8afSRuslan Bukin #include <sys/memdesc.h>
443024e8afSRuslan Bukin #include <sys/mutex.h>
453024e8afSRuslan Bukin #include <sys/sysctl.h>
463024e8afSRuslan Bukin #include <sys/rman.h>
473024e8afSRuslan Bukin #include <sys/taskqueue.h>
483024e8afSRuslan Bukin #include <sys/tree.h>
493024e8afSRuslan Bukin #include <sys/uio.h>
503024e8afSRuslan Bukin #include <sys/vmem.h>
513024e8afSRuslan Bukin #include <vm/vm.h>
523024e8afSRuslan Bukin #include <vm/vm_extern.h>
533024e8afSRuslan Bukin #include <vm/vm_kern.h>
543024e8afSRuslan Bukin #include <vm/vm_object.h>
553024e8afSRuslan Bukin #include <vm/vm_page.h>
563024e8afSRuslan Bukin #include <vm/vm_map.h>
573024e8afSRuslan Bukin #include <vm/uma.h>
58c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h>
59c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h>
60c8597a1fSRuslan Bukin #include <dev/iommu/iommu.h>
61f23f7d3aSRuslan Bukin #include <dev/iommu/iommu_gas.h>
62e707c8beSRuslan Bukin #include <dev/iommu/iommu_msi.h>
633024e8afSRuslan Bukin #include <machine/atomic.h>
643024e8afSRuslan Bukin #include <machine/bus.h>
653024e8afSRuslan Bukin #include <machine/md_var.h>
66c4cd6990SRuslan Bukin #include <machine/iommu.h>
67c8597a1fSRuslan Bukin #include <dev/iommu/busdma_iommu.h>
683024e8afSRuslan Bukin 
693024e8afSRuslan Bukin /*
703024e8afSRuslan Bukin  * Guest Address Space management.
713024e8afSRuslan Bukin  */
723024e8afSRuslan Bukin 
733024e8afSRuslan Bukin static uma_zone_t iommu_map_entry_zone;
743024e8afSRuslan Bukin 
759c843a40SRuslan Bukin #ifdef INVARIANTS
769c843a40SRuslan Bukin static int iommu_check_free;
779c843a40SRuslan Bukin #endif
789c843a40SRuslan Bukin 
793024e8afSRuslan Bukin static void
intel_gas_init(void)803024e8afSRuslan Bukin intel_gas_init(void)
813024e8afSRuslan Bukin {
823024e8afSRuslan Bukin 
833024e8afSRuslan Bukin 	iommu_map_entry_zone = uma_zcreate("IOMMU_MAP_ENTRY",
843024e8afSRuslan Bukin 	    sizeof(struct iommu_map_entry), NULL, NULL,
853024e8afSRuslan Bukin 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NODUMP);
863024e8afSRuslan Bukin }
873024e8afSRuslan Bukin SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
883024e8afSRuslan Bukin 
893024e8afSRuslan Bukin struct iommu_map_entry *
iommu_gas_alloc_entry(struct iommu_domain * domain,u_int flags)903024e8afSRuslan Bukin iommu_gas_alloc_entry(struct iommu_domain *domain, u_int flags)
913024e8afSRuslan Bukin {
923024e8afSRuslan Bukin 	struct iommu_map_entry *res;
933024e8afSRuslan Bukin 
9415f6baf4SRuslan Bukin 	KASSERT((flags & ~(IOMMU_PGF_WAITOK)) == 0,
953024e8afSRuslan Bukin 	    ("unsupported flags %x", flags));
963024e8afSRuslan Bukin 
9715f6baf4SRuslan Bukin 	res = uma_zalloc(iommu_map_entry_zone, ((flags & IOMMU_PGF_WAITOK) !=
983024e8afSRuslan Bukin 	    0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
9942736dc4SAlan Cox 	if (res != NULL && domain != NULL) {
1003024e8afSRuslan Bukin 		res->domain = domain;
1013024e8afSRuslan Bukin 		atomic_add_int(&domain->entries_cnt, 1);
1023024e8afSRuslan Bukin 	}
1033024e8afSRuslan Bukin 	return (res);
1043024e8afSRuslan Bukin }
1053024e8afSRuslan Bukin 
1063024e8afSRuslan Bukin void
iommu_gas_free_entry(struct iommu_map_entry * entry)1074670f908SAlan Cox iommu_gas_free_entry(struct iommu_map_entry *entry)
1083024e8afSRuslan Bukin {
1094670f908SAlan Cox 	struct iommu_domain *domain;
1103024e8afSRuslan Bukin 
1114670f908SAlan Cox 	domain = entry->domain;
11242736dc4SAlan Cox 	if (domain != NULL)
1133024e8afSRuslan Bukin 		atomic_subtract_int(&domain->entries_cnt, 1);
1143024e8afSRuslan Bukin 	uma_zfree(iommu_map_entry_zone, entry);
1153024e8afSRuslan Bukin }
1163024e8afSRuslan Bukin 
1173024e8afSRuslan Bukin static int
iommu_gas_cmp_entries(struct iommu_map_entry * a,struct iommu_map_entry * b)1183024e8afSRuslan Bukin iommu_gas_cmp_entries(struct iommu_map_entry *a, struct iommu_map_entry *b)
1193024e8afSRuslan Bukin {
1203024e8afSRuslan Bukin 
121f5912877SKonstantin Belousov 	/* First and last entries have zero size, so <= */
1223024e8afSRuslan Bukin 	KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
1233024e8afSRuslan Bukin 	    a, (uintmax_t)a->start, (uintmax_t)a->end));
1243024e8afSRuslan Bukin 	KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
1253024e8afSRuslan Bukin 	    b, (uintmax_t)b->start, (uintmax_t)b->end));
126a59c2529SKonstantin Belousov 	KASSERT(((a->flags | b->flags) & IOMMU_MAP_ENTRY_FAKE) != 0 ||
127a59c2529SKonstantin Belousov 	    a->end <= b->start || b->end <= a->start ||
1283024e8afSRuslan Bukin 	    a->end == a->start || b->end == b->start,
129733da1ebSKonstantin Belousov 	    ("overlapping entries %p (%jx, %jx) f %#x %p (%jx, %jx) f %#x"
130733da1ebSKonstantin Belousov 	    " domain %p %p",
131733da1ebSKonstantin Belousov 	    a, (uintmax_t)a->start, (uintmax_t)a->end, a->flags,
132733da1ebSKonstantin Belousov 	    b, (uintmax_t)b->start, (uintmax_t)b->end, b->flags,
133733da1ebSKonstantin Belousov 	    a->domain, b->domain));
1343024e8afSRuslan Bukin 
1353024e8afSRuslan Bukin 	if (a->end < b->end)
1363024e8afSRuslan Bukin 		return (-1);
1373024e8afSRuslan Bukin 	else if (b->end < a->end)
1383024e8afSRuslan Bukin 		return (1);
1393024e8afSRuslan Bukin 	return (0);
1403024e8afSRuslan Bukin }
1413024e8afSRuslan Bukin 
142b16f993eSDoug Moore /*
143b16f993eSDoug Moore  * Update augmentation data based on data from children.
144b16f993eSDoug Moore  * Return true if and only if the update changes the augmentation data.
145b16f993eSDoug Moore  */
146b16f993eSDoug Moore static bool
iommu_gas_augment_entry(struct iommu_map_entry * entry)1473024e8afSRuslan Bukin iommu_gas_augment_entry(struct iommu_map_entry *entry)
1483024e8afSRuslan Bukin {
1493024e8afSRuslan Bukin 	struct iommu_map_entry *child;
150b16f993eSDoug Moore 	iommu_gaddr_t bound, delta, free_down;
1513024e8afSRuslan Bukin 
1523024e8afSRuslan Bukin 	free_down = 0;
153b16f993eSDoug Moore 	bound = entry->start;
1543024e8afSRuslan Bukin 	if ((child = RB_LEFT(entry, rb_entry)) != NULL) {
155b16f993eSDoug Moore 		free_down = MAX(child->free_down, bound - child->last);
156b16f993eSDoug Moore 		bound = child->first;
157b16f993eSDoug Moore 	}
158b16f993eSDoug Moore 	delta = bound - entry->first;
159b16f993eSDoug Moore 	entry->first = bound;
160b16f993eSDoug Moore 	bound = entry->end;
1613024e8afSRuslan Bukin 	if ((child = RB_RIGHT(entry, rb_entry)) != NULL) {
1623024e8afSRuslan Bukin 		free_down = MAX(free_down, child->free_down);
163b16f993eSDoug Moore 		free_down = MAX(free_down, child->first - bound);
164b16f993eSDoug Moore 		bound = child->last;
165b16f993eSDoug Moore 	}
166b16f993eSDoug Moore 	delta += entry->last - bound;
167b16f993eSDoug Moore 	if (delta == 0)
168b16f993eSDoug Moore 		delta = entry->free_down - free_down;
169b16f993eSDoug Moore 	entry->last = bound;
1703024e8afSRuslan Bukin 	entry->free_down = free_down;
171b16f993eSDoug Moore 
172b16f993eSDoug Moore 	/*
173b16f993eSDoug Moore 	 * Return true either if the value of last-first changed,
174b16f993eSDoug Moore 	 * or if free_down changed.
175b16f993eSDoug Moore 	 */
176b16f993eSDoug Moore 	return (delta != 0);
1773024e8afSRuslan Bukin }
1783024e8afSRuslan Bukin 
1793024e8afSRuslan Bukin RB_GENERATE(iommu_gas_entries_tree, iommu_map_entry, rb_entry,
1803024e8afSRuslan Bukin     iommu_gas_cmp_entries);
1813024e8afSRuslan Bukin 
1823024e8afSRuslan Bukin #ifdef INVARIANTS
1833024e8afSRuslan Bukin static void
iommu_gas_check_free(struct iommu_domain * domain)1843024e8afSRuslan Bukin iommu_gas_check_free(struct iommu_domain *domain)
1853024e8afSRuslan Bukin {
1863024e8afSRuslan Bukin 	struct iommu_map_entry *entry, *l, *r;
1873024e8afSRuslan Bukin 	iommu_gaddr_t v;
1883024e8afSRuslan Bukin 
1893024e8afSRuslan Bukin 	RB_FOREACH(entry, iommu_gas_entries_tree, &domain->rb_root) {
190b64dca2bSRuslan Bukin 		KASSERT(domain == entry->domain,
1913024e8afSRuslan Bukin 		    ("mismatched free domain %p entry %p entry->domain %p",
1923024e8afSRuslan Bukin 		    domain, entry, entry->domain));
1933024e8afSRuslan Bukin 		l = RB_LEFT(entry, rb_entry);
1943024e8afSRuslan Bukin 		r = RB_RIGHT(entry, rb_entry);
1953024e8afSRuslan Bukin 		v = 0;
1963024e8afSRuslan Bukin 		if (l != NULL) {
1973024e8afSRuslan Bukin 			v = MAX(v, l->free_down);
1983024e8afSRuslan Bukin 			v = MAX(v, entry->start - l->last);
1993024e8afSRuslan Bukin 		}
2003024e8afSRuslan Bukin 		if (r != NULL) {
2013024e8afSRuslan Bukin 			v = MAX(v, r->free_down);
2023024e8afSRuslan Bukin 			v = MAX(v, r->first - entry->end);
2033024e8afSRuslan Bukin 		}
2043024e8afSRuslan Bukin 		MPASS(entry->free_down == v);
2053024e8afSRuslan Bukin 	}
2063024e8afSRuslan Bukin }
2073024e8afSRuslan Bukin #endif
2083024e8afSRuslan Bukin 
2093024e8afSRuslan Bukin static void
iommu_gas_rb_remove(struct iommu_domain * domain,struct iommu_map_entry * entry)2103024e8afSRuslan Bukin iommu_gas_rb_remove(struct iommu_domain *domain, struct iommu_map_entry *entry)
2113024e8afSRuslan Bukin {
2128b221ca6SDoug Moore 	struct iommu_map_entry *nbr;
2133024e8afSRuslan Bukin 
2148b221ca6SDoug Moore 	/* Removing entry may open a new free gap before domain->start_gap. */
2158b221ca6SDoug Moore 	if (entry->end <= domain->start_gap->end) {
2168b221ca6SDoug Moore 		if (RB_RIGHT(entry, rb_entry) != NULL)
2178b221ca6SDoug Moore 			nbr = iommu_gas_entries_tree_RB_NEXT(entry);
2188b221ca6SDoug Moore 		else if (RB_LEFT(entry, rb_entry) != NULL)
2198b221ca6SDoug Moore 			nbr = RB_LEFT(entry, rb_entry);
2208b221ca6SDoug Moore 		else
2218b221ca6SDoug Moore 			nbr = RB_PARENT(entry, rb_entry);
2228b221ca6SDoug Moore 		domain->start_gap = nbr;
2238b221ca6SDoug Moore 	}
2243024e8afSRuslan Bukin 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
2253024e8afSRuslan Bukin }
2263024e8afSRuslan Bukin 
227f5cafae1SRuslan Bukin struct iommu_domain *
iommu_get_ctx_domain(struct iommu_ctx * ctx)228f5cafae1SRuslan Bukin iommu_get_ctx_domain(struct iommu_ctx *ctx)
229f5cafae1SRuslan Bukin {
230f5cafae1SRuslan Bukin 
231f5cafae1SRuslan Bukin 	return (ctx->domain);
232f5cafae1SRuslan Bukin }
233f5cafae1SRuslan Bukin 
2343024e8afSRuslan Bukin void
iommu_gas_init_domain(struct iommu_domain * domain)2353024e8afSRuslan Bukin iommu_gas_init_domain(struct iommu_domain *domain)
2363024e8afSRuslan Bukin {
2373024e8afSRuslan Bukin 	struct iommu_map_entry *begin, *end;
2383024e8afSRuslan Bukin 
23915f6baf4SRuslan Bukin 	begin = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
24015f6baf4SRuslan Bukin 	end = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
2413024e8afSRuslan Bukin 
2423024e8afSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
2433024e8afSRuslan Bukin 	KASSERT(domain->entries_cnt == 2, ("dirty domain %p", domain));
2443024e8afSRuslan Bukin 	KASSERT(RB_EMPTY(&domain->rb_root),
2453024e8afSRuslan Bukin 	    ("non-empty entries %p", domain));
2463024e8afSRuslan Bukin 
2473024e8afSRuslan Bukin 	end->start = domain->end;
2483024e8afSRuslan Bukin 	end->end = domain->end;
2493024e8afSRuslan Bukin 	end->flags = IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED;
250368ee2f8SDoug Moore 	RB_INSERT(iommu_gas_entries_tree, &domain->rb_root, end);
2513024e8afSRuslan Bukin 
252b16f993eSDoug Moore 	begin->start = 0;
253f5912877SKonstantin Belousov 	begin->end = 0;
254b16f993eSDoug Moore 	begin->flags = IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED;
255368ee2f8SDoug Moore 	RB_INSERT_PREV(iommu_gas_entries_tree, &domain->rb_root, end, begin);
256f5912877SKonstantin Belousov 	iommu_gas_augment_entry(end);
257f5912877SKonstantin Belousov 	iommu_gas_augment_entry(begin);
258b16f993eSDoug Moore 
25987d405eaSDoug Moore 	domain->start_gap = begin;
2603024e8afSRuslan Bukin 	domain->first_place = begin;
2613024e8afSRuslan Bukin 	domain->last_place = end;
26215f6baf4SRuslan Bukin 	domain->flags |= IOMMU_DOMAIN_GAS_INITED;
2633024e8afSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
2643024e8afSRuslan Bukin }
2653024e8afSRuslan Bukin 
2663024e8afSRuslan Bukin void
iommu_gas_fini_domain(struct iommu_domain * domain)2673024e8afSRuslan Bukin iommu_gas_fini_domain(struct iommu_domain *domain)
2683024e8afSRuslan Bukin {
269a2c57c60SDoug Moore 	struct iommu_map_entry *entry;
2703024e8afSRuslan Bukin 
2713024e8afSRuslan Bukin 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
2723024e8afSRuslan Bukin 	KASSERT(domain->entries_cnt == 2,
2733024e8afSRuslan Bukin 	    ("domain still in use %p", domain));
2743024e8afSRuslan Bukin 
2753024e8afSRuslan Bukin 	entry = RB_MIN(iommu_gas_entries_tree, &domain->rb_root);
2763024e8afSRuslan Bukin 	KASSERT(entry->start == 0, ("start entry start %p", domain));
2773024e8afSRuslan Bukin 	KASSERT(entry->end == IOMMU_PAGE_SIZE, ("start entry end %p", domain));
278dea8594fSRuslan Bukin 	KASSERT(entry->flags ==
279dea8594fSRuslan Bukin 	    (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
2803024e8afSRuslan Bukin 	    ("start entry flags %p", domain));
281368ee2f8SDoug Moore 	iommu_gas_rb_remove(domain, entry);
2824670f908SAlan Cox 	iommu_gas_free_entry(entry);
2833024e8afSRuslan Bukin 
2843024e8afSRuslan Bukin 	entry = RB_MAX(iommu_gas_entries_tree, &domain->rb_root);
2853024e8afSRuslan Bukin 	KASSERT(entry->start == domain->end, ("end entry start %p", domain));
2863024e8afSRuslan Bukin 	KASSERT(entry->end == domain->end, ("end entry end %p", domain));
287dea8594fSRuslan Bukin 	KASSERT(entry->flags ==
288dea8594fSRuslan Bukin 	    (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
2893024e8afSRuslan Bukin 	    ("end entry flags %p", domain));
290368ee2f8SDoug Moore 	iommu_gas_rb_remove(domain, entry);
2914670f908SAlan Cox 	iommu_gas_free_entry(entry);
2923024e8afSRuslan Bukin }
2933024e8afSRuslan Bukin 
2943024e8afSRuslan Bukin struct iommu_gas_match_args {
2953024e8afSRuslan Bukin 	iommu_gaddr_t size;
2963024e8afSRuslan Bukin 	int offset;
2973024e8afSRuslan Bukin 	const struct bus_dma_tag_common *common;
2983024e8afSRuslan Bukin 	u_int gas_flags;
2993024e8afSRuslan Bukin 	struct iommu_map_entry *entry;
3003024e8afSRuslan Bukin };
3013024e8afSRuslan Bukin 
3023024e8afSRuslan Bukin /*
3033024e8afSRuslan Bukin  * The interval [beg, end) is a free interval between two iommu_map_entries.
3045b9b55fbSDoug Moore  * Addresses can be allocated only in the range [lbound, ubound]. Try to
305e0e8d0c8SDoug Moore  * allocate space in the free interval, subject to the conditions expressed by
306e0e8d0c8SDoug Moore  * a, and return 'true' if and only if the allocation attempt succeeds.
3073024e8afSRuslan Bukin  */
3083024e8afSRuslan Bukin static bool
iommu_gas_match_one(struct iommu_gas_match_args * a,iommu_gaddr_t beg,iommu_gaddr_t end,iommu_gaddr_t lbound,iommu_gaddr_t ubound)3093024e8afSRuslan Bukin iommu_gas_match_one(struct iommu_gas_match_args *a, iommu_gaddr_t beg,
310e0e8d0c8SDoug Moore     iommu_gaddr_t end, iommu_gaddr_t lbound, iommu_gaddr_t ubound)
3113024e8afSRuslan Bukin {
312e0e8d0c8SDoug Moore 	struct iommu_map_entry *entry;
313e0e8d0c8SDoug Moore 	iommu_gaddr_t first, size, start;
314e0e8d0c8SDoug Moore 	int offset;
3153024e8afSRuslan Bukin 
316da33f6d7SAlan Cox 	/*
317da33f6d7SAlan Cox 	 * The prev->end is always aligned on the page size, which
318da33f6d7SAlan Cox 	 * causes page alignment for the entry->start too.
319da33f6d7SAlan Cox 	 *
320e0e8d0c8SDoug Moore 	 * Create IOMMU_PAGE_SIZE gaps before, after new entry
321e0e8d0c8SDoug Moore 	 * to ensure that out-of-bounds accesses fault.
322da33f6d7SAlan Cox 	 */
323e0e8d0c8SDoug Moore 	beg = MAX(beg + IOMMU_PAGE_SIZE, lbound);
324e0e8d0c8SDoug Moore 	start = roundup2(beg, a->common->alignment);
325e0e8d0c8SDoug Moore 	if (start < beg)
326e0e8d0c8SDoug Moore 		return (false);
327a869643eSKonstantin Belousov 	if (end < IOMMU_PAGE_SIZE + 1)
328a869643eSKonstantin Belousov 		return (false);
3295b9b55fbSDoug Moore 	end = MIN(end - IOMMU_PAGE_SIZE - 1, ubound);
330e0e8d0c8SDoug Moore 	offset = a->offset;
331e0e8d0c8SDoug Moore 	size = a->size;
3325b9b55fbSDoug Moore 	if (start + offset + size - 1 > end)
3333024e8afSRuslan Bukin 		return (false);
3343024e8afSRuslan Bukin 
335e0e8d0c8SDoug Moore 	/* Check for and try to skip past boundary crossing. */
336e0e8d0c8SDoug Moore 	if (!vm_addr_bound_ok(start + offset, size, a->common->boundary)) {
3373024e8afSRuslan Bukin 		/*
3383024e8afSRuslan Bukin 		 * The start + offset to start + offset + size region crosses
339e0e8d0c8SDoug Moore 		 * the boundary.  Check if there is enough space after the next
340e0e8d0c8SDoug Moore 		 * boundary after the beg.
3413024e8afSRuslan Bukin 		 */
342e0e8d0c8SDoug Moore 		first = start;
343e0e8d0c8SDoug Moore 		beg = roundup2(start + offset + 1, a->common->boundary);
344e0e8d0c8SDoug Moore 		start = roundup2(beg, a->common->alignment);
345e0e8d0c8SDoug Moore 
3465b9b55fbSDoug Moore 		if (start + offset + size - 1 > end ||
347e0e8d0c8SDoug Moore 		    !vm_addr_bound_ok(start + offset, size,
3483024e8afSRuslan Bukin 		    a->common->boundary)) {
3493024e8afSRuslan Bukin 			/*
350e0e8d0c8SDoug Moore 			 * Not enough space to align at the requested boundary,
351e0e8d0c8SDoug Moore 			 * or boundary is smaller than the size, but allowed to
352e0e8d0c8SDoug Moore 			 * split.  We already checked that start + size does not
353e0e8d0c8SDoug Moore 			 * overlap ubound.
3543024e8afSRuslan Bukin 			 *
355e0e8d0c8SDoug Moore 			 * XXXKIB. It is possible that beg is exactly at the
356e0e8d0c8SDoug Moore 			 * start of the next entry, then we do not have gap.
357e0e8d0c8SDoug Moore 			 * Ignore for now.
3583024e8afSRuslan Bukin 			 */
359e0e8d0c8SDoug Moore 			if ((a->gas_flags & IOMMU_MF_CANSPLIT) == 0)
360e0e8d0c8SDoug Moore 				return (false);
361e0e8d0c8SDoug Moore 			size = beg - first - offset;
362e0e8d0c8SDoug Moore 			start = first;
363e0e8d0c8SDoug Moore 		}
364e0e8d0c8SDoug Moore 	}
365e0e8d0c8SDoug Moore 	entry = a->entry;
366e0e8d0c8SDoug Moore 	entry->start = start;
367e0e8d0c8SDoug Moore 	entry->end = start + roundup2(size + offset, IOMMU_PAGE_SIZE);
368e0e8d0c8SDoug Moore 	entry->flags = IOMMU_MAP_ENTRY_MAP;
3693024e8afSRuslan Bukin 	return (true);
3703024e8afSRuslan Bukin }
3713024e8afSRuslan Bukin 
372e0e8d0c8SDoug Moore /* Find the next entry that might abut a big-enough range. */
373e0e8d0c8SDoug Moore static struct iommu_map_entry *
iommu_gas_next(struct iommu_map_entry * curr,iommu_gaddr_t min_free)374e0e8d0c8SDoug Moore iommu_gas_next(struct iommu_map_entry *curr, iommu_gaddr_t min_free)
3753024e8afSRuslan Bukin {
376e0e8d0c8SDoug Moore 	struct iommu_map_entry *next;
3773024e8afSRuslan Bukin 
378e0e8d0c8SDoug Moore 	if ((next = RB_RIGHT(curr, rb_entry)) != NULL &&
379e0e8d0c8SDoug Moore 	    next->free_down >= min_free) {
380e0e8d0c8SDoug Moore 		/* Find next entry in right subtree. */
381e0e8d0c8SDoug Moore 		do
382e0e8d0c8SDoug Moore 			curr = next;
383e0e8d0c8SDoug Moore 		while ((next = RB_LEFT(curr, rb_entry)) != NULL &&
384e0e8d0c8SDoug Moore 		    next->free_down >= min_free);
385e0e8d0c8SDoug Moore 	} else {
386e0e8d0c8SDoug Moore 		/* Find next entry in a left-parent ancestor. */
387e0e8d0c8SDoug Moore 		while ((next = RB_PARENT(curr, rb_entry)) != NULL &&
388e0e8d0c8SDoug Moore 		    curr == RB_RIGHT(next, rb_entry))
389e0e8d0c8SDoug Moore 			curr = next;
390e0e8d0c8SDoug Moore 		curr = next;
391e0e8d0c8SDoug Moore 	}
392e0e8d0c8SDoug Moore 	return (curr);
3933024e8afSRuslan Bukin }
3943024e8afSRuslan Bukin 
3958b221ca6SDoug Moore /*
3968b221ca6SDoug Moore  * Address-ordered first-fit search of 'domain' for free space satisfying the
3978b221ca6SDoug Moore  * conditions of 'a'.  The space allocated is at least one page big, and is
398a2c57c60SDoug Moore  * bounded by guard pages to the left and right.  The allocated space for
399a2c57c60SDoug Moore  * 'domain' is described by an rb-tree of map entries at domain->rb_root, and
400a2c57c60SDoug Moore  * domain->start_gap points to a map entry less than or adjacent to the first
4018b221ca6SDoug Moore  * free-space of size at least 3 pages.
4028b221ca6SDoug Moore  */
4033024e8afSRuslan Bukin static int
iommu_gas_find_space(struct iommu_domain * domain,struct iommu_gas_match_args * a)4048b221ca6SDoug Moore iommu_gas_find_space(struct iommu_domain *domain,
4058b221ca6SDoug Moore     struct iommu_gas_match_args *a)
4063024e8afSRuslan Bukin {
407e0e8d0c8SDoug Moore 	struct iommu_map_entry *curr, *first;
408e0e8d0c8SDoug Moore 	iommu_gaddr_t addr, min_free;
409e0e8d0c8SDoug Moore 
4108b221ca6SDoug Moore 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
411e0e8d0c8SDoug Moore 	KASSERT(a->entry->flags == 0,
4128b221ca6SDoug Moore 	    ("dirty entry %p %p", domain, a->entry));
4138b221ca6SDoug Moore 
4148b221ca6SDoug Moore 	/*
4158b221ca6SDoug Moore 	 * start_gap may point to an entry adjacent to gaps too small for any
4168b221ca6SDoug Moore 	 * new allocation.  In that case, advance start_gap to the first free
4178b221ca6SDoug Moore 	 * space big enough for a minimum allocation plus two guard pages.
4188b221ca6SDoug Moore 	 */
4198b221ca6SDoug Moore 	min_free = 3 * IOMMU_PAGE_SIZE;
4208b221ca6SDoug Moore 	first = domain->start_gap;
4218b221ca6SDoug Moore 	while (first != NULL && first->free_down < min_free)
4228b221ca6SDoug Moore 		first = RB_PARENT(first, rb_entry);
4238b221ca6SDoug Moore 	for (curr = first; curr != NULL;
4248b221ca6SDoug Moore 	    curr = iommu_gas_next(curr, min_free)) {
4258b221ca6SDoug Moore 		if ((first = RB_LEFT(curr, rb_entry)) != NULL &&
4268b221ca6SDoug Moore 		    first->last + min_free <= curr->start)
4278b221ca6SDoug Moore 			break;
4288b221ca6SDoug Moore 		if ((first = RB_RIGHT(curr, rb_entry)) != NULL &&
4298b221ca6SDoug Moore 		    curr->end + min_free <= first->first)
4308b221ca6SDoug Moore 			break;
4318b221ca6SDoug Moore 	}
4328b221ca6SDoug Moore 	domain->start_gap = curr;
4333024e8afSRuslan Bukin 
434b831865fSDoug Moore 	/*
435b831865fSDoug Moore 	 * If the subtree doesn't have free space for the requested allocation
436f979ad00SDoug Moore 	 * plus two guard pages, skip it.
437b831865fSDoug Moore 	 */
438f979ad00SDoug Moore 	min_free = 2 * IOMMU_PAGE_SIZE +
439f979ad00SDoug Moore 	    roundup2(a->size + a->offset, IOMMU_PAGE_SIZE);
440f979ad00SDoug Moore 
4418b221ca6SDoug Moore 	/* Climb to find a node in the subtree of big-enough ranges. */
442e0e8d0c8SDoug Moore 	first = curr;
4438b221ca6SDoug Moore 	while (first != NULL && first->free_down < min_free)
4448b221ca6SDoug Moore 		first = RB_PARENT(first, rb_entry);
445f979ad00SDoug Moore 
446f979ad00SDoug Moore 	/*
4478b221ca6SDoug Moore 	 * Walk the big-enough ranges tree until one satisfies alignment
448f979ad00SDoug Moore 	 * requirements, or violates lowaddr address requirement.
449f979ad00SDoug Moore 	 */
4505b9b55fbSDoug Moore 	addr = a->common->lowaddr;
451e0e8d0c8SDoug Moore 	for (curr = first; curr != NULL;
452e0e8d0c8SDoug Moore 	    curr = iommu_gas_next(curr, min_free)) {
453e0e8d0c8SDoug Moore 		if ((first = RB_LEFT(curr, rb_entry)) != NULL &&
454e0e8d0c8SDoug Moore 		    iommu_gas_match_one(a, first->last, curr->start,
455368ee2f8SDoug Moore 		    0, addr)) {
456368ee2f8SDoug Moore 			RB_INSERT_PREV(iommu_gas_entries_tree,
457368ee2f8SDoug Moore 			    &domain->rb_root, curr, a->entry);
45830031172SDoug Moore 			return (0);
459368ee2f8SDoug Moore 		}
460e0e8d0c8SDoug Moore 		if (curr->end >= addr) {
4615b9b55fbSDoug Moore 			/* All remaining ranges > addr */
462f979ad00SDoug Moore 			break;
463f979ad00SDoug Moore 		}
464e0e8d0c8SDoug Moore 		if ((first = RB_RIGHT(curr, rb_entry)) != NULL &&
465e0e8d0c8SDoug Moore 		    iommu_gas_match_one(a, curr->end, first->first,
466368ee2f8SDoug Moore 		    0, addr)) {
467368ee2f8SDoug Moore 			RB_INSERT_NEXT(iommu_gas_entries_tree,
468368ee2f8SDoug Moore 			    &domain->rb_root, curr, a->entry);
4693024e8afSRuslan Bukin 			return (0);
4703024e8afSRuslan Bukin 		}
471368ee2f8SDoug Moore 	}
4723024e8afSRuslan Bukin 
473b831865fSDoug Moore 	/*
474e0e8d0c8SDoug Moore 	 * To resume the search at the start of the upper region, first climb to
475e0e8d0c8SDoug Moore 	 * the nearest ancestor that spans highaddr.  Then find the last entry
476e0e8d0c8SDoug Moore 	 * before highaddr that could abut a big-enough range.
477b831865fSDoug Moore 	 */
478e0e8d0c8SDoug Moore 	addr = a->common->highaddr;
479e0e8d0c8SDoug Moore 	while (curr != NULL && curr->last < addr)
480e0e8d0c8SDoug Moore 		curr = RB_PARENT(curr, rb_entry);
481e0e8d0c8SDoug Moore 	first = NULL;
482e0e8d0c8SDoug Moore 	while (curr != NULL && curr->free_down >= min_free) {
483e0e8d0c8SDoug Moore 		if (addr < curr->end)
484e0e8d0c8SDoug Moore 			curr = RB_LEFT(curr, rb_entry);
485e0e8d0c8SDoug Moore 		else {
486e0e8d0c8SDoug Moore 			first = curr;
487e0e8d0c8SDoug Moore 			curr = RB_RIGHT(curr, rb_entry);
4883024e8afSRuslan Bukin 		}
4893024e8afSRuslan Bukin 	}
4903024e8afSRuslan Bukin 
491e0e8d0c8SDoug Moore 	/*
492e0e8d0c8SDoug Moore 	 * Walk the remaining big-enough ranges until one satisfies alignment
493e0e8d0c8SDoug Moore 	 * requirements.
494e0e8d0c8SDoug Moore 	 */
495e0e8d0c8SDoug Moore 	for (curr = first; curr != NULL;
496e0e8d0c8SDoug Moore 	    curr = iommu_gas_next(curr, min_free)) {
497e0e8d0c8SDoug Moore 		if ((first = RB_LEFT(curr, rb_entry)) != NULL &&
498e0e8d0c8SDoug Moore 		    iommu_gas_match_one(a, first->last, curr->start,
4995b9b55fbSDoug Moore 		    addr + 1, domain->end - 1)) {
500368ee2f8SDoug Moore 			RB_INSERT_PREV(iommu_gas_entries_tree,
501368ee2f8SDoug Moore 			    &domain->rb_root, curr, a->entry);
5023024e8afSRuslan Bukin 			return (0);
503368ee2f8SDoug Moore 		}
504e0e8d0c8SDoug Moore 		if ((first = RB_RIGHT(curr, rb_entry)) != NULL &&
505e0e8d0c8SDoug Moore 		    iommu_gas_match_one(a, curr->end, first->first,
5065b9b55fbSDoug Moore 		    addr + 1, domain->end - 1)) {
507368ee2f8SDoug Moore 			RB_INSERT_NEXT(iommu_gas_entries_tree,
508368ee2f8SDoug Moore 			    &domain->rb_root, curr, a->entry);
509e0e8d0c8SDoug Moore 			return (0);
5103024e8afSRuslan Bukin 		}
511368ee2f8SDoug Moore 	}
512e0e8d0c8SDoug Moore 
5133024e8afSRuslan Bukin 	return (ENOMEM);
5143024e8afSRuslan Bukin }
5153024e8afSRuslan Bukin 
5163024e8afSRuslan Bukin static int
iommu_gas_alloc_region(struct iommu_domain * domain,struct iommu_map_entry * entry,u_int flags)5173024e8afSRuslan Bukin iommu_gas_alloc_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
5183024e8afSRuslan Bukin     u_int flags)
5193024e8afSRuslan Bukin {
5203024e8afSRuslan Bukin 	struct iommu_map_entry *next, *prev;
5213024e8afSRuslan Bukin 
5223024e8afSRuslan Bukin 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
5233024e8afSRuslan Bukin 
5243024e8afSRuslan Bukin 	if ((entry->start & IOMMU_PAGE_MASK) != 0 ||
5253024e8afSRuslan Bukin 	    (entry->end & IOMMU_PAGE_MASK) != 0)
5263024e8afSRuslan Bukin 		return (EINVAL);
5273024e8afSRuslan Bukin 	if (entry->start >= entry->end)
5283024e8afSRuslan Bukin 		return (EINVAL);
5293024e8afSRuslan Bukin 	if (entry->end >= domain->end)
5303024e8afSRuslan Bukin 		return (EINVAL);
5313024e8afSRuslan Bukin 
532a59c2529SKonstantin Belousov 	entry->flags |= IOMMU_MAP_ENTRY_FAKE;
5333024e8afSRuslan Bukin 	next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, entry);
5343024e8afSRuslan Bukin 	KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
5353024e8afSRuslan Bukin 	    (uintmax_t)entry->start));
5363024e8afSRuslan Bukin 	prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
5373024e8afSRuslan Bukin 	/* prev could be NULL */
538a59c2529SKonstantin Belousov 	entry->flags &= ~IOMMU_MAP_ENTRY_FAKE;
5393024e8afSRuslan Bukin 
5403024e8afSRuslan Bukin 	/*
5413024e8afSRuslan Bukin 	 * Adapt to broken BIOSes which specify overlapping RMRR
5423024e8afSRuslan Bukin 	 * entries.
5433024e8afSRuslan Bukin 	 *
5443024e8afSRuslan Bukin 	 * XXXKIB: this does not handle a case when prev or next
5453024e8afSRuslan Bukin 	 * entries are completely covered by the current one, which
5463024e8afSRuslan Bukin 	 * extends both ways.
5473024e8afSRuslan Bukin 	 */
5483024e8afSRuslan Bukin 	if (prev != NULL && prev->end > entry->start &&
5493024e8afSRuslan Bukin 	    (prev->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
5503024e8afSRuslan Bukin 		if ((flags & IOMMU_MF_RMRR) == 0 ||
5513024e8afSRuslan Bukin 		    (prev->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
5523024e8afSRuslan Bukin 			return (EBUSY);
5533024e8afSRuslan Bukin 		entry->start = prev->end;
5543024e8afSRuslan Bukin 	}
5553024e8afSRuslan Bukin 	if (next->start < entry->end &&
5563024e8afSRuslan Bukin 	    (next->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
5573024e8afSRuslan Bukin 		if ((flags & IOMMU_MF_RMRR) == 0 ||
5583024e8afSRuslan Bukin 		    (next->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
5593024e8afSRuslan Bukin 			return (EBUSY);
5603024e8afSRuslan Bukin 		entry->end = next->start;
5613024e8afSRuslan Bukin 	}
5623024e8afSRuslan Bukin 	if (entry->end == entry->start)
5633024e8afSRuslan Bukin 		return (0);
5643024e8afSRuslan Bukin 
5653024e8afSRuslan Bukin 	if (prev != NULL && prev->end > entry->start) {
5663024e8afSRuslan Bukin 		/* This assumes that prev is the placeholder entry. */
5673024e8afSRuslan Bukin 		iommu_gas_rb_remove(domain, prev);
5683024e8afSRuslan Bukin 		prev = NULL;
5693024e8afSRuslan Bukin 	}
570368ee2f8SDoug Moore 	RB_INSERT_PREV(iommu_gas_entries_tree,
571368ee2f8SDoug Moore 	    &domain->rb_root, next, entry);
5723024e8afSRuslan Bukin 	if (next->start < entry->end) {
5733024e8afSRuslan Bukin 		iommu_gas_rb_remove(domain, next);
5743024e8afSRuslan Bukin 		next = NULL;
5753024e8afSRuslan Bukin 	}
5763024e8afSRuslan Bukin 
5773024e8afSRuslan Bukin 	if ((flags & IOMMU_MF_RMRR) != 0)
5783024e8afSRuslan Bukin 		entry->flags = IOMMU_MAP_ENTRY_RMRR;
5793024e8afSRuslan Bukin 
5803024e8afSRuslan Bukin #ifdef INVARIANTS
5813024e8afSRuslan Bukin 	struct iommu_map_entry *ip, *in;
5823024e8afSRuslan Bukin 	ip = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
5833024e8afSRuslan Bukin 	in = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
5843024e8afSRuslan Bukin 	KASSERT(prev == NULL || ip == prev,
5853024e8afSRuslan Bukin 	    ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
5863024e8afSRuslan Bukin 	    entry, entry->start, entry->end, prev,
5873024e8afSRuslan Bukin 	    prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
5883024e8afSRuslan Bukin 	    ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
5893024e8afSRuslan Bukin 	KASSERT(next == NULL || in == next,
5903024e8afSRuslan Bukin 	    ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
5913024e8afSRuslan Bukin 	    entry, entry->start, entry->end, next,
5923024e8afSRuslan Bukin 	    next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
5933024e8afSRuslan Bukin 	    in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
5943024e8afSRuslan Bukin #endif
5953024e8afSRuslan Bukin 
5963024e8afSRuslan Bukin 	return (0);
5973024e8afSRuslan Bukin }
5983024e8afSRuslan Bukin 
5993024e8afSRuslan Bukin void
iommu_gas_free_space(struct iommu_map_entry * entry)6004670f908SAlan Cox iommu_gas_free_space(struct iommu_map_entry *entry)
6013024e8afSRuslan Bukin {
6024670f908SAlan Cox 	struct iommu_domain *domain;
6033024e8afSRuslan Bukin 
6044670f908SAlan Cox 	domain = entry->domain;
6053024e8afSRuslan Bukin 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
6063024e8afSRuslan Bukin 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_MAP,
6073024e8afSRuslan Bukin 	    ("permanent entry %p %p", domain, entry));
6083024e8afSRuslan Bukin 
6094670f908SAlan Cox 	IOMMU_DOMAIN_LOCK(domain);
6103024e8afSRuslan Bukin 	iommu_gas_rb_remove(domain, entry);
6113024e8afSRuslan Bukin 	entry->flags &= ~IOMMU_MAP_ENTRY_MAP;
6123024e8afSRuslan Bukin #ifdef INVARIANTS
6133024e8afSRuslan Bukin 	if (iommu_check_free)
6143024e8afSRuslan Bukin 		iommu_gas_check_free(domain);
6153024e8afSRuslan Bukin #endif
6164670f908SAlan Cox 	IOMMU_DOMAIN_UNLOCK(domain);
6173024e8afSRuslan Bukin }
6183024e8afSRuslan Bukin 
6193024e8afSRuslan Bukin void
iommu_gas_free_region(struct iommu_map_entry * entry)6204670f908SAlan Cox iommu_gas_free_region(struct iommu_map_entry *entry)
6213024e8afSRuslan Bukin {
6224670f908SAlan Cox 	struct iommu_domain *domain;
6233024e8afSRuslan Bukin 
6244670f908SAlan Cox 	domain = entry->domain;
6253024e8afSRuslan Bukin 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
6263024e8afSRuslan Bukin 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_RMRR,
6273024e8afSRuslan Bukin 	    ("non-RMRR entry %p %p", domain, entry));
6283024e8afSRuslan Bukin 
6294670f908SAlan Cox 	IOMMU_DOMAIN_LOCK(domain);
63087cd087aSDoug Moore 	if (entry != domain->first_place &&
63187cd087aSDoug Moore 	    entry != domain->last_place)
6323024e8afSRuslan Bukin 		iommu_gas_rb_remove(domain, entry);
6333024e8afSRuslan Bukin 	entry->flags &= ~IOMMU_MAP_ENTRY_RMRR;
6344670f908SAlan Cox 	IOMMU_DOMAIN_UNLOCK(domain);
6353024e8afSRuslan Bukin }
6363024e8afSRuslan Bukin 
637c9e4d250SKonstantin Belousov static struct iommu_map_entry *
iommu_gas_remove_clip_left(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t end,struct iommu_map_entry ** r)638c9e4d250SKonstantin Belousov iommu_gas_remove_clip_left(struct iommu_domain *domain, iommu_gaddr_t start,
639c9e4d250SKonstantin Belousov     iommu_gaddr_t end, struct iommu_map_entry **r)
640c9e4d250SKonstantin Belousov {
641c9e4d250SKonstantin Belousov 	struct iommu_map_entry *entry, *res, fentry;
642c9e4d250SKonstantin Belousov 
643c9e4d250SKonstantin Belousov 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
644c9e4d250SKonstantin Belousov 	MPASS(start <= end);
64587cd087aSDoug Moore 	MPASS(end <= domain->end);
646c9e4d250SKonstantin Belousov 
647c9e4d250SKonstantin Belousov 	/*
648c9e4d250SKonstantin Belousov 	 * Find an entry which contains the supplied guest's address
649c9e4d250SKonstantin Belousov 	 * start, or the first entry after the start.  Since we
650c9e4d250SKonstantin Belousov 	 * asserted that start is below domain end, entry should
651c9e4d250SKonstantin Belousov 	 * exist.  Then clip it if needed.
652c9e4d250SKonstantin Belousov 	 */
653cb1d664bSKonstantin Belousov 	bzero(&fentry, sizeof(fentry));
654c9e4d250SKonstantin Belousov 	fentry.start = start + 1;
655c9e4d250SKonstantin Belousov 	fentry.end = start + 1;
656a59c2529SKonstantin Belousov 	fentry.flags = IOMMU_MAP_ENTRY_FAKE;
657c9e4d250SKonstantin Belousov 	entry = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, &fentry);
658c9e4d250SKonstantin Belousov 
659c9e4d250SKonstantin Belousov 	if (entry->start >= start ||
660c9e4d250SKonstantin Belousov 	    (entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
661c9e4d250SKonstantin Belousov 		return (entry);
662c9e4d250SKonstantin Belousov 
663c9e4d250SKonstantin Belousov 	res = *r;
664c9e4d250SKonstantin Belousov 	*r = NULL;
665c9e4d250SKonstantin Belousov 	*res = *entry;
666c9e4d250SKonstantin Belousov 	res->start = entry->end = start;
667c9e4d250SKonstantin Belousov 	RB_UPDATE_AUGMENT(entry, rb_entry);
668368ee2f8SDoug Moore 	RB_INSERT_NEXT(iommu_gas_entries_tree,
669368ee2f8SDoug Moore 	    &domain->rb_root, entry, res);
670c9e4d250SKonstantin Belousov 	return (res);
671c9e4d250SKonstantin Belousov }
672c9e4d250SKonstantin Belousov 
673c9e4d250SKonstantin Belousov static bool
iommu_gas_remove_clip_right(struct iommu_domain * domain,iommu_gaddr_t end,struct iommu_map_entry * entry,struct iommu_map_entry * r)674c9e4d250SKonstantin Belousov iommu_gas_remove_clip_right(struct iommu_domain *domain,
675c9e4d250SKonstantin Belousov     iommu_gaddr_t end, struct iommu_map_entry *entry,
676c9e4d250SKonstantin Belousov     struct iommu_map_entry *r)
677c9e4d250SKonstantin Belousov {
678c9e4d250SKonstantin Belousov 	if (entry->start >= end || (entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
679c9e4d250SKonstantin Belousov 		return (false);
680c9e4d250SKonstantin Belousov 
681c9e4d250SKonstantin Belousov 	*r = *entry;
682c9e4d250SKonstantin Belousov 	r->end = entry->start = end;
683c9e4d250SKonstantin Belousov 	RB_UPDATE_AUGMENT(entry, rb_entry);
684368ee2f8SDoug Moore 	RB_INSERT_PREV(iommu_gas_entries_tree,
685368ee2f8SDoug Moore 	    &domain->rb_root, entry, r);
686c9e4d250SKonstantin Belousov 	return (true);
687c9e4d250SKonstantin Belousov }
688c9e4d250SKonstantin Belousov 
689c9e4d250SKonstantin Belousov static void
iommu_gas_remove_unmap(struct iommu_domain * domain,struct iommu_map_entry * entry,struct iommu_map_entries_tailq * gcp)690c9e4d250SKonstantin Belousov iommu_gas_remove_unmap(struct iommu_domain *domain,
691c9e4d250SKonstantin Belousov     struct iommu_map_entry *entry, struct iommu_map_entries_tailq *gcp)
692c9e4d250SKonstantin Belousov {
693c9e4d250SKonstantin Belousov 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
694c9e4d250SKonstantin Belousov 
695c9e4d250SKonstantin Belousov 	if ((entry->flags & (IOMMU_MAP_ENTRY_UNMAPPED |
696c0047e7cSKonstantin Belousov 	    IOMMU_MAP_ENTRY_RMRR |
697c9e4d250SKonstantin Belousov 	    IOMMU_MAP_ENTRY_REMOVING)) != 0)
698c9e4d250SKonstantin Belousov 		return;
699c9e4d250SKonstantin Belousov 	MPASS((entry->flags & IOMMU_MAP_ENTRY_PLACE) == 0);
700c9e4d250SKonstantin Belousov 	entry->flags |= IOMMU_MAP_ENTRY_REMOVING;
701c9e4d250SKonstantin Belousov 	TAILQ_INSERT_TAIL(gcp, entry, dmamap_link);
702c9e4d250SKonstantin Belousov }
703c9e4d250SKonstantin Belousov 
704273b4de3SKonstantin Belousov static void
iommu_gas_remove_locked(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t size,struct iommu_map_entries_tailq * gc,struct iommu_map_entry ** r1,struct iommu_map_entry ** r2)705273b4de3SKonstantin Belousov iommu_gas_remove_locked(struct iommu_domain *domain,
706273b4de3SKonstantin Belousov     iommu_gaddr_t start, iommu_gaddr_t size,
707273b4de3SKonstantin Belousov     struct iommu_map_entries_tailq *gc,
708273b4de3SKonstantin Belousov     struct iommu_map_entry **r1, struct iommu_map_entry **r2)
709c9e4d250SKonstantin Belousov {
710273b4de3SKonstantin Belousov 	struct iommu_map_entry *entry, *nentry;
711c9e4d250SKonstantin Belousov 	iommu_gaddr_t end;
712c9e4d250SKonstantin Belousov 
713273b4de3SKonstantin Belousov 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
714273b4de3SKonstantin Belousov 
715c9e4d250SKonstantin Belousov 	end = start + size;
716c9e4d250SKonstantin Belousov 
717273b4de3SKonstantin Belousov 	nentry = iommu_gas_remove_clip_left(domain, start, end, r1);
718c9e4d250SKonstantin Belousov 	RB_FOREACH_FROM(entry, iommu_gas_entries_tree, nentry) {
719c9e4d250SKonstantin Belousov 		if (entry->start >= end)
720c9e4d250SKonstantin Belousov 			break;
721c9e4d250SKonstantin Belousov 		KASSERT(start <= entry->start,
722c9e4d250SKonstantin Belousov 		    ("iommu_gas_remove entry (%#jx, %#jx) start %#jx",
723c9e4d250SKonstantin Belousov 		    entry->start, entry->end, start));
724273b4de3SKonstantin Belousov 		iommu_gas_remove_unmap(domain, entry, gc);
725c9e4d250SKonstantin Belousov 	}
726273b4de3SKonstantin Belousov 	if (iommu_gas_remove_clip_right(domain, end, entry, *r2)) {
727273b4de3SKonstantin Belousov 		iommu_gas_remove_unmap(domain, *r2, gc);
728273b4de3SKonstantin Belousov 		*r2 = NULL;
729c9e4d250SKonstantin Belousov 	}
730c9e4d250SKonstantin Belousov 
731c9e4d250SKonstantin Belousov #ifdef INVARIANTS
732c9e4d250SKonstantin Belousov 	RB_FOREACH(entry, iommu_gas_entries_tree, &domain->rb_root) {
733f5912877SKonstantin Belousov 		if ((entry->flags & (IOMMU_MAP_ENTRY_RMRR |
734f5912877SKonstantin Belousov 		    IOMMU_MAP_ENTRY_PLACE)) != 0)
735c9e4d250SKonstantin Belousov 			continue;
736c9e4d250SKonstantin Belousov 		KASSERT(entry->end <= start || entry->start >= end,
737c9e4d250SKonstantin Belousov 		    ("iommu_gas_remove leftover entry (%#jx, %#jx) range "
738c9e4d250SKonstantin Belousov 		    "(%#jx, %#jx)",
739c9e4d250SKonstantin Belousov 		    entry->start, entry->end, start, end));
740c9e4d250SKonstantin Belousov 	}
741c9e4d250SKonstantin Belousov #endif
742273b4de3SKonstantin Belousov }
743c9e4d250SKonstantin Belousov 
744273b4de3SKonstantin Belousov static void
iommu_gas_remove_init(struct iommu_domain * domain,struct iommu_map_entries_tailq * gc,struct iommu_map_entry ** r1,struct iommu_map_entry ** r2)745273b4de3SKonstantin Belousov iommu_gas_remove_init(struct iommu_domain *domain,
746273b4de3SKonstantin Belousov     struct iommu_map_entries_tailq *gc, struct iommu_map_entry **r1,
747273b4de3SKonstantin Belousov     struct iommu_map_entry **r2)
748273b4de3SKonstantin Belousov {
749273b4de3SKonstantin Belousov 	TAILQ_INIT(gc);
750273b4de3SKonstantin Belousov 	*r1 = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
751273b4de3SKonstantin Belousov 	*r2 = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
752273b4de3SKonstantin Belousov }
753273b4de3SKonstantin Belousov 
754273b4de3SKonstantin Belousov static void
iommu_gas_remove_cleanup(struct iommu_domain * domain,struct iommu_map_entries_tailq * gc,struct iommu_map_entry ** r1,struct iommu_map_entry ** r2)755273b4de3SKonstantin Belousov iommu_gas_remove_cleanup(struct iommu_domain *domain,
756273b4de3SKonstantin Belousov     struct iommu_map_entries_tailq *gc, struct iommu_map_entry **r1,
757273b4de3SKonstantin Belousov     struct iommu_map_entry **r2)
758273b4de3SKonstantin Belousov {
759273b4de3SKonstantin Belousov 	if (*r1 != NULL) {
760273b4de3SKonstantin Belousov 		iommu_gas_free_entry(*r1);
761273b4de3SKonstantin Belousov 		*r1 = NULL;
762273b4de3SKonstantin Belousov 	}
763273b4de3SKonstantin Belousov 	if (*r2 != NULL) {
764273b4de3SKonstantin Belousov 		iommu_gas_free_entry(*r2);
765273b4de3SKonstantin Belousov 		*r2 = NULL;
766273b4de3SKonstantin Belousov 	}
767273b4de3SKonstantin Belousov 	iommu_domain_unload(domain, gc, true);
768273b4de3SKonstantin Belousov }
769273b4de3SKonstantin Belousov 
770273b4de3SKonstantin Belousov /*
771273b4de3SKonstantin Belousov  * Remove specified range from the GAS of the domain.  Note that the
772273b4de3SKonstantin Belousov  * removal is not guaranteed to occur upon the function return, it
773273b4de3SKonstantin Belousov  * might be finalized some time after, when hardware reports that
774273b4de3SKonstantin Belousov  * (queued) IOTLB invalidation was performed.
775273b4de3SKonstantin Belousov  */
776273b4de3SKonstantin Belousov void
iommu_gas_remove(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t size)777273b4de3SKonstantin Belousov iommu_gas_remove(struct iommu_domain *domain, iommu_gaddr_t start,
778273b4de3SKonstantin Belousov     iommu_gaddr_t size)
779273b4de3SKonstantin Belousov {
780273b4de3SKonstantin Belousov 	struct iommu_map_entry *r1, *r2;
781273b4de3SKonstantin Belousov 	struct iommu_map_entries_tailq gc;
782273b4de3SKonstantin Belousov 
783273b4de3SKonstantin Belousov 	iommu_gas_remove_init(domain, &gc, &r1, &r2);
784273b4de3SKonstantin Belousov 	IOMMU_DOMAIN_LOCK(domain);
785273b4de3SKonstantin Belousov 	iommu_gas_remove_locked(domain, start, size, &gc, &r1, &r2);
786c9e4d250SKonstantin Belousov 	IOMMU_DOMAIN_UNLOCK(domain);
787273b4de3SKonstantin Belousov 	iommu_gas_remove_cleanup(domain, &gc, &r1, &r2);
788c9e4d250SKonstantin Belousov }
789c9e4d250SKonstantin Belousov 
7903024e8afSRuslan Bukin int
iommu_gas_map(struct iommu_domain * domain,const struct bus_dma_tag_common * common,iommu_gaddr_t size,int offset,u_int eflags,u_int flags,vm_page_t * ma,struct iommu_map_entry ** res)7913024e8afSRuslan Bukin iommu_gas_map(struct iommu_domain *domain,
7923024e8afSRuslan Bukin     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
7933024e8afSRuslan Bukin     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res)
7943024e8afSRuslan Bukin {
795e0e8d0c8SDoug Moore 	struct iommu_gas_match_args a;
7963024e8afSRuslan Bukin 	struct iommu_map_entry *entry;
7973024e8afSRuslan Bukin 	int error;
7983024e8afSRuslan Bukin 
7993024e8afSRuslan Bukin 	KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_CANSPLIT)) == 0,
8003024e8afSRuslan Bukin 	    ("invalid flags 0x%x", flags));
8013024e8afSRuslan Bukin 
802e0e8d0c8SDoug Moore 	a.size = size;
803e0e8d0c8SDoug Moore 	a.offset = offset;
804e0e8d0c8SDoug Moore 	a.common = common;
805e0e8d0c8SDoug Moore 	a.gas_flags = flags;
8063024e8afSRuslan Bukin 	entry = iommu_gas_alloc_entry(domain,
80715f6baf4SRuslan Bukin 	    (flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0);
8083024e8afSRuslan Bukin 	if (entry == NULL)
8093024e8afSRuslan Bukin 		return (ENOMEM);
810e0e8d0c8SDoug Moore 	a.entry = entry;
8113024e8afSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
8128b221ca6SDoug Moore 	error = iommu_gas_find_space(domain, &a);
8133024e8afSRuslan Bukin 	if (error == ENOMEM) {
8143024e8afSRuslan Bukin 		IOMMU_DOMAIN_UNLOCK(domain);
8154670f908SAlan Cox 		iommu_gas_free_entry(entry);
8163024e8afSRuslan Bukin 		return (error);
8173024e8afSRuslan Bukin 	}
8183024e8afSRuslan Bukin #ifdef INVARIANTS
8193024e8afSRuslan Bukin 	if (iommu_check_free)
8203024e8afSRuslan Bukin 		iommu_gas_check_free(domain);
8213024e8afSRuslan Bukin #endif
8223024e8afSRuslan Bukin 	KASSERT(error == 0,
8233024e8afSRuslan Bukin 	    ("unexpected error %d from iommu_gas_find_entry", error));
8243024e8afSRuslan Bukin 	KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
8253024e8afSRuslan Bukin 	    (uintmax_t)entry->end, (uintmax_t)domain->end));
8263024e8afSRuslan Bukin 	entry->flags |= eflags;
8273024e8afSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
8283024e8afSRuslan Bukin 
8290eed04c8SRuslan Bukin 	error = domain->ops->map(domain, entry->start,
8300eed04c8SRuslan Bukin 	    entry->end - entry->start, ma, eflags,
83115f6baf4SRuslan Bukin 	    ((flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0));
8323024e8afSRuslan Bukin 	if (error == ENOMEM) {
8338bc36738SAlan Cox 		iommu_domain_unload_entry(entry, true,
8348bc36738SAlan Cox 		    (flags & IOMMU_MF_CANWAIT) != 0);
8353024e8afSRuslan Bukin 		return (error);
8363024e8afSRuslan Bukin 	}
8373024e8afSRuslan Bukin 	KASSERT(error == 0,
8383024e8afSRuslan Bukin 	    ("unexpected error %d from domain_map_buf", error));
8393024e8afSRuslan Bukin 
8403024e8afSRuslan Bukin 	*res = entry;
8413024e8afSRuslan Bukin 	return (0);
8423024e8afSRuslan Bukin }
8433024e8afSRuslan Bukin 
8443024e8afSRuslan Bukin int
iommu_gas_map_region(struct iommu_domain * domain,struct iommu_map_entry * entry,u_int eflags,u_int flags,vm_page_t * ma)8453024e8afSRuslan Bukin iommu_gas_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
8463024e8afSRuslan Bukin     u_int eflags, u_int flags, vm_page_t *ma)
8473024e8afSRuslan Bukin {
8483024e8afSRuslan Bukin 	iommu_gaddr_t start;
8493024e8afSRuslan Bukin 	int error;
8503024e8afSRuslan Bukin 
8514670f908SAlan Cox 	KASSERT(entry->domain == domain,
8524670f908SAlan Cox 	    ("mismatched domain %p entry %p entry->domain %p", domain,
8534670f908SAlan Cox 	    entry, entry->domain));
8543024e8afSRuslan Bukin 	KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
8553024e8afSRuslan Bukin 	    entry, entry->flags));
8563024e8afSRuslan Bukin 	KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_RMRR)) == 0,
8573024e8afSRuslan Bukin 	    ("invalid flags 0x%x", flags));
8583024e8afSRuslan Bukin 
8593024e8afSRuslan Bukin 	start = entry->start;
8603024e8afSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
8613024e8afSRuslan Bukin 	error = iommu_gas_alloc_region(domain, entry, flags);
8623024e8afSRuslan Bukin 	if (error != 0) {
8633024e8afSRuslan Bukin 		IOMMU_DOMAIN_UNLOCK(domain);
8643024e8afSRuslan Bukin 		return (error);
8653024e8afSRuslan Bukin 	}
8663024e8afSRuslan Bukin 	entry->flags |= eflags;
8673024e8afSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
8683024e8afSRuslan Bukin 	if (entry->end == entry->start)
8693024e8afSRuslan Bukin 		return (0);
8703024e8afSRuslan Bukin 
8710eed04c8SRuslan Bukin 	error = domain->ops->map(domain, entry->start,
8720eed04c8SRuslan Bukin 	    entry->end - entry->start, ma + OFF_TO_IDX(start - entry->start),
8730eed04c8SRuslan Bukin 	    eflags, ((flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0));
8743024e8afSRuslan Bukin 	if (error == ENOMEM) {
8758bc36738SAlan Cox 		iommu_domain_unload_entry(entry, false,
8768bc36738SAlan Cox 		    (flags & IOMMU_MF_CANWAIT) != 0);
8773024e8afSRuslan Bukin 		return (error);
8783024e8afSRuslan Bukin 	}
8793024e8afSRuslan Bukin 	KASSERT(error == 0,
8803024e8afSRuslan Bukin 	    ("unexpected error %d from domain_map_buf", error));
8813024e8afSRuslan Bukin 
8823024e8afSRuslan Bukin 	return (0);
8833024e8afSRuslan Bukin }
8843024e8afSRuslan Bukin 
885ee47a12aSRyan Libby static int
iommu_gas_reserve_region_locked(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t end,struct iommu_map_entry * entry)886ee47a12aSRyan Libby iommu_gas_reserve_region_locked(struct iommu_domain *domain,
887ee47a12aSRyan Libby     iommu_gaddr_t start, iommu_gaddr_t end, struct iommu_map_entry *entry)
888ee47a12aSRyan Libby {
889ee47a12aSRyan Libby 	int error;
890ee47a12aSRyan Libby 
891ee47a12aSRyan Libby 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
892ee47a12aSRyan Libby 
893ee47a12aSRyan Libby 	entry->start = start;
894ee47a12aSRyan Libby 	entry->end = end;
895ee47a12aSRyan Libby 	error = iommu_gas_alloc_region(domain, entry, IOMMU_MF_CANWAIT);
896ee47a12aSRyan Libby 	if (error == 0)
897ee47a12aSRyan Libby 		entry->flags |= IOMMU_MAP_ENTRY_UNMAPPED;
898ee47a12aSRyan Libby 	return (error);
899ee47a12aSRyan Libby }
900ee47a12aSRyan Libby 
9013024e8afSRuslan Bukin int
iommu_gas_reserve_region(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t end,struct iommu_map_entry ** entry0)9023024e8afSRuslan Bukin iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
90394dfb28eSRuslan Bukin     iommu_gaddr_t end, struct iommu_map_entry **entry0)
9043024e8afSRuslan Bukin {
9053024e8afSRuslan Bukin 	struct iommu_map_entry *entry;
9063024e8afSRuslan Bukin 	int error;
9073024e8afSRuslan Bukin 
90815f6baf4SRuslan Bukin 	entry = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
9093024e8afSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
910ee47a12aSRyan Libby 	error = iommu_gas_reserve_region_locked(domain, start, end, entry);
9113024e8afSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
9123024e8afSRuslan Bukin 	if (error != 0)
9134670f908SAlan Cox 		iommu_gas_free_entry(entry);
91494dfb28eSRuslan Bukin 	else if (entry0 != NULL)
91594dfb28eSRuslan Bukin 		*entry0 = entry;
9163024e8afSRuslan Bukin 	return (error);
9173024e8afSRuslan Bukin }
9183024e8afSRuslan Bukin 
919ee47a12aSRyan Libby /*
920ee47a12aSRyan Libby  * As in iommu_gas_reserve_region, reserve [start, end), but allow for existing
921ee47a12aSRyan Libby  * entries.
922ee47a12aSRyan Libby  */
923ee47a12aSRyan Libby int
iommu_gas_reserve_region_extend(struct iommu_domain * domain,iommu_gaddr_t start,iommu_gaddr_t end)924ee47a12aSRyan Libby iommu_gas_reserve_region_extend(struct iommu_domain *domain,
925ee47a12aSRyan Libby     iommu_gaddr_t start, iommu_gaddr_t end)
926ee47a12aSRyan Libby {
927ee47a12aSRyan Libby 	struct iommu_map_entry *entry, *next, *prev, key = {};
928ee47a12aSRyan Libby 	iommu_gaddr_t entry_start, entry_end;
929ee47a12aSRyan Libby 	int error;
930ee47a12aSRyan Libby 
931ee47a12aSRyan Libby 	error = 0;
932ee47a12aSRyan Libby 	entry = NULL;
933ee47a12aSRyan Libby 	end = ummin(end, domain->end);
934ee47a12aSRyan Libby 	while (start < end) {
935ee47a12aSRyan Libby 		/* Preallocate an entry. */
936ee47a12aSRyan Libby 		if (entry == NULL)
937ee47a12aSRyan Libby 			entry = iommu_gas_alloc_entry(domain,
938ee47a12aSRyan Libby 			    IOMMU_PGF_WAITOK);
939ee47a12aSRyan Libby 		/* Calculate the free region from here to the next entry. */
940ee47a12aSRyan Libby 		key.start = key.end = start;
941ee47a12aSRyan Libby 		IOMMU_DOMAIN_LOCK(domain);
942ee47a12aSRyan Libby 		next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, &key);
943ee47a12aSRyan Libby 		KASSERT(next != NULL, ("domain %p with end %#jx has no entry "
944ee47a12aSRyan Libby 		    "after %#jx", domain, (uintmax_t)domain->end,
945ee47a12aSRyan Libby 		    (uintmax_t)start));
946ee47a12aSRyan Libby 		entry_end = ummin(end, next->start);
947ee47a12aSRyan Libby 		prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
948ee47a12aSRyan Libby 		if (prev != NULL)
949ee47a12aSRyan Libby 			entry_start = ummax(start, prev->end);
950ee47a12aSRyan Libby 		else
951ee47a12aSRyan Libby 			entry_start = start;
952ee47a12aSRyan Libby 		start = next->end;
953ee47a12aSRyan Libby 		/* Reserve the region if non-empty. */
954ee47a12aSRyan Libby 		if (entry_start != entry_end) {
955ee47a12aSRyan Libby 			error = iommu_gas_reserve_region_locked(domain,
956ee47a12aSRyan Libby 			    entry_start, entry_end, entry);
9570ba1d860SAlan Cox 			if (error != 0) {
9580ba1d860SAlan Cox 				IOMMU_DOMAIN_UNLOCK(domain);
959ee47a12aSRyan Libby 				break;
9600ba1d860SAlan Cox 			}
961ee47a12aSRyan Libby 			entry = NULL;
962ee47a12aSRyan Libby 		}
963ee47a12aSRyan Libby 		IOMMU_DOMAIN_UNLOCK(domain);
964ee47a12aSRyan Libby 	}
965ee47a12aSRyan Libby 	/* Release a preallocated entry if it was not used. */
966ee47a12aSRyan Libby 	if (entry != NULL)
9674670f908SAlan Cox 		iommu_gas_free_entry(entry);
968ee47a12aSRyan Libby 	return (error);
969ee47a12aSRyan Libby }
970ee47a12aSRyan Libby 
971f32f0095SRuslan Bukin void
iommu_unmap_msi(struct iommu_ctx * ctx)972f32f0095SRuslan Bukin iommu_unmap_msi(struct iommu_ctx *ctx)
973f32f0095SRuslan Bukin {
974f32f0095SRuslan Bukin 	struct iommu_map_entry *entry;
975f32f0095SRuslan Bukin 	struct iommu_domain *domain;
976f32f0095SRuslan Bukin 
977f32f0095SRuslan Bukin 	domain = ctx->domain;
978f32f0095SRuslan Bukin 	entry = domain->msi_entry;
979f32f0095SRuslan Bukin 	if (entry == NULL)
980f32f0095SRuslan Bukin 		return;
981f32f0095SRuslan Bukin 
982f32f0095SRuslan Bukin 	domain->ops->unmap(domain, entry->start, entry->end -
983f32f0095SRuslan Bukin 	    entry->start, IOMMU_PGF_WAITOK);
984f32f0095SRuslan Bukin 
9854670f908SAlan Cox 	iommu_gas_free_space(entry);
986f32f0095SRuslan Bukin 
9874670f908SAlan Cox 	iommu_gas_free_entry(entry);
988f32f0095SRuslan Bukin 
989f32f0095SRuslan Bukin 	domain->msi_entry = NULL;
990f32f0095SRuslan Bukin 	domain->msi_base = 0;
991f32f0095SRuslan Bukin 	domain->msi_phys = 0;
992f32f0095SRuslan Bukin }
993f32f0095SRuslan Bukin 
9943024e8afSRuslan Bukin int
iommu_map_msi(struct iommu_ctx * ctx,iommu_gaddr_t size,int offset,u_int eflags,u_int flags,vm_page_t * ma)995e707c8beSRuslan Bukin iommu_map_msi(struct iommu_ctx *ctx, iommu_gaddr_t size, int offset,
996e707c8beSRuslan Bukin     u_int eflags, u_int flags, vm_page_t *ma)
997e707c8beSRuslan Bukin {
998e707c8beSRuslan Bukin 	struct iommu_domain *domain;
999e707c8beSRuslan Bukin 	struct iommu_map_entry *entry;
1000e707c8beSRuslan Bukin 	int error;
1001e707c8beSRuslan Bukin 
1002e707c8beSRuslan Bukin 	error = 0;
1003e707c8beSRuslan Bukin 	domain = ctx->domain;
1004e707c8beSRuslan Bukin 
1005e707c8beSRuslan Bukin 	/* Check if there is already an MSI page allocated */
1006e707c8beSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
1007e707c8beSRuslan Bukin 	entry = domain->msi_entry;
1008e707c8beSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
1009e707c8beSRuslan Bukin 
1010e707c8beSRuslan Bukin 	if (entry == NULL) {
1011e707c8beSRuslan Bukin 		error = iommu_gas_map(domain, &ctx->tag->common, size, offset,
1012e707c8beSRuslan Bukin 		    eflags, flags, ma, &entry);
1013e707c8beSRuslan Bukin 		IOMMU_DOMAIN_LOCK(domain);
1014e707c8beSRuslan Bukin 		if (error == 0) {
1015e707c8beSRuslan Bukin 			if (domain->msi_entry == NULL) {
1016e707c8beSRuslan Bukin 				MPASS(domain->msi_base == 0);
1017e707c8beSRuslan Bukin 				MPASS(domain->msi_phys == 0);
1018e707c8beSRuslan Bukin 
1019e707c8beSRuslan Bukin 				domain->msi_entry = entry;
1020e707c8beSRuslan Bukin 				domain->msi_base = entry->start;
1021e707c8beSRuslan Bukin 				domain->msi_phys = VM_PAGE_TO_PHYS(ma[0]);
1022e707c8beSRuslan Bukin 			} else {
1023e707c8beSRuslan Bukin 				/*
1024e707c8beSRuslan Bukin 				 * We lost the race and already have an
1025e707c8beSRuslan Bukin 				 * MSI page allocated. Free the unneeded entry.
1026e707c8beSRuslan Bukin 				 */
10274670f908SAlan Cox 				iommu_gas_free_entry(entry);
1028e707c8beSRuslan Bukin 			}
1029e707c8beSRuslan Bukin 		} else if (domain->msi_entry != NULL) {
1030e707c8beSRuslan Bukin 			/*
1031e707c8beSRuslan Bukin 			 * The allocation failed, but another succeeded.
1032e707c8beSRuslan Bukin 			 * Return success as there is a valid MSI page.
1033e707c8beSRuslan Bukin 			 */
1034e707c8beSRuslan Bukin 			error = 0;
1035e707c8beSRuslan Bukin 		}
1036e707c8beSRuslan Bukin 		IOMMU_DOMAIN_UNLOCK(domain);
1037e707c8beSRuslan Bukin 	}
1038e707c8beSRuslan Bukin 
1039e707c8beSRuslan Bukin 	return (error);
1040e707c8beSRuslan Bukin }
1041e707c8beSRuslan Bukin 
1042e707c8beSRuslan Bukin void
iommu_translate_msi(struct iommu_domain * domain,uint64_t * addr)1043e707c8beSRuslan Bukin iommu_translate_msi(struct iommu_domain *domain, uint64_t *addr)
1044e707c8beSRuslan Bukin {
1045e707c8beSRuslan Bukin 
1046e707c8beSRuslan Bukin 	*addr = (*addr - domain->msi_phys) + domain->msi_base;
1047e707c8beSRuslan Bukin 
1048e707c8beSRuslan Bukin 	KASSERT(*addr >= domain->msi_entry->start,
1049e707c8beSRuslan Bukin 	    ("%s: Address is below the MSI entry start address (%jx < %jx)",
1050e707c8beSRuslan Bukin 	    __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->start));
1051e707c8beSRuslan Bukin 
1052e707c8beSRuslan Bukin 	KASSERT(*addr + sizeof(*addr) <= domain->msi_entry->end,
1053e707c8beSRuslan Bukin 	    ("%s: Address is above the MSI entry end address (%jx < %jx)",
1054e707c8beSRuslan Bukin 	    __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->end));
1055e707c8beSRuslan Bukin }
1056e707c8beSRuslan Bukin 
1057357149f0SRuslan Bukin SYSCTL_NODE(_hw, OID_AUTO, iommu, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, "");
1058357149f0SRuslan Bukin 
10599c843a40SRuslan Bukin #ifdef INVARIANTS
10609c843a40SRuslan Bukin SYSCTL_INT(_hw_iommu, OID_AUTO, check_free, CTLFLAG_RWTUN,
10619c843a40SRuslan Bukin     &iommu_check_free, 0,
10629c843a40SRuslan Bukin     "Check the GPA RBtree for free_down and free_after validity");
10639c843a40SRuslan Bukin #endif
106430ce85caSKonstantin Belousov 
106530ce85caSKonstantin Belousov #include "opt_ddb.h"
106630ce85caSKonstantin Belousov #ifdef DDB
106730ce85caSKonstantin Belousov 
106830ce85caSKonstantin Belousov #include <ddb/ddb.h>
106930ce85caSKonstantin Belousov 
107030ce85caSKonstantin Belousov static void
iommu_debug_dump_gas(struct iommu_domain * domain)107130ce85caSKonstantin Belousov iommu_debug_dump_gas(struct iommu_domain *domain)
107230ce85caSKonstantin Belousov {
107330ce85caSKonstantin Belousov 	struct iommu_map_entry *entry;
107430ce85caSKonstantin Belousov 
107530ce85caSKonstantin Belousov 	db_printf("iommu_domain %p tree %p iommu %p fl %#x\n", domain,
107630ce85caSKonstantin Belousov 	    &domain->rb_root, domain->iommu, domain->flags);
107730ce85caSKonstantin Belousov 	db_printf("iommu_domain %p tree %p\n", domain, &domain->rb_root);
107830ce85caSKonstantin Belousov 	RB_FOREACH(entry, iommu_gas_entries_tree, &domain->rb_root) {
107930ce85caSKonstantin Belousov 		db_printf(
108030ce85caSKonstantin Belousov 	    "  e %p [%#jx %#jx] fl %#x first %#jx last %#jx free_down %#jx",
108130ce85caSKonstantin Belousov 		    entry, (uintmax_t)entry->start, (uintmax_t)entry->end,
108230ce85caSKonstantin Belousov 		    entry->flags,
108330ce85caSKonstantin Belousov 		    (uintmax_t)entry->first, (uintmax_t)entry->last,
108430ce85caSKonstantin Belousov 		    (uintmax_t)entry->free_down);
108530ce85caSKonstantin Belousov 		if (entry == domain->start_gap)
108630ce85caSKonstantin Belousov 			db_printf(" start_gap");
108730ce85caSKonstantin Belousov 		if (entry == domain->first_place)
108830ce85caSKonstantin Belousov 			db_printf(" first_place");
108930ce85caSKonstantin Belousov 		if (entry == domain->last_place)
109030ce85caSKonstantin Belousov 			db_printf(" last_place");
109130ce85caSKonstantin Belousov 		db_printf("\n");
109230ce85caSKonstantin Belousov 	}
109330ce85caSKonstantin Belousov }
109430ce85caSKonstantin Belousov 
DB_SHOW_COMMAND(iommu_domain,iommu_domain_show)109530ce85caSKonstantin Belousov DB_SHOW_COMMAND(iommu_domain, iommu_domain_show)
109630ce85caSKonstantin Belousov {
109730ce85caSKonstantin Belousov 	struct iommu_domain *domain;
109830ce85caSKonstantin Belousov 
109930ce85caSKonstantin Belousov 	if (!have_addr) {
110030ce85caSKonstantin Belousov 		db_printf("show iommu_domain addr\n");
110130ce85caSKonstantin Belousov 		return;
110230ce85caSKonstantin Belousov 	}
110330ce85caSKonstantin Belousov 
110430ce85caSKonstantin Belousov 	domain = (void *)addr;
110530ce85caSKonstantin Belousov 	iommu_debug_dump_gas(domain);
110630ce85caSKonstantin Belousov }
110730ce85caSKonstantin Belousov 
110830ce85caSKonstantin Belousov #endif
1109