Skip to contentSkip to conversation

intel_iommu: Enable PASID support for passthrough device

PATCH Superseded Zhenzhong Duan 4 participants 51 messages May 9 – May 22

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover intel_iommu: Enable PASID support for passthrough device

Hi,

Now we already support first stage translation with passthrough device backed by nested translation in host, but only for IOMMU_NO_PASID.

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt, to record the current binded nested hwpt and even future PRQ support. It's also designed to support IOMMU_NO_PASID.

The potential full definition of VTDAccelPASIDCacheEntry may like:

  typedef struct VTDAccelPASIDCacheEntry {
      VTDHostIOMMUDevice *vtd_hiod;
      VTDPASIDEntry pasid_entry;
      uint32_t pasid;
      uint32_t fs_hwpt_id;
      uint32_t fault_id;
      int fault_fd;
      QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
      IOMMUPRINotifier pri_notifier_entry;
      IOMMUPRINotifier *pri_notifier;
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;

Based on newest master branch.
GIT branch: https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5

PATCH01-08: Some preparing work
PATCH09-13: Handle PASID entry addition/removal and bind/unbind
PATCH14-15: Add PASID related check and enable PASID for passthrough device

Tests:
Tested with DSA device which driver uses 2 PASIDs by default.

Thanks
Zhenzhong

Changelog:
v5:
- use %u for pasid print (Shammer)
- s/attachment/detachment in API doc (Shammer)
- save iommu pasid in vtd_as to avoid the check of PCI pasid whenever possible (Clement)
- commit log minor tweak on patch3 and patch7 (Liuyi)
- use name 'pasid-bits' for pasid property (Liuyi)

v4:
- improve documents of VIOMMU_FLAGs (Cédric)
- add a cleanup patch to simplify the check between PCI pasid and VTD pasid (Clement)
- rebase to newest master branch

v3:
- fix @Pasid parameter's comment (Liuyi)
- introduce a wrapper vtd_accel_delete_pc() (Liuyi)
- drop patch12 in v2 to avoid a race condition (Clement)
- s/PASID_0/IOMMU_NO_PASID s/gloal/global s/as_it/hiod_it (Liuyi)
- make trace_vtd_device_at/detach_hwpt use IOMMU_NO_PASID (Liuyi)
- change _accel subfix style naming to vtd_accel_ prefix style (Liuyi)
- drop unnecessary parameter vtd_hiod* from vtd_destroy_old_fs_hwpt() (Liuyi)
- introduce a new flag VIOMMU_FLAG_WANT_PASID_ATTACH for pasid attach (Nicolin)

v2:
- move the check "s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH" to patch5 (Clement)
- move #include "hw/core/iommu.h" before #include "hw/core/qdev.h" (Liuyi)
- polish the comment about @Pasid parameter (Liuyi)
- s/pe/pasid_entry, s/as_it/hiod_it, s/vtd_find_add_pc/vtd_accel_fill_pc (Liuyi)
- s/VTDACCELPASIDCacheEntry/VTDAccelPASIDCacheEntry (Liuyi)
- add explanation in code about PASID removal before addition (Liuyi)
- polish the comment about scope of VTDAccelPASIDCacheEntry vs VTDPASIDCacheEntry (Liuyi)
- add an optimization to bypass PASID entry addition for PASID selective pv_inv_dsc (Liuyi)

v1:
- use naming pattern "XXX_SET_THENAME" same as smmu (Clement)
- fix s->pasid check (Clement)

RFCv2:
- extend attach/detach_hwpt() instead of introducing new callbacks (Shammer)
- Define IOMMU_NO_PASID for device attachment without pasid (Nicolin)
- update vtd_destroy_old_fs_hwpt()'s parameter for naming consistency (Clement)
- check pasid bits size to be no more than 20 bits (Clement)
- initialize local variable max_pasid_log2 to 0 (Cédric)

Zhenzhong Duan (15):
vfio/iommufd: Extend attach/detach_hwpt callback implementations with
pasid
iommufd: Extend attach/detach_hwpt callbacks to support pasid vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID
flag
intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag
intel_iommu: Rename pasid property to "pasid-bits" and define it as
type uint8
intel_iommu: Export some functions
intel_iommu: Use IOMMU_NO_PASID and delete PASID_0
intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID
internally
intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request
intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request
intel_iommu_accel: Bypass PASID entry addition for just deleted entry
intel_iommu_accel: Handle PASID entry removal for system reset
intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID
bind/unbind and PIOTLB invalidation
intel_iommu_accel: Add pasid bits size check
intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and
VIOMMU_FLAG_WANT_PASID_ATTACH

 hw/i386/intel_iommu_accel.h    |  20 +-
 hw/i386/intel_iommu_internal.h |  44 +++-
 include/hw/core/iommu.h        |  13 ++
 include/hw/i386/intel_iommu.h  |   4 +-
 include/hw/vfio/vfio-device.h  |   1 +
 include/system/iommufd.h       |  18 +-
 include/system/memory.h        |   2 +-
 backends/iommufd.c             |   9 +-
 hw/arm/smmuv3-accel.c          |  12 +-
 hw/i386/intel_iommu.c          | 254 +++++++++-----------
 hw/i386/intel_iommu_accel.c    | 414 +++++++++++++++++++++++++++------
 hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  56 +++--
 hw/vfio/trace-events           |   4 +-
 14 files changed, 607 insertions(+), 255 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v5 1/15 vfio/iommufd: Extend attach/detach_hwpt callback implementations with pasid

For attachment with pasid, pasid together with flag VFIO_DEVICE_ATTACH_PASID should be passed in.

Define IOMMU_NO_PASID to represent device attachment without pasid same as in kernel.

The implementation is similar for detachment.

---
 include/hw/core/iommu.h |  2 ++
 hw/vfio/iommufd.c       | 44 +++++++++++++++++++++++++----------------
 hw/vfio/trace-events    |  4 ++--
 3 files changed, 31 insertions(+), 19 deletions(-)
include/hw/core/iommu.h +2 -0
index cd59a367ce..77739d4214 100644
@@ -30,4 +30,6 @@ enum host_iommu_quirks {
     HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
 };
 
+#define IOMMU_NO_PASID 0
+
 #endif /* HW_IOMMU_H */
hw/vfio/iommufd.c +27 -17

118 lines not on this page · raw message

hw/vfio/trace-events +2 -2
index 2049159015..8dbb477298 100644
@@ -183,8 +183,8 @@ vfio_vmstate_change_prepare(const char *name, int running, const char *reason, c
 
 iommufd_cdev_connect_and_bind(int iommufd, const char *name, int devfd, int devid) " [iommufd=%d] Successfully bound device %s (fd=%d): output devid=%d"
 iommufd_cdev_getfd(const char *dev, int devfd) " %s (fd=%d)"
-iommufd_cdev_attach_ioas_hwpt(int iommufd, const char *name, int devfd, int id) " [iommufd=%d] Successfully attached device %s (%d) to id=%d"
-iommufd_cdev_detach_ioas_hwpt(int iommufd, const char *name) " [iommufd=%d] Successfully detached %s"
+iommufd_cdev_pasid_attach_ioas_hwpt(int iommufd, const char *name, int devfd, uint32_t pasid, int id) " [iommufd=%d] Successfully attached device %s (%d) pasid %u to id=%d"
+iommufd_cdev_pasid_detach_ioas_hwpt(int iommufd, const char *name, uint32_t pasid) " [iommufd=%d] Successfully detached %s pasid %u"
 iommufd_cdev_fail_attach_existing_container(const char *msg) " %s"
 iommufd_cdev_alloc_ioas(int iommufd, int ioas_id) " [iommufd=%d] new IOMMUFD container with ioasid=%d"
 iommufd_cdev_device_info(char *name, int devfd, int num_irqs, int num_regions, int flags) " %s (%d) num_irqs=%d num_regions=%d flags=%d"
Signature · 1 line
2.47.3
#2 lore raw

PATCH v5 2/15 iommufd: Extend attach/detach_hwpt callbacks to support pasid

In reply to Zhenzhong Duan

Same for the two wrappers and their call sites.

---
 include/system/iommufd.h    | 18 ++++++++++++------
 backends/iommufd.c          |  9 +++++----
 hw/arm/smmuv3-accel.c       | 12 ++++++++----
 hw/i386/intel_iommu_accel.c | 19 ++++++++++---------
 hw/vfio/iommufd.c           | 10 +++++-----
 5 files changed, 40 insertions(+), 28 deletions(-)
include/system/iommufd.h +12 -6
index 2925d116ac..4257e50f62 100644
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @hiodi: host IOMMU device backed by IOMMUFD backend.
      *
+     * @pasid: target pasid of the device to be attached.
+     *
      * @hwpt_id: ID of IOMMUFD hardware page table.
      *
      * @errp: pass an Error out when attachment fails.
      *
      * Returns: true on success, false on failure.
      */
-    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, uint32_t pasid,
+                        uint32_t hwpt_id, Error **errp);
     /**
      * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table.
      * VFIO and VDPA device can have different implementation.
@@ -154,15 +156,19 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @hiodi: host IOMMU device backed by IOMMUFD backend.
      *
-     * @errp: pass an Error out when attachment fails.
+     * @pasid: target pasid of the device to be detached.
+     *
+     * @errp: pass an Error out when detachment fails.
      *
      * Returns: true on success, false on failure.
      */
-    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, Error **errp);
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *hiodi, uint32_t pasid,
+                        Error **errp);
 };
 
 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
-                                           uint32_t hwpt_id, Error **errp);
-bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
+                                           uint32_t pasid, uint32_t hwpt_id,
                                            Error **errp);
+bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
+                                           uint32_t pasid, Error **errp);
 #endif
backends/iommufd.c +5 -4
index 410b044370..cfde6f2b2c 100644
@@ -539,23 +539,24 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
 }
 
 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
 {
     HostIOMMUDeviceIOMMUFDClass *hiodic =
         HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(hiodi);
 
     g_assert(hiodic->attach_hwpt);
-    return hiodic->attach_hwpt(hiodi, hwpt_id, errp);
+    return hiodic->attach_hwpt(hiodi, pasid, hwpt_id, errp);
 }
 
 bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
 {
     HostIOMMUDeviceIOMMUFDClass *hiodic =
         HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(hiodi);
 
     g_assert(hiodic->detach_hwpt);
-    return hiodic->detach_hwpt(hiodi, errp);
+    return hiodic->detach_hwpt(hiodi, pasid, errp);
 }
 
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
hw/arm/smmuv3-accel.c +8 -4
index 862be814a0..2947e2e5dc 100644
@@ -300,7 +300,8 @@ bool smmuv3_accel_install_ste(SMMUv3State *s, SMMUDevice *sdev, int sid,
         return false;
     }
 
-    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID, hwpt_id,
+                                               errp)) {
         if (s1_hwpt) {
             iommufd_backend_free_id(hiodi->iommufd, s1_hwpt->hwpt_id);
             g_free(s1_hwpt);
@@ -575,7 +576,8 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *hiodi,
 
     /* Attach a HWPT based on SMMUv3 GBPA.ABORT value */
     hwpt_id = smmuv3_accel_gbpa_hwpt(s, accel);
-    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID, hwpt_id,
+                                               errp)) {
         goto free_veventq;
     }
     return true;
@@ -665,7 +667,8 @@ static void smmuv3_accel_unset_iommu_device(PCIBus *bus, void *opaque,
     hiodi = accel_dev->hiodi;
     accel = accel_dev->s_accel;
     /* Re-attach the default s2 hwpt id */
-    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, hiodi->hwpt_id, NULL)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID,
+                                               hiodi->hwpt_id, NULL)) {
         error_report("Unable to attach the default HW pagetable: hiodi devid "
                      "0x%x", hiodi->devid);
     }
@@ -879,7 +882,8 @@ bool smmuv3_accel_attach_gbpa_hwpt(SMMUv3State *s, Error **errp)
 
     hwpt_id = smmuv3_accel_gbpa_hwpt(s, accel);
     QLIST_FOREACH(accel_dev, &accel->device_list, next) {
-        if (!host_iommu_device_iommufd_attach_hwpt(accel_dev->hiodi, hwpt_id,
+        if (!host_iommu_device_iommufd_attach_hwpt(accel_dev->hiodi,
+                                                   IOMMU_NO_PASID, hwpt_id,
                                                    &local_err)) {
             error_append_hint(&local_err, "Failed to attach GBPA hwpt %u for "
                               "hiodi devid %u", hwpt_id,
hw/i386/intel_iommu_accel.c +10 -9
index ed3793602b..3217a2afac 100644
@@ -121,8 +121,9 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
         }
     }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(hiodi, hwpt_id, errp);
-    trace_vtd_device_attach_hwpt(hiodi->devid, vtd_as->pasid, hwpt_id, ret);
+    ret = host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID, hwpt_id,
+                                                errp);
+    trace_vtd_device_attach_hwpt(hiodi->devid, IOMMU_NO_PASID, hwpt_id, ret);
     if (ret) {
         /* Destroy old fs_hwpt if it's a replacement */
         vtd_destroy_old_fs_hwpt(hiodi, vtd_as);
@@ -141,22 +142,22 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
 {
     HostIOMMUDeviceIOMMUFD *hiodi = 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(hiodi, errp);
-        trace_vtd_device_detach_hwpt(hiodi->devid, pasid, ret);
+        ret = host_iommu_device_iommufd_detach_hwpt(hiodi, IOMMU_NO_PASID,
+                                                    errp);
+        trace_vtd_device_detach_hwpt(hiodi->devid, IOMMU_NO_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(hiodi, hiodi->hwpt_id,
-                                                    errp);
-        trace_vtd_device_reattach_def_hwpt(hiodi->devid, pasid, hiodi->hwpt_id,
-                                           ret);
+        ret = host_iommu_device_iommufd_attach_hwpt(hiodi, IOMMU_NO_PASID,
+                                                    hiodi->hwpt_id, errp);
+        trace_vtd_device_reattach_def_hwpt(hiodi->devid, IOMMU_NO_PASID,
+                                           hiodi->hwpt_id, ret);
     }
 
     if (ret) {
hw/vfio/iommufd.c +5 -5
index f86f6f0d7b..78e7b6a045 100644
@@ -934,21 +934,21 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, const void *data)
 
 static bool
 host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
 {
     VFIODevice *vbasedev = HOST_IOMMU_DEVICE(hiodi)->agent;
 
-    return !iommufd_cdev_pasid_attach_ioas_hwpt(vbasedev, IOMMU_NO_PASID,
-                                                hwpt_id, errp);
+    return !iommufd_cdev_pasid_attach_ioas_hwpt(vbasedev, pasid, hwpt_id, errp);
 }
 
 static bool
 host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
 {
     VFIODevice *vbasedev = HOST_IOMMU_DEVICE(hiodi)->agent;
 
-    return iommufd_cdev_pasid_detach_ioas_hwpt(vbasedev, IOMMU_NO_PASID, errp);
+    return iommufd_cdev_pasid_detach_ioas_hwpt(vbasedev, pasid, errp);
 }
 
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
Signature · 1 line
2.47.3
#3 lore raw

PATCH v5 3/15 vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

In reply to Zhenzhong Duan

When both device and vIOMMU have PASID enabled, then guest may setup pasid usages such as SVM.

VFIO needs to be aware of potential pasid usage and should attach the non-pasid part of pasid-capable device to hwpt flagged with IOMMU_HWPT_ALLOC_PASID.

ARM SMMU doesn't support IOMMU_HWPT_ALLOC_PASID, only VTD need it. So we can't check the existing vIOMMU flag VIOMMU_FLAG_PASID_SUPPORTED to determine if set flag IOMMU_HWPT_ALLOC_PASID. Instead, introduce a new flag VIOMMU_FLAG_WANT_PASID_ATTACH which will only be exposed by VTD.

Opportunistically add documentation for VIOMMU_FLAG_PASID_SUPPORTED and explain the difference with VIOMMU_FLAG_WANT_PASID_ATTACH.

---
 include/hw/core/iommu.h       | 11 +++++++++++
 include/hw/vfio/vfio-device.h |  1 +
 hw/vfio/device.c              | 11 +++++++++++
 hw/vfio/iommufd.c             |  8 +++++++-
 4 files changed, 30 insertions(+), 1 deletion(-)
include/hw/core/iommu.h +11 -0
index 77739d4214..20d6d79062 100644
@@ -20,9 +20,20 @@
 enum viommu_flags {
     /* vIOMMU needs nesting parent HWPT to create nested HWPT */
     VIOMMU_FLAG_WANT_NESTING_PARENT = BIT_ULL(0),
+    /*
+     * vIOMMU supports PASID capability, VFIO checks this flag and synthesize
+     * a PASID capability.
+     */
     VIOMMU_FLAG_PASID_SUPPORTED = BIT_ULL(1),
     /* vIOMMU needs dirty tracking on the nesting parent HWPT for nested use */
     VIOMMU_FLAG_WANT_NESTING_DIRTY_TRACKING = BIT_ULL(2),
+    /*
+     * vIOMMU requests other sub-system like VFIO to create a HWPT that can be
+     * used with PASID attachment. VIOMMU_FLAG_PASID_SUPPORTED can't be used
+     * for this purpose as PASID attachment is needed by VTD IOMMU but not ARM
+     * SMMU.
+     */
+    VIOMMU_FLAG_WANT_PASID_ATTACH = BIT_ULL(3),
 };
 
 /* Host IOMMU quirks. Extracted from host IOMMU capabilities */
include/hw/vfio/vfio-device.h +1 -0
index 380a55d6e5..8472420d3f 100644
@@ -282,6 +282,7 @@ void vfio_device_unprepare(VFIODevice *vbasedev);
 
 bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev);
 bool vfio_device_get_viommu_flags_want_nesting_dirty(VFIODevice *vbasedev);
+bool vfio_device_get_viommu_flags_want_pasid_attach(VFIODevice *vbasedev);
 bool vfio_device_get_host_iommu_quirk_bypass_ro(VFIODevice *vbasedev,
                                                 uint32_t type, void *caps,
                                                 uint32_t size);
hw/vfio/device.c +11 -0
index 3ffd69a579..b954b44d31 100644
@@ -544,6 +544,17 @@ bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)
     return false;
 }
 
+bool vfio_device_get_viommu_flags_want_pasid_attach(VFIODevice *vbasedev)
+{
+    VFIOPCIDevice *vdev = vfio_pci_from_vfio_device(vbasedev);
+
+    if (vdev) {
+        return !!(pci_device_get_viommu_flags(PCI_DEVICE(vdev)) &
+                  VIOMMU_FLAG_WANT_PASID_ATTACH);
+    }
+    return false;
+}
+
 bool vfio_device_get_host_iommu_quirk_bypass_ro(VFIODevice *vbasedev,
                                                 uint32_t type, void *caps,
                                                 uint32_t size)
hw/vfio/iommufd.c +7 -1
index 78e7b6a045..0718f029ca 100644
@@ -364,6 +364,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
     VendorCaps caps;
     VFIOIOASHwpt *hwpt;
     uint32_t hwpt_id;
+    uint8_t max_pasid_log2 = 0;
     int ret;
 
     /* Try to find a domain */
@@ -409,7 +410,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      */
     if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid,
                                          &type, &caps, sizeof(caps), &hw_caps,
-                                         NULL, errp)) {
+                                         &max_pasid_log2, errp)) {
         return false;
     }
 
@@ -437,6 +438,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         }
     }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_want_pasid_attach(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }
+
     if (cpr_is_incoming()) {
         hwpt_id = vbasedev->cpr.hwpt_id;
         goto skip_alloc;
Signature · 1 line
2.47.3
#4 lore raw

PATCH v5 4/15 intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

In reply to Zhenzhong Duan

When pasid is enabled, any hwpt attached to non-PASID or PASID should be IOMMU_HWPT_ALLOC_PASID flagged, or else attachment fails.

Change vtd_destroy_old_fs_hwpt() to pass in 'VTDHostIOMMUDevice *' for naming consistency.

---
 hw/i386/intel_iommu_accel.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_accel.c +12 -7
index 3217a2afac..bd1236c070 100644
@@ -69,11 +69,13 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
     return NULL;
 }
 
-static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
+static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
                                VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
                                Error **errp)
 {
+    HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
     struct iommu_hwpt_vtd_s1 vtd = {};
+    uint32_t flags = vtd_hiod->iommu_state->pasid ? IOMMU_HWPT_ALLOC_PASID : 0;
 
     vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) |
                 (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) |
@@ -82,13 +84,16 @@ static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
     vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
 
     return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
-                                      hiodi->hwpt_id, 0, IOMMU_HWPT_DATA_VTD_S1,
-                                      sizeof(vtd), &vtd, fs_hwpt_id, errp);
+                                      hiodi->hwpt_id, flags,
+                                      IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
+                                      fs_hwpt_id, errp);
 }
 
-static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *hiodi,
+static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
                                     VTDAddressSpace *vtd_as)
 {
+    HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+
     if (!vtd_as->fs_hwpt_id) {
         return;
     }
@@ -116,7 +121,7 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     }
 
     if (vtd_pe_pgtt_is_fst(pe)) {
-        if (!vtd_create_fs_hwpt(hiodi, pe, &hwpt_id, errp)) {
+        if (!vtd_create_fs_hwpt(vtd_hiod, pe, &hwpt_id, errp)) {
             return false;
         }
     }
@@ -126,7 +131,7 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     trace_vtd_device_attach_hwpt(hiodi->devid, IOMMU_NO_PASID, hwpt_id, ret);
     if (ret) {
         /* Destroy old fs_hwpt if it's a replacement */
-        vtd_destroy_old_fs_hwpt(hiodi, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
         if (vtd_pe_pgtt_is_fst(pe)) {
             vtd_as->fs_hwpt_id = hwpt_id;
         }
@@ -161,7 +166,7 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     }
 
     if (ret) {
-        vtd_destroy_old_fs_hwpt(hiodi, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
     }
 
     return ret;
Signature · 1 line
2.47.3
#5 lore raw

PATCH v5 5/15 intel_iommu: Rename pasid property to "pasid-bits" and define it as type uint8

In reply to Zhenzhong Duan

'x-pasid-mode' is a bool property, we need an extra 'pss' property to represent PASID size supported. Because there is no any device in QEMU supporting pasid capability yet, no guest could use the pasid feature until now, 'x-pasid-mode' takes no effect.

So instead of an extra 'pss' property we can use a single property of uint8 type and named 'pasid-bits' to represent if pasid is supported and the PASID bits size. A value of N > 0 means pasid is supported and N - 1 is the value in PSS field in ECAP register.

PASID bits size should also be no more than 20 bits according to PCI spec.

---
 hw/i386/intel_iommu_internal.h |  2 +-
 include/hw/i386/intel_iommu.h  |  2 +-
 hw/i386/intel_iommu.c          | 11 +++++++++--
 3 files changed, 11 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +1 -1
index 11a53aa369..db4f186a3e 100644
@@ -195,7 +195,7 @@
 #define VTD_ECAP_MHMV               (15ULL << 20)
 #define VTD_ECAP_SRS                (1ULL << 31)
 #define VTD_ECAP_NWFS               (1ULL << 33)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
+#define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_PDS                (1ULL << 42)
 #define VTD_ECAP_SMTS               (1ULL << 43)
include/hw/i386/intel_iommu.h +1 -1
index e44ce31841..95c76015e4 100644
@@ -314,7 +314,7 @@ struct IntelIOMMUState {
     bool intr_eime;                 /* Extended interrupt mode enabled */
     OnOffAuto intr_eim;             /* Toggle for EIM cabability */
     uint8_t aw_bits;                /* Host/IOVA address width (in bits) */
-    bool pasid;                     /* Whether to support PASID */
+    uint8_t pasid;                  /* PASID supported in bits, 0 if not */
     bool fs1gp;                     /* First Stage 1-GByte Page Support */
 
     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */
hw/i386/intel_iommu.c +9 -2
index b784c5f10a..cf275b496e 100644
@@ -4203,7 +4203,7 @@ static const Property vtd_properties[] = {
     DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
     DEFINE_PROP_BOOL("x-flts", IntelIOMMUState, fsts, FALSE),
     DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid-bits", IntelIOMMUState, pasid, 0),
     DEFINE_PROP_BOOL("svm", IntelIOMMUState, svm, false),
     DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
     DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -5045,7 +5045,8 @@ static void vtd_cap_init(IntelIOMMUState *s)
     }
 
     if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        VTD_ECAP_SET_PSS(s, s->pasid - 1);
+        s->ecap |= VTD_ECAP_PASID;
     }
 }
 
@@ -5586,6 +5587,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
         return false;
     }
 
+    if (s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH) {
+        error_setg(errp, "PASID width %d exceeds Max PASID Width %d allowed "
+                   "in PCI spec", s->pasid, PCI_EXT_CAP_PASID_MAX_WIDTH);
+        return false;
+    }
+
     if (s->svm) {
         if (!x86_iommu->dt_supported) {
             error_setg(errp, "Need to set device IOTLB for svm");
Signature · 1 line
2.47.3
Reviewed-by on v5 5/15
#6 lore raw

Re: [PATCH v5 05/15] intel_iommu: Rename pasid property to "pasid-bits" and define it as type uint8

Quoted 10 lines · Zhenzhong Duan

'x-pasid-mode' is a bool property, we need an extra 'pss' property to represent PASID size supported. Because there is no any device in QEMU supporting pasid capability yet, no guest could use the pasid feature until now, 'x-pasid-mode' takes no effect.

So instead of an extra 'pss' property we can use a single property of uint8 type and named 'pasid-bits' to represent if pasid is supported and the PASID bits size. A value of N > 0 means pasid is supported and N - 1 is the value in PSS field in ECAP register.

Quoted 6 lines

PASID bits size should also be no more than 20 bits according to PCI spec.

---

LGTM.

Quoted 67 lines
  hw/i386/intel_iommu_internal.h |  2 +-
  include/hw/i386/intel_iommu.h  |  2 +-
  hw/i386/intel_iommu.c          | 11 +++++++++--
  3 files changed, 11 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +1 -1
index 11a53aa369..db4f186a3e 100644
@@ -195,7 +195,7 @@
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
  #define VTD_ECAP_NWFS               (1ULL << 33)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
+#define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_PDS                (1ULL << 42)
  #define VTD_ECAP_SMTS               (1ULL << 43)
include/hw/i386/intel_iommu.h +1 -1
index e44ce31841..95c76015e4 100644
@@ -314,7 +314,7 @@ struct IntelIOMMUState {
      bool intr_eime;                 /* Extended interrupt mode enabled */
      OnOffAuto intr_eim;             /* Toggle for EIM cabability */
      uint8_t aw_bits;                /* Host/IOVA address width (in bits) */
-    bool pasid;                     /* Whether to support PASID */
+    uint8_t pasid;                  /* PASID supported in bits, 0 if not */
      bool fs1gp;                     /* First Stage 1-GByte Page Support */
 
      /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */
hw/i386/intel_iommu.c +9 -2
index b784c5f10a..cf275b496e 100644
@@ -4203,7 +4203,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
      DEFINE_PROP_BOOL("x-flts", IntelIOMMUState, fsts, FALSE),
      DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid-bits", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("svm", IntelIOMMUState, svm, false),
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -5045,7 +5045,8 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        VTD_ECAP_SET_PSS(s, s->pasid - 1);
+        s->ecap |= VTD_ECAP_PASID;
      }
  }
 
@@ -5586,6 +5587,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
          return false;
      }
 
+    if (s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH) {
+        error_setg(errp, "PASID width %d exceeds Max PASID Width %d allowed "
+                   "in PCI spec", s->pasid, PCI_EXT_CAP_PASID_MAX_WIDTH);
+        return false;
+    }
+
      if (s->svm) {
          if (!x86_iommu->dt_supported) {
              error_setg(errp, "Need to set device IOTLB for svm");
#7 lore raw

PATCH v5 6/15 intel_iommu: Export some functions

In reply to Zhenzhong Duan

Export some functions for accel code usages. Inline functions and MACROs are moved to internal header files. Then accel code in following patches could access them.

---
 hw/i386/intel_iommu_internal.h | 31 +++++++++++++++++++++++++
 hw/i386/intel_iommu.c          | 42 ++++++++--------------------------
 2 files changed, 40 insertions(+), 33 deletions(-)
hw/i386/intel_iommu_internal.h +31 -0
index db4f186a3e..c7e107fe87 100644
@@ -620,6 +620,12 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
 
+/* context entry operations */
+#define VTD_CE_GET_PASID_DIR_TABLE(ce) \
+    ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
+#define VTD_CE_GET_PRE(ce) \
+    ((ce)->val[0] & VTD_SM_CONTEXT_ENTRY_PRE)
+
 typedef struct VTDPASIDCacheInfo {
     uint8_t type;
     uint16_t did;
@@ -746,4 +752,29 @@ static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)
 {
     return (VTD_SM_PASID_ENTRY_PGTT(pe) == VTD_SM_PASID_ENTRY_FST);
 }
+
+static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
+{
+    return pdire->val & 1;
+}
+
+static inline bool vtd_pe_present(VTDPASIDEntry *pe)
+{
+    return pe->val[0] & VTD_PASID_ENTRY_P;
+}
+
+static inline int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
+{
+    return memcmp(p1, p2, sizeof(*p1));
+}
+
+int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
+                                  VTDPASIDDirEntry *pdire);
+int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
+                                   dma_addr_t addr, VTDPASIDEntry *pe);
+int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                             uint8_t devfn, VTDContextEntry *ce);
+int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
+                           VTDPASIDEntry *pe, uint32_t pasid);
+VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);
 #endif
hw/i386/intel_iommu.c +9 -33

88 lines not on this page · raw message

Signature · 1 line
2.47.3
#8 lore raw

PATCH v5 7/15 intel_iommu: Use IOMMU_NO_PASID and delete PASID_0

In reply to Zhenzhong Duan

In previous patch we introduced a global macro IOMMU_NO_PASID(0) for the RID attachment, this makes the local macro PASID_0 redundant. Delete it and use IOMMU_NO_PASID instead.

No functional changes intended.

---
 hw/i386/intel_iommu_internal.h |  1 -
 hw/i386/intel_iommu.c          | 22 +++++++++++-----------
 hw/i386/intel_iommu_accel.c    |  2 +-
 3 files changed, 12 insertions(+), 13 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index c7e107fe87..0141316f83 100644
@@ -615,7 +615,6 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
-#define PASID_0                             0
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
hw/i386/intel_iommu.c +11 -11
index 36af13cee3..5e5dcdc274 100644
@@ -941,7 +941,7 @@ int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
     dma_addr_t pasid_dir_base;
 
     if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
     return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
@@ -958,7 +958,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
@@ -1501,9 +1501,9 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry *ce)
 
     /*
      * Make sure in Scalable Mode, a present context entry
-     * has valid pasid entry setting at PASID_0.
+     * has valid pasid entry setting at IOMMU_NO_PASID.
      */
-    return vtd_ce_get_pasid_entry(s, ce, &pe, PASID_0);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, IOMMU_NO_PASID);
 }
 
 /* Map a device to its corresponding domain (context-entry) */
@@ -1564,7 +1564,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
         }
     } else {
         /*
-         * Check if the programming of pasid setting of PASID_0
+         * Check if the programming of pasid setting of IOMMU_NO_PASID
          * is valid, and thus avoids to check pasid entry fetching
          * result in future helper function calling.
          */
@@ -2122,7 +2122,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     vtd_iommu_lock(s);
 
     if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }
 
     /* Try to fetch pte from IOTLB */
@@ -2487,7 +2487,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
 }
 
 /*
- * There is no pasid field in iotlb invalidation descriptor, so PCI_NO_PASID
+ * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID
  * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
  * descriptor is passed which should not be PCI_NO_PASID.
  */
@@ -2508,10 +2508,10 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
              * 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
-             * PASID_0 of this device.
+             * IOMMU_NO_PASID of this device.
              */
             if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == PASID_0))) {
+                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) {
                 continue;
             }
 
@@ -2563,7 +2563,7 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
     vtd_iommu_unlock(s);
-    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);
+    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, IOMMU_NO_PASID);
 }
 
 /* Flush IOTLB
@@ -3022,7 +3022,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)) {
-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != PASID_0) &&
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) &&
                 vtd_as->pasid != pasid) {
                 continue;
             }
hw/i386/intel_iommu_accel.c +1 -1
index bd1236c070..8940d240a1 100644
@@ -217,7 +217,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
 
     did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
 
-    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == IOMMU_NO_PASID) {
         HostIOMMUDeviceIOMMUFD *hiodi =
             HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
         uint32_t entry_num = 1; /* Only implement one request for simplicity */
Signature · 1 line
2.47.3
Reviewed-by on v5 7/15
#9 lore raw

Re: [PATCH v5 07/15] intel_iommu: Use IOMMU_NO_PASID and delete PASID_0

Quoted 6 lines · Zhenzhong Duan

In previous patch we introduced a global macro IOMMU_NO_PASID(0) for the RID attachment, this makes the local macro PASID_0 redundant. Delete it and use IOMMU_NO_PASID instead.

No functional changes intended.

Quoted 8 lines
---
  hw/i386/intel_iommu_internal.h |  1 -
  hw/i386/intel_iommu.c          | 22 +++++++++++-----------
  hw/i386/intel_iommu_accel.c    |  2 +-
  3 files changed, 12 insertions(+), 13 deletions(-)
Quoted 117 lines
hw/i386/intel_iommu_internal.h +0 -1
index c7e107fe87..0141316f83 100644
@@ -615,7 +615,6 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_LEGACY_SIZE     16
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
-#define PASID_0                             0
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
  #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
hw/i386/intel_iommu.c +11 -11
index 36af13cee3..5e5dcdc274 100644
@@ -941,7 +941,7 @@ int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
      dma_addr_t pasid_dir_base;
 
      if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
      }
      pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
      return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
@@ -958,7 +958,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
      VTDPASIDEntry pe;
 
      if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
      }
      pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
@@ -1501,9 +1501,9 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry *ce)
 
      /*
       * Make sure in Scalable Mode, a present context entry
-     * has valid pasid entry setting at PASID_0.
+     * has valid pasid entry setting at IOMMU_NO_PASID.
       */
-    return vtd_ce_get_pasid_entry(s, ce, &pe, PASID_0);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, IOMMU_NO_PASID);
  }
 
  /* Map a device to its corresponding domain (context-entry) */
@@ -1564,7 +1564,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
          }
      } else {
          /*
-         * Check if the programming of pasid setting of PASID_0
+         * Check if the programming of pasid setting of IOMMU_NO_PASID
           * is valid, and thus avoids to check pasid entry fetching
           * result in future helper function calling.
           */
@@ -2122,7 +2122,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      vtd_iommu_lock(s);
 
      if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
      }
 
      /* Try to fetch pte from IOTLB */
@@ -2487,7 +2487,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
  }
 
  /*
- * There is no pasid field in iotlb invalidation descriptor, so PCI_NO_PASID
+ * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID
   * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
   * descriptor is passed which should not be PCI_NO_PASID.
   */
@@ -2508,10 +2508,10 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
               * 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
-             * PASID_0 of this device.
+             * IOMMU_NO_PASID of this device.
               */
              if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == PASID_0))) {
+                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) {
                  continue;
              }
 
@@ -2563,7 +2563,7 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
      vtd_iommu_lock(s);
      g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
      vtd_iommu_unlock(s);
-    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);
+    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, IOMMU_NO_PASID);
  }
 
  /* Flush IOTLB
@@ -3022,7 +3022,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)) {
-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != PASID_0) &&
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) &&
                  vtd_as->pasid != pasid) {
                  continue;
              }
hw/i386/intel_iommu_accel.c +1 -1
index bd1236c070..8940d240a1 100644
@@ -217,7 +217,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
 
      did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
 
-    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == IOMMU_NO_PASID) {
          HostIOMMUDeviceIOMMUFD *hiodi =
              HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
          uint32_t entry_num = 1; /* Only implement one request for simplicity */
Reviewed-by on v5 7/15
#10 lore raw

Re: [PATCH v5 07/15] intel_iommu: Use IOMMU_NO_PASID and delete PASID_0

In reply to Zhenzhong Duan
Quoted 132 lines · Zhenzhong Duan

In previous patch we introduced a global macro IOMMU_NO_PASID(0) for the RID attachment, this makes the local macro PASID_0 redundant. Delete it and use IOMMU_NO_PASID instead.

No functional changes intended.

---
 hw/i386/intel_iommu_internal.h |  1 - hw/i386/intel_iommu.c          | 22 +++++++++++----------- hw/i386/intel_iommu_accel.c    |  2 +-

 3 files changed, 12 insertions(+), 13 deletions(-)
hw/i386/intel_iommu_internal.h +0 -0
index c7e107fe87..0141316f83 100644  
@@ -615,7 +615,6 @@ typedef struct VTDRootEntry VTDRootEntry;  
  #define VTD_CTX_ENTRY_LEGACY_SIZE     16  
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32  


-#define PASID_0                             0
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw)) #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL

hw/i386/intel_iommu.c +0 -0
index 36af13cee3..5e5dcdc274 100644  
@@ -941,7 +941,7 @@ int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,  
      dma_addr_t pasid_dir_base;  


     if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);

hw/i386/intel_iommu.c +0 -0
@@ -958,7 +958,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,  
      VTDPASIDEntry pe;  


     if (pasid == PCI_NO_PASID) {
-        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }
     pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);

hw/i386/intel_iommu.c +0 -0
@@ -1501,9 +1501,9 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry *ce)  


     /*
      * Make sure in Scalable Mode, a present context entry -     * has valid pasid entry setting at PASID_0.
+     * has valid pasid entry setting at IOMMU_NO_PASID. */
-    return vtd_ce_get_pasid_entry(s, ce, &pe, PASID_0); +    return vtd_ce_get_pasid_entry(s, ce, &pe, IOMMU_NO_PASID); }

 /* Map a device to its corresponding domain (context-entry) */

hw/i386/intel_iommu.c +1 -1
@@ -1564,7 +1564,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,  
          }  
      } else {  
          /*  
-         * Check if the programming of pasid setting of PASID_0  
+         * Check if the programming of pasid setting of IOMMU_NO_PASID  
           * is valid, and thus avoids to check pasid entry fetching  
           * result in future helper function calling.  
           */  
@@ -2122,7 +2122,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  
      vtd_iommu_lock(s);  


     if (pasid == PCI_NO_PASID && s->root_scalable) { -        pasid = PASID_0;
+        pasid = IOMMU_NO_PASID;
     }

     /* Try to fetch pte from IOTLB */

hw/i386/intel_iommu.c +0 -0
@@ -2487,7 +2487,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)  
  }  


 /*
- * There is no pasid field in iotlb invalidation descriptor, so PCI_NO_PASID
+ * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID
  * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
  * descriptor is passed which should not be PCI_NO_PASID.
  */

hw/i386/intel_iommu.c +1 -1
@@ -2508,10 +2508,10 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,  
               * 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  
-             * PASID_0 of this device.  
+             * IOMMU_NO_PASID of this device.  
               */  

             if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == PASID_0))) { +                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) { continue;
             }

hw/i386/intel_iommu.c +1 -1
@@ -2563,7 +2563,7 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,  
      vtd_iommu_lock(s);  
      g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);  
      vtd_iommu_unlock(s);  
-    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);  
+    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, IOMMU_NO_PASID);  
  }  


 /* Flush IOTLB

hw/i386/intel_iommu.c +0 -0
@@ -3022,7 +3022,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)) {

-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != PASID_0) &&  
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) &&  

                 vtd_as->pasid != pasid) {
                 continue;
             }

hw/i386/intel_iommu_accel.c +0 -0
index bd1236c070..8940d240a1 100644  
@@ -217,7 +217,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,  


     did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);

-    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) { +    if (piotlb_info->domain_id == did && piotlb_info->pasid == IOMMU_NO_PASID) { HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
         uint32_t entry_num = 1; /* Only implement one request for simplicity */

#11 lore raw

PATCH v5 8/15 intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

In reply to Zhenzhong Duan

The PCI subsystem uses PCI_NO_PASID for requests-without-PASID, but VT-d uses IOMMU_NO_PASID internally. This leads to conversion and checking code between PCI_NO_PASID and IOMMU_NO_PASID throughout the implementation.

Refactor to use IOMMU PASID consistently within Intel IOMMU by storing IOMMU PASID value in vtd_as->pasid. After this change, PCI_NO_PASID is only used at three boundary points:

1. PCI_NO_PASID -> IOMMU_NO_PASID: Convert PCI PASID to IOMMU PASID in vtd_find_add_as() and cache in vtd_as->pasid.
2. IOMMU_NO_PASID -> PCI_NO_PASID: Convert when notifying UNMAP events via memory_region_notify_iommu() and returning IOMMUTLBEntry in vtd_iommu_translate().

This eliminates conversion/checks in PASID table lookups, simplifies invalidation logic with consistent PASID values, and improves code readability. The PCI subsystem interface remains unchanged to maintain compatibility with other IOMMU implementations that may not use PASID 0 for requests-without-PASID.

---
 include/system/memory.h     |   2 +-
 hw/i386/intel_iommu.c       | 164 +++++++++++++++++-------------------
 hw/i386/intel_iommu_accel.c |   2 +-
 3 files changed, 80 insertions(+), 88 deletions(-)
include/system/memory.h +1 -1
index 1417132f6d..1edb38b07d 100644
@@ -150,7 +150,7 @@ struct IOMMUTLBEntry {
     hwaddr           translated_addr;
     hwaddr           addr_mask;  /* 0xfff = 4k translation */
     IOMMUAccessFlags perm;
-    uint32_t         pasid;
+    uint32_t         pasid; /* PCI pasid */
 };
 
 /*
hw/i386/intel_iommu.c +78 -86

345 lines not on this page · raw message

hw/i386/intel_iommu_accel.c +1 -1
index 8940d240a1..10bdbba632 100644
@@ -207,7 +207,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
         return;
     }
 
-    assert(vtd_as->pasid == PCI_NO_PASID);
+    assert(vtd_as->pasid == IOMMU_NO_PASID);
 
     /* Nothing to do if there is no first stage HWPT attached */
     if (!pc_entry->valid ||
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 2 lines · Zhenzhong Duan

The PCI subsystem uses PCI_NO_PASID for requests-without-PASID, but VT-d uses IOMMU_NO_PASID internally. This leads to conversion and checking code

s/VT-d uses IOMMU_NO_PASID internally/VT-d emulation uses IOMMU_NO_PASID internally (ecap.RPS==0)/

Quoted 5 lines

between PCI_NO_PASID and IOMMU_NO_PASID throughout the implementation.

Refactor to use IOMMU PASID consistently within Intel IOMMU by storing IOMMU PASID value in vtd_as->pasid. After this change, PCI_NO_PASID is only used at three boundary points:

a typo or the third boundary is missed?

Quoted 108 lines

1. PCI_NO_PASID -> IOMMU_NO_PASID: Convert PCI PASID to IOMMU PASID in

    vtd_find_add_as() and cache in vtd_as->pasid.

2. IOMMU_NO_PASID -> PCI_NO_PASID: Convert when notifying UNMAP events via memory_region_notify_iommu() and returning IOMMUTLBEntry in vtd_iommu_translate().

This eliminates conversion/checks in PASID table lookups, simplifies invalidation logic with consistent PASID values, and improves code readability. The PCI subsystem interface remains unchanged to maintain compatibility with other IOMMU implementations that may not use PASID 0 for requests-without-PASID.

---
  include/system/memory.h     |   2 +-
  hw/i386/intel_iommu.c       | 164 +++++++++++++++++-------------------
  hw/i386/intel_iommu_accel.c |   2 +-
  3 files changed, 80 insertions(+), 88 deletions(-)
include/system/memory.h +1 -1
index 1417132f6d..1edb38b07d 100644
@@ -150,7 +150,7 @@ struct IOMMUTLBEntry {
      hwaddr           translated_addr;
      hwaddr           addr_mask;  /* 0xfff = 4k translation */
      IOMMUAccessFlags perm;
-    uint32_t         pasid;
+    uint32_t         pasid; /* PCI pasid */
  };
 
  /*
hw/i386/intel_iommu.c +5 -18
index 5e5dcdc274..b50c556c40 100644
@@ -938,12 +938,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
  int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
                             VTDPASIDEntry *pe, uint32_t pasid)
  {
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
      return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
  }
 
@@ -953,15 +949,10 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
                                  uint32_t pasid)
  {
      int ret;
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
      VTDPASIDDirEntry pdire;
      VTDPASIDEntry pe;
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
-
      /*
       * No present bit check since fpd is meaningful even
       * if the present bit is clear.
@@ -1750,7 +1741,7 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)
           *
           * Need to disable ir for as with PASID.
           */
-        if (as->pasid != PCI_NO_PASID) {
+        if (as->pasid != IOMMU_NO_PASID) {
              memory_region_set_enabled(&as->iommu_ir, false);
          } else {
              memory_region_set_enabled(&as->iommu_ir, true);
@@ -1780,7 +1771,7 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)
       * We enable per as memory region (iommu_ir_fault) for catching
       * the translation for interrupt range through PASID + PT.
       */
-    if (pt && as->pasid != PCI_NO_PASID) {
+    if (pt && as->pasid != IOMMU_NO_PASID) {
          memory_region_set_enabled(&as->iommu_ir_fault, true);
      } else {
          memory_region_set_enabled(&as->iommu_ir_fault, false);
@@ -1892,7 +1883,7 @@ static VTDAddressSpace *vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,
 
  VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
  {
-    return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
+    return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
  }
 
  static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
@@ -2121,10 +2112,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
 
      vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
      /* Try to fetch pte from IOTLB */
      iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
      if (iotlb_entry) {

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

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
@@ -2235,7 +2222,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      if (ret_fr) {
          if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
              vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

Other part LGTM.

Regards,
Yi Liu

Quoted 310 lines
hw/i386/intel_iommu.c +72 -67
          }
          goto error;
      }
@@ -2489,7 +2476,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
  /*
   * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID
   * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
- * descriptor is passed which should not be PCI_NO_PASID.
+ * descriptor is passed.
   */
  static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
                                               uint16_t domain_id, hwaddr addr,
@@ -2503,48 +2490,41 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
      QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
          ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                         vtd_as->devfn, &ce);
-        if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+        if (ret || vtd_as->pasid != pasid ||
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {
+            continue;
+        }
+
+        if (vtd_as_has_map_notifier(vtd_as)) {
              /*
-             * 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
-             * IOMMU_NO_PASID of this device.
+             * When first stage translation is off, as long as we have MAP
+             * notifications registered in any of our IOMMU notifiers,
+             * we need to sync the shadow page table. Otherwise VFIO
+             * device attaches to nested page table instead of shadow
+             * page table, so no need to sync.
               */
-            if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) {
-                continue;
-            }
-
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * When first stage translation is off, as long as we have MAP
-                 * notifications registered in any of our IOMMU notifiers,
-                 * we need to sync the shadow page table. Otherwise VFIO
-                 * device attaches to nested page table instead of shadow
-                 * page table, so no need to sync.
-                 */
-                if (!s->fsts || !s->root_scalable) {
-                    vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-                }
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                const IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                        .pasid = vtd_as->pasid,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+            if (!s->fsts || !s->root_scalable) {
+                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
              }
+        } else {
+            /*
+             * For UNMAP-only notifiers, we don't need to walk the
+             * page tables.  We just deliver the PSI down to
+             * invalidate caches.
+             */
+            const IOMMUTLBEvent event = {
+                .type = IOMMU_NOTIFIER_UNMAP,
+                .entry = {
+                    .target_as = &address_space_memory,
+                    .iova = addr,
+                    .translated_addr = 0,
+                    .addr_mask = size - 1,
+                    .perm = IOMMU_NONE,
+                     /* Other sub-systems use PCI pasid */
+                    .pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid,
+                },
+            };
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
          }
      }
  }
@@ -3007,6 +2987,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
      VTDIOTLBPageInvInfo info;
      VTDAddressSpace *vtd_as;
      VTDContextEntry ce;
+    int ret;
 
      info.domain_id = domain_id;
      info.pasid = pasid;
@@ -3019,17 +3000,15 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
      vtd_iommu_unlock(s);
 
      QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
-            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) &&
-                vtd_as->pasid != pasid) {
-                continue;
-            }
+        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                       vtd_as->devfn, &ce);
+        if (ret || vtd_as->pasid != pasid ||
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {
+            continue;
+        }
 
-            if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) {
-                vtd_address_space_sync(vtd_as);
-            }
+        if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) {
+            vtd_address_space_sync(vtd_as);
          }
      }
  }
@@ -3239,7 +3218,7 @@ static bool vtd_process_pasid_desc(IntelIOMMUState *s,
          /* PASID selective implies a DID selective */
          trace_vtd_inv_desc_pasid_cache_psi(did, pasid);
          pc_info.did = did;
-        pc_info.pasid = pasid ?: PCI_NO_PASID;
+        pc_info.pasid = pasid;
          break;
 
      case VTD_INV_DESC_PASIDC_G_GLOBAL:
@@ -3291,6 +3270,7 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
       * ...
       */
 
+    uint32_t pasid = vtd_dev_as->pasid;
      IOMMUTLBEvent event;
      uint64_t sz;
 
@@ -3307,7 +3287,8 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
      event.entry.iova = addr;
      event.entry.perm = IOMMU_NONE;
      event.entry.translated_addr = 0;
-    event.entry.pasid = vtd_dev_as->pasid;
+    /* Other sub-systems use PCI pasid */
+    event.entry.pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid;
      memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
  }
 
@@ -3335,7 +3316,7 @@ static bool vtd_process_device_piotlb_desc(IntelIOMMUState *s,
      sid = VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(inv_desc->lo);
      if (global) {
          QLIST_FOREACH(vtd_dev_as, &s->vtd_as_with_notifiers, next) {
-            if ((vtd_dev_as->pasid != PCI_NO_PASID) &&
+            if ((vtd_dev_as->pasid != IOMMU_NO_PASID) &&
                  (PCI_BUILD_BDF(pci_bus_num(vtd_dev_as->bus),
                                             vtd_dev_as->devfn) == sid)) {
                  do_invalidate_device_tlb(vtd_dev_as, size, addr);
@@ -3983,13 +3964,12 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
  }
 
  static void vtd_prepare_identity_entry(hwaddr addr, IOMMUAccessFlags perm,
-                                         uint32_t pasid, IOMMUTLBEntry *iotlb)
+                                       IOMMUTLBEntry *iotlb)
  {
      iotlb->iova = addr & VTD_PAGE_MASK_4K;
      iotlb->translated_addr = addr & VTD_PAGE_MASK_4K;
      iotlb->addr_mask = ~VTD_PAGE_MASK_4K;
      iotlb->perm = perm;
-    iotlb->pasid = pasid;
  }
 
  static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)
@@ -4001,6 +3981,10 @@ static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)
      entry->pasid = PCI_NO_PASID;
  }
 
