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/bus.h> 3386be9f0dSKonstantin Belousov #include <sys/kernel.h> 3486be9f0dSKonstantin Belousov #include <sys/lock.h> 3586be9f0dSKonstantin Belousov #include <sys/malloc.h> 3686be9f0dSKonstantin Belousov #include <sys/memdesc.h> 3786be9f0dSKonstantin Belousov #include <sys/mutex.h> 3886be9f0dSKonstantin Belousov #include <sys/proc.h> 3986be9f0dSKonstantin Belousov #include <sys/queue.h> 4086be9f0dSKonstantin Belousov #include <sys/rman.h> 4186be9f0dSKonstantin Belousov #include <sys/rwlock.h> 4286be9f0dSKonstantin Belousov #include <sys/sched.h> 4386be9f0dSKonstantin Belousov #include <sys/sf_buf.h> 4486be9f0dSKonstantin Belousov #include <sys/sysctl.h> 4586be9f0dSKonstantin Belousov #include <sys/systm.h> 4686be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 47476358b3SKonstantin Belousov #include <sys/time.h> 4886be9f0dSKonstantin Belousov #include <sys/tree.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_map.h> 5686be9f0dSKonstantin Belousov #include <vm/vm_pageout.h> 57c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h> 58c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h> 5986be9f0dSKonstantin Belousov #include <machine/bus.h> 6086be9f0dSKonstantin Belousov #include <machine/cpu.h> 610a110d5bSKonstantin Belousov #include <machine/intr_machdep.h> 620a110d5bSKonstantin Belousov #include <x86/include/apicvar.h> 6386be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h> 64f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h> 65c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h> 6640d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h> 6786be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 6886be9f0dSKonstantin Belousov 6986be9f0dSKonstantin Belousov u_int 7086be9f0dSKonstantin Belousov dmar_nd2mask(u_int nd) 7186be9f0dSKonstantin Belousov { 7286be9f0dSKonstantin Belousov static const u_int masks[] = { 7386be9f0dSKonstantin Belousov 0x000f, /* nd == 0 */ 7486be9f0dSKonstantin Belousov 0x002f, /* nd == 1 */ 7586be9f0dSKonstantin Belousov 0x00ff, /* nd == 2 */ 7686be9f0dSKonstantin Belousov 0x02ff, /* nd == 3 */ 7786be9f0dSKonstantin Belousov 0x0fff, /* nd == 4 */ 7886be9f0dSKonstantin Belousov 0x2fff, /* nd == 5 */ 7986be9f0dSKonstantin Belousov 0xffff, /* nd == 6 */ 8086be9f0dSKonstantin Belousov 0x0000, /* nd == 7 reserved */ 8186be9f0dSKonstantin Belousov }; 8286be9f0dSKonstantin Belousov 8386be9f0dSKonstantin Belousov KASSERT(nd <= 6, ("number of domains %d", nd)); 8486be9f0dSKonstantin Belousov return (masks[nd]); 8586be9f0dSKonstantin Belousov } 8686be9f0dSKonstantin Belousov 8786be9f0dSKonstantin Belousov static const struct sagaw_bits_tag { 8886be9f0dSKonstantin Belousov int agaw; 8986be9f0dSKonstantin Belousov int cap; 9086be9f0dSKonstantin Belousov int awlvl; 9186be9f0dSKonstantin Belousov int pglvl; 9286be9f0dSKonstantin Belousov } sagaw_bits[] = { 9386be9f0dSKonstantin Belousov {.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL, 9486be9f0dSKonstantin Belousov .pglvl = 2}, 9586be9f0dSKonstantin Belousov {.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL, 9686be9f0dSKonstantin Belousov .pglvl = 3}, 9786be9f0dSKonstantin Belousov {.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL, 9886be9f0dSKonstantin Belousov .pglvl = 4}, 9986be9f0dSKonstantin Belousov {.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL, 1006f378116SJason A. Harmening .pglvl = 5} 1016f378116SJason A. Harmening /* 1026f378116SJason A. Harmening * 6-level paging (DMAR_CAP_SAGAW_6LVL) is not supported on any 1036f378116SJason A. Harmening * current VT-d hardware and its SAGAW field value is listed as 1046f378116SJason A. Harmening * reserved in the VT-d spec. If support is added in the future, 1056f378116SJason A. Harmening * this structure and the logic in dmar_maxaddr2mgaw() will need 1066f378116SJason A. Harmening * to change to avoid attempted comparison against 1ULL << 64. 1076f378116SJason A. Harmening */ 10886be9f0dSKonstantin Belousov }; 10986be9f0dSKonstantin Belousov 11086be9f0dSKonstantin Belousov bool 11186be9f0dSKonstantin Belousov dmar_pglvl_supported(struct dmar_unit *unit, int pglvl) 11286be9f0dSKonstantin Belousov { 11386be9f0dSKonstantin Belousov int i; 11486be9f0dSKonstantin Belousov 1151abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 11686be9f0dSKonstantin Belousov if (sagaw_bits[i].pglvl != pglvl) 11786be9f0dSKonstantin Belousov continue; 11886be9f0dSKonstantin Belousov if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 11986be9f0dSKonstantin Belousov return (true); 12086be9f0dSKonstantin Belousov } 12186be9f0dSKonstantin Belousov return (false); 12286be9f0dSKonstantin Belousov } 12386be9f0dSKonstantin Belousov 12486be9f0dSKonstantin Belousov int 1251abfd355SKonstantin Belousov domain_set_agaw(struct dmar_domain *domain, int mgaw) 12686be9f0dSKonstantin Belousov { 12786be9f0dSKonstantin Belousov int sagaw, i; 12886be9f0dSKonstantin Belousov 1291abfd355SKonstantin Belousov domain->mgaw = mgaw; 1301abfd355SKonstantin Belousov sagaw = DMAR_CAP_SAGAW(domain->dmar->hw_cap); 1311abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 13286be9f0dSKonstantin Belousov if (sagaw_bits[i].agaw >= mgaw) { 1331abfd355SKonstantin Belousov domain->agaw = sagaw_bits[i].agaw; 1341abfd355SKonstantin Belousov domain->pglvl = sagaw_bits[i].pglvl; 1351abfd355SKonstantin Belousov domain->awlvl = sagaw_bits[i].awlvl; 13686be9f0dSKonstantin Belousov return (0); 13786be9f0dSKonstantin Belousov } 13886be9f0dSKonstantin Belousov } 139*164fdee1SKonstantin Belousov device_printf(domain->dmar->iommu.dev, 1401abfd355SKonstantin Belousov "context request mgaw %d: no agaw found, sagaw %x\n", 1411abfd355SKonstantin Belousov mgaw, sagaw); 14286be9f0dSKonstantin Belousov return (EINVAL); 14386be9f0dSKonstantin Belousov } 14486be9f0dSKonstantin Belousov 14586be9f0dSKonstantin Belousov /* 14686be9f0dSKonstantin Belousov * Find a best fit mgaw for the given maxaddr: 14786be9f0dSKonstantin Belousov * - if allow_less is false, must find sagaw which maps all requested 14886be9f0dSKonstantin Belousov * addresses (used by identity mappings); 14986be9f0dSKonstantin Belousov * - if allow_less is true, and no supported sagaw can map all requested 15086be9f0dSKonstantin Belousov * address space, accept the biggest sagaw, whatever is it. 15186be9f0dSKonstantin Belousov */ 15286be9f0dSKonstantin Belousov int 15359e37c8aSRuslan Bukin dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr, bool allow_less) 15486be9f0dSKonstantin Belousov { 15586be9f0dSKonstantin Belousov int i; 15686be9f0dSKonstantin Belousov 1571abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) { 15886be9f0dSKonstantin Belousov if ((1ULL << sagaw_bits[i].agaw) >= maxaddr && 15986be9f0dSKonstantin Belousov (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 16086be9f0dSKonstantin Belousov break; 16186be9f0dSKonstantin Belousov } 1621abfd355SKonstantin Belousov if (allow_less && i == nitems(sagaw_bits)) { 16386be9f0dSKonstantin Belousov do { 16486be9f0dSKonstantin Belousov i--; 16586be9f0dSKonstantin Belousov } while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) 16686be9f0dSKonstantin Belousov == 0); 16786be9f0dSKonstantin Belousov } 1681abfd355SKonstantin Belousov if (i < nitems(sagaw_bits)) 16986be9f0dSKonstantin Belousov return (sagaw_bits[i].agaw); 17086be9f0dSKonstantin Belousov KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d", 17186be9f0dSKonstantin Belousov (uintmax_t) maxaddr, allow_less)); 17286be9f0dSKonstantin Belousov return (-1); 17386be9f0dSKonstantin Belousov } 17486be9f0dSKonstantin Belousov 17586be9f0dSKonstantin Belousov /* 17686be9f0dSKonstantin Belousov * Calculate the total amount of page table pages needed to map the 17786be9f0dSKonstantin Belousov * whole bus address space on the context with the selected agaw. 17886be9f0dSKonstantin Belousov */ 17986be9f0dSKonstantin Belousov vm_pindex_t 18086be9f0dSKonstantin Belousov pglvl_max_pages(int pglvl) 18186be9f0dSKonstantin Belousov { 18286be9f0dSKonstantin Belousov vm_pindex_t res; 18386be9f0dSKonstantin Belousov int i; 18486be9f0dSKonstantin Belousov 18586be9f0dSKonstantin Belousov for (res = 0, i = pglvl; i > 0; i--) { 18640d951bcSKonstantin Belousov res *= IOMMU_NPTEPG; 18786be9f0dSKonstantin Belousov res++; 18886be9f0dSKonstantin Belousov } 18986be9f0dSKonstantin Belousov return (res); 19086be9f0dSKonstantin Belousov } 19186be9f0dSKonstantin Belousov 19286be9f0dSKonstantin Belousov /* 19386be9f0dSKonstantin Belousov * Return true if the page table level lvl supports the superpage for 19486be9f0dSKonstantin Belousov * the context ctx. 19586be9f0dSKonstantin Belousov */ 19686be9f0dSKonstantin Belousov int 1971abfd355SKonstantin Belousov domain_is_sp_lvl(struct dmar_domain *domain, int lvl) 19886be9f0dSKonstantin Belousov { 19986be9f0dSKonstantin Belousov int alvl, cap_sps; 20086be9f0dSKonstantin Belousov static const int sagaw_sp[] = { 20186be9f0dSKonstantin Belousov DMAR_CAP_SPS_2M, 20286be9f0dSKonstantin Belousov DMAR_CAP_SPS_1G, 20386be9f0dSKonstantin Belousov DMAR_CAP_SPS_512G, 20486be9f0dSKonstantin Belousov DMAR_CAP_SPS_1T 20586be9f0dSKonstantin Belousov }; 20686be9f0dSKonstantin Belousov 2071abfd355SKonstantin Belousov alvl = domain->pglvl - lvl - 1; 2081abfd355SKonstantin Belousov cap_sps = DMAR_CAP_SPS(domain->dmar->hw_cap); 2091abfd355SKonstantin Belousov return (alvl < nitems(sagaw_sp) && (sagaw_sp[alvl] & cap_sps) != 0); 21086be9f0dSKonstantin Belousov } 21186be9f0dSKonstantin Belousov 21259e37c8aSRuslan Bukin iommu_gaddr_t 21386be9f0dSKonstantin Belousov pglvl_page_size(int total_pglvl, int lvl) 21486be9f0dSKonstantin Belousov { 21586be9f0dSKonstantin Belousov int rlvl; 21659e37c8aSRuslan Bukin static const iommu_gaddr_t pg_sz[] = { 21740d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE, 21840d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE << IOMMU_NPTEPGSHIFT, 21940d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE << (2 * IOMMU_NPTEPGSHIFT), 22040d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE << (3 * IOMMU_NPTEPGSHIFT), 22140d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE << (4 * IOMMU_NPTEPGSHIFT), 22240d951bcSKonstantin Belousov (iommu_gaddr_t)IOMMU_PAGE_SIZE << (5 * IOMMU_NPTEPGSHIFT), 22386be9f0dSKonstantin Belousov }; 22486be9f0dSKonstantin Belousov 22586be9f0dSKonstantin Belousov KASSERT(lvl >= 0 && lvl < total_pglvl, 22686be9f0dSKonstantin Belousov ("total %d lvl %d", total_pglvl, lvl)); 22786be9f0dSKonstantin Belousov rlvl = total_pglvl - lvl - 1; 2281abfd355SKonstantin Belousov KASSERT(rlvl < nitems(pg_sz), ("sizeof pg_sz lvl %d", lvl)); 22986be9f0dSKonstantin Belousov return (pg_sz[rlvl]); 23086be9f0dSKonstantin Belousov } 23186be9f0dSKonstantin Belousov 23259e37c8aSRuslan Bukin iommu_gaddr_t 2331abfd355SKonstantin Belousov domain_page_size(struct dmar_domain *domain, int lvl) 23486be9f0dSKonstantin Belousov { 23586be9f0dSKonstantin Belousov 2361abfd355SKonstantin Belousov return (pglvl_page_size(domain->pglvl, lvl)); 23786be9f0dSKonstantin Belousov } 23886be9f0dSKonstantin Belousov 23968eeb96aSKonstantin Belousov int 24059e37c8aSRuslan Bukin calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size, 24159e37c8aSRuslan Bukin iommu_gaddr_t *isizep) 24268eeb96aSKonstantin Belousov { 24359e37c8aSRuslan Bukin iommu_gaddr_t isize; 24468eeb96aSKonstantin Belousov int am; 24568eeb96aSKonstantin Belousov 24668eeb96aSKonstantin Belousov for (am = DMAR_CAP_MAMV(unit->hw_cap);; am--) { 24740d951bcSKonstantin Belousov isize = 1ULL << (am + IOMMU_PAGE_SHIFT); 24868eeb96aSKonstantin Belousov if ((base & (isize - 1)) == 0 && size >= isize) 24968eeb96aSKonstantin Belousov break; 25068eeb96aSKonstantin Belousov if (am == 0) 25168eeb96aSKonstantin Belousov break; 25268eeb96aSKonstantin Belousov } 25368eeb96aSKonstantin Belousov *isizep = isize; 25468eeb96aSKonstantin Belousov return (am); 25568eeb96aSKonstantin Belousov } 25668eeb96aSKonstantin Belousov 25786be9f0dSKonstantin Belousov int haw; 25886be9f0dSKonstantin Belousov int dmar_tbl_pagecnt; 25986be9f0dSKonstantin Belousov 2606b7c46afSKonstantin Belousov static void 2616b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz) 2626b7c46afSKonstantin Belousov { 2636b7c46afSKonstantin Belousov 2646b7c46afSKonstantin Belousov if (DMAR_IS_COHERENT(unit)) 2656b7c46afSKonstantin Belousov return; 26686be9f0dSKonstantin Belousov /* 26786be9f0dSKonstantin Belousov * If DMAR does not snoop paging structures accesses, flush 26886be9f0dSKonstantin Belousov * CPU cache to memory. 26986be9f0dSKonstantin Belousov */ 270d12c4465SKonstantin Belousov pmap_force_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz); 2716b7c46afSKonstantin Belousov } 2726b7c46afSKonstantin Belousov 2736b7c46afSKonstantin Belousov void 27440d951bcSKonstantin Belousov dmar_flush_pte_to_ram(struct dmar_unit *unit, iommu_pte_t *dst) 2756b7c46afSKonstantin Belousov { 2766b7c46afSKonstantin Belousov 2776b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 2786b7c46afSKonstantin Belousov } 2796b7c46afSKonstantin Belousov 2806b7c46afSKonstantin Belousov void 2816b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst) 2826b7c46afSKonstantin Belousov { 2836b7c46afSKonstantin Belousov 2846b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 2856b7c46afSKonstantin Belousov } 2866b7c46afSKonstantin Belousov 2876b7c46afSKonstantin Belousov void 2886b7c46afSKonstantin Belousov dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst) 2896b7c46afSKonstantin Belousov { 2906b7c46afSKonstantin Belousov 2916b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 29286be9f0dSKonstantin Belousov } 29386be9f0dSKonstantin Belousov 29486be9f0dSKonstantin Belousov /* 29586be9f0dSKonstantin Belousov * Load the root entry pointer into the hardware, busily waiting for 29686be9f0dSKonstantin Belousov * the completion. 29786be9f0dSKonstantin Belousov */ 29886be9f0dSKonstantin Belousov int 29986be9f0dSKonstantin Belousov dmar_load_root_entry_ptr(struct dmar_unit *unit) 30086be9f0dSKonstantin Belousov { 30186be9f0dSKonstantin Belousov vm_page_t root_entry; 302476358b3SKonstantin Belousov int error; 30386be9f0dSKonstantin Belousov 30486be9f0dSKonstantin Belousov /* 30586be9f0dSKonstantin Belousov * Access to the GCMD register must be serialized while the 30686be9f0dSKonstantin Belousov * command is submitted. 30786be9f0dSKonstantin Belousov */ 30886be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 30986be9f0dSKonstantin Belousov 3105a49ae8eSKonstantin Belousov VM_OBJECT_RLOCK(unit->ctx_obj); 31186be9f0dSKonstantin Belousov root_entry = vm_page_lookup(unit->ctx_obj, 0); 3125a49ae8eSKonstantin Belousov VM_OBJECT_RUNLOCK(unit->ctx_obj); 31386be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry)); 31486be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP); 315476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS) 316476358b3SKonstantin Belousov != 0)); 317476358b3SKonstantin Belousov return (error); 31886be9f0dSKonstantin Belousov } 31986be9f0dSKonstantin Belousov 32086be9f0dSKonstantin Belousov /* 32186be9f0dSKonstantin Belousov * Globally invalidate the context entries cache, busily waiting for 32286be9f0dSKonstantin Belousov * the completion. 32386be9f0dSKonstantin Belousov */ 32486be9f0dSKonstantin Belousov int 32586be9f0dSKonstantin Belousov dmar_inv_ctx_glob(struct dmar_unit *unit) 32686be9f0dSKonstantin Belousov { 327476358b3SKonstantin Belousov int error; 32886be9f0dSKonstantin Belousov 32986be9f0dSKonstantin Belousov /* 33086be9f0dSKonstantin Belousov * Access to the CCMD register must be serialized while the 33186be9f0dSKonstantin Belousov * command is submitted. 33286be9f0dSKonstantin Belousov */ 33386be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 33468eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 33586be9f0dSKonstantin Belousov 33686be9f0dSKonstantin Belousov /* 33786be9f0dSKonstantin Belousov * The DMAR_CCMD_ICC bit in the upper dword should be written 33886be9f0dSKonstantin Belousov * after the low dword write is completed. Amd64 33986be9f0dSKonstantin Belousov * dmar_write8() does not have this issue, i386 dmar_write8() 34086be9f0dSKonstantin Belousov * writes the upper dword last. 34186be9f0dSKonstantin Belousov */ 34286be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB); 343476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32) 344476358b3SKonstantin Belousov == 0)); 345476358b3SKonstantin Belousov return (error); 34686be9f0dSKonstantin Belousov } 34786be9f0dSKonstantin Belousov 34886be9f0dSKonstantin Belousov /* 34986be9f0dSKonstantin Belousov * Globally invalidate the IOTLB, busily waiting for the completion. 35086be9f0dSKonstantin Belousov */ 35186be9f0dSKonstantin Belousov int 35286be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(struct dmar_unit *unit) 35386be9f0dSKonstantin Belousov { 354476358b3SKonstantin Belousov int error, reg; 35586be9f0dSKonstantin Belousov 35686be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 35768eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 35886be9f0dSKonstantin Belousov 35986be9f0dSKonstantin Belousov reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap); 36086be9f0dSKonstantin Belousov /* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */ 36186be9f0dSKonstantin Belousov dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT | 36286be9f0dSKonstantin Belousov DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW); 363476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) & 364476358b3SKonstantin Belousov DMAR_IOTLB_IVT32) == 0)); 365476358b3SKonstantin Belousov return (error); 36686be9f0dSKonstantin Belousov } 36786be9f0dSKonstantin Belousov 36886be9f0dSKonstantin Belousov /* 36986be9f0dSKonstantin Belousov * Flush the chipset write buffers. See 11.1 "Write Buffer Flushing" 37086be9f0dSKonstantin Belousov * in the architecture specification. 37186be9f0dSKonstantin Belousov */ 37286be9f0dSKonstantin Belousov int 37386be9f0dSKonstantin Belousov dmar_flush_write_bufs(struct dmar_unit *unit) 37486be9f0dSKonstantin Belousov { 375476358b3SKonstantin Belousov int error; 37686be9f0dSKonstantin Belousov 37786be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 37886be9f0dSKonstantin Belousov 37986be9f0dSKonstantin Belousov /* 38086be9f0dSKonstantin Belousov * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported. 38186be9f0dSKonstantin Belousov */ 38286be9f0dSKonstantin Belousov KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0, 38359e37c8aSRuslan Bukin ("dmar%d: no RWBF", unit->iommu.unit)); 38486be9f0dSKonstantin Belousov 38586be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF); 386476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS) 387476358b3SKonstantin Belousov != 0)); 388476358b3SKonstantin Belousov return (error); 38986be9f0dSKonstantin Belousov } 39086be9f0dSKonstantin Belousov 39106e6ca6dSKornel Duleba /* 39206e6ca6dSKornel Duleba * Some BIOSes protect memory region they reside in by using DMAR to 39306e6ca6dSKornel Duleba * prevent devices from doing any DMA transactions to that part of RAM. 39406e6ca6dSKornel Duleba * AMI refers to this as "DMA Control Guarantee". 39506e6ca6dSKornel Duleba * We need to disable this when address translation is enabled. 39606e6ca6dSKornel Duleba */ 39706e6ca6dSKornel Duleba int 39806e6ca6dSKornel Duleba dmar_disable_protected_regions(struct dmar_unit *unit) 39906e6ca6dSKornel Duleba { 40006e6ca6dSKornel Duleba uint32_t reg; 40106e6ca6dSKornel Duleba int error; 40206e6ca6dSKornel Duleba 40306e6ca6dSKornel Duleba DMAR_ASSERT_LOCKED(unit); 40406e6ca6dSKornel Duleba 40506e6ca6dSKornel Duleba /* Check if we support the feature. */ 40606e6ca6dSKornel Duleba if ((unit->hw_cap & (DMAR_CAP_PLMR | DMAR_CAP_PHMR)) == 0) 40706e6ca6dSKornel Duleba return (0); 40806e6ca6dSKornel Duleba 40906e6ca6dSKornel Duleba reg = dmar_read4(unit, DMAR_PMEN_REG); 41006f659c3SKornel Duleba if ((reg & DMAR_PMEN_EPM) == 0) 41106f659c3SKornel Duleba return (0); 41206f659c3SKornel Duleba 41306e6ca6dSKornel Duleba reg &= ~DMAR_PMEN_EPM; 41406e6ca6dSKornel Duleba dmar_write4(unit, DMAR_PMEN_REG, reg); 41506e6ca6dSKornel Duleba DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_PMEN_REG) & DMAR_PMEN_PRS) 41606e6ca6dSKornel Duleba != 0)); 41706e6ca6dSKornel Duleba 41806e6ca6dSKornel Duleba return (error); 41906e6ca6dSKornel Duleba } 42006e6ca6dSKornel Duleba 42186be9f0dSKonstantin Belousov int 42286be9f0dSKonstantin Belousov dmar_enable_translation(struct dmar_unit *unit) 42386be9f0dSKonstantin Belousov { 424476358b3SKonstantin Belousov int error; 42586be9f0dSKonstantin Belousov 42686be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 42786be9f0dSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_TE; 42886be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 429476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) 430476358b3SKonstantin Belousov != 0)); 431476358b3SKonstantin Belousov return (error); 43286be9f0dSKonstantin Belousov } 43386be9f0dSKonstantin Belousov 43486be9f0dSKonstantin Belousov int 43586be9f0dSKonstantin Belousov dmar_disable_translation(struct dmar_unit *unit) 43686be9f0dSKonstantin Belousov { 437476358b3SKonstantin Belousov int error; 43886be9f0dSKonstantin Belousov 43986be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 44086be9f0dSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_TE; 44186be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 442476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) 443476358b3SKonstantin Belousov == 0)); 444476358b3SKonstantin Belousov return (error); 44586be9f0dSKonstantin Belousov } 44686be9f0dSKonstantin Belousov 4470a110d5bSKonstantin Belousov int 4480a110d5bSKonstantin Belousov dmar_load_irt_ptr(struct dmar_unit *unit) 4490a110d5bSKonstantin Belousov { 4500a110d5bSKonstantin Belousov uint64_t irta, s; 451476358b3SKonstantin Belousov int error; 4520a110d5bSKonstantin Belousov 4530a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 4540a110d5bSKonstantin Belousov irta = unit->irt_phys; 4550a110d5bSKonstantin Belousov if (DMAR_X2APIC(unit)) 4560a110d5bSKonstantin Belousov irta |= DMAR_IRTA_EIME; 4570a110d5bSKonstantin Belousov s = fls(unit->irte_cnt) - 2; 4580a110d5bSKonstantin Belousov KASSERT(unit->irte_cnt >= 2 && s <= DMAR_IRTA_S_MASK && 4590a110d5bSKonstantin Belousov powerof2(unit->irte_cnt), 4600a110d5bSKonstantin Belousov ("IRTA_REG_S overflow %x", unit->irte_cnt)); 4610a110d5bSKonstantin Belousov irta |= s; 4620a110d5bSKonstantin Belousov dmar_write8(unit, DMAR_IRTA_REG, irta); 4630a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SIRTP); 464476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRTPS) 465476358b3SKonstantin Belousov != 0)); 466476358b3SKonstantin Belousov return (error); 4670a110d5bSKonstantin Belousov } 4680a110d5bSKonstantin Belousov 4690a110d5bSKonstantin Belousov int 4700a110d5bSKonstantin Belousov dmar_enable_ir(struct dmar_unit *unit) 4710a110d5bSKonstantin Belousov { 472476358b3SKonstantin Belousov int error; 4730a110d5bSKonstantin Belousov 4740a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 4750a110d5bSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_IRE; 4760a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_CFI; 4770a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 478476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES) 479476358b3SKonstantin Belousov != 0)); 480476358b3SKonstantin Belousov return (error); 4810a110d5bSKonstantin Belousov } 4820a110d5bSKonstantin Belousov 4830a110d5bSKonstantin Belousov int 4840a110d5bSKonstantin Belousov dmar_disable_ir(struct dmar_unit *unit) 4850a110d5bSKonstantin Belousov { 486476358b3SKonstantin Belousov int error; 4870a110d5bSKonstantin Belousov 4880a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 4890a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_IRE; 4900a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 491476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES) 492476358b3SKonstantin Belousov == 0)); 493476358b3SKonstantin Belousov return (error); 4940a110d5bSKonstantin Belousov } 4950a110d5bSKonstantin Belousov 49686be9f0dSKonstantin Belousov #define BARRIER_F \ 49786be9f0dSKonstantin Belousov u_int f_done, f_inproc, f_wakeup; \ 49886be9f0dSKonstantin Belousov \ 49986be9f0dSKonstantin Belousov f_done = 1 << (barrier_id * 3); \ 50086be9f0dSKonstantin Belousov f_inproc = 1 << (barrier_id * 3 + 1); \ 50186be9f0dSKonstantin Belousov f_wakeup = 1 << (barrier_id * 3 + 2) 50286be9f0dSKonstantin Belousov 50386be9f0dSKonstantin Belousov bool 50486be9f0dSKonstantin Belousov dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id) 50586be9f0dSKonstantin Belousov { 50686be9f0dSKonstantin Belousov BARRIER_F; 50786be9f0dSKonstantin Belousov 50886be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 50986be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_done) != 0) { 51086be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 51186be9f0dSKonstantin Belousov return (false); 51286be9f0dSKonstantin Belousov } 51386be9f0dSKonstantin Belousov 51486be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_inproc) != 0) { 51586be9f0dSKonstantin Belousov while ((dmar->barrier_flags & f_inproc) != 0) { 51686be9f0dSKonstantin Belousov dmar->barrier_flags |= f_wakeup; 51759e37c8aSRuslan Bukin msleep(&dmar->barrier_flags, &dmar->iommu.lock, 0, 51886be9f0dSKonstantin Belousov "dmarb", 0); 51986be9f0dSKonstantin Belousov } 52086be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & f_done) != 0, 52159e37c8aSRuslan Bukin ("dmar%d barrier %d missing done", dmar->iommu.unit, 52259e37c8aSRuslan Bukin barrier_id)); 52386be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 52486be9f0dSKonstantin Belousov return (false); 52586be9f0dSKonstantin Belousov } 52686be9f0dSKonstantin Belousov 52786be9f0dSKonstantin Belousov dmar->barrier_flags |= f_inproc; 52886be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 52986be9f0dSKonstantin Belousov return (true); 53086be9f0dSKonstantin Belousov } 53186be9f0dSKonstantin Belousov 53286be9f0dSKonstantin Belousov void 53386be9f0dSKonstantin Belousov dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id) 53486be9f0dSKonstantin Belousov { 53586be9f0dSKonstantin Belousov BARRIER_F; 53686be9f0dSKonstantin Belousov 53786be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar); 53886be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc, 53959e37c8aSRuslan Bukin ("dmar%d barrier %d missed entry", dmar->iommu.unit, barrier_id)); 54086be9f0dSKonstantin Belousov dmar->barrier_flags |= f_done; 54186be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_wakeup) != 0) 54286be9f0dSKonstantin Belousov wakeup(&dmar->barrier_flags); 54386be9f0dSKonstantin Belousov dmar->barrier_flags &= ~(f_inproc | f_wakeup); 54486be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 54586be9f0dSKonstantin Belousov } 54686be9f0dSKonstantin Belousov 547e164cafcSKonstantin Belousov int dmar_batch_coalesce = 100; 548476358b3SKonstantin Belousov struct timespec dmar_hw_timeout = { 549476358b3SKonstantin Belousov .tv_sec = 0, 550476358b3SKonstantin Belousov .tv_nsec = 1000000 551476358b3SKonstantin Belousov }; 552476358b3SKonstantin Belousov 553476358b3SKonstantin Belousov static const uint64_t d = 1000000000; 554476358b3SKonstantin Belousov 555476358b3SKonstantin Belousov void 556476358b3SKonstantin Belousov dmar_update_timeout(uint64_t newval) 557476358b3SKonstantin Belousov { 558476358b3SKonstantin Belousov 559476358b3SKonstantin Belousov /* XXXKIB not atomic */ 560476358b3SKonstantin Belousov dmar_hw_timeout.tv_sec = newval / d; 561476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec = newval % d; 562476358b3SKonstantin Belousov } 563476358b3SKonstantin Belousov 564476358b3SKonstantin Belousov uint64_t 565476358b3SKonstantin Belousov dmar_get_timeout(void) 566476358b3SKonstantin Belousov { 567476358b3SKonstantin Belousov 568476358b3SKonstantin Belousov return ((uint64_t)dmar_hw_timeout.tv_sec * d + 569476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec); 570476358b3SKonstantin Belousov } 571476358b3SKonstantin Belousov 572476358b3SKonstantin Belousov static int 573476358b3SKonstantin Belousov dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS) 574476358b3SKonstantin Belousov { 575476358b3SKonstantin Belousov uint64_t val; 576476358b3SKonstantin Belousov int error; 577476358b3SKonstantin Belousov 578476358b3SKonstantin Belousov val = dmar_get_timeout(); 579476358b3SKonstantin Belousov error = sysctl_handle_long(oidp, &val, 0, req); 580476358b3SKonstantin Belousov if (error != 0 || req->newptr == NULL) 581476358b3SKonstantin Belousov return (error); 582476358b3SKonstantin Belousov dmar_update_timeout(val); 583476358b3SKonstantin Belousov return (error); 584476358b3SKonstantin Belousov } 58586be9f0dSKonstantin Belousov 586357149f0SRuslan Bukin SYSCTL_INT(_hw_iommu_dmar, OID_AUTO, batch_coalesce, CTLFLAG_RWTUN, 587e164cafcSKonstantin Belousov &dmar_batch_coalesce, 0, 588e164cafcSKonstantin Belousov "Number of qi batches between interrupt"); 589357149f0SRuslan Bukin SYSCTL_PROC(_hw_iommu_dmar, OID_AUTO, timeout, 590476358b3SKonstantin Belousov CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0, 591476358b3SKonstantin Belousov dmar_timeout_sysctl, "QU", 592476358b3SKonstantin Belousov "Timeout for command wait, in nanoseconds"); 593