cpufeature.h 29 KB

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