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/bus.h> 3586be9f0dSKonstantin Belousov #include <sys/kernel.h> 3686be9f0dSKonstantin Belousov #include <sys/lock.h> 3786be9f0dSKonstantin Belousov #include <sys/malloc.h> 3886be9f0dSKonstantin Belousov #include <sys/memdesc.h> 3986be9f0dSKonstantin Belousov #include <sys/mutex.h> 4086be9f0dSKonstantin Belousov #include <sys/proc.h> 4186be9f0dSKonstantin Belousov #include <sys/queue.h> 4286be9f0dSKonstantin Belousov #include <sys/rman.h> 4386be9f0dSKonstantin Belousov #include <sys/rwlock.h> 4486be9f0dSKonstantin Belousov #include <sys/sched.h> 4586be9f0dSKonstantin Belousov #include <sys/sf_buf.h> 4686be9f0dSKonstantin Belousov #include <sys/sysctl.h> 4786be9f0dSKonstantin Belousov #include <sys/systm.h> 4886be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 4986be9f0dSKonstantin Belousov #include <sys/tree.h> 5067499354SRyan Stone #include <dev/pci/pcivar.h> 5186be9f0dSKonstantin Belousov #include <vm/vm.h> 5286be9f0dSKonstantin Belousov #include <vm/vm_extern.h> 5386be9f0dSKonstantin Belousov #include <vm/vm_kern.h> 5486be9f0dSKonstantin Belousov #include <vm/vm_object.h> 5586be9f0dSKonstantin Belousov #include <vm/vm_page.h> 5686be9f0dSKonstantin Belousov #include <vm/vm_map.h> 5786be9f0dSKonstantin Belousov #include <vm/vm_pageout.h> 5886be9f0dSKonstantin Belousov #include <machine/bus.h> 5986be9f0dSKonstantin Belousov #include <machine/cpu.h> 6086be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h> 6186be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h> 6286be9f0dSKonstantin Belousov #include <x86/iommu/busdma_dmar.h> 6386be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 6486be9f0dSKonstantin Belousov 6586be9f0dSKonstantin Belousov u_int 6686be9f0dSKonstantin Belousov dmar_nd2mask(u_int nd) 6786be9f0dSKonstantin Belousov { 6886be9f0dSKonstantin Belousov static const u_int masks[] = { 6986be9f0dSKonstantin Belousov 0x000f, /* nd == 0 */ 7086be9f0dSKonstantin Belousov 0x002f, /* nd == 1 */ 7186be9f0dSKonstantin Belousov 0x00ff, /* nd == 2 */ 7286be9f0dSKonstantin Belousov 0x02ff, /* nd == 3 */ 7386be9f0dSKonstantin Belousov 0x0fff, /* nd == 4 */ 7486be9f0dSKonstantin Belousov 0x2fff, /* nd == 5 */ 7586be9f0dSKonstantin Belousov 0xffff, /* nd == 6 */ 7686be9f0dSKonstantin Belousov 0x0000, /* nd == 7 reserved */ 7786be9f0dSKonstantin Belousov }; 7886be9f0dSKonstantin Belousov 7986be9f0dSKonstantin Belousov KASSERT(nd <= 6, ("number of domains %d", nd)); 8086be9f0dSKonstantin Belousov return (masks[nd]); 8186be9f0dSKonstantin Belousov } 8286be9f0dSKonstantin Belousov 8386be9f0dSKonstantin Belousov static const struct sagaw_bits_tag { 8486be9f0dSKonstantin Belousov int agaw; 8586be9f0dSKonstantin Belousov int cap; 8686be9f0dSKonstantin Belousov int awlvl; 8786be9f0dSKonstantin Belousov int pglvl; 8886be9f0dSKonstantin Belousov } sagaw_bits[] = { 8986be9f0dSKonstantin Belousov {.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL, 9086be9f0dSKonstantin Belousov .pglvl = 2}, 9186be9f0dSKonstantin Belousov {.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL, 9286be9f0dSKonstantin Belousov .pglvl = 3}, 9386be9f0dSKonstantin Belousov {.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL, 9486be9f0dSKonstantin Belousov .pglvl = 4}, 9586be9f0dSKonstantin Belousov {.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL, 9686be9f0dSKonstantin Belousov .pglvl = 5}, 9786be9f0dSKonstantin Belousov {.agaw = 64, .cap = DMAR_CAP_SAGAW_6LVL, .awlvl = DMAR_CTX2_AW_6LVL, 9886be9f0dSKonstantin Belousov .pglvl = 6} 9986be9f0dSKonstantin Belousov }; 10086be9f0dSKonstantin Belousov #define SIZEOF_SAGAW_BITS (sizeof(sagaw_bits) / sizeof(sagaw_bits[0])) 10186be9f0dSKonstantin Belousov 10286be9f0dSKonstantin Belousov bool 10386be9f0dSKonstantin Belousov dmar_pglvl_supported(struct dmar_unit *unit, int pglvl) 10486be9f0dSKonstantin Belousov { 10586be9f0dSKonstantin Belousov int i; 10686be9f0dSKonstantin Belousov 10786be9f0dSKonstantin Belousov for (i = 0; i < SIZEOF_SAGAW_BITS; i++) { 10886be9f0dSKonstantin Belousov if (sagaw_bits[i].pglvl != pglvl) 10986be9f0dSKonstantin Belousov continue; 11086be9f0dSKonstantin Belousov if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 11186be9f0dSKonstantin Belousov return (true); 11286be9f0dSKonstantin Belousov } 11386be9f0dSKonstantin Belousov return (false); 11486be9f0dSKonstantin Belousov } 11586be9f0dSKonstantin Belousov 11686be9f0dSKonstantin Belousov int 11786be9f0dSKonstantin Belousov ctx_set_agaw(struct dmar_ctx *ctx, int mgaw) 11886be9f0dSKonstantin Belousov { 11986be9f0dSKonstantin Belousov int sagaw, i; 12086be9f0dSKonstantin Belousov 12186be9f0dSKonstantin Belousov ctx->mgaw = mgaw; 12286be9f0dSKonstantin Belousov sagaw = DMAR_CAP_SAGAW(ctx->dmar->hw_cap); 12386be9f0dSKonstantin Belousov for (i = 0; i < SIZEOF_SAGAW_BITS; i++) { 12486be9f0dSKonstantin Belousov if (sagaw_bits[i].agaw >= mgaw) { 12586be9f0dSKonstantin Belousov ctx->agaw = sagaw_bits[i].agaw; 12686be9f0dSKonstantin Belousov ctx->pglvl = sagaw_bits[i].pglvl; 12786be9f0dSKonstantin Belousov ctx->awlvl = sagaw_bits[i].awlvl; 12886be9f0dSKonstantin Belousov return (0); 12986be9f0dSKonstantin Belousov } 13086be9f0dSKonstantin Belousov } 13186be9f0dSKonstantin Belousov device_printf(ctx->dmar->dev, 13286be9f0dSKonstantin Belousov "context request mgaw %d for pci%d:%d:%d:%d, " 13367499354SRyan Stone "no agaw found, sagaw %x\n", mgaw, ctx->dmar->segment, 13467499354SRyan Stone pci_get_bus(ctx->ctx_tag.owner), 13567499354SRyan Stone pci_get_slot(ctx->ctx_tag.owner), 13667499354SRyan Stone pci_get_function(ctx->ctx_tag.owner), sagaw); 13786be9f0dSKonstantin Belousov return (EINVAL); 13886be9f0dSKonstantin Belousov } 13986be9f0dSKonstantin Belousov 14086be9f0dSKonstantin Belousov /* 14186be9f0dSKonstantin Belousov * Find a best fit mgaw for the given maxaddr: 14286be9f0dSKonstantin Belousov * - if allow_less is false, must find sagaw which maps all requested 14386be9f0dSKonstantin Belousov * addresses (used by identity mappings); 14486be9f0dSKonstantin Belousov * - if allow_less is true, and no supported sagaw can map all requested 14586be9f0dSKonstantin Belousov * address space, accept the biggest sagaw, whatever is it. 14686be9f0dSKonstantin Belousov */ 14786be9f0dSKonstantin Belousov int 14886be9f0dSKonstantin Belousov dmar_maxaddr2mgaw(struct dmar_unit *unit, dmar_gaddr_t maxaddr, bool allow_less) 14986be9f0dSKonstantin Belousov { 15086be9f0dSKonstantin Belousov int i; 15186be9f0dSKonstantin Belousov 15286be9f0dSKonstantin Belousov for (i = 0; i < SIZEOF_SAGAW_BITS; i++) { 15386be9f0dSKonstantin Belousov if ((1ULL << sagaw_bits[i].agaw) >= maxaddr && 15486be9f0dSKonstantin Belousov (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0) 15586be9f0dSKonstantin Belousov break; 15686be9f0dSKonstantin Belousov } 15786be9f0dSKonstantin Belousov if (allow_less && i == SIZEOF_SAGAW_BITS) { 15886be9f0dSKonstantin Belousov do { 15986be9f0dSKonstantin Belousov i--; 16086be9f0dSKonstantin Belousov } while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) 16186be9f0dSKonstantin Belousov == 0); 16286be9f0dSKonstantin Belousov } 16386be9f0dSKonstantin Belousov if (i < SIZEOF_SAGAW_BITS) 16486be9f0dSKonstantin Belousov return (sagaw_bits[i].agaw); 16586be9f0dSKonstantin Belousov KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d", 16686be9f0dSKonstantin Belousov (uintmax_t) maxaddr, allow_less)); 16786be9f0dSKonstantin Belousov return (-1); 16886be9f0dSKonstantin Belousov } 16986be9f0dSKonstantin Belousov 17086be9f0dSKonstantin Belousov /* 17186be9f0dSKonstantin Belousov * Calculate the total amount of page table pages needed to map the 17286be9f0dSKonstantin Belousov * whole bus address space on the context with the selected agaw. 17386be9f0dSKonstantin Belousov */ 17486be9f0dSKonstantin Belousov vm_pindex_t 17586be9f0dSKonstantin Belousov pglvl_max_pages(int pglvl) 17686be9f0dSKonstantin Belousov { 17786be9f0dSKonstantin Belousov vm_pindex_t res; 17886be9f0dSKonstantin Belousov int i; 17986be9f0dSKonstantin Belousov 18086be9f0dSKonstantin Belousov for (res = 0, i = pglvl; i > 0; i--) { 18186be9f0dSKonstantin Belousov res *= DMAR_NPTEPG; 18286be9f0dSKonstantin Belousov res++; 18386be9f0dSKonstantin Belousov } 18486be9f0dSKonstantin Belousov return (res); 18586be9f0dSKonstantin Belousov } 18686be9f0dSKonstantin Belousov 18786be9f0dSKonstantin Belousov /* 18886be9f0dSKonstantin Belousov * Return true if the page table level lvl supports the superpage for 18986be9f0dSKonstantin Belousov * the context ctx. 19086be9f0dSKonstantin Belousov */ 19186be9f0dSKonstantin Belousov int 19286be9f0dSKonstantin Belousov ctx_is_sp_lvl(struct dmar_ctx *ctx, int lvl) 19386be9f0dSKonstantin Belousov { 19486be9f0dSKonstantin Belousov int alvl, cap_sps; 19586be9f0dSKonstantin Belousov static const int sagaw_sp[] = { 19686be9f0dSKonstantin Belousov DMAR_CAP_SPS_2M, 19786be9f0dSKonstantin Belousov DMAR_CAP_SPS_1G, 19886be9f0dSKonstantin Belousov DMAR_CAP_SPS_512G, 19986be9f0dSKonstantin Belousov DMAR_CAP_SPS_1T 20086be9f0dSKonstantin Belousov }; 20186be9f0dSKonstantin Belousov 20286be9f0dSKonstantin Belousov alvl = ctx->pglvl - lvl - 1; 20386be9f0dSKonstantin Belousov cap_sps = DMAR_CAP_SPS(ctx->dmar->hw_cap); 20486be9f0dSKonstantin Belousov return (alvl < sizeof(sagaw_sp) / sizeof(sagaw_sp[0]) && 20586be9f0dSKonstantin Belousov (sagaw_sp[alvl] & cap_sps) != 0); 20686be9f0dSKonstantin Belousov } 20786be9f0dSKonstantin Belousov 20886be9f0dSKonstantin Belousov dmar_gaddr_t 20986be9f0dSKonstantin Belousov pglvl_page_size(int total_pglvl, int lvl) 21086be9f0dSKonstantin Belousov { 21186be9f0dSKonstantin Belousov int rlvl; 21286be9f0dSKonstantin Belousov static const dmar_gaddr_t pg_sz[] = { 21386be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE, 21486be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE << DMAR_NPTEPGSHIFT, 21586be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE << (2 * DMAR_NPTEPGSHIFT), 21686be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE << (3 * DMAR_NPTEPGSHIFT), 21786be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE << (4 * DMAR_NPTEPGSHIFT), 21886be9f0dSKonstantin Belousov (dmar_gaddr_t)DMAR_PAGE_SIZE << (5 * DMAR_NPTEPGSHIFT) 21986be9f0dSKonstantin Belousov }; 22086be9f0dSKonstantin Belousov 22186be9f0dSKonstantin Belousov KASSERT(lvl >= 0 && lvl < total_pglvl, 22286be9f0dSKonstantin Belousov ("total %d lvl %d", total_pglvl, lvl)); 22386be9f0dSKonstantin Belousov rlvl = total_pglvl - lvl - 1; 22486be9f0dSKonstantin Belousov KASSERT(rlvl < sizeof(pg_sz) / sizeof(pg_sz[0]), 22586be9f0dSKonstantin Belousov ("sizeof pg_sz lvl %d", lvl)); 22686be9f0dSKonstantin Belousov return (pg_sz[rlvl]); 22786be9f0dSKonstantin Belousov } 22886be9f0dSKonstantin Belousov 22986be9f0dSKonstantin Belousov dmar_gaddr_t 23086be9f0dSKonstantin Belousov ctx_page_size(struct dmar_ctx *ctx, int lvl) 23186be9f0dSKonstantin Belousov { 23286be9f0dSKonstantin Belousov 23386be9f0dSKonstantin Belousov return (pglvl_page_size(ctx->pglvl, lvl)); 23486be9f0dSKonstantin Belousov } 23586be9f0dSKonstantin Belousov 23668eeb96aSKonstantin Belousov int 23768eeb96aSKonstantin Belousov calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size, 23868eeb96aSKonstantin Belousov dmar_gaddr_t *isizep) 23968eeb96aSKonstantin Belousov { 24068eeb96aSKonstantin Belousov dmar_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 25486be9f0dSKonstantin Belousov dmar_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; 26286be9f0dSKonstantin Belousov int zeroed; 26386be9f0dSKonstantin Belousov 26486be9f0dSKonstantin Belousov zeroed = (flags & DMAR_PGF_ZERO) != 0 ? VM_ALLOC_ZERO : 0; 26586be9f0dSKonstantin Belousov for (;;) { 26686be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 26786be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 26886be9f0dSKonstantin Belousov m = vm_page_lookup(obj, idx); 26986be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_NOALLOC) != 0 || m != NULL) { 27086be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 27186be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 27286be9f0dSKonstantin Belousov break; 27386be9f0dSKonstantin Belousov } 27486be9f0dSKonstantin Belousov m = vm_page_alloc_contig(obj, idx, VM_ALLOC_NOBUSY | 27586be9f0dSKonstantin Belousov VM_ALLOC_SYSTEM | VM_ALLOC_NODUMP | zeroed, 1, 0, 27686be9f0dSKonstantin Belousov dmar_high, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 27786be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 27886be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 27986be9f0dSKonstantin Belousov if (m != NULL) { 28086be9f0dSKonstantin Belousov if (zeroed && (m->flags & PG_ZERO) == 0) 28186be9f0dSKonstantin Belousov pmap_zero_page(m); 28286be9f0dSKonstantin Belousov atomic_add_int(&dmar_tbl_pagecnt, 1); 28386be9f0dSKonstantin Belousov break; 28486be9f0dSKonstantin Belousov } 28586be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_WAITOK) == 0) 28686be9f0dSKonstantin Belousov break; 28786be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) != 0) 28886be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 28986be9f0dSKonstantin Belousov VM_WAIT; 29086be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) != 0) 29186be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 29286be9f0dSKonstantin Belousov } 29386be9f0dSKonstantin Belousov return (m); 29486be9f0dSKonstantin Belousov } 29586be9f0dSKonstantin Belousov 29686be9f0dSKonstantin Belousov void 29786be9f0dSKonstantin Belousov dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags) 29886be9f0dSKonstantin Belousov { 29986be9f0dSKonstantin Belousov vm_page_t m; 30086be9f0dSKonstantin Belousov 30186be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 30286be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 30386be9f0dSKonstantin Belousov m = vm_page_lookup(obj, idx); 30486be9f0dSKonstantin Belousov if (m != NULL) { 30586be9f0dSKonstantin Belousov vm_page_free(m); 30686be9f0dSKonstantin Belousov atomic_subtract_int(&dmar_tbl_pagecnt, 1); 30786be9f0dSKonstantin Belousov } 30886be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 30986be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 31086be9f0dSKonstantin Belousov } 31186be9f0dSKonstantin Belousov 31286be9f0dSKonstantin Belousov void * 31386be9f0dSKonstantin Belousov dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags, 31486be9f0dSKonstantin Belousov struct sf_buf **sf) 31586be9f0dSKonstantin Belousov { 31686be9f0dSKonstantin Belousov vm_page_t m; 31786be9f0dSKonstantin Belousov bool allocated; 31886be9f0dSKonstantin Belousov 31986be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 32086be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 32186be9f0dSKonstantin Belousov m = vm_page_lookup(obj, idx); 32286be9f0dSKonstantin Belousov if (m == NULL && (flags & DMAR_PGF_ALLOC) != 0) { 32386be9f0dSKonstantin Belousov m = dmar_pgalloc(obj, idx, flags | DMAR_PGF_OBJL); 32486be9f0dSKonstantin Belousov allocated = true; 32586be9f0dSKonstantin Belousov } else 32686be9f0dSKonstantin Belousov allocated = false; 32786be9f0dSKonstantin Belousov if (m == NULL) { 32886be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 32986be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 33086be9f0dSKonstantin Belousov return (NULL); 33186be9f0dSKonstantin Belousov } 33286be9f0dSKonstantin Belousov /* Sleepable allocations cannot fail. */ 33386be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_WAITOK) != 0) 33486be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 33586be9f0dSKonstantin Belousov sched_pin(); 33686be9f0dSKonstantin Belousov *sf = sf_buf_alloc(m, SFB_CPUPRIVATE | ((flags & DMAR_PGF_WAITOK) 33786be9f0dSKonstantin Belousov == 0 ? SFB_NOWAIT : 0)); 33886be9f0dSKonstantin Belousov if (*sf == NULL) { 33986be9f0dSKonstantin Belousov sched_unpin(); 34086be9f0dSKonstantin Belousov if (allocated) { 34186be9f0dSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(obj); 34286be9f0dSKonstantin Belousov dmar_pgfree(obj, m->pindex, flags | DMAR_PGF_OBJL); 34386be9f0dSKonstantin Belousov } 34486be9f0dSKonstantin Belousov if ((flags & DMAR_PGF_OBJL) == 0) 34586be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 34686be9f0dSKonstantin Belousov return (NULL); 34786be9f0dSKonstantin Belousov } 34886be9f0dSKonstantin Belousov if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) == 34986be9f0dSKonstantin Belousov (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) 35086be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(obj); 35186be9f0dSKonstantin Belousov else if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) == 0) 35286be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 35386be9f0dSKonstantin Belousov return ((void *)sf_buf_kva(*sf)); 35486be9f0dSKonstantin Belousov } 35586be9f0dSKonstantin Belousov 35686be9f0dSKonstantin Belousov void 357*6b7c46afSKonstantin Belousov dmar_unmap_pgtbl(struct sf_buf *sf) 35886be9f0dSKonstantin Belousov { 35986be9f0dSKonstantin Belousov 36086be9f0dSKonstantin Belousov sf_buf_free(sf); 36186be9f0dSKonstantin Belousov sched_unpin(); 362*6b7c46afSKonstantin Belousov } 36386be9f0dSKonstantin Belousov 364*6b7c46afSKonstantin Belousov static void 365*6b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz) 366*6b7c46afSKonstantin Belousov { 367*6b7c46afSKonstantin Belousov 368*6b7c46afSKonstantin Belousov if (DMAR_IS_COHERENT(unit)) 369*6b7c46afSKonstantin Belousov return; 37086be9f0dSKonstantin Belousov /* 37186be9f0dSKonstantin Belousov * If DMAR does not snoop paging structures accesses, flush 37286be9f0dSKonstantin Belousov * CPU cache to memory. 37386be9f0dSKonstantin Belousov */ 374*6b7c46afSKonstantin Belousov pmap_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz, 375*6b7c46afSKonstantin Belousov TRUE); 376*6b7c46afSKonstantin Belousov } 377*6b7c46afSKonstantin Belousov 378*6b7c46afSKonstantin Belousov void 379*6b7c46afSKonstantin Belousov dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst) 380*6b7c46afSKonstantin Belousov { 381*6b7c46afSKonstantin Belousov 382*6b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 383*6b7c46afSKonstantin Belousov } 384*6b7c46afSKonstantin Belousov 385*6b7c46afSKonstantin Belousov void 386*6b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst) 387*6b7c46afSKonstantin Belousov { 388*6b7c46afSKonstantin Belousov 389*6b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 390*6b7c46afSKonstantin Belousov } 391*6b7c46afSKonstantin Belousov 392*6b7c46afSKonstantin Belousov void 393*6b7c46afSKonstantin Belousov dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst) 394*6b7c46afSKonstantin Belousov { 395*6b7c46afSKonstantin Belousov 396*6b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst)); 39786be9f0dSKonstantin Belousov } 39886be9f0dSKonstantin Belousov 39986be9f0dSKonstantin Belousov /* 40086be9f0dSKonstantin Belousov * Load the root entry pointer into the hardware, busily waiting for 40186be9f0dSKonstantin Belousov * the completion. 40286be9f0dSKonstantin Belousov */ 40386be9f0dSKonstantin Belousov int 40486be9f0dSKonstantin Belousov dmar_load_root_entry_ptr(struct dmar_unit *unit) 40586be9f0dSKonstantin Belousov { 40686be9f0dSKonstantin Belousov vm_page_t root_entry; 40786be9f0dSKonstantin Belousov 40886be9f0dSKonstantin Belousov /* 40986be9f0dSKonstantin Belousov * Access to the GCMD register must be serialized while the 41086be9f0dSKonstantin Belousov * command is submitted. 41186be9f0dSKonstantin Belousov */ 41286be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 41386be9f0dSKonstantin Belousov 41486be9f0dSKonstantin Belousov /* VM_OBJECT_RLOCK(unit->ctx_obj); */ 41586be9f0dSKonstantin Belousov VM_OBJECT_WLOCK(unit->ctx_obj); 41686be9f0dSKonstantin Belousov root_entry = vm_page_lookup(unit->ctx_obj, 0); 41786be9f0dSKonstantin Belousov /* VM_OBJECT_RUNLOCK(unit->ctx_obj); */ 41886be9f0dSKonstantin Belousov VM_OBJECT_WUNLOCK(unit->ctx_obj); 41986be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry)); 42086be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP); 42186be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 42286be9f0dSKonstantin Belousov while ((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS) == 0) 42386be9f0dSKonstantin Belousov cpu_spinwait(); 42486be9f0dSKonstantin Belousov return (0); 42586be9f0dSKonstantin Belousov } 42686be9f0dSKonstantin Belousov 42786be9f0dSKonstantin Belousov /* 42886be9f0dSKonstantin Belousov * Globally invalidate the context entries cache, busily waiting for 42986be9f0dSKonstantin Belousov * the completion. 43086be9f0dSKonstantin Belousov */ 43186be9f0dSKonstantin Belousov int 43286be9f0dSKonstantin Belousov dmar_inv_ctx_glob(struct dmar_unit *unit) 43386be9f0dSKonstantin Belousov { 43486be9f0dSKonstantin Belousov 43586be9f0dSKonstantin Belousov /* 43686be9f0dSKonstantin Belousov * Access to the CCMD register must be serialized while the 43786be9f0dSKonstantin Belousov * command is submitted. 43886be9f0dSKonstantin Belousov */ 43986be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 44068eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 44186be9f0dSKonstantin Belousov 44286be9f0dSKonstantin Belousov /* 44386be9f0dSKonstantin Belousov * The DMAR_CCMD_ICC bit in the upper dword should be written 44486be9f0dSKonstantin Belousov * after the low dword write is completed. Amd64 44586be9f0dSKonstantin Belousov * dmar_write8() does not have this issue, i386 dmar_write8() 44686be9f0dSKonstantin Belousov * writes the upper dword last. 44786be9f0dSKonstantin Belousov */ 44886be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB); 44986be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 45086be9f0dSKonstantin Belousov while ((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32) != 0) 45186be9f0dSKonstantin Belousov cpu_spinwait(); 45286be9f0dSKonstantin Belousov return (0); 45386be9f0dSKonstantin Belousov } 45486be9f0dSKonstantin Belousov 45586be9f0dSKonstantin Belousov /* 45686be9f0dSKonstantin Belousov * Globally invalidate the IOTLB, busily waiting for the completion. 45786be9f0dSKonstantin Belousov */ 45886be9f0dSKonstantin Belousov int 45986be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(struct dmar_unit *unit) 46086be9f0dSKonstantin Belousov { 46186be9f0dSKonstantin Belousov int reg; 46286be9f0dSKonstantin Belousov 46386be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 46468eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled")); 46586be9f0dSKonstantin Belousov 46686be9f0dSKonstantin Belousov reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap); 46786be9f0dSKonstantin Belousov /* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */ 46886be9f0dSKonstantin Belousov dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT | 46986be9f0dSKonstantin Belousov DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW); 47086be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 47186be9f0dSKonstantin Belousov while ((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) & 47286be9f0dSKonstantin Belousov DMAR_IOTLB_IVT32) != 0) 47386be9f0dSKonstantin Belousov cpu_spinwait(); 47486be9f0dSKonstantin Belousov return (0); 47586be9f0dSKonstantin Belousov } 47686be9f0dSKonstantin Belousov 47786be9f0dSKonstantin Belousov /* 47886be9f0dSKonstantin Belousov * Flush the chipset write buffers. See 11.1 "Write Buffer Flushing" 47986be9f0dSKonstantin Belousov * in the architecture specification. 48086be9f0dSKonstantin Belousov */ 48186be9f0dSKonstantin Belousov int 48286be9f0dSKonstantin Belousov dmar_flush_write_bufs(struct dmar_unit *unit) 48386be9f0dSKonstantin Belousov { 48486be9f0dSKonstantin Belousov 48586be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 48686be9f0dSKonstantin Belousov 48786be9f0dSKonstantin Belousov /* 48886be9f0dSKonstantin Belousov * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported. 48986be9f0dSKonstantin Belousov */ 49086be9f0dSKonstantin Belousov KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0, 49186be9f0dSKonstantin Belousov ("dmar%d: no RWBF", unit->unit)); 49286be9f0dSKonstantin Belousov 49386be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF); 49486be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 49586be9f0dSKonstantin Belousov while ((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS) == 0) 49686be9f0dSKonstantin Belousov cpu_spinwait(); 49786be9f0dSKonstantin Belousov return (0); 49886be9f0dSKonstantin Belousov } 49986be9f0dSKonstantin Belousov 50086be9f0dSKonstantin Belousov int 50186be9f0dSKonstantin Belousov dmar_enable_translation(struct dmar_unit *unit) 50286be9f0dSKonstantin Belousov { 50386be9f0dSKonstantin Belousov 50486be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 50586be9f0dSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_TE; 50686be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 50786be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 50886be9f0dSKonstantin Belousov while ((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) == 0) 50986be9f0dSKonstantin Belousov cpu_spinwait(); 51086be9f0dSKonstantin Belousov return (0); 51186be9f0dSKonstantin Belousov } 51286be9f0dSKonstantin Belousov 51386be9f0dSKonstantin Belousov int 51486be9f0dSKonstantin Belousov dmar_disable_translation(struct dmar_unit *unit) 51586be9f0dSKonstantin Belousov { 51686be9f0dSKonstantin Belousov 51786be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit); 51886be9f0dSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_TE; 51986be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd); 52086be9f0dSKonstantin Belousov /* XXXKIB should have a timeout */ 52186be9f0dSKonstantin Belousov while ((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES) != 0) 52286be9f0dSKonstantin Belousov cpu_spinwait(); 52386be9f0dSKonstantin Belousov return (0); 52486be9f0dSKonstantin Belousov } 52586be9f0dSKonstantin Belousov 52686be9f0dSKonstantin Belousov #define BARRIER_F \ 52786be9f0dSKonstantin Belousov u_int f_done, f_inproc, f_wakeup; \ 52886be9f0dSKonstantin Belousov \ 52986be9f0dSKonstantin Belousov f_done = 1 << (barrier_id * 3); \ 53086be9f0dSKonstantin Belousov f_inproc = 1 << (barrier_id * 3 + 1); \ 53186be9f0dSKonstantin Belousov f_wakeup = 1 << (barrier_id * 3 + 2) 53286be9f0dSKonstantin Belousov 53386be9f0dSKonstantin Belousov bool 53486be9f0dSKonstantin Belousov dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id) 53586be9f0dSKonstantin Belousov { 53686be9f0dSKonstantin Belousov BARRIER_F; 53786be9f0dSKonstantin Belousov 53886be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 53986be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_done) != 0) { 54086be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 54186be9f0dSKonstantin Belousov return (false); 54286be9f0dSKonstantin Belousov } 54386be9f0dSKonstantin Belousov 54486be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_inproc) != 0) { 54586be9f0dSKonstantin Belousov while ((dmar->barrier_flags & f_inproc) != 0) { 54686be9f0dSKonstantin Belousov dmar->barrier_flags |= f_wakeup; 54786be9f0dSKonstantin Belousov msleep(&dmar->barrier_flags, &dmar->lock, 0, 54886be9f0dSKonstantin Belousov "dmarb", 0); 54986be9f0dSKonstantin Belousov } 55086be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & f_done) != 0, 55186be9f0dSKonstantin Belousov ("dmar%d barrier %d missing done", dmar->unit, barrier_id)); 55286be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 55386be9f0dSKonstantin Belousov return (false); 55486be9f0dSKonstantin Belousov } 55586be9f0dSKonstantin Belousov 55686be9f0dSKonstantin Belousov dmar->barrier_flags |= f_inproc; 55786be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 55886be9f0dSKonstantin Belousov return (true); 55986be9f0dSKonstantin Belousov } 56086be9f0dSKonstantin Belousov 56186be9f0dSKonstantin Belousov void 56286be9f0dSKonstantin Belousov dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id) 56386be9f0dSKonstantin Belousov { 56486be9f0dSKonstantin Belousov BARRIER_F; 56586be9f0dSKonstantin Belousov 56686be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar); 56786be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc, 56886be9f0dSKonstantin Belousov ("dmar%d barrier %d missed entry", dmar->unit, barrier_id)); 56986be9f0dSKonstantin Belousov dmar->barrier_flags |= f_done; 57086be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_wakeup) != 0) 57186be9f0dSKonstantin Belousov wakeup(&dmar->barrier_flags); 57286be9f0dSKonstantin Belousov dmar->barrier_flags &= ~(f_inproc | f_wakeup); 57386be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar); 57486be9f0dSKonstantin Belousov } 57586be9f0dSKonstantin Belousov 57686be9f0dSKonstantin Belousov int dmar_match_verbose; 57786be9f0dSKonstantin Belousov 578af3b2549SHans Petter Selasky static SYSCTL_NODE(_hw, OID_AUTO, dmar, CTLFLAG_RD, NULL, ""); 579af3b2549SHans Petter Selasky SYSCTL_INT(_hw_dmar, OID_AUTO, tbl_pagecnt, CTLFLAG_RD, 58086be9f0dSKonstantin Belousov &dmar_tbl_pagecnt, 0, 58186be9f0dSKonstantin Belousov "Count of pages used for DMAR pagetables"); 582af3b2549SHans Petter Selasky SYSCTL_INT(_hw_dmar, OID_AUTO, match_verbose, CTLFLAG_RWTUN, 58386be9f0dSKonstantin Belousov &dmar_match_verbose, 0, 58486be9f0dSKonstantin Belousov "Verbose matching of the PCI devices to DMAR paths"); 58586be9f0dSKonstantin Belousov #ifdef INVARIANTS 58686be9f0dSKonstantin Belousov int dmar_check_free; 587af3b2549SHans Petter Selasky SYSCTL_INT(_hw_dmar, OID_AUTO, check_free, CTLFLAG_RWTUN, 58886be9f0dSKonstantin Belousov &dmar_check_free, 0, 58986be9f0dSKonstantin Belousov "Check the GPA RBtree for free_down and free_after validity"); 59086be9f0dSKonstantin Belousov #endif 59186be9f0dSKonstantin Belousov 592