Blame SOURCES/kvm-hw-arm-smmuv3-Get-prepared-for-range-invalidation.patch

8fced6
From 3f027ac56449e51a61e76c18b97fd341d302dc80 Mon Sep 17 00:00:00 2001
8fced6
From: eperezma <eperezma@redhat.com>
8fced6
Date: Tue, 12 Jan 2021 14:36:32 -0500
8fced6
Subject: [PATCH 08/17] hw/arm/smmuv3: Get prepared for range invalidation
8fced6
MIME-Version: 1.0
8fced6
Content-Type: text/plain; charset=UTF-8
8fced6
Content-Transfer-Encoding: 8bit
8fced6
8fced6
RH-Author: eperezma <eperezma@redhat.com>
8fced6
Message-id: <20210112143638.374060-8-eperezma@redhat.com>
8fced6
Patchwork-id: 100600
8fced6
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 07/13] hw/arm/smmuv3: Get prepared for range invalidation
8fced6
Bugzilla: 1843852
8fced6
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
8fced6
RH-Acked-by: Peter Xu <peterx@redhat.com>
8fced6
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
8fced6
8fced6
From: Eric Auger <eric.auger@redhat.com>
8fced6
8fced6
Enhance the smmu_iotlb_inv_iova() helper with range invalidation.
8fced6
This uses the new fields passed in the NH_VA and NH_VAA commands:
8fced6
the size of the range, the level and the granule.
8fced6
8fced6
As NH_VA and NH_VAA both use those fields, their decoding and
8fced6
handling is factorized in a new smmuv3_s1_range_inval() helper.
8fced6
8fced6
Signed-off-by: Eric Auger <eric.auger@redhat.com>
8fced6
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
8fced6
Message-id: 20200728150815.11446-8-eric.auger@redhat.com
8fced6
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
8fced6
(cherry picked from commit d52915616c059ed273caa2d496b58e5d215c5962)
8fced6
Signed-off-by: Eugenio PĂ©rez <eperezma@redhat.com>
8fced6
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
8fced6
---
8fced6
 hw/arm/smmu-common.c         | 25 +++++++++++---
8fced6
 hw/arm/smmuv3-internal.h     |  4 +++
8fced6
 hw/arm/smmuv3.c              | 64 +++++++++++++++++++++++-------------
8fced6
 hw/arm/trace-events          |  4 +--
8fced6
 include/hw/arm/smmu-common.h |  3 +-
8fced6
 5 files changed, 69 insertions(+), 31 deletions(-)
8fced6
8fced6
diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c
8fced6
index 8007edeaaa2..9780404f002 100644
8fced6
--- a/hw/arm/smmu-common.c
8fced6
+++ b/hw/arm/smmu-common.c
8fced6
@@ -143,15 +143,30 @@ static gboolean smmu_hash_remove_by_asid_iova(gpointer key, gpointer value,
8fced6
     if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) {
8fced6
         return false;
8fced6
     }
8fced6
-    return (info->iova & ~entry->addr_mask) == entry->iova;
8fced6
+    return ((info->iova & ~entry->addr_mask) == entry->iova) ||
8fced6
+           ((entry->iova & ~info->mask) == info->iova);
8fced6
 }
8fced6
 
8fced6
-inline void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova)
8fced6
+inline void
8fced6
+smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova,
8fced6
+                    uint8_t tg, uint64_t num_pages, uint8_t ttl)
8fced6
 {
8fced6
-    SMMUIOTLBPageInvInfo info = {.asid = asid, .iova = iova};
8fced6
+    if (ttl && (num_pages == 1)) {
8fced6
+        SMMUIOTLBKey key = smmu_get_iotlb_key(asid, iova, tg, ttl);
8fced6
 
8fced6
-    trace_smmu_iotlb_inv_iova(asid, iova);
8fced6
-    g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_asid_iova, &info;;
8fced6
+        g_hash_table_remove(s->iotlb, &key);
8fced6
+    } else {
8fced6
+        /* if tg is not set we use 4KB range invalidation */
8fced6
+        uint8_t granule = tg ? tg * 2 + 10 : 12;
8fced6
+
8fced6
+        SMMUIOTLBPageInvInfo info = {
8fced6
+            .asid = asid, .iova = iova,
8fced6
+            .mask = (num_pages * 1 << granule) - 1};
8fced6
+
8fced6
+        g_hash_table_foreach_remove(s->iotlb,
8fced6
+                                    smmu_hash_remove_by_asid_iova,
8fced6
+                                    &info;;
8fced6
+    }
8fced6
 }
8fced6
 
8fced6
 inline void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid)
8fced6
diff --git a/hw/arm/smmuv3-internal.h b/hw/arm/smmuv3-internal.h
8fced6
index d190181ef1b..a4ec2c591cd 100644
8fced6
--- a/hw/arm/smmuv3-internal.h
8fced6
+++ b/hw/arm/smmuv3-internal.h
8fced6
@@ -298,6 +298,8 @@ enum { /* Command completion notification */
8fced6
 };
8fced6
 
8fced6
 #define CMD_TYPE(x)         extract32((x)->word[0], 0 , 8)
8fced6
+#define CMD_NUM(x)          extract32((x)->word[0], 12 , 5)
8fced6
+#define CMD_SCALE(x)        extract32((x)->word[0], 20 , 5)
8fced6
 #define CMD_SSEC(x)         extract32((x)->word[0], 10, 1)
8fced6
 #define CMD_SSV(x)          extract32((x)->word[0], 11, 1)
8fced6
 #define CMD_RESUME_AC(x)    extract32((x)->word[0], 12, 1)
8fced6
@@ -310,6 +312,8 @@ enum { /* Command completion notification */
8fced6
 #define CMD_RESUME_STAG(x)  extract32((x)->word[2], 0 , 16)
8fced6
 #define CMD_RESP(x)         extract32((x)->word[2], 11, 2)
8fced6
 #define CMD_LEAF(x)         extract32((x)->word[2], 0 , 1)
8fced6
+#define CMD_TTL(x)          extract32((x)->word[2], 8 , 2)
8fced6
+#define CMD_TG(x)           extract32((x)->word[2], 10, 2)
8fced6
 #define CMD_STE_RANGE(x)    extract32((x)->word[2], 0 , 5)
8fced6
 #define CMD_ADDR(x) ({                                        \
8fced6
             uint64_t high = (uint64_t)(x)->word[3];           \
8fced6
diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c
8fced6
index ae2b769f891..f4d5d9d8222 100644
8fced6
--- a/hw/arm/smmuv3.c
8fced6
+++ b/hw/arm/smmuv3.c
8fced6
@@ -773,42 +773,49 @@ epilogue:
8fced6
  * @n: notifier to be called
8fced6
  * @asid: address space ID or negative value if we don't care
8fced6
  * @iova: iova
8fced6
+ * @tg: translation granule (if communicated through range invalidation)
8fced6
+ * @num_pages: number of @granule sized pages (if tg != 0), otherwise 1
8fced6
  */
8fced6
 static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,
8fced6
                                IOMMUNotifier *n,
8fced6
-                               int asid,
8fced6
-                               dma_addr_t iova)
8fced6
+                               int asid, dma_addr_t iova,
8fced6
+                               uint8_t tg, uint64_t num_pages)
8fced6
 {
8fced6
     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
8fced6
-    SMMUEventInfo event = {.inval_ste_allowed = true};
8fced6
-    SMMUTransTableInfo *tt;
8fced6
-    SMMUTransCfg *cfg;
8fced6
     IOMMUTLBEntry entry;
8fced6
+    uint8_t granule = tg;
8fced6
 
8fced6
-    cfg = smmuv3_get_config(sdev, &event);
8fced6
-    if (!cfg) {
8fced6
-        return;
8fced6
-    }
8fced6
+    if (!tg) {
8fced6
+        SMMUEventInfo event = {.inval_ste_allowed = true};
8fced6
+        SMMUTransCfg *cfg = smmuv3_get_config(sdev, &event);
8fced6
+        SMMUTransTableInfo *tt;
8fced6
 
8fced6
-    if (asid >= 0 && cfg->asid != asid) {
8fced6
-        return;
8fced6
-    }
8fced6
+        if (!cfg) {
8fced6
+            return;
8fced6
+        }
8fced6
 
8fced6
-    tt = select_tt(cfg, iova);
8fced6
-    if (!tt) {
8fced6
-        return;
8fced6
+        if (asid >= 0 && cfg->asid != asid) {
8fced6
+            return;
8fced6
+        }
8fced6
+
8fced6
+        tt = select_tt(cfg, iova);
8fced6
+        if (!tt) {
8fced6
+            return;
8fced6
+        }
8fced6
+        granule = tt->granule_sz;
8fced6
     }
8fced6
 
8fced6
     entry.target_as = &address_space_memory;
8fced6
     entry.iova = iova;
8fced6
-    entry.addr_mask = (1 << tt->granule_sz) - 1;
8fced6
+    entry.addr_mask = num_pages * (1 << granule) - 1;
8fced6
     entry.perm = IOMMU_NONE;
8fced6
 
8fced6
     memory_region_notify_one(n, &entry);
8fced6
 }
8fced6
 
8fced6
-/* invalidate an asid/iova tuple in all mr's */
8fced6
-static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova)
8fced6
+/* invalidate an asid/iova range tuple in all mr's */
8fced6
+static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova,
8fced6
+                                      uint8_t tg, uint64_t num_pages)
8fced6
 {
8fced6
     SMMUDevice *sdev;
8fced6
 
8fced6
@@ -816,28 +823,39 @@ static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova)
8fced6
         IOMMUMemoryRegion *mr = &sdev->iommu;
8fced6
         IOMMUNotifier *n;
8fced6
 
8fced6
-        trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, iova);
8fced6
+        trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, iova,
8fced6
+                                        tg, num_pages);
8fced6
 
8fced6
         IOMMU_NOTIFIER_FOREACH(n, mr) {
8fced6
-            smmuv3_notify_iova(mr, n, asid, iova);
8fced6
+            smmuv3_notify_iova(mr, n, asid, iova, tg, num_pages);
8fced6
         }
8fced6
     }
8fced6
 }
8fced6
 
8fced6
 static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd)
8fced6
 {
8fced6
+    uint8_t scale = 0, num = 0, ttl = 0;
8fced6
     dma_addr_t addr = CMD_ADDR(cmd);
8fced6
     uint8_t type = CMD_TYPE(cmd);
8fced6
     uint16_t vmid = CMD_VMID(cmd);
8fced6
     bool leaf = CMD_LEAF(cmd);
8fced6
+    uint8_t tg = CMD_TG(cmd);
8fced6
+    hwaddr num_pages = 1;
8fced6
     int asid = -1;
8fced6
 
8fced6
+    if (tg) {
8fced6
+        scale = CMD_SCALE(cmd);
8fced6
+        num = CMD_NUM(cmd);
8fced6
+        ttl = CMD_TTL(cmd);
8fced6
+        num_pages = (num + 1) * (1 << (scale));
8fced6
+    }
8fced6
+
8fced6
     if (type == SMMU_CMD_TLBI_NH_VA) {
8fced6
         asid = CMD_ASID(cmd);
8fced6
     }
8fced6
-    trace_smmuv3_s1_range_inval(vmid, asid, addr, leaf);
8fced6
-    smmuv3_inv_notifiers_iova(s, asid, addr);
8fced6
-    smmu_iotlb_inv_iova(s, asid, addr);
8fced6
+    trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, num_pages, ttl, leaf);
8fced6
+    smmuv3_inv_notifiers_iova(s, asid, addr, tg, num_pages);
8fced6
+    smmu_iotlb_inv_iova(s, asid, addr, tg, num_pages, ttl);
8fced6
 }
8fced6
 
8fced6
 static int smmuv3_cmdq_consume(SMMUv3State *s)
8fced6
diff --git a/hw/arm/trace-events b/hw/arm/trace-events
8fced6
index c219fe9e828..3d905e0f7d0 100644
8fced6
--- a/hw/arm/trace-events
8fced6
+++ b/hw/arm/trace-events
8fced6
@@ -45,11 +45,11 @@ smmuv3_cmdq_cfgi_ste_range(int start, int end) "start=0x%d - end=0x%d"
8fced6
 smmuv3_cmdq_cfgi_cd(uint32_t sid) "streamid = %d"
8fced6
 smmuv3_config_cache_hit(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache HIT for sid %d (hits=%d, misses=%d, hit rate=%d)"
8fced6
 smmuv3_config_cache_miss(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache MISS for sid %d (hits=%d, misses=%d, hit rate=%d)"
8fced6
-smmuv3_s1_range_inval(int vmid, int asid, uint64_t addr, bool leaf) "vmid =%d asid =%d addr=0x%"PRIx64" leaf=%d"
8fced6
+smmuv3_s1_range_inval(int vmid, int asid, uint64_t addr, uint8_t tg, uint64_t num_pages, uint8_t ttl, bool leaf) "vmid =%d asid =%d addr=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64" ttl=%d leaf=%d"
8fced6
 smmuv3_cmdq_tlbi_nh(void) ""
8fced6
 smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d"
8fced6
 smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %d"
8fced6
 smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s"
8fced6
 smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s"
8fced6
-smmuv3_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova) "iommu mr=%s asid=%d iova=0x%"PRIx64
8fced6
+smmuv3_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova, uint8_t tg, uint64_t num_pages) "iommu mr=%s asid=%d iova=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64
8fced6
 
8fced6
diff --git a/include/hw/arm/smmu-common.h b/include/hw/arm/smmu-common.h
8fced6
index bbf3abc41fd..13489a1ac0d 100644
8fced6
--- a/include/hw/arm/smmu-common.h
8fced6
+++ b/include/hw/arm/smmu-common.h
8fced6
@@ -168,7 +168,8 @@ SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova,
8fced6
                                 uint8_t tg, uint8_t level);
8fced6
 void smmu_iotlb_inv_all(SMMUState *s);
8fced6
 void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid);
8fced6
-void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova);
8fced6
+void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova,
8fced6
+                         uint8_t tg, uint64_t num_pages, uint8_t ttl);
8fced6
 
8fced6
 /* Unmap the range of all the notifiers registered to any IOMMU mr */
8fced6
 void smmu_inv_notifiers_all(SMMUState *s);
8fced6
-- 
8fced6
2.27.0
8fced6