Skip to contentSkip to conversation

Improve PMU support

PATCH In review Atish Patra 4 participants 20 messages Aug 24, 2022 – Nov 30, 2022 RISC-V

lore
Share & tools
lore
#0 lore raw

v14 cover Improve PMU support

The latest version of the SBI specification includes a Performance Monitoring Unit(PMU) extension[1] which allows the supervisor to start/stop/configure various PMU events. The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension[2] allows the perf like tool to handle overflow interrupts and filtering support.

This series implements remaining PMU infrastructure to support PMU in virt machine. The first seven patches from the original series have been merged already.

This will allow us to add any PMU events in future. Currently, this series enables the following omu events.
1. cycle count
2. instruction count
3. DTLB load/store miss
4. ITLB prefetch miss

The first two are computed using host ticks while last three are counted during cpu_tlb_fill. We can do both sampling and count from guest userspace. This series has been tested on both RV64 and RV32. Both Linux[3] and Opensbi[4] patches are required to get the perf working.

Here is an output of perf stat/report while running hackbench with latest OpenSBI & Linux kernel.

Perf stat:
==========
[root@fedora-riscv ~]# perf stat -e cycles -e instructions -e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses \

Quoted 1 line

perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

     Total time: 0.265 [sec]

Performance counter stats for 'perf bench sched messaging -g 1 -l 10':

     4,167,825,362      cycles                                                      
     4,166,609,256      instructions              #    1.00  insn per cycle         
         3,092,026      dTLB-load-misses                                            
           258,280      dTLB-store-misses                                           
         2,068,966      iTLB-load-misses                                            
       0.585791767 seconds time elapsed

       0.373802000 seconds user
       1.042359000 seconds sys

Perf record:
============
[root@fedora-riscv ~]# perf record -e cycles -e instructions \

Quoted 2 lines

-e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses -c 10000 \
perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

     Total time: 1.397 [sec]

[ perf record: Woken up 10 times to write data ]
Check IO/CPU overload!
[ perf record: Captured and wrote 8.211 MB perf.data (214486 samples) ]

[root@fedora-riscv riscv]# perf report
Available samples

107K cycles                                                                    ◆
107K instructions                                                              ▒
250 dTLB-load-misses                                                           ▒
13 dTLB-store-misses                                                           ▒

172 iTLB-load-misses
..

Changes from v13->v14:
1. Added sanity check for the hashtable in pmu.c

Changes from v12->v13:
1. Rebased on top of the apply-next.
2. Addressed comments about space & comment block.

Changes from v11->v12:
1. Rebased on top of the apply-next.
2. Aligned the write function & .min_priv to the previous line.
3. Fixed the FDT generations for multi-socket scenario.
4. Dropped interrupt property from the DT.
5. Generate illegal instruction fault instead of virtual instruction fault for VS/VU access while mcounteren is not set.

Changes from v10->v11:
1. Rebased on top of the master where first 7 patches were already merged.
2. Removed unnecessary additional check in ctr predicate function.
3. Removed unnecessary priv version checks in mcountinhibit read/write.
4. Added Heiko's reviewed-by/tested-by tags.

Changes from v8->v9:
1. Added the write_done flags to the vmstate.
2. Fixed the hpmcounter read access from M-mode.

Changes from v7->v8:
1. Removeding ordering constraints for mhpmcounter & mhpmevent.

Changes from v6->v7:
1. Fixed all the compilation errors for the usermode.

Changes from v5->v6:
1. Fixed compilation issue with PATCH 1.
2. Addressed other comments.

Changes from v4->v5:
1. Rebased on top of the -next with following patches.
- isa extension
- priv 1.12 spec
2. Addressed all the comments on v4
3. Removed additional isa-ext DT node in favor of riscv,isa string update

Changes from v3->v4:
1. Removed the dummy events from pmu DT node.
2. Fixed pmu_avail_counters mask generation.
3. Added a patch to simplify the predicate function for counters.

Changes from v2->v3:
1. Addressed all the comments on PATCH1-4.
2. Split patch1 into two separate patches.
3. Added explicit comments to explain the event types in DT node.
4. Rebased on latest Qemu.

Changes from v1->v2:
1. Dropped the ACks from v1 as signficant changes happened after v1.
2. sscofpmf support.
3. A generic counter management framework.

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://drive.google.com/file/d/171j4jFjIkKdj5LWcExphq4xG_2sihbfd/edit
[3] https://github.com/atishp04/qemu/tree/riscv_pmu_v14

Atish Patra (5):
target/riscv: Add sscofpmf extension support target/riscv: Simplify counter predicate function target/riscv: Add few cache related PMU events hw/riscv: virt: Add PMU DT node to the device tree target/riscv: Update the privilege field for sscofpmf CSRs

hw/riscv/virt.c           |  16 ++
target/riscv/cpu.c        |  12 ++
target/riscv/cpu.h        |  25 +++
target/riscv/cpu_bits.h   |  55 +++++
target/riscv/cpu_helper.c |  25 +++
target/riscv/csr.c        | 304 +++++++++++++++++----------
target/riscv/machine.c    |   1 +
target/riscv/pmu.c        | 425 +++++++++++++++++++++++++++++++++++++-
target/riscv/pmu.h        |   8 +
9 files changed, 760 insertions(+), 111 deletions(-)

--
2.25.1

#1 lore raw

PATCH v14 1/5 target/riscv: Add sscofpmf extension support

The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.

---
 target/riscv/cpu.c      |  12 ++
 target/riscv/cpu.h      |  25 +++
 target/riscv/cpu_bits.h |  55 ++++++
 target/riscv/csr.c      | 166 +++++++++++++++++-
 target/riscv/machine.c  |   1 +
 target/riscv/pmu.c      | 368 +++++++++++++++++++++++++++++++++++++++-
 target/riscv/pmu.h      |   7 +
 7 files changed, 623 insertions(+), 11 deletions(-)
target/riscv/cpu.c +12 -0
index 8ab36e82e190..aee14a239af8 100644
@@ -22,6 +22,7 @@
 #include "qemu/ctype.h"
 #include "qemu/log.h"
 #include "cpu.h"
+#include "pmu.h"
 #include "internals.h"
 #include "time_helper.h"
 #include "exec/exec-all.h"
@@ -102,6 +103,7 @@ static const struct isa_ext_data isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zhinxmin, true, PRIV_VERSION_1_12_0, ext_zhinxmin),
     ISA_EXT_DATA_ENTRY(smaia, true, PRIV_VERSION_1_12_0, ext_smaia),
     ISA_EXT_DATA_ENTRY(ssaia, true, PRIV_VERSION_1_12_0, ext_ssaia),
+    ISA_EXT_DATA_ENTRY(sscofpmf, true, PRIV_VERSION_1_12_0, ext_sscofpmf),
     ISA_EXT_DATA_ENTRY(sstc, true, PRIV_VERSION_1_12_0, ext_sstc),
     ISA_EXT_DATA_ENTRY(svinval, true, PRIV_VERSION_1_12_0, ext_svinval),
     ISA_EXT_DATA_ENTRY(svnapot, true, PRIV_VERSION_1_12_0, ext_svnapot),
@@ -889,6 +891,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
         set_misa(env, env->misa_mxl, ext);
     }
 
+#ifndef CONFIG_USER_ONLY
+    if (cpu->cfg.pmu_num) {
+        if (!riscv_pmu_init(cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) {
+            cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                          riscv_pmu_timer_cb, cpu);
+        }
+     }
+#endif
+
     riscv_cpu_register_gdb_regs_for_features(cs);
 
     qemu_init_vcpu(cs);
@@ -993,6 +1004,7 @@ static Property riscv_cpu_extensions[] = {
     DEFINE_PROP_BOOL("v", RISCVCPU, cfg.ext_v, false),
     DEFINE_PROP_BOOL("h", RISCVCPU, cfg.ext_h, true),
     DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
+    DEFINE_PROP_BOOL("sscofpmf", RISCVCPU, cfg.ext_sscofpmf, false),
     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
     DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),
target/riscv/cpu.h +25 -0
index d895a0af2c6d..06751e1e3e18 100644
@@ -136,6 +136,8 @@ typedef struct PMUCTRState {
     /* Snapshort value of a counter in RV32 */
     target_ulong mhpmcounterh_prev;
     bool started;
+    /* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */
+    target_ulong irq_overflow_left;
 } PMUCTRState;
 
 struct CPUArchState {
@@ -301,6 +303,9 @@ struct CPUArchState {
     /* PMU event selector configured values. First three are unused*/
     target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS];
 
+    /* PMU event selector configured values for RV32*/
+    target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS];
+
     target_ulong sscratch;
     target_ulong mscratch;
 
@@ -447,6 +452,7 @@ struct RISCVCPUConfig {
     bool ext_zmmul;
     bool ext_smaia;
     bool ext_ssaia;
+    bool ext_sscofpmf;
     bool rvv_ta_all_1s;
     bool rvv_ma_all_1s;
 
@@ -493,6 +499,12 @@ struct ArchCPU {
 
     /* Configuration Settings */
     RISCVCPUConfig cfg;
+
+    QEMUTimer *pmu_timer;
+    /* A bitmask of Available programmable counters */
+    uint32_t pmu_avail_ctrs;
+    /* Mapping of events to counters */
+    GHashTable *pmu_event_ctr_map;
 };
 
 static inline int riscv_has_ext(CPURISCVState *env, target_ulong ext)
@@ -753,6 +765,19 @@ enum {
     CSR_TABLE_SIZE = 0x1000
 };
 
+/**
+ * The event id are encoded based on the encoding specified in the
+ * SBI specification v0.3
+ */
+
+enum riscv_pmu_event_idx {
+    RISCV_PMU_EVENT_HW_CPU_CYCLES = 0x01,
+    RISCV_PMU_EVENT_HW_INSTRUCTIONS = 0x02,
+    RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS = 0x10019,
+    RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS = 0x1001B,
+    RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS = 0x10021,
+};
+
 /* CSR function table */
 extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE];
 
target/riscv/cpu_bits.h +55 -0

88 lines not on this page · raw message

target/riscv/csr.c +157 -9

272 lines not on this page · raw message

target/riscv/machine.c +1 -0
index 4ba55705d147..41098f6ad03a 100644
@@ -356,6 +356,7 @@ const VMStateDescription vmstate_riscv_cpu = {
         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_ARRAY(env.mhpmeventh_val, RISCVCPU, RV_MAX_MHPMEVENTS),
         VMSTATE_UINTTL(env.sscratch, RISCVCPU),
         VMSTATE_UINTTL(env.mscratch, RISCVCPU),
         VMSTATE_UINT64(env.mfromhost, RISCVCPU),
target/riscv/pmu.c +366 -2

380 lines not on this page · raw message

target/riscv/pmu.h +7 -0
index 58a5bc3a4089..036653627f78 100644
@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env,
                                         uint32_t target_ctr);
 bool riscv_pmu_ctr_monitor_cycles(CPURISCVState *env,
                                   uint32_t target_ctr);
+void riscv_pmu_timer_cb(void *priv);
+int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
+int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
+                               uint32_t ctr_idx);
+int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
+                          uint32_t ctr_idx);
Signature · 1 line
2.25.1
#2 lore raw

PATCH v14 2/5 target/riscv: Simplify counter predicate function

In reply to Atish Patra

All the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.

Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.

---
 target/riscv/csr.c | 110 ++++-----------------------------------------
 1 file changed, 9 insertions(+), 101 deletions(-)
target/riscv/csr.c +9 -101
index 888ddfc4dd4b..2151e280a868 100644
@@ -75,6 +75,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
     CPUState *cs = env_cpu(env);
     RISCVCPU *cpu = RISCV_CPU(cs);
     int ctr_index;
+    target_ulong ctr_mask;
     int base_csrno = CSR_CYCLE;
     bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
 
@@ -83,122 +84,29 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
         base_csrno += 0x80;
     }
     ctr_index = csrno - base_csrno;
+    ctr_mask = BIT(ctr_index);
 
     if ((csrno >= CSR_CYCLE && csrno <= CSR_INSTRET) ||
         (csrno >= CSR_CYCLEH && csrno <= CSR_INSTRETH)) {
         goto skip_ext_pmu_check;
     }
 
