cpufeature.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/hwcap.h>
  11. #include <asm/sysreg.h>
  12. /*
  13. * In the arm64 world (as in the ARM world), elf_hwcap is used both internally
  14. * in the kernel and for user space to keep track of which optional features
  15. * are supported by the current system. So let's map feature 'x' to HWCAP_x.
  16. * Note that HWCAP_x constants are bit fields so we need to take the log.
  17. */
  18. #define MAX_CPU_FEATURES (8 * sizeof(elf_hwcap))
  19. #define cpu_feature(x) ilog2(HWCAP_ ## x)
  20. #define ARM64_WORKAROUND_CLEAN_CACHE 0
  21. #define ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE 1
  22. #define ARM64_WORKAROUND_845719 2
  23. #define ARM64_HAS_SYSREG_GIC_CPUIF 3
  24. #define ARM64_HAS_PAN 4
  25. #define ARM64_HAS_LSE_ATOMICS 5
  26. #define ARM64_WORKAROUND_CAVIUM_23154 6
  27. #define ARM64_WORKAROUND_834220 7
  28. #define ARM64_WORKAROUND_CAVIUM_27456 8
  29. #define ARM64_HAS_32BIT_EL0 9
  30. #define ARM64_NCAPS 10
  31. #ifndef __ASSEMBLY__
  32. #include <linux/kernel.h>
  33. /* CPU feature register tracking */
  34. enum ftr_type {
  35. FTR_EXACT, /* Use a predefined safe value */
  36. FTR_LOWER_SAFE, /* Smaller value is safe */
  37. FTR_HIGHER_SAFE,/* Bigger value is safe */
  38. };
  39. #define FTR_STRICT true /* SANITY check strict matching required */
  40. #define FTR_NONSTRICT false /* SANITY check ignored */
  41. #define FTR_SIGNED true /* Value should be treated as signed */
  42. #define FTR_UNSIGNED false /* Value should be treated as unsigned */
  43. struct arm64_ftr_bits {
  44. bool sign; /* Value is signed ? */
  45. bool strict; /* CPU Sanity check: strict matching required ? */
  46. enum ftr_type type;
  47. u8 shift;
  48. u8 width;
  49. s64 safe_val; /* safe value for discrete features */
  50. };
  51. /*
  52. * @arm64_ftr_reg - Feature register
  53. * @strict_mask Bits which should match across all CPUs for sanity.
  54. * @sys_val Safe value across the CPUs (system view)
  55. */
  56. struct arm64_ftr_reg {
  57. u32 sys_id;
  58. const char *name;
  59. u64 strict_mask;
  60. u64 sys_val;
  61. struct arm64_ftr_bits *ftr_bits;
  62. };
  63. struct arm64_cpu_capabilities {
  64. const char *desc;
  65. u16 capability;
  66. bool (*matches)(const struct arm64_cpu_capabilities *);
  67. int (*enable)(void *); /* Called on all active CPUs */
  68. union {
  69. struct { /* To be used for erratum handling only */
  70. u32 midr_model;
  71. u32 midr_range_min, midr_range_max;
  72. };
  73. struct { /* Feature register checking */
  74. u32 sys_reg;
  75. int field_pos;
  76. int min_field_value;
  77. int hwcap_type;
  78. unsigned long hwcap;
  79. };
  80. };
  81. };
  82. extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  83. static inline bool cpu_have_feature(unsigned int num)
  84. {
  85. return elf_hwcap & (1UL << num);
  86. }
  87. static inline bool cpus_have_cap(unsigned int num)
  88. {
  89. if (num >= ARM64_NCAPS)
  90. return false;
  91. return test_bit(num, cpu_hwcaps);
  92. }
  93. static inline void cpus_set_cap(unsigned int num)
  94. {
  95. if (num >= ARM64_NCAPS)
  96. pr_warn("Attempt to set an illegal CPU capability (%d >= %d)\n",
  97. num, ARM64_NCAPS);
  98. else
  99. __set_bit(num, cpu_hwcaps);
  100. }
  101. static inline int __attribute_const__
  102. cpuid_feature_extract_field_width(u64 features, int field, int width)
  103. {
  104. return (s64)(features << (64 - width - field)) >> (64 - width);
  105. }
  106. static inline int __attribute_const__
  107. cpuid_feature_extract_field(u64 features, int field)
  108. {
  109. return cpuid_feature_extract_field_width(features, field, 4);
  110. }
  111. static inline unsigned int __attribute_const__
  112. cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
  113. {
  114. return (u64)(features << (64 - width - field)) >> (64 - width);
  115. }
  116. static inline unsigned int __attribute_const__
  117. cpuid_feature_extract_unsigned_field(u64 features, int field)
  118. {
  119. return cpuid_feature_extract_unsigned_field_width(features, field, 4);
  120. }
  121. static inline u64 arm64_ftr_mask(struct arm64_ftr_bits *ftrp)
  122. {
  123. return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
  124. }
  125. static inline s64 arm64_ftr_value(struct arm64_ftr_bits *ftrp, u64 val)
  126. {
  127. return ftrp->sign ?
  128. cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width) :
  129. cpuid_feature_extract_unsigned_field_width(val, ftrp->shift, ftrp->width);
  130. }
  131. static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
  132. {
  133. return cpuid_feature_extract_field(mmfr0, ID_AA64MMFR0_BIGENDEL_SHIFT) == 0x1 ||
  134. cpuid_feature_extract_field(mmfr0, ID_AA64MMFR0_BIGENDEL0_SHIFT) == 0x1;
  135. }
  136. void __init setup_cpu_features(void);
  137. void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
  138. const char *info);
  139. void check_local_cpu_errata(void);
  140. #ifdef CONFIG_HOTPLUG_CPU
  141. void verify_local_cpu_capabilities(void);
  142. #else
  143. static inline void verify_local_cpu_capabilities(void)
  144. {
  145. }
  146. #endif
  147. u64 read_system_reg(u32 id);
  148. static inline bool cpu_supports_mixed_endian_el0(void)
  149. {
  150. return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));
  151. }
  152. static inline bool system_supports_32bit_el0(void)
  153. {
  154. return cpus_have_cap(ARM64_HAS_32BIT_EL0);
  155. }
  156. static inline bool system_supports_mixed_endian_el0(void)
  157. {
  158. return id_aa64mmfr0_mixed_endian_el0(read_system_reg(SYS_ID_AA64MMFR0_EL1));
  159. }
  160. #endif /* __ASSEMBLY__ */
  161. #endif