2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #define RB_AUGMENT(entry) dmar_gas_augment_entry(entry)
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
44 #include <sys/rwlock.h>
45 #include <sys/memdesc.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
49 #include <sys/taskqueue.h>
53 #include <dev/pci/pcivar.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
61 #include <machine/atomic.h>
62 #include <machine/bus.h>
63 #include <machine/md_var.h>
64 #include <machine/specialreg.h>
65 #include <x86/include/busdma_impl.h>
66 #include <x86/iommu/intel_reg.h>
67 #include <x86/iommu/busdma_dmar.h>
68 #include <x86/iommu/intel_dmar.h>
71 * Guest Address Space management.
74 static uma_zone_t dmar_map_entry_zone;
80 dmar_map_entry_zone = uma_zcreate("DMAR_MAP_ENTRY",
81 sizeof(struct dmar_map_entry), NULL, NULL,
82 NULL, NULL, UMA_ALIGN_PTR, 0);
84 SYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
86 struct dmar_map_entry *
87 dmar_gas_alloc_entry(struct dmar_domain *domain, u_int flags)
89 struct dmar_map_entry *res;
91 KASSERT((flags & ~(DMAR_PGF_WAITOK)) == 0,
92 ("unsupported flags %x", flags));
94 res = uma_zalloc(dmar_map_entry_zone, ((flags & DMAR_PGF_WAITOK) !=
95 0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
98 atomic_add_int(&domain->entries_cnt, 1);
104 dmar_gas_free_entry(struct dmar_domain *domain, struct dmar_map_entry *entry)
107 KASSERT(domain == entry->domain,
108 ("mismatched free domain %p entry %p entry->domain %p", domain,
109 entry, entry->domain));
110 atomic_subtract_int(&domain->entries_cnt, 1);
111 uma_zfree(dmar_map_entry_zone, entry);
115 dmar_gas_cmp_entries(struct dmar_map_entry *a, struct dmar_map_entry *b)
118 /* Last entry have zero size, so <= */
119 KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
120 a, (uintmax_t)a->start, (uintmax_t)a->end));
121 KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
122 b, (uintmax_t)b->start, (uintmax_t)b->end));
123 KASSERT(a->end <= b->start || b->end <= a->start ||
124 a->end == a->start || b->end == b->start,
125 ("overlapping entries %p (%jx, %jx) %p (%jx, %jx)",
126 a, (uintmax_t)a->start, (uintmax_t)a->end,
127 b, (uintmax_t)b->start, (uintmax_t)b->end));
131 else if (b->end < a->end)
137 dmar_gas_augment_entry(struct dmar_map_entry *entry)
139 struct dmar_map_entry *l, *r;
141 for (; entry != NULL; entry = RB_PARENT(entry, rb_entry)) {
142 l = RB_LEFT(entry, rb_entry);
143 r = RB_RIGHT(entry, rb_entry);
144 if (l == NULL && r == NULL) {
145 entry->free_down = entry->free_after;
146 } else if (l == NULL && r != NULL) {
147 entry->free_down = MAX(entry->free_after, r->free_down);
148 } else if (/*l != NULL && */ r == NULL) {
149 entry->free_down = MAX(entry->free_after, l->free_down);
150 } else /* if (l != NULL && r != NULL) */ {
151 entry->free_down = MAX(entry->free_after, l->free_down);
152 entry->free_down = MAX(entry->free_down, r->free_down);
157 RB_GENERATE(dmar_gas_entries_tree, dmar_map_entry, rb_entry,
158 dmar_gas_cmp_entries);
161 dmar_gas_fix_free(struct dmar_domain *domain, struct dmar_map_entry *entry)
163 struct dmar_map_entry *next;
165 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
166 entry->free_after = (next != NULL ? next->start : domain->end) -
168 dmar_gas_augment_entry(entry);
173 dmar_gas_check_free(struct dmar_domain *domain)
175 struct dmar_map_entry *entry, *next, *l, *r;
178 RB_FOREACH(entry, dmar_gas_entries_tree, &domain->rb_root) {
179 KASSERT(domain == entry->domain,
180 ("mismatched free domain %p entry %p entry->domain %p",
181 domain, entry, entry->domain));
182 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
184 MPASS(entry->free_after == domain->end - entry->end);
186 MPASS(entry->free_after = next->start - entry->end);
187 MPASS(entry->end <= next->start);
189 l = RB_LEFT(entry, rb_entry);
190 r = RB_RIGHT(entry, rb_entry);
191 if (l == NULL && r == NULL) {
192 MPASS(entry->free_down == entry->free_after);
193 } else if (l == NULL && r != NULL) {
194 MPASS(entry->free_down = MAX(entry->free_after,
196 } else if (r == NULL) {
197 MPASS(entry->free_down = MAX(entry->free_after,
200 v = MAX(entry->free_after, l->free_down);
201 v = MAX(v, r->free_down);
202 MPASS(entry->free_down == v);
209 dmar_gas_rb_insert(struct dmar_domain *domain, struct dmar_map_entry *entry)
211 struct dmar_map_entry *prev, *found;
213 found = RB_INSERT(dmar_gas_entries_tree, &domain->rb_root, entry);
214 dmar_gas_fix_free(domain, entry);
215 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
217 dmar_gas_fix_free(domain, prev);
218 return (found == NULL);
222 dmar_gas_rb_remove(struct dmar_domain *domain, struct dmar_map_entry *entry)
224 struct dmar_map_entry *prev;
226 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
227 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
229 dmar_gas_fix_free(domain, prev);
233 dmar_gas_init_domain(struct dmar_domain *domain)
235 struct dmar_map_entry *begin, *end;
237 begin = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
238 end = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
240 DMAR_DOMAIN_LOCK(domain);
241 KASSERT(domain->entries_cnt == 2, ("dirty domain %p", domain));
242 KASSERT(RB_EMPTY(&domain->rb_root), ("non-empty entries %p", domain));
245 begin->end = DMAR_PAGE_SIZE;
246 begin->free_after = domain->end - begin->end;
247 begin->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
248 dmar_gas_rb_insert(domain, begin);
250 end->start = domain->end;
251 end->end = domain->end;
253 end->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
254 dmar_gas_rb_insert(domain, end);
256 domain->first_place = begin;
257 domain->last_place = end;
258 domain->flags |= DMAR_DOMAIN_GAS_INITED;
259 DMAR_DOMAIN_UNLOCK(domain);
263 dmar_gas_fini_domain(struct dmar_domain *domain)
265 struct dmar_map_entry *entry, *entry1;
267 DMAR_DOMAIN_ASSERT_LOCKED(domain);
268 KASSERT(domain->entries_cnt == 2, ("domain still in use %p", domain));
270 entry = RB_MIN(dmar_gas_entries_tree, &domain->rb_root);
271 KASSERT(entry->start == 0, ("start entry start %p", domain));
272 KASSERT(entry->end == DMAR_PAGE_SIZE, ("start entry end %p", domain));
273 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
274 ("start entry flags %p", domain));
275 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
276 dmar_gas_free_entry(domain, entry);
278 entry = RB_MAX(dmar_gas_entries_tree, &domain->rb_root);
279 KASSERT(entry->start == domain->end, ("end entry start %p", domain));
280 KASSERT(entry->end == domain->end, ("end entry end %p", domain));
281 KASSERT(entry->free_after == 0, ("end entry free_after %p", domain));
282 KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
283 ("end entry flags %p", domain));
284 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
285 dmar_gas_free_entry(domain, entry);
287 RB_FOREACH_SAFE(entry, dmar_gas_entries_tree, &domain->rb_root,
289 KASSERT((entry->flags & DMAR_MAP_ENTRY_RMRR) != 0,
290 ("non-RMRR entry left %p", domain));
291 RB_REMOVE(dmar_gas_entries_tree, &domain->rb_root, entry);
292 dmar_gas_free_entry(domain, entry);
296 struct dmar_gas_match_args {
297 struct dmar_domain *domain;
300 const struct bus_dma_tag_common *common;
302 struct dmar_map_entry *entry;
306 dmar_gas_match_one(struct dmar_gas_match_args *a, struct dmar_map_entry *prev,
309 dmar_gaddr_t bs, start;
311 if (a->entry->start + a->size > end)
314 /* DMAR_PAGE_SIZE to create gap after new entry. */
315 if (a->entry->start < prev->end + DMAR_PAGE_SIZE ||
316 a->entry->start + a->size + a->offset + DMAR_PAGE_SIZE >
317 prev->end + prev->free_after)
320 /* No boundary crossing. */
321 if (dmar_test_boundary(a->entry->start + a->offset, a->size,
322 a->common->boundary))
326 * The start + offset to start + offset + size region crosses
327 * the boundary. Check if there is enough space after the
328 * next boundary after the prev->end.
330 bs = rounddown2(a->entry->start + a->offset + a->common->boundary,
331 a->common->boundary);
332 start = roundup2(bs, a->common->alignment);
333 /* DMAR_PAGE_SIZE to create gap after new entry. */
334 if (start + a->offset + a->size + DMAR_PAGE_SIZE <=
335 prev->end + prev->free_after &&
336 start + a->offset + a->size <= end &&
337 dmar_test_boundary(start + a->offset, a->size,
338 a->common->boundary)) {
339 a->entry->start = start;
344 * Not enough space to align at the requested boundary, or
345 * boundary is smaller than the size, but allowed to split.
346 * We already checked that start + size does not overlap end.
348 * XXXKIB. It is possible that bs is exactly at the start of
349 * the next entry, then we do not have gap. Ignore for now.
351 if ((a->gas_flags & DMAR_GM_CANSPLIT) != 0) {
352 a->size = bs - a->entry->start;
360 dmar_gas_match_insert(struct dmar_gas_match_args *a,
361 struct dmar_map_entry *prev)
363 struct dmar_map_entry *next;
367 * The prev->end is always aligned on the page size, which
368 * causes page alignment for the entry->start too. The size
369 * is checked to be multiple of the page size.
371 * The page sized gap is created between consequent
372 * allocations to ensure that out-of-bounds accesses fault.
374 a->entry->end = a->entry->start + a->size;
376 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, prev);
377 KASSERT(next->start >= a->entry->end &&
378 next->start - a->entry->start >= a->size &&
379 prev->end <= a->entry->end,
380 ("dmar_gas_match_insert hole failed %p prev (%jx, %jx) "
381 "free_after %jx next (%jx, %jx) entry (%jx, %jx)", a->domain,
382 (uintmax_t)prev->start, (uintmax_t)prev->end,
383 (uintmax_t)prev->free_after,
384 (uintmax_t)next->start, (uintmax_t)next->end,
385 (uintmax_t)a->entry->start, (uintmax_t)a->entry->end));
387 prev->free_after = a->entry->start - prev->end;
388 a->entry->free_after = next->start - a->entry->end;
390 found = dmar_gas_rb_insert(a->domain, a->entry);
391 KASSERT(found, ("found dup %p start %jx size %jx",
392 a->domain, (uintmax_t)a->entry->start, (uintmax_t)a->size));
393 a->entry->flags = DMAR_MAP_ENTRY_MAP;
395 KASSERT(RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root,
397 ("entry %p prev %p inserted prev %p", a->entry, prev,
398 RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
399 KASSERT(RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
401 ("entry %p next %p inserted next %p", a->entry, next,
402 RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root, a->entry)));
406 dmar_gas_lowermatch(struct dmar_gas_match_args *a, struct dmar_map_entry *prev)
408 struct dmar_map_entry *l;
411 if (prev->end < a->common->lowaddr) {
412 a->entry->start = roundup2(prev->end + DMAR_PAGE_SIZE,
413 a->common->alignment);
414 if (dmar_gas_match_one(a, prev, a->common->lowaddr)) {
415 dmar_gas_match_insert(a, prev);
419 if (prev->free_down < a->size + a->offset + DMAR_PAGE_SIZE)
421 l = RB_LEFT(prev, rb_entry);
423 ret = dmar_gas_lowermatch(a, l);
427 l = RB_RIGHT(prev, rb_entry);
429 return (dmar_gas_lowermatch(a, l));
434 dmar_gas_uppermatch(struct dmar_gas_match_args *a)
436 struct dmar_map_entry *next, *prev, find_entry;
438 find_entry.start = a->common->highaddr;
439 next = RB_NFIND(dmar_gas_entries_tree, &a->domain->rb_root,
443 prev = RB_PREV(dmar_gas_entries_tree, &a->domain->rb_root, next);
444 KASSERT(prev != NULL, ("no prev %p %jx", a->domain,
445 (uintmax_t)find_entry.start));
447 a->entry->start = prev->start + DMAR_PAGE_SIZE;
448 if (a->entry->start < a->common->highaddr)
449 a->entry->start = a->common->highaddr;
450 a->entry->start = roundup2(a->entry->start,
451 a->common->alignment);
452 if (dmar_gas_match_one(a, prev, a->domain->end)) {
453 dmar_gas_match_insert(a, prev);
458 * XXXKIB. This falls back to linear iteration over
459 * the free space in the high region. But high
460 * regions are almost unused, the code should be
461 * enough to cover the case, although in the
465 next = RB_NEXT(dmar_gas_entries_tree, &a->domain->rb_root,
467 KASSERT(next != NULL, ("no next %p %jx", a->domain,
468 (uintmax_t)find_entry.start));
469 if (next->end >= a->domain->end)
475 dmar_gas_find_space(struct dmar_domain *domain,
476 const struct bus_dma_tag_common *common, dmar_gaddr_t size,
477 int offset, u_int flags, struct dmar_map_entry *entry)
479 struct dmar_gas_match_args a;
482 DMAR_DOMAIN_ASSERT_LOCKED(domain);
483 KASSERT(entry->flags == 0, ("dirty entry %p %p", domain, entry));
484 KASSERT((size & DMAR_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
493 /* Handle lower region. */
494 if (common->lowaddr > 0) {
495 error = dmar_gas_lowermatch(&a, RB_ROOT(&domain->rb_root));
498 KASSERT(error == ENOMEM,
499 ("error %d from dmar_gas_lowermatch", error));
501 /* Handle upper region. */
502 if (common->highaddr >= domain->end)
504 error = dmar_gas_uppermatch(&a);
505 KASSERT(error == ENOMEM,
506 ("error %d from dmar_gas_uppermatch", error));
511 dmar_gas_alloc_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
514 struct dmar_map_entry *next, *prev;
517 DMAR_DOMAIN_ASSERT_LOCKED(domain);
519 if ((entry->start & DMAR_PAGE_MASK) != 0 ||
520 (entry->end & DMAR_PAGE_MASK) != 0)
522 if (entry->start >= entry->end)
524 if (entry->end >= domain->end)
527 next = RB_NFIND(dmar_gas_entries_tree, &domain->rb_root, entry);
528 KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
529 (uintmax_t)entry->start));
530 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, next);
531 /* prev could be NULL */
534 * Adapt to broken BIOSes which specify overlapping RMRR
537 * XXXKIB: this does not handle a case when prev or next
538 * entries are completely covered by the current one, which
541 if (prev != NULL && prev->end > entry->start &&
542 (prev->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
543 if ((prev->flags & DMAR_MAP_ENTRY_RMRR) == 0)
545 entry->start = prev->end;
547 if (next != NULL && next->start < entry->end &&
548 (next->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
549 if ((next->flags & DMAR_MAP_ENTRY_RMRR) == 0)
551 entry->end = next->start;
553 if (entry->end == entry->start)
556 if (prev != NULL && prev->end > entry->start) {
557 /* This assumes that prev is the placeholder entry. */
558 dmar_gas_rb_remove(domain, prev);
561 if (next != NULL && next->start < entry->end) {
562 dmar_gas_rb_remove(domain, next);
566 found = dmar_gas_rb_insert(domain, entry);
567 KASSERT(found, ("found RMRR dup %p start %jx end %jx",
568 domain, (uintmax_t)entry->start, (uintmax_t)entry->end));
569 entry->flags = DMAR_MAP_ENTRY_RMRR;
572 struct dmar_map_entry *ip, *in;
573 ip = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
574 in = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
575 KASSERT(prev == NULL || ip == prev,
576 ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
577 entry, entry->start, entry->end, prev,
578 prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
579 ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
580 KASSERT(next == NULL || in == next,
581 ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
582 entry, entry->start, entry->end, next,
583 next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
584 in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
591 dmar_gas_free_space(struct dmar_domain *domain, struct dmar_map_entry *entry)
594 DMAR_DOMAIN_ASSERT_LOCKED(domain);
595 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
596 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_MAP,
597 ("permanent entry %p %p", domain, entry));
599 dmar_gas_rb_remove(domain, entry);
600 entry->flags &= ~DMAR_MAP_ENTRY_MAP;
603 dmar_gas_check_free(domain);
608 dmar_gas_free_region(struct dmar_domain *domain, struct dmar_map_entry *entry)
610 struct dmar_map_entry *next, *prev;
612 DMAR_DOMAIN_ASSERT_LOCKED(domain);
613 KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
614 DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_RMRR,
615 ("non-RMRR entry %p %p", domain, entry));
617 prev = RB_PREV(dmar_gas_entries_tree, &domain->rb_root, entry);
618 next = RB_NEXT(dmar_gas_entries_tree, &domain->rb_root, entry);
619 dmar_gas_rb_remove(domain, entry);
620 entry->flags &= ~DMAR_MAP_ENTRY_RMRR;
623 dmar_gas_rb_insert(domain, domain->first_place);
625 dmar_gas_rb_insert(domain, domain->last_place);
629 dmar_gas_map(struct dmar_domain *domain,
630 const struct bus_dma_tag_common *common, dmar_gaddr_t size, int offset,
631 u_int eflags, u_int flags, vm_page_t *ma, struct dmar_map_entry **res)
633 struct dmar_map_entry *entry;
636 KASSERT((flags & ~(DMAR_GM_CANWAIT | DMAR_GM_CANSPLIT)) == 0,
637 ("invalid flags 0x%x", flags));
639 entry = dmar_gas_alloc_entry(domain, (flags & DMAR_GM_CANWAIT) != 0 ?
640 DMAR_PGF_WAITOK : 0);
643 DMAR_DOMAIN_LOCK(domain);
644 error = dmar_gas_find_space(domain, common, size, offset, flags,
646 if (error == ENOMEM) {
647 DMAR_DOMAIN_UNLOCK(domain);
648 dmar_gas_free_entry(domain, entry);
653 dmar_gas_check_free(domain);
656 ("unexpected error %d from dmar_gas_find_entry", error));
657 KASSERT(entry->end < domain->end, ("allocated GPA %jx, max GPA %jx",
658 (uintmax_t)entry->end, (uintmax_t)domain->end));
659 entry->flags |= eflags;
660 DMAR_DOMAIN_UNLOCK(domain);
662 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
664 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
665 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
666 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
667 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
668 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
669 if (error == ENOMEM) {
670 dmar_domain_unload_entry(entry, true);
674 ("unexpected error %d from domain_map_buf", error));
681 dmar_gas_map_region(struct dmar_domain *domain, struct dmar_map_entry *entry,
682 u_int eflags, u_int flags, vm_page_t *ma)
687 KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
688 entry, entry->flags));
689 KASSERT((flags & ~(DMAR_GM_CANWAIT)) == 0,
690 ("invalid flags 0x%x", flags));
692 start = entry->start;
693 DMAR_DOMAIN_LOCK(domain);
694 error = dmar_gas_alloc_region(domain, entry, flags);
696 DMAR_DOMAIN_UNLOCK(domain);
699 entry->flags |= eflags;
700 DMAR_DOMAIN_UNLOCK(domain);
701 if (entry->end == entry->start)
704 error = domain_map_buf(domain, entry->start, entry->end - entry->start,
705 ma + OFF_TO_IDX(start - entry->start),
706 ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
707 ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
708 ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
709 ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
710 (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
711 if (error == ENOMEM) {
712 dmar_domain_unload_entry(entry, false);
716 ("unexpected error %d from domain_map_buf", error));
722 dmar_gas_reserve_region(struct dmar_domain *domain, dmar_gaddr_t start,
725 struct dmar_map_entry *entry;
728 entry = dmar_gas_alloc_entry(domain, DMAR_PGF_WAITOK);
729 entry->start = start;
731 DMAR_DOMAIN_LOCK(domain);
732 error = dmar_gas_alloc_region(domain, entry, DMAR_GM_CANWAIT);
734 entry->flags |= DMAR_MAP_ENTRY_UNMAPPED;
735 DMAR_DOMAIN_UNLOCK(domain);
737 dmar_gas_free_entry(domain, entry);