Skip to contentSkip to conversation

intel_iommu: Enable first stage translation for passthrough device

PATCH Superseded Zhenzhong Duan 4 participants 70 messages Oct 24, 2025 – Nov 13, 2025

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v7 cover intel_iommu: Enable first stage translation for passthrough device

Hi,

For passthrough device with intel_iommu.x-flts=on, we don't do shadowing of guest page table but pass first stage page table to host side to construct a nested HWPT. There was some effort to enable this feature in old days, see
[1] for details.

The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform the first stage address translation. Along with it, modifications to present mappings in the guest I/O page table should be followed with an IOTLB invalidation.

        .-------------.  .---------------------------.
        |   vIOMMU    |  | Guest I/O page table      |
        |             |  '---------------------------'
        .----------------/
        | PASID Entry |--- PASID cache flush --+
        '-------------'                        |
        |             |                        V
        |             |           I/O page table pointer in GPA
        '-------------'
    Guest
    ------| Shadow |---------------------------|--------
          v        v                           v
    Host
        .-------------.  .-----------------------------.
        |   pIOMMU    |  | First stage for GIOVA->GPA  |
        |             |  '-----------------------------'
        .----------------/  |
        | PASID Entry |     V (Nested xlate)
        '----------------\.--------------------------------------------.
        |             |   | Second stage for GPA->HPA, unmanaged domain|
        |             |   '--------------------------------------------'
        '-------------'

<Intel VT-d Nested translation>

This series reuse VFIO device's default HWPT as nesting parent instead of creating new one. This way avoids duplicate code of a new memory listener, all existing feature from VFIO listener can be shared, e.g., ram discard, dirty tracking, etc. Two limitations are: 1) not supporting VFIO device under a PCI bridge with emulated device, because emulated device wants IOMMU AS and VFIO device stick to system AS; 2) not supporting kexec or reboot from "intel_iommu=on,sm_on" to "intel_iommu=on,sm_off" on platform with ERRATA_772415_SPR17, because VFIO device's default HWPT is created with NEST_PARENT flag, kernel inhibit RO mappings when switch to shadow mode.

This series is also a prerequisite work for vSVA, i.e. Sharing guest application address space with passthrough devices.

There are some interactions between VFIO and vIOMMU
* vIOMMU registers PCIIOMMUOps [set|unset]_iommu_device to PCI subsystem. VFIO calls them to register/unregister HostIOMMUDevice instance to vIOMMU at vfio device realize stage.
* vIOMMU registers PCIIOMMUOps get_viommu_flags to PCI subsystem. VFIO calls it to get vIOMMU exposed flags.
* vIOMMU calls HostIOMMUDeviceIOMMUFD interface [at|de]tach_hwpt to bind/unbind device to IOMMUFD backed domains, either nested domain or not.

See below diagram:

        VFIO Device                                 Intel IOMMU
    .-----------------.                         .-------------------.
    |                 |                         |                   |
    |       .---------|PCIIOMMUOps              |.-------------.    |
    |       | IOMMUFD |(set/unset_iommu_device) || Host IOMMU  |    |
    |       | Device  |------------------------>|| Device list |    |
    |       .---------|(get_viommu_flags)       |.-------------.    |
    |                 |                         |       |           |
    |                 |                         |       V           |
    |       .---------|  HostIOMMUDeviceIOMMUFD |  .-------------.  |
    |       | IOMMUFD |            (attach_hwpt)|  | Host IOMMU  |  |
    |       | link    |<------------------------|  |   Device    |  |
    |       .---------|            (detach_hwpt)|  .-------------.  |
    |                 |                         |       |           |
    |                 |                         |       ...         |
    .-----------------.                         .-------------------.

Below is an example to enable first stage translation for passthrough device:

-M q35,...
-device intel-iommu,x-scalable-mode=on,x-flts=on... -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

Test done:
- VFIO devices hotplug/unplug
- different VFIO devices linked to different iommufds
- vhost net device ping test
- migration with QAT passthrough

PATCH01-09: Some preparing work
PATCH10-11: Compatibility check between vIOMMU and Host IOMMU
PATCH12-16: Implement first stage page table for passthrough device
PATCH17-19: Workaround for ERRATA_772415_SPR17
PATCH20-21: Add migration support and optimization
PATCH22:    Enable first stage translation for passthrough device
PATCH23:    Add doc

Qemu code can be found at [2], it's based on
vfio-next + domain_switch_series[3] + migration_relax_series[4].

Fault event injection to guest isn't supported in this series, we presume guest kernel always construct correct first stage page table for passthrough device. For emulated devices, the emulation code already provided first stage fault injection.

TODO:
- Fault event injection to guest when HW first stage page table faults

[1] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting.v7
[3] https://lore.kernel.org/qemu-devel/[email protected]/
[4] https://lore.kernel.org/qemu-devel/[email protected]/

Thanks
Zhenzhong

Changelog:
v7:
- s/host_iommu_extract_vendor_caps/host_iommu_extract_quirks (Nicolin)
- s/RID_PASID/PASID_0 (Eric)
- drop rid2pasid check in vtd_do_iommu_translate (Eric)
- refine DID check in vtd_pasid_cache_sync_locked (Liuyi)
- refine commit log (Nicolin, Eric, Liuyi)
- Fix doc build (Cedric)
- add migration support

v6:
- delete RPS capability related supporting code (Eric, Yi)
- use terminology 'first/second stage' to replace 'first/second level" (Eric, Yi)
- use get_viommu_flags() instead of get_viommu_caps() (Nicolin)
- drop non-RID_PASID related code and simplify pasid invalidation handling (Eric, Yi)
- drop the patch that handle pasid replay when context invalidation (Eric)
- move vendor specific cap check from VFIO core to backend/iommufd.c (Nicolin)

v5:
- refine commit log of patch2 (Cedric, Nicolin)
- introduce helper vfio_pci_from_vfio_device() (Cedric)
- introduce helper vfio_device_viommu_get_nested() (Cedric)
- pass 'bool bypass_ro' argument to vfio_listener_valid_section() instead of 'VFIOContainerBase *' (Cedric)
- fix a potential build error reported by Jim Shu

v4:
- s/VIOMMU_CAP_STAGE1/VIOMMU_CAP_HW_NESTED (Eric, Nicolin, Donald, Shameer)
- clarify get_viommu_cap() return pure emulated caps and explain reason in commit log (Eric)
- retrieve the ce only if vtd_as->pasid in vtd_as_to_iommu_pasid_locked (Eric)
- refine doc comment and commit log in patch10-11 (Eric)

v3:
- define enum type for VIOMMU_CAP_* (Eric)
- drop inline flag in the patch which uses the helper (Eric)
- use extract64 in new introduced MACRO (Eric)
- polish comments and fix typo error (Eric)
- split workaround patch for ERRATA_772415_SPR17 to two patches (Eric)
- optimize bind/unbind error path processing

v2:
- introduce get_viommu_cap() to get STAGE1 flag to create nesting parent HWPT (Liuyi)
- reuse VFIO's default HWPT as parent HWPT of nested translation (Nicolin, Liuyi)
- abandon support of VFIO device under pcie-to-pci bridge to simplify design (Liuyi)
- bypass RO mapping in VFIO's default HWPT if ERRATA_772415_SPR17 (Liuyi)
- drop vtd_dev_to_context_entry optimization (Liuyi)

v1:
- simplify vendor specific checking in vtd_check_hiod (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: Propagate PASID-based iotlb invalidation to host
intel_iommu: Replay all pasid bindings when either SRTP or TE bit is
changed
intel_iommu: Replay pasid bindings after context cache invalidation

Zhenzhong Duan (20):
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Delete RPS capability related supporting code
intel_iommu: Update terminology to match VTD spec
hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool hw/pci: Introduce pci_device_get_viommu_flags()
intel_iommu: Implement get_viommu_flags() callback
intel_iommu: Introduce a new structure VTDHostIOMMUDevice vfio/iommufd: Force creating nesting parent HWPT
intel_iommu: Stick to system MR for IOMMUFD backed host device when
x-flts=on
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Fail passthrough device under PCI bridge if x-flts=on
intel_iommu: Add some macros and inline functions
intel_iommu: Bind/unbind guest page table to host
iommufd: Introduce a helper function to extract vendor capabilities
vfio: Add a new element bypass_ro in VFIOContainer Workaround for ERRATA_772415_SPR17
vfio: Bypass readonly region for dirty tracking
intel_iommu: Add migration support with x-flts=on
intel_iommu: Enable host device when x-flts=on in scalable mode docs/devel: Add IOMMUFD nesting documentation

 MAINTAINERS                        |   1 +
 docs/devel/vfio-iommufd.rst        |  25 +
 hw/i386/intel_iommu_internal.h     | 107 ++--
 include/hw/i386/intel_iommu.h      |   5 +-
 include/hw/iommu.h                 |  30 ++
 include/hw/pci/pci.h               |  24 +
 include/hw/vfio/vfio-container.h   |   1 +
 include/hw/vfio/vfio-device.h      |   2 +
 include/system/host_iommu_device.h |  15 +
 backends/iommufd.c                 |  13 +
 hw/i386/intel_iommu.c              | 785 +++++++++++++++++++++--------
 hw/pci/pci.c                       |  23 +-
 hw/vfio/device.c                   |  12 +
 hw/vfio/iommufd.c                  |  19 +-
 hw/vfio/listener.c                 |  28 +-
 tests/qtest/intel-iommu-test.c     |   4 +-
 hw/i386/trace-events               |   4 +
 17 files changed, 831 insertions(+), 267 deletions(-)
 create mode 100644 include/hw/iommu.h
Signature · 1 line
2.47.1
#1 lore raw

PATCH v7 1/23 intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

In early days vtd_ce_get_rid2pasid_entry() was used to get pasid entry of rid2pasid, then it was extended to get any pasid entry. So a new name vtd_ce_get_pasid_entry is better to match what it actually does.

No functional change intended.

---
 hw/i386/intel_iommu.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
hw/i386/intel_iommu.c +10 -12
index b00fdecaf8..70746e3080 100644
@@ -956,10 +956,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
     return 0;
 }
 
-static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
-                                      VTDContextEntry *ce,
-                                      VTDPASIDEntry *pe,
-                                      uint32_t pasid)
+static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
+                                  VTDPASIDEntry *pe, uint32_t pasid)
 {
     dma_addr_t pasid_dir_base;
     int ret = 0;
@@ -1037,7 +1035,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 {
@@ -1060,7 +1058,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;
     }
 
@@ -1128,7 +1126,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 {
@@ -1534,7 +1532,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) */
@@ -1623,7 +1621,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]);
     }
 
@@ -1699,7 +1697,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
@@ -3085,7 +3083,7 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
         return ret;
     }
 
-    return vtd_ce_get_rid2pasid_entry(s, &ce, pe, vtd_as->pasid);
+    return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);
 }
 
 static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
@@ -5204,7 +5202,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
     if (ret) {
         return -EINVAL;
     }
-    ret = vtd_ce_get_rid2pasid_entry(s, &ce, &pe, vtd_as->pasid);
+    ret = vtd_ce_get_pasid_entry(s, &ce, &pe, vtd_as->pasid);
     if (ret) {
         return -EINVAL;
     }
Signature · 1 line
2.47.1
#2 lore raw

PATCH v7 2/23 intel_iommu: Delete RPS capability related supporting code

In reply to Zhenzhong Duan

RID-PASID Support(RPS) is not set in vIOMMU ECAP register, the supporting code is there but never takes effect.

Meanwhile, according to VTD spec section 3.4.3:
"Implementations not supporting RID_PASID capability (ECAP_REG.RPS is 0b), use a PASID value of 0 to perform address translation for requests without PASID."

We should delete the supporting code which fetches RID_PASID field from scalable context entry and use 0 as RID_PASID directly, because RID_PASID field is ignored if no RPS support according to spec.

This simplifies the code and doesn't bring any penalty.

---
 hw/i386/intel_iommu_internal.h |  1 -
 hw/i386/intel_iommu.c          | 82 +++++++++++-----------------------
 2 files changed, 27 insertions(+), 56 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index 75bafdf0cd..bf8fb2aa80 100644
@@ -609,7 +609,6 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
-#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
hw/i386/intel_iommu.c +27 -55

164 lines not on this page · raw message

Signature · 1 line
2.47.1
#3 lore raw

Re: [PATCH v7 02/23] intel_iommu: Delete RPS capability related supporting code

Hi Zhenzhong,

Quoted 48 lines · Zhenzhong Duan

RID-PASID Support(RPS) is not set in vIOMMU ECAP register, the supporting code is there but never takes effect.

Meanwhile, according to VTD spec section 3.4.3:
"Implementations not supporting RID_PASID capability (ECAP_REG.RPS is 0b), use a PASID value of 0 to perform address translation for requests without PASID."

We should delete the supporting code which fetches RID_PASID field from scalable context entry and use 0 as RID_PASID directly, because RID_PASID field is ignored if no RPS support according to spec.

This simplifies the code and doesn't bring any penalty.

---
 hw/i386/intel_iommu_internal.h |  1 -
 hw/i386/intel_iommu.c          | 82 +++++++++++-----------------------
 2 files changed, 27 insertions(+), 56 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index 75bafdf0cd..bf8fb2aa80 100644
@@ -609,7 +609,6 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
-#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
hw/i386/intel_iommu.c +1 -2
index 70746e3080..06065d16b6 100644
@@ -42,8 +42,7 @@
 #include "trace.h"
 
 /* context entry operations */
-#define VTD_CE_GET_RID2PASID(ce) \
-    ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK)
+#define PASID_0    0
 #define VTD_CE_GET_PASID_DIR_TABLE(ce) \
     ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
 #define VTD_CE_GET_PRE(ce) \

hw/i386/intel_iommu.c static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce, · line 962

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -963,7 +962,7 @@ static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
     int ret = 0;

while you are at it, get rid of ret and simply return vtd_get_pe_from_pasid_table()?

Quoted 41 lines
hw/i386/intel_iommu.c +5 -7
     if (pasid == PCI_NO_PASID) {
-        pasid = VTD_CE_GET_RID2PASID(ce);
+        pasid = PASID_0;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
     ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
@@ -982,7 +981,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (pasid == PCI_NO_PASID) {
-        pasid = VTD_CE_GET_RID2PASID(ce);
+        pasid = PASID_0;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
@@ -1522,17 +1521,15 @@ static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,
     return 0;
 }
 
-static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
-                                  VTDContextEntry *ce)
+static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry *ce)
 {
     VTDPASIDEntry pe;
 
     /*
      * Make sure in Scalable Mode, a present context entry
-     * has valid rid2pasid setting, which includes valid
-     * rid2pasid field and corresponding pasid entry setting
+     * has valid pasid entry setting at PASID_0.
      */
-    return vtd_ce_get_pasid_entry(s, ce, &pe, PCI_NO_PASID);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, PASID_0);
 }
 
 /* Map a device to its corresponding domain (context-entry) */
@@ -1593,12 +1590,11 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
         }
     } else {
         /*

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 1597

Quoted 10 lines
hw/i386/intel_iommu.c +4 -5
-         * Check if the programming of context-entry.rid2pasid
-         * and corresponding pasid setting is valid, and thus
-         * avoids to check pasid entry fetching result in future
-         * helper function calling.
+         * Check if the programming of pasid setting of PASID_0
+         * is valid, and thus avoids to check pasid entry fetching
+         * result in future helper function calling.
          */
-        ret_fr = vtd_ce_rid2pasid_check(s, ce);
+        ret_fr = vtd_ce_pasid_0_check(s, ce);

I guess you should be able to return vtd_ce_pasid_0_check(s, ce) directly too.

Quoted 12 lines
hw/i386/intel_iommu.c +0 -1
         if (ret_fr) {
             return ret_fr;
         }
@@ -2110,7 +2106,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     bool reads = true;
     bool writes = true;
     uint8_t access_flags, pgtt;
-    bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
     uint64_t xlat, size;
 

hw/i386/intel_iommu.c static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 2125

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
@@ -2122,21 +2117,23 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
 
     vtd_iommu_lock(s);
 
-    cc_entry = &vtd_as->context_cache_entry;

any reason why cc_entry setting was moved? Seems a spurious change.

Quoted 72 lines
hw/i386/intel_iommu.c +14 -34
+    if (pasid == PCI_NO_PASID && s->root_scalable) {
+        pasid = PASID_0;
+    }
 
-    /* Try to fetch pte from IOTLB, we don't need RID2PASID logic */
-    if (!rid2pasid) {
-        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
-        if (iotlb_entry) {
-            trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
-                                     iotlb_entry->domain_id);
-            pte = iotlb_entry->pte;
-            access_flags = iotlb_entry->access_flags;
-            page_mask = iotlb_entry->mask;
-            goto out;
-        }
+    /* Try to fetch pte from IOTLB */
+    iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
+    if (iotlb_entry) {
+        trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
+                                 iotlb_entry->domain_id);
+        pte = iotlb_entry->pte;
+        access_flags = iotlb_entry->access_flags;
+        page_mask = iotlb_entry->mask;
+        goto out;
     }
 
+    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) {
         trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
@@ -2173,10 +2170,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         cc_entry->context_cache_gen = s->context_cache_gen;
     }
 
-    if (rid2pasid) {
-        pasid = VTD_CE_GET_RID2PASID(&ce);
-    }
-
     /*
      * We don't need to translate for pass-through context entries.
      * Also, let's ignore IOTLB caching as well for PT devices.
@@ -2202,19 +2195,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         return true;
     }
 
-    /* Try to fetch pte from IOTLB for RID2PASID slow path */
-    if (rid2pasid) {
-        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
-        if (iotlb_entry) {
-            trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
-                                     iotlb_entry->domain_id);
-            pte = iotlb_entry->pte;
-            access_flags = iotlb_entry->access_flags;
-            page_mask = iotlb_entry->mask;
-            goto out;
-        }
-    }
-
     if (s->flts && s->root_scalable) {
         ret_fr = vtd_iova_to_flpte(s, &ce, addr, is_write, &pte, &level,
                                    &reads, &writes, s->aw_bits, pasid);
@@ -2477,20 +2457,14 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            uint32_t rid2pasid = PCI_NO_PASID;
-
-            if (s->root_scalable) {
-                rid2pasid = VTD_CE_GET_RID2PASID(&ce);
-            }
-

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2467

Quoted 10 lines
hw/i386/intel_iommu.c +2 -2
             /*
              * In legacy mode, vtd_as->pasid == pasid is always true.
              * In scalable mode, for vtd address space backing a PCI
              * device without pasid, needs to compare pasid with
-             * rid2pasid of this device.
+             * PASID_0 of this device.
              */
             if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == rid2pasid))) {
+                  (vtd_as->pasid == PCI_NO_PASID && pasid == PASID_0))) {

don't you need to check you are in s->root_scalable mode too?

Thanks

Eric

Quoted 14 lines
hw/i386/intel_iommu.c +1 -3
                 continue;
             }
 
@@ -2995,9 +2969,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
         if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                       vtd_as->devfn, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
-
-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != PASID_0) &&
                 vtd_as->pasid != pasid) {
                 continue;
             }
#4 lore raw

RE: [PATCH v7 02/23] intel_iommu: Delete RPS capability related supporting code

Hi Eric,

Quoted 61 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 02/23] intel_iommu: Delete RPS capability related supporting code

Hi Zhenzhong,

Quoted 5 lines · Zhenzhong Duan

RID-PASID Support(RPS) is not set in vIOMMU ECAP register, the supporting code is there but never takes effect.

Meanwhile, according to VTD spec section 3.4.3:
"Implementations not supporting RID_PASID capability (ECAP_REG.RPS is

0b),

Quoted 9 lines

use a PASID value of 0 to perform address translation for requests without PASID."

We should delete the supporting code which fetches RID_PASID field from scalable context entry and use 0 as RID_PASID directly, because RID_PASID field is ignored if no RPS support according to spec.

This simplifies the code and doesn't bring any penalty.

Review

Quoted 8 lines
---
 hw/i386/intel_iommu_internal.h |  1 -
 hw/i386/intel_iommu.c          | 82 +++++++++++-----------------------
 2 files changed, 27 insertions(+), 56 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry; · line 612

Quoted 9 lines

index 75bafdf0cd..bf8fb2aa80 100644

hw/i386/intel_iommu_internal.h +0 -1
@@ -609,7 +609,6 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
-#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL |

~VTD_HAW_MASK(aw))

hw/i386/intel_iommu_internal.h typedef struct VTDRootEntry VTDRootEntry; · line 613

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -0
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1

0xffffffffffe00000ULL

hw/i386/intel_iommu.c static int vtd_ce_get_pasid_entry(IntelIOMMUState

Quoted 16 lines
hw/i386/intel_iommu_internal.h +0 -0
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
hw/i386/intel_iommu.c +1 -2
index 70746e3080..06065d16b6 100644
@@ -42,8 +42,7 @@
 #include "trace.h"
 
 /* context entry operations */
-#define VTD_CE_GET_RID2PASID(ce) \
-    ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK)
+#define PASID_0    0
 #define VTD_CE_GET_PASID_DIR_TABLE(ce) \
     ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
 #define VTD_CE_GET_PRE(ce) \
@@ -963,7 +962,7 @@ static int vtd_ce_get_pasid_entry(IntelIOMMUState

*s, VTDContextEntry *ce,

Quoted 3 lines

hw/i386/intel_iommu.c static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce, · line 962

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
     int ret = 0;

while you are at it, get rid of ret and simply return vtd_get_pe_from_pasid_table()?

Sure

Quoted 55 lines

hw/i386/intel_iommu.c static int vtd_ce_get_pasid_fpd(IntelIOMMUState

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
     if (pasid == PCI_NO_PASID) {
-        pasid = VTD_CE_GET_RID2PASID(ce);
+        pasid = PASID_0;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
     ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
@@ -982,7 +981,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState

*s,

hw/i386/intel_iommu.c static inline int

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
     VTDPASIDEntry pe;
 
     if (pasid == PCI_NO_PASID) {
-        pasid = VTD_CE_GET_RID2PASID(ce);
+        pasid = PASID_0;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
@@ -1522,17 +1521,15 @@ static inline int

vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,

hw/i386/intel_iommu.c static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
     return 0;
 }
 
-static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
-                                  VTDContextEntry *ce)
+static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry

*ce)

hw/i386/intel_iommu.c static int

Quoted 15 lines
hw/i386/intel_iommu.c +2 -3
 {
     VTDPASIDEntry pe;
 
     /*
      * Make sure in Scalable Mode, a present context entry
-     * has valid rid2pasid setting, which includes valid
-     * rid2pasid field and corresponding pasid entry setting
+     * has valid pasid entry setting at PASID_0.
      */
-    return vtd_ce_get_pasid_entry(s, ce, &pe, PCI_NO_PASID);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, PASID_0);
 }
 
 /* Map a device to its corresponding domain (context-entry) */
@@ -1593,12 +1590,11 @@ static int

vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 1597

Quoted 13 lines
hw/i386/intel_iommu.c +4 -5
         }
     } else {
         /*
-         * Check if the programming of context-entry.rid2pasid
-         * and corresponding pasid setting is valid, and thus
-         * avoids to check pasid entry fetching result in future
-         * helper function calling.
+         * Check if the programming of pasid setting of PASID_0
+         * is valid, and thus avoids to check pasid entry fetching
+         * result in future helper function calling.
          */
-        ret_fr = vtd_ce_rid2pasid_check(s, ce);
+        ret_fr = vtd_ce_pasid_0_check(s, ce);
Quoted 2 lines

I guess you should be able to return vtd_ce_pasid_0_check(s, ce) directly too.

Yes.

Quoted 12 lines

hw/i386/intel_iommu.c static bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
         if (ret_fr) {
             return ret_fr;
         }
@@ -2110,7 +2106,6 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 7 lines
hw/i386/intel_iommu.c +0 -1
     bool reads = true;
     bool writes = true;
     uint8_t access_flags, pgtt;
-    bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
     uint64_t xlat, size;
 
Quoted 7 lines

hw/i386/intel_iommu.c static bool

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -2122,21 +2117,23 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

hw/i386/intel_iommu.c static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 2125

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
     vtd_iommu_lock(s);
 
-    cc_entry = &vtd_as->context_cache_entry;

any reason why cc_entry setting was moved? Seems a spurious change.

I'd like to initialize cc_entry right before it will be dereferenced, no need to initialize it early because we can 'goto out' early.

Quoted 88 lines
Quoted 21 lines
hw/i386/intel_iommu.c +9 -11
+    if (pasid == PCI_NO_PASID && s->root_scalable) {
+        pasid = PASID_0;
+    }
 
-    /* Try to fetch pte from IOTLB, we don't need RID2PASID logic */
-    if (!rid2pasid) {
-        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
-        if (iotlb_entry) {
-            trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
-                                     iotlb_entry->domain_id);
-            pte = iotlb_entry->pte;
-            access_flags = iotlb_entry->access_flags;
-            page_mask = iotlb_entry->mask;
-            goto out;
-        }
+    /* Try to fetch pte from IOTLB */
+    iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
+    if (iotlb_entry) {
+        trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
+                                 iotlb_entry->domain_id);
+        pte = iotlb_entry->pte;

hw/i386/intel_iommu.c static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +5 -0
+        access_flags = iotlb_entry->access_flags;
+        page_mask = iotlb_entry->mask;
+        goto out;
     }
 
