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 * 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/cdefs.h> 3286be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$"); 3386be9f0dSKonstantin Belousov 3486be9f0dSKonstantin Belousov #include <sys/param.h> 3586be9f0dSKonstantin Belousov #include <sys/bus.h> 3686be9f0dSKonstantin Belousov #include <sys/kernel.h> 3786be9f0dSKonstantin Belousov #include <sys/lock.h> 3886be9f0dSKonstantin Belousov #include <sys/malloc.h> 3986be9f0dSKonstantin Belousov #include <sys/memdesc.h> 4086be9f0dSKonstantin Belousov #include <sys/mutex.h> 4186be9f0dSKonstantin Belousov #include <sys/proc.h> 4286be9f0dSKonstantin Belousov #include <sys/queue.h> 4386be9f0dSKonstantin Belousov #include <sys/rman.h> 4486be9f0dSKonstantin Belousov #include <sys/rwlock.h> 4586be9f0dSKonstantin Belousov #include <sys/sched.h> 4686be9f0dSKonstantin Belousov #include <sys/sf_buf.h> 4786be9f0dSKonstantin Belousov #include <sys/sysctl.h> 4886be9f0dSKonstantin Belousov #include <sys/systm.h> 4986be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 50476358b3SKonstantin Belousov #include <sys/time.h> 5186be9f0dSKonstantin Belousov #include <sys/tree.h> 520a110d5bSKonstantin Belousov #include <sys/vmem.h> 5386be9f0dSKonstantin Belousov #include <vm/vm.h> 5486be9f0dSKonstantin Belousov #include <vm/vm_extern.h> 5586be9f0dSKonstantin Belousov #include <vm/vm_kern.h> 5686be9f0dSKonstantin Belousov #include <vm/vm_object.h> 5786be9f0dSKonstantin Belousov #include <vm/vm_page.h> 5886be9f0dSKonstantin Belousov #include <vm/vm_map.h> 5986be9f0dSKonstantin Belousov #include <vm/vm_pageout.h> 60c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h> 61c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h> 6286be9f0dSKonstantin Belousov #include <machine/bus.h> 6386be9f0dSKonstantin Belousov #include <machine/cpu.h> 640a110d5bSKonstantin Belousov #include <machine/intr_machdep.h> 650a110d5bSKonstantin Belousov #include <x86/include/apicvar.h> 6686be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h> 67f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h> 68c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h> 6986be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 7086be9f0dSKonstantin Belousov 7186be9f0dSKonstantin Belousov u_int 7286be9f0dSKonstantin Belousov dmar_nd2mask(u_int nd) 7386be9f0dSKonstantin Belousov { 7486be9f0dSKonstantin Belousov static const u_int masks[] = { 7586be9f0dSKonstantin Belousov 0x000f, /* nd == 0 */ 7686be9f0dSKonstantin Belousov 0x002f, /* nd == 1 */ 7786be9f0dSKonstantin Belousov 0x00ff, /* nd == 2 */ 7886be9f0dSKonstantin Belousov 0x02ff, /* nd == 3 */ 7986be9f0dSKonstantin Belousov 0x0fff, /* nd == 4 */ 8086be9f0dSKonstantin Belousov 0x2fff, /* nd == 5 */ 8186be9f0dSKonstantin Belousov 0xffff, /* nd == 6 */ 8286be9f0dSKonstantin Belousov 0x0000, /* nd == 7 reserved */ 8386be9f0dSKonstantin Belousov }; 8486be9f0dSKonstantin Belousov 8586be9f0dSKonstantin Belousov KASSERT(nd <= 6, ("number of domains %d", nd)); 8686be9f0dSKonstantin Belousov return (masks[nd]); 8786be9f0dSKonstantin Belousov } 8886be9f0dSKonstantin Belousov 8986be9f0dSKonstantin Belousov static const struct sagaw_bits_tag { 9086be9f0dSKonstantin Belousov int agaw; 9186be9f0dSKonstantin Belousov int cap; 9286be9f0dSKonstantin Belousov int awlvl; 9386be9f0dSKonstantin Belousov int pglvl; 9486be9f0dSKonstantin Belousov } sagaw_bits[] = { 9586be9f0dSKonstantin Belousov {.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL, 9686be9f0dSKonstantin Belousov .pglvl = 2}, 9786be9f0dSKonstantin Belousov {.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL, 9886be9f0dSKonstantin Belousov .pglvl = 3}, 9986be9f0dSKonstantin Belousov {.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL, 10086be9f0dSKonstantin Belousov .pglvl = 4}, 10186be9f0dSKonstantin Belousov {.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL, 10286be9f0dSKonstantin Belousov .pglvl = 5}, 10386be9f0dSKonstantin Belousov {.agaw = 64, .cap = DMAR_CAP_SAGAW_6LVL, .awlvl = DMAR_CTX2_AW_6LVL, 10486be9f0dSKonstantin Belousov .pglvl = 6} 10586be9f0dSKonstantin Belousov }; 10686be9f0dSKonstantin Belousov 10786be9f0dSKonstantin Belousov bool 10886be9f0dSKonstantin Belousov dmar_pglvl_supported(struct dmar_unit *unit, int pglvl) 10986be9f0dSKonstantin Belousov { 11086be9f0dSKonstantin Belousov int i; 11186be9f0dSKonstantin Belousov 1121abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 11386be9f0dSKonstantin Belousov if (sagaw_bits[i].pglvl != pglvl) 11486be9f0dSKonstantin Belousov continue; 11586be9f0dSKonstantin Belousov if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 11686be9f0dSKonstantin Belousov return (true); 11786be9f0dSKonstantin Belousov } 11886be9f0dSKonstantin Belousov return (false); 11986be9f0dSKonstantin Belousov } 12086be9f0dSKonstantin Belousov 12186be9f0dSKonstantin Belousov int 1221abfd355SKonstantin Belousov domain_set_agaw(struct dmar_domain *domain, int mgaw) 12386be9f0dSKonstantin Belousov { 12486be9f0dSKonstantin Belousov int sagaw, i; 12586be9f0dSKonstantin Belousov 1261abfd355SKonstantin Belousov domain->mgaw = mgaw; 1271abfd355SKonstantin Belousov sagaw = DMAR_CAP_SAGAW(domain->dmar->hw_cap); 1281abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 12986be9f0dSKonstantin Belousov if (sagaw_bits[i].agaw >= mgaw) { 1301abfd355SKonstantin Belousov domain->agaw = sagaw_bits[i].agaw; 1311abfd355SKonstantin Belousov domain->pglvl = sagaw_bits[i].pglvl; 1321abfd355SKonstantin Belousov domain->awlvl = sagaw_bits[i].awlvl; 13386be9f0dSKonstantin Belousov return (0); 13486be9f0dSKonstantin Belousov } 13586be9f0dSKonstantin Belousov } 1361abfd355SKonstantin Belousov device_printf(domain->dmar->dev, 1371abfd355SKonstantin Belousov "context request mgaw %d: no agaw found, sagaw %x\n", 1381abfd355SKonstantin Belousov mgaw, sagaw); 13986be9f0dSKonstantin Belousov return (EINVAL); 14086be9f0dSKonstantin Belousov } 14186be9f0dSKonstantin Belousov 14286be9f0dSKonstantin Belousov /* 14386be9f0dSKonstantin Belousov * Find a best fit mgaw for the given maxaddr: 14486be9f0dSKonstantin Belousov * - if allow_less is false, must find sagaw which maps all requested 14586be9f0dSKonstantin Belousov * addresses (used by identity mappings); 14686be9f0dSKonstantin Belousov * - if allow_less is true, and no supported sagaw can map all requested 14786be9f0dSKonstantin Belousov * address space, accept the biggest sagaw, whatever is it. 14886be9f0dSKonstantin Belousov */ 14986be9f0dSKonstantin Belousov int 15059e37c8aSRuslan Bukin dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr, bool allow_less) 15186be9f0dSKonstantin Belousov { 15286be9f0dSKonstantin Belousov int i; 15386be9f0dSKonstantin Belousov 1541abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 15586be9f0dSKonstantin Belousov if ((1ULL << sagaw_bits[i].agaw) >= maxaddr && 15686be9f0dSKonstantin Belousov (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 15786be9f0dSKonstantin Belousov break; 15886be9f0dSKonstantin Belousov } 1591abfd355SKonstantin Belousov if (allow_less && i == nitems(sagaw_bits)) { 16086be9f0dSKonstantin Belousov do { 16186be9f0dSKonstantin Belousov i--; 16286be9f0dSKonstantin Belousov } while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) 16386be9f0dSKonstantin Belousov == 0); 16486be9f0dSKonstantin Belousov } 1651abfd355SKonstantin Belousov if (i < nitems(sagaw_bits)) 16686be9f0dSKonstantin Belousov return (sagaw_bits[i].agaw); 16786be9f0dSKonstantin Belousov KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d", 16886be9f0dSKonstantin Belousov (uintmax_t) maxaddr, allow_less)); 16986be9f0dSKonstantin Belousov return (-1); 17086be9f0dSKonstantin Belousov } 17186be9f0dSKonstantin Belousov 17286be9f0dSKonstantin Belousov /* 17386be9f0dSKonstantin Belousov * Calculate the total amount of page table pages needed to map the 17486be9f0dSKonstantin Belousov * whole bus address space on the context with the selected agaw. 17586be9f0dSKonstantin Belousov */ 17686be9f0dSKonstantin Belousov vm_pindex_t 17786be9f0dSKonstantin Belousov pglvl_max_pages(int pglvl) 17886be9f0dSKonstantin Belousov { 17986be9f0dSKonstantin Belousov vm_pindex_t res; 18086be9f0dSKonstantin Belousov int i; 18186be9f0dSKonstantin Belousov 18286be9f0dSKonstantin Belousov for (res = 0, i = pglvl; i > 0; i--) { 18386be9f0dSKonstantin Belousov res *= DMAR_NPTEPG; 18486be9f0dSKonstantin Belousov res++; 18586be9f0dSKonstantin Belousov } 18686be9f0dSKonstantin Belousov return (res); 18786be9f0dSKonstantin Belousov } 18886be9f0dSKonstantin Belousov 18986be9f0dSKonstantin Belousov /* 19086be9f0dSKonstantin Belousov * Return true if the page table level lvl supports the superpage for 19186be9f0dSKonstantin Belousov * the context ctx. 19286be9f0dSKonstantin Belousov */ 19386be9f0dSKonstantin Belousov int 1941abfd355SKonstantin Belousov domain_is_sp_lvl(struct dmar_domain *domain, int lvl) 19586be9f0dSKonstantin Belousov { 19686be9f0dSKonstantin Belousov int alvl, cap_sps; 19786be9f0dSKonstantin Belousov static const int sagaw_sp[] = { 19886be9f0dSKonstantin Belousov DMAR_CAP_SPS_2M, 19986be9f0dSKonstantin Belousov DMAR_CAP_SPS_1G, 20086be9f0dSKonstantin Belousov DMAR_CAP_SPS_512G, 20186be9f0dSKonstantin Belousov DMAR_CAP_SPS_1T 20286be9f0dSKonstantin Belousov }; 20386be9f0dSKonstantin Belousov 2041abfd355SKonstantin Belousov alvl = domain->pglvl - lvl - 1; 2051abfd355SKonstantin Belousov cap_sps = DMAR_CAP_SPS(domain->dmar->hw_cap); 2061abfd355SKonstantin Belousov return (alvl < nitems(sagaw_sp) && (sagaw_sp[alvl] & cap_sps) != 0); 20786be9f0dSKonstantin Belousov } 20886be9f0dSKonstantin Belousov 20959e37c8aSRuslan Bukin iommu_gaddr_t 21086be9f0dSKonstantin Belousov pglvl_page_size(int total_pglvl, int lvl) 21186be9f0dSKonstantin Belousov { 21286be9f0dSKonstantin Belousov int rlvl; 21359e37c8aSRuslan Bukin static const iommu_gaddr_t pg_sz[] = { 21459e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE, 21559e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE << DMAR_NPTEPGSHIFT, 21659e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE << (2 * DMAR_NPTEPGSHIFT), 21759e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE << (3 * DMAR_NPTEPGSHIFT), 21859e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE << (4 * DMAR_NPTEPGSHIFT), 21959e37c8aSRuslan Bukin (iommu_gaddr_t)DMAR_PAGE_SIZE << (5 * DMAR_NPTEPGSHIFT) 22086be9f0dSKonstantin Belousov }; 22186be9f0dSKonstantin Belousov 22286be9f0dSKonstantin Belousov KASSERT(lvl >= 0 && lvl < total_pglvl, 22386be9f0dSKonstantin Belousov ("total %d lvl %d", total_pglvl, lvl)); 22486be9f0dSKonstantin Belousov rlvl = total_pglvl - lvl - 1; 2251abfd355SKonstantin Belousov KASSERT(rlvl < nitems(pg_sz), ("sizeof pg_sz lvl %d", lvl)); 22686be9f0dSKonstantin Belousov return (pg_sz[rlvl]); 22786be9f0dSKonstantin Belousov } 22886be9f0dSKonstantin Belousov 22959e37c8aSRuslan Bukin iommu_gaddr_t 2301abfd355SKonstantin Belousov domain_page_size(struct dmar_domain *domain, int lvl) 23186be9f0dSKonstantin Belousov { 23286be9f0dSKonstantin Belousov 2331abfd355SKonstantin Belousov return (pglvl_page_size(domain->pglvl, lvl)); 23486be9f0dSKonstantin Belousov } 23586be9f0dSKonstantin Belousov 23668eeb96aSKonstantin Belousov int 23759e37c8aSRuslan Bukin calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size, 23859e37c8aSRuslan Bukin iommu_gaddr_t *isizep) 23968eeb96aSKonstantin Belousov { 24059e37c8aSRuslan Bukin iommu_gaddr_t isize; 24168eeb96aSKonstantin Belousov int am; 24268eeb96aSKonstantin Belousov 24368eeb96aSKonstantin Belousov for (am = DMAR_CAP_MAMV(unit->hw_cap);; am--) { 24468eeb96aSKonstantin Belousov isize = 1ULL << (am + DMAR_PAGE_SHIFT); 24568eeb96aSKonstantin Belousov if ((base & (isize - 1)) == 0 && size >= isize) 24668eeb96aSKonstantin Belousov break; 24768eeb96aSKonstantin Belousov if (am == 0) 24868eeb96aSKonstantin Belousov break; 24968eeb96aSKonstantin Belousov } 25068eeb96aSKonstantin Belousov *isizep = isize; 25168eeb96aSKonstantin Belousov return (am); 25268eeb96aSKonstantin Belousov } 25368eeb96aSKonstantin Belousov 25459e37c8aSRuslan Bukin iommu_haddr_t dmar_high; 25586be9f0dSKonstantin Belousov int haw; 25686be9f0dSKonstantin Belousov int dmar_tbl_pagecnt; 25786be9f0dSKonstantin Belousov 25886be9f0dSKonstantin Belousov vm_page_t 25986be9f0dSKonstantin Belousov dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags) 26086be9f0dSKonstantin Belousov { 26186be9f0dSKonstantin Belousov vm_page_t m; 262dc00696aSKonstantin Belousov int zeroed, aflags; 26386be9f0dSKonstantin Belousov 26415f6baf4SRuslan Bukin zeroed = (flags & IOMMU_PGF_ZERO) != 0 ? VM_ALLOC_ZERO : 0; 265dc00696aSKonstantin Belousov aflags = zeroed | VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | VM_ALLOC_NODUMP | 26615f6baf4SRuslan Bukin ((flags & IOMMU_PGF_WAITOK) != 0 ? VM_ALLOC_WAITFAIL : 267dc00696aSKonstantin Belousov VM_ALLOC_NOWAIT); 26886be9f0dSKonstantin Belousov for (;;) { 26915f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 27086be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 27186be9f0dSKonstantin Belousov m = vm_page_lookup(obj, idx); 27215f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_NOALLOC) != 0 || m != NULL) { 27315f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 27486be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 27586be9f0dSKonstantin Belousov break; 27686be9f0dSKonstantin Belousov } 277dc00696aSKonstantin Belousov m = vm_page_alloc_contig(obj, idx, aflags, 1, 0, 27886be9f0dSKonstantin Belousov dmar_high, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 27915f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 28086be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 28186be9f0dSKonstantin Belousov if (m != NULL) { 28286be9f0dSKonstantin Belousov if (zeroed && (m->flags & PG_ZERO) == 0) 28386be9f0dSKonstantin Belousov pmap_zero_page(m); 28486be9f0dSKonstantin Belousov atomic_add_int(&dmar_tbl_pagecnt, 1); 28586be9f0dSKonstantin Belousov break; 28686be9f0dSKonstantin Belousov } 28715f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_WAITOK) == 0) 28886be9f0dSKonstantin Belousov break; 28986be9f0dSKonstantin Belousov } 29086be9f0dSKonstantin Belousov return (m); 29186be9f0dSKonstantin Belousov } 29286be9f0dSKonstantin Belousov 29386be9f0dSKonstantin Belousov void 29486be9f0dSKonstantin Belousov dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags) 29586be9f0dSKonstantin Belousov { 29686be9f0dSKonstantin Belousov vm_page_t m; 29786be9f0dSKonstantin Belousov 29815f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 29986be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 3000f9e06e1SJeff Roberson m = vm_page_grab(obj, idx, VM_ALLOC_NOCREAT); 30186be9f0dSKonstantin Belousov if (m != NULL) { 30286be9f0dSKonstantin Belousov vm_page_free(m); 30386be9f0dSKonstantin Belousov atomic_subtract_int(&dmar_tbl_pagecnt, 1); 30486be9f0dSKonstantin Belousov } 30515f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 30686be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 30786be9f0dSKonstantin Belousov } 30886be9f0dSKonstantin Belousov 30986be9f0dSKonstantin Belousov void * 31086be9f0dSKonstantin Belousov dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags, 31186be9f0dSKonstantin Belousov struct sf_buf **sf) 31286be9f0dSKonstantin Belousov { 31386be9f0dSKonstantin Belousov vm_page_t m; 31486be9f0dSKonstantin Belousov bool allocated; 31586be9f0dSKonstantin Belousov 31615f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 31786be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 31886be9f0dSKonstantin Belousov m = vm_page_lookup(obj, idx); 31915f6baf4SRuslan Bukin if (m == NULL && (flags & IOMMU_PGF_ALLOC) != 0) { 32015f6baf4SRuslan Bukin m = dmar_pgalloc(obj, idx, flags | IOMMU_PGF_OBJL); 32186be9f0dSKonstantin Belousov allocated = true; 32286be9f0dSKonstantin Belousov } else 32386be9f0dSKonstantin Belousov allocated = false; 32486be9f0dSKonstantin Belousov if (m == NULL) { 32515f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 32686be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 32786be9f0dSKonstantin Belousov return (NULL); 32886be9f0dSKonstantin Belousov } 32986be9f0dSKonstantin Belousov /* Sleepable allocations cannot fail. */ 33015f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_WAITOK) != 0) 33186be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 33286be9f0dSKonstantin Belousov sched_pin(); 33315f6baf4SRuslan Bukin *sf = sf_buf_alloc(m, SFB_CPUPRIVATE | ((flags & IOMMU_PGF_WAITOK) 33486be9f0dSKonstantin Belousov == 0 ? SFB_NOWAIT : 0)); 33586be9f0dSKonstantin Belousov if (*sf == NULL) { 33686be9f0dSKonstantin Belousov sched_unpin(); 33786be9f0dSKonstantin Belousov if (allocated) { 33886be9f0dSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(obj); 33915f6baf4SRuslan Bukin dmar_pgfree(obj, m->pindex, flags | IOMMU_PGF_OBJL); 34086be9f0dSKonstantin Belousov } 34115f6baf4SRuslan Bukin if ((flags & IOMMU_PGF_OBJL) == 0) 34286be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 34386be9f0dSKonstantin Belousov return (NULL); 34486be9f0dSKonstantin Belousov } 34515f6baf4SRuslan Bukin if ((flags & (IOMMU_PGF_WAITOK | IOMMU_PGF_OBJL)) == 34615f6baf4SRuslan Bukin (IOMMU_PGF_WAITOK | IOMMU_PGF_OBJL)) 34786be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 34815f6baf4SRuslan Bukin else if ((flags & (IOMMU_PGF_WAITOK | IOMMU_PGF_OBJL)) == 0) 34986be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 35086be9f0dSKonstantin Belousov return ((void *)sf_buf_kva(*sf)); 35186be9f0dSKonstantin Belousov } 35286be9f0dSKonstantin Belousov 35386be9f0dSKonstantin Belousov void 3546b7c46afSKonstantin Belousov dmar_unmap_pgtbl(struct sf_buf *sf) 35586be9f0dSKonstantin Belousov { 35686be9f0dSKonstantin Belousov 35786be9f0dSKonstantin Belousov sf_buf_free(sf); 35886be9f0dSKonstantin Belousov sched_unpin(); 3596b7c46afSKonstantin Belousov } 36086be9f0dSKonstantin Belousov 3616b7c46afSKonstantin Belousov static void 3626b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz) 3636b7c46afSKonstantin Belousov { 3646b7c46afSKonstantin Belousov 3656b7c46afSKonstantin Belousov if (DMAR_IS_COHERENT(unit)) 3666b7c46afSKonstantin Belousov return; 36786be9f0dSKonstantin Belousov /* 36886be9f0dSKonstantin Belousov * If DMAR does not snoop paging structures accesses, flush 36986be9f0dSKonstantin Belousov * CPU cache to memory. 37086be9f0dSKonstantin Belousov */ 371d12c4465SKonstantin Belousov pmap_force_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz); 3726b7c46afSKonstantin Belousov } 3736b7c46afSKonstantin Belousov 3746b7c46afSKonstantin Belousov void 3756b7c46afSKonstantin Belousov dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst) 3766b7c46afSKonstantin Belousov { 3776b7c46afSKonstantin Belousov 3786b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 3796b7c46afSKonstantin Belousov } 3806b7c46afSKonstantin Belousov 3816b7c46afSKonstantin Belousov void 3826b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst) 3836b7c46afSKonstantin Belousov { 3846b7c46afSKonstantin Belousov 3856b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 3866b7c46afSKonstantin Belousov } 3876b7c46afSKonstantin Belousov 3886b7c46afSKonstantin Belousov void 3896b7c46afSKonstantin Belousov dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst) 3906b7c46afSKonstantin Belousov { 3916b7c46afSKonstantin Belousov 3926b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 39386be9f0dSKonstantin Belousov } 39486be9f0dSKonstantin Belousov 39586be9f0dSKonstantin Belousov /* 39686be9f0dSKonstantin Belousov * Load the root entry pointer into the hardware, busily waiting for 39786be9f0dSKonstantin Belousov * the completion. 39886be9f0dSKonstantin Belousov */ 39986be9f0dSKonstantin Belousov int 40086be9f0dSKonstantin Belousov dmar_load_root_entry_ptr(struct dmar_unit *unit) 40186be9f0dSKonstantin Belousov { 40286be9f0dSKonstantin Belousov vm_page_t root_entry; 403476358b3SKonstantin Belousov int error; 40486be9f0dSKonstantin Belousov 40586be9f0dSKonstantin Belousov /* 40686be9f0dSKonstantin Belousov * Access to the GCMD register must be serialized while the 40786be9f0dSKonstantin Belousov * command is submitted. 40886be9f0dSKonstantin Belousov */ 40986be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 41086be9f0dSKonstantin Belousov 4115a49ae8eSKonstantin Belousov VM_OBJECT_RLOCK(unit->ctx_obj); 41286be9f0dSKonstantin Belousov root_entry = vm_page_lookup(unit->ctx_obj, 0); 4135a49ae8eSKonstantin Belousov VM_OBJECT_RUNLOCK(unit->ctx_obj); 41486be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry)); 41586be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP); 416476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS) 417476358b3SKonstantin Belousov != 0)); 418476358b3SKonstantin Belousov return (error); 41986be9f0dSKonstantin Belousov } 42086be9f0dSKonstantin Belousov 42186be9f0dSKonstantin Belousov /* 42286be9f0dSKonstantin Belousov * Globally invalidate the context entries cache, busily waiting for 42386be9f0dSKonstantin Belousov * the completion. 42486be9f0dSKonstantin Belousov */ 42586be9f0dSKonstantin Belousov int 42686be9f0dSKonstantin Belousov dmar_inv_ctx_glob(struct dmar_unit *unit) 42786be9f0dSKonstantin Belousov { 428476358b3SKonstantin Belousov int error; 42986be9f0dSKonstantin Belousov 43086be9f0dSKonstantin Belousov /* 43186be9f0dSKonstantin Belousov * Access to the CCMD register must be serialized while the 43286be9f0dSKonstantin Belousov * command is submitted. 43386be9f0dSKonstantin Belousov */ 43486be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 43568eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 43686be9f0dSKonstantin Belousov 43786be9f0dSKonstantin Belousov /* 43886be9f0dSKonstantin Belousov * The DMAR_CCMD_ICC bit in the upper dword should be written 43986be9f0dSKonstantin Belousov * after the low dword write is completed. Amd64 44086be9f0dSKonstantin Belousov * dmar_write8() does not have this issue, i386 dmar_write8() 44186be9f0dSKonstantin Belousov * writes the upper dword last. 44286be9f0dSKonstantin Belousov */ 44386be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB); 444476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32) 445476358b3SKonstantin Belousov == 0)); 446476358b3SKonstantin Belousov return (error); 44786be9f0dSKonstantin Belousov } 44886be9f0dSKonstantin Belousov 44986be9f0dSKonstantin Belousov /* 45086be9f0dSKonstantin Belousov * Globally invalidate the IOTLB, busily waiting for the completion. 45186be9f0dSKonstantin Belousov */ 45286be9f0dSKonstantin Belousov int 45386be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(struct dmar_unit *unit) 45486be9f0dSKonstantin Belousov { 455476358b3SKonstantin Belousov int error, reg; 45686be9f0dSKonstantin Belousov 45786be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 45868eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 45986be9f0dSKonstantin Belousov 46086be9f0dSKonstantin Belousov reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap); 46186be9f0dSKonstantin Belousov /* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */ 46286be9f0dSKonstantin Belousov dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT | 46386be9f0dSKonstantin Belousov DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW); 464476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) & 465476358b3SKonstantin Belousov DMAR_IOTLB_IVT32) == 0)); 466476358b3SKonstantin Belousov return (error); 46786be9f0dSKonstantin Belousov } 46886be9f0dSKonstantin Belousov 46986be9f0dSKonstantin Belousov /* 47086be9f0dSKonstantin Belousov * Flush the chipset write buffers. See 11.1 "Write Buffer Flushing" 47186be9f0dSKonstantin Belousov * in the architecture specification. 47286be9f0dSKonstantin Belousov */ 47386be9f0dSKonstantin Belousov int 47486be9f0dSKonstantin Belousov dmar_flush_write_bufs(struct dmar_unit *unit) 47586be9f0dSKonstantin Belousov { 476476358b3SKonstantin Belousov int error; 47786be9f0dSKonstantin Belousov 47886be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 47986be9f0dSKonstantin Belousov 48086be9f0dSKonstantin Belousov /* 48186be9f0dSKonstantin Belousov * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported. 48286be9f0dSKonstantin Belousov */ 48386be9f0dSKonstantin Belousov KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0, 48459e37c8aSRuslan Bukin ("dmar%d: no RWBF", unit->iommu.unit)); 48586be9f0dSKonstantin Belousov 48686be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF); 487476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS) 488476358b3SKonstantin Belousov != 0)); 489476358b3SKonstantin Belousov return (error); 49086be9f0dSKonstantin Belousov } 49186be9f0dSKonstantin Belousov 492*06e6ca6dSKornel Duleba /* 493*06e6ca6dSKornel Duleba * Some BIOSes protect memory region they reside in by using DMAR to 494*06e6ca6dSKornel Duleba * prevent devices from doing any DMA transactions to that part of RAM. 495*06e6ca6dSKornel Duleba * AMI refers to this as "DMA Control Guarantee". 496*06e6ca6dSKornel Duleba * We need to disable this when address translation is enabled. 497*06e6ca6dSKornel Duleba */ 498*06e6ca6dSKornel Duleba int 499*06e6ca6dSKornel Duleba dmar_disable_protected_regions(struct dmar_unit *unit) 500*06e6ca6dSKornel Duleba { 501*06e6ca6dSKornel Duleba uint32_t reg; 502*06e6ca6dSKornel Duleba int error; 503*06e6ca6dSKornel Duleba 504*06e6ca6dSKornel Duleba DMAR_ASSERT_LOCKED(unit); 505*06e6ca6dSKornel Duleba 506*06e6ca6dSKornel Duleba /* Check if we support the feature. */ 507*06e6ca6dSKornel Duleba if ((unit->hw_cap & (DMAR_CAP_PLMR | DMAR_CAP_PHMR)) == 0) 508*06e6ca6dSKornel Duleba return (0); 509*06e6ca6dSKornel Duleba 510*06e6ca6dSKornel Duleba reg = dmar_read4(unit, DMAR_PMEN_REG); 511*06e6ca6dSKornel Duleba reg &= ~DMAR_PMEN_EPM; 512*06e6ca6dSKornel Duleba dmar_write4(unit, DMAR_PMEN_REG, reg); 513*06e6ca6dSKornel Duleba DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_PMEN_REG) & DMAR_PMEN_PRS) 514*06e6ca6dSKornel Duleba != 0)); 515*06e6ca6dSKornel Duleba 516*06e6ca6dSKornel Duleba return (error); 517*06e6ca6dSKornel Duleba } 518*06e6ca6dSKornel Duleba 51986be9f0dSKonstantin Belousov int 52086be9f0dSKonstantin Belousov dmar_enable_translation(struct dmar_unit *unit) 52186be9f0dSKonstantin Belousov { 522476358b3SKonstantin Belousov int error; 52386be9f0dSKonstantin Belousov 52486be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 52586be9f0dSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_TE; 52686be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 527476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) 528476358b3SKonstantin Belousov != 0)); 529476358b3SKonstantin Belousov return (error); 53086be9f0dSKonstantin Belousov } 53186be9f0dSKonstantin Belousov 53286be9f0dSKonstantin Belousov int 53386be9f0dSKonstantin Belousov dmar_disable_translation(struct dmar_unit *unit) 53486be9f0dSKonstantin Belousov { 535476358b3SKonstantin Belousov int error; 53686be9f0dSKonstantin Belousov 53786be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 53886be9f0dSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_TE; 53986be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 540476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) 541476358b3SKonstantin Belousov == 0)); 542476358b3SKonstantin Belousov return (error); 54386be9f0dSKonstantin Belousov } 54486be9f0dSKonstantin Belousov 5450a110d5bSKonstantin Belousov int 5460a110d5bSKonstantin Belousov dmar_load_irt_ptr(struct dmar_unit *unit) 5470a110d5bSKonstantin Belousov { 5480a110d5bSKonstantin Belousov uint64_t irta, s; 549476358b3SKonstantin Belousov int error; 5500a110d5bSKonstantin Belousov 5510a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 5520a110d5bSKonstantin Belousov irta = unit->irt_phys; 5530a110d5bSKonstantin Belousov if (DMAR_X2APIC(unit)) 5540a110d5bSKonstantin Belousov irta |= DMAR_IRTA_EIME; 5550a110d5bSKonstantin Belousov s = fls(unit->irte_cnt) - 2; 5560a110d5bSKonstantin Belousov KASSERT(unit->irte_cnt >= 2 && s <= DMAR_IRTA_S_MASK && 5570a110d5bSKonstantin Belousov powerof2(unit->irte_cnt), 5580a110d5bSKonstantin Belousov ("IRTA_REG_S overflow %x", unit->irte_cnt)); 5590a110d5bSKonstantin Belousov irta |= s; 5600a110d5bSKonstantin Belousov dmar_write8(unit, DMAR_IRTA_REG, irta); 5610a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SIRTP); 562476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRTPS) 563476358b3SKonstantin Belousov != 0)); 564476358b3SKonstantin Belousov return (error); 5650a110d5bSKonstantin Belousov } 5660a110d5bSKonstantin Belousov 5670a110d5bSKonstantin Belousov int 5680a110d5bSKonstantin Belousov dmar_enable_ir(struct dmar_unit *unit) 5690a110d5bSKonstantin Belousov { 570476358b3SKonstantin Belousov int error; 5710a110d5bSKonstantin Belousov 5720a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 5730a110d5bSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_IRE; 5740a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_CFI; 5750a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 576476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES) 577476358b3SKonstantin Belousov != 0)); 578476358b3SKonstantin Belousov return (error); 5790a110d5bSKonstantin Belousov } 5800a110d5bSKonstantin Belousov 5810a110d5bSKonstantin Belousov int 5820a110d5bSKonstantin Belousov dmar_disable_ir(struct dmar_unit *unit) 5830a110d5bSKonstantin Belousov { 584476358b3SKonstantin Belousov int error; 5850a110d5bSKonstantin Belousov 5860a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 5870a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_IRE; 5880a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 589476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES) 590476358b3SKonstantin Belousov == 0)); 591476358b3SKonstantin Belousov return (error); 5920a110d5bSKonstantin Belousov } 5930a110d5bSKonstantin Belousov 59486be9f0dSKonstantin Belousov #define BARRIER_F \ 59586be9f0dSKonstantin Belousov u_int f_done, f_inproc, f_wakeup; \ 59686be9f0dSKonstantin Belousov \ 59786be9f0dSKonstantin Belousov f_done = 1 << (barrier_id * 3); \ 59886be9f0dSKonstantin Belousov f_inproc = 1 << (barrier_id * 3 + 1); \ 59986be9f0dSKonstantin Belousov f_wakeup = 1 << (barrier_id * 3 + 2) 60086be9f0dSKonstantin Belousov 60186be9f0dSKonstantin Belousov bool 60286be9f0dSKonstantin Belousov dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id) 60386be9f0dSKonstantin Belousov { 60486be9f0dSKonstantin Belousov BARRIER_F; 60586be9f0dSKonstantin Belousov 60686be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 60786be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_done) != 0) { 60886be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 60986be9f0dSKonstantin Belousov return (false); 61086be9f0dSKonstantin Belousov } 61186be9f0dSKonstantin Belousov 61286be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_inproc) != 0) { 61386be9f0dSKonstantin Belousov while ((dmar->barrier_flags & f_inproc) != 0) { 61486be9f0dSKonstantin Belousov dmar->barrier_flags |= f_wakeup; 61559e37c8aSRuslan Bukin msleep(&dmar->barrier_flags, &dmar->iommu.lock, 0, 61686be9f0dSKonstantin Belousov "dmarb", 0); 61786be9f0dSKonstantin Belousov } 61886be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & f_done) != 0, 61959e37c8aSRuslan Bukin ("dmar%d barrier %d missing done", dmar->iommu.unit, 62059e37c8aSRuslan Bukin barrier_id)); 62186be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 62286be9f0dSKonstantin Belousov return (false); 62386be9f0dSKonstantin Belousov } 62486be9f0dSKonstantin Belousov 62586be9f0dSKonstantin Belousov dmar->barrier_flags |= f_inproc; 62686be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 62786be9f0dSKonstantin Belousov return (true); 62886be9f0dSKonstantin Belousov } 62986be9f0dSKonstantin Belousov 63086be9f0dSKonstantin Belousov void 63186be9f0dSKonstantin Belousov dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id) 63286be9f0dSKonstantin Belousov { 63386be9f0dSKonstantin Belousov BARRIER_F; 63486be9f0dSKonstantin Belousov 63586be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar); 63686be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc, 63759e37c8aSRuslan Bukin ("dmar%d barrier %d missed entry", dmar->iommu.unit, barrier_id)); 63886be9f0dSKonstantin Belousov dmar->barrier_flags |= f_done; 63986be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_wakeup) != 0) 64086be9f0dSKonstantin Belousov wakeup(&dmar->barrier_flags); 64186be9f0dSKonstantin Belousov dmar->barrier_flags &= ~(f_inproc | f_wakeup); 64286be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 64386be9f0dSKonstantin Belousov } 64486be9f0dSKonstantin Belousov 645e164cafcSKonstantin Belousov int dmar_batch_coalesce = 100; 646476358b3SKonstantin Belousov struct timespec dmar_hw_timeout = { 647476358b3SKonstantin Belousov .tv_sec = 0, 648476358b3SKonstantin Belousov .tv_nsec = 1000000 649476358b3SKonstantin Belousov }; 650476358b3SKonstantin Belousov 651476358b3SKonstantin Belousov static const uint64_t d = 1000000000; 652476358b3SKonstantin Belousov 653476358b3SKonstantin Belousov void 654476358b3SKonstantin Belousov dmar_update_timeout(uint64_t newval) 655476358b3SKonstantin Belousov { 656476358b3SKonstantin Belousov 657476358b3SKonstantin Belousov /* XXXKIB not atomic */ 658476358b3SKonstantin Belousov dmar_hw_timeout.tv_sec = newval / d; 659476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec = newval % d; 660476358b3SKonstantin Belousov } 661476358b3SKonstantin Belousov 662476358b3SKonstantin Belousov uint64_t 663476358b3SKonstantin Belousov dmar_get_timeout(void) 664476358b3SKonstantin Belousov { 665476358b3SKonstantin Belousov 666476358b3SKonstantin Belousov return ((uint64_t)dmar_hw_timeout.tv_sec * d + 667476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec); 668476358b3SKonstantin Belousov } 669476358b3SKonstantin Belousov 670476358b3SKonstantin Belousov static int 671476358b3SKonstantin Belousov dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS) 672476358b3SKonstantin Belousov { 673476358b3SKonstantin Belousov uint64_t val; 674476358b3SKonstantin Belousov int error; 675476358b3SKonstantin Belousov 676476358b3SKonstantin Belousov val = dmar_get_timeout(); 677476358b3SKonstantin Belousov error = sysctl_handle_long(oidp, &val, 0, req); 678476358b3SKonstantin Belousov if (error != 0 || req->newptr == NULL) 679476358b3SKonstantin Belousov return (error); 680476358b3SKonstantin Belousov dmar_update_timeout(val); 681476358b3SKonstantin Belousov return (error); 682476358b3SKonstantin Belousov } 68386be9f0dSKonstantin Belousov 684357149f0SRuslan Bukin static SYSCTL_NODE(_hw_iommu, OID_AUTO, dmar, CTLFLAG_RD | CTLFLAG_MPSAFE, 685357149f0SRuslan Bukin NULL, ""); 686357149f0SRuslan Bukin SYSCTL_INT(_hw_iommu_dmar, OID_AUTO, tbl_pagecnt, CTLFLAG_RD, 68786be9f0dSKonstantin Belousov &dmar_tbl_pagecnt, 0, 68886be9f0dSKonstantin Belousov "Count of pages used for DMAR pagetables"); 689357149f0SRuslan Bukin SYSCTL_INT(_hw_iommu_dmar, OID_AUTO, batch_coalesce, CTLFLAG_RWTUN, 690e164cafcSKonstantin Belousov &dmar_batch_coalesce, 0, 691e164cafcSKonstantin Belousov "Number of qi batches between interrupt"); 692357149f0SRuslan Bukin SYSCTL_PROC(_hw_iommu_dmar, OID_AUTO, timeout, 693476358b3SKonstantin Belousov CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0, 694476358b3SKonstantin Belousov dmar_timeout_sysctl, "QU", 695476358b3SKonstantin Belousov "Timeout for command wait, in nanoseconds"); 696