xref: /openbmc/qemu/hw/i386/intel_iommu.c (revision 3e090e3489dbae7af65465d357a4772f012f4d90)
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     uint32_t level;
681b2b1237SJason Wang     uint16_t sid;
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;
2241b2b1237SJason Wang 
2251b2b1237SJason Wang     return key->gfn | ((key->sid) << VTD_IOTLB_SID_SHIFT) |
2261b2b1237SJason Wang            (key->level) << VTD_IOTLB_LVL_SHIFT |
2271b2b1237SJason Wang            (key->pasid) << VTD_IOTLB_PASID_SHIFT;
228b5a280c0SLe Tan }
229b5a280c0SLe Tan 
230da8d439cSJason Wang static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2)
231da8d439cSJason Wang {
232da8d439cSJason Wang     const struct vtd_as_key *key1 = v1;
233da8d439cSJason Wang     const struct vtd_as_key *key2 = v2;
234da8d439cSJason Wang 
2351b2b1237SJason Wang     return (key1->bus == key2->bus) && (key1->devfn == key2->devfn) &&
2361b2b1237SJason Wang            (key1->pasid == key2->pasid);
237da8d439cSJason Wang }
238da8d439cSJason Wang 
239da8d439cSJason Wang /*
240da8d439cSJason Wang  * Note that we use pointer to PCIBus as the key, so hashing/shifting
241da8d439cSJason Wang  * based on the pointer value is intended. Note that we deal with
242da8d439cSJason Wang  * collisions through vtd_as_equal().
243da8d439cSJason Wang  */
244da8d439cSJason Wang static guint vtd_as_hash(gconstpointer v)
245da8d439cSJason Wang {
246da8d439cSJason Wang     const struct vtd_as_key *key = v;
247da8d439cSJason Wang     guint value = (guint)(uintptr_t)key->bus;
248da8d439cSJason Wang 
249da8d439cSJason Wang     return (guint)(value << 8 | key->devfn);
250da8d439cSJason Wang }
251da8d439cSJason Wang 
252b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
253b5a280c0SLe Tan                                           gpointer user_data)
254b5a280c0SLe Tan {
255b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
256b5a280c0SLe Tan     uint16_t domain_id = *(uint16_t *)user_data;
257b5a280c0SLe Tan     return entry->domain_id == domain_id;
258b5a280c0SLe Tan }
259b5a280c0SLe Tan 
260d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */
261d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level)
262d66b969bSJason Wang {
2637e58326aSPeter Xu     assert(level != 0);
264d66b969bSJason Wang     return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
265d66b969bSJason Wang }
266d66b969bSJason Wang 
267d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
268d66b969bSJason Wang {
269d66b969bSJason Wang     return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
270d66b969bSJason Wang }
271d66b969bSJason Wang 
272b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
273b5a280c0SLe Tan                                         gpointer user_data)
274b5a280c0SLe Tan {
275b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
276b5a280c0SLe Tan     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
277d66b969bSJason Wang     uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
278d66b969bSJason Wang     uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
279b5a280c0SLe Tan     return (entry->domain_id == info->domain_id) &&
280d66b969bSJason Wang             (((entry->gfn & info->mask) == gfn) ||
281d66b969bSJason Wang              (entry->gfn == gfn_tlb));
282b5a280c0SLe Tan }
283b5a280c0SLe Tan 
284d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of
2851d9efa73SPeter Xu  * IntelIOMMUState to 1.  Must be called with IOMMU lock held.
286d92fa2dcSLe Tan  */
2871d9efa73SPeter Xu static void vtd_reset_context_cache_locked(IntelIOMMUState *s)
288d92fa2dcSLe Tan {
289d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
290da8d439cSJason Wang     GHashTableIter as_it;
291d92fa2dcSLe Tan 
2927feb51b7SPeter Xu     trace_vtd_context_cache_reset();
2937feb51b7SPeter Xu 
294da8d439cSJason Wang     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
2957df953bdSKnut Omang 
296da8d439cSJason Wang     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
297d92fa2dcSLe Tan         vtd_as->context_cache_entry.context_cache_gen = 0;
298d92fa2dcSLe Tan     }
299d92fa2dcSLe Tan     s->context_cache_gen = 1;
300d92fa2dcSLe Tan }
301d92fa2dcSLe Tan 
3021d9efa73SPeter Xu /* Must be called with IOMMU lock held. */
3031d9efa73SPeter Xu static void vtd_reset_iotlb_locked(IntelIOMMUState *s)
304b5a280c0SLe Tan {
305b5a280c0SLe Tan     assert(s->iotlb);
306b5a280c0SLe Tan     g_hash_table_remove_all(s->iotlb);
307b5a280c0SLe Tan }
308b5a280c0SLe Tan 
3091d9efa73SPeter Xu static void vtd_reset_iotlb(IntelIOMMUState *s)
3101d9efa73SPeter Xu {
3111d9efa73SPeter Xu     vtd_iommu_lock(s);
3121d9efa73SPeter Xu     vtd_reset_iotlb_locked(s);
3131d9efa73SPeter Xu     vtd_iommu_unlock(s);
3141d9efa73SPeter Xu }
3151d9efa73SPeter Xu 
31606aba4caSPeter Xu static void vtd_reset_caches(IntelIOMMUState *s)
31706aba4caSPeter Xu {
31806aba4caSPeter Xu     vtd_iommu_lock(s);
31906aba4caSPeter Xu     vtd_reset_iotlb_locked(s);
32006aba4caSPeter Xu     vtd_reset_context_cache_locked(s);
32106aba4caSPeter Xu     vtd_iommu_unlock(s);
32206aba4caSPeter Xu }
32306aba4caSPeter Xu 
324d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
325d66b969bSJason Wang {
326d66b969bSJason Wang     return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
327d66b969bSJason Wang }
328d66b969bSJason Wang 
3291d9efa73SPeter Xu /* Must be called with IOMMU lock held */
330b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
3311b2b1237SJason Wang                                        uint32_t pasid, hwaddr addr)
332b5a280c0SLe Tan {
3331b2b1237SJason Wang     struct vtd_iotlb_key key;
334d66b969bSJason Wang     VTDIOTLBEntry *entry;
335d66b969bSJason Wang     int level;
336b5a280c0SLe Tan 
337d66b969bSJason Wang     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
3381b2b1237SJason Wang         key.gfn = vtd_get_iotlb_gfn(addr, level);
3391b2b1237SJason Wang         key.level = level;
3401b2b1237SJason Wang         key.sid = source_id;
3411b2b1237SJason Wang         key.pasid = pasid;
342d66b969bSJason Wang         entry = g_hash_table_lookup(s->iotlb, &key);
343d66b969bSJason Wang         if (entry) {
344d66b969bSJason Wang             goto out;
345d66b969bSJason Wang         }
346d66b969bSJason Wang     }
347b5a280c0SLe Tan 
348d66b969bSJason Wang out:
349d66b969bSJason Wang     return entry;
350b5a280c0SLe Tan }
351b5a280c0SLe Tan 
3521d9efa73SPeter Xu /* Must be with IOMMU lock held */
353b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
354b5a280c0SLe Tan                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
3551b2b1237SJason Wang                              uint8_t access_flags, uint32_t level,
3561b2b1237SJason Wang                              uint32_t pasid)
357b5a280c0SLe Tan {
358b5a280c0SLe Tan     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
3591b2b1237SJason Wang     struct vtd_iotlb_key *key = g_malloc(sizeof(*key));
360d66b969bSJason Wang     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
361b5a280c0SLe Tan 
3626c441e1dSPeter Xu     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
363b5a280c0SLe Tan     if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
3646c441e1dSPeter Xu         trace_vtd_iotlb_reset("iotlb exceeds size limit");
3651d9efa73SPeter Xu         vtd_reset_iotlb_locked(s);
366b5a280c0SLe Tan     }
367b5a280c0SLe Tan 
368b5a280c0SLe Tan     entry->gfn = gfn;
369b5a280c0SLe Tan     entry->domain_id = domain_id;
370b5a280c0SLe Tan     entry->slpte = slpte;
37107f7b733SPeter Xu     entry->access_flags = access_flags;
372d66b969bSJason Wang     entry->mask = vtd_slpt_level_page_mask(level);
3731b2b1237SJason Wang     entry->pasid = pasid;
3741b2b1237SJason Wang 
3751b2b1237SJason Wang     key->gfn = gfn;
3761b2b1237SJason Wang     key->sid = source_id;
3771b2b1237SJason Wang     key->level = level;
3781b2b1237SJason Wang     key->pasid = pasid;
3791b2b1237SJason Wang 
380b5a280c0SLe Tan     g_hash_table_replace(s->iotlb, key, entry);
381b5a280c0SLe Tan }
382b5a280c0SLe Tan 
3831da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an
3841da12ec4SLe Tan  * interrupt via MSI.
3851da12ec4SLe Tan  */
3861da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
3871da12ec4SLe Tan                                    hwaddr mesg_data_reg)
3881da12ec4SLe Tan {
38932946019SRadim Krčmář     MSIMessage msi;
3901da12ec4SLe Tan 
3911da12ec4SLe Tan     assert(mesg_data_reg < DMAR_REG_SIZE);
3921da12ec4SLe Tan     assert(mesg_addr_reg < DMAR_REG_SIZE);
3931da12ec4SLe Tan 
39432946019SRadim Krčmář     msi.address = vtd_get_long_raw(s, mesg_addr_reg);
39532946019SRadim Krčmář     msi.data = vtd_get_long_raw(s, mesg_data_reg);
3961da12ec4SLe Tan 
3977feb51b7SPeter Xu     trace_vtd_irq_generate(msi.address, msi.data);
3987feb51b7SPeter Xu 
399eaaaf8abSPaolo Bonzini     apic_get_class(NULL)->send_msi(&msi);
4001da12ec4SLe Tan }
4011da12ec4SLe Tan 
4021da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met.
4031da12ec4SLe Tan  * Notice that the value of FSTS_REG being passed to it should be the one
4041da12ec4SLe Tan  * before any update.
4051da12ec4SLe Tan  */
4061da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
4071da12ec4SLe Tan {
4081da12ec4SLe Tan     if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
4091da12ec4SLe Tan         pre_fsts & VTD_FSTS_IQE) {
4101376211fSPeter Xu         error_report_once("There are previous interrupt conditions "
4117feb51b7SPeter Xu                           "to be serviced by software, fault event "
4121376211fSPeter Xu                           "is not generated");
4131da12ec4SLe Tan         return;
4141da12ec4SLe Tan     }
4151da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
4161da12ec4SLe Tan     if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
4171376211fSPeter Xu         error_report_once("Interrupt Mask set, irq is not generated");
4181da12ec4SLe Tan     } else {
4191da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
4201da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
4211da12ec4SLe Tan     }
4221da12ec4SLe Tan }
4231da12ec4SLe Tan 
4241da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by
4251da12ec4SLe Tan  * @index is Set.
4261da12ec4SLe Tan  */
4271da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
4281da12ec4SLe Tan {
4291da12ec4SLe Tan     /* Each reg is 128-bit */
4301da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4311da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
4321da12ec4SLe Tan 
4331da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4341da12ec4SLe Tan 
4351da12ec4SLe Tan     return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
4361da12ec4SLe Tan }
4371da12ec4SLe Tan 
4381da12ec4SLe Tan /* Update the PPF field of Fault Status Register.
4391da12ec4SLe Tan  * Should be called whenever change the F field of any fault recording
4401da12ec4SLe Tan  * registers.
4411da12ec4SLe Tan  */
4421da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s)
4431da12ec4SLe Tan {
4441da12ec4SLe Tan     uint32_t i;
4451da12ec4SLe Tan     uint32_t ppf_mask = 0;
4461da12ec4SLe Tan 
4471da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
4481da12ec4SLe Tan         if (vtd_is_frcd_set(s, i)) {
4491da12ec4SLe Tan             ppf_mask = VTD_FSTS_PPF;
4501da12ec4SLe Tan             break;
4511da12ec4SLe Tan         }
4521da12ec4SLe Tan     }
4531da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
4547feb51b7SPeter Xu     trace_vtd_fsts_ppf(!!ppf_mask);
4551da12ec4SLe Tan }
4561da12ec4SLe Tan 
4571da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
4581da12ec4SLe Tan {
4591da12ec4SLe Tan     /* Each reg is 128-bit */
4601da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4611da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
4621da12ec4SLe Tan 
4631da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4641da12ec4SLe Tan 
4651da12ec4SLe Tan     vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
4661da12ec4SLe Tan     vtd_update_fsts_ppf(s);
4671da12ec4SLe Tan }
4681da12ec4SLe Tan 
4691da12ec4SLe Tan /* Must not update F field now, should be done later */
4701da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
4711da12ec4SLe Tan                             uint16_t source_id, hwaddr addr,
4721b2b1237SJason Wang                             VTDFaultReason fault, bool is_write,
4731b2b1237SJason Wang                             bool is_pasid, uint32_t pasid)
4741da12ec4SLe Tan {
4751da12ec4SLe Tan     uint64_t hi = 0, lo;
4761da12ec4SLe Tan     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
4771da12ec4SLe Tan 
4781da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
4791da12ec4SLe Tan 
4801da12ec4SLe Tan     lo = VTD_FRCD_FI(addr);
4811b2b1237SJason Wang     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault) |
4821b2b1237SJason Wang          VTD_FRCD_PV(pasid) | VTD_FRCD_PP(is_pasid);
4831da12ec4SLe Tan     if (!is_write) {
4841da12ec4SLe Tan         hi |= VTD_FRCD_T;
4851da12ec4SLe Tan     }
4861da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr, lo);
4871da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
4887feb51b7SPeter Xu 
4897feb51b7SPeter Xu     trace_vtd_frr_new(index, hi, lo);
4901da12ec4SLe Tan }
4911da12ec4SLe Tan 
4921da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */
4931da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
4941da12ec4SLe Tan {
4951da12ec4SLe Tan     uint32_t i;
4961da12ec4SLe Tan     uint64_t frcd_reg;
4971da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
4981da12ec4SLe Tan 
4991da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
5001da12ec4SLe Tan         frcd_reg = vtd_get_quad_raw(s, addr);
5011da12ec4SLe Tan         if ((frcd_reg & VTD_FRCD_F) &&
5021da12ec4SLe Tan             ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
5031da12ec4SLe Tan             return true;
5041da12ec4SLe Tan         }
5051da12ec4SLe Tan         addr += 16; /* 128-bit for each */
5061da12ec4SLe Tan     }
5071da12ec4SLe Tan     return false;
5081da12ec4SLe Tan }
5091da12ec4SLe Tan 
5101da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */
5111da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
5121da12ec4SLe Tan                                   hwaddr addr, VTDFaultReason fault,
5131b2b1237SJason Wang                                   bool is_write, bool is_pasid,
5141b2b1237SJason Wang                                   uint32_t pasid)
5151da12ec4SLe Tan {
5161da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
5171da12ec4SLe Tan 
5181da12ec4SLe Tan     assert(fault < VTD_FR_MAX);
5191da12ec4SLe Tan 
5207feb51b7SPeter Xu     trace_vtd_dmar_fault(source_id, fault, addr, is_write);
5217feb51b7SPeter Xu 
5221da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PFO) {
5231376211fSPeter Xu         error_report_once("New fault is not recorded due to "
5241376211fSPeter Xu                           "Primary Fault Overflow");
5251da12ec4SLe Tan         return;
5261da12ec4SLe Tan     }
5277feb51b7SPeter Xu 
5281da12ec4SLe Tan     if (vtd_try_collapse_fault(s, source_id)) {
5291376211fSPeter Xu         error_report_once("New fault is not recorded due to "
5301376211fSPeter Xu                           "compression of faults");
5311da12ec4SLe Tan         return;
5321da12ec4SLe Tan     }
5337feb51b7SPeter Xu 
5341da12ec4SLe Tan     if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
5351376211fSPeter Xu         error_report_once("Next Fault Recording Reg is used, "
5361376211fSPeter Xu                           "new fault is not recorded, set PFO field");
5371da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
5381da12ec4SLe Tan         return;
5391da12ec4SLe Tan     }
5401da12ec4SLe Tan 
5411b2b1237SJason Wang     vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault,
5421b2b1237SJason Wang                     is_write, is_pasid, pasid);
5431da12ec4SLe Tan 
5441da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PPF) {
5451376211fSPeter Xu         error_report_once("There are pending faults already, "
5461376211fSPeter Xu                           "fault event is not generated");
5471da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
5481da12ec4SLe Tan         s->next_frcd_reg++;
5491da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
5501da12ec4SLe Tan             s->next_frcd_reg = 0;
5511da12ec4SLe Tan         }
5521da12ec4SLe Tan     } else {
5531da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
5541da12ec4SLe Tan                                 VTD_FSTS_FRI(s->next_frcd_reg));
5551da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
5561da12ec4SLe Tan         s->next_frcd_reg++;
5571da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
5581da12ec4SLe Tan             s->next_frcd_reg = 0;
5591da12ec4SLe Tan         }
5601da12ec4SLe Tan         /* This case actually cause the PPF to be Set.
5611da12ec4SLe Tan          * So generate fault event (interrupt).
5621da12ec4SLe Tan          */
5631da12ec4SLe Tan          vtd_generate_fault_event(s, fsts_reg);
5641da12ec4SLe Tan     }
5651da12ec4SLe Tan }
5661da12ec4SLe Tan 
567ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error
568ed7b8fbcSLe Tan  * conditions.
569ed7b8fbcSLe Tan  */
570ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
571ed7b8fbcSLe Tan {
572ed7b8fbcSLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
573ed7b8fbcSLe Tan 
574ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
575ed7b8fbcSLe Tan     vtd_generate_fault_event(s, fsts_reg);
576ed7b8fbcSLe Tan }
577ed7b8fbcSLe Tan 
578ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */
579ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s)
580ed7b8fbcSLe Tan {
581ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
582bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("One pending, skip current");
583ed7b8fbcSLe Tan         return;
584ed7b8fbcSLe Tan     }
585ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
586ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
587ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
588bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
589bc535e59SPeter Xu                                     "new event not generated");
590ed7b8fbcSLe Tan         return;
591ed7b8fbcSLe Tan     } else {
592ed7b8fbcSLe Tan         /* Generate the interrupt event */
593bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("Generating complete event");
594ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
595ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
596ed7b8fbcSLe Tan     }
597ed7b8fbcSLe Tan }
598ed7b8fbcSLe Tan 
599fb43cf73SLiu, Yi L static inline bool vtd_root_entry_present(IntelIOMMUState *s,
600fb43cf73SLiu, Yi L                                           VTDRootEntry *re,
601fb43cf73SLiu, Yi L                                           uint8_t devfn)
6021da12ec4SLe Tan {
603fb43cf73SLiu, Yi L     if (s->root_scalable && devfn > UINT8_MAX / 2) {
604fb43cf73SLiu, Yi L         return re->hi & VTD_ROOT_ENTRY_P;
605fb43cf73SLiu, Yi L     }
606fb43cf73SLiu, Yi L 
607fb43cf73SLiu, Yi L     return re->lo & VTD_ROOT_ENTRY_P;
6081da12ec4SLe Tan }
6091da12ec4SLe Tan 
6101da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
6111da12ec4SLe Tan                               VTDRootEntry *re)
6121da12ec4SLe Tan {
6131da12ec4SLe Tan     dma_addr_t addr;
6141da12ec4SLe Tan 
6151da12ec4SLe Tan     addr = s->root + index * sizeof(*re);
616ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
617ba06fe8aSPhilippe Mathieu-Daudé                         re, sizeof(*re), MEMTXATTRS_UNSPECIFIED)) {
618fb43cf73SLiu, Yi L         re->lo = 0;
6191da12ec4SLe Tan         return -VTD_FR_ROOT_TABLE_INV;
6201da12ec4SLe Tan     }
621fb43cf73SLiu, Yi L     re->lo = le64_to_cpu(re->lo);
622fb43cf73SLiu, Yi L     re->hi = le64_to_cpu(re->hi);
6231da12ec4SLe Tan     return 0;
6241da12ec4SLe Tan }
6251da12ec4SLe Tan 
6268f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context)
6271da12ec4SLe Tan {
6281da12ec4SLe Tan     return context->lo & VTD_CONTEXT_ENTRY_P;
6291da12ec4SLe Tan }
6301da12ec4SLe Tan 
631fb43cf73SLiu, Yi L static int vtd_get_context_entry_from_root(IntelIOMMUState *s,
632fb43cf73SLiu, Yi L                                            VTDRootEntry *re,
633fb43cf73SLiu, Yi L                                            uint8_t index,
6341da12ec4SLe Tan                                            VTDContextEntry *ce)
6351da12ec4SLe Tan {
636fb43cf73SLiu, Yi L     dma_addr_t addr, ce_size;
6371da12ec4SLe Tan 
6386c441e1dSPeter Xu     /* we have checked that root entry is present */
639fb43cf73SLiu, Yi L     ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE :
640fb43cf73SLiu, Yi L               VTD_CTX_ENTRY_LEGACY_SIZE;
641fb43cf73SLiu, Yi L 
642fb43cf73SLiu, Yi L     if (s->root_scalable && index > UINT8_MAX / 2) {
643fb43cf73SLiu, Yi L         index = index & (~VTD_DEVFN_CHECK_MASK);
644fb43cf73SLiu, Yi L         addr = re->hi & VTD_ROOT_ENTRY_CTP;
645fb43cf73SLiu, Yi L     } else {
646fb43cf73SLiu, Yi L         addr = re->lo & VTD_ROOT_ENTRY_CTP;
647fb43cf73SLiu, Yi L     }
648fb43cf73SLiu, Yi L 
649fb43cf73SLiu, Yi L     addr = addr + index * ce_size;
650ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
651ba06fe8aSPhilippe Mathieu-Daudé                         ce, ce_size, MEMTXATTRS_UNSPECIFIED)) {
6521da12ec4SLe Tan         return -VTD_FR_CONTEXT_TABLE_INV;
6531da12ec4SLe Tan     }
654fb43cf73SLiu, Yi L 
6551da12ec4SLe Tan     ce->lo = le64_to_cpu(ce->lo);
6561da12ec4SLe Tan     ce->hi = le64_to_cpu(ce->hi);
657fb43cf73SLiu, Yi L     if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) {
658fb43cf73SLiu, Yi L         ce->val[2] = le64_to_cpu(ce->val[2]);
659fb43cf73SLiu, Yi L         ce->val[3] = le64_to_cpu(ce->val[3]);
660fb43cf73SLiu, Yi L     }
6611da12ec4SLe Tan     return 0;
6621da12ec4SLe Tan }
6631da12ec4SLe Tan 
6648f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
6651da12ec4SLe Tan {
6661da12ec4SLe Tan     return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
6671da12ec4SLe Tan }
6681da12ec4SLe Tan 
66937f51384SPrasad Singamsetty static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw)
6701da12ec4SLe Tan {
67137f51384SPrasad Singamsetty     return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw);
6721da12ec4SLe Tan }
6731da12ec4SLe Tan 
6741da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */
6751da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
6761da12ec4SLe Tan {
6771da12ec4SLe Tan     return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
6781da12ec4SLe Tan }
6791da12ec4SLe Tan 
6801da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */
6811da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
6821da12ec4SLe Tan {
6831da12ec4SLe Tan     uint64_t slpte;
6841da12ec4SLe Tan 
6851da12ec4SLe Tan     assert(index < VTD_SL_PT_ENTRY_NR);
6861da12ec4SLe Tan 
6871da12ec4SLe Tan     if (dma_memory_read(&address_space_memory,
688ba06fe8aSPhilippe Mathieu-Daudé                         base_addr + index * sizeof(slpte),
689ba06fe8aSPhilippe Mathieu-Daudé                         &slpte, sizeof(slpte), MEMTXATTRS_UNSPECIFIED)) {
6901da12ec4SLe Tan         slpte = (uint64_t)-1;
6911da12ec4SLe Tan         return slpte;
6921da12ec4SLe Tan     }
6931da12ec4SLe Tan     slpte = le64_to_cpu(slpte);
6941da12ec4SLe Tan     return slpte;
6951da12ec4SLe Tan }
6961da12ec4SLe Tan 
6976e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset
6986e905564SPeter Xu  * of current level.
6991da12ec4SLe Tan  */
7006e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
7011da12ec4SLe Tan {
7026e905564SPeter Xu     return (iova >> vtd_slpt_level_shift(level)) &
7031da12ec4SLe Tan             ((1ULL << VTD_SL_LEVEL_BITS) - 1);
7041da12ec4SLe Tan }
7051da12ec4SLe Tan 
7061da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */
7071da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
7081da12ec4SLe Tan {
7091da12ec4SLe Tan     return VTD_CAP_SAGAW_MASK & s->cap &
7101da12ec4SLe Tan            (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
7111da12ec4SLe Tan }
7121da12ec4SLe Tan 
713fb43cf73SLiu, Yi L /* Return true if check passed, otherwise false */
714fb43cf73SLiu, Yi L static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
715fb43cf73SLiu, Yi L                                      VTDPASIDEntry *pe)
716fb43cf73SLiu, Yi L {
717fb43cf73SLiu, Yi L     switch (VTD_PE_GET_TYPE(pe)) {
718fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_FLT:
719fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_SLT:
720fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_NESTED:
721fb43cf73SLiu, Yi L         break;
722fb43cf73SLiu, Yi L     case VTD_SM_PASID_ENTRY_PT:
723fb43cf73SLiu, Yi L         if (!x86_iommu->pt_supported) {
724fb43cf73SLiu, Yi L             return false;
725fb43cf73SLiu, Yi L         }
726fb43cf73SLiu, Yi L         break;
727fb43cf73SLiu, Yi L     default:
72837557b09SCai Huoqing         /* Unknown type */
729fb43cf73SLiu, Yi L         return false;
730fb43cf73SLiu, Yi L     }
731fb43cf73SLiu, Yi L     return true;
732fb43cf73SLiu, Yi L }
733fb43cf73SLiu, Yi L 
73456fc1e6aSLiu Yi L static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
73556fc1e6aSLiu Yi L {
73656fc1e6aSLiu Yi L     return pdire->val & 1;
73756fc1e6aSLiu Yi L }
73856fc1e6aSLiu Yi L 
73956fc1e6aSLiu Yi L /**
74056fc1e6aSLiu Yi L  * Caller of this function should check present bit if wants
74137557b09SCai Huoqing  * to use pdir entry for further usage except for fpd bit check.
74256fc1e6aSLiu Yi L  */
74356fc1e6aSLiu Yi L static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
744fb43cf73SLiu, Yi L                                          uint32_t pasid,
745fb43cf73SLiu, Yi L                                          VTDPASIDDirEntry *pdire)
746fb43cf73SLiu, Yi L {
747fb43cf73SLiu, Yi L     uint32_t index;
748fb43cf73SLiu, Yi L     dma_addr_t addr, entry_size;
749fb43cf73SLiu, Yi L 
750fb43cf73SLiu, Yi L     index = VTD_PASID_DIR_INDEX(pasid);
751fb43cf73SLiu, Yi L     entry_size = VTD_PASID_DIR_ENTRY_SIZE;
752fb43cf73SLiu, Yi L     addr = pasid_dir_base + index * entry_size;
753ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
754ba06fe8aSPhilippe Mathieu-Daudé                         pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
755fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
756fb43cf73SLiu, Yi L     }
757fb43cf73SLiu, Yi L 
758fb43cf73SLiu, Yi L     return 0;
759fb43cf73SLiu, Yi L }
760fb43cf73SLiu, Yi L 
76156fc1e6aSLiu Yi L static inline bool vtd_pe_present(VTDPASIDEntry *pe)
76256fc1e6aSLiu Yi L {
76356fc1e6aSLiu Yi L     return pe->val[0] & VTD_PASID_ENTRY_P;
76456fc1e6aSLiu Yi L }
76556fc1e6aSLiu Yi L 
76656fc1e6aSLiu Yi L static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
767fb43cf73SLiu, Yi L                                           uint32_t pasid,
76856fc1e6aSLiu Yi L                                           dma_addr_t addr,
769fb43cf73SLiu, Yi L                                           VTDPASIDEntry *pe)
770fb43cf73SLiu, Yi L {
771fb43cf73SLiu, Yi L     uint32_t index;
77256fc1e6aSLiu Yi L     dma_addr_t entry_size;
773fb43cf73SLiu, Yi L     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
774fb43cf73SLiu, Yi L 
775fb43cf73SLiu, Yi L     index = VTD_PASID_TABLE_INDEX(pasid);
776fb43cf73SLiu, Yi L     entry_size = VTD_PASID_ENTRY_SIZE;
777fb43cf73SLiu, Yi L     addr = addr + index * entry_size;
778ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
779ba06fe8aSPhilippe Mathieu-Daudé                         pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
780fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
781fb43cf73SLiu, Yi L     }
782fb43cf73SLiu, Yi L 
783fb43cf73SLiu, Yi L     /* Do translation type check */
784fb43cf73SLiu, Yi L     if (!vtd_pe_type_check(x86_iommu, pe)) {
785fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
786fb43cf73SLiu, Yi L     }
787fb43cf73SLiu, Yi L 
788fb43cf73SLiu, Yi L     if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
789fb43cf73SLiu, Yi L         return -VTD_FR_PASID_TABLE_INV;
790fb43cf73SLiu, Yi L     }
791fb43cf73SLiu, Yi L 
792fb43cf73SLiu, Yi L     return 0;
793fb43cf73SLiu, Yi L }
794fb43cf73SLiu, Yi L 
79556fc1e6aSLiu Yi L /**
79656fc1e6aSLiu Yi L  * Caller of this function should check present bit if wants
79737557b09SCai Huoqing  * to use pasid entry for further usage except for fpd bit check.
79856fc1e6aSLiu Yi L  */
79956fc1e6aSLiu Yi L static int vtd_get_pe_from_pdire(IntelIOMMUState *s,
80056fc1e6aSLiu Yi L                                  uint32_t pasid,
80156fc1e6aSLiu Yi L                                  VTDPASIDDirEntry *pdire,
80256fc1e6aSLiu Yi L                                  VTDPASIDEntry *pe)
80356fc1e6aSLiu Yi L {
80456fc1e6aSLiu Yi L     dma_addr_t addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK;
80556fc1e6aSLiu Yi L 
80656fc1e6aSLiu Yi L     return vtd_get_pe_in_pasid_leaf_table(s, pasid, addr, pe);
80756fc1e6aSLiu Yi L }
80856fc1e6aSLiu Yi L 
80956fc1e6aSLiu Yi L /**
81056fc1e6aSLiu Yi L  * This function gets a pasid entry from a specified pasid
81156fc1e6aSLiu Yi L  * table (includes dir and leaf table) with a specified pasid.
81256fc1e6aSLiu Yi L  * Sanity check should be done to ensure return a present
81356fc1e6aSLiu Yi L  * pasid entry to caller.
81456fc1e6aSLiu Yi L  */
81556fc1e6aSLiu Yi L static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
816fb43cf73SLiu, Yi L                                        dma_addr_t pasid_dir_base,
817fb43cf73SLiu, Yi L                                        uint32_t pasid,
818fb43cf73SLiu, Yi L                                        VTDPASIDEntry *pe)
819fb43cf73SLiu, Yi L {
820fb43cf73SLiu, Yi L     int ret;
821fb43cf73SLiu, Yi L     VTDPASIDDirEntry pdire;
822fb43cf73SLiu, Yi L 
82356fc1e6aSLiu Yi L     ret = vtd_get_pdire_from_pdir_table(pasid_dir_base,
82456fc1e6aSLiu Yi L                                         pasid, &pdire);
825fb43cf73SLiu, Yi L     if (ret) {
826fb43cf73SLiu, Yi L         return ret;
827fb43cf73SLiu, Yi L     }
828fb43cf73SLiu, Yi L 
82956fc1e6aSLiu Yi L     if (!vtd_pdire_present(&pdire)) {
83056fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
83156fc1e6aSLiu Yi L     }
83256fc1e6aSLiu Yi L 
83356fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
834fb43cf73SLiu, Yi L     if (ret) {
835fb43cf73SLiu, Yi L         return ret;
836fb43cf73SLiu, Yi L     }
837fb43cf73SLiu, Yi L 
83856fc1e6aSLiu Yi L     if (!vtd_pe_present(pe)) {
83956fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
84056fc1e6aSLiu Yi L     }
84156fc1e6aSLiu Yi L 
84256fc1e6aSLiu Yi L     return 0;
843fb43cf73SLiu, Yi L }
844fb43cf73SLiu, Yi L 
845fb43cf73SLiu, Yi L static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
846fb43cf73SLiu, Yi L                                       VTDContextEntry *ce,
8471b2b1237SJason Wang                                       VTDPASIDEntry *pe,
8481b2b1237SJason Wang                                       uint32_t pasid)
849fb43cf73SLiu, Yi L {
850fb43cf73SLiu, Yi L     dma_addr_t pasid_dir_base;
851fb43cf73SLiu, Yi L     int ret = 0;
852fb43cf73SLiu, Yi L 
8531b2b1237SJason Wang     if (pasid == PCI_NO_PASID) {
854fb43cf73SLiu, Yi L         pasid = VTD_CE_GET_RID2PASID(ce);
8551b2b1237SJason Wang     }
856fb43cf73SLiu, Yi L     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
85756fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
858fb43cf73SLiu, Yi L 
859fb43cf73SLiu, Yi L     return ret;
860fb43cf73SLiu, Yi L }
861fb43cf73SLiu, Yi L 
862fb43cf73SLiu, Yi L static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
863fb43cf73SLiu, Yi L                                 VTDContextEntry *ce,
8641b2b1237SJason Wang                                 bool *pe_fpd_set,
8651b2b1237SJason Wang                                 uint32_t pasid)
866fb43cf73SLiu, Yi L {
867fb43cf73SLiu, Yi L     int ret;
868fb43cf73SLiu, Yi L     dma_addr_t pasid_dir_base;
869fb43cf73SLiu, Yi L     VTDPASIDDirEntry pdire;
870fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
871fb43cf73SLiu, Yi L 
8721b2b1237SJason Wang     if (pasid == PCI_NO_PASID) {
873fb43cf73SLiu, Yi L         pasid = VTD_CE_GET_RID2PASID(ce);
8741b2b1237SJason Wang     }
875fb43cf73SLiu, Yi L     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
876fb43cf73SLiu, Yi L 
87756fc1e6aSLiu Yi L     /*
87856fc1e6aSLiu Yi L      * No present bit check since fpd is meaningful even
87956fc1e6aSLiu Yi L      * if the present bit is clear.
88056fc1e6aSLiu Yi L      */
88156fc1e6aSLiu Yi L     ret = vtd_get_pdire_from_pdir_table(pasid_dir_base, pasid, &pdire);
882fb43cf73SLiu, Yi L     if (ret) {
883fb43cf73SLiu, Yi L         return ret;
884fb43cf73SLiu, Yi L     }
885fb43cf73SLiu, Yi L 
886fb43cf73SLiu, Yi L     if (pdire.val & VTD_PASID_DIR_FPD) {
887fb43cf73SLiu, Yi L         *pe_fpd_set = true;
888fb43cf73SLiu, Yi L         return 0;
889fb43cf73SLiu, Yi L     }
890fb43cf73SLiu, Yi L 
89156fc1e6aSLiu Yi L     if (!vtd_pdire_present(&pdire)) {
89256fc1e6aSLiu Yi L         return -VTD_FR_PASID_TABLE_INV;
89356fc1e6aSLiu Yi L     }
89456fc1e6aSLiu Yi L 
89556fc1e6aSLiu Yi L     /*
89656fc1e6aSLiu Yi L      * No present bit check since fpd is meaningful even
89756fc1e6aSLiu Yi L      * if the present bit is clear.
89856fc1e6aSLiu Yi L      */
89956fc1e6aSLiu Yi L     ret = vtd_get_pe_from_pdire(s, pasid, &pdire, &pe);
900fb43cf73SLiu, Yi L     if (ret) {
901fb43cf73SLiu, Yi L         return ret;
902fb43cf73SLiu, Yi L     }
903fb43cf73SLiu, Yi L 
904fb43cf73SLiu, Yi L     if (pe.val[0] & VTD_PASID_ENTRY_FPD) {
905fb43cf73SLiu, Yi L         *pe_fpd_set = true;
906fb43cf73SLiu, Yi L     }
907fb43cf73SLiu, Yi L 
908fb43cf73SLiu, Yi L     return 0;
909fb43cf73SLiu, Yi L }
910fb43cf73SLiu, Yi L 
9111da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level
9121da12ec4SLe Tan  * page-table walk from the Address Width field of context-entry.
9131da12ec4SLe Tan  */
9148f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
9151da12ec4SLe Tan {
9161da12ec4SLe Tan     return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
9171da12ec4SLe Tan }
9181da12ec4SLe Tan 
919fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
9201b2b1237SJason Wang                                    VTDContextEntry *ce,
9211b2b1237SJason Wang                                    uint32_t pasid)
922fb43cf73SLiu, Yi L {
923fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
924fb43cf73SLiu, Yi L 
925fb43cf73SLiu, Yi L     if (s->root_scalable) {
9261b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
927fb43cf73SLiu, Yi L         return VTD_PE_GET_LEVEL(&pe);
928fb43cf73SLiu, Yi L     }
929fb43cf73SLiu, Yi L 
930fb43cf73SLiu, Yi L     return vtd_ce_get_level(ce);
931fb43cf73SLiu, Yi L }
932fb43cf73SLiu, Yi L 
9338f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
9341da12ec4SLe Tan {
9351da12ec4SLe Tan     return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
9361da12ec4SLe Tan }
9371da12ec4SLe Tan 
938fb43cf73SLiu, Yi L static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
9391b2b1237SJason Wang                                   VTDContextEntry *ce,
9401b2b1237SJason Wang                                   uint32_t pasid)
941fb43cf73SLiu, Yi L {
942fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
943fb43cf73SLiu, Yi L 
944fb43cf73SLiu, Yi L     if (s->root_scalable) {
9451b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
946fb43cf73SLiu, Yi L         return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
947fb43cf73SLiu, Yi L     }
948fb43cf73SLiu, Yi L 
949fb43cf73SLiu, Yi L     return vtd_ce_get_agaw(ce);
950fb43cf73SLiu, Yi L }
951fb43cf73SLiu, Yi L 
952127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
953127ff5c3SPeter Xu {
954127ff5c3SPeter Xu     return ce->lo & VTD_CONTEXT_ENTRY_TT;
955127ff5c3SPeter Xu }
956127ff5c3SPeter Xu 
957fb43cf73SLiu, Yi L /* Only for Legacy Mode. Return true if check passed, otherwise false */
958f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
959f80c9874SPeter Xu                                      VTDContextEntry *ce)
960f80c9874SPeter Xu {
961f80c9874SPeter Xu     switch (vtd_ce_get_type(ce)) {
962f80c9874SPeter Xu     case VTD_CONTEXT_TT_MULTI_LEVEL:
963f80c9874SPeter Xu         /* Always supported */
964f80c9874SPeter Xu         break;
965f80c9874SPeter Xu     case VTD_CONTEXT_TT_DEV_IOTLB:
966f80c9874SPeter Xu         if (!x86_iommu->dt_supported) {
967095955b2SPeter Xu             error_report_once("%s: DT specified but not supported", __func__);
968f80c9874SPeter Xu             return false;
969f80c9874SPeter Xu         }
970f80c9874SPeter Xu         break;
971dbaabb25SPeter Xu     case VTD_CONTEXT_TT_PASS_THROUGH:
972dbaabb25SPeter Xu         if (!x86_iommu->pt_supported) {
973095955b2SPeter Xu             error_report_once("%s: PT specified but not supported", __func__);
974dbaabb25SPeter Xu             return false;
975dbaabb25SPeter Xu         }
976dbaabb25SPeter Xu         break;
977f80c9874SPeter Xu     default:
978fb43cf73SLiu, Yi L         /* Unknown type */
979095955b2SPeter Xu         error_report_once("%s: unknown ce type: %"PRIu32, __func__,
980095955b2SPeter Xu                           vtd_ce_get_type(ce));
981f80c9874SPeter Xu         return false;
982f80c9874SPeter Xu     }
983f80c9874SPeter Xu     return true;
984f80c9874SPeter Xu }
985f80c9874SPeter Xu 
986fb43cf73SLiu, Yi L static inline uint64_t vtd_iova_limit(IntelIOMMUState *s,
9871b2b1237SJason Wang                                       VTDContextEntry *ce, uint8_t aw,
9881b2b1237SJason Wang                                       uint32_t pasid)
989f06a696dSPeter Xu {
9901b2b1237SJason Wang     uint32_t ce_agaw = vtd_get_iova_agaw(s, ce, pasid);
99137f51384SPrasad Singamsetty     return 1ULL << MIN(ce_agaw, aw);
992f06a696dSPeter Xu }
993f06a696dSPeter Xu 
994f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */
995fb43cf73SLiu, Yi L static inline bool vtd_iova_range_check(IntelIOMMUState *s,
996fb43cf73SLiu, Yi L                                         uint64_t iova, VTDContextEntry *ce,
9971b2b1237SJason Wang                                         uint8_t aw, uint32_t pasid)
998f06a696dSPeter Xu {
999f06a696dSPeter Xu     /*
1000f06a696dSPeter Xu      * Check if @iova is above 2^X-1, where X is the minimum of MGAW
1001f06a696dSPeter Xu      * in CAP_REG and AW in context-entry.
1002f06a696dSPeter Xu      */
10031b2b1237SJason Wang     return !(iova & ~(vtd_iova_limit(s, ce, aw, pasid) - 1));
1004fb43cf73SLiu, Yi L }
1005fb43cf73SLiu, Yi L 
1006fb43cf73SLiu, Yi L static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
10071b2b1237SJason Wang                                           VTDContextEntry *ce,
10081b2b1237SJason Wang                                           uint32_t pasid)
1009fb43cf73SLiu, Yi L {
1010fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1011fb43cf73SLiu, Yi L 
1012fb43cf73SLiu, Yi L     if (s->root_scalable) {
10131b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
1014fb43cf73SLiu, Yi L         return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR;
1015fb43cf73SLiu, Yi L     }
1016fb43cf73SLiu, Yi L 
1017fb43cf73SLiu, Yi L     return vtd_ce_get_slpt_base(ce);
1018f06a696dSPeter Xu }
1019f06a696dSPeter Xu 
102092e5d85eSPrasad Singamsetty /*
102192e5d85eSPrasad Singamsetty  * Rsvd field masks for spte:
1022ce586f3bSQi, Yadong  *     vtd_spte_rsvd 4k pages
1023ce586f3bSQi, Yadong  *     vtd_spte_rsvd_large large pages
102492e5d85eSPrasad Singamsetty  */
1025ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd[5];
1026ce586f3bSQi, Yadong static uint64_t vtd_spte_rsvd_large[5];
10271da12ec4SLe Tan 
10281da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
10291da12ec4SLe Tan {
1030ce586f3bSQi, Yadong     uint64_t rsvd_mask = vtd_spte_rsvd[level];
1031ce586f3bSQi, Yadong 
1032ce586f3bSQi, Yadong     if ((level == VTD_SL_PD_LEVEL || level == VTD_SL_PDP_LEVEL) &&
1033ce586f3bSQi, Yadong         (slpte & VTD_SL_PT_PAGE_SIZE_MASK)) {
1034ce586f3bSQi, Yadong         /* large page */
1035ce586f3bSQi, Yadong         rsvd_mask = vtd_spte_rsvd_large[level];
10361da12ec4SLe Tan     }
1037ce586f3bSQi, Yadong 
1038ce586f3bSQi, Yadong     return slpte & rsvd_mask;
10391da12ec4SLe Tan }
10401da12ec4SLe Tan 
10416e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level
10421da12ec4SLe Tan  * of the translation, can be used for deciding the size of large page.
10431da12ec4SLe Tan  */
1044fb43cf73SLiu, Yi L static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce,
1045fb43cf73SLiu, Yi L                              uint64_t iova, bool is_write,
10461da12ec4SLe Tan                              uint64_t *slptep, uint32_t *slpte_level,
10471b2b1237SJason Wang                              bool *reads, bool *writes, uint8_t aw_bits,
10481b2b1237SJason Wang                              uint32_t pasid)
10491da12ec4SLe Tan {
10501b2b1237SJason Wang     dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
10511b2b1237SJason Wang     uint32_t level = vtd_get_iova_level(s, ce, pasid);
10521da12ec4SLe Tan     uint32_t offset;
10531da12ec4SLe Tan     uint64_t slpte;
10541da12ec4SLe Tan     uint64_t access_right_check;
1055ea97a1bdSJason Wang     uint64_t xlat, size;
10561da12ec4SLe Tan 
10571b2b1237SJason Wang     if (!vtd_iova_range_check(s, iova, ce, aw_bits, pasid)) {
10581b2b1237SJason Wang         error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ","
10591b2b1237SJason Wang                           "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
10601da12ec4SLe Tan         return -VTD_FR_ADDR_BEYOND_MGAW;
10611da12ec4SLe Tan     }
10621da12ec4SLe Tan 
10631da12ec4SLe Tan     /* FIXME: what is the Atomics request here? */
10641da12ec4SLe Tan     access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
10651da12ec4SLe Tan 
10661da12ec4SLe Tan     while (true) {
10676e905564SPeter Xu         offset = vtd_iova_level_offset(iova, level);
10681da12ec4SLe Tan         slpte = vtd_get_slpte(addr, offset);
10691da12ec4SLe Tan 
10701da12ec4SLe Tan         if (slpte == (uint64_t)-1) {
10714e4abd11SPeter Xu             error_report_once("%s: detected read error on DMAR slpte "
10721b2b1237SJason Wang                               "(iova=0x%" PRIx64 ", pasid=0x%" PRIx32 ")",
10731b2b1237SJason Wang                               __func__, iova, pasid);
10741b2b1237SJason Wang             if (level == vtd_get_iova_level(s, ce, pasid)) {
10751da12ec4SLe Tan                 /* Invalid programming of context-entry */
10761da12ec4SLe Tan                 return -VTD_FR_CONTEXT_ENTRY_INV;
10771da12ec4SLe Tan             } else {
10781da12ec4SLe Tan                 return -VTD_FR_PAGING_ENTRY_INV;
10791da12ec4SLe Tan             }
10801da12ec4SLe Tan         }
10811da12ec4SLe Tan         *reads = (*reads) && (slpte & VTD_SL_R);
10821da12ec4SLe Tan         *writes = (*writes) && (slpte & VTD_SL_W);
10831da12ec4SLe Tan         if (!(slpte & access_right_check)) {
10844e4abd11SPeter Xu             error_report_once("%s: detected slpte permission error "
10854e4abd11SPeter Xu                               "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
10861b2b1237SJason Wang                               "slpte=0x%" PRIx64 ", write=%d, pasid=0x%"
10871b2b1237SJason Wang                               PRIx32 ")", __func__, iova, level,
10881b2b1237SJason Wang                               slpte, is_write, pasid);
10891da12ec4SLe Tan             return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
10901da12ec4SLe Tan         }
10911da12ec4SLe Tan         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
10924e4abd11SPeter Xu             error_report_once("%s: detected splte reserve non-zero "
10934e4abd11SPeter Xu                               "iova=0x%" PRIx64 ", level=0x%" PRIx32
10941b2b1237SJason Wang                               "slpte=0x%" PRIx64 ", pasid=0x%" PRIX32 ")",
10951b2b1237SJason Wang                               __func__, iova, level, slpte, pasid);
10961da12ec4SLe Tan             return -VTD_FR_PAGING_ENTRY_RSVD;
10971da12ec4SLe Tan         }
10981da12ec4SLe Tan 
10991da12ec4SLe Tan         if (vtd_is_last_slpte(slpte, level)) {
11001da12ec4SLe Tan             *slptep = slpte;
11011da12ec4SLe Tan             *slpte_level = level;
1102ea97a1bdSJason Wang             break;
11031da12ec4SLe Tan         }
110437f51384SPrasad Singamsetty         addr = vtd_get_slpte_addr(slpte, aw_bits);
11051da12ec4SLe Tan         level--;
11061da12ec4SLe Tan     }
1107ea97a1bdSJason Wang 
1108ea97a1bdSJason Wang     xlat = vtd_get_slpte_addr(*slptep, aw_bits);
1109ea97a1bdSJason Wang     size = ~vtd_slpt_level_page_mask(level) + 1;
1110ea97a1bdSJason Wang 
1111ea97a1bdSJason Wang     /*
1112ea97a1bdSJason Wang      * From VT-d spec 3.14: Untranslated requests and translation
1113ea97a1bdSJason Wang      * requests that result in an address in the interrupt range will be
1114ea97a1bdSJason Wang      * blocked with condition code LGN.4 or SGN.8.
1115ea97a1bdSJason Wang      */
1116ea97a1bdSJason Wang     if ((xlat > VTD_INTERRUPT_ADDR_LAST ||
1117ea97a1bdSJason Wang          xlat + size - 1 < VTD_INTERRUPT_ADDR_FIRST)) {
1118ea97a1bdSJason Wang         return 0;
1119ea97a1bdSJason Wang     } else {
1120ea97a1bdSJason Wang         error_report_once("%s: xlat address is in interrupt range "
1121ea97a1bdSJason Wang                           "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
1122ea97a1bdSJason Wang                           "slpte=0x%" PRIx64 ", write=%d, "
11231b2b1237SJason Wang                           "xlat=0x%" PRIx64 ", size=0x%" PRIx64 ", "
11241b2b1237SJason Wang                           "pasid=0x%" PRIx32 ")",
1125ea97a1bdSJason Wang                           __func__, iova, level, slpte, is_write,
11261b2b1237SJason Wang                           xlat, size, pasid);
1127ea97a1bdSJason Wang         return s->scalable_mode ? -VTD_FR_SM_INTERRUPT_ADDR :
1128ea97a1bdSJason Wang                                   -VTD_FR_INTERRUPT_ADDR;
1129ea97a1bdSJason Wang     }
11301da12ec4SLe Tan }
11311da12ec4SLe Tan 
11325039caf3SEugenio Pérez typedef int (*vtd_page_walk_hook)(IOMMUTLBEvent *event, void *private);
1133f06a696dSPeter Xu 
1134fe215b0cSPeter Xu /**
1135fe215b0cSPeter Xu  * Constant information used during page walking
1136fe215b0cSPeter Xu  *
1137fe215b0cSPeter Xu  * @hook_fn: hook func to be called when detected page
1138fe215b0cSPeter Xu  * @private: private data to be passed into hook func
1139fe215b0cSPeter Xu  * @notify_unmap: whether we should notify invalid entries
11402f764fa8SPeter Xu  * @as: VT-d address space of the device
1141fe215b0cSPeter Xu  * @aw: maximum address width
1142d118c06eSPeter Xu  * @domain: domain ID of the page walk
1143fe215b0cSPeter Xu  */
1144fe215b0cSPeter Xu typedef struct {
11452f764fa8SPeter Xu     VTDAddressSpace *as;
1146fe215b0cSPeter Xu     vtd_page_walk_hook hook_fn;
1147fe215b0cSPeter Xu     void *private;
1148fe215b0cSPeter Xu     bool notify_unmap;
1149fe215b0cSPeter Xu     uint8_t aw;
1150d118c06eSPeter Xu     uint16_t domain_id;
1151fe215b0cSPeter Xu } vtd_page_walk_info;
1152fe215b0cSPeter Xu 
11535039caf3SEugenio Pérez static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
115436d2d52bSPeter Xu {
115563b88968SPeter Xu     VTDAddressSpace *as = info->as;
1156fe215b0cSPeter Xu     vtd_page_walk_hook hook_fn = info->hook_fn;
1157fe215b0cSPeter Xu     void *private = info->private;
11585039caf3SEugenio Pérez     IOMMUTLBEntry *entry = &event->entry;
115963b88968SPeter Xu     DMAMap target = {
116063b88968SPeter Xu         .iova = entry->iova,
116163b88968SPeter Xu         .size = entry->addr_mask,
116263b88968SPeter Xu         .translated_addr = entry->translated_addr,
116363b88968SPeter Xu         .perm = entry->perm,
116463b88968SPeter Xu     };
1165a89b34beSEugenio Pérez     const DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
116663b88968SPeter Xu 
11675039caf3SEugenio Pérez     if (event->type == IOMMU_NOTIFIER_UNMAP && !info->notify_unmap) {
116863b88968SPeter Xu         trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
116963b88968SPeter Xu         return 0;
117063b88968SPeter Xu     }
1171fe215b0cSPeter Xu 
117236d2d52bSPeter Xu     assert(hook_fn);
117363b88968SPeter Xu 
117463b88968SPeter Xu     /* Update local IOVA mapped ranges */
11755039caf3SEugenio Pérez     if (event->type == IOMMU_NOTIFIER_MAP) {
117663b88968SPeter Xu         if (mapped) {
117763b88968SPeter Xu             /* If it's exactly the same translation, skip */
117863b88968SPeter Xu             if (!memcmp(mapped, &target, sizeof(target))) {
117963b88968SPeter Xu                 trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask,
118063b88968SPeter Xu                                                  entry->translated_addr);
118163b88968SPeter Xu                 return 0;
118263b88968SPeter Xu             } else {
118363b88968SPeter Xu                 /*
118463b88968SPeter Xu                  * Translation changed.  Normally this should not
118563b88968SPeter Xu                  * happen, but it can happen when with buggy guest
118663b88968SPeter Xu                  * OSes.  Note that there will be a small window that
118763b88968SPeter Xu                  * we don't have map at all.  But that's the best
118863b88968SPeter Xu                  * effort we can do.  The ideal way to emulate this is
118963b88968SPeter Xu                  * atomically modify the PTE to follow what has
119063b88968SPeter Xu                  * changed, but we can't.  One example is that vfio
119163b88968SPeter Xu                  * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
119263b88968SPeter Xu                  * interface to modify a mapping (meanwhile it seems
119363b88968SPeter Xu                  * meaningless to even provide one).  Anyway, let's
119463b88968SPeter Xu                  * mark this as a TODO in case one day we'll have
119563b88968SPeter Xu                  * a better solution.
119663b88968SPeter Xu                  */
119763b88968SPeter Xu                 IOMMUAccessFlags cache_perm = entry->perm;
119863b88968SPeter Xu                 int ret;
119963b88968SPeter Xu 
120063b88968SPeter Xu                 /* Emulate an UNMAP */
12015039caf3SEugenio Pérez                 event->type = IOMMU_NOTIFIER_UNMAP;
120263b88968SPeter Xu                 entry->perm = IOMMU_NONE;
120363b88968SPeter Xu                 trace_vtd_page_walk_one(info->domain_id,
120463b88968SPeter Xu                                         entry->iova,
120563b88968SPeter Xu                                         entry->translated_addr,
120663b88968SPeter Xu                                         entry->addr_mask,
120763b88968SPeter Xu                                         entry->perm);
12085039caf3SEugenio Pérez                 ret = hook_fn(event, private);
120963b88968SPeter Xu                 if (ret) {
121063b88968SPeter Xu                     return ret;
121163b88968SPeter Xu                 }
121263b88968SPeter Xu                 /* Drop any existing mapping */
121369292a8eSEugenio Pérez                 iova_tree_remove(as->iova_tree, target);
12145039caf3SEugenio Pérez                 /* Recover the correct type */
12155039caf3SEugenio Pérez                 event->type = IOMMU_NOTIFIER_MAP;
121663b88968SPeter Xu                 entry->perm = cache_perm;
121763b88968SPeter Xu             }
121863b88968SPeter Xu         }
121963b88968SPeter Xu         iova_tree_insert(as->iova_tree, &target);
122063b88968SPeter Xu     } else {
122163b88968SPeter Xu         if (!mapped) {
122263b88968SPeter Xu             /* Skip since we didn't map this range at all */
122363b88968SPeter Xu             trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
122463b88968SPeter Xu             return 0;
122563b88968SPeter Xu         }
122669292a8eSEugenio Pérez         iova_tree_remove(as->iova_tree, target);
122763b88968SPeter Xu     }
122863b88968SPeter Xu 
1229d118c06eSPeter Xu     trace_vtd_page_walk_one(info->domain_id, entry->iova,
1230d118c06eSPeter Xu                             entry->translated_addr, entry->addr_mask,
1231d118c06eSPeter Xu                             entry->perm);
12325039caf3SEugenio Pérez     return hook_fn(event, private);
123336d2d52bSPeter Xu }
123436d2d52bSPeter Xu 
1235f06a696dSPeter Xu /**
1236f06a696dSPeter Xu  * vtd_page_walk_level - walk over specific level for IOVA range
1237f06a696dSPeter Xu  *
1238f06a696dSPeter Xu  * @addr: base GPA addr to start the walk
1239f06a696dSPeter Xu  * @start: IOVA range start address
1240f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
1241f06a696dSPeter Xu  * @read: whether parent level has read permission
1242f06a696dSPeter Xu  * @write: whether parent level has write permission
1243fe215b0cSPeter Xu  * @info: constant information for the page walk
1244f06a696dSPeter Xu  */
1245f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
1246fe215b0cSPeter Xu                                uint64_t end, uint32_t level, bool read,
1247fe215b0cSPeter Xu                                bool write, vtd_page_walk_info *info)
1248f06a696dSPeter Xu {
1249f06a696dSPeter Xu     bool read_cur, write_cur, entry_valid;
1250f06a696dSPeter Xu     uint32_t offset;
1251f06a696dSPeter Xu     uint64_t slpte;
1252f06a696dSPeter Xu     uint64_t subpage_size, subpage_mask;
12535039caf3SEugenio Pérez     IOMMUTLBEvent event;
1254f06a696dSPeter Xu     uint64_t iova = start;
1255f06a696dSPeter Xu     uint64_t iova_next;
1256f06a696dSPeter Xu     int ret = 0;
1257f06a696dSPeter Xu 
1258f06a696dSPeter Xu     trace_vtd_page_walk_level(addr, level, start, end);
1259f06a696dSPeter Xu 
1260f06a696dSPeter Xu     subpage_size = 1ULL << vtd_slpt_level_shift(level);
1261f06a696dSPeter Xu     subpage_mask = vtd_slpt_level_page_mask(level);
1262f06a696dSPeter Xu 
1263f06a696dSPeter Xu     while (iova < end) {
1264f06a696dSPeter Xu         iova_next = (iova & subpage_mask) + subpage_size;
1265f06a696dSPeter Xu 
1266f06a696dSPeter Xu         offset = vtd_iova_level_offset(iova, level);
1267f06a696dSPeter Xu         slpte = vtd_get_slpte(addr, offset);
1268f06a696dSPeter Xu 
1269f06a696dSPeter Xu         if (slpte == (uint64_t)-1) {
1270f06a696dSPeter Xu             trace_vtd_page_walk_skip_read(iova, iova_next);
1271f06a696dSPeter Xu             goto next;
1272f06a696dSPeter Xu         }
1273f06a696dSPeter Xu 
1274f06a696dSPeter Xu         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
1275f06a696dSPeter Xu             trace_vtd_page_walk_skip_reserve(iova, iova_next);
1276f06a696dSPeter Xu             goto next;
1277f06a696dSPeter Xu         }
1278f06a696dSPeter Xu 
1279f06a696dSPeter Xu         /* Permissions are stacked with parents' */
1280f06a696dSPeter Xu         read_cur = read && (slpte & VTD_SL_R);
1281f06a696dSPeter Xu         write_cur = write && (slpte & VTD_SL_W);
1282f06a696dSPeter Xu 
1283f06a696dSPeter Xu         /*
1284f06a696dSPeter Xu          * As long as we have either read/write permission, this is a
1285f06a696dSPeter Xu          * valid entry. The rule works for both page entries and page
1286f06a696dSPeter Xu          * table entries.
1287f06a696dSPeter Xu          */
1288f06a696dSPeter Xu         entry_valid = read_cur | write_cur;
1289f06a696dSPeter Xu 
129063b88968SPeter Xu         if (!vtd_is_last_slpte(slpte, level) && entry_valid) {
129163b88968SPeter Xu             /*
129263b88968SPeter Xu              * This is a valid PDE (or even bigger than PDE).  We need
129363b88968SPeter Xu              * to walk one further level.
129463b88968SPeter Xu              */
129563b88968SPeter Xu             ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw),
129663b88968SPeter Xu                                       iova, MIN(iova_next, end), level - 1,
129763b88968SPeter Xu                                       read_cur, write_cur, info);
129863b88968SPeter Xu         } else {
129963b88968SPeter Xu             /*
130063b88968SPeter Xu              * This means we are either:
130163b88968SPeter Xu              *
130263b88968SPeter Xu              * (1) the real page entry (either 4K page, or huge page)
130363b88968SPeter Xu              * (2) the whole range is invalid
130463b88968SPeter Xu              *
130563b88968SPeter Xu              * In either case, we send an IOTLB notification down.
130663b88968SPeter Xu              */
13075039caf3SEugenio Pérez             event.entry.target_as = &address_space_memory;
13085039caf3SEugenio Pérez             event.entry.iova = iova & subpage_mask;
13095039caf3SEugenio Pérez             event.entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
13105039caf3SEugenio Pérez             event.entry.addr_mask = ~subpage_mask;
1311f06a696dSPeter Xu             /* NOTE: this is only meaningful if entry_valid == true */
13125039caf3SEugenio Pérez             event.entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
13135039caf3SEugenio Pérez             event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP :
13145039caf3SEugenio Pérez                                             IOMMU_NOTIFIER_UNMAP;
13155039caf3SEugenio Pérez             ret = vtd_page_walk_one(&event, info);
131663b88968SPeter Xu         }
131763b88968SPeter Xu 
1318f06a696dSPeter Xu         if (ret < 0) {
1319f06a696dSPeter Xu             return ret;
1320f06a696dSPeter Xu         }
1321f06a696dSPeter Xu 
1322f06a696dSPeter Xu next:
1323f06a696dSPeter Xu         iova = iova_next;
1324f06a696dSPeter Xu     }
1325f06a696dSPeter Xu 
1326f06a696dSPeter Xu     return 0;
1327f06a696dSPeter Xu }
1328f06a696dSPeter Xu 
1329f06a696dSPeter Xu /**
1330f06a696dSPeter Xu  * vtd_page_walk - walk specific IOVA range, and call the hook
1331f06a696dSPeter Xu  *
1332fb43cf73SLiu, Yi L  * @s: intel iommu state
1333f06a696dSPeter Xu  * @ce: context entry to walk upon
1334f06a696dSPeter Xu  * @start: IOVA address to start the walk
1335f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
1336fe215b0cSPeter Xu  * @info: page walking information struct
1337f06a696dSPeter Xu  */
1338fb43cf73SLiu, Yi L static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce,
1339fb43cf73SLiu, Yi L                          uint64_t start, uint64_t end,
13401b2b1237SJason Wang                          vtd_page_walk_info *info,
13411b2b1237SJason Wang                          uint32_t pasid)
1342f06a696dSPeter Xu {
13431b2b1237SJason Wang     dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
13441b2b1237SJason Wang     uint32_t level = vtd_get_iova_level(s, ce, pasid);
1345f06a696dSPeter Xu 
13461b2b1237SJason Wang     if (!vtd_iova_range_check(s, start, ce, info->aw, pasid)) {
1347f06a696dSPeter Xu         return -VTD_FR_ADDR_BEYOND_MGAW;
1348f06a696dSPeter Xu     }
1349f06a696dSPeter Xu 
13501b2b1237SJason Wang     if (!vtd_iova_range_check(s, end, ce, info->aw, pasid)) {
1351f06a696dSPeter Xu         /* Fix end so that it reaches the maximum */
13521b2b1237SJason Wang         end = vtd_iova_limit(s, ce, info->aw, pasid);
1353f06a696dSPeter Xu     }
1354f06a696dSPeter Xu 
1355fe215b0cSPeter Xu     return vtd_page_walk_level(addr, start, end, level, true, true, info);
1356f06a696dSPeter Xu }
1357f06a696dSPeter Xu 
1358fb43cf73SLiu, Yi L static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s,
1359fb43cf73SLiu, Yi L                                           VTDRootEntry *re)
1360fb43cf73SLiu, Yi L {
1361fb43cf73SLiu, Yi L     /* Legacy Mode reserved bits check */
1362fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1363fb43cf73SLiu, Yi L         (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1364fb43cf73SLiu, Yi L         goto rsvd_err;
1365fb43cf73SLiu, Yi L 
1366fb43cf73SLiu, Yi L     /* Scalable Mode reserved bits check */
1367fb43cf73SLiu, Yi L     if (s->root_scalable &&
1368fb43cf73SLiu, Yi L         ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) ||
1369fb43cf73SLiu, Yi L          (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1370fb43cf73SLiu, Yi L         goto rsvd_err;
1371fb43cf73SLiu, Yi L 
1372fb43cf73SLiu, Yi L     return 0;
1373fb43cf73SLiu, Yi L 
1374fb43cf73SLiu, Yi L rsvd_err:
1375fb43cf73SLiu, Yi L     error_report_once("%s: invalid root entry: hi=0x%"PRIx64
1376fb43cf73SLiu, Yi L                       ", lo=0x%"PRIx64,
1377fb43cf73SLiu, Yi L                       __func__, re->hi, re->lo);
1378fb43cf73SLiu, Yi L     return -VTD_FR_ROOT_ENTRY_RSVD;
1379fb43cf73SLiu, Yi L }
1380fb43cf73SLiu, Yi L 
1381fb43cf73SLiu, Yi L static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,
1382fb43cf73SLiu, Yi L                                                     VTDContextEntry *ce)
1383fb43cf73SLiu, Yi L {
1384fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1385fb43cf73SLiu, Yi L         (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI ||
1386fb43cf73SLiu, Yi L          ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
1387fb43cf73SLiu, Yi L         error_report_once("%s: invalid context entry: hi=%"PRIx64
1388fb43cf73SLiu, Yi L                           ", lo=%"PRIx64" (reserved nonzero)",
1389fb43cf73SLiu, Yi L                           __func__, ce->hi, ce->lo);
1390fb43cf73SLiu, Yi L         return -VTD_FR_CONTEXT_ENTRY_RSVD;
1391fb43cf73SLiu, Yi L     }
1392fb43cf73SLiu, Yi L 
1393fb43cf73SLiu, Yi L     if (s->root_scalable &&
1394fb43cf73SLiu, Yi L         (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) ||
1395fb43cf73SLiu, Yi L          ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 ||
1396fb43cf73SLiu, Yi L          ce->val[2] ||
1397fb43cf73SLiu, Yi L          ce->val[3])) {
1398fb43cf73SLiu, Yi L         error_report_once("%s: invalid context entry: val[3]=%"PRIx64
1399fb43cf73SLiu, Yi L                           ", val[2]=%"PRIx64
1400fb43cf73SLiu, Yi L                           ", val[1]=%"PRIx64
1401fb43cf73SLiu, Yi L                           ", val[0]=%"PRIx64" (reserved nonzero)",
1402fb43cf73SLiu, Yi L                           __func__, ce->val[3], ce->val[2],
1403fb43cf73SLiu, Yi L                           ce->val[1], ce->val[0]);
1404fb43cf73SLiu, Yi L         return -VTD_FR_CONTEXT_ENTRY_RSVD;
1405fb43cf73SLiu, Yi L     }
1406fb43cf73SLiu, Yi L 
1407fb43cf73SLiu, Yi L     return 0;
1408fb43cf73SLiu, Yi L }
1409fb43cf73SLiu, Yi L 
1410fb43cf73SLiu, Yi L static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
1411fb43cf73SLiu, Yi L                                   VTDContextEntry *ce)
1412fb43cf73SLiu, Yi L {
1413fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1414fb43cf73SLiu, Yi L 
1415fb43cf73SLiu, Yi L     /*
1416fb43cf73SLiu, Yi L      * Make sure in Scalable Mode, a present context entry
1417fb43cf73SLiu, Yi L      * has valid rid2pasid setting, which includes valid
1418fb43cf73SLiu, Yi L      * rid2pasid field and corresponding pasid entry setting
1419fb43cf73SLiu, Yi L      */
14201b2b1237SJason Wang     return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
1421fb43cf73SLiu, Yi L }
1422fb43cf73SLiu, Yi L 
14231da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */
14241da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
14251da12ec4SLe Tan                                     uint8_t devfn, VTDContextEntry *ce)
14261da12ec4SLe Tan {
14271da12ec4SLe Tan     VTDRootEntry re;
14281da12ec4SLe Tan     int ret_fr;
1429f80c9874SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
14301da12ec4SLe Tan 
14311da12ec4SLe Tan     ret_fr = vtd_get_root_entry(s, bus_num, &re);
14321da12ec4SLe Tan     if (ret_fr) {
14331da12ec4SLe Tan         return ret_fr;
14341da12ec4SLe Tan     }
14351da12ec4SLe Tan 
1436fb43cf73SLiu, Yi L     if (!vtd_root_entry_present(s, &re, devfn)) {
14376c441e1dSPeter Xu         /* Not error - it's okay we don't have root entry. */
14386c441e1dSPeter Xu         trace_vtd_re_not_present(bus_num);
14391da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_P;
1440f80c9874SPeter Xu     }
1441f80c9874SPeter Xu 
1442fb43cf73SLiu, Yi L     ret_fr = vtd_root_entry_rsvd_bits_check(s, &re);
1443fb43cf73SLiu, Yi L     if (ret_fr) {
1444fb43cf73SLiu, Yi L         return ret_fr;
14451da12ec4SLe Tan     }
14461da12ec4SLe Tan 
1447fb43cf73SLiu, Yi L     ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce);
14481da12ec4SLe Tan     if (ret_fr) {
14491da12ec4SLe Tan         return ret_fr;
14501da12ec4SLe Tan     }
14511da12ec4SLe Tan 
14528f7d7161SPeter Xu     if (!vtd_ce_present(ce)) {
14536c441e1dSPeter Xu         /* Not error - it's okay we don't have context entry. */
14546c441e1dSPeter Xu         trace_vtd_ce_not_present(bus_num, devfn);
14551da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_P;
1456f80c9874SPeter Xu     }
1457f80c9874SPeter Xu 
1458fb43cf73SLiu, Yi L     ret_fr = vtd_context_entry_rsvd_bits_check(s, ce);
1459fb43cf73SLiu, Yi L     if (ret_fr) {
1460fb43cf73SLiu, Yi L         return ret_fr;
14611da12ec4SLe Tan     }
1462f80c9874SPeter Xu 
14631da12ec4SLe Tan     /* Check if the programming of context-entry is valid */
1464fb43cf73SLiu, Yi L     if (!s->root_scalable &&
1465fb43cf73SLiu, Yi L         !vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
1466095955b2SPeter Xu         error_report_once("%s: invalid context entry: hi=%"PRIx64
1467095955b2SPeter Xu                           ", lo=%"PRIx64" (level %d not supported)",
1468fb43cf73SLiu, Yi L                           __func__, ce->hi, ce->lo,
1469fb43cf73SLiu, Yi L                           vtd_ce_get_level(ce));
14701da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
1471f80c9874SPeter Xu     }
1472f80c9874SPeter Xu 
1473fb43cf73SLiu, Yi L     if (!s->root_scalable) {
1474f80c9874SPeter Xu         /* Do translation type check */
1475f80c9874SPeter Xu         if (!vtd_ce_type_check(x86_iommu, ce)) {
1476095955b2SPeter Xu             /* Errors dumped in vtd_ce_type_check() */
14771da12ec4SLe Tan             return -VTD_FR_CONTEXT_ENTRY_INV;
14781da12ec4SLe Tan         }
1479fb43cf73SLiu, Yi L     } else {
1480fb43cf73SLiu, Yi L         /*
1481fb43cf73SLiu, Yi L          * Check if the programming of context-entry.rid2pasid
1482fb43cf73SLiu, Yi L          * and corresponding pasid setting is valid, and thus
1483fb43cf73SLiu, Yi L          * avoids to check pasid entry fetching result in future
1484fb43cf73SLiu, Yi L          * helper function calling.
1485fb43cf73SLiu, Yi L          */
1486fb43cf73SLiu, Yi L         ret_fr = vtd_ce_rid2pasid_check(s, ce);
1487fb43cf73SLiu, Yi L         if (ret_fr) {
1488fb43cf73SLiu, Yi L             return ret_fr;
1489fb43cf73SLiu, Yi L         }
1490fb43cf73SLiu, Yi L     }
1491f80c9874SPeter Xu 
14921da12ec4SLe Tan     return 0;
14931da12ec4SLe Tan }
14941da12ec4SLe Tan 
14955039caf3SEugenio Pérez static int vtd_sync_shadow_page_hook(IOMMUTLBEvent *event,
149663b88968SPeter Xu                                      void *private)
149763b88968SPeter Xu {
14985039caf3SEugenio Pérez     memory_region_notify_iommu(private, 0, *event);
149963b88968SPeter Xu     return 0;
150063b88968SPeter Xu }
150163b88968SPeter Xu 
1502fb43cf73SLiu, Yi L static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
15031b2b1237SJason Wang                                   VTDContextEntry *ce,
15041b2b1237SJason Wang                                   uint32_t pasid)
1505fb43cf73SLiu, Yi L {
1506fb43cf73SLiu, Yi L     VTDPASIDEntry pe;
1507fb43cf73SLiu, Yi L 
1508fb43cf73SLiu, Yi L     if (s->root_scalable) {
15091b2b1237SJason Wang         vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
1510fb43cf73SLiu, Yi L         return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
1511fb43cf73SLiu, Yi L     }
1512fb43cf73SLiu, Yi L 
1513fb43cf73SLiu, Yi L     return VTD_CONTEXT_ENTRY_DID(ce->hi);
1514fb43cf73SLiu, Yi L }
1515fb43cf73SLiu, Yi L 
151663b88968SPeter Xu static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
151763b88968SPeter Xu                                             VTDContextEntry *ce,
151863b88968SPeter Xu                                             hwaddr addr, hwaddr size)
151963b88968SPeter Xu {
152063b88968SPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
152163b88968SPeter Xu     vtd_page_walk_info info = {
152263b88968SPeter Xu         .hook_fn = vtd_sync_shadow_page_hook,
152363b88968SPeter Xu         .private = (void *)&vtd_as->iommu,
152463b88968SPeter Xu         .notify_unmap = true,
152563b88968SPeter Xu         .aw = s->aw_bits,
152663b88968SPeter Xu         .as = vtd_as,
15271b2b1237SJason Wang         .domain_id = vtd_get_domain_id(s, ce, vtd_as->pasid),
152863b88968SPeter Xu     };
152963b88968SPeter Xu 
15301b2b1237SJason Wang     return vtd_page_walk(s, ce, addr, addr + size, &info, vtd_as->pasid);
153163b88968SPeter Xu }
153263b88968SPeter Xu 
1533*3e090e34SPeter Xu static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
153463b88968SPeter Xu {
153595ecd3dfSPeter Xu     int ret;
153695ecd3dfSPeter Xu     VTDContextEntry ce;
1537c28b535dSPeter Xu     IOMMUNotifier *n;
153895ecd3dfSPeter Xu 
1539*3e090e34SPeter Xu     /* If no MAP notifier registered, we simply invalidate all the cache */
1540*3e090e34SPeter Xu     if (!vtd_as_has_map_notifier(vtd_as)) {
1541*3e090e34SPeter Xu         IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
1542*3e090e34SPeter Xu             memory_region_unmap_iommu_notifier_range(n);
1543*3e090e34SPeter Xu         }
1544f7701e2cSEugenio Pérez         return 0;
1545f7701e2cSEugenio Pérez     }
1546f7701e2cSEugenio Pérez 
154795ecd3dfSPeter Xu     ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
154895ecd3dfSPeter Xu                                    pci_bus_num(vtd_as->bus),
154995ecd3dfSPeter Xu                                    vtd_as->devfn, &ce);
155095ecd3dfSPeter Xu     if (ret) {
1551c28b535dSPeter Xu         if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
1552c28b535dSPeter Xu             /*
1553c28b535dSPeter Xu              * It's a valid scenario to have a context entry that is
1554c28b535dSPeter Xu              * not present.  For example, when a device is removed
1555c28b535dSPeter Xu              * from an existing domain then the context entry will be
1556c28b535dSPeter Xu              * zeroed by the guest before it was put into another
1557c28b535dSPeter Xu              * domain.  When this happens, instead of synchronizing
1558c28b535dSPeter Xu              * the shadow pages we should invalidate all existing
1559c28b535dSPeter Xu              * mappings and notify the backends.
1560c28b535dSPeter Xu              */
1561c28b535dSPeter Xu             IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
1562c28b535dSPeter Xu                 vtd_address_space_unmap(vtd_as, n);
1563c28b535dSPeter Xu             }
1564c28b535dSPeter Xu             ret = 0;
1565c28b535dSPeter Xu         }
156695ecd3dfSPeter Xu         return ret;
156795ecd3dfSPeter Xu     }
156895ecd3dfSPeter Xu 
156995ecd3dfSPeter Xu     return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX);
157063b88968SPeter Xu }
157163b88968SPeter Xu 
1572dbaabb25SPeter Xu /*
157337557b09SCai Huoqing  * Check if specific device is configured to bypass address
1574fb43cf73SLiu, Yi L  * translation for DMA requests. In Scalable Mode, bypass
1575fb43cf73SLiu, Yi L  * 1st-level translation or 2nd-level translation, it depends
1576fb43cf73SLiu, Yi L  * on PGTT setting.
1577dbaabb25SPeter Xu  */
15781b2b1237SJason Wang static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
15791b2b1237SJason Wang                                uint32_t pasid)
15805178d78fSJason Wang {
15815178d78fSJason Wang     VTDPASIDEntry pe;
15825178d78fSJason Wang     int ret;
15835178d78fSJason Wang 
15845178d78fSJason Wang     if (s->root_scalable) {
15851b2b1237SJason Wang         ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
15865178d78fSJason Wang         if (ret) {
1587fb1d084bSJason Wang             /*
1588fb1d084bSJason Wang              * This error is guest triggerable. We should assumt PT
1589fb1d084bSJason Wang              * not enabled for safety.
1590fb1d084bSJason Wang              */
15915178d78fSJason Wang             return false;
15925178d78fSJason Wang         }
15935178d78fSJason Wang         return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
15945178d78fSJason Wang     }
15955178d78fSJason Wang 
15965178d78fSJason Wang     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
15975178d78fSJason Wang 
15985178d78fSJason Wang }
15995178d78fSJason Wang 
16005178d78fSJason Wang static bool vtd_as_pt_enabled(VTDAddressSpace *as)
1601dbaabb25SPeter Xu {
1602dbaabb25SPeter Xu     IntelIOMMUState *s;
1603dbaabb25SPeter Xu     VTDContextEntry ce;
1604dbaabb25SPeter Xu 
1605dbaabb25SPeter Xu     assert(as);
1606dbaabb25SPeter Xu 
1607fb43cf73SLiu, Yi L     s = as->iommu_state;
1608fb1d084bSJason Wang     if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
1609fb1d084bSJason Wang                                  &ce)) {
1610dbaabb25SPeter Xu         /*
1611dbaabb25SPeter Xu          * Possibly failed to parse the context entry for some reason
1612dbaabb25SPeter Xu          * (e.g., during init, or any guest configuration errors on
1613dbaabb25SPeter Xu          * context entries). We should assume PT not enabled for
1614dbaabb25SPeter Xu          * safety.
1615dbaabb25SPeter Xu          */
1616dbaabb25SPeter Xu         return false;
1617dbaabb25SPeter Xu     }
1618dbaabb25SPeter Xu 
16191b2b1237SJason Wang     return vtd_dev_pt_enabled(s, &ce, as->pasid);
1620dbaabb25SPeter Xu }
1621dbaabb25SPeter Xu 
1622dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */
1623dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as)
1624dbaabb25SPeter Xu {
16251b2b1237SJason Wang     bool use_iommu, pt;
162666a4a031SPeter Xu     /* Whether we need to take the BQL on our own */
162766a4a031SPeter Xu     bool take_bql = !qemu_mutex_iothread_locked();
1628dbaabb25SPeter Xu 
1629dbaabb25SPeter Xu     assert(as);
1630dbaabb25SPeter Xu 
16315178d78fSJason Wang     use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as);
16321b2b1237SJason Wang     pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);
1633dbaabb25SPeter Xu 
1634dbaabb25SPeter Xu     trace_vtd_switch_address_space(pci_bus_num(as->bus),
1635dbaabb25SPeter Xu                                    VTD_PCI_SLOT(as->devfn),
1636dbaabb25SPeter Xu                                    VTD_PCI_FUNC(as->devfn),
1637dbaabb25SPeter Xu                                    use_iommu);
1638dbaabb25SPeter Xu 
163966a4a031SPeter Xu     /*
164066a4a031SPeter Xu      * It's possible that we reach here without BQL, e.g., when called
164166a4a031SPeter Xu      * from vtd_pt_enable_fast_path(). However the memory APIs need
164266a4a031SPeter Xu      * it. We'd better make sure we have had it already, or, take it.
164366a4a031SPeter Xu      */
164466a4a031SPeter Xu     if (take_bql) {
164566a4a031SPeter Xu         qemu_mutex_lock_iothread();
164666a4a031SPeter Xu     }
164766a4a031SPeter Xu 
1648dbaabb25SPeter Xu     /* Turn off first then on the other */
1649dbaabb25SPeter Xu     if (use_iommu) {
16504b519ef1SPeter Xu         memory_region_set_enabled(&as->nodmar, false);
16513df9d748SAlexey Kardashevskiy         memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
16521b2b1237SJason Wang         /*
16531b2b1237SJason Wang          * vt-d spec v3.4 3.14:
16541b2b1237SJason Wang          *
16551b2b1237SJason Wang          * """
16561b2b1237SJason Wang          * Requests-with-PASID with input address in range 0xFEEx_xxxx
16571b2b1237SJason Wang          * are translated normally like any other request-with-PASID
16581b2b1237SJason Wang          * through DMA-remapping hardware.
16591b2b1237SJason Wang          * """
16601b2b1237SJason Wang          *
16611b2b1237SJason Wang          * Need to disable ir for as with PASID.
16621b2b1237SJason Wang          */
16631b2b1237SJason Wang         if (as->pasid != PCI_NO_PASID) {
16641b2b1237SJason Wang             memory_region_set_enabled(&as->iommu_ir, false);
16651b2b1237SJason Wang         } else {
16661b2b1237SJason Wang             memory_region_set_enabled(&as->iommu_ir, true);
16671b2b1237SJason Wang         }
1668dbaabb25SPeter Xu     } else {
16693df9d748SAlexey Kardashevskiy         memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
16704b519ef1SPeter Xu         memory_region_set_enabled(&as->nodmar, true);
1671dbaabb25SPeter Xu     }
1672dbaabb25SPeter Xu 
16731b2b1237SJason Wang     /*
16741b2b1237SJason Wang      * vtd-spec v3.4 3.14:
16751b2b1237SJason Wang      *
16761b2b1237SJason Wang      * """
16771b2b1237SJason Wang      * Requests-with-PASID with input address in range 0xFEEx_xxxx are
16781b2b1237SJason Wang      * translated normally like any other request-with-PASID through
16791b2b1237SJason Wang      * DMA-remapping hardware. However, if such a request is processed
16801b2b1237SJason Wang      * using pass-through translation, it will be blocked as described
16811b2b1237SJason Wang      * in the paragraph below.
16821b2b1237SJason Wang      *
16831b2b1237SJason Wang      * Software must not program paging-structure entries to remap any
16841b2b1237SJason Wang      * address to the interrupt address range. Untranslated requests
16851b2b1237SJason Wang      * and translation requests that result in an address in the
16861b2b1237SJason Wang      * interrupt range will be blocked with condition code LGN.4 or
16871b2b1237SJason Wang      * SGN.8.
16881b2b1237SJason Wang      * """
16891b2b1237SJason Wang      *
16901b2b1237SJason Wang      * We enable per as memory region (iommu_ir_fault) for catching
16911b2b1237SJason Wang      * the tranlsation for interrupt range through PASID + PT.
16921b2b1237SJason Wang      */
16931b2b1237SJason Wang     if (pt && as->pasid != PCI_NO_PASID) {
16941b2b1237SJason Wang         memory_region_set_enabled(&as->iommu_ir_fault, true);
16951b2b1237SJason Wang     } else {
16961b2b1237SJason Wang         memory_region_set_enabled(&as->iommu_ir_fault, false);
16971b2b1237SJason Wang     }
16981b2b1237SJason Wang 
169966a4a031SPeter Xu     if (take_bql) {
170066a4a031SPeter Xu         qemu_mutex_unlock_iothread();
170166a4a031SPeter Xu     }
170266a4a031SPeter Xu 
1703dbaabb25SPeter Xu     return use_iommu;
1704dbaabb25SPeter Xu }
1705dbaabb25SPeter Xu 
1706dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s)
1707dbaabb25SPeter Xu {
1708da8d439cSJason Wang     VTDAddressSpace *vtd_as;
1709dbaabb25SPeter Xu     GHashTableIter iter;
1710dbaabb25SPeter Xu 
1711da8d439cSJason Wang     g_hash_table_iter_init(&iter, s->vtd_address_spaces);
1712da8d439cSJason Wang     while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_as)) {
1713da8d439cSJason Wang         vtd_switch_address_space(vtd_as);
1714dbaabb25SPeter Xu     }
17151da12ec4SLe Tan }
17161da12ec4SLe Tan 
17171da12ec4SLe Tan static const bool vtd_qualified_faults[] = {
17181da12ec4SLe Tan     [VTD_FR_RESERVED] = false,
17191da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_P] = false,
17201da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_P] = true,
17211da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_INV] = true,
17221da12ec4SLe Tan     [VTD_FR_ADDR_BEYOND_MGAW] = true,
17231da12ec4SLe Tan     [VTD_FR_WRITE] = true,
17241da12ec4SLe Tan     [VTD_FR_READ] = true,
17251da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_INV] = true,
17261da12ec4SLe Tan     [VTD_FR_ROOT_TABLE_INV] = false,
17271da12ec4SLe Tan     [VTD_FR_CONTEXT_TABLE_INV] = false,
1728ea97a1bdSJason Wang     [VTD_FR_INTERRUPT_ADDR] = true,
17291da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_RSVD] = false,
17301da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_RSVD] = true,
17311da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_TT] = true,
1732fb43cf73SLiu, Yi L     [VTD_FR_PASID_TABLE_INV] = false,
1733ea97a1bdSJason Wang     [VTD_FR_SM_INTERRUPT_ADDR] = true,
17341da12ec4SLe Tan     [VTD_FR_MAX] = false,
17351da12ec4SLe Tan };
17361da12ec4SLe Tan 
17371da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software
17381da12ec4SLe Tan  * only if the FPD field in the context-entry used to process the faulting
17391da12ec4SLe Tan  * request is 0.
17401da12ec4SLe Tan  */
17411da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
17421da12ec4SLe Tan {
17431da12ec4SLe Tan     return vtd_qualified_faults[fault];
17441da12ec4SLe Tan }
17451da12ec4SLe Tan 
17461da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr)
17471da12ec4SLe Tan {
17481da12ec4SLe Tan     return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
17491da12ec4SLe Tan }
17501da12ec4SLe Tan 
1751da8d439cSJason Wang static gboolean vtd_find_as_by_sid(gpointer key, gpointer value,
1752da8d439cSJason Wang                                    gpointer user_data)
1753da8d439cSJason Wang {
1754da8d439cSJason Wang     struct vtd_as_key *as_key = (struct vtd_as_key *)key;
1755da8d439cSJason Wang     uint16_t target_sid = *(uint16_t *)user_data;
1756da8d439cSJason Wang     uint16_t sid = PCI_BUILD_BDF(pci_bus_num(as_key->bus), as_key->devfn);
1757da8d439cSJason Wang     return sid == target_sid;
1758da8d439cSJason Wang }
1759da8d439cSJason Wang 
1760da8d439cSJason Wang static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
1761da8d439cSJason Wang {
1762da8d439cSJason Wang     uint8_t bus_num = PCI_BUS_NUM(sid);
1763da8d439cSJason Wang     VTDAddressSpace *vtd_as = s->vtd_as_cache[bus_num];
1764da8d439cSJason Wang 
1765da8d439cSJason Wang     if (vtd_as &&
1766da8d439cSJason Wang         (sid == PCI_BUILD_BDF(pci_bus_num(vtd_as->bus), vtd_as->devfn))) {
1767da8d439cSJason Wang         return vtd_as;
1768da8d439cSJason Wang     }
1769da8d439cSJason Wang 
1770da8d439cSJason Wang     vtd_as = g_hash_table_find(s->vtd_address_spaces, vtd_find_as_by_sid, &sid);
1771da8d439cSJason Wang     s->vtd_as_cache[bus_num] = vtd_as;
1772da8d439cSJason Wang 
1773da8d439cSJason Wang     return vtd_as;
1774da8d439cSJason Wang }
1775da8d439cSJason Wang 
1776dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1777dbaabb25SPeter Xu {
1778dbaabb25SPeter Xu     VTDAddressSpace *vtd_as;
1779dbaabb25SPeter Xu     bool success = false;
1780dbaabb25SPeter Xu 
1781da8d439cSJason Wang     vtd_as = vtd_get_as_by_sid(s, source_id);
1782dbaabb25SPeter Xu     if (!vtd_as) {
1783dbaabb25SPeter Xu         goto out;
1784dbaabb25SPeter Xu     }
1785dbaabb25SPeter Xu 
1786dbaabb25SPeter Xu     if (vtd_switch_address_space(vtd_as) == false) {
1787dbaabb25SPeter Xu         /* We switched off IOMMU region successfully. */
1788dbaabb25SPeter Xu         success = true;
1789dbaabb25SPeter Xu     }
1790dbaabb25SPeter Xu 
1791dbaabb25SPeter Xu out:
1792dbaabb25SPeter Xu     trace_vtd_pt_enable_fast_path(source_id, success);
1793dbaabb25SPeter Xu }
1794dbaabb25SPeter Xu 
1795940e5527SJason Wang static void vtd_report_fault(IntelIOMMUState *s,
1796940e5527SJason Wang                              int err, bool is_fpd_set,
1797940e5527SJason Wang                              uint16_t source_id,
1798940e5527SJason Wang                              hwaddr addr,
17991b2b1237SJason Wang                              bool is_write,
18001b2b1237SJason Wang                              bool is_pasid,
18011b2b1237SJason Wang                              uint32_t pasid)
1802940e5527SJason Wang {
1803940e5527SJason Wang     if (is_fpd_set && vtd_is_qualified_fault(err)) {
1804940e5527SJason Wang         trace_vtd_fault_disabled();
1805940e5527SJason Wang     } else {
18061b2b1237SJason Wang         vtd_report_dmar_fault(s, source_id, addr, err, is_write,
18071b2b1237SJason Wang                               is_pasid, pasid);
1808940e5527SJason Wang     }
1809940e5527SJason Wang }
1810940e5527SJason Wang 
18111da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu
18121da12ec4SLe Tan  * translation.
181379e2b9aeSPaolo Bonzini  *
181479e2b9aeSPaolo Bonzini  * Called from RCU critical section.
181579e2b9aeSPaolo Bonzini  *
18161da12ec4SLe Tan  * @bus_num: The bus number
18171da12ec4SLe Tan  * @devfn: The devfn, which is the  combined of device and function number
18181da12ec4SLe Tan  * @is_write: The access is a write operation
18191da12ec4SLe Tan  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
1820b9313021SPeter Xu  *
1821b9313021SPeter Xu  * Returns true if translation is successful, otherwise false.
18221da12ec4SLe Tan  */
1823b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
18241da12ec4SLe Tan                                    uint8_t devfn, hwaddr addr, bool is_write,
18251da12ec4SLe Tan                                    IOMMUTLBEntry *entry)
18261da12ec4SLe Tan {
1827d92fa2dcSLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
18281da12ec4SLe Tan     VTDContextEntry ce;
18297df953bdSKnut Omang     uint8_t bus_num = pci_bus_num(bus);
18301d9efa73SPeter Xu     VTDContextCacheEntry *cc_entry;
1831d66b969bSJason Wang     uint64_t slpte, page_mask;
18321b2b1237SJason Wang     uint32_t level, pasid = vtd_as->pasid;
1833da8d439cSJason Wang     uint16_t source_id = PCI_BUILD_BDF(bus_num, devfn);
18341da12ec4SLe Tan     int ret_fr;
18351da12ec4SLe Tan     bool is_fpd_set = false;
18361da12ec4SLe Tan     bool reads = true;
18371da12ec4SLe Tan     bool writes = true;
183807f7b733SPeter Xu     uint8_t access_flags;
18391b2b1237SJason Wang     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
1840b5a280c0SLe Tan     VTDIOTLBEntry *iotlb_entry;
18411da12ec4SLe Tan 
1842046ab7e9SPeter Xu     /*
1843046ab7e9SPeter Xu      * We have standalone memory region for interrupt addresses, we
1844046ab7e9SPeter Xu      * should never receive translation requests in this region.
18451da12ec4SLe Tan      */
1846046ab7e9SPeter Xu     assert(!vtd_is_interrupt_addr(addr));
1847046ab7e9SPeter Xu 
18481d9efa73SPeter Xu     vtd_iommu_lock(s);
18491d9efa73SPeter Xu 
18501d9efa73SPeter Xu     cc_entry = &vtd_as->context_cache_entry;
18511d9efa73SPeter Xu 
18521b2b1237SJason Wang     /* Try to fetch slpte form IOTLB, we don't need RID2PASID logic */
18531b2b1237SJason Wang     if (!rid2pasid) {
18541b2b1237SJason Wang         iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
1855b5a280c0SLe Tan         if (iotlb_entry) {
18566c441e1dSPeter Xu             trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
18576c441e1dSPeter Xu                                      iotlb_entry->domain_id);
1858b5a280c0SLe Tan             slpte = iotlb_entry->slpte;
185907f7b733SPeter Xu             access_flags = iotlb_entry->access_flags;
1860d66b969bSJason Wang             page_mask = iotlb_entry->mask;
1861b5a280c0SLe Tan             goto out;
1862b5a280c0SLe Tan         }
18631b2b1237SJason Wang     }
1864b9313021SPeter Xu 
1865d92fa2dcSLe Tan     /* Try to fetch context-entry from cache first */
1866d92fa2dcSLe Tan     if (cc_entry->context_cache_gen == s->context_cache_gen) {
18676c441e1dSPeter Xu         trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
18686c441e1dSPeter Xu                                cc_entry->context_entry.lo,
18696c441e1dSPeter Xu                                cc_entry->context_cache_gen);
1870d92fa2dcSLe Tan         ce = cc_entry->context_entry;
1871d92fa2dcSLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1872fb43cf73SLiu, Yi L         if (!is_fpd_set && s->root_scalable) {
18731b2b1237SJason Wang             ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
1874940e5527SJason Wang             if (ret_fr) {
1875940e5527SJason Wang                 vtd_report_fault(s, -ret_fr, is_fpd_set,
18761b2b1237SJason Wang                                  source_id, addr, is_write,
18771b2b1237SJason Wang                                  false, 0);
1878940e5527SJason Wang                 goto error;
1879940e5527SJason Wang             }
1880fb43cf73SLiu, Yi L         }
1881d92fa2dcSLe Tan     } else {
18821da12ec4SLe Tan         ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
18831da12ec4SLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1884fb43cf73SLiu, Yi L         if (!ret_fr && !is_fpd_set && s->root_scalable) {
18851b2b1237SJason Wang             ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
18861da12ec4SLe Tan         }
1887940e5527SJason Wang         if (ret_fr) {
1888940e5527SJason Wang             vtd_report_fault(s, -ret_fr, is_fpd_set,
18891b2b1237SJason Wang                              source_id, addr, is_write,
18901b2b1237SJason Wang                              false, 0);
1891940e5527SJason Wang             goto error;
1892940e5527SJason Wang         }
1893d92fa2dcSLe Tan         /* Update context-cache */
18946c441e1dSPeter Xu         trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
18956c441e1dSPeter Xu                                   cc_entry->context_cache_gen,
18966c441e1dSPeter Xu                                   s->context_cache_gen);
1897d92fa2dcSLe Tan         cc_entry->context_entry = ce;
1898d92fa2dcSLe Tan         cc_entry->context_cache_gen = s->context_cache_gen;
1899d92fa2dcSLe Tan     }
19001da12ec4SLe Tan 
19011b2b1237SJason Wang     if (rid2pasid) {
19021b2b1237SJason Wang         pasid = VTD_CE_GET_RID2PASID(&ce);
19031b2b1237SJason Wang     }
19041b2b1237SJason Wang 
1905dbaabb25SPeter Xu     /*
1906dbaabb25SPeter Xu      * We don't need to translate for pass-through context entries.
1907dbaabb25SPeter Xu      * Also, let's ignore IOTLB caching as well for PT devices.
1908dbaabb25SPeter Xu      */
19091b2b1237SJason Wang     if (vtd_dev_pt_enabled(s, &ce, pasid)) {
1910892721d9SPeter Xu         entry->iova = addr & VTD_PAGE_MASK_4K;
1911dbaabb25SPeter Xu         entry->translated_addr = entry->iova;
1912892721d9SPeter Xu         entry->addr_mask = ~VTD_PAGE_MASK_4K;
1913dbaabb25SPeter Xu         entry->perm = IOMMU_RW;
1914dbaabb25SPeter Xu         trace_vtd_translate_pt(source_id, entry->iova);
1915dbaabb25SPeter Xu 
1916dbaabb25SPeter Xu         /*
1917dbaabb25SPeter Xu          * When this happens, it means firstly caching-mode is not
1918dbaabb25SPeter Xu          * enabled, and this is the first passthrough translation for
1919dbaabb25SPeter Xu          * the device. Let's enable the fast path for passthrough.
1920dbaabb25SPeter Xu          *
1921dbaabb25SPeter Xu          * When passthrough is disabled again for the device, we can
1922dbaabb25SPeter Xu          * capture it via the context entry invalidation, then the
1923dbaabb25SPeter Xu          * IOMMU region can be swapped back.
1924dbaabb25SPeter Xu          */
1925dbaabb25SPeter Xu         vtd_pt_enable_fast_path(s, source_id);
19261d9efa73SPeter Xu         vtd_iommu_unlock(s);
1927b9313021SPeter Xu         return true;
1928dbaabb25SPeter Xu     }
1929dbaabb25SPeter Xu 
19301b2b1237SJason Wang     /* Try to fetch slpte form IOTLB for RID2PASID slow path */
19311b2b1237SJason Wang     if (rid2pasid) {
19321b2b1237SJason Wang         iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
19331b2b1237SJason Wang         if (iotlb_entry) {
19341b2b1237SJason Wang             trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
19351b2b1237SJason Wang                                      iotlb_entry->domain_id);
19361b2b1237SJason Wang             slpte = iotlb_entry->slpte;
19371b2b1237SJason Wang             access_flags = iotlb_entry->access_flags;
19381b2b1237SJason Wang             page_mask = iotlb_entry->mask;
19391b2b1237SJason Wang             goto out;
19401b2b1237SJason Wang         }
19411b2b1237SJason Wang     }
19421b2b1237SJason Wang 
1943fb43cf73SLiu, Yi L     ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level,
19441b2b1237SJason Wang                                &reads, &writes, s->aw_bits, pasid);
1945940e5527SJason Wang     if (ret_fr) {
1946940e5527SJason Wang         vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
19471b2b1237SJason Wang                          addr, is_write, pasid != PCI_NO_PASID, pasid);
1948940e5527SJason Wang         goto error;
1949940e5527SJason Wang     }
19501da12ec4SLe Tan 
1951d66b969bSJason Wang     page_mask = vtd_slpt_level_page_mask(level);
195207f7b733SPeter Xu     access_flags = IOMMU_ACCESS_FLAG(reads, writes);
19531b2b1237SJason Wang     vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid),
19541b2b1237SJason Wang                      addr, slpte, access_flags, level, pasid);
1955b5a280c0SLe Tan out:
19561d9efa73SPeter Xu     vtd_iommu_unlock(s);
1957d66b969bSJason Wang     entry->iova = addr & page_mask;
195837f51384SPrasad Singamsetty     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
1959d66b969bSJason Wang     entry->addr_mask = ~page_mask;
196007f7b733SPeter Xu     entry->perm = access_flags;
1961b9313021SPeter Xu     return true;
1962b9313021SPeter Xu 
1963b9313021SPeter Xu error:
19641d9efa73SPeter Xu     vtd_iommu_unlock(s);
1965b9313021SPeter Xu     entry->iova = 0;
1966b9313021SPeter Xu     entry->translated_addr = 0;
1967b9313021SPeter Xu     entry->addr_mask = 0;
1968b9313021SPeter Xu     entry->perm = IOMMU_NONE;
1969b9313021SPeter Xu     return false;
19701da12ec4SLe Tan }
19711da12ec4SLe Tan 
19721da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s)
19731da12ec4SLe Tan {
19741da12ec4SLe Tan     s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
197537f51384SPrasad Singamsetty     s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
19761da12ec4SLe Tan 
19772811af3bSPeter Xu     vtd_update_scalable_state(s);
19782811af3bSPeter Xu 
197981fb1e64SPeter Xu     trace_vtd_reg_dmar_root(s->root, s->root_scalable);
19801da12ec4SLe Tan }
19811da12ec4SLe Tan 
198202a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
198302a2cbc8SPeter Xu                                uint32_t index, uint32_t mask)
198402a2cbc8SPeter Xu {
198502a2cbc8SPeter Xu     x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
198602a2cbc8SPeter Xu }
198702a2cbc8SPeter Xu 
1988a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1989a5861439SPeter Xu {
1990a5861439SPeter Xu     uint64_t value = 0;
1991a5861439SPeter Xu     value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1992a5861439SPeter Xu     s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
199337f51384SPrasad Singamsetty     s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
199428589311SJan Kiszka     s->intr_eime = value & VTD_IRTA_EIME;
1995a5861439SPeter Xu 
199602a2cbc8SPeter Xu     /* Notify global invalidation */
199702a2cbc8SPeter Xu     vtd_iec_notify_all(s, true, 0, 0);
1998a5861439SPeter Xu 
19997feb51b7SPeter Xu     trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
2000a5861439SPeter Xu }
2001a5861439SPeter Xu 
2002dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s)
2003dd4d607eSPeter Xu {
2004b4a4ba0dSPeter Xu     VTDAddressSpace *vtd_as;
2005dd4d607eSPeter Xu 
2006b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
2007*3e090e34SPeter Xu         vtd_address_space_sync(vtd_as);
2008dd4d607eSPeter Xu     }
2009dd4d607eSPeter Xu }
2010dd4d607eSPeter Xu 
2011d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s)
2012d92fa2dcSLe Tan {
2013bc535e59SPeter Xu     trace_vtd_inv_desc_cc_global();
20141d9efa73SPeter Xu     /* Protects context cache */
20151d9efa73SPeter Xu     vtd_iommu_lock(s);
2016d92fa2dcSLe Tan     s->context_cache_gen++;
2017d92fa2dcSLe Tan     if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
20181d9efa73SPeter Xu         vtd_reset_context_cache_locked(s);
2019d92fa2dcSLe Tan     }
20201d9efa73SPeter Xu     vtd_iommu_unlock(s);
20212cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
2022dd4d607eSPeter Xu     /*
2023dd4d607eSPeter Xu      * From VT-d spec 6.5.2.1, a global context entry invalidation
2024dd4d607eSPeter Xu      * should be followed by a IOTLB global invalidation, so we should
2025dd4d607eSPeter Xu      * be safe even without this. Hoewever, let's replay the region as
2026dd4d607eSPeter Xu      * well to be safer, and go back here when we need finer tunes for
2027dd4d607eSPeter Xu      * VT-d emulation codes.
2028dd4d607eSPeter Xu      */
2029dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
2030d92fa2dcSLe Tan }
2031d92fa2dcSLe Tan 
2032d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation.
2033d92fa2dcSLe Tan  * @func_mask: FM field after shifting
2034d92fa2dcSLe Tan  */
2035d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s,
2036d92fa2dcSLe Tan                                           uint16_t source_id,
2037d92fa2dcSLe Tan                                           uint16_t func_mask)
2038d92fa2dcSLe Tan {
2039da8d439cSJason Wang     GHashTableIter as_it;
2040d92fa2dcSLe Tan     uint16_t mask;
2041d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
2042bc535e59SPeter Xu     uint8_t bus_n, devfn;
2043d92fa2dcSLe Tan 
2044bc535e59SPeter Xu     trace_vtd_inv_desc_cc_devices(source_id, func_mask);
2045bc535e59SPeter Xu 
2046d92fa2dcSLe Tan     switch (func_mask & 3) {
2047d92fa2dcSLe Tan     case 0:
2048d92fa2dcSLe Tan         mask = 0;   /* No bits in the SID field masked */
2049d92fa2dcSLe Tan         break;
2050d92fa2dcSLe Tan     case 1:
2051d92fa2dcSLe Tan         mask = 4;   /* Mask bit 2 in the SID field */
2052d92fa2dcSLe Tan         break;
2053d92fa2dcSLe Tan     case 2:
2054d92fa2dcSLe Tan         mask = 6;   /* Mask bit 2:1 in the SID field */
2055d92fa2dcSLe Tan         break;
2056d92fa2dcSLe Tan     case 3:
2057d92fa2dcSLe Tan         mask = 7;   /* Mask bit 2:0 in the SID field */
2058d92fa2dcSLe Tan         break;
205941ce9a91SEric Auger     default:
206041ce9a91SEric Auger         g_assert_not_reached();
2061d92fa2dcSLe Tan     }
20626cb99accSPeter Xu     mask = ~mask;
2063bc535e59SPeter Xu 
2064bc535e59SPeter Xu     bus_n = VTD_SID_TO_BUS(source_id);
2065d92fa2dcSLe Tan     devfn = VTD_SID_TO_DEVFN(source_id);
2066da8d439cSJason Wang 
2067da8d439cSJason Wang     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
2068da8d439cSJason Wang     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
2069da8d439cSJason Wang         if ((pci_bus_num(vtd_as->bus) == bus_n) &&
2070da8d439cSJason Wang             (vtd_as->devfn & mask) == (devfn & mask)) {
2071da8d439cSJason Wang             trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(vtd_as->devfn),
2072da8d439cSJason Wang                                          VTD_PCI_FUNC(vtd_as->devfn));
20731d9efa73SPeter Xu             vtd_iommu_lock(s);
2074d92fa2dcSLe Tan             vtd_as->context_cache_entry.context_cache_gen = 0;
20751d9efa73SPeter Xu             vtd_iommu_unlock(s);
2076dd4d607eSPeter Xu             /*
2077dbaabb25SPeter Xu              * Do switch address space when needed, in case if the
2078dbaabb25SPeter Xu              * device passthrough bit is switched.
2079dbaabb25SPeter Xu              */
2080dbaabb25SPeter Xu             vtd_switch_address_space(vtd_as);
2081dbaabb25SPeter Xu             /*
2082dd4d607eSPeter Xu              * So a device is moving out of (or moving into) a
208363b88968SPeter Xu              * domain, resync the shadow page table.
2084dd4d607eSPeter Xu              * This won't bring bad even if we have no such
2085dd4d607eSPeter Xu              * notifier registered - the IOMMU notification
2086dd4d607eSPeter Xu              * framework will skip MAP notifications if that
2087dd4d607eSPeter Xu              * happened.
2088dd4d607eSPeter Xu              */
2089*3e090e34SPeter Xu             vtd_address_space_sync(vtd_as);
2090d92fa2dcSLe Tan         }
2091d92fa2dcSLe Tan     }
2092d92fa2dcSLe Tan }
2093d92fa2dcSLe Tan 
20941da12ec4SLe Tan /* Context-cache invalidation
20951da12ec4SLe Tan  * Returns the Context Actual Invalidation Granularity.
20961da12ec4SLe Tan  * @val: the content of the CCMD_REG
20971da12ec4SLe Tan  */
20981da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
20991da12ec4SLe Tan {
21001da12ec4SLe Tan     uint64_t caig;
21011da12ec4SLe Tan     uint64_t type = val & VTD_CCMD_CIRG_MASK;
21021da12ec4SLe Tan 
21031da12ec4SLe Tan     switch (type) {
21041da12ec4SLe Tan     case VTD_CCMD_DOMAIN_INVL:
2105d92fa2dcSLe Tan         /* Fall through */
2106d92fa2dcSLe Tan     case VTD_CCMD_GLOBAL_INVL:
2107d92fa2dcSLe Tan         caig = VTD_CCMD_GLOBAL_INVL_A;
2108d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
21091da12ec4SLe Tan         break;
21101da12ec4SLe Tan 
21111da12ec4SLe Tan     case VTD_CCMD_DEVICE_INVL:
21121da12ec4SLe Tan         caig = VTD_CCMD_DEVICE_INVL_A;
2113d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
21141da12ec4SLe Tan         break;
21151da12ec4SLe Tan 
21161da12ec4SLe Tan     default:
21171376211fSPeter Xu         error_report_once("%s: invalid context: 0x%" PRIx64,
21181376211fSPeter Xu                           __func__, val);
21191da12ec4SLe Tan         caig = 0;
21201da12ec4SLe Tan     }
21211da12ec4SLe Tan     return caig;
21221da12ec4SLe Tan }
21231da12ec4SLe Tan 
2124b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
2125b5a280c0SLe Tan {
21267feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_global();
2127b5a280c0SLe Tan     vtd_reset_iotlb(s);
2128dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
2129b5a280c0SLe Tan }
2130b5a280c0SLe Tan 
2131b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
2132b5a280c0SLe Tan {
2133dd4d607eSPeter Xu     VTDContextEntry ce;
2134dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
2135dd4d607eSPeter Xu 
21367feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_domain(domain_id);
21377feb51b7SPeter Xu 
21381d9efa73SPeter Xu     vtd_iommu_lock(s);
2139b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
2140b5a280c0SLe Tan                                 &domain_id);
21411d9efa73SPeter Xu     vtd_iommu_unlock(s);
2142dd4d607eSPeter Xu 
2143b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
2144dd4d607eSPeter Xu         if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
2145dd4d607eSPeter Xu                                       vtd_as->devfn, &ce) &&
21461b2b1237SJason Wang             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
2147*3e090e34SPeter Xu             vtd_address_space_sync(vtd_as);
2148dd4d607eSPeter Xu         }
2149dd4d607eSPeter Xu     }
2150dd4d607eSPeter Xu }
2151dd4d607eSPeter Xu 
2152dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
2153dd4d607eSPeter Xu                                            uint16_t domain_id, hwaddr addr,
21541b2b1237SJason Wang                                              uint8_t am, uint32_t pasid)
2155dd4d607eSPeter Xu {
2156b4a4ba0dSPeter Xu     VTDAddressSpace *vtd_as;
2157dd4d607eSPeter Xu     VTDContextEntry ce;
2158dd4d607eSPeter Xu     int ret;
21594f8a62a9SPeter Xu     hwaddr size = (1 << am) * VTD_PAGE_SIZE;
2160dd4d607eSPeter Xu 
2161b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
21621b2b1237SJason Wang         if (pasid != PCI_NO_PASID && pasid != vtd_as->pasid) {
21631b2b1237SJason Wang             continue;
21641b2b1237SJason Wang         }
2165dd4d607eSPeter Xu         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
2166dd4d607eSPeter Xu                                        vtd_as->devfn, &ce);
21671b2b1237SJason Wang         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
21684f8a62a9SPeter Xu             if (vtd_as_has_map_notifier(vtd_as)) {
21694f8a62a9SPeter Xu                 /*
21704f8a62a9SPeter Xu                  * As long as we have MAP notifications registered in
21714f8a62a9SPeter Xu                  * any of our IOMMU notifiers, we need to sync the
21724f8a62a9SPeter Xu                  * shadow page table.
21734f8a62a9SPeter Xu                  */
217463b88968SPeter Xu                 vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
21754f8a62a9SPeter Xu             } else {
21764f8a62a9SPeter Xu                 /*
21774f8a62a9SPeter Xu                  * For UNMAP-only notifiers, we don't need to walk the
21784f8a62a9SPeter Xu                  * page tables.  We just deliver the PSI down to
21794f8a62a9SPeter Xu                  * invalidate caches.
21804f8a62a9SPeter Xu                  */
21815039caf3SEugenio Pérez                 IOMMUTLBEvent event = {
21825039caf3SEugenio Pérez                     .type = IOMMU_NOTIFIER_UNMAP,
21835039caf3SEugenio Pérez                     .entry = {
21844f8a62a9SPeter Xu                         .target_as = &address_space_memory,
21854f8a62a9SPeter Xu                         .iova = addr,
21864f8a62a9SPeter Xu                         .translated_addr = 0,
21874f8a62a9SPeter Xu                         .addr_mask = size - 1,
21884f8a62a9SPeter Xu                         .perm = IOMMU_NONE,
21895039caf3SEugenio Pérez                     },
21904f8a62a9SPeter Xu                 };
21915039caf3SEugenio Pérez                 memory_region_notify_iommu(&vtd_as->iommu, 0, event);
21924f8a62a9SPeter Xu             }
2193dd4d607eSPeter Xu         }
2194dd4d607eSPeter Xu     }
2195b5a280c0SLe Tan }
2196b5a280c0SLe Tan 
2197b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
2198b5a280c0SLe Tan                                       hwaddr addr, uint8_t am)
2199b5a280c0SLe Tan {
2200b5a280c0SLe Tan     VTDIOTLBPageInvInfo info;
2201b5a280c0SLe Tan 
22027feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
22037feb51b7SPeter Xu 
2204b5a280c0SLe Tan     assert(am <= VTD_MAMV);
2205b5a280c0SLe Tan     info.domain_id = domain_id;
2206d66b969bSJason Wang     info.addr = addr;
2207b5a280c0SLe Tan     info.mask = ~((1 << am) - 1);
22081d9efa73SPeter Xu     vtd_iommu_lock(s);
2209b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
22101d9efa73SPeter Xu     vtd_iommu_unlock(s);
22111b2b1237SJason Wang     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);
2212b5a280c0SLe Tan }
2213b5a280c0SLe Tan 
22141da12ec4SLe Tan /* Flush IOTLB
22151da12ec4SLe Tan  * Returns the IOTLB Actual Invalidation Granularity.
22161da12ec4SLe Tan  * @val: the content of the IOTLB_REG
22171da12ec4SLe Tan  */
22181da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
22191da12ec4SLe Tan {
22201da12ec4SLe Tan     uint64_t iaig;
22211da12ec4SLe Tan     uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
2222b5a280c0SLe Tan     uint16_t domain_id;
2223b5a280c0SLe Tan     hwaddr addr;
2224b5a280c0SLe Tan     uint8_t am;
22251da12ec4SLe Tan 
22261da12ec4SLe Tan     switch (type) {
22271da12ec4SLe Tan     case VTD_TLB_GLOBAL_FLUSH:
22281da12ec4SLe Tan         iaig = VTD_TLB_GLOBAL_FLUSH_A;
2229b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
22301da12ec4SLe Tan         break;
22311da12ec4SLe Tan 
22321da12ec4SLe Tan     case VTD_TLB_DSI_FLUSH:
2233b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
22341da12ec4SLe Tan         iaig = VTD_TLB_DSI_FLUSH_A;
2235b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
22361da12ec4SLe Tan         break;
22371da12ec4SLe Tan 
22381da12ec4SLe Tan     case VTD_TLB_PSI_FLUSH:
2239b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
2240b5a280c0SLe Tan         addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
2241b5a280c0SLe Tan         am = VTD_IVA_AM(addr);
2242b5a280c0SLe Tan         addr = VTD_IVA_ADDR(addr);
2243b5a280c0SLe Tan         if (am > VTD_MAMV) {
22441376211fSPeter Xu             error_report_once("%s: address mask overflow: 0x%" PRIx64,
22451376211fSPeter Xu                               __func__, vtd_get_quad_raw(s, DMAR_IVA_REG));
2246b5a280c0SLe Tan             iaig = 0;
2247b5a280c0SLe Tan             break;
2248b5a280c0SLe Tan         }
22491da12ec4SLe Tan         iaig = VTD_TLB_PSI_FLUSH_A;
2250b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
22511da12ec4SLe Tan         break;
22521da12ec4SLe Tan 
22531da12ec4SLe Tan     default:
22541376211fSPeter Xu         error_report_once("%s: invalid granularity: 0x%" PRIx64,
22551376211fSPeter Xu                           __func__, val);
22561da12ec4SLe Tan         iaig = 0;
22571da12ec4SLe Tan     }
22581da12ec4SLe Tan     return iaig;
22591da12ec4SLe Tan }
22601da12ec4SLe Tan 
22618991c460SLadi Prosek static void vtd_fetch_inv_desc(IntelIOMMUState *s);
2262ed7b8fbcSLe Tan 
2263ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
2264ed7b8fbcSLe Tan {
2265ed7b8fbcSLe Tan     return s->qi_enabled && (s->iq_tail == s->iq_head) &&
2266ed7b8fbcSLe Tan            (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
2267ed7b8fbcSLe Tan }
2268ed7b8fbcSLe Tan 
2269ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
2270ed7b8fbcSLe Tan {
2271ed7b8fbcSLe Tan     uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
2272ed7b8fbcSLe Tan 
22737feb51b7SPeter Xu     trace_vtd_inv_qi_enable(en);
22747feb51b7SPeter Xu 
2275ed7b8fbcSLe Tan     if (en) {
227637f51384SPrasad Singamsetty         s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
2277ed7b8fbcSLe Tan         /* 2^(x+8) entries */
2278c0c1d351SLiu, Yi L         s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8 - (s->iq_dw ? 1 : 0));
2279ed7b8fbcSLe Tan         s->qi_enabled = true;
22807feb51b7SPeter Xu         trace_vtd_inv_qi_setup(s->iq, s->iq_size);
2281ed7b8fbcSLe Tan         /* Ok - report back to driver */
2282ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
22838991c460SLadi Prosek 
22848991c460SLadi Prosek         if (s->iq_tail != 0) {
22858991c460SLadi Prosek             /*
22868991c460SLadi Prosek              * This is a spec violation but Windows guests are known to set up
22878991c460SLadi Prosek              * Queued Invalidation this way so we allow the write and process
22888991c460SLadi Prosek              * Invalidation Descriptors right away.
22898991c460SLadi Prosek              */
22908991c460SLadi Prosek             trace_vtd_warn_invalid_qi_tail(s->iq_tail);
22918991c460SLadi Prosek             if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
22928991c460SLadi Prosek                 vtd_fetch_inv_desc(s);
22938991c460SLadi Prosek             }
2294ed7b8fbcSLe Tan         }
2295ed7b8fbcSLe Tan     } else {
2296ed7b8fbcSLe Tan         if (vtd_queued_inv_disable_check(s)) {
2297ed7b8fbcSLe Tan             /* disable Queued Invalidation */
2298ed7b8fbcSLe Tan             vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
2299ed7b8fbcSLe Tan             s->iq_head = 0;
2300ed7b8fbcSLe Tan             s->qi_enabled = false;
2301ed7b8fbcSLe Tan             /* Ok - report back to driver */
2302ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
2303ed7b8fbcSLe Tan         } else {
23044e4abd11SPeter Xu             error_report_once("%s: detected improper state when disable QI "
23054e4abd11SPeter Xu                               "(head=0x%x, tail=0x%x, last_type=%d)",
23064e4abd11SPeter Xu                               __func__,
23074e4abd11SPeter Xu                               s->iq_head, s->iq_tail, s->iq_last_desc_type);
2308ed7b8fbcSLe Tan         }
2309ed7b8fbcSLe Tan     }
2310ed7b8fbcSLe Tan }
2311ed7b8fbcSLe Tan 
23121da12ec4SLe Tan /* Set Root Table Pointer */
23131da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
23141da12ec4SLe Tan {
23151da12ec4SLe Tan     vtd_root_table_setup(s);
23161da12ec4SLe Tan     /* Ok - report back to driver */
23171da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
23182cc9ddccSPeter Xu     vtd_reset_caches(s);
23192cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
23201da12ec4SLe Tan }
23211da12ec4SLe Tan 
2322a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */
2323a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
2324a5861439SPeter Xu {
2325a5861439SPeter Xu     vtd_interrupt_remap_table_setup(s);
2326a5861439SPeter Xu     /* Ok - report back to driver */
2327a5861439SPeter Xu     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
2328a5861439SPeter Xu }
2329a5861439SPeter Xu 
23301da12ec4SLe Tan /* Handle Translation Enable/Disable */
23311da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
23321da12ec4SLe Tan {
2333558e0024SPeter Xu     if (s->dmar_enabled == en) {
2334558e0024SPeter Xu         return;
2335558e0024SPeter Xu     }
2336558e0024SPeter Xu 
23377feb51b7SPeter Xu     trace_vtd_dmar_enable(en);
23381da12ec4SLe Tan 
23391da12ec4SLe Tan     if (en) {
23401da12ec4SLe Tan         s->dmar_enabled = true;
23411da12ec4SLe Tan         /* Ok - report back to driver */
23421da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
23431da12ec4SLe Tan     } else {
23441da12ec4SLe Tan         s->dmar_enabled = false;
23451da12ec4SLe Tan 
23461da12ec4SLe Tan         /* Clear the index of Fault Recording Register */
23471da12ec4SLe Tan         s->next_frcd_reg = 0;
23481da12ec4SLe Tan         /* Ok - report back to driver */
23491da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
23501da12ec4SLe Tan     }
2351558e0024SPeter Xu 
23522cc9ddccSPeter Xu     vtd_reset_caches(s);
23532cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
23541da12ec4SLe Tan }
23551da12ec4SLe Tan 
235680de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */
235780de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
235880de52baSPeter Xu {
23597feb51b7SPeter Xu     trace_vtd_ir_enable(en);
236080de52baSPeter Xu 
236180de52baSPeter Xu     if (en) {
236280de52baSPeter Xu         s->intr_enabled = true;
236380de52baSPeter Xu         /* Ok - report back to driver */
236480de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
236580de52baSPeter Xu     } else {
236680de52baSPeter Xu         s->intr_enabled = false;
236780de52baSPeter Xu         /* Ok - report back to driver */
236880de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
236980de52baSPeter Xu     }
237080de52baSPeter Xu }
237180de52baSPeter Xu 
23721da12ec4SLe Tan /* Handle write to Global Command Register */
23731da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s)
23741da12ec4SLe Tan {
2375175f3a59SDavid Woodhouse     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
23761da12ec4SLe Tan     uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
23771da12ec4SLe Tan     uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
23781da12ec4SLe Tan     uint32_t changed = status ^ val;
23791da12ec4SLe Tan 
23807feb51b7SPeter Xu     trace_vtd_reg_write_gcmd(status, val);
23818646d9c7SDavid Woodhouse     if ((changed & VTD_GCMD_TE) && s->dma_translation) {
23821da12ec4SLe Tan         /* Translation enable/disable */
23831da12ec4SLe Tan         vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
23841da12ec4SLe Tan     }
23851da12ec4SLe Tan     if (val & VTD_GCMD_SRTP) {
23861da12ec4SLe Tan         /* Set/update the root-table pointer */
23871da12ec4SLe Tan         vtd_handle_gcmd_srtp(s);
23881da12ec4SLe Tan     }
2389ed7b8fbcSLe Tan     if (changed & VTD_GCMD_QIE) {
2390ed7b8fbcSLe Tan         /* Queued Invalidation Enable */
2391ed7b8fbcSLe Tan         vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
2392ed7b8fbcSLe Tan     }
2393a5861439SPeter Xu     if (val & VTD_GCMD_SIRTP) {
2394a5861439SPeter Xu         /* Set/update the interrupt remapping root-table pointer */
2395a5861439SPeter Xu         vtd_handle_gcmd_sirtp(s);
2396a5861439SPeter Xu     }
2397175f3a59SDavid Woodhouse     if ((changed & VTD_GCMD_IRE) &&
2398175f3a59SDavid Woodhouse         x86_iommu_ir_supported(x86_iommu)) {
239980de52baSPeter Xu         /* Interrupt remap enable/disable */
240080de52baSPeter Xu         vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
240180de52baSPeter Xu     }
24021da12ec4SLe Tan }
24031da12ec4SLe Tan 
24041da12ec4SLe Tan /* Handle write to Context Command Register */
24051da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s)
24061da12ec4SLe Tan {
24071da12ec4SLe Tan     uint64_t ret;
24081da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
24091da12ec4SLe Tan 
24101da12ec4SLe Tan     /* Context-cache invalidation request */
24111da12ec4SLe Tan     if (val & VTD_CCMD_ICC) {
2412ed7b8fbcSLe Tan         if (s->qi_enabled) {
24131376211fSPeter Xu             error_report_once("Queued Invalidation enabled, "
2414ed7b8fbcSLe Tan                               "should not use register-based invalidation");
2415ed7b8fbcSLe Tan             return;
2416ed7b8fbcSLe Tan         }
24171da12ec4SLe Tan         ret = vtd_context_cache_invalidate(s, val);
24181da12ec4SLe Tan         /* Invalidation completed. Change something to show */
24191da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
24201da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
24211da12ec4SLe Tan                                       ret);
24221da12ec4SLe Tan     }
24231da12ec4SLe Tan }
24241da12ec4SLe Tan 
24251da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */
24261da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s)
24271da12ec4SLe Tan {
24281da12ec4SLe Tan     uint64_t ret;
24291da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
24301da12ec4SLe Tan 
24311da12ec4SLe Tan     /* IOTLB invalidation request */
24321da12ec4SLe Tan     if (val & VTD_TLB_IVT) {
2433ed7b8fbcSLe Tan         if (s->qi_enabled) {
24341376211fSPeter Xu             error_report_once("Queued Invalidation enabled, "
24351376211fSPeter Xu                               "should not use register-based invalidation");
2436ed7b8fbcSLe Tan             return;
2437ed7b8fbcSLe Tan         }
24381da12ec4SLe Tan         ret = vtd_iotlb_flush(s, val);
24391da12ec4SLe Tan         /* Invalidation completed. Change something to show */
24401da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
24411da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
24421da12ec4SLe Tan                                       VTD_TLB_FLUSH_GRANU_MASK_A, ret);
24431da12ec4SLe Tan     }
24441da12ec4SLe Tan }
24451da12ec4SLe Tan 
2446ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */
2447c0c1d351SLiu, Yi L static bool vtd_get_inv_desc(IntelIOMMUState *s,
2448ed7b8fbcSLe Tan                              VTDInvDesc *inv_desc)
2449ed7b8fbcSLe Tan {
2450c0c1d351SLiu, Yi L     dma_addr_t base_addr = s->iq;
2451c0c1d351SLiu, Yi L     uint32_t offset = s->iq_head;
2452c0c1d351SLiu, Yi L     uint32_t dw = s->iq_dw ? 32 : 16;
2453c0c1d351SLiu, Yi L     dma_addr_t addr = base_addr + offset * dw;
2454c0c1d351SLiu, Yi L 
2455ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
2456ba06fe8aSPhilippe Mathieu-Daudé                         inv_desc, dw, MEMTXATTRS_UNSPECIFIED)) {
2457c0c1d351SLiu, Yi L         error_report_once("Read INV DESC failed.");
2458ed7b8fbcSLe Tan         return false;
2459ed7b8fbcSLe Tan     }
2460ed7b8fbcSLe Tan     inv_desc->lo = le64_to_cpu(inv_desc->lo);
2461ed7b8fbcSLe Tan     inv_desc->hi = le64_to_cpu(inv_desc->hi);
2462c0c1d351SLiu, Yi L     if (dw == 32) {
2463c0c1d351SLiu, Yi L         inv_desc->val[2] = le64_to_cpu(inv_desc->val[2]);
2464c0c1d351SLiu, Yi L         inv_desc->val[3] = le64_to_cpu(inv_desc->val[3]);
2465c0c1d351SLiu, Yi L     }
2466ed7b8fbcSLe Tan     return true;
2467ed7b8fbcSLe Tan }
2468ed7b8fbcSLe Tan 
2469ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2470ed7b8fbcSLe Tan {
2471ed7b8fbcSLe Tan     if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
2472ed7b8fbcSLe Tan         (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
2473095955b2SPeter Xu         error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2474095955b2SPeter Xu                           " (reserved nonzero)", __func__, inv_desc->hi,
2475095955b2SPeter Xu                           inv_desc->lo);
2476ed7b8fbcSLe Tan         return false;
2477ed7b8fbcSLe Tan     }
2478ed7b8fbcSLe Tan     if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
2479ed7b8fbcSLe Tan         /* Status Write */
2480ed7b8fbcSLe Tan         uint32_t status_data = (uint32_t)(inv_desc->lo >>
2481ed7b8fbcSLe Tan                                VTD_INV_DESC_WAIT_DATA_SHIFT);
2482ed7b8fbcSLe Tan 
2483ed7b8fbcSLe Tan         assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
2484ed7b8fbcSLe Tan 
2485ed7b8fbcSLe Tan         /* FIXME: need to be masked with HAW? */
2486ed7b8fbcSLe Tan         dma_addr_t status_addr = inv_desc->hi;
2487bc535e59SPeter Xu         trace_vtd_inv_desc_wait_sw(status_addr, status_data);
2488ed7b8fbcSLe Tan         status_data = cpu_to_le32(status_data);
2489ba06fe8aSPhilippe Mathieu-Daudé         if (dma_memory_write(&address_space_memory, status_addr,
2490ba06fe8aSPhilippe Mathieu-Daudé                              &status_data, sizeof(status_data),
2491ba06fe8aSPhilippe Mathieu-Daudé                              MEMTXATTRS_UNSPECIFIED)) {
2492bc535e59SPeter Xu             trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
2493ed7b8fbcSLe Tan             return false;
2494ed7b8fbcSLe Tan         }
2495ed7b8fbcSLe Tan     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
2496ed7b8fbcSLe Tan         /* Interrupt flag */
2497ed7b8fbcSLe Tan         vtd_generate_completion_event(s);
2498ed7b8fbcSLe Tan     } else {
2499095955b2SPeter Xu         error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2500095955b2SPeter Xu                           " (unknown type)", __func__, inv_desc->hi,
2501095955b2SPeter Xu                           inv_desc->lo);
2502ed7b8fbcSLe Tan         return false;
2503ed7b8fbcSLe Tan     }
2504ed7b8fbcSLe Tan     return true;
2505ed7b8fbcSLe Tan }
2506ed7b8fbcSLe Tan 
2507d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
2508d92fa2dcSLe Tan                                            VTDInvDesc *inv_desc)
2509d92fa2dcSLe Tan {
2510bc535e59SPeter Xu     uint16_t sid, fmask;
2511bc535e59SPeter Xu 
2512d92fa2dcSLe Tan     if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
2513095955b2SPeter Xu         error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2514095955b2SPeter Xu                           " (reserved nonzero)", __func__, inv_desc->hi,
2515095955b2SPeter Xu                           inv_desc->lo);
2516d92fa2dcSLe Tan         return false;
2517d92fa2dcSLe Tan     }
2518d92fa2dcSLe Tan     switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
2519d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DOMAIN:
2520bc535e59SPeter Xu         trace_vtd_inv_desc_cc_domain(
2521d92fa2dcSLe Tan             (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
2522d92fa2dcSLe Tan         /* Fall through */
2523d92fa2dcSLe Tan     case VTD_INV_DESC_CC_GLOBAL:
2524d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
2525d92fa2dcSLe Tan         break;
2526d92fa2dcSLe Tan 
2527d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DEVICE:
2528bc535e59SPeter Xu         sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
2529bc535e59SPeter Xu         fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
2530bc535e59SPeter Xu         vtd_context_device_invalidate(s, sid, fmask);
2531d92fa2dcSLe Tan         break;
2532d92fa2dcSLe Tan 
2533d92fa2dcSLe Tan     default:
2534095955b2SPeter Xu         error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2535095955b2SPeter Xu                           " (invalid type)", __func__, inv_desc->hi,
2536095955b2SPeter Xu                           inv_desc->lo);
2537d92fa2dcSLe Tan         return false;
2538d92fa2dcSLe Tan     }
2539d92fa2dcSLe Tan     return true;
2540d92fa2dcSLe Tan }
2541d92fa2dcSLe Tan 
2542b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2543b5a280c0SLe Tan {
2544b5a280c0SLe Tan     uint16_t domain_id;
2545b5a280c0SLe Tan     uint8_t am;
2546b5a280c0SLe Tan     hwaddr addr;
2547b5a280c0SLe Tan 
2548b5a280c0SLe Tan     if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
2549b5a280c0SLe Tan         (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
2550095955b2SPeter Xu         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2551ff5b5d5bSMarkus Armbruster                           ", lo=0x%"PRIx64" (reserved bits unzero)",
2552095955b2SPeter Xu                           __func__, inv_desc->hi, inv_desc->lo);
2553b5a280c0SLe Tan         return false;
2554b5a280c0SLe Tan     }
2555b5a280c0SLe Tan 
2556b5a280c0SLe Tan     switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
2557b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_GLOBAL:
2558b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
2559b5a280c0SLe Tan         break;
2560b5a280c0SLe Tan 
2561b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_DOMAIN:
2562b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
2563b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
2564b5a280c0SLe Tan         break;
2565b5a280c0SLe Tan 
2566b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_PAGE:
2567b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
2568b5a280c0SLe Tan         addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
2569b5a280c0SLe Tan         am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
2570b5a280c0SLe Tan         if (am > VTD_MAMV) {
2571095955b2SPeter Xu             error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2572ff5b5d5bSMarkus Armbruster                               ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)",
2573095955b2SPeter Xu                               __func__, inv_desc->hi, inv_desc->lo,
2574095955b2SPeter Xu                               am, (unsigned)VTD_MAMV);
2575b5a280c0SLe Tan             return false;
2576b5a280c0SLe Tan         }
2577b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
2578b5a280c0SLe Tan         break;
2579b5a280c0SLe Tan 
2580b5a280c0SLe Tan     default:
2581095955b2SPeter Xu         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2582ff5b5d5bSMarkus Armbruster                           ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
2583095955b2SPeter Xu                           __func__, inv_desc->hi, inv_desc->lo,
2584095955b2SPeter Xu                           inv_desc->lo & VTD_INV_DESC_IOTLB_G);
2585b5a280c0SLe Tan         return false;
2586b5a280c0SLe Tan     }
2587b5a280c0SLe Tan     return true;
2588b5a280c0SLe Tan }
2589b5a280c0SLe Tan 
259002a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
259102a2cbc8SPeter Xu                                      VTDInvDesc *inv_desc)
259202a2cbc8SPeter Xu {
25937feb51b7SPeter Xu     trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
259402a2cbc8SPeter Xu                            inv_desc->iec.index,
259502a2cbc8SPeter Xu                            inv_desc->iec.index_mask);
259602a2cbc8SPeter Xu 
259702a2cbc8SPeter Xu     vtd_iec_notify_all(s, !inv_desc->iec.granularity,
259802a2cbc8SPeter Xu                        inv_desc->iec.index,
259902a2cbc8SPeter Xu                        inv_desc->iec.index_mask);
2600554f5e16SJason Wang     return true;
2601554f5e16SJason Wang }
260202a2cbc8SPeter Xu 
2603554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
2604554f5e16SJason Wang                                           VTDInvDesc *inv_desc)
2605554f5e16SJason Wang {
2606554f5e16SJason Wang     VTDAddressSpace *vtd_dev_as;
26075039caf3SEugenio Pérez     IOMMUTLBEvent event;
2608554f5e16SJason Wang     hwaddr addr;
2609554f5e16SJason Wang     uint64_t sz;
2610554f5e16SJason Wang     uint16_t sid;
2611554f5e16SJason Wang     bool size;
2612554f5e16SJason Wang 
2613554f5e16SJason Wang     addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
2614554f5e16SJason Wang     sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
2615554f5e16SJason Wang     size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
2616554f5e16SJason Wang 
2617554f5e16SJason Wang     if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
2618554f5e16SJason Wang         (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
2619095955b2SPeter Xu         error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64
2620095955b2SPeter Xu                           ", lo=%"PRIx64" (reserved nonzero)", __func__,
2621095955b2SPeter Xu                           inv_desc->hi, inv_desc->lo);
2622554f5e16SJason Wang         return false;
2623554f5e16SJason Wang     }
2624554f5e16SJason Wang 
2625da8d439cSJason Wang     /*
2626da8d439cSJason Wang      * Using sid is OK since the guest should have finished the
2627da8d439cSJason Wang      * initialization of both the bus and device.
2628da8d439cSJason Wang      */
2629da8d439cSJason Wang     vtd_dev_as = vtd_get_as_by_sid(s, sid);
2630554f5e16SJason Wang     if (!vtd_dev_as) {
2631554f5e16SJason Wang         goto done;
2632554f5e16SJason Wang     }
2633554f5e16SJason Wang 
263404eb6247SJason Wang     /* According to ATS spec table 2.4:
263504eb6247SJason Wang      * S = 0, bits 15:12 = xxxx     range size: 4K
263604eb6247SJason Wang      * S = 1, bits 15:12 = xxx0     range size: 8K
263704eb6247SJason Wang      * S = 1, bits 15:12 = xx01     range size: 16K
263804eb6247SJason Wang      * S = 1, bits 15:12 = x011     range size: 32K
263904eb6247SJason Wang      * S = 1, bits 15:12 = 0111     range size: 64K
264004eb6247SJason Wang      * ...
264104eb6247SJason Wang      */
2642554f5e16SJason Wang     if (size) {
264304eb6247SJason Wang         sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
2644554f5e16SJason Wang         addr &= ~(sz - 1);
2645554f5e16SJason Wang     } else {
2646554f5e16SJason Wang         sz = VTD_PAGE_SIZE;
2647554f5e16SJason Wang     }
2648554f5e16SJason Wang 
2649b68ba1caSEugenio Pérez     event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
26505039caf3SEugenio Pérez     event.entry.target_as = &vtd_dev_as->as;
26515039caf3SEugenio Pérez     event.entry.addr_mask = sz - 1;
26525039caf3SEugenio Pérez     event.entry.iova = addr;
26535039caf3SEugenio Pérez     event.entry.perm = IOMMU_NONE;
26545039caf3SEugenio Pérez     event.entry.translated_addr = 0;
26555039caf3SEugenio Pérez     memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
2656554f5e16SJason Wang 
2657554f5e16SJason Wang done:
265802a2cbc8SPeter Xu     return true;
265902a2cbc8SPeter Xu }
266002a2cbc8SPeter Xu 
2661ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s)
2662ed7b8fbcSLe Tan {
2663ed7b8fbcSLe Tan     VTDInvDesc inv_desc;
2664ed7b8fbcSLe Tan     uint8_t desc_type;
2665ed7b8fbcSLe Tan 
26667feb51b7SPeter Xu     trace_vtd_inv_qi_head(s->iq_head);
2667c0c1d351SLiu, Yi L     if (!vtd_get_inv_desc(s, &inv_desc)) {
2668ed7b8fbcSLe Tan         s->iq_last_desc_type = VTD_INV_DESC_NONE;
2669ed7b8fbcSLe Tan         return false;
2670ed7b8fbcSLe Tan     }
2671c0c1d351SLiu, Yi L 
2672ed7b8fbcSLe Tan     desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
2673ed7b8fbcSLe Tan     /* FIXME: should update at first or at last? */
2674ed7b8fbcSLe Tan     s->iq_last_desc_type = desc_type;
2675ed7b8fbcSLe Tan 
2676ed7b8fbcSLe Tan     switch (desc_type) {
2677ed7b8fbcSLe Tan     case VTD_INV_DESC_CC:
2678bc535e59SPeter Xu         trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
2679d92fa2dcSLe Tan         if (!vtd_process_context_cache_desc(s, &inv_desc)) {
2680d92fa2dcSLe Tan             return false;
2681d92fa2dcSLe Tan         }
2682ed7b8fbcSLe Tan         break;
2683ed7b8fbcSLe Tan 
2684ed7b8fbcSLe Tan     case VTD_INV_DESC_IOTLB:
2685bc535e59SPeter Xu         trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
2686b5a280c0SLe Tan         if (!vtd_process_iotlb_desc(s, &inv_desc)) {
2687b5a280c0SLe Tan             return false;
2688b5a280c0SLe Tan         }
2689ed7b8fbcSLe Tan         break;
2690ed7b8fbcSLe Tan 
26914a4f219eSYi Sun     /*
26924a4f219eSYi Sun      * TODO: the entity of below two cases will be implemented in future series.
26934a4f219eSYi Sun      * To make guest (which integrates scalable mode support patch set in
26944a4f219eSYi Sun      * iommu driver) work, just return true is enough so far.
26954a4f219eSYi Sun      */
26964a4f219eSYi Sun     case VTD_INV_DESC_PC:
26974a4f219eSYi Sun         break;
26984a4f219eSYi Sun 
26994a4f219eSYi Sun     case VTD_INV_DESC_PIOTLB:
27004a4f219eSYi Sun         break;
27014a4f219eSYi Sun 
2702ed7b8fbcSLe Tan     case VTD_INV_DESC_WAIT:
2703bc535e59SPeter Xu         trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
2704ed7b8fbcSLe Tan         if (!vtd_process_wait_desc(s, &inv_desc)) {
2705ed7b8fbcSLe Tan             return false;
2706ed7b8fbcSLe Tan         }
2707ed7b8fbcSLe Tan         break;
2708ed7b8fbcSLe Tan 
2709b7910472SPeter Xu     case VTD_INV_DESC_IEC:
2710bc535e59SPeter Xu         trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
271102a2cbc8SPeter Xu         if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
271202a2cbc8SPeter Xu             return false;
271302a2cbc8SPeter Xu         }
2714b7910472SPeter Xu         break;
2715b7910472SPeter Xu 
2716554f5e16SJason Wang     case VTD_INV_DESC_DEVICE:
27177feb51b7SPeter Xu         trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
2718554f5e16SJason Wang         if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
2719554f5e16SJason Wang             return false;
2720554f5e16SJason Wang         }
2721554f5e16SJason Wang         break;
2722554f5e16SJason Wang 
2723ed7b8fbcSLe Tan     default:
2724095955b2SPeter Xu         error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64
2725095955b2SPeter Xu                           " (unknown type)", __func__, inv_desc.hi,
2726095955b2SPeter Xu                           inv_desc.lo);
2727ed7b8fbcSLe Tan         return false;
2728ed7b8fbcSLe Tan     }
2729ed7b8fbcSLe Tan     s->iq_head++;
2730ed7b8fbcSLe Tan     if (s->iq_head == s->iq_size) {
2731ed7b8fbcSLe Tan         s->iq_head = 0;
2732ed7b8fbcSLe Tan     }
2733ed7b8fbcSLe Tan     return true;
2734ed7b8fbcSLe Tan }
2735ed7b8fbcSLe Tan 
2736ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */
2737ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s)
2738ed7b8fbcSLe Tan {
2739a4544c45SLiu Yi L     int qi_shift;
2740a4544c45SLiu Yi L 
2741a4544c45SLiu Yi L     /* Refer to 10.4.23 of VT-d spec 3.0 */
2742a4544c45SLiu Yi L     qi_shift = s->iq_dw ? VTD_IQH_QH_SHIFT_5 : VTD_IQH_QH_SHIFT_4;
2743a4544c45SLiu Yi L 
27447feb51b7SPeter Xu     trace_vtd_inv_qi_fetch();
27457feb51b7SPeter Xu 
2746ed7b8fbcSLe Tan     if (s->iq_tail >= s->iq_size) {
2747ed7b8fbcSLe Tan         /* Detects an invalid Tail pointer */
27484e4abd11SPeter Xu         error_report_once("%s: detected invalid QI tail "
27494e4abd11SPeter Xu                           "(tail=0x%x, size=0x%x)",
27504e4abd11SPeter Xu                           __func__, s->iq_tail, s->iq_size);
2751ed7b8fbcSLe Tan         vtd_handle_inv_queue_error(s);
2752ed7b8fbcSLe Tan         return;
2753ed7b8fbcSLe Tan     }
2754ed7b8fbcSLe Tan     while (s->iq_head != s->iq_tail) {
2755ed7b8fbcSLe Tan         if (!vtd_process_inv_desc(s)) {
2756ed7b8fbcSLe Tan             /* Invalidation Queue Errors */
2757ed7b8fbcSLe Tan             vtd_handle_inv_queue_error(s);
2758ed7b8fbcSLe Tan             break;
2759ed7b8fbcSLe Tan         }
2760ed7b8fbcSLe Tan         /* Must update the IQH_REG in time */
2761ed7b8fbcSLe Tan         vtd_set_quad_raw(s, DMAR_IQH_REG,
2762a4544c45SLiu Yi L                          (((uint64_t)(s->iq_head)) << qi_shift) &
2763ed7b8fbcSLe Tan                          VTD_IQH_QH_MASK);
2764ed7b8fbcSLe Tan     }
2765ed7b8fbcSLe Tan }
2766ed7b8fbcSLe Tan 
2767ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */
2768ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s)
2769ed7b8fbcSLe Tan {
2770ed7b8fbcSLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
2771ed7b8fbcSLe Tan 
2772c0c1d351SLiu, Yi L     if (s->iq_dw && (val & VTD_IQT_QT_256_RSV_BIT)) {
2773c0c1d351SLiu, Yi L         error_report_once("%s: RSV bit is set: val=0x%"PRIx64,
2774c0c1d351SLiu, Yi L                           __func__, val);
2775c0c1d351SLiu, Yi L         return;
2776c0c1d351SLiu, Yi L     }
2777c0c1d351SLiu, Yi L     s->iq_tail = VTD_IQT_QT(s->iq_dw, val);
27787feb51b7SPeter Xu     trace_vtd_inv_qi_tail(s->iq_tail);
27797feb51b7SPeter Xu 
2780ed7b8fbcSLe Tan     if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
2781ed7b8fbcSLe Tan         /* Process Invalidation Queue here */
2782ed7b8fbcSLe Tan         vtd_fetch_inv_desc(s);
2783ed7b8fbcSLe Tan     }
2784ed7b8fbcSLe Tan }
2785ed7b8fbcSLe Tan 
27861da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s)
27871da12ec4SLe Tan {
27881da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
27891da12ec4SLe Tan     uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
27901da12ec4SLe Tan     uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
27911da12ec4SLe Tan 
27921da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
27931da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
27947feb51b7SPeter Xu         trace_vtd_fsts_clear_ip();
27951da12ec4SLe Tan     }
2796ed7b8fbcSLe Tan     /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
2797ed7b8fbcSLe Tan      * Descriptors if there are any when Queued Invalidation is enabled?
2798ed7b8fbcSLe Tan      */
27991da12ec4SLe Tan }
28001da12ec4SLe Tan 
28011da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s)
28021da12ec4SLe Tan {
28031da12ec4SLe Tan     uint32_t fectl_reg;
28041da12ec4SLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
28051da12ec4SLe Tan      * need to compare the old value and the new value to conclude that
28061da12ec4SLe Tan      * software clears the IM field? Or just check if the IM field is zero?
28071da12ec4SLe Tan      */
28081da12ec4SLe Tan     fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
28097feb51b7SPeter Xu 
28107feb51b7SPeter Xu     trace_vtd_reg_write_fectl(fectl_reg);
28117feb51b7SPeter Xu 
28121da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
28131da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
28141da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
28151da12ec4SLe Tan     }
28161da12ec4SLe Tan }
28171da12ec4SLe Tan 
2818ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s)
2819ed7b8fbcSLe Tan {
2820ed7b8fbcSLe Tan     uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
2821ed7b8fbcSLe Tan     uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
2822ed7b8fbcSLe Tan 
2823ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
28247feb51b7SPeter Xu         trace_vtd_reg_ics_clear_ip();
2825ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
2826ed7b8fbcSLe Tan     }
2827ed7b8fbcSLe Tan }
2828ed7b8fbcSLe Tan 
2829ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s)
2830ed7b8fbcSLe Tan {
2831ed7b8fbcSLe Tan     uint32_t iectl_reg;
2832ed7b8fbcSLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
2833ed7b8fbcSLe Tan      * need to compare the old value and the new value to conclude that
2834ed7b8fbcSLe Tan      * software clears the IM field? Or just check if the IM field is zero?
2835ed7b8fbcSLe Tan      */
2836ed7b8fbcSLe Tan     iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
28377feb51b7SPeter Xu 
28387feb51b7SPeter Xu     trace_vtd_reg_write_iectl(iectl_reg);
28397feb51b7SPeter Xu 
2840ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
2841ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
2842ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
2843ed7b8fbcSLe Tan     }
2844ed7b8fbcSLe Tan }
2845ed7b8fbcSLe Tan 
28461da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
28471da12ec4SLe Tan {
28481da12ec4SLe Tan     IntelIOMMUState *s = opaque;
28491da12ec4SLe Tan     uint64_t val;
28501da12ec4SLe Tan 
28517feb51b7SPeter Xu     trace_vtd_reg_read(addr, size);
28527feb51b7SPeter Xu 
28531da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
28541376211fSPeter Xu         error_report_once("%s: MMIO over range: addr=0x%" PRIx64
285573beb01eSPeter Xu                           " size=0x%x", __func__, addr, size);
28561da12ec4SLe Tan         return (uint64_t)-1;
28571da12ec4SLe Tan     }
28581da12ec4SLe Tan 
28591da12ec4SLe Tan     switch (addr) {
28601da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
28611da12ec4SLe Tan     case DMAR_RTADDR_REG:
28628fdee711SYi Sun         val = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
28631da12ec4SLe Tan         if (size == 4) {
28648fdee711SYi Sun             val = val & ((1ULL << 32) - 1);
28651da12ec4SLe Tan         }
28661da12ec4SLe Tan         break;
28671da12ec4SLe Tan 
28681da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
28691da12ec4SLe Tan         assert(size == 4);
28708fdee711SYi Sun         val = vtd_get_quad_raw(s, DMAR_RTADDR_REG) >> 32;
28711da12ec4SLe Tan         break;
28721da12ec4SLe Tan 
2873ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
2874ed7b8fbcSLe Tan     case DMAR_IQA_REG:
2875ed7b8fbcSLe Tan         val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2876ed7b8fbcSLe Tan         if (size == 4) {
2877ed7b8fbcSLe Tan             val = val & ((1ULL << 32) - 1);
2878ed7b8fbcSLe Tan         }
2879ed7b8fbcSLe Tan         break;
2880ed7b8fbcSLe Tan 
2881ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
2882ed7b8fbcSLe Tan         assert(size == 4);
2883ed7b8fbcSLe Tan         val = s->iq >> 32;
2884ed7b8fbcSLe Tan         break;
2885ed7b8fbcSLe Tan 
28861da12ec4SLe Tan     default:
28871da12ec4SLe Tan         if (size == 4) {
28881da12ec4SLe Tan             val = vtd_get_long(s, addr);
28891da12ec4SLe Tan         } else {
28901da12ec4SLe Tan             val = vtd_get_quad(s, addr);
28911da12ec4SLe Tan         }
28921da12ec4SLe Tan     }
28937feb51b7SPeter Xu 
28941da12ec4SLe Tan     return val;
28951da12ec4SLe Tan }
28961da12ec4SLe Tan 
28971da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr,
28981da12ec4SLe Tan                           uint64_t val, unsigned size)
28991da12ec4SLe Tan {
29001da12ec4SLe Tan     IntelIOMMUState *s = opaque;
29011da12ec4SLe Tan 
29027feb51b7SPeter Xu     trace_vtd_reg_write(addr, size, val);
29037feb51b7SPeter Xu 
29041da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
29051376211fSPeter Xu         error_report_once("%s: MMIO over range: addr=0x%" PRIx64
290673beb01eSPeter Xu                           " size=0x%x", __func__, addr, size);
29071da12ec4SLe Tan         return;
29081da12ec4SLe Tan     }
29091da12ec4SLe Tan 
29101da12ec4SLe Tan     switch (addr) {
29111da12ec4SLe Tan     /* Global Command Register, 32-bit */
29121da12ec4SLe Tan     case DMAR_GCMD_REG:
29131da12ec4SLe Tan         vtd_set_long(s, addr, val);
29141da12ec4SLe Tan         vtd_handle_gcmd_write(s);
29151da12ec4SLe Tan         break;
29161da12ec4SLe Tan 
29171da12ec4SLe Tan     /* Context Command Register, 64-bit */
29181da12ec4SLe Tan     case DMAR_CCMD_REG:
29191da12ec4SLe Tan         if (size == 4) {
29201da12ec4SLe Tan             vtd_set_long(s, addr, val);
29211da12ec4SLe Tan         } else {
29221da12ec4SLe Tan             vtd_set_quad(s, addr, val);
29231da12ec4SLe Tan             vtd_handle_ccmd_write(s);
29241da12ec4SLe Tan         }
29251da12ec4SLe Tan         break;
29261da12ec4SLe Tan 
29271da12ec4SLe Tan     case DMAR_CCMD_REG_HI:
29281da12ec4SLe Tan         assert(size == 4);
29291da12ec4SLe Tan         vtd_set_long(s, addr, val);
29301da12ec4SLe Tan         vtd_handle_ccmd_write(s);
29311da12ec4SLe Tan         break;
29321da12ec4SLe Tan 
29331da12ec4SLe Tan     /* IOTLB Invalidation Register, 64-bit */
29341da12ec4SLe Tan     case DMAR_IOTLB_REG:
29351da12ec4SLe Tan         if (size == 4) {
29361da12ec4SLe Tan             vtd_set_long(s, addr, val);
29371da12ec4SLe Tan         } else {
29381da12ec4SLe Tan             vtd_set_quad(s, addr, val);
29391da12ec4SLe Tan             vtd_handle_iotlb_write(s);
29401da12ec4SLe Tan         }
29411da12ec4SLe Tan         break;
29421da12ec4SLe Tan 
29431da12ec4SLe Tan     case DMAR_IOTLB_REG_HI:
29441da12ec4SLe Tan         assert(size == 4);
29451da12ec4SLe Tan         vtd_set_long(s, addr, val);
29461da12ec4SLe Tan         vtd_handle_iotlb_write(s);
29471da12ec4SLe Tan         break;
29481da12ec4SLe Tan 
2949b5a280c0SLe Tan     /* Invalidate Address Register, 64-bit */
2950b5a280c0SLe Tan     case DMAR_IVA_REG:
2951b5a280c0SLe Tan         if (size == 4) {
2952b5a280c0SLe Tan             vtd_set_long(s, addr, val);
2953b5a280c0SLe Tan         } else {
2954b5a280c0SLe Tan             vtd_set_quad(s, addr, val);
2955b5a280c0SLe Tan         }
2956b5a280c0SLe Tan         break;
2957b5a280c0SLe Tan 
2958b5a280c0SLe Tan     case DMAR_IVA_REG_HI:
2959b5a280c0SLe Tan         assert(size == 4);
2960b5a280c0SLe Tan         vtd_set_long(s, addr, val);
2961b5a280c0SLe Tan         break;
2962b5a280c0SLe Tan 
29631da12ec4SLe Tan     /* Fault Status Register, 32-bit */
29641da12ec4SLe Tan     case DMAR_FSTS_REG:
29651da12ec4SLe Tan         assert(size == 4);
29661da12ec4SLe Tan         vtd_set_long(s, addr, val);
29671da12ec4SLe Tan         vtd_handle_fsts_write(s);
29681da12ec4SLe Tan         break;
29691da12ec4SLe Tan 
29701da12ec4SLe Tan     /* Fault Event Control Register, 32-bit */
29711da12ec4SLe Tan     case DMAR_FECTL_REG:
29721da12ec4SLe Tan         assert(size == 4);
29731da12ec4SLe Tan         vtd_set_long(s, addr, val);
29741da12ec4SLe Tan         vtd_handle_fectl_write(s);
29751da12ec4SLe Tan         break;
29761da12ec4SLe Tan 
29771da12ec4SLe Tan     /* Fault Event Data Register, 32-bit */
29781da12ec4SLe Tan     case DMAR_FEDATA_REG:
29791da12ec4SLe Tan         assert(size == 4);
29801da12ec4SLe Tan         vtd_set_long(s, addr, val);
29811da12ec4SLe Tan         break;
29821da12ec4SLe Tan 
29831da12ec4SLe Tan     /* Fault Event Address Register, 32-bit */
29841da12ec4SLe Tan     case DMAR_FEADDR_REG:
2985b7a7bb35SJan Kiszka         if (size == 4) {
29861da12ec4SLe Tan             vtd_set_long(s, addr, val);
2987b7a7bb35SJan Kiszka         } else {
2988b7a7bb35SJan Kiszka             /*
2989b7a7bb35SJan Kiszka              * While the register is 32-bit only, some guests (Xen...) write to
2990b7a7bb35SJan Kiszka              * it with 64-bit.
2991b7a7bb35SJan Kiszka              */
2992b7a7bb35SJan Kiszka             vtd_set_quad(s, addr, val);
2993b7a7bb35SJan Kiszka         }
29941da12ec4SLe Tan         break;
29951da12ec4SLe Tan 
29961da12ec4SLe Tan     /* Fault Event Upper Address Register, 32-bit */
29971da12ec4SLe Tan     case DMAR_FEUADDR_REG:
29981da12ec4SLe Tan         assert(size == 4);
29991da12ec4SLe Tan         vtd_set_long(s, addr, val);
30001da12ec4SLe Tan         break;
30011da12ec4SLe Tan 
30021da12ec4SLe Tan     /* Protected Memory Enable Register, 32-bit */
30031da12ec4SLe Tan     case DMAR_PMEN_REG:
30041da12ec4SLe Tan         assert(size == 4);
30051da12ec4SLe Tan         vtd_set_long(s, addr, val);
30061da12ec4SLe Tan         break;
30071da12ec4SLe Tan 
30081da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
30091da12ec4SLe Tan     case DMAR_RTADDR_REG:
30101da12ec4SLe Tan         if (size == 4) {
30111da12ec4SLe Tan             vtd_set_long(s, addr, val);
30121da12ec4SLe Tan         } else {
30131da12ec4SLe Tan             vtd_set_quad(s, addr, val);
30141da12ec4SLe Tan         }
30151da12ec4SLe Tan         break;
30161da12ec4SLe Tan 
30171da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
30181da12ec4SLe Tan         assert(size == 4);
30191da12ec4SLe Tan         vtd_set_long(s, addr, val);
30201da12ec4SLe Tan         break;
30211da12ec4SLe Tan 
3022ed7b8fbcSLe Tan     /* Invalidation Queue Tail Register, 64-bit */
3023ed7b8fbcSLe Tan     case DMAR_IQT_REG:
3024ed7b8fbcSLe Tan         if (size == 4) {
3025ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
3026ed7b8fbcSLe Tan         } else {
3027ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
3028ed7b8fbcSLe Tan         }
3029ed7b8fbcSLe Tan         vtd_handle_iqt_write(s);
3030ed7b8fbcSLe Tan         break;
3031ed7b8fbcSLe Tan 
3032ed7b8fbcSLe Tan     case DMAR_IQT_REG_HI:
3033ed7b8fbcSLe Tan         assert(size == 4);
3034ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3035ed7b8fbcSLe Tan         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
3036ed7b8fbcSLe Tan         break;
3037ed7b8fbcSLe Tan 
3038ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
3039ed7b8fbcSLe Tan     case DMAR_IQA_REG:
3040ed7b8fbcSLe Tan         if (size == 4) {
3041ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
3042ed7b8fbcSLe Tan         } else {
3043ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
3044ed7b8fbcSLe Tan         }
3045147a372eSJason Wang         vtd_update_iq_dw(s);
3046ed7b8fbcSLe Tan         break;
3047ed7b8fbcSLe Tan 
3048ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
3049ed7b8fbcSLe Tan         assert(size == 4);
3050ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3051ed7b8fbcSLe Tan         break;
3052ed7b8fbcSLe Tan 
3053ed7b8fbcSLe Tan     /* Invalidation Completion Status Register, 32-bit */
3054ed7b8fbcSLe Tan     case DMAR_ICS_REG:
3055ed7b8fbcSLe Tan         assert(size == 4);
3056ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3057ed7b8fbcSLe Tan         vtd_handle_ics_write(s);
3058ed7b8fbcSLe Tan         break;
3059ed7b8fbcSLe Tan 
3060ed7b8fbcSLe Tan     /* Invalidation Event Control Register, 32-bit */
3061ed7b8fbcSLe Tan     case DMAR_IECTL_REG:
3062ed7b8fbcSLe Tan         assert(size == 4);
3063ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3064ed7b8fbcSLe Tan         vtd_handle_iectl_write(s);
3065ed7b8fbcSLe Tan         break;
3066ed7b8fbcSLe Tan 
3067ed7b8fbcSLe Tan     /* Invalidation Event Data Register, 32-bit */
3068ed7b8fbcSLe Tan     case DMAR_IEDATA_REG:
3069ed7b8fbcSLe Tan         assert(size == 4);
3070ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3071ed7b8fbcSLe Tan         break;
3072ed7b8fbcSLe Tan 
3073ed7b8fbcSLe Tan     /* Invalidation Event Address Register, 32-bit */
3074ed7b8fbcSLe Tan     case DMAR_IEADDR_REG:
3075ed7b8fbcSLe Tan         assert(size == 4);
3076ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3077ed7b8fbcSLe Tan         break;
3078ed7b8fbcSLe Tan 
3079ed7b8fbcSLe Tan     /* Invalidation Event Upper Address Register, 32-bit */
3080ed7b8fbcSLe Tan     case DMAR_IEUADDR_REG:
3081ed7b8fbcSLe Tan         assert(size == 4);
3082ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
3083ed7b8fbcSLe Tan         break;
3084ed7b8fbcSLe Tan 
30851da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
30861da12ec4SLe Tan     case DMAR_FRCD_REG_0_0:
30871da12ec4SLe Tan         if (size == 4) {
30881da12ec4SLe Tan             vtd_set_long(s, addr, val);
30891da12ec4SLe Tan         } else {
30901da12ec4SLe Tan             vtd_set_quad(s, addr, val);
30911da12ec4SLe Tan         }
30921da12ec4SLe Tan         break;
30931da12ec4SLe Tan 
30941da12ec4SLe Tan     case DMAR_FRCD_REG_0_1:
30951da12ec4SLe Tan         assert(size == 4);
30961da12ec4SLe Tan         vtd_set_long(s, addr, val);
30971da12ec4SLe Tan         break;
30981da12ec4SLe Tan 
30991da12ec4SLe Tan     case DMAR_FRCD_REG_0_2:
31001da12ec4SLe Tan         if (size == 4) {
31011da12ec4SLe Tan             vtd_set_long(s, addr, val);
31021da12ec4SLe Tan         } else {
31031da12ec4SLe Tan             vtd_set_quad(s, addr, val);
31041da12ec4SLe Tan             /* May clear bit 127 (Fault), update PPF */
31051da12ec4SLe Tan             vtd_update_fsts_ppf(s);
31061da12ec4SLe Tan         }
31071da12ec4SLe Tan         break;
31081da12ec4SLe Tan 
31091da12ec4SLe Tan     case DMAR_FRCD_REG_0_3:
31101da12ec4SLe Tan         assert(size == 4);
31111da12ec4SLe Tan         vtd_set_long(s, addr, val);
31121da12ec4SLe Tan         /* May clear bit 127 (Fault), update PPF */
31131da12ec4SLe Tan         vtd_update_fsts_ppf(s);
31141da12ec4SLe Tan         break;
31151da12ec4SLe Tan 
3116a5861439SPeter Xu     case DMAR_IRTA_REG:
3117a5861439SPeter Xu         if (size == 4) {
3118a5861439SPeter Xu             vtd_set_long(s, addr, val);
3119a5861439SPeter Xu         } else {
3120a5861439SPeter Xu             vtd_set_quad(s, addr, val);
3121a5861439SPeter Xu         }
3122a5861439SPeter Xu         break;
3123a5861439SPeter Xu 
3124a5861439SPeter Xu     case DMAR_IRTA_REG_HI:
3125a5861439SPeter Xu         assert(size == 4);
3126a5861439SPeter Xu         vtd_set_long(s, addr, val);
3127a5861439SPeter Xu         break;
3128a5861439SPeter Xu 
31291da12ec4SLe Tan     default:
31301da12ec4SLe Tan         if (size == 4) {
31311da12ec4SLe Tan             vtd_set_long(s, addr, val);
31321da12ec4SLe Tan         } else {
31331da12ec4SLe Tan             vtd_set_quad(s, addr, val);
31341da12ec4SLe Tan         }
31351da12ec4SLe Tan     }
31361da12ec4SLe Tan }
31371da12ec4SLe Tan 
31383df9d748SAlexey Kardashevskiy static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
31392c91bcf2SPeter Maydell                                          IOMMUAccessFlags flag, int iommu_idx)
31401da12ec4SLe Tan {
31411da12ec4SLe Tan     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
31421da12ec4SLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
3143b9313021SPeter Xu     IOMMUTLBEntry iotlb = {
3144b9313021SPeter Xu         /* We'll fill in the rest later. */
31451da12ec4SLe Tan         .target_as = &address_space_memory,
31461da12ec4SLe Tan     };
3147b9313021SPeter Xu     bool success;
31481da12ec4SLe Tan 
3149b9313021SPeter Xu     if (likely(s->dmar_enabled)) {
3150b9313021SPeter Xu         success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
3151b9313021SPeter Xu                                          addr, flag & IOMMU_WO, &iotlb);
3152b9313021SPeter Xu     } else {
31531da12ec4SLe Tan         /* DMAR disabled, passthrough, use 4k-page*/
3154b9313021SPeter Xu         iotlb.iova = addr & VTD_PAGE_MASK_4K;
3155b9313021SPeter Xu         iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
3156b9313021SPeter Xu         iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
3157b9313021SPeter Xu         iotlb.perm = IOMMU_RW;
3158b9313021SPeter Xu         success = true;
31591da12ec4SLe Tan     }
31601da12ec4SLe Tan 
3161b9313021SPeter Xu     if (likely(success)) {
31627feb51b7SPeter Xu         trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
31637feb51b7SPeter Xu                                  VTD_PCI_SLOT(vtd_as->devfn),
31647feb51b7SPeter Xu                                  VTD_PCI_FUNC(vtd_as->devfn),
3165b9313021SPeter Xu                                  iotlb.iova, iotlb.translated_addr,
3166b9313021SPeter Xu                                  iotlb.addr_mask);
3167b9313021SPeter Xu     } else {
31684e4abd11SPeter Xu         error_report_once("%s: detected translation failure "
31694e4abd11SPeter Xu                           "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")",
31704e4abd11SPeter Xu                           __func__, pci_bus_num(vtd_as->bus),
3171b9313021SPeter Xu                           VTD_PCI_SLOT(vtd_as->devfn),
3172b9313021SPeter Xu                           VTD_PCI_FUNC(vtd_as->devfn),
3173662b4b69SPeter Xu                           addr);
3174b9313021SPeter Xu     }
31757feb51b7SPeter Xu 
3176b9313021SPeter Xu     return iotlb;
31771da12ec4SLe Tan }
31781da12ec4SLe Tan 
3179549d4005SEric Auger static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
31805bf3d319SPeter Xu                                          IOMMUNotifierFlag old,
3181549d4005SEric Auger                                          IOMMUNotifierFlag new,
3182549d4005SEric Auger                                          Error **errp)
31833cb3b154SAlex Williamson {
31843cb3b154SAlex Williamson     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
3185dd4d607eSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
318609adb0e0SJason Wang     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
31873cb3b154SAlex Williamson 
3188b8ffd7d6SJason Wang     /* TODO: add support for VFIO and vhost users */
3189b8ffd7d6SJason Wang     if (s->snoop_control) {
3190250227f4SJason Wang         error_setg_errno(errp, ENOTSUP,
3191b8ffd7d6SJason Wang                          "Snoop Control with vhost or VFIO is not supported");
3192b8ffd7d6SJason Wang         return -ENOTSUP;
3193b8ffd7d6SJason Wang     }
3194b8d78277SJason Wang     if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
3195b8d78277SJason Wang         error_setg_errno(errp, ENOTSUP,
3196b8d78277SJason Wang                          "device %02x.%02x.%x requires caching mode",
3197b8d78277SJason Wang                          pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
3198b8d78277SJason Wang                          PCI_FUNC(vtd_as->devfn));
3199b8d78277SJason Wang         return -ENOTSUP;
3200b8d78277SJason Wang     }
320109adb0e0SJason Wang     if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
320209adb0e0SJason Wang         error_setg_errno(errp, ENOTSUP,
320309adb0e0SJason Wang                          "device %02x.%02x.%x requires device IOTLB mode",
320409adb0e0SJason Wang                          pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
320509adb0e0SJason Wang                          PCI_FUNC(vtd_as->devfn));
320609adb0e0SJason Wang         return -ENOTSUP;
320709adb0e0SJason Wang     }
3208b8ffd7d6SJason Wang 
32094f8a62a9SPeter Xu     /* Update per-address-space notifier flags */
32104f8a62a9SPeter Xu     vtd_as->notifier_flags = new;
32114f8a62a9SPeter Xu 
3212dd4d607eSPeter Xu     if (old == IOMMU_NOTIFIER_NONE) {
3213b4a4ba0dSPeter Xu         QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
3214b4a4ba0dSPeter Xu     } else if (new == IOMMU_NOTIFIER_NONE) {
3215b4a4ba0dSPeter Xu         QLIST_REMOVE(vtd_as, next);
3216dd4d607eSPeter Xu     }
3217549d4005SEric Auger     return 0;
32183cb3b154SAlex Williamson }
32193cb3b154SAlex Williamson 
3220552a1e01SPeter Xu static int vtd_post_load(void *opaque, int version_id)
3221552a1e01SPeter Xu {
3222552a1e01SPeter Xu     IntelIOMMUState *iommu = opaque;
3223552a1e01SPeter Xu 
3224552a1e01SPeter Xu     /*
32252811af3bSPeter Xu      * We don't need to migrate the root_scalable because we can
32262811af3bSPeter Xu      * simply do the calculation after the loading is complete.  We
32272811af3bSPeter Xu      * can actually do similar things with root, dmar_enabled, etc.
32282811af3bSPeter Xu      * however since we've had them already so we'd better keep them
32292811af3bSPeter Xu      * for compatibility of migration.
32302811af3bSPeter Xu      */
32312811af3bSPeter Xu     vtd_update_scalable_state(iommu);
32322811af3bSPeter Xu 
3233147a372eSJason Wang     vtd_update_iq_dw(iommu);
3234147a372eSJason Wang 
3235ceb05895SJason Wang     /*
3236ceb05895SJason Wang      * Memory regions are dynamically turned on/off depending on
3237ceb05895SJason Wang      * context entry configurations from the guest. After migration,
3238ceb05895SJason Wang      * we need to make sure the memory regions are still correct.
3239ceb05895SJason Wang      */
3240ceb05895SJason Wang     vtd_switch_address_space_all(iommu);
3241ceb05895SJason Wang 
3242552a1e01SPeter Xu     return 0;
3243552a1e01SPeter Xu }
3244552a1e01SPeter Xu 
32451da12ec4SLe Tan static const VMStateDescription vtd_vmstate = {
32461da12ec4SLe Tan     .name = "iommu-intel",
32478cdcf3c1SPeter Xu     .version_id = 1,
32488cdcf3c1SPeter Xu     .minimum_version_id = 1,
32498cdcf3c1SPeter Xu     .priority = MIG_PRI_IOMMU,
3250552a1e01SPeter Xu     .post_load = vtd_post_load,
32518cdcf3c1SPeter Xu     .fields = (VMStateField[]) {
32528cdcf3c1SPeter Xu         VMSTATE_UINT64(root, IntelIOMMUState),
32538cdcf3c1SPeter Xu         VMSTATE_UINT64(intr_root, IntelIOMMUState),
32548cdcf3c1SPeter Xu         VMSTATE_UINT64(iq, IntelIOMMUState),
32558cdcf3c1SPeter Xu         VMSTATE_UINT32(intr_size, IntelIOMMUState),
32568cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_head, IntelIOMMUState),
32578cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_tail, IntelIOMMUState),
32588cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_size, IntelIOMMUState),
32598cdcf3c1SPeter Xu         VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
32608cdcf3c1SPeter Xu         VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
32618cdcf3c1SPeter Xu         VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
326281fb1e64SPeter Xu         VMSTATE_UNUSED(1),      /* bool root_extended is obsolete by VT-d */
32638cdcf3c1SPeter Xu         VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
32648cdcf3c1SPeter Xu         VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
32658cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
32668cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_eime, IntelIOMMUState),
32678cdcf3c1SPeter Xu         VMSTATE_END_OF_LIST()
32688cdcf3c1SPeter Xu     }
32691da12ec4SLe Tan };
32701da12ec4SLe Tan 
32711da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = {
32721da12ec4SLe Tan     .read = vtd_mem_read,
32731da12ec4SLe Tan     .write = vtd_mem_write,
32741da12ec4SLe Tan     .endianness = DEVICE_LITTLE_ENDIAN,
32751da12ec4SLe Tan     .impl = {
32761da12ec4SLe Tan         .min_access_size = 4,
32771da12ec4SLe Tan         .max_access_size = 8,
32781da12ec4SLe Tan     },
32791da12ec4SLe Tan     .valid = {
32801da12ec4SLe Tan         .min_access_size = 4,
32811da12ec4SLe Tan         .max_access_size = 8,
32821da12ec4SLe Tan     },
32831da12ec4SLe Tan };
32841da12ec4SLe Tan 
32851da12ec4SLe Tan static Property vtd_properties[] = {
32861da12ec4SLe Tan     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
3287e6b6af05SRadim Krčmář     DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
3288e6b6af05SRadim Krčmář                             ON_OFF_AUTO_AUTO),
3289fb506e70SRadim Krčmář     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
32904b49b586SPeter Xu     DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
329137f51384SPrasad Singamsetty                       VTD_HOST_ADDRESS_WIDTH),
32923b40f0e5SAviv Ben-David     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
32934a4f219eSYi Sun     DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
3294b8ffd7d6SJason Wang     DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
32951b2b1237SJason Wang     DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
3296ccc23bb0SPeter Xu     DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
32978646d9c7SDavid Woodhouse     DEFINE_PROP_BOOL("dma-translation", IntelIOMMUState, dma_translation, true),
32981da12ec4SLe Tan     DEFINE_PROP_END_OF_LIST(),
32991da12ec4SLe Tan };
33001da12ec4SLe Tan 
3301651e4cefSPeter Xu /* Read IRTE entry with specific index */
3302651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
3303bc38ee10SMichael S. Tsirkin                         VTD_IR_TableEntry *entry, uint16_t sid)
3304651e4cefSPeter Xu {
3305ede9c94aSPeter Xu     static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
3306ede9c94aSPeter Xu         {0xffff, 0xfffb, 0xfff9, 0xfff8};
3307651e4cefSPeter Xu     dma_addr_t addr = 0x00;
3308ede9c94aSPeter Xu     uint16_t mask, source_id;
3309ede9c94aSPeter Xu     uint8_t bus, bus_max, bus_min;
3310651e4cefSPeter Xu 
33113c507c26SJan Kiszka     if (index >= iommu->intr_size) {
33123c507c26SJan Kiszka         error_report_once("%s: index too large: ind=0x%x",
33133c507c26SJan Kiszka                           __func__, index);
33143c507c26SJan Kiszka         return -VTD_FR_IR_INDEX_OVER;
33153c507c26SJan Kiszka     }
33163c507c26SJan Kiszka 
3317651e4cefSPeter Xu     addr = iommu->intr_root + index * sizeof(*entry);
3318ba06fe8aSPhilippe Mathieu-Daudé     if (dma_memory_read(&address_space_memory, addr,
3319ba06fe8aSPhilippe Mathieu-Daudé                         entry, sizeof(*entry), MEMTXATTRS_UNSPECIFIED)) {
33201376211fSPeter Xu         error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64,
33211376211fSPeter Xu                           __func__, index, addr);
3322651e4cefSPeter Xu         return -VTD_FR_IR_ROOT_INVAL;
3323651e4cefSPeter Xu     }
3324651e4cefSPeter Xu 
33257feb51b7SPeter Xu     trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
33267feb51b7SPeter Xu                           le64_to_cpu(entry->data[0]));
33277feb51b7SPeter Xu 
3328bc38ee10SMichael S. Tsirkin     if (!entry->irte.present) {
33294e4abd11SPeter Xu         error_report_once("%s: detected non-present IRTE "
33304e4abd11SPeter Xu                           "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
33314e4abd11SPeter Xu                           __func__, index, le64_to_cpu(entry->data[1]),
3332651e4cefSPeter Xu                           le64_to_cpu(entry->data[0]));
3333651e4cefSPeter Xu         return -VTD_FR_IR_ENTRY_P;
3334651e4cefSPeter Xu     }
3335651e4cefSPeter Xu 
3336bc38ee10SMichael S. Tsirkin     if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
3337bc38ee10SMichael S. Tsirkin         entry->irte.__reserved_2) {
33384e4abd11SPeter Xu         error_report_once("%s: detected non-zero reserved IRTE "
33394e4abd11SPeter Xu                           "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
33404e4abd11SPeter Xu                           __func__, index, le64_to_cpu(entry->data[1]),
3341651e4cefSPeter Xu                           le64_to_cpu(entry->data[0]));
3342651e4cefSPeter Xu         return -VTD_FR_IR_IRTE_RSVD;
3343651e4cefSPeter Xu     }
3344651e4cefSPeter Xu 
3345ede9c94aSPeter Xu     if (sid != X86_IOMMU_SID_INVALID) {
3346ede9c94aSPeter Xu         /* Validate IRTE SID */
3347bc38ee10SMichael S. Tsirkin         source_id = le32_to_cpu(entry->irte.source_id);
3348bc38ee10SMichael S. Tsirkin         switch (entry->irte.sid_vtype) {
3349ede9c94aSPeter Xu         case VTD_SVT_NONE:
3350ede9c94aSPeter Xu             break;
3351ede9c94aSPeter Xu 
3352ede9c94aSPeter Xu         case VTD_SVT_ALL:
3353bc38ee10SMichael S. Tsirkin             mask = vtd_svt_mask[entry->irte.sid_q];
3354ede9c94aSPeter Xu             if ((source_id & mask) != (sid & mask)) {
33554e4abd11SPeter Xu                 error_report_once("%s: invalid IRTE SID "
33564e4abd11SPeter Xu                                   "(index=%u, sid=%u, source_id=%u)",
33574e4abd11SPeter Xu                                   __func__, index, sid, source_id);
3358ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
3359ede9c94aSPeter Xu             }
3360ede9c94aSPeter Xu             break;
3361ede9c94aSPeter Xu 
3362ede9c94aSPeter Xu         case VTD_SVT_BUS:
3363ede9c94aSPeter Xu             bus_max = source_id >> 8;
3364ede9c94aSPeter Xu             bus_min = source_id & 0xff;
3365ede9c94aSPeter Xu             bus = sid >> 8;
3366ede9c94aSPeter Xu             if (bus > bus_max || bus < bus_min) {
33674e4abd11SPeter Xu                 error_report_once("%s: invalid SVT_BUS "
33684e4abd11SPeter Xu                                   "(index=%u, bus=%u, min=%u, max=%u)",
33694e4abd11SPeter Xu                                   __func__, index, bus, bus_min, bus_max);
3370ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
3371ede9c94aSPeter Xu             }
3372ede9c94aSPeter Xu             break;
3373ede9c94aSPeter Xu 
3374ede9c94aSPeter Xu         default:
33754e4abd11SPeter Xu             error_report_once("%s: detected invalid IRTE SVT "
33764e4abd11SPeter Xu                               "(index=%u, type=%d)", __func__,
33774e4abd11SPeter Xu                               index, entry->irte.sid_vtype);
3378ede9c94aSPeter Xu             /* Take this as verification failure. */
3379ede9c94aSPeter Xu             return -VTD_FR_IR_SID_ERR;
3380ede9c94aSPeter Xu         }
3381ede9c94aSPeter Xu     }
3382651e4cefSPeter Xu 
3383651e4cefSPeter Xu     return 0;
3384651e4cefSPeter Xu }
3385651e4cefSPeter Xu 
3386651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */
3387ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
338835c24501SSingh, Brijesh                              X86IOMMUIrq *irq, uint16_t sid)
3389651e4cefSPeter Xu {
3390bc38ee10SMichael S. Tsirkin     VTD_IR_TableEntry irte = {};
3391651e4cefSPeter Xu     int ret = 0;
3392651e4cefSPeter Xu 
3393ede9c94aSPeter Xu     ret = vtd_irte_get(iommu, index, &irte, sid);
3394651e4cefSPeter Xu     if (ret) {
3395651e4cefSPeter Xu         return ret;
3396651e4cefSPeter Xu     }
3397651e4cefSPeter Xu 
3398bc38ee10SMichael S. Tsirkin     irq->trigger_mode = irte.irte.trigger_mode;
3399bc38ee10SMichael S. Tsirkin     irq->vector = irte.irte.vector;
3400bc38ee10SMichael S. Tsirkin     irq->delivery_mode = irte.irte.delivery_mode;
3401bc38ee10SMichael S. Tsirkin     irq->dest = le32_to_cpu(irte.irte.dest_id);
340228589311SJan Kiszka     if (!iommu->intr_eime) {
3403651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_MASK         (0xff00ULL)
3404651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_SHIFT        (8)
340528589311SJan Kiszka         irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
3406651e4cefSPeter Xu             VTD_IR_APIC_DEST_SHIFT;
340728589311SJan Kiszka     }
3408bc38ee10SMichael S. Tsirkin     irq->dest_mode = irte.irte.dest_mode;
3409bc38ee10SMichael S. Tsirkin     irq->redir_hint = irte.irte.redir_hint;
3410651e4cefSPeter Xu 
34117feb51b7SPeter Xu     trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
34127feb51b7SPeter Xu                        irq->delivery_mode, irq->dest, irq->dest_mode);
3413651e4cefSPeter Xu 
3414651e4cefSPeter Xu     return 0;
3415651e4cefSPeter Xu }
3416651e4cefSPeter Xu 
3417651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */
3418651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
3419651e4cefSPeter Xu                                    MSIMessage *origin,
3420ede9c94aSPeter Xu                                    MSIMessage *translated,
3421ede9c94aSPeter Xu                                    uint16_t sid)
3422651e4cefSPeter Xu {
3423651e4cefSPeter Xu     int ret = 0;
3424651e4cefSPeter Xu     VTD_IR_MSIAddress addr;
3425651e4cefSPeter Xu     uint16_t index;
342635c24501SSingh, Brijesh     X86IOMMUIrq irq = {};
3427651e4cefSPeter Xu 
3428651e4cefSPeter Xu     assert(origin && translated);
3429651e4cefSPeter Xu 
34307feb51b7SPeter Xu     trace_vtd_ir_remap_msi_req(origin->address, origin->data);
34317feb51b7SPeter Xu 
3432651e4cefSPeter Xu     if (!iommu || !iommu->intr_enabled) {
3433e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
3434e7a3b91fSPeter Xu         goto out;
3435651e4cefSPeter Xu     }
3436651e4cefSPeter Xu 
3437651e4cefSPeter Xu     if (origin->address & VTD_MSI_ADDR_HI_MASK) {
34381376211fSPeter Xu         error_report_once("%s: MSI address high 32 bits non-zero detected: "
34391376211fSPeter Xu                           "address=0x%" PRIx64, __func__, origin->address);
3440651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
3441651e4cefSPeter Xu     }
3442651e4cefSPeter Xu 
3443651e4cefSPeter Xu     addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
34441a43713bSPeter Xu     if (addr.addr.__head != 0xfee) {
34451376211fSPeter Xu         error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32,
34461376211fSPeter Xu                           __func__, addr.data);
3447651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
3448651e4cefSPeter Xu     }
3449651e4cefSPeter Xu 
3450651e4cefSPeter Xu     /* This is compatible mode. */
3451bc38ee10SMichael S. Tsirkin     if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
3452e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
3453e7a3b91fSPeter Xu         goto out;
3454651e4cefSPeter Xu     }
3455651e4cefSPeter Xu 
3456bc38ee10SMichael S. Tsirkin     index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
3457651e4cefSPeter Xu 
3458651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_SUBHANDLE       (0x0000ffff)
3459651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_RESERVED        (0xffff0000)
3460651e4cefSPeter Xu 
3461bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
3462651e4cefSPeter Xu         /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
3463651e4cefSPeter Xu         index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
3464651e4cefSPeter Xu     }
3465651e4cefSPeter Xu 
3466ede9c94aSPeter Xu     ret = vtd_remap_irq_get(iommu, index, &irq, sid);
3467651e4cefSPeter Xu     if (ret) {
3468651e4cefSPeter Xu         return ret;
3469651e4cefSPeter Xu     }
3470651e4cefSPeter Xu 
3471bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
34727feb51b7SPeter Xu         trace_vtd_ir_remap_type("MSI");
3473651e4cefSPeter Xu         if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
34744e4abd11SPeter Xu             error_report_once("%s: invalid IR MSI "
34754e4abd11SPeter Xu                               "(sid=%u, address=0x%" PRIx64
34764e4abd11SPeter Xu                               ", data=0x%" PRIx32 ")",
34774e4abd11SPeter Xu                               __func__, sid, origin->address, origin->data);
3478651e4cefSPeter Xu             return -VTD_FR_IR_REQ_RSVD;
3479651e4cefSPeter Xu         }
3480651e4cefSPeter Xu     } else {
3481651e4cefSPeter Xu         uint8_t vector = origin->data & 0xff;
3482dea651a9SFeng Wu         uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
3483dea651a9SFeng Wu 
34847feb51b7SPeter Xu         trace_vtd_ir_remap_type("IOAPIC");
3485651e4cefSPeter Xu         /* IOAPIC entry vector should be aligned with IRTE vector
3486651e4cefSPeter Xu          * (see vt-d spec 5.1.5.1). */
3487651e4cefSPeter Xu         if (vector != irq.vector) {
34887feb51b7SPeter Xu             trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
3489651e4cefSPeter Xu         }
3490dea651a9SFeng Wu 
3491dea651a9SFeng Wu         /* The Trigger Mode field must match the Trigger Mode in the IRTE.
3492dea651a9SFeng Wu          * (see vt-d spec 5.1.5.1). */
3493dea651a9SFeng Wu         if (trigger_mode != irq.trigger_mode) {
34947feb51b7SPeter Xu             trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
34957feb51b7SPeter Xu                                       irq.trigger_mode);
3496dea651a9SFeng Wu         }
3497651e4cefSPeter Xu     }
3498651e4cefSPeter Xu 
3499651e4cefSPeter Xu     /*
3500651e4cefSPeter Xu      * We'd better keep the last two bits, assuming that guest OS
3501651e4cefSPeter Xu      * might modify it. Keep it does not hurt after all.
3502651e4cefSPeter Xu      */
3503bc38ee10SMichael S. Tsirkin     irq.msi_addr_last_bits = addr.addr.__not_care;
3504651e4cefSPeter Xu 
350535c24501SSingh, Brijesh     /* Translate X86IOMMUIrq to MSI message */
350635c24501SSingh, Brijesh     x86_iommu_irq_to_msi_message(&irq, translated);
3507651e4cefSPeter Xu 
3508e7a3b91fSPeter Xu out:
35097feb51b7SPeter Xu     trace_vtd_ir_remap_msi(origin->address, origin->data,
3510651e4cefSPeter Xu                            translated->address, translated->data);
3511651e4cefSPeter Xu     return 0;
3512651e4cefSPeter Xu }
3513651e4cefSPeter Xu 
35148b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
35158b5ed7dfSPeter Xu                          MSIMessage *dst, uint16_t sid)
35168b5ed7dfSPeter Xu {
3517ede9c94aSPeter Xu     return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
3518ede9c94aSPeter Xu                                    src, dst, sid);
35198b5ed7dfSPeter Xu }
35208b5ed7dfSPeter Xu 
3521651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
3522651e4cefSPeter Xu                                    uint64_t *data, unsigned size,
3523651e4cefSPeter Xu                                    MemTxAttrs attrs)
3524651e4cefSPeter Xu {
3525651e4cefSPeter Xu     return MEMTX_OK;
3526651e4cefSPeter Xu }
3527651e4cefSPeter Xu 
3528651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
3529651e4cefSPeter Xu                                     uint64_t value, unsigned size,
3530651e4cefSPeter Xu                                     MemTxAttrs attrs)
3531651e4cefSPeter Xu {
3532651e4cefSPeter Xu     int ret = 0;
353309cd058aSMichael S. Tsirkin     MSIMessage from = {}, to = {};
3534ede9c94aSPeter Xu     uint16_t sid = X86_IOMMU_SID_INVALID;
3535651e4cefSPeter Xu 
3536651e4cefSPeter Xu     from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
3537651e4cefSPeter Xu     from.data = (uint32_t) value;
3538651e4cefSPeter Xu 
3539ede9c94aSPeter Xu     if (!attrs.unspecified) {
3540ede9c94aSPeter Xu         /* We have explicit Source ID */
3541ede9c94aSPeter Xu         sid = attrs.requester_id;
3542ede9c94aSPeter Xu     }
3543ede9c94aSPeter Xu 
3544ede9c94aSPeter Xu     ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
3545651e4cefSPeter Xu     if (ret) {
3546651e4cefSPeter Xu         /* TODO: report error */
3547651e4cefSPeter Xu         /* Drop this interrupt */
3548651e4cefSPeter Xu         return MEMTX_ERROR;
3549651e4cefSPeter Xu     }
3550651e4cefSPeter Xu 
3551eaaaf8abSPaolo Bonzini     apic_get_class(NULL)->send_msi(&to);
3552651e4cefSPeter Xu 
3553651e4cefSPeter Xu     return MEMTX_OK;
3554651e4cefSPeter Xu }
3555651e4cefSPeter Xu 
3556651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = {
3557651e4cefSPeter Xu     .read_with_attrs = vtd_mem_ir_read,
3558651e4cefSPeter Xu     .write_with_attrs = vtd_mem_ir_write,
3559651e4cefSPeter Xu     .endianness = DEVICE_LITTLE_ENDIAN,
3560651e4cefSPeter Xu     .impl = {
3561651e4cefSPeter Xu         .min_access_size = 4,
3562651e4cefSPeter Xu         .max_access_size = 4,
3563651e4cefSPeter Xu     },
3564651e4cefSPeter Xu     .valid = {
3565651e4cefSPeter Xu         .min_access_size = 4,
3566651e4cefSPeter Xu         .max_access_size = 4,
3567651e4cefSPeter Xu     },
3568651e4cefSPeter Xu };
35697df953bdSKnut Omang 
35701b2b1237SJason Wang static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
35711b2b1237SJason Wang                                          hwaddr addr, bool is_write)
35721b2b1237SJason Wang {
35731b2b1237SJason Wang     IntelIOMMUState *s = vtd_as->iommu_state;
35741b2b1237SJason Wang     uint8_t bus_n = pci_bus_num(vtd_as->bus);
35751b2b1237SJason Wang     uint16_t sid = PCI_BUILD_BDF(bus_n, vtd_as->devfn);
35761b2b1237SJason Wang     bool is_fpd_set = false;
35771b2b1237SJason Wang     VTDContextEntry ce;
35781b2b1237SJason Wang 
35791b2b1237SJason Wang     assert(vtd_as->pasid != PCI_NO_PASID);
35801b2b1237SJason Wang 
35811b2b1237SJason Wang     /* Try out best to fetch FPD, we can't do anything more */
35821b2b1237SJason Wang     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
35831b2b1237SJason Wang         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
35841b2b1237SJason Wang         if (!is_fpd_set && s->root_scalable) {
35851b2b1237SJason Wang             vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
35861b2b1237SJason Wang         }
35871b2b1237SJason Wang     }
35881b2b1237SJason Wang 
35891b2b1237SJason Wang     vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR,
35901b2b1237SJason Wang                      is_fpd_set, sid, addr, is_write,
35911b2b1237SJason Wang                      true, vtd_as->pasid);
35921b2b1237SJason Wang }
35931b2b1237SJason Wang 
35941b2b1237SJason Wang static MemTxResult vtd_mem_ir_fault_read(void *opaque, hwaddr addr,
35951b2b1237SJason Wang                                          uint64_t *data, unsigned size,
35961b2b1237SJason Wang                                          MemTxAttrs attrs)
35971b2b1237SJason Wang {
35981b2b1237SJason Wang     vtd_report_ir_illegal_access(opaque, addr, false);
35991b2b1237SJason Wang 
36001b2b1237SJason Wang     return MEMTX_ERROR;
36011b2b1237SJason Wang }
36021b2b1237SJason Wang 
36031b2b1237SJason Wang static MemTxResult vtd_mem_ir_fault_write(void *opaque, hwaddr addr,
36041b2b1237SJason Wang                                           uint64_t value, unsigned size,
36051b2b1237SJason Wang                                           MemTxAttrs attrs)
36061b2b1237SJason Wang {
36071b2b1237SJason Wang     vtd_report_ir_illegal_access(opaque, addr, true);
36081b2b1237SJason Wang 
36091b2b1237SJason Wang     return MEMTX_ERROR;
36101b2b1237SJason Wang }
36111b2b1237SJason Wang 
36121b2b1237SJason Wang static const MemoryRegionOps vtd_mem_ir_fault_ops = {
36131b2b1237SJason Wang     .read_with_attrs = vtd_mem_ir_fault_read,
36141b2b1237SJason Wang     .write_with_attrs = vtd_mem_ir_fault_write,
36151b2b1237SJason Wang     .endianness = DEVICE_LITTLE_ENDIAN,
36161b2b1237SJason Wang     .impl = {
36171b2b1237SJason Wang         .min_access_size = 1,
36181b2b1237SJason Wang         .max_access_size = 8,
36191b2b1237SJason Wang     },
36201b2b1237SJason Wang     .valid = {
36211b2b1237SJason Wang         .min_access_size = 1,
36221b2b1237SJason Wang         .max_access_size = 8,
36231b2b1237SJason Wang     },
36241b2b1237SJason Wang };
36251b2b1237SJason Wang 
36261b2b1237SJason Wang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
36271b2b1237SJason Wang                                  int devfn, unsigned int pasid)
36287df953bdSKnut Omang {
3629da8d439cSJason Wang     /*
3630da8d439cSJason Wang      * We can't simply use sid here since the bus number might not be
3631da8d439cSJason Wang      * initialized by the guest.
3632da8d439cSJason Wang      */
3633da8d439cSJason Wang     struct vtd_as_key key = {
3634da8d439cSJason Wang         .bus = bus,
3635da8d439cSJason Wang         .devfn = devfn,
36361b2b1237SJason Wang         .pasid = pasid,
3637da8d439cSJason Wang     };
36387df953bdSKnut Omang     VTDAddressSpace *vtd_dev_as;
3639e0a3c8ccSJason Wang     char name[128];
36407df953bdSKnut Omang 
3641da8d439cSJason Wang     vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
36427df953bdSKnut Omang     if (!vtd_dev_as) {
3643da8d439cSJason Wang         struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
3644da8d439cSJason Wang 
3645da8d439cSJason Wang         new_key->bus = bus;
3646da8d439cSJason Wang         new_key->devfn = devfn;
36471b2b1237SJason Wang         new_key->pasid = pasid;
3648da8d439cSJason Wang 
36491b2b1237SJason Wang         if (pasid == PCI_NO_PASID) {
36504b519ef1SPeter Xu             snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
36514b519ef1SPeter Xu                      PCI_FUNC(devfn));
36521b2b1237SJason Wang         } else {
36531b2b1237SJason Wang             snprintf(name, sizeof(name), "vtd-%02x.%x-pasid-%x", PCI_SLOT(devfn),
36541b2b1237SJason Wang                      PCI_FUNC(devfn), pasid);
36551b2b1237SJason Wang         }
36561b2b1237SJason Wang 
3657da8d439cSJason Wang         vtd_dev_as = g_new0(VTDAddressSpace, 1);
36587df953bdSKnut Omang 
36597df953bdSKnut Omang         vtd_dev_as->bus = bus;
36607df953bdSKnut Omang         vtd_dev_as->devfn = (uint8_t)devfn;
36611b2b1237SJason Wang         vtd_dev_as->pasid = pasid;
36627df953bdSKnut Omang         vtd_dev_as->iommu_state = s;
36637df953bdSKnut Omang         vtd_dev_as->context_cache_entry.context_cache_gen = 0;
366463b88968SPeter Xu         vtd_dev_as->iova_tree = iova_tree_new();
3665558e0024SPeter Xu 
36664b519ef1SPeter Xu         memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX);
36674b519ef1SPeter Xu         address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root");
36684b519ef1SPeter Xu 
3669558e0024SPeter Xu         /*
36704b519ef1SPeter Xu          * Build the DMAR-disabled container with aliases to the
36714b519ef1SPeter Xu          * shared MRs.  Note that aliasing to a shared memory region
36724b519ef1SPeter Xu          * could help the memory API to detect same FlatViews so we
36734b519ef1SPeter Xu          * can have devices to share the same FlatView when DMAR is
36744b519ef1SPeter Xu          * disabled (either by not providing "intel_iommu=on" or with
36754b519ef1SPeter Xu          * "iommu=pt").  It will greatly reduce the total number of
36764b519ef1SPeter Xu          * FlatViews of the system hence VM runs faster.
3677558e0024SPeter Xu          */
36784b519ef1SPeter Xu         memory_region_init_alias(&vtd_dev_as->nodmar, OBJECT(s),
36794b519ef1SPeter Xu                                  "vtd-nodmar", &s->mr_nodmar, 0,
36804b519ef1SPeter Xu                                  memory_region_size(&s->mr_nodmar));
36814b519ef1SPeter Xu 
36824b519ef1SPeter Xu         /*
36834b519ef1SPeter Xu          * Build the per-device DMAR-enabled container.
36844b519ef1SPeter Xu          *
36854b519ef1SPeter Xu          * TODO: currently we have per-device IOMMU memory region only
36864b519ef1SPeter Xu          * because we have per-device IOMMU notifiers for devices.  If
36874b519ef1SPeter Xu          * one day we can abstract the IOMMU notifiers out of the
36884b519ef1SPeter Xu          * memory regions then we can also share the same memory
36894b519ef1SPeter Xu          * region here just like what we've done above with the nodmar
36904b519ef1SPeter Xu          * region.
36914b519ef1SPeter Xu          */
36924b519ef1SPeter Xu         strcat(name, "-dmar");
36931221a474SAlexey Kardashevskiy         memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
36941221a474SAlexey Kardashevskiy                                  TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
36954b519ef1SPeter Xu                                  name, UINT64_MAX);
36964b519ef1SPeter Xu         memory_region_init_alias(&vtd_dev_as->iommu_ir, OBJECT(s), "vtd-ir",
36974b519ef1SPeter Xu                                  &s->mr_ir, 0, memory_region_size(&s->mr_ir));
36984b519ef1SPeter Xu         memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->iommu),
3699558e0024SPeter Xu                                             VTD_INTERRUPT_ADDR_FIRST,
37004b519ef1SPeter Xu                                             &vtd_dev_as->iommu_ir, 1);
37014b519ef1SPeter Xu 
37024b519ef1SPeter Xu         /*
37031b2b1237SJason Wang          * This region is used for catching fault to access interrupt
37041b2b1237SJason Wang          * range via passthrough + PASID. See also
37051b2b1237SJason Wang          * vtd_switch_address_space(). We can't use alias since we
37061b2b1237SJason Wang          * need to know the sid which is valid for MSI who uses
37071b2b1237SJason Wang          * bus_master_as (see msi_send_message()).
37081b2b1237SJason Wang          */
37091b2b1237SJason Wang         memory_region_init_io(&vtd_dev_as->iommu_ir_fault, OBJECT(s),
37101b2b1237SJason Wang                               &vtd_mem_ir_fault_ops, vtd_dev_as, "vtd-no-ir",
37111b2b1237SJason Wang                               VTD_INTERRUPT_ADDR_SIZE);
37121b2b1237SJason Wang         /*
37131b2b1237SJason Wang          * Hook to root since when PT is enabled vtd_dev_as->iommu
37141b2b1237SJason Wang          * will be disabled.
37151b2b1237SJason Wang          */
37161b2b1237SJason Wang         memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->root),
37171b2b1237SJason Wang                                             VTD_INTERRUPT_ADDR_FIRST,
37181b2b1237SJason Wang                                             &vtd_dev_as->iommu_ir_fault, 2);
37191b2b1237SJason Wang 
37201b2b1237SJason Wang         /*
37214b519ef1SPeter Xu          * Hook both the containers under the root container, we
37224b519ef1SPeter Xu          * switch between DMAR & noDMAR by enable/disable
37234b519ef1SPeter Xu          * corresponding sub-containers
37244b519ef1SPeter Xu          */
3725558e0024SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
37263df9d748SAlexey Kardashevskiy                                             MEMORY_REGION(&vtd_dev_as->iommu),
37274b519ef1SPeter Xu                                             0);
37284b519ef1SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
37294b519ef1SPeter Xu                                             &vtd_dev_as->nodmar, 0);
37304b519ef1SPeter Xu 
3731558e0024SPeter Xu         vtd_switch_address_space(vtd_dev_as);
3732da8d439cSJason Wang 
3733da8d439cSJason Wang         g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
37347df953bdSKnut Omang     }
37357df953bdSKnut Omang     return vtd_dev_as;
37367df953bdSKnut Omang }
37377df953bdSKnut Omang 
3738dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */
3739dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
3740dd4d607eSPeter Xu {
37419a4bb839SPeter Xu     hwaddr size, remain;
3742dd4d607eSPeter Xu     hwaddr start = n->start;
3743dd4d607eSPeter Xu     hwaddr end = n->end;
374437f51384SPrasad Singamsetty     IntelIOMMUState *s = as->iommu_state;
374563b88968SPeter Xu     DMAMap map;
3746dd4d607eSPeter Xu 
3747dd4d607eSPeter Xu     /*
3748dd4d607eSPeter Xu      * Note: all the codes in this function has a assumption that IOVA
3749dd4d607eSPeter Xu      * bits are no more than VTD_MGAW bits (which is restricted by
3750dd4d607eSPeter Xu      * VT-d spec), otherwise we need to consider overflow of 64 bits.
3751dd4d607eSPeter Xu      */
3752dd4d607eSPeter Xu 
3753d6d10793SYan Zhao     if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) {
3754dd4d607eSPeter Xu         /*
3755dd4d607eSPeter Xu          * Don't need to unmap regions that is bigger than the whole
3756dd4d607eSPeter Xu          * VT-d supported address space size
3757dd4d607eSPeter Xu          */
3758d6d10793SYan Zhao         end = VTD_ADDRESS_SIZE(s->aw_bits) - 1;
3759dd4d607eSPeter Xu     }
3760dd4d607eSPeter Xu 
3761dd4d607eSPeter Xu     assert(start <= end);
37629a4bb839SPeter Xu     size = remain = end - start + 1;
3763dd4d607eSPeter Xu 
37649a4bb839SPeter Xu     while (remain >= VTD_PAGE_SIZE) {
37655039caf3SEugenio Pérez         IOMMUTLBEvent event;
3766f14fb6c2SEric Auger         uint64_t mask = dma_aligned_pow2_mask(start, end, s->aw_bits);
3767f14fb6c2SEric Auger         uint64_t size = mask + 1;
3768dd4d607eSPeter Xu 
3769f14fb6c2SEric Auger         assert(size);
37709a4bb839SPeter Xu 
37715039caf3SEugenio Pérez         event.type = IOMMU_NOTIFIER_UNMAP;
37725039caf3SEugenio Pérez         event.entry.iova = start;
3773f14fb6c2SEric Auger         event.entry.addr_mask = mask;
37745039caf3SEugenio Pérez         event.entry.target_as = &address_space_memory;
37755039caf3SEugenio Pérez         event.entry.perm = IOMMU_NONE;
3776dd4d607eSPeter Xu         /* This field is meaningless for unmap */
37775039caf3SEugenio Pérez         event.entry.translated_addr = 0;
37789a4bb839SPeter Xu 
37795039caf3SEugenio Pérez         memory_region_notify_iommu_one(n, &event);
37809a4bb839SPeter Xu 
3781f14fb6c2SEric Auger         start += size;
3782f14fb6c2SEric Auger         remain -= size;
37839a4bb839SPeter Xu     }
37849a4bb839SPeter Xu 
37859a4bb839SPeter Xu     assert(!remain);
3786dd4d607eSPeter Xu 
3787dd4d607eSPeter Xu     trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
3788dd4d607eSPeter Xu                              VTD_PCI_SLOT(as->devfn),
3789dd4d607eSPeter Xu                              VTD_PCI_FUNC(as->devfn),
37909a4bb839SPeter Xu                              n->start, size);
3791dd4d607eSPeter Xu 
37929a4bb839SPeter Xu     map.iova = n->start;
37939a4bb839SPeter Xu     map.size = size;
379469292a8eSEugenio Pérez     iova_tree_remove(as->iova_tree, map);
3795dd4d607eSPeter Xu }
3796dd4d607eSPeter Xu 
3797dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s)
3798dd4d607eSPeter Xu {
3799dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
3800dd4d607eSPeter Xu     IOMMUNotifier *n;
3801dd4d607eSPeter Xu 
3802b4a4ba0dSPeter Xu     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
3803dd4d607eSPeter Xu         IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
3804dd4d607eSPeter Xu             vtd_address_space_unmap(vtd_as, n);
3805dd4d607eSPeter Xu         }
3806dd4d607eSPeter Xu     }
3807dd4d607eSPeter Xu }
3808dd4d607eSPeter Xu 
38092cc9ddccSPeter Xu static void vtd_address_space_refresh_all(IntelIOMMUState *s)
38102cc9ddccSPeter Xu {
38112cc9ddccSPeter Xu     vtd_address_space_unmap_all(s);
38122cc9ddccSPeter Xu     vtd_switch_address_space_all(s);
38132cc9ddccSPeter Xu }
38142cc9ddccSPeter Xu 
38155039caf3SEugenio Pérez static int vtd_replay_hook(IOMMUTLBEvent *event, void *private)
3816f06a696dSPeter Xu {
38175039caf3SEugenio Pérez     memory_region_notify_iommu_one(private, event);
3818f06a696dSPeter Xu     return 0;
3819f06a696dSPeter Xu }
3820f06a696dSPeter Xu 
38213df9d748SAlexey Kardashevskiy static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
3822f06a696dSPeter Xu {
38233df9d748SAlexey Kardashevskiy     VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
3824f06a696dSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
3825f06a696dSPeter Xu     uint8_t bus_n = pci_bus_num(vtd_as->bus);
3826f06a696dSPeter Xu     VTDContextEntry ce;
3827f06a696dSPeter Xu 
3828f06a696dSPeter Xu     /*
3829dd4d607eSPeter Xu      * The replay can be triggered by either a invalidation or a newly
3830dd4d607eSPeter Xu      * created entry. No matter what, we release existing mappings
3831dd4d607eSPeter Xu      * (it means flushing caches for UNMAP-only registers).
3832f06a696dSPeter Xu      */
3833dd4d607eSPeter Xu     vtd_address_space_unmap(vtd_as, n);
3834dd4d607eSPeter Xu 
3835dd4d607eSPeter Xu     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
3836fb43cf73SLiu, Yi L         trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
3837fb43cf73SLiu, Yi L                                   "legacy mode",
3838fb43cf73SLiu, Yi L                                   bus_n, PCI_SLOT(vtd_as->devfn),
3839f06a696dSPeter Xu                                   PCI_FUNC(vtd_as->devfn),
38401b2b1237SJason Wang                                   vtd_get_domain_id(s, &ce, vtd_as->pasid),
3841f06a696dSPeter Xu                                   ce.hi, ce.lo);
38424f8a62a9SPeter Xu         if (vtd_as_has_map_notifier(vtd_as)) {
38434f8a62a9SPeter Xu             /* This is required only for MAP typed notifiers */
3844fe215b0cSPeter Xu             vtd_page_walk_info info = {
3845fe215b0cSPeter Xu                 .hook_fn = vtd_replay_hook,
3846fe215b0cSPeter Xu                 .private = (void *)n,
3847fe215b0cSPeter Xu                 .notify_unmap = false,
3848fe215b0cSPeter Xu                 .aw = s->aw_bits,
38492f764fa8SPeter Xu                 .as = vtd_as,
38501b2b1237SJason Wang                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
3851fe215b0cSPeter Xu             };
3852fe215b0cSPeter Xu 
38536da24341SZhenzhong Duan             vtd_page_walk(s, &ce, n->start, n->end, &info, vtd_as->pasid);
38544f8a62a9SPeter Xu         }
3855f06a696dSPeter Xu     } else {
3856f06a696dSPeter Xu         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
3857f06a696dSPeter Xu                                     PCI_FUNC(vtd_as->devfn));
3858f06a696dSPeter Xu     }
3859f06a696dSPeter Xu 
3860f06a696dSPeter Xu     return;
3861f06a696dSPeter Xu }
3862f06a696dSPeter Xu 
38631da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay
38641da12ec4SLe Tan  * attention when adding new initialization stuff.
38651da12ec4SLe Tan  */
38661da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s)
38671da12ec4SLe Tan {
3868d54bd7f8SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3869d54bd7f8SPeter Xu 
38701da12ec4SLe Tan     memset(s->csr, 0, DMAR_REG_SIZE);
38711da12ec4SLe Tan     memset(s->wmask, 0, DMAR_REG_SIZE);
38721da12ec4SLe Tan     memset(s->w1cmask, 0, DMAR_REG_SIZE);
38731da12ec4SLe Tan     memset(s->womask, 0, DMAR_REG_SIZE);
38741da12ec4SLe Tan 
38751da12ec4SLe Tan     s->root = 0;
3876fb43cf73SLiu, Yi L     s->root_scalable = false;
38771da12ec4SLe Tan     s->dmar_enabled = false;
3878d7bb469aSPeter Xu     s->intr_enabled = false;
38791da12ec4SLe Tan     s->iq_head = 0;
38801da12ec4SLe Tan     s->iq_tail = 0;
38811da12ec4SLe Tan     s->iq = 0;
38821da12ec4SLe Tan     s->iq_size = 0;
38831da12ec4SLe Tan     s->qi_enabled = false;
38841da12ec4SLe Tan     s->iq_last_desc_type = VTD_INV_DESC_NONE;
3885c0c1d351SLiu, Yi L     s->iq_dw = false;
38861da12ec4SLe Tan     s->next_frcd_reg = 0;
388792e5d85eSPrasad Singamsetty     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
388892e5d85eSPrasad Singamsetty              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
38898646d9c7SDavid Woodhouse              VTD_CAP_MGAW(s->aw_bits);
3890ccc23bb0SPeter Xu     if (s->dma_drain) {
3891ccc23bb0SPeter Xu         s->cap |= VTD_CAP_DRAIN;
3892ccc23bb0SPeter Xu     }
38938646d9c7SDavid Woodhouse     if (s->dma_translation) {
38948646d9c7SDavid Woodhouse             if (s->aw_bits >= VTD_HOST_AW_39BIT) {
38958646d9c7SDavid Woodhouse                     s->cap |= VTD_CAP_SAGAW_39bit;
38968646d9c7SDavid Woodhouse             }
38978646d9c7SDavid Woodhouse             if (s->aw_bits >= VTD_HOST_AW_48BIT) {
389837f51384SPrasad Singamsetty                     s->cap |= VTD_CAP_SAGAW_48bit;
389937f51384SPrasad Singamsetty             }
39008646d9c7SDavid Woodhouse     }
3901ed7b8fbcSLe Tan     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
39021da12ec4SLe Tan 
390392e5d85eSPrasad Singamsetty     /*
390492e5d85eSPrasad Singamsetty      * Rsvd field masks for spte
390592e5d85eSPrasad Singamsetty      */
3906ce586f3bSQi, Yadong     vtd_spte_rsvd[0] = ~0ULL;
3907e48929c7SQi, Yadong     vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
3908e48929c7SQi, Yadong                                                   x86_iommu->dt_supported);
3909ce586f3bSQi, Yadong     vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
3910ce586f3bSQi, Yadong     vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
3911ce586f3bSQi, Yadong     vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
3912ce586f3bSQi, Yadong 
3913e48929c7SQi, Yadong     vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
3914e48929c7SQi, Yadong                                                          x86_iommu->dt_supported);
3915e48929c7SQi, Yadong     vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
3916e48929c7SQi, Yadong                                                          x86_iommu->dt_supported);
391792e5d85eSPrasad Singamsetty 
3918b8ffd7d6SJason Wang     if (s->scalable_mode || s->snoop_control) {
39190192d667SJason Wang         vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
39200192d667SJason Wang         vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
39210192d667SJason Wang         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
39220192d667SJason Wang     }
39230192d667SJason Wang 
3924a924b3d8SPeter Xu     if (x86_iommu_ir_supported(x86_iommu)) {
3925e6b6af05SRadim Krčmář         s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
3926e6b6af05SRadim Krčmář         if (s->intr_eim == ON_OFF_AUTO_ON) {
3927e6b6af05SRadim Krčmář             s->ecap |= VTD_ECAP_EIM;
3928e6b6af05SRadim Krčmář         }
3929e6b6af05SRadim Krčmář         assert(s->intr_eim != ON_OFF_AUTO_AUTO);
3930d54bd7f8SPeter Xu     }
3931d54bd7f8SPeter Xu 
3932554f5e16SJason Wang     if (x86_iommu->dt_supported) {
3933554f5e16SJason Wang         s->ecap |= VTD_ECAP_DT;
3934554f5e16SJason Wang     }
3935554f5e16SJason Wang 
3936dbaabb25SPeter Xu     if (x86_iommu->pt_supported) {
3937dbaabb25SPeter Xu         s->ecap |= VTD_ECAP_PT;
3938dbaabb25SPeter Xu     }
3939dbaabb25SPeter Xu 
39403b40f0e5SAviv Ben-David     if (s->caching_mode) {
39413b40f0e5SAviv Ben-David         s->cap |= VTD_CAP_CM;
39423b40f0e5SAviv Ben-David     }
39433b40f0e5SAviv Ben-David 
39444a4f219eSYi Sun     /* TODO: read cap/ecap from host to decide which cap to be exposed. */
39454a4f219eSYi Sun     if (s->scalable_mode) {
39464a4f219eSYi Sun         s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
39474a4f219eSYi Sun     }
39484a4f219eSYi Sun 
3949b8ffd7d6SJason Wang     if (s->snoop_control) {
3950b8ffd7d6SJason Wang         s->ecap |= VTD_ECAP_SC;
3951b8ffd7d6SJason Wang     }
3952b8ffd7d6SJason Wang 
39531b2b1237SJason Wang     if (s->pasid) {
39541b2b1237SJason Wang         s->ecap |= VTD_ECAP_PASID;
39551b2b1237SJason Wang     }
39561b2b1237SJason Wang 
395706aba4caSPeter Xu     vtd_reset_caches(s);
3958d92fa2dcSLe Tan 
39591da12ec4SLe Tan     /* Define registers with default values and bit semantics */
39601da12ec4SLe Tan     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
39611da12ec4SLe Tan     vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
39621da12ec4SLe Tan     vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
39631da12ec4SLe Tan     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
39641da12ec4SLe Tan     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
39651da12ec4SLe Tan     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
3966fb43cf73SLiu, Yi L     vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0);
39671da12ec4SLe Tan     vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
39681da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
39691da12ec4SLe Tan 
39701da12ec4SLe Tan     /* Advanced Fault Logging not supported */
39711da12ec4SLe Tan     vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
39721da12ec4SLe Tan     vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
39731da12ec4SLe Tan     vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
39741da12ec4SLe Tan     vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
39751da12ec4SLe Tan 
39761da12ec4SLe Tan     /* Treated as RsvdZ when EIM in ECAP_REG is not supported
39771da12ec4SLe Tan      * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
39781da12ec4SLe Tan      */
39791da12ec4SLe Tan     vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
39801da12ec4SLe Tan 
39811da12ec4SLe Tan     /* Treated as RO for implementations that PLMR and PHMR fields reported
39821da12ec4SLe Tan      * as Clear in the CAP_REG.
39831da12ec4SLe Tan      * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
39841da12ec4SLe Tan      */
39851da12ec4SLe Tan     vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
39861da12ec4SLe Tan 
3987ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
3988ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
3989c0c1d351SLiu, Yi L     vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff807ULL, 0);
3990ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
3991ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3992ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
3993ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
3994ed7b8fbcSLe Tan     /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
3995ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
3996ed7b8fbcSLe Tan 
39971da12ec4SLe Tan     /* IOTLB registers */
39981da12ec4SLe Tan     vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
39991da12ec4SLe Tan     vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
40001da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
40011da12ec4SLe Tan 
40021da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
40031da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
40041da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
4005a5861439SPeter Xu 
4006a5861439SPeter Xu     /*
400728589311SJan Kiszka      * Interrupt remapping registers.
4008a5861439SPeter Xu      */
400928589311SJan Kiszka     vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
40101da12ec4SLe Tan }
40111da12ec4SLe Tan 
40121da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use
40131da12ec4SLe Tan  * the address space they got at first (won't ask the bus again).
40141da12ec4SLe Tan  */
40151da12ec4SLe Tan static void vtd_reset(DeviceState *dev)
40161da12ec4SLe Tan {
40171da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
40181da12ec4SLe Tan 
40191da12ec4SLe Tan     vtd_init(s);
40202cc9ddccSPeter Xu     vtd_address_space_refresh_all(s);
40211da12ec4SLe Tan }
40221da12ec4SLe Tan 
4023621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
4024621d983aSMarcel Apfelbaum {
4025621d983aSMarcel Apfelbaum     IntelIOMMUState *s = opaque;
4026621d983aSMarcel Apfelbaum     VTDAddressSpace *vtd_as;
4027621d983aSMarcel Apfelbaum 
4028bf33cc75SPeter Xu     assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
4029621d983aSMarcel Apfelbaum 
40301b2b1237SJason Wang     vtd_as = vtd_find_add_as(s, bus, devfn, PCI_NO_PASID);
4031621d983aSMarcel Apfelbaum     return &vtd_as->as;
4032621d983aSMarcel Apfelbaum }
4033621d983aSMarcel Apfelbaum 
4034e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
40356333e93cSRadim Krčmář {
4036e6b6af05SRadim Krčmář     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
4037e6b6af05SRadim Krčmář 
4038a924b3d8SPeter Xu     if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) {
4039e6b6af05SRadim Krčmář         error_setg(errp, "eim=on cannot be selected without intremap=on");
4040e6b6af05SRadim Krčmář         return false;
4041e6b6af05SRadim Krčmář     }
4042e6b6af05SRadim Krčmář 
4043e6b6af05SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_AUTO) {
4044fb506e70SRadim Krčmář         s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
4045a924b3d8SPeter Xu                       && x86_iommu_ir_supported(x86_iommu) ?
4046e6b6af05SRadim Krčmář                                               ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
4047e6b6af05SRadim Krčmář     }
4048fb506e70SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
404977250171SDavid Woodhouse         if (!kvm_irqchip_is_split()) {
4050fb506e70SRadim Krčmář             error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
4051fb506e70SRadim Krčmář             return false;
4052fb506e70SRadim Krčmář         }
405320ca4742SPeter Xu         if (!kvm_enable_x2apic()) {
405420ca4742SPeter Xu             error_setg(errp, "eim=on requires support on the KVM side"
405520ca4742SPeter Xu                              "(X2APIC_API, first shipped in v4.7)");
405620ca4742SPeter Xu             return false;
405720ca4742SPeter Xu         }
4058fb506e70SRadim Krčmář     }
4059e6b6af05SRadim Krčmář 
406037f51384SPrasad Singamsetty     /* Currently only address widths supported are 39 and 48 bits */
406137f51384SPrasad Singamsetty     if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
406237f51384SPrasad Singamsetty         (s->aw_bits != VTD_HOST_AW_48BIT)) {
40632a345149SMenno Lageman         error_setg(errp, "Supported values for aw-bits are: %d, %d",
406437f51384SPrasad Singamsetty                    VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
406537f51384SPrasad Singamsetty         return false;
406637f51384SPrasad Singamsetty     }
406737f51384SPrasad Singamsetty 
40684a4f219eSYi Sun     if (s->scalable_mode && !s->dma_drain) {
40694a4f219eSYi Sun         error_setg(errp, "Need to set dma_drain for scalable mode");
40704a4f219eSYi Sun         return false;
40714a4f219eSYi Sun     }
40724a4f219eSYi Sun 
40731b2b1237SJason Wang     if (s->pasid && !s->scalable_mode) {
40741b2b1237SJason Wang         error_setg(errp, "Need to set scalable mode for PASID");
40751b2b1237SJason Wang         return false;
40761b2b1237SJason Wang     }
40771b2b1237SJason Wang 
40786333e93cSRadim Krčmář     return true;
40796333e93cSRadim Krčmář }
40806333e93cSRadim Krčmář 
408128cf553aSPeter Xu static int vtd_machine_done_notify_one(Object *child, void *unused)
408228cf553aSPeter Xu {
408328cf553aSPeter Xu     IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
408428cf553aSPeter Xu 
408528cf553aSPeter Xu     /*
408628cf553aSPeter Xu      * We hard-coded here because vfio-pci is the only special case
408728cf553aSPeter Xu      * here.  Let's be more elegant in the future when we can, but so
408828cf553aSPeter Xu      * far there seems to be no better way.
408928cf553aSPeter Xu      */
409028cf553aSPeter Xu     if (object_dynamic_cast(child, "vfio-pci") && !iommu->caching_mode) {
409128cf553aSPeter Xu         vtd_panic_require_caching_mode();
409228cf553aSPeter Xu     }
409328cf553aSPeter Xu 
409428cf553aSPeter Xu     return 0;
409528cf553aSPeter Xu }
409628cf553aSPeter Xu 
409728cf553aSPeter Xu static void vtd_machine_done_hook(Notifier *notifier, void *unused)
409828cf553aSPeter Xu {
409928cf553aSPeter Xu     object_child_foreach_recursive(object_get_root(),
410028cf553aSPeter Xu                                    vtd_machine_done_notify_one, NULL);
410128cf553aSPeter Xu }
410228cf553aSPeter Xu 
410328cf553aSPeter Xu static Notifier vtd_machine_done_notify = {
410428cf553aSPeter Xu     .notify = vtd_machine_done_hook,
410528cf553aSPeter Xu };
410628cf553aSPeter Xu 
41071da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp)
41081da12ec4SLe Tan {
4109ef0e8fc7SEduardo Habkost     MachineState *ms = MACHINE(qdev_get_machine());
411029396ed9SMohammed Gamal     PCMachineState *pcms = PC_MACHINE(ms);
4111f0bb276bSPaolo Bonzini     X86MachineState *x86ms = X86_MACHINE(ms);
411229396ed9SMohammed Gamal     PCIBus *bus = pcms->bus;
41131da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
41141b2b1237SJason Wang     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
41151b2b1237SJason Wang 
41161b2b1237SJason Wang     if (s->pasid && x86_iommu->dt_supported) {
41171b2b1237SJason Wang         /*
41181b2b1237SJason Wang          * PASID-based-Device-TLB Invalidate Descriptor is not
41191b2b1237SJason Wang          * implemented and it requires support from vhost layer which
41201b2b1237SJason Wang          * needs to be implemented in the future.
41211b2b1237SJason Wang          */
41221b2b1237SJason Wang         error_setg(errp, "PASID based device IOTLB is not supported");
41231b2b1237SJason Wang         return;
41241b2b1237SJason Wang     }
41256333e93cSRadim Krčmář 
4126e6b6af05SRadim Krčmář     if (!vtd_decide_config(s, errp)) {
41276333e93cSRadim Krčmář         return;
41286333e93cSRadim Krčmář     }
41296333e93cSRadim Krčmář 
4130b4a4ba0dSPeter Xu     QLIST_INIT(&s->vtd_as_with_notifiers);
41311d9efa73SPeter Xu     qemu_mutex_init(&s->iommu_lock);
41321da12ec4SLe Tan     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
41331da12ec4SLe Tan                           "intel_iommu", DMAR_REG_SIZE);
41344b519ef1SPeter Xu 
41354b519ef1SPeter Xu     /* Create the shared memory regions by all devices */
41364b519ef1SPeter Xu     memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar",
41374b519ef1SPeter Xu                        UINT64_MAX);
41384b519ef1SPeter Xu     memory_region_init_io(&s->mr_ir, OBJECT(s), &vtd_mem_ir_ops,
41394b519ef1SPeter Xu                           s, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE);
41404b519ef1SPeter Xu     memory_region_init_alias(&s->mr_sys_alias, OBJECT(s),
41414b519ef1SPeter Xu                              "vtd-sys-alias", get_system_memory(), 0,
41424b519ef1SPeter Xu                              memory_region_size(get_system_memory()));
41434b519ef1SPeter Xu     memory_region_add_subregion_overlap(&s->mr_nodmar, 0,
41444b519ef1SPeter Xu                                         &s->mr_sys_alias, 0);
41454b519ef1SPeter Xu     memory_region_add_subregion_overlap(&s->mr_nodmar,
41464b519ef1SPeter Xu                                         VTD_INTERRUPT_ADDR_FIRST,
41474b519ef1SPeter Xu                                         &s->mr_ir, 1);
41484b519ef1SPeter Xu 
41491da12ec4SLe Tan     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
4150b5a280c0SLe Tan     /* No corresponding destroy */
41511b2b1237SJason Wang     s->iotlb = g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal,
4152b5a280c0SLe Tan                                      g_free, g_free);
4153da8d439cSJason Wang     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
41547df953bdSKnut Omang                                       g_free, g_free);
41551da12ec4SLe Tan     vtd_init(s);
4156621d983aSMarcel Apfelbaum     sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
4157621d983aSMarcel Apfelbaum     pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
4158cb135f59SPeter Xu     /* Pseudo address space under root PCI bus. */
4159f0bb276bSPaolo Bonzini     x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
416028cf553aSPeter Xu     qemu_add_machine_init_done_notifier(&vtd_machine_done_notify);
41611da12ec4SLe Tan }
41621da12ec4SLe Tan 
41631da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data)
41641da12ec4SLe Tan {
41651da12ec4SLe Tan     DeviceClass *dc = DEVICE_CLASS(klass);
416630c60f77SEduardo Habkost     X86IOMMUClass *x86_class = X86_IOMMU_DEVICE_CLASS(klass);
41671da12ec4SLe Tan 
41681da12ec4SLe Tan     dc->reset = vtd_reset;
41691da12ec4SLe Tan     dc->vmsd = &vtd_vmstate;
41704f67d30bSMarc-André Lureau     device_class_set_props(dc, vtd_properties);
4171621d983aSMarcel Apfelbaum     dc->hotpluggable = false;
41721c7955c4SPeter Xu     x86_class->realize = vtd_realize;
41738b5ed7dfSPeter Xu     x86_class->int_remap = vtd_int_remap;
41748ab5700cSEduardo Habkost     /* Supported by the pc-q35-* machine types */
4175e4f4fb1eSEduardo Habkost     dc->user_creatable = true;
41761ec202c9SErnest Esene     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
41771ec202c9SErnest Esene     dc->desc = "Intel IOMMU (VT-d) DMA Remapping device";
41781da12ec4SLe Tan }
41791da12ec4SLe Tan 
41801da12ec4SLe Tan static const TypeInfo vtd_info = {
41811da12ec4SLe Tan     .name          = TYPE_INTEL_IOMMU_DEVICE,
41821c7955c4SPeter Xu     .parent        = TYPE_X86_IOMMU_DEVICE,
41831da12ec4SLe Tan     .instance_size = sizeof(IntelIOMMUState),
41841da12ec4SLe Tan     .class_init    = vtd_class_init,
41851da12ec4SLe Tan };
41861da12ec4SLe Tan 
41871221a474SAlexey Kardashevskiy static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
41881221a474SAlexey Kardashevskiy                                                      void *data)
41891221a474SAlexey Kardashevskiy {
41901221a474SAlexey Kardashevskiy     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
41911221a474SAlexey Kardashevskiy 
41921221a474SAlexey Kardashevskiy     imrc->translate = vtd_iommu_translate;
41931221a474SAlexey Kardashevskiy     imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
41941221a474SAlexey Kardashevskiy     imrc->replay = vtd_iommu_replay;
41951221a474SAlexey Kardashevskiy }
41961221a474SAlexey Kardashevskiy 
41971221a474SAlexey Kardashevskiy static const TypeInfo vtd_iommu_memory_region_info = {
41981221a474SAlexey Kardashevskiy     .parent = TYPE_IOMMU_MEMORY_REGION,
41991221a474SAlexey Kardashevskiy     .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
42001221a474SAlexey Kardashevskiy     .class_init = vtd_iommu_memory_region_class_init,
42011221a474SAlexey Kardashevskiy };
42021221a474SAlexey Kardashevskiy 
42031da12ec4SLe Tan static void vtd_register_types(void)
42041da12ec4SLe Tan {
42051da12ec4SLe Tan     type_register_static(&vtd_info);
42061221a474SAlexey Kardashevskiy     type_register_static(&vtd_iommu_memory_region_info);
42071da12ec4SLe Tan }
42081da12ec4SLe Tan 
42091da12ec4SLe Tan type_init(vtd_register_types)
4210