+    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) {
         trace_vtd_iotlb_cc_hit(bus_num, devfn,

cc_entry->context_entry.hi,

hw/i386/intel_iommu.c static bool

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -2173,10 +2170,6 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

hw/i386/intel_iommu.c static bool

Quoted 11 lines
hw/i386/intel_iommu.c +0 -4
         cc_entry->context_cache_gen = s->context_cache_gen;
     }
 
-    if (rid2pasid) {
-        pasid = VTD_CE_GET_RID2PASID(&ce);
-    }
-
     /*
      * We don't need to translate for pass-through context entries.
      * Also, let's ignore IOTLB caching as well for PT devices.
@@ -2202,19 +2195,6 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 9 lines
hw/i386/intel_iommu.c +0 -6
         return true;
     }
 
-    /* Try to fetch pte from IOTLB for RID2PASID slow path */
-    if (rid2pasid) {
-        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
-        if (iotlb_entry) {
-            trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
-                                     iotlb_entry->domain_id);

hw/i386/intel_iommu.c static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +0 -7
-            pte = iotlb_entry->pte;
-            access_flags = iotlb_entry->access_flags;
-            page_mask = iotlb_entry->mask;
-            goto out;
-        }
-    }
-
     if (s->flts && s->root_scalable) {
         ret_fr = vtd_iova_to_flpte(s, &ce, addr, is_write, &pte, &level,
                                    &reads, &writes, s->aw_bits,

pasid);

hw/i386/intel_iommu.c static void

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -2477,20 +2457,14 @@ static void

vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce,

vtd_as->pasid)) {

Quoted 5 lines
hw/i386/intel_iommu.c +0 -5
-            uint32_t rid2pasid = PCI_NO_PASID;
-
-            if (s->root_scalable) {
-                rid2pasid = VTD_CE_GET_RID2PASID(&ce);
-            }

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,

Quoted 10 lines
hw/i386/intel_iommu.c +1 -3
-
             /*
              * In legacy mode, vtd_as->pasid == pasid is always true.
              * In scalable mode, for vtd address space backing a PCI
              * device without pasid, needs to compare pasid with
-             * rid2pasid of this device.
+             * PASID_0 of this device.
              */
             if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid ==

rid2pasid))) {

Quoted 3 lines

hw/i386/intel_iommu.c static void · line 2467

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  (vtd_as->pasid == PCI_NO_PASID && pasid ==

PASID_0))) {
don't you need to check you are in s->root_scalable mode too?

I think no need, this combination check can handle both scalable and legacy modes, because if s->root_scalable=false, pasid always is PCI_NO_PASID, 'vtd_as->pasid == pasid' becomes vtd_'as->pasid == PCI_NO_PASID', it's a superset of the remaining check.

So the remaining check is already for s->root_scalable=true.

Thanks
Zhenzhong

#5 lore raw

PATCH v7 3/23 intel_iommu: Update terminology to match VTD spec

In reply to Zhenzhong Duan

VTD spec revision 3.4 released in December 2021 renamed "First-level" to "First-stage" and "Second-level" to "Second-stage".

Do the same in intel_iommu code to match spec, change all existing "fl/sl/FL/SL/first level/second level/stage-1/stage-2" terminology to "fs/ss/FS/SS/first stage/second stage".

Opportunistically fix a error print of "flts=on" with "x-flts=on".

No functional changes intended.

---
 hw/i386/intel_iommu_internal.h |  85 ++++++------
 include/hw/i386/intel_iommu.h  |   2 +-
 hw/i386/intel_iommu.c          | 247 +++++++++++++++++----------------
 tests/qtest/intel-iommu-test.c |   4 +-
 4 files changed, 170 insertions(+), 168 deletions(-)
hw/i386/intel_iommu_internal.h +43 -42
index bf8fb2aa80..ba0f1f5096 100644
@@ -196,8 +196,8 @@
 #define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
-#define VTD_ECAP_SLTS               (1ULL << 46)
-#define VTD_ECAP_FLTS               (1ULL << 47)
+#define VTD_ECAP_SSTS               (1ULL << 46)
+#define VTD_ECAP_FSTS               (1ULL << 47)
 
 /* CAP_REG */
 /* (offset >> 4) << 24 */
@@ -211,7 +211,7 @@
 #define VTD_MAMV                    18ULL
 #define VTD_CAP_MAMV                (VTD_MAMV << 48)
 #define VTD_CAP_PSI                 (1ULL << 39)
-#define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
+#define VTD_CAP_SSLPS               ((1ULL << 34) | (1ULL << 35))
 #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
 #define VTD_CAP_DRAIN_READ          (1ULL << 55)
 #define VTD_CAP_FS1GP               (1ULL << 56)
@@ -284,7 +284,7 @@ typedef enum VTDFaultReason {
     VTD_FR_ADDR_BEYOND_MGAW,    /* Input-address above (2^x-1) */
     VTD_FR_WRITE,               /* No write permission */
     VTD_FR_READ,                /* No read permission */
-    /* Fail to access a second-level paging entry (not SL_PML4E) */
+    /* Fail to access a second-stage paging entry (not SS_PML4E) */
     VTD_FR_PAGING_ENTRY_INV,
     VTD_FR_ROOT_TABLE_INV,      /* Fail to access a root-entry */
     VTD_FR_CONTEXT_TABLE_INV,   /* Fail to access a context-entry */
@@ -292,7 +292,8 @@ typedef enum VTDFaultReason {
     VTD_FR_ROOT_ENTRY_RSVD,
     /* Non-zero reserved field in a present context-entry */
     VTD_FR_CONTEXT_ENTRY_RSVD,
-    /* Non-zero reserved field in a second-level paging entry with at lease one
+    /*
+     * Non-zero reserved field in a second-stage paging entry with at lease one
      * Read(R) and Write(W) or Execute(E) field is Set.
      */
     VTD_FR_PAGING_ENTRY_RSVD,
@@ -329,7 +330,7 @@ typedef enum VTDFaultReason {
     VTD_FR_PASID_ENTRY_P = 0x59,
     VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
 
-    /* Fail to access a first-level paging entry (not FS_PML4E) */
+    /* Fail to access a first-stage paging entry (not FS_PML4E) */
     VTD_FR_FS_PAGING_ENTRY_INV = 0x70,
     VTD_FR_FS_PAGING_ENTRY_P = 0x71,
     /* Non-zero reserved field in present first-stage paging entry */
@@ -473,23 +474,23 @@ typedef union VTDPRDesc VTDPRDesc;
 
 #define VTD_SPTE_PAGE_L1_RSVD_MASK(aw, stale_tm) \
         stale_tm ? \
-        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
-        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \
+        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 #define VTD_SPTE_PAGE_L2_RSVD_MASK(aw) \
-        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 #define VTD_SPTE_PAGE_L3_RSVD_MASK(aw) \
-        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 #define VTD_SPTE_PAGE_L4_RSVD_MASK(aw) \
-        (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 
 #define VTD_SPTE_LPAGE_L2_RSVD_MASK(aw, stale_tm) \
         stale_tm ? \
-        (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
-        (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \
+        (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 #define VTD_SPTE_LPAGE_L3_RSVD_MASK(aw, stale_tm) \
         stale_tm ? \
-        (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
-        (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
+        (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM | VTD_SS_TM)) : \
+        (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SS_IGN_COM))
 
 /* Rsvd field masks for fpte */
 #define VTD_FS_UPPER_IGNORED 0xfff0000000000000ULL
@@ -596,8 +597,8 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CONTEXT_TT_MULTI_LEVEL  0
 #define VTD_CONTEXT_TT_DEV_IOTLB    (1ULL << 2)
 #define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2)
-/* Second Level Page Translation Pointer*/
-#define VTD_CONTEXT_ENTRY_SLPTPTR   (~0xfffULL)
+/* Second Stage Page Translation Pointer*/
+#define VTD_CONTEXT_ENTRY_SSPTPTR   (~0xfffULL)
 #define VTD_CONTEXT_ENTRY_RSVD_LO(aw) (0xff0ULL | ~VTD_HAW_MASK(aw))
 /* hi */
 #define VTD_CONTEXT_ENTRY_AW        7ULL /* Adjusted guest-address-width */
@@ -634,37 +635,37 @@ typedef struct VTDPASIDCacheInfo {
 /* PASID Granular Translation Type Mask */
 #define VTD_PASID_ENTRY_P              1ULL
 #define VTD_SM_PASID_ENTRY_PGTT        (7ULL << 6)
-#define VTD_SM_PASID_ENTRY_FLT         (1ULL << 6)
-#define VTD_SM_PASID_ENTRY_SLT         (2ULL << 6)
+#define VTD_SM_PASID_ENTRY_FST         (1ULL << 6)
+#define VTD_SM_PASID_ENTRY_SST         (2ULL << 6)
 #define VTD_SM_PASID_ENTRY_NESTED      (3ULL << 6)
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
 #define VTD_SM_PASID_ENTRY_DID(val)    ((val) & VTD_DOMAIN_ID_MASK)
 
-#define VTD_SM_PASID_ENTRY_FLPM          3ULL
-#define VTD_SM_PASID_ENTRY_FLPTPTR       (~0xfffULL)
-
-/* First Level Paging Structure */
-/* Masks for First Level Paging Entry */
-#define VTD_FL_P                    1ULL
-#define VTD_FL_RW                   (1ULL << 1)
-#define VTD_FL_US                   (1ULL << 2)
-#define VTD_FL_A                    (1ULL << 5)
-#define VTD_FL_D                    (1ULL << 6)
-
-/* Second Level Page Translation Pointer*/
-#define VTD_SM_PASID_ENTRY_SLPTPTR     (~0xfffULL)
-
-/* Second Level Paging Structure */
-/* Masks for Second Level Paging Entry */
-#define VTD_SL_RW_MASK              3ULL
-#define VTD_SL_R                    1ULL
-#define VTD_SL_W                    (1ULL << 1)
-#define VTD_SL_IGN_COM              0xbff0000000000000ULL
-#define VTD_SL_TM                   (1ULL << 62)
-
-/* Common for both First Level and Second Level */
+#define VTD_SM_PASID_ENTRY_FSPM          3ULL
+#define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+
+/* First Stage Paging Structure */
+/* Masks for First Stage Paging Entry */
+#define VTD_FS_P                    1ULL
+#define VTD_FS_RW                   (1ULL << 1)
+#define VTD_FS_US                   (1ULL << 2)
+#define VTD_FS_A                    (1ULL << 5)
+#define VTD_FS_D                    (1ULL << 6)
+
+/* Second Stage Page Translation Pointer*/
+#define VTD_SM_PASID_ENTRY_SSPTPTR     (~0xfffULL)
+
+/* Second Stage Paging Structure */
+/* Masks for Second Stage Paging Entry */
+#define VTD_SS_RW_MASK              3ULL
+#define VTD_SS_R                    1ULL
+#define VTD_SS_W                    (1ULL << 1)
+#define VTD_SS_IGN_COM              0xbff0000000000000ULL
+#define VTD_SS_TM                   (1ULL << 62)
+
+/* Common for both First Stage and Second Stage */
 #define VTD_PML4_LEVEL           4
 #define VTD_PDP_LEVEL            3
 #define VTD_PD_LEVEL             2
include/hw/i386/intel_iommu.h +1 -1
index 6e68734b3c..a84d6965a4 100644
@@ -271,7 +271,7 @@ struct IntelIOMMUState {
 
     bool caching_mode;              /* RO - is cap CM enabled? */
     bool scalable_mode;             /* RO - is Scalable Mode supported? */
-    bool flts;                      /* RO - is stage-1 translation supported? */
+    bool fsts;                      /* RO - is first stage translation supported? */
     bool snoop_control;             /* RO - is SNP filed supported? */
 
     dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +124 -123

611 lines not on this page · raw message

tests/qtest/intel-iommu-test.c +2 -2
index c521b3796e..e5cc6acaf0 100644
@@ -13,9 +13,9 @@
 #include "hw/i386/intel_iommu_internal.h"
 
 #define CAP_STAGE_1_FIXED1    (VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | \
-                              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS)
+                              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SSLPS)
 #define ECAP_STAGE_1_FIXED1   (VTD_ECAP_QI |  VTD_ECAP_IR | VTD_ECAP_IRO | \
-                              VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FLTS)
+                              VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FSTS)
 
 static inline uint64_t vtd_reg_readq(QTestState *s, uint64_t offset)
 {
Signature · 1 line
2.47.1
#6 lore raw

PATCH v7 4/23 hw/pci: Export pci_device_get_iommu_bus_devfn() and return bool

In reply to Zhenzhong Duan

Returns true if PCI device is aliased or false otherwise. This will be used in following patch to determine if a PCI device is under a PCI bridge.

---
 include/hw/pci/pci.h |  2 ++
 hw/pci/pci.c         | 12 ++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +2 -0
index 6bccb25ac2..bde9dca8e2 100644
@@ -637,6 +637,8 @@ typedef struct PCIIOMMUOps {
                             bool is_write);
 } PCIIOMMUOps;
 
+bool pci_device_get_iommu_bus_devfn(PCIDevice *dev, PCIBus **piommu_bus,
+                                    PCIBus **aliased_bus, int *aliased_devfn);
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
 bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                  Error **errp);
hw/pci/pci.c +8 -4
index acc03fd470..d0e81651aa 100644
@@ -2858,20 +2858,21 @@ static void pci_device_class_base_init(ObjectClass *klass, const void *data)
  * For call sites which don't need aliased BDF, passing NULL to
  * aliased_[bus|devfn] is allowed.
  *
+ * Returns true if PCI device RID is aliased or false otherwise.
+ *
  * @piommu_bus: return root #PCIBus backed by an IOMMU for the PCI device.
  *
  * @aliased_bus: return aliased #PCIBus of the PCI device, optional.
  *
  * @aliased_devfn: return aliased devfn of the PCI device, optional.
  */
-static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
-                                           PCIBus **piommu_bus,
-                                           PCIBus **aliased_bus,
-                                           int *aliased_devfn)
+bool pci_device_get_iommu_bus_devfn(PCIDevice *dev, PCIBus **piommu_bus,
+                                    PCIBus **aliased_bus, int *aliased_devfn)
 {
     PCIBus *bus = pci_get_bus(dev);
     PCIBus *iommu_bus = bus;
     int devfn = dev->devfn;
+    bool aliased = false;
 
     while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) {
         PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev);
@@ -2908,6 +2909,7 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
                 devfn = parent->devfn;
                 bus = parent_bus;
             }
+            aliased = true;
         }
 
         /*
@@ -2942,6 +2944,8 @@ static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
     if (aliased_devfn) {
         *aliased_devfn = devfn;
     }
+
+    return aliased;
 }
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
Signature · 1 line
2.47.1
#7 lore raw

PATCH v7 5/23 hw/pci: Introduce pci_device_get_viommu_flags()

In reply to Zhenzhong Duan

Introduce a new PCIIOMMUOps optional callback, get_viommu_flags() which allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.

pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.

get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:

1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.

Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU through the set_iommu_device() callback that will be after the attach_device().

See below sequence:

vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)

---
 MAINTAINERS          |  1 +
 include/hw/iommu.h   | 25 +++++++++++++++++++++++++
 include/hw/pci/pci.h | 22 ++++++++++++++++++++++
 hw/pci/pci.c         | 11 +++++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 include/hw/iommu.h
MAINTAINERS +1 -0
index 36eef27b41..d94fbcbdfb 100644
@@ -2338,6 +2338,7 @@ F: include/system/iommufd.h
 F: backends/host_iommu_device.c
 F: include/system/host_iommu_device.h
 F: include/qemu/chardev_open.h
+F: include/hw/iommu.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
 
include/hw/iommu.h new file +25 -0
new file mode 100644
index 0000000000..9b8bb94fc2
@@ -0,0 +1,25 @@
+/*
+ * General vIOMMU flags
+ *
+ * Copyright (C) 2025 Intel Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_IOMMU_H
+#define HW_IOMMU_H
+
+#include "qemu/bitops.h"
+
+/*
+ * Theoretical vIOMMU flags. Only determined by the vIOMMU device properties and
+ * independent on the actual host IOMMU capabilities they may depend on. Each
+ * flag can be an expectation or request to other sub-system or just a pure
+ * vIOMMU capability. vIOMMU can choose which flags to expose.
+ */
+enum viommu_flags {
+    /* vIOMMU needs nesting parent HWPT to create nested HWPT */
+    VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
+};
+
+#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +22 -0
index bde9dca8e2..cf99b5bb68 100644
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {
      * @devfn: device and function number of the PCI device.
      */
     void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @get_viommu_flags: get vIOMMU flags
+     *
+     * Optional callback, if not implemented, then vIOMMU doesn't support
+     * exposing flags to other sub-system, e.g., VFIO.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * Returns: bitmap with each representing a vIOMMU flag defined in
+     * enum viommu_flags.
+     */
+    uint64_t (*get_viommu_flags)(void *opaque);
     /**
      * @get_iotlb_info: get properties required to initialize a device IOTLB.
      *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                  Error **errp);
 void pci_device_unset_iommu_device(PCIDevice *dev);
 
+/**
+ * pci_device_get_viommu_flags: get vIOMMU flags.
+ *
+ * Returns: bitmap with each representing a vIOMMU flag defined in
+ * enum viommu_flags. Or 0 if vIOMMU doesn't report any.
+ *
+ * @dev: PCI device pointer.
+ */
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev);
+
 /**
  * pci_iommu_get_iotlb_info: get properties required to initialize a
  * device IOTLB.
hw/pci/pci.c +11 -0
index d0e81651aa..c9932c87e3 100644
@@ -3010,6 +3010,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev)
     }
 }
 
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) {
+        return iommu_bus->iommu_ops->get_viommu_flags(iommu_bus->iommu_opaque);
+    }
+    return 0;
+}
+
 int pci_pri_request_page(PCIDevice *dev, uint32_t pasid, bool priv_req,
                          bool exec_req, hwaddr addr, bool lpig,
                          uint16_t prgi, bool is_read, bool is_write)
Signature · 1 line
2.47.1
Reviewed-by on v7 5/23
#8 lore raw

Re: [PATCH v7 05/23] hw/pci: Introduce pci_device_get_viommu_flags()

Quoted 32 lines · Zhenzhong Duan

Introduce a new PCIIOMMUOps optional callback, get_viommu_flags() which allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.

pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.

get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:

1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.

Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU through the set_iommu_device() callback that will be after the attach_device().

See below sequence:

vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)

Quoted 8 lines
---
  MAINTAINERS          |  1 +
  include/hw/iommu.h   | 25 +++++++++++++++++++++++++

Hmm, why not under include/hw/pci/ ? Was this discussed ?

Thanks,

C.

Quoted 110 lines
  include/hw/pci/pci.h | 22 ++++++++++++++++++++++
  hw/pci/pci.c         | 11 +++++++++++
  4 files changed, 59 insertions(+)

create mode 100644 include/hw/iommu.h

MAINTAINERS +1 -0
index 36eef27b41..d94fbcbdfb 100644
@@ -2338,6 +2338,7 @@ F: include/system/iommufd.h
  F: backends/host_iommu_device.c
  F: include/system/host_iommu_device.h
  F: include/qemu/chardev_open.h
+F: include/hw/iommu.h
  F: util/chardev_open.c
  F: docs/devel/vfio-iommufd.rst
 
include/hw/iommu.h new file +25 -0
new file mode 100644
index 0000000000..9b8bb94fc2
@@ -0,0 +1,25 @@
+/*
+ * General vIOMMU flags
+ *
+ * Copyright (C) 2025 Intel Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_IOMMU_H
+#define HW_IOMMU_H
+
+#include "qemu/bitops.h"
+
+/*
+ * Theoretical vIOMMU flags. Only determined by the vIOMMU device properties and
+ * independent on the actual host IOMMU capabilities they may depend on. Each
+ * flag can be an expectation or request to other sub-system or just a pure
+ * vIOMMU capability. vIOMMU can choose which flags to expose.
+ */
+enum viommu_flags {
+    /* vIOMMU needs nesting parent HWPT to create nested HWPT */
+    VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
+};
+
+#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +22 -0
index bde9dca8e2..cf99b5bb68 100644
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {
       * @devfn: device and function number of the PCI device.
       */
      void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @get_viommu_flags: get vIOMMU flags
+     *
+     * Optional callback, if not implemented, then vIOMMU doesn't support
+     * exposing flags to other sub-system, e.g., VFIO.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * Returns: bitmap with each representing a vIOMMU flag defined in
+     * enum viommu_flags.
+     */
+    uint64_t (*get_viommu_flags)(void *opaque);
      /**
       * @get_iotlb_info: get properties required to initialize a device IOTLB.
       *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                   Error **errp);
  void pci_device_unset_iommu_device(PCIDevice *dev);
 
+/**
+ * pci_device_get_viommu_flags: get vIOMMU flags.
+ *
+ * Returns: bitmap with each representing a vIOMMU flag defined in
+ * enum viommu_flags. Or 0 if vIOMMU doesn't report any.
+ *
+ * @dev: PCI device pointer.
+ */
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev);
+
  /**
   * pci_iommu_get_iotlb_info: get properties required to initialize a
   * device IOTLB.
hw/pci/pci.c +11 -0
index d0e81651aa..c9932c87e3 100644
@@ -3010,6 +3010,17 @@ void pci_device_unset_iommu_device(PCIDevice *dev)
      }
  }
 
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) {
+        return iommu_bus->iommu_ops->get_viommu_flags(iommu_bus->iommu_opaque);
+    }
+    return 0;
+}
+
  int pci_pri_request_page(PCIDevice *dev, uint32_t pasid, bool priv_req,
                           bool exec_req, hwaddr addr, bool lpig,
                           uint16_t prgi, bool is_read, bool is_write)
#9 lore raw

RE: [PATCH v7 05/23] hw/pci: Introduce pci_device_get_viommu_flags()

Quoted 52 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 05/23] hw/pci: Introduce
pci_device_get_viommu_flags()

Quoted 1 line · Zhenzhong Duan

Introduce a new PCIIOMMUOps optional callback, get_viommu_flags()

which

Quoted 12 lines

allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.

pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.

get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:

Quoted 6 lines

1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.

Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU

through

Quoted 13 lines

the set_iommu_device() callback that will be after the attach_device().

See below sequence:

vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)

Quoted 8 lines
---
  MAINTAINERS          |  1 +
  include/hw/iommu.h   | 25 +++++++++++++++++++++++++
Quoted 1 line

Hmm, why not under include/hw/pci/ ?

I'm not sure if it's better to restrict iommu to pci subsystem. I have a vague memory there is iommu supporting non-PCI devices.

Quoted 1 line

Was this discussed ?

No.

Thanks
Zhenzhong

Quoted 127 lines

Thanks,

C.

Quoted 28 lines
  include/hw/pci/pci.h | 22 ++++++++++++++++++++++
  hw/pci/pci.c         | 11 +++++++++++
  4 files changed, 59 insertions(+)

create mode 100644 include/hw/iommu.h

MAINTAINERS +1 -0
index 36eef27b41..d94fbcbdfb 100644
@@ -2338,6 +2338,7 @@ F: include/system/iommufd.h
  F: backends/host_iommu_device.c
  F: include/system/host_iommu_device.h
  F: include/qemu/chardev_open.h
+F: include/hw/iommu.h
  F: util/chardev_open.c
  F: docs/devel/vfio-iommufd.rst
 
include/hw/iommu.h new file +5 -0
new file mode 100644
index 0000000000..9b8bb94fc2
@@ -0,0 +1,25 @@
+/*
+ * General vIOMMU flags
+ *
+ * Copyright (C) 2025 Intel Corporation.
+ *

include/hw/iommu.h line 15

Quoted 10 lines
include/hw/iommu.h new file +10 -0
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_IOMMU_H
+#define HW_IOMMU_H
+
+#include "qemu/bitops.h"
+
+/*
+ * Theoretical vIOMMU flags. Only determined by the vIOMMU device

properties and

include/hw/iommu.h line 16

Quoted 1 line
include/hw/iommu.h new file +1 -0
+ * independent on the actual host IOMMU capabilities they may depend on.

Each

Quoted 14 lines
include/hw/iommu.h new file +9 -0
+ * flag can be an expectation or request to other sub-system or just a pure
+ * vIOMMU capability. vIOMMU can choose which flags to expose.
+ */
+enum viommu_flags {
+    /* vIOMMU needs nesting parent HWPT to create nested HWPT */
+    VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
+};
+
+#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +0 -0
index bde9dca8e2..cf99b5bb68 100644

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 468

Quoted 7 lines
include/hw/pci/pci.h +4 -0
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {
       * @devfn: device and function number of the PCI device.
       */
      void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @get_viommu_flags: get vIOMMU flags
+     *
+     * Optional callback, if not implemented, then vIOMMU doesn't

support

include/hw/pci/pci.h typedef struct PCIIOMMUOps {

Quoted 10 lines
include/hw/pci/pci.h +8 -0
+     * exposing flags to other sub-system, e.g., VFIO.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * Returns: bitmap with each representing a vIOMMU flag defined in
+     * enum viommu_flags.
+     */
+    uint64_t (*get_viommu_flags)(void *opaque);
      /**
       * @get_iotlb_info: get properties required to initialize a device

IOTLB.

include/hw/pci/pci.h bool pci_device_set_iommu_device(PCIDevice

Quoted 2 lines
include/hw/pci/pci.h +0 -0
       *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice

*dev, HostIOMMUDevice *hiod,

hw/pci/pci.c void

Quoted 21 lines
include/hw/pci/pci.h +10 -0
                                   Error **errp);
  void pci_device_unset_iommu_device(PCIDevice *dev);
 
+/**
+ * pci_device_get_viommu_flags: get vIOMMU flags.
+ *
+ * Returns: bitmap with each representing a vIOMMU flag defined in
+ * enum viommu_flags. Or 0 if vIOMMU doesn't report any.
+ *
+ * @dev: PCI device pointer.
+ */
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev);
+
  /**
   * pci_iommu_get_iotlb_info: get properties required to initialize a
   * device IOTLB.
hw/pci/pci.c +0 -0
index d0e81651aa..c9932c87e3 100644
@@ -3010,6 +3010,17 @@ void

pci_device_unset_iommu_device(PCIDevice *dev)

hw/pci/pci.c void pci_device_unset_iommu_device(PCIDevice *dev)

Quoted 10 lines
hw/pci/pci.c +7 -0
      }
  }
 
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) {
+        return

iommu_bus->iommu_ops->get_viommu_flags(iommu_bus->iommu_opaque);

Quoted 7 lines
hw/pci/pci.c +4 -0
+    }
+    return 0;
+}
+
  int pci_pri_request_page(PCIDevice *dev, uint32_t pasid, bool priv_req,
                           bool exec_req, hwaddr addr, bool lpig,
                           uint16_t prgi, bool is_read, bool is_write)
#10 lore raw

Re: [PATCH v7 05/23] hw/pci: Introduce pci_device_get_viommu_flags()

Quoted 50 lines · Zhenzhong
Quoted 50 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 05/23] hw/pci: Introduce
pci_device_get_viommu_flags()

Quoted 1 line · Zhenzhong Duan

Introduce a new PCIIOMMUOps optional callback, get_viommu_flags()

which

Quoted 12 lines

allows to retrieve flags exposed by a vIOMMU. The first planned vIOMMU device flag is VIOMMU_FLAG_WANT_NESTING_PARENT that advertises the support of HW nested stage translation scheme and wants other sub-system like VFIO's cooperation to create nesting parent HWPT.

pci_device_get_viommu_flags() is a wrapper that can be called on a PCI device potentially protected by a vIOMMU.

get_viommu_flags() is designed to return 64bit bitmap of purely vIOMMU flags which are only determined by user's configuration, no host capabilities involved. Reasons are:

Quoted 6 lines

1. host may has heterogeneous IOMMUs, each with different capabilities
2. this is migration friendly, return value is consistent between source and target.

Note that this op will be invoked at the attach_device() stage, at which point host IOMMU capabilities are not yet forwarded to the vIOMMU

through

Quoted 21 lines

the set_iommu_device() callback that will be after the attach_device().

See below sequence:

vfio_device_attach():
iommufd_cdev_attach():
pci_device_get_viommu_flags() for HW nesting cap create a nesting parent HWPT
attach device to the HWPT vfio_device_hiod_create_and_realize() creating hiod
...
pci_device_set_iommu_device(hiod)

---
  MAINTAINERS          |  1 +
  include/hw/iommu.h   | 25 +++++++++++++++++++++++++
Quoted 3 lines
Quoted 1 line

Hmm, why not under include/hw/pci/ ?

I'm not sure if it's better to restrict iommu to pci subsystem. I have a vague memory there is iommu supporting non-PCI devices.

effectively on ARM we may need to support SMMU for platform devices too

Eric

Quoted 133 lines
Quoted 2 lines

Was this discussed ?

No.

Thanks
Zhenzhong

Quoted 126 lines

Thanks,

C.

Quoted 28 lines
  include/hw/pci/pci.h | 22 ++++++++++++++++++++++
  hw/pci/pci.c         | 11 +++++++++++
  4 files changed, 59 insertions(+)

create mode 100644 include/hw/iommu.h

MAINTAINERS +1 -0
index 36eef27b41..d94fbcbdfb 100644
@@ -2338,6 +2338,7 @@ F: include/system/iommufd.h
  F: backends/host_iommu_device.c
  F: include/system/host_iommu_device.h
  F: include/qemu/chardev_open.h
+F: include/hw/iommu.h
  F: util/chardev_open.c
  F: docs/devel/vfio-iommufd.rst
 
include/hw/iommu.h new file +5 -0
new file mode 100644
index 0000000000..9b8bb94fc2
@@ -0,0 +1,25 @@
+/*
+ * General vIOMMU flags
+ *
+ * Copyright (C) 2025 Intel Corporation.
+ *

include/hw/iommu.h line 15

Quoted 10 lines
include/hw/iommu.h new file +10 -0
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_IOMMU_H
+#define HW_IOMMU_H
+
+#include "qemu/bitops.h"
+
+/*
+ * Theoretical vIOMMU flags. Only determined by the vIOMMU device

properties and

include/hw/iommu.h line 16

Quoted 1 line
include/hw/iommu.h new file +1 -0
+ * independent on the actual host IOMMU capabilities they may depend on.

Each

Quoted 14 lines
include/hw/iommu.h new file +9 -0
+ * flag can be an expectation or request to other sub-system or just a pure
+ * vIOMMU capability. vIOMMU can choose which flags to expose.
+ */
+enum viommu_flags {
+    /* vIOMMU needs nesting parent HWPT to create nested HWPT */
+    VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
+};
+
+#endif /* HW_IOMMU_H */
include/hw/pci/pci.h +0 -0
index bde9dca8e2..cf99b5bb68 100644

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 468

Quoted 7 lines
include/hw/pci/pci.h +4 -0
@@ -462,6 +462,18 @@ typedef struct PCIIOMMUOps {
       * @devfn: device and function number of the PCI device.
       */
      void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @get_viommu_flags: get vIOMMU flags
+     *
+     * Optional callback, if not implemented, then vIOMMU doesn't

support

include/hw/pci/pci.h typedef struct PCIIOMMUOps {

Quoted 10 lines
include/hw/pci/pci.h +8 -0
+     * exposing flags to other sub-system, e.g., VFIO.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * Returns: bitmap with each representing a vIOMMU flag defined in
+     * enum viommu_flags.
+     */
+    uint64_t (*get_viommu_flags)(void *opaque);
      /**
       * @get_iotlb_info: get properties required to initialize a device

IOTLB.

include/hw/pci/pci.h bool pci_device_set_iommu_device(PCIDevice

Quoted 2 lines
include/hw/pci/pci.h +0 -0
       *
@@ -644,6 +656,16 @@ bool pci_device_set_iommu_device(PCIDevice

*dev, HostIOMMUDevice *hiod,

hw/pci/pci.c void

Quoted 21 lines
include/hw/pci/pci.h +10 -0
                                   Error **errp);
  void pci_device_unset_iommu_device(PCIDevice *dev);
 
+/**
+ * pci_device_get_viommu_flags: get vIOMMU flags.
+ *
+ * Returns: bitmap with each representing a vIOMMU flag defined in
+ * enum viommu_flags. Or 0 if vIOMMU doesn't report any.
+ *
+ * @dev: PCI device pointer.
+ */
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev);
+
  /**
   * pci_iommu_get_iotlb_info: get properties required to initialize a
   * device IOTLB.
hw/pci/pci.c +0 -0
index d0e81651aa..c9932c87e3 100644
@@ -3010,6 +3010,17 @@ void

pci_device_unset_iommu_device(PCIDevice *dev)

hw/pci/pci.c void pci_device_unset_iommu_device(PCIDevice *dev)

Quoted 10 lines
hw/pci/pci.c +7 -0
      }
  }
 
+uint64_t pci_device_get_viommu_flags(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->get_viommu_flags) {
+        return

iommu_bus->iommu_ops->get_viommu_flags(iommu_bus->iommu_opaque);

Quoted 7 lines
hw/pci/pci.c +4 -0
+    }
+    return 0;
+}
+
  int pci_pri_request_page(PCIDevice *dev, uint32_t pasid, bool priv_req,
                           bool exec_req, hwaddr addr, bool lpig,
                           uint16_t prgi, bool is_read, bool is_write)
#11 lore raw

PATCH v7 6/23 intel_iommu: Implement get_viommu_flags() callback

In reply to Zhenzhong Duan

Implement get_viommu_flags() callback and expose a request for nesting parent HWPT for now.

VFIO uses it to create nesting parent HWPT which is further used to create nested HWPT in vIOMMU. All these will be implemented in following patches.

---
 hw/i386/intel_iommu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/i386/intel_iommu.c +12 -0
index d6a4e21972..db5be065bf 100644
@@ -24,6 +24,7 @@
 #include "qemu/main-loop.h"
 #include "qapi/error.h"
 #include "hw/sysbus.h"
+#include "hw/iommu.h"
 #include "intel_iommu_internal.h"
 #include "hw/pci/pci.h"
 #include "hw/pci/pci_bus.h"
@@ -4704,6 +4705,16 @@ static void vtd_address_space_unmap_in_migration(VTDAddressSpace *as,
     }
 }
 
+static uint64_t vtd_get_viommu_flags(void *opaque)
+{
+    IntelIOMMUState *s = opaque;
+    uint64_t flags;
+
+    flags = s->fsts ? VIOMMU_FLAG_WANT_NESTING_PARENT : 0;
+
+    return flags;
+}
+
 /* Unmap the whole range in the notifier's scope. */
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
 {
@@ -5362,6 +5373,7 @@ static PCIIOMMUOps vtd_iommu_ops = {
     .pri_register_notifier = vtd_pri_register_notifier,
     .pri_unregister_notifier = vtd_pri_unregister_notifier,
     .pri_request_page = vtd_pri_request_page,
+    .get_viommu_flags = vtd_get_viommu_flags,
 };
 
 static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
Signature · 1 line
2.47.1
#12 lore raw

PATCH v7 7/23 intel_iommu: Introduce a new structure VTDHostIOMMUDevice

In reply to Zhenzhong Duan

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.

---
 hw/i386/intel_iommu_internal.h |  7 +++++++
 include/hw/i386/intel_iommu.h  |  2 +-
 hw/i386/intel_iommu.c          | 15 +++++++++++++--
 3 files changed, 21 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +7 -0
index ba0f1f5096..09edba81e2 100644
@@ -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
@@ -677,4 +678,10 @@ typedef struct VTDPASIDCacheInfo {
 /* Bits to decide the offset for each level */
 #define VTD_LEVEL_BITS           9
 
+typedef struct VTDHostIOMMUDevice {
+    IntelIOMMUState *iommu_state;
+    PCIBus *bus;
+    uint8_t devfn;
+    HostIOMMUDevice *hiod;
+} VTDHostIOMMUDevice;
 #endif
include/hw/i386/intel_iommu.h +1 -1
index a84d6965a4..3758ac239c 100644
@@ -302,7 +302,7 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
-    GHashTable *vtd_host_iommu_dev;             /* HostIOMMUDevice */
+    GHashTable *vtd_host_iommu_dev;             /* VTDHostIOMMUDevice */
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
hw/i386/intel_iommu.c +13 -2
index db5be065bf..4c83578c54 100644
@@ -291,7 +291,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,
@@ -4610,6 +4613,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,
@@ -4632,7 +4636,14 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
         return false;
     }
 
+    vtd_hiod = g_malloc0(sizeof(VTDHostIOMMUDevice));
+    vtd_hiod->bus = bus;
+    vtd_hiod->devfn = (uint8_t)devfn;
+    vtd_hiod->iommu_state = s;
+    vtd_hiod->hiod = hiod;
+
     if (!vtd_check_hiod(s, hiod, errp)) {
+        g_free(vtd_hiod);
         vtd_iommu_unlock(s);
         return false;
     }
@@ -4642,7 +4653,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     new_key->devfn = devfn;
 
     object_ref(hiod);
-    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, hiod);
+    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, vtd_hiod);
 
     vtd_iommu_unlock(s);
 
Signature · 1 line
2.47.1
#13 lore raw

PATCH v7 8/23 vfio/iommufd: Force creating nesting parent HWPT

In reply to Zhenzhong Duan

Call pci_device_get_viommu_flags() to get if vIOMMU supports VIOMMU_FLAG_WANT_NESTING_PARENT.

If yes, create a nesting parent HWPT and add it to the container's hwpt_list, letting this parent HWPT cover the entire second stage mappings (GPA=>HPA).

This allows a VFIO passthrough device to directly attach to this default HWPT and then to use the system address space and its listener.

Introduce a vfio_device_get_viommu_flags_want_nesting() helper to facilitate this implementation.

It is safe to do so because a vIOMMU will be able to fail in set_iommu_device() call, if something else related to the VFIO device or vIOMMU isn't compatible.

---
 include/hw/vfio/vfio-device.h |  2 ++
 hw/vfio/device.c              | 12 ++++++++++++
 hw/vfio/iommufd.c             |  9 +++++++++
 3 files changed, 23 insertions(+)
include/hw/vfio/vfio-device.h +2 -0
index a0b8fc2eb6..48d00c7bc4 100644
@@ -267,6 +267,8 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,
 
 void vfio_device_unprepare(VFIODevice *vbasedev);
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev);
+
 int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                 struct vfio_region_info **info);
 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
hw/vfio/device.c +12 -0
index 5ed3103e72..be94947623 100644
@@ -23,6 +23,7 @@
 
 #include "hw/vfio/vfio-device.h"
 #include "hw/vfio/pci.h"
+#include "hw/iommu.h"
 #include "hw/hw.h"
 #include "trace.h"
 #include "qapi/error.h"
@@ -521,6 +522,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
     vbasedev->bcontainer = NULL;
 }
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)
+{
+    VFIOPCIDevice *vdev = vfio_pci_from_vfio_device(vbasedev);
+
+    if (vdev) {
+        return !!(pci_device_get_viommu_flags(&vdev->parent_obj) &
+                  VIOMMU_FLAG_WANT_NESTING_PARENT);
+    }
+    return false;
+}
+
 /*
  * Traditional ioctl() based io
  */
