From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EBDE5C433EF for ; Fri, 13 May 2022 06:36:49 +0000 (UTC) Received: from localhost ([::1]:60108 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1npOuu-00030v-UX for qemu-devel@archiver.kernel.org; Fri, 13 May 2022 02:36:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43246) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npOo3-0007WB-9W for qemu-devel@nongnu.org; Fri, 13 May 2022 02:29:47 -0400 Received: from mail-vs1-xe32.google.com ([2607:f8b0:4864:20::e32]:46739) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1npOnx-0000aR-Tc for qemu-devel@nongnu.org; Fri, 13 May 2022 02:29:42 -0400 Received: by mail-vs1-xe32.google.com with SMTP id z144so7392272vsz.13 for ; Thu, 12 May 2022 23:29:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wIwbdoaKg2C5ucR1fbYgbaEK0c36WPrwQQX15xS/hP0=; b=YsR/UyToreK08OkWgOBZqZL9m1+oY16oZl43TODNH/2FS0aLNZp6i3Atyr1TJ6BUqy Wz9Uh1pIrmP3FezXRTo2lWyDss9/8B5BN97LT/h2Vnw6LwzbioaYTQmtY1RzSKqoj08S Q0OazpaimnOQVOSiEOtLbYm2oZAVhWruQhXFe2nMw107kCgb8XKfhB7aBtpgu8BRcoHp pgbRCK5DD9j9OiUnZE7P+nYMxZbx8TPYnSkBXhodqS17Mh2zZ0u9jpl/jFEE2vd5kcnt 7MQ6nx5MtsoIoXWQVtGjete/1FXRFbkn/zkHmcYoaOsoTmi4Z6Mp4ex1cQgAzge4vLEd JqQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wIwbdoaKg2C5ucR1fbYgbaEK0c36WPrwQQX15xS/hP0=; b=xQ3XP6GdMz53wFWKMKJZHlN0aGNPZsSiYfrw2e/wETS42vdyXauuDI3DcErGUrYwGw eq5aCjVCz7UqsulQzpXkahwEfTCl2ODIHPaAr/hzCAJiw5Xs57UV+ME7kQA1iHI+XIuz LL/6WyfEqZc0CnBBRRg+gz4jlKOnsjOMU93uN0ESgH/y+w3PzePqCjOCpeXmAP3OiqDv wFQBNdny1nz1p3SFPQMAqxefThqcaqneX2fs2aMtfDm/poN+9PguO5vdvu5nFSj2fMsf jT5OrVl0l/6z95cNBsO+SpvbyWVmvjLBw/UNLy5LgNSNotNxfCDWbwEyj4zqxvI8jwCL dIUA== X-Gm-Message-State: AOAM532g7KaLaz7BBJh8Ul5LACkvsmUZrB2m1SeYqG5g5DoA/hBJpLez nj8ZK6gttNxm1L8ZglIvm7eenn/HbwYbqzh/ X-Google-Smtp-Source: ABdhPJzxBDlpjGuxnoL3v+JbXgCdrY1SfyM1DL9oTCqszZ6mXgmdpVqv3219V1Kw7iUMHA0E5KuoSQ== X-Received: by 2002:a67:d98a:0:b0:32d:6bc7:f78 with SMTP id u10-20020a67d98a000000b0032d6bc70f78mr1745193vsj.19.1652423374641; Thu, 12 May 2022 23:29:34 -0700 (PDT) Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com. [209.85.221.176]) by smtp.gmail.com with ESMTPSA id p6-20020ab01546000000b003626f894df0sm258267uae.30.2022.05.12.23.29.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 May 2022 23:29:33 -0700 (PDT) Received: by mail-vk1-f176.google.com with SMTP id s68so3745137vke.6; Thu, 12 May 2022 23:29:32 -0700 (PDT) X-Received: by 2002:a05:6122:d09:b0:351:f87b:b27a with SMTP id az9-20020a0561220d0900b00351f87bb27amr1542417vkb.23.1652423372532; Thu, 12 May 2022 23:29:32 -0700 (PDT) MIME-Version: 1.0 References: <20220511215956.2351243-1-atishp@rivosinc.com> <20220511215956.2351243-8-atishp@rivosinc.com> In-Reply-To: <20220511215956.2351243-8-atishp@rivosinc.com> From: Frank Chang Date: Fri, 13 May 2022 14:29:21 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v8 07/12] target/riscv: Support mcycle/minstret write operation To: Atish Patra Cc: linux-kernel@vger.kernel.org, Alistair Francis , Bin Meng , Palmer Dabbelt , "qemu-devel@nongnu.org Developers" , "open list:RISC-V" Content-Type: multipart/alternative; boundary="00000000000023709705dedecc07" Received-SPF: pass client-ip=2607:f8b0:4864:20::e32; envelope-from=frank.chang@sifive.com; helo=mail-vs1-xe32.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --00000000000023709705dedecc07 Content-Type: text/plain; charset="UTF-8" On Thu, May 12, 2022 at 6:01 AM Atish Patra wrote: > From: Atish Patra > > 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. > > Support mcycle/minstret through generic counter infrastructure. > > Reviewed-by: Alistair Francis > Signed-off-by: Atish Patra > Signed-off-by: Atish Patra > --- > target/riscv/cpu.h | 23 ++++-- > target/riscv/csr.c | 157 ++++++++++++++++++++++++++++----------- > target/riscv/machine.c | 25 ++++++- > target/riscv/meson.build | 3 +- > target/riscv/pmu.c | 32 ++++++++ > target/riscv/pmu.h | 28 +++++++ > 6 files changed, 214 insertions(+), 54 deletions(-) > create mode 100644 target/riscv/pmu.c > create mode 100644 target/riscv/pmu.h > > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h > index 32cdd9070be5..f60072e0fd3d 100644 > --- a/target/riscv/cpu.h > +++ b/target/riscv/cpu.h > @@ -111,7 +111,7 @@ typedef struct CPUArchState CPURISCVState; > #endif > > #define RV_VLEN_MAX 1024 > -#define RV_MAX_MHPMEVENTS 29 > +#define RV_MAX_MHPMEVENTS 32 > #define RV_MAX_MHPMCOUNTERS 32 > > FIELD(VTYPE, VLMUL, 0, 3) > @@ -121,6 +121,18 @@ FIELD(VTYPE, VMA, 7, 1) > FIELD(VTYPE, VEDIV, 8, 2) > FIELD(VTYPE, RESERVED, 10, sizeof(target_ulong) * 8 - 11) > > +typedef struct PMUCTRState { > + /* Current value of a counter */ > + target_ulong mhpmcounter_val; > + /* Current value of a counter in RV32*/ > + target_ulong mhpmcounterh_val; > + /* Snapshot values of counter */ > + target_ulong mhpmcounter_prev; > + /* Snapshort value of a counter in RV32 */ > + target_ulong mhpmcounterh_prev; > + bool started; > +} PMUCTRState; > + > struct CPUArchState { > target_ulong gpr[32]; > target_ulong gprh[32]; /* 64 top bits of the 128-bit registers */ > @@ -273,13 +285,10 @@ struct CPUArchState { > > target_ulong mcountinhibit; > > - /* PMU counter configured values */ > - target_ulong mhpmcounter_val[RV_MAX_MHPMCOUNTERS]; > - > - /* for RV32 */ > - target_ulong mhpmcounterh_val[RV_MAX_MHPMCOUNTERS]; > + /* PMU counter state */ > + PMUCTRState pmu_ctrs[RV_MAX_MHPMCOUNTERS]; > > - /* PMU event selector configured values */ > + /* PMU event selector configured values. First three are unused*/ > target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS]; > > target_ulong sscratch; > diff --git a/target/riscv/csr.c b/target/riscv/csr.c > index 87aa601e5ddb..c050ed2e2c1b 100644 > --- a/target/riscv/csr.c > +++ b/target/riscv/csr.c > @@ -21,6 +21,7 @@ > #include "qemu/log.h" > #include "qemu/timer.h" > #include "cpu.h" > +#include "pmu.h" > #include "qemu/main-loop.h" > #include "exec/exec-all.h" > #include "sysemu/cpu-timers.h" > @@ -597,34 +598,28 @@ static int write_vcsr(CPURISCVState *env, int csrno, > target_ulong val) > } > > /* User Timers and Counters */ > -static RISCVException read_instret(CPURISCVState *env, int csrno, > - target_ulong *val) > +static target_ulong get_ticks(bool shift) > { > + 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 > - return RISCV_EXCP_NONE; > -} > > -static RISCVException read_instreth(CPURISCVState *env, int csrno, > - target_ulong *val) > -{ > -#if !defined(CONFIG_USER_ONLY) > - if (icount_enabled()) { > - *val = icount_get() >> 32; > + if (shift) { > + result = val >> 32; > } else { > - *val = cpu_get_host_ticks() >> 32; > + result = val; > } > -#else > - *val = cpu_get_host_ticks() >> 32; > -#endif > - return RISCV_EXCP_NONE; > + > + return result; > } > > #if defined(CONFIG_USER_ONLY) > @@ -642,11 +637,23 @@ static RISCVException read_timeh(CPURISCVState *env, > int csrno, > return RISCV_EXCP_NONE; > } > > +static int read_hpmcounter(CPURISCVState *env, int csrno, target_ulong > *val) > +{ > + *val = get_ticks(false); > + return RISCV_EXCP_NONE; > +} > + > +static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong > *val) > +{ > + *val = get_ticks(true); > + return RISCV_EXCP_NONE; > +} > + > #else /* CONFIG_USER_ONLY */ > > static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong > *val) > { > - int evt_index = csrno - CSR_MHPMEVENT3; > + int evt_index = csrno - CSR_MCOUNTINHIBIT; > > *val = env->mhpmevent_val[evt_index]; > > @@ -655,7 +662,7 @@ static int read_mhpmevent(CPURISCVState *env, int > csrno, target_ulong *val) > > static int write_mhpmevent(CPURISCVState *env, int csrno, target_ulong > val) > { > - int evt_index = csrno - CSR_MHPMEVENT3; > + int evt_index = csrno - CSR_MCOUNTINHIBIT; > > env->mhpmevent_val[evt_index] = val; > > @@ -664,52 +671,102 @@ static int write_mhpmevent(CPURISCVState *env, int > csrno, target_ulong val) > > static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong > val) > { > - int ctr_index = csrno - CSR_MHPMCOUNTER3 + 3; > + int ctr_idx = csrno - CSR_MCYCLE; > + PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; > > - env->mhpmcounter_val[ctr_index] = val; > + counter->mhpmcounter_val = val; > + if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || > + riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { > + counter->mhpmcounter_prev = get_ticks(false); > + } else { > + /* Other counters can keep incrementing from the given value */ > + counter->mhpmcounter_prev = val; > + } > > return RISCV_EXCP_NONE; > } > > static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong > val) > { > - int ctr_index = csrno - CSR_MHPMCOUNTER3H + 3; > + int ctr_idx = csrno - CSR_MCYCLEH; > + PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; > > - env->mhpmcounterh_val[ctr_index] = val; > + counter->mhpmcounterh_val = val; > + if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || > + riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { > + counter->mhpmcounterh_prev = get_ticks(true); > + } else { > + counter->mhpmcounterh_prev = val; > + } > + > + return RISCV_EXCP_NONE; > +} > + > +static RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong > *val, > + bool upper_half, uint32_t > ctr_idx) > +{ > + PMUCTRState counter = env->pmu_ctrs[ctr_idx]; > + target_ulong ctr_prev = upper_half ? counter.mhpmcounterh_prev : > + counter.mhpmcounter_prev; > + target_ulong ctr_val = upper_half ? counter.mhpmcounterh_val : > + counter.mhpmcounter_val; > + > + if (get_field(env->mcountinhibit, BIT(ctr_idx))) { > + /** > + * Counter should not increment if inhibit bit is set. We can't > really > + * stop the icount counting. Just return the counter value > written by > + * the supervisor to indicate that counter was not incremented. > + */ > + if (!counter.started) { > + *val = ctr_val; > + return RISCV_EXCP_NONE; > + } else { > + /* Mark that the counter has been stopped */ > + counter.started = false; > + } > + } > + > + > + /** > + * The kernel computes the perf delta by subtracting the current > value from > + * the value it initialized previously (ctr_val). > + */ > + if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || > + riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { > + *val = get_ticks(upper_half) - ctr_prev + ctr_val; > + } else { > + *val = ctr_val; > + } > > return RISCV_EXCP_NONE; > } > > static int read_hpmcounter(CPURISCVState *env, int csrno, target_ulong > *val) > { > - int ctr_index; > + uint16_t ctr_index; > > if (env->priv == PRV_M) { > - ctr_index = csrno - CSR_MHPMCOUNTER3 + 3; > + ctr_index = csrno - CSR_MCYCLE; } else { > - ctr_index = csrno - CSR_HPMCOUNTER3 + 3; > + ctr_index = csrno - CSR_CYCLE; > } > Hi Atish, According to spec: "The RDCYCLE pseudoinstruction reads the low XLEN bits of the cycle CSR which holds a count of the number of clock cycles executed by the processor core on which the hart is running from an arbitrary start time in the past." However, the counter index calculation here would have the issue. For example, if RDCYCLE instruction is executed in M-mode: CYCLE csrno is: 0xc00, but MCYCLE csrno is: 0xb00 "csrno - CSR_MCYCLE" would result in the invalidate value: 0x100, which is out of the counter array's bound. Will it be easier to just check against csrno instead of the current privileged mode? And I think the same issue also occurs for: HPMCOUNTER3 ~ HPMCOUNTER31. Regards, Frank Chang > - *val = env->mhpmcounter_val[ctr_index]; > > - return RISCV_EXCP_NONE; > + return riscv_pmu_read_ctr(env, val, false, ctr_index); > } > > static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong > *val) > { > - int ctr_index; > + uint16_t ctr_index; > > if (env->priv == PRV_M) { > - ctr_index = csrno - CSR_MHPMCOUNTER3H + 3; > + ctr_index = csrno - CSR_MCYCLEH; > } else { > - ctr_index = csrno - CSR_HPMCOUNTER3H + 3; > + ctr_index = csrno - CSR_CYCLEH; > } > > - *val = env->mhpmcounterh_val[ctr_index]; > - > - return RISCV_EXCP_NONE; > + return riscv_pmu_read_ctr(env, val, true, ctr_index); > } > > - > static RISCVException read_time(CPURISCVState *env, int csrno, > target_ulong *val) > { > @@ -1564,11 +1621,23 @@ static RISCVException > read_mcountinhibit(CPURISCVState *env, int csrno, > static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno, > target_ulong val) > { > + int cidx; > + PMUCTRState *counter; > + > if (env->priv_ver < PRIV_VERSION_1_11_0) { > return RISCV_EXCP_ILLEGAL_INST; > } > > env->mcountinhibit = val; > + > + /* Check if any other counter is also monitoring cycles/instructions > */ > + for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) { > + if (!get_field(env->mcountinhibit, BIT(cidx))) { > + counter = &env->pmu_ctrs[cidx]; > + counter->started = true; > + } > + } > + > return RISCV_EXCP_NONE; > } > > @@ -3526,10 +3595,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { > [CSR_VLENB] = { "vlenb", vs, read_vlenb, > .min_priv_ver = > PRIV_VERSION_1_12_0 }, > /* User Timers and Counters */ > - [CSR_CYCLE] = { "cycle", ctr, read_instret }, > - [CSR_INSTRET] = { "instret", ctr, read_instret }, > - [CSR_CYCLEH] = { "cycleh", ctr32, read_instreth }, > - [CSR_INSTRETH] = { "instreth", ctr32, read_instreth }, > + [CSR_CYCLE] = { "cycle", ctr, read_hpmcounter }, > + [CSR_INSTRET] = { "instret", ctr, read_hpmcounter }, > + [CSR_CYCLEH] = { "cycleh", ctr32, read_hpmcounterh }, > + [CSR_INSTRETH] = { "instreth", ctr32, read_hpmcounterh }, > > /* > * In privileged mode, the monitor will have to emulate TIME CSRs > only if > @@ -3543,10 +3612,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_hpmcounter, > write_mhpmcounter}, > + [CSR_MINSTRET] = { "minstret", any, read_hpmcounter, > write_mhpmcounter}, > + [CSR_MCYCLEH] = { "mcycleh", any32, read_hpmcounterh, > write_mhpmcounterh}, > + [CSR_MINSTRETH] = { "minstreth", any32, read_hpmcounterh, > write_mhpmcounterh}, > > /* Machine Information Registers */ > [CSR_MVENDORID] = { "mvendorid", any, read_mvendorid }, > diff --git a/target/riscv/machine.c b/target/riscv/machine.c > index 99193c85bb97..dc182ca81119 100644 > --- a/target/riscv/machine.c > +++ b/target/riscv/machine.c > @@ -279,7 +279,28 @@ static const VMStateDescription vmstate_envcfg = { > VMSTATE_UINT64(env.menvcfg, RISCVCPU), > VMSTATE_UINTTL(env.senvcfg, RISCVCPU), > VMSTATE_UINT64(env.henvcfg, RISCVCPU), > + VMSTATE_END_OF_LIST() > + } > +}; > + > +static bool pmu_needed(void *opaque) > +{ > + RISCVCPU *cpu = opaque; > > + return cpu->cfg.pmu_num; > +} > + > +static const VMStateDescription vmstate_pmu_ctr_state = { > + .name = "cpu/pmu", > + .version_id = 1, > + .minimum_version_id = 1, > + .needed = pmu_needed, > + .fields = (VMStateField[]) { > + VMSTATE_UINTTL(mhpmcounter_val, PMUCTRState), > + VMSTATE_UINTTL(mhpmcounterh_val, PMUCTRState), > + VMSTATE_UINTTL(mhpmcounter_prev, PMUCTRState), > + VMSTATE_UINTTL(mhpmcounterh_prev, PMUCTRState), > + VMSTATE_BOOL(started, PMUCTRState), > VMSTATE_END_OF_LIST() > } > }; > @@ -331,8 +352,8 @@ const VMStateDescription vmstate_riscv_cpu = { > VMSTATE_UINTTL(env.scounteren, RISCVCPU), > VMSTATE_UINTTL(env.mcounteren, RISCVCPU), > VMSTATE_UINTTL(env.mcountinhibit, RISCVCPU), > - VMSTATE_UINTTL_ARRAY(env.mhpmcounter_val, RISCVCPU, > RV_MAX_MHPMCOUNTERS), > - VMSTATE_UINTTL_ARRAY(env.mhpmcounterh_val, RISCVCPU, > RV_MAX_MHPMCOUNTERS), > + VMSTATE_STRUCT_ARRAY(env.pmu_ctrs, RISCVCPU, RV_MAX_MHPMCOUNTERS, > 0, > + vmstate_pmu_ctr_state, PMUCTRState), > VMSTATE_UINTTL_ARRAY(env.mhpmevent_val, RISCVCPU, > RV_MAX_MHPMEVENTS), > VMSTATE_UINTTL(env.sscratch, RISCVCPU), > VMSTATE_UINTTL(env.mscratch, RISCVCPU), > diff --git a/target/riscv/meson.build b/target/riscv/meson.build > index 096249f3a30f..2c1975e72c4e 100644 > --- a/target/riscv/meson.build > +++ b/target/riscv/meson.build > @@ -30,7 +30,8 @@ riscv_softmmu_ss.add(files( > 'pmp.c', > 'debug.c', > 'monitor.c', > - 'machine.c' > + 'machine.c', > + 'pmu.c' > )) > > target_arch += {'riscv': riscv_ss} > diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c > new file mode 100644 > index 000000000000..000fe8da45ef > --- /dev/null > +++ b/target/riscv/pmu.c > @@ -0,0 +1,32 @@ > +/* > + * RISC-V PMU file. > + * > + * Copyright (c) 2021 Western Digital Corporation or its affiliates. > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms and conditions of the GNU General Public License, > + * version 2 or later, as published by the Free Software Foundation. > + * > + * This program is distributed in the hope it will be useful, but WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License > for > + * more details. > + * > + * You should have received a copy of the GNU General Public License > along with > + * this program. If not, see . > + */ > + > +#include "qemu/osdep.h" > +#include "cpu.h" > +#include "pmu.h" > + > +bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, > + uint32_t target_ctr) > +{ > + return (target_ctr == 0) ? true : false; > +} > + > +bool riscv_pmu_ctr_monitor_cycles(CPURISCVState *env, uint32_t target_ctr) > +{ > + return (target_ctr == 2) ? true : false; > +} > diff --git a/target/riscv/pmu.h b/target/riscv/pmu.h > new file mode 100644 > index 000000000000..58a5bc3a4089 > --- /dev/null > +++ b/target/riscv/pmu.h > @@ -0,0 +1,28 @@ > +/* > + * RISC-V PMU header file. > + * > + * Copyright (c) 2021 Western Digital Corporation or its affiliates. > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms and conditions of the GNU General Public License, > + * version 2 or later, as published by the Free Software Foundation. > + * > + * This program is distributed in the hope it will be useful, but WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License > for > + * more details. > + * > + * You should have received a copy of the GNU General Public License > along with > + * this program. If not, see . > + */ > + > +#include "qemu/osdep.h" > +#include "qemu/log.h" > +#include "cpu.h" > +#include "qemu/main-loop.h" > +#include "exec/exec-all.h" > + > +bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, > + uint32_t target_ctr); > +bool riscv_pmu_ctr_monitor_cycles(CPURISCVState *env, > + uint32_t target_ctr); > -- > 2.25.1 > > > --00000000000023709705dedecc07 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, May 12, 2022 at 6:01 AM Atish Pat= ra <atishp@rivosinc.com> w= rote:
From: Atish Patra <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<= br> increment if mcountinhibit is set.

Support mcycle/minstret through generic counter infrastructure.

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Atish Patra <atish.patra@wdc.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
=C2=A0target/riscv/cpu.h=C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 23 ++++--
=C2=A0target/riscv/csr.c=C2=A0 =C2=A0 =C2=A0 =C2=A0| 157 ++++++++++++++++++= ++++++++++-----------
=C2=A0target/riscv/machine.c=C2=A0 =C2=A0|=C2=A0 25 ++++++-
=C2=A0target/riscv/meson.build |=C2=A0 =C2=A03 +-
=C2=A0target/riscv/pmu.c=C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 32 ++++++++
=C2=A0target/riscv/pmu.h=C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 28 +++++++
=C2=A06 files changed, 214 insertions(+), 54 deletions(-)
=C2=A0create mode 100644 target/riscv/pmu.c
=C2=A0create mode 100644 target/riscv/pmu.h

diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index 32cdd9070be5..f60072e0fd3d 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -111,7 +111,7 @@ typedef struct CPUArchState CPURISCVState;
=C2=A0#endif

=C2=A0#define RV_VLEN_MAX 1024
-#define RV_MAX_MHPMEVENTS 29
+#define RV_MAX_MHPMEVENTS 32
=C2=A0#define RV_MAX_MHPMCOUNTERS 32