-    if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & BIT(ctr_index)))) {
+    if (!(cpu->pmu_avail_ctrs & ctr_mask)) {
         /* No counter is enabled in PMU or the counter is out of range */
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
 skip_ext_pmu_check:
 
-    if (env->priv == PRV_S) {
-        switch (csrno) {
-        case CSR_CYCLE:
-            if (!get_field(env->mcounteren, COUNTEREN_CY)) {
-                return RISCV_EXCP_ILLEGAL_INST;
-            }
-            break;
-        case CSR_TIME:
-            if (!get_field(env->mcounteren, COUNTEREN_TM)) {
-                return RISCV_EXCP_ILLEGAL_INST;
-            }
-            break;
-        case CSR_INSTRET:
-            if (!get_field(env->mcounteren, COUNTEREN_IR)) {
-                return RISCV_EXCP_ILLEGAL_INST;
-            }
-            break;
-        case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
-            if (!get_field(env->mcounteren, 1 << ctr_index)) {
-                return RISCV_EXCP_ILLEGAL_INST;
-            }
-            break;
-        }
-        if (rv32) {
-            switch (csrno) {
-            case CSR_CYCLEH:
-                if (!get_field(env->mcounteren, COUNTEREN_CY)) {
-                    return RISCV_EXCP_ILLEGAL_INST;
-                }
-                break;
-            case CSR_TIMEH:
-                if (!get_field(env->mcounteren, COUNTEREN_TM)) {
-                    return RISCV_EXCP_ILLEGAL_INST;
-                }
-                break;
-            case CSR_INSTRETH:
-                if (!get_field(env->mcounteren, COUNTEREN_IR)) {
-                    return RISCV_EXCP_ILLEGAL_INST;
-                }
-                break;
-            case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
-                if (!get_field(env->mcounteren, 1 << ctr_index)) {
-                    return RISCV_EXCP_ILLEGAL_INST;
-                }
-                break;
-            }
-        }
+    if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
+        ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
+        return RISCV_EXCP_ILLEGAL_INST;
     }
 
     if (riscv_cpu_virt_enabled(env)) {
-        switch (csrno) {
-        case CSR_CYCLE:
-            if (!get_field(env->hcounteren, COUNTEREN_CY) &&
-                get_field(env->mcounteren, COUNTEREN_CY)) {
-                return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-            }
-            break;
-        case CSR_TIME:
-            if (!get_field(env->hcounteren, COUNTEREN_TM) &&
-                get_field(env->mcounteren, COUNTEREN_TM)) {
-                return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-            }
-            break;
-        case CSR_INSTRET:
-            if (!get_field(env->hcounteren, COUNTEREN_IR) &&
-                get_field(env->mcounteren, COUNTEREN_IR)) {
-                return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-            }
-            break;
-        case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
-            if (!get_field(env->hcounteren, 1 << ctr_index) &&
-                 get_field(env->mcounteren, 1 << ctr_index)) {
-                return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-            }
-            break;
-        }
-        if (rv32) {
-            switch (csrno) {
-            case CSR_CYCLEH:
-                if (!get_field(env->hcounteren, COUNTEREN_CY) &&
-                    get_field(env->mcounteren, COUNTEREN_CY)) {
-                    return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-                }
-                break;
-            case CSR_TIMEH:
-                if (!get_field(env->hcounteren, COUNTEREN_TM) &&
-                    get_field(env->mcounteren, COUNTEREN_TM)) {
-                    return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-                }
-                break;
-            case CSR_INSTRETH:
-                if (!get_field(env->hcounteren, COUNTEREN_IR) &&
-                    get_field(env->mcounteren, COUNTEREN_IR)) {
-                    return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-                }
-                break;
-            case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
-                if (!get_field(env->hcounteren, 1 << ctr_index) &&
-                     get_field(env->mcounteren, 1 << ctr_index)) {
-                    return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
-                }
-                break;
-            }
+        if (!get_field(env->hcounteren, ctr_mask) &&
+            get_field(env->mcounteren, ctr_mask)) {
+            return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
     }
 #endif
Signature · 1 line
2.25.1
#3 lore raw

PATCH v14 3/5 target/riscv: Add few cache related PMU events

In reply to Atish Patra

From: Atish Patra <[email protected]>

Qemu can monitor the following cache related PMU events through tlb_fill functions.

1. DTLB load/store miss
3. ITLB prefetch miss

Increment the PMU counter in tlb_fill function.

---
 target/riscv/cpu_helper.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
target/riscv/cpu_helper.c +25 -0
index 719c5d5d0209..67e4c0efd216 100644
@@ -21,11 +21,13 @@
 #include "qemu/log.h"
 #include "qemu/main-loop.h"
 #include "cpu.h"
+#include "pmu.h"
 #include "exec/exec-all.h"
 #include "instmap.h"
 #include "tcg/tcg-op.h"
 #include "trace.h"
 #include "semihosting/common-semi.h"
+#include "cpu_bits.h"
 
 int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch)
 {
@@ -1189,6 +1191,28 @@ void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
     cpu_loop_exit_restore(cs, retaddr);
 }
 
+
+static void pmu_tlb_fill_incr_ctr(RISCVCPU *cpu, MMUAccessType access_type)
+{
+    enum riscv_pmu_event_idx pmu_event_type;
+
+    switch (access_type) {
+    case MMU_INST_FETCH:
+        pmu_event_type = RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS;
+        break;
+    case MMU_DATA_LOAD:
+        pmu_event_type = RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS;
+        break;
+    case MMU_DATA_STORE:
+        pmu_event_type = RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS;
+        break;
+    default:
+        return;
+    }
+
+    riscv_pmu_incr_ctr(cpu, pmu_event_type);
+}
+
 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                         MMUAccessType access_type, int mmu_idx,
                         bool probe, uintptr_t retaddr)
@@ -1287,6 +1311,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             }
         }
     } else {
+        pmu_tlb_fill_incr_ctr(cpu, access_type);
         /* Single stage lookup */
         ret = get_physical_address(env, &pa, &prot, address, NULL,
                                    access_type, mmu_idx, true, false, false);
Signature · 1 line
2.25.1
#4 lore raw

PATCH v14 4/5 hw/riscv: virt: Add PMU DT node to the device tree

In reply to Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

---
 hw/riscv/virt.c    | 16 +++++++++++++
 target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 target/riscv/pmu.h |  1 +
 3 files changed, 74 insertions(+)
hw/riscv/virt.c +16 -0
index ff8c0df5cd47..befa9d2c26ac 100644
@@ -30,6 +30,7 @@
 #include "hw/char/serial.h"
 #include "target/riscv/cpu.h"
 #include "hw/core/sysbus-fdt.h"
+#include "target/riscv/pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/boot.h"
@@ -708,6 +709,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
     aplic_phandles[socket] = aplic_s_phandle;
 }
 
+static void create_fdt_pmu(RISCVVirtState *s)
+{
+    char *pmu_name;
+    MachineState *mc = MACHINE(s);
+    RISCVCPU hart = s->soc[0].harts[0];
+
+    pmu_name = g_strdup_printf("/soc/pmu");
+    qemu_fdt_add_subnode(mc->fdt, pmu_name);
+    qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu");
+    riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name);
+
+    g_free(pmu_name);
+}
+
 static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
                                bool is_32_bit, uint32_t *phandle,
                                uint32_t *irq_mmio_phandle,
@@ -1036,6 +1051,7 @@ static void create_fdt(RISCVVirtState *s, const MemMapEntry *memmap,
 
     create_fdt_flash(s, memmap);
     create_fdt_fw_cfg(s, memmap);
+    create_fdt_pmu(s);
 
 update_bootargs:
     if (cmdline && *cmdline) {
target/riscv/pmu.c +57 -0

68 lines not on this page · raw message

target/riscv/pmu.h +1 -0
index 036653627f78..3004ce37b636 100644
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
 int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
                                uint32_t ctr_idx);
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_name);
 int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
                           uint32_t ctr_idx);
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

Thanks,
Conor.

Quoted 139 lines
---
 hw/riscv/virt.c    | 16 +++++++++++++
 target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 target/riscv/pmu.h |  1 +
 3 files changed, 74 insertions(+)
hw/riscv/virt.c +16 -0
index ff8c0df5cd47..befa9d2c26ac 100644
@@ -30,6 +30,7 @@
 #include "hw/char/serial.h"
 #include "target/riscv/cpu.h"
 #include "hw/core/sysbus-fdt.h"
+#include "target/riscv/pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/boot.h"
@@ -708,6 +709,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
     aplic_phandles[socket] = aplic_s_phandle;
 }
 
+static void create_fdt_pmu(RISCVVirtState *s)
+{
+    char *pmu_name;
+    MachineState *mc = MACHINE(s);
+    RISCVCPU hart = s->soc[0].harts[0];
+
+    pmu_name = g_strdup_printf("/soc/pmu");
+    qemu_fdt_add_subnode(mc->fdt, pmu_name);
+    qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu");
+    riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name);
+
+    g_free(pmu_name);
+}
+
 static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
                                bool is_32_bit, uint32_t *phandle,
                                uint32_t *irq_mmio_phandle,
