186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni *
486be9f0dSKonstantin Belousov * Copyright (c) 2013 The FreeBSD Foundation
586be9f0dSKonstantin Belousov *
686be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov *
986be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov * are met:
1286be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov *
1886be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov */
3086be9f0dSKonstantin Belousov
3186be9f0dSKonstantin Belousov #include <sys/param.h>
3286be9f0dSKonstantin Belousov #include <sys/systm.h>
3386be9f0dSKonstantin Belousov #include <sys/malloc.h>
3486be9f0dSKonstantin Belousov #include <sys/bus.h>
3586be9f0dSKonstantin Belousov #include <sys/interrupt.h>
3686be9f0dSKonstantin Belousov #include <sys/kernel.h>
3786be9f0dSKonstantin Belousov #include <sys/ktr.h>
3886be9f0dSKonstantin Belousov #include <sys/limits.h>
3986be9f0dSKonstantin Belousov #include <sys/lock.h>
4086be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4186be9f0dSKonstantin Belousov #include <sys/mutex.h>
4286be9f0dSKonstantin Belousov #include <sys/proc.h>
4386be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4486be9f0dSKonstantin Belousov #include <sys/rman.h>
4586be9f0dSKonstantin Belousov #include <sys/sysctl.h>
4686be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
4786be9f0dSKonstantin Belousov #include <sys/tree.h>
4886be9f0dSKonstantin Belousov #include <sys/uio.h>
490a110d5bSKonstantin Belousov #include <sys/vmem.h>
5086be9f0dSKonstantin Belousov #include <vm/vm.h>
5186be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5286be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5386be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5486be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_map.h>
57c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
58c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
59c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h>
60c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h>
6186be9f0dSKonstantin Belousov #include <machine/atomic.h>
6286be9f0dSKonstantin Belousov #include <machine/bus.h>
6386be9f0dSKonstantin Belousov #include <machine/md_var.h>
6486be9f0dSKonstantin Belousov #include <machine/specialreg.h>
6586be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
66f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h>
67c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
6840d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
69685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7086be9f0dSKonstantin Belousov
7186be9f0dSKonstantin Belousov static MALLOC_DEFINE(M_DMAR_CTX, "dmar_ctx", "Intel DMAR Context");
721abfd355SKonstantin Belousov static MALLOC_DEFINE(M_DMAR_DOMAIN, "dmar_dom", "Intel DMAR Domain");
7386be9f0dSKonstantin Belousov
741abfd355SKonstantin Belousov static void dmar_unref_domain_locked(struct dmar_unit *dmar,
751abfd355SKonstantin Belousov struct dmar_domain *domain);
761abfd355SKonstantin Belousov static void dmar_domain_destroy(struct dmar_domain *domain);
7786be9f0dSKonstantin Belousov
7865b133e5SKonstantin Belousov static void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx);
7965b133e5SKonstantin Belousov
8086be9f0dSKonstantin Belousov static void
dmar_ensure_ctx_page(struct dmar_unit * dmar,int bus)8186be9f0dSKonstantin Belousov dmar_ensure_ctx_page(struct dmar_unit *dmar, int bus)
8286be9f0dSKonstantin Belousov {
8386be9f0dSKonstantin Belousov struct sf_buf *sf;
8486be9f0dSKonstantin Belousov dmar_root_entry_t *re;
8586be9f0dSKonstantin Belousov vm_page_t ctxm;
8686be9f0dSKonstantin Belousov
8786be9f0dSKonstantin Belousov /*
8886be9f0dSKonstantin Belousov * Allocated context page must be linked.
8986be9f0dSKonstantin Belousov */
9040d951bcSKonstantin Belousov ctxm = iommu_pgalloc(dmar->ctx_obj, 1 + bus, IOMMU_PGF_NOALLOC);
9186be9f0dSKonstantin Belousov if (ctxm != NULL)
9286be9f0dSKonstantin Belousov return;
9386be9f0dSKonstantin Belousov
9486be9f0dSKonstantin Belousov /*
9586be9f0dSKonstantin Belousov * Page not present, allocate and link. Note that other
9686be9f0dSKonstantin Belousov * thread might execute this sequence in parallel. This
9786be9f0dSKonstantin Belousov * should be safe, because the context entries written by both
9886be9f0dSKonstantin Belousov * threads are equal.
9986be9f0dSKonstantin Belousov */
10086be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT;
10140d951bcSKonstantin Belousov ctxm = iommu_pgalloc(dmar->ctx_obj, 1 + bus, IOMMU_PGF_ZERO |
10215f6baf4SRuslan Bukin IOMMU_PGF_WAITOK);
10340d951bcSKonstantin Belousov re = iommu_map_pgtbl(dmar->ctx_obj, 0, IOMMU_PGF_NOALLOC, &sf);
10486be9f0dSKonstantin Belousov re += bus;
10586be9f0dSKonstantin Belousov dmar_pte_store(&re->r1, DMAR_ROOT_R1_P | (DMAR_ROOT_R1_CTP_MASK &
10686be9f0dSKonstantin Belousov VM_PAGE_TO_PHYS(ctxm)));
1076b7c46afSKonstantin Belousov dmar_flush_root_to_ram(dmar, re);
10840d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
10986be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
11086be9f0dSKonstantin Belousov }
11186be9f0dSKonstantin Belousov
11286be9f0dSKonstantin Belousov static dmar_ctx_entry_t *
dmar_map_ctx_entry(struct dmar_ctx * ctx,struct sf_buf ** sfp)11386be9f0dSKonstantin Belousov dmar_map_ctx_entry(struct dmar_ctx *ctx, struct sf_buf **sfp)
11486be9f0dSKonstantin Belousov {
11559e37c8aSRuslan Bukin struct dmar_unit *dmar;
11686be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp;
11786be9f0dSKonstantin Belousov
11878b51754SRuslan Bukin dmar = CTX2DMAR(ctx);
11959e37c8aSRuslan Bukin
12040d951bcSKonstantin Belousov ctxp = iommu_map_pgtbl(dmar->ctx_obj, 1 + PCI_RID2BUS(ctx->context.rid),
121cb9050ddSRuslan Bukin IOMMU_PGF_NOALLOC | IOMMU_PGF_WAITOK, sfp);
122cb9050ddSRuslan Bukin ctxp += ctx->context.rid & 0xff;
12386be9f0dSKonstantin Belousov return (ctxp);
12486be9f0dSKonstantin Belousov }
12586be9f0dSKonstantin Belousov
12686be9f0dSKonstantin Belousov static void
ctx_id_entry_init_one(dmar_ctx_entry_t * ctxp,struct dmar_domain * domain,vm_page_t ctx_root)127685666aaSKonstantin Belousov ctx_id_entry_init_one(dmar_ctx_entry_t *ctxp, struct dmar_domain *domain,
128685666aaSKonstantin Belousov vm_page_t ctx_root)
12986be9f0dSKonstantin Belousov {
1301abfd355SKonstantin Belousov /*
1311abfd355SKonstantin Belousov * For update due to move, the store is not atomic. It is
1321abfd355SKonstantin Belousov * possible that DMAR read upper doubleword, while low
1331abfd355SKonstantin Belousov * doubleword is not yet updated. The domain id is stored in
1341abfd355SKonstantin Belousov * the upper doubleword, while the table pointer in the lower.
1351abfd355SKonstantin Belousov *
1361abfd355SKonstantin Belousov * There is no good solution, for the same reason it is wrong
1371abfd355SKonstantin Belousov * to clear P bit in the ctx entry for update.
1381abfd355SKonstantin Belousov */
1391abfd355SKonstantin Belousov dmar_pte_store1(&ctxp->ctx2, DMAR_CTX2_DID(domain->domain) |
1401abfd355SKonstantin Belousov domain->awlvl);
141685666aaSKonstantin Belousov if (ctx_root == NULL) {
142685666aaSKonstantin Belousov dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_PASS | DMAR_CTX1_P);
143685666aaSKonstantin Belousov } else {
144685666aaSKonstantin Belousov dmar_pte_store1(&ctxp->ctx1, DMAR_CTX1_T_UNTR |
145685666aaSKonstantin Belousov (DMAR_CTX1_ASR_MASK & VM_PAGE_TO_PHYS(ctx_root)) |
146685666aaSKonstantin Belousov DMAR_CTX1_P);
147685666aaSKonstantin Belousov }
148685666aaSKonstantin Belousov }
149685666aaSKonstantin Belousov
150685666aaSKonstantin Belousov static void
ctx_id_entry_init(struct dmar_ctx * ctx,dmar_ctx_entry_t * ctxp,bool move,int busno)151685666aaSKonstantin Belousov ctx_id_entry_init(struct dmar_ctx *ctx, dmar_ctx_entry_t *ctxp, bool move,
152685666aaSKonstantin Belousov int busno)
153685666aaSKonstantin Belousov {
154685666aaSKonstantin Belousov struct dmar_unit *unit;
155685666aaSKonstantin Belousov struct dmar_domain *domain;
156685666aaSKonstantin Belousov vm_page_t ctx_root;
157685666aaSKonstantin Belousov int i;
158685666aaSKonstantin Belousov
15978b51754SRuslan Bukin domain = CTX2DOM(ctx);
16078b51754SRuslan Bukin unit = DOM2DMAR(domain);
161685666aaSKonstantin Belousov KASSERT(move || (ctxp->ctx1 == 0 && ctxp->ctx2 == 0),
162685666aaSKonstantin Belousov ("dmar%d: initialized ctx entry %d:%d:%d 0x%jx 0x%jx",
16359e37c8aSRuslan Bukin unit->iommu.unit, busno, pci_get_slot(ctx->context.tag->owner),
16459e37c8aSRuslan Bukin pci_get_function(ctx->context.tag->owner),
165685666aaSKonstantin Belousov ctxp->ctx1, ctxp->ctx2));
166685666aaSKonstantin Belousov
16715f6baf4SRuslan Bukin if ((domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 &&
16886be9f0dSKonstantin Belousov (unit->hw_ecap & DMAR_ECAP_PT) != 0) {
1691abfd355SKonstantin Belousov KASSERT(domain->pgtbl_obj == NULL,
17086be9f0dSKonstantin Belousov ("ctx %p non-null pgtbl_obj", ctx));
171685666aaSKonstantin Belousov ctx_root = NULL;
17286be9f0dSKonstantin Belousov } else {
17340d951bcSKonstantin Belousov ctx_root = iommu_pgalloc(domain->pgtbl_obj, 0,
17415f6baf4SRuslan Bukin IOMMU_PGF_NOALLOC);
175685666aaSKonstantin Belousov }
176685666aaSKonstantin Belousov
17778b51754SRuslan Bukin if (iommu_is_buswide_ctx(DMAR2IOMMU(unit), busno)) {
178685666aaSKonstantin Belousov MPASS(!move);
179685666aaSKonstantin Belousov for (i = 0; i <= PCI_BUSMAX; i++) {
180685666aaSKonstantin Belousov ctx_id_entry_init_one(&ctxp[i], domain, ctx_root);
181685666aaSKonstantin Belousov }
182685666aaSKonstantin Belousov } else {
183685666aaSKonstantin Belousov ctx_id_entry_init_one(ctxp, domain, ctx_root);
18486be9f0dSKonstantin Belousov }
1856b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(unit, ctxp);
18686be9f0dSKonstantin Belousov }
18786be9f0dSKonstantin Belousov
18886be9f0dSKonstantin Belousov static int
dmar_flush_for_ctx_entry(struct dmar_unit * dmar,bool force)1891abfd355SKonstantin Belousov dmar_flush_for_ctx_entry(struct dmar_unit *dmar, bool force)
1901abfd355SKonstantin Belousov {
1911abfd355SKonstantin Belousov int error;
1921abfd355SKonstantin Belousov
1931abfd355SKonstantin Belousov /*
1941abfd355SKonstantin Belousov * If dmar declares Caching Mode as Set, follow 11.5 "Caching
1951abfd355SKonstantin Belousov * Mode Consideration" and do the (global) invalidation of the
1961abfd355SKonstantin Belousov * negative TLB entries.
1971abfd355SKonstantin Belousov */
1981abfd355SKonstantin Belousov if ((dmar->hw_cap & DMAR_CAP_CM) == 0 && !force)
1991abfd355SKonstantin Belousov return (0);
2001abfd355SKonstantin Belousov if (dmar->qi_enabled) {
2011abfd355SKonstantin Belousov dmar_qi_invalidate_ctx_glob_locked(dmar);
2021abfd355SKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force)
2031abfd355SKonstantin Belousov dmar_qi_invalidate_iotlb_glob_locked(dmar);
2041abfd355SKonstantin Belousov return (0);
2051abfd355SKonstantin Belousov }
2061abfd355SKonstantin Belousov error = dmar_inv_ctx_glob(dmar);
2071abfd355SKonstantin Belousov if (error == 0 && ((dmar->hw_ecap & DMAR_ECAP_DI) != 0 || force))
2081abfd355SKonstantin Belousov error = dmar_inv_iotlb_glob(dmar);
2091abfd355SKonstantin Belousov return (error);
2101abfd355SKonstantin Belousov }
2111abfd355SKonstantin Belousov
2121abfd355SKonstantin Belousov static int
domain_init_rmrr(struct dmar_domain * domain,device_t dev,int bus,int slot,int func,int dev_domain,int dev_busno,const void * dev_path,int dev_path_len)213f9feb091SKonstantin Belousov domain_init_rmrr(struct dmar_domain *domain, device_t dev, int bus,
214f9feb091SKonstantin Belousov int slot, int func, int dev_domain, int dev_busno,
215f9feb091SKonstantin Belousov const void *dev_path, int dev_path_len)
21686be9f0dSKonstantin Belousov {
21759e37c8aSRuslan Bukin struct iommu_map_entries_tailq rmrr_entries;
21859e37c8aSRuslan Bukin struct iommu_map_entry *entry, *entry1;
21986be9f0dSKonstantin Belousov vm_page_t *ma;
22059e37c8aSRuslan Bukin iommu_gaddr_t start, end;
22186be9f0dSKonstantin Belousov vm_pindex_t size, i;
22286be9f0dSKonstantin Belousov int error, error1;
22386be9f0dSKonstantin Belousov
22424e38af6SKonstantin Belousov if (!dmar_rmrr_enable)
22524e38af6SKonstantin Belousov return (0);
22624e38af6SKonstantin Belousov
22786be9f0dSKonstantin Belousov error = 0;
22886be9f0dSKonstantin Belousov TAILQ_INIT(&rmrr_entries);
229f9feb091SKonstantin Belousov dmar_dev_parse_rmrr(domain, dev_domain, dev_busno, dev_path,
230f9feb091SKonstantin Belousov dev_path_len, &rmrr_entries);
231db0110a5SAlan Cox TAILQ_FOREACH_SAFE(entry, &rmrr_entries, dmamap_link, entry1) {
23286be9f0dSKonstantin Belousov /*
23386be9f0dSKonstantin Belousov * VT-d specification requires that the start of an
23486be9f0dSKonstantin Belousov * RMRR entry is 4k-aligned. Buggy BIOSes put
23586be9f0dSKonstantin Belousov * anything into the start and end fields. Truncate
23686be9f0dSKonstantin Belousov * and round as neccesary.
23786be9f0dSKonstantin Belousov *
23886be9f0dSKonstantin Belousov * We also allow the overlapping RMRR entries, see
23962ad310cSRuslan Bukin * iommu_gas_alloc_region().
24086be9f0dSKonstantin Belousov */
24186be9f0dSKonstantin Belousov start = entry->start;
24286be9f0dSKonstantin Belousov end = entry->end;
243f9feb091SKonstantin Belousov if (bootverbose)
244f9feb091SKonstantin Belousov printf("dmar%d ctx pci%d:%d:%d RMRR [%#jx, %#jx]\n",
24559e37c8aSRuslan Bukin domain->iodom.iommu->unit, bus, slot, func,
246f9feb091SKonstantin Belousov (uintmax_t)start, (uintmax_t)end);
24786be9f0dSKonstantin Belousov entry->start = trunc_page(start);
24886be9f0dSKonstantin Belousov entry->end = round_page(end);
249e02b05b3SKonstantin Belousov if (entry->start == entry->end) {
250e02b05b3SKonstantin Belousov /* Workaround for some AMI (?) BIOSes */
251e02b05b3SKonstantin Belousov if (bootverbose) {
252f9feb091SKonstantin Belousov if (dev != NULL)
253f9feb091SKonstantin Belousov device_printf(dev, "");
254f9feb091SKonstantin Belousov printf("pci%d:%d:%d ", bus, slot, func);
255f9feb091SKonstantin Belousov printf("BIOS bug: dmar%d RMRR "
256e02b05b3SKonstantin Belousov "region (%jx, %jx) corrected\n",
25759e37c8aSRuslan Bukin domain->iodom.iommu->unit, start, end);
258e02b05b3SKonstantin Belousov }
25940d951bcSKonstantin Belousov entry->end += IOMMU_PAGE_SIZE * 0x20;
260e02b05b3SKonstantin Belousov }
26186be9f0dSKonstantin Belousov size = OFF_TO_IDX(entry->end - entry->start);
26286be9f0dSKonstantin Belousov ma = malloc(sizeof(vm_page_t) * size, M_TEMP, M_WAITOK);
26386be9f0dSKonstantin Belousov for (i = 0; i < size; i++) {
26486be9f0dSKonstantin Belousov ma[i] = vm_page_getfake(entry->start + PAGE_SIZE * i,
26586be9f0dSKonstantin Belousov VM_MEMATTR_DEFAULT);
26686be9f0dSKonstantin Belousov }
26778b51754SRuslan Bukin error1 = iommu_gas_map_region(DOM2IODOM(domain), entry,
26859e37c8aSRuslan Bukin IOMMU_MAP_ENTRY_READ | IOMMU_MAP_ENTRY_WRITE,
26959e37c8aSRuslan Bukin IOMMU_MF_CANWAIT | IOMMU_MF_RMRR, ma);
27086be9f0dSKonstantin Belousov /*
27186be9f0dSKonstantin Belousov * Non-failed RMRR entries are owned by context rb
27286be9f0dSKonstantin Belousov * tree. Get rid of the failed entry, but do not stop
27386be9f0dSKonstantin Belousov * the loop. Rest of the parsed RMRR entries are
27486be9f0dSKonstantin Belousov * loaded and removed on the context destruction.
27586be9f0dSKonstantin Belousov */
27686be9f0dSKonstantin Belousov if (error1 == 0 && entry->end != entry->start) {
27759e37c8aSRuslan Bukin IOMMU_LOCK(domain->iodom.iommu);
2781abfd355SKonstantin Belousov domain->refs++; /* XXXKIB prevent free */
27915f6baf4SRuslan Bukin domain->iodom.flags |= IOMMU_DOMAIN_RMRR;
28059e37c8aSRuslan Bukin IOMMU_UNLOCK(domain->iodom.iommu);
28186be9f0dSKonstantin Belousov } else {
28286be9f0dSKonstantin Belousov if (error1 != 0) {
283f9feb091SKonstantin Belousov if (dev != NULL)
284f9feb091SKonstantin Belousov device_printf(dev, "");
285f9feb091SKonstantin Belousov printf("pci%d:%d:%d ", bus, slot, func);
286f9feb091SKonstantin Belousov printf(
28786be9f0dSKonstantin Belousov "dmar%d failed to map RMRR region (%jx, %jx) %d\n",
28859e37c8aSRuslan Bukin domain->iodom.iommu->unit, start, end,
289f9feb091SKonstantin Belousov error1);
29086be9f0dSKonstantin Belousov error = error1;
29186be9f0dSKonstantin Belousov }
292db0110a5SAlan Cox TAILQ_REMOVE(&rmrr_entries, entry, dmamap_link);
2934670f908SAlan Cox iommu_gas_free_entry(entry);
29486be9f0dSKonstantin Belousov }
29586be9f0dSKonstantin Belousov for (i = 0; i < size; i++)
29686be9f0dSKonstantin Belousov vm_page_putfake(ma[i]);
29786be9f0dSKonstantin Belousov free(ma, M_TEMP);
29886be9f0dSKonstantin Belousov }
29986be9f0dSKonstantin Belousov return (error);
30086be9f0dSKonstantin Belousov }
30186be9f0dSKonstantin Belousov
302ee47a12aSRyan Libby /*
303ee47a12aSRyan Libby * PCI memory address space is shared between memory-mapped devices (MMIO) and
304ee47a12aSRyan Libby * host memory (which may be remapped by an IOMMU). Device accesses to an
305ee47a12aSRyan Libby * address within a memory aperture in a PCIe root port will be treated as
306ee47a12aSRyan Libby * peer-to-peer and not forwarded to an IOMMU. To avoid this, reserve the
307ee47a12aSRyan Libby * address space of the root port's memory apertures in the address space used
308ee47a12aSRyan Libby * by the IOMMU for remapping.
309ee47a12aSRyan Libby */
310ee47a12aSRyan Libby static int
dmar_reserve_pci_regions(struct dmar_domain * domain,device_t dev)311ee47a12aSRyan Libby dmar_reserve_pci_regions(struct dmar_domain *domain, device_t dev)
312ee47a12aSRyan Libby {
313ee47a12aSRyan Libby struct iommu_domain *iodom;
314ee47a12aSRyan Libby device_t root;
315ee47a12aSRyan Libby uint32_t val;
316ee47a12aSRyan Libby uint64_t base, limit;
317ee47a12aSRyan Libby int error;
318ee47a12aSRyan Libby
319ee47a12aSRyan Libby iodom = DOM2IODOM(domain);
320ee47a12aSRyan Libby
321ee47a12aSRyan Libby root = pci_find_pcie_root_port(dev);
322ee47a12aSRyan Libby if (root == NULL)
323ee47a12aSRyan Libby return (0);
324ee47a12aSRyan Libby
325ee47a12aSRyan Libby /* Disable downstream memory */
326ee47a12aSRyan Libby base = PCI_PPBMEMBASE(0, pci_read_config(root, PCIR_MEMBASE_1, 2));
327ee47a12aSRyan Libby limit = PCI_PPBMEMLIMIT(0, pci_read_config(root, PCIR_MEMLIMIT_1, 2));
328ee47a12aSRyan Libby error = iommu_gas_reserve_region_extend(iodom, base, limit + 1);
329ee47a12aSRyan Libby if (bootverbose || error != 0)
330ee47a12aSRyan Libby device_printf(dev, "DMAR reserve [%#jx-%#jx] (error %d)\n",
331ee47a12aSRyan Libby base, limit + 1, error);
332ee47a12aSRyan Libby if (error != 0)
333ee47a12aSRyan Libby return (error);
334ee47a12aSRyan Libby
335ee47a12aSRyan Libby /* Disable downstream prefetchable memory */
336ee47a12aSRyan Libby val = pci_read_config(root, PCIR_PMBASEL_1, 2);
337ee47a12aSRyan Libby if (val != 0 || pci_read_config(root, PCIR_PMLIMITL_1, 2) != 0) {
338ee47a12aSRyan Libby if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
339ee47a12aSRyan Libby base = PCI_PPBMEMBASE(
340ee47a12aSRyan Libby pci_read_config(root, PCIR_PMBASEH_1, 4),
341ee47a12aSRyan Libby val);
342ee47a12aSRyan Libby limit = PCI_PPBMEMLIMIT(
343ee47a12aSRyan Libby pci_read_config(root, PCIR_PMLIMITH_1, 4),
344ee47a12aSRyan Libby pci_read_config(root, PCIR_PMLIMITL_1, 2));
345ee47a12aSRyan Libby } else {
346ee47a12aSRyan Libby base = PCI_PPBMEMBASE(0, val);
347ee47a12aSRyan Libby limit = PCI_PPBMEMLIMIT(0,
348ee47a12aSRyan Libby pci_read_config(root, PCIR_PMLIMITL_1, 2));
349ee47a12aSRyan Libby }
350ee47a12aSRyan Libby error = iommu_gas_reserve_region_extend(iodom, base,
351ee47a12aSRyan Libby limit + 1);
352ee47a12aSRyan Libby if (bootverbose || error != 0)
353ee47a12aSRyan Libby device_printf(dev, "DMAR reserve [%#jx-%#jx] "
354ee47a12aSRyan Libby "(error %d)\n", base, limit + 1, error);
355ee47a12aSRyan Libby if (error != 0)
356ee47a12aSRyan Libby return (error);
357ee47a12aSRyan Libby }
358ee47a12aSRyan Libby
359ee47a12aSRyan Libby return (error);
360ee47a12aSRyan Libby }
361ee47a12aSRyan Libby
3621abfd355SKonstantin Belousov static struct dmar_domain *
dmar_domain_alloc(struct dmar_unit * dmar,bool id_mapped)3631abfd355SKonstantin Belousov dmar_domain_alloc(struct dmar_unit *dmar, bool id_mapped)
3641abfd355SKonstantin Belousov {
36562ad310cSRuslan Bukin struct iommu_domain *iodom;
36616696f60SRuslan Bukin struct iommu_unit *unit;
3671abfd355SKonstantin Belousov struct dmar_domain *domain;
3681abfd355SKonstantin Belousov int error, id, mgaw;
3691abfd355SKonstantin Belousov
3701abfd355SKonstantin Belousov id = alloc_unr(dmar->domids);
3711abfd355SKonstantin Belousov if (id == -1)
3721abfd355SKonstantin Belousov return (NULL);
3731abfd355SKonstantin Belousov domain = malloc(sizeof(*domain), M_DMAR_DOMAIN, M_WAITOK | M_ZERO);
37478b51754SRuslan Bukin iodom = DOM2IODOM(domain);
37516696f60SRuslan Bukin unit = DMAR2IOMMU(dmar);
3761abfd355SKonstantin Belousov domain->domain = id;
377*e9d948cfSKonstantin Belousov LIST_INIT(&iodom->contexts);
37816696f60SRuslan Bukin iommu_domain_init(unit, iodom, &dmar_domain_map_ops);
37916696f60SRuslan Bukin
3801abfd355SKonstantin Belousov domain->dmar = dmar;
3811abfd355SKonstantin Belousov
3821abfd355SKonstantin Belousov /*
3831abfd355SKonstantin Belousov * For now, use the maximal usable physical address of the
3841abfd355SKonstantin Belousov * installed memory to calculate the mgaw on id_mapped domain.
3851abfd355SKonstantin Belousov * It is useful for the identity mapping, and less so for the
3861abfd355SKonstantin Belousov * virtualized bus address space.
3871abfd355SKonstantin Belousov */
38862ad310cSRuslan Bukin domain->iodom.end = id_mapped ? ptoa(Maxmem) : BUS_SPACE_MAXADDR;
38962ad310cSRuslan Bukin mgaw = dmar_maxaddr2mgaw(dmar, domain->iodom.end, !id_mapped);
3901abfd355SKonstantin Belousov error = domain_set_agaw(domain, mgaw);
3911abfd355SKonstantin Belousov if (error != 0)
3921abfd355SKonstantin Belousov goto fail;
3931abfd355SKonstantin Belousov if (!id_mapped)
3941abfd355SKonstantin Belousov /* Use all supported address space for remapping. */
39562ad310cSRuslan Bukin domain->iodom.end = 1ULL << (domain->agaw - 1);
3961abfd355SKonstantin Belousov
39778b51754SRuslan Bukin iommu_gas_init_domain(DOM2IODOM(domain));
3981abfd355SKonstantin Belousov
3991abfd355SKonstantin Belousov if (id_mapped) {
4001abfd355SKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_PT) == 0) {
40110ebd230SKonstantin Belousov domain->pgtbl_obj = dmar_get_idmap_pgtbl(domain,
40262ad310cSRuslan Bukin domain->iodom.end);
4031abfd355SKonstantin Belousov }
40415f6baf4SRuslan Bukin domain->iodom.flags |= IOMMU_DOMAIN_IDMAP;
4051abfd355SKonstantin Belousov } else {
40623145534SKonstantin Belousov error = dmar_domain_alloc_pgtbl(domain);
4071abfd355SKonstantin Belousov if (error != 0)
4081abfd355SKonstantin Belousov goto fail;
4091abfd355SKonstantin Belousov /* Disable local apic region access */
41062ad310cSRuslan Bukin error = iommu_gas_reserve_region(iodom, 0xfee00000,
41194dfb28eSRuslan Bukin 0xfeefffff + 1, &iodom->msi_entry);
4121abfd355SKonstantin Belousov if (error != 0)
4131abfd355SKonstantin Belousov goto fail;
4141abfd355SKonstantin Belousov }
4151abfd355SKonstantin Belousov return (domain);
4161abfd355SKonstantin Belousov
4171abfd355SKonstantin Belousov fail:
4181abfd355SKonstantin Belousov dmar_domain_destroy(domain);
4191abfd355SKonstantin Belousov return (NULL);
4201abfd355SKonstantin Belousov }
4211abfd355SKonstantin Belousov
42286be9f0dSKonstantin Belousov static struct dmar_ctx *
dmar_ctx_alloc(struct dmar_domain * domain,uint16_t rid)4231abfd355SKonstantin Belousov dmar_ctx_alloc(struct dmar_domain *domain, uint16_t rid)
42486be9f0dSKonstantin Belousov {
42586be9f0dSKonstantin Belousov struct dmar_ctx *ctx;
42686be9f0dSKonstantin Belousov
42786be9f0dSKonstantin Belousov ctx = malloc(sizeof(*ctx), M_DMAR_CTX, M_WAITOK | M_ZERO);
42878b51754SRuslan Bukin ctx->context.domain = DOM2IODOM(domain);
42959e37c8aSRuslan Bukin ctx->context.tag = malloc(sizeof(struct bus_dma_tag_iommu),
43059e37c8aSRuslan Bukin M_DMAR_CTX, M_WAITOK | M_ZERO);
431cb9050ddSRuslan Bukin ctx->context.rid = rid;
432*e9d948cfSKonstantin Belousov ctx->context.refs = 1;
43386be9f0dSKonstantin Belousov return (ctx);
43486be9f0dSKonstantin Belousov }
43586be9f0dSKonstantin Belousov
43686be9f0dSKonstantin Belousov static void
dmar_ctx_link(struct dmar_ctx * ctx)4371abfd355SKonstantin Belousov dmar_ctx_link(struct dmar_ctx *ctx)
4381abfd355SKonstantin Belousov {
4391abfd355SKonstantin Belousov struct dmar_domain *domain;
4401abfd355SKonstantin Belousov
44178b51754SRuslan Bukin domain = CTX2DOM(ctx);
44259e37c8aSRuslan Bukin IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4431abfd355SKonstantin Belousov KASSERT(domain->refs >= domain->ctx_cnt,
4441abfd355SKonstantin Belousov ("dom %p ref underflow %d %d", domain, domain->refs,
4451abfd355SKonstantin Belousov domain->ctx_cnt));
4461abfd355SKonstantin Belousov domain->refs++;
4471abfd355SKonstantin Belousov domain->ctx_cnt++;
448*e9d948cfSKonstantin Belousov LIST_INSERT_HEAD(&domain->iodom.contexts, &ctx->context, link);
4491abfd355SKonstantin Belousov }
4501abfd355SKonstantin Belousov
4511abfd355SKonstantin Belousov static void
dmar_ctx_unlink(struct dmar_ctx * ctx)4521abfd355SKonstantin Belousov dmar_ctx_unlink(struct dmar_ctx *ctx)
4531abfd355SKonstantin Belousov {
4541abfd355SKonstantin Belousov struct dmar_domain *domain;
4551abfd355SKonstantin Belousov
45678b51754SRuslan Bukin domain = CTX2DOM(ctx);
45759e37c8aSRuslan Bukin IOMMU_ASSERT_LOCKED(domain->iodom.iommu);
4581abfd355SKonstantin Belousov KASSERT(domain->refs > 0,
4591abfd355SKonstantin Belousov ("domain %p ctx dtr refs %d", domain, domain->refs));
4601abfd355SKonstantin Belousov KASSERT(domain->ctx_cnt >= domain->refs,
4611abfd355SKonstantin Belousov ("domain %p ctx dtr refs %d ctx_cnt %d", domain,
4621abfd355SKonstantin Belousov domain->refs, domain->ctx_cnt));
4631abfd355SKonstantin Belousov domain->refs--;
4641abfd355SKonstantin Belousov domain->ctx_cnt--;
465*e9d948cfSKonstantin Belousov LIST_REMOVE(&ctx->context, link);
4661abfd355SKonstantin Belousov }
4671abfd355SKonstantin Belousov
4681abfd355SKonstantin Belousov static void
dmar_domain_destroy(struct dmar_domain * domain)4691abfd355SKonstantin Belousov dmar_domain_destroy(struct dmar_domain *domain)
47086be9f0dSKonstantin Belousov {
47116696f60SRuslan Bukin struct iommu_domain *iodom;
47259e37c8aSRuslan Bukin struct dmar_unit *dmar;
47386be9f0dSKonstantin Belousov
47416696f60SRuslan Bukin iodom = DOM2IODOM(domain);
47516696f60SRuslan Bukin
47659e37c8aSRuslan Bukin KASSERT(TAILQ_EMPTY(&domain->iodom.unload_entries),
4771abfd355SKonstantin Belousov ("unfinished unloads %p", domain));
478*e9d948cfSKonstantin Belousov KASSERT(LIST_EMPTY(&iodom->contexts),
4791abfd355SKonstantin Belousov ("destroying dom %p with contexts", domain));
4801abfd355SKonstantin Belousov KASSERT(domain->ctx_cnt == 0,
4811abfd355SKonstantin Belousov ("destroying dom %p with ctx_cnt %d", domain, domain->ctx_cnt));
4821abfd355SKonstantin Belousov KASSERT(domain->refs == 0,
4831abfd355SKonstantin Belousov ("destroying dom %p with refs %d", domain, domain->refs));
48415f6baf4SRuslan Bukin if ((domain->iodom.flags & IOMMU_DOMAIN_GAS_INITED) != 0) {
4851abfd355SKonstantin Belousov DMAR_DOMAIN_LOCK(domain);
48616696f60SRuslan Bukin iommu_gas_fini_domain(iodom);
4871abfd355SKonstantin Belousov DMAR_DOMAIN_UNLOCK(domain);
48886be9f0dSKonstantin Belousov }
48915f6baf4SRuslan Bukin if ((domain->iodom.flags & IOMMU_DOMAIN_PGTBL_INITED) != 0) {
4901abfd355SKonstantin Belousov if (domain->pgtbl_obj != NULL)
4911abfd355SKonstantin Belousov DMAR_DOMAIN_PGLOCK(domain);
49223145534SKonstantin Belousov dmar_domain_free_pgtbl(domain);
49386be9f0dSKonstantin Belousov }
49416696f60SRuslan Bukin iommu_domain_fini(iodom);
49578b51754SRuslan Bukin dmar = DOM2DMAR(domain);
49659e37c8aSRuslan Bukin free_unr(dmar->domids, domain->domain);
4971abfd355SKonstantin Belousov free(domain, M_DMAR_DOMAIN);
49886be9f0dSKonstantin Belousov }
49986be9f0dSKonstantin Belousov
500f9feb091SKonstantin Belousov static struct dmar_ctx *
dmar_get_ctx_for_dev1(struct dmar_unit * dmar,device_t dev,uint16_t rid,int dev_domain,int dev_busno,const void * dev_path,int dev_path_len,bool id_mapped,bool rmrr_init)501f9feb091SKonstantin Belousov dmar_get_ctx_for_dev1(struct dmar_unit *dmar, device_t dev, uint16_t rid,
502f9feb091SKonstantin Belousov int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
5031abfd355SKonstantin Belousov bool id_mapped, bool rmrr_init)
50486be9f0dSKonstantin Belousov {
5051abfd355SKonstantin Belousov struct dmar_domain *domain, *domain1;
50686be9f0dSKonstantin Belousov struct dmar_ctx *ctx, *ctx1;
507661bd70bSKonstantin Belousov struct iommu_unit *unit __diagused;
50886be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp;
50986be9f0dSKonstantin Belousov struct sf_buf *sf;
5101abfd355SKonstantin Belousov int bus, slot, func, error;
51186be9f0dSKonstantin Belousov bool enable;
51286be9f0dSKonstantin Belousov
513f9feb091SKonstantin Belousov if (dev != NULL) {
51467499354SRyan Stone bus = pci_get_bus(dev);
51567499354SRyan Stone slot = pci_get_slot(dev);
51667499354SRyan Stone func = pci_get_function(dev);
517f9feb091SKonstantin Belousov } else {
518f9feb091SKonstantin Belousov bus = PCI_RID2BUS(rid);
519f9feb091SKonstantin Belousov slot = PCI_RID2SLOT(rid);
520f9feb091SKonstantin Belousov func = PCI_RID2FUNC(rid);
521f9feb091SKonstantin Belousov }
52286be9f0dSKonstantin Belousov enable = false;
52386be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT;
52478b51754SRuslan Bukin unit = DMAR2IOMMU(dmar);
52586be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
526ea4c0115SRuslan Bukin KASSERT(!iommu_is_buswide_ctx(unit, bus) || (slot == 0 && func == 0),
527ea4c0115SRuslan Bukin ("iommu%d pci%d:%d:%d get_ctx for buswide", dmar->iommu.unit, bus,
528685666aaSKonstantin Belousov slot, func));
52967499354SRyan Stone ctx = dmar_find_ctx_locked(dmar, rid);
53086be9f0dSKonstantin Belousov error = 0;
53186be9f0dSKonstantin Belousov if (ctx == NULL) {
53286be9f0dSKonstantin Belousov /*
53386be9f0dSKonstantin Belousov * Perform the allocations which require sleep or have
53486be9f0dSKonstantin Belousov * higher chance to succeed if the sleep is allowed.
53586be9f0dSKonstantin Belousov */
53686be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
537b29d186cSKonstantin Belousov dmar_ensure_ctx_page(dmar, PCI_RID2BUS(rid));
5381abfd355SKonstantin Belousov domain1 = dmar_domain_alloc(dmar, id_mapped);
5391abfd355SKonstantin Belousov if (domain1 == NULL) {
54086be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
54186be9f0dSKonstantin Belousov return (NULL);
54286be9f0dSKonstantin Belousov }
5435f8e5c7fSKonstantin Belousov if (!id_mapped) {
544f9feb091SKonstantin Belousov error = domain_init_rmrr(domain1, dev, bus,
545f9feb091SKonstantin Belousov slot, func, dev_domain, dev_busno, dev_path,
546f9feb091SKonstantin Belousov dev_path_len);
5473c02da80SKornel Duleba if (error == 0 && dev != NULL)
548ee47a12aSRyan Libby error = dmar_reserve_pci_regions(domain1, dev);
54986be9f0dSKonstantin Belousov if (error != 0) {
5501abfd355SKonstantin Belousov dmar_domain_destroy(domain1);
55186be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
55286be9f0dSKonstantin Belousov return (NULL);
55386be9f0dSKonstantin Belousov }
5545f8e5c7fSKonstantin Belousov }
5551abfd355SKonstantin Belousov ctx1 = dmar_ctx_alloc(domain1, rid);
55686be9f0dSKonstantin Belousov ctxp = dmar_map_ctx_entry(ctx1, &sf);
55786be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
55886be9f0dSKonstantin Belousov
55986be9f0dSKonstantin Belousov /*
56086be9f0dSKonstantin Belousov * Recheck the contexts, other thread might have
56186be9f0dSKonstantin Belousov * already allocated needed one.
56286be9f0dSKonstantin Belousov */
56367499354SRyan Stone ctx = dmar_find_ctx_locked(dmar, rid);
56486be9f0dSKonstantin Belousov if (ctx == NULL) {
5651abfd355SKonstantin Belousov domain = domain1;
56686be9f0dSKonstantin Belousov ctx = ctx1;
5671abfd355SKonstantin Belousov dmar_ctx_link(ctx);
56859e37c8aSRuslan Bukin ctx->context.tag->owner = dev;
569ff54674bSKonstantin Belousov iommu_device_tag_init(CTX2IOCTX(ctx), dev);
57086be9f0dSKonstantin Belousov
57186be9f0dSKonstantin Belousov /*
57286be9f0dSKonstantin Belousov * This is the first activated context for the
57386be9f0dSKonstantin Belousov * DMAR unit. Enable the translation after
57486be9f0dSKonstantin Belousov * everything is set up.
57586be9f0dSKonstantin Belousov */
5761abfd355SKonstantin Belousov if (LIST_EMPTY(&dmar->domains))
57786be9f0dSKonstantin Belousov enable = true;
5781abfd355SKonstantin Belousov LIST_INSERT_HEAD(&dmar->domains, domain, link);
579685666aaSKonstantin Belousov ctx_id_entry_init(ctx, ctxp, false, bus);
580f9feb091SKonstantin Belousov if (dev != NULL) {
58186be9f0dSKonstantin Belousov device_printf(dev,
58234e8337bSKonstantin Belousov "dmar%d pci%d:%d:%d:%d rid %x domain %d mgaw %d "
5839d0bc6d8SKonstantin Belousov "agaw %d %s-mapped\n",
58459e37c8aSRuslan Bukin dmar->iommu.unit, dmar->segment, bus, slot,
5851abfd355SKonstantin Belousov func, rid, domain->domain, domain->mgaw,
5861abfd355SKonstantin Belousov domain->agaw, id_mapped ? "id" : "re");
587f9feb091SKonstantin Belousov }
58840d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
58986be9f0dSKonstantin Belousov } else {
59040d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
5911abfd355SKonstantin Belousov dmar_domain_destroy(domain1);
5923d47c58bSKonstantin Belousov /* Nothing needs to be done to destroy ctx1. */
5933d47c58bSKonstantin Belousov free(ctx1, M_DMAR_CTX);
59478b51754SRuslan Bukin domain = CTX2DOM(ctx);
595*e9d948cfSKonstantin Belousov ctx->context.refs++; /* tag referenced us */
59686be9f0dSKonstantin Belousov }
59768eeb96aSKonstantin Belousov } else {
59878b51754SRuslan Bukin domain = CTX2DOM(ctx);
59959e37c8aSRuslan Bukin if (ctx->context.tag->owner == NULL)
60059e37c8aSRuslan Bukin ctx->context.tag->owner = dev;
601*e9d948cfSKonstantin Belousov ctx->context.refs++; /* tag referenced us */
6021abfd355SKonstantin Belousov }
6031abfd355SKonstantin Belousov
6041abfd355SKonstantin Belousov error = dmar_flush_for_ctx_entry(dmar, enable);
60586be9f0dSKonstantin Belousov if (error != 0) {
60686be9f0dSKonstantin Belousov dmar_free_ctx_locked(dmar, ctx);
60786be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
60886be9f0dSKonstantin Belousov return (NULL);
60986be9f0dSKonstantin Belousov }
61068eeb96aSKonstantin Belousov
61168eeb96aSKonstantin Belousov /*
61268eeb96aSKonstantin Belousov * The dmar lock was potentially dropped between check for the
61368eeb96aSKonstantin Belousov * empty context list and now. Recheck the state of GCMD_TE
61468eeb96aSKonstantin Belousov * to avoid unneeded command.
61568eeb96aSKonstantin Belousov */
61668eeb96aSKonstantin Belousov if (enable && !rmrr_init && (dmar->hw_gcmd & DMAR_GCMD_TE) == 0) {
61706e6ca6dSKornel Duleba error = dmar_disable_protected_regions(dmar);
61806e6ca6dSKornel Duleba if (error != 0)
61906e6ca6dSKornel Duleba printf("dmar%d: Failed to disable protected regions\n",
62006e6ca6dSKornel Duleba dmar->iommu.unit);
62186be9f0dSKonstantin Belousov error = dmar_enable_translation(dmar);
622f9feb091SKonstantin Belousov if (error == 0) {
623f9feb091SKonstantin Belousov if (bootverbose) {
624f9feb091SKonstantin Belousov printf("dmar%d: enabled translation\n",
62559e37c8aSRuslan Bukin dmar->iommu.unit);
626f9feb091SKonstantin Belousov }
627f9feb091SKonstantin Belousov } else {
628f9feb091SKonstantin Belousov printf("dmar%d: enabling translation failed, "
62959e37c8aSRuslan Bukin "error %d\n", dmar->iommu.unit, error);
63086be9f0dSKonstantin Belousov dmar_free_ctx_locked(dmar, ctx);
63186be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
63286be9f0dSKonstantin Belousov return (NULL);
63386be9f0dSKonstantin Belousov }
63486be9f0dSKonstantin Belousov }
63586be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
63686be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
63786be9f0dSKonstantin Belousov return (ctx);
63886be9f0dSKonstantin Belousov }
63986be9f0dSKonstantin Belousov
640f9feb091SKonstantin Belousov struct dmar_ctx *
dmar_get_ctx_for_dev(struct dmar_unit * dmar,device_t dev,uint16_t rid,bool id_mapped,bool rmrr_init)641f9feb091SKonstantin Belousov dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev, uint16_t rid,
642f9feb091SKonstantin Belousov bool id_mapped, bool rmrr_init)
643f9feb091SKonstantin Belousov {
644f9feb091SKonstantin Belousov int dev_domain, dev_path_len, dev_busno;
645f9feb091SKonstantin Belousov
646f9feb091SKonstantin Belousov dev_domain = pci_get_domain(dev);
647f9feb091SKonstantin Belousov dev_path_len = dmar_dev_depth(dev);
648f9feb091SKonstantin Belousov ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
649f9feb091SKonstantin Belousov dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
650f9feb091SKonstantin Belousov return (dmar_get_ctx_for_dev1(dmar, dev, rid, dev_domain, dev_busno,
651f9feb091SKonstantin Belousov dev_path, dev_path_len, id_mapped, rmrr_init));
652f9feb091SKonstantin Belousov }
653f9feb091SKonstantin Belousov
654f9feb091SKonstantin Belousov struct dmar_ctx *
dmar_get_ctx_for_devpath(struct dmar_unit * dmar,uint16_t rid,int dev_domain,int dev_busno,const void * dev_path,int dev_path_len,bool id_mapped,bool rmrr_init)655f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
656f9feb091SKonstantin Belousov int dev_domain, int dev_busno,
657f9feb091SKonstantin Belousov const void *dev_path, int dev_path_len,
658f9feb091SKonstantin Belousov bool id_mapped, bool rmrr_init)
659f9feb091SKonstantin Belousov {
660f9feb091SKonstantin Belousov
661f9feb091SKonstantin Belousov return (dmar_get_ctx_for_dev1(dmar, NULL, rid, dev_domain, dev_busno,
662f9feb091SKonstantin Belousov dev_path, dev_path_len, id_mapped, rmrr_init));
663f9feb091SKonstantin Belousov }
664f9feb091SKonstantin Belousov
6651abfd355SKonstantin Belousov int
dmar_move_ctx_to_domain(struct dmar_domain * domain,struct dmar_ctx * ctx)6661abfd355SKonstantin Belousov dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx)
6671abfd355SKonstantin Belousov {
6681abfd355SKonstantin Belousov struct dmar_unit *dmar;
6691abfd355SKonstantin Belousov struct dmar_domain *old_domain;
6701abfd355SKonstantin Belousov dmar_ctx_entry_t *ctxp;
6711abfd355SKonstantin Belousov struct sf_buf *sf;
6721abfd355SKonstantin Belousov int error;
6731abfd355SKonstantin Belousov
6741abfd355SKonstantin Belousov dmar = domain->dmar;
67578b51754SRuslan Bukin old_domain = CTX2DOM(ctx);
6761abfd355SKonstantin Belousov if (domain == old_domain)
6771abfd355SKonstantin Belousov return (0);
67859e37c8aSRuslan Bukin KASSERT(old_domain->iodom.iommu == domain->iodom.iommu,
6791abfd355SKonstantin Belousov ("domain %p %u moving between dmars %u %u", domain,
68059e37c8aSRuslan Bukin domain->domain, old_domain->iodom.iommu->unit,
68159e37c8aSRuslan Bukin domain->iodom.iommu->unit));
6821abfd355SKonstantin Belousov TD_PREP_PINNED_ASSERT;
6831abfd355SKonstantin Belousov
6841abfd355SKonstantin Belousov ctxp = dmar_map_ctx_entry(ctx, &sf);
6851abfd355SKonstantin Belousov DMAR_LOCK(dmar);
6861abfd355SKonstantin Belousov dmar_ctx_unlink(ctx);
68759e37c8aSRuslan Bukin ctx->context.domain = &domain->iodom;
6881abfd355SKonstantin Belousov dmar_ctx_link(ctx);
689685666aaSKonstantin Belousov ctx_id_entry_init(ctx, ctxp, true, PCI_BUSMAX + 100);
69040d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
6911abfd355SKonstantin Belousov error = dmar_flush_for_ctx_entry(dmar, true);
6921abfd355SKonstantin Belousov /* If flush failed, rolling back would not work as well. */
6931abfd355SKonstantin Belousov printf("dmar%d rid %x domain %d->%d %s-mapped\n",
694cb9050ddSRuslan Bukin dmar->iommu.unit, ctx->context.rid, old_domain->domain,
695cb9050ddSRuslan Bukin domain->domain, (domain->iodom.flags & IOMMU_DOMAIN_IDMAP) != 0 ?
696cb9050ddSRuslan Bukin "id" : "re");
6971abfd355SKonstantin Belousov dmar_unref_domain_locked(dmar, old_domain);
6981abfd355SKonstantin Belousov TD_PINNED_ASSERT;
6991abfd355SKonstantin Belousov return (error);
7001abfd355SKonstantin Belousov }
7011abfd355SKonstantin Belousov
7021abfd355SKonstantin Belousov static void
dmar_unref_domain_locked(struct dmar_unit * dmar,struct dmar_domain * domain)7031abfd355SKonstantin Belousov dmar_unref_domain_locked(struct dmar_unit *dmar, struct dmar_domain *domain)
7041abfd355SKonstantin Belousov {
7051abfd355SKonstantin Belousov
7061abfd355SKonstantin Belousov DMAR_ASSERT_LOCKED(dmar);
7071abfd355SKonstantin Belousov KASSERT(domain->refs >= 1,
70859e37c8aSRuslan Bukin ("dmar %d domain %p refs %u", dmar->iommu.unit, domain,
70959e37c8aSRuslan Bukin domain->refs));
7101abfd355SKonstantin Belousov KASSERT(domain->refs > domain->ctx_cnt,
71159e37c8aSRuslan Bukin ("dmar %d domain %p refs %d ctx_cnt %d", dmar->iommu.unit, domain,
7121abfd355SKonstantin Belousov domain->refs, domain->ctx_cnt));
7131abfd355SKonstantin Belousov
7141abfd355SKonstantin Belousov if (domain->refs > 1) {
7151abfd355SKonstantin Belousov domain->refs--;
7161abfd355SKonstantin Belousov DMAR_UNLOCK(dmar);
7171abfd355SKonstantin Belousov return;
7181abfd355SKonstantin Belousov }
7191abfd355SKonstantin Belousov
72015f6baf4SRuslan Bukin KASSERT((domain->iodom.flags & IOMMU_DOMAIN_RMRR) == 0,
7211abfd355SKonstantin Belousov ("lost ref on RMRR domain %p", domain));
7221abfd355SKonstantin Belousov
7231abfd355SKonstantin Belousov LIST_REMOVE(domain, link);
7241abfd355SKonstantin Belousov DMAR_UNLOCK(dmar);
7251abfd355SKonstantin Belousov
72659e37c8aSRuslan Bukin taskqueue_drain(dmar->iommu.delayed_taskqueue,
72759e37c8aSRuslan Bukin &domain->iodom.unload_task);
7281abfd355SKonstantin Belousov dmar_domain_destroy(domain);
7291abfd355SKonstantin Belousov }
7301abfd355SKonstantin Belousov
73165b133e5SKonstantin Belousov static void
dmar_free_ctx_locked(struct dmar_unit * dmar,struct dmar_ctx * ctx)73286be9f0dSKonstantin Belousov dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx)
73386be9f0dSKonstantin Belousov {
73486be9f0dSKonstantin Belousov struct sf_buf *sf;
73586be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp;
7361abfd355SKonstantin Belousov struct dmar_domain *domain;
73786be9f0dSKonstantin Belousov
73886be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar);
739*e9d948cfSKonstantin Belousov KASSERT(ctx->context.refs >= 1,
740*e9d948cfSKonstantin Belousov ("dmar %p ctx %p refs %u", dmar, ctx, ctx->context.refs));
74186be9f0dSKonstantin Belousov
74286be9f0dSKonstantin Belousov /*
74386be9f0dSKonstantin Belousov * If our reference is not last, only the dereference should
74486be9f0dSKonstantin Belousov * be performed.
74586be9f0dSKonstantin Belousov */
746*e9d948cfSKonstantin Belousov if (ctx->context.refs > 1) {
747*e9d948cfSKonstantin Belousov ctx->context.refs--;
74886be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
74986be9f0dSKonstantin Belousov return;
75086be9f0dSKonstantin Belousov }
75186be9f0dSKonstantin Belousov
75259e37c8aSRuslan Bukin KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
75386be9f0dSKonstantin Belousov ("lost ref on disabled ctx %p", ctx));
75486be9f0dSKonstantin Belousov
75586be9f0dSKonstantin Belousov /*
75686be9f0dSKonstantin Belousov * Otherwise, the context entry must be cleared before the
75786be9f0dSKonstantin Belousov * page table is destroyed. The mapping of the context
75886be9f0dSKonstantin Belousov * entries page could require sleep, unlock the dmar.
75986be9f0dSKonstantin Belousov */
76086be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
76186be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT;
76286be9f0dSKonstantin Belousov ctxp = dmar_map_ctx_entry(ctx, &sf);
76386be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
764*e9d948cfSKonstantin Belousov KASSERT(ctx->context.refs >= 1,
765*e9d948cfSKonstantin Belousov ("dmar %p ctx %p refs %u", dmar, ctx, ctx->context.refs));
76686be9f0dSKonstantin Belousov
76786be9f0dSKonstantin Belousov /*
76886be9f0dSKonstantin Belousov * Other thread might have referenced the context, in which
76986be9f0dSKonstantin Belousov * case again only the dereference should be performed.
77086be9f0dSKonstantin Belousov */
771*e9d948cfSKonstantin Belousov if (ctx->context.refs > 1) {
772*e9d948cfSKonstantin Belousov ctx->context.refs--;
77386be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
77440d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
77586be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
77686be9f0dSKonstantin Belousov return;
77786be9f0dSKonstantin Belousov }
77886be9f0dSKonstantin Belousov
77959e37c8aSRuslan Bukin KASSERT((ctx->context.flags & IOMMU_CTX_DISABLED) == 0,
78086be9f0dSKonstantin Belousov ("lost ref on disabled ctx %p", ctx));
78186be9f0dSKonstantin Belousov
78286be9f0dSKonstantin Belousov /*
78386be9f0dSKonstantin Belousov * Clear the context pointer and flush the caches.
78486be9f0dSKonstantin Belousov * XXXKIB: cannot do this if any RMRR entries are still present.
78586be9f0dSKonstantin Belousov */
78686be9f0dSKonstantin Belousov dmar_pte_clear(&ctxp->ctx1);
78786be9f0dSKonstantin Belousov ctxp->ctx2 = 0;
7886b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(dmar, ctxp);
78986be9f0dSKonstantin Belousov dmar_inv_ctx_glob(dmar);
79068eeb96aSKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) {
79168eeb96aSKonstantin Belousov if (dmar->qi_enabled)
79268eeb96aSKonstantin Belousov dmar_qi_invalidate_iotlb_glob_locked(dmar);
79368eeb96aSKonstantin Belousov else
79486be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(dmar);
79568eeb96aSKonstantin Belousov }
79640d951bcSKonstantin Belousov iommu_unmap_pgtbl(sf);
79778b51754SRuslan Bukin domain = CTX2DOM(ctx);
7981abfd355SKonstantin Belousov dmar_ctx_unlink(ctx);
79959e37c8aSRuslan Bukin free(ctx->context.tag, M_DMAR_CTX);
8001abfd355SKonstantin Belousov free(ctx, M_DMAR_CTX);
8011abfd355SKonstantin Belousov dmar_unref_domain_locked(dmar, domain);
80286be9f0dSKonstantin Belousov TD_PINNED_ASSERT;
80386be9f0dSKonstantin Belousov }
80486be9f0dSKonstantin Belousov
8051abfd355SKonstantin Belousov /*
8061abfd355SKonstantin Belousov * Returns with the domain locked.
8071abfd355SKonstantin Belousov */
80886be9f0dSKonstantin Belousov struct dmar_ctx *
dmar_find_ctx_locked(struct dmar_unit * dmar,uint16_t rid)80967499354SRyan Stone dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid)
81086be9f0dSKonstantin Belousov {
8111abfd355SKonstantin Belousov struct dmar_domain *domain;
812*e9d948cfSKonstantin Belousov struct iommu_ctx *ctx;
81386be9f0dSKonstantin Belousov
81486be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar);
81586be9f0dSKonstantin Belousov
8161abfd355SKonstantin Belousov LIST_FOREACH(domain, &dmar->domains, link) {
817*e9d948cfSKonstantin Belousov LIST_FOREACH(ctx, &domain->iodom.contexts, link) {
818*e9d948cfSKonstantin Belousov if (ctx->rid == rid)
819*e9d948cfSKonstantin Belousov return (IOCTX2CTX(ctx));
82086be9f0dSKonstantin Belousov }
8211abfd355SKonstantin Belousov }
82286be9f0dSKonstantin Belousov return (NULL);
82386be9f0dSKonstantin Belousov }
82486be9f0dSKonstantin Belousov
82542736dc4SAlan Cox /*
82642736dc4SAlan Cox * If the given value for "free" is true, then the caller must not be using
82742736dc4SAlan Cox * the entry's dmamap_link field.
82842736dc4SAlan Cox */
82968eeb96aSKonstantin Belousov void
dmar_domain_unload_entry(struct iommu_map_entry * entry,bool free,bool cansleep)83065b133e5SKonstantin Belousov dmar_domain_unload_entry(struct iommu_map_entry *entry, bool free,
8318bc36738SAlan Cox bool cansleep)
83268eeb96aSKonstantin Belousov {
83359e37c8aSRuslan Bukin struct dmar_domain *domain;
83468eeb96aSKonstantin Belousov struct dmar_unit *unit;
83568eeb96aSKonstantin Belousov
83678b51754SRuslan Bukin domain = IODOM2DOM(entry->domain);
83778b51754SRuslan Bukin unit = DOM2DMAR(domain);
8388bc36738SAlan Cox
8398bc36738SAlan Cox /*
8408bc36738SAlan Cox * If "free" is false, then the IOTLB invalidation must be performed
8418bc36738SAlan Cox * synchronously. Otherwise, the caller might free the entry before
8428bc36738SAlan Cox * dmar_qi_task() is finished processing it.
8438bc36738SAlan Cox */
84468eeb96aSKonstantin Belousov if (unit->qi_enabled) {
8458bc36738SAlan Cox if (free) {
846c2515634SAlan Cox DMAR_LOCK(unit);
847ad794e6dSKonstantin Belousov iommu_qi_invalidate_locked(&domain->iodom, entry,
848ad794e6dSKonstantin Belousov true);
84968eeb96aSKonstantin Belousov DMAR_UNLOCK(unit);
85068eeb96aSKonstantin Belousov } else {
851ad794e6dSKonstantin Belousov iommu_qi_invalidate_sync(&domain->iodom, entry->start,
852c2515634SAlan Cox entry->end - entry->start, cansleep);
853b3042e3aSKonstantin Belousov iommu_domain_free_entry(entry, false);
854c2515634SAlan Cox }
855c2515634SAlan Cox } else {
85610ebd230SKonstantin Belousov dmar_flush_iotlb_sync(domain, entry->start, entry->end -
8578bc36738SAlan Cox entry->start);
858b3042e3aSKonstantin Belousov iommu_domain_free_entry(entry, free);
85968eeb96aSKonstantin Belousov }
86068eeb96aSKonstantin Belousov }
86168eeb96aSKonstantin Belousov
862cf619a92SKonstantin Belousov static bool
dmar_domain_unload_emit_wait(struct dmar_domain * domain,struct iommu_map_entry * entry)863cf619a92SKonstantin Belousov dmar_domain_unload_emit_wait(struct dmar_domain *domain,
86459e37c8aSRuslan Bukin struct iommu_map_entry *entry)
865e164cafcSKonstantin Belousov {
866e164cafcSKonstantin Belousov
867cf619a92SKonstantin Belousov if (TAILQ_NEXT(entry, dmamap_link) == NULL)
868cf619a92SKonstantin Belousov return (true);
8690386b245SKonstantin Belousov return (domain->batch_no++ % iommu_qi_batch_coalesce == 0);
870e164cafcSKonstantin Belousov }
871e164cafcSKonstantin Belousov
87268eeb96aSKonstantin Belousov void
dmar_domain_unload(struct iommu_domain * iodom,struct iommu_map_entries_tailq * entries,bool cansleep)87365b133e5SKonstantin Belousov dmar_domain_unload(struct iommu_domain *iodom,
87459e37c8aSRuslan Bukin struct iommu_map_entries_tailq *entries, bool cansleep)
87586be9f0dSKonstantin Belousov {
876da55f86cSAlan Cox struct dmar_domain *domain;
87768eeb96aSKonstantin Belousov struct dmar_unit *unit;
87859e37c8aSRuslan Bukin struct iommu_map_entry *entry, *entry1;
879661bd70bSKonstantin Belousov int error __diagused;
88086be9f0dSKonstantin Belousov
881da55f86cSAlan Cox domain = IODOM2DOM(iodom);
88278b51754SRuslan Bukin unit = DOM2DMAR(domain);
88368eeb96aSKonstantin Belousov
88468eeb96aSKonstantin Belousov TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) {
88559e37c8aSRuslan Bukin KASSERT((entry->flags & IOMMU_MAP_ENTRY_MAP) != 0,
8861abfd355SKonstantin Belousov ("not mapped entry %p %p", domain, entry));
887bdd5eb33SKonstantin Belousov error = iodom->ops->unmap(iodom, entry,
888bdd5eb33SKonstantin Belousov cansleep ? IOMMU_PGF_WAITOK : 0);
8891abfd355SKonstantin Belousov KASSERT(error == 0, ("unmap %p error %d", domain, error));
89068eeb96aSKonstantin Belousov if (!unit->qi_enabled) {
89110ebd230SKonstantin Belousov dmar_flush_iotlb_sync(domain, entry->start,
89268eeb96aSKonstantin Belousov entry->end - entry->start);
89368eeb96aSKonstantin Belousov TAILQ_REMOVE(entries, entry, dmamap_link);
894b3042e3aSKonstantin Belousov iommu_domain_free_entry(entry, true);
89586be9f0dSKonstantin Belousov }
89686be9f0dSKonstantin Belousov }
89768eeb96aSKonstantin Belousov if (TAILQ_EMPTY(entries))
89868eeb96aSKonstantin Belousov return;
89968eeb96aSKonstantin Belousov
90068eeb96aSKonstantin Belousov KASSERT(unit->qi_enabled, ("loaded entry left"));
90168eeb96aSKonstantin Belousov DMAR_LOCK(unit);
90242736dc4SAlan Cox while ((entry = TAILQ_FIRST(entries)) != NULL) {
90342736dc4SAlan Cox TAILQ_REMOVE(entries, entry, dmamap_link);
904ad794e6dSKonstantin Belousov iommu_qi_invalidate_locked(&domain->iodom, entry,
905cf619a92SKonstantin Belousov dmar_domain_unload_emit_wait(domain, entry));
90668eeb96aSKonstantin Belousov }
90768eeb96aSKonstantin Belousov DMAR_UNLOCK(unit);
90868eeb96aSKonstantin Belousov }
90986be9f0dSKonstantin Belousov
91059e37c8aSRuslan Bukin struct iommu_ctx *
dmar_get_ctx(struct iommu_unit * iommu,device_t dev,uint16_t rid,bool id_mapped,bool rmrr_init)91165b133e5SKonstantin Belousov dmar_get_ctx(struct iommu_unit *iommu, device_t dev, uint16_t rid,
91259e37c8aSRuslan Bukin bool id_mapped, bool rmrr_init)
91359e37c8aSRuslan Bukin {
91459e37c8aSRuslan Bukin struct dmar_unit *dmar;
91559e37c8aSRuslan Bukin struct dmar_ctx *ret;
91659e37c8aSRuslan Bukin
91778b51754SRuslan Bukin dmar = IOMMU2DMAR(iommu);
91859e37c8aSRuslan Bukin ret = dmar_get_ctx_for_dev(dmar, dev, rid, id_mapped, rmrr_init);
91978b51754SRuslan Bukin return (CTX2IOCTX(ret));
92059e37c8aSRuslan Bukin }
92159e37c8aSRuslan Bukin
92259e37c8aSRuslan Bukin void
dmar_free_ctx_locked_method(struct iommu_unit * iommu,struct iommu_ctx * context)92365b133e5SKonstantin Belousov dmar_free_ctx_locked_method(struct iommu_unit *iommu,
92465b133e5SKonstantin Belousov struct iommu_ctx *context)
92559e37c8aSRuslan Bukin {
92659e37c8aSRuslan Bukin struct dmar_unit *dmar;
92759e37c8aSRuslan Bukin struct dmar_ctx *ctx;
92859e37c8aSRuslan Bukin
92978b51754SRuslan Bukin dmar = IOMMU2DMAR(iommu);
93078b51754SRuslan Bukin ctx = IOCTX2CTX(context);
93159e37c8aSRuslan Bukin dmar_free_ctx_locked(dmar, ctx);
93259e37c8aSRuslan Bukin }
933