c687bc
From 7833c0bf8321cb39614ee889cf3e3a64511c0aa5 Mon Sep 17 00:00:00 2001
c687bc
From: eperezma <eperezma@redhat.com>
c687bc
Date: Tue, 12 Jan 2021 14:36:28 -0500
c687bc
Subject: [PATCH 04/17] hw/arm/smmu: Introduce smmu_get_iotlb_key()
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-4-eperezma@redhat.com>
c687bc
Patchwork-id: 100596
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 03/13] hw/arm/smmu: Introduce smmu_get_iotlb_key()
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
Introduce the smmu_get_iotlb_key() helper and the
c687bc
SMMU_IOTLB_ASID() macro. Also move smmu_get_iotlb_key and
c687bc
smmu_iotlb_key_hash in the IOTLB related code section.
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-4-eric.auger@redhat.com
c687bc
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
c687bc
(cherry picked from commit 60a61f1b31fc03080aadb63c9b1006f8b1972adb)
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         | 66 ++++++++++++++++++++----------------
c687bc
 hw/arm/smmu-internal.h       |  1 +
c687bc
 include/hw/arm/smmu-common.h |  1 +
c687bc
 3 files changed, 38 insertions(+), 30 deletions(-)
c687bc
c687bc
diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c
c687bc
index 8e01505dbee..0b89c9fbbbc 100644
c687bc
--- a/hw/arm/smmu-common.c
c687bc
+++ b/hw/arm/smmu-common.c
c687bc
@@ -32,10 +32,42 @@
c687bc
 
c687bc
 /* IOTLB Management */
c687bc
 
c687bc
+static guint smmu_iotlb_key_hash(gconstpointer v)
c687bc
+{
c687bc
+    SMMUIOTLBKey *key = (SMMUIOTLBKey *)v;
c687bc
+    uint32_t a, b, c;
c687bc
+
c687bc
+    /* Jenkins hash */
c687bc
+    a = b = c = JHASH_INITVAL + sizeof(*key);
c687bc
+    a += key->asid;
c687bc
+    b += extract64(key->iova, 0, 32);
c687bc
+    c += extract64(key->iova, 32, 32);
c687bc
+
c687bc
+    __jhash_mix(a, b, c);
c687bc
+    __jhash_final(a, b, c);
c687bc
+
c687bc
+    return c;
c687bc
+}
c687bc
+
c687bc
+static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
c687bc
+{
c687bc
+    const SMMUIOTLBKey *k1 = v1;
c687bc
+    const SMMUIOTLBKey *k2 = v2;
c687bc
+
c687bc
+    return (k1->asid == k2->asid) && (k1->iova == k2->iova);
c687bc
+}
c687bc
+
c687bc
+SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova)
c687bc
+{
c687bc
+    SMMUIOTLBKey key = {.asid = asid, .iova = iova};
c687bc
+
c687bc
+    return key;
c687bc
+}
c687bc
+
c687bc
 IOMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
c687bc
                                  hwaddr iova)
c687bc
 {
c687bc
-    SMMUIOTLBKey key = {.asid = cfg->asid, .iova = iova};
c687bc
+    SMMUIOTLBKey key = smmu_get_iotlb_key(cfg->asid, iova);
c687bc
     IOMMUTLBEntry *entry = g_hash_table_lookup(bs->iotlb, &key);
c687bc
 
c687bc
     if (entry) {
c687bc
@@ -62,8 +94,7 @@ void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, IOMMUTLBEntry *entry)
c687bc
         smmu_iotlb_inv_all(bs);
c687bc
     }
c687bc
 
c687bc
-    key->asid = cfg->asid;
c687bc
-    key->iova = entry->iova;
c687bc
+    *key = smmu_get_iotlb_key(cfg->asid, entry->iova);
c687bc
     trace_smmu_iotlb_insert(cfg->asid, entry->iova);
c687bc
     g_hash_table_insert(bs->iotlb, key, entry);
c687bc
 }
c687bc
@@ -80,12 +111,12 @@ static gboolean smmu_hash_remove_by_asid(gpointer key, gpointer value,
c687bc
     uint16_t asid = *(uint16_t *)user_data;
c687bc
     SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
c687bc
 
c687bc
-    return iotlb_key->asid == asid;
c687bc
+    return SMMU_IOTLB_ASID(*iotlb_key) == asid;
c687bc
 }
c687bc
 
c687bc
 inline void smmu_iotlb_inv_iova(SMMUState *s, uint16_t asid, dma_addr_t iova)
c687bc
 {
c687bc
-    SMMUIOTLBKey key = {.asid = asid, .iova = iova};
c687bc
+    SMMUIOTLBKey key = smmu_get_iotlb_key(asid, iova);
c687bc
 
c687bc
     trace_smmu_iotlb_inv_iova(asid, iova);
c687bc
     g_hash_table_remove(s->iotlb, &key);
c687bc
@@ -382,31 +413,6 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)
c687bc
     return NULL;
c687bc
 }
c687bc
 
c687bc
-static guint smmu_iotlb_key_hash(gconstpointer v)
c687bc
-{
c687bc
-    SMMUIOTLBKey *key = (SMMUIOTLBKey *)v;
c687bc
-    uint32_t a, b, c;
c687bc
-
c687bc
-    /* Jenkins hash */
c687bc
-    a = b = c = JHASH_INITVAL + sizeof(*key);
c687bc
-    a += key->asid;
c687bc
-    b += extract64(key->iova, 0, 32);
c687bc
-    c += extract64(key->iova, 32, 32);
c687bc
-
c687bc
-    __jhash_mix(a, b, c);
c687bc
-    __jhash_final(a, b, c);
c687bc
-
c687bc
-    return c;
c687bc
-}
c687bc
-
c687bc
-static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
c687bc
-{
c687bc
-    const SMMUIOTLBKey *k1 = v1;
c687bc
-    const SMMUIOTLBKey *k2 = v2;
c687bc
-
c687bc
-    return (k1->asid == k2->asid) && (k1->iova == k2->iova);
c687bc
-}
c687bc
-
c687bc
 /* Unmap the whole notifier's range */
c687bc
 static void smmu_unmap_notifier_range(IOMMUNotifier *n)
c687bc
 {
c687bc
diff --git a/hw/arm/smmu-internal.h b/hw/arm/smmu-internal.h
c687bc
index 7794d6d3947..3104f768cd2 100644
c687bc
--- a/hw/arm/smmu-internal.h
c687bc
+++ b/hw/arm/smmu-internal.h
c687bc
@@ -96,4 +96,5 @@ uint64_t iova_level_offset(uint64_t iova, int inputsize,
c687bc
             MAKE_64BIT_MASK(0, gsz - 3);
c687bc
 }
c687bc
 
c687bc
+#define SMMU_IOTLB_ASID(key) ((key).asid)
c687bc
 #endif
c687bc
diff --git a/include/hw/arm/smmu-common.h b/include/hw/arm/smmu-common.h
c687bc
index a28650c9350..bceba40885c 100644
c687bc
--- a/include/hw/arm/smmu-common.h
c687bc
+++ b/include/hw/arm/smmu-common.h
c687bc
@@ -155,6 +155,7 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
c687bc
 
c687bc
 IOMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, hwaddr iova);
c687bc
 void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, IOMMUTLBEntry *entry);
c687bc
+SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova);
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