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