Skip to contentSkip to conversation

intel_iommu: Enable PASID support for passthrough device

RFC PATCH Superseded Zhenzhong Duan 5 participants 39 messages Feb 5 – Feb 14

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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 pe;
      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-09: Handle PASID entry addition and removal
PATCH10-11: Support pasid binding and unbinding
PATCH12-13: Add PASID related check and drop useless check
PATCH14:    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]/

Zhenzhong Duan (14):
backends/iommufd: Add pasid attach/detach callbacks vfio/iommufd: Implement pasid attach/detach callbacks in
TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO
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: Handle PASID entry addition for pc_inv_dsc request
intel_iommu: Handle PASID entry removal for pc_inv_dsc request
intel_iommu: 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: Drop pasid related check
intel_iommu: Expose flag VIOMMU_FLAG_PASID_SUPPORTED when configured

 hw/i386/intel_iommu_accel.h    |  34 ++-
 hw/i386/intel_iommu_internal.h |  41 +++-
 include/hw/i386/intel_iommu.h  |   4 +-
 include/hw/vfio/vfio-device.h  |   1 +
 include/system/iommufd.h       |  40 ++++
 backends/iommufd.c             |  23 ++
 hw/i386/intel_iommu.c          |  87 ++-----
 hw/i386/intel_iommu_accel.c    | 405 +++++++++++++++++++++++++++------
 hw/vfio/device.c               |  11 +
 hw/vfio/iommufd.c              |  55 ++++-
 hw/vfio/trace-events           |   2 +
 11 files changed, 558 insertions(+), 145 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

RFC PATCH 1/14 backends/iommufd: Add pasid attach/detach callbacks

Add two wrappers host_iommu_device_iommufd_pasid_[at|de]tach_hwpt to wrap the two callbacks.

Use assert to ensure the corresponding callbacks exist.

---
 include/system/iommufd.h | 40 ++++++++++++++++++++++++++++++++++++++++
 backends/iommufd.c       | 23 +++++++++++++++++++++++
 2 files changed, 63 insertions(+)
include/system/iommufd.h +40 -0
index 80d72469a9..615be10aed 100644
@@ -145,10 +145,50 @@ struct HostIOMMUDeviceIOMMUFDClass {
      * Returns: true on success, false on failure.
      */
     bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    /**
+     * @attach_hwpt: attach host IOMMU device's pasid to IOMMUFD hardware page
+     * table. VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @pasid: pasid of host IOMMU device.
+     *
+     * @hwpt_id: ID of IOMMUFD hardware page table.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*pasid_attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t pasid,
+                              uint32_t hwpt_id, Error **errp);
+    /**
+     * @detach_hwpt: detach host IOMMU device's from IOMMUFD hardware page
+     * table. VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @pasid: pasid of host IOMMU device.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*pasid_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,
                                            Error **errp);
+bool host_iommu_device_iommufd_pasid_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid,
+                                                 uint32_t hwpt_id,
+                                                 Error **errp);
+bool host_iommu_device_iommufd_pasid_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid, Error **errp);
 #endif
backends/iommufd.c +23 -0
index 13822df82f..93d3612471 100644
@@ -539,6 +539,29 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
     }
 }
 
+bool host_iommu_device_iommufd_pasid_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid,
+                                                 uint32_t hwpt_id,
+                                                 Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->pasid_attach_hwpt);
+    return idevc->pasid_attach_hwpt(idev, pasid, hwpt_id, errp);
+}
+
+bool host_iommu_device_iommufd_pasid_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid,
+                                                 Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->pasid_detach_hwpt);
+    return idevc->pasid_detach_hwpt(idev, pasid, errp);
+}
+
 static bool hiod_iommufd_get_pasid_info(HostIOMMUDevice *hiod,
                                         PasidInfo *pasid_info)
 {
Signature · 1 line
2.47.3
#2 lore raw

RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 36 lines

-----Original Message-----
From: Zhenzhong Duan <[email protected]>
Sent: 05 February 2026 03:11
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Jason Gunthorpe <[email protected]>; Nicolin Chen <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhenzhong Duan <[email protected]>
Subject: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

External email: Use caution opening links or attachments

Add two wrappers host_iommu_device_iommufd_pasid_[at|de]tach_hwpt to wrap the two callbacks.

Use assert to ensure the corresponding callbacks exist.

---
 include/system/iommufd.h | 40
++++++++++++++++++++++++++++++++++++++++
 backends/iommufd.c       | 23 +++++++++++++++++++++++
 2 files changed, 63 insertions(+)
include/system/iommufd.h +0 -0
index 80d72469a9..615be10aed 100644

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

Quoted 5 lines
include/system/iommufd.h +2 -0
@@ -145,10 +145,50 @@ struct HostIOMMUDeviceIOMMUFDClass {
      * Returns: true on success, false on failure.
      */
     bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    /**
+     * @attach_hwpt: attach host IOMMU device's pasid to IOMMUFD

s/@attach_hwpt/@pasid_attach_hwpt/

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

Quoted 4 lines

hardware page

include/system/iommufd.h +3 -0
+     * table. VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.

This currently appears Intel specific and calling it "Mandatory" may be confusing. Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Quoted 13 lines
include/system/iommufd.h +12 -0
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @pasid: pasid of host IOMMU device.
+     *
+     * @hwpt_id: ID of IOMMUFD hardware page table.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*pasid_attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t

pasid,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass {

Quoted 3 lines
include/system/iommufd.h +3 -0
+                              uint32_t hwpt_id, Error **errp);
+    /**
+     * @detach_hwpt: detach host IOMMU device's from IOMMUFD hardware

s/@detach_hwpt/@pasid_detach_hwpt/

Also comment needs update?

Thanks,
Shameer

#3 lore raw

RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 30 lines

-----Original Message-----
From: Shameer Kolothum Thodi <[email protected]>
Subject: RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 21 lines

-----Original Message-----
From: Zhenzhong Duan <[email protected]>
Subject: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

External email: Use caution opening links or attachments

Add two wrappers host_iommu_device_iommufd_pasid_[at|de]tach_hwpt to wrap the two callbacks.

Use assert to ensure the corresponding callbacks exist.

---
 include/system/iommufd.h | 40
++++++++++++++++++++++++++++++++++++++++
 backends/iommufd.c       | 23 +++++++++++++++++++++++
 2 files changed, 63 insertions(+)
Quoted 12 lines

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

Quoted 10 lines
include/system/iommufd.h +2 -0
index 80d72469a9..615be10aed 100644
@@ -145,10 +145,50 @@ struct HostIOMMUDeviceIOMMUFDClass {
      * Returns: true on success, false on failure.
      */
     bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, Error **errp);
+    /**
+     * @attach_hwpt: attach host IOMMU device's pasid to IOMMUFD

s/@attach_hwpt/@pasid_attach_hwpt/

Oh, will fix, missed it after copy/paste😊

Quoted 8 lines

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

Quoted 5 lines

hardware page

include/system/iommufd.h +3 -0
+     * table. VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.

This currently appears Intel specific and calling it "Mandatory" may be confusing.

Same here, will fix.

Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Quoted 18 lines
Quoted 14 lines
include/system/iommufd.h +12 -0
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @pasid: pasid of host IOMMU device.
+     *
+     * @hwpt_id: ID of IOMMUFD hardware page table.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*pasid_attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t

pasid,

include/system/iommufd.h struct HostIOMMUDeviceIOMMUFDClass {

Quoted 3 lines
include/system/iommufd.h +3 -0
+                              uint32_t hwpt_id, Error **errp);
+    /**
+     * @detach_hwpt: detach host IOMMU device's from IOMMUFD hardware
Quoted 3 lines

s/@detach_hwpt/@pasid_detach_hwpt/

Also comment needs update?

Yes, will fix that.

Thanks
Zhenzhong

#4 lore raw

RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 15 lines

-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Sent: 11 February 2026 10:38
To: Shameer Kolothum Thodi <[email protected]>; qemu- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Jason Gunthorpe <[email protected]>; Nicolin Chen <[email protected]>; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>
Subject: RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

[...]

Quoted 6 lines
Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

Quoted 8 lines

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Since PASID attach/detach is essentially an extension of the existing attach/detach flow, I would prefer extending the current callbacks. I think that keeps the interface simpler and avoids code duplication.

Thanks,
Shameer

#5 lore raw

Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 9 lines · Shameer Kolothum Thodi
Quoted 9 lines
Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

The RID attach/detach could be just pasid=IOMMU_NO_PASID without that "has_pasid"?

Quoted 8 lines
Quoted 4 lines

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Since PASID attach/detach is essentially an extension of the existing attach/detach flow, I would prefer extending the current callbacks. I think that keeps the interface simpler and avoids code duplication.

+1

Nicolin

#6 lore raw

RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 14 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 6 lines · Shameer Kolothum Thodi
Quoted 6 lines
Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

The RID attach/detach could be just pasid=IOMMU_NO_PASID without that "has_pasid"?

After further thinking, yes, we could.

IOMMU_NO_PASID isn't export as UAPI, we need to define it in QEMU.

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Since PASID attach/detach is essentially an extension of the existing attach/detach flow, I would prefer extending the current callbacks. I think that keeps the interface simpler and avoids code duplication.

+1

Ok, wll extend attach/detach_hwpt(). Thanks all.

#7 lore raw

Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

What about PCI_NO_PASID?

________________________________
From: Duan, Zhenzhong <[email protected]>
Sent: 12 February 2026 03:43
To: Nicolin Chen <[email protected]>; Shameer Kolothum Thodi <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected] <[email protected]>; CLEMENT MATHIEU--DRIF <[email protected]>; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>
Subject: RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 12 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 6 lines · Shameer Kolothum Thodi
Quoted 6 lines
Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

The RID attach/detach could be just pasid=IOMMU_NO_PASID without that "has_pasid"?

After further thinking, yes, we could.

IOMMU_NO_PASID isn't export as UAPI, we need to define it in QEMU.

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Since PASID attach/detach is essentially an extension of the existing attach/detach flow, I would prefer extending the current callbacks. I think that keeps the interface simpler and avoids code duplication.

+1

Ok, wll extend attach/detach_hwpt(). Thanks all.

#8 lore raw

RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

We could use PCI_NO_PASID (-1) but need to translate from PASID_0 to PCI_NO_PASID in intel_iommu, IOMMU_NO_PASID(0) which equals PASID_0 looks more straight-forward for me,
I plan to define it in include/hw/core/iommu.h

From: CLEMENT MATHIEU--DRIF <[email protected]>
Sent: Thursday, February 12, 2026 2:45 PM
To: Duan, Zhenzhong <[email protected]>; Nicolin Chen <[email protected]>; Shameer Kolothum Thodi <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Jason Gunthorpe <[email protected]>; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>
Subject: Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

What about PCI_NO_PASID?

________________________________
From: Duan, Zhenzhong <[email protected]<mailto:[email protected]>>
Sent: 12 February 2026 03:43
To: Nicolin Chen <[email protected]<mailto:[email protected]>>; Shameer Kolothum Thodi <[email protected]<mailto:[email protected]>>
Cc: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; Jason Gunthorpe <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; CLEMENT MATHIEU--DRIF <[email protected]<mailto:[email protected]>>; Tian, Kevin <[email protected]<mailto:[email protected]>>; Liu, Yi L <[email protected]<mailto:[email protected]>>; Hao, Xudong <[email protected]<mailto:[email protected]>>
Subject: RE: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 12 lines

-----Original Message-----
From: Nicolin Chen <[email protected]<mailto:[email protected]>>
Subject: Re: [RFC PATCH 01/14] backends/iommufd: Add pasid attach/detach callbacks

Quoted 6 lines · Shameer Kolothum Thodi
Quoted 6 lines
Quoted 2 lines

Have you considered extending attach_hwpt() to take a PASID instead of introducing a separate callback?

Yes, I did. To extending attach/detach_hwpt(), we need to add two parameters, "bool has_pasid, uint32_t pasid". E.g.,

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev,
bool has_pasid, uint32_t pasid, uint32_t hwpt_id, Error **errp);

The RID attach/detach could be just pasid=IOMMU_NO_PASID without that "has_pasid"?

After further thinking, yes, we could.

IOMMU_NO_PASID isn't export as UAPI, we need to define it in QEMU.

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

and refactor iommufd_cdev_attach/detach_ioas_hwpt() a bit.

I'm fine either way, two separate callbacks just look cleaner for me. Let me know if extending is preferred.

Since PASID attach/detach is essentially an extension of the existing attach/detach flow, I would prefer extending the current callbacks. I think that keeps the interface simpler and avoids code duplication.

+1

Ok, wll extend attach/detach_hwpt(). Thanks all.

#9 lore raw

RFC PATCH 2/14 vfio/iommufd: Implement pasid attach/detach callbacks in TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO

In reply to Zhenzhong Duan

2 new class functions are pasid_[at|de]tach_hwpt(). They are used to attach/detach pasid to/from hwpt. VFIO and VDPA can have different implementions, so implementation will be in sub-class instead of HostIOMMUDeviceIOMMUFD, e.g., in HostIOMMUDeviceIOMMUFDVFIO.

---
 hw/vfio/iommufd.c    | 47 ++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events |  2 ++
 2 files changed, 49 insertions(+)
hw/vfio/iommufd.c +47 -0
index 131612eb83..96735f1fc8 100644
@@ -931,6 +931,51 @@ host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
     return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp);
 }
 
+static bool
+host_iommu_device_iommufd_vfio_pasid_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid,
+                                                 uint32_t hwpt_id,
+                                                 Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+    struct vfio_device_attach_iommufd_pt attach = {
+        .argsz = sizeof(attach),
+        .flags = VFIO_DEVICE_ATTACH_PASID,
+        .pasid = pasid,
+        .pt_id = hwpt_id,
+    };
+
+    if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach)) {
+        error_setg_errno(errp, errno, "error attach %s pasid %d to id=%d",
+                         vbasedev->name, pasid, hwpt_id);
+        return false;
+    }
+
+    trace_hiod_iommufd_vfio_pasid_attach_hwpt(vbasedev->name, pasid, hwpt_id);
+    return true;
+}
+
+static bool
+host_iommu_device_iommufd_vfio_pasid_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                                 uint32_t pasid, Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+    struct vfio_device_detach_iommufd_pt detach = {
+        .argsz = sizeof(detach),
+        .pasid = pasid,
+        .flags = VFIO_DEVICE_DETACH_PASID,
+    };
+
+    if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach)) {
+        error_setg_errno(errp, errno, "detach %s pasid %d failed",
+                         vbasedev->name, pasid);
+        return false;
+    }
+
+    trace_hiod_iommufd_vfio_pasid_detach_hwpt(vbasedev->name, pasid);
+    return true;
+}
+
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
                                       Error **errp)
 {
@@ -993,6 +1038,8 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, const void *data)
 
     idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt;
     idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt;
+    idevc->pasid_attach_hwpt = host_iommu_device_iommufd_vfio_pasid_attach_hwpt;
+    idevc->pasid_detach_hwpt = host_iommu_device_iommufd_vfio_pasid_detach_hwpt;
 };
 
 static const TypeInfo types[] = {
hw/vfio/trace-events +2 -0
index 846e3625c5..2af6f46014 100644
@@ -188,6 +188,8 @@ 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"
 iommufd_cdev_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int dev_id) "\t%04x:%02x:%02x.%x devid %d"
+hiod_iommufd_vfio_pasid_attach_hwpt(const char *name, uint32_t paisd, uint32_t hwpt_id) " Successfully attached device %s pasid %d to id=%d"
+hiod_iommufd_vfio_pasid_detach_hwpt(const char *name, uint32_t paisd) " Successfully detached device %s pasid %d"
 
 # cpr-iommufd.c
 vfio_cpr_find_device(uint32_t ioas_id, int devid, uint32_t hwpt_id) "ioas_id %u, devid %d, hwpt_id %u"
Signature · 1 line
2.47.3
#10 lore raw

RFC PATCH 3/14 vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

In reply to Zhenzhong Duan

When both host 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 35a5ec6d92..983eb0ecce 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 96735f1fc8..9605bf73b7 100644
@@ -354,6 +354,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
     VendorCaps caps;
     VFIOIOASHwpt *hwpt;
     uint32_t hwpt_id;
+    uint8_t max_pasid_log2;
     int ret;
 
     /* Try to find a domain */
@@ -398,7 +399,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;
     }
 
@@ -420,6 +421,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
#11 lore raw

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

Quoted 57 lines · Zhenzhong Duan

When both host 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 35a5ec6d92..983eb0ecce 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 +0 -0
index 96735f1fc8..9605bf73b7 100644

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

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
@@ -354,6 +354,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
      VendorCaps caps;
      VFIOIOASHwpt *hwpt;
      uint32_t hwpt_id;
+    uint8_t max_pasid_log2;

I think this should be initialized to some default value.

Quoted 13 lines
hw/vfio/iommufd.c +1 -1
      int ret;
 
      /* Try to find a domain */
@@ -398,7 +399,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;
      }
 

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

Quoted 5 lines
hw/vfio/iommufd.c +2 -0
@@ -420,6 +421,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
          }
      }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {

Can an IOMMU backend not support PASID while the vIOMMU device does ? That would be a problem no ?

Quoted 6 lines
hw/vfio/iommufd.c +3 -0
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }
+
      if (cpr_is_incoming()) {
          hwpt_id = vbasedev->cpr.hwpt_id;
          goto skip_alloc;
#12 lore raw

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

Quoted 68 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [RFC PATCH 03/14] vfio/iommufd: Create nesting parent hwpt with IOMMU_HWPT_ALLOC_PASID flag

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

Quoted 23 lines · Zhenzhong Duan

When both host 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 +0 -0
index 35a5ec6d92..983eb0ecce 100644
@@ -268,6 +268,7 @@ void vfio_device_prepare(VFIODevice *vbasedev,

VFIOContainer *bcontainer,

hw/vfio/device.c bool

Quoted 12 lines
include/hw/vfio/vfio-device.h +1 -0
  void vfio_device_unprepare(VFIODevice *vbasedev);
 
  bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev);
+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 +0 -0
index 973fc35b59..b15ca6ef0a 100644
@@ -533,6 +533,17 @@ bool

vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev)

hw/vfio/iommufd.c static bool

Quoted 22 lines
hw/vfio/device.c +11 -0
      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 +0 -0
index 96735f1fc8..9605bf73b7 100644
@@ -354,6 +354,7 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 6 lines

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

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
      VendorCaps caps;
      VFIOIOASHwpt *hwpt;
      uint32_t hwpt_id;
+    uint8_t max_pasid_log2;

I think this should be initialized to some default value.

OK, will add max_pasid_log2 = 0;

Quoted 17 lines

hw/vfio/iommufd.c static bool

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
      int ret;
 
      /* Try to find a domain */
@@ -398,7 +399,7 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

hw/vfio/iommufd.c static bool

Quoted 10 lines
hw/vfio/iommufd.c +1 -1
       */
      if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev-
 devid,
                                           &type, &caps, sizeof(caps), &hw_caps,
-                                         NULL, errp)) {
+                                         &max_pasid_log2, errp)) {
          return false;
      }
@@ -420,6 +421,11 @@ static bool

iommufd_cdev_autodomains_get(VFIODevice *vbasedev,

Quoted 8 lines

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

Quoted 5 lines
hw/vfio/iommufd.c +2 -0
          }
      }
 
+    if (max_pasid_log2 &&
+        vfio_device_get_viommu_flags_pasid_supported(vbasedev)) {

Can an IOMMU backend not support PASID while the vIOMMU device does ? That would be a problem no ?

If IOMMU backend doesn't support PASID, max_pasid_log2 will be 0 returned by get_device_info().
Then QEMU will not expose vPASID capability of VFIO device to guest, then guest cannot enable PASID on this VFIO device even though vIOMMU support PASID.

Thanks
Zhenzhong

Quoted 7 lines
Quoted 7 lines
hw/vfio/iommufd.c +3 -0
+        flags |= IOMMU_HWPT_ALLOC_PASID;
+    }
+
      if (cpr_is_incoming()) {
          hwpt_id = vbasedev->cpr.hwpt_id;
          goto skip_alloc;
#13 lore raw

RFC PATCH 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.

---
 hw/i386/intel_iommu_accel.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_accel.c +6 -4
index 67d54849f2..d61cfec1e6 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,8 +84,8 @@ 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,
@@ -116,7 +118,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;
         }
     }
Signature · 1 line
2.47.3
#14 lore raw

Re: [RFC PATCH 04/14] intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 16 lines · 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.

---
 hw/i386/intel_iommu_accel.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_accel.c +0 -0
index 67d54849f2..d61cfec1e6 100644  
@@ -69,11 +69,13 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)  
      return NULL;  
  }  

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 72

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -1
-static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,  
+static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,  

Does this change break naming consistency?

Other functions use use idev for HostIOMMUDeviceIOMMUFD structures

Quoted 29 lines

                                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) |

hw/i386/intel_iommu_accel.c +0 -0
@@ -82,8 +84,8 @@ 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,

hw/i386/intel_iommu_accel.c +0 -0
@@ -116,7 +118,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;
         }
     }

#15 lore raw

RE: [RFC PATCH 04/14] intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 24 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 04/14] intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

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

Signed-off-by Zhenzhong Duan

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

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice

Quoted 9 lines

---
 hw/i386/intel_iommu_accel.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_accel.c +0 -0
index 67d54849f2..d61cfec1e6 100644
@@ -69,11 +69,13 @@ VTDHostIOMMUDevice

*vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 7 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 72

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -1
      return NULL;
  }
 
-static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Does this change break naming consistency?

Yes, because we need to check vtd_hiod->iommu_state->pasid in vtd_create_fs_hwpt() but not in vtd_destroy_old_fs_hwpt().

We can change vtd_destroy_old_fs_hwpt() to also pass vtd_hiod or add a new parameter 'iommu_state *s' to vtd_create_fs_hwpt().

Quoted 2 lines

Other functions use use idev for HostIOMMUDeviceIOMMUFD structures

Do you see others except vtd_destroy_old_fs_hwpt()?

Thanks
Zhenzhong

Quoted 35 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -0
                                 VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
                                 Error **errp)
  {
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice · line 78

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -0
      struct iommu_hwpt_vtd_s1 vtd = {};
+    uint32_t flags = vtd_hiod->iommu_state->pasid ?

IOMMU_HWPT_ALLOC_PASID : 0;

hw/i386/intel_iommu_accel.c static bool

Quoted 4 lines

     vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) | (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) |

hw/i386/intel_iommu_accel.c +0 -0
@@ -82,8 +84,8 @@ static bool

vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,

hw/i386/intel_iommu_accel.c static bool

Quoted 12 lines
hw/i386/intel_iommu_accel.c +0 -0
      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,

hw/i386/intel_iommu_accel.c +0 -0
@@ -116,7 +118,7 @@ static bool

vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -1
      }
 
      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;
          }
      }
#16 lore raw

Re: [RFC PATCH 04/14] intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

Quoted 40 lines · Zhenzhong
Quoted 30 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>
Subject: Re: [RFC PATCH 04/14] intel_iommu: Create the nested hwpt with IOMMU_HWPT_ALLOC_PASID flag

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

Signed-off-by Zhenzhong Duan

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

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice

Quoted 9 lines

---
 hw/i386/intel_iommu_accel.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_accel.c +0 -0
index 67d54849f2..d61cfec1e6 100644
@@ -69,11 +69,13 @@ VTDHostIOMMUDevice

*vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 8 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 72

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -1
      return NULL;
  }
 
-static bool vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Does this change break naming consistency?

Quoted 4 lines

Yes, because we need to check vtd_hiod->iommu_state->pasid in vtd_create_fs_hwpt() but not in vtd_destroy_old_fs_hwpt().

We can change vtd_destroy_old_fs_hwpt() to also pass vtd_hiod or add a new parameter 'iommu_state *s' to vtd_create_fs_hwpt().

First solution lgtm

Quoted 7 lines
Quoted 4 lines

Other functions use use idev for HostIOMMUDeviceIOMMUFD structures

Do you see others except vtd_destroy_old_fs_hwpt()?

I don't think so

Thanks !

Quoted 51 lines

Thanks
Zhenzhong

Quoted 44 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 5 lines
hw/i386/intel_iommu_accel.c +1 -0
                                 VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
                                 Error **errp)
  {
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice · line 78

Quoted 2 lines
hw/i386/intel_iommu_accel.c +1 -0
      struct iommu_hwpt_vtd_s1 vtd = {};
+    uint32_t flags = vtd_hiod->iommu_state->pasid ?

IOMMU_HWPT_ALLOC_PASID : 0;

hw/i386/intel_iommu_accel.c static bool

Quoted 4 lines

     vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) | (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) |

hw/i386/intel_iommu_accel.c +0 -0
@@ -82,8 +84,8 @@ static bool

vtd_create_fs_hwpt(HostIOMMUDeviceIOMMUFD *idev,

hw/i386/intel_iommu_accel.c static bool

Quoted 12 lines
hw/i386/intel_iommu_accel.c +0 -0
      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,

hw/i386/intel_iommu_accel.c +0 -0
@@ -116,7 +118,7 @@ static bool

vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,

Quoted 8 lines
hw/i386/intel_iommu_accel.c +1 -1
      }
 
      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;
          }
      }
#17 lore raw

RFC PATCH 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.

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--
 3 files changed, 3 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index a2ca79f925..71cb3b662e 100644
@@ -194,7 +194,6 @@
 #define VTD_ECAP_PRS                (1ULL << 29)
 #define VTD_ECAP_MHMV               (15ULL << 20)
 #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
 #define VTD_ECAP_SSTS               (1ULL << 46)
include/hw/i386/intel_iommu.h +1 -1
index 6c61fd39c7..5e5779e460 100644
@@ -315,7 +315,7 @@ struct IntelIOMMUState {
     bool buggy_eim;                 /* Force buggy EIM unless eim=off */
     uint8_t aw_bits;                /* Host/IOVA address width (in bits) */
     bool dma_drain;                 /* Whether DMA r/w draining enabled */
-    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 +2 -2
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,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("dma-drain", IntelIOMMUState, dma_drain, true),
     DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
     DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
     }
 
     if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);
     }
 }
 
Signature · 1 line
2.47.3
#18 lore raw

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

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

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index a2ca79f925..71cb3b662e 100644  
@@ -194,7 +194,6 @@  
  #define VTD_ECAP_PRS                (1ULL << 29)  
  #define VTD_ECAP_MHMV               (15ULL << 20)  
  #define VTD_ECAP_SRS                (1ULL << 31)  
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */  
  #define VTD_ECAP_PASID              (1ULL << 40)  
  #define VTD_ECAP_SMTS               (1ULL << 43)  
  #define VTD_ECAP_SSTS               (1ULL << 46)  
include/hw/i386/intel_iommu.h +0 -0
index 6c61fd39c7..5e5779e460 100644  
@@ -315,7 +315,7 @@ struct IntelIOMMUState {  

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    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 +1 -1
index e8a6f50a5a..916eb0af5a 100644  
@@ -4151,7 +4151,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("dma-drain", IntelIOMMUState, dma_drain, true),  
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),  
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),  
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)  
      }  
Quoted 4 lines


     if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 3 lines

     }
 }

#19 lore raw

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

Quoted 70 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to uint8

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

Signed-off-by Zhenzhong Duan

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

Quoted 36 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index a2ca79f925..71cb3b662e 100644
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
include/hw/i386/intel_iommu.h +0 -0
index 6c61fd39c7..5e5779e460 100644
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    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 e8a6f50a5a..916eb0af5a 100644

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

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4151,7 +4151,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 4153

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),

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 6 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated device's PASID capability Register is no more than 8. Emulated device should never send a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full pasid space of passthrough device if it has larger Max PASID Width value in pasid capability.

Thanks
Zhenzhong

#20 lore raw

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

Quoted 87 lines · Zhenzhong
Quoted 78 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to
uint8

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

Signed-off-by Zhenzhong Duan

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

Quoted 36 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +0 -1
index a2ca79f925..71cb3b662e 100644
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
include/hw/i386/intel_iommu.h +0 -0
index 6c61fd39c7..5e5779e460 100644
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    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 e8a6f50a5a..916eb0af5a 100644

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

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4151,7 +4151,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 4153

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),

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 7 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 8 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated device's PASID capability Register is no more than 8. Emulated device should never send a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full pasid space of passthrough device if it has larger Max PASID Width value in pasid capability.

The previous approach was to make sure that the kernel never allocates a too large pasid by limiting the width in the iommu instead of doint it in the devices. Your patch seems to reverse the problem. Is it something we are ready to accept?

Quoted 2 lines

Thanks
Zhenzhong

#21 lore raw

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

Quoted 110 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

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

Quoted 1 line
Quoted 1 line

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool

to

Quoted 10 lines
Quoted 10 lines

uint8

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

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

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.

Signed-off-by Zhenzhong Duan

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

Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -194,7 +194,6 @@
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
hw/i386/intel_iommu_internal.h +0 -1
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 25 lines
Quoted 16 lines

FALSE),

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

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),

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 7 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 7 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated device's PASID capability Register is no more than 8. Emulated device should never send a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full pasid space of passthrough device if it has larger Max PASID Width value in pasid

Quoted 8 lines

capability.

Quoted 1 line

The previous approach was to make sure that the kernel never allocates a too large pasid
by limiting the width in the iommu instead of doint it in the devices. Your patch seems to
reverse the problem. Is it something we are ready to accept?

Not get your concern. Define a hard coded limit looks an overkill.
How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width)?

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width) as the upper limit of pasid range.

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger than host side:

@@ -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;
+ }

Thanks
Zhenzhong

#22 lore raw

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

Quoted 163 lines · Zhenzhong
Quoted 156 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to
uint8

Quoted 7 lines · Zhenzhong
Quoted 6 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

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

Quoted 3 lines
Quoted 2 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool

to

Quoted 17 lines
Quoted 17 lines

uint8

Quoted 10 lines · Zhenzhong Duan

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

So instead of an extra 'pss' property we can use a single '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.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Zhenzhong Duan

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

Quoted 12 lines
Quoted 11 lines

Review

Quoted 10 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 16 lines
Quoted 15 lines

Review

Quoted 14 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -1
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid */
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 9 lines
Quoted 8 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 31 lines
Quoted 20 lines

FALSE),

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

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),

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid - 1);

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 7 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated device's PASID capability Register is no more than 8. Emulated device should never send a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full pasid space of passthrough device if it has larger Max PASID Width value in pasid

capability.

Quoted 1 line
Quoted 5 lines

The previous approach was to make sure that the kernel never allocates a too large pasid
by limiting the width in the iommu instead of doint it in the devices. Your patch seems to
reverse the problem. Is it something we are ready to accept?

Quoted 2 lines

Not get your concern. Define a hard coded limit looks an overkill.
How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width)?

Oh yep, you are righ, my "too large" is not very explicit.
I mean too large to be passed from an emulated device, all the way down to the iommu, through MemTxAttrs.

Quoted 3 lines

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width) as the upper limit of pasid range.

Yes, that's true.
Our current implementation makes sure that this MIN is always smaller than what fits in MemTxAttrs. This is always the case as pss is small enough.

Increasing pss means that it's now up to devices to know that the attrs cannot carry a full-width pasid and expose a lower pasid size to make sure the kernel never allocates an out-of-range pasid.

This a bit more dagerous for device implementors but I don't see any other way to make accel work without this change. I just wanted to make sure that you noticed the design shift behind your patch.

btw, why do we need to pass the actual value on the command line? Couldn't we fetch the pss supported by the underlying hardware and use the same value?

Thanks!
cmd

Quoted 16 lines

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger than host side:

(quoted diff) +6 -0
@@ -64,6 +65,13 @@ bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,  
          return false;  
      }
 
+    /* Only do the check when host device support PASIDs */  
+    if (caps->max_pasid_log2 && s->pasid > hpasid) {  
+        error_setg(errp, "PASID bits size %d > host IOMMU PASID bits size %d",  
+                   s->pasid, hpasid);  
+        return false;  
+    }
 

Thanks
Zhenzhong

#23 lore raw

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

Quoted 205 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to uint8

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

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

Quoted 1 line
Quoted 1 line

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool

to

Quoted 9 lines
Quoted 9 lines

uint8

Quoted 4 lines · Zhenzhong
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

[[email protected]](mailto:[email protected])](mailto:[clement.mathieu--

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

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from

bool

Quoted 21 lines
Quoted 21 lines
Quoted 1 line

to

Quoted 17 lines
Quoted 17 lines

uint8

Quoted 10 lines · Zhenzhong Duan

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

So instead of an extra 'pss' property we can use a single '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.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Zhenzhong Duan

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

Quoted 1 line
Quoted 1 line

[[email protected]](mailto:[email protected])](mailto:[zhenzhong.duan @intel.com](mailto:[email protected])))>

Quoted 18 lines
Quoted 18 lines
Quoted 4 lines
Quoted 4 lines
Quoted 9 lines
Quoted 8 lines

Review

Quoted 7 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 2 lines
Quoted 2 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu_internal.h line 197

Quoted 8 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -1
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid

*/

Quoted 36 lines
Quoted 36 lines
Quoted 6 lines
Quoted 5 lines

Review

Quoted 4 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 9 lines
Quoted 8 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 2 lines
Quoted 2 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

FALSE),

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,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

false),

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain,

true),

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid -

1);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated

device's

Quoted 1 line
Quoted 1 line
Quoted 1 line

PASID capability Register is no more than 8. Emulated device should never

send

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full

pasid

Quoted 14 lines
Quoted 7 lines
Quoted 1 line

space of passthrough device if it has larger Max PASID Width value in pasid

capability.

Quoted 1 line
Quoted 5 lines

The previous approach was to make sure that the kernel never allocates a too large pasid
by limiting the width in the iommu instead of doint it in the devices. Your patch seems to
reverse the problem. Is it something we are ready to accept?

Quoted 2 lines

Not get your concern. Define a hard coded limit looks an overkill. How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS, device's

Max PASID Width)?

Oh yep, you are righ, my "too large" is not very explicit.
I mean too large to be passed from an emulated device, all the way down to the iommu, through MemTxAttrs.

Quoted 2 lines

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width)

as the upper limit

Quoted 1 line

of pasid range.

Yes, that's true.
Our current implementation makes sure that this MIN is always smaller than what fits in MemTxAttrs.
This is always the case as pss is small enough.

Quoted 4 lines

Increasing pss means that it's now up to devices to know that the attrs cannot carry a full-width pasid
and expose a lower pasid size to make sure the kernel never allocates an out-of- range pasid.

I see your concern.
Looked into RISC-V emulation series https://lore.kernel.org/qemu-devel/[email protected]/
which introduced MemTxAttrs.pid, it looks MemTxAttrs.pid is only used by RISC-V,
E.g., only riscv_iommu_memory_region_index() returns attrs.pid and pass it to imrc->translate().

For VTD and other vIOMMUs, we create different AddressSpace for different pasid, When call imrc->translate() callback, it's enough to go ahead with AddressSpace, we don't pass in pasid through iommu_idx parameter.

Quoted 4 lines

This a bit more dagerous for device implementors but I don't see any other way to make accel work without this change. I just wanted to make sure that you noticed the design shift behind your patch.

I think it's not an issue with VTD as explained above.

Quoted 4 lines

btw, why do we need to pass the actual value on the command line? Couldn't we fetch the pss supported by
the underlying hardware and use the same value?

Because we can have heterogeneous IOMMUs on host, PSS can be different between them. Meanwhile, take migration into consideration, it needs to be configurable by user.

Thanks
Zhenzhong

Quoted 22 lines

Thanks!
cmd

Quoted 2 lines

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger than

host side:

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

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

VTDHostIOMMUDevice *vtd_hiod,

Quoted 12 lines
(quoted diff) +6 -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;
+    }
 

Thanks
Zhenzhong

#24 lore raw

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

Quoted 362 lines · Zhenzhong
Quoted 336 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to
uint8

Quoted 7 lines · Zhenzhong
Quoted 6 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

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

Quoted 3 lines
Quoted 2 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool

to

Quoted 12 lines
Quoted 12 lines

uint8

Quoted 5 lines · Zhenzhong
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))](mailto:[clement.mathieu--

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

Quoted 9 lines
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from

bool

Quoted 27 lines
Quoted 27 lines
Quoted 3 lines

to

Quoted 19 lines
Quoted 19 lines

uint8

Quoted 10 lines · Zhenzhong Duan

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

So instead of an extra 'pss' property we can use a single '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.

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Zhenzhong Duan

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

Quoted 3 lines
Quoted 2 lines

[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))](mailto:[zhenzhong.duan @intel.com](mailto:[[email protected]](mailto:[email protected]))))>

Quoted 28 lines
Quoted 28 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
Quoted 5 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 4 lines
Quoted 4 lines
Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
Quoted 9 lines

hw/i386/intel_iommu_internal.h line 197

Quoted 8 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -1
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid

*/

Quoted 55 lines
Quoted 55 lines
Quoted 4 lines
Quoted 4 lines
Quoted 8 lines
Quoted 6 lines

