Skip to contentSkip to conversation

ATS support for VT-d

PATCH Superseded CLEMENT MATHIEU--DRIF 2 participants 35 messages May 2, 2024 – May 16, 2024 Arm/Amlogic Meson SoC support

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover ATS support for VT-d

This series belongs to a list of series that add SVM support for VT-d.

As a starting point, we use the series called 'intel_iommu: Enable stage-1 translation' (rfc2) by Zhenzhong Duan and Yi Liu.

Here we focus on the implementation of ATS support in the IOMMU and on a PCI-level API for ATS to be used by virtual devices.

This work is based on the VT-d specification version 4.1 (March 2023). Here is a link to a GitHub repository where you can find the following elements :
- Qemu with all the patches for SVM
- ATS
- PRI
- Device IOTLB invalidations
- Requests with already translated addresses
- A demo device
- A simple driver for the demo device
- A userspace program (for testing and demonstration purposes)

https://github.com/BullSequana/Qemu-in-guest-SVM-demo

Clément Mathieu--Drif (24):
intel_iommu: fix FRCD construction macro.
intel_iommu: make types match
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: do not consider wait_desc as an invalid descriptor
memory: add permissions in IOMMUAccessFlags
pcie: add helper to declare PASID capability for a pcie device
pcie: helper functions to check if PASID and ATS are enabled
intel_iommu: declare supported PASID size
intel_iommu: add an internal API to find an address space with PASID
intel_iommu: add support for PASID-based device IOTLB invalidation
pci: cache the bus mastering status in the device
pci: add IOMMU operations to get address spaces and memory regions
with PASID
pci: add a pci-level initialization function for iommu notifiers
intel_iommu: implement the get_address_space_pasid iommu operation
intel_iommu: implement the get_memory_region_pasid iommu operation
memory: Allow to store the PASID in IOMMUTLBEntry
intel_iommu: fill the PASID field when creating an instance of
IOMMUTLBEntry
atc: generic ATC that can be used by PCIe devices that support SVM
memory: add an API for ATS support
pci: add a pci-level API for ATS
intel_iommu: set the address mask even when a translation fails
intel_iommu: add support for ATS

 hw/i386/intel_iommu.c          | 313 ++++++++++++++++----
 hw/i386/intel_iommu_internal.h |  21 +-
 hw/pci/pci.c                   | 127 ++++++++-
 hw/pci/pcie.c                  |  42 +++
 include/exec/memory.h          |  60 +++-
 include/hw/i386/intel_iommu.h  |   2 +-
 include/hw/pci/pci.h           |  99 +++++++
 include/hw/pci/pci_device.h    |   1 +
 include/hw/pci/pcie.h          |   9 +-
 include/hw/pci/pcie_regs.h     |   3 +
 system/memory.c                |  20 ++
 tests/unit/meson.build         |   1 +
 tests/unit/test-atc.c          | 502 +++++++++++++++++++++++++++++++++
 util/atc.c                     | 211 ++++++++++++++
 util/atc.h                     | 117 ++++++++
 util/meson.build               |   1 +
 16 files changed, 1454 insertions(+), 75 deletions(-)
 create mode 100644 tests/unit/test-atc.c
 create mode 100644 util/atc.c
 create mode 100644 util/atc.h
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/24 intel_iommu: fix FRCD construction macro.

The constant must be unsigned, otherwise the two's complement overrides the other fields when a PASID is present

---
 hw/i386/intel_iommu_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/intel_iommu_internal.h +1 -1
index c5efcff9fd..4f6b0154b5 100644
@@ -271,7 +271,7 @@
 /* For the low 64-bit of 128-bit */
 #define VTD_FRCD_FI(val)        ((val) & ~0xfffULL)
 #define VTD_FRCD_PV(val)        (((val) & 0xffffULL) << 40)
-#define VTD_FRCD_PP(val)        (((val) & 0x1) << 31)
+#define VTD_FRCD_PP(val)        (((val) & 0x1ULL) << 31)
 #define VTD_FRCD_IR_IDX(val)    (((val) & 0xffffULL) << 48)
 
 /* DMA Remapping Fault Conditions */
Signature · 1 line
2.44.0
#2 lore raw

PATCH 2/24 intel_iommu: make types match

In reply to CLEMENT MATHIEU--DRIF

The 'level' field in vtd_iotlb_key is an uint8_t.
We don't need to store level as an int in vtd_lookup_iotlb (avoids a 'loosing precision' warning).

VTDIOTLBPageInvInfo.mask is used in binary operations with addresses.

---
 hw/i386/intel_iommu.c          | 2 +-
 hw/i386/intel_iommu_internal.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 70735e2379..80cdf37870 100644
@@ -386,7 +386,7 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
 {
     struct vtd_iotlb_key key;
     VTDIOTLBEntry *entry;
-    int level;
+    uint8_t level;
 
     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
         key.gfn = vtd_get_iotlb_gfn(addr, level);
hw/i386/intel_iommu_internal.h +1 -1
index 4f6b0154b5..901691afb9 100644
@@ -473,7 +473,7 @@ struct VTDIOTLBPageInvInfo {
     uint16_t domain_id;
     uint32_t pasid;
     uint64_t addr;
-    uint8_t mask;
+    uint64_t mask;
 };
 typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
 
Signature · 1 line
2.44.0
#3 lore raw

PATCH 3/24 intel_iommu: check if the input address is canonical

In reply to CLEMENT MATHIEU--DRIF

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

---
 hw/i386/intel_iommu.c          | 22 ++++++++++++++++++++++
 hw/i386/intel_iommu_internal.h |  2 ++
 2 files changed, 24 insertions(+)
hw/i386/intel_iommu.c +22 -0
index 80cdf37870..240ecb8f72 100644
@@ -1912,6 +1912,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,
 };
 
@@ -2023,6 +2024,21 @@ static inline uint64_t vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)
     return flpte & VTD_FL_PT_BASE_ADDR_MASK(aw);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
+                                        uint64_t iova, VTDContextEntry *ce,
+                                        uint8_t aw, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, aw, 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.
@@ -2038,6 +2054,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, aw_bits, 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_fl_level_offset(iova, level);
         flpte = vtd_get_flpte(addr, offset);
hw/i386/intel_iommu_internal.h +2 -0
index 901691afb9..e9448291a4 100644
@@ -324,6 +324,8 @@ typedef enum VTDFaultReason {
     VTD_FR_PASID_ENTRY_P = 0x59, /* The Present(P) field of pasidt-entry is 0 */
     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 */
Signature · 1 line
2.44.0
#4 lore raw

RE: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Hi Clement,

Quoted 27 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

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

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

2 files changed, 24 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 80cdf37870..240ecb8f72 100644
@@ -1912,6 +1912,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,
 };
 

hw/i386/intel_iommu.c static inline uint64_t · line 2033

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
@@ -2023,6 +2024,21 @@ static inline uint64_t
 vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)
    return flpte & VTD_FL_PT_BASE_ADDR_MASK(aw);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
+                                        uint64_t iova, VTDContextEntry *ce,
+                                        uint8_t aw, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, aw, pasid);

According to spec:

"Input-address in the request subjected to first-stage translation is not canonical (i.e., address bits 63:N are not same value as address bits [N- 1], where N is 48 bits with 4-level paging and 57 bits with 5-level paging)."

So it looks not correct to use aw filed in pasid entry to calculate iova_limit. Aw can be a value configured by guest and it's used for stage-2 table. See spec:

" This field is treated as Reserved(0) for implementations not supporting Second-stage Translation (SSTS=0 in the Extended Capability Register).
This field indicates the adjusted guest-address-width (AGAW) to be used by hardware for second-stage translation through paging structures referenced through the SSPTPTR field.
• The following encodings are defined for this field:
• 001b: 39-bit AGAW (3-level page table)
• 010b: 48-bit AGAW (4-level page table)
• 011b: 57-bit AGAW (5-level page table)
• 000b,100b-111b: Reserved
When not treated as Reserved(0), hardware ignores this field for first-stage-only (PGTT=001b) and pass-through (PGTT=100b) translations."

Thanks
Zhenzhong

Quoted 45 lines
hw/i386/intel_iommu.c +9 -0
+    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.

hw/i386/intel_iommu.c +2 -0
@@ -2038,6 +2054,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, aw_bits, pasid)) {
+        error_report_once("%s: detected non canonical IOVA (iova=0x%"

PRIx64 ","

hw/i386/intel_iommu.c +4 -0
+                          "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
+        return -VTD_FR_FS_NON_CANONICAL;
+    }
+
    while (true) {
        offset = vtd_iova_fl_level_offset(iova, level);
        flpte = vtd_get_flpte(addr, offset);
hw/i386/intel_iommu_internal.h +0 -0
index 901691afb9..e9448291a4 100644
@@ -324,6 +324,8 @@ typedef enum VTDFaultReason {
    VTD_FR_PASID_ENTRY_P = 0x59, /* The Present(P) field of pasidt-entry is

0 */

    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 */

--
2.44.0

#5 lore raw

Re: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Hi zhenzhong,

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

Hi Clement,

Quoted 27 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

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

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

2 files changed, 24 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 80cdf37870..240ecb8f72 100644
@@ -1912,6 +1912,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,
 };
 

hw/i386/intel_iommu.c static inline uint64_t · line 2033

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
@@ -2023,6 +2024,21 @@ static inline uint64_t
 vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)
     return flpte & VTD_FL_PT_BASE_ADDR_MASK(aw);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
+                                        uint64_t iova, VTDContextEntry *ce,
+                                        uint8_t aw, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, aw, pasid);

According to spec:

"Input-address in the request subjected to first-stage translation is not canonical (i.e., address bits 63:N are not same value as address bits [N- 1], where N is 48 bits with 4-level paging and 57 bits with 5-level paging)."

So it looks not correct to use aw filed in pasid entry to calculate iova_limit. Aw can be a value configured by guest and it's used for stage-2 table. See spec:

" This field is treated as Reserved(0) for implementations not supporting Second-stage Translation (SSTS=0 in the Extended Capability Register).
This field indicates the adjusted guest-address-width (AGAW) to be used by hardware for second-stage translation through paging structures referenced through the SSPTPTR field.
• The following encodings are defined for this field:
• 001b: 39-bit AGAW (3-level page table)
• 010b: 48-bit AGAW (4-level page table)
• 011b: 57-bit AGAW (5-level page table)
• 000b,100b-111b: Reserved
When not treated as Reserved(0), hardware ignores this field for first-stage-only (PGTT=001b) and pass-through (PGTT=100b) translations."

Quoted 3 lines

Thanks
Zhenzhong

Not sure to understand.
Are you talking about the aw field of Scalable-Mode PASID Table Entry? The aw parameter is set to s->aw_bits in vtd_do_iommu_translate so I think it's safe to use it for canonical address check. Maybe we can just use s->aw_bits directly from vtd_iova_fl_check_canonical to avoid any mistake?

Quoted 45 lines
Quoted 45 lines
hw/i386/intel_iommu.c +9 -0
+    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.

hw/i386/intel_iommu.c +2 -0
@@ -2038,6 +2054,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, aw_bits, pasid)) {
+        error_report_once("%s: detected non canonical IOVA (iova=0x%"

PRIx64 ","

hw/i386/intel_iommu.c +4 -0
+                          "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
+        return -VTD_FR_FS_NON_CANONICAL;
+    }
+
     while (true) {
         offset = vtd_iova_fl_level_offset(iova, level);
         flpte = vtd_get_flpte(addr, offset);
hw/i386/intel_iommu_internal.h +0 -0
index 901691afb9..e9448291a4 100644
@@ -324,6 +324,8 @@ typedef enum VTDFaultReason {
     VTD_FR_PASID_ENTRY_P = 0x59, /* The Present(P) field of pasidt-entry is

0 */

     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 */

--
2.44.0

#6 lore raw

RE: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Quoted 79 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Hi zhenzhong,

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

Hi Clement,

Quoted 3 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input

address

Quoted 6 lines
Quoted 6 lines

is canonical

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

Signed-off-by Clément Mathieu--Drif <clement.mathieu--

[email protected]>

Quoted 30 lines
Quoted 18 lines

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

2 files changed, 24 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 80cdf37870..240ecb8f72 100644
@@ -1912,6 +1912,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,
 };
 

hw/i386/intel_iommu.c static inline uint64_t · line 2033

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
@@ -2023,6 +2024,21 @@ static inline uint64_t
 vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)
     return flpte & VTD_FL_PT_BASE_ADDR_MASK(aw);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
+                                        uint64_t iova, VTDContextEntry *ce,
+                                        uint8_t aw, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, aw, pasid);

According to spec:

Quoted 6 lines

"Input-address in the request subjected to first-stage translation is not canonical (i.e., address bits 63:N are not same value as address bits [N- 1], where N is 48 bits with 4-level paging and 57 bits with 5-level paging)."

So it looks not correct to use aw filed in pasid entry to calculate iova_limit. Aw can be a value configured by guest and it's used for stage-2 table. See

spec:

Quoted 2 lines

" This field is treated as Reserved(0) for implementations not supporting

Second-stage

Quoted 2 lines

Translation (SSTS=0 in the Extended Capability Register).
This field indicates the adjusted guest-address-width (AGAW) to be used by

hardware

Quoted 1 line

for second-stage translation through paging structures referenced through

the

Quoted 7 lines

SSPTPTR field.
• The following encodings are defined for this field:
• 001b: 39-bit AGAW (3-level page table)
• 010b: 48-bit AGAW (4-level page table)
• 011b: 57-bit AGAW (5-level page table)
• 000b,100b-111b: Reserved
When not treated as Reserved(0), hardware ignores this field for first-

Quoted 8 lines

stage-only

Quoted 5 lines

(PGTT=001b) and pass-through (PGTT=100b) translations."

Thanks
Zhenzhong

Not sure to understand.
Are you talking about the aw field of Scalable-Mode PASID Table Entry?

Yes.

Quoted 4 lines

The aw parameter is set to s->aw_bits in vtd_do_iommu_translate so I think it's safe to use it for canonical address check. Maybe we can just use s->aw_bits directly from vtd_iova_fl_check_canonical to avoid any mistake?

Agaw can be different from s->aw_bits.
Yes, I think using s->aw_bits is safe.

Thanks
Zhenzhong

Quoted 49 lines
Quoted 11 lines

hw/i386/intel_iommu.c static inline uint64_t vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)

Quoted 11 lines
hw/i386/intel_iommu.c +9 -0
+    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

Quoted 2 lines

hw/i386/intel_iommu.c static int

Quoted 2 lines

* of the translation, can be used for deciding the size of large page.

hw/i386/intel_iommu.c +0 -0
@@ -2038,6 +2054,12 @@ static int

vtd_iova_to_flpte(IntelIOMMUState *s,

Quoted 13 lines
Quoted 13 lines
hw/i386/intel_iommu.c +2 -0
 VTDContextEntry *ce,
     uint32_t offset;
     uint64_t flpte;
 
+    if (!vtd_iova_fl_check_canonical(s, iova, ce, aw_bits, pasid)) {
+        error_report_once("%s: detected non canonical IOVA (iova=0x%"

PRIx64 ","

hw/i386/intel_iommu.c +4 -0
+                          "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
+        return -VTD_FR_FS_NON_CANONICAL;
+    }
+
     while (true) {
         offset = vtd_iova_fl_level_offset(iova, level);
Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef enum VTDFaultReason { · line 324

Quoted 8 lines
         flpte = vtd_get_flpte(addr, offset);
hw/i386/intel_iommu_internal.h +0 -0
index 901691afb9..e9448291a4 100644
@@ -324,6 +324,8 @@ typedef enum VTDFaultReason {
     VTD_FR_PASID_ENTRY_P = 0x59, /* The Present(P) field of pasidt-

entry is

Quoted 2 lines
Quoted 2 lines

0 */

     VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry

*/

Quoted 9 lines
Quoted 9 lines

+ 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 */

--
2.44.0

#7 lore raw

Re: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Hi,

Quoted 92 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 ats_vtd v1 03/24] intel_iommu: check if the input address is canonical

Hi zhenzhong,

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 6 lines

Hi Clement,

Quoted 3 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 03/24] intel_iommu: check if the input

address

Quoted 6 lines
Quoted 6 lines

is canonical

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

Signed-off-by Clément Mathieu--Drif <clement.mathieu--

[email protected]>

Quoted 30 lines
Quoted 18 lines

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

2 files changed, 24 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 80cdf37870..240ecb8f72 100644
@@ -1912,6 +1912,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,
 };
 

hw/i386/intel_iommu.c static inline uint64_t · line 2033

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
@@ -2023,6 +2024,21 @@ static inline uint64_t
 vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)
      return flpte & VTD_FL_PT_BASE_ADDR_MASK(aw);
 }
 
+/* Return true if IOVA is canonical, otherwise false. */
+static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
+                                        uint64_t iova, VTDContextEntry *ce,
+                                        uint8_t aw, uint32_t pasid)
+{
+    uint64_t iova_limit = vtd_iova_limit(s, ce, aw, pasid);

According to spec:

Quoted 6 lines

"Input-address in the request subjected to first-stage translation is not canonical (i.e., address bits 63:N are not same value as address bits [N- 1], where N is 48 bits with 4-level paging and 57 bits with 5-level paging)."

So it looks not correct to use aw filed in pasid entry to calculate iova_limit. Aw can be a value configured by guest and it's used for stage-2 table. See

spec:

Quoted 1 line

" This field is treated as Reserved(0) for implementations not supporting

Second-stage

Quoted 2 lines

Translation (SSTS=0 in the Extended Capability Register).
This field indicates the adjusted guest-address-width (AGAW) to be used by

hardware

Quoted 1 line

for second-stage translation through paging structures referenced through

the

Quoted 7 lines

SSPTPTR field.
• The following encodings are defined for this field:
• 001b: 39-bit AGAW (3-level page table)
• 010b: 48-bit AGAW (4-level page table)
• 011b: 57-bit AGAW (5-level page table)
• 000b,100b-111b: Reserved
When not treated as Reserved(0), hardware ignores this field for first-

Quoted 8 lines

stage-only

Quoted 5 lines

(PGTT=001b) and pass-through (PGTT=100b) translations."

Thanks
Zhenzhong

Not sure to understand.
Are you talking about the aw field of Scalable-Mode PASID Table Entry?

Yes.

Quoted 4 lines

The aw parameter is set to s->aw_bits in vtd_do_iommu_translate so I think it's safe to use it for canonical address check. Maybe we can just use s->aw_bits directly from vtd_iova_fl_check_canonical to avoid any mistake?

Quoted 5 lines

Agaw can be different from s->aw_bits.
Yes, I think using s->aw_bits is safe.

Thanks
Zhenzhong

Ok fine.

I did the change in the v2 I sent yesterday.

Thanks

Quoted 49 lines
Quoted 49 lines
Quoted 12 lines

hw/i386/intel_iommu.c static inline uint64_t vtd_get_flpte_addr(uint64_t flpte, uint8_t aw)

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

Quoted 2 lines

hw/i386/intel_iommu.c static int

Quoted 2 lines

* of the translation, can be used for deciding the size of large page.

hw/i386/intel_iommu.c +0 -0
@@ -2038,6 +2054,12 @@ static int

vtd_iova_to_flpte(IntelIOMMUState *s,

Quoted 13 lines
Quoted 13 lines
hw/i386/intel_iommu.c +2 -0
 VTDContextEntry *ce,
      uint32_t offset;
      uint64_t flpte;
 
+    if (!vtd_iova_fl_check_canonical(s, iova, ce, aw_bits, pasid)) {
+        error_report_once("%s: detected non canonical IOVA (iova=0x%"

PRIx64 ","

hw/i386/intel_iommu.c +4 -0
+                          "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
+        return -VTD_FR_FS_NON_CANONICAL;
+    }
+
      while (true) {
          offset = vtd_iova_fl_level_offset(iova, level);
Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef enum VTDFaultReason { · line 324

Quoted 8 lines
          flpte = vtd_get_flpte(addr, offset);
hw/i386/intel_iommu_internal.h +0 -0
index 901691afb9..e9448291a4 100644
@@ -324,6 +324,8 @@ typedef enum VTDFaultReason {
      VTD_FR_PASID_ENTRY_P = 0x59, /* The Present(P) field of pasidt-

entry is

Quoted 2 lines
Quoted 2 lines

0 */

      VTD_FR_PASID_TABLE_ENTRY_INV = 0x5b,  /*Invalid PASID table entry

*/

Quoted 8 lines
Quoted 8 lines

+ 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 */

--
2.44.0

#8 lore raw

PATCH 4/24 intel_iommu: set accessed and dirty bits during first stage translation

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c          | 26 ++++++++++++++++++++++++++
 hw/i386/intel_iommu_internal.h |  3 +++
 2 files changed, 29 insertions(+)
hw/i386/intel_iommu.c +26 -0
index 240ecb8f72..cad70e0d05 100644
@@ -1913,6 +1913,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,
 };
 
@@ -2039,6 +2040,20 @@ static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s,
             );
 }
 
+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.
@@ -2080,11 +2095,22 @@ static int vtd_iova_to_flpte(IntelIOMMUState *s, VTDContextEntry *ce,
 
         *reads = true;
         *writes = (*writes) && (flpte & VTD_FL_RW_MASK);
+
+        if (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_PTE_A)
+                                                                != MEMTX_OK) {
+            return -VTD_FR_FS_BIT_UPDATE_FAILED;
+        }
+
         if (is_write && !(flpte & VTD_FL_RW_MASK)) {
             return -VTD_FR_WRITE;
         }
 
         if (vtd_is_last_flpte(flpte, level)) {
+            if (is_write &&
+                (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_PTE_D) !=
+                                                                    MEMTX_OK)) {
+                    return -VTD_FR_FS_BIT_UPDATE_FAILED;
+            }
             *flptep = flpte;
             *flpte_level = level;
             return 0;
hw/i386/intel_iommu_internal.h +3 -0
index e9448291a4..14879d3a58 100644
@@ -328,6 +328,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;
 
@@ -649,6 +650,8 @@ typedef struct VTDPIOTLBInvInfo {
 /* First Level Paging Structure */
 #define VTD_FL_PT_LEVEL             1
 #define VTD_FL_PT_ENTRY_NR          512
+#define VTD_FL_PTE_A                0x20
+#define VTD_FL_PTE_D                0x40
 
 /* Masks for First Level Paging Entry */
 #define VTD_FL_RW_MASK              (1ULL << 1)
Signature · 1 line
2.44.0
#9 lore raw

RE: [PATCH ats_vtd v1 04/24] intel_iommu: set accessed and dirty bits during first stage translation

Quoted 52 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 04/24] intel_iommu: set accessed and dirty bits during first stage translation

---
hw/i386/intel_iommu.c | 26 ++++++++++++++++++++++++++
hw/i386/intel_iommu_internal.h | 3 +++

2 files changed, 29 insertions(+)
hw/i386/intel_iommu.c +15 -0
index 240ecb8f72..cad70e0d05 100644
@@ -1913,6 +1913,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,
 };
 
@@ -2039,6 +2040,20 @@ static bool
 vtd_iova_fl_check_canonical(IntelIOMMUState *s,
            );
 }
 
+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.

hw/i386/intel_iommu.c +0 -0
@@ -2080,11 +2095,22 @@ static int vtd_iova_to_flpte(IntelIOMMUState
 *s, VTDContextEntry *ce,
 
        *reads = true;

hw/i386/intel_iommu.c static int vtd_iova_to_flpte(IntelIOMMUState · line 2107

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
        *writes = (*writes) && (flpte & VTD_FL_RW_MASK);
+
+        if (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_PTE_A)
+                                                                != MEMTX_OK) {
+            return -VTD_FR_FS_BIT_UPDATE_FAILED;
+        }
+
        if (is_write && !(flpte & VTD_FL_RW_MASK)) {
            return -VTD_FR_WRITE;
        }

May be better to set access bit here?
Speculatively setting access bit is allowed but not necessary.

Thanks
Zhenzhong

Quoted 34 lines
hw/i386/intel_iommu.c +5 -0
        if (vtd_is_last_flpte(flpte, level)) {
+            if (is_write &&
+                (vtd_set_flag_in_pte(addr, offset, flpte, VTD_FL_PTE_D) !=
+                                                                    MEMTX_OK)) {
+                    return -VTD_FR_FS_BIT_UPDATE_FAILED;
+            }
            *flptep = flpte;
            *flpte_level = level;
            return 0;
hw/i386/intel_iommu_internal.h +3 -1
index e9448291a4..14879d3a58 100644
@@ -328,6 +328,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;
 
@@ -649,6 +650,8 @@ typedef struct VTDPIOTLBInvInfo {
 /* First Level Paging Structure */
 #define VTD_FL_PT_LEVEL             1
 #define VTD_FL_PT_ENTRY_NR          512
+#define VTD_FL_PTE_A                0x20
+#define VTD_FL_PTE_D                0x40
 
 /* Masks for First Level Paging Entry */
 #define VTD_FL_RW_MASK              (1ULL << 1)
--

2.44.0

#10 lore raw

PATCH 5/24 intel_iommu: extract device IOTLB invalidation logic

In reply to CLEMENT MATHIEU--DRIF

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

---
 hw/i386/intel_iommu.c | 57 +++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 24 deletions(-)
hw/i386/intel_iommu.c +33 -24
index cad70e0d05..85a7ebac67 100644
@@ -4295,6 +4295,38 @@ 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_piotlb_desc(IntelIOMMUState *s,
                                            VTDInvDesc *inv_desc)
 {
@@ -4310,9 +4342,7 @@ 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;
 
@@ -4337,28 +4367,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.44.0
#11 lore raw

PATCH 6/24 intel_iommu: do not consider wait_desc as an invalid descriptor

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +5 -0
index 85a7ebac67..c475a354a0 100644
@@ -3365,6 +3365,11 @@ static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
     } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
         /* Interrupt flag */
         vtd_generate_completion_event(s);
+    } else if (inv_desc->lo & VTD_INV_DESC_WAIT_FN) {
+        /*
+         * SW = 0, IF = 0, FN = 1
+         * Nothing to do as we process the events sequentially
+         */
     } else {
         error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
                           " (unknown type)", __func__, inv_desc->hi,
Signature · 1 line
2.44.0
#12 lore raw

RE: [PATCH ats_vtd v1 06/24] intel_iommu: do not consider wait_desc as an invalid descriptor

Quoted 28 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 06/24] intel_iommu: do not consider wait_desc as an invalid descriptor

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

1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +5 -0
index 85a7ebac67..c475a354a0 100644
@@ -3365,6 +3365,11 @@ static bool
 vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
    } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
        /* Interrupt flag */
        vtd_generate_completion_event(s);
+    } else if (inv_desc->lo & VTD_INV_DESC_WAIT_FN) {
+        /*
+         * SW = 0, IF = 0, FN = 1
+         * Nothing to do as we process the events sequentially
+         */
    } else {
        error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
Quoted 1 line
                          " (unknown type)", __func__, inv_desc->hi,

LGTM.

Thanks
Zhenzhong

#13 lore raw

PATCH 7/24 memory: add permissions in IOMMUAccessFlags

In reply to CLEMENT MATHIEU--DRIF

This will be necessary for devices implementing ATS.
We also define a new macro IOMMU_ACCESS_FLAG_FULL in addition to IOMMU_ACCESS_FLAG to support more access flags. IOMMU_ACCESS_FLAG is kept for convenience and backward compatibility.

Here are the flags added (defined by the PCIe 5 specification) :
- Execute Requested
- Privileged Mode Requested
- Global
- Untranslated Only

IOMMU_ACCESS_FLAG sets the additional flags to 0

---
 include/exec/memory.h | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
include/exec/memory.h +26 -7
index 8626a355b3..304504de02 100644
@@ -110,22 +110,41 @@ struct MemoryRegionSection {
 
 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
 
-/* See address_space_translate: bit 0 is read, bit 1 is write.  */
+/*
+ * See address_space_translate:
+ *      - bit 0 : read
+ *      - bit 1 : write
+ *      - bit 2 : exec
+ *      - bit 3 : priv
+ *      - bit 4 : global
+ *      - bit 5 : untranslated only
+ */
 typedef enum {
     IOMMU_NONE = 0,
     IOMMU_RO   = 1,
     IOMMU_WO   = 2,
     IOMMU_RW   = 3,
+    IOMMU_EXEC = 4,
+    IOMMU_PRIV = 8,
+    IOMMU_GLOBAL = 16,
+    IOMMU_UNTRANSLATED_ONLY = 32,
 } IOMMUAccessFlags;
 
-#define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
+#define IOMMU_ACCESS_FLAG(r, w)     (((r) ? IOMMU_RO : 0) | \
+                                    ((w) ? IOMMU_WO : 0))
+#define IOMMU_ACCESS_FLAG_FULL(r, w, x, p, g, uo) \
+                                    (IOMMU_ACCESS_FLAG(r, w) | \
+                                    ((x) ? IOMMU_EXEC : 0) | \
+                                    ((p) ? IOMMU_PRIV : 0) | \
+                                    ((g) ? IOMMU_GLOBAL : 0) | \
+                                    ((uo) ? IOMMU_UNTRANSLATED_ONLY : 0))
 
 struct IOMMUTLBEntry {
-    AddressSpace    *target_as;
-    hwaddr           iova;
-    hwaddr           translated_addr;
-    hwaddr           addr_mask;  /* 0xfff = 4k translation */
-    IOMMUAccessFlags perm;
+    AddressSpace            *target_as;
+    hwaddr                  iova;
+    hwaddr                  translated_addr;
+    hwaddr                  addr_mask;  /* 0xfff = 4k translation */
+    IOMMUAccessFlags        perm;
 };
 
 /*
Signature · 1 line
2.44.0
#14 lore raw

RE: [PATCH ats_vtd v1 07/24] memory: add permissions in IOMMUAccessFlags

Quoted 67 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 07/24] memory: add permissions in IOMMUAccessFlags

This will be necessary for devices implementing ATS.
We also define a new macro IOMMU_ACCESS_FLAG_FULL in addition to IOMMU_ACCESS_FLAG to support more access flags. IOMMU_ACCESS_FLAG is kept for convenience and backward compatibility.

Here are the flags added (defined by the PCIe 5 specification) :
- Execute Requested
- Privileged Mode Requested
- Global
- Untranslated Only

IOMMU_ACCESS_FLAG sets the additional flags to 0

---
include/exec/memory.h | 33 ++++++++++++++++++++++++++-------

1 file changed, 26 insertions(+), 7 deletions(-)
include/exec/memory.h +21 -3
index 8626a355b3..304504de02 100644
@@ -110,22 +110,41 @@ struct MemoryRegionSection {
 
 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
 
-/* See address_space_translate: bit 0 is read, bit 1 is write.  */
+/*
+ * See address_space_translate:
+ *      - bit 0 : read
+ *      - bit 1 : write
+ *      - bit 2 : exec
+ *      - bit 3 : priv
+ *      - bit 4 : global
+ *      - bit 5 : untranslated only
+ */
 typedef enum {
    IOMMU_NONE = 0,
    IOMMU_RO   = 1,
    IOMMU_WO   = 2,
    IOMMU_RW   = 3,
+    IOMMU_EXEC = 4,
+    IOMMU_PRIV = 8,
+    IOMMU_GLOBAL = 16,
+    IOMMU_UNTRANSLATED_ONLY = 32,
 } IOMMUAccessFlags;
 
-#define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ?
 IOMMU_WO : 0))
+#define IOMMU_ACCESS_FLAG(r, w)     (((r) ? IOMMU_RO : 0) | \
+                                    ((w) ? IOMMU_WO : 0))
+#define IOMMU_ACCESS_FLAG_FULL(r, w, x, p, g, uo) \
+                                    (IOMMU_ACCESS_FLAG(r, w) | \
+                                    ((x) ? IOMMU_EXEC : 0) | \
+                                    ((p) ? IOMMU_PRIV : 0) | \
+                                    ((g) ? IOMMU_GLOBAL : 0) | \
+                                    ((uo) ? IOMMU_UNTRANSLATED_ONLY : 0))
 
 struct IOMMUTLBEntry {
-    AddressSpace    *target_as;

include/exec/memory.h struct MemoryRegionSection { · line 149

Quoted 10 lines
include/exec/memory.h +5 -4
-    hwaddr           iova;
-    hwaddr           translated_addr;
-    hwaddr           addr_mask;  /* 0xfff = 4k translation */
-    IOMMUAccessFlags perm;
+    AddressSpace            *target_as;
+    hwaddr                  iova;
+    hwaddr                  translated_addr;
+    hwaddr                  addr_mask;  /* 0xfff = 4k translation */
+    IOMMUAccessFlags        perm;
 };

Any reason for this change?

Thanks
Zhenzhong

#15 lore raw

PATCH 8/24 pcie: add helper to declare PASID capability for a pcie device

In reply to CLEMENT MATHIEU--DRIF
---
 hw/pci/pcie.c              | 24 ++++++++++++++++++++++++
 include/hw/pci/pcie.h      |  6 +++++-
 include/hw/pci/pcie_regs.h |  3 +++
 3 files changed, 32 insertions(+), 1 deletion(-)
hw/pci/pcie.c +24 -0
index 4b2f0805c6..c8e9d4c0f7 100644
@@ -1177,3 +1177,27 @@ void pcie_acs_reset(PCIDevice *dev)
         pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
     }
 }
+
+/* PASID */
+void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
+                     bool exec_perm, bool priv_mod)
+{
+    assert(pasid_width <= PCI_EXT_CAP_PASID_MAX_WIDTH);
+    static const uint16_t control_reg_rw_mask = 0x07;
+    uint16_t capability_reg = pasid_width;
+
+    pcie_add_capability(dev, PCI_EXT_CAP_ID_PASID, PCI_PASID_VER, offset,
+                        PCI_EXT_CAP_PASID_SIZEOF);
+
+    capability_reg <<= PCI_EXT_CAP_PASID_SIZEOF;
+    capability_reg |= exec_perm ? PCI_PASID_CAP_EXEC : 0;
+    capability_reg |= priv_mod  ? PCI_PASID_CAP_PRIV : 0;
+    pci_set_word(dev->config + offset + PCI_PASID_CAP, capability_reg);
+
+    /* Everything is disabled by default */
+    pci_set_word(dev->config + offset + PCI_PASID_CTRL, 0);
+
+    pci_set_word(dev->wmask + offset + PCI_PASID_CTRL, control_reg_rw_mask);
+
+    dev->exp.pasid_cap = offset;
+}
include/hw/pci/pcie.h +5 -1
index 11f5a91bbb..c59627d556 100644
@@ -69,8 +69,9 @@ struct PCIExpressDevice {
     uint16_t aer_cap;
     PCIEAERLog aer_log;
 
-    /* Offset of ATS capability in config space */
+    /* Offset of ATS and PASID capabilities in config space */
     uint16_t ats_cap;
+    uint16_t pasid_cap;
 
     /* ACS */
     uint16_t acs_cap;
@@ -147,4 +148,7 @@ void pcie_cap_slot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
                              Error **errp);
 void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp);
+
+void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
+                     bool exec_perm, bool priv_mod);
 #endif /* QEMU_PCIE_H */
include/hw/pci/pcie_regs.h +3 -0
index 9d3b6868dc..0a86598f80 100644
@@ -86,6 +86,9 @@ typedef enum PCIExpLinkWidth {
 #define PCI_ARI_VER                     1
 #define PCI_ARI_SIZEOF                  8
 
+/* PASID */
+#define PCI_PASID_VER                   1
+#define PCI_EXT_CAP_PASID_MAX_WIDTH     20
 /* AER */
 #define PCI_ERR_VER                     2
 #define PCI_ERR_SIZEOF                  0x48
Signature · 1 line
2.44.0
#16 lore raw

RE: [PATCH ats_vtd v1 08/24] pcie: add helper to declare PASID capability for a pcie device

Quoted 25 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 08/24] pcie: add helper to declare PASID capability for a pcie device

---

hw/pci/pcie.c              | 24 ++++++++++++++++++++++++
include/hw/pci/pcie.h      |  6 +++++-
include/hw/pci/pcie_regs.h |  3 +++
3 files changed, 32 insertions(+), 1 deletion(-)
hw/pci/pcie.c +3 -0
index 4b2f0805c6..c8e9d4c0f7 100644
@@ -1177,3 +1177,27 @@ void pcie_acs_reset(PCIDevice *dev)
        pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
    }
 }
+
+/* PASID */
+void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,

hw/pci/pcie.c void pcie_acs_reset(PCIDevice *dev) · line 1192

Quoted 10 lines
hw/pci/pcie.c +10 -0
+                     bool exec_perm, bool priv_mod)
+{
+    assert(pasid_width <= PCI_EXT_CAP_PASID_MAX_WIDTH);
+    static const uint16_t control_reg_rw_mask = 0x07;
+    uint16_t capability_reg = pasid_width;
+
+    pcie_add_capability(dev, PCI_EXT_CAP_ID_PASID, PCI_PASID_VER, offset,
+                        PCI_EXT_CAP_PASID_SIZEOF);
+
+    capability_reg <<= PCI_EXT_CAP_PASID_SIZEOF;

Not understand why PCI_EXT_CAP_PASID_SIZEOF is used for shifting?

Quoted 52 lines
hw/pci/pcie.c +8 -0
+    capability_reg |= exec_perm ? PCI_PASID_CAP_EXEC : 0;
+    capability_reg |= priv_mod  ? PCI_PASID_CAP_PRIV : 0;
+    pci_set_word(dev->config + offset + PCI_PASID_CAP, capability_reg);
+
+    /* Everything is disabled by default */
+    pci_set_word(dev->config + offset + PCI_PASID_CTRL, 0);
+
+    pci_set_word(dev->wmask + offset + PCI_PASID_CTRL,

control_reg_rw_mask);

hw/pci/pcie.c +3 -0
+
+    dev->exp.pasid_cap = offset;
+}
include/hw/pci/pcie.h +5 -1
index 11f5a91bbb..c59627d556 100644
@@ -69,8 +69,9 @@ struct PCIExpressDevice {
    uint16_t aer_cap;
    PCIEAERLog aer_log;
 
-    /* Offset of ATS capability in config space */
+    /* Offset of ATS and PASID capabilities in config space */
    uint16_t ats_cap;
+    uint16_t pasid_cap;
 
    /* ACS */
    uint16_t acs_cap;
@@ -147,4 +148,7 @@ void pcie_cap_slot_unplug_cb(HotplugHandler
 *hotplug_dev, DeviceState *dev,
                             Error **errp);
 void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
                                     DeviceState *dev, Error **errp);
+
+void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
+                     bool exec_perm, bool priv_mod);

#endif /* QEMU_PCIE_H */

include/hw/pci/pcie_regs.h +3 -1
index 9d3b6868dc..0a86598f80 100644
@@ -86,6 +86,9 @@ typedef enum PCIExpLinkWidth {
 #define PCI_ARI_VER                     1
 #define PCI_ARI_SIZEOF                  8
 
+/* PASID */
+#define PCI_PASID_VER                   1
+#define PCI_EXT_CAP_PASID_MAX_WIDTH     20
 /* AER */
 #define PCI_ERR_VER                     2
 #define PCI_ERR_SIZEOF                  0x48
--

2.44.0

#17 lore raw

PATCH 9/24 pcie: helper functions to check if PASID and ATS are enabled

In reply to CLEMENT MATHIEU--DRIF

ats_enabled and pasid_enabled check whether the capabilities are present or not. If so, we read the configuration space to get the status of the feature (enabled or not).

---
 hw/pci/pcie.c         | 18 ++++++++++++++++++
 include/hw/pci/pcie.h |  3 +++
 2 files changed, 21 insertions(+)
hw/pci/pcie.c +18 -0
index c8e9d4c0f7..2a638a9c3f 100644
@@ -1201,3 +1201,21 @@ void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
 
     dev->exp.pasid_cap = offset;
 }
+
+bool pcie_pasid_enabled(const PCIDevice *dev)
+{
+    if (!pci_is_express(dev) || !dev->exp.pasid_cap) {
+        return false;
+    }
+    return (pci_get_word(dev->config + dev->exp.pasid_cap + PCI_PASID_CTRL) &
+                PCI_PASID_CTRL_ENABLE) != 0;
+}
+
+bool pcie_ats_enabled(const PCIDevice *dev)
+{
+    if (!pci_is_express(dev) || !dev->exp.ats_cap) {
+        return false;
+    }
+    return (pci_get_word(dev->config + dev->exp.ats_cap + PCI_ATS_CTRL) &
+                PCI_ATS_CTRL_ENABLE) != 0;
+}
include/hw/pci/pcie.h +3 -0
index c59627d556..8c222f09da 100644
@@ -151,4 +151,7 @@ void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
 
 void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
                      bool exec_perm, bool priv_mod);
+
+bool pcie_pasid_enabled(const PCIDevice *dev);
+bool pcie_ats_enabled(const PCIDevice *dev);
 #endif /* QEMU_PCIE_H */
Signature · 1 line
2.44.0
#18 lore raw

RE: [PATCH ats_vtd v1 09/24] pcie: helper functions to check if PASID and ATS are enabled

Quoted 10 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 09/24] pcie: helper functions to check if PASID and ATS are enabled

ats_enabled and pasid_enabled check whether the capabilities are present or not. If so, we read the configuration space to get the status of the feature (enabled or not).

s/present/enabled/

Quoted 50 lines

---
hw/pci/pcie.c | 18 ++++++++++++++++++
include/hw/pci/pcie.h | 3 +++

2 files changed, 21 insertions(+)
hw/pci/pcie.c +0 -0
index c8e9d4c0f7..2a638a9c3f 100644
@@ -1201,3 +1201,21 @@ void pcie_pasid_init(PCIDevice *dev, uint16_t
 offset, uint8_t pasid_width,
 
    dev->exp.pasid_cap = offset;

}

hw/pci/pcie.c +7 -0
+
+bool pcie_pasid_enabled(const PCIDevice *dev)
+{
+    if (!pci_is_express(dev) || !dev->exp.pasid_cap) {
+        return false;
+    }
+    return (pci_get_word(dev->config + dev->exp.pasid_cap +

PCI_PASID_CTRL) &

hw/pci/pcie.c +11 -0
+                PCI_PASID_CTRL_ENABLE) != 0;
+}
+
+bool pcie_ats_enabled(const PCIDevice *dev)
+{
+    if (!pci_is_express(dev) || !dev->exp.ats_cap) {
+        return false;
+    }
+    return (pci_get_word(dev->config + dev->exp.ats_cap + PCI_ATS_CTRL) &
+                PCI_ATS_CTRL_ENABLE) != 0;
+}
include/hw/pci/pcie.h +3 -0
index c59627d556..8c222f09da 100644
@@ -151,4 +151,7 @@ void
 pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
 
 void pcie_pasid_init(PCIDevice *dev, uint16_t offset, uint8_t pasid_width,
                     bool exec_perm, bool priv_mod);
+
+bool pcie_pasid_enabled(const PCIDevice *dev);
+bool pcie_ats_enabled(const PCIDevice *dev);

#endif /* QEMU_PCIE_H */
--
2.44.0

#19 lore raw

PATCH 10/24 intel_iommu: declare supported PASID size

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c          | 2 +-
 hw/i386/intel_iommu_internal.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +1 -1
index c475a354a0..98c4a70fe0 100644
@@ -5822,7 +5822,7 @@ static void vtd_cap_init(IntelIOMMUState *s)
     }
 
     if (s->pasid) {
-        s->ecap |= VTD_ECAP_PASID;
+        s->ecap |= VTD_ECAP_PASID | VTD_ECAP_PSS;
     }
 }
 
hw/i386/intel_iommu_internal.h +1 -0
index 14879d3a58..d63ff049a7 100644
@@ -193,6 +193,7 @@
 #define VTD_ECAP_MHMV               (15ULL << 20)
 #define VTD_ECAP_NEST               (1ULL << 26)
 #define VTD_ECAP_SRS                (1ULL << 31)
+#define VTD_ECAP_PSS                (19ULL << 35)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
 #define VTD_ECAP_SLTS               (1ULL << 46)
Signature · 1 line
2.44.0
#20 lore raw

PATCH 11/24 intel_iommu: add an internal API to find an address space with PASID

In reply to CLEMENT MATHIEU--DRIF

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 98c4a70fe0..fe97930774 100644
@@ -65,6 +65,11 @@ struct vtd_as_key {
     uint32_t pasid;
 };
 
+struct vtd_as_raw_key {
+    uint16_t sid;
+    uint32_t pasid;
+};
+
 struct vtd_iotlb_key {
     uint64_t gfn;
     uint32_t pasid;
@@ -1931,29 +1936,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.44.0
#21 lore raw

PATCH 12/24 intel_iommu: add support for PASID-based device IOTLB invalidation

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c          | 42 ++++++++++++++++++++++++++++++----
 hw/i386/intel_iommu_internal.h | 10 ++++++++
 2 files changed, 47 insertions(+), 5 deletions(-)
hw/i386/intel_iommu.c +37 -5
index fe97930774..e7c1a5582a 100644
@@ -4344,11 +4344,43 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
 static bool vtd_process_device_piotlb_desc(IntelIOMMUState *s,
                                            VTDInvDesc *inv_desc)
 {
-    /*
-     * no need to handle it for passthru device, for emulated
-     * devices with device tlb, it may be required, but for now,
-     * return is enough
-     */
+    uint16_t sid;
+    VTDAddressSpace *vtd_dev_as;
+    bool size;
+    bool global;
+    hwaddr addr;
+    uint32_t pasid;
+
+    if ((inv_desc->hi & VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_HI) ||
+         (inv_desc->lo & VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_LO)) {
+        error_report_once("%s: invalid pasid-based dev iotlb inv desc:"
+                          "hi=%"PRIx64 "(reserved nonzero)",
+                          __func__, inv_desc->hi);
+        return false;
+    }
+
+    global = VTD_INV_DESC_PASID_DEVICE_IOTLB_GLOBAL(inv_desc->hi);
+    size = VTD_INV_DESC_PASID_DEVICE_IOTLB_SIZE(inv_desc->hi);
+    addr = VTD_INV_DESC_PASID_DEVICE_IOTLB_ADDR(inv_desc->hi);
+    sid = VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(inv_desc->lo);
+    if (global) {
+        QLIST_FOREACH(vtd_dev_as, &s->vtd_as_with_notifiers, next) {
+            if ((vtd_dev_as->pasid != PCI_NO_PASID) &&
+                (PCI_BUILD_BDF(pci_bus_num(vtd_dev_as->bus),
+                                           vtd_dev_as->devfn) == sid)) {
+                do_invalidate_device_tlb(vtd_dev_as, size, addr);
+            }
+        }
+    } else {
+        pasid = VTD_INV_DESC_PASID_DEVICE_IOTLB_PASID(inv_desc->lo);
+        vtd_dev_as = vtd_get_as_by_sid_and_pasid(s, sid, pasid);
+        if (!vtd_dev_as) {
+            return true;
+        }
+
+        do_invalidate_device_tlb(vtd_dev_as, size, addr);
+    }
+
     return true;
 }
 
hw/i386/intel_iommu_internal.h +10 -0
index d63ff049a7..3d59e10488 100644
@@ -424,6 +424,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
 
Signature · 1 line
2.44.0
#22 lore raw

PATCH 13/24 pci: cache the bus mastering status in the device

In reply to CLEMENT MATHIEU--DRIF
---
 hw/pci/pci.c                | 24 ++++++++++++++----------
 include/hw/pci/pci_device.h |  1 +
 2 files changed, 15 insertions(+), 10 deletions(-)
hw/pci/pci.c +14 -10
index 045d69f4c1..e5f72f9f1d 100644
@@ -116,6 +116,12 @@ static GSequence *pci_acpi_index_list(void)
     return used_acpi_index_list;
 }
 
+static void pci_set_master(PCIDevice *d, bool enable)
+{
+    memory_region_set_enabled(&d->bus_master_enable_region, enable);
+    d->is_master = enable; /* cache the status */
+}
+
 static void pci_init_bus_master(PCIDevice *pci_dev)
 {
     AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev);
@@ -123,7 +129,7 @@ static void pci_init_bus_master(PCIDevice *pci_dev)
     memory_region_init_alias(&pci_dev->bus_master_enable_region,
                              OBJECT(pci_dev), "bus master",
                              dma_as->root, 0, memory_region_size(dma_as->root));
-    memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
+    pci_set_master(pci_dev, false);
     memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,
                                 &pci_dev->bus_master_enable_region);
 }
@@ -657,9 +663,8 @@ static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
         pci_bridge_update_mappings(PCI_BRIDGE(s));
     }
 
-    memory_region_set_enabled(&s->bus_master_enable_region,
-                              pci_get_word(s->config + PCI_COMMAND)
-                              & PCI_COMMAND_MASTER);
+    pci_set_master(s,
+                   pci_get_word(s->config + PCI_COMMAND) & PCI_COMMAND_MASTER);
 
     g_free(config);
     return 0;
