xref: /linux/arch/arm64/include/asm/cpufeature.h (revision 303bff3637d13f0ecbb280c7cb403470c440a2a6)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
4  */
5 
6 #ifndef __ASM_CPUFEATURE_H
7 #define __ASM_CPUFEATURE_H
8 
9 #include <asm/alternative-macros.h>
10 #include <asm/cpucaps.h>
11 #include <asm/cputype.h>
12 #include <asm/hwcap.h>
13 #include <asm/sysreg.h>
14 
15 #define MAX_CPU_FEATURES	192
16 #define cpu_feature(x)		KERNEL_HWCAP_ ## x
17 
18 #define ARM64_SW_FEATURE_OVERRIDE_NOKASLR	0
19 #define ARM64_SW_FEATURE_OVERRIDE_HVHE		4
20 #define ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF	8
21 
22 #ifndef __ASSEMBLER__
23 
24 #include <linux/bug.h>
25 #include <linux/jump_label.h>
26 #include <linux/kernel.h>
27 #include <linux/cpumask.h>
28 
29 /*
30  * CPU feature register tracking
31  *
32  * The safe value of a CPUID feature field is dependent on the implications
33  * of the values assigned to it by the architecture. Based on the relationship
34  * between the values, the features are classified into 3 types - LOWER_SAFE,
35  * HIGHER_SAFE and EXACT.
36  *
37  * The lowest value of all the CPUs is chosen for LOWER_SAFE and highest
38  * for HIGHER_SAFE. It is expected that all CPUs have the same value for
39  * a field when EXACT is specified, failing which, the safe value specified
40  * in the table is chosen.
41  */
42 
43 enum ftr_type {
44 	FTR_EXACT,			/* Use a predefined safe value */
45 	FTR_LOWER_SAFE,			/* Smaller value is safe */
46 	FTR_HIGHER_SAFE,		/* Bigger value is safe */
47 	FTR_HIGHER_OR_ZERO_SAFE,	/* Bigger value is safe, but 0 is biggest */
48 };
49 
50 #define FTR_STRICT	true	/* SANITY check strict matching required */
51 #define FTR_NONSTRICT	false	/* SANITY check ignored */
52 
53 #define FTR_SIGNED	true	/* Value should be treated as signed */
54 #define FTR_UNSIGNED	false	/* Value should be treated as unsigned */
55 
56 #define FTR_VISIBLE	true	/* Feature visible to the user space */
57 #define FTR_HIDDEN	false	/* Feature is hidden from the user */
58 
59 #define FTR_VISIBLE_IF_IS_ENABLED(config)		\
60 	(IS_ENABLED(config) ? FTR_VISIBLE : FTR_HIDDEN)
61 
62 struct arm64_ftr_bits {
63 	bool		sign;	/* Value is signed ? */
64 	bool		visible;
65 	bool		strict;	/* CPU Sanity check: strict matching required ? */
66 	enum ftr_type	type;
67 	u8		shift;
68 	u8		width;
69 	s64		safe_val; /* safe value for FTR_EXACT features */
70 };
71 
72 /*
73  * Describe the early feature override to the core override code:
74  *
75  * @val			Values that are to be merged into the final
76  *			sanitised value of the register. Only the bitfields
77  *			set to 1 in @mask are valid
78  * @mask		Mask of the features that are overridden by @val
79  *
80  * A @mask field set to full-1 indicates that the corresponding field
81  * in @val is a valid override.
82  *
83  * A @mask field set to full-0 with the corresponding @val field set
84  * to full-0 denotes that this field has no override
85  *
86  * A @mask field set to full-0 with the corresponding @val field set
87  * to full-1 denotes that this field has an invalid override.
88  */
89 struct arm64_ftr_override {
90 	u64		val;
91 	u64		mask;
92 };
93 
94 /*
95  * @arm64_ftr_reg - Feature register
96  * @strict_mask		Bits which should match across all CPUs for sanity.
97  * @sys_val		Safe value across the CPUs (system view)
98  */
99 struct arm64_ftr_reg {
100 	const char			*name;
101 	u64				strict_mask;
102 	u64				user_mask;
103 	u64				sys_val;
104 	u64				user_val;
105 	struct arm64_ftr_override	*override;
106 	const struct arm64_ftr_bits	*ftr_bits;
107 };
108 
109 extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
110 
111 /*
112  * CPU capabilities:
113  *
114  * We use arm64_cpu_capabilities to represent system features, errata work
115  * arounds (both used internally by kernel and tracked in system_cpucaps) and
116  * ELF HWCAPs (which are exposed to user).
117  *
118  * To support systems with heterogeneous CPUs, we need to make sure that we
119  * detect the capabilities correctly on the system and take appropriate
120  * measures to ensure there are no incompatibilities.
121  *
122  * This comment tries to explain how we treat the capabilities.
123  * Each capability has the following list of attributes :
124  *
125  * 1) Scope of Detection : The system detects a given capability by
126  *    performing some checks at runtime. This could be, e.g, checking the
127  *    value of a field in CPU ID feature register or checking the cpu
128  *    model. The capability provides a call back ( @matches() ) to
129  *    perform the check. Scope defines how the checks should be performed.
130  *    There are three cases:
131  *
132  *     a) SCOPE_LOCAL_CPU: check all the CPUs and "detect" if at least one
133  *        matches. This implies, we have to run the check on all the
134  *        booting CPUs, until the system decides that state of the
135  *        capability is finalised. (See section 2 below)
136  *		Or
137  *     b) SCOPE_SYSTEM: check all the CPUs and "detect" if all the CPUs
138  *        matches. This implies, we run the check only once, when the
139  *        system decides to finalise the state of the capability. If the
140  *        capability relies on a field in one of the CPU ID feature
141  *        registers, we use the sanitised value of the register from the
142  *        CPU feature infrastructure to make the decision.
143  *		Or
144  *     c) SCOPE_BOOT_CPU: Check only on the primary boot CPU to detect the
145  *        feature. This category is for features that are "finalised"
146  *        (or used) by the kernel very early even before the SMP cpus
147  *        are brought up.
148  *
149  *    The process of detection is usually denoted by "update" capability
150  *    state in the code.
151  *
152  * 2) Finalise the state : The kernel should finalise the state of a
153  *    capability at some point during its execution and take necessary
154  *    actions if any. Usually, this is done, after all the boot-time
155  *    enabled CPUs are brought up by the kernel, so that it can make
156  *    better decision based on the available set of CPUs. However, there
157  *    are some special cases, where the action is taken during the early
158  *    boot by the primary boot CPU. (e.g, running the kernel at EL2 with
159  *    Virtualisation Host Extensions). The kernel usually disallows any
160  *    changes to the state of a capability once it finalises the capability
161  *    and takes any action, as it may be impossible to execute the actions
162  *    safely. A CPU brought up after a capability is "finalised" is
163  *    referred to as "Late CPU" w.r.t the capability. e.g, all secondary
164  *    CPUs are treated "late CPUs" for capabilities determined by the boot
165  *    CPU.
166  *
167  *    At the moment there are two passes of finalising the capabilities.
168  *      a) Boot CPU scope capabilities - Finalised by primary boot CPU via
169  *         setup_boot_cpu_capabilities().
170  *      b) Everything except (a) - Run via setup_system_capabilities().
171  *
172  * 3) Verification: When a CPU is brought online (e.g, by user or by the
173  *    kernel), the kernel should make sure that it is safe to use the CPU,
174  *    by verifying that the CPU is compliant with the state of the
175  *    capabilities finalised already. This happens via :
176  *
177  *	secondary_start_kernel()-> check_local_cpu_capabilities()
178  *
179  *    As explained in (2) above, capabilities could be finalised at
180  *    different points in the execution. Each newly booted CPU is verified
181  *    against the capabilities that have been finalised by the time it
182  *    boots.
183  *
184  *	a) SCOPE_BOOT_CPU : All CPUs are verified against the capability
185  *	except for the primary boot CPU.
186  *
187  *	b) SCOPE_LOCAL_CPU, SCOPE_SYSTEM: All CPUs hotplugged on by the
188  *	user after the kernel boot are verified against the capability.
189  *
190  *    SCOPE_BOOT_CPU and SCOPE_SYSTEM capabilities with
191  *    ARM64_CPUCAP_PROBE_ONCE are not matched again on these CPUs. They
192  *    take the state already detected, so there cannot be a conflict.
193  *
194  *    If there is a conflict, the kernel takes an action, based on the
195  *    severity (e.g, a CPU could be prevented from booting or cause a
196  *    kernel panic). The CPU is allowed to "affect" the state of the
197  *    capability, if it has not been finalised already. See section 5
198  *    for more details on conflicts.
199  *
200  * 4) Action: As mentioned in (2), the kernel can take an action for each
201  *    detected capability, on all CPUs on the system. Appropriate actions
202  *    include, turning on an architectural feature, modifying the control
203  *    registers (e.g, SCTLR, TCR etc.) or patching the kernel via
204  *    alternatives. The kernel patching is batched and performed at later
205  *    point. The actions are always initiated only after the capability
206  *    is finalised. This is usually denoted by "enabling" the capability.
207  *    The actions are initiated as follows :
208  *	a) Action is triggered on all online CPUs, after the capability is
209  *	finalised, invoked within the stop_machine() context from
210  *	enable_cpu_capabilitie().
211  *
212  *	b) Any late CPU, brought up after (1), the action is triggered via:
213  *
214  *	  check_local_cpu_capabilities() -> verify_local_cpu_capabilities()
215  *
216  * 5) Conflicts: Based on the state of the capability on a late CPU vs.
217  *    the system state, we could have the following combinations :
218  *
219  *		x-----------------------------x
220  *		| Type  | System   | Late CPU |
221  *		|-----------------------------|
222  *		|  a    |   y      |    n     |
223  *		|-----------------------------|
224  *		|  b    |   n      |    y     |
225  *		x-----------------------------x
226  *
227  *     Two separate flag bits are defined to indicate whether each kind of
228  *     conflict can be allowed:
229  *		ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU - Case(a) is allowed
230  *		ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU - Case(b) is allowed
231  *
232  *     Case (a) is not permitted for a capability that the system requires
233  *     all CPUs to have in order for the capability to be enabled. This is
234  *     typical for capabilities that represent enhanced functionality.
235  *
236  *     Case (b) is not permitted for a capability that must be enabled
237  *     during boot if any CPU in the system requires it in order to run
238  *     safely. This is typical for erratum work arounds that cannot be
239  *     enabled after the corresponding capability is finalised.
240  *
241  *     In some non-typical cases either both (a) and (b), or neither,
242  *     should be permitted. This can be described by including neither
243  *     or both flags in the capability's type field.
244  *
245  *     In case of a conflict, the CPU is prevented from booting. If the
246  *     ARM64_CPUCAP_PANIC_ON_CONFLICT flag is specified for the capability,
247  *     then a kernel panic is triggered.
248  */
249 
250 
251 /*
252  * Decide how the capability is detected.
253  * On any local CPU vs System wide vs the primary boot CPU
254  */
255 #define ARM64_CPUCAP_SCOPE_LOCAL_CPU		((u16)BIT(0))
256 #define ARM64_CPUCAP_SCOPE_SYSTEM		((u16)BIT(1))
257 /*
258  * The capability is detected on the Boot CPU and is used by kernel
259  * during early boot. i.e, the capability should be "detected" and
260  * "enabled" as early as possibly on all booting CPUs.
261  */
262 #define ARM64_CPUCAP_SCOPE_BOOT_CPU		((u16)BIT(2))
263 #define ARM64_CPUCAP_SCOPE_MASK			\
264 	(ARM64_CPUCAP_SCOPE_SYSTEM	|	\
265 	 ARM64_CPUCAP_SCOPE_LOCAL_CPU	|	\
266 	 ARM64_CPUCAP_SCOPE_BOOT_CPU)
267 
268 #define SCOPE_SYSTEM				ARM64_CPUCAP_SCOPE_SYSTEM
269 #define SCOPE_LOCAL_CPU				ARM64_CPUCAP_SCOPE_LOCAL_CPU
270 #define SCOPE_BOOT_CPU				ARM64_CPUCAP_SCOPE_BOOT_CPU
271 #define SCOPE_ALL				ARM64_CPUCAP_SCOPE_MASK
272 
273 /*
274  * matches() is called only once, when the capability is detected, and the
275  * result applies to all CPUs. Secondary and late CPUs are not checked for
276  * conflicts but cpu_enable() is still called on each of them. It has no
277  * effect on SCOPE_LOCAL_CPU capabilities.
278  */
279 #define ARM64_CPUCAP_PROBE_ONCE			((u16)BIT(3))
280 /*
281  * Is it permitted for a late CPU to have this capability when system
282  * hasn't already enabled it ?
283  */
284 #define ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU	((u16)BIT(4))
285 /* Is it safe for a late CPU to miss this capability when system has it */
286 #define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU	((u16)BIT(5))
287 /* Panic when a conflict is detected */
288 #define ARM64_CPUCAP_PANIC_ON_CONFLICT		((u16)BIT(6))
289 /*
290  * When paired with SCOPE_LOCAL_CPU, all early CPUs must satisfy the
291  * condition. This is different from SCOPE_SYSTEM where the check is performed
292  * only once at the end of the SMP boot on the sanitised ID registers.
293  * SCOPE_SYSTEM is not suitable for cases where the capability depends on
294  * properties local to a CPU like MIDR_EL1.
295  */
296 #define ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS	((u16)BIT(7))
297 
298 /*
299  * CPU errata workarounds that need to be enabled at boot time if one or
300  * more CPUs in the system requires it. When one of these capabilities
301  * has been enabled, it is safe to allow any CPU to boot that doesn't
302  * require the workaround. However, it is not safe if a "late" CPU
303  * requires a workaround and the system hasn't enabled it already.
304  */
305 #define ARM64_CPUCAP_LOCAL_CPU_ERRATUM		\
306 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU | ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
307 /*
308  * SoC (not CPU) errata workarounds. The erratum is probed once on the boot
309  * CPU, before the secondary CPUs are brought up, and no secondary or late CPU
310  * can conflict with it.
311  */
312 #define ARM64_CPUCAP_SOC_ERRATUM			\
313 	(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PROBE_ONCE)
314 /*
315  * CPU feature detected at boot time based on system-wide value of a
316  * feature. It is safe for a late CPU to have this feature even though
317  * the system hasn't enabled it, although the feature will not be used
318  * by Linux in this case. If the system has enabled this feature already,
319  * then every late CPU must have it.
320  */
321 #define ARM64_CPUCAP_SYSTEM_FEATURE	\
322 	(ARM64_CPUCAP_SCOPE_SYSTEM | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
323 /*
324  * CPU feature detected at boot time based on feature of one or more CPUs.
325  * All possible conflicts for a late CPU are ignored.
326  * NOTE: this means that a late CPU with the feature will *not* cause the
327  * capability to be advertised by cpus_have_*cap()!
328  */
329 #define ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE		\
330 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU		|	\
331 	 ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU	|	\
332 	 ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
333 /*
334  * CPU feature detected at boot time and present on all early CPUs. Late CPUs
335  * are permitted to have the feature even if it hasn't been enabled, although
336  * the feature will not be used by Linux in this case. If all early CPUs have
337  * the feature, then every late CPU must have it.
338  */
339 #define ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE		\
340 	 (ARM64_CPUCAP_SCOPE_LOCAL_CPU		|	\
341 	  ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU	|	\
342 	  ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS)
343 
344 /*
345  * CPU feature detected at boot time, on one or more CPUs. A late CPU
346  * is not allowed to have the capability when the system doesn't have it.
347  * It is Ok for a late CPU to miss the feature.
348  */
349 #define ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE	\
350 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU		|	\
351 	 ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
352 
353 /*
354  * CPU feature used early in the boot based on the boot CPU. All secondary
355  * CPUs must match the state of the capability as detected by the boot CPU. In
356  * case of a conflict, a kernel panic is triggered.
357  */
358 #define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE		\
359 	(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PANIC_ON_CONFLICT)
360 
361 /*
362  * CPU feature used early in the boot based on the boot CPU. It is safe for a
363  * late CPU to have this feature even though the boot CPU hasn't enabled it,
364  * although the feature will not be used by Linux in this case. If the boot CPU
365  * has enabled this feature already, then every late CPU must have it.
366  */
367 #define ARM64_CPUCAP_BOOT_CPU_FEATURE                  \
368 	(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
369 
370 struct arm64_cpu_capabilities {
371 	const char *desc;
372 	u16 capability;
373 	u16 type;
374 	bool (*matches)(const struct arm64_cpu_capabilities *caps, int scope);
375 	/*
376 	 * Take the appropriate actions to configure this capability
377 	 * for this CPU. If the capability is detected by the kernel
378 	 * this will be called on all the CPUs in the system,
379 	 * including the hotplugged CPUs, regardless of whether the
380 	 * capability is available on that specific CPU. This is
381 	 * useful for some capabilities (e.g, working around CPU
382 	 * errata), where all the CPUs must take some action (e.g,
383 	 * changing system control/configuration). Thus, if an action
384 	 * is required only if the CPU has the capability, then the
385 	 * routine must check it before taking any action.
386 	 */
387 	void (*cpu_enable)(const struct arm64_cpu_capabilities *cap);
388 	union {
389 		struct {	/* To be used for erratum handling only */
390 			struct midr_range midr_range;
391 			const struct arm64_midr_revidr {
392 				u32 midr_rv;		/* revision/variant */
393 				u32 revidr_mask;
394 			} * const fixed_revs;
395 		};
396 
397 		const struct midr_range *midr_range_list;
398 		struct {	/* Feature register checking */
399 			u32 sys_reg;
400 			u8 field_pos;
401 			u8 field_width;
402 			u8 min_field_value;
403 			u8 max_field_value;
404 			u8 hwcap_type;
405 			bool sign;
406 			unsigned long hwcap;
407 		};
408 	};
409 
410 	/*
411 	 * An optional list of "matches/cpu_enable" pair for the same
412 	 * "capability" of the same "type" as described by the parent.
413 	 * Only matches(), cpu_enable() and fields relevant to these
414 	 * methods are significant in the list. The cpu_enable is
415 	 * invoked only if the corresponding entry "matches()".
416 	 * However, if a cpu_enable() method is associated
417 	 * with multiple matches(), care should be taken that either
418 	 * the match criteria are mutually exclusive, or that the
419 	 * method is robust against being called multiple times.
420 	 */
421 	const struct arm64_cpu_capabilities *match_list;
422 	const struct cpumask *cpus;
423 };
424 
cpucap_default_scope(const struct arm64_cpu_capabilities * cap)425 static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)
426 {
427 	return cap->type & ARM64_CPUCAP_SCOPE_MASK;
428 }
429 
cpucap_match_all_early_cpus(const struct arm64_cpu_capabilities * cap)430 static inline bool cpucap_match_all_early_cpus(const struct arm64_cpu_capabilities *cap)
431 {
432 	return cap->type & ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS;
433 }
434 
cpucap_probe_once(const struct arm64_cpu_capabilities * cap)435 static inline bool cpucap_probe_once(const struct arm64_cpu_capabilities *cap)
436 {
437 	return (cap->type & ARM64_CPUCAP_PROBE_ONCE) &&
438 	       !(cap->type & ARM64_CPUCAP_SCOPE_LOCAL_CPU);
439 }
440 
441 /*
442  * Generic helper for handling capabilities with multiple (match,enable) pairs
443  * of call backs, sharing the same capability bit.
444  * Iterate over each entry to see if at least one matches.
445  */
446 static inline bool
cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities * entry,int scope)447 cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry,
448 			       int scope)
449 {
450 	const struct arm64_cpu_capabilities *caps;
451 
452 	for (caps = entry->match_list; caps->matches; caps++)
453 		if (caps->matches(caps, scope))
454 			return true;
455 
456 	return false;
457 }
458 
is_vhe_hyp_code(void)459 static __always_inline bool is_vhe_hyp_code(void)
460 {
461 	/* Only defined for code run in VHE hyp context */
462 	return __is_defined(__KVM_VHE_HYPERVISOR__);
463 }
464 
is_nvhe_hyp_code(void)465 static __always_inline bool is_nvhe_hyp_code(void)
466 {
467 	/* Only defined for code run in NVHE hyp context */
468 	return __is_defined(__KVM_NVHE_HYPERVISOR__);
469 }
470 
is_hyp_code(void)471 static __always_inline bool is_hyp_code(void)
472 {
473 	return is_vhe_hyp_code() || is_nvhe_hyp_code();
474 }
475 
476 extern DECLARE_BITMAP(system_cpucaps, ARM64_NCAPS);
477 
478 extern DECLARE_BITMAP(boot_cpucaps, ARM64_NCAPS);
479 
480 #define for_each_available_cap(cap)		\
481 	for_each_set_bit(cap, system_cpucaps, ARM64_NCAPS)
482 
483 bool this_cpu_has_cap(unsigned int cap);
484 void cpu_set_feature(unsigned int num);
485 bool cpu_have_feature(unsigned int num);
486 unsigned long cpu_get_elf_hwcap(void);
487 unsigned long cpu_get_elf_hwcap2(void);
488 unsigned long cpu_get_elf_hwcap3(void);
489 
490 #define cpu_set_named_feature(name) cpu_set_feature(cpu_feature(name))
491 #define cpu_have_named_feature(name) cpu_have_feature(cpu_feature(name))
492 
boot_capabilities_finalized(void)493 static __always_inline bool boot_capabilities_finalized(void)
494 {
495 	return alternative_has_cap_likely(ARM64_ALWAYS_BOOT);
496 }
497 
system_capabilities_finalized(void)498 static __always_inline bool system_capabilities_finalized(void)
499 {
500 	return alternative_has_cap_likely(ARM64_ALWAYS_SYSTEM);
501 }
502 
503 /*
504  * Test for a capability with a runtime check.
505  *
506  * Before the capability is detected, this returns false.
507  */
cpus_have_cap(unsigned int num)508 static __always_inline bool cpus_have_cap(unsigned int num)
509 {
510 	if (__builtin_constant_p(num) && !cpucap_is_possible(num))
511 		return false;
512 	if (num >= ARM64_NCAPS)
513 		return false;
514 	return arch_test_bit(num, system_cpucaps);
515 }
516 
517 /*
518  * Test for a capability without a runtime check.
519  *
520  * Before boot capabilities are finalized, this will BUG().
521  * After boot capabilities are finalized, this is patched to avoid a runtime
522  * check.
523  *
524  * @num must be a compile-time constant.
525  */
cpus_have_final_boot_cap(int num)526 static __always_inline bool cpus_have_final_boot_cap(int num)
527 {
528 	if (boot_capabilities_finalized())
529 		return alternative_has_cap_unlikely(num);
530 	else
531 		BUG();
532 }
533 
534 /*
535  * Test for a capability without a runtime check.
536  *
537  * Before system capabilities are finalized, this will BUG().
538  * After system capabilities are finalized, this is patched to avoid a runtime
539  * check.
540  *
541  * @num must be a compile-time constant.
542  */
cpus_have_final_cap(int num)543 static __always_inline bool cpus_have_final_cap(int num)
544 {
545 	if (system_capabilities_finalized())
546 		return alternative_has_cap_unlikely(num);
547 	else
548 		BUG();
549 }
550 
551 static inline int __attribute_const__
cpuid_feature_extract_signed_field_width(u64 features,int field,int width)552 cpuid_feature_extract_signed_field_width(u64 features, int field, int width)
553 {
554 	return (s64)(features << (64 - width - field)) >> (64 - width);
555 }
556 
557 static inline int __attribute_const__
cpuid_feature_extract_signed_field(u64 features,int field)558 cpuid_feature_extract_signed_field(u64 features, int field)
559 {
560 	return cpuid_feature_extract_signed_field_width(features, field, 4);
561 }
562 
563 static __always_inline unsigned int __attribute_const__
cpuid_feature_extract_unsigned_field_width(u64 features,int field,int width)564 cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
565 {
566 	return (u64)(features << (64 - width - field)) >> (64 - width);
567 }
568 
569 static __always_inline unsigned int __attribute_const__
cpuid_feature_extract_unsigned_field(u64 features,int field)570 cpuid_feature_extract_unsigned_field(u64 features, int field)
571 {
572 	return cpuid_feature_extract_unsigned_field_width(features, field, 4);
573 }
574 
arm64_ftr_mask(const struct arm64_ftr_bits * ftrp)575 static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
576 {
577 	return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
578 }
579 
arm64_ftr_reg_user_value(const struct arm64_ftr_reg * reg)580 static inline u64 arm64_ftr_reg_user_value(const struct arm64_ftr_reg *reg)
581 {
582 	return (reg->user_val | (reg->sys_val & reg->user_mask));
583 }
584 
585 static inline int __attribute_const__
cpuid_feature_extract_field_width(u64 features,int field,int width,bool sign)586 cpuid_feature_extract_field_width(u64 features, int field, int width, bool sign)
587 {
588 	if (WARN_ON_ONCE(!width))
589 		width = 4;
590 	return (sign) ?
591 		cpuid_feature_extract_signed_field_width(features, field, width) :
592 		cpuid_feature_extract_unsigned_field_width(features, field, width);
593 }
594 
595 static inline int __attribute_const__
cpuid_feature_extract_field(u64 features,int field,bool sign)596 cpuid_feature_extract_field(u64 features, int field, bool sign)
597 {
598 	return cpuid_feature_extract_field_width(features, field, 4, sign);
599 }
600 
arm64_ftr_value(const struct arm64_ftr_bits * ftrp,u64 val)601 static inline s64 arm64_ftr_value(const struct arm64_ftr_bits *ftrp, u64 val)
602 {
603 	return (s64)cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width, ftrp->sign);
604 }
605 
id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)606 static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
607 {
608 	return cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_BIGEND_SHIFT) == 0x1 ||
609 		cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_BIGENDEL0_SHIFT) == 0x1;
610 }
611 
id_aa64pfr0_32bit_el1(u64 pfr0)612 static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)
613 {
614 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_EL1_SHIFT);
615 
616 	return val == ID_AA64PFR0_EL1_EL1_AARCH32;
617 }
618 
id_aa64pfr0_32bit_el0(u64 pfr0)619 static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
620 {
621 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_EL0_SHIFT);
622 
623 	return val == ID_AA64PFR0_EL1_EL0_AARCH32;
624 }
625 
id_aa64pfr0_sve(u64 pfr0)626 static inline bool id_aa64pfr0_sve(u64 pfr0)
627 {
628 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SVE_SHIFT);
629 
630 	return val > 0;
631 }
632 
id_aa64pfr1_sme(u64 pfr1)633 static inline bool id_aa64pfr1_sme(u64 pfr1)
634 {
635 	u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_EL1_SME_SHIFT);
636 
637 	return val > 0;
638 }
639 
id_aa64pfr0_mpam(u64 pfr0)640 static inline bool id_aa64pfr0_mpam(u64 pfr0)
641 {
642 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_MPAM_SHIFT);
643 
644 	return val > 0;
645 }
646 
id_aa64pfr1_mpamfrac(u64 pfr1)647 static inline bool id_aa64pfr1_mpamfrac(u64 pfr1)
648 {
649 	u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_EL1_MPAM_frac_SHIFT);
650 
651 	return val > 0;
652 }
653 
id_aa64pfr1_mte(u64 pfr1)654 static inline bool id_aa64pfr1_mte(u64 pfr1)
655 {
656 	u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_EL1_MTE_SHIFT);
657 
658 	return val >= ID_AA64PFR1_EL1_MTE_MTE2;
659 }
660 
661 void __init setup_boot_cpu_features(void);
662 void __init setup_system_features(void);
663 void __init setup_user_features(void);
664 
665 void check_local_cpu_capabilities(void);
666 
667 u64 read_sanitised_ftr_reg(u32 id);
668 u64 __read_sysreg_by_encoding(u32 sys_id);
669 
cpu_supports_mixed_endian_el0(void)670 static inline bool cpu_supports_mixed_endian_el0(void)
671 {
672 	return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));
673 }
674 
675 
supports_csv2p3(int scope)676 static inline bool supports_csv2p3(int scope)
677 {
678 	u64 pfr0;
679 	u8 csv2_val;
680 
681 	if (scope == SCOPE_LOCAL_CPU)
682 		pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
683 	else
684 		pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
685 
686 	csv2_val = cpuid_feature_extract_unsigned_field(pfr0,
687 							ID_AA64PFR0_EL1_CSV2_SHIFT);
688 	return csv2_val == 3;
689 }
690 
supports_clearbhb(int scope)691 static inline bool supports_clearbhb(int scope)
692 {
693 	u64 isar2;
694 
695 	if (scope == SCOPE_LOCAL_CPU)
696 		isar2 = read_sysreg_s(SYS_ID_AA64ISAR2_EL1);
697 	else
698 		isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
699 
700 	return cpuid_feature_extract_unsigned_field(isar2,
701 						    ID_AA64ISAR2_EL1_CLRBHB_SHIFT);
702 }
703 
704 const struct cpumask *system_32bit_el0_cpumask(void);
705 const struct cpumask *fallback_32bit_el0_cpumask(void);
706 DECLARE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
707 
system_supports_32bit_el0(void)708 static inline bool system_supports_32bit_el0(void)
709 {
710 	u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
711 
712 	return static_branch_unlikely(&arm64_mismatched_32bit_el0) ||
713 	       id_aa64pfr0_32bit_el0(pfr0);
714 }
715 
system_supports_4kb_granule(void)716 static inline bool system_supports_4kb_granule(void)
717 {
718 	u64 mmfr0;
719 	u32 val;
720 
721 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
722 	val = cpuid_feature_extract_unsigned_field(mmfr0,
723 						ID_AA64MMFR0_EL1_TGRAN4_SHIFT);
724 
725 	return (val >= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN) &&
726 	       (val <= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX);
727 }
728 
system_supports_64kb_granule(void)729 static inline bool system_supports_64kb_granule(void)
730 {
731 	u64 mmfr0;
732 	u32 val;
733 
734 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
735 	val = cpuid_feature_extract_unsigned_field(mmfr0,
736 						ID_AA64MMFR0_EL1_TGRAN64_SHIFT);
737 
738 	return (val >= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN) &&
739 	       (val <= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX);
740 }
741 
system_supports_16kb_granule(void)742 static inline bool system_supports_16kb_granule(void)
743 {
744 	u64 mmfr0;
745 	u32 val;
746 
747 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
748 	val = cpuid_feature_extract_unsigned_field(mmfr0,
749 						ID_AA64MMFR0_EL1_TGRAN16_SHIFT);
750 
751 	return (val >= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN) &&
752 	       (val <= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX);
753 }
754 
system_supports_mixed_endian_el0(void)755 static inline bool system_supports_mixed_endian_el0(void)
756 {
757 	return id_aa64mmfr0_mixed_endian_el0(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1));
758 }
759 
system_supports_mixed_endian(void)760 static inline bool system_supports_mixed_endian(void)
761 {
762 	u64 mmfr0;
763 	u32 val;
764 
765 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
766 	val = cpuid_feature_extract_unsigned_field(mmfr0,
767 						ID_AA64MMFR0_EL1_BIGEND_SHIFT);
768 
769 	return val == 0x1;
770 }
771 
system_supports_fpsimd(void)772 static __always_inline bool system_supports_fpsimd(void)
773 {
774 	return alternative_has_cap_likely(ARM64_HAS_FPSIMD);
775 }
776 
system_uses_hw_pan(void)777 static inline bool system_uses_hw_pan(void)
778 {
779 	return alternative_has_cap_unlikely(ARM64_HAS_PAN);
780 }
781 
system_uses_ttbr0_pan(void)782 static inline bool system_uses_ttbr0_pan(void)
783 {
784 	return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&
785 		!system_uses_hw_pan();
786 }
787 
system_supports_sve(void)788 static __always_inline bool system_supports_sve(void)
789 {
790 	return alternative_has_cap_unlikely(ARM64_SVE);
791 }
792 
system_supports_sme(void)793 static __always_inline bool system_supports_sme(void)
794 {
795 	return alternative_has_cap_unlikely(ARM64_SME);
796 }
797 
system_supports_sme2(void)798 static __always_inline bool system_supports_sme2(void)
799 {
800 	return alternative_has_cap_unlikely(ARM64_SME2);
801 }
802 
system_supports_fa64(void)803 static __always_inline bool system_supports_fa64(void)
804 {
805 	return alternative_has_cap_unlikely(ARM64_SME_FA64);
806 }
807 
system_supports_tpidr2(void)808 static __always_inline bool system_supports_tpidr2(void)
809 {
810 	return system_supports_sme();
811 }
812 
system_supports_fpmr(void)813 static __always_inline bool system_supports_fpmr(void)
814 {
815 	return alternative_has_cap_unlikely(ARM64_HAS_FPMR);
816 }
817 
system_supports_cnp(void)818 static __always_inline bool system_supports_cnp(void)
819 {
820 	return alternative_has_cap_unlikely(ARM64_HAS_CNP);
821 }
822 
system_supports_address_auth(void)823 static inline bool system_supports_address_auth(void)
824 {
825 	return cpus_have_final_boot_cap(ARM64_HAS_ADDRESS_AUTH);
826 }
827 
system_supports_generic_auth(void)828 static inline bool system_supports_generic_auth(void)
829 {
830 	return alternative_has_cap_unlikely(ARM64_HAS_GENERIC_AUTH);
831 }
832 
system_has_full_ptr_auth(void)833 static inline bool system_has_full_ptr_auth(void)
834 {
835 	return system_supports_address_auth() && system_supports_generic_auth();
836 }
837 
system_uses_irq_prio_masking(void)838 static __always_inline bool system_uses_irq_prio_masking(void)
839 {
840 	return alternative_has_cap_unlikely(ARM64_HAS_GIC_PRIO_MASKING);
841 }
842 
system_supports_mte(void)843 static inline bool system_supports_mte(void)
844 {
845 	return alternative_has_cap_unlikely(ARM64_MTE);
846 }
847 
system_has_prio_mask_debugging(void)848 static inline bool system_has_prio_mask_debugging(void)
849 {
850 	return IS_ENABLED(CONFIG_ARM64_DEBUG_PRIORITY_MASKING) &&
851 	       system_uses_irq_prio_masking();
852 }
853 
system_supports_bti(void)854 static inline bool system_supports_bti(void)
855 {
856 	return cpus_have_final_cap(ARM64_BTI);
857 }
858 
system_supports_bti_kernel(void)859 static inline bool system_supports_bti_kernel(void)
860 {
861 	return IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) &&
862 		cpus_have_final_boot_cap(ARM64_BTI);
863 }
864 
system_supports_tlb_range(void)865 static inline bool system_supports_tlb_range(void)
866 {
867 	return alternative_has_cap_unlikely(ARM64_HAS_TLB_RANGE);
868 }
869 
system_supports_lpa2(void)870 static inline bool system_supports_lpa2(void)
871 {
872 	return cpus_have_final_cap(ARM64_HAS_LPA2);
873 }
874 
system_supports_poe(void)875 static inline bool system_supports_poe(void)
876 {
877 	return alternative_has_cap_unlikely(ARM64_HAS_S1POE);
878 }
879 
system_supports_gcs(void)880 static inline bool system_supports_gcs(void)
881 {
882 	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
883 }
884 
system_supports_haft(void)885 static inline bool system_supports_haft(void)
886 {
887 	return cpus_have_final_cap(ARM64_HAFT);
888 }
889 
system_supports_mpam(void)890 static __always_inline bool system_supports_mpam(void)
891 {
892 	return alternative_has_cap_unlikely(ARM64_MPAM);
893 }
894 
system_supports_mpam_hcr(void)895 static __always_inline bool system_supports_mpam_hcr(void)
896 {
897 	return alternative_has_cap_unlikely(ARM64_MPAM_HCR);
898 }
899 
system_supports_pmuv3(void)900 static inline bool system_supports_pmuv3(void)
901 {
902 	return cpus_have_final_cap(ARM64_HAS_PMUV3);
903 }
904 
905 bool cpu_supports_bbml3(void);
906 
system_supports_bbml3(void)907 static inline bool system_supports_bbml3(void)
908 {
909 	return alternative_has_cap_unlikely(ARM64_HAS_BBML3);
910 }
911 
912 int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
913 bool try_emulate_mrs(struct pt_regs *regs, u32 isn);
914 
id_aa64mmfr0_parange_to_phys_shift(int parange)915 static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)
916 {
917 	switch (parange) {
918 	case ID_AA64MMFR0_EL1_PARANGE_32: return 32;
919 	case ID_AA64MMFR0_EL1_PARANGE_36: return 36;
920 	case ID_AA64MMFR0_EL1_PARANGE_40: return 40;
921 	case ID_AA64MMFR0_EL1_PARANGE_42: return 42;
922 	case ID_AA64MMFR0_EL1_PARANGE_44: return 44;
923 	case ID_AA64MMFR0_EL1_PARANGE_48: return 48;
924 	case ID_AA64MMFR0_EL1_PARANGE_52: return 52;
925 	/*
926 	 * A future PE could use a value unknown to the kernel.
927 	 * However, by the "D10.1.4 Principles of the ID scheme
928 	 * for fields in ID registers", ARM DDI 0487C.a, any new
929 	 * value is guaranteed to be higher than what we know already.
930 	 * As a safe limit, we return the limit supported by the kernel.
931 	 */
932 	default: return CONFIG_ARM64_PA_BITS;
933 	}
934 }
935 
936 /* Check whether hardware update of the Access flag is supported */
cpu_has_hw_af(void)937 static inline bool cpu_has_hw_af(void)
938 {
939 	u64 mmfr1;
940 
941 	if (!IS_ENABLED(CONFIG_ARM64_HW_AFDBM))
942 		return false;
943 
944 	/*
945 	 * Use cached version to avoid emulated msr operation on KVM
946 	 * guests.
947 	 */
948 	mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
949 	return cpuid_feature_extract_unsigned_field(mmfr1,
950 						ID_AA64MMFR1_EL1_HAFDBS_SHIFT);
951 }
952 
cpu_has_pan(void)953 static inline bool cpu_has_pan(void)
954 {
955 	u64 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
956 	return cpuid_feature_extract_unsigned_field(mmfr1,
957 						    ID_AA64MMFR1_EL1_PAN_SHIFT);
958 }
959 
960 #ifdef CONFIG_ARM64_AMU_EXTN
961 /* Check whether the cpu supports the Activity Monitors Unit (AMU) */
962 extern bool cpu_has_amu_feat(int cpu);
963 #else
cpu_has_amu_feat(int cpu)964 static inline bool cpu_has_amu_feat(int cpu)
965 {
966 	return false;
967 }
968 #endif
969 
970 /* Get a cpu that supports the Activity Monitors Unit (AMU) */
971 extern int get_cpu_with_amu_feat(void);
972 
get_vmid_bits(u64 mmfr1)973 static inline unsigned int get_vmid_bits(u64 mmfr1)
974 {
975 	int vmid_bits;
976 
977 	vmid_bits = cpuid_feature_extract_unsigned_field(mmfr1,
978 						ID_AA64MMFR1_EL1_VMIDBits_SHIFT);
979 	if (vmid_bits == ID_AA64MMFR1_EL1_VMIDBits_16)
980 		return 16;
981 
982 	/*
983 	 * Return the default here even if any reserved
984 	 * value is fetched from the system register.
985 	 */
986 	return 8;
987 }
988 
989 s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
990 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
991 u64 arm64_ftr_reg_override(u32 sys_id, u64 val);
992 
993 /* Read this CPU's ID register with each valid command-line override applied. */
994 #define read_cpuid_with_overrides(reg)	arm64_ftr_reg_override(SYS_##reg, read_cpuid(reg))
995 
996 extern struct arm64_ftr_override id_aa64mmfr0_override;
997 extern struct arm64_ftr_override id_aa64mmfr1_override;
998 extern struct arm64_ftr_override id_aa64mmfr2_override;
999 extern struct arm64_ftr_override id_aa64mmfr4_override;
1000 extern struct arm64_ftr_override id_aa64pfr0_override;
1001 extern struct arm64_ftr_override id_aa64pfr1_override;
1002 extern struct arm64_ftr_override id_aa64zfr0_override;
1003 extern struct arm64_ftr_override id_aa64smfr0_override;
1004 extern struct arm64_ftr_override id_aa64isar1_override;
1005 extern struct arm64_ftr_override id_aa64isar2_override;
1006 
1007 extern struct arm64_ftr_override arm64_sw_feature_override;
1008 
1009 static inline
arm64_apply_feature_override(u64 val,int feat,int width,const struct arm64_ftr_override * override)1010 u64 arm64_apply_feature_override(u64 val, int feat, int width,
1011 				 const struct arm64_ftr_override *override)
1012 {
1013 	u64 oval = override->val;
1014 
1015 	/*
1016 	 * When it encounters an invalid override (e.g., an override that
1017 	 * cannot be honoured due to a missing CPU feature), the early idreg
1018 	 * override code will set the mask to 0x0 and the value to non-zero for
1019 	 * the field in question. In order to determine whether the override is
1020 	 * valid or not for the field we are interested in, we first need to
1021 	 * disregard bits belonging to other fields.
1022 	 */
1023 	oval &= GENMASK_ULL(feat + width - 1, feat);
1024 
1025 	/*
1026 	 * The override is valid if all value bits are accounted for in the
1027 	 * mask. If so, replace the masked bits with the override value.
1028 	 */
1029 	if (oval == (oval & override->mask)) {
1030 		val &= ~override->mask;
1031 		val |= oval;
1032 	}
1033 
1034 	/* Extract the field from the updated value */
1035 	return cpuid_feature_extract_unsigned_field(val, feat);
1036 }
1037 
arm64_test_sw_feature_override(int feat)1038 static inline bool arm64_test_sw_feature_override(int feat)
1039 {
1040 	/*
1041 	 * Software features are pseudo CPU features that have no underlying
1042 	 * CPUID system register value to apply the override to.
1043 	 */
1044 	return arm64_apply_feature_override(0, feat, 4,
1045 					    &arm64_sw_feature_override);
1046 }
1047 
kaslr_disabled_cmdline(void)1048 static inline bool kaslr_disabled_cmdline(void)
1049 {
1050 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_NOKASLR);
1051 }
1052 
1053 u32 get_kvm_ipa_limit(void);
1054 void dump_cpu_features(void);
1055 
cpu_has_bti(void)1056 static inline bool cpu_has_bti(void)
1057 {
1058 	if (!IS_ENABLED(CONFIG_ARM64_BTI))
1059 		return false;
1060 
1061 	return arm64_apply_feature_override(read_cpuid(ID_AA64PFR1_EL1),
1062 					    ID_AA64PFR1_EL1_BT_SHIFT, 4,
1063 					    &id_aa64pfr1_override);
1064 }
1065 
cpu_has_pac(void)1066 static inline bool cpu_has_pac(void)
1067 {
1068 	u64 isar1, isar2;
1069 
1070 	if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH))
1071 		return false;
1072 
1073 	isar1 = read_cpuid(ID_AA64ISAR1_EL1);
1074 	isar2 = read_cpuid(ID_AA64ISAR2_EL1);
1075 
1076 	if (arm64_apply_feature_override(isar1, ID_AA64ISAR1_EL1_APA_SHIFT, 4,
1077 					 &id_aa64isar1_override))
1078 		return true;
1079 
1080 	if (arm64_apply_feature_override(isar1, ID_AA64ISAR1_EL1_API_SHIFT, 4,
1081 					 &id_aa64isar1_override))
1082 		return true;
1083 
1084 	return arm64_apply_feature_override(isar2, ID_AA64ISAR2_EL1_APA3_SHIFT, 4,
1085 					    &id_aa64isar2_override);
1086 }
1087 
cpu_has_lva(void)1088 static inline bool cpu_has_lva(void)
1089 {
1090 	u64 mmfr2;
1091 
1092 	mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1093 	mmfr2 &= ~id_aa64mmfr2_override.mask;
1094 	mmfr2 |= id_aa64mmfr2_override.val;
1095 	return cpuid_feature_extract_unsigned_field(mmfr2,
1096 						    ID_AA64MMFR2_EL1_VARange_SHIFT);
1097 }
1098 
cpu_has_lpa2(void)1099 static inline bool cpu_has_lpa2(void)
1100 {
1101 #ifdef CONFIG_ARM64_LPA2
1102 	u64 mmfr0;
1103 	int feat;
1104 
1105 	mmfr0 = read_sysreg(id_aa64mmfr0_el1);
1106 	mmfr0 &= ~id_aa64mmfr0_override.mask;
1107 	mmfr0 |= id_aa64mmfr0_override.val;
1108 	feat = cpuid_feature_extract_signed_field(mmfr0,
1109 						  ID_AA64MMFR0_EL1_TGRAN_SHIFT);
1110 
1111 	return feat >= ID_AA64MMFR0_EL1_TGRAN_LPA2;
1112 #else
1113 	return false;
1114 #endif
1115 }
1116 
1117 #endif /* __ASSEMBLER__ */
1118 
1119 #endif
1120