xref: /openbmc/linux/arch/riscv/kernel/cpufeature.c (revision 8ebc80a25f9d9bf7a8e368b266d5b740c485c362)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e2c0cdfbSPalmer Dabbelt /*
3e2c0cdfbSPalmer Dabbelt  * Copied from arch/arm64/kernel/cpufeature.c
4e2c0cdfbSPalmer Dabbelt  *
5e2c0cdfbSPalmer Dabbelt  * Copyright (C) 2015 ARM Ltd.
6e2c0cdfbSPalmer Dabbelt  * Copyright (C) 2017 SiFive
7e2c0cdfbSPalmer Dabbelt  */
8e2c0cdfbSPalmer Dabbelt 
9396c0183SSunil V L #include <linux/acpi.h>
106bcff515SAnup Patel #include <linux/bitmap.h>
112a31c54bSTsukasa OI #include <linux/ctype.h>
129daaca4aSAndrew Jones #include <linux/log2.h>
139493e6f3SConor Dooley #include <linux/memory.h>
14ff689fd2SHeiko Stuebner #include <linux/module.h>
15e2c0cdfbSPalmer Dabbelt #include <linux/of.h>
16396c0183SSunil V L #include <asm/acpi.h>
17ff689fd2SHeiko Stuebner #include <asm/alternative.h>
181631ba12SHeiko Stuebner #include <asm/cacheflush.h>
19c2d3c844SConor Dooley #include <asm/cpufeature.h>
20e2c0cdfbSPalmer Dabbelt #include <asm/hwcap.h>
21584ea656SEvan Green #include <asm/hwprobe.h>
22ff689fd2SHeiko Stuebner #include <asm/patch.h>
23ff689fd2SHeiko Stuebner #include <asm/processor.h>
24274bf3ccSConor Dooley #include <asm/sbi.h>
257017858eSGreentime Hu #include <asm/vector.h>
26e2c0cdfbSPalmer Dabbelt 
27584ea656SEvan Green #include "copy-unaligned.h"
28584ea656SEvan Green 
2958004f26STsukasa OI #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
3058004f26STsukasa OI 
31584ea656SEvan Green #define MISALIGNED_ACCESS_JIFFIES_LG2 1
32584ea656SEvan Green #define MISALIGNED_BUFFER_SIZE 0x4000
33584ea656SEvan Green #define MISALIGNED_COPY_SIZE ((MISALIGNED_BUFFER_SIZE / 2) - 0x80)
34584ea656SEvan Green 
35e2c0cdfbSPalmer Dabbelt unsigned long elf_hwcap __read_mostly;
366bcff515SAnup Patel 
376bcff515SAnup Patel /* Host ISA bitmap */
386bcff515SAnup Patel static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
396bcff515SAnup Patel 
4082e9c66eSEvan Green /* Per-cpu ISA extensions. */
4182e9c66eSEvan Green struct riscv_isainfo hart_isa[NR_CPUS];
4282e9c66eSEvan Green 
4362a31d6eSEvan Green /* Performance information */
4462a31d6eSEvan Green DEFINE_PER_CPU(long, misaligned_access_speed);
4562a31d6eSEvan Green 
466bcff515SAnup Patel /**
476bcff515SAnup Patel  * riscv_isa_extension_base() - Get base extension word
486bcff515SAnup Patel  *
496bcff515SAnup Patel  * @isa_bitmap: ISA bitmap to use
506bcff515SAnup Patel  * Return: base extension word as unsigned long value
516bcff515SAnup Patel  *
526bcff515SAnup Patel  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
536bcff515SAnup Patel  */
riscv_isa_extension_base(const unsigned long * isa_bitmap)546bcff515SAnup Patel unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
556bcff515SAnup Patel {
566bcff515SAnup Patel 	if (!isa_bitmap)
576bcff515SAnup Patel 		return riscv_isa[0];
586bcff515SAnup Patel 	return isa_bitmap[0];
596bcff515SAnup Patel }
606bcff515SAnup Patel EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
616bcff515SAnup Patel 
626bcff515SAnup Patel /**
636bcff515SAnup Patel  * __riscv_isa_extension_available() - Check whether given extension
646bcff515SAnup Patel  * is available or not
656bcff515SAnup Patel  *
666bcff515SAnup Patel  * @isa_bitmap: ISA bitmap to use
676bcff515SAnup Patel  * @bit: bit position of the desired extension
686bcff515SAnup Patel  * Return: true or false
696bcff515SAnup Patel  *
706bcff515SAnup Patel  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
716bcff515SAnup Patel  */
__riscv_isa_extension_available(const unsigned long * isa_bitmap,int bit)726bcff515SAnup Patel bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit)
736bcff515SAnup Patel {
746bcff515SAnup Patel 	const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
756bcff515SAnup Patel 
766bcff515SAnup Patel 	if (bit >= RISCV_ISA_EXT_MAX)
776bcff515SAnup Patel 		return false;
786bcff515SAnup Patel 
796bcff515SAnup Patel 	return test_bit(bit, bmap) ? true : false;
806bcff515SAnup Patel }
816bcff515SAnup Patel EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
826bcff515SAnup Patel 
riscv_isa_extension_check(int id)83fb0ff0a9SAndrew Jones static bool riscv_isa_extension_check(int id)
84fb0ff0a9SAndrew Jones {
859daaca4aSAndrew Jones 	switch (id) {
869daaca4aSAndrew Jones 	case RISCV_ISA_EXT_ZICBOM:
879daaca4aSAndrew Jones 		if (!riscv_cbom_block_size) {
88c818fea8SBen Dooks 			pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
899daaca4aSAndrew Jones 			return false;
909daaca4aSAndrew Jones 		} else if (!is_power_of_2(riscv_cbom_block_size)) {
91c818fea8SBen Dooks 			pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
929daaca4aSAndrew Jones 			return false;
939daaca4aSAndrew Jones 		}
949daaca4aSAndrew Jones 		return true;
957ea5a736SAndrew Jones 	case RISCV_ISA_EXT_ZICBOZ:
967ea5a736SAndrew Jones 		if (!riscv_cboz_block_size) {
977ea5a736SAndrew Jones 			pr_err("Zicboz detected in ISA string, but no cboz-block-size found\n");
987ea5a736SAndrew Jones 			return false;
997ea5a736SAndrew Jones 		} else if (!is_power_of_2(riscv_cboz_block_size)) {
1007ea5a736SAndrew Jones 			pr_err("cboz-block-size present, but is not a power-of-2\n");
1017ea5a736SAndrew Jones 			return false;
1027ea5a736SAndrew Jones 		}
1037ea5a736SAndrew Jones 		return true;
1049daaca4aSAndrew Jones 	}
1059daaca4aSAndrew Jones 
106fb0ff0a9SAndrew Jones 	return true;
107fb0ff0a9SAndrew Jones }
108fb0ff0a9SAndrew Jones 
10937f988dcSConor Dooley #define __RISCV_ISA_EXT_DATA(_name, _id) {	\
11037f988dcSConor Dooley 	.name = #_name,				\
11190700a4fSConor Dooley 	.property = #_name,			\
11237f988dcSConor Dooley 	.id = _id,				\
1138135ade3SConor Dooley }
1148135ade3SConor Dooley 
1158135ade3SConor Dooley /*
1168135ade3SConor Dooley  * The canonical order of ISA extension names in the ISA string is defined in
1178135ade3SConor Dooley  * chapter 27 of the unprivileged specification.
1188135ade3SConor Dooley  *
1198135ade3SConor Dooley  * Ordinarily, for in-kernel data structures, this order is unimportant but
1208135ade3SConor Dooley  * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
1218135ade3SConor Dooley  *
1228135ade3SConor Dooley  * The specification uses vague wording, such as should, when it comes to
1238135ade3SConor Dooley  * ordering, so for our purposes the following rules apply:
1248135ade3SConor Dooley  *
1258135ade3SConor Dooley  * 1. All multi-letter extensions must be separated from other extensions by an
1268135ade3SConor Dooley  *    underscore.
1278135ade3SConor Dooley  *
1288135ade3SConor Dooley  * 2. Additional standard extensions (starting with 'Z') must be sorted after
1298135ade3SConor Dooley  *    single-letter extensions and before any higher-privileged extensions.
1308135ade3SConor Dooley  *
1318135ade3SConor Dooley  * 3. The first letter following the 'Z' conventionally indicates the most
1328135ade3SConor Dooley  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
1338135ade3SConor Dooley  *    If multiple 'Z' extensions are named, they must be ordered first by
1348135ade3SConor Dooley  *    category, then alphabetically within a category.
1358135ade3SConor Dooley  *
1368135ade3SConor Dooley  * 3. Standard supervisor-level extensions (starting with 'S') must be listed
1378135ade3SConor Dooley  *    after standard unprivileged extensions.  If multiple supervisor-level
1388135ade3SConor Dooley  *    extensions are listed, they must be ordered alphabetically.
1398135ade3SConor Dooley  *
1408135ade3SConor Dooley  * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
1418135ade3SConor Dooley  *    after any lower-privileged, standard extensions.  If multiple
1428135ade3SConor Dooley  *    machine-level extensions are listed, they must be ordered
1438135ade3SConor Dooley  *    alphabetically.
1448135ade3SConor Dooley  *
1458135ade3SConor Dooley  * 5. Non-standard extensions (starting with 'X') must be listed after all
1468135ade3SConor Dooley  *    standard extensions. If multiple non-standard extensions are listed, they
1478135ade3SConor Dooley  *    must be ordered alphabetically.
1488135ade3SConor Dooley  *
1498135ade3SConor Dooley  * An example string following the order is:
1508135ade3SConor Dooley  *    rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
1518135ade3SConor Dooley  *
1528135ade3SConor Dooley  * New entries to this struct should follow the ordering rules described above.
1538135ade3SConor Dooley  */
1548135ade3SConor Dooley const struct riscv_isa_ext_data riscv_isa_ext[] = {
155effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
156effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
157effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
158effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
159effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
160effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
161effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(c, RISCV_ISA_EXT_c),
162effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(b, RISCV_ISA_EXT_b),
163effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(k, RISCV_ISA_EXT_k),
164effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(j, RISCV_ISA_EXT_j),
165effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(p, RISCV_ISA_EXT_p),
166effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(v, RISCV_ISA_EXT_v),
167effc122aSConor Dooley 	__RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
1688135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
1698135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
1708135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
1718135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
1728135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
1738135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
1748135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
1758135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
1768135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
1778135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
1788135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
1798135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
1808135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
1818135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
1828135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
1838135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
1848135ade3SConor Dooley 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
1858135ade3SConor Dooley };
1868135ade3SConor Dooley 
1878135ade3SConor Dooley const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
1888135ade3SConor Dooley 
riscv_parse_isa_string(unsigned long * this_hwcap,struct riscv_isainfo * isainfo,unsigned long * isa2hwcap,const char * isa)1894265b0ecSConor Dooley static void __init riscv_parse_isa_string(unsigned long *this_hwcap, struct riscv_isainfo *isainfo,
1904265b0ecSConor Dooley 					  unsigned long *isa2hwcap, const char *isa)
191e2c0cdfbSPalmer Dabbelt {
192069b0d51SConor Dooley 	/*
193069b0d51SConor Dooley 	 * For all possible cpus, we have already validated in
194069b0d51SConor Dooley 	 * the boot process that they at least contain "rv" and
195069b0d51SConor Dooley 	 * whichever of "32"/"64" this kernel supports, and so this
196069b0d51SConor Dooley 	 * section can be skipped.
197069b0d51SConor Dooley 	 */
1982a31c54bSTsukasa OI 	isa += 4;
199fed14be4SConor Dooley 
2007816ebc1SConor Dooley 	while (*isa) {
2012a31c54bSTsukasa OI 		const char *ext = isa++;
2022a31c54bSTsukasa OI 		const char *ext_end = isa;
2032a31c54bSTsukasa OI 		bool ext_long = false, ext_err = false;
2042a31c54bSTsukasa OI 
2052a31c54bSTsukasa OI 		switch (*ext) {
2062a31c54bSTsukasa OI 		case 's':
2076b913e3dSConor Dooley 			/*
2082a31c54bSTsukasa OI 			 * Workaround for invalid single-letter 's' & 'u'(QEMU).
2092a31c54bSTsukasa OI 			 * No need to set the bit in riscv_isa as 's' & 'u' are
2102a31c54bSTsukasa OI 			 * not valid ISA extensions. It works until multi-letter
2112a31c54bSTsukasa OI 			 * extension starting with "Su" appears.
2126bcff515SAnup Patel 			 */
2132a31c54bSTsukasa OI 			if (ext[-1] != '_' && ext[1] == 'u') {
2142a31c54bSTsukasa OI 				++isa;
2152a31c54bSTsukasa OI 				ext_err = true;
2162a31c54bSTsukasa OI 				break;
2172a31c54bSTsukasa OI 			}
2182a31c54bSTsukasa OI 			fallthrough;
219255b34d7SYangyu Chen 		case 'S':
2202a31c54bSTsukasa OI 		case 'x':
221255b34d7SYangyu Chen 		case 'X':
2222a31c54bSTsukasa OI 		case 'z':
223255b34d7SYangyu Chen 		case 'Z':
2246b913e3dSConor Dooley 			/*
2256b913e3dSConor Dooley 			 * Before attempting to parse the extension itself, we find its end.
2266b913e3dSConor Dooley 			 * As multi-letter extensions must be split from other multi-letter
2276b913e3dSConor Dooley 			 * extensions with an "_", the end of a multi-letter extension will
2286b913e3dSConor Dooley 			 * either be the null character or the "_" at the start of the next
2296b913e3dSConor Dooley 			 * multi-letter extension.
2306b913e3dSConor Dooley 			 *
2316b913e3dSConor Dooley 			 * Next, as the extensions version is currently ignored, we
2326b913e3dSConor Dooley 			 * eliminate that portion. This is done by parsing backwards from
2336b913e3dSConor Dooley 			 * the end of the extension, removing any numbers. This may be a
2346b913e3dSConor Dooley 			 * major or minor number however, so the process is repeated if a
2356b913e3dSConor Dooley 			 * minor number was found.
2366b913e3dSConor Dooley 			 *
2376b913e3dSConor Dooley 			 * ext_end is intended to represent the first character *after* the
2386b913e3dSConor Dooley 			 * name portion of an extension, but will be decremented to the last
2396b913e3dSConor Dooley 			 * character itself while eliminating the extensions version number.
2406b913e3dSConor Dooley 			 * A simple re-increment solves this problem.
2416b913e3dSConor Dooley 			 */
2422a31c54bSTsukasa OI 			ext_long = true;
2432a31c54bSTsukasa OI 			for (; *isa && *isa != '_'; ++isa)
244255b34d7SYangyu Chen 				if (unlikely(!isalnum(*isa)))
2452a31c54bSTsukasa OI 					ext_err = true;
2466b913e3dSConor Dooley 
24740a4d0dfSTsukasa OI 			ext_end = isa;
24840a4d0dfSTsukasa OI 			if (unlikely(ext_err))
24940a4d0dfSTsukasa OI 				break;
2506b913e3dSConor Dooley 
25140a4d0dfSTsukasa OI 			if (!isdigit(ext_end[-1]))
25240a4d0dfSTsukasa OI 				break;
2536b913e3dSConor Dooley 
25440a4d0dfSTsukasa OI 			while (isdigit(*--ext_end))
25540a4d0dfSTsukasa OI 				;
2566b913e3dSConor Dooley 
2576b913e3dSConor Dooley 			if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
25840a4d0dfSTsukasa OI 				++ext_end;
25940a4d0dfSTsukasa OI 				break;
26040a4d0dfSTsukasa OI 			}
2616b913e3dSConor Dooley 
26240a4d0dfSTsukasa OI 			while (isdigit(*--ext_end))
26340a4d0dfSTsukasa OI 				;
2646b913e3dSConor Dooley 
26540a4d0dfSTsukasa OI 			++ext_end;
2662a31c54bSTsukasa OI 			break;
2672a31c54bSTsukasa OI 		default:
2686b913e3dSConor Dooley 			/*
2696b913e3dSConor Dooley 			 * Things are a little easier for single-letter extensions, as they
2706b913e3dSConor Dooley 			 * are parsed forwards.
2716b913e3dSConor Dooley 			 *
2726b913e3dSConor Dooley 			 * After checking that our starting position is valid, we need to
2736b913e3dSConor Dooley 			 * ensure that, when isa was incremented at the start of the loop,
2746b913e3dSConor Dooley 			 * that it arrived at the start of the next extension.
2756b913e3dSConor Dooley 			 *
2766b913e3dSConor Dooley 			 * If we are already on a non-digit, there is nothing to do. Either
2776b913e3dSConor Dooley 			 * we have a multi-letter extension's _, or the start of an
2786b913e3dSConor Dooley 			 * extension.
2796b913e3dSConor Dooley 			 *
2806b913e3dSConor Dooley 			 * Otherwise we have found the current extension's major version
2816b913e3dSConor Dooley 			 * number. Parse past it, and a subsequent p/minor version number
2826b913e3dSConor Dooley 			 * if present. The `p` extension must not appear immediately after
2836b913e3dSConor Dooley 			 * a number, so there is no fear of missing it.
2846b913e3dSConor Dooley 			 *
2856b913e3dSConor Dooley 			 */
286255b34d7SYangyu Chen 			if (unlikely(!isalpha(*ext))) {
2872a31c54bSTsukasa OI 				ext_err = true;
2882a31c54bSTsukasa OI 				break;
2892a31c54bSTsukasa OI 			}
2906b913e3dSConor Dooley 
2912a31c54bSTsukasa OI 			if (!isdigit(*isa))
2922a31c54bSTsukasa OI 				break;
2936b913e3dSConor Dooley 
2942a31c54bSTsukasa OI 			while (isdigit(*++isa))
2952a31c54bSTsukasa OI 				;
2966b913e3dSConor Dooley 
297255b34d7SYangyu Chen 			if (tolower(*isa) != 'p')
2982a31c54bSTsukasa OI 				break;
2996b913e3dSConor Dooley 
3002a31c54bSTsukasa OI 			if (!isdigit(*++isa)) {
3012a31c54bSTsukasa OI 				--isa;
3022a31c54bSTsukasa OI 				break;
3032a31c54bSTsukasa OI 			}
3046b913e3dSConor Dooley 
3052a31c54bSTsukasa OI 			while (isdigit(*++isa))
3062a31c54bSTsukasa OI 				;
3076b913e3dSConor Dooley 
3082a31c54bSTsukasa OI 			break;
3092a31c54bSTsukasa OI 		}
3106b913e3dSConor Dooley 
3116b913e3dSConor Dooley 		/*
3126b913e3dSConor Dooley 		 * The parser expects that at the start of an iteration isa points to the
3137816ebc1SConor Dooley 		 * first character of the next extension. As we stop parsing an extension
3147816ebc1SConor Dooley 		 * on meeting a non-alphanumeric character, an extra increment is needed
3157816ebc1SConor Dooley 		 * where the succeeding extension is a multi-letter prefixed with an "_".
3166b913e3dSConor Dooley 		 */
3177816ebc1SConor Dooley 		if (*isa == '_')
3187816ebc1SConor Dooley 			++isa;
31940a4d0dfSTsukasa OI 
32002d52fbdSAtish Patra #define SET_ISA_EXT_MAP(name, bit)						\
32102d52fbdSAtish Patra 		do {								\
32237f988dcSConor Dooley 			if ((ext_end - ext == strlen(name)) &&			\
32337f988dcSConor Dooley 			     !strncasecmp(ext, name, strlen(name)) &&		\
324fb0ff0a9SAndrew Jones 			     riscv_isa_extension_check(bit))			\
32582e9c66eSEvan Green 				set_bit(bit, isainfo->isa);			\
32602d52fbdSAtish Patra 		} while (false)							\
32702d52fbdSAtish Patra 
32840a4d0dfSTsukasa OI 		if (unlikely(ext_err))
3292a31c54bSTsukasa OI 			continue;
33040a4d0dfSTsukasa OI 		if (!ext_long) {
331255b34d7SYangyu Chen 			int nr = tolower(*ext) - 'a';
33272685554SAndrew Jones 
333fb0ff0a9SAndrew Jones 			if (riscv_isa_extension_check(nr)) {
3344265b0ecSConor Dooley 				*this_hwcap |= isa2hwcap[nr];
33582e9c66eSEvan Green 				set_bit(nr, isainfo->isa);
336fb0ff0a9SAndrew Jones 			}
3374905ec2fSAtish Patra 		} else {
33837f988dcSConor Dooley 			for (int i = 0; i < riscv_isa_ext_count; i++)
33937f988dcSConor Dooley 				SET_ISA_EXT_MAP(riscv_isa_ext[i].name,
34037f988dcSConor Dooley 						riscv_isa_ext[i].id);
3416bcff515SAnup Patel 		}
34202d52fbdSAtish Patra #undef SET_ISA_EXT_MAP
34340a4d0dfSTsukasa OI 	}
3444265b0ecSConor Dooley }
3454265b0ecSConor Dooley 
riscv_fill_hwcap_from_isa_string(unsigned long * isa2hwcap)3464265b0ecSConor Dooley static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
3474265b0ecSConor Dooley {
3484265b0ecSConor Dooley 	struct device_node *node;
3494265b0ecSConor Dooley 	const char *isa;
3504265b0ecSConor Dooley 	int rc;
3514265b0ecSConor Dooley 	struct acpi_table_header *rhct;
3524265b0ecSConor Dooley 	acpi_status status;
3534265b0ecSConor Dooley 	unsigned int cpu;
35418f3fdb1SCharlie Jenkins 	u64 boot_vendorid;
35518f3fdb1SCharlie Jenkins 	u64 boot_archid;
3564265b0ecSConor Dooley 
3574265b0ecSConor Dooley 	if (!acpi_disabled) {
3584265b0ecSConor Dooley 		status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
3594265b0ecSConor Dooley 		if (ACPI_FAILURE(status))
3604265b0ecSConor Dooley 			return;
3614265b0ecSConor Dooley 	}
3624265b0ecSConor Dooley 
36318f3fdb1SCharlie Jenkins 	boot_vendorid = riscv_get_mvendorid();
36418f3fdb1SCharlie Jenkins 	boot_archid = riscv_get_marchid();
36518f3fdb1SCharlie Jenkins 
3664265b0ecSConor Dooley 	for_each_possible_cpu(cpu) {
3674265b0ecSConor Dooley 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
3684265b0ecSConor Dooley 		unsigned long this_hwcap = 0;
3694265b0ecSConor Dooley 
3704265b0ecSConor Dooley 		if (acpi_disabled) {
3714265b0ecSConor Dooley 			node = of_cpu_device_node_get(cpu);
3724265b0ecSConor Dooley 			if (!node) {
3734265b0ecSConor Dooley 				pr_warn("Unable to find cpu node\n");
3744265b0ecSConor Dooley 				continue;
3754265b0ecSConor Dooley 			}
3764265b0ecSConor Dooley 
3774265b0ecSConor Dooley 			rc = of_property_read_string(node, "riscv,isa", &isa);
3784265b0ecSConor Dooley 			of_node_put(node);
3794265b0ecSConor Dooley 			if (rc) {
3804265b0ecSConor Dooley 				pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
3814265b0ecSConor Dooley 				continue;
3824265b0ecSConor Dooley 			}
3834265b0ecSConor Dooley 		} else {
3844265b0ecSConor Dooley 			rc = acpi_get_riscv_isa(rhct, cpu, &isa);
3854265b0ecSConor Dooley 			if (rc < 0) {
3864265b0ecSConor Dooley 				pr_warn("Unable to get ISA for the hart - %d\n", cpu);
3874265b0ecSConor Dooley 				continue;
3884265b0ecSConor Dooley 			}
3894265b0ecSConor Dooley 		}
3904265b0ecSConor Dooley 
3914265b0ecSConor Dooley 		riscv_parse_isa_string(&this_hwcap, isainfo, isa2hwcap, isa);
392fbdc6193SAtish Patra 
393fbdc6193SAtish Patra 		/*
39407edc327SConor Dooley 		 * These ones were as they were part of the base ISA when the
39507edc327SConor Dooley 		 * port & dt-bindings were upstreamed, and so can be set
39607edc327SConor Dooley 		 * unconditionally where `i` is in riscv,isa on DT systems.
39707edc327SConor Dooley 		 */
39807edc327SConor Dooley 		if (acpi_disabled) {
399ab2dbc7aSPalmer Dabbelt 			set_bit(RISCV_ISA_EXT_ZICSR, isainfo->isa);
400ab2dbc7aSPalmer Dabbelt 			set_bit(RISCV_ISA_EXT_ZIFENCEI, isainfo->isa);
40142b89447SPalmer Dabbelt 			set_bit(RISCV_ISA_EXT_ZICNTR, isainfo->isa);
40242b89447SPalmer Dabbelt 			set_bit(RISCV_ISA_EXT_ZIHPM, isainfo->isa);
40307edc327SConor Dooley 		}
40407edc327SConor Dooley 
40507edc327SConor Dooley 		/*
406274bf3ccSConor Dooley 		 * "V" in ISA strings is ambiguous in practice: it should mean
407274bf3ccSConor Dooley 		 * just the standard V-1.0 but vendors aren't well behaved.
408274bf3ccSConor Dooley 		 * Many vendors with T-Head CPU cores which implement the 0.7.1
409274bf3ccSConor Dooley 		 * version of the vector specification put "v" into their DTs.
410274bf3ccSConor Dooley 		 * CPU cores with the ratified spec will contain non-zero
411274bf3ccSConor Dooley 		 * marchid.
412274bf3ccSConor Dooley 		 */
41318f3fdb1SCharlie Jenkins 		if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) {
414274bf3ccSConor Dooley 			this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v];
415274bf3ccSConor Dooley 			clear_bit(RISCV_ISA_EXT_v, isainfo->isa);
416274bf3ccSConor Dooley 		}
417274bf3ccSConor Dooley 
418274bf3ccSConor Dooley 		/*
419fbdc6193SAtish Patra 		 * All "okay" hart should have same isa. Set HWCAP based on
420fbdc6193SAtish Patra 		 * common capabilities of every "okay" hart, in case they don't
421fbdc6193SAtish Patra 		 * have.
422fbdc6193SAtish Patra 		 */
423fbdc6193SAtish Patra 		if (elf_hwcap)
424fbdc6193SAtish Patra 			elf_hwcap &= this_hwcap;
425fbdc6193SAtish Patra 		else
426fbdc6193SAtish Patra 			elf_hwcap = this_hwcap;
4276bcff515SAnup Patel 
4288f51558eSYury Norov 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
42982e9c66eSEvan Green 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
4308f51558eSYury Norov 		else
43182e9c66eSEvan Green 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
432fbdc6193SAtish Patra 	}
433e2c0cdfbSPalmer Dabbelt 
434396c0183SSunil V L 	if (!acpi_disabled && rhct)
435396c0183SSunil V L 		acpi_put_table((struct acpi_table_header *)rhct);
4364265b0ecSConor Dooley }
437396c0183SSunil V L 
riscv_fill_hwcap_from_ext_list(unsigned long * isa2hwcap)43890700a4fSConor Dooley static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
43990700a4fSConor Dooley {
44090700a4fSConor Dooley 	unsigned int cpu;
44190700a4fSConor Dooley 
44290700a4fSConor Dooley 	for_each_possible_cpu(cpu) {
44390700a4fSConor Dooley 		unsigned long this_hwcap = 0;
44490700a4fSConor Dooley 		struct device_node *cpu_node;
44590700a4fSConor Dooley 		struct riscv_isainfo *isainfo = &hart_isa[cpu];
44690700a4fSConor Dooley 
44790700a4fSConor Dooley 		cpu_node = of_cpu_device_node_get(cpu);
44890700a4fSConor Dooley 		if (!cpu_node) {
44990700a4fSConor Dooley 			pr_warn("Unable to find cpu node\n");
45090700a4fSConor Dooley 			continue;
45190700a4fSConor Dooley 		}
45290700a4fSConor Dooley 
45390700a4fSConor Dooley 		if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
45490700a4fSConor Dooley 			of_node_put(cpu_node);
45590700a4fSConor Dooley 			continue;
45690700a4fSConor Dooley 		}
45790700a4fSConor Dooley 
45890700a4fSConor Dooley 		for (int i = 0; i < riscv_isa_ext_count; i++) {
45990700a4fSConor Dooley 			if (of_property_match_string(cpu_node, "riscv,isa-extensions",
46090700a4fSConor Dooley 						     riscv_isa_ext[i].property) < 0)
46190700a4fSConor Dooley 				continue;
46290700a4fSConor Dooley 
46390700a4fSConor Dooley 			if (!riscv_isa_extension_check(riscv_isa_ext[i].id))
46490700a4fSConor Dooley 				continue;
46590700a4fSConor Dooley 
46690700a4fSConor Dooley 			/* Only single letter extensions get set in hwcap */
46790700a4fSConor Dooley 			if (strnlen(riscv_isa_ext[i].name, 2) == 1)
46890700a4fSConor Dooley 				this_hwcap |= isa2hwcap[riscv_isa_ext[i].id];
46990700a4fSConor Dooley 
47090700a4fSConor Dooley 			set_bit(riscv_isa_ext[i].id, isainfo->isa);
47190700a4fSConor Dooley 		}
47290700a4fSConor Dooley 
47390700a4fSConor Dooley 		of_node_put(cpu_node);
47490700a4fSConor Dooley 
47590700a4fSConor Dooley 		/*
47690700a4fSConor Dooley 		 * All "okay" harts should have same isa. Set HWCAP based on
47790700a4fSConor Dooley 		 * common capabilities of every "okay" hart, in case they don't.
47890700a4fSConor Dooley 		 */
47990700a4fSConor Dooley 		if (elf_hwcap)
48090700a4fSConor Dooley 			elf_hwcap &= this_hwcap;
48190700a4fSConor Dooley 		else
48290700a4fSConor Dooley 			elf_hwcap = this_hwcap;
48390700a4fSConor Dooley 
48490700a4fSConor Dooley 		if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
48590700a4fSConor Dooley 			bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
48690700a4fSConor Dooley 		else
48790700a4fSConor Dooley 			bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
48890700a4fSConor Dooley 	}
48990700a4fSConor Dooley 
49090700a4fSConor Dooley 	if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
49190700a4fSConor Dooley 		return -ENOENT;
49290700a4fSConor Dooley 
49390700a4fSConor Dooley 	return 0;
49490700a4fSConor Dooley }
49590700a4fSConor Dooley 
496496ea826SConor Dooley #ifdef CONFIG_RISCV_ISA_FALLBACK
497496ea826SConor Dooley bool __initdata riscv_isa_fallback = true;
498496ea826SConor Dooley #else
499496ea826SConor Dooley bool __initdata riscv_isa_fallback;
riscv_isa_fallback_setup(char * __unused)500496ea826SConor Dooley static int __init riscv_isa_fallback_setup(char *__unused)
501496ea826SConor Dooley {
502496ea826SConor Dooley 	riscv_isa_fallback = true;
503496ea826SConor Dooley 	return 1;
504496ea826SConor Dooley }
505496ea826SConor Dooley early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
506496ea826SConor Dooley #endif
507496ea826SConor Dooley 
riscv_fill_hwcap(void)5084265b0ecSConor Dooley void __init riscv_fill_hwcap(void)
5094265b0ecSConor Dooley {
5104265b0ecSConor Dooley 	char print_str[NUM_ALPHA_EXTS + 1];
5114265b0ecSConor Dooley 	unsigned long isa2hwcap[26] = {0};
51290700a4fSConor Dooley 	int i, j;
5134265b0ecSConor Dooley 
5144265b0ecSConor Dooley 	isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
5154265b0ecSConor Dooley 	isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
5164265b0ecSConor Dooley 	isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
5174265b0ecSConor Dooley 	isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
5184265b0ecSConor Dooley 	isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
5194265b0ecSConor Dooley 	isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
5204265b0ecSConor Dooley 	isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
5214265b0ecSConor Dooley 
52290700a4fSConor Dooley 	if (!acpi_disabled) {
5234265b0ecSConor Dooley 		riscv_fill_hwcap_from_isa_string(isa2hwcap);
52490700a4fSConor Dooley 	} else {
52590700a4fSConor Dooley 		int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
526ff689fd2SHeiko Stuebner 
527496ea826SConor Dooley 		if (ret && riscv_isa_fallback) {
52890700a4fSConor Dooley 			pr_info("Falling back to deprecated \"riscv,isa\"\n");
52990700a4fSConor Dooley 			riscv_fill_hwcap_from_isa_string(isa2hwcap);
53090700a4fSConor Dooley 		}
53190700a4fSConor Dooley 	}
53290700a4fSConor Dooley 
53390700a4fSConor Dooley 	/*
53490700a4fSConor Dooley 	 * We don't support systems with F but without D, so mask those out
53590700a4fSConor Dooley 	 * here.
53690700a4fSConor Dooley 	 */
5376bcff515SAnup Patel 	if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
53858004f26STsukasa OI 		pr_info("This kernel does not support systems with F but not D\n");
5396bcff515SAnup Patel 		elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
5406bcff515SAnup Patel 	}
54102d52fbdSAtish Patra 
542dc6667a4SGuo Ren 	if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
5437017858eSGreentime Hu 		riscv_v_setup_vsize();
544dc6667a4SGuo Ren 		/*
545dc6667a4SGuo Ren 		 * ISA string in device tree might have 'v' flag, but
546dc6667a4SGuo Ren 		 * CONFIG_RISCV_ISA_V is disabled in kernel.
547dc6667a4SGuo Ren 		 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
548dc6667a4SGuo Ren 		 */
549dc6667a4SGuo Ren 		if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
550dc6667a4SGuo Ren 			elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
551dc6667a4SGuo Ren 	}
552dc6667a4SGuo Ren 
5536bcff515SAnup Patel 	memset(print_str, 0, sizeof(print_str));
5546bcff515SAnup Patel 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
55558004f26STsukasa OI 		if (riscv_isa[0] & BIT_MASK(i))
5566bcff515SAnup Patel 			print_str[j++] = (char)('a' + i);
5576bcff515SAnup Patel 	pr_info("riscv: base ISA extensions %s\n", print_str);
5586bcff515SAnup Patel 
5599411ec60SAlan Kao 	memset(print_str, 0, sizeof(print_str));
5609411ec60SAlan Kao 	for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
5619411ec60SAlan Kao 		if (elf_hwcap & BIT_MASK(i))
56237a7a2a1SJisheng Zhang 			print_str[j++] = (char)('a' + i);
5639411ec60SAlan Kao 	pr_info("riscv: ELF capabilities %s\n", print_str);
564e2c0cdfbSPalmer Dabbelt }
565ff689fd2SHeiko Stuebner 
riscv_get_elf_hwcap(void)56650724efcSAndy Chiu unsigned long riscv_get_elf_hwcap(void)
56750724efcSAndy Chiu {
5681fd96a3eSAndy Chiu 	unsigned long hwcap;
5691fd96a3eSAndy Chiu 
5701fd96a3eSAndy Chiu 	hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
5711fd96a3eSAndy Chiu 
5721fd96a3eSAndy Chiu 	if (!riscv_v_vstate_ctrl_user_allowed())
5731fd96a3eSAndy Chiu 		hwcap &= ~COMPAT_HWCAP_ISA_V;
5741fd96a3eSAndy Chiu 
5751fd96a3eSAndy Chiu 	return hwcap;
57650724efcSAndy Chiu }
57750724efcSAndy Chiu 
check_unaligned_access(int cpu)578584ea656SEvan Green void check_unaligned_access(int cpu)
579584ea656SEvan Green {
580584ea656SEvan Green 	u64 start_cycles, end_cycles;
581584ea656SEvan Green 	u64 word_cycles;
582584ea656SEvan Green 	u64 byte_cycles;
583584ea656SEvan Green 	int ratio;
584584ea656SEvan Green 	unsigned long start_jiffies, now;
585584ea656SEvan Green 	struct page *page;
586584ea656SEvan Green 	void *dst;
587584ea656SEvan Green 	void *src;
588584ea656SEvan Green 	long speed = RISCV_HWPROBE_MISALIGNED_SLOW;
589584ea656SEvan Green 
590b649a7feSJisheng Zhang 	/* We are already set since the last check */
591b649a7feSJisheng Zhang 	if (per_cpu(misaligned_access_speed, cpu) != RISCV_HWPROBE_MISALIGNED_UNKNOWN)
592b649a7feSJisheng Zhang 		return;
593b649a7feSJisheng Zhang 
594584ea656SEvan Green 	page = alloc_pages(GFP_NOWAIT, get_order(MISALIGNED_BUFFER_SIZE));
595584ea656SEvan Green 	if (!page) {
596584ea656SEvan Green 		pr_warn("Can't alloc pages to measure memcpy performance");
597584ea656SEvan Green 		return;
598584ea656SEvan Green 	}
599584ea656SEvan Green 
600584ea656SEvan Green 	/* Make an unaligned destination buffer. */
601584ea656SEvan Green 	dst = (void *)((unsigned long)page_address(page) | 0x1);
602584ea656SEvan Green 	/* Unalign src as well, but differently (off by 1 + 2 = 3). */
603584ea656SEvan Green 	src = dst + (MISALIGNED_BUFFER_SIZE / 2);
604584ea656SEvan Green 	src += 2;
605584ea656SEvan Green 	word_cycles = -1ULL;
606584ea656SEvan Green 	/* Do a warmup. */
607584ea656SEvan Green 	__riscv_copy_words_unaligned(dst, src, MISALIGNED_COPY_SIZE);
608584ea656SEvan Green 	preempt_disable();
609584ea656SEvan Green 	start_jiffies = jiffies;
610584ea656SEvan Green 	while ((now = jiffies) == start_jiffies)
611584ea656SEvan Green 		cpu_relax();
612584ea656SEvan Green 
613584ea656SEvan Green 	/*
614584ea656SEvan Green 	 * For a fixed amount of time, repeatedly try the function, and take
615584ea656SEvan Green 	 * the best time in cycles as the measurement.
616584ea656SEvan Green 	 */
617584ea656SEvan Green 	while (time_before(jiffies, now + (1 << MISALIGNED_ACCESS_JIFFIES_LG2))) {
618584ea656SEvan Green 		start_cycles = get_cycles64();
619584ea656SEvan Green 		/* Ensure the CSR read can't reorder WRT to the copy. */
620584ea656SEvan Green 		mb();
621584ea656SEvan Green 		__riscv_copy_words_unaligned(dst, src, MISALIGNED_COPY_SIZE);
622584ea656SEvan Green 		/* Ensure the copy ends before the end time is snapped. */
623584ea656SEvan Green 		mb();
624584ea656SEvan Green 		end_cycles = get_cycles64();
625584ea656SEvan Green 		if ((end_cycles - start_cycles) < word_cycles)
626584ea656SEvan Green 			word_cycles = end_cycles - start_cycles;
627584ea656SEvan Green 	}
628584ea656SEvan Green 
629584ea656SEvan Green 	byte_cycles = -1ULL;
630584ea656SEvan Green 	__riscv_copy_bytes_unaligned(dst, src, MISALIGNED_COPY_SIZE);
631584ea656SEvan Green 	start_jiffies = jiffies;
632584ea656SEvan Green 	while ((now = jiffies) == start_jiffies)
633584ea656SEvan Green 		cpu_relax();
634584ea656SEvan Green 
635584ea656SEvan Green 	while (time_before(jiffies, now + (1 << MISALIGNED_ACCESS_JIFFIES_LG2))) {
636584ea656SEvan Green 		start_cycles = get_cycles64();
637584ea656SEvan Green 		mb();
638584ea656SEvan Green 		__riscv_copy_bytes_unaligned(dst, src, MISALIGNED_COPY_SIZE);
639584ea656SEvan Green 		mb();
640584ea656SEvan Green 		end_cycles = get_cycles64();
641584ea656SEvan Green 		if ((end_cycles - start_cycles) < byte_cycles)
642584ea656SEvan Green 			byte_cycles = end_cycles - start_cycles;
643584ea656SEvan Green 	}
644584ea656SEvan Green 
645584ea656SEvan Green 	preempt_enable();
646584ea656SEvan Green 
647584ea656SEvan Green 	/* Don't divide by zero. */
648584ea656SEvan Green 	if (!word_cycles || !byte_cycles) {
649584ea656SEvan Green 		pr_warn("cpu%d: rdtime lacks granularity needed to measure unaligned access speed\n",
650584ea656SEvan Green 			cpu);
651584ea656SEvan Green 
652584ea656SEvan Green 		goto out;
653584ea656SEvan Green 	}
654584ea656SEvan Green 
655584ea656SEvan Green 	if (word_cycles < byte_cycles)
656584ea656SEvan Green 		speed = RISCV_HWPROBE_MISALIGNED_FAST;
657584ea656SEvan Green 
658584ea656SEvan Green 	ratio = div_u64((byte_cycles * 100), word_cycles);
659584ea656SEvan Green 	pr_info("cpu%d: Ratio of byte access time to unaligned word access is %d.%02d, unaligned accesses are %s\n",
660584ea656SEvan Green 		cpu,
661584ea656SEvan Green 		ratio / 100,
662584ea656SEvan Green 		ratio % 100,
663584ea656SEvan Green 		(speed == RISCV_HWPROBE_MISALIGNED_FAST) ? "fast" : "slow");
664584ea656SEvan Green 
665584ea656SEvan Green 	per_cpu(misaligned_access_speed, cpu) = speed;
666584ea656SEvan Green 
667584ea656SEvan Green out:
668584ea656SEvan Green 	__free_pages(page, get_order(MISALIGNED_BUFFER_SIZE));
669584ea656SEvan Green }
670584ea656SEvan Green 
check_unaligned_access_boot_cpu(void)671584ea656SEvan Green static int check_unaligned_access_boot_cpu(void)
672584ea656SEvan Green {
673584ea656SEvan Green 	check_unaligned_access(0);
674584ea656SEvan Green 	return 0;
675584ea656SEvan Green }
676584ea656SEvan Green 
677584ea656SEvan Green arch_initcall(check_unaligned_access_boot_cpu);
678584ea656SEvan Green 
riscv_user_isa_enable(void)6791b0a08a4SAndrew Jones void riscv_user_isa_enable(void)
6801b0a08a4SAndrew Jones {
6811b0a08a4SAndrew Jones 	if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
682*52fffb4aSSamuel Holland 		csr_set(CSR_ENVCFG, ENVCFG_CBZE);
6831b0a08a4SAndrew Jones }
6841b0a08a4SAndrew Jones 
685ff689fd2SHeiko Stuebner #ifdef CONFIG_RISCV_ALTERNATIVE
686d25f2563SAndrew Jones /*
687d25f2563SAndrew Jones  * Alternative patch sites consider 48 bits when determining when to patch
688d25f2563SAndrew Jones  * the old instruction sequence with the new. These bits are broken into a
689d25f2563SAndrew Jones  * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
690d25f2563SAndrew Jones  * patch site is for an erratum, identified by the 32-bit patch ID. When
691d25f2563SAndrew Jones  * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
692d25f2563SAndrew Jones  * further break down patch ID into two 16-bit numbers. The lower 16 bits
693d25f2563SAndrew Jones  * are the cpufeature ID and the upper 16 bits are used for a value specific
694d25f2563SAndrew Jones  * to the cpufeature and patch site. If the upper 16 bits are zero, then it
695d25f2563SAndrew Jones  * implies no specific value is specified. cpufeatures that want to control
696d25f2563SAndrew Jones  * patching on a per-site basis will provide non-zero values and implement
697d25f2563SAndrew Jones  * checks here. The checks return true when patching should be done, and
698d25f2563SAndrew Jones  * false otherwise.
699d25f2563SAndrew Jones  */
riscv_cpufeature_patch_check(u16 id,u16 value)700d25f2563SAndrew Jones static bool riscv_cpufeature_patch_check(u16 id, u16 value)
701d25f2563SAndrew Jones {
702d25f2563SAndrew Jones 	if (!value)
703d25f2563SAndrew Jones 		return true;
704d25f2563SAndrew Jones 
705ab0f7746SAndrew Jones 	switch (id) {
706ab0f7746SAndrew Jones 	case RISCV_ISA_EXT_ZICBOZ:
707ab0f7746SAndrew Jones 		/*
708ab0f7746SAndrew Jones 		 * Zicboz alternative applications provide the maximum
709ab0f7746SAndrew Jones 		 * supported block size order, or zero when it doesn't
710ab0f7746SAndrew Jones 		 * matter. If the current block size exceeds the maximum,
711ab0f7746SAndrew Jones 		 * then the alternative cannot be applied.
712ab0f7746SAndrew Jones 		 */
713ab0f7746SAndrew Jones 		return riscv_cboz_block_size <= (1U << value);
714ab0f7746SAndrew Jones 	}
715ab0f7746SAndrew Jones 
716d25f2563SAndrew Jones 	return false;
717d25f2563SAndrew Jones }
718d25f2563SAndrew Jones 
riscv_cpufeature_patch_func(struct alt_entry * begin,struct alt_entry * end,unsigned int stage)719ff689fd2SHeiko Stuebner void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
720ff689fd2SHeiko Stuebner 						  struct alt_entry *end,
721ff689fd2SHeiko Stuebner 						  unsigned int stage)
722ff689fd2SHeiko Stuebner {
723ff689fd2SHeiko Stuebner 	struct alt_entry *alt;
7248d23e94aSJisheng Zhang 	void *oldptr, *altptr;
725d25f2563SAndrew Jones 	u16 id, value;
726ff689fd2SHeiko Stuebner 
727191b27c7SJisheng Zhang 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
728191b27c7SJisheng Zhang 		return;
729ff689fd2SHeiko Stuebner 
730ff689fd2SHeiko Stuebner 	for (alt = begin; alt < end; alt++) {
731ff689fd2SHeiko Stuebner 		if (alt->vendor_id != 0)
732ff689fd2SHeiko Stuebner 			continue;
733d25f2563SAndrew Jones 
734d25f2563SAndrew Jones 		id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
735d25f2563SAndrew Jones 
736d25f2563SAndrew Jones 		if (id >= RISCV_ISA_EXT_MAX) {
737d25f2563SAndrew Jones 			WARN(1, "This extension id:%d is not in ISA extension list", id);
738ff689fd2SHeiko Stuebner 			continue;
739ff689fd2SHeiko Stuebner 		}
740ff689fd2SHeiko Stuebner 
741d25f2563SAndrew Jones 		if (!__riscv_isa_extension_available(NULL, id))
742d25f2563SAndrew Jones 			continue;
743d25f2563SAndrew Jones 
744d25f2563SAndrew Jones 		value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
745d25f2563SAndrew Jones 		if (!riscv_cpufeature_patch_check(id, value))
7464bf88607SJisheng Zhang 			continue;
7474bf88607SJisheng Zhang 
7488d23e94aSJisheng Zhang 		oldptr = ALT_OLD_PTR(alt);
7498d23e94aSJisheng Zhang 		altptr = ALT_ALT_PTR(alt);
7509493e6f3SConor Dooley 
7519493e6f3SConor Dooley 		mutex_lock(&text_mutex);
7528d23e94aSJisheng Zhang 		patch_text_nosync(oldptr, altptr, alt->alt_len);
7538d23e94aSJisheng Zhang 		riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
7549493e6f3SConor Dooley 		mutex_unlock(&text_mutex);
755ff689fd2SHeiko Stuebner 	}
756ff689fd2SHeiko Stuebner }
757ff689fd2SHeiko Stuebner #endif
758