+/*
+ * This function returns translation result to other sub-system such as PCI,
+ * so iommu pasid is converted to PCI pasid and returned in IOMMUTLBEntry.
+ */
  static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
                                           IOMMUAccessFlags flag, int iommu_idx)
  {
@@ -4009,7 +3993,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
      IOMMUTLBEntry iotlb = {
          /* We'll fill in the rest later. */
          .target_as = &address_space_memory,
-        .pasid = vtd_as->pasid,
+        .pasid = vtd_as->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_as->pasid,
      };
      bool success;
      bool is_write = flag & IOMMU_WO;
@@ -4017,9 +4001,8 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
      if (likely(s->dmar_enabled)) {
          /* Only support translated requests in scalable mode */
          if (iommu_idx == VTD_IDX_TRANSLATED && s->root_scalable) {
-            if (vtd_as->pasid == PCI_NO_PASID) {
-                vtd_prepare_identity_entry(addr, IOMMU_RW, PCI_NO_PASID,
-                                           &iotlb);
+            if (vtd_as->pasid == IOMMU_NO_PASID) {
+                vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);
                  success = true;
              } else {
                  vtd_prepare_error_entry(&iotlb);
@@ -4034,7 +4017,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
          }
      } else {
          /* DMAR disabled, passthrough, use 4k-page*/
-        vtd_prepare_identity_entry(addr, IOMMU_RW, vtd_as->pasid, &iotlb);
+        vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);
          success = true;
      }
 
@@ -4460,7 +4443,7 @@ static void vtd_report_sid_ir_illegal_access(IntelIOMMUState *s, uint16_t sid,
      }
 
      vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR, is_fpd_set, sid, addr,
-                     is_write, pasid != PCI_NO_PASID, pasid);
+                     is_write, pasid != IOMMU_NO_PASID, pasid);
  }
 
  static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
@@ -4488,7 +4471,6 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
      int ret = 0;
      MSIMessage from = {}, to = {};
      uint16_t sid = X86_IOMMU_SID_INVALID;
-    uint32_t pasid;
 
      from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
      from.data = (uint32_t) value;
@@ -4496,11 +4478,11 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
      if (!attrs.unspecified) {
          /* We have explicit Source ID */
          sid = attrs.requester_id;
-        pasid = attrs.pid != 0 ? attrs.pid : PCI_NO_PASID;
 
          if (attrs.address_type == PCI_AT_TRANSLATED &&
              sid != X86_IOMMU_SID_INVALID) {
-            vtd_report_sid_ir_illegal_access(s, sid, pasid, from.address, true);
+            vtd_report_sid_ir_illegal_access(s, sid, attrs.pid, from.address,
+                                             true);
              return MEMTX_ERROR;
          }
      }
@@ -4562,9 +4544,19 @@ static const MemoryRegionOps vtd_mem_ir_fault_ops = {
      },
  };
 
+/*
+ * This function is called by many PCIIOMMUOps callbacks to get
+ * VTDAddressSpace or create one if non-exist. Those callbacks are
+ * used by PCI sub-system and are passed in a PCI pasid value.
+ *
+ * VTD honors iommu pasid, so the first thing is to convert PCI
+ * pasid to iommu pasid.
+ */
  VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
                                   int devfn, unsigned int pasid)
  {
+    pasid = pasid == PCI_NO_PASID ? IOMMU_NO_PASID : pasid;
+
      /*
       * We can't simply use sid here since the bus number might not be
       * initialized by the guest.
@@ -4606,7 +4598,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
          new_key->devfn = devfn;
          new_key->pasid = pasid;
 
-        if (pasid == PCI_NO_PASID) {
+        if (pasid == IOMMU_NO_PASID) {
              snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
                       PCI_FUNC(devfn));
          } else {
@@ -5290,7 +5282,7 @@ error_get_fpd_and_report:
      vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
  error_report:
      vtd_report_fault(s, -ret, is_fpd_set, sid, addr, is_write,
-                     vtd_as->pasid != PCI_NO_PASID, vtd_as->pasid);
+                     vtd_as->pasid != IOMMU_NO_PASID, vtd_as->pasid);
      return false;
  }
 
@@ -5381,7 +5373,7 @@ static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
       */
 
      /* We do not support PRI without PASID */
-    if (vtd_as->pasid == PCI_NO_PASID) {
+    if (vtd_as->pasid == IOMMU_NO_PASID) {
          return -EPERM;
      }
      if (exec_req && !is_read) {
hw/i386/intel_iommu_accel.c +1 -1
index 8940d240a1..10bdbba632 100644
@@ -207,7 +207,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
          return;
      }
 
-    assert(vtd_as->pasid == PCI_NO_PASID);
+    assert(vtd_as->pasid == IOMMU_NO_PASID);
 
      /* Nothing to do if there is no first stage HWPT attached */
      if (!pc_entry->valid ||
#13 lore raw

RE: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 7 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 7 lines
Quoted 2 lines · Zhenzhong Duan

The PCI subsystem uses PCI_NO_PASID for requests-without-PASID, but VT-d uses IOMMU_NO_PASID internally. This leads to conversion and checking code

s/VT-d uses IOMMU_NO_PASID internally/VT-d emulation uses IOMMU_NO_PASID
internally (ecap.RPS==0)/

Will do.

Quoted 9 lines
Quoted 2 lines

between PCI_NO_PASID and IOMMU_NO_PASID throughout the

implementation.

Quoted 4 lines

Refactor to use IOMMU PASID consistently within Intel IOMMU by storing IOMMU PASID value in vtd_as->pasid. After this change, PCI_NO_PASID is only used at three boundary points:

a typo or the third boundary is missed?

Section 2 contains two points:

1. Convert when notifying UNMAP events via memory_region_notify_iommu()
2. Convert when returning IOMMUTLBEntry in vtd_iommu_translate()

Quoted 115 lines

hw/i386/intel_iommu.c static int

Quoted 40 lines

1. PCI_NO_PASID -> IOMMU_NO_PASID: Convert PCI PASID to IOMMU PASID in

    vtd_find_add_as() and cache in vtd_as->pasid.

2. IOMMU_NO_PASID -> PCI_NO_PASID: Convert when notifying UNMAP events via memory_region_notify_iommu() and returning IOMMUTLBEntry in vtd_iommu_translate().

This eliminates conversion/checks in PASID table lookups, simplifies invalidation logic with consistent PASID values, and improves code readability. The PCI subsystem interface remains unchanged to maintain compatibility with other IOMMU implementations that may not use PASID 0 for requests-without-PASID.

---
  include/system/memory.h     |   2 +-
  hw/i386/intel_iommu.c       | 164 +++++++++++++++++-------------------
  hw/i386/intel_iommu_accel.c |   2 +-
  3 files changed, 80 insertions(+), 88 deletions(-)
include/system/memory.h +1 -1
index 1417132f6d..1edb38b07d 100644
@@ -150,7 +150,7 @@ struct IOMMUTLBEntry {
      hwaddr           translated_addr;
      hwaddr           addr_mask;  /* 0xfff = 4k translation */
      IOMMUAccessFlags perm;
-    uint32_t         pasid;
+    uint32_t         pasid; /* PCI pasid */
  };
 
  /*
hw/i386/intel_iommu.c +0 -0
index 5e5dcdc274..b50c556c40 100644
@@ -938,12 +938,8 @@ static int

vtd_get_pe_from_pasid_table(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 31 lines
hw/i386/intel_iommu.c +2 -11
  int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
                             VTDPASIDEntry *pe, uint32_t pasid)
  {
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
      return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
  }
 
@@ -953,15 +949,10 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
                                  uint32_t pasid)
  {
      int ret;
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
      VTDPASIDDirEntry pdire;
      VTDPASIDEntry pe;
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
-
      /*
       * No present bit check since fpd is meaningful even
       * if the present bit is clear.
@@ -1750,7 +1741,7 @@ static bool

vtd_switch_address_space(VTDAddressSpace *as)

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
           *
           * Need to disable ir for as with PASID.
           */
-        if (as->pasid != PCI_NO_PASID) {
+        if (as->pasid != IOMMU_NO_PASID) {
              memory_region_set_enabled(&as->iommu_ir, false);
          } else {
              memory_region_set_enabled(&as->iommu_ir, true);
@@ -1780,7 +1771,7 @@ static bool

vtd_switch_address_space(VTDAddressSpace *as)

hw/i386/intel_iommu.c static VTDAddressSpace

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
       * We enable per as memory region (iommu_ir_fault) for catching
       * the translation for interrupt range through PASID + PT.
       */
-    if (pt && as->pasid != PCI_NO_PASID) {
+    if (pt && as->pasid != IOMMU_NO_PASID) {
          memory_region_set_enabled(&as->iommu_ir_fault, true);
      } else {
          memory_region_set_enabled(&as->iommu_ir_fault, false);
@@ -1892,7 +1883,7 @@ static VTDAddressSpace

*vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
  VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
  {
-    return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
+    return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
  }
 
  static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
@@ -2121,10 +2112,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
      vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
      /* Try to fetch pte from IOTLB */
      iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
      if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 7 lines

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

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
      if (ret_fr) {
          if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
              vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

Thanks
Zhenzhong

Quoted 338 lines

Other part LGTM.

Regards,
Yi Liu

hw/i386/intel_iommu.c static void

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
          }
          goto error;
      }
@@ -2489,7 +2476,7 @@ static void

vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
  /*
   * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID
   * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
- * descriptor is passed which should not be PCI_NO_PASID.
+ * descriptor is passed.
   */
  static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
                                               uint16_t domain_id, hwaddr addr,
@@ -2503,48 +2490,41 @@ static void

vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void

Quoted 81 lines
hw/i386/intel_iommu.c +32 -39
      QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
          ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                         vtd_as->devfn, &ce);
-        if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+        if (ret || vtd_as->pasid != pasid ||
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {
+            continue;
+        }
+
+        if (vtd_as_has_map_notifier(vtd_as)) {
              /*
-             * 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
-             * IOMMU_NO_PASID of this device.
+             * When first stage translation is off, as long as we have MAP
+             * notifications registered in any of our IOMMU notifiers,
+             * we need to sync the shadow page table. Otherwise VFIO
+             * device attaches to nested page table instead of shadow
+             * page table, so no need to sync.
               */
-            if (!(vtd_as->pasid == pasid ||
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) {
-                continue;
-            }
-
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * When first stage translation is off, as long as we have MAP
-                 * notifications registered in any of our IOMMU notifiers,
-                 * we need to sync the shadow page table. Otherwise VFIO
-                 * device attaches to nested page table instead of shadow
-                 * page table, so no need to sync.
-                 */
-                if (!s->fsts || !s->root_scalable) {
-                    vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-                }
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                const IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                        .pasid = vtd_as->pasid,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+            if (!s->fsts || !s->root_scalable) {
+                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
              }
+        } else {
+            /*
+             * For UNMAP-only notifiers, we don't need to walk the
+             * page tables.  We just deliver the PSI down to
+             * invalidate caches.
+             */
+            const IOMMUTLBEvent event = {
+                .type = IOMMU_NOTIFIER_UNMAP,
+                .entry = {
+                    .target_as = &address_space_memory,
+                    .iova = addr,
+                    .translated_addr = 0,
+                    .addr_mask = size - 1,
+                    .perm = IOMMU_NONE,
+                     /* Other sub-systems use PCI pasid */
+                    .pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid,
+                },
+            };
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
          }
      }
  }
@@ -3007,6 +2987,7 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void

Quoted 8 lines
hw/i386/intel_iommu.c +1 -0
      VTDIOTLBPageInvInfo info;
      VTDAddressSpace *vtd_as;
      VTDContextEntry ce;
+    int ret;
 
      info.domain_id = domain_id;
      info.pasid = pasid;
@@ -3019,17 +3000,15 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool vtd_process_pasid_desc(IntelIOMMUState

Quoted 26 lines
hw/i386/intel_iommu.c +8 -10
      vtd_iommu_unlock(s);
 
      QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
-            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) &&
-                vtd_as->pasid != pasid) {
-                continue;
-            }
+        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                       vtd_as->devfn, &ce);
+        if (ret || vtd_as->pasid != pasid ||
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {
+            continue;
+        }
 
-            if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) {
-                vtd_address_space_sync(vtd_as);
-            }
+        if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) {
+            vtd_address_space_sync(vtd_as);
          }
      }
  }
@@ -3239,7 +3218,7 @@ static bool vtd_process_pasid_desc(IntelIOMMUState

*s,

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
          /* PASID selective implies a DID selective */
          trace_vtd_inv_desc_pasid_cache_psi(did, pasid);
          pc_info.did = did;
-        pc_info.pasid = pasid ?: PCI_NO_PASID;
+        pc_info.pasid = pasid;
          break;
 
      case VTD_INV_DESC_PASIDC_G_GLOBAL:
@@ -3291,6 +3270,7 @@ static void

do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,

hw/i386/intel_iommu.c static void

Quoted 8 lines
hw/i386/intel_iommu.c +1 -0
       * ...
       */
 
+    uint32_t pasid = vtd_dev_as->pasid;
      IOMMUTLBEvent event;
      uint64_t sz;
 
@@ -3307,7 +3287,8 @@ static void

do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,

hw/i386/intel_iommu.c static bool

Quoted 10 lines
hw/i386/intel_iommu.c +2 -1
      event.entry.iova = addr;
      event.entry.perm = IOMMU_NONE;
      event.entry.translated_addr = 0;
-    event.entry.pasid = vtd_dev_as->pasid;
+    /* Other sub-systems use PCI pasid */
+    event.entry.pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid;
      memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
  }
 
@@ -3335,7 +3316,7 @@ static bool

vtd_process_device_piotlb_desc(IntelIOMMUState *s,

hw/i386/intel_iommu.c static void vtd_mem_write(void *opaque, hwaddr

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
      sid = VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(inv_desc->lo);
      if (global) {
          QLIST_FOREACH(vtd_dev_as, &s->vtd_as_with_notifiers, next) {
-            if ((vtd_dev_as->pasid != PCI_NO_PASID) &&
+            if ((vtd_dev_as->pasid != IOMMU_NO_PASID) &&
                  (PCI_BUILD_BDF(pci_bus_num(vtd_dev_as->bus),
                                             vtd_dev_as->devfn) == sid)) {
                  do_invalidate_device_tlb(vtd_dev_as, size, addr);
@@ -3983,13 +3964,12 @@ static void vtd_mem_write(void *opaque, hwaddr

addr,

hw/i386/intel_iommu.c static inline void

Quoted 15 lines
hw/i386/intel_iommu.c +1 -2
  }
 
  static void vtd_prepare_identity_entry(hwaddr addr, IOMMUAccessFlags perm,
-                                         uint32_t pasid, IOMMUTLBEntry *iotlb)
+                                       IOMMUTLBEntry *iotlb)
  {
      iotlb->iova = addr & VTD_PAGE_MASK_4K;
      iotlb->translated_addr = addr & VTD_PAGE_MASK_4K;
      iotlb->addr_mask = ~VTD_PAGE_MASK_4K;
      iotlb->perm = perm;
-    iotlb->pasid = pasid;
  }
 
  static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)
@@ -4001,6 +3981,10 @@ static inline void

vtd_prepare_error_entry(IOMMUTLBEntry *entry)

hw/i386/intel_iommu.c static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)

Quoted 8 lines
hw/i386/intel_iommu.c +4 -0
      entry->pasid = PCI_NO_PASID;
  }
 
+/*
+ * This function returns translation result to other sub-system such as PCI,
+ * so iommu pasid is converted to PCI pasid and returned in IOMMUTLBEntry.
+ */
  static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu,

hwaddr addr,

hw/i386/intel_iommu.c static IOMMUTLBEntry

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                           IOMMUAccessFlags flag, int iommu_idx)
  {
@@ -4009,7 +3993,7 @@ static IOMMUTLBEntry

vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,

hw/i386/intel_iommu.c static IOMMUTLBEntry

Quoted 10 lines
hw/i386/intel_iommu.c +1 -1
      IOMMUTLBEntry iotlb = {
          /* We'll fill in the rest later. */
          .target_as = &address_space_memory,
-        .pasid = vtd_as->pasid,
+        .pasid = vtd_as->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_as-
 pasid,
      };
      bool success;
      bool is_write = flag & IOMMU_WO;
hw/i386/intel_iommu.c +0 -0
@@ -4017,9 +4001,8 @@ static IOMMUTLBEntry

vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,

hw/i386/intel_iommu.c static IOMMUTLBEntry

Quoted 12 lines
hw/i386/intel_iommu.c +2 -3
      if (likely(s->dmar_enabled)) {
          /* Only support translated requests in scalable mode */
          if (iommu_idx == VTD_IDX_TRANSLATED && s->root_scalable) {
-            if (vtd_as->pasid == PCI_NO_PASID) {
-                vtd_prepare_identity_entry(addr, IOMMU_RW, PCI_NO_PASID,
-                                           &iotlb);
+            if (vtd_as->pasid == IOMMU_NO_PASID) {
+                vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);
                  success = true;
              } else {
                  vtd_prepare_error_entry(&iotlb);
@@ -4034,7 +4017,7 @@ static IOMMUTLBEntry

vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
          }
      } else {
          /* DMAR disabled, passthrough, use 4k-page*/
-        vtd_prepare_identity_entry(addr, IOMMU_RW, vtd_as->pasid, &iotlb);
+        vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);
          success = true;
      }
 
@@ -4460,7 +4443,7 @@ static void

vtd_report_sid_ir_illegal_access(IntelIOMMUState *s, uint16_t sid,

hw/i386/intel_iommu.c static MemTxResult vtd_mem_ir_write(void *opaque,

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
      }
 
      vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR, is_fpd_set, sid, addr,
-                     is_write, pasid != PCI_NO_PASID, pasid);
+                     is_write, pasid != IOMMU_NO_PASID, pasid);
  }
 
  static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
