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 40fb43cf73SLiu, Yi L /* context entry operations */ 41fb43cf73SLiu, Yi L #define VTD_CE_GET_RID2PASID(ce) \ 42fb43cf73SLiu, Yi L ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK) 43fb43cf73SLiu, Yi L #define VTD_CE_GET_PASID_DIR_TABLE(ce) \ 44fb43cf73SLiu, Yi L ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK) 45fb43cf73SLiu, Yi L 46fb43cf73SLiu, Yi L /* pe operations */ 47fb43cf73SLiu, Yi L #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT) 48fb43cf73SLiu, Yi L #define VTD_PE_GET_LEVEL(pe) (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW)) 49fb43cf73SLiu, Yi L #define VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write) {\ 50fb43cf73SLiu, Yi L if (ret_fr) { \ 51fb43cf73SLiu, Yi L ret_fr = -ret_fr; \ 52fb43cf73SLiu, Yi L if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) { \ 53fb43cf73SLiu, Yi L trace_vtd_fault_disabled(); \ 54fb43cf73SLiu, Yi L } else { \ 55fb43cf73SLiu, Yi L vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write); \ 56fb43cf73SLiu, Yi L } \ 57fb43cf73SLiu, Yi L goto error; \ 58fb43cf73SLiu, Yi L } \ 59fb43cf73SLiu, Yi L } 60fb43cf73SLiu, Yi L 612cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s); 62c28b535dSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n); 632cc9ddccSPeter Xu 641da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val, 651da12ec4SLe Tan uint64_t wmask, uint64_t w1cmask) 661da12ec4SLe Tan { 671da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 681da12ec4SLe Tan stq_le_p(&s->wmask[addr], wmask); 691da12ec4SLe Tan stq_le_p(&s->w1cmask[addr], w1cmask); 701da12ec4SLe Tan } 711da12ec4SLe Tan 721da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask) 731da12ec4SLe Tan { 741da12ec4SLe Tan stq_le_p(&s->womask[addr], mask); 751da12ec4SLe Tan } 761da12ec4SLe Tan 771da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val, 781da12ec4SLe Tan uint32_t wmask, uint32_t w1cmask) 791da12ec4SLe Tan { 801da12ec4SLe Tan stl_le_p(&s->csr[addr], val); 811da12ec4SLe Tan stl_le_p(&s->wmask[addr], wmask); 821da12ec4SLe Tan stl_le_p(&s->w1cmask[addr], w1cmask); 831da12ec4SLe Tan } 841da12ec4SLe Tan 851da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask) 861da12ec4SLe Tan { 871da12ec4SLe Tan stl_le_p(&s->womask[addr], mask); 881da12ec4SLe Tan } 891da12ec4SLe Tan 901da12ec4SLe Tan /* "External" get/set operations */ 911da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val) 921da12ec4SLe Tan { 931da12ec4SLe Tan uint64_t oldval = ldq_le_p(&s->csr[addr]); 941da12ec4SLe Tan uint64_t wmask = ldq_le_p(&s->wmask[addr]); 951da12ec4SLe Tan uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]); 961da12ec4SLe Tan stq_le_p(&s->csr[addr], 971da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 981da12ec4SLe Tan } 991da12ec4SLe Tan 1001da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val) 1011da12ec4SLe Tan { 1021da12ec4SLe Tan uint32_t oldval = ldl_le_p(&s->csr[addr]); 1031da12ec4SLe Tan uint32_t wmask = ldl_le_p(&s->wmask[addr]); 1041da12ec4SLe Tan uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]); 1051da12ec4SLe Tan stl_le_p(&s->csr[addr], 1061da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 1071da12ec4SLe Tan } 1081da12ec4SLe Tan 1091da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr) 1101da12ec4SLe Tan { 1111da12ec4SLe Tan uint64_t val = ldq_le_p(&s->csr[addr]); 1121da12ec4SLe Tan uint64_t womask = ldq_le_p(&s->womask[addr]); 1131da12ec4SLe Tan return val & ~womask; 1141da12ec4SLe Tan } 1151da12ec4SLe Tan 1161da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr) 1171da12ec4SLe Tan { 1181da12ec4SLe Tan uint32_t val = ldl_le_p(&s->csr[addr]); 1191da12ec4SLe Tan uint32_t womask = ldl_le_p(&s->womask[addr]); 1201da12ec4SLe Tan return val & ~womask; 1211da12ec4SLe Tan } 1221da12ec4SLe Tan 1231da12ec4SLe Tan /* "Internal" get/set operations */ 1241da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr) 1251da12ec4SLe Tan { 1261da12ec4SLe Tan return ldq_le_p(&s->csr[addr]); 1271da12ec4SLe Tan } 1281da12ec4SLe Tan 1291da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr) 1301da12ec4SLe Tan { 1311da12ec4SLe Tan return ldl_le_p(&s->csr[addr]); 1321da12ec4SLe Tan } 1331da12ec4SLe Tan 1341da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val) 1351da12ec4SLe Tan { 1361da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 1371da12ec4SLe Tan } 1381da12ec4SLe Tan 1391da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr, 1401da12ec4SLe Tan uint32_t clear, uint32_t mask) 1411da12ec4SLe Tan { 1421da12ec4SLe Tan uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask; 1431da12ec4SLe Tan stl_le_p(&s->csr[addr], new_val); 1441da12ec4SLe Tan return new_val; 1451da12ec4SLe Tan } 1461da12ec4SLe Tan 1471da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, 1481da12ec4SLe Tan uint64_t clear, uint64_t mask) 1491da12ec4SLe Tan { 1501da12ec4SLe Tan uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask; 1511da12ec4SLe Tan stq_le_p(&s->csr[addr], new_val); 1521da12ec4SLe Tan return new_val; 1531da12ec4SLe Tan } 1541da12ec4SLe Tan 1551d9efa73SPeter Xu static inline void vtd_iommu_lock(IntelIOMMUState *s) 1561d9efa73SPeter Xu { 1571d9efa73SPeter Xu qemu_mutex_lock(&s->iommu_lock); 1581d9efa73SPeter Xu } 1591d9efa73SPeter Xu 1601d9efa73SPeter Xu static inline void vtd_iommu_unlock(IntelIOMMUState *s) 1611d9efa73SPeter Xu { 1621d9efa73SPeter Xu qemu_mutex_unlock(&s->iommu_lock); 1631d9efa73SPeter Xu } 1641d9efa73SPeter Xu 1652811af3bSPeter Xu static void vtd_update_scalable_state(IntelIOMMUState *s) 1662811af3bSPeter Xu { 1672811af3bSPeter Xu uint64_t val = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 1682811af3bSPeter Xu 1692811af3bSPeter Xu if (s->scalable_mode) { 1702811af3bSPeter Xu s->root_scalable = val & VTD_RTADDR_SMT; 1712811af3bSPeter Xu } 1722811af3bSPeter Xu } 1732811af3bSPeter Xu 1744f8a62a9SPeter Xu /* Whether the address space needs to notify new mappings */ 1754f8a62a9SPeter Xu static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as) 1764f8a62a9SPeter Xu { 1774f8a62a9SPeter Xu return as->notifier_flags & IOMMU_NOTIFIER_MAP; 1784f8a62a9SPeter Xu } 1794f8a62a9SPeter Xu 180b5a280c0SLe Tan /* GHashTable functions */ 181b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) 182b5a280c0SLe Tan { 183b5a280c0SLe Tan return *((const uint64_t *)v1) == *((const uint64_t *)v2); 184b5a280c0SLe Tan } 185b5a280c0SLe Tan 186b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v) 187b5a280c0SLe Tan { 188b5a280c0SLe Tan return (guint)*(const uint64_t *)v; 189b5a280c0SLe Tan } 190b5a280c0SLe Tan 191b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value, 192b5a280c0SLe Tan gpointer user_data) 193b5a280c0SLe Tan { 194b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 195b5a280c0SLe Tan uint16_t domain_id = *(uint16_t *)user_data; 196b5a280c0SLe Tan return entry->domain_id == domain_id; 197b5a280c0SLe Tan } 198b5a280c0SLe Tan 199d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */ 200d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level) 201d66b969bSJason Wang { 2027e58326aSPeter Xu assert(level != 0); 203d66b969bSJason Wang return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS; 204d66b969bSJason Wang } 205d66b969bSJason Wang 206d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level) 207d66b969bSJason Wang { 208d66b969bSJason Wang return ~((1ULL << vtd_slpt_level_shift(level)) - 1); 209d66b969bSJason Wang } 210d66b969bSJason Wang 211b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value, 212b5a280c0SLe Tan gpointer user_data) 213b5a280c0SLe Tan { 214b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 215b5a280c0SLe Tan VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data; 216d66b969bSJason Wang uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask; 217d66b969bSJason Wang uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K; 218b5a280c0SLe Tan return (entry->domain_id == info->domain_id) && 219d66b969bSJason Wang (((entry->gfn & info->mask) == gfn) || 220d66b969bSJason Wang (entry->gfn == gfn_tlb)); 221b5a280c0SLe Tan } 222b5a280c0SLe Tan 223d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of 2241d9efa73SPeter Xu * IntelIOMMUState to 1. Must be called with IOMMU lock held. 225d92fa2dcSLe Tan */ 2261d9efa73SPeter Xu static void vtd_reset_context_cache_locked(IntelIOMMUState *s) 227d92fa2dcSLe Tan { 228d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 2297df953bdSKnut Omang VTDBus *vtd_bus; 2307df953bdSKnut Omang GHashTableIter bus_it; 231d92fa2dcSLe Tan uint32_t devfn_it; 232d92fa2dcSLe Tan 2337feb51b7SPeter Xu trace_vtd_context_cache_reset(); 2347feb51b7SPeter Xu 2357df953bdSKnut Omang g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr); 2367df953bdSKnut Omang 2377df953bdSKnut Omang while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) { 238bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 2397df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 240d92fa2dcSLe Tan if (!vtd_as) { 241d92fa2dcSLe Tan continue; 242d92fa2dcSLe Tan } 243d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 244d92fa2dcSLe Tan } 245d92fa2dcSLe Tan } 246d92fa2dcSLe Tan s->context_cache_gen = 1; 247d92fa2dcSLe Tan } 248d92fa2dcSLe Tan 2491d9efa73SPeter Xu /* Must be called with IOMMU lock held. */ 2501d9efa73SPeter Xu static void vtd_reset_iotlb_locked(IntelIOMMUState *s) 251b5a280c0SLe Tan { 252b5a280c0SLe Tan assert(s->iotlb); 253b5a280c0SLe Tan g_hash_table_remove_all(s->iotlb); 254b5a280c0SLe Tan } 255b5a280c0SLe Tan 2561d9efa73SPeter Xu static void vtd_reset_iotlb(IntelIOMMUState *s) 2571d9efa73SPeter Xu { 2581d9efa73SPeter Xu vtd_iommu_lock(s); 2591d9efa73SPeter Xu vtd_reset_iotlb_locked(s); 2601d9efa73SPeter Xu vtd_iommu_unlock(s); 2611d9efa73SPeter Xu } 2621d9efa73SPeter Xu 26306aba4caSPeter Xu static void vtd_reset_caches(IntelIOMMUState *s) 26406aba4caSPeter Xu { 26506aba4caSPeter Xu vtd_iommu_lock(s); 26606aba4caSPeter Xu vtd_reset_iotlb_locked(s); 26706aba4caSPeter Xu vtd_reset_context_cache_locked(s); 26806aba4caSPeter Xu vtd_iommu_unlock(s); 26906aba4caSPeter Xu } 27006aba4caSPeter Xu 271bacabb0aSJason Wang static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id, 272d66b969bSJason Wang uint32_t level) 273d66b969bSJason Wang { 274d66b969bSJason Wang return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) | 275d66b969bSJason Wang ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT); 276d66b969bSJason Wang } 277d66b969bSJason Wang 278d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level) 279d66b969bSJason Wang { 280d66b969bSJason Wang return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K; 281d66b969bSJason Wang } 282d66b969bSJason Wang 2831d9efa73SPeter Xu /* Must be called with IOMMU lock held */ 284b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id, 285b5a280c0SLe Tan hwaddr addr) 286b5a280c0SLe Tan { 287d66b969bSJason Wang VTDIOTLBEntry *entry; 288b5a280c0SLe Tan uint64_t key; 289d66b969bSJason Wang int level; 290b5a280c0SLe Tan 291d66b969bSJason Wang for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) { 292d66b969bSJason Wang key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level), 293d66b969bSJason Wang source_id, level); 294d66b969bSJason Wang entry = g_hash_table_lookup(s->iotlb, &key); 295d66b969bSJason Wang if (entry) { 296d66b969bSJason Wang goto out; 297d66b969bSJason Wang } 298d66b969bSJason Wang } 299b5a280c0SLe Tan 300d66b969bSJason Wang out: 301d66b969bSJason Wang return entry; 302b5a280c0SLe Tan } 303b5a280c0SLe Tan 3041d9efa73SPeter Xu /* Must be with IOMMU lock held */ 305b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, 306b5a280c0SLe Tan uint16_t domain_id, hwaddr addr, uint64_t slpte, 30707f7b733SPeter Xu uint8_t access_flags, uint32_t level) 308b5a280c0SLe Tan { 309b5a280c0SLe Tan VTDIOTLBEntry *entry = g_malloc(sizeof(*entry)); 310b5a280c0SLe Tan uint64_t *key = g_malloc(sizeof(*key)); 311d66b969bSJason Wang uint64_t gfn = vtd_get_iotlb_gfn(addr, level); 312b5a280c0SLe Tan 3136c441e1dSPeter Xu trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); 314b5a280c0SLe Tan if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) { 3156c441e1dSPeter Xu trace_vtd_iotlb_reset("iotlb exceeds size limit"); 3161d9efa73SPeter Xu vtd_reset_iotlb_locked(s); 317b5a280c0SLe Tan } 318b5a280c0SLe Tan 319b5a280c0SLe Tan entry->gfn = gfn; 320b5a280c0SLe Tan entry->domain_id = domain_id; 321b5a280c0SLe Tan entry->slpte = slpte; 32207f7b733SPeter Xu entry->access_flags = access_flags; 323d66b969bSJason Wang entry->mask = vtd_slpt_level_page_mask(level); 324d66b969bSJason Wang *key = vtd_get_iotlb_key(gfn, source_id, level); 325b5a280c0SLe Tan g_hash_table_replace(s->iotlb, key, entry); 326b5a280c0SLe Tan } 327b5a280c0SLe Tan 3281da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an 3291da12ec4SLe Tan * interrupt via MSI. 3301da12ec4SLe Tan */ 3311da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg, 3321da12ec4SLe Tan hwaddr mesg_data_reg) 3331da12ec4SLe Tan { 33432946019SRadim Krčmář MSIMessage msi; 3351da12ec4SLe Tan 3361da12ec4SLe Tan assert(mesg_data_reg < DMAR_REG_SIZE); 3371da12ec4SLe Tan assert(mesg_addr_reg < DMAR_REG_SIZE); 3381da12ec4SLe Tan 33932946019SRadim Krčmář msi.address = vtd_get_long_raw(s, mesg_addr_reg); 34032946019SRadim Krčmář msi.data = vtd_get_long_raw(s, mesg_data_reg); 3411da12ec4SLe Tan 3427feb51b7SPeter Xu trace_vtd_irq_generate(msi.address, msi.data); 3437feb51b7SPeter Xu 34432946019SRadim Krčmář apic_get_class()->send_msi(&msi); 3451da12ec4SLe Tan } 3461da12ec4SLe Tan 3471da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met. 3481da12ec4SLe Tan * Notice that the value of FSTS_REG being passed to it should be the one 3491da12ec4SLe Tan * before any update. 3501da12ec4SLe Tan */ 3511da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts) 3521da12ec4SLe Tan { 3531da12ec4SLe Tan if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO || 3541da12ec4SLe Tan pre_fsts & VTD_FSTS_IQE) { 3551376211fSPeter Xu error_report_once("There are previous interrupt conditions " 3567feb51b7SPeter Xu "to be serviced by software, fault event " 3571376211fSPeter Xu "is not generated"); 3581da12ec4SLe Tan return; 3591da12ec4SLe Tan } 3601da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP); 3611da12ec4SLe Tan if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) { 3621376211fSPeter Xu error_report_once("Interrupt Mask set, irq is not generated"); 3631da12ec4SLe Tan } else { 3641da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 3651da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 3661da12ec4SLe Tan } 3671da12ec4SLe Tan } 3681da12ec4SLe Tan 3691da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by 3701da12ec4SLe Tan * @index is Set. 3711da12ec4SLe Tan */ 3721da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index) 3731da12ec4SLe Tan { 3741da12ec4SLe Tan /* Each reg is 128-bit */ 3751da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 3761da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 3771da12ec4SLe Tan 3781da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 3791da12ec4SLe Tan 3801da12ec4SLe Tan return vtd_get_quad_raw(s, addr) & VTD_FRCD_F; 3811da12ec4SLe Tan } 3821da12ec4SLe Tan 3831da12ec4SLe Tan /* Update the PPF field of Fault Status Register. 3841da12ec4SLe Tan * Should be called whenever change the F field of any fault recording 3851da12ec4SLe Tan * registers. 3861da12ec4SLe Tan */ 3871da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s) 3881da12ec4SLe Tan { 3891da12ec4SLe Tan uint32_t i; 3901da12ec4SLe Tan uint32_t ppf_mask = 0; 3911da12ec4SLe Tan 3921da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 3931da12ec4SLe Tan if (vtd_is_frcd_set(s, i)) { 3941da12ec4SLe Tan ppf_mask = VTD_FSTS_PPF; 3951da12ec4SLe Tan break; 3961da12ec4SLe Tan } 3971da12ec4SLe Tan } 3981da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask); 3997feb51b7SPeter Xu trace_vtd_fsts_ppf(!!ppf_mask); 4001da12ec4SLe Tan } 4011da12ec4SLe Tan 4021da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index) 4031da12ec4SLe Tan { 4041da12ec4SLe Tan /* Each reg is 128-bit */ 4051da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 4061da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 4071da12ec4SLe Tan 4081da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 4091da12ec4SLe Tan 4101da12ec4SLe Tan vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F); 4111da12ec4SLe Tan vtd_update_fsts_ppf(s); 4121da12ec4SLe Tan } 4131da12ec4SLe Tan 4141da12ec4SLe Tan /* Must not update F field now, should be done later */ 4151da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index, 4161da12ec4SLe Tan uint16_t source_id, hwaddr addr, 4171da12ec4SLe Tan VTDFaultReason fault, bool is_write) 4181da12ec4SLe Tan { 4191da12ec4SLe Tan uint64_t hi = 0, lo; 4201da12ec4SLe Tan hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 4211da12ec4SLe Tan 4221da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 4231da12ec4SLe Tan 4241da12ec4SLe Tan lo = VTD_FRCD_FI(addr); 4251da12ec4SLe Tan hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault); 4261da12ec4SLe Tan if (!is_write) { 4271da12ec4SLe Tan hi |= VTD_FRCD_T; 4281da12ec4SLe Tan } 4291da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr, lo); 4301da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr + 8, hi); 4317feb51b7SPeter Xu 4327feb51b7SPeter Xu trace_vtd_frr_new(index, hi, lo); 4331da12ec4SLe Tan } 4341da12ec4SLe Tan 4351da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */ 4361da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id) 4371da12ec4SLe Tan { 4381da12ec4SLe Tan uint32_t i; 4391da12ec4SLe Tan uint64_t frcd_reg; 4401da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */ 4411da12ec4SLe Tan 4421da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 4431da12ec4SLe Tan frcd_reg = vtd_get_quad_raw(s, addr); 4441da12ec4SLe Tan if ((frcd_reg & VTD_FRCD_F) && 4451da12ec4SLe Tan ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) { 4461da12ec4SLe Tan return true; 4471da12ec4SLe Tan } 4481da12ec4SLe Tan addr += 16; /* 128-bit for each */ 4491da12ec4SLe Tan } 4501da12ec4SLe Tan return false; 4511da12ec4SLe Tan } 4521da12ec4SLe Tan 4531da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */ 4541da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id, 4551da12ec4SLe Tan hwaddr addr, VTDFaultReason fault, 4561da12ec4SLe Tan bool is_write) 4571da12ec4SLe Tan { 4581da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 4591da12ec4SLe Tan 4601da12ec4SLe Tan assert(fault < VTD_FR_MAX); 4611da12ec4SLe Tan 4621da12ec4SLe Tan if (fault == VTD_FR_RESERVED_ERR) { 4631da12ec4SLe Tan /* This is not a normal fault reason case. Drop it. */ 4641da12ec4SLe Tan return; 4651da12ec4SLe Tan } 4667feb51b7SPeter Xu 4677feb51b7SPeter Xu trace_vtd_dmar_fault(source_id, fault, addr, is_write); 4687feb51b7SPeter Xu 4691da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PFO) { 4701376211fSPeter Xu error_report_once("New fault is not recorded due to " 4711376211fSPeter Xu "Primary Fault Overflow"); 4721da12ec4SLe Tan return; 4731da12ec4SLe Tan } 4747feb51b7SPeter Xu 4751da12ec4SLe Tan if (vtd_try_collapse_fault(s, source_id)) { 4761376211fSPeter Xu error_report_once("New fault is not recorded due to " 4771376211fSPeter Xu "compression of faults"); 4781da12ec4SLe Tan return; 4791da12ec4SLe Tan } 4807feb51b7SPeter Xu 4811da12ec4SLe Tan if (vtd_is_frcd_set(s, s->next_frcd_reg)) { 4821376211fSPeter Xu error_report_once("Next Fault Recording Reg is used, " 4831376211fSPeter Xu "new fault is not recorded, set PFO field"); 4841da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO); 4851da12ec4SLe Tan return; 4861da12ec4SLe Tan } 4871da12ec4SLe Tan 4881da12ec4SLe Tan vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write); 4891da12ec4SLe Tan 4901da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PPF) { 4911376211fSPeter Xu error_report_once("There are pending faults already, " 4921376211fSPeter Xu "fault event is not generated"); 4931da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); 4941da12ec4SLe Tan s->next_frcd_reg++; 4951da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 4961da12ec4SLe Tan s->next_frcd_reg = 0; 4971da12ec4SLe Tan } 4981da12ec4SLe Tan } else { 4991da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK, 5001da12ec4SLe Tan VTD_FSTS_FRI(s->next_frcd_reg)); 5011da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */ 5021da12ec4SLe Tan s->next_frcd_reg++; 5031da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 5041da12ec4SLe Tan s->next_frcd_reg = 0; 5051da12ec4SLe Tan } 5061da12ec4SLe Tan /* This case actually cause the PPF to be Set. 5071da12ec4SLe Tan * So generate fault event (interrupt). 5081da12ec4SLe Tan */ 5091da12ec4SLe Tan vtd_generate_fault_event(s, fsts_reg); 5101da12ec4SLe Tan } 5111da12ec4SLe Tan } 5121da12ec4SLe Tan 513ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error 514ed7b8fbcSLe Tan * conditions. 515ed7b8fbcSLe Tan */ 516ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s) 517ed7b8fbcSLe Tan { 518ed7b8fbcSLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 519ed7b8fbcSLe Tan 520ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE); 521ed7b8fbcSLe Tan vtd_generate_fault_event(s, fsts_reg); 522ed7b8fbcSLe Tan } 523ed7b8fbcSLe Tan 524ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */ 525ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s) 526ed7b8fbcSLe Tan { 527ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) { 528bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("One pending, skip current"); 529ed7b8fbcSLe Tan return; 530ed7b8fbcSLe Tan } 531ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC); 532ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP); 533ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) { 534bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, " 535bc535e59SPeter Xu "new event not generated"); 536ed7b8fbcSLe Tan return; 537ed7b8fbcSLe Tan } else { 538ed7b8fbcSLe Tan /* Generate the interrupt event */ 539bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("Generating complete event"); 540ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 541ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 542ed7b8fbcSLe Tan } 543ed7b8fbcSLe Tan } 544ed7b8fbcSLe Tan 545fb43cf73SLiu, Yi L static inline bool vtd_root_entry_present(IntelIOMMUState *s, 546fb43cf73SLiu, Yi L VTDRootEntry *re, 547fb43cf73SLiu, Yi L uint8_t devfn) 5481da12ec4SLe Tan { 549fb43cf73SLiu, Yi L if (s->root_scalable && devfn > UINT8_MAX / 2) { 550fb43cf73SLiu, Yi L return re->hi & VTD_ROOT_ENTRY_P; 551fb43cf73SLiu, Yi L } 552fb43cf73SLiu, Yi L 553fb43cf73SLiu, Yi L return re->lo & VTD_ROOT_ENTRY_P; 5541da12ec4SLe Tan } 5551da12ec4SLe Tan 5561da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index, 5571da12ec4SLe Tan VTDRootEntry *re) 5581da12ec4SLe Tan { 5591da12ec4SLe Tan dma_addr_t addr; 5601da12ec4SLe Tan 5611da12ec4SLe Tan addr = s->root + index * sizeof(*re); 5621da12ec4SLe Tan if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) { 563fb43cf73SLiu, Yi L re->lo = 0; 5641da12ec4SLe Tan return -VTD_FR_ROOT_TABLE_INV; 5651da12ec4SLe Tan } 566fb43cf73SLiu, Yi L re->lo = le64_to_cpu(re->lo); 567fb43cf73SLiu, Yi L re->hi = le64_to_cpu(re->hi); 5681da12ec4SLe Tan return 0; 5691da12ec4SLe Tan } 5701da12ec4SLe Tan 5718f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context) 5721da12ec4SLe Tan { 5731da12ec4SLe Tan return context->lo & VTD_CONTEXT_ENTRY_P; 5741da12ec4SLe Tan } 5751da12ec4SLe Tan 576fb43cf73SLiu, Yi L static int vtd_get_context_entry_from_root(IntelIOMMUState *s, 577fb43cf73SLiu, Yi L VTDRootEntry *re, 578fb43cf73SLiu, Yi L uint8_t index, 5791da12ec4SLe Tan VTDContextEntry *ce) 5801da12ec4SLe Tan { 581fb43cf73SLiu, Yi L dma_addr_t addr, ce_size; 5821da12ec4SLe Tan 5836c441e1dSPeter Xu /* we have checked that root entry is present */ 584fb43cf73SLiu, Yi L ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE : 585fb43cf73SLiu, Yi L VTD_CTX_ENTRY_LEGACY_SIZE; 586fb43cf73SLiu, Yi L 587fb43cf73SLiu, Yi L if (s->root_scalable && index > UINT8_MAX / 2) { 588fb43cf73SLiu, Yi L index = index & (~VTD_DEVFN_CHECK_MASK); 589fb43cf73SLiu, Yi L addr = re->hi & VTD_ROOT_ENTRY_CTP; 590fb43cf73SLiu, Yi L } else { 591fb43cf73SLiu, Yi L addr = re->lo & VTD_ROOT_ENTRY_CTP; 592fb43cf73SLiu, Yi L } 593fb43cf73SLiu, Yi L 594fb43cf73SLiu, Yi L addr = addr + index * ce_size; 595fb43cf73SLiu, Yi L if (dma_memory_read(&address_space_memory, addr, ce, ce_size)) { 5961da12ec4SLe Tan return -VTD_FR_CONTEXT_TABLE_INV; 5971da12ec4SLe Tan } 598fb43cf73SLiu, Yi L 5991da12ec4SLe Tan ce->lo = le64_to_cpu(ce->lo); 6001da12ec4SLe Tan ce->hi = le64_to_cpu(ce->hi); 601fb43cf73SLiu, Yi L if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) { 602fb43cf73SLiu, Yi L ce->val[2] = le64_to_cpu(ce->val[2]); 603fb43cf73SLiu, Yi L ce->val[3] = le64_to_cpu(ce->val[3]); 604fb43cf73SLiu, Yi L } 6051da12ec4SLe Tan return 0; 6061da12ec4SLe Tan } 6071da12ec4SLe Tan 6088f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce) 6091da12ec4SLe Tan { 6101da12ec4SLe Tan return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR; 6111da12ec4SLe Tan } 6121da12ec4SLe Tan 61337f51384SPrasad Singamsetty static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw) 6141da12ec4SLe Tan { 61537f51384SPrasad Singamsetty return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw); 6161da12ec4SLe Tan } 6171da12ec4SLe Tan 6181da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */ 6191da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level) 6201da12ec4SLe Tan { 6211da12ec4SLe Tan return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK); 6221da12ec4SLe Tan } 6231da12ec4SLe Tan 6241da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */ 6251da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index) 6261da12ec4SLe Tan { 6271da12ec4SLe Tan uint64_t slpte; 6281da12ec4SLe Tan 6291da12ec4SLe Tan assert(index < VTD_SL_PT_ENTRY_NR); 6301da12ec4SLe Tan 6311da12ec4SLe Tan if (dma_memory_read(&address_space_memory, 6321da12ec4SLe Tan base_addr + index * sizeof(slpte), &slpte, 6331da12ec4SLe Tan sizeof(slpte))) { 6341da12ec4SLe Tan slpte = (uint64_t)-1; 6351da12ec4SLe Tan return slpte; 6361da12ec4SLe Tan } 6371da12ec4SLe Tan slpte = le64_to_cpu(slpte); 6381da12ec4SLe Tan return slpte; 6391da12ec4SLe Tan } 6401da12ec4SLe Tan 6416e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset 6426e905564SPeter Xu * of current level. 6431da12ec4SLe Tan */ 6446e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level) 6451da12ec4SLe Tan { 6466e905564SPeter Xu return (iova >> vtd_slpt_level_shift(level)) & 6471da12ec4SLe Tan ((1ULL << VTD_SL_LEVEL_BITS) - 1); 6481da12ec4SLe Tan } 6491da12ec4SLe Tan 6501da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */ 6511da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level) 6521da12ec4SLe Tan { 6531da12ec4SLe Tan return VTD_CAP_SAGAW_MASK & s->cap & 6541da12ec4SLe Tan (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT)); 6551da12ec4SLe Tan } 6561da12ec4SLe Tan 657fb43cf73SLiu, Yi L /* Return true if check passed, otherwise false */ 658fb43cf73SLiu, Yi L static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu, 659fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 660fb43cf73SLiu, Yi L { 661fb43cf73SLiu, Yi L switch (VTD_PE_GET_TYPE(pe)) { 662fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_FLT: 663fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_SLT: 664fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_NESTED: 665fb43cf73SLiu, Yi L break; 666fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_PT: 667fb43cf73SLiu, Yi L if (!x86_iommu->pt_supported) { 668fb43cf73SLiu, Yi L return false; 669fb43cf73SLiu, Yi L } 670fb43cf73SLiu, Yi L break; 671fb43cf73SLiu, Yi L default: 672fb43cf73SLiu, Yi L /* Unknwon type */ 673fb43cf73SLiu, Yi L return false; 674fb43cf73SLiu, Yi L } 675fb43cf73SLiu, Yi L return true; 676fb43cf73SLiu, Yi L } 677fb43cf73SLiu, Yi L 678fb43cf73SLiu, Yi L static int vtd_get_pasid_dire(dma_addr_t pasid_dir_base, 679fb43cf73SLiu, Yi L uint32_t pasid, 680fb43cf73SLiu, Yi L VTDPASIDDirEntry *pdire) 681fb43cf73SLiu, Yi L { 682fb43cf73SLiu, Yi L uint32_t index; 683fb43cf73SLiu, Yi L dma_addr_t addr, entry_size; 684fb43cf73SLiu, Yi L 685fb43cf73SLiu, Yi L index = VTD_PASID_DIR_INDEX(pasid); 686fb43cf73SLiu, Yi L entry_size = VTD_PASID_DIR_ENTRY_SIZE; 687fb43cf73SLiu, Yi L addr = pasid_dir_base + index * entry_size; 688fb43cf73SLiu, Yi L if (dma_memory_read(&address_space_memory, addr, pdire, entry_size)) { 689fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 690fb43cf73SLiu, Yi L } 691fb43cf73SLiu, Yi L 692fb43cf73SLiu, Yi L return 0; 693fb43cf73SLiu, Yi L } 694fb43cf73SLiu, Yi L 695fb43cf73SLiu, Yi L static int vtd_get_pasid_entry(IntelIOMMUState *s, 696fb43cf73SLiu, Yi L uint32_t pasid, 697fb43cf73SLiu, Yi L VTDPASIDDirEntry *pdire, 698fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 699fb43cf73SLiu, Yi L { 700fb43cf73SLiu, Yi L uint32_t index; 701fb43cf73SLiu, Yi L dma_addr_t addr, entry_size; 702fb43cf73SLiu, Yi L X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 703fb43cf73SLiu, Yi L 704fb43cf73SLiu, Yi L index = VTD_PASID_TABLE_INDEX(pasid); 705fb43cf73SLiu, Yi L entry_size = VTD_PASID_ENTRY_SIZE; 706fb43cf73SLiu, Yi L addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK; 707fb43cf73SLiu, Yi L addr = addr + index * entry_size; 708fb43cf73SLiu, Yi L if (dma_memory_read(&address_space_memory, addr, pe, entry_size)) { 709fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 710fb43cf73SLiu, Yi L } 711fb43cf73SLiu, Yi L 712fb43cf73SLiu, Yi L /* Do translation type check */ 713fb43cf73SLiu, Yi L if (!vtd_pe_type_check(x86_iommu, pe)) { 714fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 715fb43cf73SLiu, Yi L } 716fb43cf73SLiu, Yi L 717fb43cf73SLiu, Yi L if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) { 718fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 719fb43cf73SLiu, Yi L } 720fb43cf73SLiu, Yi L 721fb43cf73SLiu, Yi L return 0; 722fb43cf73SLiu, Yi L } 723fb43cf73SLiu, Yi L 724fb43cf73SLiu, Yi L static int vtd_get_pasid_entry_from_pasid(IntelIOMMUState *s, 725fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base, 726fb43cf73SLiu, Yi L uint32_t pasid, 727fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 728fb43cf73SLiu, Yi L { 729fb43cf73SLiu, Yi L int ret; 730fb43cf73SLiu, Yi L VTDPASIDDirEntry pdire; 731fb43cf73SLiu, Yi L 732fb43cf73SLiu, Yi L ret = vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire); 733fb43cf73SLiu, Yi L if (ret) { 734fb43cf73SLiu, Yi L return ret; 735fb43cf73SLiu, Yi L } 736fb43cf73SLiu, Yi L 737fb43cf73SLiu, Yi L ret = vtd_get_pasid_entry(s, pasid, &pdire, pe); 738fb43cf73SLiu, Yi L if (ret) { 739fb43cf73SLiu, Yi L return ret; 740fb43cf73SLiu, Yi L } 741fb43cf73SLiu, Yi L 742fb43cf73SLiu, Yi L return ret; 743fb43cf73SLiu, Yi L } 744fb43cf73SLiu, Yi L 745fb43cf73SLiu, Yi L static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s, 746fb43cf73SLiu, Yi L VTDContextEntry *ce, 747fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 748fb43cf73SLiu, Yi L { 749fb43cf73SLiu, Yi L uint32_t pasid; 750fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base; 751fb43cf73SLiu, Yi L int ret = 0; 752fb43cf73SLiu, Yi L 753fb43cf73SLiu, Yi L pasid = VTD_CE_GET_RID2PASID(ce); 754fb43cf73SLiu, Yi L pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); 755fb43cf73SLiu, Yi L ret = vtd_get_pasid_entry_from_pasid(s, pasid_dir_base, pasid, pe); 756fb43cf73SLiu, Yi L 757fb43cf73SLiu, Yi L return ret; 758fb43cf73SLiu, Yi L } 759fb43cf73SLiu, Yi L 760fb43cf73SLiu, Yi L static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s, 761fb43cf73SLiu, Yi L VTDContextEntry *ce, 762fb43cf73SLiu, Yi L bool *pe_fpd_set) 763fb43cf73SLiu, Yi L { 764fb43cf73SLiu, Yi L int ret; 765fb43cf73SLiu, Yi L uint32_t pasid; 766fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base; 767fb43cf73SLiu, Yi L VTDPASIDDirEntry pdire; 768fb43cf73SLiu, Yi L VTDPASIDEntry pe; 769fb43cf73SLiu, Yi L 770fb43cf73SLiu, Yi L pasid = VTD_CE_GET_RID2PASID(ce); 771fb43cf73SLiu, Yi L pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); 772fb43cf73SLiu, Yi L 773fb43cf73SLiu, Yi L ret = vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire); 774fb43cf73SLiu, Yi L if (ret) { 775fb43cf73SLiu, Yi L return ret; 776fb43cf73SLiu, Yi L } 777fb43cf73SLiu, Yi L 778fb43cf73SLiu, Yi L if (pdire.val & VTD_PASID_DIR_FPD) { 779fb43cf73SLiu, Yi L *pe_fpd_set = true; 780fb43cf73SLiu, Yi L return 0; 781fb43cf73SLiu, Yi L } 782fb43cf73SLiu, Yi L 783fb43cf73SLiu, Yi L ret = vtd_get_pasid_entry(s, pasid, &pdire, &pe); 784fb43cf73SLiu, Yi L if (ret) { 785fb43cf73SLiu, Yi L return ret; 786fb43cf73SLiu, Yi L } 787fb43cf73SLiu, Yi L 788fb43cf73SLiu, Yi L if (pe.val[0] & VTD_PASID_ENTRY_FPD) { 789fb43cf73SLiu, Yi L *pe_fpd_set = true; 790fb43cf73SLiu, Yi L } 791fb43cf73SLiu, Yi L 792fb43cf73SLiu, Yi L return 0; 793fb43cf73SLiu, Yi L } 794fb43cf73SLiu, Yi L 7951da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level 7961da12ec4SLe Tan * page-table walk from the Address Width field of context-entry. 7971da12ec4SLe Tan */ 7988f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce) 7991da12ec4SLe Tan { 8001da12ec4SLe Tan return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW); 8011da12ec4SLe Tan } 8021da12ec4SLe Tan 803fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_level(IntelIOMMUState *s, 804fb43cf73SLiu, Yi L VTDContextEntry *ce) 805fb43cf73SLiu, Yi L { 806fb43cf73SLiu, Yi L VTDPASIDEntry pe; 807fb43cf73SLiu, Yi L 808fb43cf73SLiu, Yi L if (s->root_scalable) { 809fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 810fb43cf73SLiu, Yi L return VTD_PE_GET_LEVEL(&pe); 811fb43cf73SLiu, Yi L } 812fb43cf73SLiu, Yi L 813fb43cf73SLiu, Yi L return vtd_ce_get_level(ce); 814fb43cf73SLiu, Yi L } 815fb43cf73SLiu, Yi L 8168f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce) 8171da12ec4SLe Tan { 8181da12ec4SLe Tan return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9; 8191da12ec4SLe Tan } 8201da12ec4SLe Tan 821fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s, 822fb43cf73SLiu, Yi L VTDContextEntry *ce) 823fb43cf73SLiu, Yi L { 824fb43cf73SLiu, Yi L VTDPASIDEntry pe; 825fb43cf73SLiu, Yi L 826fb43cf73SLiu, Yi L if (s->root_scalable) { 827fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 828fb43cf73SLiu, Yi L return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9; 829fb43cf73SLiu, Yi L } 830fb43cf73SLiu, Yi L 831fb43cf73SLiu, Yi L return vtd_ce_get_agaw(ce); 832fb43cf73SLiu, Yi L } 833fb43cf73SLiu, Yi L 834127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce) 835127ff5c3SPeter Xu { 836127ff5c3SPeter Xu return ce->lo & VTD_CONTEXT_ENTRY_TT; 837127ff5c3SPeter Xu } 838127ff5c3SPeter Xu 839fb43cf73SLiu, Yi L /* Only for Legacy Mode. Return true if check passed, otherwise false */ 840f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu, 841f80c9874SPeter Xu VTDContextEntry *ce) 842f80c9874SPeter Xu { 843f80c9874SPeter Xu switch (vtd_ce_get_type(ce)) { 844f80c9874SPeter Xu case VTD_CONTEXT_TT_MULTI_LEVEL: 845f80c9874SPeter Xu /* Always supported */ 846f80c9874SPeter Xu break; 847f80c9874SPeter Xu case VTD_CONTEXT_TT_DEV_IOTLB: 848f80c9874SPeter Xu if (!x86_iommu->dt_supported) { 849095955b2SPeter Xu error_report_once("%s: DT specified but not supported", __func__); 850f80c9874SPeter Xu return false; 851f80c9874SPeter Xu } 852f80c9874SPeter Xu break; 853dbaabb25SPeter Xu case VTD_CONTEXT_TT_PASS_THROUGH: 854dbaabb25SPeter Xu if (!x86_iommu->pt_supported) { 855095955b2SPeter Xu error_report_once("%s: PT specified but not supported", __func__); 856dbaabb25SPeter Xu return false; 857dbaabb25SPeter Xu } 858dbaabb25SPeter Xu break; 859f80c9874SPeter Xu default: 860fb43cf73SLiu, Yi L /* Unknown type */ 861095955b2SPeter Xu error_report_once("%s: unknown ce type: %"PRIu32, __func__, 862095955b2SPeter Xu vtd_ce_get_type(ce)); 863f80c9874SPeter Xu return false; 864f80c9874SPeter Xu } 865f80c9874SPeter Xu return true; 866f80c9874SPeter Xu } 867f80c9874SPeter Xu 868fb43cf73SLiu, Yi L static inline uint64_t vtd_iova_limit(IntelIOMMUState *s, 869fb43cf73SLiu, Yi L VTDContextEntry *ce, uint8_t aw) 870f06a696dSPeter Xu { 871fb43cf73SLiu, Yi L uint32_t ce_agaw = vtd_get_iova_agaw(s, ce); 87237f51384SPrasad Singamsetty return 1ULL << MIN(ce_agaw, aw); 873f06a696dSPeter Xu } 874f06a696dSPeter Xu 875f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */ 876fb43cf73SLiu, Yi L static inline bool vtd_iova_range_check(IntelIOMMUState *s, 877fb43cf73SLiu, Yi L uint64_t iova, VTDContextEntry *ce, 87837f51384SPrasad Singamsetty uint8_t aw) 879f06a696dSPeter Xu { 880f06a696dSPeter Xu /* 881f06a696dSPeter Xu * Check if @iova is above 2^X-1, where X is the minimum of MGAW 882f06a696dSPeter Xu * in CAP_REG and AW in context-entry. 883f06a696dSPeter Xu */ 884fb43cf73SLiu, Yi L return !(iova & ~(vtd_iova_limit(s, ce, aw) - 1)); 885fb43cf73SLiu, Yi L } 886fb43cf73SLiu, Yi L 887fb43cf73SLiu, Yi L static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s, 888fb43cf73SLiu, Yi L VTDContextEntry *ce) 889fb43cf73SLiu, Yi L { 890fb43cf73SLiu, Yi L VTDPASIDEntry pe; 891fb43cf73SLiu, Yi L 892fb43cf73SLiu, Yi L if (s->root_scalable) { 893fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 894fb43cf73SLiu, Yi L return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR; 895fb43cf73SLiu, Yi L } 896fb43cf73SLiu, Yi L 897fb43cf73SLiu, Yi L return vtd_ce_get_slpt_base(ce); 898f06a696dSPeter Xu } 899f06a696dSPeter Xu 90092e5d85eSPrasad Singamsetty /* 90192e5d85eSPrasad Singamsetty * Rsvd field masks for spte: 90292e5d85eSPrasad Singamsetty * Index [1] to [4] 4k pages 90392e5d85eSPrasad Singamsetty * Index [5] to [8] large pages 90492e5d85eSPrasad Singamsetty */ 90592e5d85eSPrasad Singamsetty static uint64_t vtd_paging_entry_rsvd_field[9]; 9061da12ec4SLe Tan 9071da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level) 9081da12ec4SLe Tan { 9091da12ec4SLe Tan if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) { 9101da12ec4SLe Tan /* Maybe large page */ 9111da12ec4SLe Tan return slpte & vtd_paging_entry_rsvd_field[level + 4]; 9121da12ec4SLe Tan } else { 9131da12ec4SLe Tan return slpte & vtd_paging_entry_rsvd_field[level]; 9141da12ec4SLe Tan } 9151da12ec4SLe Tan } 9161da12ec4SLe Tan 917dbaabb25SPeter Xu /* Find the VTD address space associated with a given bus number */ 918dbaabb25SPeter Xu static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) 919dbaabb25SPeter Xu { 920dbaabb25SPeter Xu VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; 921dbaabb25SPeter Xu if (!vtd_bus) { 922dbaabb25SPeter Xu /* 923dbaabb25SPeter Xu * Iterate over the registered buses to find the one which 924dbaabb25SPeter Xu * currently hold this bus number, and update the bus_num 925dbaabb25SPeter Xu * lookup table: 926dbaabb25SPeter Xu */ 927dbaabb25SPeter Xu GHashTableIter iter; 928dbaabb25SPeter Xu 929dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 930dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 931dbaabb25SPeter Xu if (pci_bus_num(vtd_bus->bus) == bus_num) { 932dbaabb25SPeter Xu s->vtd_as_by_bus_num[bus_num] = vtd_bus; 933dbaabb25SPeter Xu return vtd_bus; 934dbaabb25SPeter Xu } 935dbaabb25SPeter Xu } 936dbaabb25SPeter Xu } 937dbaabb25SPeter Xu return vtd_bus; 938dbaabb25SPeter Xu } 939dbaabb25SPeter Xu 9406e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level 9411da12ec4SLe Tan * of the translation, can be used for deciding the size of large page. 9421da12ec4SLe Tan */ 943fb43cf73SLiu, Yi L static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce, 944fb43cf73SLiu, Yi L uint64_t iova, bool is_write, 9451da12ec4SLe Tan uint64_t *slptep, uint32_t *slpte_level, 94637f51384SPrasad Singamsetty bool *reads, bool *writes, uint8_t aw_bits) 9471da12ec4SLe Tan { 948fb43cf73SLiu, Yi L dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce); 949fb43cf73SLiu, Yi L uint32_t level = vtd_get_iova_level(s, ce); 9501da12ec4SLe Tan uint32_t offset; 9511da12ec4SLe Tan uint64_t slpte; 9521da12ec4SLe Tan uint64_t access_right_check; 9531da12ec4SLe Tan 954fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, iova, ce, aw_bits)) { 9554e4abd11SPeter Xu error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ")", 9564e4abd11SPeter Xu __func__, iova); 9571da12ec4SLe Tan return -VTD_FR_ADDR_BEYOND_MGAW; 9581da12ec4SLe Tan } 9591da12ec4SLe Tan 9601da12ec4SLe Tan /* FIXME: what is the Atomics request here? */ 9611da12ec4SLe Tan access_right_check = is_write ? VTD_SL_W : VTD_SL_R; 9621da12ec4SLe Tan 9631da12ec4SLe Tan while (true) { 9646e905564SPeter Xu offset = vtd_iova_level_offset(iova, level); 9651da12ec4SLe Tan slpte = vtd_get_slpte(addr, offset); 9661da12ec4SLe Tan 9671da12ec4SLe Tan if (slpte == (uint64_t)-1) { 9684e4abd11SPeter Xu error_report_once("%s: detected read error on DMAR slpte " 9694e4abd11SPeter Xu "(iova=0x%" PRIx64 ")", __func__, iova); 970fb43cf73SLiu, Yi L if (level == vtd_get_iova_level(s, ce)) { 9711da12ec4SLe Tan /* Invalid programming of context-entry */ 9721da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 9731da12ec4SLe Tan } else { 9741da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_INV; 9751da12ec4SLe Tan } 9761da12ec4SLe Tan } 9771da12ec4SLe Tan *reads = (*reads) && (slpte & VTD_SL_R); 9781da12ec4SLe Tan *writes = (*writes) && (slpte & VTD_SL_W); 9791da12ec4SLe Tan if (!(slpte & access_right_check)) { 9804e4abd11SPeter Xu error_report_once("%s: detected slpte permission error " 9814e4abd11SPeter Xu "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", " 9824e4abd11SPeter Xu "slpte=0x%" PRIx64 ", write=%d)", __func__, 9834e4abd11SPeter Xu iova, level, slpte, is_write); 9841da12ec4SLe Tan return is_write ? -VTD_FR_WRITE : -VTD_FR_READ; 9851da12ec4SLe Tan } 9861da12ec4SLe Tan if (vtd_slpte_nonzero_rsvd(slpte, level)) { 9874e4abd11SPeter Xu error_report_once("%s: detected splte reserve non-zero " 9884e4abd11SPeter Xu "iova=0x%" PRIx64 ", level=0x%" PRIx32 9894e4abd11SPeter Xu "slpte=0x%" PRIx64 ")", __func__, iova, 9904e4abd11SPeter Xu level, slpte); 9911da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_RSVD; 9921da12ec4SLe Tan } 9931da12ec4SLe Tan 9941da12ec4SLe Tan if (vtd_is_last_slpte(slpte, level)) { 9951da12ec4SLe Tan *slptep = slpte; 9961da12ec4SLe Tan *slpte_level = level; 9971da12ec4SLe Tan return 0; 9981da12ec4SLe Tan } 99937f51384SPrasad Singamsetty addr = vtd_get_slpte_addr(slpte, aw_bits); 10001da12ec4SLe Tan level--; 10011da12ec4SLe Tan } 10021da12ec4SLe Tan } 10031da12ec4SLe Tan 1004f06a696dSPeter Xu typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private); 1005f06a696dSPeter Xu 1006fe215b0cSPeter Xu /** 1007fe215b0cSPeter Xu * Constant information used during page walking 1008fe215b0cSPeter Xu * 1009fe215b0cSPeter Xu * @hook_fn: hook func to be called when detected page 1010fe215b0cSPeter Xu * @private: private data to be passed into hook func 1011fe215b0cSPeter Xu * @notify_unmap: whether we should notify invalid entries 10122f764fa8SPeter Xu * @as: VT-d address space of the device 1013fe215b0cSPeter Xu * @aw: maximum address width 1014d118c06eSPeter Xu * @domain: domain ID of the page walk 1015fe215b0cSPeter Xu */ 1016fe215b0cSPeter Xu typedef struct { 10172f764fa8SPeter Xu VTDAddressSpace *as; 1018fe215b0cSPeter Xu vtd_page_walk_hook hook_fn; 1019fe215b0cSPeter Xu void *private; 1020fe215b0cSPeter Xu bool notify_unmap; 1021fe215b0cSPeter Xu uint8_t aw; 1022d118c06eSPeter Xu uint16_t domain_id; 1023fe215b0cSPeter Xu } vtd_page_walk_info; 1024fe215b0cSPeter Xu 1025d118c06eSPeter Xu static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info) 102636d2d52bSPeter Xu { 102763b88968SPeter Xu VTDAddressSpace *as = info->as; 1028fe215b0cSPeter Xu vtd_page_walk_hook hook_fn = info->hook_fn; 1029fe215b0cSPeter Xu void *private = info->private; 103063b88968SPeter Xu DMAMap target = { 103163b88968SPeter Xu .iova = entry->iova, 103263b88968SPeter Xu .size = entry->addr_mask, 103363b88968SPeter Xu .translated_addr = entry->translated_addr, 103463b88968SPeter Xu .perm = entry->perm, 103563b88968SPeter Xu }; 103663b88968SPeter Xu DMAMap *mapped = iova_tree_find(as->iova_tree, &target); 103763b88968SPeter Xu 103863b88968SPeter Xu if (entry->perm == IOMMU_NONE && !info->notify_unmap) { 103963b88968SPeter Xu trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask); 104063b88968SPeter Xu return 0; 104163b88968SPeter Xu } 1042fe215b0cSPeter Xu 104336d2d52bSPeter Xu assert(hook_fn); 104463b88968SPeter Xu 104563b88968SPeter Xu /* Update local IOVA mapped ranges */ 104663b88968SPeter Xu if (entry->perm) { 104763b88968SPeter Xu if (mapped) { 104863b88968SPeter Xu /* If it's exactly the same translation, skip */ 104963b88968SPeter Xu if (!memcmp(mapped, &target, sizeof(target))) { 105063b88968SPeter Xu trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask, 105163b88968SPeter Xu entry->translated_addr); 105263b88968SPeter Xu return 0; 105363b88968SPeter Xu } else { 105463b88968SPeter Xu /* 105563b88968SPeter Xu * Translation changed. Normally this should not 105663b88968SPeter Xu * happen, but it can happen when with buggy guest 105763b88968SPeter Xu * OSes. Note that there will be a small window that 105863b88968SPeter Xu * we don't have map at all. But that's the best 105963b88968SPeter Xu * effort we can do. The ideal way to emulate this is 106063b88968SPeter Xu * atomically modify the PTE to follow what has 106163b88968SPeter Xu * changed, but we can't. One example is that vfio 106263b88968SPeter Xu * driver only has VFIO_IOMMU_[UN]MAP_DMA but no 106363b88968SPeter Xu * interface to modify a mapping (meanwhile it seems 106463b88968SPeter Xu * meaningless to even provide one). Anyway, let's 106563b88968SPeter Xu * mark this as a TODO in case one day we'll have 106663b88968SPeter Xu * a better solution. 106763b88968SPeter Xu */ 106863b88968SPeter Xu IOMMUAccessFlags cache_perm = entry->perm; 106963b88968SPeter Xu int ret; 107063b88968SPeter Xu 107163b88968SPeter Xu /* Emulate an UNMAP */ 107263b88968SPeter Xu entry->perm = IOMMU_NONE; 107363b88968SPeter Xu trace_vtd_page_walk_one(info->domain_id, 107463b88968SPeter Xu entry->iova, 107563b88968SPeter Xu entry->translated_addr, 107663b88968SPeter Xu entry->addr_mask, 107763b88968SPeter Xu entry->perm); 107863b88968SPeter Xu ret = hook_fn(entry, private); 107963b88968SPeter Xu if (ret) { 108063b88968SPeter Xu return ret; 108163b88968SPeter Xu } 108263b88968SPeter Xu /* Drop any existing mapping */ 108363b88968SPeter Xu iova_tree_remove(as->iova_tree, &target); 108463b88968SPeter Xu /* Recover the correct permission */ 108563b88968SPeter Xu entry->perm = cache_perm; 108663b88968SPeter Xu } 108763b88968SPeter Xu } 108863b88968SPeter Xu iova_tree_insert(as->iova_tree, &target); 108963b88968SPeter Xu } else { 109063b88968SPeter Xu if (!mapped) { 109163b88968SPeter Xu /* Skip since we didn't map this range at all */ 109263b88968SPeter Xu trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask); 109363b88968SPeter Xu return 0; 109463b88968SPeter Xu } 109563b88968SPeter Xu iova_tree_remove(as->iova_tree, &target); 109663b88968SPeter Xu } 109763b88968SPeter Xu 1098d118c06eSPeter Xu trace_vtd_page_walk_one(info->domain_id, entry->iova, 1099d118c06eSPeter Xu entry->translated_addr, entry->addr_mask, 1100d118c06eSPeter Xu entry->perm); 110136d2d52bSPeter Xu return hook_fn(entry, private); 110236d2d52bSPeter Xu } 110336d2d52bSPeter Xu 1104f06a696dSPeter Xu /** 1105f06a696dSPeter Xu * vtd_page_walk_level - walk over specific level for IOVA range 1106f06a696dSPeter Xu * 1107f06a696dSPeter Xu * @addr: base GPA addr to start the walk 1108f06a696dSPeter Xu * @start: IOVA range start address 1109f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 1110f06a696dSPeter Xu * @read: whether parent level has read permission 1111f06a696dSPeter Xu * @write: whether parent level has write permission 1112fe215b0cSPeter Xu * @info: constant information for the page walk 1113f06a696dSPeter Xu */ 1114f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start, 1115fe215b0cSPeter Xu uint64_t end, uint32_t level, bool read, 1116fe215b0cSPeter Xu bool write, vtd_page_walk_info *info) 1117f06a696dSPeter Xu { 1118f06a696dSPeter Xu bool read_cur, write_cur, entry_valid; 1119f06a696dSPeter Xu uint32_t offset; 1120f06a696dSPeter Xu uint64_t slpte; 1121f06a696dSPeter Xu uint64_t subpage_size, subpage_mask; 1122f06a696dSPeter Xu IOMMUTLBEntry entry; 1123f06a696dSPeter Xu uint64_t iova = start; 1124f06a696dSPeter Xu uint64_t iova_next; 1125f06a696dSPeter Xu int ret = 0; 1126f06a696dSPeter Xu 1127f06a696dSPeter Xu trace_vtd_page_walk_level(addr, level, start, end); 1128f06a696dSPeter Xu 1129f06a696dSPeter Xu subpage_size = 1ULL << vtd_slpt_level_shift(level); 1130f06a696dSPeter Xu subpage_mask = vtd_slpt_level_page_mask(level); 1131f06a696dSPeter Xu 1132f06a696dSPeter Xu while (iova < end) { 1133f06a696dSPeter Xu iova_next = (iova & subpage_mask) + subpage_size; 1134f06a696dSPeter Xu 1135f06a696dSPeter Xu offset = vtd_iova_level_offset(iova, level); 1136f06a696dSPeter Xu slpte = vtd_get_slpte(addr, offset); 1137f06a696dSPeter Xu 1138f06a696dSPeter Xu if (slpte == (uint64_t)-1) { 1139f06a696dSPeter Xu trace_vtd_page_walk_skip_read(iova, iova_next); 1140f06a696dSPeter Xu goto next; 1141f06a696dSPeter Xu } 1142f06a696dSPeter Xu 1143f06a696dSPeter Xu if (vtd_slpte_nonzero_rsvd(slpte, level)) { 1144f06a696dSPeter Xu trace_vtd_page_walk_skip_reserve(iova, iova_next); 1145f06a696dSPeter Xu goto next; 1146f06a696dSPeter Xu } 1147f06a696dSPeter Xu 1148f06a696dSPeter Xu /* Permissions are stacked with parents' */ 1149f06a696dSPeter Xu read_cur = read && (slpte & VTD_SL_R); 1150f06a696dSPeter Xu write_cur = write && (slpte & VTD_SL_W); 1151f06a696dSPeter Xu 1152f06a696dSPeter Xu /* 1153f06a696dSPeter Xu * As long as we have either read/write permission, this is a 1154f06a696dSPeter Xu * valid entry. The rule works for both page entries and page 1155f06a696dSPeter Xu * table entries. 1156f06a696dSPeter Xu */ 1157f06a696dSPeter Xu entry_valid = read_cur | write_cur; 1158f06a696dSPeter Xu 115963b88968SPeter Xu if (!vtd_is_last_slpte(slpte, level) && entry_valid) { 116063b88968SPeter Xu /* 116163b88968SPeter Xu * This is a valid PDE (or even bigger than PDE). We need 116263b88968SPeter Xu * to walk one further level. 116363b88968SPeter Xu */ 116463b88968SPeter Xu ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw), 116563b88968SPeter Xu iova, MIN(iova_next, end), level - 1, 116663b88968SPeter Xu read_cur, write_cur, info); 116763b88968SPeter Xu } else { 116863b88968SPeter Xu /* 116963b88968SPeter Xu * This means we are either: 117063b88968SPeter Xu * 117163b88968SPeter Xu * (1) the real page entry (either 4K page, or huge page) 117263b88968SPeter Xu * (2) the whole range is invalid 117363b88968SPeter Xu * 117463b88968SPeter Xu * In either case, we send an IOTLB notification down. 117563b88968SPeter Xu */ 1176f06a696dSPeter Xu entry.target_as = &address_space_memory; 1177f06a696dSPeter Xu entry.iova = iova & subpage_mask; 117836d2d52bSPeter Xu entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur); 117936d2d52bSPeter Xu entry.addr_mask = ~subpage_mask; 1180f06a696dSPeter Xu /* NOTE: this is only meaningful if entry_valid == true */ 1181fe215b0cSPeter Xu entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw); 1182d118c06eSPeter Xu ret = vtd_page_walk_one(&entry, info); 118363b88968SPeter Xu } 118463b88968SPeter Xu 1185f06a696dSPeter Xu if (ret < 0) { 1186f06a696dSPeter Xu return ret; 1187f06a696dSPeter Xu } 1188f06a696dSPeter Xu 1189f06a696dSPeter Xu next: 1190f06a696dSPeter Xu iova = iova_next; 1191f06a696dSPeter Xu } 1192f06a696dSPeter Xu 1193f06a696dSPeter Xu return 0; 1194f06a696dSPeter Xu } 1195f06a696dSPeter Xu 1196f06a696dSPeter Xu /** 1197f06a696dSPeter Xu * vtd_page_walk - walk specific IOVA range, and call the hook 1198f06a696dSPeter Xu * 1199fb43cf73SLiu, Yi L * @s: intel iommu state 1200f06a696dSPeter Xu * @ce: context entry to walk upon 1201f06a696dSPeter Xu * @start: IOVA address to start the walk 1202f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 1203fe215b0cSPeter Xu * @info: page walking information struct 1204f06a696dSPeter Xu */ 1205fb43cf73SLiu, Yi L static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce, 1206fb43cf73SLiu, Yi L uint64_t start, uint64_t end, 1207fe215b0cSPeter Xu vtd_page_walk_info *info) 1208f06a696dSPeter Xu { 1209fb43cf73SLiu, Yi L dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce); 1210fb43cf73SLiu, Yi L uint32_t level = vtd_get_iova_level(s, ce); 1211f06a696dSPeter Xu 1212fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, start, ce, info->aw)) { 1213f06a696dSPeter Xu return -VTD_FR_ADDR_BEYOND_MGAW; 1214f06a696dSPeter Xu } 1215f06a696dSPeter Xu 1216fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, end, ce, info->aw)) { 1217f06a696dSPeter Xu /* Fix end so that it reaches the maximum */ 1218fb43cf73SLiu, Yi L end = vtd_iova_limit(s, ce, info->aw); 1219f06a696dSPeter Xu } 1220f06a696dSPeter Xu 1221fe215b0cSPeter Xu return vtd_page_walk_level(addr, start, end, level, true, true, info); 1222f06a696dSPeter Xu } 1223f06a696dSPeter Xu 1224fb43cf73SLiu, Yi L static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s, 1225fb43cf73SLiu, Yi L VTDRootEntry *re) 1226fb43cf73SLiu, Yi L { 1227fb43cf73SLiu, Yi L /* Legacy Mode reserved bits check */ 1228fb43cf73SLiu, Yi L if (!s->root_scalable && 1229fb43cf73SLiu, Yi L (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)))) 1230fb43cf73SLiu, Yi L goto rsvd_err; 1231fb43cf73SLiu, Yi L 1232fb43cf73SLiu, Yi L /* Scalable Mode reserved bits check */ 1233fb43cf73SLiu, Yi L if (s->root_scalable && 1234fb43cf73SLiu, Yi L ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) || 1235fb43cf73SLiu, Yi L (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits)))) 1236fb43cf73SLiu, Yi L goto rsvd_err; 1237fb43cf73SLiu, Yi L 1238fb43cf73SLiu, Yi L return 0; 1239fb43cf73SLiu, Yi L 1240fb43cf73SLiu, Yi L rsvd_err: 1241fb43cf73SLiu, Yi L error_report_once("%s: invalid root entry: hi=0x%"PRIx64 1242fb43cf73SLiu, Yi L ", lo=0x%"PRIx64, 1243fb43cf73SLiu, Yi L __func__, re->hi, re->lo); 1244fb43cf73SLiu, Yi L return -VTD_FR_ROOT_ENTRY_RSVD; 1245fb43cf73SLiu, Yi L } 1246fb43cf73SLiu, Yi L 1247fb43cf73SLiu, Yi L static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s, 1248fb43cf73SLiu, Yi L VTDContextEntry *ce) 1249fb43cf73SLiu, Yi L { 1250fb43cf73SLiu, Yi L if (!s->root_scalable && 1251fb43cf73SLiu, Yi L (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI || 1252fb43cf73SLiu, Yi L ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) { 1253fb43cf73SLiu, Yi L error_report_once("%s: invalid context entry: hi=%"PRIx64 1254fb43cf73SLiu, Yi L ", lo=%"PRIx64" (reserved nonzero)", 1255fb43cf73SLiu, Yi L __func__, ce->hi, ce->lo); 1256fb43cf73SLiu, Yi L return -VTD_FR_CONTEXT_ENTRY_RSVD; 1257fb43cf73SLiu, Yi L } 1258fb43cf73SLiu, Yi L 1259fb43cf73SLiu, Yi L if (s->root_scalable && 1260fb43cf73SLiu, Yi L (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) || 1261fb43cf73SLiu, Yi L ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 || 1262fb43cf73SLiu, Yi L ce->val[2] || 1263fb43cf73SLiu, Yi L ce->val[3])) { 1264fb43cf73SLiu, Yi L error_report_once("%s: invalid context entry: val[3]=%"PRIx64 1265fb43cf73SLiu, Yi L ", val[2]=%"PRIx64 1266fb43cf73SLiu, Yi L ", val[1]=%"PRIx64 1267fb43cf73SLiu, Yi L ", val[0]=%"PRIx64" (reserved nonzero)", 1268fb43cf73SLiu, Yi L __func__, ce->val[3], ce->val[2], 1269fb43cf73SLiu, Yi L ce->val[1], ce->val[0]); 1270fb43cf73SLiu, Yi L return -VTD_FR_CONTEXT_ENTRY_RSVD; 1271fb43cf73SLiu, Yi L } 1272fb43cf73SLiu, Yi L 1273fb43cf73SLiu, Yi L return 0; 1274fb43cf73SLiu, Yi L } 1275fb43cf73SLiu, Yi L 1276fb43cf73SLiu, Yi L static int vtd_ce_rid2pasid_check(IntelIOMMUState *s, 1277fb43cf73SLiu, Yi L VTDContextEntry *ce) 1278fb43cf73SLiu, Yi L { 1279fb43cf73SLiu, Yi L VTDPASIDEntry pe; 1280fb43cf73SLiu, Yi L 1281fb43cf73SLiu, Yi L /* 1282fb43cf73SLiu, Yi L * Make sure in Scalable Mode, a present context entry 1283fb43cf73SLiu, Yi L * has valid rid2pasid setting, which includes valid 1284fb43cf73SLiu, Yi L * rid2pasid field and corresponding pasid entry setting 1285fb43cf73SLiu, Yi L */ 1286fb43cf73SLiu, Yi L return vtd_ce_get_rid2pasid_entry(s, ce, &pe); 1287fb43cf73SLiu, Yi L } 1288fb43cf73SLiu, Yi L 12891da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */ 12901da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, 12911da12ec4SLe Tan uint8_t devfn, VTDContextEntry *ce) 12921da12ec4SLe Tan { 12931da12ec4SLe Tan VTDRootEntry re; 12941da12ec4SLe Tan int ret_fr; 1295f80c9874SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 12961da12ec4SLe Tan 12971da12ec4SLe Tan ret_fr = vtd_get_root_entry(s, bus_num, &re); 12981da12ec4SLe Tan if (ret_fr) { 12991da12ec4SLe Tan return ret_fr; 13001da12ec4SLe Tan } 13011da12ec4SLe Tan 1302fb43cf73SLiu, Yi L if (!vtd_root_entry_present(s, &re, devfn)) { 13036c441e1dSPeter Xu /* Not error - it's okay we don't have root entry. */ 13046c441e1dSPeter Xu trace_vtd_re_not_present(bus_num); 13051da12ec4SLe Tan return -VTD_FR_ROOT_ENTRY_P; 1306f80c9874SPeter Xu } 1307f80c9874SPeter Xu 1308fb43cf73SLiu, Yi L ret_fr = vtd_root_entry_rsvd_bits_check(s, &re); 1309fb43cf73SLiu, Yi L if (ret_fr) { 1310fb43cf73SLiu, Yi L return ret_fr; 13111da12ec4SLe Tan } 13121da12ec4SLe Tan 1313fb43cf73SLiu, Yi L ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce); 13141da12ec4SLe Tan if (ret_fr) { 13151da12ec4SLe Tan return ret_fr; 13161da12ec4SLe Tan } 13171da12ec4SLe Tan 13188f7d7161SPeter Xu if (!vtd_ce_present(ce)) { 13196c441e1dSPeter Xu /* Not error - it's okay we don't have context entry. */ 13206c441e1dSPeter Xu trace_vtd_ce_not_present(bus_num, devfn); 13211da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_P; 1322f80c9874SPeter Xu } 1323f80c9874SPeter Xu 1324fb43cf73SLiu, Yi L ret_fr = vtd_context_entry_rsvd_bits_check(s, ce); 1325fb43cf73SLiu, Yi L if (ret_fr) { 1326fb43cf73SLiu, Yi L return ret_fr; 13271da12ec4SLe Tan } 1328f80c9874SPeter Xu 13291da12ec4SLe Tan /* Check if the programming of context-entry is valid */ 1330fb43cf73SLiu, Yi L if (!s->root_scalable && 1331fb43cf73SLiu, Yi L !vtd_is_level_supported(s, vtd_ce_get_level(ce))) { 1332095955b2SPeter Xu error_report_once("%s: invalid context entry: hi=%"PRIx64 1333095955b2SPeter Xu ", lo=%"PRIx64" (level %d not supported)", 1334fb43cf73SLiu, Yi L __func__, ce->hi, ce->lo, 1335fb43cf73SLiu, Yi L vtd_ce_get_level(ce)); 13361da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 1337f80c9874SPeter Xu } 1338f80c9874SPeter Xu 1339fb43cf73SLiu, Yi L if (!s->root_scalable) { 1340f80c9874SPeter Xu /* Do translation type check */ 1341f80c9874SPeter Xu if (!vtd_ce_type_check(x86_iommu, ce)) { 1342095955b2SPeter Xu /* Errors dumped in vtd_ce_type_check() */ 13431da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 13441da12ec4SLe Tan } 1345fb43cf73SLiu, Yi L } else { 1346fb43cf73SLiu, Yi L /* 1347fb43cf73SLiu, Yi L * Check if the programming of context-entry.rid2pasid 1348fb43cf73SLiu, Yi L * and corresponding pasid setting is valid, and thus 1349fb43cf73SLiu, Yi L * avoids to check pasid entry fetching result in future 1350fb43cf73SLiu, Yi L * helper function calling. 1351fb43cf73SLiu, Yi L */ 1352fb43cf73SLiu, Yi L ret_fr = vtd_ce_rid2pasid_check(s, ce); 1353fb43cf73SLiu, Yi L if (ret_fr) { 1354fb43cf73SLiu, Yi L return ret_fr; 1355fb43cf73SLiu, Yi L } 1356fb43cf73SLiu, Yi L } 1357f80c9874SPeter Xu 13581da12ec4SLe Tan return 0; 13591da12ec4SLe Tan } 13601da12ec4SLe Tan 136163b88968SPeter Xu static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry, 136263b88968SPeter Xu void *private) 136363b88968SPeter Xu { 1364cb1efcf4SPeter Maydell memory_region_notify_iommu((IOMMUMemoryRegion *)private, 0, *entry); 136563b88968SPeter Xu return 0; 136663b88968SPeter Xu } 136763b88968SPeter Xu 1368fb43cf73SLiu, Yi L static uint16_t vtd_get_domain_id(IntelIOMMUState *s, 1369fb43cf73SLiu, Yi L VTDContextEntry *ce) 1370fb43cf73SLiu, Yi L { 1371fb43cf73SLiu, Yi L VTDPASIDEntry pe; 1372fb43cf73SLiu, Yi L 1373fb43cf73SLiu, Yi L if (s->root_scalable) { 1374fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 1375fb43cf73SLiu, Yi L return VTD_SM_PASID_ENTRY_DID(pe.val[1]); 1376fb43cf73SLiu, Yi L } 1377fb43cf73SLiu, Yi L 1378fb43cf73SLiu, Yi L return VTD_CONTEXT_ENTRY_DID(ce->hi); 1379fb43cf73SLiu, Yi L } 1380fb43cf73SLiu, Yi L 138163b88968SPeter Xu static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as, 138263b88968SPeter Xu VTDContextEntry *ce, 138363b88968SPeter Xu hwaddr addr, hwaddr size) 138463b88968SPeter Xu { 138563b88968SPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 138663b88968SPeter Xu vtd_page_walk_info info = { 138763b88968SPeter Xu .hook_fn = vtd_sync_shadow_page_hook, 138863b88968SPeter Xu .private = (void *)&vtd_as->iommu, 138963b88968SPeter Xu .notify_unmap = true, 139063b88968SPeter Xu .aw = s->aw_bits, 139163b88968SPeter Xu .as = vtd_as, 1392fb43cf73SLiu, Yi L .domain_id = vtd_get_domain_id(s, ce), 139363b88968SPeter Xu }; 139463b88968SPeter Xu 1395fb43cf73SLiu, Yi L return vtd_page_walk(s, ce, addr, addr + size, &info); 139663b88968SPeter Xu } 139763b88968SPeter Xu 139863b88968SPeter Xu static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as) 139963b88968SPeter Xu { 140095ecd3dfSPeter Xu int ret; 140195ecd3dfSPeter Xu VTDContextEntry ce; 1402c28b535dSPeter Xu IOMMUNotifier *n; 140395ecd3dfSPeter Xu 140495ecd3dfSPeter Xu ret = vtd_dev_to_context_entry(vtd_as->iommu_state, 140595ecd3dfSPeter Xu pci_bus_num(vtd_as->bus), 140695ecd3dfSPeter Xu vtd_as->devfn, &ce); 140795ecd3dfSPeter Xu if (ret) { 1408c28b535dSPeter Xu if (ret == -VTD_FR_CONTEXT_ENTRY_P) { 1409c28b535dSPeter Xu /* 1410c28b535dSPeter Xu * It's a valid scenario to have a context entry that is 1411c28b535dSPeter Xu * not present. For example, when a device is removed 1412c28b535dSPeter Xu * from an existing domain then the context entry will be 1413c28b535dSPeter Xu * zeroed by the guest before it was put into another 1414c28b535dSPeter Xu * domain. When this happens, instead of synchronizing 1415c28b535dSPeter Xu * the shadow pages we should invalidate all existing 1416c28b535dSPeter Xu * mappings and notify the backends. 1417c28b535dSPeter Xu */ 1418c28b535dSPeter Xu IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) { 1419c28b535dSPeter Xu vtd_address_space_unmap(vtd_as, n); 1420c28b535dSPeter Xu } 1421c28b535dSPeter Xu ret = 0; 1422c28b535dSPeter Xu } 142395ecd3dfSPeter Xu return ret; 142495ecd3dfSPeter Xu } 142595ecd3dfSPeter Xu 142695ecd3dfSPeter Xu return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX); 142763b88968SPeter Xu } 142863b88968SPeter Xu 1429dbaabb25SPeter Xu /* 1430fb43cf73SLiu, Yi L * Check if specific device is configed to bypass address 1431fb43cf73SLiu, Yi L * translation for DMA requests. In Scalable Mode, bypass 1432fb43cf73SLiu, Yi L * 1st-level translation or 2nd-level translation, it depends 1433fb43cf73SLiu, Yi L * on PGTT setting. 1434dbaabb25SPeter Xu */ 1435fb43cf73SLiu, Yi L static bool vtd_dev_pt_enabled(VTDAddressSpace *as) 1436dbaabb25SPeter Xu { 1437dbaabb25SPeter Xu IntelIOMMUState *s; 1438dbaabb25SPeter Xu VTDContextEntry ce; 1439fb43cf73SLiu, Yi L VTDPASIDEntry pe; 1440dbaabb25SPeter Xu int ret; 1441dbaabb25SPeter Xu 1442dbaabb25SPeter Xu assert(as); 1443dbaabb25SPeter Xu 1444fb43cf73SLiu, Yi L s = as->iommu_state; 1445fb43cf73SLiu, Yi L ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus), 1446fb43cf73SLiu, Yi L as->devfn, &ce); 1447fb43cf73SLiu, Yi L if (ret) { 1448dbaabb25SPeter Xu /* 1449dbaabb25SPeter Xu * Possibly failed to parse the context entry for some reason 1450dbaabb25SPeter Xu * (e.g., during init, or any guest configuration errors on 1451dbaabb25SPeter Xu * context entries). We should assume PT not enabled for 1452dbaabb25SPeter Xu * safety. 1453dbaabb25SPeter Xu */ 1454dbaabb25SPeter Xu return false; 1455dbaabb25SPeter Xu } 1456dbaabb25SPeter Xu 1457fb43cf73SLiu, Yi L if (s->root_scalable) { 1458fb43cf73SLiu, Yi L ret = vtd_ce_get_rid2pasid_entry(s, &ce, &pe); 1459fb43cf73SLiu, Yi L if (ret) { 1460fb43cf73SLiu, Yi L error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRId32, 1461fb43cf73SLiu, Yi L __func__, ret); 1462fb43cf73SLiu, Yi L return false; 1463fb43cf73SLiu, Yi L } 1464fb43cf73SLiu, Yi L return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT); 1465fb43cf73SLiu, Yi L } 1466fb43cf73SLiu, Yi L 1467fb43cf73SLiu, Yi L return (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH); 1468dbaabb25SPeter Xu } 1469dbaabb25SPeter Xu 1470dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */ 1471dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as) 1472dbaabb25SPeter Xu { 1473dbaabb25SPeter Xu bool use_iommu; 147466a4a031SPeter Xu /* Whether we need to take the BQL on our own */ 147566a4a031SPeter Xu bool take_bql = !qemu_mutex_iothread_locked(); 1476dbaabb25SPeter Xu 1477dbaabb25SPeter Xu assert(as); 1478dbaabb25SPeter Xu 14792a078b10SPeter Xu use_iommu = as->iommu_state->dmar_enabled && !vtd_dev_pt_enabled(as); 1480dbaabb25SPeter Xu 1481dbaabb25SPeter Xu trace_vtd_switch_address_space(pci_bus_num(as->bus), 1482dbaabb25SPeter Xu VTD_PCI_SLOT(as->devfn), 1483dbaabb25SPeter Xu VTD_PCI_FUNC(as->devfn), 1484dbaabb25SPeter Xu use_iommu); 1485dbaabb25SPeter Xu 148666a4a031SPeter Xu /* 148766a4a031SPeter Xu * It's possible that we reach here without BQL, e.g., when called 148866a4a031SPeter Xu * from vtd_pt_enable_fast_path(). However the memory APIs need 148966a4a031SPeter Xu * it. We'd better make sure we have had it already, or, take it. 149066a4a031SPeter Xu */ 149166a4a031SPeter Xu if (take_bql) { 149266a4a031SPeter Xu qemu_mutex_lock_iothread(); 149366a4a031SPeter Xu } 149466a4a031SPeter Xu 1495dbaabb25SPeter Xu /* Turn off first then on the other */ 1496dbaabb25SPeter Xu if (use_iommu) { 14974b519ef1SPeter Xu memory_region_set_enabled(&as->nodmar, false); 14983df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), true); 1499dbaabb25SPeter Xu } else { 15003df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), false); 15014b519ef1SPeter Xu memory_region_set_enabled(&as->nodmar, true); 1502dbaabb25SPeter Xu } 1503dbaabb25SPeter Xu 150466a4a031SPeter Xu if (take_bql) { 150566a4a031SPeter Xu qemu_mutex_unlock_iothread(); 150666a4a031SPeter Xu } 150766a4a031SPeter Xu 1508dbaabb25SPeter Xu return use_iommu; 1509dbaabb25SPeter Xu } 1510dbaabb25SPeter Xu 1511dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s) 1512dbaabb25SPeter Xu { 1513dbaabb25SPeter Xu GHashTableIter iter; 1514dbaabb25SPeter Xu VTDBus *vtd_bus; 1515dbaabb25SPeter Xu int i; 1516dbaabb25SPeter Xu 1517dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 1518dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 1519bf33cc75SPeter Xu for (i = 0; i < PCI_DEVFN_MAX; i++) { 1520dbaabb25SPeter Xu if (!vtd_bus->dev_as[i]) { 1521dbaabb25SPeter Xu continue; 1522dbaabb25SPeter Xu } 1523dbaabb25SPeter Xu vtd_switch_address_space(vtd_bus->dev_as[i]); 1524dbaabb25SPeter Xu } 1525dbaabb25SPeter Xu } 1526dbaabb25SPeter Xu } 1527dbaabb25SPeter Xu 15281da12ec4SLe Tan static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn) 15291da12ec4SLe Tan { 15301da12ec4SLe Tan return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL); 15311da12ec4SLe Tan } 15321da12ec4SLe Tan 15331da12ec4SLe Tan static const bool vtd_qualified_faults[] = { 15341da12ec4SLe Tan [VTD_FR_RESERVED] = false, 15351da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_P] = false, 15361da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_P] = true, 15371da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_INV] = true, 15381da12ec4SLe Tan [VTD_FR_ADDR_BEYOND_MGAW] = true, 15391da12ec4SLe Tan [VTD_FR_WRITE] = true, 15401da12ec4SLe Tan [VTD_FR_READ] = true, 15411da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_INV] = true, 15421da12ec4SLe Tan [VTD_FR_ROOT_TABLE_INV] = false, 15431da12ec4SLe Tan [VTD_FR_CONTEXT_TABLE_INV] = false, 15441da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_RSVD] = false, 15451da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_RSVD] = true, 15461da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_TT] = true, 1547fb43cf73SLiu, Yi L [VTD_FR_PASID_TABLE_INV] = false, 15481da12ec4SLe Tan [VTD_FR_RESERVED_ERR] = false, 15491da12ec4SLe Tan [VTD_FR_MAX] = false, 15501da12ec4SLe Tan }; 15511da12ec4SLe Tan 15521da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software 15531da12ec4SLe Tan * only if the FPD field in the context-entry used to process the faulting 15541da12ec4SLe Tan * request is 0. 15551da12ec4SLe Tan */ 15561da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault) 15571da12ec4SLe Tan { 15581da12ec4SLe Tan return vtd_qualified_faults[fault]; 15591da12ec4SLe Tan } 15601da12ec4SLe Tan 15611da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr) 15621da12ec4SLe Tan { 15631da12ec4SLe Tan return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST; 15641da12ec4SLe Tan } 15651da12ec4SLe Tan 1566dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id) 1567dbaabb25SPeter Xu { 1568dbaabb25SPeter Xu VTDBus *vtd_bus; 1569dbaabb25SPeter Xu VTDAddressSpace *vtd_as; 1570dbaabb25SPeter Xu bool success = false; 1571dbaabb25SPeter Xu 1572dbaabb25SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id)); 1573dbaabb25SPeter Xu if (!vtd_bus) { 1574dbaabb25SPeter Xu goto out; 1575dbaabb25SPeter Xu } 1576dbaabb25SPeter Xu 1577dbaabb25SPeter Xu vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)]; 1578dbaabb25SPeter Xu if (!vtd_as) { 1579dbaabb25SPeter Xu goto out; 1580dbaabb25SPeter Xu } 1581dbaabb25SPeter Xu 1582dbaabb25SPeter Xu if (vtd_switch_address_space(vtd_as) == false) { 1583dbaabb25SPeter Xu /* We switched off IOMMU region successfully. */ 1584dbaabb25SPeter Xu success = true; 1585dbaabb25SPeter Xu } 1586dbaabb25SPeter Xu 1587dbaabb25SPeter Xu out: 1588dbaabb25SPeter Xu trace_vtd_pt_enable_fast_path(source_id, success); 1589dbaabb25SPeter Xu } 1590dbaabb25SPeter Xu 15911da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu 15921da12ec4SLe Tan * translation. 159379e2b9aeSPaolo Bonzini * 159479e2b9aeSPaolo Bonzini * Called from RCU critical section. 159579e2b9aeSPaolo Bonzini * 15961da12ec4SLe Tan * @bus_num: The bus number 15971da12ec4SLe Tan * @devfn: The devfn, which is the combined of device and function number 15981da12ec4SLe Tan * @is_write: The access is a write operation 15991da12ec4SLe Tan * @entry: IOMMUTLBEntry that contain the addr to be translated and result 1600b9313021SPeter Xu * 1601b9313021SPeter Xu * Returns true if translation is successful, otherwise false. 16021da12ec4SLe Tan */ 1603b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, 16041da12ec4SLe Tan uint8_t devfn, hwaddr addr, bool is_write, 16051da12ec4SLe Tan IOMMUTLBEntry *entry) 16061da12ec4SLe Tan { 1607d92fa2dcSLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 16081da12ec4SLe Tan VTDContextEntry ce; 16097df953bdSKnut Omang uint8_t bus_num = pci_bus_num(bus); 16101d9efa73SPeter Xu VTDContextCacheEntry *cc_entry; 1611d66b969bSJason Wang uint64_t slpte, page_mask; 16121da12ec4SLe Tan uint32_t level; 16131da12ec4SLe Tan uint16_t source_id = vtd_make_source_id(bus_num, devfn); 16141da12ec4SLe Tan int ret_fr; 16151da12ec4SLe Tan bool is_fpd_set = false; 16161da12ec4SLe Tan bool reads = true; 16171da12ec4SLe Tan bool writes = true; 161807f7b733SPeter Xu uint8_t access_flags; 1619b5a280c0SLe Tan VTDIOTLBEntry *iotlb_entry; 16201da12ec4SLe Tan 1621046ab7e9SPeter Xu /* 1622046ab7e9SPeter Xu * We have standalone memory region for interrupt addresses, we 1623046ab7e9SPeter Xu * should never receive translation requests in this region. 16241da12ec4SLe Tan */ 1625046ab7e9SPeter Xu assert(!vtd_is_interrupt_addr(addr)); 1626046ab7e9SPeter Xu 16271d9efa73SPeter Xu vtd_iommu_lock(s); 16281d9efa73SPeter Xu 16291d9efa73SPeter Xu cc_entry = &vtd_as->context_cache_entry; 16301d9efa73SPeter Xu 1631b5a280c0SLe Tan /* Try to fetch slpte form IOTLB */ 1632b5a280c0SLe Tan iotlb_entry = vtd_lookup_iotlb(s, source_id, addr); 1633b5a280c0SLe Tan if (iotlb_entry) { 16346c441e1dSPeter Xu trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, 16356c441e1dSPeter Xu iotlb_entry->domain_id); 1636b5a280c0SLe Tan slpte = iotlb_entry->slpte; 163707f7b733SPeter Xu access_flags = iotlb_entry->access_flags; 1638d66b969bSJason Wang page_mask = iotlb_entry->mask; 1639b5a280c0SLe Tan goto out; 1640b5a280c0SLe Tan } 1641b9313021SPeter Xu 1642d92fa2dcSLe Tan /* Try to fetch context-entry from cache first */ 1643d92fa2dcSLe Tan if (cc_entry->context_cache_gen == s->context_cache_gen) { 16446c441e1dSPeter Xu trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi, 16456c441e1dSPeter Xu cc_entry->context_entry.lo, 16466c441e1dSPeter Xu cc_entry->context_cache_gen); 1647d92fa2dcSLe Tan ce = cc_entry->context_entry; 1648d92fa2dcSLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 1649fb43cf73SLiu, Yi L if (!is_fpd_set && s->root_scalable) { 1650fb43cf73SLiu, Yi L ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); 1651fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 1652fb43cf73SLiu, Yi L } 1653d92fa2dcSLe Tan } else { 16541da12ec4SLe Tan ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce); 16551da12ec4SLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 1656fb43cf73SLiu, Yi L if (!ret_fr && !is_fpd_set && s->root_scalable) { 1657fb43cf73SLiu, Yi L ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); 16581da12ec4SLe Tan } 1659fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 1660d92fa2dcSLe Tan /* Update context-cache */ 16616c441e1dSPeter Xu trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo, 16626c441e1dSPeter Xu cc_entry->context_cache_gen, 16636c441e1dSPeter Xu s->context_cache_gen); 1664d92fa2dcSLe Tan cc_entry->context_entry = ce; 1665d92fa2dcSLe Tan cc_entry->context_cache_gen = s->context_cache_gen; 1666d92fa2dcSLe Tan } 16671da12ec4SLe Tan 1668dbaabb25SPeter Xu /* 1669dbaabb25SPeter Xu * We don't need to translate for pass-through context entries. 1670dbaabb25SPeter Xu * Also, let's ignore IOTLB caching as well for PT devices. 1671dbaabb25SPeter Xu */ 1672dbaabb25SPeter Xu if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) { 1673892721d9SPeter Xu entry->iova = addr & VTD_PAGE_MASK_4K; 1674dbaabb25SPeter Xu entry->translated_addr = entry->iova; 1675892721d9SPeter Xu entry->addr_mask = ~VTD_PAGE_MASK_4K; 1676dbaabb25SPeter Xu entry->perm = IOMMU_RW; 1677dbaabb25SPeter Xu trace_vtd_translate_pt(source_id, entry->iova); 1678dbaabb25SPeter Xu 1679dbaabb25SPeter Xu /* 1680dbaabb25SPeter Xu * When this happens, it means firstly caching-mode is not 1681dbaabb25SPeter Xu * enabled, and this is the first passthrough translation for 1682dbaabb25SPeter Xu * the device. Let's enable the fast path for passthrough. 1683dbaabb25SPeter Xu * 1684dbaabb25SPeter Xu * When passthrough is disabled again for the device, we can 1685dbaabb25SPeter Xu * capture it via the context entry invalidation, then the 1686dbaabb25SPeter Xu * IOMMU region can be swapped back. 1687dbaabb25SPeter Xu */ 1688dbaabb25SPeter Xu vtd_pt_enable_fast_path(s, source_id); 16891d9efa73SPeter Xu vtd_iommu_unlock(s); 1690b9313021SPeter Xu return true; 1691dbaabb25SPeter Xu } 1692dbaabb25SPeter Xu 1693fb43cf73SLiu, Yi L ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level, 169437f51384SPrasad Singamsetty &reads, &writes, s->aw_bits); 1695fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 16961da12ec4SLe Tan 1697d66b969bSJason Wang page_mask = vtd_slpt_level_page_mask(level); 169807f7b733SPeter Xu access_flags = IOMMU_ACCESS_FLAG(reads, writes); 1699fb43cf73SLiu, Yi L vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce), addr, slpte, 170007f7b733SPeter Xu access_flags, level); 1701b5a280c0SLe Tan out: 17021d9efa73SPeter Xu vtd_iommu_unlock(s); 1703d66b969bSJason Wang entry->iova = addr & page_mask; 170437f51384SPrasad Singamsetty entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask; 1705d66b969bSJason Wang entry->addr_mask = ~page_mask; 170607f7b733SPeter Xu entry->perm = access_flags; 1707b9313021SPeter Xu return true; 1708b9313021SPeter Xu 1709b9313021SPeter Xu error: 17101d9efa73SPeter Xu vtd_iommu_unlock(s); 1711b9313021SPeter Xu entry->iova = 0; 1712b9313021SPeter Xu entry->translated_addr = 0; 1713b9313021SPeter Xu entry->addr_mask = 0; 1714b9313021SPeter Xu entry->perm = IOMMU_NONE; 1715b9313021SPeter Xu return false; 17161da12ec4SLe Tan } 17171da12ec4SLe Tan 17181da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s) 17191da12ec4SLe Tan { 17201da12ec4SLe Tan s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 172137f51384SPrasad Singamsetty s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits); 17221da12ec4SLe Tan 17232811af3bSPeter Xu vtd_update_scalable_state(s); 17242811af3bSPeter Xu 172581fb1e64SPeter Xu trace_vtd_reg_dmar_root(s->root, s->root_scalable); 17261da12ec4SLe Tan } 17271da12ec4SLe Tan 172802a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global, 172902a2cbc8SPeter Xu uint32_t index, uint32_t mask) 173002a2cbc8SPeter Xu { 173102a2cbc8SPeter Xu x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask); 173202a2cbc8SPeter Xu } 173302a2cbc8SPeter Xu 1734a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s) 1735a5861439SPeter Xu { 1736a5861439SPeter Xu uint64_t value = 0; 1737a5861439SPeter Xu value = vtd_get_quad_raw(s, DMAR_IRTA_REG); 1738a5861439SPeter Xu s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1); 173937f51384SPrasad Singamsetty s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits); 174028589311SJan Kiszka s->intr_eime = value & VTD_IRTA_EIME; 1741a5861439SPeter Xu 174202a2cbc8SPeter Xu /* Notify global invalidation */ 174302a2cbc8SPeter Xu vtd_iec_notify_all(s, true, 0, 0); 1744a5861439SPeter Xu 17457feb51b7SPeter Xu trace_vtd_reg_ir_root(s->intr_root, s->intr_size); 1746a5861439SPeter Xu } 1747a5861439SPeter Xu 1748dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s) 1749dd4d607eSPeter Xu { 1750b4a4ba0dSPeter Xu VTDAddressSpace *vtd_as; 1751dd4d607eSPeter Xu 1752b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 175363b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1754dd4d607eSPeter Xu } 1755dd4d607eSPeter Xu } 1756dd4d607eSPeter Xu 1757d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s) 1758d92fa2dcSLe Tan { 1759bc535e59SPeter Xu trace_vtd_inv_desc_cc_global(); 17601d9efa73SPeter Xu /* Protects context cache */ 17611d9efa73SPeter Xu vtd_iommu_lock(s); 1762d92fa2dcSLe Tan s->context_cache_gen++; 1763d92fa2dcSLe Tan if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) { 17641d9efa73SPeter Xu vtd_reset_context_cache_locked(s); 1765d92fa2dcSLe Tan } 17661d9efa73SPeter Xu vtd_iommu_unlock(s); 17672cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 1768dd4d607eSPeter Xu /* 1769dd4d607eSPeter Xu * From VT-d spec 6.5.2.1, a global context entry invalidation 1770dd4d607eSPeter Xu * should be followed by a IOTLB global invalidation, so we should 1771dd4d607eSPeter Xu * be safe even without this. Hoewever, let's replay the region as 1772dd4d607eSPeter Xu * well to be safer, and go back here when we need finer tunes for 1773dd4d607eSPeter Xu * VT-d emulation codes. 1774dd4d607eSPeter Xu */ 1775dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1776d92fa2dcSLe Tan } 1777d92fa2dcSLe Tan 1778d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation. 1779d92fa2dcSLe Tan * @func_mask: FM field after shifting 1780d92fa2dcSLe Tan */ 1781d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s, 1782d92fa2dcSLe Tan uint16_t source_id, 1783d92fa2dcSLe Tan uint16_t func_mask) 1784d92fa2dcSLe Tan { 1785d92fa2dcSLe Tan uint16_t mask; 17867df953bdSKnut Omang VTDBus *vtd_bus; 1787d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 1788bc535e59SPeter Xu uint8_t bus_n, devfn; 1789d92fa2dcSLe Tan uint16_t devfn_it; 1790d92fa2dcSLe Tan 1791bc535e59SPeter Xu trace_vtd_inv_desc_cc_devices(source_id, func_mask); 1792bc535e59SPeter Xu 1793d92fa2dcSLe Tan switch (func_mask & 3) { 1794d92fa2dcSLe Tan case 0: 1795d92fa2dcSLe Tan mask = 0; /* No bits in the SID field masked */ 1796d92fa2dcSLe Tan break; 1797d92fa2dcSLe Tan case 1: 1798d92fa2dcSLe Tan mask = 4; /* Mask bit 2 in the SID field */ 1799d92fa2dcSLe Tan break; 1800d92fa2dcSLe Tan case 2: 1801d92fa2dcSLe Tan mask = 6; /* Mask bit 2:1 in the SID field */ 1802d92fa2dcSLe Tan break; 1803d92fa2dcSLe Tan case 3: 1804d92fa2dcSLe Tan mask = 7; /* Mask bit 2:0 in the SID field */ 1805d92fa2dcSLe Tan break; 1806d92fa2dcSLe Tan } 18076cb99accSPeter Xu mask = ~mask; 1808bc535e59SPeter Xu 1809bc535e59SPeter Xu bus_n = VTD_SID_TO_BUS(source_id); 1810bc535e59SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, bus_n); 18117df953bdSKnut Omang if (vtd_bus) { 1812d92fa2dcSLe Tan devfn = VTD_SID_TO_DEVFN(source_id); 1813bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 18147df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 1815d92fa2dcSLe Tan if (vtd_as && ((devfn_it & mask) == (devfn & mask))) { 1816bc535e59SPeter Xu trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it), 1817bc535e59SPeter Xu VTD_PCI_FUNC(devfn_it)); 18181d9efa73SPeter Xu vtd_iommu_lock(s); 1819d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 18201d9efa73SPeter Xu vtd_iommu_unlock(s); 1821dd4d607eSPeter Xu /* 1822dbaabb25SPeter Xu * Do switch address space when needed, in case if the 1823dbaabb25SPeter Xu * device passthrough bit is switched. 1824dbaabb25SPeter Xu */ 1825dbaabb25SPeter Xu vtd_switch_address_space(vtd_as); 1826dbaabb25SPeter Xu /* 1827dd4d607eSPeter Xu * So a device is moving out of (or moving into) a 182863b88968SPeter Xu * domain, resync the shadow page table. 1829dd4d607eSPeter Xu * This won't bring bad even if we have no such 1830dd4d607eSPeter Xu * notifier registered - the IOMMU notification 1831dd4d607eSPeter Xu * framework will skip MAP notifications if that 1832dd4d607eSPeter Xu * happened. 1833dd4d607eSPeter Xu */ 183463b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1835d92fa2dcSLe Tan } 1836d92fa2dcSLe Tan } 1837d92fa2dcSLe Tan } 1838d92fa2dcSLe Tan } 1839d92fa2dcSLe Tan 18401da12ec4SLe Tan /* Context-cache invalidation 18411da12ec4SLe Tan * Returns the Context Actual Invalidation Granularity. 18421da12ec4SLe Tan * @val: the content of the CCMD_REG 18431da12ec4SLe Tan */ 18441da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val) 18451da12ec4SLe Tan { 18461da12ec4SLe Tan uint64_t caig; 18471da12ec4SLe Tan uint64_t type = val & VTD_CCMD_CIRG_MASK; 18481da12ec4SLe Tan 18491da12ec4SLe Tan switch (type) { 18501da12ec4SLe Tan case VTD_CCMD_DOMAIN_INVL: 1851d92fa2dcSLe Tan /* Fall through */ 1852d92fa2dcSLe Tan case VTD_CCMD_GLOBAL_INVL: 1853d92fa2dcSLe Tan caig = VTD_CCMD_GLOBAL_INVL_A; 1854d92fa2dcSLe Tan vtd_context_global_invalidate(s); 18551da12ec4SLe Tan break; 18561da12ec4SLe Tan 18571da12ec4SLe Tan case VTD_CCMD_DEVICE_INVL: 18581da12ec4SLe Tan caig = VTD_CCMD_DEVICE_INVL_A; 1859d92fa2dcSLe Tan vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val)); 18601da12ec4SLe Tan break; 18611da12ec4SLe Tan 18621da12ec4SLe Tan default: 18631376211fSPeter Xu error_report_once("%s: invalid context: 0x%" PRIx64, 18641376211fSPeter Xu __func__, val); 18651da12ec4SLe Tan caig = 0; 18661da12ec4SLe Tan } 18671da12ec4SLe Tan return caig; 18681da12ec4SLe Tan } 18691da12ec4SLe Tan 1870b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s) 1871b5a280c0SLe Tan { 18727feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_global(); 1873b5a280c0SLe Tan vtd_reset_iotlb(s); 1874dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1875b5a280c0SLe Tan } 1876b5a280c0SLe Tan 1877b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) 1878b5a280c0SLe Tan { 1879dd4d607eSPeter Xu VTDContextEntry ce; 1880dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 1881dd4d607eSPeter Xu 18827feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_domain(domain_id); 18837feb51b7SPeter Xu 18841d9efa73SPeter Xu vtd_iommu_lock(s); 1885b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain, 1886b5a280c0SLe Tan &domain_id); 18871d9efa73SPeter Xu vtd_iommu_unlock(s); 1888dd4d607eSPeter Xu 1889b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 1890dd4d607eSPeter Xu if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 1891dd4d607eSPeter Xu vtd_as->devfn, &ce) && 1892fb43cf73SLiu, Yi L domain_id == vtd_get_domain_id(s, &ce)) { 189363b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1894dd4d607eSPeter Xu } 1895dd4d607eSPeter Xu } 1896dd4d607eSPeter Xu } 1897dd4d607eSPeter Xu 1898dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, 1899dd4d607eSPeter Xu uint16_t domain_id, hwaddr addr, 1900dd4d607eSPeter Xu uint8_t am) 1901dd4d607eSPeter Xu { 1902b4a4ba0dSPeter Xu VTDAddressSpace *vtd_as; 1903dd4d607eSPeter Xu VTDContextEntry ce; 1904dd4d607eSPeter Xu int ret; 19054f8a62a9SPeter Xu hwaddr size = (1 << am) * VTD_PAGE_SIZE; 1906dd4d607eSPeter Xu 1907b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { 1908dd4d607eSPeter Xu ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 1909dd4d607eSPeter Xu vtd_as->devfn, &ce); 1910fb43cf73SLiu, Yi L if (!ret && domain_id == vtd_get_domain_id(s, &ce)) { 19114f8a62a9SPeter Xu if (vtd_as_has_map_notifier(vtd_as)) { 19124f8a62a9SPeter Xu /* 19134f8a62a9SPeter Xu * As long as we have MAP notifications registered in 19144f8a62a9SPeter Xu * any of our IOMMU notifiers, we need to sync the 19154f8a62a9SPeter Xu * shadow page table. 19164f8a62a9SPeter Xu */ 191763b88968SPeter Xu vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size); 19184f8a62a9SPeter Xu } else { 19194f8a62a9SPeter Xu /* 19204f8a62a9SPeter Xu * For UNMAP-only notifiers, we don't need to walk the 19214f8a62a9SPeter Xu * page tables. We just deliver the PSI down to 19224f8a62a9SPeter Xu * invalidate caches. 19234f8a62a9SPeter Xu */ 19244f8a62a9SPeter Xu IOMMUTLBEntry entry = { 19254f8a62a9SPeter Xu .target_as = &address_space_memory, 19264f8a62a9SPeter Xu .iova = addr, 19274f8a62a9SPeter Xu .translated_addr = 0, 19284f8a62a9SPeter Xu .addr_mask = size - 1, 19294f8a62a9SPeter Xu .perm = IOMMU_NONE, 19304f8a62a9SPeter Xu }; 1931cb1efcf4SPeter Maydell memory_region_notify_iommu(&vtd_as->iommu, 0, entry); 19324f8a62a9SPeter Xu } 1933dd4d607eSPeter Xu } 1934dd4d607eSPeter Xu } 1935b5a280c0SLe Tan } 1936b5a280c0SLe Tan 1937b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, 1938b5a280c0SLe Tan hwaddr addr, uint8_t am) 1939b5a280c0SLe Tan { 1940b5a280c0SLe Tan VTDIOTLBPageInvInfo info; 1941b5a280c0SLe Tan 19427feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am); 19437feb51b7SPeter Xu 1944b5a280c0SLe Tan assert(am <= VTD_MAMV); 1945b5a280c0SLe Tan info.domain_id = domain_id; 1946d66b969bSJason Wang info.addr = addr; 1947b5a280c0SLe Tan info.mask = ~((1 << am) - 1); 19481d9efa73SPeter Xu vtd_iommu_lock(s); 1949b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); 19501d9efa73SPeter Xu vtd_iommu_unlock(s); 1951dd4d607eSPeter Xu vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); 1952b5a280c0SLe Tan } 1953b5a280c0SLe Tan 19541da12ec4SLe Tan /* Flush IOTLB 19551da12ec4SLe Tan * Returns the IOTLB Actual Invalidation Granularity. 19561da12ec4SLe Tan * @val: the content of the IOTLB_REG 19571da12ec4SLe Tan */ 19581da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val) 19591da12ec4SLe Tan { 19601da12ec4SLe Tan uint64_t iaig; 19611da12ec4SLe Tan uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK; 1962b5a280c0SLe Tan uint16_t domain_id; 1963b5a280c0SLe Tan hwaddr addr; 1964b5a280c0SLe Tan uint8_t am; 19651da12ec4SLe Tan 19661da12ec4SLe Tan switch (type) { 19671da12ec4SLe Tan case VTD_TLB_GLOBAL_FLUSH: 19681da12ec4SLe Tan iaig = VTD_TLB_GLOBAL_FLUSH_A; 1969b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 19701da12ec4SLe Tan break; 19711da12ec4SLe Tan 19721da12ec4SLe Tan case VTD_TLB_DSI_FLUSH: 1973b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 19741da12ec4SLe Tan iaig = VTD_TLB_DSI_FLUSH_A; 1975b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 19761da12ec4SLe Tan break; 19771da12ec4SLe Tan 19781da12ec4SLe Tan case VTD_TLB_PSI_FLUSH: 1979b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 1980b5a280c0SLe Tan addr = vtd_get_quad_raw(s, DMAR_IVA_REG); 1981b5a280c0SLe Tan am = VTD_IVA_AM(addr); 1982b5a280c0SLe Tan addr = VTD_IVA_ADDR(addr); 1983b5a280c0SLe Tan if (am > VTD_MAMV) { 19841376211fSPeter Xu error_report_once("%s: address mask overflow: 0x%" PRIx64, 19851376211fSPeter Xu __func__, vtd_get_quad_raw(s, DMAR_IVA_REG)); 1986b5a280c0SLe Tan iaig = 0; 1987b5a280c0SLe Tan break; 1988b5a280c0SLe Tan } 19891da12ec4SLe Tan iaig = VTD_TLB_PSI_FLUSH_A; 1990b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 19911da12ec4SLe Tan break; 19921da12ec4SLe Tan 19931da12ec4SLe Tan default: 19941376211fSPeter Xu error_report_once("%s: invalid granularity: 0x%" PRIx64, 19951376211fSPeter Xu __func__, val); 19961da12ec4SLe Tan iaig = 0; 19971da12ec4SLe Tan } 19981da12ec4SLe Tan return iaig; 19991da12ec4SLe Tan } 20001da12ec4SLe Tan 20018991c460SLadi Prosek static void vtd_fetch_inv_desc(IntelIOMMUState *s); 2002ed7b8fbcSLe Tan 2003ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s) 2004ed7b8fbcSLe Tan { 2005ed7b8fbcSLe Tan return s->qi_enabled && (s->iq_tail == s->iq_head) && 2006ed7b8fbcSLe Tan (s->iq_last_desc_type == VTD_INV_DESC_WAIT); 2007ed7b8fbcSLe Tan } 2008ed7b8fbcSLe Tan 2009ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en) 2010ed7b8fbcSLe Tan { 2011ed7b8fbcSLe Tan uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG); 2012ed7b8fbcSLe Tan 20137feb51b7SPeter Xu trace_vtd_inv_qi_enable(en); 20147feb51b7SPeter Xu 2015ed7b8fbcSLe Tan if (en) { 201637f51384SPrasad Singamsetty s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits); 2017ed7b8fbcSLe Tan /* 2^(x+8) entries */ 2018c0c1d351SLiu, Yi L s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8 - (s->iq_dw ? 1 : 0)); 2019ed7b8fbcSLe Tan s->qi_enabled = true; 20207feb51b7SPeter Xu trace_vtd_inv_qi_setup(s->iq, s->iq_size); 2021ed7b8fbcSLe Tan /* Ok - report back to driver */ 2022ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES); 20238991c460SLadi Prosek 20248991c460SLadi Prosek if (s->iq_tail != 0) { 20258991c460SLadi Prosek /* 20268991c460SLadi Prosek * This is a spec violation but Windows guests are known to set up 20278991c460SLadi Prosek * Queued Invalidation this way so we allow the write and process 20288991c460SLadi Prosek * Invalidation Descriptors right away. 20298991c460SLadi Prosek */ 20308991c460SLadi Prosek trace_vtd_warn_invalid_qi_tail(s->iq_tail); 20318991c460SLadi Prosek if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 20328991c460SLadi Prosek vtd_fetch_inv_desc(s); 20338991c460SLadi Prosek } 2034ed7b8fbcSLe Tan } 2035ed7b8fbcSLe Tan } else { 2036ed7b8fbcSLe Tan if (vtd_queued_inv_disable_check(s)) { 2037ed7b8fbcSLe Tan /* disable Queued Invalidation */ 2038ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 0); 2039ed7b8fbcSLe Tan s->iq_head = 0; 2040ed7b8fbcSLe Tan s->qi_enabled = false; 2041ed7b8fbcSLe Tan /* Ok - report back to driver */ 2042ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0); 2043ed7b8fbcSLe Tan } else { 20444e4abd11SPeter Xu error_report_once("%s: detected improper state when disable QI " 20454e4abd11SPeter Xu "(head=0x%x, tail=0x%x, last_type=%d)", 20464e4abd11SPeter Xu __func__, 20474e4abd11SPeter Xu s->iq_head, s->iq_tail, s->iq_last_desc_type); 2048ed7b8fbcSLe Tan } 2049ed7b8fbcSLe Tan } 2050ed7b8fbcSLe Tan } 2051ed7b8fbcSLe Tan 20521da12ec4SLe Tan /* Set Root Table Pointer */ 20531da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s) 20541da12ec4SLe Tan { 20551da12ec4SLe Tan vtd_root_table_setup(s); 20561da12ec4SLe Tan /* Ok - report back to driver */ 20571da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS); 20582cc9ddccSPeter Xu vtd_reset_caches(s); 20592cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 20601da12ec4SLe Tan } 20611da12ec4SLe Tan 2062a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */ 2063a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s) 2064a5861439SPeter Xu { 2065a5861439SPeter Xu vtd_interrupt_remap_table_setup(s); 2066a5861439SPeter Xu /* Ok - report back to driver */ 2067a5861439SPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS); 2068a5861439SPeter Xu } 2069a5861439SPeter Xu 20701da12ec4SLe Tan /* Handle Translation Enable/Disable */ 20711da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en) 20721da12ec4SLe Tan { 2073558e0024SPeter Xu if (s->dmar_enabled == en) { 2074558e0024SPeter Xu return; 2075558e0024SPeter Xu } 2076558e0024SPeter Xu 20777feb51b7SPeter Xu trace_vtd_dmar_enable(en); 20781da12ec4SLe Tan 20791da12ec4SLe Tan if (en) { 20801da12ec4SLe Tan s->dmar_enabled = true; 20811da12ec4SLe Tan /* Ok - report back to driver */ 20821da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES); 20831da12ec4SLe Tan } else { 20841da12ec4SLe Tan s->dmar_enabled = false; 20851da12ec4SLe Tan 20861da12ec4SLe Tan /* Clear the index of Fault Recording Register */ 20871da12ec4SLe Tan s->next_frcd_reg = 0; 20881da12ec4SLe Tan /* Ok - report back to driver */ 20891da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0); 20901da12ec4SLe Tan } 2091558e0024SPeter Xu 20922cc9ddccSPeter Xu vtd_reset_caches(s); 20932cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 20941da12ec4SLe Tan } 20951da12ec4SLe Tan 209680de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */ 209780de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) 209880de52baSPeter Xu { 20997feb51b7SPeter Xu trace_vtd_ir_enable(en); 210080de52baSPeter Xu 210180de52baSPeter Xu if (en) { 210280de52baSPeter Xu s->intr_enabled = true; 210380de52baSPeter Xu /* Ok - report back to driver */ 210480de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES); 210580de52baSPeter Xu } else { 210680de52baSPeter Xu s->intr_enabled = false; 210780de52baSPeter Xu /* Ok - report back to driver */ 210880de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0); 210980de52baSPeter Xu } 211080de52baSPeter Xu } 211180de52baSPeter Xu 21121da12ec4SLe Tan /* Handle write to Global Command Register */ 21131da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s) 21141da12ec4SLe Tan { 21151da12ec4SLe Tan uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG); 21161da12ec4SLe Tan uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG); 21171da12ec4SLe Tan uint32_t changed = status ^ val; 21181da12ec4SLe Tan 21197feb51b7SPeter Xu trace_vtd_reg_write_gcmd(status, val); 21201da12ec4SLe Tan if (changed & VTD_GCMD_TE) { 21211da12ec4SLe Tan /* Translation enable/disable */ 21221da12ec4SLe Tan vtd_handle_gcmd_te(s, val & VTD_GCMD_TE); 21231da12ec4SLe Tan } 21241da12ec4SLe Tan if (val & VTD_GCMD_SRTP) { 21251da12ec4SLe Tan /* Set/update the root-table pointer */ 21261da12ec4SLe Tan vtd_handle_gcmd_srtp(s); 21271da12ec4SLe Tan } 2128ed7b8fbcSLe Tan if (changed & VTD_GCMD_QIE) { 2129ed7b8fbcSLe Tan /* Queued Invalidation Enable */ 2130ed7b8fbcSLe Tan vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE); 2131ed7b8fbcSLe Tan } 2132a5861439SPeter Xu if (val & VTD_GCMD_SIRTP) { 2133a5861439SPeter Xu /* Set/update the interrupt remapping root-table pointer */ 2134a5861439SPeter Xu vtd_handle_gcmd_sirtp(s); 2135a5861439SPeter Xu } 213680de52baSPeter Xu if (changed & VTD_GCMD_IRE) { 213780de52baSPeter Xu /* Interrupt remap enable/disable */ 213880de52baSPeter Xu vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE); 213980de52baSPeter Xu } 21401da12ec4SLe Tan } 21411da12ec4SLe Tan 21421da12ec4SLe Tan /* Handle write to Context Command Register */ 21431da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s) 21441da12ec4SLe Tan { 21451da12ec4SLe Tan uint64_t ret; 21461da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG); 21471da12ec4SLe Tan 21481da12ec4SLe Tan /* Context-cache invalidation request */ 21491da12ec4SLe Tan if (val & VTD_CCMD_ICC) { 2150ed7b8fbcSLe Tan if (s->qi_enabled) { 21511376211fSPeter Xu error_report_once("Queued Invalidation enabled, " 2152ed7b8fbcSLe Tan "should not use register-based invalidation"); 2153ed7b8fbcSLe Tan return; 2154ed7b8fbcSLe Tan } 21551da12ec4SLe Tan ret = vtd_context_cache_invalidate(s, val); 21561da12ec4SLe Tan /* Invalidation completed. Change something to show */ 21571da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL); 21581da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK, 21591da12ec4SLe Tan ret); 21601da12ec4SLe Tan } 21611da12ec4SLe Tan } 21621da12ec4SLe Tan 21631da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */ 21641da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s) 21651da12ec4SLe Tan { 21661da12ec4SLe Tan uint64_t ret; 21671da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG); 21681da12ec4SLe Tan 21691da12ec4SLe Tan /* IOTLB invalidation request */ 21701da12ec4SLe Tan if (val & VTD_TLB_IVT) { 2171ed7b8fbcSLe Tan if (s->qi_enabled) { 21721376211fSPeter Xu error_report_once("Queued Invalidation enabled, " 21731376211fSPeter Xu "should not use register-based invalidation"); 2174ed7b8fbcSLe Tan return; 2175ed7b8fbcSLe Tan } 21761da12ec4SLe Tan ret = vtd_iotlb_flush(s, val); 21771da12ec4SLe Tan /* Invalidation completed. Change something to show */ 21781da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL); 21791da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, 21801da12ec4SLe Tan VTD_TLB_FLUSH_GRANU_MASK_A, ret); 21811da12ec4SLe Tan } 21821da12ec4SLe Tan } 21831da12ec4SLe Tan 2184ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */ 2185c0c1d351SLiu, Yi L static bool vtd_get_inv_desc(IntelIOMMUState *s, 2186ed7b8fbcSLe Tan VTDInvDesc *inv_desc) 2187ed7b8fbcSLe Tan { 2188c0c1d351SLiu, Yi L dma_addr_t base_addr = s->iq; 2189c0c1d351SLiu, Yi L uint32_t offset = s->iq_head; 2190c0c1d351SLiu, Yi L uint32_t dw = s->iq_dw ? 32 : 16; 2191c0c1d351SLiu, Yi L dma_addr_t addr = base_addr + offset * dw; 2192c0c1d351SLiu, Yi L 2193c0c1d351SLiu, Yi L if (dma_memory_read(&address_space_memory, addr, inv_desc, dw)) { 2194c0c1d351SLiu, Yi L error_report_once("Read INV DESC failed."); 2195ed7b8fbcSLe Tan return false; 2196ed7b8fbcSLe Tan } 2197ed7b8fbcSLe Tan inv_desc->lo = le64_to_cpu(inv_desc->lo); 2198ed7b8fbcSLe Tan inv_desc->hi = le64_to_cpu(inv_desc->hi); 2199c0c1d351SLiu, Yi L if (dw == 32) { 2200c0c1d351SLiu, Yi L inv_desc->val[2] = le64_to_cpu(inv_desc->val[2]); 2201c0c1d351SLiu, Yi L inv_desc->val[3] = le64_to_cpu(inv_desc->val[3]); 2202c0c1d351SLiu, Yi L } 2203ed7b8fbcSLe Tan return true; 2204ed7b8fbcSLe Tan } 2205ed7b8fbcSLe Tan 2206ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 2207ed7b8fbcSLe Tan { 2208ed7b8fbcSLe Tan if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) || 2209ed7b8fbcSLe Tan (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) { 2210095955b2SPeter Xu error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64 2211095955b2SPeter Xu " (reserved nonzero)", __func__, inv_desc->hi, 2212095955b2SPeter Xu inv_desc->lo); 2213ed7b8fbcSLe Tan return false; 2214ed7b8fbcSLe Tan } 2215ed7b8fbcSLe Tan if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) { 2216ed7b8fbcSLe Tan /* Status Write */ 2217ed7b8fbcSLe Tan uint32_t status_data = (uint32_t)(inv_desc->lo >> 2218ed7b8fbcSLe Tan VTD_INV_DESC_WAIT_DATA_SHIFT); 2219ed7b8fbcSLe Tan 2220ed7b8fbcSLe Tan assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF)); 2221ed7b8fbcSLe Tan 2222ed7b8fbcSLe Tan /* FIXME: need to be masked with HAW? */ 2223ed7b8fbcSLe Tan dma_addr_t status_addr = inv_desc->hi; 2224bc535e59SPeter Xu trace_vtd_inv_desc_wait_sw(status_addr, status_data); 2225ed7b8fbcSLe Tan status_data = cpu_to_le32(status_data); 2226ed7b8fbcSLe Tan if (dma_memory_write(&address_space_memory, status_addr, &status_data, 2227ed7b8fbcSLe Tan sizeof(status_data))) { 2228bc535e59SPeter Xu trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo); 2229ed7b8fbcSLe Tan return false; 2230ed7b8fbcSLe Tan } 2231ed7b8fbcSLe Tan } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) { 2232ed7b8fbcSLe Tan /* Interrupt flag */ 2233ed7b8fbcSLe Tan vtd_generate_completion_event(s); 2234ed7b8fbcSLe Tan } else { 2235095955b2SPeter Xu error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64 2236095955b2SPeter Xu " (unknown type)", __func__, inv_desc->hi, 2237095955b2SPeter Xu inv_desc->lo); 2238ed7b8fbcSLe Tan return false; 2239ed7b8fbcSLe Tan } 2240ed7b8fbcSLe Tan return true; 2241ed7b8fbcSLe Tan } 2242ed7b8fbcSLe Tan 2243d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s, 2244d92fa2dcSLe Tan VTDInvDesc *inv_desc) 2245d92fa2dcSLe Tan { 2246bc535e59SPeter Xu uint16_t sid, fmask; 2247bc535e59SPeter Xu 2248d92fa2dcSLe Tan if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) { 2249095955b2SPeter Xu error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64 2250095955b2SPeter Xu " (reserved nonzero)", __func__, inv_desc->hi, 2251095955b2SPeter Xu inv_desc->lo); 2252d92fa2dcSLe Tan return false; 2253d92fa2dcSLe Tan } 2254d92fa2dcSLe Tan switch (inv_desc->lo & VTD_INV_DESC_CC_G) { 2255d92fa2dcSLe Tan case VTD_INV_DESC_CC_DOMAIN: 2256bc535e59SPeter Xu trace_vtd_inv_desc_cc_domain( 2257d92fa2dcSLe Tan (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo)); 2258d92fa2dcSLe Tan /* Fall through */ 2259d92fa2dcSLe Tan case VTD_INV_DESC_CC_GLOBAL: 2260d92fa2dcSLe Tan vtd_context_global_invalidate(s); 2261d92fa2dcSLe Tan break; 2262d92fa2dcSLe Tan 2263d92fa2dcSLe Tan case VTD_INV_DESC_CC_DEVICE: 2264bc535e59SPeter Xu sid = VTD_INV_DESC_CC_SID(inv_desc->lo); 2265bc535e59SPeter Xu fmask = VTD_INV_DESC_CC_FM(inv_desc->lo); 2266bc535e59SPeter Xu vtd_context_device_invalidate(s, sid, fmask); 2267d92fa2dcSLe Tan break; 2268d92fa2dcSLe Tan 2269d92fa2dcSLe Tan default: 2270095955b2SPeter Xu error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64 2271095955b2SPeter Xu " (invalid type)", __func__, inv_desc->hi, 2272095955b2SPeter Xu inv_desc->lo); 2273d92fa2dcSLe Tan return false; 2274d92fa2dcSLe Tan } 2275d92fa2dcSLe Tan return true; 2276d92fa2dcSLe Tan } 2277d92fa2dcSLe Tan 2278b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 2279b5a280c0SLe Tan { 2280b5a280c0SLe Tan uint16_t domain_id; 2281b5a280c0SLe Tan uint8_t am; 2282b5a280c0SLe Tan hwaddr addr; 2283b5a280c0SLe Tan 2284b5a280c0SLe Tan if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) || 2285b5a280c0SLe Tan (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) { 2286095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2287095955b2SPeter Xu ", lo=0x%"PRIx64" (reserved bits unzero)\n", 2288095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo); 2289b5a280c0SLe Tan return false; 2290b5a280c0SLe Tan } 2291b5a280c0SLe Tan 2292b5a280c0SLe Tan switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) { 2293b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_GLOBAL: 2294b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 2295b5a280c0SLe Tan break; 2296b5a280c0SLe Tan 2297b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_DOMAIN: 2298b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 2299b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 2300b5a280c0SLe Tan break; 2301b5a280c0SLe Tan 2302b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_PAGE: 2303b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 2304b5a280c0SLe Tan addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi); 2305b5a280c0SLe Tan am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi); 2306b5a280c0SLe Tan if (am > VTD_MAMV) { 2307095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2308095955b2SPeter Xu ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)\n", 2309095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo, 2310095955b2SPeter Xu am, (unsigned)VTD_MAMV); 2311b5a280c0SLe Tan return false; 2312b5a280c0SLe Tan } 2313b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 2314b5a280c0SLe Tan break; 2315b5a280c0SLe Tan 2316b5a280c0SLe Tan default: 2317095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2318095955b2SPeter Xu ", lo=0x%"PRIx64" (type mismatch: 0x%llx)\n", 2319095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo, 2320095955b2SPeter Xu inv_desc->lo & VTD_INV_DESC_IOTLB_G); 2321b5a280c0SLe Tan return false; 2322b5a280c0SLe Tan } 2323b5a280c0SLe Tan return true; 2324b5a280c0SLe Tan } 2325b5a280c0SLe Tan 232602a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, 232702a2cbc8SPeter Xu VTDInvDesc *inv_desc) 232802a2cbc8SPeter Xu { 23297feb51b7SPeter Xu trace_vtd_inv_desc_iec(inv_desc->iec.granularity, 233002a2cbc8SPeter Xu inv_desc->iec.index, 233102a2cbc8SPeter Xu inv_desc->iec.index_mask); 233202a2cbc8SPeter Xu 233302a2cbc8SPeter Xu vtd_iec_notify_all(s, !inv_desc->iec.granularity, 233402a2cbc8SPeter Xu inv_desc->iec.index, 233502a2cbc8SPeter Xu inv_desc->iec.index_mask); 2336554f5e16SJason Wang return true; 2337554f5e16SJason Wang } 233802a2cbc8SPeter Xu 2339554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, 2340554f5e16SJason Wang VTDInvDesc *inv_desc) 2341554f5e16SJason Wang { 2342554f5e16SJason Wang VTDAddressSpace *vtd_dev_as; 2343554f5e16SJason Wang IOMMUTLBEntry entry; 2344554f5e16SJason Wang struct VTDBus *vtd_bus; 2345554f5e16SJason Wang hwaddr addr; 2346554f5e16SJason Wang uint64_t sz; 2347554f5e16SJason Wang uint16_t sid; 2348554f5e16SJason Wang uint8_t devfn; 2349554f5e16SJason Wang bool size; 2350554f5e16SJason Wang uint8_t bus_num; 2351554f5e16SJason Wang 2352554f5e16SJason Wang addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi); 2353554f5e16SJason Wang sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo); 2354554f5e16SJason Wang devfn = sid & 0xff; 2355554f5e16SJason Wang bus_num = sid >> 8; 2356554f5e16SJason Wang size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi); 2357554f5e16SJason Wang 2358554f5e16SJason Wang if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) || 2359554f5e16SJason Wang (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) { 2360095955b2SPeter Xu error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64 2361095955b2SPeter Xu ", lo=%"PRIx64" (reserved nonzero)", __func__, 2362095955b2SPeter Xu inv_desc->hi, inv_desc->lo); 2363554f5e16SJason Wang return false; 2364554f5e16SJason Wang } 2365554f5e16SJason Wang 2366554f5e16SJason Wang vtd_bus = vtd_find_as_from_bus_num(s, bus_num); 2367554f5e16SJason Wang if (!vtd_bus) { 2368554f5e16SJason Wang goto done; 2369554f5e16SJason Wang } 2370554f5e16SJason Wang 2371554f5e16SJason Wang vtd_dev_as = vtd_bus->dev_as[devfn]; 2372554f5e16SJason Wang if (!vtd_dev_as) { 2373554f5e16SJason Wang goto done; 2374554f5e16SJason Wang } 2375554f5e16SJason Wang 237604eb6247SJason Wang /* According to ATS spec table 2.4: 237704eb6247SJason Wang * S = 0, bits 15:12 = xxxx range size: 4K 237804eb6247SJason Wang * S = 1, bits 15:12 = xxx0 range size: 8K 237904eb6247SJason Wang * S = 1, bits 15:12 = xx01 range size: 16K 238004eb6247SJason Wang * S = 1, bits 15:12 = x011 range size: 32K 238104eb6247SJason Wang * S = 1, bits 15:12 = 0111 range size: 64K 238204eb6247SJason Wang * ... 238304eb6247SJason Wang */ 2384554f5e16SJason Wang if (size) { 238504eb6247SJason Wang sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT); 2386554f5e16SJason Wang addr &= ~(sz - 1); 2387554f5e16SJason Wang } else { 2388554f5e16SJason Wang sz = VTD_PAGE_SIZE; 2389554f5e16SJason Wang } 2390554f5e16SJason Wang 2391554f5e16SJason Wang entry.target_as = &vtd_dev_as->as; 2392554f5e16SJason Wang entry.addr_mask = sz - 1; 2393554f5e16SJason Wang entry.iova = addr; 2394554f5e16SJason Wang entry.perm = IOMMU_NONE; 2395554f5e16SJason Wang entry.translated_addr = 0; 2396cb1efcf4SPeter Maydell memory_region_notify_iommu(&vtd_dev_as->iommu, 0, entry); 2397554f5e16SJason Wang 2398554f5e16SJason Wang done: 239902a2cbc8SPeter Xu return true; 240002a2cbc8SPeter Xu } 240102a2cbc8SPeter Xu 2402ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s) 2403ed7b8fbcSLe Tan { 2404ed7b8fbcSLe Tan VTDInvDesc inv_desc; 2405ed7b8fbcSLe Tan uint8_t desc_type; 2406ed7b8fbcSLe Tan 24077feb51b7SPeter Xu trace_vtd_inv_qi_head(s->iq_head); 2408c0c1d351SLiu, Yi L if (!vtd_get_inv_desc(s, &inv_desc)) { 2409ed7b8fbcSLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 2410ed7b8fbcSLe Tan return false; 2411ed7b8fbcSLe Tan } 2412c0c1d351SLiu, Yi L 2413ed7b8fbcSLe Tan desc_type = inv_desc.lo & VTD_INV_DESC_TYPE; 2414ed7b8fbcSLe Tan /* FIXME: should update at first or at last? */ 2415ed7b8fbcSLe Tan s->iq_last_desc_type = desc_type; 2416ed7b8fbcSLe Tan 2417ed7b8fbcSLe Tan switch (desc_type) { 2418ed7b8fbcSLe Tan case VTD_INV_DESC_CC: 2419bc535e59SPeter Xu trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo); 2420d92fa2dcSLe Tan if (!vtd_process_context_cache_desc(s, &inv_desc)) { 2421d92fa2dcSLe Tan return false; 2422d92fa2dcSLe Tan } 2423ed7b8fbcSLe Tan break; 2424ed7b8fbcSLe Tan 2425ed7b8fbcSLe Tan case VTD_INV_DESC_IOTLB: 2426bc535e59SPeter Xu trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo); 2427b5a280c0SLe Tan if (!vtd_process_iotlb_desc(s, &inv_desc)) { 2428b5a280c0SLe Tan return false; 2429b5a280c0SLe Tan } 2430ed7b8fbcSLe Tan break; 2431ed7b8fbcSLe Tan 24324a4f219eSYi Sun /* 24334a4f219eSYi Sun * TODO: the entity of below two cases will be implemented in future series. 24344a4f219eSYi Sun * To make guest (which integrates scalable mode support patch set in 24354a4f219eSYi Sun * iommu driver) work, just return true is enough so far. 24364a4f219eSYi Sun */ 24374a4f219eSYi Sun case VTD_INV_DESC_PC: 24384a4f219eSYi Sun break; 24394a4f219eSYi Sun 24404a4f219eSYi Sun case VTD_INV_DESC_PIOTLB: 24414a4f219eSYi Sun break; 24424a4f219eSYi Sun 2443ed7b8fbcSLe Tan case VTD_INV_DESC_WAIT: 2444bc535e59SPeter Xu trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo); 2445ed7b8fbcSLe Tan if (!vtd_process_wait_desc(s, &inv_desc)) { 2446ed7b8fbcSLe Tan return false; 2447ed7b8fbcSLe Tan } 2448ed7b8fbcSLe Tan break; 2449ed7b8fbcSLe Tan 2450b7910472SPeter Xu case VTD_INV_DESC_IEC: 2451bc535e59SPeter Xu trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo); 245202a2cbc8SPeter Xu if (!vtd_process_inv_iec_desc(s, &inv_desc)) { 245302a2cbc8SPeter Xu return false; 245402a2cbc8SPeter Xu } 2455b7910472SPeter Xu break; 2456b7910472SPeter Xu 2457554f5e16SJason Wang case VTD_INV_DESC_DEVICE: 24587feb51b7SPeter Xu trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo); 2459554f5e16SJason Wang if (!vtd_process_device_iotlb_desc(s, &inv_desc)) { 2460554f5e16SJason Wang return false; 2461554f5e16SJason Wang } 2462554f5e16SJason Wang break; 2463554f5e16SJason Wang 2464ed7b8fbcSLe Tan default: 2465095955b2SPeter Xu error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64 2466095955b2SPeter Xu " (unknown type)", __func__, inv_desc.hi, 2467095955b2SPeter Xu inv_desc.lo); 2468ed7b8fbcSLe Tan return false; 2469ed7b8fbcSLe Tan } 2470ed7b8fbcSLe Tan s->iq_head++; 2471ed7b8fbcSLe Tan if (s->iq_head == s->iq_size) { 2472ed7b8fbcSLe Tan s->iq_head = 0; 2473ed7b8fbcSLe Tan } 2474ed7b8fbcSLe Tan return true; 2475ed7b8fbcSLe Tan } 2476ed7b8fbcSLe Tan 2477ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */ 2478ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s) 2479ed7b8fbcSLe Tan { 24807feb51b7SPeter Xu trace_vtd_inv_qi_fetch(); 24817feb51b7SPeter Xu 2482ed7b8fbcSLe Tan if (s->iq_tail >= s->iq_size) { 2483ed7b8fbcSLe Tan /* Detects an invalid Tail pointer */ 24844e4abd11SPeter Xu error_report_once("%s: detected invalid QI tail " 24854e4abd11SPeter Xu "(tail=0x%x, size=0x%x)", 24864e4abd11SPeter Xu __func__, s->iq_tail, s->iq_size); 2487ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 2488ed7b8fbcSLe Tan return; 2489ed7b8fbcSLe Tan } 2490ed7b8fbcSLe Tan while (s->iq_head != s->iq_tail) { 2491ed7b8fbcSLe Tan if (!vtd_process_inv_desc(s)) { 2492ed7b8fbcSLe Tan /* Invalidation Queue Errors */ 2493ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 2494ed7b8fbcSLe Tan break; 2495ed7b8fbcSLe Tan } 2496ed7b8fbcSLe Tan /* Must update the IQH_REG in time */ 2497ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 2498ed7b8fbcSLe Tan (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) & 2499ed7b8fbcSLe Tan VTD_IQH_QH_MASK); 2500ed7b8fbcSLe Tan } 2501ed7b8fbcSLe Tan } 2502ed7b8fbcSLe Tan 2503ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */ 2504ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s) 2505ed7b8fbcSLe Tan { 2506ed7b8fbcSLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG); 2507ed7b8fbcSLe Tan 2508c0c1d351SLiu, Yi L if (s->iq_dw && (val & VTD_IQT_QT_256_RSV_BIT)) { 2509c0c1d351SLiu, Yi L error_report_once("%s: RSV bit is set: val=0x%"PRIx64, 2510c0c1d351SLiu, Yi L __func__, val); 2511c0c1d351SLiu, Yi L return; 2512c0c1d351SLiu, Yi L } 2513c0c1d351SLiu, Yi L s->iq_tail = VTD_IQT_QT(s->iq_dw, val); 25147feb51b7SPeter Xu trace_vtd_inv_qi_tail(s->iq_tail); 25157feb51b7SPeter Xu 2516ed7b8fbcSLe Tan if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 2517ed7b8fbcSLe Tan /* Process Invalidation Queue here */ 2518ed7b8fbcSLe Tan vtd_fetch_inv_desc(s); 2519ed7b8fbcSLe Tan } 2520ed7b8fbcSLe Tan } 2521ed7b8fbcSLe Tan 25221da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s) 25231da12ec4SLe Tan { 25241da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 25251da12ec4SLe Tan uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 25261da12ec4SLe Tan uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE; 25271da12ec4SLe Tan 25281da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) { 25291da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 25307feb51b7SPeter Xu trace_vtd_fsts_clear_ip(); 25311da12ec4SLe Tan } 2532ed7b8fbcSLe Tan /* FIXME: when IQE is Clear, should we try to fetch some Invalidation 2533ed7b8fbcSLe Tan * Descriptors if there are any when Queued Invalidation is enabled? 2534ed7b8fbcSLe Tan */ 25351da12ec4SLe Tan } 25361da12ec4SLe Tan 25371da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s) 25381da12ec4SLe Tan { 25391da12ec4SLe Tan uint32_t fectl_reg; 25401da12ec4SLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 25411da12ec4SLe Tan * need to compare the old value and the new value to conclude that 25421da12ec4SLe Tan * software clears the IM field? Or just check if the IM field is zero? 25431da12ec4SLe Tan */ 25441da12ec4SLe Tan fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 25457feb51b7SPeter Xu 25467feb51b7SPeter Xu trace_vtd_reg_write_fectl(fectl_reg); 25477feb51b7SPeter Xu 25481da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) { 25491da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 25501da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 25511da12ec4SLe Tan } 25521da12ec4SLe Tan } 25531da12ec4SLe Tan 2554ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s) 2555ed7b8fbcSLe Tan { 2556ed7b8fbcSLe Tan uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG); 2557ed7b8fbcSLe Tan uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 2558ed7b8fbcSLe Tan 2559ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) { 25607feb51b7SPeter Xu trace_vtd_reg_ics_clear_ip(); 2561ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 2562ed7b8fbcSLe Tan } 2563ed7b8fbcSLe Tan } 2564ed7b8fbcSLe Tan 2565ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s) 2566ed7b8fbcSLe Tan { 2567ed7b8fbcSLe Tan uint32_t iectl_reg; 2568ed7b8fbcSLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 2569ed7b8fbcSLe Tan * need to compare the old value and the new value to conclude that 2570ed7b8fbcSLe Tan * software clears the IM field? Or just check if the IM field is zero? 2571ed7b8fbcSLe Tan */ 2572ed7b8fbcSLe Tan iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 25737feb51b7SPeter Xu 25747feb51b7SPeter Xu trace_vtd_reg_write_iectl(iectl_reg); 25757feb51b7SPeter Xu 2576ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) { 2577ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 2578ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 2579ed7b8fbcSLe Tan } 2580ed7b8fbcSLe Tan } 2581ed7b8fbcSLe Tan 25821da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size) 25831da12ec4SLe Tan { 25841da12ec4SLe Tan IntelIOMMUState *s = opaque; 25851da12ec4SLe Tan uint64_t val; 25861da12ec4SLe Tan 25877feb51b7SPeter Xu trace_vtd_reg_read(addr, size); 25887feb51b7SPeter Xu 25891da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 25901376211fSPeter Xu error_report_once("%s: MMIO over range: addr=0x%" PRIx64 25911376211fSPeter Xu " size=0x%u", __func__, addr, size); 25921da12ec4SLe Tan return (uint64_t)-1; 25931da12ec4SLe Tan } 25941da12ec4SLe Tan 25951da12ec4SLe Tan switch (addr) { 25961da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 25971da12ec4SLe Tan case DMAR_RTADDR_REG: 25981da12ec4SLe Tan if (size == 4) { 25991da12ec4SLe Tan val = s->root & ((1ULL << 32) - 1); 26001da12ec4SLe Tan } else { 26011da12ec4SLe Tan val = s->root; 26021da12ec4SLe Tan } 26031da12ec4SLe Tan break; 26041da12ec4SLe Tan 26051da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 26061da12ec4SLe Tan assert(size == 4); 26071da12ec4SLe Tan val = s->root >> 32; 26081da12ec4SLe Tan break; 26091da12ec4SLe Tan 2610ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2611ed7b8fbcSLe Tan case DMAR_IQA_REG: 2612ed7b8fbcSLe Tan val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS); 2613ed7b8fbcSLe Tan if (size == 4) { 2614ed7b8fbcSLe Tan val = val & ((1ULL << 32) - 1); 2615ed7b8fbcSLe Tan } 2616ed7b8fbcSLe Tan break; 2617ed7b8fbcSLe Tan 2618ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2619ed7b8fbcSLe Tan assert(size == 4); 2620ed7b8fbcSLe Tan val = s->iq >> 32; 2621ed7b8fbcSLe Tan break; 2622ed7b8fbcSLe Tan 26231da12ec4SLe Tan default: 26241da12ec4SLe Tan if (size == 4) { 26251da12ec4SLe Tan val = vtd_get_long(s, addr); 26261da12ec4SLe Tan } else { 26271da12ec4SLe Tan val = vtd_get_quad(s, addr); 26281da12ec4SLe Tan } 26291da12ec4SLe Tan } 26307feb51b7SPeter Xu 26311da12ec4SLe Tan return val; 26321da12ec4SLe Tan } 26331da12ec4SLe Tan 26341da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr, 26351da12ec4SLe Tan uint64_t val, unsigned size) 26361da12ec4SLe Tan { 26371da12ec4SLe Tan IntelIOMMUState *s = opaque; 26381da12ec4SLe Tan 26397feb51b7SPeter Xu trace_vtd_reg_write(addr, size, val); 26407feb51b7SPeter Xu 26411da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 26421376211fSPeter Xu error_report_once("%s: MMIO over range: addr=0x%" PRIx64 26431376211fSPeter Xu " size=0x%u", __func__, addr, size); 26441da12ec4SLe Tan return; 26451da12ec4SLe Tan } 26461da12ec4SLe Tan 26471da12ec4SLe Tan switch (addr) { 26481da12ec4SLe Tan /* Global Command Register, 32-bit */ 26491da12ec4SLe Tan case DMAR_GCMD_REG: 26501da12ec4SLe Tan vtd_set_long(s, addr, val); 26511da12ec4SLe Tan vtd_handle_gcmd_write(s); 26521da12ec4SLe Tan break; 26531da12ec4SLe Tan 26541da12ec4SLe Tan /* Context Command Register, 64-bit */ 26551da12ec4SLe Tan case DMAR_CCMD_REG: 26561da12ec4SLe Tan if (size == 4) { 26571da12ec4SLe Tan vtd_set_long(s, addr, val); 26581da12ec4SLe Tan } else { 26591da12ec4SLe Tan vtd_set_quad(s, addr, val); 26601da12ec4SLe Tan vtd_handle_ccmd_write(s); 26611da12ec4SLe Tan } 26621da12ec4SLe Tan break; 26631da12ec4SLe Tan 26641da12ec4SLe Tan case DMAR_CCMD_REG_HI: 26651da12ec4SLe Tan assert(size == 4); 26661da12ec4SLe Tan vtd_set_long(s, addr, val); 26671da12ec4SLe Tan vtd_handle_ccmd_write(s); 26681da12ec4SLe Tan break; 26691da12ec4SLe Tan 26701da12ec4SLe Tan /* IOTLB Invalidation Register, 64-bit */ 26711da12ec4SLe Tan case DMAR_IOTLB_REG: 26721da12ec4SLe Tan if (size == 4) { 26731da12ec4SLe Tan vtd_set_long(s, addr, val); 26741da12ec4SLe Tan } else { 26751da12ec4SLe Tan vtd_set_quad(s, addr, val); 26761da12ec4SLe Tan vtd_handle_iotlb_write(s); 26771da12ec4SLe Tan } 26781da12ec4SLe Tan break; 26791da12ec4SLe Tan 26801da12ec4SLe Tan case DMAR_IOTLB_REG_HI: 26811da12ec4SLe Tan assert(size == 4); 26821da12ec4SLe Tan vtd_set_long(s, addr, val); 26831da12ec4SLe Tan vtd_handle_iotlb_write(s); 26841da12ec4SLe Tan break; 26851da12ec4SLe Tan 2686b5a280c0SLe Tan /* Invalidate Address Register, 64-bit */ 2687b5a280c0SLe Tan case DMAR_IVA_REG: 2688b5a280c0SLe Tan if (size == 4) { 2689b5a280c0SLe Tan vtd_set_long(s, addr, val); 2690b5a280c0SLe Tan } else { 2691b5a280c0SLe Tan vtd_set_quad(s, addr, val); 2692b5a280c0SLe Tan } 2693b5a280c0SLe Tan break; 2694b5a280c0SLe Tan 2695b5a280c0SLe Tan case DMAR_IVA_REG_HI: 2696b5a280c0SLe Tan assert(size == 4); 2697b5a280c0SLe Tan vtd_set_long(s, addr, val); 2698b5a280c0SLe Tan break; 2699b5a280c0SLe Tan 27001da12ec4SLe Tan /* Fault Status Register, 32-bit */ 27011da12ec4SLe Tan case DMAR_FSTS_REG: 27021da12ec4SLe Tan assert(size == 4); 27031da12ec4SLe Tan vtd_set_long(s, addr, val); 27041da12ec4SLe Tan vtd_handle_fsts_write(s); 27051da12ec4SLe Tan break; 27061da12ec4SLe Tan 27071da12ec4SLe Tan /* Fault Event Control Register, 32-bit */ 27081da12ec4SLe Tan case DMAR_FECTL_REG: 27091da12ec4SLe Tan assert(size == 4); 27101da12ec4SLe Tan vtd_set_long(s, addr, val); 27111da12ec4SLe Tan vtd_handle_fectl_write(s); 27121da12ec4SLe Tan break; 27131da12ec4SLe Tan 27141da12ec4SLe Tan /* Fault Event Data Register, 32-bit */ 27151da12ec4SLe Tan case DMAR_FEDATA_REG: 27161da12ec4SLe Tan assert(size == 4); 27171da12ec4SLe Tan vtd_set_long(s, addr, val); 27181da12ec4SLe Tan break; 27191da12ec4SLe Tan 27201da12ec4SLe Tan /* Fault Event Address Register, 32-bit */ 27211da12ec4SLe Tan case DMAR_FEADDR_REG: 2722b7a7bb35SJan Kiszka if (size == 4) { 27231da12ec4SLe Tan vtd_set_long(s, addr, val); 2724b7a7bb35SJan Kiszka } else { 2725b7a7bb35SJan Kiszka /* 2726b7a7bb35SJan Kiszka * While the register is 32-bit only, some guests (Xen...) write to 2727b7a7bb35SJan Kiszka * it with 64-bit. 2728b7a7bb35SJan Kiszka */ 2729b7a7bb35SJan Kiszka vtd_set_quad(s, addr, val); 2730b7a7bb35SJan Kiszka } 27311da12ec4SLe Tan break; 27321da12ec4SLe Tan 27331da12ec4SLe Tan /* Fault Event Upper Address Register, 32-bit */ 27341da12ec4SLe Tan case DMAR_FEUADDR_REG: 27351da12ec4SLe Tan assert(size == 4); 27361da12ec4SLe Tan vtd_set_long(s, addr, val); 27371da12ec4SLe Tan break; 27381da12ec4SLe Tan 27391da12ec4SLe Tan /* Protected Memory Enable Register, 32-bit */ 27401da12ec4SLe Tan case DMAR_PMEN_REG: 27411da12ec4SLe Tan assert(size == 4); 27421da12ec4SLe Tan vtd_set_long(s, addr, val); 27431da12ec4SLe Tan break; 27441da12ec4SLe Tan 27451da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 27461da12ec4SLe Tan case DMAR_RTADDR_REG: 27471da12ec4SLe Tan if (size == 4) { 27481da12ec4SLe Tan vtd_set_long(s, addr, val); 27491da12ec4SLe Tan } else { 27501da12ec4SLe Tan vtd_set_quad(s, addr, val); 27511da12ec4SLe Tan } 27521da12ec4SLe Tan break; 27531da12ec4SLe Tan 27541da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 27551da12ec4SLe Tan assert(size == 4); 27561da12ec4SLe Tan vtd_set_long(s, addr, val); 27571da12ec4SLe Tan break; 27581da12ec4SLe Tan 2759ed7b8fbcSLe Tan /* Invalidation Queue Tail Register, 64-bit */ 2760ed7b8fbcSLe Tan case DMAR_IQT_REG: 2761ed7b8fbcSLe Tan if (size == 4) { 2762ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2763ed7b8fbcSLe Tan } else { 2764ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2765ed7b8fbcSLe Tan } 2766ed7b8fbcSLe Tan vtd_handle_iqt_write(s); 2767ed7b8fbcSLe Tan break; 2768ed7b8fbcSLe Tan 2769ed7b8fbcSLe Tan case DMAR_IQT_REG_HI: 2770ed7b8fbcSLe Tan assert(size == 4); 2771ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2772ed7b8fbcSLe Tan /* 19:63 of IQT_REG is RsvdZ, do nothing here */ 2773ed7b8fbcSLe Tan break; 2774ed7b8fbcSLe Tan 2775ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2776ed7b8fbcSLe Tan case DMAR_IQA_REG: 2777ed7b8fbcSLe Tan if (size == 4) { 2778ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2779ed7b8fbcSLe Tan } else { 2780ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2781ed7b8fbcSLe Tan } 2782c0c1d351SLiu, Yi L if (s->ecap & VTD_ECAP_SMTS && 2783c0c1d351SLiu, Yi L val & VTD_IQA_DW_MASK) { 2784c0c1d351SLiu, Yi L s->iq_dw = true; 2785c0c1d351SLiu, Yi L } else { 2786c0c1d351SLiu, Yi L s->iq_dw = false; 2787c0c1d351SLiu, Yi L } 2788ed7b8fbcSLe Tan break; 2789ed7b8fbcSLe Tan 2790ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2791ed7b8fbcSLe Tan assert(size == 4); 2792ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2793ed7b8fbcSLe Tan break; 2794ed7b8fbcSLe Tan 2795ed7b8fbcSLe Tan /* Invalidation Completion Status Register, 32-bit */ 2796ed7b8fbcSLe Tan case DMAR_ICS_REG: 2797ed7b8fbcSLe Tan assert(size == 4); 2798ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2799ed7b8fbcSLe Tan vtd_handle_ics_write(s); 2800ed7b8fbcSLe Tan break; 2801ed7b8fbcSLe Tan 2802ed7b8fbcSLe Tan /* Invalidation Event Control Register, 32-bit */ 2803ed7b8fbcSLe Tan case DMAR_IECTL_REG: 2804ed7b8fbcSLe Tan assert(size == 4); 2805ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2806ed7b8fbcSLe Tan vtd_handle_iectl_write(s); 2807ed7b8fbcSLe Tan break; 2808ed7b8fbcSLe Tan 2809ed7b8fbcSLe Tan /* Invalidation Event Data Register, 32-bit */ 2810ed7b8fbcSLe Tan case DMAR_IEDATA_REG: 2811ed7b8fbcSLe Tan assert(size == 4); 2812ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2813ed7b8fbcSLe Tan break; 2814ed7b8fbcSLe Tan 2815ed7b8fbcSLe Tan /* Invalidation Event Address Register, 32-bit */ 2816ed7b8fbcSLe Tan case DMAR_IEADDR_REG: 2817ed7b8fbcSLe Tan assert(size == 4); 2818ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2819ed7b8fbcSLe Tan break; 2820ed7b8fbcSLe Tan 2821ed7b8fbcSLe Tan /* Invalidation Event Upper Address Register, 32-bit */ 2822ed7b8fbcSLe Tan case DMAR_IEUADDR_REG: 2823ed7b8fbcSLe Tan assert(size == 4); 2824ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2825ed7b8fbcSLe Tan break; 2826ed7b8fbcSLe Tan 28271da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 28281da12ec4SLe Tan case DMAR_FRCD_REG_0_0: 28291da12ec4SLe Tan if (size == 4) { 28301da12ec4SLe Tan vtd_set_long(s, addr, val); 28311da12ec4SLe Tan } else { 28321da12ec4SLe Tan vtd_set_quad(s, addr, val); 28331da12ec4SLe Tan } 28341da12ec4SLe Tan break; 28351da12ec4SLe Tan 28361da12ec4SLe Tan case DMAR_FRCD_REG_0_1: 28371da12ec4SLe Tan assert(size == 4); 28381da12ec4SLe Tan vtd_set_long(s, addr, val); 28391da12ec4SLe Tan break; 28401da12ec4SLe Tan 28411da12ec4SLe Tan case DMAR_FRCD_REG_0_2: 28421da12ec4SLe Tan if (size == 4) { 28431da12ec4SLe Tan vtd_set_long(s, addr, val); 28441da12ec4SLe Tan } else { 28451da12ec4SLe Tan vtd_set_quad(s, addr, val); 28461da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 28471da12ec4SLe Tan vtd_update_fsts_ppf(s); 28481da12ec4SLe Tan } 28491da12ec4SLe Tan break; 28501da12ec4SLe Tan 28511da12ec4SLe Tan case DMAR_FRCD_REG_0_3: 28521da12ec4SLe Tan assert(size == 4); 28531da12ec4SLe Tan vtd_set_long(s, addr, val); 28541da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 28551da12ec4SLe Tan vtd_update_fsts_ppf(s); 28561da12ec4SLe Tan break; 28571da12ec4SLe Tan 2858a5861439SPeter Xu case DMAR_IRTA_REG: 2859a5861439SPeter Xu if (size == 4) { 2860a5861439SPeter Xu vtd_set_long(s, addr, val); 2861a5861439SPeter Xu } else { 2862a5861439SPeter Xu vtd_set_quad(s, addr, val); 2863a5861439SPeter Xu } 2864a5861439SPeter Xu break; 2865a5861439SPeter Xu 2866a5861439SPeter Xu case DMAR_IRTA_REG_HI: 2867a5861439SPeter Xu assert(size == 4); 2868a5861439SPeter Xu vtd_set_long(s, addr, val); 2869a5861439SPeter Xu break; 2870a5861439SPeter Xu 28711da12ec4SLe Tan default: 28721da12ec4SLe Tan if (size == 4) { 28731da12ec4SLe Tan vtd_set_long(s, addr, val); 28741da12ec4SLe Tan } else { 28751da12ec4SLe Tan vtd_set_quad(s, addr, val); 28761da12ec4SLe Tan } 28771da12ec4SLe Tan } 28781da12ec4SLe Tan } 28791da12ec4SLe Tan 28803df9d748SAlexey Kardashevskiy static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr, 28812c91bcf2SPeter Maydell IOMMUAccessFlags flag, int iommu_idx) 28821da12ec4SLe Tan { 28831da12ec4SLe Tan VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 28841da12ec4SLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 2885b9313021SPeter Xu IOMMUTLBEntry iotlb = { 2886b9313021SPeter Xu /* We'll fill in the rest later. */ 28871da12ec4SLe Tan .target_as = &address_space_memory, 28881da12ec4SLe Tan }; 2889b9313021SPeter Xu bool success; 28901da12ec4SLe Tan 2891b9313021SPeter Xu if (likely(s->dmar_enabled)) { 2892b9313021SPeter Xu success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn, 2893b9313021SPeter Xu addr, flag & IOMMU_WO, &iotlb); 2894b9313021SPeter Xu } else { 28951da12ec4SLe Tan /* DMAR disabled, passthrough, use 4k-page*/ 2896b9313021SPeter Xu iotlb.iova = addr & VTD_PAGE_MASK_4K; 2897b9313021SPeter Xu iotlb.translated_addr = addr & VTD_PAGE_MASK_4K; 2898b9313021SPeter Xu iotlb.addr_mask = ~VTD_PAGE_MASK_4K; 2899b9313021SPeter Xu iotlb.perm = IOMMU_RW; 2900b9313021SPeter Xu success = true; 29011da12ec4SLe Tan } 29021da12ec4SLe Tan 2903b9313021SPeter Xu if (likely(success)) { 29047feb51b7SPeter Xu trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus), 29057feb51b7SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 29067feb51b7SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 2907b9313021SPeter Xu iotlb.iova, iotlb.translated_addr, 2908b9313021SPeter Xu iotlb.addr_mask); 2909b9313021SPeter Xu } else { 29104e4abd11SPeter Xu error_report_once("%s: detected translation failure " 29114e4abd11SPeter Xu "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")", 29124e4abd11SPeter Xu __func__, pci_bus_num(vtd_as->bus), 2913b9313021SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 2914b9313021SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 2915662b4b69SPeter Xu addr); 2916b9313021SPeter Xu } 29177feb51b7SPeter Xu 2918b9313021SPeter Xu return iotlb; 29191da12ec4SLe Tan } 29201da12ec4SLe Tan 29213df9d748SAlexey Kardashevskiy static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, 29225bf3d319SPeter Xu IOMMUNotifierFlag old, 29235bf3d319SPeter Xu IOMMUNotifierFlag new) 29243cb3b154SAlex Williamson { 29253cb3b154SAlex Williamson VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 2926dd4d607eSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 29273cb3b154SAlex Williamson 2928dd4d607eSPeter Xu if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) { 292975c5626cSAlex Williamson error_report("We need to set caching-mode=on for intel-iommu to enable " 2930dd4d607eSPeter Xu "device assignment with IOMMU protection."); 2931a3276f78SPeter Xu exit(1); 2932a3276f78SPeter Xu } 2933dd4d607eSPeter Xu 29344f8a62a9SPeter Xu /* Update per-address-space notifier flags */ 29354f8a62a9SPeter Xu vtd_as->notifier_flags = new; 29364f8a62a9SPeter Xu 2937dd4d607eSPeter Xu if (old == IOMMU_NOTIFIER_NONE) { 2938b4a4ba0dSPeter Xu QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next); 2939b4a4ba0dSPeter Xu } else if (new == IOMMU_NOTIFIER_NONE) { 2940b4a4ba0dSPeter Xu QLIST_REMOVE(vtd_as, next); 2941dd4d607eSPeter Xu } 29423cb3b154SAlex Williamson } 29433cb3b154SAlex Williamson 2944552a1e01SPeter Xu static int vtd_post_load(void *opaque, int version_id) 2945552a1e01SPeter Xu { 2946552a1e01SPeter Xu IntelIOMMUState *iommu = opaque; 2947552a1e01SPeter Xu 2948552a1e01SPeter Xu /* 2949552a1e01SPeter Xu * Memory regions are dynamically turned on/off depending on 2950552a1e01SPeter Xu * context entry configurations from the guest. After migration, 2951552a1e01SPeter Xu * we need to make sure the memory regions are still correct. 2952552a1e01SPeter Xu */ 2953552a1e01SPeter Xu vtd_switch_address_space_all(iommu); 2954552a1e01SPeter Xu 29552811af3bSPeter Xu /* 29562811af3bSPeter Xu * We don't need to migrate the root_scalable because we can 29572811af3bSPeter Xu * simply do the calculation after the loading is complete. We 29582811af3bSPeter Xu * can actually do similar things with root, dmar_enabled, etc. 29592811af3bSPeter Xu * however since we've had them already so we'd better keep them 29602811af3bSPeter Xu * for compatibility of migration. 29612811af3bSPeter Xu */ 29622811af3bSPeter Xu vtd_update_scalable_state(iommu); 29632811af3bSPeter Xu 2964552a1e01SPeter Xu return 0; 2965552a1e01SPeter Xu } 2966552a1e01SPeter Xu 29671da12ec4SLe Tan static const VMStateDescription vtd_vmstate = { 29681da12ec4SLe Tan .name = "iommu-intel", 29698cdcf3c1SPeter Xu .version_id = 1, 29708cdcf3c1SPeter Xu .minimum_version_id = 1, 29718cdcf3c1SPeter Xu .priority = MIG_PRI_IOMMU, 2972552a1e01SPeter Xu .post_load = vtd_post_load, 29738cdcf3c1SPeter Xu .fields = (VMStateField[]) { 29748cdcf3c1SPeter Xu VMSTATE_UINT64(root, IntelIOMMUState), 29758cdcf3c1SPeter Xu VMSTATE_UINT64(intr_root, IntelIOMMUState), 29768cdcf3c1SPeter Xu VMSTATE_UINT64(iq, IntelIOMMUState), 29778cdcf3c1SPeter Xu VMSTATE_UINT32(intr_size, IntelIOMMUState), 29788cdcf3c1SPeter Xu VMSTATE_UINT16(iq_head, IntelIOMMUState), 29798cdcf3c1SPeter Xu VMSTATE_UINT16(iq_tail, IntelIOMMUState), 29808cdcf3c1SPeter Xu VMSTATE_UINT16(iq_size, IntelIOMMUState), 29818cdcf3c1SPeter Xu VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState), 29828cdcf3c1SPeter Xu VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE), 29838cdcf3c1SPeter Xu VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState), 298481fb1e64SPeter Xu VMSTATE_UNUSED(1), /* bool root_extended is obsolete by VT-d */ 29858cdcf3c1SPeter Xu VMSTATE_BOOL(dmar_enabled, IntelIOMMUState), 29868cdcf3c1SPeter Xu VMSTATE_BOOL(qi_enabled, IntelIOMMUState), 29878cdcf3c1SPeter Xu VMSTATE_BOOL(intr_enabled, IntelIOMMUState), 29888cdcf3c1SPeter Xu VMSTATE_BOOL(intr_eime, IntelIOMMUState), 29898cdcf3c1SPeter Xu VMSTATE_END_OF_LIST() 29908cdcf3c1SPeter Xu } 29911da12ec4SLe Tan }; 29921da12ec4SLe Tan 29931da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = { 29941da12ec4SLe Tan .read = vtd_mem_read, 29951da12ec4SLe Tan .write = vtd_mem_write, 29961da12ec4SLe Tan .endianness = DEVICE_LITTLE_ENDIAN, 29971da12ec4SLe Tan .impl = { 29981da12ec4SLe Tan .min_access_size = 4, 29991da12ec4SLe Tan .max_access_size = 8, 30001da12ec4SLe Tan }, 30011da12ec4SLe Tan .valid = { 30021da12ec4SLe Tan .min_access_size = 4, 30031da12ec4SLe Tan .max_access_size = 8, 30041da12ec4SLe Tan }, 30051da12ec4SLe Tan }; 30061da12ec4SLe Tan 30071da12ec4SLe Tan static Property vtd_properties[] = { 30081da12ec4SLe Tan DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0), 3009e6b6af05SRadim Krčmář DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim, 3010e6b6af05SRadim Krčmář ON_OFF_AUTO_AUTO), 3011fb506e70SRadim Krčmář DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false), 30124b49b586SPeter Xu DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits, 301337f51384SPrasad Singamsetty VTD_HOST_ADDRESS_WIDTH), 30143b40f0e5SAviv Ben-David DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), 30154a4f219eSYi Sun DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE), 3016ccc23bb0SPeter Xu DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true), 30171da12ec4SLe Tan DEFINE_PROP_END_OF_LIST(), 30181da12ec4SLe Tan }; 30191da12ec4SLe Tan 3020651e4cefSPeter Xu /* Read IRTE entry with specific index */ 3021651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index, 3022bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry *entry, uint16_t sid) 3023651e4cefSPeter Xu { 3024ede9c94aSPeter Xu static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \ 3025ede9c94aSPeter Xu {0xffff, 0xfffb, 0xfff9, 0xfff8}; 3026651e4cefSPeter Xu dma_addr_t addr = 0x00; 3027ede9c94aSPeter Xu uint16_t mask, source_id; 3028ede9c94aSPeter Xu uint8_t bus, bus_max, bus_min; 3029651e4cefSPeter Xu 3030651e4cefSPeter Xu addr = iommu->intr_root + index * sizeof(*entry); 3031651e4cefSPeter Xu if (dma_memory_read(&address_space_memory, addr, entry, 3032651e4cefSPeter Xu sizeof(*entry))) { 30331376211fSPeter Xu error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64, 30341376211fSPeter Xu __func__, index, addr); 3035651e4cefSPeter Xu return -VTD_FR_IR_ROOT_INVAL; 3036651e4cefSPeter Xu } 3037651e4cefSPeter Xu 30387feb51b7SPeter Xu trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]), 30397feb51b7SPeter Xu le64_to_cpu(entry->data[0])); 30407feb51b7SPeter Xu 3041bc38ee10SMichael S. Tsirkin if (!entry->irte.present) { 30424e4abd11SPeter Xu error_report_once("%s: detected non-present IRTE " 30434e4abd11SPeter Xu "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")", 30444e4abd11SPeter Xu __func__, index, le64_to_cpu(entry->data[1]), 3045651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 3046651e4cefSPeter Xu return -VTD_FR_IR_ENTRY_P; 3047651e4cefSPeter Xu } 3048651e4cefSPeter Xu 3049bc38ee10SMichael S. Tsirkin if (entry->irte.__reserved_0 || entry->irte.__reserved_1 || 3050bc38ee10SMichael S. Tsirkin entry->irte.__reserved_2) { 30514e4abd11SPeter Xu error_report_once("%s: detected non-zero reserved IRTE " 30524e4abd11SPeter Xu "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")", 30534e4abd11SPeter Xu __func__, index, le64_to_cpu(entry->data[1]), 3054651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 3055651e4cefSPeter Xu return -VTD_FR_IR_IRTE_RSVD; 3056651e4cefSPeter Xu } 3057651e4cefSPeter Xu 3058ede9c94aSPeter Xu if (sid != X86_IOMMU_SID_INVALID) { 3059ede9c94aSPeter Xu /* Validate IRTE SID */ 3060bc38ee10SMichael S. Tsirkin source_id = le32_to_cpu(entry->irte.source_id); 3061bc38ee10SMichael S. Tsirkin switch (entry->irte.sid_vtype) { 3062ede9c94aSPeter Xu case VTD_SVT_NONE: 3063ede9c94aSPeter Xu break; 3064ede9c94aSPeter Xu 3065ede9c94aSPeter Xu case VTD_SVT_ALL: 3066bc38ee10SMichael S. Tsirkin mask = vtd_svt_mask[entry->irte.sid_q]; 3067ede9c94aSPeter Xu if ((source_id & mask) != (sid & mask)) { 30684e4abd11SPeter Xu error_report_once("%s: invalid IRTE SID " 30694e4abd11SPeter Xu "(index=%u, sid=%u, source_id=%u)", 30704e4abd11SPeter Xu __func__, index, sid, source_id); 3071ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3072ede9c94aSPeter Xu } 3073ede9c94aSPeter Xu break; 3074ede9c94aSPeter Xu 3075ede9c94aSPeter Xu case VTD_SVT_BUS: 3076ede9c94aSPeter Xu bus_max = source_id >> 8; 3077ede9c94aSPeter Xu bus_min = source_id & 0xff; 3078ede9c94aSPeter Xu bus = sid >> 8; 3079ede9c94aSPeter Xu if (bus > bus_max || bus < bus_min) { 30804e4abd11SPeter Xu error_report_once("%s: invalid SVT_BUS " 30814e4abd11SPeter Xu "(index=%u, bus=%u, min=%u, max=%u)", 30824e4abd11SPeter Xu __func__, index, bus, bus_min, bus_max); 3083ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3084ede9c94aSPeter Xu } 3085ede9c94aSPeter Xu break; 3086ede9c94aSPeter Xu 3087ede9c94aSPeter Xu default: 30884e4abd11SPeter Xu error_report_once("%s: detected invalid IRTE SVT " 30894e4abd11SPeter Xu "(index=%u, type=%d)", __func__, 30904e4abd11SPeter Xu index, entry->irte.sid_vtype); 3091ede9c94aSPeter Xu /* Take this as verification failure. */ 3092ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3093ede9c94aSPeter Xu break; 3094ede9c94aSPeter Xu } 3095ede9c94aSPeter Xu } 3096651e4cefSPeter Xu 3097651e4cefSPeter Xu return 0; 3098651e4cefSPeter Xu } 3099651e4cefSPeter Xu 3100651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */ 3101ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index, 310235c24501SSingh, Brijesh X86IOMMUIrq *irq, uint16_t sid) 3103651e4cefSPeter Xu { 3104bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry irte = {}; 3105651e4cefSPeter Xu int ret = 0; 3106651e4cefSPeter Xu 3107ede9c94aSPeter Xu ret = vtd_irte_get(iommu, index, &irte, sid); 3108651e4cefSPeter Xu if (ret) { 3109651e4cefSPeter Xu return ret; 3110651e4cefSPeter Xu } 3111651e4cefSPeter Xu 3112bc38ee10SMichael S. Tsirkin irq->trigger_mode = irte.irte.trigger_mode; 3113bc38ee10SMichael S. Tsirkin irq->vector = irte.irte.vector; 3114bc38ee10SMichael S. Tsirkin irq->delivery_mode = irte.irte.delivery_mode; 3115bc38ee10SMichael S. Tsirkin irq->dest = le32_to_cpu(irte.irte.dest_id); 311628589311SJan Kiszka if (!iommu->intr_eime) { 3117651e4cefSPeter Xu #define VTD_IR_APIC_DEST_MASK (0xff00ULL) 3118651e4cefSPeter Xu #define VTD_IR_APIC_DEST_SHIFT (8) 311928589311SJan Kiszka irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >> 3120651e4cefSPeter Xu VTD_IR_APIC_DEST_SHIFT; 312128589311SJan Kiszka } 3122bc38ee10SMichael S. Tsirkin irq->dest_mode = irte.irte.dest_mode; 3123bc38ee10SMichael S. Tsirkin irq->redir_hint = irte.irte.redir_hint; 3124651e4cefSPeter Xu 31257feb51b7SPeter Xu trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector, 31267feb51b7SPeter Xu irq->delivery_mode, irq->dest, irq->dest_mode); 3127651e4cefSPeter Xu 3128651e4cefSPeter Xu return 0; 3129651e4cefSPeter Xu } 3130651e4cefSPeter Xu 3131651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */ 3132651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu, 3133651e4cefSPeter Xu MSIMessage *origin, 3134ede9c94aSPeter Xu MSIMessage *translated, 3135ede9c94aSPeter Xu uint16_t sid) 3136651e4cefSPeter Xu { 3137651e4cefSPeter Xu int ret = 0; 3138651e4cefSPeter Xu VTD_IR_MSIAddress addr; 3139651e4cefSPeter Xu uint16_t index; 314035c24501SSingh, Brijesh X86IOMMUIrq irq = {}; 3141651e4cefSPeter Xu 3142651e4cefSPeter Xu assert(origin && translated); 3143651e4cefSPeter Xu 31447feb51b7SPeter Xu trace_vtd_ir_remap_msi_req(origin->address, origin->data); 31457feb51b7SPeter Xu 3146651e4cefSPeter Xu if (!iommu || !iommu->intr_enabled) { 3147e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 3148e7a3b91fSPeter Xu goto out; 3149651e4cefSPeter Xu } 3150651e4cefSPeter Xu 3151651e4cefSPeter Xu if (origin->address & VTD_MSI_ADDR_HI_MASK) { 31521376211fSPeter Xu error_report_once("%s: MSI address high 32 bits non-zero detected: " 31531376211fSPeter Xu "address=0x%" PRIx64, __func__, origin->address); 3154651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3155651e4cefSPeter Xu } 3156651e4cefSPeter Xu 3157651e4cefSPeter Xu addr.data = origin->address & VTD_MSI_ADDR_LO_MASK; 31581a43713bSPeter Xu if (addr.addr.__head != 0xfee) { 31591376211fSPeter Xu error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32, 31601376211fSPeter Xu __func__, addr.data); 3161651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3162651e4cefSPeter Xu } 3163651e4cefSPeter Xu 3164651e4cefSPeter Xu /* This is compatible mode. */ 3165bc38ee10SMichael S. Tsirkin if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) { 3166e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 3167e7a3b91fSPeter Xu goto out; 3168651e4cefSPeter Xu } 3169651e4cefSPeter Xu 3170bc38ee10SMichael S. Tsirkin index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l); 3171651e4cefSPeter Xu 3172651e4cefSPeter Xu #define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff) 3173651e4cefSPeter Xu #define VTD_IR_MSI_DATA_RESERVED (0xffff0000) 3174651e4cefSPeter Xu 3175bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 3176651e4cefSPeter Xu /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */ 3177651e4cefSPeter Xu index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE; 3178651e4cefSPeter Xu } 3179651e4cefSPeter Xu 3180ede9c94aSPeter Xu ret = vtd_remap_irq_get(iommu, index, &irq, sid); 3181651e4cefSPeter Xu if (ret) { 3182651e4cefSPeter Xu return ret; 3183651e4cefSPeter Xu } 3184651e4cefSPeter Xu 3185bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 31867feb51b7SPeter Xu trace_vtd_ir_remap_type("MSI"); 3187651e4cefSPeter Xu if (origin->data & VTD_IR_MSI_DATA_RESERVED) { 31884e4abd11SPeter Xu error_report_once("%s: invalid IR MSI " 31894e4abd11SPeter Xu "(sid=%u, address=0x%" PRIx64 31904e4abd11SPeter Xu ", data=0x%" PRIx32 ")", 31914e4abd11SPeter Xu __func__, sid, origin->address, origin->data); 3192651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3193651e4cefSPeter Xu } 3194651e4cefSPeter Xu } else { 3195651e4cefSPeter Xu uint8_t vector = origin->data & 0xff; 3196dea651a9SFeng Wu uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1; 3197dea651a9SFeng Wu 31987feb51b7SPeter Xu trace_vtd_ir_remap_type("IOAPIC"); 3199651e4cefSPeter Xu /* IOAPIC entry vector should be aligned with IRTE vector 3200651e4cefSPeter Xu * (see vt-d spec 5.1.5.1). */ 3201651e4cefSPeter Xu if (vector != irq.vector) { 32027feb51b7SPeter Xu trace_vtd_warn_ir_vector(sid, index, vector, irq.vector); 3203651e4cefSPeter Xu } 3204dea651a9SFeng Wu 3205dea651a9SFeng Wu /* The Trigger Mode field must match the Trigger Mode in the IRTE. 3206dea651a9SFeng Wu * (see vt-d spec 5.1.5.1). */ 3207dea651a9SFeng Wu if (trigger_mode != irq.trigger_mode) { 32087feb51b7SPeter Xu trace_vtd_warn_ir_trigger(sid, index, trigger_mode, 32097feb51b7SPeter Xu irq.trigger_mode); 3210dea651a9SFeng Wu } 3211651e4cefSPeter Xu } 3212651e4cefSPeter Xu 3213651e4cefSPeter Xu /* 3214651e4cefSPeter Xu * We'd better keep the last two bits, assuming that guest OS 3215651e4cefSPeter Xu * might modify it. Keep it does not hurt after all. 3216651e4cefSPeter Xu */ 3217bc38ee10SMichael S. Tsirkin irq.msi_addr_last_bits = addr.addr.__not_care; 3218651e4cefSPeter Xu 321935c24501SSingh, Brijesh /* Translate X86IOMMUIrq to MSI message */ 322035c24501SSingh, Brijesh x86_iommu_irq_to_msi_message(&irq, translated); 3221651e4cefSPeter Xu 3222e7a3b91fSPeter Xu out: 32237feb51b7SPeter Xu trace_vtd_ir_remap_msi(origin->address, origin->data, 3224651e4cefSPeter Xu translated->address, translated->data); 3225651e4cefSPeter Xu return 0; 3226651e4cefSPeter Xu } 3227651e4cefSPeter Xu 32288b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src, 32298b5ed7dfSPeter Xu MSIMessage *dst, uint16_t sid) 32308b5ed7dfSPeter Xu { 3231ede9c94aSPeter Xu return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu), 3232ede9c94aSPeter Xu src, dst, sid); 32338b5ed7dfSPeter Xu } 32348b5ed7dfSPeter Xu 3235651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr, 3236651e4cefSPeter Xu uint64_t *data, unsigned size, 3237651e4cefSPeter Xu MemTxAttrs attrs) 3238651e4cefSPeter Xu { 3239651e4cefSPeter Xu return MEMTX_OK; 3240651e4cefSPeter Xu } 3241651e4cefSPeter Xu 3242651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr, 3243651e4cefSPeter Xu uint64_t value, unsigned size, 3244651e4cefSPeter Xu MemTxAttrs attrs) 3245651e4cefSPeter Xu { 3246651e4cefSPeter Xu int ret = 0; 324709cd058aSMichael S. Tsirkin MSIMessage from = {}, to = {}; 3248ede9c94aSPeter Xu uint16_t sid = X86_IOMMU_SID_INVALID; 3249651e4cefSPeter Xu 3250651e4cefSPeter Xu from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST; 3251651e4cefSPeter Xu from.data = (uint32_t) value; 3252651e4cefSPeter Xu 3253ede9c94aSPeter Xu if (!attrs.unspecified) { 3254ede9c94aSPeter Xu /* We have explicit Source ID */ 3255ede9c94aSPeter Xu sid = attrs.requester_id; 3256ede9c94aSPeter Xu } 3257ede9c94aSPeter Xu 3258ede9c94aSPeter Xu ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid); 3259651e4cefSPeter Xu if (ret) { 3260651e4cefSPeter Xu /* TODO: report error */ 3261651e4cefSPeter Xu /* Drop this interrupt */ 3262651e4cefSPeter Xu return MEMTX_ERROR; 3263651e4cefSPeter Xu } 3264651e4cefSPeter Xu 326532946019SRadim Krčmář apic_get_class()->send_msi(&to); 3266651e4cefSPeter Xu 3267651e4cefSPeter Xu return MEMTX_OK; 3268651e4cefSPeter Xu } 3269651e4cefSPeter Xu 3270651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = { 3271651e4cefSPeter Xu .read_with_attrs = vtd_mem_ir_read, 3272651e4cefSPeter Xu .write_with_attrs = vtd_mem_ir_write, 3273651e4cefSPeter Xu .endianness = DEVICE_LITTLE_ENDIAN, 3274651e4cefSPeter Xu .impl = { 3275651e4cefSPeter Xu .min_access_size = 4, 3276651e4cefSPeter Xu .max_access_size = 4, 3277651e4cefSPeter Xu }, 3278651e4cefSPeter Xu .valid = { 3279651e4cefSPeter Xu .min_access_size = 4, 3280651e4cefSPeter Xu .max_access_size = 4, 3281651e4cefSPeter Xu }, 3282651e4cefSPeter Xu }; 32837df953bdSKnut Omang 32847df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) 32857df953bdSKnut Omang { 32867df953bdSKnut Omang uintptr_t key = (uintptr_t)bus; 32877df953bdSKnut Omang VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key); 32887df953bdSKnut Omang VTDAddressSpace *vtd_dev_as; 3289e0a3c8ccSJason Wang char name[128]; 32907df953bdSKnut Omang 32917df953bdSKnut Omang if (!vtd_bus) { 32922d3fc581SJason Wang uintptr_t *new_key = g_malloc(sizeof(*new_key)); 32932d3fc581SJason Wang *new_key = (uintptr_t)bus; 32947df953bdSKnut Omang /* No corresponding free() */ 329504af0e18SPeter Xu vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \ 3296bf33cc75SPeter Xu PCI_DEVFN_MAX); 32977df953bdSKnut Omang vtd_bus->bus = bus; 32982d3fc581SJason Wang g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); 32997df953bdSKnut Omang } 33007df953bdSKnut Omang 33017df953bdSKnut Omang vtd_dev_as = vtd_bus->dev_as[devfn]; 33027df953bdSKnut Omang 33037df953bdSKnut Omang if (!vtd_dev_as) { 33044b519ef1SPeter Xu snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn), 33054b519ef1SPeter Xu PCI_FUNC(devfn)); 33067df953bdSKnut Omang vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace)); 33077df953bdSKnut Omang 33087df953bdSKnut Omang vtd_dev_as->bus = bus; 33097df953bdSKnut Omang vtd_dev_as->devfn = (uint8_t)devfn; 33107df953bdSKnut Omang vtd_dev_as->iommu_state = s; 33117df953bdSKnut Omang vtd_dev_as->context_cache_entry.context_cache_gen = 0; 331263b88968SPeter Xu vtd_dev_as->iova_tree = iova_tree_new(); 3313558e0024SPeter Xu 33144b519ef1SPeter Xu memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX); 33154b519ef1SPeter Xu address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root"); 33164b519ef1SPeter Xu 3317558e0024SPeter Xu /* 33184b519ef1SPeter Xu * Build the DMAR-disabled container with aliases to the 33194b519ef1SPeter Xu * shared MRs. Note that aliasing to a shared memory region 33204b519ef1SPeter Xu * could help the memory API to detect same FlatViews so we 33214b519ef1SPeter Xu * can have devices to share the same FlatView when DMAR is 33224b519ef1SPeter Xu * disabled (either by not providing "intel_iommu=on" or with 33234b519ef1SPeter Xu * "iommu=pt"). It will greatly reduce the total number of 33244b519ef1SPeter Xu * FlatViews of the system hence VM runs faster. 3325558e0024SPeter Xu */ 33264b519ef1SPeter Xu memory_region_init_alias(&vtd_dev_as->nodmar, OBJECT(s), 33274b519ef1SPeter Xu "vtd-nodmar", &s->mr_nodmar, 0, 33284b519ef1SPeter Xu memory_region_size(&s->mr_nodmar)); 33294b519ef1SPeter Xu 33304b519ef1SPeter Xu /* 33314b519ef1SPeter Xu * Build the per-device DMAR-enabled container. 33324b519ef1SPeter Xu * 33334b519ef1SPeter Xu * TODO: currently we have per-device IOMMU memory region only 33344b519ef1SPeter Xu * because we have per-device IOMMU notifiers for devices. If 33354b519ef1SPeter Xu * one day we can abstract the IOMMU notifiers out of the 33364b519ef1SPeter Xu * memory regions then we can also share the same memory 33374b519ef1SPeter Xu * region here just like what we've done above with the nodmar 33384b519ef1SPeter Xu * region. 33394b519ef1SPeter Xu */ 33404b519ef1SPeter Xu strcat(name, "-dmar"); 33411221a474SAlexey Kardashevskiy memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu), 33421221a474SAlexey Kardashevskiy TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s), 33434b519ef1SPeter Xu name, UINT64_MAX); 33444b519ef1SPeter Xu memory_region_init_alias(&vtd_dev_as->iommu_ir, OBJECT(s), "vtd-ir", 33454b519ef1SPeter Xu &s->mr_ir, 0, memory_region_size(&s->mr_ir)); 33464b519ef1SPeter Xu memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->iommu), 3347558e0024SPeter Xu VTD_INTERRUPT_ADDR_FIRST, 33484b519ef1SPeter Xu &vtd_dev_as->iommu_ir, 1); 33494b519ef1SPeter Xu 33504b519ef1SPeter Xu /* 33514b519ef1SPeter Xu * Hook both the containers under the root container, we 33524b519ef1SPeter Xu * switch between DMAR & noDMAR by enable/disable 33534b519ef1SPeter Xu * corresponding sub-containers 33544b519ef1SPeter Xu */ 3355558e0024SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 33563df9d748SAlexey Kardashevskiy MEMORY_REGION(&vtd_dev_as->iommu), 33574b519ef1SPeter Xu 0); 33584b519ef1SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 33594b519ef1SPeter Xu &vtd_dev_as->nodmar, 0); 33604b519ef1SPeter Xu 3361558e0024SPeter Xu vtd_switch_address_space(vtd_dev_as); 33627df953bdSKnut Omang } 33637df953bdSKnut Omang return vtd_dev_as; 33647df953bdSKnut Omang } 33657df953bdSKnut Omang 3366dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */ 3367dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) 3368dd4d607eSPeter Xu { 3369dd4d607eSPeter Xu IOMMUTLBEntry entry; 3370dd4d607eSPeter Xu hwaddr size; 3371dd4d607eSPeter Xu hwaddr start = n->start; 3372dd4d607eSPeter Xu hwaddr end = n->end; 337337f51384SPrasad Singamsetty IntelIOMMUState *s = as->iommu_state; 337463b88968SPeter Xu DMAMap map; 3375dd4d607eSPeter Xu 3376dd4d607eSPeter Xu /* 3377dd4d607eSPeter Xu * Note: all the codes in this function has a assumption that IOVA 3378dd4d607eSPeter Xu * bits are no more than VTD_MGAW bits (which is restricted by 3379dd4d607eSPeter Xu * VT-d spec), otherwise we need to consider overflow of 64 bits. 3380dd4d607eSPeter Xu */ 3381dd4d607eSPeter Xu 3382*d6d10793SYan Zhao if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) { 3383dd4d607eSPeter Xu /* 3384dd4d607eSPeter Xu * Don't need to unmap regions that is bigger than the whole 3385dd4d607eSPeter Xu * VT-d supported address space size 3386dd4d607eSPeter Xu */ 3387*d6d10793SYan Zhao end = VTD_ADDRESS_SIZE(s->aw_bits) - 1; 3388dd4d607eSPeter Xu } 3389dd4d607eSPeter Xu 3390dd4d607eSPeter Xu assert(start <= end); 3391dd4d607eSPeter Xu size = end - start; 3392dd4d607eSPeter Xu 3393dd4d607eSPeter Xu if (ctpop64(size) != 1) { 3394dd4d607eSPeter Xu /* 3395dd4d607eSPeter Xu * This size cannot format a correct mask. Let's enlarge it to 3396dd4d607eSPeter Xu * suite the minimum available mask. 3397dd4d607eSPeter Xu */ 3398dd4d607eSPeter Xu int n = 64 - clz64(size); 339937f51384SPrasad Singamsetty if (n > s->aw_bits) { 3400dd4d607eSPeter Xu /* should not happen, but in case it happens, limit it */ 340137f51384SPrasad Singamsetty n = s->aw_bits; 3402dd4d607eSPeter Xu } 3403dd4d607eSPeter Xu size = 1ULL << n; 3404dd4d607eSPeter Xu } 3405dd4d607eSPeter Xu 3406dd4d607eSPeter Xu entry.target_as = &address_space_memory; 3407dd4d607eSPeter Xu /* Adjust iova for the size */ 3408dd4d607eSPeter Xu entry.iova = n->start & ~(size - 1); 3409dd4d607eSPeter Xu /* This field is meaningless for unmap */ 3410dd4d607eSPeter Xu entry.translated_addr = 0; 3411dd4d607eSPeter Xu entry.perm = IOMMU_NONE; 3412dd4d607eSPeter Xu entry.addr_mask = size - 1; 3413dd4d607eSPeter Xu 3414dd4d607eSPeter Xu trace_vtd_as_unmap_whole(pci_bus_num(as->bus), 3415dd4d607eSPeter Xu VTD_PCI_SLOT(as->devfn), 3416dd4d607eSPeter Xu VTD_PCI_FUNC(as->devfn), 3417dd4d607eSPeter Xu entry.iova, size); 3418dd4d607eSPeter Xu 341963b88968SPeter Xu map.iova = entry.iova; 342063b88968SPeter Xu map.size = entry.addr_mask; 342163b88968SPeter Xu iova_tree_remove(as->iova_tree, &map); 342263b88968SPeter Xu 3423dd4d607eSPeter Xu memory_region_notify_one(n, &entry); 3424dd4d607eSPeter Xu } 3425dd4d607eSPeter Xu 3426dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s) 3427dd4d607eSPeter Xu { 3428dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 3429dd4d607eSPeter Xu IOMMUNotifier *n; 3430dd4d607eSPeter Xu 3431b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 3432dd4d607eSPeter Xu IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) { 3433dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 3434dd4d607eSPeter Xu } 3435dd4d607eSPeter Xu } 3436dd4d607eSPeter Xu } 3437dd4d607eSPeter Xu 34382cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s) 34392cc9ddccSPeter Xu { 34402cc9ddccSPeter Xu vtd_address_space_unmap_all(s); 34412cc9ddccSPeter Xu vtd_switch_address_space_all(s); 34422cc9ddccSPeter Xu } 34432cc9ddccSPeter Xu 3444f06a696dSPeter Xu static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private) 3445f06a696dSPeter Xu { 3446f06a696dSPeter Xu memory_region_notify_one((IOMMUNotifier *)private, entry); 3447f06a696dSPeter Xu return 0; 3448f06a696dSPeter Xu } 3449f06a696dSPeter Xu 34503df9d748SAlexey Kardashevskiy static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) 3451f06a696dSPeter Xu { 34523df9d748SAlexey Kardashevskiy VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu); 3453f06a696dSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 3454f06a696dSPeter Xu uint8_t bus_n = pci_bus_num(vtd_as->bus); 3455f06a696dSPeter Xu VTDContextEntry ce; 3456f06a696dSPeter Xu 3457f06a696dSPeter Xu /* 3458dd4d607eSPeter Xu * The replay can be triggered by either a invalidation or a newly 3459dd4d607eSPeter Xu * created entry. No matter what, we release existing mappings 3460dd4d607eSPeter Xu * (it means flushing caches for UNMAP-only registers). 3461f06a696dSPeter Xu */ 3462dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 3463dd4d607eSPeter Xu 3464dd4d607eSPeter Xu if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) { 3465fb43cf73SLiu, Yi L trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : 3466fb43cf73SLiu, Yi L "legacy mode", 3467fb43cf73SLiu, Yi L bus_n, PCI_SLOT(vtd_as->devfn), 3468f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn), 3469fb43cf73SLiu, Yi L vtd_get_domain_id(s, &ce), 3470f06a696dSPeter Xu ce.hi, ce.lo); 34714f8a62a9SPeter Xu if (vtd_as_has_map_notifier(vtd_as)) { 34724f8a62a9SPeter Xu /* This is required only for MAP typed notifiers */ 3473fe215b0cSPeter Xu vtd_page_walk_info info = { 3474fe215b0cSPeter Xu .hook_fn = vtd_replay_hook, 3475fe215b0cSPeter Xu .private = (void *)n, 3476fe215b0cSPeter Xu .notify_unmap = false, 3477fe215b0cSPeter Xu .aw = s->aw_bits, 34782f764fa8SPeter Xu .as = vtd_as, 3479fb43cf73SLiu, Yi L .domain_id = vtd_get_domain_id(s, &ce), 3480fe215b0cSPeter Xu }; 3481fe215b0cSPeter Xu 3482fb43cf73SLiu, Yi L vtd_page_walk(s, &ce, 0, ~0ULL, &info); 34834f8a62a9SPeter Xu } 3484f06a696dSPeter Xu } else { 3485f06a696dSPeter Xu trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), 3486f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn)); 3487f06a696dSPeter Xu } 3488f06a696dSPeter Xu 3489f06a696dSPeter Xu return; 3490f06a696dSPeter Xu } 3491f06a696dSPeter Xu 34921da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay 34931da12ec4SLe Tan * attention when adding new initialization stuff. 34941da12ec4SLe Tan */ 34951da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s) 34961da12ec4SLe Tan { 3497d54bd7f8SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 3498d54bd7f8SPeter Xu 34991da12ec4SLe Tan memset(s->csr, 0, DMAR_REG_SIZE); 35001da12ec4SLe Tan memset(s->wmask, 0, DMAR_REG_SIZE); 35011da12ec4SLe Tan memset(s->w1cmask, 0, DMAR_REG_SIZE); 35021da12ec4SLe Tan memset(s->womask, 0, DMAR_REG_SIZE); 35031da12ec4SLe Tan 35041da12ec4SLe Tan s->root = 0; 3505fb43cf73SLiu, Yi L s->root_scalable = false; 35061da12ec4SLe Tan s->dmar_enabled = false; 3507d7bb469aSPeter Xu s->intr_enabled = false; 35081da12ec4SLe Tan s->iq_head = 0; 35091da12ec4SLe Tan s->iq_tail = 0; 35101da12ec4SLe Tan s->iq = 0; 35111da12ec4SLe Tan s->iq_size = 0; 35121da12ec4SLe Tan s->qi_enabled = false; 35131da12ec4SLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 3514c0c1d351SLiu, Yi L s->iq_dw = false; 35151da12ec4SLe Tan s->next_frcd_reg = 0; 351692e5d85eSPrasad Singamsetty s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | 351792e5d85eSPrasad Singamsetty VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS | 351837f51384SPrasad Singamsetty VTD_CAP_SAGAW_39bit | VTD_CAP_MGAW(s->aw_bits); 3519ccc23bb0SPeter Xu if (s->dma_drain) { 3520ccc23bb0SPeter Xu s->cap |= VTD_CAP_DRAIN; 3521ccc23bb0SPeter Xu } 352237f51384SPrasad Singamsetty if (s->aw_bits == VTD_HOST_AW_48BIT) { 352337f51384SPrasad Singamsetty s->cap |= VTD_CAP_SAGAW_48bit; 352437f51384SPrasad Singamsetty } 3525ed7b8fbcSLe Tan s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO; 35261da12ec4SLe Tan 352792e5d85eSPrasad Singamsetty /* 352892e5d85eSPrasad Singamsetty * Rsvd field masks for spte 352992e5d85eSPrasad Singamsetty */ 353092e5d85eSPrasad Singamsetty vtd_paging_entry_rsvd_field[0] = ~0ULL; 353137f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits); 353237f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits); 353337f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits); 353437f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits); 353537f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[5] = VTD_SPTE_LPAGE_L1_RSVD_MASK(s->aw_bits); 353637f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[6] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits); 353737f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[7] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits); 353837f51384SPrasad Singamsetty vtd_paging_entry_rsvd_field[8] = VTD_SPTE_LPAGE_L4_RSVD_MASK(s->aw_bits); 353992e5d85eSPrasad Singamsetty 3540a924b3d8SPeter Xu if (x86_iommu_ir_supported(x86_iommu)) { 3541e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV; 3542e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON) { 3543e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_EIM; 3544e6b6af05SRadim Krčmář } 3545e6b6af05SRadim Krčmář assert(s->intr_eim != ON_OFF_AUTO_AUTO); 3546d54bd7f8SPeter Xu } 3547d54bd7f8SPeter Xu 3548554f5e16SJason Wang if (x86_iommu->dt_supported) { 3549554f5e16SJason Wang s->ecap |= VTD_ECAP_DT; 3550554f5e16SJason Wang } 3551554f5e16SJason Wang 3552dbaabb25SPeter Xu if (x86_iommu->pt_supported) { 3553dbaabb25SPeter Xu s->ecap |= VTD_ECAP_PT; 3554dbaabb25SPeter Xu } 3555dbaabb25SPeter Xu 35563b40f0e5SAviv Ben-David if (s->caching_mode) { 35573b40f0e5SAviv Ben-David s->cap |= VTD_CAP_CM; 35583b40f0e5SAviv Ben-David } 35593b40f0e5SAviv Ben-David 35604a4f219eSYi Sun /* TODO: read cap/ecap from host to decide which cap to be exposed. */ 35614a4f219eSYi Sun if (s->scalable_mode) { 35624a4f219eSYi Sun s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; 35634a4f219eSYi Sun } 35644a4f219eSYi Sun 356506aba4caSPeter Xu vtd_reset_caches(s); 3566d92fa2dcSLe Tan 35671da12ec4SLe Tan /* Define registers with default values and bit semantics */ 35681da12ec4SLe Tan vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0); 35691da12ec4SLe Tan vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0); 35701da12ec4SLe Tan vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0); 35711da12ec4SLe Tan vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0); 35721da12ec4SLe Tan vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL); 35731da12ec4SLe Tan vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0); 3574fb43cf73SLiu, Yi L vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0); 35751da12ec4SLe Tan vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0); 35761da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL); 35771da12ec4SLe Tan 35781da12ec4SLe Tan /* Advanced Fault Logging not supported */ 35791da12ec4SLe Tan vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL); 35801da12ec4SLe Tan vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0); 35811da12ec4SLe Tan vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0); 35821da12ec4SLe Tan vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0); 35831da12ec4SLe Tan 35841da12ec4SLe Tan /* Treated as RsvdZ when EIM in ECAP_REG is not supported 35851da12ec4SLe Tan * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0); 35861da12ec4SLe Tan */ 35871da12ec4SLe Tan vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0); 35881da12ec4SLe Tan 35891da12ec4SLe Tan /* Treated as RO for implementations that PLMR and PHMR fields reported 35901da12ec4SLe Tan * as Clear in the CAP_REG. 35911da12ec4SLe Tan * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0); 35921da12ec4SLe Tan */ 35931da12ec4SLe Tan vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0); 35941da12ec4SLe Tan 3595ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0); 3596ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0); 3597c0c1d351SLiu, Yi L vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff807ULL, 0); 3598ed7b8fbcSLe Tan vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL); 3599ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0); 3600ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0); 3601ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0); 3602ed7b8fbcSLe Tan /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */ 3603ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0); 3604ed7b8fbcSLe Tan 36051da12ec4SLe Tan /* IOTLB registers */ 36061da12ec4SLe Tan vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0); 36071da12ec4SLe Tan vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0); 36081da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL); 36091da12ec4SLe Tan 36101da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 36111da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0); 36121da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL); 3613a5861439SPeter Xu 3614a5861439SPeter Xu /* 361528589311SJan Kiszka * Interrupt remapping registers. 3616a5861439SPeter Xu */ 361728589311SJan Kiszka vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0); 36181da12ec4SLe Tan } 36191da12ec4SLe Tan 36201da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use 36211da12ec4SLe Tan * the address space they got at first (won't ask the bus again). 36221da12ec4SLe Tan */ 36231da12ec4SLe Tan static void vtd_reset(DeviceState *dev) 36241da12ec4SLe Tan { 36251da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 36261da12ec4SLe Tan 36271da12ec4SLe Tan vtd_init(s); 36282cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 36291da12ec4SLe Tan } 36301da12ec4SLe Tan 3631621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) 3632621d983aSMarcel Apfelbaum { 3633621d983aSMarcel Apfelbaum IntelIOMMUState *s = opaque; 3634621d983aSMarcel Apfelbaum VTDAddressSpace *vtd_as; 3635621d983aSMarcel Apfelbaum 3636bf33cc75SPeter Xu assert(0 <= devfn && devfn < PCI_DEVFN_MAX); 3637621d983aSMarcel Apfelbaum 3638621d983aSMarcel Apfelbaum vtd_as = vtd_find_add_as(s, bus, devfn); 3639621d983aSMarcel Apfelbaum return &vtd_as->as; 3640621d983aSMarcel Apfelbaum } 3641621d983aSMarcel Apfelbaum 3642e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) 36436333e93cSRadim Krčmář { 3644e6b6af05SRadim Krčmář X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 3645e6b6af05SRadim Krčmář 3646a924b3d8SPeter Xu if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) { 3647e6b6af05SRadim Krčmář error_setg(errp, "eim=on cannot be selected without intremap=on"); 3648e6b6af05SRadim Krčmář return false; 3649e6b6af05SRadim Krčmář } 3650e6b6af05SRadim Krčmář 3651e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_AUTO) { 3652fb506e70SRadim Krčmář s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim) 3653a924b3d8SPeter Xu && x86_iommu_ir_supported(x86_iommu) ? 3654e6b6af05SRadim Krčmář ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 3655e6b6af05SRadim Krčmář } 3656fb506e70SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) { 3657fb506e70SRadim Krčmář if (!kvm_irqchip_in_kernel()) { 3658fb506e70SRadim Krčmář error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split"); 3659fb506e70SRadim Krčmář return false; 3660fb506e70SRadim Krčmář } 3661fb506e70SRadim Krčmář if (!kvm_enable_x2apic()) { 3662fb506e70SRadim Krčmář error_setg(errp, "eim=on requires support on the KVM side" 3663fb506e70SRadim Krčmář "(X2APIC_API, first shipped in v4.7)"); 3664fb506e70SRadim Krčmář return false; 3665fb506e70SRadim Krčmář } 3666fb506e70SRadim Krčmář } 3667e6b6af05SRadim Krčmář 366837f51384SPrasad Singamsetty /* Currently only address widths supported are 39 and 48 bits */ 366937f51384SPrasad Singamsetty if ((s->aw_bits != VTD_HOST_AW_39BIT) && 367037f51384SPrasad Singamsetty (s->aw_bits != VTD_HOST_AW_48BIT)) { 367137f51384SPrasad Singamsetty error_setg(errp, "Supported values for x-aw-bits are: %d, %d", 367237f51384SPrasad Singamsetty VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT); 367337f51384SPrasad Singamsetty return false; 367437f51384SPrasad Singamsetty } 367537f51384SPrasad Singamsetty 36764a4f219eSYi Sun if (s->scalable_mode && !s->dma_drain) { 36774a4f219eSYi Sun error_setg(errp, "Need to set dma_drain for scalable mode"); 36784a4f219eSYi Sun return false; 36794a4f219eSYi Sun } 36804a4f219eSYi Sun 36816333e93cSRadim Krčmář return true; 36826333e93cSRadim Krčmář } 36836333e93cSRadim Krčmář 36841da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp) 36851da12ec4SLe Tan { 3686ef0e8fc7SEduardo Habkost MachineState *ms = MACHINE(qdev_get_machine()); 368729396ed9SMohammed Gamal PCMachineState *pcms = PC_MACHINE(ms); 368829396ed9SMohammed Gamal PCIBus *bus = pcms->bus; 36891da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 36904684a204SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev); 36911da12ec4SLe Tan 3692fb9f5926SDavid Kiarie x86_iommu->type = TYPE_INTEL; 36936333e93cSRadim Krčmář 3694e6b6af05SRadim Krčmář if (!vtd_decide_config(s, errp)) { 36956333e93cSRadim Krčmář return; 36966333e93cSRadim Krčmář } 36976333e93cSRadim Krčmář 3698b4a4ba0dSPeter Xu QLIST_INIT(&s->vtd_as_with_notifiers); 36991d9efa73SPeter Xu qemu_mutex_init(&s->iommu_lock); 37007df953bdSKnut Omang memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num)); 37011da12ec4SLe Tan memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s, 37021da12ec4SLe Tan "intel_iommu", DMAR_REG_SIZE); 37034b519ef1SPeter Xu 37044b519ef1SPeter Xu /* Create the shared memory regions by all devices */ 37054b519ef1SPeter Xu memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar", 37064b519ef1SPeter Xu UINT64_MAX); 37074b519ef1SPeter Xu memory_region_init_io(&s->mr_ir, OBJECT(s), &vtd_mem_ir_ops, 37084b519ef1SPeter Xu s, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE); 37094b519ef1SPeter Xu memory_region_init_alias(&s->mr_sys_alias, OBJECT(s), 37104b519ef1SPeter Xu "vtd-sys-alias", get_system_memory(), 0, 37114b519ef1SPeter Xu memory_region_size(get_system_memory())); 37124b519ef1SPeter Xu memory_region_add_subregion_overlap(&s->mr_nodmar, 0, 37134b519ef1SPeter Xu &s->mr_sys_alias, 0); 37144b519ef1SPeter Xu memory_region_add_subregion_overlap(&s->mr_nodmar, 37154b519ef1SPeter Xu VTD_INTERRUPT_ADDR_FIRST, 37164b519ef1SPeter Xu &s->mr_ir, 1); 37174b519ef1SPeter Xu 37181da12ec4SLe Tan sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem); 3719b5a280c0SLe Tan /* No corresponding destroy */ 3720b5a280c0SLe Tan s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 3721b5a280c0SLe Tan g_free, g_free); 37227df953bdSKnut Omang s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 37237df953bdSKnut Omang g_free, g_free); 37241da12ec4SLe Tan vtd_init(s); 3725621d983aSMarcel Apfelbaum sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); 3726621d983aSMarcel Apfelbaum pci_setup_iommu(bus, vtd_host_dma_iommu, dev); 3727cb135f59SPeter Xu /* Pseudo address space under root PCI bus. */ 3728cb135f59SPeter Xu pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC); 37291da12ec4SLe Tan } 37301da12ec4SLe Tan 37311da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data) 37321da12ec4SLe Tan { 37331da12ec4SLe Tan DeviceClass *dc = DEVICE_CLASS(klass); 37341c7955c4SPeter Xu X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass); 37351da12ec4SLe Tan 37361da12ec4SLe Tan dc->reset = vtd_reset; 37371da12ec4SLe Tan dc->vmsd = &vtd_vmstate; 37381da12ec4SLe Tan dc->props = vtd_properties; 3739621d983aSMarcel Apfelbaum dc->hotpluggable = false; 37401c7955c4SPeter Xu x86_class->realize = vtd_realize; 37418b5ed7dfSPeter Xu x86_class->int_remap = vtd_int_remap; 37428ab5700cSEduardo Habkost /* Supported by the pc-q35-* machine types */ 3743e4f4fb1eSEduardo Habkost dc->user_creatable = true; 37441ec202c9SErnest Esene set_bit(DEVICE_CATEGORY_MISC, dc->categories); 37451ec202c9SErnest Esene dc->desc = "Intel IOMMU (VT-d) DMA Remapping device"; 37461da12ec4SLe Tan } 37471da12ec4SLe Tan 37481da12ec4SLe Tan static const TypeInfo vtd_info = { 37491da12ec4SLe Tan .name = TYPE_INTEL_IOMMU_DEVICE, 37501c7955c4SPeter Xu .parent = TYPE_X86_IOMMU_DEVICE, 37511da12ec4SLe Tan .instance_size = sizeof(IntelIOMMUState), 37521da12ec4SLe Tan .class_init = vtd_class_init, 37531da12ec4SLe Tan }; 37541da12ec4SLe Tan 37551221a474SAlexey Kardashevskiy static void vtd_iommu_memory_region_class_init(ObjectClass *klass, 37561221a474SAlexey Kardashevskiy void *data) 37571221a474SAlexey Kardashevskiy { 37581221a474SAlexey Kardashevskiy IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 37591221a474SAlexey Kardashevskiy 37601221a474SAlexey Kardashevskiy imrc->translate = vtd_iommu_translate; 37611221a474SAlexey Kardashevskiy imrc->notify_flag_changed = vtd_iommu_notify_flag_changed; 37621221a474SAlexey Kardashevskiy imrc->replay = vtd_iommu_replay; 37631221a474SAlexey Kardashevskiy } 37641221a474SAlexey Kardashevskiy 37651221a474SAlexey Kardashevskiy static const TypeInfo vtd_iommu_memory_region_info = { 37661221a474SAlexey Kardashevskiy .parent = TYPE_IOMMU_MEMORY_REGION, 37671221a474SAlexey Kardashevskiy .name = TYPE_INTEL_IOMMU_MEMORY_REGION, 37681221a474SAlexey Kardashevskiy .class_init = vtd_iommu_memory_region_class_init, 37691221a474SAlexey Kardashevskiy }; 37701221a474SAlexey Kardashevskiy 37711da12ec4SLe Tan static void vtd_register_types(void) 37721da12ec4SLe Tan { 37731da12ec4SLe Tan type_register_static(&vtd_info); 37741221a474SAlexey Kardashevskiy type_register_static(&vtd_iommu_memory_region_info); 37751da12ec4SLe Tan } 37761da12ec4SLe Tan 37771da12ec4SLe Tan type_init(vtd_register_types) 3778