Skip to content

Commit 8eb060e

Browse files
Dao Lupalmer-dabbelt
authored andcommitted
arch/riscv: add Zihintpause support
Implement support for the ZiHintPause extension. The PAUSE instruction is a HINT that indicates the current hart’s rate of instruction retirement should be temporarily reduced or paused. Reviewed-by: Heiko Stuebner <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Signed-off-by: Dao Lu <[email protected]> [Palmer: Some minor merge conflicts.] Link: https://lore.kernel.org/all/[email protected]/ Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 3aefb2e commit 8eb060e

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

arch/riscv/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ riscv-march-$(toolchain-need-zicsr-zifencei) := $(riscv-march-y)_zicsr_zifencei
6060
toolchain-supports-zicbom := $(call cc-option-yn, -march=$(riscv-march-y)_zicbom)
6161
riscv-march-$(toolchain-supports-zicbom) := $(riscv-march-y)_zicbom
6262

63+
# Check if the toolchain supports Zihintpause extension
64+
toolchain-supports-zihintpause := $(call cc-option-yn, -march=$(riscv-march-y)_zihintpause)
65+
riscv-march-$(toolchain-supports-zihintpause) := $(riscv-march-y)_zihintpause
66+
6367
KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y))
6468
KBUILD_AFLAGS += -march=$(riscv-march-y)
6569

arch/riscv/include/asm/hwcap.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef _ASM_RISCV_HWCAP_H
99
#define _ASM_RISCV_HWCAP_H
1010

11+
#include <asm/errno.h>
1112
#include <linux/bits.h>
1213
#include <uapi/asm/hwcap.h>
1314

@@ -55,6 +56,7 @@ enum riscv_isa_ext_id {
5556
RISCV_ISA_EXT_SSCOFPMF = RISCV_ISA_EXT_BASE,
5657
RISCV_ISA_EXT_SVPBMT,
5758
RISCV_ISA_EXT_ZICBOM,
59+
RISCV_ISA_EXT_ZIHINTPAUSE,
5860
RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
5961
};
6062

@@ -65,6 +67,7 @@ enum riscv_isa_ext_id {
6567
*/
6668
enum riscv_isa_ext_key {
6769
RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
70+
RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
6871
RISCV_ISA_EXT_KEY_MAX,
6972
};
7073

@@ -84,6 +87,8 @@ static __always_inline int riscv_isa_ext2key(int num)
8487
return RISCV_ISA_EXT_KEY_FPU;
8588
case RISCV_ISA_EXT_d:
8689
return RISCV_ISA_EXT_KEY_FPU;
90+
case RISCV_ISA_EXT_ZIHINTPAUSE:
91+
return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
8792
default:
8893
return -EINVAL;
8994
}

arch/riscv/include/asm/vdso/processor.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44

55
#ifndef __ASSEMBLY__
66

7+
#include <linux/jump_label.h>
78
#include <asm/barrier.h>
9+
#include <asm/hwcap.h>
810

911
static inline void cpu_relax(void)
1012
{
13+
if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
1114
#ifdef __riscv_muldiv
12-
int dummy;
13-
/* In lieu of a halt instruction, induce a long-latency stall. */
14-
__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
15+
int dummy;
16+
/* In lieu of a halt instruction, induce a long-latency stall. */
17+
__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
1518
#endif
19+
} else {
20+
/*
21+
* Reduce instruction retirement.
22+
* This assumes the PC changes.
23+
*/
24+
#ifdef __riscv_zihintpause
25+
__asm__ __volatile__ ("pause");
26+
#else
27+
/* Encoding of the pause instruction */
28+
__asm__ __volatile__ (".4byte 0x100000F");
29+
#endif
30+
}
1631
barrier();
1732
}
1833

arch/riscv/kernel/cpu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static struct riscv_isa_ext_data isa_ext_arr[] = {
9494
__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
9595
__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
9696
__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
97+
__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
9798
__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
9899
};
99100

arch/riscv/kernel/cpufeature.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void __init riscv_fill_hwcap(void)
202202
SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
203203
SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
204204
SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
205+
SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
205206
}
206207
#undef SET_ISA_EXT_MAP
207208
}

0 commit comments

Comments
 (0)