intel_iommu: Enable stage-1 translation for passthrough device
cover intel_iommu: Enable stage-1 translation for passthrough device
Hi,
After VFIO/IOMMUFD prerequisite patchset got accepted, now this focuses on stage-1 translation for passthrough device in intel_iommu. I thought it's time to bump to v1 from rfcv3.
rfcv3 cover-letter:
Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.
This series is 2nd part focusing on passthrough device. 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 [2] for details.
The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform the 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>
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 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_iommu_device) || Host IOMMU | |
| | Device |------------------------>|| Device list | |
| .---------|(unset_iommu_device) |.-------------. |
| | | | |
| | | V |
| .---------| HostIOMMUDeviceIOMMUFD | .-------------. |
| | IOMMUFD | (attach_hwpt)| | Host IOMMU | |
| | link |<------------------------| | Device | |
| .---------| (detach_hwpt)| .-------------. |
| | | | |
| | | ... |
.-----------------. .-------------------.Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.
E.g., Under one guest's scope, Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.
SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.
See below example diagram for a full view:
IntelIOMMUState
|
V
.------------------. .------------------. .-------------------.
| VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer |-->...
| (iommufd0,RW&RO) | | (iommufd1,RW&RO) | | (iommufd0,only RW)|
.------------------. .------------------. .-------------------.
| | |
| .-->... |
V V
.-------------------. .-------------------. .---------------.
| VTDS2Hwpt(CC) |--->| VTDS2Hwpt(non-CC) |-->... | VTDS2Hwpt(CC) |-->...
.-------------------. .-------------------. .---------------.
| | | |
| | | |
.-----------. .-----------. .------------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device | | Device(CC) |
| (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) |
| | | | | (iommufd0) | | (iommufd0) |
.-----------. .-----------. .------------. .------------.This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.
To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...
Passthrough device should use iommufd backend to work with stage-1 translation. i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...
If host doesn't support nested translation, qemu will fail with an unsupported report.
Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- build on windows
Fault report isn't supported in this series, we presume guest kernel always construct correct S1 page table for passthrough device. For emulated devices, the emulation code already provided S1 fault injection.
PATCH1-2: Some cleanup work PATCH3: cap/ecap related compatibility check between vIOMMU and Host IOMMU PATCH4-14: Implement stage-1 page table for passthrough device PATCH15: Enable stage-1 translation for passthrough device
Qemu code can be found at [3]
TODO:
- RAM discard
- dirty tracking on stage-2 page table
- Fault report to guest when HW S1 faults
[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v1
Thanks
Zhenzhong
Changelog:
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 binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed
Zhenzhong Duan (12):
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
intel_iommu: Handle PASID entry removing and updating
intel_iommu: Handle PASID entry adding
intel_iommu: Introduce a new pasid cache invalidation type FORCE_RESET
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode
hw/i386/intel_iommu_internal.h | 56 ++ include/hw/i386/intel_iommu.h | 33 +- hw/i386/intel_iommu.c | 1659 ++++++++++++++++++++++++++++---- hw/i386/trace-events | 13 + 4 files changed, 1563 insertions(+), 198 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/15 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.34.1
Re: [PATCH v1 01/15] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry
Quoted 11 lines · Zhenzhong Duan
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 namevtd_ce_get_pasid_entryis better to match what it actually does.No functional change intended.
Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Clément Mathieu--Drif<[email protected]>--- hw/i386/intel_iommu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
Quoted 59 lines
hw/i386/intel_iommu.c +6
-6@@ -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 ]); }
hw/i386/intel_iommu.c static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce, · line 1693
Quoted 8 lines
hw/i386/intel_iommu.c +1
-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
a typo. not related to this patch though.
Signature · 2 lines
Regards, Yi Liu
Re: [PATCH v1 01/15] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry
In reply to Zhenzhong DuanHi Zhenzhong,
Quoted 8 lines · Zhenzhong Duan
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 namevtd_ce_get_pasid_entryis better to match what it actually does.No functional change intended.
Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Clément Mathieu--Drif<[email protected]>
Cheers
Eric
Quoted 71 lines
--- 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
PATCH 2/15 intel_iommu: Optimize context entry cache utilization
In reply to Zhenzhong DuanThere are many call sites referencing context entry by calling vtd_dev_to_context_entry() which will traverse the DMAR table.
In most cases we can use cached context entry in vtd_as->context_cache_entry except when its entry is stale. Currently only global and domain context invalidation stale it.
So introduce a helper function vtd_as_to_context_entry() to fetch from cache before trying with vtd_dev_to_context_entry().
--- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)
hw/i386/intel_iommu.c +23 -13
@@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,return 0 ; } +static int vtd_as_to_context_entry (VTDAddressSpace *vtd_as,VTDContextEntry *ce) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + +/* Try to fetch context-entry from cache first */ +if (cc_entry->context_cache_gen == s->context_cache_gen) { + *ce = cc_entry->context_entry; +return 0 ; + }else { +return vtd_dev_to_context_entry (s, bus_num, devfn, ce); + } +} +static int vtd_sync_shadow_page_hook (const IOMMUTLBEvent *event,void *private) {
@@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)return 0 ; } - ret =vtd_dev_to_context_entry (vtd_as->iommu_state, -pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (ret) {if (ret == -VTD_FR_CONTEXT_ENTRY_P ) {/*
@@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)assert (as); s = as->iommu_state; -if (vtd_dev_to_context_entry (s,pci_bus_num (as->bus), as->devfn, - &ce)) { +if (vtd_as_to_context_entry (as, &ce)) {/* * Possibly failed to parse the context entry for some reason * (e.g., during init, or any guest configuration errors on
@@ -2435,8 +2448,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { -if (!vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce) && +if (!vtd_as_to_context_entry (vtd_as, &ce) && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {vtd_address_space_sync (vtd_as); }
@@ -2458,8 +2470,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,hwaddr size = (1 << am) *VTD_PAGE_SIZE ; QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { - ret =vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (!ret && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =PCI_NO_PASID ;
@@ -2966,8 +2977,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { -if (!vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce) && +if (!vtd_as_to_context_entry (vtd_as, &ce) && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =VTD_CE_GET_RID2PASID (&ce);
@@ -4146,7 +4156,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,assert (vtd_as->pasid !=PCI_NO_PASID );/* Try out best to fetch FPD, we can't do anything more */ -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { is_fpd_set = ce.lo &VTD_CONTEXT_ENTRY_FPD ;if (!is_fpd_set && s->root_scalable) {vtd_ce_get_pasid_fpd (s, &ce, &is_fpd_set, vtd_as->pasid);
@@ -4506,7 +4516,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)/* replay is protected by BQL, page walk will re-setup it safely */ iova_tree_remove (vtd_as->iova_tree, map); -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : "legacy mode", bus_n, PCI_SLOT(vtd_as->devfn),
Signature · 1 line
2.34.1
Re: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Quoted 9 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().
The cached context entry is now protected by vtd_iommu_lock(). While not all caller of vtd_dev_to_context_entry() are under this lock.
Also, the cached context entry is created in the translate path. IMHO, this path is not supposed to be triggered for passthrough devices. While this may need double check and may change in the future. But let's see if any locking issue with the current code.
Quoted 54 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)hw/i386/intel_iommu.c +18
-5@@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,return 0 ; } +static int vtd_as_to_context_entry (VTDAddressSpace *vtd_as,VTDContextEntry *ce) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + +/* Try to fetch context-entry from cache first */ +if (cc_entry->context_cache_gen == s->context_cache_gen) { + *ce = cc_entry->context_entry; +return 0 ; + }else { +return vtd_dev_to_context_entry (s, bus_num, devfn, ce); + } +} +static int vtd_sync_shadow_page_hook (const IOMMUTLBEvent *event,void *private) {@@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)return 0 ; } - ret =vtd_dev_to_context_entry (vtd_as->iommu_state, -pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (ret) {if (ret == -VTD_FR_CONTEXT_ENTRY_P ) {/* @@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)assert (as); s = as->iommu_state; -if (vtd_dev_to_context_entry (s,pci_bus_num (as->bus), as->devfn, - &ce)) { +if (vtd_as_to_context_entry (as, &ce)) {/* * Possibly failed to parse the context entry for some reason * (e.g., during init, or any guest configuration errors on
hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 2451
Quoted 6 lines
hw/i386/intel_iommu.c +1
-2@@ -2435,8 +2448,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { - if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), - vtd_as->devfn, &ce) && + if (!vtd_as_to_context_entry(vtd_as, &ce) &&
this one apparently is called out of lock.
Quoted 41 lines
hw/i386/intel_iommu.c +4
-6domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {vtd_address_space_sync (vtd_as); }@@ -2458,8 +2470,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,hwaddr size = (1 << am) *VTD_PAGE_SIZE ; QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { - ret =vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (!ret && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =PCI_NO_PASID ;@@ -2966,8 +2977,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { -if (!vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce) && +if (!vtd_as_to_context_entry (vtd_as, &ce) && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =VTD_CE_GET_RID2PASID (&ce);@@ -4146,7 +4156,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,assert (vtd_as->pasid !=PCI_NO_PASID );/* Try out best to fetch FPD, we can't do anything more */ -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { is_fpd_set = ce.lo &VTD_CONTEXT_ENTRY_FPD ;if (!is_fpd_set && s->root_scalable) {vtd_ce_get_pasid_fpd (s, &ce, &is_fpd_set, vtd_as->pasid);@@ -4506,7 +4516,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)/* replay is protected by BQL, page walk will re-setup it safely */ iova_tree_remove (vtd_as->iova_tree, map); -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : "legacy mode", bus_n, PCI_SLOT(vtd_as->devfn),
Signature · 2 lines
Regards, Yi Liu
RE: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Quoted 18 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationQuoted 9 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().
Quoted 7 lines
The cached context entry is now protected by
vtd_iommu_lock(). While not all caller ofvtd_dev_to_context_entry()are under this lock.Also, the cached context entry is created in the translate path. IMHO, this path is not supposed to be triggered for passthrough devices. While this may need double check and may change in the future. But let's see if any locking issue with the current code.
Good finding, yes.
Previously I thought translation path updates cc_entry->context_entry after cc_entry->context_cache_gen. In vtd_as_to_context_entry() cc_entry->context_cache_gen is checked first, so there was no real race. But I still missed a memory barrier like below:
@@ -2277,6 +2286,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
cc_entry->context_cache_gen,
s->context_cache_gen);
cc_entry->context_entry = ce;
+ smp_wmb();
cc_entry->context_cache_gen = s->context_cache_gen;
}Another option I can think of is adding lock to cache reading like below:
@@ -1659,11 +1659,15 @@ static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)
uint8_t devfn = vtd_as->devfn;
VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;+ vtd_iommu_lock(s);
+
/* Try to fetch context-entry from cache first */
if (cc_entry->context_cache_gen == s->context_cache_gen) {
*ce = cc_entry->context_entry;
+ vtd_iommu_unlock(s);
return 0;
} else {
+ vtd_iommu_unlock(s);
return vtd_dev_to_context_entry(s, bus_num, devfn, ce);
}}
Which one do you prefer?
Thanks
Zhenzhong
Re: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Quoted 26 lines · Zhenzhong
Quoted 18 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationQuoted 9 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().Quoted 7 lines
The cached context entry is now protected by
vtd_iommu_lock(). While not all caller ofvtd_dev_to_context_entry()are under this lock.Also, the cached context entry is created in the translate path. IMHO, this path is not supposed to be triggered for passthrough devices. While this may need double check and may change in the future. But let's see if any locking issue with the current code.
Quoted 4 lines
Good finding, yes.
Previously I thought translation path updatescc_entry->context_entryaftercc_entry->context_cache_gen. Invtd_as_to_context_entry()cc_entry->context_cache_genis checked first, so there was no real race. But I still missed a memory barrier like below:
yeah, testing context_cache_gen is necessary. But without lock, this cannot guarantee the cc_entry is valid after the test.
Quoted 9 lines
(quoted diff) +1
-0@@ -2277,6 +2286,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, cc_entry->context_cache_gen, s->context_cache_gen); cc_entry->context_entry = ce; + smp_wmb(); cc_entry->context_cache_gen = s->context_cache_gen; }Another option I can think of is adding lock to cache reading like below:
this is in-enough as well since the cc_entry->context_entry can be modified after lock is released.
Quoted 17 lines
(quoted diff) +4
-0@@ -1659,11 +1659,15 @@ static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) uint8_t devfn = vtd_as->devfn; VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + vtd_iommu_lock(s); + /* Try to fetch context-entry from cache first */ if (cc_entry->context_cache_gen == s->context_cache_gen) { *ce = cc_entry->context_entry; + vtd_iommu_unlock(s); return 0; } else { + vtd_iommu_unlock(s); return vtd_dev_to_context_entry(s, bus_num, devfn, ce); } }
Quoted 1 line
Which one do you prefer?
If it's just optimization, perhaps just drop it. :)
Signature · 2 lines
Regards, Yi Liu
RE: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Quoted 78 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationQuoted 7 lines · Zhenzhong
Quoted 7 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationQuoted 10 lines
Quoted 5 lines · Zhenzhong Duan
Quoted 4 lines
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in vtd_as-
context_cache_entryQuoted 4 lines
Quoted 4 lines
except when its entry is stale. Currently only global and domain context invalidation stale it.
So introduce a helper function
vtd_as_to_context_entry()to fetch fromcache
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
before trying with
vtd_dev_to_context_entry().The cached context entry is now protected by
vtd_iommu_lock(). While not all caller ofvtd_dev_to_context_entry()are under this lock.Quoted 7 lines
Quoted 4 lines
Also, the cached context entry is created in the translate path. IMHO, this path is not supposed to be triggered for passthrough devices. While this may need double check and may change in the future. But let's see if any locking issue with the current code.
Good finding, yes.
Previously I thought translation path updatescc_entry->context_entryafter
cc_entry->context_cache_gen.Quoted 1 line
In
vtd_as_to_context_entry()cc_entry->context_cache_genis checked first, sothere was no real race.
Quoted 1 line
But I still missed a memory barrier like below:
yeah, testing
context_cache_genis necessary. But without lock, this cannot guarantee thecc_entryis valid after the test.Review static bool
Quoted 1 line
(quoted diff) +0
-0@@ -2277,6 +2286,7 @@ static boolvtd_do_iommu_translate(VTDAddressSpace
*vtd_as, PCIBus *bus,Quoted 8 lines
(quoted diff) +1
-0cc_entry->context_cache_gen, s->context_cache_gen); cc_entry->context_entry = ce; + smp_wmb(); cc_entry->context_cache_gen = s->context_cache_gen; }Another option I can think of is adding lock to cache reading like below:
this is in-enough as well since the
cc_entry->context_entrycan be modified after lock is released.Review static int
Quoted 1 line
(quoted diff) +0
-0@@ -1659,11 +1659,15 @@ static int
vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)Quoted 16 lines
(quoted diff) +4
-0uint8_t devfn = vtd_as->devfn; VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + vtd_iommu_lock(s); + /* Try to fetch context-entry from cache first */ if (cc_entry->context_cache_gen == s->context_cache_gen) { *ce = cc_entry->context_entry; + vtd_iommu_unlock(s); return 0; } else { + vtd_iommu_unlock(s); return vtd_dev_to_context_entry(s, bus_num, devfn, ce); } }
Quoted 3 lines
Quoted 1 line
Which one do you prefer?
If it's just optimization, perhaps just drop it. :)
Fine for me, will do.
Thanks
Zhenzhong
Re: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
In reply to Zhenzhong DuanHi Zhenzhong,
Quoted 29 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)hw/i386/intel_iommu.c +6
-0@@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,return 0 ; } +static int vtd_as_to_context_entry (VTDAddressSpace *vtd_as,VTDContextEntry *ce) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 1615
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+ +/* Try to fetch context-entry from cache first */ +if (cc_entry->context_cache_gen == s->context_cache_gen) { + *ce = cc_entry->context_entry; +return 0 ; + }else { +return vtd_dev_to_context_entry (s, bus_num, devfn, ce); + } +} +
While the patch looks good to me can't you use the helper also in vtd_do_iommu_translate()?
See " /* Try to fetch context-entry from cache first */"
If not you may add a comment in the commit desc while it can't be applied there.
Thanks
Eric
Quoted 72 lines
hw/i386/intel_iommu.c +7
-13static int vtd_sync_shadow_page_hook (const IOMMUTLBEvent *event,void *private) {@@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)return 0 ; } - ret =vtd_dev_to_context_entry (vtd_as->iommu_state, -pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (ret) {if (ret == -VTD_FR_CONTEXT_ENTRY_P ) {/* @@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)assert (as); s = as->iommu_state; -if (vtd_dev_to_context_entry (s,pci_bus_num (as->bus), as->devfn, - &ce)) { +if (vtd_as_to_context_entry (as, &ce)) {/* * Possibly failed to parse the context entry for some reason * (e.g., during init, or any guest configuration errors on @@ -2435,8 +2448,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { -if (!vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce) && +if (!vtd_as_to_context_entry (vtd_as, &ce) && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {vtd_address_space_sync (vtd_as); }@@ -2458,8 +2470,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,hwaddr size = (1 << am) *VTD_PAGE_SIZE ; QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { - ret =vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce); + ret =vtd_as_to_context_entry (vtd_as, &ce);if (!ret && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =PCI_NO_PASID ;@@ -2966,8 +2977,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,vtd_iommu_unlock (s); QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { -if (!vtd_dev_to_context_entry (s,pci_bus_num (vtd_as->bus), - vtd_as->devfn, &ce) && +if (!vtd_as_to_context_entry (vtd_as, &ce) && domain_id ==vtd_get_domain_id (s, &ce, vtd_as->pasid)) {uint32_t rid2pasid =VTD_CE_GET_RID2PASID (&ce);@@ -4146,7 +4156,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,assert (vtd_as->pasid !=PCI_NO_PASID );/* Try out best to fetch FPD, we can't do anything more */ -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { is_fpd_set = ce.lo &VTD_CONTEXT_ENTRY_FPD ;if (!is_fpd_set && s->root_scalable) {vtd_ce_get_pasid_fpd (s, &ce, &is_fpd_set, vtd_as->pasid);@@ -4506,7 +4516,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)/* replay is protected by BQL, page walk will re-setup it safely */ iova_tree_remove (vtd_as->iova_tree, map); -if (vtd_dev_to_context_entry (s, bus_n, vtd_as->devfn, &ce) ==0 ) { +if (vtd_as_to_context_entry (vtd_as, &ce) ==0 ) { trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : "legacy mode", bus_n, PCI_SLOT(vtd_as->devfn),
RE: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Hi Eric,
Quoted 44 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationHi Zhenzhong,
hw/i386/intel_iommu.c static int
Quoted 20 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)hw/i386/intel_iommu.c +0
-0@@ -1597,6 +1597,22 @@ static intvtd_dev_to_context_entry(IntelIOMMUState *s,
uint8_tbus_num,hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0return 0 ; } +static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as,VTDContextEntry *ce)
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+{ +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + +/* Try to fetch context-entry from cache first */ +if (cc_entry->context_cache_gen == s->context_cache_gen) { + *ce = cc_entry->context_entry; +return 0 ;
Quoted 8 lines
hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 1615
Quoted 5 lines
hw/i386/intel_iommu.c +5
-0+ }else { +return vtd_dev_to_context_entry (s, bus_num, devfn, ce); + } +} +While the patch looks good to me can't you use the helper also in
vtd_do_iommu_translate()?
See " /* Try to fetch context-entry from cache first */"
It can, but it finally calls into vtd_dev_to_context_entry() so we can call vtd_dev_to_context_entry() directly. I will drop this patch following Yi's suggestion.
Thanks
Zhenzhong
Re: [PATCH v1 02/15] intel_iommu: Optimize context entry cache utilization
Quoted 54 lines · Zhenzhong
Hi Eric,
Quoted 44 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 02/15]intel_iommu: Optimize context entry cache utilizationHi Zhenzhong,
hw/i386/intel_iommu.c static int
Quoted 20 lines · Zhenzhong Duan
There are many call sites referencing context entry by calling
vtd_dev_to_context_entry()which will traverse the DMAR table.In most cases we can use cached context entry in
vtd_as->context_cache_entryexcept when its entry is stale. Currently only global and domain context invalidation stale it.So introduce a helper function
vtd_as_to_context_entry()to fetch from cache before trying withvtd_dev_to_context_entry().Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)hw/i386/intel_iommu.c +0
-0@@ -1597,6 +1597,22 @@ static intvtd_dev_to_context_entry(IntelIOMMUState *s,
uint8_tbus_num,hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0return 0 ; } +static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as,VTDContextEntry *ce)
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+{ +IntelIOMMUState *s = vtd_as->iommu_state; +uint8_t bus_num =pci_bus_num (vtd_as->bus); +uint8_t devfn = vtd_as->devfn; +VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry; + +/* Try to fetch context-entry from cache first */ +if (cc_entry->context_cache_gen == s->context_cache_gen) { + *ce = cc_entry->context_entry; +return 0 ;Quoted 8 lines
hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 1615
Quoted 5 lines
hw/i386/intel_iommu.c +5
-0+ }else { +return vtd_dev_to_context_entry (s, bus_num, devfn, ce); + } +} +While the patch looks good to me can't you use the helper also in
vtd_do_iommu_translate()?
See " /* Try to fetch context-entry from cache first */"
Quoted 2 lines
It can, but it finally calls into
vtd_dev_to_context_entry()so we can callvtd_dev_to_context_entry()directly. I will drop this patch following Yi's suggestion.
OK
Cheers
Eric
Quoted 3 lines
Thanks
Zhenzhong
PATCH 3/15 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 be failed.
--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
@@ -191,6 +191,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_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43)
hw/i386/intel_iommu.c +28 -0
@@ -39,6 +39,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) \
@@ -4361,6 +4362,33 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } +#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 device is uncompatible with stage-1 translation" );return false; }
Signature · 1 line
2.34.1
Re: [PATCH v1 03/15] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on
Hi Zhenzhong,
Quoted 40 lines · Zhenzhong Duan
When 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 be failed.
Signed-off-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)hw/i386/intel_iommu_internal.h +1
-0@@ -191,6 +191,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_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43)hw/i386/intel_iommu.c +1
-0@@ -39,6 +39,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) \
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod, · line 4365
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0@@ -4361,6 +4362,33 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,return true; } +#ifdef CONFIG_IOMMUFD
is it requested?
Cheers
Eric
Quoted 29 lines
hw/i386/intel_iommu.c +26
-0+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 device is uncompatible with stage-1 translation" );return false; }
RE: [PATCH v1 03/15] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on
Quoted 51 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 03/15]intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=onHi Zhenzhong,
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s,
Quoted 40 lines · Zhenzhong Duan
When 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 be failed.
Signed-off-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)hw/i386/intel_iommu_internal.h +1
-0@@ -191,6 +191,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_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43)hw/i386/intel_iommu.c +1
-0@@ -39,6 +39,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) \@@ -4361,6 +4362,33 @@ static bool vtd_check_hiod(IntelIOMMUState *s,HostIOMMUDevice *hiod,
Quoted 5 lines
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod, · line 4365
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0return true; } +#ifdef CONFIG_IOMMUFD is it requested?
Yes, windows build needs it.iommu_hw_info_vtd and IOMMU_HW_INFO_TYPE_INTEL_VTD are defined in linux/iommufd.h, meanwhile all below check take effect only when IOMMUFD is supported.
Thanks
Zhenzhong
Quoted 34 lines
Cheers
Eric
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
Quoted 5 lines
hw/i386/intel_iommu.c +5
-0+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)) {Quoted 24 lines
hw/i386/intel_iommu.c +21
-0+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 device is uncompatible with stage-1 translation" );return false; }
Re: [PATCH v1 03/15] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on
Quoted 50 lines · Zhenzhong
Quoted 50 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 03/15]intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=onHi Zhenzhong,
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s,
Quoted 40 lines · Zhenzhong Duan
When 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 be failed.
Signed-off-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)hw/i386/intel_iommu_internal.h +1
-0@@ -191,6 +191,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_PASID (1ULL << 40)#define VTD_ECAP_SMTS (1ULL << 43)hw/i386/intel_iommu.c +1
-0@@ -39,6 +39,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) \@@ -4361,6 +4362,33 @@ static bool vtd_check_hiod(IntelIOMMUState *s,HostIOMMUDevice *hiod,
Quoted 7 lines
Quoted 5 lines
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod, · line 4365
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0return true; } +#ifdef CONFIG_IOMMUFD is it requested?
Yes, windows build needs it.
iommu_hw_info_vtdandIOMMU_HW_INFO_TYPE_INTEL_VTDare defined inlinux/iommufd.h,
which is a linux dep
Quoted 1 line
meanwhile all below check take effect only when IOMMUFD is supported.
OK. I tried to remove #imply IOMMUFD in hw/i386/Kconfig and I did not get any error but well I did not build for Windows.
Eric
Quoted 38 lines
Thanks
Zhenzhong
Quoted 33 lines
Cheers
Eric
hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
Quoted 5 lines
hw/i386/intel_iommu.c +5
-0+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)) {Quoted 24 lines
hw/i386/intel_iommu.c +21
-0+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 device is uncompatible with stage-1 translation" );return false; }
PATCH 4/15 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 | 14 ++++++++++++-- 3 files changed, 20 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 +12 -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,
@@ -4397,6 +4400,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,
@@ -4413,6 +4417,12 @@ 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)) {vtd_iommu_unlock (s);return false;
@@ -4423,7 +4433,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.34.1
Re: [PATCH v1 04/15] intel_iommu: Introduce a new structure VTDHostIOMMUDevice
Hi,
Quoted 78 lines · Zhenzhong Duan
Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.
Introduce 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.
Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 14 ++++++++++++-- 3 files changed, 20 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 +5
-1@@ -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,@@ -4397,6 +4400,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,
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, · line 4426
Quoted 10 lines
hw/i386/intel_iommu.c +6
-0@@ -4413,6 +4417,12 @@ 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)) {
Shouldn't we free vtd_hiod here?
Quoted 14 lines
hw/i386/intel_iommu.c +1
-2vtd_iommu_unlock (s);return false;@@ -4423,7 +4433,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); --2.34.1
RE: [PATCH v1 04/15] intel_iommu: Introduce a new structure VTDHostIOMMUDevice
Hi Clement,
Quoted 101 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/15]intel_iommu: Introduce a new structure VTDHostIOMMUDeviceHi,
Quoted 1 line · Zhenzhong Duan
Caution: External email. Do not open attachments or click links, unless this
email comes from a known sender and you know the content is safe.
hw/i386/intel_iommu.c static gboolean vtd_hiod_equal(gconstpointer v1,
Quoted 57 lines
Introduce 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.
Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 14 ++++++++++++-- 3 files changed, 20 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 +0
-0@@ -281,7 +281,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1,gconstpointer v2)
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,
Quoted 12 lines
hw/i386/intel_iommu.c +4
-1static 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,@@ -4397,6 +4400,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,void *opaque, int devfn,
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,
Quoted 8 lines
hw/i386/intel_iommu.c +1
-0HostIOMMUDevice *hiod, Error **errp) {IntelIOMMUState *s = opaque; +VTDHostIOMMUDevice *vtd_hiod;struct vtd_as_key key = { .bus = bus, .devfn = devfn,@@ -4413,6 +4417,12 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,void *opaque, int devfn,
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn, · line 4426
Quoted 10 lines
hw/i386/intel_iommu.c +6
-0return 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)) {
Quoted 1 line
Shouldn't we free
vtd_hiodhere?
Good catch, Thanks.
I put g_free in patch10, but it should be in this patch, will fix.
BRs,
Zhenzhong
Eric Auger
Reviewed-byRe: [PATCH v1 04/15] intel_iommu: Introduce a new structure VTDHostIOMMUDevice
Quoted 100 lines · Zhenzhong
Hi Clement,
Quoted 98 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/15]intel_iommu: Introduce a new structure VTDHostIOMMUDeviceHi,
Quoted 1 line · Zhenzhong Duan
Caution: External email. Do not open attachments or click links, unless this
email comes from a known sender and you know the content is safe.
hw/i386/intel_iommu.c static gboolean vtd_hiod_equal(gconstpointer v1,
Quoted 56 lines
Introduce 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.
Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 2 +- hw/i386/intel_iommu.c | 14 ++++++++++++-- 3 files changed, 20 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 +0
-0@@ -281,7 +281,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1,gconstpointer v2)
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,
Quoted 11 lines
hw/i386/intel_iommu.c +4
-1static 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,@@ -4397,6 +4400,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,void *opaque, int devfn,
hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,
Quoted 8 lines
hw/i386/intel_iommu.c +1
-0HostIOMMUDevice *hiod, Error **errp) {IntelIOMMUState *s = opaque; +VTDHostIOMMUDevice *vtd_hiod;struct vtd_as_key key = { .bus = bus, .devfn = devfn,@@ -4413,6 +4417,12 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,void *opaque, int devfn,
Quoted 10 lines
hw/i386/intel_iommu.c +6
-0return 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)) {
Quoted 3 lines
Quoted 1 line
Shouldn't we free
vtd_hiodhere?Good catch, Thanks.
I putg_freein patch10, but it should be in this patch, will fix.
With this fixed,
Eric
Quoted 4 lines
BRs,
Zhenzhong
PATCH 5/15 intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
In reply to Zhenzhong DuanWe already have vtd_find_add_as() to find an AS from BDF+pasid, but this pasid is passed from PCI subsystem. PCI 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 | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)
hw/i386/intel_iommu.c +50 -0
@@ -1617,6 +1617,56 @@ static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) } } +static inline int vtd_as_to_iommu_pasid_locked (VTDAddressSpace *vtd_as, +uint32_t *pasid) +{ +VTDContextEntry ce; +int ret; + + ret =vtd_as_to_context_entry (vtd_as, &ce); +if (ret) { +return ret; + } + +/* Translate to iommu pasid if PCI_NO_PASID */ +if (vtd_as->pasid ==PCI_NO_PASID ) { + *pasid =VTD_CE_GET_RID2PASID (&ce); + }else { + *pasid = vtd_as->pasid; + } + +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.34.1
Re: [PATCH v1 05/15] intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
Quoted 5 lines · Zhenzhong Duan
We already have
vtd_find_add_as()to find an AS from BDF+pasid, but this pasid is passed from PCI subsystem. PCI device supports two request types, Requests-without-PASID and Requests-with-PASID. Requests-without-PASID doesn't include a PASID TLP prefix, IOMMU fetchesrid_pasidfrom context entry and use it as IOMMU's pasid to index pasid table.
When reading the first line, it makes me thinking why need the helpers since there is already a helper to find. The key is the later part. We need to translate the PCI_NO_PASID to ridpasid.
Quoted 75 lines
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 tovtd_aswhich contains PCI's pasidvtd_as->pasid.
vtd_as_to_iommu_pasid_locked()translates from BDF+vtd_as->pasid toiommu_pasid.Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)hw/i386/intel_iommu.c +50
-0@@ -1617,6 +1617,56 @@ static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) } } +static inline int vtd_as_to_iommu_pasid_locked (VTDAddressSpace *vtd_as, +uint32_t *pasid) +{ +VTDContextEntry ce; +int ret; + + ret =vtd_as_to_context_entry (vtd_as, &ce); +if (ret) { +return ret; + } + +/* Translate to iommu pasid if PCI_NO_PASID */ +if (vtd_as->pasid ==PCI_NO_PASID ) { + *pasid =VTD_CE_GET_RID2PASID (&ce); + }else { + *pasid = vtd_as->pasid; + } + +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 · 2 lines
Regards, Yi Liu
RE: [PATCH v1 05/15] intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
Quoted 14 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 05/15]intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_lockedQuoted 5 lines · Zhenzhong Duan
We already have
vtd_find_add_as()to find an AS from BDF+pasid, but this pasid is passed from PCI subsystem. PCI device supports two request types, Requests-without-PASID and Requests-with-PASID. Requests-without-PASID doesn't include a PASID TLP prefix, IOMMU fetchesrid_pasidfrom context entry and use it as IOMMU's pasid to index pasid table.
Quoted 3 lines
When reading the first line, it makes me thinking why need the helpers since there is already a helper to find. The key is the later part. We need to translate the
PCI_NO_PASIDto ridpasid.
OK, presume you want me to delete first line.
Let me know if you mean not.
Thanks
Zhenzhong
Quoted 85 lines
Quoted 6 lines
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_asQuoted 3 lines
which contains PCI's pasid
vtd_as->pasid.
vtd_as_to_iommu_pasid_locked()translates from BDF+vtd_as->pasid to
iommu_pasid.Quoted 4 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu.c | 50+++++++++++++++++++++++++++++++++++++++++++
hw/i386/intel_iommu.c static int
Quoted 7 lines
1 file changed, 50 insertions(+)hw/i386/intel_iommu.c +0
-0@@ -1617,6 +1617,56 @@ static int
vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)Quoted 15 lines
hw/i386/intel_iommu.c +12
-0} } +static inline int vtd_as_to_iommu_pasid_locked (VTDAddressSpace *vtd_as, +uint32_t *pasid) +{ +VTDContextEntry ce; +int ret; + + ret =vtd_as_to_context_entry (vtd_as, &ce); +if (ret) { +return ret; + } + +/* Translate to iommu pasid if PCI_NO_PASID */ hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+if (vtd_as->pasid ==PCI_NO_PASID ) { + *pasid =VTD_CE_GET_RID2PASID (&ce); + }else { + *pasid = vtd_as->pasid; + } + +return 0 ; +} + +static gboolean vtd_find_as_by_sid_and_iommu_pasid(gpointer key, gpointervalue,
Quoted 21 lines
hw/i386/intel_iommu.c +21
-0+ 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,hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1672
Quoted 10 lines
hw/i386/intel_iommu.c +7
-0+ .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) {--
Regards,
Yi Liu
Re: [PATCH v1 05/15] intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_locked
Quoted 14 lines · Zhenzhong
Quoted 14 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 05/15]intel_iommu: Introduce two helpers vtd_as_from/to_iommu_pasid_lockedQuoted 5 lines · Zhenzhong Duan
We already have
vtd_find_add_as()to find an AS from BDF+pasid, but this pasid is passed from PCI subsystem. PCI device supports two request types, Requests-without-PASID and Requests-with-PASID. Requests-without-PASID doesn't include a PASID TLP prefix, IOMMU fetchesrid_pasidfrom context entry and use it as IOMMU's pasid to index pasid table.
Quoted 7 lines
Quoted 3 lines
When reading the first line, it makes me thinking why need the helpers since there is already a helper to find. The key is the later part. We need to translate the
PCI_NO_PASIDto ridpasid.OK, presume you want me to delete first line.
Let me know if you mean not.
yes. :)
Regards,
Yi Liu
PATCH 6/15 intel_iommu: Handle PASID entry removing and updating
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 for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID tagged DMA translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.
When guest modifies a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, allocate or remove a VTDAddressSpace instance per the invalidation reasons:
a) a present pasid entry moved to non-present
b) a present pasid entry to be a present entry
c) a non-present pasid entry moved to present
This handles a) and b), following patch will handle c).
vIOMMU emulator could figure out the reason by fetching latest guest pasid entry and compare it with the PASID cache.
--- hw/i386/intel_iommu_internal.h | 26 ++++ include/hw/i386/intel_iommu.h | 6 + hw/i386/intel_iommu.c | 252 +++++++++++++++++++++++++++++++-- hw/i386/trace-events | 3 + 4 files changed, 277 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +26 -0
@@ -315,6 +315,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 */
@@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL +#define VTD_INV_DESC_PASIDC_G (3ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID (val) (((val) >> 32) & 0xfffffULL) +#define VTD_INV_DESC_PASIDC_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_PASIDC_RSVD_VAL0 0xfff000000000f1c0ULL + +#define VTD_INV_DESC_PASIDC_DSI (0ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID_SI (1ULL << 4) +#define VTD_INV_DESC_PASIDC_GLOBAL (3ULL << 4) +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;
@@ -552,6 +562,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 { +/* pasid cache invalidation rely on guest PASID entry */ +VTD_PASID_CACHE_GLOBAL_INV ,/* pasid cache global invalidation */ +VTD_PASID_CACHE_DOMSI ,/* pasid cache domain selective invalidation */ +VTD_PASID_CACHE_PASIDSI ,/* pasid cache pasid selective invalidation */ +}VTDPCInvType ; + +typedef struct VTDPASIDCacheInfo { +VTDPCInvType type; +uint16_t domain_id; +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)
@@ -563,6 +588,7 @@ typedef struct VTDRootEntry VTDRootEntry;#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
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 cache_filled; +}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 +242 -10
276 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.34.1
Re: [PATCH v1 06/15] intel_iommu: Handle PASID entry removing and updating
Quoted 16 lines · Zhenzhong Duan
This 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_PASIDis allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID tagged DMA translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.When guest modifies a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, allocate or remove a VTDAddressSpace instance per the invalidation reasons:
Quoted 8 lines
a) a present pasid entry moved to non-present
b) a present pasid entry to be a present entry
c) a non-present pasid entry moved to presentThis handles a) and b), following patch will handle c).
vIOMMU emulator could figure out the reason by fetching latest guest pasid entry and compare it with the PASID cache.
To aovid confusion, maybe better to say cached pasid entry. :)
Quoted 151 lines
Signed-off-by Yi Liu <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 26 ++++ include/hw/i386/intel_iommu.h | 6 + hw/i386/intel_iommu.c | 252 +++++++++++++++++++++++++++++++-- hw/i386/trace-events | 3 + 4 files changed, 277 insertions(+), 10 deletions(-)hw/i386/intel_iommu_internal.h +26
-0@@ -315,6 +315,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 */ @@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL +#define VTD_INV_DESC_PASIDC_G (3ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID (val) (((val) >> 32) & 0xfffffULL) +#define VTD_INV_DESC_PASIDC_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_PASIDC_RSVD_VAL0 0xfff000000000f1c0ULL + +#define VTD_INV_DESC_PASIDC_DSI (0ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID_SI (1ULL << 4) +#define VTD_INV_DESC_PASIDC_GLOBAL (3ULL << 4) +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;@@ -552,6 +562,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 { +/* pasid cache invalidation rely on guest PASID entry */ +VTD_PASID_CACHE_GLOBAL_INV ,/* pasid cache global invalidation */ +VTD_PASID_CACHE_DOMSI ,/* pasid cache domain selective invalidation */ +VTD_PASID_CACHE_PASIDSI ,/* pasid cache pasid selective invalidation */ +}VTDPCInvType ; + +typedef struct VTDPASIDCacheInfo { +VTDPCInvType type; +uint16_t domain_id; +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)@@ -563,6 +588,7 @@ typedef struct VTDRootEntry VTDRootEntry;#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 1ULLinclude/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 cache_filled; +}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 +43
-0@@ -825,6 +825,11 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) } } +static inline uint16_t vtd_pe_get_did (VTDPASIDEntry *pe) +{ +return VTD_SM_PASID_ENTRY_DID ((pe)->val[1 ]); +} +static inline bool vtd_pdire_present (VTDPASIDDirEntry *pdire) {return pdire->val &1 ;@@ -3104,6 +3109,236 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,return true; } +static inline int vtd_dev_get_pe_from_pasid (VTDAddressSpace *vtd_as, +uint32_t pasid,VTDPASIDEntry *pe) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +VTDContextEntry ce; +int ret; + +if (!s->root_scalable) { +return -VTD_FR_RTADDR_INV_TTM ; + } + + ret =vtd_as_to_context_entry (vtd_as, &ce); +if (ret) { +return ret; + } + +return vtd_ce_get_pasid_entry (s, &ce, pe, pasid); +} + +static bool vtd_pasid_entry_compare (VTDPASIDEntry *p1,VTDPASIDEntry *p2) +{ +return !memcmp (p1, p2,sizeof (*p1)); +} + +/* +* This function fills in the pasid entry in &vtd_as. Caller +* of this function should hold iommu_lock. +*/ +static void vtd_fill_pe_in_cache (IntelIOMMUState *s,VTDAddressSpace *vtd_as, +VTDPASIDEntry *pe) +{ +VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; + +if (vtd_pasid_entry_compare (pe, &pc_entry->pasid_entry)) { +/* No need to go further as cached pasid entry is latest */ +return ; + } +
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3159
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+ pc_entry->pasid_entry = *pe; + pc_entry->cache_filled = true; +/* +* TODO: send pasid bind to host for passthru devices +*/ +} + +/* +* This function is used to clear cached pasid entry in vtd_as +* instances. Caller of this function should hold iommu_lock.
it also covers pasid entry update.
Quoted 4 lines
hw/i386/intel_iommu.c +4
-0+*/ +static gboolean vtd_flush_pasid (gpointer key,gpointer value, +gpointer user_data) +{
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3173
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+VTDPASIDCacheInfo *pc_info = user_data; +VTDAddressSpace *vtd_as = value; +IntelIOMMUState *s = vtd_as->iommu_state; +VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; +VTDPASIDEntry pe; +uint16_t did; +uint32_t pasid; +int ret; + +/* Replay only filled pasid entry cache for passthrough device */
the comment of this helper already implies only continue if the pc_entry->cache_filled is true. Also, replay is a concept in the upper level helpers, no need to mention it here. I noticed replay in low level helpers in other patch as well, please drop them as well. :)
Quoted 47 lines
hw/i386/intel_iommu.c +47
-0+if (!pc_entry->cache_filled) { +return false; + } + did =vtd_pe_get_did (&pc_entry->pasid_entry); + +if (vtd_as_to_iommu_pasid_locked (vtd_as, &pasid)) { + goto remove; + } + +switch (pc_info->type) { +case VTD_PASID_CACHE_PASIDSI : +if (pc_info->pasid != pasid) { +return false; + } +/* Fall through */ +case VTD_PASID_CACHE_DOMSI : +if (pc_info->domain_id != did) { +return false; + } +/* Fall through */ +case VTD_PASID_CACHE_GLOBAL_INV : +break ; +default : +error_report ("invalid pc_info->type" ); +abort (); + } + +/* +* pasid cache invalidation may indicate a present pasid +* entry to present pasid entry modification. To cover such +* case, vIOMMU emulator needs to fetch latest guest pasid +* entry and check cached pasid entry, then update pasid +* cache and send pasid bind/unbind to host properly. +*/ + ret =vtd_dev_get_pe_from_pasid (vtd_as, pasid, &pe); +if (ret) { +/* +* No valid pasid entry in guest memory. e.g. pasid entry +* was modified to be either all-zero or non-present. Either +* case means existing pasid cache should be removed. +*/ + goto remove; + } + +vtd_fill_pe_in_cache (s, vtd_as, &pe); +return false; +
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3230
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+remove: +/* +* TODO: send pasid unbind to host for passthru devices +*/ + pc_entry->cache_filled = false; + +/* +* Don't remove address space of PCI_NO_PASID which is created by PCI +* sub-system. +*/
I get why it cannot be removed. But I think the ce and pe field of this vtd_as might need to be updated given it is supposed to be removed if it is not PCI_NO_PASID.
Quoted 50 lines
hw/i386/intel_iommu.c +50
-0+if (vtd_as->pasid ==PCI_NO_PASID ) { +return false; + } +return true; +} + +/* +* This function syncs the pasid bindings between guest and host. +* It includes updating the pasid cache in vIOMMU and updating the +* pasid bindings per guest's latest pasid entry presence. +*/ +static void vtd_pasid_cache_sync (IntelIOMMUState *s, +VTDPASIDCacheInfo *pc_info) +{ +if (!s->flts || !s->root_scalable || !s->dmar_enabled) { +return ; + } + +/* +* Regards to a pasid cache invalidation, e.g. a PSI. +* it could be either cases of below: +* a) a present pasid entry moved to non-present +* b) a present pasid entry to be a present entry +* c) a non-present pasid entry moved to present +* +* Different invalidation granularity may affect different device +* scope and pasid scope. But for each invalidation granularity, +* it needs to do two steps to sync host and guest pasid binding. +* +* Here is the handling of a PSI: +* 1) loop all the existing vtd_as instances to update them +* according to the latest guest pasid entry in pasid table. +* this will make sure affected existing vtd_as instances +* cached the latest pasid entries. Also, during the loop, the +* host should be notified if needed. e.g. pasid unbind or pasid +* update. Should be able to cover case a) and case b). +* +* 2) loop all devices to cover case c) +* - For devices which are backed by HostIOMMUDeviceIOMMUFD instances, +* we loop them and check if guest pasid entry exists. If yes, +* it is case c), we update the pasid cache and also notify +* host. +* - For devices which are not backed by HostIOMMUDeviceIOMMUFD, +* it is not necessary to create pasid cache at this phase since +* it could be created when vIOMMU does DMA address translation. +* This is not yet implemented since there is no emulated +* pasid-capable devices today. If we have such devices in +* future, the pasid cache shall be created there. +* Other granularity follow the same steps, just with different scope +*
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3290
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+*/ + +vtd_iommu_lock (s); +/* +* Step 1: loop all the existing vtd_as instances for pasid unbind and +* update. +*/ +g_hash_table_foreach_remove (s->vtd_address_spaces, vtd_flush_pasid, + pc_info); +vtd_iommu_unlock (s);
just realized s->vtd_address_spaces is not protected by iommu_lock. If so, will it be ok to drop the lock here and add it in the lower level helpers if lock is needed?
Regards,
Yi Liu
RE: [PATCH v1 06/15] intel_iommu: Handle PASID entry removing and updating
Quoted 31 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 06/15]intel_iommu: Handle PASID entry removing and updatingQuoted 8 lines · Zhenzhong Duan
This 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_PASIDis allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID taggedDMA
Quoted 5 lines
translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.
When guest modifies a PASID entry, QEMU will capture the guest pasid
selective
Quoted 1 line
pasid cache invalidation, allocate or remove a VTDAddressSpace instance per
the
Quoted 6 lines
invalidation reasons:
a) a present pasid entry moved to non-present
b) a present pasid entry to be a present entry
c) a non-present pasid entry moved to present
Quoted 7 lines
Quoted 3 lines
This handles a) and b), following patch will handle c).
vIOMMU emulator could figure out the reason by fetching latest guest pasid
entry
Quoted 1 line
and compare it with the PASID cache.
To aovid confusion, maybe better to say cached pasid entry. :)
Sure.
Quoted 164 lines
Quoted 25 lines
Signed-off-by Yi Liu <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_internal.h | 26 ++++ include/hw/i386/intel_iommu.h | 6 + hw/i386/intel_iommu.c | 252 +++++++++++++++++++++++++++++++-- hw/i386/trace-events | 3 + 4 files changed, 277 insertions(+), 10 deletions(-)hw/i386/intel_iommu_internal.h +1
-0@@ -315,6 +315,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 */ hw/i386/intel_iommu_internal.h typedef union VTDInvDesc VTDInvDesc; · line 498
Quoted 6 lines
hw/i386/intel_iommu_internal.h +3
-0@@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000f1c0ULL#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL +#define VTD_INV_DESC_PASIDC_G (3ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID (val) (((val) >> 32) & 0xfffffULL) +#define VTD_INV_DESC_PASIDC_DID (val) (((val) >> 16) &
VTD_DOMAIN_ID_MASK)hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry; · line 562
Quoted 11 lines
hw/i386/intel_iommu_internal.h +6
-0+#define VTD_INV_DESC_PASIDC_RSVD_VAL0 0xfff000000000f1c0ULL + +#define VTD_INV_DESC_PASIDC_DSI (0ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID_SI (1ULL << 4) +#define VTD_INV_DESC_PASIDC_GLOBAL (3ULL << 4) +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;@@ -552,6 +562,21 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0 (aw) (0x1e0ULL |~VTD_HAW_MASK(aw))
hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry;
Quoted 6 lines
hw/i386/intel_iommu_internal.h +4
-0#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL +typedef enum VTDPCInvType { + /* pasid cache invalidation rely on guest PASID entry */ + VTD_PASID_CACHE_GLOBAL_INV, /* pasid cache global invalidation */ + VTD_PASID_CACHE_DOMSI, /* pasid cache domain selective invalidation*/
Quoted 15 lines
hw/i386/intel_iommu_internal.h +11
-0+VTD_PASID_CACHE_PASIDSI ,/* pasid cache pasid selective invalidation */ +} VTDPCInvType; + +typedef struct VTDPASIDCacheInfo { +VTDPCInvType type; +uint16_t domain_id; +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)hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry; · line 589
Quoted 2 lines
hw/i386/intel_iommu_internal.h +0
-0@@ -563,6 +588,7 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_PASID_TABLE_BITS_MASK (0x3fULL)#define VTD_PASID_TABLE_INDEX (pasid) ((pasid) &
VTD_PASID_TABLE_BITS_MASK)hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry; · line 590
Quoted 1 line
hw/i386/intel_iommu_internal.h +0
-0#define VTD_PASID_ENTRY_FPD (1ULL << 1)/* Fault Processing Disable */
hw/i386/intel_iommu.c static inline bool
Quoted 33 lines
hw/i386/intel_iommu_internal.h +1
-0+#define VTD_PASID_TBL_ENTRY_NUM (1ULL << 6)/* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULLinclude/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 cache_filled; +}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 +0
-0@@ -825,6 +825,11 @@ static inline bool
vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)hw/i386/intel_iommu.c static bool
Quoted 12 lines
hw/i386/intel_iommu.c +5
-0} } +static inline uint16_t vtd_pe_get_did (VTDPASIDEntry *pe) +{ +return VTD_SM_PASID_ENTRY_DID ((pe)->val[1 ]); +} +static inline bool vtd_pdire_present (VTDPASIDDirEntry *pdire) {return pdire->val &1 ;@@ -3104,6 +3109,236 @@ static boolvtd_process_piotlb_desc(IntelIOMMUState *s,
Quoted 22 lines
hw/i386/intel_iommu.c +19
-0return true; } +static inline int vtd_dev_get_pe_from_pasid (VTDAddressSpace *vtd_as, +uint32_t pasid,VTDPASIDEntry *pe) +{ +IntelIOMMUState *s = vtd_as->iommu_state; +VTDContextEntry ce; +int ret; + +if (!s->root_scalable) { +return -VTD_FR_RTADDR_INV_TTM ; + } + + ret =vtd_as_to_context_entry (vtd_as, &ce); +if (ret) { +return ret; + } + +return vtd_ce_get_pasid_entry (s, &ce, pe, pasid); +} +hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+static bool vtd_pasid_entry_compare (VTDPASIDEntry *p1,VTDPASIDEntry *p2) +{ +return !memcmp (p1, p2,sizeof (*p1)); +} + +/* +* This function fills in the pasid entry in &vtd_as. Caller +* of this function should hold iommu_lock. +*/ +static void vtd_fill_pe_in_cache(IntelIOMMUState *s, VTDAddressSpace
*vtd_as,Quoted 13 lines
hw/i386/intel_iommu.c +13
-0+ VTDPASIDEntry *pe) +{ +VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; + +if (vtd_pasid_entry_compare (pe, &pc_entry->pasid_entry)) { +/* No need to go further as cached pasid entry is latest */ +return ; + } + + pc_entry->pasid_entry = *pe; + pc_entry->cache_filled = true; +/* +* TODO: send pasid bind to host for passthru devices
Quoted 8 lines
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3159
Quoted 6 lines
hw/i386/intel_iommu.c +6
-0+*/ +} + +/* +* This function is used to clear cached pasid entry in vtd_as +* instances. Caller of this function should hold iommu_lock. it also covers pasid entry update.
Will do like:
* This function is used to update or clear cached pasid entry in vtd_as
Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +11
-0+*/ +static gboolean vtd_flush_pasid (gpointer key,gpointer value, +gpointer user_data) +{ +VTDPASIDCacheInfo *pc_info = user_data; +VTDAddressSpace *vtd_as = value; +IntelIOMMUState *s = vtd_as->iommu_state; +VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry; +VTDPASIDEntry pe; +uint16_t did; +uint32_t pasid;
Quoted 8 lines
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3173
Quoted 3 lines
hw/i386/intel_iommu.c +3
-0+int ret; + +/* Replay only filled pasid entry cache for passthrough device */ the comment of this helper already implies only continue if the
pc_entry->cache_filledis true. Also, replay is a concept in the upper level helpers, no need to mention it here. I noticed replay in low level helpers in other patch as well, please drop them as well. :)
Will do.
Quoted 54 lines
Quoted 54 lines
hw/i386/intel_iommu.c +53
-0+if (!pc_entry->cache_filled) { +return false; + } + did =vtd_pe_get_did (&pc_entry->pasid_entry); + +if (vtd_as_to_iommu_pasid_locked (vtd_as, &pasid)) { + goto remove; + } + +switch (pc_info->type) { +case VTD_PASID_CACHE_PASIDSI : +if (pc_info->pasid != pasid) { +return false; + } +/* Fall through */ +case VTD_PASID_CACHE_DOMSI : +if (pc_info->domain_id != did) { +return false; + } +/* Fall through */ +case VTD_PASID_CACHE_GLOBAL_INV : +break ; +default : +error_report ("invalid pc_info->type" ); +abort (); + } + +/* +* pasid cache invalidation may indicate a present pasid +* entry to present pasid entry modification. To cover such +* case, vIOMMU emulator needs to fetch latest guest pasid +* entry and check cached pasid entry, then update pasid +* cache and send pasid bind/unbind to host properly. +*/ + ret =vtd_dev_get_pe_from_pasid (vtd_as, pasid, &pe); +if (ret) { +/* +* No valid pasid entry in guest memory. e.g. pasid entry +* was modified to be either all-zero or non-present. Either +* case means existing pasid cache should be removed. +*/ + goto remove; + } + +vtd_fill_pe_in_cache (s, vtd_as, &pe); +return false; + +remove: +/* +* TODO: send pasid unbind to host for passthru devices +*/ + pc_entry->cache_filled = false; +
Quoted 8 lines
hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3230
Quoted 4 lines
hw/i386/intel_iommu.c +4
-0+/* +* Don't remove address space of PCI_NO_PASID which is created by PCI +* sub-system. +*/ I get why it cannot be removed. But I think the ce and pe field of this
vtd_asmight need to be updated given it is supposed to be removed if it is notPCI_NO_PASID.
With patch2 dropped, we always call vtd_dev_to_context_entry() to get ce, vtd_as->ce only take effect for emulated device.
About pe, when we jump to remove label, that means pe.present == false, We record that through pc_entry->cache_filled = false, so updating unpresented pe field looks no need?
Quoted 63 lines
Quoted 30 lines
hw/i386/intel_iommu.c +29
-0+if (vtd_as->pasid ==PCI_NO_PASID ) { +return false; + } +return true; +} + +/* +* This function syncs the pasid bindings between guest and host. +* It includes updating the pasid cache in vIOMMU and updating the +* pasid bindings per guest's latest pasid entry presence. +*/ +static void vtd_pasid_cache_sync (IntelIOMMUState *s, +VTDPASIDCacheInfo *pc_info) +{ +if (!s->flts || !s->root_scalable || !s->dmar_enabled) { +return ; + } + +/* +* Regards to a pasid cache invalidation, e.g. a PSI. +* it could be either cases of below: +* a) a present pasid entry moved to non-present +* b) a present pasid entry to be a present entry +* c) a non-present pasid entry moved to present +* +* Different invalidation granularity may affect different device +* scope and pasid scope. But for each invalidation granularity, +* it needs to do two steps to sync host and guest pasid binding. +* hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+* Here is the handling of a PSI: +* 1) loop all the existing vtd_as instances to update them +* according to the latest guest pasid entry in pasid table. +* this will make sure affected existing vtd_as instances +* cached the latest pasid entries. Also, during the loop, the +* host should be notified if needed. e.g. pasid unbind or pasid +* update. Should be able to cover case a) and case b). +* +* 2) loop all devices to cover case c) +* - For devices which are backed by HostIOMMUDeviceIOMMUFD instances,
Quoted 11 lines
hw/i386/intel_iommu.c +11
-0+* we loop them and check if guest pasid entry exists. If yes, +* it is case c), we update the pasid cache and also notify +* host. +* - For devices which are not backed by HostIOMMUDeviceIOMMUFD, +* it is not necessary to create pasid cache at this phase since +* it could be created when vIOMMU does DMA address translation. +* This is not yet implemented since there is no emulated +* pasid-capable devices today. If we have such devices in +* future, the pasid cache shall be created there. +* Other granularity follow the same steps, just with different scope +* hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3290
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+*/ + +vtd_iommu_lock (s); +/* +* Step 1: loop all the existing vtd_as instances for pasid unbind and +* update. +*/ +g_hash_table_foreach_remove (s->vtd_address_spaces, vtd_flush_pasid, + pc_info); +vtd_iommu_unlock (s);
Quoted 3 lines
just realized
s->vtd_address_spacesis not protected byiommu_lock. If so, will it be ok to drop the lock here and add it in the lower level helpers if lock is needed?
Could you elaborate which lower level helper to add iommu_lock? We want to protect update to vtd_address_spaces list with iommu_lock here.
Thanks
Zhenzhong
PATCH 7/15 intel_iommu: Handle PASID entry adding
In reply to Zhenzhong DuanWhen guest modifies a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, allocate or remove a VTDAddressSpace instance per the invalidation reasons:
a) a present pasid entry moved to non-present
b) a present pasid entry to be a present entry
c) a non-present pasid entry moved to present
This handles c).
--- hw/i386/intel_iommu_internal.h | 1 + hw/i386/intel_iommu.c | 167 ++++++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -0
@@ -558,6 +558,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 (val) (((val) >> 9) & 0x7)#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
hw/i386/intel_iommu.c +166 -1
185 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 8/15 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 | 2 ++ hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 3 files changed, 31 insertions(+)
hw/i386/intel_iommu_internal.h +2 -0
@@ -564,6 +564,8 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL typedef enum VTDPCInvType { + /* Force reset all */ + VTD_PASID_CACHE_FORCE_RESET = 0, /* pasid cache invalidation rely on guest PASID entry */ VTD_PASID_CACHE_GLOBAL_INV, /* pasid cache global invalidation */ VTD_PASID_CACHE_DOMSI, /* pasid cache domain selective invalidation */
hw/i386/intel_iommu.c +28 -0
@@ -86,6 +86,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 "
@@ -390,6 +392,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); }
@@ -3186,6 +3189,8 @@ static gboolean vtd_flush_pasid(gpointer key, gpointer value, }switch (pc_info->type) { +case VTD_PASID_CACHE_FORCE_RESET : + goto remove;case VTD_PASID_CACHE_PASIDSI :if (pc_info->pasid != pasid) {return false;
@@ -3239,6 +3244,26 @@ remove:return true; } +/* Caller of this function should hold iommu_lock */ +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 the vtd_as instances. Also, as a big hammer, use +* VTD_PASID_CACHE_FORCE_RESET to ensure all the vtd_as instances are +* dropped, meanwhile the change will be passed to host if +* HostIOMMUDeviceIOMMUFD is available. +*/ +g_hash_table_foreach_remove (s->vtd_address_spaces, + vtd_flush_pasid, &pc_info); +} + static void vtd_sm_pasid_table_walk_one(IntelIOMMUState *s, dma_addr_t pt_base, int start,
@@ -3366,6 +3391,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_report ("invalid pc_info->type for replay" );abort ();
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.34.1
PATCH 9/15 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 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), the hwpt on host side is a nested page table.
The guest page table is configured as stage-1 page table (gIOVA->GPA) whose translation result would further go through host VT-d stage-2 page table(GPA->HPA) under nested translation mode. This is the key to support gIOVA over stage-1 page table for Intel VT-d in virtualization environment.
Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.
See below example diagram:
IntelIOMMUState
|
V
.------------------. .------------------.
| VTDIOASContainer |--->| VTDIOASContainer |--->...
| (iommufd0) | | (iommufd1) |
.------------------. .------------------.
| |
| .-->...
V
.-------------------. .-------------------.
| VTDS2Hwpt(CC) |--->| VTDS2Hwpt(non-CC) |-->...
.-------------------. .-------------------.
| | |
| | |
.-----------. .-----------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device |
| (iommufd0)| | (iommufd0)| | (non-CC) |
| | | | | (iommufd0) |
.-----------. .-----------. .------------.--- hw/i386/intel_iommu_internal.h | 11 + include/hw/i386/intel_iommu.h | 24 ++ hw/i386/intel_iommu.c | 581 +++++++++++++++++++++++++++++++-- hw/i386/trace-events | 8 + 4 files changed, 604 insertions(+), 20 deletions(-)
hw/i386/intel_iommu_internal.h +11 -0
@@ -563,6 +563,13 @@ 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 , +VTD_OP_NUM +}VTDPASIDOp ; + typedef enum VTDPCInvType { /* Force reset all */ VTD_PASID_CACHE_FORCE_RESET = 0,
@@ -578,6 +585,7 @@ typedef struct VTDPASIDCacheInfo {uint32_t pasid;PCIBus *bus;uint16_t devfn; +bool error_happened; } VTDPASIDCacheInfo;/* PASID Table Related Definitions */
@@ -606,6 +614,9 @@ typedef struct VTDPASIDCacheInfo {#define VTD_SM_PASID_ENTRY_FLPM 3ULL#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_SRE_BIT (val) (!!((val) & 1ULL)) +#define VTD_SM_PASID_ENTRY_WPE_BIT (val) (!!(((val) >> 4) & 1ULL)) +#define VTD_SM_PASID_ENTRY_EAFE_BIT (val) (!!(((val) >> 7) & 1ULL))/* First Level Paging Structure */ /* Masks for First Level Paging Entry */
include/hw/i386/intel_iommu.h +24 -0
@@ -100,10 +100,32 @@ typedef struct VTDPASIDCacheEntry {bool cache_filled; } VTDPASIDCacheEntry; +typedef struct VTDIOASContainer { +struct IOMMUFDBackend *iommufd; +uint32_t ioas_id; +MemoryListener listener; + QLIST_HEAD(, VTDS2Hwpt) s2_hwpt_list; +QLIST_ENTRY (VTDIOASContainer) next; +Error *error; +}VTDIOASContainer ; + +typedef struct VTDS2Hwpt { +uint32_t users; +uint32_t hwpt_id; +VTDIOASContainer *container; +QLIST_ENTRY (VTDS2Hwpt) next; +}VTDS2Hwpt ; + +typedef struct VTDHwpt { +uint32_t hwpt_id; +VTDS2Hwpt *s2_hwpt; +}VTDHwpt ; +struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;uint32_t pasid; +VTDHwpt hwpt;AddressSpace as;IOMMUMemoryRegion iommu;MemoryRegion root;/* The root container of the device */
@@ -303,6 +325,8 @@ struct IntelIOMMUState {GHashTable *vtd_host_iommu_dev;/* VTDHostIOMMUDevice */ + QLIST_HEAD(, VTDIOASContainer) containers; +/* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */ dma_addr_t intr_root;/* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +561 -20
668 lines not on this page · raw message
hw/i386/trace-events +8 -0
@@ -73,6 +73,14 @@ 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" +vtd_device_fail_attach_existing_hwpt(const char *msg) " %s" +vtd_device_attach_container(int fd, uint32_t dev_id, uint32_t pasid, uint32_t ioas_id, uint32_t hwpt_id) "iommufd %d dev_id %d pasid %d ioas_id %d hwpt_id %d" +vtd_device_detach_container(int fd, uint32_t dev_id, uint32_t pasid) "iommufd %d dev_id %d pasid %d" +vtd_device_fail_attach_existing_container(const char *msg) " %s" +vtd_device_alloc_ioas(int fd, uint32_t ioas_id) "iommufd %d ioas_id %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.34.1
PATCH 10/15 intel_iommu: ERRATA_772415 workaround
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.
We utilize the new added IOMMUFD container/ioas/hwpt management framework in VTD. Add a check to create new VTDIOASContainer to only hold RW mappings, then this VTDIOASContainer can be used as backend for device with ERRATA_772415. See below diagram for details:
IntelIOMMUState
|
V
.------------------. .------------------. .-------------------.
| VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer |-->...
| (iommufd0,RW&RO) | | (iommufd1,RW&RO) | | (iommufd0,only RW)|
.------------------. .------------------. .-------------------.
| | |
| .-->... |
V V
.-------------------. .-------------------. .---------------.
| VTDS2Hwpt(CC) |--->| VTDS2Hwpt(non-CC) |-->... | VTDS2Hwpt(CC) |-->...
.-------------------. .-------------------. .---------------.
| | | |
| | | |
.-----------. .-----------. .------------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device | | Device(CC) |
| (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) |
| | | | | (iommufd0) | | (iommufd0) |
.-----------. .-----------. .------------. .------------.Changed to pass VTDHostIOMMUDevice pointer to vtd_check_hdev() so errata could be saved.
--- hw/i386/intel_iommu_internal.h | 1 + include/hw/i386/intel_iommu.h | 1 + hw/i386/intel_iommu.c | 22 +++++++++++++++------- 3 files changed, 17 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
@@ -654,5 +654,6 @@ typedef struct VTDHostIOMMUDevice {PCIBus *bus;uint8_t devfn;HostIOMMUDevice *hiod; + uint32_t errata; } VTDHostIOMMUDevice;#endif
include/hw/i386/intel_iommu.h +1 -0
@@ -103,6 +103,7 @@ typedef struct VTDPASIDCacheEntry { typedef struct VTDIOASContainer { struct IOMMUFDBackend *iommufd; uint32_t ioas_id; + uint32_t errata; MemoryListener listener; QLIST_HEAD(, VTDS2Hwpt) s2_hwpt_list; QLIST_ENTRY(VTDIOASContainer) next;
hw/i386/intel_iommu.c +15 -7
@@ -2437,7 +2437,8 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s) }#ifdef CONFIG_IOMMUFD -static bool iommufd_listener_skipped_section (MemoryRegionSection *section) +static bool iommufd_listener_skipped_section (VTDIOASContainer *container, +MemoryRegionSection *section) {return !memory_region_is_ram (section->mr) || memory_region_is_protected(section->mr) ||
@@ -2447,7 +2448,8 @@ static bool iommufd_listener_skipped_section(MemoryRegionSection *section)* are never accessed by the CPU and beyond the address width of * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width. */ - section->offset_within_address_space & (1ULL <<63 ); + section->offset_within_address_space & (1ULL <<63 ) || + (container->errata && section->readonly); } static void iommufd_listener_region_add_s2domain(MemoryListener *listener,
@@ -2463,7 +2465,7 @@ static void iommufd_listener_region_add_s2domain(MemoryListener *listener,Error *err =NULL ;int ret; -if (iommufd_listener_skipped_section (section)) { +if (iommufd_listener_skipped_section (container, section)) {return ; } iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space);
@@ -2514,7 +2516,7 @@ static void iommufd_listener_region_del_s2domain(MemoryListener *listener,Int128 llend, llsize;int ret; -if (iommufd_listener_skipped_section (section)) { +if (iommufd_listener_skipped_section (container, section)) {return ; } iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space);
@@ -2770,7 +2772,8 @@ static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,/* try to attach to an existing container in this space */ QLIST_FOREACH(container, &s->containers, next) { -if (container->iommufd != iommufd) { +if (container->iommufd != iommufd || + container->errata != vtd_hiod->errata) {continue ; }
@@ -2797,6 +2800,7 @@ static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod, container =g_malloc0 (sizeof (*container)); container->iommufd = iommufd; container->ioas_id = ioas_id; + container->errata = vtd_hiod->errata;QLIST_INIT (&container->s2_hwpt_list); if (vtd_device_attach_container(vtd_hiod, container, pasid, pe, hwpt,
@@ -5355,9 +5359,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;
@@ -5406,6 +5411,8 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,error_setg (errp,"Stage-1 1GB huge page is unsupported by host IOMMU" ); return false; } + + vtd_hiod->errata = vtd->flags &IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 ;#endif error_setg (errp,"host device is uncompatible with stage-1 translation" );
@@ -5439,7 +5446,8 @@ 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.34.1
PATCH 11/15 intel_iommu: Replay pasid binds after context cache invalidation
In reply to Zhenzhong DuanFrom: Yi Liu <[email protected]>
This replays guest pasid attachments 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 | 1 + hw/i386/intel_iommu.c | 51 ++++++++++++++++++++++++++++++++-- hw/i386/trace-events | 1 + 3 files changed, 51 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
@@ -575,6 +575,7 @@ typedef enum VTDPCInvType {VTD_PASID_CACHE_FORCE_RESET =0 ,/* pasid cache invalidation rely on guest PASID entry */ VTD_PASID_CACHE_GLOBAL_INV ,/* pasid cache global invalidation */ +VTD_PASID_CACHE_DEVSI ,/* pasid cache device selective invalidation */ VTD_PASID_CACHE_DOMSI ,/* pasid cache domain selective invalidation */ VTD_PASID_CACHE_PASIDSI ,/* pasid cache pasid selective invalidation */ } VTDPCInvType;
hw/i386/intel_iommu.c +49 -2
99 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.34.1
PATCH 12/15 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 granular. 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 in 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 | 118 ++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_internal.h +6 -0
@@ -589,6 +589,12 @@ typedef struct VTDPASIDCacheInfo {bool error_happened; } 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 +116 -2
154 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 13/15 intel_iommu: Refresh pasid bind 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, the pasid bindings on host side become stale and need to be updated.
Introduce a helper function vtd_refresh_pasid_bind() for that purpose.
--- hw/i386/intel_iommu.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
hw/i386/intel_iommu.c +23 -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_refresh_pasid_bind (IntelIOMMUState *s);static void vtd_pasid_cache_reset_locked (IntelIOMMUState *s); static void vtd_pasid_cache_sync(IntelIOMMUState *s,
@@ -3362,6 +3363,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_refresh_pasid_bind (s); }/* Set Interrupt Remap Table Pointer */
@@ -3396,6 +3398,7 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)vtd_reset_caches (s);vtd_address_space_refresh_all (s); +vtd_refresh_pasid_bind (s); }/* Handle Interrupt Remap Enable/Disable */
@@ -4111,6 +4114,26 @@ static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s, } } +static void vtd_refresh_pasid_bind (IntelIOMMUState *s) +{ +VTDPASIDCacheInfo pc_info = { .error_happened = false, + .type =VTD_PASID_CACHE_GLOBAL_INV }; + +/* +* Only when dmar is enabled, should pasid bindings replayed, +* otherwise no need to replay. +*/ +if (!s->dmar_enabled) { +return ; + } + +if (!s->flts || !s->root_scalable) { +return ; + } + +vtd_replay_guest_pasid_bindings (s, &pc_info); +} +/* * This function syncs the pasid bindings between guest and host. * It includes updating the pasid cache in vIOMMU and updating the
Signature · 1 line
2.34.1
PATCH 14/15 intel_iommu: Bypass replay in stage-1 page table mode
In reply to Zhenzhong DuanVFIO utilizes replay to setup initial shadow iommu mappings. But when stage-1 page table is configured, it is passed to host to construct nested page table, there is no replay needed.
--- hw/i386/intel_iommu.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +8 -0
@@ -5759,6 +5759,14 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)VTDContextEntry ce;DMAMap map = { .iova =0 , .size =HWADDR_MAX }; +/* +* Replay on stage-1 page table is meaningless as stage-1 page table +* is passthroughed to host to construct nested page table +*/ +if (s->flts && s->root_scalable) { +return ; + } +/* replay is protected by BQL, page walk will re-setup it safely */ iova_tree_remove (vtd_as->iova_tree, map);
Signature · 1 line
2.34.1
PATCH 15/15 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 | 1 + 1 file changed, 1 insertion(+)
hw/i386/intel_iommu.c +1 -0
@@ -5597,6 +5597,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, } vtd_hiod->errata = vtd->flags &IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 ; +return true;#endif error_setg (errp,"host device is uncompatible with stage-1 translation" );
Signature · 1 line
2.34.1