Skip to contentSkip to conversation

Common csr_read_num() and csr_write_num() for RISC-V

PATCH New Anup Patel 1 participant 2 messages Nov 15

Subsystems (5)
lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v8 cover Common csr_read_num() and csr_write_num() for RISC-V

Some of the RISC-V drivers (such as RISC-V PMU and ACPI CPPC) need to access CSR based on CSR number discovered from somewhere. Add common RISC-V csr_read_num() and csr_write_num() functions under arch/riscv for such drivers.

These patches can be found in the riscv_csr_read_num_v8 branch at:
https://github.com/avpatel/linux.git

Changes since v7:
- Rebased on Linux-7.4-rc2
- Preserve the CSR range check in riscv_pmu_ctr_read_csr().
- Added #if CONFIG_32BIT for cycleH CSR in csr_read_num().

Changes since v6:
- Rebased on Linux-7.4-rc1

Changes since v5:
- Rebased on Linux-7.2-rc5

Changes since v4:
- Rebased on Linux-6.19-rc6

Changes since v3:
- Rebased on Linux-6.18-rc7
- Updated commit decription of PATCH1 to reflect the fact that we are removing sanity checks on CSR number which are already taken care by csr_read_num() and csr_write_num().

Changes since v2:
- Rebased on Linux-6.18-rc1
- Added reviewed-by tags

Changes since v1:
- Make "out_err" mandatory for csr_read_num() and csr_write_num() in PATCH2 as suggested by Sunil and Drew. This also helps further simplify csr_read_num() and csr_write_num().

Anup Patel (1):
RISC-V: Add common csr_read_num() and csr_write_num() functions

 arch/riscv/include/asm/csr.h |   3 +
 arch/riscv/kernel/Makefile   |   1 +
 arch/riscv/kernel/csr.c      | 167 +++++++++++++++++++++++++++++++++++
 drivers/acpi/riscv/cppc.c    |  17 ++--
 drivers/perf/riscv_pmu.c     |  52 +++--------
 5 files changed, 188 insertions(+), 52 deletions(-)
 create mode 100644 arch/riscv/kernel/csr.c
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v8 1/1 RISC-V: Add common csr_read_num() and csr_write_num() functions

From: Anup Patel <[email protected]>

In RISC-V, there is no CSR read/write instruction which takes CSR number via register so add common csr_read_num() and csr_write_num() functions which allow accessing certain CSRs by passing CSR number as parameter. These common functions will be first used by the ACPI CPPC driver and RISC-V PMU driver.

Also, the RISC-V ACPI FFH specification allows arbitrary CSR number as CPPC register and the RISC-V SBI specification allows arbitrary CSR number as PMU hardware counter. This means ACPI CPPC driver and RISC-V PMU driver no longer need to do sanity checks on CSR number which are now done by the common csr_read_num() and csr_write_num() functions.

---
 arch/riscv/include/asm/csr.h |   3 +
 arch/riscv/kernel/Makefile   |   1 +
 arch/riscv/kernel/csr.c      | 167 +++++++++++++++++++++++++++++++++++
 drivers/acpi/riscv/cppc.c    |  17 ++--
 drivers/perf/riscv_pmu.c     |  52 +++--------
 5 files changed, 188 insertions(+), 52 deletions(-)
 create mode 100644 arch/riscv/kernel/csr.c
arch/riscv/include/asm/csr.h +3 -0
index 621f84d1898f..ebd82043430b 100644
@@ -672,6 +672,9 @@
 			      : "memory");			\
 })
 
+unsigned long csr_read_num(unsigned long csr_num, int *out_err);
+void csr_write_num(unsigned long csr_num, unsigned long val, int *out_err);
+
 #endif /* __ASSEMBLER__ */
 
 #endif /* _ASM_RISCV_CSR_H */
arch/riscv/kernel/Makefile +1 -0
index f54c336bf547..7f52ae724225 100644
@@ -51,6 +51,7 @@ obj-y	+= soc.o
 obj-y	+= alternative.o
 obj-y	+= cpu.o
 obj-y	+= cpufeature.o
+obj-y	+= csr.o
 obj-y	+= entry.o
 obj-y	+= irq.o
 obj-y	+= process.o
arch/riscv/kernel/csr.c new file +167 -0

167 lines not on this page · raw message

drivers/acpi/riscv/cppc.c +6 -11
index 74a5e484f809..da1a849ddc29 100644
@@ -65,24 +65,19 @@ static void sbi_cppc_write(void *write_data)
 static void cppc_ffh_csr_read(void *read_data)
 {
 	struct sbi_cppc_data *data = (struct sbi_cppc_data *)read_data;
+	int err;
 
-	switch (data->reg) {
-	/* Support only TIME CSR for now */
-	case CSR_TIME:
-		data->ret.value = csr_read(CSR_TIME);
-		data->ret.error = 0;
-		break;
-	default:
-		data->ret.error = -EINVAL;
-		break;
-	}
+	data->ret.value = csr_read_num(data->reg, &err);
+	data->ret.error = err;
 }
 
 static void cppc_ffh_csr_write(void *write_data)
 {
 	struct sbi_cppc_data *data = (struct sbi_cppc_data *)write_data;
+	int err;
 
-	data->ret.error = -EINVAL;
+	csr_write_num(data->reg, data->val, &err);
+	data->ret.error = err;
 }
 
 /*
drivers/perf/riscv_pmu.c +11 -41

75 lines not on this page · raw message

Signature · 1 line
2.43.0