Skip to contentSkip to conversation

Add support for Renesas R-Car S4 IPMMU and other misc changes

PATCH Superseded Oleksandr Tyshchenko 3 participants 19 messages Dec 20, 2021 – Jan 27, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for Renesas R-Car S4 IPMMU and other misc changes

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

You can find the V1 patch series at [1].

The R-Car S4 is an automotive System-on-Chip (SoC) for Car Server/Communication Gateway and is one of the first products in Renesas’ 4th-generation R-Car Family.

The integrated IOMMU HW is also VMSA-compatible and supports stage 2 translation table format, therefore can be used with current R-Car Gen3 driver with slight modifications.

In the context of Xen driver the main differences between Gen3 and Gen4 are the following:
- HW capacity was enlarged to support up to 16 IPMMU contexts (sets of page table) and up to 64 micro-TLBs per IPMMU device
- the memory mapped registers have different bases and offset

The first part (commits #1-6) is a non-verbatim port of Linux upstream commits needed to add support for S4 series easily (prereq work).
The second part (commits #7-8) is based on the code from the Renesas BSP and actually introduces support for R-Car S4 IPMMU.
The third part (commits #9-10) is misc changes I have locally.

The patch series is based on the latest staging branch "f3999bc2e099 arm/efi: Handle Xen bootargs from both xen.cfg and DT" and also available at [2].

Tested on Renesas Salvator-X board with H3 ES3.0 SoC (Gen3) and Renesas Spider board with S4 SoC (Gen4).

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/otyshchenko1/xen/commits/s4_ipmmu_ml2

Oleksandr Tyshchenko (10):
iommu/ipmmu-vmsa: Remove all unused register definitions iommu/ipmmu-vmsa: Add helper functions for MMU "context" registers iommu/ipmmu-vmsa: Add helper functions for "uTLB" registers iommu/ipmmu-vmsa: Add light version of Linux's ipmmu_features iommu/ipmmu-vmsa: Calculate context registers' offset instead of a
macro
iommu/ipmmu-vmsa: Add utlb_offset_base iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0 iommu/ipmmu-vmsa: Use refcount for the micro-TLBs iommu/arm: Remove code duplication in all IOMMU drivers

 xen/drivers/passthrough/Kconfig          |   6 +-
 xen/drivers/passthrough/arm/iommu.c      |   7 +
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 267 ++++++++++++++++++-------------
 xen/drivers/passthrough/arm/smmu-v3.c    |  10 --
 xen/drivers/passthrough/arm/smmu.c       |  10 --
 5 files changed, 167 insertions(+), 133 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/10 iommu/ipmmu-vmsa: Remove all unused register definitions

From: Oleksandr Tyshchenko <[email protected]>

This is a non-verbatim port of corresponding Linux upsteam commit:
77cf983892b2e0d40dc256b784930a9ffaad4fc8

Original commit message:
commit 77cf983892b2e0d40dc256b784930a9ffaad4fc8
Author: Yoshihiro Shimoda <[email protected]>
Date: Wed Nov 6 11:35:45 2019 +0900

iommu/ipmmu-vmsa: Remove all unused register definitions

To support different registers memory mapping hardware easily in the future, this patch removes all unused register definitions.

**********

This is a prereq work needed to add support for S4 series easily in the future.

Although Linux and Xen drivers have a lot in common, the main differences are in translation stages (table formats), VMSAv8 modes, supported SoC generations, etc, therefore that's why there is a slight difference in registers/bits each driver considers unused.

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 59 ++------------------------------
 1 file changed, 2 insertions(+), 57 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +2 -57
index 1255b0d..4a8a974 100644
@@ -166,15 +166,11 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 
 #define IMCTR                0x0000
 /*
- * These fields are implemented in IPMMU-MM only. So, can be set for
+ * This field is implemented in IPMMU-MM only. So, can be set for
  * Root IPMMU only.
  */
 #define IMCTR_VA64           (1 << 29)
-#define IMCTR_TRE            (1 << 17)
-#define IMCTR_AFE            (1 << 16)
-#define IMCTR_RTSEL_MASK     (3 << 4)
-#define IMCTR_RTSEL_SHIFT    4
-#define IMCTR_TREN           (1 << 3)
+
 /*
  * These fields are common for all IPMMU devices. So, can be set for
  * Cache IPMMUs as well.
@@ -184,42 +180,9 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMCTR_MMUEN          (1 << 0)
 #define IMCTR_COMMON_MASK    (7 << 0)
 
-#define IMCAAR               0x0004
-
 #define IMTTBCR                        0x0008
 #define IMTTBCR_EAE                    (1U << 31)
 #define IMTTBCR_PMB                    (1 << 30)
-#define IMTTBCR_SH1_NON_SHAREABLE      (0 << 28)
-#define IMTTBCR_SH1_OUTER_SHAREABLE    (2 << 28)
-#define IMTTBCR_SH1_INNER_SHAREABLE    (3 << 28)
-#define IMTTBCR_SH1_MASK               (3 << 28)
-#define IMTTBCR_ORGN1_NC               (0 << 26)
-#define IMTTBCR_ORGN1_WB_WA            (1 << 26)
-#define IMTTBCR_ORGN1_WT               (2 << 26)
-#define IMTTBCR_ORGN1_WB               (3 << 26)
-#define IMTTBCR_ORGN1_MASK             (3 << 26)
-#define IMTTBCR_IRGN1_NC               (0 << 24)
-#define IMTTBCR_IRGN1_WB_WA            (1 << 24)
-#define IMTTBCR_IRGN1_WT               (2 << 24)
-#define IMTTBCR_IRGN1_WB               (3 << 24)
-#define IMTTBCR_IRGN1_MASK             (3 << 24)
-#define IMTTBCR_TSZ1_MASK              (0x1f << 16)
-#define IMTTBCR_TSZ1_SHIFT             16
-#define IMTTBCR_SH0_NON_SHAREABLE      (0 << 12)
-#define IMTTBCR_SH0_OUTER_SHAREABLE    (2 << 12)
-#define IMTTBCR_SH0_INNER_SHAREABLE    (3 << 12)
-#define IMTTBCR_SH0_MASK               (3 << 12)
-#define IMTTBCR_ORGN0_NC               (0 << 10)
-#define IMTTBCR_ORGN0_WB_WA            (1 << 10)
-#define IMTTBCR_ORGN0_WT               (2 << 10)
-#define IMTTBCR_ORGN0_WB               (3 << 10)
-#define IMTTBCR_ORGN0_MASK             (3 << 10)
-#define IMTTBCR_IRGN0_NC               (0 << 8)
-#define IMTTBCR_IRGN0_WB_WA            (1 << 8)
-#define IMTTBCR_IRGN0_WT               (2 << 8)
-#define IMTTBCR_IRGN0_WB               (3 << 8)
-#define IMTTBCR_IRGN0_MASK             (3 << 8)
-#define IMTTBCR_SL0_LVL_2              (0 << 6)
 #define IMTTBCR_SL0_LVL_1              (1 << 6)
 #define IMTTBCR_TSZ0_MASK              (0x1f << 0)
 #define IMTTBCR_TSZ0_SHIFT             0
@@ -228,18 +191,8 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMTTLBR0_TTBR_MASK    (0xfffff << 12)
 #define IMTTUBR0              0x0014
 #define IMTTUBR0_TTBR_MASK    (0xff << 0)
-#define IMTTLBR1              0x0018
-#define IMTTLBR1_TTBR_MASK    (0xfffff << 12)
-#define IMTTUBR1              0x001c
-#define IMTTUBR1_TTBR_MASK    (0xff << 0)
 
 #define IMSTR                          0x0020
-#define IMSTR_ERRLVL_MASK              (3 << 12)
-#define IMSTR_ERRLVL_SHIFT             12
-#define IMSTR_ERRCODE_TLB_FORMAT       (1 << 8)
-#define IMSTR_ERRCODE_ACCESS_PERM      (4 << 8)
-#define IMSTR_ERRCODE_SECURE_ACCESS    (5 << 8)
-#define IMSTR_ERRCODE_MASK             (7 << 8)
 #define IMSTR_MHIT                     (1 << 4)
 #define IMSTR_ABORT                    (1 << 2)
 #define IMSTR_PF                       (1 << 1)
@@ -251,11 +204,7 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMUCTR(n)              ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
 #define IMUCTR0(n)             (0x0300 + ((n) * 16))
 #define IMUCTR32(n)            (0x0600 + (((n) - 32) * 16))
-#define IMUCTR_FIXADDEN        (1U << 31)
-#define IMUCTR_FIXADD_MASK     (0xff << 16)
-#define IMUCTR_FIXADD_SHIFT    16
 #define IMUCTR_TTSEL_MMU(n)    ((n) << 4)
-#define IMUCTR_TTSEL_PMB       (8 << 4)
 #define IMUCTR_TTSEL_MASK      (15 << 4)
 #define IMUCTR_TTSEL_SHIFT     4
 #define IMUCTR_FLUSH           (1 << 1)
@@ -264,10 +213,6 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMUASID(n)             ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
 #define IMUASID0(n)            (0x0308 + ((n) * 16))
 #define IMUASID32(n)           (0x0608 + (((n) - 32) * 16))
-#define IMUASID_ASID8_MASK     (0xff << 8)
-#define IMUASID_ASID8_SHIFT    8
-#define IMUASID_ASID0_MASK     (0xff << 0)
-#define IMUASID_ASID0_SHIFT    0
 
 #define IMSAUXCTLR          0x0504
 #define IMSAUXCTLR_S2PTE    (1 << 3)
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/10 iommu/ipmmu-vmsa: Add helper functions for MMU "context" registers

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This is a non-verbatim port of corresponding Linux upsteam commit:
16d9454f5e0447f9c19cbf350b35ed377b9f64eb

Original commit message:
commit 16d9454f5e0447f9c19cbf350b35ed377b9f64eb
Author: Yoshihiro Shimoda <[email protected]>
Date: Wed Nov 6 11:35:47 2019 +0900

iommu/ipmmu-vmsa: Add helper functions for MMU "context" registers

Since we will have changed memory mapping of the IPMMU in the future, This patch adds helper functions ipmmu_ctx_{reg,read,write}() for MMU "context" registers. No behavior change.

**********

This is a prereq work needed to add support for S4 series easily in the future.

Besides changes done in the original commit, we also need to update an extra call sites which Linux driver doesn't have, but Xen driver has such as ipmmu_ctx_write_cache(), etc.

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +24 -7
index 4a8a974..ce5c3bc 100644
@@ -307,18 +307,35 @@ static void ipmmu_write(struct ipmmu_vmsa_device *mmu, uint32_t offset,
     writel(data, mmu->base + offset);
 }
 
+static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
+                                  unsigned int context_id, uint32_t reg)
+{
+    return context_id * IM_CTX_SIZE + reg;
+}
+
+static uint32_t ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
+                               unsigned int context_id, uint32_t reg)
+{
+    return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg));
+}
+
+static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu,
+                            unsigned int context_id, uint32_t reg,
+                            uint32_t data)
+{
+    ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data);
+}
+
 static uint32_t ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
                                     uint32_t reg)
 {
-    return ipmmu_read(domain->mmu->root,
-                      domain->context_id * IM_CTX_SIZE + reg);
+    return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg);
 }
 
 static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
                                  uint32_t reg, uint32_t data)
 {
-    ipmmu_write(domain->mmu->root,
-                domain->context_id * IM_CTX_SIZE + reg, data);
+    ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
 }
 
 static void ipmmu_ctx_write_cache(struct ipmmu_vmsa_domain *domain,
@@ -329,8 +346,8 @@ static void ipmmu_ctx_write_cache(struct ipmmu_vmsa_domain *domain,
 
     /* Mask fields which are implemented in IPMMU-MM only. */
     if ( !ipmmu_is_root(domain->mmu) )
-        ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg,
-                    data & IMCTR_COMMON_MASK);
+        ipmmu_ctx_write(domain->mmu, domain->context_id, reg,
+                        data & IMCTR_COMMON_MASK);
 }
 
 /*
@@ -693,7 +710,7 @@ static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
 
     /* Disable all contexts. */
     for ( i = 0; i < mmu->num_ctx; ++i )
-        ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
+        ipmmu_ctx_write(mmu, i, IMCTR, 0);
 }
 
 /* R-Car Gen3 SoCs product and cut information. */