@@ -1611,9 +1616,9 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int
 
     if (ranges_overlap(addr, l, PCI_COMMAND, 2)) {
         pci_update_irq_disabled(d, was_irq_disabled);
-        memory_region_set_enabled(&d->bus_master_enable_region,
-                                  (pci_get_word(d->config + PCI_COMMAND)
-                                   & PCI_COMMAND_MASTER) && d->has_power);
+        pci_set_master(d,
+                      (pci_get_word(d->config + PCI_COMMAND) &
+                            PCI_COMMAND_MASTER) && d->has_power);
     }
 
     msi_write_config(d, addr, val_in, l);
@@ -2888,9 +2893,8 @@ void pci_set_power(PCIDevice *d, bool state)
 
     d->has_power = state;
     pci_update_mappings(d);
-    memory_region_set_enabled(&d->bus_master_enable_region,
-                              (pci_get_word(d->config + PCI_COMMAND)
-                               & PCI_COMMAND_MASTER) && d->has_power);
+    pci_set_master(d, (pci_get_word(d->config + PCI_COMMAND)
+                        & PCI_COMMAND_MASTER) && d->has_power);
     if (!d->has_power) {
         pci_device_reset(d);
     }
include/hw/pci/pci_device.h +1 -0
index d3dd0f64b2..7fa501569a 100644
@@ -87,6 +87,7 @@ struct PCIDevice {
     char name[64];
     PCIIORegion io_regions[PCI_NUM_REGIONS];
     AddressSpace bus_master_as;
+    bool is_master;
     MemoryRegion bus_master_container_region;
     MemoryRegion bus_master_enable_region;
 
Signature · 1 line
2.44.0
#23 lore raw

PATCH 14/24 pci: add IOMMU operations to get address spaces and memory regions with PASID

In reply to CLEMENT MATHIEU--DRIF
---
 hw/pci/pci.c         | 20 ++++++++++++++++++++
 include/hw/pci/pci.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
hw/pci/pci.c +20 -0
index e5f72f9f1d..9ed788c95d 100644
@@ -2747,6 +2747,26 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
     return &address_space_memory;
 }
 
+AddressSpace *pci_device_iommu_address_space_pasid(PCIDevice *dev,
+                                                   uint32_t pasid)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    int devfn;
+
+    if (!dev->is_master || !pcie_pasid_enabled(dev) || pasid == PCI_NO_PASID) {
+        return NULL;
+    }
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops &&
+            iommu_bus->iommu_ops->get_address_space_pasid) {
+        return iommu_bus->iommu_ops->get_address_space_pasid(bus,
+                                    iommu_bus->iommu_opaque, devfn, pasid);
+    }
+    return NULL;
+}
+
 int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                 Error **errp)
 {
include/hw/pci/pci.h +34 -0
index 849e391813..0c532c563c 100644
@@ -385,6 +385,38 @@ typedef struct PCIIOMMUOps {
      * @devfn: device and function number
      */
     AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @get_address_space_pasid: same as get_address_space but returns an
+     * address space with the requested PASID
+     *
+     * This callback is required for PASID-based operations
+     *
+     * @bus: the #PCIBus being accessed.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number
+     *
+     * @pasid: the pasid associated with the requested memory region
+     */
+    AddressSpace * (*get_address_space_pasid)(PCIBus *bus, void *opaque,
+                                              int devfn, uint32_t pasid);
+    /**
+     * @get_memory_region_pasid: get the iommu memory region for a given
+     * device and pasid
+     *
+     * @bus: the #PCIBus being accessed.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number
+     *
+     * @pasid: the pasid associated with the requested memory region
+     */
+    IOMMUMemoryRegion * (*get_memory_region_pasid)(PCIBus *bus,
+                                                   void *opaque,
+                                                   int devfn,
+                                                   uint32_t pasid);
     /**
      * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU
      *
@@ -420,6 +452,8 @@ typedef struct PCIIOMMUOps {
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+AddressSpace *pci_device_iommu_address_space_pasid(PCIDevice *dev,
+                                                   uint32_t pasid);
 int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                 Error **errp);
 void pci_device_unset_iommu_device(PCIDevice *dev);
Signature · 1 line
2.44.0
#24 lore raw

RE: [PATCH ats_vtd v1 14/24] pci: add IOMMU operations to get address spaces and memory regions with PASID

Quoted 26 lines

-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH ats_vtd v1 14/24] pci: add IOMMU operations to get address spaces and memory regions with PASID

---
hw/pci/pci.c | 20 ++++++++++++++++++++
include/hw/pci/pci.h | 34 ++++++++++++++++++++++++++++++++++

2 files changed, 54 insertions(+)
hw/pci/pci.c +4 -0
index e5f72f9f1d..9ed788c95d 100644
@@ -2747,6 +2747,26 @@ AddressSpace
 *pci_device_iommu_address_space(PCIDevice *dev)
    return &address_space_memory;
 }
 
+AddressSpace *pci_device_iommu_address_space_pasid(PCIDevice *dev,
+                                                   uint32_t pasid)
+{
+    PCIBus *bus;

hw/pci/pci.c AddressSpace · line 2764

Quoted 10 lines
hw/pci/pci.c +9 -0
+    PCIBus *iommu_bus;
+    int devfn;
+
+    if (!dev->is_master || !pcie_pasid_enabled(dev) || pasid ==
 PCI_NO_PASID) {
+        return NULL;
+    }
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops &&

This is implicitly checked in pci_device_get_iommu_bus_devfn(). Just do " if (iommu_bus) && "

Thanks
Zhenzhong

Quoted 69 lines
hw/pci/pci.c +7 -0
+            iommu_bus->iommu_ops->get_address_space_pasid) {
+        return iommu_bus->iommu_ops->get_address_space_pasid(bus,
+                                    iommu_bus->iommu_opaque, devfn, pasid);
+    }
+    return NULL;
+}
+

int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
Error **errp)
{

include/hw/pci/pci.h +2 -0
index 849e391813..0c532c563c 100644
@@ -385,6 +385,38 @@ typedef struct PCIIOMMUOps {
     * @devfn: device and function number
     */
    AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int
 devfn);
+    /**
+     * @get_address_space_pasid: same as get_address_space but returns

an

include/hw/pci/pci.h +16 -0
+     * address space with the requested PASID
+     *
+     * This callback is required for PASID-based operations
+     *
+     * @bus: the #PCIBus being accessed.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number
+     *
+     * @pasid: the pasid associated with the requested memory region
+     */
+    AddressSpace * (*get_address_space_pasid)(PCIBus *bus, void *opaque,
+                                              int devfn, uint32_t pasid);
+    /**
+     * @get_memory_region_pasid: get the iommu memory region for a

given

include/hw/pci/pci.h +16 -0
+     * device and pasid
+     *
+     * @bus: the #PCIBus being accessed.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number
+     *
+     * @pasid: the pasid associated with the requested memory region
+     */
+    IOMMUMemoryRegion * (*get_memory_region_pasid)(PCIBus *bus,
+                                                   void *opaque,
+                                                   int devfn,
+                                                   uint32_t pasid);
    /**
     * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU
     *
@@ -420,6 +452,8 @@ typedef struct PCIIOMMUOps {
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+AddressSpace *pci_device_iommu_address_space_pasid(PCIDevice *dev,
+                                                   uint32_t pasid);

int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,

                                Error **errp);

void pci_device_unset_iommu_device(PCIDevice *dev); --
2.44.0

#25 lore raw

PATCH 15/24 pci: add a pci-level initialization function for iommu notifiers

In reply to CLEMENT MATHIEU--DRIF

We add a convenient way to initialize an device-iotlb notifier. This is meant to be used by ATS-capable devices.

pci_device_iommu_memory_region_pasid is introduces in this commit and will be used in several other SVM-related functions exposed in the PCI API.

---
 hw/pci/pci.c         | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h | 13 +++++++++++++
 2 files changed, 52 insertions(+)
hw/pci/pci.c +39 -0
index 9ed788c95d..d10cdb3d75 100644
@@ -2747,6 +2747,45 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
     return &address_space_memory;
 }
 
+static IOMMUMemoryRegion *pci_device_iommu_memory_region_pasid(PCIDevice *dev,
+                                                               uint32_t pasid)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    int devfn;
+
+    /*
+     * This function is for internal use in the module,
+     * we can call it with PCI_NO_PASID
+     */
+    if (!dev->is_master ||
+            ((pasid != PCI_NO_PASID) && !pcie_pasid_enabled(dev))) {
+        return NULL;
+    }
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops &&
+            iommu_bus->iommu_ops->get_memory_region_pasid) {
+        return iommu_bus->iommu_ops->get_memory_region_pasid(bus,
+                                 iommu_bus->iommu_opaque, devfn, pasid);
+    }
+    return NULL;
+}
+
+bool pci_iommu_init_iotlb_notifier(PCIDevice *dev, uint32_t pasid,
+                                   IOMMUNotifier *n, IOMMUNotify fn)
+{
+    IOMMUMemoryRegion *iommu_mr = pci_device_iommu_memory_region_pasid(dev,
+                                                                        pasid);
+    if (!iommu_mr) {
+        return false;
+    }
+    iommu_notifier_init(n, fn, IOMMU_NOTIFIER_DEVIOTLB_EVENTS, 0, HWADDR_MAX,
+                        memory_region_iommu_attrs_to_index(iommu_mr,
+                                                       MEMTXATTRS_UNSPECIFIED));
+    return true;
+}
+
 AddressSpace *pci_device_iommu_address_space_pasid(PCIDevice *dev,
                                                    uint32_t pasid)
 {
include/hw/pci/pci.h +13 -0
index 0c532c563c..1587c18cd9 100644
@@ -458,6 +458,19 @@ int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
                                 Error **errp);
 void pci_device_unset_iommu_device(PCIDevice *dev);
 
