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) dmar_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>
55 #include <dev/pci/pcivar.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_map.h>
63 #include <machine/atomic.h>
64 #include <machine/bus.h>
65 #include <machine/md_var.h>
66 #include <machine/specialreg.h>
67 #include <x86/include/busdma_impl.h>
68 #include <x86/iommu/intel_reg.h>
69 #include <x86/iommu/busdma_dmar.h>
70 #include <x86/iommu/intel_dmar.h>
73 * Guest Address Space management.
76 static uma_zone_t dmar_map_entry_zone;
82 dmar_map_entry_zone = uma_zcreate("DMAR_MAP_ENTRY",
83 sizeof(struct dmar_map_entry), NULL, NULL,
84 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NODUMP);
86 SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
88 struct dmar_map_entry *
89 dmar_gas_alloc_entry(struct dmar_domain *domain, u_int flags)
91 struct dmar_map_entry *res;
93 KASSERT((flags & ~(DMAR_PGF_WAITOK)) == 0,
94 ("unsupported flags %x", flags));
96 res = uma_zalloc(dmar_map_entry_zone, ((flags & DMAR_PGF_WAITOK) !=
97 0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
100 atomic_add_int(&domain->entries_cnt, 1);
106 dmar_gas_free_entry(struct dmar_domain *domain, struct dmar_map_entry *entry)
109 KASSERT(domain == entry->domain,
110 ("mismatched free domain %p entry %p entry->domain %p", domain,
111 entry, entry->domain));
112 atomic_subtract_int(&domain->entries_cnt, 1);
113 uma_zfree(dmar_map_entry_zone, entry);
117 dmar_gas_cmp_entries(struct dmar_map_entry *a, struct dmar_map_entry *b)
120 /* Last entry have zero size, so <= */
121 KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
122 a, (uintmax_t)a->start, (uintmax_t)a->end));
123 KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
124 b, (uintmax_t)b->start, (uintmax_t)b->end));
125 KASSERT(a->end <= b->start || b->end <= a->start ||
126 a->end == a->start || b->end == b->start,
127 ("overlapping entries %p (%jx, %jx) %p (%jx, %jx)",
128 a, (uintmax_t)a->start, (uintmax_t)a->end,
129 b, (uintmax_t)b->start, (uintmax_t)b->end));
133 else if (b->end < a->end)
139 dmar_gas_augment_entry(struct dmar_map_entry *entry)
141 struct dmar_map_entry *l, *r;
143 for (; entry != NULL; entry = RB_PARENT(entry, rb_entry)) {
144 l = RB_LEFT(entry, rb_entry);
145 r = RB_RIGHT(entry, rb_entry);
146 if (l == NULL && r == NULL) {
147 entry->free_down = entry->free_after;
148 } else if (l == NULL && r != NULL) {
149 entry->free_down = MAX(entry->free_after, r->free_down);
150 } else if (/*l != NULL && */ r == NULL) {
151 entry->free_down = MAX(entry->free_after, l->free_down);
152 } else /* if (l != NULL && r != NULL) */ {
153 entry->free_down = MAX(entry->free_after, l->free_down);
154 entry->free_down = MAX(entry->free_down, r->free_down);
159 RB_GENERATE(dmar_gas_entries_tree, dmar_map_entry, rb_entry,
160 dmar_gas_cmp_entries);
163 dmar_gas_fix_free(struct dmar_domain *domain, struct dmar_map_entry *entry)
165 struct dmar_map_entry *next;
167 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
168 entry->free_after = (next != NULL ? next->start : domain->end) -
170 dmar_gas_augment_entry(entry);
175 dmar_gas_check_free(struct dmar_domain *domain)
177 struct dmar_map_entry *entry, *next, *l, *r;
180 RB_FOREACH(entry, dmar_gas_entries_tree, &domain->rb_root) {
181 KASSERT(domain == entry->domain,
182 ("mismatched free domain %p entry %p entry->domain %p",
183 domain, entry, entry->domain));
184 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
186 MPASS(entry->free_after == domain->end - entry->end);
188 MPASS(entry->free_after = next->start - entry->end);
189 MPASS(entry->end <= next->start);
191 l = RB_LEFT(entry, rb_entry);
192 r = RB_RIGHT(entry, rb_entry);
193 if (l == NULL && r == NULL) {
194 MPASS(entry->free_down == entry->free_after);
195 } else if (l == NULL && r != NULL) {
196 MPASS(entry->free_down = MAX(entry->free_after,
198 } else if (r == NULL) {
199 MPASS(entry->free_down = MAX(entry->free_after,
202 v = MAX(entry->free_after, l->free_down);
203 v = MAX(v, r->free_down);
204 MPASS(entry->free_down == v);
211 dmar_gas_rb_insert(struct dmar_domain *domain, struct dmar_map_entry *entry)
213 struct dmar_map_entry *prev, *found;
215 found = RB_INSERT(dmar_gas_entries_tree, &domain->rb_root, entry);
216 dmar_gas_fix_free(domain, entry);
217 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
219 dmar_gas_fix_free(domain, prev);
220 return (found == NULL);
224 dmar_gas_rb_remove(struct dmar_domain *domain, struct dmar_map_entry *entry)
226 struct dmar_map_entry *prev;
228 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
229 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
231 dmar_gas_fix_free(domain, prev);
235 dmar_gas_init_domain(struct dmar_domain *domain)
237 struct dmar_map_entry *begin, *end;
239 begin = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
240 end = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
242 DMAR_DOMAIN_LOCK(domain);
243 KASSERT(domain->entries_cnt == 2, ("dirty domain %p", domain));
244 KASSERT(RB_EMPTY(&domain->rb_root), ("non-empty entries %p", domain));
247 begin->end = DMAR_PAGE_SIZE;
248 begin->free_after = domain->end - begin->end;
249 begin->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
250 dmar_gas_rb_insert(domain, begin);
252 end->start = domain->end;
253 end->end = domain->end;
255 end->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
256 dmar_gas_rb_insert(domain, end);
258 domain->first_place = begin;
259 domain->last_place = end;
260 domain->flags |= DMAR_DOMAIN_GAS_INITED;
261 DMAR_DOMAIN_UNLOCK(domain);
265 dmar_gas_fini_domain(struct dmar_domain *domain)
267 struct dmar_map_entry *entry, *entry1;
269 DMAR_DOMAIN_ASSERT_LOCKED(domain);
270 KASSERT(domain->entries_cnt == 2, ("domain still in use %p", domain));
272 entry = RB_MIN(dmar_gas_entries_tree, &domain->rb_root);
273 KASSERT(entry->start == 0, ("start entry start %p", domain));
274 KASSERT(entry->end == DMAR_PAGE_SIZE, ("start entry end %p", domain));
275 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
276 ("start entry flags %p", domain));
277 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
278 dmar_gas_free_entry(domain, entry);
280 entry = RB_MAX(dmar_gas_entries_tree, &domain->rb_root);
281 KASSERT(entry->start == domain->end, ("end entry start %p", domain));
282 KASSERT(entry->end == domain->end, ("end entry end %p", domain));
283 KASSERT(entry->free_after == 0, ("end entry free_after %p", domain));
284 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
285 ("end entry flags %p", domain));
286 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
287 dmar_gas_free_entry(domain, entry);
289 RB_FOREACH_SAFE(entry, dmar_gas_entries_tree, &domain->rb_root,
291 KASSERT((entry->flags & DMAR_MAP_ENTRY_RMRR) != 0,
292 ("non-RMRR entry left %p", domain));
293 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
294 dmar_gas_free_entry(domain, entry);
298 struct dmar_gas_match_args {
299 struct dmar_domain *domain;
302 const struct bus_dma_tag_common *common;
304 struct dmar_map_entry *entry;
308 dmar_gas_match_one(struct dmar_gas_match_args *a, struct dmar_map_entry *prev,
311 dmar_gaddr_t bs, start;
313 if (a->entry->start + a->size > end)
316 /* DMAR_PAGE_SIZE to create gap after new entry. */
317 if (a->entry->start < prev->end + DMAR_PAGE_SIZE ||
318 a->entry->start + a->size + a->offset + DMAR_PAGE_SIZE >
319 prev->end + prev->free_after)
322 /* No boundary crossing. */
323 if (dmar_test_boundary(a->entry->start + a->offset, a->size,
324 a->common->boundary))
328 * The start + offset to start + offset + size region crosses
329 * the boundary. Check if there is enough space after the
330 * next boundary after the prev->end.
332 bs = rounddown2(a->entry->start + a->offset + a->common->boundary,
333 a->common->boundary);
334 start = roundup2(bs, a->common->alignment);
335 /* DMAR_PAGE_SIZE to create gap after new entry. */
336 if (start + a->offset + a->size + DMAR_PAGE_SIZE <=
337 prev->end + prev->free_after &&
338 start + a->offset + a->size <= end &&
339 dmar_test_boundary(start + a->offset, a->size,
340 a->common->boundary)) {
341 a->entry->start = start;
346 * Not enough space to align at the requested boundary, or
347 * boundary is smaller than the size, but allowed to split.
348 * We already checked that start + size does not overlap end.
350 * XXXKIB. It is possible that bs is exactly at the start of
351 * the next entry, then we do not have gap. Ignore for now.
353 if ((a->gas_flags & DMAR_GM_CANSPLIT) != 0) {
354 a->size = bs - a->entry->start;
362 dmar_gas_match_insert(struct dmar_gas_match_args *a,
363 struct dmar_map_entry *prev)
365 struct dmar_map_entry *next;
369 * The prev->end is always aligned on the page size, which
370 * causes page alignment for the entry->start too. The size
371 * is checked to be multiple of the page size.
373 * The page sized gap is created between consequent
374 * allocations to ensure that out-of-bounds accesses fault.
376 a->entry->end = a->entry->start + a->size;
378 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, prev);
379 KASSERT(next->start >= a->entry->end &&
380 next->start - a->entry->start >= a->size &&
381 prev->end <= a->entry->end,
382 ("dmar_gas_match_insert hole failed %p prev (%jx, %jx) "
383 "free_after %jx next (%jx, %jx) entry (%jx, %jx)", a->domain,
384 (uintmax_t)prev->start, (uintmax_t)prev->end,
385 (uintmax_t)prev->free_after,
386 (uintmax_t)next->start, (uintmax_t)next->end,
387 (uintmax_t)a->entry->start, (uintmax_t)a->entry->end));
389 prev->free_after = a->entry->start - prev->end;
390 a->entry->free_after = next->start - a->entry->end;
392 found = dmar_gas_rb_insert(a->domain, a->entry);
393 KASSERT(found, ("found dup %p start %jx size %jx",
394 a->domain, (uintmax_t)a->entry->start, (uintmax_t)a->size));
395 a->entry->flags = DMAR_MAP_ENTRY_MAP;
397 KASSERT(RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root,
399 ("entry %p prev %p inserted prev %p", a->entry, prev,
400 RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
401 KASSERT(RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
403 ("entry %p next %p inserted next %p", a->entry, next,
404 RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
408 dmar_gas_lowermatch(struct dmar_gas_match_args *a, struct dmar_map_entry *prev)
410 struct dmar_map_entry *l;
413 if (prev->end < a->common->lowaddr) {
414 a->entry->start = roundup2(prev->end + DMAR_PAGE_SIZE,
415 a->common->alignment);
416 if (dmar_gas_match_one(a, prev, a->common->lowaddr)) {
417 dmar_gas_match_insert(a, prev);
421 if (prev->free_down < a->size + a->offset + DMAR_PAGE_SIZE)
423 l = RB_LEFT(prev, rb_entry);
425 ret = dmar_gas_lowermatch(a, l);
429 l = RB_RIGHT(prev, rb_entry);
431 return (dmar_gas_lowermatch(a, l));
436 dmar_gas_uppermatch(struct dmar_gas_match_args *a)
438 struct dmar_map_entry *next, *prev, find_entry;
440 find_entry.start = a->common->highaddr;
441 next = RB_NFIND(dmar_gas_entries_tree, &a->domain->rb_root,
445 prev = RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, next);
446 KASSERT(prev != NULL, ("no prev %p %jx", a->domain,
447 (uintmax_t)find_entry.start));
449 a->entry->start = prev->start + DMAR_PAGE_SIZE;
450 if (a->entry->start < a->common->highaddr)
451 a->entry->start = a->common->highaddr;
452 a->entry->start = roundup2(a->entry->start,
453 a->common->alignment);
454 if (dmar_gas_match_one(a, prev, a->domain->end)) {
455 dmar_gas_match_insert(a, prev);
460 * XXXKIB. This falls back to linear iteration over
461 * the free space in the high region. But high
462 * regions are almost unused, the code should be
463 * enough to cover the case, although in the
467 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
469 KASSERT(next != NULL, ("no next %p %jx", a->domain,
470 (uintmax_t)find_entry.start));
471 if (next->end >= a->domain->end)
477 dmar_gas_find_space(struct dmar_domain *domain,
478 const struct bus_dma_tag_common *common, dmar_gaddr_t size,
479 int offset, u_int flags, struct dmar_map_entry *entry)
481 struct dmar_gas_match_args a;
484 DMAR_DOMAIN_ASSERT_LOCKED(domain);
485 KASSERT(entry->flags == 0, ("dirty entry %p %p", domain, entry));
486 KASSERT((size & DMAR_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
495 /* Handle lower region. */
496 if (common->lowaddr > 0) {
497 error = dmar_gas_lowermatch(&a, RB_ROOT(&domain->rb_root));
500 KASSERT(error == ENOMEM,
501 ("error %d from dmar_gas_lowermatch", error));
503 /* Handle upper region. */
504 if (common->highaddr >= domain->end)
506 error = dmar_gas_uppermatch(&a);
507 KASSERT(error == ENOMEM,
508 ("error %d from dmar_gas_uppermatch", error));
513 dmar_gas_alloc_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
516 struct dmar_map_entry *next, *prev;
519 DMAR_DOMAIN_ASSERT_LOCKED(domain);
521 if ((entry->start & DMAR_PAGE_MASK) != 0 ||
522 (entry->end & DMAR_PAGE_MASK) != 0)
524 if (entry->start >= entry->end)
526 if (entry->end >= domain->end)
529 next = RB_NFIND(dmar_gas_entries_tree, &domain->rb_root, entry);
530 KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
531 (uintmax_t)entry->start));
532 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, next);
533 /* prev could be NULL */
536 * Adapt to broken BIOSes which specify overlapping RMRR
539 * XXXKIB: this does not handle a case when prev or next
540 * entries are completely covered by the current one, which
543 if (prev != NULL && prev->end > entry->start &&
544 (prev->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
545 if ((prev->flags & DMAR_MAP_ENTRY_RMRR) == 0)
547 entry->start = prev->end;
549 if (next != NULL && next->start < entry->end &&
550 (next->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
551 if ((next->flags & DMAR_MAP_ENTRY_RMRR) == 0)
553 entry->end = next->start;
555 if (entry->end == entry->start)
558 if (prev != NULL && prev->end > entry->start) {
559 /* This assumes that prev is the placeholder entry. */
560 dmar_gas_rb_remove(domain, prev);
563 if (next != NULL && next->start < entry->end) {
564 dmar_gas_rb_remove(domain, next);
568 found = dmar_gas_rb_insert(domain, entry);
569 KASSERT(found, ("found RMRR dup %p start %jx end %jx",
570 domain, (uintmax_t)entry->start, (uintmax_t)entry->end));
571 entry->flags = DMAR_MAP_ENTRY_RMRR;
574 struct dmar_map_entry *ip, *in;
575 ip = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
576 in = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
577 KASSERT(prev == NULL || ip == prev,
578 ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
579 entry, entry->start, entry->end, prev,
580 prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
581 ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
582 KASSERT(next == NULL || in == next,
583 ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
584 entry, entry->start, entry->end, next,
585 next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
586 in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
593 dmar_gas_free_space(struct dmar_domain *domain, struct dmar_map_entry *entry)
596 DMAR_DOMAIN_ASSERT_LOCKED(domain);
597 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
598 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_MAP,
599 ("permanent entry %p %p", domain, entry));
601 dmar_gas_rb_remove(domain, entry);
602 entry->flags &= ~DMAR_MAP_ENTRY_MAP;
605 dmar_gas_check_free(domain);
610 dmar_gas_free_region(struct dmar_domain *domain, struct dmar_map_entry *entry)
612 struct dmar_map_entry *next, *prev;
614 DMAR_DOMAIN_ASSERT_LOCKED(domain);
615 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
616 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_RMRR,
617 ("non-RMRR entry %p %p", domain, entry));
619 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
620 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
621 dmar_gas_rb_remove(domain, entry);
622 entry->flags &= ~DMAR_MAP_ENTRY_RMRR;
625 dmar_gas_rb_insert(domain, domain->first_place);
627 dmar_gas_rb_insert(domain, domain->last_place);
631 dmar_gas_map(struct dmar_domain *domain,
632 const struct bus_dma_tag_common *common, dmar_gaddr_t size, int offset,
633 u_int eflags, u_int flags, vm_page_t *ma, struct dmar_map_entry **res)
635 struct dmar_map_entry *entry;
638 KASSERT((flags & ~(DMAR_GM_CANWAIT | DMAR_GM_CANSPLIT)) == 0,
639 ("invalid flags 0x%x", flags));
641 entry = dmar_gas_alloc_entry(domain, (flags & DMAR_GM_CANWAIT) != 0 ?
642 DMAR_PGF_WAITOK : 0);
645 DMAR_DOMAIN_LOCK(domain);
646 error = dmar_gas_find_space(domain, common, size, offset, flags,
648 if (error == ENOMEM) {
649 DMAR_DOMAIN_UNLOCK(domain);
650 dmar_gas_free_entry(domain, entry);
655 dmar_gas_check_free(domain);
658 ("unexpected error %d from dmar_gas_find_entry", error));
659 KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
660 (uintmax_t)entry->end, (uintmax_t)domain->end));
661 entry->flags |= eflags;
662 DMAR_DOMAIN_UNLOCK(domain);
664 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
666 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
667 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
668 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
669 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
670 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
671 if (error == ENOMEM) {
672 dmar_domain_unload_entry(entry, true);
676 ("unexpected error %d from domain_map_buf", error));
683 dmar_gas_map_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
684 u_int eflags, u_int flags, vm_page_t *ma)
689 KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
690 entry, entry->flags));
691 KASSERT((flags & ~(DMAR_GM_CANWAIT)) == 0,
692 ("invalid flags 0x%x", flags));
694 start = entry->start;
695 DMAR_DOMAIN_LOCK(domain);
696 error = dmar_gas_alloc_region(domain, entry, flags);
698 DMAR_DOMAIN_UNLOCK(domain);
701 entry->flags |= eflags;
702 DMAR_DOMAIN_UNLOCK(domain);
703 if (entry->end == entry->start)
706 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
707 ma + OFF_TO_IDX(start - entry->start),
708 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
709 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
710 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
711 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
712 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
713 if (error == ENOMEM) {
714 dmar_domain_unload_entry(entry, false);
718 ("unexpected error %d from domain_map_buf", error));
724 dmar_gas_reserve_region(struct dmar_domain *domain, dmar_gaddr_t start,
727 struct dmar_map_entry *entry;
730 entry = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
731 entry->start = start;
733 DMAR_DOMAIN_LOCK(domain);
734 error = dmar_gas_alloc_region(domain, entry, DMAR_GM_CANWAIT);
736 entry->flags |= DMAR_MAP_ENTRY_UNMAPPED;
737 DMAR_DOMAIN_UNLOCK(domain);
739 dmar_gas_free_entry(domain, entry);