Signature · 1 line
2.7.4
#3 lore raw

PATCH v2 3/10 iommu/ipmmu-vmsa: Add helper functions for "uTLB" registers

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This is a non-verbatim port of corresponding Linux upsteam commit:
3667c9978b2911dc1ded77f5971df477885409c4

Original commit message:
commit 3667c9978b2911dc1ded77f5971df477885409c4
Author: Yoshihiro Shimoda <[email protected]>
Date: Wed Nov 6 11:35:49 2019 +0900

iommu/ipmmu-vmsa: Add helper functions for "uTLB" registers

Since we will have changed memory mapping of the IPMMU in the future, This patch adds helper functions ipmmu_utlb_reg() and ipmmu_imu{asid,ctr}_write() for "uTLB" registers. No behavior change.

**********

This is a prereq work needed to add support for S4 series easily in the future.

Besides changes done in the original commit, we also need to introduce ipmmu_imuctr_read() since Xen driver contains an additional logic in ipmmu_utlb_enable() to prevent the use cases where devices which use the same micro-TLB are assigned to different Xen domains.

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 33 +++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +28 -5
index ce5c3bc..1b94af2 100644
@@ -366,6 +366,29 @@ static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
     ipmmu_ctx_write_root(domain, reg, data);
 }
 
+static uint32_t ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, uint32_t reg)
+{
+    return reg;
+}
+
+static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
+                                unsigned int utlb, uint32_t data)
+{
+    ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data);
+}
+
+static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu,
+                               unsigned int utlb, uint32_t data)
+{
+    ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data);
+}
+
+static uint32_t ipmmu_imuctr_read(struct ipmmu_vmsa_device *mmu,
+                                  unsigned int utlb)
+{
+    return ipmmu_read(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)));
+}
+
 /* TLB and micro-TLB Management */
 
 /* Wait for any pending TLB invalidations to complete. */
@@ -413,7 +436,7 @@ static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
      * context_id for already enabled micro-TLB and prevent different context
      * bank from being set.
      */
