Skip to contentSkip to conversation

target/arm/tcg: refine cache descriptions with a wrapper

PATCH Superseded Alireza Sanaee 2 participants 3 messages Sep 2, 2024 – Sep 3, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 target/arm/tcg: refine cache descriptions with a wrapper

This patch allows for easier manipulation of the cache description register, CCSIDR. Which is helpful for testing as well. Currently, numbers get hard-coded and might be prone to errors.

Therefore, this patch adds a wrapper for different types of CPUs available in tcg to decribe caches. One function make_ccsidr supports two cases by carrying a parameter as FORMAT that can be LEGACY and CCIDX which determines the specification of the register.

For CCSIDR register, 32 bit version follows specification [1]. Conversely, 64 bit version follows specification [2].

Changes from v2 [3] -> v3:

1) add only one function instead of ccsidr32 and ccsidr64
2) use deposit32 and deposit64 in ccsidr function

[1] B4.1.19, ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition, https://developer.arm.com/documentation/ddi0406
[2] D23.2.29, ARM Architecture Reference Manual for A-profile Architecture,
https://developer.arm.com/documentation/ddi0487/latest/
[3] https://lore.kernel.org/qemu-devel/[email protected]/

---
 target/arm/cpu-features.h |  50 ++++++++++++++++++
 target/arm/cpu64.c        |  19 ++++---
 target/arm/tcg/cpu64.c    | 108 +++++++++++++++++++-------------------
 3 files changed, 117 insertions(+), 60 deletions(-)
target/arm/cpu-features.h +50 -0
index c59ca104fe..ef40b0dfdc 100644
@@ -1022,6 +1022,56 @@ static inline bool isar_feature_any_evt(const ARMISARegisters *id)
     return isar_feature_aa64_evt(id) || isar_feature_aa32_evt(id);
 }
 
+typedef enum {
+    CCSIDR_FORMAT_LEGACY,
+    CCSIDR_FORMAT_CCIDX,
+} CCSIDRFormat;
+
+static inline uint64_t make_ccsidr(CCSIDRFormat format, unsigned assoc,
+                                   unsigned linesize, unsigned cachesize,
+                                   uint8_t flags)
+{
+    unsigned lg_linesize = ctz32(linesize);
+    unsigned sets;
+    uint32_t ccsidr32 = 0;
+    uint64_t ccsidr64 = 0;
+
+    assert(assoc != 0);
+    assert(is_power_of_2(linesize));
+    assert(lg_linesize >= 4 && lg_linesize <= 7 + 4);
+
+    /* sets * associativity * linesize == cachesize. */
+    sets = cachesize / (assoc * linesize);
+    assert(cachesize % (assoc * linesize) == 0);
+
+    if (format == CCSIDR_FORMAT_LEGACY) {
+        /*
+         * The 32-bit CCSIDR format is:
+         *   [27:13] number of sets - 1
+         *   [12:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64 bytes, etc
+         */
+        ccsidr32 = deposit32(ccsidr32, 28,  4, flags);
+        ccsidr32 = deposit32(ccsidr32, 13, 15, sets - 1);
+        ccsidr32 = deposit32(ccsidr32,  3, 10, assoc - 1);
+        ccsidr32 = deposit32(ccsidr32,  0,  3, lg_linesize - 4);
+        return (uint64_t)ccsidr32;
+    } else {
+        /*
+         * The 64-bit CCSIDR_EL1 format is:
+         *   [55:32] number of sets - 1
+         *   [23:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64 bytes, etc
+         */
+        ccsidr64 = deposit64(ccsidr64, 32, 24, sets - 1);
+        ccsidr64 = deposit64(ccsidr64,  3, 21, assoc - 1);
+        ccsidr64 = deposit64(ccsidr64,  0,  3, lg_linesize - 4);
+        return ccsidr64;
+    }
+}
+
 /*
  * Forward to the above feature tests given an ARMCPU pointer.
  */
target/arm/cpu64.c +13 -6
index 262a1d6c0b..458d1cee01 100644
@@ -23,6 +23,7 @@
 #include "cpu.h"
 #include "cpregs.h"
 #include "qemu/module.h"
+#include "qemu/units.h"
 #include "sysemu/kvm.h"
 #include "sysemu/hvf.h"
 #include "sysemu/qtest.h"
@@ -642,9 +643,12 @@ static void aarch64_a57_initfn(Object *obj)
     cpu->isar.dbgdevid1 = 0x2;
     cpu->isar.reset_pmcr_el0 = 0x41013000;
     cpu->clidr = 0x0a200023;
-    cpu->ccsidr[0] = 0x701fe00a; /* 32KB L1 dcache */
-    cpu->ccsidr[1] = 0x201fe012; /* 48KB L1 icache */
-    cpu->ccsidr[2] = 0x70ffe07a; /* 2048KB L2 cache */
+    /* 32KB L1 dcache */
+    cpu->ccsidr[0] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7);
+    /* 48KB L1 icache */
+    cpu->ccsidr[1] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 3, 64, 48 * KiB, 2);
+    /* 2048KB L2 cache */
+    cpu->ccsidr[2] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 2 * MiB, 7);
     cpu->dcz_blocksize = 4; /* 64 bytes */
     cpu->gic_num_lrs = 4;
     cpu->gic_vpribits = 5;
@@ -700,9 +704,12 @@ static void aarch64_a53_initfn(Object *obj)
     cpu->isar.dbgdevid1 = 0x1;
     cpu->isar.reset_pmcr_el0 = 0x41033000;
     cpu->clidr = 0x0a200023;
-    cpu->ccsidr[0] = 0x700fe01a; /* 32KB L1 dcache */
-    cpu->ccsidr[1] = 0x201fe00a; /* 32KB L1 icache */
-    cpu->ccsidr[2] = 0x707fe07a; /* 1024KB L2 cache */
+    /* 32KB L1 dcache */
+    cpu->ccsidr[0] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 4, 64, 32 * KiB, 7);
+    /* 32KB L1 icache */
+    cpu->ccsidr[1] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 1, 64, 32 * KiB, 2);
+    /* 1024KB L2 cache */
+    cpu->ccsidr[2] = make_ccsidr(CCSIDR_FORMAT_LEGACY, 16, 64, 1 * MiB, 7);
     cpu->dcz_blocksize = 4; /* 64 bytes */
     cpu->gic_num_lrs = 4;
     cpu->gic_vpribits = 5;
target/arm/tcg/cpu64.c +54 -54

168 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#1 lore raw

Hi Alireza,

Quoted 11 lines · Alireza Sanaee

This patch allows for easier manipulation of the cache description register, CCSIDR. Which is helpful for testing as well. Currently, numbers get hard-coded and might be prone to errors.

Therefore, this patch adds a wrapper for different types of CPUs available in tcg to decribe caches. One function make_ccsidr supports two cases by carrying a parameter as FORMAT that can be LEGACY and CCIDX which determines the specification of the register.

For CCSIDR register, 32 bit version follows specification [1]. Conversely, 64 bit version follows specification [2].

---

[cut]

Quoted 5 lines

Changes from v2 [3] -> v3:

1) add only one function instead of ccsidr32 and ccsidr64
2) use deposit32 and deposit64 in ccsidr function

---

Quoted 4 lines

[1] B4.1.19, ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition, https://developer.arm.com/documentation/ddi0406
[2] D23.2.29, ARM Architecture Reference Manual for A-profile Architecture,
https://developer.arm.com/documentation/ddi0487/latest/

---

[cut]

Quoted 2 lines

[3] https://lore.kernel.org/qemu-devel/[email protected]/

---

Quoted 18 lines
---
  target/arm/cpu-features.h |  50 ++++++++++++++++++
  target/arm/cpu64.c        |  19 ++++---
  target/arm/tcg/cpu64.c    | 108 +++++++++++++++++++-------------------
  3 files changed, 117 insertions(+), 60 deletions(-)
target/arm/cpu-features.h +3 -0
index c59ca104fe..ef40b0dfdc 100644
@@ -1022,6 +1022,56 @@ static inline bool isar_feature_any_evt(const ARMISARegisters *id)
      return isar_feature_aa64_evt(id) || isar_feature_aa32_evt(id);
  }
 
+typedef enum {
+    CCSIDR_FORMAT_LEGACY,
+    CCSIDR_FORMAT_CCIDX,

target/arm/cpu-features.h static inline bool isar_feature_any_evt(const ARMISARegisters *id) · line 1037

Quoted 10 lines
target/arm/cpu-features.h +10 -0
+} CCSIDRFormat;
+
+static inline uint64_t make_ccsidr(CCSIDRFormat format, unsigned assoc,
+                                   unsigned linesize, unsigned cachesize,
+                                   uint8_t flags)
+{
+    unsigned lg_linesize = ctz32(linesize);
+    unsigned sets;
+    uint32_t ccsidr32 = 0;
+    uint64_t ccsidr64 = 0;

deposit32() works with unsigned so you can use 'uint64_t ccsidr' for both cases and return once.

target/arm/cpu-features.h static inline bool isar_feature_any_evt(const ARMISARegisters *id) · line 1040

Quoted 3 lines
target/arm/cpu-features.h +3 -0
+
+    assert(assoc != 0);
+    assert(is_power_of_2(linesize));

As mentioned in v2, if you insist in using an inlined method, you have to include "qemu/host-utils.h" to get is_power_of_2() declaration.

If the inlining is proven problematic later we can still un-inline it, so as this is an useful cleanup (preferably with one 'ccsidr' variable and the include):

Quoted 33 lines
target/arm/cpu-features.h +33 -0
+    assert(lg_linesize >= 4 && lg_linesize <= 7 + 4);
+
+    /* sets * associativity * linesize == cachesize. */
+    sets = cachesize / (assoc * linesize);
+    assert(cachesize % (assoc * linesize) == 0);
+
+    if (format == CCSIDR_FORMAT_LEGACY) {
+        /*
+         * The 32-bit CCSIDR format is:
+         *   [27:13] number of sets - 1
+         *   [12:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64 bytes, etc
+         */
+        ccsidr32 = deposit32(ccsidr32, 28,  4, flags);
+        ccsidr32 = deposit32(ccsidr32, 13, 15, sets - 1);
+        ccsidr32 = deposit32(ccsidr32,  3, 10, assoc - 1);
+        ccsidr32 = deposit32(ccsidr32,  0,  3, lg_linesize - 4);
+        return (uint64_t)ccsidr32;
+    } else {
+        /*
+         * The 64-bit CCSIDR_EL1 format is:
+         *   [55:32] number of sets - 1
+         *   [23:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64 bytes, etc
+         */
+        ccsidr64 = deposit64(ccsidr64, 32, 24, sets - 1);
+        ccsidr64 = deposit64(ccsidr64,  3, 21, assoc - 1);
+        ccsidr64 = deposit64(ccsidr64,  0,  3, lg_linesize - 4);
+        return ccsidr64;
+    }
+}
#2 lore raw
Quoted 79 lines · On Tue, 3 Sep 2024 00:11:04 +0200 Philippe Mathieu-Daudé <[email protected]>…

Hi Alireza,

Quoted 11 lines · Alireza Sanaee

This patch allows for easier manipulation of the cache description register, CCSIDR. Which is helpful for testing as well. Currently, numbers get hard-coded and might be prone to errors.

Therefore, this patch adds a wrapper for different types of CPUs available in tcg to decribe caches. One function make_ccsidr supports two cases by carrying a parameter as FORMAT that can be LEGACY and CCIDX which determines the specification of the register.

For CCSIDR register, 32 bit version follows specification [1]. Conversely, 64 bit version follows specification [2].

---

[cut]

Quoted 5 lines

Changes from v2 [3] -> v3:

1) add only one function instead of ccsidr32 and ccsidr64
2) use deposit32 and deposit64 in ccsidr function

---

Quoted 4 lines

[1] B4.1.19, ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition, https://developer.arm.com/documentation/ddi0406
[2] D23.2.29, ARM Architecture Reference Manual for A-profile Architecture, https://developer.arm.com/documentation/ddi0487/latest/

