Skip to contentSkip to conversation

intel_iommu: Enable stage-1 translation for emulated device

PATCH Superseded Zhenzhong Duan 4 participants 50 messages Jul 18, 2024 – Jul 29, 2024 Arm/Amlogic Meson SoC support

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

cover intel_iommu: Enable stage-1 translation for emulated 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 enables stage-1 translation support for emulated device in intel iommu which we called "modern" mode.

PATCH1-5:  Some preparing work before support stage-1 translation
PATCH6-8:  Implement stage-1 translation for emulated device
PATCH9-14: Emulate iotlb invalidation of stage-1 mapping
PATCH15:   Set default aw_bits to 48 in scalable modern mode
PATCH16:   Introduce "modern" mode to distinguish with legacy mode
PATCH17:   Add qtest

Note in spec revision 3.4, it renames "First-level" to "First-stage", "Second-level" to "Second-stage". But the scalable mode was added before that change. So we keep old favor using First-level/fl/Second-level/sl in code but change to use stage-1/stage-2 in commit log.
But keep in mind First-level/fl/stage-1 all have same meaning, same for Second-level/sl/stage-2.

Qemu code can be found at [2]

[1] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02740.html
[2] https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_stage1_emu_v1

Thanks
Zhenzhong

Changelog:
v1:
- define VTD_HOST_AW_AUTO (Clement)
- passing pgtt as a parameter to vtd_update_iotlb (Clement)
- prefix sl_/fl_ to second/first level specific functions (Clement)
- pick reserved bit check from Clement, add his Co-developed-by
- Update test without using libqtest-single.h (Thomas)

rfcv2:
- split from nesting series (Jason)
- merged some commits from Clement
- add qtest (jason)

Clément Mathieu--Drif (5):
intel_iommu: Check if the input address is canonical
intel_iommu: Set accessed and dirty bits during first stage
translation
intel_iommu: Extract device IOTLB invalidation logic
intel_iommu: Add an internal API to find an address space with PASID
intel_iommu: Add support for PASID-based device IOTLB invalidation

Yi Liu (3):
intel_iommu: Rename slpte to pte
intel_iommu: Implement stage-1 translation
intel_iommu: Modify x-scalable-mode to be string option

Yu Zhang (1):
intel_iommu: Use the latest fault reasons defined by spec

Zhenzhong Duan (8):
intel_iommu: Make pasid entry type check accurate
intel_iommu: Add a placeholder variable for scalable modern mode
intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb
invalidation
intel_iommu: Flush stage-1 cache in iotlb invalidation
intel_iommu: Process PASID-based iotlb invalidation
intel_iommu: piotlb invalidation should notify unmap
intel_iommu: Set default aw_bits to 48 in scalable modren mode tests/qtest: Add intel-iommu test

 MAINTAINERS                    |   1 +
 hw/i386/intel_iommu_internal.h |  90 +++-
 include/hw/i386/intel_iommu.h  |   8 +-
 hw/i386/intel_iommu.c          | 742 +++++++++++++++++++++++++++------
 tests/qtest/intel-iommu-test.c |  71 ++++
 tests/qtest/meson.build        |   1 +
 6 files changed, 764 insertions(+), 149 deletions(-)
 create mode 100644 tests/qtest/intel-iommu-test.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/17 intel_iommu: Use the latest fault reasons defined by spec

From: Yu Zhang <[email protected]>

Spec revision 3.0 or above defines more detailed fault reasons for scalable mode. So introduce them into emulation code, see spec section 7.1.2 for details.

Note spec revision has no relation with VERSION register, Guest kernel should not use that register to judge what features are supported. Instead cap/ecap bits should be checked.

---
 hw/i386/intel_iommu_internal.h |  9 ++++++++-
 hw/i386/intel_iommu.c          | 25 ++++++++++++++++---------
 2 files changed, 24 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +8 -1
index f8cf99bddf..c0ca7b372f 100644
@@ -311,7 +311,14 @@ typedef enum VTDFaultReason {
                                   * request while disabled */
     VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
-    VTD_FR_PASID_TABLE_INV = 0x58,  /*Invalid PASID table entry */
+    /* PASID directory entry access failure */
+    VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
+    /* The Present(P) field of pasid directory entry is 0 */
+    VTD_FR_PASID_DIR_ENTRY_P = 0x51,
+    VTD_FR_PASID_TABLE_ACCESS_ERR = 0x58, /* PASID table entry access failure */
+    /* The Present(P) field of pasid table entry is 0 */
+    VTD_FR_PASID_ENTRY_P = 0x59,
+    VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
 
     /* Output address in the interrupt address range for scalable mode */
     VTD_FR_SM_INTERRUPT_ADDR = 0x87,
hw/i386/intel_iommu.c +16 -9
index 37c21a0aec..e65f5b29a5 100644
@@ -796,7 +796,7 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
     addr = pasid_dir_base + index * entry_size;
     if (dma_memory_read(&address_space_memory, addr,
                         pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ACCESS_ERR;
     }
 
     pdire->val = le64_to_cpu(pdire->val);
@@ -814,6 +814,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
                                           dma_addr_t addr,
                                           VTDPASIDEntry *pe)
 {
+    uint8_t pgtt;
     uint32_t index;
     dma_addr_t entry_size;
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
@@ -823,7 +824,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
     addr = addr + index * entry_size;
     if (dma_memory_read(&address_space_memory, addr,
                         pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ACCESS_ERR;
     }
     for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
         pe->val[i] = le64_to_cpu(pe->val[i]);
@@ -831,11 +832,13 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
 
     /* Do translation type check */
     if (!vtd_pe_type_check(x86_iommu, pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ENTRY_INV;
     }
 
-    if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
-        return -VTD_FR_PASID_TABLE_INV;
+    pgtt = VTD_PE_GET_TYPE(pe);
+    if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
+        !vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
+            return -VTD_FR_PASID_TABLE_ENTRY_INV;
     }
 
     return 0;
@@ -876,7 +879,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
     }
 
     if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
     }
 
     ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
@@ -885,7 +888,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
     }
 
     if (!vtd_pe_present(pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_ENTRY_P;
     }
 
     return 0;
@@ -938,7 +941,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
     }
 
     if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
     }
 
     /*
@@ -1795,7 +1798,11 @@ static const bool vtd_qualified_faults[] = {
     [VTD_FR_ROOT_ENTRY_RSVD] = false,
     [VTD_FR_PAGING_ENTRY_RSVD] = true,
     [VTD_FR_CONTEXT_ENTRY_TT] = true,
-    [VTD_FR_PASID_TABLE_INV] = false,
+    [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
+    [VTD_FR_PASID_DIR_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
+    [VTD_FR_PASID_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
     [VTD_FR_SM_INTERRUPT_ADDR] = true,
     [VTD_FR_MAX] = false,
 };
Signature · 1 line
2.34.1
Reviewed-by on v1 1/17
#2 lore raw

Re: [PATCH v1 01/17] intel_iommu: Use the latest fault reasons defined by spec

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

From: Yu Zhang <[email protected]>

Spec revision 3.0 or above defines more detailed fault reasons for scalable mode. So introduce them into emulation code, see spec section 7.1.2 for details.

Note spec revision has no relation with VERSION register, Guest kernel should not use that register to judge what features are supported. Instead cap/ecap bits should be checked.

---
  hw/i386/intel_iommu_internal.h |  9 ++++++++-
  hw/i386/intel_iommu.c          | 25 ++++++++++++++++---------
  2 files changed, 24 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +8 -1
index f8cf99bddf..c0ca7b372f 100644
@@ -311,7 +311,14 @@ typedef enum VTDFaultReason {
                                    * request while disabled */
      VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
-    VTD_FR_PASID_TABLE_INV = 0x58,  /*Invalid PASID table entry */
+    /* PASID directory entry access failure */
+    VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
+    /* The Present(P) field of pasid directory entry is 0 */
+    VTD_FR_PASID_DIR_ENTRY_P = 0x51,
+    VTD_FR_PASID_TABLE_ACCESS_ERR = 0x58, /* PASID table entry access failure */
+    /* The Present(P) field of pasid table entry is 0 */
+    VTD_FR_PASID_ENTRY_P = 0x59,
+    VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
 
      /* Output address in the interrupt address range for scalable mode */
      VTD_FR_SM_INTERRUPT_ADDR = 0x87,
hw/i386/intel_iommu.c +16 -10
index 37c21a0aec..e65f5b29a5 100644
@@ -796,7 +796,7 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
      addr = pasid_dir_base + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ACCESS_ERR;
      }
 
      pdire->val = le64_to_cpu(pdire->val);
@@ -814,6 +814,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
                                            dma_addr_t addr,
                                            VTDPASIDEntry *pe)
  {
+    uint8_t pgtt;
      uint32_t index;
      dma_addr_t entry_size;
      X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
@@ -823,7 +824,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      addr = addr + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ACCESS_ERR;
      }
      for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
          pe->val[i] = le64_to_cpu(pe->val[i]);
@@ -831,11 +832,13 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
 
      /* Do translation type check */
      if (!vtd_pe_type_check(x86_iommu, pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
-    if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
-        return -VTD_FR_PASID_TABLE_INV;
+    pgtt = VTD_PE_GET_TYPE(pe);
+    if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
+        !vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
+            return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
      return 0;
@@ -876,7 +879,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
 
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
 
      ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
@@ -885,7 +888,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
 
      if (!vtd_pe_present(pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_ENTRY_P;
      }
 
      return 0;
@@ -938,7 +941,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
      }
 
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
 
      /*
@@ -1795,7 +1798,11 @@ static const bool vtd_qualified_faults[] = {
      [VTD_FR_ROOT_ENTRY_RSVD] = false,
      [VTD_FR_PAGING_ENTRY_RSVD] = true,
      [VTD_FR_CONTEXT_ENTRY_TT] = true,
-    [VTD_FR_PASID_TABLE_INV] = false,
+    [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
+    [VTD_FR_PASID_DIR_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
+    [VTD_FR_PASID_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
      [VTD_FR_SM_INTERRUPT_ADDR] = true,
      [VTD_FR_MAX] = false,
  };
--

2.34.1

#3 lore raw

Re: [PATCH v1 01/17] intel_iommu: Use the latest fault reasons defined by spec

In reply to Zhenzhong Duan
Quoted 114 lines · Zhenzhong Duan

From: Yu Zhang <[email protected]>

Spec revision 3.0 or above defines more detailed fault reasons for scalable mode. So introduce them into emulation code, see spec section 7.1.2 for details.

Note spec revision has no relation with VERSION register, Guest kernel should not use that register to judge what features are supported. Instead cap/ecap bits should be checked.

---
  hw/i386/intel_iommu_internal.h |  9 ++++++++-
  hw/i386/intel_iommu.c          | 25 ++++++++++++++++---------
  2 files changed, 24 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +8 -1
index f8cf99bddf..c0ca7b372f 100644
@@ -311,7 +311,14 @@ typedef enum VTDFaultReason {
                                    * request while disabled */
      VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
 
-    VTD_FR_PASID_TABLE_INV = 0x58,  /*Invalid PASID table entry */
+    /* PASID directory entry access failure */
+    VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
+    /* The Present(P) field of pasid directory entry is 0 */
+    VTD_FR_PASID_DIR_ENTRY_P = 0x51,
+    VTD_FR_PASID_TABLE_ACCESS_ERR = 0x58, /* PASID table entry access failure */
+    /* The Present(P) field of pasid table entry is 0 */
+    VTD_FR_PASID_ENTRY_P = 0x59,
+    VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
 
      /* Output address in the interrupt address range for scalable mode */
      VTD_FR_SM_INTERRUPT_ADDR = 0x87,
hw/i386/intel_iommu.c +11 -8
index 37c21a0aec..e65f5b29a5 100644
@@ -796,7 +796,7 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
      addr = pasid_dir_base + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ACCESS_ERR;
      }
 
      pdire->val = le64_to_cpu(pdire->val);
@@ -814,6 +814,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
                                            dma_addr_t addr,
                                            VTDPASIDEntry *pe)
  {
+    uint8_t pgtt;
      uint32_t index;
      dma_addr_t entry_size;
      X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
@@ -823,7 +824,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      addr = addr + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ACCESS_ERR;
      }
      for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
          pe->val[i] = le64_to_cpu(pe->val[i]);
@@ -831,11 +832,13 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
 
      /* Do translation type check */
      if (!vtd_pe_type_check(x86_iommu, pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
-    if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
-        return -VTD_FR_PASID_TABLE_INV;
+    pgtt = VTD_PE_GET_TYPE(pe);
+    if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
+        !vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
+            return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
      return 0;
@@ -876,7 +879,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
 
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
 
      ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
@@ -885,7 +888,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
 
      if (!vtd_pe_present(pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_ENTRY_P;
      }
 
      return 0;
@@ -938,7 +941,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
      }
 
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
 
      /*
@@ -1795,7 +1798,11 @@ static const bool vtd_qualified_faults[] = {
      [VTD_FR_ROOT_ENTRY_RSVD] = false,
      [VTD_FR_PAGING_ENTRY_RSVD] = true,

hw/i386/intel_iommu.c static const bool vtd_qualified_faults[] = { · line 1808

Quoted 10 lines
hw/i386/intel_iommu.c +5 -1
      [VTD_FR_CONTEXT_ENTRY_TT] = true,
-    [VTD_FR_PASID_TABLE_INV] = false,
+    [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
+    [VTD_FR_PASID_DIR_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
+    [VTD_FR_PASID_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
      [VTD_FR_SM_INTERRUPT_ADDR] = true,
      [VTD_FR_MAX] = false,
  };

@Jason, @Michael,

Do you know the rule of setting this table? I noticed it was introduced since day-1[1]. I didn't see any history discussion on it on lore. So not quite sure about the purpose of it. Per the usage of this table, it is used as a filter when the iommu driver has set the FPD bit. If FPD is set, some errors need not to trigger a trace which is mostly for debug purpose.

I noticed Peter had asked it as well[2]. But I don't think it was clarified clearly. May we have a clarification for it here? BTW. I didn't see VT-d spec has any definition on it. So it should just be a software trick. :)

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/20190301065219.GA22229@xz-x1/

Signature · 2 lines
Regards,
Yi Liu
#4 lore raw

Re: [PATCH v1 01/17] intel_iommu: Use the latest fault reasons defined by spec

Quoted 122 lines · Yi Liu
Quoted 102 lines · Zhenzhong Duan

From: Yu Zhang <[email protected]>

Spec revision 3.0 or above defines more detailed fault reasons for scalable mode. So introduce them into emulation code, see spec section 7.1.2 for details.

Note spec revision has no relation with VERSION register, Guest kernel should not use that register to judge what features are supported. Instead cap/ecap bits should be checked.

---
  hw/i386/intel_iommu_internal.h |  9 ++++++++-
  hw/i386/intel_iommu.c          | 25 ++++++++++++++++---------
  2 files changed, 24 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +8 -1
index f8cf99bddf..c0ca7b372f 100644
@@ -311,7 +311,14 @@ typedef enum VTDFaultReason {
                                    * request while disabled */
      VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
-    VTD_FR_PASID_TABLE_INV = 0x58,  /*Invalid PASID table entry */
+    /* PASID directory entry access failure */
+    VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
+    /* The Present(P) field of pasid directory entry is 0 */
+    VTD_FR_PASID_DIR_ENTRY_P = 0x51,
+    VTD_FR_PASID_TABLE_ACCESS_ERR = 0x58, /* PASID table entry access failure */
+    /* The Present(P) field of pasid table entry is 0 */
+    VTD_FR_PASID_ENTRY_P = 0x59,
+    VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
      /* Output address in the interrupt address range for scalable mode */
      VTD_FR_SM_INTERRUPT_ADDR = 0x87,
hw/i386/intel_iommu.c +11 -8
index 37c21a0aec..e65f5b29a5 100644
@@ -796,7 +796,7 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
      addr = pasid_dir_base + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ACCESS_ERR;
      }
      pdire->val = le64_to_cpu(pdire->val);
@@ -814,6 +814,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
                                            dma_addr_t addr,
                                            VTDPASIDEntry *pe)
  {
+    uint8_t pgtt;
      uint32_t index;
      dma_addr_t entry_size;
      X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
@@ -823,7 +824,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      addr = addr + index * entry_size;
      if (dma_memory_read(&address_space_memory, addr,
                          pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ACCESS_ERR;
      }
      for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
          pe->val[i] = le64_to_cpu(pe->val[i]);
@@ -831,11 +832,13 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      /* Do translation type check */
      if (!vtd_pe_type_check(x86_iommu, pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
-    if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
-        return -VTD_FR_PASID_TABLE_INV;
+    pgtt = VTD_PE_GET_TYPE(pe);
+    if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
+        !vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
+            return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
      return 0;
@@ -876,7 +879,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
      ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
@@ -885,7 +888,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
      }
      if (!vtd_pe_present(pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_ENTRY_P;
      }
      return 0;
@@ -938,7 +941,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
      }
      if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
      }
      /*
@@ -1795,7 +1798,11 @@ static const bool vtd_qualified_faults[] = {
      [VTD_FR_ROOT_ENTRY_RSVD] = false,
      [VTD_FR_PAGING_ENTRY_RSVD] = true,

hw/i386/intel_iommu.c static const bool vtd_qualified_faults[] = { · line 1808

Quoted 10 lines
hw/i386/intel_iommu.c +5 -1
      [VTD_FR_CONTEXT_ENTRY_TT] = true,
-    [VTD_FR_PASID_TABLE_INV] = false,
+    [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
+    [VTD_FR_PASID_DIR_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
+    [VTD_FR_PASID_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
      [VTD_FR_SM_INTERRUPT_ADDR] = true,
      [VTD_FR_MAX] = false,
  };

@Jason, @Michael,

Do you know the rule of setting this table? I noticed it was introduced since day-1[1]. I didn't see any history discussion on it on lore. So not quite sure about the purpose of it. Per the usage of this table, it is used as a filter when the iommu driver has set the FPD bit. If FPD is set, some errors need not to trigger a trace which is mostly for debug purpose.

Quoted 10 lines

I noticed Peter had asked it as well[2]. But I don't think it was clarified clearly. May we have a clarification for it here? BTW. I didn't see VT-d spec has any definition on it. So it should just be a software trick. :)

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/20190301065219.GA22229@xz-x1/

Signature · 2 lines
Regards,
Yi Liu

Are you asking for a definition of qualified fault conditions?

7.1.1 Non-Recoverable Address Translation Faults
Non-recoverable address translation faults can be detected by remapping hardware for many different kinds of requests as shown by Table 26. A non-recoverable fault condition is considered “qualified” if software can suppress reporting of the fault by setting one of the Fault Processing Disable (FPD) bits available in one or more of the address translation structures (i.e., the context-entry, scalable-mode context-entry, scalable-mode PASID-directory entry, scalable-mode PASID-table entry). For a request that encounters a “qualified” non-recoverable fault condition, if the remapping hardware encountered any translation structure entry with an FPD field value of 1, the remapping hardware must not report the fault to software. For example, when processing a request that encounters an FPD field with a value of 1 in the scalable-mode context-entry and encounters any “qualified” fault such as SCT.*, SPD.*, SPT.*, SFS.*, SSS.*, or SGN.*, the remapping hardware will not report the fault to software. Memory requests that result in non-recoverable address translation faults are blocked by hardware

Signature · 1 line
MST
#5 lore raw

Re: [PATCH v1 01/17] intel_iommu: Use the latest fault reasons defined by spec

Quoted 137 lines · Michael S. Tsirkin
Quoted 122 lines · Yi Liu
Quoted 102 lines · Zhenzhong Duan

From: Yu Zhang <[email protected]>

Spec revision 3.0 or above defines more detailed fault reasons for scalable mode. So introduce them into emulation code, see spec section 7.1.2 for details.

Note spec revision has no relation with VERSION register, Guest kernel should not use that register to judge what features are supported. Instead cap/ecap bits should be checked.

---
   hw/i386/intel_iommu_internal.h |  9 ++++++++-
   hw/i386/intel_iommu.c          | 25 ++++++++++++++++---------
   2 files changed, 24 insertions(+), 10 deletions(-)
hw/i386/intel_iommu_internal.h +8 -1
index f8cf99bddf..c0ca7b372f 100644
@@ -311,7 +311,14 @@ typedef enum VTDFaultReason {
                                     * request while disabled */
       VTD_FR_IR_SID_ERR = 0x26,   /* Invalid Source-ID */
-    VTD_FR_PASID_TABLE_INV = 0x58,  /*Invalid PASID table entry */
+    /* PASID directory entry access failure */
+    VTD_FR_PASID_DIR_ACCESS_ERR = 0x50,
+    /* The Present(P) field of pasid directory entry is 0 */
+    VTD_FR_PASID_DIR_ENTRY_P = 0x51,
+    VTD_FR_PASID_TABLE_ACCESS_ERR = 0x58, /* PASID table entry access failure */
+    /* The Present(P) field of pasid table entry is 0 */
+    VTD_FR_PASID_ENTRY_P = 0x59,
+    VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
       /* Output address in the interrupt address range for scalable mode */
       VTD_FR_SM_INTERRUPT_ADDR = 0x87,
hw/i386/intel_iommu.c +11 -8
index 37c21a0aec..e65f5b29a5 100644
@@ -796,7 +796,7 @@ static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
       addr = pasid_dir_base + index * entry_size;
       if (dma_memory_read(&address_space_memory, addr,
                           pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ACCESS_ERR;
       }
       pdire->val = le64_to_cpu(pdire->val);
@@ -814,6 +814,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
                                             dma_addr_t addr,
                                             VTDPASIDEntry *pe)
   {
+    uint8_t pgtt;
       uint32_t index;
       dma_addr_t entry_size;
       X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
@@ -823,7 +824,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
       addr = addr + index * entry_size;
       if (dma_memory_read(&address_space_memory, addr,
                           pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ACCESS_ERR;
       }
       for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
           pe->val[i] = le64_to_cpu(pe->val[i]);
@@ -831,11 +832,13 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
       /* Do translation type check */
       if (!vtd_pe_type_check(x86_iommu, pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_TABLE_ENTRY_INV;
       }
-    if (!vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
-        return -VTD_FR_PASID_TABLE_INV;
+    pgtt = VTD_PE_GET_TYPE(pe);
+    if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
+        !vtd_is_level_supported(s, VTD_PE_GET_LEVEL(pe))) {
+            return -VTD_FR_PASID_TABLE_ENTRY_INV;
       }
       return 0;
@@ -876,7 +879,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
       }
       if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
       }
       ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
@@ -885,7 +888,7 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
       }
       if (!vtd_pe_present(pe)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_ENTRY_P;
       }
       return 0;
@@ -938,7 +941,7 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
       }
       if (!vtd_pdire_present(&pdire)) {
-        return -VTD_FR_PASID_TABLE_INV;
+        return -VTD_FR_PASID_DIR_ENTRY_P;
       }
       /*
@@ -1795,7 +1798,11 @@ static const bool vtd_qualified_faults[] = {
       [VTD_FR_ROOT_ENTRY_RSVD] = false,
       [VTD_FR_PAGING_ENTRY_RSVD] = true,

hw/i386/intel_iommu.c static const bool vtd_qualified_faults[] = { · line 1808

Quoted 10 lines
hw/i386/intel_iommu.c +5 -1
       [VTD_FR_CONTEXT_ENTRY_TT] = true,
-    [VTD_FR_PASID_TABLE_INV] = false,
+    [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
+    [VTD_FR_PASID_DIR_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
+    [VTD_FR_PASID_ENTRY_P] = true,
+    [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
       [VTD_FR_SM_INTERRUPT_ADDR] = true,
       [VTD_FR_MAX] = false,
   };

@Jason, @Michael,

Do you know the rule of setting this table? I noticed it was introduced since day-1[1]. I didn't see any history discussion on it on lore. So not quite sure about the purpose of it. Per the usage of this table, it is used as a filter when the iommu driver has set the FPD bit. If FPD is set, some errors need not to trigger a trace which is mostly for debug purpose.

Quoted 10 lines

I noticed Peter had asked it as well[2]. But I don't think it was clarified clearly. May we have a clarification for it here? BTW. I didn't see VT-d spec has any definition on it. So it should just be a software trick. :)

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/20190301065219.GA22229@xz-x1/

Signature · 2 lines
Regards,
Yi Liu

Are you asking for a definition of qualified fault conditions?

Quoted 13 lines

7.1.1 Non-Recoverable Address Translation Faults
Non-recoverable address translation faults can be detected by remapping hardware for many different kinds of requests as shown by Table 26. A non-recoverable fault condition is considered “qualified” if software can suppress reporting of the fault by setting one of the Fault Processing Disable (FPD) bits available in one or more of the address translation structures (i.e., the context-entry, scalable-mode context-entry, scalable-mode PASID-directory entry, scalable-mode PASID-table entry). For a request that encounters a “qualified” non-recoverable fault condition, if the remapping hardware encountered any translation structure entry with an FPD field value of 1, the remapping hardware must not report the fault to software. For example, when processing a request that encounters an FPD field with a value of 1 in the scalable-mode context-entry and encounters any “qualified” fault such as SCT.*, SPD.*, SPT.*, SFS.*, SSS.*, or SGN.*, the remapping hardware will not report the fault to software. Memory requests that result in non-recoverable address translation faults are blocked by hardware

I see. Table 26 has the "qualified" column. It's clear to me now. Thanks. :)

Signature · 2 lines
Regards,
Yi Liu
#6 lore raw

PATCH 2/17 intel_iommu: Make pasid entry type check accurate

In reply to Zhenzhong Duan

When guest configures Nested Translation(011b) or First-stage Translation only (001b), type check passed unaccurately.

Fails the type check in those cases as their simulation isn't supported yet.

Fixes fb43cf739e1 ("intel_iommu: scalable mode emulation")
---
 hw/i386/intel_iommu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
hw/i386/intel_iommu.c +4 -8
index e65f5b29a5..1cff8b00ae 100644
@@ -759,20 +759,16 @@ static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
                                      VTDPASIDEntry *pe)
 {
     switch (VTD_PE_GET_TYPE(pe)) {
-    case VTD_SM_PASID_ENTRY_FLT:
     case VTD_SM_PASID_ENTRY_SLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
-        break;
+        return true;
     case VTD_SM_PASID_ENTRY_PT:
-        if (!x86_iommu->pt_supported) {
-            return false;
-        }
-        break;
+        return x86_iommu->pt_supported;
+    case VTD_SM_PASID_ENTRY_FLT:
+    case VTD_SM_PASID_ENTRY_NESTED:
     default:
         /* Unknown type */
         return false;
     }
-    return true;
 }
 
 static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
Signature · 1 line
2.34.1
Reviewed-by on v1 2/17
#7 lore raw

Re: [PATCH v1 02/17] intel_iommu: Make pasid entry type check accurate

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

When guest configures Nested Translation(011b) or First-stage Translation only (001b), type check passed unaccurately.

Fails the type check in those cases as their simulation isn't supported yet.

Fixes fb43cf739e1 ("intel_iommu: scalable mode emulation")
---
  hw/i386/intel_iommu.c | 12 ++++--------
  1 file changed, 4 insertions(+), 8 deletions(-)
hw/i386/intel_iommu.c +4 -9
index e65f5b29a5..1cff8b00ae 100644
@@ -759,20 +759,16 @@ static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
                                       VTDPASIDEntry *pe)
  {
      switch (VTD_PE_GET_TYPE(pe)) {
-    case VTD_SM_PASID_ENTRY_FLT:
      case VTD_SM_PASID_ENTRY_SLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
-        break;
+        return true;
      case VTD_SM_PASID_ENTRY_PT:
-        if (!x86_iommu->pt_supported) {
-            return false;
-        }
-        break;
+        return x86_iommu->pt_supported;
+    case VTD_SM_PASID_ENTRY_FLT:
+    case VTD_SM_PASID_ENTRY_NESTED:
      default:
          /* Unknown type */
          return false;
      }
-    return true;
  }
 
  static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
--

2.34.1

#8 lore raw

PATCH 3/17 intel_iommu: Add a placeholder variable for scalable modern mode

In reply to Zhenzhong Duan

Add an new element scalable_mode in IntelIOMMUState to mark scalable modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

---
 hw/i386/intel_iommu_internal.h |  2 ++
 include/hw/i386/intel_iommu.h  |  1 +
 hw/i386/intel_iommu.c          | 34 +++++++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_internal.h +2 -0
index c0ca7b372f..4e0331caba 100644
@@ -195,6 +195,7 @@
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
 #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
 /* CAP_REG */
 /* (offset >> 4) << 24 */
@@ -211,6 +212,7 @@
 #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
 #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
 #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
 #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ | VTD_CAP_DRAIN_WRITE)
 #define VTD_CAP_CM                  (1ULL << 7)
 #define VTD_PASID_ID_SHIFT          20
include/hw/i386/intel_iommu.h +1 -0
index 1eb05c29fc..788ed42477 100644
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
     bool caching_mode;              /* RO - is cap CM enabled? */
     bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
     bool snoop_control;             /* RO - is SNP filed supported? */
 
     dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +23 -11
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
 }
 
 /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 {
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
     switch (VTD_PE_GET_TYPE(pe)) {
+    case VTD_SM_PASID_ENTRY_FLT:
+        return s->scalable_modern;
     case VTD_SM_PASID_ENTRY_SLT:
-        return true;
+        return !s->scalable_modern;
+    case VTD_SM_PASID_ENTRY_NESTED:
+        /* Not support NESTED page table type yet */
+        return false;
     case VTD_SM_PASID_ENTRY_PT:
         return x86_iommu->pt_supported;
-    case VTD_SM_PASID_ENTRY_FLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
     default:
         /* Unknown type */
         return false;
@@ -813,7 +817,6 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
     uint8_t pgtt;
     uint32_t index;
     dma_addr_t entry_size;
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
     index = VTD_PASID_TABLE_INDEX(pasid);
     entry_size = VTD_PASID_ENTRY_SIZE;
@@ -827,7 +830,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
     }
 
     /* Do translation type check */
-    if (!vtd_pe_type_check(x86_iommu, pe)) {
+    if (!vtd_pe_type_check(s, pe)) {
         return -VTD_FR_PASID_TABLE_ENTRY_INV;
     }
 
@@ -3861,7 +3864,13 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
         return false;
     }
 
-    return true;
+    if (!s->scalable_modern) {
+        /* All checks requested by VTD non-modern mode pass */
+        return true;
+    }
+
+    error_setg(errp, "host device is unsupported in scalable modern mode yet");
+    return false;
 }
 
 static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
@@ -4084,7 +4093,10 @@ static void vtd_cap_init(IntelIOMMUState *s)
     }
 
     /* TODO: read cap/ecap from host to decide which cap to be exposed. */
-    if (s->scalable_mode) {
+    if (s->scalable_modern) {
+        s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
+        s->cap |= VTD_CAP_FS1GP;
+    } else if (s->scalable_mode) {
         s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
     }
 
@@ -4251,9 +4263,9 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
         }
     }
 
-    /* Currently only address widths supported are 39 and 48 bits */
     if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
-        (s->aw_bits != VTD_HOST_AW_48BIT)) {
+        (s->aw_bits != VTD_HOST_AW_48BIT) &&
+        !s->scalable_modern) {
         error_setg(errp, "Supported values for aw-bits are: %d, %d",
                    VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
         return false;
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

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

Add an new element scalable_mode in IntelIOMMUState to mark scalable modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

---
  hw/i386/intel_iommu_internal.h |  2 ++
  include/hw/i386/intel_iommu.h  |  1 +
  hw/i386/intel_iommu.c          | 34 +++++++++++++++++++++++-----------
  3 files changed, 26 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_internal.h +2 -0
index c0ca7b372f..4e0331caba 100644
@@ -195,6 +195,7 @@
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
  /* CAP_REG */
  /* (offset >> 4) << 24 */
@@ -211,6 +212,7 @@
  #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
  #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
  #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
  #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ | VTD_CAP_DRAIN_WRITE)
  #define VTD_CAP_CM                  (1ULL << 7)
  #define VTD_PASID_ID_SHIFT          20
include/hw/i386/intel_iommu.h +1 -0
index 1eb05c29fc..788ed42477 100644
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
      bool caching_mode;              /* RO - is cap CM enabled? */
      bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
      bool snoop_control;             /* RO - is SNP filed supported? */
 
      dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644

hw/i386/intel_iommu.c static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level) · line 759

Quoted 7 lines
hw/i386/intel_iommu.c +1 -2
@@ -755,16 +755,20 @@ static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
  }
 
  /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
  {

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

Quoted 77 lines
hw/i386/intel_iommu.c +22 -10
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
      switch (VTD_PE_GET_TYPE(pe)) {
+    case VTD_SM_PASID_ENTRY_FLT:
+        return s->scalable_modern;
      case VTD_SM_PASID_ENTRY_SLT:
-        return true;
+        return !s->scalable_modern;
+    case VTD_SM_PASID_ENTRY_NESTED:
+        /* Not support NESTED page table type yet */
+        return false;
      case VTD_SM_PASID_ENTRY_PT:
          return x86_iommu->pt_supported;
-    case VTD_SM_PASID_ENTRY_FLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
      default:
          /* Unknown type */
          return false;
@@ -813,7 +817,6 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      uint8_t pgtt;
      uint32_t index;
      dma_addr_t entry_size;
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
      index = VTD_PASID_TABLE_INDEX(pasid);
      entry_size = VTD_PASID_ENTRY_SIZE;
@@ -827,7 +830,7 @@ static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
      }
 
      /* Do translation type check */
-    if (!vtd_pe_type_check(x86_iommu, pe)) {
+    if (!vtd_pe_type_check(s, pe)) {
          return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
@@ -3861,7 +3864,13 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
          return false;
      }
 
-    return true;
+    if (!s->scalable_modern) {
+        /* All checks requested by VTD non-modern mode pass */
+        return true;
+    }
+
+    error_setg(errp, "host device is unsupported in scalable modern mode yet");
+    return false;
  }
 
  static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
@@ -4084,7 +4093,10 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
      /* TODO: read cap/ecap from host to decide which cap to be exposed. */
-    if (s->scalable_mode) {
+    if (s->scalable_modern) {
+        s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
+        s->cap |= VTD_CAP_FS1GP;
+    } else if (s->scalable_mode) {
          s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
      }
 
@@ -4251,9 +4263,9 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
          }
      }
 
-    /* Currently only address widths supported are 39 and 48 bits */
      if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
-        (s->aw_bits != VTD_HOST_AW_48BIT)) {
+        (s->aw_bits != VTD_HOST_AW_48BIT) &&
+        !s->scalable_modern) {
          error_setg(errp, "Supported values for aw-bits are: %d, %d",
                     VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
          return false;
--

2.34.1

#10 lore raw

RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 78 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 1 line · 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.

Quoted 12 lines

Add an new element scalable_mode in IntelIOMMUState to mark scalable modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

Review

Quoted 7 lines
---
  hw/i386/intel_iommu_internal.h |  2 ++
  include/hw/i386/intel_iommu.h  |  1 +
  hw/i386/intel_iommu.c          | 34 +++++++++++++++++++++++-----------
  3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
  #define VTD_ECAP_PASID              (1ULL << 40)
  #define VTD_ECAP_SMTS               (1ULL << 43)
  #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
  /* CAP_REG */
  /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
  #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
  #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
  #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
  #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
  #define VTD_CAP_CM                  (1ULL << 7)
  #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

hw/i386/intel_iommu.c static inline bool

Quoted 16 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
      bool caching_mode;              /* RO - is cap CM enabled? */
      bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
      bool snoop_control;             /* RO - is SNP filed supported? */
 
      dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

hw/i386/intel_iommu.c static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
  }
 
  /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else VTD_ECAP_SLTS can be set or not depending on s->scalable_mode. *So it's simpler to check s->scalable_modern directly for a PASID entry type instead ecap bits.
*/

Thanks
Zhenzhong

Quoted 84 lines

hw/i386/intel_iommu.c static int

Quoted 19 lines
hw/i386/intel_iommu.c +8 -3
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
      switch (VTD_PE_GET_TYPE(pe)) {
+    case VTD_SM_PASID_ENTRY_FLT:
+        return s->scalable_modern;
      case VTD_SM_PASID_ENTRY_SLT:
-        return true;
+        return !s->scalable_modern;
+    case VTD_SM_PASID_ENTRY_NESTED:
+        /* Not support NESTED page table type yet */
+        return false;
      case VTD_SM_PASID_ENTRY_PT:
          return x86_iommu->pt_supported;
-    case VTD_SM_PASID_ENTRY_FLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
      default:
          /* Unknown type */
          return false;
@@ -813,7 +817,6 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

hw/i386/intel_iommu.c static int

Quoted 8 lines
hw/i386/intel_iommu.c +0 -1
      uint8_t pgtt;
      uint32_t index;
      dma_addr_t entry_size;
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
      index = VTD_PASID_TABLE_INDEX(pasid);
      entry_size = VTD_PASID_ENTRY_SIZE;
@@ -827,7 +830,7 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

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

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
      }
 
      /* Do translation type check */
-    if (!vtd_pe_type_check(x86_iommu, pe)) {
+    if (!vtd_pe_type_check(s, pe)) {
          return -VTD_FR_PASID_TABLE_ENTRY_INV;
      }
 
@@ -3861,7 +3864,13 @@ static bool vtd_check_hiod(IntelIOMMUState

*s, HostIOMMUDevice *hiod,

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

Quoted 10 lines
hw/i386/intel_iommu.c +6 -1
          return false;
      }
 
-    return true;
+    if (!s->scalable_modern) {
+        /* All checks requested by VTD non-modern mode pass */
+        return true;
+    }
+
+    error_setg(errp, "host device is unsupported in scalable modern mode

yet");

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

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+    return false;
  }
 
  static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int

devfn,

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

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
@@ -4084,7 +4093,10 @@ static void vtd_cap_init(IntelIOMMUState *s)
      }
 
      /* TODO: read cap/ecap from host to decide which cap to be exposed.

*/

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

Quoted 9 lines
hw/i386/intel_iommu.c +4 -1
-    if (s->scalable_mode) {
+    if (s->scalable_modern) {
+        s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
+        s->cap |= VTD_CAP_FS1GP;
+    } else if (s->scalable_mode) {
          s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
      }
 
@@ -4251,9 +4263,9 @@ static bool vtd_decide_config(IntelIOMMUState

*s, Error **errp)

Quoted 14 lines
hw/i386/intel_iommu.c +2 -3
          }
      }
 
-    /* Currently only address widths supported are 39 and 48 bits */
      if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
-        (s->aw_bits != VTD_HOST_AW_48BIT)) {
+        (s->aw_bits != VTD_HOST_AW_48BIT) &&
+        !s->scalable_modern) {
          error_setg(errp, "Supported values for aw-bits are: %d, %d",
                     VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
          return false;
--

2.34.1

#11 lore raw

Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 84 lines · Zhenzhong
Quoted 78 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 1 line · 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.

Quoted 12 lines

Add an new element scalable_mode in IntelIOMMUState to mark scalable modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

Review

Quoted 7 lines
---
   hw/i386/intel_iommu_internal.h |  2 ++
   include/hw/i386/intel_iommu.h  |  1 +
   hw/i386/intel_iommu.c          | 34 +++++++++++++++++++++++-----------
   3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_SMTS               (1ULL << 43)
   #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
   /* CAP_REG */
   /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
   #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
   #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
   #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
   #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
   #define VTD_CAP_CM                  (1ULL << 7)
   #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

hw/i386/intel_iommu.c static inline bool

Quoted 16 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
       bool caching_mode;              /* RO - is cap CM enabled? */
       bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
       bool snoop_control;             /* RO - is SNP filed supported? */
 
       dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

hw/i386/intel_iommu.c static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
   }
 
   /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

Quoted 6 lines

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else VTD_ECAP_SLTS can be set or not depending on s->scalable_mode. *So it's simpler to check s->scalable_modern directly for a PASID entry type instead ecap bits.
*/

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this helper as well if we check the scalable_modern. But if we check the ecap/cap, then the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need to check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

Quoted 85 lines
Quoted 85 lines

hw/i386/intel_iommu.c static int

Quoted 20 lines
hw/i386/intel_iommu.c +8 -3
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
       switch (VTD_PE_GET_TYPE(pe)) {
+    case VTD_SM_PASID_ENTRY_FLT:
+        return s->scalable_modern;
       case VTD_SM_PASID_ENTRY_SLT:
-        return true;
+        return !s->scalable_modern;
+    case VTD_SM_PASID_ENTRY_NESTED:
+        /* Not support NESTED page table type yet */
+        return false;
       case VTD_SM_PASID_ENTRY_PT:
           return x86_iommu->pt_supported;
-    case VTD_SM_PASID_ENTRY_FLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
       default:
           /* Unknown type */
           return false;
@@ -813,7 +817,6 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

hw/i386/intel_iommu.c static int

Quoted 8 lines
hw/i386/intel_iommu.c +0 -1
       uint8_t pgtt;
       uint32_t index;
       dma_addr_t entry_size;
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
       index = VTD_PASID_TABLE_INDEX(pasid);
       entry_size = VTD_PASID_ENTRY_SIZE;
@@ -827,7 +830,7 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

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

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
       }
 
       /* Do translation type check */
-    if (!vtd_pe_type_check(x86_iommu, pe)) {
+    if (!vtd_pe_type_check(s, pe)) {
           return -VTD_FR_PASID_TABLE_ENTRY_INV;
       }
 
@@ -3861,7 +3864,13 @@ static bool vtd_check_hiod(IntelIOMMUState

*s, HostIOMMUDevice *hiod,

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

Quoted 10 lines
hw/i386/intel_iommu.c +6 -1
           return false;
       }
 
-    return true;
+    if (!s->scalable_modern) {
+        /* All checks requested by VTD non-modern mode pass */
+        return true;
+    }
+
+    error_setg(errp, "host device is unsupported in scalable modern mode

yet");

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

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+    return false;
   }
 
   static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int

devfn,

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

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
@@ -4084,7 +4093,10 @@ static void vtd_cap_init(IntelIOMMUState *s)
       }
 
       /* TODO: read cap/ecap from host to decide which cap to be exposed.

*/

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

Quoted 9 lines
hw/i386/intel_iommu.c +4 -1
-    if (s->scalable_mode) {
+    if (s->scalable_modern) {
+        s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
+        s->cap |= VTD_CAP_FS1GP;
+    } else if (s->scalable_mode) {
           s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
       }
 
@@ -4251,9 +4263,9 @@ static bool vtd_decide_config(IntelIOMMUState

*s, Error **errp)

Quoted 14 lines
hw/i386/intel_iommu.c +2 -3
           }
       }
 
-    /* Currently only address widths supported are 39 and 48 bits */
       if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
-        (s->aw_bits != VTD_HOST_AW_48BIT)) {
+        (s->aw_bits != VTD_HOST_AW_48BIT) &&
+        !s->scalable_modern) {
           error_setg(errp, "Supported values for aw-bits are: %d, %d",
                      VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
           return false;
--

2.34.1

Signature · 2 lines
Regards,
Yi Liu
#12 lore raw

RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 100 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 6 lines
Quoted 6 lines

scalable modern mode

Quoted 1 line · Zhenzhong Duan

Caution: External email. Do not open attachments or click links, unless

this

Quoted 4 lines
Quoted 4 lines

email comes from a known sender and you know the content is safe.

Quoted 3 lines

Add an new element scalable_mode in IntelIOMMUState to mark

scalable

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

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

--

Quoted 56 lines
Quoted 50 lines

Review

Quoted 3 lines
   3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_SMTS               (1ULL << 43)
   #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
   /* CAP_REG */
   /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
   #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
   #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
   #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
   #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
   #define VTD_CAP_CM                  (1ULL << 7)
   #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

hw/i386/intel_iommu.c static inline bool

Quoted 16 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
       bool caching_mode;              /* RO - is cap CM enabled? */
       bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
       bool snoop_control;             /* RO - is SNP filed supported? */
 
       dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

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

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
   }
 
   /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

Quoted 4 lines

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else

Quoted 7 lines

VTD_ECAP_SLTS can be set or not depending on s->scalable_mode.

Quoted 1 line

*So it's simpler to check s->scalable_modern directly for a PASID entry

type instead ecap bits.

Quoted 1 line

*/

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

It depends on which scalable mode is chosed.
In scalable legacy mode, PGTT is SS-only and we should return true.

Quoted 10 lines

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this helper as well if we check the scalable_modern. But if we check the ecap/cap, then the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need to check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

OK, will be like below:

hw/i386/intel_iommu.c +3 -3
@@ -826,14 +826,14 @@ static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
     switch (VTD_PE_GET_TYPE(pe)) {
     case VTD_SM_PASID_ENTRY_FLT:
-        return s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS);
     case VTD_SM_PASID_ENTRY_SLT:
-        return !s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS) || !(s->ecap & VTD_ECAP_SMTS);
     case VTD_SM_PASID_ENTRY_NESTED:
         /* Not support NESTED page table type yet */
         return false;
     case VTD_SM_PASID_ENTRY_PT:
-        return x86_iommu->pt_supported;
+        return !!(s->ecap & VTD_ECAP_PT);
     default:
         /* Unknown type */
         return false;

Thanks
Zhenzhong

#13 lore raw

RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 137 lines

-----Original Message-----
From: Duan, Zhenzhong]
Subject: RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 16 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 4 lines
Quoted 4 lines

scalable modern mode

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines · Zhenzhong Duan
Quoted 1 line

Caution: External email. Do not open attachments or click links, unless

this

Quoted 4 lines
Quoted 4 lines

email comes from a known sender and you know the content is safe.

Quoted 3 lines

Add an new element scalable_mode in IntelIOMMUState to mark

scalable

Quoted 1 line
Quoted 1 line
Quoted 1 line

modern mode, this element will be exposed as an intel_iommu

property

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

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

--

Quoted 27 lines

--

Quoted 26 lines
Quoted 26 lines

Review

Quoted 3 lines
   3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_SMTS               (1ULL << 43)
   #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
   /* CAP_REG */
   /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
   #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
   #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
   #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
   #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
   #define VTD_CAP_CM                  (1ULL << 7)
   #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 264

Quoted 7 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
       bool caching_mode;              /* RO - is cap CM enabled? */
       bool scalable_mode;             /* RO - is Scalable Mode supported?

*/

Quoted 27 lines
Quoted 23 lines
Quoted 17 lines

hw/i386/intel_iommu.c static inline bool

Quoted 9 lines
include/hw/i386/intel_iommu.h +1 -0
+    bool scalable_modern;           /* RO - is modern SM supported? */
       bool snoop_control;             /* RO - is SNP filed supported? */
 
       dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

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

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
   }
 
   /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

Quoted 4 lines

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else

Quoted 7 lines

VTD_ECAP_SLTS can be set or not depending on s->scalable_mode.

Quoted 1 line

*So it's simpler to check s->scalable_modern directly for a PASID entry

type instead ecap bits.

Quoted 1 line

*/

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

It depends on which scalable mode is chosed.
In scalable legacy mode, PGTT is SS-only and we should return true.

Quoted 10 lines

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this helper as well if we check the scalable_modern. But if we check the ecap/cap, then the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need to check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

OK, will be like below:

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

Quoted 9 lines
hw/i386/intel_iommu.c +2 -2
@@ -826,14 +826,14 @@ static inline bool
 vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
    switch (VTD_PE_GET_TYPE(pe)) {
    case VTD_SM_PASID_ENTRY_FLT:
-        return s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS);
    case VTD_SM_PASID_ENTRY_SLT:
-        return !s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS) || !(s->ecap & VTD_ECAP_SMTS);

Sorry typo err, should be:

+ return !!(s->ecap & VTD_ECAP_SLTS) || !(s->ecap & VTD_ECAP_SMTS);

Quoted 12 lines
hw/i386/intel_iommu.c +1 -1
    case VTD_SM_PASID_ENTRY_NESTED:
        /* Not support NESTED page table type yet */
        return false;
    case VTD_SM_PASID_ENTRY_PT:
-        return x86_iommu->pt_supported;
+        return !!(s->ecap & VTD_ECAP_PT);
    default:
        /* Unknown type */
        return false;

Thanks
Zhenzhong

#14 lore raw

Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 142 lines · Zhenzhong

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

Quoted 128 lines

-----Original Message-----
From: Duan, Zhenzhong]
Subject: RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 15 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 4 lines · Zhenzhong
Quoted 4 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 4 lines
Quoted 4 lines

scalable modern mode

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines · Zhenzhong Duan
Quoted 1 line

Caution: External email. Do not open attachments or click links, unless

this

Quoted 3 lines
Quoted 3 lines

email comes from a known sender and you know the content is safe.

Quoted 2 lines

Add an new element scalable_mode in IntelIOMMUState to mark

scalable

Quoted 1 line
Quoted 1 line
Quoted 1 line

modern mode, this element will be exposed as an intel_iommu

property

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

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

--

Quoted 27 lines

--

Quoted 26 lines
Quoted 26 lines

Review

Quoted 3 lines
    3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
    #define VTD_ECAP_PASID              (1ULL << 40)
    #define VTD_ECAP_SMTS               (1ULL << 43)
    #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
    /* CAP_REG */
    /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
    #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
    #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
    #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
    #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
    #define VTD_CAP_CM                  (1ULL << 7)
    #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 264

Quoted 7 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
        bool caching_mode;              /* RO - is cap CM enabled? */
        bool scalable_mode;             /* RO - is Scalable Mode supported?

*/

Quoted 24 lines
Quoted 24 lines
Quoted 17 lines

hw/i386/intel_iommu.c static inline bool

Quoted 9 lines
include/hw/i386/intel_iommu.h +1 -0
+    bool scalable_modern;           /* RO - is modern SM supported? */
        bool snoop_control;             /* RO - is SNP filed supported? */
 
        dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

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

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
    }
 
    /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

What about below:

Quoted 8 lines
Quoted 2 lines

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else

VTD_ECAP_SLTS can be set or not depending on s->scalable_mode.

Quoted 1 line

*So it's simpler to check s->scalable_modern directly for a PASID entry

type instead ecap bits.

Quoted 1 line
   */

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

It depends on which scalable mode is chosed.
In scalable legacy mode, PGTT is SS-only and we should return true.

Quoted 9 lines

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this helper as well if we check the scalable_modern. But if we check the ecap/cap, then the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need to check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

OK, will be like below:

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

Quoted 9 lines
hw/i386/intel_iommu.c +2 -2
@@ -826,14 +826,14 @@ static inline bool
 vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
     switch (VTD_PE_GET_TYPE(pe)) {
     case VTD_SM_PASID_ENTRY_FLT:
-        return s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS);
     case VTD_SM_PASID_ENTRY_SLT:
-        return !s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS) || !(s->ecap & VTD_ECAP_SMTS);

Sorry typo err, should be:

Quoted 2 lines

+ return !!(s->ecap & VTD_ECAP_SLTS) || !(s->ecap & VTD_ECAP_SMTS);

I agree with Yi's point of view, this version looks good

Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +1 -1
     case VTD_SM_PASID_ENTRY_NESTED:
         /* Not support NESTED page table type yet */
         return false;
     case VTD_SM_PASID_ENTRY_PT:
-        return x86_iommu->pt_supported;
+        return !!(s->ecap & VTD_ECAP_PT);
     default:
         /* Unknown type */
         return false;

Thanks
Zhenzhong

#15 lore raw

Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

In reply to Duan, Zhenzhong
Quoted 142 lines · Zhenzhong

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

Quoted 128 lines

-----Original Message-----
From: Duan, Zhenzhong]
Subject: RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 15 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 4 lines · Zhenzhong
Quoted 4 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 4 lines
Quoted 4 lines

scalable modern mode

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines · Zhenzhong Duan
Quoted 1 line

Caution: External email. Do not open attachments or click links, unless

this

Quoted 3 lines
Quoted 3 lines

email comes from a known sender and you know the content is safe.

Quoted 2 lines

Add an new element scalable_mode in IntelIOMMUState to mark

scalable

Quoted 1 line
Quoted 1 line
Quoted 1 line

modern mode, this element will be exposed as an intel_iommu

property

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

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

--

Quoted 27 lines

--

Quoted 26 lines
Quoted 26 lines

Review

Quoted 3 lines
    3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
    #define VTD_ECAP_PASID              (1ULL << 40)
    #define VTD_ECAP_SMTS               (1ULL << 43)
    #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
    /* CAP_REG */
    /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
    #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
    #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
    #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
    #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
    #define VTD_CAP_CM                  (1ULL << 7)
    #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 264

Quoted 7 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
        bool caching_mode;              /* RO - is cap CM enabled? */
        bool scalable_mode;             /* RO - is Scalable Mode supported?

*/

Quoted 24 lines
Quoted 24 lines
Quoted 17 lines

hw/i386/intel_iommu.c static inline bool

Quoted 9 lines
include/hw/i386/intel_iommu.h +1 -0
+    bool scalable_modern;           /* RO - is modern SM supported? */
        bool snoop_control;             /* RO - is SNP filed supported? */
 
        dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

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

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
    }
 
    /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

What about below:

Quoted 8 lines
Quoted 2 lines

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else

VTD_ECAP_SLTS can be set or not depending on s->scalable_mode.

Quoted 1 line

*So it's simpler to check s->scalable_modern directly for a PASID entry

type instead ecap bits.

Quoted 1 line
   */

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

It depends on which scalable mode is chosed.
In scalable legacy mode, PGTT is SS-only and we should return true.

Quoted 9 lines

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this helper as well if we check the scalable_modern. But if we check the ecap/cap, then the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need to check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

OK, will be like below:

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

Quoted 9 lines
hw/i386/intel_iommu.c +2 -2
@@ -826,14 +826,14 @@ static inline bool
 vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
     switch (VTD_PE_GET_TYPE(pe)) {
     case VTD_SM_PASID_ENTRY_FLT:
-        return s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS);
     case VTD_SM_PASID_ENTRY_SLT:
-        return !s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS) || !(s->ecap & VTD_ECAP_SMTS);

Sorry typo err, should be:

Quoted 2 lines

+ return !!(s->ecap & VTD_ECAP_SLTS) || !(s->ecap & VTD_ECAP_SMTS);

Moreover, shouldn't we declare the capabilities after the feature is implemented?
I think FLTS and FS1GP should not be declared that early.

Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +1 -1
     case VTD_SM_PASID_ENTRY_NESTED:
         /* Not support NESTED page table type yet */
         return false;
     case VTD_SM_PASID_ENTRY_PT:
-        return x86_iommu->pt_supported;
+        return !!(s->ecap & VTD_ECAP_PT);
     default:
         /* Unknown type */
         return false;

Thanks
Zhenzhong

#16 lore raw

RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 164 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 1 line · Zhenzhong

Caution: External email. Do not open attachments or click links, unless this

email comes from a known sender and you know the content is safe.

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Duan, Zhenzhong]
Subject: RE: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 7 lines
Quoted 7 lines

scalable modern mode

Quoted 3 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable

for

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

scalable modern mode

Quoted 4 lines · Zhenzhong
Quoted 4 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder

variable

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

for

Quoted 6 lines
Quoted 6 lines

scalable modern mode

Quoted 1 line · Zhenzhong Duan

Caution: External email. Do not open attachments or click links,

unless

Quoted 13 lines
Quoted 13 lines
Quoted 6 lines

this

Quoted 3 lines
Quoted 3 lines

email comes from a known sender and you know the content is safe.

Quoted 2 lines

Add an new element scalable_mode in IntelIOMMUState to mark

scalable

Quoted 1 line
Quoted 1 line
Quoted 1 line

modern mode, this element will be exposed as an intel_iommu

property

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

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

-----

Quoted 33 lines
Quoted 33 lines

--

Quoted 32 lines

--

Quoted 31 lines
Quoted 31 lines

Review

Quoted 3 lines
    3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
    #define VTD_ECAP_PASID              (1ULL << 40)
    #define VTD_ECAP_SMTS               (1ULL << 43)
    #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
    /* CAP_REG */
    /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
    #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
    #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
    #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
    #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
    #define VTD_CAP_CM                  (1ULL << 7)
    #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 5 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 264

Quoted 2 lines
include/hw/i386/intel_iommu.h +0 -0
        bool caching_mode;              /* RO - is cap CM enabled? */
        bool scalable_mode;             /* RO - is Scalable Mode supported?

*/

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
include/hw/i386/intel_iommu.h +1 -0
+    bool scalable_modern;           /* RO - is modern SM supported?

*/

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

hw/i386/intel_iommu.c static inline bool

Quoted 8 lines
include/hw/i386/intel_iommu.h +0 -0
        bool snoop_control;             /* RO - is SNP filed supported? */
 
        dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
    }
 
    /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState

*x86_iommu,

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

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

Quoted 2 lines
hw/i386/intel_iommu.c +1 -1
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

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

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

What about using the cap/ecap registers to know if the translation

types

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else

VTD_ECAP_SLTS can be set or not depending on s->scalable_mode.

Quoted 1 line

*So it's simpler to check s->scalable_modern directly for a PASID

entry

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

type instead ecap bits.

Quoted 1 line
   */

Since this helper is for pasid entry check, so you can just return false if the pe's PGTT is SS-only.

Quoted 8 lines
Quoted 8 lines

It depends on which scalable mode is chosed.
In scalable legacy mode, PGTT is SS-only and we should return true.

Quoted 5 lines

It might make more sense to check the ecap/cap here as anyhow the capability is listed in ecap/cap. This may also bring us some convenience.

Say in the future, if we want to add a new mode (e.g. scalable mode 2.0) that supports both FS and SS for guest, we may need to update this

helper

Quoted 1 line
Quoted 1 line
Quoted 1 line

as well if we check the scalable_modern. But if we check the ecap/cap,

then

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the future change just needs to control the ecap/cap setting at the beginning of the vIOMMU init. To keep the code aligned, you may need

to

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

check ecap.PT bit for VTD_SM_PASID_ENTRY_PT. :)

OK, will be like below:

hw/i386/intel_iommu.c +0 -0
@@ -826,14 +826,14 @@ static inline bool
 vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
 
Quoted 7 lines

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

Quoted 7 lines
hw/i386/intel_iommu.c +2 -2
     switch (VTD_PE_GET_TYPE(pe)) {
     case VTD_SM_PASID_ENTRY_FLT:
-        return s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS);
     case VTD_SM_PASID_ENTRY_SLT:
-        return !s->scalable_modern;
+        return !!(s->ecap & VTD_ECAP_FLTS) || !(s->ecap &

VTD_ECAP_SMTS);

Quoted 8 lines
Quoted 3 lines

Sorry typo err, should be:

+ return !!(s->ecap & VTD_ECAP_SLTS) || !(s->ecap &

VTD_ECAP_SMTS);

Quoted 1 line

Moreover, shouldn't we declare the capabilities after the feature is implemented?
I think FLTS and FS1GP should not be declared that early.

OK, I can move it to " [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option", In fact, before patch16, there is no way to enable s->scalable_modern, so those caps can never be enabled.

Thanks
Zhenzhong

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +1 -1
     case VTD_SM_PASID_ENTRY_NESTED:
         /* Not support NESTED page table type yet */
         return false;
     case VTD_SM_PASID_ENTRY_PT:
-        return x86_iommu->pt_supported;
+        return !!(s->ecap & VTD_ECAP_PT);
     default:
         /* Unknown type */
         return false;

Thanks
Zhenzhong

#17 lore raw

Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

In reply to Duan, Zhenzhong
Quoted 83 lines · Zhenzhong

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

Quoted 75 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 03/17] intel_iommu: Add a placeholder variable for scalable modern mode

Quoted 1 line · 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.

Quoted 11 lines

Add an new element scalable_mode in IntelIOMMUState to mark scalable modern mode, this element will be exposed as an intel_iommu property finally.

For now, it's only a placehholder and used for cap/ecap initialization, compatibility check and block host device passthrough until nesting is supported.

Review

Quoted 7 lines
---
   hw/i386/intel_iommu_internal.h |  2 ++
   include/hw/i386/intel_iommu.h  |  1 +
   hw/i386/intel_iommu.c          | 34 +++++++++++++++++++++++-----------
   3 files changed, 26 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 12 lines

index c0ca7b372f..4e0331caba 100644

hw/i386/intel_iommu_internal.h +1 -0
@@ -195,6 +195,7 @@
   #define VTD_ECAP_PASID              (1ULL << 40)
   #define VTD_ECAP_SMTS               (1ULL << 43)
   #define VTD_ECAP_SLTS               (1ULL << 46)
+#define VTD_ECAP_FLTS               (1ULL << 47)
 
   /* CAP_REG */
   /* (offset >> 4) << 24 */

hw/i386/intel_iommu_internal.h line 216

Quoted 5 lines
hw/i386/intel_iommu_internal.h +1 -0
@@ -211,6 +212,7 @@
   #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
   #define VTD_CAP_DRAIN_WRITE         (1ULL << 54)
   #define VTD_CAP_DRAIN_READ          (1ULL << 55)
+#define VTD_CAP_FS1GP               (1ULL << 56)
   #define VTD_CAP_DRAIN               (VTD_CAP_DRAIN_READ |

VTD_CAP_DRAIN_WRITE)

Review

Quoted 3 lines
hw/i386/intel_iommu_internal.h +0 -0
   #define VTD_CAP_CM                  (1ULL << 7)
   #define VTD_PASID_ID_SHIFT          20
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

hw/i386/intel_iommu.c static inline bool

Quoted 16 lines

index 1eb05c29fc..788ed42477 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -262,6 +262,7 @@ struct IntelIOMMUState {
 
       bool caching_mode;              /* RO - is cap CM enabled? */
       bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    bool scalable_modern;           /* RO - is modern SM supported? */
       bool snoop_control;             /* RO - is SNP filed supported? */
 
       dma_addr_t root;                /* Current root table pointer */
hw/i386/intel_iommu.c +0 -0
index 1cff8b00ae..40cbd4a0f4 100644
@@ -755,16 +755,20 @@ static inline bool

vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

hw/i386/intel_iommu.c static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -2
   }
 
   /* Return true if check passed, otherwise false */
-static inline bool vtd_pe_type_check(X86IOMMUState *x86_iommu,
-                                     VTDPASIDEntry *pe)
+static inline bool vtd_pe_type_check(IntelIOMMUState *s,

VTDPASIDEntry *pe)

Quoted 5 lines

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

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

What about using the cap/ecap registers to know if the translation types are supported or not.
Otherwise, we could add a comment to explain why we expect s->scalable_modern to give us enough information.

Quoted 6 lines

What about below:

/*
*VTD_ECAP_FLTS in ecap is set if s->scalable_modern is true, or else VTD_ECAP_SLTS can be set or not depending on s->scalable_mode. *So it's simpler to check s->scalable_modern directly for a PASID entry type instead ecap bits.
*/

Fine ;)

Quoted 88 lines

Thanks
Zhenzhong

Quoted 84 lines

hw/i386/intel_iommu.c static int

Quoted 19 lines
hw/i386/intel_iommu.c +8 -3
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
       switch (VTD_PE_GET_TYPE(pe)) {
+    case VTD_SM_PASID_ENTRY_FLT:
+        return s->scalable_modern;
       case VTD_SM_PASID_ENTRY_SLT:
-        return true;
+        return !s->scalable_modern;
+    case VTD_SM_PASID_ENTRY_NESTED:
+        /* Not support NESTED page table type yet */
+        return false;
       case VTD_SM_PASID_ENTRY_PT:
           return x86_iommu->pt_supported;
-    case VTD_SM_PASID_ENTRY_FLT:
-    case VTD_SM_PASID_ENTRY_NESTED:
       default:
           /* Unknown type */
           return false;
@@ -813,7 +817,6 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

hw/i386/intel_iommu.c static int

Quoted 8 lines
hw/i386/intel_iommu.c +0 -1
       uint8_t pgtt;
       uint32_t index;
       dma_addr_t entry_size;
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
       index = VTD_PASID_TABLE_INDEX(pasid);
       entry_size = VTD_PASID_ENTRY_SIZE;
@@ -827,7 +830,7 @@ static int

vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,

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

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
       }
 
       /* Do translation type check */
-    if (!vtd_pe_type_check(x86_iommu, pe)) {
+    if (!vtd_pe_type_check(s, pe)) {
           return -VTD_FR_PASID_TABLE_ENTRY_INV;
       }
 
@@ -3861,7 +3864,13 @@ static bool vtd_check_hiod(IntelIOMMUState

*s, HostIOMMUDevice *hiod,

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

Quoted 10 lines
hw/i386/intel_iommu.c +6 -1
           return false;
       }
 
-    return true;
+    if (!s->scalable_modern) {
+        /* All checks requested by VTD non-modern mode pass */
+        return true;
+    }
+
+    error_setg(errp, "host device is unsupported in scalable modern mode

yet");

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

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+    return false;
   }
 
   static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int

devfn,

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

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
@@ -4084,7 +4093,10 @@ static void vtd_cap_init(IntelIOMMUState *s)
       }
 
       /* TODO: read cap/ecap from host to decide which cap to be exposed.

*/

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

Quoted 9 lines
hw/i386/intel_iommu.c +4 -1
-    if (s->scalable_mode) {
+    if (s->scalable_modern) {
+        s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
+        s->cap |= VTD_CAP_FS1GP;
+    } else if (s->scalable_mode) {
           s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
       }
 
@@ -4251,9 +4263,9 @@ static bool vtd_decide_config(IntelIOMMUState

*s, Error **errp)

Quoted 14 lines
hw/i386/intel_iommu.c +2 -3
           }
       }
 
-    /* Currently only address widths supported are 39 and 48 bits */
       if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
-        (s->aw_bits != VTD_HOST_AW_48BIT)) {
+        (s->aw_bits != VTD_HOST_AW_48BIT) &&
+        !s->scalable_modern) {
           error_setg(errp, "Supported values for aw-bits are: %d, %d",
                      VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
           return false;
--

2.34.1

#18 lore raw

PATCH 4/17 intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb invalidation

In reply to Zhenzhong Duan

Per spec 6.5.2.4, PADID-selective PASID-based iotlb invalidation will flush stage-2 iotlb entries with matching domain id and pasid.

With scalable modern mode introduced, guest could send PASID-selective PASID-based iotlb invalidation to flush both stage-1 and stage-2 entries.

---
 hw/i386/intel_iommu_internal.h | 10 +++++
 hw/i386/intel_iommu.c          | 78 ++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
hw/i386/intel_iommu_internal.h +10 -0
index 4e0331caba..f71fc91234 100644
@@ -440,6 +440,16 @@ typedef union VTDInvDesc VTDInvDesc;
         (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
         (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
+#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
+
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
+
+#define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
+                                             VTD_DOMAIN_ID_MASK)
+
 /* Information about page-selective IOTLB invalidate */
 struct VTDIOTLBPageInvInfo {
     uint16_t domain_id;
hw/i386/intel_iommu.c +78 -0

90 lines not on this page · raw message

Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb invalidation

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

Per spec 6.5.2.4, PADID-selective PASID-based iotlb invalidation will flush stage-2 iotlb entries with matching domain id and pasid.

With scalable modern mode introduced, guest could send PASID-selective PASID-based iotlb invalidation to flush both stage-1 and stage-2 entries.

---
  hw/i386/intel_iommu_internal.h | 10 +++++
  hw/i386/intel_iommu.c          | 78 ++++++++++++++++++++++++++++++++++
  2 files changed, 88 insertions(+)
hw/i386/intel_iommu_internal.h +10 -0
index 4e0331caba..f71fc91234 100644
@@ -440,6 +440,16 @@ typedef union VTDInvDesc VTDInvDesc;
          (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
          (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
+#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
+
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
+
+#define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
+                                             VTD_DOMAIN_ID_MASK)
+
  /* Information about page-selective IOTLB invalidate */
  struct VTDIOTLBPageInvInfo {
      uint16_t domain_id;
hw/i386/intel_iommu.c +43 -0
index 40cbd4a0f4..075a27adac 100644
@@ -2659,6 +2659,80 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
      return true;
  }
 
+static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value,
+                                         gpointer user_data)
+{
+    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
+    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
+
+    return ((entry->domain_id == info->domain_id) &&
+            (entry->pasid == info->pasid));
+}
+
+static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
+                                        uint16_t domain_id, uint32_t pasid)
+{
+    VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+
+    info.domain_id = domain_id;
+    info.pasid = pasid;
+
+    vtd_iommu_lock(s);
+    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
+                                &info);
+    vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            if (!s->scalable_modern) {
+                vtd_address_space_sync(vtd_as);
+            }
+        }
+    }
+}
+

hw/i386/intel_iommu.c static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) · line 2714

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
+                                    VTDInvDesc *inv_desc)
+{
+    uint16_t domain_id;
+    uint32_t pasid;
+
+    if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
+        (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
+        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%" PRIx64
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

This error is not formatted as the other similar messages we print when reserved bits are non-zero.
Here is what we've done in vtd_process_iotlb_desc:

        error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 ", lo=0x%"PRIx64" (reserved bits unzero)", __func__, inv_desc->hi, inv_desc->lo);

hw/i386/intel_iommu.c static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) · line 2720

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+        return false;
+    }
+
+    domain_id = VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]);
+    pasid = VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]);
+    switch (inv_desc->val[0] & VTD_INV_DESC_IOTLB_G) {

Not critical but why don't we have VTD_INV_DESC_PIOTLB_G?

hw/i386/intel_iommu.c static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) · line 2730

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    case VTD_INV_DESC_PIOTLB_ALL_IN_PASID:
+        vtd_piotlb_pasid_invalidate(s, domain_id, pasid);
+        break;
+
+    case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
+        break;
+
+    default:
+        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%" PRIx64
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

Same comment, I think we should make the messages consistent across descriptor handlers.

Quoted 22 lines
hw/i386/intel_iommu.c +9 -1
+        return false;
+    }
+    return true;
+}
+
  static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
                                       VTDInvDesc *inv_desc)
  {
@@ -2769,6 +2843,10 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)
          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)) {
+            return false;
+        }
          break;
 
      case VTD_INV_DESC_WAIT:
--

2.34.1

#20 lore raw

RE: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb invalidation

Quoted 112 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID- selective PASID-based iotlb invalidation

Quoted 1 line · 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.

Quoted 5 lines

Per spec 6.5.2.4, PADID-selective PASID-based iotlb invalidation will flush stage-2 iotlb entries with matching domain id and pasid.

Quoted 7 lines

With scalable modern mode introduced, guest could send PASID-selective PASID-based iotlb invalidation to flush both stage-1 and stage-2 entries.

---
  hw/i386/intel_iommu_internal.h | 10 +++++
  hw/i386/intel_iommu.c          | 78

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

Review

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

b/hw/i386/intel_iommu_internal.h

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

Quoted 5 lines

index 4e0331caba..f71fc91234 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -440,6 +440,16 @@ typedef union VTDInvDesc VTDInvDesc;
          (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM |

VTD_SL_TM)) : \

hw/i386/intel_iommu.c static bool

Quoted 20 lines
hw/i386/intel_iommu_internal.h +10 -0
          (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
+#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
+
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
+
+#define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
+                                             VTD_DOMAIN_ID_MASK)
+
  /* Information about page-selective IOTLB invalidate */
  struct VTDIOTLBPageInvInfo {
      uint16_t domain_id;
hw/i386/intel_iommu.c +0 -0
index 40cbd4a0f4..075a27adac 100644
@@ -2659,6 +2659,80 @@ static bool

vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)

Quoted 45 lines
hw/i386/intel_iommu.c +42 -0
      return true;
  }
 
+static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value,
+                                         gpointer user_data)
+{
+    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
+    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
+
+    return ((entry->domain_id == info->domain_id) &&
+            (entry->pasid == info->pasid));
+}
+
+static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
+                                        uint16_t domain_id, uint32_t pasid)
+{
+    VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+
+    info.domain_id = domain_id;
+    info.pasid = pasid;
+
+    vtd_iommu_lock(s);
+    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
+                                &info);
+    vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            if (!s->scalable_modern) {
+                vtd_address_space_sync(vtd_as);
+            }
+        }
+    }
+}

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
+                                    VTDInvDesc *inv_desc)
+{
+    uint16_t domain_id;
+    uint32_t pasid;
+
+    if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
+        (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
+        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%"

PRIx64

Quoted 4 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

This error is not formatted as the other similar messages we print when reserved bits are non-zero.
Here is what we've done in vtd_process_iotlb_desc:

Sure, will change as below,

Quoted 11 lines

        error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64 ", lo=0x%"PRIx64" (reserved bits unzero)", __func__, inv_desc->hi, inv_desc->lo);

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

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+        return false;
+    }
+
+    domain_id = VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]);
+    pasid = VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]);
+    switch (inv_desc->val[0] & VTD_INV_DESC_IOTLB_G) {

Not critical but why don't we have VTD_INV_DESC_PIOTLB_G?

Will add.

Quoted 10 lines

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

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+    case VTD_INV_DESC_PIOTLB_ALL_IN_PASID:
+        vtd_piotlb_pasid_invalidate(s, domain_id, pasid);
+        break;
+
+    case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
+        break;
+
+    default:
+        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%"

PRIx64

Quoted 3 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

Same comment, I think we should make the messages consistent across descriptor handlers.

What about below:

hw/i386/intel_iommu_internal.h +2 -1
index 3290761595..e76fd9d377 100644
@@ -479,9 +479,10 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
 #define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
 
+/* Masks for IOTLB Invalidate Descriptor */
+#define VTD_INV_DESC_IOTLB_G              (3ULL << 4)
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
 #define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
-
 #define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
 #define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
                                              VTD_DOMAIN_ID_MASK)
hw/i386/intel_iommu.c +7 -4
index 0733180501..9dd41b835b 100644
@@ -3708,8 +3708,9 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
 
     if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
         (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
-        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%" PRIx64
-                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);
+        error_report_once("%s: invalid piotlb inv desc hi=0x%"PRIx64
+                          " lo=0x%"PRIx64" (reserved bits unzero)",
+                          __func__, inv_desc->val[1], inv_desc->val[0]);
         return false;
     }
 
@@ -3728,8 +3729,10 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
         break;
 
     default:
-        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%" PRIx64
-                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);
+        error_report_once("%s: invalid piotlb inv desc: hi=0x%"PRIx64
+                          ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
+                          __func__, inv_desc->val[1], inv_desc->val[0],
+                          inv_desc->val[0] & VTD_INV_DESC_IOTLB_G);
         return false;
     }
     return true;

Thanks
Zhenzhong

#21 lore raw

Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb invalidation

Quoted 150 lines · Zhenzhong

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

Quoted 109 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID- selective PASID-based iotlb invalidation

Quoted 1 line · 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.

Quoted 11 lines

Per spec 6.5.2.4, PADID-selective PASID-based iotlb invalidation will flush stage-2 iotlb entries with matching domain id and pasid.

With scalable modern mode introduced, guest could send PASID-selective PASID-based iotlb invalidation to flush both stage-1 and stage-2 entries.

---
   hw/i386/intel_iommu_internal.h | 10 +++++
   hw/i386/intel_iommu.c          | 78

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

Review

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

b/hw/i386/intel_iommu_internal.h

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

Quoted 5 lines

index 4e0331caba..f71fc91234 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -440,6 +440,16 @@ typedef union VTDInvDesc VTDInvDesc;
           (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM |

VTD_SL_TM)) : \

hw/i386/intel_iommu.c static bool

Quoted 20 lines
hw/i386/intel_iommu_internal.h +10 -0
           (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
+#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
+
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
+
+#define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
+                                             VTD_DOMAIN_ID_MASK)
+
   /* Information about page-selective IOTLB invalidate */
   struct VTDIOTLBPageInvInfo {
       uint16_t domain_id;
hw/i386/intel_iommu.c +0 -0
index 40cbd4a0f4..075a27adac 100644
@@ -2659,6 +2659,80 @@ static bool

vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)

Quoted 45 lines
hw/i386/intel_iommu.c +42 -0
       return true;
   }
 
+static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value,
+                                         gpointer user_data)
+{
+    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
+    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
+
+    return ((entry->domain_id == info->domain_id) &&
+            (entry->pasid == info->pasid));
+}
+
+static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
+                                        uint16_t domain_id, uint32_t pasid)
+{
+    VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+
+    info.domain_id = domain_id;
+    info.pasid = pasid;
+
+    vtd_iommu_lock(s);
+    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
+                                &info);
+    vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            if (!s->scalable_modern) {
+                vtd_address_space_sync(vtd_as);
+            }
+        }
+    }
+}

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
+                                    VTDInvDesc *inv_desc)
+{
+    uint16_t domain_id;
+    uint32_t pasid;
+
+    if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
+        (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
+        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%"

PRIx64

Quoted 4 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

This error is not formatted as the other similar messages we print when reserved bits are non-zero.
Here is what we've done in vtd_process_iotlb_desc:

Sure, will change as below,

Quoted 10 lines
         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
                           ", lo=0x%"PRIx64" (reserved bits unzero)",
                           __func__, inv_desc->hi, inv_desc->lo);

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

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+        return false;
+    }
+
+    domain_id = VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]);
+    pasid = VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]);
+    switch (inv_desc->val[0] & VTD_INV_DESC_IOTLB_G) {

Not critical but why don't we have VTD_INV_DESC_PIOTLB_G?

Will add.

Quoted 10 lines

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

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+    case VTD_INV_DESC_PIOTLB_ALL_IN_PASID:
+        vtd_piotlb_pasid_invalidate(s, domain_id, pasid);
+        break;
+
+    case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
+        break;
+
+    default:
+        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%"

PRIx64

Quoted 3 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

Same comment, I think we should make the messages consistent across descriptor handlers.

What about below:

hw/i386/intel_iommu_internal.h +0 -0
index 3290761595..e76fd9d377 100644

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

Quoted 5 lines
hw/i386/intel_iommu_internal.h +2 -0
@@ -479,9 +479,10 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
  #define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
 
+/* Masks for IOTLB Invalidate Descriptor */
+#define VTD_INV_DESC_IOTLB_G              (3ULL << 4)

This one is already defined

Quoted 11 lines
hw/i386/intel_iommu_internal.h +0 -1
  #define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
  #define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
-
  #define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
  #define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
                                               VTD_DOMAIN_ID_MASK)
hw/i386/intel_iommu.c +0 -0
index 0733180501..9dd41b835b 100644

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

Quoted 10 lines
hw/i386/intel_iommu.c +3 -2
@@ -3708,8 +3708,9 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
 
      if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
          (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
-        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%" PRIx64
-                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);
+        error_report_once("%s: invalid piotlb inv desc hi=0x%"PRIx64
+                          " lo=0x%"PRIx64" (reserved bits unzero)",
+                          __func__, inv_desc->val[1], inv_desc->val[0]);
          return false;
      }

lgtm

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
@@ -3728,8 +3729,10 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
          break;
 

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

Quoted 10 lines
hw/i386/intel_iommu.c +4 -2
      default:
-        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%" PRIx64
-                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);
+        error_report_once("%s: invalid piotlb inv desc: hi=0x%"PRIx64
+                          ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
+                          __func__, inv_desc->val[1], inv_desc->val[0],
+                          inv_desc->val[0] & VTD_INV_DESC_IOTLB_G);
          return false;
      }
      return true;

lgtm

Quoted 3 lines

Thanks
Zhenzhong

Thanks

Quoted 1 line

cmd

#22 lore raw

RE: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID-selective PASID-based iotlb invalidation

Quoted 170 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in PADID- selective PASID-based iotlb invalidation

Quoted 1 line · Zhenzhong

Caution: External email. Do not open attachments or click links, unless this

email comes from a known sender and you know the content is safe.

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 04/17] intel_iommu: Flush stage-2 cache in

PADID-

Quoted 6 lines
Quoted 6 lines

selective PASID-based iotlb invalidation

Quoted 1 line · Zhenzhong Duan

Caution: External email. Do not open attachments or click links, unless

this

Quoted 6 lines
Quoted 6 lines

email comes from a known sender and you know the content is safe.

Quoted 5 lines

Per spec 6.5.2.4, PADID-selective PASID-based iotlb invalidation will flush stage-2 iotlb entries with matching domain id and pasid.

With scalable modern mode introduced, guest could send PASID-

selective

Quoted 38 lines
Quoted 38 lines
Quoted 6 lines

PASID-based iotlb invalidation to flush both stage-1 and stage-2 entries.

---
   hw/i386/intel_iommu_internal.h | 10 +++++
   hw/i386/intel_iommu.c          | 78

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

Review

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

b/hw/i386/intel_iommu_internal.h

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

Quoted 5 lines

index 4e0331caba..f71fc91234 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -440,6 +440,16 @@ typedef union VTDInvDesc VTDInvDesc;
           (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM |

VTD_SL_TM)) : \

hw/i386/intel_iommu.c static bool

Quoted 20 lines
hw/i386/intel_iommu_internal.h +10 -0
           (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
+#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
+
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL0     0xfff000000000ffc0ULL
+#define VTD_INV_DESC_PIOTLB_RSVD_VAL1     0xf80ULL
+
+#define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
+                                             VTD_DOMAIN_ID_MASK)
+
   /* Information about page-selective IOTLB invalidate */
   struct VTDIOTLBPageInvInfo {
       uint16_t domain_id;
hw/i386/intel_iommu.c +0 -0
index 40cbd4a0f4..075a27adac 100644
@@ -2659,6 +2659,80 @@ static bool

vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)

Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
       return true;
   }
 