-    imuctr = ipmmu_read(mmu, IMUCTR(utlb));
+    imuctr = ipmmu_imuctr_read(mmu, utlb);
     if ( imuctr & IMUCTR_MMUEN )
     {
         unsigned int context_id;
@@ -431,9 +454,9 @@ static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
      * TODO: Reference-count the micro-TLB as several bus masters can be
      * connected to the same micro-TLB.
      */
-    ipmmu_write(mmu, IMUASID(utlb), 0);
-    ipmmu_write(mmu, IMUCTR(utlb), imuctr |
-                IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    ipmmu_imuasid_write(mmu, utlb, 0);
+    ipmmu_imuctr_write(mmu, utlb, imuctr |
+                       IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
 
     return 0;
 }
@@ -444,7 +467,7 @@ static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
 {
     struct ipmmu_vmsa_device *mmu = domain->mmu;
 
-    ipmmu_write(mmu, IMUCTR(utlb), 0);
+    ipmmu_imuctr_write(mmu, utlb, 0);
 }
 
 /* Domain/Context Management */
Signature · 1 line
2.7.4
#4 lore raw

PATCH v2 4/10 iommu/ipmmu-vmsa: Add light version of Linux's ipmmu_features

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This is a prereq work needed to add support for S4 series easily in the future.

We don't need to pull the whole struct and all instances as Xen driver doesn't support old Arm32 based Gen2 SoCs, so there is no point in keeping all differences between Gen2 and Gen3 here. All what we need is a minimal support to be able to operate with Gen3 and new S4.

Add Gen3 specific info with only two fields (number_of_contexts and num_utlbs) for now, the subsequent patches will add remaining bits.

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 54 +++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 14 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +40 -14
index 1b94af2..369be4c 100644
@@ -71,9 +71,9 @@
  * R-Car Gen3 SoCs make use of up to 8 IPMMU contexts (sets of page table) and
  * these can be managed independently. Each context is mapped to one Xen domain.
  */
-#define IPMMU_CTX_MAX     8
+#define IPMMU_CTX_MAX     8U
 /* R-Car Gen3 SoCs make use of up to 48 micro-TLBs per IPMMU device. */
-#define IPMMU_UTLB_MAX    48
+#define IPMMU_UTLB_MAX    48U
 
 /* IPMMU context supports IPA size up to 40 bit. */
 #define IPMMU_MAX_P2M_IPA_BITS    40
@@ -106,17 +106,22 @@ struct ipmmu_vmsa_xen_device {
     struct ipmmu_vmsa_device *mmu;
 };
 
+struct ipmmu_features {
+    unsigned int number_of_contexts;
+    unsigned int num_utlbs;
+};
+
 /* Root/Cache IPMMU device's information */
 struct ipmmu_vmsa_device {
     struct device *dev;
     void __iomem *base;
     struct ipmmu_vmsa_device *root;
     struct list_head list;
-    unsigned int num_utlbs;
     unsigned int num_ctx;
     spinlock_t lock;    /* Protects ctx and domains[] */
     DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
     struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
+    const struct ipmmu_features *features;
 };
 
 /*
@@ -727,6 +732,11 @@ static int ipmmu_init_platform_device(struct device *dev,
     return 0;
 }
 
+static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
+    .number_of_contexts = 8,
+    .num_utlbs = 48,
+};
+
 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
 {
     unsigned int i;
@@ -798,6 +808,27 @@ static __init bool ipmmu_stage2_supported(void)
     return stage2_supported;
 }
 
+static const struct dt_device_match ipmmu_dt_match[] __initconst =
+{
+    {
+        .compatible = "renesas,ipmmu-r8a7795",
+        .data = &ipmmu_features_rcar_gen3,
+    },
+    {
+        .compatible = "renesas,ipmmu-r8a77965",
+        .data = &ipmmu_features_rcar_gen3,
+    },
+    {
+        .compatible = "renesas,ipmmu-r8a7796",
+        .data = &ipmmu_features_rcar_gen3,
+    },
+    {
+        .compatible = "renesas,ipmmu-r8a77961",
+        .data = &ipmmu_features_rcar_gen3,
+    },
+    { /* sentinel */ },
+};
+
 /*
  * This function relies on the fact that Root IPMMU device is being probed
  * the first. If not the case, it denies further Cache IPMMU device probes
@@ -806,6 +837,7 @@ static __init bool ipmmu_stage2_supported(void)
  */
 static int ipmmu_probe(struct dt_device_node *node)
 {
+    const struct dt_device_match *match;
     struct ipmmu_vmsa_device *mmu;
     uint64_t addr, size;
     int irq, ret;
@@ -817,9 +849,12 @@ static int ipmmu_probe(struct dt_device_node *node)
         return -ENOMEM;
     }
 
+    match = dt_match_node(ipmmu_dt_match, node);
+    ASSERT(match);
+    mmu->features = match->data;
+
     mmu->dev = &node->dev;
-    mmu->num_utlbs = IPMMU_UTLB_MAX;
-    mmu->num_ctx = IPMMU_CTX_MAX;
+    mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
     spin_lock_init(&mmu->lock);
     bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
 
@@ -1296,15 +1331,6 @@ static const struct iommu_ops ipmmu_iommu_ops =
     .add_device      = ipmmu_add_device,
 };
 
-static const struct dt_device_match ipmmu_dt_match[] __initconst =
-{
-    DT_MATCH_COMPATIBLE("renesas,ipmmu-r8a7795"),
-    DT_MATCH_COMPATIBLE("renesas,ipmmu-r8a77965"),
-    DT_MATCH_COMPATIBLE("renesas,ipmmu-r8a7796"),
-    DT_MATCH_COMPATIBLE("renesas,ipmmu-r8a77961"),
-    { /* sentinel */ },
-};
-
 static __init int ipmmu_init(struct dt_device_node *node, const void *data)
 {
     int ret;
Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 5/10 iommu/ipmmu-vmsa: Calculate context registers' offset instead of a macro

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This is a non-verbatim port of corresponding Linux upsteam commit:
3dc28d9f59eaae41461542b27afe70339347ebb3

Original commit message:
commit 3dc28d9f59eaae41461542b27afe70339347ebb3
Author: Yoshihiro Shimoda <[email protected]>
Date: Wed Nov 6 11:35:48 2019 +0900

iommu/ipmmu-vmsa: Calculate context registers' offset instead of a macro

Since we will have changed memory mapping of the IPMMU in the future, this patch uses ipmmu_features values instead of a macro to calculate context registers offset. No behavior change.

**********

This is a prereq work needed to add support for S4 series easily in the future.

Almost the same change as original commit makes, but without updating struct ipmmu_features_default which Xen driver doesn't have (there is no support of old Arm32 based Gen2 SoCs).

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +6 -3
index 369be4c..ca33456 100644
@@ -109,6 +109,8 @@ struct ipmmu_vmsa_xen_device {
 struct ipmmu_features {
     unsigned int number_of_contexts;
     unsigned int num_utlbs;
+    unsigned int ctx_offset_base;
+    unsigned int ctx_offset_stride;
 };
 
 /* Root/Cache IPMMU device's information */
@@ -167,8 +169,6 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define TLB_LOOP_TIMEOUT    100 /* 100us */
 
 /* Registers Definition */
-#define IM_CTX_SIZE    0x40
-
 #define IMCTR                0x0000
 /*
  * This field is implemented in IPMMU-MM only. So, can be set for
@@ -315,7 +315,8 @@ static void ipmmu_write(struct ipmmu_vmsa_device *mmu, uint32_t offset,
 static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
                                   unsigned int context_id, uint32_t reg)
 {
-    return context_id * IM_CTX_SIZE + reg;
+    return mmu->features->ctx_offset_base +
+        context_id * mmu->features->ctx_offset_stride + reg;
 }
 
 static uint32_t ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
@@ -735,6 +736,8 @@ static int ipmmu_init_platform_device(struct device *dev,
 static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
     .number_of_contexts = 8,
     .num_utlbs = 48,
+    .ctx_offset_base = 0,
+    .ctx_offset_stride = 0x40,
 };
 
 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 6/10 iommu/ipmmu-vmsa: Add utlb_offset_base

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This is a non-verbatim port of corresponding Linux upsteam commit:
1289f7f15001c7ed36be6d23cb145c1d5feacdc8

Original commit message:
commit 1289f7f15001c7ed36be6d23cb145c1d5feacdc8
Author: Yoshihiro Shimoda <[email protected]>
Date: Wed Nov 6 11:35:50 2019 +0900

iommu/ipmmu-vmsa: Add utlb_offset_base

Since we will have changed memory mapping of the IPMMU in the future, this patch adds a utlb_offset_base into struct ipmmu_features for IMUCTR and IMUASID registers. No behavior change.

**********

This is a prereq work needed to add support for S4 series easily in the future.

Almost the same change as original commit makes, but without updating struct ipmmu_features_default which Xen driver doesn't have (there is no support of old Arm32 based Gen2 SoCs).

No change in behavior.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +3 -1
index ca33456..d8f96fc 100644
@@ -111,6 +111,7 @@ struct ipmmu_features {
     unsigned int num_utlbs;
     unsigned int ctx_offset_base;
     unsigned int ctx_offset_stride;
+    unsigned int utlb_offset_base;
 };
 
 /* Root/Cache IPMMU device's information */
@@ -374,7 +375,7 @@ static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
 
 static uint32_t ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, uint32_t reg)
 {
-    return reg;
+    return mmu->features->utlb_offset_base + reg;
 }
 
 static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
@@ -738,6 +739,7 @@ static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
     .num_utlbs = 48,
     .ctx_offset_base = 0,
     .ctx_offset_stride = 0x40,
+    .utlb_offset_base = 0,
 };
 
 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
