xref: /freebsd/sys/dev/iommu/iommu_gas.c (revision f32f0095e9da8188a3865aaa310f547ee9e0c20b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #define	RB_AUGMENT(entry) iommu_gas_augment_entry(entry)
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/bus.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/ktr.h>
44 #include <sys/lock.h>
45 #include <sys/proc.h>
46 #include <sys/rwlock.h>
47 #include <sys/memdesc.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/rman.h>
51 #include <sys/taskqueue.h>
52 #include <sys/tree.h>
53 #include <sys/uio.h>
54 #include <sys/vmem.h>
55 #include <vm/vm.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>
61 #include <vm/uma.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>
72 
73 /*
74  * Guest Address Space management.
75  */
76 
77 static uma_zone_t iommu_map_entry_zone;
78 
79 #ifdef INVARIANTS
80 static int iommu_check_free;
81 #endif
82 
83 static void
84 intel_gas_init(void)
85 {
86 
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);
90 }
91 SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
92 
93 struct iommu_map_entry *
94 iommu_gas_alloc_entry(struct iommu_domain *domain, u_int flags)
95 {
96 	struct iommu_map_entry *res;
97 
98 	KASSERT((flags & ~(IOMMU_PGF_WAITOK)) == 0,
99 	    ("unsupported flags %x", flags));
100 
101 	res = uma_zalloc(iommu_map_entry_zone, ((flags & IOMMU_PGF_WAITOK) !=
102 	    0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
103 	if (res != NULL) {
104 		res->domain = domain;
105 		atomic_add_int(&domain->entries_cnt, 1);
106 	}
107 	return (res);
108 }
109 
110 void
111 iommu_gas_free_entry(struct iommu_domain *domain, struct iommu_map_entry *entry)
112 {
113 
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);
119 }
120 
121 static int
122 iommu_gas_cmp_entries(struct iommu_map_entry *a, struct iommu_map_entry *b)
123 {
124 
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));
135 
136 	if (a->end < b->end)
137 		return (-1);
138 	else if (b->end < a->end)
139 		return (1);
140 	return (0);
141 }
142 
143 static void
144 iommu_gas_augment_entry(struct iommu_map_entry *entry)
145 {
146 	struct iommu_map_entry *child;
147 	iommu_gaddr_t free_down;
148 
149 	free_down = 0;
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;
154 	} else
155 		entry->first = entry->start;
156 
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;
161 	} else
162 		entry->last = entry->end;
163 	entry->free_down = free_down;
164 }
165 
166 RB_GENERATE(iommu_gas_entries_tree, iommu_map_entry, rb_entry,
167     iommu_gas_cmp_entries);
168 
169 #ifdef INVARIANTS
170 static void
171 iommu_gas_check_free(struct iommu_domain *domain)
172 {
173 	struct iommu_map_entry *entry, *l, *r;
174 	iommu_gaddr_t v;
175 
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);
182 		v = 0;
183 		if (l != NULL) {
184 			v = MAX(v, l->free_down);
185 			v = MAX(v, entry->start - l->last);
186 		}
187 		if (r != NULL) {
188 			v = MAX(v, r->free_down);
189 			v = MAX(v, r->first - entry->end);
190 		}
191 		MPASS(entry->free_down == v);
192 	}
193 }
194 #endif
195 
196 static bool
197 iommu_gas_rb_insert(struct iommu_domain *domain, struct iommu_map_entry *entry)
198 {
199 	struct iommu_map_entry *found;
200 
201 	found = RB_INSERT(iommu_gas_entries_tree,
202 	    &domain->rb_root, entry);
203 	return (found == NULL);
204 }
205 
206 static void
207 iommu_gas_rb_remove(struct iommu_domain *domain, struct iommu_map_entry *entry)
208 {
209 
210 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
211 }
212 
213 struct iommu_domain *
214 iommu_get_ctx_domain(struct iommu_ctx *ctx)
215 {
216 
217 	return (ctx->domain);
218 }
219 
220 void
221 iommu_gas_init_domain(struct iommu_domain *domain)
222 {
223 	struct iommu_map_entry *begin, *end;
224 
225 	begin = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
226 	end = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
227 
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));
232 
233 	begin->start = 0;
234 	begin->end = IOMMU_PAGE_SIZE;
235 	begin->flags = IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED;
236 	iommu_gas_rb_insert(domain, begin);
237 
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);
242 
243 	domain->first_place = begin;
244 	domain->last_place = end;
245 	domain->flags |= IOMMU_DOMAIN_GAS_INITED;
246 	IOMMU_DOMAIN_UNLOCK(domain);
247 }
248 
249 void
250 iommu_gas_fini_domain(struct iommu_domain *domain)
251 {
252 	struct iommu_map_entry *entry, *entry1;
253 
254 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
255 	KASSERT(domain->entries_cnt == 2,
256 	    ("domain still in use %p", domain));
257 
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 == IOMMU_MAP_ENTRY_PLACE,
262 	    ("start entry flags %p", domain));
263 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
264 	iommu_gas_free_entry(domain, entry);
265 
266 	entry = RB_MAX(iommu_gas_entries_tree, &domain->rb_root);
267 	KASSERT(entry->start == domain->end, ("end entry start %p", domain));
268 	KASSERT(entry->end == domain->end, ("end entry end %p", domain));
269 	KASSERT(entry->flags == IOMMU_MAP_ENTRY_PLACE,
270 	    ("end entry flags %p", domain));
271 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
272 	iommu_gas_free_entry(domain, entry);
273 
274 	RB_FOREACH_SAFE(entry, iommu_gas_entries_tree, &domain->rb_root,
275 	    entry1) {
276 		KASSERT((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0,
277 		    ("non-RMRR entry left %p", domain));
278 		RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root,
279 		    entry);
280 		iommu_gas_free_entry(domain, entry);
281 	}
282 }
283 
284 struct iommu_gas_match_args {
285 	struct iommu_domain *domain;
286 	iommu_gaddr_t size;
287 	int offset;
288 	const struct bus_dma_tag_common *common;
289 	u_int gas_flags;
290 	struct iommu_map_entry *entry;
291 };
292 
293 /*
294  * The interval [beg, end) is a free interval between two iommu_map_entries.
295  * maxaddr is an upper bound on addresses that can be allocated. Try to
296  * allocate space in the free interval, subject to the conditions expressed
297  * by a, and return 'true' if and only if the allocation attempt succeeds.
298  */
299 static bool
300 iommu_gas_match_one(struct iommu_gas_match_args *a, iommu_gaddr_t beg,
301     iommu_gaddr_t end, iommu_gaddr_t maxaddr)
302 {
303 	iommu_gaddr_t bs, start;
304 
305 	a->entry->start = roundup2(beg + IOMMU_PAGE_SIZE,
306 	    a->common->alignment);
307 	if (a->entry->start + a->size > maxaddr)
308 		return (false);
309 
310 	/* IOMMU_PAGE_SIZE to create gap after new entry. */
311 	if (a->entry->start < beg + IOMMU_PAGE_SIZE ||
312 	    a->entry->start + a->size + a->offset + IOMMU_PAGE_SIZE > end)
313 		return (false);
314 
315 	/* No boundary crossing. */
316 	if (iommu_test_boundary(a->entry->start + a->offset, a->size,
317 	    a->common->boundary))
318 		return (true);
319 
320 	/*
321 	 * The start + offset to start + offset + size region crosses
322 	 * the boundary.  Check if there is enough space after the
323 	 * next boundary after the beg.
324 	 */
325 	bs = rounddown2(a->entry->start + a->offset + a->common->boundary,
326 	    a->common->boundary);
327 	start = roundup2(bs, a->common->alignment);
328 	/* IOMMU_PAGE_SIZE to create gap after new entry. */
329 	if (start + a->offset + a->size + IOMMU_PAGE_SIZE <= end &&
330 	    start + a->offset + a->size <= maxaddr &&
331 	    iommu_test_boundary(start + a->offset, a->size,
332 	    a->common->boundary)) {
333 		a->entry->start = start;
334 		return (true);
335 	}
336 
337 	/*
338 	 * Not enough space to align at the requested boundary, or
339 	 * boundary is smaller than the size, but allowed to split.
340 	 * We already checked that start + size does not overlap maxaddr.
341 	 *
342 	 * XXXKIB. It is possible that bs is exactly at the start of
343 	 * the next entry, then we do not have gap.  Ignore for now.
344 	 */
345 	if ((a->gas_flags & IOMMU_MF_CANSPLIT) != 0) {
346 		a->size = bs - a->entry->start;
347 		return (true);
348 	}
349 
350 	return (false);
351 }
352 
353 static void
354 iommu_gas_match_insert(struct iommu_gas_match_args *a)
355 {
356 	bool found;
357 
358 	/*
359 	 * The prev->end is always aligned on the page size, which
360 	 * causes page alignment for the entry->start too.  The size
361 	 * is checked to be multiple of the page size.
362 	 *
363 	 * The page sized gap is created between consequent
364 	 * allocations to ensure that out-of-bounds accesses fault.
365 	 */
366 	a->entry->end = a->entry->start + a->size;
367 
368 	found = iommu_gas_rb_insert(a->domain, a->entry);
369 	KASSERT(found, ("found dup %p start %jx size %jx",
370 	    a->domain, (uintmax_t)a->entry->start, (uintmax_t)a->size));
371 	a->entry->flags = IOMMU_MAP_ENTRY_MAP;
372 }
373 
374 static int
375 iommu_gas_lowermatch(struct iommu_gas_match_args *a, struct iommu_map_entry *entry)
376 {
377 	struct iommu_map_entry *child;
378 
379 	child = RB_RIGHT(entry, rb_entry);
380 	if (child != NULL && entry->end < a->common->lowaddr &&
381 	    iommu_gas_match_one(a, entry->end, child->first,
382 	    a->common->lowaddr)) {
383 		iommu_gas_match_insert(a);
384 		return (0);
385 	}
386 	if (entry->free_down < a->size + a->offset + IOMMU_PAGE_SIZE)
387 		return (ENOMEM);
388 	if (entry->first >= a->common->lowaddr)
389 		return (ENOMEM);
390 	child = RB_LEFT(entry, rb_entry);
391 	if (child != NULL && 0 == iommu_gas_lowermatch(a, child))
392 		return (0);
393 	if (child != NULL && child->last < a->common->lowaddr &&
394 	    iommu_gas_match_one(a, child->last, entry->start,
395 	    a->common->lowaddr)) {
396 		iommu_gas_match_insert(a);
397 		return (0);
398 	}
399 	child = RB_RIGHT(entry, rb_entry);
400 	if (child != NULL && 0 == iommu_gas_lowermatch(a, child))
401 		return (0);
402 	return (ENOMEM);
403 }
404 
405 static int
406 iommu_gas_uppermatch(struct iommu_gas_match_args *a, struct iommu_map_entry *entry)
407 {
408 	struct iommu_map_entry *child;
409 
410 	if (entry->free_down < a->size + a->offset + IOMMU_PAGE_SIZE)
411 		return (ENOMEM);
412 	if (entry->last < a->common->highaddr)
413 		return (ENOMEM);
414 	child = RB_LEFT(entry, rb_entry);
415 	if (child != NULL && 0 == iommu_gas_uppermatch(a, child))
416 		return (0);
417 	if (child != NULL && child->last >= a->common->highaddr &&
418 	    iommu_gas_match_one(a, child->last, entry->start,
419 	    a->domain->end)) {
420 		iommu_gas_match_insert(a);
421 		return (0);
422 	}
423 	child = RB_RIGHT(entry, rb_entry);
424 	if (child != NULL && entry->end >= a->common->highaddr &&
425 	    iommu_gas_match_one(a, entry->end, child->first,
426 	    a->domain->end)) {
427 		iommu_gas_match_insert(a);
428 		return (0);
429 	}
430 	if (child != NULL && 0 == iommu_gas_uppermatch(a, child))
431 		return (0);
432 	return (ENOMEM);
433 }
434 
435 static int
436 iommu_gas_find_space(struct iommu_domain *domain,
437     const struct bus_dma_tag_common *common, iommu_gaddr_t size,
438     int offset, u_int flags, struct iommu_map_entry *entry)
439 {
440 	struct iommu_gas_match_args a;
441 	int error;
442 
443 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
444 	KASSERT(entry->flags == 0, ("dirty entry %p %p", domain, entry));
445 	KASSERT((size & IOMMU_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
446 
447 	a.domain = domain;
448 	a.size = size;
449 	a.offset = offset;
450 	a.common = common;
451 	a.gas_flags = flags;
452 	a.entry = entry;
453 
454 	/* Handle lower region. */
455 	if (common->lowaddr > 0) {
456 		error = iommu_gas_lowermatch(&a,
457 		    RB_ROOT(&domain->rb_root));
458 		if (error == 0)
459 			return (0);
460 		KASSERT(error == ENOMEM,
461 		    ("error %d from iommu_gas_lowermatch", error));
462 	}
463 	/* Handle upper region. */
464 	if (common->highaddr >= domain->end)
465 		return (ENOMEM);
466 	error = iommu_gas_uppermatch(&a, RB_ROOT(&domain->rb_root));
467 	KASSERT(error == ENOMEM,
468 	    ("error %d from iommu_gas_uppermatch", error));
469 	return (error);
470 }
471 
472 static int
473 iommu_gas_alloc_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
474     u_int flags)
475 {
476 	struct iommu_map_entry *next, *prev;
477 	bool found;
478 
479 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
480 
481 	if ((entry->start & IOMMU_PAGE_MASK) != 0 ||
482 	    (entry->end & IOMMU_PAGE_MASK) != 0)
483 		return (EINVAL);
484 	if (entry->start >= entry->end)
485 		return (EINVAL);
486 	if (entry->end >= domain->end)
487 		return (EINVAL);
488 
489 	next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, entry);
490 	KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
491 	    (uintmax_t)entry->start));
492 	prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
493 	/* prev could be NULL */
494 
495 	/*
496 	 * Adapt to broken BIOSes which specify overlapping RMRR
497 	 * entries.
498 	 *
499 	 * XXXKIB: this does not handle a case when prev or next
500 	 * entries are completely covered by the current one, which
501 	 * extends both ways.
502 	 */
503 	if (prev != NULL && prev->end > entry->start &&
504 	    (prev->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
505 		if ((flags & IOMMU_MF_RMRR) == 0 ||
506 		    (prev->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
507 			return (EBUSY);
508 		entry->start = prev->end;
509 	}
510 	if (next->start < entry->end &&
511 	    (next->flags & IOMMU_MAP_ENTRY_PLACE) == 0) {
512 		if ((flags & IOMMU_MF_RMRR) == 0 ||
513 		    (next->flags & IOMMU_MAP_ENTRY_RMRR) == 0)
514 			return (EBUSY);
515 		entry->end = next->start;
516 	}
517 	if (entry->end == entry->start)
518 		return (0);
519 
520 	if (prev != NULL && prev->end > entry->start) {
521 		/* This assumes that prev is the placeholder entry. */
522 		iommu_gas_rb_remove(domain, prev);
523 		prev = NULL;
524 	}
525 	if (next->start < entry->end) {
526 		iommu_gas_rb_remove(domain, next);
527 		next = NULL;
528 	}
529 
530 	found = iommu_gas_rb_insert(domain, entry);
531 	KASSERT(found, ("found RMRR dup %p start %jx end %jx",
532 	    domain, (uintmax_t)entry->start, (uintmax_t)entry->end));
533 	if ((flags & IOMMU_MF_RMRR) != 0)
534 		entry->flags = IOMMU_MAP_ENTRY_RMRR;
535 
536 #ifdef INVARIANTS
537 	struct iommu_map_entry *ip, *in;
538 	ip = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
539 	in = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
540 	KASSERT(prev == NULL || ip == prev,
541 	    ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
542 	    entry, entry->start, entry->end, prev,
543 	    prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
544 	    ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
545 	KASSERT(next == NULL || in == next,
546 	    ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
547 	    entry, entry->start, entry->end, next,
548 	    next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
549 	    in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
550 #endif
551 
552 	return (0);
553 }
554 
555 void
556 iommu_gas_free_space(struct iommu_domain *domain, struct iommu_map_entry *entry)
557 {
558 
559 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
560 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
561 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_MAP,
562 	    ("permanent entry %p %p", domain, entry));
563 
564 	iommu_gas_rb_remove(domain, entry);
565 	entry->flags &= ~IOMMU_MAP_ENTRY_MAP;
566 #ifdef INVARIANTS
567 	if (iommu_check_free)
568 		iommu_gas_check_free(domain);
569 #endif
570 }
571 
572 void
573 iommu_gas_free_region(struct iommu_domain *domain, struct iommu_map_entry *entry)
574 {
575 	struct iommu_map_entry *next, *prev;
576 
577 	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
578 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
579 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_RMRR,
580 	    ("non-RMRR entry %p %p", domain, entry));
581 
582 	prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
583 	next = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
584 	iommu_gas_rb_remove(domain, entry);
585 	entry->flags &= ~IOMMU_MAP_ENTRY_RMRR;
586 
587 	if (prev == NULL)
588 		iommu_gas_rb_insert(domain, domain->first_place);
589 	if (next == NULL)
590 		iommu_gas_rb_insert(domain, domain->last_place);
591 }
592 
593 int
594 iommu_gas_map(struct iommu_domain *domain,
595     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
596     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res)
597 {
598 	struct iommu_map_entry *entry;
599 	int error;
600 
601 	KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_CANSPLIT)) == 0,
602 	    ("invalid flags 0x%x", flags));
603 
604 	entry = iommu_gas_alloc_entry(domain,
605 	    (flags & IOMMU_MF_CANWAIT) != 0 ?  IOMMU_PGF_WAITOK : 0);
606 	if (entry == NULL)
607 		return (ENOMEM);
608 	IOMMU_DOMAIN_LOCK(domain);
609 	error = iommu_gas_find_space(domain, common, size, offset, flags,
610 	    entry);
611 	if (error == ENOMEM) {
612 		IOMMU_DOMAIN_UNLOCK(domain);
613 		iommu_gas_free_entry(domain, entry);
614 		return (error);
615 	}
616 #ifdef INVARIANTS
617 	if (iommu_check_free)
618 		iommu_gas_check_free(domain);
619 #endif
620 	KASSERT(error == 0,
621 	    ("unexpected error %d from iommu_gas_find_entry", error));
622 	KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
623 	    (uintmax_t)entry->end, (uintmax_t)domain->end));
624 	entry->flags |= eflags;
625 	IOMMU_DOMAIN_UNLOCK(domain);
626 
627 	error = domain->ops->map(domain, entry->start,
628 	    entry->end - entry->start, ma, eflags,
629 	    ((flags & IOMMU_MF_CANWAIT) != 0 ?  IOMMU_PGF_WAITOK : 0));
630 	if (error == ENOMEM) {
631 		iommu_domain_unload_entry(entry, true);
632 		return (error);
633 	}
634 	KASSERT(error == 0,
635 	    ("unexpected error %d from domain_map_buf", error));
636 
637 	*res = entry;
638 	return (0);
639 }
640 
641 int
642 iommu_gas_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
643     u_int eflags, u_int flags, vm_page_t *ma)
644 {
645 	iommu_gaddr_t start;
646 	int error;
647 
648 	KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
649 	    entry, entry->flags));
650 	KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_RMRR)) == 0,
651 	    ("invalid flags 0x%x", flags));
652 
653 	start = entry->start;
654 	IOMMU_DOMAIN_LOCK(domain);
655 	error = iommu_gas_alloc_region(domain, entry, flags);
656 	if (error != 0) {
657 		IOMMU_DOMAIN_UNLOCK(domain);
658 		return (error);
659 	}
660 	entry->flags |= eflags;
661 	IOMMU_DOMAIN_UNLOCK(domain);
662 	if (entry->end == entry->start)
663 		return (0);
664 
665 	error = domain->ops->map(domain, entry->start,
666 	    entry->end - entry->start, ma + OFF_TO_IDX(start - entry->start),
667 	    eflags, ((flags & IOMMU_MF_CANWAIT) != 0 ? IOMMU_PGF_WAITOK : 0));
668 	if (error == ENOMEM) {
669 		iommu_domain_unload_entry(entry, false);
670 		return (error);
671 	}
672 	KASSERT(error == 0,
673 	    ("unexpected error %d from domain_map_buf", error));
674 
675 	return (0);
676 }
677 
678 int
679 iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
680     iommu_gaddr_t end, struct iommu_map_entry **entry0)
681 {
682 	struct iommu_map_entry *entry;
683 	int error;
684 
685 	entry = iommu_gas_alloc_entry(domain, IOMMU_PGF_WAITOK);
686 	entry->start = start;
687 	entry->end = end;
688 	IOMMU_DOMAIN_LOCK(domain);
689 	error = iommu_gas_alloc_region(domain, entry, IOMMU_MF_CANWAIT);
690 	if (error == 0)
691 		entry->flags |= IOMMU_MAP_ENTRY_UNMAPPED;
692 	IOMMU_DOMAIN_UNLOCK(domain);
693 	if (error != 0)
694 		iommu_gas_free_entry(domain, entry);
695 	else if (entry0 != NULL)
696 		*entry0 = entry;
697 	return (error);
698 }
699 
700 struct iommu_map_entry *
701 iommu_map_alloc_entry(struct iommu_domain *domain, u_int flags)
702 {
703 	struct iommu_map_entry *res;
704 
705 	res = iommu_gas_alloc_entry(domain, flags);
706 
707 	return (res);
708 }
709 
710 void
711 iommu_map_free_entry(struct iommu_domain *domain, struct iommu_map_entry *entry)
712 {
713 
714 	iommu_gas_free_entry(domain, entry);
715 }
716 
717 int
718 iommu_map(struct iommu_domain *domain,
719     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
720     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res)
721 {
722 	int error;
723 
724 	error = iommu_gas_map(domain, common, size, offset, eflags, flags,
725 	    ma, res);
726 
727 	return (error);
728 }
729 
730 void
731 iommu_unmap_msi(struct iommu_ctx *ctx)
732 {
733 	struct iommu_map_entry *entry;
734 	struct iommu_domain *domain;
735 
736 	domain = ctx->domain;
737 	entry = domain->msi_entry;
738 	if (entry == NULL)
739 		return;
740 
741 	domain->ops->unmap(domain, entry->start, entry->end -
742 	    entry->start, IOMMU_PGF_WAITOK);
743 
744 	IOMMU_DOMAIN_LOCK(domain);
745 	iommu_gas_free_space(domain, entry);
746 	IOMMU_DOMAIN_UNLOCK(domain);
747 
748 	iommu_gas_free_entry(domain, entry);
749 
750 	domain->msi_entry = NULL;
751 	domain->msi_base = 0;
752 	domain->msi_phys = 0;
753 }
754 
755 int
756 iommu_map_msi(struct iommu_ctx *ctx, iommu_gaddr_t size, int offset,
757     u_int eflags, u_int flags, vm_page_t *ma)
758 {
759 	struct iommu_domain *domain;
760 	struct iommu_map_entry *entry;
761 	int error;
762 
763 	error = 0;
764 	domain = ctx->domain;
765 
766 	/* Check if there is already an MSI page allocated */
767 	IOMMU_DOMAIN_LOCK(domain);
768 	entry = domain->msi_entry;
769 	IOMMU_DOMAIN_UNLOCK(domain);
770 
771 	if (entry == NULL) {
772 		error = iommu_gas_map(domain, &ctx->tag->common, size, offset,
773 		    eflags, flags, ma, &entry);
774 		IOMMU_DOMAIN_LOCK(domain);
775 		if (error == 0) {
776 			if (domain->msi_entry == NULL) {
777 				MPASS(domain->msi_base == 0);
778 				MPASS(domain->msi_phys == 0);
779 
780 				domain->msi_entry = entry;
781 				domain->msi_base = entry->start;
782 				domain->msi_phys = VM_PAGE_TO_PHYS(ma[0]);
783 			} else {
784 				/*
785 				 * We lost the race and already have an
786 				 * MSI page allocated. Free the unneeded entry.
787 				 */
788 				iommu_gas_free_entry(domain, entry);
789 			}
790 		} else if (domain->msi_entry != NULL) {
791 			/*
792 			 * The allocation failed, but another succeeded.
793 			 * Return success as there is a valid MSI page.
794 			 */
795 			error = 0;
796 		}
797 		IOMMU_DOMAIN_UNLOCK(domain);
798 	}
799 
800 	return (error);
801 }
802 
803 void
804 iommu_translate_msi(struct iommu_domain *domain, uint64_t *addr)
805 {
806 
807 	*addr = (*addr - domain->msi_phys) + domain->msi_base;
808 
809 	KASSERT(*addr >= domain->msi_entry->start,
810 	    ("%s: Address is below the MSI entry start address (%jx < %jx)",
811 	    __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->start));
812 
813 	KASSERT(*addr + sizeof(*addr) <= domain->msi_entry->end,
814 	    ("%s: Address is above the MSI entry end address (%jx < %jx)",
815 	    __func__, (uintmax_t)*addr, (uintmax_t)domain->msi_entry->end));
816 }
817 
818 int
819 iommu_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
820     u_int eflags, u_int flags, vm_page_t *ma)
821 {
822 	int error;
823 
824 	error = iommu_gas_map_region(domain, entry, eflags, flags, ma);
825 
826 	return (error);
827 }
828 
829 SYSCTL_NODE(_hw, OID_AUTO, iommu, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, "");
830 
831 #ifdef INVARIANTS
832 SYSCTL_INT(_hw_iommu, OID_AUTO, check_free, CTLFLAG_RWTUN,
833     &iommu_check_free, 0,
834     "Check the GPA RBtree for free_down and free_after validity");
835 #endif
836