]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/intel_iommu.c
intel_iommu: scalable mode emulation
[mirror_qemu.git] / hw / i386 / intel_iommu.c
CommitLineData
1da12ec4
LT
1/*
2 * QEMU emulation of an Intel IOMMU (VT-d)
3 * (DMA Remapping device)
4 *
5 * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com>
6 * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
b6a0aa05 22#include "qemu/osdep.h"
4684a204 23#include "qemu/error-report.h"
6333e93c 24#include "qapi/error.h"
1da12ec4
LT
25#include "hw/sysbus.h"
26#include "exec/address-spaces.h"
27#include "intel_iommu_internal.h"
7df953bd 28#include "hw/pci/pci.h"
3cb3b154 29#include "hw/pci/pci_bus.h"
621d983a 30#include "hw/i386/pc.h"
dea651a9 31#include "hw/i386/apic-msidef.h"
04af0e18
PX
32#include "hw/boards.h"
33#include "hw/i386/x86-iommu.h"
cb135f59 34#include "hw/pci-host/q35.h"
4684a204 35#include "sysemu/kvm.h"
32946019 36#include "hw/i386/apic_internal.h"
fb506e70 37#include "kvm_i386.h"
bc535e59 38#include "trace.h"
1da12ec4 39
fb43cf73
LY
40/* context entry operations */
41#define VTD_CE_GET_RID2PASID(ce) \
42 ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK)
43#define VTD_CE_GET_PASID_DIR_TABLE(ce) \
44 ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
45
46/* pe operations */
47#define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
48#define VTD_PE_GET_LEVEL(pe) (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
49#define VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write) {\
50 if (ret_fr) { \
51 ret_fr = -ret_fr; \
52 if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) { \
53 trace_vtd_fault_disabled(); \
54 } else { \
55 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write); \
56 } \
57 goto error; \
58 } \
59}
60
2cc9ddcc 61static void vtd_address_space_refresh_all(IntelIOMMUState *s);
c28b535d 62static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
2cc9ddcc 63
1da12ec4
LT
64static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
65 uint64_t wmask, uint64_t w1cmask)
66{
67 stq_le_p(&s->csr[addr], val);
68 stq_le_p(&s->wmask[addr], wmask);
69 stq_le_p(&s->w1cmask[addr], w1cmask);
70}
71
72static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
73{
74 stq_le_p(&s->womask[addr], mask);
75}
76
77static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
78 uint32_t wmask, uint32_t w1cmask)
79{
80 stl_le_p(&s->csr[addr], val);
81 stl_le_p(&s->wmask[addr], wmask);
82 stl_le_p(&s->w1cmask[addr], w1cmask);
83}
84
85static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
86{
87 stl_le_p(&s->womask[addr], mask);
88}
89
90/* "External" get/set operations */
91static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
92{
93 uint64_t oldval = ldq_le_p(&s->csr[addr]);
94 uint64_t wmask = ldq_le_p(&s->wmask[addr]);
95 uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
96 stq_le_p(&s->csr[addr],
97 ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
98}
99
100static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
101{
102 uint32_t oldval = ldl_le_p(&s->csr[addr]);
103 uint32_t wmask = ldl_le_p(&s->wmask[addr]);
104 uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
105 stl_le_p(&s->csr[addr],
106 ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
107}
108
109static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
110{
111 uint64_t val = ldq_le_p(&s->csr[addr]);
112 uint64_t womask = ldq_le_p(&s->womask[addr]);
113 return val & ~womask;
114}
115
116static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
117{
118 uint32_t val = ldl_le_p(&s->csr[addr]);
119 uint32_t womask = ldl_le_p(&s->womask[addr]);
120 return val & ~womask;
121}
122
123/* "Internal" get/set operations */
124static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
125{
126 return ldq_le_p(&s->csr[addr]);
127}
128
129static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
130{
131 return ldl_le_p(&s->csr[addr]);
132}
133
134static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
135{
136 stq_le_p(&s->csr[addr], val);
137}
138
139static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
140 uint32_t clear, uint32_t mask)
141{
142 uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
143 stl_le_p(&s->csr[addr], new_val);
144 return new_val;
145}
146
147static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
148 uint64_t clear, uint64_t mask)
149{
150 uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
151 stq_le_p(&s->csr[addr], new_val);
152 return new_val;
153}
154
1d9efa73
PX
155static inline void vtd_iommu_lock(IntelIOMMUState *s)
156{
157 qemu_mutex_lock(&s->iommu_lock);
158}
159
160static inline void vtd_iommu_unlock(IntelIOMMUState *s)
161{
162 qemu_mutex_unlock(&s->iommu_lock);
163}
164
4f8a62a9
PX
165/* Whether the address space needs to notify new mappings */
166static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
167{
168 return as->notifier_flags & IOMMU_NOTIFIER_MAP;
169}
170
b5a280c0
LT
171/* GHashTable functions */
172static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
173{
174 return *((const uint64_t *)v1) == *((const uint64_t *)v2);
175}
176
177static guint vtd_uint64_hash(gconstpointer v)
178{
179 return (guint)*(const uint64_t *)v;
180}
181
182static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
183 gpointer user_data)
184{
185 VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
186 uint16_t domain_id = *(uint16_t *)user_data;
187 return entry->domain_id == domain_id;
188}
189
d66b969b
JW
190/* The shift of an addr for a certain level of paging structure */
191static inline uint32_t vtd_slpt_level_shift(uint32_t level)
192{
7e58326a 193 assert(level != 0);
d66b969b
JW
194 return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
195}
196
197static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
198{
199 return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
200}
201
b5a280c0
LT
202static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
203 gpointer user_data)
204{
205 VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
206 VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
d66b969b
JW
207 uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
208 uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
b5a280c0 209 return (entry->domain_id == info->domain_id) &&
d66b969b
JW
210 (((entry->gfn & info->mask) == gfn) ||
211 (entry->gfn == gfn_tlb));
b5a280c0
LT
212}
213
d92fa2dc 214/* Reset all the gen of VTDAddressSpace to zero and set the gen of
1d9efa73 215 * IntelIOMMUState to 1. Must be called with IOMMU lock held.
d92fa2dc 216 */
1d9efa73 217static void vtd_reset_context_cache_locked(IntelIOMMUState *s)
d92fa2dc 218{
d92fa2dc 219 VTDAddressSpace *vtd_as;
7df953bd
KO
220 VTDBus *vtd_bus;
221 GHashTableIter bus_it;
d92fa2dc
LT
222 uint32_t devfn_it;
223
7feb51b7
PX
224 trace_vtd_context_cache_reset();
225
7df953bd
KO
226 g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr);
227
7df953bd 228 while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) {
bf33cc75 229 for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
7df953bd 230 vtd_as = vtd_bus->dev_as[devfn_it];
d92fa2dc
LT
231 if (!vtd_as) {
232 continue;
233 }
234 vtd_as->context_cache_entry.context_cache_gen = 0;
235 }
236 }
237 s->context_cache_gen = 1;
238}
239
1d9efa73
PX
240/* Must be called with IOMMU lock held. */
241static void vtd_reset_iotlb_locked(IntelIOMMUState *s)
b5a280c0
LT
242{
243 assert(s->iotlb);
244 g_hash_table_remove_all(s->iotlb);
245}
246
1d9efa73
PX
247static void vtd_reset_iotlb(IntelIOMMUState *s)
248{
249 vtd_iommu_lock(s);
250 vtd_reset_iotlb_locked(s);
251 vtd_iommu_unlock(s);
252}
253
06aba4ca
PX
254static void vtd_reset_caches(IntelIOMMUState *s)
255{
256 vtd_iommu_lock(s);
257 vtd_reset_iotlb_locked(s);
258 vtd_reset_context_cache_locked(s);
259 vtd_iommu_unlock(s);
260}
261
bacabb0a 262static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
d66b969b
JW
263 uint32_t level)
264{
265 return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) |
266 ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT);
267}
268
269static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
270{
271 return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
272}
273
1d9efa73 274/* Must be called with IOMMU lock held */
b5a280c0
LT
275static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
276 hwaddr addr)
277{
d66b969b 278 VTDIOTLBEntry *entry;
b5a280c0 279 uint64_t key;
d66b969b
JW
280 int level;
281
282 for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
283 key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
284 source_id, level);
285 entry = g_hash_table_lookup(s->iotlb, &key);
286 if (entry) {
287 goto out;
288 }
289 }
b5a280c0 290
d66b969b
JW
291out:
292 return entry;
b5a280c0
LT
293}
294
1d9efa73 295/* Must be with IOMMU lock held */
b5a280c0
LT
296static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
297 uint16_t domain_id, hwaddr addr, uint64_t slpte,
07f7b733 298 uint8_t access_flags, uint32_t level)
b5a280c0
LT
299{
300 VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
301 uint64_t *key = g_malloc(sizeof(*key));
d66b969b 302 uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
b5a280c0 303
6c441e1d 304 trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
b5a280c0 305 if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
6c441e1d 306 trace_vtd_iotlb_reset("iotlb exceeds size limit");
1d9efa73 307 vtd_reset_iotlb_locked(s);
b5a280c0
LT
308 }
309
310 entry->gfn = gfn;
311 entry->domain_id = domain_id;
312 entry->slpte = slpte;
07f7b733 313 entry->access_flags = access_flags;
d66b969b
JW
314 entry->mask = vtd_slpt_level_page_mask(level);
315 *key = vtd_get_iotlb_key(gfn, source_id, level);
b5a280c0
LT
316 g_hash_table_replace(s->iotlb, key, entry);
317}
318
1da12ec4
LT
319/* Given the reg addr of both the message data and address, generate an
320 * interrupt via MSI.
321 */
322static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
323 hwaddr mesg_data_reg)
324{
32946019 325 MSIMessage msi;
1da12ec4
LT
326
327 assert(mesg_data_reg < DMAR_REG_SIZE);
328 assert(mesg_addr_reg < DMAR_REG_SIZE);
329
32946019
RK
330 msi.address = vtd_get_long_raw(s, mesg_addr_reg);
331 msi.data = vtd_get_long_raw(s, mesg_data_reg);
1da12ec4 332
7feb51b7
PX
333 trace_vtd_irq_generate(msi.address, msi.data);
334
32946019 335 apic_get_class()->send_msi(&msi);
1da12ec4
LT
336}
337
338/* Generate a fault event to software via MSI if conditions are met.
339 * Notice that the value of FSTS_REG being passed to it should be the one
340 * before any update.
341 */
342static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
343{
344 if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
345 pre_fsts & VTD_FSTS_IQE) {
1376211f
PX
346 error_report_once("There are previous interrupt conditions "
347 "to be serviced by software, fault event "
348 "is not generated");
1da12ec4
LT
349 return;
350 }
351 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
352 if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
1376211f 353 error_report_once("Interrupt Mask set, irq is not generated");
1da12ec4
LT
354 } else {
355 vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
356 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
357 }
358}
359
360/* Check if the Fault (F) field of the Fault Recording Register referenced by
361 * @index is Set.
362 */
363static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
364{
365 /* Each reg is 128-bit */
366 hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
367 addr += 8; /* Access the high 64-bit half */
368
369 assert(index < DMAR_FRCD_REG_NR);
370
371 return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
372}
373
374/* Update the PPF field of Fault Status Register.
375 * Should be called whenever change the F field of any fault recording
376 * registers.
377 */
378static void vtd_update_fsts_ppf(IntelIOMMUState *s)
379{
380 uint32_t i;
381 uint32_t ppf_mask = 0;
382
383 for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
384 if (vtd_is_frcd_set(s, i)) {
385 ppf_mask = VTD_FSTS_PPF;
386 break;
387 }
388 }
389 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
7feb51b7 390 trace_vtd_fsts_ppf(!!ppf_mask);
1da12ec4
LT
391}
392
393static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
394{
395 /* Each reg is 128-bit */
396 hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
397 addr += 8; /* Access the high 64-bit half */
398
399 assert(index < DMAR_FRCD_REG_NR);
400
401 vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
402 vtd_update_fsts_ppf(s);
403}
404
405/* Must not update F field now, should be done later */
406static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
407 uint16_t source_id, hwaddr addr,
408 VTDFaultReason fault, bool is_write)
409{
410 uint64_t hi = 0, lo;
411 hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
412
413 assert(index < DMAR_FRCD_REG_NR);
414
415 lo = VTD_FRCD_FI(addr);
416 hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
417 if (!is_write) {
418 hi |= VTD_FRCD_T;
419 }
420 vtd_set_quad_raw(s, frcd_reg_addr, lo);
421 vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
7feb51b7
PX
422
423 trace_vtd_frr_new(index, hi, lo);
1da12ec4
LT
424}
425
426/* Try to collapse multiple pending faults from the same requester */
427static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
428{
429 uint32_t i;
430 uint64_t frcd_reg;
431 hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
432
433 for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
434 frcd_reg = vtd_get_quad_raw(s, addr);
1da12ec4
LT
435 if ((frcd_reg & VTD_FRCD_F) &&
436 ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
437 return true;
438 }
439 addr += 16; /* 128-bit for each */
440 }
441 return false;
442}
443
444/* Log and report an DMAR (address translation) fault to software */
445static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
446 hwaddr addr, VTDFaultReason fault,
447 bool is_write)
448{
449 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
450
451 assert(fault < VTD_FR_MAX);
452
453 if (fault == VTD_FR_RESERVED_ERR) {
454 /* This is not a normal fault reason case. Drop it. */
455 return;
456 }
7feb51b7
PX
457
458 trace_vtd_dmar_fault(source_id, fault, addr, is_write);
459
1da12ec4 460 if (fsts_reg & VTD_FSTS_PFO) {
1376211f
PX
461 error_report_once("New fault is not recorded due to "
462 "Primary Fault Overflow");
1da12ec4
LT
463 return;
464 }
7feb51b7 465
1da12ec4 466 if (vtd_try_collapse_fault(s, source_id)) {
1376211f
PX
467 error_report_once("New fault is not recorded due to "
468 "compression of faults");
1da12ec4
LT
469 return;
470 }
7feb51b7 471
1da12ec4 472 if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
1376211f
PX
473 error_report_once("Next Fault Recording Reg is used, "
474 "new fault is not recorded, set PFO field");
1da12ec4
LT
475 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
476 return;
477 }
478
479 vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
480
481 if (fsts_reg & VTD_FSTS_PPF) {
1376211f
PX
482 error_report_once("There are pending faults already, "
483 "fault event is not generated");
1da12ec4
LT
484 vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
485 s->next_frcd_reg++;
486 if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
487 s->next_frcd_reg = 0;
488 }
489 } else {
490 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
491 VTD_FSTS_FRI(s->next_frcd_reg));
492 vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
493 s->next_frcd_reg++;
494 if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
495 s->next_frcd_reg = 0;
496 }
497 /* This case actually cause the PPF to be Set.
498 * So generate fault event (interrupt).
499 */
500 vtd_generate_fault_event(s, fsts_reg);
501 }
502}
503
ed7b8fbc
LT
504/* Handle Invalidation Queue Errors of queued invalidation interface error
505 * conditions.
506 */
507static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
508{
509 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
510
511 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
512 vtd_generate_fault_event(s, fsts_reg);
513}
514
515/* Set the IWC field and try to generate an invalidation completion interrupt */
516static void vtd_generate_completion_event(IntelIOMMUState *s)
517{
ed7b8fbc 518 if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
bc535e59 519 trace_vtd_inv_desc_wait_irq("One pending, skip current");
ed7b8fbc
LT
520 return;
521 }
522 vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
523 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
524 if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
bc535e59
PX
525 trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
526 "new event not generated");
ed7b8fbc
LT
527 return;
528 } else {
529 /* Generate the interrupt event */
bc535e59 530 trace_vtd_inv_desc_wait_irq("Generating complete event");
ed7b8fbc
LT
531 vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
532 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
533 }
534}
535
fb43cf73
LY
536static inline bool vtd_root_entry_present(IntelIOMMUState *s,
537 VTDRootEntry *re,
538 uint8_t devfn)
1da12ec4 539{
fb43cf73
LY
540 if (s->root_scalable && devfn > UINT8_MAX / 2) {
541 return re->hi & VTD_ROOT_ENTRY_P;
542 }
543
544 return re->lo & VTD_ROOT_ENTRY_P;
1da12ec4
LT
545}
546
547static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
548 VTDRootEntry *re)
549{
550 dma_addr_t addr;
551
552 addr = s->root + index * sizeof(*re);
553 if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) {
fb43cf73 554 re->lo = 0;
1da12ec4
LT
555 return -VTD_FR_ROOT_TABLE_INV;
556 }
fb43cf73
LY
557 re->lo = le64_to_cpu(re->lo);
558 re->hi = le64_to_cpu(re->hi);
1da12ec4
LT
559 return 0;
560}
561
8f7d7161 562static inline bool vtd_ce_present(VTDContextEntry *context)
1da12ec4
LT
563{
564 return context->lo & VTD_CONTEXT_ENTRY_P;
565}
566
fb43cf73
LY
567static int vtd_get_context_entry_from_root(IntelIOMMUState *s,
568 VTDRootEntry *re,
569 uint8_t index,
1da12ec4
LT
570 VTDContextEntry *ce)
571{
fb43cf73 572 dma_addr_t addr, ce_size;
1da12ec4 573
6c441e1d 574 /* we have checked that root entry is present */
fb43cf73
LY
575 ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE :
576 VTD_CTX_ENTRY_LEGACY_SIZE;
577
578 if (s->root_scalable && index > UINT8_MAX / 2) {
579 index = index & (~VTD_DEVFN_CHECK_MASK);
580 addr = re->hi & VTD_ROOT_ENTRY_CTP;
581 } else {
582 addr = re->lo & VTD_ROOT_ENTRY_CTP;
583 }
584
585 addr = addr + index * ce_size;
586 if (dma_memory_read(&address_space_memory, addr, ce, ce_size)) {
1da12ec4
LT
587 return -VTD_FR_CONTEXT_TABLE_INV;
588 }
fb43cf73 589
1da12ec4
LT
590 ce->lo = le64_to_cpu(ce->lo);
591 ce->hi = le64_to_cpu(ce->hi);
fb43cf73
LY
592 if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) {
593 ce->val[2] = le64_to_cpu(ce->val[2]);
594 ce->val[3] = le64_to_cpu(ce->val[3]);
595 }
1da12ec4
LT
596 return 0;
597}
598
8f7d7161 599static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
1da12ec4
LT
600{
601 return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
602}
603
37f51384 604static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw)
1da12ec4 605{
37f51384 606 return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw);
1da12ec4
LT
607}
608
609/* Whether the pte indicates the address of the page frame */
610static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
611{
612 return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
613}
614
615/* Get the content of a spte located in @base_addr[@index] */
616static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
617{
618 uint64_t slpte;
619
620 assert(index < VTD_SL_PT_ENTRY_NR);
621
622 if (dma_memory_read(&address_space_memory,
623 base_addr + index * sizeof(slpte), &slpte,
624 sizeof(slpte))) {
625 slpte = (uint64_t)-1;
626 return slpte;
627 }
628 slpte = le64_to_cpu(slpte);
629 return slpte;
630}
631
6e905564
PX
632/* Given an iova and the level of paging structure, return the offset
633 * of current level.
1da12ec4 634 */
6e905564 635static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
1da12ec4 636{
6e905564 637 return (iova >> vtd_slpt_level_shift(level)) &
1da12ec4
LT
638 ((1ULL << VTD_SL_LEVEL_BITS) - 1);
639}
640
641/* Check Capability Register to see if the @level of page-table is supported */
642static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
643{
644 return VTD_CAP_SAGAW_MASK & s->cap &
645 (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
646}
647
fb43cf73
LY
648/* Return true if check passed, otherwise false */
649static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
650 VTDPASIDEntry *pe)
651{
652 switch (VTD_PE_GET_TYPE(pe)) {
653 case VTD_SM_PASID_ENTRY_FLT:
654 case VTD_SM_PASID_ENTRY_SLT:
655 case VTD_SM_PASID_ENTRY_NESTED:
656 break;
657 case VTD_SM_PASID_ENTRY_PT:
658 if (!x86_iommu->pt_supported) {
659 return false;
660 }
661 break;
662 default:
663 /* Unknwon type */
664 return false;
665 }
666 return true;
667}
668
669static int vtd_get_pasid_dire(dma_addr_t pasid_dir_base,
670 uint32_t pasid,
671 VTDPASIDDirEntry *pdire)
672{
673 uint32_t index;
674 dma_addr_t addr, entry_size;
675
676 index = VTD_PASID_DIR_INDEX(pasid);
677 entry_size = VTD_PASID_DIR_ENTRY_SIZE;
678 addr = pasid_dir_base + index * entry_size;
679 if (dma_memory_read(&address_space_memory, addr, pdire, entry_size)) {
680 return -VTD_FR_PASID_TABLE_INV;
681 }
682
683 return 0;
684}
685
686static int vtd_get_pasid_entry(IntelIOMMUState *s,
687 uint32_t pasid,
688 VTDPASIDDirEntry *pdire,
689 VTDPASIDEntry *pe)
690{
691 uint32_t index;
692 dma_addr_t addr, entry_size;
693 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
694
695 index = VTD_PASID_TABLE_INDEX(pasid);
696 entry_size = VTD_PASID_ENTRY_SIZE;
697 addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK;
698 addr = addr + index * entry_size;
699 if (dma_memory_read(&address_space_memory, addr, pe, entry_size)) {
700 return -VTD_FR_PASID_TABLE_INV;
701 }
702
703 /* Do translation type check */
704 if (!vtd_pe_type_check(x86_iommu, pe)) {
705 return -VTD_FR_PASID_TABLE_INV;
706 }
707
708 if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
709 return -VTD_FR_PASID_TABLE_INV;
710 }
711
712 return 0;
713}
714
715static int vtd_get_pasid_entry_from_pasid(IntelIOMMUState *s,
716 dma_addr_t pasid_dir_base,
717 uint32_t pasid,
718 VTDPASIDEntry *pe)
719{
720 int ret;
721 VTDPASIDDirEntry pdire;
722
723 ret = vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire);
724 if (ret) {
725 return ret;
726 }
727
728 ret = vtd_get_pasid_entry(s, pasid, &pdire, pe);
729 if (ret) {
730 return ret;
731 }
732
733 return ret;
734}
735
736static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
737 VTDContextEntry *ce,
738 VTDPASIDEntry *pe)
739{
740 uint32_t pasid;
741 dma_addr_t pasid_dir_base;
742 int ret = 0;
743
744 pasid = VTD_CE_GET_RID2PASID(ce);
745 pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
746 ret = vtd_get_pasid_entry_from_pasid(s, pasid_dir_base, pasid, pe);
747
748 return ret;
749}
750
751static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
752 VTDContextEntry *ce,
753 bool *pe_fpd_set)
754{
755 int ret;
756 uint32_t pasid;
757 dma_addr_t pasid_dir_base;
758 VTDPASIDDirEntry pdire;
759 VTDPASIDEntry pe;
760
761 pasid = VTD_CE_GET_RID2PASID(ce);
762 pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
763
764 ret = vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire);
765 if (ret) {
766 return ret;
767 }
768
769 if (pdire.val & VTD_PASID_DIR_FPD) {
770 *pe_fpd_set = true;
771 return 0;
772 }
773
774 ret = vtd_get_pasid_entry(s, pasid, &pdire, &pe);
775 if (ret) {
776 return ret;
777 }
778
779 if (pe.val[0] & VTD_PASID_ENTRY_FPD) {
780 *pe_fpd_set = true;
781 }
782
783 return 0;
784}
785
1da12ec4
LT
786/* Get the page-table level that hardware should use for the second-level
787 * page-table walk from the Address Width field of context-entry.
788 */
8f7d7161 789static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
1da12ec4
LT
790{
791 return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
792}
793
fb43cf73
LY
794static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
795 VTDContextEntry *ce)
796{
797 VTDPASIDEntry pe;
798
799 if (s->root_scalable) {
800 vtd_ce_get_rid2pasid_entry(s, ce, &pe);
801 return VTD_PE_GET_LEVEL(&pe);
802 }
803
804 return vtd_ce_get_level(ce);
805}
806
8f7d7161 807static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
1da12ec4
LT
808{
809 return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
810}
811
fb43cf73
LY
812static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
813 VTDContextEntry *ce)
814{
815 VTDPASIDEntry pe;
816
817 if (s->root_scalable) {
818 vtd_ce_get_rid2pasid_entry(s, ce, &pe);
819 return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
820 }
821
822 return vtd_ce_get_agaw(ce);
823}
824
127ff5c3
PX
825static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
826{
827 return ce->lo & VTD_CONTEXT_ENTRY_TT;
828}
829
fb43cf73 830/* Only for Legacy Mode. Return true if check passed, otherwise false */
f80c9874
PX
831static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
832 VTDContextEntry *ce)
833{
834 switch (vtd_ce_get_type(ce)) {
835 case VTD_CONTEXT_TT_MULTI_LEVEL:
836 /* Always supported */
837 break;
838 case VTD_CONTEXT_TT_DEV_IOTLB:
839 if (!x86_iommu->dt_supported) {
095955b2 840 error_report_once("%s: DT specified but not supported", __func__);
f80c9874
PX
841 return false;
842 }
843 break;
dbaabb25
PX
844 case VTD_CONTEXT_TT_PASS_THROUGH:
845 if (!x86_iommu->pt_supported) {
095955b2 846 error_report_once("%s: PT specified but not supported", __func__);
dbaabb25
PX
847 return false;
848 }
849 break;
f80c9874 850 default:
fb43cf73 851 /* Unknown type */
095955b2
PX
852 error_report_once("%s: unknown ce type: %"PRIu32, __func__,
853 vtd_ce_get_type(ce));
f80c9874
PX
854 return false;
855 }
856 return true;
857}
858
fb43cf73
LY
859static inline uint64_t vtd_iova_limit(IntelIOMMUState *s,
860 VTDContextEntry *ce, uint8_t aw)
f06a696d 861{
fb43cf73 862 uint32_t ce_agaw = vtd_get_iova_agaw(s, ce);
37f51384 863 return 1ULL << MIN(ce_agaw, aw);
f06a696d
PX
864}
865
866/* Return true if IOVA passes range check, otherwise false. */
fb43cf73
LY
867static inline bool vtd_iova_range_check(IntelIOMMUState *s,
868 uint64_t iova, VTDContextEntry *ce,
37f51384 869 uint8_t aw)
f06a696d
PX
870{
871 /*
872 * Check if @iova is above 2^X-1, where X is the minimum of MGAW
873 * in CAP_REG and AW in context-entry.
874 */
fb43cf73
LY
875 return !(iova & ~(vtd_iova_limit(s, ce, aw) - 1));
876}
877
878static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
879 VTDContextEntry *ce)
880{
881 VTDPASIDEntry pe;
882
883 if (s->root_scalable) {
884 vtd_ce_get_rid2pasid_entry(s, ce, &pe);
885 return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR;
886 }
887
888 return vtd_ce_get_slpt_base(ce);
f06a696d
PX
889}
890
92e5d85e
PS
891/*
892 * Rsvd field masks for spte:
893 * Index [1] to [4] 4k pages
894 * Index [5] to [8] large pages
895 */
896static uint64_t vtd_paging_entry_rsvd_field[9];
1da12ec4
LT
897
898static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
899{
900 if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) {
901 /* Maybe large page */
902 return slpte & vtd_paging_entry_rsvd_field[level + 4];
903 } else {
904 return slpte & vtd_paging_entry_rsvd_field[level];
905 }
906}
907
dbaabb25
PX
908/* Find the VTD address space associated with a given bus number */
909static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
910{
911 VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];
912 if (!vtd_bus) {
913 /*
914 * Iterate over the registered buses to find the one which
915 * currently hold this bus number, and update the bus_num
916 * lookup table:
917 */
918 GHashTableIter iter;
919
920 g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
921 while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
922 if (pci_bus_num(vtd_bus->bus) == bus_num) {
923 s->vtd_as_by_bus_num[bus_num] = vtd_bus;
924 return vtd_bus;
925 }
926 }
927 }
928 return vtd_bus;
929}
930
6e905564 931/* Given the @iova, get relevant @slptep. @slpte_level will be the last level
1da12ec4
LT
932 * of the translation, can be used for deciding the size of large page.
933 */
fb43cf73
LY
934static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce,
935 uint64_t iova, bool is_write,
6e905564 936 uint64_t *slptep, uint32_t *slpte_level,
37f51384 937 bool *reads, bool *writes, uint8_t aw_bits)
1da12ec4 938{
fb43cf73
LY
939 dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce);
940 uint32_t level = vtd_get_iova_level(s, ce);
1da12ec4
LT
941 uint32_t offset;
942 uint64_t slpte;
1da12ec4
LT
943 uint64_t access_right_check;
944
fb43cf73 945 if (!vtd_iova_range_check(s, iova, ce, aw_bits)) {
4e4abd11
PX
946 error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ")",
947 __func__, iova);
1da12ec4
LT
948 return -VTD_FR_ADDR_BEYOND_MGAW;
949 }
950
951 /* FIXME: what is the Atomics request here? */
952 access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
953
954 while (true) {
6e905564 955 offset = vtd_iova_level_offset(iova, level);
1da12ec4
LT
956 slpte = vtd_get_slpte(addr, offset);
957
958 if (slpte == (uint64_t)-1) {
4e4abd11
PX
959 error_report_once("%s: detected read error on DMAR slpte "
960 "(iova=0x%" PRIx64 ")", __func__, iova);
fb43cf73 961 if (level == vtd_get_iova_level(s, ce)) {
1da12ec4
LT
962 /* Invalid programming of context-entry */
963 return -VTD_FR_CONTEXT_ENTRY_INV;
964 } else {
965 return -VTD_FR_PAGING_ENTRY_INV;
966 }
967 }
968 *reads = (*reads) && (slpte & VTD_SL_R);
969 *writes = (*writes) && (slpte & VTD_SL_W);
970 if (!(slpte & access_right_check)) {
4e4abd11
PX
971 error_report_once("%s: detected slpte permission error "
972 "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
973 "slpte=0x%" PRIx64 ", write=%d)", __func__,
974 iova, level, slpte, is_write);
1da12ec4
LT
975 return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
976 }
977 if (vtd_slpte_nonzero_rsvd(slpte, level)) {
4e4abd11
PX
978 error_report_once("%s: detected splte reserve non-zero "
979 "iova=0x%" PRIx64 ", level=0x%" PRIx32
980 "slpte=0x%" PRIx64 ")", __func__, iova,
981 level, slpte);
1da12ec4
LT
982 return -VTD_FR_PAGING_ENTRY_RSVD;
983 }
984
985 if (vtd_is_last_slpte(slpte, level)) {
986 *slptep = slpte;
987 *slpte_level = level;
988 return 0;
989 }
37f51384 990 addr = vtd_get_slpte_addr(slpte, aw_bits);
1da12ec4
LT
991 level--;
992 }
993}
994
f06a696d
PX
995typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
996
fe215b0c
PX
997/**
998 * Constant information used during page walking
999 *
1000 * @hook_fn: hook func to be called when detected page
1001 * @private: private data to be passed into hook func
1002 * @notify_unmap: whether we should notify invalid entries
2f764fa8 1003 * @as: VT-d address space of the device
fe215b0c 1004 * @aw: maximum address width
d118c06e 1005 * @domain: domain ID of the page walk
fe215b0c
PX
1006 */
1007typedef struct {
2f764fa8 1008 VTDAddressSpace *as;
fe215b0c
PX
1009 vtd_page_walk_hook hook_fn;
1010 void *private;
1011 bool notify_unmap;
1012 uint8_t aw;
d118c06e 1013 uint16_t domain_id;
fe215b0c
PX
1014} vtd_page_walk_info;
1015
d118c06e 1016static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
36d2d52b 1017{
63b88968 1018 VTDAddressSpace *as = info->as;
fe215b0c
PX
1019 vtd_page_walk_hook hook_fn = info->hook_fn;
1020 void *private = info->private;
63b88968
PX
1021 DMAMap target = {
1022 .iova = entry->iova,
1023 .size = entry->addr_mask,
1024 .translated_addr = entry->translated_addr,
1025 .perm = entry->perm,
1026 };
1027 DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
1028
1029 if (entry->perm == IOMMU_NONE && !info->notify_unmap) {
1030 trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
1031 return 0;
1032 }
fe215b0c 1033
36d2d52b 1034 assert(hook_fn);
63b88968
PX
1035
1036 /* Update local IOVA mapped ranges */
1037 if (entry->perm) {
1038 if (mapped) {
1039 /* If it's exactly the same translation, skip */
1040 if (!memcmp(mapped, &target, sizeof(target))) {
1041 trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask,
1042 entry->translated_addr);
1043 return 0;
1044 } else {
1045 /*
1046 * Translation changed. Normally this should not
1047 * happen, but it can happen when with buggy guest
1048 * OSes. Note that there will be a small window that
1049 * we don't have map at all. But that's the best
1050 * effort we can do. The ideal way to emulate this is
1051 * atomically modify the PTE to follow what has
1052 * changed, but we can't. One example is that vfio
1053 * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
1054 * interface to modify a mapping (meanwhile it seems
1055 * meaningless to even provide one). Anyway, let's
1056 * mark this as a TODO in case one day we'll have
1057 * a better solution.
1058 */
1059 IOMMUAccessFlags cache_perm = entry->perm;
1060 int ret;
1061
1062 /* Emulate an UNMAP */
1063 entry->perm = IOMMU_NONE;
1064 trace_vtd_page_walk_one(info->domain_id,
1065 entry->iova,
1066 entry->translated_addr,
1067 entry->addr_mask,
1068 entry->perm);
1069 ret = hook_fn(entry, private);
1070 if (ret) {
1071 return ret;
1072 }
1073 /* Drop any existing mapping */
1074 iova_tree_remove(as->iova_tree, &target);
1075 /* Recover the correct permission */
1076 entry->perm = cache_perm;
1077 }
1078 }
1079 iova_tree_insert(as->iova_tree, &target);
1080 } else {
1081 if (!mapped) {
1082 /* Skip since we didn't map this range at all */
1083 trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
1084 return 0;
1085 }
1086 iova_tree_remove(as->iova_tree, &target);
1087 }
1088
d118c06e
PX
1089 trace_vtd_page_walk_one(info->domain_id, entry->iova,
1090 entry->translated_addr, entry->addr_mask,
1091 entry->perm);
36d2d52b
PX
1092 return hook_fn(entry, private);
1093}
1094
f06a696d
PX
1095/**
1096 * vtd_page_walk_level - walk over specific level for IOVA range
1097 *
1098 * @addr: base GPA addr to start the walk
1099 * @start: IOVA range start address
1100 * @end: IOVA range end address (start <= addr < end)
f06a696d
PX
1101 * @read: whether parent level has read permission
1102 * @write: whether parent level has write permission
fe215b0c 1103 * @info: constant information for the page walk
f06a696d
PX
1104 */
1105static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
fe215b0c
PX
1106 uint64_t end, uint32_t level, bool read,
1107 bool write, vtd_page_walk_info *info)
f06a696d
PX
1108{
1109 bool read_cur, write_cur, entry_valid;
1110 uint32_t offset;
1111 uint64_t slpte;
1112 uint64_t subpage_size, subpage_mask;
1113 IOMMUTLBEntry entry;
1114 uint64_t iova = start;
1115 uint64_t iova_next;
1116 int ret = 0;
1117
1118 trace_vtd_page_walk_level(addr, level, start, end);
1119
1120 subpage_size = 1ULL << vtd_slpt_level_shift(level);
1121 subpage_mask = vtd_slpt_level_page_mask(level);
1122
1123 while (iova < end) {
1124 iova_next = (iova & subpage_mask) + subpage_size;
1125
1126 offset = vtd_iova_level_offset(iova, level);
1127 slpte = vtd_get_slpte(addr, offset);
1128
1129 if (slpte == (uint64_t)-1) {
1130 trace_vtd_page_walk_skip_read(iova, iova_next);
1131 goto next;
1132 }
1133
1134 if (vtd_slpte_nonzero_rsvd(slpte, level)) {
1135 trace_vtd_page_walk_skip_reserve(iova, iova_next);
1136 goto next;
1137 }
1138
1139 /* Permissions are stacked with parents' */
1140 read_cur = read && (slpte & VTD_SL_R);
1141 write_cur = write && (slpte & VTD_SL_W);
1142
1143 /*
1144 * As long as we have either read/write permission, this is a
1145 * valid entry. The rule works for both page entries and page
1146 * table entries.
1147 */
1148 entry_valid = read_cur | write_cur;
1149
63b88968
PX
1150 if (!vtd_is_last_slpte(slpte, level) && entry_valid) {
1151 /*
1152 * This is a valid PDE (or even bigger than PDE). We need
1153 * to walk one further level.
1154 */
fe215b0c
PX
1155 ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw),
1156 iova, MIN(iova_next, end), level - 1,
1157 read_cur, write_cur, info);
63b88968
PX
1158 } else {
1159 /*
1160 * This means we are either:
1161 *
1162 * (1) the real page entry (either 4K page, or huge page)
1163 * (2) the whole range is invalid
1164 *
1165 * In either case, we send an IOTLB notification down.
1166 */
1167 entry.target_as = &address_space_memory;
1168 entry.iova = iova & subpage_mask;
1169 entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
1170 entry.addr_mask = ~subpage_mask;
1171 /* NOTE: this is only meaningful if entry_valid == true */
1172 entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
1173 ret = vtd_page_walk_one(&entry, info);
1174 }
1175
1176 if (ret < 0) {
1177 return ret;
f06a696d
PX
1178 }
1179
1180next:
1181 iova = iova_next;
1182 }
1183
1184 return 0;
1185}
1186
1187/**
1188 * vtd_page_walk - walk specific IOVA range, and call the hook
1189 *
fb43cf73 1190 * @s: intel iommu state
f06a696d
PX
1191 * @ce: context entry to walk upon
1192 * @start: IOVA address to start the walk
1193 * @end: IOVA range end address (start <= addr < end)
fe215b0c 1194 * @info: page walking information struct
f06a696d 1195 */
fb43cf73
LY
1196static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce,
1197 uint64_t start, uint64_t end,
fe215b0c 1198 vtd_page_walk_info *info)
f06a696d 1199{
fb43cf73
LY
1200 dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce);
1201 uint32_t level = vtd_get_iova_level(s, ce);
f06a696d 1202
fb43cf73 1203 if (!vtd_iova_range_check(s, start, ce, info->aw)) {
f06a696d
PX
1204 return -VTD_FR_ADDR_BEYOND_MGAW;
1205 }
1206
fb43cf73 1207 if (!vtd_iova_range_check(s, end, ce, info->aw)) {
f06a696d 1208 /* Fix end so that it reaches the maximum */
fb43cf73 1209 end = vtd_iova_limit(s, ce, info->aw);
f06a696d
PX
1210 }
1211
fe215b0c 1212 return vtd_page_walk_level(addr, start, end, level, true, true, info);
f06a696d
PX
1213}
1214
fb43cf73
LY
1215static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s,
1216 VTDRootEntry *re)
1217{
1218 /* Legacy Mode reserved bits check */
1219 if (!s->root_scalable &&
1220 (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1221 goto rsvd_err;
1222
1223 /* Scalable Mode reserved bits check */
1224 if (s->root_scalable &&
1225 ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) ||
1226 (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
1227 goto rsvd_err;
1228
1229 return 0;
1230
1231rsvd_err:
1232 error_report_once("%s: invalid root entry: hi=0x%"PRIx64
1233 ", lo=0x%"PRIx64,
1234 __func__, re->hi, re->lo);
1235 return -VTD_FR_ROOT_ENTRY_RSVD;
1236}
1237
1238static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,
1239 VTDContextEntry *ce)
1240{
1241 if (!s->root_scalable &&
1242 (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI ||
1243 ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
1244 error_report_once("%s: invalid context entry: hi=%"PRIx64
1245 ", lo=%"PRIx64" (reserved nonzero)",
1246 __func__, ce->hi, ce->lo);
1247 return -VTD_FR_CONTEXT_ENTRY_RSVD;
1248 }
1249
1250 if (s->root_scalable &&
1251 (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) ||
1252 ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 ||
1253 ce->val[2] ||
1254 ce->val[3])) {
1255 error_report_once("%s: invalid context entry: val[3]=%"PRIx64
1256 ", val[2]=%"PRIx64
1257 ", val[1]=%"PRIx64
1258 ", val[0]=%"PRIx64" (reserved nonzero)",
1259 __func__, ce->val[3], ce->val[2],
1260 ce->val[1], ce->val[0]);
1261 return -VTD_FR_CONTEXT_ENTRY_RSVD;
1262 }
1263
1264 return 0;
1265}
1266
1267static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
1268 VTDContextEntry *ce)
1269{
1270 VTDPASIDEntry pe;
1271
1272 /*
1273 * Make sure in Scalable Mode, a present context entry
1274 * has valid rid2pasid setting, which includes valid
1275 * rid2pasid field and corresponding pasid entry setting
1276 */
1277 return vtd_ce_get_rid2pasid_entry(s, ce, &pe);
1278}
1279
1da12ec4
LT
1280/* Map a device to its corresponding domain (context-entry) */
1281static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
1282 uint8_t devfn, VTDContextEntry *ce)
1283{
1284 VTDRootEntry re;
1285 int ret_fr;
f80c9874 1286 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
1da12ec4
LT
1287
1288 ret_fr = vtd_get_root_entry(s, bus_num, &re);
1289 if (ret_fr) {
1290 return ret_fr;
1291 }
1292
fb43cf73 1293 if (!vtd_root_entry_present(s, &re, devfn)) {
6c441e1d
PX
1294 /* Not error - it's okay we don't have root entry. */
1295 trace_vtd_re_not_present(bus_num);
1da12ec4 1296 return -VTD_FR_ROOT_ENTRY_P;
f80c9874
PX
1297 }
1298
fb43cf73
LY
1299 ret_fr = vtd_root_entry_rsvd_bits_check(s, &re);
1300 if (ret_fr) {
1301 return ret_fr;
1da12ec4
LT
1302 }
1303
fb43cf73 1304 ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce);
1da12ec4
LT
1305 if (ret_fr) {
1306 return ret_fr;
1307 }
1308
8f7d7161 1309 if (!vtd_ce_present(ce)) {
6c441e1d
PX
1310 /* Not error - it's okay we don't have context entry. */
1311 trace_vtd_ce_not_present(bus_num, devfn);
1da12ec4 1312 return -VTD_FR_CONTEXT_ENTRY_P;
f80c9874
PX
1313 }
1314
fb43cf73
LY
1315 ret_fr = vtd_context_entry_rsvd_bits_check(s, ce);
1316 if (ret_fr) {
1317 return ret_fr;
1da12ec4 1318 }
f80c9874 1319
1da12ec4 1320 /* Check if the programming of context-entry is valid */
fb43cf73
LY
1321 if (!s->root_scalable &&
1322 !vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
095955b2
PX
1323 error_report_once("%s: invalid context entry: hi=%"PRIx64
1324 ", lo=%"PRIx64" (level %d not supported)",
fb43cf73
LY
1325 __func__, ce->hi, ce->lo,
1326 vtd_ce_get_level(ce));
1da12ec4 1327 return -VTD_FR_CONTEXT_ENTRY_INV;
1da12ec4 1328 }
f80c9874 1329
fb43cf73
LY
1330 if (!s->root_scalable) {
1331 /* Do translation type check */
1332 if (!vtd_ce_type_check(x86_iommu, ce)) {
1333 /* Errors dumped in vtd_ce_type_check() */
1334 return -VTD_FR_CONTEXT_ENTRY_INV;
1335 }
1336 } else {
1337 /*
1338 * Check if the programming of context-entry.rid2pasid
1339 * and corresponding pasid setting is valid, and thus
1340 * avoids to check pasid entry fetching result in future
1341 * helper function calling.
1342 */
1343 ret_fr = vtd_ce_rid2pasid_check(s, ce);
1344 if (ret_fr) {
1345 return ret_fr;
1346 }
f80c9874
PX
1347 }
1348
1da12ec4
LT
1349 return 0;
1350}
1351
63b88968
PX
1352static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry,
1353 void *private)
1354{
cb1efcf4 1355 memory_region_notify_iommu((IOMMUMemoryRegion *)private, 0, *entry);
63b88968
PX
1356 return 0;
1357}
1358
fb43cf73
LY
1359static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
1360 VTDContextEntry *ce)
1361{
1362 VTDPASIDEntry pe;
1363
1364 if (s->root_scalable) {
1365 vtd_ce_get_rid2pasid_entry(s, ce, &pe);
1366 return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
1367 }
1368
1369 return VTD_CONTEXT_ENTRY_DID(ce->hi);
1370}
1371
63b88968
PX
1372static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
1373 VTDContextEntry *ce,
1374 hwaddr addr, hwaddr size)
1375{
1376 IntelIOMMUState *s = vtd_as->iommu_state;
1377 vtd_page_walk_info info = {
1378 .hook_fn = vtd_sync_shadow_page_hook,
1379 .private = (void *)&vtd_as->iommu,
1380 .notify_unmap = true,
1381 .aw = s->aw_bits,
1382 .as = vtd_as,
fb43cf73 1383 .domain_id = vtd_get_domain_id(s, ce),
63b88968 1384 };
63b88968 1385
fb43cf73 1386 return vtd_page_walk(s, ce, addr, addr + size, &info);
63b88968
PX
1387}
1388
1389static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
1390{
95ecd3df
PX
1391 int ret;
1392 VTDContextEntry ce;
c28b535d 1393 IOMMUNotifier *n;
95ecd3df
PX
1394
1395 ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
1396 pci_bus_num(vtd_as->bus),
1397 vtd_as->devfn, &ce);
1398 if (ret) {
c28b535d
PX
1399 if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
1400 /*
1401 * It's a valid scenario to have a context entry that is
1402 * not present. For example, when a device is removed
1403 * from an existing domain then the context entry will be
1404 * zeroed by the guest before it was put into another
1405 * domain. When this happens, instead of synchronizing
1406 * the shadow pages we should invalidate all existing
1407 * mappings and notify the backends.
1408 */
1409 IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
1410 vtd_address_space_unmap(vtd_as, n);
1411 }
1412 ret = 0;
1413 }
95ecd3df
PX
1414 return ret;
1415 }
1416
1417 return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX);
63b88968
PX
1418}
1419
dbaabb25 1420/*
fb43cf73
LY
1421 * Check if specific device is configed to bypass address
1422 * translation for DMA requests. In Scalable Mode, bypass
1423 * 1st-level translation or 2nd-level translation, it depends
1424 * on PGTT setting.
dbaabb25 1425 */
fb43cf73 1426static bool vtd_dev_pt_enabled(VTDAddressSpace *as)
dbaabb25
PX
1427{
1428 IntelIOMMUState *s;
1429 VTDContextEntry ce;
fb43cf73 1430 VTDPASIDEntry pe;
dbaabb25
PX
1431 int ret;
1432
fb43cf73 1433 assert(as);
dbaabb25 1434
fb43cf73 1435 s = as->iommu_state;
dbaabb25
PX
1436 ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus),
1437 as->devfn, &ce);
1438 if (ret) {
dbaabb25
PX
1439 /*
1440 * Possibly failed to parse the context entry for some reason
1441 * (e.g., during init, or any guest configuration errors on
1442 * context entries). We should assume PT not enabled for
1443 * safety.
1444 */
1445 return false;
1446 }
1447
fb43cf73
LY
1448 if (s->root_scalable) {
1449 ret = vtd_ce_get_rid2pasid_entry(s, &ce, &pe);
1450 if (ret) {
1451 error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRId32,
1452 __func__, ret);
1453 return false;
1454 }
1455 return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
1456 }
1457
1458 return (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH);
dbaabb25
PX
1459}
1460
1461/* Return whether the device is using IOMMU translation. */
1462static bool vtd_switch_address_space(VTDAddressSpace *as)
1463{
1464 bool use_iommu;
66a4a031
PX
1465 /* Whether we need to take the BQL on our own */
1466 bool take_bql = !qemu_mutex_iothread_locked();
dbaabb25
PX
1467
1468 assert(as);
1469
2a078b10 1470 use_iommu = as->iommu_state->dmar_enabled && !vtd_dev_pt_enabled(as);
dbaabb25
PX
1471
1472 trace_vtd_switch_address_space(pci_bus_num(as->bus),
1473 VTD_PCI_SLOT(as->devfn),
1474 VTD_PCI_FUNC(as->devfn),
1475 use_iommu);
1476
66a4a031
PX
1477 /*
1478 * It's possible that we reach here without BQL, e.g., when called
1479 * from vtd_pt_enable_fast_path(). However the memory APIs need
1480 * it. We'd better make sure we have had it already, or, take it.
1481 */
1482 if (take_bql) {
1483 qemu_mutex_lock_iothread();
1484 }
1485
dbaabb25
PX
1486 /* Turn off first then on the other */
1487 if (use_iommu) {
1488 memory_region_set_enabled(&as->sys_alias, false);
3df9d748 1489 memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
dbaabb25 1490 } else {
3df9d748 1491 memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
dbaabb25
PX
1492 memory_region_set_enabled(&as->sys_alias, true);
1493 }
1494
66a4a031
PX
1495 if (take_bql) {
1496 qemu_mutex_unlock_iothread();
1497 }
1498
dbaabb25
PX
1499 return use_iommu;
1500}
1501
1502static void vtd_switch_address_space_all(IntelIOMMUState *s)
1503{
1504 GHashTableIter iter;
1505 VTDBus *vtd_bus;
1506 int i;
1507
1508 g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
1509 while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
bf33cc75 1510 for (i = 0; i < PCI_DEVFN_MAX; i++) {
dbaabb25
PX
1511 if (!vtd_bus->dev_as[i]) {
1512 continue;
1513 }
1514 vtd_switch_address_space(vtd_bus->dev_as[i]);
1515 }
1516 }
1517}
1518
1da12ec4
LT
1519static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
1520{
1521 return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
1522}
1523
1524static const bool vtd_qualified_faults[] = {
1525 [VTD_FR_RESERVED] = false,
1526 [VTD_FR_ROOT_ENTRY_P] = false,
1527 [VTD_FR_CONTEXT_ENTRY_P] = true,
1528 [VTD_FR_CONTEXT_ENTRY_INV] = true,
1529 [VTD_FR_ADDR_BEYOND_MGAW] = true,
1530 [VTD_FR_WRITE] = true,
1531 [VTD_FR_READ] = true,
1532 [VTD_FR_PAGING_ENTRY_INV] = true,
1533 [VTD_FR_ROOT_TABLE_INV] = false,
1534 [VTD_FR_CONTEXT_TABLE_INV] = false,
1535 [VTD_FR_ROOT_ENTRY_RSVD] = false,
1536 [VTD_FR_PAGING_ENTRY_RSVD] = true,
1537 [VTD_FR_CONTEXT_ENTRY_TT] = true,
fb43cf73 1538 [VTD_FR_PASID_TABLE_INV] = false,
1da12ec4
LT
1539 [VTD_FR_RESERVED_ERR] = false,
1540 [VTD_FR_MAX] = false,
1541};
1542
1543/* To see if a fault condition is "qualified", which is reported to software
1544 * only if the FPD field in the context-entry used to process the faulting
1545 * request is 0.
1546 */
1547static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
1548{
1549 return vtd_qualified_faults[fault];
1550}
1551
1552static inline bool vtd_is_interrupt_addr(hwaddr addr)
1553{
1554 return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
1555}
1556
dbaabb25
PX
1557static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1558{
1559 VTDBus *vtd_bus;
1560 VTDAddressSpace *vtd_as;
1561 bool success = false;
1562
1563 vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id));
1564 if (!vtd_bus) {
1565 goto out;
1566 }
1567
1568 vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)];
1569 if (!vtd_as) {
1570 goto out;
1571 }
1572
1573 if (vtd_switch_address_space(vtd_as) == false) {
1574 /* We switched off IOMMU region successfully. */
1575 success = true;
1576 }
1577
1578out:
1579 trace_vtd_pt_enable_fast_path(source_id, success);
1580}
1581
1da12ec4
LT
1582/* Map dev to context-entry then do a paging-structures walk to do a iommu
1583 * translation.
79e2b9ae
PB
1584 *
1585 * Called from RCU critical section.
1586 *
1da12ec4
LT
1587 * @bus_num: The bus number
1588 * @devfn: The devfn, which is the combined of device and function number
1589 * @is_write: The access is a write operation
1590 * @entry: IOMMUTLBEntry that contain the addr to be translated and result
b9313021
PX
1591 *
1592 * Returns true if translation is successful, otherwise false.
1da12ec4 1593 */
b9313021 1594static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
1da12ec4
LT
1595 uint8_t devfn, hwaddr addr, bool is_write,
1596 IOMMUTLBEntry *entry)
1597{
d92fa2dc 1598 IntelIOMMUState *s = vtd_as->iommu_state;
1da12ec4 1599 VTDContextEntry ce;
7df953bd 1600 uint8_t bus_num = pci_bus_num(bus);
1d9efa73 1601 VTDContextCacheEntry *cc_entry;
d66b969b 1602 uint64_t slpte, page_mask;
1da12ec4
LT
1603 uint32_t level;
1604 uint16_t source_id = vtd_make_source_id(bus_num, devfn);
1605 int ret_fr;
1606 bool is_fpd_set = false;
1607 bool reads = true;
1608 bool writes = true;
07f7b733 1609 uint8_t access_flags;
b5a280c0 1610 VTDIOTLBEntry *iotlb_entry;
1da12ec4 1611
046ab7e9
PX
1612 /*
1613 * We have standalone memory region for interrupt addresses, we
1614 * should never receive translation requests in this region.
1615 */
1616 assert(!vtd_is_interrupt_addr(addr));
1617
1d9efa73
PX
1618 vtd_iommu_lock(s);
1619
1620 cc_entry = &vtd_as->context_cache_entry;
1621
b5a280c0
LT
1622 /* Try to fetch slpte form IOTLB */
1623 iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
1624 if (iotlb_entry) {
6c441e1d
PX
1625 trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
1626 iotlb_entry->domain_id);
b5a280c0 1627 slpte = iotlb_entry->slpte;
07f7b733 1628 access_flags = iotlb_entry->access_flags;
d66b969b 1629 page_mask = iotlb_entry->mask;
b5a280c0
LT
1630 goto out;
1631 }
b9313021 1632
d92fa2dc
LT
1633 /* Try to fetch context-entry from cache first */
1634 if (cc_entry->context_cache_gen == s->context_cache_gen) {
6c441e1d
PX
1635 trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
1636 cc_entry->context_entry.lo,
1637 cc_entry->context_cache_gen);
d92fa2dc
LT
1638 ce = cc_entry->context_entry;
1639 is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
fb43cf73
LY
1640 if (!is_fpd_set && s->root_scalable) {
1641 ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set);
1642 VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write);
1643 }
d92fa2dc
LT
1644 } else {
1645 ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
1646 is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
fb43cf73
LY
1647 if (!ret_fr && !is_fpd_set && s->root_scalable) {
1648 ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set);
1da12ec4 1649 }
fb43cf73 1650 VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write);
d92fa2dc 1651 /* Update context-cache */
6c441e1d
PX
1652 trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
1653 cc_entry->context_cache_gen,
1654 s->context_cache_gen);
d92fa2dc
LT
1655 cc_entry->context_entry = ce;
1656 cc_entry->context_cache_gen = s->context_cache_gen;
1da12ec4
LT
1657 }
1658
dbaabb25
PX
1659 /*
1660 * We don't need to translate for pass-through context entries.
1661 * Also, let's ignore IOTLB caching as well for PT devices.
1662 */
1663 if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
892721d9 1664 entry->iova = addr & VTD_PAGE_MASK_4K;
dbaabb25 1665 entry->translated_addr = entry->iova;
892721d9 1666 entry->addr_mask = ~VTD_PAGE_MASK_4K;
dbaabb25
PX
1667 entry->perm = IOMMU_RW;
1668 trace_vtd_translate_pt(source_id, entry->iova);
1669
1670 /*
1671 * When this happens, it means firstly caching-mode is not
1672 * enabled, and this is the first passthrough translation for
1673 * the device. Let's enable the fast path for passthrough.
1674 *
1675 * When passthrough is disabled again for the device, we can
1676 * capture it via the context entry invalidation, then the
1677 * IOMMU region can be swapped back.
1678 */
1679 vtd_pt_enable_fast_path(s, source_id);
1d9efa73 1680 vtd_iommu_unlock(s);
b9313021 1681 return true;
dbaabb25
PX
1682 }
1683
fb43cf73 1684 ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level,
37f51384 1685 &reads, &writes, s->aw_bits);
fb43cf73 1686 VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write);
1da12ec4 1687
d66b969b 1688 page_mask = vtd_slpt_level_page_mask(level);
07f7b733 1689 access_flags = IOMMU_ACCESS_FLAG(reads, writes);
fb43cf73 1690 vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce), addr, slpte,
07f7b733 1691 access_flags, level);
b5a280c0 1692out:
1d9efa73 1693 vtd_iommu_unlock(s);
d66b969b 1694 entry->iova = addr & page_mask;
37f51384 1695 entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
d66b969b 1696 entry->addr_mask = ~page_mask;
07f7b733 1697 entry->perm = access_flags;
b9313021
PX
1698 return true;
1699
1700error:
1d9efa73 1701 vtd_iommu_unlock(s);
b9313021
PX
1702 entry->iova = 0;
1703 entry->translated_addr = 0;
1704 entry->addr_mask = 0;
1705 entry->perm = IOMMU_NONE;
1706 return false;
1da12ec4
LT
1707}
1708
1709static void vtd_root_table_setup(IntelIOMMUState *s)
1710{
1711 s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
1712 s->root_extended = s->root & VTD_RTADDR_RTT;
37f51384 1713 s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
1da12ec4 1714
7feb51b7 1715 trace_vtd_reg_dmar_root(s->root, s->root_extended);
1da12ec4
LT
1716}
1717
02a2cbc8
PX
1718static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
1719 uint32_t index, uint32_t mask)
1720{
1721 x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
1722}
1723
a5861439
PX
1724static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1725{
1726 uint64_t value = 0;
1727 value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1728 s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
37f51384 1729 s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
28589311 1730 s->intr_eime = value & VTD_IRTA_EIME;
a5861439 1731
02a2cbc8
PX
1732 /* Notify global invalidation */
1733 vtd_iec_notify_all(s, true, 0, 0);
a5861439 1734
7feb51b7 1735 trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
a5861439
PX
1736}
1737
dd4d607e
PX
1738static void vtd_iommu_replay_all(IntelIOMMUState *s)
1739{
b4a4ba0d 1740 VTDAddressSpace *vtd_as;
dd4d607e 1741
b4a4ba0d 1742 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
63b88968 1743 vtd_sync_shadow_page_table(vtd_as);
dd4d607e
PX
1744 }
1745}
1746
d92fa2dc
LT
1747static void vtd_context_global_invalidate(IntelIOMMUState *s)
1748{
bc535e59 1749 trace_vtd_inv_desc_cc_global();
1d9efa73
PX
1750 /* Protects context cache */
1751 vtd_iommu_lock(s);
d92fa2dc
LT
1752 s->context_cache_gen++;
1753 if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
1d9efa73 1754 vtd_reset_context_cache_locked(s);
d92fa2dc 1755 }
1d9efa73 1756 vtd_iommu_unlock(s);
2cc9ddcc 1757 vtd_address_space_refresh_all(s);
dd4d607e
PX
1758 /*
1759 * From VT-d spec 6.5.2.1, a global context entry invalidation
1760 * should be followed by a IOTLB global invalidation, so we should
1761 * be safe even without this. Hoewever, let's replay the region as
1762 * well to be safer, and go back here when we need finer tunes for
1763 * VT-d emulation codes.
1764 */
1765 vtd_iommu_replay_all(s);
d92fa2dc
LT
1766}
1767
1768/* Do a context-cache device-selective invalidation.
1769 * @func_mask: FM field after shifting
1770 */
1771static void vtd_context_device_invalidate(IntelIOMMUState *s,
1772 uint16_t source_id,
1773 uint16_t func_mask)
1774{
1775 uint16_t mask;
7df953bd 1776 VTDBus *vtd_bus;
d92fa2dc 1777 VTDAddressSpace *vtd_as;
bc535e59 1778 uint8_t bus_n, devfn;
d92fa2dc
LT
1779 uint16_t devfn_it;
1780
bc535e59
PX
1781 trace_vtd_inv_desc_cc_devices(source_id, func_mask);
1782
d92fa2dc
LT
1783 switch (func_mask & 3) {
1784 case 0:
1785 mask = 0; /* No bits in the SID field masked */
1786 break;
1787 case 1:
1788 mask = 4; /* Mask bit 2 in the SID field */
1789 break;
1790 case 2:
1791 mask = 6; /* Mask bit 2:1 in the SID field */
1792 break;
1793 case 3:
1794 mask = 7; /* Mask bit 2:0 in the SID field */
1795 break;
1796 }
6cb99acc 1797 mask = ~mask;
bc535e59
PX
1798
1799 bus_n = VTD_SID_TO_BUS(source_id);
1800 vtd_bus = vtd_find_as_from_bus_num(s, bus_n);
7df953bd 1801 if (vtd_bus) {
d92fa2dc 1802 devfn = VTD_SID_TO_DEVFN(source_id);
bf33cc75 1803 for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
7df953bd 1804 vtd_as = vtd_bus->dev_as[devfn_it];
d92fa2dc 1805 if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
bc535e59
PX
1806 trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it),
1807 VTD_PCI_FUNC(devfn_it));
1d9efa73 1808 vtd_iommu_lock(s);
d92fa2dc 1809 vtd_as->context_cache_entry.context_cache_gen = 0;
1d9efa73 1810 vtd_iommu_unlock(s);
dbaabb25
PX
1811 /*
1812 * Do switch address space when needed, in case if the
1813 * device passthrough bit is switched.
1814 */
1815 vtd_switch_address_space(vtd_as);
dd4d607e
PX
1816 /*
1817 * So a device is moving out of (or moving into) a
63b88968 1818 * domain, resync the shadow page table.
dd4d607e
PX
1819 * This won't bring bad even if we have no such
1820 * notifier registered - the IOMMU notification
1821 * framework will skip MAP notifications if that
1822 * happened.
1823 */
63b88968 1824 vtd_sync_shadow_page_table(vtd_as);
d92fa2dc
LT
1825 }
1826 }
1827 }
1828}
1829
1da12ec4
LT
1830/* Context-cache invalidation
1831 * Returns the Context Actual Invalidation Granularity.
1832 * @val: the content of the CCMD_REG
1833 */
1834static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
1835{
1836 uint64_t caig;
1837 uint64_t type = val & VTD_CCMD_CIRG_MASK;
1838
1839 switch (type) {
d92fa2dc 1840 case VTD_CCMD_DOMAIN_INVL:
d92fa2dc 1841 /* Fall through */
1da12ec4 1842 case VTD_CCMD_GLOBAL_INVL:
1da12ec4 1843 caig = VTD_CCMD_GLOBAL_INVL_A;
d92fa2dc 1844 vtd_context_global_invalidate(s);
1da12ec4
LT
1845 break;
1846
1847 case VTD_CCMD_DEVICE_INVL:
1da12ec4 1848 caig = VTD_CCMD_DEVICE_INVL_A;
d92fa2dc 1849 vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
1da12ec4
LT
1850 break;
1851
1852 default:
1376211f
PX
1853 error_report_once("%s: invalid context: 0x%" PRIx64,
1854 __func__, val);
1da12ec4
LT
1855 caig = 0;
1856 }
1857 return caig;
1858}
1859
b5a280c0
LT
1860static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
1861{
7feb51b7 1862 trace_vtd_inv_desc_iotlb_global();
b5a280c0 1863 vtd_reset_iotlb(s);
dd4d607e 1864 vtd_iommu_replay_all(s);
b5a280c0
LT
1865}
1866
1867static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
1868{
dd4d607e
PX
1869 VTDContextEntry ce;
1870 VTDAddressSpace *vtd_as;
1871
7feb51b7
PX
1872 trace_vtd_inv_desc_iotlb_domain(domain_id);
1873
1d9efa73 1874 vtd_iommu_lock(s);
b5a280c0
LT
1875 g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
1876 &domain_id);
1d9efa73 1877 vtd_iommu_unlock(s);
dd4d607e 1878
b4a4ba0d 1879 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
dd4d607e
PX
1880 if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1881 vtd_as->devfn, &ce) &&
fb43cf73 1882 domain_id == vtd_get_domain_id(s, &ce)) {
63b88968 1883 vtd_sync_shadow_page_table(vtd_as);
dd4d607e
PX
1884 }
1885 }
1886}
1887
dd4d607e
PX
1888static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
1889 uint16_t domain_id, hwaddr addr,
1890 uint8_t am)
1891{
b4a4ba0d 1892 VTDAddressSpace *vtd_as;
dd4d607e
PX
1893 VTDContextEntry ce;
1894 int ret;
4f8a62a9 1895 hwaddr size = (1 << am) * VTD_PAGE_SIZE;
dd4d607e 1896
b4a4ba0d 1897 QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
dd4d607e
PX
1898 ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1899 vtd_as->devfn, &ce);
fb43cf73 1900 if (!ret && domain_id == vtd_get_domain_id(s, &ce)) {
4f8a62a9
PX
1901 if (vtd_as_has_map_notifier(vtd_as)) {
1902 /*
1903 * As long as we have MAP notifications registered in
1904 * any of our IOMMU notifiers, we need to sync the
1905 * shadow page table.
1906 */
63b88968 1907 vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
4f8a62a9
PX
1908 } else {
1909 /*
1910 * For UNMAP-only notifiers, we don't need to walk the
1911 * page tables. We just deliver the PSI down to
1912 * invalidate caches.
1913 */
1914 IOMMUTLBEntry entry = {
1915 .target_as = &address_space_memory,
1916 .iova = addr,
1917 .translated_addr = 0,
1918 .addr_mask = size - 1,
1919 .perm = IOMMU_NONE,
1920 };
cb1efcf4 1921 memory_region_notify_iommu(&vtd_as->iommu, 0, entry);
4f8a62a9 1922 }
dd4d607e
PX
1923 }
1924 }
b5a280c0
LT
1925}
1926
1927static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
1928 hwaddr addr, uint8_t am)
1929{
1930 VTDIOTLBPageInvInfo info;
1931
7feb51b7
PX
1932 trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
1933
b5a280c0
LT
1934 assert(am <= VTD_MAMV);
1935 info.domain_id = domain_id;
d66b969b 1936 info.addr = addr;
b5a280c0 1937 info.mask = ~((1 << am) - 1);
1d9efa73 1938 vtd_iommu_lock(s);
b5a280c0 1939 g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
1d9efa73 1940 vtd_iommu_unlock(s);
dd4d607e 1941 vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
b5a280c0
LT
1942}
1943
1da12ec4
LT
1944/* Flush IOTLB
1945 * Returns the IOTLB Actual Invalidation Granularity.
1946 * @val: the content of the IOTLB_REG
1947 */
1948static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
1949{
1950 uint64_t iaig;
1951 uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
b5a280c0
LT
1952 uint16_t domain_id;
1953 hwaddr addr;
1954 uint8_t am;
1da12ec4
LT
1955
1956 switch (type) {
1957 case VTD_TLB_GLOBAL_FLUSH:
1da12ec4 1958 iaig = VTD_TLB_GLOBAL_FLUSH_A;
b5a280c0 1959 vtd_iotlb_global_invalidate(s);
1da12ec4
LT
1960 break;
1961
1962 case VTD_TLB_DSI_FLUSH:
b5a280c0 1963 domain_id = VTD_TLB_DID(val);
1da12ec4 1964 iaig = VTD_TLB_DSI_FLUSH_A;
b5a280c0 1965 vtd_iotlb_domain_invalidate(s, domain_id);
1da12ec4
LT
1966 break;
1967
1968 case VTD_TLB_PSI_FLUSH:
b5a280c0
LT
1969 domain_id = VTD_TLB_DID(val);
1970 addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1971 am = VTD_IVA_AM(addr);
1972 addr = VTD_IVA_ADDR(addr);
b5a280c0 1973 if (am > VTD_MAMV) {
1376211f
PX
1974 error_report_once("%s: address mask overflow: 0x%" PRIx64,
1975 __func__, vtd_get_quad_raw(s, DMAR_IVA_REG));
b5a280c0
LT
1976 iaig = 0;
1977 break;
1978 }
1da12ec4 1979 iaig = VTD_TLB_PSI_FLUSH_A;
b5a280c0 1980 vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1da12ec4
LT
1981 break;
1982
1983 default:
1376211f
PX
1984 error_report_once("%s: invalid granularity: 0x%" PRIx64,
1985 __func__, val);
1da12ec4
LT
1986 iaig = 0;
1987 }
1988 return iaig;
1989}
1990
8991c460 1991static void vtd_fetch_inv_desc(IntelIOMMUState *s);
ed7b8fbc
LT
1992
1993static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1994{
1995 return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1996 (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1997}
1998
1999static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
2000{
2001 uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
2002
7feb51b7
PX
2003 trace_vtd_inv_qi_enable(en);
2004
ed7b8fbc 2005 if (en) {
37f51384 2006 s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
8991c460
LP
2007 /* 2^(x+8) entries */
2008 s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
2009 s->qi_enabled = true;
2010 trace_vtd_inv_qi_setup(s->iq, s->iq_size);
2011 /* Ok - report back to driver */
2012 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
2013
2014 if (s->iq_tail != 0) {
2015 /*
2016 * This is a spec violation but Windows guests are known to set up
2017 * Queued Invalidation this way so we allow the write and process
2018 * Invalidation Descriptors right away.
2019 */
2020 trace_vtd_warn_invalid_qi_tail(s->iq_tail);
2021 if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
2022 vtd_fetch_inv_desc(s);
2023 }
ed7b8fbc
LT
2024 }
2025 } else {
2026 if (vtd_queued_inv_disable_check(s)) {
2027 /* disable Queued Invalidation */
2028 vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
2029 s->iq_head = 0;
2030 s->qi_enabled = false;
2031 /* Ok - report back to driver */
2032 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
2033 } else {
4e4abd11
PX
2034 error_report_once("%s: detected improper state when disable QI "
2035 "(head=0x%x, tail=0x%x, last_type=%d)",
2036 __func__,
2037 s->iq_head, s->iq_tail, s->iq_last_desc_type);
ed7b8fbc
LT
2038 }
2039 }
2040}
2041
1da12ec4
LT
2042/* Set Root Table Pointer */
2043static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
2044{
1da12ec4
LT
2045 vtd_root_table_setup(s);
2046 /* Ok - report back to driver */
2047 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
2cc9ddcc
PX
2048 vtd_reset_caches(s);
2049 vtd_address_space_refresh_all(s);
1da12ec4
LT
2050}
2051
a5861439
PX
2052/* Set Interrupt Remap Table Pointer */
2053static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
2054{
a5861439
PX
2055 vtd_interrupt_remap_table_setup(s);
2056 /* Ok - report back to driver */
2057 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
2058}
2059
1da12ec4
LT
2060/* Handle Translation Enable/Disable */
2061static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
2062{
558e0024
PX
2063 if (s->dmar_enabled == en) {
2064 return;
2065 }
2066
7feb51b7 2067 trace_vtd_dmar_enable(en);
1da12ec4
LT
2068
2069 if (en) {
2070 s->dmar_enabled = true;
2071 /* Ok - report back to driver */
2072 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
2073 } else {
2074 s->dmar_enabled = false;
2075
2076 /* Clear the index of Fault Recording Register */
2077 s->next_frcd_reg = 0;
2078 /* Ok - report back to driver */
2079 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
2080 }
558e0024 2081
2cc9ddcc
PX
2082 vtd_reset_caches(s);
2083 vtd_address_space_refresh_all(s);
1da12ec4
LT
2084}
2085
80de52ba
PX
2086/* Handle Interrupt Remap Enable/Disable */
2087static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
2088{
7feb51b7 2089 trace_vtd_ir_enable(en);
80de52ba
PX
2090
2091 if (en) {
2092 s->intr_enabled = true;
2093 /* Ok - report back to driver */
2094 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
2095 } else {
2096 s->intr_enabled = false;
2097 /* Ok - report back to driver */
2098 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
2099 }
2100}
2101
1da12ec4
LT
2102/* Handle write to Global Command Register */
2103static void vtd_handle_gcmd_write(IntelIOMMUState *s)
2104{
2105 uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
2106 uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
2107 uint32_t changed = status ^ val;
2108
7feb51b7 2109 trace_vtd_reg_write_gcmd(status, val);
1da12ec4
LT
2110 if (changed & VTD_GCMD_TE) {
2111 /* Translation enable/disable */
2112 vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
2113 }
2114 if (val & VTD_GCMD_SRTP) {
2115 /* Set/update the root-table pointer */
2116 vtd_handle_gcmd_srtp(s);
2117 }
ed7b8fbc
LT
2118 if (changed & VTD_GCMD_QIE) {
2119 /* Queued Invalidation Enable */
2120 vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
2121 }
a5861439
PX
2122 if (val & VTD_GCMD_SIRTP) {
2123 /* Set/update the interrupt remapping root-table pointer */
2124 vtd_handle_gcmd_sirtp(s);
2125 }
80de52ba
PX
2126 if (changed & VTD_GCMD_IRE) {
2127 /* Interrupt remap enable/disable */
2128 vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
2129 }
1da12ec4
LT
2130}
2131
2132/* Handle write to Context Command Register */
2133static void vtd_handle_ccmd_write(IntelIOMMUState *s)
2134{
2135 uint64_t ret;
2136 uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
2137
2138 /* Context-cache invalidation request */
2139 if (val & VTD_CCMD_ICC) {
ed7b8fbc 2140 if (s->qi_enabled) {
1376211f
PX
2141 error_report_once("Queued Invalidation enabled, "
2142 "should not use register-based invalidation");
ed7b8fbc
LT
2143 return;
2144 }
1da12ec4
LT
2145 ret = vtd_context_cache_invalidate(s, val);
2146 /* Invalidation completed. Change something to show */
2147 vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
2148 ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
2149 ret);
1da12ec4
LT
2150 }
2151}
2152
2153/* Handle write to IOTLB Invalidation Register */
2154static void vtd_handle_iotlb_write(IntelIOMMUState *s)
2155{
2156 uint64_t ret;
2157 uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
2158
2159 /* IOTLB invalidation request */
2160 if (val & VTD_TLB_IVT) {
ed7b8fbc 2161 if (s->qi_enabled) {
1376211f
PX
2162 error_report_once("Queued Invalidation enabled, "
2163 "should not use register-based invalidation");
ed7b8fbc
LT
2164 return;
2165 }
1da12ec4
LT
2166 ret = vtd_iotlb_flush(s, val);
2167 /* Invalidation completed. Change something to show */
2168 vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
2169 ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
2170 VTD_TLB_FLUSH_GRANU_MASK_A, ret);
1da12ec4
LT
2171 }
2172}
2173
ed7b8fbc
LT
2174/* Fetch an Invalidation Descriptor from the Invalidation Queue */
2175static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
2176 VTDInvDesc *inv_desc)
2177{
2178 dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
2179 if (dma_memory_read(&address_space_memory, addr, inv_desc,
2180 sizeof(*inv_desc))) {
1376211f 2181 error_report_once("Read INV DESC failed");
ed7b8fbc
LT
2182 inv_desc->lo = 0;
2183 inv_desc->hi = 0;
ed7b8fbc
LT
2184 return false;
2185 }
2186 inv_desc->lo = le64_to_cpu(inv_desc->lo);
2187 inv_desc->hi = le64_to_cpu(inv_desc->hi);
2188 return true;
2189}
2190
2191static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2192{
2193 if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
2194 (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
095955b2
PX
2195 error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2196 " (reserved nonzero)", __func__, inv_desc->hi,
2197 inv_desc->lo);
ed7b8fbc
LT
2198 return false;
2199 }
2200 if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
2201 /* Status Write */
2202 uint32_t status_data = (uint32_t)(inv_desc->lo >>
2203 VTD_INV_DESC_WAIT_DATA_SHIFT);
2204
2205 assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
2206
2207 /* FIXME: need to be masked with HAW? */
2208 dma_addr_t status_addr = inv_desc->hi;
bc535e59 2209 trace_vtd_inv_desc_wait_sw(status_addr, status_data);
ed7b8fbc
LT
2210 status_data = cpu_to_le32(status_data);
2211 if (dma_memory_write(&address_space_memory, status_addr, &status_data,
2212 sizeof(status_data))) {
bc535e59 2213 trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
ed7b8fbc
LT
2214 return false;
2215 }
2216 } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
2217 /* Interrupt flag */
ed7b8fbc
LT
2218 vtd_generate_completion_event(s);
2219 } else {
095955b2
PX
2220 error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
2221 " (unknown type)", __func__, inv_desc->hi,
2222 inv_desc->lo);
ed7b8fbc
LT
2223 return false;
2224 }
2225 return true;
2226}
2227
d92fa2dc
LT
2228static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
2229 VTDInvDesc *inv_desc)
2230{
bc535e59
PX
2231 uint16_t sid, fmask;
2232
d92fa2dc 2233 if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
095955b2
PX
2234 error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2235 " (reserved nonzero)", __func__, inv_desc->hi,
2236 inv_desc->lo);
d92fa2dc
LT
2237 return false;
2238 }
2239 switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
2240 case VTD_INV_DESC_CC_DOMAIN:
bc535e59
PX
2241 trace_vtd_inv_desc_cc_domain(
2242 (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
d92fa2dc
LT
2243 /* Fall through */
2244 case VTD_INV_DESC_CC_GLOBAL:
d92fa2dc
LT
2245 vtd_context_global_invalidate(s);
2246 break;
2247
2248 case VTD_INV_DESC_CC_DEVICE:
bc535e59
PX
2249 sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
2250 fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
2251 vtd_context_device_invalidate(s, sid, fmask);
d92fa2dc
LT
2252 break;
2253
2254 default:
095955b2
PX
2255 error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
2256 " (invalid type)", __func__, inv_desc->hi,
2257 inv_desc->lo);
d92fa2dc
LT
2258 return false;
2259 }
2260 return true;
2261}
2262
b5a280c0
LT
2263static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
2264{
2265 uint16_t domain_id;
2266 uint8_t am;
2267 hwaddr addr;
2268
2269 if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
2270 (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
095955b2
PX
2271 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2272 ", lo=0x%"PRIx64" (reserved bits unzero)\n",
2273 __func__, inv_desc->hi, inv_desc->lo);
b5a280c0
LT
2274 return false;
2275 }
2276
2277 switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
2278 case VTD_INV_DESC_IOTLB_GLOBAL:
b5a280c0
LT
2279 vtd_iotlb_global_invalidate(s);
2280 break;
2281
2282 case VTD_INV_DESC_IOTLB_DOMAIN:
2283 domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
b5a280c0
LT
2284 vtd_iotlb_domain_invalidate(s, domain_id);
2285 break;
2286
2287 case VTD_INV_DESC_IOTLB_PAGE:
2288 domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
2289 addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
2290 am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
b5a280c0 2291 if (am > VTD_MAMV) {
095955b2
PX
2292 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2293 ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)\n",
2294 __func__, inv_desc->hi, inv_desc->lo,
2295 am, (unsigned)VTD_MAMV);
b5a280c0
LT
2296 return false;
2297 }
2298 vtd_iotlb_page_invalidate(s, domain_id, addr, am);
2299 break;
2300
2301 default:
095955b2
PX
2302 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2303 ", lo=0x%"PRIx64" (type mismatch: 0x%llx)\n",
2304 __func__, inv_desc->hi, inv_desc->lo,
2305 inv_desc->lo & VTD_INV_DESC_IOTLB_G);
b5a280c0
LT
2306 return false;
2307 }
2308 return true;
2309}
2310
02a2cbc8
PX
2311static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
2312 VTDInvDesc *inv_desc)
2313{
7feb51b7
PX
2314 trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
2315 inv_desc->iec.index,
2316 inv_desc->iec.index_mask);
02a2cbc8
PX
2317
2318 vtd_iec_notify_all(s, !inv_desc->iec.granularity,
2319 inv_desc->iec.index,
2320 inv_desc->iec.index_mask);
554f5e16
JW
2321 return true;
2322}
2323
2324static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
2325 VTDInvDesc *inv_desc)
2326{
2327 VTDAddressSpace *vtd_dev_as;
2328 IOMMUTLBEntry entry;
2329 struct VTDBus *vtd_bus;
2330 hwaddr addr;
2331 uint64_t sz;
2332 uint16_t sid;
2333 uint8_t devfn;
2334 bool size;
2335 uint8_t bus_num;
2336
2337 addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
2338 sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
2339 devfn = sid & 0xff;
2340 bus_num = sid >> 8;
2341 size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
2342
2343 if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
2344 (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
095955b2
PX
2345 error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64
2346 ", lo=%"PRIx64" (reserved nonzero)", __func__,
2347 inv_desc->hi, inv_desc->lo);
554f5e16
JW
2348 return false;
2349 }
2350
2351 vtd_bus = vtd_find_as_from_bus_num(s, bus_num);
2352 if (!vtd_bus) {
2353 goto done;
2354 }
2355
2356 vtd_dev_as = vtd_bus->dev_as[devfn];
2357 if (!vtd_dev_as) {
2358 goto done;
2359 }
2360
04eb6247
JW
2361 /* According to ATS spec table 2.4:
2362 * S = 0, bits 15:12 = xxxx range size: 4K
2363 * S = 1, bits 15:12 = xxx0 range size: 8K
2364 * S = 1, bits 15:12 = xx01 range size: 16K
2365 * S = 1, bits 15:12 = x011 range size: 32K
2366 * S = 1, bits 15:12 = 0111 range size: 64K
2367 * ...
2368 */
554f5e16 2369 if (size) {
04eb6247 2370 sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
554f5e16
JW
2371 addr &= ~(sz - 1);
2372 } else {
2373 sz = VTD_PAGE_SIZE;
2374 }
02a2cbc8 2375
554f5e16
JW
2376 entry.target_as = &vtd_dev_as->as;
2377 entry.addr_mask = sz - 1;
2378 entry.iova = addr;
2379 entry.perm = IOMMU_NONE;
2380 entry.translated_addr = 0;
cb1efcf4 2381 memory_region_notify_iommu(&vtd_dev_as->iommu, 0, entry);
554f5e16
JW
2382
2383done:
02a2cbc8
PX
2384 return true;
2385}
2386
ed7b8fbc
LT
2387static bool vtd_process_inv_desc(IntelIOMMUState *s)
2388{
2389 VTDInvDesc inv_desc;
2390 uint8_t desc_type;
2391
7feb51b7 2392 trace_vtd_inv_qi_head(s->iq_head);
ed7b8fbc
LT
2393 if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
2394 s->iq_last_desc_type = VTD_INV_DESC_NONE;
2395 return false;
2396 }
2397 desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
2398 /* FIXME: should update at first or at last? */
2399 s->iq_last_desc_type = desc_type;
2400
2401 switch (desc_type) {
2402 case VTD_INV_DESC_CC:
bc535e59 2403 trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
d92fa2dc
LT
2404 if (!vtd_process_context_cache_desc(s, &inv_desc)) {
2405 return false;
2406 }
ed7b8fbc
LT
2407 break;
2408
2409 case VTD_INV_DESC_IOTLB:
bc535e59 2410 trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
b5a280c0
LT
2411 if (!vtd_process_iotlb_desc(s, &inv_desc)) {
2412 return false;
2413 }
ed7b8fbc
LT
2414 break;
2415
2416 case VTD_INV_DESC_WAIT:
bc535e59 2417 trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
ed7b8fbc
LT
2418 if (!vtd_process_wait_desc(s, &inv_desc)) {
2419 return false;
2420 }
2421 break;
2422
b7910472 2423 case VTD_INV_DESC_IEC:
bc535e59 2424 trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
02a2cbc8
PX
2425 if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
2426 return false;
2427 }
b7910472
PX
2428 break;
2429
554f5e16 2430 case VTD_INV_DESC_DEVICE:
7feb51b7 2431 trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
554f5e16
JW
2432 if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
2433 return false;
2434 }
2435 break;
2436
ed7b8fbc 2437 default:
095955b2
PX
2438 error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64
2439 " (unknown type)", __func__, inv_desc.hi,
2440 inv_desc.lo);
ed7b8fbc
LT
2441 return false;
2442 }
2443 s->iq_head++;
2444 if (s->iq_head == s->iq_size) {
2445 s->iq_head = 0;
2446 }
2447 return true;
2448}
2449
2450/* Try to fetch and process more Invalidation Descriptors */
2451static void vtd_fetch_inv_desc(IntelIOMMUState *s)
2452{
7feb51b7
PX
2453 trace_vtd_inv_qi_fetch();
2454
ed7b8fbc
LT
2455 if (s->iq_tail >= s->iq_size) {
2456 /* Detects an invalid Tail pointer */
4e4abd11
PX
2457 error_report_once("%s: detected invalid QI tail "
2458 "(tail=0x%x, size=0x%x)",
2459 __func__, s->iq_tail, s->iq_size);
ed7b8fbc
LT
2460 vtd_handle_inv_queue_error(s);
2461 return;
2462 }
2463 while (s->iq_head != s->iq_tail) {
2464 if (!vtd_process_inv_desc(s)) {
2465 /* Invalidation Queue Errors */
2466 vtd_handle_inv_queue_error(s);
2467 break;
2468 }
2469 /* Must update the IQH_REG in time */
2470 vtd_set_quad_raw(s, DMAR_IQH_REG,
2471 (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
2472 VTD_IQH_QH_MASK);
2473 }
2474}
2475
2476/* Handle write to Invalidation Queue Tail Register */
2477static void vtd_handle_iqt_write(IntelIOMMUState *s)
2478{
2479 uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
2480
2481 s->iq_tail = VTD_IQT_QT(val);
7feb51b7
PX
2482 trace_vtd_inv_qi_tail(s->iq_tail);
2483
ed7b8fbc
LT
2484 if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
2485 /* Process Invalidation Queue here */
2486 vtd_fetch_inv_desc(s);
2487 }
2488}
2489
1da12ec4
LT
2490static void vtd_handle_fsts_write(IntelIOMMUState *s)
2491{
2492 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
2493 uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
2494 uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
2495
2496 if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
2497 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
7feb51b7 2498 trace_vtd_fsts_clear_ip();
1da12ec4 2499 }
ed7b8fbc
LT
2500 /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
2501 * Descriptors if there are any when Queued Invalidation is enabled?
2502 */
1da12ec4
LT
2503}
2504
2505static void vtd_handle_fectl_write(IntelIOMMUState *s)
2506{
2507 uint32_t fectl_reg;
2508 /* FIXME: when software clears the IM field, check the IP field. But do we
2509 * need to compare the old value and the new value to conclude that
2510 * software clears the IM field? Or just check if the IM field is zero?
2511 */
2512 fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
7feb51b7
PX
2513
2514 trace_vtd_reg_write_fectl(fectl_reg);
2515
1da12ec4
LT
2516 if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
2517 vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
2518 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
1da12ec4
LT
2519 }
2520}
2521
ed7b8fbc
LT
2522static void vtd_handle_ics_write(IntelIOMMUState *s)
2523{
2524 uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
2525 uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
2526
2527 if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
7feb51b7 2528 trace_vtd_reg_ics_clear_ip();
ed7b8fbc 2529 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
ed7b8fbc
LT
2530 }
2531}
2532
2533static void vtd_handle_iectl_write(IntelIOMMUState *s)
2534{
2535 uint32_t iectl_reg;
2536 /* FIXME: when software clears the IM field, check the IP field. But do we
2537 * need to compare the old value and the new value to conclude that
2538 * software clears the IM field? Or just check if the IM field is zero?
2539 */
2540 iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
7feb51b7
PX
2541
2542 trace_vtd_reg_write_iectl(iectl_reg);
2543
ed7b8fbc
LT
2544 if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
2545 vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
2546 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
ed7b8fbc
LT
2547 }
2548}
2549
1da12ec4
LT
2550static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
2551{
2552 IntelIOMMUState *s = opaque;
2553 uint64_t val;
2554
7feb51b7
PX
2555 trace_vtd_reg_read(addr, size);
2556
1da12ec4 2557 if (addr + size > DMAR_REG_SIZE) {
1376211f
PX
2558 error_report_once("%s: MMIO over range: addr=0x%" PRIx64
2559 " size=0x%u", __func__, addr, size);
1da12ec4
LT
2560 return (uint64_t)-1;
2561 }
2562
2563 switch (addr) {
2564 /* Root Table Address Register, 64-bit */
2565 case DMAR_RTADDR_REG:
2566 if (size == 4) {
2567 val = s->root & ((1ULL << 32) - 1);
2568 } else {
2569 val = s->root;
2570 }
2571 break;
2572
2573 case DMAR_RTADDR_REG_HI:
2574 assert(size == 4);
2575 val = s->root >> 32;
2576 break;
2577
ed7b8fbc
LT
2578 /* Invalidation Queue Address Register, 64-bit */
2579 case DMAR_IQA_REG:
2580 val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2581 if (size == 4) {
2582 val = val & ((1ULL << 32) - 1);
2583 }
2584 break;
2585
2586 case DMAR_IQA_REG_HI:
2587 assert(size == 4);
2588 val = s->iq >> 32;
2589 break;
2590
1da12ec4
LT
2591 default:
2592 if (size == 4) {
2593 val = vtd_get_long(s, addr);
2594 } else {
2595 val = vtd_get_quad(s, addr);
2596 }
2597 }
7feb51b7 2598
1da12ec4
LT
2599 return val;
2600}
2601
2602static void vtd_mem_write(void *opaque, hwaddr addr,
2603 uint64_t val, unsigned size)
2604{
2605 IntelIOMMUState *s = opaque;
2606
7feb51b7
PX
2607 trace_vtd_reg_write(addr, size, val);
2608
1da12ec4 2609 if (addr + size > DMAR_REG_SIZE) {
1376211f
PX
2610 error_report_once("%s: MMIO over range: addr=0x%" PRIx64
2611 " size=0x%u", __func__, addr, size);
1da12ec4
LT
2612 return;
2613 }
2614
2615 switch (addr) {
2616 /* Global Command Register, 32-bit */
2617 case DMAR_GCMD_REG:
1da12ec4
LT
2618 vtd_set_long(s, addr, val);
2619 vtd_handle_gcmd_write(s);
2620 break;
2621
2622 /* Context Command Register, 64-bit */
2623 case DMAR_CCMD_REG:
1da12ec4
LT
2624 if (size == 4) {
2625 vtd_set_long(s, addr, val);
2626 } else {
2627 vtd_set_quad(s, addr, val);
2628 vtd_handle_ccmd_write(s);
2629 }
2630 break;
2631
2632 case DMAR_CCMD_REG_HI:
1da12ec4
LT
2633 assert(size == 4);
2634 vtd_set_long(s, addr, val);
2635 vtd_handle_ccmd_write(s);
2636 break;
2637
2638 /* IOTLB Invalidation Register, 64-bit */
2639 case DMAR_IOTLB_REG:
1da12ec4
LT
2640 if (size == 4) {
2641 vtd_set_long(s, addr, val);
2642 } else {
2643 vtd_set_quad(s, addr, val);
2644 vtd_handle_iotlb_write(s);
2645 }
2646 break;
2647
2648 case DMAR_IOTLB_REG_HI:
1da12ec4
LT
2649 assert(size == 4);
2650 vtd_set_long(s, addr, val);
2651 vtd_handle_iotlb_write(s);
2652 break;
2653
b5a280c0
LT
2654 /* Invalidate Address Register, 64-bit */
2655 case DMAR_IVA_REG:
b5a280c0
LT
2656 if (size == 4) {
2657 vtd_set_long(s, addr, val);
2658 } else {
2659 vtd_set_quad(s, addr, val);
2660 }
2661 break;
2662
2663 case DMAR_IVA_REG_HI:
b5a280c0
LT
2664 assert(size == 4);
2665 vtd_set_long(s, addr, val);
2666 break;
2667
1da12ec4
LT
2668 /* Fault Status Register, 32-bit */
2669 case DMAR_FSTS_REG:
1da12ec4
LT
2670 assert(size == 4);
2671 vtd_set_long(s, addr, val);
2672 vtd_handle_fsts_write(s);
2673 break;
2674
2675 /* Fault Event Control Register, 32-bit */
2676 case DMAR_FECTL_REG:
1da12ec4
LT
2677 assert(size == 4);
2678 vtd_set_long(s, addr, val);
2679 vtd_handle_fectl_write(s);
2680 break;
2681
2682 /* Fault Event Data Register, 32-bit */
2683 case DMAR_FEDATA_REG:
1da12ec4
LT
2684 assert(size == 4);
2685 vtd_set_long(s, addr, val);
2686 break;
2687
2688 /* Fault Event Address Register, 32-bit */
2689 case DMAR_FEADDR_REG:
b7a7bb35
JK
2690 if (size == 4) {
2691 vtd_set_long(s, addr, val);
2692 } else {
2693 /*
2694 * While the register is 32-bit only, some guests (Xen...) write to
2695 * it with 64-bit.
2696 */
2697 vtd_set_quad(s, addr, val);
2698 }
1da12ec4
LT
2699 break;
2700
2701 /* Fault Event Upper Address Register, 32-bit */
2702 case DMAR_FEUADDR_REG:
1da12ec4
LT
2703 assert(size == 4);
2704 vtd_set_long(s, addr, val);
2705 break;
2706
2707 /* Protected Memory Enable Register, 32-bit */
2708 case DMAR_PMEN_REG:
1da12ec4
LT
2709 assert(size == 4);
2710 vtd_set_long(s, addr, val);
2711 break;
2712
2713 /* Root Table Address Register, 64-bit */
2714 case DMAR_RTADDR_REG:
1da12ec4
LT
2715 if (size == 4) {
2716 vtd_set_long(s, addr, val);
2717 } else {
2718 vtd_set_quad(s, addr, val);
2719 }
2720 break;
2721
2722 case DMAR_RTADDR_REG_HI:
1da12ec4
LT
2723 assert(size == 4);
2724 vtd_set_long(s, addr, val);
2725 break;
2726
ed7b8fbc
LT
2727 /* Invalidation Queue Tail Register, 64-bit */
2728 case DMAR_IQT_REG:
ed7b8fbc
LT
2729 if (size == 4) {
2730 vtd_set_long(s, addr, val);
2731 } else {
2732 vtd_set_quad(s, addr, val);
2733 }
2734 vtd_handle_iqt_write(s);
2735 break;
2736
2737 case DMAR_IQT_REG_HI:
ed7b8fbc
LT
2738 assert(size == 4);
2739 vtd_set_long(s, addr, val);
2740 /* 19:63 of IQT_REG is RsvdZ, do nothing here */
2741 break;
2742
2743 /* Invalidation Queue Address Register, 64-bit */
2744 case DMAR_IQA_REG:
ed7b8fbc
LT
2745 if (size == 4) {
2746 vtd_set_long(s, addr, val);
2747 } else {
2748 vtd_set_quad(s, addr, val);
2749 }
2750 break;
2751
2752 case DMAR_IQA_REG_HI:
ed7b8fbc
LT
2753 assert(size == 4);
2754 vtd_set_long(s, addr, val);
2755 break;
2756
2757 /* Invalidation Completion Status Register, 32-bit */
2758 case DMAR_ICS_REG:
ed7b8fbc
LT
2759 assert(size == 4);
2760 vtd_set_long(s, addr, val);
2761 vtd_handle_ics_write(s);
2762 break;
2763
2764 /* Invalidation Event Control Register, 32-bit */
2765 case DMAR_IECTL_REG:
ed7b8fbc
LT
2766 assert(size == 4);
2767 vtd_set_long(s, addr, val);
2768 vtd_handle_iectl_write(s);
2769 break;
2770
2771 /* Invalidation Event Data Register, 32-bit */
2772 case DMAR_IEDATA_REG:
ed7b8fbc
LT
2773 assert(size == 4);
2774 vtd_set_long(s, addr, val);
2775 break;
2776
2777 /* Invalidation Event Address Register, 32-bit */
2778 case DMAR_IEADDR_REG:
ed7b8fbc
LT
2779 assert(size == 4);
2780 vtd_set_long(s, addr, val);
2781 break;
2782
2783 /* Invalidation Event Upper Address Register, 32-bit */
2784 case DMAR_IEUADDR_REG:
ed7b8fbc
LT
2785 assert(size == 4);
2786 vtd_set_long(s, addr, val);
2787 break;
2788
1da12ec4
LT
2789 /* Fault Recording Registers, 128-bit */
2790 case DMAR_FRCD_REG_0_0:
1da12ec4
LT
2791 if (size == 4) {
2792 vtd_set_long(s, addr, val);
2793 } else {
2794 vtd_set_quad(s, addr, val);
2795 }
2796 break;
2797
2798 case DMAR_FRCD_REG_0_1:
1da12ec4
LT
2799 assert(size == 4);
2800 vtd_set_long(s, addr, val);
2801 break;
2802
2803 case DMAR_FRCD_REG_0_2:
1da12ec4
LT
2804 if (size == 4) {
2805 vtd_set_long(s, addr, val);
2806 } else {
2807 vtd_set_quad(s, addr, val);
2808 /* May clear bit 127 (Fault), update PPF */
2809 vtd_update_fsts_ppf(s);
2810 }
2811 break;
2812
2813 case DMAR_FRCD_REG_0_3:
1da12ec4
LT
2814 assert(size == 4);
2815 vtd_set_long(s, addr, val);
2816 /* May clear bit 127 (Fault), update PPF */
2817 vtd_update_fsts_ppf(s);
2818 break;
2819
a5861439 2820 case DMAR_IRTA_REG:
a5861439
PX
2821 if (size == 4) {
2822 vtd_set_long(s, addr, val);
2823 } else {
2824 vtd_set_quad(s, addr, val);
2825 }
2826 break;
2827
2828 case DMAR_IRTA_REG_HI:
a5861439
PX
2829 assert(size == 4);
2830 vtd_set_long(s, addr, val);
2831 break;
2832
1da12ec4 2833 default:
1da12ec4
LT
2834 if (size == 4) {
2835 vtd_set_long(s, addr, val);
2836 } else {
2837 vtd_set_quad(s, addr, val);
2838 }
2839 }
2840}
2841
3df9d748 2842static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
2c91bcf2 2843 IOMMUAccessFlags flag, int iommu_idx)
1da12ec4
LT
2844{
2845 VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
2846 IntelIOMMUState *s = vtd_as->iommu_state;
b9313021
PX
2847 IOMMUTLBEntry iotlb = {
2848 /* We'll fill in the rest later. */
1da12ec4 2849 .target_as = &address_space_memory,
1da12ec4 2850 };
b9313021 2851 bool success;
1da12ec4 2852
b9313021
PX
2853 if (likely(s->dmar_enabled)) {
2854 success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
2855 addr, flag & IOMMU_WO, &iotlb);
2856 } else {
1da12ec4 2857 /* DMAR disabled, passthrough, use 4k-page*/
b9313021
PX
2858 iotlb.iova = addr & VTD_PAGE_MASK_4K;
2859 iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
2860 iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
2861 iotlb.perm = IOMMU_RW;
2862 success = true;
1da12ec4
LT
2863 }
2864
b9313021
PX
2865 if (likely(success)) {
2866 trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
2867 VTD_PCI_SLOT(vtd_as->devfn),
2868 VTD_PCI_FUNC(vtd_as->devfn),
2869 iotlb.iova, iotlb.translated_addr,
2870 iotlb.addr_mask);
2871 } else {
4e4abd11
PX
2872 error_report_once("%s: detected translation failure "
2873 "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")",
2874 __func__, pci_bus_num(vtd_as->bus),
2875 VTD_PCI_SLOT(vtd_as->devfn),
2876 VTD_PCI_FUNC(vtd_as->devfn),
662b4b69 2877 addr);
b9313021 2878 }
7feb51b7 2879
b9313021 2880 return iotlb;
1da12ec4
LT
2881}
2882
3df9d748 2883static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
5bf3d319
PX
2884 IOMMUNotifierFlag old,
2885 IOMMUNotifierFlag new)
3cb3b154
AW
2886{
2887 VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
dd4d607e 2888 IntelIOMMUState *s = vtd_as->iommu_state;
3cb3b154 2889
dd4d607e 2890 if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) {
4c427a4c 2891 error_report("We need to set caching-mode=1 for intel-iommu to enable "
dd4d607e 2892 "device assignment with IOMMU protection.");
a3276f78
PX
2893 exit(1);
2894 }
dd4d607e 2895
4f8a62a9
PX
2896 /* Update per-address-space notifier flags */
2897 vtd_as->notifier_flags = new;
2898
dd4d607e 2899 if (old == IOMMU_NOTIFIER_NONE) {
b4a4ba0d
PX
2900 QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
2901 } else if (new == IOMMU_NOTIFIER_NONE) {
2902 QLIST_REMOVE(vtd_as, next);
dd4d607e 2903 }
3cb3b154
AW
2904}
2905
552a1e01
PX
2906static int vtd_post_load(void *opaque, int version_id)
2907{
2908 IntelIOMMUState *iommu = opaque;
2909
2910 /*
2911 * Memory regions are dynamically turned on/off depending on
2912 * context entry configurations from the guest. After migration,
2913 * we need to make sure the memory regions are still correct.
2914 */
2915 vtd_switch_address_space_all(iommu);
2916
2917 return 0;
2918}
2919
1da12ec4
LT
2920static const VMStateDescription vtd_vmstate = {
2921 .name = "iommu-intel",
8cdcf3c1
PX
2922 .version_id = 1,
2923 .minimum_version_id = 1,
2924 .priority = MIG_PRI_IOMMU,
552a1e01 2925 .post_load = vtd_post_load,
8cdcf3c1
PX
2926 .fields = (VMStateField[]) {
2927 VMSTATE_UINT64(root, IntelIOMMUState),
2928 VMSTATE_UINT64(intr_root, IntelIOMMUState),
2929 VMSTATE_UINT64(iq, IntelIOMMUState),
2930 VMSTATE_UINT32(intr_size, IntelIOMMUState),
2931 VMSTATE_UINT16(iq_head, IntelIOMMUState),
2932 VMSTATE_UINT16(iq_tail, IntelIOMMUState),
2933 VMSTATE_UINT16(iq_size, IntelIOMMUState),
2934 VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
2935 VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
2936 VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
2937 VMSTATE_BOOL(root_extended, IntelIOMMUState),
fb43cf73 2938 VMSTATE_BOOL(root_scalable, IntelIOMMUState),
8cdcf3c1
PX
2939 VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
2940 VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
2941 VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
2942 VMSTATE_BOOL(intr_eime, IntelIOMMUState),
2943 VMSTATE_END_OF_LIST()
2944 }
1da12ec4
LT
2945};
2946
2947static const MemoryRegionOps vtd_mem_ops = {
2948 .read = vtd_mem_read,
2949 .write = vtd_mem_write,
2950 .endianness = DEVICE_LITTLE_ENDIAN,
2951 .impl = {
2952 .min_access_size = 4,
2953 .max_access_size = 8,
2954 },
2955 .valid = {
2956 .min_access_size = 4,
2957 .max_access_size = 8,
2958 },
2959};
2960
2961static Property vtd_properties[] = {
2962 DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
e6b6af05
RK
2963 DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
2964 ON_OFF_AUTO_AUTO),
fb506e70 2965 DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
4b49b586 2966 DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
37f51384 2967 VTD_HOST_ADDRESS_WIDTH),
3b40f0e5 2968 DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
ccc23bb0 2969 DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
1da12ec4
LT
2970 DEFINE_PROP_END_OF_LIST(),
2971};
2972
651e4cef
PX
2973/* Read IRTE entry with specific index */
2974static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
bc38ee10 2975 VTD_IR_TableEntry *entry, uint16_t sid)
651e4cef 2976{
ede9c94a
PX
2977 static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
2978 {0xffff, 0xfffb, 0xfff9, 0xfff8};
651e4cef 2979 dma_addr_t addr = 0x00;
ede9c94a
PX
2980 uint16_t mask, source_id;
2981 uint8_t bus, bus_max, bus_min;
651e4cef
PX
2982
2983 addr = iommu->intr_root + index * sizeof(*entry);
2984 if (dma_memory_read(&address_space_memory, addr, entry,
2985 sizeof(*entry))) {
1376211f
PX
2986 error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64,
2987 __func__, index, addr);
651e4cef
PX
2988 return -VTD_FR_IR_ROOT_INVAL;
2989 }
2990
7feb51b7
PX
2991 trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
2992 le64_to_cpu(entry->data[0]));
2993
bc38ee10 2994 if (!entry->irte.present) {
4e4abd11
PX
2995 error_report_once("%s: detected non-present IRTE "
2996 "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
2997 __func__, index, le64_to_cpu(entry->data[1]),
2998 le64_to_cpu(entry->data[0]));
651e4cef
PX
2999 return -VTD_FR_IR_ENTRY_P;
3000 }
3001
bc38ee10
MT
3002 if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
3003 entry->irte.__reserved_2) {
4e4abd11
PX
3004 error_report_once("%s: detected non-zero reserved IRTE "
3005 "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
3006 __func__, index, le64_to_cpu(entry->data[1]),
3007 le64_to_cpu(entry->data[0]));
651e4cef
PX
3008 return -VTD_FR_IR_IRTE_RSVD;
3009 }
3010
ede9c94a
PX
3011 if (sid != X86_IOMMU_SID_INVALID) {
3012 /* Validate IRTE SID */
bc38ee10
MT
3013 source_id = le32_to_cpu(entry->irte.source_id);
3014 switch (entry->irte.sid_vtype) {
ede9c94a 3015 case VTD_SVT_NONE:
ede9c94a
PX
3016 break;
3017
3018 case VTD_SVT_ALL:
bc38ee10 3019 mask = vtd_svt_mask[entry->irte.sid_q];
ede9c94a 3020 if ((source_id & mask) != (sid & mask)) {
4e4abd11
PX
3021 error_report_once("%s: invalid IRTE SID "
3022 "(index=%u, sid=%u, source_id=%u)",
3023 __func__, index, sid, source_id);
ede9c94a
PX
3024 return -VTD_FR_IR_SID_ERR;
3025 }
3026 break;
3027
3028 case VTD_SVT_BUS:
3029 bus_max = source_id >> 8;
3030 bus_min = source_id & 0xff;
3031 bus = sid >> 8;
3032 if (bus > bus_max || bus < bus_min) {
4e4abd11
PX
3033 error_report_once("%s: invalid SVT_BUS "
3034 "(index=%u, bus=%u, min=%u, max=%u)",
3035 __func__, index, bus, bus_min, bus_max);
ede9c94a
PX
3036 return -VTD_FR_IR_SID_ERR;
3037 }
3038 break;
3039
3040 default:
4e4abd11
PX
3041 error_report_once("%s: detected invalid IRTE SVT "
3042 "(index=%u, type=%d)", __func__,
3043 index, entry->irte.sid_vtype);
ede9c94a
PX
3044 /* Take this as verification failure. */
3045 return -VTD_FR_IR_SID_ERR;
3046 break;
3047 }
3048 }
651e4cef
PX
3049
3050 return 0;
3051}
3052
3053/* Fetch IRQ information of specific IR index */
ede9c94a 3054static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
35c24501 3055 X86IOMMUIrq *irq, uint16_t sid)
651e4cef 3056{
bc38ee10 3057 VTD_IR_TableEntry irte = {};
651e4cef
PX
3058 int ret = 0;
3059
ede9c94a 3060 ret = vtd_irte_get(iommu, index, &irte, sid);
651e4cef
PX
3061 if (ret) {
3062 return ret;
3063 }
3064
bc38ee10
MT
3065 irq->trigger_mode = irte.irte.trigger_mode;
3066 irq->vector = irte.irte.vector;
3067 irq->delivery_mode = irte.irte.delivery_mode;
3068 irq->dest = le32_to_cpu(irte.irte.dest_id);
28589311 3069 if (!iommu->intr_eime) {
651e4cef
PX
3070#define VTD_IR_APIC_DEST_MASK (0xff00ULL)
3071#define VTD_IR_APIC_DEST_SHIFT (8)
28589311
JK
3072 irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
3073 VTD_IR_APIC_DEST_SHIFT;
3074 }
bc38ee10
MT
3075 irq->dest_mode = irte.irte.dest_mode;
3076 irq->redir_hint = irte.irte.redir_hint;
651e4cef 3077
7feb51b7
PX
3078 trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
3079 irq->delivery_mode, irq->dest, irq->dest_mode);
651e4cef
PX
3080
3081 return 0;
3082}
3083
651e4cef
PX
3084/* Interrupt remapping for MSI/MSI-X entry */
3085static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
3086 MSIMessage *origin,
ede9c94a
PX
3087 MSIMessage *translated,
3088 uint16_t sid)
651e4cef
PX
3089{
3090 int ret = 0;
3091 VTD_IR_MSIAddress addr;
3092 uint16_t index;
35c24501 3093 X86IOMMUIrq irq = {};
651e4cef
PX
3094
3095 assert(origin && translated);
3096
7feb51b7
PX
3097 trace_vtd_ir_remap_msi_req(origin->address, origin->data);
3098
651e4cef 3099 if (!iommu || !iommu->intr_enabled) {
e7a3b91f
PX
3100 memcpy(translated, origin, sizeof(*origin));
3101 goto out;
651e4cef
PX
3102 }
3103
3104 if (origin->address & VTD_MSI_ADDR_HI_MASK) {
1376211f
PX
3105 error_report_once("%s: MSI address high 32 bits non-zero detected: "
3106 "address=0x%" PRIx64, __func__, origin->address);
651e4cef
PX
3107 return -VTD_FR_IR_REQ_RSVD;
3108 }
3109
3110 addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
1a43713b 3111 if (addr.addr.__head != 0xfee) {
1376211f
PX
3112 error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32,
3113 __func__, addr.data);
651e4cef
PX
3114 return -VTD_FR_IR_REQ_RSVD;
3115 }
3116
3117 /* This is compatible mode. */
bc38ee10 3118 if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
e7a3b91f
PX
3119 memcpy(translated, origin, sizeof(*origin));
3120 goto out;
651e4cef
PX
3121 }
3122
bc38ee10 3123 index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
651e4cef
PX
3124
3125#define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff)
3126#define VTD_IR_MSI_DATA_RESERVED (0xffff0000)
3127
bc38ee10 3128 if (addr.addr.sub_valid) {
651e4cef
PX
3129 /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
3130 index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
3131 }
3132
ede9c94a 3133 ret = vtd_remap_irq_get(iommu, index, &irq, sid);
651e4cef
PX
3134 if (ret) {
3135 return ret;
3136 }
3137
bc38ee10 3138 if (addr.addr.sub_valid) {
7feb51b7 3139 trace_vtd_ir_remap_type("MSI");
651e4cef 3140 if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
4e4abd11
PX
3141 error_report_once("%s: invalid IR MSI "
3142 "(sid=%u, address=0x%" PRIx64
3143 ", data=0x%" PRIx32 ")",
3144 __func__, sid, origin->address, origin->data);
651e4cef
PX
3145 return -VTD_FR_IR_REQ_RSVD;
3146 }
3147 } else {
3148 uint8_t vector = origin->data & 0xff;
dea651a9
FW
3149 uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
3150
7feb51b7 3151 trace_vtd_ir_remap_type("IOAPIC");
651e4cef
PX
3152 /* IOAPIC entry vector should be aligned with IRTE vector
3153 * (see vt-d spec 5.1.5.1). */
3154 if (vector != irq.vector) {
7feb51b7 3155 trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
651e4cef 3156 }
dea651a9
FW
3157
3158 /* The Trigger Mode field must match the Trigger Mode in the IRTE.
3159 * (see vt-d spec 5.1.5.1). */
3160 if (trigger_mode != irq.trigger_mode) {
7feb51b7
PX
3161 trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
3162 irq.trigger_mode);
dea651a9 3163 }
651e4cef
PX
3164 }
3165
3166 /*
3167 * We'd better keep the last two bits, assuming that guest OS
3168 * might modify it. Keep it does not hurt after all.
3169 */
bc38ee10 3170 irq.msi_addr_last_bits = addr.addr.__not_care;
651e4cef 3171
35c24501
BS
3172 /* Translate X86IOMMUIrq to MSI message */
3173 x86_iommu_irq_to_msi_message(&irq, translated);
651e4cef 3174
e7a3b91f 3175out:
7feb51b7
PX
3176 trace_vtd_ir_remap_msi(origin->address, origin->data,
3177 translated->address, translated->data);
651e4cef
PX
3178 return 0;
3179}
3180
8b5ed7df
PX
3181static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
3182 MSIMessage *dst, uint16_t sid)
3183{
ede9c94a
PX
3184 return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
3185 src, dst, sid);
8b5ed7df
PX
3186}
3187
651e4cef
PX
3188static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
3189 uint64_t *data, unsigned size,
3190 MemTxAttrs attrs)
3191{
3192 return MEMTX_OK;
3193}
3194
3195static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
3196 uint64_t value, unsigned size,
3197 MemTxAttrs attrs)
3198{
3199 int ret = 0;
09cd058a 3200 MSIMessage from = {}, to = {};
ede9c94a 3201 uint16_t sid = X86_IOMMU_SID_INVALID;
651e4cef
PX
3202
3203 from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
3204 from.data = (uint32_t) value;
3205
ede9c94a
PX
3206 if (!attrs.unspecified) {
3207 /* We have explicit Source ID */
3208 sid = attrs.requester_id;
3209 }
3210
3211 ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
651e4cef
PX
3212 if (ret) {
3213 /* TODO: report error */
651e4cef
PX
3214 /* Drop this interrupt */
3215 return MEMTX_ERROR;
3216 }
3217
32946019 3218 apic_get_class()->send_msi(&to);
651e4cef
PX
3219
3220 return MEMTX_OK;
3221}
3222
3223static const MemoryRegionOps vtd_mem_ir_ops = {
3224 .read_with_attrs = vtd_mem_ir_read,
3225 .write_with_attrs = vtd_mem_ir_write,
3226 .endianness = DEVICE_LITTLE_ENDIAN,
3227 .impl = {
3228 .min_access_size = 4,
3229 .max_access_size = 4,
3230 },
3231 .valid = {
3232 .min_access_size = 4,
3233 .max_access_size = 4,
3234 },
3235};
7df953bd
KO
3236
3237VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
3238{
3239 uintptr_t key = (uintptr_t)bus;
3240 VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key);
3241 VTDAddressSpace *vtd_dev_as;
e0a3c8cc 3242 char name[128];
7df953bd
KO
3243
3244 if (!vtd_bus) {
2d3fc581
JW
3245 uintptr_t *new_key = g_malloc(sizeof(*new_key));
3246 *new_key = (uintptr_t)bus;
7df953bd 3247 /* No corresponding free() */
04af0e18 3248 vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \
bf33cc75 3249 PCI_DEVFN_MAX);
7df953bd 3250 vtd_bus->bus = bus;
2d3fc581 3251 g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus);
7df953bd
KO
3252 }
3253
3254 vtd_dev_as = vtd_bus->dev_as[devfn];
3255
3256 if (!vtd_dev_as) {
e0a3c8cc 3257 snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn);
7df953bd
KO
3258 vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
3259
3260 vtd_dev_as->bus = bus;
3261 vtd_dev_as->devfn = (uint8_t)devfn;
3262 vtd_dev_as->iommu_state = s;
3263 vtd_dev_as->context_cache_entry.context_cache_gen = 0;
63b88968 3264 vtd_dev_as->iova_tree = iova_tree_new();
558e0024
PX
3265
3266 /*
3267 * Memory region relationships looks like (Address range shows
3268 * only lower 32 bits to make it short in length...):
3269 *
3270 * |-----------------+-------------------+----------|
3271 * | Name | Address range | Priority |
3272 * |-----------------+-------------------+----------+
3273 * | vtd_root | 00000000-ffffffff | 0 |
3274 * | intel_iommu | 00000000-ffffffff | 1 |
3275 * | vtd_sys_alias | 00000000-ffffffff | 1 |
3276 * | intel_iommu_ir | fee00000-feefffff | 64 |
3277 * |-----------------+-------------------+----------|
3278 *
3279 * We enable/disable DMAR by switching enablement for
3280 * vtd_sys_alias and intel_iommu regions. IR region is always
3281 * enabled.
3282 */
1221a474
AK
3283 memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
3284 TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
3285 "intel_iommu_dmar",
558e0024
PX
3286 UINT64_MAX);
3287 memory_region_init_alias(&vtd_dev_as->sys_alias, OBJECT(s),
3288 "vtd_sys_alias", get_system_memory(),
3289 0, memory_region_size(get_system_memory()));
651e4cef
PX
3290 memory_region_init_io(&vtd_dev_as->iommu_ir, OBJECT(s),
3291 &vtd_mem_ir_ops, s, "intel_iommu_ir",
3292 VTD_INTERRUPT_ADDR_SIZE);
558e0024
PX
3293 memory_region_init(&vtd_dev_as->root, OBJECT(s),
3294 "vtd_root", UINT64_MAX);
3295 memory_region_add_subregion_overlap(&vtd_dev_as->root,
3296 VTD_INTERRUPT_ADDR_FIRST,
3297 &vtd_dev_as->iommu_ir, 64);
3298 address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, name);
3299 memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
3300 &vtd_dev_as->sys_alias, 1);
3301 memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
3df9d748
AK
3302 MEMORY_REGION(&vtd_dev_as->iommu),
3303 1);
558e0024 3304 vtd_switch_address_space(vtd_dev_as);
7df953bd
KO
3305 }
3306 return vtd_dev_as;
3307}
3308
dd4d607e
PX
3309/* Unmap the whole range in the notifier's scope. */
3310static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
3311{
3312 IOMMUTLBEntry entry;
3313 hwaddr size;
3314 hwaddr start = n->start;
3315 hwaddr end = n->end;
37f51384 3316 IntelIOMMUState *s = as->iommu_state;
63b88968 3317 DMAMap map;
dd4d607e
PX
3318
3319 /*
3320 * Note: all the codes in this function has a assumption that IOVA
3321 * bits are no more than VTD_MGAW bits (which is restricted by
3322 * VT-d spec), otherwise we need to consider overflow of 64 bits.
3323 */
3324
37f51384 3325 if (end > VTD_ADDRESS_SIZE(s->aw_bits)) {
dd4d607e
PX
3326 /*
3327 * Don't need to unmap regions that is bigger than the whole
3328 * VT-d supported address space size
3329 */
37f51384 3330 end = VTD_ADDRESS_SIZE(s->aw_bits);
dd4d607e
PX
3331 }
3332
3333 assert(start <= end);
3334 size = end - start;
3335
3336 if (ctpop64(size) != 1) {
3337 /*
3338 * This size cannot format a correct mask. Let's enlarge it to
3339 * suite the minimum available mask.
3340 */
3341 int n = 64 - clz64(size);
37f51384 3342 if (n > s->aw_bits) {
dd4d607e 3343 /* should not happen, but in case it happens, limit it */
37f51384 3344 n = s->aw_bits;
dd4d607e
PX
3345 }
3346 size = 1ULL << n;
3347 }
3348
3349 entry.target_as = &address_space_memory;
3350 /* Adjust iova for the size */
3351 entry.iova = n->start & ~(size - 1);
3352 /* This field is meaningless for unmap */
3353 entry.translated_addr = 0;
3354 entry.perm = IOMMU_NONE;
3355 entry.addr_mask = size - 1;
3356
3357 trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
3358 VTD_PCI_SLOT(as->devfn),
3359 VTD_PCI_FUNC(as->devfn),
3360 entry.iova, size);
3361
63b88968
PX
3362 map.iova = entry.iova;
3363 map.size = entry.addr_mask;
3364 iova_tree_remove(as->iova_tree, &map);
3365
dd4d607e
PX
3366 memory_region_notify_one(n, &entry);
3367}
3368
3369static void vtd_address_space_unmap_all(IntelIOMMUState *s)
3370{
dd4d607e
PX
3371 VTDAddressSpace *vtd_as;
3372 IOMMUNotifier *n;
3373
b4a4ba0d 3374 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
dd4d607e
PX
3375 IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
3376 vtd_address_space_unmap(vtd_as, n);
3377 }
3378 }
3379}
3380
2cc9ddcc
PX
3381static void vtd_address_space_refresh_all(IntelIOMMUState *s)
3382{
3383 vtd_address_space_unmap_all(s);
3384 vtd_switch_address_space_all(s);
3385}
3386
f06a696d
PX
3387static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private)
3388{
3389 memory_region_notify_one((IOMMUNotifier *)private, entry);
3390 return 0;
3391}
3392
3df9d748 3393static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
f06a696d 3394{
3df9d748 3395 VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
f06a696d
PX
3396 IntelIOMMUState *s = vtd_as->iommu_state;
3397 uint8_t bus_n = pci_bus_num(vtd_as->bus);
3398 VTDContextEntry ce;
3399
dd4d607e
PX
3400 /*
3401 * The replay can be triggered by either a invalidation or a newly
3402 * created entry. No matter what, we release existing mappings
3403 * (it means flushing caches for UNMAP-only registers).
3404 */
3405 vtd_address_space_unmap(vtd_as, n);
3406
f06a696d 3407 if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
fb43cf73
LY
3408 trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
3409 "legacy mode",
3410 bus_n, PCI_SLOT(vtd_as->devfn),
f06a696d 3411 PCI_FUNC(vtd_as->devfn),
fb43cf73 3412 vtd_get_domain_id(s, &ce),
f06a696d 3413 ce.hi, ce.lo);
4f8a62a9
PX
3414 if (vtd_as_has_map_notifier(vtd_as)) {
3415 /* This is required only for MAP typed notifiers */
fe215b0c
PX
3416 vtd_page_walk_info info = {
3417 .hook_fn = vtd_replay_hook,
3418 .private = (void *)n,
3419 .notify_unmap = false,
3420 .aw = s->aw_bits,
2f764fa8 3421 .as = vtd_as,
fb43cf73 3422 .domain_id = vtd_get_domain_id(s, &ce),
fe215b0c
PX
3423 };
3424
fb43cf73 3425 vtd_page_walk(s, &ce, 0, ~0ULL, &info);
4f8a62a9 3426 }
f06a696d
PX
3427 } else {
3428 trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
3429 PCI_FUNC(vtd_as->devfn));
3430 }
3431
3432 return;
3433}
3434
1da12ec4
LT
3435/* Do the initialization. It will also be called when reset, so pay
3436 * attention when adding new initialization stuff.
3437 */
3438static void vtd_init(IntelIOMMUState *s)
3439{
d54bd7f8
PX
3440 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3441
1da12ec4
LT
3442 memset(s->csr, 0, DMAR_REG_SIZE);
3443 memset(s->wmask, 0, DMAR_REG_SIZE);
3444 memset(s->w1cmask, 0, DMAR_REG_SIZE);
3445 memset(s->womask, 0, DMAR_REG_SIZE);
3446
1da12ec4
LT
3447 s->root = 0;
3448 s->root_extended = false;
fb43cf73 3449 s->root_scalable = false;
1da12ec4 3450 s->dmar_enabled = false;
d7bb469a 3451 s->intr_enabled = false;
1da12ec4
LT
3452 s->iq_head = 0;
3453 s->iq_tail = 0;
3454 s->iq = 0;
3455 s->iq_size = 0;
3456 s->qi_enabled = false;
3457 s->iq_last_desc_type = VTD_INV_DESC_NONE;
3458 s->next_frcd_reg = 0;
92e5d85e
PS
3459 s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
3460 VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
37f51384 3461 VTD_CAP_SAGAW_39bit | VTD_CAP_MGAW(s->aw_bits);
ccc23bb0
PX
3462 if (s->dma_drain) {
3463 s->cap |= VTD_CAP_DRAIN;
3464 }
37f51384
PS
3465 if (s->aw_bits == VTD_HOST_AW_48BIT) {
3466 s->cap |= VTD_CAP_SAGAW_48bit;
3467 }
ed7b8fbc 3468 s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
1da12ec4 3469
92e5d85e
PS
3470 /*
3471 * Rsvd field masks for spte
3472 */
3473 vtd_paging_entry_rsvd_field[0] = ~0ULL;
37f51384
PS
3474 vtd_paging_entry_rsvd_field[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits);
3475 vtd_paging_entry_rsvd_field[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
3476 vtd_paging_entry_rsvd_field[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
3477 vtd_paging_entry_rsvd_field[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
3478 vtd_paging_entry_rsvd_field[5] = VTD_SPTE_LPAGE_L1_RSVD_MASK(s->aw_bits);
3479 vtd_paging_entry_rsvd_field[6] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits);
3480 vtd_paging_entry_rsvd_field[7] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits);
3481 vtd_paging_entry_rsvd_field[8] = VTD_SPTE_LPAGE_L4_RSVD_MASK(s->aw_bits);
92e5d85e 3482
a924b3d8 3483 if (x86_iommu_ir_supported(x86_iommu)) {
e6b6af05
RK
3484 s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
3485 if (s->intr_eim == ON_OFF_AUTO_ON) {
3486 s->ecap |= VTD_ECAP_EIM;
3487 }
3488 assert(s->intr_eim != ON_OFF_AUTO_AUTO);
d54bd7f8
PX
3489 }
3490
554f5e16
JW
3491 if (x86_iommu->dt_supported) {
3492 s->ecap |= VTD_ECAP_DT;
3493 }
3494
dbaabb25
PX
3495 if (x86_iommu->pt_supported) {
3496 s->ecap |= VTD_ECAP_PT;
3497 }
3498
3b40f0e5
ABD
3499 if (s->caching_mode) {
3500 s->cap |= VTD_CAP_CM;
3501 }
3502
06aba4ca 3503 vtd_reset_caches(s);
d92fa2dc 3504
1da12ec4
LT
3505 /* Define registers with default values and bit semantics */
3506 vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
3507 vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
3508 vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
3509 vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
3510 vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
3511 vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
fb43cf73 3512 vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0);
1da12ec4
LT
3513 vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
3514 vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
3515
3516 /* Advanced Fault Logging not supported */
3517 vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
3518 vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3519 vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
3520 vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
3521
3522 /* Treated as RsvdZ when EIM in ECAP_REG is not supported
3523 * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
3524 */
3525 vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
3526
3527 /* Treated as RO for implementations that PLMR and PHMR fields reported
3528 * as Clear in the CAP_REG.
3529 * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
3530 */
3531 vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
3532
ed7b8fbc
LT
3533 vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
3534 vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
3535 vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
3536 vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
3537 vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3538 vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
3539 vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
3540 /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
3541 vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
3542
1da12ec4
LT
3543 /* IOTLB registers */
3544 vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
3545 vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
3546 vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
3547
3548 /* Fault Recording Registers, 128-bit */
3549 vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
3550 vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
a5861439
PX
3551
3552 /*
28589311 3553 * Interrupt remapping registers.
a5861439 3554 */
28589311 3555 vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
1da12ec4
LT
3556}
3557
3558/* Should not reset address_spaces when reset because devices will still use
3559 * the address space they got at first (won't ask the bus again).
3560 */
3561static void vtd_reset(DeviceState *dev)
3562{
3563 IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
3564
1da12ec4 3565 vtd_init(s);
2cc9ddcc 3566 vtd_address_space_refresh_all(s);
1da12ec4
LT
3567}
3568
621d983a
MA
3569static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
3570{
3571 IntelIOMMUState *s = opaque;
3572 VTDAddressSpace *vtd_as;
3573
bf33cc75 3574 assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
621d983a
MA
3575
3576 vtd_as = vtd_find_add_as(s, bus, devfn);
3577 return &vtd_as->as;
3578}
3579
e6b6af05 3580static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
6333e93c 3581{
e6b6af05
RK
3582 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3583
a924b3d8 3584 if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) {
e6b6af05
RK
3585 error_setg(errp, "eim=on cannot be selected without intremap=on");
3586 return false;
3587 }
3588
3589 if (s->intr_eim == ON_OFF_AUTO_AUTO) {
fb506e70 3590 s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
a924b3d8 3591 && x86_iommu_ir_supported(x86_iommu) ?
e6b6af05
RK
3592 ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
3593 }
fb506e70
RK
3594 if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
3595 if (!kvm_irqchip_in_kernel()) {
3596 error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
3597 return false;
3598 }
3599 if (!kvm_enable_x2apic()) {
3600 error_setg(errp, "eim=on requires support on the KVM side"
3601 "(X2APIC_API, first shipped in v4.7)");
3602 return false;
3603 }
3604 }
e6b6af05 3605
37f51384
PS
3606 /* Currently only address widths supported are 39 and 48 bits */
3607 if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
3608 (s->aw_bits != VTD_HOST_AW_48BIT)) {
3609 error_setg(errp, "Supported values for x-aw-bits are: %d, %d",
3610 VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
3611 return false;
3612 }
3613
6333e93c
RK
3614 return true;
3615}
3616
1da12ec4
LT
3617static void vtd_realize(DeviceState *dev, Error **errp)
3618{
ef0e8fc7 3619 MachineState *ms = MACHINE(qdev_get_machine());
29396ed9
MG
3620 PCMachineState *pcms = PC_MACHINE(ms);
3621 PCIBus *bus = pcms->bus;
1da12ec4 3622 IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
4684a204 3623 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
1da12ec4 3624
fb9f5926 3625 x86_iommu->type = TYPE_INTEL;
6333e93c 3626
e6b6af05 3627 if (!vtd_decide_config(s, errp)) {
6333e93c
RK
3628 return;
3629 }
3630
b4a4ba0d 3631 QLIST_INIT(&s->vtd_as_with_notifiers);
1d9efa73 3632 qemu_mutex_init(&s->iommu_lock);
7df953bd 3633 memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num));
1da12ec4
LT
3634 memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
3635 "intel_iommu", DMAR_REG_SIZE);
3636 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
b5a280c0
LT
3637 /* No corresponding destroy */
3638 s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3639 g_free, g_free);
7df953bd
KO
3640 s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3641 g_free, g_free);
1da12ec4 3642 vtd_init(s);
621d983a
MA
3643 sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
3644 pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
cb135f59
PX
3645 /* Pseudo address space under root PCI bus. */
3646 pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
1da12ec4
LT
3647}
3648
3649static void vtd_class_init(ObjectClass *klass, void *data)
3650{
3651 DeviceClass *dc = DEVICE_CLASS(klass);
1c7955c4 3652 X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass);
1da12ec4
LT
3653
3654 dc->reset = vtd_reset;
1da12ec4
LT
3655 dc->vmsd = &vtd_vmstate;
3656 dc->props = vtd_properties;
621d983a 3657 dc->hotpluggable = false;
1c7955c4 3658 x86_class->realize = vtd_realize;
8b5ed7df 3659 x86_class->int_remap = vtd_int_remap;
8ab5700c 3660 /* Supported by the pc-q35-* machine types */
e4f4fb1e 3661 dc->user_creatable = true;
1da12ec4
LT
3662}
3663
3664static const TypeInfo vtd_info = {
3665 .name = TYPE_INTEL_IOMMU_DEVICE,
1c7955c4 3666 .parent = TYPE_X86_IOMMU_DEVICE,
1da12ec4
LT
3667 .instance_size = sizeof(IntelIOMMUState),
3668 .class_init = vtd_class_init,
3669};
3670
1221a474
AK
3671static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
3672 void *data)
3673{
3674 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
3675
3676 imrc->translate = vtd_iommu_translate;
3677 imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
3678 imrc->replay = vtd_iommu_replay;
3679}
3680
3681static const TypeInfo vtd_iommu_memory_region_info = {
3682 .parent = TYPE_IOMMU_MEMORY_REGION,
3683 .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
3684 .class_init = vtd_iommu_memory_region_class_init,
3685};
3686
1da12ec4
LT
3687static void vtd_register_types(void)
3688{
1da12ec4 3689 type_register_static(&vtd_info);
1221a474 3690 type_register_static(&vtd_iommu_memory_region_info);
1da12ec4
LT
3691}
3692
3693type_init(vtd_register_types)