xref: /freebsd/sys/x86/iommu/intel_ctx.c (revision 59e37c8a54091ba40db199cd9c200f10683b2af2)
186be9f0dSKonstantin Belousov /*-
2ebf5747bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3ebf5747bSPedro F. Giffuni  *
486be9f0dSKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
586be9f0dSKonstantin Belousov  * All rights reserved.
686be9f0dSKonstantin Belousov  *
786be9f0dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
886be9f0dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
986be9f0dSKonstantin Belousov  *
1086be9f0dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
1186be9f0dSKonstantin Belousov  * modification, are permitted provided that the following conditions
1286be9f0dSKonstantin Belousov  * are met:
1386be9f0dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1486be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1586be9f0dSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1686be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1786be9f0dSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1886be9f0dSKonstantin Belousov  *
1986be9f0dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2086be9f0dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2186be9f0dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2286be9f0dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2386be9f0dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2486be9f0dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2586be9f0dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2686be9f0dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2786be9f0dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2886be9f0dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2986be9f0dSKonstantin Belousov  * SUCH DAMAGE.
3086be9f0dSKonstantin Belousov  */
3186be9f0dSKonstantin Belousov 
3286be9f0dSKonstantin Belousov #include <sys/cdefs.h>
3386be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$");
3486be9f0dSKonstantin Belousov 
3586be9f0dSKonstantin Belousov #include <sys/param.h>
3686be9f0dSKonstantin Belousov #include <sys/systm.h>
3786be9f0dSKonstantin Belousov #include <sys/malloc.h>
3886be9f0dSKonstantin Belousov #include <sys/bus.h>
3986be9f0dSKonstantin Belousov #include <sys/interrupt.h>
4086be9f0dSKonstantin Belousov #include <sys/kernel.h>
4186be9f0dSKonstantin Belousov #include <sys/ktr.h>
4286be9f0dSKonstantin Belousov #include <sys/limits.h>
4386be9f0dSKonstantin Belousov #include <sys/lock.h>
4486be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4586be9f0dSKonstantin Belousov #include <sys/mutex.h>
4686be9f0dSKonstantin Belousov #include <sys/proc.h>
4786be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4886be9f0dSKonstantin Belousov #include <sys/rman.h>
4986be9f0dSKonstantin Belousov #include <sys/sysctl.h>
5086be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
5186be9f0dSKonstantin Belousov #include <sys/tree.h>
5286be9f0dSKonstantin Belousov #include <sys/uio.h>
530a110d5bSKonstantin Belousov #include <sys/vmem.h>
5486be9f0dSKonstantin Belousov #include <vm/vm.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5786be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5886be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5986be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
6086be9f0dSKonstantin Belousov #include <vm/vm_map.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>
65f9feb091SKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h>
66f9feb091SKonstantin Belousov #include <contrib/dev/acpica/include/accommon.h>
6786be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
6886be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h>
6986be9f0dSKonstantin Belousov #include <x86/iommu/busdma_dmar.h>
7067499354SRyan Stone #include <dev/pci/pcireg.h>
71685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7286be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h>
7386be9f0dSKonstantin Belousov 
7486be9f0dSKonstantin Belousov static MALLOC_DEFINE(M_DMAR_CTX, "dmar_ctx", "Intel DMAR Context");
751abfd355SKonstantin Belousov static MALLOC_DEFINE(M_DMAR_DOMAIN, "dmar_dom", "Intel DMAR Domain");
7686be9f0dSKonstantin Belousov 
771abfd355SKonstantin Belousov static void dmar_domain_unload_task(void *arg, int pending);
781abfd355SKonstantin Belousov static void dmar_unref_domain_locked(struct dmar_unit *dmar,
791abfd355SKonstantin Belousov     struct dmar_domain *domain);
801abfd355SKonstantin Belousov static void dmar_domain_destroy(struct dmar_domain *domain);
8186be9f0dSKonstantin Belousov 
8286be9f0dSKonstantin Belousov static void
8386be9f0dSKonstantin Belousov dmar_ensure_ctx_page(struct dmar_unit *dmar, int bus)
8486be9f0dSKonstantin Belousov {
8586be9f0dSKonstantin Belousov 	struct sf_buf *sf;
8686be9f0dSKonstantin Belousov 	dmar_root_entry_t *re;
8786be9f0dSKonstantin Belousov 	vm_page_t ctxm;
8886be9f0dSKonstantin Belousov 
8986be9f0dSKonstantin Belousov 	/*
9086be9f0dSKonstantin Belousov 	 * Allocated context page must be linked.
9186be9f0dSKonstantin Belousov 	 */
9286be9f0dSKonstantin Belousov 	ctxm = dmar_pgalloc(dmar->ctx_obj, 1 + bus, DMAR_PGF_NOALLOC);
9386be9f0dSKonstantin Belousov 	if (ctxm != NULL)
9486be9f0dSKonstantin Belousov 		return;
9586be9f0dSKonstantin Belousov 
9686be9f0dSKonstantin Belousov 	/*
9786be9f0dSKonstantin Belousov 	 * Page not present, allocate and link.  Note that other
9886be9f0dSKonstantin Belousov 	 * thread might execute this sequence in parallel.  This
9986be9f0dSKonstantin Belousov 	 * should be safe, because the context entries written by both
10086be9f0dSKonstantin Belousov 	 * threads are equal.
10186be9f0dSKonstantin Belousov 	 */
10286be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
10386be9f0dSKonstantin Belousov 	ctxm = dmar_pgalloc(dmar->ctx_obj, 1 + bus, DMAR_PGF_ZERO |
10486be9f0dSKonstantin Belousov 	    DMAR_PGF_WAITOK);
10586be9f0dSKonstantin Belousov 	re = dmar_map_pgtbl(dmar->ctx_obj, 0, DMAR_PGF_NOALLOC, &sf);
10686be9f0dSKonstantin Belousov 	re += bus;
10786be9f0dSKonstantin Belousov 	dmar_pte_store(&re->r1, DMAR_ROOT_R1_P | (DMAR_ROOT_R1_CTP_MASK &
10886be9f0dSKonstantin Belousov 	    VM_PAGE_TO_PHYS(ctxm)));
1096b7c46afSKonstantin Belousov 	dmar_flush_root_to_ram(dmar, re);
1106b7c46afSKonstantin Belousov 	dmar_unmap_pgtbl(sf);
11186be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
11286be9f0dSKonstantin Belousov }
11386be9f0dSKonstantin Belousov 
11486be9f0dSKonstantin Belousov static dmar_ctx_entry_t *
11586be9f0dSKonstantin Belousov dmar_map_ctx_entry(struct dmar_ctx *ctx, struct sf_buf **sfp)
11686be9f0dSKonstantin Belousov {
117*59e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
11886be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
11986be9f0dSKonstantin Belousov 
120*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)ctx->context.domain->iommu;
121*59e37c8aSRuslan Bukin 
122*59e37c8aSRuslan Bukin 	ctxp = dmar_map_pgtbl(dmar->ctx_obj, 1 +
1231abfd355SKonstantin Belousov 	    PCI_RID2BUS(ctx->rid), DMAR_PGF_NOALLOC | DMAR_PGF_WAITOK, sfp);
12467499354SRyan Stone 	ctxp += ctx->rid & 0xff;
12586be9f0dSKonstantin Belousov 	return (ctxp);
12686be9f0dSKonstantin Belousov }
12786be9f0dSKonstantin Belousov 
12886be9f0dSKonstantin Belousov static void
129*59e37c8aSRuslan Bukin device_tag_init(struct dmar_ctx *ctx, device_t dev)
13086be9f0dSKonstantin Belousov {
131*59e37c8aSRuslan Bukin 	struct dmar_domain *domain;
13286be9f0dSKonstantin Belousov 	bus_addr_t maxaddr;
13386be9f0dSKonstantin Belousov 
134*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)ctx->context.domain;
135*59e37c8aSRuslan Bukin 	maxaddr = MIN(domain->end, BUS_SPACE_MAXADDR);
136*59e37c8aSRuslan Bukin 	ctx->context.tag->common.ref_count = 1; /* Prevent free */
137*59e37c8aSRuslan Bukin 	ctx->context.tag->common.impl = &bus_dma_iommu_impl;
138*59e37c8aSRuslan Bukin 	ctx->context.tag->common.boundary = 0;
139*59e37c8aSRuslan Bukin 	ctx->context.tag->common.lowaddr = maxaddr;
140*59e37c8aSRuslan Bukin 	ctx->context.tag->common.highaddr = maxaddr;
141*59e37c8aSRuslan Bukin 	ctx->context.tag->common.maxsize = maxaddr;
142*59e37c8aSRuslan Bukin 	ctx->context.tag->common.nsegments = BUS_SPACE_UNRESTRICTED;
143*59e37c8aSRuslan Bukin 	ctx->context.tag->common.maxsegsz = maxaddr;
144*59e37c8aSRuslan Bukin 	ctx->context.tag->ctx = (struct iommu_ctx *)ctx;
145*59e37c8aSRuslan Bukin 	ctx->context.tag->owner = dev;
14686be9f0dSKonstantin Belousov }
14786be9f0dSKonstantin Belousov 
14886be9f0dSKonstantin Belousov static void
149685666aaSKonstantin Belousov ctx_id_entry_init_one(dmar_ctx_entry_t *ctxp, struct dmar_domain *domain,
150685666aaSKonstantin Belousov     vm_page_t ctx_root)
15186be9f0dSKonstantin Belousov {
1521abfd355SKonstantin Belousov 	/*
1531abfd355SKonstantin Belousov 	 * For update due to move, the store is not atomic.  It is
1541abfd355SKonstantin Belousov 	 * possible that DMAR read upper doubleword, while low
1551abfd355SKonstantin Belousov 	 * doubleword is not yet updated.  The domain id is stored in
1561abfd355SKonstantin Belousov 	 * the upper doubleword, while the table pointer in the lower.
1571abfd355SKonstantin Belousov 	 *
1581abfd355SKonstantin Belousov 	 * There is no good solution, for the same reason it is wrong
1591abfd355SKonstantin Belousov 	 * to clear P bit in the ctx entry for update.
1601abfd355SKonstantin Belousov 	 */
1611abfd355SKonstantin Belousov 	dmar_pte_store1(&ctxp->ctx2, DMAR_CTX2_DID(domain->domain) |
1621abfd355SKonstantin Belousov 	    domain->awlvl);
163685666aaSKonstantin Belousov 	if (ctx_root == NULL) {
164685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_PASS | DMAR_CTX1_P);
165685666aaSKonstantin Belousov 	} else {
166685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_UNTR |
167685666aaSKonstantin Belousov 		    (DMAR_CTX1_ASR_MASK & VM_PAGE_TO_PHYS(ctx_root)) |
168685666aaSKonstantin Belousov 		    DMAR_CTX1_P);
169685666aaSKonstantin Belousov 	}
170685666aaSKonstantin Belousov }
171685666aaSKonstantin Belousov 
172685666aaSKonstantin Belousov static void
173685666aaSKonstantin Belousov ctx_id_entry_init(struct dmar_ctx *ctx, dmar_ctx_entry_t *ctxp, bool move,
174685666aaSKonstantin Belousov     int busno)
175685666aaSKonstantin Belousov {
176685666aaSKonstantin Belousov 	struct dmar_unit *unit;
177685666aaSKonstantin Belousov 	struct dmar_domain *domain;
178685666aaSKonstantin Belousov 	vm_page_t ctx_root;
179685666aaSKonstantin Belousov 	int i;
180685666aaSKonstantin Belousov 
181*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)ctx->context.domain;
182*59e37c8aSRuslan Bukin 	unit = (struct dmar_unit *)domain->iodom.iommu;
183685666aaSKonstantin Belousov 	KASSERT(move || (ctxp->ctx1 == 0 && ctxp->ctx2 == 0),
184685666aaSKonstantin Belousov 	    ("dmar%d: initialized ctx entry %d:%d:%d 0x%jx 0x%jx",
185*59e37c8aSRuslan Bukin 	    unit->iommu.unit, busno, pci_get_slot(ctx->context.tag->owner),
186*59e37c8aSRuslan Bukin 	    pci_get_function(ctx->context.tag->owner),
187685666aaSKonstantin Belousov 	    ctxp->ctx1, ctxp->ctx2));
188685666aaSKonstantin Belousov 
1891abfd355SKonstantin Belousov 	if ((domain->flags & DMAR_DOMAIN_IDMAP) != 0 &&
19086be9f0dSKonstantin Belousov 	    (unit->hw_ecap & DMAR_ECAP_PT) != 0) {
1911abfd355SKonstantin Belousov 		KASSERT(domain->pgtbl_obj == NULL,
19286be9f0dSKonstantin Belousov 		    ("ctx %p non-null pgtbl_obj", ctx));
193685666aaSKonstantin Belousov 		ctx_root = NULL;
19486be9f0dSKonstantin Belousov 	} else {
1951abfd355SKonstantin Belousov 		ctx_root = dmar_pgalloc(domain->pgtbl_obj, 0, DMAR_PGF_NOALLOC);
196685666aaSKonstantin Belousov 	}
197685666aaSKonstantin Belousov 
198685666aaSKonstantin Belousov 	if (dmar_is_buswide_ctx(unit, busno)) {
199685666aaSKonstantin Belousov 		MPASS(!move);
200685666aaSKonstantin Belousov 		for (i = 0; i <= PCI_BUSMAX; i++) {
201685666aaSKonstantin Belousov 			ctx_id_entry_init_one(&ctxp[i], domain, ctx_root);
202685666aaSKonstantin Belousov 		}
203685666aaSKonstantin Belousov 	} else {
204685666aaSKonstantin Belousov 		ctx_id_entry_init_one(ctxp, domain, ctx_root);
20586be9f0dSKonstantin Belousov 	}
2066b7c46afSKonstantin Belousov 	dmar_flush_ctx_to_ram(unit, ctxp);
20786be9f0dSKonstantin Belousov }
20886be9f0dSKonstantin Belousov 
20986be9f0dSKonstantin Belousov static int
2101abfd355SKonstantin Belousov dmar_flush_for_ctx_entry(struct dmar_unit *dmar, bool force)
2111abfd355SKonstantin Belousov {
2121abfd355SKonstantin Belousov 	int error;
2131abfd355SKonstantin Belousov 
2141abfd355SKonstantin Belousov 	/*
2151abfd355SKonstantin Belousov 	 * If dmar declares Caching Mode as Set, follow 11.5 "Caching
2161abfd355SKonstantin Belousov 	 * Mode Consideration" and do the (global) invalidation of the
2171abfd355SKonstantin Belousov 	 * negative TLB entries.
2181abfd355SKonstantin Belousov 	 */
2191abfd355SKonstantin Belousov 	if ((dmar->hw_cap & DMAR_CAP_CM) == 0 && !force)
2201abfd355SKonstantin Belousov 		return (0);
2211abfd355SKonstantin Belousov 	if (dmar->qi_enabled) {
2221abfd355SKonstantin Belousov 		dmar_qi_invalidate_ctx_glob_locked(dmar);
2231abfd355SKonstantin Belousov 		if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force)
2241abfd355SKonstantin Belousov 			dmar_qi_invalidate_iotlb_glob_locked(dmar);
2251abfd355SKonstantin Belousov 		return (0);
2261abfd355SKonstantin Belousov 	}
2271abfd355SKonstantin Belousov 	error = dmar_inv_ctx_glob(dmar);
2281abfd355SKonstantin Belousov 	if (error == 0 && ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force))
2291abfd355SKonstantin Belousov 		error = dmar_inv_iotlb_glob(dmar);
2301abfd355SKonstantin Belousov 	return (error);
2311abfd355SKonstantin Belousov }
2321abfd355SKonstantin Belousov 
2331abfd355SKonstantin Belousov static int
234f9feb091SKonstantin Belousov domain_init_rmrr(struct dmar_domain *domain, device_t dev, int bus,
235f9feb091SKonstantin Belousov     int slot, int func, int dev_domain, int dev_busno,
236f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len)
23786be9f0dSKonstantin Belousov {
238*59e37c8aSRuslan Bukin 	struct iommu_map_entries_tailq rmrr_entries;
239*59e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
24086be9f0dSKonstantin Belousov 	vm_page_t *ma;
241*59e37c8aSRuslan Bukin 	iommu_gaddr_t start, end;
24286be9f0dSKonstantin Belousov 	vm_pindex_t size, i;
24386be9f0dSKonstantin Belousov 	int error, error1;
24486be9f0dSKonstantin Belousov 
24586be9f0dSKonstantin Belousov 	error = 0;
24686be9f0dSKonstantin Belousov 	TAILQ_INIT(&rmrr_entries);
247f9feb091SKonstantin Belousov 	dmar_dev_parse_rmrr(domain, dev_domain, dev_busno, dev_path,
248f9feb091SKonstantin Belousov 	    dev_path_len, &rmrr_entries);
24986be9f0dSKonstantin Belousov 	TAILQ_FOREACH_SAFE(entry, &rmrr_entries, unroll_link, entry1) {
25086be9f0dSKonstantin Belousov 		/*
25186be9f0dSKonstantin Belousov 		 * VT-d specification requires that the start of an
25286be9f0dSKonstantin Belousov 		 * RMRR entry is 4k-aligned.  Buggy BIOSes put
25386be9f0dSKonstantin Belousov 		 * anything into the start and end fields.  Truncate
25486be9f0dSKonstantin Belousov 		 * and round as neccesary.
25586be9f0dSKonstantin Belousov 		 *
25686be9f0dSKonstantin Belousov 		 * We also allow the overlapping RMRR entries, see
25786be9f0dSKonstantin Belousov 		 * dmar_gas_alloc_region().
25886be9f0dSKonstantin Belousov 		 */
25986be9f0dSKonstantin Belousov 		start = entry->start;
26086be9f0dSKonstantin Belousov 		end = entry->end;
261f9feb091SKonstantin Belousov 		if (bootverbose)
262f9feb091SKonstantin Belousov 			printf("dmar%d ctx pci%d:%d:%d RMRR [%#jx, %#jx]\n",
263*59e37c8aSRuslan Bukin 			    domain->iodom.iommu->unit, bus, slot, func,
264f9feb091SKonstantin Belousov 			    (uintmax_t)start, (uintmax_t)end);
26586be9f0dSKonstantin Belousov 		entry->start = trunc_page(start);
26686be9f0dSKonstantin Belousov 		entry->end = round_page(end);
267e02b05b3SKonstantin Belousov 		if (entry->start == entry->end) {
268e02b05b3SKonstantin Belousov 			/* Workaround for some AMI (?) BIOSes */
269e02b05b3SKonstantin Belousov 			if (bootverbose) {
270f9feb091SKonstantin Belousov 				if (dev != NULL)
271f9feb091SKonstantin Belousov 					device_printf(dev, "");
272f9feb091SKonstantin Belousov 				printf("pci%d:%d:%d ", bus, slot, func);
273f9feb091SKonstantin Belousov 				printf("BIOS bug: dmar%d RMRR "
274e02b05b3SKonstantin Belousov 				    "region (%jx, %jx) corrected\n",
275*59e37c8aSRuslan Bukin 				    domain->iodom.iommu->unit, start, end);
276e02b05b3SKonstantin Belousov 			}
277e02b05b3SKonstantin Belousov 			entry->end += DMAR_PAGE_SIZE * 0x20;
278e02b05b3SKonstantin Belousov 		}
27986be9f0dSKonstantin Belousov 		size = OFF_TO_IDX(entry->end - entry->start);
28086be9f0dSKonstantin Belousov 		ma = malloc(sizeof(vm_page_t) * size, M_TEMP, M_WAITOK);
28186be9f0dSKonstantin Belousov 		for (i = 0; i < size; i++) {
28286be9f0dSKonstantin Belousov 			ma[i] = vm_page_getfake(entry->start + PAGE_SIZE * i,
28386be9f0dSKonstantin Belousov 			    VM_MEMATTR_DEFAULT);
28486be9f0dSKonstantin Belousov 		}
2851abfd355SKonstantin Belousov 		error1 = dmar_gas_map_region(domain, entry,
286*59e37c8aSRuslan Bukin 		    IOMMU_MAP_ENTRY_READ | IOMMU_MAP_ENTRY_WRITE,
287*59e37c8aSRuslan Bukin 		    IOMMU_MF_CANWAIT | IOMMU_MF_RMRR, ma);
28886be9f0dSKonstantin Belousov 		/*
28986be9f0dSKonstantin Belousov 		 * Non-failed RMRR entries are owned by context rb
29086be9f0dSKonstantin Belousov 		 * tree.  Get rid of the failed entry, but do not stop
29186be9f0dSKonstantin Belousov 		 * the loop.  Rest of the parsed RMRR entries are
29286be9f0dSKonstantin Belousov 		 * loaded and removed on the context destruction.
29386be9f0dSKonstantin Belousov 		 */
29486be9f0dSKonstantin Belousov 		if (error1 == 0 && entry->end != entry->start) {
295*59e37c8aSRuslan Bukin 			IOMMU_LOCK(domain->iodom.iommu);
2961abfd355SKonstantin Belousov 			domain->refs++; /* XXXKIB prevent free */
2971abfd355SKonstantin Belousov 			domain->flags |= DMAR_DOMAIN_RMRR;
298*59e37c8aSRuslan Bukin 			IOMMU_UNLOCK(domain->iodom.iommu);
29986be9f0dSKonstantin Belousov 		} else {
30086be9f0dSKonstantin Belousov 			if (error1 != 0) {
301f9feb091SKonstantin Belousov 				if (dev != NULL)
302f9feb091SKonstantin Belousov 					device_printf(dev, "");
303f9feb091SKonstantin Belousov 				printf("pci%d:%d:%d ", bus, slot, func);
304f9feb091SKonstantin Belousov 				printf(
30586be9f0dSKonstantin Belousov 			    "dmar%d failed to map RMRR region (%jx, %jx) %d\n",
306*59e37c8aSRuslan Bukin 				    domain->iodom.iommu->unit, start, end,
307f9feb091SKonstantin Belousov 				    error1);
30886be9f0dSKonstantin Belousov 				error = error1;
30986be9f0dSKonstantin Belousov 			}
31086be9f0dSKonstantin Belousov 			TAILQ_REMOVE(&rmrr_entries, entry, unroll_link);
3111abfd355SKonstantin Belousov 			dmar_gas_free_entry(domain, entry);
31286be9f0dSKonstantin Belousov 		}
31386be9f0dSKonstantin Belousov 		for (i = 0; i < size; i++)
31486be9f0dSKonstantin Belousov 			vm_page_putfake(ma[i]);
31586be9f0dSKonstantin Belousov 		free(ma, M_TEMP);
31686be9f0dSKonstantin Belousov 	}
31786be9f0dSKonstantin Belousov 	return (error);
31886be9f0dSKonstantin Belousov }
31986be9f0dSKonstantin Belousov 
3201abfd355SKonstantin Belousov static struct dmar_domain *
3211abfd355SKonstantin Belousov dmar_domain_alloc(struct dmar_unit *dmar, bool id_mapped)
3221abfd355SKonstantin Belousov {
3231abfd355SKonstantin Belousov 	struct dmar_domain *domain;
3241abfd355SKonstantin Belousov 	int error, id, mgaw;
3251abfd355SKonstantin Belousov 
3261abfd355SKonstantin Belousov 	id = alloc_unr(dmar->domids);
3271abfd355SKonstantin Belousov 	if (id == -1)
3281abfd355SKonstantin Belousov 		return (NULL);
3291abfd355SKonstantin Belousov 	domain = malloc(sizeof(*domain), M_DMAR_DOMAIN, M_WAITOK | M_ZERO);
3301abfd355SKonstantin Belousov 	domain->domain = id;
3311abfd355SKonstantin Belousov 	LIST_INIT(&domain->contexts);
3321abfd355SKonstantin Belousov 	RB_INIT(&domain->rb_root);
333*59e37c8aSRuslan Bukin 	TAILQ_INIT(&domain->iodom.unload_entries);
334*59e37c8aSRuslan Bukin 	TASK_INIT(&domain->iodom.unload_task, 0, dmar_domain_unload_task,
335*59e37c8aSRuslan Bukin 	    domain);
336*59e37c8aSRuslan Bukin 	mtx_init(&domain->iodom.lock, "dmardom", NULL, MTX_DEF);
3371abfd355SKonstantin Belousov 	domain->dmar = dmar;
338*59e37c8aSRuslan Bukin 	domain->iodom.iommu = &dmar->iommu;
3391abfd355SKonstantin Belousov 
3401abfd355SKonstantin Belousov 	/*
3411abfd355SKonstantin Belousov 	 * For now, use the maximal usable physical address of the
3421abfd355SKonstantin Belousov 	 * installed memory to calculate the mgaw on id_mapped domain.
3431abfd355SKonstantin Belousov 	 * It is useful for the identity mapping, and less so for the
3441abfd355SKonstantin Belousov 	 * virtualized bus address space.
3451abfd355SKonstantin Belousov 	 */
3461abfd355SKonstantin Belousov 	domain->end = id_mapped ? ptoa(Maxmem) : BUS_SPACE_MAXADDR;
3471abfd355SKonstantin Belousov 	mgaw = dmar_maxaddr2mgaw(dmar, domain->end, !id_mapped);
3481abfd355SKonstantin Belousov 	error = domain_set_agaw(domain, mgaw);
3491abfd355SKonstantin Belousov 	if (error != 0)
3501abfd355SKonstantin Belousov 		goto fail;
3511abfd355SKonstantin Belousov 	if (!id_mapped)
3521abfd355SKonstantin Belousov 		/* Use all supported address space for remapping. */
3531abfd355SKonstantin Belousov 		domain->end = 1ULL << (domain->agaw - 1);
3541abfd355SKonstantin Belousov 
3551abfd355SKonstantin Belousov 	dmar_gas_init_domain(domain);
3561abfd355SKonstantin Belousov 
3571abfd355SKonstantin Belousov 	if (id_mapped) {
3581abfd355SKonstantin Belousov 		if ((dmar->hw_ecap & DMAR_ECAP_PT) == 0) {
3591abfd355SKonstantin Belousov 			domain->pgtbl_obj = domain_get_idmap_pgtbl(domain,
3601abfd355SKonstantin Belousov 			    domain->end);
3611abfd355SKonstantin Belousov 		}
3621abfd355SKonstantin Belousov 		domain->flags |= DMAR_DOMAIN_IDMAP;
3631abfd355SKonstantin Belousov 	} else {
3641abfd355SKonstantin Belousov 		error = domain_alloc_pgtbl(domain);
3651abfd355SKonstantin Belousov 		if (error != 0)
3661abfd355SKonstantin Belousov 			goto fail;
3671abfd355SKonstantin Belousov 		/* Disable local apic region access */
3681abfd355SKonstantin Belousov 		error = dmar_gas_reserve_region(domain, 0xfee00000,
3691abfd355SKonstantin Belousov 		    0xfeefffff + 1);
3701abfd355SKonstantin Belousov 		if (error != 0)
3711abfd355SKonstantin Belousov 			goto fail;
3721abfd355SKonstantin Belousov 	}
3731abfd355SKonstantin Belousov 	return (domain);
3741abfd355SKonstantin Belousov 
3751abfd355SKonstantin Belousov fail:
3761abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
3771abfd355SKonstantin Belousov 	return (NULL);
3781abfd355SKonstantin Belousov }
3791abfd355SKonstantin Belousov 
38086be9f0dSKonstantin Belousov static struct dmar_ctx *
3811abfd355SKonstantin Belousov dmar_ctx_alloc(struct dmar_domain *domain, uint16_t rid)
38286be9f0dSKonstantin Belousov {
38386be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
38486be9f0dSKonstantin Belousov 
38586be9f0dSKonstantin Belousov 	ctx = malloc(sizeof(*ctx), M_DMAR_CTX, M_WAITOK | M_ZERO);
386*59e37c8aSRuslan Bukin 	ctx->context.domain = (struct iommu_domain *)domain;
387*59e37c8aSRuslan Bukin 	ctx->context.tag = malloc(sizeof(struct bus_dma_tag_iommu),
388*59e37c8aSRuslan Bukin 	    M_DMAR_CTX, M_WAITOK | M_ZERO);
38967499354SRyan Stone 	ctx->rid = rid;
3901abfd355SKonstantin Belousov 	ctx->refs = 1;
39186be9f0dSKonstantin Belousov 	return (ctx);
39286be9f0dSKonstantin Belousov }
39386be9f0dSKonstantin Belousov 
39486be9f0dSKonstantin Belousov static void
3951abfd355SKonstantin Belousov dmar_ctx_link(struct dmar_ctx *ctx)
3961abfd355SKonstantin Belousov {
3971abfd355SKonstantin Belousov 	struct dmar_domain *domain;
3981abfd355SKonstantin Belousov 
399*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)ctx->context.domain;
400*59e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4011abfd355SKonstantin Belousov 	KASSERT(domain->refs >= domain->ctx_cnt,
4021abfd355SKonstantin Belousov 	    ("dom %p ref underflow %d %d", domain, domain->refs,
4031abfd355SKonstantin Belousov 	    domain->ctx_cnt));
4041abfd355SKonstantin Belousov 	domain->refs++;
4051abfd355SKonstantin Belousov 	domain->ctx_cnt++;
4061abfd355SKonstantin Belousov 	LIST_INSERT_HEAD(&domain->contexts, ctx, link);
4071abfd355SKonstantin Belousov }
4081abfd355SKonstantin Belousov 
4091abfd355SKonstantin Belousov static void
4101abfd355SKonstantin Belousov dmar_ctx_unlink(struct dmar_ctx *ctx)
4111abfd355SKonstantin Belousov {
4121abfd355SKonstantin Belousov 	struct dmar_domain *domain;
4131abfd355SKonstantin Belousov 
414*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)ctx->context.domain;
415*59e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4161abfd355SKonstantin Belousov 	KASSERT(domain->refs > 0,
4171abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d", domain, domain->refs));
4181abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt >= domain->refs,
4191abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d ctx_cnt %d", domain,
4201abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
4211abfd355SKonstantin Belousov 	domain->refs--;
4221abfd355SKonstantin Belousov 	domain->ctx_cnt--;
4231abfd355SKonstantin Belousov 	LIST_REMOVE(ctx, link);
4241abfd355SKonstantin Belousov }
4251abfd355SKonstantin Belousov 
4261abfd355SKonstantin Belousov static void
4271abfd355SKonstantin Belousov dmar_domain_destroy(struct dmar_domain *domain)
42886be9f0dSKonstantin Belousov {
429*59e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
43086be9f0dSKonstantin Belousov 
431*59e37c8aSRuslan Bukin 	KASSERT(TAILQ_EMPTY(&domain->iodom.unload_entries),
4321abfd355SKonstantin Belousov 	    ("unfinished unloads %p", domain));
4331abfd355SKonstantin Belousov 	KASSERT(LIST_EMPTY(&domain->contexts),
4341abfd355SKonstantin Belousov 	    ("destroying dom %p with contexts", domain));
4351abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt == 0,
4361abfd355SKonstantin Belousov 	    ("destroying dom %p with ctx_cnt %d", domain, domain->ctx_cnt));
4371abfd355SKonstantin Belousov 	KASSERT(domain->refs == 0,
4381abfd355SKonstantin Belousov 	    ("destroying dom %p with refs %d", domain, domain->refs));
4391abfd355SKonstantin Belousov 	if ((domain->flags & DMAR_DOMAIN_GAS_INITED) != 0) {
4401abfd355SKonstantin Belousov 		DMAR_DOMAIN_LOCK(domain);
4411abfd355SKonstantin Belousov 		dmar_gas_fini_domain(domain);
4421abfd355SKonstantin Belousov 		DMAR_DOMAIN_UNLOCK(domain);
44386be9f0dSKonstantin Belousov 	}
4441abfd355SKonstantin Belousov 	if ((domain->flags & DMAR_DOMAIN_PGTBL_INITED) != 0) {
4451abfd355SKonstantin Belousov 		if (domain->pgtbl_obj != NULL)
4461abfd355SKonstantin Belousov 			DMAR_DOMAIN_PGLOCK(domain);
4471abfd355SKonstantin Belousov 		domain_free_pgtbl(domain);
44886be9f0dSKonstantin Belousov 	}
449*59e37c8aSRuslan Bukin 	mtx_destroy(&domain->iodom.lock);
450*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)domain->iodom.iommu;
451*59e37c8aSRuslan Bukin 	free_unr(dmar->domids, domain->domain);
4521abfd355SKonstantin Belousov 	free(domain, M_DMAR_DOMAIN);
45386be9f0dSKonstantin Belousov }
45486be9f0dSKonstantin Belousov 
455f9feb091SKonstantin Belousov static struct dmar_ctx *
456f9feb091SKonstantin Belousov dmar_get_ctx_for_dev1(struct dmar_unit *dmar, device_t dev, uint16_t rid,
457f9feb091SKonstantin Belousov     int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
4581abfd355SKonstantin Belousov     bool id_mapped, bool rmrr_init)
45986be9f0dSKonstantin Belousov {
4601abfd355SKonstantin Belousov 	struct dmar_domain *domain, *domain1;
46186be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx, *ctx1;
46286be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
46386be9f0dSKonstantin Belousov 	struct sf_buf *sf;
4641abfd355SKonstantin Belousov 	int bus, slot, func, error;
46586be9f0dSKonstantin Belousov 	bool enable;
46686be9f0dSKonstantin Belousov 
467f9feb091SKonstantin Belousov 	if (dev != NULL) {
46867499354SRyan Stone 		bus = pci_get_bus(dev);
46967499354SRyan Stone 		slot = pci_get_slot(dev);
47067499354SRyan Stone 		func = pci_get_function(dev);
471f9feb091SKonstantin Belousov 	} else {
472f9feb091SKonstantin Belousov 		bus = PCI_RID2BUS(rid);
473f9feb091SKonstantin Belousov 		slot = PCI_RID2SLOT(rid);
474f9feb091SKonstantin Belousov 		func = PCI_RID2FUNC(rid);
475f9feb091SKonstantin Belousov 	}
47686be9f0dSKonstantin Belousov 	enable = false;
47786be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
47886be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
479685666aaSKonstantin Belousov 	KASSERT(!dmar_is_buswide_ctx(dmar, bus) || (slot == 0 && func == 0),
480*59e37c8aSRuslan Bukin 	    ("dmar%d pci%d:%d:%d get_ctx for buswide", dmar->iommu.unit, bus,
481685666aaSKonstantin Belousov 	    slot, func));
48267499354SRyan Stone 	ctx = dmar_find_ctx_locked(dmar, rid);
48386be9f0dSKonstantin Belousov 	error = 0;
48486be9f0dSKonstantin Belousov 	if (ctx == NULL) {
48586be9f0dSKonstantin Belousov 		/*
48686be9f0dSKonstantin Belousov 		 * Perform the allocations which require sleep or have
48786be9f0dSKonstantin Belousov 		 * higher chance to succeed if the sleep is allowed.
48886be9f0dSKonstantin Belousov 		 */
48986be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
490b29d186cSKonstantin Belousov 		dmar_ensure_ctx_page(dmar, PCI_RID2BUS(rid));
4911abfd355SKonstantin Belousov 		domain1 = dmar_domain_alloc(dmar, id_mapped);
4921abfd355SKonstantin Belousov 		if (domain1 == NULL) {
49386be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
49486be9f0dSKonstantin Belousov 			return (NULL);
49586be9f0dSKonstantin Belousov 		}
4965f8e5c7fSKonstantin Belousov 		if (!id_mapped) {
497f9feb091SKonstantin Belousov 			error = domain_init_rmrr(domain1, dev, bus,
498f9feb091SKonstantin Belousov 			    slot, func, dev_domain, dev_busno, dev_path,
499f9feb091SKonstantin Belousov 			    dev_path_len);
50086be9f0dSKonstantin Belousov 			if (error != 0) {
5011abfd355SKonstantin Belousov 				dmar_domain_destroy(domain1);
50286be9f0dSKonstantin Belousov 				TD_PINNED_ASSERT;
50386be9f0dSKonstantin Belousov 				return (NULL);
50486be9f0dSKonstantin Belousov 			}
5055f8e5c7fSKonstantin Belousov 		}
5061abfd355SKonstantin Belousov 		ctx1 = dmar_ctx_alloc(domain1, rid);
50786be9f0dSKonstantin Belousov 		ctxp = dmar_map_ctx_entry(ctx1, &sf);
50886be9f0dSKonstantin Belousov 		DMAR_LOCK(dmar);
50986be9f0dSKonstantin Belousov 
51086be9f0dSKonstantin Belousov 		/*
51186be9f0dSKonstantin Belousov 		 * Recheck the contexts, other thread might have
51286be9f0dSKonstantin Belousov 		 * already allocated needed one.
51386be9f0dSKonstantin Belousov 		 */
51467499354SRyan Stone 		ctx = dmar_find_ctx_locked(dmar, rid);
51586be9f0dSKonstantin Belousov 		if (ctx == NULL) {
5161abfd355SKonstantin Belousov 			domain = domain1;
51786be9f0dSKonstantin Belousov 			ctx = ctx1;
5181abfd355SKonstantin Belousov 			dmar_ctx_link(ctx);
519*59e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
520*59e37c8aSRuslan Bukin 			device_tag_init(ctx, dev);
52186be9f0dSKonstantin Belousov 
52286be9f0dSKonstantin Belousov 			/*
52386be9f0dSKonstantin Belousov 			 * This is the first activated context for the
52486be9f0dSKonstantin Belousov 			 * DMAR unit.  Enable the translation after
52586be9f0dSKonstantin Belousov 			 * everything is set up.
52686be9f0dSKonstantin Belousov 			 */
5271abfd355SKonstantin Belousov 			if (LIST_EMPTY(&dmar->domains))
52886be9f0dSKonstantin Belousov 				enable = true;
5291abfd355SKonstantin Belousov 			LIST_INSERT_HEAD(&dmar->domains, domain, link);
530685666aaSKonstantin Belousov 			ctx_id_entry_init(ctx, ctxp, false, bus);
531f9feb091SKonstantin Belousov 			if (dev != NULL) {
53286be9f0dSKonstantin Belousov 				device_printf(dev,
53334e8337bSKonstantin Belousov 			    "dmar%d pci%d:%d:%d:%d rid %x domain %d mgaw %d "
5349d0bc6d8SKonstantin Belousov 				    "agaw %d %s-mapped\n",
535*59e37c8aSRuslan Bukin 				    dmar->iommu.unit, dmar->segment, bus, slot,
5361abfd355SKonstantin Belousov 				    func, rid, domain->domain, domain->mgaw,
5371abfd355SKonstantin Belousov 				    domain->agaw, id_mapped ? "id" : "re");
538f9feb091SKonstantin Belousov 			}
5393d47c58bSKonstantin Belousov 			dmar_unmap_pgtbl(sf);
54086be9f0dSKonstantin Belousov 		} else {
5413d47c58bSKonstantin Belousov 			dmar_unmap_pgtbl(sf);
5421abfd355SKonstantin Belousov 			dmar_domain_destroy(domain1);
5433d47c58bSKonstantin Belousov 			/* Nothing needs to be done to destroy ctx1. */
5443d47c58bSKonstantin Belousov 			free(ctx1, M_DMAR_CTX);
545*59e37c8aSRuslan Bukin 			domain = (struct dmar_domain *)ctx->context.domain;
5461abfd355SKonstantin Belousov 			ctx->refs++; /* tag referenced us */
54786be9f0dSKonstantin Belousov 		}
54868eeb96aSKonstantin Belousov 	} else {
549*59e37c8aSRuslan Bukin 		domain = (struct dmar_domain *)ctx->context.domain;
550*59e37c8aSRuslan Bukin 		if (ctx->context.tag->owner == NULL)
551*59e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
5521abfd355SKonstantin Belousov 		ctx->refs++; /* tag referenced us */
5531abfd355SKonstantin Belousov 	}
5541abfd355SKonstantin Belousov 
5551abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, enable);
55686be9f0dSKonstantin Belousov 	if (error != 0) {
55786be9f0dSKonstantin Belousov 		dmar_free_ctx_locked(dmar, ctx);
55886be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
55986be9f0dSKonstantin Belousov 		return (NULL);
56086be9f0dSKonstantin Belousov 	}
56168eeb96aSKonstantin Belousov 
56268eeb96aSKonstantin Belousov 	/*
56368eeb96aSKonstantin Belousov 	 * The dmar lock was potentially dropped between check for the
56468eeb96aSKonstantin Belousov 	 * empty context list and now.  Recheck the state of GCMD_TE
56568eeb96aSKonstantin Belousov 	 * to avoid unneeded command.
56668eeb96aSKonstantin Belousov 	 */
56768eeb96aSKonstantin Belousov 	if (enable && !rmrr_init && (dmar->hw_gcmd & DMAR_GCMD_TE) == 0) {
56886be9f0dSKonstantin Belousov 		error = dmar_enable_translation(dmar);
569f9feb091SKonstantin Belousov 		if (error == 0) {
570f9feb091SKonstantin Belousov 			if (bootverbose) {
571f9feb091SKonstantin Belousov 				printf("dmar%d: enabled translation\n",
572*59e37c8aSRuslan Bukin 				    dmar->iommu.unit);
573f9feb091SKonstantin Belousov 			}
574f9feb091SKonstantin Belousov 		} else {
575f9feb091SKonstantin Belousov 			printf("dmar%d: enabling translation failed, "
576*59e37c8aSRuslan Bukin 			    "error %d\n", dmar->iommu.unit, error);
57786be9f0dSKonstantin Belousov 			dmar_free_ctx_locked(dmar, ctx);
57886be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
57986be9f0dSKonstantin Belousov 			return (NULL);
58086be9f0dSKonstantin Belousov 		}
58186be9f0dSKonstantin Belousov 	}
58286be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
58386be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
58486be9f0dSKonstantin Belousov 	return (ctx);
58586be9f0dSKonstantin Belousov }
58686be9f0dSKonstantin Belousov 
587f9feb091SKonstantin Belousov struct dmar_ctx *
588f9feb091SKonstantin Belousov dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev, uint16_t rid,
589f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
590f9feb091SKonstantin Belousov {
591f9feb091SKonstantin Belousov 	int dev_domain, dev_path_len, dev_busno;
592f9feb091SKonstantin Belousov 
593f9feb091SKonstantin Belousov 	dev_domain = pci_get_domain(dev);
594f9feb091SKonstantin Belousov 	dev_path_len = dmar_dev_depth(dev);
595f9feb091SKonstantin Belousov 	ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
596f9feb091SKonstantin Belousov 	dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
597f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, dev, rid, dev_domain, dev_busno,
598f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
599f9feb091SKonstantin Belousov }
600f9feb091SKonstantin Belousov 
601f9feb091SKonstantin Belousov struct dmar_ctx *
602f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
603f9feb091SKonstantin Belousov     int dev_domain, int dev_busno,
604f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len,
605f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
606f9feb091SKonstantin Belousov {
607f9feb091SKonstantin Belousov 
608f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, NULL, rid, dev_domain, dev_busno,
609f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
610f9feb091SKonstantin Belousov }
611f9feb091SKonstantin Belousov 
6121abfd355SKonstantin Belousov int
6131abfd355SKonstantin Belousov dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx)
6141abfd355SKonstantin Belousov {
6151abfd355SKonstantin Belousov 	struct dmar_unit *dmar;
6161abfd355SKonstantin Belousov 	struct dmar_domain *old_domain;
6171abfd355SKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
6181abfd355SKonstantin Belousov 	struct sf_buf *sf;
6191abfd355SKonstantin Belousov 	int error;
6201abfd355SKonstantin Belousov 
6211abfd355SKonstantin Belousov 	dmar = domain->dmar;
622*59e37c8aSRuslan Bukin 	old_domain = (struct dmar_domain *)ctx->context.domain;
6231abfd355SKonstantin Belousov 	if (domain == old_domain)
6241abfd355SKonstantin Belousov 		return (0);
625*59e37c8aSRuslan Bukin 	KASSERT(old_domain->iodom.iommu == domain->iodom.iommu,
6261abfd355SKonstantin Belousov 	    ("domain %p %u moving between dmars %u %u", domain,
627*59e37c8aSRuslan Bukin 	    domain->domain, old_domain->iodom.iommu->unit,
628*59e37c8aSRuslan Bukin 	    domain->iodom.iommu->unit));
6291abfd355SKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
6301abfd355SKonstantin Belousov 
6311abfd355SKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
6321abfd355SKonstantin Belousov 	DMAR_LOCK(dmar);
6331abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
634*59e37c8aSRuslan Bukin 	ctx->context.domain = &domain->iodom;
6351abfd355SKonstantin Belousov 	dmar_ctx_link(ctx);
636685666aaSKonstantin Belousov 	ctx_id_entry_init(ctx, ctxp, true, PCI_BUSMAX + 100);
6371abfd355SKonstantin Belousov 	dmar_unmap_pgtbl(sf);
6381abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, true);
6391abfd355SKonstantin Belousov 	/* If flush failed, rolling back would not work as well. */
6401abfd355SKonstantin Belousov 	printf("dmar%d rid %x domain %d->%d %s-mapped\n",
641*59e37c8aSRuslan Bukin 	    dmar->iommu.unit, ctx->rid, old_domain->domain, domain->domain,
6421abfd355SKonstantin Belousov 	    (domain->flags & DMAR_DOMAIN_IDMAP) != 0 ? "id" : "re");
6431abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, old_domain);
6441abfd355SKonstantin Belousov 	TD_PINNED_ASSERT;
6451abfd355SKonstantin Belousov 	return (error);
6461abfd355SKonstantin Belousov }
6471abfd355SKonstantin Belousov 
6481abfd355SKonstantin Belousov static void
6491abfd355SKonstantin Belousov dmar_unref_domain_locked(struct dmar_unit *dmar, struct dmar_domain *domain)
6501abfd355SKonstantin Belousov {
6511abfd355SKonstantin Belousov 
6521abfd355SKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
6531abfd355SKonstantin Belousov 	KASSERT(domain->refs >= 1,
654*59e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %u", dmar->iommu.unit, domain,
655*59e37c8aSRuslan Bukin 	    domain->refs));
6561abfd355SKonstantin Belousov 	KASSERT(domain->refs > domain->ctx_cnt,
657*59e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %d ctx_cnt %d", dmar->iommu.unit, domain,
6581abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
6591abfd355SKonstantin Belousov 
6601abfd355SKonstantin Belousov 	if (domain->refs > 1) {
6611abfd355SKonstantin Belousov 		domain->refs--;
6621abfd355SKonstantin Belousov 		DMAR_UNLOCK(dmar);
6631abfd355SKonstantin Belousov 		return;
6641abfd355SKonstantin Belousov 	}
6651abfd355SKonstantin Belousov 
6661abfd355SKonstantin Belousov 	KASSERT((domain->flags & DMAR_DOMAIN_RMRR) == 0,
6671abfd355SKonstantin Belousov 	    ("lost ref on RMRR domain %p", domain));
6681abfd355SKonstantin Belousov 
6691abfd355SKonstantin Belousov 	LIST_REMOVE(domain, link);
6701abfd355SKonstantin Belousov 	DMAR_UNLOCK(dmar);
6711abfd355SKonstantin Belousov 
672*59e37c8aSRuslan Bukin 	taskqueue_drain(dmar->iommu.delayed_taskqueue,
673*59e37c8aSRuslan Bukin 	    &domain->iodom.unload_task);
6741abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
6751abfd355SKonstantin Belousov }
6761abfd355SKonstantin Belousov 
67786be9f0dSKonstantin Belousov void
67886be9f0dSKonstantin Belousov dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx)
67986be9f0dSKonstantin Belousov {
68086be9f0dSKonstantin Belousov 	struct sf_buf *sf;
68186be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
6821abfd355SKonstantin Belousov 	struct dmar_domain *domain;
68386be9f0dSKonstantin Belousov 
68486be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
68586be9f0dSKonstantin Belousov 	KASSERT(ctx->refs >= 1,
68686be9f0dSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs));
68786be9f0dSKonstantin Belousov 
68886be9f0dSKonstantin Belousov 	/*
68986be9f0dSKonstantin Belousov 	 * If our reference is not last, only the dereference should
69086be9f0dSKonstantin Belousov 	 * be performed.
69186be9f0dSKonstantin Belousov 	 */
69286be9f0dSKonstantin Belousov 	if (ctx->refs > 1) {
69386be9f0dSKonstantin Belousov 		ctx->refs--;
69486be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
69586be9f0dSKonstantin Belousov 		return;
69686be9f0dSKonstantin Belousov 	}
69786be9f0dSKonstantin Belousov 
698*59e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
69986be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
70086be9f0dSKonstantin Belousov 
70186be9f0dSKonstantin Belousov 	/*
70286be9f0dSKonstantin Belousov 	 * Otherwise, the context entry must be cleared before the
70386be9f0dSKonstantin Belousov 	 * page table is destroyed.  The mapping of the context
70486be9f0dSKonstantin Belousov 	 * entries page could require sleep, unlock the dmar.
70586be9f0dSKonstantin Belousov 	 */
70686be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
70786be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
70886be9f0dSKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
70986be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
71086be9f0dSKonstantin Belousov 	KASSERT(ctx->refs >= 1,
71186be9f0dSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs));
71286be9f0dSKonstantin Belousov 
71386be9f0dSKonstantin Belousov 	/*
71486be9f0dSKonstantin Belousov 	 * Other thread might have referenced the context, in which
71586be9f0dSKonstantin Belousov 	 * case again only the dereference should be performed.
71686be9f0dSKonstantin Belousov 	 */
71786be9f0dSKonstantin Belousov 	if (ctx->refs > 1) {
71886be9f0dSKonstantin Belousov 		ctx->refs--;
71986be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
7206b7c46afSKonstantin Belousov 		dmar_unmap_pgtbl(sf);
72186be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
72286be9f0dSKonstantin Belousov 		return;
72386be9f0dSKonstantin Belousov 	}
72486be9f0dSKonstantin Belousov 
725*59e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
72686be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
72786be9f0dSKonstantin Belousov 
72886be9f0dSKonstantin Belousov 	/*
72986be9f0dSKonstantin Belousov 	 * Clear the context pointer and flush the caches.
73086be9f0dSKonstantin Belousov 	 * XXXKIB: cannot do this if any RMRR entries are still present.
73186be9f0dSKonstantin Belousov 	 */
73286be9f0dSKonstantin Belousov 	dmar_pte_clear(&ctxp->ctx1);
73386be9f0dSKonstantin Belousov 	ctxp->ctx2 = 0;
7346b7c46afSKonstantin Belousov 	dmar_flush_ctx_to_ram(dmar, ctxp);
73586be9f0dSKonstantin Belousov 	dmar_inv_ctx_glob(dmar);
73668eeb96aSKonstantin Belousov 	if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) {
73768eeb96aSKonstantin Belousov 		if (dmar->qi_enabled)
73868eeb96aSKonstantin Belousov 			dmar_qi_invalidate_iotlb_glob_locked(dmar);
73968eeb96aSKonstantin Belousov 		else
74086be9f0dSKonstantin Belousov 			dmar_inv_iotlb_glob(dmar);
74168eeb96aSKonstantin Belousov 	}
7426b7c46afSKonstantin Belousov 	dmar_unmap_pgtbl(sf);
743*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)ctx->context.domain;
7441abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
745*59e37c8aSRuslan Bukin 	free(ctx->context.tag, M_DMAR_CTX);
7461abfd355SKonstantin Belousov 	free(ctx, M_DMAR_CTX);
7471abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, domain);
74886be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
74986be9f0dSKonstantin Belousov }
75086be9f0dSKonstantin Belousov 
75186be9f0dSKonstantin Belousov void
75286be9f0dSKonstantin Belousov dmar_free_ctx(struct dmar_ctx *ctx)
75386be9f0dSKonstantin Belousov {
75486be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
75586be9f0dSKonstantin Belousov 
756*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)ctx->context.domain->iommu;
75786be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
75886be9f0dSKonstantin Belousov 	dmar_free_ctx_locked(dmar, ctx);
75986be9f0dSKonstantin Belousov }
76086be9f0dSKonstantin Belousov 
7611abfd355SKonstantin Belousov /*
7621abfd355SKonstantin Belousov  * Returns with the domain locked.
7631abfd355SKonstantin Belousov  */
76486be9f0dSKonstantin Belousov struct dmar_ctx *
76567499354SRyan Stone dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid)
76686be9f0dSKonstantin Belousov {
7671abfd355SKonstantin Belousov 	struct dmar_domain *domain;
76886be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
76986be9f0dSKonstantin Belousov 
77086be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
77186be9f0dSKonstantin Belousov 
7721abfd355SKonstantin Belousov 	LIST_FOREACH(domain, &dmar->domains, link) {
7731abfd355SKonstantin Belousov 		LIST_FOREACH(ctx, &domain->contexts, link) {
77467499354SRyan Stone 			if (ctx->rid == rid)
77586be9f0dSKonstantin Belousov 				return (ctx);
77686be9f0dSKonstantin Belousov 		}
7771abfd355SKonstantin Belousov 	}
77886be9f0dSKonstantin Belousov 	return (NULL);
77986be9f0dSKonstantin Belousov }
78086be9f0dSKonstantin Belousov 
78186be9f0dSKonstantin Belousov void
782*59e37c8aSRuslan Bukin dmar_domain_free_entry(struct iommu_map_entry *entry, bool free)
78368eeb96aSKonstantin Belousov {
7841abfd355SKonstantin Belousov 	struct dmar_domain *domain;
78568eeb96aSKonstantin Belousov 
786*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)entry->domain;
7871abfd355SKonstantin Belousov 	DMAR_DOMAIN_LOCK(domain);
788*59e37c8aSRuslan Bukin 	if ((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
7891abfd355SKonstantin Belousov 		dmar_gas_free_region(domain, entry);
79068eeb96aSKonstantin Belousov 	else
7911abfd355SKonstantin Belousov 		dmar_gas_free_space(domain, entry);
7921abfd355SKonstantin Belousov 	DMAR_DOMAIN_UNLOCK(domain);
79368eeb96aSKonstantin Belousov 	if (free)
7941abfd355SKonstantin Belousov 		dmar_gas_free_entry(domain, entry);
79568eeb96aSKonstantin Belousov 	else
79668eeb96aSKonstantin Belousov 		entry->flags = 0;
79768eeb96aSKonstantin Belousov }
79868eeb96aSKonstantin Belousov 
79968eeb96aSKonstantin Belousov void
800*59e37c8aSRuslan Bukin dmar_domain_unload_entry(struct iommu_map_entry *entry, bool free)
80168eeb96aSKonstantin Belousov {
802*59e37c8aSRuslan Bukin 	struct dmar_domain *domain;
80368eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
80468eeb96aSKonstantin Belousov 
805*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)entry->domain;
806*59e37c8aSRuslan Bukin 	unit = (struct dmar_unit *)domain->iodom.iommu;
80768eeb96aSKonstantin Belousov 	if (unit->qi_enabled) {
80868eeb96aSKonstantin Belousov 		DMAR_LOCK(unit);
809*59e37c8aSRuslan Bukin 		dmar_qi_invalidate_locked((struct dmar_domain *)entry->domain,
810*59e37c8aSRuslan Bukin 		    entry->start, entry->end - entry->start, &entry->gseq,
811*59e37c8aSRuslan Bukin 		    true);
81268eeb96aSKonstantin Belousov 		if (!free)
813*59e37c8aSRuslan Bukin 			entry->flags |= IOMMU_MAP_ENTRY_QI_NF;
81468eeb96aSKonstantin Belousov 		TAILQ_INSERT_TAIL(&unit->tlb_flush_entries, entry, dmamap_link);
81568eeb96aSKonstantin Belousov 		DMAR_UNLOCK(unit);
81668eeb96aSKonstantin Belousov 	} else {
817*59e37c8aSRuslan Bukin 		domain_flush_iotlb_sync((struct dmar_domain *)entry->domain,
818*59e37c8aSRuslan Bukin 		    entry->start, entry->end - entry->start);
8191abfd355SKonstantin Belousov 		dmar_domain_free_entry(entry, free);
82068eeb96aSKonstantin Belousov 	}
82168eeb96aSKonstantin Belousov }
82268eeb96aSKonstantin Belousov 
823cf619a92SKonstantin Belousov static bool
824cf619a92SKonstantin Belousov dmar_domain_unload_emit_wait(struct dmar_domain *domain,
825*59e37c8aSRuslan Bukin     struct iommu_map_entry *entry)
826e164cafcSKonstantin Belousov {
827e164cafcSKonstantin Belousov 
828cf619a92SKonstantin Belousov 	if (TAILQ_NEXT(entry, dmamap_link) == NULL)
829cf619a92SKonstantin Belousov 		return (true);
830cf619a92SKonstantin Belousov 	return (domain->batch_no++ % dmar_batch_coalesce == 0);
831e164cafcSKonstantin Belousov }
832e164cafcSKonstantin Belousov 
83368eeb96aSKonstantin Belousov void
8341abfd355SKonstantin Belousov dmar_domain_unload(struct dmar_domain *domain,
835*59e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *entries, bool cansleep)
83686be9f0dSKonstantin Belousov {
83768eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
838*59e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
83986be9f0dSKonstantin Belousov 	int error;
84086be9f0dSKonstantin Belousov 
841*59e37c8aSRuslan Bukin 	unit = (struct dmar_unit *)domain->iodom.iommu;
84268eeb96aSKonstantin Belousov 
84368eeb96aSKonstantin Belousov 	TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) {
844*59e37c8aSRuslan Bukin 		KASSERT((entry->flags & IOMMU_MAP_ENTRY_MAP) != 0,
8451abfd355SKonstantin Belousov 		    ("not mapped entry %p %p", domain, entry));
8461abfd355SKonstantin Belousov 		error = domain_unmap_buf(domain, entry->start, entry->end -
84786be9f0dSKonstantin Belousov 		    entry->start, cansleep ? DMAR_PGF_WAITOK : 0);
8481abfd355SKonstantin Belousov 		KASSERT(error == 0, ("unmap %p error %d", domain, error));
84968eeb96aSKonstantin Belousov 		if (!unit->qi_enabled) {
8501abfd355SKonstantin Belousov 			domain_flush_iotlb_sync(domain, entry->start,
85168eeb96aSKonstantin Belousov 			    entry->end - entry->start);
85268eeb96aSKonstantin Belousov 			TAILQ_REMOVE(entries, entry, dmamap_link);
8531abfd355SKonstantin Belousov 			dmar_domain_free_entry(entry, true);
85486be9f0dSKonstantin Belousov 		}
85586be9f0dSKonstantin Belousov 	}
85668eeb96aSKonstantin Belousov 	if (TAILQ_EMPTY(entries))
85768eeb96aSKonstantin Belousov 		return;
85868eeb96aSKonstantin Belousov 
85968eeb96aSKonstantin Belousov 	KASSERT(unit->qi_enabled, ("loaded entry left"));
86068eeb96aSKonstantin Belousov 	DMAR_LOCK(unit);
86168eeb96aSKonstantin Belousov 	TAILQ_FOREACH(entry, entries, dmamap_link) {
8621abfd355SKonstantin Belousov 		dmar_qi_invalidate_locked(domain, entry->start, entry->end -
863cf619a92SKonstantin Belousov 		    entry->start, &entry->gseq,
864cf619a92SKonstantin Belousov 		    dmar_domain_unload_emit_wait(domain, entry));
86568eeb96aSKonstantin Belousov 	}
866cf619a92SKonstantin Belousov 	TAILQ_CONCAT(&unit->tlb_flush_entries, entries, dmamap_link);
86768eeb96aSKonstantin Belousov 	DMAR_UNLOCK(unit);
86868eeb96aSKonstantin Belousov }
86986be9f0dSKonstantin Belousov 
87086be9f0dSKonstantin Belousov static void
8711abfd355SKonstantin Belousov dmar_domain_unload_task(void *arg, int pending)
87286be9f0dSKonstantin Belousov {
8731abfd355SKonstantin Belousov 	struct dmar_domain *domain;
874*59e37c8aSRuslan Bukin 	struct iommu_map_entries_tailq entries;
87586be9f0dSKonstantin Belousov 
8761abfd355SKonstantin Belousov 	domain = arg;
87786be9f0dSKonstantin Belousov 	TAILQ_INIT(&entries);
87886be9f0dSKonstantin Belousov 
87986be9f0dSKonstantin Belousov 	for (;;) {
8801abfd355SKonstantin Belousov 		DMAR_DOMAIN_LOCK(domain);
881*59e37c8aSRuslan Bukin 		TAILQ_SWAP(&domain->iodom.unload_entries, &entries,
882*59e37c8aSRuslan Bukin 		    iommu_map_entry, dmamap_link);
8831abfd355SKonstantin Belousov 		DMAR_DOMAIN_UNLOCK(domain);
88486be9f0dSKonstantin Belousov 		if (TAILQ_EMPTY(&entries))
88586be9f0dSKonstantin Belousov 			break;
8861abfd355SKonstantin Belousov 		dmar_domain_unload(domain, &entries, true);
88786be9f0dSKonstantin Belousov 	}
88886be9f0dSKonstantin Belousov }
889*59e37c8aSRuslan Bukin 
890*59e37c8aSRuslan Bukin struct iommu_ctx *
891*59e37c8aSRuslan Bukin iommu_get_ctx(struct iommu_unit *iommu, device_t dev, uint16_t rid,
892*59e37c8aSRuslan Bukin     bool id_mapped, bool rmrr_init)
893*59e37c8aSRuslan Bukin {
894*59e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
895*59e37c8aSRuslan Bukin 	struct dmar_ctx *ret;
896*59e37c8aSRuslan Bukin 
897*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)iommu;
898*59e37c8aSRuslan Bukin 
899*59e37c8aSRuslan Bukin 	ret = dmar_get_ctx_for_dev(dmar, dev, rid, id_mapped, rmrr_init);
900*59e37c8aSRuslan Bukin 
901*59e37c8aSRuslan Bukin 	return ((struct iommu_ctx *)ret);
902*59e37c8aSRuslan Bukin }
903*59e37c8aSRuslan Bukin 
904*59e37c8aSRuslan Bukin void
905*59e37c8aSRuslan Bukin iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *context)
906*59e37c8aSRuslan Bukin {
907*59e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
908*59e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
909*59e37c8aSRuslan Bukin 
910*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)iommu;
911*59e37c8aSRuslan Bukin 	ctx = (struct dmar_ctx *)context;
912*59e37c8aSRuslan Bukin 
913*59e37c8aSRuslan Bukin 	dmar_free_ctx_locked(dmar, ctx);
914*59e37c8aSRuslan Bukin }
915*59e37c8aSRuslan Bukin 
916*59e37c8aSRuslan Bukin void
917*59e37c8aSRuslan Bukin iommu_free_ctx(struct iommu_ctx *context)
918*59e37c8aSRuslan Bukin {
919*59e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
920*59e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
921*59e37c8aSRuslan Bukin 
922*59e37c8aSRuslan Bukin 	ctx = (struct dmar_ctx *)context;
923*59e37c8aSRuslan Bukin 	dmar = (struct dmar_unit *)ctx->context.domain->iommu;
924*59e37c8aSRuslan Bukin 
925*59e37c8aSRuslan Bukin 	dmar_free_ctx(ctx);
926*59e37c8aSRuslan Bukin }
927*59e37c8aSRuslan Bukin 
928*59e37c8aSRuslan Bukin void
929*59e37c8aSRuslan Bukin iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free)
930*59e37c8aSRuslan Bukin {
931*59e37c8aSRuslan Bukin 
932*59e37c8aSRuslan Bukin 	dmar_domain_unload_entry(entry, free);
933*59e37c8aSRuslan Bukin }
934*59e37c8aSRuslan Bukin 
935*59e37c8aSRuslan Bukin void
936*59e37c8aSRuslan Bukin iommu_domain_unload(struct iommu_domain *iodom,
937*59e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *entries, bool cansleep)
938*59e37c8aSRuslan Bukin {
939*59e37c8aSRuslan Bukin 	struct dmar_domain *domain;
940*59e37c8aSRuslan Bukin 
941*59e37c8aSRuslan Bukin 	domain = (struct dmar_domain *)iodom;
942*59e37c8aSRuslan Bukin 
943*59e37c8aSRuslan Bukin 	dmar_domain_unload(domain, entries, cansleep);
944*59e37c8aSRuslan Bukin }
945