Skip to contentSkip to conversation

intel_iommu: Enable PASID support for passthrough device

PATCH Superseded Zhenzhong Duan 7 participants 55 messages Mar 26 – Mar 31

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 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 PASID_0.

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 PASID_0.

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;

GIT branch: https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid

PATCH01-06: Some preparing work
PATCH07-10: Handle PASID entry addition and removal
PATCH11-12: Support pasid binding and unbinding
PATCH13-14: Add PASID related check and enable PASID for passthrough device

This patchset depends on a kernel feature enhancement[1] to work.

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

Thanks
Zhenzhong

[1] https://lore.kernel.org/all/[email protected]/

Changelog:
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 (14):
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: Change pasid property from bool to uint8
intel_iommu: Export some functions
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: Support pasid binding/unbinding and PIOTLB flushing
intel_iommu_accel: drop _lock suffix in
vtd_flush_host_piotlb_all_locked()
intel_iommu_accel: Add pasid bits size check
intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED when configured

 hw/i386/intel_iommu_accel.h    |  34 ++-
 hw/i386/intel_iommu_internal.h |  43 +++-
 include/hw/core/iommu.h        |   2 +
 include/hw/i386/intel_iommu.h  |   4 +-
 include/hw/vfio/vfio-device.h  |   1 +
 include/system/iommufd.h       |  16 +-
 backends/iommufd.c             |   9 +-
 hw/arm/smmuv3-accel.c          |  12 +-
 hw/i386/intel_iommu.c          |  83 +++----
 hw/i386/intel_iommu_accel.c    | 420 +++++++++++++++++++++++++++------
 hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  56 +++--
 hw/vfio/trace-events           |   4 +-
 13 files changed, 524 insertions(+), 171 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/14 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 86af315c15..bfcd511013 100644
@@ -28,4 +28,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 846e3625c5..764a3e4855 100644
@@ -182,8 +182,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 %d to id=%d"
+iommufd_cdev_pasid_detach_ioas_hwpt(int iommufd, const char *name, uint32_t pasid) " [iommufd=%d] Successfully detached %s pasid %d"
 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
Reviewed-by on v2 1/14
#2 lore raw

Re: [PATCH v2 01/14] vfio/iommufd: Extend attach/detach_hwpt callback implementations with pasid

Quoted 6 lines · Zhenzhong Duan

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.

Quoted 6 lines

The implementation is similar for detachment.

#3 lore raw

PATCH v2 2/14 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    | 16 +++++++++++-----
 backends/iommufd.c          |  9 +++++----
 hw/arm/smmuv3-accel.c       | 12 ++++++++----
 hw/i386/intel_iommu_accel.c |  8 +++++---
 hw/vfio/iommufd.c           | 10 +++++-----
 5 files changed, 34 insertions(+), 21 deletions(-)
include/system/iommufd.h +11 -5
index 7062944fe6..45a9e87cb0 100644
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.
      *
+     * @pasid: target pasid of attach.
+     *
      * @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 *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, 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 {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.
      *
+     * @pasid: target pasid of detach.
+     *
      * @errp: pass an Error out when attachment fails.
      *
      * Returns: true on success, false on failure.
      */
-    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        Error **errp);
 };
 
 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp);
-bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t pasid, uint32_t hwpt_id,
                                            Error **errp);
+bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t pasid, Error **errp);
 #endif
backends/iommufd.c +5 -4
index e1fee16acf..ab612e4874 100644
@@ -539,23 +539,24 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
 }
 
 bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
 {
     HostIOMMUDeviceIOMMUFDClass *idevc =
         HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
 
     g_assert(idevc->attach_hwpt);
-    return idevc->attach_hwpt(idev, hwpt_id, errp);
+    return idevc->attach_hwpt(idev, pasid, hwpt_id, errp);
 }
 
 bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
 {
     HostIOMMUDeviceIOMMUFDClass *idevc =
         HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
 
     g_assert(idevc->detach_hwpt);
-    return idevc->detach_hwpt(idev, errp);
+    return idevc->detach_hwpt(idev, pasid, errp);
 }
 
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
hw/arm/smmuv3-accel.c +8 -4
index 65c2f44880..0af6b3296d 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(idev, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
+                                               errp)) {
         if (s1_hwpt) {
             iommufd_backend_free_id(idev->iommufd, s1_hwpt->hwpt_id);
             g_free(s1_hwpt);
@@ -575,7 +576,8 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,
 
     /* Attach a HWPT based on SMMUv3 GBPA.ABORT value */
     hwpt_id = smmuv3_accel_gbpa_hwpt(s, accel);
-    if (!host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, 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,
     idev = accel_dev->idev;
     accel = accel_dev->s_accel;
     /* Re-attach the default s2 hwpt id */
-    if (!host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, NULL)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+                                               idev->hwpt_id, NULL)) {
         error_report("Unable to attach the default HW pagetable: idev devid "
                      "0x%x", idev->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->idev, hwpt_id,
+        if (!host_iommu_device_iommufd_attach_hwpt(accel_dev->idev,
+                                                   IOMMU_NO_PASID, hwpt_id,
                                                    &local_err)) {
             error_append_hint(&local_err, "Failed to attach GBPA hwpt %u for "
                               "idev devid %u", hwpt_id, accel_dev->idev->devid);
hw/i386/intel_iommu_accel.c +5 -3
index 67d54849f2..45c08c8f6f 100644
@@ -121,7 +121,8 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
         }
     }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
+                                                errp);
     trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);
     if (ret) {
         /* Destroy old fs_hwpt if it's a replacement */
@@ -145,7 +146,7 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     bool ret;
 
     if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, IOMMU_NO_PASID, errp);
         trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
     } else {
         /*
@@ -153,7 +154,8 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
          * we fallback to the default HWPT which contains shadow page table.
          * So guest DMA could still work.
          */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+                                                    idev->hwpt_id, errp);
         trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                            ret);
     }
hw/vfio/iommufd.c +5 -5
index 93f1e61a8c..e822039858 100644
@@ -927,21 +927,21 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, const void *data)
 
 static bool
 host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
 {
     VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->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 *idev,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
 {
     VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->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
Reviewed-by on v2 2/14
#4 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 142

Quoted 2 lines
include/system/iommufd.h +2 -0
+     * @pasid: target pasid of attach.
+     *

How about "target pasid of the device to be attached"?

The uAPI docs has the narrative "pasid of this device", which makes it clearer.

Quoted 14 lines
include/system/iommufd.h +2 -2
      * @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 *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, 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.

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 159

Quoted 4 lines
include/system/iommufd.h +1 -0
@@ -154,15 +156,19 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.
      *
+     * @pasid: target pasid of detach.

Ditto

Otherwise,

#5 lore raw

RE: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 7 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 8 lines

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

A host device under host IOMMU?

Quoted 5 lines

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 142

Quoted 3 lines
include/system/iommufd.h +2 -0
+     * @pasid: target pasid of attach.
+     *

How about "target pasid of the device to be attached"?

Looks fine, will do.

Thanks
Zhenzhong

Quoted 29 lines

The uAPI docs has the narrative "pasid of this device", which makes it clearer.

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass {

Quoted 12 lines
include/system/iommufd.h +2 -2
      * @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 *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        uint32_t hwpt_id, Error **errp);
     /**
      * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware

page table.

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 159

Quoted 6 lines
include/system/iommufd.h +1 -0
      * VFIO and VDPA device can have different implementation.
@@ -154,15 +156,19 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.
      *
+     * @pasid: target pasid of detach.

Ditto

Otherwise,

#6 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 14 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 8 lines

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

Nicolin

#7 lore raw

RE: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 23 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 3 lines · Zhenzhong
Quoted 3 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks

to

Quoted 11 lines
Quoted 10 lines

support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
      *
      * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 3 lines
Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

We want to emphasize that it's a "host device" backed by "host IOMMU", "host device" is not enough, I think.

Thanks
Zhenzhong

#8 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 23 lines · Zhenzhong
Quoted 23 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 3 lines · Zhenzhong
Quoted 3 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks

to

Quoted 11 lines
Quoted 10 lines

support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

We want to emphasize that it's a "host device" backed by "host IOMMU", "host device" is not enough, I think.

Yes. These are related to the Host IOMMU device backends :

- VFIO IOMMU Type1, a.k.a legacy backend
- IOMMUFD,

There are other implementations. VFIO IOMMU Type1 is versioned and a ppc flavor exists.

Thanks,

C.

#9 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 30 lines · Cédric Le Goater
Quoted 23 lines · Zhenzhong
Quoted 23 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 3 lines · Zhenzhong
Quoted 3 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks

to

Quoted 11 lines
Quoted 10 lines

support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

We want to emphasize that it's a "host device" backed by "host IOMMU", "host device" is not enough, I think.

Quoted 7 lines

Yes. These are related to the Host IOMMU device backends :

- VFIO IOMMU Type1, a.k.a legacy backend
- IOMMUFD,

There are other implementations. VFIO IOMMU Type1 is versioned and a ppc flavor exists.

But the @idev here is a passthrough device (VFIO/PCI)?

"IOMMU device" would be SMMU, VT-d...

Especially we are adding @pasid parameter. Relating it to SMMU or VT-d sounds weird.

Nicolin

#10 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Hi Cédric, Zhenzhong,

Quoted 39 lines · Nicolin Chen
Quoted 30 lines · Cédric Le Goater
Quoted 23 lines · Zhenzhong
Quoted 23 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 3 lines · Zhenzhong
Quoted 3 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks

to

Quoted 11 lines
Quoted 10 lines

support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
        *
        * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

We want to emphasize that it's a "host device" backed by "host IOMMU", "host device" is not enough, I think.

Quoted 7 lines

Yes. These are related to the Host IOMMU device backends :

- VFIO IOMMU Type1, a.k.a legacy backend
- IOMMUFD,

There are other implementations. VFIO IOMMU Type1 is versioned and a ppc flavor exists.

Quoted 6 lines

But the @idev here is a passthrough device (VFIO/PCI)?

"IOMMU device" would be SMMU, VT-d...

Especially we are adding @pasid parameter. Relating it to SMMU or VT-d sounds weird.

how about "@idev: the iommufd-backed assigned device to xxx"? This object is quite similar to the iommufd_device object of the kernel side, kernel refers iommufd_device parameters as device.

1046 /**

1047  * iommufd_device_detach - Disconnect a device/device to an iommu_domain
1048  * @idev: device to detach
1049  * @pasid: pasid to detach
#11 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

In reply to Nicolin Chen via
Quoted 36 lines · Nicolin Chen
Quoted 35 lines · Cédric Le Goater
Quoted 23 lines · Zhenzhong
Quoted 23 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 3 lines · Zhenzhong
Quoted 3 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks

to

Quoted 11 lines
Quoted 10 lines

support pasid

Review struct HostIOMMUDeviceIOMMUFDClass { · line 139

Quoted 3 lines · Zhenzhong Duan
(quoted diff) +0 -0
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
        *
        * @idev: host IOMMU device backed by IOMMUFD backend.

Not commenting against this patch, but I just found the "host IOMMU device" and the "HostIOMMUDeviceIOMMUFD" a bit ambiguous. It's not an "IOMMU device" right? Perhaps somebody can help me understand :)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

A host device under host IOMMU?

"host device" would make sense, not "host IOMMU device", right?

We want to emphasize that it's a "host device" backed by "host IOMMU", "host device" is not enough, I think.

Yes. These are related to the Host IOMMU device backends :

- VFIO IOMMU Type1, a.k.a legacy backend
- IOMMUFD,

Quoted 6 lines
Quoted 2 lines

There are other implementations. VFIO IOMMU Type1 is versioned and a ppc flavor exists.

But the @idev here is a passthrough device (VFIO/PCI)?

"IOMMU device" would be SMMU, VT-d...

The 'idev' above is for a 'host IOMMU device backed by IOMMUFD backend' AFAICT.

Quoted 2 lines

Especially we are adding @pasid parameter. Relating it to SMMU or VT-d sounds weird.

host IOMMU device variables are usually named 'hiod' and the class 'hiodc'.

Thanks,

C.

#12 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

In reply to Zhenzhong Duan

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 126

Quoted 12 lines · Zhenzhong Duan
hw/i386/intel_iommu_accel.c +2 -1
index 67d54849f2..45c08c8f6f 100644
@@ -121,7 +121,8 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
          }
      }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
+                                                errp);
      trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);

The trace looks to use the wrong pasid. could you make it use IOMMU_NO_PASID as well? Same to the below chunks.

Quoted 12 lines
hw/i386/intel_iommu_accel.c +1 -1
      if (ret) {
          /* Destroy old fs_hwpt if it's a replacement */
@@ -145,7 +146,7 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
      bool ret;
 
      if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, IOMMU_NO_PASID, errp);
          trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
      } else {
          /*

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 160

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -1
@@ -153,7 +154,8 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
           * we fallback to the default HWPT which contains shadow page table.
           * So guest DMA could still work.
           */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+                                                    idev->hwpt_id, errp);
          trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                             ret);

Regards,
Yi Liu

#13 lore raw

RE: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 18 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

hw/i386/intel_iommu_accel.c static bool

Quoted 5 lines · Zhenzhong Duan
hw/i386/intel_iommu_accel.c +0 -0
index 67d54849f2..45c08c8f6f 100644
@@ -121,7 +121,8 @@ static bool

vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
          }
      }
 
Quoted 8 lines

hw/i386/intel_iommu_accel.c static bool · line 124

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -1
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,

hwpt_id,

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 126

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -0
+                                                errp);
      trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);

The trace looks to use the wrong pasid. could you make it use IOMMU_NO_PASID as well? Same to the below chunks.

OK, will do. In fact vtd_as->pasid always equals to IOMMU_NO_PASID here because we don't support pasid yet.

Thanks
Zhenzhong

Quoted 29 lines

hw/i386/intel_iommu_accel.c static bool

Quoted 4 lines
hw/i386/intel_iommu_accel.c +0 -0
      if (ret) {
          /* Destroy old fs_hwpt if it's a replacement */
@@ -145,7 +146,7 @@ static bool

vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -1
      bool ret;
 
      if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        ret = host_iommu_device_iommufd_detach_hwpt(idev,

IOMMU_NO_PASID, errp);

hw/i386/intel_iommu_accel.c static bool

Quoted 4 lines
hw/i386/intel_iommu_accel.c +0 -0
          trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
      } else {
          /*
@@ -153,7 +154,8 @@ static bool

vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 4 lines
hw/i386/intel_iommu_accel.c +0 -1
           * we fallback to the default HWPT which contains shadow page table.
           * So guest DMA could still work.
           */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id,

errp);

hw/i386/intel_iommu_accel.c static bool · line 157

Quoted 1 line
hw/i386/intel_iommu_accel.c +1 -0
+        ret = host_iommu_device_iommufd_attach_hwpt(idev,

IOMMU_NO_PASID,

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 160

Quoted 3 lines
hw/i386/intel_iommu_accel.c +1 -0
+                                                    idev->hwpt_id, errp);
          trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                             ret);

Regards,
Yi Liu

#14 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

In reply to Zhenzhong Duan

Zhenzhong

Quoted 65 lines · Zhenzhong Duan

Same for the two wrappers and their call sites.

---
  include/system/iommufd.h    | 16 +++++++++++-----
  backends/iommufd.c          |  9 +++++----
  hw/arm/smmuv3-accel.c       | 12 ++++++++----
  hw/i386/intel_iommu_accel.c |  8 +++++---
  hw/vfio/iommufd.c           | 10 +++++-----
  5 files changed, 34 insertions(+), 21 deletions(-)
include/system/iommufd.h +11 -5
index 7062944fe6..45a9e87cb0 100644
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.
       *
+     * @pasid: target pasid of attach.
+     *
       * @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 *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, 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 {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.
       *
+     * @pasid: target pasid of detach.
+     *
       * @errp: pass an Error out when attachment fails.
       *
       * Returns: true on success, false on failure.
       */
-    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        Error **errp);
  };
 
  bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp);
-bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t pasid, uint32_t hwpt_id,
                                             Error **errp);
+bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t pasid, Error **errp);
  #endif
backends/iommufd.c +0 -0
index e1fee16acf..ab612e4874 100644

backends/iommufd.c bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id, · line 546

Quoted 9 lines
backends/iommufd.c +2 -1
@@ -539,23 +539,24 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
  }
 
  bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
  {
      HostIOMMUDeviceIOMMUFDClass *idevc =
          HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);

For consistency with the rest of the code, I would rename all the idev and idevc variables, to hiod and hiodc.

Thanks,

C.

Quoted 127 lines
backends/iommufd.c +3 -3
      g_assert(idevc->attach_hwpt);
-    return idevc->attach_hwpt(idev, hwpt_id, errp);
+    return idevc->attach_hwpt(idev, pasid, hwpt_id, errp);
  }
 
  bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
  {
      HostIOMMUDeviceIOMMUFDClass *idevc =
          HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
 
      g_assert(idevc->detach_hwpt);
-    return idevc->detach_hwpt(idev, errp);
+    return idevc->detach_hwpt(idev, pasid, errp);
  }
 
  static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
hw/arm/smmuv3-accel.c +8 -4
index 65c2f44880..0af6b3296d 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(idev, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
+                                               errp)) {
          if (s1_hwpt) {
              iommufd_backend_free_id(idev->iommufd, s1_hwpt->hwpt_id);
              g_free(s1_hwpt);
@@ -575,7 +576,8 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,
 
      /* Attach a HWPT based on SMMUv3 GBPA.ABORT value */
      hwpt_id = smmuv3_accel_gbpa_hwpt(s, accel);
-    if (!host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, 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,
      idev = accel_dev->idev;
      accel = accel_dev->s_accel;
      /* Re-attach the default s2 hwpt id */
-    if (!host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, NULL)) {
+    if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+                                               idev->hwpt_id, NULL)) {
          error_report("Unable to attach the default HW pagetable: idev devid "
                       "0x%x", idev->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->idev, hwpt_id,
+        if (!host_iommu_device_iommufd_attach_hwpt(accel_dev->idev,
+                                                   IOMMU_NO_PASID, hwpt_id,
                                                     &local_err)) {
              error_append_hint(&local_err, "Failed to attach GBPA hwpt %u for "
                                "idev devid %u", hwpt_id, accel_dev->idev->devid);
hw/i386/intel_iommu_accel.c +5 -3
index 67d54849f2..45c08c8f6f 100644
@@ -121,7 +121,8 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
          }
      }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, hwpt_id, errp);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
+                                                errp);
      trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);
      if (ret) {
          /* Destroy old fs_hwpt if it's a replacement */
@@ -145,7 +146,7 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
      bool ret;
 
      if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev, errp);
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, IOMMU_NO_PASID, errp);
          trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
      } else {
          /*
@@ -153,7 +154,8 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
           * we fallback to the default HWPT which contains shadow page table.
           * So guest DMA could still work.
           */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev, idev->hwpt_id, errp);
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+                                                    idev->hwpt_id, errp);
          trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                             ret);
      }
hw/vfio/iommufd.c +5 -5
index 93f1e61a8c..e822039858 100644
@@ -927,21 +927,21 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, const void *data)
 
  static bool
  host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
  {
      VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->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 *idev,
-                                           Error **errp)
+                                           uint32_t pasid, Error **errp)
  {
      VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->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,
#15 lore raw

RE: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Hi

Quoted 97 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Monday, March 30, 2026 8:43 PM
To: Duan, Zhenzhong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; clement.mathieu-- [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>; [email protected]
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Zhenzhong

Quoted 26 lines · Zhenzhong Duan

Same for the two wrappers and their call sites.

---
  include/system/iommufd.h    | 16 +++++++++++-----
  backends/iommufd.c          |  9 +++++----
  hw/arm/smmuv3-accel.c       | 12 ++++++++----
  hw/i386/intel_iommu_accel.c |  8 +++++---
  hw/vfio/iommufd.c           | 10 +++++-----
  5 files changed, 34 insertions(+), 21 deletions(-)
include/system/iommufd.h +2 -0
index 7062944fe6..45a9e87cb0 100644
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.
       *
+     * @pasid: target pasid of attach.
+     *
       * @hwpt_id: ID of IOMMUFD hardware page table.
       *

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 152

Quoted 10 lines
include/system/iommufd.h +2 -2
       * @errp: pass an Error out when attachment fails.
       *
       * Returns: true on success, false on failure.
       */
-    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        uint32_t hwpt_id, Error **errp);
      /**
       * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware

page table.

Quoted 7 lines
include/system/iommufd.h +2 -0
       * VFIO and VDPA device can have different implementation.
@@ -154,15 +156,19 @@ struct HostIOMMUDeviceIOMMUFDClass {
       *
       * @idev: host IOMMU device backed by IOMMUFD backend.
       *
+     * @pasid: target pasid of detach.
+     *

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 169

Quoted 10 lines
include/system/iommufd.h +2 -1
       * @errp: pass an Error out when attachment fails.
       *
       * Returns: true on success, false on failure.
       */
-    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        Error **errp);
  };
 
  bool

host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 166

Quoted 2 lines
include/system/iommufd.h +0 -2
-                                           uint32_t hwpt_id, Error **errp);
-bool

host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass {

Quoted 3 lines
include/system/iommufd.h +2 -0
+                                           uint32_t pasid, uint32_t hwpt_id,
                                             Error **errp);
+bool

host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

backends/iommufd.c bool

Quoted 7 lines
include/system/iommufd.h +1 -0
+                                           uint32_t pasid, Error **errp);
  #endif
backends/iommufd.c +0 -0
index e1fee16acf..ab612e4874 100644
@@ -539,23 +539,24 @@ bool

iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,

backends/iommufd.c bool · line 541

Quoted 3 lines
backends/iommufd.c +0 -0
  }
 
  bool

host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

backends/iommufd.c bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id, · line 546

Quoted 6 lines
backends/iommufd.c +2 -1
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
  {
      HostIOMMUDeviceIOMMUFDClass *idevc =
          HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
Quoted 2 lines

For consistency with the rest of the code, I would rename all the idev and idevc variables, to hiod and hiodc.

We have used hiod and hiodc for the base HostIOMMUDevice type:

HostIOMMUDevice *hiod;
HostIOMMUDeviceClass *hiodc;

To avoid conflict, what about hiodi and hiodic for HostIOMMUDeviceIOMMUFD type?

Thanks
Zhenzhong

#16 lore raw

Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Quoted 102 lines · Zhenzhong

Hi

Quoted 97 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Monday, March 30, 2026 8:43 PM
To: Duan, Zhenzhong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; clement.mathieu-- [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>; [email protected]
Subject: Re: [PATCH v2 02/14] iommufd: Extend attach/detach_hwpt callbacks to support pasid

Zhenzhong

Quoted 26 lines · Zhenzhong Duan

Same for the two wrappers and their call sites.

---
   include/system/iommufd.h    | 16 +++++++++++-----
   backends/iommufd.c          |  9 +++++----
   hw/arm/smmuv3-accel.c       | 12 ++++++++----
   hw/i386/intel_iommu_accel.c |  8 +++++---
   hw/vfio/iommufd.c           | 10 +++++-----
   5 files changed, 34 insertions(+), 21 deletions(-)
include/system/iommufd.h +2 -0
index 7062944fe6..45a9e87cb0 100644
@@ -138,14 +138,16 @@ struct HostIOMMUDeviceIOMMUFDClass {
        *
        * @idev: host IOMMU device backed by IOMMUFD backend.
        *
+     * @pasid: target pasid of attach.
+     *
        * @hwpt_id: ID of IOMMUFD hardware page table.
        *

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 152

Quoted 10 lines
include/system/iommufd.h +2 -2
        * @errp: pass an Error out when attachment fails.
        *
        * Returns: true on success, false on failure.
        */
-    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
-                        Error **errp);
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        uint32_t hwpt_id, Error **errp);
       /**
        * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware

page table.

Quoted 7 lines
include/system/iommufd.h +2 -0
        * VFIO and VDPA device can have different implementation.
@@ -154,15 +156,19 @@ struct HostIOMMUDeviceIOMMUFDClass {
        *
        * @idev: host IOMMU device backed by IOMMUFD backend.
        *
+     * @pasid: target pasid of detach.
+     *

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 169

Quoted 10 lines
include/system/iommufd.h +2 -1
        * @errp: pass an Error out when attachment fails.
        *
        * Returns: true on success, false on failure.
        */
-    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                        Error **errp);
   };
 
   bool

host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass { · line 166

Quoted 2 lines
include/system/iommufd.h +0 -2
-                                           uint32_t hwpt_id, Error **errp);
-bool

host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass {

Quoted 3 lines
include/system/iommufd.h +2 -0
+                                           uint32_t pasid, uint32_t hwpt_id,
                                              Error **errp);
+bool

host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

backends/iommufd.c bool

Quoted 7 lines
include/system/iommufd.h +1 -0
+                                           uint32_t pasid, Error **errp);
   #endif
backends/iommufd.c +0 -0
index e1fee16acf..ab612e4874 100644
@@ -539,23 +539,24 @@ bool

iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,

backends/iommufd.c bool · line 541

Quoted 3 lines
backends/iommufd.c +0 -0
   }
 
   bool

host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

backends/iommufd.c bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id, · line 546