=C2=A0FIELD(VTYPE, VLMUL, 0, 3)
@@ -121,6 +121,18 @@ FIELD(VTYPE, VMA, 7, 1)
=C2=A0FIELD(VTYPE, VEDIV, 8, 2)
=C2=A0FIELD(VTYPE, RESERVED, 10, sizeof(target_ulong) * 8 - 11)

+typedef struct PMUCTRState {
+=C2=A0 =C2=A0 /* Current value of a counter */
+=C2=A0 =C2=A0 target_ulong mhpmcounter_val;
+=C2=A0 =C2=A0 /* Current value of a counter in RV32*/
+=C2=A0 =C2=A0 target_ulong mhpmcounterh_val;
+=C2=A0 =C2=A0 /* Snapshot values of counter */
+=C2=A0 =C2=A0 target_ulong mhpmcounter_prev;
+=C2=A0 =C2=A0 /* Snapshort value of a counter in RV32 */
+=C2=A0 =C2=A0 target_ulong mhpmcounterh_prev;
+=C2=A0 =C2=A0 bool started;
+} PMUCTRState;
+
=C2=A0struct CPUArchState {
=C2=A0 =C2=A0 =C2=A0target_ulong gpr[32];
=C2=A0 =C2=A0 =C2=A0target_ulong gprh[32]; /* 64 top bits of the 128-bit re= gisters */
@@ -273,13 +285,10 @@ struct CPUArchState {

=C2=A0 =C2=A0 =C2=A0target_ulong mcountinhibit;

-=C2=A0 =C2=A0 /* PMU counter configured values */
-=C2=A0 =C2=A0 target_ulong mhpmcounter_val[RV_MAX_MHPMCOUNTERS];
-
-=C2=A0 =C2=A0 /* for RV32 */
-=C2=A0 =C2=A0 target_ulong mhpmcounterh_val[RV_MAX_MHPMCOUNTERS];
+=C2=A0 =C2=A0 /* PMU counter state */
+=C2=A0 =C2=A0 PMUCTRState pmu_ctrs[RV_MAX_MHPMCOUNTERS];

-=C2=A0 =C2=A0 /* PMU event selector configured values */
+=C2=A0 =C2=A0 /* PMU event selector configured values. First three are unu= sed*/
=C2=A0 =C2=A0 =C2=A0target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS];

=C2=A0 =C2=A0 =C2=A0target_ulong sscratch;
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index 87aa601e5ddb..c050ed2e2c1b 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -21,6 +21,7 @@
=C2=A0#include "qemu/log.h"
=C2=A0#include "qemu/timer.h"
=C2=A0#include "cpu.h"
+#include "pmu.h"
=C2=A0#include "qemu/main-loop.h"
=C2=A0#include "exec/exec-all.h"
=C2=A0#include "sysemu/cpu-timers.h"
@@ -597,34 +598,28 @@ static int write_vcsr(CPURISCVState *env, int csrno, = target_ulong val)
=C2=A0}

=C2=A0/* User Timers and Counters */
-static RISCVException read_instret(CPURISCVState *env, int csrno,
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0target_ulong *val)
+static target_ulong get_ticks(bool shift)
=C2=A0{
+=C2=A0 =C2=A0 int64_t val;
+=C2=A0 =C2=A0 target_ulong result;
+
=C2=A0#if !defined(CONFIG_USER_ONLY)
=C2=A0 =C2=A0 =C2=A0if (icount_enabled()) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D icount_get();
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 val =3D icount_get();
=C2=A0 =C2=A0 =C2=A0} else {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D cpu_get_host_ticks();
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 val =3D cpu_get_host_ticks();
=C2=A0 =C2=A0 =C2=A0}
=C2=A0#else
-=C2=A0 =C2=A0 *val =3D cpu_get_host_ticks();
+=C2=A0 =C2=A0 val =3D cpu_get_host_ticks();
=C2=A0#endif
-=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
-}

-static RISCVException read_instreth(CPURISCVState *env, int csrno,
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 target_ulong *val)
-{
-#if !defined(CONFIG_USER_ONLY)
-=C2=A0 =C2=A0 if (icount_enabled()) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D icount_get() >> 32;
+=C2=A0 =C2=A0 if (shift) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 result =3D val >> 32;
=C2=A0 =C2=A0 =C2=A0} else {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D cpu_get_host_ticks() >> 32;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 result =3D val;
=C2=A0 =C2=A0 =C2=A0}
-#else
-=C2=A0 =C2=A0 *val =3D cpu_get_host_ticks() >> 32;
-#endif
-=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+
+=C2=A0 =C2=A0 return result;
=C2=A0}

=C2=A0#if defined(CONFIG_USER_ONLY)
@@ -642,11 +637,23 @@ static RISCVException read_timeh(CPURISCVState *env, = int csrno,
=C2=A0 =C2=A0 =C2=A0return RISCV_EXCP_NONE;
=C2=A0}

+static int read_hpmcounter(CPURISCVState *env, int csrno, target_ulong *va= l)
+{
+=C2=A0 =C2=A0 *val =3D get_ticks(false);
+=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+}
+
+static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong *v= al)
+{
+=C2=A0 =C2=A0 *val =3D get_ticks(true);
+=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+}
+
=C2=A0#else /* CONFIG_USER_ONLY */

=C2=A0static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong= *val)
=C2=A0{
-=C2=A0 =C2=A0 int evt_index =3D csrno - CSR_MHPMEVENT3;
+=C2=A0 =C2=A0 int evt_index =3D csrno - CSR_MCOUNTINHIBIT;

=C2=A0 =C2=A0 =C2=A0*val =3D env->mhpmevent_val[evt_index];

@@ -655,7 +662,7 @@ static int read_mhpmevent(CPURISCVState *env, int csrno= , target_ulong *val)

=C2=A0static int write_mhpmevent(CPURISCVState *env, int csrno, target_ulon= g val)
=C2=A0{
-=C2=A0 =C2=A0 int evt_index =3D csrno - CSR_MHPMEVENT3;
+=C2=A0 =C2=A0 int evt_index =3D csrno - CSR_MCOUNTINHIBIT;

=C2=A0 =C2=A0 =C2=A0env->mhpmevent_val[evt_index] =3D val;

@@ -664,52 +671,102 @@ static int write_mhpmevent(CPURISCVState *env, int c= srno, target_ulong val)

=C2=A0static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ul= ong val)
=C2=A0{
-=C2=A0 =C2=A0 int ctr_index =3D csrno - CSR_MHPMCOUNTER3 + 3;
+=C2=A0 =C2=A0 int ctr_idx =3D csrno - CSR_MCYCLE;
+=C2=A0 =C2=A0 PMUCTRState *counter =3D &env->pmu_ctrs[ctr_idx];

-=C2=A0 =C2=A0 env->mhpmcounter_val[ctr_index] =3D val;
+=C2=A0 =C2=A0 counter->mhpmcounter_val =3D val;
+=C2=A0 =C2=A0 if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 riscv_pmu_ctr_monitor_instructions(env, ctr_id= x)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 counter->mhpmcounter_prev =3D get_ticks(fal= se);
+=C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Other counters can keep incrementing from t= he given value */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 counter->mhpmcounter_prev =3D val;
+=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0return RISCV_EXCP_NONE;
=C2=A0}

=C2=A0static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_u= long val)
=C2=A0{
-=C2=A0 =C2=A0 int ctr_index =3D csrno - CSR_MHPMCOUNTER3H + 3;
+=C2=A0 =C2=A0 int ctr_idx =3D csrno - CSR_MCYCLEH;
+=C2=A0 =C2=A0 PMUCTRState *counter =3D &env->pmu_ctrs[ctr_idx];

-=C2=A0 =C2=A0 env->mhpmcounterh_val[ctr_index] =3D val;
+=C2=A0 =C2=A0 counter->mhpmcounterh_val =3D val;
+=C2=A0 =C2=A0 if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 riscv_pmu_ctr_monitor_instructions(env, ctr_id= x)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 counter->mhpmcounterh_prev =3D get_ticks(tr= ue);
+=C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 counter->mhpmcounterh_prev =3D val;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+}
+
+static RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong = *val,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0bo= ol upper_half, uint32_t ctr_idx)
+{
+=C2=A0 =C2=A0 PMUCTRState counter =3D env->pmu_ctrs[ctr_idx];
+=C2=A0 =C2=A0 target_ulong ctr_prev =3D upper_half ? counter.mhpmcounterh_= prev :
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0co= unter.mhpmcounter_prev;
+=C2=A0 =C2=A0 target_ulong ctr_val =3D upper_half ? counter.mhpmcounterh_v= al :
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 counter.= mhpmcounter_val;
+
+=C2=A0 =C2=A0 if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 /**
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* Counter should not increment if inhibi= t bit is set. We can't really
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* stop the icount counting. Just return = the counter value written by
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* the supervisor to indicate that counte= r was not incremented.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*/
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!counter.started) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D ctr_val;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Mark that the counter has bee= n stopped */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 counter.started =3D false;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
+
+=C2=A0 =C2=A0 /**
+=C2=A0 =C2=A0 =C2=A0* The kernel computes the perf delta by subtracting th= e current value from
+=C2=A0 =C2=A0 =C2=A0* the value it initialized previously (ctr_val).
+=C2=A0 =C2=A0 =C2=A0*/
+=C2=A0 =C2=A0 if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 riscv_pmu_ctr_monitor_instructions(env, ctr_id= x)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D get_ticks(upper_half) - ctr_prev + ct= r_val;
+=C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 *val =3D ctr_val;
+=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0return RISCV_EXCP_NONE;
=C2=A0}

=C2=A0static int read_hpmcounter(CPURISCVState *env, int csrno, target_ulon= g *val)
=C2=A0{
-=C2=A0 =C2=A0 int ctr_index;
+=C2=A0 =C2=A0 uint16_t ctr_index;

=C2=A0 =C2=A0 =C2=A0if (env->priv =3D=3D PRV_M) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_MHPMCOUNTER3 + 3; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_MCYCLE;=C2=A0
=C2=A0 =C2=A0 =C2=A0} else {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_HPMCOUNTER3 + 3;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_CYCLE;
=C2=A0 =C2=A0 =C2=A0}

Hi Atish,

According to spec:
"The RDCYCLE pseudoinst= ruction reads the low XLEN bits of the cycle CSR
which holds a co= unt of the number of clock cycles executed by the
processor core = on which the hart is running from an arbitrary start time in the past."= ;

However, the counter index calculation here woul= d have the issue.
For example, if RDCYCLE instruction is executed= in M-mode:
CYCLE csrno=C2=A0is: 0xc00, but MCYCLE csrno is: 0xb0= 0
"csrno - CSR_MCYCLE" would result in the invalidate v= alue: 0x100,
which is out of the counter array's bound.
=

Will it be easier to just check against csrno instead o= f the current privileged mode?

And I think the sam= e issue also occurs for:
HPMCOUNTER3 ~=C2=A0HPMCOUNTER31.

Regards,
Frank Chang
=C2=A0
=
-=C2=A0 =C2=A0 *val =3D env->mhpmcounter_val[ctr_index];

-=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+=C2=A0 =C2=A0 return riscv_pmu_read_ctr(env, val, false, ctr_index);
=C2=A0}

=C2=A0static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulo= ng *val)
=C2=A0{
-=C2=A0 =C2=A0 int ctr_index;
+=C2=A0 =C2=A0 uint16_t ctr_index;

=C2=A0 =C2=A0 =C2=A0if (env->priv =3D=3D PRV_M) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_MHPMCOUNTER3H + 3; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_MCYCLEH;
=C2=A0 =C2=A0 =C2=A0} else {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_HPMCOUNTER3H + 3; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 ctr_index =3D csrno - CSR_CYCLEH;
=C2=A0 =C2=A0 =C2=A0}

-=C2=A0 =C2=A0 *val =3D env->mhpmcounterh_val[ctr_index];
-
-=C2=A0 =C2=A0 return RISCV_EXCP_NONE;
+=C2=A0 =C2=A0 return riscv_pmu_read_ctr(env, val, true, ctr_index);
=C2=A0}

-
=C2=A0static RISCVException read_time(CPURISCVState *env, int csrno,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0target_ulong *val)
=C2=A0{
@@ -1564,11 +1621,23 @@ static RISCVException read_mcountinhibit(CPURISCVSt= ate *env, int csrno,
=C2=A0static RISCVException write_mcountinhibit(CPURISCVState *env, int csr= no,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0target_ulong val)
=C2=A0{
+=C2=A0 =C2=A0 int cidx;
+=C2=A0 =C2=A0 PMUCTRState *counter;
+
=C2=A0 =C2=A0 =C2=A0if (env->priv_ver < PRIV_VERSION_1_11_0) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return RISCV_EXCP_ILLEGAL_INST;
=C2=A0 =C2=A0 =C2=A0}

=C2=A0 =C2=A0 =C2=A0env->mcountinhibit =3D val;
+
+=C2=A0 =C2=A0 /* Check if any other counter is also monitoring cycles/inst= ructions */
+=C2=A0 =C2=A0 for (cidx =3D 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) { +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!get_field(env->mcountinhibit, BIT(cidx= ))) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 counter =3D &env->pmu_ctr= s[cidx];
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 counter->started =3D true; +=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
=C2=A0 =C2=A0 =C2=A0return RISCV_EXCP_NONE;
=C2=A0}

@@ -3526,10 +3595,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D {<= br> =C2=A0 =C2=A0 =C2=A0[CSR_VLENB]=C2=A0 =C2=A0 =3D { "vlenb",=C2=A0= =C2=A0 vs,=C2=A0 =C2=A0 read_vlenb,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0.min_priv_ver =3D PRIV_VERSION_1_12_0 },
=C2=A0 =C2=A0 =C2=A0/* User Timers and Counters */
-=C2=A0 =C2=A0 [CSR_CYCLE]=C2=A0 =C2=A0 =3D { "cycle",=C2=A0 =C2= =A0 ctr,=C2=A0 =C2=A0 read_instret=C2=A0 },
-=C2=A0 =C2=A0 [CSR_INSTRET]=C2=A0 =3D { "instret",=C2=A0 ctr,=C2= =A0 =C2=A0 read_instret=C2=A0 },
-=C2=A0 =C2=A0 [CSR_CYCLEH]=C2=A0 =C2=A0=3D { "cycleh",=C2=A0 =C2= =A0ctr32,=C2=A0 read_instreth },
-=C2=A0 =C2=A0 [CSR_INSTRETH] =3D { "instreth", ctr32,=C2=A0 read= _instreth },
+=C2=A0 =C2=A0 [CSR_CYCLE]=C2=A0 =C2=A0 =3D { "cycle",=C2=A0 =C2= =A0 ctr,=C2=A0 =C2=A0 read_hpmcounter=C2=A0 },
+=C2=A0 =C2=A0 [CSR_INSTRET]=C2=A0 =3D { "instret",=C2=A0 ctr,=C2= =A0 =C2=A0 read_hpmcounter=C2=A0 },
+=C2=A0 =C2=A0 [CSR_CYCLEH]=C2=A0 =C2=A0=3D { "cycleh",=C2=A0 =C2= =A0ctr32,=C2=A0 read_hpmcounterh },
+=C2=A0 =C2=A0 [CSR_INSTRETH] =3D { "instreth", ctr32,=C2=A0 read= _hpmcounterh },

=C2=A0 =C2=A0 =C2=A0/*
=C2=A0 =C2=A0 =C2=A0 * In privileged mode, the monitor will have to emulate= TIME CSRs only if
@@ -3543,10 +3612,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D {<= br>
=C2=A0#if !defined(CONFIG_USER_ONLY)
=C2=A0 =C2=A0 =C2=A0/* Machine Timers and Counters */
-=C2=A0 =C2=A0 [CSR_MCYCLE]=C2=A0 =C2=A0 =3D { "mcycle",=C2=A0 = =C2=A0 any,=C2=A0 =C2=A0read_instret=C2=A0 },
-=C2=A0 =C2=A0 [CSR_MINSTRET]=C2=A0 =3D { "minstret",=C2=A0 any,= =C2=A0 =C2=A0read_instret=C2=A0 },
-=C2=A0 =C2=A0 [CSR_MCYCLEH]=C2=A0 =C2=A0=3D { "mcycleh",=C2=A0 = =C2=A0any32, read_instreth },
-=C2=A0 =C2=A0 [CSR_MINSTRETH] =3D { "minstreth", any32, read_ins= treth },
+=C2=A0 =C2=A0 [CSR_MCYCLE]=C2=A0 =C2=A0 =3D { "mcycle",=C2=A0 = =C2=A0 any,=C2=A0 =C2=A0read_hpmcounter, write_mhpmcounter},
+=C2=A0 =C2=A0 [CSR_MINSTRET]=C2=A0 =3D { "minstret",=C2=A0 any,= =C2=A0 =C2=A0read_hpmcounter, write_mhpmcounter},
+=C2=A0 =C2=A0 [CSR_MCYCLEH]=C2=A0 =C2=A0=3D { "mcycleh",=C2=A0 = =C2=A0any32, read_hpmcounterh, write_mhpmcounterh},
+=C2=A0 =C2=A0 [CSR_MINSTRETH] =3D { "minstreth", any32, read_hpm= counterh, write_mhpmcounterh},

=C2=A0 =C2=A0 =C2=A0/* Machine Information Registers */
=C2=A0 =C2=A0 =C2=A0[CSR_MVENDORID] =3D { "mvendorid", any,=C2=A0= =C2=A0read_mvendorid },
diff --git a/target/riscv/machine.c b/target/riscv/machine.c
index 99193c85bb97..dc182ca81119 100644
--- a/target/riscv/machine.c
+++ b/target/riscv/machine.c
@@ -279,7 +279,28 @@ static const VMStateDescription vmstate_envcfg =3D { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINT64(env.menvcfg, RISCVCPU), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.senvcfg, RISCVCPU), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINT64(env.henvcfg, RISCVCPU), +=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_END_OF_LIST()
+=C2=A0 =C2=A0 }
+};
+
+static bool pmu_needed(void *opaque)
+{
+=C2=A0 =C2=A0 RISCVCPU *cpu =3D opaque;

+=C2=A0 =C2=A0 return cpu->cfg.pmu_num;
+}
+
+static const VMStateDescription vmstate_pmu_ctr_state =3D {
+=C2=A0 =C2=A0 .name =3D "cpu/pmu",
+=C2=A0 =C2=A0 .version_id =3D 1,
+=C2=A0 =C2=A0 .minimum_version_id =3D 1,
+=C2=A0 =C2=A0 .needed =3D pmu_needed,
+=C2=A0 =C2=A0 .fields =3D (VMStateField[]) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL(mhpmcounter_val, PMUCTRState),<= br> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL(mhpmcounterh_val, PMUCTRState),=
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL(mhpmcounter_prev, PMUCTRState),=
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL(mhpmcounterh_prev, PMUCTRState)= ,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_BOOL(started, PMUCTRState),
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_END_OF_LIST()
=C2=A0 =C2=A0 =C2=A0}
=C2=A0};
@@ -331,8 +352,8 @@ const VMStateDescription vmstate_riscv_cpu =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.scounteren, RISCVCPU),=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.mcounteren, RISCVCPU),=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.mcountinhibit, RISCVCP= U),
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL_ARRAY(env.mhpmcounter_val, RISC= VCPU, RV_MAX_MHPMCOUNTERS),
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_UINTTL_ARRAY(env.mhpmcounterh_val, RIS= CVCPU, RV_MAX_MHPMCOUNTERS),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 VMSTATE_STRUCT_ARRAY(env.pmu_ctrs, RISCVCPU, R= V_MAX_MHPMCOUNTERS, 0,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0vmstate_pmu_ctr_state, PMUCTRState),
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL_ARRAY(env.mhpmevent_val, R= ISCVCPU, RV_MAX_MHPMEVENTS),
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.sscratch, RISCVCPU), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VMSTATE_UINTTL(env.mscratch, RISCVCPU), diff --git a/target/riscv/meson.build b/target/riscv/meson.build
index 096249f3a30f..2c1975e72c4e 100644
--- a/target/riscv/meson.build
+++ b/target/riscv/meson.build
@@ -30,7 +30,8 @@ riscv_softmmu_ss.add(files(
=C2=A0 =C2=A0'pmp.c',
=C2=A0 =C2=A0'debug.c',
=C2=A0 =C2=A0'monitor.c',
-=C2=A0 'machine.c'
+=C2=A0 'machine.c',
+=C2=A0 'pmu.c'
=C2=A0))

=C2=A0target_arch +=3D {'riscv': riscv_ss}
diff --git a/target/riscv/pmu.c b/target/riscv/pmu.c
new file mode 100644
index 000000000000..000fe8da45ef
--- /dev/null
+++ b/target/riscv/pmu.c
@@ -0,0 +1,32 @@
+/*
+ * RISC-V PMU file.
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ *
+ * This program is free software; you can redistribute it and/or modify it=
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT<= br> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE.=C2=A0 See the GNU General Public Lice= nse for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along= with
+ * this program.=C2=A0 If not, see <http://www.gnu.org/licenses/= >.
+ */
+
+#include "qemu/osdep.h"
+#include "cpu.h"
+#include "pmu.h"
+
+bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 uint32_t= target_ctr)
+{
+=C2=A0 =C2=A0 return (target_ctr =3D=3D 0) ? true : false;
+}
+
+bool riscv_pmu_ctr_monitor_cycles(CPURISCVState *env, uint32_t target_ctr)=
+{
+=C2=A0 =C2=A0 return (target_ctr =3D=3D 2) ? true : false;
+}
diff --git a/target/riscv/pmu.h b/target/riscv/pmu.h
new file mode 100644
index 000000000000..58a5bc3a4089
--- /dev/null
+++ b/target/riscv/pmu.h
@@ -0,0 +1,28 @@
+/*
+ * RISC-V PMU header file.
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ *
+ * This program is free software; you can redistribute it and/or modify it=
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT<= br> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE.=C2=A0 See the GNU General Public Lice= nse for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along= with
+ * this program.=C2=A0 If not, see <http://www.gnu.org/licenses/= >.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "cpu.h"
+#include "qemu/main-loop.h"
+#include "exec/exec-all.h"
+
+bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 uint32_t= target_ctr);
+bool riscv_pmu_ctr_monitor_cycles(CPURISCVState *env,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 uint32_t target_ctr);
--
2.25.1


--00000000000023709705dedecc07--