Skip to contentSkip to conversation

intel_iommu: Enable stage-1 translation for passthrough device

RFC PATCH Superseded Zhenzhong Duan 7 participants 68 messages Feb 19, 2025 – May 20, 2025

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover intel_iommu: Enable stage-1 translation for passthrough device

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

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

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

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

See below diagram:

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

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

      IntelIOMMUState
             |
             V
    .------------------.    .------------------.    .-------------------.
    | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |-->...
    | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
    .------------------.    .------------------.    .-------------------.
             |                       |                              |
             |                       .-->...                        |
             V                                                      V
      .-------------------.    .-------------------.          .---------------.
      |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-->...
      .-------------------.    .-------------------.          .---------------.
          |            |               |                            |
          |            |               |                            |
    .-----------.  .-----------.  .------------.              .------------.
    | IOMMUFD   |  | IOMMUFD   |  | IOMMUFD    |              | IOMMUFD    |
    | Device(CC)|  | Device(CC)|  | Device     |              | Device(CC) |
    | (iommufd0)|  | (iommufd0)|  | (non-CC)   |              | (errata)   |
    |           |  |           |  | (iommufd0) |              | (iommufd0) |
    .-----------.  .-----------.  .------------.              .------------.

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

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1 translation. i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

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

PATCH1-8:  Add HWPT-based nesting infrastructure support
PATCH9-10: Some cleanup work
PATCH11:   cap/ecap related compatibility check between vIOMMU and Host IOMMU

PATCH12-19:Implement stage-1 page table for passthrough device
PATCH20: Enable stage-1 translation for passthrough device

Qemu code can be found at [3]

TODO:
- RAM discard
- dirty tracking on stage-2 page table

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_rfcv2

Thanks
Zhenzhong

Changelog:
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace

Yi Liu (3):
intel_iommu: Replay pasid binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

Zhenzhong Duan (17):
backends/iommufd: Add helpers for invalidating user-managed HWPT vfio/iommufd: Add properties and handlers to
TYPE_HOST_IOMMU_DEVICE_IOMMUFD
HostIOMMUDevice: Introduce realize_late callback vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler vfio/iommufd: Implement [at|de]tach_hwpt handlers
host_iommu_device: Define two new capabilities
HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Add PASID cache management infrastructure
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode

 hw/i386/intel_iommu_internal.h     |   56 +
 include/hw/i386/intel_iommu.h      |   33 +-
 include/system/host_iommu_device.h |   40 +
 include/system/iommufd.h           |   53 +
 backends/iommufd.c                 |   58 +
 hw/i386/intel_iommu.c              | 1660 ++++++++++++++++++++++++----
 hw/vfio/common.c                   |   17 +-
 hw/vfio/iommufd.c                  |   48 +
 backends/trace-events              |    1 +
 hw/i386/trace-events               |   13 +
 10 files changed, 1776 insertions(+), 203 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v2 1/20 backends/iommufd: Add helpers for invalidating user-managed HWPT

---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 30 ++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 34 insertions(+)
include/system/iommufd.h +3 -0
index cbab75bfbf..5d02e9d148 100644
@@ -61,6 +61,9 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,
                                       uint64_t iova, ram_addr_t size,
                                       uint64_t page_size, uint64_t *data,
                                       Error **errp);
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
 #endif
backends/iommufd.c +30 -0
index d57da44755..fc32aad5cb 100644
@@ -311,6 +311,36 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr)
+{
+    int ret, fd = be->fd;
+    struct iommu_hwpt_invalidate cache = {
+        .size = sizeof(cache),
+        .hwpt_id = hwpt_id,
+        .data_type = data_type,
+        .entry_len = entry_len,
+        .entry_num = *entry_num,
+        .data_uptr = (uintptr_t)data_ptr,
+    };
+
+    ret = ioctl(fd, IOMMU_HWPT_INVALIDATE, &cache);
+
+    trace_iommufd_backend_invalidate_cache(fd, hwpt_id, data_type, entry_len,
+                                           *entry_num, cache.entry_num,
+                                           (uintptr_t)data_ptr, ret);
+    if (ret) {
+        *entry_num = cache.entry_num;
+        error_report("IOMMU_HWPT_INVALIDATE failed: %s", strerror(errno));
+        ret = -errno;
+    } else {
+        g_assert(*entry_num == cache.entry_num);
+    }
+
+    return ret;
+}
+
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
 {
     HostIOMMUDeviceCaps *caps = &hiod->caps;
backends/trace-events +1 -0
index 40811a3162..5a23db6c8a 100644
@@ -18,3 +18,4 @@ iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_
 iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)"
 iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret) " iommufd=%d hwpt=%u enable=%d (%d)"
 iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"
+iommufd_backend_invalidate_cache(int iommufd, uint32_t hwpt_id, uint32_t data_type, uint32_t entry_len, uint32_t entry_num, uint32_t done_num, uint64_t data_ptr, int ret) " iommufd=%d hwpt_id=%u data_type=%u entry_len=%u entry_num=%u done_num=%u data_ptr=0x%"PRIx64" (%d)"
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

in the title, there is only a single helper here. a small commit msg may help the reader

Quoted 40 lines
---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 30 ++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 34 insertions(+)
include/system/iommufd.h +3 -0
index cbab75bfbf..5d02e9d148 100644
@@ -61,6 +61,9 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,
                                       uint64_t iova, ram_addr_t size,
                                       uint64_t page_size, uint64_t *data,
                                       Error **errp);
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
 #endif
backends/iommufd.c +12 -0
index d57da44755..fc32aad5cb 100644
@@ -311,6 +311,36 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr)
+{
+    int ret, fd = be->fd;
+    struct iommu_hwpt_invalidate cache = {
+        .size = sizeof(cache),
+        .hwpt_id = hwpt_id,
+        .data_type = data_type,
+        .entry_len = entry_len,
+        .entry_num = *entry_num,
+        .data_uptr = (uintptr_t)data_ptr,

backends/iommufd.c bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, · line 335

Quoted 10 lines
backends/iommufd.c +10 -0
+    };
+
+    ret = ioctl(fd, IOMMU_HWPT_INVALIDATE, &cache);
+
+    trace_iommufd_backend_invalidate_cache(fd, hwpt_id, data_type, entry_len,
+                                           *entry_num, cache.entry_num,
+                                           (uintptr_t)data_ptr, ret);
+    if (ret) {
+        *entry_num = cache.entry_num;
+        error_report("IOMMU_HWPT_INVALIDATE failed: %s", strerror(errno));

nit: you may report *entry_num also.
Wouldn't it be useful to have an Error *errp passed to the function

Quoted 16 lines
backends/iommufd.c +8 -0
+        ret = -errno;
+    } else {
+        g_assert(*entry_num == cache.entry_num);
+    }
+
+    return ret;
+}
+
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
 {
     HostIOMMUDeviceCaps *caps = &hiod->caps;
backends/trace-events +0 -0
index 40811a3162..5a23db6c8a 100644

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_ · line 21

Quoted 4 lines
backends/trace-events +1 -0
@@ -18,3 +18,4 @@ iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_
 iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)"
 iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret) " iommufd=%d hwpt=%u enable=%d (%d)"
 iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"
+iommufd_backend_invalidate_cache(int iommufd, uint32_t hwpt_id, uint32_t data_type, uint32_t entry_len, uint32_t entry_num, uint32_t done_num, uint64_t data_ptr, int ret) " iommufd=%d hwpt_id=%u data_type=%u entry_len=%u entry_num=%u done_num=%u data_ptr=0x%"PRIx64" (%d)"

Eric

#3 lore raw

RE: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

Hi Eric,

Quoted 5 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

Quoted 8 lines

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

in the title, there is only a single helper here. a small commit msg may help the reader

Sure, will do.

Quoted 51 lines

include/system/iommufd.h bool

Quoted 11 lines
---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 30 ++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 34 insertions(+)
include/system/iommufd.h +0 -0
index cbab75bfbf..5d02e9d148 100644
@@ -61,6 +61,9 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 4 lines
include/system/iommufd.h +1 -0
                                       uint64_t iova, ram_addr_t size,
                                       uint64_t page_size, uint64_t *data,
                                       Error **errp);
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t

hwpt_id,

include/system/iommufd.h bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 4 lines
include/system/iommufd.h +2 -0
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD

TYPE_HOST_IOMMU_DEVICE "-iommufd"

backends/iommufd.c bool

Quoted 6 lines
include/system/iommufd.h +0 -0
 #endif
backends/iommufd.c +0 -0
index d57da44755..fc32aad5cb 100644
@@ -311,6 +311,36 @@ bool

iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

backends/iommufd.c bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

Quoted 4 lines
backends/iommufd.c +1 -0
     return true;
 }
 
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t

hwpt_id,

Quoted 6 lines
backends/iommufd.c +6 -0
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr)
+{
+    int ret, fd = be->fd;
+    struct iommu_hwpt_invalidate cache = {
+        .size = sizeof(cache),

backends/iommufd.c bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

Quoted 10 lines
backends/iommufd.c +10 -0
+        .hwpt_id = hwpt_id,
+        .data_type = data_type,
+        .entry_len = entry_len,
+        .entry_num = *entry_num,
+        .data_uptr = (uintptr_t)data_ptr,
+    };
+
+    ret = ioctl(fd, IOMMU_HWPT_INVALIDATE, &cache);
+
+    trace_iommufd_backend_invalidate_cache(fd, hwpt_id, data_type,

entry_len,

Quoted 7 lines

backends/iommufd.c bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, · line 335

Quoted 5 lines
backends/iommufd.c +5 -0
+                                           *entry_num, cache.entry_num,
+                                           (uintptr_t)data_ptr, ret);
+    if (ret) {
+        *entry_num = cache.entry_num;
+        error_report("IOMMU_HWPT_INVALIDATE failed: %s", strerror(errno));

nit: you may report *entry_num also.
Wouldn't it be useful to have an Error *errp passed to the function

Will do.

Thanks
Zhenzhong

Quoted 30 lines

backends/iommufd.c bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

Quoted 9 lines
backends/iommufd.c +8 -0
+        ret = -errno;
+    } else {
+        g_assert(*entry_num == cache.entry_num);
+    }
+
+    return ret;
+}
+
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error

**errp)

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t

Quoted 7 lines
backends/iommufd.c +0 -0
 {
     HostIOMMUDeviceCaps *caps = &hiod->caps;
backends/trace-events +0 -0
index 40811a3162..5a23db6c8a 100644
@@ -18,3 +18,4 @@ iommufd_backend_alloc_hwpt(int iommufd, uint32_t

dev_id, uint32_t pt_id, uint32_

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t · line 18

Quoted 1 line
backends/trace-events +0 -0
 iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d

id=%d (%d)"

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t · line 19

Quoted 1 line
backends/trace-events +0 -0
 iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret)

" iommufd=%d hwpt=%u enable=%d (%d)"

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t · line 20

Quoted 1 line
backends/trace-events +0 -0
 iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t

iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"

backends/trace-events iommufd_backend_alloc_hwpt(int iommufd, uint32_t · line 21

Quoted 1 line
backends/trace-events +1 -0
+iommufd_backend_invalidate_cache(int iommufd, uint32_t hwpt_id, uint32_t

data_type, uint32_t entry_len, uint32_t entry_num, uint32_t done_num, uint64_t data_ptr, int ret) " iommufd=%d hwpt_id=%u data_type=%u entry_len=%u entry_num=%u done_num=%u data_ptr=0x%"PRIx64" (%d)"
Eric

#4 lore raw

RE: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 45 lines

-----Original Message-----
From: Zhenzhong Duan <[email protected]>
Sent: Wednesday, February 19, 2025 8:22 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhenzhong Duan <[email protected]>
Subject: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 30 ++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 34 insertions(+)
include/system/iommufd.h +0 -0
index cbab75bfbf..5d02e9d148 100644
@@ -61,6 +61,9 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,

                                       uint64_t iova, ram_addr_t size,
                                       uint64_t page_size, uint64_t *data,
                                       Error **errp);

+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h +2 -0
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD

TYPE_HOST_IOMMU_DEVICE "-iommufd"
#endif

backends/iommufd.c +0 -0
index d57da44755..fc32aad5cb 100644

backends/iommufd.c bool · line 316

Quoted 6 lines
backends/iommufd.c +1 -0
@@ -311,6 +311,36 @@ bool
 iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t
 hwpt_id,

Nit: As per struct iommu_hwpt_invalidate documentation this can be an ID of Nested HWPT or vIOMMU. May be better to rename this just to id.

Thanks,
Shameer

#5 lore raw

RE: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

Hi Shameer,

Quoted 56 lines

-----Original Message-----
From: Shameerali Kolothum Thodi <[email protected]>
Subject: RE: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

Hi Zhenzhong,

Quoted 49 lines

-----Original Message-----
From: Zhenzhong Duan <[email protected]>
Sent: Wednesday, February 19, 2025 8:22 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Zhenzhong Duan <[email protected]>
Subject: [PATCH rfcv2 01/20] backends/iommufd: Add helpers for invalidating user-managed HWPT

---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 30 ++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 34 insertions(+)
include/system/iommufd.h +0 -0
index cbab75bfbf..5d02e9d148 100644
@@ -61,6 +61,9 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,

                                       uint64_t iova, ram_addr_t size,
                                       uint64_t page_size, uint64_t *data,
                                       Error **errp);

+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h +2 -0
+                                     uint32_t data_type, uint32_t entry_len,
+                                     uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD

TYPE_HOST_IOMMU_DEVICE "-iommufd"
#endif

backends/iommufd.c +0 -0
index d57da44755..fc32aad5cb 100644
@@ -311,6 +311,36 @@ bool
 iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
     return true;
 }
 
Quoted 5 lines

backends/iommufd.c bool · line 316

Quoted 2 lines
backends/iommufd.c +1 -0
+int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t
 hwpt_id,

Nit: As per struct iommu_hwpt_invalidate documentation this can be an ID of Nested HWPT or vIOMMU. May be better to rename this just to id.

Sure, will do.

Thanks
Zhenzhong

#6 lore raw

RFC PATCH v2 2/20 vfio/iommufd: Add properties and handlers to TYPE_HOST_IOMMU_DEVICE_IOMMUFD

In reply to Zhenzhong Duan

New added properties include IOMMUFD handle, devid and hwpt_id. IOMMUFD handle and devid are used to allocate/free ioas and hwpt. hwpt_id is used to re-attach IOMMUFD backed device to its default VFIO sub-system created hwpt, i.e., when vIOMMU is disabled by guest. These properties are initialized in .realize_late() handler.

New added handlers include [at|de]tach_hwpt. They are used to attach/detach hwpt. VFIO and VDPA have different way to attach and detach, so implementation will be in sub-class instead of HostIOMMUDeviceIOMMUFD.

Add two wrappers host_iommu_device_iommufd_[at|de]tach_hwpt to wrap the two handlers.

This is a prerequisite patch for following ones.

---
 include/system/iommufd.h | 50 ++++++++++++++++++++++++++++++++++++++++
 backends/iommufd.c       | 22 ++++++++++++++++++
 2 files changed, 72 insertions(+)
include/system/iommufd.h +50 -0
index 5d02e9d148..a871601df5 100644
@@ -66,4 +66,54 @@ int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
                                      uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
+OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass,
+                    HOST_IOMMU_DEVICE_IOMMUFD)
+
+/* Abstract of host IOMMU device with iommufd backend */
+struct HostIOMMUDeviceIOMMUFD {
+    HostIOMMUDevice parent_obj;
+
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
+    uint32_t hwpt_id;
+};
+
+struct HostIOMMUDeviceIOMMUFDClass {
+    HostIOMMUDeviceClass parent_class;
+
+    /**
+     * @attach_hwpt: attach host IOMMU device to IOMMUFD hardware page table.
+     * VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @hwpt_id: ID of IOMMUFD hardware page table.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
+                        Error **errp);
+    /**
+     * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table.
+     * VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, 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);
 #endif
backends/iommufd.c +22 -0
index fc32aad5cb..574f330c27 100644
@@ -341,6 +341,26 @@ int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
     return ret;
 }
 
+bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t hwpt_id, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->attach_hwpt);
+    return idevc->attach_hwpt(idev, hwpt_id, errp);
+}
+
+bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->detach_hwpt);
+    return idevc->detach_hwpt(idev, errp);
+}
+
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
 {
     HostIOMMUDeviceCaps *caps = &hiod->caps;
@@ -379,6 +399,8 @@ static const TypeInfo types[] = {
     }, {
         .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
         .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HostIOMMUDeviceIOMMUFD),
+        .class_size = sizeof(HostIOMMUDeviceIOMMUFDClass),
         .class_init = hiod_iommufd_class_init,
         .abstract = true,
     }
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH rfcv2 02/20] vfio/iommufd: Add properties and handlers to TYPE_HOST_IOMMU_DEVICE_IOMMUFD

Hi Zhenzhong,

Quoted 1 line · Zhenzhong Duan

New added properties include IOMMUFD handle, devid and hwpt_id.

a property generally has an other meaning in qemu (PROP*).

I would rather say you enhance HostIOMMUDeviceIOMMUFD object with 3 new members, specific to the iommufd BE + 2 new class functions.

Quoted 4 lines

IOMMUFD handle and devid are used to allocate/free ioas and hwpt. hwpt_id is used to re-attach IOMMUFD backed device to its default VFIO sub-system created hwpt, i.e., when vIOMMU is disabled by guest. These properties are initialized in .realize_late() handler.

realize_late does not exist yet

Quoted 5 lines

New added handlers include [at|de]tach_hwpt. They are used to attach/detach hwpt. VFIO and VDPA have different way to attach and detach, so implementation will be in sub-class instead of HostIOMMUDeviceIOMMUFD.

this is tricky to follow ...

Quoted 5 lines

Add two wrappers host_iommu_device_iommufd_[at|de]tach_hwpt to wrap the two handlers.

This is a prerequisite patch for following ones.

would get rid of that sentence as it does not help much

Quoted 12 lines
---
 include/system/iommufd.h | 50 ++++++++++++++++++++++++++++++++++++++++
 backends/iommufd.c       | 22 ++++++++++++++++++
 2 files changed, 72 insertions(+)
include/system/iommufd.h +0 -0
index 5d02e9d148..a871601df5 100644

include/system/iommufd.h int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id, · line 72

Quoted 7 lines
include/system/iommufd.h +4 -0
@@ -66,4 +66,54 @@ int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
                                      uint32_t *entry_num, void *data_ptr);
 
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
+OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass,
+                    HOST_IOMMU_DEVICE_IOMMUFD)
+
+/* Abstract of host IOMMU device with iommufd backend */

specialization/overload of the host IOMMU device for the iommufd BE?

Quoted 79 lines
include/system/iommufd.h +46 -0
+struct HostIOMMUDeviceIOMMUFD {
+    HostIOMMUDevice parent_obj;
+
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
+    uint32_t hwpt_id;
+};
+
+struct HostIOMMUDeviceIOMMUFDClass {
+    HostIOMMUDeviceClass parent_class;
+
+    /**
+     * @attach_hwpt: attach host IOMMU device to IOMMUFD hardware page table.
+     * VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @hwpt_id: ID of IOMMUFD hardware page table.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*attach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, uint32_t hwpt_id,
+                        Error **errp);
+    /**
+     * @detach_hwpt: detach host IOMMU device from IOMMUFD hardware page table.
+     * VFIO and VDPA device can have different implementation.
+     *
+     * Mandatory callback.
+     *
+     * @idev: host IOMMU device backed by IOMMUFD backend.
+     *
+     * @errp: pass an Error out when attachment fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*detach_hwpt)(HostIOMMUDeviceIOMMUFD *idev, 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);
 #endif
backends/iommufd.c +20 -0
index fc32aad5cb..574f330c27 100644
@@ -341,6 +341,26 @@ int iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t hwpt_id,
     return ret;
 }
 
+bool host_iommu_device_iommufd_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t hwpt_id, Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->attach_hwpt);
+    return idevc->attach_hwpt(idev, hwpt_id, errp);
+}
+
+bool host_iommu_device_iommufd_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           Error **errp)
+{
+    HostIOMMUDeviceIOMMUFDClass *idevc =
+        HOST_IOMMU_DEVICE_IOMMUFD_GET_CLASS(idev);
+
+    g_assert(idevc->detach_hwpt);
+    return idevc->detach_hwpt(idev, errp);
+}
+
 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
 {
     HostIOMMUDeviceCaps *caps = &hiod->caps;

backends/iommufd.c static const TypeInfo types[] = { · line 406

Quoted 8 lines
backends/iommufd.c +2 -0
@@ -379,6 +399,8 @@ static const TypeInfo types[] = {
     }, {
         .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
         .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HostIOMMUDeviceIOMMUFD),
+        .class_size = sizeof(HostIOMMUDeviceIOMMUFDClass),
         .class_init = hiod_iommufd_class_init,
         .abstract = true,
     }

Thanks

Eric

#8 lore raw

RE: [PATCH rfcv2 02/20] vfio/iommufd: Add properties and handlers to TYPE_HOST_IOMMU_DEVICE_IOMMUFD

Hi Eric,

Quoted 8 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 02/20] vfio/iommufd: Add properties and handlers to TYPE_HOST_IOMMU_DEVICE_IOMMUFD

Hi Zhenzhong,

Quoted 6 lines
Quoted 1 line · Zhenzhong Duan

New added properties include IOMMUFD handle, devid and hwpt_id.

a property generally has an other meaning in qemu (PROP*).

I would rather say you enhance HostIOMMUDeviceIOMMUFD object with 3 new members, specific to the iommufd BE + 2 new class functions.

Will do.

Quoted 6 lines
Quoted 6 lines

IOMMUFD handle and devid are used to allocate/free ioas and hwpt. hwpt_id is used to re-attach IOMMUFD backed device to its default VFIO sub-system created hwpt, i.e., when vIOMMU is disabled by guest. These properties are initialized in .realize_late() handler.

Quoted 7 lines

realize_late does not exist yet

Quoted 5 lines

New added handlers include [at|de]tach_hwpt. They are used to attach/detach hwpt. VFIO and VDPA have different way to attach and detach, so implementation will be in sub-class instead of HostIOMMUDeviceIOMMUFD.

this is tricky to follow ...

I mean implementing [at|de]tach_hwpt in e.g., HostIOMMUDeviceIOMMUFDVFIO.

Quoted 6 lines
Quoted 5 lines

Add two wrappers host_iommu_device_iommufd_[at|de]tach_hwpt to wrap the two handlers.

This is a prerequisite patch for following ones.

would get rid of that sentence as it does not help much

Sure.

Thanks
Zhenzhong

#9 lore raw

RFC PATCH v2 3/20 HostIOMMUDevice: Introduce realize_late callback

In reply to Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment.

---
 include/system/host_iommu_device.h | 17 +++++++++++++++++
 hw/vfio/common.c                   | 17 ++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
include/system/host_iommu_device.h +17 -0
index 809cced4ba..df782598f2 100644
@@ -66,6 +66,23 @@ struct HostIOMMUDeviceClass {
      * Returns: true on success, false on failure.
      */
     bool (*realize)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
+    /**
+     * @realize_late: initialize host IOMMU device instance after attachment,
+     *                some elements e.g., ioas are ready only after attachment.
+     *                This callback initialize them.
+     *
+     * Optional callback.
+     *
+     * @hiod: pointer to a host IOMMU device instance.
+     *
+     * @opaque: pointer to agent device of this host IOMMU device,
+     *          e.g., VFIO base device or VDPA device.
+     *
+     * @errp: pass an Error out when realize fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*realize_late)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
     /**
      * @get_cap: check if a host IOMMU device capability is supported.
      *
hw/vfio/common.c +14 -3
index abbdc56b6d..e198b1e5a2 100644
@@ -1550,6 +1550,7 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
     const VFIOIOMMUClass *ops =
         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
     HostIOMMUDevice *hiod = NULL;
+    HostIOMMUDeviceClass *hiod_ops = NULL;
 
     if (vbasedev->iommufd) {
         ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
@@ -1560,16 +1561,26 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
 
     if (!vbasedev->mdev) {
         hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
+        hiod_ops = HOST_IOMMU_DEVICE_GET_CLASS(hiod);
         vbasedev->hiod = hiod;
     }
 
     if (!ops->attach_device(name, vbasedev, as, errp)) {
-        object_unref(hiod);
-        vbasedev->hiod = NULL;
-        return false;
+        goto err_attach;
+    }
+
+    if (hiod_ops && hiod_ops->realize_late &&
+        !hiod_ops->realize_late(hiod, vbasedev, errp)) {
+        ops->detach_device(vbasedev);
+        goto err_attach;
     }
 
     return true;
+
+err_attach:
+    object_unref(hiod);
+    vbasedev->hiod = NULL;
+    return false;
 }
 
 void vfio_detach_device(VFIODevice *vbasedev)
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

from the description it is not obvious why the realize() could not have been called after the attach. Could you remind the reader what is the reason?

Thanks

Eric

Quoted 80 lines

Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment.

---
 include/system/host_iommu_device.h | 17 +++++++++++++++++
 hw/vfio/common.c                   | 17 ++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
include/system/host_iommu_device.h +17 -0
index 809cced4ba..df782598f2 100644
@@ -66,6 +66,23 @@ struct HostIOMMUDeviceClass {
      * Returns: true on success, false on failure.
      */
     bool (*realize)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
+    /**
+     * @realize_late: initialize host IOMMU device instance after attachment,
+     *                some elements e.g., ioas are ready only after attachment.
+     *                This callback initialize them.
+     *
+     * Optional callback.
+     *
+     * @hiod: pointer to a host IOMMU device instance.
+     *
+     * @opaque: pointer to agent device of this host IOMMU device,
+     *          e.g., VFIO base device or VDPA device.
+     *
+     * @errp: pass an Error out when realize fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*realize_late)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
     /**
      * @get_cap: check if a host IOMMU device capability is supported.
      *
hw/vfio/common.c +14 -3
index abbdc56b6d..e198b1e5a2 100644
@@ -1550,6 +1550,7 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
     const VFIOIOMMUClass *ops =
         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
     HostIOMMUDevice *hiod = NULL;
+    HostIOMMUDeviceClass *hiod_ops = NULL;
 
     if (vbasedev->iommufd) {
         ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
@@ -1560,16 +1561,26 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
 
     if (!vbasedev->mdev) {
         hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
+        hiod_ops = HOST_IOMMU_DEVICE_GET_CLASS(hiod);
         vbasedev->hiod = hiod;
     }
 
     if (!ops->attach_device(name, vbasedev, as, errp)) {
-        object_unref(hiod);
-        vbasedev->hiod = NULL;
-        return false;
+        goto err_attach;
+    }
+
+    if (hiod_ops && hiod_ops->realize_late &&
+        !hiod_ops->realize_late(hiod, vbasedev, errp)) {
+        ops->detach_device(vbasedev);
+        goto err_attach;
     }
 
     return true;
+
+err_attach:
+    object_unref(hiod);
+    vbasedev->hiod = NULL;
+    return false;
 }
 
 void vfio_detach_device(VFIODevice *vbasedev)
#11 lore raw

RE: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 10 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 8 lines
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

from the description it is not obvious why the realize() could not have been called after the attach. Could you remind the reader what is the reason?

Sure, will rephrase as below:

" HostIOMMUDevice provides some elements to vIOMMU, but there are some which are ready after attachment, e.g., hwpt_id.

Before create and attach to a new hwpt with IOMMU dirty tracking capability, we have to call realize() to get if hardware IOMMU supports dirty tracking capability.

So moving realize() after attach() will not work here, we need a new callback realize_late() to further initialize those elements.

Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment. "

Thanks
Zhenzhong

#12 lore raw

Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Hi Zhenzhong,

Quoted 26 lines · Zhenzhong
Quoted 9 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 8 lines
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

from the description it is not obvious why the realize() could not have been called after the attach. Could you remind the reader what is the reason?

Sure, will rephrase as below:

" HostIOMMUDevice provides some elements to vIOMMU, but there are some which are ready after attachment, e.g., hwpt_id.

Before create and attach to a new hwpt with IOMMU dirty tracking capability, we have to call realize() to get if hardware IOMMU supports dirty tracking capability.

Quoted 5 lines

So moving realize() after attach() will not work here, we need a new callback realize_late() to further initialize those elements.

Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment. "

OK this helps me

Thanks

Eric

Quoted 4 lines

Thanks
Zhenzhong

#13 lore raw

Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

In reply to Zhenzhong Duan
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

The relation between objects HostIOMMUDevice and VFIOIOMMU is starting to look too complex for me.

I think it makes sense to realize HostIOMMUDevice after the device is attached. Can't we move :

        hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
        vbasedev->hiod = hiod;

under ->attach_device() and also the call :

    if (!vfio_device_hiod_realize(vbasedev, errp)) {

later in the ->attach_device() patch ?

hiod_legacy_vfio_realize() doesn't do much. We might need to rework hiod_iommufd_vfio_realize() which queries the iommufd hw caps, later used by intel-iommu.

Anyway, it is good time to cleanup our interfaces before adding more.

Thanks,

C.

Quoted 79 lines

Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment.

---
  include/system/host_iommu_device.h | 17 +++++++++++++++++
  hw/vfio/common.c                   | 17 ++++++++++++++---
  2 files changed, 31 insertions(+), 3 deletions(-)
include/system/host_iommu_device.h +17 -0
index 809cced4ba..df782598f2 100644
@@ -66,6 +66,23 @@ struct HostIOMMUDeviceClass {
       * Returns: true on success, false on failure.
       */
      bool (*realize)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
+    /**
+     * @realize_late: initialize host IOMMU device instance after attachment,
+     *                some elements e.g., ioas are ready only after attachment.
+     *                This callback initialize them.
+     *
+     * Optional callback.
+     *
+     * @hiod: pointer to a host IOMMU device instance.
+     *
+     * @opaque: pointer to agent device of this host IOMMU device,
+     *          e.g., VFIO base device or VDPA device.
+     *
+     * @errp: pass an Error out when realize fails.
+     *
+     * Returns: true on success, false on failure.
+     */
+    bool (*realize_late)(HostIOMMUDevice *hiod, void *opaque, Error **errp);
      /**
       * @get_cap: check if a host IOMMU device capability is supported.
       *
hw/vfio/common.c +14 -3
index abbdc56b6d..e198b1e5a2 100644
@@ -1550,6 +1550,7 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
      const VFIOIOMMUClass *ops =
          VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
      HostIOMMUDevice *hiod = NULL;
+    HostIOMMUDeviceClass *hiod_ops = NULL;
 
      if (vbasedev->iommufd) {
          ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
@@ -1560,16 +1561,26 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,
 
      if (!vbasedev->mdev) {
          hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
+        hiod_ops = HOST_IOMMU_DEVICE_GET_CLASS(hiod);
          vbasedev->hiod = hiod;
      }
 
      if (!ops->attach_device(name, vbasedev, as, errp)) {
-        object_unref(hiod);
-        vbasedev->hiod = NULL;
-        return false;
+        goto err_attach;
+    }
+
+    if (hiod_ops && hiod_ops->realize_late &&
+        !hiod_ops->realize_late(hiod, vbasedev, errp)) {
+        ops->detach_device(vbasedev);
+        goto err_attach;
      }
 
      return true;
+
+err_attach:
+    object_unref(hiod);
+    vbasedev->hiod = NULL;
+    return false;
  }
 
  void vfio_detach_device(VFIODevice *vbasedev)
#14 lore raw

Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 17 lines · Cédric Le Goater
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

The relation between objects HostIOMMUDevice and VFIOIOMMU is starting to look too complex for me.

I think it makes sense to realize HostIOMMUDevice after the device is attached. Can't we move :

        hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); vbasedev->hiod = hiod;

under ->attach_device() and also the call :

Quoted 7 lines

    if (!vfio_device_hiod_realize(vbasedev, errp)) {

later in the ->attach_device() patch ?

hiod_legacy_vfio_realize() doesn't do much. We might need to rework hiod_iommufd_vfio_realize() which queries the iommufd hw caps, later used by intel-iommu.

The only dependency I see on the IOMMUFD HostIOMMUDevice when attaching the device to the container is in iommufd_cdev_autodomains_get(). The flags for IOMMU_HWPT_ALLOC depends on the HW capability of the IOMMFD backend and we rely on hiod_iommufd_vfio_realize() to have done the query on the iommufd kernel device before.

Since this is not a hot path, I don't think it is a problem to add a redundant call to iommufd_backend_get_device_info() in iommufd_cdev_autodomains_get() and avoid the IOMMUFD HostIOMMUDevice dependency. With that we can move the HostIOMMUDevice creation and realize sequence at the end of the device attach sequence.

I think this makes the code cleaner when it comes to using the vbasedev->hiod pointer too.

Quoted 1 line

Anyway, it is good time to cleanup our interfaces before adding more.

On that topic, I think

iommufd_cdev_attach_ioas_hwpt
iommufd_cdev_detach_ioas_hwpt

belong to IOMMUFD backend.

Thanks,

C.

#15 lore raw

RE: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 8 lines · Cédric Le Goater
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

The relation between objects HostIOMMUDevice and VFIOIOMMU is starting to look too complex for me.

Agree.

Quoted 23 lines
Quoted 9 lines

I think it makes sense to realize HostIOMMUDevice after the device is attached. Can't we move :

        hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); vbasedev->hiod = hiod;

under ->attach_device() and also the call :

Quoted 7 lines

    if (!vfio_device_hiod_realize(vbasedev, errp)) {

later in the ->attach_device() patch ?

hiod_legacy_vfio_realize() doesn't do much. We might need to rework hiod_iommufd_vfio_realize() which queries the iommufd hw caps, later used by intel-iommu.

The only dependency I see on the IOMMUFD HostIOMMUDevice when attaching the device to the container is in iommufd_cdev_autodomains_get(). The flags for IOMMU_HWPT_ALLOC depends on the HW capability of the IOMMFD backend and we rely on hiod_iommufd_vfio_realize() to have done the query on the iommufd kernel device before.

Quoted 5 lines

Since this is not a hot path, I don't think it is a problem to add a redundant call to iommufd_backend_get_device_info() in iommufd_cdev_autodomains_get() and avoid the IOMMUFD HostIOMMUDevice dependency. With that we can move the HostIOMMUDevice creation and realize sequence at the end of the device attach sequence.

Yes.

Quoted 5 lines

I think this makes the code cleaner when it comes to using the vbasedev->hiod pointer too.

Quoted 1 line

Anyway, it is good time to cleanup our interfaces before adding more.

OK, let me think about this further and write some patches to move .realize() after .attach_device(). will be based on vfio-next.

Quoted 7 lines

On that topic, I think

iommufd_cdev_attach_ioas_hwpt
iommufd_cdev_detach_ioas_hwpt

belong to IOMMUFD backend.

They are operation on VFIODevice, backends/iommufd.c are for operation on IOMMUFDBackend, Do we need to move iommufd_cdev_attach/detach_ioas_hwpt to backends/iommufd.c which is VFIODevice agnostic?

Thanks
Zhenzhong

#16 lore raw

Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 51 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv2 03/20] HostIOMMUDevice: Introduce realize_late callback

Quoted 8 lines · Cédric Le Goater
Quoted 4 lines · Zhenzhong Duan

Currently we have realize() callback which is called before attachment. But there are still some elements e.g., hwpt_id is not ready before attachment. So we need a realize_late() callback to further initialize them.

The relation between objects HostIOMMUDevice and VFIOIOMMU is starting to look too complex for me.

Agree.

Quoted 23 lines
Quoted 9 lines

I think it makes sense to realize HostIOMMUDevice after the device is attached. Can't we move :

          hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
          vbasedev->hiod = hiod;

under ->attach_device() and also the call :

Quoted 7 lines
      if (!vfio_device_hiod_realize(vbasedev, errp)) {

later in the ->attach_device() patch ?

hiod_legacy_vfio_realize() doesn't do much. We might need to rework hiod_iommufd_vfio_realize() which queries the iommufd hw caps, later used by intel-iommu.

The only dependency I see on the IOMMUFD HostIOMMUDevice when attaching the device to the container is in iommufd_cdev_autodomains_get(). The flags for IOMMU_HWPT_ALLOC depends on the HW capability of the IOMMFD backend and we rely on hiod_iommufd_vfio_realize() to have done the query on the iommufd kernel device before.

Quoted 5 lines

Since this is not a hot path, I don't think it is a problem to add a redundant call to iommufd_backend_get_device_info() in iommufd_cdev_autodomains_get() and avoid the IOMMUFD HostIOMMUDevice dependency. With that we can move the HostIOMMUDevice creation and realize sequence at the end of the device attach sequence.

Yes.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

I think this makes the code cleaner when it comes to using the vbasedev->hiod pointer too.

Quoted 1 line

Anyway, it is good time to cleanup our interfaces before adding more.

OK, let me think about this further and write some patches to move .realize() after .attach_device(). will be based on vfio-next.

I just updated the vfio-next branch with what should be in the next PR for QEMU 10.1.

Quoted 11 lines
Quoted 8 lines

On that topic, I think

iommufd_cdev_attach_ioas_hwpt
iommufd_cdev_detach_ioas_hwpt

belong to IOMMUFD backend.

They are operation on VFIODevice, backends/iommufd.c are for operation on IOMMUFDBackend, Do we need to move iommufd_cdev_attach/detach_ioas_hwpt to backends/iommufd.c which is VFIODevice agnostic?

My mistake. I was confused with

int iommufd = vbasedev->iommufd->fd

and thought we could simply replace 'VFIODevice *' parameter with a 'IOMMUFDBackend *be' parameter but this is not the case.

Thanks,

C.

#17 lore raw

RFC PATCH v2 4/20 vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler

In reply to Zhenzhong Duan

There are three iommufd related elements iommufd handle, devid and hwpt_id. hwpt_id is ready only after VFIO device attachment. Device id and iommufd handle are ready before attachment, but they are all iommufd related stuff, initialize them together with hwpt_id.

---
 hw/vfio/iommufd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/iommufd.c +14 -0
index df61edffc0..53639bf88b 100644
@@ -828,6 +828,19 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     return true;
 }
 
+static bool hiod_iommufd_vfio_realize_late(HostIOMMUDevice *hiod, void *opaque,
+                                           Error **errp)
+{
+    VFIODevice *vdev = opaque;
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(hiod);
+
+    idev->iommufd = vdev->iommufd;
+    idev->devid = vdev->devid;
+    idev->hwpt_id = vdev->hwpt->hwpt_id;
+
+    return true;
+}
+
 static GList *
 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
 {
@@ -852,6 +865,7 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
+    hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
 };
Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH rfcv2 04/20] vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler

Quoted 1 line · Zhenzhong Duan

There are three iommufd related elements iommufd handle, devid and

There are three iommufd specific members in HostIOMMUDevice IOMMUFD that need to be initialized after attach on realize_late() ...

Quoted 41 lines

hwpt_id. hwpt_id is ready only after VFIO device attachment. Device id and iommufd handle are ready before attachment, but they are all iommufd related stuff, initialize them together with hwpt_id.

---
 hw/vfio/iommufd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/iommufd.c +14 -0
index df61edffc0..53639bf88b 100644
@@ -828,6 +828,19 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     return true;
 }
 
+static bool hiod_iommufd_vfio_realize_late(HostIOMMUDevice *hiod, void *opaque,
+                                           Error **errp)
+{
+    VFIODevice *vdev = opaque;
+    HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(hiod);
+
+    idev->iommufd = vdev->iommufd;
+    idev->devid = vdev->devid;
+    idev->hwpt_id = vdev->hwpt->hwpt_id;
+
+    return true;
+}
+
 static GList *
 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
 {
@@ -852,6 +865,7 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
+    hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
 };
#19 lore raw

RE: [PATCH rfcv2 04/20] vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler

Quoted 10 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 04/20] vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler

Quoted 5 lines
Quoted 1 line · Zhenzhong Duan

There are three iommufd related elements iommufd handle, devid and

There are three iommufd specific members in HostIOMMUDevice IOMMUFD that need to be initialized after attach on realize_late() ...

Will do.

Thanks
Zhenzhong

Quoted 46 lines

hw/vfio/iommufd.c static bool

Quoted 15 lines

hwpt_id. hwpt_id is ready only after VFIO device attachment. Device id and iommufd handle are ready before attachment, but they are all iommufd related stuff, initialize them together with hwpt_id.

---
 hw/vfio/iommufd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/iommufd.c +0 -0
index df61edffc0..53639bf88b 100644
@@ -828,6 +828,19 @@ static bool

hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

hw/vfio/iommufd.c static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
     return true;
 }
 
+static bool hiod_iommufd_vfio_realize_late(HostIOMMUDevice *hiod, void

*opaque,

hw/vfio/iommufd.c static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

Quoted 4 lines
hw/vfio/iommufd.c +4 -0
+                                           Error **errp)
+{
+    VFIODevice *vdev = opaque;
+    HostIOMMUDeviceIOMMUFD *idev =

HOST_IOMMU_DEVICE_IOMMUFD(hiod);

hw/vfio/iommufd.c static void hiod_iommufd_vfio_class_init(ObjectClass

Quoted 12 lines
hw/vfio/iommufd.c +8 -0
+
+    idev->iommufd = vdev->iommufd;
+    idev->devid = vdev->devid;
+    idev->hwpt_id = vdev->hwpt->hwpt_id;
+
+    return true;
+}
+
 static GList *
 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
 {
@@ -852,6 +865,7 @@ static void hiod_iommufd_vfio_class_init(ObjectClass

*oc, void *data)

Quoted 7 lines
hw/vfio/iommufd.c +1 -0
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
+    hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
 };
#20 lore raw

RFC PATCH v2 5/20 vfio/iommufd: Implement [at|de]tach_hwpt handlers

In reply to Zhenzhong Duan

Implement [at|de]tach_hwpt handlers in VFIO subsystem. vIOMMU utilizes them to attach to or detach from hwpt on host side.

---
 hw/vfio/iommufd.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
hw/vfio/iommufd.c +22 -0
index 53639bf88b..175c4fe1f4 100644
@@ -802,6 +802,24 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
     vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
 };
 
+static bool
+host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t hwpt_id, Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return !iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt_id, errp);
+}
+
+static bool
+host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp);
+}
+
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
                                       Error **errp)
 {
@@ -863,11 +881,15 @@ hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
 {
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+    HostIOMMUDeviceIOMMUFDClass *idevc = HOST_IOMMU_DEVICE_IOMMUFD_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
     hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
+
+    idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt;
+    idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt;
 };
 
 static const TypeInfo types[] = {
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH rfcv2 05/20] vfio/iommufd: Implement [at|de]tach_hwpt handlers

Quoted 14 lines · Zhenzhong Duan

Implement [at|de]tach_hwpt handlers in VFIO subsystem. vIOMMU utilizes them to attach to or detach from hwpt on host side.

---
 hw/vfio/iommufd.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
hw/vfio/iommufd.c +0 -0
index 53639bf88b..175c4fe1f4 100644

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) · line 805

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
@@ -802,6 +802,24 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
     vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
 };
 
+static bool

can't we return an integer instead. This looks more standard to me

Eric

Quoted 36 lines
hw/vfio/iommufd.c +21 -0
+host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           uint32_t hwpt_id, Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return !iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt_id, errp);
+}
+
+static bool
+host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev,
+                                           Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp);
+}
+
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
                                       Error **errp)
 {
@@ -863,11 +881,15 @@ hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
 {
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+    HostIOMMUDeviceIOMMUFDClass *idevc = HOST_IOMMU_DEVICE_IOMMUFD_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
     hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
+
+    idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt;
+    idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt;
 };
 
 static const TypeInfo types[] = {
#22 lore raw

RE: [PATCH rfcv2 05/20] vfio/iommufd: Implement [at|de]tach_hwpt handlers

Quoted 26 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 05/20] vfio/iommufd: Implement [at|de]tach_hwpt handlers

hw/vfio/iommufd.c static void

Quoted 14 lines · Zhenzhong Duan

Implement [at|de]tach_hwpt handlers in VFIO subsystem. vIOMMU utilizes them to attach to or detach from hwpt on host side.

---
 hw/vfio/iommufd.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
hw/vfio/iommufd.c +0 -0
index 53639bf88b..175c4fe1f4 100644
@@ -802,6 +802,24 @@ static void

vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)

Quoted 5 lines

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) · line 805

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
     vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
 };
 
+static bool

can't we return an integer instead. This looks more standard to me

I can do that, but I remember VFIO honors bool return value whenever possible. We had ever cleanup patches to make all functions return bool when possible. Do we really want to return int for only these two functions?

Thanks
Zhenzhong

Quoted 45 lines

Eric

Quoted 1 line

+host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) · line 814

Quoted 9 lines
hw/vfio/iommufd.c +8 -0
+                                           uint32_t hwpt_id, Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return !iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt_id, errp);
+}
+
+static bool

+host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUF D *idev,

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)

Quoted 8 lines
hw/vfio/iommufd.c +7 -0
+                                           Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp);
+}
+
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void

*opaque,

hw/vfio/iommufd.c

Quoted 3 lines

Error **errp)
{

hw/vfio/iommufd.c +0 -0
@@ -863,11 +881,15 @@

hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)

hw/vfio/iommufd.c hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
 {
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+    HostIOMMUDeviceIOMMUFDClass *idevc =

HOST_IOMMU_DEVICE_IOMMUFD_CLASS(oc);

Quoted 11 lines
hw/vfio/iommufd.c +3 -0
     hiodc->realize = hiod_iommufd_vfio_realize;
     hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
+
+    idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt;
+    idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt;
 };
 
 static const TypeInfo types[] = {
#23 lore raw

Re: [PATCH rfcv2 05/20] vfio/iommufd: Implement [at|de]tach_hwpt handlers

Quoted 28 lines · Zhenzhong
Quoted 28 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 05/20] vfio/iommufd: Implement [at|de]tach_hwpt handlers

hw/vfio/iommufd.c static void

Quoted 14 lines · Zhenzhong Duan

Implement [at|de]tach_hwpt handlers in VFIO subsystem. vIOMMU utilizes them to attach to or detach from hwpt on host side.

---
 hw/vfio/iommufd.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
hw/vfio/iommufd.c +0 -0
index 53639bf88b..175c4fe1f4 100644
@@ -802,6 +802,24 @@ static void

vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
     vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
 };
 
Quoted 6 lines
Quoted 2 lines

hw/vfio/iommufd.c static void · line 805

Quoted 1 line
hw/vfio/iommufd.c +1 -0
+static bool

can't we return an integer instead. This looks more standard to me

I can do that, but I remember VFIO honors bool return value whenever possible. We had ever cleanup patches to make all functions return bool when possible. Do we really want to return int for only these two functions?

I now remember those patches from Cédric. As I mentionned realier I have not found in the errp doc that this was a requirement but nevertheless ignore this comment then ;-)

Eric

Quoted 46 lines

Thanks
Zhenzhong

Quoted 42 lines

Eric +host_iommu_device_iommufd_vfio_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev,

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)

Quoted 9 lines
hw/vfio/iommufd.c +8 -0
+                                           uint32_t hwpt_id, Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return !iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt_id, errp);
+}
+
+static bool
 

+host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUF D *idev,

hw/vfio/iommufd.c static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)

Quoted 8 lines
hw/vfio/iommufd.c +7 -0
+                                           Error **errp)
+{
+    VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent;
+
+    return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp);
+}
+
 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void

*opaque,

hw/vfio/iommufd.c

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
                                       Error **errp)
 {
@@ -863,11 +881,15 @@

hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)

hw/vfio/iommufd.c hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
 {
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+    HostIOMMUDeviceIOMMUFDClass *idevc =

HOST_IOMMU_DEVICE_IOMMUFD_CLASS(oc);

Quoted 10 lines
hw/vfio/iommufd.c +3 -0
     hiodc->realize = hiod_iommufd_vfio_realize;
     hiodc->realize_late = hiod_iommufd_vfio_realize_late;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
+
+    idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt;
+    idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt;
 };
 
 static const TypeInfo types[] = {
#24 lore raw

RFC PATCH v2 6/20 host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

In reply to Zhenzhong Duan
---
 include/system/host_iommu_device.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/system/host_iommu_device.h +8 -0
index df782598f2..18f8b5e5cf 100644
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl)
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -122,6 +128,8 @@ struct HostIOMMUDeviceClass {
  */
 #define HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE        0
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
+#define HOST_IOMMU_DEVICE_CAP_NESTING           2
+#define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
Signature · 1 line
2.34.1
#25 lore raw

Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Hi Zhenzhong,

Quoted 13 lines · Zhenzhong Duan
---
 include/system/host_iommu_device.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/system/host_iommu_device.h +0 -0
index df782598f2..18f8b5e5cf 100644
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

include/system/host_iommu_device.h struct HostIOMMUDeviceClass { · line 135

Quoted 12 lines
include/system/host_iommu_device.h +2 -0
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -122,6 +128,8 @@ struct HostIOMMUDeviceClass {
  */
 #define HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE        0
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
+#define HOST_IOMMU_DEVICE_CAP_NESTING           2
+#define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif

Maybe you could introduce the associated implementation of hiod_iommufd_get_cap in this patch too?

Eric

#26 lore raw

Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 39 lines · Eric Auger

Hi Zhenzhong,

Quoted 13 lines · Zhenzhong Duan
---
 include/system/host_iommu_device.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/system/host_iommu_device.h +0 -0
index df782598f2..18f8b5e5cf 100644
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

Quoted 10 lines
include/system/host_iommu_device.h +2 -0
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -122,6 +128,8 @@ struct HostIOMMUDeviceClass {
  */
 #define HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE        0
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
+#define HOST_IOMMU_DEVICE_CAP_NESTING           2
+#define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 
Quoted 5 lines

include/system/host_iommu_device.h struct HostIOMMUDeviceClass { · line 135

Quoted 2 lines
include/system/host_iommu_device.h +0 -0
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif

Maybe you could introduce the associated implementation of hiod_iommufd_get_cap in this patch too?

ignore this last comment :(

Eric

Quoted 2 lines

Eric

#27 lore raw

RE: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

In reply to Eric Auger

Hi Eric,

Quoted 35 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Hi Zhenzhong,

Review

Quoted 6 lines · Zhenzhong Duan
---
 include/system/host_iommu_device.h | 8 ++++++++
 1 file changed, 8 insertions(+)
(quoted diff) +0 -0

b/include/system/host_iommu_device.h

include/system/host_iommu_device.h line 23

Quoted 6 lines

index df782598f2..18f8b5e5cf 100644

include/system/host_iommu_device.h +0 -0
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this

represents

include/system/host_iommu_device.h line 24

Quoted 1 line
include/system/host_iommu_device.h +0 -0
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO

ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;
Quoted 2 lines

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

Yes? I guess ARM hw could also provide nesting support at least?

There are some reasons I perfer a flatten struct even if some Elements may be vendor specific.
1. If a vendor doesn't support an capability for other vendor, corresponding element should be zero by default.
2. An element vendor specific may become generic in future and we don't need to update the structure when that happens.
3. vIOMMU calls get_cap() to query if a capability is supported, so a vIOMMU never query a vendor specific capability it doesn't recognize. Even if that happens, zero is returned hinting no support.

Thanks
Zhenzhong

#28 lore raw

Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 39 lines · Zhenzhong

Hi Eric,

Quoted 35 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Hi Zhenzhong,

Review

Quoted 6 lines · Zhenzhong Duan
---
 include/system/host_iommu_device.h | 8 ++++++++
 1 file changed, 8 insertions(+)
(quoted diff) +0 -0

b/include/system/host_iommu_device.h

include/system/host_iommu_device.h line 23

Quoted 6 lines

index df782598f2..18f8b5e5cf 100644

include/system/host_iommu_device.h +0 -0
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this

represents

include/system/host_iommu_device.h line 24

Quoted 1 line
include/system/host_iommu_device.h +0 -0
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO

ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;
Quoted 2 lines

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

Quoted 10 lines

Yes? I guess ARM hw could also provide nesting support at least There are some reasons I perfer a flatten struct even if some Elements may be vendor specific.
1. If a vendor doesn't support an capability for other vendor, corresponding element should be zero by default.
2. An element vendor specific may become generic in future and we don't need to update the structure when that happens.
3. vIOMMU calls get_cap() to query if a capability is supported, so a vIOMMU never query a vendor specific capability it doesn't recognize. Even if that happens, zero is returned hinting no support.

I will let others comment but in general this is frown upon and unions are prefered at least.

Eric

Quoted 4 lines

Thanks
Zhenzhong

#29 lore raw

Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 29 lines · Eric Auger
Quoted 19 lines
Quoted 17 lines

Review line 23

Quoted 4 lines

+++ b/include/system/host_iommu_device.h

(quoted diff) +0 -0
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this

represents

Review line 24

Quoted 1 line
(quoted diff) +0 -0
  *           the @out_capabilities value returned from IOMMU_GET_HW_INFO

ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;
Quoted 2 lines

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

Quoted 10 lines

Yes? I guess ARM hw could also provide nesting support at least There are some reasons I perfer a flatten struct even if some Elements may be vendor specific.
1. If a vendor doesn't support an capability for other vendor, corresponding element should be zero by default.
2. An element vendor specific may become generic in future and we don't need to update the structure when that happens.
3. vIOMMU calls get_cap() to query if a capability is supported, so a vIOMMU never query a vendor specific capability it doesn't recognize. Even if that happens, zero is returned hinting no support.

Quoted 2 lines

I will let others comment but in general this is frown upon and unions are prefered at least.

Yea, it feels odd to me that we stuff vendor specific thing in the public structure.

It's okay if we want to store in HostIOMMUDeviceCaps the vendor specific data pointer (opaque), just for convenience.

I think we can have another PCIIOMMUOps op for vendor code to run iommufd_backend_get_device_info() that returns the hw_caps for the core code to read.

Or perhaps the vendor code can just return a HWPT directly? If IOMMU_HW_CAP_DIRTY_TRACKING is set in the hw_caps, the vendor code can allocate a HWPT for that. And if vendor code detects the "nesting" cap in vendor struct, then return a nest_parent HWPT. And returning NULL can let core code allocate a default HWPT (or just attach the device to IOAS for auto domain/hwpt).

I am also hoping that this can handle a shared S2 nest_parent HWPT case. Could the core container structure or so store the HWPT?

Thanks
Nicolin

#30 lore raw

RE: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Hi Eric, Nicolin,

Quoted 56 lines

-----Original Message-----
From: Nicolin Chen <[email protected]>
Subject: Re: [PATCH rfcv2 06/20] host_iommu_device: Define two new capabilities HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 6 lines · Eric Auger
Quoted 6 lines
Quoted 6 lines

Review line 23

Quoted 4 lines

+++ b/include/system/host_iommu_device.h

(quoted diff) +0 -0
@@ -22,10 +22,16 @@
  *
  * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this

represents

Quoted 1 line
(quoted diff) +0 -0
  *           the @out_capabilities value returned from

IOMMU_GET_HW_INFO

Quoted 23 lines
Quoted 13 lines
Quoted 11 lines

ioctl)

include/system/host_iommu_device.h line 34

Quoted 10 lines
include/system/host_iommu_device.h +6 -0
+ *
+ * @nesting: nesting page table support.
+ *
+ * @fs1gp: first stage(a.k.a, Stage-1) 1GB huge page support.
  */
 typedef struct HostIOMMUDeviceCaps {
     uint32_t type;
     uint64_t hw_caps;
+    bool nesting;
+    bool fs1gp;
Quoted 2 lines

this looks quite vtd specific, isn't it? Shouldn't we hide this is a vendor specific cap struct?

Quoted 10 lines

Yes? I guess ARM hw could also provide nesting support at least There are some reasons I perfer a flatten struct even if some Elements may be vendor specific.
1. If a vendor doesn't support an capability for other vendor, corresponding element should be zero by default.
2. An element vendor specific may become generic in future and we don't need to update the structure when that happens.
3. vIOMMU calls get_cap() to query if a capability is supported, so a vIOMMU never query a vendor specific capability it doesn't recognize. Even if that happens, zero is returned hinting no support.

Quoted 2 lines

I will let others comment but in general this is frown upon and unions are prefered at least.

Yea, it feels odd to me that we stuff vendor specific thing in the public structure.

It's okay if we want to store in HostIOMMUDeviceCaps the vendor specific data pointer (opaque), just for convenience.

I think we can have another PCIIOMMUOps op for vendor code to run iommufd_backend_get_device_info() that returns the hw_caps for the core code to read.

Or perhaps the vendor code can just return a HWPT directly? If IOMMU_HW_CAP_DIRTY_TRACKING is set in the hw_caps, the vendor code can allocate a HWPT for that. And if vendor code detects the "nesting" cap in vendor struct, then return a nest_parent HWPT. And returning NULL can let core code allocate a default HWPT (or just attach the device to IOAS for auto domain/hwpt).

Quoted 3 lines

I am also hoping that this can handle a shared S2 nest_parent HWPT case. Could the core container structure or so store the HWPT?

Thanks for your suggestions. It is becoming clear for me. I'll update the code and send a new version after I finish a higher priority work in my company.

Thanks
Zhenzhong

#31 lore raw

RFC PATCH v2 7/20 iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

In reply to Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP] for IOMMUFD backed host IOMMU device.

Query on these two capabilities is not supported for legacy backend because there is no plan to support nesting with leacy backend backed host device.

---
 hw/i386/intel_iommu_internal.h |  1 +
 backends/iommufd.c             |  4 ++++
 hw/vfio/iommufd.c              | 11 +++++++++++
 3 files changed, 16 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index e8b211e8b0..2cda744786 100644
@@ -191,6 +191,7 @@
 #define VTD_ECAP_PT                 (1ULL << 6)
 #define VTD_ECAP_SC                 (1ULL << 7)
 #define VTD_ECAP_MHMV               (15ULL << 20)
+#define VTD_ECAP_NEST               (1ULL << 26)
 #define VTD_ECAP_SRS                (1ULL << 31)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
backends/iommufd.c +4 -0
index 574f330c27..0a1a40cbba 100644
@@ -370,6 +370,10 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
         return caps->type;
     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
         return vfio_device_get_aw_bits(hiod->agent);
+    case HOST_IOMMU_DEVICE_CAP_NESTING:
+        return caps->nesting;
+    case HOST_IOMMU_DEVICE_CAP_FS1GP:
+        return caps->fs1gp;
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +11 -0
index 175c4fe1f4..df6a12d200 100644
@@ -26,6 +26,7 @@
 #include "qemu/chardev_open.h"
 #include "pci.h"
 #include "exec/ram_addr.h"
+#include "hw/i386/intel_iommu_internal.h"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly)
@@ -843,6 +844,16 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     caps->type = type;
     caps->hw_caps = hw_caps;
 
+    switch (type) {
+    case IOMMU_HW_INFO_TYPE_INTEL_VTD:
+        caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
+        caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        break;
+    case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
+    case IOMMU_HW_INFO_TYPE_NONE:
+        break;
+    }
+
     return true;
 }
 
Signature · 1 line
2.34.1
#32 lore raw

Re: [PATCH rfcv2 07/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 31 lines · Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP] for IOMMUFD backed host IOMMU device.

Query on these two capabilities is not supported for legacy backend because there is no plan to support nesting with leacy backend backed host device.

---
 hw/i386/intel_iommu_internal.h |  1 +
 backends/iommufd.c             |  4 ++++
 hw/vfio/iommufd.c              | 11 +++++++++++
 3 files changed, 16 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index e8b211e8b0..2cda744786 100644
@@ -191,6 +191,7 @@
 #define VTD_ECAP_PT                 (1ULL << 6)
 #define VTD_ECAP_SC                 (1ULL << 7)
 #define VTD_ECAP_MHMV               (15ULL << 20)
+#define VTD_ECAP_NEST               (1ULL << 26)
 #define VTD_ECAP_SRS                (1ULL << 31)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
backends/iommufd.c +0 -0
index 574f330c27..0a1a40cbba 100644

backends/iommufd.c static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp) · line 376

Quoted 7 lines
backends/iommufd.c +4 -0
@@ -370,6 +370,10 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
         return caps->type;
     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
         return vfio_device_get_aw_bits(hiod->agent);
+    case HOST_IOMMU_DEVICE_CAP_NESTING:
+        return caps->nesting;
+    case HOST_IOMMU_DEVICE_CAP_FS1GP:
+        return caps->fs1gp;

this is vtd specific so those caps shouldn't be return for other iommus, no?

Eric

Quoted 32 lines
backends/iommufd.c +0 -0
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +11 -0
index 175c4fe1f4..df6a12d200 100644
@@ -26,6 +26,7 @@
 #include "qemu/chardev_open.h"
 #include "pci.h"
 #include "exec/ram_addr.h"
+#include "hw/i386/intel_iommu_internal.h"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly)
@@ -843,6 +844,16 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     caps->type = type;
     caps->hw_caps = hw_caps;
 
+    switch (type) {
+    case IOMMU_HW_INFO_TYPE_INTEL_VTD:
+        caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
+        caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        break;
+    case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
+    case IOMMU_HW_INFO_TYPE_NONE:
+        break;
+    }
+
     return true;
 }
 
#33 lore raw

RE: [PATCH rfcv2 07/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 44 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 07/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]

Quoted 1 line · Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP] for

IOMMUFD

backends/iommufd.c static int hiod_iommufd_get_cap(HostIOMMUDevice

Quoted 30 lines

backed host IOMMU device.

Query on these two capabilities is not supported for legacy backend because there is no plan to support nesting with leacy backend backed host device.

---
 hw/i386/intel_iommu_internal.h |  1 +
 backends/iommufd.c             |  4 ++++
 hw/vfio/iommufd.c              | 11 +++++++++++
 3 files changed, 16 insertions(+)
hw/i386/intel_iommu_internal.h +1 -0
index e8b211e8b0..2cda744786 100644
@@ -191,6 +191,7 @@
 #define VTD_ECAP_PT                 (1ULL << 6)
 #define VTD_ECAP_SC                 (1ULL << 7)
 #define VTD_ECAP_MHMV               (15ULL << 20)
+#define VTD_ECAP_NEST               (1ULL << 26)
 #define VTD_ECAP_SRS                (1ULL << 31)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
backends/iommufd.c +0 -0
index 574f330c27..0a1a40cbba 100644
@@ -370,6 +370,10 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice

*hiod, int cap, Error **errp)

Quoted 8 lines

backends/iommufd.c static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp) · line 376

Quoted 7 lines
backends/iommufd.c +4 -0
         return caps->type;
     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
         return vfio_device_get_aw_bits(hiod->agent);
+    case HOST_IOMMU_DEVICE_CAP_NESTING:
+        return caps->nesting;
+    case HOST_IOMMU_DEVICE_CAP_FS1GP:
+        return caps->fs1gp;

this is vtd specific so those caps shouldn't be return for other iommus, no?

vIOMMU should not query a CAP it doesn't recognize, even if that happen, zero is returned hinting that CAP isn't supported for this vIOMMU.

Thanks
Zhenzhong

Quoted 36 lines

Eric

Quoted 8 lines
backends/iommufd.c +0 -0
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +0 -0
index 175c4fe1f4..df6a12d200 100644

hw/vfio/iommufd.c line 31

Quoted 6 lines
hw/vfio/iommufd.c +1 -0
@@ -26,6 +26,7 @@
 #include "qemu/chardev_open.h"
 #include "pci.h"
 #include "exec/ram_addr.h"
+#include "hw/i386/intel_iommu_internal.h"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr

iova,

hw/vfio/iommufd.c static bool

Quoted 2 lines
hw/vfio/iommufd.c +0 -0
                             ram_addr_t size, void *vaddr, bool readonly)
@@ -843,6 +844,16 @@ static bool

hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

Quoted 16 lines
hw/vfio/iommufd.c +10 -0
     caps->type = type;
     caps->hw_caps = hw_caps;
 
+    switch (type) {
+    case IOMMU_HW_INFO_TYPE_INTEL_VTD:
+        caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
+        caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        break;
+    case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
+    case IOMMU_HW_INFO_TYPE_NONE:
+        break;
+    }
+
     return true;
 }
 
#34 lore raw

RFC PATCH v2 8/20 iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA

In reply to Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA for IOMMUFD backed host IOMMU device.

Query on this capability is not supported for legacy backend because there is no plan to support nesting with leacy backend backed host device.

---
 include/system/host_iommu_device.h | 2 ++
 backends/iommufd.c                 | 2 ++
 hw/vfio/iommufd.c                  | 1 +
 3 files changed, 5 insertions(+)
include/system/host_iommu_device.h +2 -0
index 18f8b5e5cf..250600fc1d 100644
@@ -32,6 +32,7 @@ typedef struct HostIOMMUDeviceCaps {
     uint64_t hw_caps;
     bool nesting;
     bool fs1gp;
+    uint32_t errata;
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -130,6 +131,7 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
 #define HOST_IOMMU_DEVICE_CAP_NESTING           2
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
+#define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
backends/iommufd.c +2 -0
index 0a1a40cbba..3c23caef96 100644
@@ -374,6 +374,8 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
         return caps->nesting;
     case HOST_IOMMU_DEVICE_CAP_FS1GP:
         return caps->fs1gp;
+    case HOST_IOMMU_DEVICE_CAP_ERRATA:
+        return caps->errata;
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +1 -0
index df6a12d200..58bff030e1 100644
@@ -848,6 +848,7 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     case IOMMU_HW_INFO_TYPE_INTEL_VTD:
         caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
         caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        caps->errata = data.vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17;
         break;
     case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
     case IOMMU_HW_INFO_TYPE_NONE:
Signature · 1 line
2.34.1
#35 lore raw

Re: [PATCH rfcv2 08/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA

Quoted 5 lines · Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA for IOMMUFD backed host IOMMU device.

Query on this capability is not supported for legacy backend because there is no plan to support nesting with leacy backend

legacy

Quoted 14 lines

backed host device.

---
 include/system/host_iommu_device.h | 2 ++
 backends/iommufd.c                 | 2 ++
 hw/vfio/iommufd.c                  | 1 +
 3 files changed, 5 insertions(+)
include/system/host_iommu_device.h +0 -0
index 18f8b5e5cf..250600fc1d 100644

include/system/host_iommu_device.h typedef struct HostIOMMUDeviceCaps { · line 35

Quoted 4 lines
include/system/host_iommu_device.h +1 -0
@@ -32,6 +32,7 @@ typedef struct HostIOMMUDeviceCaps {
     uint64_t hw_caps;
     bool nesting;
     bool fs1gp;
+    uint32_t errata;

to be consistent with the others yu may have introduced this alongside with the 2 others?
This is also not usable by other IOMMUs.

Eric

Quoted 36 lines
include/system/host_iommu_device.h +1 -0
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -130,6 +131,7 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
 #define HOST_IOMMU_DEVICE_CAP_NESTING           2
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
+#define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
backends/iommufd.c +2 -0
index 0a1a40cbba..3c23caef96 100644
@@ -374,6 +374,8 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
         return caps->nesting;
     case HOST_IOMMU_DEVICE_CAP_FS1GP:
         return caps->fs1gp;
+    case HOST_IOMMU_DEVICE_CAP_ERRATA:
+        return caps->errata;
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +1 -0
index df6a12d200..58bff030e1 100644
@@ -848,6 +848,7 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     case IOMMU_HW_INFO_TYPE_INTEL_VTD:
         caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
         caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        caps->errata = data.vtd.flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17;
         break;
     case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
     case IOMMU_HW_INFO_TYPE_NONE:
#36 lore raw

RE: [PATCH rfcv2 08/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA

Quoted 28 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 08/20] iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA

Quoted 5 lines · Zhenzhong Duan

Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA for IOMMUFD backed host IOMMU device.

Query on this capability is not supported for legacy backend because there is no plan to support nesting with leacy backend

legacy

Review

Quoted 10 lines

backed host device.

---
 include/system/host_iommu_device.h | 2 ++
 backends/iommufd.c                 | 2 ++
 hw/vfio/iommufd.c                  | 1 +
 3 files changed, 5 insertions(+)
(quoted diff) +0 -0

b/include/system/host_iommu_device.h

Quoted 11 lines

include/system/host_iommu_device.h typedef struct HostIOMMUDeviceCaps { · line 35

Quoted 8 lines

index 18f8b5e5cf..250600fc1d 100644

include/system/host_iommu_device.h +1 -0
@@ -32,6 +32,7 @@ typedef struct HostIOMMUDeviceCaps {
     uint64_t hw_caps;
     bool nesting;
     bool fs1gp;
+    uint32_t errata;

to be consistent with the others yu may have introduced this alongside with the 2 others?
This is also not usable by other IOMMUs.

Yes, this is vendor specific element. Will merge after confirm nesting and fs1gp are vendor specific too.

Thanks
Zhenzhong

Quoted 41 lines

Eric

backends/iommufd.c static int hiod_iommufd_get_cap(HostIOMMUDevice

Quoted 16 lines
include/system/host_iommu_device.h +1 -0
 } HostIOMMUDeviceCaps;
 
 #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
@@ -130,6 +131,7 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS           1
 #define HOST_IOMMU_DEVICE_CAP_NESTING           2
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
+#define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
backends/iommufd.c +0 -0
index 0a1a40cbba..3c23caef96 100644
@@ -374,6 +374,8 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice

*hiod, int cap, Error **errp)

hw/vfio/iommufd.c static bool

Quoted 13 lines
backends/iommufd.c +2 -0
         return caps->nesting;
     case HOST_IOMMU_DEVICE_CAP_FS1GP:
         return caps->fs1gp;
+    case HOST_IOMMU_DEVICE_CAP_ERRATA:
+        return caps->errata;
     default:
         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
         return -EINVAL;
hw/vfio/iommufd.c +0 -0
index df6a12d200..58bff030e1 100644
@@ -848,6 +848,7 @@ static bool

hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

hw/vfio/iommufd.c static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,

Quoted 4 lines
hw/vfio/iommufd.c +1 -0
     case IOMMU_HW_INFO_TYPE_INTEL_VTD:
         caps->nesting = !!(data.vtd.ecap_reg & VTD_ECAP_NEST);
         caps->fs1gp = !!(data.vtd.cap_reg & VTD_CAP_FS1GP);
+        caps->errata = data.vtd.flags &

IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17;

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
         break;
     case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
     case IOMMU_HW_INFO_TYPE_NONE:
#37 lore raw

RFC PATCH v2 9/20 intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

In reply to Zhenzhong Duan

In early days vtd_ce_get_rid2pasid_entry() is used to get pasid entry of rid2pasid, then extend to any pasid. So a new name vtd_ce_get_pasid_entry is better to match its functions.

No functional change intended.

---
 hw/i386/intel_iommu.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +7 -7
index 7fde0603bf..df5fb30bc8 100644
@@ -944,7 +944,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
     return 0;
 }
 
-static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
+static int vtd_ce_get_pasid_entry(IntelIOMMUState *s,
                                       VTDContextEntry *ce,
                                       VTDPASIDEntry *pe,
                                       uint32_t pasid)
@@ -1025,7 +1025,7 @@ static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (s->flts) {
             return VTD_PE_GET_FL_LEVEL(&pe);
         } else {
@@ -1048,7 +1048,7 @@ static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
     }
 
@@ -1116,7 +1116,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (s->flts) {
             return pe.val[2] & VTD_SM_PASID_ENTRY_FLPTPTR;
         } else {
@@ -1522,7 +1522,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
      * has valid rid2pasid setting, which includes valid
      * rid2pasid field and corresponding pasid entry setting
      */
-    return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, PCI_NO_PASID);
 }
 
 /* Map a device to its corresponding domain (context-entry) */
@@ -1611,7 +1611,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
     }
 
@@ -1687,7 +1687,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
     int ret;
 
     if (s->root_scalable) {
-        ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        ret = vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (ret) {
             /*
              * This error is guest triggerable. We should assumt PT
Signature · 1 line
2.34.1
Reviewed-by on v2 9/20
#38 lore raw

Re: [PATCH rfcv2 09/20] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

Quoted 84 lines · Zhenzhong Duan

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

In early days vtd_ce_get_rid2pasid_entry() is used to get pasid entry of rid2pasid, then extend to any pasid. So a new name vtd_ce_get_pasid_entry is better to match its functions.

No functional change intended.

---
  hw/i386/intel_iommu.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +7 -8
index 7fde0603bf..df5fb30bc8 100644
@@ -944,7 +944,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      return 0;
  }
 
-static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
+static int vtd_ce_get_pasid_entry(IntelIOMMUState *s,
                                        VTDContextEntry *ce,
                                        VTDPASIDEntry *pe,
                                        uint32_t pasid)
@@ -1025,7 +1025,7 @@ static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
      VTDPASIDEntry pe;
 
      if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
          if (s->flts) {
              return VTD_PE_GET_FL_LEVEL(&pe);
          } else {
@@ -1048,7 +1048,7 @@ static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
      VTDPASIDEntry pe;
 
      if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
          return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
      }
 
@@ -1116,7 +1116,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
      VTDPASIDEntry pe;
 
      if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
          if (s->flts) {
              return pe.val[2] & VTD_SM_PASID_ENTRY_FLPTPTR;
          } else {
@@ -1522,7 +1522,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
       * has valid rid2pasid setting, which includes valid
       * rid2pasid field and corresponding pasid entry setting
       */
-    return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, PCI_NO_PASID);
  }
 
  /* Map a device to its corresponding domain (context-entry) */
@@ -1611,7 +1611,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
      VTDPASIDEntry pe;
 
      if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
          return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
      }
 
@@ -1687,7 +1687,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
      int ret;
 
      if (s->root_scalable) {
-        ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        ret = vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
          if (ret) {
              /*
               * This error is guest triggerable. We should assumt PT
--

2.34.1

#39 lore raw

Re: [PATCH rfcv2 09/20] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 1 line · Zhenzhong Duan

In early days vtd_ce_get_rid2pasid_entry() is used to get pasid entry of

is/was

Quoted 1 line

rid2pasid, then extend to any pasid. So a new name vtd_ce_get_pasid_entry

then it was extended to get any pasid entry?

Quoted 1 line

is better to match its functions.

to match what it actually does?

I do not know the vtd spec very well so I searched for rid2pasid and I did not find any reference. I think I understand what is the pasid entry from the pasid table though so the renaming does make sense to me.

Eric

Quoted 75 lines

No functional change intended.

---
 hw/i386/intel_iommu.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +7 -7
index 7fde0603bf..df5fb30bc8 100644
@@ -944,7 +944,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
     return 0;
 }
 
-static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
+static int vtd_ce_get_pasid_entry(IntelIOMMUState *s,
                                       VTDContextEntry *ce,
                                       VTDPASIDEntry *pe,
                                       uint32_t pasid)
@@ -1025,7 +1025,7 @@ static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (s->flts) {
             return VTD_PE_GET_FL_LEVEL(&pe);
         } else {
@@ -1048,7 +1048,7 @@ static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
     }
 
@@ -1116,7 +1116,7 @@ static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (s->flts) {
             return pe.val[2] & VTD_SM_PASID_ENTRY_FLPTPTR;
         } else {
@@ -1522,7 +1522,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
      * has valid rid2pasid setting, which includes valid
      * rid2pasid field and corresponding pasid entry setting
      */
-    return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
+    return vtd_ce_get_pasid_entry(s, ce, &pe, PCI_NO_PASID);
 }
 
 /* Map a device to its corresponding domain (context-entry) */
@@ -1611,7 +1611,7 @@ static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
     VTDPASIDEntry pe;
 
     if (s->root_scalable) {
-        vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
     }
 
@@ -1687,7 +1687,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
     int ret;
 
     if (s->root_scalable) {
-        ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
+        ret = vtd_ce_get_pasid_entry(s, ce, &pe, pasid);
         if (ret) {
             /*
              * This error is guest triggerable. We should assumt PT
#40 lore raw

RE: [PATCH rfcv2 09/20] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

Hi Eric,

Quoted 11 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 09/20] intel_iommu: Rename vtd_ce_get_rid2pasid_entry to vtd_ce_get_pasid_entry

Hi Zhenzhong,

Quoted 1 line · Zhenzhong Duan

In early days vtd_ce_get_rid2pasid_entry() is used to get pasid entry of

is/was

Will do.

Quoted 2 lines
Quoted 1 line

rid2pasid, then extend to any pasid. So a new name vtd_ce_get_pasid_entry

then it was extended to get any pasid entry?

Will do.

Quoted 2 lines
Quoted 1 line

is better to match its functions.

to match what it actually does?

Yes, will do.

Quoted 4 lines

I do not know the vtd spec very well so I searched for rid2pasid and I did not find any reference. I think I understand what is the pasid entry from the pasid table though so the renaming does make sense to me.

In spec it's named RID_PASID, copied some desc:

"Requests-without-PASID processed through this scalable-mode context entry are treated as Requests-with-PASID with PASID value specified in this field. ExecuteRequested field is treated as 0 for such requests."

Thanks
Zhenzhong

#41 lore raw

RFC PATCH v2 10/20 intel_iommu: Optimize context entry cache utilization

In reply to Zhenzhong Duan

There are many call sites referencing context entry by calling vtd_as_to_context_entry() which will traverse the DMAR table.

In most cases we can use cached context entry in vtd_as->context_cache_entry except it's stale. Currently only global and domain context invalidation stales it.

So introduce a helper function vtd_as_to_context_entry() to fetch from cache before trying with vtd_dev_to_context_entry().

---
 hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
hw/i386/intel_iommu.c +23 -13
index df5fb30bc8..7709f55be5 100644
@@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
     return 0;
 }
 
+static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint8_t bus_num = pci_bus_num(vtd_as->bus);
+    uint8_t devfn = vtd_as->devfn;
+    VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
+
+    /* Try to fetch context-entry from cache first */
+    if (cc_entry->context_cache_gen == s->context_cache_gen) {
+        *ce = cc_entry->context_entry;
+        return 0;
+    } else {
+        return vtd_dev_to_context_entry(s, bus_num, devfn, ce);
+    }
+}
+
 static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event,
                                      void *private)
 {
@@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
         return 0;
     }
 
-    ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
-                                   pci_bus_num(vtd_as->bus),
-                                   vtd_as->devfn, &ce);
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
     if (ret) {
         if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
             /*
@@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
     assert(as);
 
     s = as->iommu_state;
-    if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
-                                 &ce)) {
+    if (vtd_as_to_context_entry(as, &ce)) {
         /*
          * Possibly failed to parse the context entry for some reason
          * (e.g., during init, or any guest configuration errors on
@@ -2443,8 +2456,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
+        if (!vtd_as_to_context_entry(vtd_as, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             vtd_address_space_sync(vtd_as);
         }
@@ -2466,8 +2478,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
     hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
     QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
-        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                       vtd_as->devfn, &ce);
+        ret = vtd_as_to_context_entry(vtd_as, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             uint32_t rid2pasid = PCI_NO_PASID;
 
@@ -2974,8 +2985,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
+        if (!vtd_as_to_context_entry(vtd_as, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
 
@@ -4154,7 +4164,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
     assert(vtd_as->pasid != PCI_NO_PASID);
 
     /* Try out best to fetch FPD, we can't do anything more */
-    if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
+    if (vtd_as_to_context_entry(vtd_as, &ce) == 0) {
         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
         if (!is_fpd_set && s->root_scalable) {
             vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
@@ -4491,7 +4501,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     /* replay is protected by BQL, page walk will re-setup it safely */
     iova_tree_remove(vtd_as->iova_tree, map);
 
-    if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
+    if (vtd_as_to_context_entry(vtd_as, &ce) == 0) {
         trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
                                   "legacy mode",
                                   bus_n, PCI_SLOT(vtd_as->devfn),
Signature · 1 line
2.34.1
#42 lore raw

Re: [PATCH rfcv2 10/20] intel_iommu: Optimize context entry cache utilization

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

There are many call sites referencing context entry by calling vtd_as_to_context_entry() which will traverse the DMAR table.

didn't you mean vtd_dev_to_context_entry? instead

Quoted 4 lines

In most cases we can use cached context entry in vtd_as->context_cache_entry except it's stale. Currently only global and domain context invalidation stales it.

s/states/stale

Eric

Quoted 105 lines

So introduce a helper function vtd_as_to_context_entry() to fetch from cache before trying with vtd_dev_to_context_entry().

---
 hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
hw/i386/intel_iommu.c +23 -13
index df5fb30bc8..7709f55be5 100644
@@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
     return 0;
 }
 
+static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    uint8_t bus_num = pci_bus_num(vtd_as->bus);
+    uint8_t devfn = vtd_as->devfn;
+    VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
+
+    /* Try to fetch context-entry from cache first */
+    if (cc_entry->context_cache_gen == s->context_cache_gen) {
+        *ce = cc_entry->context_entry;
+        return 0;
+    } else {
+        return vtd_dev_to_context_entry(s, bus_num, devfn, ce);
+    }
+}
+
 static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event,
                                      void *private)
 {
@@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
         return 0;
     }
 
-    ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
-                                   pci_bus_num(vtd_as->bus),
-                                   vtd_as->devfn, &ce);
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
     if (ret) {
         if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
             /*
@@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
     assert(as);
 
     s = as->iommu_state;
-    if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
-                                 &ce)) {
+    if (vtd_as_to_context_entry(as, &ce)) {
         /*
          * Possibly failed to parse the context entry for some reason
          * (e.g., during init, or any guest configuration errors on
@@ -2443,8 +2456,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
+        if (!vtd_as_to_context_entry(vtd_as, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             vtd_address_space_sync(vtd_as);
         }
@@ -2466,8 +2478,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
     hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
     QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
-        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                       vtd_as->devfn, &ce);
+        ret = vtd_as_to_context_entry(vtd_as, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             uint32_t rid2pasid = PCI_NO_PASID;
 
@@ -2974,8 +2985,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
     vtd_iommu_unlock(s);
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
-                                      vtd_as->devfn, &ce) &&
+        if (!vtd_as_to_context_entry(vtd_as, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
             uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
 
@@ -4154,7 +4164,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
     assert(vtd_as->pasid != PCI_NO_PASID);
 
     /* Try out best to fetch FPD, we can't do anything more */
-    if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
+    if (vtd_as_to_context_entry(vtd_as, &ce) == 0) {
         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
         if (!is_fpd_set && s->root_scalable) {
             vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
@@ -4491,7 +4501,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     /* replay is protected by BQL, page walk will re-setup it safely */
     iova_tree_remove(vtd_as->iova_tree, map);
 
-    if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
+    if (vtd_as_to_context_entry(vtd_as, &ce) == 0) {
         trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
                                   "legacy mode",
                                   bus_n, PCI_SLOT(vtd_as->devfn),
#43 lore raw

RE: [PATCH rfcv2 10/20] intel_iommu: Optimize context entry cache utilization

Hi Eric,

Quoted 11 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 10/20] intel_iommu: Optimize context entry cache utilization

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

There are many call sites referencing context entry by calling vtd_as_to_context_entry() which will traverse the DMAR table.

didn't you mean vtd_dev_to_context_entry? Instead

Good catch, will fix.

Quoted 5 lines
Quoted 4 lines

In most cases we can use cached context entry in vtd_as->context_cache_entry except it's stale. Currently only global and domain context invalidation stales it.

s/states/stale

Will fix.

Thanks
Zhenzhong

#44 lore raw

RFC PATCH v2 11/20 intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

In reply to Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, stage-1 page table is passed to host to construct nested page table. We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest stage-1 page table could be used by host.

For instance, vIOMMU supports stage-1 1GB huge page mapping, but host does not, then this IOMMUFD backed device should be failed.

Declare an enum type host_iommu_device_iommu_hw_info_type aliased to iommu_hw_info_type which come from iommufd header file. This can avoid build failure on windows which doesn't support iommufd.

---
 include/system/host_iommu_device.h | 13 ++++++++++++
 hw/i386/intel_iommu.c              | 34 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
include/system/host_iommu_device.h +13 -0
index 250600fc1d..aa3885d7ee 100644
@@ -133,5 +133,18 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 #define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
+/**
+ * enum host_iommu_device_iommu_hw_info_type - IOMMU Hardware Info Types
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not
+ *                                             report hardware info
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
+ *
+ * This is alias to enum iommu_hw_info_type but for general purpose.
+ */
+enum host_iommu_device_iommu_hw_info_type {
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE,
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD,
+};
+
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
hw/i386/intel_iommu.c +34 -0
index 7709f55be5..9de60e607d 100644
@@ -39,6 +39,7 @@
 #include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define VTD_CE_GET_RID2PASID(ce) \
@@ -4346,6 +4347,39 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
         return true;
     }
 
+    /* Remaining checks are all stage-1 translation specific */
+    if (!object_dynamic_cast(OBJECT(hiod), TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+        error_setg(errp, "Need IOMMUFD backend when x-flts=on");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (ret != HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "Incompatible host platform IOMMU type %d", ret);
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_NESTING, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (ret != 1) {
+        error_setg(errp, "Host IOMMU doesn't support nested translation");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_FS1GP, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (s->fs1gp && ret != 1) {
+        error_setg(errp, "Stage-1 1GB huge page is unsupported by host IOMMU");
+        return false;
+    }
+
     error_setg(errp, "host device is uncompatible with stage-1 translation");
     return false;
 }
Signature · 1 line
2.34.1
#45 lore raw

Re: [PATCH rfcv2 11/20] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Hi Zhenzhong,

Quoted 7 lines · Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, stage-1 page table is passed to host to construct nested page table. We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest stage-1 page table could be used by host.

For instance, vIOMMU supports stage-1 1GB huge page mapping, but host does not, then this IOMMUFD backed device should be failed.

is this 1GB huge page mapping a requiring for SIOV?

Quoted 3 lines

Declare an enum type host_iommu_device_iommu_hw_info_type aliased to iommu_hw_info_type which come from iommufd header file. This can avoid

s/come/comes

Quoted 47 lines

build failure on windows which doesn't support iommufd.

---
 include/system/host_iommu_device.h | 13 ++++++++++++
 hw/i386/intel_iommu.c              | 34 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
include/system/host_iommu_device.h +13 -0
index 250600fc1d..aa3885d7ee 100644
@@ -133,5 +133,18 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 #define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
+/**
+ * enum host_iommu_device_iommu_hw_info_type - IOMMU Hardware Info Types
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not
+ *                                             report hardware info
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
+ *
+ * This is alias to enum iommu_hw_info_type but for general purpose.
+ */
+enum host_iommu_device_iommu_hw_info_type {
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE,
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD,
+};
+
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
hw/i386/intel_iommu.c +1 -0
index 7709f55be5..9de60e607d 100644
@@ -39,6 +39,7 @@
 #include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define VTD_CE_GET_RID2PASID(ce) \
@@ -4346,6 +4347,39 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
         return true;
     }

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

Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
 
+    /* Remaining checks are all stage-1 translation specific */
+    if (!object_dynamic_cast(OBJECT(hiod), TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+        error_setg(errp, "Need IOMMUFD backend when x-flts=on");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE, errp);
+    if (ret < 0) {
+        return false;

Can't you simply rely on the check below?

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    }
+    if (ret != HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "Incompatible host platform IOMMU type %d", ret);
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_NESTING, errp);
+    if (ret < 0) {
+        return false;
+    }

same heere

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    if (ret != 1) {
+        error_setg(errp, "Host IOMMU doesn't support nested translation");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_FS1GP, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (s->fs1gp && ret != 1) {

looking in the vtd spec I don't find FS1GP. Is it the same as FL1GP? Maybe I am not looking the correct spec though. Why do you need to check both ret and fs1gp
Even why do you need a member to store the cap? Looks FL1GP can only take 0 or 1 value?

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

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
+        error_setg(errp, "Stage-1 1GB huge page is unsupported by host IOMMU");
+        return false;
+    }
+
     error_setg(errp, "host device is uncompatible with stage-1 translation");
     return false;
 }

Eric

#46 lore raw

Re: [PATCH rfcv2 11/20] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Quoted 95 lines · Eric Auger

Hi Zhenzhong,

Quoted 7 lines · Zhenzhong Duan

When vIOMMU is configured x-flts=on in scalable mode, stage-1 page table is passed to host to construct nested page table. We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest stage-1 page table could be used by host.

For instance, vIOMMU supports stage-1 1GB huge page mapping, but host does not, then this IOMMUFD backed device should be failed.

is this 1GB huge page mapping a requiring for SIOV?

Quoted 3 lines

Declare an enum type host_iommu_device_iommu_hw_info_type aliased to iommu_hw_info_type which come from iommufd header file. This can avoid

s/come/comes

Quoted 47 lines

build failure on windows which doesn't support iommufd.

---
 include/system/host_iommu_device.h | 13 ++++++++++++
 hw/i386/intel_iommu.c              | 34 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
include/system/host_iommu_device.h +13 -0
index 250600fc1d..aa3885d7ee 100644
@@ -133,5 +133,18 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 #define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
+/**
+ * enum host_iommu_device_iommu_hw_info_type - IOMMU Hardware Info Types
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not
+ *                                             report hardware info
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
+ *
+ * This is alias to enum iommu_hw_info_type but for general purpose.
+ */
+enum host_iommu_device_iommu_hw_info_type {
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE,
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD,
+};
+
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
hw/i386/intel_iommu.c +1 -0
index 7709f55be5..9de60e607d 100644
@@ -39,6 +39,7 @@
 #include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define VTD_CE_GET_RID2PASID(ce) \
@@ -4346,6 +4347,39 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
         return true;
     }

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

Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
 
+    /* Remaining checks are all stage-1 translation specific */
+    if (!object_dynamic_cast(OBJECT(hiod), TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+        error_setg(errp, "Need IOMMUFD backend when x-flts=on");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE, errp);
+    if (ret < 0) {
+        return false;

Can't you simply rely on the check below?

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    }
+    if (ret != HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "Incompatible host platform IOMMU type %d", ret);
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_NESTING, errp);
+    if (ret < 0) {
+        return false;
+    }

same heere

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    if (ret != 1) {
+        error_setg(errp, "Host IOMMU doesn't support nested translation");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_FS1GP, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (s->fs1gp && ret != 1) {
Quoted 1 line

looking in the vtd spec I don't find FS1GP. Is it the same as FL1GP?

I am now looking at spec rev from june 22 and it seems it has been renamed. So please ignore this comment

Eric

Quoted 12 lines

Maybe I am not looking the correct spec though. Why do you need to check both ret and fs1gp
Even why do you need a member to store the cap? Looks FL1GP can only take 0 or 1 value?

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

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
+        error_setg(errp, "Stage-1 1GB huge page is unsupported by host IOMMU");
+        return false;
+    }
+
     error_setg(errp, "host device is uncompatible with stage-1 translation");
     return false;
 }

Eric

#47 lore raw

RE: [PATCH rfcv2 11/20] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

In reply to Eric Auger
Quoted 11 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 11/20] intel_iommu: Check for compatibility with IOMMUFD backed device when x-flts=on

Hi Zhenzhong,

On 2/19/25 9:22 AM, Zhenzhong Duan wrote:

Quoted 8 lines
Quoted 7 lines

When vIOMMU is configured x-flts=on in scalable mode, stage-1 page table is passed to host to construct nested page table. We need to check compatibility of some critical IOMMU capabilities between vIOMMU and host IOMMU to ensure guest stage-1 page table could be used by host.

For instance, vIOMMU supports stage-1 1GB huge page mapping, but host does not, then this IOMMUFD backed device should be failed.

is this 1GB huge page mapping a requiring for SIOV?

No, but if guest has configured that support, but host doesn't support it, VFIO device should fail the plug.

Quoted 4 lines
Quoted 3 lines

Declare an enum type host_iommu_device_iommu_hw_info_type aliased to iommu_hw_info_type which come from iommufd header file. This can avoid

s/come/comes

Will do.

Quoted 61 lines

Review

Quoted 10 lines

build failure on windows which doesn't support iommufd.

---
 include/system/host_iommu_device.h | 13 ++++++++++++
 hw/i386/intel_iommu.c              | 34 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
(quoted diff) +0 -0

b/include/system/host_iommu_device.h

include/system/host_iommu_device.h struct HostIOMMUDeviceClass { · line 137

Quoted 9 lines

index 250600fc1d..aa3885d7ee 100644

include/system/host_iommu_device.h +2 -0
@@ -133,5 +133,18 @@ struct HostIOMMUDeviceClass {
 #define HOST_IOMMU_DEVICE_CAP_FS1GP             3
 #define HOST_IOMMU_DEVICE_CAP_ERRATA            4
 
+/**
+ * enum host_iommu_device_iommu_hw_info_type - IOMMU Hardware Info

Types

include/system/host_iommu_device.h struct HostIOMMUDeviceClass { · line 138

Quoted 1 line
include/system/host_iommu_device.h +1 -0
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE: Used by the

drivers that do not

include/system/host_iommu_device.h struct HostIOMMUDeviceClass { · line 140

Quoted 2 lines
include/system/host_iommu_device.h +2 -0
+ *                                             report hardware info
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d

iommu info type

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

Quoted 23 lines
include/system/host_iommu_device.h +8 -0
+ *
+ * This is alias to enum iommu_hw_info_type but for general purpose.
+ */
+enum host_iommu_device_iommu_hw_info_type {
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE,
+    HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD,
+};
+
 #define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX       64
 #endif
hw/i386/intel_iommu.c +1 -0
index 7709f55be5..9de60e607d 100644
@@ -39,6 +39,7 @@
 #include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "trace.h"
+#include "system/iommufd.h"
 
 /* context entry operations */
 #define VTD_CE_GET_RID2PASID(ce) \
@@ -4346,6 +4347,39 @@ static bool vtd_check_hiod(IntelIOMMUState *s,

HostIOMMUDevice *hiod,

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

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
         return true;
     }
 
+    /* Remaining checks are all stage-1 translation specific */
+    if (!object_dynamic_cast(OBJECT(hiod),

TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {

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

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+        error_setg(errp, "Need IOMMUFD backend when x-flts=on");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE,
Quoted 4 lines

errp);

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

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+    if (ret < 0) {
+        return false;

Can't you simply rely on the check below?

I think not, below code will overwrite errp.

Quoted 21 lines

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    }
+    if (ret != HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "Incompatible host platform IOMMU type %d", ret);
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_NESTING, errp);
+    if (ret < 0) {
+        return false;
+    }

same heere

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    if (ret != 1) {
+        error_setg(errp, "Host IOMMU doesn't support nested translation");
+        return false;
+    }
+
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_FS1GP, errp);
+    if (ret < 0) {
+        return false;
+    }
+    if (s->fs1gp && ret != 1) {
Quoted 1 line

looking in the vtd spec I don't find FS1GP. Is it the same as FL1GP?

Yes.

Quoted 2 lines

Maybe I am not looking the correct spec though. Why do you need to check both ret and fs1gp

Ret < 0 means error happen, e.g., vIOMMU checks an unrecognized cap. 0 or 1 means no error and unsupported vs. supported for FS1GP.

Quoted 2 lines

Even why do you need a member to store the cap? Looks FL1GP can only take 0 or 1 value?

You means s->fs1gp? That's user configuration for vIOMMU.
We need to check user's config of FS1GP with host's FS1GP to ensure compatibility.

Yes, Fs1GP takes only 0 or 1, aw_bits can have other values.

Thanks
Zhenzhong

Quoted 8 lines

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

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
+        error_setg(errp, "Stage-1 1GB huge page is unsupported by host IOMMU");
+        return false;
+    }
+
     error_setg(errp, "host device is uncompatible with stage-1 translation");
     return false;
 }

Eric

#48 lore raw

RFC PATCH v2 12/20 intel_iommu: Introduce a new structure VTDHostIOMMUDevice

In reply to Zhenzhong Duan

Introduce a new structure VTDHostIOMMUDevice which replaces HostIOMMUDevice to be stored in hash table.

It includes a reference to HostIOMMUDevice and IntelIOMMUState, also includes BDF information which will be used in future patches.

---
 hw/i386/intel_iommu_internal.h |  7 +++++++
 include/hw/i386/intel_iommu.h  |  2 +-
 hw/i386/intel_iommu.c          | 14 ++++++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +7 -0
index 2cda744786..18bc22fc72 100644
@@ -28,6 +28,7 @@
 #ifndef HW_I386_INTEL_IOMMU_INTERNAL_H
 #define HW_I386_INTEL_IOMMU_INTERNAL_H
 #include "hw/i386/intel_iommu.h"
+#include "system/host_iommu_device.h"
 
 /*
  * Intel IOMMU register specification
@@ -608,4 +609,10 @@ typedef struct VTDRootEntry VTDRootEntry;
 /* Bits to decide the offset for each level */
 #define VTD_LEVEL_BITS           9
 
+typedef struct VTDHostIOMMUDevice {
+    IntelIOMMUState *iommu_state;
+    PCIBus *bus;
+    uint8_t devfn;
+    HostIOMMUDevice *hiod;
+} VTDHostIOMMUDevice;
 #endif
include/hw/i386/intel_iommu.h +1 -1
index e95477e855..50f9b27a45 100644
@@ -295,7 +295,7 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
-    GHashTable *vtd_host_iommu_dev;             /* HostIOMMUDevice */
+    GHashTable *vtd_host_iommu_dev;             /* VTDHostIOMMUDevice */
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
hw/i386/intel_iommu.c +12 -2
index 9de60e607d..fafa199f52 100644
@@ -281,7 +281,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1, gconstpointer v2)
 
 static void vtd_hiod_destroy(gpointer v)
 {
-    object_unref(v);
+    VTDHostIOMMUDevice *vtd_hiod = v;
+
+    object_unref(vtd_hiod->hiod);
+    g_free(vtd_hiod);
 }
 
 static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
@@ -4388,6 +4391,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
                                      HostIOMMUDevice *hiod, Error **errp)
 {
     IntelIOMMUState *s = opaque;
+    VTDHostIOMMUDevice *vtd_hiod;
     struct vtd_as_key key = {
         .bus = bus,
         .devfn = devfn,
@@ -4404,6 +4408,12 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
         return false;
     }
 
+    vtd_hiod = g_malloc0(sizeof(VTDHostIOMMUDevice));
+    vtd_hiod->bus = bus;
+    vtd_hiod->devfn = (uint8_t)devfn;
+    vtd_hiod->iommu_state = s;
+    vtd_hiod->hiod = hiod;
+
     if (!vtd_check_hiod(s, hiod, errp)) {
         vtd_iommu_unlock(s);
         return false;
@@ -4414,7 +4424,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     new_key->devfn = devfn;
 
     object_ref(hiod);
-    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, hiod);
+    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, vtd_hiod);
 
     vtd_iommu_unlock(s);
 
Signature · 1 line
2.34.1
#49 lore raw

Re: [PATCH rfcv2 12/20] intel_iommu: Introduce a new structure VTDHostIOMMUDevice

Quoted 27 lines · Zhenzhong Duan

Introduce a new structure VTDHostIOMMUDevice which replaces HostIOMMUDevice to be stored in hash table.

It includes a reference to HostIOMMUDevice and IntelIOMMUState, also includes BDF information which will be used in future patches.

---
 hw/i386/intel_iommu_internal.h |  7 +++++++
 include/hw/i386/intel_iommu.h  |  2 +-
 hw/i386/intel_iommu.c          | 14 ++++++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 2cda744786..18bc22fc72 100644
@@ -28,6 +28,7 @@
 #ifndef HW_I386_INTEL_IOMMU_INTERNAL_H
 #define HW_I386_INTEL_IOMMU_INTERNAL_H
 #include "hw/i386/intel_iommu.h"
+#include "system/host_iommu_device.h"
 
 /*
  * Intel IOMMU register specification

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

Quoted 7 lines
hw/i386/intel_iommu_internal.h +4 -0
@@ -608,4 +609,10 @@ typedef struct VTDRootEntry VTDRootEntry;
 /* Bits to decide the offset for each level */
 #define VTD_LEVEL_BITS           9
 
+typedef struct VTDHostIOMMUDevice {
+    IntelIOMMUState *iommu_state;
+    PCIBus *bus;
+    uint8_t devfn;

Just to make sure the parent

HostIOMMUDevice has aliased_bus and aliased_devfn. Can you explain why do you need both aliased and non aliased info?

Thanks

Eric

Quoted 62 lines
hw/i386/intel_iommu_internal.h +2 -0
+    HostIOMMUDevice *hiod;
+} VTDHostIOMMUDevice;
 #endif
include/hw/i386/intel_iommu.h +1 -1
index e95477e855..50f9b27a45 100644
@@ -295,7 +295,7 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
-    GHashTable *vtd_host_iommu_dev;             /* HostIOMMUDevice */
+    GHashTable *vtd_host_iommu_dev;             /* VTDHostIOMMUDevice */
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
hw/i386/intel_iommu.c +12 -2
index 9de60e607d..fafa199f52 100644
@@ -281,7 +281,10 @@ static gboolean vtd_hiod_equal(gconstpointer v1, gconstpointer v2)
 
 static void vtd_hiod_destroy(gpointer v)
 {
-    object_unref(v);
+    VTDHostIOMMUDevice *vtd_hiod = v;
+
+    object_unref(vtd_hiod->hiod);
+    g_free(vtd_hiod);
 }
 
 static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
@@ -4388,6 +4391,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
                                      HostIOMMUDevice *hiod, Error **errp)
 {
     IntelIOMMUState *s = opaque;
+    VTDHostIOMMUDevice *vtd_hiod;
     struct vtd_as_key key = {
         .bus = bus,
         .devfn = devfn,
@@ -4404,6 +4408,12 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
         return false;
     }
 
+    vtd_hiod = g_malloc0(sizeof(VTDHostIOMMUDevice));
+    vtd_hiod->bus = bus;
+    vtd_hiod->devfn = (uint8_t)devfn;
+    vtd_hiod->iommu_state = s;
+    vtd_hiod->hiod = hiod;
+
     if (!vtd_check_hiod(s, hiod, errp)) {
         vtd_iommu_unlock(s);
         return false;
@@ -4414,7 +4424,7 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     new_key->devfn = devfn;
 
     object_ref(hiod);
-    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, hiod);
+    g_hash_table_insert(s->vtd_host_iommu_dev, new_key, vtd_hiod);
 
     vtd_iommu_unlock(s);
 
#50 lore raw

RE: [PATCH rfcv2 12/20] intel_iommu: Introduce a new structure VTDHostIOMMUDevice

Quoted 41 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 12/20] intel_iommu: Introduce a new structure VTDHostIOMMUDevice

