Skip to contentSkip to conversation

lib: sbi_hart: Reset mhpmcounter after probing its bits

PATCH Applied Evgenii Prokopiev 2 participants 2 messages Sep 10 – Oct 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi_hart: Reset mhpmcounter after probing its bits

Writting mhpmcounter3 (and 3H) to determine the allowed bit width has a side effect: the counter starts counting.

Explicitly clear the counter right after the probe so the probing writes do not leave any persistent hardware side effect.

---
 lib/sbi/sbi_hart.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_hart.c +2 -0
index bee88557..a413bfe0 100644
@@ -448,6 +448,7 @@ static int hart_mhpm_get_allowed_bits(void)
 	csr_write_allowed(CSR_MHPMCOUNTER3, &trap, val);
 	if (!trap.cause) {
 		val = csr_read_allowed(CSR_MHPMCOUNTER3, &trap);
+		csr_write(CSR_MHPMCOUNTER3, 0);
 		if (trap.cause)
 			return 0;
 	}
@@ -456,6 +457,7 @@ static int hart_mhpm_get_allowed_bits(void)
 	csr_write_allowed(CSR_MHPMCOUNTER3H, &trap, val);
 	if (!trap.cause) {
 		val = csr_read_allowed(CSR_MHPMCOUNTER3H, &trap);
+		csr_write(CSR_MHPMCOUNTER3H, 0);
 		if (trap.cause)
 			return num_bits;
 	}
Signature · 7 lines
2.50.1 (Apple Git-155)


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 3 lines · Evgenii Prokopiev

Writting mhpmcounter3 (and 3H) to determine the allowed bit width has a side effect: the counter starts counting.

The counter will not count if mhpmevent3 is set to zero and currently we are not setting any particular value set in mhpmevent3 so I am okay with this change.

Quoted 5 lines

Explicitly clear the counter right after the probe so the probing writes do not leave any persistent hardware side effect.

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 32 lines
---
 lib/sbi/sbi_hart.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_hart.c +2 -1
index bee88557..a413bfe0 100644
@@ -448,6 +448,7 @@ static int hart_mhpm_get_allowed_bits(void)
        csr_write_allowed(CSR_MHPMCOUNTER3, &trap, val);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_MHPMCOUNTER3, &trap);
+               csr_write(CSR_MHPMCOUNTER3, 0);
                if (trap.cause)
                        return 0;
        }
@@ -456,6 +457,7 @@ static int hart_mhpm_get_allowed_bits(void)
        csr_write_allowed(CSR_MHPMCOUNTER3H, &trap, val);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_MHPMCOUNTER3H, &trap);
+               csr_write(CSR_MHPMCOUNTER3H, 0);
                if (trap.cause)
                        return num_bits;
        }
--

2.50.1 (Apple Git-155)

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi