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 <dev/pci/pcireg.h>
71 #include <x86/iommu/intel_dmar.h>
74 * Guest Address Space management.
77 static uma_zone_t dmar_map_entry_zone;
83 dmar_map_entry_zone = uma_zcreate("DMAR_MAP_ENTRY",
84 sizeof(struct dmar_map_entry), NULL, NULL,
85 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NODUMP);
87 SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
89 struct dmar_map_entry *
90 dmar_gas_alloc_entry(struct dmar_domain *domain, u_int flags)
92 struct dmar_map_entry *res;
94 KASSERT((flags & ~(DMAR_PGF_WAITOK)) == 0,
95 ("unsupported flags %x", flags));
97 res = uma_zalloc(dmar_map_entry_zone, ((flags & DMAR_PGF_WAITOK) !=
98 0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
100 res->domain = domain;
101 atomic_add_int(&domain->entries_cnt, 1);
107 dmar_gas_free_entry(struct dmar_domain *domain, struct dmar_map_entry *entry)
110 KASSERT(domain == entry->domain,
111 ("mismatched free domain %p entry %p entry->domain %p", domain,
112 entry, entry->domain));
113 atomic_subtract_int(&domain->entries_cnt, 1);
114 uma_zfree(dmar_map_entry_zone, entry);
118 dmar_gas_cmp_entries(struct dmar_map_entry *a, struct dmar_map_entry *b)
121 /* Last entry have zero size, so <= */
122 KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
123 a, (uintmax_t)a->start, (uintmax_t)a->end));
124 KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
125 b, (uintmax_t)b->start, (uintmax_t)b->end));
126 KASSERT(a->end <= b->start || b->end <= a->start ||
127 a->end == a->start || b->end == b->start,
128 ("overlapping entries %p (%jx, %jx) %p (%jx, %jx)",
129 a, (uintmax_t)a->start, (uintmax_t)a->end,
130 b, (uintmax_t)b->start, (uintmax_t)b->end));
134 else if (b->end < a->end)
140 dmar_gas_augment_entry(struct dmar_map_entry *entry)
142 struct dmar_map_entry *l, *r;
144 for (; entry != NULL; entry = RB_PARENT(entry, rb_entry)) {
145 l = RB_LEFT(entry, rb_entry);
146 r = RB_RIGHT(entry, rb_entry);
147 if (l == NULL && r == NULL) {
148 entry->free_down = entry->free_after;
149 } else if (l == NULL && r != NULL) {
150 entry->free_down = MAX(entry->free_after, r->free_down);
151 } else if (/*l != NULL && */ r == NULL) {
152 entry->free_down = MAX(entry->free_after, l->free_down);
153 } else /* if (l != NULL && r != NULL) */ {
154 entry->free_down = MAX(entry->free_after, l->free_down);
155 entry->free_down = MAX(entry->free_down, r->free_down);
160 RB_GENERATE(dmar_gas_entries_tree, dmar_map_entry, rb_entry,
161 dmar_gas_cmp_entries);
164 dmar_gas_fix_free(struct dmar_domain *domain, struct dmar_map_entry *entry)
166 struct dmar_map_entry *next;
168 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
169 entry->free_after = (next != NULL ? next->start : domain->end) -
171 dmar_gas_augment_entry(entry);
176 dmar_gas_check_free(struct dmar_domain *domain)
178 struct dmar_map_entry *entry, *next, *l, *r;
181 RB_FOREACH(entry, dmar_gas_entries_tree, &domain->rb_root) {
182 KASSERT(domain == entry->domain,
183 ("mismatched free domain %p entry %p entry->domain %p",
184 domain, entry, entry->domain));
185 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
187 MPASS(entry->free_after == domain->end - entry->end);
189 MPASS(entry->free_after = next->start - entry->end);
190 MPASS(entry->end <= next->start);
192 l = RB_LEFT(entry, rb_entry);
193 r = RB_RIGHT(entry, rb_entry);
194 if (l == NULL && r == NULL) {
195 MPASS(entry->free_down == entry->free_after);
196 } else if (l == NULL && r != NULL) {
197 MPASS(entry->free_down = MAX(entry->free_after,
199 } else if (r == NULL) {
200 MPASS(entry->free_down = MAX(entry->free_after,
203 v = MAX(entry->free_after, l->free_down);
204 v = MAX(v, r->free_down);
205 MPASS(entry->free_down == v);
212 dmar_gas_rb_insert(struct dmar_domain *domain, struct dmar_map_entry *entry)
214 struct dmar_map_entry *prev, *found;
216 found = RB_INSERT(dmar_gas_entries_tree, &domain->rb_root, entry);
217 dmar_gas_fix_free(domain, entry);
218 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
220 dmar_gas_fix_free(domain, prev);
221 return (found == NULL);
225 dmar_gas_rb_remove(struct dmar_domain *domain, struct dmar_map_entry *entry)
227 struct dmar_map_entry *prev;
229 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
230 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
232 dmar_gas_fix_free(domain, prev);
236 dmar_gas_init_domain(struct dmar_domain *domain)
238 struct dmar_map_entry *begin, *end;
240 begin = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
241 end = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
243 DMAR_DOMAIN_LOCK(domain);
244 KASSERT(domain->entries_cnt == 2, ("dirty domain %p", domain));
245 KASSERT(RB_EMPTY(&domain->rb_root), ("non-empty entries %p", domain));
248 begin->end = DMAR_PAGE_SIZE;
249 begin->free_after = domain->end - begin->end;
250 begin->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
251 dmar_gas_rb_insert(domain, begin);
253 end->start = domain->end;
254 end->end = domain->end;
256 end->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
257 dmar_gas_rb_insert(domain, end);
259 domain->first_place = begin;
260 domain->last_place = end;
261 domain->flags |= DMAR_DOMAIN_GAS_INITED;
262 DMAR_DOMAIN_UNLOCK(domain);
266 dmar_gas_fini_domain(struct dmar_domain *domain)
268 struct dmar_map_entry *entry, *entry1;
270 DMAR_DOMAIN_ASSERT_LOCKED(domain);
271 KASSERT(domain->entries_cnt == 2, ("domain still in use %p", domain));
273 entry = RB_MIN(dmar_gas_entries_tree, &domain->rb_root);
274 KASSERT(entry->start == 0, ("start entry start %p", domain));
275 KASSERT(entry->end == DMAR_PAGE_SIZE, ("start entry end %p", domain));
276 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
277 ("start entry flags %p", domain));
278 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
279 dmar_gas_free_entry(domain, entry);
281 entry = RB_MAX(dmar_gas_entries_tree, &domain->rb_root);
282 KASSERT(entry->start == domain->end, ("end entry start %p", domain));
283 KASSERT(entry->end == domain->end, ("end entry end %p", domain));
284 KASSERT(entry->free_after == 0, ("end entry free_after %p", domain));
285 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
286 ("end entry flags %p", domain));
287 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
288 dmar_gas_free_entry(domain, entry);
290 RB_FOREACH_SAFE(entry, dmar_gas_entries_tree, &domain->rb_root,
292 KASSERT((entry->flags & DMAR_MAP_ENTRY_RMRR) != 0,
293 ("non-RMRR entry left %p", domain));
294 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
295 dmar_gas_free_entry(domain, entry);
299 struct dmar_gas_match_args {
300 struct dmar_domain *domain;
303 const struct bus_dma_tag_common *common;
305 struct dmar_map_entry *entry;
309 dmar_gas_match_one(struct dmar_gas_match_args *a, struct dmar_map_entry *prev,
312 dmar_gaddr_t bs, start;
314 if (a->entry->start + a->size > end)
317 /* DMAR_PAGE_SIZE to create gap after new entry. */
318 if (a->entry->start < prev->end + DMAR_PAGE_SIZE ||
319 a->entry->start + a->size + a->offset + DMAR_PAGE_SIZE >
320 prev->end + prev->free_after)
323 /* No boundary crossing. */
324 if (dmar_test_boundary(a->entry->start + a->offset, a->size,
325 a->common->boundary))
329 * The start + offset to start + offset + size region crosses
330 * the boundary. Check if there is enough space after the
331 * next boundary after the prev->end.
333 bs = rounddown2(a->entry->start + a->offset + a->common->boundary,
334 a->common->boundary);
335 start = roundup2(bs, a->common->alignment);
336 /* DMAR_PAGE_SIZE to create gap after new entry. */
337 if (start + a->offset + a->size + DMAR_PAGE_SIZE <=
338 prev->end + prev->free_after &&
339 start + a->offset + a->size <= end &&
340 dmar_test_boundary(start + a->offset, a->size,
341 a->common->boundary)) {
342 a->entry->start = start;
347 * Not enough space to align at the requested boundary, or
348 * boundary is smaller than the size, but allowed to split.
349 * We already checked that start + size does not overlap end.
351 * XXXKIB. It is possible that bs is exactly at the start of
352 * the next entry, then we do not have gap. Ignore for now.
354 if ((a->gas_flags & DMAR_GM_CANSPLIT) != 0) {
355 a->size = bs - a->entry->start;
363 dmar_gas_match_insert(struct dmar_gas_match_args *a,
364 struct dmar_map_entry *prev)
366 struct dmar_map_entry *next;
370 * The prev->end is always aligned on the page size, which
371 * causes page alignment for the entry->start too. The size
372 * is checked to be multiple of the page size.
374 * The page sized gap is created between consequent
375 * allocations to ensure that out-of-bounds accesses fault.
377 a->entry->end = a->entry->start + a->size;
379 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, prev);
380 KASSERT(next->start >= a->entry->end &&
381 next->start - a->entry->start >= a->size &&
382 prev->end <= a->entry->end,
383 ("dmar_gas_match_insert hole failed %p prev (%jx, %jx) "
384 "free_after %jx next (%jx, %jx) entry (%jx, %jx)", a->domain,
385 (uintmax_t)prev->start, (uintmax_t)prev->end,
386 (uintmax_t)prev->free_after,
387 (uintmax_t)next->start, (uintmax_t)next->end,
388 (uintmax_t)a->entry->start, (uintmax_t)a->entry->end));
390 prev->free_after = a->entry->start - prev->end;
391 a->entry->free_after = next->start - a->entry->end;
393 found = dmar_gas_rb_insert(a->domain, a->entry);
394 KASSERT(found, ("found dup %p start %jx size %jx",
395 a->domain, (uintmax_t)a->entry->start, (uintmax_t)a->size));
396 a->entry->flags = DMAR_MAP_ENTRY_MAP;
398 KASSERT(RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root,
400 ("entry %p prev %p inserted prev %p", a->entry, prev,
401 RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
402 KASSERT(RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
404 ("entry %p next %p inserted next %p", a->entry, next,
405 RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
409 dmar_gas_lowermatch(struct dmar_gas_match_args *a, struct dmar_map_entry *prev)
411 struct dmar_map_entry *l;
414 if (prev->end < a->common->lowaddr) {
415 a->entry->start = roundup2(prev->end + DMAR_PAGE_SIZE,
416 a->common->alignment);
417 if (dmar_gas_match_one(a, prev, a->common->lowaddr)) {
418 dmar_gas_match_insert(a, prev);
422 if (prev->free_down < a->size + a->offset + DMAR_PAGE_SIZE)
424 l = RB_LEFT(prev, rb_entry);
426 ret = dmar_gas_lowermatch(a, l);
430 l = RB_RIGHT(prev, rb_entry);
432 return (dmar_gas_lowermatch(a, l));
437 dmar_gas_uppermatch(struct dmar_gas_match_args *a)
439 struct dmar_map_entry *next, *prev, find_entry;
441 find_entry.start = a->common->highaddr;
442 next = RB_NFIND(dmar_gas_entries_tree, &a->domain->rb_root,
446 prev = RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, next);
447 KASSERT(prev != NULL, ("no prev %p %jx", a->domain,
448 (uintmax_t)find_entry.start));
450 a->entry->start = prev->start + DMAR_PAGE_SIZE;
451 if (a->entry->start < a->common->highaddr)
452 a->entry->start = a->common->highaddr;
453 a->entry->start = roundup2(a->entry->start,
454 a->common->alignment);
455 if (dmar_gas_match_one(a, prev, a->domain->end)) {
456 dmar_gas_match_insert(a, prev);
461 * XXXKIB. This falls back to linear iteration over
462 * the free space in the high region. But high
463 * regions are almost unused, the code should be
464 * enough to cover the case, although in the
468 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
470 KASSERT(next != NULL, ("no next %p %jx", a->domain,
471 (uintmax_t)find_entry.start));
472 if (next->end >= a->domain->end)
478 dmar_gas_find_space(struct dmar_domain *domain,
479 const struct bus_dma_tag_common *common, dmar_gaddr_t size,
480 int offset, u_int flags, struct dmar_map_entry *entry)
482 struct dmar_gas_match_args a;
485 DMAR_DOMAIN_ASSERT_LOCKED(domain);
486 KASSERT(entry->flags == 0, ("dirty entry %p %p", domain, entry));
487 KASSERT((size & DMAR_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
496 /* Handle lower region. */
497 if (common->lowaddr > 0) {
498 error = dmar_gas_lowermatch(&a, RB_ROOT(&domain->rb_root));
501 KASSERT(error == ENOMEM,
502 ("error %d from dmar_gas_lowermatch", error));
504 /* Handle upper region. */
505 if (common->highaddr >= domain->end)
507 error = dmar_gas_uppermatch(&a);
508 KASSERT(error == ENOMEM,
509 ("error %d from dmar_gas_uppermatch", error));
514 dmar_gas_alloc_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
517 struct dmar_map_entry *next, *prev;
520 DMAR_DOMAIN_ASSERT_LOCKED(domain);
522 if ((entry->start & DMAR_PAGE_MASK) != 0 ||
523 (entry->end & DMAR_PAGE_MASK) != 0)
525 if (entry->start >= entry->end)
527 if (entry->end >= domain->end)
530 next = RB_NFIND(dmar_gas_entries_tree, &domain->rb_root, entry);
531 KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
532 (uintmax_t)entry->start));
533 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, next);
534 /* prev could be NULL */
537 * Adapt to broken BIOSes which specify overlapping RMRR
540 * XXXKIB: this does not handle a case when prev or next
541 * entries are completely covered by the current one, which
544 if (prev != NULL && prev->end > entry->start &&
545 (prev->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
546 if ((prev->flags & DMAR_MAP_ENTRY_RMRR) == 0)
548 entry->start = prev->end;
550 if (next->start < entry->end &&
551 (next->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
552 if ((next->flags & DMAR_MAP_ENTRY_RMRR) == 0)
554 entry->end = next->start;
556 if (entry->end == entry->start)
559 if (prev != NULL && prev->end > entry->start) {
560 /* This assumes that prev is the placeholder entry. */
561 dmar_gas_rb_remove(domain, prev);
564 if (next->start < entry->end) {
565 dmar_gas_rb_remove(domain, next);
569 found = dmar_gas_rb_insert(domain, entry);
570 KASSERT(found, ("found RMRR dup %p start %jx end %jx",
571 domain, (uintmax_t)entry->start, (uintmax_t)entry->end));
572 entry->flags = DMAR_MAP_ENTRY_RMRR;
575 struct dmar_map_entry *ip, *in;
576 ip = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
577 in = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
578 KASSERT(prev == NULL || ip == prev,
579 ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
580 entry, entry->start, entry->end, prev,
581 prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
582 ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
583 KASSERT(next == NULL || in == next,
584 ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
585 entry, entry->start, entry->end, next,
586 next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
587 in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
594 dmar_gas_free_space(struct dmar_domain *domain, struct dmar_map_entry *entry)
597 DMAR_DOMAIN_ASSERT_LOCKED(domain);
598 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
599 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_MAP,
600 ("permanent entry %p %p", domain, entry));
602 dmar_gas_rb_remove(domain, entry);
603 entry->flags &= ~DMAR_MAP_ENTRY_MAP;
606 dmar_gas_check_free(domain);
611 dmar_gas_free_region(struct dmar_domain *domain, struct dmar_map_entry *entry)
613 struct dmar_map_entry *next, *prev;
615 DMAR_DOMAIN_ASSERT_LOCKED(domain);
616 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
617 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_RMRR,
618 ("non-RMRR entry %p %p", domain, entry));
620 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
621 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
622 dmar_gas_rb_remove(domain, entry);
623 entry->flags &= ~DMAR_MAP_ENTRY_RMRR;
626 dmar_gas_rb_insert(domain, domain->first_place);
628 dmar_gas_rb_insert(domain, domain->last_place);
632 dmar_gas_map(struct dmar_domain *domain,
633 const struct bus_dma_tag_common *common, dmar_gaddr_t size, int offset,
634 u_int eflags, u_int flags, vm_page_t *ma, struct dmar_map_entry **res)
636 struct dmar_map_entry *entry;
639 KASSERT((flags & ~(DMAR_GM_CANWAIT | DMAR_GM_CANSPLIT)) == 0,
640 ("invalid flags 0x%x", flags));
642 entry = dmar_gas_alloc_entry(domain, (flags & DMAR_GM_CANWAIT) != 0 ?
643 DMAR_PGF_WAITOK : 0);
646 DMAR_DOMAIN_LOCK(domain);
647 error = dmar_gas_find_space(domain, common, size, offset, flags,
649 if (error == ENOMEM) {
650 DMAR_DOMAIN_UNLOCK(domain);
651 dmar_gas_free_entry(domain, entry);
656 dmar_gas_check_free(domain);
659 ("unexpected error %d from dmar_gas_find_entry", error));
660 KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
661 (uintmax_t)entry->end, (uintmax_t)domain->end));
662 entry->flags |= eflags;
663 DMAR_DOMAIN_UNLOCK(domain);
665 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
667 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
668 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
669 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
670 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
671 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
672 if (error == ENOMEM) {
673 dmar_domain_unload_entry(entry, true);
677 ("unexpected error %d from domain_map_buf", error));
684 dmar_gas_map_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
685 u_int eflags, u_int flags, vm_page_t *ma)
690 KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
691 entry, entry->flags));
692 KASSERT((flags & ~(DMAR_GM_CANWAIT)) == 0,
693 ("invalid flags 0x%x", flags));
695 start = entry->start;
696 DMAR_DOMAIN_LOCK(domain);
697 error = dmar_gas_alloc_region(domain, entry, flags);
699 DMAR_DOMAIN_UNLOCK(domain);
702 entry->flags |= eflags;
703 DMAR_DOMAIN_UNLOCK(domain);
704 if (entry->end == entry->start)
707 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
708 ma + OFF_TO_IDX(start - entry->start),
709 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
710 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
711 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
712 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
713 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
714 if (error == ENOMEM) {
715 dmar_domain_unload_entry(entry, false);
719 ("unexpected error %d from domain_map_buf", error));
725 dmar_gas_reserve_region(struct dmar_domain *domain, dmar_gaddr_t start,
728 struct dmar_map_entry *entry;
731 entry = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
732 entry->start = start;
734 DMAR_DOMAIN_LOCK(domain);
735 error = dmar_gas_alloc_region(domain, entry, DMAR_GM_CANWAIT);
737 entry->flags |= DMAR_MAP_ENTRY_UNMAPPED;
738 DMAR_DOMAIN_UNLOCK(domain);
740 dmar_gas_free_entry(domain, entry);