---

[cut]

Quoted 2 lines

[3] https://lore.kernel.org/qemu-devel/[email protected]/

---

Quoted 25 lines
---
  target/arm/cpu-features.h |  50 ++++++++++++++++++
  target/arm/cpu64.c        |  19 ++++---
  target/arm/tcg/cpu64.c    | 108

+++++++++++++++++++------------------- 3 files changed, 117 insertions(+), 60 deletions(-)

target/arm/cpu-features.h +0 -0
index c59ca104fe..ef40b0dfdc 100644
@@ -1022,6 +1022,56 @@ static inline bool

isar_feature_any_evt(const ARMISARegisters *id) return isar_feature_aa64_evt(id) || isar_feature_aa32_evt(id); }

target/arm/cpu-features.h +6 -0
+typedef enum {
+    CCSIDR_FORMAT_LEGACY,
+    CCSIDR_FORMAT_CCIDX,
+} CCSIDRFormat;
+
+static inline uint64_t make_ccsidr(CCSIDRFormat format, unsigned

assoc,
+ unsigned linesize, unsigned
cachesize,

target/arm/cpu-features.h static inline bool isar_feature_any_evt(const ARMISARegisters *id) · line 1037

Quoted 6 lines
target/arm/cpu-features.h +6 -0
+                                   uint8_t flags)
+{
+    unsigned lg_linesize = ctz32(linesize);
+    unsigned sets;
+    uint32_t ccsidr32 = 0;
+    uint64_t ccsidr64 = 0;  

deposit32() works with unsigned so you can use 'uint64_t ccsidr' for both cases and return once.

target/arm/cpu-features.h static inline bool isar_feature_any_evt(const ARMISARegisters *id) · line 1040

Quoted 3 lines
target/arm/cpu-features.h +3 -0
+
+    assert(assoc != 0);
+    assert(is_power_of_2(linesize));  
Quoted 7 lines

As mentioned in v2, if you insist in using an inlined method, you have to include "qemu/host-utils.h" to get is_power_of_2() declaration.

If the inlining is proven problematic later we can still un-inline it, so as this is an useful cleanup (preferably with one 'ccsidr' variable and the include):

Hi. sure! that makes sense. applied those two changes in v4. sorry for missing out the comment on v2.

Quoted 38 lines
Quoted 35 lines
target/arm/cpu-features.h +13 -0
+    assert(lg_linesize >= 4 && lg_linesize <= 7 + 4);
+
+    /* sets * associativity * linesie == cachesize. */
+    sets = cachesize / (assoc * linesize);
+    assert(cachesize % (assoc * linesize) == 0);
+
+    if (format == CCSIDR_FORMAT_LEGACY) {
+        /*
+         * The 32-bit CCSIDR format is:
+         *   [27:13] number of sets - 1
+         *   [12:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64

bytes, etc

target/arm/cpu-features.h +13 -0
+         */
+        ccsidr32 = deposit32(ccsidr32, 28,  4, flags);
+        ccsidr32 = deposit32(ccsidr32, 13, 15, sets - 1);
+        ccsidr32 = deposit32(ccsidr32,  3, 10, assoc - 1);
+        ccsidr32 = deposit32(ccsidr32,  0,  3, lg_linesize - 4);
+        return (uint64_t)ccsidr32;
+    } else {
+        /*
+         * The 64-bit CCSIDR_EL1 format is:
+         *   [55:32] number of sets - 1
+         *   [23:3]  associativity - 1
+         *   [2:0]   log2(linesize) - 4
+         *           so 0 == 16 bytes, 1 == 32 bytes, 2 == 64

bytes, etc

target/arm/cpu-features.h +7 -0
+         */
+        ccsidr64 = deposit64(ccsidr64, 32, 24, sets - 1);
+        ccsidr64 = deposit64(ccsidr64,  3, 21, assoc - 1);
+        ccsidr64 = deposit64(ccsidr64,  0,  3, lg_linesize - 4);
+        return ccsidr64;
+    }
+}