xref: /linux/arch/riscv/kernel/cpufeature.c (revision 2eff01ee2881becc9daaa0d53477ec202136b1f4)
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/text-patching.h>
24 #include <asm/hwprobe.h>
25 #include <asm/processor.h>
26 #include <asm/sbi.h>
27 #include <asm/vector.h>
28 #include <asm/vendor_extensions.h>
29 
30 #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
31 
32 static bool any_cpu_has_zicboz;
33 
34 unsigned long elf_hwcap __read_mostly;
35 
36 /* Host ISA bitmap */
37 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
38 
39 /* Per-cpu ISA extensions. */
40 struct riscv_isainfo hart_isa[NR_CPUS];
41 
42 /**
43  * riscv_isa_extension_base() - Get base extension word
44  *
45  * @isa_bitmap: ISA bitmap to use
46  * Return: base extension word as unsigned long value
47  *
48  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
49  */
50 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
51 {
52 	if (!isa_bitmap)
53 		return riscv_isa[0];
54 	return isa_bitmap[0];
55 }
56 EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
57 
58 /**
59  * __riscv_isa_extension_available() - Check whether given extension
60  * is available or not
61  *
62  * @isa_bitmap: ISA bitmap to use
63  * @bit: bit position of the desired extension
64  * Return: true or false
65  *
66  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
67  */
68 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit)
69 {
70 	const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
71 
72 	if (bit >= RISCV_ISA_EXT_MAX)
73 		return false;
74 
75 	return test_bit(bit, bmap) ? true : false;
76 }
77 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
78 
79 static int riscv_ext_zicbom_validate(const struct riscv_isa_ext_data *data,
80 				     const unsigned long *isa_bitmap)
81 {
82 	if (!riscv_cbom_block_size) {
83 		pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
84 		return -EINVAL;
85 	}
86 	if (!is_power_of_2(riscv_cbom_block_size)) {
87 		pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
88 		return -EINVAL;
89 	}
90 	return 0;
91 }
92 
93 static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
94 				     const unsigned long *isa_bitmap)
95 {
96 	if (!riscv_cboz_block_size) {
97 		pr_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n");
98 		return -EINVAL;
99 	}
100 	if (!is_power_of_2(riscv_cboz_block_size)) {
101 		pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
102 		return -EINVAL;
103 	}
104 	any_cpu_has_zicboz = true;
105 	return 0;
106 }
107 
108 static int riscv_ext_zca_depends(const struct riscv_isa_ext_data *data,
109 				 const unsigned long *isa_bitmap)
110 {
111 	if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA))
112 		return 0;
113 
114 	return -EPROBE_DEFER;
115 }
116 static int riscv_ext_zcd_validate(const struct riscv_isa_ext_data *data,
117 				  const unsigned long *isa_bitmap)
118 {
119 	if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) &&
120 	    __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_d))
121 		return 0;
122 
123 	return -EPROBE_DEFER;
124 }
125 
126 static int riscv_ext_zcf_validate(const struct riscv_isa_ext_data *data,
127 				  const unsigned long *isa_bitmap)
128 {
129 	if (IS_ENABLED(CONFIG_64BIT))
130 		return -EINVAL;
131 
132 	if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) &&
133 	    __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_f))
134 		return 0;
135 
136 	return -EPROBE_DEFER;
137 }
138 
139 static const unsigned int riscv_zk_bundled_exts[] = {
140 	RISCV_ISA_EXT_ZBKB,
141 	RISCV_ISA_EXT_ZBKC,
142 	RISCV_ISA_EXT_ZBKX,
143 	RISCV_ISA_EXT_ZKND,
144 	RISCV_ISA_EXT_ZKNE,
145 	RISCV_ISA_EXT_ZKR,
146 	RISCV_ISA_EXT_ZKT,
147 };
148 
149 static const unsigned int riscv_zkn_bundled_exts[] = {
150 	RISCV_ISA_EXT_ZBKB,
151 	RISCV_ISA_EXT_ZBKC,
152 	RISCV_ISA_EXT_ZBKX,
153 	RISCV_ISA_EXT_ZKND,
154 	RISCV_ISA_EXT_ZKNE,
155 	RISCV_ISA_EXT_ZKNH,
156 };
157 
158 static const unsigned int riscv_zks_bundled_exts[] = {
159 	RISCV_ISA_EXT_ZBKB,
160 	RISCV_ISA_EXT_ZBKC,
161 	RISCV_ISA_EXT_ZKSED,
162 	RISCV_ISA_EXT_ZKSH
163 };
164 
165 #define RISCV_ISA_EXT_ZVKN	\
166 	RISCV_ISA_EXT_ZVKNED,	\
167 	RISCV_ISA_EXT_ZVKNHB,	\
168 	RISCV_ISA_EXT_ZVKB,	\
169 	RISCV_ISA_EXT_ZVKT
170 
171 static const unsigned int riscv_zvkn_bundled_exts[] = {
172 	RISCV_ISA_EXT_ZVKN
173 };
174 
175 static const unsigned int riscv_zvknc_bundled_exts[] = {
176 	RISCV_ISA_EXT_ZVKN,
177 	RISCV_ISA_EXT_ZVBC
178 };
179 
180 static const unsigned int riscv_zvkng_bundled_exts[] = {
181 	RISCV_ISA_EXT_ZVKN,
182 	RISCV_ISA_EXT_ZVKG
183 };
184 
185 #define RISCV_ISA_EXT_ZVKS	\
186 	RISCV_ISA_EXT_ZVKSED,	\
187 	RISCV_ISA_EXT_ZVKSH,	\
188 	RISCV_ISA_EXT_ZVKB,	\
189 	RISCV_ISA_EXT_ZVKT
190 
191 static const unsigned int riscv_zvks_bundled_exts[] = {
192 	RISCV_ISA_EXT_ZVKS
193 };
194 
195 static const unsigned int riscv_zvksc_bundled_exts[] = {
196 	RISCV_ISA_EXT_ZVKS,
197 	RISCV_ISA_EXT_ZVBC
198 };
199 
200 static const unsigned int riscv_zvksg_bundled_exts[] = {
201 	RISCV_ISA_EXT_ZVKS,
202 	RISCV_ISA_EXT_ZVKG
203 };
204 
205 static const unsigned int riscv_zvbb_exts[] = {
206 	RISCV_ISA_EXT_ZVKB
207 };
208 
209 #define RISCV_ISA_EXT_ZVE64F_IMPLY_LIST	\
210 	RISCV_ISA_EXT_ZVE64X,		\
211 	RISCV_ISA_EXT_ZVE32F,		\
212 	RISCV_ISA_EXT_ZVE32X
213 
214 #define RISCV_ISA_EXT_ZVE64D_IMPLY_LIST	\
215 	RISCV_ISA_EXT_ZVE64F,		\
216 	RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
217 
218 #define RISCV_ISA_EXT_V_IMPLY_LIST	\
219 	RISCV_ISA_EXT_ZVE64D,		\
220 	RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
221 
222 static const unsigned int riscv_zve32f_exts[] = {
223 	RISCV_ISA_EXT_ZVE32X
224 };
225 
226 static const unsigned int riscv_zve64f_exts[] = {
227 	RISCV_ISA_EXT_ZVE64F_IMPLY_LIST
228 };
229 
230 static const unsigned int riscv_zve64d_exts[] = {
231 	RISCV_ISA_EXT_ZVE64D_IMPLY_LIST
232 };
233 
234 static const unsigned int riscv_v_exts[] = {
235 	RISCV_ISA_EXT_V_IMPLY_LIST
236 };
237 
238 static const unsigned int riscv_zve64x_exts[] = {
239 	RISCV_ISA_EXT_ZVE32X,
240 	RISCV_ISA_EXT_ZVE64X
241 };
242 
243 /*
244  * While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V
245  * privileged ISA, the existence of the CSRs is implied by any extension which
246  * specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the
247  * existence of the CSR, and treat it as a subset of those other extensions.
248  */
249 static const unsigned int riscv_xlinuxenvcfg_exts[] = {
250 	RISCV_ISA_EXT_XLINUXENVCFG
251 };
252 
253 /*
254  * Zc* spec states that:
255  * - C always implies Zca
256  * - C+F implies Zcf (RV32 only)
257  * - C+D implies Zcd
258  *
259  * These extensions will be enabled and then validated depending on the
260  * availability of F/D RV32.
261  */
262 static const unsigned int riscv_c_exts[] = {
263 	RISCV_ISA_EXT_ZCA,
264 	RISCV_ISA_EXT_ZCF,
265 	RISCV_ISA_EXT_ZCD,
266 };
267 
268 /*
269  * The canonical order of ISA extension names in the ISA string is defined in
270  * chapter 27 of the unprivileged specification.
271  *
272  * Ordinarily, for in-kernel data structures, this order is unimportant but
273  * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
274  *
275  * The specification uses vague wording, such as should, when it comes to
276  * ordering, so for our purposes the following rules apply:
277  *
278  * 1. All multi-letter extensions must be separated from other extensions by an
279  *    underscore.
280  *
281  * 2. Additional standard extensions (starting with 'Z') must be sorted after
282  *    single-letter extensions and before any higher-privileged extensions.
283  *
284  * 3. The first letter following the 'Z' conventionally indicates the most
285  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
286  *    If multiple 'Z' extensions are named, they must be ordered first by
287  *    category, then alphabetically within a category.
288  *
289  * 3. Standard supervisor-level extensions (starting with 'S') must be listed
290  *    after standard unprivileged extensions.  If multiple supervisor-level
291  *    extensions are listed, they must be ordered alphabetically.
292  *
293  * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
294  *    after any lower-privileged, standard extensions.  If multiple
295  *    machine-level extensions are listed, they must be ordered
296  *    alphabetically.
297  *
298  * 5. Non-standard extensions (starting with 'X') must be listed after all
299  *    standard extensions. If multiple non-standard extensions are listed, they
300  *    must be ordered alphabetically.
301  *
302  * An example string following the order is:
303  *    rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
304  *
305  * New entries to this struct should follow the ordering rules described above.
306  */
307 const struct riscv_isa_ext_data riscv_isa_ext[] = {
308 	__RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
309 	__RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
310 	__RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
311 	__RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
312 	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
313 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
314 	__RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
315 	__RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts),
316 	__RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
317 	__RISCV_ISA_EXT_SUPERSET_VALIDATE(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts,
318 					  riscv_ext_zicbom_validate),
319 	__RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts,
320 					  riscv_ext_zicboz_validate),
321 	__RISCV_ISA_EXT_DATA(ziccrse, RISCV_ISA_EXT_ZICCRSE),
322 	__RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
323 	__RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
324 	__RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
325 	__RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
326 	__RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL),
327 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
328 	__RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
329 	__RISCV_ISA_EXT_DATA(zimop, RISCV_ISA_EXT_ZIMOP),
330 	__RISCV_ISA_EXT_DATA(zabha, RISCV_ISA_EXT_ZABHA),
331 	__RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS),
332 	__RISCV_ISA_EXT_DATA(zawrs, RISCV_ISA_EXT_ZAWRS),
333 	__RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA),
334 	__RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH),
335 	__RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN),
336 	__RISCV_ISA_EXT_DATA(zca, RISCV_ISA_EXT_ZCA),
337 	__RISCV_ISA_EXT_DATA_VALIDATE(zcb, RISCV_ISA_EXT_ZCB, riscv_ext_zca_depends),
338 	__RISCV_ISA_EXT_DATA_VALIDATE(zcd, RISCV_ISA_EXT_ZCD, riscv_ext_zcd_validate),
339 	__RISCV_ISA_EXT_DATA_VALIDATE(zcf, RISCV_ISA_EXT_ZCF, riscv_ext_zcf_validate),
340 	__RISCV_ISA_EXT_DATA_VALIDATE(zcmop, RISCV_ISA_EXT_ZCMOP, riscv_ext_zca_depends),
341 	__RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
342 	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
343 	__RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC),
344 	__RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB),
345 	__RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC),
346 	__RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX),
347 	__RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
348 	__RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts),
349 	__RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts),
350 	__RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
351 	__RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
352 	__RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
353 	__RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
354 	__RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
355 	__RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT),
356 	__RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED),
357 	__RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH),
358 	__RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO),
359 	__RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts),
360 	__RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC),
361 	__RISCV_ISA_EXT_SUPERSET(zve32f, RISCV_ISA_EXT_ZVE32F, riscv_zve32f_exts),
362 	__RISCV_ISA_EXT_DATA(zve32x, RISCV_ISA_EXT_ZVE32X),
363 	__RISCV_ISA_EXT_SUPERSET(zve64d, RISCV_ISA_EXT_ZVE64D, riscv_zve64d_exts),
364 	__RISCV_ISA_EXT_SUPERSET(zve64f, RISCV_ISA_EXT_ZVE64F, riscv_zve64f_exts),
365 	__RISCV_ISA_EXT_SUPERSET(zve64x, RISCV_ISA_EXT_ZVE64X, riscv_zve64x_exts),
366 	__RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH),
367 	__RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN),
368 	__RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB),
369 	__RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG),
370 	__RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts),
371 	__RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts),
372 	__RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED),
373 	__RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts),
374 	__RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA),
375 	__RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB),
376 	__RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts),
377 	__RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts),
378 	__RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED),
379 	__RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH),
380 	__RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts),
381 	__RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT),
382 	__RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
383 	__RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
384 	__RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
385 	__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
386 	__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
387 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
388 	__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
389 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
390 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
391 	__RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
392 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
393 	__RISCV_ISA_EXT_DATA(svvptc, RISCV_ISA_EXT_SVVPTC),
394 };
395 
396 const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
397 
398 static void riscv_isa_set_ext(const struct riscv_isa_ext_data *ext, unsigned long *bitmap)
399 {
400 	if (ext->id != RISCV_ISA_EXT_INVALID)
401 		set_bit(ext->id, bitmap);
402 
403 	for (int i = 0; i < ext->subset_ext_size; i++) {
404 		if (ext->subset_ext_ids[i] != RISCV_ISA_EXT_INVALID)
405 			set_bit(ext->subset_ext_ids[i], bitmap);
406 	}
407 }
408 
409 static const struct riscv_isa_ext_data *riscv_get_isa_ext_data(unsigned int ext_id)
410 {
411 	for (int i = 0; i < riscv_isa_ext_count; i++) {
412 		if (riscv_isa_ext[i].id == ext_id)
413 			return &riscv_isa_ext[i];
414 	}
415 
416 	return NULL;
417 }
418 
419 /*
420  * "Resolve" a source ISA bitmap into one that matches kernel configuration as
421  * well as correct extension dependencies. Some extensions depends on specific
422  * kernel configuration to be usable (V needs CONFIG_RISCV_ISA_V for instance)
423  * and this function will actually validate all the extensions provided in
424  * source_isa into the resolved_isa based on extensions validate() callbacks.
425  */
426 static void __init riscv_resolve_isa(unsigned long *source_isa,
427 				     unsigned long *resolved_isa, unsigned long *this_hwcap,
428 				     unsigned long *isa2hwcap)
429 {
430 	bool loop;
431 	const struct riscv_isa_ext_data *ext;
432 	DECLARE_BITMAP(prev_resolved_isa, RISCV_ISA_EXT_MAX);
433 	int max_loop_count = riscv_isa_ext_count, ret;
434 	unsigned int bit;
435 
436 	do {
437 		loop = false;
438 		if (max_loop_count-- < 0) {
439 			pr_err("Failed to reach a stable ISA state\n");
440 			return;
441 		}
442 		bitmap_copy(prev_resolved_isa, resolved_isa, RISCV_ISA_EXT_MAX);
443 		for_each_set_bit(bit, source_isa, RISCV_ISA_EXT_MAX) {
444 			ext = riscv_get_isa_ext_data(bit);
445 
446 			if (ext && ext->validate) {
447 				ret = ext->validate(ext, resolved_isa);
448 				if (ret == -EPROBE_DEFER) {
449 					loop = true;
450 					continue;
451 				} else if (ret) {
452 					/* Disable the extension entirely */
453 					clear_bit(bit, source_isa);
454 					continue;
455 				}
456 			}
457 
458 			set_bit(bit, resolved_isa);
459 			/* No need to keep it in source isa now that it is enabled */
460 			clear_bit(bit, source_isa);
461 
462 			/* Single letter extensions get set in hwcap */
463 			if (bit < RISCV_ISA_EXT_BASE)
464 				*this_hwcap |= isa2hwcap[bit];
465 		}
466 	} while (loop && memcmp(prev_resolved_isa, resolved_isa, sizeof(prev_resolved_isa)));
467 }
468 
469 static void __init match_isa_ext(const char *name, const char *name_end, unsigned long *bitmap)
470 {
471 	for (int i = 0; i < riscv_isa_ext_count; i++) {
472 		const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
473 
474 		if ((name_end - name == strlen(ext->name)) &&
475 		    !strncasecmp(name, ext->name, name_end - name)) {
476 			riscv_isa_set_ext(ext, bitmap);
477 			break;
478 		}
479 	}
480 }
481 
482 static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap)
483 {
484 	/*
485 	 * For all possible cpus, we have already validated in
486 	 * the boot process that they at least contain "rv" and
487 	 * whichever of "32"/"64" this kernel supports, and so this
488 	 * section can be skipped.
489 	 */
490 	isa += 4;
491 
492 	while (*isa) {
493 		const char *ext = isa++;
494 		const char *ext_end = isa;
495 		bool ext_err = false;
496 
497 		switch (*ext) {
498 		case 'x':
499 		case 'X':
500 			if (acpi_disabled)
501 				pr_warn_once("Vendor extensions are ignored in riscv,isa. Use riscv,isa-extensions instead.");
502 			/*
503 			 * To skip an extension, we find its end.
504 			 * As multi-letter extensions must be split from other multi-letter
505 			 * extensions with an "_", the end of a multi-letter extension will
506 			 * either be the null character or the "_" at the start of the next
507 			 * multi-letter extension.
508 			 */
509 			for (; *isa && *isa != '_'; ++isa)
510 				;
511 			ext_err = true;
512 			break;
513 		case 's':
514 			/*
515 			 * Workaround for invalid single-letter 's' & 'u' (QEMU).
516 			 * No need to set the bit in riscv_isa as 's' & 'u' are
517 			 * not valid ISA extensions. It works unless the first
518 			 * multi-letter extension in the ISA string begins with
519 			 * "Su" and is not prefixed with an underscore.
520 			 */
521 			if (ext[-1] != '_' && ext[1] == 'u') {
522 				++isa;
523 				ext_err = true;
524 				break;
525 			}
526 			fallthrough;
527 		case 'S':
528 		case 'z':
529 		case 'Z':
530 			/*
531 			 * Before attempting to parse the extension itself, we find its end.
532 			 * As multi-letter extensions must be split from other multi-letter
533 			 * extensions with an "_", the end of a multi-letter extension will
534 			 * either be the null character or the "_" at the start of the next
535 			 * multi-letter extension.
536 			 *
537 			 * Next, as the extensions version is currently ignored, we
538 			 * eliminate that portion. This is done by parsing backwards from
539 			 * the end of the extension, removing any numbers. This may be a
540 			 * major or minor number however, so the process is repeated if a
541 			 * minor number was found.
542 			 *
543 			 * ext_end is intended to represent the first character *after* the
544 			 * name portion of an extension, but will be decremented to the last
545 			 * character itself while eliminating the extensions version number.
546 			 * A simple re-increment solves this problem.
547 			 */
548 			for (; *isa && *isa != '_'; ++isa)
549 				if (unlikely(!isalnum(*isa)))
550 					ext_err = true;
551 
552 			ext_end = isa;
553 			if (unlikely(ext_err))
554 				break;
555 
556 			if (!isdigit(ext_end[-1]))
557 				break;
558 
559 			while (isdigit(*--ext_end))
560 				;
561 
562 			if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
563 				++ext_end;
564 				break;
565 			}
566 
567 			while (isdigit(*--ext_end))
568 				;
569 
570 			++ext_end;
571 			break;
572 		default:
573 			/*
574 			 * Things are a little easier for single-letter extensions, as they
575 			 * are parsed forwards.
576 			 *
577 			 * After checking that our starting position is valid, we need to
578 			 * ensure that, when isa was incremented at the start of the loop,
579 			 * that it arrived at the start of the next extension.
580 			 *
581 			 * If we are already on a non-digit, there is nothing to do. Either
582 			 * we have a multi-letter extension's _, or the start of an
583 			 * extension.
584 			 *
585 			 * Otherwise we have found the current extension's major version
586 			 * number. Parse past it, and a subsequent p/minor version number
587 			 * if present. The `p` extension must not appear immediately after
588 			 * a number, so there is no fear of missing it.
589 			 *
590 			 */
591 			if (unlikely(!isalpha(*ext))) {
592 				ext_err = true;
593 				break;
594 			}
595 
596 			if (!isdigit(*isa))
597 				break;
598 
599 			while (isdigit(*++isa))
600 				;
601 
602 			if (tolower(*isa) != 'p')
603 				break;
604 
605 			if (!isdigit(*++isa)) {
606 				--isa;
607 				break;
608 			}
609 
610 			while (isdigit(*++isa))
611 				;
612 
613 			break;
614 		}
615 
616 		/*
617 		 * The parser expects that at the start of an iteration isa points to the
618 		 * first character of the next extension. As we stop parsing an extension
619 		 * on meeting a non-alphanumeric character, an extra increment is needed
620 		 * where the succeeding extension is a multi-letter prefixed with an "_".
621 		 */
622 		if (*isa == '_')
623 			++isa;
624 
625 		if (unlikely(ext_err))
626 			continue;
627 
628 		match_isa_ext(ext, ext_end, bitmap);
629 	}
630 }
631 
632 static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
633 {
634 	struct device_node *node;
635 	const char *isa;
636 	int rc;
637 	struct acpi_table_header *rhct;
638 	acpi_status status;
639 	unsigned int cpu;
640 	u64 boot_vendorid;
641 	u64 boot_archid;
642 
643 	if (!acpi_disabled) {
644 		status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
645 		if (ACPI_FAILURE(status))
646 			return;
647 	}
648 
649 	boot_vendorid = riscv_get_mvendorid();
650 	boot_archid = riscv_get_marchid();
651 
652 	for_each_possible_cpu(cpu) {
653 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
654 		unsigned long this_hwcap = 0;
655 		DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 };
656 
657 		if (acpi_disabled) {
658 			node = of_cpu_device_node_get(cpu);
659 			if (!node) {
660 				pr_warn("Unable to find cpu node\n");
661 				continue;
662 			}
663 
664 			rc = of_property_read_string(node, "riscv,isa", &isa);
665 			of_node_put(node);
666 			if (rc) {
667 				pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
668 				continue;
669 			}
670 		} else {
671 			rc = acpi_get_riscv_isa(rhct, cpu, &isa);
672 			if (rc < 0) {
673 				pr_warn("Unable to get ISA for the hart - %d\n", cpu);
674 				continue;
675 			}
676 		}
677 
678 		riscv_parse_isa_string(isa, source_isa);
679 
680 		/*
681 		 * These ones were as they were part of the base ISA when the
682 		 * port & dt-bindings were upstreamed, and so can be set
683 		 * unconditionally where `i` is in riscv,isa on DT systems.
684 		 */
685 		if (acpi_disabled) {
686 			set_bit(RISCV_ISA_EXT_ZICSR, source_isa);
687 			set_bit(RISCV_ISA_EXT_ZIFENCEI, source_isa);
688 			set_bit(RISCV_ISA_EXT_ZICNTR, source_isa);
689 			set_bit(RISCV_ISA_EXT_ZIHPM, source_isa);
690 		}
691 
692 		/*
693 		 * "V" in ISA strings is ambiguous in practice: it should mean
694 		 * just the standard V-1.0 but vendors aren't well behaved.
695 		 * Many vendors with T-Head CPU cores which implement the 0.7.1
696 		 * version of the vector specification put "v" into their DTs.
697 		 * CPU cores with the ratified spec will contain non-zero
698 		 * marchid.
699 		 */
700 		if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) {
701 			this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v];
702 			clear_bit(RISCV_ISA_EXT_v, source_isa);
703 		}
704 
705 		riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap);
706 
707 		/*
708 		 * All "okay" hart should have same isa. Set HWCAP based on
709 		 * common capabilities of every "okay" hart, in case they don't
710 		 * have.
711 		 */
712 		if (elf_hwcap)
713 			elf_hwcap &= this_hwcap;
714 		else
715 			elf_hwcap = this_hwcap;
716 
717 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
718 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
719 		else
720 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
721 	}
722 
723 	if (!acpi_disabled && rhct)
724 		acpi_put_table((struct acpi_table_header *)rhct);
725 }
726 
727 static void __init riscv_fill_cpu_vendor_ext(struct device_node *cpu_node, int cpu)
728 {
729 	if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT))
730 		return;
731 
732 	for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) {
733 		struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i];
734 
735 		for (int j = 0; j < ext_list->ext_data_count; j++) {
736 			const struct riscv_isa_ext_data ext = ext_list->ext_data[j];
737 			struct riscv_isavendorinfo *isavendorinfo = &ext_list->per_hart_isa_bitmap[cpu];
738 
739 			if (of_property_match_string(cpu_node, "riscv,isa-extensions",
740 						     ext.property) < 0)
741 				continue;
742 
743 			/*
744 			 * Assume that subset extensions are all members of the
745 			 * same vendor.
746 			 */
747 			if (ext.subset_ext_size)
748 				for (int k = 0; k < ext.subset_ext_size; k++)
749 					set_bit(ext.subset_ext_ids[k], isavendorinfo->isa);
750 
751 			set_bit(ext.id, isavendorinfo->isa);
752 		}
753 	}
754 }
755 
756 /*
757  * Populate all_harts_isa_bitmap for each vendor with all of the extensions that
758  * are shared across CPUs for that vendor.
759  */
760 static void __init riscv_fill_vendor_ext_list(int cpu)
761 {
762 	if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT))
763 		return;
764 
765 	for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) {
766 		struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i];
767 
768 		if (!ext_list->is_initialized) {
769 			bitmap_copy(ext_list->all_harts_isa_bitmap.isa,
770 				    ext_list->per_hart_isa_bitmap[cpu].isa,
771 				    RISCV_ISA_VENDOR_EXT_MAX);
772 			ext_list->is_initialized = true;
773 		} else {
774 			bitmap_and(ext_list->all_harts_isa_bitmap.isa,
775 				   ext_list->all_harts_isa_bitmap.isa,
776 				   ext_list->per_hart_isa_bitmap[cpu].isa,
777 				   RISCV_ISA_VENDOR_EXT_MAX);
778 		}
779 	}
780 }
781 
782 static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
783 {
784 	unsigned int cpu;
785 
786 	for_each_possible_cpu(cpu) {
787 		unsigned long this_hwcap = 0;
788 		struct device_node *cpu_node;
789 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
790 		DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 };
791 
792 		cpu_node = of_cpu_device_node_get(cpu);
793 		if (!cpu_node) {
794 			pr_warn("Unable to find cpu node\n");
795 			continue;
796 		}
797 
798 		if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
799 			of_node_put(cpu_node);
800 			continue;
801 		}
802 
803 		for (int i = 0; i < riscv_isa_ext_count; i++) {
804 			const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
805 
806 			if (of_property_match_string(cpu_node, "riscv,isa-extensions",
807 						     ext->property) < 0)
808 				continue;
809 
810 			riscv_isa_set_ext(ext, source_isa);
811 		}
812 
813 		riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap);
814 		riscv_fill_cpu_vendor_ext(cpu_node, cpu);
815 
816 		of_node_put(cpu_node);
817 
818 		/*
819 		 * All "okay" harts should have same isa. Set HWCAP based on
820 		 * common capabilities of every "okay" hart, in case they don't.
821 		 */
822 		if (elf_hwcap)
823 			elf_hwcap &= this_hwcap;
824 		else
825 			elf_hwcap = this_hwcap;
826 
827 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
828 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
829 		else
830 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
831 
832 		riscv_fill_vendor_ext_list(cpu);
833 	}
834 
835 	if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
836 		return -ENOENT;
837 
838 	return 0;
839 }
840 
841 #ifdef CONFIG_RISCV_ISA_FALLBACK
842 bool __initdata riscv_isa_fallback = true;
843 #else
844 bool __initdata riscv_isa_fallback;
845 static int __init riscv_isa_fallback_setup(char *__unused)
846 {
847 	riscv_isa_fallback = true;
848 	return 1;
849 }
850 early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
851 #endif
852 
853 void __init riscv_fill_hwcap(void)
854 {
855 	char print_str[NUM_ALPHA_EXTS + 1];
856 	unsigned long isa2hwcap[26] = {0};
857 	int i, j;
858 
859 	isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
860 	isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
861 	isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
862 	isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
863 	isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
864 	isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
865 	isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
866 
867 	if (!acpi_disabled) {
868 		riscv_fill_hwcap_from_isa_string(isa2hwcap);
869 	} else {
870 		int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
871 
872 		if (ret && riscv_isa_fallback) {
873 			pr_info("Falling back to deprecated \"riscv,isa\"\n");
874 			riscv_fill_hwcap_from_isa_string(isa2hwcap);
875 		}
876 	}
877 
878 	/*
879 	 * We don't support systems with F but without D, so mask those out
880 	 * here.
881 	 */
882 	if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
883 		pr_info("This kernel does not support systems with F but not D\n");
884 		elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
885 	}
886 
887 	if (__riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ZVE32X)) {
888 		/*
889 		 * This cannot fail when called on the boot hart
890 		 */
891 		riscv_v_setup_vsize();
892 	}
893 
894 	if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
895 		/*
896 		 * ISA string in device tree might have 'v' flag, but
897 		 * CONFIG_RISCV_ISA_V is disabled in kernel.
898 		 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
899 		 */
900 		if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
901 			elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
902 	}
903 
904 	memset(print_str, 0, sizeof(print_str));
905 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
906 		if (riscv_isa[0] & BIT_MASK(i))
907 			print_str[j++] = (char)('a' + i);
908 	pr_info("riscv: base ISA extensions %s\n", print_str);
909 
910 	memset(print_str, 0, sizeof(print_str));
911 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
912 		if (elf_hwcap & BIT_MASK(i))
913 			print_str[j++] = (char)('a' + i);
914 	pr_info("riscv: ELF capabilities %s\n", print_str);
915 }
916 
917 unsigned long riscv_get_elf_hwcap(void)
918 {
919 	unsigned long hwcap;
920 
921 	hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
922 
923 	if (!riscv_v_vstate_ctrl_user_allowed())
924 		hwcap &= ~COMPAT_HWCAP_ISA_V;
925 
926 	return hwcap;
927 }
928 
929 void __init riscv_user_isa_enable(void)
930 {
931 	if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
932 		current->thread.envcfg |= ENVCFG_CBZE;
933 	else if (any_cpu_has_zicboz)
934 		pr_warn("Zicboz disabled as it is unavailable on some harts\n");
935 }
936 
937 #ifdef CONFIG_RISCV_ALTERNATIVE
938 /*
939  * Alternative patch sites consider 48 bits when determining when to patch
940  * the old instruction sequence with the new. These bits are broken into a
941  * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
942  * patch site is for an erratum, identified by the 32-bit patch ID. When
943  * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
944  * further break down patch ID into two 16-bit numbers. The lower 16 bits
945  * are the cpufeature ID and the upper 16 bits are used for a value specific
946  * to the cpufeature and patch site. If the upper 16 bits are zero, then it
947  * implies no specific value is specified. cpufeatures that want to control
948  * patching on a per-site basis will provide non-zero values and implement
949  * checks here. The checks return true when patching should be done, and
950  * false otherwise.
951  */
952 static bool riscv_cpufeature_patch_check(u16 id, u16 value)
953 {
954 	if (!value)
955 		return true;
956 
957 	switch (id) {
958 	case RISCV_ISA_EXT_ZICBOZ:
959 		/*
960 		 * Zicboz alternative applications provide the maximum
961 		 * supported block size order, or zero when it doesn't
962 		 * matter. If the current block size exceeds the maximum,
963 		 * then the alternative cannot be applied.
964 		 */
965 		return riscv_cboz_block_size <= (1U << value);
966 	}
967 
968 	return false;
969 }
970 
971 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
972 						  struct alt_entry *end,
973 						  unsigned int stage)
974 {
975 	struct alt_entry *alt;
976 	void *oldptr, *altptr;
977 	u16 id, value, vendor;
978 
979 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
980 		return;
981 
982 	for (alt = begin; alt < end; alt++) {
983 		id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
984 		vendor = PATCH_ID_CPUFEATURE_ID(alt->vendor_id);
985 
986 		/*
987 		 * Any alternative with a patch_id that is less than
988 		 * RISCV_ISA_EXT_MAX is interpreted as a standard extension.
989 		 *
990 		 * Any alternative with patch_id that is greater than or equal
991 		 * to RISCV_VENDOR_EXT_ALTERNATIVES_BASE is interpreted as a
992 		 * vendor extension.
993 		 */
994 		if (id < RISCV_ISA_EXT_MAX) {
995 			/*
996 			 * This patch should be treated as errata so skip
997 			 * processing here.
998 			 */
999 			if (alt->vendor_id != 0)
1000 				continue;
1001 
1002 			if (!__riscv_isa_extension_available(NULL, id))
1003 				continue;
1004 
1005 			value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
1006 			if (!riscv_cpufeature_patch_check(id, value))
1007 				continue;
1008 		} else if (id >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE) {
1009 			if (!__riscv_isa_vendor_extension_available(VENDOR_EXT_ALL_CPUS, vendor,
1010 								    id - RISCV_VENDOR_EXT_ALTERNATIVES_BASE))
1011 				continue;
1012 		} else {
1013 			WARN(1, "This extension id:%d is not in ISA extension list", id);
1014 			continue;
1015 		}
1016 
1017 		oldptr = ALT_OLD_PTR(alt);
1018 		altptr = ALT_ALT_PTR(alt);
1019 
1020 		mutex_lock(&text_mutex);
1021 		patch_text_nosync(oldptr, altptr, alt->alt_len);
1022 		riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
1023 		mutex_unlock(&text_mutex);
1024 	}
1025 }
1026 #endif
1027