xref: /openbmc/qemu/hw/i386/intel_iommu.c (revision b5a280c00840f4ebc2f40afb4f74e37e34d19f7c)
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"
251da12ec4SLe Tan 
261da12ec4SLe Tan /*#define DEBUG_INTEL_IOMMU*/
271da12ec4SLe Tan #ifdef DEBUG_INTEL_IOMMU
281da12ec4SLe Tan enum {
291da12ec4SLe Tan     DEBUG_GENERAL, DEBUG_CSR, DEBUG_INV, DEBUG_MMU, DEBUG_FLOG,
30d92fa2dcSLe Tan     DEBUG_CACHE,
311da12ec4SLe Tan };
321da12ec4SLe Tan #define VTD_DBGBIT(x)   (1 << DEBUG_##x)
331da12ec4SLe Tan static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);
341da12ec4SLe Tan 
351da12ec4SLe Tan #define VTD_DPRINTF(what, fmt, ...) do { \
361da12ec4SLe Tan     if (vtd_dbgflags & VTD_DBGBIT(what)) { \
371da12ec4SLe Tan         fprintf(stderr, "(vtd)%s: " fmt "\n", __func__, \
381da12ec4SLe Tan                 ## __VA_ARGS__); } \
391da12ec4SLe Tan     } while (0)
401da12ec4SLe Tan #else
411da12ec4SLe Tan #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
421da12ec4SLe Tan #endif
431da12ec4SLe Tan 
441da12ec4SLe Tan static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
451da12ec4SLe Tan                             uint64_t wmask, uint64_t w1cmask)
461da12ec4SLe Tan {
471da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
481da12ec4SLe Tan     stq_le_p(&s->wmask[addr], wmask);
491da12ec4SLe Tan     stq_le_p(&s->w1cmask[addr], w1cmask);
501da12ec4SLe Tan }
511da12ec4SLe Tan 
521da12ec4SLe Tan static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
531da12ec4SLe Tan {
541da12ec4SLe Tan     stq_le_p(&s->womask[addr], mask);
551da12ec4SLe Tan }
561da12ec4SLe Tan 
571da12ec4SLe Tan static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
581da12ec4SLe Tan                             uint32_t wmask, uint32_t w1cmask)
591da12ec4SLe Tan {
601da12ec4SLe Tan     stl_le_p(&s->csr[addr], val);
611da12ec4SLe Tan     stl_le_p(&s->wmask[addr], wmask);
621da12ec4SLe Tan     stl_le_p(&s->w1cmask[addr], w1cmask);
631da12ec4SLe Tan }
641da12ec4SLe Tan 
651da12ec4SLe Tan static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
661da12ec4SLe Tan {
671da12ec4SLe Tan     stl_le_p(&s->womask[addr], mask);
681da12ec4SLe Tan }
691da12ec4SLe Tan 
701da12ec4SLe Tan /* "External" get/set operations */
711da12ec4SLe Tan static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
721da12ec4SLe Tan {
731da12ec4SLe Tan     uint64_t oldval = ldq_le_p(&s->csr[addr]);
741da12ec4SLe Tan     uint64_t wmask = ldq_le_p(&s->wmask[addr]);
751da12ec4SLe Tan     uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
761da12ec4SLe Tan     stq_le_p(&s->csr[addr],
771da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
781da12ec4SLe Tan }
791da12ec4SLe Tan 
801da12ec4SLe Tan static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
811da12ec4SLe Tan {
821da12ec4SLe Tan     uint32_t oldval = ldl_le_p(&s->csr[addr]);
831da12ec4SLe Tan     uint32_t wmask = ldl_le_p(&s->wmask[addr]);
841da12ec4SLe Tan     uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
851da12ec4SLe Tan     stl_le_p(&s->csr[addr],
861da12ec4SLe Tan              ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
871da12ec4SLe Tan }
881da12ec4SLe Tan 
891da12ec4SLe Tan static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
901da12ec4SLe Tan {
911da12ec4SLe Tan     uint64_t val = ldq_le_p(&s->csr[addr]);
921da12ec4SLe Tan     uint64_t womask = ldq_le_p(&s->womask[addr]);
931da12ec4SLe Tan     return val & ~womask;
941da12ec4SLe Tan }
951da12ec4SLe Tan 
961da12ec4SLe Tan static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
971da12ec4SLe Tan {
981da12ec4SLe Tan     uint32_t val = ldl_le_p(&s->csr[addr]);
991da12ec4SLe Tan     uint32_t womask = ldl_le_p(&s->womask[addr]);
1001da12ec4SLe Tan     return val & ~womask;
1011da12ec4SLe Tan }
1021da12ec4SLe Tan 
1031da12ec4SLe Tan /* "Internal" get/set operations */
1041da12ec4SLe Tan static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
1051da12ec4SLe Tan {
1061da12ec4SLe Tan     return ldq_le_p(&s->csr[addr]);
1071da12ec4SLe Tan }
1081da12ec4SLe Tan 
1091da12ec4SLe Tan static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
1101da12ec4SLe Tan {
1111da12ec4SLe Tan     return ldl_le_p(&s->csr[addr]);
1121da12ec4SLe Tan }
1131da12ec4SLe Tan 
1141da12ec4SLe Tan static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
1151da12ec4SLe Tan {
1161da12ec4SLe Tan     stq_le_p(&s->csr[addr], val);
1171da12ec4SLe Tan }
1181da12ec4SLe Tan 
1191da12ec4SLe Tan static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
1201da12ec4SLe Tan                                         uint32_t clear, uint32_t mask)
1211da12ec4SLe Tan {
1221da12ec4SLe Tan     uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
1231da12ec4SLe Tan     stl_le_p(&s->csr[addr], new_val);
1241da12ec4SLe Tan     return new_val;
1251da12ec4SLe Tan }
1261da12ec4SLe Tan 
1271da12ec4SLe Tan static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
1281da12ec4SLe Tan                                         uint64_t clear, uint64_t mask)
1291da12ec4SLe Tan {
1301da12ec4SLe Tan     uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
1311da12ec4SLe Tan     stq_le_p(&s->csr[addr], new_val);
1321da12ec4SLe Tan     return new_val;
1331da12ec4SLe Tan }
1341da12ec4SLe Tan 
135*b5a280c0SLe Tan /* GHashTable functions */
136*b5a280c0SLe Tan static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
137*b5a280c0SLe Tan {
138*b5a280c0SLe Tan     return *((const uint64_t *)v1) == *((const uint64_t *)v2);
139*b5a280c0SLe Tan }
140*b5a280c0SLe Tan 
141*b5a280c0SLe Tan static guint vtd_uint64_hash(gconstpointer v)
142*b5a280c0SLe Tan {
143*b5a280c0SLe Tan     return (guint)*(const uint64_t *)v;
144*b5a280c0SLe Tan }
145*b5a280c0SLe Tan 
146*b5a280c0SLe Tan static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
147*b5a280c0SLe Tan                                           gpointer user_data)
148*b5a280c0SLe Tan {
149*b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
150*b5a280c0SLe Tan     uint16_t domain_id = *(uint16_t *)user_data;
151*b5a280c0SLe Tan     return entry->domain_id == domain_id;
152*b5a280c0SLe Tan }
153*b5a280c0SLe Tan 
154*b5a280c0SLe Tan static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
155*b5a280c0SLe Tan                                         gpointer user_data)
156*b5a280c0SLe Tan {
157*b5a280c0SLe Tan     VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
158*b5a280c0SLe Tan     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
159*b5a280c0SLe Tan     uint64_t gfn = info->gfn & info->mask;
160*b5a280c0SLe Tan     return (entry->domain_id == info->domain_id) &&
161*b5a280c0SLe Tan             ((entry->gfn & info->mask) == gfn);
162*b5a280c0SLe Tan }
163*b5a280c0SLe Tan 
164d92fa2dcSLe Tan /* Reset all the gen of VTDAddressSpace to zero and set the gen of
165d92fa2dcSLe Tan  * IntelIOMMUState to 1.
166d92fa2dcSLe Tan  */
167d92fa2dcSLe Tan static void vtd_reset_context_cache(IntelIOMMUState *s)
168d92fa2dcSLe Tan {
169d92fa2dcSLe Tan     VTDAddressSpace **pvtd_as;
170d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
171d92fa2dcSLe Tan     uint32_t bus_it;
172d92fa2dcSLe Tan     uint32_t devfn_it;
173d92fa2dcSLe Tan 
174d92fa2dcSLe Tan     VTD_DPRINTF(CACHE, "global context_cache_gen=1");
175d92fa2dcSLe Tan     for (bus_it = 0; bus_it < VTD_PCI_BUS_MAX; ++bus_it) {
176d92fa2dcSLe Tan         pvtd_as = s->address_spaces[bus_it];
177d92fa2dcSLe Tan         if (!pvtd_as) {
178d92fa2dcSLe Tan             continue;
179d92fa2dcSLe Tan         }
180d92fa2dcSLe Tan         for (devfn_it = 0; devfn_it < VTD_PCI_DEVFN_MAX; ++devfn_it) {
181d92fa2dcSLe Tan             vtd_as = pvtd_as[devfn_it];
182d92fa2dcSLe Tan             if (!vtd_as) {
183d92fa2dcSLe Tan                 continue;
184d92fa2dcSLe Tan             }
185d92fa2dcSLe Tan             vtd_as->context_cache_entry.context_cache_gen = 0;
186d92fa2dcSLe Tan         }
187d92fa2dcSLe Tan     }
188d92fa2dcSLe Tan     s->context_cache_gen = 1;
189d92fa2dcSLe Tan }
190d92fa2dcSLe Tan 
191*b5a280c0SLe Tan static void vtd_reset_iotlb(IntelIOMMUState *s)
192*b5a280c0SLe Tan {
193*b5a280c0SLe Tan     assert(s->iotlb);
194*b5a280c0SLe Tan     g_hash_table_remove_all(s->iotlb);
195*b5a280c0SLe Tan }
196*b5a280c0SLe Tan 
197*b5a280c0SLe Tan static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
198*b5a280c0SLe Tan                                        hwaddr addr)
199*b5a280c0SLe Tan {
200*b5a280c0SLe Tan     uint64_t key;
201*b5a280c0SLe Tan 
202*b5a280c0SLe Tan     key = (addr >> VTD_PAGE_SHIFT_4K) |
203*b5a280c0SLe Tan            ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT);
204*b5a280c0SLe Tan     return g_hash_table_lookup(s->iotlb, &key);
205*b5a280c0SLe Tan 
206*b5a280c0SLe Tan }
207*b5a280c0SLe Tan 
208*b5a280c0SLe Tan static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
209*b5a280c0SLe Tan                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
210*b5a280c0SLe Tan                              bool read_flags, bool write_flags)
211*b5a280c0SLe Tan {
212*b5a280c0SLe Tan     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
213*b5a280c0SLe Tan     uint64_t *key = g_malloc(sizeof(*key));
214*b5a280c0SLe Tan     uint64_t gfn = addr >> VTD_PAGE_SHIFT_4K;
215*b5a280c0SLe Tan 
216*b5a280c0SLe Tan     VTD_DPRINTF(CACHE, "update iotlb sid 0x%"PRIx16 " gpa 0x%"PRIx64
217*b5a280c0SLe Tan                 " slpte 0x%"PRIx64 " did 0x%"PRIx16, source_id, addr, slpte,
218*b5a280c0SLe Tan                 domain_id);
219*b5a280c0SLe Tan     if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
220*b5a280c0SLe Tan         VTD_DPRINTF(CACHE, "iotlb exceeds size limit, forced to reset");
221*b5a280c0SLe Tan         vtd_reset_iotlb(s);
222*b5a280c0SLe Tan     }
223*b5a280c0SLe Tan 
224*b5a280c0SLe Tan     entry->gfn = gfn;
225*b5a280c0SLe Tan     entry->domain_id = domain_id;
226*b5a280c0SLe Tan     entry->slpte = slpte;
227*b5a280c0SLe Tan     entry->read_flags = read_flags;
228*b5a280c0SLe Tan     entry->write_flags = write_flags;
229*b5a280c0SLe Tan     *key = gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT);
230*b5a280c0SLe Tan     g_hash_table_replace(s->iotlb, key, entry);
231*b5a280c0SLe Tan }
232*b5a280c0SLe Tan 
2331da12ec4SLe Tan /* Given the reg addr of both the message data and address, generate an
2341da12ec4SLe Tan  * interrupt via MSI.
2351da12ec4SLe Tan  */
2361da12ec4SLe Tan static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
2371da12ec4SLe Tan                                    hwaddr mesg_data_reg)
2381da12ec4SLe Tan {
2391da12ec4SLe Tan     hwaddr addr;
2401da12ec4SLe Tan     uint32_t data;
2411da12ec4SLe Tan 
2421da12ec4SLe Tan     assert(mesg_data_reg < DMAR_REG_SIZE);
2431da12ec4SLe Tan     assert(mesg_addr_reg < DMAR_REG_SIZE);
2441da12ec4SLe Tan 
2451da12ec4SLe Tan     addr = vtd_get_long_raw(s, mesg_addr_reg);
2461da12ec4SLe Tan     data = vtd_get_long_raw(s, mesg_data_reg);
2471da12ec4SLe Tan 
2481da12ec4SLe Tan     VTD_DPRINTF(FLOG, "msi: addr 0x%"PRIx64 " data 0x%"PRIx32, addr, data);
2491da12ec4SLe Tan     stl_le_phys(&address_space_memory, addr, data);
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.
7481da12ec4SLe Tan  * @bus_num: The bus number
7491da12ec4SLe Tan  * @devfn: The devfn, which is the  combined of device and function number
7501da12ec4SLe Tan  * @is_write: The access is a write operation
7511da12ec4SLe Tan  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
7521da12ec4SLe Tan  */
753d92fa2dcSLe Tan static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, uint8_t bus_num,
7541da12ec4SLe Tan                                    uint8_t devfn, hwaddr addr, bool is_write,
7551da12ec4SLe Tan                                    IOMMUTLBEntry *entry)
7561da12ec4SLe Tan {
757d92fa2dcSLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
7581da12ec4SLe Tan     VTDContextEntry ce;
759d92fa2dcSLe Tan     VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
7601da12ec4SLe Tan     uint64_t slpte;
7611da12ec4SLe Tan     uint32_t level;
7621da12ec4SLe Tan     uint16_t source_id = vtd_make_source_id(bus_num, devfn);
7631da12ec4SLe Tan     int ret_fr;
7641da12ec4SLe Tan     bool is_fpd_set = false;
7651da12ec4SLe Tan     bool reads = true;
7661da12ec4SLe Tan     bool writes = true;
767*b5a280c0SLe Tan     VTDIOTLBEntry *iotlb_entry;
7681da12ec4SLe Tan 
7691da12ec4SLe Tan     /* Check if the request is in interrupt address range */
7701da12ec4SLe Tan     if (vtd_is_interrupt_addr(addr)) {
7711da12ec4SLe Tan         if (is_write) {
7721da12ec4SLe Tan             /* FIXME: since we don't know the length of the access here, we
7731da12ec4SLe Tan              * treat Non-DWORD length write requests without PASID as
7741da12ec4SLe Tan              * interrupt requests, too. Withoud interrupt remapping support,
7751da12ec4SLe Tan              * we just use 1:1 mapping.
7761da12ec4SLe Tan              */
7771da12ec4SLe Tan             VTD_DPRINTF(MMU, "write request to interrupt address "
7781da12ec4SLe Tan                         "gpa 0x%"PRIx64, addr);
7791da12ec4SLe Tan             entry->iova = addr & VTD_PAGE_MASK_4K;
7801da12ec4SLe Tan             entry->translated_addr = addr & VTD_PAGE_MASK_4K;
7811da12ec4SLe Tan             entry->addr_mask = ~VTD_PAGE_MASK_4K;
7821da12ec4SLe Tan             entry->perm = IOMMU_WO;
7831da12ec4SLe Tan             return;
7841da12ec4SLe Tan         } else {
7851da12ec4SLe Tan             VTD_DPRINTF(GENERAL, "error: read request from interrupt address "
7861da12ec4SLe Tan                         "gpa 0x%"PRIx64, addr);
7871da12ec4SLe Tan             vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, is_write);
7881da12ec4SLe Tan             return;
7891da12ec4SLe Tan         }
7901da12ec4SLe Tan     }
791*b5a280c0SLe Tan     /* Try to fetch slpte form IOTLB */
792*b5a280c0SLe Tan     iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
793*b5a280c0SLe Tan     if (iotlb_entry) {
794*b5a280c0SLe Tan         VTD_DPRINTF(CACHE, "hit iotlb sid 0x%"PRIx16 " gpa 0x%"PRIx64
795*b5a280c0SLe Tan                     " slpte 0x%"PRIx64 " did 0x%"PRIx16, source_id, addr,
796*b5a280c0SLe Tan                     iotlb_entry->slpte, iotlb_entry->domain_id);
797*b5a280c0SLe Tan         slpte = iotlb_entry->slpte;
798*b5a280c0SLe Tan         reads = iotlb_entry->read_flags;
799*b5a280c0SLe Tan         writes = iotlb_entry->write_flags;
800*b5a280c0SLe Tan         goto out;
801*b5a280c0SLe Tan     }
802d92fa2dcSLe Tan     /* Try to fetch context-entry from cache first */
803d92fa2dcSLe Tan     if (cc_entry->context_cache_gen == s->context_cache_gen) {
804d92fa2dcSLe Tan         VTD_DPRINTF(CACHE, "hit context-cache bus %d devfn %d "
805d92fa2dcSLe Tan                     "(hi %"PRIx64 " lo %"PRIx64 " gen %"PRIu32 ")",
806d92fa2dcSLe Tan                     bus_num, devfn, cc_entry->context_entry.hi,
807d92fa2dcSLe Tan                     cc_entry->context_entry.lo, cc_entry->context_cache_gen);
808d92fa2dcSLe Tan         ce = cc_entry->context_entry;
809d92fa2dcSLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
810d92fa2dcSLe Tan     } else {
8111da12ec4SLe Tan         ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
8121da12ec4SLe Tan         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
8131da12ec4SLe Tan         if (ret_fr) {
8141da12ec4SLe Tan             ret_fr = -ret_fr;
8151da12ec4SLe Tan             if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
816d92fa2dcSLe Tan                 VTD_DPRINTF(FLOG, "fault processing is disabled for DMA "
817d92fa2dcSLe Tan                             "requests through this context-entry "
818d92fa2dcSLe Tan                             "(with FPD Set)");
8191da12ec4SLe Tan             } else {
8201da12ec4SLe Tan                 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
8211da12ec4SLe Tan             }
8221da12ec4SLe Tan             return;
8231da12ec4SLe Tan         }
824d92fa2dcSLe Tan         /* Update context-cache */
825d92fa2dcSLe Tan         VTD_DPRINTF(CACHE, "update context-cache bus %d devfn %d "
826d92fa2dcSLe Tan                     "(hi %"PRIx64 " lo %"PRIx64 " gen %"PRIu32 "->%"PRIu32 ")",
827d92fa2dcSLe Tan                     bus_num, devfn, ce.hi, ce.lo,
828d92fa2dcSLe Tan                     cc_entry->context_cache_gen, s->context_cache_gen);
829d92fa2dcSLe Tan         cc_entry->context_entry = ce;
830d92fa2dcSLe Tan         cc_entry->context_cache_gen = s->context_cache_gen;
831d92fa2dcSLe Tan     }
8321da12ec4SLe Tan 
8331da12ec4SLe Tan     ret_fr = vtd_gpa_to_slpte(&ce, addr, is_write, &slpte, &level,
8341da12ec4SLe Tan                               &reads, &writes);
8351da12ec4SLe Tan     if (ret_fr) {
8361da12ec4SLe Tan         ret_fr = -ret_fr;
8371da12ec4SLe Tan         if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
8381da12ec4SLe Tan             VTD_DPRINTF(FLOG, "fault processing is disabled for DMA requests "
8391da12ec4SLe Tan                         "through this context-entry (with FPD Set)");
8401da12ec4SLe Tan         } else {
8411da12ec4SLe Tan             vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
8421da12ec4SLe Tan         }
8431da12ec4SLe Tan         return;
8441da12ec4SLe Tan     }
8451da12ec4SLe Tan 
846*b5a280c0SLe Tan     vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
847*b5a280c0SLe Tan                      reads, writes);
848*b5a280c0SLe Tan out:
8491da12ec4SLe Tan     entry->iova = addr & VTD_PAGE_MASK_4K;
8501da12ec4SLe Tan     entry->translated_addr = vtd_get_slpte_addr(slpte) & VTD_PAGE_MASK_4K;
8511da12ec4SLe Tan     entry->addr_mask = ~VTD_PAGE_MASK_4K;
8521da12ec4SLe Tan     entry->perm = (writes ? 2 : 0) + (reads ? 1 : 0);
8531da12ec4SLe Tan }
8541da12ec4SLe Tan 
8551da12ec4SLe Tan static void vtd_root_table_setup(IntelIOMMUState *s)
8561da12ec4SLe Tan {
8571da12ec4SLe Tan     s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
8581da12ec4SLe Tan     s->root_extended = s->root & VTD_RTADDR_RTT;
8591da12ec4SLe Tan     s->root &= VTD_RTADDR_ADDR_MASK;
8601da12ec4SLe Tan 
8611da12ec4SLe Tan     VTD_DPRINTF(CSR, "root_table addr 0x%"PRIx64 " %s", s->root,
8621da12ec4SLe Tan                 (s->root_extended ? "(extended)" : ""));
8631da12ec4SLe Tan }
8641da12ec4SLe Tan 
865d92fa2dcSLe Tan static void vtd_context_global_invalidate(IntelIOMMUState *s)
866d92fa2dcSLe Tan {
867d92fa2dcSLe Tan     s->context_cache_gen++;
868d92fa2dcSLe Tan     if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
869d92fa2dcSLe Tan         vtd_reset_context_cache(s);
870d92fa2dcSLe Tan     }
871d92fa2dcSLe Tan }
872d92fa2dcSLe Tan 
873d92fa2dcSLe Tan /* Do a context-cache device-selective invalidation.
874d92fa2dcSLe Tan  * @func_mask: FM field after shifting
875d92fa2dcSLe Tan  */
876d92fa2dcSLe Tan static void vtd_context_device_invalidate(IntelIOMMUState *s,
877d92fa2dcSLe Tan                                           uint16_t source_id,
878d92fa2dcSLe Tan                                           uint16_t func_mask)
879d92fa2dcSLe Tan {
880d92fa2dcSLe Tan     uint16_t mask;
881d92fa2dcSLe Tan     VTDAddressSpace **pvtd_as;
882d92fa2dcSLe Tan     VTDAddressSpace *vtd_as;
883d92fa2dcSLe Tan     uint16_t devfn;
884d92fa2dcSLe Tan     uint16_t devfn_it;
885d92fa2dcSLe Tan 
886d92fa2dcSLe Tan     switch (func_mask & 3) {
887d92fa2dcSLe Tan     case 0:
888d92fa2dcSLe Tan         mask = 0;   /* No bits in the SID field masked */
889d92fa2dcSLe Tan         break;
890d92fa2dcSLe Tan     case 1:
891d92fa2dcSLe Tan         mask = 4;   /* Mask bit 2 in the SID field */
892d92fa2dcSLe Tan         break;
893d92fa2dcSLe Tan     case 2:
894d92fa2dcSLe Tan         mask = 6;   /* Mask bit 2:1 in the SID field */
895d92fa2dcSLe Tan         break;
896d92fa2dcSLe Tan     case 3:
897d92fa2dcSLe Tan         mask = 7;   /* Mask bit 2:0 in the SID field */
898d92fa2dcSLe Tan         break;
899d92fa2dcSLe Tan     }
900d92fa2dcSLe Tan     VTD_DPRINTF(INV, "device-selective invalidation source 0x%"PRIx16
901d92fa2dcSLe Tan                     " mask %"PRIu16, source_id, mask);
902d92fa2dcSLe Tan     pvtd_as = s->address_spaces[VTD_SID_TO_BUS(source_id)];
903d92fa2dcSLe Tan     if (pvtd_as) {
904d92fa2dcSLe Tan         devfn = VTD_SID_TO_DEVFN(source_id);
905d92fa2dcSLe Tan         for (devfn_it = 0; devfn_it < VTD_PCI_DEVFN_MAX; ++devfn_it) {
906d92fa2dcSLe Tan             vtd_as = pvtd_as[devfn_it];
907d92fa2dcSLe Tan             if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
908d92fa2dcSLe Tan                 VTD_DPRINTF(INV, "invalidate context-cahce of devfn 0x%"PRIx16,
909d92fa2dcSLe Tan                             devfn_it);
910d92fa2dcSLe Tan                 vtd_as->context_cache_entry.context_cache_gen = 0;
911d92fa2dcSLe Tan             }
912d92fa2dcSLe Tan         }
913d92fa2dcSLe Tan     }
914d92fa2dcSLe Tan }
915d92fa2dcSLe Tan 
9161da12ec4SLe Tan /* Context-cache invalidation
9171da12ec4SLe Tan  * Returns the Context Actual Invalidation Granularity.
9181da12ec4SLe Tan  * @val: the content of the CCMD_REG
9191da12ec4SLe Tan  */
9201da12ec4SLe Tan static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
9211da12ec4SLe Tan {
9221da12ec4SLe Tan     uint64_t caig;
9231da12ec4SLe Tan     uint64_t type = val & VTD_CCMD_CIRG_MASK;
9241da12ec4SLe Tan 
9251da12ec4SLe Tan     switch (type) {
9261da12ec4SLe Tan     case VTD_CCMD_DOMAIN_INVL:
927d92fa2dcSLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
928d92fa2dcSLe Tan                     (uint16_t)VTD_CCMD_DID(val));
929d92fa2dcSLe Tan         /* Fall through */
930d92fa2dcSLe Tan     case VTD_CCMD_GLOBAL_INVL:
931d92fa2dcSLe Tan         VTD_DPRINTF(INV, "global invalidation");
932d92fa2dcSLe Tan         caig = VTD_CCMD_GLOBAL_INVL_A;
933d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
9341da12ec4SLe Tan         break;
9351da12ec4SLe Tan 
9361da12ec4SLe Tan     case VTD_CCMD_DEVICE_INVL:
9371da12ec4SLe Tan         caig = VTD_CCMD_DEVICE_INVL_A;
938d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
9391da12ec4SLe Tan         break;
9401da12ec4SLe Tan 
9411da12ec4SLe Tan     default:
942d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity");
9431da12ec4SLe Tan         caig = 0;
9441da12ec4SLe Tan     }
9451da12ec4SLe Tan     return caig;
9461da12ec4SLe Tan }
9471da12ec4SLe Tan 
948*b5a280c0SLe Tan static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
949*b5a280c0SLe Tan {
950*b5a280c0SLe Tan     vtd_reset_iotlb(s);
951*b5a280c0SLe Tan }
952*b5a280c0SLe Tan 
953*b5a280c0SLe Tan static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
954*b5a280c0SLe Tan {
955*b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
956*b5a280c0SLe Tan                                 &domain_id);
957*b5a280c0SLe Tan }
958*b5a280c0SLe Tan 
959*b5a280c0SLe Tan static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
960*b5a280c0SLe Tan                                       hwaddr addr, uint8_t am)
961*b5a280c0SLe Tan {
962*b5a280c0SLe Tan     VTDIOTLBPageInvInfo info;
963*b5a280c0SLe Tan 
964*b5a280c0SLe Tan     assert(am <= VTD_MAMV);
965*b5a280c0SLe Tan     info.domain_id = domain_id;
966*b5a280c0SLe Tan     info.gfn = addr >> VTD_PAGE_SHIFT_4K;
967*b5a280c0SLe Tan     info.mask = ~((1 << am) - 1);
968*b5a280c0SLe Tan     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
969*b5a280c0SLe Tan }
970*b5a280c0SLe Tan 
9711da12ec4SLe Tan /* Flush IOTLB
9721da12ec4SLe Tan  * Returns the IOTLB Actual Invalidation Granularity.
9731da12ec4SLe Tan  * @val: the content of the IOTLB_REG
9741da12ec4SLe Tan  */
9751da12ec4SLe Tan static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
9761da12ec4SLe Tan {
9771da12ec4SLe Tan     uint64_t iaig;
9781da12ec4SLe Tan     uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
979*b5a280c0SLe Tan     uint16_t domain_id;
980*b5a280c0SLe Tan     hwaddr addr;
981*b5a280c0SLe Tan     uint8_t am;
9821da12ec4SLe Tan 
9831da12ec4SLe Tan     switch (type) {
9841da12ec4SLe Tan     case VTD_TLB_GLOBAL_FLUSH:
985*b5a280c0SLe Tan         VTD_DPRINTF(INV, "global invalidation");
9861da12ec4SLe Tan         iaig = VTD_TLB_GLOBAL_FLUSH_A;
987*b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
9881da12ec4SLe Tan         break;
9891da12ec4SLe Tan 
9901da12ec4SLe Tan     case VTD_TLB_DSI_FLUSH:
991*b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
992*b5a280c0SLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
993*b5a280c0SLe Tan                     domain_id);
9941da12ec4SLe Tan         iaig = VTD_TLB_DSI_FLUSH_A;
995*b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
9961da12ec4SLe Tan         break;
9971da12ec4SLe Tan 
9981da12ec4SLe Tan     case VTD_TLB_PSI_FLUSH:
999*b5a280c0SLe Tan         domain_id = VTD_TLB_DID(val);
1000*b5a280c0SLe Tan         addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1001*b5a280c0SLe Tan         am = VTD_IVA_AM(addr);
1002*b5a280c0SLe Tan         addr = VTD_IVA_ADDR(addr);
1003*b5a280c0SLe Tan         VTD_DPRINTF(INV, "page-selective invalidation domain 0x%"PRIx16
1004*b5a280c0SLe Tan                     " addr 0x%"PRIx64 " mask %"PRIu8, domain_id, addr, am);
1005*b5a280c0SLe Tan         if (am > VTD_MAMV) {
1006*b5a280c0SLe Tan             VTD_DPRINTF(GENERAL, "error: supported max address mask value is "
1007*b5a280c0SLe Tan                         "%"PRIu8, (uint8_t)VTD_MAMV);
1008*b5a280c0SLe Tan             iaig = 0;
1009*b5a280c0SLe Tan             break;
1010*b5a280c0SLe Tan         }
10111da12ec4SLe Tan         iaig = VTD_TLB_PSI_FLUSH_A;
1012*b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
10131da12ec4SLe Tan         break;
10141da12ec4SLe Tan 
10151da12ec4SLe Tan     default:
1016*b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity");
10171da12ec4SLe Tan         iaig = 0;
10181da12ec4SLe Tan     }
10191da12ec4SLe Tan     return iaig;
10201da12ec4SLe Tan }
10211da12ec4SLe Tan 
1022ed7b8fbcSLe Tan static inline bool vtd_queued_inv_enable_check(IntelIOMMUState *s)
1023ed7b8fbcSLe Tan {
1024ed7b8fbcSLe Tan     return s->iq_tail == 0;
1025ed7b8fbcSLe Tan }
1026ed7b8fbcSLe Tan 
1027ed7b8fbcSLe Tan static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1028ed7b8fbcSLe Tan {
1029ed7b8fbcSLe Tan     return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1030ed7b8fbcSLe Tan            (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1031ed7b8fbcSLe Tan }
1032ed7b8fbcSLe Tan 
1033ed7b8fbcSLe Tan static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
1034ed7b8fbcSLe Tan {
1035ed7b8fbcSLe Tan     uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
1036ed7b8fbcSLe Tan 
1037ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "Queued Invalidation Enable %s", (en ? "on" : "off"));
1038ed7b8fbcSLe Tan     if (en) {
1039ed7b8fbcSLe Tan         if (vtd_queued_inv_enable_check(s)) {
1040ed7b8fbcSLe Tan             s->iq = iqa_val & VTD_IQA_IQA_MASK;
1041ed7b8fbcSLe Tan             /* 2^(x+8) entries */
1042ed7b8fbcSLe Tan             s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
1043ed7b8fbcSLe Tan             s->qi_enabled = true;
1044ed7b8fbcSLe Tan             VTD_DPRINTF(INV, "DMAR_IQA_REG 0x%"PRIx64, iqa_val);
1045ed7b8fbcSLe Tan             VTD_DPRINTF(INV, "Invalidation Queue addr 0x%"PRIx64 " size %d",
1046ed7b8fbcSLe Tan                         s->iq, s->iq_size);
1047ed7b8fbcSLe Tan             /* Ok - report back to driver */
1048ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
1049ed7b8fbcSLe Tan         } else {
1050ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: can't enable Queued Invalidation: "
1051ed7b8fbcSLe Tan                         "tail %"PRIu16, s->iq_tail);
1052ed7b8fbcSLe Tan         }
1053ed7b8fbcSLe Tan     } else {
1054ed7b8fbcSLe Tan         if (vtd_queued_inv_disable_check(s)) {
1055ed7b8fbcSLe Tan             /* disable Queued Invalidation */
1056ed7b8fbcSLe Tan             vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
1057ed7b8fbcSLe Tan             s->iq_head = 0;
1058ed7b8fbcSLe Tan             s->qi_enabled = false;
1059ed7b8fbcSLe Tan             /* Ok - report back to driver */
1060ed7b8fbcSLe Tan             vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
1061ed7b8fbcSLe Tan         } else {
1062ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: can't disable Queued Invalidation: "
1063ed7b8fbcSLe Tan                         "head %"PRIu16 ", tail %"PRIu16
1064ed7b8fbcSLe Tan                         ", last_descriptor %"PRIu8,
1065ed7b8fbcSLe Tan                         s->iq_head, s->iq_tail, s->iq_last_desc_type);
1066ed7b8fbcSLe Tan         }
1067ed7b8fbcSLe Tan     }
1068ed7b8fbcSLe Tan }
1069ed7b8fbcSLe Tan 
10701da12ec4SLe Tan /* Set Root Table Pointer */
10711da12ec4SLe Tan static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
10721da12ec4SLe Tan {
10731da12ec4SLe Tan     VTD_DPRINTF(CSR, "set Root Table Pointer");
10741da12ec4SLe Tan 
10751da12ec4SLe Tan     vtd_root_table_setup(s);
10761da12ec4SLe Tan     /* Ok - report back to driver */
10771da12ec4SLe Tan     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
10781da12ec4SLe Tan }
10791da12ec4SLe Tan 
10801da12ec4SLe Tan /* Handle Translation Enable/Disable */
10811da12ec4SLe Tan static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
10821da12ec4SLe Tan {
10831da12ec4SLe Tan     VTD_DPRINTF(CSR, "Translation Enable %s", (en ? "on" : "off"));
10841da12ec4SLe Tan 
10851da12ec4SLe Tan     if (en) {
10861da12ec4SLe Tan         s->dmar_enabled = true;
10871da12ec4SLe Tan         /* Ok - report back to driver */
10881da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
10891da12ec4SLe Tan     } else {
10901da12ec4SLe Tan         s->dmar_enabled = false;
10911da12ec4SLe Tan 
10921da12ec4SLe Tan         /* Clear the index of Fault Recording Register */
10931da12ec4SLe Tan         s->next_frcd_reg = 0;
10941da12ec4SLe Tan         /* Ok - report back to driver */
10951da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
10961da12ec4SLe Tan     }
10971da12ec4SLe Tan }
10981da12ec4SLe Tan 
10991da12ec4SLe Tan /* Handle write to Global Command Register */
11001da12ec4SLe Tan static void vtd_handle_gcmd_write(IntelIOMMUState *s)
11011da12ec4SLe Tan {
11021da12ec4SLe Tan     uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
11031da12ec4SLe Tan     uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
11041da12ec4SLe Tan     uint32_t changed = status ^ val;
11051da12ec4SLe Tan 
11061da12ec4SLe Tan     VTD_DPRINTF(CSR, "value 0x%"PRIx32 " status 0x%"PRIx32, val, status);
11071da12ec4SLe Tan     if (changed & VTD_GCMD_TE) {
11081da12ec4SLe Tan         /* Translation enable/disable */
11091da12ec4SLe Tan         vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
11101da12ec4SLe Tan     }
11111da12ec4SLe Tan     if (val & VTD_GCMD_SRTP) {
11121da12ec4SLe Tan         /* Set/update the root-table pointer */
11131da12ec4SLe Tan         vtd_handle_gcmd_srtp(s);
11141da12ec4SLe Tan     }
1115ed7b8fbcSLe Tan     if (changed & VTD_GCMD_QIE) {
1116ed7b8fbcSLe Tan         /* Queued Invalidation Enable */
1117ed7b8fbcSLe Tan         vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
1118ed7b8fbcSLe Tan     }
11191da12ec4SLe Tan }
11201da12ec4SLe Tan 
11211da12ec4SLe Tan /* Handle write to Context Command Register */
11221da12ec4SLe Tan static void vtd_handle_ccmd_write(IntelIOMMUState *s)
11231da12ec4SLe Tan {
11241da12ec4SLe Tan     uint64_t ret;
11251da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
11261da12ec4SLe Tan 
11271da12ec4SLe Tan     /* Context-cache invalidation request */
11281da12ec4SLe Tan     if (val & VTD_CCMD_ICC) {
1129ed7b8fbcSLe Tan         if (s->qi_enabled) {
1130ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: Queued Invalidation enabled, "
1131ed7b8fbcSLe Tan                         "should not use register-based invalidation");
1132ed7b8fbcSLe Tan             return;
1133ed7b8fbcSLe Tan         }
11341da12ec4SLe Tan         ret = vtd_context_cache_invalidate(s, val);
11351da12ec4SLe Tan         /* Invalidation completed. Change something to show */
11361da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
11371da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
11381da12ec4SLe Tan                                       ret);
11391da12ec4SLe Tan         VTD_DPRINTF(INV, "CCMD_REG write-back val: 0x%"PRIx64, ret);
11401da12ec4SLe Tan     }
11411da12ec4SLe Tan }
11421da12ec4SLe Tan 
11431da12ec4SLe Tan /* Handle write to IOTLB Invalidation Register */
11441da12ec4SLe Tan static void vtd_handle_iotlb_write(IntelIOMMUState *s)
11451da12ec4SLe Tan {
11461da12ec4SLe Tan     uint64_t ret;
11471da12ec4SLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
11481da12ec4SLe Tan 
11491da12ec4SLe Tan     /* IOTLB invalidation request */
11501da12ec4SLe Tan     if (val & VTD_TLB_IVT) {
1151ed7b8fbcSLe Tan         if (s->qi_enabled) {
1152ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: Queued Invalidation enabled, "
1153ed7b8fbcSLe Tan                         "should not use register-based invalidation");
1154ed7b8fbcSLe Tan             return;
1155ed7b8fbcSLe Tan         }
11561da12ec4SLe Tan         ret = vtd_iotlb_flush(s, val);
11571da12ec4SLe Tan         /* Invalidation completed. Change something to show */
11581da12ec4SLe Tan         vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
11591da12ec4SLe Tan         ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
11601da12ec4SLe Tan                                       VTD_TLB_FLUSH_GRANU_MASK_A, ret);
11611da12ec4SLe Tan         VTD_DPRINTF(INV, "IOTLB_REG write-back val: 0x%"PRIx64, ret);
11621da12ec4SLe Tan     }
11631da12ec4SLe Tan }
11641da12ec4SLe Tan 
1165ed7b8fbcSLe Tan /* Fetch an Invalidation Descriptor from the Invalidation Queue */
1166ed7b8fbcSLe Tan static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
1167ed7b8fbcSLe Tan                              VTDInvDesc *inv_desc)
1168ed7b8fbcSLe Tan {
1169ed7b8fbcSLe Tan     dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
1170ed7b8fbcSLe Tan     if (dma_memory_read(&address_space_memory, addr, inv_desc,
1171ed7b8fbcSLe Tan         sizeof(*inv_desc))) {
1172ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: fail to fetch Invalidation Descriptor "
1173ed7b8fbcSLe Tan                     "base_addr 0x%"PRIx64 " offset %"PRIu32, base_addr, offset);
1174ed7b8fbcSLe Tan         inv_desc->lo = 0;
1175ed7b8fbcSLe Tan         inv_desc->hi = 0;
1176ed7b8fbcSLe Tan 
1177ed7b8fbcSLe Tan         return false;
1178ed7b8fbcSLe Tan     }
1179ed7b8fbcSLe Tan     inv_desc->lo = le64_to_cpu(inv_desc->lo);
1180ed7b8fbcSLe Tan     inv_desc->hi = le64_to_cpu(inv_desc->hi);
1181ed7b8fbcSLe Tan     return true;
1182ed7b8fbcSLe Tan }
1183ed7b8fbcSLe Tan 
1184ed7b8fbcSLe Tan static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1185ed7b8fbcSLe Tan {
1186ed7b8fbcSLe Tan     if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
1187ed7b8fbcSLe Tan         (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
1188ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in Invalidation "
1189ed7b8fbcSLe Tan                     "Wait Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1190ed7b8fbcSLe Tan                     inv_desc->hi, inv_desc->lo);
1191ed7b8fbcSLe Tan         return false;
1192ed7b8fbcSLe Tan     }
1193ed7b8fbcSLe Tan     if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
1194ed7b8fbcSLe Tan         /* Status Write */
1195ed7b8fbcSLe Tan         uint32_t status_data = (uint32_t)(inv_desc->lo >>
1196ed7b8fbcSLe Tan                                VTD_INV_DESC_WAIT_DATA_SHIFT);
1197ed7b8fbcSLe Tan 
1198ed7b8fbcSLe Tan         assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
1199ed7b8fbcSLe Tan 
1200ed7b8fbcSLe Tan         /* FIXME: need to be masked with HAW? */
1201ed7b8fbcSLe Tan         dma_addr_t status_addr = inv_desc->hi;
1202ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "status data 0x%x, status addr 0x%"PRIx64,
1203ed7b8fbcSLe Tan                     status_data, status_addr);
1204ed7b8fbcSLe Tan         status_data = cpu_to_le32(status_data);
1205ed7b8fbcSLe Tan         if (dma_memory_write(&address_space_memory, status_addr, &status_data,
1206ed7b8fbcSLe Tan                              sizeof(status_data))) {
1207ed7b8fbcSLe Tan             VTD_DPRINTF(GENERAL, "error: fail to perform a coherent write");
1208ed7b8fbcSLe Tan             return false;
1209ed7b8fbcSLe Tan         }
1210ed7b8fbcSLe Tan     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
1211ed7b8fbcSLe Tan         /* Interrupt flag */
1212ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Invalidation Wait Descriptor interrupt completion");
1213ed7b8fbcSLe Tan         vtd_generate_completion_event(s);
1214ed7b8fbcSLe Tan     } else {
1215ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid Invalidation Wait Descriptor: "
1216ed7b8fbcSLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64, inv_desc->hi, inv_desc->lo);
1217ed7b8fbcSLe Tan         return false;
1218ed7b8fbcSLe Tan     }
1219ed7b8fbcSLe Tan     return true;
1220ed7b8fbcSLe Tan }
1221ed7b8fbcSLe Tan 
1222d92fa2dcSLe Tan static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
1223d92fa2dcSLe Tan                                            VTDInvDesc *inv_desc)
1224d92fa2dcSLe Tan {
1225d92fa2dcSLe Tan     if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
1226d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in Context-cache "
1227d92fa2dcSLe Tan                     "Invalidate Descriptor");
1228d92fa2dcSLe Tan         return false;
1229d92fa2dcSLe Tan     }
1230d92fa2dcSLe Tan     switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
1231d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DOMAIN:
1232d92fa2dcSLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
1233d92fa2dcSLe Tan                     (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
1234d92fa2dcSLe Tan         /* Fall through */
1235d92fa2dcSLe Tan     case VTD_INV_DESC_CC_GLOBAL:
1236d92fa2dcSLe Tan         VTD_DPRINTF(INV, "global invalidation");
1237d92fa2dcSLe Tan         vtd_context_global_invalidate(s);
1238d92fa2dcSLe Tan         break;
1239d92fa2dcSLe Tan 
1240d92fa2dcSLe Tan     case VTD_INV_DESC_CC_DEVICE:
1241d92fa2dcSLe Tan         vtd_context_device_invalidate(s, VTD_INV_DESC_CC_SID(inv_desc->lo),
1242d92fa2dcSLe Tan                                       VTD_INV_DESC_CC_FM(inv_desc->lo));
1243d92fa2dcSLe Tan         break;
1244d92fa2dcSLe Tan 
1245d92fa2dcSLe Tan     default:
1246d92fa2dcSLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity in Context-cache "
1247d92fa2dcSLe Tan                     "Invalidate Descriptor hi 0x%"PRIx64  " lo 0x%"PRIx64,
1248d92fa2dcSLe Tan                     inv_desc->hi, inv_desc->lo);
1249d92fa2dcSLe Tan         return false;
1250d92fa2dcSLe Tan     }
1251d92fa2dcSLe Tan     return true;
1252d92fa2dcSLe Tan }
1253d92fa2dcSLe Tan 
1254*b5a280c0SLe Tan static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1255*b5a280c0SLe Tan {
1256*b5a280c0SLe Tan     uint16_t domain_id;
1257*b5a280c0SLe Tan     uint8_t am;
1258*b5a280c0SLe Tan     hwaddr addr;
1259*b5a280c0SLe Tan 
1260*b5a280c0SLe Tan     if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
1261*b5a280c0SLe Tan         (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
1262*b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: non-zero reserved field in IOTLB "
1263*b5a280c0SLe Tan                     "Invalidate Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1264*b5a280c0SLe Tan                     inv_desc->hi, inv_desc->lo);
1265*b5a280c0SLe Tan         return false;
1266*b5a280c0SLe Tan     }
1267*b5a280c0SLe Tan 
1268*b5a280c0SLe Tan     switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
1269*b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_GLOBAL:
1270*b5a280c0SLe Tan         VTD_DPRINTF(INV, "global invalidation");
1271*b5a280c0SLe Tan         vtd_iotlb_global_invalidate(s);
1272*b5a280c0SLe Tan         break;
1273*b5a280c0SLe Tan 
1274*b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_DOMAIN:
1275*b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1276*b5a280c0SLe Tan         VTD_DPRINTF(INV, "domain-selective invalidation domain 0x%"PRIx16,
1277*b5a280c0SLe Tan                     domain_id);
1278*b5a280c0SLe Tan         vtd_iotlb_domain_invalidate(s, domain_id);
1279*b5a280c0SLe Tan         break;
1280*b5a280c0SLe Tan 
1281*b5a280c0SLe Tan     case VTD_INV_DESC_IOTLB_PAGE:
1282*b5a280c0SLe Tan         domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1283*b5a280c0SLe Tan         addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
1284*b5a280c0SLe Tan         am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
1285*b5a280c0SLe Tan         VTD_DPRINTF(INV, "page-selective invalidation domain 0x%"PRIx16
1286*b5a280c0SLe Tan                     " addr 0x%"PRIx64 " mask %"PRIu8, domain_id, addr, am);
1287*b5a280c0SLe Tan         if (am > VTD_MAMV) {
1288*b5a280c0SLe Tan             VTD_DPRINTF(GENERAL, "error: supported max address mask value is "
1289*b5a280c0SLe Tan                         "%"PRIu8, (uint8_t)VTD_MAMV);
1290*b5a280c0SLe Tan             return false;
1291*b5a280c0SLe Tan         }
1292*b5a280c0SLe Tan         vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1293*b5a280c0SLe Tan         break;
1294*b5a280c0SLe Tan 
1295*b5a280c0SLe Tan     default:
1296*b5a280c0SLe Tan         VTD_DPRINTF(GENERAL, "error: invalid granularity in IOTLB Invalidate "
1297*b5a280c0SLe Tan                     "Descriptor hi 0x%"PRIx64 " lo 0x%"PRIx64,
1298*b5a280c0SLe Tan                     inv_desc->hi, inv_desc->lo);
1299*b5a280c0SLe Tan         return false;
1300*b5a280c0SLe Tan     }
1301*b5a280c0SLe Tan     return true;
1302*b5a280c0SLe Tan }
1303*b5a280c0SLe Tan 
1304ed7b8fbcSLe Tan static bool vtd_process_inv_desc(IntelIOMMUState *s)
1305ed7b8fbcSLe Tan {
1306ed7b8fbcSLe Tan     VTDInvDesc inv_desc;
1307ed7b8fbcSLe Tan     uint8_t desc_type;
1308ed7b8fbcSLe Tan 
1309ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "iq head %"PRIu16, s->iq_head);
1310ed7b8fbcSLe Tan     if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
1311ed7b8fbcSLe Tan         s->iq_last_desc_type = VTD_INV_DESC_NONE;
1312ed7b8fbcSLe Tan         return false;
1313ed7b8fbcSLe Tan     }
1314ed7b8fbcSLe Tan     desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
1315ed7b8fbcSLe Tan     /* FIXME: should update at first or at last? */
1316ed7b8fbcSLe Tan     s->iq_last_desc_type = desc_type;
1317ed7b8fbcSLe Tan 
1318ed7b8fbcSLe Tan     switch (desc_type) {
1319ed7b8fbcSLe Tan     case VTD_INV_DESC_CC:
1320ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Context-cache Invalidate Descriptor hi 0x%"PRIx64
1321ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1322d92fa2dcSLe Tan         if (!vtd_process_context_cache_desc(s, &inv_desc)) {
1323d92fa2dcSLe Tan             return false;
1324d92fa2dcSLe Tan         }
1325ed7b8fbcSLe Tan         break;
1326ed7b8fbcSLe Tan 
1327ed7b8fbcSLe Tan     case VTD_INV_DESC_IOTLB:
1328ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "IOTLB Invalidate Descriptor hi 0x%"PRIx64
1329ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1330*b5a280c0SLe Tan         if (!vtd_process_iotlb_desc(s, &inv_desc)) {
1331*b5a280c0SLe Tan             return false;
1332*b5a280c0SLe Tan         }
1333ed7b8fbcSLe Tan         break;
1334ed7b8fbcSLe Tan 
1335ed7b8fbcSLe Tan     case VTD_INV_DESC_WAIT:
1336ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "Invalidation Wait Descriptor hi 0x%"PRIx64
1337ed7b8fbcSLe Tan                     " lo 0x%"PRIx64, inv_desc.hi, inv_desc.lo);
1338ed7b8fbcSLe Tan         if (!vtd_process_wait_desc(s, &inv_desc)) {
1339ed7b8fbcSLe Tan             return false;
1340ed7b8fbcSLe Tan         }
1341ed7b8fbcSLe Tan         break;
1342ed7b8fbcSLe Tan 
1343ed7b8fbcSLe Tan     default:
1344ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: unkonw Invalidation Descriptor type "
1345ed7b8fbcSLe Tan                     "hi 0x%"PRIx64 " lo 0x%"PRIx64 " type %"PRIu8,
1346ed7b8fbcSLe Tan                     inv_desc.hi, inv_desc.lo, desc_type);
1347ed7b8fbcSLe Tan         return false;
1348ed7b8fbcSLe Tan     }
1349ed7b8fbcSLe Tan     s->iq_head++;
1350ed7b8fbcSLe Tan     if (s->iq_head == s->iq_size) {
1351ed7b8fbcSLe Tan         s->iq_head = 0;
1352ed7b8fbcSLe Tan     }
1353ed7b8fbcSLe Tan     return true;
1354ed7b8fbcSLe Tan }
1355ed7b8fbcSLe Tan 
1356ed7b8fbcSLe Tan /* Try to fetch and process more Invalidation Descriptors */
1357ed7b8fbcSLe Tan static void vtd_fetch_inv_desc(IntelIOMMUState *s)
1358ed7b8fbcSLe Tan {
1359ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "fetch Invalidation Descriptors");
1360ed7b8fbcSLe Tan     if (s->iq_tail >= s->iq_size) {
1361ed7b8fbcSLe Tan         /* Detects an invalid Tail pointer */
1362ed7b8fbcSLe Tan         VTD_DPRINTF(GENERAL, "error: iq_tail is %"PRIu16
1363ed7b8fbcSLe Tan                     " while iq_size is %"PRIu16, s->iq_tail, s->iq_size);
1364ed7b8fbcSLe Tan         vtd_handle_inv_queue_error(s);
1365ed7b8fbcSLe Tan         return;
1366ed7b8fbcSLe Tan     }
1367ed7b8fbcSLe Tan     while (s->iq_head != s->iq_tail) {
1368ed7b8fbcSLe Tan         if (!vtd_process_inv_desc(s)) {
1369ed7b8fbcSLe Tan             /* Invalidation Queue Errors */
1370ed7b8fbcSLe Tan             vtd_handle_inv_queue_error(s);
1371ed7b8fbcSLe Tan             break;
1372ed7b8fbcSLe Tan         }
1373ed7b8fbcSLe Tan         /* Must update the IQH_REG in time */
1374ed7b8fbcSLe Tan         vtd_set_quad_raw(s, DMAR_IQH_REG,
1375ed7b8fbcSLe Tan                          (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
1376ed7b8fbcSLe Tan                          VTD_IQH_QH_MASK);
1377ed7b8fbcSLe Tan     }
1378ed7b8fbcSLe Tan }
1379ed7b8fbcSLe Tan 
1380ed7b8fbcSLe Tan /* Handle write to Invalidation Queue Tail Register */
1381ed7b8fbcSLe Tan static void vtd_handle_iqt_write(IntelIOMMUState *s)
1382ed7b8fbcSLe Tan {
1383ed7b8fbcSLe Tan     uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
1384ed7b8fbcSLe Tan 
1385ed7b8fbcSLe Tan     s->iq_tail = VTD_IQT_QT(val);
1386ed7b8fbcSLe Tan     VTD_DPRINTF(INV, "set iq tail %"PRIu16, s->iq_tail);
1387ed7b8fbcSLe Tan     if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1388ed7b8fbcSLe Tan         /* Process Invalidation Queue here */
1389ed7b8fbcSLe Tan         vtd_fetch_inv_desc(s);
1390ed7b8fbcSLe Tan     }
1391ed7b8fbcSLe Tan }
1392ed7b8fbcSLe Tan 
13931da12ec4SLe Tan static void vtd_handle_fsts_write(IntelIOMMUState *s)
13941da12ec4SLe Tan {
13951da12ec4SLe Tan     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
13961da12ec4SLe Tan     uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
13971da12ec4SLe Tan     uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
13981da12ec4SLe Tan 
13991da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
14001da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
14011da12ec4SLe Tan         VTD_DPRINTF(FLOG, "all pending interrupt conditions serviced, clear "
14021da12ec4SLe Tan                     "IP field of FECTL_REG");
14031da12ec4SLe Tan     }
1404ed7b8fbcSLe Tan     /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
1405ed7b8fbcSLe Tan      * Descriptors if there are any when Queued Invalidation is enabled?
1406ed7b8fbcSLe Tan      */
14071da12ec4SLe Tan }
14081da12ec4SLe Tan 
14091da12ec4SLe Tan static void vtd_handle_fectl_write(IntelIOMMUState *s)
14101da12ec4SLe Tan {
14111da12ec4SLe Tan     uint32_t fectl_reg;
14121da12ec4SLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
14131da12ec4SLe Tan      * need to compare the old value and the new value to conclude that
14141da12ec4SLe Tan      * software clears the IM field? Or just check if the IM field is zero?
14151da12ec4SLe Tan      */
14161da12ec4SLe Tan     fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
14171da12ec4SLe Tan     if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
14181da12ec4SLe Tan         vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
14191da12ec4SLe Tan         vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
14201da12ec4SLe Tan         VTD_DPRINTF(FLOG, "IM field is cleared, generate "
14211da12ec4SLe Tan                     "fault event interrupt");
14221da12ec4SLe Tan     }
14231da12ec4SLe Tan }
14241da12ec4SLe Tan 
1425ed7b8fbcSLe Tan static void vtd_handle_ics_write(IntelIOMMUState *s)
1426ed7b8fbcSLe Tan {
1427ed7b8fbcSLe Tan     uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
1428ed7b8fbcSLe Tan     uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1429ed7b8fbcSLe Tan 
1430ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
1431ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1432ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "pending completion interrupt condition serviced, "
1433ed7b8fbcSLe Tan                     "clear IP field of IECTL_REG");
1434ed7b8fbcSLe Tan     }
1435ed7b8fbcSLe Tan }
1436ed7b8fbcSLe Tan 
1437ed7b8fbcSLe Tan static void vtd_handle_iectl_write(IntelIOMMUState *s)
1438ed7b8fbcSLe Tan {
1439ed7b8fbcSLe Tan     uint32_t iectl_reg;
1440ed7b8fbcSLe Tan     /* FIXME: when software clears the IM field, check the IP field. But do we
1441ed7b8fbcSLe Tan      * need to compare the old value and the new value to conclude that
1442ed7b8fbcSLe Tan      * software clears the IM field? Or just check if the IM field is zero?
1443ed7b8fbcSLe Tan      */
1444ed7b8fbcSLe Tan     iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1445ed7b8fbcSLe Tan     if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
1446ed7b8fbcSLe Tan         vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
1447ed7b8fbcSLe Tan         vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1448ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "IM field is cleared, generate "
1449ed7b8fbcSLe Tan                     "invalidation event interrupt");
1450ed7b8fbcSLe Tan     }
1451ed7b8fbcSLe Tan }
1452ed7b8fbcSLe Tan 
14531da12ec4SLe Tan static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
14541da12ec4SLe Tan {
14551da12ec4SLe Tan     IntelIOMMUState *s = opaque;
14561da12ec4SLe Tan     uint64_t val;
14571da12ec4SLe Tan 
14581da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
14591da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: addr outside region: max 0x%"PRIx64
14601da12ec4SLe Tan                     ", got 0x%"PRIx64 " %d",
14611da12ec4SLe Tan                     (uint64_t)DMAR_REG_SIZE, addr, size);
14621da12ec4SLe Tan         return (uint64_t)-1;
14631da12ec4SLe Tan     }
14641da12ec4SLe Tan 
14651da12ec4SLe Tan     switch (addr) {
14661da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
14671da12ec4SLe Tan     case DMAR_RTADDR_REG:
14681da12ec4SLe Tan         if (size == 4) {
14691da12ec4SLe Tan             val = s->root & ((1ULL << 32) - 1);
14701da12ec4SLe Tan         } else {
14711da12ec4SLe Tan             val = s->root;
14721da12ec4SLe Tan         }
14731da12ec4SLe Tan         break;
14741da12ec4SLe Tan 
14751da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
14761da12ec4SLe Tan         assert(size == 4);
14771da12ec4SLe Tan         val = s->root >> 32;
14781da12ec4SLe Tan         break;
14791da12ec4SLe Tan 
1480ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
1481ed7b8fbcSLe Tan     case DMAR_IQA_REG:
1482ed7b8fbcSLe Tan         val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
1483ed7b8fbcSLe Tan         if (size == 4) {
1484ed7b8fbcSLe Tan             val = val & ((1ULL << 32) - 1);
1485ed7b8fbcSLe Tan         }
1486ed7b8fbcSLe Tan         break;
1487ed7b8fbcSLe Tan 
1488ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
1489ed7b8fbcSLe Tan         assert(size == 4);
1490ed7b8fbcSLe Tan         val = s->iq >> 32;
1491ed7b8fbcSLe Tan         break;
1492ed7b8fbcSLe Tan 
14931da12ec4SLe Tan     default:
14941da12ec4SLe Tan         if (size == 4) {
14951da12ec4SLe Tan             val = vtd_get_long(s, addr);
14961da12ec4SLe Tan         } else {
14971da12ec4SLe Tan             val = vtd_get_quad(s, addr);
14981da12ec4SLe Tan         }
14991da12ec4SLe Tan     }
15001da12ec4SLe Tan     VTD_DPRINTF(CSR, "addr 0x%"PRIx64 " size %d val 0x%"PRIx64,
15011da12ec4SLe Tan                 addr, size, val);
15021da12ec4SLe Tan     return val;
15031da12ec4SLe Tan }
15041da12ec4SLe Tan 
15051da12ec4SLe Tan static void vtd_mem_write(void *opaque, hwaddr addr,
15061da12ec4SLe Tan                           uint64_t val, unsigned size)
15071da12ec4SLe Tan {
15081da12ec4SLe Tan     IntelIOMMUState *s = opaque;
15091da12ec4SLe Tan 
15101da12ec4SLe Tan     if (addr + size > DMAR_REG_SIZE) {
15111da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: addr outside region: max 0x%"PRIx64
15121da12ec4SLe Tan                     ", got 0x%"PRIx64 " %d",
15131da12ec4SLe Tan                     (uint64_t)DMAR_REG_SIZE, addr, size);
15141da12ec4SLe Tan         return;
15151da12ec4SLe Tan     }
15161da12ec4SLe Tan 
15171da12ec4SLe Tan     switch (addr) {
15181da12ec4SLe Tan     /* Global Command Register, 32-bit */
15191da12ec4SLe Tan     case DMAR_GCMD_REG:
15201da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_GCMD_REG write addr 0x%"PRIx64
15211da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15221da12ec4SLe Tan         vtd_set_long(s, addr, val);
15231da12ec4SLe Tan         vtd_handle_gcmd_write(s);
15241da12ec4SLe Tan         break;
15251da12ec4SLe Tan 
15261da12ec4SLe Tan     /* Context Command Register, 64-bit */
15271da12ec4SLe Tan     case DMAR_CCMD_REG:
15281da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_CCMD_REG write addr 0x%"PRIx64
15291da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15301da12ec4SLe Tan         if (size == 4) {
15311da12ec4SLe Tan             vtd_set_long(s, addr, val);
15321da12ec4SLe Tan         } else {
15331da12ec4SLe Tan             vtd_set_quad(s, addr, val);
15341da12ec4SLe Tan             vtd_handle_ccmd_write(s);
15351da12ec4SLe Tan         }
15361da12ec4SLe Tan         break;
15371da12ec4SLe Tan 
15381da12ec4SLe Tan     case DMAR_CCMD_REG_HI:
15391da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_CCMD_REG_HI write addr 0x%"PRIx64
15401da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15411da12ec4SLe Tan         assert(size == 4);
15421da12ec4SLe Tan         vtd_set_long(s, addr, val);
15431da12ec4SLe Tan         vtd_handle_ccmd_write(s);
15441da12ec4SLe Tan         break;
15451da12ec4SLe Tan 
15461da12ec4SLe Tan     /* IOTLB Invalidation Register, 64-bit */
15471da12ec4SLe Tan     case DMAR_IOTLB_REG:
15481da12ec4SLe Tan         VTD_DPRINTF(INV, "DMAR_IOTLB_REG write addr 0x%"PRIx64
15491da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15501da12ec4SLe Tan         if (size == 4) {
15511da12ec4SLe Tan             vtd_set_long(s, addr, val);
15521da12ec4SLe Tan         } else {
15531da12ec4SLe Tan             vtd_set_quad(s, addr, val);
15541da12ec4SLe Tan             vtd_handle_iotlb_write(s);
15551da12ec4SLe Tan         }
15561da12ec4SLe Tan         break;
15571da12ec4SLe Tan 
15581da12ec4SLe Tan     case DMAR_IOTLB_REG_HI:
15591da12ec4SLe Tan         VTD_DPRINTF(INV, "DMAR_IOTLB_REG_HI write addr 0x%"PRIx64
15601da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15611da12ec4SLe Tan         assert(size == 4);
15621da12ec4SLe Tan         vtd_set_long(s, addr, val);
15631da12ec4SLe Tan         vtd_handle_iotlb_write(s);
15641da12ec4SLe Tan         break;
15651da12ec4SLe Tan 
1566*b5a280c0SLe Tan     /* Invalidate Address Register, 64-bit */
1567*b5a280c0SLe Tan     case DMAR_IVA_REG:
1568*b5a280c0SLe Tan         VTD_DPRINTF(INV, "DMAR_IVA_REG write addr 0x%"PRIx64
1569*b5a280c0SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1570*b5a280c0SLe Tan         if (size == 4) {
1571*b5a280c0SLe Tan             vtd_set_long(s, addr, val);
1572*b5a280c0SLe Tan         } else {
1573*b5a280c0SLe Tan             vtd_set_quad(s, addr, val);
1574*b5a280c0SLe Tan         }
1575*b5a280c0SLe Tan         break;
1576*b5a280c0SLe Tan 
1577*b5a280c0SLe Tan     case DMAR_IVA_REG_HI:
1578*b5a280c0SLe Tan         VTD_DPRINTF(INV, "DMAR_IVA_REG_HI write addr 0x%"PRIx64
1579*b5a280c0SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1580*b5a280c0SLe Tan         assert(size == 4);
1581*b5a280c0SLe Tan         vtd_set_long(s, addr, val);
1582*b5a280c0SLe Tan         break;
1583*b5a280c0SLe Tan 
15841da12ec4SLe Tan     /* Fault Status Register, 32-bit */
15851da12ec4SLe Tan     case DMAR_FSTS_REG:
15861da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FSTS_REG 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_fsts_write(s);
15911da12ec4SLe Tan         break;
15921da12ec4SLe Tan 
15931da12ec4SLe Tan     /* Fault Event Control Register, 32-bit */
15941da12ec4SLe Tan     case DMAR_FECTL_REG:
15951da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FECTL_REG write addr 0x%"PRIx64
15961da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
15971da12ec4SLe Tan         assert(size == 4);
15981da12ec4SLe Tan         vtd_set_long(s, addr, val);
15991da12ec4SLe Tan         vtd_handle_fectl_write(s);
16001da12ec4SLe Tan         break;
16011da12ec4SLe Tan 
16021da12ec4SLe Tan     /* Fault Event Data Register, 32-bit */
16031da12ec4SLe Tan     case DMAR_FEDATA_REG:
16041da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEDATA_REG write addr 0x%"PRIx64
16051da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16061da12ec4SLe Tan         assert(size == 4);
16071da12ec4SLe Tan         vtd_set_long(s, addr, val);
16081da12ec4SLe Tan         break;
16091da12ec4SLe Tan 
16101da12ec4SLe Tan     /* Fault Event Address Register, 32-bit */
16111da12ec4SLe Tan     case DMAR_FEADDR_REG:
16121da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEADDR_REG write addr 0x%"PRIx64
16131da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16141da12ec4SLe Tan         assert(size == 4);
16151da12ec4SLe Tan         vtd_set_long(s, addr, val);
16161da12ec4SLe Tan         break;
16171da12ec4SLe Tan 
16181da12ec4SLe Tan     /* Fault Event Upper Address Register, 32-bit */
16191da12ec4SLe Tan     case DMAR_FEUADDR_REG:
16201da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FEUADDR_REG write addr 0x%"PRIx64
16211da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16221da12ec4SLe Tan         assert(size == 4);
16231da12ec4SLe Tan         vtd_set_long(s, addr, val);
16241da12ec4SLe Tan         break;
16251da12ec4SLe Tan 
16261da12ec4SLe Tan     /* Protected Memory Enable Register, 32-bit */
16271da12ec4SLe Tan     case DMAR_PMEN_REG:
16281da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_PMEN_REG write addr 0x%"PRIx64
16291da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16301da12ec4SLe Tan         assert(size == 4);
16311da12ec4SLe Tan         vtd_set_long(s, addr, val);
16321da12ec4SLe Tan         break;
16331da12ec4SLe Tan 
16341da12ec4SLe Tan     /* Root Table Address Register, 64-bit */
16351da12ec4SLe Tan     case DMAR_RTADDR_REG:
16361da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_RTADDR_REG write addr 0x%"PRIx64
16371da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16381da12ec4SLe Tan         if (size == 4) {
16391da12ec4SLe Tan             vtd_set_long(s, addr, val);
16401da12ec4SLe Tan         } else {
16411da12ec4SLe Tan             vtd_set_quad(s, addr, val);
16421da12ec4SLe Tan         }
16431da12ec4SLe Tan         break;
16441da12ec4SLe Tan 
16451da12ec4SLe Tan     case DMAR_RTADDR_REG_HI:
16461da12ec4SLe Tan         VTD_DPRINTF(CSR, "DMAR_RTADDR_REG_HI write addr 0x%"PRIx64
16471da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
16481da12ec4SLe Tan         assert(size == 4);
16491da12ec4SLe Tan         vtd_set_long(s, addr, val);
16501da12ec4SLe Tan         break;
16511da12ec4SLe Tan 
1652ed7b8fbcSLe Tan     /* Invalidation Queue Tail Register, 64-bit */
1653ed7b8fbcSLe Tan     case DMAR_IQT_REG:
1654ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQT_REG write addr 0x%"PRIx64
1655ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1656ed7b8fbcSLe Tan         if (size == 4) {
1657ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
1658ed7b8fbcSLe Tan         } else {
1659ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
1660ed7b8fbcSLe Tan         }
1661ed7b8fbcSLe Tan         vtd_handle_iqt_write(s);
1662ed7b8fbcSLe Tan         break;
1663ed7b8fbcSLe Tan 
1664ed7b8fbcSLe Tan     case DMAR_IQT_REG_HI:
1665ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQT_REG_HI write addr 0x%"PRIx64
1666ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1667ed7b8fbcSLe Tan         assert(size == 4);
1668ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1669ed7b8fbcSLe Tan         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
1670ed7b8fbcSLe Tan         break;
1671ed7b8fbcSLe Tan 
1672ed7b8fbcSLe Tan     /* Invalidation Queue Address Register, 64-bit */
1673ed7b8fbcSLe Tan     case DMAR_IQA_REG:
1674ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQA_REG write addr 0x%"PRIx64
1675ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1676ed7b8fbcSLe Tan         if (size == 4) {
1677ed7b8fbcSLe Tan             vtd_set_long(s, addr, val);
1678ed7b8fbcSLe Tan         } else {
1679ed7b8fbcSLe Tan             vtd_set_quad(s, addr, val);
1680ed7b8fbcSLe Tan         }
1681ed7b8fbcSLe Tan         break;
1682ed7b8fbcSLe Tan 
1683ed7b8fbcSLe Tan     case DMAR_IQA_REG_HI:
1684ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IQA_REG_HI write addr 0x%"PRIx64
1685ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1686ed7b8fbcSLe Tan         assert(size == 4);
1687ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1688ed7b8fbcSLe Tan         break;
1689ed7b8fbcSLe Tan 
1690ed7b8fbcSLe Tan     /* Invalidation Completion Status Register, 32-bit */
1691ed7b8fbcSLe Tan     case DMAR_ICS_REG:
1692ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_ICS_REG 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         vtd_handle_ics_write(s);
1697ed7b8fbcSLe Tan         break;
1698ed7b8fbcSLe Tan 
1699ed7b8fbcSLe Tan     /* Invalidation Event Control Register, 32-bit */
1700ed7b8fbcSLe Tan     case DMAR_IECTL_REG:
1701ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IECTL_REG write addr 0x%"PRIx64
1702ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1703ed7b8fbcSLe Tan         assert(size == 4);
1704ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1705ed7b8fbcSLe Tan         vtd_handle_iectl_write(s);
1706ed7b8fbcSLe Tan         break;
1707ed7b8fbcSLe Tan 
1708ed7b8fbcSLe Tan     /* Invalidation Event Data Register, 32-bit */
1709ed7b8fbcSLe Tan     case DMAR_IEDATA_REG:
1710ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEDATA_REG write addr 0x%"PRIx64
1711ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1712ed7b8fbcSLe Tan         assert(size == 4);
1713ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1714ed7b8fbcSLe Tan         break;
1715ed7b8fbcSLe Tan 
1716ed7b8fbcSLe Tan     /* Invalidation Event Address Register, 32-bit */
1717ed7b8fbcSLe Tan     case DMAR_IEADDR_REG:
1718ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEADDR_REG write addr 0x%"PRIx64
1719ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1720ed7b8fbcSLe Tan         assert(size == 4);
1721ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1722ed7b8fbcSLe Tan         break;
1723ed7b8fbcSLe Tan 
1724ed7b8fbcSLe Tan     /* Invalidation Event Upper Address Register, 32-bit */
1725ed7b8fbcSLe Tan     case DMAR_IEUADDR_REG:
1726ed7b8fbcSLe Tan         VTD_DPRINTF(INV, "DMAR_IEUADDR_REG write addr 0x%"PRIx64
1727ed7b8fbcSLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
1728ed7b8fbcSLe Tan         assert(size == 4);
1729ed7b8fbcSLe Tan         vtd_set_long(s, addr, val);
1730ed7b8fbcSLe Tan         break;
1731ed7b8fbcSLe Tan 
17321da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
17331da12ec4SLe Tan     case DMAR_FRCD_REG_0_0:
17341da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_0 write addr 0x%"PRIx64
17351da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17361da12ec4SLe Tan         if (size == 4) {
17371da12ec4SLe Tan             vtd_set_long(s, addr, val);
17381da12ec4SLe Tan         } else {
17391da12ec4SLe Tan             vtd_set_quad(s, addr, val);
17401da12ec4SLe Tan         }
17411da12ec4SLe Tan         break;
17421da12ec4SLe Tan 
17431da12ec4SLe Tan     case DMAR_FRCD_REG_0_1:
17441da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_1 write addr 0x%"PRIx64
17451da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17461da12ec4SLe Tan         assert(size == 4);
17471da12ec4SLe Tan         vtd_set_long(s, addr, val);
17481da12ec4SLe Tan         break;
17491da12ec4SLe Tan 
17501da12ec4SLe Tan     case DMAR_FRCD_REG_0_2:
17511da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_2 write addr 0x%"PRIx64
17521da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17531da12ec4SLe Tan         if (size == 4) {
17541da12ec4SLe Tan             vtd_set_long(s, addr, val);
17551da12ec4SLe Tan         } else {
17561da12ec4SLe Tan             vtd_set_quad(s, addr, val);
17571da12ec4SLe Tan             /* May clear bit 127 (Fault), update PPF */
17581da12ec4SLe Tan             vtd_update_fsts_ppf(s);
17591da12ec4SLe Tan         }
17601da12ec4SLe Tan         break;
17611da12ec4SLe Tan 
17621da12ec4SLe Tan     case DMAR_FRCD_REG_0_3:
17631da12ec4SLe Tan         VTD_DPRINTF(FLOG, "DMAR_FRCD_REG_0_3 write addr 0x%"PRIx64
17641da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17651da12ec4SLe Tan         assert(size == 4);
17661da12ec4SLe Tan         vtd_set_long(s, addr, val);
17671da12ec4SLe Tan         /* May clear bit 127 (Fault), update PPF */
17681da12ec4SLe Tan         vtd_update_fsts_ppf(s);
17691da12ec4SLe Tan         break;
17701da12ec4SLe Tan 
17711da12ec4SLe Tan     default:
17721da12ec4SLe Tan         VTD_DPRINTF(GENERAL, "error: unhandled reg write addr 0x%"PRIx64
17731da12ec4SLe Tan                     ", size %d, val 0x%"PRIx64, addr, size, val);
17741da12ec4SLe Tan         if (size == 4) {
17751da12ec4SLe Tan             vtd_set_long(s, addr, val);
17761da12ec4SLe Tan         } else {
17771da12ec4SLe Tan             vtd_set_quad(s, addr, val);
17781da12ec4SLe Tan         }
17791da12ec4SLe Tan     }
17801da12ec4SLe Tan }
17811da12ec4SLe Tan 
17821da12ec4SLe Tan static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr,
17831da12ec4SLe Tan                                          bool is_write)
17841da12ec4SLe Tan {
17851da12ec4SLe Tan     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
17861da12ec4SLe Tan     IntelIOMMUState *s = vtd_as->iommu_state;
17871da12ec4SLe Tan     IOMMUTLBEntry ret = {
17881da12ec4SLe Tan         .target_as = &address_space_memory,
17891da12ec4SLe Tan         .iova = addr,
17901da12ec4SLe Tan         .translated_addr = 0,
17911da12ec4SLe Tan         .addr_mask = ~(hwaddr)0,
17921da12ec4SLe Tan         .perm = IOMMU_NONE,
17931da12ec4SLe Tan     };
17941da12ec4SLe Tan 
17951da12ec4SLe Tan     if (!s->dmar_enabled) {
17961da12ec4SLe Tan         /* DMAR disabled, passthrough, use 4k-page*/
17971da12ec4SLe Tan         ret.iova = addr & VTD_PAGE_MASK_4K;
17981da12ec4SLe Tan         ret.translated_addr = addr & VTD_PAGE_MASK_4K;
17991da12ec4SLe Tan         ret.addr_mask = ~VTD_PAGE_MASK_4K;
18001da12ec4SLe Tan         ret.perm = IOMMU_RW;
18011da12ec4SLe Tan         return ret;
18021da12ec4SLe Tan     }
18031da12ec4SLe Tan 
1804d92fa2dcSLe Tan     vtd_do_iommu_translate(vtd_as, vtd_as->bus_num, vtd_as->devfn, addr,
1805d92fa2dcSLe Tan                            is_write, &ret);
18061da12ec4SLe Tan     VTD_DPRINTF(MMU,
18071da12ec4SLe Tan                 "bus %"PRIu8 " slot %"PRIu8 " func %"PRIu8 " devfn %"PRIu8
1808d92fa2dcSLe Tan                 " gpa 0x%"PRIx64 " hpa 0x%"PRIx64, vtd_as->bus_num,
1809d92fa2dcSLe Tan                 VTD_PCI_SLOT(vtd_as->devfn), VTD_PCI_FUNC(vtd_as->devfn),
1810d92fa2dcSLe Tan                 vtd_as->devfn, addr, ret.translated_addr);
18111da12ec4SLe Tan     return ret;
18121da12ec4SLe Tan }
18131da12ec4SLe Tan 
18141da12ec4SLe Tan static const VMStateDescription vtd_vmstate = {
18151da12ec4SLe Tan     .name = "iommu-intel",
18161da12ec4SLe Tan     .unmigratable = 1,
18171da12ec4SLe Tan };
18181da12ec4SLe Tan 
18191da12ec4SLe Tan static const MemoryRegionOps vtd_mem_ops = {
18201da12ec4SLe Tan     .read = vtd_mem_read,
18211da12ec4SLe Tan     .write = vtd_mem_write,
18221da12ec4SLe Tan     .endianness = DEVICE_LITTLE_ENDIAN,
18231da12ec4SLe Tan     .impl = {
18241da12ec4SLe Tan         .min_access_size = 4,
18251da12ec4SLe Tan         .max_access_size = 8,
18261da12ec4SLe Tan     },
18271da12ec4SLe Tan     .valid = {
18281da12ec4SLe Tan         .min_access_size = 4,
18291da12ec4SLe Tan         .max_access_size = 8,
18301da12ec4SLe Tan     },
18311da12ec4SLe Tan };
18321da12ec4SLe Tan 
18331da12ec4SLe Tan static Property vtd_properties[] = {
18341da12ec4SLe Tan     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
18351da12ec4SLe Tan     DEFINE_PROP_END_OF_LIST(),
18361da12ec4SLe Tan };
18371da12ec4SLe Tan 
18381da12ec4SLe Tan /* Do the initialization. It will also be called when reset, so pay
18391da12ec4SLe Tan  * attention when adding new initialization stuff.
18401da12ec4SLe Tan  */
18411da12ec4SLe Tan static void vtd_init(IntelIOMMUState *s)
18421da12ec4SLe Tan {
18431da12ec4SLe Tan     memset(s->csr, 0, DMAR_REG_SIZE);
18441da12ec4SLe Tan     memset(s->wmask, 0, DMAR_REG_SIZE);
18451da12ec4SLe Tan     memset(s->w1cmask, 0, DMAR_REG_SIZE);
18461da12ec4SLe Tan     memset(s->womask, 0, DMAR_REG_SIZE);
18471da12ec4SLe Tan 
18481da12ec4SLe Tan     s->iommu_ops.translate = vtd_iommu_translate;
18491da12ec4SLe Tan     s->root = 0;
18501da12ec4SLe Tan     s->root_extended = false;
18511da12ec4SLe Tan     s->dmar_enabled = false;
18521da12ec4SLe Tan     s->iq_head = 0;
18531da12ec4SLe Tan     s->iq_tail = 0;
18541da12ec4SLe Tan     s->iq = 0;
18551da12ec4SLe Tan     s->iq_size = 0;
18561da12ec4SLe Tan     s->qi_enabled = false;
18571da12ec4SLe Tan     s->iq_last_desc_type = VTD_INV_DESC_NONE;
18581da12ec4SLe Tan     s->next_frcd_reg = 0;
18591da12ec4SLe Tan     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | VTD_CAP_MGAW |
1860*b5a280c0SLe Tan              VTD_CAP_SAGAW | VTD_CAP_MAMV | VTD_CAP_PSI;
1861ed7b8fbcSLe Tan     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
18621da12ec4SLe Tan 
1863d92fa2dcSLe Tan     vtd_reset_context_cache(s);
1864*b5a280c0SLe Tan     vtd_reset_iotlb(s);
1865d92fa2dcSLe Tan 
18661da12ec4SLe Tan     /* Define registers with default values and bit semantics */
18671da12ec4SLe Tan     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
18681da12ec4SLe Tan     vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
18691da12ec4SLe Tan     vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
18701da12ec4SLe Tan     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
18711da12ec4SLe Tan     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
18721da12ec4SLe Tan     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
18731da12ec4SLe Tan     vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0);
18741da12ec4SLe Tan     vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
18751da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
18761da12ec4SLe Tan 
18771da12ec4SLe Tan     /* Advanced Fault Logging not supported */
18781da12ec4SLe Tan     vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
18791da12ec4SLe Tan     vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
18801da12ec4SLe Tan     vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
18811da12ec4SLe Tan     vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
18821da12ec4SLe Tan 
18831da12ec4SLe Tan     /* Treated as RsvdZ when EIM in ECAP_REG is not supported
18841da12ec4SLe Tan      * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
18851da12ec4SLe Tan      */
18861da12ec4SLe Tan     vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
18871da12ec4SLe Tan 
18881da12ec4SLe Tan     /* Treated as RO for implementations that PLMR and PHMR fields reported
18891da12ec4SLe Tan      * as Clear in the CAP_REG.
18901da12ec4SLe Tan      * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
18911da12ec4SLe Tan      */
18921da12ec4SLe Tan     vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
18931da12ec4SLe Tan 
1894ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
1895ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
1896ed7b8fbcSLe Tan     vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
1897ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
1898ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
1899ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
1900ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
1901ed7b8fbcSLe Tan     /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
1902ed7b8fbcSLe Tan     vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
1903ed7b8fbcSLe Tan 
19041da12ec4SLe Tan     /* IOTLB registers */
19051da12ec4SLe Tan     vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
19061da12ec4SLe Tan     vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
19071da12ec4SLe Tan     vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
19081da12ec4SLe Tan 
19091da12ec4SLe Tan     /* Fault Recording Registers, 128-bit */
19101da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
19111da12ec4SLe Tan     vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
19121da12ec4SLe Tan }
19131da12ec4SLe Tan 
19141da12ec4SLe Tan /* Should not reset address_spaces when reset because devices will still use
19151da12ec4SLe Tan  * the address space they got at first (won't ask the bus again).
19161da12ec4SLe Tan  */
19171da12ec4SLe Tan static void vtd_reset(DeviceState *dev)
19181da12ec4SLe Tan {
19191da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
19201da12ec4SLe Tan 
19211da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
19221da12ec4SLe Tan     vtd_init(s);
19231da12ec4SLe Tan }
19241da12ec4SLe Tan 
19251da12ec4SLe Tan static void vtd_realize(DeviceState *dev, Error **errp)
19261da12ec4SLe Tan {
19271da12ec4SLe Tan     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
19281da12ec4SLe Tan 
19291da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
19301da12ec4SLe Tan     memset(s->address_spaces, 0, sizeof(s->address_spaces));
19311da12ec4SLe Tan     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
19321da12ec4SLe Tan                           "intel_iommu", DMAR_REG_SIZE);
19331da12ec4SLe Tan     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
1934*b5a280c0SLe Tan     /* No corresponding destroy */
1935*b5a280c0SLe Tan     s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
1936*b5a280c0SLe Tan                                      g_free, g_free);
19371da12ec4SLe Tan     vtd_init(s);
19381da12ec4SLe Tan }
19391da12ec4SLe Tan 
19401da12ec4SLe Tan static void vtd_class_init(ObjectClass *klass, void *data)
19411da12ec4SLe Tan {
19421da12ec4SLe Tan     DeviceClass *dc = DEVICE_CLASS(klass);
19431da12ec4SLe Tan 
19441da12ec4SLe Tan     dc->reset = vtd_reset;
19451da12ec4SLe Tan     dc->realize = vtd_realize;
19461da12ec4SLe Tan     dc->vmsd = &vtd_vmstate;
19471da12ec4SLe Tan     dc->props = vtd_properties;
19481da12ec4SLe Tan }
19491da12ec4SLe Tan 
19501da12ec4SLe Tan static const TypeInfo vtd_info = {
19511da12ec4SLe Tan     .name          = TYPE_INTEL_IOMMU_DEVICE,
19521da12ec4SLe Tan     .parent        = TYPE_SYS_BUS_DEVICE,
19531da12ec4SLe Tan     .instance_size = sizeof(IntelIOMMUState),
19541da12ec4SLe Tan     .class_init    = vtd_class_init,
19551da12ec4SLe Tan };
19561da12ec4SLe Tan 
19571da12ec4SLe Tan static void vtd_register_types(void)
19581da12ec4SLe Tan {
19591da12ec4SLe Tan     VTD_DPRINTF(GENERAL, "");
19601da12ec4SLe Tan     type_register_static(&vtd_info);
19611da12ec4SLe Tan }
19621da12ec4SLe Tan 
19631da12ec4SLe Tan type_init(vtd_register_types)
1964