1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copied from arch/arm64/kernel/cpufeature.c
5 * Copyright (C) 2015 ARM Ltd.
6 * Copyright (C) 2017 SiFive
9 #include <linux/bitmap.h>
10 #include <linux/ctype.h>
11 #include <linux/libfdt.h>
12 #include <linux/module.h>
14 #include <asm/alternative.h>
15 #include <asm/errata_list.h>
16 #include <asm/hwcap.h>
17 #include <asm/patch.h>
18 #include <asm/pgtable.h>
19 #include <asm/processor.h>
21 #include <asm/switch_to.h>
23 #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
25 unsigned long elf_hwcap __read_mostly;
28 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
31 __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu);
35 * riscv_isa_extension_base() - Get base extension word
37 * @isa_bitmap: ISA bitmap to use
38 * Return: base extension word as unsigned long value
40 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
42 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
48 EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
51 * __riscv_isa_extension_available() - Check whether given extension
54 * @isa_bitmap: ISA bitmap to use
55 * @bit: bit position of the desired extension
56 * Return: true or false
58 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
60 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit)
62 const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
64 if (bit >= RISCV_ISA_EXT_MAX)
67 return test_bit(bit, bmap) ? true : false;
69 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
71 void __init riscv_fill_hwcap(void)
73 struct device_node *node;
75 char print_str[NUM_ALPHA_EXTS + 1];
77 static unsigned long isa2hwcap[256] = {0};
79 isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
80 isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
81 isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
82 isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
83 isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
84 isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
88 bitmap_zero(riscv_isa, RISCV_ISA_EXT_MAX);
90 for_each_of_cpu_node(node) {
91 unsigned long this_hwcap = 0;
92 DECLARE_BITMAP(this_isa, RISCV_ISA_EXT_MAX);
95 if (riscv_of_processor_hartid(node) < 0)
98 if (of_property_read_string(node, "riscv,isa", &isa)) {
99 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
104 #if IS_ENABLED(CONFIG_32BIT)
105 if (!strncmp(isa, "rv32", 4))
107 #elif IS_ENABLED(CONFIG_64BIT)
108 if (!strncmp(isa, "rv64", 4))
111 /* The riscv,isa DT property must start with rv64 or rv32 */
114 bitmap_zero(this_isa, RISCV_ISA_EXT_MAX);
115 for (; *isa; ++isa) {
116 const char *ext = isa++;
117 const char *ext_end = isa;
118 bool ext_long = false, ext_err = false;
123 * Workaround for invalid single-letter 's' & 'u'(QEMU).
124 * No need to set the bit in riscv_isa as 's' & 'u' are
125 * not valid ISA extensions. It works until multi-letter
126 * extension starting with "Su" appears.
128 if (ext[-1] != '_' && ext[1] == 'u') {
137 /* Multi-letter extension must be delimited */
138 for (; *isa && *isa != '_'; ++isa)
139 if (unlikely(!islower(*isa)
142 /* Parse backwards */
144 if (unlikely(ext_err))
146 if (!isdigit(ext_end[-1]))
148 /* Skip the minor version */
149 while (isdigit(*--ext_end))
151 if (ext_end[0] != 'p'
152 || !isdigit(ext_end[-1])) {
153 /* Advance it to offset the pre-decrement */
157 /* Skip the major version */
158 while (isdigit(*--ext_end))
163 if (unlikely(!islower(*ext))) {
167 /* Find next extension */
170 /* Skip the minor version */
171 while (isdigit(*++isa))
175 if (!isdigit(*++isa)) {
179 /* Skip the major version */
180 while (isdigit(*++isa))
187 #define SET_ISA_EXT_MAP(name, bit) \
189 if ((ext_end - ext == sizeof(name) - 1) && \
190 !memcmp(ext, name, sizeof(name) - 1)) \
191 set_bit(bit, this_isa); \
194 if (unlikely(ext_err))
197 this_hwcap |= isa2hwcap[(unsigned char)(*ext)];
198 set_bit(*ext - 'a', this_isa);
200 SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
201 SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
203 #undef SET_ISA_EXT_MAP
207 * All "okay" hart should have same isa. Set HWCAP based on
208 * common capabilities of every "okay" hart, in case they don't
212 elf_hwcap &= this_hwcap;
214 elf_hwcap = this_hwcap;
216 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
217 bitmap_copy(riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
219 bitmap_and(riscv_isa, riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
222 /* We don't support systems with F but without D, so mask those out
224 if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
225 pr_info("This kernel does not support systems with F but not D\n");
226 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
229 memset(print_str, 0, sizeof(print_str));
230 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
231 if (riscv_isa[0] & BIT_MASK(i))
232 print_str[j++] = (char)('a' + i);
233 pr_info("riscv: base ISA extensions %s\n", print_str);
235 memset(print_str, 0, sizeof(print_str));
236 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
237 if (elf_hwcap & BIT_MASK(i))
238 print_str[j++] = (char)('a' + i);
239 pr_info("riscv: ELF capabilities %s\n", print_str);
242 if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
243 static_branch_enable(&cpu_hwcap_fpu);
247 #ifdef CONFIG_RISCV_ALTERNATIVE
248 struct cpufeature_info {
249 char name[ERRATA_STRING_LENGTH_MAX];
250 bool (*check_func)(unsigned int stage);
253 static bool __init_or_module cpufeature_svpbmt_check_func(unsigned int stage)
255 #ifdef CONFIG_RISCV_ISA_SVPBMT
257 case RISCV_ALTERNATIVES_EARLY_BOOT:
260 return riscv_isa_extension_available(NULL, SVPBMT);
267 static const struct cpufeature_info __initdata_or_module
268 cpufeature_list[CPUFEATURE_NUMBER] = {
271 .check_func = cpufeature_svpbmt_check_func
275 static u32 __init_or_module cpufeature_probe(unsigned int stage)
277 const struct cpufeature_info *info;
278 u32 cpu_req_feature = 0;
281 for (idx = 0; idx < CPUFEATURE_NUMBER; idx++) {
282 info = &cpufeature_list[idx];
284 if (info->check_func(stage))
285 cpu_req_feature |= (1U << idx);
288 return cpu_req_feature;
291 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
292 struct alt_entry *end,
295 u32 cpu_req_feature = cpufeature_probe(stage);
296 struct alt_entry *alt;
299 for (alt = begin; alt < end; alt++) {
300 if (alt->vendor_id != 0)
302 if (alt->errata_id >= CPUFEATURE_NUMBER) {
303 WARN(1, "This feature id:%d is not in kernel cpufeature list",
308 tmp = (1U << alt->errata_id);
309 if (cpu_req_feature & tmp)
310 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);