+/**
+ * pci_iommu_init_iotlb_notifier: initialize an IOMMU notifier
+ *
+ * This function is used by devices before registering an IOTLB notifier
+ *
+ * @dev: the device
+ * @pasid: the pasid of the address space to watch
+ * @n: the notifier to initialize
+ * @fn: the callback to be installed
+ */
+bool pci_iommu_init_iotlb_notifier(PCIDevice *dev, uint32_t pasid,
+                                   IOMMUNotifier *n, IOMMUNotify fn);
+
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
  *
Signature · 1 line
2.44.0
#26 lore raw

PATCH 16/24 intel_iommu: implement the get_address_space_pasid iommu operation

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c         | 13 ++++++++++---
 include/hw/i386/intel_iommu.h |  2 +-
 2 files changed, 11 insertions(+), 4 deletions(-)
hw/i386/intel_iommu.c +10 -3
index e7c1a5582a..e9fa48b378 100644
@@ -5432,7 +5432,7 @@ static const MemoryRegionOps vtd_mem_ir_fault_ops = {
 };
 
 VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
-                                 int devfn, unsigned int pasid)
+                                 int devfn, uint32_t pasid)
 {
     /*
      * We can't simply use sid here since the bus number might not be
@@ -5983,19 +5983,26 @@ static void vtd_reset(DeviceState *dev)
     vtd_refresh_pasid_bind(s);
 }
 
-static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
+static AddressSpace *vtd_host_dma_iommu_pasid(PCIBus *bus, void *opaque,
+                                              int devfn, uint32_t pasid)
 {
     IntelIOMMUState *s = opaque;
     VTDAddressSpace *vtd_as;
 
     assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
 
-    vtd_as = vtd_find_add_as(s, bus, devfn, PCI_NO_PASID);
+    vtd_as = vtd_find_add_as(s, bus, devfn, pasid);
     return &vtd_as->as;
 }
 
+static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
+{
+    return vtd_host_dma_iommu_pasid(bus, opaque, devfn, PCI_NO_PASID);
+}
+
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
+    .get_address_space_pasid = vtd_host_dma_iommu_pasid,
     .set_iommu_device = vtd_dev_set_iommu_device,
     .unset_iommu_device = vtd_dev_unset_iommu_device,
 };
include/hw/i386/intel_iommu.h +1 -1
index 0d5b933159..bac40e4d40 100644
@@ -325,6 +325,6 @@ struct IntelIOMMUState {
  * create a new one if none exists
  */
 VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
-                                 int devfn, unsigned int pasid);
+                                 int devfn, uint32_t pasid);
 
 #endif
Signature · 1 line
2.44.0
#27 lore raw

PATCH 17/24 intel_iommu: implement the get_memory_region_pasid iommu operation

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/i386/intel_iommu.c +15 -0
index e9fa48b378..a62cbf303d 100644
@@ -6000,9 +6000,24 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
     return vtd_host_dma_iommu_pasid(bus, opaque, devfn, PCI_NO_PASID);
 }
 
+static IOMMUMemoryRegion *vtd_get_memory_region_pasid(PCIBus *bus,
+                                                      void *opaque,
+                                                      int devfn,
+                                                      uint32_t pasid)
+{
+    IntelIOMMUState *s = opaque;
+    VTDAddressSpace *vtd_as;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    vtd_as = vtd_find_add_as(s, bus, devfn, pasid);
+    return &vtd_as->iommu;
+}
+
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
     .get_address_space_pasid = vtd_host_dma_iommu_pasid,
+    .get_memory_region_pasid = vtd_get_memory_region_pasid,
     .set_iommu_device = vtd_dev_set_iommu_device,
     .unset_iommu_device = vtd_dev_unset_iommu_device,
 };
Signature · 1 line
2.44.0
#28 lore raw

PATCH 18/24 memory: Allow to store the PASID in IOMMUTLBEntry

In reply to CLEMENT MATHIEU--DRIF

This will be useful for devices that support ATS

---
 include/exec/memory.h | 1 +
 1 file changed, 1 insertion(+)
include/exec/memory.h +1 -0
index 304504de02..f4b33415d7 100644
@@ -145,6 +145,7 @@ struct IOMMUTLBEntry {
     hwaddr                  translated_addr;
     hwaddr                  addr_mask;  /* 0xfff = 4k translation */
     IOMMUAccessFlags        perm;
+    uint32_t                pasid;
 };
 
 /*
Signature · 1 line
2.44.0
#29 lore raw

PATCH 19/24 intel_iommu: fill the PASID field when creating an instance of IOMMUTLBEntry

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/i386/intel_iommu.c +5 -0
index a62cbf303d..02c5f0fa4f 100644
@@ -2302,6 +2302,7 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(pte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+    entry->pasid = pasid;
     return true;
 
 error:
@@ -2310,6 +2311,7 @@ error:
     entry->translated_addr = 0;
     entry->addr_mask = 0;
     entry->perm = IOMMU_NONE;
+    entry->pasid = PCI_NO_PASID;
     return false;
 }
 
@@ -4338,6 +4340,7 @@ static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
     event.entry.iova = addr;
     event.entry.perm = IOMMU_NONE;
     event.entry.translated_addr = 0;
+    event.entry.pasid = vtd_dev_as->pasid;
     memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
 }
 
@@ -4914,6 +4917,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
     IOMMUTLBEntry iotlb = {
         /* We'll fill in the rest later. */
         .target_as = &address_space_memory,
+        .pasid = vtd_as->pasid,
     };
     bool success;
 
@@ -4926,6 +4930,7 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
         iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
         iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
         iotlb.perm = IOMMU_RW;
+        iotlb.pasid = PCI_NO_PASID;
         success = true;
     }
 
Signature · 1 line
2.44.0
#30 lore raw

PATCH 20/24 atc: generic ATC that can be used by PCIe devices that support SVM

In reply to CLEMENT MATHIEU--DRIF

As the SVM-capable devices will need to cache translations, we provide an first implementation.

This cache uses a two-level design based on hash tables.
The first level is indexed by a PASID and the second by a virtual addresse.

---
 tests/unit/meson.build |   1 +
 tests/unit/test-atc.c  | 502 +++++++++++++++++++++++++++++++++++++++++
 util/atc.c             | 211 +++++++++++++++++
 util/atc.h             | 117 ++++++++++
 util/meson.build       |   1 +
 5 files changed, 832 insertions(+)
 create mode 100644 tests/unit/test-atc.c
 create mode 100644 util/atc.c
 create mode 100644 util/atc.h
tests/unit/meson.build +1 -0
index 228a21d03c..5c9a6fe9f4 100644
@@ -52,6 +52,7 @@ tests = {
   'test-interval-tree': [],
   'test-xs-node': [qom],
   'test-virtio-dmabuf': [meson.project_source_root() / 'hw/display/virtio-dmabuf.c'],
+  'test-atc': []
 }
 
 if have_system or have_tools
tests/unit/test-atc.c new file +502 -0

502 lines not on this page · raw message

util/atc.c new file +211 -0

211 lines not on this page · raw message

util/atc.h new file +117 -0

117 lines not on this page · raw message

util/meson.build +1 -0
index 0ef9886be0..a2e0e9e5d7 100644
@@ -94,6 +94,7 @@ if have_block
   util_ss.add(files('hbitmap.c'))
   util_ss.add(files('hexdump.c'))
   util_ss.add(files('iova-tree.c'))
+  util_ss.add(files('atc.c'))
   util_ss.add(files('iov.c', 'uri.c'))
   util_ss.add(files('nvdimm-utils.c'))
   util_ss.add(files('block-helpers.c'))
Signature · 1 line
2.44.0
#31 lore raw

PATCH 21/24 memory: add an API for ATS support

In reply to CLEMENT MATHIEU--DRIF

IOMMU have to implement iommu_ats_request_translation to support ATS.

Devices can use IOMMU_TLB_ENTRY_TRANSLATION_ERROR to check the tlb entries returned by a translation request.

---
 include/exec/memory.h | 26 ++++++++++++++++++++++++++
 system/memory.c       | 20 ++++++++++++++++++++
 2 files changed, 46 insertions(+)
include/exec/memory.h +26 -0
index f4b33415d7..5b157b9711 100644
@@ -148,6 +148,10 @@ struct IOMMUTLBEntry {
     uint32_t                pasid;
 };
 
+/* Check if an IOMMU TLB entry indicates a translation error */
+#define IOMMU_TLB_ENTRY_TRANSLATION_ERROR(entry) ((((entry)->perm) & IOMMU_RW) \
+                                                    == IOMMU_NONE)
+
 /*
  * Bitmap for different IOMMUNotifier capabilities. Each notifier can
  * register with one or multiple IOMMU Notifier capability bit(s).
@@ -567,6 +571,20 @@ struct IOMMUMemoryRegionClass {
      int (*iommu_set_iova_ranges)(IOMMUMemoryRegion *iommu,
                                   GList *iova_ranges,
                                   Error **errp);
+
+    /**
+     * @iommu_ats_request_translation:
+     * This method must be implemented if the IOMMU has ATS enabled
+     *
+     * @see pci_ats_request_translation_pasid
+     */
+    ssize_t (*iommu_ats_request_translation)(IOMMUMemoryRegion *iommu,
+                                             bool priv_req, bool exec_req,
+                                             hwaddr addr, size_t length,
+                                             bool no_write,
+                                             IOMMUTLBEntry *result,
+                                             size_t result_length,
+                                             uint32_t *err_count);
 };
 
 typedef struct RamDiscardListener RamDiscardListener;
@@ -1870,6 +1888,14 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
                                              IOMMUNotifier *n);
 
+ssize_t memory_region_iommu_ats_request_translation(IOMMUMemoryRegion *iommu_mr,
+                                                bool priv_req, bool exec_req,
+                                                hwaddr addr, size_t length,
+                                                bool no_write,
+                                                IOMMUTLBEntry *result,
+                                                size_t result_length,
+                                                uint32_t *err_count);
+
 /**
  * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
  * defined on the IOMMU.
system/memory.c +20 -0
index a229a79988..9c9418c5ee 100644
@@ -2000,6 +2000,26 @@ void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
     memory_region_update_iommu_notify_flags(iommu_mr, NULL);
 }
 
+ssize_t memory_region_iommu_ats_request_translation(IOMMUMemoryRegion *iommu_mr,
+                                                    bool priv_req,
+                                                    bool exec_req,
+                                                    hwaddr addr, size_t length,
+                                                    bool no_write,
+                                                    IOMMUTLBEntry *result,
+                                                    size_t result_length,
+                                                    uint32_t *err_count)
+{
+    IOMMUMemoryRegionClass *imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
+
+    if (!imrc->iommu_ats_request_translation) {
+        return -ENODEV;
+    }
+
+    return imrc->iommu_ats_request_translation(iommu_mr, priv_req, exec_req,
+                                               addr, length, no_write, result,
+                                               result_length, err_count);
+}
+
 void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
                                     IOMMUTLBEvent *event)
 {
Signature · 1 line
2.44.0
#32 lore raw

PATCH 22/24 pci: add a pci-level API for ATS

In reply to CLEMENT MATHIEU--DRIF

Devices implementing ATS can send translation requests using pci_ats_request_translation_pasid.

The invalidation events are sent back to the device using the iommu notifier managed with pci_register_iommu_tlb_event_notifier and pci_unregister_iommu_tlb_event_notifier

---
 hw/pci/pci.c         | 44 +++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h | 52 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
hw/pci/pci.c +44 -0
index d10cdb3d75..fea9b57006 100644
@@ -2833,6 +2833,50 @@ void pci_device_unset_iommu_device(PCIDevice *dev)
     }
 }
 
+ssize_t pci_ats_request_translation_pasid(PCIDevice *dev, uint32_t pasid,
+                                          bool priv_req, bool exec_req, hwaddr addr,
+                                          size_t length, bool no_write,
+                                          IOMMUTLBEntry *result,
+                                          size_t result_length,
+                                          uint32_t *err_count)
+{
+    assert(result_length);
+    IOMMUMemoryRegion *iommu_mr = pci_device_iommu_memory_region_pasid(dev,
+                                                                        pasid);
+    if (!iommu_mr || !pcie_ats_enabled(dev)) {
+        return -EPERM;
+    }
+    return memory_region_iommu_ats_request_translation(iommu_mr, priv_req,
+                                                       exec_req, addr, length,
+                                                       no_write, result,
+                                                       result_length,
+                                                       err_count);
+}
+
+int pci_register_iommu_tlb_event_notifier(PCIDevice *dev, uint32_t pasid,
+                                          IOMMUNotifier *n)
+{
+    IOMMUMemoryRegion *iommu_mr = pci_device_iommu_memory_region_pasid(dev,
+                                                                        pasid);
+    if (!iommu_mr) {
+        return -EPERM;
+    }
+    return memory_region_register_iommu_notifier(MEMORY_REGION(iommu_mr), n,
+                                                 &error_fatal);
+}
+
+int pci_unregister_iommu_tlb_event_notifier(PCIDevice *dev, uint32_t pasid,
+                                             IOMMUNotifier *n)
+{
+    IOMMUMemoryRegion *iommu_mr = pci_device_iommu_memory_region_pasid(dev,
+                                                                        pasid);
+    if (!iommu_mr) {
+        return -EPERM;
+    }
+    memory_region_unregister_iommu_notifier(MEMORY_REGION(iommu_mr), n);
+    return 0;
+}
+
 void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque)
 {
     /*
include/hw/pci/pci.h +52 -0

58 lines not on this page · raw message

Signature · 1 line
2.44.0
#33 lore raw

PATCH 23/24 intel_iommu: set the address mask even when a translation fails

In reply to CLEMENT MATHIEU--DRIF

Implements the behavior defined in section 10.2.3.5 of PCIe spec rev 5. This is needed by devices that support ATS.

---
 hw/i386/intel_iommu.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +8 -2
index 02c5f0fa4f..aac7677063 100644
@@ -2167,7 +2167,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t bus_num = pci_bus_num(bus);
     VTDContextCacheEntry *cc_entry;
     uint64_t pte, page_mask;
-    uint32_t level, pasid = vtd_as->pasid;
+    uint32_t level = UINT32_MAX;
+    uint32_t pasid = vtd_as->pasid;
     uint16_t source_id = PCI_BUILD_BDF(bus_num, devfn);
     int ret_fr;
     bool is_fpd_set = false;
@@ -2309,7 +2310,12 @@ error:
     vtd_iommu_unlock(s);
     entry->iova = 0;
     entry->translated_addr = 0;
-    entry->addr_mask = 0;
+    /*
+     * Set the mask for ATS (the range must be present even when the
+     * translation fails : PCIe rev 5 10.2.3.5)
+     */
+    entry->addr_mask = (level != UINT32_MAX) ?
+                       (~vtd_slpt_level_page_mask(level)) : (~VTD_PAGE_MASK_4K);
     entry->perm = IOMMU_NONE;
     entry->pasid = PCI_NO_PASID;
     return false;
Signature · 1 line
2.44.0
#34 lore raw

PATCH 24/24 intel_iommu: add support for ATS

In reply to CLEMENT MATHIEU--DRIF
---
 hw/i386/intel_iommu.c          | 75 ++++++++++++++++++++++++++++++++--
 hw/i386/intel_iommu_internal.h |  1 +
 2 files changed, 73 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +72 -3
index aac7677063..400b27fc95 100644
@@ -5395,12 +5395,10 @@ static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
     bool is_fpd_set = false;
     VTDContextEntry ce;
 
-    assert(vtd_as->pasid != PCI_NO_PASID);
-
     /* Try out best to fetch FPD, we can't do anything more */
     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
-        if (!is_fpd_set && s->root_scalable) {
+        if (!is_fpd_set && s->root_scalable && vtd_as->pasid != PCI_NO_PASID) {
             vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
         }
     }
@@ -6025,6 +6023,75 @@ static IOMMUMemoryRegion *vtd_get_memory_region_pasid(PCIBus *bus,
     return &vtd_as->iommu;
 }
 
+static IOMMUTLBEntry vtd_iommu_ats_do_translate(IOMMUMemoryRegion *iommu,
+                                                hwaddr addr,
+                                                IOMMUAccessFlags flags,
+                                                int iommu_idx)
+{
+    IOMMUTLBEntry entry;
+    VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
+
+    if (vtd_is_interrupt_addr(addr)) {
+        vtd_report_ir_illegal_access(vtd_as, addr, flags & IOMMU_WO);
+        entry.iova = 0;
+        entry.translated_addr = 0;
+        entry.addr_mask = ~VTD_PAGE_MASK_4K;
+        entry.perm = IOMMU_NONE;
+        entry.pasid = PCI_NO_PASID;
+    } else {
+        entry = vtd_iommu_translate(iommu, addr, flags, iommu_idx);
+    }
+    return entry;
+}
+
+static ssize_t vtd_iommu_ats_request_translation(IOMMUMemoryRegion *iommu,
+                                                 bool priv_req, bool exec_req,
+                                                 hwaddr addr, size_t length,
+                                                 bool no_write,
+                                                 IOMMUTLBEntry *result,
+                                                 size_t result_length,
+                                                 uint32_t *err_count)
+{
+    IOMMUAccessFlags flags = IOMMU_ACCESS_FLAG_FULL(true, !no_write, exec_req,
+                                                    priv_req, false, false);
+    ssize_t res_index = 0;
+    hwaddr target_address = addr + length;
+    IOMMUTLBEntry entry;
+
+    *err_count = 0;
+
+    while ((addr < target_address) && (res_index < result_length)) {
+        entry = vtd_iommu_ats_do_translate(iommu, addr, flags, 0);
+        if (!IOMMU_TLB_ENTRY_TRANSLATION_ERROR(&entry)) { /* Translation done */
+            if (no_write) {
+                /* The device should not use this entry for a write access */
+                entry.perm &= ~IOMMU_WO;
+            }
+            /*
+             * 4.1.2 : Global Mapping (G) : Remapping hardware provides a value
+             * of 0 in this field
+             */
+            entry.perm &= ~IOMMU_GLOBAL;
+        } else {
+            *err_count += 1;
+        }
+        result[res_index] = entry;
+        res_index += 1;
+        addr = (addr & (~entry.addr_mask)) + (entry.addr_mask + 1);
+    }
+
+    /* Buffer too small */
+    if (addr < target_address) {
+        return -ENOMEM;
+    }
+    return res_index;
+}
+
+static uint64_t vtd_get_min_page_size(IOMMUMemoryRegion *iommu)
+{
+    return VTD_PAGE_SIZE;
+}
+
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
     .get_address_space_pasid = vtd_host_dma_iommu_pasid,
@@ -6231,6 +6298,8 @@ static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
     imrc->translate = vtd_iommu_translate;
     imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
     imrc->replay = vtd_iommu_replay;
+    imrc->iommu_ats_request_translation = vtd_iommu_ats_request_translation;
+    imrc->get_min_page_size = vtd_get_min_page_size;
 }
 
 static const TypeInfo vtd_iommu_memory_region_info = {
hw/i386/intel_iommu_internal.h +1 -0
index 3d59e10488..aa4d0d5f16 100644
@@ -193,6 +193,7 @@
 #define VTD_ECAP_MHMV               (15ULL << 20)
 #define VTD_ECAP_NEST               (1ULL << 26)
 #define VTD_ECAP_SRS                (1ULL << 31)
+#define VTD_ECAP_NWFS               (1ULL << 33)
 #define VTD_ECAP_PSS                (19ULL << 35)
 #define VTD_ECAP_PASID              (1ULL << 40)
 #define VTD_ECAP_SMTS               (1ULL << 43)
Signature · 1 line
2.44.0