hw/vfio/iommufd.c +9 -0
index 8de765c769..f9d0926274 100644
@@ -404,6 +404,15 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
     }
 
+    /*
+     * If vIOMMU requests VFIO's cooperation to create nesting parent HWPT,
+     * force to create it so that it could be reused by vIOMMU to create
+     * nested HWPT.
+     */
+    if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+    }
+
     if (cpr_is_incoming()) {
         hwpt_id = vbasedev->cpr.hwpt_id;
         goto skip_alloc;
Signature · 1 line
2.47.1
#14 lore raw

Re: [PATCH v7 08/23] vfio/iommufd: Force creating nesting parent HWPT

Quoted 53 lines · Zhenzhong Duan

Call pci_device_get_viommu_flags() to get if vIOMMU supports VIOMMU_FLAG_WANT_NESTING_PARENT.

If yes, create a nesting parent HWPT and add it to the container's hwpt_list, letting this parent HWPT cover the entire second stage mappings (GPA=>HPA).

This allows a VFIO passthrough device to directly attach to this default HWPT and then to use the system address space and its listener.

Introduce a vfio_device_get_viommu_flags_want_nesting() helper to facilitate this implementation.

It is safe to do so because a vIOMMU will be able to fail in set_iommu_device() call, if something else related to the VFIO device or vIOMMU isn't compatible.

---
  include/hw/vfio/vfio-device.h |  2 ++
  hw/vfio/device.c              | 12 ++++++++++++
  hw/vfio/iommufd.c             |  9 +++++++++
  3 files changed, 23 insertions(+)
include/hw/vfio/vfio-device.h +2 -0
index a0b8fc2eb6..48d00c7bc4 100644
@@ -267,6 +267,8 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,
 
  void vfio_device_unprepare(VFIODevice *vbasedev);
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev);
+
  int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                  struct vfio_region_info **info);
  int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
hw/vfio/device.c +1 -0
index 5ed3103e72..be94947623 100644
@@ -23,6 +23,7 @@
 
  #include "hw/vfio/vfio-device.h"
  #include "hw/vfio/pci.h"
+#include "hw/iommu.h"
  #include "hw/hw.h"
  #include "trace.h"
  #include "qapi/error.h"

hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 530

Quoted 9 lines
hw/vfio/device.c +6 -0
@@ -521,6 +522,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
      vbasedev->bcontainer = NULL;
  }
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)
+{
+    VFIOPCIDevice *vdev = vfio_pci_from_vfio_device(vbasedev);
+
+    if (vdev) {
+        return !!(pci_device_get_viommu_flags(&vdev->parent_obj) &

Using PCI_DEVICE(vdev) would be more appropriate. It can come later.

Thanks,

C.

Quoted 28 lines
hw/vfio/device.c +5 -0
+                  VIOMMU_FLAG_WANT_NESTING_PARENT);
+    }
+    return false;
+}
+
  /*
   * Traditional ioctl() based io
   */
hw/vfio/iommufd.c +9 -0
index 8de765c769..f9d0926274 100644
@@ -404,6 +404,15 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
          flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
      }
 
+    /*
+     * If vIOMMU requests VFIO's cooperation to create nesting parent HWPT,
+     * force to create it so that it could be reused by vIOMMU to create
+     * nested HWPT.
+     */
+    if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+    }
+
      if (cpr_is_incoming()) {
          hwpt_id = vbasedev->cpr.hwpt_id;
          goto skip_alloc;
#15 lore raw

RE: [PATCH v7 08/23] vfio/iommufd: Force creating nesting parent HWPT

Quoted 74 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 08/23] vfio/iommufd: Force creating nesting parent HWPT

Quoted 5 lines · Zhenzhong Duan

Call pci_device_get_viommu_flags() to get if vIOMMU supports VIOMMU_FLAG_WANT_NESTING_PARENT.

If yes, create a nesting parent HWPT and add it to the container's hwpt_list, letting this parent HWPT cover the entire second stage mappings

(GPA=>HPA).

Quoted 2 lines

This allows a VFIO passthrough device to directly attach to this default

HWPT

Quoted 3 lines

and then to use the system address space and its listener.

Introduce a vfio_device_get_viommu_flags_want_nesting() helper to

facilitate

Quoted 3 lines

this implementation.

It is safe to do so because a vIOMMU will be able to fail in

set_iommu_device()

Quoted 1 line

call, if something else related to the VFIO device or vIOMMU isn't

compatible.

include/hw/vfio/vfio-device.h void vfio_device_prepare(VFIODevice *vbasedev,

Quoted 18 lines
---
  include/hw/vfio/vfio-device.h |  2 ++
  hw/vfio/device.c              | 12 ++++++++++++
  hw/vfio/iommufd.c             |  9 +++++++++
  3 files changed, 23 insertions(+)
include/hw/vfio/vfio-device.h +0 -0
index a0b8fc2eb6..48d00c7bc4 100644
@@ -267,6 +267,8 @@ void vfio_device_prepare(VFIODevice *vbasedev,

VFIOContainer *bcontainer,

include/hw/vfio/vfio-device.h void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,

Quoted 4 lines
include/hw/vfio/vfio-device.h +1 -0
  void vfio_device_unprepare(VFIODevice *vbasedev);
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice

*vbasedev);

include/hw/vfio/vfio-device.h void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,

Quoted 4 lines
include/hw/vfio/vfio-device.h +1 -0
+
  int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                  struct vfio_region_info **info);
  int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t

type,

hw/vfio/device.c void vfio_device_unprepare(VFIODevice

Quoted 13 lines
hw/vfio/device.c +1 -0
index 5ed3103e72..be94947623 100644
@@ -23,6 +23,7 @@
 
  #include "hw/vfio/vfio-device.h"
  #include "hw/vfio/pci.h"
+#include "hw/iommu.h"
  #include "hw/hw.h"
  #include "trace.h"
  #include "qapi/error.h"
@@ -521,6 +522,17 @@ void vfio_device_unprepare(VFIODevice

*vbasedev)

hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev)

Quoted 4 lines
hw/vfio/device.c +1 -0
      vbasedev->bcontainer = NULL;
  }
 
+bool vfio_device_get_viommu_flags_want_nesting(VFIODevice
Quoted 8 lines

*vbasedev)

hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 530

Quoted 5 lines
hw/vfio/device.c +5 -0
+{
+    VFIOPCIDevice *vdev = vfio_pci_from_vfio_device(vbasedev);
+
+    if (vdev) {
+        return !!(pci_device_get_viommu_flags(&vdev->parent_obj) &

Using PCI_DEVICE(vdev) would be more appropriate. It can come later.

Yes, will do.

Thanks
Zhenzhong

#16 lore raw

PATCH v7 9/23 intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

In reply to Zhenzhong Duan

When guest enables scalable mode and setup first stage page table, we don't want to use IOMMU MR but rather continue using the system MR for IOMMUFD backed host device.

Then default HWPT in VFIO contains GPA->HPA mappings which could be reused as nesting parent HWPT to construct nested HWPT in vIOMMU.

---
 hw/i386/intel_iommu.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +34 -2
index 4c83578c54..ce4c54165e 100644
@@ -41,6 +41,7 @@
 #include "migration/misc.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define PASID_0    0
@@ -1713,6 +1714,24 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
 
 }
 
+static VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
+{
+    IntelIOMMUState *s = as->iommu_state;
+    struct vtd_as_key key = {
+        .bus = as->bus,
+        .devfn = as->devfn,
+    };
+    VTDHostIOMMUDevice *vtd_hiod = g_hash_table_lookup(s->vtd_host_iommu_dev,
+                                                       &key);
+
+    if (vtd_hiod && vtd_hiod->hiod &&
+        object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                            TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+        return vtd_hiod;
+    }
+    return NULL;
+}
+
 static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 {
     IntelIOMMUState *s;
@@ -1738,12 +1757,25 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 /* Return whether the device is using IOMMU translation. */
 static bool vtd_switch_address_space(VTDAddressSpace *as)
 {
+    IntelIOMMUState *s;
     bool use_iommu, pt;
 
     assert(as);
 
-    use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as);
-    pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);
+    s = as->iommu_state;
+    use_iommu = s->dmar_enabled && !vtd_as_pt_enabled(as);
+    pt = s->dmar_enabled && vtd_as_pt_enabled(as);
+
+    /*
+     * When guest enables scalable mode and sets up first stage page table,
+     * we stick to system MR for IOMMUFD backed host device. Then its
+     * default hwpt contains GPA->HPA mappings which is used directly if
+     * PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall back
+     * to original processing.
+     */
+    if (s->root_scalable && s->fsts && vtd_find_hiod_iommufd(as)) {
+        use_iommu = false;
+    }
 
     trace_vtd_switch_address_space(pci_bus_num(as->bus),
                                    VTD_PCI_SLOT(as->devfn),
Signature · 1 line
2.47.1
#17 lore raw

Re: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Hi Zhenzhong,

Quoted 6 lines · Zhenzhong Duan

When guest enables scalable mode and setup first stage page table, we don't want to use IOMMU MR but rather continue using the system MR for IOMMUFD backed host device.

Then default HWPT in VFIO contains GPA->HPA mappings which could be reused as nesting parent HWPT to construct nested HWPT in vIOMMU.

we had a discussion thread with Nicolin and Shameer about usage of AS for nested SMMU
(https://lore.kernel.org/all/[email protected]/)
If I understand correctly you also rely on system MR for nested. I am not sure this is a good usage of the API/AS objects as in practice you have an actual translation in place (althout implemented by HW) while by using the system MR you do not reflect that. I encouraged Shameer to try using a dummy dedicated AS that can be shared. I think it would be better if we could align the strategies.

Quoted 57 lines
---
 hw/i386/intel_iommu.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +20 -2
index 4c83578c54..ce4c54165e 100644
@@ -41,6 +41,7 @@
 #include "migration/misc.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define PASID_0    0
@@ -1713,6 +1714,24 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
 
 }
 
+static VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
+{
+    IntelIOMMUState *s = as->iommu_state;
+    struct vtd_as_key key = {
+        .bus = as->bus,
+        .devfn = as->devfn,
+    };
+    VTDHostIOMMUDevice *vtd_hiod = g_hash_table_lookup(s->vtd_host_iommu_dev,
+                                                       &key);
+
+    if (vtd_hiod && vtd_hiod->hiod &&
+        object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                            TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+        return vtd_hiod;
+    }
+    return NULL;
+}
+
 static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 {
     IntelIOMMUState *s;
@@ -1738,12 +1757,25 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 /* Return whether the device is using IOMMU translation. */
 static bool vtd_switch_address_space(VTDAddressSpace *as)
 {
+    IntelIOMMUState *s;
     bool use_iommu, pt;
 
     assert(as);
 
-    use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as);
-    pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);

hw/i386/intel_iommu.c static bool vtd_as_pt_enabled(VTDAddressSpace *as) · line 1774

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    s = as->iommu_state;
+    use_iommu = s->dmar_enabled && !vtd_as_pt_enabled(as);
+    pt = s->dmar_enabled && vtd_as_pt_enabled(as);
+
+    /*
+     * When guest enables scalable mode and sets up first stage page table,
+     * we stick to system MR for IOMMUFD backed host device. Then its
+     * default hwpt contains GPA->HPA mappings which is used directly if
+     * PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall back
+     * to original processing.

According to the above comment you have a S1 translation in place but you set use_iommu = false and use system MR?

Revoking my R-bs for now because I am not convinced we shall use system MR when S1+S2 is setup. I may be wrong but at least I need more explanations ;-)

Eric

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
+     */
+    if (s->root_scalable && s->fsts && vtd_find_hiod_iommufd(as)) {
+        use_iommu = false;
+    }
 
     trace_vtd_switch_address_space(pci_bus_num(as->bus),
                                    VTD_PCI_SLOT(as->devfn),
#18 lore raw

RE: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Hi Eric,

Quoted 18 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Hi Zhenzhong,

Quoted 1 line · Zhenzhong Duan

When guest enables scalable mode and setup first stage page table, we

don't

Quoted 1 line

want to use IOMMU MR but rather continue using the system MR for

IOMMUFD

Quoted 3 lines

backed host device.

Then default HWPT in VFIO contains GPA->HPA mappings which could be

reused

Quoted 1 line

as nesting parent HWPT to construct nested HWPT in vIOMMU.

Quoted 10 lines

we had a discussion thread with Nicolin and Shameer about usage of AS for nested SMMU
(https://lore.kernel.org/all/add07edd-3652-430d-b52c-cb2bdbc7f587@redha
t.com/)
If I understand correctly you also rely on system MR for nested. I am not sure this is a good usage of the API/AS objects as in practice you have an actual translation in place (althout implemented by HW) while by using the system MR you do not reflect that. I encouraged Shameer to try using a dummy dedicated AS that can be shared. I think it would be better if we could align the strategies.

Hmm, I think it's hard for VTD to use dedicated AS just like smmu, because VTD supports legacy mode even with 'x-scalable-mode=on,x-flts=on', we don't know guest's choice at runtime. So we always return IOMMU AS to VFIO, we should never return address_space_memory or a dedicated AS in vtd_find_add_as().

There was a discussion with Nicolin and Liuyi on this.

https://lore.kernel.org/qemu-devel/SJ0PR11MB6744340B889FF65D3BD5B8459267A@SJ0PR11MB6744.namprd11.prod.outlook.com/

Quoted 70 lines

hw/i386/intel_iommu.c static bool

Quoted 23 lines
---
 hw/i386/intel_iommu.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -0
index 4c83578c54..ce4c54165e 100644
@@ -41,6 +41,7 @@
 #include "migration/misc.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define PASID_0    0
@@ -1713,6 +1714,24 @@ static bool

vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,

hw/i386/intel_iommu.c static bool · line 1716

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
 }
 
+static VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace

*as)

hw/i386/intel_iommu.c static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,

Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+{
+    IntelIOMMUState *s = as->iommu_state;
+    struct vtd_as_key key = {
+        .bus = as->bus,
+        .devfn = as->devfn,
+    };
+    VTDHostIOMMUDevice *vtd_hiod =

g_hash_table_lookup(s->vtd_host_iommu_dev,

hw/i386/intel_iommu.c static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce, · line 1734

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                                                       &key);
+
+    if (vtd_hiod && vtd_hiod->hiod &&
+        object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+

TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +5 -0
+        return vtd_hiod;
+    }
+    return NULL;
+}
+
 static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 {
     IntelIOMMUState *s;
@@ -1738,12 +1757,25 @@ static bool

vtd_as_pt_enabled(VTDAddressSpace *as)

hw/i386/intel_iommu.c static bool vtd_as_pt_enabled(VTDAddressSpace *as)

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
 /* Return whether the device is using IOMMU translation. */
 static bool vtd_switch_address_space(VTDAddressSpace *as)
 {
+    IntelIOMMUState *s;
     bool use_iommu, pt;
 
     assert(as);
 
-    use_iommu = as->iommu_state->dmar_enabled

&& !vtd_as_pt_enabled(as);

hw/i386/intel_iommu.c static bool vtd_as_pt_enabled(VTDAddressSpace *as)

Quoted 7 lines
hw/i386/intel_iommu.c +6 -1
-    pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);
+    s = as->iommu_state;
+    use_iommu = s->dmar_enabled && !vtd_as_pt_enabled(as);
+    pt = s->dmar_enabled && vtd_as_pt_enabled(as);
+
+    /*
+     * When guest enables scalable mode and sets up first stage page
Quoted 8 lines

table,

hw/i386/intel_iommu.c static bool vtd_as_pt_enabled(VTDAddressSpace *as)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * we stick to system MR for IOMMUFD backed host device. Then its
+     * default hwpt contains GPA->HPA mappings which is used directly if
+     * PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall

back

hw/i386/intel_iommu.c static bool vtd_as_pt_enabled(VTDAddressSpace *as) · line 1774

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * to original processing.

According to the above comment you have a S1 translation in place but you set use_iommu = false and use system MR?

Yes, we have extended the usages of MRs under IOMMU AS with nesting. In nesting mode, system MR on/off isn't aligned with S1 translation anymore.

Quoted 4 lines

Revoking my R-bs for now because I am not convinced we shall use system MR when S1+S2 is setup. I may be wrong but at least I need more explanations ;-)

Okay, let's discuss further.

Thanks
Zhenzhong

#19 lore raw

Re: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Quoted 6 lines · Zhenzhong
Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    /*
+     * When guest enables scalable mode and sets up first stage page

table,

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * we stick to system MR for IOMMUFD backed host device. Then its
+     * default hwpt contains GPA->HPA mappings which is used directly if
+     * PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall
Quoted 7 lines
Quoted 4 lines

back

Quoted 1 line

+ * to original processing.

According to the above comment you have a S1 translation in place but you set use_iommu = false and use system MR?

Yes, we have extended the usages of MRs under IOMMU AS with nesting. In nesting mode, system MR on/off isn't aligned with S1 translation anymore.

Does Intel vIOMMU need S1 translation when the working in the HW accelerated mode? I thought everything is handled by the HW..no?

Nicolin

#20 lore raw

RE: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Quoted 21 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v7 09/23] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-flts=on

Quoted 4 lines · Zhenzhong
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    /*
+     * When guest enables scalable mode and sets up first stage page

table,

Quoted 1 line

+ * we stick to system MR for IOMMUFD backed host device. Then

its

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * default hwpt contains GPA->HPA mappings which is used

directly if

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

+ * PGTT=PT and used as nesting parent if PGTT=FST. Otherwise fall

back

Quoted 1 line

+ * to original processing.

According to the above comment you have a S1 translation in place but you set use_iommu = false and use system MR?

Quoted 6 lines
Quoted 2 lines

Yes, we have extended the usages of MRs under IOMMU AS with nesting. In nesting mode, system MR on/off isn't aligned with S1 translation

anymore.

Does Intel vIOMMU need S1 translation when the working in the HW accelerated mode? I thought everything is handled by the HW..no?

It needs S1 translation for emulated device. For passthrough device, yes.

#21 lore raw

PATCH v7 10/23 intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

In reply to Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, first stage page table is passed to host to construct nested page table for passthrough devices.

We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest first stage page table could be used by host.

For instance, vIOMMU supports first stage 1GB large page mapping, but host does not, then this IOMMUFD backed device should fail.

Even of the checks pass, for now we willingly reject the association because all the bits are not there yet, it will be relaxed in the end of this series.

Note vIOMMU has exposed IOMMU_HWPT_ALLOC_NEST_PARENT flag to force VFIO core to create nesting parent HWPT, if host doesn't support nested translation, the creation will fail. So no need to check nested capability here.

---
 hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +24 -1
index ce4c54165e..7d908cdb58 100644
@@ -4636,8 +4636,31 @@ 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 first stage 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 (s->fs1gp && !(vtd->cap_reg & VTD_CAP_FS1GP)) {
+        error_setg(errp,
+                   "First stage 1GB large page is unsupported by host IOMMU");
+        return false;
+    }
+#endif
+
     error_setg(errp,
-               "host device is uncompatible with first stage translation");
+               "host IOMMU is incompatible with guest first stage translation");
     return false;
 }
 
Signature · 1 line
2.47.1
#22 lore raw

Re: [PATCH v7 10/23] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Quoted 29 lines · Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, first stage page table is passed to host to construct nested page table for passthrough devices.

We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest first stage page table could be used by host.

For instance, vIOMMU supports first stage 1GB large page mapping, but host does not, then this IOMMUFD backed device should fail.

Even of the checks pass, for now we willingly reject the association because all the bits are not there yet, it will be relaxed in the end of this series.

Note vIOMMU has exposed IOMMU_HWPT_ALLOC_NEST_PARENT flag to force VFIO core to create nesting parent HWPT, if host doesn't support nested translation, the creation will fail. So no need to check nested capability here.

---
  hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +0 -0
index ce4c54165e..7d908cdb58 100644

hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod, · line 4639

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -4636,8 +4636,31 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
          return true;
      }
 
+#ifdef CONFIG_IOMMUFD

Before using CONFIG_IOMMUFD, '#include CONFIG_DEVICES' should be done first. But as said earlier, this is something we wanted to avoid in the intel-iommu model which can have different host IOMMU backends.

At first glance, it seems to me that these changes take the fast path and avoid an abstract layer. Is it too complex to keep on using HostIOMMUDeviceClass ?

Thanks,

C.

Quoted 28 lines
hw/i386/intel_iommu.c +23 -1
+    struct HostIOMMUDeviceCaps *caps = &hiod->caps;
+    struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+
+    /* Remaining checks are all first stage 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 (s->fs1gp && !(vtd->cap_reg & VTD_CAP_FS1GP)) {
+        error_setg(errp,
+                   "First stage 1GB large page is unsupported by host IOMMU");
+        return false;
+    }
+#endif
+
      error_setg(errp,
-               "host device is uncompatible with first stage translation");
+               "host IOMMU is incompatible with guest first stage translation");
      return false;
  }
 
#23 lore raw

RE: [PATCH v7 10/23] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Quoted 46 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 10/23] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Quoted 1 line · Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, first stage page

table

Quoted 3 lines

is passed to host to construct nested page table for passthrough devices.

We need to check compatibility of some critical IOMMU capabilities

between

Quoted 1 line

vIOMMU and host IOMMU to ensure guest first stage page table could be

used by

Quoted 3 lines

host.

For instance, vIOMMU supports first stage 1GB large page mapping, but

host does

Quoted 6 lines

not, then this IOMMUFD backed device should fail.

Even of the checks pass, for now we willingly reject the association because all the bits are not there yet, it will be relaxed in the end of this series.

Note vIOMMU has exposed IOMMU_HWPT_ALLOC_NEST_PARENT flag to

force VFIO core to

hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s,

Quoted 15 lines

create nesting parent HWPT, if host doesn't support nested translation, the creation will fail. So no need to check nested capability here.

---
  hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +0 -0
index ce4c54165e..7d908cdb58 100644
@@ -4636,8 +4636,31 @@ static bool vtd_check_hiod(IntelIOMMUState *s,

HostIOMMUDevice *hiod,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
          return true;
      }
 
Quoted 6 lines

hw/i386/intel_iommu.c static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod, · line 4639

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+#ifdef CONFIG_IOMMUFD

Before using CONFIG_IOMMUFD, '#include CONFIG_DEVICES' should be done first. But as said earlier, this is something we wanted to avoid in the intel-iommu model which can have different host IOMMU backends.

Ah, yes, should have '#include CONFIG_DEVICES' in this patch.

Quoted 4 lines

At first glance, it seems to me that these changes take the fast path and avoid an abstract layer. Is it too complex to keep on using HostIOMMUDeviceClass ?

It looks question in patch13 is same as here, so reply all here.

We can benefit from exposing IOMMUFD in vIOMMU, because in foreseeable future, it's the only backend supporting nested HWPT, it's straightforward for vIOMMU to create nested HWPT and do attachment through IOMMUFD.

Most of the code guarded by CONFIG_IOMMUFD are cooperation between vIOMMU and IOMMUFD backend, it's hard to abstract them with comon callbacks into VFIO, we need to take both vtd and smmu into consideration.

We are using HostIOMMUDevice whenever suitable, it's used as a connection between VFIO and vIOMMU, we do capability check and call attach/detach_dev callback through it.

Thanks
Zhenzhong

#24 lore raw

PATCH v7 11/23 intel_iommu: Fail passthrough device under PCI bridge if x-flts=on

In reply to Zhenzhong Duan

Currently we don't support nested translation for passthrough device with emulated device under same PCI bridge, because they require different address space when x-flts=on.

In theory, we do support if devices under same PCI bridge are all passthrough devices. But emulated device can be hotplugged under same bridge. To simplify, just forbid passthrough device under PCI bridge no matter if there is, or will be emulated devices under same bridge. This is acceptable because PCIE bridge is more popular than PCI bridge now.

---
 hw/i386/intel_iommu.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +11 -2
index 7d908cdb58..56abbb991d 100644
@@ -4610,9 +4610,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;
 
@@ -4639,6 +4640,8 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
 #ifdef CONFIG_IOMMUFD
     struct HostIOMMUDeviceCaps *caps = &hiod->caps;
     struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+    PCIBus *bus = vtd_hiod->bus;
+    PCIDevice *pdev = bus->devices[vtd_hiod->devfn];
 
     /* Remaining checks are all first stage translation specific */
     if (!object_dynamic_cast(OBJECT(hiod), TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
@@ -4657,6 +4660,12 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
                    "First stage 1GB large page is unsupported by host IOMMU");
         return false;
     }
+
+    if (pci_device_get_iommu_bus_devfn(pdev, &bus, NULL, NULL)) {
+        error_setg(errp, "Host device under PCI bridge is unsupported "
+                   "when x-flts=on");
+        return false;
+    }
 #endif
 
     error_setg(errp,
@@ -4697,7 +4706,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     vtd_hiod->iommu_state = s;
     vtd_hiod->hiod = hiod;
 
-    if (!vtd_check_hiod(s, hiod, errp)) {
+    if (!vtd_check_hiod(s, vtd_hiod, errp)) {
         g_free(vtd_hiod);
         vtd_iommu_unlock(s);
         return false;
Signature · 1 line
2.47.1
#25 lore raw

PATCH v7 12/23 intel_iommu: Add some macros and inline functions

In reply to Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro VTD_SM_PASID_ENTRY_DID and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu does, because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
 hw/i386/intel_iommu_internal.h |  8 +++++--
 hw/i386/intel_iommu.c          | 38 +++++++++++++++++++++++++++-------
 2 files changed, 37 insertions(+), 9 deletions(-)
hw/i386/intel_iommu_internal.h +6 -2
index 09edba81e2..df80af839d 100644
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) & VTD_DOMAIN_ID_MASK)
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
 #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2, 2)
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4, 1)
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)
 
 /* First Stage Paging Structure */
 /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +31 -7
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
     }
 }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level paging(FSPM=01)
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
 
     if (s->root_scalable) {
         vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
     }
 
     return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
              */
             return false;
         }
-        return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
+        return vtd_pe_pgtt_is_pt(&pe);
     }
 
     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
@@ -3146,9 +3170,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
         /* Fall through */
     case VTD_INV_DESC_PASIDC_G_DSI:
         if (pc_entry->valid) {
-            did = VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
         } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
         }
         if (pc_info->did != did) {
             return;
@@ -5267,7 +5291,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
         return -EINVAL;
     }
     pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:
Signature · 1 line
2.47.1
#26 lore raw

Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Quoted 59 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro VTD_SM_PASID_ENTRY_DID and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu does, because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
  hw/i386/intel_iommu_internal.h |  8 +++++--
  hw/i386/intel_iommu.c          | 38 +++++++++++++++++++++++++++-------
  2 files changed, 37 insertions(+), 9 deletions(-)
hw/i386/intel_iommu_internal.h +6 -2
index 09edba81e2..df80af839d 100644
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
  #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
  #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) & VTD_DOMAIN_ID_MASK)
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
  #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2, 2)
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4, 1)
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)
 
  /* First Stage Paging Structure */
  /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +3 -2
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
  /* pe operations */
  #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)
  #define VTD_PE_GET_SS_LEVEL(pe) \
      (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
      }
  }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 850

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level paging(FSPM=01)
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

Can't we use VTD_HOST_AW_48BIT here ?

Thanks,

C.

Quoted 55 lines
hw/i386/intel_iommu.c +18 -5
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
  static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
  {
      return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
 
      if (s->root_scalable) {
          vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
      }
 
      return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
               */
              return false;
          }
-        return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
+        return vtd_pe_pgtt_is_pt(&pe);
      }
 
      return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
@@ -3146,9 +3170,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
          /* Fall through */
      case VTD_INV_DESC_PASIDC_G_DSI:
          if (pc_entry->valid) {
-            did = VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
          } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
          }
          if (pc_info->did != did) {
              return;
@@ -5267,7 +5291,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
          return -EINVAL;
      }
      pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
      ret = 0;
      switch (pgtt) {
      case VTD_SM_PASID_ENTRY_FST:
#27 lore raw

RE: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Quoted 85 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Quoted 4 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro

VTD_SM_PASID_ENTRY_DID

Quoted 1 line

and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu

does,

Quoted 11 lines

because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
  hw/i386/intel_iommu_internal.h |  8 +++++--
  hw/i386/intel_iommu.c          | 38

+++++++++++++++++++++++++++-------

Review

Quoted 3 lines
  2 files changed, 37 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 644

Quoted 7 lines

index 09edba81e2..df80af839d 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
  #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
  #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted

guest-address-width */

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 645

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -1
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) &

VTD_DOMAIN_ID_MASK)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo {

Quoted 7 lines
hw/i386/intel_iommu_internal.h +4 -1
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
  #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2,

2)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 651

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4,

1)

hw/i386/intel_iommu.c line 54

Quoted 12 lines
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)
 
  /* First Stage Paging Structure */
  /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +0 -0
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
  /* pe operations */
  #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] &

VTD_SM_PASID_ENTRY_PGTT)

hw/i386/intel_iommu.c

Quoted 3 lines
hw/i386/intel_iommu.c +1 -2
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) +

4)

hw/i386/intel_iommu.c static inline bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
  #define VTD_PE_GET_SS_LEVEL(pe) \
      (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

Quoted 11 lines
hw/i386/intel_iommu.c +8 -0
      }
  }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level

paging(FSPM=01)

Quoted 7 lines

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 850

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

Can't we use VTD_HOST_AW_48BIT here ?

Will do.

Thanks
Zhenzhong

#28 lore raw

Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 23 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro VTD_SM_PASID_ENTRY_DID and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu does, because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
 hw/i386/intel_iommu_internal.h |  8 +++++--
 hw/i386/intel_iommu.c          | 38 +++++++++++++++++++++++++++-------
 2 files changed, 37 insertions(+), 9 deletions(-)
hw/i386/intel_iommu_internal.h +0 -0
index 09edba81e2..df80af839d 100644

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 649

Quoted 10 lines
hw/i386/intel_iommu_internal.h +3 -2
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) & VTD_DOMAIN_ID_MASK)
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
 #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */

the above comment is not that useful along with bitmask definition. I would rather move it along with VTD_PE_GET_FS_LEVEL(pe)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 652

Quoted 3 lines
hw/i386/intel_iommu_internal.h +3 -0
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2, 2)
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4, 1)
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)

I would get rid of _BIT suffix to make it consistent with other fields.

Quoted 18 lines
hw/i386/intel_iommu_internal.h +0 -0
 /* First Stage Paging Structure */
 /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +1 -2
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 841

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
@@ -837,6 +836,31 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
     }
 }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;

also introduce a define for FSPPTR using extract64((x)->val[2]

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 850

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level paging(FSPM=01)
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

can you add a comment refering to the spec here?

Quoted 47 lines
hw/i386/intel_iommu.c +17 -4
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
 
     if (s->root_scalable) {
         vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
     }
 
     return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
              */
             return false;
         }
-        return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
+        return vtd_pe_pgtt_is_pt(&pe);
     }
 
     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
@@ -3146,9 +3170,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
         /* Fall through */
     case VTD_INV_DESC_PASIDC_G_DSI:
         if (pc_entry->valid) {
-            did = VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
         } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
         }
         if (pc_info->did != did) {
             return;

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5297

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
@@ -5267,7 +5291,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
         return -EINVAL;
     }
     pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:

Otherwise looks good to me

Thanks

Eric

#29 lore raw

RE: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Eric,

I showed the planed change inline, let me know if I misunderstood.

Thanks
Zhenzhong

Quoted 41 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Zhenzhong,

Quoted 4 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro

VTD_SM_PASID_ENTRY_DID

Quoted 1 line

and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu

does,

Quoted 11 lines

because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
 hw/i386/intel_iommu_internal.h |  8 +++++--
 hw/i386/intel_iommu.c          | 38

+++++++++++++++++++++++++++-------

Review

Quoted 3 lines
 2 files changed, 37 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 644

Quoted 7 lines

index 09edba81e2..df80af839d 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted

guest-address-width */

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 645

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -1
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) &

VTD_DOMAIN_ID_MASK)

Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 649

Quoted 6 lines
hw/i386/intel_iommu_internal.h +3 -1
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
 #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */

the above comment is not that useful along with bitmask definition. I would rather move it along with VTD_PE_GET_FS_LEVEL(pe)

OK, will move it like below:

hw/i386/intel_iommu.c +1 -0
@@ -56,6 +56,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
+/* 4: 4-level paging, 5: 5-level paging */
 #define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
Quoted 6 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 650

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2,

2)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 651

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4,

1)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 652

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)

I would get rid of _BIT suffix to make it consistent with other fields.

OK

Quoted 21 lines

hw/i386/intel_iommu.c line 54

Quoted 11 lines
hw/i386/intel_iommu_internal.h +0 -0
 /* First Stage Paging Structure */
 /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +0 -0
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] &

VTD_SM_PASID_ENTRY_PGTT)

hw/i386/intel_iommu.c line 55

Quoted 3 lines
hw/i386/intel_iommu.c +1 -2
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) +

4)

hw/i386/intel_iommu.c static inline bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

Quoted 8 lines

hw/i386/intel_iommu.c static inline bool · line 841

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
     }
 }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;

also introduce a define for FSPPTR using extract64((x)->val[2]

Will change like below:

hw/i386/intel_iommu_internal.h +1 -1
@@ -650,7 +650,7 @@ typedef struct VTDPIOTLBInvInfo {
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
 #define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_FSPTPFN       extract64((x)->val[2], 12, 52)
 #define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
 /* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
 #define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2, 2)
hw/i386/intel_iommu.c +1 -1
index a3a4a8a72b..f801458649 100644
@@ -850,7 +851,7 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
 static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
 {
-    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+    return VTD_SM_PASID_ENTRY_FSPTPFN << 12;
 }
 
 
Quoted 7 lines

hw/i386/intel_iommu.c static inline bool · line 846

Quoted 6 lines
hw/i386/intel_iommu.c +5 -0
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level

paging(FSPM=01)

Quoted 5 lines

hw/i386/intel_iommu.c static inline bool · line 850

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

can you add a comment refering to the spec here?

OK

Quoted 65 lines

hw/i386/intel_iommu.c static uint16_t

Quoted 17 lines
hw/i386/intel_iommu.c +13 -0
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t

vtd_get_domain_id(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
     if (s->root_scalable) {
         vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
     }
 
     return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool

vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
              */
             return false;
         }
-        return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
+        return vtd_pe_pgtt_is_pt(&pe);
     }
 
     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
@@ -3146,9 +3170,9 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

hw/i386/intel_iommu.c static void · line 3149

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
         /* Fall through */
     case VTD_INV_DESC_PASIDC_G_DSI:
         if (pc_entry->valid) {
-            did =

VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);

hw/i386/intel_iommu.c static int

Quoted 8 lines
hw/i386/intel_iommu.c +2 -1
+            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
         } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
         }
         if (pc_info->did != did) {
             return;
@@ -5267,7 +5291,7 @@ static int

vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c static int · line 5297

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
         return -EINVAL;
     }
     pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:

Otherwise looks good to me

Thanks

Eric

#30 lore raw

Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Zhenzhong,

Quoted 61 lines · Zhenzhong

Hi Eric,

I showed the planed change inline, let me know if I misunderstood.

Thanks
Zhenzhong

Quoted 41 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Zhenzhong,

Quoted 4 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro

VTD_SM_PASID_ENTRY_DID

Quoted 1 line

and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu

does,

Quoted 11 lines

because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
 hw/i386/intel_iommu_internal.h |  8 +++++--
 hw/i386/intel_iommu.c          | 38

+++++++++++++++++++++++++++-------

Review

Quoted 3 lines
 2 files changed, 37 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 644

Quoted 7 lines

index 09edba81e2..df80af839d 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted

guest-address-width */

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 645

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -1
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) &

VTD_DOMAIN_ID_MASK)

Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 649

Quoted 6 lines
hw/i386/intel_iommu_internal.h +3 -1
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
 #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */

the above comment is not that useful along with bitmask definition. I would rather move it along with VTD_PE_GET_FS_LEVEL(pe)

OK, will move it like below:

hw/i386/intel_iommu.c line 59

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -56,6 +56,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
+/* 4: 4-level paging, 5: 5-level paging */

I think I would rather put quote the spec
paging mode for first-stage translation. • 00: 4-level paging  • 01:
5-level paging

Quoted 45 lines
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) + 4)
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 650

Quoted 3 lines
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2,

2)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 651

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2], 4,

1)

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 652

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7, 1)

I would get rid of _BIT suffix to make it consistent with other fields.

OK

Quoted 20 lines

hw/i386/intel_iommu.c line 54

Quoted 10 lines
hw/i386/intel_iommu_internal.h +0 -0
 /* First Stage Paging Structure */
 /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +0 -0
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] &

VTD_SM_PASID_ENTRY_PGTT)

hw/i386/intel_iommu.c line 55

Quoted 3 lines
hw/i386/intel_iommu.c +1 -2
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) +

4)

hw/i386/intel_iommu.c static inline bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

Quoted 7 lines

hw/i386/intel_iommu.c static inline bool · line 841

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
     }
 }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;

also introduce a define for FSPPTR using extract64((x)->val[2]

Will change like below:

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo { · line 653

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -1
@@ -650,7 +650,7 @@ typedef struct VTDPIOTLBInvInfo {
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
 #define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_FSPTPFN       extract64((x)->val[2], 12, 52)

VTD_SM_PASID_ENTRY_FSPTPFN(x)?

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 854

Quoted 13 lines
hw/i386/intel_iommu_internal.h +0 -0
 #define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0, 1)
 /* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
 #define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2, 2)
hw/i386/intel_iommu.c +1 -1
index a3a4a8a72b..f801458649 100644
@@ -850,7 +851,7 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
 static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
 {
-    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+    return VTD_SM_PASID_ENTRY_FSPTPFN << 12;

ditto

Quoted 80 lines
hw/i386/intel_iommu.c +0 -0
 }
 
 
Quoted 11 lines

hw/i386/intel_iommu.c static inline bool · line 846

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+}
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level

paging(FSPM=01)

hw/i386/intel_iommu.c static inline bool · line 850

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

can you add a comment refering to the spec here?

OK

Quoted 64 lines

hw/i386/intel_iommu.c static uint16_t

Quoted 17 lines
hw/i386/intel_iommu.c +13 -0
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t

vtd_get_domain_id(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
     if (s->root_scalable) {
         vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
     }
 
     return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool

vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
              */
             return false;
         }
-        return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
+        return vtd_pe_pgtt_is_pt(&pe);
     }
 
     return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
@@ -3146,9 +3170,9 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

hw/i386/intel_iommu.c static void · line 3149

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
         /* Fall through */
     case VTD_INV_DESC_PASIDC_G_DSI:
         if (pc_entry->valid) {
-            did =

VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);

hw/i386/intel_iommu.c static int

Quoted 8 lines
hw/i386/intel_iommu.c +2 -1
+            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
         } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
         }
         if (pc_info->did != did) {
             return;
@@ -5267,7 +5291,7 @@ static int

vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c static int · line 5297

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
         return -EINVAL;
     }
     pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:

Otherwise looks good to me

Thanks

Eric

#31 lore raw

RE: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Eric,

All clear, thanks.
btw: I'm taking vacation this week, I'll reply your comments for other patches a little later.

BRs,
Zhenzhong

Quoted 240 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Zhenzhong,

Quoted 49 lines · Zhenzhong

Hi Eric,

I showed the planed change inline, let me know if I misunderstood.

Thanks
Zhenzhong

Quoted 42 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 12/23] intel_iommu: Add some macros and inline functions

Hi Zhenzhong,

Quoted 4 lines · Zhenzhong Duan

Add some macros and inline functions that will be used by following patch.

This patch also make a cleanup to change macro

VTD_SM_PASID_ENTRY_DID

Quoted 1 line

and VTD_SM_PASID_ENTRY_FSPM to use extract64() just like what smmu

does,

Quoted 11 lines

because they are either used in following patches or used indirectly by new introduced inline functions. But we doesn't aim to change the huge amount of bit mask style macro definitions in this patch, that should be in a separate patch.

---
 hw/i386/intel_iommu_internal.h |  8 +++++--
 hw/i386/intel_iommu.c          | 38

+++++++++++++++++++++++++++-------

Review

Quoted 3 lines
 2 files changed, 37 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 644

Quoted 7 lines

index 09edba81e2..df80af839d 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -642,10 +642,14 @@ typedef struct VTDPASIDCacheInfo {
 #define VTD_SM_PASID_ENTRY_PT          (4ULL << 6)
 
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted

guest-address-width */

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 645

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -1
-#define VTD_SM_PASID_ENTRY_DID(val)    ((val) &

VTD_DOMAIN_ID_MASK)

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0,

16)

Quoted 4 lines
Quoted 4 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 647

Quoted 4 lines
hw/i386/intel_iommu_internal.h +1 -1
-#define VTD_SM_PASID_ENTRY_FSPM          3ULL
 #define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0,

1)

hw/i386/intel_iommu.c line 58

Quoted 11 lines
Quoted 3 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 648

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+/* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */

the above comment is not that useful along with bitmask definition. I would rather move it along with VTD_PE_GET_FS_LEVEL(pe)

OK, will move it like below:

hw/i386/intel_iommu.c +0 -0
@@ -56,6 +56,7 @@
 
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] &

VTD_SM_PASID_ENTRY_PGTT)

hw/i386/intel_iommu.c line 59

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+/* 4: 4-level paging, 5: 5-level paging */

I think I would rather put quote the spec
paging mode for first-stage translation. • 00: 4-level paging  • 01:
5-level paging

hw/i386/intel_iommu.c line 60

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe) +

4)

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
Quoted 3 lines

hw/i386/intel_iommu_internal.h typedef struct VTDPASIDCacheInfo { · line 649

Quoted 3 lines
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2],

2,

Quoted 2 lines
Quoted 2 lines

2)

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_WPE_BIT(x)    extract64((x)->val[2],

4,

Quoted 2 lines
Quoted 2 lines

1)

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(x)   extract64((x)->val[2], 7,

1)

Quoted 11 lines
Quoted 1 line

I would get rid of _BIT suffix to make it consistent with other fields.

OK

Quoted 8 lines
Quoted 8 lines
hw/i386/intel_iommu_internal.h +0 -0
 /* First Stage Paging Structure */
 /* Masks for First Stage Paging Entry */
hw/i386/intel_iommu.c +0 -0
index 56abbb991d..871e6aad19 100644
@@ -52,8 +52,7 @@
 
Quoted 6 lines
Quoted 6 lines

hw/i386/intel_iommu.c line 54

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
 /* pe operations */
 #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] &

VTD_SM_PASID_ENTRY_PGTT)

hw/i386/intel_iommu.c line 55

Quoted 3 lines
hw/i386/intel_iommu.c +1 -2
-#define VTD_PE_GET_FS_LEVEL(pe) \
-    (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FSPM))
+#define VTD_PE_GET_FS_LEVEL(pe) (VTD_SM_PASID_ENTRY_FSPM(pe)

+

Quoted 18 lines
Quoted 6 lines

4)

hw/i386/intel_iommu.c static inline bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
 #define VTD_PE_GET_SS_LEVEL(pe) \
     (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
 
@@ -837,6 +836,31 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

Quoted 7 lines

hw/i386/intel_iommu.c static inline bool · line 841

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
     }
 }
 
+static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
+{
+    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;

also introduce a define for FSPPTR using extract64((x)->val[2]

Will change like below:

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo { · line 650

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -0
@@ -650,7 +650,7 @@ typedef struct VTDPIOTLBInvInfo {
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted

guest-address-width */

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo {

Quoted 4 lines
hw/i386/intel_iommu_internal.h +1 -1
 #define VTD_SM_PASID_ENTRY_DID(x)      extract64((x)->val[1], 0, 16)
 
-#define VTD_SM_PASID_ENTRY_FSPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_FSPTPFN       extract64((x)->val[2], 12,

52)

VTD_SM_PASID_ENTRY_FSPTPFN(x)?

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo { · line 654

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -0
 #define VTD_SM_PASID_ENTRY_SRE_BIT(x)    extract64((x)->val[2], 0,

1)

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo { · line 656

Quoted 2 lines
hw/i386/intel_iommu_internal.h +0 -0
 /* 00: 4-level paging, 01: 5-level paging, 10-11: Reserved */
 #define VTD_SM_PASID_ENTRY_FSPM(x)       extract64((x)->val[2], 2,

2)

hw/i386/intel_iommu.c static inline bool

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index a3a4a8a72b..f801458649 100644
@@ -850,7 +851,7 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

hw/i386/intel_iommu.c static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe) · line 854

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
 static inline dma_addr_t vtd_pe_get_fspt_base(VTDPASIDEntry *pe)
 {
-    return pe->val[2] & VTD_SM_PASID_ENTRY_FSPTPTR;
+    return VTD_SM_PASID_ENTRY_FSPTPFN << 12;

ditto

Quoted 9 lines
hw/i386/intel_iommu.c +0 -0
 }
 
 
 >> +}
Quoted 5 lines

hw/i386/intel_iommu.c static inline bool · line 845

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+
+/*
+ * First stage IOVA address width: 48 bits for 4-level paging(FSPM=00)
+ *                                 57 bits for 5-level

paging(FSPM=01)

Quoted 38 lines
Quoted 5 lines

hw/i386/intel_iommu.c static inline bool · line 849

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+ */
+static inline uint32_t vtd_pe_get_fs_aw(VTDPASIDEntry *pe)
+{
+    return 48 + VTD_SM_PASID_ENTRY_FSPM(pe) * 9;

can you add a comment refering to the spec here?

OK

Quoted 31 lines

hw/i386/intel_iommu.c static uint16_t

Quoted 17 lines
hw/i386/intel_iommu.c +13 -0
+}
+
+static inline bool vtd_pe_pgtt_is_pt(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_PT);
+}
+
+/* check if pgtt is first stage translation */
+static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
+{
+    return (VTD_PE_GET_TYPE(pe) == VTD_SM_PASID_ENTRY_FST);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1625,7 +1649,7 @@ static uint16_t

vtd_get_domain_id(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
     if (s->root_scalable) {
         vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
-        return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+        return VTD_SM_PASID_ENTRY_DID(&pe);
     }
 
     return VTD_CONTEXT_ENTRY_DID(ce->hi);
@@ -1707,7 +1731,7 @@ static bool

vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
              */
             return false;
         }
-        return (VTD_PE_GET_TYPE(&pe) ==

VTD_SM_PASID_ENTRY_PT);

Quoted 4 lines
Quoted 4 lines

hw/i386/intel_iommu.c static bool · line 1737

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+        return vtd_pe_pgtt_is_pt(&pe);
     }
 
     return (vtd_ce_get_type(ce) ==

VTD_CONTEXT_TT_PASS_THROUGH);

Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c static void

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -3146,9 +3170,9 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

hw/i386/intel_iommu.c static void · line 3149

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
         /* Fall through */
     case VTD_INV_DESC_PASIDC_G_DSI:
         if (pc_entry->valid) {
-            did =

VTD_SM_PASID_ENTRY_DID(pc_entry->pasid_entry.val[1]);

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+            did =

VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);

Quoted 21 lines
Quoted 21 lines

hw/i386/intel_iommu.c static int

Quoted 7 lines
hw/i386/intel_iommu.c +1 -1
         } else {
-            did = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+            did = VTD_SM_PASID_ENTRY_DID(&pe);
         }
         if (pc_info->did != did) {
             return;
@@ -5267,7 +5291,7 @@ static int

vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c static int · line 5297

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
         return -EINVAL;
     }
     pgtt = VTD_PE_GET_TYPE(&pe);
