intel_iommu: Enable first stage translation for passthrough device
v9 cover intel_iommu: Enable first stage translation for passthrough device
Hi,
Based on Cédric's suggestions[1], The nesting series v8 is split to "base nesting series" + "ERRATA_772415_SPR17 quirk series", this is the base nesting series.
For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of guest page table but pass first stage page table to host side to construct a nested HWPT. There was some effort to enable this feature in old days, see
[2] for details.
The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform the first stage address translation. Along with it, modifications to present mappings in the guest I/O page table should be followed with an IOTLB invalidation.
.-------------. .---------------------------.
| vIOMMU | | Guest I/O page table |
| | '---------------------------'
.----------------/
| PASID Entry |--- PASID cache flush --+
'-------------' |
| | V
| | I/O page table pointer in GPA
'-------------'
Guest
------| Shadow |---------------------------|--------
v v v
Host
.-------------. .-----------------------------.
| pIOMMU | | First stage for GIOVA->GPA |
| | '-----------------------------'
.----------------/ |
| PASID Entry | V (Nested xlate)
'----------------\.--------------------------------------------.
| | | Second stage for GPA->HPA, unmanaged domain|
| | '--------------------------------------------'
'-------------'<Intel VT-d Nested translation>
This series reuse VFIO device's default HWPT as nesting parent instead of creating new one. This way avoids duplicate code of a new memory listener, all existing feature from VFIO listener can be shared, e.g., ram discard, dirty tracking, etc. Two limitations are: 1) not supporting VFIO device under a PCI bridge with emulated device, because emulated device wants IOMMU AS and VFIO device stick to system AS; 2) not supporting kexec or reboot from "intel_iommu=on,sm_on" to "intel_iommu=on,sm_off" on platform with ERRATA_772415_SPR17, because VFIO device's default HWPT is created with NEST_PARENT flag, kernel inhibit RO mappings when switch to shadow mode.
This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.
There are some interactions between VFIO and vIOMMU
* vIOMMU registers PCIIOMMUOps [set|unset]_iommu_device to PCI subsystem. VFIO calls them to register/unregister HostIOMMUDevice instance to vIOMMU at vfio device realize stage.
* vIOMMU registers PCIIOMMUOps get_viommu_flags to PCI subsystem. VFIO calls it to get vIOMMU exposed flags.
* vIOMMU calls HostIOMMUDeviceIOMMUFD interface [at|de]tach_hwpt to bind/unbind device to IOMMUFD backed domains, either nested domain or not.
See below diagram:
VFIO Device Intel IOMMU
.-----------------. .-------------------.
| | | |
| .---------|PCIIOMMUOps |.-------------. |
| | IOMMUFD |(set/unset_iommu_device) || Host IOMMU | |
| | Device |------------------------>|| Device list | |
| .---------|(get_viommu_flags) |.-------------. |
| | | | |
| | | V |
| .---------| HostIOMMUDeviceIOMMUFD | .-------------. |
| | IOMMUFD | (attach_hwpt)| | Host IOMMU | |
| | link |<------------------------| | Device | |
| .---------| (detach_hwpt)| .-------------. |
| | | | |
| | | ... |
.-----------------. .-------------------.Below is an example to enable first stage translation for passthrough device:
-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthrough
PATCH01-08: Some preparing work PATCH09-10: Compatibility check between vIOMMU and Host IOMMU PATCH11-16: Implement first stage translation for passthrough device PATCH17: Add migration support and optimization PATCH18: Enable first stage translation for passthrough device PATCH19: Add doc
Qemu code can be found at [3], it's based on vfio-next.
Fault event injection to guest isn't supported in this series, we presume guest kernel always construct correct first stage page table for passthrough device. For emulated devices, the emulation code already provided first stage fault injection.
TODO:
- Fault event injection to guest when HW first stage page table faults
[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v9
Thanks
Zhenzhong
Changelog:
v9:
- split v8 to base nesting series + ERRATA_772415_SPR17 series (Cédric)
- s/fs_hwpt/fs_hwpt_id, s/vtd_bind_guest_pasid/vtd_propagate_guest_pasid (Eric)
- polish error msg when CONFIG_VTD_ACCEL isn't defined (Eric)
- refactor hwpt_id assignment in vtd_device_attach_iommufd() (Eric)
v8:
- add hw/i386/intel_iommu_accel.[hc] to hold accel code (Eric)
- return bool for all vtd accel related functions (Cédric, Eric)
- introduce a new PCIIOMMUOps::get_host_iommu_quirks() (Eric, Nicolin)
- minor polishment to comment and code (Cédric, Eric)
- drop some R-b as they have changes needing review again
v7:
- s/host_iommu_extract_vendor_caps/host_iommu_extract_quirks (Nicolin)
- s/RID_PASID/PASID_0 (Eric)
- drop rid2pasid check in vtd_do_iommu_translate (Eric)
- refine DID check in vtd_pasid_cache_sync_locked (Liuyi)
- refine commit log (Nicolin, Eric, Liuyi)
- Fix doc build (Cédric)
- add migration support
v6:
- delete RPS capability related supporting code (Eric, Yi)
- use terminology 'first/second stage' to replace 'first/second level" (Eric, Yi)
- use get_viommu_flags() instead of get_viommu_caps() (Nicolin)
- drop non-RID_PASID related code and simplify pasid invalidation handling (Eric, Yi)
- drop the patch that handle pasid replay when context invalidation (Eric)
- move vendor specific cap check from VFIO core to backend/iommufd.c (Nicolin)
v5:
- refine commit log of patch2 (Cédric, Nicolin)
- introduce helper vfio_pci_from_vfio_device() (Cédric)
- introduce helper vfio_device_viommu_get_nested() (Cédric)
- pass 'bool bypass_ro' argument to vfio_listener_valid_section() instead of 'VFIOContainerBase *' (Cédric)
- fix a potential build error reported by Jim Shu
v4:
- s/VIOMMU_CAP_STAGE1/VIOMMU_CAP_HW_NESTED (Eric, Nicolin, Donald, Shameer)
- clarify get_viommu_cap() return pure emulated caps and explain reason in commit log (Eric)
- retrieve the ce only if vtd_as->pasid in vtd_as_to_iommu_pasid_locked (Eric)
- refine doc comment and commit log in patch10-11 (Eric)
v3:
- define enum type for VIOMMU_CAP_* (Eric)
- drop inline flag in the patch which uses the helper (Eric)
- use extract64 in new introduced MACRO (Eric)
- polish comments and fix typo error (Eric)
- split workaround patch for ERRATA_772415_SPR17 to two patches (Eric)
- optimize bind/unbind error path processing
v2:
- introduce get_viommu_cap() to get STAGE1 flag to create nesting parent HWPT (Liuyi)
- reuse VFIO's default HWPT as parent HWPT of nested translation (Nicolin, Liuyi)
- abandon support of VFIO device under pcie-to-pci bridge to simplify design (Liuyi)
- bypass RO mapping in VFIO's default HWPT if ERRATA_772415_SPR17 (Liuyi)
- drop vtd_dev_to_context_entry optimization (Liuyi)
v1:
- simplify vendor specific checking in vtd_check_hiod (Cédric, Nicolin)
- rebase to master
Yi Liu (3):
intel_iommu_accel: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changed
intel_iommu: Replay pasid bindings after context cache invalidation
Zhenzhong Duan (16):
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Delete RPS capability related supporting code
intel_iommu: Update terminology to match VTD spec
hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool hw/pci: Introduce pci_device_get_viommu_flags()
intel_iommu: Implement get_viommu_flags() callback
intel_iommu: Introduce a new structure VTDHostIOMMUDevice vfio/iommufd: Force creating nesting parent HWPT
intel_iommu_accel: Check for compatibility with IOMMUFD backed device
when x-flts=on
intel_iommu_accel: Fail passthrough device under PCI bridge if
x-flts=on
intel_iommu_accel: Stick to system MR for IOMMUFD backed host device
when x-flts=on
intel_iommu: Add some macros and inline functions
intel_iommu_accel: Bind/unbind guest page table to host
intel_iommu: Add migration support with x-flts=on
intel_iommu: Enable host device when x-flts=on in scalable mode docs/devel: Add IOMMUFD nesting documentation
MAINTAINERS | 2 + docs/devel/vfio-iommufd.rst | 17 ++ hw/i386/intel_iommu_accel.h | 51 ++++ hw/i386/intel_iommu_internal.h | 155 +++++++--- include/hw/i386/intel_iommu.h | 6 +- include/hw/iommu.h | 25 ++ include/hw/pci/pci.h | 24 ++ include/hw/vfio/vfio-device.h | 2 + hw/i386/intel_iommu.c | 528 +++++++++++++++++++-------------- hw/i386/intel_iommu_accel.c | 251 ++++++++++++++++ hw/pci/pci.c | 23 +- hw/vfio/device.c | 12 + hw/vfio/iommufd.c | 9 + tests/qtest/intel-iommu-test.c | 4 +- hw/i386/Kconfig | 5 + hw/i386/meson.build | 1 + hw/i386/trace-events | 4 + 17 files changed, 833 insertions(+), 286 deletions(-) create mode 100644 hw/i386/intel_iommu_accel.h create mode 100644 include/hw/iommu.h create mode 100644 hw/i386/intel_iommu_accel.c
Signature · 1 line
2.47.1
PATCH v9 1/19 intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry
In early days vtd_ce_get_rid2pasid_entry() was used to get pasid entry of rid2pasid, then it was extended to get any pasid entry. So a new name vtd_ce_get_pasid_entry is better to match what it actually does.
No functional change intended.
--- hw/i386/intel_iommu.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
hw/i386/intel_iommu.c +10 -12
@@ -954,10 +954,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,return 0 ; } -static int vtd_ce_get_rid2pasid_entry (IntelIOMMUState *s, -VTDContextEntry *ce, -VTDPASIDEntry *pe, -uint32_t pasid) +static int vtd_ce_get_pasid_entry (IntelIOMMUState *s,VTDContextEntry *ce, +VTDPASIDEntry *pe,uint32_t pasid) {dma_addr_t pasid_dir_base;int ret =0 ;
@@ -1035,7 +1033,7 @@ static uint32_t vtd_get_iova_level(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->flts) {return VTD_PE_GET_FL_LEVEL (&pe); }else {
@@ -1058,7 +1056,7 @@ static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);return 30 + ((pe.val[0 ] >>2 ) &VTD_SM_PASID_ENTRY_AW ) *9 ; }
@@ -1126,7 +1124,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->flts) {return pe.val[2 ] &VTD_SM_PASID_ENTRY_FLPTPTR ; }else {
@@ -1532,7 +1530,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,* has valid rid2pasid setting, which includes valid * rid2pasid field and corresponding pasid entry setting */ -return vtd_ce_get_rid2pasid_entry (s, ce, &pe,PCI_NO_PASID ); +return vtd_ce_get_pasid_entry (s, ce, &pe,PCI_NO_PASID ); }/* Map a device to its corresponding domain (context-entry) */
@@ -1621,7 +1619,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,VTDPASIDEntry pe;if (s->root_scalable) { -vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); +vtd_ce_get_pasid_entry (s, ce, &pe, pasid);return VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); }
@@ -1697,7 +1695,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,int ret;if (s->root_scalable) { - ret =vtd_ce_get_rid2pasid_entry (s, ce, &pe, pasid); + ret =vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (ret) {/* * This error is guest triggerable. We should assumt PT
@@ -3083,7 +3081,7 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,return ret; } -return vtd_ce_get_rid2pasid_entry (s, &ce, pe, vtd_as->pasid); +return vtd_ce_get_pasid_entry (s, &ce, pe, vtd_as->pasid); }static int vtd_pasid_entry_compare (VTDPASIDEntry *p1,VTDPASIDEntry *p2)
@@ -5161,7 +5159,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,if (ret) {return -EINVAL ; } - ret =vtd_ce_get_rid2pasid_entry (s, &ce, &pe, vtd_as->pasid); + ret =vtd_ce_get_pasid_entry (s, &ce, &pe, vtd_as->pasid);if (ret) {return -EINVAL ; }
Signature · 1 line
2.47.1
PATCH v9 2/19 intel_iommu: Delete RPS capability related supporting code
In reply to Zhenzhong DuanRID-PASID Support(RPS) is not set in vIOMMU ECAP register, the supporting code is there but never takes effect.
Meanwhile, according to VTD spec section 3.4.3:
"Implementations not supporting RID_PASID capability (ECAP_REG.RPS is 0b), use a PASID value of 0 to perform address translation for requests without PASID."
We should delete the supporting code which fetches RID_PASID field from scalable context entry and use 0 as RID_PASID directly, because RID_PASID field is ignored if no RPS support according to spec.
This simplifies the code and doesn't bring any penalty.
--- hw/i386/intel_iommu_internal.h | 2 +- hw/i386/intel_iommu.c | 89 +++++++++++----------------------- 2 files changed, 28 insertions(+), 63 deletions(-)
hw/i386/intel_iommu_internal.h +1 -1
@@ -609,7 +609,7 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CTX_ENTRY_LEGACY_SIZE 16#define VTD_CTX_ENTRY_SCALABLE_SIZE 32 -#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff +#define PASID_0 0#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL#define VTD_SM_CONTEXT_ENTRY_PRE 0x10ULL
hw/i386/intel_iommu.c +27 -62
175 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v9 3/19 intel_iommu: Update terminology to match VTD spec
In reply to Zhenzhong DuanVTD spec revision 3.4 released in December 2021 renamed "First-level" to "First-stage" and "Second-level" to "Second-stage".
Do the same in intel_iommu code to match spec, change all existing "fl/sl/FL/SL/first level/second level/stage-1/stage-2" terminology to "fs/ss/FS/SS/first stage/second stage".
Opportunistically fix a error print of "flts=on" with "x-flts=on".
No functional changes intended.
--- hw/i386/intel_iommu_internal.h | 85 ++++++------ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 247 +++++++++++++++++---------------- tests/qtest/intel-iommu-test.c | 4 +- 4 files changed, 170 insertions(+), 168 deletions(-)
hw/i386/intel_iommu_internal.h +43 -42
@@ -196,8 +196,8 @@#define VTD_ECAP_PSS (7ULL << 35)/* limit: MemTxAttrs::pid */ #define VTD_ECAP_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43) -#define VTD_ECAP_SLTS (1ULL << 46) -#define VTD_ECAP_FLTS (1ULL << 47) +#define VTD_ECAP_SSTS (1ULL << 46) +#define VTD_ECAP_FSTS (1ULL << 47)/* CAP_REG */ /* (offset >> 4) << 24 */
@@ -211,7 +211,7 @@#define VTD_MAMV 18ULL#define VTD_CAP_MAMV (VTD_MAMV << 48)#define VTD_CAP_PSI (1ULL << 39) -#define VTD_CAP_SLLPS ((1ULL << 34) | (1ULL << 35)) +#define VTD_CAP_SSLPS ((1ULL << 34) | (1ULL << 35))#define VTD_CAP_DRAIN_WRITE (1ULL << 54)#define VTD_CAP_DRAIN_READ (1ULL << 55)#define VTD_CAP_FS1GP (1ULL << 56)
@@ -284,7 +284,7 @@ typedef enum VTDFaultReason {VTD_FR_ADDR_BEYOND_MGAW ,/* Input-address above (2^x-1) */ VTD_FR_WRITE ,/* No write permission */ VTD_FR_READ ,/* No read permission */ -/* Fail to access a second-level paging entry (not SL_PML4E) */ +/* Fail to access a second-stage paging entry (not SS_PML4E) */ VTD_FR_PAGING_ENTRY_INV ,VTD_FR_ROOT_TABLE_INV ,/* Fail to access a root-entry */ VTD_FR_CONTEXT_TABLE_INV, /* Fail to access a context-entry */
@@ -292,7 +292,8 @@ typedef enum VTDFaultReason {VTD_FR_ROOT_ENTRY_RSVD ,/* Non-zero reserved field in a present context-entry */ VTD_FR_CONTEXT_ENTRY_RSVD , -/* Non-zero reserved field in a second-level paging entry with at lease one +/* +* Non-zero reserved field in a second-stage paging entry with at lease one * Read(R) and Write(W) or Execute(E) field is Set. */ VTD_FR_PAGING_ENTRY_RSVD,
@@ -329,7 +330,7 @@ typedef enum VTDFaultReason {VTD_FR_PASID_ENTRY_P =0x59 ,VTD_FR_PASID_TABLE_ENTRY_INV =0x5b ,/*Invalid PASID table entry */ -/* Fail to access a first-level paging entry (not FS_PML4E) */ +/* Fail to access a first-stage paging entry (not FS_PML4E) */ VTD_FR_FS_PAGING_ENTRY_INV =0x70 , VTD_FR_FS_PAGING_ENTRY_P = 0x71,/* Non-zero reserved field in present first-stage paging entry */
@@ -473,23 +474,23 @@ typedef union VTDPRDesc VTDPRDesc;#define VTD_SPTE_PAGE_L1_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L2_RSVD_MASK (aw) \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L3_RSVD_MASK (aw) \ - (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_PAGE_L4_RSVD_MASK (aw) \ - (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_LPAGE_L2_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))#define VTD_SPTE_LPAGE_L3_RSVD_MASK (aw, stale_tm) \ stale_tm ? \ - (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \ - (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) + (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \ + (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))/* Rsvd field masks for fpte */ #define VTD_FS_UPPER_IGNORED 0xfff0000000000000ULL
@@ -596,8 +597,8 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CONTEXT_TT_MULTI_LEVEL 0#define VTD_CONTEXT_TT_DEV_IOTLB (1ULL << 2)#define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2) -/* Second Level Page Translation Pointer*/ -#define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL) +/* Second Stage Page Translation Pointer*/ +#define VTD_CONTEXT_ENTRY_SSPTPTR (~0xfffULL)#define VTD_CONTEXT_ENTRY_RSVD_LO (aw) (0xff0ULL | ~VTD_HAW_MASK(aw))/* hi */ #define VTD_CONTEXT_ENTRY_AW 7ULL/* Adjusted guest-address-width */
@@ -635,37 +636,37 @@ typedef struct VTDPASIDCacheInfo {/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL#define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6) -#define VTD_SM_PASID_ENTRY_FLT (1ULL << 6) -#define VTD_SM_PASID_ENTRY_SLT (2ULL << 6) +#define VTD_SM_PASID_ENTRY_FST (1ULL << 6) +#define VTD_SM_PASID_ENTRY_SST (2ULL << 6)#define VTD_SM_PASID_ENTRY_NESTED (3ULL << 6)#define VTD_SM_PASID_ENTRY_PT (4ULL << 6)#define VTD_SM_PASID_ENTRY_AW 7ULL/* Adjusted guest-address-width */ #define VTD_SM_PASID_ENTRY_DID (val) ((val) & VTD_DOMAIN_ID_MASK) -#define VTD_SM_PASID_ENTRY_FLPM 3ULL -#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL) - -/* First Level Paging Structure */ -/* Masks for First Level Paging Entry */ -#define VTD_FL_P 1ULL -#define VTD_FL_RW (1ULL << 1) -#define VTD_FL_US (1ULL << 2) -#define VTD_FL_A (1ULL << 5) -#define VTD_FL_D (1ULL << 6) - -/* Second Level Page Translation Pointer*/ -#define VTD_SM_PASID_ENTRY_SLPTPTR (~0xfffULL) - -/* Second Level Paging Structure */ -/* Masks for Second Level Paging Entry */ -#define VTD_SL_RW_MASK 3ULL -#define VTD_SL_R 1ULL -#define VTD_SL_W (1ULL << 1) -#define VTD_SL_IGN_COM 0xbff0000000000000ULL -#define VTD_SL_TM (1ULL << 62) - -/* Common for both First Level and Second Level */ +#define VTD_SM_PASID_ENTRY_FSPM 3ULL +#define VTD_SM_PASID_ENTRY_FSPTPTR (~0xfffULL) + +/* First Stage Paging Structure */ +/* Masks for First Stage Paging Entry */ +#define VTD_FS_P 1ULL +#define VTD_FS_RW (1ULL << 1) +#define VTD_FS_US (1ULL << 2) +#define VTD_FS_A (1ULL << 5) +#define VTD_FS_D (1ULL << 6) + +/* Second Stage Page Translation Pointer*/ +#define VTD_SM_PASID_ENTRY_SSPTPTR (~0xfffULL) + +/* Second Stage Paging Structure */ +/* Masks for Second Stage Paging Entry */ +#define VTD_SS_RW_MASK 3ULL +#define VTD_SS_R 1ULL +#define VTD_SS_W (1ULL << 1) +#define VTD_SS_IGN_COM 0xbff0000000000000ULL +#define VTD_SS_TM (1ULL << 62) + +/* Common for both First Stage and Second Stage */ #define VTD_PML4_LEVEL 4#define VTD_PDP_LEVEL 3#define VTD_PD_LEVEL 2
include/hw/i386/intel_iommu.h +1 -1
@@ -271,7 +271,7 @@ struct IntelIOMMUState {bool caching_mode;/* RO - is cap CM enabled? */ bool scalable_mode;/* RO - is Scalable Mode supported? */ -bool flts;/* RO - is stage-1 translation supported? */ +bool fsts;/* RO - is first stage translation supported? */ bool snoop_control;/* RO - is SNP filed supported? */ dma_addr_t root;/* Current root table pointer */
hw/i386/intel_iommu.c +124 -123
611 lines not on this page · raw message
tests/qtest/intel-iommu-test.c +2 -2
@@ -13,9 +13,9 @@#include "hw/i386/intel_iommu_internal.h" #define CAP_STAGE_1_FIXED1 (VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | \ - VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS) + VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SSLPS)#define ECAP_STAGE_1_FIXED1 (VTD_ECAP_QI | VTD_ECAP_IR | VTD_ECAP_IRO | \ - VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FLTS) + VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FSTS)static inline uint64_t vtd_reg_readq (QTestState *s,uint64_t offset) {
Signature · 1 line
2.47.1
PATCH v9 4/19 hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool
In reply to Zhenzhong DuanReturns true if PCI device is aliased or false otherwise. This will be used in following patch to determine if a PCI device is under a PCI bridge.
--- include/hw/pci/pci.h | 2 ++ hw/pci/pci.c | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +2 -0
@@ -637,6 +637,8 @@ typedef struct PCIIOMMUOps { bool is_write); } PCIIOMMUOps; +bool pci_device_get_iommu_bus_devfn (PCIDevice *dev,PCIBus **piommu_bus, +PCIBus **aliased_bus,int *aliased_devfn);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);bool pci_device_set_iommu_device (PCIDevice *dev,HostIOMMUDevice *hiod,Error **errp);
hw/pci/pci.c +8 -4
@@ -2869,20 +2869,21 @@ static void pci_device_class_base_init(ObjectClass *klass, const void *data)* For call sites which don't need aliased BDF, passing NULL to * aliased_[bus|devfn] is allowed. * +* Returns true if PCI device RID is aliased or false otherwise. +* * @piommu_bus: return root #PCIBus backed by an IOMMU for the PCI device. * * @aliased_bus: return aliased #PCIBus of the PCI device, optional. * * @aliased_devfn: return aliased devfn of the PCI device, optional. */ -static void pci_device_get_iommu_bus_devfn (PCIDevice *dev, -PCIBus **piommu_bus, -PCIBus **aliased_bus, -int *aliased_devfn) +bool pci_device_get_iommu_bus_devfn (PCIDevice *dev,PCIBus **piommu_bus, +PCIBus **aliased_bus,int *aliased_devfn) {PCIBus *bus =pci_get_bus (dev);PCIBus *iommu_bus = bus;int devfn = dev->devfn; +bool aliased = false;while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) {PCIBus *parent_bus =pci_get_bus (iommu_bus->parent_dev);
@@ -2919,6 +2920,7 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev, devfn = parent->devfn; bus = parent_bus; } + aliased = true; }/*
@@ -2953,6 +2955,8 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,if (aliased_devfn) { *aliased_devfn = devfn; } + +return aliased; }AddressSpace *pci_device_iommu_address_space (PCIDevice *dev)
Signature · 1 line
2.47.1
PATCH v9 5/19 hw/pci: Introduce pci_device_get_viommu_flags()
In reply to Zhenzhong DuanIntroduce a new PCIIOMMUOps optional callback, get_viommu_flags() which allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.
pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:
1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.
Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU through the set_iommu_device() callback that will be after the attach_device().
See below sequence:
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)
--- MAINTAINERS | 1 + include/hw/iommu.h | 25 +++++++++++++++++++++++++ include/hw/pci/pci.h | 22 ++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 59 insertions(+) create mode 100644 include/hw/iommu.h
MAINTAINERS +1 -0
@@ -2346,6 +2346,7 @@ F: include/system/iommufd.h F: backends/host_iommu_device.c F: include/system/host_iommu_device.h F: include/qemu/chardev_open.h +F: include/hw/iommu.h F: util/chardev_open.c F: docs/devel/vfio-iommufd.rst
include/hw/iommu.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* General vIOMMU flags +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +#include "qemu/bitops.h" + +/* +* Theoretical vIOMMU flags. Only determined by the vIOMMU device properties and +* independent on the actual host IOMMU capabilities they may depend on. Each +* flag can be an expectation or request to other sub-system or just a pure +* vIOMMU capability. vIOMMU can choose which flags to expose. +*/ +enum viommu_flags { +/* vIOMMU needs nesting parent HWPT to create nested HWPT */ +VIOMMU_FLAG_WANT_NESTING_PARENT =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +22 -0
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_flags: get vIOMMU flags +* +* Optional callback, if not implemented, then vIOMMU doesn't support +* exposing flags to other sub-system, e.g., VFIO. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. +*/ +uint64_t (*get_viommu_flags)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, Error **errp);void pci_device_unset_iommu_device (PCIDevice *dev); +/** +* pci_device_get_viommu_flags: get vIOMMU flags. +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. Or 0 if vIOMMU doesn't report any. +* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_flags (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB.
hw/pci/pci.c +11 -0
@@ -3021,6 +3021,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_flags (PCIDevice *dev) +{ +PCIBus *iommu_bus; + +pci_device_get_iommu_bus_devfn (dev, &iommu_bus,NULL ,NULL ); +if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) { +return iommu_bus->iommu_ops->get_viommu_flags (iommu_bus->iommu_opaque); + } +return 0 ; +} +int pci_pri_request_page (PCIDevice *dev,uint32_t pasid,bool priv_req,bool exec_req,hwaddr addr,bool lpig,uint16_t prgi,bool is_read,bool is_write)
Signature · 1 line
2.47.1
Re: [PATCH v9 05/19] hw/pci: Introduce pci_device_get_viommu_flags()
Hi,
Quoted 38 lines · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_flags()which allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag isVIOMMU_FLAG_WANT_NESTING_PARENTthat advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.
pci_device_get_viommu_flags()is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_flags()is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.Note that this op will be invoked at the
attach_device()stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU through theset_iommu_device()callback that will be after theattach_device().See below sequence:
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags()for HW nesting cap create a nesting parent HWPT
attach device to the HWPTvfio_device_hiod_create_and_realize()creating hiod
...
pci_device_set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Eric Auger <[email protected]>Reviewed-by Yi Liu <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Quoted 3 lines
--- MAINTAINERS | 1 + include/hw/iommu.h | 25 +++++++++++++++++++++++++
All headers under include/hw/ have been moved to include/hw/core/. iommu.h should also be moved under the same directory.
The series needs a respin because of these changes.
Thanks,
C.
Quoted 110 lines
include/hw/pci/pci.h | 22 ++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 59 insertions(+)create mode 100644
include/hw/iommu.hMAINTAINERS +1
-0@@ -2346,6 +2346,7 @@ F: include/system/iommufd.h F: backends/host_iommu_device.c F: include/system/host_iommu_device.h F: include/qemu/chardev_open.h +F: include/hw/iommu.h F: util/chardev_open.c F: docs/devel/vfio-iommufd.rstinclude/hw/iommu.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* General vIOMMU flags +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +#include "qemu/bitops.h" + +/* +* Theoretical vIOMMU flags. Only determined by the vIOMMU device properties and +* independent on the actual host IOMMU capabilities they may depend on. Each +* flag can be an expectation or request to other sub-system or just a pure +* vIOMMU capability. vIOMMU can choose which flags to expose. +*/ +enum viommu_flags { +/* vIOMMU needs nesting parent HWPT to create nested HWPT */ +VIOMMU_FLAG_WANT_NESTING_PARENT =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */ include/hw/pci/pci.h +22
-0@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_flags: get vIOMMU flags +* +* Optional callback, if not implemented, then vIOMMU doesn't support +* exposing flags to other sub-system, e.g., VFIO. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. +*/ +uint64_t (*get_viommu_flags)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. * @@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, Error **errp);void pci_device_unset_iommu_device (PCIDevice *dev); +/** +* pci_device_get_viommu_flags: get vIOMMU flags. +* +* Returns: bitmap with each bit representing a vIOMMU flag defined in +* enum viommu_flags. Or 0 if vIOMMU doesn't report any. +* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_flags (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB. hw/pci/pci.c +11
-0@@ -3021,6 +3021,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_flags (PCIDevice *dev) +{ +PCIBus *iommu_bus; + +pci_device_get_iommu_bus_devfn (dev, &iommu_bus,NULL ,NULL ); +if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) { +return iommu_bus->iommu_ops->get_viommu_flags (iommu_bus->iommu_opaque); + } +return 0 ; +} +int pci_pri_request_page (PCIDevice *dev,uint32_t pasid,bool priv_req,bool exec_req,hwaddr addr,bool lpig,uint16_t prgi,bool is_read,bool is_write)
RE: [PATCH v9 05/19] hw/pci: Introduce pci_device_get_viommu_flags()
Quoted 52 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v9 05/19] hw/pci: Introducepci_device_get_viommu_flags()Hi,
Quoted 1 line · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_flags()which
Quoted 12 lines
allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is
VIOMMU_FLAG_WANT_NESTING_PARENTthat advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.
pci_device_get_viommu_flags()is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_flags()is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:Quoted 6 lines
1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.Note that this op will be invoked at the
attach_device()stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMUthrough
Quoted 19 lines
the
set_iommu_device()callback that will be after theattach_device().See below sequence:
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags()for HW nesting cap create a nesting parent HWPT
attach device to the HWPTvfio_device_hiod_create_and_realize()creating hiod
...
pci_device_set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Eric Auger <[email protected]>Reviewed-by Yi Liu <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>Quoted 3 lines
--- MAINTAINERS | 1 + include/hw/iommu.h | 25 +++++++++++++++++++++++++
Quoted 5 lines
All headers under
include/hw/have been moved toinclude/hw/core/.iommu.h
should also be moved under the same directory.The series needs a respin because of these changes.
Thanks for reminder, I just sent v10 out for below two series.
[PATCH v9 00/19] intel_iommu: Enable first stage translation for passthrough device [PATCH v9 0/4] Implement ERRATA_772415 quirk for VTD
BRs,
Zhenzhong
PATCH v9 6/19 intel_iommu: Implement get_viommu_flags() callback
In reply to Zhenzhong DuanImplement get_viommu_flags() callback and expose a request for nesting parent HWPT for now.
VFIO uses it to create nesting parent HWPT which is further used to create nested HWPT in vIOMMU. All these will be implemented in following patches.
--- hw/i386/intel_iommu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/i386/intel_iommu.c +12 -0
@@ -24,6 +24,7 @@#include "qemu/main-loop.h" #include "qapi/error.h" #include "hw/sysbus.h" +#include "hw/iommu.h" #include "intel_iommu_internal.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bus.h"
@@ -4659,6 +4660,16 @@ static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)vtd_iommu_unlock (s); } +static uint64_t vtd_get_viommu_flags (void *opaque) +{ +IntelIOMMUState *s = opaque; +uint64_t flags; + + flags = s->fsts ?VIOMMU_FLAG_WANT_NESTING_PARENT :0 ; + +return flags; +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {
@@ -5312,6 +5323,7 @@ static PCIIOMMUOps vtd_iommu_ops = { .pri_register_notifier = vtd_pri_register_notifier, .pri_unregister_notifier = vtd_pri_unregister_notifier, .pri_request_page = vtd_pri_request_page, + .get_viommu_flags = vtd_get_viommu_flags, };static bool vtd_decide_config (IntelIOMMUState *s,Error **errp)
Signature · 1 line
2.47.1
PATCH v9 7/19 intel_iommu: Introduce a new structure VTDHostIOMMUDevice
In reply to Zhenzhong DuanIntroduce a new structure VTDHostIOMMUDevice which replaces HostIOMMUDevice to be stored in hash table.
It includes a reference to HostIOMMUDevice and IntelIOMMUState, also includes BDF information which will be used in future patches.
--- hw/i386/intel_iommu_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +7 -0
@@ -28,6 +28,7 @@#ifndef HW_I386_INTEL_IOMMU_INTERNAL_H #define HW_I386_INTEL_IOMMU_INTERNAL_H #include "hw/i386/intel_iommu.h" +#include "system/host_iommu_device.h" /* * Intel IOMMU register specification
@@ -678,4 +679,10 @@ typedef struct VTDPASIDCacheInfo {/* Bits to decide the offset for each level */ #define VTD_LEVEL_BITS 9 +typedef struct VTDHostIOMMUDevice { +IntelIOMMUState *iommu_state; +PCIBus *bus; +uint8_t devfn; +HostIOMMUDevice *hiod; +}VTDHostIOMMUDevice ;#endif
include/hw/i386/intel_iommu.h +1 -1
@@ -302,7 +302,7 @@ struct IntelIOMMUState {/* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; -GHashTable *vtd_host_iommu_dev;/* HostIOMMUDevice */ +GHashTable *vtd_host_iommu_dev;/* VTDHostIOMMUDevice */ /* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */
hw/i386/intel_iommu.c +13 -2
@@ -288,7 +288,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1, gconstpointer v2)static void vtd_hiod_destroy (gpointer v) { -object_unref (v); +VTDHostIOMMUDevice *vtd_hiod = v; + +object_unref (vtd_hiod->hiod); +g_free (vtd_hiod); } static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
@@ -4601,6 +4604,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, HostIOMMUDevice *hiod, Error **errp) {IntelIOMMUState *s = opaque; +VTDHostIOMMUDevice *vtd_hiod;struct vtd_as_key key = { .bus = bus, .devfn = devfn,
@@ -4623,7 +4627,14 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,return false; } + vtd_hiod =g_malloc0 (sizeof (VTDHostIOMMUDevice)); + vtd_hiod->bus = bus; + vtd_hiod->devfn = (uint8_t )devfn; + vtd_hiod->iommu_state = s; + vtd_hiod->hiod = hiod; +if (!vtd_check_hiod (s, hiod, errp)) { +g_free (vtd_hiod);vtd_iommu_unlock (s);return false; }
@@ -4633,7 +4644,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, new_key->devfn = devfn;object_ref (hiod); -g_hash_table_insert (s->vtd_host_iommu_dev, new_key, hiod); +g_hash_table_insert (s->vtd_host_iommu_dev, new_key, vtd_hiod);vtd_iommu_unlock (s);
Signature · 1 line
2.47.1
PATCH v9 8/19 vfio/iommufd: Force creating nesting parent HWPT
In reply to Zhenzhong DuanCall pci_device_get_viommu_flags() to get if vIOMMU supports VIOMMU_FLAG_WANT_NESTING_PARENT.
If yes, create a nesting parent HWPT and add it to the container's hwpt_list, letting this parent HWPT cover the entire second stage mappings (GPA=>HPA).
This allows a VFIO passthrough device to directly attach to this default HWPT and then to use the system address space and its listener.
Introduce a vfio_device_get_viommu_flags_want_nesting() helper to facilitate this implementation.
It is safe to do so because a vIOMMU will be able to fail in set_iommu_device() call, if something else related to the VFIO device or vIOMMU isn't compatible.
--- include/hw/vfio/vfio-device.h | 2 ++ hw/vfio/device.c | 12 ++++++++++++ hw/vfio/iommufd.c | 9 +++++++++ 3 files changed, 23 insertions(+)
include/hw/vfio/vfio-device.h +2 -0
@@ -257,6 +257,8 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,void vfio_device_unprepare (VFIODevice *vbasedev); +bool vfio_device_get_viommu_flags_want_nesting (VFIODevice *vbasedev); +int vfio_device_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info); int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
hw/vfio/device.c +12 -0
@@ -23,6 +23,7 @@#include "hw/vfio/vfio-device.h" #include "hw/vfio/pci.h" +#include "hw/iommu.h" #include "hw/hw.h" #include "trace.h" #include "qapi/error.h"
@@ -515,6 +516,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev) vbasedev->bcontainer =NULL ; } +bool vfio_device_get_viommu_flags_want_nesting (VFIODevice *vbasedev) +{ +VFIOPCIDevice *vdev =vfio_pci_from_vfio_device (vbasedev); + +if (vdev) { +return !!(pci_device_get_viommu_flags (PCI_DEVICE (vdev)) & +VIOMMU_FLAG_WANT_NESTING_PARENT ); + } +return false; +} +/* * Traditional ioctl() based io */
hw/vfio/iommufd.c +9 -0
@@ -361,6 +361,15 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU requests VFIO's cooperation to create nesting parent HWPT, +* force to create it so that it could be reused by vIOMMU to create +* nested HWPT. +*/ +if (vfio_device_get_viommu_flags_want_nesting (vbasedev)) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } +if (cpr_is_incoming ()) { hwpt_id = vbasedev->cpr.hwpt_id; goto skip_alloc;
Signature · 1 line
2.47.1
PATCH v9 9/19 intel_iommu_accel: Check for compatibility with IOMMUFD backed device when x-flts=on
In reply to Zhenzhong DuanWhen vIOMMU is configured x-flts=on in scalable mode, first stage page table is passed to host to construct nested page table for passthrough devices.
We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest first stage page table could be used by host.
For instance, vIOMMU supports first stage 1GB large page mapping, but host does not, then this IOMMUFD backed device should fail.
Even of the checks pass, for now we willingly reject the association because all the bits are not there yet, it will be relaxed in the end of this series.
Note vIOMMU has exposed IOMMU_HWPT_ALLOC_NEST_PARENT flag to force VFIO core to create nesting parent HWPT, if host doesn't support nested translation, the creation will fail. So no need to check nested capability here.
--- MAINTAINERS | 1 + hw/i386/intel_iommu_accel.h | 28 +++++++++++++++++++++++++ hw/i386/intel_iommu.c | 5 ++--- hw/i386/intel_iommu_accel.c | 42 +++++++++++++++++++++++++++++++++++++ hw/i386/Kconfig | 5 +++++ hw/i386/meson.build | 1 + 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 hw/i386/intel_iommu_accel.h create mode 100644 hw/i386/intel_iommu_accel.c
MAINTAINERS +1 -0
@@ -3931,6 +3931,7 @@ R: Clément Mathieu--Drif <[email protected]> S: Supported F: hw/i386/intel_iommu.c F: hw/i386/intel_iommu_internal.h +F: hw/i386/intel_iommu_accel.* F: include/hw/i386/intel_iommu.h F: tests/functional/x86_64/test_intel_iommu.py F: tests/qtest/intel-iommu-test.c
hw/i386/intel_iommu_accel.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Intel IOMMU acceleration with nested translation +* +* Copyright (C) 2025 Intel Corporation. +* +* Authors: Zhenzhong Duan <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_I386_INTEL_IOMMU_ACCEL_H +#define HW_I386_INTEL_IOMMU_ACCEL_H +#include CONFIG_DEVICES + +#ifdef CONFIG_VTD_ACCEL +bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +Error **errp); +#else +static inline bool vtd_check_hiod_accel (IntelIOMMUState *s, +HostIOMMUDevice *hiod, +Error **errp) +{ +error_setg (errp,"host IOMMU cannot be checked!" ); +error_append_hint (errp,"CONFIG_VTD_ACCEL is not enabled" ); +return false; +} +#endif +#endif
hw/i386/intel_iommu.c +2 -3
@@ -26,6 +26,7 @@#include "hw/sysbus.h" #include "hw/iommu.h" #include "intel_iommu_internal.h" +#include "intel_iommu_accel.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bus.h" #include "hw/qdev-properties.h"
@@ -4595,9 +4596,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } -error_setg (errp, -"host device is uncompatible with first stage translation" ); -return false; +return vtd_check_hiod_accel (s, hiod, errp); } static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
hw/i386/intel_iommu_accel.c new file +42 -0
42 lines not on this page · raw message
hw/i386/Kconfig +5 -0
@@ -150,8 +150,13 @@ config X86_IOMMU config VTD bool + imply VTD_ACCEL select X86_IOMMU +config VTD_ACCEL + bool + depends on VTD && IOMMUFD + config AMD_IOMMU bool select X86_IOMMU
hw/i386/meson.build +1 -0
@@ -21,6 +21,7 @@ i386_ss.add(when: 'CONFIG_Q35', if_true: files('pc_q35.c')) i386_ss.add(when: 'CONFIG_VMMOUSE', if_true: files('vmmouse.c')) i386_ss.add(when: 'CONFIG_VMPORT', if_true: files('vmport.c')) i386_ss.add(when: 'CONFIG_VTD', if_true: files('intel_iommu.c')) +i386_ss.add(when: 'CONFIG_VTD_ACCEL', if_true: files('intel_iommu_accel.c')) i386_ss.add(when: 'CONFIG_SGX', if_true: files('sgx-epc.c','sgx.c'), if_false: files('sgx-stub.c'))
Signature · 1 line
2.47.1
PATCH v9 10/19 intel_iommu_accel: Fail passthrough device under PCI bridge if x-flts=on
In reply to Zhenzhong DuanCurrently we don't support nested translation for passthrough device with emulated device under same PCI bridge, because they require different address space when x-flts=on.
In theory, we do support if devices under same PCI bridge are all passthrough devices. But emulated device can be hotplugged under same bridge. To simplify, just forbid passthrough device under PCI bridge no matter if there is, or will be emulated devices under same bridge. This is acceptable because PCIE bridge is more popular than PCI bridge now.
--- hw/i386/intel_iommu_accel.h | 4 ++-- hw/i386/intel_iommu.c | 7 ++++--- hw/i386/intel_iommu_accel.c | 12 +++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_accel.h +2 -2
@@ -13,11 +13,11 @@#include CONFIG_DEVICES #ifdef CONFIG_VTD_ACCEL -bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp);#else static inline bool vtd_check_hiod_accel (IntelIOMMUState *s, -HostIOMMUDevice *hiod, +VTDHostIOMMUDevice *vtd_hiod,Error **errp) {error_setg (errp,"host IOMMU cannot be checked!" );
hw/i386/intel_iommu.c +4 -3
@@ -4570,9 +4570,10 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,return vtd_dev_as; } -static bool vtd_check_hiod (IntelIOMMUState *s,HostIOMMUDevice *hiod, +static bool vtd_check_hiod (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp) { +HostIOMMUDevice *hiod = vtd_hiod->hiod;HostIOMMUDeviceClass *hiodc =HOST_IOMMU_DEVICE_GET_CLASS (hiod);int ret;
@@ -4596,7 +4597,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } -return vtd_check_hiod_accel (s, hiod, errp); +return vtd_check_hiod_accel (s, vtd_hiod, errp); } static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
@@ -4632,7 +4633,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, vtd_hiod->iommu_state = s; vtd_hiod->hiod = hiod; -if (!vtd_check_hiod (s, hiod, errp)) { +if (!vtd_check_hiod (s, vtd_hiod, errp)) {g_free (vtd_hiod);vtd_iommu_unlock (s);return false;
hw/i386/intel_iommu_accel.c +11 -1
@@ -12,12 +12,16 @@#include "system/iommufd.h" #include "intel_iommu_internal.h" #include "intel_iommu_accel.h" +#include "hw/pci/pci_bus.h" -bool vtd_check_hiod_accel (IntelIOMMUState *s,HostIOMMUDevice *hiod, +bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp) { +HostIOMMUDevice *hiod = vtd_hiod->hiod;struct HostIOMMUDeviceCaps *caps = &hiod->caps;struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd; +PCIBus *bus = vtd_hiod->bus; +PCIDevice *pdev = bus->devices[vtd_hiod->devfn];if (!object_dynamic_cast (OBJECT (hiod),TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) {error_setg (errp,"Need IOMMUFD backend when x-flts=on" );
@@ -36,6 +40,12 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, HostIOMMUDevice *hiod,return false; } +if (pci_device_get_iommu_bus_devfn (pdev, &bus,NULL ,NULL )) { +error_setg (errp,"Host device downstream to a PCI bridge is " +"unsupported when x-flts=on" ); +return false; + } +error_setg (errp,"host IOMMU is incompatible with guest first stage translation" );return false;
Signature · 1 line
2.47.1
PATCH v9 11/19 intel_iommu_accel: Stick to system MR for IOMMUFD backed host device when x-flts=on
In reply to Zhenzhong DuanWhen guest enables scalable mode and setup first stage page table, we don't want to use IOMMU MR but rather continue using the system MR for IOMMUFD backed host device.
Then default HWPT in VFIO contains GPA->HPA mappings which could be reused as nesting parent HWPT to construct nested HWPT in vIOMMU.
Move vtd_as_key into intel_iommu_internal.h as it's also used by accel code.
--- hw/i386/intel_iommu_accel.h | 6 ++++++ hw/i386/intel_iommu_internal.h | 11 +++++++++++ hw/i386/intel_iommu.c | 28 +++++++++++++++------------- hw/i386/intel_iommu_accel.c | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 13 deletions(-)
hw/i386/intel_iommu_accel.h +6 -0
@@ -15,6 +15,7 @@#ifdef CONFIG_VTD_ACCEL bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp); +VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -24,5 +25,10 @@ static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,error_append_hint (errp,"CONFIG_VTD_ACCEL is not enabled" );return false; } + +static inline VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as) +{ +return NULL ; +}#endif #endif
hw/i386/intel_iommu_internal.h +11 -0
@@ -685,4 +685,15 @@ typedef struct VTDHostIOMMUDevice {uint8_t devfn; HostIOMMUDevice *hiod; } VTDHostIOMMUDevice; + +/* +* PCI bus number (or SID) is not reliable since the device is usaully +* initialized before guest can configure the PCI bridge +* (SECONDARY_BUS_NUMBER). +*/ +struct vtd_as_key { +PCIBus *bus; +uint8_t devfn; +uint32_t pasid; +};#endif
hw/i386/intel_iommu.c +15 -13
@@ -55,17 +55,6 @@#define VTD_PE_GET_SS_LEVEL (pe) \ (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW)) -/* -* PCI bus number (or SID) is not reliable since the device is usaully -* initialized before guest can configure the PCI bridge -* (SECONDARY_BUS_NUMBER). -*/ -struct vtd_as_key { -PCIBus *bus; -uint8_t devfn; -uint32_t pasid; -}; -/* bus/devfn is PCI device's real BDF not the aliased one */ struct vtd_hiod_key {PCIBus *bus;
@@ -1730,12 +1719,25 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)/* Return whether the device is using IOMMU translation. */ static bool vtd_switch_address_space (VTDAddressSpace *as) { +IntelIOMMUState *s;bool use_iommu, pt;assert (as); - use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled (as); - pt = as->iommu_state->dmar_enabled &&vtd_as_pt_enabled (as); + s = as->iommu_state; + use_iommu = s->dmar_enabled && !vtd_as_pt_enabled (as); + pt = s->dmar_enabled &&vtd_as_pt_enabled (as); + +/* +* When guest enables scalable mode and sets up first stage page table, +* we stick to system MR for IOMMUFD backed host device. Then its +* default hwpt contains GPA->HPA mappings which is used directly if +* PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall back +* to original processing. +*/ +if (s->root_scalable && s->fsts &&vtd_find_hiod_iommufd (as)) { + use_iommu = false; + } trace_vtd_switch_address_space(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn),
hw/i386/intel_iommu_accel.c +18 -0
@@ -50,3 +50,21 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, "host IOMMU is incompatible with guest first stage translation");return false; } + +VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as) +{ +IntelIOMMUState *s = as->iommu_state; +struct vtd_as_key key = { + .bus = as->bus, + .devfn = as->devfn, + }; +VTDHostIOMMUDevice *vtd_hiod =g_hash_table_lookup (s->vtd_host_iommu_dev, + &key); + +if (vtd_hiod && vtd_hiod->hiod && +object_dynamic_cast (OBJECT (vtd_hiod->hiod), +TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) { +return vtd_hiod; + } +return NULL ; +}
Signature · 1 line
2.47.1
PATCH v9 12/19 intel_iommu: Add some macros and inline functions
In reply to Zhenzhong DuanAdd some macros and inline functions that will be used by following patch.
This patch also make a cleanup to change below macros to use extract64() just like what smmu does, because they are either used in following patches or used indirectly by new introduced inline functions.
VTD_INV_DESC_PIOTLB_IH
VTD_SM_PASID_ENTRY_PGTT
VTD_SM_PASID_ENTRY_DID
VTD_SM_PASID_ENTRY_FSPM
VTD_SM_PASID_ENTRY_FSPTPTR
But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.
--- hw/i386/intel_iommu_internal.h | 50 ++++++++++++++++++++++++++++------ hw/i386/intel_iommu.c | 27 +++++++++--------- 2 files changed, 55 insertions(+), 22 deletions(-)
hw/i386/intel_iommu_internal.h +41 -9
@@ -516,7 +516,7 @@ typedef union VTDPRDesc VTDPRDesc;#define VTD_INV_DESC_PIOTLB_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK)#define VTD_INV_DESC_PIOTLB_PASID (val) (((val) >> 32) & 0xfffffULL)#define VTD_INV_DESC_PIOTLB_AM (val) ((val) & 0x3fULL) -#define VTD_INV_DESC_PIOTLB_IH (val) (((val) >> 6) & 0x1) +#define VTD_INV_DESC_PIOTLB_IH (x) extract64((x)->val[1], 6, 1)#define VTD_INV_DESC_PIOTLB_ADDR (val) ((val) & ~0xfffULL)#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL
@@ -636,17 +636,20 @@ typedef struct VTDPASIDCacheInfo {/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL -#define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6) -#define VTD_SM_PASID_ENTRY_FST (1ULL << 6) -#define VTD_SM_PASID_ENTRY_SST (2ULL << 6) -#define VTD_SM_PASID_ENTRY_NESTED (3ULL << 6) -#define VTD_SM_PASID_ENTRY_PT (4ULL << 6) +#define VTD_SM_PASID_ENTRY_PGTT (x) extract64((x)->val[0], 6, 3) +#define VTD_SM_PASID_ENTRY_FST 1 +#define VTD_SM_PASID_ENTRY_SST 2 +#define VTD_SM_PASID_ENTRY_NESTED 3 +#define VTD_SM_PASID_ENTRY_PT 4#define VTD_SM_PASID_ENTRY_AW 7ULL/* Adjusted guest-address-width */ -#define VTD_SM_PASID_ENTRY_DID (val) ((val) & VTD_DOMAIN_ID_MASK) +#define VTD_SM_PASID_ENTRY_DID (x) extract64((x)->val[1], 0, 16) -#define VTD_SM_PASID_ENTRY_FSPM 3ULL -#define VTD_SM_PASID_ENTRY_FSPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_SRE (x) extract64((x)->val[2], 0, 1) +#define VTD_SM_PASID_ENTRY_FSPM (x) extract64((x)->val[2], 2, 2) +#define VTD_SM_PASID_ENTRY_WPE (x) extract64((x)->val[2], 4, 1) +#define VTD_SM_PASID_ENTRY_EAFE (x) extract64((x)->val[2], 7, 1) +#define VTD_SM_PASID_ENTRY_FSPTPFN (x) extract64((x)->val[2], 12, 52)/* First Stage Paging Structure */ /* Masks for First Stage Paging Entry */
@@ -696,4 +699,33 @@ struct vtd_as_key {uint8_t devfn; uint32_t pasid; }; + +static inline dma_addr_t vtd_pe_get_fspt_base (VTDPASIDEntry *pe) +{ +return VTD_SM_PASID_ENTRY_FSPTPFN (pe) <<VTD_PAGE_SHIFT ; +} + +/* +* First stage IOVA address width: 48 bits for 4-level paging(FSPM=00) +* 57 bits for 5-level paging(FSPM=01) +*/ +static inline uint32_t vtd_pe_get_fs_aw (VTDPASIDEntry *pe) +{ +/* +* Paging mode for first-stage translation (VTD spec Figure 9-6) +* 00: 4-level paging, 01: 5-level paging +*/ +return VTD_HOST_AW_48BIT +VTD_SM_PASID_ENTRY_FSPM (pe) *9 ; +} + +static inline bool vtd_pe_pgtt_is_pt (VTDPASIDEntry *pe) +{ +return (VTD_SM_PASID_ENTRY_PGTT (pe) ==VTD_SM_PASID_ENTRY_PT ); +} + +/* check if PGTT is first stage translation */ +static inline bool vtd_pe_pgtt_is_fst (VTDPASIDEntry *pe) +{ +return (VTD_SM_PASID_ENTRY_PGTT (pe) ==VTD_SM_PASID_ENTRY_FST ); +}#endif
hw/i386/intel_iommu.c +14 -13
@@ -48,10 +48,11 @@#define VTD_CE_GET_PRE (ce) \ ((ce)->val[0] & VTD_SM_CONTEXT_ENTRY_PRE) -/* pe operations */ -#define VTD_PE_GET_TYPE (pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT) -#define VTD_PE_GET_FS_LEVEL (pe) \ - (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM)) +/* +* Paging mode for first-stage translation (VTD spec Figure 9-6) +* 00: 4-level paging, 01: 5-level paging +*/ +#define VTD_PE_GET_FS_LEVEL (pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)#define VTD_PE_GET_SS_LEVEL (pe) \ (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
@@ -807,7 +808,7 @@ static inline bool vtd_is_fs_level_supported(IntelIOMMUState *s, uint32_t level)/* Return true if check passed, otherwise false */ static inline bool vtd_pe_type_check (IntelIOMMUState *s,VTDPASIDEntry *pe) { -switch (VTD_PE_GET_TYPE (pe)) { +switch (VTD_SM_PASID_ENTRY_PGTT (pe)) {case VTD_SM_PASID_ENTRY_FST :return !!(s->ecap &VTD_ECAP_FSTS );case VTD_SM_PASID_ENTRY_SST :
@@ -882,7 +883,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,return -VTD_FR_PASID_TABLE_ENTRY_INV ; } - pgtt =VTD_PE_GET_TYPE (pe); + pgtt =VTD_SM_PASID_ENTRY_PGTT (pe);if (pgtt ==VTD_SM_PASID_ENTRY_SST && !vtd_is_ss_level_supported (s,VTD_PE_GET_SS_LEVEL (pe))) {return -VTD_FR_PASID_TABLE_ENTRY_INV ;
@@ -1116,7 +1117,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,if (s->root_scalable) {vtd_ce_get_pasid_entry (s, ce, &pe, pasid);if (s->fsts) { -return pe.val[2 ] &VTD_SM_PASID_ENTRY_FSPTPTR ; +return vtd_pe_get_fspt_base (&pe); }else {return pe.val[0 ] &VTD_SM_PASID_ENTRY_SSPTPTR ; }
@@ -1605,7 +1606,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,if (s->root_scalable) {vtd_ce_get_pasid_entry (s, ce, &pe, pasid); -return VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); +return VTD_SM_PASID_ENTRY_DID (&pe); }return VTD_CONTEXT_ENTRY_DID (ce->hi);
@@ -1687,7 +1688,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,*/ return false; } -return (VTD_PE_GET_TYPE (&pe) ==VTD_SM_PASID_ENTRY_PT ); +return vtd_pe_pgtt_is_pt (&pe); }return (vtd_ce_get_type (ce) ==VTD_CONTEXT_TT_PASS_THROUGH );
@@ -3108,9 +3109,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,/* Fall through */ case VTD_INV_DESC_PASIDC_G_DSI :if (pc_entry->valid) { - did =VTD_SM_PASID_ENTRY_DID (pc_entry->pasid_entry.val[1 ]); + did =VTD_SM_PASID_ENTRY_DID (&pc_entry->pasid_entry); }else { - did =VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); + did =VTD_SM_PASID_ENTRY_DID (&pe); }if (pc_info->did != did) {return ;
@@ -5154,8 +5155,8 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,if (ret) {return -EINVAL ; } - pgtt =VTD_PE_GET_TYPE (&pe); - domain_id =VTD_SM_PASID_ENTRY_DID (pe.val[1 ]); + pgtt =VTD_SM_PASID_ENTRY_PGTT (&pe); + domain_id =VTD_SM_PASID_ENTRY_DID (&pe); ret =0 ;switch (pgtt) {case VTD_SM_PASID_ENTRY_FST :
Signature · 1 line
2.47.1
PATCH v9 13/19 intel_iommu_accel: Bind/unbind guest page table to host
In reply to Zhenzhong DuanThis captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.
When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.
--- hw/i386/intel_iommu_accel.h | 7 +++ include/hw/i386/intel_iommu.h | 2 + hw/i386/intel_iommu.c | 22 ++++++- hw/i386/intel_iommu_accel.c | 114 ++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 3 + 5 files changed, 145 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_accel.h +7 -0
@@ -16,6 +16,7 @@bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp);VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as); +bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as,Error **errp);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -30,5 +31,11 @@ static inline VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) {return NULL ; } + +static inline bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as, +Error **errp) +{ +return true; +}#endif #endif
include/hw/i386/intel_iommu.h +2 -0
@@ -154,6 +154,8 @@ struct VTDAddressSpace {* with the guest IOMMU pgtables for a device. */ IOVATree *iova_tree; + +uint32_t fs_hwpt_id; };struct VTDIOTLBEntry {
hw/i386/intel_iommu.c +19 -3
@@ -87,7 +87,11 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)g_hash_table_iter_init (&as_it, s->vtd_address_spaces);while (g_hash_table_iter_next (&as_it,NULL , (void **)&vtd_as)) {VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; - pc_entry->valid = false; +if (pc_entry->valid) { + pc_entry->valid = false; +/* It's fatal to get failure during reset */ +vtd_propagate_guest_pasid (vtd_as, &error_fatal); + } } }
@@ -3073,6 +3077,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,VTDPASIDEntry pe;IOMMUNotifier *n;uint16_t did; +const char *err_prefix ="Attaching to HWPT failed: " ; +Error *local_err =NULL ;if (vtd_dev_get_pe_from_pasid (vtd_as, &pe)) {if (!pc_entry->valid) {
@@ -3093,7 +3099,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,vtd_address_space_unmap (vtd_as, n); }vtd_switch_address_space (vtd_as); -return ; + + err_prefix ="Detaching from HWPT failed: " ; + goto do_bind_unbind; }/*
@@ -3121,12 +3129,20 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,if (!pc_entry->valid) { pc_entry->pasid_entry = pe; pc_entry->valid = true; - }else if (!vtd_pasid_entry_compare (&pe, &pc_entry->pasid_entry)) { + }else if (vtd_pasid_entry_compare (&pe, &pc_entry->pasid_entry)) { + err_prefix ="Replacing HWPT attachment failed: " ; + }else {return ; }vtd_switch_address_space (vtd_as);vtd_address_space_sync (vtd_as); + +do_bind_unbind: +/* TODO: Fault event injection into guest, report error to QEMU for now */ +if (!vtd_propagate_guest_pasid (vtd_as, &local_err)) { +error_reportf_err (local_err,"%s" , err_prefix); + } }static void vtd_pasid_cache_sync (IntelIOMMUState *s,VTDPASIDCacheInfo *pc_info)
hw/i386/intel_iommu_accel.c +114 -0
123 lines not on this page · raw message
hw/i386/trace-events +3 -0
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)" vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)" vtd_reset_exit(void) "" +vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" +vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" +vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
Signature · 1 line
2.47.1
PATCH v9 14/19 intel_iommu_accel: Propagate PASID-based iotlb invalidation to host
In reply to Zhenzhong DuanFrom: Yi Liu <[email protected]>
This traps the guest PASID-based iotlb invalidation request and propagate it to host.
Intel VT-d 3.0 supports nested translation in PASID granularity. Guest SVA support could be implemented by configuring nested translation on specific pasid. This is also known as dual stage DMA translation.
Under such configuration, guest owns the GVA->GPA translation which is configured as first stage page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first stage translation table, piotlb invalidation should be propagated to host since host IOMMU will cache first level page table related mappings during DMA address translation.
--- hw/i386/intel_iommu_accel.h | 10 +++++ hw/i386/intel_iommu_internal.h | 6 +++ hw/i386/intel_iommu.c | 11 ++++-- hw/i386/intel_iommu_accel.c | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_accel.h +10 -0
@@ -17,6 +17,9 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, Error **errp);VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as);bool vtd_propagate_guest_pasid (VTDAddressSpace *vtd_as,Error **errp); +void vtd_flush_host_piotlb_all_locked (IntelIOMMUState *s,uint16_t domain_id, +uint32_t pasid,hwaddr addr, +uint64_t npages,bool ih);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
@@ -37,5 +40,12 @@ static inline bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, {return true; } + +static inline void vtd_flush_host_piotlb_all_locked (IntelIOMMUState *s, +uint16_t domain_id, +uint32_t pasid,hwaddr addr, +uint64_t npages,bool ih) +{ +}#endif #endif
hw/i386/intel_iommu_internal.h +6 -0
@@ -622,6 +622,12 @@ typedef struct VTDPASIDCacheInfo {uint32_t pasid; } VTDPASIDCacheInfo; +typedef struct VTDPIOTLBInvInfo { +uint16_t domain_id; +uint32_t pasid; +struct iommu_hwpt_vtd_s1_invalidate *inv_data; +}VTDPIOTLBInvInfo ; +/* PASID Table Related Definitions */ #define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL)#define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +8 -3
@@ -2967,6 +2967,8 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,vtd_iommu_lock (s);g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_pasid, &info); +vtd_flush_host_piotlb_all_locked (s, domain_id, pasid,0 , (uint64_t )-1 , + false);vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
@@ -2986,7 +2988,8 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s, }static void vtd_piotlb_page_invalidate (IntelIOMMUState *s,uint16_t domain_id, -uint32_t pasid,hwaddr addr,uint8_t am) +uint32_t pasid,hwaddr addr,uint8_t am, +bool ih) {VTDIOTLBPageInvInfo info;
@@ -2998,6 +3001,7 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,vtd_iommu_lock (s);g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_page_piotlb, &info); +vtd_flush_host_piotlb_all_locked (s, domain_id, pasid, addr,1 << am, ih);vtd_iommu_unlock (s);vtd_iotlb_page_invalidate_notify (s, domain_id, addr, am, pasid);
@@ -3029,7 +3033,8 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,case VTD_INV_DESC_PIOTLB_PSI_IN_PASID : am =VTD_INV_DESC_PIOTLB_AM (inv_desc->val[1 ]); addr = (hwaddr )VTD_INV_DESC_PIOTLB_ADDR (inv_desc->val[1 ]); -vtd_piotlb_page_invalidate (s, domain_id, pasid, addr, am); +vtd_piotlb_page_invalidate (s, domain_id, pasid, addr, am, +VTD_INV_DESC_PIOTLB_IH (inv_desc));break ;default :
@@ -5176,7 +5181,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, ret =0 ;switch (pgtt) {case VTD_SM_PASID_ENTRY_FST : -vtd_piotlb_page_invalidate (s, domain_id, vtd_as->pasid, addr,0 ); +vtd_piotlb_page_invalidate (s, domain_id, vtd_as->pasid, addr,0 , false);break ;/* Room for other pgtt values */ default :
hw/i386/intel_iommu_accel.c +69 -0
72 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v9 15/19 intel_iommu: Replay all pasid bindings when either SRTP or TE bit is changed
In reply to Zhenzhong DuanFrom: Yi Liu <[email protected]>
When either 'Set Root Table Pointer' or 'Translation Enable' bit is changed, all pasid bindings on host side become stale and need to be updated.
Introduce a helper function vtd_replay_pasid_bindings_all() to go through all pasid entries in all passthrough devices to update host side bindings.
--- hw/i386/intel_iommu.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/i386/intel_iommu.c +10 -0
@@ -76,6 +76,7 @@ struct vtd_iotlb_key {static void vtd_address_space_refresh_all (IntelIOMMUState *s);static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n); +static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s);static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s) {
@@ -2629,6 +2630,7 @@ static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,0 ,VTD_GSTS_RTPS );vtd_reset_caches (s);vtd_address_space_refresh_all (s); +vtd_replay_pasid_bindings_all (s); }/* Set Interrupt Remap Table Pointer */
@@ -2663,6 +2665,7 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)vtd_reset_caches (s);vtd_address_space_refresh_all (s); +vtd_replay_pasid_bindings_all (s); }/* Handle Interrupt Remap Enable/Disable */
@@ -3162,6 +3165,13 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)vtd_iommu_unlock (s); } +static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s) +{ +VTDPASIDCacheInfo pc_info = { .type =VTD_INV_DESC_PASIDC_G_GLOBAL }; + +vtd_pasid_cache_sync (s, &pc_info); +} +static bool vtd_process_pasid_desc (IntelIOMMUState *s,VTDInvDesc *inv_desc) {
Signature · 1 line
2.47.1
PATCH v9 16/19 intel_iommu: Replay pasid bindings after context cache invalidation
In reply to Zhenzhong DuanFrom: Yi Liu <[email protected]>
This replays guest pasid bindings after context cache invalidation. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing context cache invalidation.
We see old linux such as 6.7.0-rc2 not following the spec, it sends pasid cache invalidation before context cache invalidation, then QEMU depends on context cache invalidation to get pasid entry and setup binding.
--- hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 2 files changed, 43 insertions(+)
hw/i386/intel_iommu.c +42 -0
@@ -77,6 +77,8 @@ struct vtd_iotlb_key {static void vtd_address_space_refresh_all (IntelIOMMUState *s);static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n);static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s); +static void vtd_pasid_cache_sync_locked (gpointer key,gpointer value, +gpointer user_data);static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s) {
@@ -2322,6 +2324,37 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)* VT-d emulation codes. */ vtd_iommu_replay_all (s); +/* +* Same for pasid cache invalidation, per VT-d spec 6.5.2.1, a global +* context cache invalidation should be followed by global PASID cache +* invalidation. In order to work with guest not following spec, +* handle global PASID cache invalidation here. +*/ +vtd_replay_pasid_bindings_all (s); +} + +static void vtd_pasid_cache_devsi (VTDAddressSpace *vtd_as) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +PCIBus *bus = vtd_as->bus; +uint8_t devfn = vtd_as->devfn; +struct vtd_as_key key = { + .bus = bus, + .devfn = devfn, + .pasid = vtd_as->pasid, + }; +VTDPASIDCacheInfo pc_info; + +if (!s->fsts || !s->root_scalable || !s->dmar_enabled) { +return ; + } + +trace_vtd_pasid_cache_devsi (pci_bus_num (bus), +VTD_PCI_SLOT (devfn),VTD_PCI_FUNC (devfn)); + +/* We fake to be global invalidation just to bypass all checks */ + pc_info.type =VTD_INV_DESC_PASIDC_G_GLOBAL ; +vtd_pasid_cache_sync_locked (&key, vtd_as, &pc_info); }/* Do a context-cache device-selective invalidation.
@@ -2382,6 +2415,15 @@ static void vtd_context_device_invalidate(IntelIOMMUState *s,* happened. */ vtd_address_space_sync (vtd_as); +/* +* Per spec 6.5.2.1, context flush should be followed by PASID +* cache and iotlb flush. In order to work with a guest which does +* not follow spec and missed PASID cache flush, e.g., linux +* 6.7.0-rc2, we have vtd_pasid_cache_devsi() to invalidate PASID +* cache of passthrough device. Host iommu driver would flush +* piotlb when a pasid unbind is passed down to it. +*/ +vtd_pasid_cache_devsi (vtd_as); } } }
hw/i386/trace-events +1 -0
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) "" vtd_inv_desc_pasid_cache_gsi(void) "" vtd_inv_desc_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16 vtd_inv_desc_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32 +vtd_pasid_cache_devsi(uint8_t bus, uint8_t dev, uint8_t fn) "Dev selective PC invalidation dev: %02"PRIx8":%02"PRIx8".%02"PRIx8 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present" vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present" vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
Signature · 1 line
2.47.1
PATCH v9 17/19 intel_iommu: Add migration support with x-flts=on
In reply to Zhenzhong DuanWhen x-flts=on, we set up bindings to nested HWPT in host, after migration, VFIO device binds to nesting parent HWPT by default. We need to re-establish the bindings to nested HWPT, or else device DMA will break.
--- hw/i386/intel_iommu.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
@@ -4090,6 +4090,13 @@ static int vtd_post_load(void *opaque, int version_id)*/ vtd_switch_address_space_all (iommu); +/* +* Bindings to nested HWPT in host is set up dynamically depending +* on pasid entry configuration from guest. After migration, we +* need to re-establish the bindings before restoring device's DMA. +*/ +vtd_replay_pasid_bindings_all (iommu); +return 0 ; }
Signature · 1 line
2.47.1
PATCH v9 18/19 intel_iommu: Enable host device when x-flts=on in scalable mode
In reply to Zhenzhong DuanNow that all infrastructures of supporting passthrough device running with first stage translation are there, enable it now.
--- hw/i386/intel_iommu_accel.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/i386/intel_iommu_accel.c +1 -3
@@ -47,9 +47,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,return false; } -error_setg (errp, -"host IOMMU is incompatible with guest first stage translation" ); -return false; +return true; }VTDHostIOMMUDevice *vtd_find_hiod_iommufd (VTDAddressSpace *as)
Signature · 1 line
2.47.1
PATCH v9 19/19 docs/devel: Add IOMMUFD nesting documentation
In reply to Zhenzhong DuanAdd documentation about using IOMMUFD backed VFIO device with intel_iommu with x-flts=on.
--- docs/devel/vfio-iommufd.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
docs/devel/vfio-iommufd.rst +17 -0
@@ -164,3 +164,20 @@ RAM discarding for mdev. ``vfio-ap`` and ``vfio-ccw`` devices don't have same issue as their backend devices are always mdev and RAM discarding is force enabled. + +Usage with intel_iommu featuring x-flts=on +------------------------------------------ + +Only IOMMUFD backed VFIO device is supported when intel_iommu is configured +with x-flts=on, for legacy container backed VFIO device, below error shows: + +.. code-block:: none + + qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0: vfio 0000:02:00.0: Failed to set vIOMMU: Need IOMMUFD backend when x-flts=on + +VFIO device under PCI bridge is unsupported, use PCIE bridge if necessary, +otherwise below error shows: + +.. code-block:: none + + qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device downstream to a PCI bridge is unsupported when x-flts=on
Signature · 1 line
2.47.1
Hello Zhenzhong
Quoted 85 lines · Zhenzhong Duan
Hi,
Based on Cédric's suggestions[1], The nesting series v8 is split to "base nesting series" + "
ERRATA_772415_SPR17quirk series", this is the base nesting series.For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of guest page table but pass first stage page table to host side to construct a nested HWPT. There was some effort to enable this feature in old days, see
[2] for details.The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform the first stage address translation. Along with it, modifications to present mappings in the guest I/O page table should be followed with an IOTLB invalidation.
.-------------. .---------------------------. | vIOMMU | | Guest I/O page table | | | '---------------------------' .----------------/ | PASID Entry |--- PASID cache flush --+ '-------------' | | | V | | I/O page table pointer in GPA '-------------' Guest ------| Shadow |---------------------------|-------- v v v Host .-------------. .-----------------------------. | pIOMMU | | First stage for GIOVA->GPA | | | '-----------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------------. | | | Second stage for GPA->HPA, unmanaged domain| | | '--------------------------------------------' '-------------'<Intel VT-d Nested translation>
This series reuse VFIO device's default HWPT as nesting parent instead of creating new one. This way avoids duplicate code of a new memory listener, all existing feature from VFIO listener can be shared, e.g., ram discard, dirty tracking, etc. Two limitations are: 1) not supporting VFIO device under a PCI bridge with emulated device, because emulated device wants IOMMU AS and VFIO device stick to system AS; 2) not supporting kexec or reboot from "intel_iommu=on,sm_on" to "intel_iommu=on,sm_off" on platform with
ERRATA_772415_SPR17, because VFIO device's default HWPT is created withNEST_PARENTflag, kernel inhibit RO mappings when switch to shadow mode.This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.
There are some interactions between VFIO and vIOMMU
* vIOMMU registers PCIIOMMUOps [set|unset]_iommu_device to PCI subsystem. VFIO calls them to register/unregister HostIOMMUDevice instance to vIOMMU at vfio device realize stage.
* vIOMMU registers PCIIOMMUOpsget_viommu_flagsto PCI subsystem. VFIO calls it to get vIOMMU exposed flags.
* vIOMMU calls HostIOMMUDeviceIOMMUFD interface [at|de]tach_hwpt to bind/unbind device to IOMMUFD backed domains, either nested domain or not.See below diagram:
VFIO Device Intel IOMMU .-----------------. .-------------------. | | | | | .---------|PCIIOMMUOps |.-------------. | | | IOMMUFD |(set/unset_iommu_device) || Host IOMMU | | | | Device |------------------------>|| Device list | | | .---------|(get_viommu_flags) |.-------------. | | | | | | | | | V | | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | | | link |<------------------------| | Device | | | .---------| (detach_hwpt)| .-------------. | | | | | | | | | ... | .-----------------. .-------------------.
Quoted 5 lines
Below is an example to enable first stage translation for passthrough device:
-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...
What about libvirt support ? There are patches to enable IOMMUFD support with device assignment but I don't see anything related to first stage translation. Is there a plan ?
This raises a question. Should ftls support be automatically enabled based on the availability of an IOMMUFD backend ?
Quoted 6 lines
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthrough
Did you do any experiments with active mlx5 VFs ?
Thanks,
C.
Quoted 138 lines
PATCH01-08: Some preparing work PATCH09-10: Compatibility check between vIOMMU and Host IOMMU PATCH11-16: Implement first stage translation for passthrough device PATCH17: Add migration support and optimization PATCH18: Enable first stage translation for passthrough device PATCH19: Add docQemu code can be found at [3], it's based on vfio-next.
Fault event injection to guest isn't supported in this series, we presume guest kernel always construct correct first stage page table for passthrough device. For emulated devices, the emulation code already provided first stage fault injection.
TODO:
- Fault event injection to guest when HW first stage page table faults[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v9Thanks
ZhenzhongChangelog:
v9:
- split v8 to base nesting series +ERRATA_772415_SPR17series (Cédric)
- s/fs_hwpt/fs_hwpt_id, s/vtd_bind_guest_pasid/vtd_propagate_guest_pasid (Eric)
- polish error msg whenCONFIG_VTD_ACCELisn't defined (Eric)
- refactorhwpt_idassignment invtd_device_attach_iommufd()(Eric)v8:
- add hw/i386/intel_iommu_accel.[hc] to hold accel code (Eric)
- return bool for all vtd accel related functions (Cédric, Eric)
- introduce a newPCIIOMMUOps::get_host_iommu_quirks()(Eric, Nicolin)
- minor polishment to comment and code (Cédric, Eric)
- drop some R-b as they have changes needing review againv7:
- s/host_iommu_extract_vendor_caps/host_iommu_extract_quirks (Nicolin)
- s/RID_PASID/PASID_0 (Eric)
- drop rid2pasid check invtd_do_iommu_translate(Eric)
- refine DID check invtd_pasid_cache_sync_locked(Liuyi)
- refine commit log (Nicolin, Eric, Liuyi)
- Fix doc build (Cédric)
- add migration supportv6:
- delete RPS capability related supporting code (Eric, Yi)
- use terminology 'first/second stage' to replace 'first/second level" (Eric, Yi)
- useget_viommu_flags()instead ofget_viommu_caps()(Nicolin)
- drop non-RID_PASID related code and simplify pasid invalidation handling (Eric, Yi)
- drop the patch that handle pasid replay when context invalidation (Eric)
- move vendor specific cap check from VFIO core tobackend/iommufd.c(Nicolin)v5:
- refine commit log of patch2 (Cédric, Nicolin)
- introduce helpervfio_pci_from_vfio_device()(Cédric)
- introduce helpervfio_device_viommu_get_nested()(Cédric)
- pass 'boolbypass_ro' argument tovfio_listener_valid_section()instead of 'VFIOContainerBase *' (Cédric)
- fix a potential build error reported by Jim Shuv4:
- s/VIOMMU_CAP_STAGE1/VIOMMU_CAP_HW_NESTED (Eric, Nicolin, Donald, Shameer)
- clarifyget_viommu_cap()return pure emulated caps and explain reason in commit log (Eric)
- retrieve the ce only ifvtd_as->pasidinvtd_as_to_iommu_pasid_locked(Eric)
- refine doc comment and commit log in patch10-11 (Eric)v3:
- define enum type for VIOMMU_CAP_* (Eric)
- drop inline flag in the patch which uses the helper (Eric)
- use extract64 in new introduced MACRO (Eric)
- polish comments and fix typo error (Eric)
- split workaround patch forERRATA_772415_SPR17to two patches (Eric)
- optimize bind/unbind error path processingv2:
- introduceget_viommu_cap()to get STAGE1 flag to create nesting parent HWPT (Liuyi)
- reuse VFIO's default HWPT as parent HWPT of nested translation (Nicolin, Liuyi)
- abandon support of VFIO device under pcie-to-pci bridge to simplify design (Liuyi)
- bypass RO mapping in VFIO's default HWPT ifERRATA_772415_SPR17(Liuyi)
- dropvtd_dev_to_context_entryoptimization (Liuyi)v1:
- simplify vendor specific checking invtd_check_hiod(Cédric, Nicolin)
- rebase to masterYi Liu (3):
intel_iommu_accel: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changed
intel_iommu: Replay pasid bindings after context cache invalidationZhenzhong Duan (16):
intel_iommu: Renamevtd_ce_get_rid2pasid_entryto
vtd_ce_get_pasid_entry
intel_iommu: Delete RPS capability related supporting code
intel_iommu: Update terminology to match VTD spec
hw/pci: Exportpci_device_get_iommu_bus_devfn()and return bool hw/pci: Introducepci_device_get_viommu_flags()
intel_iommu: Implementget_viommu_flags()callback
intel_iommu: Introduce a new structure VTDHostIOMMUDevice vfio/iommufd: Force creating nesting parent HWPT
intel_iommu_accel: Check for compatibility with IOMMUFD backed device
when x-flts=on
intel_iommu_accel: Fail passthrough device under PCI bridge if
x-flts=on
intel_iommu_accel: Stick to system MR for IOMMUFD backed host device
when x-flts=on
intel_iommu: Add some macros and inline functions
intel_iommu_accel: Bind/unbind guest page table to host
intel_iommu: Add migration support with x-flts=on
intel_iommu: Enable host device when x-flts=on in scalable mode docs/devel: Add IOMMUFD nesting documentationMAINTAINERS | 2 + docs/devel/vfio-iommufd.rst | 17 ++ hw/i386/intel_iommu_accel.h | 51 ++++ hw/i386/intel_iommu_internal.h | 155 +++++++--- include/hw/i386/intel_iommu.h | 6 +- include/hw/iommu.h | 25 ++ include/hw/pci/pci.h | 24 ++ include/hw/vfio/vfio-device.h | 2 + hw/i386/intel_iommu.c | 528 +++++++++++++++++++-------------- hw/i386/intel_iommu_accel.c | 251 ++++++++++++++++ hw/pci/pci.c | 23 +- hw/vfio/device.c | 12 + hw/vfio/iommufd.c | 9 + tests/qtest/intel-iommu-test.c | 4 +- hw/i386/Kconfig | 5 + hw/i386/meson.build | 1 + hw/i386/trace-events | 4 + 17 files changed, 833 insertions(+), 286 deletions(-)create mode 100644
hw/i386/intel_iommu_accel.h
create mode 100644include/hw/iommu.h
create mode 100644hw/i386/intel_iommu_accel.c
Hi Cédric,
Quoted 114 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v9 00/19]intel_iommu: Enable first stage translation for passthrough deviceHello Zhenzhong
Quoted 7 lines · Zhenzhong Duan
Hi,
Based on Cédric's suggestions[1], The nesting series v8 is split to "base nesting series" + "
ERRATA_772415_SPR17quirk series", this is the base nesting series.For passthrough device with intel_iommu.x-flts=on, we don't do shadowing
of
Quoted 6 lines
guest page table but pass first stage page table to host side to construct a nested HWPT. There was some effort to enable this feature in old days, see
[2] for details.The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below
diagram shows,
Quoted 1 line
guest I/O page table pointer in GPA (guest physical address) is passed to
host
Quoted 2 lines
and be used to perform the first stage address translation. Along with it, modifications to present mappings in the guest I/O page table should be
followed
Quoted 14 lines
with an IOTLB invalidation.
.-------------. .---------------------------. | vIOMMU | | Guest I/O page table | | | '---------------------------' .----------------/ | PASID Entry |--- PASID cache flush --+ '-------------' | | | V | | I/O page table pointer in GPA '-------------' Guest ------| Shadow |---------------------------|-------- v v vQuoted 8 lines
Host .-------------. .-----------------------------. | pIOMMU | | First stage for GIOVA->GPA | | | '-----------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------------. | | | Second stage for GPA->HPA, unmanageddomain|
Quoted 6 lines
| | '--------------------------------------------' '-------------'<Intel VT-d Nested translation>
This series reuse VFIO device's default HWPT as nesting parent instead of creating new one. This way avoids duplicate code of a new memory
listener,
Quoted 5 lines
all existing feature from VFIO listener can be shared, e.g., ram discard, dirty tracking, etc. Two limitations are: 1) not supporting VFIO device under a PCI bridge with emulated device, because emulated device wants IOMMU AS and VFIO device stick to system AS; 2) not supporting kexec or reboot from "intel_iommu=on,sm_on" to "intel_iommu=on,sm_off" on
platform
Quoted 1 line
with
ERRATA_772415_SPR17, because VFIO device's default HWPT iscreated
Quoted 16 lines
with
NEST_PARENTflag, kernel inhibit RO mappings when switch to shadow mode.This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.
There are some interactions between VFIO and vIOMMU
* vIOMMU registers PCIIOMMUOps [set|unset]_iommu_device to PCI subsystem. VFIO calls them to register/unregister HostIOMMUDevice instance to vIOMMU at vfio device realize stage.
* vIOMMU registers PCIIOMMUOpsget_viommu_flagsto PCI subsystem. VFIO calls it to get vIOMMU exposed flags.
* vIOMMU calls HostIOMMUDeviceIOMMUFD interface [at|de]tach_hwpt to bind/unbind device to IOMMUFD backed domains, either nested domain or not.Quoted 3 lines
See below diagram:
VFIO Device Intel
IOMMU
Quoted 2 lines
.-----------------. .-------------------.
| | ||
Quoted 1 line
| .---------|PCIIOMMUOps |.-------------.|
Quoted 1 line
| | IOMMUFD |(set/unset_iommu_device) || Host IOMMU| | > | | Device |------------------------>|| Device list | | > | .---------|(get_viommu_flags) |.-------------. | > | | | | | > | | | V | > | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | > | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | > | | link |<------------------------| | Device | | > | .---------| (detach_hwpt)| .-------------. | > | | | | | > | | | ... | > .-----------------. .-------------------.Quoted 2 lines
Below is an example to enable first stage translation for passthrough
device:
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -devicevfio-pci,iommufd=iommufd0,...
What about libvirt support ? There are patches to enable IOMMUFD support with device assignment but I don't see anything related to first stage translation. Is there a plan ?
I think IOMMUFD support in libvirt is non-trivial, good to know there is progress. But I didn't find a match in libvirt mailing list, https://lists.libvirt.org/archives/search?q=iommufd
Do you have a link?
I think first stage support is trivial, only to support a new property <...x-flts=on/off>. I can apply a few time resource from my manager to work on it after this series is merged. It's also welcome if anyone is interested to take it.
Quoted 3 lines
This raises a question. Should ftls support be automatically enabled based on the availability of an IOMMUFD backend ?
Yes, if user doesn't force it off, like <...iommufd='off'> and IOMMUFD backend available, we can enable it automatically.
Quoted 9 lines
Quoted 7 lines
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthroughDid you do any experiments with active mlx5 VFs ?
No, there are only a few device drivers supporting VFIO migration and we only have QAT. Let me know if you see issue on other devices.
Thanks
Zhenzhong
Hello Zhenzhong,
Quoted 121 lines · Zhenzhong
Hi Cédric,
Quoted 119 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v9 00/19]intel_iommu: Enable first stage translation for passthrough deviceHello Zhenzhong
Quoted 7 lines · Zhenzhong Duan
Hi,
Based on Cédric's suggestions[1], The nesting series v8 is split to "base nesting series" + "
ERRATA_772415_SPR17quirk series", this is the base nesting series.For passthrough device with intel_iommu.x-flts=on, we don't do shadowing
of
Quoted 6 lines
guest page table but pass first stage page table to host side to construct a nested HWPT. There was some effort to enable this feature in old days, see
[2] for details.The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below
diagram shows,
Quoted 1 line
guest I/O page table pointer in GPA (guest physical address) is passed to
host
Quoted 2 lines
and be used to perform the first stage address translation. Along with it, modifications to present mappings in the guest I/O page table should be
followed
Quoted 14 lines
with an IOTLB invalidation.
.-------------. .---------------------------. | vIOMMU | | Guest I/O page table | | | '---------------------------' .----------------/ | PASID Entry |--- PASID cache flush --+ '-------------' | | | V | | I/O page table pointer in GPA '-------------' Guest ------| Shadow |---------------------------|-------- v v vQuoted 8 lines
Host .-------------. .-----------------------------. | pIOMMU | | First stage for GIOVA->GPA | | | '-----------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------------. | | | Second stage for GPA->HPA, unmanageddomain|
Quoted 6 lines
| | '--------------------------------------------' '-------------'<Intel VT-d Nested translation>
This series reuse VFIO device's default HWPT as nesting parent instead of creating new one. This way avoids duplicate code of a new memory
listener,
Quoted 5 lines
all existing feature from VFIO listener can be shared, e.g., ram discard, dirty tracking, etc. Two limitations are: 1) not supporting VFIO device under a PCI bridge with emulated device, because emulated device wants IOMMU AS and VFIO device stick to system AS; 2) not supporting kexec or reboot from "intel_iommu=on,sm_on" to "intel_iommu=on,sm_off" on
platform
Quoted 1 line
with
ERRATA_772415_SPR17, because VFIO device's default HWPT iscreated
Quoted 16 lines
with
NEST_PARENTflag, kernel inhibit RO mappings when switch to shadow mode.This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.
There are some interactions between VFIO and vIOMMU
* vIOMMU registers PCIIOMMUOps [set|unset]_iommu_device to PCI subsystem. VFIO calls them to register/unregister HostIOMMUDevice instance to vIOMMU at vfio device realize stage.
* vIOMMU registers PCIIOMMUOpsget_viommu_flagsto PCI subsystem. VFIO calls it to get vIOMMU exposed flags.
* vIOMMU calls HostIOMMUDeviceIOMMUFD interface [at|de]tach_hwpt to bind/unbind device to IOMMUFD backed domains, either nested domain or not.Quoted 3 lines
See below diagram:
VFIO Device Intel
IOMMU
Quoted 2 lines
.-----------------. .-------------------.
| | ||
Quoted 1 line
| .---------|PCIIOMMUOps |.-------------.|
Quoted 1 line
| | IOMMUFD |(set/unset_iommu_device) || Host IOMMU| | > | | Device |------------------------>|| Device list | | > | .---------|(get_viommu_flags) |.-------------. | > | | | | | > | | | V | > | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | > | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | > | | link |<------------------------| | Device | | > | .---------| (detach_hwpt)| .-------------. | > | | | | | > | | | ... | > .-----------------. .-------------------.Quoted 2 lines
Below is an example to enable first stage translation for passthrough
device:
Quoted 4 lines
-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -devicevfio-pci,iommufd=iommufd0,...
Quoted 7 lines
Quoted 3 lines
What about libvirt support ? There are patches to enable IOMMUFD support with device assignment but I don't see anything related to first stage translation. Is there a plan ?
I think IOMMUFD support in libvirt is non-trivial, good to know there is progress. But I didn't find a match in libvirt mailing list, https://lists.libvirt.org/archives/search?q=iommufd
Do you have a link?
Here :
https://lists.libvirt.org/archives/list/[email protected]/thread/KFYUQGMXWV64QPI245H66GKRNAYL7LGB/
There might be an update. We should ask Nathan.
Quoted 3 lines
I think first stage support is trivial, only to support a new property <...x-flts=on/off>. I can apply a few time resource from my manager to work on it after this series is merged. It's also welcome if anyone is interested to take it.
ok. So, currently, we have no way to benefit from translation acceleration on the host unless we directly set the 'x-flts' property on the QEMU command line.
Quoted 4 lines
Quoted 2 lines
This raises a question. Should ftls support be automatically enabled based on the availability of an IOMMUFD backend ?
Yes, if user doesn't force it off, like <...iommufd='off'> and IOMMUFD backend available, we can enable it automatically.
The plan is to keep VFIO IOMMU Type1 as the default host IOMMU backend to maintain a consistent behavior. If an IOMMUFD backend is required, it should be set explicitly. One day we might revisit this choice and change the default. Not yet.
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthroughDid you do any experiments with active mlx5 VFs ?
No, there are only a few device drivers supporting VFIO migration and we only have QAT. Let me know if you see issue on other devices.
Since we lack libvirt integration (of flts), the tests need to be run manually which is more complex for QE. IOW, it will take more time but we should definitely evaluate other devices.
Thanks,
C.
Quoted 6 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v9 00/19]intel_iommu: Enable first stage translation for passthrough device
...
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Below is an example to enable first stage translation for passthrough
device:
Quoted 4 lines
-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -deviceQuoted 7 lines
Quoted 5 lines
vfio-pci,iommufd=iommufd0,...
What about libvirt support ? There are patches to enable IOMMUFD support with device assignment but I don't see anything related to first stage translation. Is there a plan ?
I think IOMMUFD support in libvirt is non-trivial, good to know there is
progress.
Quoted 1 line
But I didn't find a match in libvirt mailing list,
Quoted 8 lines
https://lists.libvirt.org/archives/search?q=iommufd
Quoted 1 line
Do you have a link?
Here :
https://lists.libvirt.org/archives/list/[email protected]/thread/KFYUQGMX
WV64QPI245H66GKRNAYL7LGB/
Thanks
Quoted 4 lines
There might be an update. We should ask Nathan.
Quoted 1 line
I think first stage support is trivial, only to support a new property
Quoted 8 lines
<...x-flts=on/off>.
Quoted 1 line
I can apply a few time resource from my manager to work on it after this
series is merged.
Quoted 1 line
It's also welcome if anyone is interested to take it.
ok. So, currently, we have no way to benefit from translation acceleration on the host unless we directly set the 'x-flts' property on the QEMU command line.
Yes, thanks for reminding.
I'll try add 'x-flts' support to libvirt to fill the gap recently, I will take one week vacation starting this Friday, may try it after vacation.
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
This raises a question. Should ftls support be automatically enabled based on the availability of an IOMMUFD backend ?
Yes, if user doesn't force it off, like <...iommufd='off'> and IOMMUFD
backend available, we can enable it automatically.
The plan is to keep VFIO IOMMU Type1 as the default host IOMMU backend to maintain a consistent behavior. If an IOMMUFD backend is required, it should be set explicitly. One day we might revisit this choice and change the default. Not yet.
OK, maybe we need to maintain consistent behavior for intel_iommu too,
if first-stage is required, it should be set explicitly, if not set, default to second stage(shadow page).
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthrough
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Did you do any experiments with active mlx5 VFs ?
No, there are only a few device drivers supporting VFIO migration and we
only have QAT.
Quoted 1 line
Let me know if you see issue on other devices.
Since we lack libvirt integration (of flts), the tests need to be run manually which is more complex for QE. IOW, it will take more time but we should definitely evaluate other devices.
Oh, if you mean nesting feature test, we did play with different devices we had, ixgbevf, ICE vf, DSA and QAT. For VFIO migration with nesting, we only tested QAT.
Thanks
Zhenzhong