Quoted 6 lines
backends/iommufd.c +2 -1
-                                           uint32_t hwpt_id, Error **errp)
+                                           uint32_t pasid, uint32_t hwpt_id,
+                                           Error **errp)
   {
       HostIOMMUDeviceIOMMUFDClass *idevc =
           HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
Quoted 2 lines

For consistency with the rest of the code, I would rename all the idev and idevc variables, to hiod and hiodc.

Quoted 7 lines

We have used hiod and hiodc for the base HostIOMMUDevice type:

HostIOMMUDevice *hiod;
HostIOMMUDeviceClass *hiodc;

To avoid conflict, what about hiodi and hiodic for HostIOMMUDeviceIOMMUFD type?

fine with me.

Thanks,

C.

#17 lore raw

PATCH v2 3/14 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 attached translation.

We need to create the nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag because according to uAPI, any domain attached to the non-PASID part of the device must also be flagged, otherwise attaching a PASID will blocked.

Introduce a vfio_device_get_viommu_flags_pasid_supported() helper to facilitate this implementation.

---
 include/hw/vfio/vfio-device.h |  1 +
 hw/vfio/device.c              | 11 +++++++++++
 hw/vfio/iommufd.c             |  8 +++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-device.h +1 -0
index 828a31c006..dd0355eb3d 100644
@@ -268,6 +268,7 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer,
 void vfio_device_unprepare(VFIODevice *vbasedev);
 
 bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev);
+bool vfio_device_get_viommu_flags_pasid_supported(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 973fc35b59..b15ca6ef0a 100644
@@ -533,6 +533,17 @@ bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)
     return false;
 }
 
+bool vfio_device_get_viommu_flags_pasid_supported(VFIODevice *vbasedev)
+{
+    VFIOPCIDevice *vdev = vfio_pci_from_vfio_device(vbasedev);
+
+    if (vdev) {
+        return !!(pci_device_get_viommu_flags(PCI_DEVICE(vdev)) &
+                  VIOMMU_FLAG_PASID_SUPPORTED);
+    }
+    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 e822039858..dce4e4ce72 100644
@@ -363,6 +363,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 */
@@ -408,7 +409,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;
     }
 
@@ -430,6 +431,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         }
     }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(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
#18 lore raw

Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, · line 437

Quoted 8 lines · Zhenzhong Duan
hw/vfio/iommufd.c +4 -0
@@ -430,6 +431,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         }
     }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }

This would set it to:
IOMMU_HWPT_ALLOC_PASID | IOMMU_HWPT_ALLOC_NEST_PARENT
which isn't supported on ARM :-/

The VIOMMU_FLAG_PASID_SUPPORTED flag is to allow PCI core to expose PASID cap. It feels like we need a different VIOMMU flag for VT-d, e.g. VIOMMU_FLAG_WANT_PASID_ATTACH?

Thanks
Nicolin

#19 lore raw

RE: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 13 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Review static bool

Quoted 1 line · Zhenzhong Duan
(quoted diff) +0 -0
@@ -430,6 +431,11 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 3 lines
(quoted diff) +0 -0
         }
     }
 
Quoted 8 lines

hw/vfio/iommufd.c static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, · line 437

Quoted 4 lines
hw/vfio/iommufd.c +4 -0
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }

This would set it to:
IOMMU_HWPT_ALLOC_PASID | IOMMU_HWPT_ALLOC_NEST_PARENT
which isn't supported on ARM :-/

I am a bit confused, if smmu supports dirty tracking, flags would be set to IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT, in arm_smmu_domain_alloc_paging_flags(), I see it will return -EOPNOTSUPP. So how did smmu work in this case?

Quoted 7 lines

The VIOMMU_FLAG_PASID_SUPPORTED flag is to allow PCI core to expose PASID cap. It feels like we need a different VIOMMU flag for VT-d, e.g. VIOMMU_FLAG_WANT_PASID_ATTACH?

Thanks
Nicolin

#20 lore raw

Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 16 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Review static bool

Quoted 1 line · Zhenzhong Duan
(quoted diff) +0 -0
@@ -430,6 +431,11 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 7 lines
hw/vfio/iommufd.c +4 -0
         }
     }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }
Quoted 8 lines
Quoted 3 lines

This would set it to:
IOMMU_HWPT_ALLOC_PASID | IOMMU_HWPT_ALLOC_NEST_PARENT
which isn't supported on ARM :-/

I am a bit confused, if smmu supports dirty tracking, flags would be set to IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT, in arm_smmu_domain_alloc_paging_flags(), I see it will return -EOPNOTSUPP. So how did smmu work in this case?

You hit a point. I almost forgot we need to do something with that dirty tracking flag. This is currently broken..

For NVIDIA, the current generation Grace CPU doesn't support dirty tracking. So, our QEMU VMs don't set that flag. This is just lucky for us. Yet, it would trigger -EOPNOTSUPP on ARM CPU that supports, as you mentioned.

For pasid attachment however, ARM doesn't need it: regular pasid=0 attach already has the pointer to a stage-1 PASID table.

Nicolin

#21 lore raw

RE: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 37 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 16 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Review static bool

Quoted 1 line · Zhenzhong Duan
(quoted diff) +0 -0
@@ -430,6 +431,11 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 7 lines
hw/vfio/iommufd.c +4 -0
         }
     }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }
Quoted 6 lines
Quoted 3 lines

This would set it to:
IOMMU_HWPT_ALLOC_PASID | IOMMU_HWPT_ALLOC_NEST_PARENT
which isn't supported on ARM :-/

I am a bit confused, if smmu supports dirty tracking, flags would be set to IOMMU_HWPT_ALLOC_DIRTY_TRACKING |

IOMMU_HWPT_ALLOC_NEST_PARENT,

Quoted 2 lines

in arm_smmu_domain_alloc_paging_flags(), I see it will return -EOPNOTSUPP. So how did smmu work in this case?

You hit a point. I almost forgot we need to do something with that dirty tracking flag. This is currently broken..

Quoted 7 lines

For NVIDIA, the current generation Grace CPU doesn't support dirty tracking. So, our QEMU VMs don't set that flag. This is just lucky for us. Yet, it would trigger -EOPNOTSUPP on ARM CPU that supports, as you mentioned.

For pasid attachment however, ARM doesn't need it: regular pasid=0 attach already has the pointer to a stage-1 PASID table.

Clear, will add VIOMMU_FLAG_WANT_PASID_ATTACH and check it instead.

Thanks
Zhenzhong

#22 lore raw

Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

In reply to Zhenzhong Duan
Quoted 7 lines · Zhenzhong Duan

When both device and vIOMMU have PASID enabled, then guest may setup pasid attached translation.

We need to create the nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag because according to uAPI, any domain attached to the non-PASID part of the device must also be flagged, otherwise attaching a PASID will blocked.

echo the comment on the commit message.

https://lore.kernel.org/qemu-devel/[email protected]/

#23 lore raw

RE: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 16 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v2 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 7 lines · Zhenzhong Duan

When both device and vIOMMU have PASID enabled, then guest may setup pasid attached translation.

We need to create the nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag because according to uAPI, any domain attached to the non-PASID part of the device must also be flagged, otherwise attaching a PASID will blocked.

Quoted 4 lines

echo the comment on the commit message.

https://lore.kernel.org/qemu-devel/a33c785a-ab94-4dc2-85eb-
[email protected]/

Sorry, seems I missed it. Will use below to replace above paragraph.

"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."

Thanks
Zhenzhong

#24 lore raw

PATCH v2 4/14 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 | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_accel.c +11 -7
index 45c08c8f6f..c2757f3bcd 100644
@@ -69,11 +69,13 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
     return NULL;
 }
 
-static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
                                VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
                                Error **errp)
 {
+    HostIOMMUDeviceIOMMUFD *idev = 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,15 @@ static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
     vtd.pgtbl_addr = (uint64_t)vtd_pe_get_fspt_base(pe);
 
     return iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, idev->hwpt_id,
-                                      0, IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd),
-                                      &vtd, fs_hwpt_id, errp);
+                                      flags, IOMMU_HWPT_DATA_VTD_S1,
+                                      sizeof(vtd), &vtd, fs_hwpt_id, errp);
 }
 
-static void vtd_destroy_old_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
                                     VTDAddressSpace *vtd_as)
 {
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+
     if (!vtd_as->fs_hwpt_id) {
         return;
     }
@@ -116,7 +120,7 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     }
 
     if (vtd_pe_pgtt_is_fst(pe)) {
-        if (!vtd_create_fs_hwpt(idev, pe, &hwpt_id, errp)) {
+        if (!vtd_create_fs_hwpt(vtd_hiod, pe, &hwpt_id, errp)) {
             return false;
         }
     }
@@ -126,7 +130,7 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);
     if (ret) {
         /* Destroy old fs_hwpt if it's a replacement */
-        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        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 +165,7 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     }
 
     if (ret) {
-        vtd_destroy_old_fs_hwpt(idev, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
     }
 
     return ret;
Signature · 1 line
2.47.3
#25 lore raw

PATCH v2 5/14 intel_iommu: Change pasid property from bool to 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 'pasid' property of uint8 type 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 f395fa248c..a7b676cd13 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", 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),
@@ -5042,7 +5042,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;
     }
 }
 
@@ -5583,6 +5584,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, exceed 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
#26 lore raw

Re: [PATCH v2 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 24 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 'pasid' property of uint8 type 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 +0 -0
index 11a53aa369..db4f186a3e 100644

hw/i386/intel_iommu_internal.h line 198

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -1
@@ -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))

does this change still meet the limit commented by "* limit: MemTxAttrs::pid */"?

Quoted 40 lines
hw/i386/intel_iommu_internal.h +0 -0
  #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 +3 -2
index f395fa248c..a7b676cd13 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", 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),
@@ -5042,7 +5042,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;
      }
  }
 

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) · line 5589

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
@@ -5583,6 +5584,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, exceed Max PASID Width %d allowed "
+                   "in PCI spec", s->pasid, PCI_EXT_CAP_PASID_MAX_WIDTH);

s/exceed/exceeds/

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
+        return false;
+    }
+
      if (s->svm) {
          if (!x86_iommu->dt_supported) {
              error_setg(errp, "Need to set device IOTLB for svm");
#27 lore raw

RE: [PATCH v2 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 27 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v2 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 19 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 'pasid' property of uint8 type 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(-)
Quoted 13 lines

hw/i386/intel_iommu_internal.h line 198

Quoted 10 lines
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))

does this change still meet the limit commented by "* limit:
MemTxAttrs::pid */"?

VTD doesn't use MemTxAttrs::pid. Clement asked same question, see my reply at
https://lore.kernel.org/qemu-devel/IA3PR11MB9136B8106B0070F03CF9C32B9261A@IA3PR11MB9136.namprd11.prod.outlook.com/

Quoted 44 lines
Quoted 22 lines
hw/i386/intel_iommu_internal.h +0 -0
  #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 +0 -0
index f395fa248c..a7b676cd13 100644

hw/i386/intel_iommu.c static const Property vtd_properties[] = { · line 4203

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4203,7 +4203,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode,

FALSE),

hw/i386/intel_iommu.c static const Property vtd_properties[] = { · line 4205

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("x-flts", IntelIOMMUState, fsts, FALSE),
      DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control,

false),

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

Quoted 16 lines
hw/i386/intel_iommu.c +3 -2
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", 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),
@@ -5042,7 +5042,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;
      }
  }
 
@@ -5583,6 +5584,12 @@ static bool vtd_decide_config(IntelIOMMUState *s,

Error **errp)

Quoted 8 lines

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) · line 5589

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
          return false;
      }
 
+    if (s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH) {
+        error_setg(errp, "PASID width %d, exceed Max PASID Width %d allowed "
+                   "in PCI spec", s->pasid, PCI_EXT_CAP_PASID_MAX_WIDTH);

s/exceed/exceeds/

Will do.

Thanks
Zhenzhong

#28 lore raw

Re: [PATCH v2 05/14] intel_iommu: Change pasid property from bool to uint8

Hi Zhenzhong,

With Yi's suggestions:

Quoted 116 lines · Zhenzhong
Quoted 29 lines

-----Original Message-----
From: Liu, Yi L <[[email protected]](mailto:[email protected])>
Subject: Re: [PATCH v2 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 19 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 'pasid' property of uint8 type 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(-)
Quoted 14 lines

hw/i386/intel_iommu_internal.h line 198

Quoted 10 lines
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))

does this change still meet the limit commented by "* limit:
MemTxAttrs::pid */"?

VTD doesn't use MemTxAttrs::pid. Clement asked same question, see my reply at
[https://lore.kernel.org/qemu-devel/IA3PR11MB9136B8106B0070F03CF9C32B9261A@IA3PR11MB9136.namprd11.prod.outlook.com/](https://lore.kernel.org/qemu-devel/IA3PR11MB9136B8106B0070F03CF9C32B9261A@IA3PR11MB9136.namprd11.prod.outlook.com/)

Quoted 53 lines
Quoted 22 lines
hw/i386/intel_iommu_internal.h +0 -0
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_PDS                (1ULL << 42)
   #define VTD_ECAP_SMTS               (1ULL << 43)
include/hw/i386/intel_iommu.h +0 -0
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 +0 -0
index f395fa248c..a7b676cd13 100644

hw/i386/intel_iommu.c static const Property vtd_properties[] = { · line 4203

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4203,7 +4203,7 @@ static const Property vtd_properties[] = {
       DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode,

FALSE),

hw/i386/intel_iommu.c static const Property vtd_properties[] = { · line 4205

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
       DEFINE_PROP_BOOL("x-flts", IntelIOMMUState, fsts, FALSE),
       DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control,

false),

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

Quoted 16 lines
hw/i386/intel_iommu.c +3 -2
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", 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),
@@ -5042,7 +5042,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;
       }
   }
 
@@ -5583,6 +5584,12 @@ static bool vtd_decide_config(IntelIOMMUState *s,

Error **errp)

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

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) · line 5589

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+    if (s->pasid > PCI_EXT_CAP_PASID_MAX_WIDTH) {
+        error_setg(errp, "PASID width %d, exceed Max PASID Width %d allowed "
+                   "in PCI spec", s->pasid, PCI_EXT_CAP_PASID_MAX_WIDTH);

s/exceed/exceeds/

Will do.

Thanks
Zhenzhong

#29 lore raw

PATCH v2 6/14 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
#30 lore raw

PATCH v2 7/14 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 PASID_0.

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.

PASID_0 of passthrough device still need to register MRs in case guest does not operate in scalable mode. So for PASID_0, 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    | 170 +++++++++++++++++++++++++++++++++
 4 files changed, 194 insertions(+)
hw/i386/intel_iommu_accel.h +13 -0
index e5f0b077b4..c5981a23bf 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_pasid_cache_sync_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
 static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
 {
 }
hw/i386/intel_iommu_internal.h +8 -0
index c7e107fe87..d5f212ded9 100644
@@ -616,6 +616,7 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
 #define PASID_0                             0
+#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
@@ -646,6 +647,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 */
@@ -711,6 +713,7 @@ typedef struct VTDHostIOMMUDevice {
     PCIBus *bus;
     uint8_t devfn;
     HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
 } VTDHostIOMMUDevice;
 
 /*
@@ -768,6 +771,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 b5d18ae321..451ede7530 100644
@@ -3202,6 +3202,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_pasid_cache_sync_accel(s, pc_info);
 }
 
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4759,6 +4761,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 +170 -0

176 lines not on this page · raw message

Signature · 1 line
2.47.3
#31 lore raw

Re: [PATCH v2 07/14] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 24 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 PASID_0.

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.

Quoted 5 lines

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

perhaps togather with a co-d-b tag, otherwise this s-o-b tag is strange. same to other commits in this series. :)

Quoted 195 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    | 170 +++++++++++++++++++++++++++++++++
  4 files changed, 194 insertions(+)
hw/i386/intel_iommu_accel.h +13 -0
index e5f0b077b4..c5981a23bf 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_pasid_cache_sync_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu_internal.h +8 -0
index c7e107fe87..d5f212ded9 100644
@@ -616,6 +616,7 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
  #define PASID_0                             0
+#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
@@ -646,6 +647,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 */
@@ -711,6 +713,7 @@ typedef struct VTDHostIOMMUDevice {
      PCIBus *bus;
      uint8_t devfn;
      HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
  } VTDHostIOMMUDevice;
 
  /*
@@ -768,6 +771,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 b5d18ae321..451ede7530 100644
@@ -3202,6 +3202,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_pasid_cache_sync_accel(s, pc_info);
  }
 
  static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4759,6 +4761,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 +80 -0
index c2757f3bcd..32d8ab0ef9 100644
@@ -257,6 +257,176 @@ 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;

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+            vtd_sm_pasid_table_walk_one(vtd_hiod, pt_base, pasid, pasid_next,
+                                        info);
+        }
+        pasid = pasid_next;
+    }
+}
+
+static void vtd_replay_pasid_bind_for_dev(VTDHostIOMMUDevice *vtd_hiod,
+                                          int start, int end,
+                                          VTDPASIDCacheInfo *pc_info)

s/vtd_replay_pasid_bind_for_dev/vtd_accel_replay_pasid_bind_for_dev/

Quoted 15 lines
hw/i386/intel_iommu_accel.c +15 -0
+{
+    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);

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    }
+}
+
+/*
+ * 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_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)

I think it's nice to name it as vtd_accel_pasid_cache_sync().

Quoted 31 lines
hw/i386/intel_iommu_accel.c +31 -0
+{
+    int start = PASID_0, end = 1 << s->pasid;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    if (!s->fsts) {
+        return;
+    }
+
+    /*
+     * VTDPASIDCacheInfo honors PCI pasid but VTDAccelPASIDCacheEntry honors
+     * iommu pasid
+     */
+    if (pc_info->pasid == PCI_NO_PASID) {
+        pc_info->pasid = PASID_0;
+    }
+
+    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;

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    default:
+        g_assert_not_reached();
+    }
+
+    /*
+     * In this replay, one only needs to care about the devices which are
+     * backed by host IOMMU. Those devices have a corresponding vtd_hiod
+     * in s->vtd_host_iommu_dev. For devices not backed by host IOMMU, it
+     * is not necessary to replay the bindings since their cache should be
+     * created in the future DMA address translation.

above words can be dropped since this code lays in the _accel.c. Instead, I think you can add words like "Loop all the vtd_hiod instances to sync the "pasid cache" per the guest pasid configuration."

Quoted 17 lines
hw/i386/intel_iommu_accel.c +14 -0
+     *
+     * 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_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)
  {
#32 lore raw

RE: [PATCH v2 07/14] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 32 lines

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

Quoted 24 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 PASID_0.

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.

Quoted 8 lines
Quoted 5 lines

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

perhaps togather with a co-d-b tag, otherwise this s-o-b tag is strange. same to other commits in this series. :)

Sure, will do😊

Quoted 214 lines
Quoted 16 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    | 170 +++++++++++++++++++++++++++++++++
  4 files changed, 194 insertions(+)
hw/i386/intel_iommu_accel.h +0 -0
index e5f0b077b4..c5981a23bf 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_pasid_cache_sync_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu_internal.h +0 -0
index c7e107fe87..d5f212ded9 100644

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -616,6 +616,7 @@ typedef struct VTDRootEntry VTDRootEntry;
  #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
  #define PASID_0                             0
+#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 648

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
@@ -646,6 +647,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 653

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
@@ -711,6 +713,7 @@ typedef struct VTDHostIOMMUDevice {
      PCIBus *bus;
      uint8_t devfn;
      HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
  } VTDHostIOMMUDevice;
 
  /*
@@ -768,6 +771,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 b5d18ae321..451ede7530 100644
@@ -3202,6 +3202,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 3202

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_pasid_cache_sync_accel(s, pc_info);
  }
 
  static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4759,6 +4761,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 c2757f3bcd..32d8ab0ef9 100644
@@ -257,6 +257,176 @@ void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 87 lines
hw/i386/intel_iommu_accel.c +84 -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)
+            && 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;
Quoted 8 lines

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

Quoted 6 lines
hw/i386/intel_iommu_accel.c +6 -0
+    }
+}
+
+static void vtd_replay_pasid_bind_for_dev(VTDHostIOMMUDevice *vtd_hiod,
+                                          int start, int end,
+                                          VTDPASIDCacheInfo *pc_info)

s/vtd_replay_pasid_bind_for_dev/vtd_accel_replay_pasid_bind_for_dev/

An implicit rule is to use vtd_accel_* for all external functions, but for local functions, we don't force it. But I'm fine with vtd_accel_replay_pasid_bind_for_dev if you prefer.

Quoted 29 lines
Quoted 5 lines
hw/i386/intel_iommu_accel.c +4 -0
+{
+    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_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info)

Quoted 1 line

I think it's nice to name it as vtd_accel_pasid_cache_sync().

Sure, will do, this is an external function.

Quoted 39 lines

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

Quoted 12 lines
hw/i386/intel_iommu_accel.c +11 -0
+{
+    int start = PASID_0, end = 1 << s->pasid;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter hiod_it;
+
+    if (!s->fsts) {
+        return;
+    }
+
+    /*
+     * VTDPASIDCacheInfo honors PCI pasid but VTDAccelPASIDCacheEntry

honors

Quoted 26 lines
hw/i386/intel_iommu_accel.c +26 -0
+     * iommu pasid
+     */
+    if (pc_info->pasid == PCI_NO_PASID) {
+        pc_info->pasid = PASID_0;
+    }
+
+    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();
+    }
+
+    /*
+     * In this replay, one only needs to care about the devices which are
Quoted 8 lines

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

Quoted 4 lines
hw/i386/intel_iommu_accel.c +4 -0
+     * backed by host IOMMU. Those devices have a corresponding vtd_hiod
+     * in s->vtd_host_iommu_dev. For devices not backed by host IOMMU, it
+     * is not necessary to replay the bindings since their cache should be
+     * created in the future DMA address translation.

above words can be dropped since this code lays in the _accel.c. Instead, I think you can add words like "Loop all the vtd_hiod instances to sync the "pasid cache" per the guest pasid configuration."

Make sense, will do.

Thanks
Zhenzhong

#33 lore raw

Re: [PATCH v2 07/14] intel_iommu_accel: Handle PASID entry addition for pc_inv_dsc request

Quoted 266 lines · Zhenzhong
Quoted 32 lines

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

Quoted 24 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 PASID_0.

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.

Quoted 8 lines
Quoted 5 lines

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

perhaps togather with a co-d-b tag, otherwise this s-o-b tag is strange. same to other commits in this series. :)

Sure, will do😊

Quoted 214 lines
Quoted 16 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    | 170 +++++++++++++++++++++++++++++++++
   4 files changed, 194 insertions(+)
hw/i386/intel_iommu_accel.h +0 -0
index e5f0b077b4..c5981a23bf 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_pasid_cache_sync_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
+                                              VTDPASIDCacheInfo *pc_info)
+{
+}
+
   static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
   {
   }
hw/i386/intel_iommu_internal.h +0 -0
index c7e107fe87..d5f212ded9 100644

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -616,6 +616,7 @@ typedef struct VTDRootEntry VTDRootEntry;
   #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
   #define PASID_0                             0
+#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 648

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
@@ -646,6 +647,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 653

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
@@ -711,6 +713,7 @@ typedef struct VTDHostIOMMUDevice {
       PCIBus *bus;
       uint8_t devfn;
       HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDAccelPASIDCacheEntry) pasid_cache_list;
   } VTDHostIOMMUDevice;
 
   /*
@@ -768,6 +771,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 b5d18ae321..451ede7530 100644
@@ -3202,6 +3202,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 3202

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_pasid_cache_sync_accel(s, pc_info);
   }
 
   static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4759,6 +4761,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 c2757f3bcd..32d8ab0ef9 100644
@@ -257,6 +257,176 @@ void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

Quoted 87 lines
hw/i386/intel_iommu_accel.c +84 -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)
+            && 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;
Quoted 8 lines

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

Quoted 6 lines
hw/i386/intel_iommu_accel.c +6 -0
+    }
+}
+
+static void vtd_replay_pasid_bind_for_dev(VTDHostIOMMUDevice *vtd_hiod,
+                                          int start, int end,
+                                          VTDPASIDCacheInfo *pc_info)

s/vtd_replay_pasid_bind_for_dev/vtd_accel_replay_pasid_bind_for_dev/

Quoted 2 lines

An implicit rule is to use vtd_accel_* for all external functions, but for local functions, we don't force it. But I'm fine with vtd_accel_replay_pasid_bind_for_dev if you prefer.

yep. Given that we have vtd_replay_pasid_bindings_all() in intel_iommu.c, I think add vtd_accel_ prefix here would help readers.

Regards,
Yi Liu

#34 lore raw

PATCH v2 8/14 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.

This happen before the PASID entry addition, because a new added entry should never be removed.

---
 hw/i386/intel_iommu_accel.c | 75 +++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
hw/i386/intel_iommu_accel.c +75 -0
index 32d8ab0ef9..c1285ce331 100644
@@ -16,6 +16,28 @@
 #include "hw/pci/pci_bus.h"
 #include "trace.h"
 
+static inline 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)
 {
@@ -257,6 +279,52 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                          vtd_flush_host_piotlb_locked, &piotlb_info);
 }
 
+static void vtd_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.
+         */
+        QLIST_REMOVE(vtd_pce, next);
+        g_free(vtd_pce);
+    }
+}
+
+/* Delete invalid pasid cache entry from pasid_cache_list */
+static void vtd_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_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
 static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
                               VTDPASIDEntry *pe)
 {
@@ -423,6 +491,13 @@ void vtd_pasid_cache_sync_accel(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_pasid_cache_invalidate(vtd_hiod, pc_info);
         vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
     }
 }
Signature · 1 line
2.47.3
#35 lore raw

Re: [PATCH v2 08/14] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 6 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.

This happen before the PASID entry addition, because a new added entry should never be removed.

drop above line.

Quoted 64 lines
---
  hw/i386/intel_iommu_accel.c | 75 +++++++++++++++++++++++++++++++++++++
  1 file changed, 75 insertions(+)
hw/i386/intel_iommu_accel.c +44 -0
index 32d8ab0ef9..c1285ce331 100644
@@ -16,6 +16,28 @@
  #include "hw/pci/pci_bus.h"
  #include "trace.h"
 
+static inline 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)
  {
@@ -257,6 +279,52 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                           vtd_flush_host_piotlb_locked, &piotlb_info);
  }
 
+static void vtd_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;
+        }

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    }
+
+    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.
+         */
+        QLIST_REMOVE(vtd_pce, next);
+        g_free(vtd_pce);

could you wrap above two lines into a helper near to the vtd_accel_fill_pc()? Although no other callers, just more readable. :)

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

Quoted 4 lines
hw/i386/intel_iommu_accel.c +4 -0
+    }
+}
+
+/* Delete invalid pasid cache entry from pasid_cache_list */

above comment is not quite necessary.

Quoted 14 lines
hw/i386/intel_iommu_accel.c +10 -0
+static void vtd_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_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
  static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
                                VTDPASIDEntry *pe)
  {

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +7 -0
@@ -423,6 +491,13 @@ void vtd_pasid_cache_sync_accel(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_pasid_cache_invalidate(vtd_hiod, pc_info);

s/vtd_pasid_cache_invalidate/vtd_accel_pasid_cache_invalidate/

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
          vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
      }
  }
#36 lore raw

RE: [PATCH v2 08/14] intel_iommu_accel: Handle PASID entry removal for pc_inv_dsc request

Quoted 8 lines

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

On 3/26/26 17:11, Zhenzhong Duan wrote:

Quoted 8 lines
Quoted 6 lines

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.

This happen before the PASID entry addition, because a new added entry should never be removed.

drop above line.

OK.

Quoted 82 lines
Quoted 4 lines
---
  hw/i386/intel_iommu_accel.c | 75

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

hw/i386/intel_iommu_accel.c line 19

Quoted 11 lines
  1 file changed, 75 insertions(+)
hw/i386/intel_iommu_accel.c +1 -0
index 32d8ab0ef9..c1285ce331 100644
@@ -16,6 +16,28 @@
  #include "hw/pci/pci_bus.h"
  #include "trace.h"
 
+static inline int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry

*vtd_pce,

Quoted 12 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 void

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
                            Error **errp)
  {
@@ -257,6 +279,52 @@ void

vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,

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

Quoted 4 lines
hw/i386/intel_iommu_accel.c +1 -0
                           vtd_flush_host_piotlb_locked, &piotlb_info);
  }
 
+static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry

*vtd_pce,

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

Quoted 7 lines
hw/i386/intel_iommu_accel.c +7 -0
+                                           VTDPASIDCacheInfo *pc_info)
+{
+    VTDPASIDEntry pe;
+    uint16_t did;
+
+    /*
+     * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI

require

Quoted 14 lines
hw/i386/intel_iommu_accel.c +14 -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;
+        }

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    }
+
+    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.
+         */
+        QLIST_REMOVE(vtd_pce, next);
+        g_free(vtd_pce);
Quoted 2 lines

could you wrap above two lines into a helper near to the vtd_accel_fill_pc()? Although no other callers, just more readable. :)

Sure, will do.

Quoted 7 lines

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

Quoted 5 lines
hw/i386/intel_iommu_accel.c +4 -0
+    }
+}
+
+/* Delete invalid pasid cache entry from pasid_cache_list */

above comment is not quite necessary.

Will drop.

Quoted 27 lines

hw/i386/intel_iommu_accel.c void vtd_pasid_cache_sync_accel(IntelIOMMUState

Quoted 15 lines
hw/i386/intel_iommu_accel.c +10 -0
+static void vtd_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_pasid_cache_invalidate_one(vtd_pce, pc_info);
+    }
+}
+
  static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
                                VTDPASIDEntry *pe)
  {
@@ -423,6 +491,13 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +7 -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.
+         */
+        vtd_pasid_cache_invalidate(vtd_hiod, pc_info);
Quoted 1 line

s/vtd_pasid_cache_invalidate/vtd_accel_pasid_cache_invalidate/

OK.

Thanks
Zhenzhong

#37 lore raw

PATCH v2 9/14 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    | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -0
index d5f212ded9..f3cb6cff1c 100644
@@ -631,6 +631,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 +10 -1
index c1285ce331..1e27c0feb8 100644
@@ -311,6 +311,10 @@ static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
          */
         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;
+        }
     }
 }
 
@@ -498,7 +502,12 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
          * removed.
          */
         vtd_pasid_cache_invalidate(vtd_hiod, pc_info);
-        vtd_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_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+        }
     }
 }
 
Signature · 1 line
2.47.3
#38 lore raw

Re: [PATCH v2 09/14] intel_iommu_accel: Bypass PASID entry addition for just deleted entry

Quoted 42 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    | 11 ++++++++++-

 2 files changed, 11 insertions(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -0
index d5f212ded9..f3cb6cff1c 100644  
@@ -631,6 +631,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 +4 -0
index c1285ce331..1e27c0feb8 100644  
@@ -311,6 +311,10 @@ static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,  
           */  
          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;  
+        }  
      }  
  }  

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

Quoted 7 lines
hw/i386/intel_iommu_accel.c +3 -1
@@ -498,7 +502,12 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)  
           * removed.  
           */  
          vtd_pasid_cache_invalidate(vtd_hiod, pc_info);  
-        vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);  
+  
+        if (pc_info->accel_pce_deleted) {  
+            pc_info->accel_pce_deleted = false;  

Are we doing this to make sure that nobody can see the side effect from outside the function?

Quoted 6 lines
hw/i386/intel_iommu_accel.c +3 -0
+        } else {  
+            vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);  
+        }  
      }  
  }  

#39 lore raw

RE: [PATCH v2 09/14] intel_iommu_accel: Bypass PASID entry addition for just deleted entry

Quoted 62 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v2 09/14] intel_iommu_accel: Bypass PASID entry addition for just deleted entry

Quoted 9 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.

Signed-off-by Zhenzhong Duan

<[[email protected]](mailto:[email protected])>

hw/i386/intel_iommu_accel.c static void

Quoted 22 lines

---
 hw/i386/intel_iommu_internal.h |  1 +
 hw/i386/intel_iommu_accel.c    | 11 ++++++++++-

 2 files changed, 11 insertions(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -0
index d5f212ded9..f3cb6cff1c 100644
@@ -631,6 +631,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 +0 -0
index c1285ce331..1e27c0feb8 100644
@@ -311,6 +311,10 @@ static void

vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,

hw/i386/intel_iommu_accel.c void vtd_pasid_cache_sync_accel(IntelIOMMUState

Quoted 11 lines
hw/i386/intel_iommu_accel.c +4 -0
           */
          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;
+        }
      }
  }
 
@@ -498,7 +502,12 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState

*s, VTDPASIDCacheInfo *pc_info)

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

Quoted 7 lines
hw/i386/intel_iommu_accel.c +3 -1
           * removed.
           */
          vtd_pasid_cache_invalidate(vtd_hiod, pc_info);
-        vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+
+        if (pc_info->accel_pce_deleted) {
+            pc_info->accel_pce_deleted = false;
Quoted 2 lines

Are we doing this to make sure that nobody can see the side effect from outside the function?

In each iteration of the while loop, we want to ensure pc_info->accel_pce_deleted is initially false, then vtd_accel_pasid_cache_invalidate() will make decision to set it or not. Then we check it to determine if we need to call vtd_accel_replay_pasid_bind_for_dev().

Thanks
Zhenzhong

Quoted 7 lines
Quoted 7 lines
hw/i386/intel_iommu_accel.c +3 -0
+        } else {
+            vtd_replay_pasid_bind_for_dev(vtd_hiod, start, end, pc_info);
+        }
      }
  }
 
#40 lore raw

PATCH v2 10/14 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 c5981a23bf..1fb7ca0af6 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_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
+void vtd_pasid_cache_reset_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
 {
 }
 
+static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+}
+
 static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
 {
 }
hw/i386/intel_iommu.c +2 -0
index 451ede7530..b022f3cb9e 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_pasid_cache_reset_accel(s);
 }
 
 static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
hw/i386/intel_iommu_accel.c +13 -0
index 1e27c0feb8..e9e67eb1a0 100644
@@ -511,6 +511,19 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
     }
 }
 
+/* Fake a gloal pasid cache invalidation to remove all pasid cache entries */
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type = VTD_INV_DESC_PASIDC_G_GLOBAL };
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;
+
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {
+        vtd_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
Reviewed-by on v2 10/14
#41 lore raw

Re: [PATCH v2 10/14] intel_iommu_accel: Handle PASID entry removal for system reset

Quoted 56 lines · 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 c5981a23bf..1fb7ca0af6 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_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
+void vtd_pasid_cache_reset_accel(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_pasid_cache_sync_accel(IntelIOMMUState *s,
  {
  }
 
+static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+}
+
  static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
  {
  }
hw/i386/intel_iommu.c +2 -0
index 451ede7530..b022f3cb9e 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_pasid_cache_reset_accel(s);
  }
 
  static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
hw/i386/intel_iommu_accel.c +0 -0
index 1e27c0feb8..e9e67eb1a0 100644
@@ -511,6 +511,19 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
      }
  }

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

Quoted 10 lines
hw/i386/intel_iommu_accel.c +9 -0
 
+/* Fake a gloal pasid cache invalidation to remove all pasid cache entries */
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type = VTD_INV_DESC_PASIDC_G_GLOBAL };
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;
+
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {

s/as_it/hiod_it/

other part LGTM.

Quoted 7 lines
hw/i386/intel_iommu_accel.c +4 -0
+        vtd_pasid_cache_invalidate(vtd_hiod, &pc_info);
+    }
+}
+
  static uint64_t vtd_get_host_iommu_quirks(uint32_t type,
                                            void *caps, uint32_t size)
  {
#42 lore raw

Re: [PATCH v2 10/14] intel_iommu_accel: Handle PASID entry removal for system reset

Quoted 52 lines · Yi Liu
Quoted 51 lines · 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 +1 -0
index c5981a23bf..1fb7ca0af6 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_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s);
   void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
   #else

  static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,

hw/i386/intel_iommu_accel.h +4 -0
@@ -62,6 +63,10 @@ static inline void 
 vtd_pasid_cache_sync_accel(IntelIOMMUState *s,
   {
   }
+static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+}
+
   static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
   {
   }
hw/i386/intel_iommu.c +2 -0
index 451ede7530..b022f3cb9e 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_pasid_cache_reset_accel(s);
   }
   static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
hw/i386/intel_iommu_accel.c +0 -0
index 1e27c0feb8..e9e67eb1a0 100644
Quoted 8 lines
Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -0
@@ -511,6 +511,19 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState 
 *s, VTDPASIDCacheInfo *pc_info)
       }
   }
+/* Fake a gloal pasid cache invalidation to remove all pasid cache 

entries */

nit : s/gloal/global/

Quoted 12 lines
Quoted 9 lines
hw/i386/intel_iommu_accel.c +3 -0
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type = 

VTD_INV_DESC_PASIDC_G_GLOBAL };
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;
+
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev); +    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {

s/as_it/hiod_it/

Quoted 3 lines

other part LGTM.

this function is better to be vtd_accel_pasid_cache_reset()

Quoted 8 lines
Quoted 7 lines

+        vtd_pasid_cache_invalidate(vtd_hiod, &pc_info); +    }

hw/i386/intel_iommu_accel.c +2 -0
+}
+

  static uint64_t vtd_get_host_iommu_quirks(uint32_t type, void *caps, uint32_t size) {

#43 lore raw

RE: [PATCH v2 10/14] intel_iommu_accel: Handle PASID entry removal for system reset

Quoted 63 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v2 10/14] intel_iommu_accel: Handle PASID entry removal for system reset

Quoted 55 lines · Yi Liu
Quoted 54 lines · 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 +1 -0
index c5981a23bf..1fb7ca0af6 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_pasid_cache_sync_accel(IntelIOMMUState *s,
 VTDPASIDCacheInfo *pc_info);
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s);
   void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);

  #else
  static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,

hw/i386/intel_iommu_accel.h +4 -0
@@ -62,6 +63,10 @@ static inline void
 vtd_pasid_cache_sync_accel(IntelIOMMUState *s,
   {
   }
+static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+}
+
   static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops)
   {
   }
