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

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