c687bc
From fbfa584e58a560f27081043ad8e90ee9022421c0 Mon Sep 17 00:00:00 2001
c687bc
From: eperezma <eperezma@redhat.com>
c687bc
Date: Tue, 12 Jan 2021 14:36:27 -0500
c687bc
Subject: [PATCH 03/17] hw/arm/smmu-common: Add IOTLB helpers
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-3-eperezma@redhat.com>
c687bc
Patchwork-id: 100595
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 02/13] hw/arm/smmu-common: Add IOTLB helpers
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
Add two helpers: one to lookup for a given IOTLB entry and
c687bc
one to insert a new entry. We also move the tracing there.
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-3-eric.auger@redhat.com
c687bc
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
c687bc
(cherry picked from commit 6808bca939b8722d98165319ba42366ca80de907)
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         | 36 ++++++++++++++++++++++++++++++++++++
c687bc
 hw/arm/smmuv3.c              | 26 ++------------------------
c687bc
 hw/arm/trace-events          |  5 +++--
c687bc
 include/hw/arm/smmu-common.h |  2 ++
c687bc
 4 files changed, 43 insertions(+), 26 deletions(-)
c687bc
c687bc
diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c
c687bc
index d2ba8b224ba..8e01505dbee 100644
c687bc
--- a/hw/arm/smmu-common.c
c687bc
+++ b/hw/arm/smmu-common.c
c687bc
@@ -32,6 +32,42 @@
c687bc
 
c687bc
 /* IOTLB Management */
c687bc
 
c687bc
+IOMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
c687bc
+                                 hwaddr iova)
c687bc
+{
c687bc
+    SMMUIOTLBKey key = {.asid = cfg->asid, .iova = iova};
c687bc
+    IOMMUTLBEntry *entry = g_hash_table_lookup(bs->iotlb, &key);
c687bc
+
c687bc
+    if (entry) {
c687bc
+        cfg->iotlb_hits++;
c687bc
+        trace_smmu_iotlb_lookup_hit(cfg->asid, iova,
c687bc
+                                    cfg->iotlb_hits, cfg->iotlb_misses,
c687bc
+                                    100 * cfg->iotlb_hits /
c687bc
+                                    (cfg->iotlb_hits + cfg->iotlb_misses));
c687bc
+    } else {
c687bc
+        cfg->iotlb_misses++;
c687bc
+        trace_smmu_iotlb_lookup_miss(cfg->asid, iova,
c687bc
+                                     cfg->iotlb_hits, cfg->iotlb_misses,
c687bc
+                                     100 * cfg->iotlb_hits /
c687bc
+                                     (cfg->iotlb_hits + cfg->iotlb_misses));
c687bc
+    }
c687bc
+    return entry;
c687bc
+}
c687bc
+
c687bc
+void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, IOMMUTLBEntry *entry)
c687bc
+{
c687bc
+    SMMUIOTLBKey *key = g_new0(SMMUIOTLBKey, 1);
c687bc
+
c687bc
+    if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
c687bc
+        smmu_iotlb_inv_all(bs);
c687bc
+    }
c687bc
+
c687bc
+    key->asid = cfg->asid;
c687bc
+    key->iova = entry->iova;
c687bc
+    trace_smmu_iotlb_insert(cfg->asid, entry->iova);
c687bc
+    g_hash_table_insert(bs->iotlb, key, entry);
c687bc
+}
c687bc
+
c687bc
 inline void smmu_iotlb_inv_all(SMMUState *s)
c687bc
 {
c687bc
     trace_smmu_iotlb_inv_all();
c687bc
diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c
c687bc
index e2fbb8357ea..34dea4df4da 100644
c687bc
--- a/hw/arm/smmuv3.c
c687bc
+++ b/hw/arm/smmuv3.c
c687bc
@@ -624,7 +624,6 @@ static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
c687bc
         .addr_mask = ~(hwaddr)0,
c687bc
         .perm = IOMMU_NONE,
c687bc
     };
c687bc
-    SMMUIOTLBKey key, *new_key;
c687bc
 
c687bc
     qemu_mutex_lock(&s->mutex);
c687bc
 
c687bc
@@ -663,16 +662,8 @@ static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
c687bc
     page_mask = (1ULL << (tt->granule_sz)) - 1;
c687bc
     aligned_addr = addr & ~page_mask;
c687bc
 