Quoted 30 lines · Zhenzhong Duan

Introduce a new structure VTDHostIOMMUDevice which replaces HostIOMMUDevice to be stored in hash table.

It includes a reference to HostIOMMUDevice and IntelIOMMUState, also includes BDF information which will be used in future patches.

---
 hw/i386/intel_iommu_internal.h |  7 +++++++
 include/hw/i386/intel_iommu.h  |  2 +-
 hw/i386/intel_iommu.c          | 14 ++++++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 2cda744786..18bc22fc72 100644
@@ -28,6 +28,7 @@
 #ifndef HW_I386_INTEL_IOMMU_INTERNAL_H
 #define HW_I386_INTEL_IOMMU_INTERNAL_H
 #include "hw/i386/intel_iommu.h"
+#include "system/host_iommu_device.h"
 
 /*
  * Intel IOMMU register specification
@@ -608,4 +609,10 @@ typedef struct VTDRootEntry VTDRootEntry;
 /* Bits to decide the offset for each level */
 #define VTD_LEVEL_BITS           9
 
Quoted 8 lines

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

Quoted 4 lines
hw/i386/intel_iommu_internal.h +4 -0
+typedef struct VTDHostIOMMUDevice {
+    IntelIOMMUState *iommu_state;
+    PCIBus *bus;
+    uint8_t devfn;

Just to make sure the parent

HostIOMMUDevice has aliased_bus and aliased_devfn. Can you explain why do you need both aliased and non aliased info?

Virtual vtd only need non aliased bdf, it uses non aliased bdf to index HostIOMMUDevice to do attachment/detachment.

I remember virtio-iommu need aliased bdf to rebuild reserved regions for aliased IOMMUDevice.

Thanks
Zhenzhong

#51 lore raw

RFC PATCH v2 13/20 intel_iommu: Add PASID cache management infrastructure

In reply to Zhenzhong Duan

This adds an new entry VTDPASIDCacheEntry in VTDAddressSpace to cache the pasid entry and track PASID usage and future PASID tagged DMA address translation support in vIOMMU.

VTDAddressSpace of PCI_NO_PASID is allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID tagged DMA translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.

When guest modifies a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, allocate or remove a VTDAddressSpace instance per the invalidation reasons:

*) a present pasid entry moved to non-present *) a present pasid entry to be a present entry *) a non-present pasid entry moved to present

