Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copied from arch/arm64/kernel/cpufeature.c
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 * Copyright (C) 2017 SiFive
7 */
8
9#include <linux/bitmap.h>
10#include <linux/ctype.h>
11#include <linux/libfdt.h>
12#include <linux/log2.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <asm/alternative.h>
16#include <asm/cacheflush.h>
17#include <asm/errata_list.h>
18#include <asm/hwcap.h>
19#include <asm/patch.h>
20#include <asm/pgtable.h>
21#include <asm/processor.h>
22#include <asm/smp.h>
23#include <asm/switch_to.h>
24
25#define NUM_ALPHA_EXTS ('z' - 'a' + 1)
26
27unsigned long elf_hwcap __read_mostly;
28
29/* Host ISA bitmap */
30static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
31
32DEFINE_STATIC_KEY_ARRAY_FALSE(riscv_isa_ext_keys, RISCV_ISA_EXT_KEY_MAX);
33EXPORT_SYMBOL(riscv_isa_ext_keys);
34
35/**
36 * riscv_isa_extension_base() - Get base extension word
37 *
38 * @isa_bitmap: ISA bitmap to use
39 * Return: base extension word as unsigned long value
40 *
41 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
42 */
43unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
44{
45 if (!isa_bitmap)
46 return riscv_isa[0];
47 return isa_bitmap[0];
48}
49EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
50
51/**
52 * __riscv_isa_extension_available() - Check whether given extension
53 * is available or not
54 *
55 * @isa_bitmap: ISA bitmap to use
56 * @bit: bit position of the desired extension
57 * Return: true or false
58 *
59 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
60 */
61bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit)
62{
63 const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
64
65 if (bit >= RISCV_ISA_EXT_MAX)
66 return false;
67
68 return test_bit(bit, bmap) ? true : false;
69}
70EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
71
72static bool riscv_isa_extension_check(int id)
73{
74 switch (id) {
75 case RISCV_ISA_EXT_ZICBOM:
76 if (!riscv_cbom_block_size) {
77 pr_err("Zicbom detected in ISA string, but no cbom-block-size found\n");
78 return false;
79 } else if (!is_power_of_2(riscv_cbom_block_size)) {
80 pr_err("cbom-block-size present, but is not a power-of-2\n");
81 return false;
82 }
83 return true;
84 }
85
86 return true;
87}
88
89void __init riscv_fill_hwcap(void)
90{
91 struct device_node *node;
92 const char *isa;
93 char print_str[NUM_ALPHA_EXTS + 1];
94 int i, j, rc;
95 unsigned long isa2hwcap[26] = {0};
96 unsigned long hartid;
97
98 isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
99 isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
100 isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
101 isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
102 isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
103 isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
104
105 elf_hwcap = 0;
106
107 bitmap_zero(riscv_isa, RISCV_ISA_EXT_MAX);
108
109 for_each_of_cpu_node(node) {
110 unsigned long this_hwcap = 0;
111 DECLARE_BITMAP(this_isa, RISCV_ISA_EXT_MAX);
112 const char *temp;
113
114 rc = riscv_of_processor_hartid(node, &hartid);
115 if (rc < 0)
116 continue;
117
118 if (of_property_read_string(node, "riscv,isa", &isa)) {
119 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
120 continue;
121 }
122
123 temp = isa;
124#if IS_ENABLED(CONFIG_32BIT)
125 if (!strncmp(isa, "rv32", 4))
126 isa += 4;
127#elif IS_ENABLED(CONFIG_64BIT)
128 if (!strncmp(isa, "rv64", 4))
129 isa += 4;
130#endif
131 /* The riscv,isa DT property must start with rv64 or rv32 */
132 if (temp == isa)
133 continue;
134 bitmap_zero(this_isa, RISCV_ISA_EXT_MAX);
135 for (; *isa; ++isa) {
136 const char *ext = isa++;
137 const char *ext_end = isa;
138 bool ext_long = false, ext_err = false;
139
140 switch (*ext) {
141 case 's':
142 /**
143 * Workaround for invalid single-letter 's' & 'u'(QEMU).
144 * No need to set the bit in riscv_isa as 's' & 'u' are
145 * not valid ISA extensions. It works until multi-letter
146 * extension starting with "Su" appears.
147 */
148 if (ext[-1] != '_' && ext[1] == 'u') {
149 ++isa;
150 ext_err = true;
151 break;
152 }
153 fallthrough;
154 case 'x':
155 case 'z':
156 ext_long = true;
157 /* Multi-letter extension must be delimited */
158 for (; *isa && *isa != '_'; ++isa)
159 if (unlikely(!islower(*isa)
160 && !isdigit(*isa)))
161 ext_err = true;
162 /* Parse backwards */
163 ext_end = isa;
164 if (unlikely(ext_err))
165 break;
166 if (!isdigit(ext_end[-1]))
167 break;
168 /* Skip the minor version */
169 while (isdigit(*--ext_end))
170 ;
171 if (ext_end[0] != 'p'
172 || !isdigit(ext_end[-1])) {
173 /* Advance it to offset the pre-decrement */
174 ++ext_end;
175 break;
176 }
177 /* Skip the major version */
178 while (isdigit(*--ext_end))
179 ;
180 ++ext_end;
181 break;
182 default:
183 if (unlikely(!islower(*ext))) {
184 ext_err = true;
185 break;
186 }
187 /* Find next extension */
188 if (!isdigit(*isa))
189 break;
190 /* Skip the minor version */
191 while (isdigit(*++isa))
192 ;
193 if (*isa != 'p')
194 break;
195 if (!isdigit(*++isa)) {
196 --isa;
197 break;
198 }
199 /* Skip the major version */
200 while (isdigit(*++isa))
201 ;
202 break;
203 }
204 if (*isa != '_')
205 --isa;
206
207#define SET_ISA_EXT_MAP(name, bit) \
208 do { \
209 if ((ext_end - ext == sizeof(name) - 1) && \
210 !memcmp(ext, name, sizeof(name) - 1) && \
211 riscv_isa_extension_check(bit)) \
212 set_bit(bit, this_isa); \
213 } while (false) \
214
215 if (unlikely(ext_err))
216 continue;
217 if (!ext_long) {
218 int nr = *ext - 'a';
219
220 if (riscv_isa_extension_check(nr)) {
221 this_hwcap |= isa2hwcap[nr];
222 set_bit(nr, this_isa);
223 }
224 } else {
225 SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
226 SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
227 SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
228 SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE);
229 SET_ISA_EXT_MAP("sstc", RISCV_ISA_EXT_SSTC);
230 SET_ISA_EXT_MAP("svinval", RISCV_ISA_EXT_SVINVAL);
231 }
232#undef SET_ISA_EXT_MAP
233 }
234
235 /*
236 * All "okay" hart should have same isa. Set HWCAP based on
237 * common capabilities of every "okay" hart, in case they don't
238 * have.
239 */
240 if (elf_hwcap)
241 elf_hwcap &= this_hwcap;
242 else
243 elf_hwcap = this_hwcap;
244
245 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
246 bitmap_copy(riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
247 else
248 bitmap_and(riscv_isa, riscv_isa, this_isa, RISCV_ISA_EXT_MAX);
249 }
250
251 /* We don't support systems with F but without D, so mask those out
252 * here. */
253 if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
254 pr_info("This kernel does not support systems with F but not D\n");
255 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
256 }
257
258 memset(print_str, 0, sizeof(print_str));
259 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
260 if (riscv_isa[0] & BIT_MASK(i))
261 print_str[j++] = (char)('a' + i);
262 pr_info("riscv: base ISA extensions %s\n", print_str);
263
264 memset(print_str, 0, sizeof(print_str));
265 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
266 if (elf_hwcap & BIT_MASK(i))
267 print_str[j++] = (char)('a' + i);
268 pr_info("riscv: ELF capabilities %s\n", print_str);
269
270 for_each_set_bit(i, riscv_isa, RISCV_ISA_EXT_MAX) {
271 j = riscv_isa_ext2key(i);
272 if (j >= 0)
273 static_branch_enable(&riscv_isa_ext_keys[j]);
274 }
275}
276
277#ifdef CONFIG_RISCV_ALTERNATIVE
278static bool __init_or_module cpufeature_probe_svpbmt(unsigned int stage)
279{
280 if (!IS_ENABLED(CONFIG_RISCV_ISA_SVPBMT))
281 return false;
282
283 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
284 return false;
285
286 return riscv_isa_extension_available(NULL, SVPBMT);
287}
288
289static bool __init_or_module cpufeature_probe_zicbom(unsigned int stage)
290{
291 if (!IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM))
292 return false;
293
294 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
295 return false;
296
297 if (!riscv_isa_extension_available(NULL, ZICBOM))
298 return false;
299
300 riscv_noncoherent_supported();
301 return true;
302}
303
304/*
305 * Probe presence of individual extensions.
306 *
307 * This code may also be executed before kernel relocation, so we cannot use
308 * addresses generated by the address-of operator as they won't be valid in
309 * this context.
310 */
311static u32 __init_or_module cpufeature_probe(unsigned int stage)
312{
313 u32 cpu_req_feature = 0;
314
315 if (cpufeature_probe_svpbmt(stage))
316 cpu_req_feature |= BIT(CPUFEATURE_SVPBMT);
317
318 if (cpufeature_probe_zicbom(stage))
319 cpu_req_feature |= BIT(CPUFEATURE_ZICBOM);
320
321 return cpu_req_feature;
322}
323
324void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
325 struct alt_entry *end,
326 unsigned int stage)
327{
328 u32 cpu_req_feature = cpufeature_probe(stage);
329 struct alt_entry *alt;
330 u32 tmp;
331
332 for (alt = begin; alt < end; alt++) {
333 if (alt->vendor_id != 0)
334 continue;
335 if (alt->errata_id >= CPUFEATURE_NUMBER) {
336 WARN(1, "This feature id:%d is not in kernel cpufeature list",
337 alt->errata_id);
338 continue;
339 }
340
341 tmp = (1U << alt->errata_id);
342 if (cpu_req_feature & tmp)
343 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
344 }
345}
346#endif
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copied from arch/arm64/kernel/cpufeature.c
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 * Copyright (C) 2017 SiFive
7 */
8
9#include <linux/acpi.h>
10#include <linux/bitmap.h>
11#include <linux/cpu.h>
12#include <linux/cpuhotplug.h>
13#include <linux/ctype.h>
14#include <linux/log2.h>
15#include <linux/memory.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <asm/acpi.h>
19#include <asm/alternative.h>
20#include <asm/cacheflush.h>
21#include <asm/cpufeature.h>
22#include <asm/hwcap.h>
23#include <asm/patch.h>
24#include <asm/processor.h>
25#include <asm/sbi.h>
26#include <asm/vector.h>
27
28#define NUM_ALPHA_EXTS ('z' - 'a' + 1)
29
30unsigned long elf_hwcap __read_mostly;
31
32/* Host ISA bitmap */
33static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
34
35/* Per-cpu ISA extensions. */
36struct riscv_isainfo hart_isa[NR_CPUS];
37
38/**
39 * riscv_isa_extension_base() - Get base extension word
40 *
41 * @isa_bitmap: ISA bitmap to use
42 * Return: base extension word as unsigned long value
43 *
44 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
45 */
46unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
47{
48 if (!isa_bitmap)
49 return riscv_isa[0];
50 return isa_bitmap[0];
51}
52EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
53
54/**
55 * __riscv_isa_extension_available() - Check whether given extension
56 * is available or not
57 *
58 * @isa_bitmap: ISA bitmap to use
59 * @bit: bit position of the desired extension
60 * Return: true or false
61 *
62 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
63 */
64bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit)
65{
66 const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
67
68 if (bit >= RISCV_ISA_EXT_MAX)
69 return false;
70
71 return test_bit(bit, bmap) ? true : false;
72}
73EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
74
75static bool riscv_isa_extension_check(int id)
76{
77 switch (id) {
78 case RISCV_ISA_EXT_ZICBOM:
79 if (!riscv_cbom_block_size) {
80 pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
81 return false;
82 } else if (!is_power_of_2(riscv_cbom_block_size)) {
83 pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
84 return false;
85 }
86 return true;
87 case RISCV_ISA_EXT_ZICBOZ:
88 if (!riscv_cboz_block_size) {
89 pr_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n");
90 return false;
91 } else if (!is_power_of_2(riscv_cboz_block_size)) {
92 pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
93 return false;
94 }
95 return true;
96 case RISCV_ISA_EXT_INVALID:
97 return false;
98 }
99
100 return true;
101}
102
103#define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size) { \
104 .name = #_name, \
105 .property = #_name, \
106 .id = _id, \
107 .subset_ext_ids = _subset_exts, \
108 .subset_ext_size = _subset_exts_size \
109}
110
111#define __RISCV_ISA_EXT_DATA(_name, _id) _RISCV_ISA_EXT_DATA(_name, _id, NULL, 0)
112
113/* Used to declare pure "lasso" extension (Zk for instance) */
114#define __RISCV_ISA_EXT_BUNDLE(_name, _bundled_exts) \
115 _RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, ARRAY_SIZE(_bundled_exts))
116
117/* Used to declare extensions that are a superset of other extensions (Zvbb for instance) */
118#define __RISCV_ISA_EXT_SUPERSET(_name, _id, _sub_exts) \
119 _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts))
120
121static const unsigned int riscv_zk_bundled_exts[] = {
122 RISCV_ISA_EXT_ZBKB,
123 RISCV_ISA_EXT_ZBKC,
124 RISCV_ISA_EXT_ZBKX,
125 RISCV_ISA_EXT_ZKND,
126 RISCV_ISA_EXT_ZKNE,
127 RISCV_ISA_EXT_ZKR,
128 RISCV_ISA_EXT_ZKT,
129};
130
131static const unsigned int riscv_zkn_bundled_exts[] = {
132 RISCV_ISA_EXT_ZBKB,
133 RISCV_ISA_EXT_ZBKC,
134 RISCV_ISA_EXT_ZBKX,
135 RISCV_ISA_EXT_ZKND,
136 RISCV_ISA_EXT_ZKNE,
137 RISCV_ISA_EXT_ZKNH,
138};
139
140static const unsigned int riscv_zks_bundled_exts[] = {
141 RISCV_ISA_EXT_ZBKB,
142 RISCV_ISA_EXT_ZBKC,
143 RISCV_ISA_EXT_ZKSED,
144 RISCV_ISA_EXT_ZKSH
145};
146
147#define RISCV_ISA_EXT_ZVKN \
148 RISCV_ISA_EXT_ZVKNED, \
149 RISCV_ISA_EXT_ZVKNHB, \
150 RISCV_ISA_EXT_ZVKB, \
151 RISCV_ISA_EXT_ZVKT
152
153static const unsigned int riscv_zvkn_bundled_exts[] = {
154 RISCV_ISA_EXT_ZVKN
155};
156
157static const unsigned int riscv_zvknc_bundled_exts[] = {
158 RISCV_ISA_EXT_ZVKN,
159 RISCV_ISA_EXT_ZVBC
160};
161
162static const unsigned int riscv_zvkng_bundled_exts[] = {
163 RISCV_ISA_EXT_ZVKN,
164 RISCV_ISA_EXT_ZVKG
165};
166
167#define RISCV_ISA_EXT_ZVKS \
168 RISCV_ISA_EXT_ZVKSED, \
169 RISCV_ISA_EXT_ZVKSH, \
170 RISCV_ISA_EXT_ZVKB, \
171 RISCV_ISA_EXT_ZVKT
172
173static const unsigned int riscv_zvks_bundled_exts[] = {
174 RISCV_ISA_EXT_ZVKS
175};
176
177static const unsigned int riscv_zvksc_bundled_exts[] = {
178 RISCV_ISA_EXT_ZVKS,
179 RISCV_ISA_EXT_ZVBC
180};
181
182static const unsigned int riscv_zvksg_bundled_exts[] = {
183 RISCV_ISA_EXT_ZVKS,
184 RISCV_ISA_EXT_ZVKG
185};
186
187static const unsigned int riscv_zvbb_exts[] = {
188 RISCV_ISA_EXT_ZVKB
189};
190
191/*
192 * While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V
193 * privileged ISA, the existence of the CSRs is implied by any extension which
194 * specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the
195 * existence of the CSR, and treat it as a subset of those other extensions.
196 */
197static const unsigned int riscv_xlinuxenvcfg_exts[] = {
198 RISCV_ISA_EXT_XLINUXENVCFG
199};
200
201/*
202 * The canonical order of ISA extension names in the ISA string is defined in
203 * chapter 27 of the unprivileged specification.
204 *
205 * Ordinarily, for in-kernel data structures, this order is unimportant but
206 * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
207 *
208 * The specification uses vague wording, such as should, when it comes to
209 * ordering, so for our purposes the following rules apply:
210 *
211 * 1. All multi-letter extensions must be separated from other extensions by an
212 * underscore.
213 *
214 * 2. Additional standard extensions (starting with 'Z') must be sorted after
215 * single-letter extensions and before any higher-privileged extensions.
216 *
217 * 3. The first letter following the 'Z' conventionally indicates the most
218 * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
219 * If multiple 'Z' extensions are named, they must be ordered first by
220 * category, then alphabetically within a category.
221 *
222 * 3. Standard supervisor-level extensions (starting with 'S') must be listed
223 * after standard unprivileged extensions. If multiple supervisor-level
224 * extensions are listed, they must be ordered alphabetically.
225 *
226 * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
227 * after any lower-privileged, standard extensions. If multiple
228 * machine-level extensions are listed, they must be ordered
229 * alphabetically.
230 *
231 * 5. Non-standard extensions (starting with 'X') must be listed after all
232 * standard extensions. If multiple non-standard extensions are listed, they
233 * must be ordered alphabetically.
234 *
235 * An example string following the order is:
236 * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
237 *
238 * New entries to this struct should follow the ordering rules described above.
239 */
240const struct riscv_isa_ext_data riscv_isa_ext[] = {
241 __RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
242 __RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
243 __RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
244 __RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
245 __RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
246 __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
247 __RISCV_ISA_EXT_DATA(c, RISCV_ISA_EXT_c),
248 __RISCV_ISA_EXT_DATA(v, RISCV_ISA_EXT_v),
249 __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
250 __RISCV_ISA_EXT_SUPERSET(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts),
251 __RISCV_ISA_EXT_SUPERSET(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts),
252 __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
253 __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
254 __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
255 __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
256 __RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL),
257 __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
258 __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
259 __RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS),
260 __RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA),
261 __RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH),
262 __RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN),
263 __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
264 __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
265 __RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC),
266 __RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB),
267 __RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC),
268 __RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX),
269 __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
270 __RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts),
271 __RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts),
272 __RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
273 __RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
274 __RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
275 __RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
276 __RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
277 __RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT),
278 __RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED),
279 __RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH),
280 __RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO),
281 __RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts),
282 __RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC),
283 __RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH),
284 __RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN),
285 __RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB),
286 __RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG),
287 __RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts),
288 __RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts),
289 __RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED),
290 __RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts),
291 __RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA),
292 __RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB),
293 __RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts),
294 __RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts),
295 __RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED),
296 __RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH),
297 __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts),
298 __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT),
299 __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
300 __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
301 __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
302 __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
303 __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
304 __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
305 __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
306 __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
307 __RISCV_ISA_EXT_DATA(xandespmu, RISCV_ISA_EXT_XANDESPMU),
308};
309
310const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
311
312static void __init match_isa_ext(const struct riscv_isa_ext_data *ext, const char *name,
313 const char *name_end, struct riscv_isainfo *isainfo)
314{
315 if ((name_end - name == strlen(ext->name)) &&
316 !strncasecmp(name, ext->name, name_end - name)) {
317 /*
318 * If this is a bundle, enable all the ISA extensions that
319 * comprise the bundle.
320 */
321 if (ext->subset_ext_size) {
322 for (int i = 0; i < ext->subset_ext_size; i++) {
323 if (riscv_isa_extension_check(ext->subset_ext_ids[i]))
324 set_bit(ext->subset_ext_ids[i], isainfo->isa);
325 }
326 }
327
328 /*
329 * This is valid even for bundle extensions which uses the RISCV_ISA_EXT_INVALID id
330 * (rejected by riscv_isa_extension_check()).
331 */
332 if (riscv_isa_extension_check(ext->id))
333 set_bit(ext->id, isainfo->isa);
334 }
335}
336
337static void __init riscv_parse_isa_string(unsigned long *this_hwcap, struct riscv_isainfo *isainfo,
338 unsigned long *isa2hwcap, const char *isa)
339{
340 /*
341 * For all possible cpus, we have already validated in
342 * the boot process that they at least contain "rv" and
343 * whichever of "32"/"64" this kernel supports, and so this
344 * section can be skipped.
345 */
346 isa += 4;
347
348 while (*isa) {
349 const char *ext = isa++;
350 const char *ext_end = isa;
351 bool ext_long = false, ext_err = false;
352
353 switch (*ext) {
354 case 's':
355 /*
356 * Workaround for invalid single-letter 's' & 'u' (QEMU).
357 * No need to set the bit in riscv_isa as 's' & 'u' are
358 * not valid ISA extensions. It works unless the first
359 * multi-letter extension in the ISA string begins with
360 * "Su" and is not prefixed with an underscore.
361 */
362 if (ext[-1] != '_' && ext[1] == 'u') {
363 ++isa;
364 ext_err = true;
365 break;
366 }
367 fallthrough;
368 case 'S':
369 case 'x':
370 case 'X':
371 case 'z':
372 case 'Z':
373 /*
374 * Before attempting to parse the extension itself, we find its end.
375 * As multi-letter extensions must be split from other multi-letter
376 * extensions with an "_", the end of a multi-letter extension will
377 * either be the null character or the "_" at the start of the next
378 * multi-letter extension.
379 *
380 * Next, as the extensions version is currently ignored, we
381 * eliminate that portion. This is done by parsing backwards from
382 * the end of the extension, removing any numbers. This may be a
383 * major or minor number however, so the process is repeated if a
384 * minor number was found.
385 *
386 * ext_end is intended to represent the first character *after* the
387 * name portion of an extension, but will be decremented to the last
388 * character itself while eliminating the extensions version number.
389 * A simple re-increment solves this problem.
390 */
391 ext_long = true;
392 for (; *isa && *isa != '_'; ++isa)
393 if (unlikely(!isalnum(*isa)))
394 ext_err = true;
395
396 ext_end = isa;
397 if (unlikely(ext_err))
398 break;
399
400 if (!isdigit(ext_end[-1]))
401 break;
402
403 while (isdigit(*--ext_end))
404 ;
405
406 if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
407 ++ext_end;
408 break;
409 }
410
411 while (isdigit(*--ext_end))
412 ;
413
414 ++ext_end;
415 break;
416 default:
417 /*
418 * Things are a little easier for single-letter extensions, as they
419 * are parsed forwards.
420 *
421 * After checking that our starting position is valid, we need to
422 * ensure that, when isa was incremented at the start of the loop,
423 * that it arrived at the start of the next extension.
424 *
425 * If we are already on a non-digit, there is nothing to do. Either
426 * we have a multi-letter extension's _, or the start of an
427 * extension.
428 *
429 * Otherwise we have found the current extension's major version
430 * number. Parse past it, and a subsequent p/minor version number
431 * if present. The `p` extension must not appear immediately after
432 * a number, so there is no fear of missing it.
433 *
434 */
435 if (unlikely(!isalpha(*ext))) {
436 ext_err = true;
437 break;
438 }
439
440 if (!isdigit(*isa))
441 break;
442
443 while (isdigit(*++isa))
444 ;
445
446 if (tolower(*isa) != 'p')
447 break;
448
449 if (!isdigit(*++isa)) {
450 --isa;
451 break;
452 }
453
454 while (isdigit(*++isa))
455 ;
456
457 break;
458 }
459
460 /*
461 * The parser expects that at the start of an iteration isa points to the
462 * first character of the next extension. As we stop parsing an extension
463 * on meeting a non-alphanumeric character, an extra increment is needed
464 * where the succeeding extension is a multi-letter prefixed with an "_".
465 */
466 if (*isa == '_')
467 ++isa;
468
469 if (unlikely(ext_err))
470 continue;
471 if (!ext_long) {
472 int nr = tolower(*ext) - 'a';
473
474 if (riscv_isa_extension_check(nr)) {
475 *this_hwcap |= isa2hwcap[nr];
476 set_bit(nr, isainfo->isa);
477 }
478 } else {
479 for (int i = 0; i < riscv_isa_ext_count; i++)
480 match_isa_ext(&riscv_isa_ext[i], ext, ext_end, isainfo);
481 }
482 }
483}
484
485static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
486{
487 struct device_node *node;
488 const char *isa;
489 int rc;
490 struct acpi_table_header *rhct;
491 acpi_status status;
492 unsigned int cpu;
493 u64 boot_vendorid;
494 u64 boot_archid;
495
496 if (!acpi_disabled) {
497 status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
498 if (ACPI_FAILURE(status))
499 return;
500 }
501
502 boot_vendorid = riscv_get_mvendorid();
503 boot_archid = riscv_get_marchid();
504
505 for_each_possible_cpu(cpu) {
506 struct riscv_isainfo *isainfo = &hart_isa[cpu];
507 unsigned long this_hwcap = 0;
508
509 if (acpi_disabled) {
510 node = of_cpu_device_node_get(cpu);
511 if (!node) {
512 pr_warn("Unable to find cpu node\n");
513 continue;
514 }
515
516 rc = of_property_read_string(node, "riscv,isa", &isa);
517 of_node_put(node);
518 if (rc) {
519 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
520 continue;
521 }
522 } else {
523 rc = acpi_get_riscv_isa(rhct, cpu, &isa);
524 if (rc < 0) {
525 pr_warn("Unable to get ISA for the hart - %d\n", cpu);
526 continue;
527 }
528 }
529
530 riscv_parse_isa_string(&this_hwcap, isainfo, isa2hwcap, isa);
531
532 /*
533 * These ones were as they were part of the base ISA when the
534 * port & dt-bindings were upstreamed, and so can be set
535 * unconditionally where `i` is in riscv,isa on DT systems.
536 */
537 if (acpi_disabled) {
538 set_bit(RISCV_ISA_EXT_ZICSR, isainfo->isa);
539 set_bit(RISCV_ISA_EXT_ZIFENCEI, isainfo->isa);
540 set_bit(RISCV_ISA_EXT_ZICNTR, isainfo->isa);
541 set_bit(RISCV_ISA_EXT_ZIHPM, isainfo->isa);
542 }
543
544 /*
545 * "V" in ISA strings is ambiguous in practice: it should mean
546 * just the standard V-1.0 but vendors aren't well behaved.
547 * Many vendors with T-Head CPU cores which implement the 0.7.1
548 * version of the vector specification put "v" into their DTs.
549 * CPU cores with the ratified spec will contain non-zero
550 * marchid.
551 */
552 if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) {
553 this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v];
554 clear_bit(RISCV_ISA_EXT_v, isainfo->isa);
555 }
556
557 /*
558 * All "okay" hart should have same isa. Set HWCAP based on
559 * common capabilities of every "okay" hart, in case they don't
560 * have.
561 */
562 if (elf_hwcap)
563 elf_hwcap &= this_hwcap;
564 else
565 elf_hwcap = this_hwcap;
566
567 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
568 bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
569 else
570 bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
571 }
572
573 if (!acpi_disabled && rhct)
574 acpi_put_table((struct acpi_table_header *)rhct);
575}
576
577static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
578{
579 unsigned int cpu;
580
581 for_each_possible_cpu(cpu) {
582 unsigned long this_hwcap = 0;
583 struct device_node *cpu_node;
584 struct riscv_isainfo *isainfo = &hart_isa[cpu];
585
586 cpu_node = of_cpu_device_node_get(cpu);
587 if (!cpu_node) {
588 pr_warn("Unable to find cpu node\n");
589 continue;
590 }
591
592 if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
593 of_node_put(cpu_node);
594 continue;
595 }
596
597 for (int i = 0; i < riscv_isa_ext_count; i++) {
598 const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
599
600 if (of_property_match_string(cpu_node, "riscv,isa-extensions",
601 ext->property) < 0)
602 continue;
603
604 if (ext->subset_ext_size) {
605 for (int j = 0; j < ext->subset_ext_size; j++) {
606 if (riscv_isa_extension_check(ext->subset_ext_ids[j]))
607 set_bit(ext->subset_ext_ids[j], isainfo->isa);
608 }
609 }
610
611 if (riscv_isa_extension_check(ext->id)) {
612 set_bit(ext->id, isainfo->isa);
613
614 /* Only single letter extensions get set in hwcap */
615 if (strnlen(riscv_isa_ext[i].name, 2) == 1)
616 this_hwcap |= isa2hwcap[riscv_isa_ext[i].id];
617 }
618 }
619
620 of_node_put(cpu_node);
621
622 /*
623 * All "okay" harts should have same isa. Set HWCAP based on
624 * common capabilities of every "okay" hart, in case they don't.
625 */
626 if (elf_hwcap)
627 elf_hwcap &= this_hwcap;
628 else
629 elf_hwcap = this_hwcap;
630
631 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
632 bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
633 else
634 bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
635 }
636
637 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
638 return -ENOENT;
639
640 return 0;
641}
642
643#ifdef CONFIG_RISCV_ISA_FALLBACK
644bool __initdata riscv_isa_fallback = true;
645#else
646bool __initdata riscv_isa_fallback;
647static int __init riscv_isa_fallback_setup(char *__unused)
648{
649 riscv_isa_fallback = true;
650 return 1;
651}
652early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
653#endif
654
655void __init riscv_fill_hwcap(void)
656{
657 char print_str[NUM_ALPHA_EXTS + 1];
658 unsigned long isa2hwcap[26] = {0};
659 int i, j;
660
661 isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
662 isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
663 isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
664 isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
665 isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
666 isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
667 isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
668
669 if (!acpi_disabled) {
670 riscv_fill_hwcap_from_isa_string(isa2hwcap);
671 } else {
672 int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
673
674 if (ret && riscv_isa_fallback) {
675 pr_info("Falling back to deprecated \"riscv,isa\"\n");
676 riscv_fill_hwcap_from_isa_string(isa2hwcap);
677 }
678 }
679
680 /*
681 * We don't support systems with F but without D, so mask those out
682 * here.
683 */
684 if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
685 pr_info("This kernel does not support systems with F but not D\n");
686 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
687 }
688
689 if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
690 riscv_v_setup_vsize();
691 /*
692 * ISA string in device tree might have 'v' flag, but
693 * CONFIG_RISCV_ISA_V is disabled in kernel.
694 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
695 */
696 if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
697 elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
698 }
699
700 memset(print_str, 0, sizeof(print_str));
701 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
702 if (riscv_isa[0] & BIT_MASK(i))
703 print_str[j++] = (char)('a' + i);
704 pr_info("riscv: base ISA extensions %s\n", print_str);
705
706 memset(print_str, 0, sizeof(print_str));
707 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
708 if (elf_hwcap & BIT_MASK(i))
709 print_str[j++] = (char)('a' + i);
710 pr_info("riscv: ELF capabilities %s\n", print_str);
711}
712
713unsigned long riscv_get_elf_hwcap(void)
714{
715 unsigned long hwcap;
716
717 hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
718
719 if (!riscv_v_vstate_ctrl_user_allowed())
720 hwcap &= ~COMPAT_HWCAP_ISA_V;
721
722 return hwcap;
723}
724
725void riscv_user_isa_enable(void)
726{
727 if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
728 csr_set(CSR_ENVCFG, ENVCFG_CBZE);
729}
730
731#ifdef CONFIG_RISCV_ALTERNATIVE
732/*
733 * Alternative patch sites consider 48 bits when determining when to patch
734 * the old instruction sequence with the new. These bits are broken into a
735 * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
736 * patch site is for an erratum, identified by the 32-bit patch ID. When
737 * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
738 * further break down patch ID into two 16-bit numbers. The lower 16 bits
739 * are the cpufeature ID and the upper 16 bits are used for a value specific
740 * to the cpufeature and patch site. If the upper 16 bits are zero, then it
741 * implies no specific value is specified. cpufeatures that want to control
742 * patching on a per-site basis will provide non-zero values and implement
743 * checks here. The checks return true when patching should be done, and
744 * false otherwise.
745 */
746static bool riscv_cpufeature_patch_check(u16 id, u16 value)
747{
748 if (!value)
749 return true;
750
751 switch (id) {
752 case RISCV_ISA_EXT_ZICBOZ:
753 /*
754 * Zicboz alternative applications provide the maximum
755 * supported block size order, or zero when it doesn't
756 * matter. If the current block size exceeds the maximum,
757 * then the alternative cannot be applied.
758 */
759 return riscv_cboz_block_size <= (1U << value);
760 }
761
762 return false;
763}
764
765void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
766 struct alt_entry *end,
767 unsigned int stage)
768{
769 struct alt_entry *alt;
770 void *oldptr, *altptr;
771 u16 id, value;
772
773 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
774 return;
775
776 for (alt = begin; alt < end; alt++) {
777 if (alt->vendor_id != 0)
778 continue;
779
780 id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
781
782 if (id >= RISCV_ISA_EXT_MAX) {
783 WARN(1, "This extension id:%d is not in ISA extension list", id);
784 continue;
785 }
786
787 if (!__riscv_isa_extension_available(NULL, id))
788 continue;
789
790 value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
791 if (!riscv_cpufeature_patch_check(id, value))
792 continue;
793
794 oldptr = ALT_OLD_PTR(alt);
795 altptr = ALT_ALT_PTR(alt);
796
797 mutex_lock(&text_mutex);
798 patch_text_nosync(oldptr, altptr, alt->alt_len);
799 riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
800 mutex_unlock(&text_mutex);
801 }
802}
803#endif