186be9f0dSKonstantin Belousov /*- 2ebf5747bSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3ebf5747bSPedro F. Giffuni * 40a110d5bSKonstantin Belousov * Copyright (c) 2013-2015 The FreeBSD Foundation 586be9f0dSKonstantin Belousov * All rights reserved. 686be9f0dSKonstantin Belousov * 786be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 886be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 986be9f0dSKonstantin Belousov * 1086be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 1186be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions 1286be9f0dSKonstantin Belousov * are met: 1386be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 1486be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 1586be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 1686be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 1786be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution. 1886be9f0dSKonstantin Belousov * 1986be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2086be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2186be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2286be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2386be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2486be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2586be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2686be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2786be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2886be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2986be9f0dSKonstantin Belousov * SUCH DAMAGE. 3086be9f0dSKonstantin Belousov * 3186be9f0dSKonstantin Belousov * $FreeBSD$ 3286be9f0dSKonstantin Belousov */ 3386be9f0dSKonstantin Belousov 3486be9f0dSKonstantin Belousov #ifndef __X86_IOMMU_INTEL_DMAR_H 3586be9f0dSKonstantin Belousov #define __X86_IOMMU_INTEL_DMAR_H 3686be9f0dSKonstantin Belousov 3786be9f0dSKonstantin Belousov /* Host or physical memory address, after translation. */ 3886be9f0dSKonstantin Belousov typedef uint64_t dmar_haddr_t; 3986be9f0dSKonstantin Belousov /* Guest or bus address, before translation. */ 4086be9f0dSKonstantin Belousov typedef uint64_t dmar_gaddr_t; 4186be9f0dSKonstantin Belousov 4268eeb96aSKonstantin Belousov struct dmar_qi_genseq { 4368eeb96aSKonstantin Belousov u_int gen; 4468eeb96aSKonstantin Belousov uint32_t seq; 4568eeb96aSKonstantin Belousov }; 4668eeb96aSKonstantin Belousov 4786be9f0dSKonstantin Belousov struct dmar_map_entry { 4886be9f0dSKonstantin Belousov dmar_gaddr_t start; 4986be9f0dSKonstantin Belousov dmar_gaddr_t end; 5086be9f0dSKonstantin Belousov dmar_gaddr_t free_after; /* Free space after the entry */ 5186be9f0dSKonstantin Belousov dmar_gaddr_t free_down; /* Max free space below the 5286be9f0dSKonstantin Belousov current R/B tree node */ 5386be9f0dSKonstantin Belousov u_int flags; 5486be9f0dSKonstantin Belousov TAILQ_ENTRY(dmar_map_entry) dmamap_link; /* Link for dmamap entries */ 551abfd355SKonstantin Belousov RB_ENTRY(dmar_map_entry) rb_entry; /* Links for domain entries */ 5686be9f0dSKonstantin Belousov TAILQ_ENTRY(dmar_map_entry) unroll_link; /* Link for unroll after 5786be9f0dSKonstantin Belousov dmamap_load failure */ 581abfd355SKonstantin Belousov struct dmar_domain *domain; 5968eeb96aSKonstantin Belousov struct dmar_qi_genseq gseq; 6086be9f0dSKonstantin Belousov }; 6186be9f0dSKonstantin Belousov 6286be9f0dSKonstantin Belousov RB_HEAD(dmar_gas_entries_tree, dmar_map_entry); 6386be9f0dSKonstantin Belousov RB_PROTOTYPE(dmar_gas_entries_tree, dmar_map_entry, rb_entry, 6486be9f0dSKonstantin Belousov dmar_gas_cmp_entries); 6586be9f0dSKonstantin Belousov 6686be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_PLACE 0x0001 /* Fake entry */ 6786be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_RMRR 0x0002 /* Permanent, not linked by 6886be9f0dSKonstantin Belousov dmamap_link */ 6986be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_MAP 0x0004 /* Busdma created, linked by 7086be9f0dSKonstantin Belousov dmamap_link */ 7186be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_UNMAPPED 0x0010 /* No backing pages */ 7268eeb96aSKonstantin Belousov #define DMAR_MAP_ENTRY_QI_NF 0x0020 /* qi task, do not free entry */ 7386be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_READ 0x1000 /* Read permitted */ 7486be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_WRITE 0x2000 /* Write permitted */ 7586be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_SNOOP 0x4000 /* Snoop */ 7686be9f0dSKonstantin Belousov #define DMAR_MAP_ENTRY_TM 0x8000 /* Transient */ 7786be9f0dSKonstantin Belousov 781abfd355SKonstantin Belousov /* 791abfd355SKonstantin Belousov * Locking annotations: 801abfd355SKonstantin Belousov * (u) - Protected by dmar unit lock 811abfd355SKonstantin Belousov * (d) - Protected by domain lock 821abfd355SKonstantin Belousov * (c) - Immutable after initialization 831abfd355SKonstantin Belousov */ 841abfd355SKonstantin Belousov 851abfd355SKonstantin Belousov /* 861abfd355SKonstantin Belousov * The domain abstraction. Most non-constant members of the domain 87ad969cb1SKonstantin Belousov * are protected by owning dmar unit lock, not by the domain lock. 88ad969cb1SKonstantin Belousov * Most important, the dmar lock protects the contexts list. 891abfd355SKonstantin Belousov * 901abfd355SKonstantin Belousov * The domain lock protects the address map for the domain, and list 911abfd355SKonstantin Belousov * of unload entries delayed. 921abfd355SKonstantin Belousov * 931abfd355SKonstantin Belousov * Page tables pages and pages content is protected by the vm object 941abfd355SKonstantin Belousov * lock pgtbl_obj, which contains the page tables pages. 951abfd355SKonstantin Belousov */ 961abfd355SKonstantin Belousov struct dmar_domain { 971abfd355SKonstantin Belousov int domain; /* (c) DID, written in context entry */ 981abfd355SKonstantin Belousov int mgaw; /* (c) Real max address width */ 991abfd355SKonstantin Belousov int agaw; /* (c) Adjusted guest address width */ 1001abfd355SKonstantin Belousov int pglvl; /* (c) The pagelevel */ 1011abfd355SKonstantin Belousov int awlvl; /* (c) The pagelevel as the bitmask, 1021abfd355SKonstantin Belousov to set in context entry */ 1031abfd355SKonstantin Belousov dmar_gaddr_t end; /* (c) Highest address + 1 in 1041abfd355SKonstantin Belousov the guest AS */ 1051abfd355SKonstantin Belousov u_int ctx_cnt; /* (u) Number of contexts owned */ 1061abfd355SKonstantin Belousov u_int refs; /* (u) Refs, including ctx */ 1071abfd355SKonstantin Belousov struct dmar_unit *dmar; /* (c) */ 1081abfd355SKonstantin Belousov struct mtx lock; /* (c) */ 1091abfd355SKonstantin Belousov LIST_ENTRY(dmar_domain) link; /* (u) Member in the dmar list */ 1101abfd355SKonstantin Belousov LIST_HEAD(, dmar_ctx) contexts; /* (u) */ 1111abfd355SKonstantin Belousov vm_object_t pgtbl_obj; /* (c) Page table pages */ 1121abfd355SKonstantin Belousov u_int flags; /* (u) */ 1131abfd355SKonstantin Belousov u_int entries_cnt; /* (d) */ 1141abfd355SKonstantin Belousov struct dmar_gas_entries_tree rb_root; /* (d) */ 1151abfd355SKonstantin Belousov struct dmar_map_entries_tailq unload_entries; /* (d) Entries to 1161abfd355SKonstantin Belousov unload */ 1171abfd355SKonstantin Belousov struct dmar_map_entry *first_place, *last_place; /* (d) */ 1181abfd355SKonstantin Belousov struct task unload_task; /* (c) */ 119e164cafcSKonstantin Belousov u_int batch_no; 12086be9f0dSKonstantin Belousov }; 12186be9f0dSKonstantin Belousov 1221abfd355SKonstantin Belousov struct dmar_ctx { 1231abfd355SKonstantin Belousov struct bus_dma_tag_dmar ctx_tag; /* (c) Root tag */ 1241abfd355SKonstantin Belousov uint16_t rid; /* (c) pci RID */ 1251abfd355SKonstantin Belousov uint64_t last_fault_rec[2]; /* Last fault reported */ 1261abfd355SKonstantin Belousov struct dmar_domain *domain; /* (c) */ 1271abfd355SKonstantin Belousov LIST_ENTRY(dmar_ctx) link; /* (u) Member in the domain list */ 1281abfd355SKonstantin Belousov u_int refs; /* (u) References from tags */ 1291abfd355SKonstantin Belousov u_int flags; /* (u) */ 1301abfd355SKonstantin Belousov u_long loads; /* atomic updates, for stat only */ 1311abfd355SKonstantin Belousov u_long unloads; /* same */ 1321abfd355SKonstantin Belousov }; 1331abfd355SKonstantin Belousov 1341abfd355SKonstantin Belousov #define DMAR_DOMAIN_GAS_INITED 0x0001 1351abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGTBL_INITED 0x0002 1361abfd355SKonstantin Belousov #define DMAR_DOMAIN_IDMAP 0x0010 /* Domain uses identity 1371abfd355SKonstantin Belousov page table */ 1381abfd355SKonstantin Belousov #define DMAR_DOMAIN_RMRR 0x0020 /* Domain contains RMRR entry, 1391abfd355SKonstantin Belousov cannot be turned off */ 1401abfd355SKonstantin Belousov 14186be9f0dSKonstantin Belousov /* struct dmar_ctx flags */ 14286be9f0dSKonstantin Belousov #define DMAR_CTX_FAULTED 0x0001 /* Fault was reported, 14386be9f0dSKonstantin Belousov last_fault_rec is valid */ 1441abfd355SKonstantin Belousov #define DMAR_CTX_DISABLED 0x0002 /* Device is disabled, the 14586be9f0dSKonstantin Belousov ephemeral reference is kept 14686be9f0dSKonstantin Belousov to prevent context destruction */ 14786be9f0dSKonstantin Belousov 1481abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGLOCK(dom) VM_OBJECT_WLOCK((dom)->pgtbl_obj) 1491abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGTRYLOCK(dom) VM_OBJECT_TRYWLOCK((dom)->pgtbl_obj) 1501abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGUNLOCK(dom) VM_OBJECT_WUNLOCK((dom)->pgtbl_obj) 1511abfd355SKonstantin Belousov #define DMAR_DOMAIN_ASSERT_PGLOCKED(dom) \ 1521abfd355SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED((dom)->pgtbl_obj) 15386be9f0dSKonstantin Belousov 1541abfd355SKonstantin Belousov #define DMAR_DOMAIN_LOCK(dom) mtx_lock(&(dom)->lock) 1551abfd355SKonstantin Belousov #define DMAR_DOMAIN_UNLOCK(dom) mtx_unlock(&(dom)->lock) 1561abfd355SKonstantin Belousov #define DMAR_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->lock, MA_OWNED) 15786be9f0dSKonstantin Belousov 15868eeb96aSKonstantin Belousov struct dmar_msi_data { 15968eeb96aSKonstantin Belousov int irq; 16068eeb96aSKonstantin Belousov int irq_rid; 16168eeb96aSKonstantin Belousov struct resource *irq_res; 16268eeb96aSKonstantin Belousov void *intr_handle; 16368eeb96aSKonstantin Belousov int (*handler)(void *); 16468eeb96aSKonstantin Belousov int msi_data_reg; 16568eeb96aSKonstantin Belousov int msi_addr_reg; 16668eeb96aSKonstantin Belousov int msi_uaddr_reg; 16768eeb96aSKonstantin Belousov void (*enable_intr)(struct dmar_unit *); 16868eeb96aSKonstantin Belousov void (*disable_intr)(struct dmar_unit *); 16968eeb96aSKonstantin Belousov const char *name; 17068eeb96aSKonstantin Belousov }; 17168eeb96aSKonstantin Belousov 17268eeb96aSKonstantin Belousov #define DMAR_INTR_FAULT 0 17368eeb96aSKonstantin Belousov #define DMAR_INTR_QI 1 17468eeb96aSKonstantin Belousov #define DMAR_INTR_TOTAL 2 17568eeb96aSKonstantin Belousov 17686be9f0dSKonstantin Belousov struct dmar_unit { 17786be9f0dSKonstantin Belousov device_t dev; 17886be9f0dSKonstantin Belousov int unit; 17986be9f0dSKonstantin Belousov uint16_t segment; 18086be9f0dSKonstantin Belousov uint64_t base; 18186be9f0dSKonstantin Belousov 18286be9f0dSKonstantin Belousov /* Resources */ 18386be9f0dSKonstantin Belousov int reg_rid; 18486be9f0dSKonstantin Belousov struct resource *regs; 18568eeb96aSKonstantin Belousov 18668eeb96aSKonstantin Belousov struct dmar_msi_data intrs[DMAR_INTR_TOTAL]; 18786be9f0dSKonstantin Belousov 18886be9f0dSKonstantin Belousov /* Hardware registers cache */ 18986be9f0dSKonstantin Belousov uint32_t hw_ver; 19086be9f0dSKonstantin Belousov uint64_t hw_cap; 19186be9f0dSKonstantin Belousov uint64_t hw_ecap; 19286be9f0dSKonstantin Belousov uint32_t hw_gcmd; 19386be9f0dSKonstantin Belousov 19486be9f0dSKonstantin Belousov /* Data for being a dmar */ 19586be9f0dSKonstantin Belousov struct mtx lock; 1961abfd355SKonstantin Belousov LIST_HEAD(, dmar_domain) domains; 19786be9f0dSKonstantin Belousov struct unrhdr *domids; 19886be9f0dSKonstantin Belousov vm_object_t ctx_obj; 19986be9f0dSKonstantin Belousov u_int barrier_flags; 20086be9f0dSKonstantin Belousov 20186be9f0dSKonstantin Belousov /* Fault handler data */ 20286be9f0dSKonstantin Belousov struct mtx fault_lock; 20386be9f0dSKonstantin Belousov uint64_t *fault_log; 20486be9f0dSKonstantin Belousov int fault_log_head; 20586be9f0dSKonstantin Belousov int fault_log_tail; 20686be9f0dSKonstantin Belousov int fault_log_size; 20786be9f0dSKonstantin Belousov struct task fault_task; 20886be9f0dSKonstantin Belousov struct taskqueue *fault_taskqueue; 20986be9f0dSKonstantin Belousov 21068eeb96aSKonstantin Belousov /* QI */ 21168eeb96aSKonstantin Belousov int qi_enabled; 21268eeb96aSKonstantin Belousov vm_offset_t inv_queue; 21368eeb96aSKonstantin Belousov vm_size_t inv_queue_size; 21468eeb96aSKonstantin Belousov uint32_t inv_queue_avail; 21568eeb96aSKonstantin Belousov uint32_t inv_queue_tail; 21668eeb96aSKonstantin Belousov volatile uint32_t inv_waitd_seq_hw; /* hw writes there on wait 21768eeb96aSKonstantin Belousov descr completion */ 21868eeb96aSKonstantin Belousov uint64_t inv_waitd_seq_hw_phys; 21968eeb96aSKonstantin Belousov uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */ 22068eeb96aSKonstantin Belousov u_int inv_waitd_gen; /* seq number generation AKA seq overflows */ 22168eeb96aSKonstantin Belousov u_int inv_seq_waiters; /* count of waiters for seq */ 22268eeb96aSKonstantin Belousov u_int inv_queue_full; /* informational counter */ 22368eeb96aSKonstantin Belousov 2240a110d5bSKonstantin Belousov /* IR */ 2250a110d5bSKonstantin Belousov int ir_enabled; 2260a110d5bSKonstantin Belousov vm_paddr_t irt_phys; 2270a110d5bSKonstantin Belousov dmar_irte_t *irt; 2280a110d5bSKonstantin Belousov u_int irte_cnt; 2290a110d5bSKonstantin Belousov vmem_t *irtids; 2300a110d5bSKonstantin Belousov 23168eeb96aSKonstantin Belousov /* Delayed freeing of map entries queue processing */ 23268eeb96aSKonstantin Belousov struct dmar_map_entries_tailq tlb_flush_entries; 23368eeb96aSKonstantin Belousov struct task qi_task; 23468eeb96aSKonstantin Belousov struct taskqueue *qi_taskqueue; 23568eeb96aSKonstantin Belousov 23686be9f0dSKonstantin Belousov /* Busdma delayed map load */ 23786be9f0dSKonstantin Belousov struct task dmamap_load_task; 23886be9f0dSKonstantin Belousov TAILQ_HEAD(, bus_dmamap_dmar) delayed_maps; 23986be9f0dSKonstantin Belousov struct taskqueue *delayed_taskqueue; 2400a110d5bSKonstantin Belousov 2410a110d5bSKonstantin Belousov int dma_enabled; 24286be9f0dSKonstantin Belousov }; 24386be9f0dSKonstantin Belousov 24486be9f0dSKonstantin Belousov #define DMAR_LOCK(dmar) mtx_lock(&(dmar)->lock) 24586be9f0dSKonstantin Belousov #define DMAR_UNLOCK(dmar) mtx_unlock(&(dmar)->lock) 24686be9f0dSKonstantin Belousov #define DMAR_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->lock, MA_OWNED) 24786be9f0dSKonstantin Belousov 24886be9f0dSKonstantin Belousov #define DMAR_FAULT_LOCK(dmar) mtx_lock_spin(&(dmar)->fault_lock) 24986be9f0dSKonstantin Belousov #define DMAR_FAULT_UNLOCK(dmar) mtx_unlock_spin(&(dmar)->fault_lock) 25086be9f0dSKonstantin Belousov #define DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED) 25186be9f0dSKonstantin Belousov 25286be9f0dSKonstantin Belousov #define DMAR_IS_COHERENT(dmar) (((dmar)->hw_ecap & DMAR_ECAP_C) != 0) 25368eeb96aSKonstantin Belousov #define DMAR_HAS_QI(dmar) (((dmar)->hw_ecap & DMAR_ECAP_QI) != 0) 2540a110d5bSKonstantin Belousov #define DMAR_X2APIC(dmar) \ 2550a110d5bSKonstantin Belousov (x2apic_mode && ((dmar)->hw_ecap & DMAR_ECAP_EIM) != 0) 25686be9f0dSKonstantin Belousov 25786be9f0dSKonstantin Belousov /* Barrier ids */ 25886be9f0dSKonstantin Belousov #define DMAR_BARRIER_RMRR 0 25986be9f0dSKonstantin Belousov #define DMAR_BARRIER_USEQ 1 26086be9f0dSKonstantin Belousov 261*f9feb091SKonstantin Belousov struct dmar_unit *dmar_find(device_t dev, bool verbose); 2620a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_hpet(device_t dev, uint16_t *rid); 2630a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_ioapic(u_int apic_id, uint16_t *rid); 26486be9f0dSKonstantin Belousov 26586be9f0dSKonstantin Belousov u_int dmar_nd2mask(u_int nd); 26686be9f0dSKonstantin Belousov bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl); 2671abfd355SKonstantin Belousov int domain_set_agaw(struct dmar_domain *domain, int mgaw); 26886be9f0dSKonstantin Belousov int dmar_maxaddr2mgaw(struct dmar_unit *unit, dmar_gaddr_t maxaddr, 26986be9f0dSKonstantin Belousov bool allow_less); 27086be9f0dSKonstantin Belousov vm_pindex_t pglvl_max_pages(int pglvl); 2711abfd355SKonstantin Belousov int domain_is_sp_lvl(struct dmar_domain *domain, int lvl); 27286be9f0dSKonstantin Belousov dmar_gaddr_t pglvl_page_size(int total_pglvl, int lvl); 2731abfd355SKonstantin Belousov dmar_gaddr_t domain_page_size(struct dmar_domain *domain, int lvl); 27468eeb96aSKonstantin Belousov int calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size, 27568eeb96aSKonstantin Belousov dmar_gaddr_t *isizep); 27686be9f0dSKonstantin Belousov struct vm_page *dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags); 27786be9f0dSKonstantin Belousov void dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags); 27886be9f0dSKonstantin Belousov void *dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags, 27986be9f0dSKonstantin Belousov struct sf_buf **sf); 2806b7c46afSKonstantin Belousov void dmar_unmap_pgtbl(struct sf_buf *sf); 28186be9f0dSKonstantin Belousov int dmar_load_root_entry_ptr(struct dmar_unit *unit); 28286be9f0dSKonstantin Belousov int dmar_inv_ctx_glob(struct dmar_unit *unit); 28386be9f0dSKonstantin Belousov int dmar_inv_iotlb_glob(struct dmar_unit *unit); 28486be9f0dSKonstantin Belousov int dmar_flush_write_bufs(struct dmar_unit *unit); 2856b7c46afSKonstantin Belousov void dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst); 2866b7c46afSKonstantin Belousov void dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst); 2876b7c46afSKonstantin Belousov void dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst); 28886be9f0dSKonstantin Belousov int dmar_enable_translation(struct dmar_unit *unit); 28986be9f0dSKonstantin Belousov int dmar_disable_translation(struct dmar_unit *unit); 2900a110d5bSKonstantin Belousov int dmar_load_irt_ptr(struct dmar_unit *unit); 2910a110d5bSKonstantin Belousov int dmar_enable_ir(struct dmar_unit *unit); 2920a110d5bSKonstantin Belousov int dmar_disable_ir(struct dmar_unit *unit); 29386be9f0dSKonstantin Belousov bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id); 29486be9f0dSKonstantin Belousov void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id); 295476358b3SKonstantin Belousov uint64_t dmar_get_timeout(void); 296476358b3SKonstantin Belousov void dmar_update_timeout(uint64_t newval); 29786be9f0dSKonstantin Belousov 29868eeb96aSKonstantin Belousov int dmar_fault_intr(void *arg); 29968eeb96aSKonstantin Belousov void dmar_enable_fault_intr(struct dmar_unit *unit); 30068eeb96aSKonstantin Belousov void dmar_disable_fault_intr(struct dmar_unit *unit); 30186be9f0dSKonstantin Belousov int dmar_init_fault_log(struct dmar_unit *unit); 30286be9f0dSKonstantin Belousov void dmar_fini_fault_log(struct dmar_unit *unit); 30386be9f0dSKonstantin Belousov 30468eeb96aSKonstantin Belousov int dmar_qi_intr(void *arg); 30568eeb96aSKonstantin Belousov void dmar_enable_qi_intr(struct dmar_unit *unit); 30668eeb96aSKonstantin Belousov void dmar_disable_qi_intr(struct dmar_unit *unit); 30768eeb96aSKonstantin Belousov int dmar_init_qi(struct dmar_unit *unit); 30868eeb96aSKonstantin Belousov void dmar_fini_qi(struct dmar_unit *unit); 3091abfd355SKonstantin Belousov void dmar_qi_invalidate_locked(struct dmar_domain *domain, dmar_gaddr_t start, 310cf619a92SKonstantin Belousov dmar_gaddr_t size, struct dmar_qi_genseq *psec, bool emit_wait); 31168eeb96aSKonstantin Belousov void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit); 31268eeb96aSKonstantin Belousov void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit); 3130a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec_glob(struct dmar_unit *unit); 3140a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec(struct dmar_unit *unit, u_int start, u_int cnt); 31568eeb96aSKonstantin Belousov 3161abfd355SKonstantin Belousov vm_object_t domain_get_idmap_pgtbl(struct dmar_domain *domain, 3171abfd355SKonstantin Belousov dmar_gaddr_t maxaddr); 31886be9f0dSKonstantin Belousov void put_idmap_pgtbl(vm_object_t obj); 3191abfd355SKonstantin Belousov int domain_map_buf(struct dmar_domain *domain, dmar_gaddr_t base, 3201abfd355SKonstantin Belousov dmar_gaddr_t size, vm_page_t *ma, uint64_t pflags, int flags); 3211abfd355SKonstantin Belousov int domain_unmap_buf(struct dmar_domain *domain, dmar_gaddr_t base, 3221abfd355SKonstantin Belousov dmar_gaddr_t size, int flags); 3231abfd355SKonstantin Belousov void domain_flush_iotlb_sync(struct dmar_domain *domain, dmar_gaddr_t base, 32468eeb96aSKonstantin Belousov dmar_gaddr_t size); 3251abfd355SKonstantin Belousov int domain_alloc_pgtbl(struct dmar_domain *domain); 3261abfd355SKonstantin Belousov void domain_free_pgtbl(struct dmar_domain *domain); 32786be9f0dSKonstantin Belousov 328*f9feb091SKonstantin Belousov int dmar_dev_depth(device_t child); 329*f9feb091SKonstantin Belousov void dmar_dev_path(device_t child, int *busno, void *path1, int depth); 330*f9feb091SKonstantin Belousov 33186be9f0dSKonstantin Belousov struct dmar_ctx *dmar_instantiate_ctx(struct dmar_unit *dmar, device_t dev, 33286be9f0dSKonstantin Belousov bool rmrr); 3331abfd355SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev, 33467499354SRyan Stone uint16_t rid, bool id_mapped, bool rmrr_init); 335*f9feb091SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid, 336*f9feb091SKonstantin Belousov int dev_domain, int dev_busno, const void *dev_path, int dev_path_len, 337*f9feb091SKonstantin Belousov bool id_mapped, bool rmrr_init); 3381abfd355SKonstantin Belousov int dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx); 33986be9f0dSKonstantin Belousov void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx); 34086be9f0dSKonstantin Belousov void dmar_free_ctx(struct dmar_ctx *ctx); 34167499354SRyan Stone struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid); 3421abfd355SKonstantin Belousov void dmar_domain_unload_entry(struct dmar_map_entry *entry, bool free); 3431abfd355SKonstantin Belousov void dmar_domain_unload(struct dmar_domain *domain, 34486be9f0dSKonstantin Belousov struct dmar_map_entries_tailq *entries, bool cansleep); 3451abfd355SKonstantin Belousov void dmar_domain_free_entry(struct dmar_map_entry *entry, bool free); 34686be9f0dSKonstantin Belousov 34786be9f0dSKonstantin Belousov int dmar_init_busdma(struct dmar_unit *unit); 34886be9f0dSKonstantin Belousov void dmar_fini_busdma(struct dmar_unit *unit); 3490a110d5bSKonstantin Belousov device_t dmar_get_requester(device_t dev, uint16_t *rid); 35086be9f0dSKonstantin Belousov 3511abfd355SKonstantin Belousov void dmar_gas_init_domain(struct dmar_domain *domain); 3521abfd355SKonstantin Belousov void dmar_gas_fini_domain(struct dmar_domain *domain); 3531abfd355SKonstantin Belousov struct dmar_map_entry *dmar_gas_alloc_entry(struct dmar_domain *domain, 3541abfd355SKonstantin Belousov u_int flags); 3551abfd355SKonstantin Belousov void dmar_gas_free_entry(struct dmar_domain *domain, 3561abfd355SKonstantin Belousov struct dmar_map_entry *entry); 3571abfd355SKonstantin Belousov void dmar_gas_free_space(struct dmar_domain *domain, 3581abfd355SKonstantin Belousov struct dmar_map_entry *entry); 3591abfd355SKonstantin Belousov int dmar_gas_map(struct dmar_domain *domain, 3601abfd355SKonstantin Belousov const struct bus_dma_tag_common *common, dmar_gaddr_t size, int offset, 3611abfd355SKonstantin Belousov u_int eflags, u_int flags, vm_page_t *ma, struct dmar_map_entry **res); 3621abfd355SKonstantin Belousov void dmar_gas_free_region(struct dmar_domain *domain, 3631abfd355SKonstantin Belousov struct dmar_map_entry *entry); 3641abfd355SKonstantin Belousov int dmar_gas_map_region(struct dmar_domain *domain, 3651abfd355SKonstantin Belousov struct dmar_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma); 3661abfd355SKonstantin Belousov int dmar_gas_reserve_region(struct dmar_domain *domain, dmar_gaddr_t start, 36786be9f0dSKonstantin Belousov dmar_gaddr_t end); 36886be9f0dSKonstantin Belousov 369*f9feb091SKonstantin Belousov void dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain, 370*f9feb091SKonstantin Belousov int dev_busno, const void *dev_path, int dev_path_len, 37186be9f0dSKonstantin Belousov struct dmar_map_entries_tailq *rmrr_entries); 37286be9f0dSKonstantin Belousov int dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar); 37386be9f0dSKonstantin Belousov 37486be9f0dSKonstantin Belousov void dmar_quirks_post_ident(struct dmar_unit *dmar); 37586be9f0dSKonstantin Belousov void dmar_quirks_pre_use(struct dmar_unit *dmar); 37686be9f0dSKonstantin Belousov 3770a110d5bSKonstantin Belousov int dmar_init_irt(struct dmar_unit *unit); 3780a110d5bSKonstantin Belousov void dmar_fini_irt(struct dmar_unit *unit); 3790a110d5bSKonstantin Belousov 38086be9f0dSKonstantin Belousov #define DMAR_GM_CANWAIT 0x0001 38186be9f0dSKonstantin Belousov #define DMAR_GM_CANSPLIT 0x0002 38286be9f0dSKonstantin Belousov 38386be9f0dSKonstantin Belousov #define DMAR_PGF_WAITOK 0x0001 38486be9f0dSKonstantin Belousov #define DMAR_PGF_ZERO 0x0002 38586be9f0dSKonstantin Belousov #define DMAR_PGF_ALLOC 0x0004 38686be9f0dSKonstantin Belousov #define DMAR_PGF_NOALLOC 0x0008 38786be9f0dSKonstantin Belousov #define DMAR_PGF_OBJL 0x0010 38886be9f0dSKonstantin Belousov 38986be9f0dSKonstantin Belousov extern dmar_haddr_t dmar_high; 39086be9f0dSKonstantin Belousov extern int haw; 39186be9f0dSKonstantin Belousov extern int dmar_tbl_pagecnt; 392e164cafcSKonstantin Belousov extern int dmar_batch_coalesce; 39386be9f0dSKonstantin Belousov extern int dmar_check_free; 39486be9f0dSKonstantin Belousov 39586be9f0dSKonstantin Belousov static inline uint32_t 39686be9f0dSKonstantin Belousov dmar_read4(const struct dmar_unit *unit, int reg) 39786be9f0dSKonstantin Belousov { 39886be9f0dSKonstantin Belousov 39986be9f0dSKonstantin Belousov return (bus_read_4(unit->regs, reg)); 40086be9f0dSKonstantin Belousov } 40186be9f0dSKonstantin Belousov 40286be9f0dSKonstantin Belousov static inline uint64_t 40386be9f0dSKonstantin Belousov dmar_read8(const struct dmar_unit *unit, int reg) 40486be9f0dSKonstantin Belousov { 40586be9f0dSKonstantin Belousov #ifdef __i386__ 40686be9f0dSKonstantin Belousov uint32_t high, low; 40786be9f0dSKonstantin Belousov 40886be9f0dSKonstantin Belousov low = bus_read_4(unit->regs, reg); 40986be9f0dSKonstantin Belousov high = bus_read_4(unit->regs, reg + 4); 41086be9f0dSKonstantin Belousov return (low | ((uint64_t)high << 32)); 41186be9f0dSKonstantin Belousov #else 41286be9f0dSKonstantin Belousov return (bus_read_8(unit->regs, reg)); 41386be9f0dSKonstantin Belousov #endif 41486be9f0dSKonstantin Belousov } 41586be9f0dSKonstantin Belousov 41686be9f0dSKonstantin Belousov static inline void 41786be9f0dSKonstantin Belousov dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val) 41886be9f0dSKonstantin Belousov { 41986be9f0dSKonstantin Belousov 42086be9f0dSKonstantin Belousov KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) == 42186be9f0dSKonstantin Belousov (unit->hw_gcmd & DMAR_GCMD_TE), 42286be9f0dSKonstantin Belousov ("dmar%d clearing TE 0x%08x 0x%08x", unit->unit, 42386be9f0dSKonstantin Belousov unit->hw_gcmd, val)); 42486be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg, val); 42586be9f0dSKonstantin Belousov } 42686be9f0dSKonstantin Belousov 42786be9f0dSKonstantin Belousov static inline void 42886be9f0dSKonstantin Belousov dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val) 42986be9f0dSKonstantin Belousov { 43086be9f0dSKonstantin Belousov 43186be9f0dSKonstantin Belousov KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write")); 43286be9f0dSKonstantin Belousov #ifdef __i386__ 43386be9f0dSKonstantin Belousov uint32_t high, low; 43486be9f0dSKonstantin Belousov 43586be9f0dSKonstantin Belousov low = val; 43686be9f0dSKonstantin Belousov high = val >> 32; 43786be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg, low); 43886be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg + 4, high); 43986be9f0dSKonstantin Belousov #else 44086be9f0dSKonstantin Belousov bus_write_8(unit->regs, reg, val); 44186be9f0dSKonstantin Belousov #endif 44286be9f0dSKonstantin Belousov } 44386be9f0dSKonstantin Belousov 44486be9f0dSKonstantin Belousov /* 44586be9f0dSKonstantin Belousov * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes 44686be9f0dSKonstantin Belousov * are issued in the correct order. For store, the lower word, 44786be9f0dSKonstantin Belousov * containing the P or R and W bits, is set only after the high word 44886be9f0dSKonstantin Belousov * is written. For clear, the P bit is cleared first, then the high 44986be9f0dSKonstantin Belousov * word is cleared. 4500a110d5bSKonstantin Belousov * 4510a110d5bSKonstantin Belousov * dmar_pte_update updates the pte. For amd64, the update is atomic. 4520a110d5bSKonstantin Belousov * For i386, it first disables the entry by clearing the word 4530a110d5bSKonstantin Belousov * containing the P bit, and then defer to dmar_pte_store. The locked 4540a110d5bSKonstantin Belousov * cmpxchg8b is probably available on any machine having DMAR support, 4550a110d5bSKonstantin Belousov * but interrupt translation table may be mapped uncached. 45686be9f0dSKonstantin Belousov */ 45786be9f0dSKonstantin Belousov static inline void 4580a110d5bSKonstantin Belousov dmar_pte_store1(volatile uint64_t *dst, uint64_t val) 45986be9f0dSKonstantin Belousov { 46086be9f0dSKonstantin Belousov #ifdef __i386__ 46186be9f0dSKonstantin Belousov volatile uint32_t *p; 46286be9f0dSKonstantin Belousov uint32_t hi, lo; 46386be9f0dSKonstantin Belousov 46486be9f0dSKonstantin Belousov hi = val >> 32; 46586be9f0dSKonstantin Belousov lo = val; 46686be9f0dSKonstantin Belousov p = (volatile uint32_t *)dst; 46786be9f0dSKonstantin Belousov *(p + 1) = hi; 46886be9f0dSKonstantin Belousov *p = lo; 46986be9f0dSKonstantin Belousov #else 47086be9f0dSKonstantin Belousov *dst = val; 47186be9f0dSKonstantin Belousov #endif 47286be9f0dSKonstantin Belousov } 47386be9f0dSKonstantin Belousov 47486be9f0dSKonstantin Belousov static inline void 4750a110d5bSKonstantin Belousov dmar_pte_store(volatile uint64_t *dst, uint64_t val) 4760a110d5bSKonstantin Belousov { 4770a110d5bSKonstantin Belousov 4780a110d5bSKonstantin Belousov KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx", 4790a110d5bSKonstantin Belousov dst, (uintmax_t)*dst, (uintmax_t)val)); 4800a110d5bSKonstantin Belousov dmar_pte_store1(dst, val); 4810a110d5bSKonstantin Belousov } 4820a110d5bSKonstantin Belousov 4830a110d5bSKonstantin Belousov static inline void 4840a110d5bSKonstantin Belousov dmar_pte_update(volatile uint64_t *dst, uint64_t val) 4850a110d5bSKonstantin Belousov { 4860a110d5bSKonstantin Belousov 4870a110d5bSKonstantin Belousov #ifdef __i386__ 4880a110d5bSKonstantin Belousov volatile uint32_t *p; 4890a110d5bSKonstantin Belousov 4900a110d5bSKonstantin Belousov p = (volatile uint32_t *)dst; 4910a110d5bSKonstantin Belousov *p = 0; 4920a110d5bSKonstantin Belousov #endif 4930a110d5bSKonstantin Belousov dmar_pte_store1(dst, val); 4940a110d5bSKonstantin Belousov } 4950a110d5bSKonstantin Belousov 4960a110d5bSKonstantin Belousov static inline void 49786be9f0dSKonstantin Belousov dmar_pte_clear(volatile uint64_t *dst) 49886be9f0dSKonstantin Belousov { 49986be9f0dSKonstantin Belousov #ifdef __i386__ 50086be9f0dSKonstantin Belousov volatile uint32_t *p; 50186be9f0dSKonstantin Belousov 50286be9f0dSKonstantin Belousov p = (volatile uint32_t *)dst; 50386be9f0dSKonstantin Belousov *p = 0; 50486be9f0dSKonstantin Belousov *(p + 1) = 0; 50586be9f0dSKonstantin Belousov #else 50686be9f0dSKonstantin Belousov *dst = 0; 50786be9f0dSKonstantin Belousov #endif 50886be9f0dSKonstantin Belousov } 50986be9f0dSKonstantin Belousov 51086be9f0dSKonstantin Belousov static inline bool 51186be9f0dSKonstantin Belousov dmar_test_boundary(dmar_gaddr_t start, dmar_gaddr_t size, 51286be9f0dSKonstantin Belousov dmar_gaddr_t boundary) 51386be9f0dSKonstantin Belousov { 51486be9f0dSKonstantin Belousov 51586be9f0dSKonstantin Belousov if (boundary == 0) 51686be9f0dSKonstantin Belousov return (true); 51786be9f0dSKonstantin Belousov return (start + size <= ((start + boundary) & ~(boundary - 1))); 51886be9f0dSKonstantin Belousov } 51986be9f0dSKonstantin Belousov 520476358b3SKonstantin Belousov extern struct timespec dmar_hw_timeout; 521476358b3SKonstantin Belousov 522476358b3SKonstantin Belousov #define DMAR_WAIT_UNTIL(cond) \ 523476358b3SKonstantin Belousov { \ 524476358b3SKonstantin Belousov struct timespec last, curr; \ 525476358b3SKonstantin Belousov bool forever; \ 526476358b3SKonstantin Belousov \ 527476358b3SKonstantin Belousov if (dmar_hw_timeout.tv_sec == 0 && \ 528476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec == 0) { \ 529476358b3SKonstantin Belousov forever = true; \ 530476358b3SKonstantin Belousov } else { \ 531476358b3SKonstantin Belousov forever = false; \ 532476358b3SKonstantin Belousov nanouptime(&curr); \ 5336040822cSAlan Somers timespecadd(&curr, &dmar_hw_timeout, &last); \ 534476358b3SKonstantin Belousov } \ 535476358b3SKonstantin Belousov for (;;) { \ 536476358b3SKonstantin Belousov if (cond) { \ 537476358b3SKonstantin Belousov error = 0; \ 538476358b3SKonstantin Belousov break; \ 539476358b3SKonstantin Belousov } \ 540476358b3SKonstantin Belousov nanouptime(&curr); \ 541476358b3SKonstantin Belousov if (!forever && timespeccmp(&last, &curr, <)) { \ 542476358b3SKonstantin Belousov error = ETIMEDOUT; \ 543476358b3SKonstantin Belousov break; \ 544476358b3SKonstantin Belousov } \ 545476358b3SKonstantin Belousov cpu_spinwait(); \ 546476358b3SKonstantin Belousov } \ 547476358b3SKonstantin Belousov } 548476358b3SKonstantin Belousov 54986be9f0dSKonstantin Belousov #ifdef INVARIANTS 55086be9f0dSKonstantin Belousov #define TD_PREP_PINNED_ASSERT \ 55186be9f0dSKonstantin Belousov int old_td_pinned; \ 55286be9f0dSKonstantin Belousov old_td_pinned = curthread->td_pinned 55386be9f0dSKonstantin Belousov #define TD_PINNED_ASSERT \ 55486be9f0dSKonstantin Belousov KASSERT(curthread->td_pinned == old_td_pinned, \ 55586be9f0dSKonstantin Belousov ("pin count leak: %d %d %s:%d", curthread->td_pinned, \ 55686be9f0dSKonstantin Belousov old_td_pinned, __FILE__, __LINE__)) 55786be9f0dSKonstantin Belousov #else 55886be9f0dSKonstantin Belousov #define TD_PREP_PINNED_ASSERT 55986be9f0dSKonstantin Belousov #define TD_PINNED_ASSERT 56086be9f0dSKonstantin Belousov #endif 56186be9f0dSKonstantin Belousov 56286be9f0dSKonstantin Belousov #endif 563