Signature · 1 line
2.7.4
#7 lore raw

PATCH v2 7/10 iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Based on the following Linux upsteam commit:
7a62ced8ebd0e1b692c9dc4781a8d4ddb0f74792

Original commit message:
commit 7a62ced8ebd0e1b692c9dc4781a8d4ddb0f74792
Author: Yoshihiro Shimoda <[email protected]>
Date: Tue Sep 7 17:30:20 2021 +0900

iommu/ipmmu-vmsa: Add support for r8a779a0

Add support for r8a779a0 (R-Car V3U). The IPMMU hardware design of this SoC differs than others. So, add a new ipmmu_features for it.

**********

The R-Car S4 is an automotive System-on-Chip (SoC) for Car Server/Communication Gateway and is one of the first products in Renesas’ 4th-generation R-Car Family.

The integrated IOMMU HW is also VMSA-compatible and supports stage 2 translation table format, therefore can be used with current driver with slight modifications (thanks to the prereq work).

In the context of Xen driver the main differences between Gen3 and Gen4 are the following:
- HW capacity was enlarged to support up to 16 IPMMU contexts (sets of page table) and up to 64 micro-TLBs per IPMMU device
- the memory mapped registers have different bases and offsets

Please note that Linux upstream doesn't support R-Car S4 SoC yet unlike Renesas BSP [1], but it was decided to reuse upstream patch for R-Car V3U anyway as the IPMMU HW settings are similar.

[1]
7003b9f732cf iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support
https://github.com/renesas-rcar/linux-bsp/tree/v5.10.41/rcar-5.1.3.rc5

---
Changes V1 -> V2:
- base on the Linux upsteam patch instead of BSP's one
- update patch description and comments in code
- replace ipmmu_features_rcar_s4 with ipmmu_features_rcar_gen4
- introduce control_offset_base field

---
 xen/drivers/passthrough/Kconfig          |  6 +--
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 63 +++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 20 deletions(-)
xen/drivers/passthrough/Kconfig +3 -3
index 09505aa..479d7de 100644
@@ -25,14 +25,14 @@ config ARM_SMMU_V3
 	 the ARM SMMUv3 architecture.
 
 config IPMMU_VMSA
-	bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs"
+	bool "Renesas IPMMU-VMSA found in R-Car Gen3/Gen4 SoCs"
 	depends on ARM_64
 	---help---
 	  Support for implementations of the Renesas IPMMU-VMSA found
-	  in R-Car Gen3 SoCs.
+	  in R-Car Gen3/Gen4 SoCs.
 
 	  Say Y here if you are using newest R-Car Gen3 SoCs revisions
-	  (H3 ES3.0, M3-W+, etc) which IPMMU hardware supports stage 2
+	  (H3 ES3.0, M3-W+, etc) or Gen4 SoCs which IPMMU hardware supports stage 2
 	  translation table format and is able to use CPU's P2M table as is.
 
 endif
xen/drivers/passthrough/arm/ipmmu-vmsa.c +46 -17

153 lines not on this page · raw message

Signature · 1 line
2.7.4
Reviewed-by on v2 7/10
#8 lore raw

RE: [PATCH V2 07/10] iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support

Hello Oleksandr-san,

Quoted 50 lines

From: Oleksandr Tyshchenko, Sent: Tuesday, December 21, 2021 6:16 AM

From: Oleksandr Tyshchenko <[email protected]>

Based on the following Linux upsteam commit:
7a62ced8ebd0e1b692c9dc4781a8d4ddb0f74792

Original commit message:
commit 7a62ced8ebd0e1b692c9dc4781a8d4ddb0f74792
Author: Yoshihiro Shimoda <[email protected]>
Date: Tue Sep 7 17:30:20 2021 +0900

iommu/ipmmu-vmsa: Add support for r8a779a0

Add support for r8a779a0 (R-Car V3U). The IPMMU hardware design of this SoC differs than others. So, add a new ipmmu_features for it.

Link:
https://jpn01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fr%2F20210907083020.907648-3-yosh
ihiro.shimoda.uh%40renesas.com&amp;data=04%7C01%7Cyoshihiro.shimoda.uh%40renesas.com%7C995a1c66b6d841ccc79a08d9c3fdf 90a%7C53d82571da1947e49cb4625a166a4a2a%7C0%7C0%7C637756317833053012%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQ IjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=TirhJToNqy33Fzt0tSpukl9YFOj4iHgd3M1lX8W%2Bxlo%3D&amp;res erved=0

**********

The R-Car S4 is an automotive System-on-Chip (SoC) for Car Server/Communication Gateway and is one of the first products in Renesas' 4th-generation R-Car Family.

The integrated IOMMU HW is also VMSA-compatible and supports stage 2 translation table format, therefore can be used with current driver with slight modifications (thanks to the prereq work).

In the context of Xen driver the main differences between Gen3 and Gen4 are the following:
- HW capacity was enlarged to support up to 16 IPMMU contexts (sets of page table) and up to 64 micro-TLBs per IPMMU device
- the memory mapped registers have different bases and offsets

Please note that Linux upstream doesn't support R-Car S4 SoC yet unlike Renesas BSP [1], but it was decided to reuse upstream patch for R-Car V3U anyway as the IPMMU HW settings are similar.

[1]
7003b9f732cf iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support

<snip>

Quoted 3 lines

Thank you for the patch!

Best regards,
Yoshihiro Shimoda

#9 lore raw

PATCH v2 8/10 iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Based on the following commits from the Renesas BSP:
8fba83d97cca709a05139c38e29408e81ed4cf62 a8d93bc07da89a7fcf4d85f34d119a030310efa5 located at:
https://github.com/renesas-rcar/linux-bsp/tree/v5.10.41/rcar-5.1.3.rc5

Original commit messages:
commit 8fba83d97cca709a05139c38e29408e81ed4cf62
Author: Nam Nguyen <[email protected]>
Date: Wed Apr 28 18:54:44 2021 +0700

iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

Need to set bit IMSCTLR_USE_SECGRP to 0
because H/W initial value is unknown, without this dma-transfer cannot be done due to address translation doesn't work.

commit a8d93bc07da89a7fcf4d85f34d119a030310efa5
Author: Nam Nguyen <[email protected]>
Date: Tue Sep 7 14:46:12 2021 +0700

iommu/ipmmu-vmsa: Update IMSCTLR register offset address for R-Car S4

Update IMSCTLR register offset address to align with R-Car S4 H/W UM.

**********

It is still a question whether this really needs to be done in Xen, rather in firmware, but better to be on the safe side. After all, if firmware already takes care of clearing this bit, nothing bad will happen.

Please note the following:
1. I decided to squash both commits since the first commit adds clearing code and only the second one makes it functional on S4. Moreover, this is not a direct port. So it would be better to introduce complete solution by a single patch.
2. Although patch indeed does what it claims in the subject, the implementation is different in comparison with original changes. On Linux the clearing is done at runtime in ipmmu_domain_setup_context(). On Xen the clearing is done at boot time in ipmmu_probe(). The IMSCTLR is not a MMU "context" register at all, so I think there is no point in performing the clearing each time we initialize the context, instead perform the clearing at once during initialization. Also do not abuse ctx_offset_stride_adj field for the register's offset calculation, instead use recently added control_offset_base field.

---
Changes V1 -> V2:
- add R-b
- update patch description
- replace ctx_offset_stride_adj with control_offset_base for register's offset calculation

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +7 -0
index 14848ce..649b9f6 100644
@@ -222,6 +222,9 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMUASID0(n)            (0x0308 + ((n) * 16))
 #define IMUASID32(n)           (0x0608 + (((n) - 32) * 16))
 
+#define IMSCTLR             0x0500
+#define IMSCTLR_USE_SECGRP  (1 << 28)
+
 #define IMSAUXCTLR          0x0504
 #define IMSAUXCTLR_S2PTE    (1 << 3)
 
@@ -966,6 +969,10 @@ static int ipmmu_probe(struct dt_device_node *node)
         set_bit(0, mmu->ctx);
     }
 
+    /* Do not use security group function. */
+    reg = IMSCTLR + mmu->features->control_offset_base;
+    ipmmu_write(mmu, reg, ipmmu_read(mmu, reg) & ~IMSCTLR_USE_SECGRP);
+
     spin_lock(&ipmmu_devices_lock);
     list_add(&mmu->list, &ipmmu_devices);
     spin_unlock(&ipmmu_devices_lock);
Signature · 1 line
2.7.4
Reviewed-by on v2 8/10
#10 lore raw

RE: [PATCH V2 08/10] iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

Hello Oleksandr-san,

Quoted 8 lines

From: Oleksandr Tyshchenko, Sent: Tuesday, December 21, 2021 6:16 AM

From: Oleksandr Tyshchenko <[email protected]>

Based on the following commits from the Renesas BSP:
8fba83d97cca709a05139c38e29408e81ed4cf62 a8d93bc07da89a7fcf4d85f34d119a030310efa5 located at:

<snip>

Quoted 46 lines

Original commit messages:
commit 8fba83d97cca709a05139c38e29408e81ed4cf62
Author: Nam Nguyen <[email protected]>
Date: Wed Apr 28 18:54:44 2021 +0700

iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

Need to set bit IMSCTLR_USE_SECGRP to 0
because H/W initial value is unknown, without this dma-transfer cannot be done due to address translation doesn't work.

commit a8d93bc07da89a7fcf4d85f34d119a030310efa5
Author: Nam Nguyen <[email protected]>
Date: Tue Sep 7 14:46:12 2021 +0700

iommu/ipmmu-vmsa: Update IMSCTLR register offset address for R-Car S4

Update IMSCTLR register offset address to align with R-Car S4 H/W UM.

**********

It is still a question whether this really needs to be done in Xen, rather in firmware, but better to be on the safe side. After all, if firmware already takes care of clearing this bit, nothing bad will happen.

Please note the following:
1. I decided to squash both commits since the first commit adds clearing code and only the second one makes it functional on S4. Moreover, this is not a direct port. So it would be better to introduce complete solution by a single patch.
2. Although patch indeed does what it claims in the subject, the implementation is different in comparison with original changes. On Linux the clearing is done at runtime in ipmmu_domain_setup_context(). On Xen the clearing is done at boot time in ipmmu_probe(). The IMSCTLR is not a MMU "context" register at all, so I think there is no point in performing the clearing each time we initialize the context, instead perform the clearing at once during initialization. Also do not abuse ctx_offset_stride_adj field for the register's offset calculation, instead use recently added control_offset_base field.

Quoted 2 lines

Thank you for the patch!

Best regards,
Yoshihiro Shimoda

#11 lore raw

PATCH v2 9/10 iommu/ipmmu-vmsa: Use refcount for the micro-TLBs

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Reference-count the micro-TLBs as several bus masters can be connected to the same micro-TLB (and drop TODO comment). This wasn't an issue so far, since the platform devices (this driver deals with) get assigned/deassigned together during domain creation/destruction. But, in order to support PCI devices (which are hot-pluggable) in the near future we will need to take care of.

---
Changes V1 -> V2:
- add R-b
- add ASSERT() in ipmmu_utlb_disable()

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +11 -8
index 649b9f6..1224ea4 100644
@@ -127,6 +127,7 @@ struct ipmmu_vmsa_device {
     spinlock_t lock;    /* Protects ctx and domains[] */
     DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
     struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
+    unsigned int utlb_refcount[IPMMU_UTLB_MAX];
     const struct ipmmu_features *features;
 };
 
@@ -467,13 +468,12 @@ static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
         }
     }
 