hw/i386/intel_iommu.c +2 -0
index 451ede7530..b022f3cb9e 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_pasid_cache_reset_accel(s);
   }
   static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
hw/i386/intel_iommu_accel.c +0 -0
index 1e27c0feb8..e9e67eb1a0 100644
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
hw/i386/intel_iommu_accel.c +1 -0
@@ -511,6 +511,19 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState
 *s, VTDPASIDCacheInfo *pc_info)
       }
   }
+/* Fake a gloal pasid cache invalidation to remove all pasid cache

entries */

nit : s/gloal/global/

Good catch, will fix.

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
hw/i386/intel_iommu_accel.c +3 -0
+void vtd_pasid_cache_reset_accel(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .type =

VTD_INV_DESC_PASIDC_G_GLOBAL };
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;
+
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev); +    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {

s/as_it/hiod_it/

Quoted 5 lines
Quoted 3 lines

other part LGTM.

this function is better to be vtd_accel_pasid_cache_reset()

OK.

Thanks
Zhenzhong

#44 lore raw

PATCH v2 11/14 intel_iommu_accel: Support pasid binding/unbinding and PIOTLB flushing

In reply to Zhenzhong Duan

We just switched to use VTDAccelPASIDCacheEntry to cache pasid entry of passthrough device, also need to switch the binding/unbinding and PIOTLB flushing functions to use the same structure.

After the switching, we could remove accel related code from vtd_pasid_cache_[reset/sync]_locked() to make intel_iommu.c cleaner.

The VTDAddressSpace of PASID_0 is still useful as VTD supports a legacy mode which needs shadow page table instead of nested page table.

---
 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   | 125 +++++++++++++++++-----------------
 4 files changed, 64 insertions(+), 82 deletions(-)
hw/i386/intel_iommu_accel.h +1 -1
index 1fb7ca0af6..c72856a8ff 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 b022f3cb9e..f53642a611 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);
         }
     }
 }
@@ -3126,8 +3124,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) {
@@ -3148,9 +3144,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;
     }
 
     /*
@@ -3178,20 +3171,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 +62 -63

234 lines not on this page · raw message

Signature · 1 line
2.47.3
#45 lore raw

Re: [PATCH v2 11/14] intel_iommu_accel: Support pasid binding/unbinding and PIOTLB flushing

Quoted 8 lines · Zhenzhong Duan

We just switched to use VTDAccelPASIDCacheEntry to cache pasid entry of passthrough device, also need to switch the binding/unbinding and PIOTLB flushing functions to use the same structure.

Quoted 1 line

After the switching, we could remove accel related code from

vtd_pasid_cache_[reset/sync]_locked() to make intel_iommu.c cleaner.

The VTDAddressSpace of PASID_0 is still useful as VTD supports a legacy mode which needs shadow page table instead of nested page table.

This patch does quite a few things. But I don't have a good idea to split it. So wish to have a nice description.

FYI.

Subject:
intel_iommu: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation

Commit message:
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 PASID_0 (rid_pasid) and other valid PASIDs, so this switch drops the PASID_0 limitations that existed in the prior PASID bind/unbind and PIOTLB invalidation path. For PASID_0 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.

Quoted 100 lines
---
  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   | 125 +++++++++++++++++-----------------
  4 files changed, 64 insertions(+), 82 deletions(-)
hw/i386/intel_iommu_accel.h +1 -1
index 1fb7ca0af6..c72856a8ff 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 b022f3cb9e..f53642a611 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);
          }
      }
  }
@@ -3126,8 +3124,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) {
@@ -3148,9 +3144,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;
      }
 
      /*
@@ -3178,20 +3171,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 +0 -0
index e9e67eb1a0..26543489fb 100644

hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod, · line 113

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
@@ -111,23 +111,24 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
  }
 
  static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

vtd_hiod can be retrived from vtd_pce?

Quoted 137 lines
hw/i386/intel_iommu_accel.c +29 -54
-                                    VTDAddressSpace *vtd_as)
+                                    VTDAccelPASIDCacheEntry *vtd_pce)
  {
      HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
 
-    if (!vtd_as->fs_hwpt_id) {
+    if (!vtd_pce->fs_hwpt_id) {
          return;
      }
-    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt_id);
-    vtd_as->fs_hwpt_id = 0;
+    iommufd_backend_free_id(idev->iommufd, vtd_pce->fs_hwpt_id);
+    vtd_pce->fs_hwpt_id = 0;
  }
 
-static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
-                                      VTDAddressSpace *vtd_as, Error **errp)
+static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
+                                      Error **errp)
  {
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
-    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
-    uint32_t hwpt_id = idev->hwpt_id;
+    VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
+    uint32_t hwpt_id = idev->hwpt_id, pasid = vtd_pce->pasid;
      bool ret;
 
      /*
@@ -147,14 +148,13 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
          }
      }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id,
-                                                errp);
-    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, pasid, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, pasid, hwpt_id, ret);
      if (ret) {
          /* Destroy old fs_hwpt if it's a replacement */
-        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_pce);
          if (vtd_pe_pgtt_is_fst(pe)) {
-            vtd_as->fs_hwpt_id = hwpt_id;
+            vtd_pce->fs_hwpt_id = hwpt_id;
          }
      } else if (vtd_pe_pgtt_is_fst(pe)) {
          iommufd_backend_free_id(idev->iommufd, hwpt_id);
@@ -163,16 +163,17 @@ static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
      return ret;
  }
 
-static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
-                                      VTDAddressSpace *vtd_as, Error **errp)
+static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
+                                      Error **errp)
  {
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
-    IntelIOMMUState *s = vtd_as->iommu_state;
-    uint32_t pasid = vtd_as->pasid;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
      bool ret;
 
-    if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev, IOMMU_NO_PASID, errp);
+    if (pasid != IOMMU_NO_PASID || (s->dmar_enabled && s->root_scalable)) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, pasid, errp);
          trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
      } else {
          /*
@@ -180,72 +181,47 @@ static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
           * we fallback to the default HWPT which contains shadow page table.
           * So guest DMA could still work.
           */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, pasid,
                                                      idev->hwpt_id, errp);
          trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                             ret);
      }
 
      if (ret) {
-        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_pce);
      }
 
      return ret;
  }
 
-bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
-{
-    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
-
-    /* Ignore emulated device or legacy VFIO backed device */
-    if (!vtd_as->iommu_state->fsts || !vtd_hiod) {
-        return true;
-    }
-
-    if (pc_entry->valid) {
-        return vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
-    }
-
-    return vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
-}
-
  /*
- * This function is a loop function for the s->vtd_address_spaces
- * list with VTDPIOTLBInvInfo as execution filter. It propagates
- * the piotlb invalidation to host.
+ * This function is a loop function for the s->vtd_host_iommu_dev
+ * and vtd_hiod->pasid_cache_list lists with VTDPIOTLBInvInfo as
+ * execution filter. It propagates the piotlb invalidation to host.
   */
-static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
-                                         gpointer user_data)
+static void vtd_flush_host_piotlb(VTDAccelPASIDCacheEntry *vtd_pce,
+                                  VTDPIOTLBInvInfo *piotlb_info)
  {
-    VTDPIOTLBInvInfo *piotlb_info = user_data;
-    VTDAddressSpace *vtd_as = value;
-    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
-    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
      uint16_t did;
 
-    if (!vtd_hiod) {
-        return;
-    }
-
-    assert(vtd_as->pasid == PCI_NO_PASID);
-
      /* Nothing to do if there is no first stage HWPT attached */
-    if (!pc_entry->valid ||

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 216

Quoted 10 lines
hw/i386/intel_iommu_accel.c +3 -3
-        !vtd_pe_pgtt_is_fst(&pc_entry->pasid_entry)) {
+    if (!vtd_pe_pgtt_is_fst(pe)) {
          return;
      }
 
-    did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
+    did = VTD_SM_PASID_ENTRY_DID(pe);
 
-    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == vtd_pce->pasid) {

have you considered to use IOMMU_NO_PASID instead of PASID_0 before
this? When reading this change, I'm wondering why this changes
PASID_0 to vtd_pce->pasid while other parts of this patch changes
IOMMU_NO_PASID to vtd_pce->pasid. I think we've already have the consensus that IOMMU_NO_PASID is 0, so you may have a patch to switch using IOMMU_NO_PASID instead of PASID_0.

Quoted 13 lines
hw/i386/intel_iommu_accel.c +2 -1
          HostIOMMUDeviceIOMMUFD *idev =
              HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
          uint32_t entry_num = 1; /* Only implement one request for simplicity */
          Error *local_err = NULL;
          struct iommu_hwpt_vtd_s1_invalidate *cache = piotlb_info->inv_data;
 
-        if (!iommufd_backend_invalidate_cache(idev->iommufd, vtd_as->fs_hwpt_id,
+        if (!iommufd_backend_invalidate_cache(idev->iommufd,
+                                              vtd_pce->fs_hwpt_id,
                                                IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
                                                sizeof(*cache), &entry_num, cache,
                                                &local_err)) {

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

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
@@ -261,6 +237,8 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
  {
      struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
      VTDPIOTLBInvInfo piotlb_info;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;

s/as_it/hiod_it/

Quoted 74 lines
hw/i386/intel_iommu_accel.c +26 -5
      cache_info.addr = addr;
      cache_info.npages = npages;
@@ -271,12 +249,19 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
      piotlb_info.inv_data = &cache_info;
 
      /*
-     * Go through each vtd_as instance in s->vtd_address_spaces, find out
-     * affected host devices which need host piotlb invalidation. Piotlb
-     * invalidation should check pasid cache per architecture point of view.
+     * Go through each vtd_pce in vtd_hiod->pasid_cache_list for each host
+     * device, find out affected host device pasid which need host piotlb
+     * invalidation. Piotlb invalidation should check pasid cache per
+     * architecture point of view.
       */
-    g_hash_table_foreach(s->vtd_address_spaces,
-                         vtd_flush_host_piotlb_locked, &piotlb_info);
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {
+        VTDAccelPASIDCacheEntry *vtd_pce;
+
+        QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
+            vtd_flush_host_piotlb(vtd_pce, &piotlb_info);
+        }
+    }
  }
 
  static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
@@ -284,6 +269,7 @@ static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
  {
      VTDPASIDEntry pe;
      uint16_t did;
+    Error *local_err = NULL;
 
      /*
       * VTD_INV_DESC_PASIDC_G_DSI and VTD_INV_DESC_PASIDC_G_PASID_SI require
@@ -309,6 +295,9 @@ static void vtd_pasid_cache_invalidate_one(VTDAccelPASIDCacheEntry *vtd_pce,
           * to be either all-zero or non-present. Either case means existing
           * pasid cache should be invalidated.
           */
+        if (!vtd_device_detach_iommufd(vtd_pce, &local_err)) {
+            error_reportf_err(local_err, "%s", "Detaching from HWPT failed: ");
+        }
          QLIST_REMOVE(vtd_pce, next);
          g_free(vtd_pce);
 
@@ -333,11 +322,17 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
                                VTDPASIDEntry *pe)
  {
      VTDAccelPASIDCacheEntry *vtd_pce;
+    Error *local_err = NULL;
 
      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;
+
+                if (!vtd_device_attach_iommufd(vtd_pce, &local_err)) {
+                    error_reportf_err(local_err, "%s",
+                                      "Replacing HWPT attachment failed: ");
+                }
              }
              return;
          }
@@ -348,6 +343,10 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
      vtd_pce->pasid = pasid;
      vtd_pce->pasid_entry = *pe;
      QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
+
+    if (!vtd_device_attach_iommufd(vtd_pce, &local_err)) {
+        error_reportf_err(local_err, "%s", "Attaching to HWPT failed: ");
+    }
  }
 
  /*
#46 lore raw

RE: [PATCH v2 11/14] intel_iommu_accel: Support pasid binding/unbinding and PIOTLB flushing

Quoted 11 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v2 11/14] intel_iommu_accel: Support pasid binding/unbinding and PIOTLB flushing

Quoted 3 lines · Zhenzhong Duan

We just switched to use VTDAccelPASIDCacheEntry to cache pasid entry of passthrough device, also need to switch the binding/unbinding and PIOTLB flushing functions to use the same structure.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

After the switching, we could remove accel related code from

vtd_pasid_cache_[reset/sync]_locked() to make intel_iommu.c cleaner.

The VTDAddressSpace of PASID_0 is still useful as VTD supports a legacy mode which needs shadow page table instead of nested page table.

This patch does quite a few things. But I don't have a good idea to split it. So wish to have a nice description.

Yes

Quoted 7 lines

FYI.

Subject:
intel_iommu: Switch to VTDAccelPASIDCacheEntry for PASID bind/unbind and PIOTLB invalidation

Quoted 13 lines

Commit message:
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 PASID_0 (rid_pasid) and other valid PASIDs, so this switch drops the PASID_0 limitations that existed in the prior PASID bind/unbind and PIOTLB invalidation path. For PASID_0 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.

Very clear, will use it. Thanks!

Quoted 109 lines
Quoted 22 lines
---
  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   | 125 +++++++++++++++++-----------------
  4 files changed, 64 insertions(+), 82 deletions(-)
hw/i386/intel_iommu_accel.h +1 -0
index 1fb7ca0af6..c72856a8ff 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;
 

hw/i386/intel_iommu_accel.h typedef struct VTDAccelPASIDCacheEntry { · line 24

Quoted 1 line
hw/i386/intel_iommu_accel.h +0 -0
@@ -23,7 +24,6 @@ typedef struct VTDAccelPASIDCacheEntry {
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.h typedef struct VTDAccelPASIDCacheEntry {

Quoted 4 lines
hw/i386/intel_iommu_accel.h +0 -1
                            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,

hw/i386/intel_iommu.c static void

Quoted 20 lines
hw/i386/intel_iommu_accel.h +0 -0
                                        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 +0 -0
index b022f3cb9e..f53642a611 100644
@@ -86,8 +86,6 @@ static void

vtd_pasid_cache_reset_locked(IntelIOMMUState *s)

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer

Quoted 9 lines
hw/i386/intel_iommu.c +0 -2
          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);
          }
      }
  }
@@ -3126,8 +3124,6 @@ static void vtd_pasid_cache_sync_locked(gpointer

key, gpointer value,

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer

Quoted 9 lines
hw/i386/intel_iommu.c +0 -2
      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) {
@@ -3148,9 +3144,6 @@ static void vtd_pasid_cache_sync_locked(gpointer

key, gpointer value,

hw/i386/intel_iommu.c static void vtd_pasid_cache_sync_locked(gpointer

Quoted 10 lines
hw/i386/intel_iommu.c +0 -3
              vtd_address_space_unmap(vtd_as, n);
          }
          vtd_switch_address_space(vtd_as);
-
-        err_prefix = "Detaching from HWPT failed: ";
-        goto do_bind_unbind;
      }
 
      /*
@@ -3178,20 +3171,12 @@ static void vtd_pasid_cache_sync_locked(gpointer

key, gpointer value,

Quoted 11 lines
hw/i386/intel_iommu.c +1 -3
      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);

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

Quoted 10 lines
hw/i386/intel_iommu.c +0 -6
      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 static bool

Quoted 5 lines
hw/i386/intel_iommu_accel.c +0 -0
index e9e67eb1a0..26543489fb 100644
@@ -111,23 +111,24 @@ static bool

vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines

hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod, · line 113

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
  }
 
  static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

vtd_hiod can be retrived from vtd_pce?

Yes, will do

Quoted 155 lines

hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -1
-                                    VTDAddressSpace *vtd_as)
+                                    VTDAccelPASIDCacheEntry *vtd_pce)
  {
      HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -3
-    if (!vtd_as->fs_hwpt_id) {
+    if (!vtd_pce->fs_hwpt_id) {
          return;
      }
-    iommufd_backend_free_id(idev->iommufd, vtd_as->fs_hwpt_id);
-    vtd_as->fs_hwpt_id = 0;
+    iommufd_backend_free_id(idev->iommufd, vtd_pce->fs_hwpt_id);

hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +4 -2
+    vtd_pce->fs_hwpt_id = 0;
  }
 
-static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
-                                      VTDAddressSpace *vtd_as, Error **errp)
+static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
+                                      Error **errp)
  {
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c static bool

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -2
-    VTDPASIDEntry *pe = &vtd_as->pasid_cache_entry.pasid_entry;
-    uint32_t hwpt_id = idev->hwpt_id;
+    VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
+    uint32_t hwpt_id = idev->hwpt_id, pasid = vtd_pce->pasid;
      bool ret;
 
      /*
@@ -147,14 +148,13 @@ static bool

vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c static bool · line 150

Quoted 4 lines
hw/i386/intel_iommu_accel.c +0 -1
          }
      }
 
-    ret = host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID,

hwpt_id,

hw/i386/intel_iommu_accel.c static bool

Quoted 15 lines
hw/i386/intel_iommu_accel.c +4 -4
-                                                errp);
-    trace_vtd_device_attach_hwpt(idev->devid, vtd_as->pasid, hwpt_id, ret);
+    ret = host_iommu_device_iommufd_attach_hwpt(idev, pasid, hwpt_id, errp);
+    trace_vtd_device_attach_hwpt(idev->devid, pasid, hwpt_id, ret);
      if (ret) {
          /* Destroy old fs_hwpt if it's a replacement */
-        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_pce);
          if (vtd_pe_pgtt_is_fst(pe)) {
-            vtd_as->fs_hwpt_id = hwpt_id;
+            vtd_pce->fs_hwpt_id = hwpt_id;
          }
      } else if (vtd_pe_pgtt_is_fst(pe)) {
          iommufd_backend_free_id(idev->iommufd, hwpt_id);
@@ -163,16 +163,17 @@ static bool

vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 10 lines
hw/i386/intel_iommu_accel.c +3 -2
      return ret;
  }
 
-static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
-                                      VTDAddressSpace *vtd_as, Error **errp)
+static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
+                                      Error **errp)
  {
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -4
-    IntelIOMMUState *s = vtd_as->iommu_state;
-    uint32_t pasid = vtd_as->pasid;
+    IntelIOMMUState *s = vtd_hiod->iommu_state;
+    uint32_t pasid = vtd_pce->pasid;
      bool ret;
 
-    if (s->dmar_enabled && s->root_scalable) {
-        ret = host_iommu_device_iommufd_detach_hwpt(idev,

IOMMU_NO_PASID, errp);

hw/i386/intel_iommu_accel.c static bool

Quoted 6 lines
hw/i386/intel_iommu_accel.c +2 -0
+    if (pasid != IOMMU_NO_PASID || (s->dmar_enabled && s->root_scalable)) {
+        ret = host_iommu_device_iommufd_detach_hwpt(idev, pasid, errp);
          trace_vtd_device_detach_hwpt(idev->devid, pasid, ret);
      } else {
          /*
@@ -180,72 +181,47 @@ static bool

vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 4 lines
hw/i386/intel_iommu_accel.c +0 -1
           * we fallback to the default HWPT which contains shadow page table.
           * So guest DMA could still work.
           */
-        ret = host_iommu_device_iommufd_attach_hwpt(idev,

IOMMU_NO_PASID,

Quoted 68 lines
hw/i386/intel_iommu_accel.c +11 -36
+        ret = host_iommu_device_iommufd_attach_hwpt(idev, pasid,
                                                      idev->hwpt_id, errp);
          trace_vtd_device_reattach_def_hwpt(idev->devid, pasid, idev->hwpt_id,
                                             ret);
      }
 
      if (ret) {
-        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_as);
+        vtd_destroy_old_fs_hwpt(vtd_hiod, vtd_pce);
      }
 
      return ret;
  }
 
-bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as, Error **errp)
-{
-    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
-    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
-
-    /* Ignore emulated device or legacy VFIO backed device */
-    if (!vtd_as->iommu_state->fsts || !vtd_hiod) {
-        return true;
-    }
-
-    if (pc_entry->valid) {
-        return vtd_device_attach_iommufd(vtd_hiod, vtd_as, errp);
-    }
-
-    return vtd_device_detach_iommufd(vtd_hiod, vtd_as, errp);
-}
-
  /*
- * This function is a loop function for the s->vtd_address_spaces
- * list with VTDPIOTLBInvInfo as execution filter. It propagates
- * the piotlb invalidation to host.
+ * This function is a loop function for the s->vtd_host_iommu_dev
+ * and vtd_hiod->pasid_cache_list lists with VTDPIOTLBInvInfo as
+ * execution filter. It propagates the piotlb invalidation to host.
   */
-static void vtd_flush_host_piotlb_locked(gpointer key, gpointer value,
-                                         gpointer user_data)
+static void vtd_flush_host_piotlb(VTDAccelPASIDCacheEntry *vtd_pce,
+                                  VTDPIOTLBInvInfo *piotlb_info)
  {
-    VTDPIOTLBInvInfo *piotlb_info = user_data;
-    VTDAddressSpace *vtd_as = value;
-    VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as);
-    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
      uint16_t did;
 
-    if (!vtd_hiod) {
-        return;
-    }
-
-    assert(vtd_as->pasid == PCI_NO_PASID);
-
      /* Nothing to do if there is no first stage HWPT attached */
