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