* [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS
@ 2026-11-18 8:13 Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
This RFC exposes Precise Decoded Instruction Sampling (PDIS) as a
per-CPU perf event on RV64. It targets the Smpdis/Sspdis draft at
2de60e18118ce78e2da82ec33296345c94d8560c:
https://github.com/riscv/riscv-performance-event-sampling/blob/2de60e18118ce78e2da82ec33296345c94d8560c/modules/ROOT/pages/spdis.adoc
The draft defines two destinations for sample records. With MEM=0,
hardware records samples in indirect supervisor control and status
registers (CSRs). With MEM=1, hardware writes records to the PDIS Memory
Buffer. This series implements the MEM=0 path and reads records from
the sample data CSRs.
PDIS selects instructions with its own counter and delivers records on
the local counter overflow interrupt shared with the SBI PMU. The
driver samples at a fixed period and reports the recorded PC. Each
event selects either host user mode or host supervisor mode. CSR
numbers and the state-enable bit are provisional. Probing requires a
matching CPU node and explicit boot opt-in.
Open questions
--------------
- The driver registers a dedicated perf PMU for per-CPU sampling. Is
this a reasonable starting point for PDIS support in perf?
- The model cancels in-flight selections when software sets inhibit and
clears spdisctl. What sequence guarantees that an old selection can no
longer publish a record before Linux switches tasks or events?
- The CSR record lacks a privilege field. If user and supervisor
sampling are enabled together, how should perf determine the mode of
the sampled instruction? Would it make sense to include the mode in
the record itself?
- The driver reports the recorded PC but rejects perf's precise_ip
requests. What guarantees about instruction selection and the recorded
PC would justify advertising a precise_ip level for PDIS?
- COUNT can reload several times before software reads it, while OF is
a single sticky bit. What should read() on a PDIS perf event report
when the driver can only estimate the number of matching instructions?
On firmware supporting SBI PMU snapshots, restarting ordinary counters
also requires the existing counter_idx_base fix posted by liutong:
https://lore.kernel.org/r/20260823044337.517125-1-liutong@iscas.ac.cn/
The reference OpenSBI firmware uses the non-snapshot path.
Reproduction
------------
The matching experimental trees are:
QEMU: https://github.com/bows7ring/qemu/tree/4ce5dec939149ccef78358d68ddc15df1fc0c55d
OpenSBI: https://github.com/bows7ring/opensbi/tree/1cdbbd84f83d80fdc6d62ea3347a4417b388784c
Build generic OpenSBI with CONFIG_XPDIS_TEST=y and Linux with
CONFIG_RISCV_PDIS_CSR=y. Boot virt with the resulting fw_dynamic.bin,
Image and a guest initramfs containing perf and the workload.
Use these QEMU options:
-machine virt -accel tcg -smp 2 -m 512M -icount shift=0,sleep=off
-cpu rv64,smstateen=on,smcsrind=on,sscsrind=on,sscofpmf=on,smcdeleg=off,ssccfg=off,x-pdis-regs=on,x-pdis-sample=on
In the test DT, append sspdis to each CPU's riscv,isa-extensions and
set riscv,pdis-test-model = "csr-v1". Add riscv_pdis_test=1 to the
kernel command line. This setup uses provisional CSR encodings.
The test ran the following from a CPU 1-pinned init. The /workload
binary pins itself to CPU 0 and loops over a labelled load instruction
400,000 times (one load and one store per iteration).
/perf record --no-inherit -m 8 -a -C 0 \
-e 'riscv_pdis_test/select=1/u' -c 4099 \
-o /tmp/pdis-u.data -- /workload
/perf script -i /tmp/pdis-u.data -F ip,event
On the QEMU model, perf received the expected PDIS samples in both
user-mode and kernel-mode runs.
Zhanpeng Zhang (5):
dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
riscv: Detect Smpdis and Sspdis ISA extensions
perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
perf: riscv: Add PDIS event and counter callbacks
perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
.../admin-guide/kernel-parameters.txt | 10 +
.../devicetree/bindings/riscv/extensions.yaml | 19 +
MAINTAINERS | 1 +
arch/riscv/include/asm/hwcap.h | 2 +
arch/riscv/kernel/cpufeature.c | 2 +
drivers/perf/Kconfig | 13 +
drivers/perf/Makefile | 1 +
drivers/perf/riscv_pdis.c | 558 ++++++++++++++++++
drivers/perf/riscv_pdis.h | 23 +
drivers/perf/riscv_pmu_sbi.c | 43 +-
10 files changed, 660 insertions(+), 12 deletions(-)
create mode 100644 drivers/perf/riscv_pdis.c
create mode 100644 drivers/perf/riscv_pdis.h
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.50.1 (Apple Git-155)
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:23 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Describe the machine and supervisor PDIS extensions using the pinned
sampling draft. Machine support alone does not grant supervisor access.
These are draft bindings, not ratified extension or CSR allocations.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../devicetree/bindings/riscv/extensions.yaml | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml
index 79491ef8f86a..afda76b9c7c4 100644
--- a/Documentation/devicetree/bindings/riscv/extensions.yaml
+++ b/Documentation/devicetree/bindings/riscv/extensions.yaml
@@ -214,6 +214,15 @@ properties:
ratified at commit d70011dde6c2 ("Update to ratified state")
of riscv-j-extension.
+ - const: smpdis
+ description: |
+ The draft Smpdis machine-level extension for Precise Decoded
+ Instruction Sampling, as described at commit
+ 2de60e18118ce78e2da82ec33296345c94d8560c of the
+ riscv-performance-event-sampling specification. It defines
+ instruction selection, filtering and sample collection across
+ privilege levels. This does not imply supervisor access to PDIS.
+
- const: smstateen
description: |
The standard Smstateen extension for controlling access to CSRs
@@ -269,6 +278,16 @@ properties:
ratified at commit d70011dde6c2 ("Update to ratified state")
of riscv-j-extension.
+ - const: sspdis
+ description: |
+ The draft Sspdis supervisor-level extension for Precise Decoded
+ Instruction Sampling, as described at commit
+ 2de60e18118ce78e2da82ec33296345c94d8560c of the
+ riscv-performance-event-sampling specification. It provides the
+ supervisor-visible PDIS registers and behavior, excluding the
+ machine-only state. Firmware must make this state accessible to
+ supervisor software before reporting this extension.
+
- const: ssqosid
description: |
The standard Ssqosid extension for Quality of Service ID is
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v1 2/5] riscv: Detect Smpdis and Sspdis ISA extensions
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Register separate machine and supervisor PDIS extension bits with the
existing ISA parser. Reuse the DT extension-list and legacy/RHCT parsing
paths without adding userspace hwprobe bits or inferring S-mode access
from an M-mode declaration.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
arch/riscv/include/asm/hwcap.h | 2 ++
arch/riscv/kernel/cpufeature.c | 2 ++
2 files changed, 4 insertions(+)
diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index b5952db9259f..0eea8158b4af 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -126,6 +126,8 @@
#define RISCV_ISA_EXT_ZIC64B 116
#define RISCV_ISA_EXT_SV48 117
#define RISCV_ISA_EXT_SV57 118
+#define RISCV_ISA_EXT_SMPDIS 119
+#define RISCV_ISA_EXT_SSPDIS 120
#define RISCV_ISA_EXT_XLINUXENVCFG 127
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 72bce616491f..982231fa290a 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -651,12 +651,14 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
__RISCV_ISA_EXT_DATA(smcsrind, RISCV_ISA_EXT_SMCSRIND),
__RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
__RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
+ __RISCV_ISA_EXT_DATA(smpdis, RISCV_ISA_EXT_SMPDIS),
__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
__RISCV_ISA_EXT_DATA_VALIDATE(ssccfg, RISCV_ISA_EXT_SSCCFG, riscv_ext_ssccfg_validate),
__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
__RISCV_ISA_EXT_DATA(sscsrind, RISCV_ISA_EXT_SSCSRIND),
__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
+ __RISCV_ISA_EXT_DATA(sspdis, RISCV_ISA_EXT_SSPDIS),
__RISCV_ISA_EXT_DATA(ssqosid, RISCV_ISA_EXT_SSQOSID),
__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
__RISCV_ISA_EXT_DATA(svade, RISCV_ISA_EXT_SVADE),
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
The draft leaves CSR encodings unallocated. Require an explicit boot
opt-in, the csr-v1 test-model marker and the supervisor ISA extensions
before accessing the provisional registers. Probe the control, counter
width and filters with exception fixups and leave the unit inhibited.
Run the probe during CPU bringup on the standard local counter overflow
interrupt path. Quiesce the unit at CPU teardown. The experiment uses
RV64 with KVM disabled and matching firmware that excludes M/VS/VU.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../admin-guide/kernel-parameters.txt | 10 ++
MAINTAINERS | 1 +
drivers/perf/Kconfig | 13 ++
drivers/perf/Makefile | 1 +
drivers/perf/riscv_pdis.c | 137 ++++++++++++++++++
drivers/perf/riscv_pdis.h | 13 ++
drivers/perf/riscv_pmu_sbi.c | 5 +
7 files changed, 180 insertions(+)
create mode 100644 drivers/perf/riscv_pdis.c
create mode 100644 drivers/perf/riscv_pdis.h
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..dc15ddd29cf4 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6722,6 +6722,16 @@ Kernel parameters
fcfi Disable user forward CFI ABI to userspace even if the
landing pad extension is available.
+ riscv_pdis_test= [RISCV,EARLY]
+ Format: <bool>
+ Authorize the experimental PDIS PMU to probe temporary
+ CSR encodings. Requires CONFIG_RISCV_PDIS_CSR,
+ CONFIG_KVM=n and matching test firmware. The private
+ test DT must mark each matching CPU with
+ riscv,pdis-test-model = "csr-v1".
+ Firmware must also advertise Sspdis on each usable CPU.
+ Default: disabled. Do not enable on production hardware.
+
ro [KNL] Mount root device read-only on boot
rodata= [KNL,EARLY]
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..3e695d4c5fc1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24128,6 +24128,7 @@ M: Atish Patra <atish.patra@linux.dev>
R: Anup Patel <anup@brainfault.org>
L: linux-riscv@lists.infradead.org
S: Supported
+F: drivers/perf/riscv_pdis*
F: drivers/perf/riscv_pmu.c
F: drivers/perf/riscv_pmu_legacy.c
F: drivers/perf/riscv_pmu_sbi.c
diff --git a/drivers/perf/Kconfig b/drivers/perf/Kconfig
index 245e7bb763b9..791d252d4952 100644
--- a/drivers/perf/Kconfig
+++ b/drivers/perf/Kconfig
@@ -105,6 +105,19 @@ config RISCV_PMU_SBI
full perf feature support i.e. counter overflow, privilege mode
filtering, counter configuration.
+config RISCV_PDIS_CSR
+ bool "Experimental RISC-V PDIS CSR-output perf PMU"
+ depends on RISCV_PMU_SBI && 64BIT && MMU && !RISCV_M_MODE && EXPERT
+ help
+ Build an experimental per-CPU PDIS sampling PMU. This requires a
+ matching test model and firmware with the temporary CSR encodings.
+ Firmware must report Sspdis on CPUs authorized to use PDIS.
+ The riscv_pdis_test=1 boot parameter explicitly authorizes probing.
+ Task sampling, mixed privilege and KVM are not supported. Do not
+ enable the boot parameter on existing production hardware.
+
+ If unsure, say N.
+
config STARFIVE_STARLINK_PMU
depends on ARCH_STARFIVE || COMPILE_TEST
depends on 64BIT
diff --git a/drivers/perf/Makefile b/drivers/perf/Makefile
index eb8a022dad9a..203e6227dc6b 100644
--- a/drivers/perf/Makefile
+++ b/drivers/perf/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_QCOM_L3_PMU) += qcom_l3_pmu.o
obj-$(CONFIG_RISCV_PMU) += riscv_pmu.o
obj-$(CONFIG_RISCV_PMU_LEGACY) += riscv_pmu_legacy.o
obj-$(CONFIG_RISCV_PMU_SBI) += riscv_pmu_sbi.o
+obj-$(CONFIG_RISCV_PDIS_CSR) += riscv_pdis.o
obj-$(CONFIG_STARFIVE_STARLINK_PMU) += starfive_starlink_pmu.o
obj-$(CONFIG_THUNDERX2_PMU) += thunderx2_pmu.o
obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
new file mode 100644
index 000000000000..b7671e5ad63e
--- /dev/null
+++ b/drivers/perf/riscv_pdis.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Experimental RV64 PDIS CSR-output PMU for an explicitly matching model. */
+
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/of.h>
+#include <linux/percpu.h>
+
+#include <asm/asm-extable.h>
+#include <asm/cpufeature.h>
+#include <asm/csr.h>
+
+#include "riscv_pdis.h"
+
+/* Draft spdis.adoc at 2de60e18118ce78e2da82ec33296345c94d8560c. */
+#define RISCV_PDIS_COUNTER_BIT BIT(1)
+/* Local experimental encodings, not architectural CSR allocations. */
+#define RISCV_PDIS_CSR_CTL 0x5e0
+#define RISCV_PDIS_CSR_COUNTER 0x5e1
+#define RISCV_PDIS_CSR_EVMASK 0x5e2
+#define RISCV_PDIS_CSR_EVMATCH 0x5e3
+#define RISCV_PDIS_CSR_FILTER 0x5e4
+#define RISCV_PDIS_CSR_VSCTL 0x6d0
+#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
+
+struct pdis_cpu {
+ bool ready;
+};
+
+static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
+static cpumask_t pdis_cpus;
+static bool pdis_test;
+
+static int __init pdis_test_setup(char *str)
+{
+ return kstrtobool(str, &pdis_test);
+}
+early_param("riscv_pdis_test", pdis_test_setup);
+
+/* Private test DT marker, not a binding for architectural PDIS. */
+static bool pdis_test_model_matches(unsigned int cpu)
+{
+ struct device_node *node = of_cpu_device_node_get(cpu);
+ const char *model;
+ bool matches;
+
+ matches = !of_property_read_string(node, "riscv,pdis-test-model", &model) &&
+ !strcmp(model, "csr-v1");
+ of_node_put(node);
+ return matches;
+}
+
+/* Used only during opt-in probing; missing/unauthorized CSRs must not oops. */
+static __always_inline int pdis_read_csr(unsigned int csr, unsigned long *value)
+{
+ asm goto("1: csrr %[value], %[csr]\n"
+ _ASM_EXTABLE(1b, %l[fault])
+ : [value] "=r" (*value) : [csr] "i" (csr) : "memory" : fault);
+ return 0;
+fault:
+ *value = 0;
+ return -ENODEV;
+}
+
+static __always_inline int pdis_write_csr(unsigned int csr, unsigned long value)
+{
+ asm goto("1: csrw %[csr], %[value]\n"
+ _ASM_EXTABLE(1b, %l[fault])
+ : : [value] "r" (value), [csr] "i" (csr) : "memory" : fault);
+ return 0;
+fault:
+ return -ENODEV;
+}
+
+void riscv_pdis_cpu_starting(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ unsigned long ctl, inhibit, value;
+ unsigned int cpu = smp_processor_id();
+
+ pcpu->ready = false;
+ cpumask_clear_cpu(cpu, &pdis_cpus);
+ if (!pdis_test || IS_ENABLED(CONFIG_KVM) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSPDIS) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSCOFPMF) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSCSRIND) ||
+ !pdis_test_model_matches(cpu))
+ return;
+
+ if (pdis_read_csr(RISCV_PDIS_CSR_CTL, &ctl) ||
+ ctl & ~RISCV_PDIS_CTL_ACC ||
+ pdis_read_csr(CSR_SCOUNTINHIBIT, &inhibit) ||
+ !(inhibit & RISCV_PDIS_COUNTER_BIT))
+ return;
+ if (pdis_write_csr(RISCV_PDIS_CSR_CTL, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_EVMASK, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_EVMATCH, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_FILTER, 0))
+ return;
+ if (pdis_read_csr(RISCV_PDIS_CSR_EVMASK, &value) || value ||
+ pdis_read_csr(RISCV_PDIS_CSR_EVMATCH, &value) || value ||
+ pdis_read_csr(RISCV_PDIS_CSR_FILTER, &value) || value)
+ return;
+ /* The first version requires all 32 COUNT and INITVAL bits. */
+ if (pdis_write_csr(RISCV_PDIS_CSR_COUNTER, ~0UL) ||
+ pdis_read_csr(RISCV_PDIS_CSR_COUNTER, &value) || value != ~0UL ||
+ pdis_write_csr(RISCV_PDIS_CSR_COUNTER, 0))
+ return;
+ if (riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_H)) {
+ if (pdis_read_csr(RISCV_PDIS_CSR_VSCTL, &value) ||
+ value & ~RISCV_PDIS_CTL_ACC ||
+ pdis_read_csr(CSR_HSTATEEN0, &value) || value & RISCV_PDIS_STATEEN)
+ return;
+ }
+ pcpu->ready = true;
+ cpumask_set_cpu(cpu, &pdis_cpus);
+}
+
+static void pdis_quiesce(void)
+{
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ /* The test-model contract requires this to cancel in-flight selections. */
+ csr_write(RISCV_PDIS_CSR_CTL, 0);
+}
+
+void riscv_pdis_cpu_dying(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready)
+ pdis_quiesce();
+ pcpu->ready = false;
+ cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
+}
diff --git a/drivers/perf/riscv_pdis.h b/drivers/perf/riscv_pdis.h
new file mode 100644
index 000000000000..b1ffe7d16ce0
--- /dev/null
+++ b/drivers/perf/riscv_pdis.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __RISCV_PDIS_H
+#define __RISCV_PDIS_H
+
+#ifdef CONFIG_RISCV_PDIS_CSR
+void riscv_pdis_cpu_starting(void);
+void riscv_pdis_cpu_dying(void);
+#else
+static inline void riscv_pdis_cpu_starting(void) { }
+static inline void riscv_pdis_cpu_dying(void) { }
+#endif
+
+#endif /* __RISCV_PDIS_H */
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 21a2078eab98..545b8becf90a 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -27,6 +27,8 @@
#include <asm/vendor_extensions.h>
#include <asm/vendor_extensions/andes.h>
+#include "riscv_pdis.h"
+
#define ALT_SBI_PMU_OVERFLOW(__ovl) \
asm volatile(ALTERNATIVE_2( \
"csrr %0, " __stringify(CSR_SCOUNTOVF), \
@@ -1204,6 +1206,8 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
if (riscv_pmu_use_irq) {
cpu_hw_evt->irq = riscv_pmu_irq;
+ if (riscv_pmu_irq_num == RV_IRQ_PMU)
+ riscv_pdis_cpu_starting();
ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
}
@@ -1216,6 +1220,7 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
{
+ riscv_pdis_cpu_dying();
if (riscv_pmu_use_irq) {
disable_percpu_irq(riscv_pmu_irq);
}
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
` (2 preceding siblings ...)
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Manage the independent PDIS counter as a CPU-wide, fixed-period perf
event. Track partial periods on read and stop, preserve the remaining
period across resumes and claim each observed delta once on nested
reads. COUNT and sticky OF cannot report multiple unobserved reloads,
so the event count is an estimate.
Quiesce PDIS at task boundaries to keep task metadata with its sample.
Keep the remaining interval across a switch. The model must cancel
in-flight selections when software inhibits PDIS and clears spdisctl.
Keep the PMU unregistered until overflow delivery is wired up.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
drivers/perf/riscv_pdis.c | 315 +++++++++++++++++++++++++++++++++++++-
1 file changed, 312 insertions(+), 3 deletions(-)
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
index b7671e5ad63e..9a8fe5e4ab82 100644
--- a/drivers/perf/riscv_pdis.c
+++ b/drivers/perf/riscv_pdis.c
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Experimental RV64 PDIS CSR-output PMU for an explicitly matching model. */
-#include <linux/bits.h>
+#include <linux/bitfield.h>
#include <linux/cpu.h>
+#include <linux/device.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/of.h>
+#include <linux/perf_event.h>
#include <linux/percpu.h>
#include <asm/asm-extable.h>
@@ -24,15 +26,41 @@
#define RISCV_PDIS_CSR_FILTER 0x5e4
#define RISCV_PDIS_CSR_VSCTL 0x6d0
#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_SEL GENMASK_ULL(2, 0)
#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
+#define RISCV_PDIS_CTL_U BIT_ULL(60)
+#define RISCV_PDIS_CTL_S BIT_ULL(61)
+
+enum riscv_pdis_select {
+ RISCV_PDIS_SELECT_ALL,
+ RISCV_PDIS_SELECT_LOAD,
+ RISCV_PDIS_SELECT_STORE,
+ RISCV_PDIS_SELECT_LOAD_STORE,
+ RISCV_PDIS_SELECT_CONTROL,
+};
+
+#define RISCV_PDIS_SAMPLE_TYPES (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
+ PERF_SAMPLE_TIME | PERF_SAMPLE_ID | \
+ PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | \
+ PERF_SAMPLE_PERIOD | PERF_SAMPLE_IDENTIFIER)
+
+/* A policy limit for the experiment, not an architectural minimum period. */
+#define PDIS_MIN_PERIOD 4096
+
+/* prev_count holds the 32-bit position and the overflow accounting bit. */
+#define PDIS_OF_ACCOUNTED BIT_ULL(32)
struct pdis_cpu {
+ struct perf_event *event;
bool ready;
+ bool counter_valid;
+ bool switching;
};
static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
static cpumask_t pdis_cpus;
static bool pdis_test;
+static struct pmu pdis_pmu;
static int __init pdis_test_setup(char *str)
{
@@ -82,6 +110,7 @@ void riscv_pdis_cpu_starting(void)
unsigned int cpu = smp_processor_id();
pcpu->ready = false;
+ pcpu->counter_valid = false;
cpumask_clear_cpu(cpu, &pdis_cpus);
if (!pdis_test || IS_ENABLED(CONFIG_KVM) ||
!riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSPDIS) ||
@@ -119,19 +148,299 @@ void riscv_pdis_cpu_starting(void)
cpumask_set_cpu(cpu, &pdis_cpus);
}
+static void pdis_clear_accounted(struct perf_event *event)
+{
+ local64_t *prev_count;
+ s64 prev;
+
+ if (!event)
+ return;
+ prev_count = &event->hw.prev_count;
+ prev = local64_read(prev_count);
+ while (prev & PDIS_OF_ACCOUNTED) {
+ if (local64_try_cmpxchg(prev_count, &prev, (u32)prev))
+ break;
+ }
+}
+
static void pdis_quiesce(void)
{
csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
/* The test-model contract requires this to cancel in-flight selections. */
csr_write(RISCV_PDIS_CSR_CTL, 0);
+ pdis_clear_accounted(this_cpu_ptr(&pdis_cpu)->event);
}
-void riscv_pdis_cpu_dying(void)
+static int pdis_check_period(struct perf_event *event, u64 period)
+{
+ return period >= PDIS_MIN_PERIOD && period <= U32_MAX ? 0 : -EINVAL;
+}
+
+static int pdis_event_init(struct perf_event *event)
+{
+ struct perf_event *sibling;
+ u64 mode;
+ int ret;
+
+ if (event->attr.type != pdis_pmu.type)
+ return -ENOENT;
+ if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK ||
+ event->attr.inherit || event->attr.freq || event->attr.exclusive ||
+ event->attr.aux_output ||
+ !is_sampling_event(event))
+ return -EOPNOTSUPP;
+ if (!cpumask_test_cpu(event->cpu, &pdis_cpus))
+ return -ENODEV;
+ if (event->attr.config > RISCV_PDIS_SELECT_CONTROL ||
+ event->attr.config1 || event->attr.config2)
+ return -EINVAL;
+ /* Discovery requires host-only execution; an explicit H is optional. */
+ if (event->attr.sample_type & ~RISCV_PDIS_SAMPLE_TYPES ||
+ event->attr.exclude_host ||
+ event->attr.exclude_idle || event->attr.precise_ip ||
+ event->attr.exclude_user == event->attr.exclude_kernel)
+ return -EOPNOTSUPP;
+ if (event->group_leader != event && event->group_leader->pmu == event->pmu)
+ return -EINVAL;
+ for_each_sibling_event(sibling, event->group_leader) {
+ if (sibling != event && sibling->pmu == event->pmu)
+ return -EINVAL;
+ }
+ ret = pdis_check_period(event, event->attr.sample_period);
+ if (ret)
+ return ret;
+ mode = event->attr.exclude_kernel ? RISCV_PDIS_CTL_U : RISCV_PDIS_CTL_S;
+ event->hw.config = FIELD_PREP(RISCV_PDIS_CTL_SEL, event->attr.config) | mode;
+ event->hw.idx = -1;
+ event->attach_state |= PERF_ATTACH_SCHED_CB;
+ return 0;
+}
+
+/* COUNT is frozen by the caller; OF is sticky until acknowledged. */
+static void pdis_event_update(struct perf_event *event)
+{
+ struct hw_perf_event *hwc = &event->hw;
+ u64 period = hwc->last_period;
+ s64 prev = local64_read(&hwc->prev_count);
+ s64 delta, next, step;
+ u32 progress;
+
+ do {
+ u32 count = csr_read(RISCV_PDIS_CSR_COUNTER);
+ bool pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
+ bool accounted = prev & PDIS_OF_ACCOUNTED;
+
+ progress = count + (u32)period;
+ /* COUNT may have overflowed without reloading yet. */
+ if (progress >= period)
+ return;
+
+ /* OF can reveal one wrap; multiple unseen wraps cannot be recovered. */
+ step = (s64)progress - (u32)prev;
+ delta = step;
+ if (step < 0 || (pending && !accounted))
+ delta += period;
+ next = progress;
+ if (accounted || pending || step < 0)
+ next |= PDIS_OF_ACCOUNTED;
+ } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, next));
+
+ local64_add(delta, &event->count);
+ /* Apply the position change without overwriting a nested update. */
+ local64_sub(step, &hwc->period_left);
+}
+
+static void pdis_update_and_quiesce(struct perf_event *event)
+{
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pdis_event_update(event);
+ pdis_quiesce();
+}
+
+static int pdis_program(struct perf_event *event, bool reload)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct hw_perf_event *hwc = &event->hw;
+ u64 period = hwc->sample_period;
+ u64 left = local64_read(&hwc->period_left);
+ u64 counter = 0;
+ unsigned long actual;
+
+ pdis_quiesce();
+ if (reload) {
+ u32 initval = -(u32)period;
+ u32 count;
+
+ if (!left || left > period)
+ left = period;
+ count = -(u32)left;
+ counter = (u64)initval << 32 | count;
+ csr_write(RISCV_PDIS_CSR_COUNTER, counter);
+ }
+ csr_write(RISCV_PDIS_CSR_CTL, hwc->config);
+ actual = csr_read(RISCV_PDIS_CSR_CTL);
+ if ((actual & ~RISCV_PDIS_CTL_ACC) != hwc->config ||
+ (reload && csr_read(RISCV_PDIS_CSR_COUNTER) != counter)) {
+ pcpu->counter_valid = false;
+ pdis_quiesce();
+ return -EOPNOTSUPP;
+ }
+ if (reload) {
+ hwc->last_period = period;
+ local64_set(&hwc->prev_count, period - left);
+ local64_set(&hwc->period_left, left);
+ pcpu->counter_valid = true;
+ }
+ return 0;
+}
+
+static void pdis_enable(struct perf_event *event)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ event->hw.state = 0;
+ if (!pcpu->switching)
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ perf_event_update_userpage(event);
+}
+
+static void pdis_start(struct perf_event *event, int flags)
{
struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ bool reload = (flags & PERF_EF_RELOAD) || !pcpu->counter_valid;
- if (pcpu->ready)
+ if (!pcpu->ready || pcpu->event != event ||
+ !(event->hw.state & PERF_HES_STOPPED))
+ return;
+ if (!pdis_program(event, reload))
+ pdis_enable(event);
+}
+
+static void pdis_stop(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (event->hw.state & PERF_HES_STOPPED)
+ return;
+ /* Save progress even on a stop for throttling. */
+ if (pcpu->ready && pcpu->counter_valid)
+ pdis_update_and_quiesce(event);
+ event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+ perf_event_update_userpage(event);
+}
+
+static int pdis_add(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ int ret;
+
+ if (!pcpu->ready)
+ return -ENODEV;
+ if (pcpu->event)
+ return -EAGAIN;
+ ret = pdis_program(event, true);
+ if (ret)
+ return ret;
+ pcpu->event = event;
+ event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+ perf_sched_cb_inc(event->pmu);
+ if (flags & PERF_EF_START) {
+ pdis_enable(event);
+ } else {
pdis_quiesce();
+ perf_event_update_userpage(event);
+ }
+ return 0;
+}
+
+static void pdis_del(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ pdis_stop(event, PERF_EF_UPDATE);
+ pcpu->event = NULL;
+ pcpu->counter_valid = false;
+ perf_sched_cb_dec(event->pmu);
+}
+
+static void pdis_read(struct perf_event *event)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ unsigned long inhibit;
+
+ if (!pcpu->ready || !pcpu->counter_valid || pcpu->event != event ||
+ (event->hw.state & PERF_HES_STOPPED))
+ return;
+ inhibit = csr_read_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pdis_event_update(event);
+ if (!(inhibit & RISCV_PDIS_COUNTER_BIT))
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+}
+
+/* Drop pending selections before changing the task used for sample metadata. */
+static void pdis_sched_task(struct perf_event_pmu_context *ctx,
+ struct task_struct *task, bool sched_in)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+
+ pcpu->switching = !sched_in;
+ if (!pcpu->ready || !event || (event->hw.state & PERF_HES_STOPPED))
+ return;
+ if (!sched_in) {
+ pdis_update_and_quiesce(event);
+ return;
+ }
+
+ /* Restore selection without resetting the counter. */
+ if (pdis_program(event, false))
+ pdis_stop(event, PERF_EF_UPDATE);
+ else
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+}
+
+void riscv_pdis_cpu_dying(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready) {
+ if (pcpu->event)
+ pdis_stop(pcpu->event, PERF_EF_UPDATE);
+ else
+ pdis_quiesce();
+ }
pcpu->ready = false;
cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
}
+
+PMU_FORMAT_ATTR(select, "config:0-2");
+static struct attribute *pdis_format_attrs[] = { &format_attr_select.attr, NULL };
+static const struct attribute_group pdis_format_group = {
+ .name = "format",
+ .attrs = pdis_format_attrs,
+};
+
+static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(&pdis_cpus));
+}
+static DEVICE_ATTR_RO(cpumask);
+static struct attribute *pdis_attrs[] = { &dev_attr_cpumask.attr, NULL };
+static const struct attribute_group pdis_group = { .attrs = pdis_attrs };
+static const struct attribute_group *pdis_groups[] = {
+ &pdis_format_group, &pdis_group, NULL,
+};
+
+static struct pmu pdis_pmu = {
+ .task_ctx_nr = perf_invalid_context,
+ .capabilities = PERF_PMU_CAP_NO_NMI,
+ .attr_groups = pdis_groups,
+ .event_init = pdis_event_init,
+ .add = pdis_add,
+ .del = pdis_del,
+ .start = pdis_start,
+ .stop = pdis_stop,
+ .read = pdis_read,
+ .check_period = pdis_check_period,
+ .sched_task = pdis_sched_task,
+};
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
` (3 preceding siblings ...)
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Use the recorded PC and the single enabled host privilege to construct
perf samples. Only read the record header and PC from the indirect
window. Account for rejected records when applying perf throttling.
Collect PDIS before acknowledging the shared pending interrupt. Restart
ordinary counters even on a PDIS-only interrupt, then re-enable PDIS
if perf has kept the event running.
Register the PMU after probing and installing the interrupt handler.
Save the remaining period on CPU PM entry and restore it on resume.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
drivers/perf/riscv_pdis.c | 112 +++++++++++++++++++++++++++++++++++
drivers/perf/riscv_pdis.h | 10 ++++
drivers/perf/riscv_pmu_sbi.c | 38 ++++++++----
3 files changed, 148 insertions(+), 12 deletions(-)
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
index 9a8fe5e4ab82..60c9ece22dfc 100644
--- a/drivers/perf/riscv_pdis.c
+++ b/drivers/perf/riscv_pdis.c
@@ -3,6 +3,7 @@
#include <linux/bitfield.h>
#include <linux/cpu.h>
+#include <linux/cpu_pm.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/init.h>
@@ -13,10 +14,12 @@
#include <asm/asm-extable.h>
#include <asm/cpufeature.h>
#include <asm/csr.h>
+#include <asm/ptrace.h>
#include "riscv_pdis.h"
/* Draft spdis.adoc at 2de60e18118ce78e2da82ec33296345c94d8560c. */
+#define RISCV_PDIS_ISELECT 0x60
#define RISCV_PDIS_COUNTER_BIT BIT(1)
/* Local experimental encodings, not architectural CSR allocations. */
#define RISCV_PDIS_CSR_CTL 0x5e0
@@ -26,6 +29,7 @@
#define RISCV_PDIS_CSR_FILTER 0x5e4
#define RISCV_PDIS_CSR_VSCTL 0x6d0
#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_OF BIT_ULL(63)
#define RISCV_PDIS_CTL_SEL GENMASK_ULL(2, 0)
#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
#define RISCV_PDIS_CTL_U BIT_ULL(60)
@@ -39,6 +43,9 @@ enum riscv_pdis_select {
RISCV_PDIS_SELECT_CONTROL,
};
+#define RISCV_PDIS_HDR_TYPE GENMASK_ULL(2, 0)
+#define RISCV_PDIS_HDR_FMT GENMASK_ULL(63, 61)
+
#define RISCV_PDIS_SAMPLE_TYPES (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
PERF_SAMPLE_TIME | PERF_SAMPLE_ID | \
PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | \
@@ -54,7 +61,9 @@ struct pdis_cpu {
struct perf_event *event;
bool ready;
bool counter_valid;
+ bool irq_running;
bool switching;
+ bool pm_resume;
};
static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
@@ -399,6 +408,64 @@ static void pdis_sched_task(struct perf_event_pmu_context *ctx,
csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
}
+/* Called with local IRQs disabled and the PDIS counter inhibited. */
+static int pdis_overflow(struct perf_event *event)
+{
+ struct perf_sample_data data;
+ struct pt_regs regs = {};
+ unsigned long selector, header, pc;
+
+ selector = csr_swap(CSR_SISELECT, RISCV_PDIS_ISELECT);
+ header = csr_read(CSR_SIREG);
+ pc = csr_read(CSR_SIREG2);
+ csr_write(CSR_SISELECT, selector);
+
+ if (FIELD_GET(RISCV_PDIS_HDR_FMT, header) ||
+ FIELD_GET(RISCV_PDIS_HDR_TYPE, header) > 4)
+ return perf_event_account_interrupt(event);
+
+ /* The draft record has no privilege field. Only one mode is enabled. */
+ regs.epc = pc;
+ if (event->hw.config & RISCV_PDIS_CTL_S)
+ regs.status = SR_SPP;
+ perf_sample_data_init(&data, 0, event->hw.last_period);
+ data.ip = pc;
+ data.sample_flags |= PERF_SAMPLE_IP;
+ return perf_event_overflow(event, &data, ®s);
+}
+
+bool riscv_pdis_irq_prepare(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+ bool pending;
+
+ if (!pcpu->ready)
+ return false;
+ pcpu->irq_running = event && !(event->hw.state & PERF_HES_STOPPED);
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
+ if (pending && pcpu->irq_running && !pcpu->switching) {
+ pdis_event_update(event);
+ if (pdis_overflow(event))
+ pdis_stop(event, PERF_EF_UPDATE);
+ }
+ csr_clear(RISCV_PDIS_CSR_CTL, RISCV_PDIS_CTL_OF);
+ if (pending)
+ pdis_clear_accounted(event);
+ return pending;
+}
+
+void riscv_pdis_irq_finish(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready && pcpu->irq_running && pcpu->event &&
+ !pcpu->switching && !(pcpu->event->hw.state & PERF_HES_STOPPED))
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pcpu->irq_running = false;
+}
+
void riscv_pdis_cpu_dying(void)
{
struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
@@ -413,6 +480,32 @@ void riscv_pdis_cpu_dying(void)
cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
}
+static int pdis_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd, void *v)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+
+ if (!pcpu->ready)
+ return NOTIFY_DONE;
+ if (cmd == CPU_PM_ENTER) {
+ pcpu->pm_resume = event && !(event->hw.state & PERF_HES_STOPPED);
+ if (event)
+ pdis_stop(event, PERF_EF_UPDATE);
+ else
+ pdis_quiesce();
+ } else if (cmd == CPU_PM_EXIT || cmd == CPU_PM_ENTER_FAILED) {
+ riscv_pdis_cpu_starting();
+ if (pcpu->pm_resume && pcpu->ready && event)
+ pdis_start(event, PERF_EF_RELOAD);
+ pcpu->pm_resume = false;
+ }
+ return NOTIFY_OK;
+}
+
+static struct notifier_block pdis_cpu_pm_nb = {
+ .notifier_call = pdis_cpu_pm_notify,
+};
+
PMU_FORMAT_ATTR(select, "config:0-2");
static struct attribute *pdis_format_attrs[] = { &format_attr_select.attr, NULL };
static const struct attribute_group pdis_format_group = {
@@ -444,3 +537,22 @@ static struct pmu pdis_pmu = {
.check_period = pdis_check_period,
.sched_task = pdis_sched_task,
};
+
+int riscv_pdis_register(struct device *parent)
+{
+ int ret;
+
+ if (cpumask_empty(&pdis_cpus)) {
+ if (pdis_test)
+ dev_info(parent, "PDIS test PMU unavailable on all online CPUs\n");
+ return -ENODEV;
+ }
+ ret = cpu_pm_register_notifier(&pdis_cpu_pm_nb);
+ if (ret)
+ return ret;
+ pdis_pmu.parent = parent;
+ ret = perf_pmu_register(&pdis_pmu, "riscv_pdis_test", -1);
+ if (ret)
+ cpu_pm_unregister_notifier(&pdis_cpu_pm_nb);
+ return ret;
+}
diff --git a/drivers/perf/riscv_pdis.h b/drivers/perf/riscv_pdis.h
index b1ffe7d16ce0..8a9cf7afe51e 100644
--- a/drivers/perf/riscv_pdis.h
+++ b/drivers/perf/riscv_pdis.h
@@ -2,12 +2,22 @@
#ifndef __RISCV_PDIS_H
#define __RISCV_PDIS_H
+#include <linux/types.h>
+
+struct device;
+
#ifdef CONFIG_RISCV_PDIS_CSR
void riscv_pdis_cpu_starting(void);
void riscv_pdis_cpu_dying(void);
+bool riscv_pdis_irq_prepare(void);
+void riscv_pdis_irq_finish(void);
+int riscv_pdis_register(struct device *parent);
#else
static inline void riscv_pdis_cpu_starting(void) { }
static inline void riscv_pdis_cpu_dying(void) { }
+static inline bool riscv_pdis_irq_prepare(void) { return false; }
+static inline void riscv_pdis_irq_finish(void) { }
+static inline int riscv_pdis_register(struct device *parent) { return 0; }
#endif
#endif /* __RISCV_PDIS_H */
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 545b8becf90a..af86a30aae83 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -1092,24 +1092,23 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
struct cpu_hw_events *cpu_hw_evt = dev;
u64 start_clock = sched_clock();
struct riscv_pmu_snapshot_data *sdata;
+ bool handled;
if (WARN_ON_ONCE(!cpu_hw_evt))
return IRQ_NONE;
sdata = cpu_hw_evt->snapshot_addr;
+ /* Inhibit and collect PDIS before any shared pending bit is cleared. */
+ handled = riscv_pdis_irq_prepare();
/* Firmware counter don't support overflow yet */
fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
- if (fidx == RISCV_MAX_COUNTERS) {
- csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
- return IRQ_NONE;
- }
+ if (fidx == RISCV_MAX_COUNTERS)
+ goto out_clear;
event = cpu_hw_evt->events[fidx];
- if (!event) {
- ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
- return IRQ_NONE;
- }
+ if (!event)
+ goto out_clear;
pmu = to_riscv_pmu(event->pmu);
pmu_sbi_stop_hw_ctrs(pmu);
@@ -1128,7 +1127,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
/* No overflow bit is set */
if (!overflow)
- return IRQ_NONE;
+ goto restart;
regs = get_irq_regs();
@@ -1181,10 +1180,20 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
hw_evt->state = 0;
}
- pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
- perf_sample_event_took(sched_clock() - start_clock);
+ handled = true;
- return IRQ_HANDLED;
+restart:
+ /* A PDIS-only interrupt must also resume the ordinary HPM counters. */
+ pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
+out:
+ /* PDIS can restart only after all sources and shared pending are serviced. */
+ riscv_pdis_irq_finish();
+ if (handled)
+ perf_sample_event_took(sched_clock() - start_clock);
+ return handled ? IRQ_HANDLED : IRQ_NONE;
+out_clear:
+ ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+ goto out;
}
static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
@@ -1563,6 +1572,11 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
if (ret)
goto out_unregister;
+ if (irq_requested && riscv_pmu_irq_num == RV_IRQ_PMU) {
+ ret = riscv_pdis_register(&pdev->dev);
+ if (ret && ret != -ENODEV)
+ pr_warn("PDIS test PMU registration failed: %d\n", ret);
+ }
/* Asynchronously check which standard events are available */
schedule_work(&check_std_events_work);
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
@ 2026-11-18 8:23 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-18 8:23 UTC (permalink / raw)
To: Zhanpeng Zhang, linux-perf-users; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dt-bindings: riscv: unratified draft smpdis and sspdis
extensions added
--
commit 6221314245742eb097a15011dd35eda824258728
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
This commit adds devicetree binding descriptions for the draft Smpdis and
Sspdis RISC-V Precise Decoded Instruction Sampling extensions.
> diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml
> index 79491ef8f86a8..afda76b9c7c47 100644
> --- a/Documentation/devicetree/bindings/riscv/extensions.yaml
> +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml
> @@ -214,6 +214,15 @@ properties:
> ratified at commit d70011dde6c2 ("Update to ratified state")
> of riscv-j-extension.
>
> + - const: smpdis
> + description: |
> + The draft Smpdis machine-level extension for Precise Decoded
> + Instruction Sampling, as described at commit
> + 2de60e18118ce78e2da82ec33296345c94d8560c of the
> + riscv-performance-event-sampling specification. It defines
> + instruction selection, filtering and sample collection across
> + privilege levels. This does not imply supervisor access to PDIS.
> +
> - const: smstateen
> description: |
> The standard Smstateen extension for controlling access to CSRs
> @@ -269,6 +278,16 @@ properties:
> ratified at commit d70011dde6c2 ("Update to ratified state")
> of riscv-j-extension.
>
> + - const: sspdis
> + description: |
> + The draft Sspdis supervisor-level extension for Precise Decoded
> + Instruction Sampling, as described at commit
> + 2de60e18118ce78e2da82ec33296345c94d8560c of the
> + riscv-performance-event-sampling specification. It provides the
> + supervisor-visible PDIS registers and behavior, excluding the
> + machine-only state. Firmware must make this state accessible to
> + supervisor software before reporting this extension.
> +
[Severity: Medium]
Should unratified, unfrozen draft specifications claim the canonical
smpdis and sspdis strings in riscv,isa-extensions?
Documentation/devicetree/bindings/riscv/extensions.yaml states that
standard extension properties map to their originally ratified states, and
Documentation/arch/riscv/patch-acceptance.rst requires RISC-V
specifications to be Frozen or Ratified before acceptance. Defining
unversioned standard extension names for a draft without final CSR
allocations breaks devicetree ABI stability if the specification or CSRs
change before ratification.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-18 8:23 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
2026-11-18 8:23 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox