xref: /freebsd/sys/x86/iommu/intel_ctx.c (revision e9d948cfe0d21780d2e94137e322ecfe89f75d6a)
186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni  *
486be9f0dSKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
586be9f0dSKonstantin Belousov  *
686be9f0dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov  *
986be9f0dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov  * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov  * are met:
1286be9f0dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov  *
1886be9f0dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov  * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov  */
3086be9f0dSKonstantin Belousov 
3186be9f0dSKonstantin Belousov #include <sys/param.h>
3286be9f0dSKonstantin Belousov #include <sys/systm.h>
3386be9f0dSKonstantin Belousov #include <sys/malloc.h>
3486be9f0dSKonstantin Belousov #include <sys/bus.h>
3586be9f0dSKonstantin Belousov #include <sys/interrupt.h>
3686be9f0dSKonstantin Belousov #include <sys/kernel.h>
3786be9f0dSKonstantin Belousov #include <sys/ktr.h>
3886be9f0dSKonstantin Belousov #include <sys/limits.h>
3986be9f0dSKonstantin Belousov #include <sys/lock.h>
4086be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4186be9f0dSKonstantin Belousov #include <sys/mutex.h>
4286be9f0dSKonstantin Belousov #include <sys/proc.h>
4386be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4486be9f0dSKonstantin Belousov #include <sys/rman.h>
4586be9f0dSKonstantin Belousov #include <sys/sysctl.h>
4686be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
4786be9f0dSKonstantin Belousov #include <sys/tree.h>
4886be9f0dSKonstantin Belousov #include <sys/uio.h>
490a110d5bSKonstantin Belousov #include <sys/vmem.h>
5086be9f0dSKonstantin Belousov #include <vm/vm.h>
5186be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5286be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5386be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5486be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_map.h>
57c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
58c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
59c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h>
60c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h>
6186be9f0dSKonstantin Belousov #include <machine/atomic.h>
6286be9f0dSKonstantin Belousov #include <machine/bus.h>
6386be9f0dSKonstantin Belousov #include <machine/md_var.h>
6486be9f0dSKonstantin Belousov #include <machine/specialreg.h>
6586be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
66f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h>
67c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
6840d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
69685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7086be9f0dSKonstantin Belousov 
7186be9f0dSKonstantin Belousov static MALLOC_DEFINE(M_DMAR_CTX, "dmar_ctx", "Intel DMAR Context");
721abfd355SKonstantin Belousov static MALLOC_DEFINE(M_DMAR_DOMAIN, "dmar_dom", "Intel DMAR Domain");
7386be9f0dSKonstantin Belousov 
741abfd355SKonstantin Belousov static void dmar_unref_domain_locked(struct dmar_unit *dmar,
751abfd355SKonstantin Belousov     struct dmar_domain *domain);
761abfd355SKonstantin Belousov static void dmar_domain_destroy(struct dmar_domain *domain);
7786be9f0dSKonstantin Belousov 
7865b133e5SKonstantin Belousov static void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx);
7965b133e5SKonstantin Belousov static void dmar_free_ctx(struct dmar_ctx *ctx);
8065b133e5SKonstantin Belousov 
8186be9f0dSKonstantin Belousov static void
8286be9f0dSKonstantin Belousov dmar_ensure_ctx_page(struct dmar_unit *dmar, int bus)
8386be9f0dSKonstantin Belousov {
8486be9f0dSKonstantin Belousov 	struct sf_buf *sf;
8586be9f0dSKonstantin Belousov 	dmar_root_entry_t *re;
8686be9f0dSKonstantin Belousov 	vm_page_t ctxm;
8786be9f0dSKonstantin Belousov 
8886be9f0dSKonstantin Belousov 	/*
8986be9f0dSKonstantin Belousov 	 * Allocated context page must be linked.
9086be9f0dSKonstantin Belousov 	 */
9140d951bcSKonstantin Belousov 	ctxm = iommu_pgalloc(dmar->ctx_obj, 1 + bus, IOMMU_PGF_NOALLOC);
9286be9f0dSKonstantin Belousov 	if (ctxm != NULL)
9386be9f0dSKonstantin Belousov 		return;
9486be9f0dSKonstantin Belousov 
9586be9f0dSKonstantin Belousov 	/*
9686be9f0dSKonstantin Belousov 	 * Page not present, allocate and link.  Note that other
9786be9f0dSKonstantin Belousov 	 * thread might execute this sequence in parallel.  This
9886be9f0dSKonstantin Belousov 	 * should be safe, because the context entries written by both
9986be9f0dSKonstantin Belousov 	 * threads are equal.
10086be9f0dSKonstantin Belousov 	 */
10186be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
10240d951bcSKonstantin Belousov 	ctxm = iommu_pgalloc(dmar->ctx_obj, 1 + bus, IOMMU_PGF_ZERO |
10315f6baf4SRuslan Bukin 	    IOMMU_PGF_WAITOK);
10440d951bcSKonstantin Belousov 	re = iommu_map_pgtbl(dmar->ctx_obj, 0, IOMMU_PGF_NOALLOC, &sf);
10586be9f0dSKonstantin Belousov 	re += bus;
10686be9f0dSKonstantin Belousov 	dmar_pte_store(&re->r1, DMAR_ROOT_R1_P | (DMAR_ROOT_R1_CTP_MASK &
10786be9f0dSKonstantin Belousov 	    VM_PAGE_TO_PHYS(ctxm)));
1086b7c46afSKonstantin Belousov 	dmar_flush_root_to_ram(dmar, re);
10940d951bcSKonstantin Belousov 	iommu_unmap_pgtbl(sf);
11086be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
11186be9f0dSKonstantin Belousov }
11286be9f0dSKonstantin Belousov 
11386be9f0dSKonstantin Belousov static dmar_ctx_entry_t *
11486be9f0dSKonstantin Belousov dmar_map_ctx_entry(struct dmar_ctx *ctx, struct sf_buf **sfp)
11586be9f0dSKonstantin Belousov {
11659e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
11786be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
11886be9f0dSKonstantin Belousov 
11978b51754SRuslan Bukin 	dmar = CTX2DMAR(ctx);
12059e37c8aSRuslan Bukin 
12140d951bcSKonstantin Belousov 	ctxp = iommu_map_pgtbl(dmar->ctx_obj, 1 + PCI_RID2BUS(ctx->context.rid),
122cb9050ddSRuslan Bukin 	    IOMMU_PGF_NOALLOC | IOMMU_PGF_WAITOK, sfp);
123cb9050ddSRuslan Bukin 	ctxp += ctx->context.rid & 0xff;
12486be9f0dSKonstantin Belousov 	return (ctxp);
12586be9f0dSKonstantin Belousov }
12686be9f0dSKonstantin Belousov 
12786be9f0dSKonstantin Belousov static void
128685666aaSKonstantin Belousov ctx_id_entry_init_one(dmar_ctx_entry_t *ctxp, struct dmar_domain *domain,
129685666aaSKonstantin Belousov     vm_page_t ctx_root)
13086be9f0dSKonstantin Belousov {
1311abfd355SKonstantin Belousov 	/*
1321abfd355SKonstantin Belousov 	 * For update due to move, the store is not atomic.  It is
1331abfd355SKonstantin Belousov 	 * possible that DMAR read upper doubleword, while low
1341abfd355SKonstantin Belousov 	 * doubleword is not yet updated.  The domain id is stored in
1351abfd355SKonstantin Belousov 	 * the upper doubleword, while the table pointer in the lower.
1361abfd355SKonstantin Belousov 	 *
1371abfd355SKonstantin Belousov 	 * There is no good solution, for the same reason it is wrong
1381abfd355SKonstantin Belousov 	 * to clear P bit in the ctx entry for update.
1391abfd355SKonstantin Belousov 	 */
1401abfd355SKonstantin Belousov 	dmar_pte_store1(&ctxp->ctx2, DMAR_CTX2_DID(domain->domain) |
1411abfd355SKonstantin Belousov 	    domain->awlvl);
142685666aaSKonstantin Belousov 	if (ctx_root == NULL) {
143685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_PASS | DMAR_CTX1_P);
144685666aaSKonstantin Belousov 	} else {
145685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_UNTR |
146685666aaSKonstantin Belousov 		    (DMAR_CTX1_ASR_MASK & VM_PAGE_TO_PHYS(ctx_root)) |
147685666aaSKonstantin Belousov 		    DMAR_CTX1_P);
148685666aaSKonstantin Belousov 	}
149685666aaSKonstantin Belousov }
150685666aaSKonstantin Belousov 
151685666aaSKonstantin Belousov static void
152685666aaSKonstantin Belousov ctx_id_entry_init(struct dmar_ctx *ctx, dmar_ctx_entry_t *ctxp, bool move,
153685666aaSKonstantin Belousov     int busno)
154685666aaSKonstantin Belousov {
155685666aaSKonstantin Belousov 	struct dmar_unit *unit;
156685666aaSKonstantin Belousov 	struct dmar_domain *domain;
157685666aaSKonstantin Belousov 	vm_page_t ctx_root;
158685666aaSKonstantin Belousov 	int i;
159685666aaSKonstantin Belousov 
16078b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
16178b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
162685666aaSKonstantin Belousov 	KASSERT(move || (ctxp->ctx1 == 0 && ctxp->ctx2 == 0),
163685666aaSKonstantin Belousov 	    ("dmar%d: initialized ctx entry %d:%d:%d 0x%jx 0x%jx",
16459e37c8aSRuslan Bukin 	    unit->iommu.unit, busno, pci_get_slot(ctx->context.tag->owner),
16559e37c8aSRuslan Bukin 	    pci_get_function(ctx->context.tag->owner),
166685666aaSKonstantin Belousov 	    ctxp->ctx1, ctxp->ctx2));
167685666aaSKonstantin Belousov 
16815f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 &&
16986be9f0dSKonstantin Belousov 	    (unit->hw_ecap & DMAR_ECAP_PT) != 0) {
1701abfd355SKonstantin Belousov 		KASSERT(domain->pgtbl_obj == NULL,
17186be9f0dSKonstantin Belousov 		    ("ctx %p non-null pgtbl_obj", ctx));
172685666aaSKonstantin Belousov 		ctx_root = NULL;
17386be9f0dSKonstantin Belousov 	} else {
17440d951bcSKonstantin Belousov 		ctx_root = iommu_pgalloc(domain->pgtbl_obj, 0,
17515f6baf4SRuslan Bukin 		    IOMMU_PGF_NOALLOC);
176685666aaSKonstantin Belousov 	}
177685666aaSKonstantin Belousov 
17878b51754SRuslan Bukin 	if (iommu_is_buswide_ctx(DMAR2IOMMU(unit), busno)) {
179685666aaSKonstantin Belousov 		MPASS(!move);
180685666aaSKonstantin Belousov 		for (i = 0; i <= PCI_BUSMAX; i++) {
181685666aaSKonstantin Belousov 			ctx_id_entry_init_one(&ctxp[i], domain, ctx_root);
182685666aaSKonstantin Belousov 		}
183685666aaSKonstantin Belousov 	} else {
184685666aaSKonstantin Belousov 		ctx_id_entry_init_one(ctxp, domain, ctx_root);
18586be9f0dSKonstantin Belousov 	}
1866b7c46afSKonstantin Belousov 	dmar_flush_ctx_to_ram(unit, ctxp);
18786be9f0dSKonstantin Belousov }
18886be9f0dSKonstantin Belousov 
18986be9f0dSKonstantin Belousov static int
1901abfd355SKonstantin Belousov dmar_flush_for_ctx_entry(struct dmar_unit *dmar, bool force)
1911abfd355SKonstantin Belousov {
1921abfd355SKonstantin Belousov 	int error;
1931abfd355SKonstantin Belousov 
1941abfd355SKonstantin Belousov 	/*
1951abfd355SKonstantin Belousov 	 * If dmar declares Caching Mode as Set, follow 11.5 "Caching
1961abfd355SKonstantin Belousov 	 * Mode Consideration" and do the (global) invalidation of the
1971abfd355SKonstantin Belousov 	 * negative TLB entries.
1981abfd355SKonstantin Belousov 	 */
1991abfd355SKonstantin Belousov 	if ((dmar->hw_cap & DMAR_CAP_CM) == 0 && !force)
2001abfd355SKonstantin Belousov 		return (0);
2011abfd355SKonstantin Belousov 	if (dmar->qi_enabled) {
2021abfd355SKonstantin Belousov 		dmar_qi_invalidate_ctx_glob_locked(dmar);
2031abfd355SKonstantin Belousov 		if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force)
2041abfd355SKonstantin Belousov 			dmar_qi_invalidate_iotlb_glob_locked(dmar);
2051abfd355SKonstantin Belousov 		return (0);
2061abfd355SKonstantin Belousov 	}
2071abfd355SKonstantin Belousov 	error = dmar_inv_ctx_glob(dmar);
2081abfd355SKonstantin Belousov 	if (error == 0 && ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force))
2091abfd355SKonstantin Belousov 		error = dmar_inv_iotlb_glob(dmar);
2101abfd355SKonstantin Belousov 	return (error);
2111abfd355SKonstantin Belousov }
2121abfd355SKonstantin Belousov 
2131abfd355SKonstantin Belousov static int
214f9feb091SKonstantin Belousov domain_init_rmrr(struct dmar_domain *domain, device_t dev, int bus,
215f9feb091SKonstantin Belousov     int slot, int func, int dev_domain, int dev_busno,
216f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len)
21786be9f0dSKonstantin Belousov {
21859e37c8aSRuslan Bukin 	struct iommu_map_entries_tailq rmrr_entries;
21959e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
22086be9f0dSKonstantin Belousov 	vm_page_t *ma;
22159e37c8aSRuslan Bukin 	iommu_gaddr_t start, end;
22286be9f0dSKonstantin Belousov 	vm_pindex_t size, i;
22386be9f0dSKonstantin Belousov 	int error, error1;
22486be9f0dSKonstantin Belousov 
22524e38af6SKonstantin Belousov 	if (!dmar_rmrr_enable)
22624e38af6SKonstantin Belousov 		return (0);
22724e38af6SKonstantin Belousov 
22886be9f0dSKonstantin Belousov 	error = 0;
22986be9f0dSKonstantin Belousov 	TAILQ_INIT(&rmrr_entries);
230f9feb091SKonstantin Belousov 	dmar_dev_parse_rmrr(domain, dev_domain, dev_busno, dev_path,
231f9feb091SKonstantin Belousov 	    dev_path_len, &rmrr_entries);
232db0110a5SAlan Cox 	TAILQ_FOREACH_SAFE(entry, &rmrr_entries, dmamap_link, entry1) {
23386be9f0dSKonstantin Belousov 		/*
23486be9f0dSKonstantin Belousov 		 * VT-d specification requires that the start of an
23586be9f0dSKonstantin Belousov 		 * RMRR entry is 4k-aligned.  Buggy BIOSes put
23686be9f0dSKonstantin Belousov 		 * anything into the start and end fields.  Truncate
23786be9f0dSKonstantin Belousov 		 * and round as neccesary.
23886be9f0dSKonstantin Belousov 		 *
23986be9f0dSKonstantin Belousov 		 * We also allow the overlapping RMRR entries, see
24062ad310cSRuslan Bukin 		 * iommu_gas_alloc_region().
24186be9f0dSKonstantin Belousov 		 */
24286be9f0dSKonstantin Belousov 		start = entry->start;
24386be9f0dSKonstantin Belousov 		end = entry->end;
244f9feb091SKonstantin Belousov 		if (bootverbose)
245f9feb091SKonstantin Belousov 			printf("dmar%d ctx pci%d:%d:%d RMRR [%#jx, %#jx]\n",
24659e37c8aSRuslan Bukin 			    domain->iodom.iommu->unit, bus, slot, func,
247f9feb091SKonstantin Belousov 			    (uintmax_t)start, (uintmax_t)end);
24886be9f0dSKonstantin Belousov 		entry->start = trunc_page(start);
24986be9f0dSKonstantin Belousov 		entry->end = round_page(end);
250e02b05b3SKonstantin Belousov 		if (entry->start == entry->end) {
251e02b05b3SKonstantin Belousov 			/* Workaround for some AMI (?) BIOSes */
252e02b05b3SKonstantin Belousov 			if (bootverbose) {
253f9feb091SKonstantin Belousov 				if (dev != NULL)
254f9feb091SKonstantin Belousov 					device_printf(dev, "");
255f9feb091SKonstantin Belousov 				printf("pci%d:%d:%d ", bus, slot, func);
256f9feb091SKonstantin Belousov 				printf("BIOS bug: dmar%d RMRR "
257e02b05b3SKonstantin Belousov 				    "region (%jx, %jx) corrected\n",
25859e37c8aSRuslan Bukin 				    domain->iodom.iommu->unit, start, end);
259e02b05b3SKonstantin Belousov 			}
26040d951bcSKonstantin Belousov 			entry->end += IOMMU_PAGE_SIZE * 0x20;
261e02b05b3SKonstantin Belousov 		}
26286be9f0dSKonstantin Belousov 		size = OFF_TO_IDX(entry->end - entry->start);
26386be9f0dSKonstantin Belousov 		ma = malloc(sizeof(vm_page_t) * size, M_TEMP, M_WAITOK);
26486be9f0dSKonstantin Belousov 		for (i = 0; i < size; i++) {
26586be9f0dSKonstantin Belousov 			ma[i] = vm_page_getfake(entry->start + PAGE_SIZE * i,
26686be9f0dSKonstantin Belousov 			    VM_MEMATTR_DEFAULT);
26786be9f0dSKonstantin Belousov 		}
26878b51754SRuslan Bukin 		error1 = iommu_gas_map_region(DOM2IODOM(domain), entry,
26959e37c8aSRuslan Bukin 		    IOMMU_MAP_ENTRY_READ | IOMMU_MAP_ENTRY_WRITE,
27059e37c8aSRuslan Bukin 		    IOMMU_MF_CANWAIT | IOMMU_MF_RMRR, ma);
27186be9f0dSKonstantin Belousov 		/*
27286be9f0dSKonstantin Belousov 		 * Non-failed RMRR entries are owned by context rb
27386be9f0dSKonstantin Belousov 		 * tree.  Get rid of the failed entry, but do not stop
27486be9f0dSKonstantin Belousov 		 * the loop.  Rest of the parsed RMRR entries are
27586be9f0dSKonstantin Belousov 		 * loaded and removed on the context destruction.
27686be9f0dSKonstantin Belousov 		 */
27786be9f0dSKonstantin Belousov 		if (error1 == 0 && entry->end != entry->start) {
27859e37c8aSRuslan Bukin 			IOMMU_LOCK(domain->iodom.iommu);
2791abfd355SKonstantin Belousov 			domain->refs++; /* XXXKIB prevent free */
28015f6baf4SRuslan Bukin 			domain->iodom.flags |= IOMMU_DOMAIN_RMRR;
28159e37c8aSRuslan Bukin 			IOMMU_UNLOCK(domain->iodom.iommu);
28286be9f0dSKonstantin Belousov 		} else {
28386be9f0dSKonstantin Belousov 			if (error1 != 0) {
284f9feb091SKonstantin Belousov 				if (dev != NULL)
285f9feb091SKonstantin Belousov 					device_printf(dev, "");
286f9feb091SKonstantin Belousov 				printf("pci%d:%d:%d ", bus, slot, func);
287f9feb091SKonstantin Belousov 				printf(
28886be9f0dSKonstantin Belousov 			    "dmar%d failed to map RMRR region (%jx, %jx) %d\n",
28959e37c8aSRuslan Bukin 				    domain->iodom.iommu->unit, start, end,
290f9feb091SKonstantin Belousov 				    error1);
29186be9f0dSKonstantin Belousov 				error = error1;
29286be9f0dSKonstantin Belousov 			}
293db0110a5SAlan Cox 			TAILQ_REMOVE(&rmrr_entries, entry, dmamap_link);
2944670f908SAlan Cox 			iommu_gas_free_entry(entry);
29586be9f0dSKonstantin Belousov 		}
29686be9f0dSKonstantin Belousov 		for (i = 0; i < size; i++)
29786be9f0dSKonstantin Belousov 			vm_page_putfake(ma[i]);
29886be9f0dSKonstantin Belousov 		free(ma, M_TEMP);
29986be9f0dSKonstantin Belousov 	}
30086be9f0dSKonstantin Belousov 	return (error);
30186be9f0dSKonstantin Belousov }
30286be9f0dSKonstantin Belousov 
303ee47a12aSRyan Libby /*
304ee47a12aSRyan Libby  * PCI memory address space is shared between memory-mapped devices (MMIO) and
305ee47a12aSRyan Libby  * host memory (which may be remapped by an IOMMU).  Device accesses to an
306ee47a12aSRyan Libby  * address within a memory aperture in a PCIe root port will be treated as
307ee47a12aSRyan Libby  * peer-to-peer and not forwarded to an IOMMU.  To avoid this, reserve the
308ee47a12aSRyan Libby  * address space of the root port's memory apertures in the address space used
309ee47a12aSRyan Libby  * by the IOMMU for remapping.
310ee47a12aSRyan Libby  */
311ee47a12aSRyan Libby static int
312ee47a12aSRyan Libby dmar_reserve_pci_regions(struct dmar_domain *domain, device_t dev)
313ee47a12aSRyan Libby {
314ee47a12aSRyan Libby 	struct iommu_domain *iodom;
315ee47a12aSRyan Libby 	device_t root;
316ee47a12aSRyan Libby 	uint32_t val;
317ee47a12aSRyan Libby 	uint64_t base, limit;
318ee47a12aSRyan Libby 	int error;
319ee47a12aSRyan Libby 
320ee47a12aSRyan Libby 	iodom = DOM2IODOM(domain);
321ee47a12aSRyan Libby 
322ee47a12aSRyan Libby 	root = pci_find_pcie_root_port(dev);
323ee47a12aSRyan Libby 	if (root == NULL)
324ee47a12aSRyan Libby 		return (0);
325ee47a12aSRyan Libby 
326ee47a12aSRyan Libby 	/* Disable downstream memory */
327ee47a12aSRyan Libby 	base = PCI_PPBMEMBASE(0, pci_read_config(root, PCIR_MEMBASE_1, 2));
328ee47a12aSRyan Libby 	limit = PCI_PPBMEMLIMIT(0, pci_read_config(root, PCIR_MEMLIMIT_1, 2));
329ee47a12aSRyan Libby 	error = iommu_gas_reserve_region_extend(iodom, base, limit + 1);
330ee47a12aSRyan Libby 	if (bootverbose || error != 0)
331ee47a12aSRyan Libby 		device_printf(dev, "DMAR reserve [%#jx-%#jx] (error %d)\n",
332ee47a12aSRyan Libby 		    base, limit + 1, error);
333ee47a12aSRyan Libby 	if (error != 0)
334ee47a12aSRyan Libby 		return (error);
335ee47a12aSRyan Libby 
336ee47a12aSRyan Libby 	/* Disable downstream prefetchable memory */
337ee47a12aSRyan Libby 	val = pci_read_config(root, PCIR_PMBASEL_1, 2);
338ee47a12aSRyan Libby 	if (val != 0 || pci_read_config(root, PCIR_PMLIMITL_1, 2) != 0) {
339ee47a12aSRyan Libby 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
340ee47a12aSRyan Libby 			base = PCI_PPBMEMBASE(
341ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMBASEH_1, 4),
342ee47a12aSRyan Libby 			    val);
343ee47a12aSRyan Libby 			limit = PCI_PPBMEMLIMIT(
344ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITH_1, 4),
345ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITL_1, 2));
346ee47a12aSRyan Libby 		} else {
347ee47a12aSRyan Libby 			base = PCI_PPBMEMBASE(0, val);
348ee47a12aSRyan Libby 			limit = PCI_PPBMEMLIMIT(0,
349ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITL_1, 2));
350ee47a12aSRyan Libby 		}
351ee47a12aSRyan Libby 		error = iommu_gas_reserve_region_extend(iodom, base,
352ee47a12aSRyan Libby 		    limit + 1);
353ee47a12aSRyan Libby 		if (bootverbose || error != 0)
354ee47a12aSRyan Libby 			device_printf(dev, "DMAR reserve [%#jx-%#jx] "
355ee47a12aSRyan Libby 			    "(error %d)\n", base, limit + 1, error);
356ee47a12aSRyan Libby 		if (error != 0)
357ee47a12aSRyan Libby 			return (error);
358ee47a12aSRyan Libby 	}
359ee47a12aSRyan Libby 
360ee47a12aSRyan Libby 	return (error);
361ee47a12aSRyan Libby }
362ee47a12aSRyan Libby 
3631abfd355SKonstantin Belousov static struct dmar_domain *
3641abfd355SKonstantin Belousov dmar_domain_alloc(struct dmar_unit *dmar, bool id_mapped)
3651abfd355SKonstantin Belousov {
36662ad310cSRuslan Bukin 	struct iommu_domain *iodom;
36716696f60SRuslan Bukin 	struct iommu_unit *unit;
3681abfd355SKonstantin Belousov 	struct dmar_domain *domain;
3691abfd355SKonstantin Belousov 	int error, id, mgaw;
3701abfd355SKonstantin Belousov 
3711abfd355SKonstantin Belousov 	id = alloc_unr(dmar->domids);
3721abfd355SKonstantin Belousov 	if (id == -1)
3731abfd355SKonstantin Belousov 		return (NULL);
3741abfd355SKonstantin Belousov 	domain = malloc(sizeof(*domain), M_DMAR_DOMAIN, M_WAITOK | M_ZERO);
37578b51754SRuslan Bukin 	iodom = DOM2IODOM(domain);
37616696f60SRuslan Bukin 	unit = DMAR2IOMMU(dmar);
3771abfd355SKonstantin Belousov 	domain->domain = id;
378*e9d948cfSKonstantin Belousov 	LIST_INIT(&iodom->contexts);
37916696f60SRuslan Bukin 	iommu_domain_init(unit, iodom, &dmar_domain_map_ops);
38016696f60SRuslan Bukin 
3811abfd355SKonstantin Belousov 	domain->dmar = dmar;
3821abfd355SKonstantin Belousov 
3831abfd355SKonstantin Belousov 	/*
3841abfd355SKonstantin Belousov 	 * For now, use the maximal usable physical address of the
3851abfd355SKonstantin Belousov 	 * installed memory to calculate the mgaw on id_mapped domain.
3861abfd355SKonstantin Belousov 	 * It is useful for the identity mapping, and less so for the
3871abfd355SKonstantin Belousov 	 * virtualized bus address space.
3881abfd355SKonstantin Belousov 	 */
38962ad310cSRuslan Bukin 	domain->iodom.end = id_mapped ? ptoa(Maxmem) : BUS_SPACE_MAXADDR;
39062ad310cSRuslan Bukin 	mgaw = dmar_maxaddr2mgaw(dmar, domain->iodom.end, !id_mapped);
3911abfd355SKonstantin Belousov 	error = domain_set_agaw(domain, mgaw);
3921abfd355SKonstantin Belousov 	if (error != 0)
3931abfd355SKonstantin Belousov 		goto fail;
3941abfd355SKonstantin Belousov 	if (!id_mapped)
3951abfd355SKonstantin Belousov 		/* Use all supported address space for remapping. */
39662ad310cSRuslan Bukin 		domain->iodom.end = 1ULL << (domain->agaw - 1);
3971abfd355SKonstantin Belousov 
39878b51754SRuslan Bukin 	iommu_gas_init_domain(DOM2IODOM(domain));
3991abfd355SKonstantin Belousov 
4001abfd355SKonstantin Belousov 	if (id_mapped) {
4011abfd355SKonstantin Belousov 		if ((dmar->hw_ecap & DMAR_ECAP_PT) == 0) {
40210ebd230SKonstantin Belousov 			domain->pgtbl_obj = dmar_get_idmap_pgtbl(domain,
40362ad310cSRuslan Bukin 			    domain->iodom.end);
4041abfd355SKonstantin Belousov 		}
40515f6baf4SRuslan Bukin 		domain->iodom.flags |= IOMMU_DOMAIN_IDMAP;
4061abfd355SKonstantin Belousov 	} else {
40723145534SKonstantin Belousov 		error = dmar_domain_alloc_pgtbl(domain);
4081abfd355SKonstantin Belousov 		if (error != 0)
4091abfd355SKonstantin Belousov 			goto fail;
4101abfd355SKonstantin Belousov 		/* Disable local apic region access */
41162ad310cSRuslan Bukin 		error = iommu_gas_reserve_region(iodom, 0xfee00000,
41294dfb28eSRuslan Bukin 		    0xfeefffff + 1, &iodom->msi_entry);
4131abfd355SKonstantin Belousov 		if (error != 0)
4141abfd355SKonstantin Belousov 			goto fail;
4151abfd355SKonstantin Belousov 	}
4161abfd355SKonstantin Belousov 	return (domain);
4171abfd355SKonstantin Belousov 
4181abfd355SKonstantin Belousov fail:
4191abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
4201abfd355SKonstantin Belousov 	return (NULL);
4211abfd355SKonstantin Belousov }
4221abfd355SKonstantin Belousov 
42386be9f0dSKonstantin Belousov static struct dmar_ctx *
4241abfd355SKonstantin Belousov dmar_ctx_alloc(struct dmar_domain *domain, uint16_t rid)
42586be9f0dSKonstantin Belousov {
42686be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
42786be9f0dSKonstantin Belousov 
42886be9f0dSKonstantin Belousov 	ctx = malloc(sizeof(*ctx), M_DMAR_CTX, M_WAITOK | M_ZERO);
42978b51754SRuslan Bukin 	ctx->context.domain = DOM2IODOM(domain);
43059e37c8aSRuslan Bukin 	ctx->context.tag = malloc(sizeof(struct bus_dma_tag_iommu),
43159e37c8aSRuslan Bukin 	    M_DMAR_CTX, M_WAITOK | M_ZERO);
432cb9050ddSRuslan Bukin 	ctx->context.rid = rid;
433*e9d948cfSKonstantin Belousov 	ctx->context.refs = 1;
43486be9f0dSKonstantin Belousov 	return (ctx);
43586be9f0dSKonstantin Belousov }
43686be9f0dSKonstantin Belousov 
43786be9f0dSKonstantin Belousov static void
4381abfd355SKonstantin Belousov dmar_ctx_link(struct dmar_ctx *ctx)
4391abfd355SKonstantin Belousov {
4401abfd355SKonstantin Belousov 	struct dmar_domain *domain;
4411abfd355SKonstantin Belousov 
44278b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
44359e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4441abfd355SKonstantin Belousov 	KASSERT(domain->refs >= domain->ctx_cnt,
4451abfd355SKonstantin Belousov 	    ("dom %p ref underflow %d %d", domain, domain->refs,
4461abfd355SKonstantin Belousov 	    domain->ctx_cnt));
4471abfd355SKonstantin Belousov 	domain->refs++;
4481abfd355SKonstantin Belousov 	domain->ctx_cnt++;
449*e9d948cfSKonstantin Belousov 	LIST_INSERT_HEAD(&domain->iodom.contexts, &ctx->context, link);
4501abfd355SKonstantin Belousov }
4511abfd355SKonstantin Belousov 
4521abfd355SKonstantin Belousov static void
4531abfd355SKonstantin Belousov dmar_ctx_unlink(struct dmar_ctx *ctx)
4541abfd355SKonstantin Belousov {
4551abfd355SKonstantin Belousov 	struct dmar_domain *domain;
4561abfd355SKonstantin Belousov 
45778b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
45859e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4591abfd355SKonstantin Belousov 	KASSERT(domain->refs > 0,
4601abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d", domain, domain->refs));
4611abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt >= domain->refs,
4621abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d ctx_cnt %d", domain,
4631abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
4641abfd355SKonstantin Belousov 	domain->refs--;
4651abfd355SKonstantin Belousov 	domain->ctx_cnt--;
466*e9d948cfSKonstantin Belousov 	LIST_REMOVE(&ctx->context, link);
4671abfd355SKonstantin Belousov }
4681abfd355SKonstantin Belousov 
4691abfd355SKonstantin Belousov static void
4701abfd355SKonstantin Belousov dmar_domain_destroy(struct dmar_domain *domain)
47186be9f0dSKonstantin Belousov {
47216696f60SRuslan Bukin 	struct iommu_domain *iodom;
47359e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
47486be9f0dSKonstantin Belousov 
47516696f60SRuslan Bukin 	iodom = DOM2IODOM(domain);
47616696f60SRuslan Bukin 
47759e37c8aSRuslan Bukin 	KASSERT(TAILQ_EMPTY(&domain->iodom.unload_entries),
4781abfd355SKonstantin Belousov 	    ("unfinished unloads %p", domain));
479*e9d948cfSKonstantin Belousov 	KASSERT(LIST_EMPTY(&iodom->contexts),
4801abfd355SKonstantin Belousov 	    ("destroying dom %p with contexts", domain));
4811abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt == 0,
4821abfd355SKonstantin Belousov 	    ("destroying dom %p with ctx_cnt %d", domain, domain->ctx_cnt));
4831abfd355SKonstantin Belousov 	KASSERT(domain->refs == 0,
4841abfd355SKonstantin Belousov 	    ("destroying dom %p with refs %d", domain, domain->refs));
48515f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_GAS_INITED) != 0) {
4861abfd355SKonstantin Belousov 		DMAR_DOMAIN_LOCK(domain);
48716696f60SRuslan Bukin 		iommu_gas_fini_domain(iodom);
4881abfd355SKonstantin Belousov 		DMAR_DOMAIN_UNLOCK(domain);
48986be9f0dSKonstantin Belousov 	}
49015f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_PGTBL_INITED) != 0) {
4911abfd355SKonstantin Belousov 		if (domain->pgtbl_obj != NULL)
4921abfd355SKonstantin Belousov 			DMAR_DOMAIN_PGLOCK(domain);
49323145534SKonstantin Belousov 		dmar_domain_free_pgtbl(domain);
49486be9f0dSKonstantin Belousov 	}
49516696f60SRuslan Bukin 	iommu_domain_fini(iodom);
49678b51754SRuslan Bukin 	dmar = DOM2DMAR(domain);
49759e37c8aSRuslan Bukin 	free_unr(dmar->domids, domain->domain);
4981abfd355SKonstantin Belousov 	free(domain, M_DMAR_DOMAIN);
49986be9f0dSKonstantin Belousov }
50086be9f0dSKonstantin Belousov 
501f9feb091SKonstantin Belousov static struct dmar_ctx *
502f9feb091SKonstantin Belousov dmar_get_ctx_for_dev1(struct dmar_unit *dmar, device_t dev, uint16_t rid,
503f9feb091SKonstantin Belousov     int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
5041abfd355SKonstantin Belousov     bool id_mapped, bool rmrr_init)
50586be9f0dSKonstantin Belousov {
5061abfd355SKonstantin Belousov 	struct dmar_domain *domain, *domain1;
50786be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx, *ctx1;
508661bd70bSKonstantin Belousov 	struct iommu_unit *unit __diagused;
50986be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
51086be9f0dSKonstantin Belousov 	struct sf_buf *sf;
5111abfd355SKonstantin Belousov 	int bus, slot, func, error;
51286be9f0dSKonstantin Belousov 	bool enable;
51386be9f0dSKonstantin Belousov 
514f9feb091SKonstantin Belousov 	if (dev != NULL) {
51567499354SRyan Stone 		bus = pci_get_bus(dev);
51667499354SRyan Stone 		slot = pci_get_slot(dev);
51767499354SRyan Stone 		func = pci_get_function(dev);
518f9feb091SKonstantin Belousov 	} else {
519f9feb091SKonstantin Belousov 		bus = PCI_RID2BUS(rid);
520f9feb091SKonstantin Belousov 		slot = PCI_RID2SLOT(rid);
521f9feb091SKonstantin Belousov 		func = PCI_RID2FUNC(rid);
522f9feb091SKonstantin Belousov 	}
52386be9f0dSKonstantin Belousov 	enable = false;
52486be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
52578b51754SRuslan Bukin 	unit = DMAR2IOMMU(dmar);
52686be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
527ea4c0115SRuslan Bukin 	KASSERT(!iommu_is_buswide_ctx(unit, bus) || (slot == 0 && func == 0),
528ea4c0115SRuslan Bukin 	    ("iommu%d pci%d:%d:%d get_ctx for buswide", dmar->iommu.unit, bus,
529685666aaSKonstantin Belousov 	    slot, func));
53067499354SRyan Stone 	ctx = dmar_find_ctx_locked(dmar, rid);
53186be9f0dSKonstantin Belousov 	error = 0;
53286be9f0dSKonstantin Belousov 	if (ctx == NULL) {
53386be9f0dSKonstantin Belousov 		/*
53486be9f0dSKonstantin Belousov 		 * Perform the allocations which require sleep or have
53586be9f0dSKonstantin Belousov 		 * higher chance to succeed if the sleep is allowed.
53686be9f0dSKonstantin Belousov 		 */
53786be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
538b29d186cSKonstantin Belousov 		dmar_ensure_ctx_page(dmar, PCI_RID2BUS(rid));
5391abfd355SKonstantin Belousov 		domain1 = dmar_domain_alloc(dmar, id_mapped);
5401abfd355SKonstantin Belousov 		if (domain1 == NULL) {
54186be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
54286be9f0dSKonstantin Belousov 			return (NULL);
54386be9f0dSKonstantin Belousov 		}
5445f8e5c7fSKonstantin Belousov 		if (!id_mapped) {
545f9feb091SKonstantin Belousov 			error = domain_init_rmrr(domain1, dev, bus,
546f9feb091SKonstantin Belousov 			    slot, func, dev_domain, dev_busno, dev_path,
547f9feb091SKonstantin Belousov 			    dev_path_len);
5483c02da80SKornel Duleba 			if (error == 0 && dev != NULL)
549ee47a12aSRyan Libby 				error = dmar_reserve_pci_regions(domain1, dev);
55086be9f0dSKonstantin Belousov 			if (error != 0) {
5511abfd355SKonstantin Belousov 				dmar_domain_destroy(domain1);
55286be9f0dSKonstantin Belousov 				TD_PINNED_ASSERT;
55386be9f0dSKonstantin Belousov 				return (NULL);
55486be9f0dSKonstantin Belousov 			}
5555f8e5c7fSKonstantin Belousov 		}
5561abfd355SKonstantin Belousov 		ctx1 = dmar_ctx_alloc(domain1, rid);
55786be9f0dSKonstantin Belousov 		ctxp = dmar_map_ctx_entry(ctx1, &sf);
55886be9f0dSKonstantin Belousov 		DMAR_LOCK(dmar);
55986be9f0dSKonstantin Belousov 
56086be9f0dSKonstantin Belousov 		/*
56186be9f0dSKonstantin Belousov 		 * Recheck the contexts, other thread might have
56286be9f0dSKonstantin Belousov 		 * already allocated needed one.
56386be9f0dSKonstantin Belousov 		 */
56467499354SRyan Stone 		ctx = dmar_find_ctx_locked(dmar, rid);
56586be9f0dSKonstantin Belousov 		if (ctx == NULL) {
5661abfd355SKonstantin Belousov 			domain = domain1;
56786be9f0dSKonstantin Belousov 			ctx = ctx1;
5681abfd355SKonstantin Belousov 			dmar_ctx_link(ctx);
56959e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
570ff54674bSKonstantin Belousov 			iommu_device_tag_init(CTX2IOCTX(ctx), dev);
57186be9f0dSKonstantin Belousov 
57286be9f0dSKonstantin Belousov 			/*
57386be9f0dSKonstantin Belousov 			 * This is the first activated context for the
57486be9f0dSKonstantin Belousov 			 * DMAR unit.  Enable the translation after
57586be9f0dSKonstantin Belousov 			 * everything is set up.
57686be9f0dSKonstantin Belousov 			 */
5771abfd355SKonstantin Belousov 			if (LIST_EMPTY(&dmar->domains))
57886be9f0dSKonstantin Belousov 				enable = true;
5791abfd355SKonstantin Belousov 			LIST_INSERT_HEAD(&dmar->domains, domain, link);
580685666aaSKonstantin Belousov 			ctx_id_entry_init(ctx, ctxp, false, bus);
581f9feb091SKonstantin Belousov 			if (dev != NULL) {
58286be9f0dSKonstantin Belousov 				device_printf(dev,
58334e8337bSKonstantin Belousov 			    "dmar%d pci%d:%d:%d:%d rid %x domain %d mgaw %d "
5849d0bc6d8SKonstantin Belousov 				    "agaw %d %s-mapped\n",
58559e37c8aSRuslan Bukin 				    dmar->iommu.unit, dmar->segment, bus, slot,
5861abfd355SKonstantin Belousov 				    func, rid, domain->domain, domain->mgaw,
5871abfd355SKonstantin Belousov 				    domain->agaw, id_mapped ? "id" : "re");
588f9feb091SKonstantin Belousov 			}
58940d951bcSKonstantin Belousov 			iommu_unmap_pgtbl(sf);
59086be9f0dSKonstantin Belousov 		} else {
59140d951bcSKonstantin Belousov 			iommu_unmap_pgtbl(sf);
5921abfd355SKonstantin Belousov 			dmar_domain_destroy(domain1);
5933d47c58bSKonstantin Belousov 			/* Nothing needs to be done to destroy ctx1. */
5943d47c58bSKonstantin Belousov 			free(ctx1, M_DMAR_CTX);
59578b51754SRuslan Bukin 			domain = CTX2DOM(ctx);
596*e9d948cfSKonstantin Belousov 			ctx->context.refs++; /* tag referenced us */
59786be9f0dSKonstantin Belousov 		}
59868eeb96aSKonstantin Belousov 	} else {
59978b51754SRuslan Bukin 		domain = CTX2DOM(ctx);
60059e37c8aSRuslan Bukin 		if (ctx->context.tag->owner == NULL)
60159e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
602*e9d948cfSKonstantin Belousov 		ctx->context.refs++; /* tag referenced us */
6031abfd355SKonstantin Belousov 	}
6041abfd355SKonstantin Belousov 
6051abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, enable);
60686be9f0dSKonstantin Belousov 	if (error != 0) {
60786be9f0dSKonstantin Belousov 		dmar_free_ctx_locked(dmar, ctx);
60886be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
60986be9f0dSKonstantin Belousov 		return (NULL);
61086be9f0dSKonstantin Belousov 	}
61168eeb96aSKonstantin Belousov 
61268eeb96aSKonstantin Belousov 	/*
61368eeb96aSKonstantin Belousov 	 * The dmar lock was potentially dropped between check for the
61468eeb96aSKonstantin Belousov 	 * empty context list and now.  Recheck the state of GCMD_TE
61568eeb96aSKonstantin Belousov 	 * to avoid unneeded command.
61668eeb96aSKonstantin Belousov 	 */
61768eeb96aSKonstantin Belousov 	if (enable && !rmrr_init && (dmar->hw_gcmd & DMAR_GCMD_TE) == 0) {
61806e6ca6dSKornel Duleba 		error = dmar_disable_protected_regions(dmar);
61906e6ca6dSKornel Duleba 		if (error != 0)
62006e6ca6dSKornel Duleba 			printf("dmar%d: Failed to disable protected regions\n",
62106e6ca6dSKornel Duleba 			    dmar->iommu.unit);
62286be9f0dSKonstantin Belousov 		error = dmar_enable_translation(dmar);
623f9feb091SKonstantin Belousov 		if (error == 0) {
624f9feb091SKonstantin Belousov 			if (bootverbose) {
625f9feb091SKonstantin Belousov 				printf("dmar%d: enabled translation\n",
62659e37c8aSRuslan Bukin 				    dmar->iommu.unit);
627f9feb091SKonstantin Belousov 			}
628f9feb091SKonstantin Belousov 		} else {
629f9feb091SKonstantin Belousov 			printf("dmar%d: enabling translation failed, "
63059e37c8aSRuslan Bukin 			    "error %d\n", dmar->iommu.unit, error);
63186be9f0dSKonstantin Belousov 			dmar_free_ctx_locked(dmar, ctx);
63286be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
63386be9f0dSKonstantin Belousov 			return (NULL);
63486be9f0dSKonstantin Belousov 		}
63586be9f0dSKonstantin Belousov 	}
63686be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
63786be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
63886be9f0dSKonstantin Belousov 	return (ctx);
63986be9f0dSKonstantin Belousov }
64086be9f0dSKonstantin Belousov 
641f9feb091SKonstantin Belousov struct dmar_ctx *
642f9feb091SKonstantin Belousov dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev, uint16_t rid,
643f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
644f9feb091SKonstantin Belousov {
645f9feb091SKonstantin Belousov 	int dev_domain, dev_path_len, dev_busno;
646f9feb091SKonstantin Belousov 
647f9feb091SKonstantin Belousov 	dev_domain = pci_get_domain(dev);
648f9feb091SKonstantin Belousov 	dev_path_len = dmar_dev_depth(dev);
649f9feb091SKonstantin Belousov 	ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
650f9feb091SKonstantin Belousov 	dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
651f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, dev, rid, dev_domain, dev_busno,
652f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
653f9feb091SKonstantin Belousov }
654f9feb091SKonstantin Belousov 
655f9feb091SKonstantin Belousov struct dmar_ctx *
656f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
657f9feb091SKonstantin Belousov     int dev_domain, int dev_busno,
658f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len,
659f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
660f9feb091SKonstantin Belousov {
661f9feb091SKonstantin Belousov 
662f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, NULL, rid, dev_domain, dev_busno,
663f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
664f9feb091SKonstantin Belousov }
665f9feb091SKonstantin Belousov 
6661abfd355SKonstantin Belousov int
6671abfd355SKonstantin Belousov dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx)
6681abfd355SKonstantin Belousov {
6691abfd355SKonstantin Belousov 	struct dmar_unit *dmar;
6701abfd355SKonstantin Belousov 	struct dmar_domain *old_domain;
6711abfd355SKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
6721abfd355SKonstantin Belousov 	struct sf_buf *sf;
6731abfd355SKonstantin Belousov 	int error;
6741abfd355SKonstantin Belousov 
6751abfd355SKonstantin Belousov 	dmar = domain->dmar;
67678b51754SRuslan Bukin 	old_domain = CTX2DOM(ctx);
6771abfd355SKonstantin Belousov 	if (domain == old_domain)
6781abfd355SKonstantin Belousov 		return (0);
67959e37c8aSRuslan Bukin 	KASSERT(old_domain->iodom.iommu == domain->iodom.iommu,
6801abfd355SKonstantin Belousov 	    ("domain %p %u moving between dmars %u %u", domain,
68159e37c8aSRuslan Bukin 	    domain->domain, old_domain->iodom.iommu->unit,
68259e37c8aSRuslan Bukin 	    domain->iodom.iommu->unit));
6831abfd355SKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
6841abfd355SKonstantin Belousov 
6851abfd355SKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
6861abfd355SKonstantin Belousov 	DMAR_LOCK(dmar);
6871abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
68859e37c8aSRuslan Bukin 	ctx->context.domain = &domain->iodom;
6891abfd355SKonstantin Belousov 	dmar_ctx_link(ctx);
690685666aaSKonstantin Belousov 	ctx_id_entry_init(ctx, ctxp, true, PCI_BUSMAX + 100);
69140d951bcSKonstantin Belousov 	iommu_unmap_pgtbl(sf);
6921abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, true);
6931abfd355SKonstantin Belousov 	/* If flush failed, rolling back would not work as well. */
6941abfd355SKonstantin Belousov 	printf("dmar%d rid %x domain %d->%d %s-mapped\n",
695cb9050ddSRuslan Bukin 	    dmar->iommu.unit, ctx->context.rid, old_domain->domain,
696cb9050ddSRuslan Bukin 	    domain->domain, (domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 ?
697cb9050ddSRuslan Bukin 	    "id" : "re");
6981abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, old_domain);
6991abfd355SKonstantin Belousov 	TD_PINNED_ASSERT;
7001abfd355SKonstantin Belousov 	return (error);
7011abfd355SKonstantin Belousov }
7021abfd355SKonstantin Belousov 
7031abfd355SKonstantin Belousov static void
7041abfd355SKonstantin Belousov dmar_unref_domain_locked(struct dmar_unit *dmar, struct dmar_domain *domain)
7051abfd355SKonstantin Belousov {
7061abfd355SKonstantin Belousov 
7071abfd355SKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
7081abfd355SKonstantin Belousov 	KASSERT(domain->refs >= 1,
70959e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %u", dmar->iommu.unit, domain,
71059e37c8aSRuslan Bukin 	    domain->refs));
7111abfd355SKonstantin Belousov 	KASSERT(domain->refs > domain->ctx_cnt,
71259e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %d ctx_cnt %d", dmar->iommu.unit, domain,
7131abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
7141abfd355SKonstantin Belousov 
7151abfd355SKonstantin Belousov 	if (domain->refs > 1) {
7161abfd355SKonstantin Belousov 		domain->refs--;
7171abfd355SKonstantin Belousov 		DMAR_UNLOCK(dmar);
7181abfd355SKonstantin Belousov 		return;
7191abfd355SKonstantin Belousov 	}
7201abfd355SKonstantin Belousov 
72115f6baf4SRuslan Bukin 	KASSERT((domain->iodom.flags & IOMMU_DOMAIN_RMRR) == 0,
7221abfd355SKonstantin Belousov 	    ("lost ref on RMRR domain %p", domain));
7231abfd355SKonstantin Belousov 
7241abfd355SKonstantin Belousov 	LIST_REMOVE(domain, link);
7251abfd355SKonstantin Belousov 	DMAR_UNLOCK(dmar);
7261abfd355SKonstantin Belousov 
72759e37c8aSRuslan Bukin 	taskqueue_drain(dmar->iommu.delayed_taskqueue,
72859e37c8aSRuslan Bukin 	    &domain->iodom.unload_task);
7291abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
7301abfd355SKonstantin Belousov }
7311abfd355SKonstantin Belousov 
73265b133e5SKonstantin Belousov static void
73386be9f0dSKonstantin Belousov dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx)
73486be9f0dSKonstantin Belousov {
73586be9f0dSKonstantin Belousov 	struct sf_buf *sf;
73686be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
7371abfd355SKonstantin Belousov 	struct dmar_domain *domain;
73886be9f0dSKonstantin Belousov 
73986be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
740*e9d948cfSKonstantin Belousov 	KASSERT(ctx->context.refs >= 1,
741*e9d948cfSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->context.refs));
74286be9f0dSKonstantin Belousov 
74386be9f0dSKonstantin Belousov 	/*
74486be9f0dSKonstantin Belousov 	 * If our reference is not last, only the dereference should
74586be9f0dSKonstantin Belousov 	 * be performed.
74686be9f0dSKonstantin Belousov 	 */
747*e9d948cfSKonstantin Belousov 	if (ctx->context.refs > 1) {
748*e9d948cfSKonstantin Belousov 		ctx->context.refs--;
74986be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
75086be9f0dSKonstantin Belousov 		return;
75186be9f0dSKonstantin Belousov 	}
75286be9f0dSKonstantin Belousov 
75359e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
75486be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
75586be9f0dSKonstantin Belousov 
75686be9f0dSKonstantin Belousov 	/*
75786be9f0dSKonstantin Belousov 	 * Otherwise, the context entry must be cleared before the
75886be9f0dSKonstantin Belousov 	 * page table is destroyed.  The mapping of the context
75986be9f0dSKonstantin Belousov 	 * entries page could require sleep, unlock the dmar.
76086be9f0dSKonstantin Belousov 	 */
76186be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
76286be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
76386be9f0dSKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
76486be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
765*e9d948cfSKonstantin Belousov 	KASSERT(ctx->context.refs >= 1,
766*e9d948cfSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->context.refs));
76786be9f0dSKonstantin Belousov 
76886be9f0dSKonstantin Belousov 	/*
76986be9f0dSKonstantin Belousov 	 * Other thread might have referenced the context, in which
77086be9f0dSKonstantin Belousov 	 * case again only the dereference should be performed.
77186be9f0dSKonstantin Belousov 	 */
772*e9d948cfSKonstantin Belousov 	if (ctx->context.refs > 1) {
773*e9d948cfSKonstantin Belousov 		ctx->context.refs--;
77486be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
77540d951bcSKonstantin Belousov 		iommu_unmap_pgtbl(sf);
77686be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
77786be9f0dSKonstantin Belousov 		return;
77886be9f0dSKonstantin Belousov 	}
77986be9f0dSKonstantin Belousov 
78059e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
78186be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
78286be9f0dSKonstantin Belousov 
78386be9f0dSKonstantin Belousov 	/*
78486be9f0dSKonstantin Belousov 	 * Clear the context pointer and flush the caches.
78586be9f0dSKonstantin Belousov 	 * XXXKIB: cannot do this if any RMRR entries are still present.
78686be9f0dSKonstantin Belousov 	 */
78786be9f0dSKonstantin Belousov 	dmar_pte_clear(&ctxp->ctx1);
78886be9f0dSKonstantin Belousov 	ctxp->ctx2 = 0;
7896b7c46afSKonstantin Belousov 	dmar_flush_ctx_to_ram(dmar, ctxp);
79086be9f0dSKonstantin Belousov 	dmar_inv_ctx_glob(dmar);
79168eeb96aSKonstantin Belousov 	if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) {
79268eeb96aSKonstantin Belousov 		if (dmar->qi_enabled)
79368eeb96aSKonstantin Belousov 			dmar_qi_invalidate_iotlb_glob_locked(dmar);
79468eeb96aSKonstantin Belousov 		else
79586be9f0dSKonstantin Belousov 			dmar_inv_iotlb_glob(dmar);
79668eeb96aSKonstantin Belousov 	}
79740d951bcSKonstantin Belousov 	iommu_unmap_pgtbl(sf);
79878b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
7991abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
80059e37c8aSRuslan Bukin 	free(ctx->context.tag, M_DMAR_CTX);
8011abfd355SKonstantin Belousov 	free(ctx, M_DMAR_CTX);
8021abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, domain);
80386be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
80486be9f0dSKonstantin Belousov }
80586be9f0dSKonstantin Belousov 
80665b133e5SKonstantin Belousov static void
80786be9f0dSKonstantin Belousov dmar_free_ctx(struct dmar_ctx *ctx)
80886be9f0dSKonstantin Belousov {
80986be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
81086be9f0dSKonstantin Belousov 
81178b51754SRuslan Bukin 	dmar = CTX2DMAR(ctx);
81286be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
81386be9f0dSKonstantin Belousov 	dmar_free_ctx_locked(dmar, ctx);
81486be9f0dSKonstantin Belousov }
81586be9f0dSKonstantin Belousov 
8161abfd355SKonstantin Belousov /*
8171abfd355SKonstantin Belousov  * Returns with the domain locked.
8181abfd355SKonstantin Belousov  */
81986be9f0dSKonstantin Belousov struct dmar_ctx *
82067499354SRyan Stone dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid)
82186be9f0dSKonstantin Belousov {
8221abfd355SKonstantin Belousov 	struct dmar_domain *domain;
823*e9d948cfSKonstantin Belousov 	struct iommu_ctx *ctx;
82486be9f0dSKonstantin Belousov 
82586be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
82686be9f0dSKonstantin Belousov 
8271abfd355SKonstantin Belousov 	LIST_FOREACH(domain, &dmar->domains, link) {
828*e9d948cfSKonstantin Belousov 		LIST_FOREACH(ctx, &domain->iodom.contexts, link) {
829*e9d948cfSKonstantin Belousov 			if (ctx->rid == rid)
830*e9d948cfSKonstantin Belousov 				return (IOCTX2CTX(ctx));
83186be9f0dSKonstantin Belousov 		}
8321abfd355SKonstantin Belousov 	}
83386be9f0dSKonstantin Belousov 	return (NULL);
83486be9f0dSKonstantin Belousov }
83586be9f0dSKonstantin Belousov 
83642736dc4SAlan Cox /*
83742736dc4SAlan Cox  * If the given value for "free" is true, then the caller must not be using
83842736dc4SAlan Cox  * the entry's dmamap_link field.
83942736dc4SAlan Cox  */
84068eeb96aSKonstantin Belousov void
84165b133e5SKonstantin Belousov dmar_domain_unload_entry(struct iommu_map_entry *entry, bool free,
8428bc36738SAlan Cox     bool cansleep)
84368eeb96aSKonstantin Belousov {
84459e37c8aSRuslan Bukin 	struct dmar_domain *domain;
84568eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
84668eeb96aSKonstantin Belousov 
84778b51754SRuslan Bukin 	domain = IODOM2DOM(entry->domain);
84878b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
8498bc36738SAlan Cox 
8508bc36738SAlan Cox 	/*
8518bc36738SAlan Cox 	 * If "free" is false, then the IOTLB invalidation must be performed
8528bc36738SAlan Cox 	 * synchronously.  Otherwise, the caller might free the entry before
8538bc36738SAlan Cox 	 * dmar_qi_task() is finished processing it.
8548bc36738SAlan Cox 	 */
85568eeb96aSKonstantin Belousov 	if (unit->qi_enabled) {
8568bc36738SAlan Cox 		if (free) {
857c2515634SAlan Cox 			DMAR_LOCK(unit);
858ad794e6dSKonstantin Belousov 			iommu_qi_invalidate_locked(&domain->iodom, entry,
859ad794e6dSKonstantin Belousov 			    true);
86068eeb96aSKonstantin Belousov 			DMAR_UNLOCK(unit);
86168eeb96aSKonstantin Belousov 		} else {
862ad794e6dSKonstantin Belousov 			iommu_qi_invalidate_sync(&domain->iodom, entry->start,
863c2515634SAlan Cox 			    entry->end - entry->start, cansleep);
864b3042e3aSKonstantin Belousov 			iommu_domain_free_entry(entry, false);
865c2515634SAlan Cox 		}
866c2515634SAlan Cox 	} else {
86710ebd230SKonstantin Belousov 		dmar_flush_iotlb_sync(domain, entry->start, entry->end -
8688bc36738SAlan Cox 		    entry->start);
869b3042e3aSKonstantin Belousov 		iommu_domain_free_entry(entry, free);
87068eeb96aSKonstantin Belousov 	}
87168eeb96aSKonstantin Belousov }
87268eeb96aSKonstantin Belousov 
873cf619a92SKonstantin Belousov static bool
874cf619a92SKonstantin Belousov dmar_domain_unload_emit_wait(struct dmar_domain *domain,
87559e37c8aSRuslan Bukin     struct iommu_map_entry *entry)
876e164cafcSKonstantin Belousov {
877e164cafcSKonstantin Belousov 
878cf619a92SKonstantin Belousov 	if (TAILQ_NEXT(entry, dmamap_link) == NULL)
879cf619a92SKonstantin Belousov 		return (true);
8800386b245SKonstantin Belousov 	return (domain->batch_no++ % iommu_qi_batch_coalesce == 0);
881e164cafcSKonstantin Belousov }
882e164cafcSKonstantin Belousov 
88368eeb96aSKonstantin Belousov void
88465b133e5SKonstantin Belousov dmar_domain_unload(struct iommu_domain *iodom,
88559e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *entries, bool cansleep)
88686be9f0dSKonstantin Belousov {
887da55f86cSAlan Cox 	struct dmar_domain *domain;
88868eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
88959e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
890661bd70bSKonstantin Belousov 	int error __diagused;
89186be9f0dSKonstantin Belousov 
892da55f86cSAlan Cox 	domain = IODOM2DOM(iodom);
89378b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
89468eeb96aSKonstantin Belousov 
89568eeb96aSKonstantin Belousov 	TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) {
89659e37c8aSRuslan Bukin 		KASSERT((entry->flags & IOMMU_MAP_ENTRY_MAP) != 0,
8971abfd355SKonstantin Belousov 		    ("not mapped entry %p %p", domain, entry));
898bdd5eb33SKonstantin Belousov 		error = iodom->ops->unmap(iodom, entry,
899bdd5eb33SKonstantin Belousov 		    cansleep ? IOMMU_PGF_WAITOK : 0);
9001abfd355SKonstantin Belousov 		KASSERT(error == 0, ("unmap %p error %d", domain, error));
90168eeb96aSKonstantin Belousov 		if (!unit->qi_enabled) {
90210ebd230SKonstantin Belousov 			dmar_flush_iotlb_sync(domain, entry->start,
90368eeb96aSKonstantin Belousov 			    entry->end - entry->start);
90468eeb96aSKonstantin Belousov 			TAILQ_REMOVE(entries, entry, dmamap_link);
905b3042e3aSKonstantin Belousov 			iommu_domain_free_entry(entry, true);
90686be9f0dSKonstantin Belousov 		}
90786be9f0dSKonstantin Belousov 	}
90868eeb96aSKonstantin Belousov 	if (TAILQ_EMPTY(entries))
90968eeb96aSKonstantin Belousov 		return;
91068eeb96aSKonstantin Belousov 
91168eeb96aSKonstantin Belousov 	KASSERT(unit->qi_enabled, ("loaded entry left"));
91268eeb96aSKonstantin Belousov 	DMAR_LOCK(unit);
91342736dc4SAlan Cox 	while ((entry = TAILQ_FIRST(entries)) != NULL) {
91442736dc4SAlan Cox 		TAILQ_REMOVE(entries, entry, dmamap_link);
915ad794e6dSKonstantin Belousov 		iommu_qi_invalidate_locked(&domain->iodom, entry,
916cf619a92SKonstantin Belousov 		    dmar_domain_unload_emit_wait(domain, entry));
91768eeb96aSKonstantin Belousov 	}
91868eeb96aSKonstantin Belousov 	DMAR_UNLOCK(unit);
91968eeb96aSKonstantin Belousov }
92086be9f0dSKonstantin Belousov 
92159e37c8aSRuslan Bukin struct iommu_ctx *
92265b133e5SKonstantin Belousov dmar_get_ctx(struct iommu_unit *iommu, device_t dev, uint16_t rid,
92359e37c8aSRuslan Bukin     bool id_mapped, bool rmrr_init)
92459e37c8aSRuslan Bukin {
92559e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
92659e37c8aSRuslan Bukin 	struct dmar_ctx *ret;
92759e37c8aSRuslan Bukin 
92878b51754SRuslan Bukin 	dmar = IOMMU2DMAR(iommu);
92959e37c8aSRuslan Bukin 	ret = dmar_get_ctx_for_dev(dmar, dev, rid, id_mapped, rmrr_init);
93078b51754SRuslan Bukin 	return (CTX2IOCTX(ret));
93159e37c8aSRuslan Bukin }
93259e37c8aSRuslan Bukin 
93359e37c8aSRuslan Bukin void
93465b133e5SKonstantin Belousov dmar_free_ctx_locked_method(struct iommu_unit *iommu,
93565b133e5SKonstantin Belousov     struct iommu_ctx *context)
93659e37c8aSRuslan Bukin {
93759e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
93859e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
93959e37c8aSRuslan Bukin 
94078b51754SRuslan Bukin 	dmar = IOMMU2DMAR(iommu);
94178b51754SRuslan Bukin 	ctx = IOCTX2CTX(context);
94259e37c8aSRuslan Bukin 	dmar_free_ctx_locked(dmar, ctx);
94359e37c8aSRuslan Bukin }
94459e37c8aSRuslan Bukin 
94559e37c8aSRuslan Bukin void
94665b133e5SKonstantin Belousov dmar_free_ctx_method(struct iommu_ctx *context)
94759e37c8aSRuslan Bukin {
94859e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
94959e37c8aSRuslan Bukin 
95078b51754SRuslan Bukin 	ctx = IOCTX2CTX(context);
95159e37c8aSRuslan Bukin 	dmar_free_ctx(ctx);
95259e37c8aSRuslan Bukin }
953