-    domain_id = VTD_SM_PASID_ENTRY_DID(pe.val[1]);
+    domain_id = VTD_SM_PASID_ENTRY_DID(&pe);
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:

Otherwise looks good to me

Thanks

Eric

#32 lore raw

PATCH v7 13/23 intel_iommu: Bind/unbind guest page table to host

In reply to Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

---
 include/hw/i386/intel_iommu.h |   1 +
 hw/i386/intel_iommu.c         | 150 +++++++++++++++++++++++++++++++++-
 hw/i386/trace-events          |   3 +
 3 files changed, 151 insertions(+), 3 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index 3758ac239c..b5f8a9fc29 100644
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
     uint32_t pasid;
+    uint32_t fs_hwpt;
     AddressSpace as;
     IOMMUMemoryRegion iommu;
     MemoryRegion root;          /* The root container of the device */
hw/i386/intel_iommu.c +147 -3

203 lines not on this page · raw message

hw/i386/trace-events +3 -0
index b704f4f90c..5a3ee1cf64 100644
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16
 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)"
 vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)"
 vtd_reset_exit(void) ""
+vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d"
+vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
 
 # amd_iommu.c
 amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
Signature · 1 line
2.47.1
#33 lore raw

Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Quoted 43 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

---
  include/hw/i386/intel_iommu.h |   1 +
  hw/i386/intel_iommu.c         | 150 +++++++++++++++++++++++++++++++++-
  hw/i386/trace-events          |   3 +
  3 files changed, 151 insertions(+), 3 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index 3758ac239c..b5f8a9fc29 100644
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
      PCIBus *bus;
      uint8_t devfn;
      uint32_t pasid;
+    uint32_t fs_hwpt;
      AddressSpace as;
      IOMMUMemoryRegion iommu;
      MemoryRegion root;          /* The root container of the device */
hw/i386/intel_iommu.c +1 -0
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
   */
 
  #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  #include "qemu/error-report.h"
  #include "qemu/main-loop.h"
  #include "qapi/error.h"

hw/i386/intel_iommu.c line 48

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
@@ -42,6 +43,9 @@
  #include "migration/vmstate.h"
  #include "trace.h"
  #include "system/iommufd.h"
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif

Exposing IOMMUFD in the Intel vIOMMU is unexpected. Initially, we introduced HostIOMMUDeviceClass to avoid exposing the IOMMU backends. Are we OK to bypass this abstract layer now ?

Thanks,

C.

Quoted 209 lines
hw/i386/intel_iommu.c +143 -3
  /* context entry operations */
  #define PASID_0    0
@@ -87,6 +91,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 int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 
  static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
  {
@@ -98,7 +103,11 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
      g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
      while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
          VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
      }
  }
 
@@ -2380,6 +2389,128 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
      vtd_iommu_replay_all(s);
  }
 
+#ifdef CONFIG_IOMMUFD
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t *fs_hwpt,
+                              Error **errp)
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ? IOMMU_VTD_S1_SRE : 0) |
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ? IOMMU_VTD_S1_WPE : 0) |
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ? IOMMU_VTD_S1_EAFE : 0);
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
+
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid,
+                                       idev->hwpt_id, 0, IOMMU_HWPT_DATA_VTD_S1,
+                                       sizeof(vtd), &vtd, fs_hwpt, errp);
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
+    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.
+     * Catch invalid PGTT when processing invalidation request to avoid
+     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) {
+        error_setg(errp, "Invalid PGTT type");
+        return -EINVAL;
+    }
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, !ret);
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id);
+    }
+
+    return !ret;
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret);
+    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy mode,
+         * we fallback to the default HWPT which contains shadow page table.
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
+                                           !ret);
+    }
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
+    } else {
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+
  /* Do a context-cache device-selective invalidation.
   * @func_mask: FM field after shifting
   */
@@ -3134,6 +3265,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
      VTDPASIDEntry pe;
      IOMMUNotifier *n;
      uint16_t did;
+    const char *err_prefix;
+    Error *local_err = NULL;
 
      if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
          if (!pc_entry->valid) {
@@ -3154,7 +3287,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
              vtd_address_space_unmap(vtd_as, n);
          }
          vtd_switch_address_space(vtd_as);
-        return;
+
+        err_prefix = "Detaching from HWPT failed: ";
+        goto do_bind_unbind;
      }
 
      /*
@@ -3182,12 +3317,21 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
      if (!pc_entry->valid) {
          pc_entry->pasid_entry = pe;
          pc_entry->valid = true;
-    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Attaching to HWPT failed: ";
+    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Replacing HWPT attachment failed: ";
+    } else {
          return;
      }
 
      vtd_switch_address_space(vtd_as);
      vtd_address_space_sync(vtd_as);
+
+do_bind_unbind:
+    /* TODO: Fault event injection into guest, report error to QEMU for now */
+    if (vtd_bind_guest_pasid(vtd_as, &local_err)) {
+        error_reportf_err(local_err, "%s", err_prefix);
+    }
  }
 
  static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/trace-events +3 -0
index b704f4f90c..5a3ee1cf64 100644
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16
  vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)"
  vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)"
  vtd_reset_exit(void) ""
+vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d"
+vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
 
  # amd_iommu.c
  amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
#34 lore raw

Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Hi Cédric, Zhenzhong,

Quoted 48 lines · Cédric Le Goater
Quoted 47 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU
PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

---
  include/hw/i386/intel_iommu.h |   1 + hw/i386/intel_iommu.c         | 150 +++++++++++++++++++++++++++++++++- hw/i386/trace-events          |   3 +

  3 files changed, 151 insertions(+), 3 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index 3758ac239c..b5f8a9fc29 100644
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
       PCIBus *bus;
       uint8_t devfn;
       uint32_t pasid;
+    uint32_t fs_hwpt;
       AddressSpace as;
       IOMMUMemoryRegion iommu;

      MemoryRegion root;          /* The root container of the device */

hw/i386/intel_iommu.c +1 -0
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
    */
     #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
   #include "qemu/error-report.h"
   #include "qemu/main-loop.h"
   #include "qapi/error.h"
@@ -42,6 +43,9 @@
   #include "migration/vmstate.h"
   #include "trace.h"
   #include "system/iommufd.h"
Quoted 8 lines

hw/i386/intel_iommu.c line 48

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif

Exposing IOMMUFD in the Intel vIOMMU is unexpected. Initially, we introduced HostIOMMUDeviceClass to avoid exposing the IOMMU backends. Are we OK to bypass this abstract layer now ?

HostIOMMUDeviceClass was rather introduced to hide the differences between VFIO and IOMMUFD backend implementations.
This feature is only implemented based on iommufd. Besides it is specialized for VTD and ARM so to me it looks like a different class derivation

Maybe you should put that code in a separate file (-accel.c) as Shameer/Nicolin are doing for SMMU?

Thanks

Eric

Quoted 239 lines

Thanks,

C.

 >   /* context entry operations */

Quoted 229 lines
hw/i386/intel_iommu.c +6 -1
   #define PASID_0    0
@@ -87,6 +91,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 int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
     static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
   {
@@ -98,7 +103,11 @@ static void
 vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
       g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
       while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
           VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
       }
   }

  @@ -2380,6 +2389,128 @@ static void
vtd_context_global_invalidate(IntelIOMMUState *s)
      vtd_iommu_replay_all(s);
  }
  +#ifdef CONFIG_IOMMUFD

hw/i386/intel_iommu.c +7 -0
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t *fs_hwpt,
+                              Error **errp)
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ? IOMMU_VTD_S1_SRE :

0) |
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ? IOMMU_VTD_S1_WPE :
0) |
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ? IOMMU_VTD_S1_EAFE : 0);
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe); +
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, +                                       idev->hwpt_id, 0, IOMMU_HWPT_DATA_VTD_S1,
+                                       sizeof(vtd), &vtd, fs_hwpt, errp);

hw/i386/intel_iommu.c +14 -0
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error

**errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry; +    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.
+     * Catch invalid PGTT when processing invalidation request to avoid +     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) { +        error_setg(errp, "Invalid PGTT type");
+        return -EINVAL;
+    }

hw/i386/intel_iommu.c +9 -0
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid,

hwpt_id, !ret);
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */ +        vtd_destroy_old_fs_hwpt(idev, vtd_as); +        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id); +    }

hw/i386/intel_iommu.c +6 -0
+
+    return !ret;
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error

**errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp); +        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret); +    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy mode,
+         * we fallback to the default HWPT which contains shadow page table.
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
+                                           !ret);
+    }

hw/i386/intel_iommu.c +34 -0
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
+    } else {
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+

  /* Do a context-cache device-selective invalidation.
   * @func_mask: FM field after shifting
   */

hw/i386/intel_iommu.c +12 -2
@@ -3134,6 +3265,8 @@ static void
 vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
       VTDPASIDEntry pe;
       IOMMUNotifier *n;
       uint16_t did;
+    const char *err_prefix;
+    Error *local_err = NULL;
         if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
           if (!pc_entry->valid) {
@@ -3154,7 +3287,9 @@ static void
 vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
               vtd_address_space_unmap(vtd_as, n);
           }
           vtd_switch_address_space(vtd_as);
-        return;
+
+        err_prefix = "Detaching from HWPT failed: ";
+        goto do_bind_unbind;
       }
         /*
@@ -3182,12 +3317,21 @@ static void
 vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
       if (!pc_entry->valid) {
           pc_entry->pasid_entry = pe;
           pc_entry->valid = true;
-    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Attaching to HWPT failed: ";
+    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Replacing HWPT attachment failed: ";
+    } else {
           return;
       }
         vtd_switch_address_space(vtd_as);
       vtd_address_space_sync(vtd_as);
+
+do_bind_unbind:
+    /* TODO: Fault event injection into guest, report error to QEMU

for now */
+    if (vtd_bind_guest_pasid(vtd_as, &local_err)) { +        error_reportf_err(local_err, "%s", err_prefix); +    }
  }
    static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)

hw/i386/trace-events +0 -0
index b704f4f90c..5a3ee1cf64 100644
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail

0x%"PRIx16
  vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)" vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)" vtd_reset_exit(void) "" +vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" +vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" +vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"

    # amd_iommu.c
  amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to
write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
#35 lore raw

Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

In reply to Zhenzhong Duan
Quoted 74 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

---
  include/hw/i386/intel_iommu.h |   1 +
  hw/i386/intel_iommu.c         | 150 +++++++++++++++++++++++++++++++++-
  hw/i386/trace-events          |   3 +
  3 files changed, 151 insertions(+), 3 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index 3758ac239c..b5f8a9fc29 100644
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
      PCIBus *bus;
      uint8_t devfn;
      uint32_t pasid;
+    uint32_t fs_hwpt;
      AddressSpace as;
      IOMMUMemoryRegion iommu;
      MemoryRegion root;          /* The root container of the device */
hw/i386/intel_iommu.c +10 -1
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
   */
 
  #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  #include "qemu/error-report.h"
  #include "qemu/main-loop.h"
  #include "qapi/error.h"
@@ -42,6 +43,9 @@
  #include "migration/vmstate.h"
  #include "trace.h"
  #include "system/iommufd.h"
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif
 
  /* context entry operations */
  #define PASID_0    0
@@ -87,6 +91,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 int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 
  static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
  {
@@ -98,7 +103,11 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
      g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
      while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
          VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
      }
  }
 

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2395

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
@@ -2380,6 +2389,128 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
      vtd_iommu_replay_all(s);
  }
 
+#ifdef CONFIG_IOMMUFD
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t *fs_hwpt,
+                              Error **errp)

Returning a bool is better. Same for the routines below.

Quoted 122 lines
hw/i386/intel_iommu.c +118 -0
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ? IOMMU_VTD_S1_SRE : 0) |
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ? IOMMU_VTD_S1_WPE : 0) |
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ? IOMMU_VTD_S1_EAFE : 0);
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
+
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid,
+                                       idev->hwpt_id, 0, IOMMU_HWPT_DATA_VTD_S1,
+                                       sizeof(vtd), &vtd, fs_hwpt, errp);
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
+    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.
+     * Catch invalid PGTT when processing invalidation request to avoid
+     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) {
+        error_setg(errp, "Invalid PGTT type");
+        return -EINVAL;
+    }
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, !ret);
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id);
+    }
+
+    return !ret;
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret);
+    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy mode,
+         * we fallback to the default HWPT which contains shadow page table.
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
+                                           !ret);
+    }
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
+    } else {
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+
  /* Do a context-cache device-selective invalidation.
   * @func_mask: FM field after shifting
   */

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value, · line 3268

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -3134,6 +3265,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
      VTDPASIDEntry pe;
      IOMMUNotifier *n;
      uint16_t did;
+    const char *err_prefix;

Setting this prefix looks a bit fragile. May be add a default value here.

Thanks,

C.

Quoted 52 lines
hw/i386/intel_iommu.c +14 -2
+    Error *local_err = NULL;
 
      if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
          if (!pc_entry->valid) {
@@ -3154,7 +3287,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
              vtd_address_space_unmap(vtd_as, n);
          }
          vtd_switch_address_space(vtd_as);
-        return;
+
+        err_prefix = "Detaching from HWPT failed: ";
+        goto do_bind_unbind;
      }
 
      /*
@@ -3182,12 +3317,21 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
      if (!pc_entry->valid) {
          pc_entry->pasid_entry = pe;
          pc_entry->valid = true;
-    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Attaching to HWPT failed: ";
+    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Replacing HWPT attachment failed: ";
+    } else {
          return;
      }
 
      vtd_switch_address_space(vtd_as);
      vtd_address_space_sync(vtd_as);
+
+do_bind_unbind:
+    /* TODO: Fault event injection into guest, report error to QEMU for now */
+    if (vtd_bind_guest_pasid(vtd_as, &local_err)) {
+        error_reportf_err(local_err, "%s", err_prefix);
+    }
  }
 
  static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/trace-events +3 -0
index b704f4f90c..5a3ee1cf64 100644
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16
  vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)"
  vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)"
  vtd_reset_exit(void) ""
+vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d"
+vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
 
  # amd_iommu.c
  amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
#36 lore raw

RE: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Quoted 93 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Quoted 2 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest

IOMMU

Quoted 6 lines

PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

Quoted 7 lines
---
  include/hw/i386/intel_iommu.h |   1 +
  hw/i386/intel_iommu.c         | 150

+++++++++++++++++++++++++++++++++-

Review

Quoted 4 lines
  hw/i386/trace-events          |   3 +
  3 files changed, 151 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 110

Quoted 11 lines

index 3758ac239c..b5f8a9fc29 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
      PCIBus *bus;
      uint8_t devfn;
      uint32_t pasid;
+    uint32_t fs_hwpt;
      AddressSpace as;
      IOMMUMemoryRegion iommu;
      MemoryRegion root;          /* The root container of the

device */

Quoted 23 lines
hw/i386/intel_iommu.c +4 -0
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
   */
 
  #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  #include "qemu/error-report.h"
  #include "qemu/main-loop.h"
  #include "qapi/error.h"
@@ -42,6 +43,9 @@
  #include "migration/vmstate.h"
  #include "trace.h"
  #include "system/iommufd.h"
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif
 
  /* context entry operations */
  #define PASID_0    0

hw/i386/intel_iommu.c struct vtd_iotlb_key { · line 93

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
@@ -87,6 +91,7 @@ struct vtd_iotlb_key {
 
  static void vtd_address_space_refresh_all(IntelIOMMUState *s);
  static void vtd_address_space_unmap(VTDAddressSpace *as,

IOMMUNotifier *n);

hw/i386/intel_iommu.c static void

Quoted 5 lines
hw/i386/intel_iommu.c +1 -0
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 
  static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
  {
@@ -98,7 +103,11 @@ static void

vtd_pasid_cache_reset_locked(IntelIOMMUState *s)

hw/i386/intel_iommu.c static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
      g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
      while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
          VTDPASIDCacheEntry *pc_entry =

&vtd_as->pasid_cache_entry;

hw/i386/intel_iommu.c static void

Quoted 10 lines
hw/i386/intel_iommu.c +5 -1
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
      }
  }
 
@@ -2380,6 +2389,128 @@ static void

vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
      vtd_iommu_replay_all(s);
  }
 
Quoted 7 lines

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+#ifdef CONFIG_IOMMUFD
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t

*fs_hwpt,

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2395

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                              Error **errp)

Returning a bool is better. Same for the routines below.

We will need the returned error code to determine the fault event to inject to guest in future fault event series.

Quoted 139 lines

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 5 lines
hw/i386/intel_iommu.c +4 -0
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ?

IOMMU_VTD_S1_SRE : 0) |

hw/i386/intel_iommu.c static void · line 2400

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ?

IOMMU_VTD_S1_WPE : 0) |

hw/i386/intel_iommu.c static void · line 2401

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ?

IOMMU_VTD_S1_EAFE : 0);

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
+
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid,
+                                       idev->hwpt_id, 0,

IOMMU_HWPT_DATA_VTD_S1,

hw/i386/intel_iommu.c static void · line 2407

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                       sizeof(vtd), &vtd, fs_hwpt,

errp);

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as,

Error **errp)

hw/i386/intel_iommu.c static void · line 2423

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+{
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

Quoted 11 lines
hw/i386/intel_iommu.c +11 -0
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
+    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.
+     * Catch invalid PGTT when processing invalidation request to avoid
+     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) {
+        error_setg(errp, "Invalid PGTT type");

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        return -EINVAL;
+    }
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id,

errp);

hw/i386/intel_iommu.c static void · line 2445

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid,

hwpt_id, !ret);

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id);
+    }
+
+    return !ret;
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as,

Error **errp)

hw/i386/intel_iommu.c static void · line 2462

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+{
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret);
+    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy

mode,

hw/i386/intel_iommu.c static void · line 2473

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+         * we fallback to the default HWPT which contains shadow page

table.

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev,

idev->hwpt_id, errp);

hw/i386/intel_iommu.c static void · line 2477

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+        trace_vtd_device_reattach_def_hwpt(idev->devid, pasid,

idev->hwpt_id,

hw/i386/intel_iommu.c static void

Quoted 40 lines
hw/i386/intel_iommu.c +36 -0
+                                           !ret);
+    }
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
+    } else {
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+
  /* Do a context-cache device-selective invalidation.
   * @func_mask: FM field after shifting
   */
@@ -3134,6 +3265,8 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

Quoted 6 lines

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value, · line 3268

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
      VTDPASIDEntry pe;
      IOMMUNotifier *n;
      uint16_t did;
+    const char *err_prefix;

Setting this prefix looks a bit fragile. May be add a default value here.

OK, like:

    const char *err_prefix = "Attaching to HWPT failed: ";

Thanks
Zhenzhong

#37 lore raw

Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 83 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest IOMMU PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

---
 include/hw/i386/intel_iommu.h |   1 +
 hw/i386/intel_iommu.c         | 150 +++++++++++++++++++++++++++++++++-
 hw/i386/trace-events          |   3 +
 3 files changed, 151 insertions(+), 3 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index 3758ac239c..b5f8a9fc29 100644
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
     uint32_t pasid;
+    uint32_t fs_hwpt;
     AddressSpace as;
     IOMMUMemoryRegion iommu;
     MemoryRegion root;          /* The root container of the device */
hw/i386/intel_iommu.c +16 -1
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
  */
 
 #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 #include "qapi/error.h"
@@ -42,6 +43,9 @@
 #include "migration/vmstate.h"
 #include "trace.h"
 #include "system/iommufd.h"
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif
 
 /* context entry operations */
 #define PASID_0    0
@@ -87,6 +91,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 int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -98,7 +103,11 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
         VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
     }
 }
 
@@ -2380,6 +2389,128 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
     vtd_iommu_replay_all(s);
 }
 
+#ifdef CONFIG_IOMMUFD
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t *fs_hwpt,
+                              Error **errp)
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2407

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ? IOMMU_VTD_S1_SRE : 0) |
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ? IOMMU_VTD_S1_WPE : 0) |
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ? IOMMU_VTD_S1_EAFE : 0);
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
+
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid,
+                                       idev->hwpt_id, 0, IOMMU_HWPT_DATA_VTD_S1,
+                                       sizeof(vtd), &vtd, fs_hwpt, errp);

see comments below

Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2429

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
+    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.

is FST or PT

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2434

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+     * Catch invalid PGTT when processing invalidation request to avoid
+     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) {
+        error_setg(errp, "Invalid PGTT type");

print the wrong PGTT value?

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2445

Quoted 11 lines
hw/i386/intel_iommu.c +11 -0
+        return -EINVAL;
+    }
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, !ret);

why !ret in the trace? I think e chose to return true on success for

host_iommu_device_iommufd_attach_hwpt()

. Let's keep this consistent otherwise we end up not knowning what is the success value.

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2456

Quoted 11 lines
hw/i386/intel_iommu.c +11 -0
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id);
+    }
+
+    return !ret;

argh. Eventually it is difficult to follow ;-)

Quoted 14 lines
hw/i386/intel_iommu.c +13 -0
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret);

same

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2478

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy mode,
+         * we fallback to the default HWPT which contains shadow page table.
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
+                                           !ret);

same

Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+    }
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2500

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);

return directly

Quoted 1 line

+ } else {

remove the else and return directly?

Quoted 63 lines
hw/i386/intel_iommu.c +27 -2
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+
 /* Do a context-cache device-selective invalidation.
  * @func_mask: FM field after shifting
  */
@@ -3134,6 +3265,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     VTDPASIDEntry pe;
     IOMMUNotifier *n;
     uint16_t did;
+    const char *err_prefix;
+    Error *local_err = NULL;
 
     if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
         if (!pc_entry->valid) {
@@ -3154,7 +3287,9 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
             vtd_address_space_unmap(vtd_as, n);
         }
         vtd_switch_address_space(vtd_as);
-        return;
+
+        err_prefix = "Detaching from HWPT failed: ";
+        goto do_bind_unbind;
     }
 
     /*
@@ -3182,12 +3317,21 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     if (!pc_entry->valid) {
         pc_entry->pasid_entry = pe;
         pc_entry->valid = true;
-    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Attaching to HWPT failed: ";
+    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Replacing HWPT attachment failed: ";
+    } else {
         return;
     }
 
     vtd_switch_address_space(vtd_as);
     vtd_address_space_sync(vtd_as);
+
+do_bind_unbind:
+    /* TODO: Fault event injection into guest, report error to QEMU for now */
+    if (vtd_bind_guest_pasid(vtd_as, &local_err)) {
+        error_reportf_err(local_err, "%s", err_prefix);
+    }
 }
 
 static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/trace-events +0 -0
index b704f4f90c..5a3ee1cf64 100644

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16 · line 81

Quoted 9 lines
hw/i386/trace-events +3 -0
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16
 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)"
 vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)"
 vtd_reset_exit(void) ""
+vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d"
+vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
 
 # amd_iommu.c
 amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32

Eric

#38 lore raw

RE: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Hi Eric,

Quoted 157 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 13/23] intel_iommu: Bind/unbind guest page table to host

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

This captures the guest PASID table entry modifications and propagates the changes to host to attach a hwpt with type determined per guest

IOMMU

Quoted 6 lines

PGTT configuration.

When PGTT=PT, attach PASID_0 to a second stage HWPT(GPA->HPA). When PGTT=FST, attach PASID_0 to nested HWPT with nesting parent HWPT coming from VFIO.

Quoted 7 lines
---
 include/hw/i386/intel_iommu.h |   1 +
 hw/i386/intel_iommu.c         | 150

+++++++++++++++++++++++++++++++++-

Review

Quoted 4 lines
 hw/i386/trace-events          |   3 +
 3 files changed, 151 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 110

Quoted 11 lines

index 3758ac239c..b5f8a9fc29 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -104,6 +104,7 @@ struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
     uint32_t pasid;
+    uint32_t fs_hwpt;
     AddressSpace as;
     IOMMUMemoryRegion iommu;
     MemoryRegion root;          /* The root container of the device

*/

Quoted 23 lines
hw/i386/intel_iommu.c +4 -0
index 871e6aad19..3789a36147 100644
@@ -20,6 +20,7 @@
  */
 
 #include "qemu/osdep.h"
+#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 #include "qapi/error.h"
@@ -42,6 +43,9 @@
 #include "migration/vmstate.h"
 #include "trace.h"
 #include "system/iommufd.h"
+#ifdef CONFIG_IOMMUFD
+#include <linux/iommufd.h>
+#endif
 
 /* context entry operations */
 #define PASID_0    0

hw/i386/intel_iommu.c struct vtd_iotlb_key { · line 93

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
@@ -87,6 +91,7 @@ struct vtd_iotlb_key {
 
 static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as,

IOMMUNotifier *n);

hw/i386/intel_iommu.c static void

Quoted 5 lines
hw/i386/intel_iommu.c +1 -0
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -98,7 +103,11 @@ static void

vtd_pasid_cache_reset_locked(IntelIOMMUState *s)

hw/i386/intel_iommu.c static void

Quoted 13 lines
hw/i386/intel_iommu.c +5 -1
     g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
     while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
         VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-        pc_entry->valid = false;
+        if (pc_entry->valid) {
+            pc_entry->valid = false;
+            /* It's fatal to get failure during reset */
+            vtd_bind_guest_pasid(vtd_as, &error_fatal);
+        }
     }
 }
 
@@ -2380,6 +2389,128 @@ static void

vtd_context_global_invalidate(IntelIOMMUState *s)

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
     vtd_iommu_replay_all(s);
 }
 
+#ifdef CONFIG_IOMMUFD
+static int vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                              VTDPASIDEntry *pe, uint32_t

*fs_hwpt,

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                              Error **errp)
+{
+    struct iommu_hwpt_vtd_s1 vtd = {};
+
+    vtd.flags = (VTD_SM_PASID_ENTRY_SRE_BIT(pe) ?

IOMMU_VTD_S1_SRE : 0) |

hw/i386/intel_iommu.c static void · line 2400

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                (VTD_SM_PASID_ENTRY_WPE_BIT(pe) ?

IOMMU_VTD_S1_WPE : 0) |

hw/i386/intel_iommu.c static void · line 2401

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                (VTD_SM_PASID_ENTRY_EAFE_BIT(pe) ?

IOMMU_VTD_S1_EAFE : 0);

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+    vtd.addr_width = vtd_pe_get_fs_aw(pe);
+    vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
+
+    return !iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid,
+                                       idev->hwpt_id, 0,

IOMMU_HWPT_DATA_VTD_S1,

hw/i386/intel_iommu.c static void · line 2407

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                       sizeof(vtd), &vtd, fs_hwpt,

errp);
see comments below

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+}
+
+static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                    VTDAddressSpace *vtd_as)

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+{
+    if (!vtd_as->fs_hwpt) {
+        return;
+    }
+    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt);
+    vtd_as->fs_hwpt = 0;
+}
+
+static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as,

Error **errp)