vIOMMU emulator could figure out the reason by fetching latest guest pasid entry and compare it with the PASID cache.

---
 hw/i386/intel_iommu_internal.h |  29 ++
 include/hw/i386/intel_iommu.h  |   6 +
 hw/i386/intel_iommu.c          | 484 ++++++++++++++++++++++++++++++++-
 hw/i386/trace-events           |   4 +
 4 files changed, 513 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +29 -0
index 18bc22fc72..632fda2853 100644
@@ -315,6 +315,7 @@ typedef enum VTDFaultReason {
                                   * request while disabled */
     VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
+    VTD_FR_RTADDR_INV_TTM = 0x31,  /* Invalid TTM in RTADDR */
     /* PASID directory entry access failure */
     VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
     /* The Present(P) field of pasid directory entry is 0 */
@@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000f1c0ULL
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
 
+#define VTD_INV_DESC_PASIDC_G          (3ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID(val) (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PASIDC_DID(val)   (((val) >> 16) & VTD_DOMAIN_ID_MASK)
+#define VTD_INV_DESC_PASIDC_RSVD_VAL0  0xfff000000000f1c0ULL
+
+#define VTD_INV_DESC_PASIDC_DSI        (0ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID_SI   (1ULL << 4)
+#define VTD_INV_DESC_PASIDC_GLOBAL     (3ULL << 4)
+
 /* Information about page-selective IOTLB invalidate */
 struct VTDIOTLBPageInvInfo {
     uint16_t domain_id;
@@ -548,10 +558,28 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(val)      (((val) >> 9) & 0x7)
 #define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 
+typedef enum VTDPCInvType {
+    /* force reset all */
+    VTD_PASID_CACHE_FORCE_RESET = 0,
+    /* pasid cache invalidation rely on guest PASID entry */
+    VTD_PASID_CACHE_GLOBAL_INV,
+    VTD_PASID_CACHE_DOMSI,
+    VTD_PASID_CACHE_PASIDSI,
+} VTDPCInvType;
+
+typedef struct VTDPASIDCacheInfo {
+    VTDPCInvType type;
+    uint16_t domain_id;
+    uint32_t pasid;
+    PCIBus *bus;
+    uint16_t devfn;
+} VTDPASIDCacheInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
@@ -563,6 +591,7 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
 #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) & VTD_PASID_TABLE_BITS_MASK)
 #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TBL_ENTRY_NUM       (1ULL << 6)
 
 /* PASID Granular Translation Type Mask */
 #define VTD_PASID_ENTRY_P              1ULL
include/hw/i386/intel_iommu.h +6 -0
index 50f9b27a45..fbc9da903a 100644
@@ -95,6 +95,11 @@ struct VTDPASIDEntry {
     uint64_t val[8];
 };
 
+typedef struct VTDPASIDCacheEntry {
+    struct VTDPASIDEntry pasid_entry;
+    bool cache_filled;
+} VTDPASIDCacheEntry;
+
 struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
@@ -107,6 +112,7 @@ struct VTDAddressSpace {
     MemoryRegion iommu_ir_fault; /* Interrupt region for catching fault */
     IntelIOMMUState *iommu_state;
     VTDContextCacheEntry context_cache_entry;
+    VTDPASIDCacheEntry pasid_cache_entry;
     QLIST_ENTRY(VTDAddressSpace) next;
     /* Superset of notifier flags that this address space has */
     IOMMUNotifierFlag notifier_flags;
hw/i386/intel_iommu.c +474 -10

526 lines not on this page · raw message

hw/i386/trace-events +4 -0
index 53c02d7ac8..a26b38b52c 100644
@@ -24,6 +24,10 @@ vtd_inv_qi_head(uint16_t head) "read head %d"
 vtd_inv_qi_tail(uint16_t head) "write tail %d"
 vtd_inv_qi_fetch(void) ""
 vtd_context_cache_reset(void) ""
+vtd_pasid_cache_gsi(void) ""
+vtd_pasid_cache_reset(void) ""
+vtd_pasid_cache_dsi(uint16_t domain) "Domain slective PC invalidation domain 0x%"PRIx16
+vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present"
 vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present"
 vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
Signature · 1 line
2.34.1
#52 lore raw

Re: [PATCH rfcv2 13/20] intel_iommu: Add PASID cache management infrastructure

Quoted 23 lines · Zhenzhong Duan

This adds an new entry VTDPASIDCacheEntry in VTDAddressSpace to cache the pasid entry and track PASID usage and future PASID tagged DMA address translation support in vIOMMU.

VTDAddressSpace of PCI_NO_PASID is allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID tagged DMA translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.

When guest modifies a PASID entry, QEMU will capture the guest pasid selective pasid cache invalidation, allocate or remove a VTDAddressSpace instance per the invalidation reasons:

*) a present pasid entry moved to non-present *) a present pasid entry to be a present entry *) a non-present pasid entry moved to present

vIOMMU emulator could figure out the reason by fetching latest guest pasid entry and compare it with the PASID cache.

Quoted 7 lines
---
 hw/i386/intel_iommu_internal.h |  29 ++
 include/hw/i386/intel_iommu.h  |   6 +
 hw/i386/intel_iommu.c          | 484 ++++++++++++++++++++++++++++++++-

Don't you have ways to split this patch. It has a huge change set and this is really heavy to digest at once (at least for me).

Quoted 97 lines
 hw/i386/trace-events           |   4 +
 4 files changed, 513 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +29 -0
index 18bc22fc72..632fda2853 100644
@@ -315,6 +315,7 @@ typedef enum VTDFaultReason {
                                   * request while disabled */
     VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
+    VTD_FR_RTADDR_INV_TTM = 0x31,  /* Invalid TTM in RTADDR */
     /* PASID directory entry access failure */
     VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
     /* The Present(P) field of pasid directory entry is 0 */
@@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000f1c0ULL
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
 
+#define VTD_INV_DESC_PASIDC_G          (3ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID(val) (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PASIDC_DID(val)   (((val) >> 16) & VTD_DOMAIN_ID_MASK)
+#define VTD_INV_DESC_PASIDC_RSVD_VAL0  0xfff000000000f1c0ULL
+
+#define VTD_INV_DESC_PASIDC_DSI        (0ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID_SI   (1ULL << 4)
+#define VTD_INV_DESC_PASIDC_GLOBAL     (3ULL << 4)
+
 /* Information about page-selective IOTLB invalidate */
 struct VTDIOTLBPageInvInfo {
     uint16_t domain_id;
@@ -548,10 +558,28 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(val)      (((val) >> 9) & 0x7)
 #define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 
+typedef enum VTDPCInvType {
+    /* force reset all */
+    VTD_PASID_CACHE_FORCE_RESET = 0,
+    /* pasid cache invalidation rely on guest PASID entry */
+    VTD_PASID_CACHE_GLOBAL_INV,
+    VTD_PASID_CACHE_DOMSI,
+    VTD_PASID_CACHE_PASIDSI,
+} VTDPCInvType;
+
+typedef struct VTDPASIDCacheInfo {
+    VTDPCInvType type;
+    uint16_t domain_id;
+    uint32_t pasid;
+    PCIBus *bus;
+    uint16_t devfn;
+} VTDPASIDCacheInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
@@ -563,6 +591,7 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
 #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) & VTD_PASID_TABLE_BITS_MASK)
 #define VTD_PASID_ENTRY_FPD           (1ULL << 1) /* Fault Processing Disable */
+#define VTD_PASID_TBL_ENTRY_NUM       (1ULL << 6)
 
 /* PASID Granular Translation Type Mask */
 #define VTD_PASID_ENTRY_P              1ULL
include/hw/i386/intel_iommu.h +6 -0
index 50f9b27a45..fbc9da903a 100644
@@ -95,6 +95,11 @@ struct VTDPASIDEntry {
     uint64_t val[8];
 };
 
+typedef struct VTDPASIDCacheEntry {
+    struct VTDPASIDEntry pasid_entry;
+    bool cache_filled;
+} VTDPASIDCacheEntry;
+
 struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
@@ -107,6 +112,7 @@ struct VTDAddressSpace {
     MemoryRegion iommu_ir_fault; /* Interrupt region for catching fault */
     IntelIOMMUState *iommu_state;
     VTDContextCacheEntry context_cache_entry;
+    VTDPASIDCacheEntry pasid_cache_entry;
     QLIST_ENTRY(VTDAddressSpace) next;
     /* Superset of notifier flags that this address space has */
     IOMMUNotifierFlag notifier_flags;
hw/i386/intel_iommu.c +0 -0
index fafa199f52..b8f3b85803 100644

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

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -86,6 +86,8 @@ struct vtd_iotlb_key {
 static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
 
+static void vtd_pasid_cache_reset(IntelIOMMUState *s);

use _locked suffix to be consistent with the others and emphases the lock is held?

Quoted 13 lines
hw/i386/intel_iommu.c +2 -0
+
 static void vtd_panic_require_caching_mode(void)
 {
     error_report("We need to set caching-mode=on for intel-iommu to enable "
@@ -390,6 +392,7 @@ static void vtd_reset_caches(IntelIOMMUState *s)
     vtd_iommu_lock(s);
     vtd_reset_iotlb_locked(s);
     vtd_reset_context_cache_locked(s);
+    vtd_pasid_cache_reset(s);
     vtd_iommu_unlock(s);
 }
 

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

Quoted 9 lines
hw/i386/intel_iommu.c +6 -0
@@ -825,6 +828,16 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
     }
 }
 
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce->val[0]) + 7);
+}
+
+static inline uint16_t vtd_pe_get_domain_id(VTDPASIDEntry *pe)

nit: vtd_pe_get_did as the filed is named DID?

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1633

Quoted 12 lines
hw/i386/intel_iommu.c +5 -0
+{
+    return VTD_SM_PASID_ENTRY_DID((pe)->val[1]);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1617,6 +1630,54 @@ static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)
     }
 }
 
+/* Translate to iommu pasid if PCI_NO_PASID */

I don't really get the comment above. Ay best, shouldn't it be put in

(vtd_as->pasid == PCI_NO_PASID) block?
What you call "iommu pasid" is the value set in RID_PASID, right? Is "iommu pasid" a conventional terminology?

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1645

Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+static int vtd_as_to_iommu_pasid(VTDAddressSpace *vtd_as, uint32_t *pasid)
+{
+    VTDContextEntry ce;
+    int ret;
+
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    if (vtd_as->pasid == PCI_NO_PASID) {
+        *pasid = VTD_CE_GET_RID2PASID(&ce);

This is called RID_PASID in the spec. I think it would be easier for the reader if could have a direct match with named fields so that we can easily seach the spec.

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1654

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+    } else {
+        *pasid = vtd_as->pasid;
+    }
+
+    return 0;
+}
+
+static gboolean vtd_find_as_by_sid_and_iommu_pasid(gpointer key, gpointer value,
+                                                   gpointer user_data)

why iommu_pasid and not directly pasid?

Quoted 30 lines
hw/i386/intel_iommu.c +26 -0
+{
+    VTDAddressSpace *vtd_as = (VTDAddressSpace *)value;
+    struct vtd_as_raw_key *target = (struct vtd_as_raw_key *)user_data;
+    uint16_t sid = PCI_BUILD_BDF(pci_bus_num(vtd_as->bus), vtd_as->devfn);
+    uint32_t pasid;
+
+    if (vtd_as_to_iommu_pasid(vtd_as, &pasid)) {
+        return false;
+    }
+
+    return (pasid == target->pasid) && (sid == target->sid);
+}
+
+/* Translate iommu pasid to vtd_as */
+static VTDAddressSpace *vtd_as_from_iommu_pasid(IntelIOMMUState *s,
+                                                uint16_t sid, uint32_t pasid)
+{
+    struct vtd_as_raw_key key = {
+        .sid = sid,
+        .pasid = pasid
+    };
+
+    return g_hash_table_find(s->vtd_address_spaces,
+                             vtd_find_as_by_sid_and_iommu_pasid, &key);
+}
+
 static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event,
                                      void *private)
 {

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

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
@@ -3062,6 +3123,412 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
     return true;
 }
 
+static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
+                                            uint32_t pasid, VTDPASIDEntry *pe)

does "pe" means pasid entry? It is not obvious for a dummy reader like me. May be worth a comment at least once.

Quoted 18 lines
hw/i386/intel_iommu.c +18 -0
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+
+static bool vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+{
+    return !memcmp(p1, p2, sizeof(*p1));
+}
+
+/*
+ * This function fills in the pasid entry in &vtd_as. Caller
+ * of this function should hold iommu_lock.
+ */
+static void vtd_fill_pe_in_cache(IntelIOMMUState *s, VTDAddressSpace *vtd_as,
+                                 VTDPASIDEntry *pe)

seems some other functions used the _locked suffix

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

Quoted 13 lines
hw/i386/intel_iommu.c +13 -0
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+
+    if (vtd_pasid_entry_compare(pe, &pc_entry->pasid_entry)) {
+        /* No need to go further as cached pasid entry is latest */
+        return;
+    }
+
+    pc_entry->pasid_entry = *pe;
+    pc_entry->cache_filled = true;
+    /*
+     * TODO: send pasid bind to host for passthru devices
+     */

what does it mean?

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+}
+
+/*
+ * This function is used to clear cached pasid entry in vtd_as
+ * instances. Caller of this function should hold iommu_lock.
+ */
+static gboolean vtd_flush_pasid(gpointer key, gpointer value,
+                                gpointer user_data)
+{

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    VTDPASIDCacheInfo *pc_info = user_data;
+    VTDAddressSpace *vtd_as = value;
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDPASIDEntry pe;
+    uint16_t did;
+    uint32_t pasid;
+    int ret;
+
+    /* Replay only fill pasid entry cache for passthrough device */

filled

Eric

Quoted 393 lines
hw/i386/intel_iommu.c +351 -10
+    if (!pc_entry->cache_filled) {
+        return false;
+    }
+    did = vtd_pe_get_domain_id(&pc_entry->pasid_entry);
+
+    if (vtd_as_to_iommu_pasid(vtd_as, &pasid)) {
+        goto remove;
+    }
+
+    switch (pc_info->type) {
+    case VTD_PASID_CACHE_FORCE_RESET:
+        goto remove;
+    case VTD_PASID_CACHE_PASIDSI:
+        if (pc_info->pasid != pasid) {
+            return false;
+        }
+        /* Fall through */
+    case VTD_PASID_CACHE_DOMSI:
+        if (pc_info->domain_id != did) {
+            return false;
+        }
+        /* Fall through */
+    case VTD_PASID_CACHE_GLOBAL_INV:
+        break;
+    default:
+        error_report("invalid pc_info->type");
+        abort();
+    }
+
+    /*
+     * pasid cache invalidation may indicate a present pasid
+     * entry to present pasid entry modification. To cover such
+     * case, vIOMMU emulator needs to fetch latest guest pasid
+     * entry and check cached pasid entry, then update pasid
+     * cache and send pasid bind/unbind to host properly.
+     */
+    ret = vtd_dev_get_pe_from_pasid(vtd_as, pasid, &pe);
+    if (ret) {
+        /*
+         * No valid pasid entry in guest memory. e.g. pasid entry
+         * was modified to be either all-zero or non-present. Either
+         * case means existing pasid cache should be removed.
+         */
+        goto remove;
+    }
+
+    vtd_fill_pe_in_cache(s, vtd_as, &pe);
+    return false;
+
+remove:
+    /*
+     * TODO: send pasid unbind to host for passthru devices
+     */
+    pc_entry->cache_filled = false;
+
+    /*
+     * Don't remove address space of PCI_NO_PASID which is created by PCI
+     * sub-system.
+     */
+    if (vtd_as->pasid == PCI_NO_PASID) {
+        return false;
+    }
+    return true;
+}
+
+/* Caller of this function should hold iommu_lock */
+static void vtd_pasid_cache_reset(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info;
+
+    trace_vtd_pasid_cache_reset();
+
+    pc_info.type = VTD_PASID_CACHE_FORCE_RESET;
+
+    /*
+     * Reset pasid cache is a big hammer, so use
+     * g_hash_table_foreach_remove which will free
+     * the vtd_as instances. Also, as a big
+     * hammer, use VTD_PASID_CACHE_FORCE_RESET to
+     * ensure all the vtd_as instances are
+     * dropped, meanwhile the change will be passed
+     * to host if HostIOMMUDeviceIOMMUFD is available.
+     */
+    g_hash_table_foreach_remove(s->vtd_address_spaces,
+                                vtd_flush_pasid, &pc_info);
+}
+
+/* Caller of this function should hold iommu_lock. */
+static void vtd_sm_pasid_table_walk_one(IntelIOMMUState *s,
+                                        dma_addr_t pt_base,
+                                        int start,
+                                        int end,
+                                        VTDPASIDCacheInfo *info)
+{
+    VTDPASIDEntry pe;
+    int pasid = start;
+    int pasid_next;
+
+    while (pasid < end) {
+        pasid_next = pasid + 1;
+
+        if (!vtd_get_pe_in_pasid_leaf_table(s, pasid, pt_base, &pe)
+            && vtd_pe_present(&pe)) {
+            int bus_n = pci_bus_num(info->bus), devfn = info->devfn;
+            uint16_t sid = PCI_BUILD_BDF(bus_n, devfn);
+            VTDAddressSpace *vtd_as;
+
+            vtd_as = vtd_as_from_iommu_pasid(s, sid, pasid);
+            if (!vtd_as) {
+                vtd_as = vtd_find_add_as(s, info->bus, devfn, pasid);
+            }
+
+            if ((info->type == VTD_PASID_CACHE_DOMSI ||
+                 info->type == VTD_PASID_CACHE_PASIDSI) &&
+                !(info->domain_id == vtd_pe_get_domain_id(&pe))) {
+                /*
+                 * VTD_PASID_CACHE_DOMSI and VTD_PASID_CACHE_PASIDSI
+                 * requires domain ID check. If domain Id check fail,
+                 * go to next pasid.
+                 */
+                pasid = pasid_next;
+                continue;
+            }
+            vtd_fill_pe_in_cache(s, vtd_as, &pe);
+        }
+        pasid = pasid_next;
+    }
+}
+
+/*
+ * Currently, VT-d scalable mode pasid table is a two level table,
+ * this function aims to loop a range of PASIDs in a given pasid
+ * table to identify the pasid config in guest.
+ * Caller of this function should hold iommu_lock.
+ */
+static void vtd_sm_pasid_table_walk(IntelIOMMUState *s,
+                                    dma_addr_t pdt_base,
+                                    int start,
+                                    int end,
+                                    VTDPASIDCacheInfo *info)
+{
+    VTDPASIDDirEntry pdire;
+    int pasid = start;
+    int pasid_next;
+    dma_addr_t pt_base;
+
+    while (pasid < end) {
+        pasid_next = ((end - pasid) > VTD_PASID_TBL_ENTRY_NUM) ?
+                      (pasid + VTD_PASID_TBL_ENTRY_NUM) : end;
+        if (!vtd_get_pdire_from_pdir_table(pdt_base, pasid, &pdire)
+            && vtd_pdire_present(&pdire)) {
+            pt_base = pdire.val & VTD_PASID_TABLE_BASE_ADDR_MASK;
+            vtd_sm_pasid_table_walk_one(s, pt_base, pasid, pasid_next, info);
+        }
+        pasid = pasid_next;
+    }
+}
+
+static void vtd_replay_pasid_bind_for_dev(IntelIOMMUState *s,
+                                          int start, int end,
+                                          VTDPASIDCacheInfo *info)
+{
+    VTDContextEntry ce;
+    VTDAddressSpace *vtd_as;
+
+    vtd_as = vtd_find_add_as(s, info->bus, info->devfn, PCI_NO_PASID);
+
+    if (!vtd_as_to_context_entry(vtd_as, &ce)) {
+        uint32_t max_pasid;
+
+        max_pasid = vtd_sm_ce_get_pdt_entry_num(&ce) * VTD_PASID_TBL_ENTRY_NUM;
+        if (end > max_pasid) {
+            end = max_pasid;
+        }
+        vtd_sm_pasid_table_walk(s,
+                                VTD_CE_GET_PASID_DIR_TABLE(&ce),
+                                start,
+                                end,
+                                info);
+    }
+}
+
+/*
+ * This function replay the guest pasid bindings to hosts by
+ * walking the guest PASID table. This ensures host will have
+ * latest guest pasid bindings. Caller should hold iommu_lock.
+ */
+static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s,
+                                            VTDPASIDCacheInfo *pc_info)
+{
+    VTDHostIOMMUDevice *vtd_hiod;
+    int start = 0, end = 1; /* only rid2pasid is supported */
+    VTDPASIDCacheInfo walk_info;
+    GHashTableIter as_it;
+
+    switch (pc_info->type) {
+    case VTD_PASID_CACHE_PASIDSI:
+        start = pc_info->pasid;
+        end = pc_info->pasid + 1;
+        /*
+         * PASID selective invalidation is within domain,
+         * thus fall through.
+         */
+    case VTD_PASID_CACHE_DOMSI:
+    case VTD_PASID_CACHE_GLOBAL_INV:
+        /* loop all assigned devices */
+        break;
+    case VTD_PASID_CACHE_FORCE_RESET:
+        /* For force reset, no need to go further replay */
+        return;
+    default:
+        error_report("invalid pc_info->type for replay");
+        abort();
+    }
+
+    /*
+     * In this replay, only needs to care about the devices which
+     * are backed by host IOMMU. For such devices, their vtd_hiod
+     * instances are in the s->vtd_host_iommu_dev. For devices which
+     * are not backed by host IOMMU, it is not necessary to replay
+     * the bindings since their cache could be re-created in the future
+     * DMA address translation.
+     */
+    walk_info = *pc_info;
+    g_hash_table_iter_init(&as_it, s->vtd_host_iommu_dev);
+    while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_hiod)) {
+        /* bus|devfn fields are not identical with pc_info */
+        walk_info.bus = vtd_hiod->bus;
+        walk_info.devfn = vtd_hiod->devfn;
+        vtd_replay_pasid_bind_for_dev(s, start, end, &walk_info);
+    }
+}
+
+/*
+ * This function syncs the pasid bindings between guest and host.
+ * It includes updating the pasid cache in vIOMMU and updating the
+ * pasid bindings per guest's latest pasid entry presence.
+ */
+static void vtd_pasid_cache_sync(IntelIOMMUState *s,
+                                 VTDPASIDCacheInfo *pc_info)
+{
+    if (!s->flts || !s->root_scalable || !s->dmar_enabled) {
+        return;
+    }
+
+    /*
+     * Regards to a pasid cache invalidation, e.g. a PSI.
+     * it could be either cases of below:
+     * a) a present pasid entry moved to non-present
+     * b) a present pasid entry to be a present entry
+     * c) a non-present pasid entry moved to present
+     *
+     * Different invalidation granularity may affect different device
+     * scope and pasid scope. But for each invalidation granularity,
+     * it needs to do two steps to sync host and guest pasid binding.
+     *
+     * Here is the handling of a PSI:
+     * 1) loop all the existing vtd_as instances to update them
+     *    according to the latest guest pasid entry in pasid table.
+     *    this will make sure affected existing vtd_as instances
+     *    cached the latest pasid entries. Also, during the loop, the
+     *    host should be notified if needed. e.g. pasid unbind or pasid
+     *    update. Should be able to cover case a) and case b).
+     *
+     * 2) loop all devices to cover case c)
+     *    - For devices which are backed by HostIOMMUDeviceIOMMUFD instances,
+     *      we loop them and check if guest pasid entry exists. If yes,
+     *      it is case c), we update the pasid cache and also notify
+     *      host.
+     *    - For devices which are not backed by HostIOMMUDeviceIOMMUFD,
+     *      it is not necessary to create pasid cache at this phase since
+     *      it could be created when vIOMMU does DMA address translation.
+     *      This is not yet implemented since there is no emulated
+     *      pasid-capable devices today. If we have such devices in
+     *      future, the pasid cache shall be created there.
+     * Other granularity follow the same steps, just with different scope
+     *
+     */
+
+    vtd_iommu_lock(s);
+    /* Step 1: loop all the existing vtd_as instances */
+    g_hash_table_foreach_remove(s->vtd_address_spaces,
+                                vtd_flush_pasid, pc_info);
+
+    /*
+     * Step 2: loop all the existing vtd_hiod instances.
+     * Ideally, needs to loop all devices to find if there is any new
+     * PASID binding regards to the PASID cache invalidation request.
+     * But it is enough to loop the devices which are backed by host
+     * IOMMU. For devices backed by vIOMMU (a.k.a emulated devices),
+     * if new PASID happened on them, their vtd_as instance could
+     * be created during future vIOMMU DMA translation.
+     */
+    vtd_replay_guest_pasid_bindings(s, pc_info);
+    vtd_iommu_unlock(s);
+}
+
+static bool vtd_process_pasid_desc(IntelIOMMUState *s,
+                                   VTDInvDesc *inv_desc)
+{
+    uint16_t domain_id;
+    uint32_t pasid;
+    VTDPASIDCacheInfo pc_info;
+    uint64_t mask[4] = {VTD_INV_DESC_PASIDC_RSVD_VAL0, VTD_INV_DESC_ALL_ONE,
+                        VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
+
+    if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, true,
+                                     __func__, "pasid cache inv")) {
+        return false;
+    }
+
+    domain_id = VTD_INV_DESC_PASIDC_DID(inv_desc->val[0]);
+    pasid = VTD_INV_DESC_PASIDC_PASID(inv_desc->val[0]);
+
+    switch (inv_desc->val[0] & VTD_INV_DESC_PASIDC_G) {
+    case VTD_INV_DESC_PASIDC_DSI:
+        trace_vtd_pasid_cache_dsi(domain_id);
+        pc_info.type = VTD_PASID_CACHE_DOMSI;
+        pc_info.domain_id = domain_id;
+        break;
+
+    case VTD_INV_DESC_PASIDC_PASID_SI:
+        /* PASID selective implies a DID selective */
+        trace_vtd_pasid_cache_psi(domain_id, pasid);
+        pc_info.type = VTD_PASID_CACHE_PASIDSI;
+        pc_info.domain_id = domain_id;
+        pc_info.pasid = pasid;
+        break;
+
+    case VTD_INV_DESC_PASIDC_GLOBAL:
+        trace_vtd_pasid_cache_gsi();
+        pc_info.type = VTD_PASID_CACHE_GLOBAL_INV;
+        break;
+
+    default:
+        error_report_once("invalid-inv-granu-in-pc_inv_desc hi: 0x%" PRIx64
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);
+        return false;
+    }
+
+    vtd_pasid_cache_sync(s, &pc_info);
+    return true;
+}
+
 static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
                                      VTDInvDesc *inv_desc)
 {
@@ -3223,6 +3690,13 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)
         }
         break;
 
