cpufeature.h 33 KB

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