c687bc
-    key.asid = cfg->asid;
c687bc
-    key.iova = aligned_addr;
c687bc
-
c687bc
-    cached_entry = g_hash_table_lookup(bs->iotlb, &key);
c687bc
+    cached_entry = smmu_iotlb_lookup(bs, cfg, aligned_addr);
c687bc
     if (cached_entry) {
c687bc
-        cfg->iotlb_hits++;
c687bc
-        trace_smmu_iotlb_cache_hit(cfg->asid, aligned_addr,
c687bc
-                                   cfg->iotlb_hits, cfg->iotlb_misses,
c687bc
-                                   100 * cfg->iotlb_hits /
c687bc
-                                   (cfg->iotlb_hits + cfg->iotlb_misses));
c687bc
         if ((flag & IOMMU_WO) && !(cached_entry->perm & IOMMU_WO)) {
c687bc
             status = SMMU_TRANS_ERROR;
c687bc
             if (event.record_trans_faults) {
c687bc
@@ -686,16 +677,6 @@ static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
c687bc
         goto epilogue;
c687bc
     }
c687bc
 
c687bc
-    cfg->iotlb_misses++;
c687bc
-    trace_smmu_iotlb_cache_miss(cfg->asid, addr & ~page_mask,
c687bc
-                                cfg->iotlb_hits, cfg->iotlb_misses,
c687bc
-                                100 * cfg->iotlb_hits /
c687bc
-                                (cfg->iotlb_hits + cfg->iotlb_misses));
c687bc
-
c687bc
-    if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
c687bc
-        smmu_iotlb_inv_all(bs);
c687bc
-    }
c687bc
-
c687bc
     cached_entry = g_new0(IOMMUTLBEntry, 1);
c687bc
 
c687bc
     if (smmu_ptw(cfg, aligned_addr, flag, cached_entry, &ptw_info)) {
c687bc
@@ -741,10 +722,7 @@ static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
c687bc
         }
c687bc
         status = SMMU_TRANS_ERROR;
c687bc
     } else {
c687bc
-        new_key = g_new0(SMMUIOTLBKey, 1);
c687bc
-        new_key->asid = cfg->asid;
c687bc
-        new_key->iova = aligned_addr;
c687bc
-        g_hash_table_insert(bs->iotlb, new_key, cached_entry);
c687bc
+        smmu_iotlb_insert(bs, cfg, cached_entry);
c687bc
         status = SMMU_TRANS_SUCCESS;
c687bc
     }
c687bc
 
c687bc
diff --git a/hw/arm/trace-events b/hw/arm/trace-events
c687bc
index 0acedcedc6f..b808a1bfc19 100644
c687bc
--- a/hw/arm/trace-events
c687bc
+++ b/hw/arm/trace-events
c687bc
@@ -14,6 +14,9 @@ smmu_iotlb_inv_all(void) "IOTLB invalidate all"
c687bc
 smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d"
c687bc
 smmu_iotlb_inv_iova(uint16_t asid, uint64_t addr) "IOTLB invalidate asid=%d addr=0x%"PRIx64
c687bc
 smmu_inv_notifiers_mr(const char *name) "iommu mr=%s"
c687bc
+smmu_iotlb_lookup_hit(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache HIT asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d"
c687bc
+smmu_iotlb_lookup_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache MISS asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d"
c687bc
+smmu_iotlb_insert(uint16_t asid, uint64_t addr) "IOTLB ++ asid=%d addr=0x%"PRIx64
c687bc
 
c687bc
 # smmuv3.c
c687bc
 smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"
c687bc
@@ -46,8 +49,6 @@ smmuv3_cmdq_tlbi_nh_va(int vmid, int asid, uint64_t addr, bool leaf) "vmid =%d a
c687bc
 smmuv3_cmdq_tlbi_nh_vaa(int vmid, uint64_t addr) "vmid =%d addr=0x%"PRIx64
c687bc
 smmuv3_cmdq_tlbi_nh(void) ""
c687bc
 smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d"
c687bc
-smmu_iotlb_cache_hit(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache HIT asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d"
c687bc
-smmu_iotlb_cache_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache MISS asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%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
diff --git a/include/hw/arm/smmu-common.h b/include/hw/arm/smmu-common.h
c687bc
index 1f37844e5c9..a28650c9350 100644
c687bc
--- a/include/hw/arm/smmu-common.h
c687bc
+++ b/include/hw/arm/smmu-common.h
c687bc
@@ -153,6 +153,8 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
c687bc
 
c687bc
 #define SMMU_IOTLB_MAX_SIZE 256
c687bc
 
c687bc
+IOMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, hwaddr iova);
c687bc
+void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, IOMMUTLBEntry *entry);
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, uint16_t asid, dma_addr_t iova);
c687bc
-- 
c687bc
2.27.0
c687bc