cpufeature.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __ASM_CPUFEATURE_H
  9. #define __ASM_CPUFEATURE_H
  10. #include <asm/cpucaps.h>
  11. #include <asm/cputype.h>
  12. #include <asm/hwcap.h>
  13. #include <asm/sysreg.h>
  14. /*
  15. * In the arm64 world (as in the ARM world), elf_hwcap is used both internally
  16. * in the kernel and for user space to keep track of which optional features
  17. * are supported by the current system. So let's map feature 'x' to HWCAP_x.
  18. * Note that HWCAP_x constants are bit fields so we need to take the log.
  19. */
  20. #define MAX_CPU_FEATURES (8 * sizeof(elf_hwcap))
  21. #define cpu_feature(x) ilog2(HWCAP_ ## x)
  22. #ifndef __ASSEMBLY__
  23. #include <linux/bug.h>
  24. #include <linux/jump_label.h>
  25. #include <linux/kernel.h>
  26. /*
  27. * CPU feature register tracking
  28. *
  29. * The safe value of a CPUID feature field is dependent on the implications
  30. * of the values assigned to it by the architecture. Based on the relationship
  31. * between the values, the features are classified into 3 types - LOWER_SAFE,
  32. * HIGHER_SAFE and EXACT.
  33. *
  34. * The lowest value of all the CPUs is chosen for LOWER_SAFE and highest
  35. * for HIGHER_SAFE. It is expected that all CPUs have the same value for
  36. * a field when EXACT is specified, failing which, the safe value specified
  37. * in the table is chosen.
  38. */
  39. enum ftr_type {
  40. FTR_EXACT, /* Use a predefined safe value */
  41. FTR_LOWER_SAFE, /* Smaller value is safe */
  42. FTR_HIGHER_SAFE, /* Bigger value is safe */
  43. FTR_HIGHER_OR_ZERO_SAFE, /* Bigger value is safe, but 0 is biggest */
  44. };
  45. #define FTR_STRICT true /* SANITY check strict matching required */
  46. #define FTR_NONSTRICT false /* SANITY check ignored */
  47. #define FTR_SIGNED true /* Value should be treated as signed */
  48. #define FTR_UNSIGNED false /* Value should be treated as unsigned */
  49. #define FTR_VISIBLE true /* Feature visible to the user space */
  50. #define FTR_HIDDEN false /* Feature is hidden from the user */
  51. #define FTR_VISIBLE_IF_IS_ENABLED(config) \
  52. (IS_ENABLED(config) ? FTR_VISIBLE : FTR_HIDDEN)
  53. struct arm64_ftr_bits {
  54. bool sign; /* Value is signed ? */
  55. bool visible;
  56. bool strict; /* CPU Sanity check: strict matching required ? */
  57. enum ftr_type type;
  58. u8 shift;
  59. u8 width;
  60. s64 safe_val; /* safe value for FTR_EXACT features */
  61. };
  62. /*
  63. * @arm64_ftr_reg - Feature register
  64. * @strict_mask Bits which should match across all CPUs for sanity.
  65. * @sys_val Safe value across the CPUs (system view)
  66. */
  67. struct arm64_ftr_reg {
  68. const char *name;
  69. u64 strict_mask;
  70. u64 user_mask;
  71. u64 sys_val;
  72. u64 user_val;
  73. const struct arm64_ftr_bits *ftr_bits;
  74. };
  75. extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
  76. /*
  77. * CPU capabilities:
  78. *
  79. * We use arm64_cpu_capabilities to represent system features, errata work
  80. * arounds (both used internally by kernel and tracked in cpu_hwcaps) and
  81. * ELF HWCAPs (which are exposed to user).
  82. *
  83. * To support systems with heterogeneous CPUs, we need to make sure that we
  84. * detect the capabilities correctly on the system and take appropriate
  85. * measures to ensure there are no incompatibilities.
  86. *
  87. * This comment tries to explain how we treat the capabilities.
  88. * Each capability has the following list of attributes :
  89. *
  90. * 1) Scope of Detection : The system detects a given capability by
  91. * performing some checks at runtime. This could be, e.g, checking the
  92. * value of a field in CPU ID feature register or checking the cpu
  93. * model. The capability provides a call back ( @matches() ) to
  94. * perform the check. Scope defines how the checks should be performed.
  95. * There are three cases:
  96. *
  97. * a) SCOPE_LOCAL_CPU: check all the CPUs and "detect" if at least one
  98. * matches. This implies, we have to run the check on all the
  99. * booting CPUs, until the system decides that state of the
  100. * capability is finalised. (See section 2 below)
  101. * Or
  102. * b) SCOPE_SYSTEM: check all the CPUs and "detect" if all the CPUs
  103. * matches. This implies, we run the check only once, when the
  104. * system decides to finalise the state of the capability. If the
  105. * capability relies on a field in one of the CPU ID feature
  106. * registers, we use the sanitised value of the register from the
  107. * CPU feature infrastructure to make the decision.
  108. * Or
  109. * c) SCOPE_BOOT_CPU: Check only on the primary boot CPU to detect the
  110. * feature. This category is for features that are "finalised"
  111. * (or used) by the kernel very early even before the SMP cpus
  112. * are brought up.
  113. *
  114. * The process of detection is usually denoted by "update" capability
  115. * state in the code.
  116. *
  117. * 2) Finalise the state : The kernel should finalise the state of a
  118. * capability at some point during its execution and take necessary
  119. * actions if any. Usually, this is done, after all the boot-time
  120. * enabled CPUs are brought up by the kernel, so that it can make
  121. * better decision based on the available set of CPUs. However, there
  122. * are some special cases, where the action is taken during the early
  123. * boot by the primary boot CPU. (e.g, running the kernel at EL2 with
  124. * Virtualisation Host Extensions). The kernel usually disallows any
  125. * changes to the state of a capability once it finalises the capability
  126. * and takes any action, as it may be impossible to execute the actions
  127. * safely. A CPU brought up after a capability is "finalised" is
  128. * referred to as "Late CPU" w.r.t the capability. e.g, all secondary
  129. * CPUs are treated "late CPUs" for capabilities determined by the boot
  130. * CPU.
  131. *
  132. * At the moment there are two passes of finalising the capabilities.
  133. * a) Boot CPU scope capabilities - Finalised by primary boot CPU via
  134. * setup_boot_cpu_capabilities().
  135. * b) Everything except (a) - Run via setup_system_capabilities().
  136. *
  137. * 3) Verification: When a CPU is brought online (e.g, by user or by the
  138. * kernel), the kernel should make sure that it is safe to use the CPU,
  139. * by verifying that the CPU is compliant with the state of the
  140. * capabilities finalised already. This happens via :
  141. *
  142. * secondary_start_kernel()-> check_local_cpu_capabilities()
  143. *
  144. * As explained in (2) above, capabilities could be finalised at
  145. * different points in the execution. Each newly booted CPU is verified
  146. * against the capabilities that have been finalised by the time it
  147. * boots.
  148. *
  149. * a) SCOPE_BOOT_CPU : All CPUs are verified against the capability
  150. * except for the primary boot CPU.
  151. *
  152. * b) SCOPE_LOCAL_CPU, SCOPE_SYSTEM: All CPUs hotplugged on by the
  153. * user after the kernel boot are verified against the capability.
  154. *
  155. * If there is a conflict, the kernel takes an action, based on the
  156. * severity (e.g, a CPU could be prevented from booting or cause a
  157. * kernel panic). The CPU is allowed to "affect" the state of the
  158. * capability, if it has not been finalised already. See section 5
  159. * for more details on conflicts.
  160. *
  161. * 4) Action: As mentioned in (2), the kernel can take an action for each
  162. * detected capability, on all CPUs on the system. Appropriate actions
  163. * include, turning on an architectural feature, modifying the control
  164. * registers (e.g, SCTLR, TCR etc.) or patching the kernel via
  165. * alternatives. The kernel patching is batched and performed at later
  166. * point. The actions are always initiated only after the capability
  167. * is finalised. This is usally denoted by "enabling" the capability.
  168. * The actions are initiated as follows :
  169. * a) Action is triggered on all online CPUs, after the capability is
  170. * finalised, invoked within the stop_machine() context from
  171. * enable_cpu_capabilitie().
  172. *
  173. * b) Any late CPU, brought up after (1), the action is triggered via:
  174. *
  175. * check_local_cpu_capabilities() -> verify_local_cpu_capabilities()
  176. *
  177. * 5) Conflicts: Based on the state of the capability on a late CPU vs.
  178. * the system state, we could have the following combinations :
  179. *
  180. * x-----------------------------x
  181. * | Type | System | Late CPU |
  182. * |-----------------------------|
  183. * | a | y | n |
  184. * |-----------------------------|
  185. * | b | n | y |
  186. * x-----------------------------x
  187. *
  188. * Two separate flag bits are defined to indicate whether each kind of
  189. * conflict can be allowed:
  190. * ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU - Case(a) is allowed
  191. * ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU - Case(b) is allowed
  192. *
  193. * Case (a) is not permitted for a capability that the system requires
  194. * all CPUs to have in order for the capability to be enabled. This is
  195. * typical for capabilities that represent enhanced functionality.
  196. *
  197. * Case (b) is not permitted for a capability that must be enabled
  198. * during boot if any CPU in the system requires it in order to run
  199. * safely. This is typical for erratum work arounds that cannot be
  200. * enabled after the corresponding capability is finalised.
  201. *
  202. * In some non-typical cases either both (a) and (b), or neither,
  203. * should be permitted. This can be described by including neither
  204. * or both flags in the capability's type field.
  205. */
  206. /*
  207. * Decide how the capability is detected.
  208. * On any local CPU vs System wide vs the primary boot CPU
  209. */
  210. #define ARM64_CPUCAP_SCOPE_LOCAL_CPU ((u16)BIT(0))
  211. #define ARM64_CPUCAP_SCOPE_SYSTEM ((u16)BIT(1))
  212. /*
  213. * The capabilitiy is detected on the Boot CPU and is used by kernel
  214. * during early boot. i.e, the capability should be "detected" and
  215. * "enabled" as early as possibly on all booting CPUs.
  216. */
  217. #define ARM64_CPUCAP_SCOPE_BOOT_CPU ((u16)BIT(2))
  218. #define ARM64_CPUCAP_SCOPE_MASK \
  219. (ARM64_CPUCAP_SCOPE_SYSTEM | \
  220. ARM64_CPUCAP_SCOPE_LOCAL_CPU | \
  221. ARM64_CPUCAP_SCOPE_BOOT_CPU)
  222. #define SCOPE_SYSTEM ARM64_CPUCAP_SCOPE_SYSTEM
  223. #define SCOPE_LOCAL_CPU ARM64_CPUCAP_SCOPE_LOCAL_CPU
  224. #define SCOPE_BOOT_CPU ARM64_CPUCAP_SCOPE_BOOT_CPU
  225. #define SCOPE_ALL ARM64_CPUCAP_SCOPE_MASK
  226. /*
  227. * Is it permitted for a late CPU to have this capability when system
  228. * hasn't already enabled it ?
  229. */
  230. #define ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU ((u16)BIT(4))
  231. /* Is it safe for a late CPU to miss this capability when system has it */
  232. #define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU ((u16)BIT(5))
  233. /*
  234. * CPU errata workarounds that need to be enabled at boot time if one or
  235. * more CPUs in the system requires it. When one of these capabilities
  236. * has been enabled, it is safe to allow any CPU to boot that doesn't
  237. * require the workaround. However, it is not safe if a "late" CPU
  238. * requires a workaround and the system hasn't enabled it already.
  239. */
  240. #define ARM64_CPUCAP_LOCAL_CPU_ERRATUM \
  241. (ARM64_CPUCAP_SCOPE_LOCAL_CPU | ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
  242. /*
  243. * CPU feature detected at boot time based on system-wide value of a
  244. * feature. It is safe for a late CPU to have this feature even though
  245. * the system hasn't enabled it, although the featuer will not be used
  246. * by Linux in this case. If the system has enabled this feature already,
  247. * then every late CPU must have it.
  248. */
  249. #define ARM64_CPUCAP_SYSTEM_FEATURE \
  250. (ARM64_CPUCAP_SCOPE_SYSTEM | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
  251. /*
  252. * CPU feature detected at boot time based on feature of one or more CPUs.
  253. * All possible conflicts for a late CPU are ignored.
  254. */
  255. #define ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE \
  256. (ARM64_CPUCAP_SCOPE_LOCAL_CPU | \
  257. ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU | \
  258. ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
  259. /*
  260. * CPU feature detected at boot time, on one or more CPUs. A late CPU
  261. * is not allowed to have the capability when the system doesn't have it.
  262. * It is Ok for a late CPU to miss the feature.
  263. */
  264. #define ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE \
  265. (ARM64_CPUCAP_SCOPE_LOCAL_CPU | \
  266. ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
  267. /*
  268. * CPU feature used early in the boot based on the boot CPU. All secondary
  269. * CPUs must match the state of the capability as detected by the boot CPU.
  270. */
  271. #define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE ARM64_CPUCAP_SCOPE_BOOT_CPU
  272. struct arm64_cpu_capabilities {
  273. const char *desc;
  274. u16 capability;
  275. u16 type;
  276. bool (*matches)(const struct arm64_cpu_capabilities *caps, int scope);
  277. /*
  278. * Take the appropriate actions to enable this capability for this CPU.
  279. * For each successfully booted CPU, this method is called for each
  280. * globally detected capability.
  281. */
  282. void (*cpu_enable)(const struct arm64_cpu_capabilities *cap);
  283. union {
  284. struct { /* To be used for erratum handling only */
  285. struct midr_range midr_range;
  286. const struct arm64_midr_revidr {
  287. u32 midr_rv; /* revision/variant */
  288. u32 revidr_mask;
  289. } * const fixed_revs;
  290. };
  291. const struct midr_range *midr_range_list;
  292. struct { /* Feature register checking */
  293. u32 sys_reg;
  294. u8 field_pos;
  295. u8 min_field_value;
  296. u8 hwcap_type;
  297. bool sign;
  298. unsigned long hwcap;
  299. };
  300. /*
  301. * A list of "matches/cpu_enable" pair for the same
  302. * "capability" of the same "type" as described by the parent.
  303. * Only matches(), cpu_enable() and fields relevant to these
  304. * methods are significant in the list. The cpu_enable is
  305. * invoked only if the corresponding entry "matches()".
  306. * However, if a cpu_enable() method is associated
  307. * with multiple matches(), care should be taken that either
  308. * the match criteria are mutually exclusive, or that the
  309. * method is robust against being called multiple times.
  310. */
  311. const struct arm64_cpu_capabilities *match_list;
  312. };
  313. };
  314. static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)
  315. {
  316. return cap->type & ARM64_CPUCAP_SCOPE_MASK;
  317. }
  318. static inline bool
  319. cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
  320. {
  321. return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
  322. }
  323. static inline bool
  324. cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
  325. {
  326. return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
  327. }
  328. extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  329. extern struct static_key_false cpu_hwcap_keys[ARM64_NCAPS];
  330. extern struct static_key_false arm64_const_caps_ready;
  331. bool this_cpu_has_cap(unsigned int cap);
  332. static inline bool cpu_have_feature(unsigned int num)
  333. {
  334. return elf_hwcap & (1UL << num);
  335. }
  336. /* System capability check for constant caps */
  337. static inline bool __cpus_have_const_cap(int num)
  338. {
  339. if (num >= ARM64_NCAPS)
  340. return false;
  341. return static_branch_unlikely(&cpu_hwcap_keys[num]);
  342. }
  343. static inline bool cpus_have_cap(unsigned int num)
  344. {
  345. if (num >= ARM64_NCAPS)
  346. return false;
  347. return test_bit(num, cpu_hwcaps);
  348. }
  349. static inline bool cpus_have_const_cap(int num)
  350. {
  351. if (static_branch_likely(&arm64_const_caps_ready))
  352. return __cpus_have_const_cap(num);
  353. else
  354. return cpus_have_cap(num);
  355. }
  356. static inline void cpus_set_cap(unsigned int num)
  357. {
  358. if (num >= ARM64_NCAPS) {
  359. pr_warn("Attempt to set an illegal CPU capability (%d >= %d)\n",
  360. num, ARM64_NCAPS);
  361. } else {
  362. __set_bit(num, cpu_hwcaps);
  363. }
  364. }
  365. static inline int __attribute_const__
  366. cpuid_feature_extract_signed_field_width(u64 features, int field, int width)
  367. {
  368. return (s64)(features << (64 - width - field)) >> (64 - width);
  369. }
  370. static inline int __attribute_const__
  371. cpuid_feature_extract_signed_field(u64 features, int field)
  372. {
  373. return cpuid_feature_extract_signed_field_width(features, field, 4);
  374. }
  375. static inline unsigned int __attribute_const__
  376. cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
  377. {
  378. return (u64)(features << (64 - width - field)) >> (64 - width);
  379. }
  380. static inline unsigned int __attribute_const__
  381. cpuid_feature_extract_unsigned_field(u64 features, int field)
  382. {
  383. return cpuid_feature_extract_unsigned_field_width(features, field, 4);
  384. }
  385. static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
  386. {
  387. return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
  388. }
  389. static inline u64 arm64_ftr_reg_user_value(const struct arm64_ftr_reg *reg)
  390. {
  391. return (reg->user_val | (reg->sys_val & reg->user_mask));
  392. }
  393. static inline int __attribute_const__
  394. cpuid_feature_extract_field_width(u64 features, int field, int width, bool sign)
  395. {
  396. return (sign) ?
  397. cpuid_feature_extract_signed_field_width(features, field, width) :
  398. cpuid_feature_extract_unsigned_field_width(features, field, width);
  399. }
  400. static inline int __attribute_const__
  401. cpuid_feature_extract_field(u64 features, int field, bool sign)
  402. {
  403. return cpuid_feature_extract_field_width(features, field, 4, sign);
  404. }
  405. static inline s64 arm64_ftr_value(const struct arm64_ftr_bits *ftrp, u64 val)
  406. {
  407. return (s64)cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width, ftrp->sign);
  408. }
  409. static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
  410. {
  411. return cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL_SHIFT) == 0x1 ||
  412. cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL0_SHIFT) == 0x1;
  413. }
  414. static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
  415. {
  416. u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL0_SHIFT);
  417. return val == ID_AA64PFR0_EL0_32BIT_64BIT;
  418. }
  419. static inline bool id_aa64pfr0_sve(u64 pfr0)
  420. {
  421. u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_SVE_SHIFT);
  422. return val > 0;
  423. }
  424. void __init setup_cpu_features(void);
  425. void check_local_cpu_capabilities(void);
  426. u64 read_sanitised_ftr_reg(u32 id);
  427. static inline bool cpu_supports_mixed_endian_el0(void)
  428. {
  429. return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));
  430. }
  431. static inline bool system_supports_32bit_el0(void)
  432. {
  433. return cpus_have_const_cap(ARM64_HAS_32BIT_EL0);
  434. }
  435. static inline bool system_supports_mixed_endian_el0(void)
  436. {
  437. return id_aa64mmfr0_mixed_endian_el0(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1));
  438. }
  439. static inline bool system_supports_fpsimd(void)
  440. {
  441. return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD);
  442. }
  443. static inline bool system_uses_ttbr0_pan(void)
  444. {
  445. return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&
  446. !cpus_have_const_cap(ARM64_HAS_PAN);
  447. }
  448. static inline bool system_supports_sve(void)
  449. {
  450. return IS_ENABLED(CONFIG_ARM64_SVE) &&
  451. cpus_have_const_cap(ARM64_SVE);
  452. }
  453. #define ARM64_SSBD_UNKNOWN -1
  454. #define ARM64_SSBD_FORCE_DISABLE 0
  455. #define ARM64_SSBD_KERNEL 1
  456. #define ARM64_SSBD_FORCE_ENABLE 2
  457. #define ARM64_SSBD_MITIGATED 3
  458. static inline int arm64_get_ssbd_state(void)
  459. {
  460. #ifdef CONFIG_ARM64_SSBD
  461. extern int ssbd_state;
  462. return ssbd_state;
  463. #else
  464. return ARM64_SSBD_UNKNOWN;
  465. #endif
  466. }
  467. void arm64_set_ssbd_mitigation(bool state);
  468. #endif /* __ASSEMBLY__ */
  469. #endif