cpufeature.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Contains CPU feature definitions
  3. *
  4. * Copyright (C) 2015 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) "CPU features: " fmt
  19. #include <linux/bsearch.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/sort.h>
  22. #include <linux/stop_machine.h>
  23. #include <linux/types.h>
  24. #include <asm/cpu.h>
  25. #include <asm/cpufeature.h>
  26. #include <asm/cpu_ops.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/processor.h>
  29. #include <asm/sysreg.h>
  30. #include <asm/virt.h>
  31. unsigned long elf_hwcap __read_mostly;
  32. EXPORT_SYMBOL_GPL(elf_hwcap);
  33. #ifdef CONFIG_COMPAT
  34. #define COMPAT_ELF_HWCAP_DEFAULT \
  35. (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  36. COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  37. COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
  38. COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
  39. COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
  40. COMPAT_HWCAP_LPAE)
  41. unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  42. unsigned int compat_elf_hwcap2 __read_mostly;
  43. #endif
  44. DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  45. EXPORT_SYMBOL(cpu_hwcaps);
  46. DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
  47. EXPORT_SYMBOL(cpu_hwcap_keys);
  48. #define __ARM64_FTR_BITS(SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  49. { \
  50. .sign = SIGNED, \
  51. .strict = STRICT, \
  52. .type = TYPE, \
  53. .shift = SHIFT, \
  54. .width = WIDTH, \
  55. .safe_val = SAFE_VAL, \
  56. }
  57. /* Define a feature with unsigned values */
  58. #define ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  59. __ARM64_FTR_BITS(FTR_UNSIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
  60. /* Define a feature with a signed value */
  61. #define S_ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  62. __ARM64_FTR_BITS(FTR_SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
  63. #define ARM64_FTR_END \
  64. { \
  65. .width = 0, \
  66. }
  67. /* meta feature for alternatives */
  68. static bool __maybe_unused
  69. cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
  70. static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
  71. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
  72. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
  73. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0),
  74. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
  75. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
  76. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
  77. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
  78. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
  79. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
  80. ARM64_FTR_END,
  81. };
  82. static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
  83. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
  84. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0),
  85. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_GIC_SHIFT, 4, 0),
  86. S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
  87. S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
  88. /* Linux doesn't care about the EL3 */
  89. ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64PFR0_EL3_SHIFT, 4, 0),
  90. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL2_SHIFT, 4, 0),
  91. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
  92. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
  93. ARM64_FTR_END,
  94. };
  95. static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
  96. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
  97. S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
  98. S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
  99. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
  100. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
  101. /* Linux shouldn't care about secure memory */
  102. ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
  103. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
  104. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
  105. /*
  106. * Differing PARange is fine as long as all peripherals and memory are mapped
  107. * within the minimum PARange of all CPUs
  108. */
  109. ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
  110. ARM64_FTR_END,
  111. };
  112. static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
  113. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
  114. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
  115. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
  116. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
  117. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
  118. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
  119. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
  120. ARM64_FTR_END,
  121. };
  122. static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
  123. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
  124. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
  125. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
  126. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
  127. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
  128. ARM64_FTR_END,
  129. };
  130. static const struct arm64_ftr_bits ftr_ctr[] = {
  131. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RAO */
  132. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 3, 0),
  133. ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_SAFE, 24, 4, 0), /* CWG */
  134. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0), /* ERG */
  135. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 1), /* DminLine */
  136. /*
  137. * Linux can handle differing I-cache policies. Userspace JITs will
  138. * make use of *minLine.
  139. * If we have differing I-cache policies, report it as the weakest - AIVIVT.
  140. */
  141. ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_AIVIVT), /* L1Ip */
  142. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 10, 0), /* RAZ */
  143. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* IminLine */
  144. ARM64_FTR_END,
  145. };
  146. struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
  147. .name = "SYS_CTR_EL0",
  148. .ftr_bits = ftr_ctr
  149. };
  150. static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
  151. S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0xf), /* InnerShr */
  152. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0), /* FCSE */
  153. ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
  154. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 4, 0), /* TCM */
  155. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* ShareLvl */
  156. S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0xf), /* OuterShr */
  157. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* PMSA */
  158. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* VMSA */
  159. ARM64_FTR_END,
  160. };
  161. static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
  162. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
  163. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
  164. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
  165. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
  166. S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
  167. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
  168. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
  169. ARM64_FTR_END,
  170. };
  171. static const struct arm64_ftr_bits ftr_mvfr2[] = {
  172. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
  173. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* FPMisc */
  174. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* SIMDMisc */
  175. ARM64_FTR_END,
  176. };
  177. static const struct arm64_ftr_bits ftr_dczid[] = {
  178. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 5, 27, 0), /* RAZ */
  179. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
  180. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
  181. ARM64_FTR_END,
  182. };
  183. static const struct arm64_ftr_bits ftr_id_isar5[] = {
  184. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_RDM_SHIFT, 4, 0),
  185. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 20, 4, 0), /* RAZ */
  186. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_CRC32_SHIFT, 4, 0),
  187. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA2_SHIFT, 4, 0),
  188. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA1_SHIFT, 4, 0),
  189. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_AES_SHIFT, 4, 0),
  190. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SEVL_SHIFT, 4, 0),
  191. ARM64_FTR_END,
  192. };
  193. static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
  194. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
  195. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* ac2 */
  196. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
  197. ARM64_FTR_END,
  198. };
  199. static const struct arm64_ftr_bits ftr_id_pfr0[] = {
  200. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 16, 0), /* RAZ */
  201. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* State3 */
  202. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0), /* State2 */
  203. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* State1 */
  204. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* State0 */
  205. ARM64_FTR_END,
  206. };
  207. static const struct arm64_ftr_bits ftr_id_dfr0[] = {
  208. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
  209. S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
  210. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
  211. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
  212. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
  213. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
  214. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
  215. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
  216. ARM64_FTR_END,
  217. };
  218. /*
  219. * Common ftr bits for a 32bit register with all hidden, strict
  220. * attributes, with 4bit feature fields and a default safe value of
  221. * 0. Covers the following 32bit registers:
  222. * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
  223. */
  224. static const struct arm64_ftr_bits ftr_generic_32bits[] = {
  225. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
  226. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
  227. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
  228. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
  229. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
  230. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
  231. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
  232. ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
  233. ARM64_FTR_END,
  234. };
  235. static const struct arm64_ftr_bits ftr_generic[] = {
  236. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
  237. ARM64_FTR_END,
  238. };
  239. static const struct arm64_ftr_bits ftr_generic32[] = {
  240. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 32, 0),
  241. ARM64_FTR_END,
  242. };
  243. static const struct arm64_ftr_bits ftr_aa64raz[] = {
  244. ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
  245. ARM64_FTR_END,
  246. };
  247. #define ARM64_FTR_REG(id, table) { \
  248. .sys_id = id, \
  249. .reg = &(struct arm64_ftr_reg){ \
  250. .name = #id, \
  251. .ftr_bits = &((table)[0]), \
  252. }}
  253. static const struct __ftr_reg_entry {
  254. u32 sys_id;
  255. struct arm64_ftr_reg *reg;
  256. } arm64_ftr_regs[] = {
  257. /* Op1 = 0, CRn = 0, CRm = 1 */
  258. ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
  259. ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
  260. ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
  261. ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
  262. ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
  263. ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
  264. ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
  265. /* Op1 = 0, CRn = 0, CRm = 2 */
  266. ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
  267. ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
  268. ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
  269. ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
  270. ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
  271. ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
  272. ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
  273. /* Op1 = 0, CRn = 0, CRm = 3 */
  274. ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
  275. ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
  276. ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
  277. /* Op1 = 0, CRn = 0, CRm = 4 */
  278. ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
  279. ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_aa64raz),
  280. /* Op1 = 0, CRn = 0, CRm = 5 */
  281. ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
  282. ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_generic),
  283. /* Op1 = 0, CRn = 0, CRm = 6 */
  284. ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
  285. ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_aa64raz),
  286. /* Op1 = 0, CRn = 0, CRm = 7 */
  287. ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
  288. ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
  289. ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
  290. /* Op1 = 3, CRn = 0, CRm = 0 */
  291. { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
  292. ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
  293. /* Op1 = 3, CRn = 14, CRm = 0 */
  294. ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_generic32),
  295. };
  296. static int search_cmp_ftr_reg(const void *id, const void *regp)
  297. {
  298. return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
  299. }
  300. /*
  301. * get_arm64_ftr_reg - Lookup a feature register entry using its
  302. * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
  303. * ascending order of sys_id , we use binary search to find a matching
  304. * entry.
  305. *
  306. * returns - Upon success, matching ftr_reg entry for id.
  307. * - NULL on failure. It is upto the caller to decide
  308. * the impact of a failure.
  309. */
  310. static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
  311. {
  312. const struct __ftr_reg_entry *ret;
  313. ret = bsearch((const void *)(unsigned long)sys_id,
  314. arm64_ftr_regs,
  315. ARRAY_SIZE(arm64_ftr_regs),
  316. sizeof(arm64_ftr_regs[0]),
  317. search_cmp_ftr_reg);
  318. if (ret)
  319. return ret->reg;
  320. return NULL;
  321. }
  322. static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
  323. s64 ftr_val)
  324. {
  325. u64 mask = arm64_ftr_mask(ftrp);
  326. reg &= ~mask;
  327. reg |= (ftr_val << ftrp->shift) & mask;
  328. return reg;
  329. }
  330. static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
  331. s64 cur)
  332. {
  333. s64 ret = 0;
  334. switch (ftrp->type) {
  335. case FTR_EXACT:
  336. ret = ftrp->safe_val;
  337. break;
  338. case FTR_LOWER_SAFE:
  339. ret = new < cur ? new : cur;
  340. break;
  341. case FTR_HIGHER_SAFE:
  342. ret = new > cur ? new : cur;
  343. break;
  344. default:
  345. BUG();
  346. }
  347. return ret;
  348. }
  349. static void __init sort_ftr_regs(void)
  350. {
  351. int i;
  352. /* Check that the array is sorted so that we can do the binary search */
  353. for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
  354. BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
  355. }
  356. /*
  357. * Initialise the CPU feature register from Boot CPU values.
  358. * Also initiliases the strict_mask for the register.
  359. */
  360. static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
  361. {
  362. u64 val = 0;
  363. u64 strict_mask = ~0x0ULL;
  364. const struct arm64_ftr_bits *ftrp;
  365. struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
  366. BUG_ON(!reg);
  367. for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
  368. s64 ftr_new = arm64_ftr_value(ftrp, new);
  369. val = arm64_ftr_set_value(ftrp, val, ftr_new);
  370. if (!ftrp->strict)
  371. strict_mask &= ~arm64_ftr_mask(ftrp);
  372. }
  373. reg->sys_val = val;
  374. reg->strict_mask = strict_mask;
  375. }
  376. void __init init_cpu_features(struct cpuinfo_arm64 *info)
  377. {
  378. /* Before we start using the tables, make sure it is sorted */
  379. sort_ftr_regs();
  380. init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
  381. init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
  382. init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
  383. init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
  384. init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
  385. init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
  386. init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
  387. init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
  388. init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
  389. init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
  390. init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
  391. init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
  392. if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
  393. init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
  394. init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
  395. init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
  396. init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
  397. init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
  398. init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
  399. init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
  400. init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
  401. init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
  402. init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
  403. init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
  404. init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
  405. init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
  406. init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
  407. init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
  408. init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
  409. }
  410. }
  411. static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
  412. {
  413. const struct arm64_ftr_bits *ftrp;
  414. for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
  415. s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
  416. s64 ftr_new = arm64_ftr_value(ftrp, new);
  417. if (ftr_cur == ftr_new)
  418. continue;
  419. /* Find a safe value */
  420. ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
  421. reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
  422. }
  423. }
  424. static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
  425. {
  426. struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
  427. BUG_ON(!regp);
  428. update_cpu_ftr_reg(regp, val);
  429. if ((boot & regp->strict_mask) == (val & regp->strict_mask))
  430. return 0;
  431. pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
  432. regp->name, boot, cpu, val);
  433. return 1;
  434. }
  435. /*
  436. * Update system wide CPU feature registers with the values from a
  437. * non-boot CPU. Also performs SANITY checks to make sure that there
  438. * aren't any insane variations from that of the boot CPU.
  439. */
  440. void update_cpu_features(int cpu,
  441. struct cpuinfo_arm64 *info,
  442. struct cpuinfo_arm64 *boot)
  443. {
  444. int taint = 0;
  445. /*
  446. * The kernel can handle differing I-cache policies, but otherwise
  447. * caches should look identical. Userspace JITs will make use of
  448. * *minLine.
  449. */
  450. taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
  451. info->reg_ctr, boot->reg_ctr);
  452. /*
  453. * Userspace may perform DC ZVA instructions. Mismatched block sizes
  454. * could result in too much or too little memory being zeroed if a
  455. * process is preempted and migrated between CPUs.
  456. */
  457. taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
  458. info->reg_dczid, boot->reg_dczid);
  459. /* If different, timekeeping will be broken (especially with KVM) */
  460. taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
  461. info->reg_cntfrq, boot->reg_cntfrq);
  462. /*
  463. * The kernel uses self-hosted debug features and expects CPUs to
  464. * support identical debug features. We presently need CTX_CMPs, WRPs,
  465. * and BRPs to be identical.
  466. * ID_AA64DFR1 is currently RES0.
  467. */
  468. taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
  469. info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
  470. taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
  471. info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
  472. /*
  473. * Even in big.LITTLE, processors should be identical instruction-set
  474. * wise.
  475. */
  476. taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
  477. info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
  478. taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
  479. info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
  480. /*
  481. * Differing PARange support is fine as long as all peripherals and
  482. * memory are mapped within the minimum PARange of all CPUs.
  483. * Linux should not care about secure memory.
  484. */
  485. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
  486. info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
  487. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
  488. info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
  489. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
  490. info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
  491. /*
  492. * EL3 is not our concern.
  493. * ID_AA64PFR1 is currently RES0.
  494. */
  495. taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
  496. info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
  497. taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
  498. info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
  499. /*
  500. * If we have AArch32, we care about 32-bit features for compat.
  501. * If the system doesn't support AArch32, don't update them.
  502. */
  503. if (id_aa64pfr0_32bit_el0(read_system_reg(SYS_ID_AA64PFR0_EL1)) &&
  504. id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
  505. taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
  506. info->reg_id_dfr0, boot->reg_id_dfr0);
  507. taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
  508. info->reg_id_isar0, boot->reg_id_isar0);
  509. taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
  510. info->reg_id_isar1, boot->reg_id_isar1);
  511. taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
  512. info->reg_id_isar2, boot->reg_id_isar2);
  513. taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
  514. info->reg_id_isar3, boot->reg_id_isar3);
  515. taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
  516. info->reg_id_isar4, boot->reg_id_isar4);
  517. taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
  518. info->reg_id_isar5, boot->reg_id_isar5);
  519. /*
  520. * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
  521. * ACTLR formats could differ across CPUs and therefore would have to
  522. * be trapped for virtualization anyway.
  523. */
  524. taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
  525. info->reg_id_mmfr0, boot->reg_id_mmfr0);
  526. taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
  527. info->reg_id_mmfr1, boot->reg_id_mmfr1);
  528. taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
  529. info->reg_id_mmfr2, boot->reg_id_mmfr2);
  530. taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
  531. info->reg_id_mmfr3, boot->reg_id_mmfr3);
  532. taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
  533. info->reg_id_pfr0, boot->reg_id_pfr0);
  534. taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
  535. info->reg_id_pfr1, boot->reg_id_pfr1);
  536. taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
  537. info->reg_mvfr0, boot->reg_mvfr0);
  538. taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
  539. info->reg_mvfr1, boot->reg_mvfr1);
  540. taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
  541. info->reg_mvfr2, boot->reg_mvfr2);
  542. }
  543. /*
  544. * Mismatched CPU features are a recipe for disaster. Don't even
  545. * pretend to support them.
  546. */
  547. WARN_TAINT_ONCE(taint, TAINT_CPU_OUT_OF_SPEC,
  548. "Unsupported CPU feature variation.\n");
  549. }
  550. u64 read_system_reg(u32 id)
  551. {
  552. struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
  553. /* We shouldn't get a request for an unsupported register */
  554. BUG_ON(!regp);
  555. return regp->sys_val;
  556. }
  557. /*
  558. * __raw_read_system_reg() - Used by a STARTING cpu before cpuinfo is populated.
  559. * Read the system register on the current CPU
  560. */
  561. static u64 __raw_read_system_reg(u32 sys_id)
  562. {
  563. switch (sys_id) {
  564. case SYS_ID_PFR0_EL1: return read_cpuid(ID_PFR0_EL1);
  565. case SYS_ID_PFR1_EL1: return read_cpuid(ID_PFR1_EL1);
  566. case SYS_ID_DFR0_EL1: return read_cpuid(ID_DFR0_EL1);
  567. case SYS_ID_MMFR0_EL1: return read_cpuid(ID_MMFR0_EL1);
  568. case SYS_ID_MMFR1_EL1: return read_cpuid(ID_MMFR1_EL1);
  569. case SYS_ID_MMFR2_EL1: return read_cpuid(ID_MMFR2_EL1);
  570. case SYS_ID_MMFR3_EL1: return read_cpuid(ID_MMFR3_EL1);
  571. case SYS_ID_ISAR0_EL1: return read_cpuid(ID_ISAR0_EL1);
  572. case SYS_ID_ISAR1_EL1: return read_cpuid(ID_ISAR1_EL1);
  573. case SYS_ID_ISAR2_EL1: return read_cpuid(ID_ISAR2_EL1);
  574. case SYS_ID_ISAR3_EL1: return read_cpuid(ID_ISAR3_EL1);
  575. case SYS_ID_ISAR4_EL1: return read_cpuid(ID_ISAR4_EL1);
  576. case SYS_ID_ISAR5_EL1: return read_cpuid(ID_ISAR5_EL1);
  577. case SYS_MVFR0_EL1: return read_cpuid(MVFR0_EL1);
  578. case SYS_MVFR1_EL1: return read_cpuid(MVFR1_EL1);
  579. case SYS_MVFR2_EL1: return read_cpuid(MVFR2_EL1);
  580. case SYS_ID_AA64PFR0_EL1: return read_cpuid(ID_AA64PFR0_EL1);
  581. case SYS_ID_AA64PFR1_EL1: return read_cpuid(ID_AA64PFR1_EL1);
  582. case SYS_ID_AA64DFR0_EL1: return read_cpuid(ID_AA64DFR0_EL1);
  583. case SYS_ID_AA64DFR1_EL1: return read_cpuid(ID_AA64DFR1_EL1);
  584. case SYS_ID_AA64MMFR0_EL1: return read_cpuid(ID_AA64MMFR0_EL1);
  585. case SYS_ID_AA64MMFR1_EL1: return read_cpuid(ID_AA64MMFR1_EL1);
  586. case SYS_ID_AA64MMFR2_EL1: return read_cpuid(ID_AA64MMFR2_EL1);
  587. case SYS_ID_AA64ISAR0_EL1: return read_cpuid(ID_AA64ISAR0_EL1);
  588. case SYS_ID_AA64ISAR1_EL1: return read_cpuid(ID_AA64ISAR1_EL1);
  589. case SYS_CNTFRQ_EL0: return read_cpuid(CNTFRQ_EL0);
  590. case SYS_CTR_EL0: return read_cpuid(CTR_EL0);
  591. case SYS_DCZID_EL0: return read_cpuid(DCZID_EL0);
  592. default:
  593. BUG();
  594. return 0;
  595. }
  596. }
  597. #include <linux/irqchip/arm-gic-v3.h>
  598. static bool
  599. feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
  600. {
  601. int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
  602. return val >= entry->min_field_value;
  603. }
  604. static bool
  605. has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
  606. {
  607. u64 val;
  608. WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
  609. if (scope == SCOPE_SYSTEM)
  610. val = read_system_reg(entry->sys_reg);
  611. else
  612. val = __raw_read_system_reg(entry->sys_reg);
  613. return feature_matches(val, entry);
  614. }
  615. static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
  616. {
  617. bool has_sre;
  618. if (!has_cpuid_feature(entry, scope))
  619. return false;
  620. has_sre = gic_enable_sre();
  621. if (!has_sre)
  622. pr_warn_once("%s present but disabled by higher exception level\n",
  623. entry->desc);
  624. return has_sre;
  625. }
  626. static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
  627. {
  628. u32 midr = read_cpuid_id();
  629. u32 rv_min, rv_max;
  630. /* Cavium ThunderX pass 1.x and 2.x */
  631. rv_min = 0;
  632. rv_max = (1 << MIDR_VARIANT_SHIFT) | MIDR_REVISION_MASK;
  633. return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX, rv_min, rv_max);
  634. }
  635. static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
  636. {
  637. return is_kernel_in_hyp_mode();
  638. }
  639. static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
  640. int __unused)
  641. {
  642. phys_addr_t idmap_addr = virt_to_phys(__hyp_idmap_text_start);
  643. /*
  644. * Activate the lower HYP offset only if:
  645. * - the idmap doesn't clash with it,
  646. * - the kernel is not running at EL2.
  647. */
  648. return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
  649. }
  650. static const struct arm64_cpu_capabilities arm64_features[] = {
  651. {
  652. .desc = "GIC system register CPU interface",
  653. .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
  654. .def_scope = SCOPE_SYSTEM,
  655. .matches = has_useable_gicv3_cpuif,
  656. .sys_reg = SYS_ID_AA64PFR0_EL1,
  657. .field_pos = ID_AA64PFR0_GIC_SHIFT,
  658. .sign = FTR_UNSIGNED,
  659. .min_field_value = 1,
  660. },
  661. #ifdef CONFIG_ARM64_PAN
  662. {
  663. .desc = "Privileged Access Never",
  664. .capability = ARM64_HAS_PAN,
  665. .def_scope = SCOPE_SYSTEM,
  666. .matches = has_cpuid_feature,
  667. .sys_reg = SYS_ID_AA64MMFR1_EL1,
  668. .field_pos = ID_AA64MMFR1_PAN_SHIFT,
  669. .sign = FTR_UNSIGNED,
  670. .min_field_value = 1,
  671. .enable = cpu_enable_pan,
  672. },
  673. #endif /* CONFIG_ARM64_PAN */
  674. #if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
  675. {
  676. .desc = "LSE atomic instructions",
  677. .capability = ARM64_HAS_LSE_ATOMICS,
  678. .def_scope = SCOPE_SYSTEM,
  679. .matches = has_cpuid_feature,
  680. .sys_reg = SYS_ID_AA64ISAR0_EL1,
  681. .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
  682. .sign = FTR_UNSIGNED,
  683. .min_field_value = 2,
  684. },
  685. #endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
  686. {
  687. .desc = "Software prefetching using PRFM",
  688. .capability = ARM64_HAS_NO_HW_PREFETCH,
  689. .def_scope = SCOPE_SYSTEM,
  690. .matches = has_no_hw_prefetch,
  691. },
  692. #ifdef CONFIG_ARM64_UAO
  693. {
  694. .desc = "User Access Override",
  695. .capability = ARM64_HAS_UAO,
  696. .def_scope = SCOPE_SYSTEM,
  697. .matches = has_cpuid_feature,
  698. .sys_reg = SYS_ID_AA64MMFR2_EL1,
  699. .field_pos = ID_AA64MMFR2_UAO_SHIFT,
  700. .min_field_value = 1,
  701. .enable = cpu_enable_uao,
  702. },
  703. #endif /* CONFIG_ARM64_UAO */
  704. #ifdef CONFIG_ARM64_PAN
  705. {
  706. .capability = ARM64_ALT_PAN_NOT_UAO,
  707. .def_scope = SCOPE_SYSTEM,
  708. .matches = cpufeature_pan_not_uao,
  709. },
  710. #endif /* CONFIG_ARM64_PAN */
  711. {
  712. .desc = "Virtualization Host Extensions",
  713. .capability = ARM64_HAS_VIRT_HOST_EXTN,
  714. .def_scope = SCOPE_SYSTEM,
  715. .matches = runs_at_el2,
  716. },
  717. {
  718. .desc = "32-bit EL0 Support",
  719. .capability = ARM64_HAS_32BIT_EL0,
  720. .def_scope = SCOPE_SYSTEM,
  721. .matches = has_cpuid_feature,
  722. .sys_reg = SYS_ID_AA64PFR0_EL1,
  723. .sign = FTR_UNSIGNED,
  724. .field_pos = ID_AA64PFR0_EL0_SHIFT,
  725. .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
  726. },
  727. {
  728. .desc = "Reduced HYP mapping offset",
  729. .capability = ARM64_HYP_OFFSET_LOW,
  730. .def_scope = SCOPE_SYSTEM,
  731. .matches = hyp_offset_low,
  732. },
  733. {},
  734. };
  735. #define HWCAP_CAP(reg, field, s, min_value, type, cap) \
  736. { \
  737. .desc = #cap, \
  738. .def_scope = SCOPE_SYSTEM, \
  739. .matches = has_cpuid_feature, \
  740. .sys_reg = reg, \
  741. .field_pos = field, \
  742. .sign = s, \
  743. .min_field_value = min_value, \
  744. .hwcap_type = type, \
  745. .hwcap = cap, \
  746. }
  747. static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
  748. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
  749. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
  750. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
  751. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
  752. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
  753. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
  754. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_FP),
  755. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_FPHP),
  756. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_ASIMD),
  757. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_ASIMDHP),
  758. {},
  759. };
  760. static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
  761. #ifdef CONFIG_COMPAT
  762. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
  763. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
  764. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
  765. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
  766. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
  767. #endif
  768. {},
  769. };
  770. static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
  771. {
  772. switch (cap->hwcap_type) {
  773. case CAP_HWCAP:
  774. elf_hwcap |= cap->hwcap;
  775. break;
  776. #ifdef CONFIG_COMPAT
  777. case CAP_COMPAT_HWCAP:
  778. compat_elf_hwcap |= (u32)cap->hwcap;
  779. break;
  780. case CAP_COMPAT_HWCAP2:
  781. compat_elf_hwcap2 |= (u32)cap->hwcap;
  782. break;
  783. #endif
  784. default:
  785. WARN_ON(1);
  786. break;
  787. }
  788. }
  789. /* Check if we have a particular HWCAP enabled */
  790. static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
  791. {
  792. bool rc;
  793. switch (cap->hwcap_type) {
  794. case CAP_HWCAP:
  795. rc = (elf_hwcap & cap->hwcap) != 0;
  796. break;
  797. #ifdef CONFIG_COMPAT
  798. case CAP_COMPAT_HWCAP:
  799. rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
  800. break;
  801. case CAP_COMPAT_HWCAP2:
  802. rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
  803. break;
  804. #endif
  805. default:
  806. WARN_ON(1);
  807. rc = false;
  808. }
  809. return rc;
  810. }
  811. static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
  812. {
  813. for (; hwcaps->matches; hwcaps++)
  814. if (hwcaps->matches(hwcaps, hwcaps->def_scope))
  815. cap_set_elf_hwcap(hwcaps);
  816. }
  817. void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
  818. const char *info)
  819. {
  820. for (; caps->matches; caps++) {
  821. if (!caps->matches(caps, caps->def_scope))
  822. continue;
  823. if (!cpus_have_cap(caps->capability) && caps->desc)
  824. pr_info("%s %s\n", info, caps->desc);
  825. cpus_set_cap(caps->capability);
  826. }
  827. }
  828. /*
  829. * Run through the enabled capabilities and enable() it on all active
  830. * CPUs
  831. */
  832. void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps)
  833. {
  834. for (; caps->matches; caps++)
  835. if (caps->enable && cpus_have_cap(caps->capability))
  836. /*
  837. * Use stop_machine() as it schedules the work allowing
  838. * us to modify PSTATE, instead of on_each_cpu() which
  839. * uses an IPI, giving us a PSTATE that disappears when
  840. * we return.
  841. */
  842. stop_machine(caps->enable, NULL, cpu_online_mask);
  843. }
  844. /*
  845. * Flag to indicate if we have computed the system wide
  846. * capabilities based on the boot time active CPUs. This
  847. * will be used to determine if a new booting CPU should
  848. * go through the verification process to make sure that it
  849. * supports the system capabilities, without using a hotplug
  850. * notifier.
  851. */
  852. static bool sys_caps_initialised;
  853. static inline void set_sys_caps_initialised(void)
  854. {
  855. sys_caps_initialised = true;
  856. }
  857. /*
  858. * Check for CPU features that are used in early boot
  859. * based on the Boot CPU value.
  860. */
  861. static void check_early_cpu_features(void)
  862. {
  863. verify_cpu_run_el();
  864. verify_cpu_asid_bits();
  865. }
  866. static void
  867. verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
  868. {
  869. for (; caps->matches; caps++)
  870. if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
  871. pr_crit("CPU%d: missing HWCAP: %s\n",
  872. smp_processor_id(), caps->desc);
  873. cpu_die_early();
  874. }
  875. }
  876. static void
  877. verify_local_cpu_features(const struct arm64_cpu_capabilities *caps)
  878. {
  879. for (; caps->matches; caps++) {
  880. if (!cpus_have_cap(caps->capability))
  881. continue;
  882. /*
  883. * If the new CPU misses an advertised feature, we cannot proceed
  884. * further, park the cpu.
  885. */
  886. if (!caps->matches(caps, SCOPE_LOCAL_CPU)) {
  887. pr_crit("CPU%d: missing feature: %s\n",
  888. smp_processor_id(), caps->desc);
  889. cpu_die_early();
  890. }
  891. if (caps->enable)
  892. caps->enable(NULL);
  893. }
  894. }
  895. /*
  896. * Run through the enabled system capabilities and enable() it on this CPU.
  897. * The capabilities were decided based on the available CPUs at the boot time.
  898. * Any new CPU should match the system wide status of the capability. If the
  899. * new CPU doesn't have a capability which the system now has enabled, we
  900. * cannot do anything to fix it up and could cause unexpected failures. So
  901. * we park the CPU.
  902. */
  903. static void verify_local_cpu_capabilities(void)
  904. {
  905. verify_local_cpu_errata_workarounds();
  906. verify_local_cpu_features(arm64_features);
  907. verify_local_elf_hwcaps(arm64_elf_hwcaps);
  908. if (system_supports_32bit_el0())
  909. verify_local_elf_hwcaps(compat_elf_hwcaps);
  910. }
  911. void check_local_cpu_capabilities(void)
  912. {
  913. /*
  914. * All secondary CPUs should conform to the early CPU features
  915. * in use by the kernel based on boot CPU.
  916. */
  917. check_early_cpu_features();
  918. /*
  919. * If we haven't finalised the system capabilities, this CPU gets
  920. * a chance to update the errata work arounds.
  921. * Otherwise, this CPU should verify that it has all the system
  922. * advertised capabilities.
  923. */
  924. if (!sys_caps_initialised)
  925. update_cpu_errata_workarounds();
  926. else
  927. verify_local_cpu_capabilities();
  928. }
  929. static void __init setup_feature_capabilities(void)
  930. {
  931. update_cpu_capabilities(arm64_features, "detected feature:");
  932. enable_cpu_capabilities(arm64_features);
  933. }
  934. /*
  935. * Check if the current CPU has a given feature capability.
  936. * Should be called from non-preemptible context.
  937. */
  938. bool this_cpu_has_cap(unsigned int cap)
  939. {
  940. const struct arm64_cpu_capabilities *caps;
  941. if (WARN_ON(preemptible()))
  942. return false;
  943. for (caps = arm64_features; caps->desc; caps++)
  944. if (caps->capability == cap && caps->matches)
  945. return caps->matches(caps, SCOPE_LOCAL_CPU);
  946. return false;
  947. }
  948. void __init setup_cpu_features(void)
  949. {
  950. u32 cwg;
  951. int cls;
  952. /* Set the CPU feature capabilies */
  953. setup_feature_capabilities();
  954. enable_errata_workarounds();
  955. setup_elf_hwcaps(arm64_elf_hwcaps);
  956. if (system_supports_32bit_el0())
  957. setup_elf_hwcaps(compat_elf_hwcaps);
  958. /* Advertise that we have computed the system capabilities */
  959. set_sys_caps_initialised();
  960. /*
  961. * Check for sane CTR_EL0.CWG value.
  962. */
  963. cwg = cache_type_cwg();
  964. cls = cache_line_size();
  965. if (!cwg)
  966. pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
  967. cls);
  968. if (L1_CACHE_BYTES < cls)
  969. pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
  970. L1_CACHE_BYTES, cls);
  971. }
  972. static bool __maybe_unused
  973. cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
  974. {
  975. return (cpus_have_cap(ARM64_HAS_PAN) && !cpus_have_cap(ARM64_HAS_UAO));
  976. }