+    case VTD_INV_DESC_PC:
+        trace_vtd_inv_desc("pasid-cache", inv_desc.val[1], inv_desc.val[0]);
+        if (!vtd_process_pasid_desc(s, &inv_desc)) {
+            return false;
+        }
+        break;
+
     case VTD_INV_DESC_PIOTLB:
         trace_vtd_inv_desc("p-iotlb", inv_desc.val[1], inv_desc.val[0]);
         if (!vtd_process_piotlb_desc(s, &inv_desc)) {
@@ -3258,16 +3732,6 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)
         }
         break;
 
-    /*
-     * TODO: the entity of below two cases will be implemented in future series.
-     * To make guest (which integrates scalable mode support patch set in
-     * iommu driver) work, just return true is enough so far.
-     */
-    case VTD_INV_DESC_PC:
-        if (s->scalable_mode) {
-            break;
-        }
-    /* fallthrough */
     default:
         error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64
                           " (unknown type)", __func__, inv_desc.hi,
hw/i386/trace-events +4 -0
index 53c02d7ac8..a26b38b52c 100644
@@ -24,6 +24,10 @@ vtd_inv_qi_head(uint16_t head) "read head %d"
 vtd_inv_qi_tail(uint16_t head) "write tail %d"
 vtd_inv_qi_fetch(void) ""
 vtd_context_cache_reset(void) ""
+vtd_pasid_cache_gsi(void) ""
+vtd_pasid_cache_reset(void) ""
+vtd_pasid_cache_dsi(uint16_t domain) "Domain slective PC invalidation domain 0x%"PRIx16
+vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present"
 vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present"
 vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
#53 lore raw

RE: [PATCH rfcv2 13/20] intel_iommu: Add PASID cache management infrastructure

Quoted 45 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 13/20] intel_iommu: Add PASID cache management infrastructure

Quoted 8 lines · Zhenzhong Duan

This adds an new entry VTDPASIDCacheEntry in VTDAddressSpace to cache the pasid entry and track PASID usage and future PASID tagged DMA address translation support in vIOMMU.

VTDAddressSpace of PCI_NO_PASID is allocated when device is plugged and never freed. For other pasid, VTDAddressSpace instance is created/destroyed per the guest pasid entry set up/destroy for passthrough devices. While for emulated devices, VTDAddressSpace instance is created in the PASID tagged

DMA

Quoted 5 lines

translation and be destroyed per guest PASID cache invalidation. This focuses on the PASID cache management for passthrough devices as there is no PASID capable emulated devices yet.

When guest modifies a PASID entry, QEMU will capture the guest pasid

selective

Quoted 1 line

pasid cache invalidation, allocate or remove a VTDAddressSpace instance per

the

Quoted 7 lines

invalidation reasons:

*) a present pasid entry moved to non-present *) a present pasid entry to be a present entry *) a non-present pasid entry moved to present

vIOMMU emulator could figure out the reason by fetching latest guest pasid

entry

Quoted 9 lines

and compare it with the PASID cache.

---
 hw/i386/intel_iommu_internal.h |  29 ++
 include/hw/i386/intel_iommu.h  |   6 +
 hw/i386/intel_iommu.c          | 484 ++++++++++++++++++++++++++++++++-
Quoted 2 lines

Don't you have ways to split this patch. It has a huge change set and this is really heavy to digest at once (at least for me).

Sure, will try.

Quoted 105 lines
Quoted 16 lines
 hw/i386/trace-events           |   4 +
 4 files changed, 513 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 18bc22fc72..632fda2853 100644
@@ -315,6 +315,7 @@ typedef enum VTDFaultReason {
                                   * request while disabled */
     VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
+    VTD_FR_RTADDR_INV_TTM = 0x31,  /* Invalid TTM in RTADDR */
     /* PASID directory entry access failure */
     VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
     /* The Present(P) field of pasid directory entry is 0 */

hw/i386/intel_iommu_internal.h typedef union VTDInvDesc VTDInvDesc; · line 498

Quoted 6 lines
hw/i386/intel_iommu_internal.h +3 -0
@@ -492,6 +493,15 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000f1c0ULL
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
 
+#define VTD_INV_DESC_PASIDC_G          (3ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID(val) (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PASIDC_DID(val)   (((val) >> 16) &

VTD_DOMAIN_ID_MASK)

Quoted 10 lines
hw/i386/intel_iommu_internal.h +6 -0
+#define VTD_INV_DESC_PASIDC_RSVD_VAL0  0xfff000000000f1c0ULL
+
+#define VTD_INV_DESC_PASIDC_DSI        (0ULL << 4)
+#define VTD_INV_DESC_PASIDC_PASID_SI   (1ULL << 4)
+#define VTD_INV_DESC_PASIDC_GLOBAL     (3ULL << 4)
+
 /* Information about page-selective IOTLB invalidate */
 struct VTDIOTLBPageInvInfo {
     uint16_t domain_id;

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

Quoted 6 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -548,10 +558,28 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_CTX_ENTRY_LEGACY_SIZE     16
 #define VTD_CTX_ENTRY_SCALABLE_SIZE   32
 
+#define VTD_SM_CONTEXT_ENTRY_PDTS(val)      (((val) >> 9) & 0x7)
 #define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL |

~VTD_HAW_MASK(aw))

Quoted 23 lines
hw/i386/intel_iommu_internal.h +17 -0
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 
+typedef enum VTDPCInvType {
+    /* force reset all */
+    VTD_PASID_CACHE_FORCE_RESET = 0,
+    /* pasid cache invalidation rely on guest PASID entry */
+    VTD_PASID_CACHE_GLOBAL_INV,
+    VTD_PASID_CACHE_DOMSI,
+    VTD_PASID_CACHE_PASIDSI,
+} VTDPCInvType;
+
+typedef struct VTDPASIDCacheInfo {
+    VTDPCInvType type;
+    uint16_t domain_id;
+    uint32_t pasid;
+    PCIBus *bus;
+    uint16_t devfn;
+} VTDPASIDCacheInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)

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

Quoted 2 lines
hw/i386/intel_iommu_internal.h +0 -0
@@ -563,6 +591,7 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_PASID_TABLE_BITS_MASK     (0x3fULL)
 #define VTD_PASID_TABLE_INDEX(pasid)  ((pasid) &

VTD_PASID_TABLE_BITS_MASK)

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

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

*/

Quoted 33 lines
hw/i386/intel_iommu_internal.h +1 -0
+#define VTD_PASID_TBL_ENTRY_NUM       (1ULL << 6)
 
 /* PASID Granular Translation Type Mask */
 #define VTD_PASID_ENTRY_P              1ULL
include/hw/i386/intel_iommu.h +6 -0
index 50f9b27a45..fbc9da903a 100644
@@ -95,6 +95,11 @@ struct VTDPASIDEntry {
     uint64_t val[8];
 };
 
+typedef struct VTDPASIDCacheEntry {
+    struct VTDPASIDEntry pasid_entry;
+    bool cache_filled;
+} VTDPASIDCacheEntry;
+
 struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
@@ -107,6 +112,7 @@ struct VTDAddressSpace {
     MemoryRegion iommu_ir_fault; /* Interrupt region for catching fault */
     IntelIOMMUState *iommu_state;
     VTDContextCacheEntry context_cache_entry;
+    VTDPASIDCacheEntry pasid_cache_entry;
     QLIST_ENTRY(VTDAddressSpace) next;
     /* Superset of notifier flags that this address space has */
     IOMMUNotifierFlag notifier_flags;
hw/i386/intel_iommu.c +0 -0
index fafa199f52..b8f3b85803 100644

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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
@@ -86,6 +86,8 @@ struct vtd_iotlb_key {
 static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier

*n);

Quoted 1 line
Quoted 3 lines
Quoted 1 line

+static void vtd_pasid_cache_reset(IntelIOMMUState *s);

use _locked suffix to be consistent with the others and emphases the lock is held?

Will do.

Quoted 17 lines

hw/i386/intel_iommu.c static inline bool

Quoted 13 lines
hw/i386/intel_iommu.c +2 -0
+
 static void vtd_panic_require_caching_mode(void)
 {
     error_report("We need to set caching-mode=on for intel-iommu to enable "
@@ -390,6 +392,7 @@ static void vtd_reset_caches(IntelIOMMUState *s)
     vtd_iommu_lock(s);
     vtd_reset_iotlb_locked(s);
     vtd_reset_context_cache_locked(s);
+    vtd_pasid_cache_reset(s);
     vtd_iommu_unlock(s);
 }
 
@@ -825,6 +828,16 @@ static inline bool

vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)

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

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

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce)
+{
+    return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce->val[0]) + 7);
+}
+
+static inline uint16_t vtd_pe_get_domain_id(VTDPASIDEntry *pe)

nit: vtd_pe_get_did as the filed is named DID?

Will do.

Quoted 10 lines

hw/i386/intel_iommu.c static int

Quoted 9 lines
hw/i386/intel_iommu.c +4 -0
+{
+    return VTD_SM_PASID_ENTRY_DID((pe)->val[1]);
+}
+
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
 {
     return pdire->val & 1;
@@ -1617,6 +1630,54 @@ static int

vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)

Quoted 5 lines

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1633

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     }
 }
 
+/* Translate to iommu pasid if PCI_NO_PASID */

I don't really get the comment above. Ay best, shouldn't it be put in

Will do, it's pci's pasid value vs. guest's pasid value which I call iommu pasid.

Quoted 4 lines

(vtd_as->pasid == PCI_NO_PASID) block?
What you call "iommu pasid" is the value set in RID_PASID, right? Is "iommu pasid" a conventional terminology?

PCI subsystem uses PCI_NO_PASID(-1) to represent a Requests-without-PASID, but guest doesn't recognize PCI_NO_PASID(-1), so vIOMMU has to translate PCI_NO_PASID(-1) to an normal pasid value(pasid >= 0), I call it iommu pasid to distinguish with PCI's pasid, it's not conventional terminology.

Quoted 16 lines

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce) · line 1645

Quoted 13 lines
hw/i386/intel_iommu.c +12 -0
+static int vtd_as_to_iommu_pasid(VTDAddressSpace *vtd_as, uint32_t *pasid)
+{
+    VTDContextEntry ce;
+    int ret;
+
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    if (vtd_as->pasid == PCI_NO_PASID) {
+        *pasid = VTD_CE_GET_RID2PASID(&ce);

This is called RID_PASID in the spec. I think it would be easier for the reader if could have a direct match with named fields so that we can easily seach the spec.

I agree with you about the match, but rid2pasid is wildly used in intel_iommu.c for a long history. I'd like to here more voice before renaming.

Quoted 11 lines

hw/i386/intel_iommu.c static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntry *ce)

Quoted 8 lines
hw/i386/intel_iommu.c +8 -0
+    } else {
+        *pasid = vtd_as->pasid;
+    }
+
+    return 0;
+}
+
+static gboolean vtd_find_as_by_sid_and_iommu_pasid(gpointer key, gpointer

value,

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                                   gpointer user_data)

why iommu_pasid and not directly pasid?

Because vtd_as->pasid is PCI's pasid, it needs to be transformed into iommu pasid.

Quoted 31 lines

hw/i386/intel_iommu.c static bool

Quoted 30 lines
hw/i386/intel_iommu.c +26 -0
+{
+    VTDAddressSpace *vtd_as = (VTDAddressSpace *)value;
+    struct vtd_as_raw_key *target = (struct vtd_as_raw_key *)user_data;
+    uint16_t sid = PCI_BUILD_BDF(pci_bus_num(vtd_as->bus), vtd_as->devfn);
+    uint32_t pasid;
+
+    if (vtd_as_to_iommu_pasid(vtd_as, &pasid)) {
+        return false;
+    }
+
+    return (pasid == target->pasid) && (sid == target->sid);
+}
+
+/* Translate iommu pasid to vtd_as */
+static VTDAddressSpace *vtd_as_from_iommu_pasid(IntelIOMMUState *s,
+                                                uint16_t sid, uint32_t pasid)
+{
+    struct vtd_as_raw_key key = {
+        .sid = sid,
+        .pasid = pasid
+    };
+
+    return g_hash_table_find(s->vtd_address_spaces,
+                             vtd_find_as_by_sid_and_iommu_pasid, &key);
+}
+
 static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event,
                                      void *private)
 {
@@ -3062,6 +3123,412 @@ static bool

vtd_process_piotlb_desc(IntelIOMMUState *s,

Quoted 7 lines

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

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
     return true;
 }
 
+static inline int vtd_dev_get_pe_from_pasid(VTDAddressSpace *vtd_as,
+                                            uint32_t pasid, VTDPASIDEntry *pe)

does "pe" means pasid entry? It is not obvious for a dummy reader like me. May be worth a comment at least once.

May VTDPASIDEntry have given you enough hint?

Quoted 28 lines
Quoted 17 lines
hw/i386/intel_iommu.c +17 -0
+{
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    VTDContextEntry ce;
+    int ret;
+
+    if (!s->root_scalable) {
+        return -VTD_FR_RTADDR_INV_TTM;
+    }
+
+    ret = vtd_as_to_context_entry(vtd_as, &ce);
+    if (ret) {
+        return ret;
+    }
+
+    return vtd_ce_get_pasid_entry(s, &ce, pe, pasid);
+}
+

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+static bool vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2)
+{
+    return !memcmp(p1, p2, sizeof(*p1));
+}
+
+/*
+ * This function fills in the pasid entry in &vtd_as. Caller
+ * of this function should hold iommu_lock.
+ */
+static void vtd_fill_pe_in_cache(IntelIOMMUState *s, VTDAddressSpace

*vtd_as,

Quoted 2 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                 VTDPASIDEntry *pe)

seems some other functions used the _locked suffix

Yes, vtd_pasid_cache_reset_locked() is, so no need to add suffix here, right?

Quoted 6 lines
Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+{
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+
+    if (vtd_pasid_entry_compare(pe, &pc_entry->pasid_entry)) {
+        /* No need to go further as cached pasid entry is latest */
+        return;
Quoted 8 lines

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

Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+    }
+
+    pc_entry->pasid_entry = *pe;
+    pc_entry->cache_filled = true;
+    /*
+     * TODO: send pasid bind to host for passthru devices
+     */

what does it mean?

Here means a new entry is found, we need to send pasid<->hwpt binding to host.

Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+}
+
+/*
+ * This function is used to clear cached pasid entry in vtd_as
+ * instances. Caller of this function should hold iommu_lock.
+ */
+static gboolean vtd_flush_pasid(gpointer key, gpointer value,
+                                gpointer user_data)
+{
+    VTDPASIDCacheInfo *pc_info = user_data;
+    VTDAddressSpace *vtd_as = value;
+    IntelIOMMUState *s = vtd_as->iommu_state;
Quoted 8 lines

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

Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+    VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+    VTDPASIDEntry pe;
+    uint16_t did;
+    uint32_t pasid;
+    int ret;
+
+    /* Replay only fill pasid entry cache for passthrough device */

filled

Will do.

Thanks
Zhenzhong

#54 lore raw

RFC PATCH v2 14/20 intel_iommu: Bind/unbind guest page table to host

In reply to Zhenzhong Duan

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

When PGTT is Pass-through(100b), the hwpt on host side is a stage-2 page table(GPA->HPA). When PGTT is First-stage Translation only(001b), the hwpt on host side is a nested page table.

The guest page table is configured as stage-1 page table (gIOVA->GPA) whose translation result would further go through host VT-d stage-2 page table(GPA->HPA) under nested translation mode. This is the key to support gIOVA over stage-1 page table for Intel VT-d in virtualization environment.

Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

See below example diagram:

      IntelIOMMUState
             |
             V
    .------------------.    .------------------.
    | VTDIOASContainer |--->| VTDIOASContainer |--->...
    |    (iommufd0)    |    |    (iommufd1)    |
    .------------------.    .------------------.
             |                       |
             |                       .-->...
             V
      .-------------------.    .-------------------.
      |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...
      .-------------------.    .-------------------.
          |            |               |
          |            |               |
    .-----------.  .-----------.  .------------.
    | IOMMUFD   |  | IOMMUFD   |  | IOMMUFD    |
    | Device(CC)|  | Device(CC)|  | Device     |
    | (iommufd0)|  | (iommufd0)|  | (non-CC)   |
    |           |  |           |  | (iommufd0) |
    .-----------.  .-----------.  .------------.
---
 hw/i386/intel_iommu_internal.h |  11 +
 include/hw/i386/intel_iommu.h  |  24 ++
 hw/i386/intel_iommu.c          | 581 +++++++++++++++++++++++++++++++--
 hw/i386/trace-events           |   8 +
 4 files changed, 604 insertions(+), 20 deletions(-)
hw/i386/intel_iommu_internal.h +11 -0
index 632fda2853..23b7e236b0 100644
@@ -563,6 +563,13 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw)  (0x1e0ULL | ~VTD_HAW_MASK(aw))
 #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1      0xffffffffffe00000ULL
 
+typedef enum VTDPASIDOp {
+    VTD_PASID_BIND,
+    VTD_PASID_UPDATE,
+    VTD_PASID_UNBIND,
+    VTD_OP_NUM
+} VTDPASIDOp;
+
 typedef enum VTDPCInvType {
     /* force reset all */
     VTD_PASID_CACHE_FORCE_RESET = 0,
@@ -578,6 +585,7 @@ typedef struct VTDPASIDCacheInfo {
     uint32_t pasid;
     PCIBus *bus;
     uint16_t devfn;
+    bool error_happened;
 } VTDPASIDCacheInfo;
 
 /* PASID Table Related Definitions */
@@ -606,6 +614,9 @@ typedef struct VTDPASIDCacheInfo {
 
 #define VTD_SM_PASID_ENTRY_FLPM          3ULL
 #define VTD_SM_PASID_ENTRY_FLPTPTR       (~0xfffULL)
+#define VTD_SM_PASID_ENTRY_SRE_BIT(val)  (!!((val) & 1ULL))
+#define VTD_SM_PASID_ENTRY_WPE_BIT(val)  (!!(((val) >> 4) & 1ULL))
+#define VTD_SM_PASID_ENTRY_EAFE_BIT(val) (!!(((val) >> 7) & 1ULL))
 
 /* First Level Paging Structure */
 /* Masks for First Level Paging Entry */
include/hw/i386/intel_iommu.h +24 -0
index fbc9da903a..594281c1d3 100644
@@ -100,10 +100,32 @@ typedef struct VTDPASIDCacheEntry {
     bool cache_filled;
 } VTDPASIDCacheEntry;
 
+typedef struct VTDIOASContainer {
+    struct IOMMUFDBackend *iommufd;
+    uint32_t ioas_id;
+    MemoryListener listener;
+    QLIST_HEAD(, VTDS2Hwpt) s2_hwpt_list;
+    QLIST_ENTRY(VTDIOASContainer) next;
+    Error *error;
+} VTDIOASContainer;
+
+typedef struct VTDS2Hwpt {
+    uint32_t users;
+    uint32_t hwpt_id;
+    VTDIOASContainer *container;
+    QLIST_ENTRY(VTDS2Hwpt) next;
+} VTDS2Hwpt;
+
+typedef struct VTDHwpt {
+    uint32_t hwpt_id;
+    VTDS2Hwpt *s2_hwpt;
+} VTDHwpt;
+
 struct VTDAddressSpace {
     PCIBus *bus;
     uint8_t devfn;
     uint32_t pasid;
+    VTDHwpt hwpt;
     AddressSpace as;
     IOMMUMemoryRegion iommu;
     MemoryRegion root;          /* The root container of the device */
@@ -303,6 +325,8 @@ struct IntelIOMMUState {
 
     GHashTable *vtd_host_iommu_dev;             /* VTDHostIOMMUDevice */
 
+    QLIST_HEAD(, VTDIOASContainer) containers;
+
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
     dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +561 -20

668 lines not on this page · raw message

hw/i386/trace-events +8 -0
index a26b38b52c..22559eb787 100644
@@ -72,6 +72,14 @@ vtd_frr_new(int index, uint64_t hi, uint64_t lo) "index %d high 0x%"PRIx64" low
 vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16
 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)"
 vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)"
+vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d"
+vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"
+vtd_device_fail_attach_existing_hwpt(const char *msg) " %s"
+vtd_device_attach_container(int fd, uint32_t dev_id, uint32_t pasid, uint32_t ioas_id, uint32_t hwpt_id) "iommufd %d dev_id %d pasid %d ioas_id %d hwpt_id %d"
+vtd_device_detach_container(int fd, uint32_t dev_id, uint32_t pasid) "iommufd %d dev_id %d pasid %d"
+vtd_device_fail_attach_existing_container(const char *msg) " %s"
+vtd_device_alloc_ioas(int fd, uint32_t ioas_id) "iommufd %d ioas_id %d"
 
 # amd_iommu.c
 amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
Signature · 1 line
2.34.1
#55 lore raw

RFC PATCH v2 15/20 intel_iommu: ERRATA_772415 workaround

In reply to Zhenzhong Duan

On a system influenced by ERRATA_772415, IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 is repored by IOMMU_DEVICE_GET_HW_INFO. Due to this errata, even the readonly range mapped on stage-2 page table could still be written.

Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17.

[0] https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/eagle-stream/sapphire-rapids-specification-update

We utilize the new added IOMMUFD container/ioas/hwpt management framework in VTD. Add a check to create new VTDIOASContainer to hold RW-only mappings, then this VTDIOASContainer can be used as backend for device with ERRATA_772415. See below diagram for details:

      IntelIOMMUState
             |
             V
    .------------------.    .------------------.    .-------------------.
    | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |-->...
    | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
    .------------------.    .------------------.    .-------------------.
             |                       |                              |
             |                       .-->...                        |
             V                                                      V
      .-------------------.    .-------------------.          .---------------.
      |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-->...
      .-------------------.    .-------------------.          .---------------.
          |            |               |                            |
          |            |               |                            |
    .-----------.  .-----------.  .------------.              .------------.
    | IOMMUFD   |  | IOMMUFD   |  | IOMMUFD    |              | IOMMUFD    |
    | Device(CC)|  | Device(CC)|  | Device     |              | Device(CC) |
    | (iommufd0)|  | (iommufd0)|  | (non-CC)   |              | (errata)   |
    |           |  |           |  | (iommufd0) |              | (iommufd0) |
    .-----------.  .-----------.  .------------.              .------------.

Changed to pass VTDHostIOMMUDevice pointer to vtd_check_hdev() so errata could be saved.

---
 hw/i386/intel_iommu_internal.h |  1 +
 include/hw/i386/intel_iommu.h  |  1 +
 hw/i386/intel_iommu.c          | 26 +++++++++++++++++++-------
 3 files changed, 21 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 23b7e236b0..8558781af8 100644
@@ -654,5 +654,6 @@ typedef struct VTDHostIOMMUDevice {
     PCIBus *bus;
     uint8_t devfn;
     HostIOMMUDevice *hiod;
+    uint32_t errata;
 } VTDHostIOMMUDevice;
 #endif
include/hw/i386/intel_iommu.h +1 -0
index 594281c1d3..9b156dc32e 100644
@@ -103,6 +103,7 @@ typedef struct VTDPASIDCacheEntry {
 typedef struct VTDIOASContainer {
     struct IOMMUFDBackend *iommufd;
     uint32_t ioas_id;
+    uint32_t errata;
     MemoryListener listener;
     QLIST_HEAD(, VTDS2Hwpt) s2_hwpt_list;
     QLIST_ENTRY(VTDIOASContainer) next;
hw/i386/intel_iommu.c +19 -7
index e36ac44110..dae1716629 100644
@@ -2443,7 +2443,8 @@ static void vtd_context_global_invalidate(IntelIOMMUState *s)
 }
 
 #ifdef CONFIG_IOMMUFD
-static bool iommufd_listener_skipped_section(MemoryRegionSection *section)
+static bool iommufd_listener_skipped_section(VTDIOASContainer *container,
+                                             MemoryRegionSection *section)
 {
     return !memory_region_is_ram(section->mr) ||
            memory_region_is_protected(section->mr) ||
@@ -2453,7 +2454,8 @@ static bool iommufd_listener_skipped_section(MemoryRegionSection *section)
             * are never accessed by the CPU and beyond the address width of
             * some IOMMU hardware.  TODO: VFIO should tell us the IOMMU width.
             */
-           section->offset_within_address_space & (1ULL << 63);
+           section->offset_within_address_space & (1ULL << 63) ||
+           (container->errata && section->readonly);
 }
 
 static void iommufd_listener_region_add_s2domain(MemoryListener *listener,
@@ -2469,7 +2471,7 @@ static void iommufd_listener_region_add_s2domain(MemoryListener *listener,
     Error *err = NULL;
     int ret;
 
-    if (iommufd_listener_skipped_section(section)) {
+    if (iommufd_listener_skipped_section(container, section)) {
         return;
     }
     iova = REAL_HOST_PAGE_ALIGN(section->offset_within_address_space);
@@ -2520,7 +2522,7 @@ static void iommufd_listener_region_del_s2domain(MemoryListener *listener,
     Int128 llend, llsize;
     int ret;
 
-    if (iommufd_listener_skipped_section(section)) {
+    if (iommufd_listener_skipped_section(container, section)) {
         return;
     }
     iova = REAL_HOST_PAGE_ALIGN(section->offset_within_address_space);
@@ -2776,7 +2778,8 @@ static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
 
     /* try to attach to an existing container in this space */
     QLIST_FOREACH(container, &s->containers, next) {
-        if (container->iommufd != iommufd) {
+        if (container->iommufd != iommufd ||
+            container->errata != vtd_hiod->errata) {
             continue;
         }
 
@@ -2803,6 +2806,7 @@ static int vtd_device_attach_iommufd(VTDHostIOMMUDevice *vtd_hiod,
     container = g_malloc0(sizeof(*container));
     container->iommufd = iommufd;
     container->ioas_id = ioas_id;
+    container->errata = vtd_hiod->errata;
     QLIST_INIT(&container->s2_hwpt_list);
 
     if (vtd_device_attach_container(vtd_hiod, container, pasid, pe, hwpt,
@@ -5329,9 +5333,10 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     return vtd_dev_as;
 }
 
-static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
+static bool vtd_check_hiod(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                            Error **errp)
 {
+    HostIOMMUDevice *hiod = vtd_hiod->hiod;
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_GET_CLASS(hiod);
     int ret;
 
@@ -5388,6 +5393,12 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
         return false;
     }
 
+    ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_ERRATA, errp);
+    if (ret < 0) {
+        return false;
+    }
+    vtd_hiod->errata = ret;
+
     error_setg(errp, "host device is uncompatible with stage-1 translation");
     return false;
 }
@@ -5419,7 +5430,8 @@ static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
     vtd_hiod->iommu_state = s;
     vtd_hiod->hiod = hiod;
 
-    if (!vtd_check_hiod(s, hiod, errp)) {
+    if (!vtd_check_hiod(s, vtd_hiod, errp)) {
+        g_free(vtd_hiod);
         vtd_iommu_unlock(s);
         return false;
     }
Signature · 1 line
2.34.1
#56 lore raw

RFC PATCH v2 16/20 intel_iommu: Replay pasid binds after context cache invalidation

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This replays guest pasid attachments after context cache invalidation. This is a behavior to ensure safety. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing a context cache invalidation.

---
 hw/i386/intel_iommu_internal.h |  1 +
 hw/i386/intel_iommu.c          | 51 ++++++++++++++++++++++++++++++++--
 hw/i386/trace-events           |  1 +
 3 files changed, 51 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_internal.h +1 -0
index 8558781af8..8f7be7f123 100644
@@ -575,6 +575,7 @@ typedef enum VTDPCInvType {
     VTD_PASID_CACHE_FORCE_RESET = 0,
     /* pasid cache invalidation rely on guest PASID entry */
     VTD_PASID_CACHE_GLOBAL_INV,
+    VTD_PASID_CACHE_DEVSI,
     VTD_PASID_CACHE_DOMSI,
     VTD_PASID_CACHE_PASIDSI,
 } VTDPCInvType;
hw/i386/intel_iommu.c +49 -2

99 lines not on this page · raw message

hw/i386/trace-events +1 -0
index 22559eb787..e520133f18 100644
@@ -28,6 +28,7 @@ vtd_pasid_cache_gsi(void) ""
 vtd_pasid_cache_reset(void) ""
 vtd_pasid_cache_dsi(uint16_t domain) "Domain slective PC invalidation domain 0x%"PRIx16
 vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC invalidation domain 0x%"PRIx16" pasid 0x%"PRIx32
+vtd_pasid_cache_devsi(uint16_t devfn) "Dev selective PC invalidation dev: 0x%"PRIx16
 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present"
 vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8" devfn %"PRIu8" not present"
 vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
Signature · 1 line
2.34.1
#57 lore raw

RFC PATCH v2 17/20 intel_iommu: Propagate PASID-based iotlb invalidation to host

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

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

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

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

---
 hw/i386/intel_iommu_internal.h |   6 ++
 hw/i386/intel_iommu.c          | 116 ++++++++++++++++++++++++++++++++-
 2 files changed, 120 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_internal.h +6 -0
index 8f7be7f123..630394a8c3 100644
@@ -589,6 +589,12 @@ typedef struct VTDPASIDCacheInfo {
     bool error_happened;
 } VTDPASIDCacheInfo;
 
+typedef struct VTDPIOTLBInvInfo {
+    uint16_t domain_id;
+    uint32_t pasid;
+    struct iommu_hwpt_vtd_s1_invalidate *inv_data;
+} VTDPIOTLBInvInfo;
+
 /* PASID Table Related Definitions */
 #define VTD_PASID_DIR_BASE_ADDR_MASK  (~0xfffULL)
 #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
hw/i386/intel_iommu.c +114 -2

152 lines not on this page · raw message

Signature · 1 line
2.34.1
#58 lore raw

RFC PATCH v2 18/20 intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

When either 'Set Root Table Pointer' or 'Translation Enable' bit is changed, the pasid bindings on host side become stale and need to be updated.

Introduce a helper function vtd_refresh_pasid_bind() for that purpose.

---
 hw/i386/intel_iommu.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
hw/i386/intel_iommu.c +25 -0
index 8f7fb473f5..225e332132 100644
@@ -89,6 +89,7 @@ struct vtd_iotlb_key {
 
 static void vtd_address_space_refresh_all(IntelIOMMUState *s);
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
+static void vtd_refresh_pasid_bind(IntelIOMMUState *s);
 
 static void vtd_pasid_cache_reset(IntelIOMMUState *s);
 static void vtd_pasid_cache_sync(IntelIOMMUState *s,
@@ -3366,6 +3367,7 @@ static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
     vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
     vtd_reset_caches(s);
     vtd_address_space_refresh_all(s);
+    vtd_refresh_pasid_bind(s);
 }
 
 /* Set Interrupt Remap Table Pointer */
@@ -3400,6 +3402,7 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
 
     vtd_reset_caches(s);
     vtd_address_space_refresh_all(s);
+    vtd_refresh_pasid_bind(s);
 }
 
 /* Handle Interrupt Remap Enable/Disable */
@@ -4109,6 +4112,28 @@ static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s,
     }
 }
 
+static void vtd_refresh_pasid_bind(IntelIOMMUState *s)
+{
+    VTDPASIDCacheInfo pc_info = { .error_happened = false,
+                                  .type = VTD_PASID_CACHE_GLOBAL_INV };
+
+    /*
+     * Only when dmar is enabled, should pasid bindings replayed,
+     * otherwise no need to replay.
+     */
+    if (!s->dmar_enabled) {
+        return;
+    }
+
+    if (!s->flts || !s->root_scalable) {
+        return;
+    }
+
+    vtd_iommu_lock(s);
+    vtd_replay_guest_pasid_bindings(s, &pc_info);
+    vtd_iommu_unlock(s);
+}
+
 /*
  * This function syncs the pasid bindings between guest and host.
  * It includes updating the pasid cache in vIOMMU and updating the
Signature · 1 line
2.34.1
#59 lore raw

RFC PATCH v2 19/20 intel_iommu: Bypass replay in stage-1 page table mode

In reply to Zhenzhong Duan

VFIO utilizes replay to setup initial shadow iommu mappings. But when stage-1 page table is configured, it is passed to host to construct nested page table, there is no replay needed.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +8 -0
index 225e332132..e4b83cbe50 100644
@@ -5743,6 +5743,14 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     VTDContextEntry ce;
     DMAMap map = { .iova = 0, .size = HWADDR_MAX };
 
+    /*
+     * Replay on stage-1 page table is meaningless as stage-1 page table
+     * is passthroughed to host to construct nested page table
+     */
+    if (s->flts && s->root_scalable) {
+        return;
+    }
+
     /* replay is protected by BQL, page walk will re-setup it safely */
     iova_tree_remove(vtd_as->iova_tree, map);
 
Signature · 1 line
2.34.1
#60 lore raw

RFC PATCH v2 20/20 intel_iommu: Enable host device when x-flts=on in scalable mode

In reply to Zhenzhong Duan

Now that all infrastructures of supporting passthrough device running with stage-1 translation are there, enable it now.

---
 hw/i386/intel_iommu.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -2
index e4b83cbe50..908c28f9be 100644
@@ -5583,8 +5583,7 @@ static bool vtd_check_hiod(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
     }
     vtd_hiod->errata = ret;
 
-    error_setg(errp, "host device is uncompatible with stage-1 translation");
-    return false;
+    return true;
 }
 
 static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
Signature · 1 line
2.34.1
#61 lore raw
In reply to Zhenzhong Duan

Hi Zhenzhong

Quoted 11 lines · Zhenzhong Duan

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

Quoted 4 lines

The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform

s/be/is

Quoted 102 lines

the stage-1 address translation. Along with it, modifications to present mappings in the guest I/O page table should be followed with an IOTLB invalidation.

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

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

See below diagram:

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

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

      IntelIOMMUState
             |
             V
    .------------------.    .------------------.    .-------------------.
    | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |-->...
    | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
    .------------------.    .------------------.    .-------------------.
             |                       |                              |
             |                       .-->...                        |
             V                                                      V
      .-------------------.    .-------------------.          .---------------.
      |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-->...
      .-------------------.    .-------------------.          .---------------.
          |            |               |                            |
          |            |               |                            |
    .-----------.  .-----------.  .------------.              .------------.
    | IOMMUFD   |  | IOMMUFD   |  | IOMMUFD    |              | IOMMUFD    |
    | Device(CC)|  | Device(CC)|  | Device     |              | Device(CC) |
    | (iommufd0)|  | (iommufd0)|  | (non-CC)   |              | (errata)   |
    |           |  |           |  | (iommufd0) |              | (iommufd0) |
    .-----------.  .-----------.  .------------.              .------------.

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

Quoted 8 lines

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1 translation. i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

you're not mentioning lack of error reporting from HW S1 faults to guests. Are there other deps missing?

Eric

Quoted 74 lines

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

PATCH1-8:  Add HWPT-based nesting infrastructure support
PATCH9-10: Some cleanup work
PATCH11:   cap/ecap related compatibility check between vIOMMU and Host IOMMU

PATCH12-19:Implement stage-1 page table for passthrough device
PATCH20: Enable stage-1 translation for passthrough device

Qemu code can be found at [3]

TODO:
- RAM discard
- dirty tracking on stage-2 page table

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_rfcv2

Thanks
Zhenzhong

Changelog:
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace

Yi Liu (3):
intel_iommu: Replay pasid binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

Zhenzhong Duan (17):
backends/iommufd: Add helpers for invalidating user-managed HWPT vfio/iommufd: Add properties and handlers to
TYPE_HOST_IOMMU_DEVICE_IOMMUFD
HostIOMMUDevice: Introduce realize_late callback vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler vfio/iommufd: Implement [at|de]tach_hwpt handlers
host_iommu_device: Define two new capabilities
HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Add PASID cache management infrastructure
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode

 hw/i386/intel_iommu_internal.h     |   56 +
 include/hw/i386/intel_iommu.h      |   33 +-
 include/system/host_iommu_device.h |   40 +
 include/system/iommufd.h           |   53 +
 backends/iommufd.c                 |   58 +
 hw/i386/intel_iommu.c              | 1660 ++++++++++++++++++++++++----
 hw/vfio/common.c                   |   17 +-
 hw/vfio/iommufd.c                  |   48 +
 backends/trace-events              |    1 +
 hw/i386/trace-events               |   13 +
 10 files changed, 1776 insertions(+), 203 deletions(-)
#62 lore raw

Hi Eric,

Quoted 133 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Hi Zhenzhong

Quoted 11 lines · Zhenzhong Duan

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

Quoted 4 lines

The key design is to utilize the dual-stage IOMMU translation (also known as IOMMU nested translation) capability in host IOMMU. As the below diagram shows, guest I/O page table pointer in GPA (guest physical address) is passed to host and be used to perform

s/be/is

Quoted 77 lines

the stage-1 address translation. Along with it, modifications to present mappings in the guest I/O page table should be followed with an IOTLB invalidation.

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

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

See below diagram:

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

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

Quoted 13 lines
      IntelIOMMUState
             |
             V
    .------------------.    .------------------.    .-------------------.
    | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |--
...
    | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
    .------------------.    .------------------.    .-------------------.
             |                       |                              |
             |                       .-->...                        |
             V                                                      V
      .-------------------.    .-------------------.          .---------------.
      |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-

->...

Quoted 10 lines

.-------------------. .-------------------. .---------------.
| | | |
| | | |
.-----------. .-----------. .------------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device | | Device(CC) |
| (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) |
| | | | | (iommufd0) | | (iommufd0) |
.-----------. .-----------. .------------. .------------.

Quoted 7 lines

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

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1

Quoted 8 lines

translation.

Quoted 4 lines

i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

you're not mentioning lack of error reporting from HW S1 faults to guests. Are there other deps missing?

Good question, this will be in future series. Plan is:

1) vtd nesting
2) pasid support
3) PRQ support(this includes S1 faults passing)

So to play with this series, we have to presume guest kernel always construct correct S1 page table for passthrough device, for emulated devices, the emulation code already provided S1 fault injection.

Thanks
Zhenzhong

#63 lore raw
In reply to Zhenzhong Duan

Zhenzhong,

Hi!
Eric asked me to review this series.
Since it's rather late since you posted will summarize review feedback below/bottom.

- Don

Quoted 41 lines · Zhenzhong Duan

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

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

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

I'd prefer the use of 's1' for stage1/First stage, and 's2' for stage2/second stage. We don't need different terms for the same technology in the iommu/iommufd space(s).

Quoted 27 lines

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

See below diagram:

         VFIO Device                                 Intel IOMMU
     .-----------------.                         .-------------------.
     |                 |                         |                   |
     |       .---------|PCIIOMMUOps              |.-------------.    |
     |       | IOMMUFD |(set_iommu_device)       || Host IOMMU  |    |
     |       | Device  |------------------------>|| Device list |    |
     |       .---------|(unset_iommu_device)     |.-------------.    |
     |                 |                         |       |           |
     |                 |                         |       V           |
     |       .---------|  HostIOMMUDeviceIOMMUFD |  .-------------.  |
     |       | IOMMUFD |            (attach_hwpt)|  | Host IOMMU  |  |
     |       | link    |<------------------------|  |   Device    |  |
     |       .---------|            (detach_hwpt)|  .-------------.  |
     |                 |                         |       |           |
     |                 |                         |       ...         |
     .-----------------.                         .-------------------.
Quoted 7 lines

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is

and 'devices under the same guest'.
Different guests cant be sharing the same stage-2 page tables.

Quoted 107 lines

conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

       IntelIOMMUState
              |
              V
     .------------------.    .------------------.    .-------------------.
     | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |-->...
     | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
     .------------------.    .------------------.    .-------------------.
              |                       |                              |
              |                       .-->...                        |
              V                                                      V
       .-------------------.    .-------------------.          .---------------.
       |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-->...
       .-------------------.    .-------------------.          .---------------.
           |            |               |                            |
           |            |               |                            |
     .-----------.  .-----------.  .------------.              .------------.
     | IOMMUFD   |  | IOMMUFD   |  | IOMMUFD    |              | IOMMUFD    |
     | Device(CC)|  | Device(CC)|  | Device     |              | Device(CC) |
     | (iommufd0)|  | (iommufd0)|  | (non-CC)   |              | (errata)   |
     |           |  |           |  | (iommufd0) |              | (iommufd0) |
     .-----------.  .-----------.  .------------.              .------------.

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

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1 translation. i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

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

PATCH1-8:  Add HWPT-based nesting infrastructure support
PATCH9-10: Some cleanup work
PATCH11:   cap/ecap related compatibility check between vIOMMU and Host IOMMU

PATCH12-19:Implement stage-1 page table for passthrough device
PATCH20: Enable stage-1 translation for passthrough device

Qemu code can be found at [3]

TODO:
- RAM discard
- dirty tracking on stage-2 page table

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/[email protected]/
[3] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_rfcv2

Thanks
Zhenzhong

Changelog:
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of iommufd/devid/ioas_id
- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace

Yi Liu (3):
intel_iommu: Replay pasid binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

Zhenzhong Duan (17):
backends/iommufd: Add helpers for invalidating user-managed HWPT vfio/iommufd: Add properties and handlers to
TYPE_HOST_IOMMU_DEVICE_IOMMUFD
HostIOMMUDevice: Introduce realize_late callback vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler vfio/iommufd: Implement [at|de]tach_hwpt handlers
host_iommu_device: Define two new capabilities
HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Add PASID cache management infrastructure
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode

Quoted 12 lines
  hw/i386/intel_iommu_internal.h     |   56 +
  include/hw/i386/intel_iommu.h      |   33 +-
  include/system/host_iommu_device.h |   40 +
  include/system/iommufd.h           |   53 +
  backends/iommufd.c                 |   58 +
  hw/i386/intel_iommu.c              | 1660 ++++++++++++++++++++++++----
  hw/vfio/common.c                   |   17 +-
  hw/vfio/iommufd.c                  |   48 +
  backends/trace-events              |    1 +
  hw/i386/trace-events               |   13 +
  10 files changed, 1776 insertions(+), 203 deletions(-)

Relative to the patches:
Patch 1: As Eric eluded to, a proper description for the patch should be written, and the title should change 'helpers' to 'helper'

Patch 2:
(1) Introduce 'realize_late()' interface, but leave the reader wondering 'ah, why? what?' ... after reading farther down the series, I learn more about realize_late(), but more on that later...
(2) For my education, can you provide ptrs to VFIO & VPDA code paths that demonstrate the need for different [at|de]tach_<>_hwpt()

Patch 3: Why can't the realize() be moved to after attach? isn't realize() suppose to indicate 'all is setup and object can now be used' -- apologies for what could be a dumb question, as that's my understanding of realize(). If the argument is such that there needs to be two steps, how does the first realize() that put the object into a used state <somehow> wait until realize_late()?

Patch 4: Shouldn't the current/existing realize callback just be overwritten with the later one, when this is needed?

Patch 5: no issues.

Patch 6: ewww -- fs1gp ... we use underlines all over the place for multi-word elements; so how about 's1_1g_pg'
-- how many places is that really used that multiple underlines is an issue?

Patch 7: intel-iommu-specific callbacks in the common vfio & iommufd-backend code; nack. This won't compile w/intel-iommu included with iommufd... I think backend, intel-iommu hw-caps should provide the generic 'caps' boolean-type values/states; ... and maybe they should be extracted via vfio? .... like

     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
         return vfio_device_get_aw_bits(hiod->agent);

Patch 8: Again, VTD-specific code in IOMMUFD is a nack; again, maybe via vfio, or a direct call into an iommu-device-cap api.

Patch 9: no issues.

Patch 10: "except it's stale" likely "except when it's entry is stale" ?
Did you ever put some tracing in to capture avg hits in cache? ... if so, add as a comment. Otherwise, looks good.

Patch 11: Apologies, I don't know what 'flts' stands for, and why it is relative to 2-stage mapping, or SIOV. Could you add verbage to explain the use of it, as the rest of this patch doesn't make any sense to me without the background. The patch introduces hw-info-type (none or intel), and then goes on to add a large set of checks; seems like the caps & this checking should go together (split for each cap; put all caps together & the check...).

Patch 12: Why isn't HostIOMMUDevice extended to have another iommu-specif element, opaque in HostIOMMUDevice, but set to specific IOMMU in use? e.g. void *hostiommustate;

Patch 13: Isn't PASID just an extension/addition of BDF id? and doesn't each PASID have its own address space?
So, why isn't it handle via a uniqe AS cache like 'any other device'? Maybe I'm thinking too SMMU-StreamID, which can be varying length, depending on subsystem support. I see what appears to be sid+pasid calls to do the AS lookups; hmm, so maybe this is the generalized BDF+pasid AS lookup? if so, maybe a better description stating this transition to a wider stream-id would set the code context better. As for the rest of the (400 intel-iommu) code, I'm not that in-depth in intel-iommu to determine if its all correct or not.

Patch 14: Define PGTT; the second paragraph seem self-contradicting -- it says it uses a 2-stage page table in each case, but it implies it should be different. At 580 lines of code changes, you win! ;-)

Patch 15: Read-only and Read/write areas have different IOMMUFDs? is that an intel-iommu requriement?
At least this intel-iommu-errata code is only in hw/i386/<> modules.

Patch 16: Looks reasonable. What does the 'SI' mean after "CACHE_DEV", "CACHE_DOM" & "CACHE_PASID" ? -- stream-invalidation?

Patch 17: Looks reasonable.

Patch 18: Looks good.

Patch 19: No comment; believe AlexW should weigh in on this change.

Patch 20: Looks good.

Patch 17:

Patch 14

Patch 14:

#64 lore raw

Hi Donald,

Quoted 61 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Zhenzhong,

Hi!
Eric asked me to review this series.
Since it's rather late since you posted will summarize review feedback below/bottom.

- Don

Quoted 41 lines · Zhenzhong Duan

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

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

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

Quoted 3 lines

I'd prefer the use of 's1' for stage1/First stage, and 's2' for stage2/second stage. We don't need different terms for the same technology in the iommu/iommufd space(s).

OK, then I'd like to use stage1 and stage2 everywhere which is more verbose.

Quoted 32 lines
Quoted 32 lines

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

See below diagram:

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

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

Quoted 5 lines
Quoted 3 lines

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is

and 'devices under the same guest'.
Different guests cant be sharing the same stage-2 page tables.

Yes, will update.

Quoted 127 lines
Quoted 13 lines

conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

Quoted 13 lines
       IntelIOMMUState
              |
              V
     .------------------.    .------------------.    .-------------------.
     | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |--
...
     | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
     .------------------.    .------------------.    .-------------------.
              |                       |                              |
              |                       .-->...                        |
              V                                                      V
       .-------------------.    .-------------------.          .---------------.
       |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-

->...

Quoted 10 lines

.-------------------. .-------------------. .---------------.
| | | |
| | | |
.-----------. .-----------. .------------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device | | Device(CC) |
| (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) |
| | | | | (iommufd0) | | (iommufd0) |
.-----------. .-----------. .------------. .------------.

Quoted 7 lines

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

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1

translation.

Quoted 5 lines

i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

Quoted 8 lines

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

PATCH1-8:  Add HWPT-based nesting infrastructure support
PATCH9-10: Some cleanup work
PATCH11:   cap/ecap related compatibility check between vIOMMU and Host

IOMMU

Quoted 11 lines

PATCH12-19:Implement stage-1 page table for passthrough device
PATCH20: Enable stage-1 translation for passthrough device

Qemu code can be found at [3]

TODO:
- RAM discard
- dirty tracking on stage-2 page table

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/20210302203827.437645-

[email protected]/

Quoted 1 line

[3]

https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_rfcv2

Quoted 10 lines

Thanks
Zhenzhong

Changelog:
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of

iommufd/devid/ioas_id

Quoted 31 lines

- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace

Yi Liu (3):
intel_iommu: Replay pasid binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

Zhenzhong Duan (17):
backends/iommufd: Add helpers for invalidating user-managed HWPT vfio/iommufd: Add properties and handlers to
TYPE_HOST_IOMMU_DEVICE_IOMMUFD
HostIOMMUDevice: Introduce realize_late callback vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler vfio/iommufd: Implement [at|de]tach_hwpt handlers
host_iommu_device: Define two new capabilities
HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Add PASID cache management infrastructure
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode

Quoted 12 lines
  hw/i386/intel_iommu_internal.h     |   56 +
  include/hw/i386/intel_iommu.h      |   33 +-
  include/system/host_iommu_device.h |   40 +
  include/system/iommufd.h           |   53 +
  backends/iommufd.c                 |   58 +
  hw/i386/intel_iommu.c              | 1660 ++++++++++++++++++++++++----
  hw/vfio/common.c                   |   17 +-
  hw/vfio/iommufd.c                  |   48 +
  backends/trace-events              |    1 +
  hw/i386/trace-events               |   13 +
  10 files changed, 1776 insertions(+), 203 deletions(-)
Quoted 3 lines

Relative to the patches:
Patch 1: As Eric eluded to, a proper description for the patch should be written, and the title should change 'helpers' to 'helper'

This is addressed in [1]

[1] https://github.com/yiliu1765/qemu/commits/zhenzhong/iommufd_nesting_rfcv3.wip/

Quoted 5 lines

Patch 2:
(1) Introduce 'realize_late()' interface, but leave the reader wondering 'ah, why? what?' ... after reading farther down the series, I learn more about realize_late(), but more on that later...

realize_late() has been removed in [1]

Quoted 2 lines

(2) For my education, can you provide ptrs to VFIO & VPDA code paths that demonstrate the need for different [at|de]tach_<>_hwpt()

Google help me find this link https://lkml.iu.edu/2309.2/08079.html
specially https://gitlab.com/lulu6/gitlabqemutmp/-/commit/354870ff6bd9bac80c9fc5c7f944331cb24b0331

Quoted 9 lines

Patch 3: Why can't the realize() be moved to after attach? isn't realize() suppose to indicate 'all is setup and object can now be used' -- apologies for what could be a dumb question, as that's my understanding of realize(). If the argument is such that there needs to be two steps, how does the first realize() that put the object into a used state <somehow> wait until realize_late()?

Patch 4: Shouldn't the current/existing realize callback just be overwritten with the later one, when this is needed?

realize_late() has been removed in [1]

Quoted 6 lines

Patch 5: no issues.

Patch 6: ewww -- fs1gp ... we use underlines all over the place for multi-word elements; so how about 's1_1g_pg'
-- how many places is that really used that multiple underlines is an issue?

This is vtd specific, so to follow VTD spec's naming so that we can easily find its definition by searching fs1gp.

Quoted 10 lines

Patch 7: intel-iommu-specific callbacks in the common vfio & iommufd-backend code; nack. This won't compile w/intel-iommu included with iommufd... I think backend, intel-iommu hw-caps should provide the generic 'caps' boolean-type values/states; ... and maybe they should be extracted via vfio? .... like

     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
         return vfio_device_get_aw_bits(hiod->agent);

Patch 8: Again, VTD-specific code in IOMMUFD is a nack; again, maybe via vfio, or a direct call into an iommu-device-cap api.

Patch 7&8 addressed in [1]

Quoted 4 lines

Patch 9: no issues.

Patch 10: "except it's stale" likely "except when it's entry is stale" ?

addressed in [1]

Quoted 10 lines

Did you ever put some tracing in to capture avg hits in cache? ... if so, add
as a comment.
Otherwise, looks good.

Patch 11: Apologies, I don't know what 'flts' stands for, and why it is relative to 2- stage mapping, or SIOV. Could you add verbage to explain the use of it, as the rest of this patch doesn't make any sense to me without the background. The patch introduces hw-info-type (none or intel), and then goes on to add a large set of checks; seems like the caps & this checking should go together (split for each cap; put all caps together & the check...).

OK, will do. There are some explanations in cover-letter.
For history reason, old vtd spec define stage-1 as first level then switch to first stage.

Quoted 4 lines

Patch 12: Why isn't HostIOMMUDevice extended to have another iommu-specif
element, opaque in HostIOMMUDevice, but set to specific IOMMU in use? e.g.
void *hostiommustate;

Yes, that's possible, but we want to make a generic interface between VFIO/VDPA and vIOMMU.

Quoted 3 lines

Patch 13: Isn't PASID just an extension/addition of BDF id? and doesn't each PASID have its own address space?

Yes, it is.

Quoted 6 lines

So, why isn't it handle via a uniqe AS cache like 'any other device'? Maybe I'm thinking too SMMU-StreamID, which can be varying length, depending on subsystem support. I see what appears to be sid+pasid calls to do the AS lookups; hmm, so maybe this is the generalized BDF+pasid AS lookup? if so, maybe a better description stating this transition to a wider stream-id would set the code context better.

Not quite get..

Quoted 6 lines

As for the rest of the (400 intel-iommu) code, I'm not that in-depth in intel-iommu to determine if its all correct or not.

Patch 14: Define PGTT; the second paragraph seem self-contradicting -- it says it uses a 2-stage page table in each case, but it implies it should be different. At 580 lines of code changes, you win! ;-)

The host side's using nested or only stage-2 page table depends on PGTT's setting in guest.

Quoted 4 lines

Patch 15: Read-only and Read/write areas have different IOMMUFDs? is that an intel-iommu requriement?
At least this intel-iommu-errata code is only in hw/i386/<> modules.

No, if ERRATA_772415, read-only areas should not be mapped, so we allocate a new VTDIOASContainer to hold only read/write areas mapping. We can use same IOMMUFDs for different VTDIOASContainer.

Quoted 3 lines

Patch 16: Looks reasonable. What does the 'SI' mean after "CACHE_DEV", "CACHE_DOM" & "CACHE_PASID" ? -- stream-invalidation?

VTD_PASID_CACHE_DEVSI stands for 'pasid cache device selective invalidation', VTD_PASID_CACHE_DOMSI means 'pasid cache domain selective invalidation'.

Thanks
Zhenzhong

#65 lore raw

Hey Zhenzhong,
Thanks for feedback. replies below.
- Don

Quoted 63 lines · Zhenzhong

Hi Donald,

Quoted 61 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Zhenzhong,

Hi!
Eric asked me to review this series.
Since it's rather late since you posted will summarize review feedback below/bottom.

- Don

Quoted 46 lines · Zhenzhong Duan

Hi,

Per Jason Wang's suggestion, iommufd nesting series[1] is split into "Enable stage-1 translation for emulated device" series and "Enable stage-1 translation for passthrough device" series.

This series is 2nd part focusing on passthrough device. We don't do shadowing of guest page table for passthrough device but pass stage-1 page table to host side to construct a nested domain. There was some effort to enable this feature in old days, see [2] for details.

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

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

Where:
- FS = First stage page tables
- SS = Second stage page tables
<Intel VT-d Nested translation>

Quoted 6 lines
Quoted 3 lines

I'd prefer the use of 's1' for stage1/First stage, and 's2' for stage2/second stage. We don't need different terms for the same technology in the iommu/iommufd space(s).

OK, then I'd like to use stage1 and stage2 everywhere which is more verbose.

your choice; in other kernel & qemu code I've seen, 's1' and 's2' are used quite frequently, that's why I recommended it -- call it plagerizing! ;-)

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines

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

See below diagram:

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

Based on Yi's suggestion, this design is optimal in sharing ioas/hwpt whenever possible and create new one on demand, also supports multiple iommufd objects and ERRATA_772415.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

E.g., Stage-2 page table could be shared by different devices if there is no conflict and devices link to same iommufd object, i.e. devices under same host IOMMU can share same stage-2 page table. If there is

and 'devices under the same guest'.
Different guests cant be sharing the same stage-2 page tables.

Yes, will update.

Thanks.

Quoted 127 lines
Quoted 127 lines
Quoted 13 lines

conflict, i.e. there is one device under non cache coherency mode which is different from others, it requires a separate stage-2 page table in non-CC mode.

SPR platform has ERRATA_772415 which requires no readonly mappings in stage-2 page table. This series supports creating VTDIOASContainer with no readonly mappings. If there is a rare case that some IOMMUs on a multiple IOMMU host have ERRATA_772415 and others not, this design can still survive.

See below example diagram for a full view:

Quoted 13 lines
        IntelIOMMUState
               |
               V
      .------------------.    .------------------.    .-------------------.
      | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer  |--
...
      | (iommufd0,RW&RO) |    | (iommufd1,RW&RO) |    | (iommufd0,RW only)|
      .------------------.    .------------------.    .-------------------.
               |                       |                              |
               |                       .-->...                        |
               V                                                      V
        .-------------------.    .-------------------.          .---------------.
        |   VTDS2Hwpt(CC)   |--->| VTDS2Hwpt(non-CC) |-->...    | VTDS2Hwpt(CC) |-

->...

Quoted 10 lines

.-------------------. .-------------------. .---------------.
| | | |
| | | |
.-----------. .-----------. .------------. .------------.
| IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD |
| Device(CC)| | Device(CC)| | Device | | Device(CC) |
| (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) |
| | | | | (iommufd0) | | (iommufd0) |
.-----------. .-----------. .------------. .------------.

Quoted 7 lines

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

To enable stage-1 translation, only need to add "x-scalable-mode=on,x-flts=on". i.e. -device intel-iommu,x-scalable-mode=on,x-flts=on...

Passthrough device should use iommufd backend to work with stage-1

translation.

Quoted 5 lines

i.e. -object iommufd,id=iommufd0 -device vfio-pci,iommufd=iommufd0,...

If host doesn't support nested translation, qemu will fail with an unsupported report.

Quoted 8 lines

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

PATCH1-8:  Add HWPT-based nesting infrastructure support
PATCH9-10: Some cleanup work
PATCH11:   cap/ecap related compatibility check between vIOMMU and Host

IOMMU

Quoted 11 lines

PATCH12-19:Implement stage-1 page table for passthrough device
PATCH20: Enable stage-1 translation for passthrough device

Qemu code can be found at [3]

TODO:
- RAM discard
- dirty tracking on stage-2 page table

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://patchwork.kernel.org/project/kvm/cover/20210302203827.437645-

[email protected]/

Quoted 1 line

[3]

https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_rfcv2

Quoted 10 lines

Thanks
Zhenzhong

Changelog:
rfcv2:
- Drop VTDPASIDAddressSpace and use VTDAddressSpace (Eric, Liuyi)
- Move HWPT uAPI patches ahead(patch1-8) so arm nesting could easily rebase
- add two cleanup patches(patch9-10)
- VFIO passes iommufd/devid/hwpt_id to vIOMMU instead of

iommufd/devid/ioas_id

Quoted 43 lines

- add vtd_as_[from|to]_iommu_pasid() helper to translate between vtd_as and iommu pasid, this is important for dropping VTDPASIDAddressSpace

Yi Liu (3):
intel_iommu: Replay pasid binds after context cache invalidation
intel_iommu: Propagate PASID-based iotlb invalidation to host
intel_iommu: Refresh pasid bind when either SRTP or TE bit is changed

Zhenzhong Duan (17):
backends/iommufd: Add helpers for invalidating user-managed HWPT vfio/iommufd: Add properties and handlers to
TYPE_HOST_IOMMU_DEVICE_IOMMUFD
HostIOMMUDevice: Introduce realize_late callback vfio/iommufd: Implement HostIOMMUDeviceClass::realize_late() handler vfio/iommufd: Implement [at|de]tach_hwpt handlers
host_iommu_device: Define two new capabilities
HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_[NESTING|FS1GP]
iommufd: Implement query of HOST_IOMMU_DEVICE_CAP_ERRATA
intel_iommu: Rename vtd_ce_get_rid2pasid_entry to
vtd_ce_get_pasid_entry
intel_iommu: Optimize context entry cache utilization
intel_iommu: Check for compatibility with IOMMUFD backed device when
x-flts=on
intel_iommu: Introduce a new structure VTDHostIOMMUDevice
intel_iommu: Add PASID cache management infrastructure
intel_iommu: Bind/unbind guest page table to host
intel_iommu: ERRATA_772415 workaround
intel_iommu: Bypass replay in stage-1 page table mode
intel_iommu: Enable host device when x-flts=on in scalable mode

   hw/i386/intel_iommu_internal.h     |   56 +
   include/hw/i386/intel_iommu.h      |   33 +-
   include/system/host_iommu_device.h |   40 +
   include/system/iommufd.h           |   53 +
   backends/iommufd.c                 |   58 +
   hw/i386/intel_iommu.c              | 1660 ++++++++++++++++++++++++----
   hw/vfio/common.c                   |   17 +-
   hw/vfio/iommufd.c                  |   48 +
   backends/trace-events              |    1 +
   hw/i386/trace-events               |   13 +
   10 files changed, 1776 insertions(+), 203 deletions(-)
Quoted 8 lines
Quoted 3 lines

Relative to the patches:
Patch 1: As Eric eluded to, a proper description for the patch should be written, and the title should change 'helpers' to 'helper'

This is addressed in [1]

[1] https://github.com/yiliu1765/qemu/commits/zhenzhong/iommufd_nesting_rfcv3.wip/

ok.

Quoted 8 lines
Quoted 5 lines

Patch 2:
(1) Introduce 'realize_late()' interface, but leave the reader wondering 'ah, why? what?' ... after reading farther down the series, I learn more about realize_late(), but more on that later...

realize_late() has been removed in [1]

Yes, I saw. :)

Quoted 6 lines
Quoted 2 lines

(2) For my education, can you provide ptrs to VFIO & VPDA code paths that demonstrate the need for different [at|de]tach_<>_hwpt()

Google help me find this link https://lkml.iu.edu/2309.2/08079.html
specially https://gitlab.com/lulu6/gitlabqemutmp/-/commit/354870ff6bd9bac80c9fc5c7f944331cb24b0331

thanks for vdpa ptrs; I have my diff work cut out for me, but heh, I asked for it! :)

Quoted 7 lines
Quoted 7 lines

Patch 3: Why can't the realize() be moved to after attach? isn't realize() suppose to indicate 'all is setup and object can now be used' -- apologies for what could be a dumb question, as that's my understanding of realize(). If the argument is such that there needs to be two steps, how does the first realize() that put the object into a used state <somehow> wait until realize_late()?

Quoted 5 lines
Quoted 2 lines

Patch 4: Shouldn't the current/existing realize callback just be overwritten with the later one, when this is needed?

realize_late() has been removed in [1]

+1

Quoted 10 lines
Quoted 6 lines

Patch 5: no issues.

Patch 6: ewww -- fs1gp ... we use underlines all over the place for multi-word elements; so how about 's1_1g_pg'
-- how many places is that really used that multiple underlines is an issue?

This is vtd specific, so to follow VTD spec's naming so that we can easily find its definition by searching fs1gp.

Quoted 1 line

ok. You had mentioned earlier that you used VTD nomenclature, so I should have concluded the same here. /my bad.

Quoted 7 lines
Quoted 7 lines

Patch 7: intel-iommu-specific callbacks in the common vfio & iommufd-backend code; nack. This won't compile w/intel-iommu included with iommufd... I think backend, intel-iommu hw-caps should provide the generic 'caps' boolean-type values/states; ... and maybe they should be extracted via vfio? .... like

      case HOST_IOMMU_DEVICE_CAP_AW_BITS:
          return vfio_device_get_aw_bits(hiod->agent);
Quoted 5 lines
Quoted 2 lines

Patch 8: Again, VTD-specific code in IOMMUFD is a nack; again, maybe via vfio, or a direct call into an iommu-device-cap api.

Patch 7&8 addressed in [1]

+1

Quoted 7 lines
Quoted 4 lines

Patch 9: no issues.

Patch 10: "except it's stale" likely "except when it's entry is stale" ?

addressed in [1]

+1

Quoted 11 lines
Quoted 10 lines

Did you ever put some tracing in to capture avg hits in cache? ... if so, add
as a comment.
Otherwise, looks good.

Patch 11: Apologies, I don't know what 'flts' stands for, and why it is relative to 2- stage mapping, or SIOV. Could you add verbage to explain the use of it, as the rest of this patch doesn't make any sense to me without the background. The patch introduces hw-info-type (none or intel), and then goes on to add a large set of checks; seems like the caps & this checking should go together (split for each cap; put all caps together & the check...).

Quoted 3 lines

OK, will do. There are some explanations in cover-letter.
For history reason, old vtd spec define stage-1 as first level then switch to first stage.

So 'flts' is 'first level then switch' .

Quoted 7 lines
Quoted 4 lines

Patch 12: Why isn't HostIOMMUDevice extended to have another iommu-specif
element, opaque in HostIOMMUDevice, but set to specific IOMMU in use? e.g.
void *hostiommustate;

Yes, that's possible, but we want to make a generic interface between VFIO/VDPA and vIOMMU.

ok. I don't understand how VFIO & VPDA complicate that add.

Quoted 6 lines
Quoted 3 lines

Patch 13: Isn't PASID just an extension/addition of BDF id? and doesn't each PASID have its own address space?

Yes, it is.

Quoted 9 lines
Quoted 6 lines

So, why isn't it handle via a uniqe AS cache like 'any other device'? Maybe I'm thinking too SMMU-StreamID, which can be varying length, depending on subsystem support. I see what appears to be sid+pasid calls to do the AS lookups; hmm, so maybe this is the generalized BDF+pasid AS lookup? if so, maybe a better description stating this transition to a wider stream-id would set the code context better.

Not quite get..

I'm looking for a better description that states the AS cache lookup is broadened from bdf to bdf+pasid.

Quoted 9 lines
Quoted 6 lines

As for the rest of the (400 intel-iommu) code, I'm not that in-depth in intel-iommu to determine if its all correct or not.

Patch 14: Define PGTT; the second paragraph seem self-contradicting -- it says it uses a 2-stage page table in each case, but it implies it should be different. At 580 lines of code changes, you win! ;-)

The host side's using nested or only stage-2 page table depends on PGTT's setting in guest.

Thanks for clarification.

Quoted 8 lines
Quoted 4 lines

Patch 15: Read-only and Read/write areas have different IOMMUFDs? is that an intel-iommu requriement?
At least this intel-iommu-errata code is only in hw/i386/<> modules.

No, if ERRATA_772415, read-only areas should not be mapped, so we allocate a new VTDIOASContainer to hold only read/write areas mapping. We can use same IOMMUFDs for different VTDIOASContainer.

ah yes; I got hung-up on different mappings, and didn't back up to AS-container split & same IOMMUFD.

Quoted 7 lines
Quoted 3 lines

Patch 16: Looks reasonable. What does the 'SI' mean after "CACHE_DEV", "CACHE_DOM" & "CACHE_PASID" ? -- stream-invalidation?

VTD_PASID_CACHE_DEVSI stands for 'pasid cache device selective invalidation', VTD_PASID_CACHE_DOMSI means 'pasid cache domain selective invalidation'.

That explanation helps. :) maybe put a short blurb in the commit log, or code, so one doesn't have to be a ninja-VTD spec consumer to comprehend those (important) diffs.

Quoted 3 lines

Thanks
Zhenzhong

Again, thanks for the reply.
Looking fwd to the rfcv3 (on list) or move to v1-POST.
- Don

#66 lore raw
Quoted 29 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Hey Zhenzhong,
Thanks for feedback. replies below.
- Don

Quoted 17 lines · Zhenzhong

Hi Donald,

Quoted 15 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Zhenzhong,

Hi!
Eric asked me to review this series.
Since it's rather late since you posted will summarize review feedback below/bottom.

- Don

On 2/19/25 3:22 AM, Zhenzhong Duan wrote:

...

Quoted 11 lines
Quoted 1 line
Quoted 1 line

Did you ever put some tracing in to capture avg hits in cache? ... if so,

add

Quoted 4 lines
Quoted 4 lines

as a comment.
Otherwise, looks good.

Patch 11: Apologies, I don't know what 'flts' stands for, and why it is relative

to 2-

Quoted 4 lines
Quoted 4 lines

stage mapping, or SIOV. Could you add verbage to explain the use of it, as the rest of this patch doesn't make any sense to me without the background. The patch introduces hw-info-type (none or intel), and then goes on to add a large set of checks; seems like the caps & this checking should go together

Quoted 8 lines

(split

Quoted 4 lines
Quoted 1 line

for each cap; put all caps together & the check...).

OK, will do. There are some explanations in cover-letter.
For history reason, old vtd spec define stage-1 as first level then switch to first

stage.

Quoted 1 line

So 'flts' is 'first level then switch' .

Sorry for confusion, it stands for 'first level translation support'.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

Patch 12: Why isn't HostIOMMUDevice extended to have another iommu-

specif

Quoted 8 lines
Quoted 1 line
Quoted 1 line

element, opaque in HostIOMMUDevice, but set to specific IOMMU in use?

e.g.

Quoted 3 lines
Quoted 1 line

void *hostiommustate;

Yes, that's possible, but we want to make a generic interface between

VFIO/VDPA and vIOMMU.

Quoted 1 line

ok. I don't understand how VFIO & VPDA complicate that add.

IIUC, the hostiommustate provided by VFIO and VDPA may be different format. By using a general interface like .get_cap(), we hide the resolving under VFIO and VDPA backend. This is like the KVM extension checking between QEMU and KVM.

FYI, there was some discuss on the interface before,
see https://lists.gnu.org/archive/html/qemu-devel/2024-04/msg02658.html

Quoted 13 lines
Quoted 10 lines
Quoted 4 lines

Patch 13: Isn't PASID just an extension/addition of BDF id? and doesn't each PASID have its own address space?

Yes, it is.

Quoted 3 lines

So, why isn't it handle via a uniqe AS cache like 'any other device'? Maybe I'm thinking too SMMU-StreamID, which can be varying length, depending on subsystem support. I see what appears to be sid+pasid calls to do the AS

lookups;

Quoted 2 lines
Quoted 2 lines

hmm, so maybe this is the generalized BDF+pasid AS lookup? if so, maybe a better description stating this transition to a wider stream-id would set the

Quoted 8 lines

code

Quoted 4 lines
Quoted 1 line

context better.

Not quite get..

I'm looking for a better description that states the AS cache lookup is broadened from bdf
to bdf+pasid.

Guess you mean vtd_as_from_iommu_pasid(), it's a variant of vtd_find_add_as(). We support AS cache lookup by bdf+pasid for a long time, see vtd_find_add_as().

Quoted 33 lines
Quoted 2 lines
Quoted 2 lines

As for the rest of the (400 intel-iommu) code, I'm not that in-depth in intel-

iommu

Quoted 3 lines
Quoted 3 lines

to determine if its all correct or not.

Patch 14: Define PGTT; the second paragraph seem self-contradicting -- it says

it

Quoted 1 line
Quoted 1 line

uses a 2-stage page table in each case, but it implies it should be different. At

580

Quoted 3 lines
Quoted 1 line

lines of code changes, you win! ;-)

The host side's using nested or only stage-2 page table depends on PGTT's

setting in guest.

Quoted 1 line

Thanks for clarification.

Quoted 2 lines
Quoted 2 lines

Patch 15: Read-only and Read/write areas have different IOMMUFDs? is that

an

Quoted 4 lines
Quoted 2 lines

intel-iommu requriement?
At least this intel-iommu-errata code is only in hw/i386/<> modules.

No, if ERRATA_772415, read-only areas should not be mapped, so we allocate a

new VTDIOASContainer to hold only read/write areas mapping.

Quoted 2 lines

We can use same IOMMUFDs for different VTDIOASContainer.

ah yes; I got hung-up on different mappings, and didn't back up to AS-container split & same IOMMUFD.

Quoted 4 lines
Quoted 3 lines

Patch 16: Looks reasonable. What does the 'SI' mean after "CACHE_DEV", "CACHE_DOM" & "CACHE_PASID" ? -- stream-invalidation?

Quoted 6 lines
Quoted 3 lines

VTD_PASID_CACHE_DEVSI stands for 'pasid cache device selective invalidation', VTD_PASID_CACHE_DOMSI means 'pasid cache domain selective invalidation'.

That explanation helps. :) maybe put a short blurb in the commit log, or code, so one doesn't have to be a ninja-VTD spec consumer to comprehend those (important) diffs.

Good idea, will do.

Quoted 6 lines
Quoted 4 lines

Thanks
Zhenzhong

Again, thanks for the reply.
Looking fwd to the rfcv3 (on list) or move to v1-POST.

Thanks for your comments!

BRs,
Zhenzhong

#67 lore raw
Quoted 45 lines · Zhenzhong
Quoted 29 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Hey Zhenzhong,
Thanks for feedback. replies below.
- Don

Quoted 17 lines · Zhenzhong

Hi Donald,

Quoted 15 lines

-----Original Message-----
From: Donald Dutile <[email protected]>
Subject: Re: [PATCH rfcv2 00/20] intel_iommu: Enable stage-1 translation for passthrough device

Zhenzhong,

Hi!
Eric asked me to review this series.
Since it's rather late since you posted will summarize review feedback below/bottom.

- Don

On 2/19/25 3:22 AM, Zhenzhong Duan wrote:

...

Quoted 14 lines
Quoted 1 line
Quoted 1 line

Did you ever put some tracing in to capture avg hits in cache? ... if so,

add

Quoted 4 lines
Quoted 4 lines

as a comment.
Otherwise, looks good.

Patch 11: Apologies, I don't know what 'flts' stands for, and why it is relative

to 2-

Quoted 4 lines
Quoted 4 lines

stage mapping, or SIOV. Could you add verbage to explain the use of it, as the rest of this patch doesn't make any sense to me without the background. The patch introduces hw-info-type (none or intel), and then goes on to add a large set of checks; seems like the caps & this checking should go together

(split

Quoted 2 lines
Quoted 1 line

for each cap; put all caps together & the check...).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

OK, will do. There are some explanations in cover-letter.
For history reason, old vtd spec define stage-1 as first level then switch to first

stage.

Quoted 1 line

So 'flts' is 'first level then switch' .

Sorry for confusion, it stands for 'first level translation support'.

Thanks.

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
Quoted 3 lines

Patch 12: Why isn't HostIOMMUDevice extended to have another iommu-

specif

Quoted 1 line
Quoted 1 line

element, opaque in HostIOMMUDevice, but set to specific IOMMU in use?

e.g.

Quoted 3 lines
Quoted 1 line

void *hostiommustate;

Yes, that's possible, but we want to make a generic interface between

VFIO/VDPA and vIOMMU.

Quoted 1 line
Quoted 9 lines
Quoted 1 line

ok. I don't understand how VFIO & VPDA complicate that add.

IIUC, the hostiommustate provided by VFIO and VDPA may be different format. By using a general interface like .get_cap(), we hide the resolving under VFIO and VDPA backend. This is like the KVM extension checking between QEMU and KVM.

FYI, there was some discuss on the interface before,
see https://lists.gnu.org/archive/html/qemu-devel/2024-04/msg02658.html

Good analogy, thanks. I'll reach out to Cedric on the above discussion as well.

Quoted 16 lines
Quoted 16 lines
Quoted 10 lines
Quoted 4 lines

Patch 13: Isn't PASID just an extension/addition of BDF id? and doesn't each PASID have its own address space?

Yes, it is.

Quoted 3 lines

So, why isn't it handle via a uniqe AS cache like 'any other device'? Maybe I'm thinking too SMMU-StreamID, which can be varying length, depending on subsystem support. I see what appears to be sid+pasid calls to do the AS

lookups;

Quoted 2 lines
Quoted 2 lines

hmm, so maybe this is the generalized BDF+pasid AS lookup? if so, maybe a better description stating this transition to a wider stream-id would set the

code

Quoted 2 lines
Quoted 1 line

context better.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Not quite get..

I'm looking for a better description that states the AS cache lookup is broadened from bdf
to bdf+pasid.

Guess you mean vtd_as_from_iommu_pasid(), it's a variant of vtd_find_add_as(). We support AS cache lookup by bdf+pasid for a long time, see vtd_find_add_as().

Thanks for clarification.

Quoted 46 lines
Quoted 33 lines
Quoted 2 lines
Quoted 2 lines

As for the rest of the (400 intel-iommu) code, I'm not that in-depth in intel-

iommu

Quoted 3 lines
Quoted 3 lines

to determine if its all correct or not.

Patch 14: Define PGTT; the second paragraph seem self-contradicting -- it says

it

Quoted 1 line
Quoted 1 line

uses a 2-stage page table in each case, but it implies it should be different. At

580

Quoted 3 lines
Quoted 1 line

lines of code changes, you win! ;-)

The host side's using nested or only stage-2 page table depends on PGTT's

setting in guest.

Quoted 1 line

Thanks for clarification.

Quoted 2 lines
Quoted 2 lines

Patch 15: Read-only and Read/write areas have different IOMMUFDs? is that

an

Quoted 4 lines
Quoted 2 lines

intel-iommu requriement?
At least this intel-iommu-errata code is only in hw/i386/<> modules.

No, if ERRATA_772415, read-only areas should not be mapped, so we allocate a

new VTDIOASContainer to hold only read/write areas mapping.

Quoted 2 lines

We can use same IOMMUFDs for different VTDIOASContainer.

ah yes; I got hung-up on different mappings, and didn't back up to AS-container split & same IOMMUFD.

Quoted 4 lines
Quoted 3 lines

Patch 16: Looks reasonable. What does the 'SI' mean after "CACHE_DEV", "CACHE_DOM" & "CACHE_PASID" ? -- stream-invalidation?

Quoted 6 lines
Quoted 3 lines

VTD_PASID_CACHE_DEVSI stands for 'pasid cache device selective invalidation', VTD_PASID_CACHE_DOMSI means 'pasid cache domain selective invalidation'.

That explanation helps. :) maybe put a short blurb in the commit log, or code, so one doesn't have to be a ninja-VTD spec consumer to comprehend those (important) diffs.

Good idea, will do.

Quoted 4 lines
Quoted 4 lines

Thanks
Zhenzhong

Quoted 8 lines
Quoted 2 lines

Again, thanks for the reply.
Looking fwd to the rfcv3 (on list) or move to v1-POST.

Thanks for your comments!

BRs,
Zhenzhong

Thanks for the added clarifications.
- Don