2 * QEMU emulation of an Intel IOMMU (VT-d)
3 * (DMA Remapping device)
5 * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com>
6 * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "intel_iommu_internal.h"
28 #include "hw/pci/pci.h"
29 #include "hw/pci/pci_bus.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/i386/pc.h"
32 #include "hw/i386/apic-msidef.h"
33 #include "hw/i386/x86-iommu.h"
34 #include "hw/pci-host/q35.h"
35 #include "sysemu/kvm.h"
36 #include "sysemu/dma.h"
37 #include "sysemu/sysemu.h"
38 #include "hw/i386/apic_internal.h"
39 #include "kvm/kvm_i386.h"
40 #include "migration/vmstate.h"
43 /* context entry operations */
44 #define VTD_CE_GET_RID2PASID(ce) \
45 ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK)
46 #define VTD_CE_GET_PASID_DIR_TABLE(ce) \
47 ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
50 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
51 #define VTD_PE_GET_LEVEL(pe) (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
54 * PCI bus number (or SID) is not reliable since the device is usaully
55 * initalized before guest can configure the PCI bridge
56 * (SECONDARY_BUS_NUMBER).
64 struct vtd_iotlb_key
{
71 static void vtd_address_space_refresh_all(IntelIOMMUState
*s
);
72 static void vtd_address_space_unmap(VTDAddressSpace
*as
, IOMMUNotifier
*n
);
74 static void vtd_panic_require_caching_mode(void)
76 error_report("We need to set caching-mode=on for intel-iommu to enable "
77 "device assignment with IOMMU protection.");
81 static void vtd_define_quad(IntelIOMMUState
*s
, hwaddr addr
, uint64_t val
,
82 uint64_t wmask
, uint64_t w1cmask
)
84 stq_le_p(&s
->csr
[addr
], val
);
85 stq_le_p(&s
->wmask
[addr
], wmask
);
86 stq_le_p(&s
->w1cmask
[addr
], w1cmask
);
89 static void vtd_define_quad_wo(IntelIOMMUState
*s
, hwaddr addr
, uint64_t mask
)
91 stq_le_p(&s
->womask
[addr
], mask
);
94 static void vtd_define_long(IntelIOMMUState
*s
, hwaddr addr
, uint32_t val
,
95 uint32_t wmask
, uint32_t w1cmask
)
97 stl_le_p(&s
->csr
[addr
], val
);
98 stl_le_p(&s
->wmask
[addr
], wmask
);
99 stl_le_p(&s
->w1cmask
[addr
], w1cmask
);
102 static void vtd_define_long_wo(IntelIOMMUState
*s
, hwaddr addr
, uint32_t mask
)
104 stl_le_p(&s
->womask
[addr
], mask
);
107 /* "External" get/set operations */
108 static void vtd_set_quad(IntelIOMMUState
*s
, hwaddr addr
, uint64_t val
)
110 uint64_t oldval
= ldq_le_p(&s
->csr
[addr
]);
111 uint64_t wmask
= ldq_le_p(&s
->wmask
[addr
]);
112 uint64_t w1cmask
= ldq_le_p(&s
->w1cmask
[addr
]);
113 stq_le_p(&s
->csr
[addr
],
114 ((oldval
& ~wmask
) | (val
& wmask
)) & ~(w1cmask
& val
));
117 static void vtd_set_long(IntelIOMMUState
*s
, hwaddr addr
, uint32_t val
)
119 uint32_t oldval
= ldl_le_p(&s
->csr
[addr
]);
120 uint32_t wmask
= ldl_le_p(&s
->wmask
[addr
]);
121 uint32_t w1cmask
= ldl_le_p(&s
->w1cmask
[addr
]);
122 stl_le_p(&s
->csr
[addr
],
123 ((oldval
& ~wmask
) | (val
& wmask
)) & ~(w1cmask
& val
));
126 static uint64_t vtd_get_quad(IntelIOMMUState
*s
, hwaddr addr
)
128 uint64_t val
= ldq_le_p(&s
->csr
[addr
]);
129 uint64_t womask
= ldq_le_p(&s
->womask
[addr
]);
130 return val
& ~womask
;
133 static uint32_t vtd_get_long(IntelIOMMUState
*s
, hwaddr addr
)
135 uint32_t val
= ldl_le_p(&s
->csr
[addr
]);
136 uint32_t womask
= ldl_le_p(&s
->womask
[addr
]);
137 return val
& ~womask
;
140 /* "Internal" get/set operations */
141 static uint64_t vtd_get_quad_raw(IntelIOMMUState
*s
, hwaddr addr
)
143 return ldq_le_p(&s
->csr
[addr
]);
146 static uint32_t vtd_get_long_raw(IntelIOMMUState
*s
, hwaddr addr
)
148 return ldl_le_p(&s
->csr
[addr
]);
151 static void vtd_set_quad_raw(IntelIOMMUState
*s
, hwaddr addr
, uint64_t val
)
153 stq_le_p(&s
->csr
[addr
], val
);
156 static uint32_t vtd_set_clear_mask_long(IntelIOMMUState
*s
, hwaddr addr
,
157 uint32_t clear
, uint32_t mask
)
159 uint32_t new_val
= (ldl_le_p(&s
->csr
[addr
]) & ~clear
) | mask
;
160 stl_le_p(&s
->csr
[addr
], new_val
);
164 static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState
*s
, hwaddr addr
,
165 uint64_t clear
, uint64_t mask
)
167 uint64_t new_val
= (ldq_le_p(&s
->csr
[addr
]) & ~clear
) | mask
;
168 stq_le_p(&s
->csr
[addr
], new_val
);
172 static inline void vtd_iommu_lock(IntelIOMMUState
*s
)
174 qemu_mutex_lock(&s
->iommu_lock
);
177 static inline void vtd_iommu_unlock(IntelIOMMUState
*s
)
179 qemu_mutex_unlock(&s
->iommu_lock
);
182 static void vtd_update_scalable_state(IntelIOMMUState
*s
)
184 uint64_t val
= vtd_get_quad_raw(s
, DMAR_RTADDR_REG
);
186 if (s
->scalable_mode
) {
187 s
->root_scalable
= val
& VTD_RTADDR_SMT
;
191 static void vtd_update_iq_dw(IntelIOMMUState
*s
)
193 uint64_t val
= vtd_get_quad_raw(s
, DMAR_IQA_REG
);
195 if (s
->ecap
& VTD_ECAP_SMTS
&&
196 val
& VTD_IQA_DW_MASK
) {
203 /* Whether the address space needs to notify new mappings */
204 static inline gboolean
vtd_as_has_map_notifier(VTDAddressSpace
*as
)
206 return as
->notifier_flags
& IOMMU_NOTIFIER_MAP
;
209 /* GHashTable functions */
210 static gboolean
vtd_iotlb_equal(gconstpointer v1
, gconstpointer v2
)
212 const struct vtd_iotlb_key
*key1
= v1
;
213 const struct vtd_iotlb_key
*key2
= v2
;
215 return key1
->sid
== key2
->sid
&&
216 key1
->pasid
== key2
->pasid
&&
217 key1
->level
== key2
->level
&&
218 key1
->gfn
== key2
->gfn
;
221 static guint
vtd_iotlb_hash(gconstpointer v
)
223 const struct vtd_iotlb_key
*key
= v
;
225 return key
->gfn
| ((key
->sid
) << VTD_IOTLB_SID_SHIFT
) |
226 (key
->level
) << VTD_IOTLB_LVL_SHIFT
|
227 (key
->pasid
) << VTD_IOTLB_PASID_SHIFT
;
230 static gboolean
vtd_as_equal(gconstpointer v1
, gconstpointer v2
)
232 const struct vtd_as_key
*key1
= v1
;
233 const struct vtd_as_key
*key2
= v2
;
235 return (key1
->bus
== key2
->bus
) && (key1
->devfn
== key2
->devfn
) &&
236 (key1
->pasid
== key2
->pasid
);
240 * Note that we use pointer to PCIBus as the key, so hashing/shifting
241 * based on the pointer value is intended. Note that we deal with
242 * collisions through vtd_as_equal().
244 static guint
vtd_as_hash(gconstpointer v
)
246 const struct vtd_as_key
*key
= v
;
247 guint value
= (guint
)(uintptr_t)key
->bus
;
249 return (guint
)(value
<< 8 | key
->devfn
);
252 static gboolean
vtd_hash_remove_by_domain(gpointer key
, gpointer value
,
255 VTDIOTLBEntry
*entry
= (VTDIOTLBEntry
*)value
;
256 uint16_t domain_id
= *(uint16_t *)user_data
;
257 return entry
->domain_id
== domain_id
;
260 /* The shift of an addr for a certain level of paging structure */
261 static inline uint32_t vtd_slpt_level_shift(uint32_t level
)
264 return VTD_PAGE_SHIFT_4K
+ (level
- 1) * VTD_SL_LEVEL_BITS
;
267 static inline uint64_t vtd_slpt_level_page_mask(uint32_t level
)
269 return ~((1ULL << vtd_slpt_level_shift(level
)) - 1);
272 static gboolean
vtd_hash_remove_by_page(gpointer key
, gpointer value
,
275 VTDIOTLBEntry
*entry
= (VTDIOTLBEntry
*)value
;
276 VTDIOTLBPageInvInfo
*info
= (VTDIOTLBPageInvInfo
*)user_data
;
277 uint64_t gfn
= (info
->addr
>> VTD_PAGE_SHIFT_4K
) & info
->mask
;
278 uint64_t gfn_tlb
= (info
->addr
& entry
->mask
) >> VTD_PAGE_SHIFT_4K
;
279 return (entry
->domain_id
== info
->domain_id
) &&
280 (((entry
->gfn
& info
->mask
) == gfn
) ||
281 (entry
->gfn
== gfn_tlb
));
284 /* Reset all the gen of VTDAddressSpace to zero and set the gen of
285 * IntelIOMMUState to 1. Must be called with IOMMU lock held.
287 static void vtd_reset_context_cache_locked(IntelIOMMUState
*s
)
289 VTDAddressSpace
*vtd_as
;
290 GHashTableIter as_it
;
292 trace_vtd_context_cache_reset();
294 g_hash_table_iter_init(&as_it
, s
->vtd_address_spaces
);
296 while (g_hash_table_iter_next(&as_it
, NULL
, (void **)&vtd_as
)) {
297 vtd_as
->context_cache_entry
.context_cache_gen
= 0;
299 s
->context_cache_gen
= 1;
302 /* Must be called with IOMMU lock held. */
303 static void vtd_reset_iotlb_locked(IntelIOMMUState
*s
)
306 g_hash_table_remove_all(s
->iotlb
);
309 static void vtd_reset_iotlb(IntelIOMMUState
*s
)
312 vtd_reset_iotlb_locked(s
);
316 static void vtd_reset_caches(IntelIOMMUState
*s
)
319 vtd_reset_iotlb_locked(s
);
320 vtd_reset_context_cache_locked(s
);
324 static uint64_t vtd_get_iotlb_gfn(hwaddr addr
, uint32_t level
)
326 return (addr
& vtd_slpt_level_page_mask(level
)) >> VTD_PAGE_SHIFT_4K
;
329 /* Must be called with IOMMU lock held */
330 static VTDIOTLBEntry
*vtd_lookup_iotlb(IntelIOMMUState
*s
, uint16_t source_id
,
331 uint32_t pasid
, hwaddr addr
)
333 struct vtd_iotlb_key key
;
334 VTDIOTLBEntry
*entry
;
337 for (level
= VTD_SL_PT_LEVEL
; level
< VTD_SL_PML4_LEVEL
; level
++) {
338 key
.gfn
= vtd_get_iotlb_gfn(addr
, level
);
342 entry
= g_hash_table_lookup(s
->iotlb
, &key
);
352 /* Must be with IOMMU lock held */
353 static void vtd_update_iotlb(IntelIOMMUState
*s
, uint16_t source_id
,
354 uint16_t domain_id
, hwaddr addr
, uint64_t slpte
,
355 uint8_t access_flags
, uint32_t level
,
358 VTDIOTLBEntry
*entry
= g_malloc(sizeof(*entry
));
359 struct vtd_iotlb_key
*key
= g_malloc(sizeof(*key
));
360 uint64_t gfn
= vtd_get_iotlb_gfn(addr
, level
);
362 trace_vtd_iotlb_page_update(source_id
, addr
, slpte
, domain_id
);
363 if (g_hash_table_size(s
->iotlb
) >= VTD_IOTLB_MAX_SIZE
) {
364 trace_vtd_iotlb_reset("iotlb exceeds size limit");
365 vtd_reset_iotlb_locked(s
);
369 entry
->domain_id
= domain_id
;
370 entry
->slpte
= slpte
;
371 entry
->access_flags
= access_flags
;
372 entry
->mask
= vtd_slpt_level_page_mask(level
);
373 entry
->pasid
= pasid
;
376 key
->sid
= source_id
;
380 g_hash_table_replace(s
->iotlb
, key
, entry
);
383 /* Given the reg addr of both the message data and address, generate an
386 static void vtd_generate_interrupt(IntelIOMMUState
*s
, hwaddr mesg_addr_reg
,
387 hwaddr mesg_data_reg
)
391 assert(mesg_data_reg
< DMAR_REG_SIZE
);
392 assert(mesg_addr_reg
< DMAR_REG_SIZE
);
394 msi
.address
= vtd_get_long_raw(s
, mesg_addr_reg
);
395 msi
.data
= vtd_get_long_raw(s
, mesg_data_reg
);
397 trace_vtd_irq_generate(msi
.address
, msi
.data
);
399 apic_get_class(NULL
)->send_msi(&msi
);
402 /* Generate a fault event to software via MSI if conditions are met.
403 * Notice that the value of FSTS_REG being passed to it should be the one
406 static void vtd_generate_fault_event(IntelIOMMUState
*s
, uint32_t pre_fsts
)
408 if (pre_fsts
& VTD_FSTS_PPF
|| pre_fsts
& VTD_FSTS_PFO
||
409 pre_fsts
& VTD_FSTS_IQE
) {
410 error_report_once("There are previous interrupt conditions "
411 "to be serviced by software, fault event "
415 vtd_set_clear_mask_long(s
, DMAR_FECTL_REG
, 0, VTD_FECTL_IP
);
416 if (vtd_get_long_raw(s
, DMAR_FECTL_REG
) & VTD_FECTL_IM
) {
417 error_report_once("Interrupt Mask set, irq is not generated");
419 vtd_generate_interrupt(s
, DMAR_FEADDR_REG
, DMAR_FEDATA_REG
);
420 vtd_set_clear_mask_long(s
, DMAR_FECTL_REG
, VTD_FECTL_IP
, 0);
424 /* Check if the Fault (F) field of the Fault Recording Register referenced by
427 static bool vtd_is_frcd_set(IntelIOMMUState
*s
, uint16_t index
)
429 /* Each reg is 128-bit */
430 hwaddr addr
= DMAR_FRCD_REG_OFFSET
+ (((uint64_t)index
) << 4);
431 addr
+= 8; /* Access the high 64-bit half */
433 assert(index
< DMAR_FRCD_REG_NR
);
435 return vtd_get_quad_raw(s
, addr
) & VTD_FRCD_F
;
438 /* Update the PPF field of Fault Status Register.
439 * Should be called whenever change the F field of any fault recording
442 static void vtd_update_fsts_ppf(IntelIOMMUState
*s
)
445 uint32_t ppf_mask
= 0;
447 for (i
= 0; i
< DMAR_FRCD_REG_NR
; i
++) {
448 if (vtd_is_frcd_set(s
, i
)) {
449 ppf_mask
= VTD_FSTS_PPF
;
453 vtd_set_clear_mask_long(s
, DMAR_FSTS_REG
, VTD_FSTS_PPF
, ppf_mask
);
454 trace_vtd_fsts_ppf(!!ppf_mask
);
457 static void vtd_set_frcd_and_update_ppf(IntelIOMMUState
*s
, uint16_t index
)
459 /* Each reg is 128-bit */
460 hwaddr addr
= DMAR_FRCD_REG_OFFSET
+ (((uint64_t)index
) << 4);
461 addr
+= 8; /* Access the high 64-bit half */
463 assert(index
< DMAR_FRCD_REG_NR
);
465 vtd_set_clear_mask_quad(s
, addr
, 0, VTD_FRCD_F
);
466 vtd_update_fsts_ppf(s
);
469 /* Must not update F field now, should be done later */
470 static void vtd_record_frcd(IntelIOMMUState
*s
, uint16_t index
,
471 uint16_t source_id
, hwaddr addr
,
472 VTDFaultReason fault
, bool is_write
,
473 bool is_pasid
, uint32_t pasid
)
476 hwaddr frcd_reg_addr
= DMAR_FRCD_REG_OFFSET
+ (((uint64_t)index
) << 4);
478 assert(index
< DMAR_FRCD_REG_NR
);
480 lo
= VTD_FRCD_FI(addr
);
481 hi
= VTD_FRCD_SID(source_id
) | VTD_FRCD_FR(fault
) |
482 VTD_FRCD_PV(pasid
) | VTD_FRCD_PP(is_pasid
);
486 vtd_set_quad_raw(s
, frcd_reg_addr
, lo
);
487 vtd_set_quad_raw(s
, frcd_reg_addr
+ 8, hi
);
489 trace_vtd_frr_new(index
, hi
, lo
);
492 /* Try to collapse multiple pending faults from the same requester */
493 static bool vtd_try_collapse_fault(IntelIOMMUState
*s
, uint16_t source_id
)
497 hwaddr addr
= DMAR_FRCD_REG_OFFSET
+ 8; /* The high 64-bit half */
499 for (i
= 0; i
< DMAR_FRCD_REG_NR
; i
++) {
500 frcd_reg
= vtd_get_quad_raw(s
, addr
);
501 if ((frcd_reg
& VTD_FRCD_F
) &&
502 ((frcd_reg
& VTD_FRCD_SID_MASK
) == source_id
)) {
505 addr
+= 16; /* 128-bit for each */
510 /* Log and report an DMAR (address translation) fault to software */
511 static void vtd_report_dmar_fault(IntelIOMMUState
*s
, uint16_t source_id
,
512 hwaddr addr
, VTDFaultReason fault
,
513 bool is_write
, bool is_pasid
,
516 uint32_t fsts_reg
= vtd_get_long_raw(s
, DMAR_FSTS_REG
);
518 assert(fault
< VTD_FR_MAX
);
520 trace_vtd_dmar_fault(source_id
, fault
, addr
, is_write
);
522 if (fsts_reg
& VTD_FSTS_PFO
) {
523 error_report_once("New fault is not recorded due to "
524 "Primary Fault Overflow");
528 if (vtd_try_collapse_fault(s
, source_id
)) {
529 error_report_once("New fault is not recorded due to "
530 "compression of faults");
534 if (vtd_is_frcd_set(s
, s
->next_frcd_reg
)) {
535 error_report_once("Next Fault Recording Reg is used, "
536 "new fault is not recorded, set PFO field");
537 vtd_set_clear_mask_long(s
, DMAR_FSTS_REG
, 0, VTD_FSTS_PFO
);
541 vtd_record_frcd(s
, s
->next_frcd_reg
, source_id
, addr
, fault
,
542 is_write
, is_pasid
, pasid
);
544 if (fsts_reg
& VTD_FSTS_PPF
) {
545 error_report_once("There are pending faults already, "
546 "fault event is not generated");
547 vtd_set_frcd_and_update_ppf(s
, s
->next_frcd_reg
);
549 if (s
->next_frcd_reg
== DMAR_FRCD_REG_NR
) {
550 s
->next_frcd_reg
= 0;
553 vtd_set_clear_mask_long(s
, DMAR_FSTS_REG
, VTD_FSTS_FRI_MASK
,
554 VTD_FSTS_FRI(s
->next_frcd_reg
));
555 vtd_set_frcd_and_update_ppf(s
, s
->next_frcd_reg
); /* Will set PPF */
557 if (s
->next_frcd_reg
== DMAR_FRCD_REG_NR
) {
558 s
->next_frcd_reg
= 0;
560 /* This case actually cause the PPF to be Set.
561 * So generate fault event (interrupt).
563 vtd_generate_fault_event(s
, fsts_reg
);
567 /* Handle Invalidation Queue Errors of queued invalidation interface error
570 static void vtd_handle_inv_queue_error(IntelIOMMUState
*s
)
572 uint32_t fsts_reg
= vtd_get_long_raw(s
, DMAR_FSTS_REG
);
574 vtd_set_clear_mask_long(s
, DMAR_FSTS_REG
, 0, VTD_FSTS_IQE
);
575 vtd_generate_fault_event(s
, fsts_reg
);
578 /* Set the IWC field and try to generate an invalidation completion interrupt */
579 static void vtd_generate_completion_event(IntelIOMMUState
*s
)
581 if (vtd_get_long_raw(s
, DMAR_ICS_REG
) & VTD_ICS_IWC
) {
582 trace_vtd_inv_desc_wait_irq("One pending, skip current");
585 vtd_set_clear_mask_long(s
, DMAR_ICS_REG
, 0, VTD_ICS_IWC
);
586 vtd_set_clear_mask_long(s
, DMAR_IECTL_REG
, 0, VTD_IECTL_IP
);
587 if (vtd_get_long_raw(s
, DMAR_IECTL_REG
) & VTD_IECTL_IM
) {
588 trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
589 "new event not generated");
592 /* Generate the interrupt event */
593 trace_vtd_inv_desc_wait_irq("Generating complete event");
594 vtd_generate_interrupt(s
, DMAR_IEADDR_REG
, DMAR_IEDATA_REG
);
595 vtd_set_clear_mask_long(s
, DMAR_IECTL_REG
, VTD_IECTL_IP
, 0);
599 static inline bool vtd_root_entry_present(IntelIOMMUState
*s
,
603 if (s
->root_scalable
&& devfn
> UINT8_MAX
/ 2) {
604 return re
->hi
& VTD_ROOT_ENTRY_P
;
607 return re
->lo
& VTD_ROOT_ENTRY_P
;
610 static int vtd_get_root_entry(IntelIOMMUState
*s
, uint8_t index
,
615 addr
= s
->root
+ index
* sizeof(*re
);
616 if (dma_memory_read(&address_space_memory
, addr
,
617 re
, sizeof(*re
), MEMTXATTRS_UNSPECIFIED
)) {
619 return -VTD_FR_ROOT_TABLE_INV
;
621 re
->lo
= le64_to_cpu(re
->lo
);
622 re
->hi
= le64_to_cpu(re
->hi
);
626 static inline bool vtd_ce_present(VTDContextEntry
*context
)
628 return context
->lo
& VTD_CONTEXT_ENTRY_P
;
631 static int vtd_get_context_entry_from_root(IntelIOMMUState
*s
,
636 dma_addr_t addr
, ce_size
;
638 /* we have checked that root entry is present */
639 ce_size
= s
->root_scalable
? VTD_CTX_ENTRY_SCALABLE_SIZE
:
640 VTD_CTX_ENTRY_LEGACY_SIZE
;
642 if (s
->root_scalable
&& index
> UINT8_MAX
/ 2) {
643 index
= index
& (~VTD_DEVFN_CHECK_MASK
);
644 addr
= re
->hi
& VTD_ROOT_ENTRY_CTP
;
646 addr
= re
->lo
& VTD_ROOT_ENTRY_CTP
;
649 addr
= addr
+ index
* ce_size
;
650 if (dma_memory_read(&address_space_memory
, addr
,
651 ce
, ce_size
, MEMTXATTRS_UNSPECIFIED
)) {
652 return -VTD_FR_CONTEXT_TABLE_INV
;
655 ce
->lo
= le64_to_cpu(ce
->lo
);
656 ce
->hi
= le64_to_cpu(ce
->hi
);
657 if (ce_size
== VTD_CTX_ENTRY_SCALABLE_SIZE
) {
658 ce
->val
[2] = le64_to_cpu(ce
->val
[2]);
659 ce
->val
[3] = le64_to_cpu(ce
->val
[3]);
664 static inline dma_addr_t
vtd_ce_get_slpt_base(VTDContextEntry
*ce
)
666 return ce
->lo
& VTD_CONTEXT_ENTRY_SLPTPTR
;
669 static inline uint64_t vtd_get_slpte_addr(uint64_t slpte
, uint8_t aw
)
671 return slpte
& VTD_SL_PT_BASE_ADDR_MASK(aw
);
674 /* Whether the pte indicates the address of the page frame */
675 static inline bool vtd_is_last_slpte(uint64_t slpte
, uint32_t level
)
677 return level
== VTD_SL_PT_LEVEL
|| (slpte
& VTD_SL_PT_PAGE_SIZE_MASK
);
680 /* Get the content of a spte located in @base_addr[@index] */
681 static uint64_t vtd_get_slpte(dma_addr_t base_addr
, uint32_t index
)
685 assert(index
< VTD_SL_PT_ENTRY_NR
);
687 if (dma_memory_read(&address_space_memory
,
688 base_addr
+ index
* sizeof(slpte
),
689 &slpte
, sizeof(slpte
), MEMTXATTRS_UNSPECIFIED
)) {
690 slpte
= (uint64_t)-1;
693 slpte
= le64_to_cpu(slpte
);
697 /* Given an iova and the level of paging structure, return the offset
700 static inline uint32_t vtd_iova_level_offset(uint64_t iova
, uint32_t level
)
702 return (iova
>> vtd_slpt_level_shift(level
)) &
703 ((1ULL << VTD_SL_LEVEL_BITS
) - 1);
706 /* Check Capability Register to see if the @level of page-table is supported */
707 static inline bool vtd_is_level_supported(IntelIOMMUState
*s
, uint32_t level
)
709 return VTD_CAP_SAGAW_MASK
& s
->cap
&
710 (1ULL << (level
- 2 + VTD_CAP_SAGAW_SHIFT
));
713 /* Return true if check passed, otherwise false */
714 static inline bool vtd_pe_type_check(X86IOMMUState
*x86_iommu
,
717 switch (VTD_PE_GET_TYPE(pe
)) {
718 case VTD_SM_PASID_ENTRY_FLT
:
719 case VTD_SM_PASID_ENTRY_SLT
:
720 case VTD_SM_PASID_ENTRY_NESTED
:
722 case VTD_SM_PASID_ENTRY_PT
:
723 if (!x86_iommu
->pt_supported
) {
734 static inline bool vtd_pdire_present(VTDPASIDDirEntry
*pdire
)
736 return pdire
->val
& 1;
740 * Caller of this function should check present bit if wants
741 * to use pdir entry for further usage except for fpd bit check.
743 static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base
,
745 VTDPASIDDirEntry
*pdire
)
748 dma_addr_t addr
, entry_size
;
750 index
= VTD_PASID_DIR_INDEX(pasid
);
751 entry_size
= VTD_PASID_DIR_ENTRY_SIZE
;
752 addr
= pasid_dir_base
+ index
* entry_size
;
753 if (dma_memory_read(&address_space_memory
, addr
,
754 pdire
, entry_size
, MEMTXATTRS_UNSPECIFIED
)) {
755 return -VTD_FR_PASID_TABLE_INV
;
761 static inline bool vtd_pe_present(VTDPASIDEntry
*pe
)
763 return pe
->val
[0] & VTD_PASID_ENTRY_P
;
766 static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState
*s
,
772 dma_addr_t entry_size
;
773 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
775 index
= VTD_PASID_TABLE_INDEX(pasid
);
776 entry_size
= VTD_PASID_ENTRY_SIZE
;
777 addr
= addr
+ index
* entry_size
;
778 if (dma_memory_read(&address_space_memory
, addr
,
779 pe
, entry_size
, MEMTXATTRS_UNSPECIFIED
)) {
780 return -VTD_FR_PASID_TABLE_INV
;
783 /* Do translation type check */
784 if (!vtd_pe_type_check(x86_iommu
, pe
)) {
785 return -VTD_FR_PASID_TABLE_INV
;
788 if (!vtd_is_level_supported(s
, VTD_PE_GET_LEVEL(pe
))) {
789 return -VTD_FR_PASID_TABLE_INV
;
796 * Caller of this function should check present bit if wants
797 * to use pasid entry for further usage except for fpd bit check.
799 static int vtd_get_pe_from_pdire(IntelIOMMUState
*s
,
801 VTDPASIDDirEntry
*pdire
,
804 dma_addr_t addr
= pdire
->val
& VTD_PASID_TABLE_BASE_ADDR_MASK
;
806 return vtd_get_pe_in_pasid_leaf_table(s
, pasid
, addr
, pe
);
810 * This function gets a pasid entry from a specified pasid
811 * table (includes dir and leaf table) with a specified pasid.
812 * Sanity check should be done to ensure return a present
813 * pasid entry to caller.
815 static int vtd_get_pe_from_pasid_table(IntelIOMMUState
*s
,
816 dma_addr_t pasid_dir_base
,
821 VTDPASIDDirEntry pdire
;
823 ret
= vtd_get_pdire_from_pdir_table(pasid_dir_base
,
829 if (!vtd_pdire_present(&pdire
)) {
830 return -VTD_FR_PASID_TABLE_INV
;
833 ret
= vtd_get_pe_from_pdire(s
, pasid
, &pdire
, pe
);
838 if (!vtd_pe_present(pe
)) {
839 return -VTD_FR_PASID_TABLE_INV
;
845 static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState
*s
,
850 dma_addr_t pasid_dir_base
;
853 if (pasid
== PCI_NO_PASID
) {
854 pasid
= VTD_CE_GET_RID2PASID(ce
);
856 pasid_dir_base
= VTD_CE_GET_PASID_DIR_TABLE(ce
);
857 ret
= vtd_get_pe_from_pasid_table(s
, pasid_dir_base
, pasid
, pe
);
862 static int vtd_ce_get_pasid_fpd(IntelIOMMUState
*s
,
868 dma_addr_t pasid_dir_base
;
869 VTDPASIDDirEntry pdire
;
872 if (pasid
== PCI_NO_PASID
) {
873 pasid
= VTD_CE_GET_RID2PASID(ce
);
875 pasid_dir_base
= VTD_CE_GET_PASID_DIR_TABLE(ce
);
878 * No present bit check since fpd is meaningful even
879 * if the present bit is clear.
881 ret
= vtd_get_pdire_from_pdir_table(pasid_dir_base
, pasid
, &pdire
);
886 if (pdire
.val
& VTD_PASID_DIR_FPD
) {
891 if (!vtd_pdire_present(&pdire
)) {
892 return -VTD_FR_PASID_TABLE_INV
;
896 * No present bit check since fpd is meaningful even
897 * if the present bit is clear.
899 ret
= vtd_get_pe_from_pdire(s
, pasid
, &pdire
, &pe
);
904 if (pe
.val
[0] & VTD_PASID_ENTRY_FPD
) {
911 /* Get the page-table level that hardware should use for the second-level
912 * page-table walk from the Address Width field of context-entry.
914 static inline uint32_t vtd_ce_get_level(VTDContextEntry
*ce
)
916 return 2 + (ce
->hi
& VTD_CONTEXT_ENTRY_AW
);
919 static uint32_t vtd_get_iova_level(IntelIOMMUState
*s
,
925 if (s
->root_scalable
) {
926 vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, pasid
);
927 return VTD_PE_GET_LEVEL(&pe
);
930 return vtd_ce_get_level(ce
);
933 static inline uint32_t vtd_ce_get_agaw(VTDContextEntry
*ce
)
935 return 30 + (ce
->hi
& VTD_CONTEXT_ENTRY_AW
) * 9;
938 static uint32_t vtd_get_iova_agaw(IntelIOMMUState
*s
,
944 if (s
->root_scalable
) {
945 vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, pasid
);
946 return 30 + ((pe
.val
[0] >> 2) & VTD_SM_PASID_ENTRY_AW
) * 9;
949 return vtd_ce_get_agaw(ce
);
952 static inline uint32_t vtd_ce_get_type(VTDContextEntry
*ce
)
954 return ce
->lo
& VTD_CONTEXT_ENTRY_TT
;
957 /* Only for Legacy Mode. Return true if check passed, otherwise false */
958 static inline bool vtd_ce_type_check(X86IOMMUState
*x86_iommu
,
961 switch (vtd_ce_get_type(ce
)) {
962 case VTD_CONTEXT_TT_MULTI_LEVEL
:
963 /* Always supported */
965 case VTD_CONTEXT_TT_DEV_IOTLB
:
966 if (!x86_iommu
->dt_supported
) {
967 error_report_once("%s: DT specified but not supported", __func__
);
971 case VTD_CONTEXT_TT_PASS_THROUGH
:
972 if (!x86_iommu
->pt_supported
) {
973 error_report_once("%s: PT specified but not supported", __func__
);
979 error_report_once("%s: unknown ce type: %"PRIu32
, __func__
,
980 vtd_ce_get_type(ce
));
986 static inline uint64_t vtd_iova_limit(IntelIOMMUState
*s
,
987 VTDContextEntry
*ce
, uint8_t aw
,
990 uint32_t ce_agaw
= vtd_get_iova_agaw(s
, ce
, pasid
);
991 return 1ULL << MIN(ce_agaw
, aw
);
994 /* Return true if IOVA passes range check, otherwise false. */
995 static inline bool vtd_iova_range_check(IntelIOMMUState
*s
,
996 uint64_t iova
, VTDContextEntry
*ce
,
997 uint8_t aw
, uint32_t pasid
)
1000 * Check if @iova is above 2^X-1, where X is the minimum of MGAW
1001 * in CAP_REG and AW in context-entry.
1003 return !(iova
& ~(vtd_iova_limit(s
, ce
, aw
, pasid
) - 1));
1006 static dma_addr_t
vtd_get_iova_pgtbl_base(IntelIOMMUState
*s
,
1007 VTDContextEntry
*ce
,
1012 if (s
->root_scalable
) {
1013 vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, pasid
);
1014 return pe
.val
[0] & VTD_SM_PASID_ENTRY_SLPTPTR
;
1017 return vtd_ce_get_slpt_base(ce
);
1021 * Rsvd field masks for spte:
1022 * vtd_spte_rsvd 4k pages
1023 * vtd_spte_rsvd_large large pages
1025 static uint64_t vtd_spte_rsvd
[5];
1026 static uint64_t vtd_spte_rsvd_large
[5];
1028 static bool vtd_slpte_nonzero_rsvd(uint64_t slpte
, uint32_t level
)
1030 uint64_t rsvd_mask
= vtd_spte_rsvd
[level
];
1032 if ((level
== VTD_SL_PD_LEVEL
|| level
== VTD_SL_PDP_LEVEL
) &&
1033 (slpte
& VTD_SL_PT_PAGE_SIZE_MASK
)) {
1035 rsvd_mask
= vtd_spte_rsvd_large
[level
];
1038 return slpte
& rsvd_mask
;
1041 /* Given the @iova, get relevant @slptep. @slpte_level will be the last level
1042 * of the translation, can be used for deciding the size of large page.
1044 static int vtd_iova_to_slpte(IntelIOMMUState
*s
, VTDContextEntry
*ce
,
1045 uint64_t iova
, bool is_write
,
1046 uint64_t *slptep
, uint32_t *slpte_level
,
1047 bool *reads
, bool *writes
, uint8_t aw_bits
,
1050 dma_addr_t addr
= vtd_get_iova_pgtbl_base(s
, ce
, pasid
);
1051 uint32_t level
= vtd_get_iova_level(s
, ce
, pasid
);
1054 uint64_t access_right_check
;
1055 uint64_t xlat
, size
;
1057 if (!vtd_iova_range_check(s
, iova
, ce
, aw_bits
, pasid
)) {
1058 error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64
","
1059 "pasid=0x%" PRIx32
")", __func__
, iova
, pasid
);
1060 return -VTD_FR_ADDR_BEYOND_MGAW
;
1063 /* FIXME: what is the Atomics request here? */
1064 access_right_check
= is_write
? VTD_SL_W
: VTD_SL_R
;
1067 offset
= vtd_iova_level_offset(iova
, level
);
1068 slpte
= vtd_get_slpte(addr
, offset
);
1070 if (slpte
== (uint64_t)-1) {
1071 error_report_once("%s: detected read error on DMAR slpte "
1072 "(iova=0x%" PRIx64
", pasid=0x%" PRIx32
")",
1073 __func__
, iova
, pasid
);
1074 if (level
== vtd_get_iova_level(s
, ce
, pasid
)) {
1075 /* Invalid programming of context-entry */
1076 return -VTD_FR_CONTEXT_ENTRY_INV
;
1078 return -VTD_FR_PAGING_ENTRY_INV
;
1081 *reads
= (*reads
) && (slpte
& VTD_SL_R
);
1082 *writes
= (*writes
) && (slpte
& VTD_SL_W
);
1083 if (!(slpte
& access_right_check
)) {
1084 error_report_once("%s: detected slpte permission error "
1085 "(iova=0x%" PRIx64
", level=0x%" PRIx32
", "
1086 "slpte=0x%" PRIx64
", write=%d, pasid=0x%"
1087 PRIx32
")", __func__
, iova
, level
,
1088 slpte
, is_write
, pasid
);
1089 return is_write
? -VTD_FR_WRITE
: -VTD_FR_READ
;
1091 if (vtd_slpte_nonzero_rsvd(slpte
, level
)) {
1092 error_report_once("%s: detected splte reserve non-zero "
1093 "iova=0x%" PRIx64
", level=0x%" PRIx32
1094 "slpte=0x%" PRIx64
", pasid=0x%" PRIX32
")",
1095 __func__
, iova
, level
, slpte
, pasid
);
1096 return -VTD_FR_PAGING_ENTRY_RSVD
;
1099 if (vtd_is_last_slpte(slpte
, level
)) {
1101 *slpte_level
= level
;
1104 addr
= vtd_get_slpte_addr(slpte
, aw_bits
);
1108 xlat
= vtd_get_slpte_addr(*slptep
, aw_bits
);
1109 size
= ~vtd_slpt_level_page_mask(level
) + 1;
1112 * From VT-d spec 3.14: Untranslated requests and translation
1113 * requests that result in an address in the interrupt range will be
1114 * blocked with condition code LGN.4 or SGN.8.
1116 if ((xlat
> VTD_INTERRUPT_ADDR_LAST
||
1117 xlat
+ size
- 1 < VTD_INTERRUPT_ADDR_FIRST
)) {
1120 error_report_once("%s: xlat address is in interrupt range "
1121 "(iova=0x%" PRIx64
", level=0x%" PRIx32
", "
1122 "slpte=0x%" PRIx64
", write=%d, "
1123 "xlat=0x%" PRIx64
", size=0x%" PRIx64
", "
1124 "pasid=0x%" PRIx32
")",
1125 __func__
, iova
, level
, slpte
, is_write
,
1127 return s
->scalable_mode
? -VTD_FR_SM_INTERRUPT_ADDR
:
1128 -VTD_FR_INTERRUPT_ADDR
;
1132 typedef int (*vtd_page_walk_hook
)(IOMMUTLBEvent
*event
, void *private);
1135 * Constant information used during page walking
1137 * @hook_fn: hook func to be called when detected page
1138 * @private: private data to be passed into hook func
1139 * @notify_unmap: whether we should notify invalid entries
1140 * @as: VT-d address space of the device
1141 * @aw: maximum address width
1142 * @domain: domain ID of the page walk
1145 VTDAddressSpace
*as
;
1146 vtd_page_walk_hook hook_fn
;
1151 } vtd_page_walk_info
;
1153 static int vtd_page_walk_one(IOMMUTLBEvent
*event
, vtd_page_walk_info
*info
)
1155 VTDAddressSpace
*as
= info
->as
;
1156 vtd_page_walk_hook hook_fn
= info
->hook_fn
;
1157 void *private = info
->private;
1158 IOMMUTLBEntry
*entry
= &event
->entry
;
1160 .iova
= entry
->iova
,
1161 .size
= entry
->addr_mask
,
1162 .translated_addr
= entry
->translated_addr
,
1163 .perm
= entry
->perm
,
1165 const DMAMap
*mapped
= iova_tree_find(as
->iova_tree
, &target
);
1167 if (event
->type
== IOMMU_NOTIFIER_UNMAP
&& !info
->notify_unmap
) {
1168 trace_vtd_page_walk_one_skip_unmap(entry
->iova
, entry
->addr_mask
);
1174 /* Update local IOVA mapped ranges */
1175 if (event
->type
== IOMMU_NOTIFIER_MAP
) {
1177 /* If it's exactly the same translation, skip */
1178 if (!memcmp(mapped
, &target
, sizeof(target
))) {
1179 trace_vtd_page_walk_one_skip_map(entry
->iova
, entry
->addr_mask
,
1180 entry
->translated_addr
);
1184 * Translation changed. Normally this should not
1185 * happen, but it can happen when with buggy guest
1186 * OSes. Note that there will be a small window that
1187 * we don't have map at all. But that's the best
1188 * effort we can do. The ideal way to emulate this is
1189 * atomically modify the PTE to follow what has
1190 * changed, but we can't. One example is that vfio
1191 * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
1192 * interface to modify a mapping (meanwhile it seems
1193 * meaningless to even provide one). Anyway, let's
1194 * mark this as a TODO in case one day we'll have
1195 * a better solution.
1197 IOMMUAccessFlags cache_perm
= entry
->perm
;
1200 /* Emulate an UNMAP */
1201 event
->type
= IOMMU_NOTIFIER_UNMAP
;
1202 entry
->perm
= IOMMU_NONE
;
1203 trace_vtd_page_walk_one(info
->domain_id
,
1205 entry
->translated_addr
,
1208 ret
= hook_fn(event
, private);
1212 /* Drop any existing mapping */
1213 iova_tree_remove(as
->iova_tree
, target
);
1214 /* Recover the correct type */
1215 event
->type
= IOMMU_NOTIFIER_MAP
;
1216 entry
->perm
= cache_perm
;
1219 iova_tree_insert(as
->iova_tree
, &target
);
1222 /* Skip since we didn't map this range at all */
1223 trace_vtd_page_walk_one_skip_unmap(entry
->iova
, entry
->addr_mask
);
1226 iova_tree_remove(as
->iova_tree
, target
);
1229 trace_vtd_page_walk_one(info
->domain_id
, entry
->iova
,
1230 entry
->translated_addr
, entry
->addr_mask
,
1232 return hook_fn(event
, private);
1236 * vtd_page_walk_level - walk over specific level for IOVA range
1238 * @addr: base GPA addr to start the walk
1239 * @start: IOVA range start address
1240 * @end: IOVA range end address (start <= addr < end)
1241 * @read: whether parent level has read permission
1242 * @write: whether parent level has write permission
1243 * @info: constant information for the page walk
1245 static int vtd_page_walk_level(dma_addr_t addr
, uint64_t start
,
1246 uint64_t end
, uint32_t level
, bool read
,
1247 bool write
, vtd_page_walk_info
*info
)
1249 bool read_cur
, write_cur
, entry_valid
;
1252 uint64_t subpage_size
, subpage_mask
;
1253 IOMMUTLBEvent event
;
1254 uint64_t iova
= start
;
1258 trace_vtd_page_walk_level(addr
, level
, start
, end
);
1260 subpage_size
= 1ULL << vtd_slpt_level_shift(level
);
1261 subpage_mask
= vtd_slpt_level_page_mask(level
);
1263 while (iova
< end
) {
1264 iova_next
= (iova
& subpage_mask
) + subpage_size
;
1266 offset
= vtd_iova_level_offset(iova
, level
);
1267 slpte
= vtd_get_slpte(addr
, offset
);
1269 if (slpte
== (uint64_t)-1) {
1270 trace_vtd_page_walk_skip_read(iova
, iova_next
);
1274 if (vtd_slpte_nonzero_rsvd(slpte
, level
)) {
1275 trace_vtd_page_walk_skip_reserve(iova
, iova_next
);
1279 /* Permissions are stacked with parents' */
1280 read_cur
= read
&& (slpte
& VTD_SL_R
);
1281 write_cur
= write
&& (slpte
& VTD_SL_W
);
1284 * As long as we have either read/write permission, this is a
1285 * valid entry. The rule works for both page entries and page
1288 entry_valid
= read_cur
| write_cur
;
1290 if (!vtd_is_last_slpte(slpte
, level
) && entry_valid
) {
1292 * This is a valid PDE (or even bigger than PDE). We need
1293 * to walk one further level.
1295 ret
= vtd_page_walk_level(vtd_get_slpte_addr(slpte
, info
->aw
),
1296 iova
, MIN(iova_next
, end
), level
- 1,
1297 read_cur
, write_cur
, info
);
1300 * This means we are either:
1302 * (1) the real page entry (either 4K page, or huge page)
1303 * (2) the whole range is invalid
1305 * In either case, we send an IOTLB notification down.
1307 event
.entry
.target_as
= &address_space_memory
;
1308 event
.entry
.iova
= iova
& subpage_mask
;
1309 event
.entry
.perm
= IOMMU_ACCESS_FLAG(read_cur
, write_cur
);
1310 event
.entry
.addr_mask
= ~subpage_mask
;
1311 /* NOTE: this is only meaningful if entry_valid == true */
1312 event
.entry
.translated_addr
= vtd_get_slpte_addr(slpte
, info
->aw
);
1313 event
.type
= event
.entry
.perm
? IOMMU_NOTIFIER_MAP
:
1314 IOMMU_NOTIFIER_UNMAP
;
1315 ret
= vtd_page_walk_one(&event
, info
);
1330 * vtd_page_walk - walk specific IOVA range, and call the hook
1332 * @s: intel iommu state
1333 * @ce: context entry to walk upon
1334 * @start: IOVA address to start the walk
1335 * @end: IOVA range end address (start <= addr < end)
1336 * @info: page walking information struct
1338 static int vtd_page_walk(IntelIOMMUState
*s
, VTDContextEntry
*ce
,
1339 uint64_t start
, uint64_t end
,
1340 vtd_page_walk_info
*info
,
1343 dma_addr_t addr
= vtd_get_iova_pgtbl_base(s
, ce
, pasid
);
1344 uint32_t level
= vtd_get_iova_level(s
, ce
, pasid
);
1346 if (!vtd_iova_range_check(s
, start
, ce
, info
->aw
, pasid
)) {
1347 return -VTD_FR_ADDR_BEYOND_MGAW
;
1350 if (!vtd_iova_range_check(s
, end
, ce
, info
->aw
, pasid
)) {
1351 /* Fix end so that it reaches the maximum */
1352 end
= vtd_iova_limit(s
, ce
, info
->aw
, pasid
);
1355 return vtd_page_walk_level(addr
, start
, end
, level
, true, true, info
);
1358 static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState
*s
,
1361 /* Legacy Mode reserved bits check */
1362 if (!s
->root_scalable
&&
1363 (re
->hi
|| (re
->lo
& VTD_ROOT_ENTRY_RSVD(s
->aw_bits
))))
1366 /* Scalable Mode reserved bits check */
1367 if (s
->root_scalable
&&
1368 ((re
->lo
& VTD_ROOT_ENTRY_RSVD(s
->aw_bits
)) ||
1369 (re
->hi
& VTD_ROOT_ENTRY_RSVD(s
->aw_bits
))))
1375 error_report_once("%s: invalid root entry: hi=0x%"PRIx64
1377 __func__
, re
->hi
, re
->lo
);
1378 return -VTD_FR_ROOT_ENTRY_RSVD
;
1381 static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState
*s
,
1382 VTDContextEntry
*ce
)
1384 if (!s
->root_scalable
&&
1385 (ce
->hi
& VTD_CONTEXT_ENTRY_RSVD_HI
||
1386 ce
->lo
& VTD_CONTEXT_ENTRY_RSVD_LO(s
->aw_bits
))) {
1387 error_report_once("%s: invalid context entry: hi=%"PRIx64
1388 ", lo=%"PRIx64
" (reserved nonzero)",
1389 __func__
, ce
->hi
, ce
->lo
);
1390 return -VTD_FR_CONTEXT_ENTRY_RSVD
;
1393 if (s
->root_scalable
&&
1394 (ce
->val
[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s
->aw_bits
) ||
1395 ce
->val
[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1
||
1398 error_report_once("%s: invalid context entry: val[3]=%"PRIx64
1401 ", val[0]=%"PRIx64
" (reserved nonzero)",
1402 __func__
, ce
->val
[3], ce
->val
[2],
1403 ce
->val
[1], ce
->val
[0]);
1404 return -VTD_FR_CONTEXT_ENTRY_RSVD
;
1410 static int vtd_ce_rid2pasid_check(IntelIOMMUState
*s
,
1411 VTDContextEntry
*ce
)
1416 * Make sure in Scalable Mode, a present context entry
1417 * has valid rid2pasid setting, which includes valid
1418 * rid2pasid field and corresponding pasid entry setting
1420 return vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, PCI_NO_PASID
);
1423 /* Map a device to its corresponding domain (context-entry) */
1424 static int vtd_dev_to_context_entry(IntelIOMMUState
*s
, uint8_t bus_num
,
1425 uint8_t devfn
, VTDContextEntry
*ce
)
1429 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
1431 ret_fr
= vtd_get_root_entry(s
, bus_num
, &re
);
1436 if (!vtd_root_entry_present(s
, &re
, devfn
)) {
1437 /* Not error - it's okay we don't have root entry. */
1438 trace_vtd_re_not_present(bus_num
);
1439 return -VTD_FR_ROOT_ENTRY_P
;
1442 ret_fr
= vtd_root_entry_rsvd_bits_check(s
, &re
);
1447 ret_fr
= vtd_get_context_entry_from_root(s
, &re
, devfn
, ce
);
1452 if (!vtd_ce_present(ce
)) {
1453 /* Not error - it's okay we don't have context entry. */
1454 trace_vtd_ce_not_present(bus_num
, devfn
);
1455 return -VTD_FR_CONTEXT_ENTRY_P
;
1458 ret_fr
= vtd_context_entry_rsvd_bits_check(s
, ce
);
1463 /* Check if the programming of context-entry is valid */
1464 if (!s
->root_scalable
&&
1465 !vtd_is_level_supported(s
, vtd_ce_get_level(ce
))) {
1466 error_report_once("%s: invalid context entry: hi=%"PRIx64
1467 ", lo=%"PRIx64
" (level %d not supported)",
1468 __func__
, ce
->hi
, ce
->lo
,
1469 vtd_ce_get_level(ce
));
1470 return -VTD_FR_CONTEXT_ENTRY_INV
;
1473 if (!s
->root_scalable
) {
1474 /* Do translation type check */
1475 if (!vtd_ce_type_check(x86_iommu
, ce
)) {
1476 /* Errors dumped in vtd_ce_type_check() */
1477 return -VTD_FR_CONTEXT_ENTRY_INV
;
1481 * Check if the programming of context-entry.rid2pasid
1482 * and corresponding pasid setting is valid, and thus
1483 * avoids to check pasid entry fetching result in future
1484 * helper function calling.
1486 ret_fr
= vtd_ce_rid2pasid_check(s
, ce
);
1495 static int vtd_sync_shadow_page_hook(IOMMUTLBEvent
*event
,
1498 memory_region_notify_iommu(private, 0, *event
);
1502 static uint16_t vtd_get_domain_id(IntelIOMMUState
*s
,
1503 VTDContextEntry
*ce
,
1508 if (s
->root_scalable
) {
1509 vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, pasid
);
1510 return VTD_SM_PASID_ENTRY_DID(pe
.val
[1]);
1513 return VTD_CONTEXT_ENTRY_DID(ce
->hi
);
1516 static int vtd_sync_shadow_page_table_range(VTDAddressSpace
*vtd_as
,
1517 VTDContextEntry
*ce
,
1518 hwaddr addr
, hwaddr size
)
1520 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
1521 vtd_page_walk_info info
= {
1522 .hook_fn
= vtd_sync_shadow_page_hook
,
1523 .private = (void *)&vtd_as
->iommu
,
1524 .notify_unmap
= true,
1527 .domain_id
= vtd_get_domain_id(s
, ce
, vtd_as
->pasid
),
1530 return vtd_page_walk(s
, ce
, addr
, addr
+ size
, &info
, vtd_as
->pasid
);
1533 static int vtd_address_space_sync(VTDAddressSpace
*vtd_as
)
1539 /* If no MAP notifier registered, we simply invalidate all the cache */
1540 if (!vtd_as_has_map_notifier(vtd_as
)) {
1541 IOMMU_NOTIFIER_FOREACH(n
, &vtd_as
->iommu
) {
1542 memory_region_unmap_iommu_notifier_range(n
);
1547 ret
= vtd_dev_to_context_entry(vtd_as
->iommu_state
,
1548 pci_bus_num(vtd_as
->bus
),
1549 vtd_as
->devfn
, &ce
);
1551 if (ret
== -VTD_FR_CONTEXT_ENTRY_P
) {
1553 * It's a valid scenario to have a context entry that is
1554 * not present. For example, when a device is removed
1555 * from an existing domain then the context entry will be
1556 * zeroed by the guest before it was put into another
1557 * domain. When this happens, instead of synchronizing
1558 * the shadow pages we should invalidate all existing
1559 * mappings and notify the backends.
1561 IOMMU_NOTIFIER_FOREACH(n
, &vtd_as
->iommu
) {
1562 vtd_address_space_unmap(vtd_as
, n
);
1569 return vtd_sync_shadow_page_table_range(vtd_as
, &ce
, 0, UINT64_MAX
);
1573 * Check if specific device is configured to bypass address
1574 * translation for DMA requests. In Scalable Mode, bypass
1575 * 1st-level translation or 2nd-level translation, it depends
1578 static bool vtd_dev_pt_enabled(IntelIOMMUState
*s
, VTDContextEntry
*ce
,
1584 if (s
->root_scalable
) {
1585 ret
= vtd_ce_get_rid2pasid_entry(s
, ce
, &pe
, pasid
);
1588 * This error is guest triggerable. We should assumt PT
1589 * not enabled for safety.
1593 return (VTD_PE_GET_TYPE(&pe
) == VTD_SM_PASID_ENTRY_PT
);
1596 return (vtd_ce_get_type(ce
) == VTD_CONTEXT_TT_PASS_THROUGH
);
1600 static bool vtd_as_pt_enabled(VTDAddressSpace
*as
)
1607 s
= as
->iommu_state
;
1608 if (vtd_dev_to_context_entry(s
, pci_bus_num(as
->bus
), as
->devfn
,
1611 * Possibly failed to parse the context entry for some reason
1612 * (e.g., during init, or any guest configuration errors on
1613 * context entries). We should assume PT not enabled for
1619 return vtd_dev_pt_enabled(s
, &ce
, as
->pasid
);
1622 /* Return whether the device is using IOMMU translation. */
1623 static bool vtd_switch_address_space(VTDAddressSpace
*as
)
1626 /* Whether we need to take the BQL on our own */
1627 bool take_bql
= !qemu_mutex_iothread_locked();
1631 use_iommu
= as
->iommu_state
->dmar_enabled
&& !vtd_as_pt_enabled(as
);
1632 pt
= as
->iommu_state
->dmar_enabled
&& vtd_as_pt_enabled(as
);
1634 trace_vtd_switch_address_space(pci_bus_num(as
->bus
),
1635 VTD_PCI_SLOT(as
->devfn
),
1636 VTD_PCI_FUNC(as
->devfn
),
1640 * It's possible that we reach here without BQL, e.g., when called
1641 * from vtd_pt_enable_fast_path(). However the memory APIs need
1642 * it. We'd better make sure we have had it already, or, take it.
1645 qemu_mutex_lock_iothread();
1648 /* Turn off first then on the other */
1650 memory_region_set_enabled(&as
->nodmar
, false);
1651 memory_region_set_enabled(MEMORY_REGION(&as
->iommu
), true);
1653 * vt-d spec v3.4 3.14:
1656 * Requests-with-PASID with input address in range 0xFEEx_xxxx
1657 * are translated normally like any other request-with-PASID
1658 * through DMA-remapping hardware.
1661 * Need to disable ir for as with PASID.
1663 if (as
->pasid
!= PCI_NO_PASID
) {
1664 memory_region_set_enabled(&as
->iommu_ir
, false);
1666 memory_region_set_enabled(&as
->iommu_ir
, true);
1669 memory_region_set_enabled(MEMORY_REGION(&as
->iommu
), false);
1670 memory_region_set_enabled(&as
->nodmar
, true);
1674 * vtd-spec v3.4 3.14:
1677 * Requests-with-PASID with input address in range 0xFEEx_xxxx are
1678 * translated normally like any other request-with-PASID through
1679 * DMA-remapping hardware. However, if such a request is processed
1680 * using pass-through translation, it will be blocked as described
1681 * in the paragraph below.
1683 * Software must not program paging-structure entries to remap any
1684 * address to the interrupt address range. Untranslated requests
1685 * and translation requests that result in an address in the
1686 * interrupt range will be blocked with condition code LGN.4 or
1690 * We enable per as memory region (iommu_ir_fault) for catching
1691 * the tranlsation for interrupt range through PASID + PT.
1693 if (pt
&& as
->pasid
!= PCI_NO_PASID
) {
1694 memory_region_set_enabled(&as
->iommu_ir_fault
, true);
1696 memory_region_set_enabled(&as
->iommu_ir_fault
, false);
1700 qemu_mutex_unlock_iothread();
1706 static void vtd_switch_address_space_all(IntelIOMMUState
*s
)
1708 VTDAddressSpace
*vtd_as
;
1709 GHashTableIter iter
;
1711 g_hash_table_iter_init(&iter
, s
->vtd_address_spaces
);
1712 while (g_hash_table_iter_next(&iter
, NULL
, (void **)&vtd_as
)) {
1713 vtd_switch_address_space(vtd_as
);
1717 static const bool vtd_qualified_faults
[] = {
1718 [VTD_FR_RESERVED
] = false,
1719 [VTD_FR_ROOT_ENTRY_P
] = false,
1720 [VTD_FR_CONTEXT_ENTRY_P
] = true,
1721 [VTD_FR_CONTEXT_ENTRY_INV
] = true,
1722 [VTD_FR_ADDR_BEYOND_MGAW
] = true,
1723 [VTD_FR_WRITE
] = true,
1724 [VTD_FR_READ
] = true,
1725 [VTD_FR_PAGING_ENTRY_INV
] = true,
1726 [VTD_FR_ROOT_TABLE_INV
] = false,
1727 [VTD_FR_CONTEXT_TABLE_INV
] = false,
1728 [VTD_FR_INTERRUPT_ADDR
] = true,
1729 [VTD_FR_ROOT_ENTRY_RSVD
] = false,
1730 [VTD_FR_PAGING_ENTRY_RSVD
] = true,
1731 [VTD_FR_CONTEXT_ENTRY_TT
] = true,
1732 [VTD_FR_PASID_TABLE_INV
] = false,
1733 [VTD_FR_SM_INTERRUPT_ADDR
] = true,
1734 [VTD_FR_MAX
] = false,
1737 /* To see if a fault condition is "qualified", which is reported to software
1738 * only if the FPD field in the context-entry used to process the faulting
1741 static inline bool vtd_is_qualified_fault(VTDFaultReason fault
)
1743 return vtd_qualified_faults
[fault
];
1746 static inline bool vtd_is_interrupt_addr(hwaddr addr
)
1748 return VTD_INTERRUPT_ADDR_FIRST
<= addr
&& addr
<= VTD_INTERRUPT_ADDR_LAST
;
1751 static gboolean
vtd_find_as_by_sid(gpointer key
, gpointer value
,
1754 struct vtd_as_key
*as_key
= (struct vtd_as_key
*)key
;
1755 uint16_t target_sid
= *(uint16_t *)user_data
;
1756 uint16_t sid
= PCI_BUILD_BDF(pci_bus_num(as_key
->bus
), as_key
->devfn
);
1757 return sid
== target_sid
;
1760 static VTDAddressSpace
*vtd_get_as_by_sid(IntelIOMMUState
*s
, uint16_t sid
)
1762 uint8_t bus_num
= PCI_BUS_NUM(sid
);
1763 VTDAddressSpace
*vtd_as
= s
->vtd_as_cache
[bus_num
];
1766 (sid
== PCI_BUILD_BDF(pci_bus_num(vtd_as
->bus
), vtd_as
->devfn
))) {
1770 vtd_as
= g_hash_table_find(s
->vtd_address_spaces
, vtd_find_as_by_sid
, &sid
);
1771 s
->vtd_as_cache
[bus_num
] = vtd_as
;
1776 static void vtd_pt_enable_fast_path(IntelIOMMUState
*s
, uint16_t source_id
)
1778 VTDAddressSpace
*vtd_as
;
1779 bool success
= false;
1781 vtd_as
= vtd_get_as_by_sid(s
, source_id
);
1786 if (vtd_switch_address_space(vtd_as
) == false) {
1787 /* We switched off IOMMU region successfully. */
1792 trace_vtd_pt_enable_fast_path(source_id
, success
);
1795 static void vtd_report_fault(IntelIOMMUState
*s
,
1796 int err
, bool is_fpd_set
,
1803 if (is_fpd_set
&& vtd_is_qualified_fault(err
)) {
1804 trace_vtd_fault_disabled();
1806 vtd_report_dmar_fault(s
, source_id
, addr
, err
, is_write
,
1811 /* Map dev to context-entry then do a paging-structures walk to do a iommu
1814 * Called from RCU critical section.
1816 * @bus_num: The bus number
1817 * @devfn: The devfn, which is the combined of device and function number
1818 * @is_write: The access is a write operation
1819 * @entry: IOMMUTLBEntry that contain the addr to be translated and result
1821 * Returns true if translation is successful, otherwise false.
1823 static bool vtd_do_iommu_translate(VTDAddressSpace
*vtd_as
, PCIBus
*bus
,
1824 uint8_t devfn
, hwaddr addr
, bool is_write
,
1825 IOMMUTLBEntry
*entry
)
1827 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
1829 uint8_t bus_num
= pci_bus_num(bus
);
1830 VTDContextCacheEntry
*cc_entry
;
1831 uint64_t slpte
, page_mask
;
1832 uint32_t level
, pasid
= vtd_as
->pasid
;
1833 uint16_t source_id
= PCI_BUILD_BDF(bus_num
, devfn
);
1835 bool is_fpd_set
= false;
1838 uint8_t access_flags
;
1839 bool rid2pasid
= (pasid
== PCI_NO_PASID
) && s
->root_scalable
;
1840 VTDIOTLBEntry
*iotlb_entry
;
1843 * We have standalone memory region for interrupt addresses, we
1844 * should never receive translation requests in this region.
1846 assert(!vtd_is_interrupt_addr(addr
));
1850 cc_entry
= &vtd_as
->context_cache_entry
;
1852 /* Try to fetch slpte form IOTLB, we don't need RID2PASID logic */
1854 iotlb_entry
= vtd_lookup_iotlb(s
, source_id
, pasid
, addr
);
1856 trace_vtd_iotlb_page_hit(source_id
, addr
, iotlb_entry
->slpte
,
1857 iotlb_entry
->domain_id
);
1858 slpte
= iotlb_entry
->slpte
;
1859 access_flags
= iotlb_entry
->access_flags
;
1860 page_mask
= iotlb_entry
->mask
;
1865 /* Try to fetch context-entry from cache first */
1866 if (cc_entry
->context_cache_gen
== s
->context_cache_gen
) {
1867 trace_vtd_iotlb_cc_hit(bus_num
, devfn
, cc_entry
->context_entry
.hi
,
1868 cc_entry
->context_entry
.lo
,
1869 cc_entry
->context_cache_gen
);
1870 ce
= cc_entry
->context_entry
;
1871 is_fpd_set
= ce
.lo
& VTD_CONTEXT_ENTRY_FPD
;
1872 if (!is_fpd_set
&& s
->root_scalable
) {
1873 ret_fr
= vtd_ce_get_pasid_fpd(s
, &ce
, &is_fpd_set
, pasid
);
1875 vtd_report_fault(s
, -ret_fr
, is_fpd_set
,
1876 source_id
, addr
, is_write
,
1882 ret_fr
= vtd_dev_to_context_entry(s
, bus_num
, devfn
, &ce
);
1883 is_fpd_set
= ce
.lo
& VTD_CONTEXT_ENTRY_FPD
;
1884 if (!ret_fr
&& !is_fpd_set
&& s
->root_scalable
) {
1885 ret_fr
= vtd_ce_get_pasid_fpd(s
, &ce
, &is_fpd_set
, pasid
);
1888 vtd_report_fault(s
, -ret_fr
, is_fpd_set
,
1889 source_id
, addr
, is_write
,
1893 /* Update context-cache */
1894 trace_vtd_iotlb_cc_update(bus_num
, devfn
, ce
.hi
, ce
.lo
,
1895 cc_entry
->context_cache_gen
,
1896 s
->context_cache_gen
);
1897 cc_entry
->context_entry
= ce
;
1898 cc_entry
->context_cache_gen
= s
->context_cache_gen
;
1902 pasid
= VTD_CE_GET_RID2PASID(&ce
);
1906 * We don't need to translate for pass-through context entries.
1907 * Also, let's ignore IOTLB caching as well for PT devices.
1909 if (vtd_dev_pt_enabled(s
, &ce
, pasid
)) {
1910 entry
->iova
= addr
& VTD_PAGE_MASK_4K
;
1911 entry
->translated_addr
= entry
->iova
;
1912 entry
->addr_mask
= ~VTD_PAGE_MASK_4K
;
1913 entry
->perm
= IOMMU_RW
;
1914 trace_vtd_translate_pt(source_id
, entry
->iova
);
1917 * When this happens, it means firstly caching-mode is not
1918 * enabled, and this is the first passthrough translation for
1919 * the device. Let's enable the fast path for passthrough.
1921 * When passthrough is disabled again for the device, we can
1922 * capture it via the context entry invalidation, then the
1923 * IOMMU region can be swapped back.
1925 vtd_pt_enable_fast_path(s
, source_id
);
1926 vtd_iommu_unlock(s
);
1930 /* Try to fetch slpte form IOTLB for RID2PASID slow path */
1932 iotlb_entry
= vtd_lookup_iotlb(s
, source_id
, pasid
, addr
);
1934 trace_vtd_iotlb_page_hit(source_id
, addr
, iotlb_entry
->slpte
,
1935 iotlb_entry
->domain_id
);
1936 slpte
= iotlb_entry
->slpte
;
1937 access_flags
= iotlb_entry
->access_flags
;
1938 page_mask
= iotlb_entry
->mask
;
1943 ret_fr
= vtd_iova_to_slpte(s
, &ce
, addr
, is_write
, &slpte
, &level
,
1944 &reads
, &writes
, s
->aw_bits
, pasid
);
1946 vtd_report_fault(s
, -ret_fr
, is_fpd_set
, source_id
,
1947 addr
, is_write
, pasid
!= PCI_NO_PASID
, pasid
);
1951 page_mask
= vtd_slpt_level_page_mask(level
);
1952 access_flags
= IOMMU_ACCESS_FLAG(reads
, writes
);
1953 vtd_update_iotlb(s
, source_id
, vtd_get_domain_id(s
, &ce
, pasid
),
1954 addr
, slpte
, access_flags
, level
, pasid
);
1956 vtd_iommu_unlock(s
);
1957 entry
->iova
= addr
& page_mask
;
1958 entry
->translated_addr
= vtd_get_slpte_addr(slpte
, s
->aw_bits
) & page_mask
;
1959 entry
->addr_mask
= ~page_mask
;
1960 entry
->perm
= access_flags
;
1964 vtd_iommu_unlock(s
);
1966 entry
->translated_addr
= 0;
1967 entry
->addr_mask
= 0;
1968 entry
->perm
= IOMMU_NONE
;
1972 static void vtd_root_table_setup(IntelIOMMUState
*s
)
1974 s
->root
= vtd_get_quad_raw(s
, DMAR_RTADDR_REG
);
1975 s
->root
&= VTD_RTADDR_ADDR_MASK(s
->aw_bits
);
1977 vtd_update_scalable_state(s
);
1979 trace_vtd_reg_dmar_root(s
->root
, s
->root_scalable
);
1982 static void vtd_iec_notify_all(IntelIOMMUState
*s
, bool global
,
1983 uint32_t index
, uint32_t mask
)
1985 x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s
), global
, index
, mask
);
1988 static void vtd_interrupt_remap_table_setup(IntelIOMMUState
*s
)
1991 value
= vtd_get_quad_raw(s
, DMAR_IRTA_REG
);
1992 s
->intr_size
= 1UL << ((value
& VTD_IRTA_SIZE_MASK
) + 1);
1993 s
->intr_root
= value
& VTD_IRTA_ADDR_MASK(s
->aw_bits
);
1994 s
->intr_eime
= value
& VTD_IRTA_EIME
;
1996 /* Notify global invalidation */
1997 vtd_iec_notify_all(s
, true, 0, 0);
1999 trace_vtd_reg_ir_root(s
->intr_root
, s
->intr_size
);
2002 static void vtd_iommu_replay_all(IntelIOMMUState
*s
)
2004 VTDAddressSpace
*vtd_as
;
2006 QLIST_FOREACH(vtd_as
, &s
->vtd_as_with_notifiers
, next
) {
2007 vtd_address_space_sync(vtd_as
);
2011 static void vtd_context_global_invalidate(IntelIOMMUState
*s
)
2013 trace_vtd_inv_desc_cc_global();
2014 /* Protects context cache */
2016 s
->context_cache_gen
++;
2017 if (s
->context_cache_gen
== VTD_CONTEXT_CACHE_GEN_MAX
) {
2018 vtd_reset_context_cache_locked(s
);
2020 vtd_iommu_unlock(s
);
2021 vtd_address_space_refresh_all(s
);
2023 * From VT-d spec 6.5.2.1, a global context entry invalidation
2024 * should be followed by a IOTLB global invalidation, so we should
2025 * be safe even without this. Hoewever, let's replay the region as
2026 * well to be safer, and go back here when we need finer tunes for
2027 * VT-d emulation codes.
2029 vtd_iommu_replay_all(s
);
2032 /* Do a context-cache device-selective invalidation.
2033 * @func_mask: FM field after shifting
2035 static void vtd_context_device_invalidate(IntelIOMMUState
*s
,
2039 GHashTableIter as_it
;
2041 VTDAddressSpace
*vtd_as
;
2042 uint8_t bus_n
, devfn
;
2044 trace_vtd_inv_desc_cc_devices(source_id
, func_mask
);
2046 switch (func_mask
& 3) {
2048 mask
= 0; /* No bits in the SID field masked */
2051 mask
= 4; /* Mask bit 2 in the SID field */
2054 mask
= 6; /* Mask bit 2:1 in the SID field */
2057 mask
= 7; /* Mask bit 2:0 in the SID field */
2060 g_assert_not_reached();
2064 bus_n
= VTD_SID_TO_BUS(source_id
);
2065 devfn
= VTD_SID_TO_DEVFN(source_id
);
2067 g_hash_table_iter_init(&as_it
, s
->vtd_address_spaces
);
2068 while (g_hash_table_iter_next(&as_it
, NULL
, (void **)&vtd_as
)) {
2069 if ((pci_bus_num(vtd_as
->bus
) == bus_n
) &&
2070 (vtd_as
->devfn
& mask
) == (devfn
& mask
)) {
2071 trace_vtd_inv_desc_cc_device(bus_n
, VTD_PCI_SLOT(vtd_as
->devfn
),
2072 VTD_PCI_FUNC(vtd_as
->devfn
));
2074 vtd_as
->context_cache_entry
.context_cache_gen
= 0;
2075 vtd_iommu_unlock(s
);
2077 * Do switch address space when needed, in case if the
2078 * device passthrough bit is switched.
2080 vtd_switch_address_space(vtd_as
);
2082 * So a device is moving out of (or moving into) a
2083 * domain, resync the shadow page table.
2084 * This won't bring bad even if we have no such
2085 * notifier registered - the IOMMU notification
2086 * framework will skip MAP notifications if that
2089 vtd_address_space_sync(vtd_as
);
2094 /* Context-cache invalidation
2095 * Returns the Context Actual Invalidation Granularity.
2096 * @val: the content of the CCMD_REG
2098 static uint64_t vtd_context_cache_invalidate(IntelIOMMUState
*s
, uint64_t val
)
2101 uint64_t type
= val
& VTD_CCMD_CIRG_MASK
;
2104 case VTD_CCMD_DOMAIN_INVL
:
2106 case VTD_CCMD_GLOBAL_INVL
:
2107 caig
= VTD_CCMD_GLOBAL_INVL_A
;
2108 vtd_context_global_invalidate(s
);
2111 case VTD_CCMD_DEVICE_INVL
:
2112 caig
= VTD_CCMD_DEVICE_INVL_A
;
2113 vtd_context_device_invalidate(s
, VTD_CCMD_SID(val
), VTD_CCMD_FM(val
));
2117 error_report_once("%s: invalid context: 0x%" PRIx64
,
2124 static void vtd_iotlb_global_invalidate(IntelIOMMUState
*s
)
2126 trace_vtd_inv_desc_iotlb_global();
2128 vtd_iommu_replay_all(s
);
2131 static void vtd_iotlb_domain_invalidate(IntelIOMMUState
*s
, uint16_t domain_id
)
2134 VTDAddressSpace
*vtd_as
;
2136 trace_vtd_inv_desc_iotlb_domain(domain_id
);
2139 g_hash_table_foreach_remove(s
->iotlb
, vtd_hash_remove_by_domain
,
2141 vtd_iommu_unlock(s
);
2143 QLIST_FOREACH(vtd_as
, &s
->vtd_as_with_notifiers
, next
) {
2144 if (!vtd_dev_to_context_entry(s
, pci_bus_num(vtd_as
->bus
),
2145 vtd_as
->devfn
, &ce
) &&
2146 domain_id
== vtd_get_domain_id(s
, &ce
, vtd_as
->pasid
)) {
2147 vtd_address_space_sync(vtd_as
);
2152 static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState
*s
,
2153 uint16_t domain_id
, hwaddr addr
,
2154 uint8_t am
, uint32_t pasid
)
2156 VTDAddressSpace
*vtd_as
;
2159 hwaddr size
= (1 << am
) * VTD_PAGE_SIZE
;
2161 QLIST_FOREACH(vtd_as
, &(s
->vtd_as_with_notifiers
), next
) {
2162 if (pasid
!= PCI_NO_PASID
&& pasid
!= vtd_as
->pasid
) {
2165 ret
= vtd_dev_to_context_entry(s
, pci_bus_num(vtd_as
->bus
),
2166 vtd_as
->devfn
, &ce
);
2167 if (!ret
&& domain_id
== vtd_get_domain_id(s
, &ce
, vtd_as
->pasid
)) {
2168 if (vtd_as_has_map_notifier(vtd_as
)) {
2170 * As long as we have MAP notifications registered in
2171 * any of our IOMMU notifiers, we need to sync the
2172 * shadow page table.
2174 vtd_sync_shadow_page_table_range(vtd_as
, &ce
, addr
, size
);
2177 * For UNMAP-only notifiers, we don't need to walk the
2178 * page tables. We just deliver the PSI down to
2179 * invalidate caches.
2181 IOMMUTLBEvent event
= {
2182 .type
= IOMMU_NOTIFIER_UNMAP
,
2184 .target_as
= &address_space_memory
,
2186 .translated_addr
= 0,
2187 .addr_mask
= size
- 1,
2191 memory_region_notify_iommu(&vtd_as
->iommu
, 0, event
);
2197 static void vtd_iotlb_page_invalidate(IntelIOMMUState
*s
, uint16_t domain_id
,
2198 hwaddr addr
, uint8_t am
)
2200 VTDIOTLBPageInvInfo info
;
2202 trace_vtd_inv_desc_iotlb_pages(domain_id
, addr
, am
);
2204 assert(am
<= VTD_MAMV
);
2205 info
.domain_id
= domain_id
;
2207 info
.mask
= ~((1 << am
) - 1);
2209 g_hash_table_foreach_remove(s
->iotlb
, vtd_hash_remove_by_page
, &info
);
2210 vtd_iommu_unlock(s
);
2211 vtd_iotlb_page_invalidate_notify(s
, domain_id
, addr
, am
, PCI_NO_PASID
);
2215 * Returns the IOTLB Actual Invalidation Granularity.
2216 * @val: the content of the IOTLB_REG
2218 static uint64_t vtd_iotlb_flush(IntelIOMMUState
*s
, uint64_t val
)
2221 uint64_t type
= val
& VTD_TLB_FLUSH_GRANU_MASK
;
2227 case VTD_TLB_GLOBAL_FLUSH
:
2228 iaig
= VTD_TLB_GLOBAL_FLUSH_A
;
2229 vtd_iotlb_global_invalidate(s
);
2232 case VTD_TLB_DSI_FLUSH
:
2233 domain_id
= VTD_TLB_DID(val
);
2234 iaig
= VTD_TLB_DSI_FLUSH_A
;
2235 vtd_iotlb_domain_invalidate(s
, domain_id
);
2238 case VTD_TLB_PSI_FLUSH
:
2239 domain_id
= VTD_TLB_DID(val
);
2240 addr
= vtd_get_quad_raw(s
, DMAR_IVA_REG
);
2241 am
= VTD_IVA_AM(addr
);
2242 addr
= VTD_IVA_ADDR(addr
);
2243 if (am
> VTD_MAMV
) {
2244 error_report_once("%s: address mask overflow: 0x%" PRIx64
,
2245 __func__
, vtd_get_quad_raw(s
, DMAR_IVA_REG
));
2249 iaig
= VTD_TLB_PSI_FLUSH_A
;
2250 vtd_iotlb_page_invalidate(s
, domain_id
, addr
, am
);
2254 error_report_once("%s: invalid granularity: 0x%" PRIx64
,
2261 static void vtd_fetch_inv_desc(IntelIOMMUState
*s
);
2263 static inline bool vtd_queued_inv_disable_check(IntelIOMMUState
*s
)
2265 return s
->qi_enabled
&& (s
->iq_tail
== s
->iq_head
) &&
2266 (s
->iq_last_desc_type
== VTD_INV_DESC_WAIT
);
2269 static void vtd_handle_gcmd_qie(IntelIOMMUState
*s
, bool en
)
2271 uint64_t iqa_val
= vtd_get_quad_raw(s
, DMAR_IQA_REG
);
2273 trace_vtd_inv_qi_enable(en
);
2276 s
->iq
= iqa_val
& VTD_IQA_IQA_MASK(s
->aw_bits
);
2277 /* 2^(x+8) entries */
2278 s
->iq_size
= 1UL << ((iqa_val
& VTD_IQA_QS
) + 8 - (s
->iq_dw
? 1 : 0));
2279 s
->qi_enabled
= true;
2280 trace_vtd_inv_qi_setup(s
->iq
, s
->iq_size
);
2281 /* Ok - report back to driver */
2282 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, 0, VTD_GSTS_QIES
);
2284 if (s
->iq_tail
!= 0) {
2286 * This is a spec violation but Windows guests are known to set up
2287 * Queued Invalidation this way so we allow the write and process
2288 * Invalidation Descriptors right away.
2290 trace_vtd_warn_invalid_qi_tail(s
->iq_tail
);
2291 if (!(vtd_get_long_raw(s
, DMAR_FSTS_REG
) & VTD_FSTS_IQE
)) {
2292 vtd_fetch_inv_desc(s
);
2296 if (vtd_queued_inv_disable_check(s
)) {
2297 /* disable Queued Invalidation */
2298 vtd_set_quad_raw(s
, DMAR_IQH_REG
, 0);
2300 s
->qi_enabled
= false;
2301 /* Ok - report back to driver */
2302 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, VTD_GSTS_QIES
, 0);
2304 error_report_once("%s: detected improper state when disable QI "
2305 "(head=0x%x, tail=0x%x, last_type=%d)",
2307 s
->iq_head
, s
->iq_tail
, s
->iq_last_desc_type
);
2312 /* Set Root Table Pointer */
2313 static void vtd_handle_gcmd_srtp(IntelIOMMUState
*s
)
2315 vtd_root_table_setup(s
);
2316 /* Ok - report back to driver */
2317 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, 0, VTD_GSTS_RTPS
);
2318 vtd_reset_caches(s
);
2319 vtd_address_space_refresh_all(s
);
2322 /* Set Interrupt Remap Table Pointer */
2323 static void vtd_handle_gcmd_sirtp(IntelIOMMUState
*s
)
2325 vtd_interrupt_remap_table_setup(s
);
2326 /* Ok - report back to driver */
2327 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, 0, VTD_GSTS_IRTPS
);
2330 /* Handle Translation Enable/Disable */
2331 static void vtd_handle_gcmd_te(IntelIOMMUState
*s
, bool en
)
2333 if (s
->dmar_enabled
== en
) {
2337 trace_vtd_dmar_enable(en
);
2340 s
->dmar_enabled
= true;
2341 /* Ok - report back to driver */
2342 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, 0, VTD_GSTS_TES
);
2344 s
->dmar_enabled
= false;
2346 /* Clear the index of Fault Recording Register */
2347 s
->next_frcd_reg
= 0;
2348 /* Ok - report back to driver */
2349 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, VTD_GSTS_TES
, 0);
2352 vtd_reset_caches(s
);
2353 vtd_address_space_refresh_all(s
);
2356 /* Handle Interrupt Remap Enable/Disable */
2357 static void vtd_handle_gcmd_ire(IntelIOMMUState
*s
, bool en
)
2359 trace_vtd_ir_enable(en
);
2362 s
->intr_enabled
= true;
2363 /* Ok - report back to driver */
2364 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, 0, VTD_GSTS_IRES
);
2366 s
->intr_enabled
= false;
2367 /* Ok - report back to driver */
2368 vtd_set_clear_mask_long(s
, DMAR_GSTS_REG
, VTD_GSTS_IRES
, 0);
2372 /* Handle write to Global Command Register */
2373 static void vtd_handle_gcmd_write(IntelIOMMUState
*s
)
2375 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
2376 uint32_t status
= vtd_get_long_raw(s
, DMAR_GSTS_REG
);
2377 uint32_t val
= vtd_get_long_raw(s
, DMAR_GCMD_REG
);
2378 uint32_t changed
= status
^ val
;
2380 trace_vtd_reg_write_gcmd(status
, val
);
2381 if ((changed
& VTD_GCMD_TE
) && s
->dma_translation
) {
2382 /* Translation enable/disable */
2383 vtd_handle_gcmd_te(s
, val
& VTD_GCMD_TE
);
2385 if (val
& VTD_GCMD_SRTP
) {
2386 /* Set/update the root-table pointer */
2387 vtd_handle_gcmd_srtp(s
);
2389 if (changed
& VTD_GCMD_QIE
) {
2390 /* Queued Invalidation Enable */
2391 vtd_handle_gcmd_qie(s
, val
& VTD_GCMD_QIE
);
2393 if (val
& VTD_GCMD_SIRTP
) {
2394 /* Set/update the interrupt remapping root-table pointer */
2395 vtd_handle_gcmd_sirtp(s
);
2397 if ((changed
& VTD_GCMD_IRE
) &&
2398 x86_iommu_ir_supported(x86_iommu
)) {
2399 /* Interrupt remap enable/disable */
2400 vtd_handle_gcmd_ire(s
, val
& VTD_GCMD_IRE
);
2404 /* Handle write to Context Command Register */
2405 static void vtd_handle_ccmd_write(IntelIOMMUState
*s
)
2408 uint64_t val
= vtd_get_quad_raw(s
, DMAR_CCMD_REG
);
2410 /* Context-cache invalidation request */
2411 if (val
& VTD_CCMD_ICC
) {
2412 if (s
->qi_enabled
) {
2413 error_report_once("Queued Invalidation enabled, "
2414 "should not use register-based invalidation");
2417 ret
= vtd_context_cache_invalidate(s
, val
);
2418 /* Invalidation completed. Change something to show */
2419 vtd_set_clear_mask_quad(s
, DMAR_CCMD_REG
, VTD_CCMD_ICC
, 0ULL);
2420 ret
= vtd_set_clear_mask_quad(s
, DMAR_CCMD_REG
, VTD_CCMD_CAIG_MASK
,
2425 /* Handle write to IOTLB Invalidation Register */
2426 static void vtd_handle_iotlb_write(IntelIOMMUState
*s
)
2429 uint64_t val
= vtd_get_quad_raw(s
, DMAR_IOTLB_REG
);
2431 /* IOTLB invalidation request */
2432 if (val
& VTD_TLB_IVT
) {
2433 if (s
->qi_enabled
) {
2434 error_report_once("Queued Invalidation enabled, "
2435 "should not use register-based invalidation");
2438 ret
= vtd_iotlb_flush(s
, val
);
2439 /* Invalidation completed. Change something to show */
2440 vtd_set_clear_mask_quad(s
, DMAR_IOTLB_REG
, VTD_TLB_IVT
, 0ULL);
2441 ret
= vtd_set_clear_mask_quad(s
, DMAR_IOTLB_REG
,
2442 VTD_TLB_FLUSH_GRANU_MASK_A
, ret
);
2446 /* Fetch an Invalidation Descriptor from the Invalidation Queue */
2447 static bool vtd_get_inv_desc(IntelIOMMUState
*s
,
2448 VTDInvDesc
*inv_desc
)
2450 dma_addr_t base_addr
= s
->iq
;
2451 uint32_t offset
= s
->iq_head
;
2452 uint32_t dw
= s
->iq_dw
? 32 : 16;
2453 dma_addr_t addr
= base_addr
+ offset
* dw
;
2455 if (dma_memory_read(&address_space_memory
, addr
,
2456 inv_desc
, dw
, MEMTXATTRS_UNSPECIFIED
)) {
2457 error_report_once("Read INV DESC failed.");
2460 inv_desc
->lo
= le64_to_cpu(inv_desc
->lo
);
2461 inv_desc
->hi
= le64_to_cpu(inv_desc
->hi
);
2463 inv_desc
->val
[2] = le64_to_cpu(inv_desc
->val
[2]);
2464 inv_desc
->val
[3] = le64_to_cpu(inv_desc
->val
[3]);
2469 static bool vtd_process_wait_desc(IntelIOMMUState
*s
, VTDInvDesc
*inv_desc
)
2471 if ((inv_desc
->hi
& VTD_INV_DESC_WAIT_RSVD_HI
) ||
2472 (inv_desc
->lo
& VTD_INV_DESC_WAIT_RSVD_LO
)) {
2473 error_report_once("%s: invalid wait desc: hi=%"PRIx64
", lo=%"PRIx64
2474 " (reserved nonzero)", __func__
, inv_desc
->hi
,
2478 if (inv_desc
->lo
& VTD_INV_DESC_WAIT_SW
) {
2480 uint32_t status_data
= (uint32_t)(inv_desc
->lo
>>
2481 VTD_INV_DESC_WAIT_DATA_SHIFT
);
2483 assert(!(inv_desc
->lo
& VTD_INV_DESC_WAIT_IF
));
2485 /* FIXME: need to be masked with HAW? */
2486 dma_addr_t status_addr
= inv_desc
->hi
;
2487 trace_vtd_inv_desc_wait_sw(status_addr
, status_data
);
2488 status_data
= cpu_to_le32(status_data
);
2489 if (dma_memory_write(&address_space_memory
, status_addr
,
2490 &status_data
, sizeof(status_data
),
2491 MEMTXATTRS_UNSPECIFIED
)) {
2492 trace_vtd_inv_desc_wait_write_fail(inv_desc
->hi
, inv_desc
->lo
);
2495 } else if (inv_desc
->lo
& VTD_INV_DESC_WAIT_IF
) {
2496 /* Interrupt flag */
2497 vtd_generate_completion_event(s
);
2499 error_report_once("%s: invalid wait desc: hi=%"PRIx64
", lo=%"PRIx64
2500 " (unknown type)", __func__
, inv_desc
->hi
,
2507 static bool vtd_process_context_cache_desc(IntelIOMMUState
*s
,
2508 VTDInvDesc
*inv_desc
)
2510 uint16_t sid
, fmask
;
2512 if ((inv_desc
->lo
& VTD_INV_DESC_CC_RSVD
) || inv_desc
->hi
) {
2513 error_report_once("%s: invalid cc inv desc: hi=%"PRIx64
", lo=%"PRIx64
2514 " (reserved nonzero)", __func__
, inv_desc
->hi
,
2518 switch (inv_desc
->lo
& VTD_INV_DESC_CC_G
) {
2519 case VTD_INV_DESC_CC_DOMAIN
:
2520 trace_vtd_inv_desc_cc_domain(
2521 (uint16_t)VTD_INV_DESC_CC_DID(inv_desc
->lo
));
2523 case VTD_INV_DESC_CC_GLOBAL
:
2524 vtd_context_global_invalidate(s
);
2527 case VTD_INV_DESC_CC_DEVICE
:
2528 sid
= VTD_INV_DESC_CC_SID(inv_desc
->lo
);
2529 fmask
= VTD_INV_DESC_CC_FM(inv_desc
->lo
);
2530 vtd_context_device_invalidate(s
, sid
, fmask
);
2534 error_report_once("%s: invalid cc inv desc: hi=%"PRIx64
", lo=%"PRIx64
2535 " (invalid type)", __func__
, inv_desc
->hi
,
2542 static bool vtd_process_iotlb_desc(IntelIOMMUState
*s
, VTDInvDesc
*inv_desc
)
2548 if ((inv_desc
->lo
& VTD_INV_DESC_IOTLB_RSVD_LO
) ||
2549 (inv_desc
->hi
& VTD_INV_DESC_IOTLB_RSVD_HI
)) {
2550 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2551 ", lo=0x%"PRIx64
" (reserved bits unzero)",
2552 __func__
, inv_desc
->hi
, inv_desc
->lo
);
2556 switch (inv_desc
->lo
& VTD_INV_DESC_IOTLB_G
) {
2557 case VTD_INV_DESC_IOTLB_GLOBAL
:
2558 vtd_iotlb_global_invalidate(s
);
2561 case VTD_INV_DESC_IOTLB_DOMAIN
:
2562 domain_id
= VTD_INV_DESC_IOTLB_DID(inv_desc
->lo
);
2563 vtd_iotlb_domain_invalidate(s
, domain_id
);
2566 case VTD_INV_DESC_IOTLB_PAGE
:
2567 domain_id
= VTD_INV_DESC_IOTLB_DID(inv_desc
->lo
);
2568 addr
= VTD_INV_DESC_IOTLB_ADDR(inv_desc
->hi
);
2569 am
= VTD_INV_DESC_IOTLB_AM(inv_desc
->hi
);
2570 if (am
> VTD_MAMV
) {
2571 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2572 ", lo=0x%"PRIx64
" (am=%u > VTD_MAMV=%u)",
2573 __func__
, inv_desc
->hi
, inv_desc
->lo
,
2574 am
, (unsigned)VTD_MAMV
);
2577 vtd_iotlb_page_invalidate(s
, domain_id
, addr
, am
);
2581 error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
2582 ", lo=0x%"PRIx64
" (type mismatch: 0x%llx)",
2583 __func__
, inv_desc
->hi
, inv_desc
->lo
,
2584 inv_desc
->lo
& VTD_INV_DESC_IOTLB_G
);
2590 static bool vtd_process_inv_iec_desc(IntelIOMMUState
*s
,
2591 VTDInvDesc
*inv_desc
)
2593 trace_vtd_inv_desc_iec(inv_desc
->iec
.granularity
,
2594 inv_desc
->iec
.index
,
2595 inv_desc
->iec
.index_mask
);
2597 vtd_iec_notify_all(s
, !inv_desc
->iec
.granularity
,
2598 inv_desc
->iec
.index
,
2599 inv_desc
->iec
.index_mask
);
2603 static bool vtd_process_device_iotlb_desc(IntelIOMMUState
*s
,
2604 VTDInvDesc
*inv_desc
)
2606 VTDAddressSpace
*vtd_dev_as
;
2607 IOMMUTLBEvent event
;
2613 addr
= VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc
->hi
);
2614 sid
= VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc
->lo
);
2615 size
= VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc
->hi
);
2617 if ((inv_desc
->lo
& VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO
) ||
2618 (inv_desc
->hi
& VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI
)) {
2619 error_report_once("%s: invalid dev-iotlb inv desc: hi=%"PRIx64
2620 ", lo=%"PRIx64
" (reserved nonzero)", __func__
,
2621 inv_desc
->hi
, inv_desc
->lo
);
2626 * Using sid is OK since the guest should have finished the
2627 * initialization of both the bus and device.
2629 vtd_dev_as
= vtd_get_as_by_sid(s
, sid
);
2634 /* According to ATS spec table 2.4:
2635 * S = 0, bits 15:12 = xxxx range size: 4K
2636 * S = 1, bits 15:12 = xxx0 range size: 8K
2637 * S = 1, bits 15:12 = xx01 range size: 16K
2638 * S = 1, bits 15:12 = x011 range size: 32K
2639 * S = 1, bits 15:12 = 0111 range size: 64K
2643 sz
= (VTD_PAGE_SIZE
* 2) << cto64(addr
>> VTD_PAGE_SHIFT
);
2649 event
.type
= IOMMU_NOTIFIER_DEVIOTLB_UNMAP
;
2650 event
.entry
.target_as
= &vtd_dev_as
->as
;
2651 event
.entry
.addr_mask
= sz
- 1;
2652 event
.entry
.iova
= addr
;
2653 event
.entry
.perm
= IOMMU_NONE
;
2654 event
.entry
.translated_addr
= 0;
2655 memory_region_notify_iommu(&vtd_dev_as
->iommu
, 0, event
);
2661 static bool vtd_process_inv_desc(IntelIOMMUState
*s
)
2663 VTDInvDesc inv_desc
;
2666 trace_vtd_inv_qi_head(s
->iq_head
);
2667 if (!vtd_get_inv_desc(s
, &inv_desc
)) {
2668 s
->iq_last_desc_type
= VTD_INV_DESC_NONE
;
2672 desc_type
= inv_desc
.lo
& VTD_INV_DESC_TYPE
;
2673 /* FIXME: should update at first or at last? */
2674 s
->iq_last_desc_type
= desc_type
;
2676 switch (desc_type
) {
2677 case VTD_INV_DESC_CC
:
2678 trace_vtd_inv_desc("context-cache", inv_desc
.hi
, inv_desc
.lo
);
2679 if (!vtd_process_context_cache_desc(s
, &inv_desc
)) {
2684 case VTD_INV_DESC_IOTLB
:
2685 trace_vtd_inv_desc("iotlb", inv_desc
.hi
, inv_desc
.lo
);
2686 if (!vtd_process_iotlb_desc(s
, &inv_desc
)) {
2692 * TODO: the entity of below two cases will be implemented in future series.
2693 * To make guest (which integrates scalable mode support patch set in
2694 * iommu driver) work, just return true is enough so far.
2696 case VTD_INV_DESC_PC
:
2699 case VTD_INV_DESC_PIOTLB
:
2702 case VTD_INV_DESC_WAIT
:
2703 trace_vtd_inv_desc("wait", inv_desc
.hi
, inv_desc
.lo
);
2704 if (!vtd_process_wait_desc(s
, &inv_desc
)) {
2709 case VTD_INV_DESC_IEC
:
2710 trace_vtd_inv_desc("iec", inv_desc
.hi
, inv_desc
.lo
);
2711 if (!vtd_process_inv_iec_desc(s
, &inv_desc
)) {
2716 case VTD_INV_DESC_DEVICE
:
2717 trace_vtd_inv_desc("device", inv_desc
.hi
, inv_desc
.lo
);
2718 if (!vtd_process_device_iotlb_desc(s
, &inv_desc
)) {
2724 error_report_once("%s: invalid inv desc: hi=%"PRIx64
", lo=%"PRIx64
2725 " (unknown type)", __func__
, inv_desc
.hi
,
2730 if (s
->iq_head
== s
->iq_size
) {
2736 /* Try to fetch and process more Invalidation Descriptors */
2737 static void vtd_fetch_inv_desc(IntelIOMMUState
*s
)
2741 /* Refer to 10.4.23 of VT-d spec 3.0 */
2742 qi_shift
= s
->iq_dw
? VTD_IQH_QH_SHIFT_5
: VTD_IQH_QH_SHIFT_4
;
2744 trace_vtd_inv_qi_fetch();
2746 if (s
->iq_tail
>= s
->iq_size
) {
2747 /* Detects an invalid Tail pointer */
2748 error_report_once("%s: detected invalid QI tail "
2749 "(tail=0x%x, size=0x%x)",
2750 __func__
, s
->iq_tail
, s
->iq_size
);
2751 vtd_handle_inv_queue_error(s
);
2754 while (s
->iq_head
!= s
->iq_tail
) {
2755 if (!vtd_process_inv_desc(s
)) {
2756 /* Invalidation Queue Errors */
2757 vtd_handle_inv_queue_error(s
);
2760 /* Must update the IQH_REG in time */
2761 vtd_set_quad_raw(s
, DMAR_IQH_REG
,
2762 (((uint64_t)(s
->iq_head
)) << qi_shift
) &
2767 /* Handle write to Invalidation Queue Tail Register */
2768 static void vtd_handle_iqt_write(IntelIOMMUState
*s
)
2770 uint64_t val
= vtd_get_quad_raw(s
, DMAR_IQT_REG
);
2772 if (s
->iq_dw
&& (val
& VTD_IQT_QT_256_RSV_BIT
)) {
2773 error_report_once("%s: RSV bit is set: val=0x%"PRIx64
,
2777 s
->iq_tail
= VTD_IQT_QT(s
->iq_dw
, val
);
2778 trace_vtd_inv_qi_tail(s
->iq_tail
);
2780 if (s
->qi_enabled
&& !(vtd_get_long_raw(s
, DMAR_FSTS_REG
) & VTD_FSTS_IQE
)) {
2781 /* Process Invalidation Queue here */
2782 vtd_fetch_inv_desc(s
);
2786 static void vtd_handle_fsts_write(IntelIOMMUState
*s
)
2788 uint32_t fsts_reg
= vtd_get_long_raw(s
, DMAR_FSTS_REG
);
2789 uint32_t fectl_reg
= vtd_get_long_raw(s
, DMAR_FECTL_REG
);
2790 uint32_t status_fields
= VTD_FSTS_PFO
| VTD_FSTS_PPF
| VTD_FSTS_IQE
;
2792 if ((fectl_reg
& VTD_FECTL_IP
) && !(fsts_reg
& status_fields
)) {
2793 vtd_set_clear_mask_long(s
, DMAR_FECTL_REG
, VTD_FECTL_IP
, 0);
2794 trace_vtd_fsts_clear_ip();
2796 /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
2797 * Descriptors if there are any when Queued Invalidation is enabled?
2801 static void vtd_handle_fectl_write(IntelIOMMUState
*s
)
2804 /* FIXME: when software clears the IM field, check the IP field. But do we
2805 * need to compare the old value and the new value to conclude that
2806 * software clears the IM field? Or just check if the IM field is zero?
2808 fectl_reg
= vtd_get_long_raw(s
, DMAR_FECTL_REG
);
2810 trace_vtd_reg_write_fectl(fectl_reg
);
2812 if ((fectl_reg
& VTD_FECTL_IP
) && !(fectl_reg
& VTD_FECTL_IM
)) {
2813 vtd_generate_interrupt(s
, DMAR_FEADDR_REG
, DMAR_FEDATA_REG
);
2814 vtd_set_clear_mask_long(s
, DMAR_FECTL_REG
, VTD_FECTL_IP
, 0);
2818 static void vtd_handle_ics_write(IntelIOMMUState
*s
)
2820 uint32_t ics_reg
= vtd_get_long_raw(s
, DMAR_ICS_REG
);
2821 uint32_t iectl_reg
= vtd_get_long_raw(s
, DMAR_IECTL_REG
);
2823 if ((iectl_reg
& VTD_IECTL_IP
) && !(ics_reg
& VTD_ICS_IWC
)) {
2824 trace_vtd_reg_ics_clear_ip();
2825 vtd_set_clear_mask_long(s
, DMAR_IECTL_REG
, VTD_IECTL_IP
, 0);
2829 static void vtd_handle_iectl_write(IntelIOMMUState
*s
)
2832 /* FIXME: when software clears the IM field, check the IP field. But do we
2833 * need to compare the old value and the new value to conclude that
2834 * software clears the IM field? Or just check if the IM field is zero?
2836 iectl_reg
= vtd_get_long_raw(s
, DMAR_IECTL_REG
);
2838 trace_vtd_reg_write_iectl(iectl_reg
);
2840 if ((iectl_reg
& VTD_IECTL_IP
) && !(iectl_reg
& VTD_IECTL_IM
)) {
2841 vtd_generate_interrupt(s
, DMAR_IEADDR_REG
, DMAR_IEDATA_REG
);
2842 vtd_set_clear_mask_long(s
, DMAR_IECTL_REG
, VTD_IECTL_IP
, 0);
2846 static uint64_t vtd_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
2848 IntelIOMMUState
*s
= opaque
;
2851 trace_vtd_reg_read(addr
, size
);
2853 if (addr
+ size
> DMAR_REG_SIZE
) {
2854 error_report_once("%s: MMIO over range: addr=0x%" PRIx64
2855 " size=0x%x", __func__
, addr
, size
);
2856 return (uint64_t)-1;
2860 /* Root Table Address Register, 64-bit */
2861 case DMAR_RTADDR_REG
:
2862 val
= vtd_get_quad_raw(s
, DMAR_RTADDR_REG
);
2864 val
= val
& ((1ULL << 32) - 1);
2868 case DMAR_RTADDR_REG_HI
:
2870 val
= vtd_get_quad_raw(s
, DMAR_RTADDR_REG
) >> 32;
2873 /* Invalidation Queue Address Register, 64-bit */
2875 val
= s
->iq
| (vtd_get_quad(s
, DMAR_IQA_REG
) & VTD_IQA_QS
);
2877 val
= val
& ((1ULL << 32) - 1);
2881 case DMAR_IQA_REG_HI
:
2888 val
= vtd_get_long(s
, addr
);
2890 val
= vtd_get_quad(s
, addr
);
2897 static void vtd_mem_write(void *opaque
, hwaddr addr
,
2898 uint64_t val
, unsigned size
)
2900 IntelIOMMUState
*s
= opaque
;
2902 trace_vtd_reg_write(addr
, size
, val
);
2904 if (addr
+ size
> DMAR_REG_SIZE
) {
2905 error_report_once("%s: MMIO over range: addr=0x%" PRIx64
2906 " size=0x%x", __func__
, addr
, size
);
2911 /* Global Command Register, 32-bit */
2913 vtd_set_long(s
, addr
, val
);
2914 vtd_handle_gcmd_write(s
);
2917 /* Context Command Register, 64-bit */
2920 vtd_set_long(s
, addr
, val
);
2922 vtd_set_quad(s
, addr
, val
);
2923 vtd_handle_ccmd_write(s
);
2927 case DMAR_CCMD_REG_HI
:
2929 vtd_set_long(s
, addr
, val
);
2930 vtd_handle_ccmd_write(s
);
2933 /* IOTLB Invalidation Register, 64-bit */
2934 case DMAR_IOTLB_REG
:
2936 vtd_set_long(s
, addr
, val
);
2938 vtd_set_quad(s
, addr
, val
);
2939 vtd_handle_iotlb_write(s
);
2943 case DMAR_IOTLB_REG_HI
:
2945 vtd_set_long(s
, addr
, val
);
2946 vtd_handle_iotlb_write(s
);
2949 /* Invalidate Address Register, 64-bit */
2952 vtd_set_long(s
, addr
, val
);
2954 vtd_set_quad(s
, addr
, val
);
2958 case DMAR_IVA_REG_HI
:
2960 vtd_set_long(s
, addr
, val
);
2963 /* Fault Status Register, 32-bit */
2966 vtd_set_long(s
, addr
, val
);
2967 vtd_handle_fsts_write(s
);
2970 /* Fault Event Control Register, 32-bit */
2971 case DMAR_FECTL_REG
:
2973 vtd_set_long(s
, addr
, val
);
2974 vtd_handle_fectl_write(s
);
2977 /* Fault Event Data Register, 32-bit */
2978 case DMAR_FEDATA_REG
:
2980 vtd_set_long(s
, addr
, val
);
2983 /* Fault Event Address Register, 32-bit */
2984 case DMAR_FEADDR_REG
:
2986 vtd_set_long(s
, addr
, val
);
2989 * While the register is 32-bit only, some guests (Xen...) write to
2992 vtd_set_quad(s
, addr
, val
);
2996 /* Fault Event Upper Address Register, 32-bit */
2997 case DMAR_FEUADDR_REG
:
2999 vtd_set_long(s
, addr
, val
);
3002 /* Protected Memory Enable Register, 32-bit */
3005 vtd_set_long(s
, addr
, val
);
3008 /* Root Table Address Register, 64-bit */
3009 case DMAR_RTADDR_REG
:
3011 vtd_set_long(s
, addr
, val
);
3013 vtd_set_quad(s
, addr
, val
);
3017 case DMAR_RTADDR_REG_HI
:
3019 vtd_set_long(s
, addr
, val
);
3022 /* Invalidation Queue Tail Register, 64-bit */
3025 vtd_set_long(s
, addr
, val
);
3027 vtd_set_quad(s
, addr
, val
);
3029 vtd_handle_iqt_write(s
);
3032 case DMAR_IQT_REG_HI
:
3034 vtd_set_long(s
, addr
, val
);
3035 /* 19:63 of IQT_REG is RsvdZ, do nothing here */
3038 /* Invalidation Queue Address Register, 64-bit */
3041 vtd_set_long(s
, addr
, val
);
3043 vtd_set_quad(s
, addr
, val
);
3045 vtd_update_iq_dw(s
);
3048 case DMAR_IQA_REG_HI
:
3050 vtd_set_long(s
, addr
, val
);
3053 /* Invalidation Completion Status Register, 32-bit */
3056 vtd_set_long(s
, addr
, val
);
3057 vtd_handle_ics_write(s
);
3060 /* Invalidation Event Control Register, 32-bit */
3061 case DMAR_IECTL_REG
:
3063 vtd_set_long(s
, addr
, val
);
3064 vtd_handle_iectl_write(s
);
3067 /* Invalidation Event Data Register, 32-bit */
3068 case DMAR_IEDATA_REG
:
3070 vtd_set_long(s
, addr
, val
);
3073 /* Invalidation Event Address Register, 32-bit */
3074 case DMAR_IEADDR_REG
:
3076 vtd_set_long(s
, addr
, val
);
3079 /* Invalidation Event Upper Address Register, 32-bit */
3080 case DMAR_IEUADDR_REG
:
3082 vtd_set_long(s
, addr
, val
);
3085 /* Fault Recording Registers, 128-bit */
3086 case DMAR_FRCD_REG_0_0
:
3088 vtd_set_long(s
, addr
, val
);
3090 vtd_set_quad(s
, addr
, val
);
3094 case DMAR_FRCD_REG_0_1
:
3096 vtd_set_long(s
, addr
, val
);
3099 case DMAR_FRCD_REG_0_2
:
3101 vtd_set_long(s
, addr
, val
);
3103 vtd_set_quad(s
, addr
, val
);
3104 /* May clear bit 127 (Fault), update PPF */
3105 vtd_update_fsts_ppf(s
);
3109 case DMAR_FRCD_REG_0_3
:
3111 vtd_set_long(s
, addr
, val
);
3112 /* May clear bit 127 (Fault), update PPF */
3113 vtd_update_fsts_ppf(s
);
3118 vtd_set_long(s
, addr
, val
);
3120 vtd_set_quad(s
, addr
, val
);
3124 case DMAR_IRTA_REG_HI
:
3126 vtd_set_long(s
, addr
, val
);
3131 vtd_set_long(s
, addr
, val
);
3133 vtd_set_quad(s
, addr
, val
);
3138 static IOMMUTLBEntry
vtd_iommu_translate(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
3139 IOMMUAccessFlags flag
, int iommu_idx
)
3141 VTDAddressSpace
*vtd_as
= container_of(iommu
, VTDAddressSpace
, iommu
);
3142 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
3143 IOMMUTLBEntry iotlb
= {
3144 /* We'll fill in the rest later. */
3145 .target_as
= &address_space_memory
,
3149 if (likely(s
->dmar_enabled
)) {
3150 success
= vtd_do_iommu_translate(vtd_as
, vtd_as
->bus
, vtd_as
->devfn
,
3151 addr
, flag
& IOMMU_WO
, &iotlb
);
3153 /* DMAR disabled, passthrough, use 4k-page*/
3154 iotlb
.iova
= addr
& VTD_PAGE_MASK_4K
;
3155 iotlb
.translated_addr
= addr
& VTD_PAGE_MASK_4K
;
3156 iotlb
.addr_mask
= ~VTD_PAGE_MASK_4K
;
3157 iotlb
.perm
= IOMMU_RW
;
3161 if (likely(success
)) {
3162 trace_vtd_dmar_translate(pci_bus_num(vtd_as
->bus
),
3163 VTD_PCI_SLOT(vtd_as
->devfn
),
3164 VTD_PCI_FUNC(vtd_as
->devfn
),
3165 iotlb
.iova
, iotlb
.translated_addr
,
3168 error_report_once("%s: detected translation failure "
3169 "(dev=%02x:%02x:%02x, iova=0x%" PRIx64
")",
3170 __func__
, pci_bus_num(vtd_as
->bus
),
3171 VTD_PCI_SLOT(vtd_as
->devfn
),
3172 VTD_PCI_FUNC(vtd_as
->devfn
),
3179 static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion
*iommu
,
3180 IOMMUNotifierFlag old
,
3181 IOMMUNotifierFlag
new,
3184 VTDAddressSpace
*vtd_as
= container_of(iommu
, VTDAddressSpace
, iommu
);
3185 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
3186 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
3188 /* TODO: add support for VFIO and vhost users */
3189 if (s
->snoop_control
) {
3190 error_setg_errno(errp
, ENOTSUP
,
3191 "Snoop Control with vhost or VFIO is not supported");
3194 if (!s
->caching_mode
&& (new & IOMMU_NOTIFIER_MAP
)) {
3195 error_setg_errno(errp
, ENOTSUP
,
3196 "device %02x.%02x.%x requires caching mode",
3197 pci_bus_num(vtd_as
->bus
), PCI_SLOT(vtd_as
->devfn
),
3198 PCI_FUNC(vtd_as
->devfn
));
3201 if (!x86_iommu
->dt_supported
&& (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP
)) {
3202 error_setg_errno(errp
, ENOTSUP
,
3203 "device %02x.%02x.%x requires device IOTLB mode",
3204 pci_bus_num(vtd_as
->bus
), PCI_SLOT(vtd_as
->devfn
),
3205 PCI_FUNC(vtd_as
->devfn
));
3209 /* Update per-address-space notifier flags */
3210 vtd_as
->notifier_flags
= new;
3212 if (old
== IOMMU_NOTIFIER_NONE
) {
3213 QLIST_INSERT_HEAD(&s
->vtd_as_with_notifiers
, vtd_as
, next
);
3214 } else if (new == IOMMU_NOTIFIER_NONE
) {
3215 QLIST_REMOVE(vtd_as
, next
);
3220 static int vtd_post_load(void *opaque
, int version_id
)
3222 IntelIOMMUState
*iommu
= opaque
;
3225 * We don't need to migrate the root_scalable because we can
3226 * simply do the calculation after the loading is complete. We
3227 * can actually do similar things with root, dmar_enabled, etc.
3228 * however since we've had them already so we'd better keep them
3229 * for compatibility of migration.
3231 vtd_update_scalable_state(iommu
);
3233 vtd_update_iq_dw(iommu
);
3236 * Memory regions are dynamically turned on/off depending on
3237 * context entry configurations from the guest. After migration,
3238 * we need to make sure the memory regions are still correct.
3240 vtd_switch_address_space_all(iommu
);
3245 static const VMStateDescription vtd_vmstate
= {
3246 .name
= "iommu-intel",
3248 .minimum_version_id
= 1,
3249 .priority
= MIG_PRI_IOMMU
,
3250 .post_load
= vtd_post_load
,
3251 .fields
= (VMStateField
[]) {
3252 VMSTATE_UINT64(root
, IntelIOMMUState
),
3253 VMSTATE_UINT64(intr_root
, IntelIOMMUState
),
3254 VMSTATE_UINT64(iq
, IntelIOMMUState
),
3255 VMSTATE_UINT32(intr_size
, IntelIOMMUState
),
3256 VMSTATE_UINT16(iq_head
, IntelIOMMUState
),
3257 VMSTATE_UINT16(iq_tail
, IntelIOMMUState
),
3258 VMSTATE_UINT16(iq_size
, IntelIOMMUState
),
3259 VMSTATE_UINT16(next_frcd_reg
, IntelIOMMUState
),
3260 VMSTATE_UINT8_ARRAY(csr
, IntelIOMMUState
, DMAR_REG_SIZE
),
3261 VMSTATE_UINT8(iq_last_desc_type
, IntelIOMMUState
),
3262 VMSTATE_UNUSED(1), /* bool root_extended is obsolete by VT-d */
3263 VMSTATE_BOOL(dmar_enabled
, IntelIOMMUState
),
3264 VMSTATE_BOOL(qi_enabled
, IntelIOMMUState
),
3265 VMSTATE_BOOL(intr_enabled
, IntelIOMMUState
),
3266 VMSTATE_BOOL(intr_eime
, IntelIOMMUState
),
3267 VMSTATE_END_OF_LIST()
3271 static const MemoryRegionOps vtd_mem_ops
= {
3272 .read
= vtd_mem_read
,
3273 .write
= vtd_mem_write
,
3274 .endianness
= DEVICE_LITTLE_ENDIAN
,
3276 .min_access_size
= 4,
3277 .max_access_size
= 8,
3280 .min_access_size
= 4,
3281 .max_access_size
= 8,
3285 static Property vtd_properties
[] = {
3286 DEFINE_PROP_UINT32("version", IntelIOMMUState
, version
, 0),
3287 DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState
, intr_eim
,
3289 DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState
, buggy_eim
, false),
3290 DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState
, aw_bits
,
3291 VTD_HOST_ADDRESS_WIDTH
),
3292 DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState
, caching_mode
, FALSE
),
3293 DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState
, scalable_mode
, FALSE
),
3294 DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState
, snoop_control
, false),
3295 DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState
, pasid
, false),
3296 DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState
, dma_drain
, true),
3297 DEFINE_PROP_BOOL("dma-translation", IntelIOMMUState
, dma_translation
, true),
3298 DEFINE_PROP_END_OF_LIST(),
3301 /* Read IRTE entry with specific index */
3302 static int vtd_irte_get(IntelIOMMUState
*iommu
, uint16_t index
,
3303 VTD_IR_TableEntry
*entry
, uint16_t sid
)
3305 static const uint16_t vtd_svt_mask
[VTD_SQ_MAX
] = \
3306 {0xffff, 0xfffb, 0xfff9, 0xfff8};
3307 dma_addr_t addr
= 0x00;
3308 uint16_t mask
, source_id
;
3309 uint8_t bus
, bus_max
, bus_min
;
3311 if (index
>= iommu
->intr_size
) {
3312 error_report_once("%s: index too large: ind=0x%x",
3314 return -VTD_FR_IR_INDEX_OVER
;
3317 addr
= iommu
->intr_root
+ index
* sizeof(*entry
);
3318 if (dma_memory_read(&address_space_memory
, addr
,
3319 entry
, sizeof(*entry
), MEMTXATTRS_UNSPECIFIED
)) {
3320 error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64
,
3321 __func__
, index
, addr
);
3322 return -VTD_FR_IR_ROOT_INVAL
;
3325 trace_vtd_ir_irte_get(index
, le64_to_cpu(entry
->data
[1]),
3326 le64_to_cpu(entry
->data
[0]));
3328 if (!entry
->irte
.present
) {
3329 error_report_once("%s: detected non-present IRTE "
3330 "(index=%u, high=0x%" PRIx64
", low=0x%" PRIx64
")",
3331 __func__
, index
, le64_to_cpu(entry
->data
[1]),
3332 le64_to_cpu(entry
->data
[0]));
3333 return -VTD_FR_IR_ENTRY_P
;
3336 if (entry
->irte
.__reserved_0
|| entry
->irte
.__reserved_1
||
3337 entry
->irte
.__reserved_2
) {
3338 error_report_once("%s: detected non-zero reserved IRTE "
3339 "(index=%u, high=0x%" PRIx64
", low=0x%" PRIx64
")",
3340 __func__
, index
, le64_to_cpu(entry
->data
[1]),
3341 le64_to_cpu(entry
->data
[0]));
3342 return -VTD_FR_IR_IRTE_RSVD
;
3345 if (sid
!= X86_IOMMU_SID_INVALID
) {
3346 /* Validate IRTE SID */
3347 source_id
= le32_to_cpu(entry
->irte
.source_id
);
3348 switch (entry
->irte
.sid_vtype
) {
3353 mask
= vtd_svt_mask
[entry
->irte
.sid_q
];
3354 if ((source_id
& mask
) != (sid
& mask
)) {
3355 error_report_once("%s: invalid IRTE SID "
3356 "(index=%u, sid=%u, source_id=%u)",
3357 __func__
, index
, sid
, source_id
);
3358 return -VTD_FR_IR_SID_ERR
;
3363 bus_max
= source_id
>> 8;
3364 bus_min
= source_id
& 0xff;
3366 if (bus
> bus_max
|| bus
< bus_min
) {
3367 error_report_once("%s: invalid SVT_BUS "
3368 "(index=%u, bus=%u, min=%u, max=%u)",
3369 __func__
, index
, bus
, bus_min
, bus_max
);
3370 return -VTD_FR_IR_SID_ERR
;
3375 error_report_once("%s: detected invalid IRTE SVT "
3376 "(index=%u, type=%d)", __func__
,
3377 index
, entry
->irte
.sid_vtype
);
3378 /* Take this as verification failure. */
3379 return -VTD_FR_IR_SID_ERR
;
3386 /* Fetch IRQ information of specific IR index */
3387 static int vtd_remap_irq_get(IntelIOMMUState
*iommu
, uint16_t index
,
3388 X86IOMMUIrq
*irq
, uint16_t sid
)
3390 VTD_IR_TableEntry irte
= {};
3393 ret
= vtd_irte_get(iommu
, index
, &irte
, sid
);
3398 irq
->trigger_mode
= irte
.irte
.trigger_mode
;
3399 irq
->vector
= irte
.irte
.vector
;
3400 irq
->delivery_mode
= irte
.irte
.delivery_mode
;
3401 irq
->dest
= le32_to_cpu(irte
.irte
.dest_id
);
3402 if (!iommu
->intr_eime
) {
3403 #define VTD_IR_APIC_DEST_MASK (0xff00ULL)
3404 #define VTD_IR_APIC_DEST_SHIFT (8)
3405 irq
->dest
= (irq
->dest
& VTD_IR_APIC_DEST_MASK
) >>
3406 VTD_IR_APIC_DEST_SHIFT
;
3408 irq
->dest_mode
= irte
.irte
.dest_mode
;
3409 irq
->redir_hint
= irte
.irte
.redir_hint
;
3411 trace_vtd_ir_remap(index
, irq
->trigger_mode
, irq
->vector
,
3412 irq
->delivery_mode
, irq
->dest
, irq
->dest_mode
);
3417 /* Interrupt remapping for MSI/MSI-X entry */
3418 static int vtd_interrupt_remap_msi(IntelIOMMUState
*iommu
,
3420 MSIMessage
*translated
,
3424 VTD_IR_MSIAddress addr
;
3426 X86IOMMUIrq irq
= {};
3428 assert(origin
&& translated
);
3430 trace_vtd_ir_remap_msi_req(origin
->address
, origin
->data
);
3432 if (!iommu
|| !iommu
->intr_enabled
) {
3433 memcpy(translated
, origin
, sizeof(*origin
));
3437 if (origin
->address
& VTD_MSI_ADDR_HI_MASK
) {
3438 error_report_once("%s: MSI address high 32 bits non-zero detected: "
3439 "address=0x%" PRIx64
, __func__
, origin
->address
);
3440 return -VTD_FR_IR_REQ_RSVD
;
3443 addr
.data
= origin
->address
& VTD_MSI_ADDR_LO_MASK
;
3444 if (addr
.addr
.__head
!= 0xfee) {
3445 error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32
,
3446 __func__
, addr
.data
);
3447 return -VTD_FR_IR_REQ_RSVD
;
3450 /* This is compatible mode. */
3451 if (addr
.addr
.int_mode
!= VTD_IR_INT_FORMAT_REMAP
) {
3452 memcpy(translated
, origin
, sizeof(*origin
));
3456 index
= addr
.addr
.index_h
<< 15 | le16_to_cpu(addr
.addr
.index_l
);
3458 #define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff)
3459 #define VTD_IR_MSI_DATA_RESERVED (0xffff0000)
3461 if (addr
.addr
.sub_valid
) {
3462 /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
3463 index
+= origin
->data
& VTD_IR_MSI_DATA_SUBHANDLE
;
3466 ret
= vtd_remap_irq_get(iommu
, index
, &irq
, sid
);
3471 if (addr
.addr
.sub_valid
) {
3472 trace_vtd_ir_remap_type("MSI");
3473 if (origin
->data
& VTD_IR_MSI_DATA_RESERVED
) {
3474 error_report_once("%s: invalid IR MSI "
3475 "(sid=%u, address=0x%" PRIx64
3476 ", data=0x%" PRIx32
")",
3477 __func__
, sid
, origin
->address
, origin
->data
);
3478 return -VTD_FR_IR_REQ_RSVD
;
3481 uint8_t vector
= origin
->data
& 0xff;
3482 uint8_t trigger_mode
= (origin
->data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
3484 trace_vtd_ir_remap_type("IOAPIC");
3485 /* IOAPIC entry vector should be aligned with IRTE vector
3486 * (see vt-d spec 5.1.5.1). */
3487 if (vector
!= irq
.vector
) {
3488 trace_vtd_warn_ir_vector(sid
, index
, vector
, irq
.vector
);
3491 /* The Trigger Mode field must match the Trigger Mode in the IRTE.
3492 * (see vt-d spec 5.1.5.1). */
3493 if (trigger_mode
!= irq
.trigger_mode
) {
3494 trace_vtd_warn_ir_trigger(sid
, index
, trigger_mode
,
3500 * We'd better keep the last two bits, assuming that guest OS
3501 * might modify it. Keep it does not hurt after all.
3503 irq
.msi_addr_last_bits
= addr
.addr
.__not_care
;
3505 /* Translate X86IOMMUIrq to MSI message */
3506 x86_iommu_irq_to_msi_message(&irq
, translated
);
3509 trace_vtd_ir_remap_msi(origin
->address
, origin
->data
,
3510 translated
->address
, translated
->data
);
3514 static int vtd_int_remap(X86IOMMUState
*iommu
, MSIMessage
*src
,
3515 MSIMessage
*dst
, uint16_t sid
)
3517 return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu
),
3521 static MemTxResult
vtd_mem_ir_read(void *opaque
, hwaddr addr
,
3522 uint64_t *data
, unsigned size
,
3528 static MemTxResult
vtd_mem_ir_write(void *opaque
, hwaddr addr
,
3529 uint64_t value
, unsigned size
,
3533 MSIMessage from
= {}, to
= {};
3534 uint16_t sid
= X86_IOMMU_SID_INVALID
;
3536 from
.address
= (uint64_t) addr
+ VTD_INTERRUPT_ADDR_FIRST
;
3537 from
.data
= (uint32_t) value
;
3539 if (!attrs
.unspecified
) {
3540 /* We have explicit Source ID */
3541 sid
= attrs
.requester_id
;
3544 ret
= vtd_interrupt_remap_msi(opaque
, &from
, &to
, sid
);
3546 /* TODO: report error */
3547 /* Drop this interrupt */
3551 apic_get_class(NULL
)->send_msi(&to
);
3556 static const MemoryRegionOps vtd_mem_ir_ops
= {
3557 .read_with_attrs
= vtd_mem_ir_read
,
3558 .write_with_attrs
= vtd_mem_ir_write
,
3559 .endianness
= DEVICE_LITTLE_ENDIAN
,
3561 .min_access_size
= 4,
3562 .max_access_size
= 4,
3565 .min_access_size
= 4,
3566 .max_access_size
= 4,
3570 static void vtd_report_ir_illegal_access(VTDAddressSpace
*vtd_as
,
3571 hwaddr addr
, bool is_write
)
3573 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
3574 uint8_t bus_n
= pci_bus_num(vtd_as
->bus
);
3575 uint16_t sid
= PCI_BUILD_BDF(bus_n
, vtd_as
->devfn
);
3576 bool is_fpd_set
= false;
3579 assert(vtd_as
->pasid
!= PCI_NO_PASID
);
3581 /* Try out best to fetch FPD, we can't do anything more */
3582 if (vtd_dev_to_context_entry(s
, bus_n
, vtd_as
->devfn
, &ce
) == 0) {
3583 is_fpd_set
= ce
.lo
& VTD_CONTEXT_ENTRY_FPD
;
3584 if (!is_fpd_set
&& s
->root_scalable
) {
3585 vtd_ce_get_pasid_fpd(s
, &ce
, &is_fpd_set
, vtd_as
->pasid
);
3589 vtd_report_fault(s
, VTD_FR_SM_INTERRUPT_ADDR
,
3590 is_fpd_set
, sid
, addr
, is_write
,
3591 true, vtd_as
->pasid
);
3594 static MemTxResult
vtd_mem_ir_fault_read(void *opaque
, hwaddr addr
,
3595 uint64_t *data
, unsigned size
,
3598 vtd_report_ir_illegal_access(opaque
, addr
, false);
3603 static MemTxResult
vtd_mem_ir_fault_write(void *opaque
, hwaddr addr
,
3604 uint64_t value
, unsigned size
,
3607 vtd_report_ir_illegal_access(opaque
, addr
, true);
3612 static const MemoryRegionOps vtd_mem_ir_fault_ops
= {
3613 .read_with_attrs
= vtd_mem_ir_fault_read
,
3614 .write_with_attrs
= vtd_mem_ir_fault_write
,
3615 .endianness
= DEVICE_LITTLE_ENDIAN
,
3617 .min_access_size
= 1,
3618 .max_access_size
= 8,
3621 .min_access_size
= 1,
3622 .max_access_size
= 8,
3626 VTDAddressSpace
*vtd_find_add_as(IntelIOMMUState
*s
, PCIBus
*bus
,
3627 int devfn
, unsigned int pasid
)
3630 * We can't simply use sid here since the bus number might not be
3631 * initialized by the guest.
3633 struct vtd_as_key key
= {
3638 VTDAddressSpace
*vtd_dev_as
;
3641 vtd_dev_as
= g_hash_table_lookup(s
->vtd_address_spaces
, &key
);
3643 struct vtd_as_key
*new_key
= g_malloc(sizeof(*new_key
));
3646 new_key
->devfn
= devfn
;
3647 new_key
->pasid
= pasid
;
3649 if (pasid
== PCI_NO_PASID
) {
3650 snprintf(name
, sizeof(name
), "vtd-%02x.%x", PCI_SLOT(devfn
),
3653 snprintf(name
, sizeof(name
), "vtd-%02x.%x-pasid-%x", PCI_SLOT(devfn
),
3654 PCI_FUNC(devfn
), pasid
);
3657 vtd_dev_as
= g_new0(VTDAddressSpace
, 1);
3659 vtd_dev_as
->bus
= bus
;
3660 vtd_dev_as
->devfn
= (uint8_t)devfn
;
3661 vtd_dev_as
->pasid
= pasid
;
3662 vtd_dev_as
->iommu_state
= s
;
3663 vtd_dev_as
->context_cache_entry
.context_cache_gen
= 0;
3664 vtd_dev_as
->iova_tree
= iova_tree_new();
3666 memory_region_init(&vtd_dev_as
->root
, OBJECT(s
), name
, UINT64_MAX
);
3667 address_space_init(&vtd_dev_as
->as
, &vtd_dev_as
->root
, "vtd-root");
3670 * Build the DMAR-disabled container with aliases to the
3671 * shared MRs. Note that aliasing to a shared memory region
3672 * could help the memory API to detect same FlatViews so we
3673 * can have devices to share the same FlatView when DMAR is
3674 * disabled (either by not providing "intel_iommu=on" or with
3675 * "iommu=pt"). It will greatly reduce the total number of
3676 * FlatViews of the system hence VM runs faster.
3678 memory_region_init_alias(&vtd_dev_as
->nodmar
, OBJECT(s
),
3679 "vtd-nodmar", &s
->mr_nodmar
, 0,
3680 memory_region_size(&s
->mr_nodmar
));
3683 * Build the per-device DMAR-enabled container.
3685 * TODO: currently we have per-device IOMMU memory region only
3686 * because we have per-device IOMMU notifiers for devices. If
3687 * one day we can abstract the IOMMU notifiers out of the
3688 * memory regions then we can also share the same memory
3689 * region here just like what we've done above with the nodmar
3692 strcat(name
, "-dmar");
3693 memory_region_init_iommu(&vtd_dev_as
->iommu
, sizeof(vtd_dev_as
->iommu
),
3694 TYPE_INTEL_IOMMU_MEMORY_REGION
, OBJECT(s
),
3696 memory_region_init_alias(&vtd_dev_as
->iommu_ir
, OBJECT(s
), "vtd-ir",
3697 &s
->mr_ir
, 0, memory_region_size(&s
->mr_ir
));
3698 memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as
->iommu
),
3699 VTD_INTERRUPT_ADDR_FIRST
,
3700 &vtd_dev_as
->iommu_ir
, 1);
3703 * This region is used for catching fault to access interrupt
3704 * range via passthrough + PASID. See also
3705 * vtd_switch_address_space(). We can't use alias since we
3706 * need to know the sid which is valid for MSI who uses
3707 * bus_master_as (see msi_send_message()).
3709 memory_region_init_io(&vtd_dev_as
->iommu_ir_fault
, OBJECT(s
),
3710 &vtd_mem_ir_fault_ops
, vtd_dev_as
, "vtd-no-ir",
3711 VTD_INTERRUPT_ADDR_SIZE
);
3713 * Hook to root since when PT is enabled vtd_dev_as->iommu
3716 memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as
->root
),
3717 VTD_INTERRUPT_ADDR_FIRST
,
3718 &vtd_dev_as
->iommu_ir_fault
, 2);
3721 * Hook both the containers under the root container, we
3722 * switch between DMAR & noDMAR by enable/disable
3723 * corresponding sub-containers
3725 memory_region_add_subregion_overlap(&vtd_dev_as
->root
, 0,
3726 MEMORY_REGION(&vtd_dev_as
->iommu
),
3728 memory_region_add_subregion_overlap(&vtd_dev_as
->root
, 0,
3729 &vtd_dev_as
->nodmar
, 0);
3731 vtd_switch_address_space(vtd_dev_as
);
3733 g_hash_table_insert(s
->vtd_address_spaces
, new_key
, vtd_dev_as
);
3738 /* Unmap the whole range in the notifier's scope. */
3739 static void vtd_address_space_unmap(VTDAddressSpace
*as
, IOMMUNotifier
*n
)
3741 hwaddr size
, remain
;
3742 hwaddr start
= n
->start
;
3743 hwaddr end
= n
->end
;
3744 IntelIOMMUState
*s
= as
->iommu_state
;
3748 * Note: all the codes in this function has a assumption that IOVA
3749 * bits are no more than VTD_MGAW bits (which is restricted by
3750 * VT-d spec), otherwise we need to consider overflow of 64 bits.
3753 if (end
> VTD_ADDRESS_SIZE(s
->aw_bits
) - 1) {
3755 * Don't need to unmap regions that is bigger than the whole
3756 * VT-d supported address space size
3758 end
= VTD_ADDRESS_SIZE(s
->aw_bits
) - 1;
3761 assert(start
<= end
);
3762 size
= remain
= end
- start
+ 1;
3764 while (remain
>= VTD_PAGE_SIZE
) {
3765 IOMMUTLBEvent event
;
3766 uint64_t mask
= dma_aligned_pow2_mask(start
, end
, s
->aw_bits
);
3767 uint64_t size
= mask
+ 1;
3771 event
.type
= IOMMU_NOTIFIER_UNMAP
;
3772 event
.entry
.iova
= start
;
3773 event
.entry
.addr_mask
= mask
;
3774 event
.entry
.target_as
= &address_space_memory
;
3775 event
.entry
.perm
= IOMMU_NONE
;
3776 /* This field is meaningless for unmap */
3777 event
.entry
.translated_addr
= 0;
3779 memory_region_notify_iommu_one(n
, &event
);
3787 trace_vtd_as_unmap_whole(pci_bus_num(as
->bus
),
3788 VTD_PCI_SLOT(as
->devfn
),
3789 VTD_PCI_FUNC(as
->devfn
),
3792 map
.iova
= n
->start
;
3794 iova_tree_remove(as
->iova_tree
, map
);
3797 static void vtd_address_space_unmap_all(IntelIOMMUState
*s
)
3799 VTDAddressSpace
*vtd_as
;
3802 QLIST_FOREACH(vtd_as
, &s
->vtd_as_with_notifiers
, next
) {
3803 IOMMU_NOTIFIER_FOREACH(n
, &vtd_as
->iommu
) {
3804 vtd_address_space_unmap(vtd_as
, n
);
3809 static void vtd_address_space_refresh_all(IntelIOMMUState
*s
)
3811 vtd_address_space_unmap_all(s
);
3812 vtd_switch_address_space_all(s
);
3815 static int vtd_replay_hook(IOMMUTLBEvent
*event
, void *private)
3817 memory_region_notify_iommu_one(private, event
);
3821 static void vtd_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
3823 VTDAddressSpace
*vtd_as
= container_of(iommu_mr
, VTDAddressSpace
, iommu
);
3824 IntelIOMMUState
*s
= vtd_as
->iommu_state
;
3825 uint8_t bus_n
= pci_bus_num(vtd_as
->bus
);
3829 * The replay can be triggered by either a invalidation or a newly
3830 * created entry. No matter what, we release existing mappings
3831 * (it means flushing caches for UNMAP-only registers).
3833 vtd_address_space_unmap(vtd_as
, n
);
3835 if (vtd_dev_to_context_entry(s
, bus_n
, vtd_as
->devfn
, &ce
) == 0) {
3836 trace_vtd_replay_ce_valid(s
->root_scalable
? "scalable mode" :
3838 bus_n
, PCI_SLOT(vtd_as
->devfn
),
3839 PCI_FUNC(vtd_as
->devfn
),
3840 vtd_get_domain_id(s
, &ce
, vtd_as
->pasid
),
3842 if (vtd_as_has_map_notifier(vtd_as
)) {
3843 /* This is required only for MAP typed notifiers */
3844 vtd_page_walk_info info
= {
3845 .hook_fn
= vtd_replay_hook
,
3846 .private = (void *)n
,
3847 .notify_unmap
= false,
3850 .domain_id
= vtd_get_domain_id(s
, &ce
, vtd_as
->pasid
),
3853 vtd_page_walk(s
, &ce
, 0, ~0ULL, &info
, vtd_as
->pasid
);
3856 trace_vtd_replay_ce_invalid(bus_n
, PCI_SLOT(vtd_as
->devfn
),
3857 PCI_FUNC(vtd_as
->devfn
));
3863 /* Do the initialization. It will also be called when reset, so pay
3864 * attention when adding new initialization stuff.
3866 static void vtd_init(IntelIOMMUState
*s
)
3868 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
3870 memset(s
->csr
, 0, DMAR_REG_SIZE
);
3871 memset(s
->wmask
, 0, DMAR_REG_SIZE
);
3872 memset(s
->w1cmask
, 0, DMAR_REG_SIZE
);
3873 memset(s
->womask
, 0, DMAR_REG_SIZE
);
3876 s
->root_scalable
= false;
3877 s
->dmar_enabled
= false;
3878 s
->intr_enabled
= false;
3883 s
->qi_enabled
= false;
3884 s
->iq_last_desc_type
= VTD_INV_DESC_NONE
;
3886 s
->next_frcd_reg
= 0;
3887 s
->cap
= VTD_CAP_FRO
| VTD_CAP_NFR
| VTD_CAP_ND
|
3888 VTD_CAP_MAMV
| VTD_CAP_PSI
| VTD_CAP_SLLPS
|
3889 VTD_CAP_MGAW(s
->aw_bits
);
3891 s
->cap
|= VTD_CAP_DRAIN
;
3893 if (s
->dma_translation
) {
3894 if (s
->aw_bits
>= VTD_HOST_AW_39BIT
) {
3895 s
->cap
|= VTD_CAP_SAGAW_39bit
;
3897 if (s
->aw_bits
>= VTD_HOST_AW_48BIT
) {
3898 s
->cap
|= VTD_CAP_SAGAW_48bit
;
3901 s
->ecap
= VTD_ECAP_QI
| VTD_ECAP_IRO
;
3904 * Rsvd field masks for spte
3906 vtd_spte_rsvd
[0] = ~0ULL;
3907 vtd_spte_rsvd
[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s
->aw_bits
,
3908 x86_iommu
->dt_supported
);
3909 vtd_spte_rsvd
[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s
->aw_bits
);
3910 vtd_spte_rsvd
[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s
->aw_bits
);
3911 vtd_spte_rsvd
[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s
->aw_bits
);
3913 vtd_spte_rsvd_large
[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s
->aw_bits
,
3914 x86_iommu
->dt_supported
);
3915 vtd_spte_rsvd_large
[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s
->aw_bits
,
3916 x86_iommu
->dt_supported
);
3918 if (s
->scalable_mode
|| s
->snoop_control
) {
3919 vtd_spte_rsvd
[1] &= ~VTD_SPTE_SNP
;
3920 vtd_spte_rsvd_large
[2] &= ~VTD_SPTE_SNP
;
3921 vtd_spte_rsvd_large
[3] &= ~VTD_SPTE_SNP
;
3924 if (x86_iommu_ir_supported(x86_iommu
)) {
3925 s
->ecap
|= VTD_ECAP_IR
| VTD_ECAP_MHMV
;
3926 if (s
->intr_eim
== ON_OFF_AUTO_ON
) {
3927 s
->ecap
|= VTD_ECAP_EIM
;
3929 assert(s
->intr_eim
!= ON_OFF_AUTO_AUTO
);
3932 if (x86_iommu
->dt_supported
) {
3933 s
->ecap
|= VTD_ECAP_DT
;
3936 if (x86_iommu
->pt_supported
) {
3937 s
->ecap
|= VTD_ECAP_PT
;
3940 if (s
->caching_mode
) {
3941 s
->cap
|= VTD_CAP_CM
;
3944 /* TODO: read cap/ecap from host to decide which cap to be exposed. */
3945 if (s
->scalable_mode
) {
3946 s
->ecap
|= VTD_ECAP_SMTS
| VTD_ECAP_SRS
| VTD_ECAP_SLTS
;
3949 if (s
->snoop_control
) {
3950 s
->ecap
|= VTD_ECAP_SC
;
3954 s
->ecap
|= VTD_ECAP_PASID
;
3957 vtd_reset_caches(s
);
3959 /* Define registers with default values and bit semantics */
3960 vtd_define_long(s
, DMAR_VER_REG
, 0x10UL
, 0, 0);
3961 vtd_define_quad(s
, DMAR_CAP_REG
, s
->cap
, 0, 0);
3962 vtd_define_quad(s
, DMAR_ECAP_REG
, s
->ecap
, 0, 0);
3963 vtd_define_long(s
, DMAR_GCMD_REG
, 0, 0xff800000UL
, 0);
3964 vtd_define_long_wo(s
, DMAR_GCMD_REG
, 0xff800000UL
);
3965 vtd_define_long(s
, DMAR_GSTS_REG
, 0, 0, 0);
3966 vtd_define_quad(s
, DMAR_RTADDR_REG
, 0, 0xfffffffffffffc00ULL
, 0);
3967 vtd_define_quad(s
, DMAR_CCMD_REG
, 0, 0xe0000003ffffffffULL
, 0);
3968 vtd_define_quad_wo(s
, DMAR_CCMD_REG
, 0x3ffff0000ULL
);
3970 /* Advanced Fault Logging not supported */
3971 vtd_define_long(s
, DMAR_FSTS_REG
, 0, 0, 0x11UL
);
3972 vtd_define_long(s
, DMAR_FECTL_REG
, 0x80000000UL
, 0x80000000UL
, 0);
3973 vtd_define_long(s
, DMAR_FEDATA_REG
, 0, 0x0000ffffUL
, 0);
3974 vtd_define_long(s
, DMAR_FEADDR_REG
, 0, 0xfffffffcUL
, 0);
3976 /* Treated as RsvdZ when EIM in ECAP_REG is not supported
3977 * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
3979 vtd_define_long(s
, DMAR_FEUADDR_REG
, 0, 0, 0);
3981 /* Treated as RO for implementations that PLMR and PHMR fields reported
3982 * as Clear in the CAP_REG.
3983 * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
3985 vtd_define_long(s
, DMAR_PMEN_REG
, 0, 0, 0);
3987 vtd_define_quad(s
, DMAR_IQH_REG
, 0, 0, 0);
3988 vtd_define_quad(s
, DMAR_IQT_REG
, 0, 0x7fff0ULL
, 0);
3989 vtd_define_quad(s
, DMAR_IQA_REG
, 0, 0xfffffffffffff807ULL
, 0);
3990 vtd_define_long(s
, DMAR_ICS_REG
, 0, 0, 0x1UL
);
3991 vtd_define_long(s
, DMAR_IECTL_REG
, 0x80000000UL
, 0x80000000UL
, 0);
3992 vtd_define_long(s
, DMAR_IEDATA_REG
, 0, 0xffffffffUL
, 0);
3993 vtd_define_long(s
, DMAR_IEADDR_REG
, 0, 0xfffffffcUL
, 0);
3994 /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
3995 vtd_define_long(s
, DMAR_IEUADDR_REG
, 0, 0, 0);
3997 /* IOTLB registers */
3998 vtd_define_quad(s
, DMAR_IOTLB_REG
, 0, 0Xb003ffff00000000ULL
, 0);
3999 vtd_define_quad(s
, DMAR_IVA_REG
, 0, 0xfffffffffffff07fULL
, 0);
4000 vtd_define_quad_wo(s
, DMAR_IVA_REG
, 0xfffffffffffff07fULL
);
4002 /* Fault Recording Registers, 128-bit */
4003 vtd_define_quad(s
, DMAR_FRCD_REG_0_0
, 0, 0, 0);
4004 vtd_define_quad(s
, DMAR_FRCD_REG_0_2
, 0, 0, 0x8000000000000000ULL
);
4007 * Interrupt remapping registers.
4009 vtd_define_quad(s
, DMAR_IRTA_REG
, 0, 0xfffffffffffff80fULL
, 0);
4012 /* Should not reset address_spaces when reset because devices will still use
4013 * the address space they got at first (won't ask the bus again).
4015 static void vtd_reset(DeviceState
*dev
)
4017 IntelIOMMUState
*s
= INTEL_IOMMU_DEVICE(dev
);
4020 vtd_address_space_refresh_all(s
);
4023 static AddressSpace
*vtd_host_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
4025 IntelIOMMUState
*s
= opaque
;
4026 VTDAddressSpace
*vtd_as
;
4028 assert(0 <= devfn
&& devfn
< PCI_DEVFN_MAX
);
4030 vtd_as
= vtd_find_add_as(s
, bus
, devfn
, PCI_NO_PASID
);
4034 static bool vtd_decide_config(IntelIOMMUState
*s
, Error
**errp
)
4036 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
4038 if (s
->intr_eim
== ON_OFF_AUTO_ON
&& !x86_iommu_ir_supported(x86_iommu
)) {
4039 error_setg(errp
, "eim=on cannot be selected without intremap=on");
4043 if (s
->intr_eim
== ON_OFF_AUTO_AUTO
) {
4044 s
->intr_eim
= (kvm_irqchip_in_kernel() || s
->buggy_eim
)
4045 && x86_iommu_ir_supported(x86_iommu
) ?
4046 ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
4048 if (s
->intr_eim
== ON_OFF_AUTO_ON
&& !s
->buggy_eim
) {
4049 if (!kvm_irqchip_is_split()) {
4050 error_setg(errp
, "eim=on requires accel=kvm,kernel-irqchip=split");
4053 if (!kvm_enable_x2apic()) {
4054 error_setg(errp
, "eim=on requires support on the KVM side"
4055 "(X2APIC_API, first shipped in v4.7)");
4060 /* Currently only address widths supported are 39 and 48 bits */
4061 if ((s
->aw_bits
!= VTD_HOST_AW_39BIT
) &&
4062 (s
->aw_bits
!= VTD_HOST_AW_48BIT
)) {
4063 error_setg(errp
, "Supported values for aw-bits are: %d, %d",
4064 VTD_HOST_AW_39BIT
, VTD_HOST_AW_48BIT
);
4068 if (s
->scalable_mode
&& !s
->dma_drain
) {
4069 error_setg(errp
, "Need to set dma_drain for scalable mode");
4073 if (s
->pasid
&& !s
->scalable_mode
) {
4074 error_setg(errp
, "Need to set scalable mode for PASID");
4081 static int vtd_machine_done_notify_one(Object
*child
, void *unused
)
4083 IntelIOMMUState
*iommu
= INTEL_IOMMU_DEVICE(x86_iommu_get_default());
4086 * We hard-coded here because vfio-pci is the only special case
4087 * here. Let's be more elegant in the future when we can, but so
4088 * far there seems to be no better way.
4090 if (object_dynamic_cast(child
, "vfio-pci") && !iommu
->caching_mode
) {
4091 vtd_panic_require_caching_mode();
4097 static void vtd_machine_done_hook(Notifier
*notifier
, void *unused
)
4099 object_child_foreach_recursive(object_get_root(),
4100 vtd_machine_done_notify_one
, NULL
);
4103 static Notifier vtd_machine_done_notify
= {
4104 .notify
= vtd_machine_done_hook
,
4107 static void vtd_realize(DeviceState
*dev
, Error
**errp
)
4109 MachineState
*ms
= MACHINE(qdev_get_machine());
4110 PCMachineState
*pcms
= PC_MACHINE(ms
);
4111 X86MachineState
*x86ms
= X86_MACHINE(ms
);
4112 PCIBus
*bus
= pcms
->bus
;
4113 IntelIOMMUState
*s
= INTEL_IOMMU_DEVICE(dev
);
4114 X86IOMMUState
*x86_iommu
= X86_IOMMU_DEVICE(s
);
4116 if (s
->pasid
&& x86_iommu
->dt_supported
) {
4118 * PASID-based-Device-TLB Invalidate Descriptor is not
4119 * implemented and it requires support from vhost layer which
4120 * needs to be implemented in the future.
4122 error_setg(errp
, "PASID based device IOTLB is not supported");
4126 if (!vtd_decide_config(s
, errp
)) {
4130 QLIST_INIT(&s
->vtd_as_with_notifiers
);
4131 qemu_mutex_init(&s
->iommu_lock
);
4132 memory_region_init_io(&s
->csrmem
, OBJECT(s
), &vtd_mem_ops
, s
,
4133 "intel_iommu", DMAR_REG_SIZE
);
4135 /* Create the shared memory regions by all devices */
4136 memory_region_init(&s
->mr_nodmar
, OBJECT(s
), "vtd-nodmar",
4138 memory_region_init_io(&s
->mr_ir
, OBJECT(s
), &vtd_mem_ir_ops
,
4139 s
, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE
);
4140 memory_region_init_alias(&s
->mr_sys_alias
, OBJECT(s
),
4141 "vtd-sys-alias", get_system_memory(), 0,
4142 memory_region_size(get_system_memory()));
4143 memory_region_add_subregion_overlap(&s
->mr_nodmar
, 0,
4144 &s
->mr_sys_alias
, 0);
4145 memory_region_add_subregion_overlap(&s
->mr_nodmar
,
4146 VTD_INTERRUPT_ADDR_FIRST
,
4149 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->csrmem
);
4150 /* No corresponding destroy */
4151 s
->iotlb
= g_hash_table_new_full(vtd_iotlb_hash
, vtd_iotlb_equal
,
4153 s
->vtd_address_spaces
= g_hash_table_new_full(vtd_as_hash
, vtd_as_equal
,
4156 sysbus_mmio_map(SYS_BUS_DEVICE(s
), 0, Q35_HOST_BRIDGE_IOMMU_ADDR
);
4157 pci_setup_iommu(bus
, vtd_host_dma_iommu
, dev
);
4158 /* Pseudo address space under root PCI bus. */
4159 x86ms
->ioapic_as
= vtd_host_dma_iommu(bus
, s
, Q35_PSEUDO_DEVFN_IOAPIC
);
4160 qemu_add_machine_init_done_notifier(&vtd_machine_done_notify
);
4163 static void vtd_class_init(ObjectClass
*klass
, void *data
)
4165 DeviceClass
*dc
= DEVICE_CLASS(klass
);
4166 X86IOMMUClass
*x86_class
= X86_IOMMU_DEVICE_CLASS(klass
);
4168 dc
->reset
= vtd_reset
;
4169 dc
->vmsd
= &vtd_vmstate
;
4170 device_class_set_props(dc
, vtd_properties
);
4171 dc
->hotpluggable
= false;
4172 x86_class
->realize
= vtd_realize
;
4173 x86_class
->int_remap
= vtd_int_remap
;
4174 /* Supported by the pc-q35-* machine types */
4175 dc
->user_creatable
= true;
4176 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
4177 dc
->desc
= "Intel IOMMU (VT-d) DMA Remapping device";
4180 static const TypeInfo vtd_info
= {
4181 .name
= TYPE_INTEL_IOMMU_DEVICE
,
4182 .parent
= TYPE_X86_IOMMU_DEVICE
,
4183 .instance_size
= sizeof(IntelIOMMUState
),
4184 .class_init
= vtd_class_init
,
4187 static void vtd_iommu_memory_region_class_init(ObjectClass
*klass
,
4190 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
4192 imrc
->translate
= vtd_iommu_translate
;
4193 imrc
->notify_flag_changed
= vtd_iommu_notify_flag_changed
;
4194 imrc
->replay
= vtd_iommu_replay
;
4197 static const TypeInfo vtd_iommu_memory_region_info
= {
4198 .parent
= TYPE_IOMMU_MEMORY_REGION
,
4199 .name
= TYPE_INTEL_IOMMU_MEMORY_REGION
,
4200 .class_init
= vtd_iommu_memory_region_class_init
,
4203 static void vtd_register_types(void)
4205 type_register_static(&vtd_info
);
4206 type_register_static(&vtd_iommu_memory_region_info
);
4209 type_init(vtd_register_types
)