From: Atish Patra <atish.patra@wdc.com>
To: qemu-devel@nongnu.org
Cc: qemu-riscv@nongnu.org,
Sagar Karandikar <sagark@eecs.berkeley.edu>,
Bastian Koppelmann <kbastian@mail.uni-paderborn.de>,
anup.patel@wdc.com, Atish Patra <atish.patra@wdc.com>,
Alistair Francis <Alistair.Francis@wdc.com>,
Palmer Dabbelt <palmer@dabbelt.com>
Subject: [ RFC 3/6] target/riscv: Support mcycle/minstret write operation
Date: Fri, 19 Mar 2021 12:45:31 -0700 [thread overview]
Message-ID: <20210319194534.2082397-4-atish.patra@wdc.com> (raw)
In-Reply-To: <20210319194534.2082397-1-atish.patra@wdc.com>
mcycle/minstret are actually WARL registers and can be written with any
given value. With SBI PMU extension, it will be used to store a initial
value provided from supervisor OS. The Qemu also need prohibit the counter
increment if mcountinhibit is set.
Signed-off-by: Atish Patra <atish.patra@wdc.com>
---
target/riscv/cpu.h | 8 +++
target/riscv/csr.c | 111 ++++++++++++++++++++++++++++++++++-------
target/riscv/machine.c | 4 ++
3 files changed, 105 insertions(+), 18 deletions(-)
diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index ef2a7fdc3980..47d6caeb7354 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -216,6 +216,14 @@ struct CPURISCVState {
target_ulong mcountinhibit;
+ /* Snapshot values for mcycle & minstret */
+ target_ulong mcycle_prev;
+ target_ulong minstret_prev;
+
+ /* for RV32 */
+ target_ulong mcycleh_prev;
+ target_ulong minstreth_prev;
+
target_ulong sscratch;
target_ulong mscratch;
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index b9d795389532..61036649b044 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -319,31 +319,106 @@ static int write_vstart(CPURISCVState *env, int csrno, target_ulong val)
}
/* User Timers and Counters */
-static int read_instret(CPURISCVState *env, int csrno, target_ulong *val)
+
+static target_ulong get_icount_ticks(bool brv32)
{
+ int64_t val;
+ target_ulong result;
+
#if !defined(CONFIG_USER_ONLY)
if (icount_enabled()) {
- *val = icount_get();
+ val = icount_get();
} else {
- *val = cpu_get_host_ticks();
+ val = cpu_get_host_ticks();
}
#else
- *val = cpu_get_host_ticks();
+ val = cpu_get_host_ticks();
#endif
+
+ if (brv32) {
+ result = val >> 32;
+ } else {
+ result = val;
+ }
+
+ return result;
+}
+
+static int read_cycle(CPURISCVState *env, int csrno, target_ulong *val)
+{
+ if (get_field(env->mcountinhibit, HCOUNTEREN_CY)) {
+ /**
+ * Counter should not increment if inhibit bit is set. We can't really
+ * stop the icount counting. Just return the previous value to indicate
+ * that counter was not incremented.
+ */
+ *val = env->mcycle_prev;
+ return 0;
+ }
+
+ *val = get_icount_ticks(false);
+
+ if (*val > env->mcycle_prev)
+ *val = *val - env->mcycle_prev + env->mphmcounter_val[0];
+ else
+ /* Overflow scenario */
+ *val = UINT64_MAX - env->mcycle_prev + 1 + env->mphmcounter_val[0] + *val;
+
+ return 0;
+}
+
+static int read_instret(CPURISCVState *env, int csrno, target_ulong *val)
+{
+ if (get_field(env->mcountinhibit, HCOUNTEREN_IR)) {
+ *val = env->minstret_prev;
+ return 0;
+ }
+
+ *val = get_icount_ticks(false);
+
+ if (*val > env->minstret_prev)
+ *val = *val - env->minstret_prev + env->mphmcounter_val[2];
+ else
+ /* Overflow scenario */
+ *val = UINT64_MAX - env->minstret_prev + 1 + env->mphmcounter_val[2] + *val;
+
+ return 0;
+}
+
+static int read_cycleh(CPURISCVState *env, int csrno, target_ulong *val)
+{
+
+ if (get_field(env->mcountinhibit, HCOUNTEREN_CY)) {
+ *val = env->mcycleh_prev;
+ return 0;
+ }
+
+ *val = get_icount_ticks(true);
+
+ if (*val > env->mcycleh_prev)
+ *val = *val - env->mcycleh_prev + env->mphmcounterh_val[0];
+ else
+ /* Overflow scenario */
+ *val = UINT32_MAX - env->mcycleh_prev + 1 + env->mphmcounterh_val[0] + *val;
+
return 0;
}
static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val)
{
-#if !defined(CONFIG_USER_ONLY)
- if (icount_enabled()) {
- *val = icount_get() >> 32;
- } else {
- *val = cpu_get_host_ticks() >> 32;
+ if (get_field(env->mcountinhibit, HCOUNTEREN_IR)) {
+ *val = env->minstreth_prev;
+ return 0;
}
-#else
- *val = cpu_get_host_ticks() >> 32;
-#endif
+
+ *val = get_icount_ticks(true);
+
+ if (*val > env->minstreth_prev)
+ *val = *val - env->minstreth_prev + env->mphmcounterh_val[2];
+ else
+ /* Overflow scenario */
+ *val = UINT32_MAX - env->minstreth_prev + 1 + env->mphmcounterh_val[2] + *val;
+
return 0;
}
@@ -1383,9 +1458,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
[CSR_VL] = { "vl", vs, read_vl },
[CSR_VTYPE] = { "vtype", vs, read_vtype },
/* User Timers and Counters */
- [CSR_CYCLE] = { "cycle", ctr, read_instret },
+ [CSR_CYCLE] = { "cycle", ctr, read_cycle },
[CSR_INSTRET] = { "instret", ctr, read_instret },
- [CSR_CYCLEH] = { "cycleh", ctr32, read_instreth },
+ [CSR_CYCLEH] = { "cycleh", ctr32, read_cycleh },
[CSR_INSTRETH] = { "instreth", ctr32, read_instreth },
/*
@@ -1397,10 +1472,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
#if !defined(CONFIG_USER_ONLY)
/* Machine Timers and Counters */
- [CSR_MCYCLE] = { "mcycle", any, read_instret },
- [CSR_MINSTRET] = { "minstret", any, read_instret },
- [CSR_MCYCLEH] = { "mcycleh", any32, read_instreth },
- [CSR_MINSTRETH] = { "minstreth", any32, read_instreth },
+ [CSR_MCYCLE] = { "mcycle", any, read_cycle , write_mhpmcounter},
+ [CSR_MINSTRET] = { "minstret", any, read_instret, write_mhpmcounter},
+ [CSR_MCYCLEH] = { "mcycleh", any32, read_cycleh , write_mhpmcounterh},
+ [CSR_MINSTRETH] = { "minstreth", any32, read_instreth , write_mhpmcounterh},
/* Machine Information Registers */
[CSR_MVENDORID] = { "mvendorid", any, read_zero },
diff --git a/target/riscv/machine.c b/target/riscv/machine.c
index cb7ec8a4c656..b1410419cc1f 100644
--- a/target/riscv/machine.c
+++ b/target/riscv/machine.c
@@ -178,6 +178,10 @@ const VMStateDescription vmstate_riscv_cpu = {
VMSTATE_UINTTL(env.scounteren, RISCVCPU),
VMSTATE_UINTTL(env.mcounteren, RISCVCPU),
VMSTATE_UINTTL(env.mcountinhibit, RISCVCPU),
+ VMSTATE_UINTTL(env.mcycle_prev, RISCVCPU),
+ VMSTATE_UINTTL(env.mcycleh_prev, RISCVCPU),
+ VMSTATE_UINTTL(env.minstret_prev, RISCVCPU),
+ VMSTATE_UINTTL(env.minstreth_prev, RISCVCPU),
VMSTATE_UINTTL(env.sscratch, RISCVCPU),
VMSTATE_UINTTL(env.mscratch, RISCVCPU),
VMSTATE_UINT64(env.mfromhost, RISCVCPU),
--
2.25.1
next prev parent reply other threads:[~2021-03-19 19:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-19 19:45 [ RFC 0/6] Improve PMU support Atish Patra
2021-03-19 19:45 ` [ RFC 1/6] target/riscv: Remove privilege v1.9 specific CSR related code Atish Patra
2021-03-22 14:53 ` Alistair Francis
2021-03-19 19:45 ` [ RFC 2/6] target/riscv: Implement mcountinhibit CSR Atish Patra
2021-03-31 15:29 ` Alistair Francis
2021-03-19 19:45 ` Atish Patra [this message]
2021-04-07 17:34 ` [ RFC 3/6] target/riscv: Support mcycle/minstret write operation Alistair Francis
2021-03-19 19:45 ` [ RFC 4/6] target/riscv: Add support for hpmcounters/hpmevents Atish Patra
2021-04-07 17:34 ` Alistair Francis
2021-03-19 19:45 ` [ RFC 5/6] hw/riscv: virt: Add PMU device tree node to support SBI PMU extension Atish Patra
2021-03-19 19:45 ` [ RFC 6/6] hw/riscv: virt: DEBUG PATCH to test PMU Atish Patra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210319194534.2082397-4-atish.patra@wdc.com \
--to=atish.patra@wdc.com \
--cc=Alistair.Francis@wdc.com \
--cc=anup.patel@wdc.com \
--cc=kbastian@mail.uni-paderborn.de \
--cc=palmer@dabbelt.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-riscv@nongnu.org \
--cc=sagark@eecs.berkeley.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).