+static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer

value,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
hw/i386/intel_iommu.c +13 -0
+                                         gpointer user_data)
+{
+    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
+    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
+
+    return ((entry->domain_id == info->domain_id) &&
+            (entry->pasid == info->pasid));
+}
+
+static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
+                                        uint16_t domain_id, uint32_t pasid)
+{
+    VTDIOTLBPageInvInfo info;
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/i386/intel_iommu.c +8 -0
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+
+    info.domain_id = domain_id;
+    info.pasid = pasid;
+
+    vtd_iommu_lock(s);
+    g_hash_table_foreach_remove(s->iotlb,

vtd_hash_remove_by_pasid,

Quoted 59 lines
Quoted 31 lines
Quoted 20 lines
hw/i386/intel_iommu.c +20 -0
+                                &info);
+    vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            if (!s->scalable_modern) {
+                vtd_address_space_sync(vtd_as);
+            }
+        }
+    }
+}

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

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
+                                    VTDInvDesc *inv_desc)
+{
+    uint16_t domain_id;
+    uint32_t pasid;
+
+    if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
+        (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
+        error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%"

PRIx64

Quoted 4 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

This error is not formatted as the other similar messages we print when reserved bits are non-zero.
Here is what we've done in vtd_process_iotlb_desc:

Sure, will change as below,

Quoted 10 lines
         error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
                           ", lo=0x%"PRIx64" (reserved bits unzero)",
                           __func__, inv_desc->hi, inv_desc->lo);

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

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+        return false;
+    }
+
+    domain_id = VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]);
+    pasid = VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]);
+    switch (inv_desc->val[0] & VTD_INV_DESC_IOTLB_G) {

Not critical but why don't we have VTD_INV_DESC_PIOTLB_G?

Will add.

Quoted 10 lines

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

Quoted 9 lines
hw/i386/intel_iommu.c +9 -0
+    case VTD_INV_DESC_PIOTLB_ALL_IN_PASID:
+        vtd_piotlb_pasid_invalidate(s, domain_id, pasid);
+        break;
+
+    case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
+        break;
+
+    default:
+        error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%"

PRIx64

Review

Quoted 6 lines
Quoted 3 lines

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

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                  " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]);

Same comment, I think we should make the messages consistent across descriptor handlers.

What about below:

(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 7 lines

index 3290761595..e76fd9d377 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -479,9 +479,10 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
  #define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
 
Quoted 3 lines

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

Quoted 2 lines
hw/i386/intel_iommu_internal.h +2 -0
+/* Masks for IOTLB Invalidate Descriptor */
+#define VTD_INV_DESC_IOTLB_G              (3ULL << 4)

This one is already defined

Ah, typo, I mean:
+#define VTD_INV_DESC_PIOTLB_G (3ULL << 4)

Thanks
Zhenzhong

#23 lore raw

PATCH 5/17 intel_iommu: Rename slpte to pte

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

Because we will support both FST(a.k.a, FLT) and SST(a.k.a, SLT) translation, rename variable and functions from slpte to pte whenever possible.

But some are SST only, they are renamed with sl_ prefix.

---
 hw/i386/intel_iommu_internal.h |  24 +++---
 include/hw/i386/intel_iommu.h  |   2 +-
 hw/i386/intel_iommu.c          | 129 +++++++++++++++++----------------
 3 files changed, 78 insertions(+), 77 deletions(-)
hw/i386/intel_iommu_internal.h +12 -12
index f71fc91234..1875c2ddd6 100644
@@ -536,24 +536,24 @@ typedef struct VTDRootEntry VTDRootEntry;
 /* Second Level Page Translation Pointer*/
 #define VTD_SM_PASID_ENTRY_SLPTPTR     (~0xfffULL)
 
-/* Paging Structure common */
-#define VTD_SL_PT_PAGE_SIZE_MASK    (1ULL << 7)
-/* Bits to decide the offset for each level */
-#define VTD_SL_LEVEL_BITS           9
-
 /* Second Level Paging Structure */
-#define VTD_SL_PML4_LEVEL           4
-#define VTD_SL_PDP_LEVEL            3
-#define VTD_SL_PD_LEVEL             2
-#define VTD_SL_PT_LEVEL             1
-#define VTD_SL_PT_ENTRY_NR          512
-
 /* Masks for Second Level Paging Entry */
 #define VTD_SL_RW_MASK              3ULL
 #define VTD_SL_R                    1ULL
 #define VTD_SL_W                    (1ULL << 1)
-#define VTD_SL_PT_BASE_ADDR_MASK(aw) (~(VTD_PAGE_SIZE - 1) & VTD_HAW_MASK(aw))
 #define VTD_SL_IGN_COM              0xbff0000000000000ULL
 #define VTD_SL_TM                   (1ULL << 62)
 
+/* Common for both First Level and Second Level */
+#define VTD_PML4_LEVEL           4
+#define VTD_PDP_LEVEL            3
+#define VTD_PD_LEVEL             2
+#define VTD_PT_LEVEL             1
+#define VTD_PT_ENTRY_NR          512
+#define VTD_PT_PAGE_SIZE_MASK    (1ULL << 7)
+#define VTD_PT_BASE_ADDR_MASK(aw) (~(VTD_PAGE_SIZE - 1) & VTD_HAW_MASK(aw))
+
+/* Bits to decide the offset for each level */
+#define VTD_LEVEL_BITS           9
+
 #endif
include/hw/i386/intel_iommu.h +1 -1
index 788ed42477..fe9057c50d 100644
@@ -152,7 +152,7 @@ struct VTDIOTLBEntry {
     uint64_t gfn;
     uint16_t domain_id;
     uint32_t pasid;
-    uint64_t slpte;
+    uint64_t pte;
     uint64_t mask;
     uint8_t access_flags;
 };
hw/i386/intel_iommu.c +65 -64

344 lines not on this page · raw message

Signature · 1 line
2.34.1
#24 lore raw

PATCH 6/17 intel_iommu: Implement stage-1 translation

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds stage-1 page table walking to support stage-1 only transltion in scalable modern mode.

---
 hw/i386/intel_iommu_internal.h |  26 ++++++
 hw/i386/intel_iommu.c          | 146 ++++++++++++++++++++++++++++++++-
 2 files changed, 168 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_internal.h +26 -0
index 1875c2ddd6..36fcc6bb5e 100644
@@ -440,6 +440,24 @@ typedef union VTDInvDesc VTDInvDesc;
         (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
         (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
 
+/* Rsvd field masks for fpte */
+#define VTD_FS_UPPER_IGNORED 0xfff0000000000000ULL
+#define VTD_FPTE_PAGE_L1_RSVD_MASK(aw) (~(VTD_HAW_MASK(aw)) & \
+                                       (~VTD_FS_UPPER_IGNORED))
+#define VTD_FPTE_PAGE_L2_RSVD_MASK(aw) (~(VTD_HAW_MASK(aw)) & \
+                                       (~VTD_FS_UPPER_IGNORED))
+#define VTD_FPTE_PAGE_L3_RSVD_MASK(aw) (~(VTD_HAW_MASK(aw)) & \
+                                       (~VTD_FS_UPPER_IGNORED))
+#define VTD_FPTE_PAGE_L3_FS1GP_RSVD_MASK(aw) ((0x3fffe000ULL | \
+                                                ~(VTD_HAW_MASK(aw))) \
+                                              & (~VTD_FS_UPPER_IGNORED))
+#define VTD_FPTE_PAGE_L2_FS2MP_RSVD_MASK(aw) ((0x1fe000ULL | \
+                                                ~(VTD_HAW_MASK(aw))) \
+                                              & (~VTD_FS_UPPER_IGNORED))
+#define VTD_FPTE_PAGE_L4_RSVD_MASK(aw) ((0x80ULL | \
+                                            ~(VTD_HAW_MASK(aw))) \
+                                        & (~VTD_FS_UPPER_IGNORED))
+
 #define VTD_INV_DESC_PIOTLB_ALL_IN_PASID  (2ULL << 4)
 #define VTD_INV_DESC_PIOTLB_PSI_IN_PASID  (3ULL << 4)
 
@@ -533,6 +551,14 @@ typedef struct VTDRootEntry VTDRootEntry;
 #define VTD_SM_PASID_ENTRY_AW          7ULL /* Adjusted guest-address-width */
 #define VTD_SM_PASID_ENTRY_DID(val)    ((val) & VTD_DOMAIN_ID_MASK)
 
+#define VTD_SM_PASID_ENTRY_FLPM          3ULL
+#define VTD_SM_PASID_ENTRY_FLPTPTR       (~0xfffULL)
+
+/* First Level Paging Structure */
+/* Masks for First Level Paging Entry */
+#define VTD_FL_P                    1ULL
+#define VTD_FL_RW_MASK              (1ULL << 1)
+
 /* Second Level Page Translation Pointer*/
 #define VTD_SM_PASID_ENTRY_SLPTPTR     (~0xfffULL)
 
hw/i386/intel_iommu.c +142 -4

194 lines not on this page · raw message

Signature · 1 line
2.34.1
#25 lore raw

PATCH 7/17 intel_iommu: Check if the input address is canonical

In reply to Zhenzhong Duan

From: Clément Mathieu--Drif <[email protected]>

First stage translation must fail if the address to translate is not canonical.

---
 hw/i386/intel_iommu_internal.h |  2 ++
 hw/i386/intel_iommu.c          | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)
hw/i386/intel_iommu_internal.h +2 -0
index 36fcc6bb5e..168185b850 100644
@@ -322,6 +322,8 @@ typedef enum VTDFaultReason {
     VTD_FR_PASID_ENTRY_P = 0x59,
     VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry */
 
+    VTD_FR_FS_NON_CANONICAL = 0x80, /* SNG.1 : Address for FS not canonical.*/
+
     /* Output address in the interrupt address range for scalable mode */
     VTD_FR_SM_INTERRUPT_ADDR = 0x87,
     VTD_FR_MAX,                 /* Guard */
hw/i386/intel_iommu.c +21 -0
index 287741b687..495a41cf80 100644
@@ -1824,6 +1824,7 @@ static const bool vtd_qualified_faults[] = {
     [VTD_FR_PASID_ENTRY_P] = true,
     [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
     [VTD_FR_SM_INTERRUPT_ADDR] = true,
+    [VTD_FR_FS_NON_CANONICAL] = true,
     [VTD_FR_MAX] = false,
 };
 
@@ -1927,6 +1928,20 @@ static inline bool vtd_flpte_present(uint64_t flpte)
     return !!(flpte & VTD_FL_P);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s, uint64_t iova,
+                                        VTDContextEntry *ce, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, s->aw_bits, pasid);
+    uint64_t upper_bits_mask = ~(iova_limit - 1);
+    uint64_t upper_bits = iova & upper_bits_mask;
+    bool msb = ((iova & (iova_limit >> 1)) != 0);
+    return !(
+             (!msb && (upper_bits != 0)) ||
+             (msb && (upper_bits != upper_bits_mask))
+            );
+}
+
 /*
  * Given the @iova, get relevant @flptep. @flpte_level will be the last level
  * of the translation, can be used for deciding the size of large page.
@@ -1942,6 +1957,12 @@ static int vtd_iova_to_flpte(IntelIOMMUState *s, VTDContextEntry *ce,
     uint32_t offset;
     uint64_t flpte;
 
+    if (!vtd_iova_fl_check_canonical(s, iova, ce, pasid)) {
+        error_report_once("%s: detected non canonical IOVA (iova=0x%" PRIx64 ","
+                          "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
+        return -VTD_FR_FS_NON_CANONICAL;
+    }
+
     while (true) {
         offset = vtd_iova_level_offset(iova, level);
         flpte = vtd_get_pte(addr, offset);
Signature · 1 line
2.34.1
#26 lore raw

PATCH 8/17 intel_iommu: Set accessed and dirty bits during first stage translation

In reply to Zhenzhong Duan

From: Clément Mathieu--Drif <[email protected]>

---
 hw/i386/intel_iommu_internal.h |  3 +++
 hw/i386/intel_iommu.c          | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
hw/i386/intel_iommu_internal.h +3 -0
index 168185b850..cf0f176e06 100644
@@ -326,6 +326,7 @@ typedef enum VTDFaultReason {
 
     /* Output address in the interrupt address range for scalable mode */
     VTD_FR_SM_INTERRUPT_ADDR = 0x87,
+    VTD_FR_FS_BIT_UPDATE_FAILED = 0x91, /* SFS.10 */
     VTD_FR_MAX,                 /* Guard */
 } VTDFaultReason;
 
@@ -560,6 +561,8 @@ typedef struct VTDRootEntry VTDRootEntry;
 /* Masks for First Level Paging Entry */
 #define VTD_FL_P                    1ULL
 #define VTD_FL_RW_MASK              (1ULL << 1)
+#define VTD_FL_A                    0x20
+#define VTD_FL_D                    0x40
 
 /* Second Level Page Translation Pointer*/
 #define VTD_SM_PASID_ENTRY_SLPTPTR     (~0xfffULL)
hw/i386/intel_iommu.c +24 -0
index 495a41cf80..210df32f01 100644
@@ -1825,6 +1825,7 @@ static const bool vtd_qualified_faults[] = {
     [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
     [VTD_FR_SM_INTERRUPT_ADDR] = true,
     [VTD_FR_FS_NON_CANONICAL] = true,
+    [VTD_FR_FS_BIT_UPDATE_FAILED] = true,
     [VTD_FR_MAX] = false,
 };
 
@@ -1942,6 +1943,20 @@ static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s, uint64_t iova,
             );
 }
 
+static MemTxResult vtd_set_flag_in_pte(dma_addr_t base_addr, uint32_t index,
+                                       uint64_t pte, uint64_t flag)
+{
+    if (pte & flag) {
+        return MEMTX_OK;
+    }
+    pte |= flag;
+    pte = cpu_to_le64(pte);
+    return dma_memory_write(&address_space_memory,
+                            base_addr + index * sizeof(pte),
+                            &pte, sizeof(pte),
+                            MEMTXATTRS_UNSPECIFIED);
+}
+
 /*
  * Given the @iova, get relevant @flptep. @flpte_level will be the last level
  * of the translation, can be used for deciding the size of large page.
@@ -1993,7 +2008,16 @@ static int vtd_iova_to_flpte(IntelIOMMUState *s, VTDContextEntry *ce,
             return -VTD_FR_PAGING_ENTRY_RSVD;
         }
 
+        if (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_A) != MEMTX_OK) {
+            return -VTD_FR_FS_BIT_UPDATE_FAILED;
+        }
+
         if (vtd_is_last_pte(flpte, level)) {
+            if (is_write &&
+                (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_D) !=
+                                                                    MEMTX_OK)) {
+                    return -VTD_FR_FS_BIT_UPDATE_FAILED;
+            }
             *flptep = flpte;
             *flpte_level = level;
             return 0;
Signature · 1 line
2.34.1
#27 lore raw

PATCH 9/17 intel_iommu: Flush stage-1 cache in iotlb invalidation

In reply to Zhenzhong Duan

According to spec, Page-Selective-within-Domain Invalidation (11b):

1. IOTLB entries caching second-stage mappings (PGTT=010b) or pass-through (PGTT=100b) mappings associated with the specified domain-id and the input-address range are invalidated.
2. IOTLB entries caching first-stage (PGTT=001b) or nested (PGTT=011b) mapping associated with specified domain-id are invalidated.

So per spec definition the Page-Selective-within-Domain Invalidation needs to flush first stage and nested cached IOTLB enties as well.

We don't support nested yet and pass-through mapping is never cached, so what in iotlb cache are only first-stage and second-stage mappings.

Add a tag pgtt in VTDIOTLBEntry to mark PGTT type of the mapping and invalidate entries based on PGTT type.

---
 include/hw/i386/intel_iommu.h |  1 +
 hw/i386/intel_iommu.c         | 27 +++++++++++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index fe9057c50d..b843d069cc 100644
@@ -155,6 +155,7 @@ struct VTDIOTLBEntry {
     uint64_t pte;
     uint64_t mask;
     uint8_t access_flags;
+    uint8_t pgtt;
 };
 
 /* VT-d Source-ID Qualifier types */
hw/i386/intel_iommu.c +21 -6
index 210df32f01..8d47e5ba78 100644
@@ -305,9 +305,21 @@ static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
     VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
     uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
     uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
-    return (entry->domain_id == info->domain_id) &&
-            (((entry->gfn & info->mask) == gfn) ||
-             (entry->gfn == gfn_tlb));
+
+    if (entry->domain_id != info->domain_id) {
+        return false;
+    }
+
+    /*
+     * According to spec, IOTLB entries caching first-stage (PGTT=001b) or
+     * nested (PGTT=011b) mapping associated with specified domain-id are
+     * invalidated. Nested isn't supported yet, so only need to check 001b.
+     */
+    if (entry->pgtt == VTD_SM_PASID_ENTRY_FLT) {
+        return true;
+    }
+
+    return (entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb;
 }
 
 /* Reset all the gen of VTDAddressSpace to zero and set the gen of
@@ -382,7 +394,7 @@ out:
 static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                              uint16_t domain_id, hwaddr addr, uint64_t pte,
                              uint8_t access_flags, uint32_t level,
-                             uint32_t pasid)
+                             uint32_t pasid, uint8_t pgtt)
 {
     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
     struct vtd_iotlb_key *key = g_malloc(sizeof(*key));
@@ -400,6 +412,7 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     entry->access_flags = access_flags;
     entry->mask = vtd_pt_level_page_mask(level);
     entry->pasid = pasid;
+    entry->pgtt = pgtt;
 
     key->gfn = gfn;
     key->sid = source_id;
@@ -2071,7 +2084,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     bool is_fpd_set = false;
     bool reads = true;
     bool writes = true;
-    uint8_t access_flags;
+    uint8_t access_flags, pgtt;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
 
@@ -2179,9 +2192,11 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     if (s->scalable_modern && s->root_scalable) {
         ret_fr = vtd_iova_to_flpte(s, &ce, addr, is_write, &pte, &level,
                                    &reads, &writes, s->aw_bits, pasid);
+        pgtt = VTD_SM_PASID_ENTRY_FLT;
     } else {
         ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &pte, &level,
                                    &reads, &writes, s->aw_bits, pasid);
+        pgtt = VTD_SM_PASID_ENTRY_SLT;
     }
     if (ret_fr) {
         vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
@@ -2192,7 +2207,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     page_mask = vtd_pt_level_page_mask(level);
     access_flags = IOMMU_ACCESS_FLAG(reads, writes);
     vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid),
-                     addr, pte, access_flags, level, pasid);
+                     addr, pte, access_flags, level, pasid, pgtt);
 out:
     vtd_iommu_unlock(s);
     entry->iova = addr & page_mask;
Signature · 1 line
2.34.1
Reviewed-by on v1 9/17
#28 lore raw

Re: [PATCH v1 09/17] intel_iommu: Flush stage-1 cache in iotlb invalidation

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

According to spec, Page-Selective-within-Domain Invalidation (11b):

1. IOTLB entries caching second-stage mappings (PGTT=010b) or pass-through (PGTT=100b) mappings associated with the specified domain-id and the input-address range are invalidated.
2. IOTLB entries caching first-stage (PGTT=001b) or nested (PGTT=011b) mapping associated with specified domain-id are invalidated.

So per spec definition the Page-Selective-within-Domain Invalidation needs to flush first stage and nested cached IOTLB enties as well.

We don't support nested yet and pass-through mapping is never cached, so what in iotlb cache are only first-stage and second-stage mappings.

Add a tag pgtt in VTDIOTLBEntry to mark PGTT type of the mapping and invalidate entries based on PGTT type.

---
  include/hw/i386/intel_iommu.h |  1 +
  hw/i386/intel_iommu.c         | 27 +++++++++++++++++++++------
  2 files changed, 22 insertions(+), 6 deletions(-)
include/hw/i386/intel_iommu.h +1 -0
index fe9057c50d..b843d069cc 100644
@@ -155,6 +155,7 @@ struct VTDIOTLBEntry {
      uint64_t pte;
      uint64_t mask;
      uint8_t access_flags;
+    uint8_t pgtt;
  };
 
  /* VT-d Source-ID Qualifier types */
hw/i386/intel_iommu.c +21 -7
index 210df32f01..8d47e5ba78 100644
@@ -305,9 +305,21 @@ static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
      VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
      uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
      uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
-    return (entry->domain_id == info->domain_id) &&
-            (((entry->gfn & info->mask) == gfn) ||
-             (entry->gfn == gfn_tlb));
+
+    if (entry->domain_id != info->domain_id) {
+        return false;
+    }
+
+    /*
+     * According to spec, IOTLB entries caching first-stage (PGTT=001b) or
+     * nested (PGTT=011b) mapping associated with specified domain-id are
+     * invalidated. Nested isn't supported yet, so only need to check 001b.
+     */
+    if (entry->pgtt == VTD_SM_PASID_ENTRY_FLT) {
+        return true;
+    }
+
+    return (entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb;
  }
 
  /* Reset all the gen of VTDAddressSpace to zero and set the gen of
@@ -382,7 +394,7 @@ out:
  static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                               uint16_t domain_id, hwaddr addr, uint64_t pte,
                               uint8_t access_flags, uint32_t level,
-                             uint32_t pasid)
+                             uint32_t pasid, uint8_t pgtt)
  {
      VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
      struct vtd_iotlb_key *key = g_malloc(sizeof(*key));
@@ -400,6 +412,7 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
      entry->access_flags = access_flags;
      entry->mask = vtd_pt_level_page_mask(level);
      entry->pasid = pasid;
+    entry->pgtt = pgtt;
 
      key->gfn = gfn;
      key->sid = source_id;
@@ -2071,7 +2084,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      bool is_fpd_set = false;
      bool reads = true;
      bool writes = true;
-    uint8_t access_flags;
+    uint8_t access_flags, pgtt;
      bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
      VTDIOTLBEntry *iotlb_entry;
 
@@ -2179,9 +2192,11 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      if (s->scalable_modern && s->root_scalable) {
          ret_fr = vtd_iova_to_flpte(s, &ce, addr, is_write, &pte, &level,
                                     &reads, &writes, s->aw_bits, pasid);
+        pgtt = VTD_SM_PASID_ENTRY_FLT;
      } else {
          ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &pte, &level,
                                     &reads, &writes, s->aw_bits, pasid);
+        pgtt = VTD_SM_PASID_ENTRY_SLT;
      }
      if (ret_fr) {
          vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
@@ -2192,7 +2207,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      page_mask = vtd_pt_level_page_mask(level);
      access_flags = IOMMU_ACCESS_FLAG(reads, writes);
      vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid),
-                     addr, pte, access_flags, level, pasid);
+                     addr, pte, access_flags, level, pasid, pgtt);
  out:
      vtd_iommu_unlock(s);
      entry->iova = addr & page_mask;
--

2.34.1

#29 lore raw

PATCH 10/17 intel_iommu: Process PASID-based iotlb invalidation

In reply to Zhenzhong Duan

PASID-based iotlb (piotlb) is used during walking Intel VT-d stage-1 page table.

This emulates the stage-1 page table iotlb invalidation requested by a PASID-based IOTLB Invalidate Descriptor (P_IOTLB).

---
 hw/i386/intel_iommu_internal.h |  3 +++
 hw/i386/intel_iommu.c          | 45 ++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
hw/i386/intel_iommu_internal.h +3 -0
index cf0f176e06..7dd8176e86 100644
@@ -470,6 +470,9 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
 #define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
                                              VTD_DOMAIN_ID_MASK)
+#define VTD_INV_DESC_PIOTLB_ADDR(val)     ((val) & ~0xfffULL)
+#define VTD_INV_DESC_PIOTLB_AM(val)       ((val) & 0x3fULL)
+#define VTD_INV_DESC_PIOTLB_IH(val)       (((val) >> 6) & 0x1)
 
 /* Information about page-selective IOTLB invalidate */
 struct VTDIOTLBPageInvInfo {
hw/i386/intel_iommu.c +45 -0

68 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 10/17
#30 lore raw

Re: [PATCH v1 10/17] intel_iommu: Process PASID-based iotlb invalidation

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

PASID-based iotlb (piotlb) is used during walking Intel VT-d stage-1 page table.

This emulates the stage-1 page table iotlb invalidation requested by a PASID-based IOTLB Invalidate Descriptor (P_IOTLB).

---
  hw/i386/intel_iommu_internal.h |  3 +++
  hw/i386/intel_iommu.c          | 45 ++++++++++++++++++++++++++++++++++
  2 files changed, 48 insertions(+)
hw/i386/intel_iommu_internal.h +3 -0
index cf0f176e06..7dd8176e86 100644
@@ -470,6 +470,9 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_PIOTLB_PASID(val)    (((val) >> 32) & 0xfffffULL)
  #define VTD_INV_DESC_PIOTLB_DID(val)      (((val) >> 16) & \
                                               VTD_DOMAIN_ID_MASK)
+#define VTD_INV_DESC_PIOTLB_ADDR(val)     ((val) & ~0xfffULL)
+#define VTD_INV_DESC_PIOTLB_AM(val)       ((val) & 0x3fULL)
+#define VTD_INV_DESC_PIOTLB_IH(val)       (((val) >> 6) & 0x1)
 
  /* Information about page-selective IOTLB invalidate */
  struct VTDIOTLBPageInvInfo {
hw/i386/intel_iommu.c +45 -1
index 8d47e5ba78..8ebb6dbd7d 100644
@@ -322,6 +322,28 @@ static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
      return (entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb;
  }
 
+static gboolean vtd_hash_remove_by_page_piotlb(gpointer key, gpointer value,
+                                               gpointer user_data)
+{
+    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
+    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
+    uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
+    uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
+
+    /*
+     * According to spec, PASID-based-IOTLB Invalidation in page granularity
+     * doesn't invalidate IOTLB entries caching second-stage (PGTT=010b)
+     * or pass-through (PGTT=100b) mappings. Nested isn't supported yet,
+     * so only need to check first-stage (PGTT=001b) mappings.
+     */
+    if (entry->pgtt != VTD_SM_PASID_ENTRY_FLT) {
+        return false;
+    }
+
+    return entry->domain_id == info->domain_id && entry->pasid == info->pasid &&
+           ((entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb);
+}
+
  /* Reset all the gen of VTDAddressSpace to zero and set the gen of
   * IntelIOMMUState to 1.  Must be called with IOMMU lock held.
   */
@@ -2886,11 +2908,30 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
      }
  }
 
+static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
+                                       uint32_t pasid, hwaddr addr, uint8_t am,
+                                       bool ih)
+{
+    VTDIOTLBPageInvInfo info;
+
+    info.domain_id = domain_id;
+    info.pasid = pasid;
+    info.addr = addr;
+    info.mask = ~((1 << am) - 1);
+
+    vtd_iommu_lock(s);
+    g_hash_table_foreach_remove(s->iotlb,
+                                vtd_hash_remove_by_page_piotlb, &info);
+    vtd_iommu_unlock(s);
+}
+
  static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
                                      VTDInvDesc *inv_desc)
  {
      uint16_t domain_id;
      uint32_t pasid;
+    uint8_t am;
+    hwaddr addr;
 
      if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) ||
          (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) {
@@ -2907,6 +2948,10 @@ static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
          break;
 
      case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
+        am = VTD_INV_DESC_PIOTLB_AM(inv_desc->val[1]);
+        addr = (hwaddr) VTD_INV_DESC_PIOTLB_ADDR(inv_desc->val[1]);
+        vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am,
+                                   VTD_INV_DESC_PIOTLB_IH(inv_desc->val[1]));
          break;
 
      default:
--

2.34.1

#31 lore raw

PATCH 11/17 intel_iommu: Extract device IOTLB invalidation logic

In reply to Zhenzhong Duan

From: Clément Mathieu--Drif <[email protected]>

This piece of code can be shared by both IOTLB invalidation and PASID-based IOTLB invalidation

No functional changes intended.

---
 hw/i386/intel_iommu.c | 57 +++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 24 deletions(-)
hw/i386/intel_iommu.c +33 -24
index 8ebb6dbd7d..4d5a457f92 100644
@@ -2975,13 +2975,43 @@ static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
     return true;
 }
 
+static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
+                                     bool size, hwaddr addr)
+{
+    /*
+     * According to ATS spec table 2.4:
+     * S = 0, bits 15:12 = xxxx     range size: 4K
+     * S = 1, bits 15:12 = xxx0     range size: 8K
+     * S = 1, bits 15:12 = xx01     range size: 16K
+     * S = 1, bits 15:12 = x011     range size: 32K
+     * S = 1, bits 15:12 = 0111     range size: 64K
+     * ...
+     */
+
+    IOMMUTLBEvent event;
+    uint64_t sz;
+
+    if (size) {
+        sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
+        addr &= ~(sz - 1);
+    } else {
+        sz = VTD_PAGE_SIZE;
+    }
+
+    event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
+    event.entry.target_as = &vtd_dev_as->as;
+    event.entry.addr_mask = sz - 1;
+    event.entry.iova = addr;
+    event.entry.perm = IOMMU_NONE;
+    event.entry.translated_addr = 0;
+    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
+}
+
 static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
                                           VTDInvDesc *inv_desc)
 {
     VTDAddressSpace *vtd_dev_as;
-    IOMMUTLBEvent event;
     hwaddr addr;
-    uint64_t sz;
     uint16_t sid;
     bool size;
 
@@ -3006,28 +3036,7 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
         goto done;
     }
 
-    /* According to ATS spec table 2.4:
-     * S = 0, bits 15:12 = xxxx     range size: 4K
-     * S = 1, bits 15:12 = xxx0     range size: 8K
-     * S = 1, bits 15:12 = xx01     range size: 16K
-     * S = 1, bits 15:12 = x011     range size: 32K
-     * S = 1, bits 15:12 = 0111     range size: 64K
-     * ...
-     */
-    if (size) {
-        sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
-        addr &= ~(sz - 1);
-    } else {
-        sz = VTD_PAGE_SIZE;
-    }
-
-    event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
-    event.entry.target_as = &vtd_dev_as->as;
-    event.entry.addr_mask = sz - 1;
-    event.entry.iova = addr;
-    event.entry.perm = IOMMU_NONE;
-    event.entry.translated_addr = 0;
-    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
+    do_invalidate_device_tlb(vtd_dev_as, size, addr);
 
 done:
     return true;
Signature · 1 line
2.34.1
#32 lore raw

Re: [PATCH v1 11/17] intel_iommu: Extract device IOTLB invalidation logic

Hi Zhenzhong,

This patch has been merged into staging this morning, be careful when re-sending your series.
Here is the link :
https://github.com/qemu/qemu/commit/6410f877f5ed535acd01bbfaa4baec379e44d0ef#diff-c19adbf518f644e9b651b67266802e14787292ab9d6cd4210b4f974585be6009

Quoted 1 line

cmd

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

From: Clément Mathieu--Drif <[email protected]>

This piece of code can be shared by both IOTLB invalidation and PASID-based IOTLB invalidation

No functional changes intended.

---
  hw/i386/intel_iommu.c | 57 +++++++++++++++++++++++++------------------
  1 file changed, 33 insertions(+), 24 deletions(-)
hw/i386/intel_iommu.c +33 -25
index 8ebb6dbd7d..4d5a457f92 100644
@@ -2975,13 +2975,43 @@ static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
      return true;
  }
 
+static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
+                                     bool size, hwaddr addr)
+{
+    /*
+     * According to ATS spec table 2.4:
+     * S = 0, bits 15:12 = xxxx     range size: 4K
+     * S = 1, bits 15:12 = xxx0     range size: 8K
+     * S = 1, bits 15:12 = xx01     range size: 16K
+     * S = 1, bits 15:12 = x011     range size: 32K
+     * S = 1, bits 15:12 = 0111     range size: 64K
+     * ...
+     */
+
+    IOMMUTLBEvent event;
+    uint64_t sz;
+
+    if (size) {
+        sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
+        addr &= ~(sz - 1);
+    } else {
+        sz = VTD_PAGE_SIZE;
+    }
+
+    event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
+    event.entry.target_as = &vtd_dev_as->as;
+    event.entry.addr_mask = sz - 1;
+    event.entry.iova = addr;
+    event.entry.perm = IOMMU_NONE;
+    event.entry.translated_addr = 0;
+    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
+}
+
  static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
                                            VTDInvDesc *inv_desc)
  {
      VTDAddressSpace *vtd_dev_as;
-    IOMMUTLBEvent event;
      hwaddr addr;
-    uint64_t sz;
      uint16_t sid;
      bool size;
 
@@ -3006,28 +3036,7 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
          goto done;
      }
 
-    /* According to ATS spec table 2.4:
-     * S = 0, bits 15:12 = xxxx     range size: 4K
-     * S = 1, bits 15:12 = xxx0     range size: 8K
-     * S = 1, bits 15:12 = xx01     range size: 16K
-     * S = 1, bits 15:12 = x011     range size: 32K
-     * S = 1, bits 15:12 = 0111     range size: 64K
-     * ...
-     */
-    if (size) {
-        sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
-        addr &= ~(sz - 1);
-    } else {
-        sz = VTD_PAGE_SIZE;
-    }
-
-    event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
-    event.entry.target_as = &vtd_dev_as->as;
-    event.entry.addr_mask = sz - 1;
-    event.entry.iova = addr;
-    event.entry.perm = IOMMU_NONE;
-    event.entry.translated_addr = 0;
-    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
+    do_invalidate_device_tlb(vtd_dev_as, size, addr);
 
  done:
      return true;
--

2.34.1

#33 lore raw

RE: [PATCH v1 11/17] intel_iommu: Extract device IOTLB invalidation logic

Sure, thanks for reminding.

BRs.
Zhenzhong

Quoted 7 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 11/17] intel_iommu: Extract device IOTLB invalidation logic

Hi Zhenzhong,

Quoted 7 lines

This patch has been merged into staging this morning, be careful when re-sending your series.
Here is the link :
https://github.com/qemu/qemu/commit/6410f877f5ed535acd01bbfaa4ba
ec379e44d0ef#diff- c19adbf518f644e9b651b67266802e14787292ab9d6cd4210b4f974585be6 009

Sure, thanks for reminding😊

BRs.
Zhenzhong

#34 lore raw

PATCH 12/17 intel_iommu: Add an internal API to find an address space with PASID

In reply to Zhenzhong Duan

From: Clément Mathieu--Drif <[email protected]>

This will be used to implement the device IOTLB invalidation

---
 hw/i386/intel_iommu.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)
hw/i386/intel_iommu.c +24 -15
index 4d5a457f92..a17ce2b1f1 100644
@@ -70,6 +70,11 @@ struct vtd_hiod_key {
     uint8_t devfn;
 };
 
+struct vtd_as_raw_key {
+    uint16_t sid;
+    uint32_t pasid;
+};
+
 struct vtd_iotlb_key {
     uint64_t gfn;
     uint32_t pasid;
@@ -1878,29 +1883,33 @@ static inline bool vtd_is_interrupt_addr(hwaddr addr)
     return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
 }
 
-static gboolean vtd_find_as_by_sid(gpointer key, gpointer value,
-                                   gpointer user_data)
+static gboolean vtd_find_as_by_sid_and_pasid(gpointer key, gpointer value,
+                                             gpointer user_data)
 {
     struct vtd_as_key *as_key = (struct vtd_as_key *)key;
-    uint16_t target_sid = *(uint16_t *)user_data;
+    struct vtd_as_raw_key target = *(struct vtd_as_raw_key *)user_data;
     uint16_t sid = PCI_BUILD_BDF(pci_bus_num(as_key->bus), as_key->devfn);
-    return sid == target_sid;
+
+    return (as_key->pasid == target.pasid) &&
+           (sid == target.sid);
 }
 
-static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
+static VTDAddressSpace *vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,
+                                                    uint16_t sid,
+                                                    uint32_t pasid)
 {
-    uint8_t bus_num = PCI_BUS_NUM(sid);
-    VTDAddressSpace *vtd_as = s->vtd_as_cache[bus_num];
-
-    if (vtd_as &&
-        (sid == PCI_BUILD_BDF(pci_bus_num(vtd_as->bus), vtd_as->devfn))) {
-        return vtd_as;
-    }
+    struct vtd_as_raw_key key = {
+        .sid = sid,
+        .pasid = pasid
+    };
 
-    vtd_as = g_hash_table_find(s->vtd_address_spaces, vtd_find_as_by_sid, &sid);
-    s->vtd_as_cache[bus_num] = vtd_as;
+    return g_hash_table_find(s->vtd_address_spaces,
+                             vtd_find_as_by_sid_and_pasid, &key);
+}
 
-    return vtd_as;
+static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
+{
+    return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
 }
 
 static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
Signature · 1 line
2.34.1
#35 lore raw

PATCH 13/17 intel_iommu: Add support for PASID-based device IOTLB invalidation

In reply to Zhenzhong Duan

From: Clément Mathieu--Drif <[email protected]>

---
 hw/i386/intel_iommu_internal.h | 11 ++++++++
 hw/i386/intel_iommu.c          | 50 ++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
hw/i386/intel_iommu_internal.h +11 -0
index 7dd8176e86..ed358aa763 100644
@@ -377,6 +377,7 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_WAIT               0x5 /* Invalidation Wait Descriptor */
 #define VTD_INV_DESC_PIOTLB             0x6 /* PASID-IOTLB Invalidate Desc */
 #define VTD_INV_DESC_PC                 0x7 /* PASID-cache Invalidate Desc */
+#define VTD_INV_DESC_DEV_PIOTLB         0x8 /* PASID-based-DIOTLB inv_desc*/
 #define VTD_INV_DESC_NONE               0   /* Not an Invalidate Descriptor */
 
 /* Masks for Invalidation Wait Descriptor*/
@@ -420,6 +421,16 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
 
+/* Mask for PASID Device IOTLB Invalidate Descriptor */
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_ADDR(val) ((val) & \
+                                                   0xfffffffffffff000ULL)
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_SIZE(val) ((val >> 11) & 0x1)
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_GLOBAL(val) ((val) & 0x1)
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(val) (((val) >> 16) & 0xffffULL)
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_PASID(val) ((val >> 32) & 0xfffffULL)
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_HI 0x7feULL
+#define VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_LO 0xfff000000000f000ULL
+
 /* Rsvd field masks for spte */
 #define VTD_SPTE_SNP 0x800ULL
 
hw/i386/intel_iommu.c +50 -0

62 lines not on this page · raw message

Signature · 1 line
2.34.1
#36 lore raw

PATCH 14/17 intel_iommu: piotlb invalidation should notify unmap

In reply to Zhenzhong Duan

This is used by some emulated devices which caches address translation result. When piotlb invalidation issued in guest, those caches should be refreshed.

---
 hw/i386/intel_iommu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +34 -1
index 8b66d6cfa5..c0116497b1 100644
@@ -2910,7 +2910,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
                 continue;
             }
 
-            if (!s->scalable_modern) {
+            if (!s->scalable_modern || !vtd_as_has_map_notifier(vtd_as)) {
                 vtd_address_space_sync(vtd_as);
             }
         }
@@ -2922,6 +2922,9 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                        bool ih)
 {
     VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
     info.domain_id = domain_id;
     info.pasid = pasid;
@@ -2932,6 +2935,36 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     g_hash_table_foreach_remove(s->iotlb,
                                 vtd_hash_remove_by_page_piotlb, &info);
     vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+            IOMMUTLBEvent event;
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            /*
+             * Page-Selective-within-PASID PASID-based-IOTLB Invalidation
+             * does not flush stage-2 entries. See spec section 6.5.2.4
+             */
+            if (!s->scalable_modern) {
+                continue;
+            }
+
+            event.type = IOMMU_NOTIFIER_UNMAP;
+            event.entry.target_as = &address_space_memory;
+            event.entry.iova = addr;
+            event.entry.perm = IOMMU_NONE;
+            event.entry.addr_mask = size - 1;
+            event.entry.translated_addr = 0;
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+        }
+    }
 }
 
 static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
Signature · 1 line
2.34.1
#37 lore raw

Re: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Maybe I'm missing something but why do we invalidate device IOTLB upon piotlb receipt of a regular IOTLB inv desc?
I don't get why we don't wait for a device IOTLB inv desc?

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

This is used by some emulated devices which caches address translation result. When piotlb invalidation issued in guest, those caches should be refreshed.

---
  hw/i386/intel_iommu.c | 35 ++++++++++++++++++++++++++++++++++-
  1 file changed, 34 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +34 -2
index 8b66d6cfa5..c0116497b1 100644
@@ -2910,7 +2910,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
                  continue;
              }
 
-            if (!s->scalable_modern) {
+            if (!s->scalable_modern || !vtd_as_has_map_notifier(vtd_as)) {
                  vtd_address_space_sync(vtd_as);
              }
          }
@@ -2922,6 +2922,9 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                         bool ih)
  {
      VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
      info.domain_id = domain_id;
      info.pasid = pasid;
@@ -2932,6 +2935,36 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
      g_hash_table_foreach_remove(s->iotlb,
                                  vtd_hash_remove_by_page_piotlb, &info);
      vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+            IOMMUTLBEvent event;
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            /*
+             * Page-Selective-within-PASID PASID-based-IOTLB Invalidation
+             * does not flush stage-2 entries. See spec section 6.5.2.4
+             */
+            if (!s->scalable_modern) {
+                continue;
+            }
+
+            event.type = IOMMU_NOTIFIER_UNMAP;
+            event.entry.target_as = &address_space_memory;
+            event.entry.iova = addr;
+            event.entry.perm = IOMMU_NONE;
+            event.entry.addr_mask = size - 1;
+            event.entry.translated_addr = 0;
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+        }
+    }
  }
 
  static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
--

2.34.1

#38 lore raw

Re: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Quoted 3 lines · CLEMENT MATHIEU--DRIF

Maybe I'm missing something but why do we invalidate device IOTLB upon piotlb receipt of a regular IOTLB inv desc?
I don't get why we don't wait for a device IOTLB inv desc?

I thought you were planning to remove that after the last rfc version

Quoted 78 lines · Zhenzhong Duan
Quoted 76 lines

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.

This is used by some emulated devices which caches address translation result. When piotlb invalidation issued in guest, those caches should be refreshed.

---
   hw/i386/intel_iommu.c | 35 ++++++++++++++++++++++++++++++++++-
   1 file changed, 34 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +34 -2
index 8b66d6cfa5..c0116497b1 100644
@@ -2910,7 +2910,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
                   continue;
               }
 
-            if (!s->scalable_modern) {
+            if (!s->scalable_modern || !vtd_as_has_map_notifier(vtd_as)) {
                   vtd_address_space_sync(vtd_as);
               }
           }
@@ -2922,6 +2922,9 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                          bool ih)
   {
       VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
       info.domain_id = domain_id;
       info.pasid = pasid;
@@ -2932,6 +2935,36 @@ static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
       g_hash_table_foreach_remove(s->iotlb,
                                   vtd_hash_remove_by_page_piotlb, &info);
       vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+            IOMMUTLBEvent event;
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            /*
+             * Page-Selective-within-PASID PASID-based-IOTLB Invalidation
+             * does not flush stage-2 entries. See spec section 6.5.2.4
+             */
+            if (!s->scalable_modern) {
+                continue;
+            }
+
+            event.type = IOMMU_NOTIFIER_UNMAP;
+            event.entry.target_as = &address_space_memory;
+            event.entry.iova = addr;
+            event.entry.perm = IOMMU_NONE;
+            event.entry.addr_mask = size - 1;
+            event.entry.translated_addr = 0;
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+        }
+    }
   }
 
   static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
--

2.34.1

#39 lore raw

RE: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Quoted 9 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Quoted 5 lines
Quoted 3 lines · CLEMENT MATHIEU--DRIF

Maybe I'm missing something but why do we invalidate device IOTLB upon piotlb receipt of a regular IOTLB inv desc?
I don't get why we don't wait for a device IOTLB inv desc?

I thought you were planning to remove that after the last rfc version

Look at vtd_iotlb_page_invalidate(), it has same operation.
Reason is even if we don't enable device IOTLB, devices such as vhost may still caches IOTLB entries. So we need to flush those stale IOTLB entries in this case.

Thanks
Zhenzhong

Quoted 83 lines
Quoted 3 lines · Zhenzhong Duan
Quoted 1 line

Caution: External email. Do not open attachments or click links, unless

this email comes from a known sender and you know the content is safe.

Quoted 10 lines
Quoted 10 lines

This is used by some emulated devices which caches address translation result. When piotlb invalidation issued in guest, those caches should be refreshed.

---
   hw/i386/intel_iommu.c | 35

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

Quoted 7 lines

hw/i386/intel_iommu.c static void

Quoted 7 lines
   1 file changed, 34 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +0 -0
index 8b66d6cfa5..c0116497b1 100644
@@ -2910,7 +2910,7 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

Quoted 9 lines

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
                   continue;
               }
 
-            if (!s->scalable_modern) {
+            if (!s->scalable_modern || !vtd_as_has_map_notifier(vtd_as)) {
                   vtd_address_space_sync(vtd_as);
               }
           }
@@ -2922,6 +2922,9 @@ static void

vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines

hw/i386/intel_iommu.c static void

Quoted 10 lines
hw/i386/intel_iommu.c +3 -0
                                          bool ih)
   {
       VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
       info.domain_id = domain_id;
       info.pasid = pasid;
@@ -2932,6 +2935,36 @@ static void

vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

Quoted 39 lines
Quoted 39 lines
hw/i386/intel_iommu.c +30 -1
       g_hash_table_foreach_remove(s->iotlb,
                                   vtd_hash_remove_by_page_piotlb, &info);
       vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+            IOMMUTLBEvent event;
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            /*
+             * Page-Selective-within-PASID PASID-based-IOTLB Invalidation
+             * does not flush stage-2 entries. See spec section 6.5.2.4
+             */
+            if (!s->scalable_modern) {
+                continue;
+            }
+
+            event.type = IOMMU_NOTIFIER_UNMAP;
+            event.entry.target_as = &address_space_memory;
+            event.entry.iova = addr;
+            event.entry.perm = IOMMU_NONE;
+            event.entry.addr_mask = size - 1;
+            event.entry.translated_addr = 0;
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+        }
+    }
   }
 
   static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
--

2.34.1

#40 lore raw

Re: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Quoted 14 lines · Zhenzhong

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

Quoted 11 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 14/17] intel_iommu: piotlb invalidation should notify unmap

Quoted 3 lines · CLEMENT MATHIEU--DRIF

Maybe I'm missing something but why do we invalidate device IOTLB upon piotlb receipt of a regular IOTLB inv desc?
I don't get why we don't wait for a device IOTLB inv desc?

Quoted 6 lines
Quoted 1 line

I thought you were planning to remove that after the last rfc version

Look at vtd_iotlb_page_invalidate(), it has same operation.
Reason is even if we don't enable device IOTLB, devices such as vhost may still caches IOTLB entries. So we need to flush those stale IOTLB entries in this case.

Thanks
Zhenzhong

Ok fine,

Quoted 82 lines
Quoted 82 lines
Quoted 3 lines · Zhenzhong Duan
Quoted 1 line

Caution: External email. Do not open attachments or click links, unless

this email comes from a known sender and you know the content is safe.

Quoted 9 lines
Quoted 9 lines

This is used by some emulated devices which caches address translation result. When piotlb invalidation issued in guest, those caches should be refreshed.

---
    hw/i386/intel_iommu.c | 35

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

Quoted 7 lines

hw/i386/intel_iommu.c static void

Quoted 7 lines
    1 file changed, 34 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +0 -0
index 8b66d6cfa5..c0116497b1 100644
@@ -2910,7 +2910,7 @@ static void

vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,

Quoted 9 lines

hw/i386/intel_iommu.c static void

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
                    continue;
                }
 
-            if (!s->scalable_modern) {
+            if (!s->scalable_modern || !vtd_as_has_map_notifier(vtd_as)) {
                    vtd_address_space_sync(vtd_as);
                }
            }
@@ -2922,6 +2922,9 @@ static void

vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

Quoted 10 lines

hw/i386/intel_iommu.c static void

Quoted 10 lines
hw/i386/intel_iommu.c +3 -0
                                           bool ih)
    {
        VTDIOTLBPageInvInfo info;
+    VTDAddressSpace *vtd_as;
+    VTDContextEntry ce;
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
 
        info.domain_id = domain_id;
        info.pasid = pasid;
@@ -2932,6 +2935,36 @@ static void

vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,

Quoted 39 lines
Quoted 39 lines
hw/i386/intel_iommu.c +30 -1
        g_hash_table_foreach_remove(s->iotlb,
                                    vtd_hash_remove_by_page_piotlb, &info);
        vtd_iommu_unlock(s);
+
+    QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
+        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
+                                      vtd_as->devfn, &ce) &&
+            domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
+            uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
+            IOMMUTLBEvent event;
+
+            if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
+                vtd_as->pasid != pasid) {
+                continue;
+            }
+
+            /*
+             * Page-Selective-within-PASID PASID-based-IOTLB Invalidation
+             * does not flush stage-2 entries. See spec section 6.5.2.4
+             */
+            if (!s->scalable_modern) {
+                continue;
+            }
+
+            event.type = IOMMU_NOTIFIER_UNMAP;
+            event.entry.target_as = &address_space_memory;
+            event.entry.iova = addr;
+            event.entry.perm = IOMMU_NONE;
+            event.entry.addr_mask = size - 1;
+            event.entry.translated_addr = 0;
+            memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+        }
+    }
    }
 
    static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
--

2.34.1

#41 lore raw

PATCH 15/17 intel_iommu: Set default aw_bits to 48 in scalable modren mode

In reply to Zhenzhong Duan

According to VTD spec, stage-1 page table could support 4-level and 5-level paging.

However, 5-level paging translation emulation is unsupported yet. That means the only supported value for aw_bits is 48.

So default aw_bits to 48 in scalable modern mode. In other cases, it is still default to 39 for compatibility.

Add a check to ensure user specified value is 48 in modern mode for now.

---
 include/hw/i386/intel_iommu.h |  2 +-
 hw/i386/intel_iommu.c         | 16 +++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
include/hw/i386/intel_iommu.h +1 -1
index b843d069cc..48134bda11 100644
@@ -45,7 +45,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState, INTEL_IOMMU_DEVICE)
 #define DMAR_REG_SIZE               0x230
 #define VTD_HOST_AW_39BIT           39
 #define VTD_HOST_AW_48BIT           48
-#define VTD_HOST_ADDRESS_WIDTH      VTD_HOST_AW_39BIT
+#define VTD_HOST_AW_AUTO            0xff
 #define VTD_HAW_MASK(aw)            ((1ULL << (aw)) - 1)
 
 #define DMAR_REPORT_F_INTR          (1)
hw/i386/intel_iommu.c +15 -1
index c0116497b1..2804c3628a 100644
@@ -3768,7 +3768,7 @@ static Property vtd_properties[] = {
                             ON_OFF_AUTO_AUTO),
     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
     DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
-                      VTD_HOST_ADDRESS_WIDTH),
+                      VTD_HOST_AW_AUTO),
     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
     DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
     DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
@@ -4686,6 +4686,14 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
         }
     }
 
+    if (s->aw_bits == VTD_HOST_AW_AUTO) {
+        if (s->scalable_modern) {
+            s->aw_bits = VTD_HOST_AW_48BIT;
+        } else {
+            s->aw_bits = VTD_HOST_AW_39BIT;
+        }
+    }
+
     if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
         (s->aw_bits != VTD_HOST_AW_48BIT) &&
         !s->scalable_modern) {
@@ -4694,6 +4702,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
         return false;
     }
 
+    if ((s->aw_bits != VTD_HOST_AW_48BIT) && s->scalable_modern) {
+        error_setg(errp, "Supported values for aw-bits are: %d",
+                   VTD_HOST_AW_48BIT);
+        return false;
+    }
+
     if (s->scalable_mode && !s->dma_drain) {
         error_setg(errp, "Need to set dma_drain for scalable mode");
         return false;
Signature · 1 line
2.34.1
Reviewed-by on v1 15/17
#42 lore raw

Re: [PATCH v1 15/17] intel_iommu: Set default aw_bits to 48 in scalable modren mode

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

According to VTD spec, stage-1 page table could support 4-level and 5-level paging.

However, 5-level paging translation emulation is unsupported yet. That means the only supported value for aw_bits is 48.

So default aw_bits to 48 in scalable modern mode. In other cases, it is still default to 39 for compatibility.

Add a check to ensure user specified value is 48 in modern mode for now.

---
  include/hw/i386/intel_iommu.h |  2 +-
  hw/i386/intel_iommu.c         | 16 +++++++++++++++-
  2 files changed, 16 insertions(+), 2 deletions(-)
include/hw/i386/intel_iommu.h +1 -1
index b843d069cc..48134bda11 100644
@@ -45,7 +45,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState, INTEL_IOMMU_DEVICE)
  #define DMAR_REG_SIZE               0x230
  #define VTD_HOST_AW_39BIT           39
  #define VTD_HOST_AW_48BIT           48
-#define VTD_HOST_ADDRESS_WIDTH      VTD_HOST_AW_39BIT
+#define VTD_HOST_AW_AUTO            0xff
  #define VTD_HAW_MASK(aw)            ((1ULL << (aw)) - 1)
 
  #define DMAR_REPORT_F_INTR          (1)
hw/i386/intel_iommu.c +15 -2
index c0116497b1..2804c3628a 100644
@@ -3768,7 +3768,7 @@ static Property vtd_properties[] = {
                              ON_OFF_AUTO_AUTO),
      DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
      DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
-                      VTD_HOST_ADDRESS_WIDTH),
+                      VTD_HOST_AW_AUTO),
      DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
      DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
      DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
@@ -4686,6 +4686,14 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
          }
      }
 
+    if (s->aw_bits == VTD_HOST_AW_AUTO) {
+        if (s->scalable_modern) {
+            s->aw_bits = VTD_HOST_AW_48BIT;
+        } else {
+            s->aw_bits = VTD_HOST_AW_39BIT;
+        }
+    }
+
      if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
          (s->aw_bits != VTD_HOST_AW_48BIT) &&
          !s->scalable_modern) {
@@ -4694,6 +4702,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
          return false;
      }
 
+    if ((s->aw_bits != VTD_HOST_AW_48BIT) && s->scalable_modern) {
+        error_setg(errp, "Supported values for aw-bits are: %d",
+                   VTD_HOST_AW_48BIT);
+        return false;
+    }
+
      if (s->scalable_mode && !s->dma_drain) {
          error_setg(errp, "Need to set dma_drain for scalable mode");
          return false;
--

2.34.1

#43 lore raw

PATCH 16/17 intel_iommu: Modify x-scalable-mode to be string option

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

Intel VT-d 3.0 introduces scalable mode, and it has a bunch of capabilities related to scalable mode translation, thus there are multiple combinations. While this vIOMMU implementation wants to simplify it for user by providing typical combinations. User could config it by "x-scalable-mode" option. The usage is as below:

"-device intel-iommu,x-scalable-mode=["legacy"|"modern"|"off"]"

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- if not configured, means no scalable mode support, if not proper configured, will throw error

---
 include/hw/i386/intel_iommu.h |  1 +
 hw/i386/intel_iommu.c         | 24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
include/hw/i386/intel_iommu.h +1 -0
index 48134bda11..650641544c 100644
@@ -263,6 +263,7 @@ struct IntelIOMMUState {
 
     bool caching_mode;              /* RO - is cap CM enabled? */
     bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    char *scalable_mode_str;        /* RO - admin's Scalable Mode config */
     bool scalable_modern;           /* RO - is modern SM supported? */
     bool snoop_control;             /* RO - is SNP filed supported? */
 
hw/i386/intel_iommu.c +23 -1
index 2804c3628a..14d05fce1d 100644
@@ -3770,7 +3770,7 @@ static Property vtd_properties[] = {
     DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
                       VTD_HOST_AW_AUTO),
     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
-    DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
+    DEFINE_PROP_STRING("x-scalable-mode", IntelIOMMUState, scalable_mode_str),
     DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
     DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
     DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
@@ -4686,6 +4686,28 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
         }
     }
 
+    if (s->scalable_mode_str &&
+        (strcmp(s->scalable_mode_str, "off") &&
+         strcmp(s->scalable_mode_str, "modern") &&
+         strcmp(s->scalable_mode_str, "legacy"))) {
+        error_setg(errp, "Invalid x-scalable-mode config,"
+                         "Please use \"modern\", \"legacy\" or \"off\"");
+        return false;
+    }
+
+    if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "legacy")) {
+        s->scalable_mode = true;
+        s->scalable_modern = false;
+    } else if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "modern")) {
+        s->scalable_mode = true;
+        s->scalable_modern = true;
+    } else {
+        s->scalable_mode = false;
+        s->scalable_modern = false;
+    }
+
     if (s->aw_bits == VTD_HOST_AW_AUTO) {
         if (s->scalable_modern) {
             s->aw_bits = VTD_HOST_AW_48BIT;
Signature · 1 line
2.34.1
#44 lore raw

Re: [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option

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

From: Yi Liu <[email protected]>

Intel VT-d 3.0 introduces scalable mode, and it has a bunch of capabilities related to scalable mode translation, thus there are multiple combinations. While this vIOMMU implementation wants to simplify it for user by providing typical combinations. User could config it by "x-scalable-mode" option. The usage is as below:

Quoted 7 lines

"-device intel-iommu,x-scalable-mode=["legacy"|"modern"|"off"]"

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- if not configured, means no scalable mode support, if not proper configured, will throw error

s/proper/properly
Maybe we could split and rephrase the last bullet point to make it clear that "off" is equivalent to not using the option at all

Quoted 64 lines
---
  include/hw/i386/intel_iommu.h |  1 +
  hw/i386/intel_iommu.c         | 24 +++++++++++++++++++++++-
  2 files changed, 24 insertions(+), 1 deletion(-)
include/hw/i386/intel_iommu.h +1 -0
index 48134bda11..650641544c 100644
@@ -263,6 +263,7 @@ struct IntelIOMMUState {
 
      bool caching_mode;              /* RO - is cap CM enabled? */
      bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    char *scalable_mode_str;        /* RO - admin's Scalable Mode config */
      bool scalable_modern;           /* RO - is modern SM supported? */
      bool snoop_control;             /* RO - is SNP filed supported? */
 
hw/i386/intel_iommu.c +23 -2
index 2804c3628a..14d05fce1d 100644
@@ -3770,7 +3770,7 @@ static Property vtd_properties[] = {
      DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
                        VTD_HOST_AW_AUTO),
      DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
-    DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
+    DEFINE_PROP_STRING("x-scalable-mode", IntelIOMMUState, scalable_mode_str),
      DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
      DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
@@ -4686,6 +4686,28 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
          }
      }
 
+    if (s->scalable_mode_str &&
+        (strcmp(s->scalable_mode_str, "off") &&
+         strcmp(s->scalable_mode_str, "modern") &&
+         strcmp(s->scalable_mode_str, "legacy"))) {
+        error_setg(errp, "Invalid x-scalable-mode config,"
+                         "Please use \"modern\", \"legacy\" or \"off\"");
+        return false;
+    }
+
+    if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "legacy")) {
+        s->scalable_mode = true;
+        s->scalable_modern = false;
+    } else if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "modern")) {
+        s->scalable_mode = true;
+        s->scalable_modern = true;
+    } else {
+        s->scalable_mode = false;
+        s->scalable_modern = false;
+    }
+
      if (s->aw_bits == VTD_HOST_AW_AUTO) {
          if (s->scalable_modern) {
              s->aw_bits = VTD_HOST_AW_48BIT;
--
Quoted 2 lines

2.34.1

LGTM

#45 lore raw

RE: [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option

Quoted 25 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option

Quoted 1 line · 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.

Quoted 7 lines

From: Yi Liu <[email protected]>

Intel VT-d 3.0 introduces scalable mode, and it has a bunch of capabilities related to scalable mode translation, thus there are multiple combinations. While this vIOMMU implementation wants to simplify it for user by

providing

Quoted 5 lines

typical combinations. User could config it by "x-scalable-mode" option. The usage is as below:

"-device intel-iommu,x-scalable-mode=["legacy"|"modern"|"off"]"

Quoted 8 lines
Quoted 5 lines

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- if not configured, means no scalable mode support, if not proper configured, will throw error

s/proper/properly
Maybe we could split and rephrase the last bullet point to make it clear that "off" is equivalent to not using the option at all

You mean split last bullet as a separate paragraph? Then what about below:

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- any other string, will throw error

If x-scalable-mode is not configured, it is equivalent to x-scalable-mode=off.

Thanks
Zhenzhong

Quoted 73 lines

Review

Quoted 10 lines
---
  include/hw/i386/intel_iommu.h |  1 +
  hw/i386/intel_iommu.c         | 24 +++++++++++++++++++++++-
  2 files changed, 24 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 16 lines

index 48134bda11..650641544c 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -263,6 +263,7 @@ struct IntelIOMMUState {
 
      bool caching_mode;              /* RO - is cap CM enabled? */
      bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    char *scalable_mode_str;        /* RO - admin's Scalable Mode config */
      bool scalable_modern;           /* RO - is modern SM supported? */
      bool snoop_control;             /* RO - is SNP filed supported? */
 
hw/i386/intel_iommu.c +0 -0
index 2804c3628a..14d05fce1d 100644

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

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
@@ -3770,7 +3770,7 @@ static Property vtd_properties[] = {
      DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
                        VTD_HOST_AW_AUTO),
      DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode,

FALSE),

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

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

scalable_mode, FALSE),

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

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

scalable_mode_str),

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

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState,

snoop_control, false),