@@ -1036,6 +1051,7 @@ static void create_fdt(RISCVVirtState *s, const MemMapEntry *memmap,
 
     create_fdt_flash(s, memmap);
     create_fdt_fw_cfg(s, memmap);
+    create_fdt_pmu(s);
 
 update_bootargs:
     if (cmdline && *cmdline) {
target/riscv/pmu.c +57 -0
index a5f504e53c88..b8e56d2b7b8e 100644
@@ -20,11 +20,68 @@
 #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
+ * QEMU. The generic cycle & instruction count events can also be monitored
+ * using programmable counters. In that case, mcycle & minstret must continue
+ * to provide the correct value as well. Heterogeneous PMU per hart is not
+ * supported yet. Thus, number of counters are same across all harts.
+ */
+void riscv_pmu_generate_fdt_node(void *fdt, int num_ctrs, char *pmu_name)
+{
+    uint32_t fdt_event_ctr_map[20] = {};
+    uint32_t cmask;
+
+    /* All the programmable counters can map to any event */
+    cmask = MAKE_32BIT_MASK(3, num_ctrs);
+
+   /*
+    * The event encoding is specified in the SBI specification
+    * Event idx is a 20bits wide number encoded as follows:
+    * event_idx[19:16] = type
+    * event_idx[15:0] = code
+    * The code field in cache events are encoded as follows:
+    * event_idx.code[15:3] = cache_id
+    * event_idx.code[2:1] = op_id
+    * event_idx.code[0:0] = result_id
+    */
+
+   /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */
+   fdt_event_ctr_map[0] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[1] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[2] = cpu_to_be32(cmask | 1 << 0);
+
+   /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */
+   fdt_event_ctr_map[3] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[4] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[5] = cpu_to_be32(cmask | 1 << 2);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[6] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[7] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[8] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[9] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[10] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[11] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[12] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[13] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[14] = cpu_to_be32(cmask);
+
+   /* This a OpenSBI specific DT property documented in OpenSBI docs */
+   qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters",
+                    fdt_event_ctr_map, sizeof(fdt_event_ctr_map));
+}
+
 static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx)
 {
     if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||
target/riscv/pmu.h +1 -0
index 036653627f78..3004ce37b636 100644
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
 int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
                                uint32_t ctr_idx);
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_name);
 int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
                           uint32_t ctr_idx);
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 15 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

Quoted 9 lines

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Quoted 142 lines

Thanks,
Conor.

Quoted 139 lines
---
 hw/riscv/virt.c    | 16 +++++++++++++
 target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 target/riscv/pmu.h |  1 +
 3 files changed, 74 insertions(+)
hw/riscv/virt.c +16 -0
index ff8c0df5cd47..befa9d2c26ac 100644
@@ -30,6 +30,7 @@
 #include "hw/char/serial.h"
 #include "target/riscv/cpu.h"
 #include "hw/core/sysbus-fdt.h"
+#include "target/riscv/pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/boot.h"
@@ -708,6 +709,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
     aplic_phandles[socket] = aplic_s_phandle;
 }
 
+static void create_fdt_pmu(RISCVVirtState *s)
+{
+    char *pmu_name;
+    MachineState *mc = MACHINE(s);
+    RISCVCPU hart = s->soc[0].harts[0];
+
+    pmu_name = g_strdup_printf("/soc/pmu");
+    qemu_fdt_add_subnode(mc->fdt, pmu_name);
+    qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu");
+    riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name);
+
+    g_free(pmu_name);
+}
+
 static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
                                bool is_32_bit, uint32_t *phandle,
                                uint32_t *irq_mmio_phandle,
@@ -1036,6 +1051,7 @@ static void create_fdt(RISCVVirtState *s, const MemMapEntry *memmap,
 
     create_fdt_flash(s, memmap);
     create_fdt_fw_cfg(s, memmap);
+    create_fdt_pmu(s);
 
 update_bootargs:
     if (cmdline && *cmdline) {
target/riscv/pmu.c +57 -0
index a5f504e53c88..b8e56d2b7b8e 100644
@@ -20,11 +20,68 @@
 #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
+ * QEMU. The generic cycle & instruction count events can also be monitored
+ * using programmable counters. In that case, mcycle & minstret must continue
+ * to provide the correct value as well. Heterogeneous PMU per hart is not
+ * supported yet. Thus, number of counters are same across all harts.
+ */
+void riscv_pmu_generate_fdt_node(void *fdt, int num_ctrs, char *pmu_name)
+{
+    uint32_t fdt_event_ctr_map[20] = {};
+    uint32_t cmask;
+
+    /* All the programmable counters can map to any event */
+    cmask = MAKE_32BIT_MASK(3, num_ctrs);
+
+   /*
+    * The event encoding is specified in the SBI specification
+    * Event idx is a 20bits wide number encoded as follows:
+    * event_idx[19:16] = type
+    * event_idx[15:0] = code
+    * The code field in cache events are encoded as follows:
+    * event_idx.code[15:3] = cache_id
+    * event_idx.code[2:1] = op_id
+    * event_idx.code[0:0] = result_id
+    */
+
+   /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */
+   fdt_event_ctr_map[0] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[1] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[2] = cpu_to_be32(cmask | 1 << 0);
+
+   /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */
+   fdt_event_ctr_map[3] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[4] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[5] = cpu_to_be32(cmask | 1 << 2);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[6] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[7] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[8] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[9] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[10] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[11] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[12] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[13] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[14] = cpu_to_be32(cmask);
+
+   /* This a OpenSBI specific DT property documented in OpenSBI docs */
+   qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters",
+                    fdt_event_ctr_map, sizeof(fdt_event_ctr_map));
+}
+
 static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx)
 {
     if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||
target/riscv/pmu.h +1 -1
index 036653627f78..3004ce37b636 100644
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
 int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
                                uint32_t ctr_idx);
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_name);
 int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
                           uint32_t ctr_idx);
--

2.25.1

#7 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 140 lines
Quoted 139 lines
Quoted 139 lines
---
 hw/riscv/virt.c    | 16 +++++++++++++
 target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 target/riscv/pmu.h |  1 +
 3 files changed, 74 insertions(+)
hw/riscv/virt.c +16 -0
index ff8c0df5cd47..befa9d2c26ac 100644
@@ -30,6 +30,7 @@
 #include "hw/char/serial.h"
 #include "target/riscv/cpu.h"
 #include "hw/core/sysbus-fdt.h"
+#include "target/riscv/pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/boot.h"
@@ -708,6 +709,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
     aplic_phandles[socket] = aplic_s_phandle;
 }
 
+static void create_fdt_pmu(RISCVVirtState *s)
+{
+    char *pmu_name;
+    MachineState *mc = MACHINE(s);
+    RISCVCPU hart = s->soc[0].harts[0];
+
+    pmu_name = g_strdup_printf("/soc/pmu");
+    qemu_fdt_add_subnode(mc->fdt, pmu_name);
+    qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu");
+    riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name);
+
+    g_free(pmu_name);
+}
+
 static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
                                bool is_32_bit, uint32_t *phandle,
                                uint32_t *irq_mmio_phandle,
@@ -1036,6 +1051,7 @@ static void create_fdt(RISCVVirtState *s, const MemMapEntry *memmap,
 
     create_fdt_flash(s, memmap);
     create_fdt_fw_cfg(s, memmap);
+    create_fdt_pmu(s);
 
 update_bootargs:
     if (cmdline && *cmdline) {
target/riscv/pmu.c +57 -0
index a5f504e53c88..b8e56d2b7b8e 100644
@@ -20,11 +20,68 @@
 #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
+ * QEMU. The generic cycle & instruction count events can also be monitored
+ * using programmable counters. In that case, mcycle & minstret must continue
+ * to provide the correct value as well. Heterogeneous PMU per hart is not
+ * supported yet. Thus, number of counters are same across all harts.
+ */
+void riscv_pmu_generate_fdt_node(void *fdt, int num_ctrs, char *pmu_name)
+{
+    uint32_t fdt_event_ctr_map[20] = {};
+    uint32_t cmask;
+
+    /* All the programmable counters can map to any event */
+    cmask = MAKE_32BIT_MASK(3, num_ctrs);
+
+   /*
+    * The event encoding is specified in the SBI specification
+    * Event idx is a 20bits wide number encoded as follows:
+    * event_idx[19:16] = type
+    * event_idx[15:0] = code
+    * The code field in cache events are encoded as follows:
+    * event_idx.code[15:3] = cache_id
+    * event_idx.code[2:1] = op_id
+    * event_idx.code[0:0] = result_id
+    */
+
+   /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */
+   fdt_event_ctr_map[0] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[1] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[2] = cpu_to_be32(cmask | 1 << 0);
+
+   /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */
+   fdt_event_ctr_map[3] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[4] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[5] = cpu_to_be32(cmask | 1 << 2);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[6] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[7] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[8] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[9] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[10] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[11] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[12] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[13] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[14] = cpu_to_be32(cmask);
+
+   /* This a OpenSBI specific DT property documented in OpenSBI docs */
+   qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters",
+                    fdt_event_ctr_map, sizeof(fdt_event_ctr_map));
+}
+
 static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx)
 {
     if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||
target/riscv/pmu.h +1 -1
index 036653627f78..3004ce37b636 100644
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
 int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
                                uint32_t ctr_idx);
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_name);
 int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
                           uint32_t ctr_idx);
--

2.25.1

#8 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 28 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 26 lines · Atish Kumar Patra
Quoted 24 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

Quoted 6 lines
Quoted 2 lines

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

Quoted 140 lines
Quoted 140 lines
Quoted 139 lines
Quoted 139 lines
---
 hw/riscv/virt.c    | 16 +++++++++++++
 target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 target/riscv/pmu.h |  1 +
 3 files changed, 74 insertions(+)
hw/riscv/virt.c +16 -0
index ff8c0df5cd47..befa9d2c26ac 100644
@@ -30,6 +30,7 @@
 #include "hw/char/serial.h"
 #include "target/riscv/cpu.h"
 #include "hw/core/sysbus-fdt.h"
+#include "target/riscv/pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/riscv/virt.h"
 #include "hw/riscv/boot.h"
@@ -708,6 +709,20 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
     aplic_phandles[socket] = aplic_s_phandle;
 }
 
+static void create_fdt_pmu(RISCVVirtState *s)
+{
+    char *pmu_name;
+    MachineState *mc = MACHINE(s);
+    RISCVCPU hart = s->soc[0].harts[0];
+
+    pmu_name = g_strdup_printf("/soc/pmu");
+    qemu_fdt_add_subnode(mc->fdt, pmu_name);
+    qemu_fdt_setprop_string(mc->fdt, pmu_name, "compatible", "riscv,pmu");
+    riscv_pmu_generate_fdt_node(mc->fdt, hart.cfg.pmu_num, pmu_name);
+
+    g_free(pmu_name);
+}
+
 static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
                                bool is_32_bit, uint32_t *phandle,
                                uint32_t *irq_mmio_phandle,
@@ -1036,6 +1051,7 @@ static void create_fdt(RISCVVirtState *s, const MemMapEntry *memmap,
 
     create_fdt_flash(s, memmap);
     create_fdt_fw_cfg(s, memmap);
+    create_fdt_pmu(s);
 
 update_bootargs:
     if (cmdline && *cmdline) {
target/riscv/pmu.c +57 -0
index a5f504e53c88..b8e56d2b7b8e 100644
@@ -20,11 +20,68 @@
 #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
+ * QEMU. The generic cycle & instruction count events can also be monitored
+ * using programmable counters. In that case, mcycle & minstret must continue
+ * to provide the correct value as well. Heterogeneous PMU per hart is not
+ * supported yet. Thus, number of counters are same across all harts.
+ */
+void riscv_pmu_generate_fdt_node(void *fdt, int num_ctrs, char *pmu_name)
+{
+    uint32_t fdt_event_ctr_map[20] = {};
+    uint32_t cmask;
+
+    /* All the programmable counters can map to any event */
+    cmask = MAKE_32BIT_MASK(3, num_ctrs);
+
+   /*
+    * The event encoding is specified in the SBI specification
+    * Event idx is a 20bits wide number encoded as follows:
+    * event_idx[19:16] = type
+    * event_idx[15:0] = code
+    * The code field in cache events are encoded as follows:
+    * event_idx.code[15:3] = cache_id
+    * event_idx.code[2:1] = op_id
+    * event_idx.code[0:0] = result_id
+    */
+
+   /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */
+   fdt_event_ctr_map[0] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[1] = cpu_to_be32(0x00000001);
+   fdt_event_ctr_map[2] = cpu_to_be32(cmask | 1 << 0);
+
+   /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */
+   fdt_event_ctr_map[3] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[4] = cpu_to_be32(0x00000002);
+   fdt_event_ctr_map[5] = cpu_to_be32(cmask | 1 << 2);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[6] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[7] = cpu_to_be32(0x00010019);
+   fdt_event_ctr_map[8] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[9] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[10] = cpu_to_be32(0x0001001B);
+   fdt_event_ctr_map[11] = cpu_to_be32(cmask);
+
+   /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */
+   fdt_event_ctr_map[12] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[13] = cpu_to_be32(0x00010021);
+   fdt_event_ctr_map[14] = cpu_to_be32(cmask);
+
+   /* This a OpenSBI specific DT property documented in OpenSBI docs */
+   qemu_fdt_setprop(fdt, pmu_name, "riscv,event-to-mhpmcounters",
+                    fdt_event_ctr_map, sizeof(fdt_event_ctr_map));
+}
+
 static bool riscv_pmu_counter_valid(RISCVCPU *cpu, uint32_t ctr_idx)
 {
     if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||
target/riscv/pmu.h +1 -1
index 036653627f78..3004ce37b636 100644
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);
 int riscv_pmu_update_event_map(CPURISCVState *env, uint64_t value,
                                uint32_t ctr_idx);
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx);
+void riscv_pmu_generate_fdt_node(void *fdt, int num_counters, char *pmu_name);
 int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value,
                           uint32_t ctr_idx);
--

2.25.1

#9 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 34 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 31 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 28 lines · Atish Kumar Patra
Quoted 15 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

Quoted 9 lines

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Quoted 8 lines
Quoted 3 lines

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

#10 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Thanks,
drew

#11 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 51 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

Quoted 5 lines

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Yah, that was also my understanding. See also U-Boot moving to unify their custom bindings into the linux repo:
https://lore.kernel.org/linux-devicetree/[email protected]/

#12 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 57 lines · On Mon, Nov 28, 2022 at 11:32 PM <[email protected]> wrote:
Quoted 55 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