hw/i386/intel_iommu.c static void · line 2423

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+{
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2429

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
+    uint32_t hwpt_id;
+    bool ret;
+
+    /*
+     * We can get here only if flts=on, the supported PGTT is FST and PT.

is FST or PT

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2434

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+     * Catch invalid PGTT when processing invalidation request to avoid
+     * attaching to wrong hwpt.
+     */
+    if (!vtd_pe_pgtt_is_fst(pe) && !vtd_pe_pgtt_is_pt(pe)) {
+        error_setg(errp, "Invalid PGTT type");

print the wrong PGTT value?

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        return -EINVAL;
+    }
+
+    if (vtd_pe_pgtt_is_pt(pe)) {
+        hwpt_id = idev->hwpt_id;
+    } else if (vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        return -EINVAL;
+    }
+
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id,

errp);

Quoted 8 lines

hw/i386/intel_iommu.c static void · line 2445

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid,

hwpt_id, !ret);
why !ret in the trace? I think e chose to return true on success for

host_iommu_device_iommufd_attach_hwpt()

. Let's keep this consistent otherwise we end up not knowning what is the success value.

OK, will return bool for all funcs in this patch, previously I thought intel_iommu.c honors int return value.

Quoted 65 lines

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2456

Quoted 11 lines
hw/i386/intel_iommu.c +11 -0
+    if (ret) {
+        /* Destroy old fs_hwpt if it's a replacement */
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        if (vtd_pe_pgtt_is_fst(pe)) {
+            vtd_as->fs_hwpt = hwpt_id;
+        }
+    } else if (vtd_pe_pgtt_is_fst(pe)) {
+        iommufd_backend_free_id(idev->iommufd, hwpt_id);
+    }
+
+    return !ret;

argh. Eventually it is difficult to follow ;-)

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+}
+
+static int vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
+                                     VTDAddressSpace *vtd_as,

Error **errp)

hw/i386/intel_iommu.c static void · line 2462

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+{
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 11 lines
hw/i386/intel_iommu.c +7 -0
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint32_t pasid = vtd_as->pasid;
+    bool ret;
+
+    if (s->dmar_enabled && s->root_scalable) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        trace_vtd_device_detach_hwpt(idev->devid, pasid, !ret);

same

hw/i386/intel_iommu.c +3 -0
+    } else {
+        /*
+         * If DMAR remapping is disabled or guest switches to legacy

mode,

Quoted 1 line

+ * we fallback to the default HWPT which contains shadow page

table.

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+         * So guest DMA could still work.
+         */
+        ret = host_iommu_device_iommufd_attach_hwpt(idev,

idev->hwpt_id, errp);

Quoted 1 line

+ trace_vtd_device_reattach_def_hwpt(idev->devid, pasid,

idev->hwpt_id,

Quoted 1 line

+ !ret);

same

Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+    }
+
+    if (ret) {
+        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+    }
+
+    return !ret;
+}
+
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s) · line 2500

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    int ret;
+
+    /* Ignore emulated device or legacy VFIO backed device */
+    if (!vtd_hiod) {
+        return 0;
+    }
+
+    if (pc_entry->valid) {
+        ret = vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);

return directly

Quoted 2 lines
Quoted 1 line

+ } else {

remove the else and return directly?

Will address all above comments.

Thanks
Zhenzhong

Quoted 85 lines

hw/i386/intel_iommu.c static void

Quoted 16 lines
hw/i386/intel_iommu.c +12 -0
+        ret = vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
+    }
+
+    return ret;
+}
+#else
+static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
+{
+    return 0;
+}
+#endif
+
 /* Do a context-cache device-selective invalidation.
  * @func_mask: FM field after shifting
  */
@@ -3134,6 +3265,8 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +2 -0
     VTDPASIDEntry pe;
     IOMMUNotifier *n;
     uint16_t did;
+    const char *err_prefix;
+    Error *local_err = NULL;
 
     if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
         if (!pc_entry->valid) {
@@ -3154,7 +3287,9 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

hw/i386/intel_iommu.c static void

Quoted 11 lines
hw/i386/intel_iommu.c +3 -1
             vtd_address_space_unmap(vtd_as, n);
         }
         vtd_switch_address_space(vtd_as);
-        return;
+
+        err_prefix = "Detaching from HWPT failed: ";
+        goto do_bind_unbind;
     }
 
     /*
@@ -3182,12 +3317,21 @@ static void

vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

Quoted 6 lines
hw/i386/intel_iommu.c +2 -1
     if (!pc_entry->valid) {
         pc_entry->pasid_entry = pe;
         pc_entry->valid = true;
-    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
+        err_prefix = "Attaching to HWPT failed: ";
+    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

Quoted 10 lines
hw/i386/intel_iommu.c +5 -0
+        err_prefix = "Replacing HWPT attachment failed: ";
+    } else {
         return;
     }
 
     vtd_switch_address_space(vtd_as);
     vtd_address_space_sync(vtd_as);
+
+do_bind_unbind:
+    /* TODO: Fault event injection into guest, report error to QEMU for

now */

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
+    if (vtd_bind_guest_pasid(vtd_as, &local_err)) {
+        error_reportf_err(local_err, "%s", err_prefix);
+    }
 }
 
 static void vtd_pasid_cache_sync(IntelIOMMUState *s,

VTDPASIDCacheInfo *pc_info)

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail

Quoted 5 lines
hw/i386/trace-events +0 -0
index b704f4f90c..5a3ee1cf64 100644
@@ -73,6 +73,9 @@ vtd_warn_invalid_qi_tail(uint16_t tail) "tail

0x%"PRIx16

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 73

Quoted 1 line
hw/i386/trace-events +0 -0
 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid

0x%"PRIx16" index %d vec %d (should be: %d)"

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 74

Quoted 1 line
hw/i386/trace-events +0 -0
 vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid

0x%"PRIx16" index %d trigger %d (should be: %d)"

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 76

Quoted 2 lines
hw/i386/trace-events +1 -0
 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"

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 77

Quoted 1 line
hw/i386/trace-events +1 -0
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret)

"dev_id %d pasid %d ret: %d"

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 78

Quoted 1 line
hw/i386/trace-events +1 -0
+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"

hw/i386/trace-events vtd_warn_invalid_qi_tail(uint16_t tail) "tail · line 80

Quoted 3 lines
hw/i386/trace-events +0 -0
 # amd_iommu.c
 amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at

addr 0x%"PRIx64" + offset 0x%"PRIx32
Eric

#39 lore raw

PATCH v7 14/23 intel_iommu: Propagate PASID-based iotlb invalidation to host

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This traps the guest PASID-based iotlb invalidation request and propagate it to host.

Intel VT-d 3.0 supports nested translation in PASID granularity. Guest SVA support could be implemented by configuring nested translation on specific pasid. This is also known as dual stage DMA translation.

Under such configuration, guest owns the GVA->GPA translation which is configured as first stage page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first stage translation table, piotlb invalidation should be propagated to host since host IOMMU will cache first level page table related mappings during DMA address translation.

---
 hw/i386/intel_iommu_internal.h |  6 +++
 hw/i386/intel_iommu.c          | 87 ++++++++++++++++++++++++++++++++--
 2 files changed, 90 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +6 -0
index df80af839d..97b48544d2 100644
@@ -621,6 +621,12 @@ typedef struct VTDPASIDCacheInfo {
     uint32_t pasid;
 } VTDPASIDCacheInfo;
 
+typedef struct VTDPIOTLBInvInfo {
+    uint16_t domain_id;
+    uint32_t pasid;
+    struct iommu_hwpt_vtd_s1_invalidate *inv_data;
+} VTDPIOTLBInvInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +84 -3

128 lines not on this page · raw message

Signature · 1 line
2.47.1
#40 lore raw

Re: [PATCH v7 14/23] intel_iommu: Propagate PASID-based iotlb invalidation to host

Hi Zhenzhong,

Quoted 103 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This traps the guest PASID-based iotlb invalidation request and propagate it to host.

Intel VT-d 3.0 supports nested translation in PASID granularity. Guest SVA support could be implemented by configuring nested translation on specific pasid. This is also known as dual stage DMA translation.

Under such configuration, guest owns the GVA->GPA translation which is configured as first stage page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first stage translation table, piotlb invalidation should be propagated to host since host IOMMU will cache first level page table related mappings during DMA address translation.

---
 hw/i386/intel_iommu_internal.h |  6 +++
 hw/i386/intel_iommu.c          | 87 ++++++++++++++++++++++++++++++++--
 2 files changed, 90 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +6 -0
index df80af839d..97b48544d2 100644
@@ -621,6 +621,12 @@ typedef struct VTDPASIDCacheInfo {
     uint32_t pasid;
 } VTDPASIDCacheInfo;
 
+typedef struct VTDPIOTLBInvInfo {
+    uint16_t domain_id;
+    uint32_t pasid;
+    struct iommu_hwpt_vtd_s1_invalidate *inv_data;
+} VTDPIOTLBInvInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +55 -0
index 3789a36147..ef6477de53 100644
@@ -2504,11 +2504,88 @@ static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
 
     return ret;
 }
+
+/*
+ * This function is a loop function for the s->vtd_address_spaces
+ * list with VTDPIOTLBInvInfo as execution filter. It propagates
+ * the piotlb invalidation to host.
+ */
+static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
+                                         gpointer user_data)
+{
+    VTDPIOTLBInvInfo *piotlb_info = user_data;
+    VTDAddressSpace *vtd_as = value;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    uint16_t did;
+
+    if (!vtd_hiod) {
+        return;
+    }
+
+    assert(vtd_as->pasid == PCI_NO_PASID);
+
+    /* Nothing to do if there is no first stage HWPT attached */
+    if (!pc_entry->valid ||
+        !vtd_pe_pgtt_is_fst(&pc_entry->pasid_entry)) {
+        return;
+    }
+
+    did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
+
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+        HostIOMMUDeviceIOMMUFD *idev =
+            HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+        uint32_t entry_num = 1; /* Only implement one request for simplicity */
+        Error *local_err = NULL;
+        struct iommu_hwpt_vtd_s1_invalidate *cache = piotlb_info->inv_data;
+
+        if (!iommufd_backend_invalidate_cache(idev->iommufd, vtd_as->fs_hwpt,
+                                              IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
+                                              sizeof(*cache), &entry_num, cache,
+                                              &local_err)) {
+            /* Something wrong in kernel, but trying to continue */
+            error_report_err(local_err);
+        }
+    }
+}
+
+static void
+vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
+                                 uint16_t domain_id, uint32_t pasid,
+                                 hwaddr addr, uint64_t npages, bool ih)
+{
+    struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
+    VTDPIOTLBInvInfo piotlb_info;
+
+    cache_info.addr = addr;

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp) · line 2571

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    cache_info.npages = npages;
+    cache_info.flags = ih ? IOMMU_VTD_INV_FLAGS_LEAF : 0;
+
+    piotlb_info.domain_id = domain_id;
+    piotlb_info.pasid = pasid;
+    piotlb_info.inv_data = &cache_info;
+
+    /*
+     * Go through each vtd_as instance in s->vtd_address_spaces, find out
+     * the affected host device which need host piotlb invalidation. Piotlb

the affected host devices? There might be several of them, isn't it?

Quoted 21 lines
hw/i386/intel_iommu.c +12 -0
+     * invalidation should check pasid cache per architecture point of view.
+     */
+    g_hash_table_foreach(s->vtd_address_spaces,
+                         vtd_flush_host_piotlb_locked, &piotlb_info);
+}
 #else
 static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
 {
     return 0;
 }
+
+static void
+vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
+                                 uint16_t domain_id, uint32_t pasid,
+                                 hwaddr addr, uint64_t npages, bool ih)
+{
+}
 #endif
 
 /* Do a context-cache device-selective invalidation.

hw/i386/intel_iommu.c static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s, · line 3235

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -3155,6 +3232,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
                                 &info);
+    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, 0, (uint64_t)-1, 0);

UINT64_MAX

Quoted 22 lines
hw/i386/intel_iommu.c +3 -1
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
@@ -3174,7 +3252,8 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
 }
 
 static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
-                                       uint32_t pasid, hwaddr addr, uint8_t am)
+                                       uint32_t pasid, hwaddr addr, uint8_t am,
+                                       bool ih)
 {
     VTDIOTLBPageInvInfo info;
 
@@ -3186,6 +3265,7 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb,
                                 vtd_hash_remove_by_page_piotlb, &info);
+    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, addr, 1 << am, ih);
     vtd_iommu_unlock(s);
 
     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, pasid);

hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3220

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
@@ -3217,7 +3297,8 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
     case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
         am = VTD_INV_DESC_PIOTLB_AM(inv_desc->val[1]);
         addr = (hwaddr) VTD_INV_DESC_PIOTLB_ADDR(inv_desc->val[1]);
-        vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am);

it is not obvious we hold the lock here

Quoted 6 lines
hw/i386/intel_iommu.c +2 -0
+        vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am,
+                                   VTD_INV_DESC_PIOTLB_IH(inv_desc->val[1]));
         break;
 
     default:

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5526

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
@@ -5439,7 +5520,7 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
     ret = 0;
     switch (pgtt) {
     case VTD_SM_PASID_ENTRY_FST:
-        vtd_piotlb_page_invalidate(s, domain_id, vtd_as->pasid, addr, 0);
+        vtd_piotlb_page_invalidate(s, domain_id, vtd_as->pasid, addr, 0, 0);
         break;
     /* Room for other pgtt values */
     default:

Thanks

Eric

#41 lore raw

RE: [PATCH v7 14/23] intel_iommu: Propagate PASID-based iotlb invalidation to host

Quoted 134 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 14/23] intel_iommu: Propagate PASID-based iotlb invalidation to host

Hi Zhenzhong,

Quoted 5 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This traps the guest PASID-based iotlb invalidation request and propagate it to host.

Quoted 8 lines

Intel VT-d 3.0 supports nested translation in PASID granularity. Guest SVA support could be implemented by configuring nested translation on specific pasid. This is also known as dual stage DMA translation.

Under such configuration, guest owns the GVA->GPA translation which is configured as first stage page table on host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first stage translation table, piotlb invalidation should be propagated to host since host IOMMU will

cache

Quoted 8 lines

first level page table related mappings during DMA address translation.

---
 hw/i386/intel_iommu_internal.h |  6 +++
 hw/i386/intel_iommu.c          | 87

++++++++++++++++++++++++++++++++--

Review

Quoted 3 lines
 2 files changed, 90 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu.c static int

Quoted 21 lines

index df80af839d..97b48544d2 100644

hw/i386/intel_iommu_internal.h +6 -0
@@ -621,6 +621,12 @@ typedef struct VTDPASIDCacheInfo {
     uint32_t pasid;
 } VTDPASIDCacheInfo;
 
+typedef struct VTDPIOTLBInvInfo {
+    uint16_t domain_id;
+    uint32_t pasid;
+    struct iommu_hwpt_vtd_s1_invalidate *inv_data;
+} VTDPIOTLBInvInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +0 -0
index 3789a36147..ef6477de53 100644
@@ -2504,11 +2504,88 @@ static int

vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 26 lines
hw/i386/intel_iommu.c +23 -0
     return ret;
 }
+
+/*
+ * This function is a loop function for the s->vtd_address_spaces
+ * list with VTDPIOTLBInvInfo as execution filter. It propagates
+ * the piotlb invalidation to host.
+ */
+static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
+                                         gpointer user_data)
+{
+    VTDPIOTLBInvInfo *piotlb_info = user_data;
+    VTDAddressSpace *vtd_as = value;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    uint16_t did;
+
+    if (!vtd_hiod) {
+        return;
+    }
+
+    assert(vtd_as->pasid == PCI_NO_PASID);
+
+    /* Nothing to do if there is no first stage HWPT attached */
+    if (!pc_entry->valid ||

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        !vtd_pe_pgtt_is_fst(&pc_entry->pasid_entry)) {
+        return;
+    }
+
+    did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
+
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+        HostIOMMUDeviceIOMMUFD *idev =
+            HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+        uint32_t entry_num = 1; /* Only implement one request for

simplicity */

hw/i386/intel_iommu.c static int · line 2540

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+        Error *local_err = NULL;
+        struct iommu_hwpt_vtd_s1_invalidate *cache =

piotlb_info->inv_data;

hw/i386/intel_iommu.c static int · line 2542

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+
+        if (!iommufd_backend_invalidate_cache(idev->iommufd,

vtd_as->fs_hwpt,

hw/i386/intel_iommu.c static int · line 2543

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,

hw/i386/intel_iommu.c static int · line 2544

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                              sizeof(*cache),

&entry_num, cache,

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+                                              &local_err)) {
+            /* Something wrong in kernel, but trying to continue */
+            error_report_err(local_err);
+        }
+    }
+}
+
+static void
+vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
+                                 uint16_t domain_id, uint32_t

pasid,

hw/i386/intel_iommu.c static int · line 2555

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                 hwaddr addr, uint64_t npages,

bool ih)

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+{
+    struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
+    VTDPIOTLBInvInfo piotlb_info;
+

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    cache_info.addr = addr;
+    cache_info.npages = npages;
+    cache_info.flags = ih ? IOMMU_VTD_INV_FLAGS_LEAF : 0;
+
+    piotlb_info.domain_id = domain_id;
+    piotlb_info.pasid = pasid;
+    piotlb_info.inv_data = &cache_info;
+
+    /*
+     * Go through each vtd_as instance in s->vtd_address_spaces, find

out

Quoted 2 lines

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp) · line 2571

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * the affected host device which need host piotlb invalidation. Piotlb

the affected host devices? There might be several of them, isn't it?

Yes, it's possible, especially if they are under same iommu group in the guest.

Quoted 64 lines

hw/i386/intel_iommu.c static int · line 2571

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * invalidation should check pasid cache per architecture point of

view.

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     */
+    g_hash_table_foreach(s->vtd_address_spaces,
+                         vtd_flush_host_piotlb_locked,

&piotlb_info);

hw/i386/intel_iommu.c static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)

Quoted 10 lines
hw/i386/intel_iommu.c +5 -0
+}
 #else
 static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
 {
     return 0;
 }
+
+static void
+vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
+                                 uint16_t domain_id, uint32_t

pasid,

hw/i386/intel_iommu.c static int · line 2585

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                 hwaddr addr, uint64_t npages,

bool ih)

hw/i386/intel_iommu.c static void

Quoted 6 lines
hw/i386/intel_iommu.c +2 -0
+{
+}
 #endif
 
 /* Do a context-cache device-selective invalidation.
@@ -3155,6 +3232,7 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, · line 3266

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb,

vtd_hash_remove_by_pasid,

hw/i386/intel_iommu.c static void · line 3235

Quoted 2 lines
hw/i386/intel_iommu.c +1 -0
                                 &info);
+    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, 0, (uint64_t)-1,

0);
UINT64_MAX

hw/i386/intel_iommu.c static void

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
@@ -3174,7 +3252,8 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void · line 3254

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
 }
 
 static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t

domain_id,

hw/i386/intel_iommu.c static void · line 3177

Quoted 1 line
hw/i386/intel_iommu.c +0 -1
-                                       uint32_t pasid, hwaddr

addr, uint8_t am)

hw/i386/intel_iommu.c static void · line 3255

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                       uint32_t pasid, hwaddr

addr, uint8_t am,

hw/i386/intel_iommu.c static void

Quoted 5 lines
hw/i386/intel_iommu.c +1 -0
+                                       bool ih)
 {
     VTDIOTLBPageInvInfo info;
 
@@ -3186,6 +3265,7 @@ static void

vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

hw/i386/intel_iommu.c static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb,
                                 vtd_hash_remove_by_page_piotlb,

&info);

hw/i386/intel_iommu.c static void · line 3268

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, addr, 1 << am,

ih);

hw/i386/intel_iommu.c static bool

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
     vtd_iommu_unlock(s);
 
     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, pasid);
@@ -3217,7 +3297,8 @@ static bool

vtd_process_piotlb_desc(IntelIOMMUState *s,

Quoted 6 lines

hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
     case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
         am = VTD_INV_DESC_PIOTLB_AM(inv_desc->val[1]);
         addr = (hwaddr)

VTD_INV_DESC_PIOTLB_ADDR(inv_desc->val[1]);

hw/i386/intel_iommu.c static bool vtd_process_piotlb_desc(IntelIOMMUState *s, · line 3220

Quoted 1 line
hw/i386/intel_iommu.c +0 -1
-        vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am);

it is not obvious we hold the lock here

Do you mean vtd_iommu_lock? It's hold inside vtd_piotlb_page_invalidate().

Thanks
Zhenzhong

#42 lore raw

PATCH v7 15/23 intel_iommu: Replay all pasid bindings when either SRTP or TE bit is changed

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

When either 'Set Root Table Pointer' or 'Translation Enable' bit is changed, all pasid bindings on host side become stale and need to be updated.

Introduce a helper function vtd_replay_pasid_bindings_all() to go through all pasid entries in all passthrough devices to update host side bindings.

---
 hw/i386/intel_iommu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/i386/intel_iommu.c +10 -0
index ef6477de53..1f78274204 100644
@@ -92,6 +92,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 int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
+static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -2894,6 +2895,7 @@ static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
     vtd_reset_caches(s);
     vtd_address_space_refresh_all(s);
+    vtd_replay_pasid_bindings_all(s);
 }
 
 /* Set Interrupt Remap Table Pointer */
@@ -2928,6 +2930,7 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
 
     vtd_reset_caches(s);
     vtd_address_space_refresh_all(s);
+    vtd_replay_pasid_bindings_all(s);
 }
 
 /* Handle Interrupt Remap Enable/Disable */
@@ -3427,6 +3430,13 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
     vtd_iommu_unlock(s);
 }
 
+static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type = VTD_INV_DESC_PASIDC_G_GLOBAL };
+
+    vtd_pasid_cache_sync(s, &pc_info);
+}
+
 static bool vtd_process_pasid_desc(IntelIOMMUState *s,
                                    VTDInvDesc *inv_desc)
 {
Signature · 1 line
2.47.1
#43 lore raw

PATCH v7 16/23 intel_iommu: Replay pasid bindings after context cache invalidation

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This replays guest pasid bindings after context cache invalidation. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing context cache invalidation.

We see old linux such as 6.7.0-rc2 not following the spec, it sends pasid cache invalidation before context cache invalidation, then QEMU depends on context cache invalidation to get pasid entry and setup binding.

---
 hw/i386/intel_iommu.c | 47 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/trace-events  |  1 +
 2 files changed, 48 insertions(+)
hw/i386/intel_iommu.c +47 -0
index 1f78274204..edd1416382 100644
@@ -93,6 +93,8 @@ static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
 static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s);
+static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
+                                        gpointer user_data);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -2388,6 +2390,13 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
      * VT-d emulation codes.
      */
     vtd_iommu_replay_all(s);
+    /*
+     * Same for pasid cache invalidation, per VT-d spec 6.5.2.1, a global
+     * context cache invalidation should be followed by global PASID cache
+     * invalidation. In order to work with guest not following spec,
+     * handle global PASID cache invalidation here.
+     */
+    vtd_replay_pasid_bindings_all(s);
 }
 
 #ifdef CONFIG_IOMMUFD
@@ -2589,6 +2598,35 @@ vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
 }
 #endif
 
+static void vtd_pasid_cache_devsi(VTDAddressSpace *vtd_as)
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    PCIBus *bus = vtd_as->bus;
+    uint8_t devfn = vtd_as->devfn;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+        .pasid = vtd_as->pasid,
+    };
+    VTDPASIDCacheInfo pc_info;
+
+    if (!s->fsts || !s->root_scalable || !s->dmar_enabled) {
+        return;
+    }
+
+    trace_vtd_pasid_cache_devsi(pci_bus_num(bus),
+                                VTD_PCI_SLOT(devfn), VTD_PCI_FUNC(devfn));
+
+    /* We fake to be global invalidation just to bypass all checks */
+    pc_info.type = VTD_INV_DESC_PASIDC_G_GLOBAL;
+
+    /*
+     * We already get vtd_as of the device whose PASID cache is invalidated,
+     * so just call vtd_pasid_cache_sync_locked() once.
+     */
+    vtd_pasid_cache_sync_locked(&key, vtd_as, &pc_info);
+}
+
 /* Do a context-cache device-selective invalidation.
  * @func_mask: FM field after shifting
  */
@@ -2647,6 +2685,15 @@ static void vtd_context_device_invalidate(IntelIOMMUState *s,
              * happened.
              */
             vtd_address_space_sync(vtd_as);
+            /*
+             * Per spec 6.5.2.1, context flush should be followed by PASID
+             * cache and iotlb flush. In order to work with a guest which does
+             * not follow spec and missed PASID cache flush, e.g., linux
+             * 6.7.0-rc2, we have vtd_pasid_cache_devsi() to invalidate PASID
+             * cache of passthrough device. Host iommu driver would flush
+             * piotlb when a pasid unbind is pass down to it.
+             */
+            vtd_pasid_cache_devsi(vtd_as);
         }
     }
 }
hw/i386/trace-events +1 -0
index 5a3ee1cf64..5fa5e93b68 100644
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) ""
 vtd_inv_desc_pasid_cache_gsi(void) ""
 vtd_inv_desc_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16
 vtd_inv_desc_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
+vtd_pasid_cache_devsi(uint8_t bus, uint8_t dev, uint8_t fn) "Dev selective PC invalidation dev: %02"PRIx8":%02"PRIx8".%02"PRIx8
 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present"
 vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present"
 vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
Signature · 1 line
2.47.1
Reviewed-by on v7 16/23
#44 lore raw

Re: [PATCH v7 16/23] intel_iommu: Replay pasid bindings after context cache invalidation

Quoted 59 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This replays guest pasid bindings after context cache invalidation. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing context cache invalidation.

We see old linux such as 6.7.0-rc2 not following the spec, it sends pasid cache invalidation before context cache invalidation, then QEMU depends on context cache invalidation to get pasid entry and setup binding.

---
 hw/i386/intel_iommu.c | 47 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/trace-events  |  1 +
 2 files changed, 48 insertions(+)
hw/i386/intel_iommu.c +19 -0
index 1f78274204..edd1416382 100644
@@ -93,6 +93,8 @@ static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
 static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s);
+static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
+                                        gpointer user_data);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -2388,6 +2390,13 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
      * VT-d emulation codes.
      */
     vtd_iommu_replay_all(s);
+    /*
+     * Same for pasid cache invalidation, per VT-d spec 6.5.2.1, a global
+     * context cache invalidation should be followed by global PASID cache
+     * invalidation. In order to work with guest not following spec,
+     * handle global PASID cache invalidation here.
+     */
+    vtd_replay_pasid_bindings_all(s);
 }
 
 #ifdef CONFIG_IOMMUFD
@@ -2589,6 +2598,35 @@ vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
 }
 #endif
 
+static void vtd_pasid_cache_devsi(VTDAddressSpace *vtd_as)
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    PCIBus *bus = vtd_as->bus;
+    uint8_t devfn = vtd_as->devfn;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+        .pasid = vtd_as->pasid,
+    };

hw/i386/intel_iommu.c vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, · line 2620

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    VTDPASIDCacheInfo pc_info;
+
+    if (!s->fsts || !s->root_scalable || !s->dmar_enabled) {
+        return;
+    }
+
+    trace_vtd_pasid_cache_devsi(pci_bus_num(bus),
+                                VTD_PCI_SLOT(devfn), VTD_PCI_FUNC(devfn));
+
+    /* We fake to be global invalidation just to bypass all checks */

can you clarify which checks and why you want to bypass them?

hw/i386/intel_iommu.c vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, · line 2624

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+    pc_info.type = VTD_INV_DESC_PASIDC_G_GLOBAL;
+
+    /*
+     * We already get vtd_as of the device whose PASID cache is invalidated,

s/get/got. Not sure the comment is worth.

Quoted 9 lines
hw/i386/intel_iommu.c +5 -0
+     * so just call vtd_pasid_cache_sync_locked() once.
+     */
+    vtd_pasid_cache_sync_locked(&key, vtd_as, &pc_info);
+}
+
 /* Do a context-cache device-selective invalidation.
  * @func_mask: FM field after shifting
  */

