xref: /OK3568_Linux_fs/kernel/arch/arm64/include/asm/cpufeature.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #ifndef __ASM_CPUFEATURE_H
7*4882a593Smuzhiyun #define __ASM_CPUFEATURE_H
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <asm/cpucaps.h>
10*4882a593Smuzhiyun #include <asm/cputype.h>
11*4882a593Smuzhiyun #include <asm/hwcap.h>
12*4882a593Smuzhiyun #include <asm/sysreg.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define MAX_CPU_FEATURES	64
15*4882a593Smuzhiyun #define cpu_feature(x)		KERNEL_HWCAP_ ## x
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #ifndef __ASSEMBLY__
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/bug.h>
20*4882a593Smuzhiyun #include <linux/jump_label.h>
21*4882a593Smuzhiyun #include <linux/kernel.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * CPU feature register tracking
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * The safe value of a CPUID feature field is dependent on the implications
27*4882a593Smuzhiyun  * of the values assigned to it by the architecture. Based on the relationship
28*4882a593Smuzhiyun  * between the values, the features are classified into 3 types - LOWER_SAFE,
29*4882a593Smuzhiyun  * HIGHER_SAFE and EXACT.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * The lowest value of all the CPUs is chosen for LOWER_SAFE and highest
32*4882a593Smuzhiyun  * for HIGHER_SAFE. It is expected that all CPUs have the same value for
33*4882a593Smuzhiyun  * a field when EXACT is specified, failing which, the safe value specified
34*4882a593Smuzhiyun  * in the table is chosen.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun enum ftr_type {
38*4882a593Smuzhiyun 	FTR_EXACT,			/* Use a predefined safe value */
39*4882a593Smuzhiyun 	FTR_LOWER_SAFE,			/* Smaller value is safe */
40*4882a593Smuzhiyun 	FTR_HIGHER_SAFE,		/* Bigger value is safe */
41*4882a593Smuzhiyun 	FTR_HIGHER_OR_ZERO_SAFE,	/* Bigger value is safe, but 0 is biggest */
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define FTR_STRICT	true	/* SANITY check strict matching required */
45*4882a593Smuzhiyun #define FTR_NONSTRICT	false	/* SANITY check ignored */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define FTR_SIGNED	true	/* Value should be treated as signed */
48*4882a593Smuzhiyun #define FTR_UNSIGNED	false	/* Value should be treated as unsigned */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define FTR_VISIBLE	true	/* Feature visible to the user space */
51*4882a593Smuzhiyun #define FTR_HIDDEN	false	/* Feature is hidden from the user */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define FTR_VISIBLE_IF_IS_ENABLED(config)		\
54*4882a593Smuzhiyun 	(IS_ENABLED(config) ? FTR_VISIBLE : FTR_HIDDEN)
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun struct arm64_ftr_bits {
57*4882a593Smuzhiyun 	bool		sign;	/* Value is signed ? */
58*4882a593Smuzhiyun 	bool		visible;
59*4882a593Smuzhiyun 	bool		strict;	/* CPU Sanity check: strict matching required ? */
60*4882a593Smuzhiyun 	enum ftr_type	type;
61*4882a593Smuzhiyun 	u8		shift;
62*4882a593Smuzhiyun 	u8		width;
63*4882a593Smuzhiyun 	s64		safe_val; /* safe value for FTR_EXACT features */
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun struct arm64_ftr_override {
67*4882a593Smuzhiyun 	u64		val;
68*4882a593Smuzhiyun 	u64		mask;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * @arm64_ftr_reg - Feature register
73*4882a593Smuzhiyun  * @strict_mask		Bits which should match across all CPUs for sanity.
74*4882a593Smuzhiyun  * @sys_val		Safe value across the CPUs (system view)
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun struct arm64_ftr_reg {
77*4882a593Smuzhiyun 	const char			*name;
78*4882a593Smuzhiyun 	u64				strict_mask;
79*4882a593Smuzhiyun 	u64				user_mask;
80*4882a593Smuzhiyun 	u64				sys_val;
81*4882a593Smuzhiyun 	u64				user_val;
82*4882a593Smuzhiyun 	struct arm64_ftr_override	*override;
83*4882a593Smuzhiyun 	const struct arm64_ftr_bits	*ftr_bits;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * CPU capabilities:
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * We use arm64_cpu_capabilities to represent system features, errata work
92*4882a593Smuzhiyun  * arounds (both used internally by kernel and tracked in cpu_hwcaps) and
93*4882a593Smuzhiyun  * ELF HWCAPs (which are exposed to user).
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * To support systems with heterogeneous CPUs, we need to make sure that we
96*4882a593Smuzhiyun  * detect the capabilities correctly on the system and take appropriate
97*4882a593Smuzhiyun  * measures to ensure there are no incompatibilities.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * This comment tries to explain how we treat the capabilities.
100*4882a593Smuzhiyun  * Each capability has the following list of attributes :
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * 1) Scope of Detection : The system detects a given capability by
103*4882a593Smuzhiyun  *    performing some checks at runtime. This could be, e.g, checking the
104*4882a593Smuzhiyun  *    value of a field in CPU ID feature register or checking the cpu
105*4882a593Smuzhiyun  *    model. The capability provides a call back ( @matches() ) to
106*4882a593Smuzhiyun  *    perform the check. Scope defines how the checks should be performed.
107*4882a593Smuzhiyun  *    There are three cases:
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  *     a) SCOPE_LOCAL_CPU: check all the CPUs and "detect" if at least one
110*4882a593Smuzhiyun  *        matches. This implies, we have to run the check on all the
111*4882a593Smuzhiyun  *        booting CPUs, until the system decides that state of the
112*4882a593Smuzhiyun  *        capability is finalised. (See section 2 below)
113*4882a593Smuzhiyun  *		Or
114*4882a593Smuzhiyun  *     b) SCOPE_SYSTEM: check all the CPUs and "detect" if all the CPUs
115*4882a593Smuzhiyun  *        matches. This implies, we run the check only once, when the
116*4882a593Smuzhiyun  *        system decides to finalise the state of the capability. If the
117*4882a593Smuzhiyun  *        capability relies on a field in one of the CPU ID feature
118*4882a593Smuzhiyun  *        registers, we use the sanitised value of the register from the
119*4882a593Smuzhiyun  *        CPU feature infrastructure to make the decision.
120*4882a593Smuzhiyun  *		Or
121*4882a593Smuzhiyun  *     c) SCOPE_BOOT_CPU: Check only on the primary boot CPU to detect the
122*4882a593Smuzhiyun  *        feature. This category is for features that are "finalised"
123*4882a593Smuzhiyun  *        (or used) by the kernel very early even before the SMP cpus
124*4882a593Smuzhiyun  *        are brought up.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  *    The process of detection is usually denoted by "update" capability
127*4882a593Smuzhiyun  *    state in the code.
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * 2) Finalise the state : The kernel should finalise the state of a
130*4882a593Smuzhiyun  *    capability at some point during its execution and take necessary
131*4882a593Smuzhiyun  *    actions if any. Usually, this is done, after all the boot-time
132*4882a593Smuzhiyun  *    enabled CPUs are brought up by the kernel, so that it can make
133*4882a593Smuzhiyun  *    better decision based on the available set of CPUs. However, there
134*4882a593Smuzhiyun  *    are some special cases, where the action is taken during the early
135*4882a593Smuzhiyun  *    boot by the primary boot CPU. (e.g, running the kernel at EL2 with
136*4882a593Smuzhiyun  *    Virtualisation Host Extensions). The kernel usually disallows any
137*4882a593Smuzhiyun  *    changes to the state of a capability once it finalises the capability
138*4882a593Smuzhiyun  *    and takes any action, as it may be impossible to execute the actions
139*4882a593Smuzhiyun  *    safely. A CPU brought up after a capability is "finalised" is
140*4882a593Smuzhiyun  *    referred to as "Late CPU" w.r.t the capability. e.g, all secondary
141*4882a593Smuzhiyun  *    CPUs are treated "late CPUs" for capabilities determined by the boot
142*4882a593Smuzhiyun  *    CPU.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  *    At the moment there are two passes of finalising the capabilities.
145*4882a593Smuzhiyun  *      a) Boot CPU scope capabilities - Finalised by primary boot CPU via
146*4882a593Smuzhiyun  *         setup_boot_cpu_capabilities().
147*4882a593Smuzhiyun  *      b) Everything except (a) - Run via setup_system_capabilities().
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * 3) Verification: When a CPU is brought online (e.g, by user or by the
150*4882a593Smuzhiyun  *    kernel), the kernel should make sure that it is safe to use the CPU,
151*4882a593Smuzhiyun  *    by verifying that the CPU is compliant with the state of the
152*4882a593Smuzhiyun  *    capabilities finalised already. This happens via :
153*4882a593Smuzhiyun  *
154*4882a593Smuzhiyun  *	secondary_start_kernel()-> check_local_cpu_capabilities()
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  *    As explained in (2) above, capabilities could be finalised at
157*4882a593Smuzhiyun  *    different points in the execution. Each newly booted CPU is verified
158*4882a593Smuzhiyun  *    against the capabilities that have been finalised by the time it
159*4882a593Smuzhiyun  *    boots.
160*4882a593Smuzhiyun  *
161*4882a593Smuzhiyun  *	a) SCOPE_BOOT_CPU : All CPUs are verified against the capability
162*4882a593Smuzhiyun  *	except for the primary boot CPU.
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  *	b) SCOPE_LOCAL_CPU, SCOPE_SYSTEM: All CPUs hotplugged on by the
165*4882a593Smuzhiyun  *	user after the kernel boot are verified against the capability.
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  *    If there is a conflict, the kernel takes an action, based on the
168*4882a593Smuzhiyun  *    severity (e.g, a CPU could be prevented from booting or cause a
169*4882a593Smuzhiyun  *    kernel panic). The CPU is allowed to "affect" the state of the
170*4882a593Smuzhiyun  *    capability, if it has not been finalised already. See section 5
171*4882a593Smuzhiyun  *    for more details on conflicts.
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * 4) Action: As mentioned in (2), the kernel can take an action for each
174*4882a593Smuzhiyun  *    detected capability, on all CPUs on the system. Appropriate actions
175*4882a593Smuzhiyun  *    include, turning on an architectural feature, modifying the control
176*4882a593Smuzhiyun  *    registers (e.g, SCTLR, TCR etc.) or patching the kernel via
177*4882a593Smuzhiyun  *    alternatives. The kernel patching is batched and performed at later
178*4882a593Smuzhiyun  *    point. The actions are always initiated only after the capability
179*4882a593Smuzhiyun  *    is finalised. This is usally denoted by "enabling" the capability.
180*4882a593Smuzhiyun  *    The actions are initiated as follows :
181*4882a593Smuzhiyun  *	a) Action is triggered on all online CPUs, after the capability is
182*4882a593Smuzhiyun  *	finalised, invoked within the stop_machine() context from
183*4882a593Smuzhiyun  *	enable_cpu_capabilitie().
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  *	b) Any late CPU, brought up after (1), the action is triggered via:
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  *	  check_local_cpu_capabilities() -> verify_local_cpu_capabilities()
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * 5) Conflicts: Based on the state of the capability on a late CPU vs.
190*4882a593Smuzhiyun  *    the system state, we could have the following combinations :
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  *		x-----------------------------x
193*4882a593Smuzhiyun  *		| Type  | System   | Late CPU |
194*4882a593Smuzhiyun  *		|-----------------------------|
195*4882a593Smuzhiyun  *		|  a    |   y      |    n     |
196*4882a593Smuzhiyun  *		|-----------------------------|
197*4882a593Smuzhiyun  *		|  b    |   n      |    y     |
198*4882a593Smuzhiyun  *		x-----------------------------x
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  *     Two separate flag bits are defined to indicate whether each kind of
201*4882a593Smuzhiyun  *     conflict can be allowed:
202*4882a593Smuzhiyun  *		ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU - Case(a) is allowed
203*4882a593Smuzhiyun  *		ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU - Case(b) is allowed
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  *     Case (a) is not permitted for a capability that the system requires
206*4882a593Smuzhiyun  *     all CPUs to have in order for the capability to be enabled. This is
207*4882a593Smuzhiyun  *     typical for capabilities that represent enhanced functionality.
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  *     Case (b) is not permitted for a capability that must be enabled
210*4882a593Smuzhiyun  *     during boot if any CPU in the system requires it in order to run
211*4882a593Smuzhiyun  *     safely. This is typical for erratum work arounds that cannot be
212*4882a593Smuzhiyun  *     enabled after the corresponding capability is finalised.
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  *     In some non-typical cases either both (a) and (b), or neither,
215*4882a593Smuzhiyun  *     should be permitted. This can be described by including neither
216*4882a593Smuzhiyun  *     or both flags in the capability's type field.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  *     In case of a conflict, the CPU is prevented from booting. If the
219*4882a593Smuzhiyun  *     ARM64_CPUCAP_PANIC_ON_CONFLICT flag is specified for the capability,
220*4882a593Smuzhiyun  *     then a kernel panic is triggered.
221*4882a593Smuzhiyun  */
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun  * Decide how the capability is detected.
226*4882a593Smuzhiyun  * On any local CPU vs System wide vs the primary boot CPU
227*4882a593Smuzhiyun  */
228*4882a593Smuzhiyun #define ARM64_CPUCAP_SCOPE_LOCAL_CPU		((u16)BIT(0))
229*4882a593Smuzhiyun #define ARM64_CPUCAP_SCOPE_SYSTEM		((u16)BIT(1))
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun  * The capabilitiy is detected on the Boot CPU and is used by kernel
232*4882a593Smuzhiyun  * during early boot. i.e, the capability should be "detected" and
233*4882a593Smuzhiyun  * "enabled" as early as possibly on all booting CPUs.
234*4882a593Smuzhiyun  */
235*4882a593Smuzhiyun #define ARM64_CPUCAP_SCOPE_BOOT_CPU		((u16)BIT(2))
236*4882a593Smuzhiyun #define ARM64_CPUCAP_SCOPE_MASK			\
237*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_SYSTEM	|	\
238*4882a593Smuzhiyun 	 ARM64_CPUCAP_SCOPE_LOCAL_CPU	|	\
239*4882a593Smuzhiyun 	 ARM64_CPUCAP_SCOPE_BOOT_CPU)
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun #define SCOPE_SYSTEM				ARM64_CPUCAP_SCOPE_SYSTEM
242*4882a593Smuzhiyun #define SCOPE_LOCAL_CPU				ARM64_CPUCAP_SCOPE_LOCAL_CPU
243*4882a593Smuzhiyun #define SCOPE_BOOT_CPU				ARM64_CPUCAP_SCOPE_BOOT_CPU
244*4882a593Smuzhiyun #define SCOPE_ALL				ARM64_CPUCAP_SCOPE_MASK
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun  * Is it permitted for a late CPU to have this capability when system
248*4882a593Smuzhiyun  * hasn't already enabled it ?
249*4882a593Smuzhiyun  */
250*4882a593Smuzhiyun #define ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU	((u16)BIT(4))
251*4882a593Smuzhiyun /* Is it safe for a late CPU to miss this capability when system has it */
252*4882a593Smuzhiyun #define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU	((u16)BIT(5))
253*4882a593Smuzhiyun /* Panic when a conflict is detected */
254*4882a593Smuzhiyun #define ARM64_CPUCAP_PANIC_ON_CONFLICT		((u16)BIT(6))
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun  * CPU errata workarounds that need to be enabled at boot time if one or
258*4882a593Smuzhiyun  * more CPUs in the system requires it. When one of these capabilities
259*4882a593Smuzhiyun  * has been enabled, it is safe to allow any CPU to boot that doesn't
260*4882a593Smuzhiyun  * require the workaround. However, it is not safe if a "late" CPU
261*4882a593Smuzhiyun  * requires a workaround and the system hasn't enabled it already.
262*4882a593Smuzhiyun  */
263*4882a593Smuzhiyun #define ARM64_CPUCAP_LOCAL_CPU_ERRATUM		\
264*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU | ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun  * CPU feature detected at boot time based on system-wide value of a
267*4882a593Smuzhiyun  * feature. It is safe for a late CPU to have this feature even though
268*4882a593Smuzhiyun  * the system hasn't enabled it, although the feature will not be used
269*4882a593Smuzhiyun  * by Linux in this case. If the system has enabled this feature already,
270*4882a593Smuzhiyun  * then every late CPU must have it.
271*4882a593Smuzhiyun  */
272*4882a593Smuzhiyun #define ARM64_CPUCAP_SYSTEM_FEATURE	\
273*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_SYSTEM | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
274*4882a593Smuzhiyun /*
275*4882a593Smuzhiyun  * CPU feature detected at boot time based on feature of one or more CPUs.
276*4882a593Smuzhiyun  * All possible conflicts for a late CPU are ignored.
277*4882a593Smuzhiyun  * NOTE: this means that a late CPU with the feature will *not* cause the
278*4882a593Smuzhiyun  * capability to be advertised by cpus_have_*cap()!
279*4882a593Smuzhiyun  */
280*4882a593Smuzhiyun #define ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE		\
281*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU		|	\
282*4882a593Smuzhiyun 	 ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU	|	\
283*4882a593Smuzhiyun 	 ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /*
286*4882a593Smuzhiyun  * CPU feature detected at boot time, on one or more CPUs. A late CPU
287*4882a593Smuzhiyun  * is not allowed to have the capability when the system doesn't have it.
288*4882a593Smuzhiyun  * It is Ok for a late CPU to miss the feature.
289*4882a593Smuzhiyun  */
290*4882a593Smuzhiyun #define ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE	\
291*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_LOCAL_CPU		|	\
292*4882a593Smuzhiyun 	 ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun  * CPU feature used early in the boot based on the boot CPU. All secondary
296*4882a593Smuzhiyun  * CPUs must match the state of the capability as detected by the boot CPU. In
297*4882a593Smuzhiyun  * case of a conflict, a kernel panic is triggered.
298*4882a593Smuzhiyun  */
299*4882a593Smuzhiyun #define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE		\
300*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PANIC_ON_CONFLICT)
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun  * CPU feature used early in the boot based on the boot CPU. It is safe for a
304*4882a593Smuzhiyun  * late CPU to have this feature even though the boot CPU hasn't enabled it,
305*4882a593Smuzhiyun  * although the feature will not be used by Linux in this case. If the boot CPU
306*4882a593Smuzhiyun  * has enabled this feature already, then every late CPU must have it.
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun #define ARM64_CPUCAP_BOOT_CPU_FEATURE                  \
309*4882a593Smuzhiyun 	(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun struct arm64_cpu_capabilities {
312*4882a593Smuzhiyun 	const char *desc;
313*4882a593Smuzhiyun 	u16 capability;
314*4882a593Smuzhiyun 	u16 type;
315*4882a593Smuzhiyun 	bool (*matches)(const struct arm64_cpu_capabilities *caps, int scope);
316*4882a593Smuzhiyun 	/*
317*4882a593Smuzhiyun 	 * Take the appropriate actions to configure this capability
318*4882a593Smuzhiyun 	 * for this CPU. If the capability is detected by the kernel
319*4882a593Smuzhiyun 	 * this will be called on all the CPUs in the system,
320*4882a593Smuzhiyun 	 * including the hotplugged CPUs, regardless of whether the
321*4882a593Smuzhiyun 	 * capability is available on that specific CPU. This is
322*4882a593Smuzhiyun 	 * useful for some capabilities (e.g, working around CPU
323*4882a593Smuzhiyun 	 * errata), where all the CPUs must take some action (e.g,
324*4882a593Smuzhiyun 	 * changing system control/configuration). Thus, if an action
325*4882a593Smuzhiyun 	 * is required only if the CPU has the capability, then the
326*4882a593Smuzhiyun 	 * routine must check it before taking any action.
327*4882a593Smuzhiyun 	 */
328*4882a593Smuzhiyun 	void (*cpu_enable)(const struct arm64_cpu_capabilities *cap);
329*4882a593Smuzhiyun 	union {
330*4882a593Smuzhiyun 		struct {	/* To be used for erratum handling only */
331*4882a593Smuzhiyun 			struct midr_range midr_range;
332*4882a593Smuzhiyun 			const struct arm64_midr_revidr {
333*4882a593Smuzhiyun 				u32 midr_rv;		/* revision/variant */
334*4882a593Smuzhiyun 				u32 revidr_mask;
335*4882a593Smuzhiyun 			} * const fixed_revs;
336*4882a593Smuzhiyun 		};
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 		const struct midr_range *midr_range_list;
339*4882a593Smuzhiyun 		struct {	/* Feature register checking */
340*4882a593Smuzhiyun 			u32 sys_reg;
341*4882a593Smuzhiyun 			u8 field_pos;
342*4882a593Smuzhiyun 			u8 min_field_value;
343*4882a593Smuzhiyun 			u8 hwcap_type;
344*4882a593Smuzhiyun 			bool sign;
345*4882a593Smuzhiyun 			unsigned long hwcap;
346*4882a593Smuzhiyun 		};
347*4882a593Smuzhiyun 	};
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	/*
350*4882a593Smuzhiyun 	 * An optional list of "matches/cpu_enable" pair for the same
351*4882a593Smuzhiyun 	 * "capability" of the same "type" as described by the parent.
352*4882a593Smuzhiyun 	 * Only matches(), cpu_enable() and fields relevant to these
353*4882a593Smuzhiyun 	 * methods are significant in the list. The cpu_enable is
354*4882a593Smuzhiyun 	 * invoked only if the corresponding entry "matches()".
355*4882a593Smuzhiyun 	 * However, if a cpu_enable() method is associated
356*4882a593Smuzhiyun 	 * with multiple matches(), care should be taken that either
357*4882a593Smuzhiyun 	 * the match criteria are mutually exclusive, or that the
358*4882a593Smuzhiyun 	 * method is robust against being called multiple times.
359*4882a593Smuzhiyun 	 */
360*4882a593Smuzhiyun 	const struct arm64_cpu_capabilities *match_list;
361*4882a593Smuzhiyun };
362*4882a593Smuzhiyun 
cpucap_default_scope(const struct arm64_cpu_capabilities * cap)363*4882a593Smuzhiyun static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	return cap->type & ARM64_CPUCAP_SCOPE_MASK;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun  * Generic helper for handling capabilities with multiple (match,enable) pairs
370*4882a593Smuzhiyun  * of call backs, sharing the same capability bit.
371*4882a593Smuzhiyun  * Iterate over each entry to see if at least one matches.
372*4882a593Smuzhiyun  */
373*4882a593Smuzhiyun static inline bool
cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities * entry,int scope)374*4882a593Smuzhiyun cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry,
375*4882a593Smuzhiyun 			       int scope)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	const struct arm64_cpu_capabilities *caps;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	for (caps = entry->match_list; caps->matches; caps++)
380*4882a593Smuzhiyun 		if (caps->matches(caps, scope))
381*4882a593Smuzhiyun 			return true;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	return false;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
is_vhe_hyp_code(void)386*4882a593Smuzhiyun static __always_inline bool is_vhe_hyp_code(void)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	/* Only defined for code run in VHE hyp context */
389*4882a593Smuzhiyun 	return __is_defined(__KVM_VHE_HYPERVISOR__);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
is_nvhe_hyp_code(void)392*4882a593Smuzhiyun static __always_inline bool is_nvhe_hyp_code(void)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	/* Only defined for code run in NVHE hyp context */
395*4882a593Smuzhiyun 	return __is_defined(__KVM_NVHE_HYPERVISOR__);
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
is_hyp_code(void)398*4882a593Smuzhiyun static __always_inline bool is_hyp_code(void)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun 	return is_vhe_hyp_code() || is_nvhe_hyp_code();
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
404*4882a593Smuzhiyun extern struct static_key_false cpu_hwcap_keys[ARM64_NCAPS];
405*4882a593Smuzhiyun extern struct static_key_false arm64_const_caps_ready;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun /* ARM64 CAPS + alternative_cb */
408*4882a593Smuzhiyun #define ARM64_NPATCHABLE (ARM64_NCAPS + 1)
409*4882a593Smuzhiyun extern DECLARE_BITMAP(boot_capabilities, ARM64_NPATCHABLE);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun #define for_each_available_cap(cap)		\
412*4882a593Smuzhiyun 	for_each_set_bit(cap, cpu_hwcaps, ARM64_NCAPS)
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun bool this_cpu_has_cap(unsigned int cap);
415*4882a593Smuzhiyun void cpu_set_feature(unsigned int num);
416*4882a593Smuzhiyun bool cpu_have_feature(unsigned int num);
417*4882a593Smuzhiyun unsigned long cpu_get_elf_hwcap(void);
418*4882a593Smuzhiyun unsigned long cpu_get_elf_hwcap2(void);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun #define cpu_set_named_feature(name) cpu_set_feature(cpu_feature(name))
421*4882a593Smuzhiyun #define cpu_have_named_feature(name) cpu_have_feature(cpu_feature(name))
422*4882a593Smuzhiyun 
system_capabilities_finalized(void)423*4882a593Smuzhiyun static __always_inline bool system_capabilities_finalized(void)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	return static_branch_likely(&arm64_const_caps_ready);
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun /*
429*4882a593Smuzhiyun  * Test for a capability with a runtime check.
430*4882a593Smuzhiyun  *
431*4882a593Smuzhiyun  * Before the capability is detected, this returns false.
432*4882a593Smuzhiyun  */
cpus_have_cap(unsigned int num)433*4882a593Smuzhiyun static inline bool cpus_have_cap(unsigned int num)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	if (num >= ARM64_NCAPS)
436*4882a593Smuzhiyun 		return false;
437*4882a593Smuzhiyun 	return test_bit(num, cpu_hwcaps);
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /*
441*4882a593Smuzhiyun  * Test for a capability without a runtime check.
442*4882a593Smuzhiyun  *
443*4882a593Smuzhiyun  * Before capabilities are finalized, this returns false.
444*4882a593Smuzhiyun  * After capabilities are finalized, this is patched to avoid a runtime check.
445*4882a593Smuzhiyun  *
446*4882a593Smuzhiyun  * @num must be a compile-time constant.
447*4882a593Smuzhiyun  */
__cpus_have_const_cap(int num)448*4882a593Smuzhiyun static __always_inline bool __cpus_have_const_cap(int num)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	if (num >= ARM64_NCAPS)
451*4882a593Smuzhiyun 		return false;
452*4882a593Smuzhiyun 	return static_branch_unlikely(&cpu_hwcap_keys[num]);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * Test for a capability without a runtime check.
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * Before capabilities are finalized, this will BUG().
459*4882a593Smuzhiyun  * After capabilities are finalized, this is patched to avoid a runtime check.
460*4882a593Smuzhiyun  *
461*4882a593Smuzhiyun  * @num must be a compile-time constant.
462*4882a593Smuzhiyun  */
cpus_have_final_cap(int num)463*4882a593Smuzhiyun static __always_inline bool cpus_have_final_cap(int num)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	if (system_capabilities_finalized())
466*4882a593Smuzhiyun 		return __cpus_have_const_cap(num);
467*4882a593Smuzhiyun 	else
468*4882a593Smuzhiyun 		BUG();
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun /*
472*4882a593Smuzhiyun  * Test for a capability, possibly with a runtime check for non-hyp code.
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * For hyp code, this behaves the same as cpus_have_final_cap().
475*4882a593Smuzhiyun  *
476*4882a593Smuzhiyun  * For non-hyp code:
477*4882a593Smuzhiyun  * Before capabilities are finalized, this behaves as cpus_have_cap().
478*4882a593Smuzhiyun  * After capabilities are finalized, this is patched to avoid a runtime check.
479*4882a593Smuzhiyun  *
480*4882a593Smuzhiyun  * @num must be a compile-time constant.
481*4882a593Smuzhiyun  */
cpus_have_const_cap(int num)482*4882a593Smuzhiyun static __always_inline bool cpus_have_const_cap(int num)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	if (is_hyp_code())
485*4882a593Smuzhiyun 		return cpus_have_final_cap(num);
486*4882a593Smuzhiyun 	else if (system_capabilities_finalized())
487*4882a593Smuzhiyun 		return __cpus_have_const_cap(num);
488*4882a593Smuzhiyun 	else
489*4882a593Smuzhiyun 		return cpus_have_cap(num);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
cpus_set_cap(unsigned int num)492*4882a593Smuzhiyun static inline void cpus_set_cap(unsigned int num)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	if (num >= ARM64_NCAPS) {
495*4882a593Smuzhiyun 		pr_warn("Attempt to set an illegal CPU capability (%d >= %d)\n",
496*4882a593Smuzhiyun 			num, ARM64_NCAPS);
497*4882a593Smuzhiyun 	} else {
498*4882a593Smuzhiyun 		__set_bit(num, cpu_hwcaps);
499*4882a593Smuzhiyun 	}
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun static inline int __attribute_const__
cpuid_feature_extract_signed_field_width(u64 features,int field,int width)503*4882a593Smuzhiyun cpuid_feature_extract_signed_field_width(u64 features, int field, int width)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	return (s64)(features << (64 - width - field)) >> (64 - width);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun static inline int __attribute_const__
cpuid_feature_extract_signed_field(u64 features,int field)509*4882a593Smuzhiyun cpuid_feature_extract_signed_field(u64 features, int field)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	return cpuid_feature_extract_signed_field_width(features, field, 4);
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun static __always_inline unsigned int __attribute_const__
cpuid_feature_extract_unsigned_field_width(u64 features,int field,int width)515*4882a593Smuzhiyun cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun 	return (u64)(features << (64 - width - field)) >> (64 - width);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun static __always_inline unsigned int __attribute_const__
cpuid_feature_extract_unsigned_field(u64 features,int field)521*4882a593Smuzhiyun cpuid_feature_extract_unsigned_field(u64 features, int field)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	return cpuid_feature_extract_unsigned_field_width(features, field, 4);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun  * Fields that identify the version of the Performance Monitors Extension do
528*4882a593Smuzhiyun  * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825,
529*4882a593Smuzhiyun  * "Alternative ID scheme used for the Performance Monitors Extension version".
530*4882a593Smuzhiyun  */
531*4882a593Smuzhiyun static inline u64 __attribute_const__
cpuid_feature_cap_perfmon_field(u64 features,int field,u64 cap)532*4882a593Smuzhiyun cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	u64 val = cpuid_feature_extract_unsigned_field(features, field);
535*4882a593Smuzhiyun 	u64 mask = GENMASK_ULL(field + 3, field);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */
538*4882a593Smuzhiyun 	if (val == 0xf)
539*4882a593Smuzhiyun 		val = 0;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (val > cap) {
542*4882a593Smuzhiyun 		features &= ~mask;
543*4882a593Smuzhiyun 		features |= (cap << field) & mask;
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	return features;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
arm64_ftr_mask(const struct arm64_ftr_bits * ftrp)549*4882a593Smuzhiyun static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
arm64_ftr_reg_user_value(const struct arm64_ftr_reg * reg)554*4882a593Smuzhiyun static inline u64 arm64_ftr_reg_user_value(const struct arm64_ftr_reg *reg)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	return (reg->user_val | (reg->sys_val & reg->user_mask));
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun static inline int __attribute_const__
cpuid_feature_extract_field_width(u64 features,int field,int width,bool sign)560*4882a593Smuzhiyun cpuid_feature_extract_field_width(u64 features, int field, int width, bool sign)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	return (sign) ?
563*4882a593Smuzhiyun 		cpuid_feature_extract_signed_field_width(features, field, width) :
564*4882a593Smuzhiyun 		cpuid_feature_extract_unsigned_field_width(features, field, width);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun static inline int __attribute_const__
cpuid_feature_extract_field(u64 features,int field,bool sign)568*4882a593Smuzhiyun cpuid_feature_extract_field(u64 features, int field, bool sign)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	return cpuid_feature_extract_field_width(features, field, 4, sign);
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
arm64_ftr_value(const struct arm64_ftr_bits * ftrp,u64 val)573*4882a593Smuzhiyun static inline s64 arm64_ftr_value(const struct arm64_ftr_bits *ftrp, u64 val)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	return (s64)cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width, ftrp->sign);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)578*4882a593Smuzhiyun static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	return cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL_SHIFT) == 0x1 ||
581*4882a593Smuzhiyun 		cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL0_SHIFT) == 0x1;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
id_aa64pfr0_32bit_el1(u64 pfr0)584*4882a593Smuzhiyun static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SHIFT);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	return val == ID_AA64PFR0_EL1_32BIT_64BIT;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
id_aa64pfr0_32bit_el0(u64 pfr0)591*4882a593Smuzhiyun static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL0_SHIFT);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	return val == ID_AA64PFR0_EL0_32BIT_64BIT;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun 
id_aa64pfr0_sve(u64 pfr0)598*4882a593Smuzhiyun static inline bool id_aa64pfr0_sve(u64 pfr0)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_SVE_SHIFT);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	return val > 0;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun void __init setup_cpu_features(void);
606*4882a593Smuzhiyun void check_local_cpu_capabilities(void);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun u64 read_sanitised_ftr_reg(u32 id);
609*4882a593Smuzhiyun u64 __read_sysreg_by_encoding(u32 sys_id);
610*4882a593Smuzhiyun 
cpu_supports_mixed_endian_el0(void)611*4882a593Smuzhiyun static inline bool cpu_supports_mixed_endian_el0(void)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 
supports_csv2p3(int scope)617*4882a593Smuzhiyun static inline bool supports_csv2p3(int scope)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	u64 pfr0;
620*4882a593Smuzhiyun 	u8 csv2_val;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (scope == SCOPE_LOCAL_CPU)
623*4882a593Smuzhiyun 		pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
624*4882a593Smuzhiyun 	else
625*4882a593Smuzhiyun 		pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	csv2_val = cpuid_feature_extract_unsigned_field(pfr0,
628*4882a593Smuzhiyun 							ID_AA64PFR0_CSV2_SHIFT);
629*4882a593Smuzhiyun 	return csv2_val == 3;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
supports_clearbhb(int scope)632*4882a593Smuzhiyun static inline bool supports_clearbhb(int scope)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	u64 isar2;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	if (scope == SCOPE_LOCAL_CPU)
637*4882a593Smuzhiyun 		isar2 = read_sysreg_s(SYS_ID_AA64ISAR2_EL1);
638*4882a593Smuzhiyun 	else
639*4882a593Smuzhiyun 		isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	return cpuid_feature_extract_unsigned_field(isar2,
642*4882a593Smuzhiyun 						    ID_AA64ISAR2_CLEARBHB_SHIFT);
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun const struct cpumask *system_32bit_el0_cpumask(void);
646*4882a593Smuzhiyun DECLARE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
647*4882a593Smuzhiyun 
system_supports_32bit_el0(void)648*4882a593Smuzhiyun static inline bool system_supports_32bit_el0(void)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	return static_branch_unlikely(&arm64_mismatched_32bit_el0) ||
653*4882a593Smuzhiyun 	       id_aa64pfr0_32bit_el0(pfr0);
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
system_supports_4kb_granule(void)656*4882a593Smuzhiyun static inline bool system_supports_4kb_granule(void)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	u64 mmfr0;
659*4882a593Smuzhiyun 	u32 val;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
662*4882a593Smuzhiyun 	val = cpuid_feature_extract_unsigned_field(mmfr0,
663*4882a593Smuzhiyun 						ID_AA64MMFR0_TGRAN4_SHIFT);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return (val >= ID_AA64MMFR0_TGRAN4_SUPPORTED_MIN) &&
666*4882a593Smuzhiyun 	       (val <= ID_AA64MMFR0_TGRAN4_SUPPORTED_MAX);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
system_supports_64kb_granule(void)669*4882a593Smuzhiyun static inline bool system_supports_64kb_granule(void)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	u64 mmfr0;
672*4882a593Smuzhiyun 	u32 val;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
675*4882a593Smuzhiyun 	val = cpuid_feature_extract_unsigned_field(mmfr0,
676*4882a593Smuzhiyun 						ID_AA64MMFR0_TGRAN64_SHIFT);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return (val >= ID_AA64MMFR0_TGRAN64_SUPPORTED_MIN) &&
679*4882a593Smuzhiyun 	       (val <= ID_AA64MMFR0_TGRAN64_SUPPORTED_MAX);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
system_supports_16kb_granule(void)682*4882a593Smuzhiyun static inline bool system_supports_16kb_granule(void)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	u64 mmfr0;
685*4882a593Smuzhiyun 	u32 val;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
688*4882a593Smuzhiyun 	val = cpuid_feature_extract_unsigned_field(mmfr0,
689*4882a593Smuzhiyun 						ID_AA64MMFR0_TGRAN16_SHIFT);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	return (val >= ID_AA64MMFR0_TGRAN16_SUPPORTED_MIN) &&
692*4882a593Smuzhiyun 	       (val <= ID_AA64MMFR0_TGRAN16_SUPPORTED_MAX);
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
system_supports_mixed_endian_el0(void)695*4882a593Smuzhiyun static inline bool system_supports_mixed_endian_el0(void)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun 	return id_aa64mmfr0_mixed_endian_el0(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1));
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
system_supports_mixed_endian(void)700*4882a593Smuzhiyun static inline bool system_supports_mixed_endian(void)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	u64 mmfr0;
703*4882a593Smuzhiyun 	u32 val;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	mmfr0 =	read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
706*4882a593Smuzhiyun 	val = cpuid_feature_extract_unsigned_field(mmfr0,
707*4882a593Smuzhiyun 						ID_AA64MMFR0_BIGENDEL_SHIFT);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	return val == 0x1;
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun 
system_supports_fpsimd(void)712*4882a593Smuzhiyun static __always_inline bool system_supports_fpsimd(void)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun 	return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD);
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun 
system_uses_ttbr0_pan(void)717*4882a593Smuzhiyun static inline bool system_uses_ttbr0_pan(void)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&
720*4882a593Smuzhiyun 		!cpus_have_const_cap(ARM64_HAS_PAN);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
system_supports_sve(void)723*4882a593Smuzhiyun static __always_inline bool system_supports_sve(void)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_SVE) &&
726*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_SVE);
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
system_supports_cnp(void)729*4882a593Smuzhiyun static __always_inline bool system_supports_cnp(void)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_CNP) &&
732*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_HAS_CNP);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun 
system_supports_address_auth(void)735*4882a593Smuzhiyun static inline bool system_supports_address_auth(void)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_PTR_AUTH) &&
738*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_HAS_ADDRESS_AUTH);
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun 
system_supports_generic_auth(void)741*4882a593Smuzhiyun static inline bool system_supports_generic_auth(void)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_PTR_AUTH) &&
744*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_HAS_GENERIC_AUTH);
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
system_has_full_ptr_auth(void)747*4882a593Smuzhiyun static inline bool system_has_full_ptr_auth(void)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	return system_supports_address_auth() && system_supports_generic_auth();
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun 
system_uses_irq_prio_masking(void)752*4882a593Smuzhiyun static __always_inline bool system_uses_irq_prio_masking(void)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) &&
755*4882a593Smuzhiyun 	       cpus_have_const_cap(ARM64_HAS_IRQ_PRIO_MASKING);
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun 
system_supports_mte(void)758*4882a593Smuzhiyun static inline bool system_supports_mte(void)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_MTE) &&
761*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_MTE);
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
system_has_prio_mask_debugging(void)764*4882a593Smuzhiyun static inline bool system_has_prio_mask_debugging(void)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_DEBUG_PRIORITY_MASKING) &&
767*4882a593Smuzhiyun 	       system_uses_irq_prio_masking();
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun 
system_supports_bti(void)770*4882a593Smuzhiyun static inline bool system_supports_bti(void)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_BTI) && cpus_have_const_cap(ARM64_BTI);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
system_supports_tlb_range(void)775*4882a593Smuzhiyun static inline bool system_supports_tlb_range(void)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_ARM64_TLB_RANGE) &&
778*4882a593Smuzhiyun 		cpus_have_const_cap(ARM64_HAS_TLB_RANGE);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun extern int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
782*4882a593Smuzhiyun 
id_aa64mmfr0_parange_to_phys_shift(int parange)783*4882a593Smuzhiyun static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)
784*4882a593Smuzhiyun {
785*4882a593Smuzhiyun 	switch (parange) {
786*4882a593Smuzhiyun 	case 0: return 32;
787*4882a593Smuzhiyun 	case 1: return 36;
788*4882a593Smuzhiyun 	case 2: return 40;
789*4882a593Smuzhiyun 	case 3: return 42;
790*4882a593Smuzhiyun 	case 4: return 44;
791*4882a593Smuzhiyun 	case 5: return 48;
792*4882a593Smuzhiyun 	case 6: return 52;
793*4882a593Smuzhiyun 	/*
794*4882a593Smuzhiyun 	 * A future PE could use a value unknown to the kernel.
795*4882a593Smuzhiyun 	 * However, by the "D10.1.4 Principles of the ID scheme
796*4882a593Smuzhiyun 	 * for fields in ID registers", ARM DDI 0487C.a, any new
797*4882a593Smuzhiyun 	 * value is guaranteed to be higher than what we know already.
798*4882a593Smuzhiyun 	 * As a safe limit, we return the limit supported by the kernel.
799*4882a593Smuzhiyun 	 */
800*4882a593Smuzhiyun 	default: return CONFIG_ARM64_PA_BITS;
801*4882a593Smuzhiyun 	}
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun /* Check whether hardware update of the Access flag is supported */
cpu_has_hw_af(void)805*4882a593Smuzhiyun static inline bool cpu_has_hw_af(void)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	u64 mmfr1;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_ARM64_HW_AFDBM))
810*4882a593Smuzhiyun 		return false;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
813*4882a593Smuzhiyun 	return cpuid_feature_extract_unsigned_field(mmfr1,
814*4882a593Smuzhiyun 						ID_AA64MMFR1_HADBS_SHIFT);
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun #ifdef CONFIG_ARM64_AMU_EXTN
818*4882a593Smuzhiyun /* Check whether the cpu supports the Activity Monitors Unit (AMU) */
819*4882a593Smuzhiyun extern bool cpu_has_amu_feat(int cpu);
820*4882a593Smuzhiyun #endif
821*4882a593Smuzhiyun 
get_vmid_bits(u64 mmfr1)822*4882a593Smuzhiyun static inline unsigned int get_vmid_bits(u64 mmfr1)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	int vmid_bits;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	vmid_bits = cpuid_feature_extract_unsigned_field(mmfr1,
827*4882a593Smuzhiyun 						ID_AA64MMFR1_VMIDBITS_SHIFT);
828*4882a593Smuzhiyun 	if (vmid_bits == ID_AA64MMFR1_VMIDBITS_16)
829*4882a593Smuzhiyun 		return 16;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	/*
832*4882a593Smuzhiyun 	 * Return the default here even if any reserved
833*4882a593Smuzhiyun 	 * value is fetched from the system register.
834*4882a593Smuzhiyun 	 */
835*4882a593Smuzhiyun 	return 8;
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun extern struct arm64_ftr_override id_aa64mmfr1_override;
839*4882a593Smuzhiyun extern struct arm64_ftr_override id_aa64pfr1_override;
840*4882a593Smuzhiyun extern struct arm64_ftr_override id_aa64isar1_override;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun u32 get_kvm_ipa_limit(void);
843*4882a593Smuzhiyun void dump_cpu_features(void);
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun #endif /* __ASSEMBLY__ */
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun #endif
848