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" 24db725815SMarkus Armbruster #include "qemu/main-loop.h" 256333e93cSRadim Krčmář #include "qapi/error.h" 261da12ec4SLe Tan #include "hw/sysbus.h" 271da12ec4SLe Tan #include "intel_iommu_internal.h" 287df953bdSKnut Omang #include "hw/pci/pci.h" 293cb3b154SAlex Williamson #include "hw/pci/pci_bus.h" 30a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 31621d983aSMarcel Apfelbaum #include "hw/i386/pc.h" 32dea651a9SFeng Wu #include "hw/i386/apic-msidef.h" 3304af0e18SPeter Xu #include "hw/i386/x86-iommu.h" 34cb135f59SPeter Xu #include "hw/pci-host/q35.h" 354684a204SPeter Xu #include "sysemu/kvm.h" 36f14fb6c2SEric Auger #include "sysemu/dma.h" 3728cf553aSPeter Xu #include "sysemu/sysemu.h" 3832946019SRadim Krčmář #include "hw/i386/apic_internal.h" 39a9dc68d9SClaudio Fontana #include "kvm/kvm_i386.h" 40d6454270SMarkus Armbruster #include "migration/vmstate.h" 41bc535e59SPeter Xu #include "trace.h" 421da12ec4SLe Tan 43fb43cf73SLiu, Yi L /* context entry operations */ 44fb43cf73SLiu, Yi L #define VTD_CE_GET_RID2PASID(ce) \ 45fb43cf73SLiu, Yi L ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK) 46fb43cf73SLiu, Yi L #define VTD_CE_GET_PASID_DIR_TABLE(ce) \ 47fb43cf73SLiu, Yi L ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK) 48fb43cf73SLiu, Yi L 49fb43cf73SLiu, Yi L /* pe operations */ 50fb43cf73SLiu, Yi L #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT) 51fb43cf73SLiu, Yi L #define VTD_PE_GET_LEVEL(pe) (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW)) 52fb43cf73SLiu, Yi L #define VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write) {\ 53fb43cf73SLiu, Yi L if (ret_fr) { \ 54fb43cf73SLiu, Yi L ret_fr = -ret_fr; \ 55fb43cf73SLiu, Yi L if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) { \ 56fb43cf73SLiu, Yi L trace_vtd_fault_disabled(); \ 57fb43cf73SLiu, Yi L } else { \ 58fb43cf73SLiu, Yi L vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write); \ 59fb43cf73SLiu, Yi L } \ 60fb43cf73SLiu, Yi L goto error; \ 61fb43cf73SLiu, Yi L } \ 62fb43cf73SLiu, Yi L } 63fb43cf73SLiu, Yi L 642cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s); 65c28b535dSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n); 662cc9ddccSPeter Xu 6728cf553aSPeter Xu static void vtd_panic_require_caching_mode(void) 6828cf553aSPeter Xu { 6928cf553aSPeter Xu error_report("We need to set caching-mode=on for intel-iommu to enable " 7028cf553aSPeter Xu "device assignment with IOMMU protection."); 7128cf553aSPeter Xu exit(1); 7228cf553aSPeter Xu } 7328cf553aSPeter Xu 741da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val, 751da12ec4SLe Tan uint64_t wmask, uint64_t w1cmask) 761da12ec4SLe Tan { 771da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 781da12ec4SLe Tan stq_le_p(&s->wmask[addr], wmask); 791da12ec4SLe Tan stq_le_p(&s->w1cmask[addr], w1cmask); 801da12ec4SLe Tan } 811da12ec4SLe Tan 821da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask) 831da12ec4SLe Tan { 841da12ec4SLe Tan stq_le_p(&s->womask[addr], mask); 851da12ec4SLe Tan } 861da12ec4SLe Tan 871da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val, 881da12ec4SLe Tan uint32_t wmask, uint32_t w1cmask) 891da12ec4SLe Tan { 901da12ec4SLe Tan stl_le_p(&s->csr[addr], val); 911da12ec4SLe Tan stl_le_p(&s->wmask[addr], wmask); 921da12ec4SLe Tan stl_le_p(&s->w1cmask[addr], w1cmask); 931da12ec4SLe Tan } 941da12ec4SLe Tan 951da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask) 961da12ec4SLe Tan { 971da12ec4SLe Tan stl_le_p(&s->womask[addr], mask); 981da12ec4SLe Tan } 991da12ec4SLe Tan 1001da12ec4SLe Tan /* "External" get/set operations */ 1011da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val) 1021da12ec4SLe Tan { 1031da12ec4SLe Tan uint64_t oldval = ldq_le_p(&s->csr[addr]); 1041da12ec4SLe Tan uint64_t wmask = ldq_le_p(&s->wmask[addr]); 1051da12ec4SLe Tan uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]); 1061da12ec4SLe Tan stq_le_p(&s->csr[addr], 1071da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 1081da12ec4SLe Tan } 1091da12ec4SLe Tan 1101da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val) 1111da12ec4SLe Tan { 1121da12ec4SLe Tan uint32_t oldval = ldl_le_p(&s->csr[addr]); 1131da12ec4SLe Tan uint32_t wmask = ldl_le_p(&s->wmask[addr]); 1141da12ec4SLe Tan uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]); 1151da12ec4SLe Tan stl_le_p(&s->csr[addr], 1161da12ec4SLe Tan ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val)); 1171da12ec4SLe Tan } 1181da12ec4SLe Tan 1191da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr) 1201da12ec4SLe Tan { 1211da12ec4SLe Tan uint64_t val = ldq_le_p(&s->csr[addr]); 1221da12ec4SLe Tan uint64_t womask = ldq_le_p(&s->womask[addr]); 1231da12ec4SLe Tan return val & ~womask; 1241da12ec4SLe Tan } 1251da12ec4SLe Tan 1261da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr) 1271da12ec4SLe Tan { 1281da12ec4SLe Tan uint32_t val = ldl_le_p(&s->csr[addr]); 1291da12ec4SLe Tan uint32_t womask = ldl_le_p(&s->womask[addr]); 1301da12ec4SLe Tan return val & ~womask; 1311da12ec4SLe Tan } 1321da12ec4SLe Tan 1331da12ec4SLe Tan /* "Internal" get/set operations */ 1341da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr) 1351da12ec4SLe Tan { 1361da12ec4SLe Tan return ldq_le_p(&s->csr[addr]); 1371da12ec4SLe Tan } 1381da12ec4SLe Tan 1391da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr) 1401da12ec4SLe Tan { 1411da12ec4SLe Tan return ldl_le_p(&s->csr[addr]); 1421da12ec4SLe Tan } 1431da12ec4SLe Tan 1441da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val) 1451da12ec4SLe Tan { 1461da12ec4SLe Tan stq_le_p(&s->csr[addr], val); 1471da12ec4SLe Tan } 1481da12ec4SLe Tan 1491da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr, 1501da12ec4SLe Tan uint32_t clear, uint32_t mask) 1511da12ec4SLe Tan { 1521da12ec4SLe Tan uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask; 1531da12ec4SLe Tan stl_le_p(&s->csr[addr], new_val); 1541da12ec4SLe Tan return new_val; 1551da12ec4SLe Tan } 1561da12ec4SLe Tan 1571da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, 1581da12ec4SLe Tan uint64_t clear, uint64_t mask) 1591da12ec4SLe Tan { 1601da12ec4SLe Tan uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask; 1611da12ec4SLe Tan stq_le_p(&s->csr[addr], new_val); 1621da12ec4SLe Tan return new_val; 1631da12ec4SLe Tan } 1641da12ec4SLe Tan 1651d9efa73SPeter Xu static inline void vtd_iommu_lock(IntelIOMMUState *s) 1661d9efa73SPeter Xu { 1671d9efa73SPeter Xu qemu_mutex_lock(&s->iommu_lock); 1681d9efa73SPeter Xu } 1691d9efa73SPeter Xu 1701d9efa73SPeter Xu static inline void vtd_iommu_unlock(IntelIOMMUState *s) 1711d9efa73SPeter Xu { 1721d9efa73SPeter Xu qemu_mutex_unlock(&s->iommu_lock); 1731d9efa73SPeter Xu } 1741d9efa73SPeter Xu 1752811af3bSPeter Xu static void vtd_update_scalable_state(IntelIOMMUState *s) 1762811af3bSPeter Xu { 1772811af3bSPeter Xu uint64_t val = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 1782811af3bSPeter Xu 1792811af3bSPeter Xu if (s->scalable_mode) { 1802811af3bSPeter Xu s->root_scalable = val & VTD_RTADDR_SMT; 1812811af3bSPeter Xu } 1822811af3bSPeter Xu } 1832811af3bSPeter Xu 1844f8a62a9SPeter Xu /* Whether the address space needs to notify new mappings */ 1854f8a62a9SPeter Xu static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as) 1864f8a62a9SPeter Xu { 1874f8a62a9SPeter Xu return as->notifier_flags & IOMMU_NOTIFIER_MAP; 1884f8a62a9SPeter Xu } 1894f8a62a9SPeter Xu 190b5a280c0SLe Tan /* GHashTable functions */ 191b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) 192b5a280c0SLe Tan { 193b5a280c0SLe Tan return *((const uint64_t *)v1) == *((const uint64_t *)v2); 194b5a280c0SLe Tan } 195b5a280c0SLe Tan 196b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v) 197b5a280c0SLe Tan { 198b5a280c0SLe Tan return (guint)*(const uint64_t *)v; 199b5a280c0SLe Tan } 200b5a280c0SLe Tan 201b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value, 202b5a280c0SLe Tan gpointer user_data) 203b5a280c0SLe Tan { 204b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 205b5a280c0SLe Tan uint16_t domain_id = *(uint16_t *)user_data; 206b5a280c0SLe Tan return entry->domain_id == domain_id; 207b5a280c0SLe Tan } 208b5a280c0SLe Tan 209d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */ 210d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level) 211d66b969bSJason Wang { 2127e58326aSPeter Xu assert(level != 0); 213d66b969bSJason Wang return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS; 214d66b969bSJason Wang } 215d66b969bSJason Wang 216d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level) 217d66b969bSJason Wang { 218d66b969bSJason Wang return ~((1ULL << vtd_slpt_level_shift(level)) - 1); 219d66b969bSJason Wang } 220d66b969bSJason Wang 221b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value, 222b5a280c0SLe Tan gpointer user_data) 223b5a280c0SLe Tan { 224b5a280c0SLe Tan VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value; 225b5a280c0SLe Tan VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data; 226d66b969bSJason Wang uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask; 227d66b969bSJason Wang uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K; 228b5a280c0SLe Tan return (entry->domain_id == info->domain_id) && 229d66b969bSJason Wang (((entry->gfn & info->mask) == gfn) || 230d66b969bSJason Wang (entry->gfn == gfn_tlb)); 231b5a280c0SLe Tan } 232b5a280c0SLe Tan 233d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of 2341d9efa73SPeter Xu * IntelIOMMUState to 1. Must be called with IOMMU lock held. 235d92fa2dcSLe Tan */ 2361d9efa73SPeter Xu static void vtd_reset_context_cache_locked(IntelIOMMUState *s) 237d92fa2dcSLe Tan { 238d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 2397df953bdSKnut Omang VTDBus *vtd_bus; 2407df953bdSKnut Omang GHashTableIter bus_it; 241d92fa2dcSLe Tan uint32_t devfn_it; 242d92fa2dcSLe Tan 2437feb51b7SPeter Xu trace_vtd_context_cache_reset(); 2447feb51b7SPeter Xu 2457df953bdSKnut Omang g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr); 2467df953bdSKnut Omang 2477df953bdSKnut Omang while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) { 248bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 2497df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 250d92fa2dcSLe Tan if (!vtd_as) { 251d92fa2dcSLe Tan continue; 252d92fa2dcSLe Tan } 253d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 254d92fa2dcSLe Tan } 255d92fa2dcSLe Tan } 256d92fa2dcSLe Tan s->context_cache_gen = 1; 257d92fa2dcSLe Tan } 258d92fa2dcSLe Tan 2591d9efa73SPeter Xu /* Must be called with IOMMU lock held. */ 2601d9efa73SPeter Xu static void vtd_reset_iotlb_locked(IntelIOMMUState *s) 261b5a280c0SLe Tan { 262b5a280c0SLe Tan assert(s->iotlb); 263b5a280c0SLe Tan g_hash_table_remove_all(s->iotlb); 264b5a280c0SLe Tan } 265b5a280c0SLe Tan 2661d9efa73SPeter Xu static void vtd_reset_iotlb(IntelIOMMUState *s) 2671d9efa73SPeter Xu { 2681d9efa73SPeter Xu vtd_iommu_lock(s); 2691d9efa73SPeter Xu vtd_reset_iotlb_locked(s); 2701d9efa73SPeter Xu vtd_iommu_unlock(s); 2711d9efa73SPeter Xu } 2721d9efa73SPeter Xu 27306aba4caSPeter Xu static void vtd_reset_caches(IntelIOMMUState *s) 27406aba4caSPeter Xu { 27506aba4caSPeter Xu vtd_iommu_lock(s); 27606aba4caSPeter Xu vtd_reset_iotlb_locked(s); 27706aba4caSPeter Xu vtd_reset_context_cache_locked(s); 27806aba4caSPeter Xu vtd_iommu_unlock(s); 27906aba4caSPeter Xu } 28006aba4caSPeter Xu 281bacabb0aSJason Wang static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id, 282d66b969bSJason Wang uint32_t level) 283d66b969bSJason Wang { 284d66b969bSJason Wang return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) | 285d66b969bSJason Wang ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT); 286d66b969bSJason Wang } 287d66b969bSJason Wang 288d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level) 289d66b969bSJason Wang { 290d66b969bSJason Wang return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K; 291d66b969bSJason Wang } 292d66b969bSJason Wang 2931d9efa73SPeter Xu /* Must be called with IOMMU lock held */ 294b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id, 295b5a280c0SLe Tan hwaddr addr) 296b5a280c0SLe Tan { 297d66b969bSJason Wang VTDIOTLBEntry *entry; 298b5a280c0SLe Tan uint64_t key; 299d66b969bSJason Wang int level; 300b5a280c0SLe Tan 301d66b969bSJason Wang for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) { 302d66b969bSJason Wang key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level), 303d66b969bSJason Wang source_id, level); 304d66b969bSJason Wang entry = g_hash_table_lookup(s->iotlb, &key); 305d66b969bSJason Wang if (entry) { 306d66b969bSJason Wang goto out; 307d66b969bSJason Wang } 308d66b969bSJason Wang } 309b5a280c0SLe Tan 310d66b969bSJason Wang out: 311d66b969bSJason Wang return entry; 312b5a280c0SLe Tan } 313b5a280c0SLe Tan 3141d9efa73SPeter Xu /* Must be with IOMMU lock held */ 315b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, 316b5a280c0SLe Tan uint16_t domain_id, hwaddr addr, uint64_t slpte, 31707f7b733SPeter Xu uint8_t access_flags, uint32_t level) 318b5a280c0SLe Tan { 319b5a280c0SLe Tan VTDIOTLBEntry *entry = g_malloc(sizeof(*entry)); 320b5a280c0SLe Tan uint64_t *key = g_malloc(sizeof(*key)); 321d66b969bSJason Wang uint64_t gfn = vtd_get_iotlb_gfn(addr, level); 322b5a280c0SLe Tan 3236c441e1dSPeter Xu trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); 324b5a280c0SLe Tan if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) { 3256c441e1dSPeter Xu trace_vtd_iotlb_reset("iotlb exceeds size limit"); 3261d9efa73SPeter Xu vtd_reset_iotlb_locked(s); 327b5a280c0SLe Tan } 328b5a280c0SLe Tan 329b5a280c0SLe Tan entry->gfn = gfn; 330b5a280c0SLe Tan entry->domain_id = domain_id; 331b5a280c0SLe Tan entry->slpte = slpte; 33207f7b733SPeter Xu entry->access_flags = access_flags; 333d66b969bSJason Wang entry->mask = vtd_slpt_level_page_mask(level); 334d66b969bSJason Wang *key = vtd_get_iotlb_key(gfn, source_id, level); 335b5a280c0SLe Tan g_hash_table_replace(s->iotlb, key, entry); 336b5a280c0SLe Tan } 337b5a280c0SLe Tan 3381da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an 3391da12ec4SLe Tan * interrupt via MSI. 3401da12ec4SLe Tan */ 3411da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg, 3421da12ec4SLe Tan hwaddr mesg_data_reg) 3431da12ec4SLe Tan { 34432946019SRadim Krčmář MSIMessage msi; 3451da12ec4SLe Tan 3461da12ec4SLe Tan assert(mesg_data_reg < DMAR_REG_SIZE); 3471da12ec4SLe Tan assert(mesg_addr_reg < DMAR_REG_SIZE); 3481da12ec4SLe Tan 34932946019SRadim Krčmář msi.address = vtd_get_long_raw(s, mesg_addr_reg); 35032946019SRadim Krčmář msi.data = vtd_get_long_raw(s, mesg_data_reg); 3511da12ec4SLe Tan 3527feb51b7SPeter Xu trace_vtd_irq_generate(msi.address, msi.data); 3537feb51b7SPeter Xu 35432946019SRadim Krčmář apic_get_class()->send_msi(&msi); 3551da12ec4SLe Tan } 3561da12ec4SLe Tan 3571da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met. 3581da12ec4SLe Tan * Notice that the value of FSTS_REG being passed to it should be the one 3591da12ec4SLe Tan * before any update. 3601da12ec4SLe Tan */ 3611da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts) 3621da12ec4SLe Tan { 3631da12ec4SLe Tan if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO || 3641da12ec4SLe Tan pre_fsts & VTD_FSTS_IQE) { 3651376211fSPeter Xu error_report_once("There are previous interrupt conditions " 3667feb51b7SPeter Xu "to be serviced by software, fault event " 3671376211fSPeter Xu "is not generated"); 3681da12ec4SLe Tan return; 3691da12ec4SLe Tan } 3701da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP); 3711da12ec4SLe Tan if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) { 3721376211fSPeter Xu error_report_once("Interrupt Mask set, irq is not generated"); 3731da12ec4SLe Tan } else { 3741da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 3751da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 3761da12ec4SLe Tan } 3771da12ec4SLe Tan } 3781da12ec4SLe Tan 3791da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by 3801da12ec4SLe Tan * @index is Set. 3811da12ec4SLe Tan */ 3821da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index) 3831da12ec4SLe Tan { 3841da12ec4SLe Tan /* Each reg is 128-bit */ 3851da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 3861da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 3871da12ec4SLe Tan 3881da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 3891da12ec4SLe Tan 3901da12ec4SLe Tan return vtd_get_quad_raw(s, addr) & VTD_FRCD_F; 3911da12ec4SLe Tan } 3921da12ec4SLe Tan 3931da12ec4SLe Tan /* Update the PPF field of Fault Status Register. 3941da12ec4SLe Tan * Should be called whenever change the F field of any fault recording 3951da12ec4SLe Tan * registers. 3961da12ec4SLe Tan */ 3971da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s) 3981da12ec4SLe Tan { 3991da12ec4SLe Tan uint32_t i; 4001da12ec4SLe Tan uint32_t ppf_mask = 0; 4011da12ec4SLe Tan 4021da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 4031da12ec4SLe Tan if (vtd_is_frcd_set(s, i)) { 4041da12ec4SLe Tan ppf_mask = VTD_FSTS_PPF; 4051da12ec4SLe Tan break; 4061da12ec4SLe Tan } 4071da12ec4SLe Tan } 4081da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask); 4097feb51b7SPeter Xu trace_vtd_fsts_ppf(!!ppf_mask); 4101da12ec4SLe Tan } 4111da12ec4SLe Tan 4121da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index) 4131da12ec4SLe Tan { 4141da12ec4SLe Tan /* Each reg is 128-bit */ 4151da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 4161da12ec4SLe Tan addr += 8; /* Access the high 64-bit half */ 4171da12ec4SLe Tan 4181da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 4191da12ec4SLe Tan 4201da12ec4SLe Tan vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F); 4211da12ec4SLe Tan vtd_update_fsts_ppf(s); 4221da12ec4SLe Tan } 4231da12ec4SLe Tan 4241da12ec4SLe Tan /* Must not update F field now, should be done later */ 4251da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index, 4261da12ec4SLe Tan uint16_t source_id, hwaddr addr, 4271da12ec4SLe Tan VTDFaultReason fault, bool is_write) 4281da12ec4SLe Tan { 4291da12ec4SLe Tan uint64_t hi = 0, lo; 4301da12ec4SLe Tan hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4); 4311da12ec4SLe Tan 4321da12ec4SLe Tan assert(index < DMAR_FRCD_REG_NR); 4331da12ec4SLe Tan 4341da12ec4SLe Tan lo = VTD_FRCD_FI(addr); 4351da12ec4SLe Tan hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault); 4361da12ec4SLe Tan if (!is_write) { 4371da12ec4SLe Tan hi |= VTD_FRCD_T; 4381da12ec4SLe Tan } 4391da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr, lo); 4401da12ec4SLe Tan vtd_set_quad_raw(s, frcd_reg_addr + 8, hi); 4417feb51b7SPeter Xu 4427feb51b7SPeter Xu trace_vtd_frr_new(index, hi, lo); 4431da12ec4SLe Tan } 4441da12ec4SLe Tan 4451da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */ 4461da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id) 4471da12ec4SLe Tan { 4481da12ec4SLe Tan uint32_t i; 4491da12ec4SLe Tan uint64_t frcd_reg; 4501da12ec4SLe Tan hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */ 4511da12ec4SLe Tan 4521da12ec4SLe Tan for (i = 0; i < DMAR_FRCD_REG_NR; i++) { 4531da12ec4SLe Tan frcd_reg = vtd_get_quad_raw(s, addr); 4541da12ec4SLe Tan if ((frcd_reg & VTD_FRCD_F) && 4551da12ec4SLe Tan ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) { 4561da12ec4SLe Tan return true; 4571da12ec4SLe Tan } 4581da12ec4SLe Tan addr += 16; /* 128-bit for each */ 4591da12ec4SLe Tan } 4601da12ec4SLe Tan return false; 4611da12ec4SLe Tan } 4621da12ec4SLe Tan 4631da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */ 4641da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id, 4651da12ec4SLe Tan hwaddr addr, VTDFaultReason fault, 4661da12ec4SLe Tan bool is_write) 4671da12ec4SLe Tan { 4681da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 4691da12ec4SLe Tan 4701da12ec4SLe Tan assert(fault < VTD_FR_MAX); 4711da12ec4SLe Tan 4721da12ec4SLe Tan if (fault == VTD_FR_RESERVED_ERR) { 4731da12ec4SLe Tan /* This is not a normal fault reason case. Drop it. */ 4741da12ec4SLe Tan return; 4751da12ec4SLe Tan } 4767feb51b7SPeter Xu 4777feb51b7SPeter Xu trace_vtd_dmar_fault(source_id, fault, addr, is_write); 4787feb51b7SPeter Xu 4791da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PFO) { 4801376211fSPeter Xu error_report_once("New fault is not recorded due to " 4811376211fSPeter Xu "Primary Fault Overflow"); 4821da12ec4SLe Tan return; 4831da12ec4SLe Tan } 4847feb51b7SPeter Xu 4851da12ec4SLe Tan if (vtd_try_collapse_fault(s, source_id)) { 4861376211fSPeter Xu error_report_once("New fault is not recorded due to " 4871376211fSPeter Xu "compression of faults"); 4881da12ec4SLe Tan return; 4891da12ec4SLe Tan } 4907feb51b7SPeter Xu 4911da12ec4SLe Tan if (vtd_is_frcd_set(s, s->next_frcd_reg)) { 4921376211fSPeter Xu error_report_once("Next Fault Recording Reg is used, " 4931376211fSPeter Xu "new fault is not recorded, set PFO field"); 4941da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO); 4951da12ec4SLe Tan return; 4961da12ec4SLe Tan } 4971da12ec4SLe Tan 4981da12ec4SLe Tan vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write); 4991da12ec4SLe Tan 5001da12ec4SLe Tan if (fsts_reg & VTD_FSTS_PPF) { 5011376211fSPeter Xu error_report_once("There are pending faults already, " 5021376211fSPeter Xu "fault event is not generated"); 5031da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); 5041da12ec4SLe Tan s->next_frcd_reg++; 5051da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 5061da12ec4SLe Tan s->next_frcd_reg = 0; 5071da12ec4SLe Tan } 5081da12ec4SLe Tan } else { 5091da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK, 5101da12ec4SLe Tan VTD_FSTS_FRI(s->next_frcd_reg)); 5111da12ec4SLe Tan vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */ 5121da12ec4SLe Tan s->next_frcd_reg++; 5131da12ec4SLe Tan if (s->next_frcd_reg == DMAR_FRCD_REG_NR) { 5141da12ec4SLe Tan s->next_frcd_reg = 0; 5151da12ec4SLe Tan } 5161da12ec4SLe Tan /* This case actually cause the PPF to be Set. 5171da12ec4SLe Tan * So generate fault event (interrupt). 5181da12ec4SLe Tan */ 5191da12ec4SLe Tan vtd_generate_fault_event(s, fsts_reg); 5201da12ec4SLe Tan } 5211da12ec4SLe Tan } 5221da12ec4SLe Tan 523ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error 524ed7b8fbcSLe Tan * conditions. 525ed7b8fbcSLe Tan */ 526ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s) 527ed7b8fbcSLe Tan { 528ed7b8fbcSLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 529ed7b8fbcSLe Tan 530ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE); 531ed7b8fbcSLe Tan vtd_generate_fault_event(s, fsts_reg); 532ed7b8fbcSLe Tan } 533ed7b8fbcSLe Tan 534ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */ 535ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s) 536ed7b8fbcSLe Tan { 537ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) { 538bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("One pending, skip current"); 539ed7b8fbcSLe Tan return; 540ed7b8fbcSLe Tan } 541ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC); 542ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP); 543ed7b8fbcSLe Tan if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) { 544bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, " 545bc535e59SPeter Xu "new event not generated"); 546ed7b8fbcSLe Tan return; 547ed7b8fbcSLe Tan } else { 548ed7b8fbcSLe Tan /* Generate the interrupt event */ 549bc535e59SPeter Xu trace_vtd_inv_desc_wait_irq("Generating complete event"); 550ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 551ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 552ed7b8fbcSLe Tan } 553ed7b8fbcSLe Tan } 554ed7b8fbcSLe Tan 555fb43cf73SLiu, Yi L static inline bool vtd_root_entry_present(IntelIOMMUState *s, 556fb43cf73SLiu, Yi L VTDRootEntry *re, 557fb43cf73SLiu, Yi L uint8_t devfn) 5581da12ec4SLe Tan { 559fb43cf73SLiu, Yi L if (s->root_scalable && devfn > UINT8_MAX / 2) { 560fb43cf73SLiu, Yi L return re->hi & VTD_ROOT_ENTRY_P; 561fb43cf73SLiu, Yi L } 562fb43cf73SLiu, Yi L 563fb43cf73SLiu, Yi L return re->lo & VTD_ROOT_ENTRY_P; 5641da12ec4SLe Tan } 5651da12ec4SLe Tan 5661da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index, 5671da12ec4SLe Tan VTDRootEntry *re) 5681da12ec4SLe Tan { 5691da12ec4SLe Tan dma_addr_t addr; 5701da12ec4SLe Tan 5711da12ec4SLe Tan addr = s->root + index * sizeof(*re); 572ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 573ba06fe8aSPhilippe Mathieu-Daudé re, sizeof(*re), MEMTXATTRS_UNSPECIFIED)) { 574fb43cf73SLiu, Yi L re->lo = 0; 5751da12ec4SLe Tan return -VTD_FR_ROOT_TABLE_INV; 5761da12ec4SLe Tan } 577fb43cf73SLiu, Yi L re->lo = le64_to_cpu(re->lo); 578fb43cf73SLiu, Yi L re->hi = le64_to_cpu(re->hi); 5791da12ec4SLe Tan return 0; 5801da12ec4SLe Tan } 5811da12ec4SLe Tan 5828f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context) 5831da12ec4SLe Tan { 5841da12ec4SLe Tan return context->lo & VTD_CONTEXT_ENTRY_P; 5851da12ec4SLe Tan } 5861da12ec4SLe Tan 587fb43cf73SLiu, Yi L static int vtd_get_context_entry_from_root(IntelIOMMUState *s, 588fb43cf73SLiu, Yi L VTDRootEntry *re, 589fb43cf73SLiu, Yi L uint8_t index, 5901da12ec4SLe Tan VTDContextEntry *ce) 5911da12ec4SLe Tan { 592fb43cf73SLiu, Yi L dma_addr_t addr, ce_size; 5931da12ec4SLe Tan 5946c441e1dSPeter Xu /* we have checked that root entry is present */ 595fb43cf73SLiu, Yi L ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE : 596fb43cf73SLiu, Yi L VTD_CTX_ENTRY_LEGACY_SIZE; 597fb43cf73SLiu, Yi L 598fb43cf73SLiu, Yi L if (s->root_scalable && index > UINT8_MAX / 2) { 599fb43cf73SLiu, Yi L index = index & (~VTD_DEVFN_CHECK_MASK); 600fb43cf73SLiu, Yi L addr = re->hi & VTD_ROOT_ENTRY_CTP; 601fb43cf73SLiu, Yi L } else { 602fb43cf73SLiu, Yi L addr = re->lo & VTD_ROOT_ENTRY_CTP; 603fb43cf73SLiu, Yi L } 604fb43cf73SLiu, Yi L 605fb43cf73SLiu, Yi L addr = addr + index * ce_size; 606ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 607ba06fe8aSPhilippe Mathieu-Daudé ce, ce_size, MEMTXATTRS_UNSPECIFIED)) { 6081da12ec4SLe Tan return -VTD_FR_CONTEXT_TABLE_INV; 6091da12ec4SLe Tan } 610fb43cf73SLiu, Yi L 6111da12ec4SLe Tan ce->lo = le64_to_cpu(ce->lo); 6121da12ec4SLe Tan ce->hi = le64_to_cpu(ce->hi); 613fb43cf73SLiu, Yi L if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) { 614fb43cf73SLiu, Yi L ce->val[2] = le64_to_cpu(ce->val[2]); 615fb43cf73SLiu, Yi L ce->val[3] = le64_to_cpu(ce->val[3]); 616fb43cf73SLiu, Yi L } 6171da12ec4SLe Tan return 0; 6181da12ec4SLe Tan } 6191da12ec4SLe Tan 6208f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce) 6211da12ec4SLe Tan { 6221da12ec4SLe Tan return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR; 6231da12ec4SLe Tan } 6241da12ec4SLe Tan 62537f51384SPrasad Singamsetty static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw) 6261da12ec4SLe Tan { 62737f51384SPrasad Singamsetty return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw); 6281da12ec4SLe Tan } 6291da12ec4SLe Tan 6301da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */ 6311da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level) 6321da12ec4SLe Tan { 6331da12ec4SLe Tan return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK); 6341da12ec4SLe Tan } 6351da12ec4SLe Tan 6361da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */ 6371da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index) 6381da12ec4SLe Tan { 6391da12ec4SLe Tan uint64_t slpte; 6401da12ec4SLe Tan 6411da12ec4SLe Tan assert(index < VTD_SL_PT_ENTRY_NR); 6421da12ec4SLe Tan 6431da12ec4SLe Tan if (dma_memory_read(&address_space_memory, 644ba06fe8aSPhilippe Mathieu-Daudé base_addr + index * sizeof(slpte), 645ba06fe8aSPhilippe Mathieu-Daudé &slpte, sizeof(slpte), MEMTXATTRS_UNSPECIFIED)) { 6461da12ec4SLe Tan slpte = (uint64_t)-1; 6471da12ec4SLe Tan return slpte; 6481da12ec4SLe Tan } 6491da12ec4SLe Tan slpte = le64_to_cpu(slpte); 6501da12ec4SLe Tan return slpte; 6511da12ec4SLe Tan } 6521da12ec4SLe Tan 6536e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset 6546e905564SPeter Xu * of current level. 6551da12ec4SLe Tan */ 6566e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level) 6571da12ec4SLe Tan { 6586e905564SPeter Xu return (iova >> vtd_slpt_level_shift(level)) & 6591da12ec4SLe Tan ((1ULL << VTD_SL_LEVEL_BITS) - 1); 6601da12ec4SLe Tan } 6611da12ec4SLe Tan 6621da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */ 6631da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level) 6641da12ec4SLe Tan { 6651da12ec4SLe Tan return VTD_CAP_SAGAW_MASK & s->cap & 6661da12ec4SLe Tan (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT)); 6671da12ec4SLe Tan } 6681da12ec4SLe Tan 669fb43cf73SLiu, Yi L /* Return true if check passed, otherwise false */ 670fb43cf73SLiu, Yi L static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu, 671fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 672fb43cf73SLiu, Yi L { 673fb43cf73SLiu, Yi L switch (VTD_PE_GET_TYPE(pe)) { 674fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_FLT: 675fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_SLT: 676fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_NESTED: 677fb43cf73SLiu, Yi L break; 678fb43cf73SLiu, Yi L case VTD_SM_PASID_ENTRY_PT: 679fb43cf73SLiu, Yi L if (!x86_iommu->pt_supported) { 680fb43cf73SLiu, Yi L return false; 681fb43cf73SLiu, Yi L } 682fb43cf73SLiu, Yi L break; 683fb43cf73SLiu, Yi L default: 68437557b09SCai Huoqing /* Unknown type */ 685fb43cf73SLiu, Yi L return false; 686fb43cf73SLiu, Yi L } 687fb43cf73SLiu, Yi L return true; 688fb43cf73SLiu, Yi L } 689fb43cf73SLiu, Yi L 69056fc1e6aSLiu Yi L static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire) 69156fc1e6aSLiu Yi L { 69256fc1e6aSLiu Yi L return pdire->val & 1; 69356fc1e6aSLiu Yi L } 69456fc1e6aSLiu Yi L 69556fc1e6aSLiu Yi L /** 69656fc1e6aSLiu Yi L * Caller of this function should check present bit if wants 69737557b09SCai Huoqing * to use pdir entry for further usage except for fpd bit check. 69856fc1e6aSLiu Yi L */ 69956fc1e6aSLiu Yi L static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, 700fb43cf73SLiu, Yi L uint32_t pasid, 701fb43cf73SLiu, Yi L VTDPASIDDirEntry *pdire) 702fb43cf73SLiu, Yi L { 703fb43cf73SLiu, Yi L uint32_t index; 704fb43cf73SLiu, Yi L dma_addr_t addr, entry_size; 705fb43cf73SLiu, Yi L 706fb43cf73SLiu, Yi L index = VTD_PASID_DIR_INDEX(pasid); 707fb43cf73SLiu, Yi L entry_size = VTD_PASID_DIR_ENTRY_SIZE; 708fb43cf73SLiu, Yi L addr = pasid_dir_base + index * entry_size; 709ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 710ba06fe8aSPhilippe Mathieu-Daudé pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) { 711fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 712fb43cf73SLiu, Yi L } 713fb43cf73SLiu, Yi L 714fb43cf73SLiu, Yi L return 0; 715fb43cf73SLiu, Yi L } 716fb43cf73SLiu, Yi L 71756fc1e6aSLiu Yi L static inline bool vtd_pe_present(VTDPASIDEntry *pe) 71856fc1e6aSLiu Yi L { 71956fc1e6aSLiu Yi L return pe->val[0] & VTD_PASID_ENTRY_P; 72056fc1e6aSLiu Yi L } 72156fc1e6aSLiu Yi L 72256fc1e6aSLiu Yi L static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, 723fb43cf73SLiu, Yi L uint32_t pasid, 72456fc1e6aSLiu Yi L dma_addr_t addr, 725fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 726fb43cf73SLiu, Yi L { 727fb43cf73SLiu, Yi L uint32_t index; 72856fc1e6aSLiu Yi L dma_addr_t entry_size; 729fb43cf73SLiu, Yi L X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 730fb43cf73SLiu, Yi L 731fb43cf73SLiu, Yi L index = VTD_PASID_TABLE_INDEX(pasid); 732fb43cf73SLiu, Yi L entry_size = VTD_PASID_ENTRY_SIZE; 733fb43cf73SLiu, Yi L addr = addr + index * entry_size; 734ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 735ba06fe8aSPhilippe Mathieu-Daudé pe, entry_size, MEMTXATTRS_UNSPECIFIED)) { 736fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 737fb43cf73SLiu, Yi L } 738fb43cf73SLiu, Yi L 739fb43cf73SLiu, Yi L /* Do translation type check */ 740fb43cf73SLiu, Yi L if (!vtd_pe_type_check(x86_iommu, pe)) { 741fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 742fb43cf73SLiu, Yi L } 743fb43cf73SLiu, Yi L 744fb43cf73SLiu, Yi L if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) { 745fb43cf73SLiu, Yi L return -VTD_FR_PASID_TABLE_INV; 746fb43cf73SLiu, Yi L } 747fb43cf73SLiu, Yi L 748fb43cf73SLiu, Yi L return 0; 749fb43cf73SLiu, Yi L } 750fb43cf73SLiu, Yi L 75156fc1e6aSLiu Yi L /** 75256fc1e6aSLiu Yi L * Caller of this function should check present bit if wants 75337557b09SCai Huoqing * to use pasid entry for further usage except for fpd bit check. 75456fc1e6aSLiu Yi L */ 75556fc1e6aSLiu Yi L static int vtd_get_pe_from_pdire(IntelIOMMUState *s, 75656fc1e6aSLiu Yi L uint32_t pasid, 75756fc1e6aSLiu Yi L VTDPASIDDirEntry *pdire, 75856fc1e6aSLiu Yi L VTDPASIDEntry *pe) 75956fc1e6aSLiu Yi L { 76056fc1e6aSLiu Yi L dma_addr_t addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK; 76156fc1e6aSLiu Yi L 76256fc1e6aSLiu Yi L return vtd_get_pe_in_pasid_leaf_table(s, pasid, addr, pe); 76356fc1e6aSLiu Yi L } 76456fc1e6aSLiu Yi L 76556fc1e6aSLiu Yi L /** 76656fc1e6aSLiu Yi L * This function gets a pasid entry from a specified pasid 76756fc1e6aSLiu Yi L * table (includes dir and leaf table) with a specified pasid. 76856fc1e6aSLiu Yi L * Sanity check should be done to ensure return a present 76956fc1e6aSLiu Yi L * pasid entry to caller. 77056fc1e6aSLiu Yi L */ 77156fc1e6aSLiu Yi L static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s, 772fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base, 773fb43cf73SLiu, Yi L uint32_t pasid, 774fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 775fb43cf73SLiu, Yi L { 776fb43cf73SLiu, Yi L int ret; 777fb43cf73SLiu, Yi L VTDPASIDDirEntry pdire; 778fb43cf73SLiu, Yi L 77956fc1e6aSLiu Yi L ret = vtd_get_pdire_from_pdir_table(pasid_dir_base, 78056fc1e6aSLiu Yi L pasid, &pdire); 781fb43cf73SLiu, Yi L if (ret) { 782fb43cf73SLiu, Yi L return ret; 783fb43cf73SLiu, Yi L } 784fb43cf73SLiu, Yi L 78556fc1e6aSLiu Yi L if (!vtd_pdire_present(&pdire)) { 78656fc1e6aSLiu Yi L return -VTD_FR_PASID_TABLE_INV; 78756fc1e6aSLiu Yi L } 78856fc1e6aSLiu Yi L 78956fc1e6aSLiu Yi L ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe); 790fb43cf73SLiu, Yi L if (ret) { 791fb43cf73SLiu, Yi L return ret; 792fb43cf73SLiu, Yi L } 793fb43cf73SLiu, Yi L 79456fc1e6aSLiu Yi L if (!vtd_pe_present(pe)) { 79556fc1e6aSLiu Yi L return -VTD_FR_PASID_TABLE_INV; 79656fc1e6aSLiu Yi L } 79756fc1e6aSLiu Yi L 79856fc1e6aSLiu Yi L return 0; 799fb43cf73SLiu, Yi L } 800fb43cf73SLiu, Yi L 801fb43cf73SLiu, Yi L static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s, 802fb43cf73SLiu, Yi L VTDContextEntry *ce, 803fb43cf73SLiu, Yi L VTDPASIDEntry *pe) 804fb43cf73SLiu, Yi L { 805fb43cf73SLiu, Yi L uint32_t pasid; 806fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base; 807fb43cf73SLiu, Yi L int ret = 0; 808fb43cf73SLiu, Yi L 809fb43cf73SLiu, Yi L pasid = VTD_CE_GET_RID2PASID(ce); 810fb43cf73SLiu, Yi L pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); 81156fc1e6aSLiu Yi L ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe); 812fb43cf73SLiu, Yi L 813fb43cf73SLiu, Yi L return ret; 814fb43cf73SLiu, Yi L } 815fb43cf73SLiu, Yi L 816fb43cf73SLiu, Yi L static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s, 817fb43cf73SLiu, Yi L VTDContextEntry *ce, 818fb43cf73SLiu, Yi L bool *pe_fpd_set) 819fb43cf73SLiu, Yi L { 820fb43cf73SLiu, Yi L int ret; 821fb43cf73SLiu, Yi L uint32_t pasid; 822fb43cf73SLiu, Yi L dma_addr_t pasid_dir_base; 823fb43cf73SLiu, Yi L VTDPASIDDirEntry pdire; 824fb43cf73SLiu, Yi L VTDPASIDEntry pe; 825fb43cf73SLiu, Yi L 826fb43cf73SLiu, Yi L pasid = VTD_CE_GET_RID2PASID(ce); 827fb43cf73SLiu, Yi L pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); 828fb43cf73SLiu, Yi L 82956fc1e6aSLiu Yi L /* 83056fc1e6aSLiu Yi L * No present bit check since fpd is meaningful even 83156fc1e6aSLiu Yi L * if the present bit is clear. 83256fc1e6aSLiu Yi L */ 83356fc1e6aSLiu Yi L ret = vtd_get_pdire_from_pdir_table(pasid_dir_base, pasid, &pdire); 834fb43cf73SLiu, Yi L if (ret) { 835fb43cf73SLiu, Yi L return ret; 836fb43cf73SLiu, Yi L } 837fb43cf73SLiu, Yi L 838fb43cf73SLiu, Yi L if (pdire.val & VTD_PASID_DIR_FPD) { 839fb43cf73SLiu, Yi L *pe_fpd_set = true; 840fb43cf73SLiu, Yi L return 0; 841fb43cf73SLiu, Yi L } 842fb43cf73SLiu, Yi L 84356fc1e6aSLiu Yi L if (!vtd_pdire_present(&pdire)) { 84456fc1e6aSLiu Yi L return -VTD_FR_PASID_TABLE_INV; 84556fc1e6aSLiu Yi L } 84656fc1e6aSLiu Yi L 84756fc1e6aSLiu Yi L /* 84856fc1e6aSLiu Yi L * No present bit check since fpd is meaningful even 84956fc1e6aSLiu Yi L * if the present bit is clear. 85056fc1e6aSLiu Yi L */ 85156fc1e6aSLiu Yi L ret = vtd_get_pe_from_pdire(s, pasid, &pdire, &pe); 852fb43cf73SLiu, Yi L if (ret) { 853fb43cf73SLiu, Yi L return ret; 854fb43cf73SLiu, Yi L } 855fb43cf73SLiu, Yi L 856fb43cf73SLiu, Yi L if (pe.val[0] & VTD_PASID_ENTRY_FPD) { 857fb43cf73SLiu, Yi L *pe_fpd_set = true; 858fb43cf73SLiu, Yi L } 859fb43cf73SLiu, Yi L 860fb43cf73SLiu, Yi L return 0; 861fb43cf73SLiu, Yi L } 862fb43cf73SLiu, Yi L 8631da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level 8641da12ec4SLe Tan * page-table walk from the Address Width field of context-entry. 8651da12ec4SLe Tan */ 8668f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce) 8671da12ec4SLe Tan { 8681da12ec4SLe Tan return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW); 8691da12ec4SLe Tan } 8701da12ec4SLe Tan 871fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_level(IntelIOMMUState *s, 872fb43cf73SLiu, Yi L VTDContextEntry *ce) 873fb43cf73SLiu, Yi L { 874fb43cf73SLiu, Yi L VTDPASIDEntry pe; 875fb43cf73SLiu, Yi L 876fb43cf73SLiu, Yi L if (s->root_scalable) { 877fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 878fb43cf73SLiu, Yi L return VTD_PE_GET_LEVEL(&pe); 879fb43cf73SLiu, Yi L } 880fb43cf73SLiu, Yi L 881fb43cf73SLiu, Yi L return vtd_ce_get_level(ce); 882fb43cf73SLiu, Yi L } 883fb43cf73SLiu, Yi L 8848f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce) 8851da12ec4SLe Tan { 8861da12ec4SLe Tan return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9; 8871da12ec4SLe Tan } 8881da12ec4SLe Tan 889fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s, 890fb43cf73SLiu, Yi L VTDContextEntry *ce) 891fb43cf73SLiu, Yi L { 892fb43cf73SLiu, Yi L VTDPASIDEntry pe; 893fb43cf73SLiu, Yi L 894fb43cf73SLiu, Yi L if (s->root_scalable) { 895fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 896fb43cf73SLiu, Yi L return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9; 897fb43cf73SLiu, Yi L } 898fb43cf73SLiu, Yi L 899fb43cf73SLiu, Yi L return vtd_ce_get_agaw(ce); 900fb43cf73SLiu, Yi L } 901fb43cf73SLiu, Yi L 902127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce) 903127ff5c3SPeter Xu { 904127ff5c3SPeter Xu return ce->lo & VTD_CONTEXT_ENTRY_TT; 905127ff5c3SPeter Xu } 906127ff5c3SPeter Xu 907fb43cf73SLiu, Yi L /* Only for Legacy Mode. Return true if check passed, otherwise false */ 908f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu, 909f80c9874SPeter Xu VTDContextEntry *ce) 910f80c9874SPeter Xu { 911f80c9874SPeter Xu switch (vtd_ce_get_type(ce)) { 912f80c9874SPeter Xu case VTD_CONTEXT_TT_MULTI_LEVEL: 913f80c9874SPeter Xu /* Always supported */ 914f80c9874SPeter Xu break; 915f80c9874SPeter Xu case VTD_CONTEXT_TT_DEV_IOTLB: 916f80c9874SPeter Xu if (!x86_iommu->dt_supported) { 917095955b2SPeter Xu error_report_once("%s: DT specified but not supported", __func__); 918f80c9874SPeter Xu return false; 919f80c9874SPeter Xu } 920f80c9874SPeter Xu break; 921dbaabb25SPeter Xu case VTD_CONTEXT_TT_PASS_THROUGH: 922dbaabb25SPeter Xu if (!x86_iommu->pt_supported) { 923095955b2SPeter Xu error_report_once("%s: PT specified but not supported", __func__); 924dbaabb25SPeter Xu return false; 925dbaabb25SPeter Xu } 926dbaabb25SPeter Xu break; 927f80c9874SPeter Xu default: 928fb43cf73SLiu, Yi L /* Unknown type */ 929095955b2SPeter Xu error_report_once("%s: unknown ce type: %"PRIu32, __func__, 930095955b2SPeter Xu vtd_ce_get_type(ce)); 931f80c9874SPeter Xu return false; 932f80c9874SPeter Xu } 933f80c9874SPeter Xu return true; 934f80c9874SPeter Xu } 935f80c9874SPeter Xu 936fb43cf73SLiu, Yi L static inline uint64_t vtd_iova_limit(IntelIOMMUState *s, 937fb43cf73SLiu, Yi L VTDContextEntry *ce, uint8_t aw) 938f06a696dSPeter Xu { 939fb43cf73SLiu, Yi L uint32_t ce_agaw = vtd_get_iova_agaw(s, ce); 94037f51384SPrasad Singamsetty return 1ULL << MIN(ce_agaw, aw); 941f06a696dSPeter Xu } 942f06a696dSPeter Xu 943f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */ 944fb43cf73SLiu, Yi L static inline bool vtd_iova_range_check(IntelIOMMUState *s, 945fb43cf73SLiu, Yi L uint64_t iova, VTDContextEntry *ce, 94637f51384SPrasad Singamsetty uint8_t aw) 947f06a696dSPeter Xu { 948f06a696dSPeter Xu /* 949f06a696dSPeter Xu * Check if @iova is above 2^X-1, where X is the minimum of MGAW 950f06a696dSPeter Xu * in CAP_REG and AW in context-entry. 951f06a696dSPeter Xu */ 952fb43cf73SLiu, Yi L return !(iova & ~(vtd_iova_limit(s, ce, aw) - 1)); 953fb43cf73SLiu, Yi L } 954fb43cf73SLiu, Yi L 955fb43cf73SLiu, Yi L static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s, 956fb43cf73SLiu, Yi L VTDContextEntry *ce) 957fb43cf73SLiu, Yi L { 958fb43cf73SLiu, Yi L VTDPASIDEntry pe; 959fb43cf73SLiu, Yi L 960fb43cf73SLiu, Yi L if (s->root_scalable) { 961fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 962fb43cf73SLiu, Yi L return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR; 963fb43cf73SLiu, Yi L } 964fb43cf73SLiu, Yi L 965fb43cf73SLiu, Yi L return vtd_ce_get_slpt_base(ce); 966f06a696dSPeter Xu } 967f06a696dSPeter Xu 96892e5d85eSPrasad Singamsetty /* 96992e5d85eSPrasad Singamsetty * Rsvd field masks for spte: 970ce586f3bSQi, Yadong * vtd_spte_rsvd 4k pages 971ce586f3bSQi, Yadong * vtd_spte_rsvd_large large pages 97292e5d85eSPrasad Singamsetty */ 973ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd[5]; 974ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd_large[5]; 9751da12ec4SLe Tan 9761da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level) 9771da12ec4SLe Tan { 978ce586f3bSQi, Yadong uint64_t rsvd_mask = vtd_spte_rsvd[level]; 979ce586f3bSQi, Yadong 980ce586f3bSQi, Yadong if ((level == VTD_SL_PD_LEVEL || level == VTD_SL_PDP_LEVEL) && 981ce586f3bSQi, Yadong (slpte & VTD_SL_PT_PAGE_SIZE_MASK)) { 982ce586f3bSQi, Yadong /* large page */ 983ce586f3bSQi, Yadong rsvd_mask = vtd_spte_rsvd_large[level]; 9841da12ec4SLe Tan } 985ce586f3bSQi, Yadong 986ce586f3bSQi, Yadong return slpte & rsvd_mask; 9871da12ec4SLe Tan } 9881da12ec4SLe Tan 989dbaabb25SPeter Xu /* Find the VTD address space associated with a given bus number */ 990dbaabb25SPeter Xu static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) 991dbaabb25SPeter Xu { 992dbaabb25SPeter Xu VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; 993dbaabb25SPeter Xu GHashTableIter iter; 994dbaabb25SPeter Xu 995a6f65f4fSPhilippe Mathieu-Daudé if (vtd_bus) { 996a6f65f4fSPhilippe Mathieu-Daudé return vtd_bus; 997a6f65f4fSPhilippe Mathieu-Daudé } 998a6f65f4fSPhilippe Mathieu-Daudé 999a6f65f4fSPhilippe Mathieu-Daudé /* 1000a6f65f4fSPhilippe Mathieu-Daudé * Iterate over the registered buses to find the one which 1001a6f65f4fSPhilippe Mathieu-Daudé * currently holds this bus number and update the bus_num 1002a6f65f4fSPhilippe Mathieu-Daudé * lookup table. 1003a6f65f4fSPhilippe Mathieu-Daudé */ 1004dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 1005dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 1006dbaabb25SPeter Xu if (pci_bus_num(vtd_bus->bus) == bus_num) { 1007dbaabb25SPeter Xu s->vtd_as_by_bus_num[bus_num] = vtd_bus; 1008dbaabb25SPeter Xu return vtd_bus; 1009dbaabb25SPeter Xu } 1010dbaabb25SPeter Xu } 1011a6f65f4fSPhilippe Mathieu-Daudé 1012a6f65f4fSPhilippe Mathieu-Daudé return NULL; 1013dbaabb25SPeter Xu } 1014dbaabb25SPeter Xu 10156e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level 10161da12ec4SLe Tan * of the translation, can be used for deciding the size of large page. 10171da12ec4SLe Tan */ 1018fb43cf73SLiu, Yi L static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce, 1019fb43cf73SLiu, Yi L uint64_t iova, bool is_write, 10201da12ec4SLe Tan uint64_t *slptep, uint32_t *slpte_level, 102137f51384SPrasad Singamsetty bool *reads, bool *writes, uint8_t aw_bits) 10221da12ec4SLe Tan { 1023fb43cf73SLiu, Yi L dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce); 1024fb43cf73SLiu, Yi L uint32_t level = vtd_get_iova_level(s, ce); 10251da12ec4SLe Tan uint32_t offset; 10261da12ec4SLe Tan uint64_t slpte; 10271da12ec4SLe Tan uint64_t access_right_check; 10281da12ec4SLe Tan 1029fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, iova, ce, aw_bits)) { 10304e4abd11SPeter Xu error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ")", 10314e4abd11SPeter Xu __func__, iova); 10321da12ec4SLe Tan return -VTD_FR_ADDR_BEYOND_MGAW; 10331da12ec4SLe Tan } 10341da12ec4SLe Tan 10351da12ec4SLe Tan /* FIXME: what is the Atomics request here? */ 10361da12ec4SLe Tan access_right_check = is_write ? VTD_SL_W : VTD_SL_R; 10371da12ec4SLe Tan 10381da12ec4SLe Tan while (true) { 10396e905564SPeter Xu offset = vtd_iova_level_offset(iova, level); 10401da12ec4SLe Tan slpte = vtd_get_slpte(addr, offset); 10411da12ec4SLe Tan 10421da12ec4SLe Tan if (slpte == (uint64_t)-1) { 10434e4abd11SPeter Xu error_report_once("%s: detected read error on DMAR slpte " 10444e4abd11SPeter Xu "(iova=0x%" PRIx64 ")", __func__, iova); 1045fb43cf73SLiu, Yi L if (level == vtd_get_iova_level(s, ce)) { 10461da12ec4SLe Tan /* Invalid programming of context-entry */ 10471da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 10481da12ec4SLe Tan } else { 10491da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_INV; 10501da12ec4SLe Tan } 10511da12ec4SLe Tan } 10521da12ec4SLe Tan *reads = (*reads) && (slpte & VTD_SL_R); 10531da12ec4SLe Tan *writes = (*writes) && (slpte & VTD_SL_W); 10541da12ec4SLe Tan if (!(slpte & access_right_check)) { 10554e4abd11SPeter Xu error_report_once("%s: detected slpte permission error " 10564e4abd11SPeter Xu "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", " 10574e4abd11SPeter Xu "slpte=0x%" PRIx64 ", write=%d)", __func__, 10584e4abd11SPeter Xu iova, level, slpte, is_write); 10591da12ec4SLe Tan return is_write ? -VTD_FR_WRITE : -VTD_FR_READ; 10601da12ec4SLe Tan } 10611da12ec4SLe Tan if (vtd_slpte_nonzero_rsvd(slpte, level)) { 10624e4abd11SPeter Xu error_report_once("%s: detected splte reserve non-zero " 10634e4abd11SPeter Xu "iova=0x%" PRIx64 ", level=0x%" PRIx32 10644e4abd11SPeter Xu "slpte=0x%" PRIx64 ")", __func__, iova, 10654e4abd11SPeter Xu level, slpte); 10661da12ec4SLe Tan return -VTD_FR_PAGING_ENTRY_RSVD; 10671da12ec4SLe Tan } 10681da12ec4SLe Tan 10691da12ec4SLe Tan if (vtd_is_last_slpte(slpte, level)) { 10701da12ec4SLe Tan *slptep = slpte; 10711da12ec4SLe Tan *slpte_level = level; 10721da12ec4SLe Tan return 0; 10731da12ec4SLe Tan } 107437f51384SPrasad Singamsetty addr = vtd_get_slpte_addr(slpte, aw_bits); 10751da12ec4SLe Tan level--; 10761da12ec4SLe Tan } 10771da12ec4SLe Tan } 10781da12ec4SLe Tan 10795039caf3SEugenio Pérez typedef int (*vtd_page_walk_hook)(IOMMUTLBEvent *event, void *private); 1080f06a696dSPeter Xu 1081fe215b0cSPeter Xu /** 1082fe215b0cSPeter Xu * Constant information used during page walking 1083fe215b0cSPeter Xu * 1084fe215b0cSPeter Xu * @hook_fn: hook func to be called when detected page 1085fe215b0cSPeter Xu * @private: private data to be passed into hook func 1086fe215b0cSPeter Xu * @notify_unmap: whether we should notify invalid entries 10872f764fa8SPeter Xu * @as: VT-d address space of the device 1088fe215b0cSPeter Xu * @aw: maximum address width 1089d118c06eSPeter Xu * @domain: domain ID of the page walk 1090fe215b0cSPeter Xu */ 1091fe215b0cSPeter Xu typedef struct { 10922f764fa8SPeter Xu VTDAddressSpace *as; 1093fe215b0cSPeter Xu vtd_page_walk_hook hook_fn; 1094fe215b0cSPeter Xu void *private; 1095fe215b0cSPeter Xu bool notify_unmap; 1096fe215b0cSPeter Xu uint8_t aw; 1097d118c06eSPeter Xu uint16_t domain_id; 1098fe215b0cSPeter Xu } vtd_page_walk_info; 1099fe215b0cSPeter Xu 11005039caf3SEugenio Pérez static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info) 110136d2d52bSPeter Xu { 110263b88968SPeter Xu VTDAddressSpace *as = info->as; 1103fe215b0cSPeter Xu vtd_page_walk_hook hook_fn = info->hook_fn; 1104fe215b0cSPeter Xu void *private = info->private; 11055039caf3SEugenio Pérez IOMMUTLBEntry *entry = &event->entry; 110663b88968SPeter Xu DMAMap target = { 110763b88968SPeter Xu .iova = entry->iova, 110863b88968SPeter Xu .size = entry->addr_mask, 110963b88968SPeter Xu .translated_addr = entry->translated_addr, 111063b88968SPeter Xu .perm = entry->perm, 111163b88968SPeter Xu }; 1112a89b34beSEugenio Pérez const DMAMap *mapped = iova_tree_find(as->iova_tree, &target); 111363b88968SPeter Xu 11145039caf3SEugenio Pérez if (event->type == IOMMU_NOTIFIER_UNMAP && !info->notify_unmap) { 111563b88968SPeter Xu trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask); 111663b88968SPeter Xu return 0; 111763b88968SPeter Xu } 1118fe215b0cSPeter Xu 111936d2d52bSPeter Xu assert(hook_fn); 112063b88968SPeter Xu 112163b88968SPeter Xu /* Update local IOVA mapped ranges */ 11225039caf3SEugenio Pérez if (event->type == IOMMU_NOTIFIER_MAP) { 112363b88968SPeter Xu if (mapped) { 112463b88968SPeter Xu /* If it's exactly the same translation, skip */ 112563b88968SPeter Xu if (!memcmp(mapped, &target, sizeof(target))) { 112663b88968SPeter Xu trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask, 112763b88968SPeter Xu entry->translated_addr); 112863b88968SPeter Xu return 0; 112963b88968SPeter Xu } else { 113063b88968SPeter Xu /* 113163b88968SPeter Xu * Translation changed. Normally this should not 113263b88968SPeter Xu * happen, but it can happen when with buggy guest 113363b88968SPeter Xu * OSes. Note that there will be a small window that 113463b88968SPeter Xu * we don't have map at all. But that's the best 113563b88968SPeter Xu * effort we can do. The ideal way to emulate this is 113663b88968SPeter Xu * atomically modify the PTE to follow what has 113763b88968SPeter Xu * changed, but we can't. One example is that vfio 113863b88968SPeter Xu * driver only has VFIO_IOMMU_[UN]MAP_DMA but no 113963b88968SPeter Xu * interface to modify a mapping (meanwhile it seems 114063b88968SPeter Xu * meaningless to even provide one). Anyway, let's 114163b88968SPeter Xu * mark this as a TODO in case one day we'll have 114263b88968SPeter Xu * a better solution. 114363b88968SPeter Xu */ 114463b88968SPeter Xu IOMMUAccessFlags cache_perm = entry->perm; 114563b88968SPeter Xu int ret; 114663b88968SPeter Xu 114763b88968SPeter Xu /* Emulate an UNMAP */ 11485039caf3SEugenio Pérez event->type = IOMMU_NOTIFIER_UNMAP; 114963b88968SPeter Xu entry->perm = IOMMU_NONE; 115063b88968SPeter Xu trace_vtd_page_walk_one(info->domain_id, 115163b88968SPeter Xu entry->iova, 115263b88968SPeter Xu entry->translated_addr, 115363b88968SPeter Xu entry->addr_mask, 115463b88968SPeter Xu entry->perm); 11555039caf3SEugenio Pérez ret = hook_fn(event, private); 115663b88968SPeter Xu if (ret) { 115763b88968SPeter Xu return ret; 115863b88968SPeter Xu } 115963b88968SPeter Xu /* Drop any existing mapping */ 116063b88968SPeter Xu iova_tree_remove(as->iova_tree, &target); 11615039caf3SEugenio Pérez /* Recover the correct type */ 11625039caf3SEugenio Pérez event->type = IOMMU_NOTIFIER_MAP; 116363b88968SPeter Xu entry->perm = cache_perm; 116463b88968SPeter Xu } 116563b88968SPeter Xu } 116663b88968SPeter Xu iova_tree_insert(as->iova_tree, &target); 116763b88968SPeter Xu } else { 116863b88968SPeter Xu if (!mapped) { 116963b88968SPeter Xu /* Skip since we didn't map this range at all */ 117063b88968SPeter Xu trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask); 117163b88968SPeter Xu return 0; 117263b88968SPeter Xu } 117363b88968SPeter Xu iova_tree_remove(as->iova_tree, &target); 117463b88968SPeter Xu } 117563b88968SPeter Xu 1176d118c06eSPeter Xu trace_vtd_page_walk_one(info->domain_id, entry->iova, 1177d118c06eSPeter Xu entry->translated_addr, entry->addr_mask, 1178d118c06eSPeter Xu entry->perm); 11795039caf3SEugenio Pérez return hook_fn(event, private); 118036d2d52bSPeter Xu } 118136d2d52bSPeter Xu 1182f06a696dSPeter Xu /** 1183f06a696dSPeter Xu * vtd_page_walk_level - walk over specific level for IOVA range 1184f06a696dSPeter Xu * 1185f06a696dSPeter Xu * @addr: base GPA addr to start the walk 1186f06a696dSPeter Xu * @start: IOVA range start address 1187f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 1188f06a696dSPeter Xu * @read: whether parent level has read permission 1189f06a696dSPeter Xu * @write: whether parent level has write permission 1190fe215b0cSPeter Xu * @info: constant information for the page walk 1191f06a696dSPeter Xu */ 1192f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start, 1193fe215b0cSPeter Xu uint64_t end, uint32_t level, bool read, 1194fe215b0cSPeter Xu bool write, vtd_page_walk_info *info) 1195f06a696dSPeter Xu { 1196f06a696dSPeter Xu bool read_cur, write_cur, entry_valid; 1197f06a696dSPeter Xu uint32_t offset; 1198f06a696dSPeter Xu uint64_t slpte; 1199f06a696dSPeter Xu uint64_t subpage_size, subpage_mask; 12005039caf3SEugenio Pérez IOMMUTLBEvent event; 1201f06a696dSPeter Xu uint64_t iova = start; 1202f06a696dSPeter Xu uint64_t iova_next; 1203f06a696dSPeter Xu int ret = 0; 1204f06a696dSPeter Xu 1205f06a696dSPeter Xu trace_vtd_page_walk_level(addr, level, start, end); 1206f06a696dSPeter Xu 1207f06a696dSPeter Xu subpage_size = 1ULL << vtd_slpt_level_shift(level); 1208f06a696dSPeter Xu subpage_mask = vtd_slpt_level_page_mask(level); 1209f06a696dSPeter Xu 1210f06a696dSPeter Xu while (iova < end) { 1211f06a696dSPeter Xu iova_next = (iova & subpage_mask) + subpage_size; 1212f06a696dSPeter Xu 1213f06a696dSPeter Xu offset = vtd_iova_level_offset(iova, level); 1214f06a696dSPeter Xu slpte = vtd_get_slpte(addr, offset); 1215f06a696dSPeter Xu 1216f06a696dSPeter Xu if (slpte == (uint64_t)-1) { 1217f06a696dSPeter Xu trace_vtd_page_walk_skip_read(iova, iova_next); 1218f06a696dSPeter Xu goto next; 1219f06a696dSPeter Xu } 1220f06a696dSPeter Xu 1221f06a696dSPeter Xu if (vtd_slpte_nonzero_rsvd(slpte, level)) { 1222f06a696dSPeter Xu trace_vtd_page_walk_skip_reserve(iova, iova_next); 1223f06a696dSPeter Xu goto next; 1224f06a696dSPeter Xu } 1225f06a696dSPeter Xu 1226f06a696dSPeter Xu /* Permissions are stacked with parents' */ 1227f06a696dSPeter Xu read_cur = read && (slpte & VTD_SL_R); 1228f06a696dSPeter Xu write_cur = write && (slpte & VTD_SL_W); 1229f06a696dSPeter Xu 1230f06a696dSPeter Xu /* 1231f06a696dSPeter Xu * As long as we have either read/write permission, this is a 1232f06a696dSPeter Xu * valid entry. The rule works for both page entries and page 1233f06a696dSPeter Xu * table entries. 1234f06a696dSPeter Xu */ 1235f06a696dSPeter Xu entry_valid = read_cur | write_cur; 1236f06a696dSPeter Xu 123763b88968SPeter Xu if (!vtd_is_last_slpte(slpte, level) && entry_valid) { 123863b88968SPeter Xu /* 123963b88968SPeter Xu * This is a valid PDE (or even bigger than PDE). We need 124063b88968SPeter Xu * to walk one further level. 124163b88968SPeter Xu */ 124263b88968SPeter Xu ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw), 124363b88968SPeter Xu iova, MIN(iova_next, end), level - 1, 124463b88968SPeter Xu read_cur, write_cur, info); 124563b88968SPeter Xu } else { 124663b88968SPeter Xu /* 124763b88968SPeter Xu * This means we are either: 124863b88968SPeter Xu * 124963b88968SPeter Xu * (1) the real page entry (either 4K page, or huge page) 125063b88968SPeter Xu * (2) the whole range is invalid 125163b88968SPeter Xu * 125263b88968SPeter Xu * In either case, we send an IOTLB notification down. 125363b88968SPeter Xu */ 12545039caf3SEugenio Pérez event.entry.target_as = &address_space_memory; 12555039caf3SEugenio Pérez event.entry.iova = iova & subpage_mask; 12565039caf3SEugenio Pérez event.entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur); 12575039caf3SEugenio Pérez event.entry.addr_mask = ~subpage_mask; 1258f06a696dSPeter Xu /* NOTE: this is only meaningful if entry_valid == true */ 12595039caf3SEugenio Pérez event.entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw); 12605039caf3SEugenio Pérez event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP : 12615039caf3SEugenio Pérez IOMMU_NOTIFIER_UNMAP; 12625039caf3SEugenio Pérez ret = vtd_page_walk_one(&event, info); 126363b88968SPeter Xu } 126463b88968SPeter Xu 1265f06a696dSPeter Xu if (ret < 0) { 1266f06a696dSPeter Xu return ret; 1267f06a696dSPeter Xu } 1268f06a696dSPeter Xu 1269f06a696dSPeter Xu next: 1270f06a696dSPeter Xu iova = iova_next; 1271f06a696dSPeter Xu } 1272f06a696dSPeter Xu 1273f06a696dSPeter Xu return 0; 1274f06a696dSPeter Xu } 1275f06a696dSPeter Xu 1276f06a696dSPeter Xu /** 1277f06a696dSPeter Xu * vtd_page_walk - walk specific IOVA range, and call the hook 1278f06a696dSPeter Xu * 1279fb43cf73SLiu, Yi L * @s: intel iommu state 1280f06a696dSPeter Xu * @ce: context entry to walk upon 1281f06a696dSPeter Xu * @start: IOVA address to start the walk 1282f06a696dSPeter Xu * @end: IOVA range end address (start <= addr < end) 1283fe215b0cSPeter Xu * @info: page walking information struct 1284f06a696dSPeter Xu */ 1285fb43cf73SLiu, Yi L static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce, 1286fb43cf73SLiu, Yi L uint64_t start, uint64_t end, 1287fe215b0cSPeter Xu vtd_page_walk_info *info) 1288f06a696dSPeter Xu { 1289fb43cf73SLiu, Yi L dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce); 1290fb43cf73SLiu, Yi L uint32_t level = vtd_get_iova_level(s, ce); 1291f06a696dSPeter Xu 1292fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, start, ce, info->aw)) { 1293f06a696dSPeter Xu return -VTD_FR_ADDR_BEYOND_MGAW; 1294f06a696dSPeter Xu } 1295f06a696dSPeter Xu 1296fb43cf73SLiu, Yi L if (!vtd_iova_range_check(s, end, ce, info->aw)) { 1297f06a696dSPeter Xu /* Fix end so that it reaches the maximum */ 1298fb43cf73SLiu, Yi L end = vtd_iova_limit(s, ce, info->aw); 1299f06a696dSPeter Xu } 1300f06a696dSPeter Xu 1301fe215b0cSPeter Xu return vtd_page_walk_level(addr, start, end, level, true, true, info); 1302f06a696dSPeter Xu } 1303f06a696dSPeter Xu 1304fb43cf73SLiu, Yi L static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s, 1305fb43cf73SLiu, Yi L VTDRootEntry *re) 1306fb43cf73SLiu, Yi L { 1307fb43cf73SLiu, Yi L /* Legacy Mode reserved bits check */ 1308fb43cf73SLiu, Yi L if (!s->root_scalable && 1309fb43cf73SLiu, Yi L (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)))) 1310fb43cf73SLiu, Yi L goto rsvd_err; 1311fb43cf73SLiu, Yi L 1312fb43cf73SLiu, Yi L /* Scalable Mode reserved bits check */ 1313fb43cf73SLiu, Yi L if (s->root_scalable && 1314fb43cf73SLiu, Yi L ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) || 1315fb43cf73SLiu, Yi L (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits)))) 1316fb43cf73SLiu, Yi L goto rsvd_err; 1317fb43cf73SLiu, Yi L 1318fb43cf73SLiu, Yi L return 0; 1319fb43cf73SLiu, Yi L 1320fb43cf73SLiu, Yi L rsvd_err: 1321fb43cf73SLiu, Yi L error_report_once("%s: invalid root entry: hi=0x%"PRIx64 1322fb43cf73SLiu, Yi L ", lo=0x%"PRIx64, 1323fb43cf73SLiu, Yi L __func__, re->hi, re->lo); 1324fb43cf73SLiu, Yi L return -VTD_FR_ROOT_ENTRY_RSVD; 1325fb43cf73SLiu, Yi L } 1326fb43cf73SLiu, Yi L 1327fb43cf73SLiu, Yi L static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s, 1328fb43cf73SLiu, Yi L VTDContextEntry *ce) 1329fb43cf73SLiu, Yi L { 1330fb43cf73SLiu, Yi L if (!s->root_scalable && 1331fb43cf73SLiu, Yi L (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI || 1332fb43cf73SLiu, Yi L ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) { 1333fb43cf73SLiu, Yi L error_report_once("%s: invalid context entry: hi=%"PRIx64 1334fb43cf73SLiu, Yi L ", lo=%"PRIx64" (reserved nonzero)", 1335fb43cf73SLiu, Yi L __func__, ce->hi, ce->lo); 1336fb43cf73SLiu, Yi L return -VTD_FR_CONTEXT_ENTRY_RSVD; 1337fb43cf73SLiu, Yi L } 1338fb43cf73SLiu, Yi L 1339fb43cf73SLiu, Yi L if (s->root_scalable && 1340fb43cf73SLiu, Yi L (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) || 1341fb43cf73SLiu, Yi L ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 || 1342fb43cf73SLiu, Yi L ce->val[2] || 1343fb43cf73SLiu, Yi L ce->val[3])) { 1344fb43cf73SLiu, Yi L error_report_once("%s: invalid context entry: val[3]=%"PRIx64 1345fb43cf73SLiu, Yi L ", val[2]=%"PRIx64 1346fb43cf73SLiu, Yi L ", val[1]=%"PRIx64 1347fb43cf73SLiu, Yi L ", val[0]=%"PRIx64" (reserved nonzero)", 1348fb43cf73SLiu, Yi L __func__, ce->val[3], ce->val[2], 1349fb43cf73SLiu, Yi L ce->val[1], ce->val[0]); 1350fb43cf73SLiu, Yi L return -VTD_FR_CONTEXT_ENTRY_RSVD; 1351fb43cf73SLiu, Yi L } 1352fb43cf73SLiu, Yi L 1353fb43cf73SLiu, Yi L return 0; 1354fb43cf73SLiu, Yi L } 1355fb43cf73SLiu, Yi L 1356fb43cf73SLiu, Yi L static int vtd_ce_rid2pasid_check(IntelIOMMUState *s, 1357fb43cf73SLiu, Yi L VTDContextEntry *ce) 1358fb43cf73SLiu, Yi L { 1359fb43cf73SLiu, Yi L VTDPASIDEntry pe; 1360fb43cf73SLiu, Yi L 1361fb43cf73SLiu, Yi L /* 1362fb43cf73SLiu, Yi L * Make sure in Scalable Mode, a present context entry 1363fb43cf73SLiu, Yi L * has valid rid2pasid setting, which includes valid 1364fb43cf73SLiu, Yi L * rid2pasid field and corresponding pasid entry setting 1365fb43cf73SLiu, Yi L */ 1366fb43cf73SLiu, Yi L return vtd_ce_get_rid2pasid_entry(s, ce, &pe); 1367fb43cf73SLiu, Yi L } 1368fb43cf73SLiu, Yi L 13691da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */ 13701da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, 13711da12ec4SLe Tan uint8_t devfn, VTDContextEntry *ce) 13721da12ec4SLe Tan { 13731da12ec4SLe Tan VTDRootEntry re; 13741da12ec4SLe Tan int ret_fr; 1375f80c9874SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 13761da12ec4SLe Tan 13771da12ec4SLe Tan ret_fr = vtd_get_root_entry(s, bus_num, &re); 13781da12ec4SLe Tan if (ret_fr) { 13791da12ec4SLe Tan return ret_fr; 13801da12ec4SLe Tan } 13811da12ec4SLe Tan 1382fb43cf73SLiu, Yi L if (!vtd_root_entry_present(s, &re, devfn)) { 13836c441e1dSPeter Xu /* Not error - it's okay we don't have root entry. */ 13846c441e1dSPeter Xu trace_vtd_re_not_present(bus_num); 13851da12ec4SLe Tan return -VTD_FR_ROOT_ENTRY_P; 1386f80c9874SPeter Xu } 1387f80c9874SPeter Xu 1388fb43cf73SLiu, Yi L ret_fr = vtd_root_entry_rsvd_bits_check(s, &re); 1389fb43cf73SLiu, Yi L if (ret_fr) { 1390fb43cf73SLiu, Yi L return ret_fr; 13911da12ec4SLe Tan } 13921da12ec4SLe Tan 1393fb43cf73SLiu, Yi L ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce); 13941da12ec4SLe Tan if (ret_fr) { 13951da12ec4SLe Tan return ret_fr; 13961da12ec4SLe Tan } 13971da12ec4SLe Tan 13988f7d7161SPeter Xu if (!vtd_ce_present(ce)) { 13996c441e1dSPeter Xu /* Not error - it's okay we don't have context entry. */ 14006c441e1dSPeter Xu trace_vtd_ce_not_present(bus_num, devfn); 14011da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_P; 1402f80c9874SPeter Xu } 1403f80c9874SPeter Xu 1404fb43cf73SLiu, Yi L ret_fr = vtd_context_entry_rsvd_bits_check(s, ce); 1405fb43cf73SLiu, Yi L if (ret_fr) { 1406fb43cf73SLiu, Yi L return ret_fr; 14071da12ec4SLe Tan } 1408f80c9874SPeter Xu 14091da12ec4SLe Tan /* Check if the programming of context-entry is valid */ 1410fb43cf73SLiu, Yi L if (!s->root_scalable && 1411fb43cf73SLiu, Yi L !vtd_is_level_supported(s, vtd_ce_get_level(ce))) { 1412095955b2SPeter Xu error_report_once("%s: invalid context entry: hi=%"PRIx64 1413095955b2SPeter Xu ", lo=%"PRIx64" (level %d not supported)", 1414fb43cf73SLiu, Yi L __func__, ce->hi, ce->lo, 1415fb43cf73SLiu, Yi L vtd_ce_get_level(ce)); 14161da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 1417f80c9874SPeter Xu } 1418f80c9874SPeter Xu 1419fb43cf73SLiu, Yi L if (!s->root_scalable) { 1420f80c9874SPeter Xu /* Do translation type check */ 1421f80c9874SPeter Xu if (!vtd_ce_type_check(x86_iommu, ce)) { 1422095955b2SPeter Xu /* Errors dumped in vtd_ce_type_check() */ 14231da12ec4SLe Tan return -VTD_FR_CONTEXT_ENTRY_INV; 14241da12ec4SLe Tan } 1425fb43cf73SLiu, Yi L } else { 1426fb43cf73SLiu, Yi L /* 1427fb43cf73SLiu, Yi L * Check if the programming of context-entry.rid2pasid 1428fb43cf73SLiu, Yi L * and corresponding pasid setting is valid, and thus 1429fb43cf73SLiu, Yi L * avoids to check pasid entry fetching result in future 1430fb43cf73SLiu, Yi L * helper function calling. 1431fb43cf73SLiu, Yi L */ 1432fb43cf73SLiu, Yi L ret_fr = vtd_ce_rid2pasid_check(s, ce); 1433fb43cf73SLiu, Yi L if (ret_fr) { 1434fb43cf73SLiu, Yi L return ret_fr; 1435fb43cf73SLiu, Yi L } 1436fb43cf73SLiu, Yi L } 1437f80c9874SPeter Xu 14381da12ec4SLe Tan return 0; 14391da12ec4SLe Tan } 14401da12ec4SLe Tan 14415039caf3SEugenio Pérez static int vtd_sync_shadow_page_hook(IOMMUTLBEvent *event, 144263b88968SPeter Xu void *private) 144363b88968SPeter Xu { 14445039caf3SEugenio Pérez memory_region_notify_iommu(private, 0, *event); 144563b88968SPeter Xu return 0; 144663b88968SPeter Xu } 144763b88968SPeter Xu 1448fb43cf73SLiu, Yi L static uint16_t vtd_get_domain_id(IntelIOMMUState *s, 1449fb43cf73SLiu, Yi L VTDContextEntry *ce) 1450fb43cf73SLiu, Yi L { 1451fb43cf73SLiu, Yi L VTDPASIDEntry pe; 1452fb43cf73SLiu, Yi L 1453fb43cf73SLiu, Yi L if (s->root_scalable) { 1454fb43cf73SLiu, Yi L vtd_ce_get_rid2pasid_entry(s, ce, &pe); 1455fb43cf73SLiu, Yi L return VTD_SM_PASID_ENTRY_DID(pe.val[1]); 1456fb43cf73SLiu, Yi L } 1457fb43cf73SLiu, Yi L 1458fb43cf73SLiu, Yi L return VTD_CONTEXT_ENTRY_DID(ce->hi); 1459fb43cf73SLiu, Yi L } 1460fb43cf73SLiu, Yi L 146163b88968SPeter Xu static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as, 146263b88968SPeter Xu VTDContextEntry *ce, 146363b88968SPeter Xu hwaddr addr, hwaddr size) 146463b88968SPeter Xu { 146563b88968SPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 146663b88968SPeter Xu vtd_page_walk_info info = { 146763b88968SPeter Xu .hook_fn = vtd_sync_shadow_page_hook, 146863b88968SPeter Xu .private = (void *)&vtd_as->iommu, 146963b88968SPeter Xu .notify_unmap = true, 147063b88968SPeter Xu .aw = s->aw_bits, 147163b88968SPeter Xu .as = vtd_as, 1472fb43cf73SLiu, Yi L .domain_id = vtd_get_domain_id(s, ce), 147363b88968SPeter Xu }; 147463b88968SPeter Xu 1475fb43cf73SLiu, Yi L return vtd_page_walk(s, ce, addr, addr + size, &info); 147663b88968SPeter Xu } 147763b88968SPeter Xu 147863b88968SPeter Xu static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as) 147963b88968SPeter Xu { 148095ecd3dfSPeter Xu int ret; 148195ecd3dfSPeter Xu VTDContextEntry ce; 1482c28b535dSPeter Xu IOMMUNotifier *n; 148395ecd3dfSPeter Xu 1484f7701e2cSEugenio Pérez if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) { 1485f7701e2cSEugenio Pérez return 0; 1486f7701e2cSEugenio Pérez } 1487f7701e2cSEugenio Pérez 148895ecd3dfSPeter Xu ret = vtd_dev_to_context_entry(vtd_as->iommu_state, 148995ecd3dfSPeter Xu pci_bus_num(vtd_as->bus), 149095ecd3dfSPeter Xu vtd_as->devfn, &ce); 149195ecd3dfSPeter Xu if (ret) { 1492c28b535dSPeter Xu if (ret == -VTD_FR_CONTEXT_ENTRY_P) { 1493c28b535dSPeter Xu /* 1494c28b535dSPeter Xu * It's a valid scenario to have a context entry that is 1495c28b535dSPeter Xu * not present. For example, when a device is removed 1496c28b535dSPeter Xu * from an existing domain then the context entry will be 1497c28b535dSPeter Xu * zeroed by the guest before it was put into another 1498c28b535dSPeter Xu * domain. When this happens, instead of synchronizing 1499c28b535dSPeter Xu * the shadow pages we should invalidate all existing 1500c28b535dSPeter Xu * mappings and notify the backends. 1501c28b535dSPeter Xu */ 1502c28b535dSPeter Xu IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) { 1503c28b535dSPeter Xu vtd_address_space_unmap(vtd_as, n); 1504c28b535dSPeter Xu } 1505c28b535dSPeter Xu ret = 0; 1506c28b535dSPeter Xu } 150795ecd3dfSPeter Xu return ret; 150895ecd3dfSPeter Xu } 150995ecd3dfSPeter Xu 151095ecd3dfSPeter Xu return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX); 151163b88968SPeter Xu } 151263b88968SPeter Xu 1513dbaabb25SPeter Xu /* 151437557b09SCai Huoqing * Check if specific device is configured to bypass address 1515fb43cf73SLiu, Yi L * translation for DMA requests. In Scalable Mode, bypass 1516fb43cf73SLiu, Yi L * 1st-level translation or 2nd-level translation, it depends 1517fb43cf73SLiu, Yi L * on PGTT setting. 1518dbaabb25SPeter Xu */ 15195178d78fSJason Wang static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce) 15205178d78fSJason Wang { 15215178d78fSJason Wang VTDPASIDEntry pe; 15225178d78fSJason Wang int ret; 15235178d78fSJason Wang 15245178d78fSJason Wang if (s->root_scalable) { 15255178d78fSJason Wang ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe); 15265178d78fSJason Wang if (ret) { 15275178d78fSJason Wang error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRId32, 15285178d78fSJason Wang __func__, ret); 15295178d78fSJason Wang return false; 15305178d78fSJason Wang } 15315178d78fSJason Wang return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT); 15325178d78fSJason Wang } 15335178d78fSJason Wang 15345178d78fSJason Wang return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH); 15355178d78fSJason Wang 15365178d78fSJason Wang } 15375178d78fSJason Wang 15385178d78fSJason Wang static bool vtd_as_pt_enabled(VTDAddressSpace *as) 1539dbaabb25SPeter Xu { 1540dbaabb25SPeter Xu IntelIOMMUState *s; 1541dbaabb25SPeter Xu VTDContextEntry ce; 1542dbaabb25SPeter Xu int ret; 1543dbaabb25SPeter Xu 1544dbaabb25SPeter Xu assert(as); 1545dbaabb25SPeter Xu 1546fb43cf73SLiu, Yi L s = as->iommu_state; 1547fb43cf73SLiu, Yi L ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus), 1548fb43cf73SLiu, Yi L as->devfn, &ce); 1549fb43cf73SLiu, Yi L if (ret) { 1550dbaabb25SPeter Xu /* 1551dbaabb25SPeter Xu * Possibly failed to parse the context entry for some reason 1552dbaabb25SPeter Xu * (e.g., during init, or any guest configuration errors on 1553dbaabb25SPeter Xu * context entries). We should assume PT not enabled for 1554dbaabb25SPeter Xu * safety. 1555dbaabb25SPeter Xu */ 1556dbaabb25SPeter Xu return false; 1557dbaabb25SPeter Xu } 1558dbaabb25SPeter Xu 15595178d78fSJason Wang return vtd_dev_pt_enabled(s, &ce); 1560dbaabb25SPeter Xu } 1561dbaabb25SPeter Xu 1562dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */ 1563dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as) 1564dbaabb25SPeter Xu { 1565dbaabb25SPeter Xu bool use_iommu; 156666a4a031SPeter Xu /* Whether we need to take the BQL on our own */ 156766a4a031SPeter Xu bool take_bql = !qemu_mutex_iothread_locked(); 1568dbaabb25SPeter Xu 1569dbaabb25SPeter Xu assert(as); 1570dbaabb25SPeter Xu 15715178d78fSJason Wang use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as); 1572dbaabb25SPeter Xu 1573dbaabb25SPeter Xu trace_vtd_switch_address_space(pci_bus_num(as->bus), 1574dbaabb25SPeter Xu VTD_PCI_SLOT(as->devfn), 1575dbaabb25SPeter Xu VTD_PCI_FUNC(as->devfn), 1576dbaabb25SPeter Xu use_iommu); 1577dbaabb25SPeter Xu 157866a4a031SPeter Xu /* 157966a4a031SPeter Xu * It's possible that we reach here without BQL, e.g., when called 158066a4a031SPeter Xu * from vtd_pt_enable_fast_path(). However the memory APIs need 158166a4a031SPeter Xu * it. We'd better make sure we have had it already, or, take it. 158266a4a031SPeter Xu */ 158366a4a031SPeter Xu if (take_bql) { 158466a4a031SPeter Xu qemu_mutex_lock_iothread(); 158566a4a031SPeter Xu } 158666a4a031SPeter Xu 1587dbaabb25SPeter Xu /* Turn off first then on the other */ 1588dbaabb25SPeter Xu if (use_iommu) { 15894b519ef1SPeter Xu memory_region_set_enabled(&as->nodmar, false); 15903df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), true); 1591dbaabb25SPeter Xu } else { 15923df9d748SAlexey Kardashevskiy memory_region_set_enabled(MEMORY_REGION(&as->iommu), false); 15934b519ef1SPeter Xu memory_region_set_enabled(&as->nodmar, true); 1594dbaabb25SPeter Xu } 1595dbaabb25SPeter Xu 159666a4a031SPeter Xu if (take_bql) { 159766a4a031SPeter Xu qemu_mutex_unlock_iothread(); 159866a4a031SPeter Xu } 159966a4a031SPeter Xu 1600dbaabb25SPeter Xu return use_iommu; 1601dbaabb25SPeter Xu } 1602dbaabb25SPeter Xu 1603dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s) 1604dbaabb25SPeter Xu { 1605dbaabb25SPeter Xu GHashTableIter iter; 1606dbaabb25SPeter Xu VTDBus *vtd_bus; 1607dbaabb25SPeter Xu int i; 1608dbaabb25SPeter Xu 1609dbaabb25SPeter Xu g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); 1610dbaabb25SPeter Xu while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { 1611bf33cc75SPeter Xu for (i = 0; i < PCI_DEVFN_MAX; i++) { 1612dbaabb25SPeter Xu if (!vtd_bus->dev_as[i]) { 1613dbaabb25SPeter Xu continue; 1614dbaabb25SPeter Xu } 1615dbaabb25SPeter Xu vtd_switch_address_space(vtd_bus->dev_as[i]); 1616dbaabb25SPeter Xu } 1617dbaabb25SPeter Xu } 1618dbaabb25SPeter Xu } 1619dbaabb25SPeter Xu 16201da12ec4SLe Tan static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn) 16211da12ec4SLe Tan { 16221da12ec4SLe Tan return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL); 16231da12ec4SLe Tan } 16241da12ec4SLe Tan 16251da12ec4SLe Tan static const bool vtd_qualified_faults[] = { 16261da12ec4SLe Tan [VTD_FR_RESERVED] = false, 16271da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_P] = false, 16281da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_P] = true, 16291da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_INV] = true, 16301da12ec4SLe Tan [VTD_FR_ADDR_BEYOND_MGAW] = true, 16311da12ec4SLe Tan [VTD_FR_WRITE] = true, 16321da12ec4SLe Tan [VTD_FR_READ] = true, 16331da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_INV] = true, 16341da12ec4SLe Tan [VTD_FR_ROOT_TABLE_INV] = false, 16351da12ec4SLe Tan [VTD_FR_CONTEXT_TABLE_INV] = false, 16361da12ec4SLe Tan [VTD_FR_ROOT_ENTRY_RSVD] = false, 16371da12ec4SLe Tan [VTD_FR_PAGING_ENTRY_RSVD] = true, 16381da12ec4SLe Tan [VTD_FR_CONTEXT_ENTRY_TT] = true, 1639fb43cf73SLiu, Yi L [VTD_FR_PASID_TABLE_INV] = false, 16401da12ec4SLe Tan [VTD_FR_RESERVED_ERR] = false, 16411da12ec4SLe Tan [VTD_FR_MAX] = false, 16421da12ec4SLe Tan }; 16431da12ec4SLe Tan 16441da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software 16451da12ec4SLe Tan * only if the FPD field in the context-entry used to process the faulting 16461da12ec4SLe Tan * request is 0. 16471da12ec4SLe Tan */ 16481da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault) 16491da12ec4SLe Tan { 16501da12ec4SLe Tan return vtd_qualified_faults[fault]; 16511da12ec4SLe Tan } 16521da12ec4SLe Tan 16531da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr) 16541da12ec4SLe Tan { 16551da12ec4SLe Tan return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST; 16561da12ec4SLe Tan } 16571da12ec4SLe Tan 1658dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id) 1659dbaabb25SPeter Xu { 1660dbaabb25SPeter Xu VTDBus *vtd_bus; 1661dbaabb25SPeter Xu VTDAddressSpace *vtd_as; 1662dbaabb25SPeter Xu bool success = false; 1663dbaabb25SPeter Xu 1664dbaabb25SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id)); 1665dbaabb25SPeter Xu if (!vtd_bus) { 1666dbaabb25SPeter Xu goto out; 1667dbaabb25SPeter Xu } 1668dbaabb25SPeter Xu 1669dbaabb25SPeter Xu vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)]; 1670dbaabb25SPeter Xu if (!vtd_as) { 1671dbaabb25SPeter Xu goto out; 1672dbaabb25SPeter Xu } 1673dbaabb25SPeter Xu 1674dbaabb25SPeter Xu if (vtd_switch_address_space(vtd_as) == false) { 1675dbaabb25SPeter Xu /* We switched off IOMMU region successfully. */ 1676dbaabb25SPeter Xu success = true; 1677dbaabb25SPeter Xu } 1678dbaabb25SPeter Xu 1679dbaabb25SPeter Xu out: 1680dbaabb25SPeter Xu trace_vtd_pt_enable_fast_path(source_id, success); 1681dbaabb25SPeter Xu } 1682dbaabb25SPeter Xu 16831da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu 16841da12ec4SLe Tan * translation. 168579e2b9aeSPaolo Bonzini * 168679e2b9aeSPaolo Bonzini * Called from RCU critical section. 168779e2b9aeSPaolo Bonzini * 16881da12ec4SLe Tan * @bus_num: The bus number 16891da12ec4SLe Tan * @devfn: The devfn, which is the combined of device and function number 16901da12ec4SLe Tan * @is_write: The access is a write operation 16911da12ec4SLe Tan * @entry: IOMMUTLBEntry that contain the addr to be translated and result 1692b9313021SPeter Xu * 1693b9313021SPeter Xu * Returns true if translation is successful, otherwise false. 16941da12ec4SLe Tan */ 1695b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, 16961da12ec4SLe Tan uint8_t devfn, hwaddr addr, bool is_write, 16971da12ec4SLe Tan IOMMUTLBEntry *entry) 16981da12ec4SLe Tan { 1699d92fa2dcSLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 17001da12ec4SLe Tan VTDContextEntry ce; 17017df953bdSKnut Omang uint8_t bus_num = pci_bus_num(bus); 17021d9efa73SPeter Xu VTDContextCacheEntry *cc_entry; 1703d66b969bSJason Wang uint64_t slpte, page_mask; 17041da12ec4SLe Tan uint32_t level; 17051da12ec4SLe Tan uint16_t source_id = vtd_make_source_id(bus_num, devfn); 17061da12ec4SLe Tan int ret_fr; 17071da12ec4SLe Tan bool is_fpd_set = false; 17081da12ec4SLe Tan bool reads = true; 17091da12ec4SLe Tan bool writes = true; 171007f7b733SPeter Xu uint8_t access_flags; 1711b5a280c0SLe Tan VTDIOTLBEntry *iotlb_entry; 17121da12ec4SLe Tan 1713046ab7e9SPeter Xu /* 1714046ab7e9SPeter Xu * We have standalone memory region for interrupt addresses, we 1715046ab7e9SPeter Xu * should never receive translation requests in this region. 17161da12ec4SLe Tan */ 1717046ab7e9SPeter Xu assert(!vtd_is_interrupt_addr(addr)); 1718046ab7e9SPeter Xu 17191d9efa73SPeter Xu vtd_iommu_lock(s); 17201d9efa73SPeter Xu 17211d9efa73SPeter Xu cc_entry = &vtd_as->context_cache_entry; 17221d9efa73SPeter Xu 1723b5a280c0SLe Tan /* Try to fetch slpte form IOTLB */ 1724b5a280c0SLe Tan iotlb_entry = vtd_lookup_iotlb(s, source_id, addr); 1725b5a280c0SLe Tan if (iotlb_entry) { 17266c441e1dSPeter Xu trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, 17276c441e1dSPeter Xu iotlb_entry->domain_id); 1728b5a280c0SLe Tan slpte = iotlb_entry->slpte; 172907f7b733SPeter Xu access_flags = iotlb_entry->access_flags; 1730d66b969bSJason Wang page_mask = iotlb_entry->mask; 1731b5a280c0SLe Tan goto out; 1732b5a280c0SLe Tan } 1733b9313021SPeter Xu 1734d92fa2dcSLe Tan /* Try to fetch context-entry from cache first */ 1735d92fa2dcSLe Tan if (cc_entry->context_cache_gen == s->context_cache_gen) { 17366c441e1dSPeter Xu trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi, 17376c441e1dSPeter Xu cc_entry->context_entry.lo, 17386c441e1dSPeter Xu cc_entry->context_cache_gen); 1739d92fa2dcSLe Tan ce = cc_entry->context_entry; 1740d92fa2dcSLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 1741fb43cf73SLiu, Yi L if (!is_fpd_set && s->root_scalable) { 1742fb43cf73SLiu, Yi L ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); 1743fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 1744fb43cf73SLiu, Yi L } 1745d92fa2dcSLe Tan } else { 17461da12ec4SLe Tan ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce); 17471da12ec4SLe Tan is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD; 1748fb43cf73SLiu, Yi L if (!ret_fr && !is_fpd_set && s->root_scalable) { 1749fb43cf73SLiu, Yi L ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); 17501da12ec4SLe Tan } 1751fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 1752d92fa2dcSLe Tan /* Update context-cache */ 17536c441e1dSPeter Xu trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo, 17546c441e1dSPeter Xu cc_entry->context_cache_gen, 17556c441e1dSPeter Xu s->context_cache_gen); 1756d92fa2dcSLe Tan cc_entry->context_entry = ce; 1757d92fa2dcSLe Tan cc_entry->context_cache_gen = s->context_cache_gen; 1758d92fa2dcSLe Tan } 17591da12ec4SLe Tan 1760dbaabb25SPeter Xu /* 1761dbaabb25SPeter Xu * We don't need to translate for pass-through context entries. 1762dbaabb25SPeter Xu * Also, let's ignore IOTLB caching as well for PT devices. 1763dbaabb25SPeter Xu */ 17645178d78fSJason Wang if (vtd_dev_pt_enabled(s, &ce)) { 1765892721d9SPeter Xu entry->iova = addr & VTD_PAGE_MASK_4K; 1766dbaabb25SPeter Xu entry->translated_addr = entry->iova; 1767892721d9SPeter Xu entry->addr_mask = ~VTD_PAGE_MASK_4K; 1768dbaabb25SPeter Xu entry->perm = IOMMU_RW; 1769dbaabb25SPeter Xu trace_vtd_translate_pt(source_id, entry->iova); 1770dbaabb25SPeter Xu 1771dbaabb25SPeter Xu /* 1772dbaabb25SPeter Xu * When this happens, it means firstly caching-mode is not 1773dbaabb25SPeter Xu * enabled, and this is the first passthrough translation for 1774dbaabb25SPeter Xu * the device. Let's enable the fast path for passthrough. 1775dbaabb25SPeter Xu * 1776dbaabb25SPeter Xu * When passthrough is disabled again for the device, we can 1777dbaabb25SPeter Xu * capture it via the context entry invalidation, then the 1778dbaabb25SPeter Xu * IOMMU region can be swapped back. 1779dbaabb25SPeter Xu */ 1780dbaabb25SPeter Xu vtd_pt_enable_fast_path(s, source_id); 17811d9efa73SPeter Xu vtd_iommu_unlock(s); 1782b9313021SPeter Xu return true; 1783dbaabb25SPeter Xu } 1784dbaabb25SPeter Xu 1785fb43cf73SLiu, Yi L ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level, 178637f51384SPrasad Singamsetty &reads, &writes, s->aw_bits); 1787fb43cf73SLiu, Yi L VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); 17881da12ec4SLe Tan 1789d66b969bSJason Wang page_mask = vtd_slpt_level_page_mask(level); 179007f7b733SPeter Xu access_flags = IOMMU_ACCESS_FLAG(reads, writes); 1791fb43cf73SLiu, Yi L vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce), addr, slpte, 179207f7b733SPeter Xu access_flags, level); 1793b5a280c0SLe Tan out: 17941d9efa73SPeter Xu vtd_iommu_unlock(s); 1795d66b969bSJason Wang entry->iova = addr & page_mask; 179637f51384SPrasad Singamsetty entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask; 1797d66b969bSJason Wang entry->addr_mask = ~page_mask; 179807f7b733SPeter Xu entry->perm = access_flags; 1799b9313021SPeter Xu return true; 1800b9313021SPeter Xu 1801b9313021SPeter Xu error: 18021d9efa73SPeter Xu vtd_iommu_unlock(s); 1803b9313021SPeter Xu entry->iova = 0; 1804b9313021SPeter Xu entry->translated_addr = 0; 1805b9313021SPeter Xu entry->addr_mask = 0; 1806b9313021SPeter Xu entry->perm = IOMMU_NONE; 1807b9313021SPeter Xu return false; 18081da12ec4SLe Tan } 18091da12ec4SLe Tan 18101da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s) 18111da12ec4SLe Tan { 18121da12ec4SLe Tan s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 181337f51384SPrasad Singamsetty s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits); 18141da12ec4SLe Tan 18152811af3bSPeter Xu vtd_update_scalable_state(s); 18162811af3bSPeter Xu 181781fb1e64SPeter Xu trace_vtd_reg_dmar_root(s->root, s->root_scalable); 18181da12ec4SLe Tan } 18191da12ec4SLe Tan 182002a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global, 182102a2cbc8SPeter Xu uint32_t index, uint32_t mask) 182202a2cbc8SPeter Xu { 182302a2cbc8SPeter Xu x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask); 182402a2cbc8SPeter Xu } 182502a2cbc8SPeter Xu 1826a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s) 1827a5861439SPeter Xu { 1828a5861439SPeter Xu uint64_t value = 0; 1829a5861439SPeter Xu value = vtd_get_quad_raw(s, DMAR_IRTA_REG); 1830a5861439SPeter Xu s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1); 183137f51384SPrasad Singamsetty s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits); 183228589311SJan Kiszka s->intr_eime = value & VTD_IRTA_EIME; 1833a5861439SPeter Xu 183402a2cbc8SPeter Xu /* Notify global invalidation */ 183502a2cbc8SPeter Xu vtd_iec_notify_all(s, true, 0, 0); 1836a5861439SPeter Xu 18377feb51b7SPeter Xu trace_vtd_reg_ir_root(s->intr_root, s->intr_size); 1838a5861439SPeter Xu } 1839a5861439SPeter Xu 1840dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s) 1841dd4d607eSPeter Xu { 1842b4a4ba0dSPeter Xu VTDAddressSpace *vtd_as; 1843dd4d607eSPeter Xu 1844b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 184563b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1846dd4d607eSPeter Xu } 1847dd4d607eSPeter Xu } 1848dd4d607eSPeter Xu 1849d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s) 1850d92fa2dcSLe Tan { 1851bc535e59SPeter Xu trace_vtd_inv_desc_cc_global(); 18521d9efa73SPeter Xu /* Protects context cache */ 18531d9efa73SPeter Xu vtd_iommu_lock(s); 1854d92fa2dcSLe Tan s->context_cache_gen++; 1855d92fa2dcSLe Tan if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) { 18561d9efa73SPeter Xu vtd_reset_context_cache_locked(s); 1857d92fa2dcSLe Tan } 18581d9efa73SPeter Xu vtd_iommu_unlock(s); 18592cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 1860dd4d607eSPeter Xu /* 1861dd4d607eSPeter Xu * From VT-d spec 6.5.2.1, a global context entry invalidation 1862dd4d607eSPeter Xu * should be followed by a IOTLB global invalidation, so we should 1863dd4d607eSPeter Xu * be safe even without this. Hoewever, let's replay the region as 1864dd4d607eSPeter Xu * well to be safer, and go back here when we need finer tunes for 1865dd4d607eSPeter Xu * VT-d emulation codes. 1866dd4d607eSPeter Xu */ 1867dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1868d92fa2dcSLe Tan } 1869d92fa2dcSLe Tan 1870d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation. 1871d92fa2dcSLe Tan * @func_mask: FM field after shifting 1872d92fa2dcSLe Tan */ 1873d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s, 1874d92fa2dcSLe Tan uint16_t source_id, 1875d92fa2dcSLe Tan uint16_t func_mask) 1876d92fa2dcSLe Tan { 1877d92fa2dcSLe Tan uint16_t mask; 18787df953bdSKnut Omang VTDBus *vtd_bus; 1879d92fa2dcSLe Tan VTDAddressSpace *vtd_as; 1880bc535e59SPeter Xu uint8_t bus_n, devfn; 1881d92fa2dcSLe Tan uint16_t devfn_it; 1882d92fa2dcSLe Tan 1883bc535e59SPeter Xu trace_vtd_inv_desc_cc_devices(source_id, func_mask); 1884bc535e59SPeter Xu 1885d92fa2dcSLe Tan switch (func_mask & 3) { 1886d92fa2dcSLe Tan case 0: 1887d92fa2dcSLe Tan mask = 0; /* No bits in the SID field masked */ 1888d92fa2dcSLe Tan break; 1889d92fa2dcSLe Tan case 1: 1890d92fa2dcSLe Tan mask = 4; /* Mask bit 2 in the SID field */ 1891d92fa2dcSLe Tan break; 1892d92fa2dcSLe Tan case 2: 1893d92fa2dcSLe Tan mask = 6; /* Mask bit 2:1 in the SID field */ 1894d92fa2dcSLe Tan break; 1895d92fa2dcSLe Tan case 3: 1896d92fa2dcSLe Tan mask = 7; /* Mask bit 2:0 in the SID field */ 1897d92fa2dcSLe Tan break; 189841ce9a91SEric Auger default: 189941ce9a91SEric Auger g_assert_not_reached(); 1900d92fa2dcSLe Tan } 19016cb99accSPeter Xu mask = ~mask; 1902bc535e59SPeter Xu 1903bc535e59SPeter Xu bus_n = VTD_SID_TO_BUS(source_id); 1904bc535e59SPeter Xu vtd_bus = vtd_find_as_from_bus_num(s, bus_n); 19057df953bdSKnut Omang if (vtd_bus) { 1906d92fa2dcSLe Tan devfn = VTD_SID_TO_DEVFN(source_id); 1907bf33cc75SPeter Xu for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { 19087df953bdSKnut Omang vtd_as = vtd_bus->dev_as[devfn_it]; 1909d92fa2dcSLe Tan if (vtd_as && ((devfn_it & mask) == (devfn & mask))) { 1910bc535e59SPeter Xu trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it), 1911bc535e59SPeter Xu VTD_PCI_FUNC(devfn_it)); 19121d9efa73SPeter Xu vtd_iommu_lock(s); 1913d92fa2dcSLe Tan vtd_as->context_cache_entry.context_cache_gen = 0; 19141d9efa73SPeter Xu vtd_iommu_unlock(s); 1915dd4d607eSPeter Xu /* 1916dbaabb25SPeter Xu * Do switch address space when needed, in case if the 1917dbaabb25SPeter Xu * device passthrough bit is switched. 1918dbaabb25SPeter Xu */ 1919dbaabb25SPeter Xu vtd_switch_address_space(vtd_as); 1920dbaabb25SPeter Xu /* 1921dd4d607eSPeter Xu * So a device is moving out of (or moving into) a 192263b88968SPeter Xu * domain, resync the shadow page table. 1923dd4d607eSPeter Xu * This won't bring bad even if we have no such 1924dd4d607eSPeter Xu * notifier registered - the IOMMU notification 1925dd4d607eSPeter Xu * framework will skip MAP notifications if that 1926dd4d607eSPeter Xu * happened. 1927dd4d607eSPeter Xu */ 192863b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1929d92fa2dcSLe Tan } 1930d92fa2dcSLe Tan } 1931d92fa2dcSLe Tan } 1932d92fa2dcSLe Tan } 1933d92fa2dcSLe Tan 19341da12ec4SLe Tan /* Context-cache invalidation 19351da12ec4SLe Tan * Returns the Context Actual Invalidation Granularity. 19361da12ec4SLe Tan * @val: the content of the CCMD_REG 19371da12ec4SLe Tan */ 19381da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val) 19391da12ec4SLe Tan { 19401da12ec4SLe Tan uint64_t caig; 19411da12ec4SLe Tan uint64_t type = val & VTD_CCMD_CIRG_MASK; 19421da12ec4SLe Tan 19431da12ec4SLe Tan switch (type) { 19441da12ec4SLe Tan case VTD_CCMD_DOMAIN_INVL: 1945d92fa2dcSLe Tan /* Fall through */ 1946d92fa2dcSLe Tan case VTD_CCMD_GLOBAL_INVL: 1947d92fa2dcSLe Tan caig = VTD_CCMD_GLOBAL_INVL_A; 1948d92fa2dcSLe Tan vtd_context_global_invalidate(s); 19491da12ec4SLe Tan break; 19501da12ec4SLe Tan 19511da12ec4SLe Tan case VTD_CCMD_DEVICE_INVL: 19521da12ec4SLe Tan caig = VTD_CCMD_DEVICE_INVL_A; 1953d92fa2dcSLe Tan vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val)); 19541da12ec4SLe Tan break; 19551da12ec4SLe Tan 19561da12ec4SLe Tan default: 19571376211fSPeter Xu error_report_once("%s: invalid context: 0x%" PRIx64, 19581376211fSPeter Xu __func__, val); 19591da12ec4SLe Tan caig = 0; 19601da12ec4SLe Tan } 19611da12ec4SLe Tan return caig; 19621da12ec4SLe Tan } 19631da12ec4SLe Tan 1964b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s) 1965b5a280c0SLe Tan { 19667feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_global(); 1967b5a280c0SLe Tan vtd_reset_iotlb(s); 1968dd4d607eSPeter Xu vtd_iommu_replay_all(s); 1969b5a280c0SLe Tan } 1970b5a280c0SLe Tan 1971b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) 1972b5a280c0SLe Tan { 1973dd4d607eSPeter Xu VTDContextEntry ce; 1974dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 1975dd4d607eSPeter Xu 19767feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_domain(domain_id); 19777feb51b7SPeter Xu 19781d9efa73SPeter Xu vtd_iommu_lock(s); 1979b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain, 1980b5a280c0SLe Tan &domain_id); 19811d9efa73SPeter Xu vtd_iommu_unlock(s); 1982dd4d607eSPeter Xu 1983b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 1984dd4d607eSPeter Xu if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 1985dd4d607eSPeter Xu vtd_as->devfn, &ce) && 1986fb43cf73SLiu, Yi L domain_id == vtd_get_domain_id(s, &ce)) { 198763b88968SPeter Xu vtd_sync_shadow_page_table(vtd_as); 1988dd4d607eSPeter Xu } 1989dd4d607eSPeter Xu } 1990dd4d607eSPeter Xu } 1991dd4d607eSPeter Xu 1992dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, 1993dd4d607eSPeter Xu uint16_t domain_id, hwaddr addr, 1994dd4d607eSPeter Xu uint8_t am) 1995dd4d607eSPeter Xu { 1996b4a4ba0dSPeter Xu VTDAddressSpace *vtd_as; 1997dd4d607eSPeter Xu VTDContextEntry ce; 1998dd4d607eSPeter Xu int ret; 19994f8a62a9SPeter Xu hwaddr size = (1 << am) * VTD_PAGE_SIZE; 2000dd4d607eSPeter Xu 2001b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { 2002dd4d607eSPeter Xu ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), 2003dd4d607eSPeter Xu vtd_as->devfn, &ce); 2004fb43cf73SLiu, Yi L if (!ret && domain_id == vtd_get_domain_id(s, &ce)) { 20054f8a62a9SPeter Xu if (vtd_as_has_map_notifier(vtd_as)) { 20064f8a62a9SPeter Xu /* 20074f8a62a9SPeter Xu * As long as we have MAP notifications registered in 20084f8a62a9SPeter Xu * any of our IOMMU notifiers, we need to sync the 20094f8a62a9SPeter Xu * shadow page table. 20104f8a62a9SPeter Xu */ 201163b88968SPeter Xu vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size); 20124f8a62a9SPeter Xu } else { 20134f8a62a9SPeter Xu /* 20144f8a62a9SPeter Xu * For UNMAP-only notifiers, we don't need to walk the 20154f8a62a9SPeter Xu * page tables. We just deliver the PSI down to 20164f8a62a9SPeter Xu * invalidate caches. 20174f8a62a9SPeter Xu */ 20185039caf3SEugenio Pérez IOMMUTLBEvent event = { 20195039caf3SEugenio Pérez .type = IOMMU_NOTIFIER_UNMAP, 20205039caf3SEugenio Pérez .entry = { 20214f8a62a9SPeter Xu .target_as = &address_space_memory, 20224f8a62a9SPeter Xu .iova = addr, 20234f8a62a9SPeter Xu .translated_addr = 0, 20244f8a62a9SPeter Xu .addr_mask = size - 1, 20254f8a62a9SPeter Xu .perm = IOMMU_NONE, 20265039caf3SEugenio Pérez }, 20274f8a62a9SPeter Xu }; 20285039caf3SEugenio Pérez memory_region_notify_iommu(&vtd_as->iommu, 0, event); 20294f8a62a9SPeter Xu } 2030dd4d607eSPeter Xu } 2031dd4d607eSPeter Xu } 2032b5a280c0SLe Tan } 2033b5a280c0SLe Tan 2034b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, 2035b5a280c0SLe Tan hwaddr addr, uint8_t am) 2036b5a280c0SLe Tan { 2037b5a280c0SLe Tan VTDIOTLBPageInvInfo info; 2038b5a280c0SLe Tan 20397feb51b7SPeter Xu trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am); 20407feb51b7SPeter Xu 2041b5a280c0SLe Tan assert(am <= VTD_MAMV); 2042b5a280c0SLe Tan info.domain_id = domain_id; 2043d66b969bSJason Wang info.addr = addr; 2044b5a280c0SLe Tan info.mask = ~((1 << am) - 1); 20451d9efa73SPeter Xu vtd_iommu_lock(s); 2046b5a280c0SLe Tan g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); 20471d9efa73SPeter Xu vtd_iommu_unlock(s); 2048dd4d607eSPeter Xu vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); 2049b5a280c0SLe Tan } 2050b5a280c0SLe Tan 20511da12ec4SLe Tan /* Flush IOTLB 20521da12ec4SLe Tan * Returns the IOTLB Actual Invalidation Granularity. 20531da12ec4SLe Tan * @val: the content of the IOTLB_REG 20541da12ec4SLe Tan */ 20551da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val) 20561da12ec4SLe Tan { 20571da12ec4SLe Tan uint64_t iaig; 20581da12ec4SLe Tan uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK; 2059b5a280c0SLe Tan uint16_t domain_id; 2060b5a280c0SLe Tan hwaddr addr; 2061b5a280c0SLe Tan uint8_t am; 20621da12ec4SLe Tan 20631da12ec4SLe Tan switch (type) { 20641da12ec4SLe Tan case VTD_TLB_GLOBAL_FLUSH: 20651da12ec4SLe Tan iaig = VTD_TLB_GLOBAL_FLUSH_A; 2066b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 20671da12ec4SLe Tan break; 20681da12ec4SLe Tan 20691da12ec4SLe Tan case VTD_TLB_DSI_FLUSH: 2070b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 20711da12ec4SLe Tan iaig = VTD_TLB_DSI_FLUSH_A; 2072b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 20731da12ec4SLe Tan break; 20741da12ec4SLe Tan 20751da12ec4SLe Tan case VTD_TLB_PSI_FLUSH: 2076b5a280c0SLe Tan domain_id = VTD_TLB_DID(val); 2077b5a280c0SLe Tan addr = vtd_get_quad_raw(s, DMAR_IVA_REG); 2078b5a280c0SLe Tan am = VTD_IVA_AM(addr); 2079b5a280c0SLe Tan addr = VTD_IVA_ADDR(addr); 2080b5a280c0SLe Tan if (am > VTD_MAMV) { 20811376211fSPeter Xu error_report_once("%s: address mask overflow: 0x%" PRIx64, 20821376211fSPeter Xu __func__, vtd_get_quad_raw(s, DMAR_IVA_REG)); 2083b5a280c0SLe Tan iaig = 0; 2084b5a280c0SLe Tan break; 2085b5a280c0SLe Tan } 20861da12ec4SLe Tan iaig = VTD_TLB_PSI_FLUSH_A; 2087b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 20881da12ec4SLe Tan break; 20891da12ec4SLe Tan 20901da12ec4SLe Tan default: 20911376211fSPeter Xu error_report_once("%s: invalid granularity: 0x%" PRIx64, 20921376211fSPeter Xu __func__, val); 20931da12ec4SLe Tan iaig = 0; 20941da12ec4SLe Tan } 20951da12ec4SLe Tan return iaig; 20961da12ec4SLe Tan } 20971da12ec4SLe Tan 20988991c460SLadi Prosek static void vtd_fetch_inv_desc(IntelIOMMUState *s); 2099ed7b8fbcSLe Tan 2100ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s) 2101ed7b8fbcSLe Tan { 2102ed7b8fbcSLe Tan return s->qi_enabled && (s->iq_tail == s->iq_head) && 2103ed7b8fbcSLe Tan (s->iq_last_desc_type == VTD_INV_DESC_WAIT); 2104ed7b8fbcSLe Tan } 2105ed7b8fbcSLe Tan 2106ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en) 2107ed7b8fbcSLe Tan { 2108ed7b8fbcSLe Tan uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG); 2109ed7b8fbcSLe Tan 21107feb51b7SPeter Xu trace_vtd_inv_qi_enable(en); 21117feb51b7SPeter Xu 2112ed7b8fbcSLe Tan if (en) { 211337f51384SPrasad Singamsetty s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits); 2114ed7b8fbcSLe Tan /* 2^(x+8) entries */ 2115c0c1d351SLiu, Yi L s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8 - (s->iq_dw ? 1 : 0)); 2116ed7b8fbcSLe Tan s->qi_enabled = true; 21177feb51b7SPeter Xu trace_vtd_inv_qi_setup(s->iq, s->iq_size); 2118ed7b8fbcSLe Tan /* Ok - report back to driver */ 2119ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES); 21208991c460SLadi Prosek 21218991c460SLadi Prosek if (s->iq_tail != 0) { 21228991c460SLadi Prosek /* 21238991c460SLadi Prosek * This is a spec violation but Windows guests are known to set up 21248991c460SLadi Prosek * Queued Invalidation this way so we allow the write and process 21258991c460SLadi Prosek * Invalidation Descriptors right away. 21268991c460SLadi Prosek */ 21278991c460SLadi Prosek trace_vtd_warn_invalid_qi_tail(s->iq_tail); 21288991c460SLadi Prosek if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 21298991c460SLadi Prosek vtd_fetch_inv_desc(s); 21308991c460SLadi Prosek } 2131ed7b8fbcSLe Tan } 2132ed7b8fbcSLe Tan } else { 2133ed7b8fbcSLe Tan if (vtd_queued_inv_disable_check(s)) { 2134ed7b8fbcSLe Tan /* disable Queued Invalidation */ 2135ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 0); 2136ed7b8fbcSLe Tan s->iq_head = 0; 2137ed7b8fbcSLe Tan s->qi_enabled = false; 2138ed7b8fbcSLe Tan /* Ok - report back to driver */ 2139ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0); 2140ed7b8fbcSLe Tan } else { 21414e4abd11SPeter Xu error_report_once("%s: detected improper state when disable QI " 21424e4abd11SPeter Xu "(head=0x%x, tail=0x%x, last_type=%d)", 21434e4abd11SPeter Xu __func__, 21444e4abd11SPeter Xu s->iq_head, s->iq_tail, s->iq_last_desc_type); 2145ed7b8fbcSLe Tan } 2146ed7b8fbcSLe Tan } 2147ed7b8fbcSLe Tan } 2148ed7b8fbcSLe Tan 21491da12ec4SLe Tan /* Set Root Table Pointer */ 21501da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s) 21511da12ec4SLe Tan { 21521da12ec4SLe Tan vtd_root_table_setup(s); 21531da12ec4SLe Tan /* Ok - report back to driver */ 21541da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS); 21552cc9ddccSPeter Xu vtd_reset_caches(s); 21562cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 21571da12ec4SLe Tan } 21581da12ec4SLe Tan 2159a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */ 2160a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s) 2161a5861439SPeter Xu { 2162a5861439SPeter Xu vtd_interrupt_remap_table_setup(s); 2163a5861439SPeter Xu /* Ok - report back to driver */ 2164a5861439SPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS); 2165a5861439SPeter Xu } 2166a5861439SPeter Xu 21671da12ec4SLe Tan /* Handle Translation Enable/Disable */ 21681da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en) 21691da12ec4SLe Tan { 2170558e0024SPeter Xu if (s->dmar_enabled == en) { 2171558e0024SPeter Xu return; 2172558e0024SPeter Xu } 2173558e0024SPeter Xu 21747feb51b7SPeter Xu trace_vtd_dmar_enable(en); 21751da12ec4SLe Tan 21761da12ec4SLe Tan if (en) { 21771da12ec4SLe Tan s->dmar_enabled = true; 21781da12ec4SLe Tan /* Ok - report back to driver */ 21791da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES); 21801da12ec4SLe Tan } else { 21811da12ec4SLe Tan s->dmar_enabled = false; 21821da12ec4SLe Tan 21831da12ec4SLe Tan /* Clear the index of Fault Recording Register */ 21841da12ec4SLe Tan s->next_frcd_reg = 0; 21851da12ec4SLe Tan /* Ok - report back to driver */ 21861da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0); 21871da12ec4SLe Tan } 2188558e0024SPeter Xu 21892cc9ddccSPeter Xu vtd_reset_caches(s); 21902cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 21911da12ec4SLe Tan } 21921da12ec4SLe Tan 219380de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */ 219480de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) 219580de52baSPeter Xu { 21967feb51b7SPeter Xu trace_vtd_ir_enable(en); 219780de52baSPeter Xu 219880de52baSPeter Xu if (en) { 219980de52baSPeter Xu s->intr_enabled = true; 220080de52baSPeter Xu /* Ok - report back to driver */ 220180de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES); 220280de52baSPeter Xu } else { 220380de52baSPeter Xu s->intr_enabled = false; 220480de52baSPeter Xu /* Ok - report back to driver */ 220580de52baSPeter Xu vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0); 220680de52baSPeter Xu } 220780de52baSPeter Xu } 220880de52baSPeter Xu 22091da12ec4SLe Tan /* Handle write to Global Command Register */ 22101da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s) 22111da12ec4SLe Tan { 22121da12ec4SLe Tan uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG); 22131da12ec4SLe Tan uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG); 22141da12ec4SLe Tan uint32_t changed = status ^ val; 22151da12ec4SLe Tan 22167feb51b7SPeter Xu trace_vtd_reg_write_gcmd(status, val); 2217*8646d9c7SDavid Woodhouse if ((changed & VTD_GCMD_TE) && s->dma_translation) { 22181da12ec4SLe Tan /* Translation enable/disable */ 22191da12ec4SLe Tan vtd_handle_gcmd_te(s, val & VTD_GCMD_TE); 22201da12ec4SLe Tan } 22211da12ec4SLe Tan if (val & VTD_GCMD_SRTP) { 22221da12ec4SLe Tan /* Set/update the root-table pointer */ 22231da12ec4SLe Tan vtd_handle_gcmd_srtp(s); 22241da12ec4SLe Tan } 2225ed7b8fbcSLe Tan if (changed & VTD_GCMD_QIE) { 2226ed7b8fbcSLe Tan /* Queued Invalidation Enable */ 2227ed7b8fbcSLe Tan vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE); 2228ed7b8fbcSLe Tan } 2229a5861439SPeter Xu if (val & VTD_GCMD_SIRTP) { 2230a5861439SPeter Xu /* Set/update the interrupt remapping root-table pointer */ 2231a5861439SPeter Xu vtd_handle_gcmd_sirtp(s); 2232a5861439SPeter Xu } 223380de52baSPeter Xu if (changed & VTD_GCMD_IRE) { 223480de52baSPeter Xu /* Interrupt remap enable/disable */ 223580de52baSPeter Xu vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE); 223680de52baSPeter Xu } 22371da12ec4SLe Tan } 22381da12ec4SLe Tan 22391da12ec4SLe Tan /* Handle write to Context Command Register */ 22401da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s) 22411da12ec4SLe Tan { 22421da12ec4SLe Tan uint64_t ret; 22431da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG); 22441da12ec4SLe Tan 22451da12ec4SLe Tan /* Context-cache invalidation request */ 22461da12ec4SLe Tan if (val & VTD_CCMD_ICC) { 2247ed7b8fbcSLe Tan if (s->qi_enabled) { 22481376211fSPeter Xu error_report_once("Queued Invalidation enabled, " 2249ed7b8fbcSLe Tan "should not use register-based invalidation"); 2250ed7b8fbcSLe Tan return; 2251ed7b8fbcSLe Tan } 22521da12ec4SLe Tan ret = vtd_context_cache_invalidate(s, val); 22531da12ec4SLe Tan /* Invalidation completed. Change something to show */ 22541da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL); 22551da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK, 22561da12ec4SLe Tan ret); 22571da12ec4SLe Tan } 22581da12ec4SLe Tan } 22591da12ec4SLe Tan 22601da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */ 22611da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s) 22621da12ec4SLe Tan { 22631da12ec4SLe Tan uint64_t ret; 22641da12ec4SLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG); 22651da12ec4SLe Tan 22661da12ec4SLe Tan /* IOTLB invalidation request */ 22671da12ec4SLe Tan if (val & VTD_TLB_IVT) { 2268ed7b8fbcSLe Tan if (s->qi_enabled) { 22691376211fSPeter Xu error_report_once("Queued Invalidation enabled, " 22701376211fSPeter Xu "should not use register-based invalidation"); 2271ed7b8fbcSLe Tan return; 2272ed7b8fbcSLe Tan } 22731da12ec4SLe Tan ret = vtd_iotlb_flush(s, val); 22741da12ec4SLe Tan /* Invalidation completed. Change something to show */ 22751da12ec4SLe Tan vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL); 22761da12ec4SLe Tan ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, 22771da12ec4SLe Tan VTD_TLB_FLUSH_GRANU_MASK_A, ret); 22781da12ec4SLe Tan } 22791da12ec4SLe Tan } 22801da12ec4SLe Tan 2281ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */ 2282c0c1d351SLiu, Yi L static bool vtd_get_inv_desc(IntelIOMMUState *s, 2283ed7b8fbcSLe Tan VTDInvDesc *inv_desc) 2284ed7b8fbcSLe Tan { 2285c0c1d351SLiu, Yi L dma_addr_t base_addr = s->iq; 2286c0c1d351SLiu, Yi L uint32_t offset = s->iq_head; 2287c0c1d351SLiu, Yi L uint32_t dw = s->iq_dw ? 32 : 16; 2288c0c1d351SLiu, Yi L dma_addr_t addr = base_addr + offset * dw; 2289c0c1d351SLiu, Yi L 2290ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 2291ba06fe8aSPhilippe Mathieu-Daudé inv_desc, dw, MEMTXATTRS_UNSPECIFIED)) { 2292c0c1d351SLiu, Yi L error_report_once("Read INV DESC failed."); 2293ed7b8fbcSLe Tan return false; 2294ed7b8fbcSLe Tan } 2295ed7b8fbcSLe Tan inv_desc->lo = le64_to_cpu(inv_desc->lo); 2296ed7b8fbcSLe Tan inv_desc->hi = le64_to_cpu(inv_desc->hi); 2297c0c1d351SLiu, Yi L if (dw == 32) { 2298c0c1d351SLiu, Yi L inv_desc->val[2] = le64_to_cpu(inv_desc->val[2]); 2299c0c1d351SLiu, Yi L inv_desc->val[3] = le64_to_cpu(inv_desc->val[3]); 2300c0c1d351SLiu, Yi L } 2301ed7b8fbcSLe Tan return true; 2302ed7b8fbcSLe Tan } 2303ed7b8fbcSLe Tan 2304ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 2305ed7b8fbcSLe Tan { 2306ed7b8fbcSLe Tan if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) || 2307ed7b8fbcSLe Tan (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) { 2308095955b2SPeter Xu error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64 2309095955b2SPeter Xu " (reserved nonzero)", __func__, inv_desc->hi, 2310095955b2SPeter Xu inv_desc->lo); 2311ed7b8fbcSLe Tan return false; 2312ed7b8fbcSLe Tan } 2313ed7b8fbcSLe Tan if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) { 2314ed7b8fbcSLe Tan /* Status Write */ 2315ed7b8fbcSLe Tan uint32_t status_data = (uint32_t)(inv_desc->lo >> 2316ed7b8fbcSLe Tan VTD_INV_DESC_WAIT_DATA_SHIFT); 2317ed7b8fbcSLe Tan 2318ed7b8fbcSLe Tan assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF)); 2319ed7b8fbcSLe Tan 2320ed7b8fbcSLe Tan /* FIXME: need to be masked with HAW? */ 2321ed7b8fbcSLe Tan dma_addr_t status_addr = inv_desc->hi; 2322bc535e59SPeter Xu trace_vtd_inv_desc_wait_sw(status_addr, status_data); 2323ed7b8fbcSLe Tan status_data = cpu_to_le32(status_data); 2324ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_write(&address_space_memory, status_addr, 2325ba06fe8aSPhilippe Mathieu-Daudé &status_data, sizeof(status_data), 2326ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED)) { 2327bc535e59SPeter Xu trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo); 2328ed7b8fbcSLe Tan return false; 2329ed7b8fbcSLe Tan } 2330ed7b8fbcSLe Tan } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) { 2331ed7b8fbcSLe Tan /* Interrupt flag */ 2332ed7b8fbcSLe Tan vtd_generate_completion_event(s); 2333ed7b8fbcSLe Tan } else { 2334095955b2SPeter Xu error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64 2335095955b2SPeter Xu " (unknown type)", __func__, inv_desc->hi, 2336095955b2SPeter Xu inv_desc->lo); 2337ed7b8fbcSLe Tan return false; 2338ed7b8fbcSLe Tan } 2339ed7b8fbcSLe Tan return true; 2340ed7b8fbcSLe Tan } 2341ed7b8fbcSLe Tan 2342d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s, 2343d92fa2dcSLe Tan VTDInvDesc *inv_desc) 2344d92fa2dcSLe Tan { 2345bc535e59SPeter Xu uint16_t sid, fmask; 2346bc535e59SPeter Xu 2347d92fa2dcSLe Tan if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) { 2348095955b2SPeter Xu error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64 2349095955b2SPeter Xu " (reserved nonzero)", __func__, inv_desc->hi, 2350095955b2SPeter Xu inv_desc->lo); 2351d92fa2dcSLe Tan return false; 2352d92fa2dcSLe Tan } 2353d92fa2dcSLe Tan switch (inv_desc->lo & VTD_INV_DESC_CC_G) { 2354d92fa2dcSLe Tan case VTD_INV_DESC_CC_DOMAIN: 2355bc535e59SPeter Xu trace_vtd_inv_desc_cc_domain( 2356d92fa2dcSLe Tan (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo)); 2357d92fa2dcSLe Tan /* Fall through */ 2358d92fa2dcSLe Tan case VTD_INV_DESC_CC_GLOBAL: 2359d92fa2dcSLe Tan vtd_context_global_invalidate(s); 2360d92fa2dcSLe Tan break; 2361d92fa2dcSLe Tan 2362d92fa2dcSLe Tan case VTD_INV_DESC_CC_DEVICE: 2363bc535e59SPeter Xu sid = VTD_INV_DESC_CC_SID(inv_desc->lo); 2364bc535e59SPeter Xu fmask = VTD_INV_DESC_CC_FM(inv_desc->lo); 2365bc535e59SPeter Xu vtd_context_device_invalidate(s, sid, fmask); 2366d92fa2dcSLe Tan break; 2367d92fa2dcSLe Tan 2368d92fa2dcSLe Tan default: 2369095955b2SPeter Xu error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64 2370095955b2SPeter Xu " (invalid type)", __func__, inv_desc->hi, 2371095955b2SPeter Xu inv_desc->lo); 2372d92fa2dcSLe Tan return false; 2373d92fa2dcSLe Tan } 2374d92fa2dcSLe Tan return true; 2375d92fa2dcSLe Tan } 2376d92fa2dcSLe Tan 2377b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) 2378b5a280c0SLe Tan { 2379b5a280c0SLe Tan uint16_t domain_id; 2380b5a280c0SLe Tan uint8_t am; 2381b5a280c0SLe Tan hwaddr addr; 2382b5a280c0SLe Tan 2383b5a280c0SLe Tan if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) || 2384b5a280c0SLe Tan (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) { 2385095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2386ff5b5d5bSMarkus Armbruster ", lo=0x%"PRIx64" (reserved bits unzero)", 2387095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo); 2388b5a280c0SLe Tan return false; 2389b5a280c0SLe Tan } 2390b5a280c0SLe Tan 2391b5a280c0SLe Tan switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) { 2392b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_GLOBAL: 2393b5a280c0SLe Tan vtd_iotlb_global_invalidate(s); 2394b5a280c0SLe Tan break; 2395b5a280c0SLe Tan 2396b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_DOMAIN: 2397b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 2398b5a280c0SLe Tan vtd_iotlb_domain_invalidate(s, domain_id); 2399b5a280c0SLe Tan break; 2400b5a280c0SLe Tan 2401b5a280c0SLe Tan case VTD_INV_DESC_IOTLB_PAGE: 2402b5a280c0SLe Tan domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo); 2403b5a280c0SLe Tan addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi); 2404b5a280c0SLe Tan am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi); 2405b5a280c0SLe Tan if (am > VTD_MAMV) { 2406095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2407ff5b5d5bSMarkus Armbruster ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)", 2408095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo, 2409095955b2SPeter Xu am, (unsigned)VTD_MAMV); 2410b5a280c0SLe Tan return false; 2411b5a280c0SLe Tan } 2412b5a280c0SLe Tan vtd_iotlb_page_invalidate(s, domain_id, addr, am); 2413b5a280c0SLe Tan break; 2414b5a280c0SLe Tan 2415b5a280c0SLe Tan default: 2416095955b2SPeter Xu error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 2417ff5b5d5bSMarkus Armbruster ", lo=0x%"PRIx64" (type mismatch: 0x%llx)", 2418095955b2SPeter Xu __func__, inv_desc->hi, inv_desc->lo, 2419095955b2SPeter Xu inv_desc->lo & VTD_INV_DESC_IOTLB_G); 2420b5a280c0SLe Tan return false; 2421b5a280c0SLe Tan } 2422b5a280c0SLe Tan return true; 2423b5a280c0SLe Tan } 2424b5a280c0SLe Tan 242502a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, 242602a2cbc8SPeter Xu VTDInvDesc *inv_desc) 242702a2cbc8SPeter Xu { 24287feb51b7SPeter Xu trace_vtd_inv_desc_iec(inv_desc->iec.granularity, 242902a2cbc8SPeter Xu inv_desc->iec.index, 243002a2cbc8SPeter Xu inv_desc->iec.index_mask); 243102a2cbc8SPeter Xu 243202a2cbc8SPeter Xu vtd_iec_notify_all(s, !inv_desc->iec.granularity, 243302a2cbc8SPeter Xu inv_desc->iec.index, 243402a2cbc8SPeter Xu inv_desc->iec.index_mask); 2435554f5e16SJason Wang return true; 2436554f5e16SJason Wang } 243702a2cbc8SPeter Xu 2438554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, 2439554f5e16SJason Wang VTDInvDesc *inv_desc) 2440554f5e16SJason Wang { 2441554f5e16SJason Wang VTDAddressSpace *vtd_dev_as; 24425039caf3SEugenio Pérez IOMMUTLBEvent event; 2443554f5e16SJason Wang struct VTDBus *vtd_bus; 2444554f5e16SJason Wang hwaddr addr; 2445554f5e16SJason Wang uint64_t sz; 2446554f5e16SJason Wang uint16_t sid; 2447554f5e16SJason Wang uint8_t devfn; 2448554f5e16SJason Wang bool size; 2449554f5e16SJason Wang uint8_t bus_num; 2450554f5e16SJason Wang 2451554f5e16SJason Wang addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi); 2452554f5e16SJason Wang sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo); 2453554f5e16SJason Wang devfn = sid & 0xff; 2454554f5e16SJason Wang bus_num = sid >> 8; 2455554f5e16SJason Wang size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi); 2456554f5e16SJason Wang 2457554f5e16SJason Wang if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) || 2458554f5e16SJason Wang (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) { 2459095955b2SPeter Xu error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64 2460095955b2SPeter Xu ", lo=%"PRIx64" (reserved nonzero)", __func__, 2461095955b2SPeter Xu inv_desc->hi, inv_desc->lo); 2462554f5e16SJason Wang return false; 2463554f5e16SJason Wang } 2464554f5e16SJason Wang 2465554f5e16SJason Wang vtd_bus = vtd_find_as_from_bus_num(s, bus_num); 2466554f5e16SJason Wang if (!vtd_bus) { 2467554f5e16SJason Wang goto done; 2468554f5e16SJason Wang } 2469554f5e16SJason Wang 2470554f5e16SJason Wang vtd_dev_as = vtd_bus->dev_as[devfn]; 2471554f5e16SJason Wang if (!vtd_dev_as) { 2472554f5e16SJason Wang goto done; 2473554f5e16SJason Wang } 2474554f5e16SJason Wang 247504eb6247SJason Wang /* According to ATS spec table 2.4: 247604eb6247SJason Wang * S = 0, bits 15:12 = xxxx range size: 4K 247704eb6247SJason Wang * S = 1, bits 15:12 = xxx0 range size: 8K 247804eb6247SJason Wang * S = 1, bits 15:12 = xx01 range size: 16K 247904eb6247SJason Wang * S = 1, bits 15:12 = x011 range size: 32K 248004eb6247SJason Wang * S = 1, bits 15:12 = 0111 range size: 64K 248104eb6247SJason Wang * ... 248204eb6247SJason Wang */ 2483554f5e16SJason Wang if (size) { 248404eb6247SJason Wang sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT); 2485554f5e16SJason Wang addr &= ~(sz - 1); 2486554f5e16SJason Wang } else { 2487554f5e16SJason Wang sz = VTD_PAGE_SIZE; 2488554f5e16SJason Wang } 2489554f5e16SJason Wang 2490b68ba1caSEugenio Pérez event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP; 24915039caf3SEugenio Pérez event.entry.target_as = &vtd_dev_as->as; 24925039caf3SEugenio Pérez event.entry.addr_mask = sz - 1; 24935039caf3SEugenio Pérez event.entry.iova = addr; 24945039caf3SEugenio Pérez event.entry.perm = IOMMU_NONE; 24955039caf3SEugenio Pérez event.entry.translated_addr = 0; 24965039caf3SEugenio Pérez memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event); 2497554f5e16SJason Wang 2498554f5e16SJason Wang done: 249902a2cbc8SPeter Xu return true; 250002a2cbc8SPeter Xu } 250102a2cbc8SPeter Xu 2502ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s) 2503ed7b8fbcSLe Tan { 2504ed7b8fbcSLe Tan VTDInvDesc inv_desc; 2505ed7b8fbcSLe Tan uint8_t desc_type; 2506ed7b8fbcSLe Tan 25077feb51b7SPeter Xu trace_vtd_inv_qi_head(s->iq_head); 2508c0c1d351SLiu, Yi L if (!vtd_get_inv_desc(s, &inv_desc)) { 2509ed7b8fbcSLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 2510ed7b8fbcSLe Tan return false; 2511ed7b8fbcSLe Tan } 2512c0c1d351SLiu, Yi L 2513ed7b8fbcSLe Tan desc_type = inv_desc.lo & VTD_INV_DESC_TYPE; 2514ed7b8fbcSLe Tan /* FIXME: should update at first or at last? */ 2515ed7b8fbcSLe Tan s->iq_last_desc_type = desc_type; 2516ed7b8fbcSLe Tan 2517ed7b8fbcSLe Tan switch (desc_type) { 2518ed7b8fbcSLe Tan case VTD_INV_DESC_CC: 2519bc535e59SPeter Xu trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo); 2520d92fa2dcSLe Tan if (!vtd_process_context_cache_desc(s, &inv_desc)) { 2521d92fa2dcSLe Tan return false; 2522d92fa2dcSLe Tan } 2523ed7b8fbcSLe Tan break; 2524ed7b8fbcSLe Tan 2525ed7b8fbcSLe Tan case VTD_INV_DESC_IOTLB: 2526bc535e59SPeter Xu trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo); 2527b5a280c0SLe Tan if (!vtd_process_iotlb_desc(s, &inv_desc)) { 2528b5a280c0SLe Tan return false; 2529b5a280c0SLe Tan } 2530ed7b8fbcSLe Tan break; 2531ed7b8fbcSLe Tan 25324a4f219eSYi Sun /* 25334a4f219eSYi Sun * TODO: the entity of below two cases will be implemented in future series. 25344a4f219eSYi Sun * To make guest (which integrates scalable mode support patch set in 25354a4f219eSYi Sun * iommu driver) work, just return true is enough so far. 25364a4f219eSYi Sun */ 25374a4f219eSYi Sun case VTD_INV_DESC_PC: 25384a4f219eSYi Sun break; 25394a4f219eSYi Sun 25404a4f219eSYi Sun case VTD_INV_DESC_PIOTLB: 25414a4f219eSYi Sun break; 25424a4f219eSYi Sun 2543ed7b8fbcSLe Tan case VTD_INV_DESC_WAIT: 2544bc535e59SPeter Xu trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo); 2545ed7b8fbcSLe Tan if (!vtd_process_wait_desc(s, &inv_desc)) { 2546ed7b8fbcSLe Tan return false; 2547ed7b8fbcSLe Tan } 2548ed7b8fbcSLe Tan break; 2549ed7b8fbcSLe Tan 2550b7910472SPeter Xu case VTD_INV_DESC_IEC: 2551bc535e59SPeter Xu trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo); 255202a2cbc8SPeter Xu if (!vtd_process_inv_iec_desc(s, &inv_desc)) { 255302a2cbc8SPeter Xu return false; 255402a2cbc8SPeter Xu } 2555b7910472SPeter Xu break; 2556b7910472SPeter Xu 2557554f5e16SJason Wang case VTD_INV_DESC_DEVICE: 25587feb51b7SPeter Xu trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo); 2559554f5e16SJason Wang if (!vtd_process_device_iotlb_desc(s, &inv_desc)) { 2560554f5e16SJason Wang return false; 2561554f5e16SJason Wang } 2562554f5e16SJason Wang break; 2563554f5e16SJason Wang 2564ed7b8fbcSLe Tan default: 2565095955b2SPeter Xu error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64 2566095955b2SPeter Xu " (unknown type)", __func__, inv_desc.hi, 2567095955b2SPeter Xu inv_desc.lo); 2568ed7b8fbcSLe Tan return false; 2569ed7b8fbcSLe Tan } 2570ed7b8fbcSLe Tan s->iq_head++; 2571ed7b8fbcSLe Tan if (s->iq_head == s->iq_size) { 2572ed7b8fbcSLe Tan s->iq_head = 0; 2573ed7b8fbcSLe Tan } 2574ed7b8fbcSLe Tan return true; 2575ed7b8fbcSLe Tan } 2576ed7b8fbcSLe Tan 2577ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */ 2578ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s) 2579ed7b8fbcSLe Tan { 2580a4544c45SLiu Yi L int qi_shift; 2581a4544c45SLiu Yi L 2582a4544c45SLiu Yi L /* Refer to 10.4.23 of VT-d spec 3.0 */ 2583a4544c45SLiu Yi L qi_shift = s->iq_dw ? VTD_IQH_QH_SHIFT_5 : VTD_IQH_QH_SHIFT_4; 2584a4544c45SLiu Yi L 25857feb51b7SPeter Xu trace_vtd_inv_qi_fetch(); 25867feb51b7SPeter Xu 2587ed7b8fbcSLe Tan if (s->iq_tail >= s->iq_size) { 2588ed7b8fbcSLe Tan /* Detects an invalid Tail pointer */ 25894e4abd11SPeter Xu error_report_once("%s: detected invalid QI tail " 25904e4abd11SPeter Xu "(tail=0x%x, size=0x%x)", 25914e4abd11SPeter Xu __func__, s->iq_tail, s->iq_size); 2592ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 2593ed7b8fbcSLe Tan return; 2594ed7b8fbcSLe Tan } 2595ed7b8fbcSLe Tan while (s->iq_head != s->iq_tail) { 2596ed7b8fbcSLe Tan if (!vtd_process_inv_desc(s)) { 2597ed7b8fbcSLe Tan /* Invalidation Queue Errors */ 2598ed7b8fbcSLe Tan vtd_handle_inv_queue_error(s); 2599ed7b8fbcSLe Tan break; 2600ed7b8fbcSLe Tan } 2601ed7b8fbcSLe Tan /* Must update the IQH_REG in time */ 2602ed7b8fbcSLe Tan vtd_set_quad_raw(s, DMAR_IQH_REG, 2603a4544c45SLiu Yi L (((uint64_t)(s->iq_head)) << qi_shift) & 2604ed7b8fbcSLe Tan VTD_IQH_QH_MASK); 2605ed7b8fbcSLe Tan } 2606ed7b8fbcSLe Tan } 2607ed7b8fbcSLe Tan 2608ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */ 2609ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s) 2610ed7b8fbcSLe Tan { 2611ed7b8fbcSLe Tan uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG); 2612ed7b8fbcSLe Tan 2613c0c1d351SLiu, Yi L if (s->iq_dw && (val & VTD_IQT_QT_256_RSV_BIT)) { 2614c0c1d351SLiu, Yi L error_report_once("%s: RSV bit is set: val=0x%"PRIx64, 2615c0c1d351SLiu, Yi L __func__, val); 2616c0c1d351SLiu, Yi L return; 2617c0c1d351SLiu, Yi L } 2618c0c1d351SLiu, Yi L s->iq_tail = VTD_IQT_QT(s->iq_dw, val); 26197feb51b7SPeter Xu trace_vtd_inv_qi_tail(s->iq_tail); 26207feb51b7SPeter Xu 2621ed7b8fbcSLe Tan if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) { 2622ed7b8fbcSLe Tan /* Process Invalidation Queue here */ 2623ed7b8fbcSLe Tan vtd_fetch_inv_desc(s); 2624ed7b8fbcSLe Tan } 2625ed7b8fbcSLe Tan } 2626ed7b8fbcSLe Tan 26271da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s) 26281da12ec4SLe Tan { 26291da12ec4SLe Tan uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG); 26301da12ec4SLe Tan uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 26311da12ec4SLe Tan uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE; 26321da12ec4SLe Tan 26331da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) { 26341da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 26357feb51b7SPeter Xu trace_vtd_fsts_clear_ip(); 26361da12ec4SLe Tan } 2637ed7b8fbcSLe Tan /* FIXME: when IQE is Clear, should we try to fetch some Invalidation 2638ed7b8fbcSLe Tan * Descriptors if there are any when Queued Invalidation is enabled? 2639ed7b8fbcSLe Tan */ 26401da12ec4SLe Tan } 26411da12ec4SLe Tan 26421da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s) 26431da12ec4SLe Tan { 26441da12ec4SLe Tan uint32_t fectl_reg; 26451da12ec4SLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 26461da12ec4SLe Tan * need to compare the old value and the new value to conclude that 26471da12ec4SLe Tan * software clears the IM field? Or just check if the IM field is zero? 26481da12ec4SLe Tan */ 26491da12ec4SLe Tan fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG); 26507feb51b7SPeter Xu 26517feb51b7SPeter Xu trace_vtd_reg_write_fectl(fectl_reg); 26527feb51b7SPeter Xu 26531da12ec4SLe Tan if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) { 26541da12ec4SLe Tan vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG); 26551da12ec4SLe Tan vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0); 26561da12ec4SLe Tan } 26571da12ec4SLe Tan } 26581da12ec4SLe Tan 2659ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s) 2660ed7b8fbcSLe Tan { 2661ed7b8fbcSLe Tan uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG); 2662ed7b8fbcSLe Tan uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 2663ed7b8fbcSLe Tan 2664ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) { 26657feb51b7SPeter Xu trace_vtd_reg_ics_clear_ip(); 2666ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 2667ed7b8fbcSLe Tan } 2668ed7b8fbcSLe Tan } 2669ed7b8fbcSLe Tan 2670ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s) 2671ed7b8fbcSLe Tan { 2672ed7b8fbcSLe Tan uint32_t iectl_reg; 2673ed7b8fbcSLe Tan /* FIXME: when software clears the IM field, check the IP field. But do we 2674ed7b8fbcSLe Tan * need to compare the old value and the new value to conclude that 2675ed7b8fbcSLe Tan * software clears the IM field? Or just check if the IM field is zero? 2676ed7b8fbcSLe Tan */ 2677ed7b8fbcSLe Tan iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG); 26787feb51b7SPeter Xu 26797feb51b7SPeter Xu trace_vtd_reg_write_iectl(iectl_reg); 26807feb51b7SPeter Xu 2681ed7b8fbcSLe Tan if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) { 2682ed7b8fbcSLe Tan vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG); 2683ed7b8fbcSLe Tan vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0); 2684ed7b8fbcSLe Tan } 2685ed7b8fbcSLe Tan } 2686ed7b8fbcSLe Tan 26871da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size) 26881da12ec4SLe Tan { 26891da12ec4SLe Tan IntelIOMMUState *s = opaque; 26901da12ec4SLe Tan uint64_t val; 26911da12ec4SLe Tan 26927feb51b7SPeter Xu trace_vtd_reg_read(addr, size); 26937feb51b7SPeter Xu 26941da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 26951376211fSPeter Xu error_report_once("%s: MMIO over range: addr=0x%" PRIx64 269673beb01eSPeter Xu " size=0x%x", __func__, addr, size); 26971da12ec4SLe Tan return (uint64_t)-1; 26981da12ec4SLe Tan } 26991da12ec4SLe Tan 27001da12ec4SLe Tan switch (addr) { 27011da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 27021da12ec4SLe Tan case DMAR_RTADDR_REG: 27038fdee711SYi Sun val = vtd_get_quad_raw(s, DMAR_RTADDR_REG); 27041da12ec4SLe Tan if (size == 4) { 27058fdee711SYi Sun val = val & ((1ULL << 32) - 1); 27061da12ec4SLe Tan } 27071da12ec4SLe Tan break; 27081da12ec4SLe Tan 27091da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 27101da12ec4SLe Tan assert(size == 4); 27118fdee711SYi Sun val = vtd_get_quad_raw(s, DMAR_RTADDR_REG) >> 32; 27121da12ec4SLe Tan break; 27131da12ec4SLe Tan 2714ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2715ed7b8fbcSLe Tan case DMAR_IQA_REG: 2716ed7b8fbcSLe Tan val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS); 2717ed7b8fbcSLe Tan if (size == 4) { 2718ed7b8fbcSLe Tan val = val & ((1ULL << 32) - 1); 2719ed7b8fbcSLe Tan } 2720ed7b8fbcSLe Tan break; 2721ed7b8fbcSLe Tan 2722ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2723ed7b8fbcSLe Tan assert(size == 4); 2724ed7b8fbcSLe Tan val = s->iq >> 32; 2725ed7b8fbcSLe Tan break; 2726ed7b8fbcSLe Tan 27271da12ec4SLe Tan default: 27281da12ec4SLe Tan if (size == 4) { 27291da12ec4SLe Tan val = vtd_get_long(s, addr); 27301da12ec4SLe Tan } else { 27311da12ec4SLe Tan val = vtd_get_quad(s, addr); 27321da12ec4SLe Tan } 27331da12ec4SLe Tan } 27347feb51b7SPeter Xu 27351da12ec4SLe Tan return val; 27361da12ec4SLe Tan } 27371da12ec4SLe Tan 27381da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr, 27391da12ec4SLe Tan uint64_t val, unsigned size) 27401da12ec4SLe Tan { 27411da12ec4SLe Tan IntelIOMMUState *s = opaque; 27421da12ec4SLe Tan 27437feb51b7SPeter Xu trace_vtd_reg_write(addr, size, val); 27447feb51b7SPeter Xu 27451da12ec4SLe Tan if (addr + size > DMAR_REG_SIZE) { 27461376211fSPeter Xu error_report_once("%s: MMIO over range: addr=0x%" PRIx64 274773beb01eSPeter Xu " size=0x%x", __func__, addr, size); 27481da12ec4SLe Tan return; 27491da12ec4SLe Tan } 27501da12ec4SLe Tan 27511da12ec4SLe Tan switch (addr) { 27521da12ec4SLe Tan /* Global Command Register, 32-bit */ 27531da12ec4SLe Tan case DMAR_GCMD_REG: 27541da12ec4SLe Tan vtd_set_long(s, addr, val); 27551da12ec4SLe Tan vtd_handle_gcmd_write(s); 27561da12ec4SLe Tan break; 27571da12ec4SLe Tan 27581da12ec4SLe Tan /* Context Command Register, 64-bit */ 27591da12ec4SLe Tan case DMAR_CCMD_REG: 27601da12ec4SLe Tan if (size == 4) { 27611da12ec4SLe Tan vtd_set_long(s, addr, val); 27621da12ec4SLe Tan } else { 27631da12ec4SLe Tan vtd_set_quad(s, addr, val); 27641da12ec4SLe Tan vtd_handle_ccmd_write(s); 27651da12ec4SLe Tan } 27661da12ec4SLe Tan break; 27671da12ec4SLe Tan 27681da12ec4SLe Tan case DMAR_CCMD_REG_HI: 27691da12ec4SLe Tan assert(size == 4); 27701da12ec4SLe Tan vtd_set_long(s, addr, val); 27711da12ec4SLe Tan vtd_handle_ccmd_write(s); 27721da12ec4SLe Tan break; 27731da12ec4SLe Tan 27741da12ec4SLe Tan /* IOTLB Invalidation Register, 64-bit */ 27751da12ec4SLe Tan case DMAR_IOTLB_REG: 27761da12ec4SLe Tan if (size == 4) { 27771da12ec4SLe Tan vtd_set_long(s, addr, val); 27781da12ec4SLe Tan } else { 27791da12ec4SLe Tan vtd_set_quad(s, addr, val); 27801da12ec4SLe Tan vtd_handle_iotlb_write(s); 27811da12ec4SLe Tan } 27821da12ec4SLe Tan break; 27831da12ec4SLe Tan 27841da12ec4SLe Tan case DMAR_IOTLB_REG_HI: 27851da12ec4SLe Tan assert(size == 4); 27861da12ec4SLe Tan vtd_set_long(s, addr, val); 27871da12ec4SLe Tan vtd_handle_iotlb_write(s); 27881da12ec4SLe Tan break; 27891da12ec4SLe Tan 2790b5a280c0SLe Tan /* Invalidate Address Register, 64-bit */ 2791b5a280c0SLe Tan case DMAR_IVA_REG: 2792b5a280c0SLe Tan if (size == 4) { 2793b5a280c0SLe Tan vtd_set_long(s, addr, val); 2794b5a280c0SLe Tan } else { 2795b5a280c0SLe Tan vtd_set_quad(s, addr, val); 2796b5a280c0SLe Tan } 2797b5a280c0SLe Tan break; 2798b5a280c0SLe Tan 2799b5a280c0SLe Tan case DMAR_IVA_REG_HI: 2800b5a280c0SLe Tan assert(size == 4); 2801b5a280c0SLe Tan vtd_set_long(s, addr, val); 2802b5a280c0SLe Tan break; 2803b5a280c0SLe Tan 28041da12ec4SLe Tan /* Fault Status Register, 32-bit */ 28051da12ec4SLe Tan case DMAR_FSTS_REG: 28061da12ec4SLe Tan assert(size == 4); 28071da12ec4SLe Tan vtd_set_long(s, addr, val); 28081da12ec4SLe Tan vtd_handle_fsts_write(s); 28091da12ec4SLe Tan break; 28101da12ec4SLe Tan 28111da12ec4SLe Tan /* Fault Event Control Register, 32-bit */ 28121da12ec4SLe Tan case DMAR_FECTL_REG: 28131da12ec4SLe Tan assert(size == 4); 28141da12ec4SLe Tan vtd_set_long(s, addr, val); 28151da12ec4SLe Tan vtd_handle_fectl_write(s); 28161da12ec4SLe Tan break; 28171da12ec4SLe Tan 28181da12ec4SLe Tan /* Fault Event Data Register, 32-bit */ 28191da12ec4SLe Tan case DMAR_FEDATA_REG: 28201da12ec4SLe Tan assert(size == 4); 28211da12ec4SLe Tan vtd_set_long(s, addr, val); 28221da12ec4SLe Tan break; 28231da12ec4SLe Tan 28241da12ec4SLe Tan /* Fault Event Address Register, 32-bit */ 28251da12ec4SLe Tan case DMAR_FEADDR_REG: 2826b7a7bb35SJan Kiszka if (size == 4) { 28271da12ec4SLe Tan vtd_set_long(s, addr, val); 2828b7a7bb35SJan Kiszka } else { 2829b7a7bb35SJan Kiszka /* 2830b7a7bb35SJan Kiszka * While the register is 32-bit only, some guests (Xen...) write to 2831b7a7bb35SJan Kiszka * it with 64-bit. 2832b7a7bb35SJan Kiszka */ 2833b7a7bb35SJan Kiszka vtd_set_quad(s, addr, val); 2834b7a7bb35SJan Kiszka } 28351da12ec4SLe Tan break; 28361da12ec4SLe Tan 28371da12ec4SLe Tan /* Fault Event Upper Address Register, 32-bit */ 28381da12ec4SLe Tan case DMAR_FEUADDR_REG: 28391da12ec4SLe Tan assert(size == 4); 28401da12ec4SLe Tan vtd_set_long(s, addr, val); 28411da12ec4SLe Tan break; 28421da12ec4SLe Tan 28431da12ec4SLe Tan /* Protected Memory Enable Register, 32-bit */ 28441da12ec4SLe Tan case DMAR_PMEN_REG: 28451da12ec4SLe Tan assert(size == 4); 28461da12ec4SLe Tan vtd_set_long(s, addr, val); 28471da12ec4SLe Tan break; 28481da12ec4SLe Tan 28491da12ec4SLe Tan /* Root Table Address Register, 64-bit */ 28501da12ec4SLe Tan case DMAR_RTADDR_REG: 28511da12ec4SLe Tan if (size == 4) { 28521da12ec4SLe Tan vtd_set_long(s, addr, val); 28531da12ec4SLe Tan } else { 28541da12ec4SLe Tan vtd_set_quad(s, addr, val); 28551da12ec4SLe Tan } 28561da12ec4SLe Tan break; 28571da12ec4SLe Tan 28581da12ec4SLe Tan case DMAR_RTADDR_REG_HI: 28591da12ec4SLe Tan assert(size == 4); 28601da12ec4SLe Tan vtd_set_long(s, addr, val); 28611da12ec4SLe Tan break; 28621da12ec4SLe Tan 2863ed7b8fbcSLe Tan /* Invalidation Queue Tail Register, 64-bit */ 2864ed7b8fbcSLe Tan case DMAR_IQT_REG: 2865ed7b8fbcSLe Tan if (size == 4) { 2866ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2867ed7b8fbcSLe Tan } else { 2868ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2869ed7b8fbcSLe Tan } 2870ed7b8fbcSLe Tan vtd_handle_iqt_write(s); 2871ed7b8fbcSLe Tan break; 2872ed7b8fbcSLe Tan 2873ed7b8fbcSLe Tan case DMAR_IQT_REG_HI: 2874ed7b8fbcSLe Tan assert(size == 4); 2875ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2876ed7b8fbcSLe Tan /* 19:63 of IQT_REG is RsvdZ, do nothing here */ 2877ed7b8fbcSLe Tan break; 2878ed7b8fbcSLe Tan 2879ed7b8fbcSLe Tan /* Invalidation Queue Address Register, 64-bit */ 2880ed7b8fbcSLe Tan case DMAR_IQA_REG: 2881ed7b8fbcSLe Tan if (size == 4) { 2882ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2883ed7b8fbcSLe Tan } else { 2884ed7b8fbcSLe Tan vtd_set_quad(s, addr, val); 2885ed7b8fbcSLe Tan } 2886c0c1d351SLiu, Yi L if (s->ecap & VTD_ECAP_SMTS && 2887c0c1d351SLiu, Yi L val & VTD_IQA_DW_MASK) { 2888c0c1d351SLiu, Yi L s->iq_dw = true; 2889c0c1d351SLiu, Yi L } else { 2890c0c1d351SLiu, Yi L s->iq_dw = false; 2891c0c1d351SLiu, Yi L } 2892ed7b8fbcSLe Tan break; 2893ed7b8fbcSLe Tan 2894ed7b8fbcSLe Tan case DMAR_IQA_REG_HI: 2895ed7b8fbcSLe Tan assert(size == 4); 2896ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2897ed7b8fbcSLe Tan break; 2898ed7b8fbcSLe Tan 2899ed7b8fbcSLe Tan /* Invalidation Completion Status Register, 32-bit */ 2900ed7b8fbcSLe Tan case DMAR_ICS_REG: 2901ed7b8fbcSLe Tan assert(size == 4); 2902ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2903ed7b8fbcSLe Tan vtd_handle_ics_write(s); 2904ed7b8fbcSLe Tan break; 2905ed7b8fbcSLe Tan 2906ed7b8fbcSLe Tan /* Invalidation Event Control Register, 32-bit */ 2907ed7b8fbcSLe Tan case DMAR_IECTL_REG: 2908ed7b8fbcSLe Tan assert(size == 4); 2909ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2910ed7b8fbcSLe Tan vtd_handle_iectl_write(s); 2911ed7b8fbcSLe Tan break; 2912ed7b8fbcSLe Tan 2913ed7b8fbcSLe Tan /* Invalidation Event Data Register, 32-bit */ 2914ed7b8fbcSLe Tan case DMAR_IEDATA_REG: 2915ed7b8fbcSLe Tan assert(size == 4); 2916ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2917ed7b8fbcSLe Tan break; 2918ed7b8fbcSLe Tan 2919ed7b8fbcSLe Tan /* Invalidation Event Address Register, 32-bit */ 2920ed7b8fbcSLe Tan case DMAR_IEADDR_REG: 2921ed7b8fbcSLe Tan assert(size == 4); 2922ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2923ed7b8fbcSLe Tan break; 2924ed7b8fbcSLe Tan 2925ed7b8fbcSLe Tan /* Invalidation Event Upper Address Register, 32-bit */ 2926ed7b8fbcSLe Tan case DMAR_IEUADDR_REG: 2927ed7b8fbcSLe Tan assert(size == 4); 2928ed7b8fbcSLe Tan vtd_set_long(s, addr, val); 2929ed7b8fbcSLe Tan break; 2930ed7b8fbcSLe Tan 29311da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 29321da12ec4SLe Tan case DMAR_FRCD_REG_0_0: 29331da12ec4SLe Tan if (size == 4) { 29341da12ec4SLe Tan vtd_set_long(s, addr, val); 29351da12ec4SLe Tan } else { 29361da12ec4SLe Tan vtd_set_quad(s, addr, val); 29371da12ec4SLe Tan } 29381da12ec4SLe Tan break; 29391da12ec4SLe Tan 29401da12ec4SLe Tan case DMAR_FRCD_REG_0_1: 29411da12ec4SLe Tan assert(size == 4); 29421da12ec4SLe Tan vtd_set_long(s, addr, val); 29431da12ec4SLe Tan break; 29441da12ec4SLe Tan 29451da12ec4SLe Tan case DMAR_FRCD_REG_0_2: 29461da12ec4SLe Tan if (size == 4) { 29471da12ec4SLe Tan vtd_set_long(s, addr, val); 29481da12ec4SLe Tan } else { 29491da12ec4SLe Tan vtd_set_quad(s, addr, val); 29501da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 29511da12ec4SLe Tan vtd_update_fsts_ppf(s); 29521da12ec4SLe Tan } 29531da12ec4SLe Tan break; 29541da12ec4SLe Tan 29551da12ec4SLe Tan case DMAR_FRCD_REG_0_3: 29561da12ec4SLe Tan assert(size == 4); 29571da12ec4SLe Tan vtd_set_long(s, addr, val); 29581da12ec4SLe Tan /* May clear bit 127 (Fault), update PPF */ 29591da12ec4SLe Tan vtd_update_fsts_ppf(s); 29601da12ec4SLe Tan break; 29611da12ec4SLe Tan 2962a5861439SPeter Xu case DMAR_IRTA_REG: 2963a5861439SPeter Xu if (size == 4) { 2964a5861439SPeter Xu vtd_set_long(s, addr, val); 2965a5861439SPeter Xu } else { 2966a5861439SPeter Xu vtd_set_quad(s, addr, val); 2967a5861439SPeter Xu } 2968a5861439SPeter Xu break; 2969a5861439SPeter Xu 2970a5861439SPeter Xu case DMAR_IRTA_REG_HI: 2971a5861439SPeter Xu assert(size == 4); 2972a5861439SPeter Xu vtd_set_long(s, addr, val); 2973a5861439SPeter Xu break; 2974a5861439SPeter Xu 29751da12ec4SLe Tan default: 29761da12ec4SLe Tan if (size == 4) { 29771da12ec4SLe Tan vtd_set_long(s, addr, val); 29781da12ec4SLe Tan } else { 29791da12ec4SLe Tan vtd_set_quad(s, addr, val); 29801da12ec4SLe Tan } 29811da12ec4SLe Tan } 29821da12ec4SLe Tan } 29831da12ec4SLe Tan 29843df9d748SAlexey Kardashevskiy static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr, 29852c91bcf2SPeter Maydell IOMMUAccessFlags flag, int iommu_idx) 29861da12ec4SLe Tan { 29871da12ec4SLe Tan VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 29881da12ec4SLe Tan IntelIOMMUState *s = vtd_as->iommu_state; 2989b9313021SPeter Xu IOMMUTLBEntry iotlb = { 2990b9313021SPeter Xu /* We'll fill in the rest later. */ 29911da12ec4SLe Tan .target_as = &address_space_memory, 29921da12ec4SLe Tan }; 2993b9313021SPeter Xu bool success; 29941da12ec4SLe Tan 2995b9313021SPeter Xu if (likely(s->dmar_enabled)) { 2996b9313021SPeter Xu success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn, 2997b9313021SPeter Xu addr, flag & IOMMU_WO, &iotlb); 2998b9313021SPeter Xu } else { 29991da12ec4SLe Tan /* DMAR disabled, passthrough, use 4k-page*/ 3000b9313021SPeter Xu iotlb.iova = addr & VTD_PAGE_MASK_4K; 3001b9313021SPeter Xu iotlb.translated_addr = addr & VTD_PAGE_MASK_4K; 3002b9313021SPeter Xu iotlb.addr_mask = ~VTD_PAGE_MASK_4K; 3003b9313021SPeter Xu iotlb.perm = IOMMU_RW; 3004b9313021SPeter Xu success = true; 30051da12ec4SLe Tan } 30061da12ec4SLe Tan 3007b9313021SPeter Xu if (likely(success)) { 30087feb51b7SPeter Xu trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus), 30097feb51b7SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 30107feb51b7SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 3011b9313021SPeter Xu iotlb.iova, iotlb.translated_addr, 3012b9313021SPeter Xu iotlb.addr_mask); 3013b9313021SPeter Xu } else { 30144e4abd11SPeter Xu error_report_once("%s: detected translation failure " 30154e4abd11SPeter Xu "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")", 30164e4abd11SPeter Xu __func__, pci_bus_num(vtd_as->bus), 3017b9313021SPeter Xu VTD_PCI_SLOT(vtd_as->devfn), 3018b9313021SPeter Xu VTD_PCI_FUNC(vtd_as->devfn), 3019662b4b69SPeter Xu addr); 3020b9313021SPeter Xu } 30217feb51b7SPeter Xu 3022b9313021SPeter Xu return iotlb; 30231da12ec4SLe Tan } 30241da12ec4SLe Tan 3025549d4005SEric Auger static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, 30265bf3d319SPeter Xu IOMMUNotifierFlag old, 3027549d4005SEric Auger IOMMUNotifierFlag new, 3028549d4005SEric Auger Error **errp) 30293cb3b154SAlex Williamson { 30303cb3b154SAlex Williamson VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); 3031dd4d607eSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 30323cb3b154SAlex Williamson 3033b8ffd7d6SJason Wang /* TODO: add support for VFIO and vhost users */ 3034b8ffd7d6SJason Wang if (s->snoop_control) { 3035250227f4SJason Wang error_setg_errno(errp, ENOTSUP, 3036b8ffd7d6SJason Wang "Snoop Control with vhost or VFIO is not supported"); 3037b8ffd7d6SJason Wang return -ENOTSUP; 3038b8ffd7d6SJason Wang } 3039b8ffd7d6SJason Wang 30404f8a62a9SPeter Xu /* Update per-address-space notifier flags */ 30414f8a62a9SPeter Xu vtd_as->notifier_flags = new; 30424f8a62a9SPeter Xu 3043dd4d607eSPeter Xu if (old == IOMMU_NOTIFIER_NONE) { 3044b4a4ba0dSPeter Xu QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next); 3045b4a4ba0dSPeter Xu } else if (new == IOMMU_NOTIFIER_NONE) { 3046b4a4ba0dSPeter Xu QLIST_REMOVE(vtd_as, next); 3047dd4d607eSPeter Xu } 3048549d4005SEric Auger return 0; 30493cb3b154SAlex Williamson } 30503cb3b154SAlex Williamson 3051552a1e01SPeter Xu static int vtd_post_load(void *opaque, int version_id) 3052552a1e01SPeter Xu { 3053552a1e01SPeter Xu IntelIOMMUState *iommu = opaque; 3054552a1e01SPeter Xu 3055552a1e01SPeter Xu /* 3056552a1e01SPeter Xu * Memory regions are dynamically turned on/off depending on 3057552a1e01SPeter Xu * context entry configurations from the guest. After migration, 3058552a1e01SPeter Xu * we need to make sure the memory regions are still correct. 3059552a1e01SPeter Xu */ 3060552a1e01SPeter Xu vtd_switch_address_space_all(iommu); 3061552a1e01SPeter Xu 30622811af3bSPeter Xu /* 30632811af3bSPeter Xu * We don't need to migrate the root_scalable because we can 30642811af3bSPeter Xu * simply do the calculation after the loading is complete. We 30652811af3bSPeter Xu * can actually do similar things with root, dmar_enabled, etc. 30662811af3bSPeter Xu * however since we've had them already so we'd better keep them 30672811af3bSPeter Xu * for compatibility of migration. 30682811af3bSPeter Xu */ 30692811af3bSPeter Xu vtd_update_scalable_state(iommu); 30702811af3bSPeter Xu 3071552a1e01SPeter Xu return 0; 3072552a1e01SPeter Xu } 3073552a1e01SPeter Xu 30741da12ec4SLe Tan static const VMStateDescription vtd_vmstate = { 30751da12ec4SLe Tan .name = "iommu-intel", 30768cdcf3c1SPeter Xu .version_id = 1, 30778cdcf3c1SPeter Xu .minimum_version_id = 1, 30788cdcf3c1SPeter Xu .priority = MIG_PRI_IOMMU, 3079552a1e01SPeter Xu .post_load = vtd_post_load, 30808cdcf3c1SPeter Xu .fields = (VMStateField[]) { 30818cdcf3c1SPeter Xu VMSTATE_UINT64(root, IntelIOMMUState), 30828cdcf3c1SPeter Xu VMSTATE_UINT64(intr_root, IntelIOMMUState), 30838cdcf3c1SPeter Xu VMSTATE_UINT64(iq, IntelIOMMUState), 30848cdcf3c1SPeter Xu VMSTATE_UINT32(intr_size, IntelIOMMUState), 30858cdcf3c1SPeter Xu VMSTATE_UINT16(iq_head, IntelIOMMUState), 30868cdcf3c1SPeter Xu VMSTATE_UINT16(iq_tail, IntelIOMMUState), 30878cdcf3c1SPeter Xu VMSTATE_UINT16(iq_size, IntelIOMMUState), 30888cdcf3c1SPeter Xu VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState), 30898cdcf3c1SPeter Xu VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE), 30908cdcf3c1SPeter Xu VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState), 309181fb1e64SPeter Xu VMSTATE_UNUSED(1), /* bool root_extended is obsolete by VT-d */ 30928cdcf3c1SPeter Xu VMSTATE_BOOL(dmar_enabled, IntelIOMMUState), 30938cdcf3c1SPeter Xu VMSTATE_BOOL(qi_enabled, IntelIOMMUState), 30948cdcf3c1SPeter Xu VMSTATE_BOOL(intr_enabled, IntelIOMMUState), 30958cdcf3c1SPeter Xu VMSTATE_BOOL(intr_eime, IntelIOMMUState), 30968cdcf3c1SPeter Xu VMSTATE_END_OF_LIST() 30978cdcf3c1SPeter Xu } 30981da12ec4SLe Tan }; 30991da12ec4SLe Tan 31001da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = { 31011da12ec4SLe Tan .read = vtd_mem_read, 31021da12ec4SLe Tan .write = vtd_mem_write, 31031da12ec4SLe Tan .endianness = DEVICE_LITTLE_ENDIAN, 31041da12ec4SLe Tan .impl = { 31051da12ec4SLe Tan .min_access_size = 4, 31061da12ec4SLe Tan .max_access_size = 8, 31071da12ec4SLe Tan }, 31081da12ec4SLe Tan .valid = { 31091da12ec4SLe Tan .min_access_size = 4, 31101da12ec4SLe Tan .max_access_size = 8, 31111da12ec4SLe Tan }, 31121da12ec4SLe Tan }; 31131da12ec4SLe Tan 31141da12ec4SLe Tan static Property vtd_properties[] = { 31151da12ec4SLe Tan DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0), 3116e6b6af05SRadim Krčmář DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim, 3117e6b6af05SRadim Krčmář ON_OFF_AUTO_AUTO), 3118fb506e70SRadim Krčmář DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false), 31194b49b586SPeter Xu DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits, 312037f51384SPrasad Singamsetty VTD_HOST_ADDRESS_WIDTH), 31213b40f0e5SAviv Ben-David DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), 31224a4f219eSYi Sun DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE), 3123b8ffd7d6SJason Wang DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false), 3124ccc23bb0SPeter Xu DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true), 3125*8646d9c7SDavid Woodhouse DEFINE_PROP_BOOL("dma-translation", IntelIOMMUState, dma_translation, true), 31261da12ec4SLe Tan DEFINE_PROP_END_OF_LIST(), 31271da12ec4SLe Tan }; 31281da12ec4SLe Tan 3129651e4cefSPeter Xu /* Read IRTE entry with specific index */ 3130651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index, 3131bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry *entry, uint16_t sid) 3132651e4cefSPeter Xu { 3133ede9c94aSPeter Xu static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \ 3134ede9c94aSPeter Xu {0xffff, 0xfffb, 0xfff9, 0xfff8}; 3135651e4cefSPeter Xu dma_addr_t addr = 0x00; 3136ede9c94aSPeter Xu uint16_t mask, source_id; 3137ede9c94aSPeter Xu uint8_t bus, bus_max, bus_min; 3138651e4cefSPeter Xu 31393c507c26SJan Kiszka if (index >= iommu->intr_size) { 31403c507c26SJan Kiszka error_report_once("%s: index too large: ind=0x%x", 31413c507c26SJan Kiszka __func__, index); 31423c507c26SJan Kiszka return -VTD_FR_IR_INDEX_OVER; 31433c507c26SJan Kiszka } 31443c507c26SJan Kiszka 3145651e4cefSPeter Xu addr = iommu->intr_root + index * sizeof(*entry); 3146ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, 3147ba06fe8aSPhilippe Mathieu-Daudé entry, sizeof(*entry), MEMTXATTRS_UNSPECIFIED)) { 31481376211fSPeter Xu error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64, 31491376211fSPeter Xu __func__, index, addr); 3150651e4cefSPeter Xu return -VTD_FR_IR_ROOT_INVAL; 3151651e4cefSPeter Xu } 3152651e4cefSPeter Xu 31537feb51b7SPeter Xu trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]), 31547feb51b7SPeter Xu le64_to_cpu(entry->data[0])); 31557feb51b7SPeter Xu 3156bc38ee10SMichael S. Tsirkin if (!entry->irte.present) { 31574e4abd11SPeter Xu error_report_once("%s: detected non-present IRTE " 31584e4abd11SPeter Xu "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")", 31594e4abd11SPeter Xu __func__, index, le64_to_cpu(entry->data[1]), 3160651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 3161651e4cefSPeter Xu return -VTD_FR_IR_ENTRY_P; 3162651e4cefSPeter Xu } 3163651e4cefSPeter Xu 3164bc38ee10SMichael S. Tsirkin if (entry->irte.__reserved_0 || entry->irte.__reserved_1 || 3165bc38ee10SMichael S. Tsirkin entry->irte.__reserved_2) { 31664e4abd11SPeter Xu error_report_once("%s: detected non-zero reserved IRTE " 31674e4abd11SPeter Xu "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")", 31684e4abd11SPeter Xu __func__, index, le64_to_cpu(entry->data[1]), 3169651e4cefSPeter Xu le64_to_cpu(entry->data[0])); 3170651e4cefSPeter Xu return -VTD_FR_IR_IRTE_RSVD; 3171651e4cefSPeter Xu } 3172651e4cefSPeter Xu 3173ede9c94aSPeter Xu if (sid != X86_IOMMU_SID_INVALID) { 3174ede9c94aSPeter Xu /* Validate IRTE SID */ 3175bc38ee10SMichael S. Tsirkin source_id = le32_to_cpu(entry->irte.source_id); 3176bc38ee10SMichael S. Tsirkin switch (entry->irte.sid_vtype) { 3177ede9c94aSPeter Xu case VTD_SVT_NONE: 3178ede9c94aSPeter Xu break; 3179ede9c94aSPeter Xu 3180ede9c94aSPeter Xu case VTD_SVT_ALL: 3181bc38ee10SMichael S. Tsirkin mask = vtd_svt_mask[entry->irte.sid_q]; 3182ede9c94aSPeter Xu if ((source_id & mask) != (sid & mask)) { 31834e4abd11SPeter Xu error_report_once("%s: invalid IRTE SID " 31844e4abd11SPeter Xu "(index=%u, sid=%u, source_id=%u)", 31854e4abd11SPeter Xu __func__, index, sid, source_id); 3186ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3187ede9c94aSPeter Xu } 3188ede9c94aSPeter Xu break; 3189ede9c94aSPeter Xu 3190ede9c94aSPeter Xu case VTD_SVT_BUS: 3191ede9c94aSPeter Xu bus_max = source_id >> 8; 3192ede9c94aSPeter Xu bus_min = source_id & 0xff; 3193ede9c94aSPeter Xu bus = sid >> 8; 3194ede9c94aSPeter Xu if (bus > bus_max || bus < bus_min) { 31954e4abd11SPeter Xu error_report_once("%s: invalid SVT_BUS " 31964e4abd11SPeter Xu "(index=%u, bus=%u, min=%u, max=%u)", 31974e4abd11SPeter Xu __func__, index, bus, bus_min, bus_max); 3198ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3199ede9c94aSPeter Xu } 3200ede9c94aSPeter Xu break; 3201ede9c94aSPeter Xu 3202ede9c94aSPeter Xu default: 32034e4abd11SPeter Xu error_report_once("%s: detected invalid IRTE SVT " 32044e4abd11SPeter Xu "(index=%u, type=%d)", __func__, 32054e4abd11SPeter Xu index, entry->irte.sid_vtype); 3206ede9c94aSPeter Xu /* Take this as verification failure. */ 3207ede9c94aSPeter Xu return -VTD_FR_IR_SID_ERR; 3208ede9c94aSPeter Xu } 3209ede9c94aSPeter Xu } 3210651e4cefSPeter Xu 3211651e4cefSPeter Xu return 0; 3212651e4cefSPeter Xu } 3213651e4cefSPeter Xu 3214651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */ 3215ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index, 321635c24501SSingh, Brijesh X86IOMMUIrq *irq, uint16_t sid) 3217651e4cefSPeter Xu { 3218bc38ee10SMichael S. Tsirkin VTD_IR_TableEntry irte = {}; 3219651e4cefSPeter Xu int ret = 0; 3220651e4cefSPeter Xu 3221ede9c94aSPeter Xu ret = vtd_irte_get(iommu, index, &irte, sid); 3222651e4cefSPeter Xu if (ret) { 3223651e4cefSPeter Xu return ret; 3224651e4cefSPeter Xu } 3225651e4cefSPeter Xu 3226bc38ee10SMichael S. Tsirkin irq->trigger_mode = irte.irte.trigger_mode; 3227bc38ee10SMichael S. Tsirkin irq->vector = irte.irte.vector; 3228bc38ee10SMichael S. Tsirkin irq->delivery_mode = irte.irte.delivery_mode; 3229bc38ee10SMichael S. Tsirkin irq->dest = le32_to_cpu(irte.irte.dest_id); 323028589311SJan Kiszka if (!iommu->intr_eime) { 3231651e4cefSPeter Xu #define VTD_IR_APIC_DEST_MASK (0xff00ULL) 3232651e4cefSPeter Xu #define VTD_IR_APIC_DEST_SHIFT (8) 323328589311SJan Kiszka irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >> 3234651e4cefSPeter Xu VTD_IR_APIC_DEST_SHIFT; 323528589311SJan Kiszka } 3236bc38ee10SMichael S. Tsirkin irq->dest_mode = irte.irte.dest_mode; 3237bc38ee10SMichael S. Tsirkin irq->redir_hint = irte.irte.redir_hint; 3238651e4cefSPeter Xu 32397feb51b7SPeter Xu trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector, 32407feb51b7SPeter Xu irq->delivery_mode, irq->dest, irq->dest_mode); 3241651e4cefSPeter Xu 3242651e4cefSPeter Xu return 0; 3243651e4cefSPeter Xu } 3244651e4cefSPeter Xu 3245651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */ 3246651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu, 3247651e4cefSPeter Xu MSIMessage *origin, 3248ede9c94aSPeter Xu MSIMessage *translated, 3249ede9c94aSPeter Xu uint16_t sid) 3250651e4cefSPeter Xu { 3251651e4cefSPeter Xu int ret = 0; 3252651e4cefSPeter Xu VTD_IR_MSIAddress addr; 3253651e4cefSPeter Xu uint16_t index; 325435c24501SSingh, Brijesh X86IOMMUIrq irq = {}; 3255651e4cefSPeter Xu 3256651e4cefSPeter Xu assert(origin && translated); 3257651e4cefSPeter Xu 32587feb51b7SPeter Xu trace_vtd_ir_remap_msi_req(origin->address, origin->data); 32597feb51b7SPeter Xu 3260651e4cefSPeter Xu if (!iommu || !iommu->intr_enabled) { 3261e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 3262e7a3b91fSPeter Xu goto out; 3263651e4cefSPeter Xu } 3264651e4cefSPeter Xu 3265651e4cefSPeter Xu if (origin->address & VTD_MSI_ADDR_HI_MASK) { 32661376211fSPeter Xu error_report_once("%s: MSI address high 32 bits non-zero detected: " 32671376211fSPeter Xu "address=0x%" PRIx64, __func__, origin->address); 3268651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3269651e4cefSPeter Xu } 3270651e4cefSPeter Xu 3271651e4cefSPeter Xu addr.data = origin->address & VTD_MSI_ADDR_LO_MASK; 32721a43713bSPeter Xu if (addr.addr.__head != 0xfee) { 32731376211fSPeter Xu error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32, 32741376211fSPeter Xu __func__, addr.data); 3275651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3276651e4cefSPeter Xu } 3277651e4cefSPeter Xu 3278651e4cefSPeter Xu /* This is compatible mode. */ 3279bc38ee10SMichael S. Tsirkin if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) { 3280e7a3b91fSPeter Xu memcpy(translated, origin, sizeof(*origin)); 3281e7a3b91fSPeter Xu goto out; 3282651e4cefSPeter Xu } 3283651e4cefSPeter Xu 3284bc38ee10SMichael S. Tsirkin index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l); 3285651e4cefSPeter Xu 3286651e4cefSPeter Xu #define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff) 3287651e4cefSPeter Xu #define VTD_IR_MSI_DATA_RESERVED (0xffff0000) 3288651e4cefSPeter Xu 3289bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 3290651e4cefSPeter Xu /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */ 3291651e4cefSPeter Xu index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE; 3292651e4cefSPeter Xu } 3293651e4cefSPeter Xu 3294ede9c94aSPeter Xu ret = vtd_remap_irq_get(iommu, index, &irq, sid); 3295651e4cefSPeter Xu if (ret) { 3296651e4cefSPeter Xu return ret; 3297651e4cefSPeter Xu } 3298651e4cefSPeter Xu 3299bc38ee10SMichael S. Tsirkin if (addr.addr.sub_valid) { 33007feb51b7SPeter Xu trace_vtd_ir_remap_type("MSI"); 3301651e4cefSPeter Xu if (origin->data & VTD_IR_MSI_DATA_RESERVED) { 33024e4abd11SPeter Xu error_report_once("%s: invalid IR MSI " 33034e4abd11SPeter Xu "(sid=%u, address=0x%" PRIx64 33044e4abd11SPeter Xu ", data=0x%" PRIx32 ")", 33054e4abd11SPeter Xu __func__, sid, origin->address, origin->data); 3306651e4cefSPeter Xu return -VTD_FR_IR_REQ_RSVD; 3307651e4cefSPeter Xu } 3308651e4cefSPeter Xu } else { 3309651e4cefSPeter Xu uint8_t vector = origin->data & 0xff; 3310dea651a9SFeng Wu uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1; 3311dea651a9SFeng Wu 33127feb51b7SPeter Xu trace_vtd_ir_remap_type("IOAPIC"); 3313651e4cefSPeter Xu /* IOAPIC entry vector should be aligned with IRTE vector 3314651e4cefSPeter Xu * (see vt-d spec 5.1.5.1). */ 3315651e4cefSPeter Xu if (vector != irq.vector) { 33167feb51b7SPeter Xu trace_vtd_warn_ir_vector(sid, index, vector, irq.vector); 3317651e4cefSPeter Xu } 3318dea651a9SFeng Wu 3319dea651a9SFeng Wu /* The Trigger Mode field must match the Trigger Mode in the IRTE. 3320dea651a9SFeng Wu * (see vt-d spec 5.1.5.1). */ 3321dea651a9SFeng Wu if (trigger_mode != irq.trigger_mode) { 33227feb51b7SPeter Xu trace_vtd_warn_ir_trigger(sid, index, trigger_mode, 33237feb51b7SPeter Xu irq.trigger_mode); 3324dea651a9SFeng Wu } 3325651e4cefSPeter Xu } 3326651e4cefSPeter Xu 3327651e4cefSPeter Xu /* 3328651e4cefSPeter Xu * We'd better keep the last two bits, assuming that guest OS 3329651e4cefSPeter Xu * might modify it. Keep it does not hurt after all. 3330651e4cefSPeter Xu */ 3331bc38ee10SMichael S. Tsirkin irq.msi_addr_last_bits = addr.addr.__not_care; 3332651e4cefSPeter Xu 333335c24501SSingh, Brijesh /* Translate X86IOMMUIrq to MSI message */ 333435c24501SSingh, Brijesh x86_iommu_irq_to_msi_message(&irq, translated); 3335651e4cefSPeter Xu 3336e7a3b91fSPeter Xu out: 33377feb51b7SPeter Xu trace_vtd_ir_remap_msi(origin->address, origin->data, 3338651e4cefSPeter Xu translated->address, translated->data); 3339651e4cefSPeter Xu return 0; 3340651e4cefSPeter Xu } 3341651e4cefSPeter Xu 33428b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src, 33438b5ed7dfSPeter Xu MSIMessage *dst, uint16_t sid) 33448b5ed7dfSPeter Xu { 3345ede9c94aSPeter Xu return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu), 3346ede9c94aSPeter Xu src, dst, sid); 33478b5ed7dfSPeter Xu } 33488b5ed7dfSPeter Xu 3349651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr, 3350651e4cefSPeter Xu uint64_t *data, unsigned size, 3351651e4cefSPeter Xu MemTxAttrs attrs) 3352651e4cefSPeter Xu { 3353651e4cefSPeter Xu return MEMTX_OK; 3354651e4cefSPeter Xu } 3355651e4cefSPeter Xu 3356651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr, 3357651e4cefSPeter Xu uint64_t value, unsigned size, 3358651e4cefSPeter Xu MemTxAttrs attrs) 3359651e4cefSPeter Xu { 3360651e4cefSPeter Xu int ret = 0; 336109cd058aSMichael S. Tsirkin MSIMessage from = {}, to = {}; 3362ede9c94aSPeter Xu uint16_t sid = X86_IOMMU_SID_INVALID; 3363651e4cefSPeter Xu 3364651e4cefSPeter Xu from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST; 3365651e4cefSPeter Xu from.data = (uint32_t) value; 3366651e4cefSPeter Xu 3367ede9c94aSPeter Xu if (!attrs.unspecified) { 3368ede9c94aSPeter Xu /* We have explicit Source ID */ 3369ede9c94aSPeter Xu sid = attrs.requester_id; 3370ede9c94aSPeter Xu } 3371ede9c94aSPeter Xu 3372ede9c94aSPeter Xu ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid); 3373651e4cefSPeter Xu if (ret) { 3374651e4cefSPeter Xu /* TODO: report error */ 3375651e4cefSPeter Xu /* Drop this interrupt */ 3376651e4cefSPeter Xu return MEMTX_ERROR; 3377651e4cefSPeter Xu } 3378651e4cefSPeter Xu 337932946019SRadim Krčmář apic_get_class()->send_msi(&to); 3380651e4cefSPeter Xu 3381651e4cefSPeter Xu return MEMTX_OK; 3382651e4cefSPeter Xu } 3383651e4cefSPeter Xu 3384651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = { 3385651e4cefSPeter Xu .read_with_attrs = vtd_mem_ir_read, 3386651e4cefSPeter Xu .write_with_attrs = vtd_mem_ir_write, 3387651e4cefSPeter Xu .endianness = DEVICE_LITTLE_ENDIAN, 3388651e4cefSPeter Xu .impl = { 3389651e4cefSPeter Xu .min_access_size = 4, 3390651e4cefSPeter Xu .max_access_size = 4, 3391651e4cefSPeter Xu }, 3392651e4cefSPeter Xu .valid = { 3393651e4cefSPeter Xu .min_access_size = 4, 3394651e4cefSPeter Xu .max_access_size = 4, 3395651e4cefSPeter Xu }, 3396651e4cefSPeter Xu }; 33977df953bdSKnut Omang 33987df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) 33997df953bdSKnut Omang { 34007df953bdSKnut Omang uintptr_t key = (uintptr_t)bus; 34017df953bdSKnut Omang VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key); 34027df953bdSKnut Omang VTDAddressSpace *vtd_dev_as; 3403e0a3c8ccSJason Wang char name[128]; 34047df953bdSKnut Omang 34057df953bdSKnut Omang if (!vtd_bus) { 34062d3fc581SJason Wang uintptr_t *new_key = g_malloc(sizeof(*new_key)); 34072d3fc581SJason Wang *new_key = (uintptr_t)bus; 34087df953bdSKnut Omang /* No corresponding free() */ 340904af0e18SPeter Xu vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \ 3410bf33cc75SPeter Xu PCI_DEVFN_MAX); 34117df953bdSKnut Omang vtd_bus->bus = bus; 34122d3fc581SJason Wang g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); 34137df953bdSKnut Omang } 34147df953bdSKnut Omang 34157df953bdSKnut Omang vtd_dev_as = vtd_bus->dev_as[devfn]; 34167df953bdSKnut Omang 34177df953bdSKnut Omang if (!vtd_dev_as) { 34184b519ef1SPeter Xu snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn), 34194b519ef1SPeter Xu PCI_FUNC(devfn)); 3420b21e2380SMarkus Armbruster vtd_bus->dev_as[devfn] = vtd_dev_as = g_new0(VTDAddressSpace, 1); 34217df953bdSKnut Omang 34227df953bdSKnut Omang vtd_dev_as->bus = bus; 34237df953bdSKnut Omang vtd_dev_as->devfn = (uint8_t)devfn; 34247df953bdSKnut Omang vtd_dev_as->iommu_state = s; 34257df953bdSKnut Omang vtd_dev_as->context_cache_entry.context_cache_gen = 0; 342663b88968SPeter Xu vtd_dev_as->iova_tree = iova_tree_new(); 3427558e0024SPeter Xu 34284b519ef1SPeter Xu memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX); 34294b519ef1SPeter Xu address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root"); 34304b519ef1SPeter Xu 3431558e0024SPeter Xu /* 34324b519ef1SPeter Xu * Build the DMAR-disabled container with aliases to the 34334b519ef1SPeter Xu * shared MRs. Note that aliasing to a shared memory region 34344b519ef1SPeter Xu * could help the memory API to detect same FlatViews so we 34354b519ef1SPeter Xu * can have devices to share the same FlatView when DMAR is 34364b519ef1SPeter Xu * disabled (either by not providing "intel_iommu=on" or with 34374b519ef1SPeter Xu * "iommu=pt"). It will greatly reduce the total number of 34384b519ef1SPeter Xu * FlatViews of the system hence VM runs faster. 3439558e0024SPeter Xu */ 34404b519ef1SPeter Xu memory_region_init_alias(&vtd_dev_as->nodmar, OBJECT(s), 34414b519ef1SPeter Xu "vtd-nodmar", &s->mr_nodmar, 0, 34424b519ef1SPeter Xu memory_region_size(&s->mr_nodmar)); 34434b519ef1SPeter Xu 34444b519ef1SPeter Xu /* 34454b519ef1SPeter Xu * Build the per-device DMAR-enabled container. 34464b519ef1SPeter Xu * 34474b519ef1SPeter Xu * TODO: currently we have per-device IOMMU memory region only 34484b519ef1SPeter Xu * because we have per-device IOMMU notifiers for devices. If 34494b519ef1SPeter Xu * one day we can abstract the IOMMU notifiers out of the 34504b519ef1SPeter Xu * memory regions then we can also share the same memory 34514b519ef1SPeter Xu * region here just like what we've done above with the nodmar 34524b519ef1SPeter Xu * region. 34534b519ef1SPeter Xu */ 34544b519ef1SPeter Xu strcat(name, "-dmar"); 34551221a474SAlexey Kardashevskiy memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu), 34561221a474SAlexey Kardashevskiy TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s), 34574b519ef1SPeter Xu name, UINT64_MAX); 34584b519ef1SPeter Xu memory_region_init_alias(&vtd_dev_as->iommu_ir, OBJECT(s), "vtd-ir", 34594b519ef1SPeter Xu &s->mr_ir, 0, memory_region_size(&s->mr_ir)); 34604b519ef1SPeter Xu memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->iommu), 3461558e0024SPeter Xu VTD_INTERRUPT_ADDR_FIRST, 34624b519ef1SPeter Xu &vtd_dev_as->iommu_ir, 1); 34634b519ef1SPeter Xu 34644b519ef1SPeter Xu /* 34654b519ef1SPeter Xu * Hook both the containers under the root container, we 34664b519ef1SPeter Xu * switch between DMAR & noDMAR by enable/disable 34674b519ef1SPeter Xu * corresponding sub-containers 34684b519ef1SPeter Xu */ 3469558e0024SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 34703df9d748SAlexey Kardashevskiy MEMORY_REGION(&vtd_dev_as->iommu), 34714b519ef1SPeter Xu 0); 34724b519ef1SPeter Xu memory_region_add_subregion_overlap(&vtd_dev_as->root, 0, 34734b519ef1SPeter Xu &vtd_dev_as->nodmar, 0); 34744b519ef1SPeter Xu 3475558e0024SPeter Xu vtd_switch_address_space(vtd_dev_as); 34767df953bdSKnut Omang } 34777df953bdSKnut Omang return vtd_dev_as; 34787df953bdSKnut Omang } 34797df953bdSKnut Omang 3480dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */ 3481dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) 3482dd4d607eSPeter Xu { 34839a4bb839SPeter Xu hwaddr size, remain; 3484dd4d607eSPeter Xu hwaddr start = n->start; 3485dd4d607eSPeter Xu hwaddr end = n->end; 348637f51384SPrasad Singamsetty IntelIOMMUState *s = as->iommu_state; 348763b88968SPeter Xu DMAMap map; 3488dd4d607eSPeter Xu 3489dd4d607eSPeter Xu /* 3490dd4d607eSPeter Xu * Note: all the codes in this function has a assumption that IOVA 3491dd4d607eSPeter Xu * bits are no more than VTD_MGAW bits (which is restricted by 3492dd4d607eSPeter Xu * VT-d spec), otherwise we need to consider overflow of 64 bits. 3493dd4d607eSPeter Xu */ 3494dd4d607eSPeter Xu 3495d6d10793SYan Zhao if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) { 3496dd4d607eSPeter Xu /* 3497dd4d607eSPeter Xu * Don't need to unmap regions that is bigger than the whole 3498dd4d607eSPeter Xu * VT-d supported address space size 3499dd4d607eSPeter Xu */ 3500d6d10793SYan Zhao end = VTD_ADDRESS_SIZE(s->aw_bits) - 1; 3501dd4d607eSPeter Xu } 3502dd4d607eSPeter Xu 3503dd4d607eSPeter Xu assert(start <= end); 35049a4bb839SPeter Xu size = remain = end - start + 1; 3505dd4d607eSPeter Xu 35069a4bb839SPeter Xu while (remain >= VTD_PAGE_SIZE) { 35075039caf3SEugenio Pérez IOMMUTLBEvent event; 3508f14fb6c2SEric Auger uint64_t mask = dma_aligned_pow2_mask(start, end, s->aw_bits); 3509f14fb6c2SEric Auger uint64_t size = mask + 1; 3510dd4d607eSPeter Xu 3511f14fb6c2SEric Auger assert(size); 35129a4bb839SPeter Xu 35135039caf3SEugenio Pérez event.type = IOMMU_NOTIFIER_UNMAP; 35145039caf3SEugenio Pérez event.entry.iova = start; 3515f14fb6c2SEric Auger event.entry.addr_mask = mask; 35165039caf3SEugenio Pérez event.entry.target_as = &address_space_memory; 35175039caf3SEugenio Pérez event.entry.perm = IOMMU_NONE; 3518dd4d607eSPeter Xu /* This field is meaningless for unmap */ 35195039caf3SEugenio Pérez event.entry.translated_addr = 0; 35209a4bb839SPeter Xu 35215039caf3SEugenio Pérez memory_region_notify_iommu_one(n, &event); 35229a4bb839SPeter Xu 3523f14fb6c2SEric Auger start += size; 3524f14fb6c2SEric Auger remain -= size; 35259a4bb839SPeter Xu } 35269a4bb839SPeter Xu 35279a4bb839SPeter Xu assert(!remain); 3528dd4d607eSPeter Xu 3529dd4d607eSPeter Xu trace_vtd_as_unmap_whole(pci_bus_num(as->bus), 3530dd4d607eSPeter Xu VTD_PCI_SLOT(as->devfn), 3531dd4d607eSPeter Xu VTD_PCI_FUNC(as->devfn), 35329a4bb839SPeter Xu n->start, size); 3533dd4d607eSPeter Xu 35349a4bb839SPeter Xu map.iova = n->start; 35359a4bb839SPeter Xu map.size = size; 353663b88968SPeter Xu iova_tree_remove(as->iova_tree, &map); 3537dd4d607eSPeter Xu } 3538dd4d607eSPeter Xu 3539dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s) 3540dd4d607eSPeter Xu { 3541dd4d607eSPeter Xu VTDAddressSpace *vtd_as; 3542dd4d607eSPeter Xu IOMMUNotifier *n; 3543dd4d607eSPeter Xu 3544b4a4ba0dSPeter Xu QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { 3545dd4d607eSPeter Xu IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) { 3546dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 3547dd4d607eSPeter Xu } 3548dd4d607eSPeter Xu } 3549dd4d607eSPeter Xu } 3550dd4d607eSPeter Xu 35512cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s) 35522cc9ddccSPeter Xu { 35532cc9ddccSPeter Xu vtd_address_space_unmap_all(s); 35542cc9ddccSPeter Xu vtd_switch_address_space_all(s); 35552cc9ddccSPeter Xu } 35562cc9ddccSPeter Xu 35575039caf3SEugenio Pérez static int vtd_replay_hook(IOMMUTLBEvent *event, void *private) 3558f06a696dSPeter Xu { 35595039caf3SEugenio Pérez memory_region_notify_iommu_one(private, event); 3560f06a696dSPeter Xu return 0; 3561f06a696dSPeter Xu } 3562f06a696dSPeter Xu 35633df9d748SAlexey Kardashevskiy static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) 3564f06a696dSPeter Xu { 35653df9d748SAlexey Kardashevskiy VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu); 3566f06a696dSPeter Xu IntelIOMMUState *s = vtd_as->iommu_state; 3567f06a696dSPeter Xu uint8_t bus_n = pci_bus_num(vtd_as->bus); 3568f06a696dSPeter Xu VTDContextEntry ce; 3569f06a696dSPeter Xu 3570f06a696dSPeter Xu /* 3571dd4d607eSPeter Xu * The replay can be triggered by either a invalidation or a newly 3572dd4d607eSPeter Xu * created entry. No matter what, we release existing mappings 3573dd4d607eSPeter Xu * (it means flushing caches for UNMAP-only registers). 3574f06a696dSPeter Xu */ 3575dd4d607eSPeter Xu vtd_address_space_unmap(vtd_as, n); 3576dd4d607eSPeter Xu 3577dd4d607eSPeter Xu if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) { 3578fb43cf73SLiu, Yi L trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : 3579fb43cf73SLiu, Yi L "legacy mode", 3580fb43cf73SLiu, Yi L bus_n, PCI_SLOT(vtd_as->devfn), 3581f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn), 3582fb43cf73SLiu, Yi L vtd_get_domain_id(s, &ce), 3583f06a696dSPeter Xu ce.hi, ce.lo); 35844f8a62a9SPeter Xu if (vtd_as_has_map_notifier(vtd_as)) { 35854f8a62a9SPeter Xu /* This is required only for MAP typed notifiers */ 3586fe215b0cSPeter Xu vtd_page_walk_info info = { 3587fe215b0cSPeter Xu .hook_fn = vtd_replay_hook, 3588fe215b0cSPeter Xu .private = (void *)n, 3589fe215b0cSPeter Xu .notify_unmap = false, 3590fe215b0cSPeter Xu .aw = s->aw_bits, 35912f764fa8SPeter Xu .as = vtd_as, 3592fb43cf73SLiu, Yi L .domain_id = vtd_get_domain_id(s, &ce), 3593fe215b0cSPeter Xu }; 3594fe215b0cSPeter Xu 3595fb43cf73SLiu, Yi L vtd_page_walk(s, &ce, 0, ~0ULL, &info); 35964f8a62a9SPeter Xu } 3597f06a696dSPeter Xu } else { 3598f06a696dSPeter Xu trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), 3599f06a696dSPeter Xu PCI_FUNC(vtd_as->devfn)); 3600f06a696dSPeter Xu } 3601f06a696dSPeter Xu 3602f06a696dSPeter Xu return; 3603f06a696dSPeter Xu } 3604f06a696dSPeter Xu 36051da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay 36061da12ec4SLe Tan * attention when adding new initialization stuff. 36071da12ec4SLe Tan */ 36081da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s) 36091da12ec4SLe Tan { 3610d54bd7f8SPeter Xu X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 3611d54bd7f8SPeter Xu 36121da12ec4SLe Tan memset(s->csr, 0, DMAR_REG_SIZE); 36131da12ec4SLe Tan memset(s->wmask, 0, DMAR_REG_SIZE); 36141da12ec4SLe Tan memset(s->w1cmask, 0, DMAR_REG_SIZE); 36151da12ec4SLe Tan memset(s->womask, 0, DMAR_REG_SIZE); 36161da12ec4SLe Tan 36171da12ec4SLe Tan s->root = 0; 3618fb43cf73SLiu, Yi L s->root_scalable = false; 36191da12ec4SLe Tan s->dmar_enabled = false; 3620d7bb469aSPeter Xu s->intr_enabled = false; 36211da12ec4SLe Tan s->iq_head = 0; 36221da12ec4SLe Tan s->iq_tail = 0; 36231da12ec4SLe Tan s->iq = 0; 36241da12ec4SLe Tan s->iq_size = 0; 36251da12ec4SLe Tan s->qi_enabled = false; 36261da12ec4SLe Tan s->iq_last_desc_type = VTD_INV_DESC_NONE; 3627c0c1d351SLiu, Yi L s->iq_dw = false; 36281da12ec4SLe Tan s->next_frcd_reg = 0; 362992e5d85eSPrasad Singamsetty s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | 363092e5d85eSPrasad Singamsetty VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS | 3631*8646d9c7SDavid Woodhouse VTD_CAP_MGAW(s->aw_bits); 3632ccc23bb0SPeter Xu if (s->dma_drain) { 3633ccc23bb0SPeter Xu s->cap |= VTD_CAP_DRAIN; 3634ccc23bb0SPeter Xu } 3635*8646d9c7SDavid Woodhouse if (s->dma_translation) { 3636*8646d9c7SDavid Woodhouse if (s->aw_bits >= VTD_HOST_AW_39BIT) { 3637*8646d9c7SDavid Woodhouse s->cap |= VTD_CAP_SAGAW_39bit; 3638*8646d9c7SDavid Woodhouse } 3639*8646d9c7SDavid Woodhouse if (s->aw_bits >= VTD_HOST_AW_48BIT) { 364037f51384SPrasad Singamsetty s->cap |= VTD_CAP_SAGAW_48bit; 364137f51384SPrasad Singamsetty } 3642*8646d9c7SDavid Woodhouse } 3643ed7b8fbcSLe Tan s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO; 36441da12ec4SLe Tan 364592e5d85eSPrasad Singamsetty /* 364692e5d85eSPrasad Singamsetty * Rsvd field masks for spte 364792e5d85eSPrasad Singamsetty */ 3648ce586f3bSQi, Yadong vtd_spte_rsvd[0] = ~0ULL; 3649e48929c7SQi, Yadong vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits, 3650e48929c7SQi, Yadong x86_iommu->dt_supported); 3651ce586f3bSQi, Yadong vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits); 3652ce586f3bSQi, Yadong vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits); 3653ce586f3bSQi, Yadong vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits); 3654ce586f3bSQi, Yadong 3655e48929c7SQi, Yadong vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits, 3656e48929c7SQi, Yadong x86_iommu->dt_supported); 3657e48929c7SQi, Yadong vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits, 3658e48929c7SQi, Yadong x86_iommu->dt_supported); 365992e5d85eSPrasad Singamsetty 3660b8ffd7d6SJason Wang if (s->scalable_mode || s->snoop_control) { 36610192d667SJason Wang vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP; 36620192d667SJason Wang vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP; 36630192d667SJason Wang vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP; 36640192d667SJason Wang } 36650192d667SJason Wang 3666a924b3d8SPeter Xu if (x86_iommu_ir_supported(x86_iommu)) { 3667e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV; 3668e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON) { 3669e6b6af05SRadim Krčmář s->ecap |= VTD_ECAP_EIM; 3670e6b6af05SRadim Krčmář } 3671e6b6af05SRadim Krčmář assert(s->intr_eim != ON_OFF_AUTO_AUTO); 3672d54bd7f8SPeter Xu } 3673d54bd7f8SPeter Xu 3674554f5e16SJason Wang if (x86_iommu->dt_supported) { 3675554f5e16SJason Wang s->ecap |= VTD_ECAP_DT; 3676554f5e16SJason Wang } 3677554f5e16SJason Wang 3678dbaabb25SPeter Xu if (x86_iommu->pt_supported) { 3679dbaabb25SPeter Xu s->ecap |= VTD_ECAP_PT; 3680dbaabb25SPeter Xu } 3681dbaabb25SPeter Xu 36823b40f0e5SAviv Ben-David if (s->caching_mode) { 36833b40f0e5SAviv Ben-David s->cap |= VTD_CAP_CM; 36843b40f0e5SAviv Ben-David } 36853b40f0e5SAviv Ben-David 36864a4f219eSYi Sun /* TODO: read cap/ecap from host to decide which cap to be exposed. */ 36874a4f219eSYi Sun if (s->scalable_mode) { 36884a4f219eSYi Sun s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; 36894a4f219eSYi Sun } 36904a4f219eSYi Sun 3691b8ffd7d6SJason Wang if (s->snoop_control) { 3692b8ffd7d6SJason Wang s->ecap |= VTD_ECAP_SC; 3693b8ffd7d6SJason Wang } 3694b8ffd7d6SJason Wang 369506aba4caSPeter Xu vtd_reset_caches(s); 3696d92fa2dcSLe Tan 36971da12ec4SLe Tan /* Define registers with default values and bit semantics */ 36981da12ec4SLe Tan vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0); 36991da12ec4SLe Tan vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0); 37001da12ec4SLe Tan vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0); 37011da12ec4SLe Tan vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0); 37021da12ec4SLe Tan vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL); 37031da12ec4SLe Tan vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0); 3704fb43cf73SLiu, Yi L vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0); 37051da12ec4SLe Tan vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0); 37061da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL); 37071da12ec4SLe Tan 37081da12ec4SLe Tan /* Advanced Fault Logging not supported */ 37091da12ec4SLe Tan vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL); 37101da12ec4SLe Tan vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0); 37111da12ec4SLe Tan vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0); 37121da12ec4SLe Tan vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0); 37131da12ec4SLe Tan 37141da12ec4SLe Tan /* Treated as RsvdZ when EIM in ECAP_REG is not supported 37151da12ec4SLe Tan * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0); 37161da12ec4SLe Tan */ 37171da12ec4SLe Tan vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0); 37181da12ec4SLe Tan 37191da12ec4SLe Tan /* Treated as RO for implementations that PLMR and PHMR fields reported 37201da12ec4SLe Tan * as Clear in the CAP_REG. 37211da12ec4SLe Tan * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0); 37221da12ec4SLe Tan */ 37231da12ec4SLe Tan vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0); 37241da12ec4SLe Tan 3725ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0); 3726ed7b8fbcSLe Tan vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0); 3727c0c1d351SLiu, Yi L vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff807ULL, 0); 3728ed7b8fbcSLe Tan vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL); 3729ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0); 3730ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0); 3731ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0); 3732ed7b8fbcSLe Tan /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */ 3733ed7b8fbcSLe Tan vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0); 3734ed7b8fbcSLe Tan 37351da12ec4SLe Tan /* IOTLB registers */ 37361da12ec4SLe Tan vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0); 37371da12ec4SLe Tan vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0); 37381da12ec4SLe Tan vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL); 37391da12ec4SLe Tan 37401da12ec4SLe Tan /* Fault Recording Registers, 128-bit */ 37411da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0); 37421da12ec4SLe Tan vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL); 3743a5861439SPeter Xu 3744a5861439SPeter Xu /* 374528589311SJan Kiszka * Interrupt remapping registers. 3746a5861439SPeter Xu */ 374728589311SJan Kiszka vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0); 37481da12ec4SLe Tan } 37491da12ec4SLe Tan 37501da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use 37511da12ec4SLe Tan * the address space they got at first (won't ask the bus again). 37521da12ec4SLe Tan */ 37531da12ec4SLe Tan static void vtd_reset(DeviceState *dev) 37541da12ec4SLe Tan { 37551da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 37561da12ec4SLe Tan 37571da12ec4SLe Tan vtd_init(s); 37582cc9ddccSPeter Xu vtd_address_space_refresh_all(s); 37591da12ec4SLe Tan } 37601da12ec4SLe Tan 3761621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) 3762621d983aSMarcel Apfelbaum { 3763621d983aSMarcel Apfelbaum IntelIOMMUState *s = opaque; 3764621d983aSMarcel Apfelbaum VTDAddressSpace *vtd_as; 3765621d983aSMarcel Apfelbaum 3766bf33cc75SPeter Xu assert(0 <= devfn && devfn < PCI_DEVFN_MAX); 3767621d983aSMarcel Apfelbaum 3768621d983aSMarcel Apfelbaum vtd_as = vtd_find_add_as(s, bus, devfn); 3769621d983aSMarcel Apfelbaum return &vtd_as->as; 3770621d983aSMarcel Apfelbaum } 3771621d983aSMarcel Apfelbaum 3772e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) 37736333e93cSRadim Krčmář { 3774e6b6af05SRadim Krčmář X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s); 3775e6b6af05SRadim Krčmář 3776a924b3d8SPeter Xu if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) { 3777e6b6af05SRadim Krčmář error_setg(errp, "eim=on cannot be selected without intremap=on"); 3778e6b6af05SRadim Krčmář return false; 3779e6b6af05SRadim Krčmář } 3780e6b6af05SRadim Krčmář 3781e6b6af05SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_AUTO) { 3782fb506e70SRadim Krčmář s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim) 3783a924b3d8SPeter Xu && x86_iommu_ir_supported(x86_iommu) ? 3784e6b6af05SRadim Krčmář ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 3785e6b6af05SRadim Krčmář } 3786fb506e70SRadim Krčmář if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) { 3787fb506e70SRadim Krčmář if (!kvm_irqchip_in_kernel()) { 3788fb506e70SRadim Krčmář error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split"); 3789fb506e70SRadim Krčmář return false; 3790fb506e70SRadim Krčmář } 3791fb506e70SRadim Krčmář if (!kvm_enable_x2apic()) { 3792fb506e70SRadim Krčmář error_setg(errp, "eim=on requires support on the KVM side" 3793fb506e70SRadim Krčmář "(X2APIC_API, first shipped in v4.7)"); 3794fb506e70SRadim Krčmář return false; 3795fb506e70SRadim Krčmář } 3796fb506e70SRadim Krčmář } 3797e6b6af05SRadim Krčmář 379837f51384SPrasad Singamsetty /* Currently only address widths supported are 39 and 48 bits */ 379937f51384SPrasad Singamsetty if ((s->aw_bits != VTD_HOST_AW_39BIT) && 380037f51384SPrasad Singamsetty (s->aw_bits != VTD_HOST_AW_48BIT)) { 38012a345149SMenno Lageman error_setg(errp, "Supported values for aw-bits are: %d, %d", 380237f51384SPrasad Singamsetty VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT); 380337f51384SPrasad Singamsetty return false; 380437f51384SPrasad Singamsetty } 380537f51384SPrasad Singamsetty 38064a4f219eSYi Sun if (s->scalable_mode && !s->dma_drain) { 38074a4f219eSYi Sun error_setg(errp, "Need to set dma_drain for scalable mode"); 38084a4f219eSYi Sun return false; 38094a4f219eSYi Sun } 38104a4f219eSYi Sun 38116333e93cSRadim Krčmář return true; 38126333e93cSRadim Krčmář } 38136333e93cSRadim Krčmář 381428cf553aSPeter Xu static int vtd_machine_done_notify_one(Object *child, void *unused) 381528cf553aSPeter Xu { 381628cf553aSPeter Xu IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default()); 381728cf553aSPeter Xu 381828cf553aSPeter Xu /* 381928cf553aSPeter Xu * We hard-coded here because vfio-pci is the only special case 382028cf553aSPeter Xu * here. Let's be more elegant in the future when we can, but so 382128cf553aSPeter Xu * far there seems to be no better way. 382228cf553aSPeter Xu */ 382328cf553aSPeter Xu if (object_dynamic_cast(child, "vfio-pci") && !iommu->caching_mode) { 382428cf553aSPeter Xu vtd_panic_require_caching_mode(); 382528cf553aSPeter Xu } 382628cf553aSPeter Xu 382728cf553aSPeter Xu return 0; 382828cf553aSPeter Xu } 382928cf553aSPeter Xu 383028cf553aSPeter Xu static void vtd_machine_done_hook(Notifier *notifier, void *unused) 383128cf553aSPeter Xu { 383228cf553aSPeter Xu object_child_foreach_recursive(object_get_root(), 383328cf553aSPeter Xu vtd_machine_done_notify_one, NULL); 383428cf553aSPeter Xu } 383528cf553aSPeter Xu 383628cf553aSPeter Xu static Notifier vtd_machine_done_notify = { 383728cf553aSPeter Xu .notify = vtd_machine_done_hook, 383828cf553aSPeter Xu }; 383928cf553aSPeter Xu 38401da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp) 38411da12ec4SLe Tan { 3842ef0e8fc7SEduardo Habkost MachineState *ms = MACHINE(qdev_get_machine()); 384329396ed9SMohammed Gamal PCMachineState *pcms = PC_MACHINE(ms); 3844f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(ms); 384529396ed9SMohammed Gamal PCIBus *bus = pcms->bus; 38461da12ec4SLe Tan IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev); 38476333e93cSRadim Krčmář 3848e6b6af05SRadim Krčmář if (!vtd_decide_config(s, errp)) { 38496333e93cSRadim Krčmář return; 38506333e93cSRadim Krčmář } 38516333e93cSRadim Krčmář 3852b4a4ba0dSPeter Xu QLIST_INIT(&s->vtd_as_with_notifiers); 38531d9efa73SPeter Xu qemu_mutex_init(&s->iommu_lock); 38547df953bdSKnut Omang memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num)); 38551da12ec4SLe Tan memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s, 38561da12ec4SLe Tan "intel_iommu", DMAR_REG_SIZE); 38574b519ef1SPeter Xu 38584b519ef1SPeter Xu /* Create the shared memory regions by all devices */ 38594b519ef1SPeter Xu memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar", 38604b519ef1SPeter Xu UINT64_MAX); 38614b519ef1SPeter Xu memory_region_init_io(&s->mr_ir, OBJECT(s), &vtd_mem_ir_ops, 38624b519ef1SPeter Xu s, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE); 38634b519ef1SPeter Xu memory_region_init_alias(&s->mr_sys_alias, OBJECT(s), 38644b519ef1SPeter Xu "vtd-sys-alias", get_system_memory(), 0, 38654b519ef1SPeter Xu memory_region_size(get_system_memory())); 38664b519ef1SPeter Xu memory_region_add_subregion_overlap(&s->mr_nodmar, 0, 38674b519ef1SPeter Xu &s->mr_sys_alias, 0); 38684b519ef1SPeter Xu memory_region_add_subregion_overlap(&s->mr_nodmar, 38694b519ef1SPeter Xu VTD_INTERRUPT_ADDR_FIRST, 38704b519ef1SPeter Xu &s->mr_ir, 1); 38714b519ef1SPeter Xu 38721da12ec4SLe Tan sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem); 3873b5a280c0SLe Tan /* No corresponding destroy */ 3874b5a280c0SLe Tan s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 3875b5a280c0SLe Tan g_free, g_free); 38767df953bdSKnut Omang s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, 38777df953bdSKnut Omang g_free, g_free); 38781da12ec4SLe Tan vtd_init(s); 3879621d983aSMarcel Apfelbaum sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); 3880621d983aSMarcel Apfelbaum pci_setup_iommu(bus, vtd_host_dma_iommu, dev); 3881cb135f59SPeter Xu /* Pseudo address space under root PCI bus. */ 3882f0bb276bSPaolo Bonzini x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC); 388328cf553aSPeter Xu qemu_add_machine_init_done_notifier(&vtd_machine_done_notify); 38841da12ec4SLe Tan } 38851da12ec4SLe Tan 38861da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data) 38871da12ec4SLe Tan { 38881da12ec4SLe Tan DeviceClass *dc = DEVICE_CLASS(klass); 388930c60f77SEduardo Habkost X86IOMMUClass *x86_class = X86_IOMMU_DEVICE_CLASS(klass); 38901da12ec4SLe Tan 38911da12ec4SLe Tan dc->reset = vtd_reset; 38921da12ec4SLe Tan dc->vmsd = &vtd_vmstate; 38934f67d30bSMarc-André Lureau device_class_set_props(dc, vtd_properties); 3894621d983aSMarcel Apfelbaum dc->hotpluggable = false; 38951c7955c4SPeter Xu x86_class->realize = vtd_realize; 38968b5ed7dfSPeter Xu x86_class->int_remap = vtd_int_remap; 38978ab5700cSEduardo Habkost /* Supported by the pc-q35-* machine types */ 3898e4f4fb1eSEduardo Habkost dc->user_creatable = true; 38991ec202c9SErnest Esene set_bit(DEVICE_CATEGORY_MISC, dc->categories); 39001ec202c9SErnest Esene dc->desc = "Intel IOMMU (VT-d) DMA Remapping device"; 39011da12ec4SLe Tan } 39021da12ec4SLe Tan 39031da12ec4SLe Tan static const TypeInfo vtd_info = { 39041da12ec4SLe Tan .name = TYPE_INTEL_IOMMU_DEVICE, 39051c7955c4SPeter Xu .parent = TYPE_X86_IOMMU_DEVICE, 39061da12ec4SLe Tan .instance_size = sizeof(IntelIOMMUState), 39071da12ec4SLe Tan .class_init = vtd_class_init, 39081da12ec4SLe Tan }; 39091da12ec4SLe Tan 39101221a474SAlexey Kardashevskiy static void vtd_iommu_memory_region_class_init(ObjectClass *klass, 39111221a474SAlexey Kardashevskiy void *data) 39121221a474SAlexey Kardashevskiy { 39131221a474SAlexey Kardashevskiy IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 39141221a474SAlexey Kardashevskiy 39151221a474SAlexey Kardashevskiy imrc->translate = vtd_iommu_translate; 39161221a474SAlexey Kardashevskiy imrc->notify_flag_changed = vtd_iommu_notify_flag_changed; 39171221a474SAlexey Kardashevskiy imrc->replay = vtd_iommu_replay; 39181221a474SAlexey Kardashevskiy } 39191221a474SAlexey Kardashevskiy 39201221a474SAlexey Kardashevskiy static const TypeInfo vtd_iommu_memory_region_info = { 39211221a474SAlexey Kardashevskiy .parent = TYPE_IOMMU_MEMORY_REGION, 39221221a474SAlexey Kardashevskiy .name = TYPE_INTEL_IOMMU_MEMORY_REGION, 39231221a474SAlexey Kardashevskiy .class_init = vtd_iommu_memory_region_class_init, 39241221a474SAlexey Kardashevskiy }; 39251221a474SAlexey Kardashevskiy 39261da12ec4SLe Tan static void vtd_register_types(void) 39271da12ec4SLe Tan { 39281da12ec4SLe Tan type_register_static(&vtd_info); 39291221a474SAlexey Kardashevskiy type_register_static(&vtd_iommu_memory_region_info); 39301da12ec4SLe Tan } 39311da12ec4SLe Tan 39321da12ec4SLe Tan type_init(vtd_register_types) 3933