186be9f0dSKonstantin Belousov /*- 286be9f0dSKonstantin Belousov * Copyright (c) 2013 The FreeBSD Foundation 386be9f0dSKonstantin Belousov * All rights reserved. 486be9f0dSKonstantin Belousov * 586be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 686be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 786be9f0dSKonstantin Belousov * 886be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 986be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions 1086be9f0dSKonstantin Belousov * are met: 1186be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 1286be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 1386be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 1486be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 1586be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution. 1686be9f0dSKonstantin Belousov * 1786be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1886be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1986be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2086be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2186be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2286be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2386be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2486be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2586be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2686be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2786be9f0dSKonstantin Belousov * SUCH DAMAGE. 2886be9f0dSKonstantin Belousov */ 2986be9f0dSKonstantin Belousov 3086be9f0dSKonstantin Belousov #include <sys/cdefs.h> 3186be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$"); 3286be9f0dSKonstantin Belousov 3386be9f0dSKonstantin Belousov #include <sys/param.h> 3486be9f0dSKonstantin Belousov #include <sys/systm.h> 3586be9f0dSKonstantin Belousov #include <sys/malloc.h> 3686be9f0dSKonstantin Belousov #include <sys/bus.h> 3786be9f0dSKonstantin Belousov #include <sys/interrupt.h> 3886be9f0dSKonstantin Belousov #include <sys/kernel.h> 3986be9f0dSKonstantin Belousov #include <sys/ktr.h> 4086be9f0dSKonstantin Belousov #include <sys/limits.h> 4186be9f0dSKonstantin Belousov #include <sys/lock.h> 4286be9f0dSKonstantin Belousov #include <sys/memdesc.h> 4386be9f0dSKonstantin Belousov #include <sys/mutex.h> 4486be9f0dSKonstantin Belousov #include <sys/proc.h> 4586be9f0dSKonstantin Belousov #include <sys/rwlock.h> 4686be9f0dSKonstantin Belousov #include <sys/rman.h> 4786be9f0dSKonstantin Belousov #include <sys/sysctl.h> 4886be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 4986be9f0dSKonstantin Belousov #include <sys/tree.h> 5086be9f0dSKonstantin Belousov #include <sys/uio.h> 51*0a110d5bSKonstantin Belousov #include <sys/vmem.h> 5286be9f0dSKonstantin Belousov #include <vm/vm.h> 5386be9f0dSKonstantin Belousov #include <vm/vm_extern.h> 5486be9f0dSKonstantin Belousov #include <vm/vm_kern.h> 5586be9f0dSKonstantin Belousov #include <vm/vm_object.h> 5686be9f0dSKonstantin Belousov #include <vm/vm_page.h> 5786be9f0dSKonstantin Belousov #include <vm/vm_pager.h> 5886be9f0dSKonstantin Belousov #include <vm/vm_map.h> 5986be9f0dSKonstantin Belousov #include <machine/atomic.h> 6086be9f0dSKonstantin Belousov #include <machine/bus.h> 6186be9f0dSKonstantin Belousov #include <machine/md_var.h> 6286be9f0dSKonstantin Belousov #include <machine/specialreg.h> 6386be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h> 6486be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h> 6586be9f0dSKonstantin Belousov #include <x86/iommu/busdma_dmar.h> 6686be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 6767499354SRyan Stone #include <dev/pci/pcireg.h> 6886be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h> 6986be9f0dSKonstantin Belousov 7086be9f0dSKonstantin Belousov static MALLOC_DEFINE(M_DMAR_CTX, "dmar_ctx", "Intel DMAR Context"); 7186be9f0dSKonstantin Belousov 7286be9f0dSKonstantin Belousov static void dmar_ctx_unload_task(void *arg, int pending); 7386be9f0dSKonstantin Belousov 7486be9f0dSKonstantin Belousov static void 7586be9f0dSKonstantin Belousov dmar_ensure_ctx_page(struct dmar_unit *dmar, int bus) 7686be9f0dSKonstantin Belousov { 7786be9f0dSKonstantin Belousov struct sf_buf *sf; 7886be9f0dSKonstantin Belousov dmar_root_entry_t *re; 7986be9f0dSKonstantin Belousov vm_page_t ctxm; 8086be9f0dSKonstantin Belousov 8186be9f0dSKonstantin Belousov /* 8286be9f0dSKonstantin Belousov * Allocated context page must be linked. 8386be9f0dSKonstantin Belousov */ 8486be9f0dSKonstantin Belousov ctxm = dmar_pgalloc(dmar->ctx_obj, 1 + bus, DMAR_PGF_NOALLOC); 8586be9f0dSKonstantin Belousov if (ctxm != NULL) 8686be9f0dSKonstantin Belousov return; 8786be9f0dSKonstantin Belousov 8886be9f0dSKonstantin Belousov /* 8986be9f0dSKonstantin Belousov * Page not present, allocate and link. Note that other 9086be9f0dSKonstantin Belousov * thread might execute this sequence in parallel. This 9186be9f0dSKonstantin Belousov * should be safe, because the context entries written by both 9286be9f0dSKonstantin Belousov * threads are equal. 9386be9f0dSKonstantin Belousov */ 9486be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT; 9586be9f0dSKonstantin Belousov ctxm = dmar_pgalloc(dmar->ctx_obj, 1 + bus, DMAR_PGF_ZERO | 9686be9f0dSKonstantin Belousov DMAR_PGF_WAITOK); 9786be9f0dSKonstantin Belousov re = dmar_map_pgtbl(dmar->ctx_obj, 0, DMAR_PGF_NOALLOC, &sf); 9886be9f0dSKonstantin Belousov re += bus; 9986be9f0dSKonstantin Belousov dmar_pte_store(&re->r1, DMAR_ROOT_R1_P | (DMAR_ROOT_R1_CTP_MASK & 10086be9f0dSKonstantin Belousov VM_PAGE_TO_PHYS(ctxm))); 1016b7c46afSKonstantin Belousov dmar_flush_root_to_ram(dmar, re); 1026b7c46afSKonstantin Belousov dmar_unmap_pgtbl(sf); 10386be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 10486be9f0dSKonstantin Belousov } 10586be9f0dSKonstantin Belousov 10686be9f0dSKonstantin Belousov static dmar_ctx_entry_t * 10786be9f0dSKonstantin Belousov dmar_map_ctx_entry(struct dmar_ctx *ctx, struct sf_buf **sfp) 10886be9f0dSKonstantin Belousov { 10986be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp; 11086be9f0dSKonstantin Belousov 11167499354SRyan Stone ctxp = dmar_map_pgtbl(ctx->dmar->ctx_obj, 1 + PCI_RID2BUS(ctx->rid), 11286be9f0dSKonstantin Belousov DMAR_PGF_NOALLOC | DMAR_PGF_WAITOK, sfp); 11367499354SRyan Stone ctxp += ctx->rid & 0xff; 11486be9f0dSKonstantin Belousov return (ctxp); 11586be9f0dSKonstantin Belousov } 11686be9f0dSKonstantin Belousov 11786be9f0dSKonstantin Belousov static void 11867499354SRyan Stone ctx_tag_init(struct dmar_ctx *ctx, device_t dev) 11986be9f0dSKonstantin Belousov { 12086be9f0dSKonstantin Belousov bus_addr_t maxaddr; 12186be9f0dSKonstantin Belousov 12286be9f0dSKonstantin Belousov maxaddr = MIN(ctx->end, BUS_SPACE_MAXADDR); 12386be9f0dSKonstantin Belousov ctx->ctx_tag.common.ref_count = 1; /* Prevent free */ 12486be9f0dSKonstantin Belousov ctx->ctx_tag.common.impl = &bus_dma_dmar_impl; 12586be9f0dSKonstantin Belousov ctx->ctx_tag.common.boundary = PCI_DMA_BOUNDARY; 12686be9f0dSKonstantin Belousov ctx->ctx_tag.common.lowaddr = maxaddr; 12786be9f0dSKonstantin Belousov ctx->ctx_tag.common.highaddr = maxaddr; 12886be9f0dSKonstantin Belousov ctx->ctx_tag.common.maxsize = maxaddr; 12986be9f0dSKonstantin Belousov ctx->ctx_tag.common.nsegments = BUS_SPACE_UNRESTRICTED; 13086be9f0dSKonstantin Belousov ctx->ctx_tag.common.maxsegsz = maxaddr; 13186be9f0dSKonstantin Belousov ctx->ctx_tag.ctx = ctx; 13267499354SRyan Stone ctx->ctx_tag.owner = dev; 13386be9f0dSKonstantin Belousov /* XXXKIB initialize tag further */ 13486be9f0dSKonstantin Belousov } 13586be9f0dSKonstantin Belousov 13686be9f0dSKonstantin Belousov static void 13786be9f0dSKonstantin Belousov ctx_id_entry_init(struct dmar_ctx *ctx, dmar_ctx_entry_t *ctxp) 13886be9f0dSKonstantin Belousov { 13986be9f0dSKonstantin Belousov struct dmar_unit *unit; 14086be9f0dSKonstantin Belousov vm_page_t ctx_root; 14186be9f0dSKonstantin Belousov 14286be9f0dSKonstantin Belousov unit = ctx->dmar; 14386be9f0dSKonstantin Belousov KASSERT(ctxp->ctx1 == 0 && ctxp->ctx2 == 0, 14486be9f0dSKonstantin Belousov ("dmar%d: initialized ctx entry %d:%d:%d 0x%jx 0x%jx", 14567499354SRyan Stone unit->unit, pci_get_bus(ctx->ctx_tag.owner), 14667499354SRyan Stone pci_get_slot(ctx->ctx_tag.owner), 14767499354SRyan Stone pci_get_function(ctx->ctx_tag.owner), 14867499354SRyan Stone ctxp->ctx1, 14986be9f0dSKonstantin Belousov ctxp->ctx2)); 15086be9f0dSKonstantin Belousov ctxp->ctx2 = DMAR_CTX2_DID(ctx->domain); 15186be9f0dSKonstantin Belousov ctxp->ctx2 |= ctx->awlvl; 15286be9f0dSKonstantin Belousov if ((ctx->flags & DMAR_CTX_IDMAP) != 0 && 15386be9f0dSKonstantin Belousov (unit->hw_ecap & DMAR_ECAP_PT) != 0) { 15486be9f0dSKonstantin Belousov KASSERT(ctx->pgtbl_obj == NULL, 15586be9f0dSKonstantin Belousov ("ctx %p non-null pgtbl_obj", ctx)); 15686be9f0dSKonstantin Belousov dmar_pte_store(&ctxp->ctx1, DMAR_CTX1_T_PASS | DMAR_CTX1_P); 15786be9f0dSKonstantin Belousov } else { 15886be9f0dSKonstantin Belousov ctx_root = dmar_pgalloc(ctx->pgtbl_obj, 0, DMAR_PGF_NOALLOC); 15986be9f0dSKonstantin Belousov dmar_pte_store(&ctxp->ctx1, DMAR_CTX1_T_UNTR | 16086be9f0dSKonstantin Belousov (DMAR_CTX1_ASR_MASK & VM_PAGE_TO_PHYS(ctx_root)) | 16186be9f0dSKonstantin Belousov DMAR_CTX1_P); 16286be9f0dSKonstantin Belousov } 1636b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(unit, ctxp); 16486be9f0dSKonstantin Belousov } 16586be9f0dSKonstantin Belousov 16686be9f0dSKonstantin Belousov static int 16786be9f0dSKonstantin Belousov ctx_init_rmrr(struct dmar_ctx *ctx, device_t dev) 16886be9f0dSKonstantin Belousov { 16986be9f0dSKonstantin Belousov struct dmar_map_entries_tailq rmrr_entries; 17086be9f0dSKonstantin Belousov struct dmar_map_entry *entry, *entry1; 17186be9f0dSKonstantin Belousov vm_page_t *ma; 17286be9f0dSKonstantin Belousov dmar_gaddr_t start, end; 17386be9f0dSKonstantin Belousov vm_pindex_t size, i; 17486be9f0dSKonstantin Belousov int error, error1; 17586be9f0dSKonstantin Belousov 17686be9f0dSKonstantin Belousov error = 0; 17786be9f0dSKonstantin Belousov TAILQ_INIT(&rmrr_entries); 17886be9f0dSKonstantin Belousov dmar_ctx_parse_rmrr(ctx, dev, &rmrr_entries); 17986be9f0dSKonstantin Belousov TAILQ_FOREACH_SAFE(entry, &rmrr_entries, unroll_link, entry1) { 18086be9f0dSKonstantin Belousov /* 18186be9f0dSKonstantin Belousov * VT-d specification requires that the start of an 18286be9f0dSKonstantin Belousov * RMRR entry is 4k-aligned. Buggy BIOSes put 18386be9f0dSKonstantin Belousov * anything into the start and end fields. Truncate 18486be9f0dSKonstantin Belousov * and round as neccesary. 18586be9f0dSKonstantin Belousov * 18686be9f0dSKonstantin Belousov * We also allow the overlapping RMRR entries, see 18786be9f0dSKonstantin Belousov * dmar_gas_alloc_region(). 18886be9f0dSKonstantin Belousov */ 18986be9f0dSKonstantin Belousov start = entry->start; 19086be9f0dSKonstantin Belousov end = entry->end; 19186be9f0dSKonstantin Belousov entry->start = trunc_page(start); 19286be9f0dSKonstantin Belousov entry->end = round_page(end); 193e02b05b3SKonstantin Belousov if (entry->start == entry->end) { 194e02b05b3SKonstantin Belousov /* Workaround for some AMI (?) BIOSes */ 195e02b05b3SKonstantin Belousov if (bootverbose) { 196e02b05b3SKonstantin Belousov device_printf(dev, "BIOS bug: dmar%d RMRR " 197e02b05b3SKonstantin Belousov "region (%jx, %jx) corrected\n", 198e02b05b3SKonstantin Belousov ctx->dmar->unit, start, end); 199e02b05b3SKonstantin Belousov } 200e02b05b3SKonstantin Belousov entry->end += DMAR_PAGE_SIZE * 0x20; 201e02b05b3SKonstantin Belousov } 20286be9f0dSKonstantin Belousov size = OFF_TO_IDX(entry->end - entry->start); 20386be9f0dSKonstantin Belousov ma = malloc(sizeof(vm_page_t) * size, M_TEMP, M_WAITOK); 20486be9f0dSKonstantin Belousov for (i = 0; i < size; i++) { 20586be9f0dSKonstantin Belousov ma[i] = vm_page_getfake(entry->start + PAGE_SIZE * i, 20686be9f0dSKonstantin Belousov VM_MEMATTR_DEFAULT); 20786be9f0dSKonstantin Belousov } 20886be9f0dSKonstantin Belousov error1 = dmar_gas_map_region(ctx, entry, DMAR_MAP_ENTRY_READ | 20986be9f0dSKonstantin Belousov DMAR_MAP_ENTRY_WRITE, DMAR_GM_CANWAIT, ma); 21086be9f0dSKonstantin Belousov /* 21186be9f0dSKonstantin Belousov * Non-failed RMRR entries are owned by context rb 21286be9f0dSKonstantin Belousov * tree. Get rid of the failed entry, but do not stop 21386be9f0dSKonstantin Belousov * the loop. Rest of the parsed RMRR entries are 21486be9f0dSKonstantin Belousov * loaded and removed on the context destruction. 21586be9f0dSKonstantin Belousov */ 21686be9f0dSKonstantin Belousov if (error1 == 0 && entry->end != entry->start) { 21786be9f0dSKonstantin Belousov DMAR_LOCK(ctx->dmar); 21886be9f0dSKonstantin Belousov ctx->flags |= DMAR_CTX_RMRR; 21986be9f0dSKonstantin Belousov DMAR_UNLOCK(ctx->dmar); 22086be9f0dSKonstantin Belousov } else { 22186be9f0dSKonstantin Belousov if (error1 != 0) { 22286be9f0dSKonstantin Belousov device_printf(dev, 22386be9f0dSKonstantin Belousov "dmar%d failed to map RMRR region (%jx, %jx) %d\n", 22486be9f0dSKonstantin Belousov ctx->dmar->unit, start, end, error1); 22586be9f0dSKonstantin Belousov error = error1; 22686be9f0dSKonstantin Belousov } 22786be9f0dSKonstantin Belousov TAILQ_REMOVE(&rmrr_entries, entry, unroll_link); 22886be9f0dSKonstantin Belousov dmar_gas_free_entry(ctx, entry); 22986be9f0dSKonstantin Belousov } 23086be9f0dSKonstantin Belousov for (i = 0; i < size; i++) 23186be9f0dSKonstantin Belousov vm_page_putfake(ma[i]); 23286be9f0dSKonstantin Belousov free(ma, M_TEMP); 23386be9f0dSKonstantin Belousov } 23486be9f0dSKonstantin Belousov return (error); 23586be9f0dSKonstantin Belousov } 23686be9f0dSKonstantin Belousov 23786be9f0dSKonstantin Belousov static struct dmar_ctx * 23867499354SRyan Stone dmar_get_ctx_alloc(struct dmar_unit *dmar, uint16_t rid) 23986be9f0dSKonstantin Belousov { 24086be9f0dSKonstantin Belousov struct dmar_ctx *ctx; 24186be9f0dSKonstantin Belousov 24286be9f0dSKonstantin Belousov ctx = malloc(sizeof(*ctx), M_DMAR_CTX, M_WAITOK | M_ZERO); 24386be9f0dSKonstantin Belousov RB_INIT(&ctx->rb_root); 24486be9f0dSKonstantin Belousov TAILQ_INIT(&ctx->unload_entries); 24586be9f0dSKonstantin Belousov TASK_INIT(&ctx->unload_task, 0, dmar_ctx_unload_task, ctx); 24686be9f0dSKonstantin Belousov mtx_init(&ctx->lock, "dmarctx", NULL, MTX_DEF); 24786be9f0dSKonstantin Belousov ctx->dmar = dmar; 24867499354SRyan Stone ctx->rid = rid; 24986be9f0dSKonstantin Belousov return (ctx); 25086be9f0dSKonstantin Belousov } 25186be9f0dSKonstantin Belousov 25286be9f0dSKonstantin Belousov static void 25386be9f0dSKonstantin Belousov dmar_ctx_dtr(struct dmar_ctx *ctx, bool gas_inited, bool pgtbl_inited) 25486be9f0dSKonstantin Belousov { 25586be9f0dSKonstantin Belousov 25686be9f0dSKonstantin Belousov if (gas_inited) { 25786be9f0dSKonstantin Belousov DMAR_CTX_LOCK(ctx); 25886be9f0dSKonstantin Belousov dmar_gas_fini_ctx(ctx); 25986be9f0dSKonstantin Belousov DMAR_CTX_UNLOCK(ctx); 26086be9f0dSKonstantin Belousov } 26186be9f0dSKonstantin Belousov if (pgtbl_inited) { 26286be9f0dSKonstantin Belousov if (ctx->pgtbl_obj != NULL) 26386be9f0dSKonstantin Belousov DMAR_CTX_PGLOCK(ctx); 26486be9f0dSKonstantin Belousov ctx_free_pgtbl(ctx); 26586be9f0dSKonstantin Belousov } 26686be9f0dSKonstantin Belousov mtx_destroy(&ctx->lock); 26786be9f0dSKonstantin Belousov free(ctx, M_DMAR_CTX); 26886be9f0dSKonstantin Belousov } 26986be9f0dSKonstantin Belousov 27086be9f0dSKonstantin Belousov struct dmar_ctx * 27167499354SRyan Stone dmar_get_ctx(struct dmar_unit *dmar, device_t dev, uint16_t rid, bool id_mapped, 27267499354SRyan Stone bool rmrr_init) 27386be9f0dSKonstantin Belousov { 27486be9f0dSKonstantin Belousov struct dmar_ctx *ctx, *ctx1; 27586be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp; 27686be9f0dSKonstantin Belousov struct sf_buf *sf; 27767499354SRyan Stone int bus, slot, func, error, mgaw; 27886be9f0dSKonstantin Belousov bool enable; 27986be9f0dSKonstantin Belousov 28067499354SRyan Stone bus = pci_get_bus(dev); 28167499354SRyan Stone slot = pci_get_slot(dev); 28267499354SRyan Stone func = pci_get_function(dev); 28386be9f0dSKonstantin Belousov enable = false; 28486be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT; 28586be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 28667499354SRyan Stone ctx = dmar_find_ctx_locked(dmar, rid); 28786be9f0dSKonstantin Belousov error = 0; 28886be9f0dSKonstantin Belousov if (ctx == NULL) { 28986be9f0dSKonstantin Belousov /* 29086be9f0dSKonstantin Belousov * Perform the allocations which require sleep or have 29186be9f0dSKonstantin Belousov * higher chance to succeed if the sleep is allowed. 29286be9f0dSKonstantin Belousov */ 29386be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 294b29d186cSKonstantin Belousov dmar_ensure_ctx_page(dmar, PCI_RID2BUS(rid)); 29567499354SRyan Stone ctx1 = dmar_get_ctx_alloc(dmar, rid); 29686be9f0dSKonstantin Belousov 29786be9f0dSKonstantin Belousov if (id_mapped) { 29886be9f0dSKonstantin Belousov /* 29986be9f0dSKonstantin Belousov * For now, use the maximal usable physical 30086be9f0dSKonstantin Belousov * address of the installed memory to 30186be9f0dSKonstantin Belousov * calculate the mgaw. It is useful for the 30286be9f0dSKonstantin Belousov * identity mapping, and less so for the 30386be9f0dSKonstantin Belousov * virtualized bus address space. 30486be9f0dSKonstantin Belousov */ 30586be9f0dSKonstantin Belousov ctx1->end = ptoa(Maxmem); 30686be9f0dSKonstantin Belousov mgaw = dmar_maxaddr2mgaw(dmar, ctx1->end, false); 30786be9f0dSKonstantin Belousov error = ctx_set_agaw(ctx1, mgaw); 30886be9f0dSKonstantin Belousov if (error != 0) { 30986be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, false, false); 31086be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 31186be9f0dSKonstantin Belousov return (NULL); 31286be9f0dSKonstantin Belousov } 31386be9f0dSKonstantin Belousov } else { 31486be9f0dSKonstantin Belousov ctx1->end = BUS_SPACE_MAXADDR; 31586be9f0dSKonstantin Belousov mgaw = dmar_maxaddr2mgaw(dmar, ctx1->end, true); 31686be9f0dSKonstantin Belousov error = ctx_set_agaw(ctx1, mgaw); 31786be9f0dSKonstantin Belousov if (error != 0) { 31886be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, false, false); 31986be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 32086be9f0dSKonstantin Belousov return (NULL); 32186be9f0dSKonstantin Belousov } 32286be9f0dSKonstantin Belousov /* Use all supported address space for remapping. */ 32386be9f0dSKonstantin Belousov ctx1->end = 1ULL << (ctx1->agaw - 1); 32486be9f0dSKonstantin Belousov } 32586be9f0dSKonstantin Belousov 32686be9f0dSKonstantin Belousov 32786be9f0dSKonstantin Belousov dmar_gas_init_ctx(ctx1); 32886be9f0dSKonstantin Belousov if (id_mapped) { 32986be9f0dSKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_PT) == 0) { 33086be9f0dSKonstantin Belousov ctx1->pgtbl_obj = ctx_get_idmap_pgtbl(ctx1, 33186be9f0dSKonstantin Belousov ctx1->end); 33286be9f0dSKonstantin Belousov } 33386be9f0dSKonstantin Belousov ctx1->flags |= DMAR_CTX_IDMAP; 33486be9f0dSKonstantin Belousov } else { 33586be9f0dSKonstantin Belousov error = ctx_alloc_pgtbl(ctx1); 33686be9f0dSKonstantin Belousov if (error != 0) { 33786be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, true, false); 33886be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 33986be9f0dSKonstantin Belousov return (NULL); 34086be9f0dSKonstantin Belousov } 34186be9f0dSKonstantin Belousov /* Disable local apic region access */ 34286be9f0dSKonstantin Belousov error = dmar_gas_reserve_region(ctx1, 0xfee00000, 34386be9f0dSKonstantin Belousov 0xfeefffff + 1); 34486be9f0dSKonstantin Belousov if (error != 0) { 34586be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, true, true); 34686be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 34786be9f0dSKonstantin Belousov return (NULL); 34886be9f0dSKonstantin Belousov } 34986be9f0dSKonstantin Belousov error = ctx_init_rmrr(ctx1, dev); 35086be9f0dSKonstantin Belousov if (error != 0) { 35186be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, true, true); 35286be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 35386be9f0dSKonstantin Belousov return (NULL); 35486be9f0dSKonstantin Belousov } 35586be9f0dSKonstantin Belousov } 35686be9f0dSKonstantin Belousov ctxp = dmar_map_ctx_entry(ctx1, &sf); 35786be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 35886be9f0dSKonstantin Belousov 35986be9f0dSKonstantin Belousov /* 36086be9f0dSKonstantin Belousov * Recheck the contexts, other thread might have 36186be9f0dSKonstantin Belousov * already allocated needed one. 36286be9f0dSKonstantin Belousov */ 36367499354SRyan Stone ctx = dmar_find_ctx_locked(dmar, rid); 36486be9f0dSKonstantin Belousov if (ctx == NULL) { 36586be9f0dSKonstantin Belousov ctx = ctx1; 3669d0bc6d8SKonstantin Belousov ctx->ctx_tag.owner = dev; 36786be9f0dSKonstantin Belousov ctx->domain = alloc_unrl(dmar->domids); 36886be9f0dSKonstantin Belousov if (ctx->domain == -1) { 36986be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 3706b7c46afSKonstantin Belousov dmar_unmap_pgtbl(sf); 37186be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx, true, true); 37286be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 37386be9f0dSKonstantin Belousov return (NULL); 37486be9f0dSKonstantin Belousov } 37567499354SRyan Stone ctx_tag_init(ctx, dev); 37686be9f0dSKonstantin Belousov 37786be9f0dSKonstantin Belousov /* 37886be9f0dSKonstantin Belousov * This is the first activated context for the 37986be9f0dSKonstantin Belousov * DMAR unit. Enable the translation after 38086be9f0dSKonstantin Belousov * everything is set up. 38186be9f0dSKonstantin Belousov */ 38286be9f0dSKonstantin Belousov if (LIST_EMPTY(&dmar->contexts)) 38386be9f0dSKonstantin Belousov enable = true; 38486be9f0dSKonstantin Belousov LIST_INSERT_HEAD(&dmar->contexts, ctx, link); 38586be9f0dSKonstantin Belousov ctx_id_entry_init(ctx, ctxp); 38686be9f0dSKonstantin Belousov device_printf(dev, 38734e8337bSKonstantin Belousov "dmar%d pci%d:%d:%d:%d rid %x domain %d mgaw %d " 3889d0bc6d8SKonstantin Belousov "agaw %d %s-mapped\n", 38986be9f0dSKonstantin Belousov dmar->unit, dmar->segment, bus, slot, 39034e8337bSKonstantin Belousov func, rid, ctx->domain, ctx->mgaw, ctx->agaw, 3919d0bc6d8SKonstantin Belousov id_mapped ? "id" : "re"); 39286be9f0dSKonstantin Belousov } else { 39386be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx1, true, true); 39486be9f0dSKonstantin Belousov } 3956b7c46afSKonstantin Belousov dmar_unmap_pgtbl(sf); 39686be9f0dSKonstantin Belousov } 39786be9f0dSKonstantin Belousov ctx->refs++; 39886be9f0dSKonstantin Belousov if ((ctx->flags & DMAR_CTX_RMRR) != 0) 39986be9f0dSKonstantin Belousov ctx->refs++; /* XXXKIB */ 40086be9f0dSKonstantin Belousov 40186be9f0dSKonstantin Belousov /* 40286be9f0dSKonstantin Belousov * If dmar declares Caching Mode as Set, follow 11.5 "Caching 40386be9f0dSKonstantin Belousov * Mode Consideration" and do the (global) invalidation of the 40486be9f0dSKonstantin Belousov * negative TLB entries. 40586be9f0dSKonstantin Belousov */ 40686be9f0dSKonstantin Belousov if ((dmar->hw_cap & DMAR_CAP_CM) != 0 || enable) { 40768eeb96aSKonstantin Belousov if (dmar->qi_enabled) { 40868eeb96aSKonstantin Belousov dmar_qi_invalidate_ctx_glob_locked(dmar); 40968eeb96aSKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) 41068eeb96aSKonstantin Belousov dmar_qi_invalidate_iotlb_glob_locked(dmar); 41168eeb96aSKonstantin Belousov } else { 41286be9f0dSKonstantin Belousov error = dmar_inv_ctx_glob(dmar); 41386be9f0dSKonstantin Belousov if (error == 0 && 41486be9f0dSKonstantin Belousov (dmar->hw_ecap & DMAR_ECAP_DI) != 0) 41586be9f0dSKonstantin Belousov error = dmar_inv_iotlb_glob(dmar); 41686be9f0dSKonstantin Belousov if (error != 0) { 41786be9f0dSKonstantin Belousov dmar_free_ctx_locked(dmar, ctx); 41886be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 41986be9f0dSKonstantin Belousov return (NULL); 42086be9f0dSKonstantin Belousov } 42186be9f0dSKonstantin Belousov } 42268eeb96aSKonstantin Belousov } 42368eeb96aSKonstantin Belousov 42468eeb96aSKonstantin Belousov /* 42568eeb96aSKonstantin Belousov * The dmar lock was potentially dropped between check for the 42668eeb96aSKonstantin Belousov * empty context list and now. Recheck the state of GCMD_TE 42768eeb96aSKonstantin Belousov * to avoid unneeded command. 42868eeb96aSKonstantin Belousov */ 42968eeb96aSKonstantin Belousov if (enable && !rmrr_init && (dmar->hw_gcmd & DMAR_GCMD_TE) == 0) { 43086be9f0dSKonstantin Belousov error = dmar_enable_translation(dmar); 43186be9f0dSKonstantin Belousov if (error != 0) { 43286be9f0dSKonstantin Belousov dmar_free_ctx_locked(dmar, ctx); 43386be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 43486be9f0dSKonstantin Belousov return (NULL); 43586be9f0dSKonstantin Belousov } 43686be9f0dSKonstantin Belousov } 43786be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 43886be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 43986be9f0dSKonstantin Belousov return (ctx); 44086be9f0dSKonstantin Belousov } 44186be9f0dSKonstantin Belousov 44286be9f0dSKonstantin Belousov void 44386be9f0dSKonstantin Belousov dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx) 44486be9f0dSKonstantin Belousov { 44586be9f0dSKonstantin Belousov struct sf_buf *sf; 44686be9f0dSKonstantin Belousov dmar_ctx_entry_t *ctxp; 44786be9f0dSKonstantin Belousov 44886be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar); 44986be9f0dSKonstantin Belousov KASSERT(ctx->refs >= 1, 45086be9f0dSKonstantin Belousov ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs)); 45186be9f0dSKonstantin Belousov 45286be9f0dSKonstantin Belousov /* 45386be9f0dSKonstantin Belousov * If our reference is not last, only the dereference should 45486be9f0dSKonstantin Belousov * be performed. 45586be9f0dSKonstantin Belousov */ 45686be9f0dSKonstantin Belousov if (ctx->refs > 1) { 45786be9f0dSKonstantin Belousov ctx->refs--; 45886be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 45986be9f0dSKonstantin Belousov return; 46086be9f0dSKonstantin Belousov } 46186be9f0dSKonstantin Belousov 46286be9f0dSKonstantin Belousov KASSERT((ctx->flags & DMAR_CTX_RMRR) == 0, 46386be9f0dSKonstantin Belousov ("lost ref on RMRR ctx %p", ctx)); 46486be9f0dSKonstantin Belousov KASSERT((ctx->flags & DMAR_CTX_DISABLED) == 0, 46586be9f0dSKonstantin Belousov ("lost ref on disabled ctx %p", ctx)); 46686be9f0dSKonstantin Belousov 46786be9f0dSKonstantin Belousov /* 46886be9f0dSKonstantin Belousov * Otherwise, the context entry must be cleared before the 46986be9f0dSKonstantin Belousov * page table is destroyed. The mapping of the context 47086be9f0dSKonstantin Belousov * entries page could require sleep, unlock the dmar. 47186be9f0dSKonstantin Belousov */ 47286be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 47386be9f0dSKonstantin Belousov TD_PREP_PINNED_ASSERT; 47486be9f0dSKonstantin Belousov ctxp = dmar_map_ctx_entry(ctx, &sf); 47586be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 47686be9f0dSKonstantin Belousov KASSERT(ctx->refs >= 1, 47786be9f0dSKonstantin Belousov ("dmar %p ctx %p refs %u", dmar, ctx, ctx->refs)); 47886be9f0dSKonstantin Belousov 47986be9f0dSKonstantin Belousov /* 48086be9f0dSKonstantin Belousov * Other thread might have referenced the context, in which 48186be9f0dSKonstantin Belousov * case again only the dereference should be performed. 48286be9f0dSKonstantin Belousov */ 48386be9f0dSKonstantin Belousov if (ctx->refs > 1) { 48486be9f0dSKonstantin Belousov ctx->refs--; 48586be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 4866b7c46afSKonstantin Belousov dmar_unmap_pgtbl(sf); 48786be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 48886be9f0dSKonstantin Belousov return; 48986be9f0dSKonstantin Belousov } 49086be9f0dSKonstantin Belousov 49186be9f0dSKonstantin Belousov KASSERT((ctx->flags & DMAR_CTX_RMRR) == 0, 49286be9f0dSKonstantin Belousov ("lost ref on RMRR ctx %p", ctx)); 49386be9f0dSKonstantin Belousov KASSERT((ctx->flags & DMAR_CTX_DISABLED) == 0, 49486be9f0dSKonstantin Belousov ("lost ref on disabled ctx %p", ctx)); 49586be9f0dSKonstantin Belousov 49686be9f0dSKonstantin Belousov /* 49786be9f0dSKonstantin Belousov * Clear the context pointer and flush the caches. 49886be9f0dSKonstantin Belousov * XXXKIB: cannot do this if any RMRR entries are still present. 49986be9f0dSKonstantin Belousov */ 50086be9f0dSKonstantin Belousov dmar_pte_clear(&ctxp->ctx1); 50186be9f0dSKonstantin Belousov ctxp->ctx2 = 0; 5026b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(dmar, ctxp); 50386be9f0dSKonstantin Belousov dmar_inv_ctx_glob(dmar); 50468eeb96aSKonstantin Belousov if ((dmar->hw_ecap & DMAR_ECAP_DI) != 0) { 50568eeb96aSKonstantin Belousov if (dmar->qi_enabled) 50668eeb96aSKonstantin Belousov dmar_qi_invalidate_iotlb_glob_locked(dmar); 50768eeb96aSKonstantin Belousov else 50886be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(dmar); 50968eeb96aSKonstantin Belousov } 51086be9f0dSKonstantin Belousov LIST_REMOVE(ctx, link); 51186be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 51286be9f0dSKonstantin Belousov 51386be9f0dSKonstantin Belousov /* 51486be9f0dSKonstantin Belousov * The rest of the destruction is invisible for other users of 51586be9f0dSKonstantin Belousov * the dmar unit. 51686be9f0dSKonstantin Belousov */ 51786be9f0dSKonstantin Belousov taskqueue_drain(dmar->delayed_taskqueue, &ctx->unload_task); 51886be9f0dSKonstantin Belousov KASSERT(TAILQ_EMPTY(&ctx->unload_entries), 51986be9f0dSKonstantin Belousov ("unfinished unloads %p", ctx)); 5206b7c46afSKonstantin Belousov dmar_unmap_pgtbl(sf); 52186be9f0dSKonstantin Belousov free_unr(dmar->domids, ctx->domain); 52286be9f0dSKonstantin Belousov dmar_ctx_dtr(ctx, true, true); 52386be9f0dSKonstantin Belousov TD_PINNED_ASSERT; 52486be9f0dSKonstantin Belousov } 52586be9f0dSKonstantin Belousov 52686be9f0dSKonstantin Belousov void 52786be9f0dSKonstantin Belousov dmar_free_ctx(struct dmar_ctx *ctx) 52886be9f0dSKonstantin Belousov { 52986be9f0dSKonstantin Belousov struct dmar_unit *dmar; 53086be9f0dSKonstantin Belousov 53186be9f0dSKonstantin Belousov dmar = ctx->dmar; 53286be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 53386be9f0dSKonstantin Belousov dmar_free_ctx_locked(dmar, ctx); 53486be9f0dSKonstantin Belousov } 53586be9f0dSKonstantin Belousov 53686be9f0dSKonstantin Belousov struct dmar_ctx * 53767499354SRyan Stone dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid) 53886be9f0dSKonstantin Belousov { 53986be9f0dSKonstantin Belousov struct dmar_ctx *ctx; 54086be9f0dSKonstantin Belousov 54186be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar); 54286be9f0dSKonstantin Belousov 54386be9f0dSKonstantin Belousov LIST_FOREACH(ctx, &dmar->contexts, link) { 54467499354SRyan Stone if (ctx->rid == rid) 54586be9f0dSKonstantin Belousov return (ctx); 54686be9f0dSKonstantin Belousov } 54786be9f0dSKonstantin Belousov return (NULL); 54886be9f0dSKonstantin Belousov } 54986be9f0dSKonstantin Belousov 55086be9f0dSKonstantin Belousov void 55168eeb96aSKonstantin Belousov dmar_ctx_free_entry(struct dmar_map_entry *entry, bool free) 55268eeb96aSKonstantin Belousov { 55368eeb96aSKonstantin Belousov struct dmar_ctx *ctx; 55468eeb96aSKonstantin Belousov 55568eeb96aSKonstantin Belousov ctx = entry->ctx; 55668eeb96aSKonstantin Belousov DMAR_CTX_LOCK(ctx); 55768eeb96aSKonstantin Belousov if ((entry->flags & DMAR_MAP_ENTRY_RMRR) != 0) 55868eeb96aSKonstantin Belousov dmar_gas_free_region(ctx, entry); 55968eeb96aSKonstantin Belousov else 56068eeb96aSKonstantin Belousov dmar_gas_free_space(ctx, entry); 56168eeb96aSKonstantin Belousov DMAR_CTX_UNLOCK(ctx); 56268eeb96aSKonstantin Belousov if (free) 56368eeb96aSKonstantin Belousov dmar_gas_free_entry(ctx, entry); 56468eeb96aSKonstantin Belousov else 56568eeb96aSKonstantin Belousov entry->flags = 0; 56668eeb96aSKonstantin Belousov } 56768eeb96aSKonstantin Belousov 56868eeb96aSKonstantin Belousov void 56968eeb96aSKonstantin Belousov dmar_ctx_unload_entry(struct dmar_map_entry *entry, bool free) 57068eeb96aSKonstantin Belousov { 57168eeb96aSKonstantin Belousov struct dmar_unit *unit; 57268eeb96aSKonstantin Belousov 57368eeb96aSKonstantin Belousov unit = entry->ctx->dmar; 57468eeb96aSKonstantin Belousov if (unit->qi_enabled) { 57568eeb96aSKonstantin Belousov DMAR_LOCK(unit); 57668eeb96aSKonstantin Belousov dmar_qi_invalidate_locked(entry->ctx, entry->start, 57768eeb96aSKonstantin Belousov entry->end - entry->start, &entry->gseq); 57868eeb96aSKonstantin Belousov if (!free) 57968eeb96aSKonstantin Belousov entry->flags |= DMAR_MAP_ENTRY_QI_NF; 58068eeb96aSKonstantin Belousov TAILQ_INSERT_TAIL(&unit->tlb_flush_entries, entry, dmamap_link); 58168eeb96aSKonstantin Belousov DMAR_UNLOCK(unit); 58268eeb96aSKonstantin Belousov } else { 58368eeb96aSKonstantin Belousov ctx_flush_iotlb_sync(entry->ctx, entry->start, entry->end - 58468eeb96aSKonstantin Belousov entry->start); 58568eeb96aSKonstantin Belousov dmar_ctx_free_entry(entry, free); 58668eeb96aSKonstantin Belousov } 58768eeb96aSKonstantin Belousov } 58868eeb96aSKonstantin Belousov 58968eeb96aSKonstantin Belousov void 59086be9f0dSKonstantin Belousov dmar_ctx_unload(struct dmar_ctx *ctx, struct dmar_map_entries_tailq *entries, 59186be9f0dSKonstantin Belousov bool cansleep) 59286be9f0dSKonstantin Belousov { 59368eeb96aSKonstantin Belousov struct dmar_unit *unit; 59468eeb96aSKonstantin Belousov struct dmar_map_entry *entry, *entry1; 59568eeb96aSKonstantin Belousov struct dmar_qi_genseq gseq; 59686be9f0dSKonstantin Belousov int error; 59786be9f0dSKonstantin Belousov 59868eeb96aSKonstantin Belousov unit = ctx->dmar; 59968eeb96aSKonstantin Belousov 60068eeb96aSKonstantin Belousov TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) { 60186be9f0dSKonstantin Belousov KASSERT((entry->flags & DMAR_MAP_ENTRY_MAP) != 0, 60286be9f0dSKonstantin Belousov ("not mapped entry %p %p", ctx, entry)); 60386be9f0dSKonstantin Belousov error = ctx_unmap_buf(ctx, entry->start, entry->end - 60486be9f0dSKonstantin Belousov entry->start, cansleep ? DMAR_PGF_WAITOK : 0); 60586be9f0dSKonstantin Belousov KASSERT(error == 0, ("unmap %p error %d", ctx, error)); 60668eeb96aSKonstantin Belousov if (!unit->qi_enabled) { 60768eeb96aSKonstantin Belousov ctx_flush_iotlb_sync(ctx, entry->start, 60868eeb96aSKonstantin Belousov entry->end - entry->start); 60968eeb96aSKonstantin Belousov TAILQ_REMOVE(entries, entry, dmamap_link); 61068eeb96aSKonstantin Belousov dmar_ctx_free_entry(entry, true); 61186be9f0dSKonstantin Belousov } 61286be9f0dSKonstantin Belousov } 61368eeb96aSKonstantin Belousov if (TAILQ_EMPTY(entries)) 61468eeb96aSKonstantin Belousov return; 61568eeb96aSKonstantin Belousov 61668eeb96aSKonstantin Belousov KASSERT(unit->qi_enabled, ("loaded entry left")); 61768eeb96aSKonstantin Belousov DMAR_LOCK(unit); 61868eeb96aSKonstantin Belousov TAILQ_FOREACH(entry, entries, dmamap_link) { 61968eeb96aSKonstantin Belousov entry->gseq.gen = 0; 62068eeb96aSKonstantin Belousov entry->gseq.seq = 0; 62168eeb96aSKonstantin Belousov dmar_qi_invalidate_locked(ctx, entry->start, entry->end - 62268eeb96aSKonstantin Belousov entry->start, TAILQ_NEXT(entry, dmamap_link) == NULL ? 62368eeb96aSKonstantin Belousov &gseq : NULL); 62468eeb96aSKonstantin Belousov } 62568eeb96aSKonstantin Belousov TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) { 62668eeb96aSKonstantin Belousov entry->gseq = gseq; 62768eeb96aSKonstantin Belousov TAILQ_REMOVE(entries, entry, dmamap_link); 62868eeb96aSKonstantin Belousov TAILQ_INSERT_TAIL(&unit->tlb_flush_entries, entry, dmamap_link); 62968eeb96aSKonstantin Belousov } 63068eeb96aSKonstantin Belousov DMAR_UNLOCK(unit); 63168eeb96aSKonstantin Belousov } 63286be9f0dSKonstantin Belousov 63386be9f0dSKonstantin Belousov static void 63486be9f0dSKonstantin Belousov dmar_ctx_unload_task(void *arg, int pending) 63586be9f0dSKonstantin Belousov { 63686be9f0dSKonstantin Belousov struct dmar_ctx *ctx; 63786be9f0dSKonstantin Belousov struct dmar_map_entries_tailq entries; 63886be9f0dSKonstantin Belousov 63986be9f0dSKonstantin Belousov ctx = arg; 64086be9f0dSKonstantin Belousov TAILQ_INIT(&entries); 64186be9f0dSKonstantin Belousov 64286be9f0dSKonstantin Belousov for (;;) { 64386be9f0dSKonstantin Belousov DMAR_CTX_LOCK(ctx); 64486be9f0dSKonstantin Belousov TAILQ_SWAP(&ctx->unload_entries, &entries, dmar_map_entry, 64586be9f0dSKonstantin Belousov dmamap_link); 64686be9f0dSKonstantin Belousov DMAR_CTX_UNLOCK(ctx); 64786be9f0dSKonstantin Belousov if (TAILQ_EMPTY(&entries)) 64886be9f0dSKonstantin Belousov break; 64986be9f0dSKonstantin Belousov dmar_ctx_unload(ctx, &entries, true); 65086be9f0dSKonstantin Belousov } 65186be9f0dSKonstantin Belousov } 652