Quoted 6 lines
Quoted 1 line

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Yah, that was also my understanding. See also U-Boot moving to unify their custom bindings into the linux repo:
https://lore.kernel.org/linux-devicetree/[email protected]/

This adds the U-Boot specific DT properties to the dts schema itself, not Linux kernel DT bindings.

I am not opposed to adding PMU DT bindings to Linux but there should be a clear policy on this.
What about OpenSBI domain DT bindings ?
If every other DT based open source project starts adding their DT binding to the Linux kernel, that may go downhill pretty soon.

Quoted 3 lines
#13 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 62 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 59 lines · On Mon, Nov 28, 2022 at 11:32 PM <[email protected]> wrote:
Quoted 56 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Quoted 7 lines
Quoted 4 lines

Yah, that was also my understanding. See also U-Boot moving to unify their custom bindings into the linux repo:
https://lore.kernel.org/linux-devicetree/[email protected]/

This adds the U-Boot specific DT properties to the dts schema itself, not Linux kernel DT bindings.

Yeah, sorry. I muddled things up a little there. My point was that they are trying to get to a stage where dt-validate and those tools work for them too. I'm not sure were I said "linux repo" rather than "dt-schema repo" when I double checked the file paths in the link before pasting it to make sure it was the dt-schema one.. I blame it being early.

Quoted 5 lines

I am not opposed to adding PMU DT bindings to Linux but there should be a clear policy on this.
What about OpenSBI domain DT bindings ?
If every other DT based open source project starts adding their DT binding to the Linux kernel, that may go downhill pretty soon.

Maybe I am misunderstanding, but I had thought the goal was to get to user-independent bindings. Rob and Krzysztof certainly labour the point that the bindings should not reflect how one operating system's drivers would like to see them & u-boot or FreeBSD using a property is grounds for it not being removed from the bindings in the linux tree.

I'll go and actually ask Rob.

#14 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

+CC Rob, which I probably should've done earlier, so context all preserved

Quoted 68 lines · Conor Dooley
Quoted 67 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 63 lines · On Mon, Nov 28, 2022 at 11:32 PM <[email protected]> wrote:
Quoted 51 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'} From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

Quoted 5 lines

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Yah, that was also my understanding. See also U-Boot moving to unify their custom bindings into the linux repo:
https://lore.kernel.org/linux-devicetree/[email protected]/

Quoted 14 lines
Quoted 2 lines

This adds the U-Boot specific DT properties to the dts schema itself, not Linux kernel DT bindings.

Yeah, sorry. I muddled things up a little there. My point was that they are trying to get to a stage where dt-validate and those tools work for them too. I'm not sure were I said "linux repo" rather than "dt-schema repo" when I double checked the file paths in the link before pasting it to make sure it was the dt-schema one.. I blame it being early.

Quoted 5 lines

I am not opposed to adding PMU DT bindings to Linux but there should be a clear policy on this.
What about OpenSBI domain DT bindings ?
If every other DT based open source project starts adding their DT binding to the Linux kernel, that may go downhill pretty soon.

Rob, perhaps you can be a source of clarity here :) My early morning muddling didn't help things.

Quoted 7 lines

Maybe I am misunderstanding, but I had thought the goal was to get to user-independent bindings. Rob and Krzysztof certainly labour the point that the bindings should not reflect how one operating system's drivers would like to see them & u-boot or FreeBSD using a property is grounds for it not being removed from the bindings in the linux tree.

I'll go and actually ask Rob.

I did go & ask Rob, to which he said "I'll apply it even if no driver."

Do you want to whip up a binding, or shall I?

#15 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 98 lines · On Tue, Nov 29, 2022 at 3:54 PM <[email protected]> wrote:

+CC Rob, which I probably should've done earlier, so context all preserved

Quoted 68 lines · Conor Dooley
Quoted 67 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 63 lines · On Mon, Nov 28, 2022 at 11:32 PM <[email protected]> wrote:
Quoted 51 lines · Andrew Jones

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 38 lines · On Mon, Nov 28, 2022 at 09:10:03PM +0000, [email protected] wrote:
Quoted 37 lines · Atish Kumar Patra

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 34 lines · On Mon, Nov 28, 2022 at 12:38 PM <[email protected]> wrote:
Quoted 22 lines · Atish Kumar Patra
Quoted 21 lines · Conor Dooley
Quoted 5 lines · Atish Patra

Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.

Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.

Hey Atish!

I was fiddling with dumping the virt machine dtb again today to check some dt-binding changes I was making for the isa string would play nicely with the virt machine & I noticed that this patch has introduced a new validation failure:

./build/qemu-system-riscv64 -nographic -machine virt,dumpdtb=qemu.dtb

dt-validate -p ../linux/Documentation/devicetree/bindings/processed-schema.json qemu.dtb
/home/conor/stuff/qemu/qemu.dtb: soc: pmu: {'riscv,event-to-mhpmcounters': [[1, 1, 524281, 2, 2, 524284, 65561, 65561, 524280, 65563, 65563, 524280, 65569, 65569, 524280, 0, 0, 0, 0, 0]], 'compatible': ['riscv,pmu']} should not be valid under {'type': 'object'}
From schema: /home/conor/.local/lib/python3.10/site-packages/dtschema/schemas/simple-bus.yaml

Quoted 6 lines
Quoted 3 lines

I assume this is the aforementioned "dummy" node & you have no intention of creating a binding for this?

It is a dummy node from Linux kernel perspective. OpenSbi use this node to figure out the hpmcounter mappings.

Aye, but should it not have a binding anyway, since they're not meant to be linux specific?

Quoted 9 lines
Quoted 5 lines

It is documented in OpenSBI.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md

Are you suggesting that any non-Linux specific DT nodes should be part of Linux DT binding as well ?

I thought the point was that they were *not* meant to be linux specific, just happening to be housed there.

Quoted 5 lines

I'm not sure if there's an official policy on where DT nodes should be specified, but it looks like Samuel's opinion is that they should live in the Linux kernel, whether they're used there or not [1].

[1] http://lists.infradead.org/pipermail/opensbi/2022-October/003522.html

Yah, that was also my understanding. See also U-Boot moving to unify their custom bindings into the linux repo:
https://lore.kernel.org/linux-devicetree/[email protected]/

Quoted 14 lines
Quoted 2 lines

This adds the U-Boot specific DT properties to the dts schema itself, not Linux kernel DT bindings.

Yeah, sorry. I muddled things up a little there. My point was that they are trying to get to a stage where dt-validate and those tools work for them too. I'm not sure were I said "linux repo" rather than "dt-schema repo" when I double checked the file paths in the link before pasting it to make sure it was the dt-schema one.. I blame it being early.

Quoted 5 lines

I am not opposed to adding PMU DT bindings to Linux but there should be a clear policy on this.
What about OpenSBI domain DT bindings ?
If every other DT based open source project starts adding their DT binding to the Linux kernel, that may go downhill pretty soon.

Rob, perhaps you can be a source of clarity here :) My early morning muddling didn't help things.

Quoted 6 lines

Maybe I am misunderstanding, but I had thought the goal was to get to user-independent bindings. Rob and Krzysztof certainly labour the point that the bindings should not reflect how one operating system's drivers would like to see them & u-boot or FreeBSD using a property is grounds for it not being removed from the bindings in the linux tree.