Review

Quoted 4 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 11 lines
Quoted 9 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 7 lines
Quoted 7 lines

FALSE),

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

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

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,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

false),

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

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

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain,

true),

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid -

1);

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 8 lines

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated

device's

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

PASID capability Register is no more than 8. Emulated device should never

send

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full

pasid

Quoted 20 lines
Quoted 12 lines
Quoted 3 lines

space of passthrough device if it has larger Max PASID Width value in pasid

capability.

Quoted 1 line
Quoted 5 lines

The previous approach was to make sure that the kernel never allocates a too large pasid
by limiting the width in the iommu instead of doint it in the devices. Your patch seems to
reverse the problem. Is it something we are ready to accept?

Quoted 2 lines

Not get your concern. Define a hard coded limit looks an overkill. How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS, device's

Max PASID Width)?

Oh yep, you are righ, my "too large" is not very explicit.
I mean too large to be passed from an emulated device, all the way down to the iommu, through MemTxAttrs.

Quoted 2 lines

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width)

as the upper limit

Quoted 1 line

of pasid range.

Yes, that's true.
Our current implementation makes sure that this MIN is always smaller than what fits in MemTxAttrs.
This is always the case as pss is small enough.

Quoted 4 lines

Increasing pss means that it's now up to devices to know that the attrs cannot carry a full-width pasid
and expose a lower pasid size to make sure the kernel never allocates an out-of- range pasid.

I see your concern.
Looked into RISC-V emulation series [https://lore.kernel.org/qemu-devel/[email protected]/](https://lore.kernel.org/qemu-devel/[email protected]/)
which introduced MemTxAttrs.pid, it looks MemTxAttrs.pid is only used by RISC-V,
E.g., only riscv_iommu_memory_region_index() returns attrs.pid and pass it to imrc->translate().

For VTD and other vIOMMUs, we create different AddressSpace for different pasid, When call imrc->translate() callback, it's enough to go ahead with AddressSpace, we don't pass in pasid through iommu_idx parameter.

Quoted 3 lines

This a bit more dagerous for device implementors but I don't see any other way to make accel work without this change. I just wanted to make sure that you noticed the design shift behind your patch.

I think it's not an issue with VTD as explained above.

Quoted 7 lines
Quoted 3 lines

btw, why do we need to pass the actual value on the command line? Couldn't we fetch the pss supported by
the underlying hardware and use the same value?

Because we can have heterogeneous IOMMUs on host, PSS can be different between them. Meanwhile, take migration into consideration, it needs to be configurable by user.

right

Thanks
cmd

Quoted 34 lines

Thanks
Zhenzhong

Quoted 27 lines

Thanks!
cmd

Quoted 2 lines

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger than

host side:

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

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

VTDHostIOMMUDevice *vtd_hiod,

Quoted 12 lines
(quoted diff) +6 -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;
+    }
 

Thanks
Zhenzhong

#25 lore raw

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

Shouldn't we check that the user never asks for more than 20 bits on the cli btw?

Quoted 560 lines · Clement Mathieu--Drif
Quoted 496 lines · Zhenzhong
Quoted 466 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to
uint8

Quoted 9 lines · Zhenzhong
Quoted 7 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

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

Quoted 5 lines
Quoted 3 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool

to

Quoted 23 lines
Quoted 23 lines

uint8

Quoted 11 lines · Zhenzhong
Quoted 8 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

Quoted 3 lines
Quoted 1 line

[[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))](mailto:[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected])))](mailto:[clement.mathieu--

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

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from

bool

Quoted 33 lines
Quoted 33 lines
Quoted 5 lines

to

Quoted 21 lines
Quoted 21 lines

uint8

Quoted 10 lines · Zhenzhong Duan

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

So instead of an extra 'pss' property we can use a single '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.

Quoted 15 lines
Quoted 13 lines
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Signed-off-by Zhenzhong Duan

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

Quoted 5 lines
Quoted 3 lines

[[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))](mailto:[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected])))](mailto:[zhenzhong.duan @intel.com](mailto:[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected])))))>

Quoted 38 lines
Quoted 38 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
Quoted 7 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 6 lines
Quoted 6 lines
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -194,7 +194,6 @@
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

hw/i386/intel_iommu_internal.h line 197

Quoted 4 lines
hw/i386/intel_iommu_internal.h +0 -1
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit: MemTxAttrs::pid

*/

Quoted 71 lines
Quoted 71 lines
Quoted 8 lines
Quoted 8 lines
Quoted 10 lines
Quoted 7 lines

Review

Quoted 4 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled */ -    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 13 lines
Quoted 10 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 9 lines
Quoted 9 lines

FALSE),

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

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

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,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

false),

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

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

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain,

true),

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_cap_init(IntelIOMMUState *s) · line 4984

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s->pasid -

1);

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 11 lines

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about this.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated

device's

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

PASID capability Register is no more than 8. Emulated device should never

send

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the full

pasid

Quoted 26 lines
Quoted 17 lines
Quoted 5 lines

space of passthrough device if it has larger Max PASID Width value in pasid

capability.

Quoted 1 line
Quoted 5 lines

The previous approach was to make sure that the kernel never allocates a too large pasid
by limiting the width in the iommu instead of doint it in the devices. Your patch seems to
reverse the problem. Is it something we are ready to accept?

Quoted 2 lines

Not get your concern. Define a hard coded limit looks an overkill. How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS, device's

Max PASID Width)?

Oh yep, you are righ, my "too large" is not very explicit.
I mean too large to be passed from an emulated device, all the way down to the iommu, through MemTxAttrs.

Quoted 2 lines

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID Width)

as the upper limit

Quoted 1 line

of pasid range.

Yes, that's true.
Our current implementation makes sure that this MIN is always smaller than what fits in MemTxAttrs.
This is always the case as pss is small enough.

Quoted 4 lines

Increasing pss means that it's now up to devices to know that the attrs cannot carry a full-width pasid
and expose a lower pasid size to make sure the kernel never allocates an out-of- range pasid.

I see your concern.
Looked into RISC-V emulation series [[https://lore.kernel.org/qemu-devel/[email protected]/](https://lore.kernel.org/qemu-devel/[email protected]/)](https://lore.kernel.org/qemu-devel/[email protected]/](https://lore.kernel.org/qemu-devel/[email protected]/))
which introduced MemTxAttrs.pid, it looks MemTxAttrs.pid is only used by RISC-V,
E.g., only riscv_iommu_memory_region_index() returns attrs.pid and pass it to imrc->translate().

For VTD and other vIOMMUs, we create different AddressSpace for different pasid, When call imrc->translate() callback, it's enough to go ahead with AddressSpace, we don't pass in pasid through iommu_idx parameter.

Quoted 3 lines

This a bit more dagerous for device implementors but I don't see any other way to make accel work without this change. I just wanted to make sure that you noticed the design shift behind your patch.

I think it's not an issue with VTD as explained above.

Quoted 8 lines
Quoted 3 lines

btw, why do we need to pass the actual value on the command line? Couldn't we fetch the pss supported by
the underlying hardware and use the same value?

Because we can have heterogeneous IOMMUs on host, PSS can be different between them. Meanwhile, take migration into consideration, it needs to be configurable by user.

right

Thanks
cmd

Quoted 41 lines

Thanks
Zhenzhong

Quoted 33 lines

Thanks!
cmd

Quoted 2 lines

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger than

host side:

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

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

VTDHostIOMMUDevice *vtd_hiod,

Quoted 12 lines
(quoted diff) +6 -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;
+    }
 

Thanks
Zhenzhong

#26 lore raw

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

Good suggestion, will add:

@@ -5551,6 +5551,11 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

         error_setg(errp, "Need to set scalable mode for PASID");
         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;
+ }

Thanks
Zhenzhong

Quoted 638 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from bool to uint8

Shouldn't we check that the user never asks for more than 20 bits on the cli btw?

Quoted 9 lines · Clement Mathieu--Drif
Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[clement.mathieu--

[email protected]](mailto:clement.mathieu-- [email protected])](mailto:[clement.mathieu-- [email protected]](mailto:[email protected]))>

Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from

bool to

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

uint8

Quoted 5 lines · Zhenzhong
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:dr [email protected]))](mailto:[[clement.mathieu-- [email protected]](mailto:clement.mathieu-- [email protected])](mailto:[clement.mathieu-- [email protected]](mailto:[email protected])))>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property from

bool

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 2 lines

to

Quoted 15 lines
Quoted 15 lines

uint8

Quoted 6 lines · Zhenzhong
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

-----Original Message-----
From: Clement Mathieu--Drif <[clement.mathieu--

[[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:d [email protected]))](mailto:[[[email protected]](mailto:[email protected])](mailto:[dr [email protected]](mailto:[email protected])))](mailto:[clement.mathieu--

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:dr [email protected]))](mailto:[[clement.mathieu-- [email protected]](mailto:clement.mathieu-- [email protected])](mailto:[clement.mathieu-- [email protected]](mailto:[email protected]))))>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Subject: Re: [RFC PATCH 05/14] intel_iommu: Change pasid property

from

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

bool

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines

to

Quoted 6 lines
Quoted 6 lines

uint8

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Zhenzhong Duan
Quoted 6 lines

'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

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 4 lines

bits size. A value of N > 0 means pasid is supported and N - 1 is the value in PSS field in ECAP register.

Signed-off-by Zhenzhong Duan
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

<[[[[[[email protected]](mailto:[email protected])](mailto:[zh [email protected]](mailto:[email protected]))](mailto:[[zhenzho [email protected]](mailto:[email protected])](mailto:[zhenzhong.duan @intel.com](mailto:[email protected])))](mailto:[zhen

Quoted 1 line
Quoted 1 line
Quoted 1 line

[[[email protected]](mailto:[email protected])](mailto:[zhong.duan@in tel.com](mailto:[email protected]))](mailto:[[[email protected]](m ailto:[email protected])](mailto:[[email protected]](mailto:zh [email protected]))))](mailto:[zhen

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

[[[[email protected]](mailto:[email protected])](mailto:[zhong.duan@i ntel.com](mailto:[email protected]))](mailto:[[[email protected]](mailt o:[email protected])](mailto:[[email protected]](mailto:zhong.duan@in tel.com)))](mailto:[zhenzhong.duan

Quoted 1 line
Quoted 1 line
Quoted 1 line

@intel.com](mailto:[[[email protected]](mailto:[email protected] om)](mailto:[[email protected]](mailto:[email protected])))))>

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

---
 hw/i386/intel_iommu_internal.h | 1 -
 include/hw/i386/intel_iommu.h  | 2 +-
 hw/i386/intel_iommu.c          | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)
Quoted 7 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 6 lines
Quoted 6 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu_internal.h line 197

Quoted 8 lines

index a2ca79f925..71cb3b662e 100644

hw/i386/intel_iommu_internal.h +0 -1
@@ -194,7 +194,6 @@
  #define VTD_ECAP_PRS                (1ULL << 29)
  #define VTD_ECAP_MHMV               (15ULL << 20)
  #define VTD_ECAP_SRS                (1ULL << 31)
-#define VTD_ECAP_PSS                (7ULL << 35) /* limit:

MemTxAttrs::pid

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

*/

Quoted 31 lines
Quoted 31 lines
Quoted 8 lines
Quoted 8 lines
Quoted 10 lines
Quoted 7 lines

Review

Quoted 4 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SSTS               (1ULL << 46)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 6 lines
Quoted 6 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index 6c61fd39c7..5e5779e460 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -315,7 +315,7 @@ struct IntelIOMMUState {

     bool buggy_eim;                 /* Force buggy EIM unless eim=off */ uint8_t aw_bits;                /* Host/IOVA address width (in bits) */ bool dma_drain;                 /* Whether DMA r/w draining enabled

*/

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 33 lines
Quoted 33 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

-    bool pasid;                     /* Whether to support PASID */ +    uint8_t pasid;                  /* PASID supported in bits, 0 if not */ bool fs1gp;                     /* First Stage 1-GByte Page Support */

Quoted 13 lines
Quoted 10 lines

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

Quoted 7 lines

     /* Transient Mapping, Reserved(0) since VTD spec revision 3.2 */

hw/i386/intel_iommu.c +0 -0
index e8a6f50a5a..916eb0af5a 100644
@@ -4151,7 +4151,7 @@ static const Property vtd_properties[] = {
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState,

scalable_mode,

Quoted 9 lines
Quoted 9 lines

FALSE),

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

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

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,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

false),

Quoted 1 line
hw/i386/intel_iommu.c +0 -1
-    DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid,

false),

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

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

Quoted 2 lines
hw/i386/intel_iommu.c +1 -0
+    DEFINE_PROP_UINT8("pasid", IntelIOMMUState, pasid, 0),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState,

dma_drain,

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

true),

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

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

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm,

false),

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hw/i386/intel_iommu.c static void

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
@@ -4981,7 +4981,7 @@ static void

vtd_cap_init(IntelIOMMUState *s)

Quoted 30 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
      }
 
      if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
+        s->ecap = VTD_ECAP_PASID | deposit64(s->ecap, 35, 5, s-

pasid -

Quoted 24 lines
Quoted 24 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

1);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

I think this is a bit dangerous, the MemTxAttrs only allow up to 8 bits. We have a comment next to the definition of VTD_ECAP_PSS about

this.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

Could you elaborate what dangerous do you see if s->pasid > 8? MemTxAttrs only allow up to 8bits, then Max PASID Width in emulated

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

device's

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

PASID capability Register is no more than 8. Emulated device should

never

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 2 lines

send

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

a Request-with-pasid with pasid > 255.

Hard code VTD_ECAP_PSS to 8 bits will make us not able to utilize the

full

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

pasid

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

space of passthrough device if it has larger Max PASID Width value in

pasid

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

capability.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

The previous approach was to make sure that the kernel never allocates a

too

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

large pasid
by limiting the width in the iommu instead of doint it in the devices. Your

patch

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

seems to
reverse the problem. Is it something we are ready to accept?

Not get your concern. Define a hard coded limit looks an overkill. How you define too large here, larger than MIN(iommu.VTD_ECAP_PSS,

device's

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Max PASID Width)?

Oh yep, you are righ, my "too large" is not very explicit.
I mean too large to be passed from an emulated device, all the way down to

the

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

iommu, through MemTxAttrs.

Quoted 2 lines

IIUC, kernel should take MIN(iommu.VTD_ECAP_PSS, device's Max PASID

Width)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

as the upper limit

Quoted 1 line

of pasid range.

Yes, that's true.
Our current implementation makes sure that this MIN is always smaller than

what

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

fits in MemTxAttrs.
This is always the case as pss is small enough.

Increasing pss means that it's now up to devices to know that the attrs cannot carry a full-width pasid
and expose a lower pasid size to make sure the kernel never allocates an out-

of-

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

range pasid.

I see your concern.
Looked into RISC-V emulation series [[https://lore.kernel.org/qemu-

devel/20241004155721.2154626-4- [email protected]/](https://lore.kernel.org/qemu- devel/20241004155721.2154626-4- [email protected]/)](https://lore.kernel.org/qemu- devel/20241004155721.2154626-4- [email protected]/](https://lore.kernel.org/qemu- devel/[email protected]/))

Quoted 1 line
Quoted 1 line

which introduced MemTxAttrs.pid, it looks MemTxAttrs.pid is only used by

RISC-V,

Quoted 1 line
Quoted 1 line

E.g., only riscv_iommu_memory_region_index() returns attrs.pid and pass it to

imrc->translate().

Quoted 2 lines
Quoted 2 lines

For VTD and other vIOMMUs, we create different AddressSpace for different

pasid,

Quoted 1 line
Quoted 1 line

When call imrc->translate() callback, it's enough to go ahead with

AddressSpace,

Quoted 6 lines
Quoted 6 lines

we don't pass in pasid through iommu_idx parameter.

Quoted 1 line

This a bit more dagerous for device implementors but I don't see any other

way to

Quoted 1 line
Quoted 1 line
Quoted 1 line

make accel work without this change. I just wanted to make sure that you

noticed

Quoted 10 lines
Quoted 10 lines
Quoted 1 line

the design shift behind your patch.

I think it's not an issue with VTD as explained above.

Quoted 1 line

btw, why do we need to pass the actual value on the command line? Couldn't

we

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

fetch the pss supported by
the underlying hardware and use the same value?

Because we can have heterogeneous IOMMUs on host, PSS can be different

between them.

Quoted 1 line
Quoted 1 line

Meanwhile, take migration into consideration, it needs to be configurable by

user.

Quoted 22 lines

right

Thanks
cmd

Quoted 13 lines

Thanks
Zhenzhong

Quoted 7 lines

Thanks!
cmd

Quoted 2 lines

We also have patch12 to ensure vIOMMU's pasid upper limit not bigger

than

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

host side:

Review bool vtd_check_hiod_accel(IntelIOMMUState *s,

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

VTDHostIOMMUDevice *vtd_hiod,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review bool vtd_check_hiod_accel(IntelIOMMUState *s, · line 70

Quoted 6 lines
(quoted diff) +3 -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",

Quoted 12 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
(quoted diff) +3 -0
+                   s->pasid, hpasid);
+        return false;
+    }
 

Thanks
Zhenzhong

#27 lore raw

RFC PATCH 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 71cb3b662e..6753a20ca4 100644
@@ -615,6 +615,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;
@@ -733,4 +739,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
#28 lore raw

Re: [RFC PATCH 06/14] intel_iommu: Export some functions

lgtm

Quoted 156 lines · 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 +0 -0
index 71cb3b662e..6753a20ca4 100644  
@@ -615,6 +615,12 @@ typedef struct VTDRootEntry VTDRootEntry;  
  #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL  
  #define VTD_SM_CONTEXT_ENTRY_PRE            0x10ULL  

hw/i386/intel_iommu_internal.h +6 -0
+/* 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;

hw/i386/intel_iommu_internal.h +25 -0
@@ -733,4 +739,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 +0 -0
index 916eb0af5a..ab52a688c5 100644  
@@ -42,12 +42,6 @@  
  #include "migration/vmstate.h"  
  #include "trace.h"  

hw/i386/intel_iommu.c +0 -6
-/* 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)  
-  

 /*
  * Paging mode for first-stage translation (VTD spec Figure 9-6)
  * 00: 4-level paging, 01: 5-level paging

hw/i386/intel_iommu.c +0 -0
@@ -831,18 +825,12 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)  
      }  
  }  

hw/i386/intel_iommu.c +0 -5
-static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)  
-{  
-    return pdire->val & 1;  
-}  
-  

 /**
  * Caller of this function should check present bit if wants
  * to use pdir entry for further usage except for fpd bit check. */

hw/i386/intel_iommu.c +2 -3
-static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,  
-                                         uint32_t pasid,  
-                                         VTDPASIDDirEntry *pdire)  
+int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,  
+                                  VTDPASIDDirEntry *pdire)  

 {
     uint32_t index;
     dma_addr_t addr, entry_size;

hw/i386/intel_iommu.c +0 -0
@@ -860,15 +848,8 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,  
      return 0;  
  }  

hw/i386/intel_iommu.c +2 -9
-static inline bool vtd_pe_present(VTDPASIDEntry *pe)  
-{  
-    return pe->val[0] & VTD_PASID_ENTRY_P;  
-}  
-  
-static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,  
-                                          uint32_t pasid,  
-                                          dma_addr_t addr,  
-                                          VTDPASIDEntry *pe)  
+int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,  
+                                   dma_addr_t addr, VTDPASIDEntry *pe)  

 {
     uint8_t pgtt;
     uint32_t index;

hw/i386/intel_iommu.c +0 -0
@@ -954,8 +935,8 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,  
      return 0;  
  }  

hw/i386/intel_iommu.c +2 -2
-static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,  
-                                  VTDPASIDEntry *pe, uint32_t pasid)  
+int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,  
+                           VTDPASIDEntry *pe, uint32_t pasid)  

 {
     dma_addr_t pasid_dir_base;

hw/i386/intel_iommu.c +0 -0
@@ -1531,8 +1512,8 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s, VTDContextEntry *ce)  
  }  


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

hw/i386/intel_iommu.c +2 -2
-static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,  
-                                    uint8_t devfn, VTDContextEntry *ce)  
+int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,  
+                             uint8_t devfn, VTDContextEntry *ce)  

 {
     VTDRootEntry re;
     int ret_fr;

hw/i386/intel_iommu.c +0 -0
@@ -1894,7 +1875,7 @@ static VTDAddressSpace *vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,  
                               vtd_find_as_by_sid_and_pasid, &key);  
  }  

hw/i386/intel_iommu.c +1 -1
-static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)  
+VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)  

 {
     return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID); }

hw/i386/intel_iommu.c +0 -0
@@ -3112,11 +3093,6 @@ static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,  
      return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);  
  }  

hw/i386/intel_iommu.c +0 -5
-static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)  
-{  
-    return memcmp(p1, p2, sizeof(*p1));  
-}  
-  

 /* Update or invalidate pasid cache based on the pasid entry in guest memory. */ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value, gpointer user_data)

#29 lore raw

RE: [RFC PATCH 06/14] intel_iommu: Export some functions

Hi Clement,

Could I take it as a formal "Reviewed-by: Clement Mathieu--Drif <[email protected]>" or not?

Thanks
Zhenzhong

Quoted 173 lines

-----Original Message-----
From: Clement Mathieu--Drif <[email protected]>
Subject: Re: [RFC PATCH 06/14] intel_iommu: Export some functions

lgtm

Quoted 5 lines · 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.

Signed-off-by Zhenzhong Duan

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

hw/i386/intel_iommu_internal.h static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry

Quoted 23 lines

---
 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 +6 -0
index 71cb3b662e..6753a20ca4 100644
@@ -615,6 +615,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;
@@ -733,4 +739,29 @@ static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry

*pe)

Quoted 5 lines
hw/i386/intel_iommu_internal.h +2 -0
  {
      return (VTD_SM_PASID_ENTRY_PGTT(pe) == VTD_SM_PASID_ENTRY_FST);
  }
+
+static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)

hw/i386/intel_iommu_internal.h static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)

Quoted 10 lines
hw/i386/intel_iommu_internal.h +10 -0
+{
+    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)

hw/i386/intel_iommu.c static inline bool

Quoted 32 lines
hw/i386/intel_iommu_internal.h +13 -0
+{
+    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 +0 -6
index 916eb0af5a..ab52a688c5 100644
@@ -42,12 +42,6 @@
  #include "migration/vmstate.h"
  #include "trace.h"
 
-/* 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)
-
  /*
   * Paging mode for first-stage translation (VTD spec Figure 9-6)
   * 00: 4-level paging, 01: 5-level paging
@@ -831,18 +825,12 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

hw/i386/intel_iommu.c static int vtd_get_pdire_from_pdir_table(dma_addr_t

Quoted 21 lines
hw/i386/intel_iommu.c +2 -8
      }
  }
 
-static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
-{
-    return pdire->val & 1;
-}
-
  /**
   * Caller of this function should check present bit if wants
   * to use pdir entry for further usage except for fpd bit check.
   */
-static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
-                                         uint32_t pasid,
-                                         VTDPASIDDirEntry *pdire)
+int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
+                                  VTDPASIDDirEntry *pdire)
  {
      uint32_t index;
      dma_addr_t addr, entry_size;
@@ -860,15 +848,8 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t

pasid_dir_base,

hw/i386/intel_iommu.c static int

Quoted 18 lines
hw/i386/intel_iommu.c +2 -9
      return 0;
  }
 
-static inline bool vtd_pe_present(VTDPASIDEntry *pe)
-{
-    return pe->val[0] & VTD_PASID_ENTRY_P;
-}
-
-static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
-                                          uint32_t pasid,
-                                          dma_addr_t addr,
-                                          VTDPASIDEntry *pe)
+int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
+                                   dma_addr_t addr, VTDPASIDEntry *pe)
  {
      uint8_t pgtt;
      uint32_t index;
@@ -954,8 +935,8 @@ static int

vtd_get_pe_from_pasid_table(IntelIOMMUState *s,

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

Quoted 11 lines
hw/i386/intel_iommu.c +2 -2
      return 0;
  }
 
-static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
-                                  VTDPASIDEntry *pe, uint32_t pasid)
+int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
+                           VTDPASIDEntry *pe, uint32_t pasid)
  {
      dma_addr_t pasid_dir_base;
 
@@ -1531,8 +1512,8 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s,

VTDContextEntry *ce)

hw/i386/intel_iommu.c static VTDAddressSpace

Quoted 11 lines
hw/i386/intel_iommu.c +2 -2
  }
 
  /* Map a device to its corresponding domain (context-entry) */
-static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
-                                    uint8_t devfn, VTDContextEntry *ce)
+int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                             uint8_t devfn, VTDContextEntry *ce)
  {
      VTDRootEntry re;
      int ret_fr;
@@ -1894,7 +1875,7 @@ static VTDAddressSpace

*vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,

hw/i386/intel_iommu.c static inline int

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
                               vtd_find_as_by_sid_and_pasid, &key);
  }
 
-static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
+VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
  {
      return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
  }
@@ -3112,11 +3093,6 @@ static inline int

vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,

Quoted 9 lines
hw/i386/intel_iommu.c +0 -5
      return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);
  }
 
-static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
-{
-    return memcmp(p1, p2, sizeof(*p1));
-}
-
  /* Update or invalidate pasid cache based on the pasid entry in guest memory.

*/

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
  static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
                                          gpointer user_data)
#30 lore raw

Re: [RFC PATCH 06/14] intel_iommu: Export some functions

Quoted 3 lines · Zhenzhong

Hi Clement,

Could I take it as a formal "Reviewed-by: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>" or not?

sure

Quoted 201 lines

Thanks
Zhenzhong

Quoted 195 lines

-----Original Message-----
From: Clement Mathieu--Drif <[[email protected]](mailto:[email protected])>
Subject: Re: [RFC PATCH 06/14] intel_iommu: Export some functions

lgtm

Quoted 5 lines · 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.

Signed-off-by Zhenzhong Duan

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

hw/i386/intel_iommu_internal.h static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry

Quoted 23 lines

---
 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 +6 -0
index 71cb3b662e..6753a20ca4 100644
@@ -615,6 +615,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;
@@ -733,4 +739,29 @@ static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry

*pe)

Quoted 5 lines
hw/i386/intel_iommu_internal.h +2 -0
  {
      return (VTD_SM_PASID_ENTRY_PGTT(pe) == VTD_SM_PASID_ENTRY_FST);
  }
+
+static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)

hw/i386/intel_iommu_internal.h static inline bool vtd_pe_pgtt_is_fst(VTDPASIDEntry *pe)

Quoted 10 lines
hw/i386/intel_iommu_internal.h +10 -0
+{
+    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)

hw/i386/intel_iommu.c static inline bool

Quoted 32 lines
hw/i386/intel_iommu_internal.h +13 -0
+{
+    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 +0 -6
index 916eb0af5a..ab52a688c5 100644
@@ -42,12 +42,6 @@
  #include "migration/vmstate.h"
  #include "trace.h"
 
-/* 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)
-
  /*
   * Paging mode for first-stage translation (VTD spec Figure 9-6)
   * 00: 4-level paging, 01: 5-level paging
@@ -831,18 +825,12 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

hw/i386/intel_iommu.c static int vtd_get_pdire_from_pdir_table(dma_addr_t

Quoted 21 lines
hw/i386/intel_iommu.c +2 -8
      }
  }
 
-static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
-{
-    return pdire->val & 1;
-}
-
  /**
   * Caller of this function should check present bit if wants
   * to use pdir entry for further usage except for fpd bit check.
   */
-static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
-                                         uint32_t pasid,
-                                         VTDPASIDDirEntry *pdire)
+int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, uint32_t pasid,
+                                  VTDPASIDDirEntry *pdire)
  {
      uint32_t index;
      dma_addr_t addr, entry_size;
@@ -860,15 +848,8 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t

pasid_dir_base,

hw/i386/intel_iommu.c static int

Quoted 18 lines
hw/i386/intel_iommu.c +2 -9
      return 0;
  }
 
-static inline bool vtd_pe_present(VTDPASIDEntry *pe)
-{
-    return pe->val[0] & VTD_PASID_ENTRY_P;
-}
-
-static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
-                                          uint32_t pasid,
-                                          dma_addr_t addr,
-                                          VTDPASIDEntry *pe)
+int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, uint32_t pasid,
+                                   dma_addr_t addr, VTDPASIDEntry *pe)
  {
      uint8_t pgtt;
      uint32_t index;
@@ -954,8 +935,8 @@ static int

vtd_get_pe_from_pasid_table(IntelIOMMUState *s,

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

Quoted 11 lines
hw/i386/intel_iommu.c +2 -2
      return 0;
  }
 
-static int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
-                                  VTDPASIDEntry *pe, uint32_t pasid)
+int vtd_ce_get_pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce,
+                           VTDPASIDEntry *pe, uint32_t pasid)
  {
      dma_addr_t pasid_dir_base;
 
@@ -1531,8 +1512,8 @@ static int vtd_ce_pasid_0_check(IntelIOMMUState *s,

VTDContextEntry *ce)

hw/i386/intel_iommu.c static VTDAddressSpace

Quoted 11 lines
hw/i386/intel_iommu.c +2 -2
  }
 
  /* Map a device to its corresponding domain (context-entry) */
-static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
-                                    uint8_t devfn, VTDContextEntry *ce)
+int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                             uint8_t devfn, VTDContextEntry *ce)
  {
      VTDRootEntry re;
      int ret_fr;
@@ -1894,7 +1875,7 @@ static VTDAddressSpace

*vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,

hw/i386/intel_iommu.c static inline int

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
                               vtd_find_as_by_sid_and_pasid, &key);
  }
 
-static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
+VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
  {
      return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
  }
@@ -3112,11 +3093,6 @@ static inline int

vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,

hw/i386/intel_iommu.c static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,

Quoted 9 lines
hw/i386/intel_iommu.c +0 -5
      return vtd_ce_get_pasid_entry(s, &ce, pe, vtd_as->pasid);
  }
 
-static int vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
-{
-    return memcmp(p1, p2, sizeof(*p1));
-}
-
  /* Update or invalidate pasid cache based on the pasid entry in guest memory.

*/

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
  static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
                                          gpointer user_data)
#31 lore raw

RFC PATCH 7/14 intel_iommu: 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 binded nested hwpt. It's also designed to support PASID_0.

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.

---
 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..a77fd06fe0 100644
@@ -12,6 +12,13 @@
 #define HW_I386_INTEL_IOMMU_ACCEL_H
 #include CONFIG_DEVICES
 
+typedef struct VTDACCELPASIDCacheEntry {
+    VTDHostIOMMUDevice *vtd_hiod;
+    VTDPASIDEntry pe;
+    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 6753a20ca4..fe81c47819 100644
@@ -611,6 +611,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
@@ -641,6 +642,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 */
@@ -698,6 +700,7 @@ typedef struct VTDHostIOMMUDevice {
     PCIBus *bus;
     uint8_t devfn;
     HostIOMMUDevice *hiod;
+    QLIST_HEAD(, VTDACCELPASIDCacheEntry) pasid_cache_list;
 } VTDHostIOMMUDevice;
 
 /*
@@ -755,6 +758,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 ab52a688c5..50569460ab 100644
@@ -3181,6 +3181,8 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
     g_hash_table_foreach(s->vtd_address_spaces, vtd_pasid_cache_sync_locked,
                          pc_info);
     vtd_iommu_unlock(s);
+
+    vtd_pasid_cache_sync_accel(s, pc_info);
 }
 
 static void vtd_replay_pasid_bindings_all(IntelIOMMUState *s)
@@ -4691,6 +4693,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
#32 lore raw

RFC PATCH 8/14 intel_iommu: 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 | 70 +++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
hw/i386/intel_iommu_accel.c +70 -0
index 57286f443e..2c17365a27 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)
 {
@@ -253,6 +275,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->pe);
+        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_find_add_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,
                             VTDPASIDEntry *pe)
 {
@@ -419,6 +487,8 @@ void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
                                  TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
             continue;
         }
+
+        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
#33 lore raw

RFC PATCH 9/14 intel_iommu: 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 a77fd06fe0..914c690c26 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 50569460ab..1d0f0bf68b 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 2c17365a27..1d7dae87ec 100644
@@ -493,6 +493,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
#34 lore raw

RFC PATCH 10/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 a PASID, also need to switch the binding/unbinding and PIOTLB flushing functions to use the same structure.

The backend implementations of binding/unbinding to nonzero PASID and PASID_0 are different, so we need to check the pasid to call different callbacks.

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   | 128 ++++++++++++++++++----------------
 4 files changed, 70 insertions(+), 79 deletions(-)
hw/i386/intel_iommu_accel.h +1 -1
index 914c690c26..1ae46d9250 100644
@@ -16,6 +16,7 @@ typedef struct VTDACCELPASIDCacheEntry {
     VTDHostIOMMUDevice *vtd_hiod;
     VTDPASIDEntry pe;
     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 5e5779e460..dfcd8ee652 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 1d0f0bf68b..1626c8f96b 100644
@@ -86,8 +86,6 @@ static void vtd_pasid_cache_reset_locked(IntelIOMMUState *s)
         VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
         if (pc_entry->valid) {
             pc_entry->valid = false;
-            /* It's fatal to get failure during reset */
-            vtd_propagate_guest_pasid(vtd_as, &error_fatal);
         }
     }
 }
@@ -3105,8 +3103,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     VTDPASIDEntry pe;
     IOMMUNotifier *n;
     uint16_t did;
-    const char *err_prefix = "Attaching to HWPT failed: ";
-    Error *local_err = NULL;
 
     if (vtd_dev_get_pe_from_pasid(vtd_as, &pe)) {
         if (!pc_entry->valid) {
@@ -3127,9 +3123,6 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
             vtd_address_space_unmap(vtd_as, n);
         }
         vtd_switch_address_space(vtd_as);
-
-        err_prefix = "Detaching from HWPT failed: ";
-        goto do_bind_unbind;
     }
 
     /*
@@ -3157,20 +3150,12 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     if (!pc_entry->valid) {
         pc_entry->pasid_entry = pe;
         pc_entry->valid = true;
-    } else if (vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
-        err_prefix = "Replacing HWPT attachment failed: ";
-    } else {
+    } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
         return;
     }
 
     vtd_switch_address_space(vtd_as);
     vtd_address_space_sync(vtd_as);
-
-do_bind_unbind:
-    /* TODO: Fault event injection into guest, report error to QEMU for now */
-    if (!vtd_propagate_guest_pasid(vtd_as, &local_err)) {
-        error_reportf_err(local_err, "%s", err_prefix);
-    }
 }
 
 static void vtd_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info)
hw/i386/intel_iommu_accel.c +68 -60

229 lines not on this page · raw message

Signature · 1 line
2.47.3
#35 lore raw

RFC PATCH 11/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 1ae46d9250..3f1b1002b8 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 1626c8f96b..17252c804b 100644
@@ -2990,11 +2990,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) {
@@ -3024,10 +3024,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 5abc462a45..a30786e361 100644
@@ -236,9 +236,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
#36 lore raw

RFC PATCH 12/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.

---
 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 fe81c47819..2901cf44aa 100644
@@ -194,6 +194,7 @@
 #define VTD_ECAP_PRS                (1ULL << 29)
 #define VTD_ECAP_MHMV               (15ULL << 20)
 #define VTD_ECAP_SRS                (1ULL << 31)
+#define VTD_ECAP_PSS(x)             extract64(x, 35, 5)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
 #define VTD_ECAP_SSTS               (1ULL << 46)
hw/i386/intel_iommu_accel.c +8 -0
index a30786e361..61ff531803 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
#37 lore raw

RFC PATCH 13/14 intel_iommu: Drop pasid related check

In reply to Zhenzhong Duan

The check of 's->pasid && x86_iommu->dt_supported' is an overkill, because all emulated devices don't support PASID capability yet, they never send Requests-with-PASID, no pasid tagged iotlb entries are cached in the device. Even if guest send PASID-based-Device-TLB invalidation request, we can ignore it safely.

---
 hw/i386/intel_iommu.c | 11 -----------
 1 file changed, 11 deletions(-)
hw/i386/intel_iommu.c +0 -11
index 17252c804b..4ab50a9fd9 100644
@@ -5490,17 +5490,6 @@ static void vtd_realize(DeviceState *dev, Error **errp)
     X86MachineState *x86ms = X86_MACHINE(ms);
     PCIBus *bus = pcms->pcibus;
     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
-
-    if (s->pasid && x86_iommu->dt_supported) {
-        /*
-         * PASID-based-Device-TLB Invalidate Descriptor is not
-         * implemented and it requires support from vhost layer which
-         * needs to be implemented in the future.
-         */
-        error_setg(errp, "PASID based device IOTLB is not supported");
-        return;
-    }
 
     if (!vtd_decide_config(s, errp)) {
         return;
Signature · 1 line
2.47.3
#38 lore raw

RFC PATCH 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 4ab50a9fd9..86998dad94 100644
@@ -4727,6 +4727,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