xref: /openbmc/qemu/hw/i386/intel_iommu.c (revision 7df953bd456da45f761064974820ab5c3fd7b2aa)
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 
221da12ec4SLe Tan #include "hw/sysbus.h"
231da12ec4SLe Tan #include "exec/address-spaces.h"
241da12ec4SLe Tan #include "intel_iommu_internal.h"
25*7df953bdSKnut Omang #include "hw/pci/pci.h"
261da12ec4SLe Tan 
271da12ec4SLe Tan /*#define DEBUG_INTEL_IOMMU*/
281da12ec4SLe Tan #ifdef DEBUG_INTEL_IOMMU
291da12ec4SLe Tan enum {
301da12ec4SLe Tan     DEBUG_GENERAL, DEBUG_CSR, DEBUG_INV, DEBUG_MMU, DEBUG_FLOG,
31d92fa2dcSLe Tan     DEBUG_CACHE,
321da12ec4SLe Tan };
331da12ec4SLe Tan #define VTD_DBGBIT(x)   (1 << DEBUG_##x)
341da12ec4SLe Tan static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);
351da12ec4SLe Tan 
361da12ec4SLe Tan #define VTD_DPRINTF(what, fmt, ...) do { \
371da12ec4SLe Tan     if (vtd_dbgflags & VTD_DBGBIT(what)) { \
381da12ec4SLe Tan         fprintf(stderr, "(vtd)%s: " fmt "\n", __func__, \
391da12ec4SLe Tan                 ## __VA_ARGS__); } \
401da12ec4SLe Tan     } while (0)
411da12ec4SLe Tan #else
421da12ec4SLe Tan #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
431da12ec4SLe Tan #endif
441da12ec4SLe Tan 
451da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
461da12ec4SLe Tan                             uint64_t wmask, uint64_t w1cmask)
471da12ec4SLe Tan {
481da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
491da12ec4SLe Tan     stq_le_p(&s->wmask[addr], wmask);
501da12ec4SLe Tan     stq_le_p(&s->w1cmask[addr], w1cmask);
511da12ec4SLe Tan }
521da12ec4SLe Tan 
531da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
541da12ec4SLe Tan {
551da12ec4SLe Tan     stq_le_p(&s->womask[addr], mask);
561da12ec4SLe Tan }
571da12ec4SLe Tan 
581da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
591da12ec4SLe Tan                             uint32_t wmask, uint32_t w1cmask)
601da12ec4SLe Tan {
611da12ec4SLe Tan     stl_le_p(&s->csr[addr], val);
621da12ec4SLe Tan     stl_le_p(&s->wmask[addr], wmask);
631da12ec4SLe Tan     stl_le_p(&s->w1cmask[addr], w1cmask);
641da12ec4SLe Tan }
651da12ec4SLe Tan 
661da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
671da12ec4SLe Tan {
681da12ec4SLe Tan     stl_le_p(&s->womask[addr], mask);
691da12ec4SLe Tan }
701da12ec4SLe Tan 
711da12ec4SLe Tan /* "External" get/set operations */
721da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
731da12ec4SLe Tan {
741da12ec4SLe Tan     uint64_t oldval = ldq_le_p(&s->csr[addr]);
751da12ec4SLe Tan     uint64_t wmask = ldq_le_p(&s->wmask[addr]);
761da12ec4SLe Tan     uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
771da12ec4SLe Tan     stq_le_p(&s->csr[addr],
781da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
791da12ec4SLe Tan }
801da12ec4SLe Tan 
811da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
821da12ec4SLe Tan {
831da12ec4SLe Tan     uint32_t oldval = ldl_le_p(&s->csr[addr]);
841da12ec4SLe Tan     uint32_t wmask = ldl_le_p(&s->wmask[addr]);
851da12ec4SLe Tan     uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
861da12ec4SLe Tan     stl_le_p(&s->csr[addr],
871da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
881da12ec4SLe Tan }
891da12ec4SLe Tan 
901da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
911da12ec4SLe Tan {
921da12ec4SLe Tan     uint64_t val = ldq_le_p(&s->csr[addr]);
931da12ec4SLe Tan     uint64_t womask = ldq_le_p(&s->womask[addr]);
941da12ec4SLe Tan     return val & ~womask;
951da12ec4SLe Tan }
961da12ec4SLe Tan 
971da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
981da12ec4SLe Tan {
991da12ec4SLe Tan     uint32_t val = ldl_le_p(&s->csr[addr]);
1001da12ec4SLe Tan     uint32_t womask = ldl_le_p(&s->womask[addr]);
1011da12ec4SLe Tan     return val & ~womask;
1021da12ec4SLe Tan }
1031da12ec4SLe Tan 
1041da12ec4SLe Tan /* "Internal" get/set operations */
1051da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
1061da12ec4SLe Tan {
1071da12ec4SLe Tan     return ldq_le_p(&s->csr[addr]);
1081da12ec4SLe Tan }
1091da12ec4SLe Tan 
1101da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
1111da12ec4SLe Tan {
1121da12ec4SLe Tan     return ldl_le_p(&s->csr[addr]);
1131da12ec4SLe Tan }
1141da12ec4SLe Tan 
1151da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
1161da12ec4SLe Tan {
1171da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
1181da12ec4SLe Tan }
1191da12ec4SLe Tan 
1201da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
1211da12ec4SLe Tan                                         uint32_t clear, uint32_t mask)
1221da12ec4SLe Tan {
1231da12ec4SLe Tan     uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
1241da12ec4SLe Tan     stl_le_p(&s->csr[addr], new_val);
1251da12ec4SLe Tan     return new_val;
1261da12ec4SLe Tan }
1271da12ec4SLe Tan 
1281da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
1291da12ec4SLe Tan                                         uint64_t clear, uint64_t mask)
1301da12ec4SLe Tan {
1311da12ec4SLe Tan     uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
1321da12ec4SLe Tan     stq_le_p(&s->csr[addr], new_val);
1331da12ec4SLe Tan     return new_val;
1341da12ec4SLe Tan }
1351da12ec4SLe Tan 
136b5a280c0SLe Tan /* GHashTable functions */
137b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
138b5a280c0SLe Tan {
139b5a280c0SLe Tan     return *((const uint64_t *)v1) == *((const uint64_t *)v2);
140b5a280c0SLe Tan }
141b5a280c0SLe Tan 
142b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v)
143b5a280c0SLe Tan {
144b5a280c0SLe Tan     return (guint)*(const uint64_t *)v;
145b5a280c0SLe Tan }
146b5a280c0SLe Tan 
147b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
148b5a280c0SLe Tan                                           gpointer user_data)
149b5a280c0SLe Tan {
150b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
151b5a280c0SLe Tan     uint16_t domain_id = *(uint16_t *)user_data;
152b5a280c0SLe Tan     return entry->domain_id == domain_id;
153b5a280c0SLe Tan }
154b5a280c0SLe Tan 
155b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
156b5a280c0SLe Tan                                         gpointer user_data)
157b5a280c0SLe Tan {
158b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
159b5a280c0SLe Tan     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
160b5a280c0SLe Tan     uint64_t gfn = info->gfn & info->mask;
161b5a280c0SLe Tan     return (entry->domain_id == info->domain_id) &&
162b5a280c0SLe Tan             ((entry->gfn & info->mask) == gfn);
163b5a280c0SLe Tan }
164b5a280c0SLe Tan 
165d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of
166d92fa2dcSLe Tan  * IntelIOMMUState to 1.
167d92fa2dcSLe Tan  */
168d92fa2dcSLe Tan static void vtd_reset_context_cache(IntelIOMMUState *s)
169d92fa2dcSLe Tan {
170d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
171*7df953bdSKnut Omang     VTDBus *vtd_bus;
172*7df953bdSKnut Omang     GHashTableIter bus_it;
173d92fa2dcSLe Tan     uint32_t devfn_it;
174d92fa2dcSLe Tan 
175*7df953bdSKnut Omang     g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr);
176*7df953bdSKnut Omang 
177d92fa2dcSLe Tan     VTD_DPRINTF(CACHE, "global context_cache_gen=1");
178*7df953bdSKnut Omang     while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) {
179d92fa2dcSLe Tan         for (devfn_it = 0; devfn_it < VTD_PCI_DEVFN_MAX; ++devfn_it) {
180*7df953bdSKnut Omang             vtd_as = vtd_bus->dev_as[devfn_it];
181d92fa2dcSLe Tan             if (!vtd_as) {
182d92fa2dcSLe Tan                 continue;
183d92fa2dcSLe Tan             }
184d92fa2dcSLe Tan             vtd_as->context_cache_entry.context_cache_gen = 0;
185d92fa2dcSLe Tan         }
186d92fa2dcSLe Tan     }
187d92fa2dcSLe Tan     s->context_cache_gen = 1;
188d92fa2dcSLe Tan }
189d92fa2dcSLe Tan 
190b5a280c0SLe Tan static void vtd_reset_iotlb(IntelIOMMUState *s)
191b5a280c0SLe Tan {
192b5a280c0SLe Tan     assert(s->iotlb);
193b5a280c0SLe Tan     g_hash_table_remove_all(s->iotlb);
194b5a280c0SLe Tan }
195b5a280c0SLe Tan 
196b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
197b5a280c0SLe Tan                                        hwaddr addr)
198b5a280c0SLe Tan {
199b5a280c0SLe Tan     uint64_t key;
200b5a280c0SLe Tan 
201b5a280c0SLe Tan     key = (addr >> VTD_PAGE_SHIFT_4K) |
202b5a280c0SLe Tan            ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT);
203b5a280c0SLe Tan     return g_hash_table_lookup(s->iotlb, &key);
204b5a280c0SLe Tan 
205b5a280c0SLe Tan }
206b5a280c0SLe Tan 
207b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
208b5a280c0SLe Tan                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
209b5a280c0SLe Tan                              bool read_flags, bool write_flags)
210b5a280c0SLe Tan {
211b5a280c0SLe Tan     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
212b5a280c0SLe Tan     uint64_t *key = g_malloc(sizeof(*key));
213b5a280c0SLe Tan     uint64_t gfn = addr >> VTD_PAGE_SHIFT_4K;
214b5a280c0SLe Tan 
215b5a280c0SLe Tan     VTD_DPRINTF(CACHE, "update iotlb sid 0x%"PRIx16 " gpa 0x%"PRIx64
216b5a280c0SLe Tan                 " slpte 0x%"PRIx64 " did 0x%"PRIx16, source_id, addr, slpte,
217b5a280c0SLe Tan                 domain_id);
218b5a280c0SLe Tan     if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
219b5a280c0SLe Tan         VTD_DPRINTF(CACHE, "iotlb exceeds size limit, forced to reset");
220b5a280c0SLe Tan         vtd_reset_iotlb(s);
221b5a280c0SLe Tan     }
222b5a280c0SLe Tan 
223b5a280c0SLe Tan     entry->gfn = gfn;
224b5a280c0SLe Tan     entry->domain_id = domain_id;
225b5a280c0SLe Tan     entry->slpte = slpte;
226b5a280c0SLe Tan     entry->read_flags = read_flags;
227b5a280c0SLe Tan     entry->write_flags = write_flags;
228b5a280c0SLe Tan     *key = gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT);
229b5a280c0SLe Tan     g_hash_table_replace(s->iotlb, key, entry);
230b5a280c0SLe Tan }
231b5a280c0SLe Tan 
2321da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an
2331da12ec4SLe Tan  * interrupt via MSI.
2341da12ec4SLe Tan  */
2351da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
2361da12ec4SLe Tan                                    hwaddr mesg_data_reg)
2371da12ec4SLe Tan {
2381da12ec4SLe Tan     hwaddr addr;
2391da12ec4SLe Tan     uint32_t data;
2401da12ec4SLe Tan 
2411da12ec4SLe Tan     assert(mesg_data_reg < DMAR_REG_SIZE);
2421da12ec4SLe Tan     assert(mesg_addr_reg < DMAR_REG_SIZE);
2431da12ec4SLe Tan 
2441da12ec4SLe Tan     addr = vtd_get_long_raw(s, mesg_addr_reg);
2451da12ec4SLe Tan     data = vtd_get_long_raw(s, mesg_data_reg);
2461da12ec4SLe Tan 
2471da12ec4SLe Tan     VTD_DPRINTF(FLOG, "msi: addr 0x%"PRIx64 " data 0x%"PRIx32, addr, data);
24842874d3aSPeter Maydell     address_space_stl_le(&address_space_memory, addr, data,
24942874d3aSPeter Maydell                          MEMTXATTRS_UNSPECIFIED, NULL);
2501da12ec4SLe Tan }
2511da12ec4SLe Tan 
2521da12ec4SLe Tan /* Generate a fault event to software via MSI if conditions are met.
2531da12ec4SLe Tan  * Notice that the value of FSTS_REG being passed to it should be the one
2541da12ec4SLe Tan  * before any update.
2551da12ec4SLe Tan  */
2561da12ec4SLe Tan static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
2571da12ec4SLe Tan {
2581da12ec4SLe Tan     if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
2591da12ec4SLe Tan         pre_fsts & VTD_FSTS_IQE) {
2601da12ec4SLe Tan         VTD_DPRINTF(FLOG, "there are previous interrupt conditions "
2611da12ec4SLe Tan                     "to be serviced by software, fault event is not generated "
2621da12ec4SLe Tan                     "(FSTS_REG 0x%"PRIx32 ")", pre_fsts);
2631da12ec4SLe Tan         return;
2641da12ec4SLe Tan     }
2651da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
2661da12ec4SLe Tan     if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
2671da12ec4SLe Tan         VTD_DPRINTF(FLOG, "Interrupt Mask set, fault event is not generated");
2681da12ec4SLe Tan     } else {
2691da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
2701da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
2711da12ec4SLe Tan     }
2721da12ec4SLe Tan }
2731da12ec4SLe Tan 
2741da12ec4SLe Tan /* Check if the Fault (F) field of the Fault Recording Register referenced by
2751da12ec4SLe Tan  * @index is Set.
2761da12ec4SLe Tan  */
2771da12ec4SLe Tan static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
2781da12ec4SLe Tan {
2791da12ec4SLe Tan     /* Each reg is 128-bit */
2801da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
2811da12ec4SLe Tan     addr += 8; /* Access the high 64-bit half */
2821da12ec4SLe Tan 
2831da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
2841da12ec4SLe Tan 
2851da12ec4SLe Tan     return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
2861da12ec4SLe Tan }
2871da12ec4SLe Tan 
2881da12ec4SLe Tan /* Update the PPF field of Fault Status Register.
2891da12ec4SLe Tan  * Should be called whenever change the F field of any fault recording
2901da12ec4SLe Tan  * registers.
2911da12ec4SLe Tan  */
2921da12ec4SLe Tan static void vtd_update_fsts_ppf(IntelIOMMUState *s)
2931da12ec4SLe Tan {
2941da12ec4SLe Tan     uint32_t i;
2951da12ec4SLe Tan     uint32_t ppf_mask = 0;
2961da12ec4SLe Tan 
2971da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
2981da12ec4SLe Tan         if (vtd_is_frcd_set(s, i)) {
2991da12ec4SLe Tan             ppf_mask = VTD_FSTS_PPF;
3001da12ec4SLe Tan             break;
3011da12ec4SLe Tan         }
3021da12ec4SLe Tan     }
3031da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
3041da12ec4SLe Tan     VTD_DPRINTF(FLOG, "set PPF of FSTS_REG to %d", ppf_mask ? 1 : 0);
3051da12ec4SLe Tan }
3061da12ec4SLe Tan 
3071da12ec4SLe Tan static void vtd_set_frcd_and_update_ppf(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     vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
3161da12ec4SLe Tan     vtd_update_fsts_ppf(s);
3171da12ec4SLe Tan }
3181da12ec4SLe Tan 
3191da12ec4SLe Tan /* Must not update F field now, should be done later */
3201da12ec4SLe Tan static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
3211da12ec4SLe Tan                             uint16_t source_id, hwaddr addr,
3221da12ec4SLe Tan                             VTDFaultReason fault, bool is_write)
3231da12ec4SLe Tan {
3241da12ec4SLe Tan     uint64_t hi = 0, lo;
3251da12ec4SLe Tan     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
3261da12ec4SLe Tan 
3271da12ec4SLe Tan     assert(index < DMAR_FRCD_REG_NR);
3281da12ec4SLe Tan 
3291da12ec4SLe Tan     lo = VTD_FRCD_FI(addr);
3301da12ec4SLe Tan     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
3311da12ec4SLe Tan     if (!is_write) {
3321da12ec4SLe Tan         hi |= VTD_FRCD_T;
3331da12ec4SLe Tan     }
3341da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr, lo);
3351da12ec4SLe Tan     vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
3361da12ec4SLe Tan     VTD_DPRINTF(FLOG, "record to FRCD_REG #%"PRIu16 ": hi 0x%"PRIx64
3371da12ec4SLe Tan                 ", lo 0x%"PRIx64, index, hi, lo);
3381da12ec4SLe Tan }
3391da12ec4SLe Tan 
3401da12ec4SLe Tan /* Try to collapse multiple pending faults from the same requester */
3411da12ec4SLe Tan static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
3421da12ec4SLe Tan {
3431da12ec4SLe Tan     uint32_t i;
3441da12ec4SLe Tan     uint64_t frcd_reg;
3451da12ec4SLe Tan     hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
3461da12ec4SLe Tan 
3471da12ec4SLe Tan     for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
3481da12ec4SLe Tan         frcd_reg = vtd_get_quad_raw(s, addr);
3491da12ec4SLe Tan         VTD_DPRINTF(FLOG, "frcd_reg #%d 0x%"PRIx64, i, frcd_reg);
3501da12ec4SLe Tan         if ((frcd_reg & VTD_FRCD_F) &&
3511da12ec4SLe Tan             ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
3521da12ec4SLe Tan             return true;
3531da12ec4SLe Tan         }
3541da12ec4SLe Tan         addr += 16; /* 128-bit for each */
3551da12ec4SLe Tan     }
3561da12ec4SLe Tan     return false;
3571da12ec4SLe Tan }
3581da12ec4SLe Tan 
3591da12ec4SLe Tan /* Log and report an DMAR (address translation) fault to software */
3601da12ec4SLe Tan static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
3611da12ec4SLe Tan                                   hwaddr addr, VTDFaultReason fault,
3621da12ec4SLe Tan                                   bool is_write)
3631da12ec4SLe Tan {
3641da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
3651da12ec4SLe Tan 
3661da12ec4SLe Tan     assert(fault < VTD_FR_MAX);
3671da12ec4SLe Tan 
3681da12ec4SLe Tan     if (fault == VTD_FR_RESERVED_ERR) {
3691da12ec4SLe Tan         /* This is not a normal fault reason case. Drop it. */
3701da12ec4SLe Tan         return;
3711da12ec4SLe Tan     }
3721da12ec4SLe Tan     VTD_DPRINTF(FLOG, "sid 0x%"PRIx16 ", fault %d, addr 0x%"PRIx64
3731da12ec4SLe Tan                 ", is_write %d", source_id, fault, addr, is_write);
3741da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PFO) {
3751da12ec4SLe Tan         VTD_DPRINTF(FLOG, "new fault is not recorded due to "
3761da12ec4SLe Tan                     "Primary Fault Overflow");
3771da12ec4SLe Tan         return;
3781da12ec4SLe Tan     }
3791da12ec4SLe Tan     if (vtd_try_collapse_fault(s, source_id)) {
3801da12ec4SLe Tan         VTD_DPRINTF(FLOG, "new fault is not recorded due to "
3811da12ec4SLe Tan                     "compression of faults");
3821da12ec4SLe Tan         return;
3831da12ec4SLe Tan     }
3841da12ec4SLe Tan     if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
3851da12ec4SLe Tan         VTD_DPRINTF(FLOG, "Primary Fault Overflow and "
3861da12ec4SLe Tan                     "new fault is not recorded, set PFO field");
3871da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
3881da12ec4SLe Tan         return;
3891da12ec4SLe Tan     }
3901da12ec4SLe Tan 
3911da12ec4SLe Tan     vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
3921da12ec4SLe Tan 
3931da12ec4SLe Tan     if (fsts_reg & VTD_FSTS_PPF) {
3941da12ec4SLe Tan         VTD_DPRINTF(FLOG, "there are pending faults already, "
3951da12ec4SLe Tan                     "fault event is not generated");
3961da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
3971da12ec4SLe Tan         s->next_frcd_reg++;
3981da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
3991da12ec4SLe Tan             s->next_frcd_reg = 0;
4001da12ec4SLe Tan         }
4011da12ec4SLe Tan     } else {
4021da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
4031da12ec4SLe Tan                                 VTD_FSTS_FRI(s->next_frcd_reg));
4041da12ec4SLe Tan         vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
4051da12ec4SLe Tan         s->next_frcd_reg++;
4061da12ec4SLe Tan         if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
4071da12ec4SLe Tan             s->next_frcd_reg = 0;
4081da12ec4SLe Tan         }
4091da12ec4SLe Tan         /* This case actually cause the PPF to be Set.
4101da12ec4SLe Tan          * So generate fault event (interrupt).
4111da12ec4SLe Tan          */
4121da12ec4SLe Tan          vtd_generate_fault_event(s, fsts_reg);
4131da12ec4SLe Tan     }
4141da12ec4SLe Tan }
4151da12ec4SLe Tan 
416ed7b8fbcSLe Tan /* Handle Invalidation Queue Errors of queued invalidation interface error
417ed7b8fbcSLe Tan  * conditions.
418ed7b8fbcSLe Tan  */
419ed7b8fbcSLe Tan static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
420ed7b8fbcSLe Tan {
421ed7b8fbcSLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
422ed7b8fbcSLe Tan 
423ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
424ed7b8fbcSLe Tan     vtd_generate_fault_event(s, fsts_reg);
425ed7b8fbcSLe Tan }
426ed7b8fbcSLe Tan 
427ed7b8fbcSLe Tan /* Set the IWC field and try to generate an invalidation completion interrupt */
428ed7b8fbcSLe Tan static void vtd_generate_completion_event(IntelIOMMUState *s)
429ed7b8fbcSLe Tan {
430ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "completes an invalidation wait command with "
431ed7b8fbcSLe Tan                 "Interrupt Flag");
432ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
433ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "there is a previous interrupt condition to be "
434ed7b8fbcSLe Tan                     "serviced by software, "
435ed7b8fbcSLe Tan                     "new invalidation event is not generated");
436ed7b8fbcSLe Tan         return;
437ed7b8fbcSLe Tan     }
438ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
439ed7b8fbcSLe Tan     vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
440ed7b8fbcSLe Tan     if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
441ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "IM filed in IECTL_REG is set, new invalidation "
442ed7b8fbcSLe Tan                     "event is not generated");
443ed7b8fbcSLe Tan         return;
444ed7b8fbcSLe Tan     } else {
445ed7b8fbcSLe Tan         /* Generate the interrupt event */
446ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
447ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
448ed7b8fbcSLe Tan     }
449ed7b8fbcSLe Tan }
450ed7b8fbcSLe Tan 
4511da12ec4SLe Tan static inline bool vtd_root_entry_present(VTDRootEntry *root)
4521da12ec4SLe Tan {
4531da12ec4SLe Tan     return root->val & VTD_ROOT_ENTRY_P;
4541da12ec4SLe Tan }
4551da12ec4SLe Tan 
4561da12ec4SLe Tan static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
4571da12ec4SLe Tan                               VTDRootEntry *re)
4581da12ec4SLe Tan {
4591da12ec4SLe Tan     dma_addr_t addr;
4601da12ec4SLe Tan 
4611da12ec4SLe Tan     addr = s->root + index * sizeof(*re);
4621da12ec4SLe Tan     if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) {
4631da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: fail to access root-entry at 0x%"PRIx64
4641da12ec4SLe Tan                     " + %"PRIu8, s->root, index);
4651da12ec4SLe Tan         re->val = 0;
4661da12ec4SLe Tan         return -VTD_FR_ROOT_TABLE_INV;
4671da12ec4SLe Tan     }
4681da12ec4SLe Tan     re->val = le64_to_cpu(re->val);
4691da12ec4SLe Tan     return 0;
4701da12ec4SLe Tan }
4711da12ec4SLe Tan 
4721da12ec4SLe Tan static inline bool vtd_context_entry_present(VTDContextEntry *context)
4731da12ec4SLe Tan {
4741da12ec4SLe Tan     return context->lo & VTD_CONTEXT_ENTRY_P;
4751da12ec4SLe Tan }
4761da12ec4SLe Tan 
4771da12ec4SLe Tan static int vtd_get_context_entry_from_root(VTDRootEntry *root, uint8_t index,
4781da12ec4SLe Tan                                            VTDContextEntry *ce)
4791da12ec4SLe Tan {
4801da12ec4SLe Tan     dma_addr_t addr;
4811da12ec4SLe Tan 
4821da12ec4SLe Tan     if (!vtd_root_entry_present(root)) {
4831da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: root-entry is not present");
4841da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_P;
4851da12ec4SLe Tan     }
4861da12ec4SLe Tan     addr = (root->val & VTD_ROOT_ENTRY_CTP) + index * sizeof(*ce);
4871da12ec4SLe Tan     if (dma_memory_read(&address_space_memory, addr, ce, sizeof(*ce))) {
4881da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: fail to access context-entry at 0x%"PRIx64
4891da12ec4SLe Tan                     " + %"PRIu8,
4901da12ec4SLe Tan                     (uint64_t)(root->val & VTD_ROOT_ENTRY_CTP), index);
4911da12ec4SLe Tan         return -VTD_FR_CONTEXT_TABLE_INV;
4921da12ec4SLe Tan     }
4931da12ec4SLe Tan     ce->lo = le64_to_cpu(ce->lo);
4941da12ec4SLe Tan     ce->hi = le64_to_cpu(ce->hi);
4951da12ec4SLe Tan     return 0;
4961da12ec4SLe Tan }
4971da12ec4SLe Tan 
4981da12ec4SLe Tan static inline dma_addr_t vtd_get_slpt_base_from_context(VTDContextEntry *ce)
4991da12ec4SLe Tan {
5001da12ec4SLe Tan     return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
5011da12ec4SLe Tan }
5021da12ec4SLe Tan 
5031da12ec4SLe Tan /* The shift of an addr for a certain level of paging structure */
5041da12ec4SLe Tan static inline uint32_t vtd_slpt_level_shift(uint32_t level)
5051da12ec4SLe Tan {
5061da12ec4SLe Tan     return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
5071da12ec4SLe Tan }
5081da12ec4SLe Tan 
5091da12ec4SLe Tan static inline uint64_t vtd_get_slpte_addr(uint64_t slpte)
5101da12ec4SLe Tan {
5111da12ec4SLe Tan     return slpte & VTD_SL_PT_BASE_ADDR_MASK;
5121da12ec4SLe Tan }
5131da12ec4SLe Tan 
5141da12ec4SLe Tan /* Whether the pte indicates the address of the page frame */
5151da12ec4SLe Tan static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
5161da12ec4SLe Tan {
5171da12ec4SLe Tan     return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
5181da12ec4SLe Tan }
5191da12ec4SLe Tan 
5201da12ec4SLe Tan /* Get the content of a spte located in @base_addr[@index] */
5211da12ec4SLe Tan static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
5221da12ec4SLe Tan {
5231da12ec4SLe Tan     uint64_t slpte;
5241da12ec4SLe Tan 
5251da12ec4SLe Tan     assert(index < VTD_SL_PT_ENTRY_NR);
5261da12ec4SLe Tan 
5271da12ec4SLe Tan     if (dma_memory_read(&address_space_memory,
5281da12ec4SLe Tan                         base_addr + index * sizeof(slpte), &slpte,
5291da12ec4SLe Tan                         sizeof(slpte))) {
5301da12ec4SLe Tan         slpte = (uint64_t)-1;
5311da12ec4SLe Tan         return slpte;
5321da12ec4SLe Tan     }
5331da12ec4SLe Tan     slpte = le64_to_cpu(slpte);
5341da12ec4SLe Tan     return slpte;
5351da12ec4SLe Tan }
5361da12ec4SLe Tan 
5371da12ec4SLe Tan /* Given a gpa and the level of paging structure, return the offset of current
5381da12ec4SLe Tan  * level.
5391da12ec4SLe Tan  */
5401da12ec4SLe Tan static inline uint32_t vtd_gpa_level_offset(uint64_t gpa, uint32_t level)
5411da12ec4SLe Tan {
5421da12ec4SLe Tan     return (gpa >> vtd_slpt_level_shift(level)) &
5431da12ec4SLe Tan             ((1ULL << VTD_SL_LEVEL_BITS) - 1);
5441da12ec4SLe Tan }
5451da12ec4SLe Tan 
5461da12ec4SLe Tan /* Check Capability Register to see if the @level of page-table is supported */
5471da12ec4SLe Tan static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
5481da12ec4SLe Tan {
5491da12ec4SLe Tan     return VTD_CAP_SAGAW_MASK & s->cap &
5501da12ec4SLe Tan            (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
5511da12ec4SLe Tan }
5521da12ec4SLe Tan 
5531da12ec4SLe Tan /* Get the page-table level that hardware should use for the second-level
5541da12ec4SLe Tan  * page-table walk from the Address Width field of context-entry.
5551da12ec4SLe Tan  */
5561da12ec4SLe Tan static inline uint32_t vtd_get_level_from_context_entry(VTDContextEntry *ce)
5571da12ec4SLe Tan {
5581da12ec4SLe Tan     return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
5591da12ec4SLe Tan }
5601da12ec4SLe Tan 
5611da12ec4SLe Tan static inline uint32_t vtd_get_agaw_from_context_entry(VTDContextEntry *ce)
5621da12ec4SLe Tan {
5631da12ec4SLe Tan     return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
5641da12ec4SLe Tan }
5651da12ec4SLe Tan 
5661da12ec4SLe Tan static const uint64_t vtd_paging_entry_rsvd_field[] = {
5671da12ec4SLe Tan     [0] = ~0ULL,
5681da12ec4SLe Tan     /* For not large page */
5691da12ec4SLe Tan     [1] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5701da12ec4SLe Tan     [2] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5711da12ec4SLe Tan     [3] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5721da12ec4SLe Tan     [4] = 0x880ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5731da12ec4SLe Tan     /* For large page */
5741da12ec4SLe Tan     [5] = 0x800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5751da12ec4SLe Tan     [6] = 0x1ff800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5761da12ec4SLe Tan     [7] = 0x3ffff800ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5771da12ec4SLe Tan     [8] = 0x880ULL | ~(VTD_HAW_MASK | VTD_SL_IGN_COM),
5781da12ec4SLe Tan };
5791da12ec4SLe Tan 
5801da12ec4SLe Tan static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
5811da12ec4SLe Tan {
5821da12ec4SLe Tan     if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) {
5831da12ec4SLe Tan         /* Maybe large page */
5841da12ec4SLe Tan         return slpte & vtd_paging_entry_rsvd_field[level + 4];
5851da12ec4SLe Tan     } else {
5861da12ec4SLe Tan         return slpte & vtd_paging_entry_rsvd_field[level];
5871da12ec4SLe Tan     }
5881da12ec4SLe Tan }
5891da12ec4SLe Tan 
5901da12ec4SLe Tan /* Given the @gpa, get relevant @slptep. @slpte_level will be the last level
5911da12ec4SLe Tan  * of the translation, can be used for deciding the size of large page.
5921da12ec4SLe Tan  */
5931da12ec4SLe Tan static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, bool is_write,
5941da12ec4SLe Tan                             uint64_t *slptep, uint32_t *slpte_level,
5951da12ec4SLe Tan                             bool *reads, bool *writes)
5961da12ec4SLe Tan {
5971da12ec4SLe Tan     dma_addr_t addr = vtd_get_slpt_base_from_context(ce);
5981da12ec4SLe Tan     uint32_t level = vtd_get_level_from_context_entry(ce);
5991da12ec4SLe Tan     uint32_t offset;
6001da12ec4SLe Tan     uint64_t slpte;
6011da12ec4SLe Tan     uint32_t ce_agaw = vtd_get_agaw_from_context_entry(ce);
6021da12ec4SLe Tan     uint64_t access_right_check;
6031da12ec4SLe Tan 
6041da12ec4SLe Tan     /* Check if @gpa is above 2^X-1, where X is the minimum of MGAW in CAP_REG
6051da12ec4SLe Tan      * and AW in context-entry.
6061da12ec4SLe Tan      */
6071da12ec4SLe Tan     if (gpa & ~((1ULL << MIN(ce_agaw, VTD_MGAW)) - 1)) {
6081da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: gpa 0x%"PRIx64 " exceeds limits", gpa);
6091da12ec4SLe Tan         return -VTD_FR_ADDR_BEYOND_MGAW;
6101da12ec4SLe Tan     }
6111da12ec4SLe Tan 
6121da12ec4SLe Tan     /* FIXME: what is the Atomics request here? */
6131da12ec4SLe Tan     access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
6141da12ec4SLe Tan 
6151da12ec4SLe Tan     while (true) {
6161da12ec4SLe Tan         offset = vtd_gpa_level_offset(gpa, level);
6171da12ec4SLe Tan         slpte = vtd_get_slpte(addr, offset);
6181da12ec4SLe Tan 
6191da12ec4SLe Tan         if (slpte == (uint64_t)-1) {
6201da12ec4SLe Tan             VTD_DPRINTF(GENERAL, "error: fail to access second-level paging "
6211da12ec4SLe Tan                         "entry at level %"PRIu32 " for gpa 0x%"PRIx64,
6221da12ec4SLe Tan                         level, gpa);
6231da12ec4SLe Tan             if (level == vtd_get_level_from_context_entry(ce)) {
6241da12ec4SLe Tan                 /* Invalid programming of context-entry */
6251da12ec4SLe Tan                 return -VTD_FR_CONTEXT_ENTRY_INV;
6261da12ec4SLe Tan             } else {
6271da12ec4SLe Tan                 return -VTD_FR_PAGING_ENTRY_INV;
6281da12ec4SLe Tan             }
6291da12ec4SLe Tan         }
6301da12ec4SLe Tan         *reads = (*reads) && (slpte & VTD_SL_R);
6311da12ec4SLe Tan         *writes = (*writes) && (slpte & VTD_SL_W);
6321da12ec4SLe Tan         if (!(slpte & access_right_check)) {
6331da12ec4SLe Tan             VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
6341da12ec4SLe Tan                         "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
6351da12ec4SLe Tan                         (is_write ? "write" : "read"), gpa, slpte);
6361da12ec4SLe Tan             return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
6371da12ec4SLe Tan         }
6381da12ec4SLe Tan         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
6391da12ec4SLe Tan             VTD_DPRINTF(GENERAL, "error: non-zero reserved field in second "
6401da12ec4SLe Tan                         "level paging entry level %"PRIu32 " slpte 0x%"PRIx64,
6411da12ec4SLe Tan                         level, slpte);
6421da12ec4SLe Tan             return -VTD_FR_PAGING_ENTRY_RSVD;
6431da12ec4SLe Tan         }
6441da12ec4SLe Tan 
6451da12ec4SLe Tan         if (vtd_is_last_slpte(slpte, level)) {
6461da12ec4SLe Tan             *slptep = slpte;
6471da12ec4SLe Tan             *slpte_level = level;
6481da12ec4SLe Tan             return 0;
6491da12ec4SLe Tan         }
6501da12ec4SLe Tan         addr = vtd_get_slpte_addr(slpte);
6511da12ec4SLe Tan         level--;
6521da12ec4SLe Tan     }
6531da12ec4SLe Tan }
6541da12ec4SLe Tan 
6551da12ec4SLe Tan /* Map a device to its corresponding domain (context-entry) */
6561da12ec4SLe Tan static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
6571da12ec4SLe Tan                                     uint8_t devfn, VTDContextEntry *ce)
6581da12ec4SLe Tan {
6591da12ec4SLe Tan     VTDRootEntry re;
6601da12ec4SLe Tan     int ret_fr;
6611da12ec4SLe Tan 
6621da12ec4SLe Tan     ret_fr = vtd_get_root_entry(s, bus_num, &re);
6631da12ec4SLe Tan     if (ret_fr) {
6641da12ec4SLe Tan         return ret_fr;
6651da12ec4SLe Tan     }
6661da12ec4SLe Tan 
6671da12ec4SLe Tan     if (!vtd_root_entry_present(&re)) {
6681da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: root-entry #%"PRIu8 " is not present",
6691da12ec4SLe Tan                     bus_num);
6701da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_P;
6711da12ec4SLe Tan     } else if (re.rsvd || (re.val & VTD_ROOT_ENTRY_RSVD)) {
6721da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in root-entry "
6731da12ec4SLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64, re.rsvd, re.val);
6741da12ec4SLe Tan         return -VTD_FR_ROOT_ENTRY_RSVD;
6751da12ec4SLe Tan     }
6761da12ec4SLe Tan 
6771da12ec4SLe Tan     ret_fr = vtd_get_context_entry_from_root(&re, devfn, ce);
6781da12ec4SLe Tan     if (ret_fr) {
6791da12ec4SLe Tan         return ret_fr;
6801da12ec4SLe Tan     }
6811da12ec4SLe Tan 
6821da12ec4SLe Tan     if (!vtd_context_entry_present(ce)) {
6831da12ec4SLe Tan         VTD_DPRINTF(GENERAL,
6841da12ec4SLe Tan                     "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
6851da12ec4SLe Tan                     "is not present", devfn, bus_num);
6861da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_P;
6871da12ec4SLe Tan     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
6881da12ec4SLe Tan                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
6891da12ec4SLe Tan         VTD_DPRINTF(GENERAL,
6901da12ec4SLe Tan                     "error: non-zero reserved field in context-entry "
6911da12ec4SLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64, ce->hi, ce->lo);
6921da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_RSVD;
6931da12ec4SLe Tan     }
6941da12ec4SLe Tan     /* Check if the programming of context-entry is valid */
6951da12ec4SLe Tan     if (!vtd_is_level_supported(s, vtd_get_level_from_context_entry(ce))) {
6961da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: unsupported Address Width value in "
6971da12ec4SLe Tan                     "context-entry hi 0x%"PRIx64 " lo 0x%"PRIx64,
6981da12ec4SLe Tan                     ce->hi, ce->lo);
6991da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
7001da12ec4SLe Tan     } else if (ce->lo & VTD_CONTEXT_ENTRY_TT) {
7011da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: unsupported Translation Type in "
7021da12ec4SLe Tan                     "context-entry hi 0x%"PRIx64 " lo 0x%"PRIx64,
7031da12ec4SLe Tan                     ce->hi, ce->lo);
7041da12ec4SLe Tan         return -VTD_FR_CONTEXT_ENTRY_INV;
7051da12ec4SLe Tan     }
7061da12ec4SLe Tan     return 0;
7071da12ec4SLe Tan }
7081da12ec4SLe Tan 
7091da12ec4SLe Tan static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
7101da12ec4SLe Tan {
7111da12ec4SLe Tan     return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
7121da12ec4SLe Tan }
7131da12ec4SLe Tan 
7141da12ec4SLe Tan static const bool vtd_qualified_faults[] = {
7151da12ec4SLe Tan     [VTD_FR_RESERVED] = false,
7161da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_P] = false,
7171da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_P] = true,
7181da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_INV] = true,
7191da12ec4SLe Tan     [VTD_FR_ADDR_BEYOND_MGAW] = true,
7201da12ec4SLe Tan     [VTD_FR_WRITE] = true,
7211da12ec4SLe Tan     [VTD_FR_READ] = true,
7221da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_INV] = true,
7231da12ec4SLe Tan     [VTD_FR_ROOT_TABLE_INV] = false,
7241da12ec4SLe Tan     [VTD_FR_CONTEXT_TABLE_INV] = false,
7251da12ec4SLe Tan     [VTD_FR_ROOT_ENTRY_RSVD] = false,
7261da12ec4SLe Tan     [VTD_FR_PAGING_ENTRY_RSVD] = true,
7271da12ec4SLe Tan     [VTD_FR_CONTEXT_ENTRY_TT] = true,
7281da12ec4SLe Tan     [VTD_FR_RESERVED_ERR] = false,
7291da12ec4SLe Tan     [VTD_FR_MAX] = false,
7301da12ec4SLe Tan };
7311da12ec4SLe Tan 
7321da12ec4SLe Tan /* To see if a fault condition is "qualified", which is reported to software
7331da12ec4SLe Tan  * only if the FPD field in the context-entry used to process the faulting
7341da12ec4SLe Tan  * request is 0.
7351da12ec4SLe Tan  */
7361da12ec4SLe Tan static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
7371da12ec4SLe Tan {
7381da12ec4SLe Tan     return vtd_qualified_faults[fault];
7391da12ec4SLe Tan }
7401da12ec4SLe Tan 
7411da12ec4SLe Tan static inline bool vtd_is_interrupt_addr(hwaddr addr)
7421da12ec4SLe Tan {
7431da12ec4SLe Tan     return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
7441da12ec4SLe Tan }
7451da12ec4SLe Tan 
7461da12ec4SLe Tan /* Map dev to context-entry then do a paging-structures walk to do a iommu
7471da12ec4SLe Tan  * translation.
74879e2b9aeSPaolo Bonzini  *
74979e2b9aeSPaolo Bonzini  * Called from RCU critical section.
75079e2b9aeSPaolo Bonzini  *
7511da12ec4SLe Tan  * @bus_num: The bus number
7521da12ec4SLe Tan  * @devfn: The devfn, which is the  combined of device and function number
7531da12ec4SLe Tan  * @is_write: The access is a write operation
7541da12ec4SLe Tan  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
7551da12ec4SLe Tan  */
756*7df953bdSKnut Omang static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
7571da12ec4SLe Tan                                    uint8_t devfn, hwaddr addr, bool is_write,
7581da12ec4SLe Tan                                    IOMMUTLBEntry *entry)
7591da12ec4SLe Tan {
760d92fa2dcSLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
7611da12ec4SLe Tan     VTDContextEntry ce;
762*7df953bdSKnut Omang     uint8_t bus_num = pci_bus_num(bus);
763d92fa2dcSLe Tan     VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
7641da12ec4SLe Tan     uint64_t slpte;
7651da12ec4SLe Tan     uint32_t level;
7661da12ec4SLe Tan     uint16_t source_id = vtd_make_source_id(bus_num, devfn);
7671da12ec4SLe Tan     int ret_fr;
7681da12ec4SLe Tan     bool is_fpd_set = false;
7691da12ec4SLe Tan     bool reads = true;
7701da12ec4SLe Tan     bool writes = true;
771b5a280c0SLe Tan     VTDIOTLBEntry *iotlb_entry;
7721da12ec4SLe Tan 
7731da12ec4SLe Tan     /* Check if the request is in interrupt address range */
7741da12ec4SLe Tan     if (vtd_is_interrupt_addr(addr)) {
7751da12ec4SLe Tan         if (is_write) {
7761da12ec4SLe Tan             /* FIXME: since we don't know the length of the access here, we
7771da12ec4SLe Tan              * treat Non-DWORD length write requests without PASID as
7781da12ec4SLe Tan              * interrupt requests, too. Withoud interrupt remapping support,
7791da12ec4SLe Tan              * we just use 1:1 mapping.
7801da12ec4SLe Tan              */
7811da12ec4SLe Tan             VTD_DPRINTF(MMU, "write request to interrupt address "
7821da12ec4SLe Tan                         "gpa 0x%"PRIx64, addr);
7831da12ec4SLe Tan             entry->iova = addr & VTD_PAGE_MASK_4K;
7841da12ec4SLe Tan             entry->translated_addr = addr & VTD_PAGE_MASK_4K;
7851da12ec4SLe Tan             entry->addr_mask = ~VTD_PAGE_MASK_4K;
7861da12ec4SLe Tan             entry->perm = IOMMU_WO;
7871da12ec4SLe Tan             return;
7881da12ec4SLe Tan         } else {
7891da12ec4SLe Tan             VTD_DPRINTF(GENERAL, "error: read request from interrupt address "
7901da12ec4SLe Tan                         "gpa 0x%"PRIx64, addr);
7911da12ec4SLe Tan             vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, is_write);
7921da12ec4SLe Tan             return;
7931da12ec4SLe Tan         }
7941da12ec4SLe Tan     }
795b5a280c0SLe Tan     /* Try to fetch slpte form IOTLB */
796b5a280c0SLe Tan     iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
797b5a280c0SLe Tan     if (iotlb_entry) {
798b5a280c0SLe Tan         VTD_DPRINTF(CACHE, "hit iotlb sid 0x%"PRIx16 " gpa 0x%"PRIx64
799b5a280c0SLe Tan                     " slpte 0x%"PRIx64 " did 0x%"PRIx16, source_id, addr,
800b5a280c0SLe Tan                     iotlb_entry->slpte, iotlb_entry->domain_id);
801b5a280c0SLe Tan         slpte = iotlb_entry->slpte;
802b5a280c0SLe Tan         reads = iotlb_entry->read_flags;
803b5a280c0SLe Tan         writes = iotlb_entry->write_flags;
804b5a280c0SLe Tan         goto out;
805b5a280c0SLe Tan     }
806d92fa2dcSLe Tan     /* Try to fetch context-entry from cache first */
807d92fa2dcSLe Tan     if (cc_entry->context_cache_gen == s->context_cache_gen) {
808d92fa2dcSLe Tan         VTD_DPRINTF(CACHE, "hit context-cache bus %d devfn %d "
809d92fa2dcSLe Tan                     "(hi %"PRIx64 " lo %"PRIx64 " gen %"PRIu32 ")",
810d92fa2dcSLe Tan                     bus_num, devfn, cc_entry->context_entry.hi,
811d92fa2dcSLe Tan                     cc_entry->context_entry.lo, cc_entry->context_cache_gen);
812d92fa2dcSLe Tan         ce = cc_entry->context_entry;
813d92fa2dcSLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
814d92fa2dcSLe Tan     } else {
8151da12ec4SLe Tan         ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
8161da12ec4SLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
8171da12ec4SLe Tan         if (ret_fr) {
8181da12ec4SLe Tan             ret_fr = -ret_fr;
8191da12ec4SLe Tan             if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
820d92fa2dcSLe Tan                 VTD_DPRINTF(FLOG, "fault processing is disabled for DMA "
821d92fa2dcSLe Tan                             "requests through this context-entry "
822d92fa2dcSLe Tan                             "(with FPD Set)");
8231da12ec4SLe Tan             } else {
8241da12ec4SLe Tan                 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
8251da12ec4SLe Tan             }
8261da12ec4SLe Tan             return;
8271da12ec4SLe Tan         }
828d92fa2dcSLe Tan         /* Update context-cache */
829d92fa2dcSLe Tan         VTD_DPRINTF(CACHE, "update context-cache bus %d devfn %d "
830d92fa2dcSLe Tan                     "(hi %"PRIx64 " lo %"PRIx64 " gen %"PRIu32 "->%"PRIu32 ")",
831d92fa2dcSLe Tan                     bus_num, devfn, ce.hi, ce.lo,
832d92fa2dcSLe Tan                     cc_entry->context_cache_gen, s->context_cache_gen);
833d92fa2dcSLe Tan         cc_entry->context_entry = ce;
834d92fa2dcSLe Tan         cc_entry->context_cache_gen = s->context_cache_gen;
835d92fa2dcSLe Tan     }
8361da12ec4SLe Tan 
8371da12ec4SLe Tan     ret_fr = vtd_gpa_to_slpte(&ce, addr, is_write, &slpte, &level,
8381da12ec4SLe Tan                               &reads, &writes);
8391da12ec4SLe Tan     if (ret_fr) {
8401da12ec4SLe Tan         ret_fr = -ret_fr;
8411da12ec4SLe Tan         if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
8421da12ec4SLe Tan             VTD_DPRINTF(FLOG, "fault processing is disabled for DMA requests "
8431da12ec4SLe Tan                         "through this context-entry (with FPD Set)");
8441da12ec4SLe Tan         } else {
8451da12ec4SLe Tan             vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
8461da12ec4SLe Tan         }
8471da12ec4SLe Tan         return;
8481da12ec4SLe Tan     }
8491da12ec4SLe Tan 
850b5a280c0SLe Tan     vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
851b5a280c0SLe Tan                      reads, writes);
852b5a280c0SLe Tan out:
8531da12ec4SLe Tan     entry->iova = addr & VTD_PAGE_MASK_4K;
8541da12ec4SLe Tan     entry->translated_addr = vtd_get_slpte_addr(slpte) & VTD_PAGE_MASK_4K;
8551da12ec4SLe Tan     entry->addr_mask = ~VTD_PAGE_MASK_4K;
8561da12ec4SLe Tan     entry->perm = (writes ? 2 : 0) + (reads ? 1 : 0);
8571da12ec4SLe Tan }
8581da12ec4SLe Tan 
8591da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s)
8601da12ec4SLe Tan {
8611da12ec4SLe Tan     s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
8621da12ec4SLe Tan     s->root_extended = s->root & VTD_RTADDR_RTT;
8631da12ec4SLe Tan     s->root &= VTD_RTADDR_ADDR_MASK;
8641da12ec4SLe Tan 
8651da12ec4SLe Tan     VTD_DPRINTF(CSR, "root_table addr 0x%"PRIx64 " %s", s->root,
8661da12ec4SLe Tan                 (s->root_extended ? "(extended)" : ""));
8671da12ec4SLe Tan }
8681da12ec4SLe Tan 
869d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s)
870d92fa2dcSLe Tan {
871d92fa2dcSLe Tan     s->context_cache_gen++;
872d92fa2dcSLe Tan     if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
873d92fa2dcSLe Tan         vtd_reset_context_cache(s);
874d92fa2dcSLe Tan     }
875d92fa2dcSLe Tan }
876d92fa2dcSLe Tan 
877*7df953bdSKnut Omang 
878*7df953bdSKnut Omang /* Find the VTD address space currently associated with a given bus number,
879*7df953bdSKnut Omang  */
880*7df953bdSKnut Omang static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
881*7df953bdSKnut Omang {
882*7df953bdSKnut Omang     VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];
883*7df953bdSKnut Omang     if (!vtd_bus) {
884*7df953bdSKnut Omang         /* Iterate over the registered buses to find the one
885*7df953bdSKnut Omang          * which currently hold this bus number, and update the bus_num lookup table:
886*7df953bdSKnut Omang          */
887*7df953bdSKnut Omang         GHashTableIter iter;
888*7df953bdSKnut Omang 
889*7df953bdSKnut Omang         g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
890*7df953bdSKnut Omang         while (g_hash_table_iter_next (&iter, NULL, (void**)&vtd_bus)) {
891*7df953bdSKnut Omang             if (pci_bus_num(vtd_bus->bus) == bus_num) {
892*7df953bdSKnut Omang                 s->vtd_as_by_bus_num[bus_num] = vtd_bus;
893*7df953bdSKnut Omang                 return vtd_bus;
894*7df953bdSKnut Omang             }
895*7df953bdSKnut Omang         }
896*7df953bdSKnut Omang     }
897*7df953bdSKnut Omang     return vtd_bus;
898*7df953bdSKnut Omang }
899*7df953bdSKnut Omang 
900d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation.
901d92fa2dcSLe Tan  * @func_mask: FM field after shifting
902d92fa2dcSLe Tan  */
903d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s,
904d92fa2dcSLe Tan                                           uint16_t source_id,
905d92fa2dcSLe Tan                                           uint16_t func_mask)
906d92fa2dcSLe Tan {
907d92fa2dcSLe Tan     uint16_t mask;
908*7df953bdSKnut Omang     VTDBus *vtd_bus;
909d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
910d92fa2dcSLe Tan     uint16_t devfn;
911d92fa2dcSLe Tan     uint16_t devfn_it;
912d92fa2dcSLe Tan 
913d92fa2dcSLe Tan     switch (func_mask & 3) {
914d92fa2dcSLe Tan     case 0:
915d92fa2dcSLe Tan         mask = 0;   /* No bits in the SID field masked */
916d92fa2dcSLe Tan         break;
917d92fa2dcSLe Tan     case 1:
918d92fa2dcSLe Tan         mask = 4;   /* Mask bit 2 in the SID field */
919d92fa2dcSLe Tan         break;
920d92fa2dcSLe Tan     case 2:
921d92fa2dcSLe Tan         mask = 6;   /* Mask bit 2:1 in the SID field */
922d92fa2dcSLe Tan         break;
923d92fa2dcSLe Tan     case 3:
924d92fa2dcSLe Tan         mask = 7;   /* Mask bit 2:0 in the SID field */
925d92fa2dcSLe Tan         break;
926d92fa2dcSLe Tan     }
927d92fa2dcSLe Tan     VTD_DPRINTF(INV, "device-selective invalidation source 0x%"PRIx16
928d92fa2dcSLe Tan                     " mask %"PRIu16, source_id, mask);
929*7df953bdSKnut Omang     vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id));
930*7df953bdSKnut Omang     if (vtd_bus) {
931d92fa2dcSLe Tan         devfn = VTD_SID_TO_DEVFN(source_id);
932d92fa2dcSLe Tan         for (devfn_it = 0; devfn_it < VTD_PCI_DEVFN_MAX; ++devfn_it) {
933*7df953bdSKnut Omang             vtd_as = vtd_bus->dev_as[devfn_it];
934d92fa2dcSLe Tan             if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
935d92fa2dcSLe Tan                 VTD_DPRINTF(INV, "invalidate context-cahce of devfn 0x%"PRIx16,
936d92fa2dcSLe Tan                             devfn_it);
937d92fa2dcSLe Tan                 vtd_as->context_cache_entry.context_cache_gen = 0;
938d92fa2dcSLe Tan             }
939d92fa2dcSLe Tan         }
940d92fa2dcSLe Tan     }
941d92fa2dcSLe Tan }
942d92fa2dcSLe Tan 
9431da12ec4SLe Tan /* Context-cache invalidation
9441da12ec4SLe Tan  * Returns the Context Actual Invalidation Granularity.
9451da12ec4SLe Tan  * @val: the content of the CCMD_REG
9461da12ec4SLe Tan  */
9471da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
9481da12ec4SLe Tan {
9491da12ec4SLe Tan     uint64_t caig;
9501da12ec4SLe Tan     uint64_t type = val & VTD_CCMD_CIRG_MASK;
9511da12ec4SLe Tan 
9521da12ec4SLe Tan     switch (type) {
9531da12ec4SLe Tan     case VTD_CCMD_DOMAIN_INVL:
954d92fa2dcSLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
955d92fa2dcSLe Tan                     (uint16_t)VTD_CCMD_DID(val));
956d92fa2dcSLe Tan         /* Fall through */
957d92fa2dcSLe Tan     case VTD_CCMD_GLOBAL_INVL:
958d92fa2dcSLe Tan         VTD_DPRINTF(INV, "global invalidation");
959d92fa2dcSLe Tan         caig = VTD_CCMD_GLOBAL_INVL_A;
960d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
9611da12ec4SLe Tan         break;
9621da12ec4SLe Tan 
9631da12ec4SLe Tan     case VTD_CCMD_DEVICE_INVL:
9641da12ec4SLe Tan         caig = VTD_CCMD_DEVICE_INVL_A;
965d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
9661da12ec4SLe Tan         break;
9671da12ec4SLe Tan 
9681da12ec4SLe Tan     default:
969d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity");
9701da12ec4SLe Tan         caig = 0;
9711da12ec4SLe Tan     }
9721da12ec4SLe Tan     return caig;
9731da12ec4SLe Tan }
9741da12ec4SLe Tan 
975b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
976b5a280c0SLe Tan {
977b5a280c0SLe Tan     vtd_reset_iotlb(s);
978b5a280c0SLe Tan }
979b5a280c0SLe Tan 
980b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
981b5a280c0SLe Tan {
982b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
983b5a280c0SLe Tan                                 &domain_id);
984b5a280c0SLe Tan }
985b5a280c0SLe Tan 
986b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
987b5a280c0SLe Tan                                       hwaddr addr, uint8_t am)
988b5a280c0SLe Tan {
989b5a280c0SLe Tan     VTDIOTLBPageInvInfo info;
990b5a280c0SLe Tan 
991b5a280c0SLe Tan     assert(am <= VTD_MAMV);
992b5a280c0SLe Tan     info.domain_id = domain_id;
993b5a280c0SLe Tan     info.gfn = addr >> VTD_PAGE_SHIFT_4K;
994b5a280c0SLe Tan     info.mask = ~((1 << am) - 1);
995b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
996b5a280c0SLe Tan }
997b5a280c0SLe Tan 
9981da12ec4SLe Tan /* Flush IOTLB
9991da12ec4SLe Tan  * Returns the IOTLB Actual Invalidation Granularity.
10001da12ec4SLe Tan  * @val: the content of the IOTLB_REG
10011da12ec4SLe Tan  */
10021da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
10031da12ec4SLe Tan {
10041da12ec4SLe Tan     uint64_t iaig;
10051da12ec4SLe Tan     uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
1006b5a280c0SLe Tan     uint16_t domain_id;
1007b5a280c0SLe Tan     hwaddr addr;
1008b5a280c0SLe Tan     uint8_t am;
10091da12ec4SLe Tan 
10101da12ec4SLe Tan     switch (type) {
10111da12ec4SLe Tan     case VTD_TLB_GLOBAL_FLUSH:
1012b5a280c0SLe Tan         VTD_DPRINTF(INV, "global invalidation");
10131da12ec4SLe Tan         iaig = VTD_TLB_GLOBAL_FLUSH_A;
1014b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
10151da12ec4SLe Tan         break;
10161da12ec4SLe Tan 
10171da12ec4SLe Tan     case VTD_TLB_DSI_FLUSH:
1018b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
1019b5a280c0SLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
1020b5a280c0SLe Tan                     domain_id);
10211da12ec4SLe Tan         iaig = VTD_TLB_DSI_FLUSH_A;
1022b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
10231da12ec4SLe Tan         break;
10241da12ec4SLe Tan 
10251da12ec4SLe Tan     case VTD_TLB_PSI_FLUSH:
1026b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
1027b5a280c0SLe Tan         addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1028b5a280c0SLe Tan         am = VTD_IVA_AM(addr);
1029b5a280c0SLe Tan         addr = VTD_IVA_ADDR(addr);
1030b5a280c0SLe Tan         VTD_DPRINTF(INV, "page-selective invalidation domain 0x%"PRIx16
1031b5a280c0SLe Tan                     " addr 0x%"PRIx64 " mask %"PRIu8, domain_id, addr, am);
1032b5a280c0SLe Tan         if (am > VTD_MAMV) {
1033b5a280c0SLe Tan             VTD_DPRINTF(GENERAL, "error: supported max address mask value is "
1034b5a280c0SLe Tan                         "%"PRIu8, (uint8_t)VTD_MAMV);
1035b5a280c0SLe Tan             iaig = 0;
1036b5a280c0SLe Tan             break;
1037b5a280c0SLe Tan         }
10381da12ec4SLe Tan         iaig = VTD_TLB_PSI_FLUSH_A;
1039b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
10401da12ec4SLe Tan         break;
10411da12ec4SLe Tan 
10421da12ec4SLe Tan     default:
1043b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity");
10441da12ec4SLe Tan         iaig = 0;
10451da12ec4SLe Tan     }
10461da12ec4SLe Tan     return iaig;
10471da12ec4SLe Tan }
10481da12ec4SLe Tan 
1049ed7b8fbcSLe Tan static inline bool vtd_queued_inv_enable_check(IntelIOMMUState *s)
1050ed7b8fbcSLe Tan {
1051ed7b8fbcSLe Tan     return s->iq_tail == 0;
1052ed7b8fbcSLe Tan }
1053ed7b8fbcSLe Tan 
1054ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1055ed7b8fbcSLe Tan {
1056ed7b8fbcSLe Tan     return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1057ed7b8fbcSLe Tan            (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1058ed7b8fbcSLe Tan }
1059ed7b8fbcSLe Tan 
1060ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
1061ed7b8fbcSLe Tan {
1062ed7b8fbcSLe Tan     uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
1063ed7b8fbcSLe Tan 
1064ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "Queued Invalidation Enable %s", (en ? "on" : "off"));
1065ed7b8fbcSLe Tan     if (en) {
1066ed7b8fbcSLe Tan         if (vtd_queued_inv_enable_check(s)) {
1067ed7b8fbcSLe Tan             s->iq = iqa_val & VTD_IQA_IQA_MASK;
1068ed7b8fbcSLe Tan             /* 2^(x+8) entries */
1069ed7b8fbcSLe Tan             s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
1070ed7b8fbcSLe Tan             s->qi_enabled = true;
1071ed7b8fbcSLe Tan             VTD_DPRINTF(INV, "DMAR_IQA_REG 0x%"PRIx64, iqa_val);
1072ed7b8fbcSLe Tan             VTD_DPRINTF(INV, "Invalidation Queue addr 0x%"PRIx64 " size %d",
1073ed7b8fbcSLe Tan                         s->iq, s->iq_size);
1074ed7b8fbcSLe Tan             /* Ok - report back to driver */
1075ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
1076ed7b8fbcSLe Tan         } else {
1077ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: can't enable Queued Invalidation: "
1078ed7b8fbcSLe Tan                         "tail %"PRIu16, s->iq_tail);
1079ed7b8fbcSLe Tan         }
1080ed7b8fbcSLe Tan     } else {
1081ed7b8fbcSLe Tan         if (vtd_queued_inv_disable_check(s)) {
1082ed7b8fbcSLe Tan             /* disable Queued Invalidation */
1083ed7b8fbcSLe Tan             vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
1084ed7b8fbcSLe Tan             s->iq_head = 0;
1085ed7b8fbcSLe Tan             s->qi_enabled = false;
1086ed7b8fbcSLe Tan             /* Ok - report back to driver */
1087ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
1088ed7b8fbcSLe Tan         } else {
1089ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: can't disable Queued Invalidation: "
1090ed7b8fbcSLe Tan                         "head %"PRIu16 ", tail %"PRIu16
1091ed7b8fbcSLe Tan                         ", last_descriptor %"PRIu8,
1092ed7b8fbcSLe Tan                         s->iq_head, s->iq_tail, s->iq_last_desc_type);
1093ed7b8fbcSLe Tan         }
1094ed7b8fbcSLe Tan     }
1095ed7b8fbcSLe Tan }
1096ed7b8fbcSLe Tan 
10971da12ec4SLe Tan /* Set Root Table Pointer */
10981da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
10991da12ec4SLe Tan {
11001da12ec4SLe Tan     VTD_DPRINTF(CSR, "set Root Table Pointer");
11011da12ec4SLe Tan 
11021da12ec4SLe Tan     vtd_root_table_setup(s);
11031da12ec4SLe Tan     /* Ok - report back to driver */
11041da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
11051da12ec4SLe Tan }
11061da12ec4SLe Tan 
11071da12ec4SLe Tan /* Handle Translation Enable/Disable */
11081da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
11091da12ec4SLe Tan {
11101da12ec4SLe Tan     VTD_DPRINTF(CSR, "Translation Enable %s", (en ? "on" : "off"));
11111da12ec4SLe Tan 
11121da12ec4SLe Tan     if (en) {
11131da12ec4SLe Tan         s->dmar_enabled = true;
11141da12ec4SLe Tan         /* Ok - report back to driver */
11151da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
11161da12ec4SLe Tan     } else {
11171da12ec4SLe Tan         s->dmar_enabled = false;
11181da12ec4SLe Tan 
11191da12ec4SLe Tan         /* Clear the index of Fault Recording Register */
11201da12ec4SLe Tan         s->next_frcd_reg = 0;
11211da12ec4SLe Tan         /* Ok - report back to driver */
11221da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
11231da12ec4SLe Tan     }
11241da12ec4SLe Tan }
11251da12ec4SLe Tan 
11261da12ec4SLe Tan /* Handle write to Global Command Register */
11271da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s)
11281da12ec4SLe Tan {
11291da12ec4SLe Tan     uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
11301da12ec4SLe Tan     uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
11311da12ec4SLe Tan     uint32_t changed = status ^ val;
11321da12ec4SLe Tan 
11331da12ec4SLe Tan     VTD_DPRINTF(CSR, "value 0x%"PRIx32 " status 0x%"PRIx32, val, status);
11341da12ec4SLe Tan     if (changed & VTD_GCMD_TE) {
11351da12ec4SLe Tan         /* Translation enable/disable */
11361da12ec4SLe Tan         vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
11371da12ec4SLe Tan     }
11381da12ec4SLe Tan     if (val & VTD_GCMD_SRTP) {
11391da12ec4SLe Tan         /* Set/update the root-table pointer */
11401da12ec4SLe Tan         vtd_handle_gcmd_srtp(s);
11411da12ec4SLe Tan     }
1142ed7b8fbcSLe Tan     if (changed & VTD_GCMD_QIE) {
1143ed7b8fbcSLe Tan         /* Queued Invalidation Enable */
1144ed7b8fbcSLe Tan         vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
1145ed7b8fbcSLe Tan     }
11461da12ec4SLe Tan }
11471da12ec4SLe Tan 
11481da12ec4SLe Tan /* Handle write to Context Command Register */
11491da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s)
11501da12ec4SLe Tan {
11511da12ec4SLe Tan     uint64_t ret;
11521da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
11531da12ec4SLe Tan 
11541da12ec4SLe Tan     /* Context-cache invalidation request */
11551da12ec4SLe Tan     if (val & VTD_CCMD_ICC) {
1156ed7b8fbcSLe Tan         if (s->qi_enabled) {
1157ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: Queued Invalidation enabled, "
1158ed7b8fbcSLe Tan                         "should not use register-based invalidation");
1159ed7b8fbcSLe Tan             return;
1160ed7b8fbcSLe Tan         }
11611da12ec4SLe Tan         ret = vtd_context_cache_invalidate(s, val);
11621da12ec4SLe Tan         /* Invalidation completed. Change something to show */
11631da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
11641da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
11651da12ec4SLe Tan                                       ret);
11661da12ec4SLe Tan         VTD_DPRINTF(INV, "CCMD_REG write-back val: 0x%"PRIx64, ret);
11671da12ec4SLe Tan     }
11681da12ec4SLe Tan }
11691da12ec4SLe Tan 
11701da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */
11711da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s)
11721da12ec4SLe Tan {
11731da12ec4SLe Tan     uint64_t ret;
11741da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
11751da12ec4SLe Tan 
11761da12ec4SLe Tan     /* IOTLB invalidation request */
11771da12ec4SLe Tan     if (val & VTD_TLB_IVT) {
1178ed7b8fbcSLe Tan         if (s->qi_enabled) {
1179ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: Queued Invalidation enabled, "
1180ed7b8fbcSLe Tan                         "should not use register-based invalidation");
1181ed7b8fbcSLe Tan             return;
1182ed7b8fbcSLe Tan         }
11831da12ec4SLe Tan         ret = vtd_iotlb_flush(s, val);
11841da12ec4SLe Tan         /* Invalidation completed. Change something to show */
11851da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
11861da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
11871da12ec4SLe Tan                                       VTD_TLB_FLUSH_GRANU_MASK_A, ret);
11881da12ec4SLe Tan         VTD_DPRINTF(INV, "IOTLB_REG write-back val: 0x%"PRIx64, ret);
11891da12ec4SLe Tan     }
11901da12ec4SLe Tan }
11911da12ec4SLe Tan 
1192ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */
1193ed7b8fbcSLe Tan static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
1194ed7b8fbcSLe Tan                              VTDInvDesc *inv_desc)
1195ed7b8fbcSLe Tan {
1196ed7b8fbcSLe Tan     dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
1197ed7b8fbcSLe Tan     if (dma_memory_read(&address_space_memory, addr, inv_desc,
1198ed7b8fbcSLe Tan         sizeof(*inv_desc))) {
1199ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: fail to fetch Invalidation Descriptor "
1200ed7b8fbcSLe Tan                     "base_addr 0x%"PRIx64 " offset %"PRIu32, base_addr, offset);
1201ed7b8fbcSLe Tan         inv_desc->lo = 0;
1202ed7b8fbcSLe Tan         inv_desc->hi = 0;
1203ed7b8fbcSLe Tan 
1204ed7b8fbcSLe Tan         return false;
1205ed7b8fbcSLe Tan     }
1206ed7b8fbcSLe Tan     inv_desc->lo = le64_to_cpu(inv_desc->lo);
1207ed7b8fbcSLe Tan     inv_desc->hi = le64_to_cpu(inv_desc->hi);
1208ed7b8fbcSLe Tan     return true;
1209ed7b8fbcSLe Tan }
1210ed7b8fbcSLe Tan 
1211ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1212ed7b8fbcSLe Tan {
1213ed7b8fbcSLe Tan     if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
1214ed7b8fbcSLe Tan         (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
1215ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in Invalidation "
1216ed7b8fbcSLe Tan                     "Wait Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1217ed7b8fbcSLe Tan                     inv_desc->hi, inv_desc->lo);
1218ed7b8fbcSLe Tan         return false;
1219ed7b8fbcSLe Tan     }
1220ed7b8fbcSLe Tan     if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
1221ed7b8fbcSLe Tan         /* Status Write */
1222ed7b8fbcSLe Tan         uint32_t status_data = (uint32_t)(inv_desc->lo >>
1223ed7b8fbcSLe Tan                                VTD_INV_DESC_WAIT_DATA_SHIFT);
1224ed7b8fbcSLe Tan 
1225ed7b8fbcSLe Tan         assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
1226ed7b8fbcSLe Tan 
1227ed7b8fbcSLe Tan         /* FIXME: need to be masked with HAW? */
1228ed7b8fbcSLe Tan         dma_addr_t status_addr = inv_desc->hi;
1229ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "status data 0x%x, status addr 0x%"PRIx64,
1230ed7b8fbcSLe Tan                     status_data, status_addr);
1231ed7b8fbcSLe Tan         status_data = cpu_to_le32(status_data);
1232ed7b8fbcSLe Tan         if (dma_memory_write(&address_space_memory, status_addr, &status_data,
1233ed7b8fbcSLe Tan                              sizeof(status_data))) {
1234ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: fail to perform a coherent write");
1235ed7b8fbcSLe Tan             return false;
1236ed7b8fbcSLe Tan         }
1237ed7b8fbcSLe Tan     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
1238ed7b8fbcSLe Tan         /* Interrupt flag */
1239ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Invalidation Wait Descriptor interrupt completion");
1240ed7b8fbcSLe Tan         vtd_generate_completion_event(s);
1241ed7b8fbcSLe Tan     } else {
1242ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid Invalidation Wait Descriptor: "
1243ed7b8fbcSLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64, inv_desc->hi, inv_desc->lo);
1244ed7b8fbcSLe Tan         return false;
1245ed7b8fbcSLe Tan     }
1246ed7b8fbcSLe Tan     return true;
1247ed7b8fbcSLe Tan }
1248ed7b8fbcSLe Tan 
1249d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
1250d92fa2dcSLe Tan                                            VTDInvDesc *inv_desc)
1251d92fa2dcSLe Tan {
1252d92fa2dcSLe Tan     if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
1253d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in Context-cache "
1254d92fa2dcSLe Tan                     "Invalidate Descriptor");
1255d92fa2dcSLe Tan         return false;
1256d92fa2dcSLe Tan     }
1257d92fa2dcSLe Tan     switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
1258d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DOMAIN:
1259d92fa2dcSLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
1260d92fa2dcSLe Tan                     (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
1261d92fa2dcSLe Tan         /* Fall through */
1262d92fa2dcSLe Tan     case VTD_INV_DESC_CC_GLOBAL:
1263d92fa2dcSLe Tan         VTD_DPRINTF(INV, "global invalidation");
1264d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
1265d92fa2dcSLe Tan         break;
1266d92fa2dcSLe Tan 
1267d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DEVICE:
1268d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_INV_DESC_CC_SID(inv_desc->lo),
1269d92fa2dcSLe Tan                                       VTD_INV_DESC_CC_FM(inv_desc->lo));
1270d92fa2dcSLe Tan         break;
1271d92fa2dcSLe Tan 
1272d92fa2dcSLe Tan     default:
1273d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity in Context-cache "
1274d92fa2dcSLe Tan                     "Invalidate Descriptor hi 0x%"PRIx64  " lo 0x%"PRIx64,
1275d92fa2dcSLe Tan                     inv_desc->hi, inv_desc->lo);
1276d92fa2dcSLe Tan         return false;
1277d92fa2dcSLe Tan     }
1278d92fa2dcSLe Tan     return true;
1279d92fa2dcSLe Tan }
1280d92fa2dcSLe Tan 
1281b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1282b5a280c0SLe Tan {
1283b5a280c0SLe Tan     uint16_t domain_id;
1284b5a280c0SLe Tan     uint8_t am;
1285b5a280c0SLe Tan     hwaddr addr;
1286b5a280c0SLe Tan 
1287b5a280c0SLe Tan     if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
1288b5a280c0SLe Tan         (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
1289b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in IOTLB "
1290b5a280c0SLe Tan                     "Invalidate Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1291b5a280c0SLe Tan                     inv_desc->hi, inv_desc->lo);
1292b5a280c0SLe Tan         return false;
1293b5a280c0SLe Tan     }
1294b5a280c0SLe Tan 
1295b5a280c0SLe Tan     switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
1296b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_GLOBAL:
1297b5a280c0SLe Tan         VTD_DPRINTF(INV, "global invalidation");
1298b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
1299b5a280c0SLe Tan         break;
1300b5a280c0SLe Tan 
1301b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_DOMAIN:
1302b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1303b5a280c0SLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
1304b5a280c0SLe Tan                     domain_id);
1305b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
1306b5a280c0SLe Tan         break;
1307b5a280c0SLe Tan 
1308b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_PAGE:
1309b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1310b5a280c0SLe Tan         addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
1311b5a280c0SLe Tan         am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
1312b5a280c0SLe Tan         VTD_DPRINTF(INV, "page-selective invalidation domain 0x%"PRIx16
1313b5a280c0SLe Tan                     " addr 0x%"PRIx64 " mask %"PRIu8, domain_id, addr, am);
1314b5a280c0SLe Tan         if (am > VTD_MAMV) {
1315b5a280c0SLe Tan             VTD_DPRINTF(GENERAL, "error: supported max address mask value is "
1316b5a280c0SLe Tan                         "%"PRIu8, (uint8_t)VTD_MAMV);
1317b5a280c0SLe Tan             return false;
1318b5a280c0SLe Tan         }
1319b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1320b5a280c0SLe Tan         break;
1321b5a280c0SLe Tan 
1322b5a280c0SLe Tan     default:
1323b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity in IOTLB Invalidate "
1324b5a280c0SLe Tan                     "Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1325b5a280c0SLe Tan                     inv_desc->hi, inv_desc->lo);
1326b5a280c0SLe Tan         return false;
1327b5a280c0SLe Tan     }
1328b5a280c0SLe Tan     return true;
1329b5a280c0SLe Tan }
1330b5a280c0SLe Tan 
1331ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s)
1332ed7b8fbcSLe Tan {
1333ed7b8fbcSLe Tan     VTDInvDesc inv_desc;
1334ed7b8fbcSLe Tan     uint8_t desc_type;
1335ed7b8fbcSLe Tan 
1336ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "iq head %"PRIu16, s->iq_head);
1337ed7b8fbcSLe Tan     if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
1338ed7b8fbcSLe Tan         s->iq_last_desc_type = VTD_INV_DESC_NONE;
1339ed7b8fbcSLe Tan         return false;
1340ed7b8fbcSLe Tan     }
1341ed7b8fbcSLe Tan     desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
1342ed7b8fbcSLe Tan     /* FIXME: should update at first or at last? */
1343ed7b8fbcSLe Tan     s->iq_last_desc_type = desc_type;
1344ed7b8fbcSLe Tan 
1345ed7b8fbcSLe Tan     switch (desc_type) {
1346ed7b8fbcSLe Tan     case VTD_INV_DESC_CC:
1347ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Context-cache Invalidate Descriptor hi 0x%"PRIx64
1348ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1349d92fa2dcSLe Tan         if (!vtd_process_context_cache_desc(s, &inv_desc)) {
1350d92fa2dcSLe Tan             return false;
1351d92fa2dcSLe Tan         }
1352ed7b8fbcSLe Tan         break;
1353ed7b8fbcSLe Tan 
1354ed7b8fbcSLe Tan     case VTD_INV_DESC_IOTLB:
1355ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "IOTLB Invalidate Descriptor hi 0x%"PRIx64
1356ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1357b5a280c0SLe Tan         if (!vtd_process_iotlb_desc(s, &inv_desc)) {
1358b5a280c0SLe Tan             return false;
1359b5a280c0SLe Tan         }
1360ed7b8fbcSLe Tan         break;
1361ed7b8fbcSLe Tan 
1362ed7b8fbcSLe Tan     case VTD_INV_DESC_WAIT:
1363ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Invalidation Wait Descriptor hi 0x%"PRIx64
1364ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1365ed7b8fbcSLe Tan         if (!vtd_process_wait_desc(s, &inv_desc)) {
1366ed7b8fbcSLe Tan             return false;
1367ed7b8fbcSLe Tan         }
1368ed7b8fbcSLe Tan         break;
1369ed7b8fbcSLe Tan 
1370ed7b8fbcSLe Tan     default:
1371ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: unkonw Invalidation Descriptor type "
1372ed7b8fbcSLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64 " type %"PRIu8,
1373ed7b8fbcSLe Tan                     inv_desc.hi, inv_desc.lo, desc_type);
1374ed7b8fbcSLe Tan         return false;
1375ed7b8fbcSLe Tan     }
1376ed7b8fbcSLe Tan     s->iq_head++;
1377ed7b8fbcSLe Tan     if (s->iq_head == s->iq_size) {
1378ed7b8fbcSLe Tan         s->iq_head = 0;
1379ed7b8fbcSLe Tan     }
1380ed7b8fbcSLe Tan     return true;
1381ed7b8fbcSLe Tan }
1382ed7b8fbcSLe Tan 
1383ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */
1384ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s)
1385ed7b8fbcSLe Tan {
1386ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "fetch Invalidation Descriptors");
1387ed7b8fbcSLe Tan     if (s->iq_tail >= s->iq_size) {
1388ed7b8fbcSLe Tan         /* Detects an invalid Tail pointer */
1389ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: iq_tail is %"PRIu16
1390ed7b8fbcSLe Tan                     " while iq_size is %"PRIu16, s->iq_tail, s->iq_size);
1391ed7b8fbcSLe Tan         vtd_handle_inv_queue_error(s);
1392ed7b8fbcSLe Tan         return;
1393ed7b8fbcSLe Tan     }
1394ed7b8fbcSLe Tan     while (s->iq_head != s->iq_tail) {
1395ed7b8fbcSLe Tan         if (!vtd_process_inv_desc(s)) {
1396ed7b8fbcSLe Tan             /* Invalidation Queue Errors */
1397ed7b8fbcSLe Tan             vtd_handle_inv_queue_error(s);
1398ed7b8fbcSLe Tan             break;
1399ed7b8fbcSLe Tan         }
1400ed7b8fbcSLe Tan         /* Must update the IQH_REG in time */
1401ed7b8fbcSLe Tan         vtd_set_quad_raw(s, DMAR_IQH_REG,
1402ed7b8fbcSLe Tan                          (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
1403ed7b8fbcSLe Tan                          VTD_IQH_QH_MASK);
1404ed7b8fbcSLe Tan     }
1405ed7b8fbcSLe Tan }
1406ed7b8fbcSLe Tan 
1407ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */
1408ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s)
1409ed7b8fbcSLe Tan {
1410ed7b8fbcSLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
1411ed7b8fbcSLe Tan 
1412ed7b8fbcSLe Tan     s->iq_tail = VTD_IQT_QT(val);
1413ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "set iq tail %"PRIu16, s->iq_tail);
1414ed7b8fbcSLe Tan     if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1415ed7b8fbcSLe Tan         /* Process Invalidation Queue here */
1416ed7b8fbcSLe Tan         vtd_fetch_inv_desc(s);
1417ed7b8fbcSLe Tan     }
1418ed7b8fbcSLe Tan }
1419ed7b8fbcSLe Tan 
14201da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s)
14211da12ec4SLe Tan {
14221da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
14231da12ec4SLe Tan     uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
14241da12ec4SLe Tan     uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
14251da12ec4SLe Tan 
14261da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
14271da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
14281da12ec4SLe Tan         VTD_DPRINTF(FLOG, "all pending interrupt conditions serviced, clear "
14291da12ec4SLe Tan                     "IP field of FECTL_REG");
14301da12ec4SLe Tan     }
1431ed7b8fbcSLe Tan     /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
1432ed7b8fbcSLe Tan      * Descriptors if there are any when Queued Invalidation is enabled?
1433ed7b8fbcSLe Tan      */
14341da12ec4SLe Tan }
14351da12ec4SLe Tan 
14361da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s)
14371da12ec4SLe Tan {
14381da12ec4SLe Tan     uint32_t fectl_reg;
14391da12ec4SLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
14401da12ec4SLe Tan      * need to compare the old value and the new value to conclude that
14411da12ec4SLe Tan      * software clears the IM field? Or just check if the IM field is zero?
14421da12ec4SLe Tan      */
14431da12ec4SLe Tan     fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
14441da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
14451da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
14461da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
14471da12ec4SLe Tan         VTD_DPRINTF(FLOG, "IM field is cleared, generate "
14481da12ec4SLe Tan                     "fault event interrupt");
14491da12ec4SLe Tan     }
14501da12ec4SLe Tan }
14511da12ec4SLe Tan 
1452ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s)
1453ed7b8fbcSLe Tan {
1454ed7b8fbcSLe Tan     uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
1455ed7b8fbcSLe Tan     uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1456ed7b8fbcSLe Tan 
1457ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
1458ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1459ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "pending completion interrupt condition serviced, "
1460ed7b8fbcSLe Tan                     "clear IP field of IECTL_REG");
1461ed7b8fbcSLe Tan     }
1462ed7b8fbcSLe Tan }
1463ed7b8fbcSLe Tan 
1464ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s)
1465ed7b8fbcSLe Tan {
1466ed7b8fbcSLe Tan     uint32_t iectl_reg;
1467ed7b8fbcSLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
1468ed7b8fbcSLe Tan      * need to compare the old value and the new value to conclude that
1469ed7b8fbcSLe Tan      * software clears the IM field? Or just check if the IM field is zero?
1470ed7b8fbcSLe Tan      */
1471ed7b8fbcSLe Tan     iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1472ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
1473ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
1474ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1475ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "IM field is cleared, generate "
1476ed7b8fbcSLe Tan                     "invalidation event interrupt");
1477ed7b8fbcSLe Tan     }
1478ed7b8fbcSLe Tan }
1479ed7b8fbcSLe Tan 
14801da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
14811da12ec4SLe Tan {
14821da12ec4SLe Tan     IntelIOMMUState *s = opaque;
14831da12ec4SLe Tan     uint64_t val;
14841da12ec4SLe Tan 
14851da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
14861da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: addr outside region: max 0x%"PRIx64
14871da12ec4SLe Tan                     ", got 0x%"PRIx64 " %d",
14881da12ec4SLe Tan                     (uint64_t)DMAR_REG_SIZE, addr, size);
14891da12ec4SLe Tan         return (uint64_t)-1;
14901da12ec4SLe Tan     }
14911da12ec4SLe Tan 
14921da12ec4SLe Tan     switch (addr) {
14931da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
14941da12ec4SLe Tan     case DMAR_RTADDR_REG:
14951da12ec4SLe Tan         if (size == 4) {
14961da12ec4SLe Tan             val = s->root & ((1ULL << 32) - 1);
14971da12ec4SLe Tan         } else {
14981da12ec4SLe Tan             val = s->root;
14991da12ec4SLe Tan         }
15001da12ec4SLe Tan         break;
15011da12ec4SLe Tan 
15021da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
15031da12ec4SLe Tan         assert(size == 4);
15041da12ec4SLe Tan         val = s->root >> 32;
15051da12ec4SLe Tan         break;
15061da12ec4SLe Tan 
1507ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
1508ed7b8fbcSLe Tan     case DMAR_IQA_REG:
1509ed7b8fbcSLe Tan         val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
1510ed7b8fbcSLe Tan         if (size == 4) {
1511ed7b8fbcSLe Tan             val = val & ((1ULL << 32) - 1);
1512ed7b8fbcSLe Tan         }
1513ed7b8fbcSLe Tan         break;
1514ed7b8fbcSLe Tan 
1515ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
1516ed7b8fbcSLe Tan         assert(size == 4);
1517ed7b8fbcSLe Tan         val = s->iq >> 32;
1518ed7b8fbcSLe Tan         break;
1519ed7b8fbcSLe Tan 
15201da12ec4SLe Tan     default:
15211da12ec4SLe Tan         if (size == 4) {
15221da12ec4SLe Tan             val = vtd_get_long(s, addr);
15231da12ec4SLe Tan         } else {
15241da12ec4SLe Tan             val = vtd_get_quad(s, addr);
15251da12ec4SLe Tan         }
15261da12ec4SLe Tan     }
15271da12ec4SLe Tan     VTD_DPRINTF(CSR, "addr 0x%"PRIx64 " size %d val 0x%"PRIx64,
15281da12ec4SLe Tan                 addr, size, val);
15291da12ec4SLe Tan     return val;
15301da12ec4SLe Tan }
15311da12ec4SLe Tan 
15321da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr,
15331da12ec4SLe Tan                           uint64_t val, unsigned size)
15341da12ec4SLe Tan {
15351da12ec4SLe Tan     IntelIOMMUState *s = opaque;
15361da12ec4SLe Tan 
15371da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
15381da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: addr outside region: max 0x%"PRIx64
15391da12ec4SLe Tan                     ", got 0x%"PRIx64 " %d",
15401da12ec4SLe Tan                     (uint64_t)DMAR_REG_SIZE, addr, size);
15411da12ec4SLe Tan         return;
15421da12ec4SLe Tan     }
15431da12ec4SLe Tan 
15441da12ec4SLe Tan     switch (addr) {
15451da12ec4SLe Tan     /* Global Command Register, 32-bit */
15461da12ec4SLe Tan     case DMAR_GCMD_REG:
15471da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_GCMD_REG write addr 0x%"PRIx64
15481da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15491da12ec4SLe Tan         vtd_set_long(s, addr, val);
15501da12ec4SLe Tan         vtd_handle_gcmd_write(s);
15511da12ec4SLe Tan         break;
15521da12ec4SLe Tan 
15531da12ec4SLe Tan     /* Context Command Register, 64-bit */
15541da12ec4SLe Tan     case DMAR_CCMD_REG:
15551da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_CCMD_REG write addr 0x%"PRIx64
15561da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15571da12ec4SLe Tan         if (size == 4) {
15581da12ec4SLe Tan             vtd_set_long(s, addr, val);
15591da12ec4SLe Tan         } else {
15601da12ec4SLe Tan             vtd_set_quad(s, addr, val);
15611da12ec4SLe Tan             vtd_handle_ccmd_write(s);
15621da12ec4SLe Tan         }
15631da12ec4SLe Tan         break;
15641da12ec4SLe Tan 
15651da12ec4SLe Tan     case DMAR_CCMD_REG_HI:
15661da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_CCMD_REG_HI write addr 0x%"PRIx64
15671da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15681da12ec4SLe Tan         assert(size == 4);
15691da12ec4SLe Tan         vtd_set_long(s, addr, val);
15701da12ec4SLe Tan         vtd_handle_ccmd_write(s);
15711da12ec4SLe Tan         break;
15721da12ec4SLe Tan 
15731da12ec4SLe Tan     /* IOTLB Invalidation Register, 64-bit */
15741da12ec4SLe Tan     case DMAR_IOTLB_REG:
15751da12ec4SLe Tan         VTD_DPRINTF(INV, "DMAR_IOTLB_REG write addr 0x%"PRIx64
15761da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15771da12ec4SLe Tan         if (size == 4) {
15781da12ec4SLe Tan             vtd_set_long(s, addr, val);
15791da12ec4SLe Tan         } else {
15801da12ec4SLe Tan             vtd_set_quad(s, addr, val);
15811da12ec4SLe Tan             vtd_handle_iotlb_write(s);
15821da12ec4SLe Tan         }
15831da12ec4SLe Tan         break;
15841da12ec4SLe Tan 
15851da12ec4SLe Tan     case DMAR_IOTLB_REG_HI:
15861da12ec4SLe Tan         VTD_DPRINTF(INV, "DMAR_IOTLB_REG_HI write addr 0x%"PRIx64
15871da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15881da12ec4SLe Tan         assert(size == 4);
15891da12ec4SLe Tan         vtd_set_long(s, addr, val);
15901da12ec4SLe Tan         vtd_handle_iotlb_write(s);
15911da12ec4SLe Tan         break;
15921da12ec4SLe Tan 
1593b5a280c0SLe Tan     /* Invalidate Address Register, 64-bit */
1594b5a280c0SLe Tan     case DMAR_IVA_REG:
1595b5a280c0SLe Tan         VTD_DPRINTF(INV, "DMAR_IVA_REG write addr 0x%"PRIx64
1596b5a280c0SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1597b5a280c0SLe Tan         if (size == 4) {
1598b5a280c0SLe Tan             vtd_set_long(s, addr, val);
1599b5a280c0SLe Tan         } else {
1600b5a280c0SLe Tan             vtd_set_quad(s, addr, val);
1601b5a280c0SLe Tan         }
1602b5a280c0SLe Tan         break;
1603b5a280c0SLe Tan 
1604b5a280c0SLe Tan     case DMAR_IVA_REG_HI:
1605b5a280c0SLe Tan         VTD_DPRINTF(INV, "DMAR_IVA_REG_HI write addr 0x%"PRIx64
1606b5a280c0SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1607b5a280c0SLe Tan         assert(size == 4);
1608b5a280c0SLe Tan         vtd_set_long(s, addr, val);
1609b5a280c0SLe Tan         break;
1610b5a280c0SLe Tan 
16111da12ec4SLe Tan     /* Fault Status Register, 32-bit */
16121da12ec4SLe Tan     case DMAR_FSTS_REG:
16131da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FSTS_REG write addr 0x%"PRIx64
16141da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16151da12ec4SLe Tan         assert(size == 4);
16161da12ec4SLe Tan         vtd_set_long(s, addr, val);
16171da12ec4SLe Tan         vtd_handle_fsts_write(s);
16181da12ec4SLe Tan         break;
16191da12ec4SLe Tan 
16201da12ec4SLe Tan     /* Fault Event Control Register, 32-bit */
16211da12ec4SLe Tan     case DMAR_FECTL_REG:
16221da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FECTL_REG write addr 0x%"PRIx64
16231da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16241da12ec4SLe Tan         assert(size == 4);
16251da12ec4SLe Tan         vtd_set_long(s, addr, val);
16261da12ec4SLe Tan         vtd_handle_fectl_write(s);
16271da12ec4SLe Tan         break;
16281da12ec4SLe Tan 
16291da12ec4SLe Tan     /* Fault Event Data Register, 32-bit */
16301da12ec4SLe Tan     case DMAR_FEDATA_REG:
16311da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEDATA_REG write addr 0x%"PRIx64
16321da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16331da12ec4SLe Tan         assert(size == 4);
16341da12ec4SLe Tan         vtd_set_long(s, addr, val);
16351da12ec4SLe Tan         break;
16361da12ec4SLe Tan 
16371da12ec4SLe Tan     /* Fault Event Address Register, 32-bit */
16381da12ec4SLe Tan     case DMAR_FEADDR_REG:
16391da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEADDR_REG write addr 0x%"PRIx64
16401da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16411da12ec4SLe Tan         assert(size == 4);
16421da12ec4SLe Tan         vtd_set_long(s, addr, val);
16431da12ec4SLe Tan         break;
16441da12ec4SLe Tan 
16451da12ec4SLe Tan     /* Fault Event Upper Address Register, 32-bit */
16461da12ec4SLe Tan     case DMAR_FEUADDR_REG:
16471da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEUADDR_REG write addr 0x%"PRIx64
16481da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16491da12ec4SLe Tan         assert(size == 4);
16501da12ec4SLe Tan         vtd_set_long(s, addr, val);
16511da12ec4SLe Tan         break;
16521da12ec4SLe Tan 
16531da12ec4SLe Tan     /* Protected Memory Enable Register, 32-bit */
16541da12ec4SLe Tan     case DMAR_PMEN_REG:
16551da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_PMEN_REG write addr 0x%"PRIx64
16561da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16571da12ec4SLe Tan         assert(size == 4);
16581da12ec4SLe Tan         vtd_set_long(s, addr, val);
16591da12ec4SLe Tan         break;
16601da12ec4SLe Tan 
16611da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
16621da12ec4SLe Tan     case DMAR_RTADDR_REG:
16631da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_RTADDR_REG write addr 0x%"PRIx64
16641da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16651da12ec4SLe Tan         if (size == 4) {
16661da12ec4SLe Tan             vtd_set_long(s, addr, val);
16671da12ec4SLe Tan         } else {
16681da12ec4SLe Tan             vtd_set_quad(s, addr, val);
16691da12ec4SLe Tan         }
16701da12ec4SLe Tan         break;
16711da12ec4SLe Tan 
16721da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
16731da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_RTADDR_REG_HI write addr 0x%"PRIx64
16741da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16751da12ec4SLe Tan         assert(size == 4);
16761da12ec4SLe Tan         vtd_set_long(s, addr, val);
16771da12ec4SLe Tan         break;
16781da12ec4SLe Tan 
1679ed7b8fbcSLe Tan     /* Invalidation Queue Tail Register, 64-bit */
1680ed7b8fbcSLe Tan     case DMAR_IQT_REG:
1681ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQT_REG write addr 0x%"PRIx64
1682ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1683ed7b8fbcSLe Tan         if (size == 4) {
1684ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
1685ed7b8fbcSLe Tan         } else {
1686ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
1687ed7b8fbcSLe Tan         }
1688ed7b8fbcSLe Tan         vtd_handle_iqt_write(s);
1689ed7b8fbcSLe Tan         break;
1690ed7b8fbcSLe Tan 
1691ed7b8fbcSLe Tan     case DMAR_IQT_REG_HI:
1692ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQT_REG_HI write addr 0x%"PRIx64
1693ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1694ed7b8fbcSLe Tan         assert(size == 4);
1695ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1696ed7b8fbcSLe Tan         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
1697ed7b8fbcSLe Tan         break;
1698ed7b8fbcSLe Tan 
1699ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
1700ed7b8fbcSLe Tan     case DMAR_IQA_REG:
1701ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQA_REG write addr 0x%"PRIx64
1702ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1703ed7b8fbcSLe Tan         if (size == 4) {
1704ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
1705ed7b8fbcSLe Tan         } else {
1706ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
1707ed7b8fbcSLe Tan         }
1708ed7b8fbcSLe Tan         break;
1709ed7b8fbcSLe Tan 
1710ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
1711ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQA_REG_HI write addr 0x%"PRIx64
1712ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1713ed7b8fbcSLe Tan         assert(size == 4);
1714ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1715ed7b8fbcSLe Tan         break;
1716ed7b8fbcSLe Tan 
1717ed7b8fbcSLe Tan     /* Invalidation Completion Status Register, 32-bit */
1718ed7b8fbcSLe Tan     case DMAR_ICS_REG:
1719ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_ICS_REG write addr 0x%"PRIx64
1720ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1721ed7b8fbcSLe Tan         assert(size == 4);
1722ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1723ed7b8fbcSLe Tan         vtd_handle_ics_write(s);
1724ed7b8fbcSLe Tan         break;
1725ed7b8fbcSLe Tan 
1726ed7b8fbcSLe Tan     /* Invalidation Event Control Register, 32-bit */
1727ed7b8fbcSLe Tan     case DMAR_IECTL_REG:
1728ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IECTL_REG write addr 0x%"PRIx64
1729ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1730ed7b8fbcSLe Tan         assert(size == 4);
1731ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1732ed7b8fbcSLe Tan         vtd_handle_iectl_write(s);
1733ed7b8fbcSLe Tan         break;
1734ed7b8fbcSLe Tan 
1735ed7b8fbcSLe Tan     /* Invalidation Event Data Register, 32-bit */
1736ed7b8fbcSLe Tan     case DMAR_IEDATA_REG:
1737ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEDATA_REG write addr 0x%"PRIx64
1738ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1739ed7b8fbcSLe Tan         assert(size == 4);
1740ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1741ed7b8fbcSLe Tan         break;
1742ed7b8fbcSLe Tan 
1743ed7b8fbcSLe Tan     /* Invalidation Event Address Register, 32-bit */
1744ed7b8fbcSLe Tan     case DMAR_IEADDR_REG:
1745ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEADDR_REG write addr 0x%"PRIx64
1746ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1747ed7b8fbcSLe Tan         assert(size == 4);
1748ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1749ed7b8fbcSLe Tan         break;
1750ed7b8fbcSLe Tan 
1751ed7b8fbcSLe Tan     /* Invalidation Event Upper Address Register, 32-bit */
1752ed7b8fbcSLe Tan     case DMAR_IEUADDR_REG:
1753ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEUADDR_REG write addr 0x%"PRIx64
1754ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1755ed7b8fbcSLe Tan         assert(size == 4);
1756ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1757ed7b8fbcSLe Tan         break;
1758ed7b8fbcSLe Tan 
17591da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
17601da12ec4SLe Tan     case DMAR_FRCD_REG_0_0:
17611da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_0 write addr 0x%"PRIx64
17621da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17631da12ec4SLe Tan         if (size == 4) {
17641da12ec4SLe Tan             vtd_set_long(s, addr, val);
17651da12ec4SLe Tan         } else {
17661da12ec4SLe Tan             vtd_set_quad(s, addr, val);
17671da12ec4SLe Tan         }
17681da12ec4SLe Tan         break;
17691da12ec4SLe Tan 
17701da12ec4SLe Tan     case DMAR_FRCD_REG_0_1:
17711da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_1 write addr 0x%"PRIx64
17721da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17731da12ec4SLe Tan         assert(size == 4);
17741da12ec4SLe Tan         vtd_set_long(s, addr, val);
17751da12ec4SLe Tan         break;
17761da12ec4SLe Tan 
17771da12ec4SLe Tan     case DMAR_FRCD_REG_0_2:
17781da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_2 write addr 0x%"PRIx64
17791da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17801da12ec4SLe Tan         if (size == 4) {
17811da12ec4SLe Tan             vtd_set_long(s, addr, val);
17821da12ec4SLe Tan         } else {
17831da12ec4SLe Tan             vtd_set_quad(s, addr, val);
17841da12ec4SLe Tan             /* May clear bit 127 (Fault), update PPF */
17851da12ec4SLe Tan             vtd_update_fsts_ppf(s);
17861da12ec4SLe Tan         }
17871da12ec4SLe Tan         break;
17881da12ec4SLe Tan 
17891da12ec4SLe Tan     case DMAR_FRCD_REG_0_3:
17901da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_3 write addr 0x%"PRIx64
17911da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17921da12ec4SLe Tan         assert(size == 4);
17931da12ec4SLe Tan         vtd_set_long(s, addr, val);
17941da12ec4SLe Tan         /* May clear bit 127 (Fault), update PPF */
17951da12ec4SLe Tan         vtd_update_fsts_ppf(s);
17961da12ec4SLe Tan         break;
17971da12ec4SLe Tan 
17981da12ec4SLe Tan     default:
17991da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: unhandled reg write addr 0x%"PRIx64
18001da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
18011da12ec4SLe Tan         if (size == 4) {
18021da12ec4SLe Tan             vtd_set_long(s, addr, val);
18031da12ec4SLe Tan         } else {
18041da12ec4SLe Tan             vtd_set_quad(s, addr, val);
18051da12ec4SLe Tan         }
18061da12ec4SLe Tan     }
18071da12ec4SLe Tan }
18081da12ec4SLe Tan 
18091da12ec4SLe Tan static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr,
18101da12ec4SLe Tan                                          bool is_write)
18111da12ec4SLe Tan {
18121da12ec4SLe Tan     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
18131da12ec4SLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
18141da12ec4SLe Tan     IOMMUTLBEntry ret = {
18151da12ec4SLe Tan         .target_as = &address_space_memory,
18161da12ec4SLe Tan         .iova = addr,
18171da12ec4SLe Tan         .translated_addr = 0,
18181da12ec4SLe Tan         .addr_mask = ~(hwaddr)0,
18191da12ec4SLe Tan         .perm = IOMMU_NONE,
18201da12ec4SLe Tan     };
18211da12ec4SLe Tan 
18221da12ec4SLe Tan     if (!s->dmar_enabled) {
18231da12ec4SLe Tan         /* DMAR disabled, passthrough, use 4k-page*/
18241da12ec4SLe Tan         ret.iova = addr & VTD_PAGE_MASK_4K;
18251da12ec4SLe Tan         ret.translated_addr = addr & VTD_PAGE_MASK_4K;
18261da12ec4SLe Tan         ret.addr_mask = ~VTD_PAGE_MASK_4K;
18271da12ec4SLe Tan         ret.perm = IOMMU_RW;
18281da12ec4SLe Tan         return ret;
18291da12ec4SLe Tan     }
18301da12ec4SLe Tan 
1831*7df953bdSKnut Omang     vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn, addr,
1832d92fa2dcSLe Tan                            is_write, &ret);
18331da12ec4SLe Tan     VTD_DPRINTF(MMU,
18341da12ec4SLe Tan                 "bus %"PRIu8 " slot %"PRIu8 " func %"PRIu8 " devfn %"PRIu8
1835*7df953bdSKnut Omang                 " gpa 0x%"PRIx64 " hpa 0x%"PRIx64, pci_bus_num(vtd_as->bus),
1836d92fa2dcSLe Tan                 VTD_PCI_SLOT(vtd_as->devfn), VTD_PCI_FUNC(vtd_as->devfn),
1837d92fa2dcSLe Tan                 vtd_as->devfn, addr, ret.translated_addr);
18381da12ec4SLe Tan     return ret;
18391da12ec4SLe Tan }
18401da12ec4SLe Tan 
18411da12ec4SLe Tan static const VMStateDescription vtd_vmstate = {
18421da12ec4SLe Tan     .name = "iommu-intel",
18431da12ec4SLe Tan     .unmigratable = 1,
18441da12ec4SLe Tan };
18451da12ec4SLe Tan 
18461da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = {
18471da12ec4SLe Tan     .read = vtd_mem_read,
18481da12ec4SLe Tan     .write = vtd_mem_write,
18491da12ec4SLe Tan     .endianness = DEVICE_LITTLE_ENDIAN,
18501da12ec4SLe Tan     .impl = {
18511da12ec4SLe Tan         .min_access_size = 4,
18521da12ec4SLe Tan         .max_access_size = 8,
18531da12ec4SLe Tan     },
18541da12ec4SLe Tan     .valid = {
18551da12ec4SLe Tan         .min_access_size = 4,
18561da12ec4SLe Tan         .max_access_size = 8,
18571da12ec4SLe Tan     },
18581da12ec4SLe Tan };
18591da12ec4SLe Tan 
18601da12ec4SLe Tan static Property vtd_properties[] = {
18611da12ec4SLe Tan     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
18621da12ec4SLe Tan     DEFINE_PROP_END_OF_LIST(),
18631da12ec4SLe Tan };
18641da12ec4SLe Tan 
1865*7df953bdSKnut Omang 
1866*7df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
1867*7df953bdSKnut Omang {
1868*7df953bdSKnut Omang     uintptr_t key = (uintptr_t)bus;
1869*7df953bdSKnut Omang     VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key);
1870*7df953bdSKnut Omang     VTDAddressSpace *vtd_dev_as;
1871*7df953bdSKnut Omang 
1872*7df953bdSKnut Omang     if (!vtd_bus) {
1873*7df953bdSKnut Omang         /* No corresponding free() */
1874*7df953bdSKnut Omang         vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * VTD_PCI_DEVFN_MAX);
1875*7df953bdSKnut Omang         vtd_bus->bus = bus;
1876*7df953bdSKnut Omang         key = (uintptr_t)bus;
1877*7df953bdSKnut Omang         g_hash_table_insert(s->vtd_as_by_busptr, &key, vtd_bus);
1878*7df953bdSKnut Omang     }
1879*7df953bdSKnut Omang 
1880*7df953bdSKnut Omang     vtd_dev_as = vtd_bus->dev_as[devfn];
1881*7df953bdSKnut Omang 
1882*7df953bdSKnut Omang     if (!vtd_dev_as) {
1883*7df953bdSKnut Omang         vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
1884*7df953bdSKnut Omang 
1885*7df953bdSKnut Omang         vtd_dev_as->bus = bus;
1886*7df953bdSKnut Omang         vtd_dev_as->devfn = (uint8_t)devfn;
1887*7df953bdSKnut Omang         vtd_dev_as->iommu_state = s;
1888*7df953bdSKnut Omang         vtd_dev_as->context_cache_entry.context_cache_gen = 0;
1889*7df953bdSKnut Omang         memory_region_init_iommu(&vtd_dev_as->iommu, OBJECT(s),
1890*7df953bdSKnut Omang                                  &s->iommu_ops, "intel_iommu", UINT64_MAX);
1891*7df953bdSKnut Omang         address_space_init(&vtd_dev_as->as,
1892*7df953bdSKnut Omang                            &vtd_dev_as->iommu, "intel_iommu");
1893*7df953bdSKnut Omang     }
1894*7df953bdSKnut Omang     return vtd_dev_as;
1895*7df953bdSKnut Omang }
1896*7df953bdSKnut Omang 
18971da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay
18981da12ec4SLe Tan  * attention when adding new initialization stuff.
18991da12ec4SLe Tan  */
19001da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s)
19011da12ec4SLe Tan {
19021da12ec4SLe Tan     memset(s->csr, 0, DMAR_REG_SIZE);
19031da12ec4SLe Tan     memset(s->wmask, 0, DMAR_REG_SIZE);
19041da12ec4SLe Tan     memset(s->w1cmask, 0, DMAR_REG_SIZE);
19051da12ec4SLe Tan     memset(s->womask, 0, DMAR_REG_SIZE);
19061da12ec4SLe Tan 
19071da12ec4SLe Tan     s->iommu_ops.translate = vtd_iommu_translate;
19081da12ec4SLe Tan     s->root = 0;
19091da12ec4SLe Tan     s->root_extended = false;
19101da12ec4SLe Tan     s->dmar_enabled = false;
19111da12ec4SLe Tan     s->iq_head = 0;
19121da12ec4SLe Tan     s->iq_tail = 0;
19131da12ec4SLe Tan     s->iq = 0;
19141da12ec4SLe Tan     s->iq_size = 0;
19151da12ec4SLe Tan     s->qi_enabled = false;
19161da12ec4SLe Tan     s->iq_last_desc_type = VTD_INV_DESC_NONE;
19171da12ec4SLe Tan     s->next_frcd_reg = 0;
19181da12ec4SLe Tan     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | VTD_CAP_MGAW |
1919b5a280c0SLe Tan              VTD_CAP_SAGAW | VTD_CAP_MAMV | VTD_CAP_PSI;
1920ed7b8fbcSLe Tan     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
19211da12ec4SLe Tan 
1922d92fa2dcSLe Tan     vtd_reset_context_cache(s);
1923b5a280c0SLe Tan     vtd_reset_iotlb(s);
1924d92fa2dcSLe Tan 
19251da12ec4SLe Tan     /* Define registers with default values and bit semantics */
19261da12ec4SLe Tan     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
19271da12ec4SLe Tan     vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
19281da12ec4SLe Tan     vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
19291da12ec4SLe Tan     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
19301da12ec4SLe Tan     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
19311da12ec4SLe Tan     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
19321da12ec4SLe Tan     vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0);
19331da12ec4SLe Tan     vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
19341da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
19351da12ec4SLe Tan 
19361da12ec4SLe Tan     /* Advanced Fault Logging not supported */
19371da12ec4SLe Tan     vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
19381da12ec4SLe Tan     vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
19391da12ec4SLe Tan     vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
19401da12ec4SLe Tan     vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
19411da12ec4SLe Tan 
19421da12ec4SLe Tan     /* Treated as RsvdZ when EIM in ECAP_REG is not supported
19431da12ec4SLe Tan      * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
19441da12ec4SLe Tan      */
19451da12ec4SLe Tan     vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
19461da12ec4SLe Tan 
19471da12ec4SLe Tan     /* Treated as RO for implementations that PLMR and PHMR fields reported
19481da12ec4SLe Tan      * as Clear in the CAP_REG.
19491da12ec4SLe Tan      * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
19501da12ec4SLe Tan      */
19511da12ec4SLe Tan     vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
19521da12ec4SLe Tan 
1953ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
1954ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
1955ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
1956ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
1957ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
1958ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
1959ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
1960ed7b8fbcSLe Tan     /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
1961ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
1962ed7b8fbcSLe Tan 
19631da12ec4SLe Tan     /* IOTLB registers */
19641da12ec4SLe Tan     vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
19651da12ec4SLe Tan     vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
19661da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
19671da12ec4SLe Tan 
19681da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
19691da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
19701da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
19711da12ec4SLe Tan }
19721da12ec4SLe Tan 
19731da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use
19741da12ec4SLe Tan  * the address space they got at first (won't ask the bus again).
19751da12ec4SLe Tan  */
19761da12ec4SLe Tan static void vtd_reset(DeviceState *dev)
19771da12ec4SLe Tan {
19781da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
19791da12ec4SLe Tan 
19801da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
19811da12ec4SLe Tan     vtd_init(s);
19821da12ec4SLe Tan }
19831da12ec4SLe Tan 
19841da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp)
19851da12ec4SLe Tan {
19861da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
19871da12ec4SLe Tan 
19881da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
1989*7df953bdSKnut Omang     memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num));
19901da12ec4SLe Tan     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
19911da12ec4SLe Tan                           "intel_iommu", DMAR_REG_SIZE);
19921da12ec4SLe Tan     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
1993b5a280c0SLe Tan     /* No corresponding destroy */
1994b5a280c0SLe Tan     s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
1995b5a280c0SLe Tan                                      g_free, g_free);
1996*7df953bdSKnut Omang     s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
1997*7df953bdSKnut Omang                                               g_free, g_free);
19981da12ec4SLe Tan     vtd_init(s);
19991da12ec4SLe Tan }
20001da12ec4SLe Tan 
20011da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data)
20021da12ec4SLe Tan {
20031da12ec4SLe Tan     DeviceClass *dc = DEVICE_CLASS(klass);
20041da12ec4SLe Tan 
20051da12ec4SLe Tan     dc->reset = vtd_reset;
20061da12ec4SLe Tan     dc->realize = vtd_realize;
20071da12ec4SLe Tan     dc->vmsd = &vtd_vmstate;
20081da12ec4SLe Tan     dc->props = vtd_properties;
20091da12ec4SLe Tan }
20101da12ec4SLe Tan 
20111da12ec4SLe Tan static const TypeInfo vtd_info = {
20121da12ec4SLe Tan     .name          = TYPE_INTEL_IOMMU_DEVICE,
20131da12ec4SLe Tan     .parent        = TYPE_SYS_BUS_DEVICE,
20141da12ec4SLe Tan     .instance_size = sizeof(IntelIOMMUState),
20151da12ec4SLe Tan     .class_init    = vtd_class_init,
20161da12ec4SLe Tan };
20171da12ec4SLe Tan 
20181da12ec4SLe Tan static void vtd_register_types(void)
20191da12ec4SLe Tan {
20201da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
20211da12ec4SLe Tan     type_register_static(&vtd_info);
20221da12ec4SLe Tan }
20231da12ec4SLe Tan 
20241da12ec4SLe Tan type_init(vtd_register_types)
2025