11da12ec4SLe Tan /* 21da12ec4SLe Tan * QEMU emulation of an Intel IOMMU (VT-d) 31da12ec4SLe Tan * (DMA Remapping device) 41da12ec4SLe Tan * 51da12ec4SLe Tan * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com> 61da12ec4SLe Tan * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com> 71da12ec4SLe Tan * 81da12ec4SLe Tan * This program is free software; you can redistribute it and/or modify 91da12ec4SLe Tan * it under the terms of the GNU General Public License as published by 101da12ec4SLe Tan * the Free Software Foundation; either version 2 of the License, or 111da12ec4SLe Tan * (at your option) any later version. 121da12ec4SLe Tan 131da12ec4SLe Tan * This program is distributed in the hope that it will be useful, 141da12ec4SLe Tan * but WITHOUT ANY WARRANTY; without even the implied warranty of 151da12ec4SLe Tan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161da12ec4SLe Tan * GNU General Public License for more details. 171da12ec4SLe Tan 181da12ec4SLe Tan * You should have received a copy of the GNU General Public License along 191da12ec4SLe Tan * with this program; if not, see <http://www.gnu.org/licenses/>. 201da12ec4SLe Tan */ 211da12ec4SLe Tan 22b6a0aa05SPeter Maydell #include "qemu/osdep.h" 234684a204SPeter Xu #include "qemu/error-report.h" 246333e93cSRadim Krčmář #include "qapi/error.h" 251da12ec4SLe Tan #include "hw/sysbus.h" 261da12ec4SLe Tan #include "exec/address-spaces.h" 271da12ec4SLe Tan #include "intel_iommu_internal.h" 287df953bdSKnut Omang #include "hw/pci/pci.h" 293cb3b154SAlex Williamson #include "hw/pci/pci_bus.h" 30621d983aSMarcel Apfelbaum #include "hw/i386/pc.h" 31dea651a9SFeng Wu #include "hw/i386/apic-msidef.h" 3204af0e18SPeter Xu #include "hw/boards.h" 3304af0e18SPeter Xu #include "hw/i386/x86-iommu.h" 34cb135f59SPeter Xu #include "hw/pci-host/q35.h" 354684a204SPeter Xu #include "sysemu/kvm.h" 3632946019SRadim Krčmář #include "hw/i386/apic_internal.h" 37fb506e70SRadim Krčmář #include "kvm_i386.h" 38bc535e59SPeter Xu #include "trace.h" 391da12ec4SLe Tan 401da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val, 411da12ec4SLe Tan uint64_t wmask, uint64_t w1cmask) 421da12ec4SLe Tan { 431da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 441da12ec4SLe Tan stq_le_p(&s->wmask[addr], wmask); 451da12ec4SLe Tan stq_le_p(&s->w1cmask[addr], w1cmask); 461da12ec4SLe Tan } 471da12ec4SLe Tan 481da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask) 491da12ec4SLe Tan { 501da12ec4SLe Tan stq_le_p(&s->womask[addr], mask); 511da12ec4SLe Tan } 521da12ec4SLe Tan 531da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val, 541da12ec4SLe Tan uint32_t wmask, uint32_t w1cmask) 551da12ec4SLe Tan { 561da12ec4SLe Tan stl_le_p(&s->csr[addr], val); 571da12ec4SLe Tan stl_le_p(&s->wmask[addr], wmask); 581da12ec4SLe Tan stl_le_p(&s->w1cmask[addr], w1cmask); 591da12ec4SLe Tan } 601da12ec4SLe Tan 611da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask) 621da12ec4SLe Tan { 631da12ec4SLe Tan stl_le_p(&s->womask[addr], mask); 641da12ec4SLe Tan } 651da12ec4SLe Tan 661da12ec4SLe Tan /* "External" get/set operations */ 671da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val) 681da12ec4SLe Tan { 691da12ec4SLe Tan uint64_t oldval = ldq_le_p(&s->csr[addr]); 701da12ec4SLe Tan uint64_t wmask = ldq_le_p(&s->wmask[addr]); 711da12ec4SLe Tan uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]); 721da12ec4SLe Tan stq_le_p(&s->csr[addr], 731da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 741da12ec4SLe Tan } 751da12ec4SLe Tan 761da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val) 771da12ec4SLe Tan { 781da12ec4SLe Tan uint32_t oldval = ldl_le_p(&s->csr[addr]); 791da12ec4SLe Tan uint32_t wmask = ldl_le_p(&s->wmask[addr]); 801da12ec4SLe Tan uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]); 811da12ec4SLe Tan stl_le_p(&s->csr[addr], 821da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 831da12ec4SLe Tan } 841da12ec4SLe Tan 851da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr) 861da12ec4SLe Tan { 871da12ec4SLe Tan uint64_t val = ldq_le_p(&s->csr[addr]); 881da12ec4SLe Tan uint64_t womask = ldq_le_p(&s->womask[addr]); 891da12ec4SLe Tan return val & ~womask; 901da12ec4SLe Tan } 911da12ec4SLe Tan 921da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr) 931da12ec4SLe Tan { 941da12ec4SLe Tan uint32_t val = ldl_le_p(&s->csr[addr]); 951da12ec4SLe Tan uint32_t womask = ldl_le_p(&s->womask[addr]); 961da12ec4SLe Tan return val & ~womask; 971da12ec4SLe Tan } 981da12ec4SLe Tan 991da12ec4SLe Tan /* "Internal" get/set operations */ 1001da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr) 1011da12ec4SLe Tan { 1021da12ec4SLe Tan return ldq_le_p(&s->csr[addr]); 1031da12ec4SLe Tan } 1041da12ec4SLe Tan 1051da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr) 1061da12ec4SLe Tan { 1071da12ec4SLe Tan return ldl_le_p(&s->csr[addr]); 1081da12ec4SLe Tan } 1091da12ec4SLe Tan 1101da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val) 1111da12ec4SLe Tan { 1121da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 1131da12ec4SLe Tan } 1141da12ec4SLe Tan 1151da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr, 1161da12ec4SLe Tan uint32_t clear, uint32_t mask) 1171da12ec4SLe Tan { 1181da12ec4SLe Tan uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask; 1191da12ec4SLe Tan stl_le_p(&s->csr[addr], new_val); 1201da12ec4SLe Tan return new_val; 1211da12ec4SLe Tan } 1221da12ec4SLe Tan 1231da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, 1241da12ec4SLe Tan uint64_t clear, uint64_t mask) 1251da12ec4SLe Tan { 1261da12ec4SLe Tan uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask; 1271da12ec4SLe Tan stq_le_p(&s->csr[addr], new_val); 1281da12ec4SLe Tan return new_val; 1291da12ec4SLe Tan } 1301da12ec4SLe Tan 131b5a280c0SLe Tan /* GHashTable functions */ 132b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) 133b5a280c0SLe Tan { 134b5a280c0SLe Tan return *((const uint64_t *)v1) == *((const uint64_t *)v2); 135b5a280c0SLe Tan } 136b5a280c0SLe Tan 137b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v) 138b5a280c0SLe Tan { 139b5a280c0SLe Tan return (guint)*(const uint64_t *)v; 140b5a280c0SLe Tan } 141b5a280c0SLe Tan 142b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value, 143b5a280c0SLe Tan gpointer user_data) 144b5a280c0SLe Tan { 145b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 146b5a280c0SLe Tan uint16_t domain_id = *(uint16_t *)user_data; 147b5a280c0SLe Tan return entry->domain_id == domain_id; 148b5a280c0SLe Tan } 149b5a280c0SLe Tan 150d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */ 151d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level) 152d66b969bSJason Wang { 1537e58326aSPeter Xu assert(level != 0); 154d66b969bSJason Wang return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS; 155d66b969bSJason Wang } 156d66b969bSJason Wang 157d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level) 158d66b969bSJason Wang { 159d66b969bSJason Wang return ~((1ULL << vtd_slpt_level_shift(level)) - 1); 160d66b969bSJason Wang } 161d66b969bSJason Wang 162b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value, 163b5a280c0SLe Tan gpointer user_data) 164b5a280c0SLe Tan { 165b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 166b5a280c0SLe Tan VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data; 167d66b969bSJason Wang uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask; 168d66b969bSJason Wang uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K; 169b5a280c0SLe Tan return (entry->domain_id == info->domain_id) && 170d66b969bSJason Wang (((entry->gfn & info->mask) == gfn) || 171d66b969bSJason Wang (entry->gfn == gfn_tlb)); 172b5a280c0SLe Tan } 173b5a280c0SLe Tan 174d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of 175d92fa2dcSLe Tan * IntelIOMMUState to 1. 176d92fa2dcSLe Tan */ 177d92fa2dcSLe Tan static void vtd_reset_context_cache(IntelIOMMUState *s) 178d92fa2dcSLe Tan { 179d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 1807df953bdSKnut Omang VTDBus *vtd_bus; 1817df953bdSKnut Omang GHashTableIter bus_it; 182d92fa2dcSLe Tan uint32_t devfn_it; 183d92fa2dcSLe Tan 1847feb51b7SPeter Xu trace_vtd_context_cache_reset(); 1857feb51b7SPeter Xu 1867df953bdSKnut Omang g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr); 1877df953bdSKnut Omang 1887df953bdSKnut Omang while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) { 189bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 1907df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 191d92fa2dcSLe Tan if (!vtd_as) { 192d92fa2dcSLe Tan continue; 193d92fa2dcSLe Tan } 194d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 195d92fa2dcSLe Tan } 196d92fa2dcSLe Tan } 197d92fa2dcSLe Tan s->context_cache_gen = 1; 198d92fa2dcSLe Tan } 199d92fa2dcSLe Tan 200b5a280c0SLe Tan static void vtd_reset_iotlb(IntelIOMMUState *s) 201b5a280c0SLe Tan { 202b5a280c0SLe Tan assert(s->iotlb); 203b5a280c0SLe Tan g_hash_table_remove_all(s->iotlb); 204b5a280c0SLe Tan } 205b5a280c0SLe Tan 206bacabb0aSJason Wang static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id, 207d66b969bSJason Wang uint32_t level) 208d66b969bSJason Wang { 209d66b969bSJason Wang return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) | 210d66b969bSJason Wang ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT); 211d66b969bSJason Wang } 212d66b969bSJason Wang 213d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level) 214d66b969bSJason Wang { 215d66b969bSJason Wang return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K; 216d66b969bSJason Wang } 217d66b969bSJason Wang 218b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id, 219b5a280c0SLe Tan hwaddr addr) 220b5a280c0SLe Tan { 221d66b969bSJason Wang VTDIOTLBEntry *entry; 222b5a280c0SLe Tan uint64_t key; 223d66b969bSJason Wang int level; 224b5a280c0SLe Tan 225d66b969bSJason Wang for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) { 226d66b969bSJason Wang key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level), 227d66b969bSJason Wang source_id, level); 228d66b969bSJason Wang entry = g_hash_table_lookup(s->iotlb, &key); 229d66b969bSJason Wang if (entry) { 230d66b969bSJason Wang goto out; 231d66b969bSJason Wang } 232d66b969bSJason Wang } 233b5a280c0SLe Tan 234d66b969bSJason Wang out: 235d66b969bSJason Wang return entry; 236b5a280c0SLe Tan } 237b5a280c0SLe Tan 238b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, 239b5a280c0SLe Tan uint16_t domain_id, hwaddr addr, uint64_t slpte, 24007f7b733SPeter Xu uint8_t access_flags, uint32_t level) 241b5a280c0SLe Tan { 242b5a280c0SLe Tan VTDIOTLBEntry *entry = g_malloc(sizeof(*entry)); 243b5a280c0SLe Tan uint64_t *key = g_malloc(sizeof(*key)); 244d66b969bSJason Wang uint64_t gfn = vtd_get_iotlb_gfn(addr, level); 245b5a280c0SLe Tan 2466c441e1dSPeter Xu trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); 247b5a280c0SLe Tan if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) { 2486c441e1dSPeter Xu trace_vtd_iotlb_reset("iotlb exceeds size limit"); 249b5a280c0SLe Tan vtd_reset_iotlb(s); 250b5a280c0SLe Tan } 251b5a280c0SLe Tan 252b5a280c0SLe Tan entry->gfn = gfn; 253b5a280c0SLe Tan entry->domain_id = domain_id; 254b5a280c0SLe Tan entry->slpte = slpte; 25507f7b733SPeter Xu entry->access_flags = access_flags; 256d66b969bSJason Wang entry->mask = vtd_slpt_level_page_mask(level); 257d66b969bSJason Wang *key = vtd_get_iotlb_key(gfn, source_id, level); 258b5a280c0SLe Tan g_hash_table_replace(s->iotlb, key, entry); 259b5a280c0SLe Tan } 260b5a280c0SLe Tan 2611da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an 2621da12ec4SLe Tan * interrupt via MSI. 2631da12ec4SLe Tan */ 2641da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg, 2651da12ec4SLe Tan hwaddr mesg_data_reg) 2661da12ec4SLe Tan { 26732946019SRadim Krčmář MSIMessage msi; 2681da12ec4SLe Tan 2691da12ec4SLe Tan assert(mesg_data_reg < DMAR_REG_SIZE); 2701da12ec4SLe Tan assert(mesg_addr_reg < DMAR_REG_SIZE); 2711da12ec4SLe Tan 27232946019SRadim Krčmář msi.address = vtd_get_long_raw(s, mesg_addr_reg); 27332946019SRadim Krčmář msi.data = vtd_get_long_raw(s, mesg_data_reg); 2741da12ec4SLe Tan 2757feb51b7SPeter Xu trace_vtd_irq_generate(msi.address, msi.data); 2767feb51b7SPeter Xu 27732946019SRadim Krčmář apic_get_class()->send_msi(&msi); 2781da12ec4SLe Tan } 2791da12ec4SLe Tan 2801da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met. 2811da12ec4SLe Tan * Notice that the value of FSTS_REG being passed to it should be the one 2821da12ec4SLe Tan * before any update. 2831da12ec4SLe Tan */ 2841da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts) 2851da12ec4SLe Tan { 2861da12ec4SLe Tan if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO || 2871da12ec4SLe Tan pre_fsts & VTD_FSTS_IQE) { 2887feb51b7SPeter Xu trace_vtd_err("There are previous interrupt conditions " 2897feb51b7SPeter Xu "to be serviced by software, fault event " 2907feb51b7SPeter Xu "is not generated."); 2911da12ec4SLe Tan return; 2921da12ec4SLe Tan } 2931da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP); 2941da12ec4SLe Tan if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) { 2957feb51b7SPeter Xu trace_vtd_err("Interrupt Mask set, irq is not generated."); 2961da12ec4SLe Tan } else { 2971da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 2981da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 2991da12ec4SLe Tan } 3001da12ec4SLe Tan } 3011da12ec4SLe Tan 3021da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by 3031da12ec4SLe Tan * @index is Set. 3041da12ec4SLe Tan */ 3051da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index) 3061da12ec4SLe Tan { 3071da12ec4SLe Tan /* Each reg is 128-bit */ 3081da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 3091da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 3101da12ec4SLe Tan 3111da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 3121da12ec4SLe Tan 3131da12ec4SLe Tan return vtd_get_quad_raw(s, addr) & VTD_FRCD_F; 3141da12ec4SLe Tan } 3151da12ec4SLe Tan 3161da12ec4SLe Tan /* Update the PPF field of Fault Status Register. 3171da12ec4SLe Tan * Should be called whenever change the F field of any fault recording 3181da12ec4SLe Tan * registers. 3191da12ec4SLe Tan */ 3201da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s) 3211da12ec4SLe Tan { 3221da12ec4SLe Tan uint32_t i; 3231da12ec4SLe Tan uint32_t ppf_mask = 0; 3241da12ec4SLe Tan 3251da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 3261da12ec4SLe Tan if (vtd_is_frcd_set(s, i)) { 3271da12ec4SLe Tan ppf_mask = VTD_FSTS_PPF; 3281da12ec4SLe Tan break; 3291da12ec4SLe Tan } 3301da12ec4SLe Tan } 3311da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask); 3327feb51b7SPeter Xu trace_vtd_fsts_ppf(!!ppf_mask); 3331da12ec4SLe Tan } 3341da12ec4SLe Tan 3351da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index) 3361da12ec4SLe Tan { 3371da12ec4SLe Tan /* Each reg is 128-bit */ 3381da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 3391da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 3401da12ec4SLe Tan 3411da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 3421da12ec4SLe Tan 3431da12ec4SLe Tan vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F); 3441da12ec4SLe Tan vtd_update_fsts_ppf(s); 3451da12ec4SLe Tan } 3461da12ec4SLe Tan 3471da12ec4SLe Tan /* Must not update F field now, should be done later */ 3481da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index, 3491da12ec4SLe Tan uint16_t source_id, hwaddr addr, 3501da12ec4SLe Tan VTDFaultReason fault, bool is_write) 3511da12ec4SLe Tan { 3521da12ec4SLe Tan uint64_t hi = 0, lo; 3531da12ec4SLe Tan hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 3541da12ec4SLe Tan 3551da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 3561da12ec4SLe Tan 3571da12ec4SLe Tan lo = VTD_FRCD_FI(addr); 3581da12ec4SLe Tan hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault); 3591da12ec4SLe Tan if (!is_write) { 3601da12ec4SLe Tan hi |= VTD_FRCD_T; 3611da12ec4SLe Tan } 3621da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr, lo); 3631da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr + 8, hi); 3647feb51b7SPeter Xu 3657feb51b7SPeter Xu trace_vtd_frr_new(index, hi, lo); 3661da12ec4SLe Tan } 3671da12ec4SLe Tan 3681da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */ 3691da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id) 3701da12ec4SLe Tan { 3711da12ec4SLe Tan uint32_t i; 3721da12ec4SLe Tan uint64_t frcd_reg; 3731da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */ 3741da12ec4SLe Tan 3751da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 3761da12ec4SLe Tan frcd_reg = vtd_get_quad_raw(s, addr); 3771da12ec4SLe Tan if ((frcd_reg & VTD_FRCD_F) && 3781da12ec4SLe Tan ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) { 3791da12ec4SLe Tan return true; 3801da12ec4SLe Tan } 3811da12ec4SLe Tan addr += 16; /* 128-bit for each */ 3821da12ec4SLe Tan } 3831da12ec4SLe Tan return false; 3841da12ec4SLe Tan } 3851da12ec4SLe Tan 3861da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */ 3871da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id, 3881da12ec4SLe Tan hwaddr addr, VTDFaultReason fault, 3891da12ec4SLe Tan bool is_write) 3901da12ec4SLe Tan { 3911da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 3921da12ec4SLe Tan 3931da12ec4SLe Tan assert(fault < VTD_FR_MAX); 3941da12ec4SLe Tan 3951da12ec4SLe Tan if (fault == VTD_FR_RESERVED_ERR) { 3961da12ec4SLe Tan /* This is not a normal fault reason case. Drop it. */ 3971da12ec4SLe Tan return; 3981da12ec4SLe Tan } 3997feb51b7SPeter Xu 4007feb51b7SPeter Xu trace_vtd_dmar_fault(source_id, fault, addr, is_write); 4017feb51b7SPeter Xu 4021da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PFO) { 4037feb51b7SPeter Xu trace_vtd_err("New fault is not recorded due to " 4047feb51b7SPeter Xu "Primary Fault Overflow."); 4051da12ec4SLe Tan return; 4061da12ec4SLe Tan } 4077feb51b7SPeter Xu 4081da12ec4SLe Tan if (vtd_try_collapse_fault(s, source_id)) { 4097feb51b7SPeter Xu trace_vtd_err("New fault is not recorded due to " 4107feb51b7SPeter Xu "compression of faults."); 4111da12ec4SLe Tan return; 4121da12ec4SLe Tan } 4137feb51b7SPeter Xu 4141da12ec4SLe Tan if (vtd_is_frcd_set(s, s->next_frcd_reg)) { 4157feb51b7SPeter Xu trace_vtd_err("Next Fault Recording Reg is used, " 4167feb51b7SPeter Xu "new fault is not recorded, set PFO field."); 4171da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO); 4181da12ec4SLe Tan return; 4191da12ec4SLe Tan } 4201da12ec4SLe Tan 4211da12ec4SLe Tan vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write); 4221da12ec4SLe Tan 4231da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PPF) { 4247feb51b7SPeter Xu trace_vtd_err("There are pending faults already, " 4257feb51b7SPeter Xu "fault event is not generated."); 4261da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); 4271da12ec4SLe Tan s->next_frcd_reg++; 4281da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 4291da12ec4SLe Tan s->next_frcd_reg = 0; 4301da12ec4SLe Tan } 4311da12ec4SLe Tan } else { 4321da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK, 4331da12ec4SLe Tan VTD_FSTS_FRI(s->next_frcd_reg)); 4341da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */ 4351da12ec4SLe Tan s->next_frcd_reg++; 4361da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 4371da12ec4SLe Tan s->next_frcd_reg = 0; 4381da12ec4SLe Tan } 4391da12ec4SLe Tan /* This case actually cause the PPF to be Set. 4401da12ec4SLe Tan * So generate fault event (interrupt). 4411da12ec4SLe Tan */ 4421da12ec4SLe Tan vtd_generate_fault_event(s, fsts_reg); 4431da12ec4SLe Tan } 4441da12ec4SLe Tan } 4451da12ec4SLe Tan 446ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error 447ed7b8fbcSLe Tan * conditions. 448ed7b8fbcSLe Tan */ 449ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s) 450ed7b8fbcSLe Tan { 451ed7b8fbcSLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 452ed7b8fbcSLe Tan 453ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE); 454ed7b8fbcSLe Tan vtd_generate_fault_event(s, fsts_reg); 455ed7b8fbcSLe Tan } 456ed7b8fbcSLe Tan 457ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */ 458ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s) 459ed7b8fbcSLe Tan { 460ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) { 461bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("One pending, skip current"); 462ed7b8fbcSLe Tan return; 463ed7b8fbcSLe Tan } 464ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC); 465ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP); 466ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) { 467bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, " 468bc535e59SPeter Xu "new event not generated"); 469ed7b8fbcSLe Tan return; 470ed7b8fbcSLe Tan } else { 471ed7b8fbcSLe Tan /* Generate the interrupt event */ 472bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("Generating complete event"); 473ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 474ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 475ed7b8fbcSLe Tan } 476ed7b8fbcSLe Tan } 477ed7b8fbcSLe Tan 4781da12ec4SLe Tan static inline bool vtd_root_entry_present(VTDRootEntry *root) 4791da12ec4SLe Tan { 4801da12ec4SLe Tan return root->val & VTD_ROOT_ENTRY_P; 4811da12ec4SLe Tan } 4821da12ec4SLe Tan 4831da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index, 4841da12ec4SLe Tan VTDRootEntry *re) 4851da12ec4SLe Tan { 4861da12ec4SLe Tan dma_addr_t addr; 4871da12ec4SLe Tan 4881da12ec4SLe Tan addr = s->root + index * sizeof(*re); 4891da12ec4SLe Tan if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) { 4906c441e1dSPeter Xu trace_vtd_re_invalid(re->rsvd, re->val); 4911da12ec4SLe Tan re->val = 0; 4921da12ec4SLe Tan return -VTD_FR_ROOT_TABLE_INV; 4931da12ec4SLe Tan } 4941da12ec4SLe Tan re->val = le64_to_cpu(re->val); 4951da12ec4SLe Tan return 0; 4961da12ec4SLe Tan } 4971da12ec4SLe Tan 4988f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context) 4991da12ec4SLe Tan { 5001da12ec4SLe Tan return context->lo & VTD_CONTEXT_ENTRY_P; 5011da12ec4SLe Tan } 5021da12ec4SLe Tan 5031da12ec4SLe Tan static int vtd_get_context_entry_from_root(VTDRootEntry *root, uint8_t index, 5041da12ec4SLe Tan VTDContextEntry *ce) 5051da12ec4SLe Tan { 5061da12ec4SLe Tan dma_addr_t addr; 5071da12ec4SLe Tan 5086c441e1dSPeter Xu /* we have checked that root entry is present */ 5091da12ec4SLe Tan addr = (root->val & VTD_ROOT_ENTRY_CTP) + index * sizeof(*ce); 5101da12ec4SLe Tan if (dma_memory_read(&address_space_memory, addr, ce, sizeof(*ce))) { 5116c441e1dSPeter Xu trace_vtd_re_invalid(root->rsvd, root->val); 5121da12ec4SLe Tan return -VTD_FR_CONTEXT_TABLE_INV; 5131da12ec4SLe Tan } 5141da12ec4SLe Tan ce->lo = le64_to_cpu(ce->lo); 5151da12ec4SLe Tan ce->hi = le64_to_cpu(ce->hi); 5161da12ec4SLe Tan return 0; 5171da12ec4SLe Tan } 5181da12ec4SLe Tan 5198f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce) 5201da12ec4SLe Tan { 5211da12ec4SLe Tan return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR; 5221da12ec4SLe Tan } 5231da12ec4SLe Tan 52437f51384SPrasad Singamsetty static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw) 5251da12ec4SLe Tan { 52637f51384SPrasad Singamsetty return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw); 5271da12ec4SLe Tan } 5281da12ec4SLe Tan 5291da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */ 5301da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level) 5311da12ec4SLe Tan { 5321da12ec4SLe Tan return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK); 5331da12ec4SLe Tan } 5341da12ec4SLe Tan 5351da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */ 5361da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index) 5371da12ec4SLe Tan { 5381da12ec4SLe Tan uint64_t slpte; 5391da12ec4SLe Tan 5401da12ec4SLe Tan assert(index < VTD_SL_PT_ENTRY_NR); 5411da12ec4SLe Tan 5421da12ec4SLe Tan if (dma_memory_read(&address_space_memory, 5431da12ec4SLe Tan base_addr + index * sizeof(slpte), &slpte, 5441da12ec4SLe Tan sizeof(slpte))) { 5451da12ec4SLe Tan slpte = (uint64_t)-1; 5461da12ec4SLe Tan return slpte; 5471da12ec4SLe Tan } 5481da12ec4SLe Tan slpte = le64_to_cpu(slpte); 5491da12ec4SLe Tan return slpte; 5501da12ec4SLe Tan } 5511da12ec4SLe Tan 5526e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset 5536e905564SPeter Xu * of current level. 5541da12ec4SLe Tan */ 5556e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level) 5561da12ec4SLe Tan { 5576e905564SPeter Xu return (iova >> vtd_slpt_level_shift(level)) & 5581da12ec4SLe Tan ((1ULL << VTD_SL_LEVEL_BITS) - 1); 5591da12ec4SLe Tan } 5601da12ec4SLe Tan 5611da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */ 5621da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level) 5631da12ec4SLe Tan { 5641da12ec4SLe Tan return VTD_CAP_SAGAW_MASK & s->cap & 5651da12ec4SLe Tan (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT)); 5661da12ec4SLe Tan } 5671da12ec4SLe Tan 5681da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level 5691da12ec4SLe Tan * page-table walk from the Address Width field of context-entry. 5701da12ec4SLe Tan */ 5718f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce) 5721da12ec4SLe Tan { 5731da12ec4SLe Tan return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW); 5741da12ec4SLe Tan } 5751da12ec4SLe Tan 5768f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce) 5771da12ec4SLe Tan { 5781da12ec4SLe Tan return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9; 5791da12ec4SLe Tan } 5801da12ec4SLe Tan 581127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce) 582127ff5c3SPeter Xu { 583127ff5c3SPeter Xu return ce->lo & VTD_CONTEXT_ENTRY_TT; 584127ff5c3SPeter Xu } 585127ff5c3SPeter Xu 586f80c9874SPeter Xu /* Return true if check passed, otherwise false */ 587f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu, 588f80c9874SPeter Xu VTDContextEntry *ce) 589f80c9874SPeter Xu { 590f80c9874SPeter Xu switch (vtd_ce_get_type(ce)) { 591f80c9874SPeter Xu case VTD_CONTEXT_TT_MULTI_LEVEL: 592f80c9874SPeter Xu /* Always supported */ 593f80c9874SPeter Xu break; 594f80c9874SPeter Xu case VTD_CONTEXT_TT_DEV_IOTLB: 595f80c9874SPeter Xu if (!x86_iommu->dt_supported) { 596f80c9874SPeter Xu return false; 597f80c9874SPeter Xu } 598f80c9874SPeter Xu break; 599dbaabb25SPeter Xu case VTD_CONTEXT_TT_PASS_THROUGH: 600dbaabb25SPeter Xu if (!x86_iommu->pt_supported) { 601dbaabb25SPeter Xu return false; 602dbaabb25SPeter Xu } 603dbaabb25SPeter Xu break; 604f80c9874SPeter Xu default: 605f80c9874SPeter Xu /* Unknwon type */ 606f80c9874SPeter Xu return false; 607f80c9874SPeter Xu } 608f80c9874SPeter Xu return true; 609f80c9874SPeter Xu } 610f80c9874SPeter Xu 61137f51384SPrasad Singamsetty static inline uint64_t vtd_iova_limit(VTDContextEntry *ce, uint8_t aw) 612f06a696dSPeter Xu { 6138f7d7161SPeter Xu uint32_t ce_agaw = vtd_ce_get_agaw(ce); 61437f51384SPrasad Singamsetty return 1ULL << MIN(ce_agaw, aw); 615f06a696dSPeter Xu } 616f06a696dSPeter Xu 617f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */ 61837f51384SPrasad Singamsetty static inline bool vtd_iova_range_check(uint64_t iova, VTDContextEntry *ce, 61937f51384SPrasad Singamsetty uint8_t aw) 620f06a696dSPeter Xu { 621f06a696dSPeter Xu /* 622f06a696dSPeter Xu * Check if @iova is above 2^X-1, where X is the minimum of MGAW 623f06a696dSPeter Xu * in CAP_REG and AW in context-entry. 624f06a696dSPeter Xu */ 62537f51384SPrasad Singamsetty return !(iova & ~(vtd_iova_limit(ce, aw) - 1)); 626f06a696dSPeter Xu } 627f06a696dSPeter Xu 62892e5d85eSPrasad Singamsetty /* 62992e5d85eSPrasad Singamsetty * Rsvd field masks for spte: 63092e5d85eSPrasad Singamsetty * Index [1] to [4] 4k pages 63192e5d85eSPrasad Singamsetty * Index [5] to [8] large pages 63292e5d85eSPrasad Singamsetty */ 63392e5d85eSPrasad Singamsetty static uint64_t vtd_paging_entry_rsvd_field[9]; 6341da12ec4SLe Tan 6351da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level) 6361da12ec4SLe Tan { 6371da12ec4SLe Tan if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) { 6381da12ec4SLe Tan /* Maybe large page */ 6391da12ec4SLe Tan return slpte & vtd_paging_entry_rsvd_field[level + 4]; 6401da12ec4SLe Tan } else { 6411da12ec4SLe Tan return slpte & vtd_paging_entry_rsvd_field[level]; 6421da12ec4SLe Tan } 6431da12ec4SLe Tan } 6441da12ec4SLe Tan 645dbaabb25SPeter Xu /* Find the VTD address space associated with a given bus number */ 646dbaabb25SPeter Xu static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) 647dbaabb25SPeter Xu { 648dbaabb25SPeter Xu VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; 649dbaabb25SPeter Xu if (!vtd_bus) { 650dbaabb25SPeter Xu /* 651dbaabb25SPeter Xu * Iterate over the registered buses to find the one which 652dbaabb25SPeter Xu * currently hold this bus number, and update the bus_num 653dbaabb25SPeter Xu * lookup table: 654dbaabb25SPeter Xu */ 655dbaabb25SPeter Xu GHashTableIter iter; 656dbaabb25SPeter Xu 657dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 658dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 659dbaabb25SPeter Xu if (pci_bus_num(vtd_bus->bus) == bus_num) { 660dbaabb25SPeter Xu s->vtd_as_by_bus_num[bus_num] = vtd_bus; 661dbaabb25SPeter Xu return vtd_bus; 662dbaabb25SPeter Xu } 663dbaabb25SPeter Xu } 664dbaabb25SPeter Xu } 665dbaabb25SPeter Xu return vtd_bus; 666dbaabb25SPeter Xu } 667dbaabb25SPeter Xu 6686e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level 6691da12ec4SLe Tan * of the translation, can be used for deciding the size of large page. 6701da12ec4SLe Tan */ 6716e905564SPeter Xu static int vtd_iova_to_slpte(VTDContextEntry *ce, uint64_t iova, bool is_write, 6721da12ec4SLe Tan uint64_t *slptep, uint32_t *slpte_level, 67337f51384SPrasad Singamsetty bool *reads, bool *writes, uint8_t aw_bits) 6741da12ec4SLe Tan { 6758f7d7161SPeter Xu dma_addr_t addr = vtd_ce_get_slpt_base(ce); 6768f7d7161SPeter Xu uint32_t level = vtd_ce_get_level(ce); 6771da12ec4SLe Tan uint32_t offset; 6781da12ec4SLe Tan uint64_t slpte; 6791da12ec4SLe Tan uint64_t access_right_check; 6801da12ec4SLe Tan 68137f51384SPrasad Singamsetty if (!vtd_iova_range_check(iova, ce, aw_bits)) { 6827feb51b7SPeter Xu trace_vtd_err_dmar_iova_overflow(iova); 6831da12ec4SLe Tan return -VTD_FR_ADDR_BEYOND_MGAW; 6841da12ec4SLe Tan } 6851da12ec4SLe Tan 6861da12ec4SLe Tan /* FIXME: what is the Atomics request here? */ 6871da12ec4SLe Tan access_right_check = is_write ? VTD_SL_W : VTD_SL_R; 6881da12ec4SLe Tan 6891da12ec4SLe Tan while (true) { 6906e905564SPeter Xu offset = vtd_iova_level_offset(iova, level); 6911da12ec4SLe Tan slpte = vtd_get_slpte(addr, offset); 6921da12ec4SLe Tan 6931da12ec4SLe Tan if (slpte == (uint64_t)-1) { 6947feb51b7SPeter Xu trace_vtd_err_dmar_slpte_read_error(iova, level); 6958f7d7161SPeter Xu if (level == vtd_ce_get_level(ce)) { 6961da12ec4SLe Tan /* Invalid programming of context-entry */ 6971da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 6981da12ec4SLe Tan } else { 6991da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_INV; 7001da12ec4SLe Tan } 7011da12ec4SLe Tan } 7021da12ec4SLe Tan *reads = (*reads) && (slpte & VTD_SL_R); 7031da12ec4SLe Tan *writes = (*writes) && (slpte & VTD_SL_W); 7041da12ec4SLe Tan if (!(slpte & access_right_check)) { 7057feb51b7SPeter Xu trace_vtd_err_dmar_slpte_perm_error(iova, level, slpte, is_write); 7061da12ec4SLe Tan return is_write ? -VTD_FR_WRITE : -VTD_FR_READ; 7071da12ec4SLe Tan } 7081da12ec4SLe Tan if (vtd_slpte_nonzero_rsvd(slpte, level)) { 7097feb51b7SPeter Xu trace_vtd_err_dmar_slpte_resv_error(iova, level, slpte); 7101da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_RSVD; 7111da12ec4SLe Tan } 7121da12ec4SLe Tan 7131da12ec4SLe Tan if (vtd_is_last_slpte(slpte, level)) { 7141da12ec4SLe Tan *slptep = slpte; 7151da12ec4SLe Tan *slpte_level = level; 7161da12ec4SLe Tan return 0; 7171da12ec4SLe Tan } 71837f51384SPrasad Singamsetty addr = vtd_get_slpte_addr(slpte, aw_bits); 7191da12ec4SLe Tan level--; 7201da12ec4SLe Tan } 7211da12ec4SLe Tan } 7221da12ec4SLe Tan 723f06a696dSPeter Xu typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private); 724f06a696dSPeter Xu 725f06a696dSPeter Xu /** 726f06a696dSPeter Xu * vtd_page_walk_level - walk over specific level for IOVA range 727f06a696dSPeter Xu * 728f06a696dSPeter Xu * @addr: base GPA addr to start the walk 729f06a696dSPeter Xu * @start: IOVA range start address 730f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 731f06a696dSPeter Xu * @hook_fn: hook func to be called when detected page 732f06a696dSPeter Xu * @private: private data to be passed into hook func 733f06a696dSPeter Xu * @read: whether parent level has read permission 734f06a696dSPeter Xu * @write: whether parent level has write permission 735f06a696dSPeter Xu * @notify_unmap: whether we should notify invalid entries 73637f51384SPrasad Singamsetty * @aw: maximum address width 737f06a696dSPeter Xu */ 738f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start, 739f06a696dSPeter Xu uint64_t end, vtd_page_walk_hook hook_fn, 74037f51384SPrasad Singamsetty void *private, uint32_t level, bool read, 74137f51384SPrasad Singamsetty bool write, bool notify_unmap, uint8_t aw) 742f06a696dSPeter Xu { 743f06a696dSPeter Xu bool read_cur, write_cur, entry_valid; 744f06a696dSPeter Xu uint32_t offset; 745f06a696dSPeter Xu uint64_t slpte; 746f06a696dSPeter Xu uint64_t subpage_size, subpage_mask; 747f06a696dSPeter Xu IOMMUTLBEntry entry; 748f06a696dSPeter Xu uint64_t iova = start; 749f06a696dSPeter Xu uint64_t iova_next; 750f06a696dSPeter Xu int ret = 0; 751f06a696dSPeter Xu 752f06a696dSPeter Xu trace_vtd_page_walk_level(addr, level, start, end); 753f06a696dSPeter Xu 754f06a696dSPeter Xu subpage_size = 1ULL << vtd_slpt_level_shift(level); 755f06a696dSPeter Xu subpage_mask = vtd_slpt_level_page_mask(level); 756f06a696dSPeter Xu 757f06a696dSPeter Xu while (iova < end) { 758f06a696dSPeter Xu iova_next = (iova & subpage_mask) + subpage_size; 759f06a696dSPeter Xu 760f06a696dSPeter Xu offset = vtd_iova_level_offset(iova, level); 761f06a696dSPeter Xu slpte = vtd_get_slpte(addr, offset); 762f06a696dSPeter Xu 763f06a696dSPeter Xu if (slpte == (uint64_t)-1) { 764f06a696dSPeter Xu trace_vtd_page_walk_skip_read(iova, iova_next); 765f06a696dSPeter Xu goto next; 766f06a696dSPeter Xu } 767f06a696dSPeter Xu 768f06a696dSPeter Xu if (vtd_slpte_nonzero_rsvd(slpte, level)) { 769f06a696dSPeter Xu trace_vtd_page_walk_skip_reserve(iova, iova_next); 770f06a696dSPeter Xu goto next; 771f06a696dSPeter Xu } 772f06a696dSPeter Xu 773f06a696dSPeter Xu /* Permissions are stacked with parents' */ 774f06a696dSPeter Xu read_cur = read && (slpte & VTD_SL_R); 775f06a696dSPeter Xu write_cur = write && (slpte & VTD_SL_W); 776f06a696dSPeter Xu 777f06a696dSPeter Xu /* 778f06a696dSPeter Xu * As long as we have either read/write permission, this is a 779f06a696dSPeter Xu * valid entry. The rule works for both page entries and page 780f06a696dSPeter Xu * table entries. 781f06a696dSPeter Xu */ 782f06a696dSPeter Xu entry_valid = read_cur | write_cur; 783f06a696dSPeter Xu 784f06a696dSPeter Xu if (vtd_is_last_slpte(slpte, level)) { 785f06a696dSPeter Xu entry.target_as = &address_space_memory; 786f06a696dSPeter Xu entry.iova = iova & subpage_mask; 787f06a696dSPeter Xu /* NOTE: this is only meaningful if entry_valid == true */ 78837f51384SPrasad Singamsetty entry.translated_addr = vtd_get_slpte_addr(slpte, aw); 789f06a696dSPeter Xu entry.addr_mask = ~subpage_mask; 790f06a696dSPeter Xu entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur); 791f06a696dSPeter Xu if (!entry_valid && !notify_unmap) { 792f06a696dSPeter Xu trace_vtd_page_walk_skip_perm(iova, iova_next); 793f06a696dSPeter Xu goto next; 794f06a696dSPeter Xu } 795f06a696dSPeter Xu trace_vtd_page_walk_one(level, entry.iova, entry.translated_addr, 796f06a696dSPeter Xu entry.addr_mask, entry.perm); 797f06a696dSPeter Xu if (hook_fn) { 798f06a696dSPeter Xu ret = hook_fn(&entry, private); 799f06a696dSPeter Xu if (ret < 0) { 800f06a696dSPeter Xu return ret; 801f06a696dSPeter Xu } 802f06a696dSPeter Xu } 803f06a696dSPeter Xu } else { 804f06a696dSPeter Xu if (!entry_valid) { 805f06a696dSPeter Xu trace_vtd_page_walk_skip_perm(iova, iova_next); 806f06a696dSPeter Xu goto next; 807f06a696dSPeter Xu } 80837f51384SPrasad Singamsetty ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, aw), iova, 809f06a696dSPeter Xu MIN(iova_next, end), hook_fn, private, 810f06a696dSPeter Xu level - 1, read_cur, write_cur, 81137f51384SPrasad Singamsetty notify_unmap, aw); 812f06a696dSPeter Xu if (ret < 0) { 813f06a696dSPeter Xu return ret; 814f06a696dSPeter Xu } 815f06a696dSPeter Xu } 816f06a696dSPeter Xu 817f06a696dSPeter Xu next: 818f06a696dSPeter Xu iova = iova_next; 819f06a696dSPeter Xu } 820f06a696dSPeter Xu 821f06a696dSPeter Xu return 0; 822f06a696dSPeter Xu } 823f06a696dSPeter Xu 824f06a696dSPeter Xu /** 825f06a696dSPeter Xu * vtd_page_walk - walk specific IOVA range, and call the hook 826f06a696dSPeter Xu * 827f06a696dSPeter Xu * @ce: context entry to walk upon 828f06a696dSPeter Xu * @start: IOVA address to start the walk 829f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 830f06a696dSPeter Xu * @hook_fn: the hook that to be called for each detected area 831f06a696dSPeter Xu * @private: private data for the hook function 83237f51384SPrasad Singamsetty * @aw: maximum address width 833f06a696dSPeter Xu */ 834f06a696dSPeter Xu static int vtd_page_walk(VTDContextEntry *ce, uint64_t start, uint64_t end, 835dd4d607eSPeter Xu vtd_page_walk_hook hook_fn, void *private, 83637f51384SPrasad Singamsetty bool notify_unmap, uint8_t aw) 837f06a696dSPeter Xu { 8388f7d7161SPeter Xu dma_addr_t addr = vtd_ce_get_slpt_base(ce); 8398f7d7161SPeter Xu uint32_t level = vtd_ce_get_level(ce); 840f06a696dSPeter Xu 84137f51384SPrasad Singamsetty if (!vtd_iova_range_check(start, ce, aw)) { 842f06a696dSPeter Xu return -VTD_FR_ADDR_BEYOND_MGAW; 843f06a696dSPeter Xu } 844f06a696dSPeter Xu 84537f51384SPrasad Singamsetty if (!vtd_iova_range_check(end, ce, aw)) { 846f06a696dSPeter Xu /* Fix end so that it reaches the maximum */ 84737f51384SPrasad Singamsetty end = vtd_iova_limit(ce, aw); 848f06a696dSPeter Xu } 849f06a696dSPeter Xu 850f06a696dSPeter Xu return vtd_page_walk_level(addr, start, end, hook_fn, private, 85137f51384SPrasad Singamsetty level, true, true, notify_unmap, aw); 852f06a696dSPeter Xu } 853f06a696dSPeter Xu 8541da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */ 8551da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, 8561da12ec4SLe Tan uint8_t devfn, VTDContextEntry *ce) 8571da12ec4SLe Tan { 8581da12ec4SLe Tan VTDRootEntry re; 8591da12ec4SLe Tan int ret_fr; 860f80c9874SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 8611da12ec4SLe Tan 8621da12ec4SLe Tan ret_fr = vtd_get_root_entry(s, bus_num, &re); 8631da12ec4SLe Tan if (ret_fr) { 8641da12ec4SLe Tan return ret_fr; 8651da12ec4SLe Tan } 8661da12ec4SLe Tan 8671da12ec4SLe Tan if (!vtd_root_entry_present(&re)) { 8686c441e1dSPeter Xu /* Not error - it's okay we don't have root entry. */ 8696c441e1dSPeter Xu trace_vtd_re_not_present(bus_num); 8701da12ec4SLe Tan return -VTD_FR_ROOT_ENTRY_P; 871f80c9874SPeter Xu } 872f80c9874SPeter Xu 87337f51384SPrasad Singamsetty if (re.rsvd || (re.val & VTD_ROOT_ENTRY_RSVD(s->aw_bits))) { 8746c441e1dSPeter Xu trace_vtd_re_invalid(re.rsvd, re.val); 8751da12ec4SLe Tan return -VTD_FR_ROOT_ENTRY_RSVD; 8761da12ec4SLe Tan } 8771da12ec4SLe Tan 8781da12ec4SLe Tan ret_fr = vtd_get_context_entry_from_root(&re, devfn, ce); 8791da12ec4SLe Tan if (ret_fr) { 8801da12ec4SLe Tan return ret_fr; 8811da12ec4SLe Tan } 8821da12ec4SLe Tan 8838f7d7161SPeter Xu if (!vtd_ce_present(ce)) { 8846c441e1dSPeter Xu /* Not error - it's okay we don't have context entry. */ 8856c441e1dSPeter Xu trace_vtd_ce_not_present(bus_num, devfn); 8861da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_P; 887f80c9874SPeter Xu } 888f80c9874SPeter Xu 889f80c9874SPeter Xu if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) || 89037f51384SPrasad Singamsetty (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) { 8916c441e1dSPeter Xu trace_vtd_ce_invalid(ce->hi, ce->lo); 8921da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_RSVD; 8931da12ec4SLe Tan } 894f80c9874SPeter Xu 8951da12ec4SLe Tan /* Check if the programming of context-entry is valid */ 8968f7d7161SPeter Xu if (!vtd_is_level_supported(s, vtd_ce_get_level(ce))) { 8976c441e1dSPeter Xu trace_vtd_ce_invalid(ce->hi, ce->lo); 8981da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 899f80c9874SPeter Xu } 900f80c9874SPeter Xu 901f80c9874SPeter Xu /* Do translation type check */ 902f80c9874SPeter Xu if (!vtd_ce_type_check(x86_iommu, ce)) { 9036c441e1dSPeter Xu trace_vtd_ce_invalid(ce->hi, ce->lo); 9041da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 9051da12ec4SLe Tan } 906f80c9874SPeter Xu 9071da12ec4SLe Tan return 0; 9081da12ec4SLe Tan } 9091da12ec4SLe Tan 910dbaabb25SPeter Xu /* 911dbaabb25SPeter Xu * Fetch translation type for specific device. Returns <0 if error 912dbaabb25SPeter Xu * happens, otherwise return the shifted type to check against 913dbaabb25SPeter Xu * VTD_CONTEXT_TT_*. 914dbaabb25SPeter Xu */ 915dbaabb25SPeter Xu static int vtd_dev_get_trans_type(VTDAddressSpace *as) 916dbaabb25SPeter Xu { 917dbaabb25SPeter Xu IntelIOMMUState *s; 918dbaabb25SPeter Xu VTDContextEntry ce; 919dbaabb25SPeter Xu int ret; 920dbaabb25SPeter Xu 921dbaabb25SPeter Xu s = as->iommu_state; 922dbaabb25SPeter Xu 923dbaabb25SPeter Xu ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus), 924dbaabb25SPeter Xu as->devfn, &ce); 925dbaabb25SPeter Xu if (ret) { 926dbaabb25SPeter Xu return ret; 927dbaabb25SPeter Xu } 928dbaabb25SPeter Xu 929dbaabb25SPeter Xu return vtd_ce_get_type(&ce); 930dbaabb25SPeter Xu } 931dbaabb25SPeter Xu 932dbaabb25SPeter Xu static bool vtd_dev_pt_enabled(VTDAddressSpace *as) 933dbaabb25SPeter Xu { 934dbaabb25SPeter Xu int ret; 935dbaabb25SPeter Xu 936dbaabb25SPeter Xu assert(as); 937dbaabb25SPeter Xu 938dbaabb25SPeter Xu ret = vtd_dev_get_trans_type(as); 939dbaabb25SPeter Xu if (ret < 0) { 940dbaabb25SPeter Xu /* 941dbaabb25SPeter Xu * Possibly failed to parse the context entry for some reason 942dbaabb25SPeter Xu * (e.g., during init, or any guest configuration errors on 943dbaabb25SPeter Xu * context entries). We should assume PT not enabled for 944dbaabb25SPeter Xu * safety. 945dbaabb25SPeter Xu */ 946dbaabb25SPeter Xu return false; 947dbaabb25SPeter Xu } 948dbaabb25SPeter Xu 949dbaabb25SPeter Xu return ret == VTD_CONTEXT_TT_PASS_THROUGH; 950dbaabb25SPeter Xu } 951dbaabb25SPeter Xu 952dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */ 953dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as) 954dbaabb25SPeter Xu { 955dbaabb25SPeter Xu bool use_iommu; 95666a4a031SPeter Xu /* Whether we need to take the BQL on our own */ 95766a4a031SPeter Xu bool take_bql = !qemu_mutex_iothread_locked(); 958dbaabb25SPeter Xu 959dbaabb25SPeter Xu assert(as); 960dbaabb25SPeter Xu 961dbaabb25SPeter Xu use_iommu = as->iommu_state->dmar_enabled & !vtd_dev_pt_enabled(as); 962dbaabb25SPeter Xu 963dbaabb25SPeter Xu trace_vtd_switch_address_space(pci_bus_num(as->bus), 964dbaabb25SPeter Xu VTD_PCI_SLOT(as->devfn), 965dbaabb25SPeter Xu VTD_PCI_FUNC(as->devfn), 966dbaabb25SPeter Xu use_iommu); 967dbaabb25SPeter Xu 96866a4a031SPeter Xu /* 96966a4a031SPeter Xu * It's possible that we reach here without BQL, e.g., when called 97066a4a031SPeter Xu * from vtd_pt_enable_fast_path(). However the memory APIs need 97166a4a031SPeter Xu * it. We'd better make sure we have had it already, or, take it. 97266a4a031SPeter Xu */ 97366a4a031SPeter Xu if (take_bql) { 97466a4a031SPeter Xu qemu_mutex_lock_iothread(); 97566a4a031SPeter Xu } 97666a4a031SPeter Xu 977dbaabb25SPeter Xu /* Turn off first then on the other */ 978dbaabb25SPeter Xu if (use_iommu) { 979dbaabb25SPeter Xu memory_region_set_enabled(&as->sys_alias, false); 9803df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), true); 981dbaabb25SPeter Xu } else { 9823df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), false); 983dbaabb25SPeter Xu memory_region_set_enabled(&as->sys_alias, true); 984dbaabb25SPeter Xu } 985dbaabb25SPeter Xu 98666a4a031SPeter Xu if (take_bql) { 98766a4a031SPeter Xu qemu_mutex_unlock_iothread(); 98866a4a031SPeter Xu } 98966a4a031SPeter Xu 990dbaabb25SPeter Xu return use_iommu; 991dbaabb25SPeter Xu } 992dbaabb25SPeter Xu 993dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s) 994dbaabb25SPeter Xu { 995dbaabb25SPeter Xu GHashTableIter iter; 996dbaabb25SPeter Xu VTDBus *vtd_bus; 997dbaabb25SPeter Xu int i; 998dbaabb25SPeter Xu 999dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 1000dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 1001bf33cc75SPeter Xu for (i = 0; i < PCI_DEVFN_MAX; i++) { 1002dbaabb25SPeter Xu if (!vtd_bus->dev_as[i]) { 1003dbaabb25SPeter Xu continue; 1004dbaabb25SPeter Xu } 1005dbaabb25SPeter Xu vtd_switch_address_space(vtd_bus->dev_as[i]); 1006dbaabb25SPeter Xu } 1007dbaabb25SPeter Xu } 1008dbaabb25SPeter Xu } 1009dbaabb25SPeter Xu 10101da12ec4SLe Tan static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn) 10111da12ec4SLe Tan { 10121da12ec4SLe Tan return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL); 10131da12ec4SLe Tan } 10141da12ec4SLe Tan 10151da12ec4SLe Tan static const bool vtd_qualified_faults[] = { 10161da12ec4SLe Tan [VTD_FR_RESERVED] = false, 10171da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_P] = false, 10181da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_P] = true, 10191da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_INV] = true, 10201da12ec4SLe Tan [VTD_FR_ADDR_BEYOND_MGAW] = true, 10211da12ec4SLe Tan [VTD_FR_WRITE] = true, 10221da12ec4SLe Tan [VTD_FR_READ] = true, 10231da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_INV] = true, 10241da12ec4SLe Tan [VTD_FR_ROOT_TABLE_INV] = false, 10251da12ec4SLe Tan [VTD_FR_CONTEXT_TABLE_INV] = false, 10261da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_RSVD] = false, 10271da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_RSVD] = true, 10281da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_TT] = true, 10291da12ec4SLe Tan [VTD_FR_RESERVED_ERR] = false, 10301da12ec4SLe Tan [VTD_FR_MAX] = false, 10311da12ec4SLe Tan }; 10321da12ec4SLe Tan 10331da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software 10341da12ec4SLe Tan * only if the FPD field in the context-entry used to process the faulting 10351da12ec4SLe Tan * request is 0. 10361da12ec4SLe Tan */ 10371da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault) 10381da12ec4SLe Tan { 10391da12ec4SLe Tan return vtd_qualified_faults[fault]; 10401da12ec4SLe Tan } 10411da12ec4SLe Tan 10421da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr) 10431da12ec4SLe Tan { 10441da12ec4SLe Tan return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST; 10451da12ec4SLe Tan } 10461da12ec4SLe Tan 1047dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id) 1048dbaabb25SPeter Xu { 1049dbaabb25SPeter Xu VTDBus *vtd_bus; 1050dbaabb25SPeter Xu VTDAddressSpace *vtd_as; 1051dbaabb25SPeter Xu bool success = false; 1052dbaabb25SPeter Xu 1053dbaabb25SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id)); 1054dbaabb25SPeter Xu if (!vtd_bus) { 1055dbaabb25SPeter Xu goto out; 1056dbaabb25SPeter Xu } 1057dbaabb25SPeter Xu 1058dbaabb25SPeter Xu vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)]; 1059dbaabb25SPeter Xu if (!vtd_as) { 1060dbaabb25SPeter Xu goto out; 1061dbaabb25SPeter Xu } 1062dbaabb25SPeter Xu 1063dbaabb25SPeter Xu if (vtd_switch_address_space(vtd_as) == false) { 1064dbaabb25SPeter Xu /* We switched off IOMMU region successfully. */ 1065dbaabb25SPeter Xu success = true; 1066dbaabb25SPeter Xu } 1067dbaabb25SPeter Xu 1068dbaabb25SPeter Xu out: 1069dbaabb25SPeter Xu trace_vtd_pt_enable_fast_path(source_id, success); 1070dbaabb25SPeter Xu } 1071dbaabb25SPeter Xu 10721da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu 10731da12ec4SLe Tan * translation. 107479e2b9aeSPaolo Bonzini * 107579e2b9aeSPaolo Bonzini * Called from RCU critical section. 107679e2b9aeSPaolo Bonzini * 10771da12ec4SLe Tan * @bus_num: The bus number 10781da12ec4SLe Tan * @devfn: The devfn, which is the combined of device and function number 10791da12ec4SLe Tan * @is_write: The access is a write operation 10801da12ec4SLe Tan * @entry: IOMMUTLBEntry that contain the addr to be translated and result 1081b9313021SPeter Xu * 1082b9313021SPeter Xu * Returns true if translation is successful, otherwise false. 10831da12ec4SLe Tan */ 1084b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, 10851da12ec4SLe Tan uint8_t devfn, hwaddr addr, bool is_write, 10861da12ec4SLe Tan IOMMUTLBEntry *entry) 10871da12ec4SLe Tan { 1088d92fa2dcSLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 10891da12ec4SLe Tan VTDContextEntry ce; 10907df953bdSKnut Omang uint8_t bus_num = pci_bus_num(bus); 1091d92fa2dcSLe Tan VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; 1092d66b969bSJason Wang uint64_t slpte, page_mask; 10931da12ec4SLe Tan uint32_t level; 10941da12ec4SLe Tan uint16_t source_id = vtd_make_source_id(bus_num, devfn); 10951da12ec4SLe Tan int ret_fr; 10961da12ec4SLe Tan bool is_fpd_set = false; 10971da12ec4SLe Tan bool reads = true; 10981da12ec4SLe Tan bool writes = true; 109907f7b733SPeter Xu uint8_t access_flags; 1100b5a280c0SLe Tan VTDIOTLBEntry *iotlb_entry; 11011da12ec4SLe Tan 1102046ab7e9SPeter Xu /* 1103046ab7e9SPeter Xu * We have standalone memory region for interrupt addresses, we 1104046ab7e9SPeter Xu * should never receive translation requests in this region. 11051da12ec4SLe Tan */ 1106046ab7e9SPeter Xu assert(!vtd_is_interrupt_addr(addr)); 1107046ab7e9SPeter Xu 1108b5a280c0SLe Tan /* Try to fetch slpte form IOTLB */ 1109b5a280c0SLe Tan iotlb_entry = vtd_lookup_iotlb(s, source_id, addr); 1110b5a280c0SLe Tan if (iotlb_entry) { 11116c441e1dSPeter Xu trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, 11126c441e1dSPeter Xu iotlb_entry->domain_id); 1113b5a280c0SLe Tan slpte = iotlb_entry->slpte; 111407f7b733SPeter Xu access_flags = iotlb_entry->access_flags; 1115d66b969bSJason Wang page_mask = iotlb_entry->mask; 1116b5a280c0SLe Tan goto out; 1117b5a280c0SLe Tan } 1118b9313021SPeter Xu 1119d92fa2dcSLe Tan /* Try to fetch context-entry from cache first */ 1120d92fa2dcSLe Tan if (cc_entry->context_cache_gen == s->context_cache_gen) { 11216c441e1dSPeter Xu trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi, 11226c441e1dSPeter Xu cc_entry->context_entry.lo, 11236c441e1dSPeter Xu cc_entry->context_cache_gen); 1124d92fa2dcSLe Tan ce = cc_entry->context_entry; 1125d92fa2dcSLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 1126d92fa2dcSLe Tan } else { 11271da12ec4SLe Tan ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce); 11281da12ec4SLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 11291da12ec4SLe Tan if (ret_fr) { 11301da12ec4SLe Tan ret_fr = -ret_fr; 11311da12ec4SLe Tan if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) { 11326c441e1dSPeter Xu trace_vtd_fault_disabled(); 11331da12ec4SLe Tan } else { 11341da12ec4SLe Tan vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write); 11351da12ec4SLe Tan } 1136b9313021SPeter Xu goto error; 11371da12ec4SLe Tan } 1138d92fa2dcSLe Tan /* Update context-cache */ 11396c441e1dSPeter Xu trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo, 11406c441e1dSPeter Xu cc_entry->context_cache_gen, 11416c441e1dSPeter Xu s->context_cache_gen); 1142d92fa2dcSLe Tan cc_entry->context_entry = ce; 1143d92fa2dcSLe Tan cc_entry->context_cache_gen = s->context_cache_gen; 1144d92fa2dcSLe Tan } 11451da12ec4SLe Tan 1146dbaabb25SPeter Xu /* 1147dbaabb25SPeter Xu * We don't need to translate for pass-through context entries. 1148dbaabb25SPeter Xu * Also, let's ignore IOTLB caching as well for PT devices. 1149dbaabb25SPeter Xu */ 1150dbaabb25SPeter Xu if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) { 1151892721d9SPeter Xu entry->iova = addr & VTD_PAGE_MASK_4K; 1152dbaabb25SPeter Xu entry->translated_addr = entry->iova; 1153892721d9SPeter Xu entry->addr_mask = ~VTD_PAGE_MASK_4K; 1154dbaabb25SPeter Xu entry->perm = IOMMU_RW; 1155dbaabb25SPeter Xu trace_vtd_translate_pt(source_id, entry->iova); 1156dbaabb25SPeter Xu 1157dbaabb25SPeter Xu /* 1158dbaabb25SPeter Xu * When this happens, it means firstly caching-mode is not 1159dbaabb25SPeter Xu * enabled, and this is the first passthrough translation for 1160dbaabb25SPeter Xu * the device. Let's enable the fast path for passthrough. 1161dbaabb25SPeter Xu * 1162dbaabb25SPeter Xu * When passthrough is disabled again for the device, we can 1163dbaabb25SPeter Xu * capture it via the context entry invalidation, then the 1164dbaabb25SPeter Xu * IOMMU region can be swapped back. 1165dbaabb25SPeter Xu */ 1166dbaabb25SPeter Xu vtd_pt_enable_fast_path(s, source_id); 1167dbaabb25SPeter Xu 1168b9313021SPeter Xu return true; 1169dbaabb25SPeter Xu } 1170dbaabb25SPeter Xu 11716e905564SPeter Xu ret_fr = vtd_iova_to_slpte(&ce, addr, is_write, &slpte, &level, 117237f51384SPrasad Singamsetty &reads, &writes, s->aw_bits); 11731da12ec4SLe Tan if (ret_fr) { 11741da12ec4SLe Tan ret_fr = -ret_fr; 11751da12ec4SLe Tan if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) { 11766c441e1dSPeter Xu trace_vtd_fault_disabled(); 11771da12ec4SLe Tan } else { 11781da12ec4SLe Tan vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write); 11791da12ec4SLe Tan } 1180b9313021SPeter Xu goto error; 11811da12ec4SLe Tan } 11821da12ec4SLe Tan 1183d66b969bSJason Wang page_mask = vtd_slpt_level_page_mask(level); 118407f7b733SPeter Xu access_flags = IOMMU_ACCESS_FLAG(reads, writes); 1185b5a280c0SLe Tan vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte, 118607f7b733SPeter Xu access_flags, level); 1187b5a280c0SLe Tan out: 1188d66b969bSJason Wang entry->iova = addr & page_mask; 118937f51384SPrasad Singamsetty entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask; 1190d66b969bSJason Wang entry->addr_mask = ~page_mask; 119107f7b733SPeter Xu entry->perm = access_flags; 1192b9313021SPeter Xu return true; 1193b9313021SPeter Xu 1194b9313021SPeter Xu error: 1195b9313021SPeter Xu entry->iova = 0; 1196b9313021SPeter Xu entry->translated_addr = 0; 1197b9313021SPeter Xu entry->addr_mask = 0; 1198b9313021SPeter Xu entry->perm = IOMMU_NONE; 1199b9313021SPeter Xu return false; 12001da12ec4SLe Tan } 12011da12ec4SLe Tan 12021da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s) 12031da12ec4SLe Tan { 12041da12ec4SLe Tan s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 12051da12ec4SLe Tan s->root_extended = s->root & VTD_RTADDR_RTT; 120637f51384SPrasad Singamsetty s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits); 12071da12ec4SLe Tan 12087feb51b7SPeter Xu trace_vtd_reg_dmar_root(s->root, s->root_extended); 12091da12ec4SLe Tan } 12101da12ec4SLe Tan 121102a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global, 121202a2cbc8SPeter Xu uint32_t index, uint32_t mask) 121302a2cbc8SPeter Xu { 121402a2cbc8SPeter Xu x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask); 121502a2cbc8SPeter Xu } 121602a2cbc8SPeter Xu 1217a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s) 1218a5861439SPeter Xu { 1219a5861439SPeter Xu uint64_t value = 0; 1220a5861439SPeter Xu value = vtd_get_quad_raw(s, DMAR_IRTA_REG); 1221a5861439SPeter Xu s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1); 122237f51384SPrasad Singamsetty s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits); 122328589311SJan Kiszka s->intr_eime = value & VTD_IRTA_EIME; 1224a5861439SPeter Xu 122502a2cbc8SPeter Xu /* Notify global invalidation */ 122602a2cbc8SPeter Xu vtd_iec_notify_all(s, true, 0, 0); 1227a5861439SPeter Xu 12287feb51b7SPeter Xu trace_vtd_reg_ir_root(s->intr_root, s->intr_size); 1229a5861439SPeter Xu } 1230a5861439SPeter Xu 1231dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s) 1232dd4d607eSPeter Xu { 1233dd4d607eSPeter Xu IntelIOMMUNotifierNode *node; 1234dd4d607eSPeter Xu 1235dd4d607eSPeter Xu QLIST_FOREACH(node, &s->notifiers_list, next) { 1236dd4d607eSPeter Xu memory_region_iommu_replay_all(&node->vtd_as->iommu); 1237dd4d607eSPeter Xu } 1238dd4d607eSPeter Xu } 1239dd4d607eSPeter Xu 1240d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s) 1241d92fa2dcSLe Tan { 1242bc535e59SPeter Xu trace_vtd_inv_desc_cc_global(); 1243d92fa2dcSLe Tan s->context_cache_gen++; 1244d92fa2dcSLe Tan if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) { 1245d92fa2dcSLe Tan vtd_reset_context_cache(s); 1246d92fa2dcSLe Tan } 1247dbaabb25SPeter Xu vtd_switch_address_space_all(s); 1248dd4d607eSPeter Xu /* 1249dd4d607eSPeter Xu * From VT-d spec 6.5.2.1, a global context entry invalidation 1250dd4d607eSPeter Xu * should be followed by a IOTLB global invalidation, so we should 1251dd4d607eSPeter Xu * be safe even without this. Hoewever, let's replay the region as 1252dd4d607eSPeter Xu * well to be safer, and go back here when we need finer tunes for 1253dd4d607eSPeter Xu * VT-d emulation codes. 1254dd4d607eSPeter Xu */ 1255dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1256d92fa2dcSLe Tan } 1257d92fa2dcSLe Tan 1258d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation. 1259d92fa2dcSLe Tan * @func_mask: FM field after shifting 1260d92fa2dcSLe Tan */ 1261d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s, 1262d92fa2dcSLe Tan uint16_t source_id, 1263d92fa2dcSLe Tan uint16_t func_mask) 1264d92fa2dcSLe Tan { 1265d92fa2dcSLe Tan uint16_t mask; 12667df953bdSKnut Omang VTDBus *vtd_bus; 1267d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 1268bc535e59SPeter Xu uint8_t bus_n, devfn; 1269d92fa2dcSLe Tan uint16_t devfn_it; 1270d92fa2dcSLe Tan 1271bc535e59SPeter Xu trace_vtd_inv_desc_cc_devices(source_id, func_mask); 1272bc535e59SPeter Xu 1273d92fa2dcSLe Tan switch (func_mask & 3) { 1274d92fa2dcSLe Tan case 0: 1275d92fa2dcSLe Tan mask = 0; /* No bits in the SID field masked */ 1276d92fa2dcSLe Tan break; 1277d92fa2dcSLe Tan case 1: 1278d92fa2dcSLe Tan mask = 4; /* Mask bit 2 in the SID field */ 1279d92fa2dcSLe Tan break; 1280d92fa2dcSLe Tan case 2: 1281d92fa2dcSLe Tan mask = 6; /* Mask bit 2:1 in the SID field */ 1282d92fa2dcSLe Tan break; 1283d92fa2dcSLe Tan case 3: 1284d92fa2dcSLe Tan mask = 7; /* Mask bit 2:0 in the SID field */ 1285d92fa2dcSLe Tan break; 1286d92fa2dcSLe Tan } 12876cb99accSPeter Xu mask = ~mask; 1288bc535e59SPeter Xu 1289bc535e59SPeter Xu bus_n = VTD_SID_TO_BUS(source_id); 1290bc535e59SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, bus_n); 12917df953bdSKnut Omang if (vtd_bus) { 1292d92fa2dcSLe Tan devfn = VTD_SID_TO_DEVFN(source_id); 1293bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 12947df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 1295d92fa2dcSLe Tan if (vtd_as && ((devfn_it & mask) == (devfn & mask))) { 1296bc535e59SPeter Xu trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it), 1297bc535e59SPeter Xu VTD_PCI_FUNC(devfn_it)); 1298d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 1299dd4d607eSPeter Xu /* 1300dbaabb25SPeter Xu * Do switch address space when needed, in case if the 1301dbaabb25SPeter Xu * device passthrough bit is switched. 1302dbaabb25SPeter Xu */ 1303dbaabb25SPeter Xu vtd_switch_address_space(vtd_as); 1304dbaabb25SPeter Xu /* 1305dd4d607eSPeter Xu * So a device is moving out of (or moving into) a 1306dd4d607eSPeter Xu * domain, a replay() suites here to notify all the 1307dd4d607eSPeter Xu * IOMMU_NOTIFIER_MAP registers about this change. 1308dd4d607eSPeter Xu * This won't bring bad even if we have no such 1309dd4d607eSPeter Xu * notifier registered - the IOMMU notification 1310dd4d607eSPeter Xu * framework will skip MAP notifications if that 1311dd4d607eSPeter Xu * happened. 1312dd4d607eSPeter Xu */ 1313dd4d607eSPeter Xu memory_region_iommu_replay_all(&vtd_as->iommu); 1314d92fa2dcSLe Tan } 1315d92fa2dcSLe Tan } 1316d92fa2dcSLe Tan } 1317d92fa2dcSLe Tan } 1318d92fa2dcSLe Tan 13191da12ec4SLe Tan /* Context-cache invalidation 13201da12ec4SLe Tan * Returns the Context Actual Invalidation Granularity. 13211da12ec4SLe Tan * @val: the content of the CCMD_REG 13221da12ec4SLe Tan */ 13231da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val) 13241da12ec4SLe Tan { 13251da12ec4SLe Tan uint64_t caig; 13261da12ec4SLe Tan uint64_t type = val & VTD_CCMD_CIRG_MASK; 13271da12ec4SLe Tan 13281da12ec4SLe Tan switch (type) { 13291da12ec4SLe Tan case VTD_CCMD_DOMAIN_INVL: 1330d92fa2dcSLe Tan /* Fall through */ 1331d92fa2dcSLe Tan case VTD_CCMD_GLOBAL_INVL: 1332d92fa2dcSLe Tan caig = VTD_CCMD_GLOBAL_INVL_A; 1333d92fa2dcSLe Tan vtd_context_global_invalidate(s); 13341da12ec4SLe Tan break; 13351da12ec4SLe Tan 13361da12ec4SLe Tan case VTD_CCMD_DEVICE_INVL: 13371da12ec4SLe Tan caig = VTD_CCMD_DEVICE_INVL_A; 1338d92fa2dcSLe Tan vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val)); 13391da12ec4SLe Tan break; 13401da12ec4SLe Tan 13411da12ec4SLe Tan default: 13427feb51b7SPeter Xu trace_vtd_err("Context cache invalidate type error."); 13431da12ec4SLe Tan caig = 0; 13441da12ec4SLe Tan } 13451da12ec4SLe Tan return caig; 13461da12ec4SLe Tan } 13471da12ec4SLe Tan 1348b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s) 1349b5a280c0SLe Tan { 13507feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_global(); 1351b5a280c0SLe Tan vtd_reset_iotlb(s); 1352dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1353b5a280c0SLe Tan } 1354b5a280c0SLe Tan 1355b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) 1356b5a280c0SLe Tan { 1357dd4d607eSPeter Xu IntelIOMMUNotifierNode *node; 1358dd4d607eSPeter Xu VTDContextEntry ce; 1359dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 1360dd4d607eSPeter Xu 13617feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_domain(domain_id); 13627feb51b7SPeter Xu 1363b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain, 1364b5a280c0SLe Tan &domain_id); 1365dd4d607eSPeter Xu 1366dd4d607eSPeter Xu QLIST_FOREACH(node, &s->notifiers_list, next) { 1367dd4d607eSPeter Xu vtd_as = node->vtd_as; 1368dd4d607eSPeter Xu if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 1369dd4d607eSPeter Xu vtd_as->devfn, &ce) && 1370dd4d607eSPeter Xu domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) { 1371dd4d607eSPeter Xu memory_region_iommu_replay_all(&vtd_as->iommu); 1372dd4d607eSPeter Xu } 1373dd4d607eSPeter Xu } 1374dd4d607eSPeter Xu } 1375dd4d607eSPeter Xu 1376dd4d607eSPeter Xu static int vtd_page_invalidate_notify_hook(IOMMUTLBEntry *entry, 1377dd4d607eSPeter Xu void *private) 1378dd4d607eSPeter Xu { 13793df9d748SAlexey Kardashevskiy memory_region_notify_iommu((IOMMUMemoryRegion *)private, *entry); 1380dd4d607eSPeter Xu return 0; 1381dd4d607eSPeter Xu } 1382dd4d607eSPeter Xu 1383dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, 1384dd4d607eSPeter Xu uint16_t domain_id, hwaddr addr, 1385dd4d607eSPeter Xu uint8_t am) 1386dd4d607eSPeter Xu { 1387dd4d607eSPeter Xu IntelIOMMUNotifierNode *node; 1388dd4d607eSPeter Xu VTDContextEntry ce; 1389dd4d607eSPeter Xu int ret; 1390dd4d607eSPeter Xu 1391dd4d607eSPeter Xu QLIST_FOREACH(node, &(s->notifiers_list), next) { 1392dd4d607eSPeter Xu VTDAddressSpace *vtd_as = node->vtd_as; 1393dd4d607eSPeter Xu ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 1394dd4d607eSPeter Xu vtd_as->devfn, &ce); 1395dd4d607eSPeter Xu if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) { 1396dd4d607eSPeter Xu vtd_page_walk(&ce, addr, addr + (1 << am) * VTD_PAGE_SIZE, 1397dd4d607eSPeter Xu vtd_page_invalidate_notify_hook, 139837f51384SPrasad Singamsetty (void *)&vtd_as->iommu, true, s->aw_bits); 1399dd4d607eSPeter Xu } 1400dd4d607eSPeter Xu } 1401b5a280c0SLe Tan } 1402b5a280c0SLe Tan 1403b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, 1404b5a280c0SLe Tan hwaddr addr, uint8_t am) 1405b5a280c0SLe Tan { 1406b5a280c0SLe Tan VTDIOTLBPageInvInfo info; 1407b5a280c0SLe Tan 14087feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am); 14097feb51b7SPeter Xu 1410b5a280c0SLe Tan assert(am <= VTD_MAMV); 1411b5a280c0SLe Tan info.domain_id = domain_id; 1412d66b969bSJason Wang info.addr = addr; 1413b5a280c0SLe Tan info.mask = ~((1 << am) - 1); 1414b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); 1415dd4d607eSPeter Xu vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); 1416b5a280c0SLe Tan } 1417b5a280c0SLe Tan 14181da12ec4SLe Tan /* Flush IOTLB 14191da12ec4SLe Tan * Returns the IOTLB Actual Invalidation Granularity. 14201da12ec4SLe Tan * @val: the content of the IOTLB_REG 14211da12ec4SLe Tan */ 14221da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val) 14231da12ec4SLe Tan { 14241da12ec4SLe Tan uint64_t iaig; 14251da12ec4SLe Tan uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK; 1426b5a280c0SLe Tan uint16_t domain_id; 1427b5a280c0SLe Tan hwaddr addr; 1428b5a280c0SLe Tan uint8_t am; 14291da12ec4SLe Tan 14301da12ec4SLe Tan switch (type) { 14311da12ec4SLe Tan case VTD_TLB_GLOBAL_FLUSH: 14321da12ec4SLe Tan iaig = VTD_TLB_GLOBAL_FLUSH_A; 1433b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 14341da12ec4SLe Tan break; 14351da12ec4SLe Tan 14361da12ec4SLe Tan case VTD_TLB_DSI_FLUSH: 1437b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 14381da12ec4SLe Tan iaig = VTD_TLB_DSI_FLUSH_A; 1439b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 14401da12ec4SLe Tan break; 14411da12ec4SLe Tan 14421da12ec4SLe Tan case VTD_TLB_PSI_FLUSH: 1443b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 1444b5a280c0SLe Tan addr = vtd_get_quad_raw(s, DMAR_IVA_REG); 1445b5a280c0SLe Tan am = VTD_IVA_AM(addr); 1446b5a280c0SLe Tan addr = VTD_IVA_ADDR(addr); 1447b5a280c0SLe Tan if (am > VTD_MAMV) { 14487feb51b7SPeter Xu trace_vtd_err("IOTLB PSI flush: address mask overflow."); 1449b5a280c0SLe Tan iaig = 0; 1450b5a280c0SLe Tan break; 1451b5a280c0SLe Tan } 14521da12ec4SLe Tan iaig = VTD_TLB_PSI_FLUSH_A; 1453b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 14541da12ec4SLe Tan break; 14551da12ec4SLe Tan 14561da12ec4SLe Tan default: 14577feb51b7SPeter Xu trace_vtd_err("IOTLB flush: invalid granularity."); 14581da12ec4SLe Tan iaig = 0; 14591da12ec4SLe Tan } 14601da12ec4SLe Tan return iaig; 14611da12ec4SLe Tan } 14621da12ec4SLe Tan 14638991c460SLadi Prosek static void vtd_fetch_inv_desc(IntelIOMMUState *s); 1464ed7b8fbcSLe Tan 1465ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s) 1466ed7b8fbcSLe Tan { 1467ed7b8fbcSLe Tan return s->qi_enabled && (s->iq_tail == s->iq_head) && 1468ed7b8fbcSLe Tan (s->iq_last_desc_type == VTD_INV_DESC_WAIT); 1469ed7b8fbcSLe Tan } 1470ed7b8fbcSLe Tan 1471ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en) 1472ed7b8fbcSLe Tan { 1473ed7b8fbcSLe Tan uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG); 1474ed7b8fbcSLe Tan 14757feb51b7SPeter Xu trace_vtd_inv_qi_enable(en); 14767feb51b7SPeter Xu 1477ed7b8fbcSLe Tan if (en) { 147837f51384SPrasad Singamsetty s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits); 1479ed7b8fbcSLe Tan /* 2^(x+8) entries */ 1480ed7b8fbcSLe Tan s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8); 1481ed7b8fbcSLe Tan s->qi_enabled = true; 14827feb51b7SPeter Xu trace_vtd_inv_qi_setup(s->iq, s->iq_size); 1483ed7b8fbcSLe Tan /* Ok - report back to driver */ 1484ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES); 14858991c460SLadi Prosek 14868991c460SLadi Prosek if (s->iq_tail != 0) { 14878991c460SLadi Prosek /* 14888991c460SLadi Prosek * This is a spec violation but Windows guests are known to set up 14898991c460SLadi Prosek * Queued Invalidation this way so we allow the write and process 14908991c460SLadi Prosek * Invalidation Descriptors right away. 14918991c460SLadi Prosek */ 14928991c460SLadi Prosek trace_vtd_warn_invalid_qi_tail(s->iq_tail); 14938991c460SLadi Prosek if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 14948991c460SLadi Prosek vtd_fetch_inv_desc(s); 14958991c460SLadi Prosek } 1496ed7b8fbcSLe Tan } 1497ed7b8fbcSLe Tan } else { 1498ed7b8fbcSLe Tan if (vtd_queued_inv_disable_check(s)) { 1499ed7b8fbcSLe Tan /* disable Queued Invalidation */ 1500ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 0); 1501ed7b8fbcSLe Tan s->iq_head = 0; 1502ed7b8fbcSLe Tan s->qi_enabled = false; 1503ed7b8fbcSLe Tan /* Ok - report back to driver */ 1504ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0); 1505ed7b8fbcSLe Tan } else { 15067feb51b7SPeter Xu trace_vtd_err_qi_disable(s->iq_head, s->iq_tail, s->iq_last_desc_type); 1507ed7b8fbcSLe Tan } 1508ed7b8fbcSLe Tan } 1509ed7b8fbcSLe Tan } 1510ed7b8fbcSLe Tan 15111da12ec4SLe Tan /* Set Root Table Pointer */ 15121da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s) 15131da12ec4SLe Tan { 15141da12ec4SLe Tan vtd_root_table_setup(s); 15151da12ec4SLe Tan /* Ok - report back to driver */ 15161da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS); 15171da12ec4SLe Tan } 15181da12ec4SLe Tan 1519a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */ 1520a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s) 1521a5861439SPeter Xu { 1522a5861439SPeter Xu vtd_interrupt_remap_table_setup(s); 1523a5861439SPeter Xu /* Ok - report back to driver */ 1524a5861439SPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS); 1525a5861439SPeter Xu } 1526a5861439SPeter Xu 15271da12ec4SLe Tan /* Handle Translation Enable/Disable */ 15281da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en) 15291da12ec4SLe Tan { 1530558e0024SPeter Xu if (s->dmar_enabled == en) { 1531558e0024SPeter Xu return; 1532558e0024SPeter Xu } 1533558e0024SPeter Xu 15347feb51b7SPeter Xu trace_vtd_dmar_enable(en); 15351da12ec4SLe Tan 15361da12ec4SLe Tan if (en) { 15371da12ec4SLe Tan s->dmar_enabled = true; 15381da12ec4SLe Tan /* Ok - report back to driver */ 15391da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES); 15401da12ec4SLe Tan } else { 15411da12ec4SLe Tan s->dmar_enabled = false; 15421da12ec4SLe Tan 15431da12ec4SLe Tan /* Clear the index of Fault Recording Register */ 15441da12ec4SLe Tan s->next_frcd_reg = 0; 15451da12ec4SLe Tan /* Ok - report back to driver */ 15461da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0); 15471da12ec4SLe Tan } 1548558e0024SPeter Xu 1549558e0024SPeter Xu vtd_switch_address_space_all(s); 15501da12ec4SLe Tan } 15511da12ec4SLe Tan 155280de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */ 155380de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) 155480de52baSPeter Xu { 15557feb51b7SPeter Xu trace_vtd_ir_enable(en); 155680de52baSPeter Xu 155780de52baSPeter Xu if (en) { 155880de52baSPeter Xu s->intr_enabled = true; 155980de52baSPeter Xu /* Ok - report back to driver */ 156080de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES); 156180de52baSPeter Xu } else { 156280de52baSPeter Xu s->intr_enabled = false; 156380de52baSPeter Xu /* Ok - report back to driver */ 156480de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0); 156580de52baSPeter Xu } 156680de52baSPeter Xu } 156780de52baSPeter Xu 15681da12ec4SLe Tan /* Handle write to Global Command Register */ 15691da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s) 15701da12ec4SLe Tan { 15711da12ec4SLe Tan uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG); 15721da12ec4SLe Tan uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG); 15731da12ec4SLe Tan uint32_t changed = status ^ val; 15741da12ec4SLe Tan 15757feb51b7SPeter Xu trace_vtd_reg_write_gcmd(status, val); 15761da12ec4SLe Tan if (changed & VTD_GCMD_TE) { 15771da12ec4SLe Tan /* Translation enable/disable */ 15781da12ec4SLe Tan vtd_handle_gcmd_te(s, val & VTD_GCMD_TE); 15791da12ec4SLe Tan } 15801da12ec4SLe Tan if (val & VTD_GCMD_SRTP) { 15811da12ec4SLe Tan /* Set/update the root-table pointer */ 15821da12ec4SLe Tan vtd_handle_gcmd_srtp(s); 15831da12ec4SLe Tan } 1584ed7b8fbcSLe Tan if (changed & VTD_GCMD_QIE) { 1585ed7b8fbcSLe Tan /* Queued Invalidation Enable */ 1586ed7b8fbcSLe Tan vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE); 1587ed7b8fbcSLe Tan } 1588a5861439SPeter Xu if (val & VTD_GCMD_SIRTP) { 1589a5861439SPeter Xu /* Set/update the interrupt remapping root-table pointer */ 1590a5861439SPeter Xu vtd_handle_gcmd_sirtp(s); 1591a5861439SPeter Xu } 159280de52baSPeter Xu if (changed & VTD_GCMD_IRE) { 159380de52baSPeter Xu /* Interrupt remap enable/disable */ 159480de52baSPeter Xu vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE); 159580de52baSPeter Xu } 15961da12ec4SLe Tan } 15971da12ec4SLe Tan 15981da12ec4SLe Tan /* Handle write to Context Command Register */ 15991da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s) 16001da12ec4SLe Tan { 16011da12ec4SLe Tan uint64_t ret; 16021da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG); 16031da12ec4SLe Tan 16041da12ec4SLe Tan /* Context-cache invalidation request */ 16051da12ec4SLe Tan if (val & VTD_CCMD_ICC) { 1606ed7b8fbcSLe Tan if (s->qi_enabled) { 16077feb51b7SPeter Xu trace_vtd_err("Queued Invalidation enabled, " 1608ed7b8fbcSLe Tan "should not use register-based invalidation"); 1609ed7b8fbcSLe Tan return; 1610ed7b8fbcSLe Tan } 16111da12ec4SLe Tan ret = vtd_context_cache_invalidate(s, val); 16121da12ec4SLe Tan /* Invalidation completed. Change something to show */ 16131da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL); 16141da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK, 16151da12ec4SLe Tan ret); 16161da12ec4SLe Tan } 16171da12ec4SLe Tan } 16181da12ec4SLe Tan 16191da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */ 16201da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s) 16211da12ec4SLe Tan { 16221da12ec4SLe Tan uint64_t ret; 16231da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG); 16241da12ec4SLe Tan 16251da12ec4SLe Tan /* IOTLB invalidation request */ 16261da12ec4SLe Tan if (val & VTD_TLB_IVT) { 1627ed7b8fbcSLe Tan if (s->qi_enabled) { 16287feb51b7SPeter Xu trace_vtd_err("Queued Invalidation enabled, " 16297feb51b7SPeter Xu "should not use register-based invalidation."); 1630ed7b8fbcSLe Tan return; 1631ed7b8fbcSLe Tan } 16321da12ec4SLe Tan ret = vtd_iotlb_flush(s, val); 16331da12ec4SLe Tan /* Invalidation completed. Change something to show */ 16341da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL); 16351da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, 16361da12ec4SLe Tan VTD_TLB_FLUSH_GRANU_MASK_A, ret); 16371da12ec4SLe Tan } 16381da12ec4SLe Tan } 16391da12ec4SLe Tan 1640ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */ 1641ed7b8fbcSLe Tan static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset, 1642ed7b8fbcSLe Tan VTDInvDesc *inv_desc) 1643ed7b8fbcSLe Tan { 1644ed7b8fbcSLe Tan dma_addr_t addr = base_addr + offset * sizeof(*inv_desc); 1645ed7b8fbcSLe Tan if (dma_memory_read(&address_space_memory, addr, inv_desc, 1646ed7b8fbcSLe Tan sizeof(*inv_desc))) { 16477feb51b7SPeter Xu trace_vtd_err("Read INV DESC failed."); 1648ed7b8fbcSLe Tan inv_desc->lo = 0; 1649ed7b8fbcSLe Tan inv_desc->hi = 0; 1650ed7b8fbcSLe Tan return false; 1651ed7b8fbcSLe Tan } 1652ed7b8fbcSLe Tan inv_desc->lo = le64_to_cpu(inv_desc->lo); 1653ed7b8fbcSLe Tan inv_desc->hi = le64_to_cpu(inv_desc->hi); 1654ed7b8fbcSLe Tan return true; 1655ed7b8fbcSLe Tan } 1656ed7b8fbcSLe Tan 1657ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 1658ed7b8fbcSLe Tan { 1659ed7b8fbcSLe Tan if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) || 1660ed7b8fbcSLe Tan (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) { 1661bc535e59SPeter Xu trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo); 1662ed7b8fbcSLe Tan return false; 1663ed7b8fbcSLe Tan } 1664ed7b8fbcSLe Tan if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) { 1665ed7b8fbcSLe Tan /* Status Write */ 1666ed7b8fbcSLe Tan uint32_t status_data = (uint32_t)(inv_desc->lo >> 1667ed7b8fbcSLe Tan VTD_INV_DESC_WAIT_DATA_SHIFT); 1668ed7b8fbcSLe Tan 1669ed7b8fbcSLe Tan assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF)); 1670ed7b8fbcSLe Tan 1671ed7b8fbcSLe Tan /* FIXME: need to be masked with HAW? */ 1672ed7b8fbcSLe Tan dma_addr_t status_addr = inv_desc->hi; 1673bc535e59SPeter Xu trace_vtd_inv_desc_wait_sw(status_addr, status_data); 1674ed7b8fbcSLe Tan status_data = cpu_to_le32(status_data); 1675ed7b8fbcSLe Tan if (dma_memory_write(&address_space_memory, status_addr, &status_data, 1676ed7b8fbcSLe Tan sizeof(status_data))) { 1677bc535e59SPeter Xu trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo); 1678ed7b8fbcSLe Tan return false; 1679ed7b8fbcSLe Tan } 1680ed7b8fbcSLe Tan } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) { 1681ed7b8fbcSLe Tan /* Interrupt flag */ 1682ed7b8fbcSLe Tan vtd_generate_completion_event(s); 1683ed7b8fbcSLe Tan } else { 1684bc535e59SPeter Xu trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo); 1685ed7b8fbcSLe Tan return false; 1686ed7b8fbcSLe Tan } 1687ed7b8fbcSLe Tan return true; 1688ed7b8fbcSLe Tan } 1689ed7b8fbcSLe Tan 1690d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s, 1691d92fa2dcSLe Tan VTDInvDesc *inv_desc) 1692d92fa2dcSLe Tan { 1693bc535e59SPeter Xu uint16_t sid, fmask; 1694bc535e59SPeter Xu 1695d92fa2dcSLe Tan if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) { 1696bc535e59SPeter Xu trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo); 1697d92fa2dcSLe Tan return false; 1698d92fa2dcSLe Tan } 1699d92fa2dcSLe Tan switch (inv_desc->lo & VTD_INV_DESC_CC_G) { 1700d92fa2dcSLe Tan case VTD_INV_DESC_CC_DOMAIN: 1701bc535e59SPeter Xu trace_vtd_inv_desc_cc_domain( 1702d92fa2dcSLe Tan (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo)); 1703d92fa2dcSLe Tan /* Fall through */ 1704d92fa2dcSLe Tan case VTD_INV_DESC_CC_GLOBAL: 1705d92fa2dcSLe Tan vtd_context_global_invalidate(s); 1706d92fa2dcSLe Tan break; 1707d92fa2dcSLe Tan 1708d92fa2dcSLe Tan case VTD_INV_DESC_CC_DEVICE: 1709bc535e59SPeter Xu sid = VTD_INV_DESC_CC_SID(inv_desc->lo); 1710bc535e59SPeter Xu fmask = VTD_INV_DESC_CC_FM(inv_desc->lo); 1711bc535e59SPeter Xu vtd_context_device_invalidate(s, sid, fmask); 1712d92fa2dcSLe Tan break; 1713d92fa2dcSLe Tan 1714d92fa2dcSLe Tan default: 1715bc535e59SPeter Xu trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo); 1716d92fa2dcSLe Tan return false; 1717d92fa2dcSLe Tan } 1718d92fa2dcSLe Tan return true; 1719d92fa2dcSLe Tan } 1720d92fa2dcSLe Tan 1721b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 1722b5a280c0SLe Tan { 1723b5a280c0SLe Tan uint16_t domain_id; 1724b5a280c0SLe Tan uint8_t am; 1725b5a280c0SLe Tan hwaddr addr; 1726b5a280c0SLe Tan 1727b5a280c0SLe Tan if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) || 1728b5a280c0SLe Tan (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) { 1729bc535e59SPeter Xu trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo); 1730b5a280c0SLe Tan return false; 1731b5a280c0SLe Tan } 1732b5a280c0SLe Tan 1733b5a280c0SLe Tan switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) { 1734b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_GLOBAL: 1735b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 1736b5a280c0SLe Tan break; 1737b5a280c0SLe Tan 1738b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_DOMAIN: 1739b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 1740b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 1741b5a280c0SLe Tan break; 1742b5a280c0SLe Tan 1743b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_PAGE: 1744b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 1745b5a280c0SLe Tan addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi); 1746b5a280c0SLe Tan am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi); 1747b5a280c0SLe Tan if (am > VTD_MAMV) { 1748bc535e59SPeter Xu trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo); 1749b5a280c0SLe Tan return false; 1750b5a280c0SLe Tan } 1751b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 1752b5a280c0SLe Tan break; 1753b5a280c0SLe Tan 1754b5a280c0SLe Tan default: 1755bc535e59SPeter Xu trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo); 1756b5a280c0SLe Tan return false; 1757b5a280c0SLe Tan } 1758b5a280c0SLe Tan return true; 1759b5a280c0SLe Tan } 1760b5a280c0SLe Tan 176102a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, 176202a2cbc8SPeter Xu VTDInvDesc *inv_desc) 176302a2cbc8SPeter Xu { 17647feb51b7SPeter Xu trace_vtd_inv_desc_iec(inv_desc->iec.granularity, 176502a2cbc8SPeter Xu inv_desc->iec.index, 176602a2cbc8SPeter Xu inv_desc->iec.index_mask); 176702a2cbc8SPeter Xu 176802a2cbc8SPeter Xu vtd_iec_notify_all(s, !inv_desc->iec.granularity, 176902a2cbc8SPeter Xu inv_desc->iec.index, 177002a2cbc8SPeter Xu inv_desc->iec.index_mask); 1771554f5e16SJason Wang return true; 1772554f5e16SJason Wang } 177302a2cbc8SPeter Xu 1774554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, 1775554f5e16SJason Wang VTDInvDesc *inv_desc) 1776554f5e16SJason Wang { 1777554f5e16SJason Wang VTDAddressSpace *vtd_dev_as; 1778554f5e16SJason Wang IOMMUTLBEntry entry; 1779554f5e16SJason Wang struct VTDBus *vtd_bus; 1780554f5e16SJason Wang hwaddr addr; 1781554f5e16SJason Wang uint64_t sz; 1782554f5e16SJason Wang uint16_t sid; 1783554f5e16SJason Wang uint8_t devfn; 1784554f5e16SJason Wang bool size; 1785554f5e16SJason Wang uint8_t bus_num; 1786554f5e16SJason Wang 1787554f5e16SJason Wang addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi); 1788554f5e16SJason Wang sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo); 1789554f5e16SJason Wang devfn = sid & 0xff; 1790554f5e16SJason Wang bus_num = sid >> 8; 1791554f5e16SJason Wang size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi); 1792554f5e16SJason Wang 1793554f5e16SJason Wang if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) || 1794554f5e16SJason Wang (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) { 17957feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo); 1796554f5e16SJason Wang return false; 1797554f5e16SJason Wang } 1798554f5e16SJason Wang 1799554f5e16SJason Wang vtd_bus = vtd_find_as_from_bus_num(s, bus_num); 1800554f5e16SJason Wang if (!vtd_bus) { 1801554f5e16SJason Wang goto done; 1802554f5e16SJason Wang } 1803554f5e16SJason Wang 1804554f5e16SJason Wang vtd_dev_as = vtd_bus->dev_as[devfn]; 1805554f5e16SJason Wang if (!vtd_dev_as) { 1806554f5e16SJason Wang goto done; 1807554f5e16SJason Wang } 1808554f5e16SJason Wang 180904eb6247SJason Wang /* According to ATS spec table 2.4: 181004eb6247SJason Wang * S = 0, bits 15:12 = xxxx range size: 4K 181104eb6247SJason Wang * S = 1, bits 15:12 = xxx0 range size: 8K 181204eb6247SJason Wang * S = 1, bits 15:12 = xx01 range size: 16K 181304eb6247SJason Wang * S = 1, bits 15:12 = x011 range size: 32K 181404eb6247SJason Wang * S = 1, bits 15:12 = 0111 range size: 64K 181504eb6247SJason Wang * ... 181604eb6247SJason Wang */ 1817554f5e16SJason Wang if (size) { 181804eb6247SJason Wang sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT); 1819554f5e16SJason Wang addr &= ~(sz - 1); 1820554f5e16SJason Wang } else { 1821554f5e16SJason Wang sz = VTD_PAGE_SIZE; 1822554f5e16SJason Wang } 1823554f5e16SJason Wang 1824554f5e16SJason Wang entry.target_as = &vtd_dev_as->as; 1825554f5e16SJason Wang entry.addr_mask = sz - 1; 1826554f5e16SJason Wang entry.iova = addr; 1827554f5e16SJason Wang entry.perm = IOMMU_NONE; 1828554f5e16SJason Wang entry.translated_addr = 0; 182910315b9bSJason Wang memory_region_notify_iommu(&vtd_dev_as->iommu, entry); 1830554f5e16SJason Wang 1831554f5e16SJason Wang done: 183202a2cbc8SPeter Xu return true; 183302a2cbc8SPeter Xu } 183402a2cbc8SPeter Xu 1835ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s) 1836ed7b8fbcSLe Tan { 1837ed7b8fbcSLe Tan VTDInvDesc inv_desc; 1838ed7b8fbcSLe Tan uint8_t desc_type; 1839ed7b8fbcSLe Tan 18407feb51b7SPeter Xu trace_vtd_inv_qi_head(s->iq_head); 1841ed7b8fbcSLe Tan if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) { 1842ed7b8fbcSLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 1843ed7b8fbcSLe Tan return false; 1844ed7b8fbcSLe Tan } 1845ed7b8fbcSLe Tan desc_type = inv_desc.lo & VTD_INV_DESC_TYPE; 1846ed7b8fbcSLe Tan /* FIXME: should update at first or at last? */ 1847ed7b8fbcSLe Tan s->iq_last_desc_type = desc_type; 1848ed7b8fbcSLe Tan 1849ed7b8fbcSLe Tan switch (desc_type) { 1850ed7b8fbcSLe Tan case VTD_INV_DESC_CC: 1851bc535e59SPeter Xu trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo); 1852d92fa2dcSLe Tan if (!vtd_process_context_cache_desc(s, &inv_desc)) { 1853d92fa2dcSLe Tan return false; 1854d92fa2dcSLe Tan } 1855ed7b8fbcSLe Tan break; 1856ed7b8fbcSLe Tan 1857ed7b8fbcSLe Tan case VTD_INV_DESC_IOTLB: 1858bc535e59SPeter Xu trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo); 1859b5a280c0SLe Tan if (!vtd_process_iotlb_desc(s, &inv_desc)) { 1860b5a280c0SLe Tan return false; 1861b5a280c0SLe Tan } 1862ed7b8fbcSLe Tan break; 1863ed7b8fbcSLe Tan 1864ed7b8fbcSLe Tan case VTD_INV_DESC_WAIT: 1865bc535e59SPeter Xu trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo); 1866ed7b8fbcSLe Tan if (!vtd_process_wait_desc(s, &inv_desc)) { 1867ed7b8fbcSLe Tan return false; 1868ed7b8fbcSLe Tan } 1869ed7b8fbcSLe Tan break; 1870ed7b8fbcSLe Tan 1871b7910472SPeter Xu case VTD_INV_DESC_IEC: 1872bc535e59SPeter Xu trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo); 187302a2cbc8SPeter Xu if (!vtd_process_inv_iec_desc(s, &inv_desc)) { 187402a2cbc8SPeter Xu return false; 187502a2cbc8SPeter Xu } 1876b7910472SPeter Xu break; 1877b7910472SPeter Xu 1878554f5e16SJason Wang case VTD_INV_DESC_DEVICE: 18797feb51b7SPeter Xu trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo); 1880554f5e16SJason Wang if (!vtd_process_device_iotlb_desc(s, &inv_desc)) { 1881554f5e16SJason Wang return false; 1882554f5e16SJason Wang } 1883554f5e16SJason Wang break; 1884554f5e16SJason Wang 1885ed7b8fbcSLe Tan default: 1886bc535e59SPeter Xu trace_vtd_inv_desc_invalid(inv_desc.hi, inv_desc.lo); 1887ed7b8fbcSLe Tan return false; 1888ed7b8fbcSLe Tan } 1889ed7b8fbcSLe Tan s->iq_head++; 1890ed7b8fbcSLe Tan if (s->iq_head == s->iq_size) { 1891ed7b8fbcSLe Tan s->iq_head = 0; 1892ed7b8fbcSLe Tan } 1893ed7b8fbcSLe Tan return true; 1894ed7b8fbcSLe Tan } 1895ed7b8fbcSLe Tan 1896ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */ 1897ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s) 1898ed7b8fbcSLe Tan { 18997feb51b7SPeter Xu trace_vtd_inv_qi_fetch(); 19007feb51b7SPeter Xu 1901ed7b8fbcSLe Tan if (s->iq_tail >= s->iq_size) { 1902ed7b8fbcSLe Tan /* Detects an invalid Tail pointer */ 19037feb51b7SPeter Xu trace_vtd_err_qi_tail(s->iq_tail, s->iq_size); 1904ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 1905ed7b8fbcSLe Tan return; 1906ed7b8fbcSLe Tan } 1907ed7b8fbcSLe Tan while (s->iq_head != s->iq_tail) { 1908ed7b8fbcSLe Tan if (!vtd_process_inv_desc(s)) { 1909ed7b8fbcSLe Tan /* Invalidation Queue Errors */ 1910ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 1911ed7b8fbcSLe Tan break; 1912ed7b8fbcSLe Tan } 1913ed7b8fbcSLe Tan /* Must update the IQH_REG in time */ 1914ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 1915ed7b8fbcSLe Tan (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) & 1916ed7b8fbcSLe Tan VTD_IQH_QH_MASK); 1917ed7b8fbcSLe Tan } 1918ed7b8fbcSLe Tan } 1919ed7b8fbcSLe Tan 1920ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */ 1921ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s) 1922ed7b8fbcSLe Tan { 1923ed7b8fbcSLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG); 1924ed7b8fbcSLe Tan 1925ed7b8fbcSLe Tan s->iq_tail = VTD_IQT_QT(val); 19267feb51b7SPeter Xu trace_vtd_inv_qi_tail(s->iq_tail); 19277feb51b7SPeter Xu 1928ed7b8fbcSLe Tan if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 1929ed7b8fbcSLe Tan /* Process Invalidation Queue here */ 1930ed7b8fbcSLe Tan vtd_fetch_inv_desc(s); 1931ed7b8fbcSLe Tan } 1932ed7b8fbcSLe Tan } 1933ed7b8fbcSLe Tan 19341da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s) 19351da12ec4SLe Tan { 19361da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 19371da12ec4SLe Tan uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 19381da12ec4SLe Tan uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE; 19391da12ec4SLe Tan 19401da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) { 19411da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 19427feb51b7SPeter Xu trace_vtd_fsts_clear_ip(); 19431da12ec4SLe Tan } 1944ed7b8fbcSLe Tan /* FIXME: when IQE is Clear, should we try to fetch some Invalidation 1945ed7b8fbcSLe Tan * Descriptors if there are any when Queued Invalidation is enabled? 1946ed7b8fbcSLe Tan */ 19471da12ec4SLe Tan } 19481da12ec4SLe Tan 19491da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s) 19501da12ec4SLe Tan { 19511da12ec4SLe Tan uint32_t fectl_reg; 19521da12ec4SLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 19531da12ec4SLe Tan * need to compare the old value and the new value to conclude that 19541da12ec4SLe Tan * software clears the IM field? Or just check if the IM field is zero? 19551da12ec4SLe Tan */ 19561da12ec4SLe Tan fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 19577feb51b7SPeter Xu 19587feb51b7SPeter Xu trace_vtd_reg_write_fectl(fectl_reg); 19597feb51b7SPeter Xu 19601da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) { 19611da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 19621da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 19631da12ec4SLe Tan } 19641da12ec4SLe Tan } 19651da12ec4SLe Tan 1966ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s) 1967ed7b8fbcSLe Tan { 1968ed7b8fbcSLe Tan uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG); 1969ed7b8fbcSLe Tan uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 1970ed7b8fbcSLe Tan 1971ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) { 19727feb51b7SPeter Xu trace_vtd_reg_ics_clear_ip(); 1973ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 1974ed7b8fbcSLe Tan } 1975ed7b8fbcSLe Tan } 1976ed7b8fbcSLe Tan 1977ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s) 1978ed7b8fbcSLe Tan { 1979ed7b8fbcSLe Tan uint32_t iectl_reg; 1980ed7b8fbcSLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 1981ed7b8fbcSLe Tan * need to compare the old value and the new value to conclude that 1982ed7b8fbcSLe Tan * software clears the IM field? Or just check if the IM field is zero? 1983ed7b8fbcSLe Tan */ 1984ed7b8fbcSLe Tan iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 19857feb51b7SPeter Xu 19867feb51b7SPeter Xu trace_vtd_reg_write_iectl(iectl_reg); 19877feb51b7SPeter Xu 1988ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) { 1989ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 1990ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 1991ed7b8fbcSLe Tan } 1992ed7b8fbcSLe Tan } 1993ed7b8fbcSLe Tan 19941da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size) 19951da12ec4SLe Tan { 19961da12ec4SLe Tan IntelIOMMUState *s = opaque; 19971da12ec4SLe Tan uint64_t val; 19981da12ec4SLe Tan 19997feb51b7SPeter Xu trace_vtd_reg_read(addr, size); 20007feb51b7SPeter Xu 20011da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 20027feb51b7SPeter Xu trace_vtd_err("Read MMIO over range."); 20031da12ec4SLe Tan return (uint64_t)-1; 20041da12ec4SLe Tan } 20051da12ec4SLe Tan 20061da12ec4SLe Tan switch (addr) { 20071da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 20081da12ec4SLe Tan case DMAR_RTADDR_REG: 20091da12ec4SLe Tan if (size == 4) { 20101da12ec4SLe Tan val = s->root & ((1ULL << 32) - 1); 20111da12ec4SLe Tan } else { 20121da12ec4SLe Tan val = s->root; 20131da12ec4SLe Tan } 20141da12ec4SLe Tan break; 20151da12ec4SLe Tan 20161da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 20171da12ec4SLe Tan assert(size == 4); 20181da12ec4SLe Tan val = s->root >> 32; 20191da12ec4SLe Tan break; 20201da12ec4SLe Tan 2021ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2022ed7b8fbcSLe Tan case DMAR_IQA_REG: 2023ed7b8fbcSLe Tan val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS); 2024ed7b8fbcSLe Tan if (size == 4) { 2025ed7b8fbcSLe Tan val = val & ((1ULL << 32) - 1); 2026ed7b8fbcSLe Tan } 2027ed7b8fbcSLe Tan break; 2028ed7b8fbcSLe Tan 2029ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2030ed7b8fbcSLe Tan assert(size == 4); 2031ed7b8fbcSLe Tan val = s->iq >> 32; 2032ed7b8fbcSLe Tan break; 2033ed7b8fbcSLe Tan 20341da12ec4SLe Tan default: 20351da12ec4SLe Tan if (size == 4) { 20361da12ec4SLe Tan val = vtd_get_long(s, addr); 20371da12ec4SLe Tan } else { 20381da12ec4SLe Tan val = vtd_get_quad(s, addr); 20391da12ec4SLe Tan } 20401da12ec4SLe Tan } 20417feb51b7SPeter Xu 20421da12ec4SLe Tan return val; 20431da12ec4SLe Tan } 20441da12ec4SLe Tan 20451da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr, 20461da12ec4SLe Tan uint64_t val, unsigned size) 20471da12ec4SLe Tan { 20481da12ec4SLe Tan IntelIOMMUState *s = opaque; 20491da12ec4SLe Tan 20507feb51b7SPeter Xu trace_vtd_reg_write(addr, size, val); 20517feb51b7SPeter Xu 20521da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 20537feb51b7SPeter Xu trace_vtd_err("Write MMIO over range."); 20541da12ec4SLe Tan return; 20551da12ec4SLe Tan } 20561da12ec4SLe Tan 20571da12ec4SLe Tan switch (addr) { 20581da12ec4SLe Tan /* Global Command Register, 32-bit */ 20591da12ec4SLe Tan case DMAR_GCMD_REG: 20601da12ec4SLe Tan vtd_set_long(s, addr, val); 20611da12ec4SLe Tan vtd_handle_gcmd_write(s); 20621da12ec4SLe Tan break; 20631da12ec4SLe Tan 20641da12ec4SLe Tan /* Context Command Register, 64-bit */ 20651da12ec4SLe Tan case DMAR_CCMD_REG: 20661da12ec4SLe Tan if (size == 4) { 20671da12ec4SLe Tan vtd_set_long(s, addr, val); 20681da12ec4SLe Tan } else { 20691da12ec4SLe Tan vtd_set_quad(s, addr, val); 20701da12ec4SLe Tan vtd_handle_ccmd_write(s); 20711da12ec4SLe Tan } 20721da12ec4SLe Tan break; 20731da12ec4SLe Tan 20741da12ec4SLe Tan case DMAR_CCMD_REG_HI: 20751da12ec4SLe Tan assert(size == 4); 20761da12ec4SLe Tan vtd_set_long(s, addr, val); 20771da12ec4SLe Tan vtd_handle_ccmd_write(s); 20781da12ec4SLe Tan break; 20791da12ec4SLe Tan 20801da12ec4SLe Tan /* IOTLB Invalidation Register, 64-bit */ 20811da12ec4SLe Tan case DMAR_IOTLB_REG: 20821da12ec4SLe Tan if (size == 4) { 20831da12ec4SLe Tan vtd_set_long(s, addr, val); 20841da12ec4SLe Tan } else { 20851da12ec4SLe Tan vtd_set_quad(s, addr, val); 20861da12ec4SLe Tan vtd_handle_iotlb_write(s); 20871da12ec4SLe Tan } 20881da12ec4SLe Tan break; 20891da12ec4SLe Tan 20901da12ec4SLe Tan case DMAR_IOTLB_REG_HI: 20911da12ec4SLe Tan assert(size == 4); 20921da12ec4SLe Tan vtd_set_long(s, addr, val); 20931da12ec4SLe Tan vtd_handle_iotlb_write(s); 20941da12ec4SLe Tan break; 20951da12ec4SLe Tan 2096b5a280c0SLe Tan /* Invalidate Address Register, 64-bit */ 2097b5a280c0SLe Tan case DMAR_IVA_REG: 2098b5a280c0SLe Tan if (size == 4) { 2099b5a280c0SLe Tan vtd_set_long(s, addr, val); 2100b5a280c0SLe Tan } else { 2101b5a280c0SLe Tan vtd_set_quad(s, addr, val); 2102b5a280c0SLe Tan } 2103b5a280c0SLe Tan break; 2104b5a280c0SLe Tan 2105b5a280c0SLe Tan case DMAR_IVA_REG_HI: 2106b5a280c0SLe Tan assert(size == 4); 2107b5a280c0SLe Tan vtd_set_long(s, addr, val); 2108b5a280c0SLe Tan break; 2109b5a280c0SLe Tan 21101da12ec4SLe Tan /* Fault Status Register, 32-bit */ 21111da12ec4SLe Tan case DMAR_FSTS_REG: 21121da12ec4SLe Tan assert(size == 4); 21131da12ec4SLe Tan vtd_set_long(s, addr, val); 21141da12ec4SLe Tan vtd_handle_fsts_write(s); 21151da12ec4SLe Tan break; 21161da12ec4SLe Tan 21171da12ec4SLe Tan /* Fault Event Control Register, 32-bit */ 21181da12ec4SLe Tan case DMAR_FECTL_REG: 21191da12ec4SLe Tan assert(size == 4); 21201da12ec4SLe Tan vtd_set_long(s, addr, val); 21211da12ec4SLe Tan vtd_handle_fectl_write(s); 21221da12ec4SLe Tan break; 21231da12ec4SLe Tan 21241da12ec4SLe Tan /* Fault Event Data Register, 32-bit */ 21251da12ec4SLe Tan case DMAR_FEDATA_REG: 21261da12ec4SLe Tan assert(size == 4); 21271da12ec4SLe Tan vtd_set_long(s, addr, val); 21281da12ec4SLe Tan break; 21291da12ec4SLe Tan 21301da12ec4SLe Tan /* Fault Event Address Register, 32-bit */ 21311da12ec4SLe Tan case DMAR_FEADDR_REG: 21321da12ec4SLe Tan assert(size == 4); 21331da12ec4SLe Tan vtd_set_long(s, addr, val); 21341da12ec4SLe Tan break; 21351da12ec4SLe Tan 21361da12ec4SLe Tan /* Fault Event Upper Address Register, 32-bit */ 21371da12ec4SLe Tan case DMAR_FEUADDR_REG: 21381da12ec4SLe Tan assert(size == 4); 21391da12ec4SLe Tan vtd_set_long(s, addr, val); 21401da12ec4SLe Tan break; 21411da12ec4SLe Tan 21421da12ec4SLe Tan /* Protected Memory Enable Register, 32-bit */ 21431da12ec4SLe Tan case DMAR_PMEN_REG: 21441da12ec4SLe Tan assert(size == 4); 21451da12ec4SLe Tan vtd_set_long(s, addr, val); 21461da12ec4SLe Tan break; 21471da12ec4SLe Tan 21481da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 21491da12ec4SLe Tan case DMAR_RTADDR_REG: 21501da12ec4SLe Tan if (size == 4) { 21511da12ec4SLe Tan vtd_set_long(s, addr, val); 21521da12ec4SLe Tan } else { 21531da12ec4SLe Tan vtd_set_quad(s, addr, val); 21541da12ec4SLe Tan } 21551da12ec4SLe Tan break; 21561da12ec4SLe Tan 21571da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 21581da12ec4SLe Tan assert(size == 4); 21591da12ec4SLe Tan vtd_set_long(s, addr, val); 21601da12ec4SLe Tan break; 21611da12ec4SLe Tan 2162ed7b8fbcSLe Tan /* Invalidation Queue Tail Register, 64-bit */ 2163ed7b8fbcSLe Tan case DMAR_IQT_REG: 2164ed7b8fbcSLe Tan if (size == 4) { 2165ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2166ed7b8fbcSLe Tan } else { 2167ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2168ed7b8fbcSLe Tan } 2169ed7b8fbcSLe Tan vtd_handle_iqt_write(s); 2170ed7b8fbcSLe Tan break; 2171ed7b8fbcSLe Tan 2172ed7b8fbcSLe Tan case DMAR_IQT_REG_HI: 2173ed7b8fbcSLe Tan assert(size == 4); 2174ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2175ed7b8fbcSLe Tan /* 19:63 of IQT_REG is RsvdZ, do nothing here */ 2176ed7b8fbcSLe Tan break; 2177ed7b8fbcSLe Tan 2178ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2179ed7b8fbcSLe Tan case DMAR_IQA_REG: 2180ed7b8fbcSLe Tan if (size == 4) { 2181ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2182ed7b8fbcSLe Tan } else { 2183ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2184ed7b8fbcSLe Tan } 2185ed7b8fbcSLe Tan break; 2186ed7b8fbcSLe Tan 2187ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2188ed7b8fbcSLe Tan assert(size == 4); 2189ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2190ed7b8fbcSLe Tan break; 2191ed7b8fbcSLe Tan 2192ed7b8fbcSLe Tan /* Invalidation Completion Status Register, 32-bit */ 2193ed7b8fbcSLe Tan case DMAR_ICS_REG: 2194ed7b8fbcSLe Tan assert(size == 4); 2195ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2196ed7b8fbcSLe Tan vtd_handle_ics_write(s); 2197ed7b8fbcSLe Tan break; 2198ed7b8fbcSLe Tan 2199ed7b8fbcSLe Tan /* Invalidation Event Control Register, 32-bit */ 2200ed7b8fbcSLe Tan case DMAR_IECTL_REG: 2201ed7b8fbcSLe Tan assert(size == 4); 2202ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2203ed7b8fbcSLe Tan vtd_handle_iectl_write(s); 2204ed7b8fbcSLe Tan break; 2205ed7b8fbcSLe Tan 2206ed7b8fbcSLe Tan /* Invalidation Event Data Register, 32-bit */ 2207ed7b8fbcSLe Tan case DMAR_IEDATA_REG: 2208ed7b8fbcSLe Tan assert(size == 4); 2209ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2210ed7b8fbcSLe Tan break; 2211ed7b8fbcSLe Tan 2212ed7b8fbcSLe Tan /* Invalidation Event Address Register, 32-bit */ 2213ed7b8fbcSLe Tan case DMAR_IEADDR_REG: 2214ed7b8fbcSLe Tan assert(size == 4); 2215ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2216ed7b8fbcSLe Tan break; 2217ed7b8fbcSLe Tan 2218ed7b8fbcSLe Tan /* Invalidation Event Upper Address Register, 32-bit */ 2219ed7b8fbcSLe Tan case DMAR_IEUADDR_REG: 2220ed7b8fbcSLe Tan assert(size == 4); 2221ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2222ed7b8fbcSLe Tan break; 2223ed7b8fbcSLe Tan 22241da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 22251da12ec4SLe Tan case DMAR_FRCD_REG_0_0: 22261da12ec4SLe Tan if (size == 4) { 22271da12ec4SLe Tan vtd_set_long(s, addr, val); 22281da12ec4SLe Tan } else { 22291da12ec4SLe Tan vtd_set_quad(s, addr, val); 22301da12ec4SLe Tan } 22311da12ec4SLe Tan break; 22321da12ec4SLe Tan 22331da12ec4SLe Tan case DMAR_FRCD_REG_0_1: 22341da12ec4SLe Tan assert(size == 4); 22351da12ec4SLe Tan vtd_set_long(s, addr, val); 22361da12ec4SLe Tan break; 22371da12ec4SLe Tan 22381da12ec4SLe Tan case DMAR_FRCD_REG_0_2: 22391da12ec4SLe Tan if (size == 4) { 22401da12ec4SLe Tan vtd_set_long(s, addr, val); 22411da12ec4SLe Tan } else { 22421da12ec4SLe Tan vtd_set_quad(s, addr, val); 22431da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 22441da12ec4SLe Tan vtd_update_fsts_ppf(s); 22451da12ec4SLe Tan } 22461da12ec4SLe Tan break; 22471da12ec4SLe Tan 22481da12ec4SLe Tan case DMAR_FRCD_REG_0_3: 22491da12ec4SLe Tan assert(size == 4); 22501da12ec4SLe Tan vtd_set_long(s, addr, val); 22511da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 22521da12ec4SLe Tan vtd_update_fsts_ppf(s); 22531da12ec4SLe Tan break; 22541da12ec4SLe Tan 2255a5861439SPeter Xu case DMAR_IRTA_REG: 2256a5861439SPeter Xu if (size == 4) { 2257a5861439SPeter Xu vtd_set_long(s, addr, val); 2258a5861439SPeter Xu } else { 2259a5861439SPeter Xu vtd_set_quad(s, addr, val); 2260a5861439SPeter Xu } 2261a5861439SPeter Xu break; 2262a5861439SPeter Xu 2263a5861439SPeter Xu case DMAR_IRTA_REG_HI: 2264a5861439SPeter Xu assert(size == 4); 2265a5861439SPeter Xu vtd_set_long(s, addr, val); 2266a5861439SPeter Xu break; 2267a5861439SPeter Xu 22681da12ec4SLe Tan default: 22691da12ec4SLe Tan if (size == 4) { 22701da12ec4SLe Tan vtd_set_long(s, addr, val); 22711da12ec4SLe Tan } else { 22721da12ec4SLe Tan vtd_set_quad(s, addr, val); 22731da12ec4SLe Tan } 22741da12ec4SLe Tan } 22751da12ec4SLe Tan } 22761da12ec4SLe Tan 22773df9d748SAlexey Kardashevskiy static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr, 2278bf55b7afSPeter Xu IOMMUAccessFlags flag) 22791da12ec4SLe Tan { 22801da12ec4SLe Tan VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 22811da12ec4SLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 2282b9313021SPeter Xu IOMMUTLBEntry iotlb = { 2283b9313021SPeter Xu /* We'll fill in the rest later. */ 22841da12ec4SLe Tan .target_as = &address_space_memory, 22851da12ec4SLe Tan }; 2286b9313021SPeter Xu bool success; 22871da12ec4SLe Tan 2288b9313021SPeter Xu if (likely(s->dmar_enabled)) { 2289b9313021SPeter Xu success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn, 2290b9313021SPeter Xu addr, flag & IOMMU_WO, &iotlb); 2291b9313021SPeter Xu } else { 22921da12ec4SLe Tan /* DMAR disabled, passthrough, use 4k-page*/ 2293b9313021SPeter Xu iotlb.iova = addr & VTD_PAGE_MASK_4K; 2294b9313021SPeter Xu iotlb.translated_addr = addr & VTD_PAGE_MASK_4K; 2295b9313021SPeter Xu iotlb.addr_mask = ~VTD_PAGE_MASK_4K; 2296b9313021SPeter Xu iotlb.perm = IOMMU_RW; 2297b9313021SPeter Xu success = true; 22981da12ec4SLe Tan } 22991da12ec4SLe Tan 2300b9313021SPeter Xu if (likely(success)) { 23017feb51b7SPeter Xu trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus), 23027feb51b7SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 23037feb51b7SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 2304b9313021SPeter Xu iotlb.iova, iotlb.translated_addr, 2305b9313021SPeter Xu iotlb.addr_mask); 2306b9313021SPeter Xu } else { 2307b9313021SPeter Xu trace_vtd_err_dmar_translate(pci_bus_num(vtd_as->bus), 2308b9313021SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 2309b9313021SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 2310b9313021SPeter Xu iotlb.iova); 2311b9313021SPeter Xu } 23127feb51b7SPeter Xu 2313b9313021SPeter Xu return iotlb; 23141da12ec4SLe Tan } 23151da12ec4SLe Tan 23163df9d748SAlexey Kardashevskiy static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, 23175bf3d319SPeter Xu IOMMUNotifierFlag old, 23185bf3d319SPeter Xu IOMMUNotifierFlag new) 23193cb3b154SAlex Williamson { 23203cb3b154SAlex Williamson VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 2321dd4d607eSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 2322dd4d607eSPeter Xu IntelIOMMUNotifierNode *node = NULL; 2323dd4d607eSPeter Xu IntelIOMMUNotifierNode *next_node = NULL; 23243cb3b154SAlex Williamson 2325dd4d607eSPeter Xu if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) { 23264c427a4cSPeter Xu error_report("We need to set caching-mode=1 for intel-iommu to enable " 2327dd4d607eSPeter Xu "device assignment with IOMMU protection."); 2328a3276f78SPeter Xu exit(1); 2329a3276f78SPeter Xu } 2330dd4d607eSPeter Xu 2331dd4d607eSPeter Xu if (old == IOMMU_NOTIFIER_NONE) { 2332dd4d607eSPeter Xu node = g_malloc0(sizeof(*node)); 2333dd4d607eSPeter Xu node->vtd_as = vtd_as; 2334dd4d607eSPeter Xu QLIST_INSERT_HEAD(&s->notifiers_list, node, next); 2335dd4d607eSPeter Xu return; 2336dd4d607eSPeter Xu } 2337dd4d607eSPeter Xu 2338dd4d607eSPeter Xu /* update notifier node with new flags */ 2339dd4d607eSPeter Xu QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { 2340dd4d607eSPeter Xu if (node->vtd_as == vtd_as) { 2341dd4d607eSPeter Xu if (new == IOMMU_NOTIFIER_NONE) { 2342dd4d607eSPeter Xu QLIST_REMOVE(node, next); 2343dd4d607eSPeter Xu g_free(node); 2344dd4d607eSPeter Xu } 2345dd4d607eSPeter Xu return; 2346dd4d607eSPeter Xu } 2347dd4d607eSPeter Xu } 23483cb3b154SAlex Williamson } 23493cb3b154SAlex Williamson 2350552a1e01SPeter Xu static int vtd_post_load(void *opaque, int version_id) 2351552a1e01SPeter Xu { 2352552a1e01SPeter Xu IntelIOMMUState *iommu = opaque; 2353552a1e01SPeter Xu 2354552a1e01SPeter Xu /* 2355552a1e01SPeter Xu * Memory regions are dynamically turned on/off depending on 2356552a1e01SPeter Xu * context entry configurations from the guest. After migration, 2357552a1e01SPeter Xu * we need to make sure the memory regions are still correct. 2358552a1e01SPeter Xu */ 2359552a1e01SPeter Xu vtd_switch_address_space_all(iommu); 2360552a1e01SPeter Xu 2361552a1e01SPeter Xu return 0; 2362552a1e01SPeter Xu } 2363552a1e01SPeter Xu 23641da12ec4SLe Tan static const VMStateDescription vtd_vmstate = { 23651da12ec4SLe Tan .name = "iommu-intel", 23668cdcf3c1SPeter Xu .version_id = 1, 23678cdcf3c1SPeter Xu .minimum_version_id = 1, 23688cdcf3c1SPeter Xu .priority = MIG_PRI_IOMMU, 2369552a1e01SPeter Xu .post_load = vtd_post_load, 23708cdcf3c1SPeter Xu .fields = (VMStateField[]) { 23718cdcf3c1SPeter Xu VMSTATE_UINT64(root, IntelIOMMUState), 23728cdcf3c1SPeter Xu VMSTATE_UINT64(intr_root, IntelIOMMUState), 23738cdcf3c1SPeter Xu VMSTATE_UINT64(iq, IntelIOMMUState), 23748cdcf3c1SPeter Xu VMSTATE_UINT32(intr_size, IntelIOMMUState), 23758cdcf3c1SPeter Xu VMSTATE_UINT16(iq_head, IntelIOMMUState), 23768cdcf3c1SPeter Xu VMSTATE_UINT16(iq_tail, IntelIOMMUState), 23778cdcf3c1SPeter Xu VMSTATE_UINT16(iq_size, IntelIOMMUState), 23788cdcf3c1SPeter Xu VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState), 23798cdcf3c1SPeter Xu VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE), 23808cdcf3c1SPeter Xu VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState), 23818cdcf3c1SPeter Xu VMSTATE_BOOL(root_extended, IntelIOMMUState), 23828cdcf3c1SPeter Xu VMSTATE_BOOL(dmar_enabled, IntelIOMMUState), 23838cdcf3c1SPeter Xu VMSTATE_BOOL(qi_enabled, IntelIOMMUState), 23848cdcf3c1SPeter Xu VMSTATE_BOOL(intr_enabled, IntelIOMMUState), 23858cdcf3c1SPeter Xu VMSTATE_BOOL(intr_eime, IntelIOMMUState), 23868cdcf3c1SPeter Xu VMSTATE_END_OF_LIST() 23878cdcf3c1SPeter Xu } 23881da12ec4SLe Tan }; 23891da12ec4SLe Tan 23901da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = { 23911da12ec4SLe Tan .read = vtd_mem_read, 23921da12ec4SLe Tan .write = vtd_mem_write, 23931da12ec4SLe Tan .endianness = DEVICE_LITTLE_ENDIAN, 23941da12ec4SLe Tan .impl = { 23951da12ec4SLe Tan .min_access_size = 4, 23961da12ec4SLe Tan .max_access_size = 8, 23971da12ec4SLe Tan }, 23981da12ec4SLe Tan .valid = { 23991da12ec4SLe Tan .min_access_size = 4, 24001da12ec4SLe Tan .max_access_size = 8, 24011da12ec4SLe Tan }, 24021da12ec4SLe Tan }; 24031da12ec4SLe Tan 24041da12ec4SLe Tan static Property vtd_properties[] = { 24051da12ec4SLe Tan DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0), 2406e6b6af05SRadim Krčmář DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim, 2407e6b6af05SRadim Krčmář ON_OFF_AUTO_AUTO), 2408fb506e70SRadim Krčmář DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false), 240937f51384SPrasad Singamsetty DEFINE_PROP_UINT8("x-aw-bits", IntelIOMMUState, aw_bits, 241037f51384SPrasad Singamsetty VTD_HOST_ADDRESS_WIDTH), 24113b40f0e5SAviv Ben-David DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), 24121da12ec4SLe Tan DEFINE_PROP_END_OF_LIST(), 24131da12ec4SLe Tan }; 24141da12ec4SLe Tan 2415651e4cefSPeter Xu /* Read IRTE entry with specific index */ 2416651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index, 2417bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry *entry, uint16_t sid) 2418651e4cefSPeter Xu { 2419ede9c94aSPeter Xu static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \ 2420ede9c94aSPeter Xu {0xffff, 0xfffb, 0xfff9, 0xfff8}; 2421651e4cefSPeter Xu dma_addr_t addr = 0x00; 2422ede9c94aSPeter Xu uint16_t mask, source_id; 2423ede9c94aSPeter Xu uint8_t bus, bus_max, bus_min; 2424651e4cefSPeter Xu 2425651e4cefSPeter Xu addr = iommu->intr_root + index * sizeof(*entry); 2426651e4cefSPeter Xu if (dma_memory_read(&address_space_memory, addr, entry, 2427651e4cefSPeter Xu sizeof(*entry))) { 24287feb51b7SPeter Xu trace_vtd_err("Memory read failed for IRTE."); 2429651e4cefSPeter Xu return -VTD_FR_IR_ROOT_INVAL; 2430651e4cefSPeter Xu } 2431651e4cefSPeter Xu 24327feb51b7SPeter Xu trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]), 24337feb51b7SPeter Xu le64_to_cpu(entry->data[0])); 24347feb51b7SPeter Xu 2435bc38ee10SMichael S. Tsirkin if (!entry->irte.present) { 24367feb51b7SPeter Xu trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]), 2437651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 2438651e4cefSPeter Xu return -VTD_FR_IR_ENTRY_P; 2439651e4cefSPeter Xu } 2440651e4cefSPeter Xu 2441bc38ee10SMichael S. Tsirkin if (entry->irte.__reserved_0 || entry->irte.__reserved_1 || 2442bc38ee10SMichael S. Tsirkin entry->irte.__reserved_2) { 24437feb51b7SPeter Xu trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]), 2444651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 2445651e4cefSPeter Xu return -VTD_FR_IR_IRTE_RSVD; 2446651e4cefSPeter Xu } 2447651e4cefSPeter Xu 2448ede9c94aSPeter Xu if (sid != X86_IOMMU_SID_INVALID) { 2449ede9c94aSPeter Xu /* Validate IRTE SID */ 2450bc38ee10SMichael S. Tsirkin source_id = le32_to_cpu(entry->irte.source_id); 2451bc38ee10SMichael S. Tsirkin switch (entry->irte.sid_vtype) { 2452ede9c94aSPeter Xu case VTD_SVT_NONE: 2453ede9c94aSPeter Xu break; 2454ede9c94aSPeter Xu 2455ede9c94aSPeter Xu case VTD_SVT_ALL: 2456bc38ee10SMichael S. Tsirkin mask = vtd_svt_mask[entry->irte.sid_q]; 2457ede9c94aSPeter Xu if ((source_id & mask) != (sid & mask)) { 24587feb51b7SPeter Xu trace_vtd_err_irte_sid(index, sid, source_id); 2459ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 2460ede9c94aSPeter Xu } 2461ede9c94aSPeter Xu break; 2462ede9c94aSPeter Xu 2463ede9c94aSPeter Xu case VTD_SVT_BUS: 2464ede9c94aSPeter Xu bus_max = source_id >> 8; 2465ede9c94aSPeter Xu bus_min = source_id & 0xff; 2466ede9c94aSPeter Xu bus = sid >> 8; 2467ede9c94aSPeter Xu if (bus > bus_max || bus < bus_min) { 24687feb51b7SPeter Xu trace_vtd_err_irte_sid_bus(index, bus, bus_min, bus_max); 2469ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 2470ede9c94aSPeter Xu } 2471ede9c94aSPeter Xu break; 2472ede9c94aSPeter Xu 2473ede9c94aSPeter Xu default: 24747feb51b7SPeter Xu trace_vtd_err_irte_svt(index, entry->irte.sid_vtype); 2475ede9c94aSPeter Xu /* Take this as verification failure. */ 2476ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 2477ede9c94aSPeter Xu break; 2478ede9c94aSPeter Xu } 2479ede9c94aSPeter Xu } 2480651e4cefSPeter Xu 2481651e4cefSPeter Xu return 0; 2482651e4cefSPeter Xu } 2483651e4cefSPeter Xu 2484651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */ 2485ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index, 2486ede9c94aSPeter Xu VTDIrq *irq, uint16_t sid) 2487651e4cefSPeter Xu { 2488bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry irte = {}; 2489651e4cefSPeter Xu int ret = 0; 2490651e4cefSPeter Xu 2491ede9c94aSPeter Xu ret = vtd_irte_get(iommu, index, &irte, sid); 2492651e4cefSPeter Xu if (ret) { 2493651e4cefSPeter Xu return ret; 2494651e4cefSPeter Xu } 2495651e4cefSPeter Xu 2496bc38ee10SMichael S. Tsirkin irq->trigger_mode = irte.irte.trigger_mode; 2497bc38ee10SMichael S. Tsirkin irq->vector = irte.irte.vector; 2498bc38ee10SMichael S. Tsirkin irq->delivery_mode = irte.irte.delivery_mode; 2499bc38ee10SMichael S. Tsirkin irq->dest = le32_to_cpu(irte.irte.dest_id); 250028589311SJan Kiszka if (!iommu->intr_eime) { 2501651e4cefSPeter Xu #define VTD_IR_APIC_DEST_MASK (0xff00ULL) 2502651e4cefSPeter Xu #define VTD_IR_APIC_DEST_SHIFT (8) 250328589311SJan Kiszka irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >> 2504651e4cefSPeter Xu VTD_IR_APIC_DEST_SHIFT; 250528589311SJan Kiszka } 2506bc38ee10SMichael S. Tsirkin irq->dest_mode = irte.irte.dest_mode; 2507bc38ee10SMichael S. Tsirkin irq->redir_hint = irte.irte.redir_hint; 2508651e4cefSPeter Xu 25097feb51b7SPeter Xu trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector, 25107feb51b7SPeter Xu irq->delivery_mode, irq->dest, irq->dest_mode); 2511651e4cefSPeter Xu 2512651e4cefSPeter Xu return 0; 2513651e4cefSPeter Xu } 2514651e4cefSPeter Xu 2515651e4cefSPeter Xu /* Generate one MSI message from VTDIrq info */ 2516651e4cefSPeter Xu static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out) 2517651e4cefSPeter Xu { 2518651e4cefSPeter Xu VTD_MSIMessage msg = {}; 2519651e4cefSPeter Xu 2520651e4cefSPeter Xu /* Generate address bits */ 2521651e4cefSPeter Xu msg.dest_mode = irq->dest_mode; 2522651e4cefSPeter Xu msg.redir_hint = irq->redir_hint; 2523651e4cefSPeter Xu msg.dest = irq->dest; 252432946019SRadim Krčmář msg.__addr_hi = irq->dest & 0xffffff00; 2525651e4cefSPeter Xu msg.__addr_head = cpu_to_le32(0xfee); 2526651e4cefSPeter Xu /* Keep this from original MSI address bits */ 2527651e4cefSPeter Xu msg.__not_used = irq->msi_addr_last_bits; 2528651e4cefSPeter Xu 2529651e4cefSPeter Xu /* Generate data bits */ 2530651e4cefSPeter Xu msg.vector = irq->vector; 2531651e4cefSPeter Xu msg.delivery_mode = irq->delivery_mode; 2532651e4cefSPeter Xu msg.level = 1; 2533651e4cefSPeter Xu msg.trigger_mode = irq->trigger_mode; 2534651e4cefSPeter Xu 2535651e4cefSPeter Xu msg_out->address = msg.msi_addr; 2536651e4cefSPeter Xu msg_out->data = msg.msi_data; 2537651e4cefSPeter Xu } 2538651e4cefSPeter Xu 2539651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */ 2540651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu, 2541651e4cefSPeter Xu MSIMessage *origin, 2542ede9c94aSPeter Xu MSIMessage *translated, 2543ede9c94aSPeter Xu uint16_t sid) 2544651e4cefSPeter Xu { 2545651e4cefSPeter Xu int ret = 0; 2546651e4cefSPeter Xu VTD_IR_MSIAddress addr; 2547651e4cefSPeter Xu uint16_t index; 254809cd058aSMichael S. Tsirkin VTDIrq irq = {}; 2549651e4cefSPeter Xu 2550651e4cefSPeter Xu assert(origin && translated); 2551651e4cefSPeter Xu 25527feb51b7SPeter Xu trace_vtd_ir_remap_msi_req(origin->address, origin->data); 25537feb51b7SPeter Xu 2554651e4cefSPeter Xu if (!iommu || !iommu->intr_enabled) { 2555e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 2556e7a3b91fSPeter Xu goto out; 2557651e4cefSPeter Xu } 2558651e4cefSPeter Xu 2559651e4cefSPeter Xu if (origin->address & VTD_MSI_ADDR_HI_MASK) { 25607feb51b7SPeter Xu trace_vtd_err("MSI address high 32 bits non-zero when " 25617feb51b7SPeter Xu "Interrupt Remapping enabled."); 2562651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 2563651e4cefSPeter Xu } 2564651e4cefSPeter Xu 2565651e4cefSPeter Xu addr.data = origin->address & VTD_MSI_ADDR_LO_MASK; 25661a43713bSPeter Xu if (addr.addr.__head != 0xfee) { 25677feb51b7SPeter Xu trace_vtd_err("MSI addr low 32 bit invalid."); 2568651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 2569651e4cefSPeter Xu } 2570651e4cefSPeter Xu 2571651e4cefSPeter Xu /* This is compatible mode. */ 2572bc38ee10SMichael S. Tsirkin if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) { 2573e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 2574e7a3b91fSPeter Xu goto out; 2575651e4cefSPeter Xu } 2576651e4cefSPeter Xu 2577bc38ee10SMichael S. Tsirkin index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l); 2578651e4cefSPeter Xu 2579651e4cefSPeter Xu #define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff) 2580651e4cefSPeter Xu #define VTD_IR_MSI_DATA_RESERVED (0xffff0000) 2581651e4cefSPeter Xu 2582bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 2583651e4cefSPeter Xu /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */ 2584651e4cefSPeter Xu index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE; 2585651e4cefSPeter Xu } 2586651e4cefSPeter Xu 2587ede9c94aSPeter Xu ret = vtd_remap_irq_get(iommu, index, &irq, sid); 2588651e4cefSPeter Xu if (ret) { 2589651e4cefSPeter Xu return ret; 2590651e4cefSPeter Xu } 2591651e4cefSPeter Xu 2592bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 25937feb51b7SPeter Xu trace_vtd_ir_remap_type("MSI"); 2594651e4cefSPeter Xu if (origin->data & VTD_IR_MSI_DATA_RESERVED) { 25957feb51b7SPeter Xu trace_vtd_err_ir_msi_invalid(sid, origin->address, origin->data); 2596651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 2597651e4cefSPeter Xu } 2598651e4cefSPeter Xu } else { 2599651e4cefSPeter Xu uint8_t vector = origin->data & 0xff; 2600dea651a9SFeng Wu uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1; 2601dea651a9SFeng Wu 26027feb51b7SPeter Xu trace_vtd_ir_remap_type("IOAPIC"); 2603651e4cefSPeter Xu /* IOAPIC entry vector should be aligned with IRTE vector 2604651e4cefSPeter Xu * (see vt-d spec 5.1.5.1). */ 2605651e4cefSPeter Xu if (vector != irq.vector) { 26067feb51b7SPeter Xu trace_vtd_warn_ir_vector(sid, index, vector, irq.vector); 2607651e4cefSPeter Xu } 2608dea651a9SFeng Wu 2609dea651a9SFeng Wu /* The Trigger Mode field must match the Trigger Mode in the IRTE. 2610dea651a9SFeng Wu * (see vt-d spec 5.1.5.1). */ 2611dea651a9SFeng Wu if (trigger_mode != irq.trigger_mode) { 26127feb51b7SPeter Xu trace_vtd_warn_ir_trigger(sid, index, trigger_mode, 26137feb51b7SPeter Xu irq.trigger_mode); 2614dea651a9SFeng Wu } 2615651e4cefSPeter Xu } 2616651e4cefSPeter Xu 2617651e4cefSPeter Xu /* 2618651e4cefSPeter Xu * We'd better keep the last two bits, assuming that guest OS 2619651e4cefSPeter Xu * might modify it. Keep it does not hurt after all. 2620651e4cefSPeter Xu */ 2621bc38ee10SMichael S. Tsirkin irq.msi_addr_last_bits = addr.addr.__not_care; 2622651e4cefSPeter Xu 2623651e4cefSPeter Xu /* Translate VTDIrq to MSI message */ 2624651e4cefSPeter Xu vtd_generate_msi_message(&irq, translated); 2625651e4cefSPeter Xu 2626e7a3b91fSPeter Xu out: 26277feb51b7SPeter Xu trace_vtd_ir_remap_msi(origin->address, origin->data, 2628651e4cefSPeter Xu translated->address, translated->data); 2629651e4cefSPeter Xu return 0; 2630651e4cefSPeter Xu } 2631651e4cefSPeter Xu 26328b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src, 26338b5ed7dfSPeter Xu MSIMessage *dst, uint16_t sid) 26348b5ed7dfSPeter Xu { 2635ede9c94aSPeter Xu return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu), 2636ede9c94aSPeter Xu src, dst, sid); 26378b5ed7dfSPeter Xu } 26388b5ed7dfSPeter Xu 2639651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr, 2640651e4cefSPeter Xu uint64_t *data, unsigned size, 2641651e4cefSPeter Xu MemTxAttrs attrs) 2642651e4cefSPeter Xu { 2643651e4cefSPeter Xu return MEMTX_OK; 2644651e4cefSPeter Xu } 2645651e4cefSPeter Xu 2646651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr, 2647651e4cefSPeter Xu uint64_t value, unsigned size, 2648651e4cefSPeter Xu MemTxAttrs attrs) 2649651e4cefSPeter Xu { 2650651e4cefSPeter Xu int ret = 0; 265109cd058aSMichael S. Tsirkin MSIMessage from = {}, to = {}; 2652ede9c94aSPeter Xu uint16_t sid = X86_IOMMU_SID_INVALID; 2653651e4cefSPeter Xu 2654651e4cefSPeter Xu from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST; 2655651e4cefSPeter Xu from.data = (uint32_t) value; 2656651e4cefSPeter Xu 2657ede9c94aSPeter Xu if (!attrs.unspecified) { 2658ede9c94aSPeter Xu /* We have explicit Source ID */ 2659ede9c94aSPeter Xu sid = attrs.requester_id; 2660ede9c94aSPeter Xu } 2661ede9c94aSPeter Xu 2662ede9c94aSPeter Xu ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid); 2663651e4cefSPeter Xu if (ret) { 2664651e4cefSPeter Xu /* TODO: report error */ 2665651e4cefSPeter Xu /* Drop this interrupt */ 2666651e4cefSPeter Xu return MEMTX_ERROR; 2667651e4cefSPeter Xu } 2668651e4cefSPeter Xu 266932946019SRadim Krčmář apic_get_class()->send_msi(&to); 2670651e4cefSPeter Xu 2671651e4cefSPeter Xu return MEMTX_OK; 2672651e4cefSPeter Xu } 2673651e4cefSPeter Xu 2674651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = { 2675651e4cefSPeter Xu .read_with_attrs = vtd_mem_ir_read, 2676651e4cefSPeter Xu .write_with_attrs = vtd_mem_ir_write, 2677651e4cefSPeter Xu .endianness = DEVICE_LITTLE_ENDIAN, 2678651e4cefSPeter Xu .impl = { 2679651e4cefSPeter Xu .min_access_size = 4, 2680651e4cefSPeter Xu .max_access_size = 4, 2681651e4cefSPeter Xu }, 2682651e4cefSPeter Xu .valid = { 2683651e4cefSPeter Xu .min_access_size = 4, 2684651e4cefSPeter Xu .max_access_size = 4, 2685651e4cefSPeter Xu }, 2686651e4cefSPeter Xu }; 26877df953bdSKnut Omang 26887df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) 26897df953bdSKnut Omang { 26907df953bdSKnut Omang uintptr_t key = (uintptr_t)bus; 26917df953bdSKnut Omang VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key); 26927df953bdSKnut Omang VTDAddressSpace *vtd_dev_as; 2693e0a3c8ccSJason Wang char name[128]; 26947df953bdSKnut Omang 26957df953bdSKnut Omang if (!vtd_bus) { 26962d3fc581SJason Wang uintptr_t *new_key = g_malloc(sizeof(*new_key)); 26972d3fc581SJason Wang *new_key = (uintptr_t)bus; 26987df953bdSKnut Omang /* No corresponding free() */ 269904af0e18SPeter Xu vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \ 2700bf33cc75SPeter Xu PCI_DEVFN_MAX); 27017df953bdSKnut Omang vtd_bus->bus = bus; 27022d3fc581SJason Wang g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); 27037df953bdSKnut Omang } 27047df953bdSKnut Omang 27057df953bdSKnut Omang vtd_dev_as = vtd_bus->dev_as[devfn]; 27067df953bdSKnut Omang 27077df953bdSKnut Omang if (!vtd_dev_as) { 2708e0a3c8ccSJason Wang snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn); 27097df953bdSKnut Omang vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace)); 27107df953bdSKnut Omang 27117df953bdSKnut Omang vtd_dev_as->bus = bus; 27127df953bdSKnut Omang vtd_dev_as->devfn = (uint8_t)devfn; 27137df953bdSKnut Omang vtd_dev_as->iommu_state = s; 27147df953bdSKnut Omang vtd_dev_as->context_cache_entry.context_cache_gen = 0; 2715558e0024SPeter Xu 2716558e0024SPeter Xu /* 2717558e0024SPeter Xu * Memory region relationships looks like (Address range shows 2718558e0024SPeter Xu * only lower 32 bits to make it short in length...): 2719558e0024SPeter Xu * 2720558e0024SPeter Xu * |-----------------+-------------------+----------| 2721558e0024SPeter Xu * | Name | Address range | Priority | 2722558e0024SPeter Xu * |-----------------+-------------------+----------+ 2723558e0024SPeter Xu * | vtd_root | 00000000-ffffffff | 0 | 2724558e0024SPeter Xu * | intel_iommu | 00000000-ffffffff | 1 | 2725558e0024SPeter Xu * | vtd_sys_alias | 00000000-ffffffff | 1 | 2726558e0024SPeter Xu * | intel_iommu_ir | fee00000-feefffff | 64 | 2727558e0024SPeter Xu * |-----------------+-------------------+----------| 2728558e0024SPeter Xu * 2729558e0024SPeter Xu * We enable/disable DMAR by switching enablement for 2730558e0024SPeter Xu * vtd_sys_alias and intel_iommu regions. IR region is always 2731558e0024SPeter Xu * enabled. 2732558e0024SPeter Xu */ 27331221a474SAlexey Kardashevskiy memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu), 27341221a474SAlexey Kardashevskiy TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s), 27351221a474SAlexey Kardashevskiy "intel_iommu_dmar", 2736558e0024SPeter Xu UINT64_MAX); 2737558e0024SPeter Xu memory_region_init_alias(&vtd_dev_as->sys_alias, OBJECT(s), 2738558e0024SPeter Xu "vtd_sys_alias", get_system_memory(), 2739558e0024SPeter Xu 0, memory_region_size(get_system_memory())); 2740651e4cefSPeter Xu memory_region_init_io(&vtd_dev_as->iommu_ir, OBJECT(s), 2741651e4cefSPeter Xu &vtd_mem_ir_ops, s, "intel_iommu_ir", 2742651e4cefSPeter Xu VTD_INTERRUPT_ADDR_SIZE); 2743558e0024SPeter Xu memory_region_init(&vtd_dev_as->root, OBJECT(s), 2744558e0024SPeter Xu "vtd_root", UINT64_MAX); 2745558e0024SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 2746558e0024SPeter Xu VTD_INTERRUPT_ADDR_FIRST, 2747558e0024SPeter Xu &vtd_dev_as->iommu_ir, 64); 2748558e0024SPeter Xu address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, name); 2749558e0024SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 2750558e0024SPeter Xu &vtd_dev_as->sys_alias, 1); 2751558e0024SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 27523df9d748SAlexey Kardashevskiy MEMORY_REGION(&vtd_dev_as->iommu), 27533df9d748SAlexey Kardashevskiy 1); 2754558e0024SPeter Xu vtd_switch_address_space(vtd_dev_as); 27557df953bdSKnut Omang } 27567df953bdSKnut Omang return vtd_dev_as; 27577df953bdSKnut Omang } 27587df953bdSKnut Omang 2759dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */ 2760dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) 2761dd4d607eSPeter Xu { 2762dd4d607eSPeter Xu IOMMUTLBEntry entry; 2763dd4d607eSPeter Xu hwaddr size; 2764dd4d607eSPeter Xu hwaddr start = n->start; 2765dd4d607eSPeter Xu hwaddr end = n->end; 276637f51384SPrasad Singamsetty IntelIOMMUState *s = as->iommu_state; 2767dd4d607eSPeter Xu 2768dd4d607eSPeter Xu /* 2769dd4d607eSPeter Xu * Note: all the codes in this function has a assumption that IOVA 2770dd4d607eSPeter Xu * bits are no more than VTD_MGAW bits (which is restricted by 2771dd4d607eSPeter Xu * VT-d spec), otherwise we need to consider overflow of 64 bits. 2772dd4d607eSPeter Xu */ 2773dd4d607eSPeter Xu 277437f51384SPrasad Singamsetty if (end > VTD_ADDRESS_SIZE(s->aw_bits)) { 2775dd4d607eSPeter Xu /* 2776dd4d607eSPeter Xu * Don't need to unmap regions that is bigger than the whole 2777dd4d607eSPeter Xu * VT-d supported address space size 2778dd4d607eSPeter Xu */ 277937f51384SPrasad Singamsetty end = VTD_ADDRESS_SIZE(s->aw_bits); 2780dd4d607eSPeter Xu } 2781dd4d607eSPeter Xu 2782dd4d607eSPeter Xu assert(start <= end); 2783dd4d607eSPeter Xu size = end - start; 2784dd4d607eSPeter Xu 2785dd4d607eSPeter Xu if (ctpop64(size) != 1) { 2786dd4d607eSPeter Xu /* 2787dd4d607eSPeter Xu * This size cannot format a correct mask. Let's enlarge it to 2788dd4d607eSPeter Xu * suite the minimum available mask. 2789dd4d607eSPeter Xu */ 2790dd4d607eSPeter Xu int n = 64 - clz64(size); 279137f51384SPrasad Singamsetty if (n > s->aw_bits) { 2792dd4d607eSPeter Xu /* should not happen, but in case it happens, limit it */ 279337f51384SPrasad Singamsetty n = s->aw_bits; 2794dd4d607eSPeter Xu } 2795dd4d607eSPeter Xu size = 1ULL << n; 2796dd4d607eSPeter Xu } 2797dd4d607eSPeter Xu 2798dd4d607eSPeter Xu entry.target_as = &address_space_memory; 2799dd4d607eSPeter Xu /* Adjust iova for the size */ 2800dd4d607eSPeter Xu entry.iova = n->start & ~(size - 1); 2801dd4d607eSPeter Xu /* This field is meaningless for unmap */ 2802dd4d607eSPeter Xu entry.translated_addr = 0; 2803dd4d607eSPeter Xu entry.perm = IOMMU_NONE; 2804dd4d607eSPeter Xu entry.addr_mask = size - 1; 2805dd4d607eSPeter Xu 2806dd4d607eSPeter Xu trace_vtd_as_unmap_whole(pci_bus_num(as->bus), 2807dd4d607eSPeter Xu VTD_PCI_SLOT(as->devfn), 2808dd4d607eSPeter Xu VTD_PCI_FUNC(as->devfn), 2809dd4d607eSPeter Xu entry.iova, size); 2810dd4d607eSPeter Xu 2811dd4d607eSPeter Xu memory_region_notify_one(n, &entry); 2812dd4d607eSPeter Xu } 2813dd4d607eSPeter Xu 2814dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s) 2815dd4d607eSPeter Xu { 2816dd4d607eSPeter Xu IntelIOMMUNotifierNode *node; 2817dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 2818dd4d607eSPeter Xu IOMMUNotifier *n; 2819dd4d607eSPeter Xu 2820dd4d607eSPeter Xu QLIST_FOREACH(node, &s->notifiers_list, next) { 2821dd4d607eSPeter Xu vtd_as = node->vtd_as; 2822dd4d607eSPeter Xu IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) { 2823dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 2824dd4d607eSPeter Xu } 2825dd4d607eSPeter Xu } 2826dd4d607eSPeter Xu } 2827dd4d607eSPeter Xu 2828f06a696dSPeter Xu static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private) 2829f06a696dSPeter Xu { 2830f06a696dSPeter Xu memory_region_notify_one((IOMMUNotifier *)private, entry); 2831f06a696dSPeter Xu return 0; 2832f06a696dSPeter Xu } 2833f06a696dSPeter Xu 28343df9d748SAlexey Kardashevskiy static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) 2835f06a696dSPeter Xu { 28363df9d748SAlexey Kardashevskiy VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu); 2837f06a696dSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 2838f06a696dSPeter Xu uint8_t bus_n = pci_bus_num(vtd_as->bus); 2839f06a696dSPeter Xu VTDContextEntry ce; 2840f06a696dSPeter Xu 2841f06a696dSPeter Xu /* 2842dd4d607eSPeter Xu * The replay can be triggered by either a invalidation or a newly 2843dd4d607eSPeter Xu * created entry. No matter what, we release existing mappings 2844dd4d607eSPeter Xu * (it means flushing caches for UNMAP-only registers). 2845f06a696dSPeter Xu */ 2846dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 2847dd4d607eSPeter Xu 2848dd4d607eSPeter Xu if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) { 2849f06a696dSPeter Xu trace_vtd_replay_ce_valid(bus_n, PCI_SLOT(vtd_as->devfn), 2850f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn), 2851f06a696dSPeter Xu VTD_CONTEXT_ENTRY_DID(ce.hi), 2852f06a696dSPeter Xu ce.hi, ce.lo); 285337f51384SPrasad Singamsetty vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false, 285437f51384SPrasad Singamsetty s->aw_bits); 2855f06a696dSPeter Xu } else { 2856f06a696dSPeter Xu trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), 2857f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn)); 2858f06a696dSPeter Xu } 2859f06a696dSPeter Xu 2860f06a696dSPeter Xu return; 2861f06a696dSPeter Xu } 2862f06a696dSPeter Xu 28631da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay 28641da12ec4SLe Tan * attention when adding new initialization stuff. 28651da12ec4SLe Tan */ 28661da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s) 28671da12ec4SLe Tan { 2868d54bd7f8SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 2869d54bd7f8SPeter Xu 28701da12ec4SLe Tan memset(s->csr, 0, DMAR_REG_SIZE); 28711da12ec4SLe Tan memset(s->wmask, 0, DMAR_REG_SIZE); 28721da12ec4SLe Tan memset(s->w1cmask, 0, DMAR_REG_SIZE); 28731da12ec4SLe Tan memset(s->womask, 0, DMAR_REG_SIZE); 28741da12ec4SLe Tan 28751da12ec4SLe Tan s->root = 0; 28761da12ec4SLe Tan s->root_extended = false; 28771da12ec4SLe Tan s->dmar_enabled = false; 28781da12ec4SLe Tan s->iq_head = 0; 28791da12ec4SLe Tan s->iq_tail = 0; 28801da12ec4SLe Tan s->iq = 0; 28811da12ec4SLe Tan s->iq_size = 0; 28821da12ec4SLe Tan s->qi_enabled = false; 28831da12ec4SLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 28841da12ec4SLe Tan s->next_frcd_reg = 0; 288592e5d85eSPrasad Singamsetty s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | 288692e5d85eSPrasad Singamsetty VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS | 288737f51384SPrasad Singamsetty VTD_CAP_SAGAW_39bit | VTD_CAP_MGAW(s->aw_bits); 288837f51384SPrasad Singamsetty if (s->aw_bits == VTD_HOST_AW_48BIT) { 288937f51384SPrasad Singamsetty s->cap |= VTD_CAP_SAGAW_48bit; 289037f51384SPrasad Singamsetty } 2891ed7b8fbcSLe Tan s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO; 28921da12ec4SLe Tan 289392e5d85eSPrasad Singamsetty /* 289492e5d85eSPrasad Singamsetty * Rsvd field masks for spte 289592e5d85eSPrasad Singamsetty */ 289692e5d85eSPrasad Singamsetty vtd_paging_entry_rsvd_field[0] = ~0ULL; 289737f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits); 289837f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits); 289937f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits); 290037f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits); 290137f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[5] = VTD_SPTE_LPAGE_L1_RSVD_MASK(s->aw_bits); 290237f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[6] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits); 290337f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[7] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits); 290437f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[8] = VTD_SPTE_LPAGE_L4_RSVD_MASK(s->aw_bits); 290592e5d85eSPrasad Singamsetty 2906d54bd7f8SPeter Xu if (x86_iommu->intr_supported) { 2907e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV; 2908e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON) { 2909e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_EIM; 2910e6b6af05SRadim Krčmář } 2911e6b6af05SRadim Krčmář assert(s->intr_eim != ON_OFF_AUTO_AUTO); 2912d54bd7f8SPeter Xu } 2913d54bd7f8SPeter Xu 2914554f5e16SJason Wang if (x86_iommu->dt_supported) { 2915554f5e16SJason Wang s->ecap |= VTD_ECAP_DT; 2916554f5e16SJason Wang } 2917554f5e16SJason Wang 2918dbaabb25SPeter Xu if (x86_iommu->pt_supported) { 2919dbaabb25SPeter Xu s->ecap |= VTD_ECAP_PT; 2920dbaabb25SPeter Xu } 2921dbaabb25SPeter Xu 29223b40f0e5SAviv Ben-David if (s->caching_mode) { 29233b40f0e5SAviv Ben-David s->cap |= VTD_CAP_CM; 29243b40f0e5SAviv Ben-David } 29253b40f0e5SAviv Ben-David 2926d92fa2dcSLe Tan vtd_reset_context_cache(s); 2927b5a280c0SLe Tan vtd_reset_iotlb(s); 2928d92fa2dcSLe Tan 29291da12ec4SLe Tan /* Define registers with default values and bit semantics */ 29301da12ec4SLe Tan vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0); 29311da12ec4SLe Tan vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0); 29321da12ec4SLe Tan vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0); 29331da12ec4SLe Tan vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0); 29341da12ec4SLe Tan vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL); 29351da12ec4SLe Tan vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0); 29361da12ec4SLe Tan vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0); 29371da12ec4SLe Tan vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0); 29381da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL); 29391da12ec4SLe Tan 29401da12ec4SLe Tan /* Advanced Fault Logging not supported */ 29411da12ec4SLe Tan vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL); 29421da12ec4SLe Tan vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0); 29431da12ec4SLe Tan vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0); 29441da12ec4SLe Tan vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0); 29451da12ec4SLe Tan 29461da12ec4SLe Tan /* Treated as RsvdZ when EIM in ECAP_REG is not supported 29471da12ec4SLe Tan * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0); 29481da12ec4SLe Tan */ 29491da12ec4SLe Tan vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0); 29501da12ec4SLe Tan 29511da12ec4SLe Tan /* Treated as RO for implementations that PLMR and PHMR fields reported 29521da12ec4SLe Tan * as Clear in the CAP_REG. 29531da12ec4SLe Tan * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0); 29541da12ec4SLe Tan */ 29551da12ec4SLe Tan vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0); 29561da12ec4SLe Tan 2957ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0); 2958ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0); 2959ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0); 2960ed7b8fbcSLe Tan vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL); 2961ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0); 2962ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0); 2963ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0); 2964ed7b8fbcSLe Tan /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */ 2965ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0); 2966ed7b8fbcSLe Tan 29671da12ec4SLe Tan /* IOTLB registers */ 29681da12ec4SLe Tan vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0); 29691da12ec4SLe Tan vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0); 29701da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL); 29711da12ec4SLe Tan 29721da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 29731da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0); 29741da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL); 2975a5861439SPeter Xu 2976a5861439SPeter Xu /* 297728589311SJan Kiszka * Interrupt remapping registers. 2978a5861439SPeter Xu */ 297928589311SJan Kiszka vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0); 29801da12ec4SLe Tan } 29811da12ec4SLe Tan 29821da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use 29831da12ec4SLe Tan * the address space they got at first (won't ask the bus again). 29841da12ec4SLe Tan */ 29851da12ec4SLe Tan static void vtd_reset(DeviceState *dev) 29861da12ec4SLe Tan { 29871da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 29881da12ec4SLe Tan 29891da12ec4SLe Tan vtd_init(s); 2990dd4d607eSPeter Xu 2991dd4d607eSPeter Xu /* 2992dd4d607eSPeter Xu * When device reset, throw away all mappings and external caches 2993dd4d607eSPeter Xu */ 2994dd4d607eSPeter Xu vtd_address_space_unmap_all(s); 29951da12ec4SLe Tan } 29961da12ec4SLe Tan 2997621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) 2998621d983aSMarcel Apfelbaum { 2999621d983aSMarcel Apfelbaum IntelIOMMUState *s = opaque; 3000621d983aSMarcel Apfelbaum VTDAddressSpace *vtd_as; 3001621d983aSMarcel Apfelbaum 3002bf33cc75SPeter Xu assert(0 <= devfn && devfn < PCI_DEVFN_MAX); 3003621d983aSMarcel Apfelbaum 3004621d983aSMarcel Apfelbaum vtd_as = vtd_find_add_as(s, bus, devfn); 3005621d983aSMarcel Apfelbaum return &vtd_as->as; 3006621d983aSMarcel Apfelbaum } 3007621d983aSMarcel Apfelbaum 3008e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) 30096333e93cSRadim Krčmář { 3010e6b6af05SRadim Krčmář X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 3011e6b6af05SRadim Krčmář 30126333e93cSRadim Krčmář /* Currently Intel IOMMU IR only support "kernel-irqchip={off|split}" */ 30136333e93cSRadim Krčmář if (x86_iommu->intr_supported && kvm_irqchip_in_kernel() && 30146333e93cSRadim Krčmář !kvm_irqchip_is_split()) { 30156333e93cSRadim Krčmář error_setg(errp, "Intel Interrupt Remapping cannot work with " 30166333e93cSRadim Krčmář "kernel-irqchip=on, please use 'split|off'."); 30176333e93cSRadim Krčmář return false; 30186333e93cSRadim Krčmář } 3019e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu->intr_supported) { 3020e6b6af05SRadim Krčmář error_setg(errp, "eim=on cannot be selected without intremap=on"); 3021e6b6af05SRadim Krčmář return false; 3022e6b6af05SRadim Krčmář } 3023e6b6af05SRadim Krčmář 3024e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_AUTO) { 3025fb506e70SRadim Krčmář s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim) 3026fb506e70SRadim Krčmář && x86_iommu->intr_supported ? 3027e6b6af05SRadim Krčmář ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 3028e6b6af05SRadim Krčmář } 3029fb506e70SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) { 3030fb506e70SRadim Krčmář if (!kvm_irqchip_in_kernel()) { 3031fb506e70SRadim Krčmář error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split"); 3032fb506e70SRadim Krčmář return false; 3033fb506e70SRadim Krčmář } 3034fb506e70SRadim Krčmář if (!kvm_enable_x2apic()) { 3035fb506e70SRadim Krčmář error_setg(errp, "eim=on requires support on the KVM side" 3036fb506e70SRadim Krčmář "(X2APIC_API, first shipped in v4.7)"); 3037fb506e70SRadim Krčmář return false; 3038fb506e70SRadim Krčmář } 3039fb506e70SRadim Krčmář } 3040e6b6af05SRadim Krčmář 304137f51384SPrasad Singamsetty /* Currently only address widths supported are 39 and 48 bits */ 304237f51384SPrasad Singamsetty if ((s->aw_bits != VTD_HOST_AW_39BIT) && 304337f51384SPrasad Singamsetty (s->aw_bits != VTD_HOST_AW_48BIT)) { 304437f51384SPrasad Singamsetty error_setg(errp, "Supported values for x-aw-bits are: %d, %d", 304537f51384SPrasad Singamsetty VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT); 304637f51384SPrasad Singamsetty return false; 304737f51384SPrasad Singamsetty } 304837f51384SPrasad Singamsetty 30496333e93cSRadim Krčmář return true; 30506333e93cSRadim Krčmář } 30516333e93cSRadim Krčmář 30521da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp) 30531da12ec4SLe Tan { 3054ef0e8fc7SEduardo Habkost MachineState *ms = MACHINE(qdev_get_machine()); 3055*29396ed9SMohammed Gamal PCMachineState *pcms = PC_MACHINE(ms); 3056*29396ed9SMohammed Gamal PCIBus *bus = pcms->bus; 30571da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 30584684a204SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev); 30591da12ec4SLe Tan 3060fb9f5926SDavid Kiarie x86_iommu->type = TYPE_INTEL; 30616333e93cSRadim Krčmář 3062e6b6af05SRadim Krčmář if (!vtd_decide_config(s, errp)) { 30636333e93cSRadim Krčmář return; 30646333e93cSRadim Krčmář } 30656333e93cSRadim Krčmář 3066dd4d607eSPeter Xu QLIST_INIT(&s->notifiers_list); 30677df953bdSKnut Omang memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num)); 30681da12ec4SLe Tan memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s, 30691da12ec4SLe Tan "intel_iommu", DMAR_REG_SIZE); 30701da12ec4SLe Tan sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem); 3071b5a280c0SLe Tan /* No corresponding destroy */ 3072b5a280c0SLe Tan s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 3073b5a280c0SLe Tan g_free, g_free); 30747df953bdSKnut Omang s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 30757df953bdSKnut Omang g_free, g_free); 30761da12ec4SLe Tan vtd_init(s); 3077621d983aSMarcel Apfelbaum sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); 3078621d983aSMarcel Apfelbaum pci_setup_iommu(bus, vtd_host_dma_iommu, dev); 3079cb135f59SPeter Xu /* Pseudo address space under root PCI bus. */ 3080cb135f59SPeter Xu pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC); 30811da12ec4SLe Tan } 30821da12ec4SLe Tan 30831da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data) 30841da12ec4SLe Tan { 30851da12ec4SLe Tan DeviceClass *dc = DEVICE_CLASS(klass); 30861c7955c4SPeter Xu X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass); 30871da12ec4SLe Tan 30881da12ec4SLe Tan dc->reset = vtd_reset; 30891da12ec4SLe Tan dc->vmsd = &vtd_vmstate; 30901da12ec4SLe Tan dc->props = vtd_properties; 3091621d983aSMarcel Apfelbaum dc->hotpluggable = false; 30921c7955c4SPeter Xu x86_class->realize = vtd_realize; 30938b5ed7dfSPeter Xu x86_class->int_remap = vtd_int_remap; 30948ab5700cSEduardo Habkost /* Supported by the pc-q35-* machine types */ 3095e4f4fb1eSEduardo Habkost dc->user_creatable = true; 30961da12ec4SLe Tan } 30971da12ec4SLe Tan 30981da12ec4SLe Tan static const TypeInfo vtd_info = { 30991da12ec4SLe Tan .name = TYPE_INTEL_IOMMU_DEVICE, 31001c7955c4SPeter Xu .parent = TYPE_X86_IOMMU_DEVICE, 31011da12ec4SLe Tan .instance_size = sizeof(IntelIOMMUState), 31021da12ec4SLe Tan .class_init = vtd_class_init, 31031da12ec4SLe Tan }; 31041da12ec4SLe Tan 31051221a474SAlexey Kardashevskiy static void vtd_iommu_memory_region_class_init(ObjectClass *klass, 31061221a474SAlexey Kardashevskiy void *data) 31071221a474SAlexey Kardashevskiy { 31081221a474SAlexey Kardashevskiy IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 31091221a474SAlexey Kardashevskiy 31101221a474SAlexey Kardashevskiy imrc->translate = vtd_iommu_translate; 31111221a474SAlexey Kardashevskiy imrc->notify_flag_changed = vtd_iommu_notify_flag_changed; 31121221a474SAlexey Kardashevskiy imrc->replay = vtd_iommu_replay; 31131221a474SAlexey Kardashevskiy } 31141221a474SAlexey Kardashevskiy 31151221a474SAlexey Kardashevskiy static const TypeInfo vtd_iommu_memory_region_info = { 31161221a474SAlexey Kardashevskiy .parent = TYPE_IOMMU_MEMORY_REGION, 31171221a474SAlexey Kardashevskiy .name = TYPE_INTEL_IOMMU_MEMORY_REGION, 31181221a474SAlexey Kardashevskiy .class_init = vtd_iommu_memory_region_class_init, 31191221a474SAlexey Kardashevskiy }; 31201221a474SAlexey Kardashevskiy 31211da12ec4SLe Tan static void vtd_register_types(void) 31221da12ec4SLe Tan { 31231da12ec4SLe Tan type_register_static(&vtd_info); 31241221a474SAlexey Kardashevskiy type_register_static(&vtd_iommu_memory_region_info); 31251da12ec4SLe Tan } 31261da12ec4SLe Tan 31271da12ec4SLe Tan type_init(vtd_register_types) 3128