hw/i386/intel_iommu.c static bool

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
      DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
      DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
@@ -4686,6 +4686,28 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 29 lines
hw/i386/intel_iommu.c +22 -1
          }
      }
 
+    if (s->scalable_mode_str &&
+        (strcmp(s->scalable_mode_str, "off") &&
+         strcmp(s->scalable_mode_str, "modern") &&
+         strcmp(s->scalable_mode_str, "legacy"))) {
+        error_setg(errp, "Invalid x-scalable-mode config,"
+                         "Please use \"modern\", \"legacy\" or \"off\"");
+        return false;
+    }
+
+    if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "legacy")) {
+        s->scalable_mode = true;
+        s->scalable_modern = false;
+    } else if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "modern")) {
+        s->scalable_mode = true;
+        s->scalable_modern = true;
+    } else {
+        s->scalable_mode = false;
+        s->scalable_modern = false;
+    }
+
      if (s->aw_bits == VTD_HOST_AW_AUTO) {
          if (s->scalable_modern) {
              s->aw_bits = VTD_HOST_AW_48BIT;
--
Quoted 2 lines

2.34.1

LGTM

#46 lore raw

Re: [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option

Quoted 36 lines · Zhenzhong

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

Quoted 22 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 16/17] intel_iommu: Modify x-scalable-mode to be string option

Quoted 1 line · 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.

Quoted 6 lines

From: Yi Liu <[email protected]>

Intel VT-d 3.0 introduces scalable mode, and it has a bunch of capabilities related to scalable mode translation, thus there are multiple combinations. While this vIOMMU implementation wants to simplify it for user by

providing

Quoted 5 lines

typical combinations. User could config it by "x-scalable-mode" option. The usage is as below:

"-device intel-iommu,x-scalable-mode=["legacy"|"modern"|"off"]"

Quoted 8 lines
Quoted 5 lines

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- if not configured, means no scalable mode support, if not proper configured, will throw error

s/proper/properly
Maybe we could split and rephrase the last bullet point to make it clear that "off" is equivalent to not using the option at all

You mean split last bullet as a separate paragraph? Then what about below:

Quoted 6 lines

- "legacy": gives support for stage-2 page table
- "modern": gives support for stage-1 page table
- "off": no scalable mode support
- any other string, will throw error

If x-scalable-mode is not configured, it is equivalent to x-scalable-mode=off.

Yes, lgtm

Quoted 76 lines

Thanks
Zhenzhong

Quoted 72 lines

Review

Quoted 9 lines
---
   include/hw/i386/intel_iommu.h |  1 +
   hw/i386/intel_iommu.c         | 24 +++++++++++++++++++++++-
   2 files changed, 24 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 16 lines

index 48134bda11..650641544c 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -263,6 +263,7 @@ struct IntelIOMMUState {
 
       bool caching_mode;              /* RO - is cap CM enabled? */
       bool scalable_mode;             /* RO - is Scalable Mode supported? */
+    char *scalable_mode_str;        /* RO - admin's Scalable Mode config */
       bool scalable_modern;           /* RO - is modern SM supported? */
       bool snoop_control;             /* RO - is SNP filed supported? */
 
hw/i386/intel_iommu.c +0 -0
index 2804c3628a..14d05fce1d 100644

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

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
@@ -3770,7 +3770,7 @@ static Property vtd_properties[] = {
       DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
                         VTD_HOST_AW_AUTO),
       DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode,

FALSE),

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

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

scalable_mode, FALSE),

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

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

scalable_mode_str),

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

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
       DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState,

snoop_control, false),

hw/i386/intel_iommu.c static bool

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
       DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
       DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
@@ -4686,6 +4686,28 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 29 lines
hw/i386/intel_iommu.c +22 -1
           }
       }
 
+    if (s->scalable_mode_str &&
+        (strcmp(s->scalable_mode_str, "off") &&
+         strcmp(s->scalable_mode_str, "modern") &&
+         strcmp(s->scalable_mode_str, "legacy"))) {
+        error_setg(errp, "Invalid x-scalable-mode config,"
+                         "Please use \"modern\", \"legacy\" or \"off\"");
+        return false;
+    }
+
+    if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "legacy")) {
+        s->scalable_mode = true;
+        s->scalable_modern = false;
+    } else if (s->scalable_mode_str &&
+        !strcmp(s->scalable_mode_str, "modern")) {
+        s->scalable_mode = true;
+        s->scalable_modern = true;
+    } else {
+        s->scalable_mode = false;
+        s->scalable_modern = false;
+    }
+
       if (s->aw_bits == VTD_HOST_AW_AUTO) {
           if (s->scalable_modern) {
               s->aw_bits = VTD_HOST_AW_48BIT;
--
Quoted 2 lines

2.34.1

LGTM

#47 lore raw

PATCH 17/17 tests/qtest: Add intel-iommu test

In reply to Zhenzhong Duan

Add the framework to test the intel-iommu device.

Currently only tested cap/ecap bits correctness in scalable modern mode. Also tested cap/ecap bits consistency before and after system reset.

---
 MAINTAINERS                    |  1 +
 include/hw/i386/intel_iommu.h  |  1 +
 tests/qtest/intel-iommu-test.c | 71 ++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build        |  1 +
 4 files changed, 74 insertions(+)
 create mode 100644 tests/qtest/intel-iommu-test.c
MAINTAINERS +1 -0
index 7d9811458c..ec765bf3d3 100644
@@ -3662,6 +3662,7 @@ S: Supported
 F: hw/i386/intel_iommu.c
 F: hw/i386/intel_iommu_internal.h
 F: include/hw/i386/intel_iommu.h
+F: tests/qtest/intel-iommu-test.c
 
 AMD-Vi Emulation
 S: Orphan
include/hw/i386/intel_iommu.h +1 -0
index 650641544c..b1848dbec6 100644
@@ -47,6 +47,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState, INTEL_IOMMU_DEVICE)
 #define VTD_HOST_AW_48BIT           48
 #define VTD_HOST_AW_AUTO            0xff
 #define VTD_HAW_MASK(aw)            ((1ULL << (aw)) - 1)
+#define VTD_MGAW_FROM_CAP(cap)      ((cap >> 16) & 0x3fULL)
 
 #define DMAR_REPORT_F_INTR          (1)
 
tests/qtest/intel-iommu-test.c new file +71 -0

71 lines not on this page · raw message

tests/qtest/meson.build +1 -0
index 6508bfb1a2..20d05d471b 100644
@@ -79,6 +79,7 @@ qtests_i386 = \
   (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) +                   \
   (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) +            \
   (config_all_devices.has_key('CONFIG_ESP_PCI') ? ['am53c974-test'] : []) +                 \
+  (config_all_devices.has_key('CONFIG_VTD') ? ['intel-iommu-test'] : []) +                 \
   (host_os != 'windows' and                                                                \
    config_all_devices.has_key('CONFIG_ACPI_ERST') ? ['erst-test'] : []) +                   \
   (config_all_devices.has_key('CONFIG_PCIE_PORT') and                                       \
Signature · 1 line
2.34.1
Reviewed-by on v1 17/17
#48 lore raw

Re: [PATCH v1 17/17] tests/qtest: Add intel-iommu test

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

Add the framework to test the intel-iommu device.

Currently only tested cap/ecap bits correctness in scalable modern mode. Also tested cap/ecap bits consistency before and after system reset.

---
  MAINTAINERS                    |  1 +
  include/hw/i386/intel_iommu.h  |  1 +
  tests/qtest/intel-iommu-test.c | 71 ++++++++++++++++++++++++++++++++++
  tests/qtest/meson.build        |  1 +
  4 files changed, 74 insertions(+)

create mode 100644 tests/qtest/intel-iommu-test.c

MAINTAINERS +1 -0
index 7d9811458c..ec765bf3d3 100644
@@ -3662,6 +3662,7 @@ S: Supported
  F: hw/i386/intel_iommu.c
  F: hw/i386/intel_iommu_internal.h
  F: include/hw/i386/intel_iommu.h
+F: tests/qtest/intel-iommu-test.c
 
  AMD-Vi Emulation
  S: Orphan
include/hw/i386/intel_iommu.h +1 -0
index 650641544c..b1848dbec6 100644
@@ -47,6 +47,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState, INTEL_IOMMU_DEVICE)
  #define VTD_HOST_AW_48BIT           48
  #define VTD_HOST_AW_AUTO            0xff
  #define VTD_HAW_MASK(aw)            ((1ULL << (aw)) - 1)
+#define VTD_MGAW_FROM_CAP(cap)      ((cap >> 16) & 0x3fULL)
 
  #define DMAR_REPORT_F_INTR          (1)
 
tests/qtest/intel-iommu-test.c new file +41 -0
new file mode 100644
index 0000000000..8e07034f6f
@@ -0,0 +1,71 @@
+/*
+ * QTest testcase for intel-iommu
+ *
+ * Copyright (c) 2024 Intel, Inc.
+ *
+ * Author: Zhenzhong Duan <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "hw/i386/intel_iommu_internal.h"
+
+#define CAP_MODERN_FIXED1    (VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND | \
+                              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS)
+#define ECAP_MODERN_FIXED1   (VTD_ECAP_QI |  VTD_ECAP_IRO | VTD_ECAP_MHMV | \
+                              VTD_ECAP_SMTS | VTD_ECAP_FLTS)
+
+static inline uint32_t vtd_reg_readl(QTestState *s, uint64_t offset)
+{
+    return qtest_readl(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
+}
+
+static inline uint64_t vtd_reg_readq(QTestState *s, uint64_t offset)
+{
+    return qtest_readq(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
+}
+
+static void test_intel_iommu_modern(void)
+{
+    uint8_t init_csr[DMAR_REG_SIZE];     /* register values */
+    uint8_t post_reset_csr[DMAR_REG_SIZE];     /* register values */
+    uint64_t cap, ecap, tmp;
+    QTestState *s;
+
+    s = qtest_init("-M q35 -device intel-iommu,x-scalable-mode=modern");
+
+    cap = vtd_reg_readq(s, DMAR_CAP_REG);
+    g_assert((cap & CAP_MODERN_FIXED1) == CAP_MODERN_FIXED1);

tests/qtest/intel-iommu-test.c line 51

Quoted 10 lines
tests/qtest/intel-iommu-test.c new file +10 -0
+
+    tmp = cap & VTD_CAP_SAGAW_MASK;
+    g_assert(tmp == (VTD_CAP_SAGAW_39bit | VTD_CAP_SAGAW_48bit));
+
+    tmp = VTD_MGAW_FROM_CAP(cap);
+    g_assert(tmp == VTD_HOST_AW_48BIT - 1);
+
+    ecap = vtd_reg_readq(s, DMAR_ECAP_REG);
+    g_assert((ecap & ECAP_MODERN_FIXED1) == ECAP_MODERN_FIXED1);
+    g_assert(ecap & VTD_ECAP_IR);

Can we add VTD_ECAP_IR to ECAP_MODERN_FIXED1?

Quoted 33 lines
tests/qtest/intel-iommu-test.c new file +20 -0
+
+    qtest_memread(s, Q35_HOST_BRIDGE_IOMMU_ADDR, init_csr, DMAR_REG_SIZE);
+
+    qobject_unref(qtest_qmp(s, "{ 'execute': 'system_reset' }"));
+    qtest_qmp_eventwait(s, "RESET");
+
+    qtest_memread(s, Q35_HOST_BRIDGE_IOMMU_ADDR, post_reset_csr, DMAR_REG_SIZE);
+    /* Ensure registers are consistent after hard reset */
+    g_assert(!memcmp(init_csr, post_reset_csr, DMAR_REG_SIZE));
+
+    qtest_quit(s);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/q35/intel-iommu/modern", test_intel_iommu_modern);
+
+    return g_test_run();
+}
tests/qtest/meson.build +1 -1
index 6508bfb1a2..20d05d471b 100644
@@ -79,6 +79,7 @@ qtests_i386 = \
    (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) +                   \
    (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) +            \
    (config_all_devices.has_key('CONFIG_ESP_PCI') ? ['am53c974-test'] : []) +                 \
+  (config_all_devices.has_key('CONFIG_VTD') ? ['intel-iommu-test'] : []) +                 \
    (host_os != 'windows' and                                                                \
     config_all_devices.has_key('CONFIG_ACPI_ERST') ? ['erst-test'] : []) +                   \
    (config_all_devices.has_key('CONFIG_PCIE_PORT') and                                       \
--
Quoted 2 lines

2.34.1

#49 lore raw

RE: [PATCH v1 17/17] tests/qtest: Add intel-iommu test

Quoted 108 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v1 17/17] tests/qtest: Add intel-iommu test

Quoted 1 line · 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.

Quoted 8 lines

Add the framework to test the intel-iommu device.

Currently only tested cap/ecap bits correctness in scalable modern mode. Also tested cap/ecap bits consistency before and after system reset.

Quoted 5 lines
---
  MAINTAINERS                    |  1 +
  include/hw/i386/intel_iommu.h  |  1 +
  tests/qtest/intel-iommu-test.c | 71

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

Review

Quoted 17 lines
  tests/qtest/meson.build        |  1 +
  4 files changed, 74 insertions(+)

create mode 100644 tests/qtest/intel-iommu-test.c

MAINTAINERS +1 -0
index 7d9811458c..ec765bf3d3 100644
@@ -3662,6 +3662,7 @@ S: Supported
  F: hw/i386/intel_iommu.c
  F: hw/i386/intel_iommu_internal.h
  F: include/hw/i386/intel_iommu.h
+F: tests/qtest/intel-iommu-test.c
 
  AMD-Vi Emulation
  S: Orphan
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

include/hw/i386/intel_iommu.h OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState,

Quoted 4 lines

index 650641544c..b1848dbec6 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -47,6 +47,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState,

INTEL_IOMMU_DEVICE)

Quoted 13 lines
include/hw/i386/intel_iommu.h +1 -0
  #define VTD_HOST_AW_48BIT           48
  #define VTD_HOST_AW_AUTO            0xff
  #define VTD_HAW_MASK(aw)            ((1ULL << (aw)) - 1)
+#define VTD_MGAW_FROM_CAP(cap)      ((cap >> 16) & 0x3fULL)
 
  #define DMAR_REPORT_F_INTR          (1)
 
tests/qtest/intel-iommu-test.c new file +0 -0
new file mode 100644
index 0000000000..8e07034f6f

tests/qtest/intel-iommu-test.c line 8

Quoted 8 lines
tests/qtest/intel-iommu-test.c new file +8 -0
@@ -0,0 +1,71 @@
+/*
+ * QTest testcase for intel-iommu
+ *
+ * Copyright (c) 2024 Intel, Inc.
+ *
+ * Author: Zhenzhong Duan <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or

later.

tests/qtest/intel-iommu-test.c

Quoted 8 lines
tests/qtest/intel-iommu-test.c new file +8 -0
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "hw/i386/intel_iommu_internal.h"
+
+#define CAP_MODERN_FIXED1    (VTD_CAP_FRO | VTD_CAP_NFR |

VTD_CAP_ND | \

tests/qtest/intel-iommu-test.c line 18

Quoted 2 lines
tests/qtest/intel-iommu-test.c new file +2 -0
+                              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS)
+#define ECAP_MODERN_FIXED1   (VTD_ECAP_QI |  VTD_ECAP_IRO |

VTD_ECAP_MHMV | \

Quoted 26 lines
tests/qtest/intel-iommu-test.c new file +26 -0
+                              VTD_ECAP_SMTS | VTD_ECAP_FLTS)
+
+static inline uint32_t vtd_reg_readl(QTestState *s, uint64_t offset)
+{
+    return qtest_readl(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
+}
+
+static inline uint64_t vtd_reg_readq(QTestState *s, uint64_t offset)
+{
+    return qtest_readq(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
+}
+
+static void test_intel_iommu_modern(void)
+{
+    uint8_t init_csr[DMAR_REG_SIZE];     /* register values */
+    uint8_t post_reset_csr[DMAR_REG_SIZE];     /* register values */
+    uint64_t cap, ecap, tmp;
+    QTestState *s;
+
+    s = qtest_init("-M q35 -device intel-iommu,x-scalable-mode=modern");
+
+    cap = vtd_reg_readq(s, DMAR_CAP_REG);
+    g_assert((cap & CAP_MODERN_FIXED1) == CAP_MODERN_FIXED1);
+
+    tmp = cap & VTD_CAP_SAGAW_MASK;
+    g_assert(tmp == (VTD_CAP_SAGAW_39bit | VTD_CAP_SAGAW_48bit));
Quoted 8 lines

tests/qtest/intel-iommu-test.c line 51

Quoted 7 lines
tests/qtest/intel-iommu-test.c new file +7 -0
+
+    tmp = VTD_MGAW_FROM_CAP(cap);
+    g_assert(tmp == VTD_HOST_AW_48BIT - 1);
+
+    ecap = vtd_reg_readq(s, DMAR_ECAP_REG);
+    g_assert((ecap & ECAP_MODERN_FIXED1) == ECAP_MODERN_FIXED1);
+    g_assert(ecap & VTD_ECAP_IR);

Can we add VTD_ECAP_IR to ECAP_MODERN_FIXED1?

Will do.

Thanks
Zhenzhong

Quoted 46 lines

tests/qtest/intel-iommu-test.c line 53

Quoted 2 lines
tests/qtest/intel-iommu-test.c new file +2 -0
+
+    qtest_memread(s, Q35_HOST_BRIDGE_IOMMU_ADDR, init_csr,

DMAR_REG_SIZE);

tests/qtest/intel-iommu-test.c

Quoted 5 lines
tests/qtest/intel-iommu-test.c new file +5 -0
+
+    qobject_unref(qtest_qmp(s, "{ 'execute': 'system_reset' }"));
+    qtest_qmp_eventwait(s, "RESET");
+
+    qtest_memread(s, Q35_HOST_BRIDGE_IOMMU_ADDR, post_reset_csr,

DMAR_REG_SIZE);

tests/qtest/intel-iommu-test.c

Quoted 10 lines
tests/qtest/intel-iommu-test.c new file +10 -0
+    /* Ensure registers are consistent after hard reset */
+    g_assert(!memcmp(init_csr, post_reset_csr, DMAR_REG_SIZE));
+
+    qtest_quit(s);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/q35/intel-iommu/modern",

test_intel_iommu_modern);

tests/qtest/meson.build qtests_i386 = \ · line 79

Quoted 9 lines
tests/qtest/intel-iommu-test.c new file +3 -0
+
+    return g_test_run();
+}
tests/qtest/meson.build +0 -0
index 6508bfb1a2..20d05d471b 100644
@@ -79,6 +79,7 @@ qtests_i386 = \
    (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) +

\

tests/qtest/meson.build qtests_i386 = \ · line 80

Quoted 1 line
tests/qtest/meson.build +0 -0
    (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] :

[]) + \

tests/qtest/meson.build qtests_i386 = \ · line 81

Quoted 1 line
tests/qtest/meson.build +0 -0
    (config_all_devices.has_key('CONFIG_ESP_PCI') ? ['am53c974-test'] : [])

+ \

tests/qtest/meson.build qtests_i386 = \ · line 82

Quoted 1 line
tests/qtest/meson.build +1 -0
+  (config_all_devices.has_key('CONFIG_VTD') ? ['intel-iommu-test'] : []) +

\

tests/qtest/meson.build qtests_i386 = \ · line 84

Quoted 2 lines
tests/qtest/meson.build +0 -0
    (host_os != 'windows' and                                                                \
     config_all_devices.has_key('CONFIG_ACPI_ERST') ? ['erst-test'] : []) +

\

tests/qtest/meson.build qtests_i386 = \ · line 85

Quoted 1 line
tests/qtest/meson.build +0 -0
    (config_all_devices.has_key('CONFIG_PCIE_PORT') and

\

Quoted 3 lines

--
2.34.1