Skip to content

Commit

Permalink
target/riscv: Add the mcountinhibit CSR
Browse files Browse the repository at this point in the history
1.11 defines mcountinhibit, which has the same numeric CSR value as
mucounteren from 1.09.1 but has different semantics.  This patch enables
the CSR for 1.11-based targets, which is trivial to implement because
the counters in QEMU never tick (legal according to the spec).

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
[Palmer: Fix counter access semantics, change commit message to indicate
the behavior is fully emulated.]
Reviewed-by: Palmer Dabbelt <palmer@sifive.com>
Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
  • Loading branch information
alistair23 authored and palmer-dabbelt committed Jun 25, 2019
1 parent 6729dbb commit 747a43e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions target/riscv/cpu_bits.h
Expand Up @@ -136,6 +136,7 @@
#define CSR_MCOUNTEREN 0x306

/* Legacy Counter Setup (priv v1.9.1) */
/* Update to #define CSR_MCOUNTINHIBIT 0x320 for 1.11.0 */
#define CSR_MUCOUNTEREN 0x320
#define CSR_MSCOUNTEREN 0x321
#define CSR_MHCOUNTEREN 0x322
Expand Down
17 changes: 15 additions & 2 deletions target/riscv/csr.c
Expand Up @@ -56,6 +56,15 @@ static int fs(CPURISCVState *env, int csrno)
static int ctr(CPURISCVState *env, int csrno)
{
#if !defined(CONFIG_USER_ONLY)
/*
* The counters are always enabled on newer priv specs, as the CSR has
* changed from controlling that the counters can be read to controlling
* that the counters increment.
*/
if (env->priv_ver > PRIV_VERSION_1_09_1) {
return 0;
}

uint32_t ctr_en = ~0u;

if (env->priv < PRV_M) {
Expand Down Expand Up @@ -461,18 +470,22 @@ static int write_mcounteren(CPURISCVState *env, int csrno, target_ulong val)
return 0;
}

/* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
static int read_mscounteren(CPURISCVState *env, int csrno, target_ulong *val)
{
if (env->priv_ver > PRIV_VERSION_1_09_1) {
if (env->priv_ver > PRIV_VERSION_1_09_1
&& env->priv_ver < PRIV_VERSION_1_11_0) {
return -1;
}
*val = env->mcounteren;
return 0;
}

/* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
static int write_mscounteren(CPURISCVState *env, int csrno, target_ulong val)
{
if (env->priv_ver > PRIV_VERSION_1_09_1) {
if (env->priv_ver > PRIV_VERSION_1_09_1
&& env->priv_ver < PRIV_VERSION_1_11_0) {
return -1;
}
env->mcounteren = val;
Expand Down

0 comments on commit 747a43e

Please sign in to comment.