-    if (!pc_entry->valid ||
-        !vtd_pe_pgtt_is_fst(&pc_entry->pasid_entry)) {
+    if (!vtd_pe_pgtt_is_fst(pe)) {
          return;
      }
 
-    did = VTD_SM_PASID_ENTRY_DID(&pc_entry->pasid_entry);
+    did = VTD_SM_PASID_ENTRY_DID(pe);
 
Quoted 9 lines

hw/i386/intel_iommu_accel.c static bool vtd_device_detach_iommufd(VTDHostIOMMUDevice *vtd_hiod, · line 216

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -1
-    if (piotlb_info->domain_id == did && piotlb_info->pasid == PASID_0) {
+    if (piotlb_info->domain_id == did && piotlb_info->pasid == vtd_pce->pasid) {

have you considered to use IOMMU_NO_PASID instead of PASID_0 before this? When reading this change, I'm wondering why this changes PASID_0 to vtd_pce->pasid while other parts of this patch changes IOMMU_NO_PASID to vtd_pce->pasid. I think we've already have the consensus that IOMMU_NO_PASID is 0, so you may have a patch to switch using IOMMU_NO_PASID instead of PASID_0.

Yes, will be in the patch introducing IOMMU_NO_PASID or a new patch.

Quoted 16 lines

hw/i386/intel_iommu_accel.c void vtd_flush_host_piotlb_all_locked(IntelIOMMUState

Quoted 15 lines
hw/i386/intel_iommu_accel.c +2 -1
          HostIOMMUDeviceIOMMUFD *idev =
              HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
          uint32_t entry_num = 1; /* Only implement one request for simplicity */
          Error *local_err = NULL;
          struct iommu_hwpt_vtd_s1_invalidate *cache = piotlb_info->inv_data;
 
-        if (!iommufd_backend_invalidate_cache(idev->iommufd, vtd_as-
 fs_hwpt_id,
+        if (!iommufd_backend_invalidate_cache(idev->iommufd,
+                                              vtd_pce->fs_hwpt_id,
                                                IOMMU_HWPT_INVALIDATE_DATA_VTD_S1,
                                                sizeof(*cache), &entry_num, cache,
                                                &local_err)) {
hw/i386/intel_iommu_accel.c +0 -0
@@ -261,6 +237,8 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState

*s, uint16_t domain_id,

Quoted 7 lines

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

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -0
  {
      struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
      VTDPIOTLBInvInfo piotlb_info;
+    VTDHostIOMMUDevice *vtd_hiod;
+    GHashTableIter as_it;

s/as_it/hiod_it/

OK

Thanks
Zhenzhong

#47 lore raw

PATCH v2 12/14 intel_iommu_accel: drop _lock suffix in vtd_flush_host_piotlb_all_locked()

In reply to Zhenzhong Duan

In order to support PASID, we have switched from looping vtd_as to vtd_hiod, vtd_hiod represents host passthrough device and never deferenced without BQL. So we don't need extra iommu lock to protect it.

---
 hw/i386/intel_iommu_accel.h | 14 +++++++-------
 hw/i386/intel_iommu.c       |  7 ++++---
 hw/i386/intel_iommu_accel.c |  6 +++---
 3 files changed, 14 insertions(+), 13 deletions(-)
hw/i386/intel_iommu_accel.h +7 -7
index c72856a8ff..45a12e0292 100644
@@ -24,9 +24,9 @@ typedef struct VTDAccelPASIDCacheEntry {
 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                           Error **errp);
 VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as);
-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_flush_host_piotlb_all_accel(IntelIOMMUState *s, uint16_t domain_id,
+                                     uint32_t pasid, hwaddr addr,
+                                     uint64_t npages, bool ih);
 void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
 void vtd_pasid_cache_reset_accel(IntelIOMMUState *s);
 void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
@@ -51,10 +51,10 @@ static inline bool vtd_propagate_guest_pasid(VTDAddressSpace *vtd_as,
     return true;
 }
 
-static inline void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s,
-                                                    uint16_t domain_id,
-                                                    uint32_t pasid, hwaddr addr,
-                                                    uint64_t npages, bool ih)
+static inline void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
+                                                   uint16_t domain_id,
+                                                   uint32_t pasid, hwaddr addr,
+                                                   uint64_t npages, bool ih)
 {
 }
 
hw/i386/intel_iommu.c +4 -3
index f53642a611..5d0184fa0d 100644
@@ -3011,11 +3011,11 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
     info.domain_id = domain_id;
     info.pasid = pasid;
 
+    vtd_flush_host_piotlb_all_accel(s, domain_id, pasid, 0, (uint64_t)-1,
+                                     false);
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
                                 &info);
-    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, 0, (uint64_t)-1,
-                                     false);
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
@@ -3045,10 +3045,11 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     info.addr = addr;
     info.mask = ~((1 << am) - 1);
 
+    vtd_flush_host_piotlb_all_accel(s, domain_id, pasid, addr, 1 << am, ih);
+
     vtd_iommu_lock(s);
     g_hash_table_foreach_remove(s->iotlb,
                                 vtd_hash_remove_by_page_piotlb, &info);
-    vtd_flush_host_piotlb_all_locked(s, domain_id, pasid, addr, 1 << am, ih);
     vtd_iommu_unlock(s);
 
     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, pasid);
hw/i386/intel_iommu_accel.c +3 -3
index 26543489fb..2fd26690b9 100644
@@ -231,9 +231,9 @@ static void vtd_flush_host_piotlb(VTDAccelPASIDCacheEntry *vtd_pce,
     }
 }
 
-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_flush_host_piotlb_all_accel(IntelIOMMUState *s, uint16_t domain_id,
+                                     uint32_t pasid, hwaddr addr,
+                                     uint64_t npages, bool ih)
 {
     struct iommu_hwpt_vtd_s1_invalidate cache_info = { 0 };
     VTDPIOTLBInvInfo piotlb_info;
Signature · 1 line
2.47.3
#48 lore raw

PATCH v2 13/14 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.

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

---
 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 f3cb6cff1c..d11064b527 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_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 2fd26690b9..e73695ff83 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_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
#49 lore raw

Re: [PATCH v2 13/14] intel_iommu_accel: Add pasid bits size check

Quoted 4 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.

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

this has been enforced in the prior patch. right? hence just drop it. Or you may be merge them into one patch.

Quoted 12 lines
---
  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 +0 -0
index f3cb6cff1c..d11064b527 100644

hw/i386/intel_iommu_internal.h line 199

Quoted 4 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -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_PSS(ecap)          extract64(ecap, 35, 5)

perhaps VTD_ECAP_GET_PSS. I'm also wondering if we should have a set of macros to get other caps as well. Like "s->ecap & VTD_ECAP_SMTS" may be VTD_EAP_GET_SMTS(s->ecap, VTD_ECAP_SMTS)".

Quoted 29 lines
hw/i386/intel_iommu_internal.h +0 -0
  #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 2fd26690b9..e73695ff83 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_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");
#50 lore raw

RE: [PATCH v2 13/14] intel_iommu_accel: Add pasid bits size check

Quoted 6 lines

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

Quoted 8 lines
Quoted 4 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.

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

this has been enforced in the prior patch. right? hence just drop it. Or you may be merge them into one patch.

Yes, will delete it.

Quoted 8 lines
Quoted 8 lines
---
  hw/i386/intel_iommu_internal.h | 1 +
  hw/i386/intel_iommu_accel.c    | 8 ++++++++
  2 files changed, 9 insertions(+)
Quoted 13 lines

hw/i386/intel_iommu_internal.h line 199

Quoted 9 lines
hw/i386/intel_iommu_internal.h +1 -0
index f3cb6cff1c..d11064b527 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_PSS(ecap)          extract64(ecap, 35, 5)

perhaps VTD_ECAP_GET_PSS. I'm also wondering if we should have a set of macros to get other caps as well. Like "s->ecap & VTD_ECAP_SMTS" may be VTD_EAP_GET_SMTS(s->ecap, VTD_ECAP_SMTS)".

Currently VTD_ECAP_PSS is an implicit VTD_ECAP_GET_PSS like other definitions.

I'm not sure if we should follow arm's style to use VTD_ECAP_GET_PSS or vtd's style to use VTD_ECAP_PSS.

Thanks
Zhenzhong

Quoted 32 lines

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

Quoted 9 lines
hw/i386/intel_iommu_internal.h +0 -0
  #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 2fd26690b9..e73695ff83 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_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 13 lines
hw/i386/intel_iommu_accel.c +7 -0
          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");
#51 lore raw

Re: [PATCH v2 13/14] intel_iommu_accel: Add pasid bits size check

Quoted 39 lines · Zhenzhong
Quoted 6 lines

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

Quoted 8 lines
Quoted 4 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.

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

this has been enforced in the prior patch. right? hence just drop it. Or you may be merge them into one patch.

Yes, will delete it.

Quoted 8 lines
Quoted 8 lines
---
   hw/i386/intel_iommu_internal.h | 1 +
   hw/i386/intel_iommu_accel.c    | 8 ++++++++
   2 files changed, 9 insertions(+)
Quoted 13 lines

hw/i386/intel_iommu_internal.h line 199

Quoted 9 lines
hw/i386/intel_iommu_internal.h +1 -0
index f3cb6cff1c..d11064b527 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_PSS(ecap)          extract64(ecap, 35, 5)

perhaps VTD_ECAP_GET_PSS. I'm also wondering if we should have a set of macros to get other caps as well. Like "s->ecap & VTD_ECAP_SMTS" may be VTD_EAP_GET_SMTS(s->ecap, VTD_ECAP_SMTS)".

Quoted 3 lines

Currently VTD_ECAP_PSS is an implicit VTD_ECAP_GET_PSS like other definitions.

I'm not sure if we should follow arm's style to use VTD_ECAP_GET_PSS or vtd's style to use VTD_ECAP_PSS.

Hmmm. The existing list is already a mixture.. Some covers both SET and
GET depending on how caller uses it, while some only covers SET (like
VTD_ECAP_MHMV and VTD_ECAP_IRO). Then adding the PSS macros here is fine. You may use a explicit GET/SET for PSS if you don't have a good idea. :)

/* ECAP_REG */
/* (offset >> 4) << 8 */

#define VTD_ECAP_IRO                (DMAR_IOTLB_REG_OFFSET << 4)
#define VTD_ECAP_QI                 (1ULL << 1)
#define VTD_ECAP_DT                 (1ULL << 2)
/* Interrupt Remapping support */
#define VTD_ECAP_IR                 (1ULL << 3)
#define VTD_ECAP_EIM                (1ULL << 4)
#define VTD_ECAP_PT                 (1ULL << 6)
#define VTD_ECAP_SC                 (1ULL << 7)
#define VTD_ECAP_PRS                (1ULL << 29)
#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_PASID              (1ULL << 40)
#define VTD_ECAP_PDS                (1ULL << 42)
#define VTD_ECAP_SMTS               (1ULL << 43)
#define VTD_ECAP_SSTS               (1ULL << 46)
#define VTD_ECAP_FSTS               (1ULL << 47)
Quoted 34 lines
Quoted 33 lines

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

Quoted 10 lines
hw/i386/intel_iommu_internal.h +0 -0
   #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 2fd26690b9..e73695ff83 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_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 13 lines
hw/i386/intel_iommu_accel.c +7 -0
           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");
#52 lore raw

RE: [PATCH v2 13/14] intel_iommu_accel: Add pasid bits size check

Quoted 51 lines

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

Quoted 19 lines · Zhenzhong
Quoted 6 lines

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

Quoted 8 lines
Quoted 4 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.

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

this has been enforced in the prior patch. right? hence just drop it. Or you may be merge them into one patch.

Yes, will delete it.

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
---
   hw/i386/intel_iommu_internal.h | 1 +
   hw/i386/intel_iommu_accel.c    | 8 ++++++++
   2 files changed, 9 insertions(+)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu_internal.h line 198

Quoted 7 lines

index f3cb6cff1c..d11064b527 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -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))

Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu_internal.h line 199

Quoted 1 line
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_ECAP_PSS(ecap)          extract64(ecap, 35, 5)

perhaps VTD_ECAP_GET_PSS. I'm also wondering if we should have a set of macros to get other caps as well. Like "s->ecap & VTD_ECAP_SMTS" may be VTD_EAP_GET_SMTS(s->ecap, VTD_ECAP_SMTS)".

Currently VTD_ECAP_PSS is an implicit VTD_ECAP_GET_PSS like other

definitions.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

I'm not sure if we should follow arm's style to use VTD_ECAP_GET_PSS or vtd's

style to use VTD_ECAP_PSS.

Hmmm. The existing list is already a mixture.. Some covers both SET and GET depending on how caller uses it, while some only covers SET (like VTD_ECAP_MHMV and VTD_ECAP_IRO). Then adding the PSS macros here is fine. You may use a explicit GET/SET for PSS if you don't have a good idea. :)

OK, will use GET/SET pair for PSS.

Thanks
Zhenzhong

Quoted 57 lines

/* ECAP_REG */
/* (offset >> 4) << 8 */

#define VTD_ECAP_IRO                (DMAR_IOTLB_REG_OFFSET << 4)
#define VTD_ECAP_QI                 (1ULL << 1)
#define VTD_ECAP_DT                 (1ULL << 2)
/* Interrupt Remapping support */
#define VTD_ECAP_IR                 (1ULL << 3)
#define VTD_ECAP_EIM                (1ULL << 4)
#define VTD_ECAP_PT                 (1ULL << 6)
#define VTD_ECAP_SC                 (1ULL << 7)
#define VTD_ECAP_PRS                (1ULL << 29)
#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_PASID              (1ULL << 40)
#define VTD_ECAP_PDS                (1ULL << 42)
#define VTD_ECAP_SMTS               (1ULL << 43)
#define VTD_ECAP_SSTS               (1ULL << 46)
#define VTD_ECAP_FSTS               (1ULL << 47)
Quoted 34 lines
Quoted 33 lines

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

Quoted 10 lines
hw/i386/intel_iommu_internal.h +0 -0
   #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 2fd26690b9..e73695ff83 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_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 13 lines
hw/i386/intel_iommu_accel.c +7 -0
           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");
#53 lore raw

PATCH v2 14/14 intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED when configured

In reply to Zhenzhong Duan

VFIO device will check flag VIOMMU_FLAG_PASID_SUPPORTED and expose PASID capability, or else guest could not enable PASID of this device even if vIOMMU's pasid is configured.

This is the final knob to enable PASID.

---
 hw/i386/intel_iommu.c | 1 +
 1 file changed, 1 insertion(+)
hw/i386/intel_iommu.c +1 -0
index 5d0184fa0d..96b4102ab9 100644
@@ -4795,6 +4795,7 @@ static uint64_t vtd_get_viommu_flags(void *opaque)
     uint64_t flags;
 
     flags = s->fsts ? VIOMMU_FLAG_WANT_NESTING_PARENT : 0;
+    flags |= s->pasid ? VIOMMU_FLAG_PASID_SUPPORTED : 0;
 
     return flags;
 }
Signature · 1 line
2.47.3
Tested-by on the v2 series
#54 lore raw
In reply to Zhenzhong Duan
Quoted 131 lines

-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Sent: Thursday, March 26, 2026 5:11 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH v2 00/14] 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 PASID_0.

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 PASID_0.

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;

GIT branch:
https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_pasid

PATCH01-06: Some preparing work
PATCH07-10: Handle PASID entry addition and removal
PATCH11-12: Support pasid binding and unbinding
PATCH13-14: Add PASID related check and enable PASID for passthrough device

This patchset depends on a kernel feature enhancement[1] to work.

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

Thanks
Zhenzhong

[1] https://lore.kernel.org/all/20260205023405.41583-1-
[email protected]/

Changelog:
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 (14):
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: Change pasid property from bool to uint8
intel_iommu: Export some functions
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: Support pasid binding/unbinding and PIOTLB flushing
intel_iommu_accel: drop _lock suffix in
vtd_flush_host_piotlb_all_locked()
intel_iommu_accel: Add pasid bits size check
intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED when
configured

 hw/i386/intel_iommu_accel.h    |  34 ++-
 hw/i386/intel_iommu_internal.h |  43 +++-
 include/hw/core/iommu.h        |   2 +
 include/hw/i386/intel_iommu.h  |   4 +-
 include/hw/vfio/vfio-device.h  |   1 +
 include/system/iommufd.h       |  16 +-
 backends/iommufd.c             |   9 +-
 hw/arm/smmuv3-accel.c          |  12 +-
 hw/i386/intel_iommu.c          |  83 +++----
 hw/i386/intel_iommu_accel.c    | 420 +++++++++++++++++++++++++++------
 hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  56 +++--
 hw/vfio/trace-events           |   4 +-
 13 files changed, 524 insertions(+), 171 deletions(-)
Quoted 2 lines

--
2.47.3

DSA PF assignment to VM with vIOMMU scalable mode, dmatest passed in VM.