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/acpi.h>
10 #include <linux/bitmap.h>
11 #include <linux/ctype.h>
12 #include <linux/log2.h>
13 #include <linux/memory.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
18 #include <asm/alternative.h>
19 #include <asm/cacheflush.h>
20 #include <asm/cpufeature.h>
21 #include <asm/hwcap.h>
22 #include <asm/patch.h>
23 #include <asm/processor.h>
24 #include <asm/vector.h>
26 #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
28 unsigned long elf_hwcap __read_mostly;
31 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
33 /* Per-cpu ISA extensions. */
34 struct riscv_isainfo hart_isa[NR_CPUS];
36 /* Performance information */
37 DEFINE_PER_CPU(long, misaligned_access_speed);
40 * riscv_isa_extension_base() - Get base extension word
42 * @isa_bitmap: ISA bitmap to use
43 * Return: base extension word as unsigned long value
45 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
47 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
53 EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
56 * __riscv_isa_extension_available() - Check whether given extension
59 * @isa_bitmap: ISA bitmap to use
60 * @bit: bit position of the desired extension
61 * Return: true or false
63 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
65 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit)
67 const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
69 if (bit >= RISCV_ISA_EXT_MAX)
72 return test_bit(bit, bmap) ? true : false;
74 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
76 static bool riscv_isa_extension_check(int id)
79 case RISCV_ISA_EXT_ZICBOM:
80 if (!riscv_cbom_block_size) {
81 pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
83 } else if (!is_power_of_2(riscv_cbom_block_size)) {
84 pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
88 case RISCV_ISA_EXT_ZICBOZ:
89 if (!riscv_cboz_block_size) {
90 pr_err("Zicboz detected in ISA string, but no cboz-block-size found\n");
92 } else if (!is_power_of_2(riscv_cboz_block_size)) {
93 pr_err("cboz-block-size present, but is not a power-of-2\n");
102 void __init riscv_fill_hwcap(void)
104 struct device_node *node;
106 char print_str[NUM_ALPHA_EXTS + 1];
108 unsigned long isa2hwcap[26] = {0};
109 struct acpi_table_header *rhct;
113 isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
114 isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
115 isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
116 isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
117 isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
118 isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
119 isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
123 bitmap_zero(riscv_isa, RISCV_ISA_EXT_MAX);
125 if (!acpi_disabled) {
126 status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
127 if (ACPI_FAILURE(status))
131 for_each_possible_cpu(cpu) {
132 struct riscv_isainfo *isainfo = &hart_isa[cpu];
133 unsigned long this_hwcap = 0;
137 node = of_cpu_device_node_get(cpu);
139 pr_warn("Unable to find cpu node\n");
143 rc = of_property_read_string(node, "riscv,isa", &isa);
146 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
150 rc = acpi_get_riscv_isa(rhct, cpu, &isa);
152 pr_warn("Unable to get ISA for the hart - %d\n", cpu);
158 if (IS_ENABLED(CONFIG_32BIT) && !strncasecmp(isa, "rv32", 4))
160 else if (IS_ENABLED(CONFIG_64BIT) && !strncasecmp(isa, "rv64", 4))
162 /* The riscv,isa DT property must start with rv64 or rv32 */
165 for (; *isa; ++isa) {
166 const char *ext = isa++;
167 const char *ext_end = isa;
168 bool ext_long = false, ext_err = false;
173 * Workaround for invalid single-letter 's' & 'u'(QEMU).
174 * No need to set the bit in riscv_isa as 's' & 'u' are
175 * not valid ISA extensions. It works until multi-letter
176 * extension starting with "Su" appears.
178 if (ext[-1] != '_' && ext[1] == 'u') {
190 /* Multi-letter extension must be delimited */
191 for (; *isa && *isa != '_'; ++isa)
192 if (unlikely(!isalnum(*isa)))
194 /* Parse backwards */
196 if (unlikely(ext_err))
198 if (!isdigit(ext_end[-1]))
200 /* Skip the minor version */
201 while (isdigit(*--ext_end))
203 if (tolower(ext_end[0]) != 'p'
204 || !isdigit(ext_end[-1])) {
205 /* Advance it to offset the pre-decrement */
209 /* Skip the major version */
210 while (isdigit(*--ext_end))
215 if (unlikely(!isalpha(*ext))) {
219 /* Find next extension */
222 /* Skip the minor version */
223 while (isdigit(*++isa))
225 if (tolower(*isa) != 'p')
227 if (!isdigit(*++isa)) {
231 /* Skip the major version */
232 while (isdigit(*++isa))
239 #define SET_ISA_EXT_MAP(name, bit) \
241 if ((ext_end - ext == sizeof(name) - 1) && \
242 !strncasecmp(ext, name, sizeof(name) - 1) && \
243 riscv_isa_extension_check(bit)) \
244 set_bit(bit, isainfo->isa); \
247 if (unlikely(ext_err))
250 int nr = tolower(*ext) - 'a';
252 if (riscv_isa_extension_check(nr)) {
253 this_hwcap |= isa2hwcap[nr];
254 set_bit(nr, isainfo->isa);
257 /* sorted alphabetically */
258 SET_ISA_EXT_MAP("smaia", RISCV_ISA_EXT_SMAIA);
259 SET_ISA_EXT_MAP("ssaia", RISCV_ISA_EXT_SSAIA);
260 SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
261 SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);
262 SET_ISA_EXT_MAP("svinval", RISCV_ISA_EXT_SVINVAL);
263 SET_ISA_EXT_MAP("svnapot", RISCV_ISA_EXT_SVNAPOT);
264 SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
265 SET_ISA_EXT_MAP("zba", RISCV_ISA_EXT_ZBA);
266 SET_ISA_EXT_MAP("zbb", RISCV_ISA_EXT_ZBB);
267 SET_ISA_EXT_MAP("zbs", RISCV_ISA_EXT_ZBS);
268 SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
269 SET_ISA_EXT_MAP("zicboz", RISCV_ISA_EXT_ZICBOZ);
270 SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
272 #undef SET_ISA_EXT_MAP
276 * All "okay" hart should have same isa. Set HWCAP based on
277 * common capabilities of every "okay" hart, in case they don't
281 elf_hwcap &= this_hwcap;
283 elf_hwcap = this_hwcap;
285 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
286 bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
288 bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
291 if (!acpi_disabled && rhct)
292 acpi_put_table((struct acpi_table_header *)rhct);
294 /* We don't support systems with F but without D, so mask those out
296 if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
297 pr_info("This kernel does not support systems with F but not D\n");
298 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
301 if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
302 riscv_v_setup_vsize();
304 * ISA string in device tree might have 'v' flag, but
305 * CONFIG_RISCV_ISA_V is disabled in kernel.
306 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
308 if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
309 elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
312 memset(print_str, 0, sizeof(print_str));
313 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
314 if (riscv_isa[0] & BIT_MASK(i))
315 print_str[j++] = (char)('a' + i);
316 pr_info("riscv: base ISA extensions %s\n", print_str);
318 memset(print_str, 0, sizeof(print_str));
319 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
320 if (elf_hwcap & BIT_MASK(i))
321 print_str[j++] = (char)('a' + i);
322 pr_info("riscv: ELF capabilities %s\n", print_str);
325 unsigned long riscv_get_elf_hwcap(void)
329 hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
331 if (!riscv_v_vstate_ctrl_user_allowed())
332 hwcap &= ~COMPAT_HWCAP_ISA_V;
337 #ifdef CONFIG_RISCV_ALTERNATIVE
339 * Alternative patch sites consider 48 bits when determining when to patch
340 * the old instruction sequence with the new. These bits are broken into a
341 * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
342 * patch site is for an erratum, identified by the 32-bit patch ID. When
343 * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
344 * further break down patch ID into two 16-bit numbers. The lower 16 bits
345 * are the cpufeature ID and the upper 16 bits are used for a value specific
346 * to the cpufeature and patch site. If the upper 16 bits are zero, then it
347 * implies no specific value is specified. cpufeatures that want to control
348 * patching on a per-site basis will provide non-zero values and implement
349 * checks here. The checks return true when patching should be done, and
352 static bool riscv_cpufeature_patch_check(u16 id, u16 value)
358 case RISCV_ISA_EXT_ZICBOZ:
360 * Zicboz alternative applications provide the maximum
361 * supported block size order, or zero when it doesn't
362 * matter. If the current block size exceeds the maximum,
363 * then the alternative cannot be applied.
365 return riscv_cboz_block_size <= (1U << value);
371 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
372 struct alt_entry *end,
375 struct alt_entry *alt;
376 void *oldptr, *altptr;
379 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
382 for (alt = begin; alt < end; alt++) {
383 if (alt->vendor_id != 0)
386 id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
388 if (id >= RISCV_ISA_EXT_MAX) {
389 WARN(1, "This extension id:%d is not in ISA extension list", id);
393 if (!__riscv_isa_extension_available(NULL, id))
396 value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
397 if (!riscv_cpufeature_patch_check(id, value))
400 oldptr = ALT_OLD_PTR(alt);
401 altptr = ALT_ALT_PTR(alt);
403 mutex_lock(&text_mutex);
404 patch_text_nosync(oldptr, altptr, alt->alt_len);
405 riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
406 mutex_unlock(&text_mutex);