xref: /openbmc/qemu/hw/i386/intel_iommu.c (revision ce735ff03349eeac9efe59c118d78f088a151ec4)
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 
53da8d439cSJason Wang /*
54da8d439cSJason Wang  * PCI bus number (or SID) is not reliable since the device is usaully
55da8d439cSJason Wang  * initalized before guest can configure the PCI bridge
56da8d439cSJason Wang  * (SECONDARY_BUS_NUMBER).
57da8d439cSJason Wang  */
58da8d439cSJason Wang struct vtd_as_key {
59da8d439cSJason Wang     PCIBus *bus;
60da8d439cSJason Wang     uint8_t devfn;
611b2b1237SJason Wang     uint32_t pasid;
621b2b1237SJason Wang };
631b2b1237SJason Wang 
641b2b1237SJason Wang struct vtd_iotlb_key {
651b2b1237SJason Wang     uint64_t gfn;
661b2b1237SJason Wang     uint32_t pasid;
671b2b1237SJason Wang     uint16_t sid;
68ec1a78ceSJason Wang     uint8_t level;
69da8d439cSJason Wang };
70da8d439cSJason Wang 
712cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s);
72c28b535dSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
732cc9ddccSPeter Xu 
7428cf553aSPeter Xu static void vtd_panic_require_caching_mode(void)
7528cf553aSPeter Xu {
7628cf553aSPeter Xu     error_report("We need to set caching-mode=on for intel-iommu to enable "
7728cf553aSPeter Xu                  "device assignment with IOMMU protection.");
7828cf553aSPeter Xu     exit(1);
7928cf553aSPeter Xu }
8028cf553aSPeter Xu 
811da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
821da12ec4SLe Tan                             uint64_t wmask, uint64_t w1cmask)
831da12ec4SLe Tan {
841da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
851da12ec4SLe Tan     stq_le_p(&s->wmask[addr], wmask);
861da12ec4SLe Tan     stq_le_p(&s->w1cmask[addr], w1cmask);
871da12ec4SLe Tan }
881da12ec4SLe Tan 
891da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
901da12ec4SLe Tan {
911da12ec4SLe Tan     stq_le_p(&s->womask[addr], mask);
921da12ec4SLe Tan }
931da12ec4SLe Tan 
941da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
951da12ec4SLe Tan                             uint32_t wmask, uint32_t w1cmask)
961da12ec4SLe Tan {
971da12ec4SLe Tan     stl_le_p(&s->csr[addr], val);
981da12ec4SLe Tan     stl_le_p(&s->wmask[addr], wmask);
991da12ec4SLe Tan     stl_le_p(&s->w1cmask[addr], w1cmask);
1001da12ec4SLe Tan }
1011da12ec4SLe Tan 
1021da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
1031da12ec4SLe Tan {
1041da12ec4SLe Tan     stl_le_p(&s->womask[addr], mask);
1051da12ec4SLe Tan }
1061da12ec4SLe Tan 
1071da12ec4SLe Tan /* "External" get/set operations */
1081da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
1091da12ec4SLe Tan {
1101da12ec4SLe Tan     uint64_t oldval = ldq_le_p(&s->csr[addr]);
1111da12ec4SLe Tan     uint64_t wmask = ldq_le_p(&s->wmask[addr]);
1121da12ec4SLe Tan     uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
1131da12ec4SLe Tan     stq_le_p(&s->csr[addr],
1141da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
1151da12ec4SLe Tan }
1161da12ec4SLe Tan 
1171da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
1181da12ec4SLe Tan {
1191da12ec4SLe Tan     uint32_t oldval = ldl_le_p(&s->csr[addr]);
1201da12ec4SLe Tan     uint32_t wmask = ldl_le_p(&s->wmask[addr]);
1211da12ec4SLe Tan     uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
1221da12ec4SLe Tan     stl_le_p(&s->csr[addr],
1231da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
1241da12ec4SLe Tan }
1251da12ec4SLe Tan 
1261da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
1271da12ec4SLe Tan {
1281da12ec4SLe Tan     uint64_t val = ldq_le_p(&s->csr[addr]);
1291da12ec4SLe Tan     uint64_t womask = ldq_le_p(&s->womask[addr]);
1301da12ec4SLe Tan     return val & ~womask;
1311da12ec4SLe Tan }
1321da12ec4SLe Tan 
1331da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
1341da12ec4SLe Tan {
1351da12ec4SLe Tan     uint32_t val = ldl_le_p(&s->csr[addr]);
1361da12ec4SLe Tan     uint32_t womask = ldl_le_p(&s->womask[addr]);
1371da12ec4SLe Tan     return val & ~womask;
1381da12ec4SLe Tan }
1391da12ec4SLe Tan 
1401da12ec4SLe Tan /* "Internal" get/set operations */
1411da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
1421da12ec4SLe Tan {
1431da12ec4SLe Tan     return ldq_le_p(&s->csr[addr]);
1441da12ec4SLe Tan }
1451da12ec4SLe Tan 
1461da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
1471da12ec4SLe Tan {
1481da12ec4SLe Tan     return ldl_le_p(&s->csr[addr]);
1491da12ec4SLe Tan }
1501da12ec4SLe Tan 
1511da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
1521da12ec4SLe Tan {
1531da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
1541da12ec4SLe Tan }
1551da12ec4SLe Tan 
1561da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
1571da12ec4SLe Tan                                         uint32_t clear, uint32_t mask)
1581da12ec4SLe Tan {
1591da12ec4SLe Tan     uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
1601da12ec4SLe Tan     stl_le_p(&s->csr[addr], new_val);
1611da12ec4SLe Tan     return new_val;
1621da12ec4SLe Tan }
1631da12ec4SLe Tan 
1641da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
1651da12ec4SLe Tan                                         uint64_t clear, uint64_t mask)
1661da12ec4SLe Tan {
1671da12ec4SLe Tan     uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
1681da12ec4SLe Tan     stq_le_p(&s->csr[addr], new_val);
1691da12ec4SLe Tan     return new_val;
1701da12ec4SLe Tan }
1711da12ec4SLe Tan 
1721d9efa73SPeter Xu static inline void vtd_iommu_lock(IntelIOMMUState *s)
1731d9efa73SPeter Xu {
1741d9efa73SPeter Xu     qemu_mutex_lock(&s->iommu_lock);
1751d9efa73SPeter Xu }
1761d9efa73SPeter Xu 
1771d9efa73SPeter Xu static inline void vtd_iommu_unlock(IntelIOMMUState *s)
1781d9efa73SPeter Xu {
1791d9efa73SPeter Xu     qemu_mutex_unlock(&s->iommu_lock);
1801d9efa73SPeter Xu }
1811d9efa73SPeter Xu 
1822811af3bSPeter Xu static void vtd_update_scalable_state(IntelIOMMUState *s)
1832811af3bSPeter Xu {
1842811af3bSPeter Xu     uint64_t val = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
1852811af3bSPeter Xu 
1862811af3bSPeter Xu     if (s->scalable_mode) {
1872811af3bSPeter Xu         s->root_scalable = val & VTD_RTADDR_SMT;
1882811af3bSPeter Xu     }
1892811af3bSPeter Xu }
1902811af3bSPeter Xu 
191147a372eSJason Wang static void vtd_update_iq_dw(IntelIOMMUState *s)
192147a372eSJason Wang {
193147a372eSJason Wang     uint64_t val = vtd_get_quad_raw(s, DMAR_IQA_REG);
194147a372eSJason Wang 
195147a372eSJason Wang     if (s->ecap & VTD_ECAP_SMTS &&
196147a372eSJason Wang         val & VTD_IQA_DW_MASK) {
197147a372eSJason Wang         s->iq_dw = true;
198147a372eSJason Wang     } else {
199147a372eSJason Wang         s->iq_dw = false;
200147a372eSJason Wang     }
201147a372eSJason Wang }
202147a372eSJason Wang 
2034f8a62a9SPeter Xu /* Whether the address space needs to notify new mappings */
2044f8a62a9SPeter Xu static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
2054f8a62a9SPeter Xu {
2064f8a62a9SPeter Xu     return as->notifier_flags & IOMMU_NOTIFIER_MAP;
2074f8a62a9SPeter Xu }
2084f8a62a9SPeter Xu 
209b5a280c0SLe Tan /* GHashTable functions */
2101b2b1237SJason Wang static gboolean vtd_iotlb_equal(gconstpointer v1, gconstpointer v2)
211b5a280c0SLe Tan {
2121b2b1237SJason Wang     const struct vtd_iotlb_key *key1 = v1;
2131b2b1237SJason Wang     const struct vtd_iotlb_key *key2 = v2;
2141b2b1237SJason Wang 
2151b2b1237SJason Wang     return key1->sid == key2->sid &&
2161b2b1237SJason Wang            key1->pasid == key2->pasid &&
2171b2b1237SJason Wang            key1->level == key2->level &&
2181b2b1237SJason Wang            key1->gfn == key2->gfn;
219b5a280c0SLe Tan }
220b5a280c0SLe Tan 
2211b2b1237SJason Wang static guint vtd_iotlb_hash(gconstpointer v)
222b5a280c0SLe Tan {
2231b2b1237SJason Wang     const struct vtd_iotlb_key *key = v;
224ec1a78ceSJason Wang     uint64_t hash64 = key->gfn | ((uint64_t)(key->sid) << VTD_IOTLB_SID_SHIFT) |
225ec1a78ceSJason Wang         (uint64_t)(key->level - 1) << VTD_IOTLB_LVL_SHIFT |
226ec1a78ceSJason Wang         (uint64_t)(key->pasid) << VTD_IOTLB_PASID_SHIFT;
2271b2b1237SJason Wang 
228ec1a78ceSJason Wang     return (guint)((hash64 >> 32) ^ (hash64 & 0xffffffffU));
229b5a280c0SLe Tan }
230b5a280c0SLe Tan 
231da8d439cSJason Wang static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2)
232da8d439cSJason Wang {
233da8d439cSJason Wang     const struct vtd_as_key *key1 = v1;
234da8d439cSJason Wang     const struct vtd_as_key *key2 = v2;
235da8d439cSJason Wang 
2361b2b1237SJason Wang     return (key1->bus == key2->bus) && (key1->devfn == key2->devfn) &&
2371b2b1237SJason Wang            (key1->pasid == key2->pasid);
238da8d439cSJason Wang }
239da8d439cSJason Wang 
240da8d439cSJason Wang /*
241da8d439cSJason Wang  * Note that we use pointer to PCIBus as the key, so hashing/shifting
242da8d439cSJason Wang  * based on the pointer value is intended. Note that we deal with
243da8d439cSJason Wang  * collisions through vtd_as_equal().
244da8d439cSJason Wang  */
245da8d439cSJason Wang static guint vtd_as_hash(gconstpointer v)
246da8d439cSJason Wang {
247da8d439cSJason Wang     const struct vtd_as_key *key = v;
248da8d439cSJason Wang     guint value = (guint)(uintptr_t)key->bus;
249da8d439cSJason Wang 
250da8d439cSJason Wang     return (guint)(value << 8 | key->devfn);
251da8d439cSJason Wang }
252da8d439cSJason Wang 
253b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
254b5a280c0SLe Tan                                           gpointer user_data)
255b5a280c0SLe Tan {
256b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
257b5a280c0SLe Tan     uint16_t domain_id = *(uint16_t *)user_data;
258b5a280c0SLe Tan     return entry->domain_id == domain_id;
259b5a280c0SLe Tan }
260b5a280c0SLe Tan 
261d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */
262d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level)
263d66b969bSJason Wang {
2647e58326aSPeter Xu     assert(level != 0);
265d66b969bSJason Wang     return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
266d66b969bSJason Wang }
267d66b969bSJason Wang 
268d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
269d66b969bSJason Wang {
270d66b969bSJason Wang     return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
271d66b969bSJason Wang }
272d66b969bSJason Wang 
273b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
274b5a280c0SLe Tan                                         gpointer user_data)
275b5a280c0SLe Tan {
276b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
277b5a280c0SLe Tan     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
278d66b969bSJason Wang     uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
279d66b969bSJason Wang     uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
280b5a280c0SLe Tan     return (entry->domain_id == info->domain_id) &&
281d66b969bSJason Wang             (((entry->gfn & info->mask) == gfn) ||
282d66b969bSJason Wang              (entry->gfn == gfn_tlb));
283b5a280c0SLe Tan }
284b5a280c0SLe Tan 
285d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of
2861d9efa73SPeter Xu  * IntelIOMMUState to 1.  Must be called with IOMMU lock held.
287d92fa2dcSLe Tan  */
2881d9efa73SPeter Xu static void vtd_reset_context_cache_locked(IntelIOMMUState *s)
289d92fa2dcSLe Tan {
290d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
291da8d439cSJason Wang     GHashTableIter as_it;
292d92fa2dcSLe Tan 
2937feb51b7SPeter Xu     trace_vtd_context_cache_reset();
2947feb51b7SPeter Xu 
295da8d439cSJason Wang     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
2967df953bdSKnut Omang 
297da8d439cSJason Wang     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
298d92fa2dcSLe Tan         vtd_as->context_cache_entry.context_cache_gen = 0;
299d92fa2dcSLe Tan     }
300d92fa2dcSLe Tan     s->context_cache_gen = 1;
301d92fa2dcSLe Tan }
302d92fa2dcSLe Tan 
3031d9efa73SPeter Xu /* Must be called with IOMMU lock held. */
3041d9efa73SPeter Xu static void vtd_reset_iotlb_locked(IntelIOMMUState *s)
305b5a280c0SLe Tan {
306b5a280c0SLe Tan     assert(s->iotlb);
307b5a280c0SLe Tan     g_hash_table_remove_all(s->iotlb);
308b5a280c0SLe Tan }
309b5a280c0SLe Tan 
3101d9efa73SPeter Xu static void vtd_reset_iotlb(IntelIOMMUState *s)
3111d9efa73SPeter Xu {
3121d9efa73SPeter Xu     vtd_iommu_lock(s);
3131d9efa73SPeter Xu     vtd_reset_iotlb_locked(s);
3141d9efa73SPeter Xu     vtd_iommu_unlock(s);
3151d9efa73SPeter Xu }
3161d9efa73SPeter Xu 
31706aba4caSPeter Xu static void vtd_reset_caches(IntelIOMMUState *s)
31806aba4caSPeter Xu {
31906aba4caSPeter Xu     vtd_iommu_lock(s);
32006aba4caSPeter Xu     vtd_reset_iotlb_locked(s);
32106aba4caSPeter Xu     vtd_reset_context_cache_locked(s);
32206aba4caSPeter Xu     vtd_iommu_unlock(s);
32306aba4caSPeter Xu }
32406aba4caSPeter Xu 
325d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
326d66b969bSJason Wang {
327d66b969bSJason Wang     return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
328d66b969bSJason Wang }
329d66b969bSJason Wang 
3301d9efa73SPeter Xu /* Must be called with IOMMU lock held */
331b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
3321b2b1237SJason Wang                                        uint32_t pasid, hwaddr addr)
333b5a280c0SLe Tan {
3341b2b1237SJason Wang     struct vtd_iotlb_key key;
335d66b969bSJason Wang     VTDIOTLBEntry *entry;
336d66b969bSJason Wang     int level;
337b5a280c0SLe Tan 
338d66b969bSJason Wang     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
3391b2b1237SJason Wang         key.gfn = vtd_get_iotlb_gfn(addr, level);
3401b2b1237SJason Wang         key.level = level;
3411b2b1237SJason Wang         key.sid = source_id;
3421b2b1237SJason Wang         key.pasid = pasid;
343d66b969bSJason Wang         entry = g_hash_table_lookup(s->iotlb, &key);
344d66b969bSJason Wang         if (entry) {
345d66b969bSJason Wang             goto out;
346d66b969bSJason Wang         }
347d66b969bSJason Wang     }
348b5a280c0SLe Tan 
349d66b969bSJason Wang out:
350d66b969bSJason Wang     return entry;
351b5a280c0SLe Tan }
352b5a280c0SLe Tan 
3531d9efa73SPeter Xu /* Must be with IOMMU lock held */
354b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
355b5a280c0SLe Tan                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
3561b2b1237SJason Wang                              uint8_t access_flags, uint32_t level,
3571b2b1237SJason Wang                              uint32_t pasid)
358b5a280c0SLe Tan {
359b5a280c0SLe Tan     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
3601b2b1237SJason Wang     struct vtd_iotlb_key *key = g_malloc(sizeof(*key));
361d66b969bSJason Wang     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
362b5a280c0SLe Tan 
3636c441e1dSPeter Xu     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
364b5a280c0SLe Tan     if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
3656c441e1dSPeter Xu         trace_vtd_iotlb_reset("iotlb exceeds size limit");
3661d9efa73SPeter Xu         vtd_reset_iotlb_locked(s);
367b5a280c0SLe Tan     }
368b5a280c0SLe Tan 
369b5a280c0SLe Tan     entry->gfn = gfn;
370b5a280c0SLe Tan     entry->domain_id = domain_id;
371b5a280c0SLe Tan     entry->slpte = slpte;
37207f7b733SPeter Xu     entry->access_flags = access_flags;
373d66b969bSJason Wang     entry->mask = vtd_slpt_level_page_mask(level);
3741b2b1237SJason Wang     entry->pasid = pasid;
3751b2b1237SJason Wang 
3761b2b1237SJason Wang     key->gfn = gfn;
3771b2b1237SJason Wang     key->sid = source_id;
3781b2b1237SJason Wang     key->level = level;
3791b2b1237SJason Wang     key->pasid = pasid;
3801b2b1237SJason Wang 
381b5a280c0SLe Tan     g_hash_table_replace(s->iotlb, key, entry);
382b5a280c0SLe Tan }
383b5a280c0SLe Tan 
3841da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an
3851da12ec4SLe Tan  * interrupt via MSI.
3861da12ec4SLe Tan  */
3871da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
3881da12ec4SLe Tan                                    hwaddr mesg_data_reg)
3891da12ec4SLe Tan {
39032946019SRadim Krčmář     MSIMessage msi;
3911da12ec4SLe Tan 
3921da12ec4SLe Tan     assert(mesg_data_reg < DMAR_REG_SIZE);
3931da12ec4SLe Tan     assert(mesg_addr_reg < DMAR_REG_SIZE);
3941da12ec4SLe Tan 
39532946019SRadim Krčmář     msi.address = vtd_get_long_raw(s, mesg_addr_reg);
39632946019SRadim Krčmář     msi.data = vtd_get_long_raw(s, mesg_data_reg);
3971da12ec4SLe Tan 
3987feb51b7SPeter Xu     trace_vtd_irq_generate(msi.address, msi.data);
3997feb51b7SPeter Xu 
400eaaaf8abSPaolo Bonzini     apic_get_class(NULL)->send_msi(&msi);
4011da12ec4SLe Tan }
4021da12ec4SLe Tan 
4031da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met.
4041da12ec4SLe Tan  * Notice that the value of FSTS_REG being passed to it should be the one
4051da12ec4SLe Tan  * before any update.
4061da12ec4SLe Tan  */
4071da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
4081da12ec4SLe Tan {
4091da12ec4SLe Tan     if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
4101da12ec4SLe Tan         pre_fsts & VTD_FSTS_IQE) {
4111376211fSPeter Xu         error_report_once("There are previous interrupt conditions "
4127feb51b7SPeter Xu                           "to be serviced by software, fault event "
4131376211fSPeter Xu                           "is not generated");
4141da12ec4SLe Tan         return;
4151da12ec4SLe Tan     }
4161da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
4171da12ec4SLe Tan     if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
4181376211fSPeter Xu         error_report_once("Interrupt Mask set, irq is not generated");
4191da12ec4SLe Tan     } else {
4201da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
4211da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
4221da12ec4SLe Tan     }
4231da12ec4SLe Tan }
4241da12ec4SLe Tan 
4251da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by
4261da12ec4SLe Tan  * @index is Set.
4271da12ec4SLe Tan  */
4281da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
4291da12ec4SLe Tan {
4301da12ec4SLe Tan     /* Each reg is 128-bit */
4311da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4321da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
4331da12ec4SLe Tan 
4341da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4351da12ec4SLe Tan 
4361da12ec4SLe Tan     return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
4371da12ec4SLe Tan }
4381da12ec4SLe Tan 
4391da12ec4SLe Tan /* Update the PPF field of Fault Status Register.
4401da12ec4SLe Tan  * Should be called whenever change the F field of any fault recording
4411da12ec4SLe Tan  * registers.
4421da12ec4SLe Tan  */
4431da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s)
4441da12ec4SLe Tan {
4451da12ec4SLe Tan     uint32_t i;
4461da12ec4SLe Tan     uint32_t ppf_mask = 0;
4471da12ec4SLe Tan 
4481da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
4491da12ec4SLe Tan         if (vtd_is_frcd_set(s, i)) {
4501da12ec4SLe Tan             ppf_mask = VTD_FSTS_PPF;
4511da12ec4SLe Tan             break;
4521da12ec4SLe Tan         }
4531da12ec4SLe Tan     }
4541da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
4557feb51b7SPeter Xu     trace_vtd_fsts_ppf(!!ppf_mask);
4561da12ec4SLe Tan }
4571da12ec4SLe Tan 
4581da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
4591da12ec4SLe Tan {
4601da12ec4SLe Tan     /* Each reg is 128-bit */
4611da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4621da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
4631da12ec4SLe Tan 
4641da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4651da12ec4SLe Tan 
4661da12ec4SLe Tan     vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
4671da12ec4SLe Tan     vtd_update_fsts_ppf(s);
4681da12ec4SLe Tan }
4691da12ec4SLe Tan 
4701da12ec4SLe Tan /* Must not update F field now, should be done later */
4711da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
4721da12ec4SLe Tan                             uint16_t source_id, hwaddr addr,
4731b2b1237SJason Wang                             VTDFaultReason fault, bool is_write,
4741b2b1237SJason Wang                             bool is_pasid, uint32_t pasid)
4751da12ec4SLe Tan {
4761da12ec4SLe Tan     uint64_t hi = 0, lo;
4771da12ec4SLe Tan     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4781da12ec4SLe Tan 
4791da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4801da12ec4SLe Tan 
4811da12ec4SLe Tan     lo = VTD_FRCD_FI(addr);
4821b2b1237SJason Wang     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault) |
4831b2b1237SJason Wang          VTD_FRCD_PV(pasid) | VTD_FRCD_PP(is_pasid);
4841da12ec4SLe Tan     if (!is_write) {
4851da12ec4SLe Tan         hi |= VTD_FRCD_T;
4861da12ec4SLe Tan     }
4871da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr, lo);
4881da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
4897feb51b7SPeter Xu 
4907feb51b7SPeter Xu     trace_vtd_frr_new(index, hi, lo);
4911da12ec4SLe Tan }
4921da12ec4SLe Tan 
4931da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */
4941da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
4951da12ec4SLe Tan {
4961da12ec4SLe Tan     uint32_t i;
4971da12ec4SLe Tan     uint64_t frcd_reg;
4981da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
4991da12ec4SLe Tan 
5001da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
5011da12ec4SLe Tan         frcd_reg = vtd_get_quad_raw(s, addr);
5021da12ec4SLe Tan         if ((frcd_reg & VTD_FRCD_F) &&
5031da12ec4SLe Tan             ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
5041da12ec4SLe Tan             return true;
5051da12ec4SLe Tan         }
5061da12ec4SLe Tan         addr += 16; /* 128-bit for each */
5071da12ec4SLe Tan     }
5081da12ec4SLe Tan     return false;
5091da12ec4SLe Tan }
5101da12ec4SLe Tan 
5111da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */
5121da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
5131da12ec4SLe Tan                                   hwaddr addr, VTDFaultReason fault,
5141b2b1237SJason Wang                                   bool is_write, bool is_pasid,
5151b2b1237SJason Wang                                   uint32_t pasid)
5161da12ec4SLe Tan {
5171da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
5181da12ec4SLe Tan 
5191da12ec4SLe Tan     assert(fault < VTD_FR_MAX);
5201da12ec4SLe Tan 
5217feb51b7SPeter Xu     trace_vtd_dmar_fault(source_id, fault, addr, is_write);
5227feb51b7SPeter Xu 
5231da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PFO) {
5241376211fSPeter Xu         error_report_once("New fault is not recorded due to "
5251376211fSPeter Xu                           "Primary Fault Overflow");
5261da12ec4SLe Tan         return;
5271da12ec4SLe Tan     }
5287feb51b7SPeter Xu 
5291da12ec4SLe Tan     if (vtd_try_collapse_fault(s, source_id)) {
5301376211fSPeter Xu         error_report_once("New fault is not recorded due to "
5311376211fSPeter Xu                           "compression of faults");
5321da12ec4SLe Tan         return;
5331da12ec4SLe Tan     }
5347feb51b7SPeter Xu 
5351da12ec4SLe Tan     if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
5361376211fSPeter Xu         error_report_once("Next Fault Recording Reg is used, "
5371376211fSPeter Xu                           "new fault is not recorded, set PFO field");
5381da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
5391da12ec4SLe Tan         return;
5401da12ec4SLe Tan     }
5411da12ec4SLe Tan 
5421b2b1237SJason Wang     vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault,
5431b2b1237SJason Wang                     is_write, is_pasid, pasid);
5441da12ec4SLe Tan 
5451da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PPF) {
5461376211fSPeter Xu         error_report_once("There are pending faults already, "
5471376211fSPeter Xu                           "fault event is not generated");
5481da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
5491da12ec4SLe Tan         s->next_frcd_reg++;
5501da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
5511da12ec4SLe Tan             s->next_frcd_reg = 0;
5521da12ec4SLe Tan         }
5531da12ec4SLe Tan     } else {
5541da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
5551da12ec4SLe Tan                                 VTD_FSTS_FRI(s->next_frcd_reg));
5561da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
5571da12ec4SLe Tan         s->next_frcd_reg++;
5581da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
5591da12ec4SLe Tan             s->next_frcd_reg = 0;
5601da12ec4SLe Tan         }
5611da12ec4SLe Tan         /* This case actually cause the PPF to be Set.
5621da12ec4SLe Tan          * So generate fault event (interrupt).
5631da12ec4SLe Tan          */
5641da12ec4SLe Tan          vtd_generate_fault_event(s, fsts_reg);
5651da12ec4SLe Tan     }
5661da12ec4SLe Tan }
5671da12ec4SLe Tan 
568ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error
569ed7b8fbcSLe Tan  * conditions.
570ed7b8fbcSLe Tan  */
571ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
572ed7b8fbcSLe Tan {
573ed7b8fbcSLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
574ed7b8fbcSLe Tan 
575ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
576ed7b8fbcSLe Tan     vtd_generate_fault_event(s, fsts_reg);
577ed7b8fbcSLe Tan }
578ed7b8fbcSLe Tan 
579ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */
580ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s)
581ed7b8fbcSLe Tan {
582ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
583bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("One pending, skip current");
584ed7b8fbcSLe Tan         return;
585ed7b8fbcSLe Tan     }
586ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
587ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
588ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
589bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
590bc535e59SPeter Xu                                     "new event not generated");
591ed7b8fbcSLe Tan         return;
592ed7b8fbcSLe Tan     } else {
593ed7b8fbcSLe Tan         /* Generate the interrupt event */
594bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("Generating complete event");
595ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
596ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
597ed7b8fbcSLe Tan     }
598ed7b8fbcSLe Tan }
599ed7b8fbcSLe Tan 
600fb43cf73SLiu, Yi L static inline bool vtd_root_entry_present(IntelIOMMUState *s,
601fb43cf73SLiu, Yi L                                           VTDRootEntry *re,
602fb43cf73SLiu, Yi L                                           uint8_t devfn)
6031da12ec4SLe Tan {
604fb43cf73SLiu, Yi L     if (s->root_scalable && devfn > UINT8_MAX / 2) {
605fb43cf73SLiu, Yi L         return re->hi & VTD_ROOT_ENTRY_P;
606fb43cf73SLiu, Yi L     }
607fb43cf73SLiu, Yi L 
608fb43cf73SLiu, Yi L     return re->lo & VTD_ROOT_ENTRY_P;
6091da12ec4SLe Tan }
6101da12ec4SLe Tan 
6111da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
6121da12ec4SLe Tan                               VTDRootEntry *re)
6131da12ec4SLe Tan {
6141da12ec4SLe Tan     dma_addr_t addr;
6151da12ec4SLe Tan 
6161da12ec4SLe Tan     addr = s->root + index * sizeof(*re);
617ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
618ba06fe8aSPhilippe Mathieu-Daudé                         re, sizeof(*re), MEMTXATTRS_UNSPECIFIED)) {
619fb43cf73SLiu, Yi L         re->lo = 0;
6201da12ec4SLe Tan         return -VTD_FR_ROOT_TABLE_INV;
6211da12ec4SLe Tan     }
622fb43cf73SLiu, Yi L     re->lo = le64_to_cpu(re->lo);
623fb43cf73SLiu, Yi L     re->hi = le64_to_cpu(re->hi);
6241da12ec4SLe Tan     return 0;
6251da12ec4SLe Tan }
6261da12ec4SLe Tan 
6278f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context)
6281da12ec4SLe Tan {
6291da12ec4SLe Tan     return context->lo & VTD_CONTEXT_ENTRY_P;
6301da12ec4SLe Tan }
6311da12ec4SLe Tan 
632fb43cf73SLiu, Yi L static int vtd_get_context_entry_from_root(IntelIOMMUState *s,
633fb43cf73SLiu, Yi L                                            VTDRootEntry *re,
634fb43cf73SLiu, Yi L                                            uint8_t index,
6351da12ec4SLe Tan                                            VTDContextEntry *ce)
6361da12ec4SLe Tan {
637fb43cf73SLiu, Yi L     dma_addr_t addr, ce_size;
6381da12ec4SLe Tan 
6396c441e1dSPeter Xu     /* we have checked that root entry is present */
640fb43cf73SLiu, Yi L     ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE :
641fb43cf73SLiu, Yi L               VTD_CTX_ENTRY_LEGACY_SIZE;
642fb43cf73SLiu, Yi L 
643fb43cf73SLiu, Yi L     if (s->root_scalable && index > UINT8_MAX / 2) {
644fb43cf73SLiu, Yi L         index = index & (~VTD_DEVFN_CHECK_MASK);
645fb43cf73SLiu, Yi L         addr = re->hi & VTD_ROOT_ENTRY_CTP;
646fb43cf73SLiu, Yi L     } else {
647fb43cf73SLiu, Yi L         addr = re->lo & VTD_ROOT_ENTRY_CTP;
648fb43cf73SLiu, Yi L     }
649fb43cf73SLiu, Yi L 
650fb43cf73SLiu, Yi L     addr = addr + index * ce_size;
651ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
652ba06fe8aSPhilippe Mathieu-Daudé                         ce, ce_size, MEMTXATTRS_UNSPECIFIED)) {
6531da12ec4SLe Tan         return -VTD_FR_CONTEXT_TABLE_INV;
6541da12ec4SLe Tan     }
655fb43cf73SLiu, Yi L 
6561da12ec4SLe Tan     ce->lo = le64_to_cpu(ce->lo);
6571da12ec4SLe Tan     ce->hi = le64_to_cpu(ce->hi);
658fb43cf73SLiu, Yi L     if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) {
659fb43cf73SLiu, Yi L         ce->val[2] = le64_to_cpu(ce->val[2]);
660fb43cf73SLiu, Yi L         ce->val[3] = le64_to_cpu(ce->val[3]);
661fb43cf73SLiu, Yi L     }
6621da12ec4SLe Tan     return 0;
6631da12ec4SLe Tan }
6641da12ec4SLe Tan 
6658f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
6661da12ec4SLe Tan {
6671da12ec4SLe Tan     return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
6681da12ec4SLe Tan }
6691da12ec4SLe Tan 
67037f51384SPrasad Singamsetty static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw)
6711da12ec4SLe Tan {
67237f51384SPrasad Singamsetty     return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw);
6731da12ec4SLe Tan }
6741da12ec4SLe Tan 
6751da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */
6761da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
6771da12ec4SLe Tan {
6781da12ec4SLe Tan     return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
6791da12ec4SLe Tan }
6801da12ec4SLe Tan 
6811da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */
6821da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
6831da12ec4SLe Tan {
6841da12ec4SLe Tan     uint64_t slpte;
6851da12ec4SLe Tan 
6861da12ec4SLe Tan     assert(index < VTD_SL_PT_ENTRY_NR);
6871da12ec4SLe Tan 
6881da12ec4SLe Tan     if (dma_memory_read(&address_space_memory,
689ba06fe8aSPhilippe Mathieu-Daudé                         base_addr + index * sizeof(slpte),
690ba06fe8aSPhilippe Mathieu-Daudé                         &slpte, sizeof(slpte), MEMTXATTRS_UNSPECIFIED)) {
6911da12ec4SLe Tan         slpte = (uint64_t)-1;
6921da12ec4SLe Tan         return slpte;
6931da12ec4SLe Tan     }
6941da12ec4SLe Tan     slpte = le64_to_cpu(slpte);
6951da12ec4SLe Tan     return slpte;
6961da12ec4SLe Tan }
6971da12ec4SLe Tan 
6986e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset
6996e905564SPeter Xu  * of current level.
7001da12ec4SLe Tan  */
7016e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
7021da12ec4SLe Tan {
7036e905564SPeter Xu     return (iova >> vtd_slpt_level_shift(level)) &
7041da12ec4SLe Tan             ((1ULL << VTD_SL_LEVEL_BITS) - 1);
7051da12ec4SLe Tan }
7061da12ec4SLe Tan 
7071da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */
7081da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
7091da12ec4SLe Tan {
7101da12ec4SLe Tan     return VTD_CAP_SAGAW_MASK & s->cap &
7111da12ec4SLe Tan            (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
7121da12ec4SLe Tan }
7131da12ec4SLe Tan 
714fb43cf73SLiu, Yi L /* Return true if check passed, otherwise false */
715fb43cf73SLiu, Yi L static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
716fb43cf73SLiu, Yi L                                      VTDPASIDEntry *pe)
717fb43cf73SLiu, Yi L {
718fb43cf73SLiu, Yi L     switch (VTD_PE_GET_TYPE(pe)) {
719fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_FLT:
720fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_SLT:
721fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_NESTED:
722fb43cf73SLiu, Yi L         break;
723fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_PT:
724fb43cf73SLiu, Yi L         if (!x86_iommu->pt_supported) {
725fb43cf73SLiu, Yi L             return false;
726fb43cf73SLiu, Yi L         }
727fb43cf73SLiu, Yi L         break;
728fb43cf73SLiu, Yi L     default:
72937557b09SCai Huoqing         /* Unknown type */
730fb43cf73SLiu, Yi L         return false;
731fb43cf73SLiu, Yi L     }
732fb43cf73SLiu, Yi L     return true;
733fb43cf73SLiu, Yi L }
734fb43cf73SLiu, Yi L 
73556fc1e6aSLiu Yi L static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
73656fc1e6aSLiu Yi L {
73756fc1e6aSLiu Yi L     return pdire->val & 1;
73856fc1e6aSLiu Yi L }
73956fc1e6aSLiu Yi L 
74056fc1e6aSLiu Yi L /**
74156fc1e6aSLiu Yi L  * Caller of this function should check present bit if wants
74237557b09SCai Huoqing  * to use pdir entry for further usage except for fpd bit check.
74356fc1e6aSLiu Yi L  */
74456fc1e6aSLiu Yi L static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
745fb43cf73SLiu, Yi L                                          uint32_t pasid,
746fb43cf73SLiu, Yi L                                          VTDPASIDDirEntry *pdire)
747fb43cf73SLiu, Yi L {
748fb43cf73SLiu, Yi L     uint32_t index;
749fb43cf73SLiu, Yi L     dma_addr_t addr, entry_size;
750fb43cf73SLiu, Yi L 
751fb43cf73SLiu, Yi L     index = VTD_PASID_DIR_INDEX(pasid);
752fb43cf73SLiu, Yi L     entry_size = VTD_PASID_DIR_ENTRY_SIZE;
753fb43cf73SLiu, Yi L     addr = pasid_dir_base + index * entry_size;
754ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
755ba06fe8aSPhilippe Mathieu-Daudé                         pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
756fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
757fb43cf73SLiu, Yi L     }
758fb43cf73SLiu, Yi L 
759fb43cf73SLiu, Yi L     return 0;
760fb43cf73SLiu, Yi L }
761fb43cf73SLiu, Yi L 
76256fc1e6aSLiu Yi L static inline bool vtd_pe_present(VTDPASIDEntry *pe)
76356fc1e6aSLiu Yi L {
76456fc1e6aSLiu Yi L     return pe->val[0] & VTD_PASID_ENTRY_P;
76556fc1e6aSLiu Yi L }
76656fc1e6aSLiu Yi L 
76756fc1e6aSLiu Yi L static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
768fb43cf73SLiu, Yi L                                           uint32_t pasid,
76956fc1e6aSLiu Yi L                                           dma_addr_t addr,
770fb43cf73SLiu, Yi L                                           VTDPASIDEntry *pe)
771fb43cf73SLiu, Yi L {
772fb43cf73SLiu, Yi L     uint32_t index;
77356fc1e6aSLiu Yi L     dma_addr_t entry_size;
774fb43cf73SLiu, Yi L     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
775fb43cf73SLiu, Yi L 
776fb43cf73SLiu, Yi L     index = VTD_PASID_TABLE_INDEX(pasid);
777fb43cf73SLiu, Yi L     entry_size = VTD_PASID_ENTRY_SIZE;
778fb43cf73SLiu, Yi L     addr = addr + index * entry_size;
779ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
780ba06fe8aSPhilippe Mathieu-Daudé                         pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
781fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
782fb43cf73SLiu, Yi L     }
783fb43cf73SLiu, Yi L 
784fb43cf73SLiu, Yi L     /* Do translation type check */
785fb43cf73SLiu, Yi L     if (!vtd_pe_type_check(x86_iommu, pe)) {
786fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
787fb43cf73SLiu, Yi L     }
788fb43cf73SLiu, Yi L 
789fb43cf73SLiu, Yi L     if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
790fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
791fb43cf73SLiu, Yi L     }
792fb43cf73SLiu, Yi L 
793fb43cf73SLiu, Yi L     return 0;
794fb43cf73SLiu, Yi L }
795fb43cf73SLiu, Yi L 
79656fc1e6aSLiu Yi L /**
79756fc1e6aSLiu Yi L  * Caller of this function should check present bit if wants
79837557b09SCai Huoqing  * to use pasid entry for further usage except for fpd bit check.
79956fc1e6aSLiu Yi L  */
80056fc1e6aSLiu Yi L static int vtd_get_pe_from_pdire(IntelIOMMUState *s,
80156fc1e6aSLiu Yi L                                  uint32_t pasid,
80256fc1e6aSLiu Yi L                                  VTDPASIDDirEntry *pdire,
80356fc1e6aSLiu Yi L                                  VTDPASIDEntry *pe)
80456fc1e6aSLiu Yi L {
80556fc1e6aSLiu Yi L     dma_addr_t addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK;
80656fc1e6aSLiu Yi L 
80756fc1e6aSLiu Yi L     return vtd_get_pe_in_pasid_leaf_table(s, pasid, addr, pe);
80856fc1e6aSLiu Yi L }
80956fc1e6aSLiu Yi L 
81056fc1e6aSLiu Yi L /**
81156fc1e6aSLiu Yi L  * This function gets a pasid entry from a specified pasid
81256fc1e6aSLiu Yi L  * table (includes dir and leaf table) with a specified pasid.
81356fc1e6aSLiu Yi L  * Sanity check should be done to ensure return a present
81456fc1e6aSLiu Yi L  * pasid entry to caller.
81556fc1e6aSLiu Yi L  */
81656fc1e6aSLiu Yi L static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
817fb43cf73SLiu, Yi L                                        dma_addr_t pasid_dir_base,
818fb43cf73SLiu, Yi L                                        uint32_t pasid,
819fb43cf73SLiu, Yi L                                        VTDPASIDEntry *pe)
820fb43cf73SLiu, Yi L {
821fb43cf73SLiu, Yi L     int ret;
822fb43cf73SLiu, Yi L     VTDPASIDDirEntry pdire;
823fb43cf73SLiu, Yi L 
82456fc1e6aSLiu Yi L     ret = vtd_get_pdire_from_pdir_table(pasid_dir_base,
82556fc1e6aSLiu Yi L                                         pasid, &pdire);
826fb43cf73SLiu, Yi L     if (ret) {
827fb43cf73SLiu, Yi L         return ret;
828fb43cf73SLiu, Yi L     }
829fb43cf73SLiu, Yi L 
83056fc1e6aSLiu Yi L     if (!vtd_pdire_present(&pdire)) {
83156fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
83256fc1e6aSLiu Yi L     }
83356fc1e6aSLiu Yi L 
83456fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
835fb43cf73SLiu, Yi L     if (ret) {
836fb43cf73SLiu, Yi L         return ret;
837fb43cf73SLiu, Yi L     }
838fb43cf73SLiu, Yi L 
83956fc1e6aSLiu Yi L     if (!vtd_pe_present(pe)) {
84056fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
84156fc1e6aSLiu Yi L     }
84256fc1e6aSLiu Yi L 
84356fc1e6aSLiu Yi L     return 0;
844fb43cf73SLiu, Yi L }
845fb43cf73SLiu, Yi L 
846fb43cf73SLiu, Yi L static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
847fb43cf73SLiu, Yi L                                       VTDContextEntry *ce,
8481b2b1237SJason Wang                                       VTDPASIDEntry *pe,
8491b2b1237SJason Wang                                       uint32_t pasid)
850fb43cf73SLiu, Yi L {
851fb43cf73SLiu, Yi L     dma_addr_t pasid_dir_base;
852fb43cf73SLiu, Yi L     int ret = 0;
853fb43cf73SLiu, Yi L 
8541b2b1237SJason Wang     if (pasid == PCI_NO_PASID) {
855fb43cf73SLiu, Yi L         pasid = VTD_CE_GET_RID2PASID(ce);
8561b2b1237SJason Wang     }
857fb43cf73SLiu, Yi L     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
85856fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
859fb43cf73SLiu, Yi L 
860fb43cf73SLiu, Yi L     return ret;
861fb43cf73SLiu, Yi L }
862fb43cf73SLiu, Yi L 
863fb43cf73SLiu, Yi L static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
864fb43cf73SLiu, Yi L                                 VTDContextEntry *ce,
8651b2b1237SJason Wang                                 bool *pe_fpd_set,
8661b2b1237SJason Wang                                 uint32_t pasid)
867fb43cf73SLiu, Yi L {
868fb43cf73SLiu, Yi L     int ret;
869fb43cf73SLiu, Yi L     dma_addr_t pasid_dir_base;
870fb43cf73SLiu, Yi L     VTDPASIDDirEntry pdire;
871fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
872fb43cf73SLiu, Yi L 
8731b2b1237SJason Wang     if (pasid == PCI_NO_PASID) {
874fb43cf73SLiu, Yi L         pasid = VTD_CE_GET_RID2PASID(ce);
8751b2b1237SJason Wang     }
876fb43cf73SLiu, Yi L     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
877fb43cf73SLiu, Yi L 
87856fc1e6aSLiu Yi L     /*
87956fc1e6aSLiu Yi L      * No present bit check since fpd is meaningful even
88056fc1e6aSLiu Yi L      * if the present bit is clear.
88156fc1e6aSLiu Yi L      */
88256fc1e6aSLiu Yi L     ret = vtd_get_pdire_from_pdir_table(pasid_dir_base, pasid, &pdire);
883fb43cf73SLiu, Yi L     if (ret) {
884fb43cf73SLiu, Yi L         return ret;
885fb43cf73SLiu, Yi L     }
886fb43cf73SLiu, Yi L 
887fb43cf73SLiu, Yi L     if (pdire.val & VTD_PASID_DIR_FPD) {
888fb43cf73SLiu, Yi L         *pe_fpd_set = true;
889fb43cf73SLiu, Yi L         return 0;
890fb43cf73SLiu, Yi L     }
891fb43cf73SLiu, Yi L 
89256fc1e6aSLiu Yi L     if (!vtd_pdire_present(&pdire)) {
89356fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
89456fc1e6aSLiu Yi L     }
89556fc1e6aSLiu Yi L 
89656fc1e6aSLiu Yi L     /*
89756fc1e6aSLiu Yi L      * No present bit check since fpd is meaningful even
89856fc1e6aSLiu Yi L      * if the present bit is clear.
89956fc1e6aSLiu Yi L      */
90056fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pdire(s, pasid, &pdire, &pe);
901fb43cf73SLiu, Yi L     if (ret) {
902fb43cf73SLiu, Yi L         return ret;
903fb43cf73SLiu, Yi L     }
904fb43cf73SLiu, Yi L 
905fb43cf73SLiu, Yi L     if (pe.val[0] & VTD_PASID_ENTRY_FPD) {
906fb43cf73SLiu, Yi L         *pe_fpd_set = true;
907fb43cf73SLiu, Yi L     }
908fb43cf73SLiu, Yi L 
909fb43cf73SLiu, Yi L     return 0;
910fb43cf73SLiu, Yi L }
911fb43cf73SLiu, Yi L 
9121da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level
9131da12ec4SLe Tan  * page-table walk from the Address Width field of context-entry.
9141da12ec4SLe Tan  */
9158f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
9161da12ec4SLe Tan {
9171da12ec4SLe Tan     return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
9181da12ec4SLe Tan }
9191da12ec4SLe Tan 
920fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
9211b2b1237SJason Wang                                    VTDContextEntry *ce,
9221b2b1237SJason Wang                                    uint32_t pasid)
923fb43cf73SLiu, Yi L {
924fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
925fb43cf73SLiu, Yi L 
926fb43cf73SLiu, Yi L     if (s->root_scalable) {
9271b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
928fb43cf73SLiu, Yi L         return VTD_PE_GET_LEVEL(&pe);
929fb43cf73SLiu, Yi L     }
930fb43cf73SLiu, Yi L 
931fb43cf73SLiu, Yi L     return vtd_ce_get_level(ce);
932fb43cf73SLiu, Yi L }
933fb43cf73SLiu, Yi L 
9348f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
9351da12ec4SLe Tan {
9361da12ec4SLe Tan     return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
9371da12ec4SLe Tan }
9381da12ec4SLe Tan 
939fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
9401b2b1237SJason Wang                                   VTDContextEntry *ce,
9411b2b1237SJason Wang                                   uint32_t pasid)
942fb43cf73SLiu, Yi L {
943fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
944fb43cf73SLiu, Yi L 
945fb43cf73SLiu, Yi L     if (s->root_scalable) {
9461b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
947fb43cf73SLiu, Yi L         return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
948fb43cf73SLiu, Yi L     }
949fb43cf73SLiu, Yi L 
950fb43cf73SLiu, Yi L     return vtd_ce_get_agaw(ce);
951fb43cf73SLiu, Yi L }
952fb43cf73SLiu, Yi L 
953127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
954127ff5c3SPeter Xu {
955127ff5c3SPeter Xu     return ce->lo & VTD_CONTEXT_ENTRY_TT;
956127ff5c3SPeter Xu }
957127ff5c3SPeter Xu 
958fb43cf73SLiu, Yi L /* Only for Legacy Mode. Return true if check passed, otherwise false */
959f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
960f80c9874SPeter Xu                                      VTDContextEntry *ce)
961f80c9874SPeter Xu {
962f80c9874SPeter Xu     switch (vtd_ce_get_type(ce)) {
963f80c9874SPeter Xu     case VTD_CONTEXT_TT_MULTI_LEVEL:
964f80c9874SPeter Xu         /* Always supported */
965f80c9874SPeter Xu         break;
966f80c9874SPeter Xu     case VTD_CONTEXT_TT_DEV_IOTLB:
967f80c9874SPeter Xu         if (!x86_iommu->dt_supported) {
968095955b2SPeter Xu             error_report_once("%s: DT specified but not supported", __func__);
969f80c9874SPeter Xu             return false;
970f80c9874SPeter Xu         }
971f80c9874SPeter Xu         break;
972dbaabb25SPeter Xu     case VTD_CONTEXT_TT_PASS_THROUGH:
973dbaabb25SPeter Xu         if (!x86_iommu->pt_supported) {
974095955b2SPeter Xu             error_report_once("%s: PT specified but not supported", __func__);
975dbaabb25SPeter Xu             return false;
976dbaabb25SPeter Xu         }
977dbaabb25SPeter Xu         break;
978f80c9874SPeter Xu     default:
979fb43cf73SLiu, Yi L         /* Unknown type */
980095955b2SPeter Xu         error_report_once("%s: unknown ce type: %"PRIu32, __func__,
981095955b2SPeter Xu                           vtd_ce_get_type(ce));
982f80c9874SPeter Xu         return false;
983f80c9874SPeter Xu     }
984f80c9874SPeter Xu     return true;
985f80c9874SPeter Xu }
986f80c9874SPeter Xu 
987fb43cf73SLiu, Yi L static inline uint64_t vtd_iova_limit(IntelIOMMUState *s,
9881b2b1237SJason Wang                                       VTDContextEntry *ce, uint8_t aw,
9891b2b1237SJason Wang                                       uint32_t pasid)
990f06a696dSPeter Xu {
9911b2b1237SJason Wang     uint32_t ce_agaw = vtd_get_iova_agaw(s, ce, pasid);
99237f51384SPrasad Singamsetty     return 1ULL << MIN(ce_agaw, aw);
993f06a696dSPeter Xu }
994f06a696dSPeter Xu 
995f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */
996fb43cf73SLiu, Yi L static inline bool vtd_iova_range_check(IntelIOMMUState *s,
997fb43cf73SLiu, Yi L                                         uint64_t iova, VTDContextEntry *ce,
9981b2b1237SJason Wang                                         uint8_t aw, uint32_t pasid)
999f06a696dSPeter Xu {
1000f06a696dSPeter Xu     /*
1001f06a696dSPeter Xu      * Check if @iova is above 2^X-1, where X is the minimum of MGAW
1002f06a696dSPeter Xu      * in CAP_REG and AW in context-entry.
1003f06a696dSPeter Xu      */
10041b2b1237SJason Wang     return !(iova & ~(vtd_iova_limit(s, ce, aw, pasid) - 1));
1005fb43cf73SLiu, Yi L }
1006fb43cf73SLiu, Yi L 
1007fb43cf73SLiu, Yi L static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
10081b2b1237SJason Wang                                           VTDContextEntry *ce,
10091b2b1237SJason Wang                                           uint32_t pasid)
1010fb43cf73SLiu, Yi L {
1011fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1012fb43cf73SLiu, Yi L 
1013fb43cf73SLiu, Yi L     if (s->root_scalable) {
10141b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
1015fb43cf73SLiu, Yi L         return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR;
1016fb43cf73SLiu, Yi L     }
1017fb43cf73SLiu, Yi L 
1018fb43cf73SLiu, Yi L     return vtd_ce_get_slpt_base(ce);
1019f06a696dSPeter Xu }
1020f06a696dSPeter Xu 
102192e5d85eSPrasad Singamsetty /*
102292e5d85eSPrasad Singamsetty  * Rsvd field masks for spte:
1023ce586f3bSQi, Yadong  *     vtd_spte_rsvd 4k pages
1024ce586f3bSQi, Yadong  *     vtd_spte_rsvd_large large pages
102592e5d85eSPrasad Singamsetty  */
1026ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd[5];
1027ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd_large[5];
10281da12ec4SLe Tan 
10291da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
10301da12ec4SLe Tan {
1031ce586f3bSQi, Yadong     uint64_t rsvd_mask = vtd_spte_rsvd[level];
1032ce586f3bSQi, Yadong 
1033ce586f3bSQi, Yadong     if ((level == VTD_SL_PD_LEVEL || level == VTD_SL_PDP_LEVEL) &&
1034ce586f3bSQi, Yadong         (slpte & VTD_SL_PT_PAGE_SIZE_MASK)) {
1035ce586f3bSQi, Yadong         /* large page */
1036ce586f3bSQi, Yadong         rsvd_mask = vtd_spte_rsvd_large[level];
10371da12ec4SLe Tan     }
1038ce586f3bSQi, Yadong 
1039ce586f3bSQi, Yadong     return slpte & rsvd_mask;
10401da12ec4SLe Tan }
10411da12ec4SLe Tan 
10426e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level
10431da12ec4SLe Tan  * of the translation, can be used for deciding the size of large page.
10441da12ec4SLe Tan  */
1045fb43cf73SLiu, Yi L static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce,
1046fb43cf73SLiu, Yi L                              uint64_t iova, bool is_write,
10471da12ec4SLe Tan                              uint64_t *slptep, uint32_t *slpte_level,
10481b2b1237SJason Wang                              bool *reads, bool *writes, uint8_t aw_bits,
10491b2b1237SJason Wang                              uint32_t pasid)
10501da12ec4SLe Tan {
10511b2b1237SJason Wang     dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
10521b2b1237SJason Wang     uint32_t level = vtd_get_iova_level(s, ce, pasid);
10531da12ec4SLe Tan     uint32_t offset;
10541da12ec4SLe Tan     uint64_t slpte;
10551da12ec4SLe Tan     uint64_t access_right_check;
1056ea97a1bdSJason Wang     uint64_t xlat, size;
10571da12ec4SLe Tan 
10581b2b1237SJason Wang     if (!vtd_iova_range_check(s, iova, ce, aw_bits, pasid)) {
10591b2b1237SJason Wang         error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ","
10601b2b1237SJason Wang                           "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
10611da12ec4SLe Tan         return -VTD_FR_ADDR_BEYOND_MGAW;
10621da12ec4SLe Tan     }
10631da12ec4SLe Tan 
10641da12ec4SLe Tan     /* FIXME: what is the Atomics request here? */
10651da12ec4SLe Tan     access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
10661da12ec4SLe Tan 
10671da12ec4SLe Tan     while (true) {
10686e905564SPeter Xu         offset = vtd_iova_level_offset(iova, level);
10691da12ec4SLe Tan         slpte = vtd_get_slpte(addr, offset);
10701da12ec4SLe Tan 
10711da12ec4SLe Tan         if (slpte == (uint64_t)-1) {
10724e4abd11SPeter Xu             error_report_once("%s: detected read error on DMAR slpte "
10731b2b1237SJason Wang                               "(iova=0x%" PRIx64 ", pasid=0x%" PRIx32 ")",
10741b2b1237SJason Wang                               __func__, iova, pasid);
10751b2b1237SJason Wang             if (level == vtd_get_iova_level(s, ce, pasid)) {
10761da12ec4SLe Tan                 /* Invalid programming of context-entry */
10771da12ec4SLe Tan                 return -VTD_FR_CONTEXT_ENTRY_INV;
10781da12ec4SLe Tan             } else {
10791da12ec4SLe Tan                 return -VTD_FR_PAGING_ENTRY_INV;
10801da12ec4SLe Tan             }
10811da12ec4SLe Tan         }
10821da12ec4SLe Tan         *reads = (*reads) && (slpte & VTD_SL_R);
10831da12ec4SLe Tan         *writes = (*writes) && (slpte & VTD_SL_W);
10841da12ec4SLe Tan         if (!(slpte & access_right_check)) {
10854e4abd11SPeter Xu             error_report_once("%s: detected slpte permission error "
10864e4abd11SPeter Xu                               "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
10871b2b1237SJason Wang                               "slpte=0x%" PRIx64 ", write=%d, pasid=0x%"
10881b2b1237SJason Wang                               PRIx32 ")", __func__, iova, level,
10891b2b1237SJason Wang                               slpte, is_write, pasid);
10901da12ec4SLe Tan             return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
10911da12ec4SLe Tan         }
10921da12ec4SLe Tan         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
10934e4abd11SPeter Xu             error_report_once("%s: detected splte reserve non-zero "
10944e4abd11SPeter Xu                               "iova=0x%" PRIx64 ", level=0x%" PRIx32
10951b2b1237SJason Wang                               "slpte=0x%" PRIx64 ", pasid=0x%" PRIX32 ")",
10961b2b1237SJason Wang                               __func__, iova, level, slpte, pasid);
10971da12ec4SLe Tan             return -VTD_FR_PAGING_ENTRY_RSVD;
10981da12ec4SLe Tan         }
10991da12ec4SLe Tan 
11001da12ec4SLe Tan         if (vtd_is_last_slpte(slpte, level)) {
11011da12ec4SLe Tan             *slptep = slpte;
11021da12ec4SLe Tan             *slpte_level = level;
1103ea97a1bdSJason Wang             break;
11041da12ec4SLe Tan         }
110537f51384SPrasad Singamsetty         addr = vtd_get_slpte_addr(slpte, aw_bits);
11061da12ec4SLe Tan         level--;
11071da12ec4SLe Tan     }
1108ea97a1bdSJason Wang 
1109ea97a1bdSJason Wang     xlat = vtd_get_slpte_addr(*slptep, aw_bits);
1110ea97a1bdSJason Wang     size = ~vtd_slpt_level_page_mask(level) + 1;
1111ea97a1bdSJason Wang 
1112ea97a1bdSJason Wang     /*
1113ea97a1bdSJason Wang      * From VT-d spec 3.14: Untranslated requests and translation
1114ea97a1bdSJason Wang      * requests that result in an address in the interrupt range will be
1115ea97a1bdSJason Wang      * blocked with condition code LGN.4 or SGN.8.
1116ea97a1bdSJason Wang      */
1117ea97a1bdSJason Wang     if ((xlat > VTD_INTERRUPT_ADDR_LAST ||
1118ea97a1bdSJason Wang          xlat + size - 1 < VTD_INTERRUPT_ADDR_FIRST)) {
1119ea97a1bdSJason Wang         return 0;
1120ea97a1bdSJason Wang     } else {
1121ea97a1bdSJason Wang         error_report_once("%s: xlat address is in interrupt range "
1122ea97a1bdSJason Wang                           "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
1123ea97a1bdSJason Wang                           "slpte=0x%" PRIx64 ", write=%d, "
11241b2b1237SJason Wang                           "xlat=0x%" PRIx64 ", size=0x%" PRIx64 ", "
11251b2b1237SJason Wang                           "pasid=0x%" PRIx32 ")",
1126ea97a1bdSJason Wang                           __func__, iova, level, slpte, is_write,
11271b2b1237SJason Wang                           xlat, size, pasid);
1128ea97a1bdSJason Wang         return s->scalable_mode ? -VTD_FR_SM_INTERRUPT_ADDR :
1129ea97a1bdSJason Wang                                   -VTD_FR_INTERRUPT_ADDR;
1130ea97a1bdSJason Wang     }
11311da12ec4SLe Tan }
11321da12ec4SLe Tan 
11335039caf3SEugenio Pérez typedef int (*vtd_page_walk_hook)(IOMMUTLBEvent *event, void *private);
1134f06a696dSPeter Xu 
1135fe215b0cSPeter Xu /**
1136fe215b0cSPeter Xu  * Constant information used during page walking
1137fe215b0cSPeter Xu  *
1138fe215b0cSPeter Xu  * @hook_fn: hook func to be called when detected page
1139fe215b0cSPeter Xu  * @private: private data to be passed into hook func
1140fe215b0cSPeter Xu  * @notify_unmap: whether we should notify invalid entries
11412f764fa8SPeter Xu  * @as: VT-d address space of the device
1142fe215b0cSPeter Xu  * @aw: maximum address width
1143d118c06eSPeter Xu  * @domain: domain ID of the page walk
1144fe215b0cSPeter Xu  */
1145fe215b0cSPeter Xu typedef struct {
11462f764fa8SPeter Xu     VTDAddressSpace *as;
1147fe215b0cSPeter Xu     vtd_page_walk_hook hook_fn;
1148fe215b0cSPeter Xu     void *private;
1149fe215b0cSPeter Xu     bool notify_unmap;
1150fe215b0cSPeter Xu     uint8_t aw;
1151d118c06eSPeter Xu     uint16_t domain_id;
1152fe215b0cSPeter Xu } vtd_page_walk_info;
1153fe215b0cSPeter Xu 
11545039caf3SEugenio Pérez static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
115536d2d52bSPeter Xu {
115663b88968SPeter Xu     VTDAddressSpace *as = info->as;
1157fe215b0cSPeter Xu     vtd_page_walk_hook hook_fn = info->hook_fn;
1158fe215b0cSPeter Xu     void *private = info->private;
11595039caf3SEugenio Pérez     IOMMUTLBEntry *entry = &event->entry;
116063b88968SPeter Xu     DMAMap target = {
116163b88968SPeter Xu         .iova = entry->iova,
116263b88968SPeter Xu         .size = entry->addr_mask,
116363b88968SPeter Xu         .translated_addr = entry->translated_addr,
116463b88968SPeter Xu         .perm = entry->perm,
116563b88968SPeter Xu     };
1166a89b34beSEugenio Pérez     const DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
116763b88968SPeter Xu 
11685039caf3SEugenio Pérez     if (event->type == IOMMU_NOTIFIER_UNMAP && !info->notify_unmap) {
116963b88968SPeter Xu         trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
117063b88968SPeter Xu         return 0;
117163b88968SPeter Xu     }
1172fe215b0cSPeter Xu 
117336d2d52bSPeter Xu     assert(hook_fn);
117463b88968SPeter Xu 
117563b88968SPeter Xu     /* Update local IOVA mapped ranges */
11765039caf3SEugenio Pérez     if (event->type == IOMMU_NOTIFIER_MAP) {
117763b88968SPeter Xu         if (mapped) {
117863b88968SPeter Xu             /* If it's exactly the same translation, skip */
117963b88968SPeter Xu             if (!memcmp(mapped, &target, sizeof(target))) {
118063b88968SPeter Xu                 trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask,
118163b88968SPeter Xu                                                  entry->translated_addr);
118263b88968SPeter Xu                 return 0;
118363b88968SPeter Xu             } else {
118463b88968SPeter Xu                 /*
118563b88968SPeter Xu                  * Translation changed.  Normally this should not
118663b88968SPeter Xu                  * happen, but it can happen when with buggy guest
118763b88968SPeter Xu                  * OSes.  Note that there will be a small window that
118863b88968SPeter Xu                  * we don't have map at all.  But that's the best
118963b88968SPeter Xu                  * effort we can do.  The ideal way to emulate this is
119063b88968SPeter Xu                  * atomically modify the PTE to follow what has
119163b88968SPeter Xu                  * changed, but we can't.  One example is that vfio
119263b88968SPeter Xu                  * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
119363b88968SPeter Xu                  * interface to modify a mapping (meanwhile it seems
119463b88968SPeter Xu                  * meaningless to even provide one).  Anyway, let's
119563b88968SPeter Xu                  * mark this as a TODO in case one day we'll have
119663b88968SPeter Xu                  * a better solution.
119763b88968SPeter Xu                  */
119863b88968SPeter Xu                 IOMMUAccessFlags cache_perm = entry->perm;
119963b88968SPeter Xu                 int ret;
120063b88968SPeter Xu 
120163b88968SPeter Xu                 /* Emulate an UNMAP */
12025039caf3SEugenio Pérez                 event->type = IOMMU_NOTIFIER_UNMAP;
120363b88968SPeter Xu                 entry->perm = IOMMU_NONE;
120463b88968SPeter Xu                 trace_vtd_page_walk_one(info->domain_id,
120563b88968SPeter Xu                                         entry->iova,
120663b88968SPeter Xu                                         entry->translated_addr,
120763b88968SPeter Xu                                         entry->addr_mask,
120863b88968SPeter Xu                                         entry->perm);
12095039caf3SEugenio Pérez                 ret = hook_fn(event, private);
121063b88968SPeter Xu                 if (ret) {
121163b88968SPeter Xu                     return ret;
121263b88968SPeter Xu                 }
121363b88968SPeter Xu                 /* Drop any existing mapping */
121469292a8eSEugenio Pérez                 iova_tree_remove(as->iova_tree, target);
12155039caf3SEugenio Pérez                 /* Recover the correct type */
12165039caf3SEugenio Pérez                 event->type = IOMMU_NOTIFIER_MAP;
121763b88968SPeter Xu                 entry->perm = cache_perm;
121863b88968SPeter Xu             }
121963b88968SPeter Xu         }
122063b88968SPeter Xu         iova_tree_insert(as->iova_tree, &target);
122163b88968SPeter Xu     } else {
122263b88968SPeter Xu         if (!mapped) {
122363b88968SPeter Xu             /* Skip since we didn't map this range at all */
122463b88968SPeter Xu             trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
122563b88968SPeter Xu             return 0;
122663b88968SPeter Xu         }
122769292a8eSEugenio Pérez         iova_tree_remove(as->iova_tree, target);
122863b88968SPeter Xu     }
122963b88968SPeter Xu 
1230d118c06eSPeter Xu     trace_vtd_page_walk_one(info->domain_id, entry->iova,
1231d118c06eSPeter Xu                             entry->translated_addr, entry->addr_mask,
1232d118c06eSPeter Xu                             entry->perm);
12335039caf3SEugenio Pérez     return hook_fn(event, private);
123436d2d52bSPeter Xu }
123536d2d52bSPeter Xu 
1236f06a696dSPeter Xu /**
1237f06a696dSPeter Xu  * vtd_page_walk_level - walk over specific level for IOVA range
1238f06a696dSPeter Xu  *
1239f06a696dSPeter Xu  * @addr: base GPA addr to start the walk
1240f06a696dSPeter Xu  * @start: IOVA range start address
1241f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
1242f06a696dSPeter Xu  * @read: whether parent level has read permission
1243f06a696dSPeter Xu  * @write: whether parent level has write permission
1244fe215b0cSPeter Xu  * @info: constant information for the page walk
1245f06a696dSPeter Xu  */
1246f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
1247fe215b0cSPeter Xu                                uint64_t end, uint32_t level, bool read,
1248fe215b0cSPeter Xu                                bool write, vtd_page_walk_info *info)
1249f06a696dSPeter Xu {
1250f06a696dSPeter Xu     bool read_cur, write_cur, entry_valid;
1251f06a696dSPeter Xu     uint32_t offset;
1252f06a696dSPeter Xu     uint64_t slpte;
1253f06a696dSPeter Xu     uint64_t subpage_size, subpage_mask;
12545039caf3SEugenio Pérez     IOMMUTLBEvent event;
1255f06a696dSPeter Xu     uint64_t iova = start;
1256f06a696dSPeter Xu     uint64_t iova_next;
1257f06a696dSPeter Xu     int ret = 0;
1258f06a696dSPeter Xu 
1259f06a696dSPeter Xu     trace_vtd_page_walk_level(addr, level, start, end);
1260f06a696dSPeter Xu 
1261f06a696dSPeter Xu     subpage_size = 1ULL << vtd_slpt_level_shift(level);
1262f06a696dSPeter Xu     subpage_mask = vtd_slpt_level_page_mask(level);
1263f06a696dSPeter Xu 
1264f06a696dSPeter Xu     while (iova < end) {
1265f06a696dSPeter Xu         iova_next = (iova & subpage_mask) + subpage_size;
1266f06a696dSPeter Xu 
1267f06a696dSPeter Xu         offset = vtd_iova_level_offset(iova, level);
1268f06a696dSPeter Xu         slpte = vtd_get_slpte(addr, offset);
1269f06a696dSPeter Xu 
1270f06a696dSPeter Xu         if (slpte == (uint64_t)-1) {
1271f06a696dSPeter Xu             trace_vtd_page_walk_skip_read(iova, iova_next);
1272f06a696dSPeter Xu             goto next;
1273f06a696dSPeter Xu         }
1274f06a696dSPeter Xu 
1275f06a696dSPeter Xu         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
1276f06a696dSPeter Xu             trace_vtd_page_walk_skip_reserve(iova, iova_next);
1277f06a696dSPeter Xu             goto next;
1278f06a696dSPeter Xu         }
1279f06a696dSPeter Xu 
1280f06a696dSPeter Xu         /* Permissions are stacked with parents' */
1281f06a696dSPeter Xu         read_cur = read && (slpte & VTD_SL_R);
1282f06a696dSPeter Xu         write_cur = write && (slpte & VTD_SL_W);
1283f06a696dSPeter Xu 
1284f06a696dSPeter Xu         /*
1285f06a696dSPeter Xu          * As long as we have either read/write permission, this is a
1286f06a696dSPeter Xu          * valid entry. The rule works for both page entries and page
1287f06a696dSPeter Xu          * table entries.
1288f06a696dSPeter Xu          */
1289f06a696dSPeter Xu         entry_valid = read_cur | write_cur;
1290f06a696dSPeter Xu 
129163b88968SPeter Xu         if (!vtd_is_last_slpte(slpte, level) && entry_valid) {
129263b88968SPeter Xu             /*
129363b88968SPeter Xu              * This is a valid PDE (or even bigger than PDE).  We need
129463b88968SPeter Xu              * to walk one further level.
129563b88968SPeter Xu              */
129663b88968SPeter Xu             ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw),
129763b88968SPeter Xu                                       iova, MIN(iova_next, end), level - 1,
129863b88968SPeter Xu                                       read_cur, write_cur, info);
129963b88968SPeter Xu         } else {
130063b88968SPeter Xu             /*
130163b88968SPeter Xu              * This means we are either:
130263b88968SPeter Xu              *
130363b88968SPeter Xu              * (1) the real page entry (either 4K page, or huge page)
130463b88968SPeter Xu              * (2) the whole range is invalid
130563b88968SPeter Xu              *
130663b88968SPeter Xu              * In either case, we send an IOTLB notification down.
130763b88968SPeter Xu              */
13085039caf3SEugenio Pérez             event.entry.target_as = &address_space_memory;
13095039caf3SEugenio Pérez             event.entry.iova = iova & subpage_mask;
13105039caf3SEugenio Pérez             event.entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
13115039caf3SEugenio Pérez             event.entry.addr_mask = ~subpage_mask;
1312f06a696dSPeter Xu             /* NOTE: this is only meaningful if entry_valid == true */
13135039caf3SEugenio Pérez             event.entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
13145039caf3SEugenio Pérez             event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP :
13155039caf3SEugenio Pérez                                             IOMMU_NOTIFIER_UNMAP;
13165039caf3SEugenio Pérez             ret = vtd_page_walk_one(&event, info);
131763b88968SPeter Xu         }
131863b88968SPeter Xu 
1319f06a696dSPeter Xu         if (ret < 0) {
1320f06a696dSPeter Xu             return ret;
1321f06a696dSPeter Xu         }
1322f06a696dSPeter Xu 
1323f06a696dSPeter Xu next:
1324f06a696dSPeter Xu         iova = iova_next;
1325f06a696dSPeter Xu     }
1326f06a696dSPeter Xu 
1327f06a696dSPeter Xu     return 0;
1328f06a696dSPeter Xu }
1329f06a696dSPeter Xu 
1330f06a696dSPeter Xu /**
1331f06a696dSPeter Xu  * vtd_page_walk - walk specific IOVA range, and call the hook
1332f06a696dSPeter Xu  *
1333fb43cf73SLiu, Yi L  * @s: intel iommu state
1334f06a696dSPeter Xu  * @ce: context entry to walk upon
1335f06a696dSPeter Xu  * @start: IOVA address to start the walk
1336f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
1337fe215b0cSPeter Xu  * @info: page walking information struct
1338f06a696dSPeter Xu  */
1339fb43cf73SLiu, Yi L static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce,
1340fb43cf73SLiu, Yi L                          uint64_t start, uint64_t end,
13411b2b1237SJason Wang                          vtd_page_walk_info *info,
13421b2b1237SJason Wang                          uint32_t pasid)
1343f06a696dSPeter Xu {
13441b2b1237SJason Wang     dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
13451b2b1237SJason Wang     uint32_t level = vtd_get_iova_level(s, ce, pasid);
1346f06a696dSPeter Xu 
13471b2b1237SJason Wang     if (!vtd_iova_range_check(s, start, ce, info->aw, pasid)) {
1348f06a696dSPeter Xu         return -VTD_FR_ADDR_BEYOND_MGAW;
1349f06a696dSPeter Xu     }
1350f06a696dSPeter Xu 
13511b2b1237SJason Wang     if (!vtd_iova_range_check(s, end, ce, info->aw, pasid)) {
1352f06a696dSPeter Xu         /* Fix end so that it reaches the maximum */
13531b2b1237SJason Wang         end = vtd_iova_limit(s, ce, info->aw, pasid);
1354f06a696dSPeter Xu     }
1355f06a696dSPeter Xu 
1356fe215b0cSPeter Xu     return vtd_page_walk_level(addr, start, end, level, true, true, info);
1357f06a696dSPeter Xu }
1358f06a696dSPeter Xu 
1359fb43cf73SLiu, Yi L static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s,
1360fb43cf73SLiu, Yi L                                           VTDRootEntry *re)
1361fb43cf73SLiu, Yi L {
1362fb43cf73SLiu, Yi L     /* Legacy Mode reserved bits check */
1363fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1364fb43cf73SLiu, Yi L         (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1365fb43cf73SLiu, Yi L         goto rsvd_err;
1366fb43cf73SLiu, Yi L 
1367fb43cf73SLiu, Yi L     /* Scalable Mode reserved bits check */
1368fb43cf73SLiu, Yi L     if (s->root_scalable &&
1369fb43cf73SLiu, Yi L         ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) ||
1370fb43cf73SLiu, Yi L          (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1371fb43cf73SLiu, Yi L         goto rsvd_err;
1372fb43cf73SLiu, Yi L 
1373fb43cf73SLiu, Yi L     return 0;
1374fb43cf73SLiu, Yi L 
1375fb43cf73SLiu, Yi L rsvd_err:
1376fb43cf73SLiu, Yi L     error_report_once("%s: invalid root entry: hi=0x%"PRIx64
1377fb43cf73SLiu, Yi L                       ", lo=0x%"PRIx64,
1378fb43cf73SLiu, Yi L                       __func__, re->hi, re->lo);
1379fb43cf73SLiu, Yi L     return -VTD_FR_ROOT_ENTRY_RSVD;
1380fb43cf73SLiu, Yi L }
1381fb43cf73SLiu, Yi L 
1382fb43cf73SLiu, Yi L static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,
1383fb43cf73SLiu, Yi L                                                     VTDContextEntry *ce)
1384fb43cf73SLiu, Yi L {
1385fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1386fb43cf73SLiu, Yi L         (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI ||
1387fb43cf73SLiu, Yi L          ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
1388fb43cf73SLiu, Yi L         error_report_once("%s: invalid context entry: hi=%"PRIx64
1389fb43cf73SLiu, Yi L                           ", lo=%"PRIx64" (reserved nonzero)",
1390fb43cf73SLiu, Yi L                           __func__, ce->hi, ce->lo);
1391fb43cf73SLiu, Yi L         return -VTD_FR_CONTEXT_ENTRY_RSVD;
1392fb43cf73SLiu, Yi L     }
1393fb43cf73SLiu, Yi L 
1394fb43cf73SLiu, Yi L     if (s->root_scalable &&
1395fb43cf73SLiu, Yi L         (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) ||
1396fb43cf73SLiu, Yi L          ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 ||
1397fb43cf73SLiu, Yi L          ce->val[2] ||
1398fb43cf73SLiu, Yi L          ce->val[3])) {
1399fb43cf73SLiu, Yi L         error_report_once("%s: invalid context entry: val[3]=%"PRIx64
1400fb43cf73SLiu, Yi L                           ", val[2]=%"PRIx64
1401fb43cf73SLiu, Yi L                           ", val[1]=%"PRIx64
1402fb43cf73SLiu, Yi L                           ", val[0]=%"PRIx64" (reserved nonzero)",
1403fb43cf73SLiu, Yi L                           __func__, ce->val[3], ce->val[2],
1404fb43cf73SLiu, Yi L                           ce->val[1], ce->val[0]);
1405fb43cf73SLiu, Yi L         return -VTD_FR_CONTEXT_ENTRY_RSVD;
1406fb43cf73SLiu, Yi L     }
1407fb43cf73SLiu, Yi L 
1408fb43cf73SLiu, Yi L     return 0;
1409fb43cf73SLiu, Yi L }
1410fb43cf73SLiu, Yi L 
1411fb43cf73SLiu, Yi L static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
1412fb43cf73SLiu, Yi L                                   VTDContextEntry *ce)
1413fb43cf73SLiu, Yi L {
1414fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1415fb43cf73SLiu, Yi L 
1416fb43cf73SLiu, Yi L     /*
1417fb43cf73SLiu, Yi L      * Make sure in Scalable Mode, a present context entry
1418fb43cf73SLiu, Yi L      * has valid rid2pasid setting, which includes valid
1419fb43cf73SLiu, Yi L      * rid2pasid field and corresponding pasid entry setting
1420fb43cf73SLiu, Yi L      */
14211b2b1237SJason Wang     return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
1422fb43cf73SLiu, Yi L }
1423fb43cf73SLiu, Yi L 
14241da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */
14251da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
14261da12ec4SLe Tan                                     uint8_t devfn, VTDContextEntry *ce)
14271da12ec4SLe Tan {
14281da12ec4SLe Tan     VTDRootEntry re;
14291da12ec4SLe Tan     int ret_fr;
1430f80c9874SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
14311da12ec4SLe Tan 
14321da12ec4SLe Tan     ret_fr = vtd_get_root_entry(s, bus_num, &re);
14331da12ec4SLe Tan     if (ret_fr) {
14341da12ec4SLe Tan         return ret_fr;
14351da12ec4SLe Tan     }
14361da12ec4SLe Tan 
1437fb43cf73SLiu, Yi L     if (!vtd_root_entry_present(s, &re, devfn)) {
14386c441e1dSPeter Xu         /* Not error - it's okay we don't have root entry. */
14396c441e1dSPeter Xu         trace_vtd_re_not_present(bus_num);
14401da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_P;
1441f80c9874SPeter Xu     }
1442f80c9874SPeter Xu 
1443fb43cf73SLiu, Yi L     ret_fr = vtd_root_entry_rsvd_bits_check(s, &re);
1444fb43cf73SLiu, Yi L     if (ret_fr) {
1445fb43cf73SLiu, Yi L         return ret_fr;
14461da12ec4SLe Tan     }
14471da12ec4SLe Tan 
1448fb43cf73SLiu, Yi L     ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce);
14491da12ec4SLe Tan     if (ret_fr) {
14501da12ec4SLe Tan         return ret_fr;
14511da12ec4SLe Tan     }
14521da12ec4SLe Tan 
14538f7d7161SPeter Xu     if (!vtd_ce_present(ce)) {
14546c441e1dSPeter Xu         /* Not error - it's okay we don't have context entry. */
14556c441e1dSPeter Xu         trace_vtd_ce_not_present(bus_num, devfn);
14561da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_P;
1457f80c9874SPeter Xu     }
1458f80c9874SPeter Xu 
1459fb43cf73SLiu, Yi L     ret_fr = vtd_context_entry_rsvd_bits_check(s, ce);
1460fb43cf73SLiu, Yi L     if (ret_fr) {
1461fb43cf73SLiu, Yi L         return ret_fr;
14621da12ec4SLe Tan     }
1463f80c9874SPeter Xu 
14641da12ec4SLe Tan     /* Check if the programming of context-entry is valid */
1465fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1466fb43cf73SLiu, Yi L         !vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
1467095955b2SPeter Xu         error_report_once("%s: invalid context entry: hi=%"PRIx64
1468095955b2SPeter Xu                           ", lo=%"PRIx64" (level %d not supported)",
1469fb43cf73SLiu, Yi L                           __func__, ce->hi, ce->lo,
1470fb43cf73SLiu, Yi L                           vtd_ce_get_level(ce));
14711da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
1472f80c9874SPeter Xu     }
1473f80c9874SPeter Xu 
1474fb43cf73SLiu, Yi L     if (!s->root_scalable) {
1475f80c9874SPeter Xu         /* Do translation type check */
1476f80c9874SPeter Xu         if (!vtd_ce_type_check(x86_iommu, ce)) {
1477095955b2SPeter Xu             /* Errors dumped in vtd_ce_type_check() */
14781da12ec4SLe Tan             return -VTD_FR_CONTEXT_ENTRY_INV;
14791da12ec4SLe Tan         }
1480fb43cf73SLiu, Yi L     } else {
1481fb43cf73SLiu, Yi L         /*
1482fb43cf73SLiu, Yi L          * Check if the programming of context-entry.rid2pasid
1483fb43cf73SLiu, Yi L          * and corresponding pasid setting is valid, and thus
1484fb43cf73SLiu, Yi L          * avoids to check pasid entry fetching result in future
1485fb43cf73SLiu, Yi L          * helper function calling.
1486fb43cf73SLiu, Yi L          */
1487fb43cf73SLiu, Yi L         ret_fr = vtd_ce_rid2pasid_check(s, ce);
1488fb43cf73SLiu, Yi L         if (ret_fr) {
1489fb43cf73SLiu, Yi L             return ret_fr;
1490fb43cf73SLiu, Yi L         }
1491fb43cf73SLiu, Yi L     }
1492f80c9874SPeter Xu 
14931da12ec4SLe Tan     return 0;
14941da12ec4SLe Tan }
14951da12ec4SLe Tan 
14965039caf3SEugenio Pérez static int vtd_sync_shadow_page_hook(IOMMUTLBEvent *event,
149763b88968SPeter Xu                                      void *private)
149863b88968SPeter Xu {
14995039caf3SEugenio Pérez     memory_region_notify_iommu(private, 0, *event);
150063b88968SPeter Xu     return 0;
150163b88968SPeter Xu }
150263b88968SPeter Xu 
1503fb43cf73SLiu, Yi L static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
15041b2b1237SJason Wang                                   VTDContextEntry *ce,
15051b2b1237SJason Wang                                   uint32_t pasid)
1506fb43cf73SLiu, Yi L {
1507fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1508fb43cf73SLiu, Yi L 
1509fb43cf73SLiu, Yi L     if (s->root_scalable) {
15101b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
1511fb43cf73SLiu, Yi L         return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
1512fb43cf73SLiu, Yi L     }
1513fb43cf73SLiu, Yi L 
1514fb43cf73SLiu, Yi L     return VTD_CONTEXT_ENTRY_DID(ce->hi);
1515fb43cf73SLiu, Yi L }
1516fb43cf73SLiu, Yi L 
151763b88968SPeter Xu static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
151863b88968SPeter Xu                                             VTDContextEntry *ce,
151963b88968SPeter Xu                                             hwaddr addr, hwaddr size)
152063b88968SPeter Xu {
152163b88968SPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
152263b88968SPeter Xu     vtd_page_walk_info info = {
152363b88968SPeter Xu         .hook_fn = vtd_sync_shadow_page_hook,
152463b88968SPeter Xu         .private = (void *)&vtd_as->iommu,
152563b88968SPeter Xu         .notify_unmap = true,
152663b88968SPeter Xu         .aw = s->aw_bits,
152763b88968SPeter Xu         .as = vtd_as,
15281b2b1237SJason Wang         .domain_id = vtd_get_domain_id(s, ce, vtd_as->pasid),
152963b88968SPeter Xu     };
153063b88968SPeter Xu 
15311b2b1237SJason Wang     return vtd_page_walk(s, ce, addr, addr + size, &info, vtd_as->pasid);
153263b88968SPeter Xu }
153363b88968SPeter Xu 
15343e090e34SPeter Xu static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
153563b88968SPeter Xu {
153695ecd3dfSPeter Xu     int ret;
153795ecd3dfSPeter Xu     VTDContextEntry ce;
1538c28b535dSPeter Xu     IOMMUNotifier *n;
153995ecd3dfSPeter Xu 
15403e090e34SPeter Xu     /* If no MAP notifier registered, we simply invalidate all the cache */
15413e090e34SPeter Xu     if (!vtd_as_has_map_notifier(vtd_as)) {
15423e090e34SPeter Xu         IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
15433e090e34SPeter Xu             memory_region_unmap_iommu_notifier_range(n);
15443e090e34SPeter Xu         }
1545f7701e2cSEugenio Pérez         return 0;
1546f7701e2cSEugenio Pérez     }
1547f7701e2cSEugenio Pérez 
154895ecd3dfSPeter Xu     ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
154995ecd3dfSPeter Xu                                    pci_bus_num(vtd_as->bus),
155095ecd3dfSPeter Xu                                    vtd_as->devfn, &ce);
155195ecd3dfSPeter Xu     if (ret) {
1552c28b535dSPeter Xu         if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
1553c28b535dSPeter Xu             /*
1554c28b535dSPeter Xu              * It's a valid scenario to have a context entry that is
1555c28b535dSPeter Xu              * not present.  For example, when a device is removed
1556c28b535dSPeter Xu              * from an existing domain then the context entry will be
1557c28b535dSPeter Xu              * zeroed by the guest before it was put into another
1558c28b535dSPeter Xu              * domain.  When this happens, instead of synchronizing
1559c28b535dSPeter Xu              * the shadow pages we should invalidate all existing
1560c28b535dSPeter Xu              * mappings and notify the backends.
1561c28b535dSPeter Xu              */
1562c28b535dSPeter Xu             IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
1563c28b535dSPeter Xu                 vtd_address_space_unmap(vtd_as, n);
1564c28b535dSPeter Xu             }
1565c28b535dSPeter Xu             ret = 0;
1566c28b535dSPeter Xu         }
156795ecd3dfSPeter Xu         return ret;
156895ecd3dfSPeter Xu     }
156995ecd3dfSPeter Xu 
157095ecd3dfSPeter Xu     return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX);
157163b88968SPeter Xu }
157263b88968SPeter Xu 
1573dbaabb25SPeter Xu /*
157437557b09SCai Huoqing  * Check if specific device is configured to bypass address
1575fb43cf73SLiu, Yi L  * translation for DMA requests. In Scalable Mode, bypass
1576fb43cf73SLiu, Yi L  * 1st-level translation or 2nd-level translation, it depends
1577fb43cf73SLiu, Yi L  * on PGTT setting.
1578dbaabb25SPeter Xu  */
15791b2b1237SJason Wang static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
15801b2b1237SJason Wang                                uint32_t pasid)
15815178d78fSJason Wang {
15825178d78fSJason Wang     VTDPASIDEntry pe;
15835178d78fSJason Wang     int ret;
15845178d78fSJason Wang 
15855178d78fSJason Wang     if (s->root_scalable) {
15861b2b1237SJason Wang         ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
15875178d78fSJason Wang         if (ret) {
1588fb1d084bSJason Wang             /*
1589fb1d084bSJason Wang              * This error is guest triggerable. We should assumt PT
1590fb1d084bSJason Wang              * not enabled for safety.
1591fb1d084bSJason Wang              */
15925178d78fSJason Wang             return false;
15935178d78fSJason Wang         }
15945178d78fSJason Wang         return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
15955178d78fSJason Wang     }
15965178d78fSJason Wang 
15975178d78fSJason Wang     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
15985178d78fSJason Wang 
15995178d78fSJason Wang }
16005178d78fSJason Wang 
16015178d78fSJason Wang static bool vtd_as_pt_enabled(VTDAddressSpace *as)
1602dbaabb25SPeter Xu {
1603dbaabb25SPeter Xu     IntelIOMMUState *s;
1604dbaabb25SPeter Xu     VTDContextEntry ce;
1605dbaabb25SPeter Xu 
1606dbaabb25SPeter Xu     assert(as);
1607dbaabb25SPeter Xu 
1608fb43cf73SLiu, Yi L     s = as->iommu_state;
1609fb1d084bSJason Wang     if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
1610fb1d084bSJason Wang                                  &ce)) {
1611dbaabb25SPeter Xu         /*
1612dbaabb25SPeter Xu          * Possibly failed to parse the context entry for some reason
1613dbaabb25SPeter Xu          * (e.g., during init, or any guest configuration errors on
1614dbaabb25SPeter Xu          * context entries). We should assume PT not enabled for
1615dbaabb25SPeter Xu          * safety.
1616dbaabb25SPeter Xu          */
1617dbaabb25SPeter Xu         return false;
1618dbaabb25SPeter Xu     }
1619dbaabb25SPeter Xu 
16201b2b1237SJason Wang     return vtd_dev_pt_enabled(s, &ce, as->pasid);
1621dbaabb25SPeter Xu }
1622dbaabb25SPeter Xu 
1623dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */
1624dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as)
1625dbaabb25SPeter Xu {
16261b2b1237SJason Wang     bool use_iommu, pt;
162766a4a031SPeter Xu     /* Whether we need to take the BQL on our own */
162866a4a031SPeter Xu     bool take_bql = !qemu_mutex_iothread_locked();
1629dbaabb25SPeter Xu 
1630dbaabb25SPeter Xu     assert(as);
1631dbaabb25SPeter Xu 
16325178d78fSJason Wang     use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as);
16331b2b1237SJason Wang     pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);
1634dbaabb25SPeter Xu 
1635dbaabb25SPeter Xu     trace_vtd_switch_address_space(pci_bus_num(as->bus),
1636dbaabb25SPeter Xu                                    VTD_PCI_SLOT(as->devfn),
1637dbaabb25SPeter Xu                                    VTD_PCI_FUNC(as->devfn),
1638dbaabb25SPeter Xu                                    use_iommu);
1639dbaabb25SPeter Xu 
164066a4a031SPeter Xu     /*
164166a4a031SPeter Xu      * It's possible that we reach here without BQL, e.g., when called
164266a4a031SPeter Xu      * from vtd_pt_enable_fast_path(). However the memory APIs need
164366a4a031SPeter Xu      * it. We'd better make sure we have had it already, or, take it.
164466a4a031SPeter Xu      */
164566a4a031SPeter Xu     if (take_bql) {
164666a4a031SPeter Xu         qemu_mutex_lock_iothread();
164766a4a031SPeter Xu     }
164866a4a031SPeter Xu 
1649dbaabb25SPeter Xu     /* Turn off first then on the other */
1650dbaabb25SPeter Xu     if (use_iommu) {
16514b519ef1SPeter Xu         memory_region_set_enabled(&as->nodmar, false);
16523df9d748SAlexey Kardashevskiy         memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
16531b2b1237SJason Wang         /*
16541b2b1237SJason Wang          * vt-d spec v3.4 3.14:
16551b2b1237SJason Wang          *
16561b2b1237SJason Wang          * """
16571b2b1237SJason Wang          * Requests-with-PASID with input address in range 0xFEEx_xxxx
16581b2b1237SJason Wang          * are translated normally like any other request-with-PASID
16591b2b1237SJason Wang          * through DMA-remapping hardware.
16601b2b1237SJason Wang          * """
16611b2b1237SJason Wang          *
16621b2b1237SJason Wang          * Need to disable ir for as with PASID.
16631b2b1237SJason Wang          */
16641b2b1237SJason Wang         if (as->pasid != PCI_NO_PASID) {
16651b2b1237SJason Wang             memory_region_set_enabled(&as->iommu_ir, false);
16661b2b1237SJason Wang         } else {
16671b2b1237SJason Wang             memory_region_set_enabled(&as->iommu_ir, true);
16681b2b1237SJason Wang         }
1669dbaabb25SPeter Xu     } else {
16703df9d748SAlexey Kardashevskiy         memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
16714b519ef1SPeter Xu         memory_region_set_enabled(&as->nodmar, true);
1672dbaabb25SPeter Xu     }
1673dbaabb25SPeter Xu 
16741b2b1237SJason Wang     /*
16751b2b1237SJason Wang      * vtd-spec v3.4 3.14:
16761b2b1237SJason Wang      *
16771b2b1237SJason Wang      * """
16781b2b1237SJason Wang      * Requests-with-PASID with input address in range 0xFEEx_xxxx are
16791b2b1237SJason Wang      * translated normally like any other request-with-PASID through
16801b2b1237SJason Wang      * DMA-remapping hardware. However, if such a request is processed
16811b2b1237SJason Wang      * using pass-through translation, it will be blocked as described
16821b2b1237SJason Wang      * in the paragraph below.
16831b2b1237SJason Wang      *
16841b2b1237SJason Wang      * Software must not program paging-structure entries to remap any
16851b2b1237SJason Wang      * address to the interrupt address range. Untranslated requests
16861b2b1237SJason Wang      * and translation requests that result in an address in the
16871b2b1237SJason Wang      * interrupt range will be blocked with condition code LGN.4 or
16881b2b1237SJason Wang      * SGN.8.
16891b2b1237SJason Wang      * """
16901b2b1237SJason Wang      *
16911b2b1237SJason Wang      * We enable per as memory region (iommu_ir_fault) for catching
16921b2b1237SJason Wang      * the tranlsation for interrupt range through PASID + PT.
16931b2b1237SJason Wang      */
16941b2b1237SJason Wang     if (pt && as->pasid != PCI_NO_PASID) {
16951b2b1237SJason Wang         memory_region_set_enabled(&as->iommu_ir_fault, true);
16961b2b1237SJason Wang     } else {
16971b2b1237SJason Wang         memory_region_set_enabled(&as->iommu_ir_fault, false);
16981b2b1237SJason Wang     }
16991b2b1237SJason Wang 
170066a4a031SPeter Xu     if (take_bql) {
170166a4a031SPeter Xu         qemu_mutex_unlock_iothread();
170266a4a031SPeter Xu     }
170366a4a031SPeter Xu 
1704dbaabb25SPeter Xu     return use_iommu;
1705dbaabb25SPeter Xu }
1706dbaabb25SPeter Xu 
1707dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s)
1708dbaabb25SPeter Xu {
1709da8d439cSJason Wang     VTDAddressSpace *vtd_as;
1710dbaabb25SPeter Xu     GHashTableIter iter;
1711dbaabb25SPeter Xu 
1712da8d439cSJason Wang     g_hash_table_iter_init(&iter, s->vtd_address_spaces);
1713da8d439cSJason Wang     while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_as)) {
1714da8d439cSJason Wang         vtd_switch_address_space(vtd_as);
1715dbaabb25SPeter Xu     }
17161da12ec4SLe Tan }
17171da12ec4SLe Tan 
17181da12ec4SLe Tan static const bool vtd_qualified_faults[] = {
17191da12ec4SLe Tan     [VTD_FR_RESERVED] = false,
17201da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_P] = false,
17211da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_P] = true,
17221da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_INV] = true,
17231da12ec4SLe Tan     [VTD_FR_ADDR_BEYOND_MGAW] = true,
17241da12ec4SLe Tan     [VTD_FR_WRITE] = true,
17251da12ec4SLe Tan     [VTD_FR_READ] = true,
17261da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_INV] = true,
17271da12ec4SLe Tan     [VTD_FR_ROOT_TABLE_INV] = false,
17281da12ec4SLe Tan     [VTD_FR_CONTEXT_TABLE_INV] = false,
1729ea97a1bdSJason Wang     [VTD_FR_INTERRUPT_ADDR] = true,
17301da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_RSVD] = false,
17311da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_RSVD] = true,
17321da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_TT] = true,
1733fb43cf73SLiu, Yi L     [VTD_FR_PASID_TABLE_INV] = false,
1734ea97a1bdSJason Wang     [VTD_FR_SM_INTERRUPT_ADDR] = true,
17351da12ec4SLe Tan     [VTD_FR_MAX] = false,
17361da12ec4SLe Tan };
17371da12ec4SLe Tan 
17381da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software
17391da12ec4SLe Tan  * only if the FPD field in the context-entry used to process the faulting
17401da12ec4SLe Tan  * request is 0.
17411da12ec4SLe Tan  */
17421da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
17431da12ec4SLe Tan {
17441da12ec4SLe Tan     return vtd_qualified_faults[fault];
17451da12ec4SLe Tan }
17461da12ec4SLe Tan 
17471da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr)
17481da12ec4SLe Tan {
17491da12ec4SLe Tan     return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
17501da12ec4SLe Tan }
17511da12ec4SLe Tan 
1752da8d439cSJason Wang static gboolean vtd_find_as_by_sid(gpointer key, gpointer value,
1753da8d439cSJason Wang                                    gpointer user_data)
1754da8d439cSJason Wang {
1755da8d439cSJason Wang     struct vtd_as_key *as_key = (struct vtd_as_key *)key;
1756da8d439cSJason Wang     uint16_t target_sid = *(uint16_t *)user_data;
1757da8d439cSJason Wang     uint16_t sid = PCI_BUILD_BDF(pci_bus_num(as_key->bus), as_key->devfn);
1758da8d439cSJason Wang     return sid == target_sid;
1759da8d439cSJason Wang }
1760da8d439cSJason Wang 
1761da8d439cSJason Wang static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
1762da8d439cSJason Wang {
1763da8d439cSJason Wang     uint8_t bus_num = PCI_BUS_NUM(sid);
1764da8d439cSJason Wang     VTDAddressSpace *vtd_as = s->vtd_as_cache[bus_num];
1765da8d439cSJason Wang 
1766da8d439cSJason Wang     if (vtd_as &&
1767da8d439cSJason Wang         (sid == PCI_BUILD_BDF(pci_bus_num(vtd_as->bus), vtd_as->devfn))) {
1768da8d439cSJason Wang         return vtd_as;
1769da8d439cSJason Wang     }
1770da8d439cSJason Wang 
1771da8d439cSJason Wang     vtd_as = g_hash_table_find(s->vtd_address_spaces, vtd_find_as_by_sid, &sid);
1772da8d439cSJason Wang     s->vtd_as_cache[bus_num] = vtd_as;
1773da8d439cSJason Wang 
1774da8d439cSJason Wang     return vtd_as;
1775da8d439cSJason Wang }
1776da8d439cSJason Wang 
1777dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1778dbaabb25SPeter Xu {
1779dbaabb25SPeter Xu     VTDAddressSpace *vtd_as;
1780dbaabb25SPeter Xu     bool success = false;
1781dbaabb25SPeter Xu 
1782da8d439cSJason Wang     vtd_as = vtd_get_as_by_sid(s, source_id);
1783dbaabb25SPeter Xu     if (!vtd_as) {
1784dbaabb25SPeter Xu         goto out;
1785dbaabb25SPeter Xu     }
1786dbaabb25SPeter Xu 
1787dbaabb25SPeter Xu     if (vtd_switch_address_space(vtd_as) == false) {
1788dbaabb25SPeter Xu         /* We switched off IOMMU region successfully. */
1789dbaabb25SPeter Xu         success = true;
1790dbaabb25SPeter Xu     }
1791dbaabb25SPeter Xu 
1792dbaabb25SPeter Xu out:
1793dbaabb25SPeter Xu     trace_vtd_pt_enable_fast_path(source_id, success);
1794dbaabb25SPeter Xu }
1795dbaabb25SPeter Xu 
1796940e5527SJason Wang static void vtd_report_fault(IntelIOMMUState *s,
1797940e5527SJason Wang                              int err, bool is_fpd_set,
1798940e5527SJason Wang                              uint16_t source_id,
1799940e5527SJason Wang                              hwaddr addr,
18001b2b1237SJason Wang                              bool is_write,
18011b2b1237SJason Wang                              bool is_pasid,
18021b2b1237SJason Wang                              uint32_t pasid)
1803940e5527SJason Wang {
1804940e5527SJason Wang     if (is_fpd_set && vtd_is_qualified_fault(err)) {
1805940e5527SJason Wang         trace_vtd_fault_disabled();
1806940e5527SJason Wang     } else {
18071b2b1237SJason Wang         vtd_report_dmar_fault(s, source_id, addr, err, is_write,
18081b2b1237SJason Wang                               is_pasid, pasid);
1809940e5527SJason Wang     }
1810940e5527SJason Wang }
1811940e5527SJason Wang 
18121da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu
18131da12ec4SLe Tan  * translation.
181479e2b9aeSPaolo Bonzini  *
181579e2b9aeSPaolo Bonzini  * Called from RCU critical section.
181679e2b9aeSPaolo Bonzini  *
18171da12ec4SLe Tan  * @bus_num: The bus number
18181da12ec4SLe Tan  * @devfn: The devfn, which is the  combined of device and function number
18191da12ec4SLe Tan  * @is_write: The access is a write operation
18201da12ec4SLe Tan  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
1821b9313021SPeter Xu  *
1822b9313021SPeter Xu  * Returns true if translation is successful, otherwise false.
18231da12ec4SLe Tan  */
1824b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
18251da12ec4SLe Tan                                    uint8_t devfn, hwaddr addr, bool is_write,
18261da12ec4SLe Tan                                    IOMMUTLBEntry *entry)
18271da12ec4SLe Tan {
1828d92fa2dcSLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
18291da12ec4SLe Tan     VTDContextEntry ce;
18307df953bdSKnut Omang     uint8_t bus_num = pci_bus_num(bus);
18311d9efa73SPeter Xu     VTDContextCacheEntry *cc_entry;
1832d66b969bSJason Wang     uint64_t slpte, page_mask;
18331b2b1237SJason Wang     uint32_t level, pasid = vtd_as->pasid;
1834da8d439cSJason Wang     uint16_t source_id = PCI_BUILD_BDF(bus_num, devfn);
18351da12ec4SLe Tan     int ret_fr;
18361da12ec4SLe Tan     bool is_fpd_set = false;
18371da12ec4SLe Tan     bool reads = true;
18381da12ec4SLe Tan     bool writes = true;
183907f7b733SPeter Xu     uint8_t access_flags;
18401b2b1237SJason Wang     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
1841b5a280c0SLe Tan     VTDIOTLBEntry *iotlb_entry;
18421da12ec4SLe Tan 
1843046ab7e9SPeter Xu     /*
1844046ab7e9SPeter Xu      * We have standalone memory region for interrupt addresses, we
1845046ab7e9SPeter Xu      * should never receive translation requests in this region.
18461da12ec4SLe Tan      */
1847046ab7e9SPeter Xu     assert(!vtd_is_interrupt_addr(addr));
1848046ab7e9SPeter Xu 
18491d9efa73SPeter Xu     vtd_iommu_lock(s);
18501d9efa73SPeter Xu 
18511d9efa73SPeter Xu     cc_entry = &vtd_as->context_cache_entry;
18521d9efa73SPeter Xu 
18531b2b1237SJason Wang     /* Try to fetch slpte form IOTLB, we don't need RID2PASID logic */
18541b2b1237SJason Wang     if (!rid2pasid) {
18551b2b1237SJason Wang         iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
1856b5a280c0SLe Tan         if (iotlb_entry) {
18576c441e1dSPeter Xu             trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
18586c441e1dSPeter Xu                                      iotlb_entry->domain_id);
1859b5a280c0SLe Tan             slpte = iotlb_entry->slpte;
186007f7b733SPeter Xu             access_flags = iotlb_entry->access_flags;
1861d66b969bSJason Wang             page_mask = iotlb_entry->mask;
1862b5a280c0SLe Tan             goto out;
1863b5a280c0SLe Tan         }
18641b2b1237SJason Wang     }
1865b9313021SPeter Xu 
1866d92fa2dcSLe Tan     /* Try to fetch context-entry from cache first */
1867d92fa2dcSLe Tan     if (cc_entry->context_cache_gen == s->context_cache_gen) {
18686c441e1dSPeter Xu         trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
18696c441e1dSPeter Xu                                cc_entry->context_entry.lo,
18706c441e1dSPeter Xu                                cc_entry->context_cache_gen);
1871d92fa2dcSLe Tan         ce = cc_entry->context_entry;
1872d92fa2dcSLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1873fb43cf73SLiu, Yi L         if (!is_fpd_set && s->root_scalable) {
18741b2b1237SJason Wang             ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
1875940e5527SJason Wang             if (ret_fr) {
1876940e5527SJason Wang                 vtd_report_fault(s, -ret_fr, is_fpd_set,
18771b2b1237SJason Wang                                  source_id, addr, is_write,
18781b2b1237SJason Wang                                  false, 0);
1879940e5527SJason Wang                 goto error;
1880940e5527SJason Wang             }
1881fb43cf73SLiu, Yi L         }
1882d92fa2dcSLe Tan     } else {
18831da12ec4SLe Tan         ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
18841da12ec4SLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1885fb43cf73SLiu, Yi L         if (!ret_fr && !is_fpd_set && s->root_scalable) {
18861b2b1237SJason Wang             ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
18871da12ec4SLe Tan         }
1888940e5527SJason Wang         if (ret_fr) {
1889940e5527SJason Wang             vtd_report_fault(s, -ret_fr, is_fpd_set,
18901b2b1237SJason Wang                              source_id, addr, is_write,
18911b2b1237SJason Wang                              false, 0);
1892940e5527SJason Wang             goto error;
1893940e5527SJason Wang         }
1894d92fa2dcSLe Tan         /* Update context-cache */
18956c441e1dSPeter Xu         trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
18966c441e1dSPeter Xu                                   cc_entry->context_cache_gen,
18976c441e1dSPeter Xu                                   s->context_cache_gen);
1898d92fa2dcSLe Tan         cc_entry->context_entry = ce;
1899d92fa2dcSLe Tan         cc_entry->context_cache_gen = s->context_cache_gen;
1900d92fa2dcSLe Tan     }
19011da12ec4SLe Tan 
19021b2b1237SJason Wang     if (rid2pasid) {
19031b2b1237SJason Wang         pasid = VTD_CE_GET_RID2PASID(&ce);
19041b2b1237SJason Wang     }
19051b2b1237SJason Wang 
1906dbaabb25SPeter Xu     /*
1907dbaabb25SPeter Xu      * We don't need to translate for pass-through context entries.
1908dbaabb25SPeter Xu      * Also, let's ignore IOTLB caching as well for PT devices.
1909dbaabb25SPeter Xu      */
19101b2b1237SJason Wang     if (vtd_dev_pt_enabled(s, &ce, pasid)) {
1911892721d9SPeter Xu         entry->iova = addr & VTD_PAGE_MASK_4K;
1912dbaabb25SPeter Xu         entry->translated_addr = entry->iova;
1913892721d9SPeter Xu         entry->addr_mask = ~VTD_PAGE_MASK_4K;
1914dbaabb25SPeter Xu         entry->perm = IOMMU_RW;
1915dbaabb25SPeter Xu         trace_vtd_translate_pt(source_id, entry->iova);
1916dbaabb25SPeter Xu 
1917dbaabb25SPeter Xu         /*
1918dbaabb25SPeter Xu          * When this happens, it means firstly caching-mode is not
1919dbaabb25SPeter Xu          * enabled, and this is the first passthrough translation for
1920dbaabb25SPeter Xu          * the device. Let's enable the fast path for passthrough.
1921dbaabb25SPeter Xu          *
1922dbaabb25SPeter Xu          * When passthrough is disabled again for the device, we can
1923dbaabb25SPeter Xu          * capture it via the context entry invalidation, then the
1924dbaabb25SPeter Xu          * IOMMU region can be swapped back.
1925dbaabb25SPeter Xu          */
1926dbaabb25SPeter Xu         vtd_pt_enable_fast_path(s, source_id);
19271d9efa73SPeter Xu         vtd_iommu_unlock(s);
1928b9313021SPeter Xu         return true;
1929dbaabb25SPeter Xu     }
1930dbaabb25SPeter Xu 
19311b2b1237SJason Wang     /* Try to fetch slpte form IOTLB for RID2PASID slow path */
19321b2b1237SJason Wang     if (rid2pasid) {
19331b2b1237SJason Wang         iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
19341b2b1237SJason Wang         if (iotlb_entry) {
19351b2b1237SJason Wang             trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
19361b2b1237SJason Wang                                      iotlb_entry->domain_id);
19371b2b1237SJason Wang             slpte = iotlb_entry->slpte;
19381b2b1237SJason Wang             access_flags = iotlb_entry->access_flags;
19391b2b1237SJason Wang             page_mask = iotlb_entry->mask;
19401b2b1237SJason Wang             goto out;
19411b2b1237SJason Wang         }
19421b2b1237SJason Wang     }
19431b2b1237SJason Wang 
1944fb43cf73SLiu, Yi L     ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level,
19451b2b1237SJason Wang                                &reads, &writes, s->aw_bits, pasid);
1946940e5527SJason Wang     if (ret_fr) {
1947940e5527SJason Wang         vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
19481b2b1237SJason Wang                          addr, is_write, pasid != PCI_NO_PASID, pasid);
1949940e5527SJason Wang         goto error;
1950940e5527SJason Wang     }
19511da12ec4SLe Tan 
1952d66b969bSJason Wang     page_mask = vtd_slpt_level_page_mask(level);
195307f7b733SPeter Xu     access_flags = IOMMU_ACCESS_FLAG(reads, writes);
19541b2b1237SJason Wang     vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid),
19551b2b1237SJason Wang                      addr, slpte, access_flags, level, pasid);
1956b5a280c0SLe Tan out:
19571d9efa73SPeter Xu     vtd_iommu_unlock(s);
1958d66b969bSJason Wang     entry->iova = addr & page_mask;
195937f51384SPrasad Singamsetty     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
1960d66b969bSJason Wang     entry->addr_mask = ~page_mask;
196107f7b733SPeter Xu     entry->perm = access_flags;
1962b9313021SPeter Xu     return true;
1963b9313021SPeter Xu 
1964b9313021SPeter Xu error:
19651d9efa73SPeter Xu     vtd_iommu_unlock(s);
1966b9313021SPeter Xu     entry->iova = 0;
1967b9313021SPeter Xu     entry->translated_addr = 0;
1968b9313021SPeter Xu     entry->addr_mask = 0;
1969b9313021SPeter Xu     entry->perm = IOMMU_NONE;
1970b9313021SPeter Xu     return false;
19711da12ec4SLe Tan }
19721da12ec4SLe Tan 
19731da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s)
19741da12ec4SLe Tan {
19751da12ec4SLe Tan     s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
197637f51384SPrasad Singamsetty     s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
19771da12ec4SLe Tan 
19782811af3bSPeter Xu     vtd_update_scalable_state(s);
19792811af3bSPeter Xu 
198081fb1e64SPeter Xu     trace_vtd_reg_dmar_root(s->root, s->root_scalable);
19811da12ec4SLe Tan }
19821da12ec4SLe Tan 
198302a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
198402a2cbc8SPeter Xu                                uint32_t index, uint32_t mask)
198502a2cbc8SPeter Xu {
198602a2cbc8SPeter Xu     x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
198702a2cbc8SPeter Xu }
198802a2cbc8SPeter Xu 
1989a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1990a5861439SPeter Xu {
1991a5861439SPeter Xu     uint64_t value = 0;
1992a5861439SPeter Xu     value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1993a5861439SPeter Xu     s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
199437f51384SPrasad Singamsetty     s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
199528589311SJan Kiszka     s->intr_eime = value & VTD_IRTA_EIME;
1996a5861439SPeter Xu 
199702a2cbc8SPeter Xu     /* Notify global invalidation */
199802a2cbc8SPeter Xu     vtd_iec_notify_all(s, true, 0, 0);
1999a5861439SPeter Xu 
20007feb51b7SPeter Xu     trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
2001a5861439SPeter Xu }
2002a5861439SPeter Xu 
2003dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s)
2004dd4d607eSPeter Xu {
2005b4a4ba0dSPeter Xu     VTDAddressSpace *vtd_as;
2006dd4d607eSPeter Xu 
2007b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
20083e090e34SPeter Xu         vtd_address_space_sync(vtd_as);
2009dd4d607eSPeter Xu     }
2010dd4d607eSPeter Xu }
2011dd4d607eSPeter Xu 
2012d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s)
2013d92fa2dcSLe Tan {
2014bc535e59SPeter Xu     trace_vtd_inv_desc_cc_global();
20151d9efa73SPeter Xu     /* Protects context cache */
20161d9efa73SPeter Xu     vtd_iommu_lock(s);
2017d92fa2dcSLe Tan     s->context_cache_gen++;
2018d92fa2dcSLe Tan     if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
20191d9efa73SPeter Xu         vtd_reset_context_cache_locked(s);
2020d92fa2dcSLe Tan     }
20211d9efa73SPeter Xu     vtd_iommu_unlock(s);
20222cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
2023dd4d607eSPeter Xu     /*
2024dd4d607eSPeter Xu      * From VT-d spec 6.5.2.1, a global context entry invalidation
2025dd4d607eSPeter Xu      * should be followed by a IOTLB global invalidation, so we should
2026dd4d607eSPeter Xu      * be safe even without this. Hoewever, let's replay the region as
2027dd4d607eSPeter Xu      * well to be safer, and go back here when we need finer tunes for
2028dd4d607eSPeter Xu      * VT-d emulation codes.
2029dd4d607eSPeter Xu      */
2030dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
2031d92fa2dcSLe Tan }
2032d92fa2dcSLe Tan 
2033d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation.
2034d92fa2dcSLe Tan  * @func_mask: FM field after shifting
2035d92fa2dcSLe Tan  */
2036d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s,
2037d92fa2dcSLe Tan                                           uint16_t source_id,
2038d92fa2dcSLe Tan                                           uint16_t func_mask)
2039d92fa2dcSLe Tan {
2040da8d439cSJason Wang     GHashTableIter as_it;
2041d92fa2dcSLe Tan     uint16_t mask;
2042d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
2043bc535e59SPeter Xu     uint8_t bus_n, devfn;
2044d92fa2dcSLe Tan 
2045bc535e59SPeter Xu     trace_vtd_inv_desc_cc_devices(source_id, func_mask);
2046bc535e59SPeter Xu 
2047d92fa2dcSLe Tan     switch (func_mask & 3) {
2048d92fa2dcSLe Tan     case 0:
2049d92fa2dcSLe Tan         mask = 0;   /* No bits in the SID field masked */
2050d92fa2dcSLe Tan         break;
2051d92fa2dcSLe Tan     case 1:
2052d92fa2dcSLe Tan         mask = 4;   /* Mask bit 2 in the SID field */
2053d92fa2dcSLe Tan         break;
2054d92fa2dcSLe Tan     case 2:
2055d92fa2dcSLe Tan         mask = 6;   /* Mask bit 2:1 in the SID field */
2056d92fa2dcSLe Tan         break;
2057d92fa2dcSLe Tan     case 3:
2058d92fa2dcSLe Tan         mask = 7;   /* Mask bit 2:0 in the SID field */
2059d92fa2dcSLe Tan         break;
206041ce9a91SEric Auger     default:
206141ce9a91SEric Auger         g_assert_not_reached();
2062d92fa2dcSLe Tan     }
20636cb99accSPeter Xu     mask = ~mask;
2064bc535e59SPeter Xu 
2065bc535e59SPeter Xu     bus_n = VTD_SID_TO_BUS(source_id);
2066d92fa2dcSLe Tan     devfn = VTD_SID_TO_DEVFN(source_id);
2067da8d439cSJason Wang 
2068da8d439cSJason Wang     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
2069da8d439cSJason Wang     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
2070da8d439cSJason Wang         if ((pci_bus_num(vtd_as->bus) == bus_n) &&
2071da8d439cSJason Wang             (vtd_as->devfn & mask) == (devfn & mask)) {
2072da8d439cSJason Wang             trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(vtd_as->devfn),
2073da8d439cSJason Wang                                          VTD_PCI_FUNC(vtd_as->devfn));
20741d9efa73SPeter Xu             vtd_iommu_lock(s);
2075d92fa2dcSLe Tan             vtd_as->context_cache_entry.context_cache_gen = 0;
20761d9efa73SPeter Xu             vtd_iommu_unlock(s);
2077dd4d607eSPeter Xu             /*
2078dbaabb25SPeter Xu              * Do switch address space when needed, in case if the
2079dbaabb25SPeter Xu              * device passthrough bit is switched.
2080dbaabb25SPeter Xu              */
2081dbaabb25SPeter Xu             vtd_switch_address_space(vtd_as);
2082dbaabb25SPeter Xu             /*
2083dd4d607eSPeter Xu              * So a device is moving out of (or moving into) a
208463b88968SPeter Xu              * domain, resync the shadow page table.
2085dd4d607eSPeter Xu              * This won't bring bad even if we have no such
2086dd4d607eSPeter Xu              * notifier registered - the IOMMU notification
2087dd4d607eSPeter Xu              * framework will skip MAP notifications if that
2088dd4d607eSPeter Xu              * happened.
2089dd4d607eSPeter Xu              */
20903e090e34SPeter Xu             vtd_address_space_sync(vtd_as);
2091d92fa2dcSLe Tan         }
2092d92fa2dcSLe Tan     }
2093d92fa2dcSLe Tan }
2094d92fa2dcSLe Tan 
20951da12ec4SLe Tan /* Context-cache invalidation
20961da12ec4SLe Tan  * Returns the Context Actual Invalidation Granularity.
20971da12ec4SLe Tan  * @val: the content of the CCMD_REG
20981da12ec4SLe Tan  */
20991da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
21001da12ec4SLe Tan {
21011da12ec4SLe Tan     uint64_t caig;
21021da12ec4SLe Tan     uint64_t type = val & VTD_CCMD_CIRG_MASK;
21031da12ec4SLe Tan 
21041da12ec4SLe Tan     switch (type) {
21051da12ec4SLe Tan     case VTD_CCMD_DOMAIN_INVL:
2106d92fa2dcSLe Tan         /* Fall through */
2107d92fa2dcSLe Tan     case VTD_CCMD_GLOBAL_INVL:
2108d92fa2dcSLe Tan         caig = VTD_CCMD_GLOBAL_INVL_A;
2109d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
21101da12ec4SLe Tan         break;
21111da12ec4SLe Tan 
21121da12ec4SLe Tan     case VTD_CCMD_DEVICE_INVL:
21131da12ec4SLe Tan         caig = VTD_CCMD_DEVICE_INVL_A;
2114d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
21151da12ec4SLe Tan         break;
21161da12ec4SLe Tan 
21171da12ec4SLe Tan     default:
21181376211fSPeter Xu         error_report_once("%s: invalid context: 0x%" PRIx64,
21191376211fSPeter Xu                           __func__, val);
21201da12ec4SLe Tan         caig = 0;
21211da12ec4SLe Tan     }
21221da12ec4SLe Tan     return caig;
21231da12ec4SLe Tan }
21241da12ec4SLe Tan 
2125b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
2126b5a280c0SLe Tan {
21277feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_global();
2128b5a280c0SLe Tan     vtd_reset_iotlb(s);
2129dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
2130b5a280c0SLe Tan }
2131b5a280c0SLe Tan 
2132b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
2133b5a280c0SLe Tan {
2134dd4d607eSPeter Xu     VTDContextEntry ce;
2135dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
2136dd4d607eSPeter Xu 
21377feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_domain(domain_id);
21387feb51b7SPeter Xu 
21391d9efa73SPeter Xu     vtd_iommu_lock(s);
2140b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
2141b5a280c0SLe Tan                                 &domain_id);
21421d9efa73SPeter Xu     vtd_iommu_unlock(s);
2143dd4d607eSPeter Xu 
2144b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
2145dd4d607eSPeter Xu         if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
2146dd4d607eSPeter Xu                                       vtd_as->devfn, &ce) &&
21471b2b1237SJason Wang             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
21483e090e34SPeter Xu             vtd_address_space_sync(vtd_as);
2149dd4d607eSPeter Xu         }
2150dd4d607eSPeter Xu     }
2151dd4d607eSPeter Xu }
2152dd4d607eSPeter Xu 
2153dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
2154dd4d607eSPeter Xu                                            uint16_t domain_id, hwaddr addr,
21551b2b1237SJason Wang                                              uint8_t am, uint32_t pasid)
2156dd4d607eSPeter Xu {
2157b4a4ba0dSPeter Xu     VTDAddressSpace *vtd_as;
2158dd4d607eSPeter Xu     VTDContextEntry ce;
2159dd4d607eSPeter Xu     int ret;
21604f8a62a9SPeter Xu     hwaddr size = (1 << am) * VTD_PAGE_SIZE;
2161dd4d607eSPeter Xu 
2162b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
21631b2b1237SJason Wang         if (pasid != PCI_NO_PASID && pasid != vtd_as->pasid) {
21641b2b1237SJason Wang             continue;
21651b2b1237SJason Wang         }
2166dd4d607eSPeter Xu         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
2167dd4d607eSPeter Xu                                        vtd_as->devfn, &ce);
21681b2b1237SJason Wang         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
21694f8a62a9SPeter Xu             if (vtd_as_has_map_notifier(vtd_as)) {
21704f8a62a9SPeter Xu                 /*
21714f8a62a9SPeter Xu                  * As long as we have MAP notifications registered in
21724f8a62a9SPeter Xu                  * any of our IOMMU notifiers, we need to sync the
21734f8a62a9SPeter Xu                  * shadow page table.
21744f8a62a9SPeter Xu                  */
217563b88968SPeter Xu                 vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
21764f8a62a9SPeter Xu             } else {
21774f8a62a9SPeter Xu                 /*
21784f8a62a9SPeter Xu                  * For UNMAP-only notifiers, we don't need to walk the
21794f8a62a9SPeter Xu                  * page tables.  We just deliver the PSI down to
21804f8a62a9SPeter Xu                  * invalidate caches.
21814f8a62a9SPeter Xu                  */
21825039caf3SEugenio Pérez                 IOMMUTLBEvent event = {
21835039caf3SEugenio Pérez                     .type = IOMMU_NOTIFIER_UNMAP,
21845039caf3SEugenio Pérez                     .entry = {
21854f8a62a9SPeter Xu                         .target_as = &address_space_memory,
21864f8a62a9SPeter Xu                         .iova = addr,
21874f8a62a9SPeter Xu                         .translated_addr = 0,
21884f8a62a9SPeter Xu                         .addr_mask = size - 1,
21894f8a62a9SPeter Xu                         .perm = IOMMU_NONE,
21905039caf3SEugenio Pérez                     },
21914f8a62a9SPeter Xu                 };
21925039caf3SEugenio Pérez                 memory_region_notify_iommu(&vtd_as->iommu, 0, event);
21934f8a62a9SPeter Xu             }
2194dd4d607eSPeter Xu         }
2195dd4d607eSPeter Xu     }
2196b5a280c0SLe Tan }
2197b5a280c0SLe Tan 
2198b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
2199b5a280c0SLe Tan                                       hwaddr addr, uint8_t am)
2200b5a280c0SLe Tan {
2201b5a280c0SLe Tan     VTDIOTLBPageInvInfo info;
2202b5a280c0SLe Tan 
22037feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
22047feb51b7SPeter Xu 
2205b5a280c0SLe Tan     assert(am <= VTD_MAMV);
2206b5a280c0SLe Tan     info.domain_id = domain_id;
2207d66b969bSJason Wang     info.addr = addr;
2208b5a280c0SLe Tan     info.mask = ~((1 << am) - 1);
22091d9efa73SPeter Xu     vtd_iommu_lock(s);
2210b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
22111d9efa73SPeter Xu     vtd_iommu_unlock(s);
22121b2b1237SJason Wang     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);
2213b5a280c0SLe Tan }
2214b5a280c0SLe Tan 
22151da12ec4SLe Tan /* Flush IOTLB
22161da12ec4SLe Tan  * Returns the IOTLB Actual Invalidation Granularity.
22171da12ec4SLe Tan  * @val: the content of the IOTLB_REG
22181da12ec4SLe Tan  */
22191da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
22201da12ec4SLe Tan {
22211da12ec4SLe Tan     uint64_t iaig;
22221da12ec4SLe Tan     uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
2223b5a280c0SLe Tan     uint16_t domain_id;
2224b5a280c0SLe Tan     hwaddr addr;
2225b5a280c0SLe Tan     uint8_t am;
22261da12ec4SLe Tan 
22271da12ec4SLe Tan     switch (type) {
22281da12ec4SLe Tan     case VTD_TLB_GLOBAL_FLUSH:
22291da12ec4SLe Tan         iaig = VTD_TLB_GLOBAL_FLUSH_A;
2230b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
22311da12ec4SLe Tan         break;
22321da12ec4SLe Tan 
22331da12ec4SLe Tan     case VTD_TLB_DSI_FLUSH:
2234b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
22351da12ec4SLe Tan         iaig = VTD_TLB_DSI_FLUSH_A;
2236b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
22371da12ec4SLe Tan         break;
22381da12ec4SLe Tan 
22391da12ec4SLe Tan     case VTD_TLB_PSI_FLUSH:
2240b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
2241b5a280c0SLe Tan         addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
2242b5a280c0SLe Tan         am = VTD_IVA_AM(addr);
2243b5a280c0SLe Tan         addr = VTD_IVA_ADDR(addr);
2244b5a280c0SLe Tan         if (am > VTD_MAMV) {
22451376211fSPeter Xu             error_report_once("%s: address mask overflow: 0x%" PRIx64,
22461376211fSPeter Xu                               __func__, vtd_get_quad_raw(s, DMAR_IVA_REG));
2247b5a280c0SLe Tan             iaig = 0;
2248b5a280c0SLe Tan             break;
2249b5a280c0SLe Tan         }
22501da12ec4SLe Tan         iaig = VTD_TLB_PSI_FLUSH_A;
2251b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
22521da12ec4SLe Tan         break;
22531da12ec4SLe Tan 
22541da12ec4SLe Tan     default:
22551376211fSPeter Xu         error_report_once("%s: invalid granularity: 0x%" PRIx64,
22561376211fSPeter Xu                           __func__, val);
22571da12ec4SLe Tan         iaig = 0;
22581da12ec4SLe Tan     }
22591da12ec4SLe Tan     return iaig;
22601da12ec4SLe Tan }
22611da12ec4SLe Tan 
22628991c460SLadi Prosek static void vtd_fetch_inv_desc(IntelIOMMUState *s);
2263ed7b8fbcSLe Tan 
2264ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
2265ed7b8fbcSLe Tan {
2266ed7b8fbcSLe Tan     return s->qi_enabled && (s->iq_tail == s->iq_head) &&
2267ed7b8fbcSLe Tan            (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
2268ed7b8fbcSLe Tan }
2269ed7b8fbcSLe Tan 
2270ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
2271ed7b8fbcSLe Tan {
2272ed7b8fbcSLe Tan     uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
2273ed7b8fbcSLe Tan 
22747feb51b7SPeter Xu     trace_vtd_inv_qi_enable(en);
22757feb51b7SPeter Xu 
2276ed7b8fbcSLe Tan     if (en) {
227737f51384SPrasad Singamsetty         s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
2278ed7b8fbcSLe Tan         /* 2^(x+8) entries */
2279c0c1d351SLiu, Yi L         s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8 - (s->iq_dw ? 1 : 0));
2280ed7b8fbcSLe Tan         s->qi_enabled = true;
22817feb51b7SPeter Xu         trace_vtd_inv_qi_setup(s->iq, s->iq_size);
2282ed7b8fbcSLe Tan         /* Ok - report back to driver */
2283ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
22848991c460SLadi Prosek 
22858991c460SLadi Prosek         if (s->iq_tail != 0) {
22868991c460SLadi Prosek             /*
22878991c460SLadi Prosek              * This is a spec violation but Windows guests are known to set up
22888991c460SLadi Prosek              * Queued Invalidation this way so we allow the write and process
22898991c460SLadi Prosek              * Invalidation Descriptors right away.
22908991c460SLadi Prosek              */
22918991c460SLadi Prosek             trace_vtd_warn_invalid_qi_tail(s->iq_tail);
22928991c460SLadi Prosek             if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
22938991c460SLadi Prosek                 vtd_fetch_inv_desc(s);
22948991c460SLadi Prosek             }
2295ed7b8fbcSLe Tan         }
2296ed7b8fbcSLe Tan     } else {
2297ed7b8fbcSLe Tan         if (vtd_queued_inv_disable_check(s)) {
2298ed7b8fbcSLe Tan             /* disable Queued Invalidation */
2299ed7b8fbcSLe Tan             vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
2300ed7b8fbcSLe Tan             s->iq_head = 0;
2301ed7b8fbcSLe Tan             s->qi_enabled = false;
2302ed7b8fbcSLe Tan             /* Ok - report back to driver */
2303ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
2304ed7b8fbcSLe Tan         } else {
23054e4abd11SPeter Xu             error_report_once("%s: detected improper state when disable QI "
23064e4abd11SPeter Xu                               "(head=0x%x, tail=0x%x, last_type=%d)",
23074e4abd11SPeter Xu                               __func__,
23084e4abd11SPeter Xu                               s->iq_head, s->iq_tail, s->iq_last_desc_type);
2309ed7b8fbcSLe Tan         }
2310ed7b8fbcSLe Tan     }
2311ed7b8fbcSLe Tan }
2312ed7b8fbcSLe Tan 
23131da12ec4SLe Tan /* Set Root Table Pointer */
23141da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
23151da12ec4SLe Tan {
23161da12ec4SLe Tan     vtd_root_table_setup(s);
23171da12ec4SLe Tan     /* Ok - report back to driver */
23181da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
23192cc9ddccSPeter Xu     vtd_reset_caches(s);
23202cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
23211da12ec4SLe Tan }
23221da12ec4SLe Tan 
2323a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */
2324a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
2325a5861439SPeter Xu {
2326a5861439SPeter Xu     vtd_interrupt_remap_table_setup(s);
2327a5861439SPeter Xu     /* Ok - report back to driver */
2328a5861439SPeter Xu     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
2329a5861439SPeter Xu }
2330a5861439SPeter Xu 
23311da12ec4SLe Tan /* Handle Translation Enable/Disable */
23321da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
23331da12ec4SLe Tan {
2334558e0024SPeter Xu     if (s->dmar_enabled == en) {
2335558e0024SPeter Xu         return;
2336558e0024SPeter Xu     }
2337558e0024SPeter Xu 
23387feb51b7SPeter Xu     trace_vtd_dmar_enable(en);
23391da12ec4SLe Tan 
23401da12ec4SLe Tan     if (en) {
23411da12ec4SLe Tan         s->dmar_enabled = true;
23421da12ec4SLe Tan         /* Ok - report back to driver */
23431da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
23441da12ec4SLe Tan     } else {
23451da12ec4SLe Tan         s->dmar_enabled = false;
23461da12ec4SLe Tan 
23471da12ec4SLe Tan         /* Clear the index of Fault Recording Register */
23481da12ec4SLe Tan         s->next_frcd_reg = 0;
23491da12ec4SLe Tan         /* Ok - report back to driver */
23501da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
23511da12ec4SLe Tan     }
2352558e0024SPeter Xu 
23532cc9ddccSPeter Xu     vtd_reset_caches(s);
23542cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
23551da12ec4SLe Tan }
23561da12ec4SLe Tan 
235780de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */
235880de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
235980de52baSPeter Xu {
23607feb51b7SPeter Xu     trace_vtd_ir_enable(en);
236180de52baSPeter Xu 
236280de52baSPeter Xu     if (en) {
236380de52baSPeter Xu         s->intr_enabled = true;
236480de52baSPeter Xu         /* Ok - report back to driver */
236580de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
236680de52baSPeter Xu     } else {
236780de52baSPeter Xu         s->intr_enabled = false;
236880de52baSPeter Xu         /* Ok - report back to driver */
236980de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
237080de52baSPeter Xu     }
237180de52baSPeter Xu }
237280de52baSPeter Xu 
23731da12ec4SLe Tan /* Handle write to Global Command Register */
23741da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s)
23751da12ec4SLe Tan {
2376175f3a59SDavid Woodhouse     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
23771da12ec4SLe Tan     uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
23781da12ec4SLe Tan     uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
23791da12ec4SLe Tan     uint32_t changed = status ^ val;
23801da12ec4SLe Tan 
23817feb51b7SPeter Xu     trace_vtd_reg_write_gcmd(status, val);
23828646d9c7SDavid Woodhouse     if ((changed & VTD_GCMD_TE) && s->dma_translation) {
23831da12ec4SLe Tan         /* Translation enable/disable */
23841da12ec4SLe Tan         vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
23851da12ec4SLe Tan     }
23861da12ec4SLe Tan     if (val & VTD_GCMD_SRTP) {
23871da12ec4SLe Tan         /* Set/update the root-table pointer */
23881da12ec4SLe Tan         vtd_handle_gcmd_srtp(s);
23891da12ec4SLe Tan     }
2390ed7b8fbcSLe Tan     if (changed & VTD_GCMD_QIE) {
2391ed7b8fbcSLe Tan         /* Queued Invalidation Enable */
2392ed7b8fbcSLe Tan         vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
2393ed7b8fbcSLe Tan     }
2394a5861439SPeter Xu     if (val & VTD_GCMD_SIRTP) {
2395a5861439SPeter Xu         /* Set/update the interrupt remapping root-table pointer */
2396a5861439SPeter Xu         vtd_handle_gcmd_sirtp(s);
2397a5861439SPeter Xu     }
2398175f3a59SDavid Woodhouse     if ((changed & VTD_GCMD_IRE) &&
2399175f3a59SDavid Woodhouse         x86_iommu_ir_supported(x86_iommu)) {
240080de52baSPeter Xu         /* Interrupt remap enable/disable */
240180de52baSPeter Xu         vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
240280de52baSPeter Xu     }
24031da12ec4SLe Tan }
24041da12ec4SLe Tan 
24051da12ec4SLe Tan /* Handle write to Context Command Register */
24061da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s)
24071da12ec4SLe Tan {
24081da12ec4SLe Tan     uint64_t ret;
24091da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
24101da12ec4SLe Tan 
24111da12ec4SLe Tan     /* Context-cache invalidation request */
24121da12ec4SLe Tan     if (val & VTD_CCMD_ICC) {
2413ed7b8fbcSLe Tan         if (s->qi_enabled) {
24141376211fSPeter Xu             error_report_once("Queued Invalidation enabled, "
2415ed7b8fbcSLe Tan                               "should not use register-based invalidation");
2416ed7b8fbcSLe Tan             return;
2417ed7b8fbcSLe Tan         }
24181da12ec4SLe Tan         ret = vtd_context_cache_invalidate(s, val);
24191da12ec4SLe Tan         /* Invalidation completed. Change something to show */
24201da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
24211da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
24221da12ec4SLe Tan                                       ret);
24231da12ec4SLe Tan     }
24241da12ec4SLe Tan }
24251da12ec4SLe Tan 
24261da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */
24271da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s)
24281da12ec4SLe Tan {
24291da12ec4SLe Tan     uint64_t ret;
24301da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
24311da12ec4SLe Tan 
24321da12ec4SLe Tan     /* IOTLB invalidation request */
24331da12ec4SLe Tan     if (val & VTD_TLB_IVT) {
2434ed7b8fbcSLe Tan         if (s->qi_enabled) {
24351376211fSPeter Xu             error_report_once("Queued Invalidation enabled, "
24361376211fSPeter Xu                               "should not use register-based invalidation");
2437ed7b8fbcSLe Tan             return;
2438ed7b8fbcSLe Tan         }
24391da12ec4SLe Tan         ret = vtd_iotlb_flush(s, val);
24401da12ec4SLe Tan         /* Invalidation completed. Change something to show */
24411da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
24421da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
24431da12ec4SLe Tan                                       VTD_TLB_FLUSH_GRANU_MASK_A, ret);
24441da12ec4SLe Tan     }
24451da12ec4SLe Tan }
24461da12ec4SLe Tan 
2447ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */
2448c0c1d351SLiu, Yi L static bool vtd_get_inv_desc(IntelIOMMUState *s,
2449ed7b8fbcSLe Tan                              VTDInvDesc *inv_desc)
2450ed7b8fbcSLe Tan {
2451c0c1d351SLiu, Yi L     dma_addr_t base_addr = s->iq;
2452c0c1d351SLiu, Yi L     uint32_t offset = s->iq_head;
2453c0c1d351SLiu, Yi L     uint32_t dw = s->iq_dw ? 32 : 16;
2454c0c1d351SLiu, Yi L     dma_addr_t addr = base_addr + offset * dw;
2455c0c1d351SLiu, Yi L 
2456ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
2457ba06fe8aSPhilippe Mathieu-Daudé                         inv_desc, dw, MEMTXATTRS_UNSPECIFIED)) {
2458c0c1d351SLiu, Yi L         error_report_once("Read INV DESC failed.");
2459ed7b8fbcSLe Tan         return false;
2460ed7b8fbcSLe Tan     }
2461ed7b8fbcSLe Tan     inv_desc->lo = le64_to_cpu(inv_desc->lo);
2462ed7b8fbcSLe Tan     inv_desc->hi = le64_to_cpu(inv_desc->hi);
2463c0c1d351SLiu, Yi L     if (dw == 32) {
2464c0c1d351SLiu, Yi L         inv_desc->val[2] = le64_to_cpu(inv_desc->val[2]);
2465c0c1d351SLiu, Yi L         inv_desc->val[3] = le64_to_cpu(inv_desc->val[3]);
2466c0c1d351SLiu, Yi L     }
2467ed7b8fbcSLe Tan     return true;
2468ed7b8fbcSLe Tan }
2469ed7b8fbcSLe Tan 
2470ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2471ed7b8fbcSLe Tan {
2472ed7b8fbcSLe Tan     if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
2473ed7b8fbcSLe Tan         (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
2474095955b2SPeter Xu         error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2475095955b2SPeter Xu                           " (reserved nonzero)", __func__, inv_desc->hi,
2476095955b2SPeter Xu                           inv_desc->lo);
2477ed7b8fbcSLe Tan         return false;
2478ed7b8fbcSLe Tan     }
2479ed7b8fbcSLe Tan     if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
2480ed7b8fbcSLe Tan         /* Status Write */
2481ed7b8fbcSLe Tan         uint32_t status_data = (uint32_t)(inv_desc->lo >>
2482ed7b8fbcSLe Tan                                VTD_INV_DESC_WAIT_DATA_SHIFT);
2483ed7b8fbcSLe Tan 
2484ed7b8fbcSLe Tan         assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
2485ed7b8fbcSLe Tan 
2486ed7b8fbcSLe Tan         /* FIXME: need to be masked with HAW? */
2487ed7b8fbcSLe Tan         dma_addr_t status_addr = inv_desc->hi;
2488bc535e59SPeter Xu         trace_vtd_inv_desc_wait_sw(status_addr, status_data);
2489ed7b8fbcSLe Tan         status_data = cpu_to_le32(status_data);
2490ba06fe8aSPhilippe Mathieu-Daudé         if (dma_memory_write(&address_space_memory, status_addr,
2491ba06fe8aSPhilippe Mathieu-Daudé                              &status_data, sizeof(status_data),
2492ba06fe8aSPhilippe Mathieu-Daudé                              MEMTXATTRS_UNSPECIFIED)) {
2493bc535e59SPeter Xu             trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
2494ed7b8fbcSLe Tan             return false;
2495ed7b8fbcSLe Tan         }
2496ed7b8fbcSLe Tan     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
2497ed7b8fbcSLe Tan         /* Interrupt flag */
2498ed7b8fbcSLe Tan         vtd_generate_completion_event(s);
2499ed7b8fbcSLe Tan     } else {
2500095955b2SPeter Xu         error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2501095955b2SPeter Xu                           " (unknown type)", __func__, inv_desc->hi,
2502095955b2SPeter Xu                           inv_desc->lo);
2503ed7b8fbcSLe Tan         return false;
2504ed7b8fbcSLe Tan     }
2505ed7b8fbcSLe Tan     return true;
2506ed7b8fbcSLe Tan }
2507ed7b8fbcSLe Tan 
2508d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
2509d92fa2dcSLe Tan                                            VTDInvDesc *inv_desc)
2510d92fa2dcSLe Tan {
2511bc535e59SPeter Xu     uint16_t sid, fmask;
2512bc535e59SPeter Xu 
2513d92fa2dcSLe Tan     if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
2514095955b2SPeter Xu         error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2515095955b2SPeter Xu                           " (reserved nonzero)", __func__, inv_desc->hi,
2516095955b2SPeter Xu                           inv_desc->lo);
2517d92fa2dcSLe Tan         return false;
2518d92fa2dcSLe Tan     }
2519d92fa2dcSLe Tan     switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
2520d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DOMAIN:
2521bc535e59SPeter Xu         trace_vtd_inv_desc_cc_domain(
2522d92fa2dcSLe Tan             (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
2523d92fa2dcSLe Tan         /* Fall through */
2524d92fa2dcSLe Tan     case VTD_INV_DESC_CC_GLOBAL:
2525d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
2526d92fa2dcSLe Tan         break;
2527d92fa2dcSLe Tan 
2528d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DEVICE:
2529bc535e59SPeter Xu         sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
2530bc535e59SPeter Xu         fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
2531bc535e59SPeter Xu         vtd_context_device_invalidate(s, sid, fmask);
2532d92fa2dcSLe Tan         break;
2533d92fa2dcSLe Tan 
2534d92fa2dcSLe Tan     default:
2535095955b2SPeter Xu         error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2536095955b2SPeter Xu                           " (invalid type)", __func__, inv_desc->hi,
2537095955b2SPeter Xu                           inv_desc->lo);
2538d92fa2dcSLe Tan         return false;
2539d92fa2dcSLe Tan     }
2540d92fa2dcSLe Tan     return true;
2541d92fa2dcSLe Tan }
2542d92fa2dcSLe Tan 
2543b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2544b5a280c0SLe Tan {
2545b5a280c0SLe Tan     uint16_t domain_id;
2546b5a280c0SLe Tan     uint8_t am;
2547b5a280c0SLe Tan     hwaddr addr;
2548b5a280c0SLe Tan 
2549b5a280c0SLe Tan     if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
2550b5a280c0SLe Tan         (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
2551095955b2SPeter Xu         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2552ff5b5d5bSMarkus Armbruster                           ", lo=0x%"PRIx64" (reserved bits unzero)",
2553095955b2SPeter Xu                           __func__, inv_desc->hi, inv_desc->lo);
2554b5a280c0SLe Tan         return false;
2555b5a280c0SLe Tan     }
2556b5a280c0SLe Tan 
2557b5a280c0SLe Tan     switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
2558b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_GLOBAL:
2559b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
2560b5a280c0SLe Tan         break;
2561b5a280c0SLe Tan 
2562b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_DOMAIN:
2563b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
2564b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
2565b5a280c0SLe Tan         break;
2566b5a280c0SLe Tan 
2567b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_PAGE:
2568b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
2569b5a280c0SLe Tan         addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
2570b5a280c0SLe Tan         am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
2571b5a280c0SLe Tan         if (am > VTD_MAMV) {
2572095955b2SPeter Xu             error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2573ff5b5d5bSMarkus Armbruster                               ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)",
2574095955b2SPeter Xu                               __func__, inv_desc->hi, inv_desc->lo,
2575095955b2SPeter Xu                               am, (unsigned)VTD_MAMV);
2576b5a280c0SLe Tan             return false;
2577b5a280c0SLe Tan         }
2578b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
2579b5a280c0SLe Tan         break;
2580b5a280c0SLe Tan 
2581b5a280c0SLe Tan     default:
2582095955b2SPeter Xu         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2583ff5b5d5bSMarkus Armbruster                           ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
2584095955b2SPeter Xu                           __func__, inv_desc->hi, inv_desc->lo,
2585095955b2SPeter Xu                           inv_desc->lo & VTD_INV_DESC_IOTLB_G);
2586b5a280c0SLe Tan         return false;
2587b5a280c0SLe Tan     }
2588b5a280c0SLe Tan     return true;
2589b5a280c0SLe Tan }
2590b5a280c0SLe Tan 
259102a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
259202a2cbc8SPeter Xu                                      VTDInvDesc *inv_desc)
259302a2cbc8SPeter Xu {
25947feb51b7SPeter Xu     trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
259502a2cbc8SPeter Xu                            inv_desc->iec.index,
259602a2cbc8SPeter Xu                            inv_desc->iec.index_mask);
259702a2cbc8SPeter Xu 
259802a2cbc8SPeter Xu     vtd_iec_notify_all(s, !inv_desc->iec.granularity,
259902a2cbc8SPeter Xu                        inv_desc->iec.index,
260002a2cbc8SPeter Xu                        inv_desc->iec.index_mask);
2601554f5e16SJason Wang     return true;
2602554f5e16SJason Wang }
260302a2cbc8SPeter Xu 
2604554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
2605554f5e16SJason Wang                                           VTDInvDesc *inv_desc)
2606554f5e16SJason Wang {
2607554f5e16SJason Wang     VTDAddressSpace *vtd_dev_as;
26085039caf3SEugenio Pérez     IOMMUTLBEvent event;
2609554f5e16SJason Wang     hwaddr addr;
2610554f5e16SJason Wang     uint64_t sz;
2611554f5e16SJason Wang     uint16_t sid;
2612554f5e16SJason Wang     bool size;
2613554f5e16SJason Wang 
2614554f5e16SJason Wang     addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
2615554f5e16SJason Wang     sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
2616554f5e16SJason Wang     size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
2617554f5e16SJason Wang 
2618554f5e16SJason Wang     if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
2619554f5e16SJason Wang         (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
2620095955b2SPeter Xu         error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64
2621095955b2SPeter Xu                           ", lo=%"PRIx64" (reserved nonzero)", __func__,
2622095955b2SPeter Xu                           inv_desc->hi, inv_desc->lo);
2623554f5e16SJason Wang         return false;
2624554f5e16SJason Wang     }
2625554f5e16SJason Wang 
2626da8d439cSJason Wang     /*
2627da8d439cSJason Wang      * Using sid is OK since the guest should have finished the
2628da8d439cSJason Wang      * initialization of both the bus and device.
2629da8d439cSJason Wang      */
2630da8d439cSJason Wang     vtd_dev_as = vtd_get_as_by_sid(s, sid);
2631554f5e16SJason Wang     if (!vtd_dev_as) {
2632554f5e16SJason Wang         goto done;
2633554f5e16SJason Wang     }
2634554f5e16SJason Wang 
263504eb6247SJason Wang     /* According to ATS spec table 2.4:
263604eb6247SJason Wang      * S = 0, bits 15:12 = xxxx     range size: 4K
263704eb6247SJason Wang      * S = 1, bits 15:12 = xxx0     range size: 8K
263804eb6247SJason Wang      * S = 1, bits 15:12 = xx01     range size: 16K
263904eb6247SJason Wang      * S = 1, bits 15:12 = x011     range size: 32K
264004eb6247SJason Wang      * S = 1, bits 15:12 = 0111     range size: 64K
264104eb6247SJason Wang      * ...
264204eb6247SJason Wang      */
2643554f5e16SJason Wang     if (size) {
264404eb6247SJason Wang         sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
2645554f5e16SJason Wang         addr &= ~(sz - 1);
2646554f5e16SJason Wang     } else {
2647554f5e16SJason Wang         sz = VTD_PAGE_SIZE;
2648554f5e16SJason Wang     }
2649554f5e16SJason Wang 
2650b68ba1caSEugenio Pérez     event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
26515039caf3SEugenio Pérez     event.entry.target_as = &vtd_dev_as->as;
26525039caf3SEugenio Pérez     event.entry.addr_mask = sz - 1;
26535039caf3SEugenio Pérez     event.entry.iova = addr;
26545039caf3SEugenio Pérez     event.entry.perm = IOMMU_NONE;
26555039caf3SEugenio Pérez     event.entry.translated_addr = 0;
26565039caf3SEugenio Pérez     memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
2657554f5e16SJason Wang 
2658554f5e16SJason Wang done:
265902a2cbc8SPeter Xu     return true;
266002a2cbc8SPeter Xu }
266102a2cbc8SPeter Xu 
2662ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s)
2663ed7b8fbcSLe Tan {
2664ed7b8fbcSLe Tan     VTDInvDesc inv_desc;
2665ed7b8fbcSLe Tan     uint8_t desc_type;
2666ed7b8fbcSLe Tan 
26677feb51b7SPeter Xu     trace_vtd_inv_qi_head(s->iq_head);
2668c0c1d351SLiu, Yi L     if (!vtd_get_inv_desc(s, &inv_desc)) {
2669ed7b8fbcSLe Tan         s->iq_last_desc_type = VTD_INV_DESC_NONE;
2670ed7b8fbcSLe Tan         return false;
2671ed7b8fbcSLe Tan     }
2672c0c1d351SLiu, Yi L 
2673ed7b8fbcSLe Tan     desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
2674ed7b8fbcSLe Tan     /* FIXME: should update at first or at last? */
2675ed7b8fbcSLe Tan     s->iq_last_desc_type = desc_type;
2676ed7b8fbcSLe Tan 
2677ed7b8fbcSLe Tan     switch (desc_type) {
2678ed7b8fbcSLe Tan     case VTD_INV_DESC_CC:
2679bc535e59SPeter Xu         trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
2680d92fa2dcSLe Tan         if (!vtd_process_context_cache_desc(s, &inv_desc)) {
2681d92fa2dcSLe Tan             return false;
2682d92fa2dcSLe Tan         }
2683ed7b8fbcSLe Tan         break;
2684ed7b8fbcSLe Tan 
2685ed7b8fbcSLe Tan     case VTD_INV_DESC_IOTLB:
2686bc535e59SPeter Xu         trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
2687b5a280c0SLe Tan         if (!vtd_process_iotlb_desc(s, &inv_desc)) {
2688b5a280c0SLe Tan             return false;
2689b5a280c0SLe Tan         }
2690ed7b8fbcSLe Tan         break;
2691ed7b8fbcSLe Tan 
26924a4f219eSYi Sun     /*
26934a4f219eSYi Sun      * TODO: the entity of below two cases will be implemented in future series.
26944a4f219eSYi Sun      * To make guest (which integrates scalable mode support patch set in
26954a4f219eSYi Sun      * iommu driver) work, just return true is enough so far.
26964a4f219eSYi Sun      */
26974a4f219eSYi Sun     case VTD_INV_DESC_PC:
26984a4f219eSYi Sun         break;
26994a4f219eSYi Sun 
27004a4f219eSYi Sun     case VTD_INV_DESC_PIOTLB:
27014a4f219eSYi Sun         break;
27024a4f219eSYi Sun 
2703ed7b8fbcSLe Tan     case VTD_INV_DESC_WAIT:
2704bc535e59SPeter Xu         trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
2705ed7b8fbcSLe Tan         if (!vtd_process_wait_desc(s, &inv_desc)) {
2706ed7b8fbcSLe Tan             return false;
2707ed7b8fbcSLe Tan         }
2708ed7b8fbcSLe Tan         break;
2709ed7b8fbcSLe Tan 
2710b7910472SPeter Xu     case VTD_INV_DESC_IEC:
2711bc535e59SPeter Xu         trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
271202a2cbc8SPeter Xu         if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
271302a2cbc8SPeter Xu             return false;
271402a2cbc8SPeter Xu         }
2715b7910472SPeter Xu         break;
2716b7910472SPeter Xu 
2717554f5e16SJason Wang     case VTD_INV_DESC_DEVICE:
27187feb51b7SPeter Xu         trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
2719554f5e16SJason Wang         if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
2720554f5e16SJason Wang             return false;
2721554f5e16SJason Wang         }
2722554f5e16SJason Wang         break;
2723554f5e16SJason Wang 
2724ed7b8fbcSLe Tan     default:
2725095955b2SPeter Xu         error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64
2726095955b2SPeter Xu                           " (unknown type)", __func__, inv_desc.hi,
2727095955b2SPeter Xu                           inv_desc.lo);
2728ed7b8fbcSLe Tan         return false;
2729ed7b8fbcSLe Tan     }
2730ed7b8fbcSLe Tan     s->iq_head++;
2731ed7b8fbcSLe Tan     if (s->iq_head == s->iq_size) {
2732ed7b8fbcSLe Tan         s->iq_head = 0;
2733ed7b8fbcSLe Tan     }
2734ed7b8fbcSLe Tan     return true;
2735ed7b8fbcSLe Tan }
2736ed7b8fbcSLe Tan 
2737ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */
2738ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s)
2739ed7b8fbcSLe Tan {
2740a4544c45SLiu Yi L     int qi_shift;
2741a4544c45SLiu Yi L 
2742a4544c45SLiu Yi L     /* Refer to 10.4.23 of VT-d spec 3.0 */
2743a4544c45SLiu Yi L     qi_shift = s->iq_dw ? VTD_IQH_QH_SHIFT_5 : VTD_IQH_QH_SHIFT_4;
2744a4544c45SLiu Yi L 
27457feb51b7SPeter Xu     trace_vtd_inv_qi_fetch();
27467feb51b7SPeter Xu 
2747ed7b8fbcSLe Tan     if (s->iq_tail >= s->iq_size) {
2748ed7b8fbcSLe Tan         /* Detects an invalid Tail pointer */
27494e4abd11SPeter Xu         error_report_once("%s: detected invalid QI tail "
27504e4abd11SPeter Xu                           "(tail=0x%x, size=0x%x)",
27514e4abd11SPeter Xu                           __func__, s->iq_tail, s->iq_size);
2752ed7b8fbcSLe Tan         vtd_handle_inv_queue_error(s);
2753ed7b8fbcSLe Tan         return;
2754ed7b8fbcSLe Tan     }
2755ed7b8fbcSLe Tan     while (s->iq_head != s->iq_tail) {
2756ed7b8fbcSLe Tan         if (!vtd_process_inv_desc(s)) {
2757ed7b8fbcSLe Tan             /* Invalidation Queue Errors */
2758ed7b8fbcSLe Tan             vtd_handle_inv_queue_error(s);
2759ed7b8fbcSLe Tan             break;
2760ed7b8fbcSLe Tan         }
2761ed7b8fbcSLe Tan         /* Must update the IQH_REG in time */
2762ed7b8fbcSLe Tan         vtd_set_quad_raw(s, DMAR_IQH_REG,
2763a4544c45SLiu Yi L                          (((uint64_t)(s->iq_head)) << qi_shift) &
2764ed7b8fbcSLe Tan                          VTD_IQH_QH_MASK);
2765ed7b8fbcSLe Tan     }
2766ed7b8fbcSLe Tan }
2767ed7b8fbcSLe Tan 
2768ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */
2769ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s)
2770ed7b8fbcSLe Tan {
2771ed7b8fbcSLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
2772ed7b8fbcSLe Tan 
2773c0c1d351SLiu, Yi L     if (s->iq_dw && (val & VTD_IQT_QT_256_RSV_BIT)) {
2774c0c1d351SLiu, Yi L         error_report_once("%s: RSV bit is set: val=0x%"PRIx64,
2775c0c1d351SLiu, Yi L                           __func__, val);
2776c0c1d351SLiu, Yi L         return;
2777c0c1d351SLiu, Yi L     }
2778c0c1d351SLiu, Yi L     s->iq_tail = VTD_IQT_QT(s->iq_dw, val);
27797feb51b7SPeter Xu     trace_vtd_inv_qi_tail(s->iq_tail);
27807feb51b7SPeter Xu 
2781ed7b8fbcSLe Tan     if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
2782ed7b8fbcSLe Tan         /* Process Invalidation Queue here */
2783ed7b8fbcSLe Tan         vtd_fetch_inv_desc(s);
2784ed7b8fbcSLe Tan     }
2785ed7b8fbcSLe Tan }
2786ed7b8fbcSLe Tan 
27871da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s)
27881da12ec4SLe Tan {
27891da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
27901da12ec4SLe Tan     uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
27911da12ec4SLe Tan     uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
27921da12ec4SLe Tan 
27931da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
27941da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
27957feb51b7SPeter Xu         trace_vtd_fsts_clear_ip();
27961da12ec4SLe Tan     }
2797ed7b8fbcSLe Tan     /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
2798ed7b8fbcSLe Tan      * Descriptors if there are any when Queued Invalidation is enabled?
2799ed7b8fbcSLe Tan      */
28001da12ec4SLe Tan }
28011da12ec4SLe Tan 
28021da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s)
28031da12ec4SLe Tan {
28041da12ec4SLe Tan     uint32_t fectl_reg;
28051da12ec4SLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
28061da12ec4SLe Tan      * need to compare the old value and the new value to conclude that
28071da12ec4SLe Tan      * software clears the IM field? Or just check if the IM field is zero?
28081da12ec4SLe Tan      */
28091da12ec4SLe Tan     fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
28107feb51b7SPeter Xu 
28117feb51b7SPeter Xu     trace_vtd_reg_write_fectl(fectl_reg);
28127feb51b7SPeter Xu 
28131da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
28141da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
28151da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
28161da12ec4SLe Tan     }
28171da12ec4SLe Tan }
28181da12ec4SLe Tan 
2819ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s)
2820ed7b8fbcSLe Tan {
2821ed7b8fbcSLe Tan     uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
2822ed7b8fbcSLe Tan     uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
2823ed7b8fbcSLe Tan 
2824ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
28257feb51b7SPeter Xu         trace_vtd_reg_ics_clear_ip();
2826ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
2827ed7b8fbcSLe Tan     }
2828ed7b8fbcSLe Tan }
2829ed7b8fbcSLe Tan 
2830ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s)
2831ed7b8fbcSLe Tan {
2832ed7b8fbcSLe Tan     uint32_t iectl_reg;
2833ed7b8fbcSLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
2834ed7b8fbcSLe Tan      * need to compare the old value and the new value to conclude that
2835ed7b8fbcSLe Tan      * software clears the IM field? Or just check if the IM field is zero?
2836ed7b8fbcSLe Tan      */
2837ed7b8fbcSLe Tan     iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
28387feb51b7SPeter Xu 
28397feb51b7SPeter Xu     trace_vtd_reg_write_iectl(iectl_reg);
28407feb51b7SPeter Xu 
2841ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
2842ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
2843ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
2844ed7b8fbcSLe Tan     }
2845ed7b8fbcSLe Tan }
2846ed7b8fbcSLe Tan 
28471da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
28481da12ec4SLe Tan {
28491da12ec4SLe Tan     IntelIOMMUState *s = opaque;
28501da12ec4SLe Tan     uint64_t val;
28511da12ec4SLe Tan 
28527feb51b7SPeter Xu     trace_vtd_reg_read(addr, size);
28537feb51b7SPeter Xu 
28541da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
28551376211fSPeter Xu         error_report_once("%s: MMIO over range: addr=0x%" PRIx64
285673beb01eSPeter Xu                           " size=0x%x", __func__, addr, size);
28571da12ec4SLe Tan         return (uint64_t)-1;
28581da12ec4SLe Tan     }
28591da12ec4SLe Tan 
28601da12ec4SLe Tan     switch (addr) {
28611da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
28621da12ec4SLe Tan     case DMAR_RTADDR_REG:
28638fdee711SYi Sun         val = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
28641da12ec4SLe Tan         if (size == 4) {
28658fdee711SYi Sun             val = val & ((1ULL << 32) - 1);
28661da12ec4SLe Tan         }
28671da12ec4SLe Tan         break;
28681da12ec4SLe Tan 
28691da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
28701da12ec4SLe Tan         assert(size == 4);
28718fdee711SYi Sun         val = vtd_get_quad_raw(s, DMAR_RTADDR_REG) >> 32;
28721da12ec4SLe Tan         break;
28731da12ec4SLe Tan 
2874ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
2875ed7b8fbcSLe Tan     case DMAR_IQA_REG:
2876ed7b8fbcSLe Tan         val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2877ed7b8fbcSLe Tan         if (size == 4) {
2878ed7b8fbcSLe Tan             val = val & ((1ULL << 32) - 1);
2879ed7b8fbcSLe Tan         }
2880ed7b8fbcSLe Tan         break;
2881ed7b8fbcSLe Tan 
2882ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
2883ed7b8fbcSLe Tan         assert(size == 4);
2884ed7b8fbcSLe Tan         val = s->iq >> 32;
2885ed7b8fbcSLe Tan         break;
2886ed7b8fbcSLe Tan 
28871da12ec4SLe Tan     default:
28881da12ec4SLe Tan         if (size == 4) {
28891da12ec4SLe Tan             val = vtd_get_long(s, addr);
28901da12ec4SLe Tan         } else {
28911da12ec4SLe Tan             val = vtd_get_quad(s, addr);
28921da12ec4SLe Tan         }
28931da12ec4SLe Tan     }
28947feb51b7SPeter Xu 
28951da12ec4SLe Tan     return val;
28961da12ec4SLe Tan }
28971da12ec4SLe Tan 
28981da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr,
28991da12ec4SLe Tan                           uint64_t val, unsigned size)
29001da12ec4SLe Tan {
29011da12ec4SLe Tan     IntelIOMMUState *s = opaque;
29021da12ec4SLe Tan 
29037feb51b7SPeter Xu     trace_vtd_reg_write(addr, size, val);
29047feb51b7SPeter Xu 
29051da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
29061376211fSPeter Xu         error_report_once("%s: MMIO over range: addr=0x%" PRIx64
290773beb01eSPeter Xu                           " size=0x%x", __func__, addr, size);
29081da12ec4SLe Tan         return;
29091da12ec4SLe Tan     }
29101da12ec4SLe Tan 
29111da12ec4SLe Tan     switch (addr) {
29121da12ec4SLe Tan     /* Global Command Register, 32-bit */
29131da12ec4SLe Tan     case DMAR_GCMD_REG:
29141da12ec4SLe Tan         vtd_set_long(s, addr, val);
29151da12ec4SLe Tan         vtd_handle_gcmd_write(s);
29161da12ec4SLe Tan         break;
29171da12ec4SLe Tan 
29181da12ec4SLe Tan     /* Context Command Register, 64-bit */
29191da12ec4SLe Tan     case DMAR_CCMD_REG:
29201da12ec4SLe Tan         if (size == 4) {
29211da12ec4SLe Tan             vtd_set_long(s, addr, val);
29221da12ec4SLe Tan         } else {
29231da12ec4SLe Tan             vtd_set_quad(s, addr, val);
29241da12ec4SLe Tan             vtd_handle_ccmd_write(s);
29251da12ec4SLe Tan         }
29261da12ec4SLe Tan         break;
29271da12ec4SLe Tan 
29281da12ec4SLe Tan     case DMAR_CCMD_REG_HI:
29291da12ec4SLe Tan         assert(size == 4);
29301da12ec4SLe Tan         vtd_set_long(s, addr, val);
29311da12ec4SLe Tan         vtd_handle_ccmd_write(s);
29321da12ec4SLe Tan         break;
29331da12ec4SLe Tan 
29341da12ec4SLe Tan     /* IOTLB Invalidation Register, 64-bit */
29351da12ec4SLe Tan     case DMAR_IOTLB_REG:
29361da12ec4SLe Tan         if (size == 4) {
29371da12ec4SLe Tan             vtd_set_long(s, addr, val);
29381da12ec4SLe Tan         } else {
29391da12ec4SLe Tan             vtd_set_quad(s, addr, val);
29401da12ec4SLe Tan             vtd_handle_iotlb_write(s);
29411da12ec4SLe Tan         }
29421da12ec4SLe Tan         break;
29431da12ec4SLe Tan 
29441da12ec4SLe Tan     case DMAR_IOTLB_REG_HI:
29451da12ec4SLe Tan         assert(size == 4);
29461da12ec4SLe Tan         vtd_set_long(s, addr, val);
29471da12ec4SLe Tan         vtd_handle_iotlb_write(s);
29481da12ec4SLe Tan         break;
29491da12ec4SLe Tan 
2950b5a280c0SLe Tan     /* Invalidate Address Register, 64-bit */
2951b5a280c0SLe Tan     case DMAR_IVA_REG:
2952b5a280c0SLe Tan         if (size == 4) {
2953b5a280c0SLe Tan             vtd_set_long(s, addr, val);
2954b5a280c0SLe Tan         } else {
2955b5a280c0SLe Tan             vtd_set_quad(s, addr, val);
2956b5a280c0SLe Tan         }
2957b5a280c0SLe Tan         break;
2958b5a280c0SLe Tan 
2959b5a280c0SLe Tan     case DMAR_IVA_REG_HI:
2960b5a280c0SLe Tan         assert(size == 4);
2961b5a280c0SLe Tan         vtd_set_long(s, addr, val);
2962b5a280c0SLe Tan         break;
2963b5a280c0SLe Tan 
29641da12ec4SLe Tan     /* Fault Status Register, 32-bit */
29651da12ec4SLe Tan     case DMAR_FSTS_REG:
29661da12ec4SLe Tan         assert(size == 4);
29671da12ec4SLe Tan         vtd_set_long(s, addr, val);
29681da12ec4SLe Tan         vtd_handle_fsts_write(s);
29691da12ec4SLe Tan         break;
29701da12ec4SLe Tan 
29711da12ec4SLe Tan     /* Fault Event Control Register, 32-bit */
29721da12ec4SLe Tan     case DMAR_FECTL_REG:
29731da12ec4SLe Tan         assert(size == 4);
29741da12ec4SLe Tan         vtd_set_long(s, addr, val);
29751da12ec4SLe Tan         vtd_handle_fectl_write(s);
29761da12ec4SLe Tan         break;
29771da12ec4SLe Tan 
29781da12ec4SLe Tan     /* Fault Event Data Register, 32-bit */
29791da12ec4SLe Tan     case DMAR_FEDATA_REG:
29801da12ec4SLe Tan         assert(size == 4);
29811da12ec4SLe Tan         vtd_set_long(s, addr, val);
29821da12ec4SLe Tan         break;
29831da12ec4SLe Tan 
29841da12ec4SLe Tan     /* Fault Event Address Register, 32-bit */
29851da12ec4SLe Tan     case DMAR_FEADDR_REG:
2986b7a7bb35SJan Kiszka         if (size == 4) {
29871da12ec4SLe Tan             vtd_set_long(s, addr, val);
2988b7a7bb35SJan Kiszka         } else {
2989b7a7bb35SJan Kiszka             /*
2990b7a7bb35SJan Kiszka              * While the register is 32-bit only, some guests (Xen...) write to
2991b7a7bb35SJan Kiszka              * it with 64-bit.
2992b7a7bb35SJan Kiszka              */
2993b7a7bb35SJan Kiszka             vtd_set_quad(s, addr, val);
2994b7a7bb35SJan Kiszka         }
29951da12ec4SLe Tan         break;
29961da12ec4SLe Tan 
29971da12ec4SLe Tan     /* Fault Event Upper Address Register, 32-bit */
29981da12ec4SLe Tan     case DMAR_FEUADDR_REG:
29991da12ec4SLe Tan         assert(size == 4);
30001da12ec4SLe Tan         vtd_set_long(s, addr, val);
30011da12ec4SLe Tan         break;
30021da12ec4SLe Tan 
30031da12ec4SLe Tan     /* Protected Memory Enable Register, 32-bit */
30041da12ec4SLe Tan     case DMAR_PMEN_REG:
30051da12ec4SLe Tan         assert(size == 4);
30061da12ec4SLe Tan         vtd_set_long(s, addr, val);
30071da12ec4SLe Tan         break;
30081da12ec4SLe Tan 
30091da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
30101da12ec4SLe Tan     case DMAR_RTADDR_REG:
30111da12ec4SLe Tan         if (size == 4) {
30121da12ec4SLe Tan             vtd_set_long(s, addr, val);
30131da12ec4SLe Tan         } else {
30141da12ec4SLe Tan             vtd_set_quad(s, addr, val);
30151da12ec4SLe Tan         }
30161da12ec4SLe Tan         break;
30171da12ec4SLe Tan 
30181da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
30191da12ec4SLe Tan         assert(size == 4);
30201da12ec4SLe Tan         vtd_set_long(s, addr, val);
30211da12ec4SLe Tan         break;
30221da12ec4SLe Tan 
3023ed7b8fbcSLe Tan     /* Invalidation Queue Tail Register, 64-bit */
3024ed7b8fbcSLe Tan     case DMAR_IQT_REG:
3025ed7b8fbcSLe Tan         if (size == 4) {
3026ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
3027ed7b8fbcSLe Tan         } else {
3028ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
3029ed7b8fbcSLe Tan         }
3030ed7b8fbcSLe Tan         vtd_handle_iqt_write(s);
3031ed7b8fbcSLe Tan         break;
3032ed7b8fbcSLe Tan 
3033ed7b8fbcSLe Tan     case DMAR_IQT_REG_HI:
3034ed7b8fbcSLe Tan         assert(size == 4);
3035ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3036ed7b8fbcSLe Tan         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
3037ed7b8fbcSLe Tan         break;
3038ed7b8fbcSLe Tan 
3039ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
3040ed7b8fbcSLe Tan     case DMAR_IQA_REG:
3041ed7b8fbcSLe Tan         if (size == 4) {
3042ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
3043ed7b8fbcSLe Tan         } else {
3044ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
3045ed7b8fbcSLe Tan         }
3046147a372eSJason Wang         vtd_update_iq_dw(s);
3047ed7b8fbcSLe Tan         break;
3048ed7b8fbcSLe Tan 
3049ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
3050ed7b8fbcSLe Tan         assert(size == 4);
3051ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3052ed7b8fbcSLe Tan         break;
3053ed7b8fbcSLe Tan 
3054ed7b8fbcSLe Tan     /* Invalidation Completion Status Register, 32-bit */
3055ed7b8fbcSLe Tan     case DMAR_ICS_REG:
3056ed7b8fbcSLe Tan         assert(size == 4);
3057ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3058ed7b8fbcSLe Tan         vtd_handle_ics_write(s);
3059ed7b8fbcSLe Tan         break;
3060ed7b8fbcSLe Tan 
3061ed7b8fbcSLe Tan     /* Invalidation Event Control Register, 32-bit */
3062ed7b8fbcSLe Tan     case DMAR_IECTL_REG:
3063ed7b8fbcSLe Tan         assert(size == 4);
3064ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3065ed7b8fbcSLe Tan         vtd_handle_iectl_write(s);
3066ed7b8fbcSLe Tan         break;
3067ed7b8fbcSLe Tan 
3068ed7b8fbcSLe Tan     /* Invalidation Event Data Register, 32-bit */
3069ed7b8fbcSLe Tan     case DMAR_IEDATA_REG:
3070ed7b8fbcSLe Tan         assert(size == 4);
3071ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3072ed7b8fbcSLe Tan         break;
3073ed7b8fbcSLe Tan 
3074ed7b8fbcSLe Tan     /* Invalidation Event Address Register, 32-bit */
3075ed7b8fbcSLe Tan     case DMAR_IEADDR_REG:
3076ed7b8fbcSLe Tan         assert(size == 4);
3077ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3078ed7b8fbcSLe Tan         break;
3079ed7b8fbcSLe Tan 
3080ed7b8fbcSLe Tan     /* Invalidation Event Upper Address Register, 32-bit */
3081ed7b8fbcSLe Tan     case DMAR_IEUADDR_REG:
3082ed7b8fbcSLe Tan         assert(size == 4);
3083ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3084ed7b8fbcSLe Tan         break;
3085ed7b8fbcSLe Tan 
30861da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
30871da12ec4SLe Tan     case DMAR_FRCD_REG_0_0:
30881da12ec4SLe Tan         if (size == 4) {
30891da12ec4SLe Tan             vtd_set_long(s, addr, val);
30901da12ec4SLe Tan         } else {
30911da12ec4SLe Tan             vtd_set_quad(s, addr, val);
30921da12ec4SLe Tan         }
30931da12ec4SLe Tan         break;
30941da12ec4SLe Tan 
30951da12ec4SLe Tan     case DMAR_FRCD_REG_0_1:
30961da12ec4SLe Tan         assert(size == 4);
30971da12ec4SLe Tan         vtd_set_long(s, addr, val);
30981da12ec4SLe Tan         break;
30991da12ec4SLe Tan 
31001da12ec4SLe Tan     case DMAR_FRCD_REG_0_2:
31011da12ec4SLe Tan         if (size == 4) {
31021da12ec4SLe Tan             vtd_set_long(s, addr, val);
31031da12ec4SLe Tan         } else {
31041da12ec4SLe Tan             vtd_set_quad(s, addr, val);
31051da12ec4SLe Tan             /* May clear bit 127 (Fault), update PPF */
31061da12ec4SLe Tan             vtd_update_fsts_ppf(s);
31071da12ec4SLe Tan         }
31081da12ec4SLe Tan         break;
31091da12ec4SLe Tan 
31101da12ec4SLe Tan     case DMAR_FRCD_REG_0_3:
31111da12ec4SLe Tan         assert(size == 4);
31121da12ec4SLe Tan         vtd_set_long(s, addr, val);
31131da12ec4SLe Tan         /* May clear bit 127 (Fault), update PPF */
31141da12ec4SLe Tan         vtd_update_fsts_ppf(s);
31151da12ec4SLe Tan         break;
31161da12ec4SLe Tan 
3117a5861439SPeter Xu     case DMAR_IRTA_REG:
3118a5861439SPeter Xu         if (size == 4) {
3119a5861439SPeter Xu             vtd_set_long(s, addr, val);
3120a5861439SPeter Xu         } else {
3121a5861439SPeter Xu             vtd_set_quad(s, addr, val);
3122a5861439SPeter Xu         }
3123a5861439SPeter Xu         break;
3124a5861439SPeter Xu 
3125a5861439SPeter Xu     case DMAR_IRTA_REG_HI:
3126a5861439SPeter Xu         assert(size == 4);
3127a5861439SPeter Xu         vtd_set_long(s, addr, val);
3128a5861439SPeter Xu         break;
3129a5861439SPeter Xu 
31301da12ec4SLe Tan     default:
31311da12ec4SLe Tan         if (size == 4) {
31321da12ec4SLe Tan             vtd_set_long(s, addr, val);
31331da12ec4SLe Tan         } else {
31341da12ec4SLe Tan             vtd_set_quad(s, addr, val);
31351da12ec4SLe Tan         }
31361da12ec4SLe Tan     }
31371da12ec4SLe Tan }
31381da12ec4SLe Tan 
31393df9d748SAlexey Kardashevskiy static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
31402c91bcf2SPeter Maydell                                          IOMMUAccessFlags flag, int iommu_idx)
31411da12ec4SLe Tan {
31421da12ec4SLe Tan     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
31431da12ec4SLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
3144b9313021SPeter Xu     IOMMUTLBEntry iotlb = {
3145b9313021SPeter Xu         /* We'll fill in the rest later. */
31461da12ec4SLe Tan         .target_as = &address_space_memory,
31471da12ec4SLe Tan     };
3148b9313021SPeter Xu     bool success;
31491da12ec4SLe Tan 
3150b9313021SPeter Xu     if (likely(s->dmar_enabled)) {
3151b9313021SPeter Xu         success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
3152b9313021SPeter Xu                                          addr, flag & IOMMU_WO, &iotlb);
3153b9313021SPeter Xu     } else {
31541da12ec4SLe Tan         /* DMAR disabled, passthrough, use 4k-page*/
3155b9313021SPeter Xu         iotlb.iova = addr & VTD_PAGE_MASK_4K;
3156b9313021SPeter Xu         iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
3157b9313021SPeter Xu         iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
3158b9313021SPeter Xu         iotlb.perm = IOMMU_RW;
3159b9313021SPeter Xu         success = true;
31601da12ec4SLe Tan     }
31611da12ec4SLe Tan 
3162b9313021SPeter Xu     if (likely(success)) {
31637feb51b7SPeter Xu         trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
31647feb51b7SPeter Xu                                  VTD_PCI_SLOT(vtd_as->devfn),
31657feb51b7SPeter Xu                                  VTD_PCI_FUNC(vtd_as->devfn),
3166b9313021SPeter Xu                                  iotlb.iova, iotlb.translated_addr,
3167b9313021SPeter Xu                                  iotlb.addr_mask);
3168b9313021SPeter Xu     } else {
31694e4abd11SPeter Xu         error_report_once("%s: detected translation failure "
31704e4abd11SPeter Xu                           "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")",
31714e4abd11SPeter Xu                           __func__, pci_bus_num(vtd_as->bus),
3172b9313021SPeter Xu                           VTD_PCI_SLOT(vtd_as->devfn),
3173b9313021SPeter Xu                           VTD_PCI_FUNC(vtd_as->devfn),
3174662b4b69SPeter Xu                           addr);
3175b9313021SPeter Xu     }
31767feb51b7SPeter Xu 
3177b9313021SPeter Xu     return iotlb;
31781da12ec4SLe Tan }
31791da12ec4SLe Tan 
3180549d4005SEric Auger static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
31815bf3d319SPeter Xu                                          IOMMUNotifierFlag old,
3182549d4005SEric Auger                                          IOMMUNotifierFlag new,
3183549d4005SEric Auger                                          Error **errp)
31843cb3b154SAlex Williamson {
31853cb3b154SAlex Williamson     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
3186dd4d607eSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
318709adb0e0SJason Wang     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
31883cb3b154SAlex Williamson 
3189b8ffd7d6SJason Wang     /* TODO: add support for VFIO and vhost users */
3190b8ffd7d6SJason Wang     if (s->snoop_control) {
3191250227f4SJason Wang         error_setg_errno(errp, ENOTSUP,
3192b8ffd7d6SJason Wang                          "Snoop Control with vhost or VFIO is not supported");
3193b8ffd7d6SJason Wang         return -ENOTSUP;
3194b8ffd7d6SJason Wang     }
3195b8d78277SJason Wang     if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
3196b8d78277SJason Wang         error_setg_errno(errp, ENOTSUP,
3197b8d78277SJason Wang                          "device %02x.%02x.%x requires caching mode",
3198b8d78277SJason Wang                          pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
3199b8d78277SJason Wang                          PCI_FUNC(vtd_as->devfn));
3200b8d78277SJason Wang         return -ENOTSUP;
3201b8d78277SJason Wang     }
320209adb0e0SJason Wang     if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
320309adb0e0SJason Wang         error_setg_errno(errp, ENOTSUP,
320409adb0e0SJason Wang                          "device %02x.%02x.%x requires device IOTLB mode",
320509adb0e0SJason Wang                          pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
320609adb0e0SJason Wang                          PCI_FUNC(vtd_as->devfn));
320709adb0e0SJason Wang         return -ENOTSUP;
320809adb0e0SJason Wang     }
3209b8ffd7d6SJason Wang 
32104f8a62a9SPeter Xu     /* Update per-address-space notifier flags */
32114f8a62a9SPeter Xu     vtd_as->notifier_flags = new;
32124f8a62a9SPeter Xu 
3213dd4d607eSPeter Xu     if (old == IOMMU_NOTIFIER_NONE) {
3214b4a4ba0dSPeter Xu         QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
3215b4a4ba0dSPeter Xu     } else if (new == IOMMU_NOTIFIER_NONE) {
3216b4a4ba0dSPeter Xu         QLIST_REMOVE(vtd_as, next);
3217dd4d607eSPeter Xu     }
3218549d4005SEric Auger     return 0;
32193cb3b154SAlex Williamson }
32203cb3b154SAlex Williamson 
3221552a1e01SPeter Xu static int vtd_post_load(void *opaque, int version_id)
3222552a1e01SPeter Xu {
3223552a1e01SPeter Xu     IntelIOMMUState *iommu = opaque;
3224552a1e01SPeter Xu 
3225552a1e01SPeter Xu     /*
32262811af3bSPeter Xu      * We don't need to migrate the root_scalable because we can
32272811af3bSPeter Xu      * simply do the calculation after the loading is complete.  We
32282811af3bSPeter Xu      * can actually do similar things with root, dmar_enabled, etc.
32292811af3bSPeter Xu      * however since we've had them already so we'd better keep them
32302811af3bSPeter Xu      * for compatibility of migration.
32312811af3bSPeter Xu      */
32322811af3bSPeter Xu     vtd_update_scalable_state(iommu);
32332811af3bSPeter Xu 
3234147a372eSJason Wang     vtd_update_iq_dw(iommu);
3235147a372eSJason Wang 
3236ceb05895SJason Wang     /*
3237ceb05895SJason Wang      * Memory regions are dynamically turned on/off depending on
3238ceb05895SJason Wang      * context entry configurations from the guest. After migration,
3239ceb05895SJason Wang      * we need to make sure the memory regions are still correct.
3240ceb05895SJason Wang      */
3241ceb05895SJason Wang     vtd_switch_address_space_all(iommu);
3242ceb05895SJason Wang 
3243552a1e01SPeter Xu     return 0;
3244552a1e01SPeter Xu }
3245552a1e01SPeter Xu 
32461da12ec4SLe Tan static const VMStateDescription vtd_vmstate = {
32471da12ec4SLe Tan     .name = "iommu-intel",
32488cdcf3c1SPeter Xu     .version_id = 1,
32498cdcf3c1SPeter Xu     .minimum_version_id = 1,
32508cdcf3c1SPeter Xu     .priority = MIG_PRI_IOMMU,
3251552a1e01SPeter Xu     .post_load = vtd_post_load,
32528cdcf3c1SPeter Xu     .fields = (VMStateField[]) {
32538cdcf3c1SPeter Xu         VMSTATE_UINT64(root, IntelIOMMUState),
32548cdcf3c1SPeter Xu         VMSTATE_UINT64(intr_root, IntelIOMMUState),
32558cdcf3c1SPeter Xu         VMSTATE_UINT64(iq, IntelIOMMUState),
32568cdcf3c1SPeter Xu         VMSTATE_UINT32(intr_size, IntelIOMMUState),
32578cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_head, IntelIOMMUState),
32588cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_tail, IntelIOMMUState),
32598cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_size, IntelIOMMUState),
32608cdcf3c1SPeter Xu         VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
32618cdcf3c1SPeter Xu         VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
32628cdcf3c1SPeter Xu         VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
326381fb1e64SPeter Xu         VMSTATE_UNUSED(1),      /* bool root_extended is obsolete by VT-d */
32648cdcf3c1SPeter Xu         VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
32658cdcf3c1SPeter Xu         VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
32668cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
32678cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_eime, IntelIOMMUState),
32688cdcf3c1SPeter Xu         VMSTATE_END_OF_LIST()
32698cdcf3c1SPeter Xu     }
32701da12ec4SLe Tan };
32711da12ec4SLe Tan 
32721da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = {
32731da12ec4SLe Tan     .read = vtd_mem_read,
32741da12ec4SLe Tan     .write = vtd_mem_write,
32751da12ec4SLe Tan     .endianness = DEVICE_LITTLE_ENDIAN,
32761da12ec4SLe Tan     .impl = {
32771da12ec4SLe Tan         .min_access_size = 4,
32781da12ec4SLe Tan         .max_access_size = 8,
32791da12ec4SLe Tan     },
32801da12ec4SLe Tan     .valid = {
32811da12ec4SLe Tan         .min_access_size = 4,
32821da12ec4SLe Tan         .max_access_size = 8,
32831da12ec4SLe Tan     },
32841da12ec4SLe Tan };
32851da12ec4SLe Tan 
32861da12ec4SLe Tan static Property vtd_properties[] = {
32871da12ec4SLe Tan     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
3288e6b6af05SRadim Krčmář     DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
3289e6b6af05SRadim Krčmář                             ON_OFF_AUTO_AUTO),
3290fb506e70SRadim Krčmář     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
32914b49b586SPeter Xu     DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
329237f51384SPrasad Singamsetty                       VTD_HOST_ADDRESS_WIDTH),
32933b40f0e5SAviv Ben-David     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
32944a4f219eSYi Sun     DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
3295b8ffd7d6SJason Wang     DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
32961b2b1237SJason Wang     DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
3297ccc23bb0SPeter Xu     DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
32988646d9c7SDavid Woodhouse     DEFINE_PROP_BOOL("dma-translation", IntelIOMMUState, dma_translation, true),
32991da12ec4SLe Tan     DEFINE_PROP_END_OF_LIST(),
33001da12ec4SLe Tan };
33011da12ec4SLe Tan 
3302651e4cefSPeter Xu /* Read IRTE entry with specific index */
3303651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
3304bc38ee10SMichael S. Tsirkin                         VTD_IR_TableEntry *entry, uint16_t sid)
3305651e4cefSPeter Xu {
3306ede9c94aSPeter Xu     static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
3307ede9c94aSPeter Xu         {0xffff, 0xfffb, 0xfff9, 0xfff8};
3308651e4cefSPeter Xu     dma_addr_t addr = 0x00;
3309ede9c94aSPeter Xu     uint16_t mask, source_id;
3310ede9c94aSPeter Xu     uint8_t bus, bus_max, bus_min;
3311651e4cefSPeter Xu 
33123c507c26SJan Kiszka     if (index >= iommu->intr_size) {
33133c507c26SJan Kiszka         error_report_once("%s: index too large: ind=0x%x",
33143c507c26SJan Kiszka                           __func__, index);
33153c507c26SJan Kiszka         return -VTD_FR_IR_INDEX_OVER;
33163c507c26SJan Kiszka     }
33173c507c26SJan Kiszka 
3318651e4cefSPeter Xu     addr = iommu->intr_root + index * sizeof(*entry);
3319ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
3320ba06fe8aSPhilippe Mathieu-Daudé                         entry, sizeof(*entry), MEMTXATTRS_UNSPECIFIED)) {
33211376211fSPeter Xu         error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64,
33221376211fSPeter Xu                           __func__, index, addr);
3323651e4cefSPeter Xu         return -VTD_FR_IR_ROOT_INVAL;
3324651e4cefSPeter Xu     }
3325651e4cefSPeter Xu 
33267feb51b7SPeter Xu     trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
33277feb51b7SPeter Xu                           le64_to_cpu(entry->data[0]));
33287feb51b7SPeter Xu 
3329bc38ee10SMichael S. Tsirkin     if (!entry->irte.present) {
33304e4abd11SPeter Xu         error_report_once("%s: detected non-present IRTE "
33314e4abd11SPeter Xu                           "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
33324e4abd11SPeter Xu                           __func__, index, le64_to_cpu(entry->data[1]),
3333651e4cefSPeter Xu                           le64_to_cpu(entry->data[0]));
3334651e4cefSPeter Xu         return -VTD_FR_IR_ENTRY_P;
3335651e4cefSPeter Xu     }
3336651e4cefSPeter Xu 
3337bc38ee10SMichael S. Tsirkin     if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
3338bc38ee10SMichael S. Tsirkin         entry->irte.__reserved_2) {
33394e4abd11SPeter Xu         error_report_once("%s: detected non-zero reserved IRTE "
33404e4abd11SPeter Xu                           "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
33414e4abd11SPeter Xu                           __func__, index, le64_to_cpu(entry->data[1]),
3342651e4cefSPeter Xu                           le64_to_cpu(entry->data[0]));
3343651e4cefSPeter Xu         return -VTD_FR_IR_IRTE_RSVD;
3344651e4cefSPeter Xu     }
3345651e4cefSPeter Xu 
3346ede9c94aSPeter Xu     if (sid != X86_IOMMU_SID_INVALID) {
3347ede9c94aSPeter Xu         /* Validate IRTE SID */
3348bc38ee10SMichael S. Tsirkin         source_id = le32_to_cpu(entry->irte.source_id);
3349bc38ee10SMichael S. Tsirkin         switch (entry->irte.sid_vtype) {
3350ede9c94aSPeter Xu         case VTD_SVT_NONE:
3351ede9c94aSPeter Xu             break;
3352ede9c94aSPeter Xu 
3353ede9c94aSPeter Xu         case VTD_SVT_ALL:
3354bc38ee10SMichael S. Tsirkin             mask = vtd_svt_mask[entry->irte.sid_q];
3355ede9c94aSPeter Xu             if ((source_id & mask) != (sid & mask)) {
33564e4abd11SPeter Xu                 error_report_once("%s: invalid IRTE SID "
33574e4abd11SPeter Xu                                   "(index=%u, sid=%u, source_id=%u)",
33584e4abd11SPeter Xu                                   __func__, index, sid, source_id);
3359ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
3360ede9c94aSPeter Xu             }
3361ede9c94aSPeter Xu             break;
3362ede9c94aSPeter Xu 
3363ede9c94aSPeter Xu         case VTD_SVT_BUS:
3364ede9c94aSPeter Xu             bus_max = source_id >> 8;
3365ede9c94aSPeter Xu             bus_min = source_id & 0xff;
3366ede9c94aSPeter Xu             bus = sid >> 8;
3367ede9c94aSPeter Xu             if (bus > bus_max || bus < bus_min) {
33684e4abd11SPeter Xu                 error_report_once("%s: invalid SVT_BUS "
33694e4abd11SPeter Xu                                   "(index=%u, bus=%u, min=%u, max=%u)",
33704e4abd11SPeter Xu                                   __func__, index, bus, bus_min, bus_max);
3371ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
3372ede9c94aSPeter Xu             }
3373ede9c94aSPeter Xu             break;
3374ede9c94aSPeter Xu 
3375ede9c94aSPeter Xu         default:
33764e4abd11SPeter Xu             error_report_once("%s: detected invalid IRTE SVT "
33774e4abd11SPeter Xu                               "(index=%u, type=%d)", __func__,
33784e4abd11SPeter Xu                               index, entry->irte.sid_vtype);
3379ede9c94aSPeter Xu             /* Take this as verification failure. */
3380ede9c94aSPeter Xu             return -VTD_FR_IR_SID_ERR;
3381ede9c94aSPeter Xu         }
3382ede9c94aSPeter Xu     }
3383651e4cefSPeter Xu 
3384651e4cefSPeter Xu     return 0;
3385651e4cefSPeter Xu }
3386651e4cefSPeter Xu 
3387651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */
3388ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
338935c24501SSingh, Brijesh                              X86IOMMUIrq *irq, uint16_t sid)
3390651e4cefSPeter Xu {
3391bc38ee10SMichael S. Tsirkin     VTD_IR_TableEntry irte = {};
3392651e4cefSPeter Xu     int ret = 0;
3393651e4cefSPeter Xu 
3394ede9c94aSPeter Xu     ret = vtd_irte_get(iommu, index, &irte, sid);
3395651e4cefSPeter Xu     if (ret) {
3396651e4cefSPeter Xu         return ret;
3397651e4cefSPeter Xu     }
3398651e4cefSPeter Xu 
3399bc38ee10SMichael S. Tsirkin     irq->trigger_mode = irte.irte.trigger_mode;
3400bc38ee10SMichael S. Tsirkin     irq->vector = irte.irte.vector;
3401bc38ee10SMichael S. Tsirkin     irq->delivery_mode = irte.irte.delivery_mode;
3402bc38ee10SMichael S. Tsirkin     irq->dest = le32_to_cpu(irte.irte.dest_id);
340328589311SJan Kiszka     if (!iommu->intr_eime) {
3404651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_MASK         (0xff00ULL)
3405651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_SHIFT        (8)
340628589311SJan Kiszka         irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
3407651e4cefSPeter Xu             VTD_IR_APIC_DEST_SHIFT;
340828589311SJan Kiszka     }
3409bc38ee10SMichael S. Tsirkin     irq->dest_mode = irte.irte.dest_mode;
3410bc38ee10SMichael S. Tsirkin     irq->redir_hint = irte.irte.redir_hint;
3411651e4cefSPeter Xu 
34127feb51b7SPeter Xu     trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
34137feb51b7SPeter Xu                        irq->delivery_mode, irq->dest, irq->dest_mode);
3414651e4cefSPeter Xu 
3415651e4cefSPeter Xu     return 0;
3416651e4cefSPeter Xu }
3417651e4cefSPeter Xu 
3418651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */
3419651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
3420651e4cefSPeter Xu                                    MSIMessage *origin,
3421ede9c94aSPeter Xu                                    MSIMessage *translated,
3422ede9c94aSPeter Xu                                    uint16_t sid)
3423651e4cefSPeter Xu {
3424651e4cefSPeter Xu     int ret = 0;
3425651e4cefSPeter Xu     VTD_IR_MSIAddress addr;
3426651e4cefSPeter Xu     uint16_t index;
342735c24501SSingh, Brijesh     X86IOMMUIrq irq = {};
3428651e4cefSPeter Xu 
3429651e4cefSPeter Xu     assert(origin && translated);
3430651e4cefSPeter Xu 
34317feb51b7SPeter Xu     trace_vtd_ir_remap_msi_req(origin->address, origin->data);
34327feb51b7SPeter Xu 
3433651e4cefSPeter Xu     if (!iommu || !iommu->intr_enabled) {
3434e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
3435e7a3b91fSPeter Xu         goto out;
3436651e4cefSPeter Xu     }
3437651e4cefSPeter Xu 
3438651e4cefSPeter Xu     if (origin->address & VTD_MSI_ADDR_HI_MASK) {
34391376211fSPeter Xu         error_report_once("%s: MSI address high 32 bits non-zero detected: "
34401376211fSPeter Xu                           "address=0x%" PRIx64, __func__, origin->address);
3441651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
3442651e4cefSPeter Xu     }
3443651e4cefSPeter Xu 
3444651e4cefSPeter Xu     addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
34451a43713bSPeter Xu     if (addr.addr.__head != 0xfee) {
34461376211fSPeter Xu         error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32,
34471376211fSPeter Xu                           __func__, addr.data);
3448651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
3449651e4cefSPeter Xu     }
3450651e4cefSPeter Xu 
3451651e4cefSPeter Xu     /* This is compatible mode. */
3452bc38ee10SMichael S. Tsirkin     if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
3453e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
3454e7a3b91fSPeter Xu         goto out;
3455651e4cefSPeter Xu     }
3456651e4cefSPeter Xu 
3457bc38ee10SMichael S. Tsirkin     index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
3458651e4cefSPeter Xu 
3459651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_SUBHANDLE       (0x0000ffff)
3460651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_RESERVED        (0xffff0000)
3461651e4cefSPeter Xu 
3462bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
3463651e4cefSPeter Xu         /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
3464651e4cefSPeter Xu         index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
3465651e4cefSPeter Xu     }
3466651e4cefSPeter Xu 
3467ede9c94aSPeter Xu     ret = vtd_remap_irq_get(iommu, index, &irq, sid);
3468651e4cefSPeter Xu     if (ret) {
3469651e4cefSPeter Xu         return ret;
3470651e4cefSPeter Xu     }
3471651e4cefSPeter Xu 
3472bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
34737feb51b7SPeter Xu         trace_vtd_ir_remap_type("MSI");
3474651e4cefSPeter Xu         if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
34754e4abd11SPeter Xu             error_report_once("%s: invalid IR MSI "
34764e4abd11SPeter Xu                               "(sid=%u, address=0x%" PRIx64
34774e4abd11SPeter Xu                               ", data=0x%" PRIx32 ")",
34784e4abd11SPeter Xu                               __func__, sid, origin->address, origin->data);
3479651e4cefSPeter Xu             return -VTD_FR_IR_REQ_RSVD;
3480651e4cefSPeter Xu         }
3481651e4cefSPeter Xu     } else {
3482651e4cefSPeter Xu         uint8_t vector = origin->data & 0xff;
3483dea651a9SFeng Wu         uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
3484dea651a9SFeng Wu 
34857feb51b7SPeter Xu         trace_vtd_ir_remap_type("IOAPIC");
3486651e4cefSPeter Xu         /* IOAPIC entry vector should be aligned with IRTE vector
3487651e4cefSPeter Xu          * (see vt-d spec 5.1.5.1). */
3488651e4cefSPeter Xu         if (vector != irq.vector) {
34897feb51b7SPeter Xu             trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
3490651e4cefSPeter Xu         }
3491dea651a9SFeng Wu 
3492dea651a9SFeng Wu         /* The Trigger Mode field must match the Trigger Mode in the IRTE.
3493dea651a9SFeng Wu          * (see vt-d spec 5.1.5.1). */
3494dea651a9SFeng Wu         if (trigger_mode != irq.trigger_mode) {
34957feb51b7SPeter Xu             trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
34967feb51b7SPeter Xu                                       irq.trigger_mode);
3497dea651a9SFeng Wu         }
3498651e4cefSPeter Xu     }
3499651e4cefSPeter Xu 
3500651e4cefSPeter Xu     /*
3501651e4cefSPeter Xu      * We'd better keep the last two bits, assuming that guest OS
3502651e4cefSPeter Xu      * might modify it. Keep it does not hurt after all.
3503651e4cefSPeter Xu      */
3504bc38ee10SMichael S. Tsirkin     irq.msi_addr_last_bits = addr.addr.__not_care;
3505651e4cefSPeter Xu 
350635c24501SSingh, Brijesh     /* Translate X86IOMMUIrq to MSI message */
350735c24501SSingh, Brijesh     x86_iommu_irq_to_msi_message(&irq, translated);
3508651e4cefSPeter Xu 
3509e7a3b91fSPeter Xu out:
35107feb51b7SPeter Xu     trace_vtd_ir_remap_msi(origin->address, origin->data,
3511651e4cefSPeter Xu                            translated->address, translated->data);
3512651e4cefSPeter Xu     return 0;
3513651e4cefSPeter Xu }
3514651e4cefSPeter Xu 
35158b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
35168b5ed7dfSPeter Xu                          MSIMessage *dst, uint16_t sid)
35178b5ed7dfSPeter Xu {
3518ede9c94aSPeter Xu     return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
3519ede9c94aSPeter Xu                                    src, dst, sid);
35208b5ed7dfSPeter Xu }
35218b5ed7dfSPeter Xu 
3522651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
3523651e4cefSPeter Xu                                    uint64_t *data, unsigned size,
3524651e4cefSPeter Xu                                    MemTxAttrs attrs)
3525651e4cefSPeter Xu {
3526651e4cefSPeter Xu     return MEMTX_OK;
3527651e4cefSPeter Xu }
3528651e4cefSPeter Xu 
3529651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
3530651e4cefSPeter Xu                                     uint64_t value, unsigned size,
3531651e4cefSPeter Xu                                     MemTxAttrs attrs)
3532651e4cefSPeter Xu {
3533651e4cefSPeter Xu     int ret = 0;
353409cd058aSMichael S. Tsirkin     MSIMessage from = {}, to = {};
3535ede9c94aSPeter Xu     uint16_t sid = X86_IOMMU_SID_INVALID;
3536651e4cefSPeter Xu 
3537651e4cefSPeter Xu     from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
3538651e4cefSPeter Xu     from.data = (uint32_t) value;
3539651e4cefSPeter Xu 
3540ede9c94aSPeter Xu     if (!attrs.unspecified) {
3541ede9c94aSPeter Xu         /* We have explicit Source ID */
3542ede9c94aSPeter Xu         sid = attrs.requester_id;
3543ede9c94aSPeter Xu     }
3544ede9c94aSPeter Xu 
3545ede9c94aSPeter Xu     ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
3546651e4cefSPeter Xu     if (ret) {
3547651e4cefSPeter Xu         /* TODO: report error */
3548651e4cefSPeter Xu         /* Drop this interrupt */
3549651e4cefSPeter Xu         return MEMTX_ERROR;
3550651e4cefSPeter Xu     }
3551651e4cefSPeter Xu 
3552eaaaf8abSPaolo Bonzini     apic_get_class(NULL)->send_msi(&to);
3553651e4cefSPeter Xu 
3554651e4cefSPeter Xu     return MEMTX_OK;
3555651e4cefSPeter Xu }
3556651e4cefSPeter Xu 
3557651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = {
3558651e4cefSPeter Xu     .read_with_attrs = vtd_mem_ir_read,
3559651e4cefSPeter Xu     .write_with_attrs = vtd_mem_ir_write,
3560651e4cefSPeter Xu     .endianness = DEVICE_LITTLE_ENDIAN,
3561651e4cefSPeter Xu     .impl = {
3562651e4cefSPeter Xu         .min_access_size = 4,
3563651e4cefSPeter Xu         .max_access_size = 4,
3564651e4cefSPeter Xu     },
3565651e4cefSPeter Xu     .valid = {
3566651e4cefSPeter Xu         .min_access_size = 4,
3567651e4cefSPeter Xu         .max_access_size = 4,
3568651e4cefSPeter Xu     },
3569651e4cefSPeter Xu };
35707df953bdSKnut Omang 
35711b2b1237SJason Wang static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
35721b2b1237SJason Wang                                          hwaddr addr, bool is_write)
35731b2b1237SJason Wang {
35741b2b1237SJason Wang     IntelIOMMUState *s = vtd_as->iommu_state;
35751b2b1237SJason Wang     uint8_t bus_n = pci_bus_num(vtd_as->bus);
35761b2b1237SJason Wang     uint16_t sid = PCI_BUILD_BDF(bus_n, vtd_as->devfn);
35771b2b1237SJason Wang     bool is_fpd_set = false;
35781b2b1237SJason Wang     VTDContextEntry ce;
35791b2b1237SJason Wang 
35801b2b1237SJason Wang     assert(vtd_as->pasid != PCI_NO_PASID);
35811b2b1237SJason Wang 
35821b2b1237SJason Wang     /* Try out best to fetch FPD, we can't do anything more */
35831b2b1237SJason Wang     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
35841b2b1237SJason Wang         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
35851b2b1237SJason Wang         if (!is_fpd_set && s->root_scalable) {
35861b2b1237SJason Wang             vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
35871b2b1237SJason Wang         }
35881b2b1237SJason Wang     }
35891b2b1237SJason Wang 
35901b2b1237SJason Wang     vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR,
35911b2b1237SJason Wang                      is_fpd_set, sid, addr, is_write,
35921b2b1237SJason Wang                      true, vtd_as->pasid);
35931b2b1237SJason Wang }
35941b2b1237SJason Wang 
35951b2b1237SJason Wang static MemTxResult vtd_mem_ir_fault_read(void *opaque, hwaddr addr,
35961b2b1237SJason Wang                                          uint64_t *data, unsigned size,
35971b2b1237SJason Wang                                          MemTxAttrs attrs)
35981b2b1237SJason Wang {
35991b2b1237SJason Wang     vtd_report_ir_illegal_access(opaque, addr, false);
36001b2b1237SJason Wang 
36011b2b1237SJason Wang     return MEMTX_ERROR;
36021b2b1237SJason Wang }
36031b2b1237SJason Wang 
36041b2b1237SJason Wang static MemTxResult vtd_mem_ir_fault_write(void *opaque, hwaddr addr,
36051b2b1237SJason Wang                                           uint64_t value, unsigned size,
36061b2b1237SJason Wang                                           MemTxAttrs attrs)
36071b2b1237SJason Wang {
36081b2b1237SJason Wang     vtd_report_ir_illegal_access(opaque, addr, true);
36091b2b1237SJason Wang 
36101b2b1237SJason Wang     return MEMTX_ERROR;
36111b2b1237SJason Wang }
36121b2b1237SJason Wang 
36131b2b1237SJason Wang static const MemoryRegionOps vtd_mem_ir_fault_ops = {
36141b2b1237SJason Wang     .read_with_attrs = vtd_mem_ir_fault_read,
36151b2b1237SJason Wang     .write_with_attrs = vtd_mem_ir_fault_write,
36161b2b1237SJason Wang     .endianness = DEVICE_LITTLE_ENDIAN,
36171b2b1237SJason Wang     .impl = {
36181b2b1237SJason Wang         .min_access_size = 1,
36191b2b1237SJason Wang         .max_access_size = 8,
36201b2b1237SJason Wang     },
36211b2b1237SJason Wang     .valid = {
36221b2b1237SJason Wang         .min_access_size = 1,
36231b2b1237SJason Wang         .max_access_size = 8,
36241b2b1237SJason Wang     },
36251b2b1237SJason Wang };
36261b2b1237SJason Wang 
36271b2b1237SJason Wang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
36281b2b1237SJason Wang                                  int devfn, unsigned int pasid)
36297df953bdSKnut Omang {
3630da8d439cSJason Wang     /*
3631da8d439cSJason Wang      * We can't simply use sid here since the bus number might not be
3632da8d439cSJason Wang      * initialized by the guest.
3633da8d439cSJason Wang      */
3634da8d439cSJason Wang     struct vtd_as_key key = {
3635da8d439cSJason Wang         .bus = bus,
3636da8d439cSJason Wang         .devfn = devfn,
36371b2b1237SJason Wang         .pasid = pasid,
3638da8d439cSJason Wang     };
36397df953bdSKnut Omang     VTDAddressSpace *vtd_dev_as;
3640e0a3c8ccSJason Wang     char name[128];
36417df953bdSKnut Omang 
3642da8d439cSJason Wang     vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
36437df953bdSKnut Omang     if (!vtd_dev_as) {
3644da8d439cSJason Wang         struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
3645da8d439cSJason Wang 
3646da8d439cSJason Wang         new_key->bus = bus;
3647da8d439cSJason Wang         new_key->devfn = devfn;
36481b2b1237SJason Wang         new_key->pasid = pasid;
3649da8d439cSJason Wang 
36501b2b1237SJason Wang         if (pasid == PCI_NO_PASID) {
36514b519ef1SPeter Xu             snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
36524b519ef1SPeter Xu                      PCI_FUNC(devfn));
36531b2b1237SJason Wang         } else {
36541b2b1237SJason Wang             snprintf(name, sizeof(name), "vtd-%02x.%x-pasid-%x", PCI_SLOT(devfn),
36551b2b1237SJason Wang                      PCI_FUNC(devfn), pasid);
36561b2b1237SJason Wang         }
36571b2b1237SJason Wang 
3658da8d439cSJason Wang         vtd_dev_as = g_new0(VTDAddressSpace, 1);
36597df953bdSKnut Omang 
36607df953bdSKnut Omang         vtd_dev_as->bus = bus;
36617df953bdSKnut Omang         vtd_dev_as->devfn = (uint8_t)devfn;
36621b2b1237SJason Wang         vtd_dev_as->pasid = pasid;
36637df953bdSKnut Omang         vtd_dev_as->iommu_state = s;
36647df953bdSKnut Omang         vtd_dev_as->context_cache_entry.context_cache_gen = 0;
366563b88968SPeter Xu         vtd_dev_as->iova_tree = iova_tree_new();
3666558e0024SPeter Xu 
36674b519ef1SPeter Xu         memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX);
36684b519ef1SPeter Xu         address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root");
36694b519ef1SPeter Xu 
3670558e0024SPeter Xu         /*
36714b519ef1SPeter Xu          * Build the DMAR-disabled container with aliases to the
36724b519ef1SPeter Xu          * shared MRs.  Note that aliasing to a shared memory region
36734b519ef1SPeter Xu          * could help the memory API to detect same FlatViews so we
36744b519ef1SPeter Xu          * can have devices to share the same FlatView when DMAR is
36754b519ef1SPeter Xu          * disabled (either by not providing "intel_iommu=on" or with
36764b519ef1SPeter Xu          * "iommu=pt").  It will greatly reduce the total number of
36774b519ef1SPeter Xu          * FlatViews of the system hence VM runs faster.
3678558e0024SPeter Xu          */
36794b519ef1SPeter Xu         memory_region_init_alias(&vtd_dev_as->nodmar, OBJECT(s),
36804b519ef1SPeter Xu                                  "vtd-nodmar", &s->mr_nodmar, 0,
36814b519ef1SPeter Xu                                  memory_region_size(&s->mr_nodmar));
36824b519ef1SPeter Xu 
36834b519ef1SPeter Xu         /*
36844b519ef1SPeter Xu          * Build the per-device DMAR-enabled container.
36854b519ef1SPeter Xu          *
36864b519ef1SPeter Xu          * TODO: currently we have per-device IOMMU memory region only
36874b519ef1SPeter Xu          * because we have per-device IOMMU notifiers for devices.  If
36884b519ef1SPeter Xu          * one day we can abstract the IOMMU notifiers out of the
36894b519ef1SPeter Xu          * memory regions then we can also share the same memory
36904b519ef1SPeter Xu          * region here just like what we've done above with the nodmar
36914b519ef1SPeter Xu          * region.
36924b519ef1SPeter Xu          */
36934b519ef1SPeter Xu         strcat(name, "-dmar");
36941221a474SAlexey Kardashevskiy         memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
36951221a474SAlexey Kardashevskiy                                  TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
36964b519ef1SPeter Xu                                  name, UINT64_MAX);
36974b519ef1SPeter Xu         memory_region_init_alias(&vtd_dev_as->iommu_ir, OBJECT(s), "vtd-ir",
36984b519ef1SPeter Xu                                  &s->mr_ir, 0, memory_region_size(&s->mr_ir));
36994b519ef1SPeter Xu         memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->iommu),
3700558e0024SPeter Xu                                             VTD_INTERRUPT_ADDR_FIRST,
37014b519ef1SPeter Xu                                             &vtd_dev_as->iommu_ir, 1);
37024b519ef1SPeter Xu 
37034b519ef1SPeter Xu         /*
37041b2b1237SJason Wang          * This region is used for catching fault to access interrupt
37051b2b1237SJason Wang          * range via passthrough + PASID. See also
37061b2b1237SJason Wang          * vtd_switch_address_space(). We can't use alias since we
37071b2b1237SJason Wang          * need to know the sid which is valid for MSI who uses
37081b2b1237SJason Wang          * bus_master_as (see msi_send_message()).
37091b2b1237SJason Wang          */
37101b2b1237SJason Wang         memory_region_init_io(&vtd_dev_as->iommu_ir_fault, OBJECT(s),
37111b2b1237SJason Wang                               &vtd_mem_ir_fault_ops, vtd_dev_as, "vtd-no-ir",
37121b2b1237SJason Wang                               VTD_INTERRUPT_ADDR_SIZE);
37131b2b1237SJason Wang         /*
37141b2b1237SJason Wang          * Hook to root since when PT is enabled vtd_dev_as->iommu
37151b2b1237SJason Wang          * will be disabled.
37161b2b1237SJason Wang          */
37171b2b1237SJason Wang         memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->root),
37181b2b1237SJason Wang                                             VTD_INTERRUPT_ADDR_FIRST,
37191b2b1237SJason Wang                                             &vtd_dev_as->iommu_ir_fault, 2);
37201b2b1237SJason Wang 
37211b2b1237SJason Wang         /*
37224b519ef1SPeter Xu          * Hook both the containers under the root container, we
37234b519ef1SPeter Xu          * switch between DMAR & noDMAR by enable/disable
37244b519ef1SPeter Xu          * corresponding sub-containers
37254b519ef1SPeter Xu          */
3726558e0024SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
37273df9d748SAlexey Kardashevskiy                                             MEMORY_REGION(&vtd_dev_as->iommu),
37284b519ef1SPeter Xu                                             0);
37294b519ef1SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
37304b519ef1SPeter Xu                                             &vtd_dev_as->nodmar, 0);
37314b519ef1SPeter Xu 
3732558e0024SPeter Xu         vtd_switch_address_space(vtd_dev_as);
3733da8d439cSJason Wang 
3734da8d439cSJason Wang         g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
37357df953bdSKnut Omang     }
37367df953bdSKnut Omang     return vtd_dev_as;
37377df953bdSKnut Omang }
37387df953bdSKnut Omang 
3739dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */
3740dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
3741dd4d607eSPeter Xu {
37429a4bb839SPeter Xu     hwaddr size, remain;
3743dd4d607eSPeter Xu     hwaddr start = n->start;
3744dd4d607eSPeter Xu     hwaddr end = n->end;
374537f51384SPrasad Singamsetty     IntelIOMMUState *s = as->iommu_state;
374663b88968SPeter Xu     DMAMap map;
3747dd4d607eSPeter Xu 
3748dd4d607eSPeter Xu     /*
3749dd4d607eSPeter Xu      * Note: all the codes in this function has a assumption that IOVA
3750dd4d607eSPeter Xu      * bits are no more than VTD_MGAW bits (which is restricted by
3751dd4d607eSPeter Xu      * VT-d spec), otherwise we need to consider overflow of 64 bits.
3752dd4d607eSPeter Xu      */
3753dd4d607eSPeter Xu 
3754d6d10793SYan Zhao     if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) {
3755dd4d607eSPeter Xu         /*
3756dd4d607eSPeter Xu          * Don't need to unmap regions that is bigger than the whole
3757dd4d607eSPeter Xu          * VT-d supported address space size
3758dd4d607eSPeter Xu          */
3759d6d10793SYan Zhao         end = VTD_ADDRESS_SIZE(s->aw_bits) - 1;
3760dd4d607eSPeter Xu     }
3761dd4d607eSPeter Xu 
3762dd4d607eSPeter Xu     assert(start <= end);
37639a4bb839SPeter Xu     size = remain = end - start + 1;
3764dd4d607eSPeter Xu 
37659a4bb839SPeter Xu     while (remain >= VTD_PAGE_SIZE) {
37665039caf3SEugenio Pérez         IOMMUTLBEvent event;
3767f14fb6c2SEric Auger         uint64_t mask = dma_aligned_pow2_mask(start, end, s->aw_bits);
3768f14fb6c2SEric Auger         uint64_t size = mask + 1;
3769dd4d607eSPeter Xu 
3770f14fb6c2SEric Auger         assert(size);
37719a4bb839SPeter Xu 
37725039caf3SEugenio Pérez         event.type = IOMMU_NOTIFIER_UNMAP;
37735039caf3SEugenio Pérez         event.entry.iova = start;
3774f14fb6c2SEric Auger         event.entry.addr_mask = mask;
37755039caf3SEugenio Pérez         event.entry.target_as = &address_space_memory;
37765039caf3SEugenio Pérez         event.entry.perm = IOMMU_NONE;
3777dd4d607eSPeter Xu         /* This field is meaningless for unmap */
37785039caf3SEugenio Pérez         event.entry.translated_addr = 0;
37799a4bb839SPeter Xu 
37805039caf3SEugenio Pérez         memory_region_notify_iommu_one(n, &event);
37819a4bb839SPeter Xu 
3782f14fb6c2SEric Auger         start += size;
3783f14fb6c2SEric Auger         remain -= size;
37849a4bb839SPeter Xu     }
37859a4bb839SPeter Xu 
37869a4bb839SPeter Xu     assert(!remain);
3787dd4d607eSPeter Xu 
3788dd4d607eSPeter Xu     trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
3789dd4d607eSPeter Xu                              VTD_PCI_SLOT(as->devfn),
3790dd4d607eSPeter Xu                              VTD_PCI_FUNC(as->devfn),
37919a4bb839SPeter Xu                              n->start, size);
3792dd4d607eSPeter Xu 
37939a4bb839SPeter Xu     map.iova = n->start;
37949a4bb839SPeter Xu     map.size = size;
379569292a8eSEugenio Pérez     iova_tree_remove(as->iova_tree, map);
3796dd4d607eSPeter Xu }
3797dd4d607eSPeter Xu 
3798dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s)
3799dd4d607eSPeter Xu {
3800dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
3801dd4d607eSPeter Xu     IOMMUNotifier *n;
3802dd4d607eSPeter Xu 
3803b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
3804dd4d607eSPeter Xu         IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
3805dd4d607eSPeter Xu             vtd_address_space_unmap(vtd_as, n);
3806dd4d607eSPeter Xu         }
3807dd4d607eSPeter Xu     }
3808dd4d607eSPeter Xu }
3809dd4d607eSPeter Xu 
38102cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s)
38112cc9ddccSPeter Xu {
38122cc9ddccSPeter Xu     vtd_address_space_unmap_all(s);
38132cc9ddccSPeter Xu     vtd_switch_address_space_all(s);
38142cc9ddccSPeter Xu }
38152cc9ddccSPeter Xu 
38165039caf3SEugenio Pérez static int vtd_replay_hook(IOMMUTLBEvent *event, void *private)
3817f06a696dSPeter Xu {
38185039caf3SEugenio Pérez     memory_region_notify_iommu_one(private, event);
3819f06a696dSPeter Xu     return 0;
3820f06a696dSPeter Xu }
3821f06a696dSPeter Xu 
38223df9d748SAlexey Kardashevskiy static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
3823f06a696dSPeter Xu {
38243df9d748SAlexey Kardashevskiy     VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
3825f06a696dSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
3826f06a696dSPeter Xu     uint8_t bus_n = pci_bus_num(vtd_as->bus);
3827f06a696dSPeter Xu     VTDContextEntry ce;
3828e80c1e4cSZhenzhong Duan     DMAMap map = { .iova = 0, .size = HWADDR_MAX };
3829f06a696dSPeter Xu 
3830e80c1e4cSZhenzhong Duan     /* replay is protected by BQL, page walk will re-setup it safely */
3831e80c1e4cSZhenzhong Duan     iova_tree_remove(vtd_as->iova_tree, map);
3832dd4d607eSPeter Xu 
3833dd4d607eSPeter Xu     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
3834fb43cf73SLiu, Yi L         trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
3835fb43cf73SLiu, Yi L                                   "legacy mode",
3836fb43cf73SLiu, Yi L                                   bus_n, PCI_SLOT(vtd_as->devfn),
3837f06a696dSPeter Xu                                   PCI_FUNC(vtd_as->devfn),
38381b2b1237SJason Wang                                   vtd_get_domain_id(s, &ce, vtd_as->pasid),
3839f06a696dSPeter Xu                                   ce.hi, ce.lo);
3840*ce735ff0SZhenzhong Duan         if (n->notifier_flags & IOMMU_NOTIFIER_MAP) {
38414f8a62a9SPeter Xu             /* This is required only for MAP typed notifiers */
3842fe215b0cSPeter Xu             vtd_page_walk_info info = {
3843fe215b0cSPeter Xu                 .hook_fn = vtd_replay_hook,
3844fe215b0cSPeter Xu                 .private = (void *)n,
3845fe215b0cSPeter Xu                 .notify_unmap = false,
3846fe215b0cSPeter Xu                 .aw = s->aw_bits,
38472f764fa8SPeter Xu                 .as = vtd_as,
38481b2b1237SJason Wang                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
3849fe215b0cSPeter Xu             };
3850fe215b0cSPeter Xu 
3851b1ab8f9cSPeter Maydell             vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
38524f8a62a9SPeter Xu         }
3853f06a696dSPeter Xu     } else {
3854f06a696dSPeter Xu         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
3855f06a696dSPeter Xu                                     PCI_FUNC(vtd_as->devfn));
3856f06a696dSPeter Xu     }
3857f06a696dSPeter Xu 
3858f06a696dSPeter Xu     return;
3859f06a696dSPeter Xu }
3860f06a696dSPeter Xu 
38611da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay
38621da12ec4SLe Tan  * attention when adding new initialization stuff.
38631da12ec4SLe Tan  */
38641da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s)
38651da12ec4SLe Tan {
3866d54bd7f8SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3867d54bd7f8SPeter Xu 
38681da12ec4SLe Tan     memset(s->csr, 0, DMAR_REG_SIZE);
38691da12ec4SLe Tan     memset(s->wmask, 0, DMAR_REG_SIZE);
38701da12ec4SLe Tan     memset(s->w1cmask, 0, DMAR_REG_SIZE);
38711da12ec4SLe Tan     memset(s->womask, 0, DMAR_REG_SIZE);
38721da12ec4SLe Tan 
38731da12ec4SLe Tan     s->root = 0;
3874fb43cf73SLiu, Yi L     s->root_scalable = false;
38751da12ec4SLe Tan     s->dmar_enabled = false;
3876d7bb469aSPeter Xu     s->intr_enabled = false;
38771da12ec4SLe Tan     s->iq_head = 0;
38781da12ec4SLe Tan     s->iq_tail = 0;
38791da12ec4SLe Tan     s->iq = 0;
38801da12ec4SLe Tan     s->iq_size = 0;
38811da12ec4SLe Tan     s->qi_enabled = false;
38821da12ec4SLe Tan     s->iq_last_desc_type = VTD_INV_DESC_NONE;
3883c0c1d351SLiu, Yi L     s->iq_dw = false;
38841da12ec4SLe Tan     s->next_frcd_reg = 0;
388592e5d85eSPrasad Singamsetty     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
388692e5d85eSPrasad Singamsetty              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
38878646d9c7SDavid Woodhouse              VTD_CAP_MGAW(s->aw_bits);
3888ccc23bb0SPeter Xu     if (s->dma_drain) {
3889ccc23bb0SPeter Xu         s->cap |= VTD_CAP_DRAIN;
3890ccc23bb0SPeter Xu     }
38918646d9c7SDavid Woodhouse     if (s->dma_translation) {
38928646d9c7SDavid Woodhouse             if (s->aw_bits >= VTD_HOST_AW_39BIT) {
38938646d9c7SDavid Woodhouse                     s->cap |= VTD_CAP_SAGAW_39bit;
38948646d9c7SDavid Woodhouse             }
38958646d9c7SDavid Woodhouse             if (s->aw_bits >= VTD_HOST_AW_48BIT) {
389637f51384SPrasad Singamsetty                     s->cap |= VTD_CAP_SAGAW_48bit;
389737f51384SPrasad Singamsetty             }
38988646d9c7SDavid Woodhouse     }
3899ed7b8fbcSLe Tan     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
39001da12ec4SLe Tan 
390192e5d85eSPrasad Singamsetty     /*
390292e5d85eSPrasad Singamsetty      * Rsvd field masks for spte
390392e5d85eSPrasad Singamsetty      */
3904ce586f3bSQi, Yadong     vtd_spte_rsvd[0] = ~0ULL;
3905e48929c7SQi, Yadong     vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
3906e48929c7SQi, Yadong                                                   x86_iommu->dt_supported);
3907ce586f3bSQi, Yadong     vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
3908ce586f3bSQi, Yadong     vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
3909ce586f3bSQi, Yadong     vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
3910ce586f3bSQi, Yadong 
3911e48929c7SQi, Yadong     vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
3912e48929c7SQi, Yadong                                                          x86_iommu->dt_supported);
3913e48929c7SQi, Yadong     vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
3914e48929c7SQi, Yadong                                                          x86_iommu->dt_supported);
391592e5d85eSPrasad Singamsetty 
3916b8ffd7d6SJason Wang     if (s->scalable_mode || s->snoop_control) {
39170192d667SJason Wang         vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
39180192d667SJason Wang         vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
39190192d667SJason Wang         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
39200192d667SJason Wang     }
39210192d667SJason Wang 
3922a924b3d8SPeter Xu     if (x86_iommu_ir_supported(x86_iommu)) {
3923e6b6af05SRadim Krčmář         s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
3924e6b6af05SRadim Krčmář         if (s->intr_eim == ON_OFF_AUTO_ON) {
3925e6b6af05SRadim Krčmář             s->ecap |= VTD_ECAP_EIM;
3926e6b6af05SRadim Krčmář         }
3927e6b6af05SRadim Krčmář         assert(s->intr_eim != ON_OFF_AUTO_AUTO);
3928d54bd7f8SPeter Xu     }
3929d54bd7f8SPeter Xu 
3930554f5e16SJason Wang     if (x86_iommu->dt_supported) {
3931554f5e16SJason Wang         s->ecap |= VTD_ECAP_DT;
3932554f5e16SJason Wang     }
3933554f5e16SJason Wang 
3934dbaabb25SPeter Xu     if (x86_iommu->pt_supported) {
3935dbaabb25SPeter Xu         s->ecap |= VTD_ECAP_PT;
3936dbaabb25SPeter Xu     }
3937dbaabb25SPeter Xu 
39383b40f0e5SAviv Ben-David     if (s->caching_mode) {
39393b40f0e5SAviv Ben-David         s->cap |= VTD_CAP_CM;
39403b40f0e5SAviv Ben-David     }
39413b40f0e5SAviv Ben-David 
39424a4f219eSYi Sun     /* TODO: read cap/ecap from host to decide which cap to be exposed. */
39434a4f219eSYi Sun     if (s->scalable_mode) {
39444a4f219eSYi Sun         s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
39454a4f219eSYi Sun     }
39464a4f219eSYi Sun 
3947b8ffd7d6SJason Wang     if (s->snoop_control) {
3948b8ffd7d6SJason Wang         s->ecap |= VTD_ECAP_SC;
3949b8ffd7d6SJason Wang     }
3950b8ffd7d6SJason Wang 
39511b2b1237SJason Wang     if (s->pasid) {
39521b2b1237SJason Wang         s->ecap |= VTD_ECAP_PASID;
39531b2b1237SJason Wang     }
39541b2b1237SJason Wang 
395506aba4caSPeter Xu     vtd_reset_caches(s);
3956d92fa2dcSLe Tan 
39571da12ec4SLe Tan     /* Define registers with default values and bit semantics */
39581da12ec4SLe Tan     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
39591da12ec4SLe Tan     vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
39601da12ec4SLe Tan     vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
39611da12ec4SLe Tan     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
39621da12ec4SLe Tan     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
39631da12ec4SLe Tan     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
3964fb43cf73SLiu, Yi L     vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0);
39651da12ec4SLe Tan     vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
39661da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
39671da12ec4SLe Tan 
39681da12ec4SLe Tan     /* Advanced Fault Logging not supported */
39691da12ec4SLe Tan     vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
39701da12ec4SLe Tan     vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
39711da12ec4SLe Tan     vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
39721da12ec4SLe Tan     vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
39731da12ec4SLe Tan 
39741da12ec4SLe Tan     /* Treated as RsvdZ when EIM in ECAP_REG is not supported
39751da12ec4SLe Tan      * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
39761da12ec4SLe Tan      */
39771da12ec4SLe Tan     vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
39781da12ec4SLe Tan 
39791da12ec4SLe Tan     /* Treated as RO for implementations that PLMR and PHMR fields reported
39801da12ec4SLe Tan      * as Clear in the CAP_REG.
39811da12ec4SLe Tan      * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
39821da12ec4SLe Tan      */
39831da12ec4SLe Tan     vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
39841da12ec4SLe Tan 
3985ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
3986ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
3987c0c1d351SLiu, Yi L     vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff807ULL, 0);
3988ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
3989ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3990ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
3991ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
3992ed7b8fbcSLe Tan     /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
3993ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
3994ed7b8fbcSLe Tan 
39951da12ec4SLe Tan     /* IOTLB registers */
39961da12ec4SLe Tan     vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
39971da12ec4SLe Tan     vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
39981da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
39991da12ec4SLe Tan 
40001da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
40011da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
40021da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
4003a5861439SPeter Xu 
4004a5861439SPeter Xu     /*
400528589311SJan Kiszka      * Interrupt remapping registers.
4006a5861439SPeter Xu      */
400728589311SJan Kiszka     vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
40081da12ec4SLe Tan }
40091da12ec4SLe Tan 
40101da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use
40111da12ec4SLe Tan  * the address space they got at first (won't ask the bus again).
40121da12ec4SLe Tan  */
40131da12ec4SLe Tan static void vtd_reset(DeviceState *dev)
40141da12ec4SLe Tan {
40151da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
40161da12ec4SLe Tan 
40171da12ec4SLe Tan     vtd_init(s);
40182cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
40191da12ec4SLe Tan }
40201da12ec4SLe Tan 
4021621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
4022621d983aSMarcel Apfelbaum {
4023621d983aSMarcel Apfelbaum     IntelIOMMUState *s = opaque;
4024621d983aSMarcel Apfelbaum     VTDAddressSpace *vtd_as;
4025621d983aSMarcel Apfelbaum 
4026bf33cc75SPeter Xu     assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
4027621d983aSMarcel Apfelbaum 
40281b2b1237SJason Wang     vtd_as = vtd_find_add_as(s, bus, devfn, PCI_NO_PASID);
4029621d983aSMarcel Apfelbaum     return &vtd_as->as;
4030621d983aSMarcel Apfelbaum }
4031621d983aSMarcel Apfelbaum 
4032e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
40336333e93cSRadim Krčmář {
4034e6b6af05SRadim Krčmář     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
4035e6b6af05SRadim Krčmář 
4036a924b3d8SPeter Xu     if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) {
4037e6b6af05SRadim Krčmář         error_setg(errp, "eim=on cannot be selected without intremap=on");
4038e6b6af05SRadim Krčmář         return false;
4039e6b6af05SRadim Krčmář     }
4040e6b6af05SRadim Krčmář 
4041e6b6af05SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_AUTO) {
4042fb506e70SRadim Krčmář         s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
4043a924b3d8SPeter Xu                       && x86_iommu_ir_supported(x86_iommu) ?
4044e6b6af05SRadim Krčmář                                               ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
4045e6b6af05SRadim Krčmář     }
4046fb506e70SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
404777250171SDavid Woodhouse         if (!kvm_irqchip_is_split()) {
4048fb506e70SRadim Krčmář             error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
4049fb506e70SRadim Krčmář             return false;
4050fb506e70SRadim Krčmář         }
405120ca4742SPeter Xu         if (!kvm_enable_x2apic()) {
405220ca4742SPeter Xu             error_setg(errp, "eim=on requires support on the KVM side"
405320ca4742SPeter Xu                              "(X2APIC_API, first shipped in v4.7)");
405420ca4742SPeter Xu             return false;
405520ca4742SPeter Xu         }
4056fb506e70SRadim Krčmář     }
4057e6b6af05SRadim Krčmář 
405837f51384SPrasad Singamsetty     /* Currently only address widths supported are 39 and 48 bits */
405937f51384SPrasad Singamsetty     if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
406037f51384SPrasad Singamsetty         (s->aw_bits != VTD_HOST_AW_48BIT)) {
40612a345149SMenno Lageman         error_setg(errp, "Supported values for aw-bits are: %d, %d",
406237f51384SPrasad Singamsetty                    VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
406337f51384SPrasad Singamsetty         return false;
406437f51384SPrasad Singamsetty     }
406537f51384SPrasad Singamsetty 
40664a4f219eSYi Sun     if (s->scalable_mode && !s->dma_drain) {
40674a4f219eSYi Sun         error_setg(errp, "Need to set dma_drain for scalable mode");
40684a4f219eSYi Sun         return false;
40694a4f219eSYi Sun     }
40704a4f219eSYi Sun 
40711b2b1237SJason Wang     if (s->pasid && !s->scalable_mode) {
40721b2b1237SJason Wang         error_setg(errp, "Need to set scalable mode for PASID");
40731b2b1237SJason Wang         return false;
40741b2b1237SJason Wang     }
40751b2b1237SJason Wang 
40766333e93cSRadim Krčmář     return true;
40776333e93cSRadim Krčmář }
40786333e93cSRadim Krčmář 
407928cf553aSPeter Xu static int vtd_machine_done_notify_one(Object *child, void *unused)
408028cf553aSPeter Xu {
408128cf553aSPeter Xu     IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
408228cf553aSPeter Xu 
408328cf553aSPeter Xu     /*
408428cf553aSPeter Xu      * We hard-coded here because vfio-pci is the only special case
408528cf553aSPeter Xu      * here.  Let's be more elegant in the future when we can, but so
408628cf553aSPeter Xu      * far there seems to be no better way.
408728cf553aSPeter Xu      */
408828cf553aSPeter Xu     if (object_dynamic_cast(child, "vfio-pci") && !iommu->caching_mode) {
408928cf553aSPeter Xu         vtd_panic_require_caching_mode();
409028cf553aSPeter Xu     }
409128cf553aSPeter Xu 
409228cf553aSPeter Xu     return 0;
409328cf553aSPeter Xu }
409428cf553aSPeter Xu 
409528cf553aSPeter Xu static void vtd_machine_done_hook(Notifier *notifier, void *unused)
409628cf553aSPeter Xu {
409728cf553aSPeter Xu     object_child_foreach_recursive(object_get_root(),
409828cf553aSPeter Xu                                    vtd_machine_done_notify_one, NULL);
409928cf553aSPeter Xu }
410028cf553aSPeter Xu 
410128cf553aSPeter Xu static Notifier vtd_machine_done_notify = {
410228cf553aSPeter Xu     .notify = vtd_machine_done_hook,
410328cf553aSPeter Xu };
410428cf553aSPeter Xu 
41051da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp)
41061da12ec4SLe Tan {
4107ef0e8fc7SEduardo Habkost     MachineState *ms = MACHINE(qdev_get_machine());
410829396ed9SMohammed Gamal     PCMachineState *pcms = PC_MACHINE(ms);
4109f0bb276bSPaolo Bonzini     X86MachineState *x86ms = X86_MACHINE(ms);
411029396ed9SMohammed Gamal     PCIBus *bus = pcms->bus;
41111da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
41121b2b1237SJason Wang     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
41131b2b1237SJason Wang 
41141b2b1237SJason Wang     if (s->pasid && x86_iommu->dt_supported) {
41151b2b1237SJason Wang         /*
41161b2b1237SJason Wang          * PASID-based-Device-TLB Invalidate Descriptor is not
41171b2b1237SJason Wang          * implemented and it requires support from vhost layer which
41181b2b1237SJason Wang          * needs to be implemented in the future.
41191b2b1237SJason Wang          */
41201b2b1237SJason Wang         error_setg(errp, "PASID based device IOTLB is not supported");
41211b2b1237SJason Wang         return;
41221b2b1237SJason Wang     }
41236333e93cSRadim Krčmář 
4124e6b6af05SRadim Krčmář     if (!vtd_decide_config(s, errp)) {
41256333e93cSRadim Krčmář         return;
41266333e93cSRadim Krčmář     }
41276333e93cSRadim Krčmář 
4128b4a4ba0dSPeter Xu     QLIST_INIT(&s->vtd_as_with_notifiers);
41291d9efa73SPeter Xu     qemu_mutex_init(&s->iommu_lock);
41301da12ec4SLe Tan     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
41311da12ec4SLe Tan                           "intel_iommu", DMAR_REG_SIZE);
41324b519ef1SPeter Xu 
41334b519ef1SPeter Xu     /* Create the shared memory regions by all devices */
41344b519ef1SPeter Xu     memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar",
41354b519ef1SPeter Xu                        UINT64_MAX);
41364b519ef1SPeter Xu     memory_region_init_io(&s->mr_ir, OBJECT(s), &vtd_mem_ir_ops,
41374b519ef1SPeter Xu                           s, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE);
41384b519ef1SPeter Xu     memory_region_init_alias(&s->mr_sys_alias, OBJECT(s),
41394b519ef1SPeter Xu                              "vtd-sys-alias", get_system_memory(), 0,
41404b519ef1SPeter Xu                              memory_region_size(get_system_memory()));
41414b519ef1SPeter Xu     memory_region_add_subregion_overlap(&s->mr_nodmar, 0,
41424b519ef1SPeter Xu                                         &s->mr_sys_alias, 0);
41434b519ef1SPeter Xu     memory_region_add_subregion_overlap(&s->mr_nodmar,
41444b519ef1SPeter Xu                                         VTD_INTERRUPT_ADDR_FIRST,
41454b519ef1SPeter Xu                                         &s->mr_ir, 1);
41464b519ef1SPeter Xu 
41471da12ec4SLe Tan     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
4148b5a280c0SLe Tan     /* No corresponding destroy */
41491b2b1237SJason Wang     s->iotlb = g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal,
4150b5a280c0SLe Tan                                      g_free, g_free);
4151da8d439cSJason Wang     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
41527df953bdSKnut Omang                                       g_free, g_free);
41531da12ec4SLe Tan     vtd_init(s);
4154621d983aSMarcel Apfelbaum     sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
4155621d983aSMarcel Apfelbaum     pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
4156cb135f59SPeter Xu     /* Pseudo address space under root PCI bus. */
4157f0bb276bSPaolo Bonzini     x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
415828cf553aSPeter Xu     qemu_add_machine_init_done_notifier(&vtd_machine_done_notify);
41591da12ec4SLe Tan }
41601da12ec4SLe Tan 
41611da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data)
41621da12ec4SLe Tan {
41631da12ec4SLe Tan     DeviceClass *dc = DEVICE_CLASS(klass);
416430c60f77SEduardo Habkost     X86IOMMUClass *x86_class = X86_IOMMU_DEVICE_CLASS(klass);
41651da12ec4SLe Tan 
41661da12ec4SLe Tan     dc->reset = vtd_reset;
41671da12ec4SLe Tan     dc->vmsd = &vtd_vmstate;
41684f67d30bSMarc-André Lureau     device_class_set_props(dc, vtd_properties);
4169621d983aSMarcel Apfelbaum     dc->hotpluggable = false;
41701c7955c4SPeter Xu     x86_class->realize = vtd_realize;
41718b5ed7dfSPeter Xu     x86_class->int_remap = vtd_int_remap;
41728ab5700cSEduardo Habkost     /* Supported by the pc-q35-* machine types */
4173e4f4fb1eSEduardo Habkost     dc->user_creatable = true;
41741ec202c9SErnest Esene     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
41751ec202c9SErnest Esene     dc->desc = "Intel IOMMU (VT-d) DMA Remapping device";
41761da12ec4SLe Tan }
41771da12ec4SLe Tan 
41781da12ec4SLe Tan static const TypeInfo vtd_info = {
41791da12ec4SLe Tan     .name          = TYPE_INTEL_IOMMU_DEVICE,
41801c7955c4SPeter Xu     .parent        = TYPE_X86_IOMMU_DEVICE,
41811da12ec4SLe Tan     .instance_size = sizeof(IntelIOMMUState),
41821da12ec4SLe Tan     .class_init    = vtd_class_init,
41831da12ec4SLe Tan };
41841da12ec4SLe Tan 
41851221a474SAlexey Kardashevskiy static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
41861221a474SAlexey Kardashevskiy                                                      void *data)
41871221a474SAlexey Kardashevskiy {
41881221a474SAlexey Kardashevskiy     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
41891221a474SAlexey Kardashevskiy 
41901221a474SAlexey Kardashevskiy     imrc->translate = vtd_iommu_translate;
41911221a474SAlexey Kardashevskiy     imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
41921221a474SAlexey Kardashevskiy     imrc->replay = vtd_iommu_replay;
41931221a474SAlexey Kardashevskiy }
41941221a474SAlexey Kardashevskiy 
41951221a474SAlexey Kardashevskiy static const TypeInfo vtd_iommu_memory_region_info = {
41961221a474SAlexey Kardashevskiy     .parent = TYPE_IOMMU_MEMORY_REGION,
41971221a474SAlexey Kardashevskiy     .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
41981221a474SAlexey Kardashevskiy     .class_init = vtd_iommu_memory_region_class_init,
41991221a474SAlexey Kardashevskiy };
42001221a474SAlexey Kardashevskiy 
42011da12ec4SLe Tan static void vtd_register_types(void)
42021da12ec4SLe Tan {
42031da12ec4SLe Tan     type_register_static(&vtd_info);
42041221a474SAlexey Kardashevskiy     type_register_static(&vtd_iommu_memory_region_info);
42051da12ec4SLe Tan }
42061da12ec4SLe Tan 
42071da12ec4SLe Tan type_init(vtd_register_types)
4208