hw/i386/intel_iommu.c static void vtd_context_device_invalidate(IntelIOMMUState *s, · line 2694

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -2647,6 +2685,15 @@ static void vtd_context_device_invalidate(IntelIOMMUState *s,
              * happened.
              */
             vtd_address_space_sync(vtd_as);
+            /*
+             * Per spec 6.5.2.1, context flush should be followed by PASID
+             * cache and iotlb flush. In order to work with a guest which does
+             * not follow spec and missed PASID cache flush, e.g., linux
+             * 6.7.0-rc2, we have vtd_pasid_cache_devsi() to invalidate PASID
+             * cache of passthrough device. Host iommu driver would flush
+             * piotlb when a pasid unbind is pass down to it.

passed

Quoted 10 lines
hw/i386/intel_iommu.c +2 -0
+             */
+            vtd_pasid_cache_devsi(vtd_as);
         }
     }
 }
hw/i386/trace-events +0 -0
index 5a3ee1cf64..5fa5e93b68 100644

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 34

Quoted 7 lines
hw/i386/trace-events +1 -0
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) ""
 vtd_inv_desc_pasid_cache_gsi(void) ""
 vtd_inv_desc_pasid_cache_dsi(uint16_t domain) "Domain selective PC invalidation domain 0x%"PRIx16
 vtd_inv_desc_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
+vtd_pasid_cache_devsi(uint8_t bus, uint8_t dev, uint8_t fn) "Dev selective PC invalidation dev: %02"PRIx8":%02"PRIx8".%02"PRIx8
 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present"
 vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present"
 vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16

Besides

Eric

#45 lore raw

RE: [PATCH v7 16/23] intel_iommu: Replay pasid bindings after context cache invalidation

Quoted 84 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 16/23] intel_iommu: Replay pasid bindings after context cache invalidation

Quoted 11 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This replays guest pasid bindings after context cache invalidation. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing context cache invalidation.

We see old linux such as 6.7.0-rc2 not following the spec, it sends pasid cache invalidation before context cache invalidation, then QEMU depends on context cache invalidation to get pasid entry and setup binding.

Quoted 4 lines
---
 hw/i386/intel_iommu.c | 47

+++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 8 lines
 hw/i386/trace-events  |  1 +
 2 files changed, 48 insertions(+)
hw/i386/intel_iommu.c +0 -0
index 1f78274204..edd1416382 100644
@@ -93,6 +93,8 @@ static void

vtd_address_space_refresh_all(IntelIOMMUState *s);

hw/i386/intel_iommu.c static void · line 93

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
 static void vtd_address_space_unmap(VTDAddressSpace *as,

IOMMUNotifier *n);

hw/i386/intel_iommu.c static void

Quoted 8 lines
hw/i386/intel_iommu.c +2 -0
 static int vtd_bind_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s);
+static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
+                                        gpointer user_data);
 
 static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
 {
@@ -2388,6 +2390,13 @@ static void

vtd_context_global_invalidate(IntelIOMMUState *s)

hw/i386/intel_iommu.c static void vtd_context_global_invalidate(IntelIOMMUState *s)

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
      * VT-d emulation codes.
      */
     vtd_iommu_replay_all(s);
+    /*
+     * Same for pasid cache invalidation, per VT-d spec 6.5.2.1, a global
+     * context cache invalidation should be followed by global PASID

cache

hw/i386/intel_iommu.c

Quoted 8 lines
hw/i386/intel_iommu.c +4 -0
+     * invalidation. In order to work with guest not following spec,
+     * handle global PASID cache invalidation here.
+     */
+    vtd_replay_pasid_bindings_all(s);
 }
 
 #ifdef CONFIG_IOMMUFD
@@ -2589,6 +2598,35 @@

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,

Quoted 11 lines
hw/i386/intel_iommu.c +8 -0
 }
 #endif
 
+static void vtd_pasid_cache_devsi(VTDAddressSpace *vtd_as)
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    PCIBus *bus = vtd_as->bus;
+    uint8_t devfn = vtd_as->devfn;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,

hw/i386/intel_iommu.c vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        .pasid = vtd_as->pasid,
+    };
+    VTDPASIDCacheInfo pc_info;
+
+    if (!s->fsts || !s->root_scalable || !s->dmar_enabled) {
+        return;
+    }
+
+    trace_vtd_pasid_cache_devsi(pci_bus_num(bus),
+                                VTD_PCI_SLOT(devfn),

VTD_PCI_FUNC(devfn));

Quoted 3 lines

hw/i386/intel_iommu.c line 2620

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+
+    /* We fake to be global invalidation just to bypass all checks */

can you clarify which checks and why you want to bypass them?

All below checks in vtd_pasid_cache_sync_locked(). Because after context cache invalidation, all cached pasid entry for this device become stale. We need to invalidate them all.

    switch (pc_info->type) {
    case VTD_INV_DESC_PASIDC_G_PASID_SI:
        if (pc_info->pasid != vtd_as->pasid) {
            return;
        }
        /* Fall through */
    case VTD_INV_DESC_PASIDC_G_DSI:
        if (pc_entry->valid) {
            did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
        } else {
            did = VTD_SM_PASID_ENTRY_DID(&pe);
        }
        if (pc_info->did != did) {
            return;
        }
    }
Quoted 6 lines

hw/i386/intel_iommu.c vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+    pc_info.type = VTD_INV_DESC_PASIDC_G_GLOBAL;
+
+    /*
+     * We already get vtd_as of the device whose PASID cache is

invalidated,
s/get/got. Not sure the comment is worth.

OK, I can remove it.

Quoted 17 lines

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +5 -0
+     * so just call vtd_pasid_cache_sync_locked() once.
+     */
+    vtd_pasid_cache_sync_locked(&key, vtd_as, &pc_info);
+}
+
 /* Do a context-cache device-selective invalidation.
  * @func_mask: FM field after shifting
  */
@@ -2647,6 +2685,15 @@ static void

vtd_context_device_invalidate(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void vtd_context_device_invalidate(IntelIOMMUState *s,

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
              * happened.
              */
             vtd_address_space_sync(vtd_as);
+            /*
+             * Per spec 6.5.2.1, context flush should be followed by

PASID

hw/i386/intel_iommu.c static void · line 2690

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+             * cache and iotlb flush. In order to work with a guest
Quoted 8 lines

which does

hw/i386/intel_iommu.c static void · line 2692

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+             * not follow spec and missed PASID cache flush, e.g., linux
+             * 6.7.0-rc2, we have vtd_pasid_cache_devsi() to invalidate

PASID

hw/i386/intel_iommu.c static void · line 2693

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+             * cache of passthrough device. Host iommu driver would

flush

hw/i386/intel_iommu.c static void vtd_context_device_invalidate(IntelIOMMUState *s, · line 2694

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+             * piotlb when a pasid unbind is pass down to it.

passed

Will do

Thanks
Zhenzhong

Quoted 28 lines

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 29

Quoted 12 lines
hw/i386/intel_iommu.c +2 -0
+             */
+            vtd_pasid_cache_devsi(vtd_as);
         }
     }
 }
hw/i386/trace-events +0 -0
index 5a3ee1cf64..5fa5e93b68 100644
@@ -28,6 +28,7 @@ vtd_pasid_cache_reset(void) ""
 vtd_inv_desc_pasid_cache_gsi(void) ""
 vtd_inv_desc_pasid_cache_dsi(uint16_t domain) "Domain selective PC

invalidation domain 0x%"PRIx16

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 30

Quoted 1 line
hw/i386/trace-events +0 -0
 vtd_inv_desc_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID

selective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 31

Quoted 1 line
hw/i386/trace-events +1 -0
+vtd_pasid_cache_devsi(uint8_t bus, uint8_t dev, uint8_t fn) "Dev selective

PC invalidation dev: %02"PRIx8":%02"PRIx8".%02"PRIx8

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 33

Quoted 2 lines
hw/i386/trace-events +0 -0
 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"

hw/i386/trace-events vtd_pasid_cache_reset(void) "" · line 34

Quoted 1 line
hw/i386/trace-events +0 -0
 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
Besides

Eric

#46 lore raw

PATCH v7 17/23 iommufd: Introduce a helper function to extract vendor capabilities

In reply to Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

---
 include/hw/iommu.h                 |  5 +++++
 include/system/host_iommu_device.h | 15 +++++++++++++++
 backends/iommufd.c                 | 13 +++++++++++++
 3 files changed, 33 insertions(+)
include/hw/iommu.h +5 -0
index 9b8bb94fc2..6d61410703 100644
@@ -22,4 +22,9 @@ enum viommu_flags {
     VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
 };
 
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
+};
+
 #endif /* HW_IOMMU_H */
include/system/host_iommu_device.h +15 -0
index ab849a4a82..9ae7f4cc6d 100644
@@ -39,6 +39,21 @@ typedef struct HostIOMMUDeviceCaps {
     uint64_t hw_caps;
     VendorCaps vendor_caps;
 } HostIOMMUDeviceCaps;
+
+/**
+ * host_iommu_extract_quirk: Extract host IOMMU quirks
+ *
+ * This function converts @type specific hardware information data
+ * into a standard bitmap format.
+ *
+ * @type: IOMMU Hardware Info Types
+ *
+ * @VendorCaps: IOMMU @type specific hardware information data
+ *
+ * Returns: bitmap with each representing a host IOMMU quirk defined in
+ * enum host_iommu_quirks
+ */
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps);
 #endif
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
backends/iommufd.c +13 -0
index 086bd67aea..61b991ec53 100644
@@ -19,6 +19,7 @@
 #include "migration/cpr.h"
 #include "monitor/monitor.h"
 #include "trace.h"
+#include "hw/iommu.h"
 #include "hw/vfio/vfio-device.h"
 #include <sys/ioctl.h>
 #include <linux/iommufd.h>
@@ -411,6 +412,18 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps)
+{
+    uint64_t quirks = 0;
+
+    if (type == IOMMU_HW_INFO_TYPE_INTEL_VTD &&
+        caps->vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17) {
+        quirks |= HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO;
+    }
+
+    return quirks;
+}
+
 bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
                                       uint32_t data_type, uint32_t entry_len,
                                       uint32_t *entry_num, void *data,
Signature · 1 line
2.47.1
#47 lore raw

Re: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

Quoted 49 lines · Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

---
  include/hw/iommu.h                 |  5 +++++
  include/system/host_iommu_device.h | 15 +++++++++++++++
  backends/iommufd.c                 | 13 +++++++++++++
  3 files changed, 33 insertions(+)
include/hw/iommu.h +5 -0
index 9b8bb94fc2..6d61410703 100644
@@ -22,4 +22,9 @@ enum viommu_flags {
      VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
  };
 
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
+};
+
  #endif /* HW_IOMMU_H */
include/system/host_iommu_device.h +5 -0
index ab849a4a82..9ae7f4cc6d 100644
@@ -39,6 +39,21 @@ typedef struct HostIOMMUDeviceCaps {
      uint64_t hw_caps;
      VendorCaps vendor_caps;
  } HostIOMMUDeviceCaps;
+
+/**
+ * host_iommu_extract_quirk: Extract host IOMMU quirks
+ *
+ * This function converts @type specific hardware information data

include/system/host_iommu_device.h typedef struct HostIOMMUDeviceCaps { · line 56

Quoted 10 lines
include/system/host_iommu_device.h +10 -0
+ * into a standard bitmap format.
+ *
+ * @type: IOMMU Hardware Info Types
+ *
+ * @VendorCaps: IOMMU @type specific hardware information data
+ *
+ * Returns: bitmap with each representing a host IOMMU quirk defined in
+ * enum host_iommu_quirks
+ */
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps);

..._get_quirks() sounds nicer. This is minor.

Thanks,

C.

Quoted 34 lines
include/system/host_iommu_device.h +0 -0
  #endif
 
  #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
backends/iommufd.c +13 -0
index 086bd67aea..61b991ec53 100644
@@ -19,6 +19,7 @@
  #include "migration/cpr.h"
  #include "monitor/monitor.h"
  #include "trace.h"
+#include "hw/iommu.h"
  #include "hw/vfio/vfio-device.h"
  #include <sys/ioctl.h>
  #include <linux/iommufd.h>
@@ -411,6 +412,18 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
      return true;
  }
 
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps)
+{
+    uint64_t quirks = 0;
+
+    if (type == IOMMU_HW_INFO_TYPE_INTEL_VTD &&
+        caps->vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17) {
+        quirks |= HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO;
+    }
+
+    return quirks;
+}
+
  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
                                        uint32_t data_type, uint32_t entry_len,
                                        uint32_t *entry_num, void *data,
#48 lore raw

RE: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

Quoted 62 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

Review

Quoted 37 lines · Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

---
  include/hw/iommu.h                 |  5 +++++
  include/system/host_iommu_device.h | 15 +++++++++++++++
  backends/iommufd.c                 | 13 +++++++++++++
  3 files changed, 33 insertions(+)
include/hw/iommu.h +5 -0
index 9b8bb94fc2..6d61410703 100644
@@ -22,4 +22,9 @@ enum viommu_flags {
      VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
  };
 
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
+};
+
  #endif /* HW_IOMMU_H */
(quoted diff) +0 -0

b/include/system/host_iommu_device.h

Quoted 16 lines

index ab849a4a82..9ae7f4cc6d 100644

include/system/host_iommu_device.h +9 -0
@@ -39,6 +39,21 @@ typedef struct HostIOMMUDeviceCaps {
      uint64_t hw_caps;
      VendorCaps vendor_caps;
  } HostIOMMUDeviceCaps;
+
+/**
+ * host_iommu_extract_quirk: Extract host IOMMU quirks
+ *
+ * This function converts @type specific hardware information data
+ * into a standard bitmap format.
+ *
+ * @type: IOMMU Hardware Info Types
+ *
Quoted 8 lines

include/system/host_iommu_device.h typedef struct HostIOMMUDeviceCaps { · line 56

Quoted 6 lines
include/system/host_iommu_device.h +6 -0
+ * @VendorCaps: IOMMU @type specific hardware information data
+ *
+ * Returns: bitmap with each representing a host IOMMU quirk defined in
+ * enum host_iommu_quirks
+ */
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps);

..._get_quirks() sounds nicer. This is minor.

OK, will do.

Thanks
Zhenzhong

#49 lore raw

Re: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

In reply to Zhenzhong Duan
Quoted 27 lines · Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

---
  include/hw/iommu.h                 |  5 +++++
  include/system/host_iommu_device.h | 15 +++++++++++++++
  backends/iommufd.c                 | 13 +++++++++++++
  3 files changed, 33 insertions(+)
include/hw/iommu.h +0 -0
index 9b8bb94fc2..6d61410703 100644

include/hw/iommu.h enum viommu_flags { · line 27

Quoted 6 lines
include/hw/iommu.h +3 -0
@@ -22,4 +22,9 @@ enum viommu_flags {
      VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
  };
 
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),

This host IOMMU quirk definition in a vIOMMU header file is not very consistent.

Thanks,

C.

Quoted 60 lines
include/hw/iommu.h +2 -0
+};
+
  #endif /* HW_IOMMU_H */
include/system/host_iommu_device.h +15 -0
index ab849a4a82..9ae7f4cc6d 100644
@@ -39,6 +39,21 @@ typedef struct HostIOMMUDeviceCaps {
      uint64_t hw_caps;
      VendorCaps vendor_caps;
  } HostIOMMUDeviceCaps;
+
+/**
+ * host_iommu_extract_quirk: Extract host IOMMU quirks
+ *
+ * This function converts @type specific hardware information data
+ * into a standard bitmap format.
+ *
+ * @type: IOMMU Hardware Info Types
+ *
+ * @VendorCaps: IOMMU @type specific hardware information data
+ *
+ * Returns: bitmap with each representing a host IOMMU quirk defined in
+ * enum host_iommu_quirks
+ */
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps);
  #endif
 
  #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
backends/iommufd.c +13 -0
index 086bd67aea..61b991ec53 100644
@@ -19,6 +19,7 @@
  #include "migration/cpr.h"
  #include "monitor/monitor.h"
  #include "trace.h"
+#include "hw/iommu.h"
  #include "hw/vfio/vfio-device.h"
  #include <sys/ioctl.h>
  #include <linux/iommufd.h>
@@ -411,6 +412,18 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
      return true;
  }
 
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps)
+{
+    uint64_t quirks = 0;
+
+    if (type == IOMMU_HW_INFO_TYPE_INTEL_VTD &&
+        caps->vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17) {
+        quirks |= HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO;
+    }
+
+    return quirks;
+}
+
  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
                                        uint32_t data_type, uint32_t entry_len,
                                        uint32_t *entry_num, void *data,
#50 lore raw

RE: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

Quoted 38 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

Quoted 30 lines · Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

---
  include/hw/iommu.h                 |  5 +++++
  include/system/host_iommu_device.h | 15 +++++++++++++++
  backends/iommufd.c                 | 13 +++++++++++++
  3 files changed, 33 insertions(+)
include/hw/iommu.h +0 -0
index 9b8bb94fc2..6d61410703 100644
@@ -22,4 +22,9 @@ enum viommu_flags {
      VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
  };
 
Quoted 7 lines

include/hw/iommu.h enum viommu_flags { · line 27

Quoted 3 lines
include/hw/iommu.h +3 -0
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),

This host IOMMU quirk definition in a vIOMMU header file is not very consistent.

I take it as general IOMMMU header, both vIOMMU info and host IOMMU info can be there.

Thanks
Zhenzhong

#51 lore raw

Re: [PATCH v7 17/23] iommufd: Introduce a helper function to extract vendor capabilities

In reply to Zhenzhong Duan
Quoted 4 lines · Zhenzhong Duan

In VFIO core, we call iommufd_backend_get_device_info() to return vendor specific hardware information data, but it's not good to extract this raw data in VFIO core.

Quoted 8 lines

Introduce host_iommu_extract_quirks() to help extracting the raw data and return a bitmap in iommufd.c because it's the place defining iommufd_backend_get_device_info().

The other choice is to put vendor data extracting code in vendor vIOMMU emulation file, but that will make those files mixed with vIOMMU emulation and host IOMMU extracting code, also need a new callback in PCIIOMMUOps. So we choose a simpler way as above.

up to now iommufd.c was arch independent. And on the other hand you started to put iommufd specific info in intel_iommu.c so I am wondering whether this is the good place to put this helper. Again, maybe consider consider creating  an intel_iommu_accel.c or something alike

Eric

Quoted 34 lines
---
 include/hw/iommu.h                 |  5 +++++
 include/system/host_iommu_device.h | 15 +++++++++++++++
 backends/iommufd.c                 | 13 +++++++++++++
 3 files changed, 33 insertions(+)
include/hw/iommu.h +5 -0
index 9b8bb94fc2..6d61410703 100644
@@ -22,4 +22,9 @@ enum viommu_flags {
     VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
 };
 
+/* Host IOMMU quirks. Extracted from host IOMMU capabilities */
+enum host_iommu_quirks {
+    HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
+};
+
 #endif /* HW_IOMMU_H */
include/system/host_iommu_device.h +2 -0
index ab849a4a82..9ae7f4cc6d 100644
@@ -39,6 +39,21 @@ typedef struct HostIOMMUDeviceCaps {
     uint64_t hw_caps;
     VendorCaps vendor_caps;
 } HostIOMMUDeviceCaps;
+
+/**

include/system/host_iommu_device.h typedef struct HostIOMMUDeviceCaps { · line 53

Quoted 10 lines
include/system/host_iommu_device.h +10 -0
+ * host_iommu_extract_quirk: Extract host IOMMU quirks
+ *
+ * This function converts @type specific hardware information data
+ * into a standard bitmap format.
+ *
+ * @type: IOMMU Hardware Info Types
+ *
+ * @VendorCaps: IOMMU @type specific hardware information data
+ *
+ * Returns: bitmap with each representing a host IOMMU quirk defined in

s/each/each bit

Quoted 37 lines
include/system/host_iommu_device.h +3 -0
+ * enum host_iommu_quirks
+ */
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps);
 #endif
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
backends/iommufd.c +13 -0
index 086bd67aea..61b991ec53 100644
@@ -19,6 +19,7 @@
 #include "migration/cpr.h"
 #include "monitor/monitor.h"
 #include "trace.h"
+#include "hw/iommu.h"
 #include "hw/vfio/vfio-device.h"
 #include <sys/ioctl.h>
 #include <linux/iommufd.h>
@@ -411,6 +412,18 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
+uint64_t host_iommu_extract_quirks(uint32_t type, VendorCaps *caps)
+{
+    uint64_t quirks = 0;
+
+    if (type == IOMMU_HW_INFO_TYPE_INTEL_VTD &&
+        caps->vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17) {
+        quirks |= HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO;
+    }
+
+    return quirks;
+}
+
 bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
                                       uint32_t data_type, uint32_t entry_len,
                                       uint32_t *entry_num, void *data,
#52 lore raw

PATCH v7 18/23 vfio: Add a new element bypass_ro in VFIOContainer

In reply to Zhenzhong Duan

When bypass_ro is true, readonly memory section is bypassed from mapping in the container.

This is a preparing patch to workaround Intel ERRATA_772415, see changelog in next patch for details about the errata.

---
 include/hw/vfio/vfio-container.h |  1 +
 hw/vfio/listener.c               | 21 ++++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)
include/hw/vfio/vfio-container.h +1 -0
index 9f6e8cedfc..a7d5c5ed67 100644
@@ -52,6 +52,7 @@ struct VFIOContainer {
     QLIST_HEAD(, VFIODevice) device_list;
     GList *iova_ranges;
     NotifierWithReturn cpr_reboot_notifier;
+    bool bypass_ro;
 };
 
 #define TYPE_VFIO_IOMMU "vfio-iommu"
hw/vfio/listener.c +14 -7
index 2109101158..0862b2b834 100644
@@ -76,8 +76,13 @@ static bool vfio_log_sync_needed(const VFIOContainer *bcontainer)
     return true;
 }
 
