xref: /freebsd/sys/x86/iommu/intel_ctx.c (revision ee47a12a490f89226ae736f92d8578ec1ca3b157)
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>
61c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
62c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
63c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h>
64c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h>
6586be9f0dSKonstantin Belousov #include <machine/atomic.h>
6686be9f0dSKonstantin Belousov #include <machine/bus.h>
6786be9f0dSKonstantin Belousov #include <machine/md_var.h>
6886be9f0dSKonstantin Belousov #include <machine/specialreg.h>
6986be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
70f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h>
71c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
72685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.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_unref_domain_locked(struct dmar_unit *dmar,
781abfd355SKonstantin Belousov     struct dmar_domain *domain);
791abfd355SKonstantin Belousov static void dmar_domain_destroy(struct dmar_domain *domain);
8086be9f0dSKonstantin 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 	 */
9115f6baf4SRuslan Bukin 	ctxm = dmar_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;
10215f6baf4SRuslan Bukin 	ctxm = dmar_pgalloc(dmar->ctx_obj, 1 + bus, IOMMU_PGF_ZERO |
10315f6baf4SRuslan Bukin 	    IOMMU_PGF_WAITOK);
10415f6baf4SRuslan Bukin 	re = dmar_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);
1096b7c46afSKonstantin Belousov 	dmar_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 
121cb9050ddSRuslan Bukin 	ctxp = dmar_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
12859e37c8aSRuslan Bukin device_tag_init(struct dmar_ctx *ctx, device_t dev)
12986be9f0dSKonstantin Belousov {
13059e37c8aSRuslan Bukin 	struct dmar_domain *domain;
13186be9f0dSKonstantin Belousov 	bus_addr_t maxaddr;
13286be9f0dSKonstantin Belousov 
13378b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
13462ad310cSRuslan Bukin 	maxaddr = MIN(domain->iodom.end, BUS_SPACE_MAXADDR);
13559e37c8aSRuslan Bukin 	ctx->context.tag->common.ref_count = 1; /* Prevent free */
13659e37c8aSRuslan Bukin 	ctx->context.tag->common.impl = &bus_dma_iommu_impl;
13759e37c8aSRuslan Bukin 	ctx->context.tag->common.boundary = 0;
13859e37c8aSRuslan Bukin 	ctx->context.tag->common.lowaddr = maxaddr;
13959e37c8aSRuslan Bukin 	ctx->context.tag->common.highaddr = maxaddr;
14059e37c8aSRuslan Bukin 	ctx->context.tag->common.maxsize = maxaddr;
14159e37c8aSRuslan Bukin 	ctx->context.tag->common.nsegments = BUS_SPACE_UNRESTRICTED;
14259e37c8aSRuslan Bukin 	ctx->context.tag->common.maxsegsz = maxaddr;
14378b51754SRuslan Bukin 	ctx->context.tag->ctx = CTX2IOCTX(ctx);
14459e37c8aSRuslan Bukin 	ctx->context.tag->owner = dev;
14586be9f0dSKonstantin Belousov }
14686be9f0dSKonstantin Belousov 
14786be9f0dSKonstantin Belousov static void
148685666aaSKonstantin Belousov ctx_id_entry_init_one(dmar_ctx_entry_t *ctxp, struct dmar_domain *domain,
149685666aaSKonstantin Belousov     vm_page_t ctx_root)
15086be9f0dSKonstantin Belousov {
1511abfd355SKonstantin Belousov 	/*
1521abfd355SKonstantin Belousov 	 * For update due to move, the store is not atomic.  It is
1531abfd355SKonstantin Belousov 	 * possible that DMAR read upper doubleword, while low
1541abfd355SKonstantin Belousov 	 * doubleword is not yet updated.  The domain id is stored in
1551abfd355SKonstantin Belousov 	 * the upper doubleword, while the table pointer in the lower.
1561abfd355SKonstantin Belousov 	 *
1571abfd355SKonstantin Belousov 	 * There is no good solution, for the same reason it is wrong
1581abfd355SKonstantin Belousov 	 * to clear P bit in the ctx entry for update.
1591abfd355SKonstantin Belousov 	 */
1601abfd355SKonstantin Belousov 	dmar_pte_store1(&ctxp->ctx2, DMAR_CTX2_DID(domain->domain) |
1611abfd355SKonstantin Belousov 	    domain->awlvl);
162685666aaSKonstantin Belousov 	if (ctx_root == NULL) {
163685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_PASS | DMAR_CTX1_P);
164685666aaSKonstantin Belousov 	} else {
165685666aaSKonstantin Belousov 		dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_UNTR |
166685666aaSKonstantin Belousov 		    (DMAR_CTX1_ASR_MASK & VM_PAGE_TO_PHYS(ctx_root)) |
167685666aaSKonstantin Belousov 		    DMAR_CTX1_P);
168685666aaSKonstantin Belousov 	}
169685666aaSKonstantin Belousov }
170685666aaSKonstantin Belousov 
171685666aaSKonstantin Belousov static void
172685666aaSKonstantin Belousov ctx_id_entry_init(struct dmar_ctx *ctx, dmar_ctx_entry_t *ctxp, bool move,
173685666aaSKonstantin Belousov     int busno)
174685666aaSKonstantin Belousov {
175685666aaSKonstantin Belousov 	struct dmar_unit *unit;
176685666aaSKonstantin Belousov 	struct dmar_domain *domain;
177685666aaSKonstantin Belousov 	vm_page_t ctx_root;
178685666aaSKonstantin Belousov 	int i;
179685666aaSKonstantin Belousov 
18078b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
18178b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
182685666aaSKonstantin Belousov 	KASSERT(move || (ctxp->ctx1 == 0 && ctxp->ctx2 == 0),
183685666aaSKonstantin Belousov 	    ("dmar%d: initialized ctx entry %d:%d:%d 0x%jx 0x%jx",
18459e37c8aSRuslan Bukin 	    unit->iommu.unit, busno, pci_get_slot(ctx->context.tag->owner),
18559e37c8aSRuslan Bukin 	    pci_get_function(ctx->context.tag->owner),
186685666aaSKonstantin Belousov 	    ctxp->ctx1, ctxp->ctx2));
187685666aaSKonstantin Belousov 
18815f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 &&
18986be9f0dSKonstantin Belousov 	    (unit->hw_ecap & DMAR_ECAP_PT) != 0) {
1901abfd355SKonstantin Belousov 		KASSERT(domain->pgtbl_obj == NULL,
19186be9f0dSKonstantin Belousov 		    ("ctx %p non-null pgtbl_obj", ctx));
192685666aaSKonstantin Belousov 		ctx_root = NULL;
19386be9f0dSKonstantin Belousov 	} else {
19415f6baf4SRuslan Bukin 		ctx_root = dmar_pgalloc(domain->pgtbl_obj, 0,
19515f6baf4SRuslan Bukin 		    IOMMU_PGF_NOALLOC);
196685666aaSKonstantin Belousov 	}
197685666aaSKonstantin Belousov 
19878b51754SRuslan Bukin 	if (iommu_is_buswide_ctx(DMAR2IOMMU(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 {
23859e37c8aSRuslan Bukin 	struct iommu_map_entries_tailq rmrr_entries;
23959e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
24086be9f0dSKonstantin Belousov 	vm_page_t *ma;
24159e37c8aSRuslan 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
25762ad310cSRuslan Bukin 		 * iommu_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",
26359e37c8aSRuslan 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",
27559e37c8aSRuslan 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 		}
28578b51754SRuslan Bukin 		error1 = iommu_gas_map_region(DOM2IODOM(domain), entry,
28659e37c8aSRuslan Bukin 		    IOMMU_MAP_ENTRY_READ | IOMMU_MAP_ENTRY_WRITE,
28759e37c8aSRuslan 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) {
29559e37c8aSRuslan Bukin 			IOMMU_LOCK(domain->iodom.iommu);
2961abfd355SKonstantin Belousov 			domain->refs++; /* XXXKIB prevent free */
29715f6baf4SRuslan Bukin 			domain->iodom.flags |= IOMMU_DOMAIN_RMRR;
29859e37c8aSRuslan 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",
30659e37c8aSRuslan 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);
31178b51754SRuslan Bukin 			iommu_gas_free_entry(DOM2IODOM(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 
320*ee47a12aSRyan Libby /*
321*ee47a12aSRyan Libby  * PCI memory address space is shared between memory-mapped devices (MMIO) and
322*ee47a12aSRyan Libby  * host memory (which may be remapped by an IOMMU).  Device accesses to an
323*ee47a12aSRyan Libby  * address within a memory aperture in a PCIe root port will be treated as
324*ee47a12aSRyan Libby  * peer-to-peer and not forwarded to an IOMMU.  To avoid this, reserve the
325*ee47a12aSRyan Libby  * address space of the root port's memory apertures in the address space used
326*ee47a12aSRyan Libby  * by the IOMMU for remapping.
327*ee47a12aSRyan Libby  */
328*ee47a12aSRyan Libby static int
329*ee47a12aSRyan Libby dmar_reserve_pci_regions(struct dmar_domain *domain, device_t dev)
330*ee47a12aSRyan Libby {
331*ee47a12aSRyan Libby 	struct iommu_domain *iodom;
332*ee47a12aSRyan Libby 	device_t root;
333*ee47a12aSRyan Libby 	uint32_t val;
334*ee47a12aSRyan Libby 	uint64_t base, limit;
335*ee47a12aSRyan Libby 	int error;
336*ee47a12aSRyan Libby 
337*ee47a12aSRyan Libby 	iodom = DOM2IODOM(domain);
338*ee47a12aSRyan Libby 
339*ee47a12aSRyan Libby 	root = pci_find_pcie_root_port(dev);
340*ee47a12aSRyan Libby 	if (root == NULL)
341*ee47a12aSRyan Libby 		return (0);
342*ee47a12aSRyan Libby 
343*ee47a12aSRyan Libby 	/* Disable downstream memory */
344*ee47a12aSRyan Libby 	base = PCI_PPBMEMBASE(0, pci_read_config(root, PCIR_MEMBASE_1, 2));
345*ee47a12aSRyan Libby 	limit = PCI_PPBMEMLIMIT(0, pci_read_config(root, PCIR_MEMLIMIT_1, 2));
346*ee47a12aSRyan Libby 	error = iommu_gas_reserve_region_extend(iodom, base, limit + 1);
347*ee47a12aSRyan Libby 	if (bootverbose || error != 0)
348*ee47a12aSRyan Libby 		device_printf(dev, "DMAR reserve [%#jx-%#jx] (error %d)\n",
349*ee47a12aSRyan Libby 		    base, limit + 1, error);
350*ee47a12aSRyan Libby 	if (error != 0)
351*ee47a12aSRyan Libby 		return (error);
352*ee47a12aSRyan Libby 
353*ee47a12aSRyan Libby 	/* Disable downstream prefetchable memory */
354*ee47a12aSRyan Libby 	val = pci_read_config(root, PCIR_PMBASEL_1, 2);
355*ee47a12aSRyan Libby 	if (val != 0 || pci_read_config(root, PCIR_PMLIMITL_1, 2) != 0) {
356*ee47a12aSRyan Libby 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
357*ee47a12aSRyan Libby 			base = PCI_PPBMEMBASE(
358*ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMBASEH_1, 4),
359*ee47a12aSRyan Libby 			    val);
360*ee47a12aSRyan Libby 			limit = PCI_PPBMEMLIMIT(
361*ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITH_1, 4),
362*ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITL_1, 2));
363*ee47a12aSRyan Libby 		} else {
364*ee47a12aSRyan Libby 			base = PCI_PPBMEMBASE(0, val);
365*ee47a12aSRyan Libby 			limit = PCI_PPBMEMLIMIT(0,
366*ee47a12aSRyan Libby 			    pci_read_config(root, PCIR_PMLIMITL_1, 2));
367*ee47a12aSRyan Libby 		}
368*ee47a12aSRyan Libby 		error = iommu_gas_reserve_region_extend(iodom, base,
369*ee47a12aSRyan Libby 		    limit + 1);
370*ee47a12aSRyan Libby 		if (bootverbose || error != 0)
371*ee47a12aSRyan Libby 			device_printf(dev, "DMAR reserve [%#jx-%#jx] "
372*ee47a12aSRyan Libby 			    "(error %d)\n", base, limit + 1, error);
373*ee47a12aSRyan Libby 		if (error != 0)
374*ee47a12aSRyan Libby 			return (error);
375*ee47a12aSRyan Libby 	}
376*ee47a12aSRyan Libby 
377*ee47a12aSRyan Libby 	return (error);
378*ee47a12aSRyan Libby }
379*ee47a12aSRyan Libby 
3801abfd355SKonstantin Belousov static struct dmar_domain *
3811abfd355SKonstantin Belousov dmar_domain_alloc(struct dmar_unit *dmar, bool id_mapped)
3821abfd355SKonstantin Belousov {
38362ad310cSRuslan Bukin 	struct iommu_domain *iodom;
38416696f60SRuslan Bukin 	struct iommu_unit *unit;
3851abfd355SKonstantin Belousov 	struct dmar_domain *domain;
3861abfd355SKonstantin Belousov 	int error, id, mgaw;
3871abfd355SKonstantin Belousov 
3881abfd355SKonstantin Belousov 	id = alloc_unr(dmar->domids);
3891abfd355SKonstantin Belousov 	if (id == -1)
3901abfd355SKonstantin Belousov 		return (NULL);
3911abfd355SKonstantin Belousov 	domain = malloc(sizeof(*domain), M_DMAR_DOMAIN, M_WAITOK | M_ZERO);
39278b51754SRuslan Bukin 	iodom = DOM2IODOM(domain);
39316696f60SRuslan Bukin 	unit = DMAR2IOMMU(dmar);
3941abfd355SKonstantin Belousov 	domain->domain = id;
3951abfd355SKonstantin Belousov 	LIST_INIT(&domain->contexts);
39616696f60SRuslan Bukin 	iommu_domain_init(unit, iodom, &dmar_domain_map_ops);
39716696f60SRuslan Bukin 
3981abfd355SKonstantin Belousov 	domain->dmar = dmar;
3991abfd355SKonstantin Belousov 
4001abfd355SKonstantin Belousov 	/*
4011abfd355SKonstantin Belousov 	 * For now, use the maximal usable physical address of the
4021abfd355SKonstantin Belousov 	 * installed memory to calculate the mgaw on id_mapped domain.
4031abfd355SKonstantin Belousov 	 * It is useful for the identity mapping, and less so for the
4041abfd355SKonstantin Belousov 	 * virtualized bus address space.
4051abfd355SKonstantin Belousov 	 */
40662ad310cSRuslan Bukin 	domain->iodom.end = id_mapped ? ptoa(Maxmem) : BUS_SPACE_MAXADDR;
40762ad310cSRuslan Bukin 	mgaw = dmar_maxaddr2mgaw(dmar, domain->iodom.end, !id_mapped);
4081abfd355SKonstantin Belousov 	error = domain_set_agaw(domain, mgaw);
4091abfd355SKonstantin Belousov 	if (error != 0)
4101abfd355SKonstantin Belousov 		goto fail;
4111abfd355SKonstantin Belousov 	if (!id_mapped)
4121abfd355SKonstantin Belousov 		/* Use all supported address space for remapping. */
41362ad310cSRuslan Bukin 		domain->iodom.end = 1ULL << (domain->agaw - 1);
4141abfd355SKonstantin Belousov 
41578b51754SRuslan Bukin 	iommu_gas_init_domain(DOM2IODOM(domain));
4161abfd355SKonstantin Belousov 
4171abfd355SKonstantin Belousov 	if (id_mapped) {
4181abfd355SKonstantin Belousov 		if ((dmar->hw_ecap & DMAR_ECAP_PT) == 0) {
4191abfd355SKonstantin Belousov 			domain->pgtbl_obj = domain_get_idmap_pgtbl(domain,
42062ad310cSRuslan Bukin 			    domain->iodom.end);
4211abfd355SKonstantin Belousov 		}
42215f6baf4SRuslan Bukin 		domain->iodom.flags |= IOMMU_DOMAIN_IDMAP;
4231abfd355SKonstantin Belousov 	} else {
4241abfd355SKonstantin Belousov 		error = domain_alloc_pgtbl(domain);
4251abfd355SKonstantin Belousov 		if (error != 0)
4261abfd355SKonstantin Belousov 			goto fail;
4271abfd355SKonstantin Belousov 		/* Disable local apic region access */
42862ad310cSRuslan Bukin 		error = iommu_gas_reserve_region(iodom, 0xfee00000,
42994dfb28eSRuslan Bukin 		    0xfeefffff + 1, &iodom->msi_entry);
4301abfd355SKonstantin Belousov 		if (error != 0)
4311abfd355SKonstantin Belousov 			goto fail;
4321abfd355SKonstantin Belousov 	}
4331abfd355SKonstantin Belousov 	return (domain);
4341abfd355SKonstantin Belousov 
4351abfd355SKonstantin Belousov fail:
4361abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
4371abfd355SKonstantin Belousov 	return (NULL);
4381abfd355SKonstantin Belousov }
4391abfd355SKonstantin Belousov 
44086be9f0dSKonstantin Belousov static struct dmar_ctx *
4411abfd355SKonstantin Belousov dmar_ctx_alloc(struct dmar_domain *domain, uint16_t rid)
44286be9f0dSKonstantin Belousov {
44386be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
44486be9f0dSKonstantin Belousov 
44586be9f0dSKonstantin Belousov 	ctx = malloc(sizeof(*ctx), M_DMAR_CTX, M_WAITOK | M_ZERO);
44678b51754SRuslan Bukin 	ctx->context.domain = DOM2IODOM(domain);
44759e37c8aSRuslan Bukin 	ctx->context.tag = malloc(sizeof(struct bus_dma_tag_iommu),
44859e37c8aSRuslan Bukin 	    M_DMAR_CTX, M_WAITOK | M_ZERO);
449cb9050ddSRuslan Bukin 	ctx->context.rid = rid;
4501abfd355SKonstantin Belousov 	ctx->refs = 1;
45186be9f0dSKonstantin Belousov 	return (ctx);
45286be9f0dSKonstantin Belousov }
45386be9f0dSKonstantin Belousov 
45486be9f0dSKonstantin Belousov static void
4551abfd355SKonstantin Belousov dmar_ctx_link(struct dmar_ctx *ctx)
4561abfd355SKonstantin Belousov {
4571abfd355SKonstantin Belousov 	struct dmar_domain *domain;
4581abfd355SKonstantin Belousov 
45978b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
46059e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4611abfd355SKonstantin Belousov 	KASSERT(domain->refs >= domain->ctx_cnt,
4621abfd355SKonstantin Belousov 	    ("dom %p ref underflow %d %d", domain, domain->refs,
4631abfd355SKonstantin Belousov 	    domain->ctx_cnt));
4641abfd355SKonstantin Belousov 	domain->refs++;
4651abfd355SKonstantin Belousov 	domain->ctx_cnt++;
4661abfd355SKonstantin Belousov 	LIST_INSERT_HEAD(&domain->contexts, ctx, link);
4671abfd355SKonstantin Belousov }
4681abfd355SKonstantin Belousov 
4691abfd355SKonstantin Belousov static void
4701abfd355SKonstantin Belousov dmar_ctx_unlink(struct dmar_ctx *ctx)
4711abfd355SKonstantin Belousov {
4721abfd355SKonstantin Belousov 	struct dmar_domain *domain;
4731abfd355SKonstantin Belousov 
47478b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
47559e37c8aSRuslan Bukin 	IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4761abfd355SKonstantin Belousov 	KASSERT(domain->refs > 0,
4771abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d", domain, domain->refs));
4781abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt >= domain->refs,
4791abfd355SKonstantin Belousov 	    ("domain %p ctx dtr refs %d ctx_cnt %d", domain,
4801abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
4811abfd355SKonstantin Belousov 	domain->refs--;
4821abfd355SKonstantin Belousov 	domain->ctx_cnt--;
4831abfd355SKonstantin Belousov 	LIST_REMOVE(ctx, link);
4841abfd355SKonstantin Belousov }
4851abfd355SKonstantin Belousov 
4861abfd355SKonstantin Belousov static void
4871abfd355SKonstantin Belousov dmar_domain_destroy(struct dmar_domain *domain)
48886be9f0dSKonstantin Belousov {
48916696f60SRuslan Bukin 	struct iommu_domain *iodom;
49059e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
49186be9f0dSKonstantin Belousov 
49216696f60SRuslan Bukin 	iodom = DOM2IODOM(domain);
49316696f60SRuslan Bukin 
49459e37c8aSRuslan Bukin 	KASSERT(TAILQ_EMPTY(&domain->iodom.unload_entries),
4951abfd355SKonstantin Belousov 	    ("unfinished unloads %p", domain));
4961abfd355SKonstantin Belousov 	KASSERT(LIST_EMPTY(&domain->contexts),
4971abfd355SKonstantin Belousov 	    ("destroying dom %p with contexts", domain));
4981abfd355SKonstantin Belousov 	KASSERT(domain->ctx_cnt == 0,
4991abfd355SKonstantin Belousov 	    ("destroying dom %p with ctx_cnt %d", domain, domain->ctx_cnt));
5001abfd355SKonstantin Belousov 	KASSERT(domain->refs == 0,
5011abfd355SKonstantin Belousov 	    ("destroying dom %p with refs %d", domain, domain->refs));
50215f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_GAS_INITED) != 0) {
5031abfd355SKonstantin Belousov 		DMAR_DOMAIN_LOCK(domain);
50416696f60SRuslan Bukin 		iommu_gas_fini_domain(iodom);
5051abfd355SKonstantin Belousov 		DMAR_DOMAIN_UNLOCK(domain);
50686be9f0dSKonstantin Belousov 	}
50715f6baf4SRuslan Bukin 	if ((domain->iodom.flags & IOMMU_DOMAIN_PGTBL_INITED) != 0) {
5081abfd355SKonstantin Belousov 		if (domain->pgtbl_obj != NULL)
5091abfd355SKonstantin Belousov 			DMAR_DOMAIN_PGLOCK(domain);
5101abfd355SKonstantin Belousov 		domain_free_pgtbl(domain);
51186be9f0dSKonstantin Belousov 	}
51216696f60SRuslan Bukin 	iommu_domain_fini(iodom);
51378b51754SRuslan Bukin 	dmar = DOM2DMAR(domain);
51459e37c8aSRuslan Bukin 	free_unr(dmar->domids, domain->domain);
5151abfd355SKonstantin Belousov 	free(domain, M_DMAR_DOMAIN);
51686be9f0dSKonstantin Belousov }
51786be9f0dSKonstantin Belousov 
518f9feb091SKonstantin Belousov static struct dmar_ctx *
519f9feb091SKonstantin Belousov dmar_get_ctx_for_dev1(struct dmar_unit *dmar, device_t dev, uint16_t rid,
520f9feb091SKonstantin Belousov     int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
5211abfd355SKonstantin Belousov     bool id_mapped, bool rmrr_init)
52286be9f0dSKonstantin Belousov {
5231abfd355SKonstantin Belousov 	struct dmar_domain *domain, *domain1;
52486be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx, *ctx1;
525ea4c0115SRuslan Bukin 	struct iommu_unit *unit;
52686be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
52786be9f0dSKonstantin Belousov 	struct sf_buf *sf;
5281abfd355SKonstantin Belousov 	int bus, slot, func, error;
52986be9f0dSKonstantin Belousov 	bool enable;
53086be9f0dSKonstantin Belousov 
531f9feb091SKonstantin Belousov 	if (dev != NULL) {
53267499354SRyan Stone 		bus = pci_get_bus(dev);
53367499354SRyan Stone 		slot = pci_get_slot(dev);
53467499354SRyan Stone 		func = pci_get_function(dev);
535f9feb091SKonstantin Belousov 	} else {
536f9feb091SKonstantin Belousov 		bus = PCI_RID2BUS(rid);
537f9feb091SKonstantin Belousov 		slot = PCI_RID2SLOT(rid);
538f9feb091SKonstantin Belousov 		func = PCI_RID2FUNC(rid);
539f9feb091SKonstantin Belousov 	}
54086be9f0dSKonstantin Belousov 	enable = false;
54186be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
54278b51754SRuslan Bukin 	unit = DMAR2IOMMU(dmar);
54386be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
544ea4c0115SRuslan Bukin 	KASSERT(!iommu_is_buswide_ctx(unit, bus) || (slot == 0 && func == 0),
545ea4c0115SRuslan Bukin 	    ("iommu%d pci%d:%d:%d get_ctx for buswide", dmar->iommu.unit, bus,
546685666aaSKonstantin Belousov 	    slot, func));
54767499354SRyan Stone 	ctx = dmar_find_ctx_locked(dmar, rid);
54886be9f0dSKonstantin Belousov 	error = 0;
54986be9f0dSKonstantin Belousov 	if (ctx == NULL) {
55086be9f0dSKonstantin Belousov 		/*
55186be9f0dSKonstantin Belousov 		 * Perform the allocations which require sleep or have
55286be9f0dSKonstantin Belousov 		 * higher chance to succeed if the sleep is allowed.
55386be9f0dSKonstantin Belousov 		 */
55486be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
555b29d186cSKonstantin Belousov 		dmar_ensure_ctx_page(dmar, PCI_RID2BUS(rid));
5561abfd355SKonstantin Belousov 		domain1 = dmar_domain_alloc(dmar, id_mapped);
5571abfd355SKonstantin Belousov 		if (domain1 == NULL) {
55886be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
55986be9f0dSKonstantin Belousov 			return (NULL);
56086be9f0dSKonstantin Belousov 		}
5615f8e5c7fSKonstantin Belousov 		if (!id_mapped) {
562f9feb091SKonstantin Belousov 			error = domain_init_rmrr(domain1, dev, bus,
563f9feb091SKonstantin Belousov 			    slot, func, dev_domain, dev_busno, dev_path,
564f9feb091SKonstantin Belousov 			    dev_path_len);
565*ee47a12aSRyan Libby 			if (error == 0)
566*ee47a12aSRyan Libby 				error = dmar_reserve_pci_regions(domain1, dev);
56786be9f0dSKonstantin Belousov 			if (error != 0) {
5681abfd355SKonstantin Belousov 				dmar_domain_destroy(domain1);
56986be9f0dSKonstantin Belousov 				TD_PINNED_ASSERT;
57086be9f0dSKonstantin Belousov 				return (NULL);
57186be9f0dSKonstantin Belousov 			}
5725f8e5c7fSKonstantin Belousov 		}
5731abfd355SKonstantin Belousov 		ctx1 = dmar_ctx_alloc(domain1, rid);
57486be9f0dSKonstantin Belousov 		ctxp = dmar_map_ctx_entry(ctx1, &sf);
57586be9f0dSKonstantin Belousov 		DMAR_LOCK(dmar);
57686be9f0dSKonstantin Belousov 
57786be9f0dSKonstantin Belousov 		/*
57886be9f0dSKonstantin Belousov 		 * Recheck the contexts, other thread might have
57986be9f0dSKonstantin Belousov 		 * already allocated needed one.
58086be9f0dSKonstantin Belousov 		 */
58167499354SRyan Stone 		ctx = dmar_find_ctx_locked(dmar, rid);
58286be9f0dSKonstantin Belousov 		if (ctx == NULL) {
5831abfd355SKonstantin Belousov 			domain = domain1;
58486be9f0dSKonstantin Belousov 			ctx = ctx1;
5851abfd355SKonstantin Belousov 			dmar_ctx_link(ctx);
58659e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
58759e37c8aSRuslan Bukin 			device_tag_init(ctx, dev);
58886be9f0dSKonstantin Belousov 
58986be9f0dSKonstantin Belousov 			/*
59086be9f0dSKonstantin Belousov 			 * This is the first activated context for the
59186be9f0dSKonstantin Belousov 			 * DMAR unit.  Enable the translation after
59286be9f0dSKonstantin Belousov 			 * everything is set up.
59386be9f0dSKonstantin Belousov 			 */
5941abfd355SKonstantin Belousov 			if (LIST_EMPTY(&dmar->domains))
59586be9f0dSKonstantin Belousov 				enable = true;
5961abfd355SKonstantin Belousov 			LIST_INSERT_HEAD(&dmar->domains, domain, link);
597685666aaSKonstantin Belousov 			ctx_id_entry_init(ctx, ctxp, false, bus);
598f9feb091SKonstantin Belousov 			if (dev != NULL) {
59986be9f0dSKonstantin Belousov 				device_printf(dev,
60034e8337bSKonstantin Belousov 			    "dmar%d pci%d:%d:%d:%d rid %x domain %d mgaw %d "
6019d0bc6d8SKonstantin Belousov 				    "agaw %d %s-mapped\n",
60259e37c8aSRuslan Bukin 				    dmar->iommu.unit, dmar->segment, bus, slot,
6031abfd355SKonstantin Belousov 				    func, rid, domain->domain, domain->mgaw,
6041abfd355SKonstantin Belousov 				    domain->agaw, id_mapped ? "id" : "re");
605f9feb091SKonstantin Belousov 			}
6063d47c58bSKonstantin Belousov 			dmar_unmap_pgtbl(sf);
60786be9f0dSKonstantin Belousov 		} else {
6083d47c58bSKonstantin Belousov 			dmar_unmap_pgtbl(sf);
6091abfd355SKonstantin Belousov 			dmar_domain_destroy(domain1);
6103d47c58bSKonstantin Belousov 			/* Nothing needs to be done to destroy ctx1. */
6113d47c58bSKonstantin Belousov 			free(ctx1, M_DMAR_CTX);
61278b51754SRuslan Bukin 			domain = CTX2DOM(ctx);
6131abfd355SKonstantin Belousov 			ctx->refs++; /* tag referenced us */
61486be9f0dSKonstantin Belousov 		}
61568eeb96aSKonstantin Belousov 	} else {
61678b51754SRuslan Bukin 		domain = CTX2DOM(ctx);
61759e37c8aSRuslan Bukin 		if (ctx->context.tag->owner == NULL)
61859e37c8aSRuslan Bukin 			ctx->context.tag->owner = dev;
6191abfd355SKonstantin Belousov 		ctx->refs++; /* tag referenced us */
6201abfd355SKonstantin Belousov 	}
6211abfd355SKonstantin Belousov 
6221abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, enable);
62386be9f0dSKonstantin Belousov 	if (error != 0) {
62486be9f0dSKonstantin Belousov 		dmar_free_ctx_locked(dmar, ctx);
62586be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
62686be9f0dSKonstantin Belousov 		return (NULL);
62786be9f0dSKonstantin Belousov 	}
62868eeb96aSKonstantin Belousov 
62968eeb96aSKonstantin Belousov 	/*
63068eeb96aSKonstantin Belousov 	 * The dmar lock was potentially dropped between check for the
63168eeb96aSKonstantin Belousov 	 * empty context list and now.  Recheck the state of GCMD_TE
63268eeb96aSKonstantin Belousov 	 * to avoid unneeded command.
63368eeb96aSKonstantin Belousov 	 */
63468eeb96aSKonstantin Belousov 	if (enable && !rmrr_init && (dmar->hw_gcmd & DMAR_GCMD_TE) == 0) {
63586be9f0dSKonstantin Belousov 		error = dmar_enable_translation(dmar);
636f9feb091SKonstantin Belousov 		if (error == 0) {
637f9feb091SKonstantin Belousov 			if (bootverbose) {
638f9feb091SKonstantin Belousov 				printf("dmar%d: enabled translation\n",
63959e37c8aSRuslan Bukin 				    dmar->iommu.unit);
640f9feb091SKonstantin Belousov 			}
641f9feb091SKonstantin Belousov 		} else {
642f9feb091SKonstantin Belousov 			printf("dmar%d: enabling translation failed, "
64359e37c8aSRuslan Bukin 			    "error %d\n", dmar->iommu.unit, error);
64486be9f0dSKonstantin Belousov 			dmar_free_ctx_locked(dmar, ctx);
64586be9f0dSKonstantin Belousov 			TD_PINNED_ASSERT;
64686be9f0dSKonstantin Belousov 			return (NULL);
64786be9f0dSKonstantin Belousov 		}
64886be9f0dSKonstantin Belousov 	}
64986be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
65086be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
65186be9f0dSKonstantin Belousov 	return (ctx);
65286be9f0dSKonstantin Belousov }
65386be9f0dSKonstantin Belousov 
654f9feb091SKonstantin Belousov struct dmar_ctx *
655f9feb091SKonstantin Belousov dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev, uint16_t rid,
656f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
657f9feb091SKonstantin Belousov {
658f9feb091SKonstantin Belousov 	int dev_domain, dev_path_len, dev_busno;
659f9feb091SKonstantin Belousov 
660f9feb091SKonstantin Belousov 	dev_domain = pci_get_domain(dev);
661f9feb091SKonstantin Belousov 	dev_path_len = dmar_dev_depth(dev);
662f9feb091SKonstantin Belousov 	ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
663f9feb091SKonstantin Belousov 	dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
664f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, dev, rid, dev_domain, dev_busno,
665f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
666f9feb091SKonstantin Belousov }
667f9feb091SKonstantin Belousov 
668f9feb091SKonstantin Belousov struct dmar_ctx *
669f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
670f9feb091SKonstantin Belousov     int dev_domain, int dev_busno,
671f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len,
672f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init)
673f9feb091SKonstantin Belousov {
674f9feb091SKonstantin Belousov 
675f9feb091SKonstantin Belousov 	return (dmar_get_ctx_for_dev1(dmar, NULL, rid, dev_domain, dev_busno,
676f9feb091SKonstantin Belousov 	    dev_path, dev_path_len, id_mapped, rmrr_init));
677f9feb091SKonstantin Belousov }
678f9feb091SKonstantin Belousov 
6791abfd355SKonstantin Belousov int
6801abfd355SKonstantin Belousov dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx)
6811abfd355SKonstantin Belousov {
6821abfd355SKonstantin Belousov 	struct dmar_unit *dmar;
6831abfd355SKonstantin Belousov 	struct dmar_domain *old_domain;
6841abfd355SKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
6851abfd355SKonstantin Belousov 	struct sf_buf *sf;
6861abfd355SKonstantin Belousov 	int error;
6871abfd355SKonstantin Belousov 
6881abfd355SKonstantin Belousov 	dmar = domain->dmar;
68978b51754SRuslan Bukin 	old_domain = CTX2DOM(ctx);
6901abfd355SKonstantin Belousov 	if (domain == old_domain)
6911abfd355SKonstantin Belousov 		return (0);
69259e37c8aSRuslan Bukin 	KASSERT(old_domain->iodom.iommu == domain->iodom.iommu,
6931abfd355SKonstantin Belousov 	    ("domain %p %u moving between dmars %u %u", domain,
69459e37c8aSRuslan Bukin 	    domain->domain, old_domain->iodom.iommu->unit,
69559e37c8aSRuslan Bukin 	    domain->iodom.iommu->unit));
6961abfd355SKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
6971abfd355SKonstantin Belousov 
6981abfd355SKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
6991abfd355SKonstantin Belousov 	DMAR_LOCK(dmar);
7001abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
70159e37c8aSRuslan Bukin 	ctx->context.domain = &domain->iodom;
7021abfd355SKonstantin Belousov 	dmar_ctx_link(ctx);
703685666aaSKonstantin Belousov 	ctx_id_entry_init(ctx, ctxp, true, PCI_BUSMAX + 100);
7041abfd355SKonstantin Belousov 	dmar_unmap_pgtbl(sf);
7051abfd355SKonstantin Belousov 	error = dmar_flush_for_ctx_entry(dmar, true);
7061abfd355SKonstantin Belousov 	/* If flush failed, rolling back would not work as well. */
7071abfd355SKonstantin Belousov 	printf("dmar%d rid %x domain %d->%d %s-mapped\n",
708cb9050ddSRuslan Bukin 	    dmar->iommu.unit, ctx->context.rid, old_domain->domain,
709cb9050ddSRuslan Bukin 	    domain->domain, (domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 ?
710cb9050ddSRuslan Bukin 	    "id" : "re");
7111abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, old_domain);
7121abfd355SKonstantin Belousov 	TD_PINNED_ASSERT;
7131abfd355SKonstantin Belousov 	return (error);
7141abfd355SKonstantin Belousov }
7151abfd355SKonstantin Belousov 
7161abfd355SKonstantin Belousov static void
7171abfd355SKonstantin Belousov dmar_unref_domain_locked(struct dmar_unit *dmar, struct dmar_domain *domain)
7181abfd355SKonstantin Belousov {
7191abfd355SKonstantin Belousov 
7201abfd355SKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
7211abfd355SKonstantin Belousov 	KASSERT(domain->refs >= 1,
72259e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %u", dmar->iommu.unit, domain,
72359e37c8aSRuslan Bukin 	    domain->refs));
7241abfd355SKonstantin Belousov 	KASSERT(domain->refs > domain->ctx_cnt,
72559e37c8aSRuslan Bukin 	    ("dmar %d domain %p refs %d ctx_cnt %d", dmar->iommu.unit, domain,
7261abfd355SKonstantin Belousov 	    domain->refs, domain->ctx_cnt));
7271abfd355SKonstantin Belousov 
7281abfd355SKonstantin Belousov 	if (domain->refs > 1) {
7291abfd355SKonstantin Belousov 		domain->refs--;
7301abfd355SKonstantin Belousov 		DMAR_UNLOCK(dmar);
7311abfd355SKonstantin Belousov 		return;
7321abfd355SKonstantin Belousov 	}
7331abfd355SKonstantin Belousov 
73415f6baf4SRuslan Bukin 	KASSERT((domain->iodom.flags & IOMMU_DOMAIN_RMRR) == 0,
7351abfd355SKonstantin Belousov 	    ("lost ref on RMRR domain %p", domain));
7361abfd355SKonstantin Belousov 
7371abfd355SKonstantin Belousov 	LIST_REMOVE(domain, link);
7381abfd355SKonstantin Belousov 	DMAR_UNLOCK(dmar);
7391abfd355SKonstantin Belousov 
74059e37c8aSRuslan Bukin 	taskqueue_drain(dmar->iommu.delayed_taskqueue,
74159e37c8aSRuslan Bukin 	    &domain->iodom.unload_task);
7421abfd355SKonstantin Belousov 	dmar_domain_destroy(domain);
7431abfd355SKonstantin Belousov }
7441abfd355SKonstantin Belousov 
74586be9f0dSKonstantin Belousov void
74686be9f0dSKonstantin Belousov dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx)
74786be9f0dSKonstantin Belousov {
74886be9f0dSKonstantin Belousov 	struct sf_buf *sf;
74986be9f0dSKonstantin Belousov 	dmar_ctx_entry_t *ctxp;
7501abfd355SKonstantin Belousov 	struct dmar_domain *domain;
75186be9f0dSKonstantin Belousov 
75286be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
75386be9f0dSKonstantin Belousov 	KASSERT(ctx->refs >= 1,
75486be9f0dSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs));
75586be9f0dSKonstantin Belousov 
75686be9f0dSKonstantin Belousov 	/*
75786be9f0dSKonstantin Belousov 	 * If our reference is not last, only the dereference should
75886be9f0dSKonstantin Belousov 	 * be performed.
75986be9f0dSKonstantin Belousov 	 */
76086be9f0dSKonstantin Belousov 	if (ctx->refs > 1) {
76186be9f0dSKonstantin Belousov 		ctx->refs--;
76286be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
76386be9f0dSKonstantin Belousov 		return;
76486be9f0dSKonstantin Belousov 	}
76586be9f0dSKonstantin Belousov 
76659e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
76786be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
76886be9f0dSKonstantin Belousov 
76986be9f0dSKonstantin Belousov 	/*
77086be9f0dSKonstantin Belousov 	 * Otherwise, the context entry must be cleared before the
77186be9f0dSKonstantin Belousov 	 * page table is destroyed.  The mapping of the context
77286be9f0dSKonstantin Belousov 	 * entries page could require sleep, unlock the dmar.
77386be9f0dSKonstantin Belousov 	 */
77486be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
77586be9f0dSKonstantin Belousov 	TD_PREP_PINNED_ASSERT;
77686be9f0dSKonstantin Belousov 	ctxp = dmar_map_ctx_entry(ctx, &sf);
77786be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
77886be9f0dSKonstantin Belousov 	KASSERT(ctx->refs >= 1,
77986be9f0dSKonstantin Belousov 	    ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs));
78086be9f0dSKonstantin Belousov 
78186be9f0dSKonstantin Belousov 	/*
78286be9f0dSKonstantin Belousov 	 * Other thread might have referenced the context, in which
78386be9f0dSKonstantin Belousov 	 * case again only the dereference should be performed.
78486be9f0dSKonstantin Belousov 	 */
78586be9f0dSKonstantin Belousov 	if (ctx->refs > 1) {
78686be9f0dSKonstantin Belousov 		ctx->refs--;
78786be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
7886b7c46afSKonstantin Belousov 		dmar_unmap_pgtbl(sf);
78986be9f0dSKonstantin Belousov 		TD_PINNED_ASSERT;
79086be9f0dSKonstantin Belousov 		return;
79186be9f0dSKonstantin Belousov 	}
79286be9f0dSKonstantin Belousov 
79359e37c8aSRuslan Bukin 	KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
79486be9f0dSKonstantin Belousov 	    ("lost ref on disabled ctx %p", ctx));
79586be9f0dSKonstantin Belousov 
79686be9f0dSKonstantin Belousov 	/*
79786be9f0dSKonstantin Belousov 	 * Clear the context pointer and flush the caches.
79886be9f0dSKonstantin Belousov 	 * XXXKIB: cannot do this if any RMRR entries are still present.
79986be9f0dSKonstantin Belousov 	 */
80086be9f0dSKonstantin Belousov 	dmar_pte_clear(&ctxp->ctx1);
80186be9f0dSKonstantin Belousov 	ctxp->ctx2 = 0;
8026b7c46afSKonstantin Belousov 	dmar_flush_ctx_to_ram(dmar, ctxp);
80386be9f0dSKonstantin Belousov 	dmar_inv_ctx_glob(dmar);
80468eeb96aSKonstantin Belousov 	if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) {
80568eeb96aSKonstantin Belousov 		if (dmar->qi_enabled)
80668eeb96aSKonstantin Belousov 			dmar_qi_invalidate_iotlb_glob_locked(dmar);
80768eeb96aSKonstantin Belousov 		else
80886be9f0dSKonstantin Belousov 			dmar_inv_iotlb_glob(dmar);
80968eeb96aSKonstantin Belousov 	}
8106b7c46afSKonstantin Belousov 	dmar_unmap_pgtbl(sf);
81178b51754SRuslan Bukin 	domain = CTX2DOM(ctx);
8121abfd355SKonstantin Belousov 	dmar_ctx_unlink(ctx);
81359e37c8aSRuslan Bukin 	free(ctx->context.tag, M_DMAR_CTX);
8141abfd355SKonstantin Belousov 	free(ctx, M_DMAR_CTX);
8151abfd355SKonstantin Belousov 	dmar_unref_domain_locked(dmar, domain);
81686be9f0dSKonstantin Belousov 	TD_PINNED_ASSERT;
81786be9f0dSKonstantin Belousov }
81886be9f0dSKonstantin Belousov 
81986be9f0dSKonstantin Belousov void
82086be9f0dSKonstantin Belousov dmar_free_ctx(struct dmar_ctx *ctx)
82186be9f0dSKonstantin Belousov {
82286be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
82386be9f0dSKonstantin Belousov 
82478b51754SRuslan Bukin 	dmar = CTX2DMAR(ctx);
82586be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
82686be9f0dSKonstantin Belousov 	dmar_free_ctx_locked(dmar, ctx);
82786be9f0dSKonstantin Belousov }
82886be9f0dSKonstantin Belousov 
8291abfd355SKonstantin Belousov /*
8301abfd355SKonstantin Belousov  * Returns with the domain locked.
8311abfd355SKonstantin Belousov  */
83286be9f0dSKonstantin Belousov struct dmar_ctx *
83367499354SRyan Stone dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid)
83486be9f0dSKonstantin Belousov {
8351abfd355SKonstantin Belousov 	struct dmar_domain *domain;
83686be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
83786be9f0dSKonstantin Belousov 
83886be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
83986be9f0dSKonstantin Belousov 
8401abfd355SKonstantin Belousov 	LIST_FOREACH(domain, &dmar->domains, link) {
8411abfd355SKonstantin Belousov 		LIST_FOREACH(ctx, &domain->contexts, link) {
842cb9050ddSRuslan Bukin 			if (ctx->context.rid == rid)
84386be9f0dSKonstantin Belousov 				return (ctx);
84486be9f0dSKonstantin Belousov 		}
8451abfd355SKonstantin Belousov 	}
84686be9f0dSKonstantin Belousov 	return (NULL);
84786be9f0dSKonstantin Belousov }
84886be9f0dSKonstantin Belousov 
84986be9f0dSKonstantin Belousov void
85059e37c8aSRuslan Bukin dmar_domain_free_entry(struct iommu_map_entry *entry, bool free)
85168eeb96aSKonstantin Belousov {
85262ad310cSRuslan Bukin 	struct iommu_domain *domain;
85368eeb96aSKonstantin Belousov 
85462ad310cSRuslan Bukin 	domain = entry->domain;
85562ad310cSRuslan Bukin 	IOMMU_DOMAIN_LOCK(domain);
85659e37c8aSRuslan Bukin 	if ((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
85762ad310cSRuslan Bukin 		iommu_gas_free_region(domain, entry);
85868eeb96aSKonstantin Belousov 	else
85962ad310cSRuslan Bukin 		iommu_gas_free_space(domain, entry);
86062ad310cSRuslan Bukin 	IOMMU_DOMAIN_UNLOCK(domain);
86168eeb96aSKonstantin Belousov 	if (free)
86262ad310cSRuslan Bukin 		iommu_gas_free_entry(domain, entry);
86368eeb96aSKonstantin Belousov 	else
86468eeb96aSKonstantin Belousov 		entry->flags = 0;
86568eeb96aSKonstantin Belousov }
86668eeb96aSKonstantin Belousov 
86768eeb96aSKonstantin Belousov void
86859e37c8aSRuslan Bukin dmar_domain_unload_entry(struct iommu_map_entry *entry, bool free)
86968eeb96aSKonstantin Belousov {
87059e37c8aSRuslan Bukin 	struct dmar_domain *domain;
87168eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
87268eeb96aSKonstantin Belousov 
87378b51754SRuslan Bukin 	domain = IODOM2DOM(entry->domain);
87478b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
87568eeb96aSKonstantin Belousov 	if (unit->qi_enabled) {
87668eeb96aSKonstantin Belousov 		DMAR_LOCK(unit);
87778b51754SRuslan Bukin 		dmar_qi_invalidate_locked(IODOM2DOM(entry->domain),
87859e37c8aSRuslan Bukin 		    entry->start, entry->end - entry->start, &entry->gseq,
87959e37c8aSRuslan Bukin 		    true);
88068eeb96aSKonstantin Belousov 		if (!free)
88159e37c8aSRuslan Bukin 			entry->flags |= IOMMU_MAP_ENTRY_QI_NF;
88268eeb96aSKonstantin Belousov 		TAILQ_INSERT_TAIL(&unit->tlb_flush_entries, entry, dmamap_link);
88368eeb96aSKonstantin Belousov 		DMAR_UNLOCK(unit);
88468eeb96aSKonstantin Belousov 	} else {
88578b51754SRuslan Bukin 		domain_flush_iotlb_sync(IODOM2DOM(entry->domain),
88659e37c8aSRuslan Bukin 		    entry->start, entry->end - entry->start);
8871abfd355SKonstantin Belousov 		dmar_domain_free_entry(entry, free);
88868eeb96aSKonstantin Belousov 	}
88968eeb96aSKonstantin Belousov }
89068eeb96aSKonstantin Belousov 
891cf619a92SKonstantin Belousov static bool
892cf619a92SKonstantin Belousov dmar_domain_unload_emit_wait(struct dmar_domain *domain,
89359e37c8aSRuslan Bukin     struct iommu_map_entry *entry)
894e164cafcSKonstantin Belousov {
895e164cafcSKonstantin Belousov 
896cf619a92SKonstantin Belousov 	if (TAILQ_NEXT(entry, dmamap_link) == NULL)
897cf619a92SKonstantin Belousov 		return (true);
898cf619a92SKonstantin Belousov 	return (domain->batch_no++ % dmar_batch_coalesce == 0);
899e164cafcSKonstantin Belousov }
900e164cafcSKonstantin Belousov 
90168eeb96aSKonstantin Belousov void
9021abfd355SKonstantin Belousov dmar_domain_unload(struct dmar_domain *domain,
90359e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *entries, bool cansleep)
90486be9f0dSKonstantin Belousov {
90568eeb96aSKonstantin Belousov 	struct dmar_unit *unit;
9060eed04c8SRuslan Bukin 	struct iommu_domain *iodom;
90759e37c8aSRuslan Bukin 	struct iommu_map_entry *entry, *entry1;
90886be9f0dSKonstantin Belousov 	int error;
90986be9f0dSKonstantin Belousov 
91078b51754SRuslan Bukin 	iodom = DOM2IODOM(domain);
91178b51754SRuslan Bukin 	unit = DOM2DMAR(domain);
91268eeb96aSKonstantin Belousov 
91368eeb96aSKonstantin Belousov 	TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) {
91459e37c8aSRuslan Bukin 		KASSERT((entry->flags & IOMMU_MAP_ENTRY_MAP) != 0,
9151abfd355SKonstantin Belousov 		    ("not mapped entry %p %p", domain, entry));
9160eed04c8SRuslan Bukin 		error = iodom->ops->unmap(iodom, entry->start, entry->end -
91715f6baf4SRuslan Bukin 		    entry->start, cansleep ? IOMMU_PGF_WAITOK : 0);
9181abfd355SKonstantin Belousov 		KASSERT(error == 0, ("unmap %p error %d", domain, error));
91968eeb96aSKonstantin Belousov 		if (!unit->qi_enabled) {
9201abfd355SKonstantin Belousov 			domain_flush_iotlb_sync(domain, entry->start,
92168eeb96aSKonstantin Belousov 			    entry->end - entry->start);
92268eeb96aSKonstantin Belousov 			TAILQ_REMOVE(entries, entry, dmamap_link);
9231abfd355SKonstantin Belousov 			dmar_domain_free_entry(entry, true);
92486be9f0dSKonstantin Belousov 		}
92586be9f0dSKonstantin Belousov 	}
92668eeb96aSKonstantin Belousov 	if (TAILQ_EMPTY(entries))
92768eeb96aSKonstantin Belousov 		return;
92868eeb96aSKonstantin Belousov 
92968eeb96aSKonstantin Belousov 	KASSERT(unit->qi_enabled, ("loaded entry left"));
93068eeb96aSKonstantin Belousov 	DMAR_LOCK(unit);
93168eeb96aSKonstantin Belousov 	TAILQ_FOREACH(entry, entries, dmamap_link) {
9321abfd355SKonstantin Belousov 		dmar_qi_invalidate_locked(domain, entry->start, entry->end -
933cf619a92SKonstantin Belousov 		    entry->start, &entry->gseq,
934cf619a92SKonstantin Belousov 		    dmar_domain_unload_emit_wait(domain, entry));
93568eeb96aSKonstantin Belousov 	}
936cf619a92SKonstantin Belousov 	TAILQ_CONCAT(&unit->tlb_flush_entries, entries, dmamap_link);
93768eeb96aSKonstantin Belousov 	DMAR_UNLOCK(unit);
93868eeb96aSKonstantin Belousov }
93986be9f0dSKonstantin Belousov 
94059e37c8aSRuslan Bukin struct iommu_ctx *
94159e37c8aSRuslan Bukin iommu_get_ctx(struct iommu_unit *iommu, device_t dev, uint16_t rid,
94259e37c8aSRuslan Bukin     bool id_mapped, bool rmrr_init)
94359e37c8aSRuslan Bukin {
94459e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
94559e37c8aSRuslan Bukin 	struct dmar_ctx *ret;
94659e37c8aSRuslan Bukin 
94778b51754SRuslan Bukin 	dmar = IOMMU2DMAR(iommu);
94859e37c8aSRuslan Bukin 
94959e37c8aSRuslan Bukin 	ret = dmar_get_ctx_for_dev(dmar, dev, rid, id_mapped, rmrr_init);
95059e37c8aSRuslan Bukin 
95178b51754SRuslan Bukin 	return (CTX2IOCTX(ret));
95259e37c8aSRuslan Bukin }
95359e37c8aSRuslan Bukin 
95459e37c8aSRuslan Bukin void
95559e37c8aSRuslan Bukin iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *context)
95659e37c8aSRuslan Bukin {
95759e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
95859e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
95959e37c8aSRuslan Bukin 
96078b51754SRuslan Bukin 	dmar = IOMMU2DMAR(iommu);
96178b51754SRuslan Bukin 	ctx = IOCTX2CTX(context);
96259e37c8aSRuslan Bukin 
96359e37c8aSRuslan Bukin 	dmar_free_ctx_locked(dmar, ctx);
96459e37c8aSRuslan Bukin }
96559e37c8aSRuslan Bukin 
96659e37c8aSRuslan Bukin void
96759e37c8aSRuslan Bukin iommu_free_ctx(struct iommu_ctx *context)
96859e37c8aSRuslan Bukin {
96959e37c8aSRuslan Bukin 	struct dmar_ctx *ctx;
97059e37c8aSRuslan Bukin 
97178b51754SRuslan Bukin 	ctx = IOCTX2CTX(context);
97259e37c8aSRuslan Bukin 
97359e37c8aSRuslan Bukin 	dmar_free_ctx(ctx);
97459e37c8aSRuslan Bukin }
97559e37c8aSRuslan Bukin 
97659e37c8aSRuslan Bukin void
97759e37c8aSRuslan Bukin iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free)
97859e37c8aSRuslan Bukin {
97959e37c8aSRuslan Bukin 
98059e37c8aSRuslan Bukin 	dmar_domain_unload_entry(entry, free);
98159e37c8aSRuslan Bukin }
98259e37c8aSRuslan Bukin 
98359e37c8aSRuslan Bukin void
98459e37c8aSRuslan Bukin iommu_domain_unload(struct iommu_domain *iodom,
98559e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *entries, bool cansleep)
98659e37c8aSRuslan Bukin {
98759e37c8aSRuslan Bukin 	struct dmar_domain *domain;
98859e37c8aSRuslan Bukin 
98978b51754SRuslan Bukin 	domain = IODOM2DOM(iodom);
99059e37c8aSRuslan Bukin 
99159e37c8aSRuslan Bukin 	dmar_domain_unload(domain, entries, cansleep);
99259e37c8aSRuslan Bukin }
993