2 * Contains CPU specific errata definitions
4 * Copyright (C) 2014 ARM Ltd.
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.
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.
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/>.
19 #include <linux/arm-smccc.h>
20 #include <linux/psci.h>
21 #include <linux/types.h>
23 #include <asm/cputype.h>
24 #include <asm/cpufeature.h>
26 static bool __maybe_unused
27 is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
29 const struct arm64_midr_revidr *fix;
30 u32 midr = read_cpuid_id(), revidr;
32 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
33 if (!is_midr_in_range(midr, &entry->midr_range))
36 midr &= MIDR_REVISION_MASK | MIDR_VARIANT_MASK;
37 revidr = read_cpuid(REVIDR_EL1);
38 for (fix = entry->fixed_revs; fix && fix->revidr_mask; fix++)
39 if (midr == fix->midr_rv && (revidr & fix->revidr_mask))
45 static bool __maybe_unused
46 is_affected_midr_range_list(const struct arm64_cpu_capabilities *entry,
49 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
50 return is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list);
53 static bool __maybe_unused
54 is_kryo_midr(const struct arm64_cpu_capabilities *entry, int scope)
58 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
60 model = read_cpuid_id();
61 model &= MIDR_IMPLEMENTOR_MASK | (0xf00 << MIDR_PARTNUM_SHIFT) |
62 MIDR_ARCHITECTURE_MASK;
64 return model == entry->midr_range.model;
68 has_mismatched_cache_line_size(const struct arm64_cpu_capabilities *entry,
71 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
72 return (read_cpuid_cachetype() & arm64_ftr_reg_ctrel0.strict_mask) !=
73 (arm64_ftr_reg_ctrel0.sys_val & arm64_ftr_reg_ctrel0.strict_mask);
77 cpu_enable_trap_ctr_access(const struct arm64_cpu_capabilities *__unused)
79 /* Clear SCTLR_EL1.UCT */
80 config_sctlr_el1(SCTLR_EL1_UCT, 0);
83 atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
85 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
86 #include <asm/mmu_context.h>
87 #include <asm/cacheflush.h>
89 DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
91 #ifdef CONFIG_KVM_INDIRECT_VECTORS
92 extern char __smccc_workaround_1_smc_start[];
93 extern char __smccc_workaround_1_smc_end[];
95 static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
96 const char *hyp_vecs_end)
98 void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
101 for (i = 0; i < SZ_2K; i += 0x80)
102 memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
104 flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
107 static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
108 const char *hyp_vecs_start,
109 const char *hyp_vecs_end)
111 static DEFINE_SPINLOCK(bp_lock);
115 for_each_possible_cpu(cpu) {
116 if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
117 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
123 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
124 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
125 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
128 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
129 __this_cpu_write(bp_hardening_data.fn, fn);
130 spin_unlock(&bp_lock);
133 #define __smccc_workaround_1_smc_start NULL
134 #define __smccc_workaround_1_smc_end NULL
136 static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
137 const char *hyp_vecs_start,
138 const char *hyp_vecs_end)
140 __this_cpu_write(bp_hardening_data.fn, fn);
142 #endif /* CONFIG_KVM_INDIRECT_VECTORS */
144 static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
145 bp_hardening_cb_t fn,
146 const char *hyp_vecs_start,
147 const char *hyp_vecs_end)
151 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
154 pfr0 = read_cpuid(ID_AA64PFR0_EL1);
155 if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
158 __install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
161 #include <uapi/linux/psci.h>
162 #include <linux/arm-smccc.h>
163 #include <linux/psci.h>
165 static void call_smc_arch_workaround_1(void)
167 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
170 static void call_hvc_arch_workaround_1(void)
172 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
175 static void qcom_link_stack_sanitization(void)
179 asm volatile("mov %0, x30 \n"
188 enable_smccc_arch_workaround_1(const struct arm64_cpu_capabilities *entry)
190 bp_hardening_cb_t cb;
191 void *smccc_start, *smccc_end;
192 struct arm_smccc_res res;
193 u32 midr = read_cpuid_id();
195 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
198 if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
201 switch (psci_ops.conduit) {
202 case PSCI_CONDUIT_HVC:
203 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
204 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
207 cb = call_hvc_arch_workaround_1;
208 /* This is a guest, no need to patch KVM vectors */
213 case PSCI_CONDUIT_SMC:
214 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
215 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
218 cb = call_smc_arch_workaround_1;
219 smccc_start = __smccc_workaround_1_smc_start;
220 smccc_end = __smccc_workaround_1_smc_end;
227 if (((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR) ||
228 ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1))
229 cb = qcom_link_stack_sanitization;
231 install_bp_hardening_cb(entry, cb, smccc_start, smccc_end);
235 #endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
237 #ifdef CONFIG_ARM64_SSBD
238 DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
240 int ssbd_state __read_mostly = ARM64_SSBD_KERNEL;
242 static const struct ssbd_options {
246 { "force-on", ARM64_SSBD_FORCE_ENABLE, },
247 { "force-off", ARM64_SSBD_FORCE_DISABLE, },
248 { "kernel", ARM64_SSBD_KERNEL, },
251 static int __init ssbd_cfg(char *buf)
258 for (i = 0; i < ARRAY_SIZE(ssbd_options); i++) {
259 int len = strlen(ssbd_options[i].str);
261 if (strncmp(buf, ssbd_options[i].str, len))
264 ssbd_state = ssbd_options[i].state;
270 early_param("ssbd", ssbd_cfg);
272 void __init arm64_update_smccc_conduit(struct alt_instr *alt,
273 __le32 *origptr, __le32 *updptr,
278 BUG_ON(nr_inst != 1);
280 switch (psci_ops.conduit) {
281 case PSCI_CONDUIT_HVC:
282 insn = aarch64_insn_get_hvc_value();
284 case PSCI_CONDUIT_SMC:
285 insn = aarch64_insn_get_smc_value();
291 *updptr = cpu_to_le32(insn);
294 void __init arm64_enable_wa2_handling(struct alt_instr *alt,
295 __le32 *origptr, __le32 *updptr,
298 BUG_ON(nr_inst != 1);
300 * Only allow mitigation on EL1 entry/exit and guest
301 * ARCH_WORKAROUND_2 handling if the SSBD state allows it to
304 if (arm64_get_ssbd_state() == ARM64_SSBD_KERNEL)
305 *updptr = cpu_to_le32(aarch64_insn_gen_nop());
308 void arm64_set_ssbd_mitigation(bool state)
310 switch (psci_ops.conduit) {
311 case PSCI_CONDUIT_HVC:
312 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
315 case PSCI_CONDUIT_SMC:
316 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
325 static bool has_ssbd_mitigation(const struct arm64_cpu_capabilities *entry,
328 struct arm_smccc_res res;
329 bool required = true;
332 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
334 if (psci_ops.smccc_version == SMCCC_VERSION_1_0) {
335 ssbd_state = ARM64_SSBD_UNKNOWN;
339 switch (psci_ops.conduit) {
340 case PSCI_CONDUIT_HVC:
341 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
342 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
345 case PSCI_CONDUIT_SMC:
346 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
347 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
351 ssbd_state = ARM64_SSBD_UNKNOWN;
358 case SMCCC_RET_NOT_SUPPORTED:
359 ssbd_state = ARM64_SSBD_UNKNOWN;
362 case SMCCC_RET_NOT_REQUIRED:
363 pr_info_once("%s mitigation not required\n", entry->desc);
364 ssbd_state = ARM64_SSBD_MITIGATED;
367 case SMCCC_RET_SUCCESS:
371 case 1: /* Mitigation not required on this CPU */
380 switch (ssbd_state) {
381 case ARM64_SSBD_FORCE_DISABLE:
382 pr_info_once("%s disabled from command-line\n", entry->desc);
383 arm64_set_ssbd_mitigation(false);
387 case ARM64_SSBD_KERNEL:
389 __this_cpu_write(arm64_ssbd_callback_required, 1);
390 arm64_set_ssbd_mitigation(true);
394 case ARM64_SSBD_FORCE_ENABLE:
395 pr_info_once("%s forced from command-line\n", entry->desc);
396 arm64_set_ssbd_mitigation(true);
407 #endif /* CONFIG_ARM64_SSBD */
409 #define CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
410 .matches = is_affected_midr_range, \
411 .midr_range = MIDR_RANGE(model, v_min, r_min, v_max, r_max)
413 #define CAP_MIDR_ALL_VERSIONS(model) \
414 .matches = is_affected_midr_range, \
415 .midr_range = MIDR_ALL_VERSIONS(model)
417 #define MIDR_FIXED(rev, revidr_mask) \
418 .fixed_revs = (struct arm64_midr_revidr[]){{ (rev), (revidr_mask) }, {}}
420 #define ERRATA_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
421 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
422 CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max)
424 #define CAP_MIDR_RANGE_LIST(list) \
425 .matches = is_affected_midr_range_list, \
426 .midr_range_list = list
428 /* Errata affecting a range of revisions of given model variant */
429 #define ERRATA_MIDR_REV_RANGE(m, var, r_min, r_max) \
430 ERRATA_MIDR_RANGE(m, var, r_min, var, r_max)
432 /* Errata affecting a single variant/revision of a model */
433 #define ERRATA_MIDR_REV(model, var, rev) \
434 ERRATA_MIDR_RANGE(model, var, rev, var, rev)
436 /* Errata affecting all variants/revisions of a given a model */
437 #define ERRATA_MIDR_ALL_VERSIONS(model) \
438 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
439 CAP_MIDR_ALL_VERSIONS(model)
441 /* Errata affecting a list of midr ranges, with same work around */
442 #define ERRATA_MIDR_RANGE_LIST(midr_list) \
443 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
444 CAP_MIDR_RANGE_LIST(midr_list)
447 * Generic helper for handling capabilties with multiple (match,enable) pairs
448 * of call backs, sharing the same capability bit.
449 * Iterate over each entry to see if at least one matches.
451 static bool __maybe_unused
452 multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry, int scope)
454 const struct arm64_cpu_capabilities *caps;
456 for (caps = entry->match_list; caps->matches; caps++)
457 if (caps->matches(caps, scope))
464 * Take appropriate action for all matching entries in the shared capability
467 static void __maybe_unused
468 multi_entry_cap_cpu_enable(const struct arm64_cpu_capabilities *entry)
470 const struct arm64_cpu_capabilities *caps;
472 for (caps = entry->match_list; caps->matches; caps++)
473 if (caps->matches(caps, SCOPE_LOCAL_CPU) &&
475 caps->cpu_enable(caps);
478 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
481 * List of CPUs where we need to issue a psci call to
482 * harden the branch predictor.
484 static const struct midr_range arm64_bp_harden_smccc_cpus[] = {
485 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
486 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
487 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
488 MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
489 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
490 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
491 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR_V1),
492 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR),
493 MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER),
499 #ifdef CONFIG_HARDEN_EL2_VECTORS
501 static const struct midr_range arm64_harden_el2_vectors[] = {
502 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
503 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
509 const struct arm64_cpu_capabilities arm64_errata[] = {
510 #if defined(CONFIG_ARM64_ERRATUM_826319) || \
511 defined(CONFIG_ARM64_ERRATUM_827319) || \
512 defined(CONFIG_ARM64_ERRATUM_824069)
514 /* Cortex-A53 r0p[012] */
515 .desc = "ARM errata 826319, 827319, 824069",
516 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
517 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 2),
518 .cpu_enable = cpu_enable_cache_maint_trap,
521 #ifdef CONFIG_ARM64_ERRATUM_819472
523 /* Cortex-A53 r0p[01] */
524 .desc = "ARM errata 819472",
525 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
526 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 1),
527 .cpu_enable = cpu_enable_cache_maint_trap,
530 #ifdef CONFIG_ARM64_ERRATUM_832075
532 /* Cortex-A57 r0p0 - r1p2 */
533 .desc = "ARM erratum 832075",
534 .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
535 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
540 #ifdef CONFIG_ARM64_ERRATUM_834220
542 /* Cortex-A57 r0p0 - r1p2 */
543 .desc = "ARM erratum 834220",
544 .capability = ARM64_WORKAROUND_834220,
545 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
550 #ifdef CONFIG_ARM64_ERRATUM_843419
552 /* Cortex-A53 r0p[01234] */
553 .desc = "ARM erratum 843419",
554 .capability = ARM64_WORKAROUND_843419,
555 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
556 MIDR_FIXED(0x4, BIT(8)),
559 #ifdef CONFIG_ARM64_ERRATUM_845719
561 /* Cortex-A53 r0p[01234] */
562 .desc = "ARM erratum 845719",
563 .capability = ARM64_WORKAROUND_845719,
564 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
567 #ifdef CONFIG_CAVIUM_ERRATUM_23154
569 /* Cavium ThunderX, pass 1.x */
570 .desc = "Cavium erratum 23154",
571 .capability = ARM64_WORKAROUND_CAVIUM_23154,
572 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX, 0, 0, 1),
575 #ifdef CONFIG_CAVIUM_ERRATUM_27456
577 /* Cavium ThunderX, T88 pass 1.x - 2.1 */
578 .desc = "Cavium erratum 27456",
579 .capability = ARM64_WORKAROUND_CAVIUM_27456,
580 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
585 /* Cavium ThunderX, T81 pass 1.0 */
586 .desc = "Cavium erratum 27456",
587 .capability = ARM64_WORKAROUND_CAVIUM_27456,
588 ERRATA_MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
591 #ifdef CONFIG_CAVIUM_ERRATUM_30115
593 /* Cavium ThunderX, T88 pass 1.x - 2.2 */
594 .desc = "Cavium erratum 30115",
595 .capability = ARM64_WORKAROUND_CAVIUM_30115,
596 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
601 /* Cavium ThunderX, T81 pass 1.0 - 1.2 */
602 .desc = "Cavium erratum 30115",
603 .capability = ARM64_WORKAROUND_CAVIUM_30115,
604 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX_81XX, 0, 0, 2),
607 /* Cavium ThunderX, T83 pass 1.0 */
608 .desc = "Cavium erratum 30115",
609 .capability = ARM64_WORKAROUND_CAVIUM_30115,
610 ERRATA_MIDR_REV(MIDR_THUNDERX_83XX, 0, 0),
614 .desc = "Mismatched cache line size",
615 .capability = ARM64_MISMATCHED_CACHE_LINE_SIZE,
616 .matches = has_mismatched_cache_line_size,
617 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
618 .cpu_enable = cpu_enable_trap_ctr_access,
620 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
622 .desc = "Qualcomm Technologies Falkor erratum 1003",
623 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
624 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
627 .desc = "Qualcomm Technologies Kryo erratum 1003",
628 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
629 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
630 .midr_range.model = MIDR_QCOM_KRYO,
631 .matches = is_kryo_midr,
634 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
636 .desc = "Qualcomm Technologies Falkor erratum 1009",
637 .capability = ARM64_WORKAROUND_REPEAT_TLBI,
638 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
641 #ifdef CONFIG_ARM64_ERRATUM_858921
643 /* Cortex-A73 all versions */
644 .desc = "ARM erratum 858921",
645 .capability = ARM64_WORKAROUND_858921,
646 ERRATA_MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
649 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
651 .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
652 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
653 .cpu_enable = enable_smccc_arch_workaround_1,
654 ERRATA_MIDR_RANGE_LIST(arm64_bp_harden_smccc_cpus),
657 #ifdef CONFIG_HARDEN_EL2_VECTORS
659 .desc = "EL2 vector hardening",
660 .capability = ARM64_HARDEN_EL2_VECTORS,
661 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
662 ERRATA_MIDR_RANGE_LIST(arm64_harden_el2_vectors),
665 #ifdef CONFIG_ARM64_SSBD
667 .desc = "Speculative Store Bypass Disable",
668 .capability = ARM64_SSBD,
669 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
670 .matches = has_ssbd_mitigation,