-static bool vfio_listener_skipped_section(MemoryRegionSection *section)
+static bool vfio_listener_skipped_section(MemoryRegionSection *section,
+                                          bool bypass_ro)
 {
+    if (bypass_ro && section->readonly) {
+        return true;
+    }
+
     return (!memory_region_is_ram(section->mr) &&
             !memory_region_is_iommu(section->mr)) ||
            memory_region_is_protected(section->mr) ||
@@ -368,9 +373,9 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
 }
 
 static bool vfio_listener_valid_section(MemoryRegionSection *section,
-                                        const char *name)
+                                        bool bypass_ro, const char *name)
 {
-    if (vfio_listener_skipped_section(section)) {
+    if (vfio_listener_skipped_section(section, bypass_ro)) {
         trace_vfio_listener_region_skip(name,
                 section->offset_within_address_space,
                 section->offset_within_address_space +
@@ -497,7 +502,8 @@ void vfio_container_region_add(VFIOContainer *bcontainer,
     int ret;
     Error *err = NULL;
 
-    if (!vfio_listener_valid_section(section, "region_add")) {
+    if (!vfio_listener_valid_section(section, bcontainer->bypass_ro,
+                                     "region_add")) {
         return;
     }
 
@@ -663,7 +669,8 @@ static void vfio_listener_region_del(MemoryListener *listener,
     int ret;
     bool try_unmap = true;
 
-    if (!vfio_listener_valid_section(section, "region_del")) {
+    if (!vfio_listener_valid_section(section, bcontainer->bypass_ro,
+                                     "region_del")) {
         return;
     }
 
@@ -821,7 +828,7 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,
         container_of(listener, VFIODirtyRangesListener, listener);
     hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, "tracking_update") ||
+    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
         !vfio_get_section_iova_range(dirty->bcontainer, section,
                                      &iova, &end, NULL)) {
         return;
@@ -1215,7 +1222,7 @@ static void vfio_listener_log_sync(MemoryListener *listener,
     int ret;
     Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section)) {
+    if (vfio_listener_skipped_section(section, false)) {
         return;
     }
 
Signature · 1 line
2.47.1
#53 lore raw

Re: [PATCH v7 18/23] vfio: Add a new element bypass_ro in VFIOContainer

Quoted 77 lines · Zhenzhong Duan

When bypass_ro is true, readonly memory section is bypassed from mapping in the container.

This is a preparing patch to workaround Intel ERRATA_772415, see changelog in next patch for details about the errata.

---
 include/hw/vfio/vfio-container.h |  1 +
 hw/vfio/listener.c               | 21 ++++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)
include/hw/vfio/vfio-container.h +1 -0
index 9f6e8cedfc..a7d5c5ed67 100644
@@ -52,6 +52,7 @@ struct VFIOContainer {
     QLIST_HEAD(, VFIODevice) device_list;
     GList *iova_ranges;
     NotifierWithReturn cpr_reboot_notifier;
+    bool bypass_ro;
 };
 
 #define TYPE_VFIO_IOMMU "vfio-iommu"
hw/vfio/listener.c +12 -5
index 2109101158..0862b2b834 100644
@@ -76,8 +76,13 @@ static bool vfio_log_sync_needed(const VFIOContainer *bcontainer)
     return true;
 }
 
-static bool vfio_listener_skipped_section(MemoryRegionSection *section)
+static bool vfio_listener_skipped_section(MemoryRegionSection *section,
+                                          bool bypass_ro)
 {
+    if (bypass_ro && section->readonly) {
+        return true;
+    }
+
     return (!memory_region_is_ram(section->mr) &&
             !memory_region_is_iommu(section->mr)) ||
            memory_region_is_protected(section->mr) ||
@@ -368,9 +373,9 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
 }
 
 static bool vfio_listener_valid_section(MemoryRegionSection *section,
-                                        const char *name)
+                                        bool bypass_ro, const char *name)
 {
-    if (vfio_listener_skipped_section(section)) {
+    if (vfio_listener_skipped_section(section, bypass_ro)) {
         trace_vfio_listener_region_skip(name,
                 section->offset_within_address_space,
                 section->offset_within_address_space +
@@ -497,7 +502,8 @@ void vfio_container_region_add(VFIOContainer *bcontainer,
     int ret;
     Error *err = NULL;
 
-    if (!vfio_listener_valid_section(section, "region_add")) {
+    if (!vfio_listener_valid_section(section, bcontainer->bypass_ro,
+                                     "region_add")) {
         return;
     }
 
@@ -663,7 +669,8 @@ static void vfio_listener_region_del(MemoryListener *listener,
     int ret;
     bool try_unmap = true;
 
-    if (!vfio_listener_valid_section(section, "region_del")) {
+    if (!vfio_listener_valid_section(section, bcontainer->bypass_ro,
+                                     "region_del")) {
         return;
     }
 

hw/vfio/listener.c static void vfio_dirty_tracking_update(MemoryListener *listener, · line 831

Quoted 5 lines
hw/vfio/listener.c +1 -1
@@ -821,7 +828,7 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,
         container_of(listener, VFIODirtyRangesListener, listener);
     hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, "tracking_update") ||
+    if (!vfio_listener_valid_section(section, false, "tracking_update") ||

I think you shall document in the commit msg and maybe here why we set bypass_ro to false in that case

Eric

Quoted 12 lines
hw/vfio/listener.c +1 -1
         !vfio_get_section_iova_range(dirty->bcontainer, section,
                                      &iova, &end, NULL)) {
         return;
@@ -1215,7 +1222,7 @@ static void vfio_listener_log_sync(MemoryListener *listener,
     int ret;
     Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section)) {
+    if (vfio_listener_skipped_section(section, false)) {
         return;
     }
 
#54 lore raw

PATCH v7 19/23 Workaround for ERRATA_772415_SPR17

In reply to Zhenzhong Duan

On 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 second stage page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/

Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.

Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.

Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."

---
 hw/vfio/iommufd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +9 -1
index f9d0926274..f9da0e79cc 100644
@@ -15,6 +15,7 @@
 #include <linux/vfio.h>
 #include <linux/iommufd.h>
 
+#include "hw/iommu.h"
 #include "hw/vfio/vfio-device.h"
 #include "qemu/error-report.h"
 #include "trace.h"
@@ -351,6 +352,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
     VFIOContainer *bcontainer = VFIO_IOMMU(container);
     uint32_t type, flags = 0;
     uint64_t hw_caps;
+    VendorCaps caps;
     VFIOIOASHwpt *hwpt;
     uint32_t hwpt_id;
     int ret;
@@ -396,7 +398,8 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      * instead.
      */
     if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid,
-                                         &type, NULL, 0, &hw_caps, errp)) {
+                                         &type, &caps, sizeof(caps), &hw_caps,
+                                         errp)) {
         return false;
     }
 
@@ -411,6 +414,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      */
     if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
         flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+
+        if (host_iommu_extract_quirks(type, &caps) &
+            HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO) {
+            bcontainer->bypass_ro = true;
+        }
     }
 
     if (cpr_is_incoming()) {
Signature · 1 line
2.47.1
#55 lore raw

Re: [PATCH v7 19/23] Workaround for ERRATA_772415_SPR17

Quoted 33 lines · Zhenzhong Duan

On 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 second stage page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/

Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.

Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.

Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."

---
  hw/vfio/iommufd.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +0 -0
index f9d0926274..f9da0e79cc 100644

hw/vfio/iommufd.c line 18

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
@@ -15,6 +15,7 @@
  #include <linux/vfio.h>
  #include <linux/iommufd.h>
 
+#include "hw/iommu.h"

Changes look ok apart from this include.

Thanks,

C.

Quoted 33 lines
hw/vfio/iommufd.c +8 -1
  #include "hw/vfio/vfio-device.h"
  #include "qemu/error-report.h"
  #include "trace.h"
@@ -351,6 +352,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      VFIOContainer *bcontainer = VFIO_IOMMU(container);
      uint32_t type, flags = 0;
      uint64_t hw_caps;
+    VendorCaps caps;
      VFIOIOASHwpt *hwpt;
      uint32_t hwpt_id;
      int ret;
@@ -396,7 +398,8 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
       * instead.
       */
      if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid,
-                                         &type, NULL, 0, &hw_caps, errp)) {
+                                         &type, &caps, sizeof(caps), &hw_caps,
+                                         errp)) {
          return false;
      }
 
@@ -411,6 +414,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
       */
      if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
          flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+
+        if (host_iommu_extract_quirks(type, &caps) &
+            HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO) {
+            bcontainer->bypass_ro = true;
+        }
      }
 
      if (cpr_is_incoming()) {
#56 lore raw

Re: [PATCH v7 19/23] Workaround for ERRATA_772415_SPR17

In reply to Zhenzhong Duan
Quoted 33 lines · Zhenzhong Duan

On 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 second stage page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/

Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.

Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.

Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."

---
  hw/vfio/iommufd.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +0 -0
index f9d0926274..f9da0e79cc 100644

hw/vfio/iommufd.c line 18

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
@@ -15,6 +15,7 @@
  #include <linux/vfio.h>
  #include <linux/iommufd.h>
 
+#include "hw/iommu.h"

Changes look ok apart from this include.

Thanks,

C.

Quoted 33 lines
hw/vfio/iommufd.c +8 -1
  #include "hw/vfio/vfio-device.h"
  #include "qemu/error-report.h"
  #include "trace.h"
@@ -351,6 +352,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      VFIOContainer *bcontainer = VFIO_IOMMU(container);
      uint32_t type, flags = 0;
      uint64_t hw_caps;
+    VendorCaps caps;
      VFIOIOASHwpt *hwpt;
      uint32_t hwpt_id;
      int ret;
@@ -396,7 +398,8 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
       * instead.
       */
      if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid,
-                                         &type, NULL, 0, &hw_caps, errp)) {
+                                         &type, &caps, sizeof(caps), &hw_caps,
+                                         errp)) {
          return false;
      }
 
@@ -411,6 +414,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
       */
      if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
          flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+
+        if (host_iommu_extract_quirks(type, &caps) &
+            HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO) {
+            bcontainer->bypass_ro = true;
+        }
      }
 
      if (cpr_is_incoming()) {
#57 lore raw

Re: [PATCH v7 19/23] Workaround for ERRATA_772415_SPR17

In reply to Zhenzhong Duan
Quoted 59 lines · Zhenzhong Duan

On 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 second stage page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/

Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.

Implication: Due to this erratum, pages mapped as Read-only in second-stage page-tables may be modified by remapping hardware Access/Dirty bit updates.

Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."

---
 hw/vfio/iommufd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +4 -1
index f9d0926274..f9da0e79cc 100644
@@ -15,6 +15,7 @@
 #include <linux/vfio.h>
 #include <linux/iommufd.h>
 
+#include "hw/iommu.h"
 #include "hw/vfio/vfio-device.h"
 #include "qemu/error-report.h"
 #include "trace.h"
@@ -351,6 +352,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
     VFIOContainer *bcontainer = VFIO_IOMMU(container);
     uint32_t type, flags = 0;
     uint64_t hw_caps;
+    VendorCaps caps;
     VFIOIOASHwpt *hwpt;
     uint32_t hwpt_id;
     int ret;
@@ -396,7 +398,8 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      * instead.
      */
     if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid,
-                                         &type, NULL, 0, &hw_caps, errp)) {
+                                         &type, &caps, sizeof(caps), &hw_caps,
+                                         errp)) {
         return false;
     }
 

hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, · line 418

Quoted 5 lines
hw/vfio/iommufd.c +2 -0
@@ -411,6 +414,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      */
     if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
         flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+
+        if (host_iommu_extract_quirks(type, &caps) &

Given that call site, I would rather implement a new PCIIOMMUOps, no?

Eric

Quoted 6 lines
hw/vfio/iommufd.c +3 -0
+            HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO) {
+            bcontainer->bypass_ro = true;
+        }
     }
 
     if (cpr_is_incoming()) {
#58 lore raw

RE: [PATCH v7 19/23] Workaround for ERRATA_772415_SPR17

Quoted 78 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 19/23] Workaround for ERRATA_772415_SPR17

Quoted 1 line · Zhenzhong Duan

On a system influenced by ERRATA_772415,

IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17

Quoted 1 line

is repored by IOMMU_DEVICE_GET_HW_INFO. Due to this errata, even the

readonly

Quoted 5 lines

range mapped on second stage page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.

https://edc.intel.com/content/www/us/en/design/products-and-solutions/pr
ocessors-and-chipsets/eagle-stream/sapphire-rapids-specification-update/

Quoted 10 lines

Also copied the SPR17 details from above link:
"Problem: When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.

Implication: Due to this erratum, pages mapped as Read-only in

second-stage

Quoted 1 line

page-tables may be modified by remapping hardware Access/Dirty bit

updates.

hw/vfio/iommufd.c static bool

Quoted 23 lines

Workaround: None identified. System software enabling nested translations for a VM should ensure that there are no read-only pages in the corresponding second-stage mappings."

---
 hw/vfio/iommufd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +1 -0
index f9d0926274..f9da0e79cc 100644
@@ -15,6 +15,7 @@
 #include <linux/vfio.h>
 #include <linux/iommufd.h>
 
+#include "hw/iommu.h"
 #include "hw/vfio/vfio-device.h"
 #include "qemu/error-report.h"
 #include "trace.h"
@@ -351,6 +352,7 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

hw/vfio/iommufd.c static bool

Quoted 8 lines
hw/vfio/iommufd.c +1 -0
     VFIOContainer *bcontainer = VFIO_IOMMU(container);
     uint32_t type, flags = 0;
     uint64_t hw_caps;
+    VendorCaps caps;
     VFIOIOASHwpt *hwpt;
     uint32_t hwpt_id;
     int ret;
@@ -396,7 +398,8 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
      * instead.
      */
     if (!iommufd_backend_get_device_info(vbasedev->iommufd,

vbasedev->devid,

hw/vfio/iommufd.c static bool · line 399

Quoted 1 line
hw/vfio/iommufd.c +0 -1
-                                         &type, NULL, 0,

&hw_caps, errp)) {

hw/vfio/iommufd.c static bool · line 401

Quoted 1 line
hw/vfio/iommufd.c +1 -0
+                                         &type, &caps,

sizeof(caps), &hw_caps,

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
+                                         errp)) {
         return false;
     }
 
Quoted 8 lines

hw/vfio/iommufd.c static bool

Quoted 1 line
hw/vfio/iommufd.c +0 -0
@@ -411,6 +414,11 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, · line 418

Quoted 5 lines
hw/vfio/iommufd.c +2 -0
      */
     if (vfio_device_get_viommu_flags_want_nesting(vbasedev)) {
         flags |= IOMMU_HWPT_ALLOC_NEST_PARENT;
+
+        if (host_iommu_extract_quirks(type, &caps) &

Given that call site, I would rather implement a new PCIIOMMUOps, no?

OK, will move CONFIG_IOMMUFD guarded code from intel_iommu.c to intel_iommu_accel.c and add new PCIIOMMUOps to call into intel_iommu_accel.c

Thanks
Zhenzhong

Thanks
Zhenzhong

#59 lore raw

PATCH v7 20/23 vfio: Bypass readonly region for dirty tracking

In reply to Zhenzhong Duan

When doing ditry tracking or calculating dirty tracking range, readonly regions can be bypassed, because corresponding DMA mappings are readonly and never become dirty.

This can optimize dirty tracking a bit for passthrough device.

---
 hw/vfio/listener.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/vfio/listener.c +9 -2
index 0862b2b834..cbd86c79af 100644
@@ -828,7 +828,8 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,
         container_of(listener, VFIODirtyRangesListener, listener);
     hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
+    /* Bypass readonly section as it never become dirty */
+    if (!vfio_listener_valid_section(section, true, "tracking_update") ||
         !vfio_get_section_iova_range(dirty->bcontainer, section,
                                      &iova, &end, NULL)) {
         return;
@@ -1087,6 +1088,12 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if (!mr) {
         goto out_unlock;
     }
+
+    if (!(iotlb->perm & IOMMU_WO) || mr->readonly) {
+        rcu_read_unlock();
+        return;
+    }
+
     translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
     ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
@@ -1222,7 +1229,7 @@ static void vfio_listener_log_sync(MemoryListener *listener,
     int ret;
     Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section, false)) {
+    if (vfio_listener_skipped_section(section, true)) {
         return;
     }
 
Signature · 1 line
2.47.1
Reviewed-by on v7 20/23
#60 lore raw

Re: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

Quoted 26 lines · Zhenzhong Duan

When doing ditry tracking or calculating dirty tracking range, readonly regions can be bypassed, because corresponding DMA mappings are readonly and never become dirty.

This can optimize dirty tracking a bit for passthrough device.

---
  hw/vfio/listener.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
hw/vfio/listener.c +2 -1
index 0862b2b834..cbd86c79af 100644
@@ -828,7 +828,8 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,
          container_of(listener, VFIODirtyRangesListener, listener);
      hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
+    /* Bypass readonly section as it never become dirty */
+    if (!vfio_listener_valid_section(section, true, "tracking_update") ||
          !vfio_get_section_iova_range(dirty->bcontainer, section,
                                       &iova, &end, NULL)) {
          return;

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 1092

Quoted 5 lines
hw/vfio/listener.c +2 -0
@@ -1087,6 +1088,12 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
      if (!mr) {
          goto out_unlock;
      }
+
+    if (!(iotlb->perm & IOMMU_WO) || mr->readonly) {

In case you resend, please add a trace event.

Anyhow,

Thanks,

C.

Quoted 16 lines
hw/vfio/listener.c +5 -1
+        rcu_read_unlock();
+        return;
+    }
+
      translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
      ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
@@ -1222,7 +1229,7 @@ static void vfio_listener_log_sync(MemoryListener *listener,
      int ret;
      Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section, false)) {
+    if (vfio_listener_skipped_section(section, true)) {
          return;
      }
 
#61 lore raw

RE: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

Quoted 36 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

Quoted 2 lines · Zhenzhong Duan

When doing ditry tracking or calculating dirty tracking range, readonly regions can be bypassed, because corresponding DMA mappings are

readonly

hw/vfio/listener.c static void

Quoted 14 lines

and never become dirty.

This can optimize dirty tracking a bit for passthrough device.

---
  hw/vfio/listener.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
hw/vfio/listener.c +0 -0
index 0862b2b834..cbd86c79af 100644
@@ -828,7 +828,8 @@ static void

vfio_dirty_tracking_update(MemoryListener *listener,

hw/vfio/listener.c static void

Quoted 10 lines
hw/vfio/listener.c +2 -1
          container_of(listener, VFIODirtyRangesListener, listener);
      hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
+    /* Bypass readonly section as it never become dirty */
+    if (!vfio_listener_valid_section(section, true, "tracking_update") ||
          !vfio_get_section_iova_range(dirty->bcontainer, section,
                                       &iova, &end, NULL)) {
          return;
@@ -1087,6 +1088,12 @@ static void

vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)

Quoted 8 lines

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 1092

Quoted 5 lines
hw/vfio/listener.c +2 -0
      if (!mr) {
          goto out_unlock;
      }
+
+    if (!(iotlb->perm & IOMMU_WO) || mr->readonly) {

In case you resend, please add a trace event.

OK, will add:

  trace_vfio_iommu_map_dirty_notify_skip_ro(iova, iova + iotlb->addr_mask);

Thanks
Zhenzhong

Quoted 28 lines

Anyhow,

Thanks,

C.

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)

Quoted 7 lines
hw/vfio/listener.c +4 -0
+        rcu_read_unlock();
+        return;
+    }
+
      translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
      ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

iotlb->addr_mask + 1,

hw/vfio/listener.c static void

Quoted 1 line
hw/vfio/listener.c +0 -0
@@ -1222,7 +1229,7 @@ static void

vfio_listener_log_sync(MemoryListener *listener,

Quoted 8 lines
hw/vfio/listener.c +1 -1
      int ret;
      Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section, false)) {
+    if (vfio_listener_skipped_section(section, true)) {
          return;
      }
 
#62 lore raw

Re: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

In reply to Zhenzhong Duan
Quoted 1 line · Zhenzhong Duan

When doing ditry tracking or calculating dirty tracking range, readonly

dirty tracking or when ...

Quoted 25 lines

regions can be bypassed, because corresponding DMA mappings are readonly and never become dirty.

This can optimize dirty tracking a bit for passthrough device.

---
 hw/vfio/listener.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/vfio/listener.c +2 -1
index 0862b2b834..cbd86c79af 100644
@@ -828,7 +828,8 @@ static void vfio_dirty_tracking_update(MemoryListener *listener,
         container_of(listener, VFIODirtyRangesListener, listener);
     hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
+    /* Bypass readonly section as it never become dirty */
+    if (!vfio_listener_valid_section(section, true, "tracking_update") ||
         !vfio_get_section_iova_range(dirty->bcontainer, section,
                                      &iova, &end, NULL)) {
         return;

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 1094

Quoted 7 lines
hw/vfio/listener.c +4 -0
@@ -1087,6 +1088,12 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if (!mr) {
         goto out_unlock;
     }
+
+    if (!(iotlb->perm & IOMMU_WO) || mr->readonly) {
+        rcu_read_unlock();
+        return;

This change is less obvsious compared to the others. Might be worth explaining why you add it.

hw/vfio/listener.c static void vfio_listener_log_sync(MemoryListener *listener, · line 1232

Quoted 11 lines
hw/vfio/listener.c +3 -1
+    }
+
     translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
     ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
@@ -1222,7 +1229,7 @@ static void vfio_listener_log_sync(MemoryListener *listener,
     int ret;
     Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section, false)) {
+    if (vfio_listener_skipped_section(section, true)) {

the false -> true change might be squashed into the 18/23 patch while explainig why you set this value.

Eric

Quoted 3 lines
hw/vfio/listener.c +0 -0
         return;
     }
 
#63 lore raw

RE: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

Quoted 46 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 20/23] vfio: Bypass readonly region for dirty tracking

Quoted 1 line · Zhenzhong Duan

When doing ditry tracking or calculating dirty tracking range, readonly

dirty tracking or when ...

Quoted 1 line

regions can be bypassed, because corresponding DMA mappings are

readonly

hw/vfio/listener.c static void

Quoted 14 lines

and never become dirty.

This can optimize dirty tracking a bit for passthrough device.

---
 hw/vfio/listener.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/vfio/listener.c +0 -0
index 0862b2b834..cbd86c79af 100644
@@ -828,7 +828,8 @@ static void

vfio_dirty_tracking_update(MemoryListener *listener,

hw/vfio/listener.c static void

Quoted 10 lines
hw/vfio/listener.c +2 -1
         container_of(listener, VFIODirtyRangesListener, listener);
     hwaddr iova, end;
 
-    if (!vfio_listener_valid_section(section, false, "tracking_update") ||
+    /* Bypass readonly section as it never become dirty */
+    if (!vfio_listener_valid_section(section, true, "tracking_update") ||
         !vfio_get_section_iova_range(dirty->bcontainer, section,
                                      &iova, &end, NULL)) {
         return;
@@ -1087,6 +1088,12 @@ static void

vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 1094

Quoted 7 lines
hw/vfio/listener.c +4 -0
     if (!mr) {
         goto out_unlock;
     }
+
+    if (!(iotlb->perm & IOMMU_WO) || mr->readonly) {
+        rcu_read_unlock();
+        return;
Quoted 2 lines

This change is less obvsious compared to the others. Might be worth explaining why you add it.

OK

Quoted 8 lines

hw/vfio/listener.c static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)

Quoted 5 lines
hw/vfio/listener.c +2 -0
+    }
+
     translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
     ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

iotlb->addr_mask + 1,

hw/vfio/listener.c static void

Quoted 1 line
hw/vfio/listener.c +0 -0
@@ -1222,7 +1229,7 @@ static void

vfio_listener_log_sync(MemoryListener *listener,

Quoted 7 lines

hw/vfio/listener.c static void vfio_listener_log_sync(MemoryListener *listener, · line 1232

Quoted 5 lines
hw/vfio/listener.c +1 -1
     int ret;
     Error *local_err = NULL;
 
-    if (vfio_listener_skipped_section(section, false)) {
+    if (vfio_listener_skipped_section(section, true)) {

the false -> true change might be squashed into the 18/23 patch while explainig why you set this value.

OK, will refactor this patch and patch18

Thanks
Zhenzhong

#64 lore raw

PATCH v7 21/23 intel_iommu: Add migration support with x-flts=on

In reply to Zhenzhong Duan

When x-flts=on, we set up bindings to nested HWPT in host, after migration, VFIO device binds to nesting parent HWPT by default. We need to re-establish the bindings to nested HWPT, or else device DMA will break.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
index edd1416382..8fec61be3e 100644
@@ -4360,6 +4360,13 @@ static int vtd_post_load(void *opaque, int version_id)
      */
     vtd_switch_address_space_all(iommu);
 
+    /*
+     * Bindings to nested HWPT in host is set up dynamically depending
+     * on pasid entry configuration from guest. After migration, we
+     * need to re-establish the bindings before restore device's DMA.
+     */
+    vtd_replay_pasid_bindings_all(iommu);
+
     return 0;
 }
 
Signature · 1 line
2.47.1
Reviewed-by on v7 21/23
#65 lore raw

Re: [PATCH v7 21/23] intel_iommu: Add migration support with x-flts=on

Quoted 3 lines · Zhenzhong Duan

When x-flts=on, we set up bindings to nested HWPT in host, after migration, VFIO device binds to nesting parent HWPT by default. We need to re-establish the bindings to nested HWPT, or else device

s/else/otherwise

Quoted 13 lines

DMA will break.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +0 -0
index edd1416382..8fec61be3e 100644

hw/i386/intel_iommu.c static int vtd_post_load(void *opaque, int version_id) · line 4366

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
@@ -4360,6 +4360,13 @@ static int vtd_post_load(void *opaque, int version_id)
      */
     vtd_switch_address_space_all(iommu);
 
+    /*
+     * Bindings to nested HWPT in host is set up dynamically depending
+     * on pasid entry configuration from guest. After migration, we
+     * need to re-establish the bindings before restore device's DMA.

restoring

hw/i386/intel_iommu.c static int vtd_post_load(void *opaque, int version_id) · line 4372

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
+     */
+    vtd_replay_pasid_bindings_all(iommu);
+
     return 0;
 }
 

Eric

#66 lore raw

PATCH v7 22/23 intel_iommu: Enable host device when x-flts=on in scalable mode

In reply to Zhenzhong Duan

Now that all infrastructures of supporting passthrough device running with first stage translation are there, enable it now.

---
 hw/i386/intel_iommu.c | 2 ++
 1 file changed, 2 insertions(+)
hw/i386/intel_iommu.c +2 -0
index 8fec61be3e..356623ef13 100644
@@ -4979,6 +4979,8 @@ static bool vtd_check_hiod(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                    "when x-flts=on");
         return false;
     }
+
+    return true;
 #endif
 
     error_setg(errp,
Signature · 1 line
2.47.1
#67 lore raw

PATCH v7 23/23 docs/devel: Add IOMMUFD nesting documentation

In reply to Zhenzhong Duan

Add documentation about using IOMMUFD backed VFIO device with intel_iommu with x-flts=on.

---
 docs/devel/vfio-iommufd.rst | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
docs/devel/vfio-iommufd.rst +25 -0
index 3d1c11f175..f1e4d940e6 100644
@@ -164,3 +164,28 @@ RAM discarding for mdev.
 
 ``vfio-ap`` and ``vfio-ccw`` devices don't have same issue as their backend
 devices are always mdev and RAM discarding is force enabled.
+
+Usage with intel_iommu with x-flts=on
+-------------------------------------
+
+Only IOMMUFD backed VFIO device is supported when intel_iommu is configured
+with x-flts=on, for legacy container backed VFIO device, below error shows:
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0: vfio 0000:02:00.0: Failed to set vIOMMU: Need IOMMUFD backend when x-flts=on
+
+VFIO device under PCI bridge is unsupported, use PCIE bridge if necessary,
+or else below error shows:
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device under PCI bridge is unsupported when x-flts=on
+
+If host IOMMU has ERRATA_772415_SPR17, kexec or reboot from "intel_iommu=on,sm_on"
+to "intel_iommu=on,sm_off" in guest is also unsupported. Configure scalable mode
+off as below if it's not needed by guest.
+
+.. code-block:: bash
+
+    -device intel-iommu,x-scalable-mode=off
Signature · 1 line
2.47.1
Reviewed-by on v7 23/23
#68 lore raw

Re: [PATCH v7 23/23] docs/devel: Add IOMMUFD nesting documentation

Zhenzhong,

Quoted 14 lines · Zhenzhong Duan

Add documentation about using IOMMUFD backed VFIO device with intel_iommu with x-flts=on.

---
 docs/devel/vfio-iommufd.rst | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
docs/devel/vfio-iommufd.rst +0 -0
index 3d1c11f175..f1e4d940e6 100644

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 168

Quoted 5 lines
docs/devel/vfio-iommufd.rst +2 -0
@@ -164,3 +164,28 @@ RAM discarding for mdev.
 
 ``vfio-ap`` and ``vfio-ccw`` devices don't have same issue as their backend
 devices are always mdev and RAM discarding is force enabled.
+
+Usage with intel_iommu with x-flts=on

s/with/featuring to void repetition?

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 179

Quoted 11 lines
docs/devel/vfio-iommufd.rst +11 -0
+-------------------------------------
+
+Only IOMMUFD backed VFIO device is supported when intel_iommu is configured
+with x-flts=on, for legacy container backed VFIO device, below error shows:
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0: vfio 0000:02:00.0: Failed to set vIOMMU: Need IOMMUFD backend when x-flts=on
+
+VFIO device under PCI bridge is unsupported, use PCIE bridge if necessary,
+or else below error shows:

s/or else/otherwise

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 183

Quoted 4 lines
docs/devel/vfio-iommufd.rst +4 -0
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device under PCI bridge is unsupported when x-flts=on

s/under PCI bridge/downstream to a PCI bridge
may have been better I think

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 191

Quoted 8 lines
docs/devel/vfio-iommufd.rst +8 -0
+
+If host IOMMU has ERRATA_772415_SPR17, kexec or reboot from "intel_iommu=on,sm_on"
+to "intel_iommu=on,sm_off" in guest is also unsupported. Configure scalable mode
+off as below if it's not needed by guest.
+
+.. code-block:: bash
+
+    -device intel-iommu,x-scalable-mode=off

Besides

Eric

#69 lore raw

RE: [PATCH v7 23/23] docs/devel: Add IOMMUFD nesting documentation

Hi Eric,

All done for suggestions on this patch and patch21.

Thanks
Zhenzhong

Quoted 68 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v7 23/23] docs/devel: Add IOMMUFD nesting documentation

Zhenzhong,

Quoted 1 line · Zhenzhong Duan

Add documentation about using IOMMUFD backed VFIO device with

intel_iommu with

Quoted 13 lines

x-flts=on.

---
 docs/devel/vfio-iommufd.rst | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
docs/devel/vfio-iommufd.rst +0 -0
index 3d1c11f175..f1e4d940e6 100644

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 168

Quoted 5 lines
docs/devel/vfio-iommufd.rst +2 -0
@@ -164,3 +164,28 @@ RAM discarding for mdev.
 
 ``vfio-ap`` and ``vfio-ccw`` devices don't have same issue as their backend
 devices are always mdev and RAM discarding is force enabled.
+
+Usage with intel_iommu with x-flts=on

s/with/featuring to void repetition?

docs/devel/vfio-iommufd.rst RAM discarding for mdev.

Quoted 3 lines
docs/devel/vfio-iommufd.rst +3 -0
+-------------------------------------
+
+Only IOMMUFD backed VFIO device is supported when intel_iommu is

configured

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 172

Quoted 1 line
docs/devel/vfio-iommufd.rst +1 -0
+with x-flts=on, for legacy container backed VFIO device, below error

shows:

docs/devel/vfio-iommufd.rst RAM discarding for mdev.

Quoted 4 lines
docs/devel/vfio-iommufd.rst +4 -0
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device vfio-pci,host=0000:02:00.0: vfio

0000:02:00.0: Failed to set vIOMMU: Need IOMMUFD backend when x-flts=on

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 179

Quoted 3 lines
docs/devel/vfio-iommufd.rst +3 -0
+
+VFIO device under PCI bridge is unsupported, use PCIE bridge if necessary,
+or else below error shows:

s/or else/otherwise

docs/devel/vfio-iommufd.rst RAM discarding for mdev.

Quoted 4 lines
docs/devel/vfio-iommufd.rst +4 -0
+
+.. code-block:: none
+
+    qemu-system-x86_64: -device

vfio-pci,host=0000:02:00.0,bus=bridge1,iommufd=iommufd0: vfio 0000:02:00.0: Failed to set vIOMMU: Host device under PCI bridge is unsupported when x-flts=on
s/under PCI bridge/downstream to a PCI bridge
may have been better I think

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 185

Quoted 2 lines
docs/devel/vfio-iommufd.rst +2 -0
+
+If host IOMMU has ERRATA_772415_SPR17, kexec or reboot from

"intel_iommu=on,sm_on"

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 186

Quoted 1 line
docs/devel/vfio-iommufd.rst +1 -0
+to "intel_iommu=on,sm_off" in guest is also unsupported. Configure

scalable mode

docs/devel/vfio-iommufd.rst RAM discarding for mdev. · line 191

Quoted 5 lines
docs/devel/vfio-iommufd.rst +5 -0
+off as below if it's not needed by guest.
+
+.. code-block:: bash
+
+    -device intel-iommu,x-scalable-mode=off

Besides

Eric