Skip to content

Commit e2b6bc2

Browse files
Merge patch series "riscv: cbo.zero fixes"
Samuel Holland <samuel.holland@sifive.com> says: This series fixes a couple of issues related to using the cbo.zero instruction in userspace. The first patch fixes a bug where the wrong enable bit gets set if the kernel is running in M-mode. The remaining patches fix a bug where the enable bit gets reset to its default value after a nonretentive idle state. I have hardware which reproduces this: Before this series: $ tools/testing/selftests/riscv/hwprobe/cbo TAP version 13 1..3 ok 1 Zicboz block size # Zicboz block size: 64 Illegal instruction After applying this series: $ tools/testing/selftests/riscv/hwprobe/cbo TAP version 13 1..3 ok 1 Zicboz block size # Zicboz block size: 64 ok 2 cbo.zero ok 3 cbo.zero check # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 * b4-shazam-merge: riscv: Save/restore envcfg CSR during CPU suspend riscv: Add a custom ISA extension for the [ms]envcfg CSR riscv: Fix enabling cbo.zero when running in M-mode Link: https://lore.kernel.org/r/20240228065559.3434837-1-samuel.holland@sifive.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2 parents 34b5678 + 05ab803 commit e2b6bc2

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

arch/riscv/include/asm/csr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@
424424
# define CSR_STATUS CSR_MSTATUS
425425
# define CSR_IE CSR_MIE
426426
# define CSR_TVEC CSR_MTVEC
427+
# define CSR_ENVCFG CSR_MENVCFG
427428
# define CSR_SCRATCH CSR_MSCRATCH
428429
# define CSR_EPC CSR_MEPC
429430
# define CSR_CAUSE CSR_MCAUSE
@@ -448,6 +449,7 @@
448449
# define CSR_STATUS CSR_SSTATUS
449450
# define CSR_IE CSR_SIE
450451
# define CSR_TVEC CSR_STVEC
452+
# define CSR_ENVCFG CSR_SENVCFG
451453
# define CSR_SCRATCH CSR_SSCRATCH
452454
# define CSR_EPC CSR_SEPC
453455
# define CSR_CAUSE CSR_SCAUSE

arch/riscv/include/asm/hwcap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
#define RISCV_ISA_EXT_ZTSO 72
8282
#define RISCV_ISA_EXT_ZACAS 73
8383

84+
#define RISCV_ISA_EXT_XLINUXENVCFG 127
85+
8486
#define RISCV_ISA_EXT_MAX 128
8587
#define RISCV_ISA_EXT_INVALID U32_MAX
8688

arch/riscv/include/asm/suspend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct suspend_context {
1414
struct pt_regs regs;
1515
/* Saved and restored by high-level functions */
1616
unsigned long scratch;
17+
unsigned long envcfg;
1718
unsigned long tvec;
1819
unsigned long ie;
1920
#ifdef CONFIG_MMU

arch/riscv/kernel/cpufeature.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ static const unsigned int riscv_zvbb_exts[] = {
202202
RISCV_ISA_EXT_ZVKB
203203
};
204204

205+
/*
206+
* While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V
207+
* privileged ISA, the existence of the CSRs is implied by any extension which
208+
* specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the
209+
* existence of the CSR, and treat it as a subset of those other extensions.
210+
*/
211+
static const unsigned int riscv_xlinuxenvcfg_exts[] = {
212+
RISCV_ISA_EXT_XLINUXENVCFG
213+
};
214+
205215
/*
206216
* The canonical order of ISA extension names in the ISA string is defined in
207217
* chapter 27 of the unprivileged specification.
@@ -251,8 +261,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
251261
__RISCV_ISA_EXT_DATA(c, RISCV_ISA_EXT_c),
252262
__RISCV_ISA_EXT_DATA(v, RISCV_ISA_EXT_v),
253263
__RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
254-
__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
255-
__RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
264+
__RISCV_ISA_EXT_SUPERSET(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts),
265+
__RISCV_ISA_EXT_SUPERSET(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts),
256266
__RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
257267
__RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
258268
__RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
@@ -965,7 +975,7 @@ arch_initcall(check_unaligned_access_all_cpus);
965975
void riscv_user_isa_enable(void)
966976
{
967977
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
968-
csr_set(CSR_SENVCFG, ENVCFG_CBZE);
978+
csr_set(CSR_ENVCFG, ENVCFG_CBZE);
969979
}
970980

971981
#ifdef CONFIG_RISCV_ALTERNATIVE

arch/riscv/kernel/suspend.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
void suspend_save_csrs(struct suspend_context *context)
1616
{
1717
context->scratch = csr_read(CSR_SCRATCH);
18+
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
19+
context->envcfg = csr_read(CSR_ENVCFG);
1820
context->tvec = csr_read(CSR_TVEC);
1921
context->ie = csr_read(CSR_IE);
2022

@@ -36,6 +38,8 @@ void suspend_save_csrs(struct suspend_context *context)
3638
void suspend_restore_csrs(struct suspend_context *context)
3739
{
3840
csr_write(CSR_SCRATCH, context->scratch);
41+
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
42+
csr_write(CSR_ENVCFG, context->envcfg);
3943
csr_write(CSR_TVEC, context->tvec);
4044
csr_write(CSR_IE, context->ie);
4145

0 commit comments

Comments
 (0)