Skip to content

Commit c360cbe

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: introduce unified static key mechanism for ISA extensions
Currently, riscv has several extensions which may not be supported on all riscv platforms, for example, FPU and so on. To support unified kernel Image style, we need to check whether the feature is supported or not. If the check sits at hot code path, then performance will be impacted a lot. static key can be used to solve the issue. In the past, FPU support has been converted to use static key mechanism. I believe we will have similar cases in the future. This patch tries to add an unified mechanism to use static keys for some ISA extensions by implementing an array of default-false static keys and enabling them when detected. Signed-off-by: Jisheng Zhang <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent f2906aa commit c360cbe

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

arch/riscv/include/asm/hwcap.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <uapi/asm/hwcap.h>
1313

1414
#ifndef __ASSEMBLY__
15+
#include <linux/jump_label.h>
1516
/*
1617
* This yields a mask that user programs can use to figure out what
1718
* instruction set this cpu supports.
@@ -56,13 +57,37 @@ enum riscv_isa_ext_id {
5657
RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
5758
};
5859

60+
/*
61+
* This enum represents the logical ID for each RISC-V ISA extension static
62+
* keys. We can use static key to optimize code path if some ISA extensions
63+
* are available.
64+
*/
65+
enum riscv_isa_ext_key {
66+
RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
67+
RISCV_ISA_EXT_KEY_MAX,
68+
};
69+
5970
struct riscv_isa_ext_data {
6071
/* Name of the extension displayed to userspace via /proc/cpuinfo */
6172
char uprop[RISCV_ISA_EXT_NAME_LEN_MAX];
6273
/* The logical ISA extension ID */
6374
unsigned int isa_ext_id;
6475
};
6576

77+
extern struct static_key_false riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_MAX];
78+
79+
static __always_inline int riscv_isa_ext2key(int num)
80+
{
81+
switch (num) {
82+
case RISCV_ISA_EXT_f:
83+
return RISCV_ISA_EXT_KEY_FPU;
84+
case RISCV_ISA_EXT_d:
85+
return RISCV_ISA_EXT_KEY_FPU;
86+
default:
87+
return -EINVAL;
88+
}
89+
}
90+
6691
unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap);
6792

6893
#define riscv_isa_extension_mask(ext) BIT_MASK(RISCV_ISA_EXT_##ext)

arch/riscv/kernel/cpufeature.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
3030
#ifdef CONFIG_FPU
3131
__ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu);
3232
#endif
33+
__ro_after_init DEFINE_STATIC_KEY_ARRAY_FALSE(riscv_isa_ext_keys, RISCV_ISA_EXT_KEY_MAX);
34+
EXPORT_SYMBOL(riscv_isa_ext_keys);
3335

3436
/**
3537
* riscv_isa_extension_base() - Get base extension word
@@ -238,6 +240,11 @@ void __init riscv_fill_hwcap(void)
238240
print_str[j++] = (char)('a' + i);
239241
pr_info("riscv: ELF capabilities %s\n", print_str);
240242

243+
for_each_set_bit(i, riscv_isa, RISCV_ISA_EXT_MAX) {
244+
j = riscv_isa_ext2key(i);
245+
if (j >= 0)
246+
static_branch_enable(&riscv_isa_ext_keys[j]);
247+
}
241248
#ifdef CONFIG_FPU
242249
if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
243250
static_branch_enable(&cpu_hwcap_fpu);

0 commit comments

Comments
 (0)