lib: sbi_pmu: remove mhpm_count field in hart feature
authorInochi Amaoto <inochiama@outlook.com>
Fri, 11 Aug 2023 00:24:43 +0000 (08:24 +0800)
committerAnup Patel <anup@brainfault.org>
Tue, 22 Aug 2023 07:56:09 +0000 (13:26 +0530)
After supporting noncontigous hpm event and counters in opensbi, the
number of hpm counters can be calculated by the mhpm_mask. So this field
is unnecessary and can be removed to save some space.

Signed-off-by: Inochi Amaoto <inochiama@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
include/sbi/sbi_bitops.h
include/sbi/sbi_hart.h
lib/sbi/sbi_emulate_csr.c
lib/sbi/sbi_hart.c
lib/sbi/sbi_init.c

index 2e08947beedec060e404cd16159358304cb217a7..48a20901eff0f7fbb0c7d216bd8351404d07251f 100644 (file)
@@ -112,6 +112,22 @@ static inline unsigned long sbi_fls(unsigned long word)
        return num;
 }
 
+/**
+ * sbi_popcount - find the number of set bit in a long word
+ * @word: the word to search
+ */
+static inline unsigned long sbi_popcount(unsigned long word)
+{
+       unsigned long count = 0;
+
+       while (word) {
+               word &= word - 1;
+               count++;
+       }
+
+       return count;
+}
+
 #define for_each_set_bit(bit, addr, size) \
        for ((bit) = find_first_bit((addr), (size));            \
             (bit) < (size);                                    \
index 17d3ada9a5889adc92b69a6a19e8fb65203acb69..e60f41502f61757a80f243fc1bb922266a2d8bd0 100644 (file)
@@ -69,7 +69,6 @@ struct sbi_hart_features {
        unsigned int pmp_count;
        unsigned int pmp_addr_bits;
        unsigned long pmp_gran;
-       unsigned int mhpm_count;
        unsigned int mhpm_mask;
        unsigned int mhpm_bits;
 };
@@ -85,7 +84,6 @@ static inline ulong sbi_hart_expected_trap_addr(void)
        return (ulong)sbi_hart_expected_trap;
 }
 
-unsigned int sbi_hart_mhpm_count(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_mask(struct sbi_scratch *scratch);
 void sbi_hart_delegation_dump(struct sbi_scratch *scratch,
                              const char *prefix, const char *suffix);
index da811653661549b4bed5c73fa0b7104e1bd3635f..5f3b111d69f9d76ac94facfdbdd41e2b6802e1ed 100644 (file)
@@ -109,7 +109,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 
 #define switchcase_hpm(__uref, __mref, __csr)                          \
        case __csr:                                                     \
-               if ((sbi_hart_mhpm_count(scratch) + 3) <= (__csr - __uref))\
+               if (sbi_hart_mhpm_mask(scratch) & (1 << (__csr - __uref)))\
                        return SBI_ENOTSUPP;                            \
                if (!hpm_allowed(__csr - __uref, prev_mode, virt))      \
                        return SBI_ENOTSUPP;                            \
index be795f562b93d9f9a906dc7b2c6a065f414d2976..f7cefe4d9b2205b0f92fda6616fc4d8262cc2c2a 100644 (file)
@@ -253,14 +253,6 @@ unsigned int sbi_hart_mhpm_mask(struct sbi_scratch *scratch)
        return hfeatures->mhpm_mask;
 }
 
-unsigned int sbi_hart_mhpm_count(struct sbi_scratch *scratch)
-{
-       struct sbi_hart_features *hfeatures =
-                       sbi_scratch_offset_ptr(scratch, hart_features_offset);
-
-       return hfeatures->mhpm_count;
-}
-
 unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch)
 {
        struct sbi_hart_features *hfeatures =
@@ -726,31 +718,29 @@ static int hart_detect_features(struct sbi_scratch *scratch)
        /* Clear hart features */
        hfeatures->extensions = 0;
        hfeatures->pmp_count = 0;
-       hfeatures->mhpm_count = 0;
        hfeatures->mhpm_mask = 0;
 
-#define __check_hpm_csr(__csr, __count, __mask)                          \
+#define __check_hpm_csr(__csr, __mask)                                           \
        oldval = csr_read_allowed(__csr, (ulong)&trap);                   \
        if (!trap.cause) {                                                \
                csr_write_allowed(__csr, (ulong)&trap, 1UL);              \
                if (!trap.cause && csr_swap(__csr, oldval) == 1UL) {      \
-                       (hfeatures->__count)++;                           \
                        (hfeatures->__mask) |= 1 << (__csr - CSR_MCYCLE); \
                }                                                         \
        }
 
-#define __check_hpm_csr_2(__csr, __count, __mask)                        \
-       __check_hpm_csr(__csr + 0, __count, __mask)                       \
-       __check_hpm_csr(__csr + 1, __count, __mask)
-#define __check_hpm_csr_4(__csr, __count, __mask)                        \
-       __check_hpm_csr_2(__csr + 0, __count, __mask)                     \
-       __check_hpm_csr_2(__csr + 2, __count, __mask)
-#define __check_hpm_csr_8(__csr, __count, __mask)                        \
-       __check_hpm_csr_4(__csr + 0, __count, __mask)                     \
-       __check_hpm_csr_4(__csr + 4, __count, __mask)
-#define __check_hpm_csr_16(__csr, __count, __mask)                       \
-       __check_hpm_csr_8(__csr + 0, __count, __mask)                     \
-       __check_hpm_csr_8(__csr + 8, __count, __mask)
+#define __check_hpm_csr_2(__csr, __mask)                         \
+       __check_hpm_csr(__csr + 0, __mask)                        \
+       __check_hpm_csr(__csr + 1, __mask)
+#define __check_hpm_csr_4(__csr, __mask)                         \
+       __check_hpm_csr_2(__csr + 0, __mask)                      \
+       __check_hpm_csr_2(__csr + 2, __mask)
+#define __check_hpm_csr_8(__csr, __mask)                         \
+       __check_hpm_csr_4(__csr + 0, __mask)                      \
+       __check_hpm_csr_4(__csr + 4, __mask)
+#define __check_hpm_csr_16(__csr, __mask)                        \
+       __check_hpm_csr_8(__csr + 0, __mask)                      \
+       __check_hpm_csr_8(__csr + 8, __mask)
 
 #define __check_csr(__csr, __rdonly, __wrval, __field, __skip) \
        oldval = csr_read_allowed(__csr, (ulong)&trap);                 \
@@ -803,11 +793,11 @@ static int hart_detect_features(struct sbi_scratch *scratch)
        }
 __pmp_skip:
        /* Detect number of MHPM counters */
-       __check_hpm_csr(CSR_MHPMCOUNTER3, mhpm_count, mhpm_mask);
+       __check_hpm_csr(CSR_MHPMCOUNTER3, mhpm_mask);
        hfeatures->mhpm_bits = hart_mhpm_get_allowed_bits();
-       __check_hpm_csr_4(CSR_MHPMCOUNTER4, mhpm_count, mhpm_mask);
-       __check_hpm_csr_8(CSR_MHPMCOUNTER8, mhpm_count, mhpm_mask);
-       __check_hpm_csr_16(CSR_MHPMCOUNTER16, mhpm_count, mhpm_mask);
+       __check_hpm_csr_4(CSR_MHPMCOUNTER4, mhpm_mask);
+       __check_hpm_csr_8(CSR_MHPMCOUNTER8, mhpm_mask);
+       __check_hpm_csr_16(CSR_MHPMCOUNTER16, mhpm_mask);
 
        /**
         * No need to check for MHPMCOUNTERH for RV32 as they are expected to be
@@ -890,7 +880,7 @@ __pmp_skip:
                return rc;
 
        /* Extensions implied by other extensions and features */
-       if (hfeatures->mhpm_count)
+       if (hfeatures->mhpm_mask)
                __sbi_hart_update_extension(hfeatures,
                                        SBI_HART_EXT_ZIHPM, true);
 
index eae4f28d8a4f3871dc1ee5390532172a64945f23..252b41ace41e3c051e0746d67404a5158e63ce8d 100644 (file)
@@ -180,9 +180,8 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
                   sbi_hart_pmp_granularity(scratch));
        sbi_printf("Boot HART PMP Address Bits: %d\n",
                   sbi_hart_pmp_addrbits(scratch));
-       sbi_printf("Boot HART MHPM Count      : %d\n",
-                  sbi_hart_mhpm_count(scratch));
-       sbi_printf("Boot HART MHPM Mask       : 0x%x\n",
+       sbi_printf("Boot HART MHPM Info       : %lu (0x%08x)\n",
+                  sbi_popcount(sbi_hart_mhpm_mask(scratch)),
                   sbi_hart_mhpm_mask(scratch));
        sbi_hart_delegation_dump(scratch, "Boot HART ", "         ");
 }