@@ -4488,7 +4471,6 @@ static MemTxResult vtd_mem_ir_write(void *opaque,

hwaddr addr,

hw/i386/intel_iommu.c static MemTxResult vtd_mem_ir_write(void

Quoted 8 lines
hw/i386/intel_iommu.c +0 -1
      int ret = 0;
      MSIMessage from = {}, to = {};
      uint16_t sid = X86_IOMMU_SID_INVALID;
-    uint32_t pasid;
 
      from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
      from.data = (uint32_t) value;
@@ -4496,11 +4478,11 @@ static MemTxResult vtd_mem_ir_write(void

*opaque, hwaddr addr,

hw/i386/intel_iommu.c static const MemoryRegionOps

Quoted 14 lines
hw/i386/intel_iommu.c +2 -2
      if (!attrs.unspecified) {
          /* We have explicit Source ID */
          sid = attrs.requester_id;
-        pasid = attrs.pid != 0 ? attrs.pid : PCI_NO_PASID;
 
          if (attrs.address_type == PCI_AT_TRANSLATED &&
              sid != X86_IOMMU_SID_INVALID) {
-            vtd_report_sid_ir_illegal_access(s, sid, pasid, from.address, true);
+            vtd_report_sid_ir_illegal_access(s, sid, attrs.pid, from.address,
+                                             true);
              return MEMTX_ERROR;
          }
      }
@@ -4562,9 +4544,19 @@ static const MemoryRegionOps

vtd_mem_ir_fault_ops = {

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 20 lines
hw/i386/intel_iommu.c +10 -0
      },
  };
 
+/*
+ * This function is called by many PCIIOMMUOps callbacks to get
+ * VTDAddressSpace or create one if non-exist. Those callbacks are
+ * used by PCI sub-system and are passed in a PCI pasid value.
+ *
+ * VTD honors iommu pasid, so the first thing is to convert PCI
+ * pasid to iommu pasid.
+ */
  VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
                                   int devfn, unsigned int pasid)
  {
+    pasid = pasid == PCI_NO_PASID ? IOMMU_NO_PASID : pasid;
+
      /*
       * We can't simply use sid here since the bus number might not be
       * initialized by the guest.
@@ -4606,7 +4598,7 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c static int vtd_pri_request_page(PCIBus *bus, void

Quoted 18 lines
hw/i386/intel_iommu.c +2 -2
          new_key->devfn = devfn;
          new_key->pasid = pasid;
 
-        if (pasid == PCI_NO_PASID) {
+        if (pasid == IOMMU_NO_PASID) {
              snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
                       PCI_FUNC(devfn));
          } else {
@@ -5290,7 +5282,7 @@ error_get_fpd_and_report:
      vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
  error_report:
      vtd_report_fault(s, -ret, is_fpd_set, sid, addr, is_write,
-                     vtd_as->pasid != PCI_NO_PASID, vtd_as->pasid);
+                     vtd_as->pasid != IOMMU_NO_PASID, vtd_as->pasid);
      return false;
  }
 
@@ -5381,7 +5373,7 @@ static int vtd_pri_request_page(PCIBus *bus, void

*opaque, int devfn,

hw/i386/intel_iommu_accel.c static void vtd_flush_host_piotlb_locked(gpointer key,

Quoted 13 lines
hw/i386/intel_iommu.c +1 -1
       */
 
      /* We do not support PRI without PASID */
-    if (vtd_as->pasid == PCI_NO_PASID) {
+    if (vtd_as->pasid == IOMMU_NO_PASID) {
          return -EPERM;
      }
      if (exec_req && !is_read) {
hw/i386/intel_iommu_accel.c +0 -0
index 8940d240a1..10bdbba632 100644
@@ -207,7 +207,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key,

gpointer value,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -1
          return;
      }
 
-    assert(vtd_as->pasid == PCI_NO_PASID);
+    assert(vtd_as->pasid == IOMMU_NO_PASID);
 
      /* Nothing to do if there is no first stage HWPT attached */
      if (!pc_entry->valid ||
#14 lore raw

Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 25 lines · Zhenzhong
Quoted 7 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 7 lines
Quoted 2 lines · Zhenzhong Duan

The PCI subsystem uses PCI_NO_PASID for requests-without-PASID, but VT-d uses IOMMU_NO_PASID internally. This leads to conversion and checking code

s/VT-d uses IOMMU_NO_PASID internally/VT-d emulation uses IOMMU_NO_PASID
internally (ecap.RPS==0)/

Will do.

Quoted 8 lines
Quoted 2 lines

between PCI_NO_PASID and IOMMU_NO_PASID throughout the

implementation.

Quoted 4 lines

Refactor to use IOMMU PASID consistently within Intel IOMMU by storing IOMMU PASID value in vtd_as->pasid. After this change, PCI_NO_PASID is only used at three boundary points:

Quoted 6 lines
Quoted 1 line

a typo or the third boundary is missed?

Section 2 contains two points:

1. Convert when notifying UNMAP events via memory_region_notify_iommu()
2. Convert when returning IOMMUTLBEntry in vtd_iommu_translate()

got it.

Quoted 121 lines
Quoted 121 lines

hw/i386/intel_iommu.c static int

Quoted 40 lines

1. PCI_NO_PASID -> IOMMU_NO_PASID: Convert PCI PASID to IOMMU PASID in

     vtd_find_add_as() and cache in vtd_as->pasid.

2. IOMMU_NO_PASID -> PCI_NO_PASID: Convert when notifying UNMAP events via memory_region_notify_iommu() and returning IOMMUTLBEntry in vtd_iommu_translate().

This eliminates conversion/checks in PASID table lookups, simplifies invalidation logic with consistent PASID values, and improves code readability. The PCI subsystem interface remains unchanged to maintain compatibility with other IOMMU implementations that may not use PASID 0 for requests-without-PASID.

---
   include/system/memory.h     |   2 +-
   hw/i386/intel_iommu.c       | 164 +++++++++++++++++-------------------
   hw/i386/intel_iommu_accel.c |   2 +-
   3 files changed, 80 insertions(+), 88 deletions(-)
include/system/memory.h +1 -1
index 1417132f6d..1edb38b07d 100644
@@ -150,7 +150,7 @@ struct IOMMUTLBEntry {
       hwaddr           translated_addr;
       hwaddr           addr_mask;  /* 0xfff = 4k translation */
       IOMMUAccessFlags perm;
-    uint32_t         pasid;
+    uint32_t         pasid; /* PCI pasid */
   };
 
   /*
hw/i386/intel_iommu.c +0 -0
index 5e5dcdc274..b50c556c40 100644
@@ -938,12 +938,8 @@ static int

vtd_get_pe_from_pasid_table(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 31 lines
hw/i386/intel_iommu.c +2 -11
   int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
                              VTDPASIDEntry *pe, uint32_t pasid)
   {
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
       return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
   }
 
@@ -953,15 +949,10 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
                                   uint32_t pasid)
   {
       int ret;
-    dma_addr_t pasid_dir_base;
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
       VTDPASIDDirEntry pdire;
       VTDPASIDEntry pe;
 
-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
-
       /*
        * No present bit check since fpd is meaningful even
        * if the present bit is clear.
@@ -1750,7 +1741,7 @@ static bool

vtd_switch_address_space(VTDAddressSpace *as)

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
            *
            * Need to disable ir for as with PASID.
            */
-        if (as->pasid != PCI_NO_PASID) {
+        if (as->pasid != IOMMU_NO_PASID) {
               memory_region_set_enabled(&as->iommu_ir, false);
           } else {
               memory_region_set_enabled(&as->iommu_ir, true);
@@ -1780,7 +1771,7 @@ static bool

vtd_switch_address_space(VTDAddressSpace *as)

hw/i386/intel_iommu.c static VTDAddressSpace

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
        * We enable per as memory region (iommu_ir_fault) for catching
        * the translation for interrupt range through PASID + PT.
        */
-    if (pt && as->pasid != PCI_NO_PASID) {
+    if (pt && as->pasid != IOMMU_NO_PASID) {
           memory_region_set_enabled(&as->iommu_ir_fault, true);
       } else {
           memory_region_set_enabled(&as->iommu_ir_fault, false);
@@ -1892,7 +1883,7 @@ static VTDAddressSpace

*vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,

hw/i386/intel_iommu.c static bool

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
   VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
   {
-    return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
+    return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
   }
 
   static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
@@ -2121,10 +2112,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
       vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
       /* Try to fetch pte from IOTLB */
       iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
       if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
       if (ret_fr) {
           if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
               vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);
Quoted 4 lines
Quoted 1 line

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

not quite get. It can still possibly be IOMMU_NO_PASID, right? If not, this looks to have introduced functional change, which should not be expected by this patch.

#15 lore raw

RE: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Review static bool

Quoted 1 line
(quoted diff) +0 -0
@@ -2121,10 +2112,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
       vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
       /* Try to fetch pte from IOTLB */
       iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
       if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
       if (ret_fr) {
           if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
               vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

not quite get. It can still possibly be IOMMU_NO_PASID, right? If not, this looks to have introduced functional change, which should not be expected by this patch.

Before this patch, If pasid == PCI_NO_PASID && s->root_scalable is true, then we pass is_pasid=true to vtd_report_fault().
If we change to use "pasid != IOMMU_NO_PASID" in this patch, then we pass is_pasid=false to vtd_report_fault() for the same scenario because pasid is IOMMU_NO_PASID now.

But using s->root_scalable could keep functions unchanged, right?

Thanks
Zhenzhong

#16 lore raw

Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 25 lines · Zhenzhong
Quoted 25 lines
Quoted 24 lines
Quoted 14 lines

Review static bool

Quoted 1 line
(quoted diff) +0 -0
@@ -2121,10 +2112,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
        vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
        /* Try to fetch pte from IOTLB */
        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
        if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Quoted 7 lines

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

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
        if (ret_fr) {
            if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
                vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

Quoted 8 lines
Quoted 3 lines

not quite get. It can still possibly be IOMMU_NO_PASID, right? If not, this looks to have introduced functional change, which should not be expected by this patch.

Before this patch, If pasid == PCI_NO_PASID && s->root_scalable is true, then we pass is_pasid=true to vtd_report_fault().
If we change to use "pasid != IOMMU_NO_PASID" in this patch, then we pass is_pasid=false to vtd_report_fault() for the same scenario because pasid is IOMMU_NO_PASID now.

But using s->root_scalable could keep functions unchanged, right?

got your point. But this helper should cover both translation w/ and w/o pasid, it should not reply on the s->root_scalable, but rely on if pasid is included. So the logic does not look correct.

checked the original commit. It seems recording the rid_pasid in the fault reporting wrongly. And that's why you got the current logic here. May need a fix. Below is the original commit, and the spec definition.

- commit: 1b2b12376c8a513a0c7b5e3b8ea702038d3d7db5

- Spec says pasid is only valid when the fault request is request with PASID.

"
When Set, indicates the faulted request has a PASID TLP Prefix. The value of the PASID field is reported in the PASID Value (PV) field. "

#17 lore raw

RE: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 55 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 22 lines · Zhenzhong
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines

Review static bool

Quoted 1 line
(quoted diff) +0 -0
@@ -2121,10 +2112,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
        vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
        /* Try to fetch pte from IOTLB */
        iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
        if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ 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 2225

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
        if (ret_fr) {
            if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
                vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

not quite get. It can still possibly be IOMMU_NO_PASID, right? If not, this looks to have introduced functional change, which should not be expected by this patch.

Before this patch, If pasid == PCI_NO_PASID && s->root_scalable is true, then we

pass is_pasid=true to vtd_report_fault().

Quoted 1 line

If we change to use "pasid != IOMMU_NO_PASID" in this patch, then we pass

is_pasid=false to vtd_report_fault() for the same scenario because pasid is IOMMU_NO_PASID now.

Quoted 2 lines

But using s->root_scalable could keep functions unchanged, right?

got your point. But this helper should cover both translation w/ and w/o pasid, it should not reply on the s->root_scalable, but rely on if pasid is included. So the logic does not look correct.

checked the original commit. It seems recording the rid_pasid in the fault reporting wrongly. And that's why you got the current logic here. May need a fix. Below is the original commit, and the spec definition.

- commit: 1b2b12376c8a513a0c7b5e3b8ea702038d3d7db5

Quoted 7 lines

- Spec says pasid is only valid when the fault request is request with PASID.

"
When Set, indicates the faulted request has a PASID TLP Prefix. The value of the PASID field is reported in the PASID Value (PV) field. "

I see, you are right, will use "pasid != IOMMU_NO_PASID" in this patch, let me know if you want a separate fix patch instead.

Thanks
Zhenzhong

#18 lore raw

Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 58 lines · Zhenzhong
Quoted 58 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 22 lines · Zhenzhong
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines

Review static bool

Quoted 1 line
(quoted diff) +0 -0
@@ -2121,10 +2112,6 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Review static bool

Quoted 11 lines
(quoted diff) +0 -4
         vtd_iommu_lock(s);
 
-    if (pasid == PCI_NO_PASID && s->root_scalable) {
-        pasid = IOMMU_NO_PASID;
-    }
-
         /* Try to fetch pte from IOTLB */
         iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
         if (iotlb_entry) {
@@ -2235,7 +2222,7 @@ static bool

vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,

Review static bool · line 2225

Quoted 5 lines
(quoted diff) +1 -1
         if (ret_fr) {
             if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {
                 vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);
+                            addr, is_write, s->root_scalable, pasid);

a typo here? s->root_scalable should be "pasid != IOMMU_NO_PASID"?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

This is intentional.
pasid = vtd_as->pasid = iommu_pasid, so it never equals PCI_NO_PASID now.

not quite get. It can still possibly be IOMMU_NO_PASID, right? If not, this looks to have introduced functional change, which should not be expected by this patch.

Before this patch, If pasid == PCI_NO_PASID && s->root_scalable is true, then we

pass is_pasid=true to vtd_report_fault().

Quoted 1 line

If we change to use "pasid != IOMMU_NO_PASID" in this patch, then we pass

is_pasid=false to vtd_report_fault() for the same scenario because pasid is IOMMU_NO_PASID now.

Quoted 2 lines

But using s->root_scalable could keep functions unchanged, right?

got your point. But this helper should cover both translation w/ and w/o pasid, it should not reply on the s->root_scalable, but rely on if pasid is included. So the logic does not look correct.

checked the original commit. It seems recording the rid_pasid in the fault reporting wrongly. And that's why you got the current logic here. May need a fix. Below is the original commit, and the spec definition.

- commit: 1b2b12376c8a513a0c7b5e3b8ea702038d3d7db5

- Spec says pasid is only valid when the fault request is request with PASID.

Quoted 7 lines
Quoted 4 lines

"
When Set, indicates the faulted request has a PASID TLP Prefix. The value of the PASID field is reported in the PASID Value (PV) field. "

I see, you are right, will use "pasid != IOMMU_NO_PASID" in this patch, let me know if you want a separate fix patch instead.

go with a separate fix patch is better. :)

#19 lore raw

Re: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

In reply to Zhenzhong Duan
Quoted 357 lines · Zhenzhong Duan

The PCI subsystem uses PCI_NO_PASID for requests-without-PASID, but VT-d uses IOMMU_NO_PASID internally. This leads to conversion and checking code between PCI_NO_PASID and IOMMU_NO_PASID throughout the implementation.

Refactor to use IOMMU PASID consistently within Intel IOMMU by storing IOMMU PASID value in vtd_as->pasid. After this change, PCI_NO_PASID is only used at three boundary points:

1. PCI_NO_PASID -> IOMMU_NO_PASID: Convert PCI PASID to IOMMU PASID in vtd_find_add_as() and cache in vtd_as->pasid.
2. IOMMU_NO_PASID -> PCI_NO_PASID: Convert when notifying UNMAP events via memory_region_notify_iommu() and returning IOMMUTLBEntry in vtd_iommu_translate().

This eliminates conversion/checks in PASID table lookups, simplifies invalidation logic with consistent PASID values, and improves code readability. The PCI subsystem interface remains unchanged to maintain compatibility with other IOMMU implementations that may not use PASID 0 for requests-without-PASID.

---
 include/system/memory.h     |   2 +-
 hw/i386/intel_iommu.c       | 164 +++++++++++++++++------------------- hw/i386/intel_iommu_accel.c |   2 +-

 3 files changed, 80 insertions(+), 88 deletions(-)
include/system/memory.h +0 -0
index 1417132f6d..1edb38b07d 100644  
@@ -150,7 +150,7 @@ struct IOMMUTLBEntry {  
      hwaddr           translated_addr;  

     hwaddr           addr_mask;  /* 0xfff = 4k translation */ IOMMUAccessFlags perm;
-    uint32_t         pasid;
+    uint32_t         pasid; /* PCI pasid */
 };

 /*

hw/i386/intel_iommu.c +1 -1
index 5e5dcdc274..b50c556c40 100644  
@@ -938,12 +938,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,  
  int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,  
                             VTDPASIDEntry *pe, uint32_t pasid)  
  {  
-    dma_addr_t pasid_dir_base;  
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);  


-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); return vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe); }

hw/i386/intel_iommu.c +1 -1
@@ -953,15 +949,10 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,  
                                  uint32_t pasid)  
  {  
      int ret;  
-    dma_addr_t pasid_dir_base;  
+    dma_addr_t pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);  
      VTDPASIDDirEntry pdire;  
      VTDPASIDEntry pe;  


-    if (pasid == PCI_NO_PASID) {
-        pasid = IOMMU_NO_PASID;
-    }
-    pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce); -
     /*
      * No present bit check since fpd is meaningful even
      * if the present bit is clear.

hw/i386/intel_iommu.c +2 -2
@@ -1750,7 +1741,7 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)  
           *  
           * Need to disable ir for as with PASID.  
           */  
-        if (as->pasid != PCI_NO_PASID) {  
+        if (as->pasid != IOMMU_NO_PASID) {  
              memory_region_set_enabled(&as->iommu_ir, false);  
          } else {  
              memory_region_set_enabled(&as->iommu_ir, true);  
@@ -1780,7 +1771,7 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)  
       * We enable per as memory region (iommu_ir_fault) for catching  
       * the translation for interrupt range through PASID + PT.  
       */  
-    if (pt && as->pasid != PCI_NO_PASID) {  
+    if (pt && as->pasid != IOMMU_NO_PASID) {  
          memory_region_set_enabled(&as->iommu_ir_fault, true);  
      } else {  
          memory_region_set_enabled(&as->iommu_ir_fault, false);  
@@ -1892,7 +1883,7 @@ static VTDAddressSpace *vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,  


 VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid) {
-    return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
+    return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID); }

 static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)

hw/i386/intel_iommu.c +0 -0
@@ -2121,10 +2112,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  


     vtd_iommu_lock(s);

-    if (pasid == PCI_NO_PASID && s->root_scalable) { -        pasid = IOMMU_NO_PASID;
-    }
-
     /* Try to fetch pte from IOTLB */
     iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr); if (iotlb_entry) {

hw/i386/intel_iommu.c +35 -41
@@ -2235,7 +2222,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  
      if (ret_fr) {  
          if (!vtd_is_recoverable_fault(-ret_fr, iommu_idx)) {  
              vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,  
-                            addr, is_write, pasid != PCI_NO_PASID, pasid);  
+                            addr, is_write, s->root_scalable, pasid);  
          }  
          goto error;  
      }  
@@ -2489,7 +2476,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)  
  /*  
   * There is no pasid field in iotlb invalidation descriptor, so IOMMU_NO_PASID  
   * is passed as parameter. Piotlb invalidation supports pasid, pasid in its  
- * descriptor is passed which should not be PCI_NO_PASID.  
+ * descriptor is passed.  
   */  
  static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,  
                                               uint16_t domain_id, hwaddr addr,  
@@ -2503,48 +2490,41 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,  
      QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {  
          ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),  
                                         vtd_as->devfn, &ce);  
-        if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {  
+        if (ret || vtd_as->pasid != pasid ||  
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {  
+            continue;  
+        }  
+  
+        if (vtd_as_has_map_notifier(vtd_as)) {  
              /*  
-             * 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  
-             * IOMMU_NO_PASID of this device.  
+             * When first stage translation is off, as long as we have MAP  
+             * notifications registered in any of our IOMMU notifiers,  
+             * we need to sync the shadow page table. Otherwise VFIO  
+             * device attaches to nested page table instead of shadow  
+             * page table, so no need to sync.  
               */  
-            if (!(vtd_as->pasid == pasid ||  
-                  (vtd_as->pasid == PCI_NO_PASID && pasid == IOMMU_NO_PASID))) {  
-                continue;  
-            }  
-  
-            if (vtd_as_has_map_notifier(vtd_as)) {  
-                /*  
-                 * When first stage translation is off, as long as we have MAP  
-                 * notifications registered in any of our IOMMU notifiers,  
-                 * we need to sync the shadow page table. Otherwise VFIO  
-                 * device attaches to nested page table instead of shadow  
-                 * page table, so no need to sync.  
-                 */  
-                if (!s->fsts || !s->root_scalable) {  
-                    vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);  
-                }  
-            } else {  
-                /*  
-                 * For UNMAP-only notifiers, we don't need to walk the  
-                 * page tables.  We just deliver the PSI down to  
-                 * invalidate caches.  
-                 */  
-                const IOMMUTLBEvent event = {  
-                    .type = IOMMU_NOTIFIER_UNMAP,  
-                    .entry = {  
-                        .target_as = &address_space_memory,  
-                        .iova = addr,  
-                        .translated_addr = 0,  
-                        .addr_mask = size - 1,  
-                        .perm = IOMMU_NONE,  
-                        .pasid = vtd_as->pasid,  
-                    },  
-                };  
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);  
+            if (!s->fsts || !s->root_scalable) {  
+                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);  
              }  
+        } else {  
+            /*  
+             * For UNMAP-only notifiers, we don't need to walk the  
+             * page tables.  We just deliver the PSI down to  
+             * invalidate caches.  
+             */  
+            const IOMMUTLBEvent event = {  
+                .type = IOMMU_NOTIFIER_UNMAP,  
+                .entry = {  
+                    .target_as = &address_space_memory,  
+                    .iova = addr,  
+                    .translated_addr = 0,  
+                    .addr_mask = size - 1,  
+                    .perm = IOMMU_NONE,  
+                     /* Other sub-systems use PCI pasid */  
+                    .pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid,  
+                },  
+            };  
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);  
          }  
      }  
  }  
@@ -3007,6 +2987,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,  
      VTDIOTLBPageInvInfo info;  
      VTDAddressSpace *vtd_as;  
      VTDContextEntry ce;  
+    int ret;  


     info.domain_id = domain_id;
     info.pasid = pasid;

hw/i386/intel_iommu.c +0 -0
@@ -3019,17 +3000,15 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,  
      vtd_iommu_unlock(s);  


     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), -                                      vtd_as->devfn, &ce) &&
-            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) { -            if ((vtd_as->pasid != PCI_NO_PASID || pasid != IOMMU_NO_PASID) && -                vtd_as->pasid != pasid) {
-                continue;
-            }
+        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), +                                       vtd_as->devfn, &ce);
+        if (ret || vtd_as->pasid != pasid ||
+            domain_id != vtd_get_domain_id(s, &ce, pasid)) {
+            continue;
+        }

-            if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) { -                vtd_address_space_sync(vtd_as);
-            }
+        if (!s->fsts || !vtd_as_has_map_notifier(vtd_as)) {
+            vtd_address_space_sync(vtd_as);
         }
     }
 }

hw/i386/intel_iommu.c +1 -1
@@ -3239,7 +3218,7 @@ static bool vtd_process_pasid_desc(IntelIOMMUState *s,  
          /* PASID selective implies a DID selective */  
          trace_vtd_inv_desc_pasid_cache_psi(did, pasid);  
          pc_info.did = did;  
-        pc_info.pasid = pasid ?: PCI_NO_PASID;  
+        pc_info.pasid = pasid;  
          break;  


     case VTD_INV_DESC_PASIDC_G_GLOBAL:

hw/i386/intel_iommu.c +0 -0
@@ -3291,6 +3270,7 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,  
       * ...  
       */  


+    uint32_t pasid = vtd_dev_as->pasid;
     IOMMUTLBEvent event;
     uint64_t sz;

hw/i386/intel_iommu.c +2 -1
@@ -3307,7 +3287,8 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,  
      event.entry.iova = addr;  
      event.entry.perm = IOMMU_NONE;  
      event.entry.translated_addr = 0;  
-    event.entry.pasid = vtd_dev_as->pasid;  
+    /* Other sub-systems use PCI pasid */  
+    event.entry.pasid = pasid == IOMMU_NO_PASID ? PCI_NO_PASID : pasid;  
      memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);  
  }  

hw/i386/intel_iommu.c +1 -1
@@ -3335,7 +3316,7 @@ static bool vtd_process_device_piotlb_desc(IntelIOMMUState *s,  
      sid = VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(inv_desc->lo);  
      if (global) {  
          QLIST_FOREACH(vtd_dev_as, &s->vtd_as_with_notifiers, next) {  
-            if ((vtd_dev_as->pasid != PCI_NO_PASID) &&  
+            if ((vtd_dev_as->pasid != IOMMU_NO_PASID) &&  
                  (PCI_BUILD_BDF(pci_bus_num(vtd_dev_as->bus),  
                                             vtd_dev_as->devfn) == sid)) {  
                  do_invalidate_device_tlb(vtd_dev_as, size, addr);  
@@ -3983,13 +3964,12 @@ static void vtd_mem_write(void *opaque, hwaddr addr,  
  }  


 static void vtd_prepare_identity_entry(hwaddr addr, IOMMUAccessFlags perm, -                                         uint32_t pasid, IOMMUTLBEntry *iotlb) +                                       IOMMUTLBEntry *iotlb)
 {
     iotlb->iova = addr & VTD_PAGE_MASK_4K;
     iotlb->translated_addr = addr & VTD_PAGE_MASK_4K; iotlb->addr_mask = ~VTD_PAGE_MASK_4K;
     iotlb->perm = perm;
-    iotlb->pasid = pasid;
 }

 static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)

hw/i386/intel_iommu.c +0 -0
@@ -4001,6 +3981,10 @@ static inline void vtd_prepare_error_entry(IOMMUTLBEntry *entry)  
      entry->pasid = PCI_NO_PASID;  
  }  


+/*
+ * This function returns translation result to other sub-system such as PCI,
+ * so iommu pasid is converted to PCI pasid and returned in IOMMUTLBEntry.
+ */
 static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr, IOMMUAccessFlags flag, int iommu_idx) {

hw/i386/intel_iommu.c +4 -5
@@ -4009,7 +3993,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,  
      IOMMUTLBEntry iotlb = {  
          /* We'll fill in the rest later. */  
          .target_as = &address_space_memory,  
-        .pasid = vtd_as->pasid,  
+        .pasid = vtd_as->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_as->pasid,  
      };  
      bool success;  
      bool is_write = flag & IOMMU_WO;  
@@ -4017,9 +4001,8 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,  
      if (likely(s->dmar_enabled)) {  
          /* Only support translated requests in scalable mode */  
          if (iommu_idx == VTD_IDX_TRANSLATED && s->root_scalable) {  
-            if (vtd_as->pasid == PCI_NO_PASID) {  
-                vtd_prepare_identity_entry(addr, IOMMU_RW, PCI_NO_PASID,  
-                                           &iotlb);  
+            if (vtd_as->pasid == IOMMU_NO_PASID) {  
+                vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);  
                  success = true;  
              } else {  
                  vtd_prepare_error_entry(&iotlb);  
@@ -4034,7 +4017,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,  
          }  
      } else {  
          /* DMAR disabled, passthrough, use 4k-page*/  
-        vtd_prepare_identity_entry(addr, IOMMU_RW, vtd_as->pasid, &iotlb);  
+        vtd_prepare_identity_entry(addr, IOMMU_RW, &iotlb);  
          success = true;  
      }  

hw/i386/intel_iommu.c +0 -0
@@ -4460,7 +4443,7 @@ static void vtd_report_sid_ir_illegal_access(IntelIOMMUState *s, uint16_t sid,  
      }  


     vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR, is_fpd_set, sid, addr, -                     is_write, pasid != PCI_NO_PASID, pasid); +                     is_write, pasid != IOMMU_NO_PASID, pasid); }

 static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c +0 -1
@@ -4488,7 +4471,6 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,  
      int ret = 0;  
      MSIMessage from = {}, to = {};  
      uint16_t sid = X86_IOMMU_SID_INVALID;  
-    uint32_t pasid;  


     from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST; from.data = (uint32_t) value;

hw/i386/intel_iommu.c static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr, · line 4499

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
@@ -4496,11 +4478,11 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,  
      if (!attrs.unspecified) {  
          /* We have explicit Source ID */  
          sid = attrs.requester_id;  
-        pasid = attrs.pid != 0 ? attrs.pid : PCI_NO_PASID;  

Are we relying on the fact that IOMMU_NO_PASID is 0? Maybe we could add a comment/assert about that?

Quoted 69 lines


         if (attrs.address_type == PCI_AT_TRANSLATED &&
             sid != X86_IOMMU_SID_INVALID) {
-            vtd_report_sid_ir_illegal_access(s, sid, pasid, from.address, true); +            vtd_report_sid_ir_illegal_access(s, sid, attrs.pid, from.address, +                                             true);
             return MEMTX_ERROR;
         }
     }

hw/i386/intel_iommu.c +0 -0
@@ -4562,9 +4544,19 @@ static const MemoryRegionOps vtd_mem_ir_fault_ops = {  
      },  
  };  


+/*
+ * This function is called by many PCIIOMMUOps callbacks to get
+ * VTDAddressSpace or create one if non-exist. Those callbacks are
+ * used by PCI sub-system and are passed in a PCI pasid value.
+ *
+ * VTD honors iommu pasid, so the first thing is to convert PCI
+ * pasid to iommu pasid.
+ */
 VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn, unsigned int pasid) {
+    pasid = pasid == PCI_NO_PASID ? IOMMU_NO_PASID : pasid; +
     /*
      * We can't simply use sid here since the bus number might not be
      * initialized by the guest.

hw/i386/intel_iommu.c +0 -0
@@ -4606,7 +4598,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,  
          new_key->devfn = devfn;  
          new_key->pasid = pasid;  


-        if (pasid == PCI_NO_PASID) {
+        if (pasid == IOMMU_NO_PASID) {
             snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn), PCI_FUNC(devfn));
         } else {

hw/i386/intel_iommu.c +0 -0
@@ -5290,7 +5282,7 @@ error_get_fpd_and_report:  
      vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);  

 error_report:
     vtd_report_fault(s, -ret, is_fpd_set, sid, addr, is_write, -                     vtd_as->pasid != PCI_NO_PASID, vtd_as->pasid); +                     vtd_as->pasid != IOMMU_NO_PASID, vtd_as->pasid); return false;
 }

hw/i386/intel_iommu.c +0 -0
@@ -5381,7 +5373,7 @@ static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,  
       */  


     /* We do not support PRI without PASID */
-    if (vtd_as->pasid == PCI_NO_PASID) {
+    if (vtd_as->pasid == IOMMU_NO_PASID) {
         return -EPERM;
     }
     if (exec_req && !is_read) {

hw/i386/intel_iommu_accel.c +0 -0
index 8940d240a1..10bdbba632 100644  
@@ -207,7 +207,7 @@ static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,  
          return;  
      }  


-    assert(vtd_as->pasid == PCI_NO_PASID);
+    assert(vtd_as->pasid == IOMMU_NO_PASID);

     /* Nothing to do if there is no first stage HWPT attached */ if (!pc_entry->valid ||

#20 lore raw

RE: [PATCH v5 08/15] intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally

Quoted 11 lines

Review static MemTxResult vtd_mem_ir_write(void *opaque,

Quoted 1 line
(quoted diff) +0 -0
@@ -4488,7 +4471,6 @@ static MemTxResult vtd_mem_ir_write(void *opaque,

hwaddr addr,

Review static MemTxResult vtd_mem_ir_write(void

Quoted 8 lines
(quoted diff) +0 -1
      int ret = 0;
      MSIMessage from = {}, to = {};
      uint16_t sid = X86_IOMMU_SID_INVALID;
-    uint32_t pasid;
 
      from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
      from.data = (uint32_t) value;
@@ -4496,11 +4478,11 @@ static MemTxResult vtd_mem_ir_write(void

*opaque, hwaddr addr,

Quoted 7 lines

Review static MemTxResult vtd_mem_ir_write(void · line 4499

Quoted 4 lines
(quoted diff) +0 -1
      if (!attrs.unspecified) {
          /* We have explicit Source ID */
          sid = attrs.requester_id;
-        pasid = attrs.pid != 0 ? attrs.pid : PCI_NO_PASID;

Are we relying on the fact that IOMMU_NO_PASID is 0? Maybe we could add a comment/assert about that?

Yes, will add below to prevent anyone changing its value.

include/hw/core/iommu.h +4 -0
@@ -41,6 +41,10 @@ enum host_iommu_quirks {
     HOST_IOMMU_QUIRK_NESTING_PARENT_BYPASS_RO = BIT_ULL(0),
 };
 
+/*
+ * Value 0 is shared between kernel and QEMU for IOMMU_NO_PASID, so never
+ * change it
+ */
 #define IOMMU_NO_PASID 0
 

Thanks
Zhenzhong

#21 lore raw

PATCH v5 9/15 intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

In reply to Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

---
 hw/i386/intel_iommu_accel.h    |  13 +++
 hw/i386/intel_iommu_internal.h |   8 ++
 hw/i386/intel_iommu.c          |   3 +
 hw/i386/intel_iommu_accel.c    | 156 +++++++++++++++++++++++++++++++++
 4 files changed, 180 insertions(+)
hw/i386/intel_iommu_accel.h +13 -0
index e5f0b077b4..c9b1823745 100644
@@ -12,6 +12,13 @@
 #define HW_I386_INTEL_IOMMU_ACCEL_H
 #include CONFIG_DEVICES
 
+typedef struct VTDAccelPASIDCacheEntry {
+    VTDHostIOMMUDevice *vtd_hiod;
+    VTDPASIDEntry pasid_entry;
+    uint32_t pasid;
+    QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
+} VTDAccelPASIDCacheEntry;
+
 #ifdef CONFIG_VTD_ACCEL
 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                           Error **errp);
@@ -20,6 +27,7 @@ bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                                       uint32_t pasid, hwaddr addr,
                                       uint64_t npages, bool ih);
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
 void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
 #else
 static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -49,6 +57,11 @@ static inline void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
 {
 }
 
+static inline void vtd_accel_pasid_cache_sync(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
 static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
 {
 }
hw/i386/intel_iommu_internal.h +8 -0
index 0141316f83..623dc24760 100644
@@ -615,6 +615,7 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(x)        extract64((x)->val[0], 9, 3)
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
@@ -645,6 +646,7 @@ typedef struct VTDPIOTLBInvInfo {
 #define VTD_PASID_DIR_BITS_MASK       (0x3fffULL)
 #define VTD_PASID_DIR_INDEX(pasid)    (((pasid) >> 6) & VTD_PASID_DIR_BITS_MASK)
 #define VTD_PASID_DIR_FPD             (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TABLE_ENTRY_NUM     (1ULL << 6)
 #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
 #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) & VTD_PASID_TABLE_BITS_MASK)
 #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable */
@@ -710,6 +712,7 @@ typedef struct VTDHostIOMMUDevice {
     PCIBus *bus;
     uint8_t devfn;
     HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
 } VTDHostIOMMUDevice;
 
 /*
@@ -767,6 +770,11 @@ static inline int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
     return memcmp(p1, p2, sizeof(*p1));
 }
 
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce) + 7);
+}
+
 int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
                                   VTDPASIDDirEntry *pdire);
 int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
hw/i386/intel_iommu.c +3 -0
index b50c556c40..e1e32959d3 100644
@@ -3181,6 +3181,8 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
     g_hash_table_foreach(s->vtd_address_spaces, vtd_pasid_cache_sync_locked,
                          pc_info);
     vtd_iommu_unlock(s);
+
+    vtd_accel_pasid_cache_sync(s, pc_info);
 }
 
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4751,6 +4753,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     vtd_hiod->devfn = (uint8_t)devfn;
     vtd_hiod->iommu_state = s;
     vtd_hiod->hiod = hiod;
+    QLIST_INIT(&vtd_hiod->pasid_cache_list);
 
     if (!vtd_check_hiod(s, vtd_hiod, errp)) {
         g_free(vtd_hiod);
hw/i386/intel_iommu_accel.c +156 -0

162 lines not on this page · raw message

Signature · 1 line
2.47.3
#22 lore raw

Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 19 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Quoted 8 lines

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Quoted 277 lines
---
  hw/i386/intel_iommu_accel.h    |  13 +++
  hw/i386/intel_iommu_internal.h |   8 ++
  hw/i386/intel_iommu.c          |   3 +
  hw/i386/intel_iommu_accel.c    | 156 +++++++++++++++++++++++++++++++++
  4 files changed, 180 insertions(+)
hw/i386/intel_iommu_accel.h +13 -0
index e5f0b077b4..c9b1823745 100644
@@ -12,6 +12,13 @@
  #define HW_I386_INTEL_IOMMU_ACCEL_H
  #include CONFIG_DEVICES
 
+typedef struct VTDAccelPASIDCacheEntry {
+    VTDHostIOMMUDevice *vtd_hiod;
+    VTDPASIDEntry pasid_entry;
+    uint32_t pasid;
+    QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
+} VTDAccelPASIDCacheEntry;
+
  #ifdef CONFIG_VTD_ACCEL
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                            Error **errp);
@@ -20,6 +27,7 @@ bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
  void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                                        uint32_t pasid, hwaddr addr,
                                        uint64_t npages, bool ih);
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
  void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
  #else
  static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -49,6 +57,11 @@ static inline void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
  {
  }
 
+static inline void vtd_accel_pasid_cache_sync(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu_internal.h +8 -0
index 0141316f83..623dc24760 100644
@@ -615,6 +615,7 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_LEGACY_SIZE     16
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(x)        extract64((x)->val[0], 9, 3)
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
  #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
@@ -645,6 +646,7 @@ typedef struct VTDPIOTLBInvInfo {
  #define VTD_PASID_DIR_BITS_MASK       (0x3fffULL)
  #define VTD_PASID_DIR_INDEX(pasid)    (((pasid) >> 6) & VTD_PASID_DIR_BITS_MASK)
  #define VTD_PASID_DIR_FPD             (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TABLE_ENTRY_NUM     (1ULL << 6)
  #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
  #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) & VTD_PASID_TABLE_BITS_MASK)
  #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable */
@@ -710,6 +712,7 @@ typedef struct VTDHostIOMMUDevice {
      PCIBus *bus;
      uint8_t devfn;
      HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
  } VTDHostIOMMUDevice;
 
  /*
@@ -767,6 +770,11 @@ static inline int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
      return memcmp(p1, p2, sizeof(*p1));
  }
 
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce) + 7);
+}
+
  int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
                                    VTDPASIDDirEntry *pdire);
  int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
hw/i386/intel_iommu.c +3 -0
index b50c556c40..e1e32959d3 100644
@@ -3181,6 +3181,8 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
      g_hash_table_foreach(s->vtd_address_spaces, vtd_pasid_cache_sync_locked,
                           pc_info);
      vtd_iommu_unlock(s);
+
+    vtd_accel_pasid_cache_sync(s, pc_info);
  }
 
  static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4751,6 +4753,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
      vtd_hiod->devfn = (uint8_t)devfn;
      vtd_hiod->iommu_state = s;
      vtd_hiod->hiod = hiod;
+    QLIST_INIT(&vtd_hiod->pasid_cache_list);
 
      if (!vtd_check_hiod(s, vtd_hiod, errp)) {
          g_free(vtd_hiod);
hw/i386/intel_iommu_accel.c +156 -0
index 10bdbba632..a66d63b4c8 100644
@@ -259,6 +259,162 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                           vtd_flush_host_piotlb_locked, &piotlb_info);
  }
 
+static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
+                              VTDPASIDEntry *pe)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce;
+
+    QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
+        if (vtd_pce->pasid == pasid) {
+            if (vtd_pasid_entry_compare(pe, &vtd_pce->pasid_entry)) {
+                vtd_pce->pasid_entry = *pe;
+            }
+            return;
+        }
+    }
+
+    vtd_pce = g_malloc0(sizeof(VTDAccelPASIDCacheEntry));
+    vtd_pce->vtd_hiod = vtd_hiod;
+    vtd_pce->pasid = pasid;
+    vtd_pce->pasid_entry = *pe;
+    QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
+}
+
+/*
+ * This function walks over PASID range within [start, end) in a single
+ * PASID table for entries matching @info type/did, then create
+ * VTDAccelPASIDCacheEntry if not exist yet.
+ */
+static void vtd_sm_pasid_table_walk_one(VTDHostIOMMUDevice *vtd_hiod,
+                                        dma_addr_t pt_base, int start, int end,
+                                        VTDPASIDCacheInfo *info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDPASIDEntry pe;
+    int pasid;
+
+    for (pasid = start; pasid < end; pasid++) {
+        if (vtd_get_pe_in_pasid_leaf_table(s, pasid, pt_base, &pe) ||
+            !vtd_pe_present(&pe)) {
+            continue;
+        }
+
+        if ((info->type == VTD_INV_DESC_PASIDC_G_DSI ||
+             info->type == VTD_INV_DESC_PASIDC_G_PASID_SI) &&
+            (info->did != VTD_SM_PASID_ENTRY_DID(&pe))) {
+            /*
+             * VTD_PASID_CACHE_DOMSI and VTD_PASID_CACHE_PASIDSI
+             * requires domain id check. If domain id check fail,
+             * go to next pasid.
+             */
+            continue;
+        }
+
+        vtd_accel_fill_pc(vtd_hiod, pasid, &pe);
+    }
+}
+
+/*
+ * In VT-d scalable mode translation, PASID dir + PASID table is used.
+ * This function aims at looping over a range of PASIDs in the given
+ * two level table to identify the pasid config in guest.
+ */
+static void vtd_sm_pasid_table_walk(VTDHostIOMMUDevice *vtd_hiod,
+                                    dma_addr_t pdt_base, int start, int end,
+                                    VTDPASIDCacheInfo *info)
+{
+    VTDPASIDDirEntry pdire;
+    int pasid = start;
+    int pasid_next;
+    dma_addr_t pt_base;
+
+    while (pasid < end) {
+        pasid_next = (pasid + VTD_PASID_TABLE_ENTRY_NUM) &
+                     ~(VTD_PASID_TABLE_ENTRY_NUM - 1);
+        pasid_next = pasid_next < end ? pasid_next : end;
+
+        if (!vtd_get_pdire_from_pdir_table(pdt_base, pasid, &pdire)
+            && vtd_pdire_present(&pdire)) {
+            pt_base = pdire.val & VTD_PASID_TABLE_BASE_ADDR_MASK;
+            vtd_sm_pasid_table_walk_one(vtd_hiod, pt_base, pasid, pasid_next,
+                                        info);
+        }
+        pasid = pasid_next;
+    }
+}
+
+static void vtd_accel_replay_pasid_bind_for_dev(VTDHostIOMMUDevice *vtd_hiod,
+                                                int start, int end,
+                                                VTDPASIDCacheInfo *pc_info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDContextEntry ce;
+    int dev_max_pasid = 1 << vtd_hiod->hiod->caps.max_pasid_log2;
+
+    if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                  vtd_hiod->devfn, &ce)) {
+        VTDPASIDCacheInfo walk_info = *pc_info;
+        uint32_t ce_max_pasid = vtd_sm_ce_get_pdt_entry_num(&ce) *
+                                VTD_PASID_TABLE_ENTRY_NUM;
+
+        end = MIN(end, MIN(dev_max_pasid, ce_max_pasid));
+
+        vtd_sm_pasid_table_walk(vtd_hiod, VTD_CE_GET_PASID_DIR_TABLE(&ce),
+                                start, end, &walk_info);
+    }
+}
+
+/*
+ * This function replays the guest pasid bindings by walking the two level
+ * guest PASID table. For each valid pasid entry, it creates an entry
+ * VTDAccelPASIDCacheEntry dynamically if not exist yet. This entry holds
+ * info specific to a pasid
+ */
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
+{
+    int start = IOMMU_NO_PASID, end = 1 << s->pasid;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    if (!s->fsts) {
+        return;
+    }
+
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        start = pc_info->pasid;
+        end = pc_info->pasid + 1;
+        /* fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        /*
+         * loop all assigned devices, do domain id check in
+         * vtd_sm_pasid_table_walk_one() after get pasid entry.
+         */
+        break;
+    case VTD_INV_DESC_PASIDC_G_GLOBAL:
+        /* loop all assigned devices */
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /*
+     * Loop all the vtd_hiod instances to sync the "pasid cache" per the
+     * guest pasid configuration.
+     *
+     * VTD translation callback never accesses vtd_hiod and its corresponding
+     * cached pasid entry, so no iommu lock needed here.
+     */
+    g_hash_table_iter_init(&hiod_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&hiod_it, NULL, (void **)&vtd_hiod)) {
+        if (!object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                                 TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+            continue;
+        }
+        vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+    }
+}
+
  static uint64_t vtd_get_host_iommu_quirks(uint32_t type,
                                            void *caps, uint32_t size)
  {
#23 lore raw

RE: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 36 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 19 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Quoted 8 lines

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

Quoted 6 lines

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Will do.

Thanks
Zhenzhong

Quoted 295 lines
Quoted 19 lines
---
  hw/i386/intel_iommu_accel.h    |  13 +++
  hw/i386/intel_iommu_internal.h |   8 ++
  hw/i386/intel_iommu.c          |   3 +
  hw/i386/intel_iommu_accel.c    | 156 +++++++++++++++++++++++++++++++++
  4 files changed, 180 insertions(+)
hw/i386/intel_iommu_accel.h +0 -0
index e5f0b077b4..c9b1823745 100644
@@ -12,6 +12,13 @@
  #define HW_I386_INTEL_IOMMU_ACCEL_H
  #include CONFIG_DEVICES

hw/i386/intel_iommu_accel.h line 23

Quoted 10 lines
hw/i386/intel_iommu_accel.h +7 -0
 
+typedef struct VTDAccelPASIDCacheEntry {
+    VTDHostIOMMUDevice *vtd_hiod;
+    VTDPASIDEntry pasid_entry;
+    uint32_t pasid;
+    QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
+} VTDAccelPASIDCacheEntry;
+
  #ifdef CONFIG_VTD_ACCEL
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace

Quoted 2 lines
hw/i386/intel_iommu_accel.h +0 -0
                            Error **errp);
@@ -20,6 +27,7 @@ bool vtd_propagate_guest_pasid(VTDAddressSpace

*vtd_as, Error **errp);

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace · line 27

Quoted 1 line
hw/i386/intel_iommu_accel.h +0 -0
  void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t

domain_id,

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);

Quoted 3 lines
hw/i386/intel_iommu_accel.h +1 -0
                                        uint32_t pasid, hwaddr addr,
                                        uint64_t npages, bool ih);
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info);

hw/i386/intel_iommu_accel.h static inline void

Quoted 4 lines
hw/i386/intel_iommu_accel.h +0 -0
  void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
  #else
  static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -49,6 +57,11 @@ static inline void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,

Quoted 16 lines
hw/i386/intel_iommu_accel.h +5 -0
  {
  }
 
+static inline void vtd_accel_pasid_cache_sync(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu_internal.h +0 -0
index 0141316f83..623dc24760 100644

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -615,6 +615,7 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_LEGACY_SIZE     16
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(x)        extract64((x)->val[0], 9, 3)
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL |

~VTD_HAW_MASK(aw))

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
  #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
@@ -645,6 +646,7 @@ typedef struct VTDPIOTLBInvInfo {
  #define VTD_PASID_DIR_BITS_MASK       (0x3fffULL)
  #define VTD_PASID_DIR_INDEX(pasid)    (((pasid) >> 6) &

VTD_PASID_DIR_BITS_MASK)

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo {

Quoted 4 lines
hw/i386/intel_iommu_internal.h +1 -0
  #define VTD_PASID_DIR_FPD             (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TABLE_ENTRY_NUM     (1ULL << 6)
  #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
  #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) &

VTD_PASID_TABLE_BITS_MASK)

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

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable

*/

hw/i386/intel_iommu_internal.h static inline int

Quoted 9 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -710,6 +712,7 @@ typedef struct VTDHostIOMMUDevice {
      PCIBus *bus;
      uint8_t devfn;
      HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
  } VTDHostIOMMUDevice;
 
  /*
@@ -767,6 +770,11 @@ static inline int

vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync(IntelIOMMUState

Quoted 16 lines
hw/i386/intel_iommu_internal.h +5 -0
      return memcmp(p1, p2, sizeof(*p1));
  }
 
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce) + 7);
+}
+
  int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
                                    VTDPASIDDirEntry *pdire);
  int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
hw/i386/intel_iommu.c +0 -0
index b50c556c40..e1e32959d3 100644
@@ -3181,6 +3181,8 @@ static void vtd_pasid_cache_sync(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync(IntelIOMMUState · line 3181

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
      g_hash_table_foreach(s->vtd_address_spaces,

vtd_pasid_cache_sync_locked,

hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +2 -0
                           pc_info);
      vtd_iommu_unlock(s);
+
+    vtd_accel_pasid_cache_sync(s, pc_info);
  }
 
  static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4751,6 +4753,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,

void *opaque, int devfn,

hw/i386/intel_iommu_accel.c void

Quoted 12 lines
hw/i386/intel_iommu.c +1 -0
      vtd_hiod->devfn = (uint8_t)devfn;
      vtd_hiod->iommu_state = s;
      vtd_hiod->hiod = hiod;
+    QLIST_INIT(&vtd_hiod->pasid_cache_list);
 
      if (!vtd_check_hiod(s, vtd_hiod, errp)) {
          g_free(vtd_hiod);
hw/i386/intel_iommu_accel.c +0 -0
index 10bdbba632..a66d63b4c8 100644
@@ -259,6 +259,162 @@ void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 78 lines
hw/i386/intel_iommu_accel.c +75 -0
                           vtd_flush_host_piotlb_locked, &piotlb_info);
  }
 
+static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
+                              VTDPASIDEntry *pe)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce;
+
+    QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
+        if (vtd_pce->pasid == pasid) {
+            if (vtd_pasid_entry_compare(pe, &vtd_pce->pasid_entry)) {
+                vtd_pce->pasid_entry = *pe;
+            }
+            return;
+        }
+    }
+
+    vtd_pce = g_malloc0(sizeof(VTDAccelPASIDCacheEntry));
+    vtd_pce->vtd_hiod = vtd_hiod;
+    vtd_pce->pasid = pasid;
+    vtd_pce->pasid_entry = *pe;
+    QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
+}
+
+/*
+ * This function walks over PASID range within [start, end) in a single
+ * PASID table for entries matching @info type/did, then create
+ * VTDAccelPASIDCacheEntry if not exist yet.
+ */
+static void vtd_sm_pasid_table_walk_one(VTDHostIOMMUDevice *vtd_hiod,
+                                        dma_addr_t pt_base, int start, int end,
+                                        VTDPASIDCacheInfo *info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDPASIDEntry pe;
+    int pasid;
+
+    for (pasid = start; pasid < end; pasid++) {
+        if (vtd_get_pe_in_pasid_leaf_table(s, pasid, pt_base, &pe) ||
+            !vtd_pe_present(&pe)) {
+            continue;
+        }
+
+        if ((info->type == VTD_INV_DESC_PASIDC_G_DSI ||
+             info->type == VTD_INV_DESC_PASIDC_G_PASID_SI) &&
+            (info->did != VTD_SM_PASID_ENTRY_DID(&pe))) {
+            /*
+             * VTD_PASID_CACHE_DOMSI and VTD_PASID_CACHE_PASIDSI
+             * requires domain id check. If domain id check fail,
+             * go to next pasid.
+             */
+            continue;
+        }
+
+        vtd_accel_fill_pc(vtd_hiod, pasid, &pe);
+    }
+}
+
+/*
+ * In VT-d scalable mode translation, PASID dir + PASID table is used.
+ * This function aims at looping over a range of PASIDs in the given
+ * two level table to identify the pasid config in guest.
+ */
+static void vtd_sm_pasid_table_walk(VTDHostIOMMUDevice *vtd_hiod,
+                                    dma_addr_t pdt_base, int start, int end,
+                                    VTDPASIDCacheInfo *info)
+{
+    VTDPASIDDirEntry pdire;
+    int pasid = start;
+    int pasid_next;
+    dma_addr_t pt_base;
+
+    while (pasid < end) {
+        pasid_next = (pasid + VTD_PASID_TABLE_ENTRY_NUM) &
+                     ~(VTD_PASID_TABLE_ENTRY_NUM - 1);
+        pasid_next = pasid_next < end ? pasid_next : end;
+
+        if (!vtd_get_pdire_from_pdir_table(pdt_base, pasid, &pdire)

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+            && vtd_pdire_present(&pdire)) {
+            pt_base = pdire.val & VTD_PASID_TABLE_BASE_ADDR_MASK;
+            vtd_sm_pasid_table_walk_one(vtd_hiod, pt_base, pasid, pasid_next,
+                                        info);
+        }
+        pasid = pasid_next;
+    }
+}
+
+static void vtd_accel_replay_pasid_bind_for_dev(VTDHostIOMMUDevice

*vtd_hiod,

Quoted 6 lines
hw/i386/intel_iommu_accel.c +6 -0
+                                                int start, int end,
+                                                VTDPASIDCacheInfo *pc_info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDContextEntry ce;
+    int dev_max_pasid = 1 << vtd_hiod->hiod->caps.max_pasid_log2;

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+
+    if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                  vtd_hiod->devfn, &ce)) {
+        VTDPASIDCacheInfo walk_info = *pc_info;
+        uint32_t ce_max_pasid = vtd_sm_ce_get_pdt_entry_num(&ce) *
+                                VTD_PASID_TABLE_ENTRY_NUM;
+
+        end = MIN(end, MIN(dev_max_pasid, ce_max_pasid));
+
+        vtd_sm_pasid_table_walk(vtd_hiod,

VTD_CE_GET_PASID_DIR_TABLE(&ce),

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 11 lines
hw/i386/intel_iommu_accel.c +11 -0
+                                start, end, &walk_info);
+    }
+}
+
+/*
+ * This function replays the guest pasid bindings by walking the two level
+ * guest PASID table. For each valid pasid entry, it creates an entry
+ * VTDAccelPASIDCacheEntry dynamically if not exist yet. This entry holds
+ * info specific to a pasid
+ */
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info)

Quoted 47 lines
hw/i386/intel_iommu_accel.c +44 -0
+{
+    int start = IOMMU_NO_PASID, end = 1 << s->pasid;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    if (!s->fsts) {
+        return;
+    }
+
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        start = pc_info->pasid;
+        end = pc_info->pasid + 1;
+        /* fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        /*
+         * loop all assigned devices, do domain id check in
+         * vtd_sm_pasid_table_walk_one() after get pasid entry.
+         */
+        break;
+    case VTD_INV_DESC_PASIDC_G_GLOBAL:
+        /* loop all assigned devices */
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /*
+     * Loop all the vtd_hiod instances to sync the "pasid cache" per the
+     * guest pasid configuration.
+     *
+     * VTD translation callback never accesses vtd_hiod and its corresponding
+     * cached pasid entry, so no iommu lock needed here.
+     */
+    g_hash_table_iter_init(&hiod_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&hiod_it, NULL, (void **)&vtd_hiod)) {
+        if (!object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                                 TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+            continue;
+        }
+        vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+    }
+}
+
  static uint64_t vtd_get_host_iommu_quirks(uint32_t type,
                                            void *caps, uint32_t size)
  {
#24 lore raw

RE: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

In reply to Yi Liu
Quoted 36 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 19 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Quoted 8 lines

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

Quoted 6 lines

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Hmm, you mean why not putting VTDAccelPASIDCacheEntry list in VTDAddressSpace? There is explanation above, paste here:

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Thanks
Zhenzhong

Quoted 295 lines
Quoted 19 lines
---
  hw/i386/intel_iommu_accel.h    |  13 +++
  hw/i386/intel_iommu_internal.h |   8 ++
  hw/i386/intel_iommu.c          |   3 +
  hw/i386/intel_iommu_accel.c    | 156 +++++++++++++++++++++++++++++++++
  4 files changed, 180 insertions(+)
hw/i386/intel_iommu_accel.h +0 -0
index e5f0b077b4..c9b1823745 100644
@@ -12,6 +12,13 @@
  #define HW_I386_INTEL_IOMMU_ACCEL_H
  #include CONFIG_DEVICES

hw/i386/intel_iommu_accel.h line 23

Quoted 10 lines
hw/i386/intel_iommu_accel.h +7 -0
 
+typedef struct VTDAccelPASIDCacheEntry {
+    VTDHostIOMMUDevice *vtd_hiod;
+    VTDPASIDEntry pasid_entry;
+    uint32_t pasid;
+    QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
+} VTDAccelPASIDCacheEntry;
+
  #ifdef CONFIG_VTD_ACCEL
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace

Quoted 2 lines
hw/i386/intel_iommu_accel.h +0 -0
                            Error **errp);
@@ -20,6 +27,7 @@ bool vtd_propagate_guest_pasid(VTDAddressSpace

*vtd_as, Error **errp);

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace · line 27

Quoted 1 line
hw/i386/intel_iommu_accel.h +0 -0
  void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t

domain_id,

hw/i386/intel_iommu_accel.h bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);

Quoted 3 lines
hw/i386/intel_iommu_accel.h +1 -0
                                        uint32_t pasid, hwaddr addr,
                                        uint64_t npages, bool ih);
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info);

hw/i386/intel_iommu_accel.h static inline void

Quoted 4 lines
hw/i386/intel_iommu_accel.h +0 -0
  void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
  #else
  static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -49,6 +57,11 @@ static inline void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,

Quoted 16 lines
hw/i386/intel_iommu_accel.h +5 -0
  {
  }
 
+static inline void vtd_accel_pasid_cache_sync(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu_internal.h +0 -0
index 0141316f83..623dc24760 100644

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -615,6 +615,7 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_LEGACY_SIZE     16
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(x)        extract64((x)->val[0], 9, 3)
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL |

~VTD_HAW_MASK(aw))

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
  #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL
@@ -645,6 +646,7 @@ typedef struct VTDPIOTLBInvInfo {
  #define VTD_PASID_DIR_BITS_MASK       (0x3fffULL)
  #define VTD_PASID_DIR_INDEX(pasid)    (((pasid) >> 6) &

VTD_PASID_DIR_BITS_MASK)

hw/i386/intel_iommu_internal.h typedef struct VTDPIOTLBInvInfo {

Quoted 4 lines
hw/i386/intel_iommu_internal.h +1 -0
  #define VTD_PASID_DIR_FPD             (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TABLE_ENTRY_NUM     (1ULL << 6)
  #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
  #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) &

VTD_PASID_TABLE_BITS_MASK)

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

Quoted 1 line
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable

*/

hw/i386/intel_iommu_internal.h static inline int

Quoted 9 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -710,6 +712,7 @@ typedef struct VTDHostIOMMUDevice {
      PCIBus *bus;
      uint8_t devfn;
      HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
  } VTDHostIOMMUDevice;
 
  /*
@@ -767,6 +770,11 @@ static inline int

vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync(IntelIOMMUState

Quoted 16 lines
hw/i386/intel_iommu_internal.h +5 -0
      return memcmp(p1, p2, sizeof(*p1));
  }
 
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce) + 7);
+}
+
  int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
                                    VTDPASIDDirEntry *pdire);
  int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
hw/i386/intel_iommu.c +0 -0
index b50c556c40..e1e32959d3 100644
@@ -3181,6 +3181,8 @@ static void vtd_pasid_cache_sync(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync(IntelIOMMUState · line 3181

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
      g_hash_table_foreach(s->vtd_address_spaces,

vtd_pasid_cache_sync_locked,

hw/i386/intel_iommu.c static bool vtd_dev_set_iommu_device(PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +2 -0
                           pc_info);
      vtd_iommu_unlock(s);
+
+    vtd_accel_pasid_cache_sync(s, pc_info);
  }
 
  static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4751,6 +4753,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus,

void *opaque, int devfn,

hw/i386/intel_iommu_accel.c void

Quoted 12 lines
hw/i386/intel_iommu.c +1 -0
      vtd_hiod->devfn = (uint8_t)devfn;
      vtd_hiod->iommu_state = s;
      vtd_hiod->hiod = hiod;
+    QLIST_INIT(&vtd_hiod->pasid_cache_list);
 
      if (!vtd_check_hiod(s, vtd_hiod, errp)) {
          g_free(vtd_hiod);
hw/i386/intel_iommu_accel.c +0 -0
index 10bdbba632..a66d63b4c8 100644
@@ -259,6 +259,162 @@ void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 78 lines
hw/i386/intel_iommu_accel.c +75 -0
                           vtd_flush_host_piotlb_locked, &piotlb_info);
  }
 
+static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
+                              VTDPASIDEntry *pe)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce;
+
+    QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
+        if (vtd_pce->pasid == pasid) {
+            if (vtd_pasid_entry_compare(pe, &vtd_pce->pasid_entry)) {
+                vtd_pce->pasid_entry = *pe;
+            }
+            return;
+        }
+    }
+
+    vtd_pce = g_malloc0(sizeof(VTDAccelPASIDCacheEntry));
+    vtd_pce->vtd_hiod = vtd_hiod;
+    vtd_pce->pasid = pasid;
+    vtd_pce->pasid_entry = *pe;
+    QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
+}
+
+/*
+ * This function walks over PASID range within [start, end) in a single
+ * PASID table for entries matching @info type/did, then create
+ * VTDAccelPASIDCacheEntry if not exist yet.
+ */
+static void vtd_sm_pasid_table_walk_one(VTDHostIOMMUDevice *vtd_hiod,
+                                        dma_addr_t pt_base, int start, int end,
+                                        VTDPASIDCacheInfo *info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDPASIDEntry pe;
+    int pasid;
+
+    for (pasid = start; pasid < end; pasid++) {
+        if (vtd_get_pe_in_pasid_leaf_table(s, pasid, pt_base, &pe) ||
+            !vtd_pe_present(&pe)) {
+            continue;
+        }
+
+        if ((info->type == VTD_INV_DESC_PASIDC_G_DSI ||
+             info->type == VTD_INV_DESC_PASIDC_G_PASID_SI) &&
+            (info->did != VTD_SM_PASID_ENTRY_DID(&pe))) {
+            /*
+             * VTD_PASID_CACHE_DOMSI and VTD_PASID_CACHE_PASIDSI
+             * requires domain id check. If domain id check fail,
+             * go to next pasid.
+             */
+            continue;
+        }
+
+        vtd_accel_fill_pc(vtd_hiod, pasid, &pe);
+    }
+}
+
+/*
+ * In VT-d scalable mode translation, PASID dir + PASID table is used.
+ * This function aims at looping over a range of PASIDs in the given
+ * two level table to identify the pasid config in guest.
+ */
+static void vtd_sm_pasid_table_walk(VTDHostIOMMUDevice *vtd_hiod,
+                                    dma_addr_t pdt_base, int start, int end,
+                                    VTDPASIDCacheInfo *info)
+{
+    VTDPASIDDirEntry pdire;
+    int pasid = start;
+    int pasid_next;
+    dma_addr_t pt_base;
+
+    while (pasid < end) {
+        pasid_next = (pasid + VTD_PASID_TABLE_ENTRY_NUM) &
+                     ~(VTD_PASID_TABLE_ENTRY_NUM - 1);
+        pasid_next = pasid_next < end ? pasid_next : end;
+
+        if (!vtd_get_pdire_from_pdir_table(pdt_base, pasid, &pdire)

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+            && vtd_pdire_present(&pdire)) {
+            pt_base = pdire.val & VTD_PASID_TABLE_BASE_ADDR_MASK;
+            vtd_sm_pasid_table_walk_one(vtd_hiod, pt_base, pasid, pasid_next,
+                                        info);
+        }
+        pasid = pasid_next;
+    }
+}
+
+static void vtd_accel_replay_pasid_bind_for_dev(VTDHostIOMMUDevice

*vtd_hiod,

Quoted 6 lines
hw/i386/intel_iommu_accel.c +6 -0
+                                                int start, int end,
+                                                VTDPASIDCacheInfo *pc_info)
+{
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    VTDContextEntry ce;
+    int dev_max_pasid = 1 << vtd_hiod->hiod->caps.max_pasid_log2;

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+
+    if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                  vtd_hiod->devfn, &ce)) {
+        VTDPASIDCacheInfo walk_info = *pc_info;
+        uint32_t ce_max_pasid = vtd_sm_ce_get_pdt_entry_num(&ce) *
+                                VTD_PASID_TABLE_ENTRY_NUM;
+
+        end = MIN(end, MIN(dev_max_pasid, ce_max_pasid));
+
+        vtd_sm_pasid_table_walk(vtd_hiod,

VTD_CE_GET_PASID_DIR_TABLE(&ce),

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 11 lines
hw/i386/intel_iommu_accel.c +11 -0
+                                start, end, &walk_info);
+    }
+}
+
+/*
+ * This function replays the guest pasid bindings by walking the two level
+ * guest PASID table. For each valid pasid entry, it creates an entry
+ * VTDAccelPASIDCacheEntry dynamically if not exist yet. This entry holds
+ * info specific to a pasid
+ */
+void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info)

Quoted 47 lines
hw/i386/intel_iommu_accel.c +44 -0
+{
+    int start = IOMMU_NO_PASID, end = 1 << s->pasid;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    if (!s->fsts) {
+        return;
+    }
+
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        start = pc_info->pasid;
+        end = pc_info->pasid + 1;
+        /* fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        /*
+         * loop all assigned devices, do domain id check in
+         * vtd_sm_pasid_table_walk_one() after get pasid entry.
+         */
+        break;
+    case VTD_INV_DESC_PASIDC_G_GLOBAL:
+        /* loop all assigned devices */
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /*
+     * Loop all the vtd_hiod instances to sync the "pasid cache" per the
+     * guest pasid configuration.
+     *
+     * VTD translation callback never accesses vtd_hiod and its corresponding
+     * cached pasid entry, so no iommu lock needed here.
+     */
+    g_hash_table_iter_init(&hiod_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&hiod_it, NULL, (void **)&vtd_hiod)) {
+        if (!object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                                 TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+            continue;
+        }
+        vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+    }
+}
+
  static uint64_t vtd_get_host_iommu_quirks(uint32_t type,
                                            void *caps, uint32_t size)
  {
#25 lore raw

Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 43 lines · Zhenzhong
Quoted 36 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 19 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Quoted 8 lines

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

Quoted 6 lines

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Quoted 5 lines

Hmm, you mean why not putting VTDAccelPASIDCacheEntry list in VTDAddressSpace? There is explanation above, paste here:

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

hmmm. code isolation is a good reason. Is there any other reason?

#26 lore raw

RE: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 54 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition

for

Quoted 12 lines
Quoted 12 lines

pc_inv_dsc request

Quoted 9 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in

intel_iommu_accel.c,

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

Quoted 8 lines
Quoted 6 lines

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Hmm, you mean why not putting VTDAccelPASIDCacheEntry list in

Quoted 8 lines

VTDAddressSpace?

Quoted 4 lines

There is explanation above, paste here:

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

hmmm. code isolation is a good reason. Is there any other reason?

Could you hint?:)
The first four paragraphs in commit log showed all rationales I can think of about not using VTDAddressSpace.

Thanks
Zhenzhong

#27 lore raw

Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 61 lines · Zhenzhong
Quoted 61 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 09/15] intel_iommu_accel: Handle PASID entry addition

for

Quoted 12 lines
Quoted 12 lines

pc_inv_dsc request

Quoted 9 lines · Zhenzhong Duan

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt and to record the current bound nested hwpt. It's also designed to support IOMMU_NO_PASID.

VTDAccelPASIDCacheEntry is designed to only be used in

intel_iommu_accel.c,

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

When guest creates new PASID entries, QEMU will capture the pc_inv_dsc (pasid cache invalidation) request, walk through each pasid in each passthrough device for valid pasid entries, create a new VTDAccelPASIDCacheEntry if not existing yet.

IOMMU_NO_PASID of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for IOMMU_NO_PASID, we have both VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry.

Quoted 8 lines
Quoted 6 lines

The implementation LGTM. But I got a question to ask here. VTDAPASIDCacheEntry is cached in VTDAddressSpace, while VTDAccelPASIDCacheEntry is cached in VTDHostIOMMUDevice. A natural question is why usingVTDHostIOMMUDevice instead of VTDAddressSpace. I think it might be valuable to mark the reason of this choice. This would help maintaining it in future as we might forgot the reason. :)

Hmm, you mean why not putting VTDAccelPASIDCacheEntry list in

VTDAddressSpace?

Quoted 4 lines

There is explanation above, paste here:

VTDAccelPASIDCacheEntry is designed to only be used in intel_iommu_accel.c, similarly VTDPASIDCacheEntry should only be used in hw/i386/intel_iommu.c

Quoted 5 lines
Quoted 1 line

hmmm. code isolation is a good reason. Is there any other reason?

Could you hint?:)
The first four paragraphs in commit log showed all rationales I can think of about not using VTDAddressSpace.

TBH. I don't have any hint. When reading this patch, I tried to compare the four objects. I view VTDAPASIDCacheEntry and VTDAccelPASIDCacheEntry as they are per-PASID. But VTDAddressSpace is
per-bdf/pasid, while VTDHostIOMMUDevice is per-bdf. That's why I got
above ask.

never mind, I think code isolation is enough. :)

Regards,
Yi Liu

#28 lore raw

PATCH v5 10/15 intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

In reply to Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
 hw/i386/intel_iommu_accel.c | 80 +++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +80 -0
index a66d63b4c8..82bfbdf484 100644
@@ -16,6 +16,28 @@
 #include "hw/pci/pci_bus.h"
 #include "trace.h"
 
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,
+                                      VTDPASIDEntry *pe)
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->dmar_enabled || !s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                   vtd_hiod->devfn, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+
 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                           Error **errp)
 {
@@ -280,6 +302,57 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
     QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
 }
 
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    QLIST_REMOVE(vtd_pce, next);
+    g_free(vtd_pce);
+}
+
+static void
+vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
+                                     VTDPASIDCacheInfo *pc_info)
+{
+    VTDPASIDEntry pe;
+    uint16_t did;
+
+    /*
+     * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI require
+     * DID check. If DID doesn't match the value in cache or memory, then
+     * it's not a pasid entry we want to invalidate.
+     */
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        if (pc_info->pasid != vtd_pce->pasid) {
+            return;
+        }
+        /* Fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        did = VTD_SM_PASID_ENTRY_DID(&vtd_pce->pasid_entry);
+        if (pc_info->did != did) {
+            return;
+        }
+    }
+
+    if (vtd_hiod_get_pe_from_pasid(vtd_pce, &pe)) {
+        /*
+         * No valid pasid entry in guest memory. e.g. pasid entry was modified
+         * to be either all-zero or non-present. Either case means existing
+         * pasid cache should be invalidated.
+         */
+        vtd_accel_delete_pc(vtd_pce);
+    }
+}
+
+static void vtd_accel_pasid_cache_invalidate(VTDHostIOMMUDevice *vtd_hiod,
+                                             VTDPASIDCacheInfo *pc_info)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce, *next;
+
+    QLIST_FOREACH_SAFE(vtd_pce, &vtd_hiod->pasid_cache_list, next, next) {
+        vtd_accel_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
 /*
  * This function walks over PASID range within [start, end) in a single
  * PASID table for entries matching @info type/did, then create
@@ -411,6 +484,13 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
                                  TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
             continue;
         }
+
+        /*
+         * PASID entry removal is handled before addition intentionally,
+         * because it's unnecessary to iterate on an entry that will be
+         * removed.
+         */
+        vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);
         vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
     }
 }
Signature · 1 line
2.47.3
#29 lore raw

Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 17 lines · Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
  hw/i386/intel_iommu_accel.c | 80 +++++++++++++++++++++++++++++++++++++
  1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +0 -0
index a66d63b4c8..82bfbdf484 100644

hw/i386/intel_iommu_accel.c line 19

Quoted 4 lines
hw/i386/intel_iommu_accel.c +1 -0
@@ -16,6 +16,28 @@
  #include "hw/pci/pci_bus.h"
  #include "trace.h"
 
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,

this helper is tricky. vtd_pce already has a pe cached, then why need to do this? I think it's better to have a helper receives s, devfn, and pasid instead of accepting vtd_pce. btw. I think the intel_iommu.c should have such a helper. is it?

Quoted 83 lines
hw/i386/intel_iommu_accel.c +72 -0
+                                      VTDPASIDEntry *pe)
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->dmar_enabled || !s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                   vtd_hiod->devfn, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                            Error **errp)
  {
@@ -280,6 +302,57 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
      QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
  }
 
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    QLIST_REMOVE(vtd_pce, next);
+    g_free(vtd_pce);
+}
+
+static void
+vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
+                                     VTDPASIDCacheInfo *pc_info)
+{
+    VTDPASIDEntry pe;
+    uint16_t did;
+
+    /*
+     * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI require
+     * DID check. If DID doesn't match the value in cache or memory, then
+     * it's not a pasid entry we want to invalidate.
+     */
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        if (pc_info->pasid != vtd_pce->pasid) {
+            return;
+        }
+        /* Fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        did = VTD_SM_PASID_ENTRY_DID(&vtd_pce->pasid_entry);
+        if (pc_info->did != did) {
+            return;
+        }
+    }
+
+    if (vtd_hiod_get_pe_from_pasid(vtd_pce, &pe)) {
+        /*
+         * No valid pasid entry in guest memory. e.g. pasid entry was modified
+         * to be either all-zero or non-present. Either case means existing
+         * pasid cache should be invalidated.
+         */
+        vtd_accel_delete_pc(vtd_pce);
+    }
+}
+
+static void vtd_accel_pasid_cache_invalidate(VTDHostIOMMUDevice *vtd_hiod,
+                                             VTDPASIDCacheInfo *pc_info)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce, *next;
+
+    QLIST_FOREACH_SAFE(vtd_pce, &vtd_hiod->pasid_cache_list, next, next) {
+        vtd_accel_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
  /*
   * This function walks over PASID range within [start, end) in a single
   * PASID table for entries matching @info type/did, then create

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info) · line 491

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
@@ -411,6 +484,13 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
                                   TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
              continue;
          }
+
+        /*
+         * PASID entry removal is handled before addition intentionally,
+         * because it's unnecessary to iterate on an entry that will be
+         * removed.

how about below? :)

/*
* The replay path inevitably needs to iterate through existing
* PASID cache entries. Since cached PASID entries that are marked
* for removal don't need to be iterated, we intentionally handle
* removals before additions to optimize the replay process. */

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
+         */
+        vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);
          vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
      }
  }
#30 lore raw

RE: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 29 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 10 lines · Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
  hw/i386/intel_iommu_accel.c | 80

+++++++++++++++++++++++++++++++++++++

Quoted 10 lines
  1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +0 -0
index a66d63b4c8..82bfbdf484 100644
@@ -16,6 +16,28 @@
  #include "hw/pci/pci_bus.h"
  #include "trace.h"
 
Quoted 4 lines

hw/i386/intel_iommu_accel.c line 19

Quoted 1 line
hw/i386/intel_iommu_accel.c +1 -0
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,

this helper is tricky. vtd_pce already has a pe cached, then why need to do this?

It's used on pc_inv_dsc handler path, vtd_pce caches old pe, we need to get refreshed one in memory.

Quoted 3 lines

I think it's better to have a helper receives s, devfn, and pasid instead of accepting vtd_pce. btw. I think the intel_iommu.c should have such a helper. is it?

In intel_iommu.c, there is a helper for vtd_as:

static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
VTDPASIDEntry *pe)

so in intel_iommu_accel.c, vtd_hiod_get_pe_from_pasid() is added for vtd_pce.

Thanks
Zhenzhong

Quoted 113 lines
Quoted 13 lines
hw/i386/intel_iommu_accel.c +12 -0
+                                      VTDPASIDEntry *pe)
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->dmar_enabled || !s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+

hw/i386/intel_iommu_accel.c

Quoted 10 lines
hw/i386/intel_iommu_accel.c +9 -0
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                   vtd_hiod->devfn, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
                            Error **errp)
  {
@@ -280,6 +302,57 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice

*vtd_hiod, uint32_t pasid,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
      QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
  }
 
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    QLIST_REMOVE(vtd_pce, next);
+    g_free(vtd_pce);
+}

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+
+static void
+vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
+                                     VTDPASIDCacheInfo *pc_info)
+{
+    VTDPASIDEntry pe;
+    uint16_t did;
+
+    /*
+     * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI

require

Quoted 17 lines
hw/i386/intel_iommu_accel.c +17 -0
+     * DID check. If DID doesn't match the value in cache or memory, then
+     * it's not a pasid entry we want to invalidate.
+     */
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        if (pc_info->pasid != vtd_pce->pasid) {
+            return;
+        }
+        /* Fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        did = VTD_SM_PASID_ENTRY_DID(&vtd_pce->pasid_entry);
+        if (pc_info->did != did) {
+            return;
+        }
+    }
+
+    if (vtd_hiod_get_pe_from_pasid(vtd_pce, &pe)) {

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+        /*
+         * No valid pasid entry in guest memory. e.g. pasid entry was modified
+         * to be either all-zero or non-present. Either case means existing
+         * pasid cache should be invalidated.
+         */
+        vtd_accel_delete_pc(vtd_pce);
+    }
+}
+
+static void vtd_accel_pasid_cache_invalidate(VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState

Quoted 13 lines
hw/i386/intel_iommu_accel.c +9 -0
+                                             VTDPASIDCacheInfo *pc_info)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce, *next;
+
+    QLIST_FOREACH_SAFE(vtd_pce, &vtd_hiod->pasid_cache_list, next, next) {
+        vtd_accel_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
  /*
   * This function walks over PASID range within [start, end) in a single
   * PASID table for entries matching @info type/did, then create
@@ -411,6 +484,13 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info) · line 491

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
                                   TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
              continue;
          }
+
+        /*
+         * PASID entry removal is handled before addition intentionally,
+         * because it's unnecessary to iterate on an entry that will be
+         * removed.

how about below? :)

/*
* The replay path inevitably needs to iterate through existing
* PASID cache entries. Since cached PASID entries that are marked
* for removal don't need to be iterated, we intentionally handle
* removals before additions to optimize the replay process. */

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
+         */
+        vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);
          vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
      }
  }
#31 lore raw

Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 40 lines · Zhenzhong
Quoted 29 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 10 lines · Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
   hw/i386/intel_iommu_accel.c | 80

+++++++++++++++++++++++++++++++++++++

Quoted 10 lines
   1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +0 -0
index a66d63b4c8..82bfbdf484 100644
@@ -16,6 +16,28 @@
   #include "hw/pci/pci_bus.h"
   #include "trace.h"
 
Quoted 4 lines

hw/i386/intel_iommu_accel.c line 19

Quoted 1 line
hw/i386/intel_iommu_accel.c +1 -0
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,

this helper is tricky. vtd_pce already has a pe cached, then why need to do this?

It's used on pc_inv_dsc handler path, vtd_pce caches old pe, we need to get refreshed one in memory.

Quoted 3 lines

I think it's better to have a helper receives s, devfn, and pasid instead of accepting vtd_pce. btw. I think the intel_iommu.c should have such a helper. is it?

Quoted 7 lines

In intel_iommu.c, there is a helper for vtd_as:

static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
VTDPASIDEntry *pe)

so in intel_iommu_accel.c, vtd_hiod_get_pe_from_pasid() is added for vtd_pce.

got it. VTDAddressSpace also cached a pe, with a closer read, it's tricky as well. Could you refactor a bit? e.g. avoid using the VTDAddressSpace as an input.

Quoted 115 lines
Quoted 114 lines
Quoted 14 lines
hw/i386/intel_iommu_accel.c +12 -0
+                                      VTDPASIDEntry *pe)
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->dmar_enabled || !s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+

hw/i386/intel_iommu_accel.c

Quoted 10 lines
hw/i386/intel_iommu_accel.c +9 -0
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_hiod->bus),
+                                   vtd_hiod->devfn, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+
   bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
                             Error **errp)
   {
@@ -280,6 +302,57 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice

*vtd_hiod, uint32_t pasid,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
       QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
   }
 
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    QLIST_REMOVE(vtd_pce, next);
+    g_free(vtd_pce);
+}

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+
+static void
+vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
+                                     VTDPASIDCacheInfo *pc_info)
+{
+    VTDPASIDEntry pe;
+    uint16_t did;
+
+    /*
+     * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI

require

Quoted 17 lines
hw/i386/intel_iommu_accel.c +17 -0
+     * DID check. If DID doesn't match the value in cache or memory, then
+     * it's not a pasid entry we want to invalidate.
+     */
+    switch (pc_info->type) {
+    case VTD_INV_DESC_PASIDC_G_PASID_SI:
+        if (pc_info->pasid != vtd_pce->pasid) {
+            return;
+        }
+        /* Fall through */
+    case VTD_INV_DESC_PASIDC_G_DSI:
+        did = VTD_SM_PASID_ENTRY_DID(&vtd_pce->pasid_entry);
+        if (pc_info->did != did) {
+            return;
+        }
+    }
+
+    if (vtd_hiod_get_pe_from_pasid(vtd_pce, &pe)) {

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+        /*
+         * No valid pasid entry in guest memory. e.g. pasid entry was modified
+         * to be either all-zero or non-present. Either case means existing
+         * pasid cache should be invalidated.
+         */
+        vtd_accel_delete_pc(vtd_pce);
+    }
+}
+
+static void vtd_accel_pasid_cache_invalidate(VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState

Quoted 13 lines
hw/i386/intel_iommu_accel.c +9 -0
+                                             VTDPASIDCacheInfo *pc_info)
+{
+    VTDAccelPASIDCacheEntry *vtd_pce, *next;
+
+    QLIST_FOREACH_SAFE(vtd_pce, &vtd_hiod->pasid_cache_list, next, next) {
+        vtd_accel_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
   /*
    * This function walks over PASID range within [start, end) in a single
    * PASID table for entries matching @info type/did, then create
@@ -411,6 +484,13 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info) · line 491

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
                                    TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
               continue;
           }
+
+        /*
+         * PASID entry removal is handled before addition intentionally,
+         * because it's unnecessary to iterate on an entry that will be
+         * removed.

how about below? :)

/*
* The replay path inevitably needs to iterate through existing
* PASID cache entries. Since cached PASID entries that are marked
* for removal don't need to be iterated, we intentionally handle
* removals before additions to optimize the replay process. */

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
+         */
+        vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);
           vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
       }
   }
#32 lore raw

RE: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 55 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal

for

Quoted 24 lines
Quoted 24 lines

pc_inv_dsc request

Quoted 10 lines · Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
   hw/i386/intel_iommu_accel.c | 80

+++++++++++++++++++++++++++++++++++++

Quoted 10 lines
   1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +0 -0
index a66d63b4c8..82bfbdf484 100644
@@ -16,6 +16,28 @@
   #include "hw/pci/pci_bus.h"
   #include "trace.h"
 
Quoted 6 lines
Quoted 4 lines

hw/i386/intel_iommu_accel.c line 19

Quoted 1 line
hw/i386/intel_iommu_accel.c +1 -0
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,

this helper is tricky. vtd_pce already has a pe cached, then why need to do this?

It's used on pc_inv_dsc handler path, vtd_pce caches old pe, we need to get

refreshed one in memory.

Quoted 10 lines
Quoted 4 lines

I think it's better to have a helper receives s, devfn, and pasid instead of accepting vtd_pce. btw. I think the intel_iommu.c should have such a helper. is it?

In intel_iommu.c, there is a helper for vtd_as:

static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
VTDPASIDEntry *pe)

Quoted 6 lines
Quoted 2 lines

so in intel_iommu_accel.c, vtd_hiod_get_pe_from_pasid() is added for vtd_pce.

got it. VTDAddressSpace also cached a pe, with a closer read, it's tricky as well. Could you refactor a bit? e.g. avoid using the VTDAddressSpace as an input.

Added as a preparing patch, let me know if any misunderstandings:

commit 31fc6e409f285a2a93f09f99fd271d4b01d87c53 (HEAD)

Author: Zhenzhong Duan <[email protected]>
Date:   Fri May 22 02:14:30 2026 -0400

intel_iommu: make vtd_dev_get_pe_from_pasid() more generic

Refactor vtd_dev_get_pe_from_pasid() to take individual parameters (IntelIOMMUState, PCIBus, devfn, pasid) instead of VTDAddressSpace.

This makes the function more reusable and prepares it for export to be used in the acceleration path. Remove inline qualifier as the function will be exported in a subsequent patch.

No functional changes intended.

hw/i386/intel_iommu.c +7 -7
index cf275b496e..aa27187e02 100644
@@ -3113,10 +3113,10 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
     return true;
 }
 
-static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
-                                            VTDPASIDEntry *pe)
+static int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus,
+                                     uint8_t devfn, uint32_t pasid,
+                                     VTDPASIDEntry *pe)
 {
-    IntelIOMMUState *s = vtd_as->iommu_state;
     VTDContextEntry ce;
     int ret;
 
@@ -3124,13 +3124,12 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
         return -VTD_FR_RTADDR_INV_TTM;
     }
 
-    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), vtd_as->devfn,
-                                   &ce);
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(bus), devfn, &ce);
     if (ret) {
         return ret;
     }
 
-    return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
 }
 
 static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
@@ -3151,7 +3150,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     const char *err_prefix = "Attaching to HWPT failed: ";
     Error *local_err = NULL;
 
-    if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
+    if (vtd_dev_get_pe_from_pasid(vtd_as->iommu_state, vtd_as->bus,
+                                  vtd_as->devfn, vtd_as->pasid, &pe)) {
         if (!pc_entry->valid) {
             return;
         }
#33 lore raw

Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 55 lines · Zhenzhong
Quoted 55 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal

for

Quoted 24 lines
Quoted 24 lines

pc_inv_dsc request

Quoted 10 lines · Zhenzhong Duan

When guest deletes PASID entries, QEMU will capture the pasid cache invalidation request, walk through pasid_cache_list in each passthrough device to find stale VTDAccelPASIDCacheEntry and delete them.

---
    hw/i386/intel_iommu_accel.c | 80

+++++++++++++++++++++++++++++++++++++

Quoted 10 lines
    1 file changed, 80 insertions(+)
hw/i386/intel_iommu_accel.c +0 -0
index a66d63b4c8..82bfbdf484 100644
@@ -16,6 +16,28 @@
    #include "hw/pci/pci_bus.h"
    #include "trace.h"
 
Quoted 6 lines
Quoted 4 lines

hw/i386/intel_iommu_accel.c line 19

Quoted 1 line
hw/i386/intel_iommu_accel.c +1 -0
+static int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,

this helper is tricky. vtd_pce already has a pe cached, then why need to do this?

It's used on pc_inv_dsc handler path, vtd_pce caches old pe, we need to get

refreshed one in memory.

Quoted 10 lines
Quoted 4 lines

I think it's better to have a helper receives s, devfn, and pasid instead of accepting vtd_pce. btw. I think the intel_iommu.c should have such a helper. is it?

In intel_iommu.c, there is a helper for vtd_as:

static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
VTDPASIDEntry *pe)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

so in intel_iommu_accel.c, vtd_hiod_get_pe_from_pasid() is added for vtd_pce.

got it. VTDAddressSpace also cached a pe, with a closer read, it's tricky as well. Could you refactor a bit? e.g. avoid using the VTDAddressSpace as an input.

Added as a preparing patch, let me know if any misunderstandings:

yes. this is what I meant.

Quoted 62 lines

commit 31fc6e409f285a2a93f09f99fd271d4b01d87c53 (HEAD)

Author: Zhenzhong Duan <[email protected]>
Date:   Fri May 22 02:14:30 2026 -0400

intel_iommu: make vtd_dev_get_pe_from_pasid() more generic

Refactor vtd_dev_get_pe_from_pasid() to take individual parameters (IntelIOMMUState, PCIBus, devfn, pasid) instead of VTDAddressSpace.

This makes the function more reusable and prepares it for export to be used in the acceleration path. Remove inline qualifier as the function will be exported in a subsequent patch.

No functional changes intended.

hw/i386/intel_iommu.c +7 -7
index cf275b496e..aa27187e02 100644
@@ -3113,10 +3113,10 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
      return true;
  }
 
-static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
-                                            VTDPASIDEntry *pe)
+static int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus,
+                                     uint8_t devfn, uint32_t pasid,
+                                     VTDPASIDEntry *pe)
  {
-    IntelIOMMUState *s = vtd_as->iommu_state;
      VTDContextEntry ce;
      int ret;
 
@@ -3124,13 +3124,12 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
          return -VTD_FR_RTADDR_INV_TTM;
      }
 
-    ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), vtd_as->devfn,
-                                   &ce);
+    ret = vtd_dev_to_context_entry(s, pci_bus_num(bus), devfn, &ce);
      if (ret) {
          return ret;
      }
 
-    return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
  }
 
  static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
@@ -3151,7 +3150,8 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
      const char *err_prefix = "Attaching to HWPT failed: ";
      Error *local_err = NULL;
 
-    if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
+    if (vtd_dev_get_pe_from_pasid(vtd_as->iommu_state, vtd_as->bus,
+                                  vtd_as->devfn, vtd_as->pasid, &pe)) {
          if (!pc_entry->valid) {
              return;
          }
#34 lore raw

RE: [PATCH v5 10/15] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

In reply to Yi Liu
Quoted 11 lines

Review void vtd_accel_pasid_cache_sync(IntelIOMMUState

Quoted 1 line
(quoted diff) +0 -0
@@ -411,6 +484,13 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info) · line 491

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
                                   TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
              continue;
          }
+
+        /*
+         * PASID entry removal is handled before addition intentionally,
+         * because it's unnecessary to iterate on an entry that will be
+         * removed.
Quoted 8 lines

how about below? :)

/*
* The replay path inevitably needs to iterate through existing
* PASID cache entries. Since cached PASID entries that are marked
* for removal don't need to be iterated, we intentionally handle
* removals before additions to optimize the replay process. */

Good, will do.

Thanks
Zhenzhong

#35 lore raw

PATCH v5 11/15 intel_iommu_accel: Bypass PASID entry addition for just deleted entry

In reply to Zhenzhong Duan

For VTD_INV_DESC_PASIDC_G_PASID_SI typed pc_inv_dsc invalidation, if an pasid entry is just removed, it can never be a new entry to add. So calling vtd_replay_pasid_bind_for_dev() is unnecessary.

Introduce a new field accel_pce_deleted in VTDPASIDCacheInfo to mark this case and to do the bypassing.

---
 hw/i386/intel_iommu_internal.h |  1 +
 hw/i386/intel_iommu_accel.c    | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 623dc24760..2c716c5297 100644
@@ -630,6 +630,7 @@ typedef struct VTDPASIDCacheInfo {
     uint8_t type;
     uint16_t did;
     uint32_t pasid;
+    bool accel_pce_deleted;
 } VTDPASIDCacheInfo;
 
 typedef struct VTDPIOTLBInvInfo {
hw/i386/intel_iommu_accel.c +13 -3
index 82bfbdf484..07a1e41a95 100644
@@ -302,10 +302,15 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
     QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
 }
 
-static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce,
+                                VTDPASIDCacheInfo *pc_info)
 {
     QLIST_REMOVE(vtd_pce, next);
     g_free(vtd_pce);
+
+    if (pc_info->type == VTD_INV_DESC_PASIDC_G_PASID_SI) {
+        pc_info->accel_pce_deleted = true;
+    }
 }
 
 static void
@@ -339,7 +344,7 @@ vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
          * to be either all-zero or non-present. Either case means existing
          * pasid cache should be invalidated.
          */
-        vtd_accel_delete_pc(vtd_pce);
+        vtd_accel_delete_pc(vtd_pce, pc_info);
     }
 }
 
@@ -491,7 +496,12 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
          * removed.
          */
         vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);
-        vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+
+        if (pc_info->accel_pce_deleted) {
+            pc_info->accel_pce_deleted = false;
+        } else {
+            vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+        }
     }
 }
 
Signature · 1 line
2.47.3
Reviewed-by on v5 11/15
#36 lore raw

Re: [PATCH v5 11/15] intel_iommu_accel: Bypass PASID entry addition for just deleted entry

Quoted 61 lines · Zhenzhong Duan

For VTD_INV_DESC_PASIDC_G_PASID_SI typed pc_inv_dsc invalidation, if an pasid entry is just removed, it can never be a new entry to add. So calling vtd_replay_pasid_bind_for_dev() is unnecessary.

Introduce a new field accel_pce_deleted in VTDPASIDCacheInfo to mark this case and to do the bypassing.

---
  hw/i386/intel_iommu_internal.h |  1 +
  hw/i386/intel_iommu_accel.c    | 16 +++++++++++++---
  2 files changed, 14 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 623dc24760..2c716c5297 100644
@@ -630,6 +630,7 @@ typedef struct VTDPASIDCacheInfo {
      uint8_t type;
      uint16_t did;
      uint32_t pasid;
+    bool accel_pce_deleted;
  } VTDPASIDCacheInfo;
 
  typedef struct VTDPIOTLBInvInfo {
hw/i386/intel_iommu_accel.c +7 -2
index 82bfbdf484..07a1e41a95 100644
@@ -302,10 +302,15 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
      QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
  }
 
-static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce)
+static void vtd_accel_delete_pc(VTDAccelPASIDCacheEntry *vtd_pce,
+                                VTDPASIDCacheInfo *pc_info)
  {
      QLIST_REMOVE(vtd_pce, next);
      g_free(vtd_pce);
+
+    if (pc_info->type == VTD_INV_DESC_PASIDC_G_PASID_SI) {
+        pc_info->accel_pce_deleted = true;
+    }
  }
 
  static void
@@ -339,7 +344,7 @@ vtd_accel_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
           * to be either all-zero or non-present. Either case means existing
           * pasid cache should be invalidated.
           */
-        vtd_accel_delete_pc(vtd_pce);
+        vtd_accel_delete_pc(vtd_pce, pc_info);
      }
  }
 
@@ -491,7 +496,12 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
           * removed.
           */
          vtd_accel_pasid_cache_invalidate(vtd_hiod, pc_info);

hw/i386/intel_iommu_accel.c void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info) · line 507

Quoted 10 lines
hw/i386/intel_iommu_accel.c +6 -1
-        vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+
+        if (pc_info->accel_pce_deleted) {
+            pc_info->accel_pce_deleted = false;
+        } else {
+            vtd_accel_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+        }
      }
  }
 

LGTM.

#37 lore raw

PATCH v5 12/15 intel_iommu_accel: Handle PASID entry removal for system reset

In reply to Zhenzhong Duan

When system level reset, DMA translation is turned off, all PASID entries become stale and should be deleted.

vtd_hiod list is never accessed without BQL, so no need to guard with iommu lock.

---
 hw/i386/intel_iommu_accel.h |  5 +++++
 hw/i386/intel_iommu.c       |  2 ++
 hw/i386/intel_iommu_accel.c | 13 +++++++++++++
 3 files changed, 20 insertions(+)
hw/i386/intel_iommu_accel.h +5 -0
index c9b1823745..a2226b28b6 100644
@@ -28,6 +28,7 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                                       uint32_t pasid, hwaddr addr,
                                       uint64_t npages, bool ih);
 void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
+void vtd_accel_pasid_cache_reset(IntelIOMMUState *s);
 void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
 #else
 static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -62,6 +63,10 @@ static inline void vtd_accel_pasid_cache_sync(IntelIOMMUState *s,
 {
 }
 
+static inline void vtd_accel_pasid_cache_reset(IntelIOMMUState *s)
+{
+}
+
 static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
 {
 }
hw/i386/intel_iommu.c +2 -0
index e1e32959d3..ac1b5f4d79 100644
@@ -391,6 +391,8 @@ static void vtd_reset_caches(IntelIOMMUState *s)
     vtd_reset_context_cache_locked(s);
     vtd_pasid_cache_reset_locked(s);
     vtd_iommu_unlock(s);
+
+    vtd_accel_pasid_cache_reset(s);
 }
 
 static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
hw/i386/intel_iommu_accel.c +13 -0
index 07a1e41a95..b1c1cd719a 100644
@@ -505,6 +505,19 @@ void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
     }
 }
 
+/* Fake a global pasid cache invalidation to remove all pasid cache entries */
+void vtd_accel_pasid_cache_reset(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type = VTD_INV_DESC_PASIDC_G_GLOBAL };
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    g_hash_table_iter_init(&hiod_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&hiod_it, NULL, (void **)&vtd_hiod)) {
+        vtd_accel_pasid_cache_invalidate(vtd_hiod, &pc_info);
+    }
+}
+
 static uint64_t vtd_get_host_iommu_quirks(uint32_t type,
                                           void *caps, uint32_t size)
 {
Signature · 1 line
2.47.3
#38 lore raw

PATCH v5 13/15 intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation

In reply to Zhenzhong Duan

This patch switches from VTDAddressSpace to VTDAccelPASIDCacheEntry for handling PASID bind/unbind operations and PIOTLB invalidations in passthrough scenarios. VTDAccelPASIDCacheEntry was introduced to cache PASID entries for passthrough devices and is now ready to propagate PASID bind/unbind operations and PIOTLB invalidations to the host.

Unlike the previous approach, VTDAccelPASIDCacheEntry supports both IOMMU_NO_PASID (rid_pasid) and other valid PASIDs, so this switch drops IOMMU_NO_PASID limitations that existed in the prior PASID bind/unbind and PIOTLB invalidation path. For IOMMU_NO_PASID of passthrough devices, VTDAddressSpace continues to handle shadow page modifications to the host, but no longer manages PASID bind/unbind operations or PIOTLB invalidations for passthrough scenarios.

---
 hw/i386/intel_iommu_accel.h   |   2 +-
 include/hw/i386/intel_iommu.h |   2 -
 hw/i386/intel_iommu.c         |  17 +----
 hw/i386/intel_iommu_accel.c   | 131 +++++++++++++++++-----------------
 4 files changed, 68 insertions(+), 84 deletions(-)
hw/i386/intel_iommu_accel.h +1 -1
index a2226b28b6..4a9003c92d 100644
@@ -16,6 +16,7 @@ typedef struct VTDAccelPASIDCacheEntry {
     VTDHostIOMMUDevice *vtd_hiod;
     VTDPASIDEntry pasid_entry;
     uint32_t pasid;
+    uint32_t fs_hwpt_id;
     QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
 } VTDAccelPASIDCacheEntry;
 
@@ -23,7 +24,6 @@ typedef struct VTDAccelPASIDCacheEntry {
 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                           Error **errp);
 VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as);
-bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp);
 void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                                       uint32_t pasid, hwaddr addr,
                                       uint64_t npages, bool ih);
include/hw/i386/intel_iommu.h +0 -2
index 95c76015e4..1842ba5840 100644
@@ -154,8 +154,6 @@ struct VTDAddressSpace {
      * with the guest IOMMU pgtables for a device.
      */
     IOVATree *iova_tree;
-
-    uint32_t fs_hwpt_id;
 };
 
 struct VTDIOTLBEntry {
hw/i386/intel_iommu.c +1 -16
index ac1b5f4d79..6067069e02 100644
@@ -86,8 +86,6 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
         VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
         if (pc_entry->valid) {
             pc_entry->valid = false;
-            /* It's fatal to get failure during reset */
-            vtd_propagate_guest_pasid(vtd_as, &error_fatal);
         }
     }
 }
@@ -3105,8 +3103,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     VTDPASIDEntry pe;
     IOMMUNotifier *n;
     uint16_t did;
-    const char *err_prefix = "Attaching to HWPT failed: ";
-    Error *local_err = NULL;
 
     if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
         if (!pc_entry->valid) {
@@ -3127,9 +3123,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
             vtd_address_space_unmap(vtd_as, n);
         }
         vtd_switch_address_space(vtd_as);
-
-        err_prefix = "Detaching from HWPT failed: ";
-        goto do_bind_unbind;
     }
 
     /*
@@ -3157,20 +3150,12 @@ 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 = "Replacing HWPT attachment failed: ";
-    } else {
+    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
         return;
     }
 
     vtd_switch_address_space(vtd_as);
     vtd_address_space_sync(vtd_as);
-
-do_bind_unbind:
-    /* TODO: Fault event injection into guest, report error to QEMU for now */
-    if (!vtd_propagate_guest_pasid(vtd_as, &local_err)) {
-        error_reportf_err(local_err, "%s", err_prefix);
-    }
 }
 
 static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/intel_iommu_accel.c +66 -65

226 lines not on this page · raw message

Signature · 1 line
2.47.3
#39 lore raw

PATCH v5 14/15 intel_iommu_accel: Add pasid bits size check

In reply to Zhenzhong Duan

If pasid bits size is bigger than host side, host could fail to emulate all bindings in guest. Add a check to fail device plug early.

---
 hw/i386/intel_iommu_internal.h | 1 +
 hw/i386/intel_iommu_accel.c    | 8 ++++++++
 2 files changed, 9 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index 2c716c5297..519af3fa90 100644
@@ -196,6 +196,7 @@
 #define VTD_ECAP_SRS                (1ULL << 31)
 #define VTD_ECAP_NWFS               (1ULL << 33)
 #define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
+#define VTD_ECAP_GET_PSS(ecap)      extract64(ecap, 35, 5)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_PDS                (1ULL << 42)
 #define VTD_ECAP_SMTS               (1ULL << 43)
hw/i386/intel_iommu_accel.c +8 -0
index 4ddf66262c..a0dd6b0ee0 100644
@@ -44,6 +44,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
     HostIOMMUDevice *hiod = vtd_hiod->hiod;
     struct HostIOMMUDeviceCaps *caps = &hiod->caps;
     struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+    uint8_t hpasid = VTD_ECAP_GET_PSS(vtd->ecap_reg) + 1;
     PCIBus *bus = vtd_hiod->bus;
     PCIDevice *pdev = bus->devices[vtd_hiod->devfn];
 
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
         return false;
     }
 
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {
+        error_setg(errp, "PASID bits size %d > host IOMMU PASID bits size %d",
+                   s->pasid, hpasid);
+        return false;
+    }
+
     if (pci_device_get_iommu_bus_devfn(pdev, &bus, NULL, NULL)) {
         error_setg(errp, "Host device downstream to a PCI bridge is "
                    "unsupported when x-flts=on");
Signature · 1 line
2.47.3
#40 lore raw

Re: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

Quoted 36 lines · Zhenzhong Duan

If pasid bits size is bigger than host side, host could fail to emulate all bindings in guest. Add a check to fail device plug early.

---
  hw/i386/intel_iommu_internal.h | 1 +
  hw/i386/intel_iommu_accel.c    | 8 ++++++++
  2 files changed, 9 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index 2c716c5297..519af3fa90 100644
@@ -196,6 +196,7 @@
  #define VTD_ECAP_SRS                (1ULL << 31)
  #define VTD_ECAP_NWFS               (1ULL << 33)
  #define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
+#define VTD_ECAP_GET_PSS(ecap)      extract64(ecap, 35, 5)
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_PDS                (1ULL << 42)
  #define VTD_ECAP_SMTS               (1ULL << 43)
hw/i386/intel_iommu_accel.c +1 -0
index 4ddf66262c..a0dd6b0ee0 100644
@@ -44,6 +44,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
      HostIOMMUDevice *hiod = vtd_hiod->hiod;
      struct HostIOMMUDeviceCaps *caps = &hiod->caps;
      struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+    uint8_t hpasid = VTD_ECAP_GET_PSS(vtd->ecap_reg) + 1;
      PCIBus *bus = vtd_hiod->bus;
      PCIDevice *pdev = bus->devices[vtd_hiod->devfn];
 

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, · line 69

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
          return false;
      }
 
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {

the second comparison looks strange. hpasid is derived from ecap_reg, while ecap_reg is from s->pasid... is there any place that changes the pss filed of ecap_reg afterward? I think this check should be
against caps->max_pasid_log2 as this is the value from hardware. right?

Quoted 8 lines
hw/i386/intel_iommu_accel.c +5 -0
+        error_setg(errp, "PASID bits size %d > host IOMMU PASID bits size %d",
+                   s->pasid, hpasid);
+        return false;
+    }
+
      if (pci_device_get_iommu_bus_devfn(pdev, &bus, NULL, NULL)) {
          error_setg(errp, "Host device downstream to a PCI bridge is "
                     "unsupported when x-flts=on");
#41 lore raw

RE: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

Hi

Quoted 46 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 28 lines · Zhenzhong Duan

If pasid bits size is bigger than host side, host could fail to emulate all bindings in guest. Add a check to fail device plug early.

---
  hw/i386/intel_iommu_internal.h | 1 +
  hw/i386/intel_iommu_accel.c    | 8 ++++++++
  2 files changed, 9 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index 2c716c5297..519af3fa90 100644
@@ -196,6 +196,7 @@
  #define VTD_ECAP_SRS                (1ULL << 31)
  #define VTD_ECAP_NWFS               (1ULL << 33)
  #define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
+#define VTD_ECAP_GET_PSS(ecap)      extract64(ecap, 35, 5)
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_PDS                (1ULL << 42)
  #define VTD_ECAP_SMTS               (1ULL << 43)
hw/i386/intel_iommu_accel.c +0 -0
index 4ddf66262c..a0dd6b0ee0 100644
@@ -44,6 +44,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -0
      HostIOMMUDevice *hiod = vtd_hiod->hiod;
      struct HostIOMMUDeviceCaps *caps = &hiod->caps;
      struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+    uint8_t hpasid = VTD_ECAP_GET_PSS(vtd->ecap_reg) + 1;
      PCIBus *bus = vtd_hiod->bus;
      PCIDevice *pdev = bus->devices[vtd_hiod->devfn];
 
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
          return false;
      }
 
Quoted 7 lines

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod, · line 69

Quoted 2 lines
hw/i386/intel_iommu_accel.c +2 -0
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {

the second comparison looks strange. hpasid is derived from ecap_reg, while ecap_reg is from s->pasid... is there any place that changes the pss filed of ecap_reg afterward? I think this check should be against caps->max_pasid_log2 as this is the value from hardware. right?

ecap_reg is from ioctl(IOMMU_GET_HW_INFO), it's not ecap register in vIOMMU.
My understanding is that guest kernel will pick the min(caps->max_pasid_log2, s->pasid), so checking the two doesn't make sense?

Thanks
Zhenzhong

Quoted 9 lines
Quoted 9 lines
hw/i386/intel_iommu_accel.c +5 -0
+        error_setg(errp, "PASID bits size %d > host IOMMU PASID bits size %d",
+                   s->pasid, hpasid);
+        return false;
+    }
+
      if (pci_device_get_iommu_bus_devfn(pdev, &bus, NULL, NULL)) {
          error_setg(errp, "Host device downstream to a PCI bridge is "
                     "unsupported when x-flts=on");
#42 lore raw

Re: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

Quoted 51 lines · Zhenzhong

Hi

Quoted 49 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 28 lines · Zhenzhong Duan

If pasid bits size is bigger than host side, host could fail to emulate all bindings in guest. Add a check to fail device plug early.

---
   hw/i386/intel_iommu_internal.h | 1 +
   hw/i386/intel_iommu_accel.c    | 8 ++++++++
   2 files changed, 9 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index 2c716c5297..519af3fa90 100644
@@ -196,6 +196,7 @@
   #define VTD_ECAP_SRS                (1ULL << 31)
   #define VTD_ECAP_NWFS               (1ULL << 33)
   #define VTD_ECAP_SET_PSS(x, v)      ((x)->ecap = deposit64((x)->ecap, 35, 5, v))
+#define VTD_ECAP_GET_PSS(ecap)      extract64(ecap, 35, 5)
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_PDS                (1ULL << 42)
   #define VTD_ECAP_SMTS               (1ULL << 43)
hw/i386/intel_iommu_accel.c +0 -0
index 4ddf66262c..a0dd6b0ee0 100644
@@ -44,6 +44,7 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -0
       HostIOMMUDevice *hiod = vtd_hiod->hiod;
       struct HostIOMMUDeviceCaps *caps = &hiod->caps;
       struct iommu_hw_info_vtd *vtd = &caps->vendor_caps.vtd;
+    uint8_t hpasid = VTD_ECAP_GET_PSS(vtd->ecap_reg) + 1;
       PCIBus *bus = vtd_hiod->bus;
       PCIDevice *pdev = bus->devices[vtd_hiod->devfn];
 
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
           return false;
       }
 
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {
Quoted 6 lines
Quoted 4 lines

the second comparison looks strange. hpasid is derived from ecap_reg, while ecap_reg is from s->pasid... is there any place that changes the pss filed of ecap_reg afterward? I think this check should be against caps->max_pasid_log2 as this is the value from hardware. right?

ecap_reg is from ioctl(IOMMU_GET_HW_INFO), it's not ecap register in vIOMMU.

I see. I misunderstood that the hpasid is got from the vIOMMU ecap register.

Quoted 1 line

My understanding is that guest kernel will pick the min(caps->max_pasid_log2, s->pasid), so checking the two doesn't make sense?

I think the caps->max_pasid_log2 should be the same with hpasid since
both are from host. Also, I didn't see the logic to pick the min(caps->max_pasid_log2, s->pasid). This is not expected just like the aw bits. We fail if aw-bits > host aw-bits.

#43 lore raw

RE: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 1 line
(quoted diff) +0 -0
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
           return false;
       }
 
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {
Quoted 6 lines
Quoted 4 lines

the second comparison looks strange. hpasid is derived from ecap_reg, while ecap_reg is from s->pasid... is there any place that changes the pss filed of ecap_reg afterward? I think this check should be against caps->max_pasid_log2 as this is the value from hardware. right?

ecap_reg is from ioctl(IOMMU_GET_HW_INFO), it's not ecap register in

vIOMMU.

I see. I misunderstood that the hpasid is got from the vIOMMU ecap register.

Quoted 8 lines
Quoted 2 lines

My understanding is that guest kernel will pick the min(caps->max_pasid_log2, s- pasid), so checking the two doesn't make sense?

I think the caps->max_pasid_log2 should be the same with hpasid since both are from host. Also, I didn't see the logic to pick the min(caps->max_pasid_log2, s->pasid). This is not expected just like the aw bits.
We fail if aw-bits > host aw-bits.

It's a bit different, see below:

static u32 dev_iommu_get_max_pasids(struct device *dev)

{
        u32 max_pasids = 0, bits = 0;
        int ret;

        if (dev_is_pci(dev)) {
                ret = pci_max_pasids(to_pci_dev(dev));
                if (ret > 0)
                        max_pasids = ret;
        } else {
                ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
                if (!ret)
                        max_pasids = 1UL << bits;
        }

        return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
}

dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);

caps->max_pasid_log2 = ilog2(idev->dev->iommu->max_pasids);

caps->max_pasid_log2 is then exposed to guest as pasid bit value in virtual PCI capability. Then guest kernel does the same min() calculation.

Zhenzhong

#44 lore raw

Re: [PATCH v5 14/15] intel_iommu_accel: Add pasid bits size check

Quoted 48 lines · Zhenzhong
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

Quoted 1 line
(quoted diff) +0 -0
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s,

VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
            return false;
        }
 
+    /* Only do the check when host device support PASIDs */
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {
Quoted 6 lines
Quoted 4 lines

the second comparison looks strange. hpasid is derived from ecap_reg, while ecap_reg is from s->pasid... is there any place that changes the pss filed of ecap_reg afterward? I think this check should be against caps->max_pasid_log2 as this is the value from hardware. right?

ecap_reg is from ioctl(IOMMU_GET_HW_INFO), it's not ecap register in

vIOMMU.

I see. I misunderstood that the hpasid is got from the vIOMMU ecap register.

Quoted 8 lines
Quoted 2 lines

My understanding is that guest kernel will pick the min(caps->max_pasid_log2, s- pasid), so checking the two doesn't make sense?

I think the caps->max_pasid_log2 should be the same with hpasid since both are from host. Also, I didn't see the logic to pick the min(caps->max_pasid_log2, s->pasid). This is not expected just like the aw bits.
We fail if aw-bits > host aw-bits.

It's a bit different, see below:

static u32 dev_iommu_get_max_pasids(struct device *dev)

{
         u32 max_pasids = 0, bits = 0;
         int ret;

         if (dev_is_pci(dev)) {
                 ret = pci_max_pasids(to_pci_dev(dev));
                 if (ret > 0)
                         max_pasids = ret;
         } else {
                 ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
                 if (!ret)
                         max_pasids = 1UL << bits;
         }

         return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
}
Quoted 6 lines

dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);

caps->max_pasid_log2 = ilog2(idev->dev->iommu->max_pasids);

caps->max_pasid_log2 is then exposed to guest as pasid bit value in virtual PCI capability. Then guest kernel does the same min() calculation.

got it. yes, vtd should check the bits reported via the ecap_reg.

#45 lore raw

PATCH v5 15/15 intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

In reply to Zhenzhong Duan

VFIO device will check flag VIOMMU_FLAG_PASID_SUPPORTED and expose PASID capability, also check VIOMMU_FLAG_WANT_PASID_ATTACH to enable pasid attachment, without those guest could not enable PASID of this device even if vIOMMU's pasid is configured.

We don't expose the two flags when first stage is not configured as we don't support shadow page table on a PASID.

This is the final knob to enable PASID.

---
 hw/i386/intel_iommu.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +5 -0
index 6067069e02..70546e91d4 100644
@@ -4788,6 +4788,11 @@ static uint64_t vtd_get_viommu_flags(void *opaque)
     if (s->fsts) {
         flags = VIOMMU_FLAG_WANT_NESTING_PARENT |
                 VIOMMU_FLAG_WANT_NESTING_DIRTY_TRACKING;
+
+        if (s->pasid) {
+            flags |= VIOMMU_FLAG_PASID_SUPPORTED |
+                     VIOMMU_FLAG_WANT_PASID_ATTACH;
+        }
     }
 
     return flags;
Signature · 1 line
2.47.3
Reviewed-by on v5 15/15
#46 lore raw

Re: [PATCH v5 15/15] intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

Quoted 6 lines · Zhenzhong Duan

VFIO device will check flag VIOMMU_FLAG_PASID_SUPPORTED and expose PASID capability, also check VIOMMU_FLAG_WANT_PASID_ATTACH to enable pasid attachment, without those guest could not enable PASID of this device even if vIOMMU's pasid is configured.

We don't expose the two flags when first stage is not configured as we

nit: s/first stage/fist stage translation/

Quoted 16 lines

don't support shadow page table on a PASID.

This is the final knob to enable PASID.

---
  hw/i386/intel_iommu.c | 5 +++++
  1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +0 -0
index 6067069e02..70546e91d4 100644
@@ -4788,6 +4788,11 @@ static uint64_t vtd_get_viommu_flags(void *opaque)
      if (s->fsts) {

hw/i386/intel_iommu.c static uint64_t vtd_get_viommu_flags(void *opaque) · line 4798

Quoted 10 lines
hw/i386/intel_iommu.c +5 -0
          flags = VIOMMU_FLAG_WANT_NESTING_PARENT |
                  VIOMMU_FLAG_WANT_NESTING_DIRTY_TRACKING;
+
+        if (s->pasid) {
+            flags |= VIOMMU_FLAG_PASID_SUPPORTED |
+                     VIOMMU_FLAG_WANT_PASID_ATTACH;
+        }
      }
 
      return flags;
Reviewed-by on v5 15/15
#47 lore raw

Re: [PATCH v5 15/15] intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

In reply to Zhenzhong Duan
Quoted 32 lines · Zhenzhong Duan

VFIO device will check flag VIOMMU_FLAG_PASID_SUPPORTED and expose PASID capability, also check VIOMMU_FLAG_WANT_PASID_ATTACH to enable pasid attachment, without those guest could not enable PASID of this device even if vIOMMU's pasid is configured.

We don't expose the two flags when first stage is not configured as we don't support shadow page table on a PASID.

This is the final knob to enable PASID.

---
 hw/i386/intel_iommu.c | 5 +++++

 1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +0 -0
index 6067069e02..70546e91d4 100644  
@@ -4788,6 +4788,11 @@ static uint64_t vtd_get_viommu_flags(void *opaque)  
      if (s->fsts) {  

         flags = VIOMMU_FLAG_WANT_NESTING_PARENT | VIOMMU_FLAG_WANT_NESTING_DIRTY_TRACKING; +
+        if (s->pasid) {
+            flags |= VIOMMU_FLAG_PASID_SUPPORTED | +                     VIOMMU_FLAG_WANT_PASID_ATTACH; +        }
     }

     return flags;

#48 lore raw
In reply to Zhenzhong Duan

Hi Zhenzhong,

The series passed our non-regression test suite for emulated svm. I will try to test upcoming versions as well.

cmd

Quoted 133 lines · Zhenzhong Duan

Hi,

Now we already support first stage translation with passthrough device backed by nested translation in host, but only for IOMMU_NO_PASID.

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt, to record the current binded nested hwpt and even future PRQ support. It's also designed to support IOMMU_NO_PASID.

The potential full definition of VTDAccelPASIDCacheEntry may like:

  typedef struct VTDAccelPASIDCacheEntry {
      VTDHostIOMMUDevice *vtd_hiod;
      VTDPASIDEntry pasid_entry;
      uint32_t pasid;
      uint32_t fs_hwpt_id;
      uint32_t fault_id;
      int fault_fd;
      QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
      IOMMUPRINotifier pri_notifier_entry;
      IOMMUPRINotifier *pri_notifier;
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;

Based on newest master branch.
GIT branch: [https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5](https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5)

PATCH01-08: Some preparing work
PATCH09-13: Handle PASID entry addition/removal and bind/unbind
PATCH14-15: Add PASID related check and enable PASID for passthrough device

Tests:
Tested with DSA device which driver uses 2 PASIDs by default.

Thanks
Zhenzhong

Changelog:
v5:
- use %u for pasid print (Shammer)
- s/attachment/detachment in API doc (Shammer)
- save iommu pasid in vtd_as to avoid the check of PCI pasid whenever possible (Clement)
- commit log minor tweak on patch3 and patch7 (Liuyi)
- use name 'pasid-bits' for pasid property (Liuyi)

v4:
- improve documents of VIOMMU_FLAGs (Cédric)
- add a cleanup patch to simplify the check between PCI pasid and VTD pasid (Clement)
- rebase to newest master branch

v3:
- fix @Pasid parameter's comment (Liuyi)
- introduce a wrapper vtd_accel_delete_pc() (Liuyi)
- drop patch12 in v2 to avoid a race condition (Clement)
- s/PASID_0/IOMMU_NO_PASID s/gloal/global s/as_it/hiod_it (Liuyi)
- make trace_vtd_device_at/detach_hwpt use IOMMU_NO_PASID (Liuyi)
- change _accel subfix style naming to vtd_accel_ prefix style (Liuyi)
- drop unnecessary parameter vtd_hiod* from vtd_destroy_old_fs_hwpt() (Liuyi)
- introduce a new flag VIOMMU_FLAG_WANT_PASID_ATTACH for pasid attach (Nicolin)

v2:
- move the check "s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH" to patch5 (Clement)
- move #include "hw/core/iommu.h" before #include "hw/core/qdev.h" (Liuyi)
- polish the comment about @Pasid parameter (Liuyi)
- s/pe/pasid_entry, s/as_it/hiod_it, s/vtd_find_add_pc/vtd_accel_fill_pc (Liuyi)
- s/VTDACCELPASIDCacheEntry/VTDAccelPASIDCacheEntry (Liuyi)
- add explanation in code about PASID removal before addition (Liuyi)
- polish the comment about scope of VTDAccelPASIDCacheEntry vs VTDPASIDCacheEntry (Liuyi)
- add an optimization to bypass PASID entry addition for PASID selective pv_inv_dsc (Liuyi)

v1:
- use naming pattern "XXX_SET_THENAME" same as smmu (Clement)
- fix s->pasid check (Clement)

RFCv2:
- extend attach/detach_hwpt() instead of introducing new callbacks (Shammer)
- Define IOMMU_NO_PASID for device attachment without pasid (Nicolin)
- update vtd_destroy_old_fs_hwpt()'s parameter for naming consistency (Clement)
- check pasid bits size to be no more than 20 bits (Clement)
- initialize local variable max_pasid_log2 to 0 (Cédric)

Zhenzhong Duan (15):
  vfio/iommufd: Extend attach/detach_hwpt callback implementations with pasid
  iommufd: Extend attach/detach_hwpt callbacks to support pasid vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Rename pasid property to "pasid-bits" and define it as type uint8
  intel_iommu: Export some functions
  intel_iommu: Use IOMMU_NO_PASID and delete PASID_0
  intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally
  intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request
  intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request
  intel_iommu_accel: Bypass PASID entry addition for just deleted entry
  intel_iommu_accel: Handle PASID entry removal for system reset
  intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation
  intel_iommu_accel: Add pasid bits size check
  intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

 hw/i386/intel_iommu_accel.h    |  20 +- hw/i386/intel_iommu_internal.h |  44 +++- include/hw/core/iommu.h        |  13 ++ include/hw/i386/intel_iommu.h  |   4 +- include/hw/vfio/vfio-device.h  |   1 + include/system/iommufd.h       |  18 +- include/system/memory.h        |   2 +-
 backends/iommufd.c             |   9 +- hw/arm/smmuv3-accel.c          |  12 +- hw/i386/intel_iommu.c          | 254 +++++++++----------- hw/i386/intel_iommu_accel.c    | 414 +++++++++++++++++++++++++++------ hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  56 +++-- hw/vfio/trace-events           |   4 +-

 14 files changed, 607 insertions(+), 255 deletions(-)
#49 lore raw

Glad to know, thanks Clement. Let me know if I can have your Tested-by.

Quoted 155 lines

-----Original Message-----
From: Clément MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v5 00/15] intel_iommu: Enable PASID support for passthrough device

Hi Zhenzhong,

The series passed our non-regression test suite for emulated svm. I will try to test upcoming versions as well.

cmd

Quoted 36 lines · Zhenzhong Duan

Hi,

Now we already support first stage translation with passthrough device backed by nested translation in host, but only for IOMMU_NO_PASID.

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt, to record the current binded nested hwpt and even future PRQ support. It's also designed to support IOMMU_NO_PASID.

The potential full definition of VTDAccelPASIDCacheEntry may like:

  typedef struct VTDAccelPASIDCacheEntry {
      VTDHostIOMMUDevice *vtd_hiod;
      VTDPASIDEntry pasid_entry;
      uint32_t pasid;
      uint32_t fs_hwpt_id;
      uint32_t fault_id;
      int fault_fd;
      QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
      IOMMUPRINotifier pri_notifier_entry;
      IOMMUPRINotifier *pri_notifier;
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;

Quoted 2 lines

Based on newest master branch.
GIT branch:

[https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5](https://g
ithub.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5)

Quoted 8 lines

PATCH01-08: Some preparing work
PATCH09-13: Handle PASID entry addition/removal and bind/unbind
PATCH14-15: Add PASID related check and enable PASID for passthrough device

Tests:
Tested with DSA device which driver uses 2 PASIDs by default.

Quoted 8 lines

Thanks
Zhenzhong

Changelog:
v5:
- use %u for pasid print (Shammer)
- s/attachment/detachment in API doc (Shammer)
- save iommu pasid in vtd_as to avoid the check of PCI pasid whenever possible

(Clement)

Quoted 6 lines

- commit log minor tweak on patch3 and patch7 (Liuyi)
- use name 'pasid-bits' for pasid property (Liuyi)

v4:
- improve documents of VIOMMU_FLAGs (Cédric)
- add a cleanup patch to simplify the check between PCI pasid and VTD pasid

(Clement)

Quoted 11 lines

- rebase to newest master branch

v3:
- fix @Pasid parameter's comment (Liuyi)
- introduce a wrapper vtd_accel_delete_pc() (Liuyi)
- drop patch12 in v2 to avoid a race condition (Clement)
- s/PASID_0/IOMMU_NO_PASID s/gloal/global s/as_it/hiod_it (Liuyi)
- make trace_vtd_device_at/detach_hwpt use IOMMU_NO_PASID (Liuyi)
- change _accel subfix style naming to vtd_accel_ prefix style (Liuyi)
- drop unnecessary parameter vtd_hiod* from vtd_destroy_old_fs_hwpt() (Liuyi)
- introduce a new flag VIOMMU_FLAG_WANT_PASID_ATTACH for pasid attach

(Nicolin)

Quoted 3 lines

v2:
- move the check "s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH" to patch5

(Clement)

Quoted 6 lines

- move #include "hw/core/iommu.h" before #include "hw/core/qdev.h" (Liuyi)
- polish the comment about @Pasid parameter (Liuyi)
- s/pe/pasid_entry, s/as_it/hiod_it, s/vtd_find_add_pc/vtd_accel_fill_pc (Liuyi)
- s/VTDACCELPASIDCacheEntry/VTDAccelPASIDCacheEntry (Liuyi)
- add explanation in code about PASID removal before addition (Liuyi)
- polish the comment about scope of VTDAccelPASIDCacheEntry vs

VTDPASIDCacheEntry (Liuyi)

Quoted 1 line

- add an optimization to bypass PASID entry addition for PASID selective

pv_inv_dsc (Liuyi)

Quoted 9 lines

v1:
- use naming pattern "XXX_SET_THENAME" same as smmu (Clement)
- fix s->pasid check (Clement)

RFCv2:
- extend attach/detach_hwpt() instead of introducing new callbacks (Shammer)
- Define IOMMU_NO_PASID for device attachment without pasid (Nicolin)
- update vtd_destroy_old_fs_hwpt()'s parameter for naming consistency

(Clement)

Quoted 43 lines

- check pasid bits size to be no more than 20 bits (Clement)
- initialize local variable max_pasid_log2 to 0 (Cédric)

Zhenzhong Duan (15):
  vfio/iommufd: Extend attach/detach_hwpt callback implementations with pasid
  iommufd: Extend attach/detach_hwpt callbacks to support pasid vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Rename pasid property to "pasid-bits" and define it as type uint8
  intel_iommu: Export some functions
  intel_iommu: Use IOMMU_NO_PASID and delete PASID_0
  intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally
  intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request
  intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request
  intel_iommu_accel: Bypass PASID entry addition for just deleted entry
  intel_iommu_accel: Handle PASID entry removal for system reset
  intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation
  intel_iommu_accel: Add pasid bits size check
  intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

 hw/i386/intel_iommu_accel.h    |  20 +- hw/i386/intel_iommu_internal.h |  44 +++- include/hw/core/iommu.h        |  13 ++ include/hw/i386/intel_iommu.h  |   4 +- include/hw/vfio/vfio-device.h  |   1 + include/system/iommufd.h       |  18 +- include/system/memory.h        |   2 +-
 backends/iommufd.c             |   9 +- hw/arm/smmuv3-accel.c          |  12 +- hw/i386/intel_iommu.c          | 254 +++++++++----------- hw/i386/intel_iommu_accel.c    | 414 +++++++++++++++++++++++++++------ hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  56 +++-- hw/vfio/trace-events           |   4 +-

 14 files changed, 607 insertions(+), 255 deletions(-)
#50 lore raw
In reply to Clément MATHIEU--DRIF

Hello Clément,

Quoted 3 lines · Clément MATHIEU--DRIF

Hi Zhenzhong,

The series passed our non-regression test suite for emulated svm.

Is that svm suite something you could share, even upstream ?

C.

Quoted 138 lines

I will try to test upcoming versions as well.

cmd

Quoted 133 lines · Zhenzhong Duan

Hi,

Now we already support first stage translation with passthrough device backed by nested translation in host, but only for IOMMU_NO_PASID.

Structure VTDAddressSpace includes some elements suitable for emulated device and passthrough device without PASID, e.g., address space, different memory regions, etc, it is also protected by vtd iommu lock, all these are useless and become a burden for passthrough device with PASID.

When there are lots of PASIDs used in one device, the AS and MRs are all registered to memory core and impact the whole system performance.

So instead of using VTDAddressSpace to cache pasid entry for each pasid of a passthrough device, we define a light weight structure VTDAccelPASIDCacheEntry with only necessary elements for each pasid. We will use this struct as a parameter to conduct binding/unbinding to nested hwpt, to record the current binded nested hwpt and even future PRQ support. It's also designed to support IOMMU_NO_PASID.

The potential full definition of VTDAccelPASIDCacheEntry may like:

  typedef struct VTDAccelPASIDCacheEntry {
      VTDHostIOMMUDevice *vtd_hiod;
      VTDPASIDEntry pasid_entry;
      uint32_t pasid;
      uint32_t fs_hwpt_id;
      uint32_t fault_id;
      int fault_fd;
      QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
      IOMMUPRINotifier pri_notifier_entry;
      IOMMUPRINotifier *pri_notifier;
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;

Based on newest master branch.
GIT branch: [https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5](https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid.v5)

PATCH01-08: Some preparing work
PATCH09-13: Handle PASID entry addition/removal and bind/unbind
PATCH14-15: Add PASID related check and enable PASID for passthrough device

Tests:
Tested with DSA device which driver uses 2 PASIDs by default.

Thanks
Zhenzhong

Changelog:
v5:
- use %u for pasid print (Shammer)
- s/attachment/detachment in API doc (Shammer)
- save iommu pasid in vtd_as to avoid the check of PCI pasid whenever possible (Clement)
- commit log minor tweak on patch3 and patch7 (Liuyi)
- use name 'pasid-bits' for pasid property (Liuyi)

v4:
- improve documents of VIOMMU_FLAGs (Cédric)
- add a cleanup patch to simplify the check between PCI pasid and VTD pasid (Clement)
- rebase to newest master branch

v3:
- fix @Pasid parameter's comment (Liuyi)
- introduce a wrapper vtd_accel_delete_pc() (Liuyi)
- drop patch12 in v2 to avoid a race condition (Clement)
- s/PASID_0/IOMMU_NO_PASID s/gloal/global s/as_it/hiod_it (Liuyi)
- make trace_vtd_device_at/detach_hwpt use IOMMU_NO_PASID (Liuyi)
- change _accel subfix style naming to vtd_accel_ prefix style (Liuyi)
- drop unnecessary parameter vtd_hiod* from vtd_destroy_old_fs_hwpt() (Liuyi)
- introduce a new flag VIOMMU_FLAG_WANT_PASID_ATTACH for pasid attach (Nicolin)

v2:
- move the check "s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH" to patch5 (Clement)
- move #include "hw/core/iommu.h" before #include "hw/core/qdev.h" (Liuyi)
- polish the comment about @Pasid parameter (Liuyi)
- s/pe/pasid_entry, s/as_it/hiod_it, s/vtd_find_add_pc/vtd_accel_fill_pc (Liuyi)
- s/VTDACCELPASIDCacheEntry/VTDAccelPASIDCacheEntry (Liuyi)
- add explanation in code about PASID removal before addition (Liuyi)
- polish the comment about scope of VTDAccelPASIDCacheEntry vs VTDPASIDCacheEntry (Liuyi)
- add an optimization to bypass PASID entry addition for PASID selective pv_inv_dsc (Liuyi)

v1:
- use naming pattern "XXX_SET_THENAME" same as smmu (Clement)
- fix s->pasid check (Clement)

RFCv2:
- extend attach/detach_hwpt() instead of introducing new callbacks (Shammer)
- Define IOMMU_NO_PASID for device attachment without pasid (Nicolin)
- update vtd_destroy_old_fs_hwpt()'s parameter for naming consistency (Clement)
- check pasid bits size to be no more than 20 bits (Clement)
- initialize local variable max_pasid_log2 to 0 (Cédric)

Zhenzhong Duan (15):
  vfio/iommufd: Extend attach/detach_hwpt callback implementations with pasid
  iommufd: Extend attach/detach_hwpt callbacks to support pasid vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag
  intel_iommu: Rename pasid property to "pasid-bits" and define it as type uint8
  intel_iommu: Export some functions
  intel_iommu: Use IOMMU_NO_PASID and delete PASID_0
  intel_iommu: Refactor PASID processing to use IOMMU_NO_PASID internally
  intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request
  intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request
  intel_iommu_accel: Bypass PASID entry addition for just deleted entry
  intel_iommu_accel: Handle PASID entry removal for system reset
  intel_iommu_accel: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation
  intel_iommu_accel: Add pasid bits size check
  intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED and VIOMMU_FLAG_WANT_PASID_ATTACH

  hw/i386/intel_iommu_accel.h    |  20 +-
  hw/i386/intel_iommu_internal.h |  44 +++-
  include/hw/core/iommu.h        |  13 ++
  include/hw/i386/intel_iommu.h  |   4 +-
  include/hw/vfio/vfio-device.h  |   1 +
  include/system/iommufd.h       |  18 +-
  include/system/memory.h        |   2 +-
  backends/iommufd.c             |   9 +-
  hw/arm/smmuv3-accel.c          |  12 +-
  hw/i386/intel_iommu.c          | 254 +++++++++-----------
  hw/i386/intel_iommu_accel.c    | 414 +++++++++++++++++++++++++++------
  hw/vfio/device.c               |  11 +
  hw/vfio/iommufd.c              |  56 +++--
  hw/vfio/trace-events           |   4 +-
  14 files changed, 607 insertions(+), 255 deletions(-)