Skip to content

Commit

Permalink
target/riscv: Add "pmu-mask" property to replace "pmu-num"
Browse files Browse the repository at this point in the history
Using a mask instead of the number of PMU devices supports the accurate
emulation of platforms that have a discontinuous set of PMU counters.

The "pmu-num" property now generates a warning when used by the user on
the command line.

Rather than storing the value for "pmu-num" convert it directly to the
mask if it is specified (overwriting the default "pmu-mask" value)
likewise the value is calculated from the mask if the property value is
obtained.

In the unusual situation that both "pmu-mask" and "pmu-num" are provided
then then the order on the command line determines which takes
precedence (later overwriting earlier.)

Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-ID: <20231031154000.18134-5-rbradford@rivosinc.com>
[Changes by AF
 - Fixup ext_zihpm logic after rebase
]
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
  • Loading branch information
rbradford authored and alistair23 committed Nov 7, 2023
1 parent 2571a64 commit 69b3849
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
40 changes: 39 additions & 1 deletion target/riscv/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,46 @@ const RISCVCPUMultiExtConfig riscv_cpu_deprecated_exts[] = {
DEFINE_PROP_END_OF_LIST(),
};

static void prop_pmu_num_set(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
RISCVCPU *cpu = RISCV_CPU(obj);
uint8_t pmu_num;

visit_type_uint8(v, name, &pmu_num, errp);

if (pmu_num > (RV_MAX_MHPMCOUNTERS - 3)) {
error_setg(errp, "Number of counters exceeds maximum available");
return;
}

if (pmu_num == 0) {
cpu->cfg.pmu_mask = 0;
} else {
cpu->cfg.pmu_mask = MAKE_64BIT_MASK(3, pmu_num);
}

warn_report("\"pmu-num\" property is deprecated; use \"pmu-mask\"");
}

static void prop_pmu_num_get(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
RISCVCPU *cpu = RISCV_CPU(obj);
uint8_t pmu_num = ctpop32(cpu->cfg.pmu_mask);

visit_type_uint8(v, name, &pmu_num, errp);
}

const PropertyInfo prop_pmu_num = {
.name = "pmu-num",
.get = prop_pmu_num_get,
.set = prop_pmu_num_set,
};

Property riscv_cpu_options[] = {
DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
DEFINE_PROP_UINT32("pmu-mask", RISCVCPU, cfg.pmu_mask, MAKE_64BIT_MASK(3, 16)),
{.name = "pmu-num", .info = &prop_pmu_num}, /* Deprecated */

DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/cpu_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct RISCVCPUConfig {
bool ext_xtheadsync;
bool ext_XVentanaCondOps;

uint8_t pmu_num;
uint32_t pmu_mask;
char *priv_spec;
char *user_spec;
char *bext_spec;
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static bool pmu_needed(void *opaque)
{
RISCVCPU *cpu = opaque;

return cpu->cfg.pmu_num;
return (cpu->cfg.pmu_mask > 0);
}

static const VMStateDescription vmstate_pmu_ctr_state = {
Expand Down
15 changes: 8 additions & 7 deletions target/riscv/pmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

#include "qemu/osdep.h"
#include "qemu/log.h"
#include "qemu/error-report.h"
#include "cpu.h"
#include "pmu.h"
#include "sysemu/cpu-timers.h"
#include "sysemu/device_tree.h"

#define RISCV_TIMEBASE_FREQ 1000000000 /* 1Ghz */
#define MAKE_32BIT_MASK(shift, length) \
(((uint32_t)(~0UL) >> (32 - (length))) << (shift))

/*
* To keep it simple, any event can be mapped to any programmable counters in
Expand Down Expand Up @@ -184,7 +183,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)
CPURISCVState *env = &cpu->env;
gpointer value;

if (!cpu->cfg.pmu_num) {
if (!cpu->cfg.pmu_mask) {
return 0;
}
value = g_hash_table_lookup(cpu->pmu_event_ctr_map,
Expand Down Expand Up @@ -432,9 +431,12 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)

void riscv_pmu_init(RISCVCPU *cpu, Error **errp)
{
uint8_t pmu_num = cpu->cfg.pmu_num;
if (cpu->cfg.pmu_mask & (COUNTEREN_CY | COUNTEREN_TM | COUNTEREN_IR)) {
error_setg(errp, "\"pmu-mask\" contains invalid bits (0-2) set");
return;
}

if (pmu_num > (RV_MAX_MHPMCOUNTERS - 3)) {
if (ctpop32(cpu->cfg.pmu_mask) > (RV_MAX_MHPMCOUNTERS - 3)) {
error_setg(errp, "Number of counters exceeds maximum available");
return;
}
Expand All @@ -445,6 +447,5 @@ void riscv_pmu_init(RISCVCPU *cpu, Error **errp)
return;
}

/* Create a bitmask of available programmable counters */
cpu->pmu_avail_ctrs = MAKE_32BIT_MASK(3, pmu_num);
cpu->pmu_avail_ctrs = cpu->cfg.pmu_mask;
}
4 changes: 2 additions & 2 deletions target/riscv/tcg/tcg-cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
}

if (!cpu->cfg.ext_zihpm) {
cpu->cfg.pmu_num = 0;
cpu->cfg.pmu_mask = 0;
cpu->pmu_avail_ctrs = 0;
}

Expand Down Expand Up @@ -688,7 +688,7 @@ static bool tcg_cpu_realize(CPUState *cs, Error **errp)
riscv_timer_init(cpu);
}

if (cpu->cfg.pmu_num) {
if (cpu->cfg.pmu_mask) {
riscv_pmu_init(cpu, &local_err);
if (local_err != NULL) {
error_propagate(errp, local_err);
Expand Down

0 comments on commit 69b3849

Please sign in to comment.