-    /*
-     * TODO: Reference-count the micro-TLB as several bus masters can be
-     * connected to the same micro-TLB.
-     */
-    ipmmu_imuasid_write(mmu, utlb, 0);
-    ipmmu_imuctr_write(mmu, utlb, imuctr |
-                       IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    if ( mmu->utlb_refcount[utlb]++ == 0 )
+    {
+        ipmmu_imuasid_write(mmu, utlb, 0);
+        ipmmu_imuctr_write(mmu, utlb, imuctr |
+                           IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    }
 
     return 0;
 }
@@ -484,7 +484,10 @@ static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
 {
     struct ipmmu_vmsa_device *mmu = domain->mmu;
 
-    ipmmu_imuctr_write(mmu, utlb, 0);
+    ASSERT(mmu->utlb_refcount[utlb] > 0);
+
+    if ( --mmu->utlb_refcount[utlb] == 0 )
+        ipmmu_imuctr_write(mmu, utlb, 0);
 }
 
 /* Domain/Context Management */
Signature · 1 line
2.7.4
#12 lore raw

Re: [PATCH V2 09/10] iommu/ipmmu-vmsa: Use refcount for the micro-TLBs

Hi Oleksandr,

Quoted 9 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Reference-count the micro-TLBs as several bus masters can be connected to the same micro-TLB (and drop TODO comment). This wasn't an issue so far, since the platform devices (this driver deals with) get assigned/deassigned together during domain creation/destruction. But, in order to support PCI devices (which are hot-pluggable) in the near future we will need to take care of.

Looking at the code, it is not possible to share the micro-TLB between domains (or even Xen). So technically, we will still want to {, un}hotplug the devices using the same u-TLB together.

Therefore, I would clarify that this is necessary because even if we have to remove all the devices together, the IOMMU driver will be de-assigning them one-by-one.

I would add a similar comment in the code as well.

Quoted 45 lines

---
Changes V1 -> V2:
- add R-b
- add ASSERT() in ipmmu_utlb_disable()

---
  xen/drivers/passthrough/arm/ipmmu-vmsa.c | 19 +++++++++++--------
  1 file changed, 11 insertions(+), 8 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +7 -7
index 649b9f6..1224ea4 100644
@@ -127,6 +127,7 @@ struct ipmmu_vmsa_device {
      spinlock_t lock;    /* Protects ctx and domains[] */
      DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
      struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
+    unsigned int utlb_refcount[IPMMU_UTLB_MAX];
      const struct ipmmu_features *features;
  };
 
@@ -467,13 +468,12 @@ static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
          }
      }
 
-    /*
-     * TODO: Reference-count the micro-TLB as several bus masters can be
-     * connected to the same micro-TLB.
-     */
-    ipmmu_imuasid_write(mmu, utlb, 0);
-    ipmmu_imuctr_write(mmu, utlb, imuctr |
-                       IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    if ( mmu->utlb_refcount[utlb]++ == 0 )
+    {
+        ipmmu_imuasid_write(mmu, utlb, 0);
+        ipmmu_imuctr_write(mmu, utlb, imuctr |
+                           IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    }
 
      return 0;
  }
@@ -484,7 +484,10 @@ static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
  {

xen/drivers/passthrough/arm/ipmmu-vmsa.c static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain, · line 493

Quoted 10 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +4 -1
      struct ipmmu_vmsa_device *mmu = domain->mmu;
 
-    ipmmu_imuctr_write(mmu, utlb, 0);
+    ASSERT(mmu->utlb_refcount[utlb] > 0);
+
+    if ( --mmu->utlb_refcount[utlb] == 0 )
+        ipmmu_imuctr_write(mmu, utlb, 0);
  }
 
  /* Domain/Context Management */

Cheers,

Signature · 1 line
Julien Grall
author
#13 lore raw

Re: [PATCH V2 09/10] iommu/ipmmu-vmsa: Use refcount for the micro-TLBs

Quoted 1 line · Julien Grall

Hi Oleksandr,

Hi Julien

Quoted 12 lines · Oleksandr Tyshchenko
Quoted 9 lines

From: Oleksandr Tyshchenko <[email protected]>

Reference-count the micro-TLBs as several bus masters can be connected to the same micro-TLB (and drop TODO comment). This wasn't an issue so far, since the platform devices (this driver deals with) get assigned/deassigned together during domain creation/destruction. But, in order to support PCI devices (which are hot-pluggable) in the near future we will need to take care of.

Quoted 5 lines

Looking at the code, it is not possible to share the micro-TLB between domains (or even Xen). So technically, we will still want to {, un}hotplug the devices using the same u-TLB together.

Therefore, I would clarify that this is necessary because even if we have to remove all the devices together, the IOMMU driver will be de-assigning them one-by-one.

I would add a similar comment in the code as well.

ok, will add.

Quoted 58 lines
Quoted 45 lines

---
Changes V1 -> V2:
    - add R-b
    - add ASSERT() in ipmmu_utlb_disable()
---
  xen/drivers/passthrough/arm/ipmmu-vmsa.c | 19 +++++++++++--------

  1 file changed, 11 insertions(+), 8 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -0
index 649b9f6..1224ea4 100644
@@ -127,6 +127,7 @@ struct ipmmu_vmsa_device {

      spinlock_t lock;    /* Protects ctx and domains[] */ DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
      struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
+    unsigned int utlb_refcount[IPMMU_UTLB_MAX];
      const struct ipmmu_features *features;
  };
  @@ -467,13 +468,12 @@ static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain, }
      }
  -    /*
-     * TODO: Reference-count the micro-TLB as several bus masters
can be
-     * connected to the same micro-TLB.
-     */
-    ipmmu_imuasid_write(mmu, utlb, 0);
-    ipmmu_imuctr_write(mmu, utlb, imuctr |
-                       IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    if ( mmu->utlb_refcount[utlb]++ == 0 )
+    {
+        ipmmu_imuasid_write(mmu, utlb, 0);
+        ipmmu_imuctr_write(mmu, utlb, imuctr |
+ IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+    }
        return 0;
  }

xen/drivers/passthrough/arm/ipmmu-vmsa.c static void ipmmu_utlb_disable(struct · line 493

Quoted 10 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +4 -0
@@ -484,7 +484,10 @@ static void ipmmu_utlb_disable(struct 
 ipmmu_vmsa_domain *domain,
   {
       struct ipmmu_vmsa_device *mmu = domain->mmu;
   -    ipmmu_imuctr_write(mmu, utlb, 0);
+    ASSERT(mmu->utlb_refcount[utlb] > 0);
+
+    if ( --mmu->utlb_refcount[utlb] == 0 )
+        ipmmu_imuctr_write(mmu, utlb, 0);
   }
     /* Domain/Context Management */

Cheers,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#14 lore raw

PATCH v2 10/10 iommu/arm: Remove code duplication in all IOMMU drivers

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

All IOMMU drivers on Arm perform almost the same generic actions in hwdom_init callback. Move this code to common arch_iommu_hwdom_init() in order to get rid of code duplication.

---
Changes V1 -> V2:
- add R-b

---
 xen/drivers/passthrough/arm/iommu.c      |  7 +++++++
 xen/drivers/passthrough/arm/ipmmu-vmsa.c |  8 --------
 xen/drivers/passthrough/arm/smmu-v3.c    | 10 ----------
 xen/drivers/passthrough/arm/smmu.c       | 10 ----------
 4 files changed, 7 insertions(+), 28 deletions(-)
xen/drivers/passthrough/arm/iommu.c +7 -0
index ee653a9..fc45318 100644
@@ -134,6 +134,13 @@ void arch_iommu_domain_destroy(struct domain *d)
 
 void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
 {
+    /* Set to false options not supported on ARM. */
+    if ( iommu_hwdom_inclusive )
+        printk(XENLOG_WARNING "map-inclusive dom0-iommu option is not supported on ARM\n");
+    iommu_hwdom_inclusive = false;
+    if ( iommu_hwdom_reserved == 1 )
+        printk(XENLOG_WARNING "map-reserved dom0-iommu option is not supported on ARM\n");
+    iommu_hwdom_reserved = 0;
 }
 
 /*
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -8
index 1224ea4..64d8ddc 100644
@@ -1325,14 +1325,6 @@ static int ipmmu_iommu_domain_init(struct domain *d)
 
 static void __hwdom_init ipmmu_iommu_hwdom_init(struct domain *d)
 {
-    /* Set to false options not supported on ARM. */
-    if ( iommu_hwdom_inclusive )
-        printk(XENLOG_WARNING "ipmmu: map-inclusive dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_inclusive = false;
-    if ( iommu_hwdom_reserved == 1 )
-        printk(XENLOG_WARNING "ipmmu: map-reserved dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_reserved = 0;
-
     arch_iommu_hwdom_init(d);
 }
 
xen/drivers/passthrough/arm/smmu-v3.c +0 -10
index d115df7..ca8b5c7 100644
@@ -3404,16 +3404,6 @@ static int arm_smmu_iommu_xen_domain_init(struct domain *d)
 
 static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
 {
-	/* Set to false options not supported on ARM. */
-	if (iommu_hwdom_inclusive)
-		printk(XENLOG_WARNING
-		"map-inclusive dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_inclusive = false;
-	if (iommu_hwdom_reserved == 1)
-		printk(XENLOG_WARNING
-		"map-reserved dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_reserved = 0;
-
 	arch_iommu_hwdom_init(d);
 }
 
xen/drivers/passthrough/arm/smmu.c +0 -10
index c9dfc4c..ec18df7 100644
@@ -2851,16 +2851,6 @@ static int arm_smmu_iommu_domain_init(struct domain *d)
 
 static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
 {
-	/* Set to false options not supported on ARM. */
-	if ( iommu_hwdom_inclusive )
-		printk(XENLOG_WARNING
-		"map-inclusive dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_inclusive = false;
-	if ( iommu_hwdom_reserved == 1 )
-		printk(XENLOG_WARNING
-		"map-reserved dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_reserved = 0;
-
 	arch_iommu_hwdom_init(d);
 }
 
Signature · 1 line
2.7.4
#15 lore raw

Re: [PATCH V2 10/10] iommu/arm: Remove code duplication in all IOMMU drivers

Hi,

Quoted 45 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

All IOMMU drivers on Arm perform almost the same generic actions in hwdom_init callback. Move this code to common arch_iommu_hwdom_init() in order to get rid of code duplication.

---
Changes V1 -> V2:
- add R-b

---
  xen/drivers/passthrough/arm/iommu.c      |  7 +++++++
  xen/drivers/passthrough/arm/ipmmu-vmsa.c |  8 --------
  xen/drivers/passthrough/arm/smmu-v3.c    | 10 ----------
  xen/drivers/passthrough/arm/smmu.c       | 10 ----------
  4 files changed, 7 insertions(+), 28 deletions(-)
xen/drivers/passthrough/arm/iommu.c +7 -0
index ee653a9..fc45318 100644
@@ -134,6 +134,13 @@ void arch_iommu_domain_destroy(struct domain *d)
 
  void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
  {
+    /* Set to false options not supported on ARM. */
+    if ( iommu_hwdom_inclusive )
+        printk(XENLOG_WARNING "map-inclusive dom0-iommu option is not supported on ARM\n");
+    iommu_hwdom_inclusive = false;
+    if ( iommu_hwdom_reserved == 1 )
+        printk(XENLOG_WARNING "map-reserved dom0-iommu option is not supported on ARM\n");
+    iommu_hwdom_reserved = 0;
  }
 
  /*
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -0
index 1224ea4..64d8ddc 100644
@@ -1325,14 +1325,6 @@ static int ipmmu_iommu_domain_init(struct domain *d)
 
  static void __hwdom_init ipmmu_iommu_hwdom_init(struct domain *d)
  {

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_iommu_domain_init(struct domain *d) · line 1329

Quoted 10 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -8
-    /* Set to false options not supported on ARM. */
-    if ( iommu_hwdom_inclusive )
-        printk(XENLOG_WARNING "ipmmu: map-inclusive dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_inclusive = false;
-    if ( iommu_hwdom_reserved == 1 )
-        printk(XENLOG_WARNING "ipmmu: map-reserved dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_reserved = 0;
-
      arch_iommu_hwdom_init(d);
  }

With this change, ipmmu_iommu_domain_init() is just a call to arch_iommu_hwdom_init(). The two functions have the same prototype, so could we simply drop the former and update the .hwdom_init to directly point to the latter?

Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +0 -3
index d115df7..ca8b5c7 100644
@@ -3404,16 +3404,6 @@ static int arm_smmu_iommu_xen_domain_init(struct domain *d)
 
  static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
  {
-	/* Set to false options not supported on ARM. */
-	if (iommu_hwdom_inclusive)
-		printk(XENLOG_WARNING

xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_iommu_xen_domain_init(struct domain *d) · line 3409

Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +0 -7
-		"map-inclusive dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_inclusive = false;
-	if (iommu_hwdom_reserved == 1)
-		printk(XENLOG_WARNING
-		"map-reserved dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_reserved = 0;
-
  	arch_iommu_hwdom_init(d);
  }
 

Same here.

Quoted 11 lines
xen/drivers/passthrough/arm/smmu.c +0 -3
index c9dfc4c..ec18df7 100644
@@ -2851,16 +2851,6 @@ static int arm_smmu_iommu_domain_init(struct domain *d)
 
  static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
  {
-	/* Set to false options not supported on ARM. */
-	if ( iommu_hwdom_inclusive )
-		printk(XENLOG_WARNING

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_iommu_domain_init(struct domain *d) · line 2856

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +0 -7
-		"map-inclusive dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_inclusive = false;
-	if ( iommu_hwdom_reserved == 1 )
-		printk(XENLOG_WARNING
-		"map-reserved dom0-iommu option is not supported on ARM\n");
-	iommu_hwdom_reserved = 0;
-
  	arch_iommu_hwdom_init(d);
  }
 

Same here.

Cheers,

Signature · 1 line
Julien Grall
author
#16 lore raw

Re: [PATCH V2 10/10] iommu/arm: Remove code duplication in all IOMMU drivers

Quoted 1 line · Julien Grall

Hi,

Hi Julien

Quoted 53 lines · Oleksandr Tyshchenko
Quoted 51 lines

From: Oleksandr Tyshchenko <[email protected]>

All IOMMU drivers on Arm perform almost the same generic actions in hwdom_init callback. Move this code to common arch_iommu_hwdom_init() in order to get rid of code duplication.

---
Changes V1 -> V2:
    - add R-b
---
  xen/drivers/passthrough/arm/iommu.c      |  7 +++++++ xen/drivers/passthrough/arm/ipmmu-vmsa.c |  8 -------- xen/drivers/passthrough/arm/smmu-v3.c    | 10 ---------- xen/drivers/passthrough/arm/smmu.c       | 10 ----------

  4 files changed, 7 insertions(+), 28 deletions(-)
xen/drivers/passthrough/arm/iommu.c +7 -0
index ee653a9..fc45318 100644
@@ -134,6 +134,13 @@ void arch_iommu_domain_destroy(struct domain *d)
     void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
   {
+    /* Set to false options not supported on ARM. */
+    if ( iommu_hwdom_inclusive )
+        printk(XENLOG_WARNING "map-inclusive dom0-iommu option is 
 not supported on ARM\n");
+    iommu_hwdom_inclusive = false;
+    if ( iommu_hwdom_reserved == 1 )
+        printk(XENLOG_WARNING "map-reserved dom0-iommu option is not 
 supported on ARM\n");
+    iommu_hwdom_reserved = 0;
   }
     /*
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -5
index 1224ea4..64d8ddc 100644
@@ -1325,14 +1325,6 @@ static int ipmmu_iommu_domain_init(struct 
 domain *d)
     static void __hwdom_init ipmmu_iommu_hwdom_init(struct domain *d)
   {
-    /* Set to false options not supported on ARM. */
-    if ( iommu_hwdom_inclusive )
-        printk(XENLOG_WARNING "ipmmu: map-inclusive dom0-iommu 
 option is not supported on ARM\n");
-    iommu_hwdom_inclusive = false;
-    if ( iommu_hwdom_reserved == 1 )
Quoted 8 lines
Quoted 6 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -3
-        printk(XENLOG_WARNING "ipmmu: map-reserved dom0-iommu option 
 is not supported on ARM\n");
-    iommu_hwdom_reserved = 0;
-
       arch_iommu_hwdom_init(d);

  }

With this change, ipmmu_iommu_domain_init() is just a call to arch_iommu_hwdom_init(). The two functions have the same prototype, so could we simply drop the former and update the .hwdom_init to directly point to the latter?

Yes, good idea, will update. Thank you.

Quoted 50 lines
Quoted 12 lines

  diff --git a/xen/drivers/passthrough/arm/smmu-v3.c b/xen/drivers/passthrough/arm/smmu-v3.c index d115df7..ca8b5c7 100644

xen/drivers/passthrough/arm/smmu-v3.c +0 -2
@@ -3404,16 +3404,6 @@ static int 
 arm_smmu_iommu_xen_domain_init(struct domain *d)
     static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
   {
-    /* Set to false options not supported on ARM. */
-    if (iommu_hwdom_inclusive)

xen/drivers/passthrough/arm/smmu-v3.c static int · line 3408

Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +0 -8
-        printk(XENLOG_WARNING
-        "map-inclusive dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_inclusive = false;
-    if (iommu_hwdom_reserved == 1)
-        printk(XENLOG_WARNING
-        "map-reserved dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_reserved = 0;
-
       arch_iommu_hwdom_init(d);
   }

Same here.

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +0 -2
index c9dfc4c..ec18df7 100644
@@ -2851,16 +2851,6 @@ static int arm_smmu_iommu_domain_init(struct 
 domain *d)
     static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d)
   {
-    /* Set to false options not supported on ARM. */
-    if ( iommu_hwdom_inclusive )

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_iommu_domain_init(struct · line 2855

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +0 -8
-        printk(XENLOG_WARNING
-        "map-inclusive dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_inclusive = false;
-    if ( iommu_hwdom_reserved == 1 )
-        printk(XENLOG_WARNING
-        "map-reserved dom0-iommu option is not supported on ARM\n");
-    iommu_hwdom_reserved = 0;
-
       arch_iommu_hwdom_init(d);
   }

Same here.

Cheers,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
Acked-by on the v2 series
#17 lore raw
In reply to Oleksandr Tyshchenko

Hi,

Quoted 36 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

You can find the V1 patch series at [1].

The R-Car S4 is an automotive System-on-Chip (SoC) for Car Server/Communication Gateway and is one of the first products in Renesas’ 4th-generation R-Car Family.

The integrated IOMMU HW is also VMSA-compatible and supports stage 2 translation table format, therefore can be used with current R-Car Gen3 driver with slight modifications.

In the context of Xen driver the main differences between Gen3 and Gen4 are the following:
- HW capacity was enlarged to support up to 16 IPMMU contexts (sets of page table) and up to 64 micro-TLBs per IPMMU device
- the memory mapped registers have different bases and offset

The first part (commits #1-6) is a non-verbatim port of Linux upstream commits needed to add support for S4 series easily (prereq work).
The second part (commits #7-8) is based on the code from the Renesas BSP and actually introduces support for R-Car S4 IPMMU.
The third part (commits #9-10) is misc changes I have locally.

The patch series is based on the latest staging branch "f3999bc2e099 arm/efi: Handle Xen bootargs from both xen.cfg and DT" and also available at [2].

Tested on Renesas Salvator-X board with H3 ES3.0 SoC (Gen3) and Renesas Spider board with S4 SoC (Gen4).

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/otyshchenko1/xen/commits/s4_ipmmu_ml2

Quoted 10 lines

Oleksandr Tyshchenko (10):
iommu/ipmmu-vmsa: Remove all unused register definitions iommu/ipmmu-vmsa: Add helper functions for MMU "context" registers iommu/ipmmu-vmsa: Add helper functions for "uTLB" registers iommu/ipmmu-vmsa: Add light version of Linux's ipmmu_features iommu/ipmmu-vmsa: Calculate context registers' offset instead of a
macro
iommu/ipmmu-vmsa: Add utlb_offset_base iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

For the first 8 patches:

I have also committed them. The last..

Quoted 2 lines

iommu/ipmmu-vmsa: Use refcount for the micro-TLBs iommu/arm: Remove code duplication in all IOMMU drivers

... 2 patches have some comments. So I have committed them yet.

Cheers,

Signature · 1 line
Julien Grall
author
#18 lore raw
Quoted 1 line · Julien Grall

Hi,

Hi Julien

Quoted 49 lines · Oleksandr Tyshchenko
Quoted 36 lines

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

You can find the V1 patch series at [1].

The R-Car S4 is an automotive System-on-Chip (SoC) for Car Server/Communication Gateway and is one of the first products in Renesas’ 4th-generation R-Car Family.

The integrated IOMMU HW is also VMSA-compatible and supports stage 2 translation table format, therefore can be used with current R-Car Gen3 driver with slight modifications.

In the context of Xen driver the main differences between Gen3 and Gen4 are the following:
  - HW capacity was enlarged to support up to 16 IPMMU contexts (sets of page table) and up to 64 micro-TLBs per IPMMU device
  - the memory mapped registers have different bases and offset

The first part (commits #1-6) is a non-verbatim port of Linux upstream commits needed to add support for S4 series easily (prereq work).
The second part (commits #7-8) is based on the code from the Renesas BSP and actually introduces support for R-Car S4 IPMMU.
The third part (commits #9-10) is misc changes I have locally.

The patch series is based on the latest staging branch "f3999bc2e099 arm/efi: Handle Xen bootargs from both xen.cfg and DT" and also available at [2].

Tested on Renesas Salvator-X board with H3 ES3.0 SoC (Gen3) and Renesas Spider board with S4 SoC (Gen4).

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/otyshchenko1/xen/commits/s4_ipmmu_ml2

Quoted 10 lines

Oleksandr Tyshchenko (10):
   iommu/ipmmu-vmsa: Remove all unused register definitions iommu/ipmmu-vmsa: Add helper functions for MMU "context" registers iommu/ipmmu-vmsa: Add helper functions for "uTLB" registers iommu/ipmmu-vmsa: Add light version of Linux's ipmmu_features iommu/ipmmu-vmsa: Calculate context registers' offset instead of a macro
   iommu/ipmmu-vmsa: Add utlb_offset_base iommu/ipmmu-vmsa: Add Renesas R8A779F0 (R-Car S4) support iommu/ipmmu-vmsa: Set IPMMU bit IMSCTLR_USE_SECGRP to 0

Quoted 3 lines

For the first 8 patches:

Thank you!

Quoted 7 lines

I have also committed them. The last..

Quoted 2 lines

   iommu/ipmmu-vmsa: Use refcount for the micro-TLBs iommu/arm: Remove code duplication in all IOMMU drivers

... 2 patches have some comments. So I have committed them yet.

ok, I will update them and re-push probably today.

Quoted 4 lines

Cheers,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko