xref: /openbmc/linux/arch/riscv/kernel/cpufeature.c (revision 1c9f8dff62d85ce00b0e99f774a84bd783af7cac)
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/ctype.h>
12 #include <linux/log2.h>
13 #include <linux/memory.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <asm/acpi.h>
17 #include <asm/alternative.h>
18 #include <asm/cacheflush.h>
19 #include <asm/cpufeature.h>
20 #include <asm/hwcap.h>
21 #include <asm/patch.h>
22 #include <asm/processor.h>
23 #include <asm/vector.h>
24 
25 #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
26 
27 unsigned long elf_hwcap __read_mostly;
28 
29 /* Host ISA bitmap */
30 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
31 
32 /* Per-cpu ISA extensions. */
33 struct riscv_isainfo hart_isa[NR_CPUS];
34 
35 /* Performance information */
36 DEFINE_PER_CPU(long, misaligned_access_speed);
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  */
46 unsigned 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 }
52 EXPORT_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  */
64 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, 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 }
73 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
74 
75 static 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, but no cboz-block-size found\n");
90 			return false;
91 		} else if (!is_power_of_2(riscv_cboz_block_size)) {
92 			pr_err("cboz-block-size present, but is not a power-of-2\n");
93 			return false;
94 		}
95 		return true;
96 	}
97 
98 	return true;
99 }
100 
101 #define __RISCV_ISA_EXT_DATA(_name, _id) {	\
102 	.name = #_name,				\
103 	.property = #_name,			\
104 	.id = _id,				\
105 }
106 
107 /*
108  * The canonical order of ISA extension names in the ISA string is defined in
109  * chapter 27 of the unprivileged specification.
110  *
111  * Ordinarily, for in-kernel data structures, this order is unimportant but
112  * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
113  *
114  * The specification uses vague wording, such as should, when it comes to
115  * ordering, so for our purposes the following rules apply:
116  *
117  * 1. All multi-letter extensions must be separated from other extensions by an
118  *    underscore.
119  *
120  * 2. Additional standard extensions (starting with 'Z') must be sorted after
121  *    single-letter extensions and before any higher-privileged extensions.
122  *
123  * 3. The first letter following the 'Z' conventionally indicates the most
124  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
125  *    If multiple 'Z' extensions are named, they must be ordered first by
126  *    category, then alphabetically within a category.
127  *
128  * 3. Standard supervisor-level extensions (starting with 'S') must be listed
129  *    after standard unprivileged extensions.  If multiple supervisor-level
130  *    extensions are listed, they must be ordered alphabetically.
131  *
132  * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
133  *    after any lower-privileged, standard extensions.  If multiple
134  *    machine-level extensions are listed, they must be ordered
135  *    alphabetically.
136  *
137  * 5. Non-standard extensions (starting with 'X') must be listed after all
138  *    standard extensions. If multiple non-standard extensions are listed, they
139  *    must be ordered alphabetically.
140  *
141  * An example string following the order is:
142  *    rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
143  *
144  * New entries to this struct should follow the ordering rules described above.
145  */
146 const struct riscv_isa_ext_data riscv_isa_ext[] = {
147 	__RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
148 	__RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
149 	__RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
150 	__RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
151 	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
152 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
153 	__RISCV_ISA_EXT_DATA(c, RISCV_ISA_EXT_c),
154 	__RISCV_ISA_EXT_DATA(b, RISCV_ISA_EXT_b),
155 	__RISCV_ISA_EXT_DATA(k, RISCV_ISA_EXT_k),
156 	__RISCV_ISA_EXT_DATA(j, RISCV_ISA_EXT_j),
157 	__RISCV_ISA_EXT_DATA(p, RISCV_ISA_EXT_p),
158 	__RISCV_ISA_EXT_DATA(v, RISCV_ISA_EXT_v),
159 	__RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
160 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
161 	__RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
162 	__RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
163 	__RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
164 	__RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
165 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
166 	__RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
167 	__RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
168 	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
169 	__RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
170 	__RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
171 	__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
172 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
173 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
174 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
175 	__RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
176 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
177 };
178 
179 const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
180 
181 static void __init riscv_parse_isa_string(unsigned long *this_hwcap, struct riscv_isainfo *isainfo,
182 					  unsigned long *isa2hwcap, const char *isa)
183 {
184 	/*
185 	 * For all possible cpus, we have already validated in
186 	 * the boot process that they at least contain "rv" and
187 	 * whichever of "32"/"64" this kernel supports, and so this
188 	 * section can be skipped.
189 	 */
190 	isa += 4;
191 
192 	while (*isa) {
193 		const char *ext = isa++;
194 		const char *ext_end = isa;
195 		bool ext_long = false, ext_err = false;
196 
197 		switch (*ext) {
198 		case 's':
199 			/*
200 			 * Workaround for invalid single-letter 's' & 'u'(QEMU).
201 			 * No need to set the bit in riscv_isa as 's' & 'u' are
202 			 * not valid ISA extensions. It works until multi-letter
203 			 * extension starting with "Su" appears.
204 			 */
205 			if (ext[-1] != '_' && ext[1] == 'u') {
206 				++isa;
207 				ext_err = true;
208 				break;
209 			}
210 			fallthrough;
211 		case 'S':
212 		case 'x':
213 		case 'X':
214 		case 'z':
215 		case 'Z':
216 			/*
217 			 * Before attempting to parse the extension itself, we find its end.
218 			 * As multi-letter extensions must be split from other multi-letter
219 			 * extensions with an "_", the end of a multi-letter extension will
220 			 * either be the null character or the "_" at the start of the next
221 			 * multi-letter extension.
222 			 *
223 			 * Next, as the extensions version is currently ignored, we
224 			 * eliminate that portion. This is done by parsing backwards from
225 			 * the end of the extension, removing any numbers. This may be a
226 			 * major or minor number however, so the process is repeated if a
227 			 * minor number was found.
228 			 *
229 			 * ext_end is intended to represent the first character *after* the
230 			 * name portion of an extension, but will be decremented to the last
231 			 * character itself while eliminating the extensions version number.
232 			 * A simple re-increment solves this problem.
233 			 */
234 			ext_long = true;
235 			for (; *isa && *isa != '_'; ++isa)
236 				if (unlikely(!isalnum(*isa)))
237 					ext_err = true;
238 
239 			ext_end = isa;
240 			if (unlikely(ext_err))
241 				break;
242 
243 			if (!isdigit(ext_end[-1]))
244 				break;
245 
246 			while (isdigit(*--ext_end))
247 				;
248 
249 			if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
250 				++ext_end;
251 				break;
252 			}
253 
254 			while (isdigit(*--ext_end))
255 				;
256 
257 			++ext_end;
258 			break;
259 		default:
260 			/*
261 			 * Things are a little easier for single-letter extensions, as they
262 			 * are parsed forwards.
263 			 *
264 			 * After checking that our starting position is valid, we need to
265 			 * ensure that, when isa was incremented at the start of the loop,
266 			 * that it arrived at the start of the next extension.
267 			 *
268 			 * If we are already on a non-digit, there is nothing to do. Either
269 			 * we have a multi-letter extension's _, or the start of an
270 			 * extension.
271 			 *
272 			 * Otherwise we have found the current extension's major version
273 			 * number. Parse past it, and a subsequent p/minor version number
274 			 * if present. The `p` extension must not appear immediately after
275 			 * a number, so there is no fear of missing it.
276 			 *
277 			 */
278 			if (unlikely(!isalpha(*ext))) {
279 				ext_err = true;
280 				break;
281 			}
282 
283 			if (!isdigit(*isa))
284 				break;
285 
286 			while (isdigit(*++isa))
287 				;
288 
289 			if (tolower(*isa) != 'p')
290 				break;
291 
292 			if (!isdigit(*++isa)) {
293 				--isa;
294 				break;
295 			}
296 
297 			while (isdigit(*++isa))
298 				;
299 
300 			break;
301 		}
302 
303 		/*
304 		 * The parser expects that at the start of an iteration isa points to the
305 		 * first character of the next extension. As we stop parsing an extension
306 		 * on meeting a non-alphanumeric character, an extra increment is needed
307 		 * where the succeeding extension is a multi-letter prefixed with an "_".
308 		 */
309 		if (*isa == '_')
310 			++isa;
311 
312 #define SET_ISA_EXT_MAP(name, bit)						\
313 		do {								\
314 			if ((ext_end - ext == strlen(name)) &&			\
315 			     !strncasecmp(ext, name, strlen(name)) &&		\
316 			     riscv_isa_extension_check(bit))			\
317 				set_bit(bit, isainfo->isa);			\
318 		} while (false)							\
319 
320 		if (unlikely(ext_err))
321 			continue;
322 		if (!ext_long) {
323 			int nr = tolower(*ext) - 'a';
324 
325 			if (riscv_isa_extension_check(nr)) {
326 				*this_hwcap |= isa2hwcap[nr];
327 				set_bit(nr, isainfo->isa);
328 			}
329 		} else {
330 			for (int i = 0; i < riscv_isa_ext_count; i++)
331 				SET_ISA_EXT_MAP(riscv_isa_ext[i].name,
332 						riscv_isa_ext[i].id);
333 		}
334 #undef SET_ISA_EXT_MAP
335 	}
336 }
337 
338 static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
339 {
340 	struct device_node *node;
341 	const char *isa;
342 	int rc;
343 	struct acpi_table_header *rhct;
344 	acpi_status status;
345 	unsigned int cpu;
346 
347 	if (!acpi_disabled) {
348 		status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
349 		if (ACPI_FAILURE(status))
350 			return;
351 	}
352 
353 	for_each_possible_cpu(cpu) {
354 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
355 		unsigned long this_hwcap = 0;
356 
357 		if (acpi_disabled) {
358 			node = of_cpu_device_node_get(cpu);
359 			if (!node) {
360 				pr_warn("Unable to find cpu node\n");
361 				continue;
362 			}
363 
364 			rc = of_property_read_string(node, "riscv,isa", &isa);
365 			of_node_put(node);
366 			if (rc) {
367 				pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
368 				continue;
369 			}
370 		} else {
371 			rc = acpi_get_riscv_isa(rhct, cpu, &isa);
372 			if (rc < 0) {
373 				pr_warn("Unable to get ISA for the hart - %d\n", cpu);
374 				continue;
375 			}
376 		}
377 
378 		riscv_parse_isa_string(&this_hwcap, isainfo, isa2hwcap, isa);
379 
380 		/*
381 		 * These ones were as they were part of the base ISA when the
382 		 * port & dt-bindings were upstreamed, and so can be set
383 		 * unconditionally where `i` is in riscv,isa on DT systems.
384 		 */
385 		if (acpi_disabled) {
386 			set_bit(RISCV_ISA_EXT_ZICSR, isainfo->isa);
387 			set_bit(RISCV_ISA_EXT_ZIFENCEI, isainfo->isa);
388 			set_bit(RISCV_ISA_EXT_ZICNTR, isainfo->isa);
389 			set_bit(RISCV_ISA_EXT_ZIHPM, isainfo->isa);
390 		}
391 
392 		/*
393 		 * All "okay" hart should have same isa. Set HWCAP based on
394 		 * common capabilities of every "okay" hart, in case they don't
395 		 * have.
396 		 */
397 		if (elf_hwcap)
398 			elf_hwcap &= this_hwcap;
399 		else
400 			elf_hwcap = this_hwcap;
401 
402 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
403 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
404 		else
405 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
406 	}
407 
408 	if (!acpi_disabled && rhct)
409 		acpi_put_table((struct acpi_table_header *)rhct);
410 }
411 
412 static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
413 {
414 	unsigned int cpu;
415 
416 	for_each_possible_cpu(cpu) {
417 		unsigned long this_hwcap = 0;
418 		struct device_node *cpu_node;
419 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
420 
421 		cpu_node = of_cpu_device_node_get(cpu);
422 		if (!cpu_node) {
423 			pr_warn("Unable to find cpu node\n");
424 			continue;
425 		}
426 
427 		if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
428 			of_node_put(cpu_node);
429 			continue;
430 		}
431 
432 		for (int i = 0; i < riscv_isa_ext_count; i++) {
433 			if (of_property_match_string(cpu_node, "riscv,isa-extensions",
434 						     riscv_isa_ext[i].property) < 0)
435 				continue;
436 
437 			if (!riscv_isa_extension_check(riscv_isa_ext[i].id))
438 				continue;
439 
440 			/* Only single letter extensions get set in hwcap */
441 			if (strnlen(riscv_isa_ext[i].name, 2) == 1)
442 				this_hwcap |= isa2hwcap[riscv_isa_ext[i].id];
443 
444 			set_bit(riscv_isa_ext[i].id, isainfo->isa);
445 		}
446 
447 		of_node_put(cpu_node);
448 
449 		/*
450 		 * All "okay" harts should have same isa. Set HWCAP based on
451 		 * common capabilities of every "okay" hart, in case they don't.
452 		 */
453 		if (elf_hwcap)
454 			elf_hwcap &= this_hwcap;
455 		else
456 			elf_hwcap = this_hwcap;
457 
458 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
459 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
460 		else
461 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
462 	}
463 
464 	if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
465 		return -ENOENT;
466 
467 	return 0;
468 }
469 
470 #ifdef CONFIG_RISCV_ISA_FALLBACK
471 bool __initdata riscv_isa_fallback = true;
472 #else
473 bool __initdata riscv_isa_fallback;
474 static int __init riscv_isa_fallback_setup(char *__unused)
475 {
476 	riscv_isa_fallback = true;
477 	return 1;
478 }
479 early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
480 #endif
481 
482 void __init riscv_fill_hwcap(void)
483 {
484 	char print_str[NUM_ALPHA_EXTS + 1];
485 	unsigned long isa2hwcap[26] = {0};
486 	int i, j;
487 
488 	isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
489 	isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
490 	isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
491 	isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
492 	isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
493 	isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
494 	isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
495 
496 	if (!acpi_disabled) {
497 		riscv_fill_hwcap_from_isa_string(isa2hwcap);
498 	} else {
499 		int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
500 
501 		if (ret && riscv_isa_fallback) {
502 			pr_info("Falling back to deprecated \"riscv,isa\"\n");
503 			riscv_fill_hwcap_from_isa_string(isa2hwcap);
504 		}
505 	}
506 
507 	/*
508 	 * We don't support systems with F but without D, so mask those out
509 	 * here.
510 	 */
511 	if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
512 		pr_info("This kernel does not support systems with F but not D\n");
513 		elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
514 	}
515 
516 	if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
517 		riscv_v_setup_vsize();
518 		/*
519 		 * ISA string in device tree might have 'v' flag, but
520 		 * CONFIG_RISCV_ISA_V is disabled in kernel.
521 		 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
522 		 */
523 		if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
524 			elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
525 	}
526 
527 	memset(print_str, 0, sizeof(print_str));
528 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
529 		if (riscv_isa[0] & BIT_MASK(i))
530 			print_str[j++] = (char)('a' + i);
531 	pr_info("riscv: base ISA extensions %s\n", print_str);
532 
533 	memset(print_str, 0, sizeof(print_str));
534 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
535 		if (elf_hwcap & BIT_MASK(i))
536 			print_str[j++] = (char)('a' + i);
537 	pr_info("riscv: ELF capabilities %s\n", print_str);
538 }
539 
540 unsigned long riscv_get_elf_hwcap(void)
541 {
542 	unsigned long hwcap;
543 
544 	hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
545 
546 	if (!riscv_v_vstate_ctrl_user_allowed())
547 		hwcap &= ~COMPAT_HWCAP_ISA_V;
548 
549 	return hwcap;
550 }
551 
552 #ifdef CONFIG_RISCV_ALTERNATIVE
553 /*
554  * Alternative patch sites consider 48 bits when determining when to patch
555  * the old instruction sequence with the new. These bits are broken into a
556  * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
557  * patch site is for an erratum, identified by the 32-bit patch ID. When
558  * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
559  * further break down patch ID into two 16-bit numbers. The lower 16 bits
560  * are the cpufeature ID and the upper 16 bits are used for a value specific
561  * to the cpufeature and patch site. If the upper 16 bits are zero, then it
562  * implies no specific value is specified. cpufeatures that want to control
563  * patching on a per-site basis will provide non-zero values and implement
564  * checks here. The checks return true when patching should be done, and
565  * false otherwise.
566  */
567 static bool riscv_cpufeature_patch_check(u16 id, u16 value)
568 {
569 	if (!value)
570 		return true;
571 
572 	switch (id) {
573 	case RISCV_ISA_EXT_ZICBOZ:
574 		/*
575 		 * Zicboz alternative applications provide the maximum
576 		 * supported block size order, or zero when it doesn't
577 		 * matter. If the current block size exceeds the maximum,
578 		 * then the alternative cannot be applied.
579 		 */
580 		return riscv_cboz_block_size <= (1U << value);
581 	}
582 
583 	return false;
584 }
585 
586 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
587 						  struct alt_entry *end,
588 						  unsigned int stage)
589 {
590 	struct alt_entry *alt;
591 	void *oldptr, *altptr;
592 	u16 id, value;
593 
594 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
595 		return;
596 
597 	for (alt = begin; alt < end; alt++) {
598 		if (alt->vendor_id != 0)
599 			continue;
600 
601 		id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
602 
603 		if (id >= RISCV_ISA_EXT_MAX) {
604 			WARN(1, "This extension id:%d is not in ISA extension list", id);
605 			continue;
606 		}
607 
608 		if (!__riscv_isa_extension_available(NULL, id))
609 			continue;
610 
611 		value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
612 		if (!riscv_cpufeature_patch_check(id, value))
613 			continue;
614 
615 		oldptr = ALT_OLD_PTR(alt);
616 		altptr = ALT_ALT_PTR(alt);
617 
618 		mutex_lock(&text_mutex);
619 		patch_text_nosync(oldptr, altptr, alt->alt_len);
620 		riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
621 		mutex_unlock(&text_mutex);
622 	}
623 }
624 #endif
625