xref: /openbmc/qemu/hw/i386/intel_iommu.c (revision e7a3b91fdfa84c9028003b315ca959ea4de30363)
11da12ec4SLe Tan /*
21da12ec4SLe Tan  * QEMU emulation of an Intel IOMMU (VT-d)
31da12ec4SLe Tan  *   (DMA Remapping device)
41da12ec4SLe Tan  *
51da12ec4SLe Tan  * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com>
61da12ec4SLe Tan  * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com>
71da12ec4SLe Tan  *
81da12ec4SLe Tan  * This program is free software; you can redistribute it and/or modify
91da12ec4SLe Tan  * it under the terms of the GNU General Public License as published by
101da12ec4SLe Tan  * the Free Software Foundation; either version 2 of the License, or
111da12ec4SLe Tan  * (at your option) any later version.
121da12ec4SLe Tan 
131da12ec4SLe Tan  * This program is distributed in the hope that it will be useful,
141da12ec4SLe Tan  * but WITHOUT ANY WARRANTY; without even the implied warranty of
151da12ec4SLe Tan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161da12ec4SLe Tan  * GNU General Public License for more details.
171da12ec4SLe Tan 
181da12ec4SLe Tan  * You should have received a copy of the GNU General Public License along
191da12ec4SLe Tan  * with this program; if not, see <http://www.gnu.org/licenses/>.
201da12ec4SLe Tan  */
211da12ec4SLe Tan 
22b6a0aa05SPeter Maydell #include "qemu/osdep.h"
234684a204SPeter Xu #include "qemu/error-report.h"
246333e93cSRadim Krčmář #include "qapi/error.h"
251da12ec4SLe Tan #include "hw/sysbus.h"
261da12ec4SLe Tan #include "exec/address-spaces.h"
271da12ec4SLe Tan #include "intel_iommu_internal.h"
287df953bdSKnut Omang #include "hw/pci/pci.h"
293cb3b154SAlex Williamson #include "hw/pci/pci_bus.h"
30621d983aSMarcel Apfelbaum #include "hw/i386/pc.h"
31dea651a9SFeng Wu #include "hw/i386/apic-msidef.h"
3204af0e18SPeter Xu #include "hw/boards.h"
3304af0e18SPeter Xu #include "hw/i386/x86-iommu.h"
34cb135f59SPeter Xu #include "hw/pci-host/q35.h"
354684a204SPeter Xu #include "sysemu/kvm.h"
3632946019SRadim Krčmář #include "hw/i386/apic_internal.h"
37fb506e70SRadim Krčmář #include "kvm_i386.h"
38bc535e59SPeter Xu #include "trace.h"
391da12ec4SLe Tan 
401da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
411da12ec4SLe Tan                             uint64_t wmask, uint64_t w1cmask)
421da12ec4SLe Tan {
431da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
441da12ec4SLe Tan     stq_le_p(&s->wmask[addr], wmask);
451da12ec4SLe Tan     stq_le_p(&s->w1cmask[addr], w1cmask);
461da12ec4SLe Tan }
471da12ec4SLe Tan 
481da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
491da12ec4SLe Tan {
501da12ec4SLe Tan     stq_le_p(&s->womask[addr], mask);
511da12ec4SLe Tan }
521da12ec4SLe Tan 
531da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
541da12ec4SLe Tan                             uint32_t wmask, uint32_t w1cmask)
551da12ec4SLe Tan {
561da12ec4SLe Tan     stl_le_p(&s->csr[addr], val);
571da12ec4SLe Tan     stl_le_p(&s->wmask[addr], wmask);
581da12ec4SLe Tan     stl_le_p(&s->w1cmask[addr], w1cmask);
591da12ec4SLe Tan }
601da12ec4SLe Tan 
611da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
621da12ec4SLe Tan {
631da12ec4SLe Tan     stl_le_p(&s->womask[addr], mask);
641da12ec4SLe Tan }
651da12ec4SLe Tan 
661da12ec4SLe Tan /* "External" get/set operations */
671da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
681da12ec4SLe Tan {
691da12ec4SLe Tan     uint64_t oldval = ldq_le_p(&s->csr[addr]);
701da12ec4SLe Tan     uint64_t wmask = ldq_le_p(&s->wmask[addr]);
711da12ec4SLe Tan     uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
721da12ec4SLe Tan     stq_le_p(&s->csr[addr],
731da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
741da12ec4SLe Tan }
751da12ec4SLe Tan 
761da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
771da12ec4SLe Tan {
781da12ec4SLe Tan     uint32_t oldval = ldl_le_p(&s->csr[addr]);
791da12ec4SLe Tan     uint32_t wmask = ldl_le_p(&s->wmask[addr]);
801da12ec4SLe Tan     uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
811da12ec4SLe Tan     stl_le_p(&s->csr[addr],
821da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
831da12ec4SLe Tan }
841da12ec4SLe Tan 
851da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
861da12ec4SLe Tan {
871da12ec4SLe Tan     uint64_t val = ldq_le_p(&s->csr[addr]);
881da12ec4SLe Tan     uint64_t womask = ldq_le_p(&s->womask[addr]);
891da12ec4SLe Tan     return val & ~womask;
901da12ec4SLe Tan }
911da12ec4SLe Tan 
921da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
931da12ec4SLe Tan {
941da12ec4SLe Tan     uint32_t val = ldl_le_p(&s->csr[addr]);
951da12ec4SLe Tan     uint32_t womask = ldl_le_p(&s->womask[addr]);
961da12ec4SLe Tan     return val & ~womask;
971da12ec4SLe Tan }
981da12ec4SLe Tan 
991da12ec4SLe Tan /* "Internal" get/set operations */
1001da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
1011da12ec4SLe Tan {
1021da12ec4SLe Tan     return ldq_le_p(&s->csr[addr]);
1031da12ec4SLe Tan }
1041da12ec4SLe Tan 
1051da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
1061da12ec4SLe Tan {
1071da12ec4SLe Tan     return ldl_le_p(&s->csr[addr]);
1081da12ec4SLe Tan }
1091da12ec4SLe Tan 
1101da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
1111da12ec4SLe Tan {
1121da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
1131da12ec4SLe Tan }
1141da12ec4SLe Tan 
1151da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
1161da12ec4SLe Tan                                         uint32_t clear, uint32_t mask)
1171da12ec4SLe Tan {
1181da12ec4SLe Tan     uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
1191da12ec4SLe Tan     stl_le_p(&s->csr[addr], new_val);
1201da12ec4SLe Tan     return new_val;
1211da12ec4SLe Tan }
1221da12ec4SLe Tan 
1231da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
1241da12ec4SLe Tan                                         uint64_t clear, uint64_t mask)
1251da12ec4SLe Tan {
1261da12ec4SLe Tan     uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
1271da12ec4SLe Tan     stq_le_p(&s->csr[addr], new_val);
1281da12ec4SLe Tan     return new_val;
1291da12ec4SLe Tan }
1301da12ec4SLe Tan 
131b5a280c0SLe Tan /* GHashTable functions */
132b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
133b5a280c0SLe Tan {
134b5a280c0SLe Tan     return *((const uint64_t *)v1) == *((const uint64_t *)v2);
135b5a280c0SLe Tan }
136b5a280c0SLe Tan 
137b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v)
138b5a280c0SLe Tan {
139b5a280c0SLe Tan     return (guint)*(const uint64_t *)v;
140b5a280c0SLe Tan }
141b5a280c0SLe Tan 
142b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
143b5a280c0SLe Tan                                           gpointer user_data)
144b5a280c0SLe Tan {
145b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
146b5a280c0SLe Tan     uint16_t domain_id = *(uint16_t *)user_data;
147b5a280c0SLe Tan     return entry->domain_id == domain_id;
148b5a280c0SLe Tan }
149b5a280c0SLe Tan 
150d66b969bSJason Wang /* The shift of an addr for a certain level of paging structure */
151d66b969bSJason Wang static inline uint32_t vtd_slpt_level_shift(uint32_t level)
152d66b969bSJason Wang {
1537e58326aSPeter Xu     assert(level != 0);
154d66b969bSJason Wang     return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
155d66b969bSJason Wang }
156d66b969bSJason Wang 
157d66b969bSJason Wang static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
158d66b969bSJason Wang {
159d66b969bSJason Wang     return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
160d66b969bSJason Wang }
161d66b969bSJason Wang 
162b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
163b5a280c0SLe Tan                                         gpointer user_data)
164b5a280c0SLe Tan {
165b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
166b5a280c0SLe Tan     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
167d66b969bSJason Wang     uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
168d66b969bSJason Wang     uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
169b5a280c0SLe Tan     return (entry->domain_id == info->domain_id) &&
170d66b969bSJason Wang             (((entry->gfn & info->mask) == gfn) ||
171d66b969bSJason Wang              (entry->gfn == gfn_tlb));
172b5a280c0SLe Tan }
173b5a280c0SLe Tan 
174d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of
175d92fa2dcSLe Tan  * IntelIOMMUState to 1.
176d92fa2dcSLe Tan  */
177d92fa2dcSLe Tan static void vtd_reset_context_cache(IntelIOMMUState *s)
178d92fa2dcSLe Tan {
179d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
1807df953bdSKnut Omang     VTDBus *vtd_bus;
1817df953bdSKnut Omang     GHashTableIter bus_it;
182d92fa2dcSLe Tan     uint32_t devfn_it;
183d92fa2dcSLe Tan 
1847feb51b7SPeter Xu     trace_vtd_context_cache_reset();
1857feb51b7SPeter Xu 
1867df953bdSKnut Omang     g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr);
1877df953bdSKnut Omang 
1887df953bdSKnut Omang     while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) {
18904af0e18SPeter Xu         for (devfn_it = 0; devfn_it < X86_IOMMU_PCI_DEVFN_MAX; ++devfn_it) {
1907df953bdSKnut Omang             vtd_as = vtd_bus->dev_as[devfn_it];
191d92fa2dcSLe Tan             if (!vtd_as) {
192d92fa2dcSLe Tan                 continue;
193d92fa2dcSLe Tan             }
194d92fa2dcSLe Tan             vtd_as->context_cache_entry.context_cache_gen = 0;
195d92fa2dcSLe Tan         }
196d92fa2dcSLe Tan     }
197d92fa2dcSLe Tan     s->context_cache_gen = 1;
198d92fa2dcSLe Tan }
199d92fa2dcSLe Tan 
200b5a280c0SLe Tan static void vtd_reset_iotlb(IntelIOMMUState *s)
201b5a280c0SLe Tan {
202b5a280c0SLe Tan     assert(s->iotlb);
203b5a280c0SLe Tan     g_hash_table_remove_all(s->iotlb);
204b5a280c0SLe Tan }
205b5a280c0SLe Tan 
206bacabb0aSJason Wang static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
207d66b969bSJason Wang                                   uint32_t level)
208d66b969bSJason Wang {
209d66b969bSJason Wang     return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) |
210d66b969bSJason Wang            ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT);
211d66b969bSJason Wang }
212d66b969bSJason Wang 
213d66b969bSJason Wang static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
214d66b969bSJason Wang {
215d66b969bSJason Wang     return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
216d66b969bSJason Wang }
217d66b969bSJason Wang 
218b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
219b5a280c0SLe Tan                                        hwaddr addr)
220b5a280c0SLe Tan {
221d66b969bSJason Wang     VTDIOTLBEntry *entry;
222b5a280c0SLe Tan     uint64_t key;
223d66b969bSJason Wang     int level;
224b5a280c0SLe Tan 
225d66b969bSJason Wang     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
226d66b969bSJason Wang         key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
227d66b969bSJason Wang                                 source_id, level);
228d66b969bSJason Wang         entry = g_hash_table_lookup(s->iotlb, &key);
229d66b969bSJason Wang         if (entry) {
230d66b969bSJason Wang             goto out;
231d66b969bSJason Wang         }
232d66b969bSJason Wang     }
233b5a280c0SLe Tan 
234d66b969bSJason Wang out:
235d66b969bSJason Wang     return entry;
236b5a280c0SLe Tan }
237b5a280c0SLe Tan 
238b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
239b5a280c0SLe Tan                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
240d66b969bSJason Wang                              bool read_flags, bool write_flags,
241d66b969bSJason Wang                              uint32_t level)
242b5a280c0SLe Tan {
243b5a280c0SLe Tan     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
244b5a280c0SLe Tan     uint64_t *key = g_malloc(sizeof(*key));
245d66b969bSJason Wang     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
246b5a280c0SLe Tan 
2476c441e1dSPeter Xu     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
248b5a280c0SLe Tan     if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
2496c441e1dSPeter Xu         trace_vtd_iotlb_reset("iotlb exceeds size limit");
250b5a280c0SLe Tan         vtd_reset_iotlb(s);
251b5a280c0SLe Tan     }
252b5a280c0SLe Tan 
253b5a280c0SLe Tan     entry->gfn = gfn;
254b5a280c0SLe Tan     entry->domain_id = domain_id;
255b5a280c0SLe Tan     entry->slpte = slpte;
256b5a280c0SLe Tan     entry->read_flags = read_flags;
257b5a280c0SLe Tan     entry->write_flags = write_flags;
258d66b969bSJason Wang     entry->mask = vtd_slpt_level_page_mask(level);
259d66b969bSJason Wang     *key = vtd_get_iotlb_key(gfn, source_id, level);
260b5a280c0SLe Tan     g_hash_table_replace(s->iotlb, key, entry);
261b5a280c0SLe Tan }
262b5a280c0SLe Tan 
2631da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an
2641da12ec4SLe Tan  * interrupt via MSI.
2651da12ec4SLe Tan  */
2661da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
2671da12ec4SLe Tan                                    hwaddr mesg_data_reg)
2681da12ec4SLe Tan {
26932946019SRadim Krčmář     MSIMessage msi;
2701da12ec4SLe Tan 
2711da12ec4SLe Tan     assert(mesg_data_reg < DMAR_REG_SIZE);
2721da12ec4SLe Tan     assert(mesg_addr_reg < DMAR_REG_SIZE);
2731da12ec4SLe Tan 
27432946019SRadim Krčmář     msi.address = vtd_get_long_raw(s, mesg_addr_reg);
27532946019SRadim Krčmář     msi.data = vtd_get_long_raw(s, mesg_data_reg);
2761da12ec4SLe Tan 
2777feb51b7SPeter Xu     trace_vtd_irq_generate(msi.address, msi.data);
2787feb51b7SPeter Xu 
27932946019SRadim Krčmář     apic_get_class()->send_msi(&msi);
2801da12ec4SLe Tan }
2811da12ec4SLe Tan 
2821da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met.
2831da12ec4SLe Tan  * Notice that the value of FSTS_REG being passed to it should be the one
2841da12ec4SLe Tan  * before any update.
2851da12ec4SLe Tan  */
2861da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
2871da12ec4SLe Tan {
2881da12ec4SLe Tan     if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
2891da12ec4SLe Tan         pre_fsts & VTD_FSTS_IQE) {
2907feb51b7SPeter Xu         trace_vtd_err("There are previous interrupt conditions "
2917feb51b7SPeter Xu                       "to be serviced by software, fault event "
2927feb51b7SPeter Xu                       "is not generated.");
2931da12ec4SLe Tan         return;
2941da12ec4SLe Tan     }
2951da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
2961da12ec4SLe Tan     if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
2977feb51b7SPeter Xu         trace_vtd_err("Interrupt Mask set, irq is not generated.");
2981da12ec4SLe Tan     } else {
2991da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
3001da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
3011da12ec4SLe Tan     }
3021da12ec4SLe Tan }
3031da12ec4SLe Tan 
3041da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by
3051da12ec4SLe Tan  * @index is Set.
3061da12ec4SLe Tan  */
3071da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
3081da12ec4SLe Tan {
3091da12ec4SLe Tan     /* Each reg is 128-bit */
3101da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
3111da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
3121da12ec4SLe Tan 
3131da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
3141da12ec4SLe Tan 
3151da12ec4SLe Tan     return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
3161da12ec4SLe Tan }
3171da12ec4SLe Tan 
3181da12ec4SLe Tan /* Update the PPF field of Fault Status Register.
3191da12ec4SLe Tan  * Should be called whenever change the F field of any fault recording
3201da12ec4SLe Tan  * registers.
3211da12ec4SLe Tan  */
3221da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s)
3231da12ec4SLe Tan {
3241da12ec4SLe Tan     uint32_t i;
3251da12ec4SLe Tan     uint32_t ppf_mask = 0;
3261da12ec4SLe Tan 
3271da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
3281da12ec4SLe Tan         if (vtd_is_frcd_set(s, i)) {
3291da12ec4SLe Tan             ppf_mask = VTD_FSTS_PPF;
3301da12ec4SLe Tan             break;
3311da12ec4SLe Tan         }
3321da12ec4SLe Tan     }
3331da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
3347feb51b7SPeter Xu     trace_vtd_fsts_ppf(!!ppf_mask);
3351da12ec4SLe Tan }
3361da12ec4SLe Tan 
3371da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
3381da12ec4SLe Tan {
3391da12ec4SLe Tan     /* Each reg is 128-bit */
3401da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
3411da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
3421da12ec4SLe Tan 
3431da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
3441da12ec4SLe Tan 
3451da12ec4SLe Tan     vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
3461da12ec4SLe Tan     vtd_update_fsts_ppf(s);
3471da12ec4SLe Tan }
3481da12ec4SLe Tan 
3491da12ec4SLe Tan /* Must not update F field now, should be done later */
3501da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
3511da12ec4SLe Tan                             uint16_t source_id, hwaddr addr,
3521da12ec4SLe Tan                             VTDFaultReason fault, bool is_write)
3531da12ec4SLe Tan {
3541da12ec4SLe Tan     uint64_t hi = 0, lo;
3551da12ec4SLe Tan     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
3561da12ec4SLe Tan 
3571da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
3581da12ec4SLe Tan 
3591da12ec4SLe Tan     lo = VTD_FRCD_FI(addr);
3601da12ec4SLe Tan     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
3611da12ec4SLe Tan     if (!is_write) {
3621da12ec4SLe Tan         hi |= VTD_FRCD_T;
3631da12ec4SLe Tan     }
3641da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr, lo);
3651da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
3667feb51b7SPeter Xu 
3677feb51b7SPeter Xu     trace_vtd_frr_new(index, hi, lo);
3681da12ec4SLe Tan }
3691da12ec4SLe Tan 
3701da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */
3711da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
3721da12ec4SLe Tan {
3731da12ec4SLe Tan     uint32_t i;
3741da12ec4SLe Tan     uint64_t frcd_reg;
3751da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
3761da12ec4SLe Tan 
3771da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
3781da12ec4SLe Tan         frcd_reg = vtd_get_quad_raw(s, addr);
3791da12ec4SLe Tan         if ((frcd_reg & VTD_FRCD_F) &&
3801da12ec4SLe Tan             ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
3811da12ec4SLe Tan             return true;
3821da12ec4SLe Tan         }
3831da12ec4SLe Tan         addr += 16; /* 128-bit for each */
3841da12ec4SLe Tan     }
3851da12ec4SLe Tan     return false;
3861da12ec4SLe Tan }
3871da12ec4SLe Tan 
3881da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */
3891da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
3901da12ec4SLe Tan                                   hwaddr addr, VTDFaultReason fault,
3911da12ec4SLe Tan                                   bool is_write)
3921da12ec4SLe Tan {
3931da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
3941da12ec4SLe Tan 
3951da12ec4SLe Tan     assert(fault < VTD_FR_MAX);
3961da12ec4SLe Tan 
3971da12ec4SLe Tan     if (fault == VTD_FR_RESERVED_ERR) {
3981da12ec4SLe Tan         /* This is not a normal fault reason case. Drop it. */
3991da12ec4SLe Tan         return;
4001da12ec4SLe Tan     }
4017feb51b7SPeter Xu 
4027feb51b7SPeter Xu     trace_vtd_dmar_fault(source_id, fault, addr, is_write);
4037feb51b7SPeter Xu 
4041da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PFO) {
4057feb51b7SPeter Xu         trace_vtd_err("New fault is not recorded due to "
4067feb51b7SPeter Xu                       "Primary Fault Overflow.");
4071da12ec4SLe Tan         return;
4081da12ec4SLe Tan     }
4097feb51b7SPeter Xu 
4101da12ec4SLe Tan     if (vtd_try_collapse_fault(s, source_id)) {
4117feb51b7SPeter Xu         trace_vtd_err("New fault is not recorded due to "
4127feb51b7SPeter Xu                       "compression of faults.");
4131da12ec4SLe Tan         return;
4141da12ec4SLe Tan     }
4157feb51b7SPeter Xu 
4161da12ec4SLe Tan     if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
4177feb51b7SPeter Xu         trace_vtd_err("Next Fault Recording Reg is used, "
4187feb51b7SPeter Xu                       "new fault is not recorded, set PFO field.");
4191da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
4201da12ec4SLe Tan         return;
4211da12ec4SLe Tan     }
4221da12ec4SLe Tan 
4231da12ec4SLe Tan     vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
4241da12ec4SLe Tan 
4251da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PPF) {
4267feb51b7SPeter Xu         trace_vtd_err("There are pending faults already, "
4277feb51b7SPeter Xu                       "fault event is not generated.");
4281da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
4291da12ec4SLe Tan         s->next_frcd_reg++;
4301da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
4311da12ec4SLe Tan             s->next_frcd_reg = 0;
4321da12ec4SLe Tan         }
4331da12ec4SLe Tan     } else {
4341da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
4351da12ec4SLe Tan                                 VTD_FSTS_FRI(s->next_frcd_reg));
4361da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
4371da12ec4SLe Tan         s->next_frcd_reg++;
4381da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
4391da12ec4SLe Tan             s->next_frcd_reg = 0;
4401da12ec4SLe Tan         }
4411da12ec4SLe Tan         /* This case actually cause the PPF to be Set.
4421da12ec4SLe Tan          * So generate fault event (interrupt).
4431da12ec4SLe Tan          */
4441da12ec4SLe Tan          vtd_generate_fault_event(s, fsts_reg);
4451da12ec4SLe Tan     }
4461da12ec4SLe Tan }
4471da12ec4SLe Tan 
448ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error
449ed7b8fbcSLe Tan  * conditions.
450ed7b8fbcSLe Tan  */
451ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
452ed7b8fbcSLe Tan {
453ed7b8fbcSLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
454ed7b8fbcSLe Tan 
455ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
456ed7b8fbcSLe Tan     vtd_generate_fault_event(s, fsts_reg);
457ed7b8fbcSLe Tan }
458ed7b8fbcSLe Tan 
459ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */
460ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s)
461ed7b8fbcSLe Tan {
462ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
463bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("One pending, skip current");
464ed7b8fbcSLe Tan         return;
465ed7b8fbcSLe Tan     }
466ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
467ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
468ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
469bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
470bc535e59SPeter Xu                                     "new event not generated");
471ed7b8fbcSLe Tan         return;
472ed7b8fbcSLe Tan     } else {
473ed7b8fbcSLe Tan         /* Generate the interrupt event */
474bc535e59SPeter Xu         trace_vtd_inv_desc_wait_irq("Generating complete event");
475ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
476ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
477ed7b8fbcSLe Tan     }
478ed7b8fbcSLe Tan }
479ed7b8fbcSLe Tan 
4801da12ec4SLe Tan static inline bool vtd_root_entry_present(VTDRootEntry *root)
4811da12ec4SLe Tan {
4821da12ec4SLe Tan     return root->val & VTD_ROOT_ENTRY_P;
4831da12ec4SLe Tan }
4841da12ec4SLe Tan 
4851da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
4861da12ec4SLe Tan                               VTDRootEntry *re)
4871da12ec4SLe Tan {
4881da12ec4SLe Tan     dma_addr_t addr;
4891da12ec4SLe Tan 
4901da12ec4SLe Tan     addr = s->root + index * sizeof(*re);
4911da12ec4SLe Tan     if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) {
4926c441e1dSPeter Xu         trace_vtd_re_invalid(re->rsvd, re->val);
4931da12ec4SLe Tan         re->val = 0;
4941da12ec4SLe Tan         return -VTD_FR_ROOT_TABLE_INV;
4951da12ec4SLe Tan     }
4961da12ec4SLe Tan     re->val = le64_to_cpu(re->val);
4971da12ec4SLe Tan     return 0;
4981da12ec4SLe Tan }
4991da12ec4SLe Tan 
5008f7d7161SPeter Xu static inline bool vtd_ce_present(VTDContextEntry *context)
5011da12ec4SLe Tan {
5021da12ec4SLe Tan     return context->lo & VTD_CONTEXT_ENTRY_P;
5031da12ec4SLe Tan }
5041da12ec4SLe Tan 
5051da12ec4SLe Tan static int vtd_get_context_entry_from_root(VTDRootEntry *root, uint8_t index,
5061da12ec4SLe Tan                                            VTDContextEntry *ce)
5071da12ec4SLe Tan {
5081da12ec4SLe Tan     dma_addr_t addr;
5091da12ec4SLe Tan 
5106c441e1dSPeter Xu     /* we have checked that root entry is present */
5111da12ec4SLe Tan     addr = (root->val & VTD_ROOT_ENTRY_CTP) + index * sizeof(*ce);
5121da12ec4SLe Tan     if (dma_memory_read(&address_space_memory, addr, ce, sizeof(*ce))) {
5136c441e1dSPeter Xu         trace_vtd_re_invalid(root->rsvd, root->val);
5141da12ec4SLe Tan         return -VTD_FR_CONTEXT_TABLE_INV;
5151da12ec4SLe Tan     }
5161da12ec4SLe Tan     ce->lo = le64_to_cpu(ce->lo);
5171da12ec4SLe Tan     ce->hi = le64_to_cpu(ce->hi);
5181da12ec4SLe Tan     return 0;
5191da12ec4SLe Tan }
5201da12ec4SLe Tan 
5218f7d7161SPeter Xu static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
5221da12ec4SLe Tan {
5231da12ec4SLe Tan     return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
5241da12ec4SLe Tan }
5251da12ec4SLe Tan 
5261da12ec4SLe Tan static inline uint64_t vtd_get_slpte_addr(uint64_t slpte)
5271da12ec4SLe Tan {
5281da12ec4SLe Tan     return slpte & VTD_SL_PT_BASE_ADDR_MASK;
5291da12ec4SLe Tan }
5301da12ec4SLe Tan 
5311da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */
5321da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
5331da12ec4SLe Tan {
5341da12ec4SLe Tan     return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
5351da12ec4SLe Tan }
5361da12ec4SLe Tan 
5371da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */
5381da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
5391da12ec4SLe Tan {
5401da12ec4SLe Tan     uint64_t slpte;
5411da12ec4SLe Tan 
5421da12ec4SLe Tan     assert(index < VTD_SL_PT_ENTRY_NR);
5431da12ec4SLe Tan 
5441da12ec4SLe Tan     if (dma_memory_read(&address_space_memory,
5451da12ec4SLe Tan                         base_addr + index * sizeof(slpte), &slpte,
5461da12ec4SLe Tan                         sizeof(slpte))) {
5471da12ec4SLe Tan         slpte = (uint64_t)-1;
5481da12ec4SLe Tan         return slpte;
5491da12ec4SLe Tan     }
5501da12ec4SLe Tan     slpte = le64_to_cpu(slpte);
5511da12ec4SLe Tan     return slpte;
5521da12ec4SLe Tan }
5531da12ec4SLe Tan 
5546e905564SPeter Xu /* Given an iova and the level of paging structure, return the offset
5556e905564SPeter Xu  * of current level.
5561da12ec4SLe Tan  */
5576e905564SPeter Xu static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
5581da12ec4SLe Tan {
5596e905564SPeter Xu     return (iova >> vtd_slpt_level_shift(level)) &
5601da12ec4SLe Tan             ((1ULL << VTD_SL_LEVEL_BITS) - 1);
5611da12ec4SLe Tan }
5621da12ec4SLe Tan 
5631da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */
5641da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
5651da12ec4SLe Tan {
5661da12ec4SLe Tan     return VTD_CAP_SAGAW_MASK & s->cap &
5671da12ec4SLe Tan            (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
5681da12ec4SLe Tan }
5691da12ec4SLe Tan 
5701da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level
5711da12ec4SLe Tan  * page-table walk from the Address Width field of context-entry.
5721da12ec4SLe Tan  */
5738f7d7161SPeter Xu static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
5741da12ec4SLe Tan {
5751da12ec4SLe Tan     return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
5761da12ec4SLe Tan }
5771da12ec4SLe Tan 
5788f7d7161SPeter Xu static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
5791da12ec4SLe Tan {
5801da12ec4SLe Tan     return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
5811da12ec4SLe Tan }
5821da12ec4SLe Tan 
583127ff5c3SPeter Xu static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
584127ff5c3SPeter Xu {
585127ff5c3SPeter Xu     return ce->lo & VTD_CONTEXT_ENTRY_TT;
586127ff5c3SPeter Xu }
587127ff5c3SPeter Xu 
588f80c9874SPeter Xu /* Return true if check passed, otherwise false */
589f80c9874SPeter Xu static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
590f80c9874SPeter Xu                                      VTDContextEntry *ce)
591f80c9874SPeter Xu {
592f80c9874SPeter Xu     switch (vtd_ce_get_type(ce)) {
593f80c9874SPeter Xu     case VTD_CONTEXT_TT_MULTI_LEVEL:
594f80c9874SPeter Xu         /* Always supported */
595f80c9874SPeter Xu         break;
596f80c9874SPeter Xu     case VTD_CONTEXT_TT_DEV_IOTLB:
597f80c9874SPeter Xu         if (!x86_iommu->dt_supported) {
598f80c9874SPeter Xu             return false;
599f80c9874SPeter Xu         }
600f80c9874SPeter Xu         break;
601dbaabb25SPeter Xu     case VTD_CONTEXT_TT_PASS_THROUGH:
602dbaabb25SPeter Xu         if (!x86_iommu->pt_supported) {
603dbaabb25SPeter Xu             return false;
604dbaabb25SPeter Xu         }
605dbaabb25SPeter Xu         break;
606f80c9874SPeter Xu     default:
607f80c9874SPeter Xu         /* Unknwon type */
608f80c9874SPeter Xu         return false;
609f80c9874SPeter Xu     }
610f80c9874SPeter Xu     return true;
611f80c9874SPeter Xu }
612f80c9874SPeter Xu 
613f06a696dSPeter Xu static inline uint64_t vtd_iova_limit(VTDContextEntry *ce)
614f06a696dSPeter Xu {
6158f7d7161SPeter Xu     uint32_t ce_agaw = vtd_ce_get_agaw(ce);
616f06a696dSPeter Xu     return 1ULL << MIN(ce_agaw, VTD_MGAW);
617f06a696dSPeter Xu }
618f06a696dSPeter Xu 
619f06a696dSPeter Xu /* Return true if IOVA passes range check, otherwise false. */
620f06a696dSPeter Xu static inline bool vtd_iova_range_check(uint64_t iova, VTDContextEntry *ce)
621f06a696dSPeter Xu {
622f06a696dSPeter Xu     /*
623f06a696dSPeter Xu      * Check if @iova is above 2^X-1, where X is the minimum of MGAW
624f06a696dSPeter Xu      * in CAP_REG and AW in context-entry.
625f06a696dSPeter Xu      */
626f06a696dSPeter Xu     return !(iova & ~(vtd_iova_limit(ce) - 1));
627f06a696dSPeter Xu }
628f06a696dSPeter Xu 
6291da12ec4SLe Tan static const uint64_t vtd_paging_entry_rsvd_field[] = {
6301da12ec4SLe Tan     [0] = ~0ULL,
6311da12ec4SLe Tan     /* For not large page */
6321da12ec4SLe Tan     [1] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6331da12ec4SLe Tan     [2] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6341da12ec4SLe Tan     [3] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6351da12ec4SLe Tan     [4] = 0x880ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6361da12ec4SLe Tan     /* For large page */
6371da12ec4SLe Tan     [5] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6381da12ec4SLe Tan     [6] = 0x1ff800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6391da12ec4SLe Tan     [7] = 0x3ffff800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6401da12ec4SLe Tan     [8] = 0x880ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
6411da12ec4SLe Tan };
6421da12ec4SLe Tan 
6431da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
6441da12ec4SLe Tan {
6451da12ec4SLe Tan     if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) {
6461da12ec4SLe Tan         /* Maybe large page */
6471da12ec4SLe Tan         return slpte & vtd_paging_entry_rsvd_field[level + 4];
6481da12ec4SLe Tan     } else {
6491da12ec4SLe Tan         return slpte & vtd_paging_entry_rsvd_field[level];
6501da12ec4SLe Tan     }
6511da12ec4SLe Tan }
6521da12ec4SLe Tan 
653dbaabb25SPeter Xu /* Find the VTD address space associated with a given bus number */
654dbaabb25SPeter Xu static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
655dbaabb25SPeter Xu {
656dbaabb25SPeter Xu     VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];
657dbaabb25SPeter Xu     if (!vtd_bus) {
658dbaabb25SPeter Xu         /*
659dbaabb25SPeter Xu          * Iterate over the registered buses to find the one which
660dbaabb25SPeter Xu          * currently hold this bus number, and update the bus_num
661dbaabb25SPeter Xu          * lookup table:
662dbaabb25SPeter Xu          */
663dbaabb25SPeter Xu         GHashTableIter iter;
664dbaabb25SPeter Xu 
665dbaabb25SPeter Xu         g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
666dbaabb25SPeter Xu         while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
667dbaabb25SPeter Xu             if (pci_bus_num(vtd_bus->bus) == bus_num) {
668dbaabb25SPeter Xu                 s->vtd_as_by_bus_num[bus_num] = vtd_bus;
669dbaabb25SPeter Xu                 return vtd_bus;
670dbaabb25SPeter Xu             }
671dbaabb25SPeter Xu         }
672dbaabb25SPeter Xu     }
673dbaabb25SPeter Xu     return vtd_bus;
674dbaabb25SPeter Xu }
675dbaabb25SPeter Xu 
6766e905564SPeter Xu /* Given the @iova, get relevant @slptep. @slpte_level will be the last level
6771da12ec4SLe Tan  * of the translation, can be used for deciding the size of large page.
6781da12ec4SLe Tan  */
6796e905564SPeter Xu static int vtd_iova_to_slpte(VTDContextEntry *ce, uint64_t iova, bool is_write,
6801da12ec4SLe Tan                              uint64_t *slptep, uint32_t *slpte_level,
6811da12ec4SLe Tan                              bool *reads, bool *writes)
6821da12ec4SLe Tan {
6838f7d7161SPeter Xu     dma_addr_t addr = vtd_ce_get_slpt_base(ce);
6848f7d7161SPeter Xu     uint32_t level = vtd_ce_get_level(ce);
6851da12ec4SLe Tan     uint32_t offset;
6861da12ec4SLe Tan     uint64_t slpte;
6871da12ec4SLe Tan     uint64_t access_right_check;
6881da12ec4SLe Tan 
689f06a696dSPeter Xu     if (!vtd_iova_range_check(iova, ce)) {
6907feb51b7SPeter Xu         trace_vtd_err_dmar_iova_overflow(iova);
6911da12ec4SLe Tan         return -VTD_FR_ADDR_BEYOND_MGAW;
6921da12ec4SLe Tan     }
6931da12ec4SLe Tan 
6941da12ec4SLe Tan     /* FIXME: what is the Atomics request here? */
6951da12ec4SLe Tan     access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
6961da12ec4SLe Tan 
6971da12ec4SLe Tan     while (true) {
6986e905564SPeter Xu         offset = vtd_iova_level_offset(iova, level);
6991da12ec4SLe Tan         slpte = vtd_get_slpte(addr, offset);
7001da12ec4SLe Tan 
7011da12ec4SLe Tan         if (slpte == (uint64_t)-1) {
7027feb51b7SPeter Xu             trace_vtd_err_dmar_slpte_read_error(iova, level);
7038f7d7161SPeter Xu             if (level == vtd_ce_get_level(ce)) {
7041da12ec4SLe Tan                 /* Invalid programming of context-entry */
7051da12ec4SLe Tan                 return -VTD_FR_CONTEXT_ENTRY_INV;
7061da12ec4SLe Tan             } else {
7071da12ec4SLe Tan                 return -VTD_FR_PAGING_ENTRY_INV;
7081da12ec4SLe Tan             }
7091da12ec4SLe Tan         }
7101da12ec4SLe Tan         *reads = (*reads) && (slpte & VTD_SL_R);
7111da12ec4SLe Tan         *writes = (*writes) && (slpte & VTD_SL_W);
7121da12ec4SLe Tan         if (!(slpte & access_right_check)) {
7137feb51b7SPeter Xu             trace_vtd_err_dmar_slpte_perm_error(iova, level, slpte, is_write);
7141da12ec4SLe Tan             return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
7151da12ec4SLe Tan         }
7161da12ec4SLe Tan         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
7177feb51b7SPeter Xu             trace_vtd_err_dmar_slpte_resv_error(iova, level, slpte);
7181da12ec4SLe Tan             return -VTD_FR_PAGING_ENTRY_RSVD;
7191da12ec4SLe Tan         }
7201da12ec4SLe Tan 
7211da12ec4SLe Tan         if (vtd_is_last_slpte(slpte, level)) {
7221da12ec4SLe Tan             *slptep = slpte;
7231da12ec4SLe Tan             *slpte_level = level;
7241da12ec4SLe Tan             return 0;
7251da12ec4SLe Tan         }
7261da12ec4SLe Tan         addr = vtd_get_slpte_addr(slpte);
7271da12ec4SLe Tan         level--;
7281da12ec4SLe Tan     }
7291da12ec4SLe Tan }
7301da12ec4SLe Tan 
731f06a696dSPeter Xu typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
732f06a696dSPeter Xu 
733f06a696dSPeter Xu /**
734f06a696dSPeter Xu  * vtd_page_walk_level - walk over specific level for IOVA range
735f06a696dSPeter Xu  *
736f06a696dSPeter Xu  * @addr: base GPA addr to start the walk
737f06a696dSPeter Xu  * @start: IOVA range start address
738f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
739f06a696dSPeter Xu  * @hook_fn: hook func to be called when detected page
740f06a696dSPeter Xu  * @private: private data to be passed into hook func
741f06a696dSPeter Xu  * @read: whether parent level has read permission
742f06a696dSPeter Xu  * @write: whether parent level has write permission
743f06a696dSPeter Xu  * @notify_unmap: whether we should notify invalid entries
744f06a696dSPeter Xu  */
745f06a696dSPeter Xu static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
746f06a696dSPeter Xu                                uint64_t end, vtd_page_walk_hook hook_fn,
747f06a696dSPeter Xu                                void *private, uint32_t level,
748f06a696dSPeter Xu                                bool read, bool write, bool notify_unmap)
749f06a696dSPeter Xu {
750f06a696dSPeter Xu     bool read_cur, write_cur, entry_valid;
751f06a696dSPeter Xu     uint32_t offset;
752f06a696dSPeter Xu     uint64_t slpte;
753f06a696dSPeter Xu     uint64_t subpage_size, subpage_mask;
754f06a696dSPeter Xu     IOMMUTLBEntry entry;
755f06a696dSPeter Xu     uint64_t iova = start;
756f06a696dSPeter Xu     uint64_t iova_next;
757f06a696dSPeter Xu     int ret = 0;
758f06a696dSPeter Xu 
759f06a696dSPeter Xu     trace_vtd_page_walk_level(addr, level, start, end);
760f06a696dSPeter Xu 
761f06a696dSPeter Xu     subpage_size = 1ULL << vtd_slpt_level_shift(level);
762f06a696dSPeter Xu     subpage_mask = vtd_slpt_level_page_mask(level);
763f06a696dSPeter Xu 
764f06a696dSPeter Xu     while (iova < end) {
765f06a696dSPeter Xu         iova_next = (iova & subpage_mask) + subpage_size;
766f06a696dSPeter Xu 
767f06a696dSPeter Xu         offset = vtd_iova_level_offset(iova, level);
768f06a696dSPeter Xu         slpte = vtd_get_slpte(addr, offset);
769f06a696dSPeter Xu 
770f06a696dSPeter Xu         if (slpte == (uint64_t)-1) {
771f06a696dSPeter Xu             trace_vtd_page_walk_skip_read(iova, iova_next);
772f06a696dSPeter Xu             goto next;
773f06a696dSPeter Xu         }
774f06a696dSPeter Xu 
775f06a696dSPeter Xu         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
776f06a696dSPeter Xu             trace_vtd_page_walk_skip_reserve(iova, iova_next);
777f06a696dSPeter Xu             goto next;
778f06a696dSPeter Xu         }
779f06a696dSPeter Xu 
780f06a696dSPeter Xu         /* Permissions are stacked with parents' */
781f06a696dSPeter Xu         read_cur = read && (slpte & VTD_SL_R);
782f06a696dSPeter Xu         write_cur = write && (slpte & VTD_SL_W);
783f06a696dSPeter Xu 
784f06a696dSPeter Xu         /*
785f06a696dSPeter Xu          * As long as we have either read/write permission, this is a
786f06a696dSPeter Xu          * valid entry. The rule works for both page entries and page
787f06a696dSPeter Xu          * table entries.
788f06a696dSPeter Xu          */
789f06a696dSPeter Xu         entry_valid = read_cur | write_cur;
790f06a696dSPeter Xu 
791f06a696dSPeter Xu         if (vtd_is_last_slpte(slpte, level)) {
792f06a696dSPeter Xu             entry.target_as = &address_space_memory;
793f06a696dSPeter Xu             entry.iova = iova & subpage_mask;
794f06a696dSPeter Xu             /* NOTE: this is only meaningful if entry_valid == true */
795f06a696dSPeter Xu             entry.translated_addr = vtd_get_slpte_addr(slpte);
796f06a696dSPeter Xu             entry.addr_mask = ~subpage_mask;
797f06a696dSPeter Xu             entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
798f06a696dSPeter Xu             if (!entry_valid && !notify_unmap) {
799f06a696dSPeter Xu                 trace_vtd_page_walk_skip_perm(iova, iova_next);
800f06a696dSPeter Xu                 goto next;
801f06a696dSPeter Xu             }
802f06a696dSPeter Xu             trace_vtd_page_walk_one(level, entry.iova, entry.translated_addr,
803f06a696dSPeter Xu                                     entry.addr_mask, entry.perm);
804f06a696dSPeter Xu             if (hook_fn) {
805f06a696dSPeter Xu                 ret = hook_fn(&entry, private);
806f06a696dSPeter Xu                 if (ret < 0) {
807f06a696dSPeter Xu                     return ret;
808f06a696dSPeter Xu                 }
809f06a696dSPeter Xu             }
810f06a696dSPeter Xu         } else {
811f06a696dSPeter Xu             if (!entry_valid) {
812f06a696dSPeter Xu                 trace_vtd_page_walk_skip_perm(iova, iova_next);
813f06a696dSPeter Xu                 goto next;
814f06a696dSPeter Xu             }
815f06a696dSPeter Xu             ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte), iova,
816f06a696dSPeter Xu                                       MIN(iova_next, end), hook_fn, private,
817f06a696dSPeter Xu                                       level - 1, read_cur, write_cur,
818f06a696dSPeter Xu                                       notify_unmap);
819f06a696dSPeter Xu             if (ret < 0) {
820f06a696dSPeter Xu                 return ret;
821f06a696dSPeter Xu             }
822f06a696dSPeter Xu         }
823f06a696dSPeter Xu 
824f06a696dSPeter Xu next:
825f06a696dSPeter Xu         iova = iova_next;
826f06a696dSPeter Xu     }
827f06a696dSPeter Xu 
828f06a696dSPeter Xu     return 0;
829f06a696dSPeter Xu }
830f06a696dSPeter Xu 
831f06a696dSPeter Xu /**
832f06a696dSPeter Xu  * vtd_page_walk - walk specific IOVA range, and call the hook
833f06a696dSPeter Xu  *
834f06a696dSPeter Xu  * @ce: context entry to walk upon
835f06a696dSPeter Xu  * @start: IOVA address to start the walk
836f06a696dSPeter Xu  * @end: IOVA range end address (start <= addr < end)
837f06a696dSPeter Xu  * @hook_fn: the hook that to be called for each detected area
838f06a696dSPeter Xu  * @private: private data for the hook function
839f06a696dSPeter Xu  */
840f06a696dSPeter Xu static int vtd_page_walk(VTDContextEntry *ce, uint64_t start, uint64_t end,
841dd4d607eSPeter Xu                          vtd_page_walk_hook hook_fn, void *private,
842dd4d607eSPeter Xu                          bool notify_unmap)
843f06a696dSPeter Xu {
8448f7d7161SPeter Xu     dma_addr_t addr = vtd_ce_get_slpt_base(ce);
8458f7d7161SPeter Xu     uint32_t level = vtd_ce_get_level(ce);
846f06a696dSPeter Xu 
847f06a696dSPeter Xu     if (!vtd_iova_range_check(start, ce)) {
848f06a696dSPeter Xu         return -VTD_FR_ADDR_BEYOND_MGAW;
849f06a696dSPeter Xu     }
850f06a696dSPeter Xu 
851f06a696dSPeter Xu     if (!vtd_iova_range_check(end, ce)) {
852f06a696dSPeter Xu         /* Fix end so that it reaches the maximum */
853f06a696dSPeter Xu         end = vtd_iova_limit(ce);
854f06a696dSPeter Xu     }
855f06a696dSPeter Xu 
856f06a696dSPeter Xu     return vtd_page_walk_level(addr, start, end, hook_fn, private,
857dd4d607eSPeter Xu                                level, true, true, notify_unmap);
858f06a696dSPeter Xu }
859f06a696dSPeter Xu 
8601da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */
8611da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
8621da12ec4SLe Tan                                     uint8_t devfn, VTDContextEntry *ce)
8631da12ec4SLe Tan {
8641da12ec4SLe Tan     VTDRootEntry re;
8651da12ec4SLe Tan     int ret_fr;
866f80c9874SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
8671da12ec4SLe Tan 
8681da12ec4SLe Tan     ret_fr = vtd_get_root_entry(s, bus_num, &re);
8691da12ec4SLe Tan     if (ret_fr) {
8701da12ec4SLe Tan         return ret_fr;
8711da12ec4SLe Tan     }
8721da12ec4SLe Tan 
8731da12ec4SLe Tan     if (!vtd_root_entry_present(&re)) {
8746c441e1dSPeter Xu         /* Not error - it's okay we don't have root entry. */
8756c441e1dSPeter Xu         trace_vtd_re_not_present(bus_num);
8761da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_P;
877f80c9874SPeter Xu     }
878f80c9874SPeter Xu 
879f80c9874SPeter Xu     if (re.rsvd || (re.val & VTD_ROOT_ENTRY_RSVD)) {
8806c441e1dSPeter Xu         trace_vtd_re_invalid(re.rsvd, re.val);
8811da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_RSVD;
8821da12ec4SLe Tan     }
8831da12ec4SLe Tan 
8841da12ec4SLe Tan     ret_fr = vtd_get_context_entry_from_root(&re, devfn, ce);
8851da12ec4SLe Tan     if (ret_fr) {
8861da12ec4SLe Tan         return ret_fr;
8871da12ec4SLe Tan     }
8881da12ec4SLe Tan 
8898f7d7161SPeter Xu     if (!vtd_ce_present(ce)) {
8906c441e1dSPeter Xu         /* Not error - it's okay we don't have context entry. */
8916c441e1dSPeter Xu         trace_vtd_ce_not_present(bus_num, devfn);
8921da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_P;
893f80c9874SPeter Xu     }
894f80c9874SPeter Xu 
895f80c9874SPeter Xu     if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
8961da12ec4SLe Tan         (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
8976c441e1dSPeter Xu         trace_vtd_ce_invalid(ce->hi, ce->lo);
8981da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_RSVD;
8991da12ec4SLe Tan     }
900f80c9874SPeter Xu 
9011da12ec4SLe Tan     /* Check if the programming of context-entry is valid */
9028f7d7161SPeter Xu     if (!vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
9036c441e1dSPeter Xu         trace_vtd_ce_invalid(ce->hi, ce->lo);
9041da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
905f80c9874SPeter Xu     }
906f80c9874SPeter Xu 
907f80c9874SPeter Xu     /* Do translation type check */
908f80c9874SPeter Xu     if (!vtd_ce_type_check(x86_iommu, ce)) {
9096c441e1dSPeter Xu         trace_vtd_ce_invalid(ce->hi, ce->lo);
9101da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
9111da12ec4SLe Tan     }
912f80c9874SPeter Xu 
9131da12ec4SLe Tan     return 0;
9141da12ec4SLe Tan }
9151da12ec4SLe Tan 
916dbaabb25SPeter Xu /*
917dbaabb25SPeter Xu  * Fetch translation type for specific device. Returns <0 if error
918dbaabb25SPeter Xu  * happens, otherwise return the shifted type to check against
919dbaabb25SPeter Xu  * VTD_CONTEXT_TT_*.
920dbaabb25SPeter Xu  */
921dbaabb25SPeter Xu static int vtd_dev_get_trans_type(VTDAddressSpace *as)
922dbaabb25SPeter Xu {
923dbaabb25SPeter Xu     IntelIOMMUState *s;
924dbaabb25SPeter Xu     VTDContextEntry ce;
925dbaabb25SPeter Xu     int ret;
926dbaabb25SPeter Xu 
927dbaabb25SPeter Xu     s = as->iommu_state;
928dbaabb25SPeter Xu 
929dbaabb25SPeter Xu     ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus),
930dbaabb25SPeter Xu                                    as->devfn, &ce);
931dbaabb25SPeter Xu     if (ret) {
932dbaabb25SPeter Xu         return ret;
933dbaabb25SPeter Xu     }
934dbaabb25SPeter Xu 
935dbaabb25SPeter Xu     return vtd_ce_get_type(&ce);
936dbaabb25SPeter Xu }
937dbaabb25SPeter Xu 
938dbaabb25SPeter Xu static bool vtd_dev_pt_enabled(VTDAddressSpace *as)
939dbaabb25SPeter Xu {
940dbaabb25SPeter Xu     int ret;
941dbaabb25SPeter Xu 
942dbaabb25SPeter Xu     assert(as);
943dbaabb25SPeter Xu 
944dbaabb25SPeter Xu     ret = vtd_dev_get_trans_type(as);
945dbaabb25SPeter Xu     if (ret < 0) {
946dbaabb25SPeter Xu         /*
947dbaabb25SPeter Xu          * Possibly failed to parse the context entry for some reason
948dbaabb25SPeter Xu          * (e.g., during init, or any guest configuration errors on
949dbaabb25SPeter Xu          * context entries). We should assume PT not enabled for
950dbaabb25SPeter Xu          * safety.
951dbaabb25SPeter Xu          */
952dbaabb25SPeter Xu         return false;
953dbaabb25SPeter Xu     }
954dbaabb25SPeter Xu 
955dbaabb25SPeter Xu     return ret == VTD_CONTEXT_TT_PASS_THROUGH;
956dbaabb25SPeter Xu }
957dbaabb25SPeter Xu 
958dbaabb25SPeter Xu /* Return whether the device is using IOMMU translation. */
959dbaabb25SPeter Xu static bool vtd_switch_address_space(VTDAddressSpace *as)
960dbaabb25SPeter Xu {
961dbaabb25SPeter Xu     bool use_iommu;
962dbaabb25SPeter Xu 
963dbaabb25SPeter Xu     assert(as);
964dbaabb25SPeter Xu 
965dbaabb25SPeter Xu     use_iommu = as->iommu_state->dmar_enabled & !vtd_dev_pt_enabled(as);
966dbaabb25SPeter Xu 
967dbaabb25SPeter Xu     trace_vtd_switch_address_space(pci_bus_num(as->bus),
968dbaabb25SPeter Xu                                    VTD_PCI_SLOT(as->devfn),
969dbaabb25SPeter Xu                                    VTD_PCI_FUNC(as->devfn),
970dbaabb25SPeter Xu                                    use_iommu);
971dbaabb25SPeter Xu 
972dbaabb25SPeter Xu     /* Turn off first then on the other */
973dbaabb25SPeter Xu     if (use_iommu) {
974dbaabb25SPeter Xu         memory_region_set_enabled(&as->sys_alias, false);
975dbaabb25SPeter Xu         memory_region_set_enabled(&as->iommu, true);
976dbaabb25SPeter Xu     } else {
977dbaabb25SPeter Xu         memory_region_set_enabled(&as->iommu, false);
978dbaabb25SPeter Xu         memory_region_set_enabled(&as->sys_alias, true);
979dbaabb25SPeter Xu     }
980dbaabb25SPeter Xu 
981dbaabb25SPeter Xu     return use_iommu;
982dbaabb25SPeter Xu }
983dbaabb25SPeter Xu 
984dbaabb25SPeter Xu static void vtd_switch_address_space_all(IntelIOMMUState *s)
985dbaabb25SPeter Xu {
986dbaabb25SPeter Xu     GHashTableIter iter;
987dbaabb25SPeter Xu     VTDBus *vtd_bus;
988dbaabb25SPeter Xu     int i;
989dbaabb25SPeter Xu 
990dbaabb25SPeter Xu     g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
991dbaabb25SPeter Xu     while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
992dbaabb25SPeter Xu         for (i = 0; i < X86_IOMMU_PCI_DEVFN_MAX; i++) {
993dbaabb25SPeter Xu             if (!vtd_bus->dev_as[i]) {
994dbaabb25SPeter Xu                 continue;
995dbaabb25SPeter Xu             }
996dbaabb25SPeter Xu             vtd_switch_address_space(vtd_bus->dev_as[i]);
997dbaabb25SPeter Xu         }
998dbaabb25SPeter Xu     }
999dbaabb25SPeter Xu }
1000dbaabb25SPeter Xu 
10011da12ec4SLe Tan static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
10021da12ec4SLe Tan {
10031da12ec4SLe Tan     return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
10041da12ec4SLe Tan }
10051da12ec4SLe Tan 
10061da12ec4SLe Tan static const bool vtd_qualified_faults[] = {
10071da12ec4SLe Tan     [VTD_FR_RESERVED] = false,
10081da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_P] = false,
10091da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_P] = true,
10101da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_INV] = true,
10111da12ec4SLe Tan     [VTD_FR_ADDR_BEYOND_MGAW] = true,
10121da12ec4SLe Tan     [VTD_FR_WRITE] = true,
10131da12ec4SLe Tan     [VTD_FR_READ] = true,
10141da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_INV] = true,
10151da12ec4SLe Tan     [VTD_FR_ROOT_TABLE_INV] = false,
10161da12ec4SLe Tan     [VTD_FR_CONTEXT_TABLE_INV] = false,
10171da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_RSVD] = false,
10181da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_RSVD] = true,
10191da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_TT] = true,
10201da12ec4SLe Tan     [VTD_FR_RESERVED_ERR] = false,
10211da12ec4SLe Tan     [VTD_FR_MAX] = false,
10221da12ec4SLe Tan };
10231da12ec4SLe Tan 
10241da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software
10251da12ec4SLe Tan  * only if the FPD field in the context-entry used to process the faulting
10261da12ec4SLe Tan  * request is 0.
10271da12ec4SLe Tan  */
10281da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
10291da12ec4SLe Tan {
10301da12ec4SLe Tan     return vtd_qualified_faults[fault];
10311da12ec4SLe Tan }
10321da12ec4SLe Tan 
10331da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr)
10341da12ec4SLe Tan {
10351da12ec4SLe Tan     return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
10361da12ec4SLe Tan }
10371da12ec4SLe Tan 
1038dbaabb25SPeter Xu static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1039dbaabb25SPeter Xu {
1040dbaabb25SPeter Xu     VTDBus *vtd_bus;
1041dbaabb25SPeter Xu     VTDAddressSpace *vtd_as;
1042dbaabb25SPeter Xu     bool success = false;
1043dbaabb25SPeter Xu 
1044dbaabb25SPeter Xu     vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id));
1045dbaabb25SPeter Xu     if (!vtd_bus) {
1046dbaabb25SPeter Xu         goto out;
1047dbaabb25SPeter Xu     }
1048dbaabb25SPeter Xu 
1049dbaabb25SPeter Xu     vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)];
1050dbaabb25SPeter Xu     if (!vtd_as) {
1051dbaabb25SPeter Xu         goto out;
1052dbaabb25SPeter Xu     }
1053dbaabb25SPeter Xu 
1054dbaabb25SPeter Xu     if (vtd_switch_address_space(vtd_as) == false) {
1055dbaabb25SPeter Xu         /* We switched off IOMMU region successfully. */
1056dbaabb25SPeter Xu         success = true;
1057dbaabb25SPeter Xu     }
1058dbaabb25SPeter Xu 
1059dbaabb25SPeter Xu out:
1060dbaabb25SPeter Xu     trace_vtd_pt_enable_fast_path(source_id, success);
1061dbaabb25SPeter Xu }
1062dbaabb25SPeter Xu 
10631da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu
10641da12ec4SLe Tan  * translation.
106579e2b9aeSPaolo Bonzini  *
106679e2b9aeSPaolo Bonzini  * Called from RCU critical section.
106779e2b9aeSPaolo Bonzini  *
10681da12ec4SLe Tan  * @bus_num: The bus number
10691da12ec4SLe Tan  * @devfn: The devfn, which is the  combined of device and function number
10701da12ec4SLe Tan  * @is_write: The access is a write operation
10711da12ec4SLe Tan  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
1072b9313021SPeter Xu  *
1073b9313021SPeter Xu  * Returns true if translation is successful, otherwise false.
10741da12ec4SLe Tan  */
1075b9313021SPeter Xu static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
10761da12ec4SLe Tan                                    uint8_t devfn, hwaddr addr, bool is_write,
10771da12ec4SLe Tan                                    IOMMUTLBEntry *entry)
10781da12ec4SLe Tan {
1079d92fa2dcSLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
10801da12ec4SLe Tan     VTDContextEntry ce;
10817df953bdSKnut Omang     uint8_t bus_num = pci_bus_num(bus);
1082d92fa2dcSLe Tan     VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
1083d66b969bSJason Wang     uint64_t slpte, page_mask;
10841da12ec4SLe Tan     uint32_t level;
10851da12ec4SLe Tan     uint16_t source_id = vtd_make_source_id(bus_num, devfn);
10861da12ec4SLe Tan     int ret_fr;
10871da12ec4SLe Tan     bool is_fpd_set = false;
10881da12ec4SLe Tan     bool reads = true;
10891da12ec4SLe Tan     bool writes = true;
1090b5a280c0SLe Tan     VTDIOTLBEntry *iotlb_entry;
10911da12ec4SLe Tan 
1092046ab7e9SPeter Xu     /*
1093046ab7e9SPeter Xu      * We have standalone memory region for interrupt addresses, we
1094046ab7e9SPeter Xu      * should never receive translation requests in this region.
10951da12ec4SLe Tan      */
1096046ab7e9SPeter Xu     assert(!vtd_is_interrupt_addr(addr));
1097046ab7e9SPeter Xu 
1098b5a280c0SLe Tan     /* Try to fetch slpte form IOTLB */
1099b5a280c0SLe Tan     iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
1100b5a280c0SLe Tan     if (iotlb_entry) {
11016c441e1dSPeter Xu         trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
11026c441e1dSPeter Xu                                  iotlb_entry->domain_id);
1103b5a280c0SLe Tan         slpte = iotlb_entry->slpte;
1104b5a280c0SLe Tan         reads = iotlb_entry->read_flags;
1105b5a280c0SLe Tan         writes = iotlb_entry->write_flags;
1106d66b969bSJason Wang         page_mask = iotlb_entry->mask;
1107b5a280c0SLe Tan         goto out;
1108b5a280c0SLe Tan     }
1109b9313021SPeter Xu 
1110d92fa2dcSLe Tan     /* Try to fetch context-entry from cache first */
1111d92fa2dcSLe Tan     if (cc_entry->context_cache_gen == s->context_cache_gen) {
11126c441e1dSPeter Xu         trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
11136c441e1dSPeter Xu                                cc_entry->context_entry.lo,
11146c441e1dSPeter Xu                                cc_entry->context_cache_gen);
1115d92fa2dcSLe Tan         ce = cc_entry->context_entry;
1116d92fa2dcSLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1117d92fa2dcSLe Tan     } else {
11181da12ec4SLe Tan         ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
11191da12ec4SLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
11201da12ec4SLe Tan         if (ret_fr) {
11211da12ec4SLe Tan             ret_fr = -ret_fr;
11221da12ec4SLe Tan             if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
11236c441e1dSPeter Xu                 trace_vtd_fault_disabled();
11241da12ec4SLe Tan             } else {
11251da12ec4SLe Tan                 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
11261da12ec4SLe Tan             }
1127b9313021SPeter Xu             goto error;
11281da12ec4SLe Tan         }
1129d92fa2dcSLe Tan         /* Update context-cache */
11306c441e1dSPeter Xu         trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
11316c441e1dSPeter Xu                                   cc_entry->context_cache_gen,
11326c441e1dSPeter Xu                                   s->context_cache_gen);
1133d92fa2dcSLe Tan         cc_entry->context_entry = ce;
1134d92fa2dcSLe Tan         cc_entry->context_cache_gen = s->context_cache_gen;
1135d92fa2dcSLe Tan     }
11361da12ec4SLe Tan 
1137dbaabb25SPeter Xu     /*
1138dbaabb25SPeter Xu      * We don't need to translate for pass-through context entries.
1139dbaabb25SPeter Xu      * Also, let's ignore IOTLB caching as well for PT devices.
1140dbaabb25SPeter Xu      */
1141dbaabb25SPeter Xu     if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
1142b9313021SPeter Xu         entry->iova = addr & VTD_PAGE_MASK;
1143dbaabb25SPeter Xu         entry->translated_addr = entry->iova;
1144b9313021SPeter Xu         entry->addr_mask = VTD_PAGE_MASK;
1145dbaabb25SPeter Xu         entry->perm = IOMMU_RW;
1146dbaabb25SPeter Xu         trace_vtd_translate_pt(source_id, entry->iova);
1147dbaabb25SPeter Xu 
1148dbaabb25SPeter Xu         /*
1149dbaabb25SPeter Xu          * When this happens, it means firstly caching-mode is not
1150dbaabb25SPeter Xu          * enabled, and this is the first passthrough translation for
1151dbaabb25SPeter Xu          * the device. Let's enable the fast path for passthrough.
1152dbaabb25SPeter Xu          *
1153dbaabb25SPeter Xu          * When passthrough is disabled again for the device, we can
1154dbaabb25SPeter Xu          * capture it via the context entry invalidation, then the
1155dbaabb25SPeter Xu          * IOMMU region can be swapped back.
1156dbaabb25SPeter Xu          */
1157dbaabb25SPeter Xu         vtd_pt_enable_fast_path(s, source_id);
1158dbaabb25SPeter Xu 
1159b9313021SPeter Xu         return true;
1160dbaabb25SPeter Xu     }
1161dbaabb25SPeter Xu 
11626e905564SPeter Xu     ret_fr = vtd_iova_to_slpte(&ce, addr, is_write, &slpte, &level,
11631da12ec4SLe Tan                                &reads, &writes);
11641da12ec4SLe Tan     if (ret_fr) {
11651da12ec4SLe Tan         ret_fr = -ret_fr;
11661da12ec4SLe Tan         if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
11676c441e1dSPeter Xu             trace_vtd_fault_disabled();
11681da12ec4SLe Tan         } else {
11691da12ec4SLe Tan             vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
11701da12ec4SLe Tan         }
1171b9313021SPeter Xu         goto error;
11721da12ec4SLe Tan     }
11731da12ec4SLe Tan 
1174d66b969bSJason Wang     page_mask = vtd_slpt_level_page_mask(level);
1175b5a280c0SLe Tan     vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
1176d66b969bSJason Wang                      reads, writes, level);
1177b5a280c0SLe Tan out:
1178d66b969bSJason Wang     entry->iova = addr & page_mask;
1179d66b969bSJason Wang     entry->translated_addr = vtd_get_slpte_addr(slpte) & page_mask;
1180d66b969bSJason Wang     entry->addr_mask = ~page_mask;
11815a38cb59SPeter Xu     entry->perm = IOMMU_ACCESS_FLAG(reads, writes);
1182b9313021SPeter Xu     return true;
1183b9313021SPeter Xu 
1184b9313021SPeter Xu error:
1185b9313021SPeter Xu     entry->iova = 0;
1186b9313021SPeter Xu     entry->translated_addr = 0;
1187b9313021SPeter Xu     entry->addr_mask = 0;
1188b9313021SPeter Xu     entry->perm = IOMMU_NONE;
1189b9313021SPeter Xu     return false;
11901da12ec4SLe Tan }
11911da12ec4SLe Tan 
11921da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s)
11931da12ec4SLe Tan {
11941da12ec4SLe Tan     s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
11951da12ec4SLe Tan     s->root_extended = s->root & VTD_RTADDR_RTT;
11961da12ec4SLe Tan     s->root &= VTD_RTADDR_ADDR_MASK;
11971da12ec4SLe Tan 
11987feb51b7SPeter Xu     trace_vtd_reg_dmar_root(s->root, s->root_extended);
11991da12ec4SLe Tan }
12001da12ec4SLe Tan 
120102a2cbc8SPeter Xu static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
120202a2cbc8SPeter Xu                                uint32_t index, uint32_t mask)
120302a2cbc8SPeter Xu {
120402a2cbc8SPeter Xu     x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
120502a2cbc8SPeter Xu }
120602a2cbc8SPeter Xu 
1207a5861439SPeter Xu static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1208a5861439SPeter Xu {
1209a5861439SPeter Xu     uint64_t value = 0;
1210a5861439SPeter Xu     value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1211a5861439SPeter Xu     s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
1212a5861439SPeter Xu     s->intr_root = value & VTD_IRTA_ADDR_MASK;
121328589311SJan Kiszka     s->intr_eime = value & VTD_IRTA_EIME;
1214a5861439SPeter Xu 
121502a2cbc8SPeter Xu     /* Notify global invalidation */
121602a2cbc8SPeter Xu     vtd_iec_notify_all(s, true, 0, 0);
1217a5861439SPeter Xu 
12187feb51b7SPeter Xu     trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
1219a5861439SPeter Xu }
1220a5861439SPeter Xu 
1221dd4d607eSPeter Xu static void vtd_iommu_replay_all(IntelIOMMUState *s)
1222dd4d607eSPeter Xu {
1223dd4d607eSPeter Xu     IntelIOMMUNotifierNode *node;
1224dd4d607eSPeter Xu 
1225dd4d607eSPeter Xu     QLIST_FOREACH(node, &s->notifiers_list, next) {
1226dd4d607eSPeter Xu         memory_region_iommu_replay_all(&node->vtd_as->iommu);
1227dd4d607eSPeter Xu     }
1228dd4d607eSPeter Xu }
1229dd4d607eSPeter Xu 
1230d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s)
1231d92fa2dcSLe Tan {
1232bc535e59SPeter Xu     trace_vtd_inv_desc_cc_global();
1233d92fa2dcSLe Tan     s->context_cache_gen++;
1234d92fa2dcSLe Tan     if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
1235d92fa2dcSLe Tan         vtd_reset_context_cache(s);
1236d92fa2dcSLe Tan     }
1237dbaabb25SPeter Xu     vtd_switch_address_space_all(s);
1238dd4d607eSPeter Xu     /*
1239dd4d607eSPeter Xu      * From VT-d spec 6.5.2.1, a global context entry invalidation
1240dd4d607eSPeter Xu      * should be followed by a IOTLB global invalidation, so we should
1241dd4d607eSPeter Xu      * be safe even without this. Hoewever, let's replay the region as
1242dd4d607eSPeter Xu      * well to be safer, and go back here when we need finer tunes for
1243dd4d607eSPeter Xu      * VT-d emulation codes.
1244dd4d607eSPeter Xu      */
1245dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
1246d92fa2dcSLe Tan }
1247d92fa2dcSLe Tan 
1248d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation.
1249d92fa2dcSLe Tan  * @func_mask: FM field after shifting
1250d92fa2dcSLe Tan  */
1251d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s,
1252d92fa2dcSLe Tan                                           uint16_t source_id,
1253d92fa2dcSLe Tan                                           uint16_t func_mask)
1254d92fa2dcSLe Tan {
1255d92fa2dcSLe Tan     uint16_t mask;
12567df953bdSKnut Omang     VTDBus *vtd_bus;
1257d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
1258bc535e59SPeter Xu     uint8_t bus_n, devfn;
1259d92fa2dcSLe Tan     uint16_t devfn_it;
1260d92fa2dcSLe Tan 
1261bc535e59SPeter Xu     trace_vtd_inv_desc_cc_devices(source_id, func_mask);
1262bc535e59SPeter Xu 
1263d92fa2dcSLe Tan     switch (func_mask & 3) {
1264d92fa2dcSLe Tan     case 0:
1265d92fa2dcSLe Tan         mask = 0;   /* No bits in the SID field masked */
1266d92fa2dcSLe Tan         break;
1267d92fa2dcSLe Tan     case 1:
1268d92fa2dcSLe Tan         mask = 4;   /* Mask bit 2 in the SID field */
1269d92fa2dcSLe Tan         break;
1270d92fa2dcSLe Tan     case 2:
1271d92fa2dcSLe Tan         mask = 6;   /* Mask bit 2:1 in the SID field */
1272d92fa2dcSLe Tan         break;
1273d92fa2dcSLe Tan     case 3:
1274d92fa2dcSLe Tan         mask = 7;   /* Mask bit 2:0 in the SID field */
1275d92fa2dcSLe Tan         break;
1276d92fa2dcSLe Tan     }
12776cb99accSPeter Xu     mask = ~mask;
1278bc535e59SPeter Xu 
1279bc535e59SPeter Xu     bus_n = VTD_SID_TO_BUS(source_id);
1280bc535e59SPeter Xu     vtd_bus = vtd_find_as_from_bus_num(s, bus_n);
12817df953bdSKnut Omang     if (vtd_bus) {
1282d92fa2dcSLe Tan         devfn = VTD_SID_TO_DEVFN(source_id);
128304af0e18SPeter Xu         for (devfn_it = 0; devfn_it < X86_IOMMU_PCI_DEVFN_MAX; ++devfn_it) {
12847df953bdSKnut Omang             vtd_as = vtd_bus->dev_as[devfn_it];
1285d92fa2dcSLe Tan             if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
1286bc535e59SPeter Xu                 trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it),
1287bc535e59SPeter Xu                                              VTD_PCI_FUNC(devfn_it));
1288d92fa2dcSLe Tan                 vtd_as->context_cache_entry.context_cache_gen = 0;
1289dd4d607eSPeter Xu                 /*
1290dbaabb25SPeter Xu                  * Do switch address space when needed, in case if the
1291dbaabb25SPeter Xu                  * device passthrough bit is switched.
1292dbaabb25SPeter Xu                  */
1293dbaabb25SPeter Xu                 vtd_switch_address_space(vtd_as);
1294dbaabb25SPeter Xu                 /*
1295dd4d607eSPeter Xu                  * So a device is moving out of (or moving into) a
1296dd4d607eSPeter Xu                  * domain, a replay() suites here to notify all the
1297dd4d607eSPeter Xu                  * IOMMU_NOTIFIER_MAP registers about this change.
1298dd4d607eSPeter Xu                  * This won't bring bad even if we have no such
1299dd4d607eSPeter Xu                  * notifier registered - the IOMMU notification
1300dd4d607eSPeter Xu                  * framework will skip MAP notifications if that
1301dd4d607eSPeter Xu                  * happened.
1302dd4d607eSPeter Xu                  */
1303dd4d607eSPeter Xu                 memory_region_iommu_replay_all(&vtd_as->iommu);
1304d92fa2dcSLe Tan             }
1305d92fa2dcSLe Tan         }
1306d92fa2dcSLe Tan     }
1307d92fa2dcSLe Tan }
1308d92fa2dcSLe Tan 
13091da12ec4SLe Tan /* Context-cache invalidation
13101da12ec4SLe Tan  * Returns the Context Actual Invalidation Granularity.
13111da12ec4SLe Tan  * @val: the content of the CCMD_REG
13121da12ec4SLe Tan  */
13131da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
13141da12ec4SLe Tan {
13151da12ec4SLe Tan     uint64_t caig;
13161da12ec4SLe Tan     uint64_t type = val & VTD_CCMD_CIRG_MASK;
13171da12ec4SLe Tan 
13181da12ec4SLe Tan     switch (type) {
13191da12ec4SLe Tan     case VTD_CCMD_DOMAIN_INVL:
1320d92fa2dcSLe Tan         /* Fall through */
1321d92fa2dcSLe Tan     case VTD_CCMD_GLOBAL_INVL:
1322d92fa2dcSLe Tan         caig = VTD_CCMD_GLOBAL_INVL_A;
1323d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
13241da12ec4SLe Tan         break;
13251da12ec4SLe Tan 
13261da12ec4SLe Tan     case VTD_CCMD_DEVICE_INVL:
13271da12ec4SLe Tan         caig = VTD_CCMD_DEVICE_INVL_A;
1328d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
13291da12ec4SLe Tan         break;
13301da12ec4SLe Tan 
13311da12ec4SLe Tan     default:
13327feb51b7SPeter Xu         trace_vtd_err("Context cache invalidate type error.");
13331da12ec4SLe Tan         caig = 0;
13341da12ec4SLe Tan     }
13351da12ec4SLe Tan     return caig;
13361da12ec4SLe Tan }
13371da12ec4SLe Tan 
1338b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
1339b5a280c0SLe Tan {
13407feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_global();
1341b5a280c0SLe Tan     vtd_reset_iotlb(s);
1342dd4d607eSPeter Xu     vtd_iommu_replay_all(s);
1343b5a280c0SLe Tan }
1344b5a280c0SLe Tan 
1345b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
1346b5a280c0SLe Tan {
1347dd4d607eSPeter Xu     IntelIOMMUNotifierNode *node;
1348dd4d607eSPeter Xu     VTDContextEntry ce;
1349dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
1350dd4d607eSPeter Xu 
13517feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_domain(domain_id);
13527feb51b7SPeter Xu 
1353b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
1354b5a280c0SLe Tan                                 &domain_id);
1355dd4d607eSPeter Xu 
1356dd4d607eSPeter Xu     QLIST_FOREACH(node, &s->notifiers_list, next) {
1357dd4d607eSPeter Xu         vtd_as = node->vtd_as;
1358dd4d607eSPeter Xu         if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1359dd4d607eSPeter Xu                                       vtd_as->devfn, &ce) &&
1360dd4d607eSPeter Xu             domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
1361dd4d607eSPeter Xu             memory_region_iommu_replay_all(&vtd_as->iommu);
1362dd4d607eSPeter Xu         }
1363dd4d607eSPeter Xu     }
1364dd4d607eSPeter Xu }
1365dd4d607eSPeter Xu 
1366dd4d607eSPeter Xu static int vtd_page_invalidate_notify_hook(IOMMUTLBEntry *entry,
1367dd4d607eSPeter Xu                                            void *private)
1368dd4d607eSPeter Xu {
1369dd4d607eSPeter Xu     memory_region_notify_iommu((MemoryRegion *)private, *entry);
1370dd4d607eSPeter Xu     return 0;
1371dd4d607eSPeter Xu }
1372dd4d607eSPeter Xu 
1373dd4d607eSPeter Xu static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
1374dd4d607eSPeter Xu                                            uint16_t domain_id, hwaddr addr,
1375dd4d607eSPeter Xu                                            uint8_t am)
1376dd4d607eSPeter Xu {
1377dd4d607eSPeter Xu     IntelIOMMUNotifierNode *node;
1378dd4d607eSPeter Xu     VTDContextEntry ce;
1379dd4d607eSPeter Xu     int ret;
1380dd4d607eSPeter Xu 
1381dd4d607eSPeter Xu     QLIST_FOREACH(node, &(s->notifiers_list), next) {
1382dd4d607eSPeter Xu         VTDAddressSpace *vtd_as = node->vtd_as;
1383dd4d607eSPeter Xu         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1384dd4d607eSPeter Xu                                        vtd_as->devfn, &ce);
1385dd4d607eSPeter Xu         if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
1386dd4d607eSPeter Xu             vtd_page_walk(&ce, addr, addr + (1 << am) * VTD_PAGE_SIZE,
1387dd4d607eSPeter Xu                           vtd_page_invalidate_notify_hook,
1388dd4d607eSPeter Xu                           (void *)&vtd_as->iommu, true);
1389dd4d607eSPeter Xu         }
1390dd4d607eSPeter Xu     }
1391b5a280c0SLe Tan }
1392b5a280c0SLe Tan 
1393b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
1394b5a280c0SLe Tan                                       hwaddr addr, uint8_t am)
1395b5a280c0SLe Tan {
1396b5a280c0SLe Tan     VTDIOTLBPageInvInfo info;
1397b5a280c0SLe Tan 
13987feb51b7SPeter Xu     trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
13997feb51b7SPeter Xu 
1400b5a280c0SLe Tan     assert(am <= VTD_MAMV);
1401b5a280c0SLe Tan     info.domain_id = domain_id;
1402d66b969bSJason Wang     info.addr = addr;
1403b5a280c0SLe Tan     info.mask = ~((1 << am) - 1);
1404b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
1405dd4d607eSPeter Xu     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
1406b5a280c0SLe Tan }
1407b5a280c0SLe Tan 
14081da12ec4SLe Tan /* Flush IOTLB
14091da12ec4SLe Tan  * Returns the IOTLB Actual Invalidation Granularity.
14101da12ec4SLe Tan  * @val: the content of the IOTLB_REG
14111da12ec4SLe Tan  */
14121da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
14131da12ec4SLe Tan {
14141da12ec4SLe Tan     uint64_t iaig;
14151da12ec4SLe Tan     uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
1416b5a280c0SLe Tan     uint16_t domain_id;
1417b5a280c0SLe Tan     hwaddr addr;
1418b5a280c0SLe Tan     uint8_t am;
14191da12ec4SLe Tan 
14201da12ec4SLe Tan     switch (type) {
14211da12ec4SLe Tan     case VTD_TLB_GLOBAL_FLUSH:
14221da12ec4SLe Tan         iaig = VTD_TLB_GLOBAL_FLUSH_A;
1423b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
14241da12ec4SLe Tan         break;
14251da12ec4SLe Tan 
14261da12ec4SLe Tan     case VTD_TLB_DSI_FLUSH:
1427b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
14281da12ec4SLe Tan         iaig = VTD_TLB_DSI_FLUSH_A;
1429b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
14301da12ec4SLe Tan         break;
14311da12ec4SLe Tan 
14321da12ec4SLe Tan     case VTD_TLB_PSI_FLUSH:
1433b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
1434b5a280c0SLe Tan         addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1435b5a280c0SLe Tan         am = VTD_IVA_AM(addr);
1436b5a280c0SLe Tan         addr = VTD_IVA_ADDR(addr);
1437b5a280c0SLe Tan         if (am > VTD_MAMV) {
14387feb51b7SPeter Xu             trace_vtd_err("IOTLB PSI flush: address mask overflow.");
1439b5a280c0SLe Tan             iaig = 0;
1440b5a280c0SLe Tan             break;
1441b5a280c0SLe Tan         }
14421da12ec4SLe Tan         iaig = VTD_TLB_PSI_FLUSH_A;
1443b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
14441da12ec4SLe Tan         break;
14451da12ec4SLe Tan 
14461da12ec4SLe Tan     default:
14477feb51b7SPeter Xu         trace_vtd_err("IOTLB flush: invalid granularity.");
14481da12ec4SLe Tan         iaig = 0;
14491da12ec4SLe Tan     }
14501da12ec4SLe Tan     return iaig;
14511da12ec4SLe Tan }
14521da12ec4SLe Tan 
1453ed7b8fbcSLe Tan static inline bool vtd_queued_inv_enable_check(IntelIOMMUState *s)
1454ed7b8fbcSLe Tan {
1455ed7b8fbcSLe Tan     return s->iq_tail == 0;
1456ed7b8fbcSLe Tan }
1457ed7b8fbcSLe Tan 
1458ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1459ed7b8fbcSLe Tan {
1460ed7b8fbcSLe Tan     return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1461ed7b8fbcSLe Tan            (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1462ed7b8fbcSLe Tan }
1463ed7b8fbcSLe Tan 
1464ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
1465ed7b8fbcSLe Tan {
1466ed7b8fbcSLe Tan     uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
1467ed7b8fbcSLe Tan 
14687feb51b7SPeter Xu     trace_vtd_inv_qi_enable(en);
14697feb51b7SPeter Xu 
1470ed7b8fbcSLe Tan     if (en) {
1471ed7b8fbcSLe Tan         if (vtd_queued_inv_enable_check(s)) {
1472ed7b8fbcSLe Tan             s->iq = iqa_val & VTD_IQA_IQA_MASK;
1473ed7b8fbcSLe Tan             /* 2^(x+8) entries */
1474ed7b8fbcSLe Tan             s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
1475ed7b8fbcSLe Tan             s->qi_enabled = true;
14767feb51b7SPeter Xu             trace_vtd_inv_qi_setup(s->iq, s->iq_size);
1477ed7b8fbcSLe Tan             /* Ok - report back to driver */
1478ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
1479ed7b8fbcSLe Tan         } else {
14807feb51b7SPeter Xu             trace_vtd_err_qi_enable(s->iq_tail);
1481ed7b8fbcSLe Tan         }
1482ed7b8fbcSLe Tan     } else {
1483ed7b8fbcSLe Tan         if (vtd_queued_inv_disable_check(s)) {
1484ed7b8fbcSLe Tan             /* disable Queued Invalidation */
1485ed7b8fbcSLe Tan             vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
1486ed7b8fbcSLe Tan             s->iq_head = 0;
1487ed7b8fbcSLe Tan             s->qi_enabled = false;
1488ed7b8fbcSLe Tan             /* Ok - report back to driver */
1489ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
1490ed7b8fbcSLe Tan         } else {
14917feb51b7SPeter Xu             trace_vtd_err_qi_disable(s->iq_head, s->iq_tail, s->iq_last_desc_type);
1492ed7b8fbcSLe Tan         }
1493ed7b8fbcSLe Tan     }
1494ed7b8fbcSLe Tan }
1495ed7b8fbcSLe Tan 
14961da12ec4SLe Tan /* Set Root Table Pointer */
14971da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
14981da12ec4SLe Tan {
14991da12ec4SLe Tan     vtd_root_table_setup(s);
15001da12ec4SLe Tan     /* Ok - report back to driver */
15011da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
15021da12ec4SLe Tan }
15031da12ec4SLe Tan 
1504a5861439SPeter Xu /* Set Interrupt Remap Table Pointer */
1505a5861439SPeter Xu static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
1506a5861439SPeter Xu {
1507a5861439SPeter Xu     vtd_interrupt_remap_table_setup(s);
1508a5861439SPeter Xu     /* Ok - report back to driver */
1509a5861439SPeter Xu     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
1510a5861439SPeter Xu }
1511a5861439SPeter Xu 
15121da12ec4SLe Tan /* Handle Translation Enable/Disable */
15131da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
15141da12ec4SLe Tan {
1515558e0024SPeter Xu     if (s->dmar_enabled == en) {
1516558e0024SPeter Xu         return;
1517558e0024SPeter Xu     }
1518558e0024SPeter Xu 
15197feb51b7SPeter Xu     trace_vtd_dmar_enable(en);
15201da12ec4SLe Tan 
15211da12ec4SLe Tan     if (en) {
15221da12ec4SLe Tan         s->dmar_enabled = true;
15231da12ec4SLe Tan         /* Ok - report back to driver */
15241da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
15251da12ec4SLe Tan     } else {
15261da12ec4SLe Tan         s->dmar_enabled = false;
15271da12ec4SLe Tan 
15281da12ec4SLe Tan         /* Clear the index of Fault Recording Register */
15291da12ec4SLe Tan         s->next_frcd_reg = 0;
15301da12ec4SLe Tan         /* Ok - report back to driver */
15311da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
15321da12ec4SLe Tan     }
1533558e0024SPeter Xu 
1534558e0024SPeter Xu     vtd_switch_address_space_all(s);
15351da12ec4SLe Tan }
15361da12ec4SLe Tan 
153780de52baSPeter Xu /* Handle Interrupt Remap Enable/Disable */
153880de52baSPeter Xu static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
153980de52baSPeter Xu {
15407feb51b7SPeter Xu     trace_vtd_ir_enable(en);
154180de52baSPeter Xu 
154280de52baSPeter Xu     if (en) {
154380de52baSPeter Xu         s->intr_enabled = true;
154480de52baSPeter Xu         /* Ok - report back to driver */
154580de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
154680de52baSPeter Xu     } else {
154780de52baSPeter Xu         s->intr_enabled = false;
154880de52baSPeter Xu         /* Ok - report back to driver */
154980de52baSPeter Xu         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
155080de52baSPeter Xu     }
155180de52baSPeter Xu }
155280de52baSPeter Xu 
15531da12ec4SLe Tan /* Handle write to Global Command Register */
15541da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s)
15551da12ec4SLe Tan {
15561da12ec4SLe Tan     uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
15571da12ec4SLe Tan     uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
15581da12ec4SLe Tan     uint32_t changed = status ^ val;
15591da12ec4SLe Tan 
15607feb51b7SPeter Xu     trace_vtd_reg_write_gcmd(status, val);
15611da12ec4SLe Tan     if (changed & VTD_GCMD_TE) {
15621da12ec4SLe Tan         /* Translation enable/disable */
15631da12ec4SLe Tan         vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
15641da12ec4SLe Tan     }
15651da12ec4SLe Tan     if (val & VTD_GCMD_SRTP) {
15661da12ec4SLe Tan         /* Set/update the root-table pointer */
15671da12ec4SLe Tan         vtd_handle_gcmd_srtp(s);
15681da12ec4SLe Tan     }
1569ed7b8fbcSLe Tan     if (changed & VTD_GCMD_QIE) {
1570ed7b8fbcSLe Tan         /* Queued Invalidation Enable */
1571ed7b8fbcSLe Tan         vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
1572ed7b8fbcSLe Tan     }
1573a5861439SPeter Xu     if (val & VTD_GCMD_SIRTP) {
1574a5861439SPeter Xu         /* Set/update the interrupt remapping root-table pointer */
1575a5861439SPeter Xu         vtd_handle_gcmd_sirtp(s);
1576a5861439SPeter Xu     }
157780de52baSPeter Xu     if (changed & VTD_GCMD_IRE) {
157880de52baSPeter Xu         /* Interrupt remap enable/disable */
157980de52baSPeter Xu         vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
158080de52baSPeter Xu     }
15811da12ec4SLe Tan }
15821da12ec4SLe Tan 
15831da12ec4SLe Tan /* Handle write to Context Command Register */
15841da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s)
15851da12ec4SLe Tan {
15861da12ec4SLe Tan     uint64_t ret;
15871da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
15881da12ec4SLe Tan 
15891da12ec4SLe Tan     /* Context-cache invalidation request */
15901da12ec4SLe Tan     if (val & VTD_CCMD_ICC) {
1591ed7b8fbcSLe Tan         if (s->qi_enabled) {
15927feb51b7SPeter Xu             trace_vtd_err("Queued Invalidation enabled, "
1593ed7b8fbcSLe Tan                           "should not use register-based invalidation");
1594ed7b8fbcSLe Tan             return;
1595ed7b8fbcSLe Tan         }
15961da12ec4SLe Tan         ret = vtd_context_cache_invalidate(s, val);
15971da12ec4SLe Tan         /* Invalidation completed. Change something to show */
15981da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
15991da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
16001da12ec4SLe Tan                                       ret);
16011da12ec4SLe Tan     }
16021da12ec4SLe Tan }
16031da12ec4SLe Tan 
16041da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */
16051da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s)
16061da12ec4SLe Tan {
16071da12ec4SLe Tan     uint64_t ret;
16081da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
16091da12ec4SLe Tan 
16101da12ec4SLe Tan     /* IOTLB invalidation request */
16111da12ec4SLe Tan     if (val & VTD_TLB_IVT) {
1612ed7b8fbcSLe Tan         if (s->qi_enabled) {
16137feb51b7SPeter Xu             trace_vtd_err("Queued Invalidation enabled, "
16147feb51b7SPeter Xu                           "should not use register-based invalidation.");
1615ed7b8fbcSLe Tan             return;
1616ed7b8fbcSLe Tan         }
16171da12ec4SLe Tan         ret = vtd_iotlb_flush(s, val);
16181da12ec4SLe Tan         /* Invalidation completed. Change something to show */
16191da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
16201da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
16211da12ec4SLe Tan                                       VTD_TLB_FLUSH_GRANU_MASK_A, ret);
16221da12ec4SLe Tan     }
16231da12ec4SLe Tan }
16241da12ec4SLe Tan 
1625ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */
1626ed7b8fbcSLe Tan static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
1627ed7b8fbcSLe Tan                              VTDInvDesc *inv_desc)
1628ed7b8fbcSLe Tan {
1629ed7b8fbcSLe Tan     dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
1630ed7b8fbcSLe Tan     if (dma_memory_read(&address_space_memory, addr, inv_desc,
1631ed7b8fbcSLe Tan         sizeof(*inv_desc))) {
16327feb51b7SPeter Xu         trace_vtd_err("Read INV DESC failed.");
1633ed7b8fbcSLe Tan         inv_desc->lo = 0;
1634ed7b8fbcSLe Tan         inv_desc->hi = 0;
1635ed7b8fbcSLe Tan         return false;
1636ed7b8fbcSLe Tan     }
1637ed7b8fbcSLe Tan     inv_desc->lo = le64_to_cpu(inv_desc->lo);
1638ed7b8fbcSLe Tan     inv_desc->hi = le64_to_cpu(inv_desc->hi);
1639ed7b8fbcSLe Tan     return true;
1640ed7b8fbcSLe Tan }
1641ed7b8fbcSLe Tan 
1642ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1643ed7b8fbcSLe Tan {
1644ed7b8fbcSLe Tan     if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
1645ed7b8fbcSLe Tan         (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
1646bc535e59SPeter Xu         trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
1647ed7b8fbcSLe Tan         return false;
1648ed7b8fbcSLe Tan     }
1649ed7b8fbcSLe Tan     if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
1650ed7b8fbcSLe Tan         /* Status Write */
1651ed7b8fbcSLe Tan         uint32_t status_data = (uint32_t)(inv_desc->lo >>
1652ed7b8fbcSLe Tan                                VTD_INV_DESC_WAIT_DATA_SHIFT);
1653ed7b8fbcSLe Tan 
1654ed7b8fbcSLe Tan         assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
1655ed7b8fbcSLe Tan 
1656ed7b8fbcSLe Tan         /* FIXME: need to be masked with HAW? */
1657ed7b8fbcSLe Tan         dma_addr_t status_addr = inv_desc->hi;
1658bc535e59SPeter Xu         trace_vtd_inv_desc_wait_sw(status_addr, status_data);
1659ed7b8fbcSLe Tan         status_data = cpu_to_le32(status_data);
1660ed7b8fbcSLe Tan         if (dma_memory_write(&address_space_memory, status_addr, &status_data,
1661ed7b8fbcSLe Tan                              sizeof(status_data))) {
1662bc535e59SPeter Xu             trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
1663ed7b8fbcSLe Tan             return false;
1664ed7b8fbcSLe Tan         }
1665ed7b8fbcSLe Tan     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
1666ed7b8fbcSLe Tan         /* Interrupt flag */
1667ed7b8fbcSLe Tan         vtd_generate_completion_event(s);
1668ed7b8fbcSLe Tan     } else {
1669bc535e59SPeter Xu         trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
1670ed7b8fbcSLe Tan         return false;
1671ed7b8fbcSLe Tan     }
1672ed7b8fbcSLe Tan     return true;
1673ed7b8fbcSLe Tan }
1674ed7b8fbcSLe Tan 
1675d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
1676d92fa2dcSLe Tan                                            VTDInvDesc *inv_desc)
1677d92fa2dcSLe Tan {
1678bc535e59SPeter Xu     uint16_t sid, fmask;
1679bc535e59SPeter Xu 
1680d92fa2dcSLe Tan     if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
1681bc535e59SPeter Xu         trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
1682d92fa2dcSLe Tan         return false;
1683d92fa2dcSLe Tan     }
1684d92fa2dcSLe Tan     switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
1685d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DOMAIN:
1686bc535e59SPeter Xu         trace_vtd_inv_desc_cc_domain(
1687d92fa2dcSLe Tan             (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
1688d92fa2dcSLe Tan         /* Fall through */
1689d92fa2dcSLe Tan     case VTD_INV_DESC_CC_GLOBAL:
1690d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
1691d92fa2dcSLe Tan         break;
1692d92fa2dcSLe Tan 
1693d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DEVICE:
1694bc535e59SPeter Xu         sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
1695bc535e59SPeter Xu         fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
1696bc535e59SPeter Xu         vtd_context_device_invalidate(s, sid, fmask);
1697d92fa2dcSLe Tan         break;
1698d92fa2dcSLe Tan 
1699d92fa2dcSLe Tan     default:
1700bc535e59SPeter Xu         trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
1701d92fa2dcSLe Tan         return false;
1702d92fa2dcSLe Tan     }
1703d92fa2dcSLe Tan     return true;
1704d92fa2dcSLe Tan }
1705d92fa2dcSLe Tan 
1706b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1707b5a280c0SLe Tan {
1708b5a280c0SLe Tan     uint16_t domain_id;
1709b5a280c0SLe Tan     uint8_t am;
1710b5a280c0SLe Tan     hwaddr addr;
1711b5a280c0SLe Tan 
1712b5a280c0SLe Tan     if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
1713b5a280c0SLe Tan         (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
1714bc535e59SPeter Xu         trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1715b5a280c0SLe Tan         return false;
1716b5a280c0SLe Tan     }
1717b5a280c0SLe Tan 
1718b5a280c0SLe Tan     switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
1719b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_GLOBAL:
1720b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
1721b5a280c0SLe Tan         break;
1722b5a280c0SLe Tan 
1723b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_DOMAIN:
1724b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1725b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
1726b5a280c0SLe Tan         break;
1727b5a280c0SLe Tan 
1728b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_PAGE:
1729b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1730b5a280c0SLe Tan         addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
1731b5a280c0SLe Tan         am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
1732b5a280c0SLe Tan         if (am > VTD_MAMV) {
1733bc535e59SPeter Xu             trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1734b5a280c0SLe Tan             return false;
1735b5a280c0SLe Tan         }
1736b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1737b5a280c0SLe Tan         break;
1738b5a280c0SLe Tan 
1739b5a280c0SLe Tan     default:
1740bc535e59SPeter Xu         trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1741b5a280c0SLe Tan         return false;
1742b5a280c0SLe Tan     }
1743b5a280c0SLe Tan     return true;
1744b5a280c0SLe Tan }
1745b5a280c0SLe Tan 
174602a2cbc8SPeter Xu static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
174702a2cbc8SPeter Xu                                      VTDInvDesc *inv_desc)
174802a2cbc8SPeter Xu {
17497feb51b7SPeter Xu     trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
175002a2cbc8SPeter Xu                            inv_desc->iec.index,
175102a2cbc8SPeter Xu                            inv_desc->iec.index_mask);
175202a2cbc8SPeter Xu 
175302a2cbc8SPeter Xu     vtd_iec_notify_all(s, !inv_desc->iec.granularity,
175402a2cbc8SPeter Xu                        inv_desc->iec.index,
175502a2cbc8SPeter Xu                        inv_desc->iec.index_mask);
1756554f5e16SJason Wang     return true;
1757554f5e16SJason Wang }
175802a2cbc8SPeter Xu 
1759554f5e16SJason Wang static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
1760554f5e16SJason Wang                                           VTDInvDesc *inv_desc)
1761554f5e16SJason Wang {
1762554f5e16SJason Wang     VTDAddressSpace *vtd_dev_as;
1763554f5e16SJason Wang     IOMMUTLBEntry entry;
1764554f5e16SJason Wang     struct VTDBus *vtd_bus;
1765554f5e16SJason Wang     hwaddr addr;
1766554f5e16SJason Wang     uint64_t sz;
1767554f5e16SJason Wang     uint16_t sid;
1768554f5e16SJason Wang     uint8_t devfn;
1769554f5e16SJason Wang     bool size;
1770554f5e16SJason Wang     uint8_t bus_num;
1771554f5e16SJason Wang 
1772554f5e16SJason Wang     addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
1773554f5e16SJason Wang     sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
1774554f5e16SJason Wang     devfn = sid & 0xff;
1775554f5e16SJason Wang     bus_num = sid >> 8;
1776554f5e16SJason Wang     size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
1777554f5e16SJason Wang 
1778554f5e16SJason Wang     if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
1779554f5e16SJason Wang         (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
17807feb51b7SPeter Xu         trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1781554f5e16SJason Wang         return false;
1782554f5e16SJason Wang     }
1783554f5e16SJason Wang 
1784554f5e16SJason Wang     vtd_bus = vtd_find_as_from_bus_num(s, bus_num);
1785554f5e16SJason Wang     if (!vtd_bus) {
1786554f5e16SJason Wang         goto done;
1787554f5e16SJason Wang     }
1788554f5e16SJason Wang 
1789554f5e16SJason Wang     vtd_dev_as = vtd_bus->dev_as[devfn];
1790554f5e16SJason Wang     if (!vtd_dev_as) {
1791554f5e16SJason Wang         goto done;
1792554f5e16SJason Wang     }
1793554f5e16SJason Wang 
179404eb6247SJason Wang     /* According to ATS spec table 2.4:
179504eb6247SJason Wang      * S = 0, bits 15:12 = xxxx     range size: 4K
179604eb6247SJason Wang      * S = 1, bits 15:12 = xxx0     range size: 8K
179704eb6247SJason Wang      * S = 1, bits 15:12 = xx01     range size: 16K
179804eb6247SJason Wang      * S = 1, bits 15:12 = x011     range size: 32K
179904eb6247SJason Wang      * S = 1, bits 15:12 = 0111     range size: 64K
180004eb6247SJason Wang      * ...
180104eb6247SJason Wang      */
1802554f5e16SJason Wang     if (size) {
180304eb6247SJason Wang         sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
1804554f5e16SJason Wang         addr &= ~(sz - 1);
1805554f5e16SJason Wang     } else {
1806554f5e16SJason Wang         sz = VTD_PAGE_SIZE;
1807554f5e16SJason Wang     }
1808554f5e16SJason Wang 
1809554f5e16SJason Wang     entry.target_as = &vtd_dev_as->as;
1810554f5e16SJason Wang     entry.addr_mask = sz - 1;
1811554f5e16SJason Wang     entry.iova = addr;
1812554f5e16SJason Wang     entry.perm = IOMMU_NONE;
1813554f5e16SJason Wang     entry.translated_addr = 0;
181410315b9bSJason Wang     memory_region_notify_iommu(&vtd_dev_as->iommu, entry);
1815554f5e16SJason Wang 
1816554f5e16SJason Wang done:
181702a2cbc8SPeter Xu     return true;
181802a2cbc8SPeter Xu }
181902a2cbc8SPeter Xu 
1820ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s)
1821ed7b8fbcSLe Tan {
1822ed7b8fbcSLe Tan     VTDInvDesc inv_desc;
1823ed7b8fbcSLe Tan     uint8_t desc_type;
1824ed7b8fbcSLe Tan 
18257feb51b7SPeter Xu     trace_vtd_inv_qi_head(s->iq_head);
1826ed7b8fbcSLe Tan     if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
1827ed7b8fbcSLe Tan         s->iq_last_desc_type = VTD_INV_DESC_NONE;
1828ed7b8fbcSLe Tan         return false;
1829ed7b8fbcSLe Tan     }
1830ed7b8fbcSLe Tan     desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
1831ed7b8fbcSLe Tan     /* FIXME: should update at first or at last? */
1832ed7b8fbcSLe Tan     s->iq_last_desc_type = desc_type;
1833ed7b8fbcSLe Tan 
1834ed7b8fbcSLe Tan     switch (desc_type) {
1835ed7b8fbcSLe Tan     case VTD_INV_DESC_CC:
1836bc535e59SPeter Xu         trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
1837d92fa2dcSLe Tan         if (!vtd_process_context_cache_desc(s, &inv_desc)) {
1838d92fa2dcSLe Tan             return false;
1839d92fa2dcSLe Tan         }
1840ed7b8fbcSLe Tan         break;
1841ed7b8fbcSLe Tan 
1842ed7b8fbcSLe Tan     case VTD_INV_DESC_IOTLB:
1843bc535e59SPeter Xu         trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
1844b5a280c0SLe Tan         if (!vtd_process_iotlb_desc(s, &inv_desc)) {
1845b5a280c0SLe Tan             return false;
1846b5a280c0SLe Tan         }
1847ed7b8fbcSLe Tan         break;
1848ed7b8fbcSLe Tan 
1849ed7b8fbcSLe Tan     case VTD_INV_DESC_WAIT:
1850bc535e59SPeter Xu         trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
1851ed7b8fbcSLe Tan         if (!vtd_process_wait_desc(s, &inv_desc)) {
1852ed7b8fbcSLe Tan             return false;
1853ed7b8fbcSLe Tan         }
1854ed7b8fbcSLe Tan         break;
1855ed7b8fbcSLe Tan 
1856b7910472SPeter Xu     case VTD_INV_DESC_IEC:
1857bc535e59SPeter Xu         trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
185802a2cbc8SPeter Xu         if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
185902a2cbc8SPeter Xu             return false;
186002a2cbc8SPeter Xu         }
1861b7910472SPeter Xu         break;
1862b7910472SPeter Xu 
1863554f5e16SJason Wang     case VTD_INV_DESC_DEVICE:
18647feb51b7SPeter Xu         trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
1865554f5e16SJason Wang         if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
1866554f5e16SJason Wang             return false;
1867554f5e16SJason Wang         }
1868554f5e16SJason Wang         break;
1869554f5e16SJason Wang 
1870ed7b8fbcSLe Tan     default:
1871bc535e59SPeter Xu         trace_vtd_inv_desc_invalid(inv_desc.hi, inv_desc.lo);
1872ed7b8fbcSLe Tan         return false;
1873ed7b8fbcSLe Tan     }
1874ed7b8fbcSLe Tan     s->iq_head++;
1875ed7b8fbcSLe Tan     if (s->iq_head == s->iq_size) {
1876ed7b8fbcSLe Tan         s->iq_head = 0;
1877ed7b8fbcSLe Tan     }
1878ed7b8fbcSLe Tan     return true;
1879ed7b8fbcSLe Tan }
1880ed7b8fbcSLe Tan 
1881ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */
1882ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s)
1883ed7b8fbcSLe Tan {
18847feb51b7SPeter Xu     trace_vtd_inv_qi_fetch();
18857feb51b7SPeter Xu 
1886ed7b8fbcSLe Tan     if (s->iq_tail >= s->iq_size) {
1887ed7b8fbcSLe Tan         /* Detects an invalid Tail pointer */
18887feb51b7SPeter Xu         trace_vtd_err_qi_tail(s->iq_tail, s->iq_size);
1889ed7b8fbcSLe Tan         vtd_handle_inv_queue_error(s);
1890ed7b8fbcSLe Tan         return;
1891ed7b8fbcSLe Tan     }
1892ed7b8fbcSLe Tan     while (s->iq_head != s->iq_tail) {
1893ed7b8fbcSLe Tan         if (!vtd_process_inv_desc(s)) {
1894ed7b8fbcSLe Tan             /* Invalidation Queue Errors */
1895ed7b8fbcSLe Tan             vtd_handle_inv_queue_error(s);
1896ed7b8fbcSLe Tan             break;
1897ed7b8fbcSLe Tan         }
1898ed7b8fbcSLe Tan         /* Must update the IQH_REG in time */
1899ed7b8fbcSLe Tan         vtd_set_quad_raw(s, DMAR_IQH_REG,
1900ed7b8fbcSLe Tan                          (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
1901ed7b8fbcSLe Tan                          VTD_IQH_QH_MASK);
1902ed7b8fbcSLe Tan     }
1903ed7b8fbcSLe Tan }
1904ed7b8fbcSLe Tan 
1905ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */
1906ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s)
1907ed7b8fbcSLe Tan {
1908ed7b8fbcSLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
1909ed7b8fbcSLe Tan 
1910ed7b8fbcSLe Tan     s->iq_tail = VTD_IQT_QT(val);
19117feb51b7SPeter Xu     trace_vtd_inv_qi_tail(s->iq_tail);
19127feb51b7SPeter Xu 
1913ed7b8fbcSLe Tan     if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1914ed7b8fbcSLe Tan         /* Process Invalidation Queue here */
1915ed7b8fbcSLe Tan         vtd_fetch_inv_desc(s);
1916ed7b8fbcSLe Tan     }
1917ed7b8fbcSLe Tan }
1918ed7b8fbcSLe Tan 
19191da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s)
19201da12ec4SLe Tan {
19211da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
19221da12ec4SLe Tan     uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
19231da12ec4SLe Tan     uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
19241da12ec4SLe Tan 
19251da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
19261da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
19277feb51b7SPeter Xu         trace_vtd_fsts_clear_ip();
19281da12ec4SLe Tan     }
1929ed7b8fbcSLe Tan     /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
1930ed7b8fbcSLe Tan      * Descriptors if there are any when Queued Invalidation is enabled?
1931ed7b8fbcSLe Tan      */
19321da12ec4SLe Tan }
19331da12ec4SLe Tan 
19341da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s)
19351da12ec4SLe Tan {
19361da12ec4SLe Tan     uint32_t fectl_reg;
19371da12ec4SLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
19381da12ec4SLe Tan      * need to compare the old value and the new value to conclude that
19391da12ec4SLe Tan      * software clears the IM field? Or just check if the IM field is zero?
19401da12ec4SLe Tan      */
19411da12ec4SLe Tan     fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
19427feb51b7SPeter Xu 
19437feb51b7SPeter Xu     trace_vtd_reg_write_fectl(fectl_reg);
19447feb51b7SPeter Xu 
19451da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
19461da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
19471da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
19481da12ec4SLe Tan     }
19491da12ec4SLe Tan }
19501da12ec4SLe Tan 
1951ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s)
1952ed7b8fbcSLe Tan {
1953ed7b8fbcSLe Tan     uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
1954ed7b8fbcSLe Tan     uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1955ed7b8fbcSLe Tan 
1956ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
19577feb51b7SPeter Xu         trace_vtd_reg_ics_clear_ip();
1958ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1959ed7b8fbcSLe Tan     }
1960ed7b8fbcSLe Tan }
1961ed7b8fbcSLe Tan 
1962ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s)
1963ed7b8fbcSLe Tan {
1964ed7b8fbcSLe Tan     uint32_t iectl_reg;
1965ed7b8fbcSLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
1966ed7b8fbcSLe Tan      * need to compare the old value and the new value to conclude that
1967ed7b8fbcSLe Tan      * software clears the IM field? Or just check if the IM field is zero?
1968ed7b8fbcSLe Tan      */
1969ed7b8fbcSLe Tan     iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
19707feb51b7SPeter Xu 
19717feb51b7SPeter Xu     trace_vtd_reg_write_iectl(iectl_reg);
19727feb51b7SPeter Xu 
1973ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
1974ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
1975ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1976ed7b8fbcSLe Tan     }
1977ed7b8fbcSLe Tan }
1978ed7b8fbcSLe Tan 
19791da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
19801da12ec4SLe Tan {
19811da12ec4SLe Tan     IntelIOMMUState *s = opaque;
19821da12ec4SLe Tan     uint64_t val;
19831da12ec4SLe Tan 
19847feb51b7SPeter Xu     trace_vtd_reg_read(addr, size);
19857feb51b7SPeter Xu 
19861da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
19877feb51b7SPeter Xu         trace_vtd_err("Read MMIO over range.");
19881da12ec4SLe Tan         return (uint64_t)-1;
19891da12ec4SLe Tan     }
19901da12ec4SLe Tan 
19911da12ec4SLe Tan     switch (addr) {
19921da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
19931da12ec4SLe Tan     case DMAR_RTADDR_REG:
19941da12ec4SLe Tan         if (size == 4) {
19951da12ec4SLe Tan             val = s->root & ((1ULL << 32) - 1);
19961da12ec4SLe Tan         } else {
19971da12ec4SLe Tan             val = s->root;
19981da12ec4SLe Tan         }
19991da12ec4SLe Tan         break;
20001da12ec4SLe Tan 
20011da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
20021da12ec4SLe Tan         assert(size == 4);
20031da12ec4SLe Tan         val = s->root >> 32;
20041da12ec4SLe Tan         break;
20051da12ec4SLe Tan 
2006ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
2007ed7b8fbcSLe Tan     case DMAR_IQA_REG:
2008ed7b8fbcSLe Tan         val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2009ed7b8fbcSLe Tan         if (size == 4) {
2010ed7b8fbcSLe Tan             val = val & ((1ULL << 32) - 1);
2011ed7b8fbcSLe Tan         }
2012ed7b8fbcSLe Tan         break;
2013ed7b8fbcSLe Tan 
2014ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
2015ed7b8fbcSLe Tan         assert(size == 4);
2016ed7b8fbcSLe Tan         val = s->iq >> 32;
2017ed7b8fbcSLe Tan         break;
2018ed7b8fbcSLe Tan 
20191da12ec4SLe Tan     default:
20201da12ec4SLe Tan         if (size == 4) {
20211da12ec4SLe Tan             val = vtd_get_long(s, addr);
20221da12ec4SLe Tan         } else {
20231da12ec4SLe Tan             val = vtd_get_quad(s, addr);
20241da12ec4SLe Tan         }
20251da12ec4SLe Tan     }
20267feb51b7SPeter Xu 
20271da12ec4SLe Tan     return val;
20281da12ec4SLe Tan }
20291da12ec4SLe Tan 
20301da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr,
20311da12ec4SLe Tan                           uint64_t val, unsigned size)
20321da12ec4SLe Tan {
20331da12ec4SLe Tan     IntelIOMMUState *s = opaque;
20341da12ec4SLe Tan 
20357feb51b7SPeter Xu     trace_vtd_reg_write(addr, size, val);
20367feb51b7SPeter Xu 
20371da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
20387feb51b7SPeter Xu         trace_vtd_err("Write MMIO over range.");
20391da12ec4SLe Tan         return;
20401da12ec4SLe Tan     }
20411da12ec4SLe Tan 
20421da12ec4SLe Tan     switch (addr) {
20431da12ec4SLe Tan     /* Global Command Register, 32-bit */
20441da12ec4SLe Tan     case DMAR_GCMD_REG:
20451da12ec4SLe Tan         vtd_set_long(s, addr, val);
20461da12ec4SLe Tan         vtd_handle_gcmd_write(s);
20471da12ec4SLe Tan         break;
20481da12ec4SLe Tan 
20491da12ec4SLe Tan     /* Context Command Register, 64-bit */
20501da12ec4SLe Tan     case DMAR_CCMD_REG:
20511da12ec4SLe Tan         if (size == 4) {
20521da12ec4SLe Tan             vtd_set_long(s, addr, val);
20531da12ec4SLe Tan         } else {
20541da12ec4SLe Tan             vtd_set_quad(s, addr, val);
20551da12ec4SLe Tan             vtd_handle_ccmd_write(s);
20561da12ec4SLe Tan         }
20571da12ec4SLe Tan         break;
20581da12ec4SLe Tan 
20591da12ec4SLe Tan     case DMAR_CCMD_REG_HI:
20601da12ec4SLe Tan         assert(size == 4);
20611da12ec4SLe Tan         vtd_set_long(s, addr, val);
20621da12ec4SLe Tan         vtd_handle_ccmd_write(s);
20631da12ec4SLe Tan         break;
20641da12ec4SLe Tan 
20651da12ec4SLe Tan     /* IOTLB Invalidation Register, 64-bit */
20661da12ec4SLe Tan     case DMAR_IOTLB_REG:
20671da12ec4SLe Tan         if (size == 4) {
20681da12ec4SLe Tan             vtd_set_long(s, addr, val);
20691da12ec4SLe Tan         } else {
20701da12ec4SLe Tan             vtd_set_quad(s, addr, val);
20711da12ec4SLe Tan             vtd_handle_iotlb_write(s);
20721da12ec4SLe Tan         }
20731da12ec4SLe Tan         break;
20741da12ec4SLe Tan 
20751da12ec4SLe Tan     case DMAR_IOTLB_REG_HI:
20761da12ec4SLe Tan         assert(size == 4);
20771da12ec4SLe Tan         vtd_set_long(s, addr, val);
20781da12ec4SLe Tan         vtd_handle_iotlb_write(s);
20791da12ec4SLe Tan         break;
20801da12ec4SLe Tan 
2081b5a280c0SLe Tan     /* Invalidate Address Register, 64-bit */
2082b5a280c0SLe Tan     case DMAR_IVA_REG:
2083b5a280c0SLe Tan         if (size == 4) {
2084b5a280c0SLe Tan             vtd_set_long(s, addr, val);
2085b5a280c0SLe Tan         } else {
2086b5a280c0SLe Tan             vtd_set_quad(s, addr, val);
2087b5a280c0SLe Tan         }
2088b5a280c0SLe Tan         break;
2089b5a280c0SLe Tan 
2090b5a280c0SLe Tan     case DMAR_IVA_REG_HI:
2091b5a280c0SLe Tan         assert(size == 4);
2092b5a280c0SLe Tan         vtd_set_long(s, addr, val);
2093b5a280c0SLe Tan         break;
2094b5a280c0SLe Tan 
20951da12ec4SLe Tan     /* Fault Status Register, 32-bit */
20961da12ec4SLe Tan     case DMAR_FSTS_REG:
20971da12ec4SLe Tan         assert(size == 4);
20981da12ec4SLe Tan         vtd_set_long(s, addr, val);
20991da12ec4SLe Tan         vtd_handle_fsts_write(s);
21001da12ec4SLe Tan         break;
21011da12ec4SLe Tan 
21021da12ec4SLe Tan     /* Fault Event Control Register, 32-bit */
21031da12ec4SLe Tan     case DMAR_FECTL_REG:
21041da12ec4SLe Tan         assert(size == 4);
21051da12ec4SLe Tan         vtd_set_long(s, addr, val);
21061da12ec4SLe Tan         vtd_handle_fectl_write(s);
21071da12ec4SLe Tan         break;
21081da12ec4SLe Tan 
21091da12ec4SLe Tan     /* Fault Event Data Register, 32-bit */
21101da12ec4SLe Tan     case DMAR_FEDATA_REG:
21111da12ec4SLe Tan         assert(size == 4);
21121da12ec4SLe Tan         vtd_set_long(s, addr, val);
21131da12ec4SLe Tan         break;
21141da12ec4SLe Tan 
21151da12ec4SLe Tan     /* Fault Event Address Register, 32-bit */
21161da12ec4SLe Tan     case DMAR_FEADDR_REG:
21171da12ec4SLe Tan         assert(size == 4);
21181da12ec4SLe Tan         vtd_set_long(s, addr, val);
21191da12ec4SLe Tan         break;
21201da12ec4SLe Tan 
21211da12ec4SLe Tan     /* Fault Event Upper Address Register, 32-bit */
21221da12ec4SLe Tan     case DMAR_FEUADDR_REG:
21231da12ec4SLe Tan         assert(size == 4);
21241da12ec4SLe Tan         vtd_set_long(s, addr, val);
21251da12ec4SLe Tan         break;
21261da12ec4SLe Tan 
21271da12ec4SLe Tan     /* Protected Memory Enable Register, 32-bit */
21281da12ec4SLe Tan     case DMAR_PMEN_REG:
21291da12ec4SLe Tan         assert(size == 4);
21301da12ec4SLe Tan         vtd_set_long(s, addr, val);
21311da12ec4SLe Tan         break;
21321da12ec4SLe Tan 
21331da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
21341da12ec4SLe Tan     case DMAR_RTADDR_REG:
21351da12ec4SLe Tan         if (size == 4) {
21361da12ec4SLe Tan             vtd_set_long(s, addr, val);
21371da12ec4SLe Tan         } else {
21381da12ec4SLe Tan             vtd_set_quad(s, addr, val);
21391da12ec4SLe Tan         }
21401da12ec4SLe Tan         break;
21411da12ec4SLe Tan 
21421da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
21431da12ec4SLe Tan         assert(size == 4);
21441da12ec4SLe Tan         vtd_set_long(s, addr, val);
21451da12ec4SLe Tan         break;
21461da12ec4SLe Tan 
2147ed7b8fbcSLe Tan     /* Invalidation Queue Tail Register, 64-bit */
2148ed7b8fbcSLe Tan     case DMAR_IQT_REG:
2149ed7b8fbcSLe Tan         if (size == 4) {
2150ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
2151ed7b8fbcSLe Tan         } else {
2152ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
2153ed7b8fbcSLe Tan         }
2154ed7b8fbcSLe Tan         vtd_handle_iqt_write(s);
2155ed7b8fbcSLe Tan         break;
2156ed7b8fbcSLe Tan 
2157ed7b8fbcSLe Tan     case DMAR_IQT_REG_HI:
2158ed7b8fbcSLe Tan         assert(size == 4);
2159ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2160ed7b8fbcSLe Tan         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
2161ed7b8fbcSLe Tan         break;
2162ed7b8fbcSLe Tan 
2163ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
2164ed7b8fbcSLe Tan     case DMAR_IQA_REG:
2165ed7b8fbcSLe Tan         if (size == 4) {
2166ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
2167ed7b8fbcSLe Tan         } else {
2168ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
2169ed7b8fbcSLe Tan         }
2170ed7b8fbcSLe Tan         break;
2171ed7b8fbcSLe Tan 
2172ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
2173ed7b8fbcSLe Tan         assert(size == 4);
2174ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2175ed7b8fbcSLe Tan         break;
2176ed7b8fbcSLe Tan 
2177ed7b8fbcSLe Tan     /* Invalidation Completion Status Register, 32-bit */
2178ed7b8fbcSLe Tan     case DMAR_ICS_REG:
2179ed7b8fbcSLe Tan         assert(size == 4);
2180ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2181ed7b8fbcSLe Tan         vtd_handle_ics_write(s);
2182ed7b8fbcSLe Tan         break;
2183ed7b8fbcSLe Tan 
2184ed7b8fbcSLe Tan     /* Invalidation Event Control Register, 32-bit */
2185ed7b8fbcSLe Tan     case DMAR_IECTL_REG:
2186ed7b8fbcSLe Tan         assert(size == 4);
2187ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2188ed7b8fbcSLe Tan         vtd_handle_iectl_write(s);
2189ed7b8fbcSLe Tan         break;
2190ed7b8fbcSLe Tan 
2191ed7b8fbcSLe Tan     /* Invalidation Event Data Register, 32-bit */
2192ed7b8fbcSLe Tan     case DMAR_IEDATA_REG:
2193ed7b8fbcSLe Tan         assert(size == 4);
2194ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2195ed7b8fbcSLe Tan         break;
2196ed7b8fbcSLe Tan 
2197ed7b8fbcSLe Tan     /* Invalidation Event Address Register, 32-bit */
2198ed7b8fbcSLe Tan     case DMAR_IEADDR_REG:
2199ed7b8fbcSLe Tan         assert(size == 4);
2200ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2201ed7b8fbcSLe Tan         break;
2202ed7b8fbcSLe Tan 
2203ed7b8fbcSLe Tan     /* Invalidation Event Upper Address Register, 32-bit */
2204ed7b8fbcSLe Tan     case DMAR_IEUADDR_REG:
2205ed7b8fbcSLe Tan         assert(size == 4);
2206ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
2207ed7b8fbcSLe Tan         break;
2208ed7b8fbcSLe Tan 
22091da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
22101da12ec4SLe Tan     case DMAR_FRCD_REG_0_0:
22111da12ec4SLe Tan         if (size == 4) {
22121da12ec4SLe Tan             vtd_set_long(s, addr, val);
22131da12ec4SLe Tan         } else {
22141da12ec4SLe Tan             vtd_set_quad(s, addr, val);
22151da12ec4SLe Tan         }
22161da12ec4SLe Tan         break;
22171da12ec4SLe Tan 
22181da12ec4SLe Tan     case DMAR_FRCD_REG_0_1:
22191da12ec4SLe Tan         assert(size == 4);
22201da12ec4SLe Tan         vtd_set_long(s, addr, val);
22211da12ec4SLe Tan         break;
22221da12ec4SLe Tan 
22231da12ec4SLe Tan     case DMAR_FRCD_REG_0_2:
22241da12ec4SLe Tan         if (size == 4) {
22251da12ec4SLe Tan             vtd_set_long(s, addr, val);
22261da12ec4SLe Tan         } else {
22271da12ec4SLe Tan             vtd_set_quad(s, addr, val);
22281da12ec4SLe Tan             /* May clear bit 127 (Fault), update PPF */
22291da12ec4SLe Tan             vtd_update_fsts_ppf(s);
22301da12ec4SLe Tan         }
22311da12ec4SLe Tan         break;
22321da12ec4SLe Tan 
22331da12ec4SLe Tan     case DMAR_FRCD_REG_0_3:
22341da12ec4SLe Tan         assert(size == 4);
22351da12ec4SLe Tan         vtd_set_long(s, addr, val);
22361da12ec4SLe Tan         /* May clear bit 127 (Fault), update PPF */
22371da12ec4SLe Tan         vtd_update_fsts_ppf(s);
22381da12ec4SLe Tan         break;
22391da12ec4SLe Tan 
2240a5861439SPeter Xu     case DMAR_IRTA_REG:
2241a5861439SPeter Xu         if (size == 4) {
2242a5861439SPeter Xu             vtd_set_long(s, addr, val);
2243a5861439SPeter Xu         } else {
2244a5861439SPeter Xu             vtd_set_quad(s, addr, val);
2245a5861439SPeter Xu         }
2246a5861439SPeter Xu         break;
2247a5861439SPeter Xu 
2248a5861439SPeter Xu     case DMAR_IRTA_REG_HI:
2249a5861439SPeter Xu         assert(size == 4);
2250a5861439SPeter Xu         vtd_set_long(s, addr, val);
2251a5861439SPeter Xu         break;
2252a5861439SPeter Xu 
22531da12ec4SLe Tan     default:
22541da12ec4SLe Tan         if (size == 4) {
22551da12ec4SLe Tan             vtd_set_long(s, addr, val);
22561da12ec4SLe Tan         } else {
22571da12ec4SLe Tan             vtd_set_quad(s, addr, val);
22581da12ec4SLe Tan         }
22591da12ec4SLe Tan     }
22601da12ec4SLe Tan }
22611da12ec4SLe Tan 
22621da12ec4SLe Tan static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr,
2263bf55b7afSPeter Xu                                          IOMMUAccessFlags flag)
22641da12ec4SLe Tan {
22651da12ec4SLe Tan     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
22661da12ec4SLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
2267b9313021SPeter Xu     IOMMUTLBEntry iotlb = {
2268b9313021SPeter Xu         /* We'll fill in the rest later. */
22691da12ec4SLe Tan         .target_as = &address_space_memory,
22701da12ec4SLe Tan     };
2271b9313021SPeter Xu     bool success;
22721da12ec4SLe Tan 
2273b9313021SPeter Xu     if (likely(s->dmar_enabled)) {
2274b9313021SPeter Xu         success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
2275b9313021SPeter Xu                                          addr, flag & IOMMU_WO, &iotlb);
2276b9313021SPeter Xu     } else {
22771da12ec4SLe Tan         /* DMAR disabled, passthrough, use 4k-page*/
2278b9313021SPeter Xu         iotlb.iova = addr & VTD_PAGE_MASK_4K;
2279b9313021SPeter Xu         iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
2280b9313021SPeter Xu         iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
2281b9313021SPeter Xu         iotlb.perm = IOMMU_RW;
2282b9313021SPeter Xu         success = true;
22831da12ec4SLe Tan     }
22841da12ec4SLe Tan 
2285b9313021SPeter Xu     if (likely(success)) {
22867feb51b7SPeter Xu         trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
22877feb51b7SPeter Xu                                  VTD_PCI_SLOT(vtd_as->devfn),
22887feb51b7SPeter Xu                                  VTD_PCI_FUNC(vtd_as->devfn),
2289b9313021SPeter Xu                                  iotlb.iova, iotlb.translated_addr,
2290b9313021SPeter Xu                                  iotlb.addr_mask);
2291b9313021SPeter Xu     } else {
2292b9313021SPeter Xu         trace_vtd_err_dmar_translate(pci_bus_num(vtd_as->bus),
2293b9313021SPeter Xu                                      VTD_PCI_SLOT(vtd_as->devfn),
2294b9313021SPeter Xu                                      VTD_PCI_FUNC(vtd_as->devfn),
2295b9313021SPeter Xu                                      iotlb.iova);
2296b9313021SPeter Xu     }
22977feb51b7SPeter Xu 
2298b9313021SPeter Xu     return iotlb;
22991da12ec4SLe Tan }
23001da12ec4SLe Tan 
23015bf3d319SPeter Xu static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
23025bf3d319SPeter Xu                                           IOMMUNotifierFlag old,
23035bf3d319SPeter Xu                                           IOMMUNotifierFlag new)
23043cb3b154SAlex Williamson {
23053cb3b154SAlex Williamson     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
2306dd4d607eSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
2307dd4d607eSPeter Xu     IntelIOMMUNotifierNode *node = NULL;
2308dd4d607eSPeter Xu     IntelIOMMUNotifierNode *next_node = NULL;
23093cb3b154SAlex Williamson 
2310dd4d607eSPeter Xu     if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) {
2311dd4d607eSPeter Xu         error_report("We need to set cache_mode=1 for intel-iommu to enable "
2312dd4d607eSPeter Xu                      "device assignment with IOMMU protection.");
2313a3276f78SPeter Xu         exit(1);
2314a3276f78SPeter Xu     }
2315dd4d607eSPeter Xu 
2316dd4d607eSPeter Xu     if (old == IOMMU_NOTIFIER_NONE) {
2317dd4d607eSPeter Xu         node = g_malloc0(sizeof(*node));
2318dd4d607eSPeter Xu         node->vtd_as = vtd_as;
2319dd4d607eSPeter Xu         QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
2320dd4d607eSPeter Xu         return;
2321dd4d607eSPeter Xu     }
2322dd4d607eSPeter Xu 
2323dd4d607eSPeter Xu     /* update notifier node with new flags */
2324dd4d607eSPeter Xu     QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) {
2325dd4d607eSPeter Xu         if (node->vtd_as == vtd_as) {
2326dd4d607eSPeter Xu             if (new == IOMMU_NOTIFIER_NONE) {
2327dd4d607eSPeter Xu                 QLIST_REMOVE(node, next);
2328dd4d607eSPeter Xu                 g_free(node);
2329dd4d607eSPeter Xu             }
2330dd4d607eSPeter Xu             return;
2331dd4d607eSPeter Xu         }
2332dd4d607eSPeter Xu     }
23333cb3b154SAlex Williamson }
23343cb3b154SAlex Williamson 
23351da12ec4SLe Tan static const VMStateDescription vtd_vmstate = {
23361da12ec4SLe Tan     .name = "iommu-intel",
23378cdcf3c1SPeter Xu     .version_id = 1,
23388cdcf3c1SPeter Xu     .minimum_version_id = 1,
23398cdcf3c1SPeter Xu     .priority = MIG_PRI_IOMMU,
23408cdcf3c1SPeter Xu     .fields = (VMStateField[]) {
23418cdcf3c1SPeter Xu         VMSTATE_UINT64(root, IntelIOMMUState),
23428cdcf3c1SPeter Xu         VMSTATE_UINT64(intr_root, IntelIOMMUState),
23438cdcf3c1SPeter Xu         VMSTATE_UINT64(iq, IntelIOMMUState),
23448cdcf3c1SPeter Xu         VMSTATE_UINT32(intr_size, IntelIOMMUState),
23458cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_head, IntelIOMMUState),
23468cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_tail, IntelIOMMUState),
23478cdcf3c1SPeter Xu         VMSTATE_UINT16(iq_size, IntelIOMMUState),
23488cdcf3c1SPeter Xu         VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
23498cdcf3c1SPeter Xu         VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
23508cdcf3c1SPeter Xu         VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
23518cdcf3c1SPeter Xu         VMSTATE_BOOL(root_extended, IntelIOMMUState),
23528cdcf3c1SPeter Xu         VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
23538cdcf3c1SPeter Xu         VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
23548cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
23558cdcf3c1SPeter Xu         VMSTATE_BOOL(intr_eime, IntelIOMMUState),
23568cdcf3c1SPeter Xu         VMSTATE_END_OF_LIST()
23578cdcf3c1SPeter Xu     }
23581da12ec4SLe Tan };
23591da12ec4SLe Tan 
23601da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = {
23611da12ec4SLe Tan     .read = vtd_mem_read,
23621da12ec4SLe Tan     .write = vtd_mem_write,
23631da12ec4SLe Tan     .endianness = DEVICE_LITTLE_ENDIAN,
23641da12ec4SLe Tan     .impl = {
23651da12ec4SLe Tan         .min_access_size = 4,
23661da12ec4SLe Tan         .max_access_size = 8,
23671da12ec4SLe Tan     },
23681da12ec4SLe Tan     .valid = {
23691da12ec4SLe Tan         .min_access_size = 4,
23701da12ec4SLe Tan         .max_access_size = 8,
23711da12ec4SLe Tan     },
23721da12ec4SLe Tan };
23731da12ec4SLe Tan 
23741da12ec4SLe Tan static Property vtd_properties[] = {
23751da12ec4SLe Tan     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
2376e6b6af05SRadim Krčmář     DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
2377e6b6af05SRadim Krčmář                             ON_OFF_AUTO_AUTO),
2378fb506e70SRadim Krčmář     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
23793b40f0e5SAviv Ben-David     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
23801da12ec4SLe Tan     DEFINE_PROP_END_OF_LIST(),
23811da12ec4SLe Tan };
23821da12ec4SLe Tan 
2383651e4cefSPeter Xu /* Read IRTE entry with specific index */
2384651e4cefSPeter Xu static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
2385bc38ee10SMichael S. Tsirkin                         VTD_IR_TableEntry *entry, uint16_t sid)
2386651e4cefSPeter Xu {
2387ede9c94aSPeter Xu     static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
2388ede9c94aSPeter Xu         {0xffff, 0xfffb, 0xfff9, 0xfff8};
2389651e4cefSPeter Xu     dma_addr_t addr = 0x00;
2390ede9c94aSPeter Xu     uint16_t mask, source_id;
2391ede9c94aSPeter Xu     uint8_t bus, bus_max, bus_min;
2392651e4cefSPeter Xu 
2393651e4cefSPeter Xu     addr = iommu->intr_root + index * sizeof(*entry);
2394651e4cefSPeter Xu     if (dma_memory_read(&address_space_memory, addr, entry,
2395651e4cefSPeter Xu                         sizeof(*entry))) {
23967feb51b7SPeter Xu         trace_vtd_err("Memory read failed for IRTE.");
2397651e4cefSPeter Xu         return -VTD_FR_IR_ROOT_INVAL;
2398651e4cefSPeter Xu     }
2399651e4cefSPeter Xu 
24007feb51b7SPeter Xu     trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
24017feb51b7SPeter Xu                           le64_to_cpu(entry->data[0]));
24027feb51b7SPeter Xu 
2403bc38ee10SMichael S. Tsirkin     if (!entry->irte.present) {
24047feb51b7SPeter Xu         trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2405651e4cefSPeter Xu                            le64_to_cpu(entry->data[0]));
2406651e4cefSPeter Xu         return -VTD_FR_IR_ENTRY_P;
2407651e4cefSPeter Xu     }
2408651e4cefSPeter Xu 
2409bc38ee10SMichael S. Tsirkin     if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
2410bc38ee10SMichael S. Tsirkin         entry->irte.__reserved_2) {
24117feb51b7SPeter Xu         trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2412651e4cefSPeter Xu                            le64_to_cpu(entry->data[0]));
2413651e4cefSPeter Xu         return -VTD_FR_IR_IRTE_RSVD;
2414651e4cefSPeter Xu     }
2415651e4cefSPeter Xu 
2416ede9c94aSPeter Xu     if (sid != X86_IOMMU_SID_INVALID) {
2417ede9c94aSPeter Xu         /* Validate IRTE SID */
2418bc38ee10SMichael S. Tsirkin         source_id = le32_to_cpu(entry->irte.source_id);
2419bc38ee10SMichael S. Tsirkin         switch (entry->irte.sid_vtype) {
2420ede9c94aSPeter Xu         case VTD_SVT_NONE:
2421ede9c94aSPeter Xu             break;
2422ede9c94aSPeter Xu 
2423ede9c94aSPeter Xu         case VTD_SVT_ALL:
2424bc38ee10SMichael S. Tsirkin             mask = vtd_svt_mask[entry->irte.sid_q];
2425ede9c94aSPeter Xu             if ((source_id & mask) != (sid & mask)) {
24267feb51b7SPeter Xu                 trace_vtd_err_irte_sid(index, sid, source_id);
2427ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
2428ede9c94aSPeter Xu             }
2429ede9c94aSPeter Xu             break;
2430ede9c94aSPeter Xu 
2431ede9c94aSPeter Xu         case VTD_SVT_BUS:
2432ede9c94aSPeter Xu             bus_max = source_id >> 8;
2433ede9c94aSPeter Xu             bus_min = source_id & 0xff;
2434ede9c94aSPeter Xu             bus = sid >> 8;
2435ede9c94aSPeter Xu             if (bus > bus_max || bus < bus_min) {
24367feb51b7SPeter Xu                 trace_vtd_err_irte_sid_bus(index, bus, bus_min, bus_max);
2437ede9c94aSPeter Xu                 return -VTD_FR_IR_SID_ERR;
2438ede9c94aSPeter Xu             }
2439ede9c94aSPeter Xu             break;
2440ede9c94aSPeter Xu 
2441ede9c94aSPeter Xu         default:
24427feb51b7SPeter Xu             trace_vtd_err_irte_svt(index, entry->irte.sid_vtype);
2443ede9c94aSPeter Xu             /* Take this as verification failure. */
2444ede9c94aSPeter Xu             return -VTD_FR_IR_SID_ERR;
2445ede9c94aSPeter Xu             break;
2446ede9c94aSPeter Xu         }
2447ede9c94aSPeter Xu     }
2448651e4cefSPeter Xu 
2449651e4cefSPeter Xu     return 0;
2450651e4cefSPeter Xu }
2451651e4cefSPeter Xu 
2452651e4cefSPeter Xu /* Fetch IRQ information of specific IR index */
2453ede9c94aSPeter Xu static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
2454ede9c94aSPeter Xu                              VTDIrq *irq, uint16_t sid)
2455651e4cefSPeter Xu {
2456bc38ee10SMichael S. Tsirkin     VTD_IR_TableEntry irte = {};
2457651e4cefSPeter Xu     int ret = 0;
2458651e4cefSPeter Xu 
2459ede9c94aSPeter Xu     ret = vtd_irte_get(iommu, index, &irte, sid);
2460651e4cefSPeter Xu     if (ret) {
2461651e4cefSPeter Xu         return ret;
2462651e4cefSPeter Xu     }
2463651e4cefSPeter Xu 
2464bc38ee10SMichael S. Tsirkin     irq->trigger_mode = irte.irte.trigger_mode;
2465bc38ee10SMichael S. Tsirkin     irq->vector = irte.irte.vector;
2466bc38ee10SMichael S. Tsirkin     irq->delivery_mode = irte.irte.delivery_mode;
2467bc38ee10SMichael S. Tsirkin     irq->dest = le32_to_cpu(irte.irte.dest_id);
246828589311SJan Kiszka     if (!iommu->intr_eime) {
2469651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_MASK         (0xff00ULL)
2470651e4cefSPeter Xu #define  VTD_IR_APIC_DEST_SHIFT        (8)
247128589311SJan Kiszka         irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
2472651e4cefSPeter Xu             VTD_IR_APIC_DEST_SHIFT;
247328589311SJan Kiszka     }
2474bc38ee10SMichael S. Tsirkin     irq->dest_mode = irte.irte.dest_mode;
2475bc38ee10SMichael S. Tsirkin     irq->redir_hint = irte.irte.redir_hint;
2476651e4cefSPeter Xu 
24777feb51b7SPeter Xu     trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
24787feb51b7SPeter Xu                        irq->delivery_mode, irq->dest, irq->dest_mode);
2479651e4cefSPeter Xu 
2480651e4cefSPeter Xu     return 0;
2481651e4cefSPeter Xu }
2482651e4cefSPeter Xu 
2483651e4cefSPeter Xu /* Generate one MSI message from VTDIrq info */
2484651e4cefSPeter Xu static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out)
2485651e4cefSPeter Xu {
2486651e4cefSPeter Xu     VTD_MSIMessage msg = {};
2487651e4cefSPeter Xu 
2488651e4cefSPeter Xu     /* Generate address bits */
2489651e4cefSPeter Xu     msg.dest_mode = irq->dest_mode;
2490651e4cefSPeter Xu     msg.redir_hint = irq->redir_hint;
2491651e4cefSPeter Xu     msg.dest = irq->dest;
249232946019SRadim Krčmář     msg.__addr_hi = irq->dest & 0xffffff00;
2493651e4cefSPeter Xu     msg.__addr_head = cpu_to_le32(0xfee);
2494651e4cefSPeter Xu     /* Keep this from original MSI address bits */
2495651e4cefSPeter Xu     msg.__not_used = irq->msi_addr_last_bits;
2496651e4cefSPeter Xu 
2497651e4cefSPeter Xu     /* Generate data bits */
2498651e4cefSPeter Xu     msg.vector = irq->vector;
2499651e4cefSPeter Xu     msg.delivery_mode = irq->delivery_mode;
2500651e4cefSPeter Xu     msg.level = 1;
2501651e4cefSPeter Xu     msg.trigger_mode = irq->trigger_mode;
2502651e4cefSPeter Xu 
2503651e4cefSPeter Xu     msg_out->address = msg.msi_addr;
2504651e4cefSPeter Xu     msg_out->data = msg.msi_data;
2505651e4cefSPeter Xu }
2506651e4cefSPeter Xu 
2507651e4cefSPeter Xu /* Interrupt remapping for MSI/MSI-X entry */
2508651e4cefSPeter Xu static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
2509651e4cefSPeter Xu                                    MSIMessage *origin,
2510ede9c94aSPeter Xu                                    MSIMessage *translated,
2511ede9c94aSPeter Xu                                    uint16_t sid)
2512651e4cefSPeter Xu {
2513651e4cefSPeter Xu     int ret = 0;
2514651e4cefSPeter Xu     VTD_IR_MSIAddress addr;
2515651e4cefSPeter Xu     uint16_t index;
251609cd058aSMichael S. Tsirkin     VTDIrq irq = {};
2517651e4cefSPeter Xu 
2518651e4cefSPeter Xu     assert(origin && translated);
2519651e4cefSPeter Xu 
25207feb51b7SPeter Xu     trace_vtd_ir_remap_msi_req(origin->address, origin->data);
25217feb51b7SPeter Xu 
2522651e4cefSPeter Xu     if (!iommu || !iommu->intr_enabled) {
2523*e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
2524*e7a3b91fSPeter Xu         goto out;
2525651e4cefSPeter Xu     }
2526651e4cefSPeter Xu 
2527651e4cefSPeter Xu     if (origin->address & VTD_MSI_ADDR_HI_MASK) {
25287feb51b7SPeter Xu         trace_vtd_err("MSI address high 32 bits non-zero when "
25297feb51b7SPeter Xu                       "Interrupt Remapping enabled.");
2530651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
2531651e4cefSPeter Xu     }
2532651e4cefSPeter Xu 
2533651e4cefSPeter Xu     addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
25341a43713bSPeter Xu     if (addr.addr.__head != 0xfee) {
25357feb51b7SPeter Xu         trace_vtd_err("MSI addr low 32 bit invalid.");
2536651e4cefSPeter Xu         return -VTD_FR_IR_REQ_RSVD;
2537651e4cefSPeter Xu     }
2538651e4cefSPeter Xu 
2539651e4cefSPeter Xu     /* This is compatible mode. */
2540bc38ee10SMichael S. Tsirkin     if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
2541*e7a3b91fSPeter Xu         memcpy(translated, origin, sizeof(*origin));
2542*e7a3b91fSPeter Xu         goto out;
2543651e4cefSPeter Xu     }
2544651e4cefSPeter Xu 
2545bc38ee10SMichael S. Tsirkin     index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
2546651e4cefSPeter Xu 
2547651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_SUBHANDLE       (0x0000ffff)
2548651e4cefSPeter Xu #define  VTD_IR_MSI_DATA_RESERVED        (0xffff0000)
2549651e4cefSPeter Xu 
2550bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
2551651e4cefSPeter Xu         /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
2552651e4cefSPeter Xu         index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
2553651e4cefSPeter Xu     }
2554651e4cefSPeter Xu 
2555ede9c94aSPeter Xu     ret = vtd_remap_irq_get(iommu, index, &irq, sid);
2556651e4cefSPeter Xu     if (ret) {
2557651e4cefSPeter Xu         return ret;
2558651e4cefSPeter Xu     }
2559651e4cefSPeter Xu 
2560bc38ee10SMichael S. Tsirkin     if (addr.addr.sub_valid) {
25617feb51b7SPeter Xu         trace_vtd_ir_remap_type("MSI");
2562651e4cefSPeter Xu         if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
25637feb51b7SPeter Xu             trace_vtd_err_ir_msi_invalid(sid, origin->address, origin->data);
2564651e4cefSPeter Xu             return -VTD_FR_IR_REQ_RSVD;
2565651e4cefSPeter Xu         }
2566651e4cefSPeter Xu     } else {
2567651e4cefSPeter Xu         uint8_t vector = origin->data & 0xff;
2568dea651a9SFeng Wu         uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
2569dea651a9SFeng Wu 
25707feb51b7SPeter Xu         trace_vtd_ir_remap_type("IOAPIC");
2571651e4cefSPeter Xu         /* IOAPIC entry vector should be aligned with IRTE vector
2572651e4cefSPeter Xu          * (see vt-d spec 5.1.5.1). */
2573651e4cefSPeter Xu         if (vector != irq.vector) {
25747feb51b7SPeter Xu             trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
2575651e4cefSPeter Xu         }
2576dea651a9SFeng Wu 
2577dea651a9SFeng Wu         /* The Trigger Mode field must match the Trigger Mode in the IRTE.
2578dea651a9SFeng Wu          * (see vt-d spec 5.1.5.1). */
2579dea651a9SFeng Wu         if (trigger_mode != irq.trigger_mode) {
25807feb51b7SPeter Xu             trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
25817feb51b7SPeter Xu                                       irq.trigger_mode);
2582dea651a9SFeng Wu         }
2583651e4cefSPeter Xu     }
2584651e4cefSPeter Xu 
2585651e4cefSPeter Xu     /*
2586651e4cefSPeter Xu      * We'd better keep the last two bits, assuming that guest OS
2587651e4cefSPeter Xu      * might modify it. Keep it does not hurt after all.
2588651e4cefSPeter Xu      */
2589bc38ee10SMichael S. Tsirkin     irq.msi_addr_last_bits = addr.addr.__not_care;
2590651e4cefSPeter Xu 
2591651e4cefSPeter Xu     /* Translate VTDIrq to MSI message */
2592651e4cefSPeter Xu     vtd_generate_msi_message(&irq, translated);
2593651e4cefSPeter Xu 
2594*e7a3b91fSPeter Xu out:
25957feb51b7SPeter Xu     trace_vtd_ir_remap_msi(origin->address, origin->data,
2596651e4cefSPeter Xu                            translated->address, translated->data);
2597651e4cefSPeter Xu     return 0;
2598651e4cefSPeter Xu }
2599651e4cefSPeter Xu 
26008b5ed7dfSPeter Xu static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
26018b5ed7dfSPeter Xu                          MSIMessage *dst, uint16_t sid)
26028b5ed7dfSPeter Xu {
2603ede9c94aSPeter Xu     return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
2604ede9c94aSPeter Xu                                    src, dst, sid);
26058b5ed7dfSPeter Xu }
26068b5ed7dfSPeter Xu 
2607651e4cefSPeter Xu static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
2608651e4cefSPeter Xu                                    uint64_t *data, unsigned size,
2609651e4cefSPeter Xu                                    MemTxAttrs attrs)
2610651e4cefSPeter Xu {
2611651e4cefSPeter Xu     return MEMTX_OK;
2612651e4cefSPeter Xu }
2613651e4cefSPeter Xu 
2614651e4cefSPeter Xu static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
2615651e4cefSPeter Xu                                     uint64_t value, unsigned size,
2616651e4cefSPeter Xu                                     MemTxAttrs attrs)
2617651e4cefSPeter Xu {
2618651e4cefSPeter Xu     int ret = 0;
261909cd058aSMichael S. Tsirkin     MSIMessage from = {}, to = {};
2620ede9c94aSPeter Xu     uint16_t sid = X86_IOMMU_SID_INVALID;
2621651e4cefSPeter Xu 
2622651e4cefSPeter Xu     from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
2623651e4cefSPeter Xu     from.data = (uint32_t) value;
2624651e4cefSPeter Xu 
2625ede9c94aSPeter Xu     if (!attrs.unspecified) {
2626ede9c94aSPeter Xu         /* We have explicit Source ID */
2627ede9c94aSPeter Xu         sid = attrs.requester_id;
2628ede9c94aSPeter Xu     }
2629ede9c94aSPeter Xu 
2630ede9c94aSPeter Xu     ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
2631651e4cefSPeter Xu     if (ret) {
2632651e4cefSPeter Xu         /* TODO: report error */
2633651e4cefSPeter Xu         /* Drop this interrupt */
2634651e4cefSPeter Xu         return MEMTX_ERROR;
2635651e4cefSPeter Xu     }
2636651e4cefSPeter Xu 
263732946019SRadim Krčmář     apic_get_class()->send_msi(&to);
2638651e4cefSPeter Xu 
2639651e4cefSPeter Xu     return MEMTX_OK;
2640651e4cefSPeter Xu }
2641651e4cefSPeter Xu 
2642651e4cefSPeter Xu static const MemoryRegionOps vtd_mem_ir_ops = {
2643651e4cefSPeter Xu     .read_with_attrs = vtd_mem_ir_read,
2644651e4cefSPeter Xu     .write_with_attrs = vtd_mem_ir_write,
2645651e4cefSPeter Xu     .endianness = DEVICE_LITTLE_ENDIAN,
2646651e4cefSPeter Xu     .impl = {
2647651e4cefSPeter Xu         .min_access_size = 4,
2648651e4cefSPeter Xu         .max_access_size = 4,
2649651e4cefSPeter Xu     },
2650651e4cefSPeter Xu     .valid = {
2651651e4cefSPeter Xu         .min_access_size = 4,
2652651e4cefSPeter Xu         .max_access_size = 4,
2653651e4cefSPeter Xu     },
2654651e4cefSPeter Xu };
26557df953bdSKnut Omang 
26567df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
26577df953bdSKnut Omang {
26587df953bdSKnut Omang     uintptr_t key = (uintptr_t)bus;
26597df953bdSKnut Omang     VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key);
26607df953bdSKnut Omang     VTDAddressSpace *vtd_dev_as;
2661e0a3c8ccSJason Wang     char name[128];
26627df953bdSKnut Omang 
26637df953bdSKnut Omang     if (!vtd_bus) {
26642d3fc581SJason Wang         uintptr_t *new_key = g_malloc(sizeof(*new_key));
26652d3fc581SJason Wang         *new_key = (uintptr_t)bus;
26667df953bdSKnut Omang         /* No corresponding free() */
266704af0e18SPeter Xu         vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \
266804af0e18SPeter Xu                             X86_IOMMU_PCI_DEVFN_MAX);
26697df953bdSKnut Omang         vtd_bus->bus = bus;
26702d3fc581SJason Wang         g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus);
26717df953bdSKnut Omang     }
26727df953bdSKnut Omang 
26737df953bdSKnut Omang     vtd_dev_as = vtd_bus->dev_as[devfn];
26747df953bdSKnut Omang 
26757df953bdSKnut Omang     if (!vtd_dev_as) {
2676e0a3c8ccSJason Wang         snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn);
26777df953bdSKnut Omang         vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
26787df953bdSKnut Omang 
26797df953bdSKnut Omang         vtd_dev_as->bus = bus;
26807df953bdSKnut Omang         vtd_dev_as->devfn = (uint8_t)devfn;
26817df953bdSKnut Omang         vtd_dev_as->iommu_state = s;
26827df953bdSKnut Omang         vtd_dev_as->context_cache_entry.context_cache_gen = 0;
2683558e0024SPeter Xu 
2684558e0024SPeter Xu         /*
2685558e0024SPeter Xu          * Memory region relationships looks like (Address range shows
2686558e0024SPeter Xu          * only lower 32 bits to make it short in length...):
2687558e0024SPeter Xu          *
2688558e0024SPeter Xu          * |-----------------+-------------------+----------|
2689558e0024SPeter Xu          * | Name            | Address range     | Priority |
2690558e0024SPeter Xu          * |-----------------+-------------------+----------+
2691558e0024SPeter Xu          * | vtd_root        | 00000000-ffffffff |        0 |
2692558e0024SPeter Xu          * |  intel_iommu    | 00000000-ffffffff |        1 |
2693558e0024SPeter Xu          * |  vtd_sys_alias  | 00000000-ffffffff |        1 |
2694558e0024SPeter Xu          * |  intel_iommu_ir | fee00000-feefffff |       64 |
2695558e0024SPeter Xu          * |-----------------+-------------------+----------|
2696558e0024SPeter Xu          *
2697558e0024SPeter Xu          * We enable/disable DMAR by switching enablement for
2698558e0024SPeter Xu          * vtd_sys_alias and intel_iommu regions. IR region is always
2699558e0024SPeter Xu          * enabled.
2700558e0024SPeter Xu          */
27017df953bdSKnut Omang         memory_region_init_iommu(&vtd_dev_as->iommu, OBJECT(s),
2702558e0024SPeter Xu                                  &s->iommu_ops, "intel_iommu_dmar",
2703558e0024SPeter Xu                                  UINT64_MAX);
2704558e0024SPeter Xu         memory_region_init_alias(&vtd_dev_as->sys_alias, OBJECT(s),
2705558e0024SPeter Xu                                  "vtd_sys_alias", get_system_memory(),
2706558e0024SPeter Xu                                  0, memory_region_size(get_system_memory()));
2707651e4cefSPeter Xu         memory_region_init_io(&vtd_dev_as->iommu_ir, OBJECT(s),
2708651e4cefSPeter Xu                               &vtd_mem_ir_ops, s, "intel_iommu_ir",
2709651e4cefSPeter Xu                               VTD_INTERRUPT_ADDR_SIZE);
2710558e0024SPeter Xu         memory_region_init(&vtd_dev_as->root, OBJECT(s),
2711558e0024SPeter Xu                            "vtd_root", UINT64_MAX);
2712558e0024SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root,
2713558e0024SPeter Xu                                             VTD_INTERRUPT_ADDR_FIRST,
2714558e0024SPeter Xu                                             &vtd_dev_as->iommu_ir, 64);
2715558e0024SPeter Xu         address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, name);
2716558e0024SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
2717558e0024SPeter Xu                                             &vtd_dev_as->sys_alias, 1);
2718558e0024SPeter Xu         memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
2719558e0024SPeter Xu                                             &vtd_dev_as->iommu, 1);
2720558e0024SPeter Xu         vtd_switch_address_space(vtd_dev_as);
27217df953bdSKnut Omang     }
27227df953bdSKnut Omang     return vtd_dev_as;
27237df953bdSKnut Omang }
27247df953bdSKnut Omang 
2725dd4d607eSPeter Xu /* Unmap the whole range in the notifier's scope. */
2726dd4d607eSPeter Xu static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
2727dd4d607eSPeter Xu {
2728dd4d607eSPeter Xu     IOMMUTLBEntry entry;
2729dd4d607eSPeter Xu     hwaddr size;
2730dd4d607eSPeter Xu     hwaddr start = n->start;
2731dd4d607eSPeter Xu     hwaddr end = n->end;
2732dd4d607eSPeter Xu 
2733dd4d607eSPeter Xu     /*
2734dd4d607eSPeter Xu      * Note: all the codes in this function has a assumption that IOVA
2735dd4d607eSPeter Xu      * bits are no more than VTD_MGAW bits (which is restricted by
2736dd4d607eSPeter Xu      * VT-d spec), otherwise we need to consider overflow of 64 bits.
2737dd4d607eSPeter Xu      */
2738dd4d607eSPeter Xu 
2739dd4d607eSPeter Xu     if (end > VTD_ADDRESS_SIZE) {
2740dd4d607eSPeter Xu         /*
2741dd4d607eSPeter Xu          * Don't need to unmap regions that is bigger than the whole
2742dd4d607eSPeter Xu          * VT-d supported address space size
2743dd4d607eSPeter Xu          */
2744dd4d607eSPeter Xu         end = VTD_ADDRESS_SIZE;
2745dd4d607eSPeter Xu     }
2746dd4d607eSPeter Xu 
2747dd4d607eSPeter Xu     assert(start <= end);
2748dd4d607eSPeter Xu     size = end - start;
2749dd4d607eSPeter Xu 
2750dd4d607eSPeter Xu     if (ctpop64(size) != 1) {
2751dd4d607eSPeter Xu         /*
2752dd4d607eSPeter Xu          * This size cannot format a correct mask. Let's enlarge it to
2753dd4d607eSPeter Xu          * suite the minimum available mask.
2754dd4d607eSPeter Xu          */
2755dd4d607eSPeter Xu         int n = 64 - clz64(size);
2756dd4d607eSPeter Xu         if (n > VTD_MGAW) {
2757dd4d607eSPeter Xu             /* should not happen, but in case it happens, limit it */
2758dd4d607eSPeter Xu             n = VTD_MGAW;
2759dd4d607eSPeter Xu         }
2760dd4d607eSPeter Xu         size = 1ULL << n;
2761dd4d607eSPeter Xu     }
2762dd4d607eSPeter Xu 
2763dd4d607eSPeter Xu     entry.target_as = &address_space_memory;
2764dd4d607eSPeter Xu     /* Adjust iova for the size */
2765dd4d607eSPeter Xu     entry.iova = n->start & ~(size - 1);
2766dd4d607eSPeter Xu     /* This field is meaningless for unmap */
2767dd4d607eSPeter Xu     entry.translated_addr = 0;
2768dd4d607eSPeter Xu     entry.perm = IOMMU_NONE;
2769dd4d607eSPeter Xu     entry.addr_mask = size - 1;
2770dd4d607eSPeter Xu 
2771dd4d607eSPeter Xu     trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
2772dd4d607eSPeter Xu                              VTD_PCI_SLOT(as->devfn),
2773dd4d607eSPeter Xu                              VTD_PCI_FUNC(as->devfn),
2774dd4d607eSPeter Xu                              entry.iova, size);
2775dd4d607eSPeter Xu 
2776dd4d607eSPeter Xu     memory_region_notify_one(n, &entry);
2777dd4d607eSPeter Xu }
2778dd4d607eSPeter Xu 
2779dd4d607eSPeter Xu static void vtd_address_space_unmap_all(IntelIOMMUState *s)
2780dd4d607eSPeter Xu {
2781dd4d607eSPeter Xu     IntelIOMMUNotifierNode *node;
2782dd4d607eSPeter Xu     VTDAddressSpace *vtd_as;
2783dd4d607eSPeter Xu     IOMMUNotifier *n;
2784dd4d607eSPeter Xu 
2785dd4d607eSPeter Xu     QLIST_FOREACH(node, &s->notifiers_list, next) {
2786dd4d607eSPeter Xu         vtd_as = node->vtd_as;
2787dd4d607eSPeter Xu         IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
2788dd4d607eSPeter Xu             vtd_address_space_unmap(vtd_as, n);
2789dd4d607eSPeter Xu         }
2790dd4d607eSPeter Xu     }
2791dd4d607eSPeter Xu }
2792dd4d607eSPeter Xu 
2793f06a696dSPeter Xu static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private)
2794f06a696dSPeter Xu {
2795f06a696dSPeter Xu     memory_region_notify_one((IOMMUNotifier *)private, entry);
2796f06a696dSPeter Xu     return 0;
2797f06a696dSPeter Xu }
2798f06a696dSPeter Xu 
2799f06a696dSPeter Xu static void vtd_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n)
2800f06a696dSPeter Xu {
2801f06a696dSPeter Xu     VTDAddressSpace *vtd_as = container_of(mr, VTDAddressSpace, iommu);
2802f06a696dSPeter Xu     IntelIOMMUState *s = vtd_as->iommu_state;
2803f06a696dSPeter Xu     uint8_t bus_n = pci_bus_num(vtd_as->bus);
2804f06a696dSPeter Xu     VTDContextEntry ce;
2805f06a696dSPeter Xu 
2806f06a696dSPeter Xu     /*
2807dd4d607eSPeter Xu      * The replay can be triggered by either a invalidation or a newly
2808dd4d607eSPeter Xu      * created entry. No matter what, we release existing mappings
2809dd4d607eSPeter Xu      * (it means flushing caches for UNMAP-only registers).
2810f06a696dSPeter Xu      */
2811dd4d607eSPeter Xu     vtd_address_space_unmap(vtd_as, n);
2812dd4d607eSPeter Xu 
2813dd4d607eSPeter Xu     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
2814f06a696dSPeter Xu         trace_vtd_replay_ce_valid(bus_n, PCI_SLOT(vtd_as->devfn),
2815f06a696dSPeter Xu                                   PCI_FUNC(vtd_as->devfn),
2816f06a696dSPeter Xu                                   VTD_CONTEXT_ENTRY_DID(ce.hi),
2817f06a696dSPeter Xu                                   ce.hi, ce.lo);
2818dd4d607eSPeter Xu         vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false);
2819f06a696dSPeter Xu     } else {
2820f06a696dSPeter Xu         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
2821f06a696dSPeter Xu                                     PCI_FUNC(vtd_as->devfn));
2822f06a696dSPeter Xu     }
2823f06a696dSPeter Xu 
2824f06a696dSPeter Xu     return;
2825f06a696dSPeter Xu }
2826f06a696dSPeter Xu 
28271da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay
28281da12ec4SLe Tan  * attention when adding new initialization stuff.
28291da12ec4SLe Tan  */
28301da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s)
28311da12ec4SLe Tan {
2832d54bd7f8SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
2833d54bd7f8SPeter Xu 
28341da12ec4SLe Tan     memset(s->csr, 0, DMAR_REG_SIZE);
28351da12ec4SLe Tan     memset(s->wmask, 0, DMAR_REG_SIZE);
28361da12ec4SLe Tan     memset(s->w1cmask, 0, DMAR_REG_SIZE);
28371da12ec4SLe Tan     memset(s->womask, 0, DMAR_REG_SIZE);
28381da12ec4SLe Tan 
28391da12ec4SLe Tan     s->iommu_ops.translate = vtd_iommu_translate;
28405bf3d319SPeter Xu     s->iommu_ops.notify_flag_changed = vtd_iommu_notify_flag_changed;
2841f06a696dSPeter Xu     s->iommu_ops.replay = vtd_iommu_replay;
28421da12ec4SLe Tan     s->root = 0;
28431da12ec4SLe Tan     s->root_extended = false;
28441da12ec4SLe Tan     s->dmar_enabled = false;
28451da12ec4SLe Tan     s->iq_head = 0;
28461da12ec4SLe Tan     s->iq_tail = 0;
28471da12ec4SLe Tan     s->iq = 0;
28481da12ec4SLe Tan     s->iq_size = 0;
28491da12ec4SLe Tan     s->qi_enabled = false;
28501da12ec4SLe Tan     s->iq_last_desc_type = VTD_INV_DESC_NONE;
28511da12ec4SLe Tan     s->next_frcd_reg = 0;
28521da12ec4SLe Tan     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | VTD_CAP_MGAW |
2853d66b969bSJason Wang              VTD_CAP_SAGAW | VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS;
2854ed7b8fbcSLe Tan     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
28551da12ec4SLe Tan 
2856d54bd7f8SPeter Xu     if (x86_iommu->intr_supported) {
2857e6b6af05SRadim Krčmář         s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
2858e6b6af05SRadim Krčmář         if (s->intr_eim == ON_OFF_AUTO_ON) {
2859e6b6af05SRadim Krčmář             s->ecap |= VTD_ECAP_EIM;
2860e6b6af05SRadim Krčmář         }
2861e6b6af05SRadim Krčmář         assert(s->intr_eim != ON_OFF_AUTO_AUTO);
2862d54bd7f8SPeter Xu     }
2863d54bd7f8SPeter Xu 
2864554f5e16SJason Wang     if (x86_iommu->dt_supported) {
2865554f5e16SJason Wang         s->ecap |= VTD_ECAP_DT;
2866554f5e16SJason Wang     }
2867554f5e16SJason Wang 
2868dbaabb25SPeter Xu     if (x86_iommu->pt_supported) {
2869dbaabb25SPeter Xu         s->ecap |= VTD_ECAP_PT;
2870dbaabb25SPeter Xu     }
2871dbaabb25SPeter Xu 
28723b40f0e5SAviv Ben-David     if (s->caching_mode) {
28733b40f0e5SAviv Ben-David         s->cap |= VTD_CAP_CM;
28743b40f0e5SAviv Ben-David     }
28753b40f0e5SAviv Ben-David 
2876d92fa2dcSLe Tan     vtd_reset_context_cache(s);
2877b5a280c0SLe Tan     vtd_reset_iotlb(s);
2878d92fa2dcSLe Tan 
28791da12ec4SLe Tan     /* Define registers with default values and bit semantics */
28801da12ec4SLe Tan     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
28811da12ec4SLe Tan     vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
28821da12ec4SLe Tan     vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
28831da12ec4SLe Tan     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
28841da12ec4SLe Tan     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
28851da12ec4SLe Tan     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
28861da12ec4SLe Tan     vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0);
28871da12ec4SLe Tan     vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
28881da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
28891da12ec4SLe Tan 
28901da12ec4SLe Tan     /* Advanced Fault Logging not supported */
28911da12ec4SLe Tan     vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
28921da12ec4SLe Tan     vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
28931da12ec4SLe Tan     vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
28941da12ec4SLe Tan     vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
28951da12ec4SLe Tan 
28961da12ec4SLe Tan     /* Treated as RsvdZ when EIM in ECAP_REG is not supported
28971da12ec4SLe Tan      * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
28981da12ec4SLe Tan      */
28991da12ec4SLe Tan     vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
29001da12ec4SLe Tan 
29011da12ec4SLe Tan     /* Treated as RO for implementations that PLMR and PHMR fields reported
29021da12ec4SLe Tan      * as Clear in the CAP_REG.
29031da12ec4SLe Tan      * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
29041da12ec4SLe Tan      */
29051da12ec4SLe Tan     vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
29061da12ec4SLe Tan 
2907ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
2908ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
2909ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
2910ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
2911ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
2912ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
2913ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
2914ed7b8fbcSLe Tan     /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
2915ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
2916ed7b8fbcSLe Tan 
29171da12ec4SLe Tan     /* IOTLB registers */
29181da12ec4SLe Tan     vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
29191da12ec4SLe Tan     vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
29201da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
29211da12ec4SLe Tan 
29221da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
29231da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
29241da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
2925a5861439SPeter Xu 
2926a5861439SPeter Xu     /*
292728589311SJan Kiszka      * Interrupt remapping registers.
2928a5861439SPeter Xu      */
292928589311SJan Kiszka     vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
29301da12ec4SLe Tan }
29311da12ec4SLe Tan 
29321da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use
29331da12ec4SLe Tan  * the address space they got at first (won't ask the bus again).
29341da12ec4SLe Tan  */
29351da12ec4SLe Tan static void vtd_reset(DeviceState *dev)
29361da12ec4SLe Tan {
29371da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
29381da12ec4SLe Tan 
29391da12ec4SLe Tan     vtd_init(s);
2940dd4d607eSPeter Xu 
2941dd4d607eSPeter Xu     /*
2942dd4d607eSPeter Xu      * When device reset, throw away all mappings and external caches
2943dd4d607eSPeter Xu      */
2944dd4d607eSPeter Xu     vtd_address_space_unmap_all(s);
29451da12ec4SLe Tan }
29461da12ec4SLe Tan 
2947621d983aSMarcel Apfelbaum static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
2948621d983aSMarcel Apfelbaum {
2949621d983aSMarcel Apfelbaum     IntelIOMMUState *s = opaque;
2950621d983aSMarcel Apfelbaum     VTDAddressSpace *vtd_as;
2951621d983aSMarcel Apfelbaum 
29528e7a0a16SPeter Xu     assert(0 <= devfn && devfn < X86_IOMMU_PCI_DEVFN_MAX);
2953621d983aSMarcel Apfelbaum 
2954621d983aSMarcel Apfelbaum     vtd_as = vtd_find_add_as(s, bus, devfn);
2955621d983aSMarcel Apfelbaum     return &vtd_as->as;
2956621d983aSMarcel Apfelbaum }
2957621d983aSMarcel Apfelbaum 
2958e6b6af05SRadim Krčmář static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
29596333e93cSRadim Krčmář {
2960e6b6af05SRadim Krčmář     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
2961e6b6af05SRadim Krčmář 
29626333e93cSRadim Krčmář     /* Currently Intel IOMMU IR only support "kernel-irqchip={off|split}" */
29636333e93cSRadim Krčmář     if (x86_iommu->intr_supported && kvm_irqchip_in_kernel() &&
29646333e93cSRadim Krčmář         !kvm_irqchip_is_split()) {
29656333e93cSRadim Krčmář         error_setg(errp, "Intel Interrupt Remapping cannot work with "
29666333e93cSRadim Krčmář                          "kernel-irqchip=on, please use 'split|off'.");
29676333e93cSRadim Krčmář         return false;
29686333e93cSRadim Krčmář     }
2969e6b6af05SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu->intr_supported) {
2970e6b6af05SRadim Krčmář         error_setg(errp, "eim=on cannot be selected without intremap=on");
2971e6b6af05SRadim Krčmář         return false;
2972e6b6af05SRadim Krčmář     }
2973e6b6af05SRadim Krčmář 
2974e6b6af05SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_AUTO) {
2975fb506e70SRadim Krčmář         s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
2976fb506e70SRadim Krčmář                       && x86_iommu->intr_supported ?
2977e6b6af05SRadim Krčmář                                               ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
2978e6b6af05SRadim Krčmář     }
2979fb506e70SRadim Krčmář     if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
2980fb506e70SRadim Krčmář         if (!kvm_irqchip_in_kernel()) {
2981fb506e70SRadim Krčmář             error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
2982fb506e70SRadim Krčmář             return false;
2983fb506e70SRadim Krčmář         }
2984fb506e70SRadim Krčmář         if (!kvm_enable_x2apic()) {
2985fb506e70SRadim Krčmář             error_setg(errp, "eim=on requires support on the KVM side"
2986fb506e70SRadim Krčmář                              "(X2APIC_API, first shipped in v4.7)");
2987fb506e70SRadim Krčmář             return false;
2988fb506e70SRadim Krčmář         }
2989fb506e70SRadim Krčmář     }
2990e6b6af05SRadim Krčmář 
29916333e93cSRadim Krčmář     return true;
29926333e93cSRadim Krčmář }
29936333e93cSRadim Krčmář 
29941da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp)
29951da12ec4SLe Tan {
2996ef0e8fc7SEduardo Habkost     MachineState *ms = MACHINE(qdev_get_machine());
2997ef0e8fc7SEduardo Habkost     MachineClass *mc = MACHINE_GET_CLASS(ms);
2998ef0e8fc7SEduardo Habkost     PCMachineState *pcms =
2999ef0e8fc7SEduardo Habkost         PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
3000ef0e8fc7SEduardo Habkost     PCIBus *bus;
30011da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
30024684a204SPeter Xu     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
30031da12ec4SLe Tan 
3004ef0e8fc7SEduardo Habkost     if (!pcms) {
3005ef0e8fc7SEduardo Habkost         error_setg(errp, "Machine-type '%s' not supported by intel-iommu",
3006ef0e8fc7SEduardo Habkost                    mc->name);
3007ef0e8fc7SEduardo Habkost         return;
3008ef0e8fc7SEduardo Habkost     }
3009ef0e8fc7SEduardo Habkost 
3010ef0e8fc7SEduardo Habkost     bus = pcms->bus;
3011fb9f5926SDavid Kiarie     x86_iommu->type = TYPE_INTEL;
30126333e93cSRadim Krčmář 
3013e6b6af05SRadim Krčmář     if (!vtd_decide_config(s, errp)) {
30146333e93cSRadim Krčmář         return;
30156333e93cSRadim Krčmář     }
30166333e93cSRadim Krčmář 
3017dd4d607eSPeter Xu     QLIST_INIT(&s->notifiers_list);
30187df953bdSKnut Omang     memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num));
30191da12ec4SLe Tan     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
30201da12ec4SLe Tan                           "intel_iommu", DMAR_REG_SIZE);
30211da12ec4SLe Tan     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
3022b5a280c0SLe Tan     /* No corresponding destroy */
3023b5a280c0SLe Tan     s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3024b5a280c0SLe Tan                                      g_free, g_free);
30257df953bdSKnut Omang     s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
30267df953bdSKnut Omang                                               g_free, g_free);
30271da12ec4SLe Tan     vtd_init(s);
3028621d983aSMarcel Apfelbaum     sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
3029621d983aSMarcel Apfelbaum     pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
3030cb135f59SPeter Xu     /* Pseudo address space under root PCI bus. */
3031cb135f59SPeter Xu     pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
30321da12ec4SLe Tan }
30331da12ec4SLe Tan 
30341da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data)
30351da12ec4SLe Tan {
30361da12ec4SLe Tan     DeviceClass *dc = DEVICE_CLASS(klass);
30371c7955c4SPeter Xu     X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass);
30381da12ec4SLe Tan 
30391da12ec4SLe Tan     dc->reset = vtd_reset;
30401da12ec4SLe Tan     dc->vmsd = &vtd_vmstate;
30411da12ec4SLe Tan     dc->props = vtd_properties;
3042621d983aSMarcel Apfelbaum     dc->hotpluggable = false;
30431c7955c4SPeter Xu     x86_class->realize = vtd_realize;
30448b5ed7dfSPeter Xu     x86_class->int_remap = vtd_int_remap;
30458ab5700cSEduardo Habkost     /* Supported by the pc-q35-* machine types */
3046e4f4fb1eSEduardo Habkost     dc->user_creatable = true;
30471da12ec4SLe Tan }
30481da12ec4SLe Tan 
30491da12ec4SLe Tan static const TypeInfo vtd_info = {
30501da12ec4SLe Tan     .name          = TYPE_INTEL_IOMMU_DEVICE,
30511c7955c4SPeter Xu     .parent        = TYPE_X86_IOMMU_DEVICE,
30521da12ec4SLe Tan     .instance_size = sizeof(IntelIOMMUState),
30531da12ec4SLe Tan     .class_init    = vtd_class_init,
30541da12ec4SLe Tan };
30551da12ec4SLe Tan 
30561da12ec4SLe Tan static void vtd_register_types(void)
30571da12ec4SLe Tan {
30581da12ec4SLe Tan     type_register_static(&vtd_info);
30591da12ec4SLe Tan }
30601da12ec4SLe Tan 
30611da12ec4SLe Tan type_init(vtd_register_types)
3062