intel_iommu: Enable stage-1 translation for passthrough device
v4 cover intel_iommu: Enable stage-1 translation for passthrough device
Hi,
For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [1] 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 stage-1 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 | | Stage1 for GIOVA->GPA |
| | '------------------------'
.----------------/ |
| PASID Entry | V (Nested xlate)
'----------------\.--------------------------------------.
| | | Stage2 for GPA->HPA, unmanaged domain|
| | '--------------------------------------'
'-------------'For history reason, there are different namings in different VTD spec rev, Where:
- Stage1 = First stage = First level = flts
- Stage2 = Second stage = Second level = slts
<Intel VT-d Nested translation>
This series reuse VFIO device's default hwpt as nested 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", 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_cap to PCI subsystem. VFIO calls it to get vIOMMU exposed capabilities.
* 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_cap) |.-------------. |
| | | | |
| | | V |
| .---------| HostIOMMUDeviceIOMMUFD | .-------------. |
| | IOMMUFD | (attach_hwpt)| | Host IOMMU | |
| | link |<------------------------| | Device | |
| .---------| (detach_hwpt)| .-------------. |
| | | | |
| | | ... |
.-----------------. .-------------------.Below is an example to enable stage-1 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
PATCH1-6: Some preparing work PATCH7-8: Compatibility check between vIOMMU and Host IOMMU PATCH9-17: Implement stage-1 page table for passthrough device
PATCH18-19:Workaround for ERRATA_772415_SPR17
PATCH20: Enable stage-1 translation for passthrough device
Qemu code can be found at [2]
Fault report isn't supported in this series, we presume guest kernel always construct correct stage1 page table for passthrough device. For emulated devices, the emulation code already provided stage1 fault injection.
TODO:
- Fault report to guest when HW stage1 faults
[1] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v4
Thanks
Zhenzhong
Changelog:
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 nested 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 (Cedric, Nicolin)
- rebase to master
rfcv3:
- s/hwpt_id/id in iommufd_backend_invalidate_cache()'s parameter (Shameer)
- hide vtd vendor specific caps in a wrapper union (Eric, Nicolin)
- simplify return value check of get_cap() (Eric)
- drop realize_late (Cedric, Eric)
- split patch13:intel_iommu: Add PASID cache management infrastructure (Eric)
- s/vtd_pasid_cache_reset/vtd_pasid_cache_reset_locked (Eric)
- s/vtd_pe_get_domain_id/vtd_pe_get_did (Eric)
- refine comments (Eric, Donald)
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace
Yi Liu (3):
intel_iommu: Replay pasid bindings after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changed
Zhenzhong Duan (17):
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
hw/pci: Introduce pci_device_get_viommu_cap()
intel_iommu: Implement get_viommu_cap() callback vfio/iommufd: Force creating nested parent domain
hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
intel_iommu: Handle PASID entry removal and update
intel_iommu: Handle PASID entry addition
intel_iommu: Introduce a new pasid cache invalidation type FORCE_RESET
intel_iommu: Stick to system MR for IOMMUFD backed host device when
x-fls=on
intel_iommu: Bind/unbind guest page table to host
vfio: Add a new element bypass_ro in VFIOContainerBase Workaround for ERRATA_772415_SPR17
intel_iommu: Enable host device when x-flts=on in scalable mode
MAINTAINERS | 1 + hw/i386/intel_iommu_internal.h | 68 +- include/hw/i386/intel_iommu.h | 9 +- include/hw/iommu.h | 17 + include/hw/pci/pci.h | 27 + include/hw/vfio/vfio-container-base.h | 1 + hw/i386/intel_iommu.c | 941 +++++++++++++++++++++++++- hw/pci/pci.c | 23 +- hw/vfio/iommufd.c | 22 +- hw/vfio/listener.c | 13 +- hw/i386/trace-events | 8 + 11 files changed, 1088 insertions(+), 42 deletions(-) create mode 100644 include/hw/iommu.h
base-commit: 92c05be4dfb59a71033d4c57dac944b29f7dabf0
Signature · 1 line
2.47.1
PATCH v4 1/20 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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +7 -7
@@ -944,7 +944,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,return 0 ; } -static int vtd_ce_get_rid2pasid_entry (IntelIOMMUState *s, +static int vtd_ce_get_pasid_entry (IntelIOMMUState *s,VTDContextEntry *ce,VTDPASIDEntry *pe,uint32_t pasid)
@@ -1025,7 +1025,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 {
@@ -1048,7 +1048,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 ; }
@@ -1116,7 +1116,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 {
@@ -1522,7 +1522,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) */
@@ -1611,7 +1611,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 ]); }
@@ -1687,7 +1687,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
Signature · 1 line
2.47.1
PATCH v4 2/20 hw/pci: Introduce pci_device_get_viommu_cap()
In reply to Zhenzhong DuanIntroduce a new PCIIOMMUOps optional callback, get_viommu_cap() which allows to retrieve capabilities exposed by a vIOMMU. The first planned capability is VIOMMU_CAP_HW_NESTED that advertises the support of HW nested stage translation scheme. pci_device_get_viommu_cap is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_cap() is designed to return 64bit bitmap of purely emulated capabilities which are only derermined by user's configuration, no host capabilities involved. Reasons are:
1. there can be more than one host IOMMUs with different capabilities
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
3. This is migration friendly, return value is consistent between source and target.
4. It's too late for VFIO to call get_viommu_cap() after set_iommu_device() because we need get_viommu_cap() to determine if creating nested parent hwpt or not at attaching stage, meanwhile hiod realize needs iommufd, devid and hwpt_id which are ready after attach_device().
See below sequence:
attach_device()
get_viommu_cap()
create hwpt
...
create hiod
set_iommu_device(hiod)--- MAINTAINERS | 1 + include/hw/iommu.h | 17 +++++++++++++++++ include/hw/pci/pci.h | 25 +++++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 54 insertions(+) create mode 100644 include/hw/iommu.h
MAINTAINERS +1 -0
@@ -2304,6 +2304,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 +17 -0
@@ -0,0 +1,17 @@ +/* +* General vIOMMU capabilities, flags, etc +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +enum { +/* hardware nested stage-1 page table support */ +VIOMMU_CAP_HW_NESTED =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +25 -0
@@ -462,6 +462,21 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_cap: get vIOMMU capabilities +* +* Optional callback, if not implemented, then vIOMMU doesn't +* support exposing capabilities to other subsystem, e.g., VFIO. +* vIOMMU can choose which capabilities to expose. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: 64bit bitmap with each bit represents a capability emulated by +* VIOMMU_CAP_* in include/hw/iommu.h, these capabilities are theoretical +* which are only determined by user's configuration and independent on the +* actual host capabilities they may depend on. +*/ +uint64_t (*get_viommu_cap)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. *
@@ -642,6 +657,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_cap: get vIOMMU capabilities. +* +* Returns a 64bit bitmap with each bit represents a vIOMMU exposed +* capability, 0 if vIOMMU doesn't support esposing capabilities. +* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_cap (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB.
hw/pci/pci.c +11 -0
@@ -2992,6 +2992,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_cap (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_cap) { +return iommu_bus->iommu_ops->get_viommu_cap (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 v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
Hello Zhenzhong
Quoted 2 lines · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_cap()which allows to retrieve capabilities exposed by a vIOMMU. The first planned
vIOMMU device
"device" is implied, but I find it easier to understand if it is stated openly.
Quoted 6 lines
capability is
VIOMMU_CAP_HW_NESTEDthat advertises the support of HW nested stage translation scheme.pci_device_get_viommu_capis a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_cap()is designed to return 64bit bitmap of purely emulated capabilities which are only derermined by user's configuration, no host
typo: determined
Quoted 3 lines
capabilities involved. Reasons are:
1. there can be more than one host IOMMUs with different capabilities
"host IOMMU devices"
Such as iommufd and VFIO IOMMU Type1 ?
Quoted 7 lines
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
3. This is migration friendly, return value is consistent between source and target.
4. It's too late for VFIO to callget_viommu_cap()afterset_iommu_device()because we needget_viommu_cap()to determine if creating nested parent hwpt or not at attaching stage, meanwhile hiod realize needs iommufd,
hiod -> "host IOMMU device"
Quoted 1 line
devid and
hwpt_idwhich are ready afterattach_device().
I find the above sentence difficult to understand.
Quoted 64 lines
See below sequence:
attach_device()
get_viommu_cap()
create hwpt
...
create hiod
set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- MAINTAINERS | 1 + include/hw/iommu.h | 17 +++++++++++++++++ include/hw/pci/pci.h | 25 +++++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 54 insertions(+)create mode 100644
include/hw/iommu.hMAINTAINERS +1
-0@@ -2304,6 +2304,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 +17
-0@@ -0,0 +1,17 @@ +/* +* General vIOMMU capabilities, flags, etc +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +enum { +/* hardware nested stage-1 page table support */ +VIOMMU_CAP_HW_NESTED =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */ include/hw/pci/pci.h +2
-0@@ -462,6 +462,21 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_cap: get vIOMMU capabilities
include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 476
Quoted 10 lines
include/hw/pci/pci.h +10
-0+* +* Optional callback, if not implemented, then vIOMMU doesn't +* support exposing capabilities to other subsystem, e.g., VFIO. +* vIOMMU can choose which capabilities to expose. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: 64bit bitmap with each bit represents a capability emulated by +* VIOMMU_CAP_* in include/hw/iommu.h, these capabilities are theoretical +* which are only determined by user's configuration and independent on the
What is meant by "user's configuration" ? the vIOMMU device properties ?
Quoted 7 lines
include/hw/pci/pci.h +3
-0+* actual host capabilities they may depend on. +*/ +uint64_t (*get_viommu_cap)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. *
include/hw/pci/pci.h bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, · line 664
Quoted 8 lines
include/hw/pci/pci.h +5
-0@@ -642,6 +657,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_cap: get vIOMMU capabilities. +* +* Returns a 64bit bitmap with each bit represents a vIOMMU exposed +* capability, 0 if vIOMMU doesn't support esposing capabilities.
exposing
Thanks,
C.
Quoted 30 lines
include/hw/pci/pci.h +5
-0+* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_cap (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB. hw/pci/pci.c +11
-0@@ -2992,6 +2992,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_cap (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_cap) { +return iommu_bus->iommu_ops->get_viommu_cap (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 v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
Hi Cédric,
Quoted 7 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 02/20] hw/pci: Introducepci_device_get_viommu_cap()Hello Zhenzhong
Quoted 7 lines
Quoted 2 lines · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_cap()which allows to retrieve capabilities exposed by a vIOMMU. The first plannedvIOMMU device
"device" is implied, but I find it easier to understand if it is stated openly.
Will do.
Quoted 9 lines
Quoted 7 lines
capability is
VIOMMU_CAP_HW_NESTEDthat advertises the support of HW nested stage translation scheme.pci_device_get_viommu_capis a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_cap()is designed to return 64bit bitmap of purely emulated capabilities which are only derermined by user's configuration, no hosttypo: determined
Will fix.
Quoted 6 lines
Quoted 4 lines
capabilities involved. Reasons are:
1. there can be more than one host IOMMUs with different capabilities
"host IOMMU devices"
Will do.
Quoted 2 lines
Such as iommufd and VFIO IOMMU Type1 ?
Not VFIO iommu backend, I mean host iommu hardware units.
Quoted 7 lines
Quoted 7 lines
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
3. This is migration friendly, return value is consistent between source and target.
4. It's too late for VFIO to callget_viommu_cap()afterset_iommu_device()because we needget_viommu_cap()to determine if creating nested
Quoted 5 lines
parent
Quoted 1 line
hwpt or not at attaching stage, meanwhile hiod realize needs
iommufd,
hiod -> "host IOMMU device"
Will do.
Quoted 4 lines
Quoted 2 lines
devid and
hwpt_idwhich are ready afterattach_device().I find the above sentence difficult to understand.
This is trying to explain the reason of order between attach_device(), get_viommu_cap() and hiod realizing. What about:
4. host IOMMU capabilities are passed to vIOMMU through set_iommu_device() interface which have to be after attach_device(), when get_viommu_cap() is called in attach_device(), there is no way for vIOMMU to get host IOMMU capabilities yet, so only emulated capabilities can be returned. See below sequence:
attach_device()
get_viommu_cap()
create hwpt
...
vfio_device_hiod_create_and_realize()
set_iommu_device(hiod)Quoted 75 lines
Quoted 64 lines
See below sequence:
attach_device()
get_viommu_cap()
create hwpt
...
create hiod
set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- MAINTAINERS | 1 + include/hw/iommu.h | 17 +++++++++++++++++ include/hw/pci/pci.h | 25 +++++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 54 insertions(+)create mode 100644
include/hw/iommu.hMAINTAINERS +1
-0@@ -2304,6 +2304,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 +17
-0@@ -0,0 +1,17 @@ +/* +* General vIOMMU capabilities, flags, etc +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H + +enum { +/* hardware nested stage-1 page table support */ +VIOMMU_CAP_HW_NESTED =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */ include/hw/pci/pci.h +0
-0@@ -462,6 +462,21 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn);include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 474
Quoted 10 lines
include/hw/pci/pci.h +10
-0+/** +* @get_viommu_cap: get vIOMMU capabilities +* +* Optional callback, if not implemented, then vIOMMU doesn't +* support exposing capabilities to other subsystem, e.g., VFIO. +* vIOMMU can choose which capabilities to expose. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: 64bit bitmap with each bit represents a capability emulated by
Quoted 6 lines
include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 475
Quoted 1 line
include/hw/pci/pci.h +1
-0+* VIOMMU_CAP_* in include/hw/iommu.h, these capabilities are theoretical
include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 476
Quoted 1 line
include/hw/pci/pci.h +1
-0+* which are only determined by user's configuration and independent on the
What is meant by "user's configuration" ? the vIOMMU device properties ?
Yes, I mean user's qemu cmdline configuration.
Quoted 13 lines
include/hw/pci/pci.h typedef struct PCIIOMMUOps {
Quoted 6 lines
include/hw/pci/pci.h +3
-0+* actual host capabilities they may depend on. +*/ +uint64_t (*get_viommu_cap)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB.
include/hw/pci/pci.h bool pci_device_set_iommu_device(PCIDevice
Quoted 2 lines
include/hw/pci/pci.h +0
-0* @@ -642,6 +657,16 @@ bool pci_device_set_iommu_device(PCIDevice*dev, HostIOMMUDevice *hiod,
Quoted 3 lines
include/hw/pci/pci.h +0
-0Error **errp);void pci_device_unset_iommu_device (PCIDevice *dev);
Quoted 7 lines
include/hw/pci/pci.h bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, · line 664
Quoted 5 lines
include/hw/pci/pci.h +5
-0+/** +* pci_device_get_viommu_cap: get vIOMMU capabilities. +* +* Returns a 64bit bitmap with each bit represents a vIOMMU exposed +* capability, 0 if vIOMMU doesn't support esposing capabilities. exposing
Will fix.
Thanks
Zhenzhong
Re: [PATCH v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
Quoted 2 lines · Zhenzhong
Quoted 2 lines
Quoted 2 lines
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
That's correct. But would you please elaborate how different user configurations would benefit from this new op? I don't see a good reasoning behind that.
Quoted 24 lines
Quoted 7 lines
Quoted 2 lines
4. It's too late for VFIO to call
get_viommu_cap()afterset_iommu_device()because we needget_viommu_cap()to determine if creating nestedparent
Quoted 1 line
hwpt or not at attaching stage, meanwhile hiod realize needs
iommufd,
hiod -> "host IOMMU device"
Will do.
Quoted 4 lines
Quoted 2 lines
devid and
hwpt_idwhich are ready afterattach_device().I find the above sentence difficult to understand.
This is trying to explain the reason of order between
attach_device(),get_viommu_cap()and hiod realizing. What about:4. host IOMMU capabilities are passed to vIOMMU through
set_iommu_device()interface which have to be afterattach_device(), whenget_viommu_cap()is called inattach_device(), there is no way for vIOMMU to get host IOMMU capabilities yet, so only emulated capabilities can be returned. See below sequence:
Quoted 6 lines
attach_device()
get_viommu_cap()
create hwpt
...
vfio_device_hiod_create_and_realize()
set_iommu_device(hiod)I think it should be:
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_cap() 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)
?
Thanks
Nicolin
RE: [PATCH v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
Quoted 7 lines
-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v4 02/20] hw/pci: Introducepci_device_get_viommu_cap()
Quoted 7 lines
Quoted 2 lines · Zhenzhong
Quoted 2 lines
Quoted 2 lines
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
That's correct. But would you please elaborate how different user configurations would benefit from this new op? I don't see a good reasoning behind that.
It is the reason to have get_viommu_cap(), get_viommu_cap()
returns capabilities of vIOMMU which underlying VFIO device attaches to, but indeed, it's not reason for leaving out host capabilities, I'll drop this line.
Quoted 39 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
4. It's too late for VFIO to call
get_viommu_cap()after
set_iommu_device()Quoted 1 line
Quoted 1 line
Quoted 1 line
because we need
get_viommu_cap()to determine if creatingnested
Quoted 6 lines
Quoted 5 lines
parent
Quoted 1 line
hwpt or not at attaching stage, meanwhile hiod realize needs
iommufd,
hiod -> "host IOMMU device"
Quoted 8 lines
Will do.
Quoted 4 lines
Quoted 2 lines
devid and
hwpt_idwhich are ready afterattach_device().I find the above sentence difficult to understand.
This is trying to explain the reason of order between
attach_device(),
get_viommu_cap()and hiod realizing.Quoted 3 lines
What about:
4. host IOMMU capabilities are passed to vIOMMU through
set_iommu_device()Quoted 1 line
interface which have to be after
attach_device(), when
get_viommu_cap()Quoted 10 lines
is called in
attach_device(), there is no way for vIOMMU to get host IOMMU capabilities yet, so only emulated capabilities can be returned. See below sequence:attach_device() get_viommu_cap() create hwpt ... vfio_device_hiod_create_and_realize() set_iommu_device(hiod)I think it should be:
Quoted 8 lines
vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_cap()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)
Good catch, done, https://github.com/yiliu1765/qemu/commit/34b02bdc0d615c60d1b07bfee842aabf1f7e2b28
Thanks
Zhenzhong
Re: [PATCH v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
In reply to Zhenzhong DuanHi Zhenzhong,
Quoted 58 lines · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_cap()which allows to retrieve capabilities exposed by a vIOMMU. The first planned capability isVIOMMU_CAP_HW_NESTEDthat advertises the support of HW nested stage translation scheme.pci_device_get_viommu_capis a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_cap()is designed to return 64bit bitmap of purely emulated capabilities which are only derermined by user's configuration, no host capabilities involved. Reasons are:1. there can be more than one host IOMMUs with different capabilities
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
3. This is migration friendly, return value is consistent between source and target.
4. It's too late for VFIO to callget_viommu_cap()afterset_iommu_device()because we needget_viommu_cap()to determine if creating nested parent hwpt or not at attaching stage, meanwhile hiod realize needs iommufd, devid andhwpt_idwhich are ready afterattach_device().
See below sequence:attach_device() get_viommu_cap() create hwpt ... create hiod set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- MAINTAINERS | 1 + include/hw/iommu.h | 17 +++++++++++++++++ include/hw/pci/pci.h | 25 +++++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 54 insertions(+) create mode 100644 include/hw/iommu.hMAINTAINERS +1
-0@@ -2304,6 +2304,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 +1
-0@@ -0,0 +1,17 @@ +/*
include/hw/iommu.h line 11
Quoted 10 lines
include/hw/iommu.h new file +10
-0+* General vIOMMU capabilities, flags, etc +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H +
Could we include header "qemu/bitops.h" here to use BIT_ULL() in this file? Or it will get compile error when including it in the other IOMMU.
Quoted 72 lines
include/hw/iommu.h new file +6
-0+enum { +/* hardware nested stage-1 page table support */ +VIOMMU_CAP_HW_NESTED =BIT_ULL (0 ), +}; + +#endif /* HW_IOMMU_H */ include/hw/pci/pci.h +25
-0@@ -462,6 +462,21 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number of the PCI device. */ void (*unset_iommu_device)(PCIBus *bus,void *opaque,int devfn); +/** +* @get_viommu_cap: get vIOMMU capabilities +* +* Optional callback, if not implemented, then vIOMMU doesn't +* support exposing capabilities to other subsystem, e.g., VFIO. +* vIOMMU can choose which capabilities to expose. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* Returns: 64bit bitmap with each bit represents a capability emulated by +* VIOMMU_CAP_* in include/hw/iommu.h, these capabilities are theoretical +* which are only determined by user's configuration and independent on the +* actual host capabilities they may depend on. +*/ +uint64_t (*get_viommu_cap)(void *opaque);/** * @get_iotlb_info: get properties required to initialize a device IOTLB. * @@ -642,6 +657,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_cap: get vIOMMU capabilities. +* +* Returns a 64bit bitmap with each bit represents a vIOMMU exposed +* capability, 0 if vIOMMU doesn't support esposing capabilities. +* +* @dev: PCI device pointer. +*/ +uint64_t pci_device_get_viommu_cap (PCIDevice *dev); +/** * pci_iommu_get_iotlb_info: get properties required to initialize a * device IOTLB. hw/pci/pci.c +11
-1@@ -2992,6 +2992,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev) } } +uint64_t pci_device_get_viommu_cap (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_cap) { +return iommu_bus->iommu_ops->get_viommu_cap (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) --
Quoted 3 lines
2.47.1
Thanks,
Jim
RE: [PATCH v4 02/20] hw/pci: Introduce pci_device_get_viommu_cap()
Quoted 75 lines
-----Original Message-----
From: Jim Shu <[email protected]>
Subject: Re: [PATCH v4 02/20] hw/pci: Introducepci_device_get_viommu_cap()Hi Zhenzhong,
Quoted 11 lines · Zhenzhong Duan
Introduce a new PCIIOMMUOps optional callback,
get_viommu_cap()which allows to retrieve capabilities exposed by a vIOMMU. The first planned capability isVIOMMU_CAP_HW_NESTEDthat advertises the support of HW nested stage translation scheme.pci_device_get_viommu_capis a wrapper that can be called on a PCI device potentially protected by a vIOMMU.
get_viommu_cap()is designed to return 64bit bitmap of purely emulated capabilities which are only derermined by user's configuration, no host capabilities involved. Reasons are:Quoted 7 lines
1. there can be more than one host IOMMUs with different capabilities
2. there can also be more than one vIOMMUs with different user configuration, e.g., arm smmuv3.
3. This is migration friendly, return value is consistent between source and target.
4. It's too late for VFIO to callget_viommu_cap()afterset_iommu_device()because we needget_viommu_cap()to determine if creating nestedparent
Quoted 45 lines
hwpt or not at attaching stage, meanwhile hiod realize needs iommufd, devid and
hwpt_idwhich are ready afterattach_device(). See below sequence:attach_device() get_viommu_cap() create hwpt ... create hiod set_iommu_device(hiod)Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- MAINTAINERS | 1 + include/hw/iommu.h | 17 +++++++++++++++++ include/hw/pci/pci.h | 25 +++++++++++++++++++++++++ hw/pci/pci.c | 11 +++++++++++ 4 files changed, 54 insertions(+) create mode 100644 include/hw/iommu.hMAINTAINERS +1
-0@@ -2304,6 +2304,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 +6
-0@@ -0,0 +1,17 @@ +/* +* General vIOMMU capabilities, flags, etc +* +* Copyright (C) 2025 Intel Corporation. +* +* SPDX-License-Identifier: GPL-2.0-or-later
Quoted 8 lines
include/hw/iommu.h line 11
Quoted 5 lines
include/hw/iommu.h new file +5
-0+*/ + +#ifndef HW_IOMMU_H +#define HW_IOMMU_H +Could we include header "
qemu/bitops.h" here to useBIT_ULL()in this file? Or it will get compile error when including it in the other IOMMU.
Oh, thanks for point out, fixed at https://github.com/yiliu1765/qemu/commits/zhenzhong/iommufd_nesting.v5.wip/
BRs,
Zhenzhong
PATCH v4 3/20 intel_iommu: Implement get_viommu_cap() callback
In reply to Zhenzhong DuanImplement get_viommu_cap() callback and expose stage-1 capability for now.
VFIO uses it to create nested parent domain which is further used to create nested domain 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"
@@ -4420,6 +4421,16 @@ static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)vtd_iommu_unlock (s); } +static uint64_t vtd_get_viommu_cap (void *opaque) +{ +IntelIOMMUState *s = opaque; +uint64_t caps; + + caps = s->flts ?VIOMMU_CAP_HW_NESTED :0 ; + +return caps; +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {
@@ -4850,6 +4861,7 @@ static PCIIOMMUOps vtd_iommu_ops = { .register_iotlb_notifier = vtd_register_iotlb_notifier, .unregister_iotlb_notifier = vtd_unregister_iotlb_notifier, .ats_request_translation = vtd_ats_request_translation, + .get_viommu_cap = vtd_get_viommu_cap, };static bool vtd_decide_config (IntelIOMMUState *s,Error **errp)
Signature · 1 line
2.47.1
PATCH v4 4/20 vfio/iommufd: Force creating nested parent domain
In reply to Zhenzhong DuanCall pci_device_get_viommu_cap() to get if vIOMMU supports VIOMMU_CAP_HW_NESTED, if yes, create nested parent domain which could be reused by vIOMMU to create nested domain.
It is safe because hw_caps & VIOMMU_CAP_HW_NESTED cannot be set yet because s->flts is forbidden until we support passthrough device with x-flts=on.
--- hw/vfio/iommufd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
hw/vfio/iommufd.c +14 -0
@@ -20,6 +20,7 @@#include "trace.h" #include "qapi/error.h" #include "system/iommufd.h" +#include "hw/iommu.h" #include "hw/qdev-core.h" #include "hw/vfio/vfio-cpr.h" #include "system/reset.h"
@@ -379,6 +380,19 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU supports stage-1 translation, force to create nested parent +* domain which could be reused by vIOMMU to create nested domain. +*/ +if (vbasedev->type ==VFIO_DEVICE_TYPE_PCI ) { +VFIOPCIDevice *vdev =container_of (vbasedev, VFIOPCIDevice, vbasedev); + + hw_caps =pci_device_get_viommu_cap (&vdev->pdev); +if (hw_caps &VIOMMU_CAP_HW_NESTED ) { + 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
Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domain
Quoted 34 lines · Zhenzhong Duan
Call
pci_device_get_viommu_cap()to get if vIOMMU supportsVIOMMU_CAP_HW_NESTED, if yes, create nested parent domain which could be reused by vIOMMU to create nested domain.It is safe because
hw_caps&VIOMMU_CAP_HW_NESTEDcannot be set yet becauses->fltsis forbidden until we support passthrough device with x-flts=on.Suggested-by Nicolin Chen <[email protected]>Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Eric Auger <[email protected]>--- hw/vfio/iommufd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)hw/vfio/iommufd.c +4
-0@@ -20,6 +20,7 @@#include "trace.h" #include "qapi/error.h" #include "system/iommufd.h" +#include "hw/iommu.h" #include "hw/qdev-core.h" #include "hw/vfio/vfio-cpr.h" #include "system/reset.h" @@ -379,6 +380,19 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU supports stage-1 translation, force to create nested parent +* domain which could be reused by vIOMMU to create nested domain.
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, · line 395
Quoted 10 lines
hw/vfio/iommufd.c +9
-0+*/ +if (vbasedev->type ==VFIO_DEVICE_TYPE_PCI ) { +VFIOPCIDevice *vdev =container_of (vbasedev, VFIOPCIDevice, vbasedev); + + hw_caps =pci_device_get_viommu_cap (&vdev->pdev); +if (hw_caps &VIOMMU_CAP_HW_NESTED ) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } + }
Could you please add a wrapper for the above ? Something like :
static bool vfio_device_viommu_get_nested(VFIODevice *vbasedev)
{
if (vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
return !!(pci_device_get_viommu_cap(&vdev->pdev) & VIOMMU_CAP_HW_NESTED);
}
return false;
}May be this routine belongs to hw/vfio/device.c.
Thanks,
C.
Quoted 3 lines
hw/vfio/iommufd.c +0
-0if (cpr_is_incoming ()) { hwpt_id = vbasedev->cpr.hwpt_id; goto skip_alloc;
RE: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domain
Quoted 68 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domainQuoted 1 line · Zhenzhong Duan
Call
pci_device_get_viommu_cap()to get if vIOMMU supports
VIOMMU_CAP_HW_NESTED,Quoted 1 line
if yes, create nested parent domain which could be reused by vIOMMU to
create
Quoted 3 lines
nested domain.
It is safe because
hw_caps&VIOMMU_CAP_HW_NESTEDcannot be set yetbecause
hw/vfio/iommufd.c static bool
Quoted 23 lines
s->fltsis forbidden until we support passthrough device with x-flts=on.Suggested-by Nicolin Chen <[email protected]>Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Eric Auger <[email protected]>--- hw/vfio/iommufd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)hw/vfio/iommufd.c +1
-0@@ -20,6 +20,7 @@#include "trace.h" #include "qapi/error.h" #include "system/iommufd.h" +#include "hw/iommu.h" #include "hw/qdev-core.h" #include "hw/vfio/vfio-cpr.h" #include "system/reset.h" @@ -379,6 +380,19 @@ static booliommufd_cdev_autodomains_get(VFIODevice *vbasedev,
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 5 lines
hw/vfio/iommufd.c +2
-0flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU supports stage-1 translation, force to create nested parent
hw/vfio/iommufd.c static bool · line 385
Quoted 1 line
hw/vfio/iommufd.c +1
-0+* domain which could be reused by vIOMMU to create nested domain.
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 3 lines
hw/vfio/iommufd.c +3
-0+*/ +if (vbasedev->type ==VFIO_DEVICE_TYPE_PCI ) { + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice,vbasedev);
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 7 lines
hw/vfio/iommufd.c +6
-0+ + hw_caps =pci_device_get_viommu_cap (&vdev->pdev); +if (hw_caps &VIOMMU_CAP_HW_NESTED ) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } + }Could you please add a wrapper for the above ? Something like :
static bool
vfio_device_viommu_get_nested(VFIODevice *vbasedev){
if (vbasedev->type==VFIO_DEVICE_TYPE_PCI) {
VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice,
vbasedev);
Quoted 7 lines
return !!(pci_device_get_viommu_cap(&vdev->pdev) & VIOMMU_CAP_HW_NESTED); } return false; }May be this routine belongs to
hw/vfio/device.c.
Done, see https://github.com/yiliu1765/qemu/commit/7ce1af90d5c1f418f23c9e397a16a3914b30f09f
I also introduced another wrapper vfio_device_to_vfio_pci(), let me know if you think it's unnecessary, I'll fold it.
Thanks
Zhenzhong
Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domain
Quoted 74 lines · Zhenzhong
Quoted 74 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domainQuoted 1 line · Zhenzhong Duan
Call
pci_device_get_viommu_cap()to get if vIOMMU supports
VIOMMU_CAP_HW_NESTED,Quoted 1 line
if yes, create nested parent domain which could be reused by vIOMMU to
create
Quoted 3 lines
nested domain.
It is safe because
hw_caps&VIOMMU_CAP_HW_NESTEDcannot be set yetbecause
hw/vfio/iommufd.c static bool
Quoted 23 lines
s->fltsis forbidden until we support passthrough device with x-flts=on.Suggested-by Nicolin Chen <[email protected]>Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Eric Auger <[email protected]>--- hw/vfio/iommufd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)hw/vfio/iommufd.c +1
-0@@ -20,6 +20,7 @@#include "trace.h" #include "qapi/error.h" #include "system/iommufd.h" +#include "hw/iommu.h" #include "hw/qdev-core.h" #include "hw/vfio/vfio-cpr.h" #include "system/reset.h" @@ -379,6 +380,19 @@ static booliommufd_cdev_autodomains_get(VFIODevice *vbasedev,
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 5 lines
hw/vfio/iommufd.c +2
-0flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +/* +* If vIOMMU supports stage-1 translation, force to create nested parent
hw/vfio/iommufd.c static bool · line 385
Quoted 1 line
hw/vfio/iommufd.c +1
-0+* domain which could be reused by vIOMMU to create nested domain.
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 3 lines
hw/vfio/iommufd.c +3
-0+*/ +if (vbasedev->type ==VFIO_DEVICE_TYPE_PCI ) { + VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice,vbasedev);
hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 7 lines
hw/vfio/iommufd.c +6
-0+ + hw_caps =pci_device_get_viommu_cap (&vdev->pdev); +if (hw_caps &VIOMMU_CAP_HW_NESTED ) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } + }Could you please add a wrapper for the above ? Something like :
static bool
vfio_device_viommu_get_nested(VFIODevice *vbasedev){
if (vbasedev->type==VFIO_DEVICE_TYPE_PCI) {
VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice,
vbasedev);return !!(pci_device_get_viommu_cap(&vdev->pdev) & VIOMMU_CAP_HW_NESTED); } return false; }
Quoted 5 lines
Quoted 1 line
May be this routine belongs to
hw/vfio/device.c.Done, see https://github.com/yiliu1765/qemu/commit/7ce1af90d5c1f418f23c9e397a16a3914b30f09f
I also introduced another wrapper
vfio_device_to_vfio_pci(), let me know if you think it's unnecessary, I'll fold it.
It's good to have it if you use it everywhere:
hw/vfio/device.c: if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
hw/vfio/container.c: vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
hw/vfio/container.c: vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
hw/vfio/listener.c: if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
hw/vfio/listener.c: if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
hw/vfio/iommufd.c: if (vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
hw/vfio/iommufd.c: vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {So, I would address this cleanup separately.
FYI, We also have this helper :
VFIODevice *vfio_get_vfio_device(Object *obj)
{
if (object_dynamic_cast(obj, TYPE_VFIO_PCI)) {
return &VFIO_PCI_BASE(obj)->vbasedev;
} else {
return NULL;
}
}Thanks,
C.
RE: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domain
Quoted 103 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domainQuoted 7 lines · Zhenzhong
Quoted 7 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 04/20] vfio/iommufd: Force creating nested parent domainQuoted 8 lines
Quoted 8 lines · Zhenzhong Duan
Quoted 1 line
Call
pci_device_get_viommu_cap()to get if vIOMMU supports
VIOMMU_CAP_HW_NESTED,Quoted 1 line
if yes, create nested parent domain which could be reused by vIOMMU to
create
Quoted 3 lines
nested domain.
It is safe because
hw_caps&VIOMMU_CAP_HW_NESTEDcannot be setyet
Quoted 28 lines
Quoted 28 lines
because
hw/vfio/iommufd.c static bool
Quoted 23 lines
s->fltsis forbidden until we support passthrough device with x-flts=on.Suggested-by Nicolin Chen <[email protected]>Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Eric Auger <[email protected]>--- hw/vfio/iommufd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)hw/vfio/iommufd.c +1
-0@@ -20,6 +20,7 @@#include "trace.h" #include "qapi/error.h" #include "system/iommufd.h" +#include "hw/iommu.h" #include "hw/qdev-core.h" #include "hw/vfio/vfio-cpr.h" #include "system/reset.h" @@ -379,6 +380,19 @@ static booliommufd_cdev_autodomains_get(VFIODevice *vbasedev,
Quoted 3 lines
hw/vfio/iommufd.c +0
-0flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; }Quoted 8 lines
Quoted 8 lines
hw/vfio/iommufd.c static bool · line 384
Quoted 2 lines
hw/vfio/iommufd.c +2
-0+/* +* If vIOMMU supports stage-1 translation, force to create nested parent
hw/vfio/iommufd.c static bool · line 385
Quoted 1 line
hw/vfio/iommufd.c +1
-0+* domain which could be reused by vIOMMU to create nested domain.
Quoted 3 lines
hw/vfio/iommufd.c +3
-0+*/ +if (vbasedev->type ==VFIO_DEVICE_TYPE_PCI ) { + VFIOPCIDevice *vdev = container_of(vbasedev,VFIOPCIDevice,
Quoted 9 lines
Quoted 9 lines
vbasedev);
Quoted 7 lines
hw/vfio/iommufd.c +6
-0+ + hw_caps =pci_device_get_viommu_cap (&vdev->pdev); +if (hw_caps &VIOMMU_CAP_HW_NESTED ) { + flags |=IOMMU_HWPT_ALLOC_NEST_PARENT ; + } + }Quoted 6 lines
Quoted 6 lines
Could you please add a wrapper for the above ? Something like :
static bool
vfio_device_viommu_get_nested(VFIODevice *vbasedev){
if (vbasedev->type==VFIO_DEVICE_TYPE_PCI) {
VFIOPCIDevice *vdev = container_of(vbasedev,VFIOPCIDevice,
Quoted 11 lines
Quoted 9 lines
vbasedev);
return !!(pci_device_get_viommu_cap(&vdev->pdev) & VIOMMU_CAP_HW_NESTED); } return false; }May be this routine belongs to
hw/vfio/device.c.Done, see
https://github.com/yiliu1765/qemu/commit/7ce1af90d5c1f418f23c9e397a16
a3914b30f09fQuoted 2 lines
I also introduced another wrapper
vfio_device_to_vfio_pci(), let me know ifyou think it's unnecessary, I'll fold it.
It's good to have it if you use it everywhere:
hw/vfio/device.c: if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) { hw/vfio/container.c: vbasedev_iter->type !=
VFIO_DEVICE_TYPE_PCI) {hw/vfio/container.c:vbasedev_iter->type!=VFIO_DEVICE_TYPE_PCI) {hw/vfio/listener.c: if (vbasedev &&vbasedev->type==VFIO_DEVICE_TYPE_PCI) {
Quoted 6 lines
hw/vfio/listener.c: if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) { hw/vfio/iommufd.c: if (vbasedev->type == VFIO_DEVICE_TYPE_PCI) { hw/vfio/iommufd.c: vbasedev_tmp->type !=
VFIO_DEVICE_TYPE_PCI) {So, I would address this cleanup separately.
OK, will send a separate one.
Quoted 11 lines
FYI, We also have this helper :
VFIODevice *vfio_get_vfio_device(Object *obj)
{ if (object_dynamic_cast(obj, TYPE_VFIO_PCI)) { return &VFIO_PCI_BASE(obj)->vbasedev; } else { return NULL; } }
I'll put new helper right under it.
Thanks
Zhenzhong
PATCH v4 5/20 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
@@ -652,6 +652,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
@@ -2857,20 +2857,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);
@@ -2907,6 +2908,7 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev, devfn = parent->devfn; bus = parent_bus; } + aliased = true; } iommu_bus = parent_bus;
@@ -2928,6 +2930,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 v4 6/20 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
@@ -608,4 +609,10 @@ typedef struct VTDRootEntry VTDRootEntry;/* 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
@@ -295,7 +295,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
@@ -281,7 +281,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,
@@ -4368,6 +4371,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,
@@ -4384,7 +4388,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; }
@@ -4394,7 +4405,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 v4 7/20 intel_iommu: 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, stage-1 page table is passed to host to construct nested page table. We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest stage-1 page table could be used by host.
For instance, vIOMMU supports stage-1 1GB huge 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.
--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -0
@@ -192,6 +192,7 @@#define VTD_ECAP_PT (1ULL << 6)#define VTD_ECAP_SC (1ULL << 7)#define VTD_ECAP_MHMV (15ULL << 20) +#define VTD_ECAP_NEST (1ULL << 26)#define VTD_ECAP_SRS (1ULL << 31)#define VTD_ECAP_PSS (7ULL << 35)/* limit: MemTxAttrs::pid */ #define VTD_ECAP_PASID (1ULL << 40)
hw/i386/intel_iommu.c +29 -1
@@ -40,6 +40,7 @@#include "kvm/kvm_i386.h" #include "migration/vmstate.h" #include "trace.h" +#include "system/iommufd.h" /* context entry operations */ #define VTD_CE_GET_RID2PASID (ce) \
@@ -4363,7 +4364,34 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } -error_setg (errp,"host device is uncompatible with stage-1 translation" ); +#ifdef CONFIG_IOMMUFD +struct HostIOMMUDeviceCaps *caps = &hiod->caps; +struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd; + +/* Remaining checks are all stage-1 translation specific */ +if (!object_dynamic_cast (OBJECT (hiod),TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) { +error_setg (errp,"Need IOMMUFD backend when x-flts=on" ); +return false; + } + +if (caps->type !=IOMMU_HW_INFO_TYPE_INTEL_VTD ) { +error_setg (errp,"Incompatible host platform IOMMU type %d" , + caps->type); +return false; + } + +if (!(vtd->ecap_reg &VTD_ECAP_NEST )) { +error_setg (errp,"Host IOMMU doesn't support nested translation" ); +return false; + } + +if (s->fs1gp && !(vtd->cap_reg &VTD_CAP_FS1GP )) { +error_setg (errp,"Stage-1 1GB huge page is unsupported by host IOMMU" ); +return false; + } +#endif + +error_setg (errp,"host IOMMU is incompatible with stage-1 translation" );return false; }
Signature · 1 line
2.47.1
PATCH v4 8/20 intel_iommu: 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.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +11 -2
@@ -4338,9 +4338,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;
@@ -4367,6 +4368,8 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,#ifdef CONFIG_IOMMUFD struct HostIOMMUDeviceCaps *caps = &hiod->caps;struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd; +PCIBus *bus = vtd_hiod->bus; +PCIDevice *pdev =pci_find_device (bus,pci_bus_num (bus), vtd_hiod->devfn);/* Remaining checks are all stage-1 translation specific */ if (!object_dynamic_cast (OBJECT (hiod),TYPE_HOST_IOMMU_DEVICE_IOMMUFD )) {
@@ -4389,6 +4392,12 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,error_setg (errp,"Stage-1 1GB huge page is unsupported by host IOMMU" ); return false; } + +if (pci_device_get_iommu_bus_devfn (pdev, &bus,NULL ,NULL )) { +error_setg (errp,"Host device under PCI bridge is unsupported " +"when x-flts=on" ); +return false; + }#endif error_setg (errp,"host IOMMU is incompatible with stage-1 translation" );
@@ -4422,7 +4431,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;
Signature · 1 line
2.47.1
PATCH v4 9/20 intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
In reply to Zhenzhong DuanPCI device supports two request types, Requests-without-PASID and Requests-with-PASID. Requests-without-PASID doesn't include a PASID TLP prefix, IOMMU fetches rid_pasid from context entry and use it as IOMMU's pasid to index pasid table.
So we need to translate between PCI's pasid and IOMMU's pasid specially for Requests-without-PASID, e.g., PCI_NO_PASID(-1) <-> rid_pasid. For Requests-with-PASID, PCI's pasid and IOMMU's pasid are same value.
vtd_as_from_iommu_pasid_locked() translates from BDF+iommu_pasid to vtd_as which contains PCI's pasid vtd_as->pasid.
vtd_as_to_iommu_pasid_locked() translates from BDF+vtd_as->pasid to iommu_pasid.
--- hw/i386/intel_iommu.c | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
hw/i386/intel_iommu.c +58 -0
@@ -1602,6 +1602,64 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,return 0 ; } +static int vtd_as_to_iommu_pasid_locked (VTDAddressSpace *vtd_as, +uint32_t *pasid) +{ +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextEntry ce; +int ret; + +/* For Requests-with-PASID, its pasid value is used by vIOMMU directly */ +if (vtd_as->pasid !=PCI_NO_PASID ) { + *pasid = vtd_as->pasid; +return 0 ; + } + +if (cc_entry->context_cache_gen == s->context_cache_gen) { + ce = cc_entry->context_entry; + }else { + ret =vtd_dev_to_context_entry (s, bus_num, devfn, &ce); +if (ret) { +return ret; + } + } + + *pasid =VTD_CE_GET_RID2PASID (&ce); +return 0 ; +} + +static gboolean vtd_find_as_by_sid_and_iommu_pasid (gpointer key,gpointer value, +gpointer user_data) +{ +VTDAddressSpace *vtd_as = (VTDAddressSpace *)value; +struct vtd_as_raw_key *target = (struct vtd_as_raw_key *)user_data; +uint16_t sid =PCI_BUILD_BDF (pci_bus_num (vtd_as->bus), vtd_as->devfn); +uint32_t pasid; + +if (vtd_as_to_iommu_pasid_locked (vtd_as, &pasid)) { +return false; + } + +return (pasid == target->pasid) && (sid == target->sid); +} + +/* Translate iommu pasid to vtd_as */ +static inline +VTDAddressSpace *vtd_as_from_iommu_pasid_locked (IntelIOMMUState *s, +uint16_t sid,uint32_t pasid) +{ +struct vtd_as_raw_key key = { + .sid = sid, + .pasid = pasid + }; + +return g_hash_table_find (s->vtd_address_spaces, + vtd_find_as_by_sid_and_iommu_pasid, &key); +} +static int vtd_sync_shadow_page_hook (const IOMMUTLBEvent *event,void *private) {
Signature · 1 line
2.47.1
PATCH v4 10/20 intel_iommu: Handle PASID entry removal and update
In reply to Zhenzhong DuanThis adds an new entry VTDPASIDCacheEntry in VTDAddressSpace to cache the pasid entry and track PASID usage and future PASID tagged DMA address translation support in vIOMMU.
VTDAddressSpace of PCI_NO_PASID is allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy.
When guest removes or updates a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, removes VTDAddressSpace or update cached PASID entry.
vIOMMU emulator could figure out the reason by fetching latest guest pasid entry and compare it with cached PASID entry.
--- hw/i386/intel_iommu_internal.h | 27 ++++- include/hw/i386/intel_iommu.h | 6 + hw/i386/intel_iommu.c | 196 +++++++++++++++++++++++++++++++-- hw/i386/trace-events | 3 + 4 files changed, 220 insertions(+), 12 deletions(-)
hw/i386/intel_iommu_internal.h +26 -1
@@ -316,6 +316,7 @@ typedef enum VTDFaultReason {* request while disabled */ VTD_FR_IR_SID_ERR =0x26 ,/* Invalid Source-ID */ +VTD_FR_RTADDR_INV_TTM =0x31 ,/* Invalid TTM in RTADDR */ /* PASID directory entry access failure */ VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,/* The Present(P) field of pasid directory entry is 0 */
@@ -493,6 +494,15 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL +/* PASID-cache Invalidate Descriptor (pc_inv_dsc) fields */ +#define VTD_INV_DESC_PASIDC_G (x) extract64((x)->val[0], 4, 2) +#define VTD_INV_DESC_PASIDC_G_DSI 0 +#define VTD_INV_DESC_PASIDC_G_PASID_SI 1 +#define VTD_INV_DESC_PASIDC_G_GLOBAL 3 +#define VTD_INV_DESC_PASIDC_DID (x) extract64((x)->val[0], 16, 16) +#define VTD_INV_DESC_PASIDC_PASID (x) extract64((x)->val[0], 32, 20) +#define VTD_INV_DESC_PASIDC_RSVD_VAL0 0xfff000000000f1c0ULL +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;
@@ -553,6 +563,21 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL +typedef enum VTDPCInvType { +/* VTD spec defined PASID cache invalidation type */ +VTD_PASID_CACHE_DOMSI =VTD_INV_DESC_PASIDC_G_DSI , +VTD_PASID_CACHE_PASIDSI =VTD_INV_DESC_PASIDC_G_PASID_SI , +VTD_PASID_CACHE_GLOBAL_INV =VTD_INV_DESC_PASIDC_G_GLOBAL , +}VTDPCInvType ; + +typedef struct VTDPASIDCacheInfo { +VTDPCInvType type; +uint16_t did; +uint32_t pasid; +PCIBus *bus; +uint16_t devfn; +}VTDPASIDCacheInfo ; +/* PASID Table Related Definitions */ #define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL)#define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
@@ -574,7 +599,7 @@ typedef struct VTDRootEntry VTDRootEntry;#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_DID (x) extract64((x)->val[1], 0, 16)#define VTD_SM_PASID_ENTRY_FLPM 3ULL#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL)
include/hw/i386/intel_iommu.h +6 -0
@@ -95,6 +95,11 @@ struct VTDPASIDEntry {uint64_t val[8 ]; }; +typedef struct VTDPASIDCacheEntry { +struct VTDPASIDEntry pasid_entry; +bool valid; +}VTDPASIDCacheEntry ; +struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;
@@ -107,6 +112,7 @@ struct VTDAddressSpace {MemoryRegion iommu_ir_fault;/* Interrupt region for catching fault */ IntelIOMMUState *iommu_state;VTDContextCacheEntry context_cache_entry; +VTDPASIDCacheEntry pasid_cache_entry;QLIST_ENTRY (VTDAddressSpace) next;/* Superset of notifier flags that this address space has */ IOMMUNotifierFlag notifier_flags;
hw/i386/intel_iommu.c +185 -11
220 lines not on this page · raw message
hw/i386/trace-events +3 -0
@@ -24,6 +24,9 @@ vtd_inv_qi_head(uint16_t head) "read head %d" vtd_inv_qi_tail(uint16_t head) "write tail %d" vtd_inv_qi_fetch(void) "" vtd_context_cache_reset(void) "" +vtd_pasid_cache_gsi(void) "" +vtd_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16 +vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32 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 v4 11/20 intel_iommu: Handle PASID entry addition
In reply to Zhenzhong DuanWhen guest creates new PASID entries, QEMU will capture the guest pasid selective pasid cache invalidation, walk through each passthrough device and each pasid, when a match is found, identify an existing vtd_as or create a new one and update its corresponding cached pasid entry.
--- hw/i386/intel_iommu_internal.h | 2 + hw/i386/intel_iommu.c | 176 ++++++++++++++++++++++++++++++++- 2 files changed, 175 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +2 -0
@@ -559,6 +559,7 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CTX_ENTRY_LEGACY_SIZE 16#define VTD_CTX_ENTRY_SCALABLE_SIZE 32 +#define VTD_SM_CONTEXT_ENTRY_PDTS (x) extract64((x)->val[0], 9, 3)#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL
@@ -589,6 +590,7 @@ typedef struct VTDPASIDCacheInfo {#define VTD_PASID_TABLE_BITS_MASK (0x3fULL)#define VTD_PASID_TABLE_INDEX (pasid) ((pasid) & VTD_PASID_TABLE_BITS_MASK)#define VTD_PASID_ENTRY_FPD (1ULL << 1)/* Fault Processing Disable */ +#define VTD_PASID_TBL_ENTRY_NUM (1ULL << 6)/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL
hw/i386/intel_iommu.c +173 -3
204 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v4 12/20 intel_iommu: Introduce a new pasid cache invalidation type FORCE_RESET
In reply to Zhenzhong DuanFORCE_RESET is different from GLOBAL_INV which updates pasid cache if underlying pasid entry is still valid, it drops all the pasid caches.
FORCE_RESET isn't a VTD spec defined invalidation type for pasid cache, only used internally in system level reset.
--- hw/i386/intel_iommu_internal.h | 9 +++++++++ hw/i386/intel_iommu.c | 25 +++++++++++++++++++++++++ hw/i386/trace-events | 1 + 3 files changed, 35 insertions(+)
hw/i386/intel_iommu_internal.h +9 -0
@@ -569,6 +569,15 @@ typedef enum VTDPCInvType {VTD_PASID_CACHE_DOMSI =VTD_INV_DESC_PASIDC_G_DSI ,VTD_PASID_CACHE_PASIDSI =VTD_INV_DESC_PASIDC_G_PASID_SI ,VTD_PASID_CACHE_GLOBAL_INV =VTD_INV_DESC_PASIDC_G_GLOBAL , + +/* +* Internally used PASID cache invalidation type starts here, +* 0x10 is large enough as invalidation type in pc_inv_desc +* is 2bits in size. +*/ + +/* Reset all PASID cache entries, used in system level reset */ +VTD_PASID_CACHE_FORCE_RESET =0x10 , } VTDPCInvType; typedef struct VTDPASIDCacheInfo {
hw/i386/intel_iommu.c +25 -0
@@ -87,6 +87,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_pasid_cache_reset_locked (IntelIOMMUState *s); +static void vtd_panic_require_caching_mode (void ) { error_report("We need to set caching-mode=on for intel-iommu to enable "
@@ -391,6 +393,7 @@ static void vtd_reset_caches(IntelIOMMUState *s)vtd_iommu_lock (s);vtd_reset_iotlb_locked (s);vtd_reset_context_cache_locked (s); +vtd_pasid_cache_reset_locked (s);vtd_iommu_unlock (s); }
@@ -3180,6 +3183,8 @@ static gboolean vtd_flush_pasid_locked(gpointer key, gpointer value,/* fall through */ case VTD_PASID_CACHE_GLOBAL_INV :break ; +case VTD_PASID_CACHE_FORCE_RESET : + goto remove;default :error_setg (&error_fatal,"invalid pc_info->type for flush" ); }
@@ -3222,6 +3227,23 @@ remove:return true; } +static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s) +{ +VTDPASIDCacheInfo pc_info; + +trace_vtd_pasid_cache_reset (); + + pc_info.type =VTD_PASID_CACHE_FORCE_RESET ; + +/* +* Reset pasid cache is a big hammer, so use g_hash_table_foreach_remove +* which will free all vtd_as instances except those created for PCI +* sub-system. +*/ +g_hash_table_foreach_remove (s->vtd_address_spaces, + vtd_flush_pasid_locked, &pc_info); +} +/* * This function walks over PASID range within [start, end) in a single * PASID table for entries matching @info type/did, then retrieve/create
@@ -3360,6 +3382,9 @@ static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s,case VTD_PASID_CACHE_GLOBAL_INV :/* loop all assigned devices */ break ; +case VTD_PASID_CACHE_FORCE_RESET : +/* For force reset, no need to go further replay */ +return ;default :error_setg (&error_fatal,"invalid pc_info->type for replay" ); }
hw/i386/trace-events +1 -0
@@ -24,6 +24,7 @@ vtd_inv_qi_head(uint16_t head) "read head %d" vtd_inv_qi_tail(uint16_t head) "write tail %d" vtd_inv_qi_fetch(void) "" vtd_context_cache_reset(void) "" +vtd_pasid_cache_reset(void) "" vtd_pasid_cache_gsi(void) "" vtd_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16 vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
Signature · 1 line
2.47.1
PATCH v4 13/20 intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on
In reply to Zhenzhong DuanWhen guest in scalable mode and x-flts=on, 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 nested parent if PGTT=FLT. Otherwise fallback to original processing.
--- hw/i386/intel_iommu.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
hw/i386/intel_iommu.c +34 -0
@@ -1773,6 +1773,28 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce, } +static VTDHostIOMMUDevice *vtd_find_hiod_iommufd (IntelIOMMUState *s, +VTDAddressSpace *as) +{ +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 ; +} + +/* +* vtd_switch_address_space() calls vtd_as_pt_enabled() to determine which +* MR to switch to. Switch to system MR if return true, iommu MR otherwise. +*/ static bool vtd_as_pt_enabled (VTDAddressSpace *as) {IntelIOMMUState *s;
@@ -1781,6 +1803,18 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)assert (as); s = as->iommu_state; + +/* +* When guest in scalable mode and x-flts=on, 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 +* nested parent if PGTT=FLT. Otherwise fallback to original +* processing. +*/ +if (s->root_scalable && s->flts &&vtd_find_hiod_iommufd (s, as)) { +return true; + } +if (vtd_dev_to_context_entry (s,pci_bus_num (as->bus), as->devfn, &ce)) {/*
Signature · 1 line
2.47.1
PATCH v4 14/20 intel_iommu: 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 mode and PGTT configuration.
When PGTT is Pass-through(100b), the hwpt on host side is a stage-2 page table(GPA->HPA). When PGTT is First-stage Translation only(001b), vIOMMU reuse hwpt(GPA->HPA) provided by VFIO as nested parent to construct nested page table.
When guest decides to use legacy mode then vIOMMU switches the MRs of the device's AS, hence the IOAS created by VFIO container would be switched to using the IOMMU_NOTIFIER_IOTLB_EVENTS since the MR is switched to IOMMU MR. So it is able to support shadowing the guest IO page table.
--- hw/i386/intel_iommu_internal.h | 14 ++- include/hw/i386/intel_iommu.h | 1 + hw/i386/intel_iommu.c | 221 ++++++++++++++++++++++++++++++++- hw/i386/trace-events | 3 + 4 files changed, 233 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_internal.h +12 -2
@@ -564,6 +564,12 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL +typedef enum VTDPASIDOp { +VTD_PASID_BIND , +VTD_PASID_UPDATE , +VTD_PASID_UNBIND , +}VTDPASIDOp ; + typedef enum VTDPCInvType { /* VTD spec defined PASID cache invalidation type */ VTD_PASID_CACHE_DOMSI = VTD_INV_DESC_PASIDC_G_DSI,
@@ -612,8 +618,12 @@ typedef struct VTDPASIDCacheInfo {#define VTD_SM_PASID_ENTRY_AW 7ULL/* Adjusted guest-address-width */ #define VTD_SM_PASID_ENTRY_DID (x) extract64((x)->val[1], 0, 16) -#define VTD_SM_PASID_ENTRY_FLPM 3ULL -#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_FSPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_SRE_BIT (x) extract64((x)->val[2], 0, 1) +/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */ +#define VTD_SM_PASID_ENTRY_FSPM (x) extract64((x)->val[2], 2, 2) +#define VTD_SM_PASID_ENTRY_WPE_BIT (x) extract64((x)->val[2], 4, 1) +#define VTD_SM_PASID_ENTRY_EAFE_BIT (x) extract64((x)->val[2], 7, 1)/* First Level Paging Structure */ /* Masks for First Level Paging Entry */
include/hw/i386/intel_iommu.h +1 -0
@@ -104,6 +104,7 @@ struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;uint32_t pasid; +uint32_t s1_hwpt;AddressSpace as;IOMMUMemoryRegion iommu;MemoryRegion root;/* The root container of the device */
hw/i386/intel_iommu.c +217 -4
281 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 v4 15/20 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. This is a behavior to ensure safety. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing a context cache invalidation.
--- hw/i386/intel_iommu_internal.h | 2 ++ hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 3 files changed, 45 insertions(+)
hw/i386/intel_iommu_internal.h +2 -0
@@ -584,6 +584,8 @@ typedef enum VTDPCInvType {/* Reset all PASID cache entries, used in system level reset */ VTD_PASID_CACHE_FORCE_RESET =0x10 , +/* Invalidate all PASID entries in a device */ +VTD_PASID_CACHE_DEVSI , } VTDPCInvType; typedef struct VTDPASIDCacheInfo {
hw/i386/intel_iommu.c +42 -0
84 lines not on this page · raw message
hw/i386/trace-events +1 -0
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) "" vtd_pasid_cache_gsi(void) "" vtd_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16 vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32 +vtd_pasid_cache_devsi(uint16_t devfn) "Dev selective PC invalidation dev: 0x%"PRIx16 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 v4 16/20 intel_iommu: 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 stage-1 page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns stage-1 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_internal.h | 6 +++ hw/i386/intel_iommu.c | 95 +++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_internal.h +6 -0
@@ -596,6 +596,12 @@ typedef struct VTDPASIDCacheInfo {uint16_t devfn; } 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 +93 -2
131 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v4 17/20 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 | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
hw/i386/intel_iommu.c +14 -0
@@ -89,6 +89,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); static void vtd_pasid_cache_sync(IntelIOMMUState *s,
@@ -3050,6 +3051,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 */
@@ -3084,6 +3086,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 */
@@ -3774,6 +3777,17 @@ static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s, } } +static void vtd_replay_pasid_bindings_all (IntelIOMMUState *s) +{ +VTDPASIDCacheInfo pc_info = { .type =VTD_PASID_CACHE_GLOBAL_INV }; + +if (!s->flts || !s->root_scalable || !s->dmar_enabled) { +return ; + } + +vtd_replay_guest_pasid_bindings (s, &pc_info); +} +/* * For a PASID cache invalidation, this function handles below scenarios: * a) a present cached pasid entry needs to be removed
Signature · 1 line
2.47.1
PATCH v4 18/20 vfio: Add a new element bypass_ro in VFIOContainerBase
In reply to Zhenzhong DuanWhen bypass_ro is true, read only memory section is bypassed from mapping in the container.
This is a preparing patch to workaround Intel ERRATA_772415.
--- include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/listener.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
include/hw/vfio/vfio-container-base.h +1 -0
@@ -51,6 +51,7 @@ typedef struct VFIOContainerBase { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; } VFIOContainerBase; typedef struct VFIOGuestIOMMU {
hw/vfio/listener.c +9 -4
@@ -364,7 +364,8 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)return true; } -static bool vfio_listener_valid_section (MemoryRegionSection *section, +static bool vfio_listener_valid_section (VFIOContainerBase *bcontainer, +MemoryRegionSection *section,const char *name) {if (vfio_listener_skipped_section (section)) {
@@ -375,6 +376,10 @@ static bool vfio_listener_valid_section(MemoryRegionSection *section,return false; } +if (bcontainer && bcontainer->bypass_ro && section->readonly) { +return false; + } +if (unlikely ((section->offset_within_address_space & ~qemu_real_host_page_mask ()) != (section->offset_within_region & ~qemu_real_host_page_mask ()))) {
@@ -494,7 +499,7 @@ void vfio_container_region_add(VFIOContainerBase *bcontainer,int ret;Error *err =NULL ; -if (!vfio_listener_valid_section (section,"region_add" )) { +if (!vfio_listener_valid_section (bcontainer, section,"region_add" )) {return ; }
@@ -655,7 +660,7 @@ static void vfio_listener_region_del(MemoryListener *listener,int ret;bool try_unmap = true; -if (!vfio_listener_valid_section (section,"region_del" )) { +if (!vfio_listener_valid_section (bcontainer, section,"region_del" )) {return ; }
@@ -812,7 +817,7 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,container_of (listener, VFIODirtyRangesListener, listener);hwaddr iova, end; -if (!vfio_listener_valid_section (section,"tracking_update" ) || +if (!vfio_listener_valid_section (NULL , section,"tracking_update" ) || !vfio_get_section_iova_range (dirty->bcontainer, section, &iova, &end,NULL )) {return ;
Signature · 1 line
2.47.1
Re: [PATCH v4 18/20] vfio: Add a new element bypass_ro in VFIOContainerBase
Quoted 28 lines · Zhenzhong Duan
When
bypass_rois true, read only memory section is bypassed from mapping in the container.This is a preparing patch to workaround Intel
ERRATA_772415.Signed-off-by Zhenzhong Duan <[email protected]>--- include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/listener.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)include/hw/vfio/vfio-container-base.h +1
-0@@ -51,6 +51,7 @@ typedef struct VFIOContainerBase { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; } VFIOContainerBase; typedef struct VFIOGuestIOMMU {hw/vfio/listener.c +0
-0
hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section) · line 369
Quoted 7 lines
hw/vfio/listener.c +2
-1@@ -364,7 +364,8 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)return true; } -static bool vfio_listener_valid_section (MemoryRegionSection *section, +static bool vfio_listener_valid_section (VFIOContainerBase *bcontainer, +MemoryRegionSection *section,const char *name)
Instead of adding a 'VFIOContainerBase *' argument, I would add an extra 'bool bypass_ro' argument.
Thanks,
C.
Quoted 40 lines
hw/vfio/listener.c +7
-3{if (vfio_listener_skipped_section (section)) {@@ -375,6 +376,10 @@ static bool vfio_listener_valid_section(MemoryRegionSection *section,return false; } +if (bcontainer && bcontainer->bypass_ro && section->readonly) { +return false; + } +if (unlikely ((section->offset_within_address_space & ~qemu_real_host_page_mask ()) != (section->offset_within_region & ~qemu_real_host_page_mask ()))) {@@ -494,7 +499,7 @@ void vfio_container_region_add(VFIOContainerBase *bcontainer,int ret;Error *err =NULL ; -if (!vfio_listener_valid_section (section,"region_add" )) { +if (!vfio_listener_valid_section (bcontainer, section,"region_add" )) {return ; }@@ -655,7 +660,7 @@ static void vfio_listener_region_del(MemoryListener *listener,int ret;bool try_unmap = true; -if (!vfio_listener_valid_section (section,"region_del" )) { +if (!vfio_listener_valid_section (bcontainer, section,"region_del" )) {return ; }@@ -812,7 +817,7 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,container_of (listener, VFIODirtyRangesListener, listener);hwaddr iova, end; -if (!vfio_listener_valid_section (section,"tracking_update" ) || +if (!vfio_listener_valid_section (NULL , section,"tracking_update" ) || !vfio_get_section_iova_range (dirty->bcontainer, section, &iova, &end,NULL )) {return ;
RE: [PATCH v4 18/20] vfio: Add a new element bypass_ro in VFIOContainerBase
Quoted 41 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 18/20] vfio: Add a new elementbypass_roin VFIOContainerBaseQuoted 5 lines · Zhenzhong Duan
When
bypass_rois true, read only memory section is bypassed from mapping in the container.This is a preparing patch to workaround Intel
ERRATA_772415.Review
Quoted 7 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/listener.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)(quoted diff) +0
-0
b/include/hw/vfio/vfio-container-base.hhw/vfio/listener.c static bool
Quoted 16 lines
index
bded6e993f..31fd784d76100644include/hw/vfio/vfio-container-base.h +1
-0@@ -51,6 +51,7 @@ typedef struct VFIOContainerBase { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; } VFIOContainerBase; typedef struct VFIOGuestIOMMU {hw/vfio/listener.c +0
-0@@ -364,7 +364,8 @@ static bool
vfio_known_safe_misalignment(MemoryRegionSection *section)Quoted 3 lines
hw/vfio/listener.c +0
-0return true; }
Quoted 8 lines
hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
Quoted 3 lines
hw/vfio/listener.c +2
-1-static bool vfio_listener_valid_section(MemoryRegionSection *section, +static bool vfio_listener_valid_section (VFIOContainerBase *bcontainer, + MemoryRegionSection*section,
hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section) · line 369
Quoted 1 line
hw/vfio/listener.c +0
-0const char *name)Instead of adding a 'VFIOContainerBase *' argument, I would add an extra 'bool
bypass_ro' argument.
Done, see https://github.com/yiliu1765/qemu/commit/b0be8bfc9a899334819f3f4f0704e47116944a53
Opportunistically, I also introduced another patch to bypass dirty tracking for readonly region, see https://github.com/yiliu1765/qemu/commit/a21ce0afd8aec5d5a9e6de46cf46757530cb7d9f
Thanks
Zhenzhong
Re: [PATCH v4 18/20] vfio: Add a new element bypass_ro in VFIOContainerBase
Quoted 45 lines · Zhenzhong
Quoted 45 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 18/20] vfio: Add a new elementbypass_roin VFIOContainerBaseQuoted 5 lines · Zhenzhong Duan
When
bypass_rois true, read only memory section is bypassed from mapping in the container.This is a preparing patch to workaround Intel
ERRATA_772415.Review
Quoted 7 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/listener.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)(quoted diff) +0
-0
b/include/hw/vfio/vfio-container-base.hhw/vfio/listener.c static bool
Quoted 16 lines
index
bded6e993f..31fd784d76100644include/hw/vfio/vfio-container-base.h +1
-0@@ -51,6 +51,7 @@ typedef struct VFIOContainerBase { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; } VFIOContainerBase; typedef struct VFIOGuestIOMMU {hw/vfio/listener.c +0
-0@@ -364,7 +364,8 @@ static bool
vfio_known_safe_misalignment(MemoryRegionSection *section)hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
Quoted 6 lines
hw/vfio/listener.c +2
-1return true; } -static bool vfio_listener_valid_section(MemoryRegionSection *section, +static bool vfio_listener_valid_section (VFIOContainerBase *bcontainer, + MemoryRegionSection*section,
Quoted 6 lines
Quoted 4 lines
hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section) · line 369
Quoted 1 line
hw/vfio/listener.c +0
-0const char *name)Instead of adding a 'VFIOContainerBase *' argument, I would add an extra 'bool
bypass_ro' argument.Done, see https://github.com/yiliu1765/qemu/commit/b0be8bfc9a899334819f3f4f0704e47116944a53
I don't think the extra 'bool bypass_ro' are useful. A part from that, looks good.
Thanks,
C.
Quoted 1 line
Opportunistically, I also introduced another patch to bypass dirty tracking for readonly region, see https://github.com/yiliu1765/qemu/commit/a21ce0afd8aec5d5a9e6de46cf46757530cb7d9f
RE: [PATCH v4 18/20] vfio: Add a new element bypass_ro in VFIOContainerBase
Quoted 58 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 18/20] vfio: Add a new elementbypass_roin VFIOContainerBaseQuoted 50 lines · Zhenzhong
Quoted 49 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v4 18/20] vfio: Add a new elementbypass_roin VFIOContainerBaseQuoted 5 lines · Zhenzhong Duan
When
bypass_rois true, read only memory section is bypassed from mapping in the container.This is a preparing patch to workaround Intel
ERRATA_772415.Review
Quoted 7 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/listener.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)(quoted diff) +0
-0
b/include/hw/vfio/vfio-container-base.hhw/vfio/listener.c static bool
Quoted 16 lines
index
bded6e993f..31fd784d76100644include/hw/vfio/vfio-container-base.h +1
-0@@ -51,6 +51,7 @@ typedef struct VFIOContainerBase { QLIST_HEAD(, VFIODevice) device_list;GList *iova_ranges;NotifierWithReturn cpr_reboot_notifier; +bool bypass_ro; } VFIOContainerBase; typedef struct VFIOGuestIOMMU {hw/vfio/listener.c +0
-0@@ -364,7 +364,8 @@ static bool
vfio_known_safe_misalignment(MemoryRegionSection *section)hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
Quoted 6 lines
hw/vfio/listener.c +2
-1return true; } -static bool vfio_listener_valid_section(MemoryRegionSection *section, +static bool vfio_listener_valid_section (VFIOContainerBase *bcontainer, + MemoryRegionSection*section,
hw/vfio/listener.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section) · line 369
Quoted 1 line
hw/vfio/listener.c +0
-0const char *name)Instead of adding a 'VFIOContainerBase *' argument, I would add an extra 'bool
bypass_ro' argument.
Quoted 6 lines
Quoted 1 line
Done, see
https://github.com/yiliu1765/qemu/commit/b0be8bfc9a899334819f3f4f0704
e47116944a53I don't think the extra 'bool
bypass_ro' are useful. A part from that, looks good.
Guess you mean " bypass_ro = bcontainer->bypass_ro", I'll use bcontainer->bypass_ro directly.
Thanks
Zhenzhong
Quoted 12 lines
Thanks,
C.
Quoted 1 line
Opportunistically, I also introduced another patch to bypass dirty tracking
for readonly region, see
https://github.com/yiliu1765/qemu/commit/a21ce0afd8aec5d5a9e6de46cf46757530cb7d9f
PATCH v4 19/20 Workaround for ERRATA_772415_SPR17
In reply to Zhenzhong DuanOn a system influenced by ERRATA_772415, IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 is repored by IOMMU_DEVICE_GET_HW_INFO. Due to this errata, even the readonly range mapped on stage-2 page table could still be written.
Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/
Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.
Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.
Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."
--- hw/vfio/iommufd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +7 -1
@@ -325,6 +325,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, {ERRP_GUARD ();IOMMUFDBackend *iommufd = vbasedev->iommufd; +struct iommu_hw_info_vtd vtd;uint32_t type, flags =0 ;uint64_t hw_caps;VFIOIOASHwpt *hwpt;
@@ -372,10 +373,15 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,* instead. */ if (!iommufd_backend_get_device_info (vbasedev->iommufd, vbasedev->devid, - &type,NULL ,0 , &hw_caps, errp)) { + &type, &vtd,sizeof (vtd), &hw_caps, + errp)) {return false; } +if (vtd.flags &IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 ) { + container->bcontainer.bypass_ro = true; + } +if (hw_caps &IOMMU_HW_CAP_DIRTY_TRACKING ) { flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; }
Signature · 1 line
2.47.1
PATCH v4 20/20 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 stage-1 translation are there, enable it now.
--- hw/i386/intel_iommu.c | 2 ++ 1 file changed, 2 insertions(+)
hw/i386/intel_iommu.c +2 -0
@@ -5219,6 +5219,8 @@ static bool vtd_check_hiod(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, "when x-flts=on"); return false; } + +return true;#endif error_setg (errp,"host IOMMU is incompatible with stage-1 translation" );
Signature · 1 line
2.47.1
Kindly ping, any more comments?
Thanks
Zhenzhong
Quoted 236 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v4 00/20]intel_iommu: Enable stage-1 translation for passthrough deviceHi,
For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of
guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature
in old days, see [1] 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 stage-1 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 | | Stage1 for GIOVA->GPA | | | '------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------. | | | Stage2 for GPA->HPA, unmanaged domain| | | '--------------------------------------' '-------------'For history reason, there are different namings in different VTD spec rev, Where:
- Stage1 = First stage = First level = flts
- Stage2 = Second stage = Second level = slts
<Intel VT-d Nested translation>This series reuse VFIO device's default hwpt as nested 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", because VFIO device's default hwpt is created 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_capto PCI subsystem. VFIO calls it to get vIOMMU exposed capabilities.
* 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_cap) |.-------------. | | | | | | | | | V | | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | | | link |<------------------------| | Device | | | .---------| (detach_hwpt)| .-------------. | | | | | | | | | ... | .-----------------. .-------------------.Below is an example to enable stage-1 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 testPATCH1-6: Some preparing work PATCH7-8: Compatibility check between vIOMMU and Host IOMMU PATCH9-17: Implement stage-1 page table for passthrough devicePATCH18-19:Workaround for
ERRATA_772415_SPR17
PATCH20: Enable stage-1 translation for passthrough deviceQemu code can be found at [2]
Fault report isn't supported in this series, we presume guest kernel always construct correct stage1 page table for passthrough device. For emulated devices, the emulation code already provided stage1 fault injection.
TODO:
- Fault report to guest when HW stage1 faults[1]
https://patchwork.kernel.org/project/kvm/cover/20210302203827.437645-1
[email protected]/
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v4Thanks
ZhenzhongChangelog:
v4:
- 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 nested 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(Cedric, Nicolin)
- rebase to masterrfcv3:
- s/hwpt_id/id in iommufd_backend_invalidate_cache()'s parameter (Shameer)
- hide vtd vendor specific caps in a wrapper union (Eric, Nicolin)
- simplify return value check ofget_cap()(Eric)
- droprealize_late(Cedric, Eric)
- split patch13:intel_iommu: Add PASID cache management infrastructure (Eric)
- s/vtd_pasid_cache_reset/vtd_pasid_cache_reset_locked (Eric)
- s/vtd_pe_get_domain_id/vtd_pe_get_did (Eric)
- refine comments (Eric, Donald)rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate betweenvtd_asand
iommu pasid, this is important for dropping VTDPASIDAddressSpaceYi Liu (3):
intel_iommu: Replay pasid bindings after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changedZhenzhong Duan (17):
intel_iommu: Renamevtd_ce_get_rid2pasid_entryto
vtd_ce_get_pasid_entry
hw/pci: Introducepci_device_get_viommu_cap()
intel_iommu: Implementget_viommu_cap()callback vfio/iommufd: Force creating nested parent domain
hw/pci: Exportpci_device_get_iommu_bus_devfn()and return bool
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
intel_iommu: Handle PASID entry removal and update
intel_iommu: Handle PASID entry addition
intel_iommu: Introduce a new pasid cache invalidation typeFORCE_RESET
intel_iommu: Stick to system MR for IOMMUFD backed host device when
x-fls=on
intel_iommu: Bind/unbind guest page table to host
vfio: Add a new elementbypass_roin VFIOContainerBase Workaround forERRATA_772415_SPR17
intel_iommu: Enable host device when x-flts=on in scalable modeMAINTAINERS | 1 + hw/i386/intel_iommu_internal.h | 68 +- include/hw/i386/intel_iommu.h | 9 +- include/hw/iommu.h | 17 + include/hw/pci/pci.h | 27 + include/hw/vfio/vfio-container-base.h | 1 + hw/i386/intel_iommu.c | 941 +++++++++++++++++++++++++- hw/pci/pci.c | 23 +- hw/vfio/iommufd.c | 22 +- hw/vfio/listener.c | 13 +- hw/i386/trace-events | 8 + 11 files changed, 1088 insertions(+), 42 deletions(-)create mode 100644
include/hw/iommu.hbase-commit:
92c05be4dfb59a71033d4c57dac944b29f7dabf0--
2.47.1
Quoted 1 line · Zhenzhong
Kindly ping, any more comments?
Do you have enough comments for a new version. I plan to have a look either this version or a new version next week. :)
Regards,
Yi Liu
Quoted 240 lines
Thanks
ZhenzhongQuoted 236 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v4 00/20]intel_iommu: Enable stage-1 translation for passthrough deviceHi,
For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of
guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature
in old days, see [1] 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 stage-1 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 | | Stage1 for GIOVA->GPA | | | '------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------. | | | Stage2 for GPA->HPA, unmanaged domain| | | '--------------------------------------' '-------------'For history reason, there are different namings in different VTD spec rev, Where:
- Stage1 = First stage = First level = flts
- Stage2 = Second stage = Second level = slts
<Intel VT-d Nested translation>This series reuse VFIO device's default hwpt as nested 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", because VFIO device's default hwpt is created 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_capto PCI subsystem. VFIO calls it to get vIOMMU exposed capabilities.
* 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_cap) |.-------------. | | | | | | | | | V | | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | | | link |<------------------------| | Device | | | .---------| (detach_hwpt)| .-------------. | | | | | | | | | ... | .-----------------. .-------------------.Below is an example to enable stage-1 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 testPATCH1-6: Some preparing work PATCH7-8: Compatibility check between vIOMMU and Host IOMMU PATCH9-17: Implement stage-1 page table for passthrough devicePATCH18-19:Workaround for
ERRATA_772415_SPR17
PATCH20: Enable stage-1 translation for passthrough deviceQemu code can be found at [2]
Fault report isn't supported in this series, we presume guest kernel always construct correct stage1 page table for passthrough device. For emulated devices, the emulation code already provided stage1 fault injection.
TODO:
- Fault report to guest when HW stage1 faults[1]
https://patchwork.kernel.org/project/kvm/cover/20210302203827.437645-1
[email protected]/
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v4Thanks
ZhenzhongChangelog:
v4:
- 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 nested 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(Cedric, Nicolin)
- rebase to masterrfcv3:
- s/hwpt_id/id in iommufd_backend_invalidate_cache()'s parameter (Shameer)
- hide vtd vendor specific caps in a wrapper union (Eric, Nicolin)
- simplify return value check ofget_cap()(Eric)
- droprealize_late(Cedric, Eric)
- split patch13:intel_iommu: Add PASID cache management infrastructure (Eric)
- s/vtd_pasid_cache_reset/vtd_pasid_cache_reset_locked (Eric)
- s/vtd_pe_get_domain_id/vtd_pe_get_did (Eric)
- refine comments (Eric, Donald)rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate betweenvtd_asand
iommu pasid, this is important for dropping VTDPASIDAddressSpaceYi Liu (3):
intel_iommu: Replay pasid bindings after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changedZhenzhong Duan (17):
intel_iommu: Renamevtd_ce_get_rid2pasid_entryto
vtd_ce_get_pasid_entry
hw/pci: Introducepci_device_get_viommu_cap()
intel_iommu: Implementget_viommu_cap()callback vfio/iommufd: Force creating nested parent domain
hw/pci: Exportpci_device_get_iommu_bus_devfn()and return bool
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
intel_iommu: Handle PASID entry removal and update
intel_iommu: Handle PASID entry addition
intel_iommu: Introduce a new pasid cache invalidation typeFORCE_RESET
intel_iommu: Stick to system MR for IOMMUFD backed host device when
x-fls=on
intel_iommu: Bind/unbind guest page table to host
vfio: Add a new elementbypass_roin VFIOContainerBase Workaround forERRATA_772415_SPR17
intel_iommu: Enable host device when x-flts=on in scalable modeMAINTAINERS | 1 + hw/i386/intel_iommu_internal.h | 68 +- include/hw/i386/intel_iommu.h | 9 +- include/hw/iommu.h | 17 + include/hw/pci/pci.h | 27 + include/hw/vfio/vfio-container-base.h | 1 + hw/i386/intel_iommu.c | 941 +++++++++++++++++++++++++- hw/pci/pci.c | 23 +- hw/vfio/iommufd.c | 22 +- hw/vfio/listener.c | 13 +- hw/i386/trace-events | 8 + 11 files changed, 1088 insertions(+), 42 deletions(-)create mode 100644
include/hw/iommu.hbase-commit:
92c05be4dfb59a71033d4c57dac944b29f7dabf0--
2.47.1
Quoted 7 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v4 00/20]intel_iommu: Enable stage-1 translation for passthrough device
Quoted 5 lines
Quoted 1 line · Zhenzhong
Kindly ping, any more comments?
Do you have enough comments for a new version. I plan to have a look either this version or a new version next week. :)
That's appreciated, thanks Yi. I think not, there are only a few comments from Cedric for VFIO related patches.
Zhenzhong
Quoted 5 lines · Yi Liu
Quoted 1 line · Zhenzhong
Kindly ping, any more comments?
Do you have enough comments for a new version. I plan to have a look either this version or a new version next week. :)
same for me ;-)
Eric
Quoted 263 lines
Regards,
Yi LiuQuoted 258 lines
Thanks
ZhenzhongQuoted 254 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v4 00/20]intel_iommu: Enable stage-1 translation for passthrough deviceHi,
For passthrough device with intel_iommu.x-flts=on, we don't do shadowing
of
guest page table for passthrough device but pass stage-1 page table to host
side to construct a nested domain. There was some effort to enable this feature
in old days, see [1] 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 stage-1 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 | | Stage1 for GIOVA->GPA | | | '------------------------' .----------------/ | | PASID Entry | V (Nested xlate) '----------------\.--------------------------------------. | | | Stage2 for GPA->HPA, unmanaged domain| | | '--------------------------------------''-------------'
For history reason, there are different namings in different VTD spec rev,
Where:
- Stage1 = First stage = First level = flts
- Stage2 = Second stage = Second level = slts
<Intel VT-d Nested translation>This series reuse VFIO device's default hwpt as nested 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", 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_capto PCI subsystem. VFIO calls it to get vIOMMU exposed capabilities.
* 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_cap) |.-------------. | | | | | | | | | V | | .---------| HostIOMMUDeviceIOMMUFD | .-------------. | | | IOMMUFD | (attach_hwpt)| | Host IOMMU | | | | link |<------------------------| | Device | | | .---------| (detach_hwpt)| .-------------. | | | | | | | | | ... |.-----------------. .-------------------.
Below is an example to enable stage-1 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 testPATCH1-6: Some preparing work
PATCH7-8: Compatibility check between vIOMMU and Host IOMMU
PATCH9-17: Implement stage-1 page table for passthrough device PATCH18-19:Workaround forERRATA_772415_SPR17PATCH20: Enable stage-1 translation for passthrough deviceQemu code can be found at [2]
Fault report isn't supported in this series, we presume guest kernel always
construct correct stage1 page table for passthrough device. For emulated
devices, the emulation code already provided stage1 fault injection.TODO:
- Fault report to guest when HW stage1 faults[1]
https://patchwork.kernel.org/project/kvm/cover/20210302203827.437645-1
[email protected]/
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v4Thanks
ZhenzhongChangelog:
v4:
- 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 nested 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(Cedric, Nicolin)
- rebase to masterrfcv3:
- s/hwpt_id/id in iommufd_backend_invalidate_cache()'s parameter (Shameer)
- hide vtd vendor specific caps in a wrapper union (Eric, Nicolin)
- simplify return value check ofget_cap()(Eric)
- droprealize_late(Cedric, Eric)
- split patch13:intel_iommu: Add PASID cache management infrastructure (Eric)
- s/vtd_pasid_cache_reset/vtd_pasid_cache_reset_locked (Eric)
- s/vtd_pe_get_domain_id/vtd_pe_get_did (Eric)
- refine comments (Eric, Donald)rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate betweenvtd_asand
iommu pasid, this is important for dropping VTDPASIDAddressSpaceYi Liu (3):
intel_iommu: Replay pasid bindings after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is changedZhenzhong Duan (17):
intel_iommu: Renamevtd_ce_get_rid2pasid_entrytovtd_ce_get_pasid_entry
hw/pci: Introducepci_device_get_viommu_cap()
intel_iommu: Implementget_viommu_cap()callback vfio/iommufd: Force creating nested parent domain
hw/pci: Exportpci_device_get_iommu_bus_devfn()and return bool
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on
intel_iommu: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
intel_iommu: Handle PASID entry removal and update
intel_iommu: Handle PASID entry addition
intel_iommu: Introduce a new pasid cache invalidation typeFORCE_RESET
intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on
intel_iommu: Bind/unbind guest page table to host
vfio: Add a new elementbypass_roin VFIOContainerBase Workaround forERRATA_772415_SPR17
intel_iommu: Enable host device when x-flts=on in scalable modeMAINTAINERS | 1 +
hw/i386/intel_iommu_internal.h | 68 +-
include/hw/i386/intel_iommu.h | 9 +-
include/hw/iommu.h | 17 +
include/hw/pci/pci.h | 27 +include/hw/vfio/vfio-container-base.h| 1 +
hw/i386/intel_iommu.c | 941
+++++++++++++++++++++++++-
hw/pci/pci.c | 23 +-
hw/vfio/iommufd.c | 22 +-
hw/vfio/listener.c | 13 +-
hw/i386/trace-events | 8 +11 files changed, 1088 insertions(+), 42 deletions(-)create mode 100644
include/hw/iommu.hbase-commit:
92c05be4dfb59a71033d4c57dac944b29f7dabf0Signature · 1 line
2.47.1
Hi Eric, Yi,
Quoted 7 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v4 00/20]intel_iommu: Enable stage-1 translation for passthrough device
Quoted 7 lines
Quoted 5 lines · Yi Liu
Quoted 1 line · Zhenzhong
Kindly ping, any more comments?
Do you have enough comments for a new version. I plan to have a look either this version or a new version next week. :)
same for me ;-)
I'll send v5 per Michael's suggestion by end of this week.
Thanks
Zhenzhong
Quoted 4 lines · Zhenzhong
Kindly ping, any more comments?
Thanks
Zhenzhong
I think there's been enough comments to spin v5.
Signature · 1 line
MST