Quoted 4 lines
Quoted 1 line

I'll go and actually ask Rob.

I did go & ask Rob, to which he said "I'll apply it even if no driver."

In Linux DT binding or dt schema repo ? I am a bit confused now as we talked about both above :).

Quoted 2 lines

Do you want to whip up a binding, or shall I?

#16 lore raw

Re: [PATCH v14 4/5] hw/riscv: virt: Add PMU DT node to the device tree

Quoted 2 lines · Atish Kumar Patra

In Linux DT binding or dt schema repo ? I am a bit confused now as we talked about both above :).

I asked about a dt-binding and not a schema change, so the former ;)

#17 lore raw

PATCH v14 5/5 target/riscv: Update the privilege field for sscofpmf CSRs

In reply to Atish Patra

The sscofpmf extension was ratified as a part of priv spec v1.12. Mark the csr_ops accordingly.

---
 target/riscv/csr.c | 90 ++++++++++++++++++++++++++++++----------------
 1 file changed, 60 insertions(+), 30 deletions(-)
target/riscv/csr.c +60 -30
index 2151e280a868..b96db1b62b38 100644
@@ -4067,63 +4067,92 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
                              write_mhpmevent                           },
 
     [CSR_MHPMEVENT3H]    = { "mhpmevent3h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT4H]    = { "mhpmevent4h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT5H]    = { "mhpmevent5h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT6H]    = { "mhpmevent6h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT7H]    = { "mhpmevent7h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT8H]    = { "mhpmevent8h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT9H]    = { "mhpmevent9h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT10H]   = { "mhpmevent10h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT11H]   = { "mhpmevent11h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT12H]   = { "mhpmevent12h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT13H]   = { "mhpmevent13h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT14H]   = { "mhpmevent14h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT15H]   = { "mhpmevent15h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT16H]   = { "mhpmevent16h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT17H]   = { "mhpmevent17h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT18H]   = { "mhpmevent18h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT19H]   = { "mhpmevent19h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT20H]   = { "mhpmevent20h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT21H]   = { "mhpmevent21h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT22H]   = { "mhpmevent22h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT23H]   = { "mhpmevent23h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT24H]   = { "mhpmevent24h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT25H]   = { "mhpmevent25h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT26H]   = { "mhpmevent26h",    sscofpmf,  read_mhpmeventh,
-                              write_mhpmeventh                          },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT27H]   = { "mhpmevent27h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT28H]   = { "mhpmevent28h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT29H]   = { "mhpmevent29h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT30H]   = { "mhpmevent30h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
     [CSR_MHPMEVENT31H]   = { "mhpmevent31h",    sscofpmf,  read_mhpmeventh,
-                             write_mhpmeventh                           },
+                             write_mhpmeventh,
+                             .min_priv_ver = PRIV_VERSION_1_12_0        },
 
     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_hpmcounterh },
     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_hpmcounterh },
@@ -4213,7 +4242,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
                              write_mhpmcounterh                         },
     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32,  read_hpmcounterh,
                              write_mhpmcounterh                         },
-    [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf,  read_scountovf },
+    [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf,  read_scountovf,
+                             .min_priv_ver = PRIV_VERSION_1_12_0 },
 
 #endif /* !CONFIG_USER_ONLY */
 };
Signature · 1 line
2.25.1
#18 lore raw
In reply to Atish Patra
Quoted 135 lines · Atish Patra

The latest version of the SBI specification includes a Performance Monitoring Unit(PMU) extension[1] which allows the supervisor to start/stop/configure various PMU events. The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension[2] allows the perf like tool to handle overflow interrupts and filtering support.

This series implements remaining PMU infrastructure to support PMU in virt machine. The first seven patches from the original series have been merged already.

This will allow us to add any PMU events in future. Currently, this series enables the following omu events.
1. cycle count
2. instruction count
3. DTLB load/store miss
4. ITLB prefetch miss

The first two are computed using host ticks while last three are counted during cpu_tlb_fill. We can do both sampling and count from guest userspace. This series has been tested on both RV64 and RV32. Both Linux[3] and Opensbi[4] patches are required to get the perf working.

Here is an output of perf stat/report while running hackbench with latest OpenSBI & Linux kernel.

Perf stat:
==========
[root@fedora-riscv ~]# perf stat -e cycles -e instructions -e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses \

Quoted 1 line

perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

     Total time: 0.265 [sec]

Performance counter stats for 'perf bench sched messaging -g 1 -l 10':

     4,167,825,362      cycles
     4,166,609,256      instructions              #    1.00  insn per cycle
         3,092,026      dTLB-load-misses
           258,280      dTLB-store-misses
         2,068,966      iTLB-load-misses
       0.585791767 seconds time elapsed

       0.373802000 seconds user
       1.042359000 seconds sys

Perf record:
============
[root@fedora-riscv ~]# perf record -e cycles -e instructions \

Quoted 2 lines

-e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses -c 10000 \
perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

     Total time: 1.397 [sec]

[ perf record: Woken up 10 times to write data ]
Check IO/CPU overload!
[ perf record: Captured and wrote 8.211 MB perf.data (214486 samples) ]

[root@fedora-riscv riscv]# perf report
Available samples

107K cycles                                                                    ◆
107K instructions                                                              ▒
250 dTLB-load-misses                                                           ▒
13 dTLB-store-misses                                                           ▒

172 iTLB-load-misses
..

Changes from v13->v14:
1. Added sanity check for the hashtable in pmu.c

Changes from v12->v13:
1. Rebased on top of the apply-next.
2. Addressed comments about space & comment block.

Changes from v11->v12:
1. Rebased on top of the apply-next.
2. Aligned the write function & .min_priv to the previous line.
3. Fixed the FDT generations for multi-socket scenario.
4. Dropped interrupt property from the DT.
5. Generate illegal instruction fault instead of virtual instruction fault for VS/VU access while mcounteren is not set.

Changes from v10->v11:
1. Rebased on top of the master where first 7 patches were already merged.
2. Removed unnecessary additional check in ctr predicate function.
3. Removed unnecessary priv version checks in mcountinhibit read/write.
4. Added Heiko's reviewed-by/tested-by tags.

Changes from v8->v9:
1. Added the write_done flags to the vmstate.
2. Fixed the hpmcounter read access from M-mode.

Changes from v7->v8:
1. Removeding ordering constraints for mhpmcounter & mhpmevent.

Changes from v6->v7:
1. Fixed all the compilation errors for the usermode.

Changes from v5->v6:
1. Fixed compilation issue with PATCH 1.
2. Addressed other comments.

Changes from v4->v5:
1. Rebased on top of the -next with following patches.
- isa extension
- priv 1.12 spec
2. Addressed all the comments on v4
3. Removed additional isa-ext DT node in favor of riscv,isa string update

Changes from v3->v4:
1. Removed the dummy events from pmu DT node.
2. Fixed pmu_avail_counters mask generation.
3. Added a patch to simplify the predicate function for counters.

Changes from v2->v3:
1. Addressed all the comments on PATCH1-4.
2. Split patch1 into two separate patches.
3. Added explicit comments to explain the event types in DT node.
4. Rebased on latest Qemu.

Changes from v1->v2:
1. Dropped the ACks from v1 as signficant changes happened after v1.
2. sscofpmf support.
3. A generic counter management framework.

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://drive.google.com/file/d/171j4jFjIkKdj5LWcExphq4xG_2sihbfd/edit
[3] https://github.com/atishp04/qemu/tree/riscv_pmu_v14

Quoted 6 lines

Atish Patra (5):
target/riscv: Add sscofpmf extension support target/riscv: Simplify counter predicate function target/riscv: Add few cache related PMU events hw/riscv: virt: Add PMU DT node to the device tree target/riscv: Update the privilege field for sscofpmf CSRs

Sorry, but this doesn't apply. Are you able to rebase it?

Alistair

Quoted 16 lines
hw/riscv/virt.c           |  16 ++
target/riscv/cpu.c        |  12 ++
target/riscv/cpu.h        |  25 +++
target/riscv/cpu_bits.h   |  55 +++++
target/riscv/cpu_helper.c |  25 +++
target/riscv/csr.c        | 304 +++++++++++++++++----------
target/riscv/machine.c    |   1 +
target/riscv/pmu.c        | 425 +++++++++++++++++++++++++++++++++++++-
target/riscv/pmu.h        |   8 +
9 files changed, 760 insertions(+), 111 deletions(-)

--
2.25.1

#19 lore raw
Quoted 151 lines · Alistair Francis
Quoted 2 lines · Atish Patra

The latest version of the SBI specification includes a Performance

Monitoring

Quoted 1 line

Unit(PMU) extension[1] which allows the supervisor to

start/stop/configure

Quoted 1 line

various PMU events. The Sscofpmf ('Ss' for Privileged arch and

Supervisor-level

Quoted 8 lines

extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension[2] allows the perf like tool to handle overflow interrupts and filtering support.

This series implements remaining PMU infrastructure to support PMU in virt machine. The first seven patches from the original series have been merged already.

Quoted 8 lines

This will allow us to add any PMU events in future. Currently, this series enables the following omu events.
1. cycle count
2. instruction count
3. DTLB load/store miss
4. ITLB prefetch miss

The first two are computed using host ticks while last three are counted

during

Quoted 2 lines

cpu_tlb_fill. We can do both sampling and count from guest userspace. This series has been tested on both RV64 and RV32. Both Linux[3] and

Opensbi[4]

Quoted 8 lines

patches are required to get the perf working.

Here is an output of perf stat/report while running hackbench with latest OpenSBI & Linux kernel.

Perf stat:
==========
[root@fedora-riscv ~]# perf stat -e cycles -e instructions -e

dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses \

Quoted 11 lines
Quoted 1 line

perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

     Total time: 0.265 [sec]

Performance counter stats for 'perf bench sched messaging -g 1 -l 10':

     4,167,825,362      cycles
     4,166,609,256      instructions              #    1.00  insn per

cycle

Quoted 18 lines
         3,092,026      dTLB-load-misses
           258,280      dTLB-store-misses
         2,068,966      iTLB-load-misses
       0.585791767 seconds time elapsed

       0.373802000 seconds user
       1.042359000 seconds sys

Perf record:
============
[root@fedora-riscv ~]# perf record -e cycles -e instructions \

Quoted 2 lines

-e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses -c 10000 \
perf bench sched messaging -g 1 -l 10

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run

Quoted 8 lines
     Total time: 1.397 [sec]

[ perf record: Woken up 10 times to write data ]
Check IO/CPU overload!
[ perf record: Captured and wrote 8.211 MB perf.data (214486 samples) ]

[root@fedora-riscv riscv]# perf report
Available samples
107K cycles

◆

Quoted 1 line

107K instructions

▒

Quoted 1 line

250 dTLB-load-misses

▒

Quoted 1 line

13 dTLB-store-misses

▒

Quoted 10 lines

172 iTLB-load-misses
..

Changes from v13->v14:
1. Added sanity check for the hashtable in pmu.c

Changes from v12->v13:
1. Rebased on top of the apply-next.
2. Addressed comments about space & comment block.

Quoted 6 lines

Changes from v11->v12:
1. Rebased on top of the apply-next.
2. Aligned the write function & .min_priv to the previous line.
3. Fixed the FDT generations for multi-socket scenario.
4. Dropped interrupt property from the DT.
5. Generate illegal instruction fault instead of virtual instruction

fault

Quoted 4 lines

for VS/VU access while mcounteren is not set.

Changes from v10->v11:
1. Rebased on top of the master where first 7 patches were already

merged.

Quoted 36 lines

2. Removed unnecessary additional check in ctr predicate function.
3. Removed unnecessary priv version checks in mcountinhibit read/write.
4. Added Heiko's reviewed-by/tested-by tags.

Changes from v8->v9:
1. Added the write_done flags to the vmstate.
2. Fixed the hpmcounter read access from M-mode.

Changes from v7->v8:
1. Removeding ordering constraints for mhpmcounter & mhpmevent.

Changes from v6->v7:
1. Fixed all the compilation errors for the usermode.

Changes from v5->v6:
1. Fixed compilation issue with PATCH 1.
2. Addressed other comments.

Changes from v4->v5:
1. Rebased on top of the -next with following patches.
- isa extension
- priv 1.12 spec
2. Addressed all the comments on v4
3. Removed additional isa-ext DT node in favor of riscv,isa string update

Changes from v3->v4:
1. Removed the dummy events from pmu DT node.
2. Fixed pmu_avail_counters mask generation.
3. Added a patch to simplify the predicate function for counters.

Changes from v2->v3:
1. Addressed all the comments on PATCH1-4.
2. Split patch1 into two separate patches.
3. Added explicit comments to explain the event types in DT node.
4. Rebased on latest Qemu.

Quoted 6 lines

Changes from v1->v2:
1. Dropped the ACks from v1 as signficant changes happened after v1.
2. sscofpmf support.
3. A generic counter management framework.

[1]

https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc

Quoted 1 line

[2]

https://drive.google.com/file/d/171j4jFjIkKdj5LWcExphq4xG_2sihbfd/edit

Quoted 2 lines

[3] https://github.com/atishp04/qemu/tree/riscv_pmu_v14

Quoted 10 lines
Quoted 6 lines

Atish Patra (5):
target/riscv: Add sscofpmf extension support target/riscv: Simplify counter predicate function target/riscv: Add few cache related PMU events hw/riscv: virt: Add PMU DT node to the device tree target/riscv: Update the privilege field for sscofpmf CSRs

Sorry, but this doesn't apply. Are you able to rebase it?

I am a bit confused. Your PULL request on Sep 7th already included this & sstc series.
I can see the patches in upstream qemu as well.

Quoted 19 lines

Alistair

Quoted 16 lines
hw/riscv/virt.c           |  16 ++
target/riscv/cpu.c        |  12 ++
target/riscv/cpu.h        |  25 +++
target/riscv/cpu_bits.h   |  55 +++++
target/riscv/cpu_helper.c |  25 +++
target/riscv/csr.c        | 304 +++++++++++++++++----------
target/riscv/machine.c    |   1 +
target/riscv/pmu.c        | 425 +++++++++++++++++++++++++++++++++++++-
target/riscv/pmu.h        |   8 +
9 files changed, 760 insertions(+), 111 deletions(-)

--
2.25.1