cpu_errata.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Contains CPU specific errata definitions
  3. *
  4. * Copyright (C) 2014 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. #include <linux/types.h>
  19. #include <asm/cpu.h>
  20. #include <asm/cputype.h>
  21. #include <asm/cpufeature.h>
  22. static bool __maybe_unused
  23. is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
  24. {
  25. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  26. return MIDR_IS_CPU_MODEL_RANGE(read_cpuid_id(), entry->midr_model,
  27. entry->midr_range_min,
  28. entry->midr_range_max);
  29. }
  30. static bool
  31. has_mismatched_cache_line_size(const struct arm64_cpu_capabilities *entry,
  32. int scope)
  33. {
  34. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  35. return (read_cpuid_cachetype() & arm64_ftr_reg_ctrel0.strict_mask) !=
  36. (arm64_ftr_reg_ctrel0.sys_val & arm64_ftr_reg_ctrel0.strict_mask);
  37. }
  38. static int cpu_enable_trap_ctr_access(void *__unused)
  39. {
  40. /* Clear SCTLR_EL1.UCT */
  41. config_sctlr_el1(SCTLR_EL1_UCT, 0);
  42. return 0;
  43. }
  44. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  45. #include <asm/mmu_context.h>
  46. #include <asm/cacheflush.h>
  47. DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
  48. #ifdef CONFIG_KVM
  49. extern char __smccc_workaround_1_smc_start[];
  50. extern char __smccc_workaround_1_smc_end[];
  51. extern char __smccc_workaround_1_hvc_start[];
  52. extern char __smccc_workaround_1_hvc_end[];
  53. static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
  54. const char *hyp_vecs_end)
  55. {
  56. void *dst = __bp_harden_hyp_vecs_start + slot * SZ_2K;
  57. int i;
  58. for (i = 0; i < SZ_2K; i += 0x80)
  59. memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
  60. flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
  61. }
  62. static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
  63. const char *hyp_vecs_start,
  64. const char *hyp_vecs_end)
  65. {
  66. static int last_slot = -1;
  67. static DEFINE_SPINLOCK(bp_lock);
  68. int cpu, slot = -1;
  69. spin_lock(&bp_lock);
  70. for_each_possible_cpu(cpu) {
  71. if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
  72. slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
  73. break;
  74. }
  75. }
  76. if (slot == -1) {
  77. last_slot++;
  78. BUG_ON(((__bp_harden_hyp_vecs_end - __bp_harden_hyp_vecs_start)
  79. / SZ_2K) <= last_slot);
  80. slot = last_slot;
  81. __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
  82. }
  83. __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
  84. __this_cpu_write(bp_hardening_data.fn, fn);
  85. spin_unlock(&bp_lock);
  86. }
  87. #else
  88. #define __smccc_workaround_1_smc_start NULL
  89. #define __smccc_workaround_1_smc_end NULL
  90. #define __smccc_workaround_1_hvc_start NULL
  91. #define __smccc_workaround_1_hvc_end NULL
  92. static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
  93. const char *hyp_vecs_start,
  94. const char *hyp_vecs_end)
  95. {
  96. __this_cpu_write(bp_hardening_data.fn, fn);
  97. }
  98. #endif /* CONFIG_KVM */
  99. static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
  100. bp_hardening_cb_t fn,
  101. const char *hyp_vecs_start,
  102. const char *hyp_vecs_end)
  103. {
  104. u64 pfr0;
  105. if (!entry->matches(entry, SCOPE_LOCAL_CPU))
  106. return;
  107. pfr0 = read_cpuid(ID_AA64PFR0_EL1);
  108. if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
  109. return;
  110. __install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
  111. }
  112. #include <uapi/linux/psci.h>
  113. #include <linux/arm-smccc.h>
  114. #include <linux/psci.h>
  115. static void call_smc_arch_workaround_1(void)
  116. {
  117. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  118. }
  119. static void call_hvc_arch_workaround_1(void)
  120. {
  121. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  122. }
  123. static int enable_smccc_arch_workaround_1(void *data)
  124. {
  125. const struct arm64_cpu_capabilities *entry = data;
  126. bp_hardening_cb_t cb;
  127. void *smccc_start, *smccc_end;
  128. struct arm_smccc_res res;
  129. if (!entry->matches(entry, SCOPE_LOCAL_CPU))
  130. return 0;
  131. if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
  132. return 0;
  133. switch (psci_ops.conduit) {
  134. case PSCI_CONDUIT_HVC:
  135. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  136. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  137. if ((int)res.a0 < 0)
  138. return 0;
  139. cb = call_hvc_arch_workaround_1;
  140. smccc_start = __smccc_workaround_1_hvc_start;
  141. smccc_end = __smccc_workaround_1_hvc_end;
  142. break;
  143. case PSCI_CONDUIT_SMC:
  144. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  145. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  146. if ((int)res.a0 < 0)
  147. return 0;
  148. cb = call_smc_arch_workaround_1;
  149. smccc_start = __smccc_workaround_1_smc_start;
  150. smccc_end = __smccc_workaround_1_smc_end;
  151. break;
  152. default:
  153. return 0;
  154. }
  155. install_bp_hardening_cb(entry, cb, smccc_start, smccc_end);
  156. return 0;
  157. }
  158. #endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
  159. #ifdef CONFIG_ARM64_SSBD
  160. DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
  161. int ssbd_state __read_mostly = ARM64_SSBD_KERNEL;
  162. static const struct ssbd_options {
  163. const char *str;
  164. int state;
  165. } ssbd_options[] = {
  166. { "force-on", ARM64_SSBD_FORCE_ENABLE, },
  167. { "force-off", ARM64_SSBD_FORCE_DISABLE, },
  168. { "kernel", ARM64_SSBD_KERNEL, },
  169. };
  170. static int __init ssbd_cfg(char *buf)
  171. {
  172. int i;
  173. if (!buf || !buf[0])
  174. return -EINVAL;
  175. for (i = 0; i < ARRAY_SIZE(ssbd_options); i++) {
  176. int len = strlen(ssbd_options[i].str);
  177. if (strncmp(buf, ssbd_options[i].str, len))
  178. continue;
  179. ssbd_state = ssbd_options[i].state;
  180. return 0;
  181. }
  182. return -EINVAL;
  183. }
  184. early_param("ssbd", ssbd_cfg);
  185. void __init arm64_update_smccc_conduit(struct alt_instr *alt,
  186. __le32 *origptr, __le32 *updptr,
  187. int nr_inst)
  188. {
  189. u32 insn;
  190. BUG_ON(nr_inst != 1);
  191. switch (psci_ops.conduit) {
  192. case PSCI_CONDUIT_HVC:
  193. insn = aarch64_insn_get_hvc_value();
  194. break;
  195. case PSCI_CONDUIT_SMC:
  196. insn = aarch64_insn_get_smc_value();
  197. break;
  198. default:
  199. return;
  200. }
  201. *updptr = cpu_to_le32(insn);
  202. }
  203. void __init arm64_enable_wa2_handling(struct alt_instr *alt,
  204. __le32 *origptr, __le32 *updptr,
  205. int nr_inst)
  206. {
  207. BUG_ON(nr_inst != 1);
  208. /*
  209. * Only allow mitigation on EL1 entry/exit and guest
  210. * ARCH_WORKAROUND_2 handling if the SSBD state allows it to
  211. * be flipped.
  212. */
  213. if (arm64_get_ssbd_state() == ARM64_SSBD_KERNEL)
  214. *updptr = cpu_to_le32(aarch64_insn_gen_nop());
  215. }
  216. void arm64_set_ssbd_mitigation(bool state)
  217. {
  218. switch (psci_ops.conduit) {
  219. case PSCI_CONDUIT_HVC:
  220. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
  221. break;
  222. case PSCI_CONDUIT_SMC:
  223. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
  224. break;
  225. default:
  226. WARN_ON_ONCE(1);
  227. break;
  228. }
  229. }
  230. static bool has_ssbd_mitigation(const struct arm64_cpu_capabilities *entry,
  231. int scope)
  232. {
  233. struct arm_smccc_res res;
  234. bool required = true;
  235. s32 val;
  236. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  237. if (psci_ops.smccc_version == SMCCC_VERSION_1_0) {
  238. ssbd_state = ARM64_SSBD_UNKNOWN;
  239. return false;
  240. }
  241. switch (psci_ops.conduit) {
  242. case PSCI_CONDUIT_HVC:
  243. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  244. ARM_SMCCC_ARCH_WORKAROUND_2, &res);
  245. break;
  246. case PSCI_CONDUIT_SMC:
  247. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  248. ARM_SMCCC_ARCH_WORKAROUND_2, &res);
  249. break;
  250. default:
  251. ssbd_state = ARM64_SSBD_UNKNOWN;
  252. return false;
  253. }
  254. val = (s32)res.a0;
  255. switch (val) {
  256. case SMCCC_RET_NOT_SUPPORTED:
  257. ssbd_state = ARM64_SSBD_UNKNOWN;
  258. return false;
  259. case SMCCC_RET_NOT_REQUIRED:
  260. pr_info_once("%s mitigation not required\n", entry->desc);
  261. ssbd_state = ARM64_SSBD_MITIGATED;
  262. return false;
  263. case SMCCC_RET_SUCCESS:
  264. required = true;
  265. break;
  266. case 1: /* Mitigation not required on this CPU */
  267. required = false;
  268. break;
  269. default:
  270. WARN_ON(1);
  271. return false;
  272. }
  273. switch (ssbd_state) {
  274. case ARM64_SSBD_FORCE_DISABLE:
  275. pr_info_once("%s disabled from command-line\n", entry->desc);
  276. arm64_set_ssbd_mitigation(false);
  277. required = false;
  278. break;
  279. case ARM64_SSBD_KERNEL:
  280. if (required) {
  281. __this_cpu_write(arm64_ssbd_callback_required, 1);
  282. arm64_set_ssbd_mitigation(true);
  283. }
  284. break;
  285. case ARM64_SSBD_FORCE_ENABLE:
  286. pr_info_once("%s forced from command-line\n", entry->desc);
  287. arm64_set_ssbd_mitigation(true);
  288. required = true;
  289. break;
  290. default:
  291. WARN_ON(1);
  292. break;
  293. }
  294. return required;
  295. }
  296. #endif /* CONFIG_ARM64_SSBD */
  297. #define MIDR_RANGE(model, min, max) \
  298. .def_scope = SCOPE_LOCAL_CPU, \
  299. .matches = is_affected_midr_range, \
  300. .midr_model = model, \
  301. .midr_range_min = min, \
  302. .midr_range_max = max
  303. #define MIDR_ALL_VERSIONS(model) \
  304. .def_scope = SCOPE_LOCAL_CPU, \
  305. .matches = is_affected_midr_range, \
  306. .midr_model = model, \
  307. .midr_range_min = 0, \
  308. .midr_range_max = (MIDR_VARIANT_MASK | MIDR_REVISION_MASK)
  309. const struct arm64_cpu_capabilities arm64_errata[] = {
  310. #if defined(CONFIG_ARM64_ERRATUM_826319) || \
  311. defined(CONFIG_ARM64_ERRATUM_827319) || \
  312. defined(CONFIG_ARM64_ERRATUM_824069)
  313. {
  314. /* Cortex-A53 r0p[012] */
  315. .desc = "ARM errata 826319, 827319, 824069",
  316. .capability = ARM64_WORKAROUND_CLEAN_CACHE,
  317. MIDR_RANGE(MIDR_CORTEX_A53, 0x00, 0x02),
  318. .enable = cpu_enable_cache_maint_trap,
  319. },
  320. #endif
  321. #ifdef CONFIG_ARM64_ERRATUM_819472
  322. {
  323. /* Cortex-A53 r0p[01] */
  324. .desc = "ARM errata 819472",
  325. .capability = ARM64_WORKAROUND_CLEAN_CACHE,
  326. MIDR_RANGE(MIDR_CORTEX_A53, 0x00, 0x01),
  327. .enable = cpu_enable_cache_maint_trap,
  328. },
  329. #endif
  330. #ifdef CONFIG_ARM64_ERRATUM_832075
  331. {
  332. /* Cortex-A57 r0p0 - r1p2 */
  333. .desc = "ARM erratum 832075",
  334. .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
  335. MIDR_RANGE(MIDR_CORTEX_A57, 0x00,
  336. (1 << MIDR_VARIANT_SHIFT) | 2),
  337. },
  338. #endif
  339. #ifdef CONFIG_ARM64_ERRATUM_834220
  340. {
  341. /* Cortex-A57 r0p0 - r1p2 */
  342. .desc = "ARM erratum 834220",
  343. .capability = ARM64_WORKAROUND_834220,
  344. MIDR_RANGE(MIDR_CORTEX_A57, 0x00,
  345. (1 << MIDR_VARIANT_SHIFT) | 2),
  346. },
  347. #endif
  348. #ifdef CONFIG_ARM64_ERRATUM_845719
  349. {
  350. /* Cortex-A53 r0p[01234] */
  351. .desc = "ARM erratum 845719",
  352. .capability = ARM64_WORKAROUND_845719,
  353. MIDR_RANGE(MIDR_CORTEX_A53, 0x00, 0x04),
  354. },
  355. #endif
  356. #ifdef CONFIG_CAVIUM_ERRATUM_23154
  357. {
  358. /* Cavium ThunderX, pass 1.x */
  359. .desc = "Cavium erratum 23154",
  360. .capability = ARM64_WORKAROUND_CAVIUM_23154,
  361. MIDR_RANGE(MIDR_THUNDERX, 0x00, 0x01),
  362. },
  363. #endif
  364. #ifdef CONFIG_CAVIUM_ERRATUM_27456
  365. {
  366. /* Cavium ThunderX, T88 pass 1.x - 2.1 */
  367. .desc = "Cavium erratum 27456",
  368. .capability = ARM64_WORKAROUND_CAVIUM_27456,
  369. MIDR_RANGE(MIDR_THUNDERX, 0x00,
  370. (1 << MIDR_VARIANT_SHIFT) | 1),
  371. },
  372. {
  373. /* Cavium ThunderX, T81 pass 1.0 */
  374. .desc = "Cavium erratum 27456",
  375. .capability = ARM64_WORKAROUND_CAVIUM_27456,
  376. MIDR_RANGE(MIDR_THUNDERX_81XX, 0x00, 0x00),
  377. },
  378. #endif
  379. {
  380. .desc = "Mismatched cache line size",
  381. .capability = ARM64_MISMATCHED_CACHE_LINE_SIZE,
  382. .matches = has_mismatched_cache_line_size,
  383. .def_scope = SCOPE_LOCAL_CPU,
  384. .enable = cpu_enable_trap_ctr_access,
  385. },
  386. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  387. {
  388. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  389. MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
  390. .enable = enable_smccc_arch_workaround_1,
  391. },
  392. {
  393. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  394. MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
  395. .enable = enable_smccc_arch_workaround_1,
  396. },
  397. {
  398. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  399. MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
  400. .enable = enable_smccc_arch_workaround_1,
  401. },
  402. {
  403. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  404. MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
  405. .enable = enable_smccc_arch_workaround_1,
  406. },
  407. {
  408. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  409. MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
  410. .enable = enable_smccc_arch_workaround_1,
  411. },
  412. {
  413. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  414. MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
  415. .enable = enable_smccc_arch_workaround_1,
  416. },
  417. #endif
  418. #ifdef CONFIG_ARM64_SSBD
  419. {
  420. .desc = "Speculative Store Bypass Disable",
  421. .def_scope = SCOPE_LOCAL_CPU,
  422. .capability = ARM64_SSBD,
  423. .matches = has_ssbd_mitigation,
  424. },
  425. #endif
  426. {
  427. }
  428. };
  429. /*
  430. * The CPU Errata work arounds are detected and applied at boot time
  431. * and the related information is freed soon after. If the new CPU requires
  432. * an errata not detected at boot, fail this CPU.
  433. */
  434. void verify_local_cpu_errata_workarounds(void)
  435. {
  436. const struct arm64_cpu_capabilities *caps = arm64_errata;
  437. for (; caps->matches; caps++) {
  438. if (cpus_have_cap(caps->capability)) {
  439. if (caps->enable)
  440. caps->enable((void *)caps);
  441. } else if (caps->matches(caps, SCOPE_LOCAL_CPU)) {
  442. pr_crit("CPU%d: Requires work around for %s, not detected"
  443. " at boot time\n",
  444. smp_processor_id(),
  445. caps->desc ? : "an erratum");
  446. cpu_die_early();
  447. }
  448. }
  449. }
  450. void update_cpu_errata_workarounds(void)
  451. {
  452. update_cpu_capabilities(arm64_errata, "enabling workaround for");
  453. }
  454. void __init enable_errata_workarounds(void)
  455. {
  456. enable_cpu_capabilities(arm64_errata);
  457. }