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_type(const struct arm64_cpu_capabilities *entry,
71 u64 mask = CTR_CACHE_MINLINE_MASK;
73 /* Skip matching the min line sizes for cache type check */
74 if (entry->capability == ARM64_MISMATCHED_CACHE_TYPE)
75 mask ^= arm64_ftr_reg_ctrel0.strict_mask;
77 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
78 return (read_cpuid_cachetype() & mask) !=
79 (arm64_ftr_reg_ctrel0.sys_val & mask);
83 cpu_enable_trap_ctr_access(const struct arm64_cpu_capabilities *__unused)
85 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
88 atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
90 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
91 #include <asm/mmu_context.h>
92 #include <asm/cacheflush.h>
94 DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
96 #ifdef CONFIG_KVM_INDIRECT_VECTORS
97 extern char __smccc_workaround_1_smc_start[];
98 extern char __smccc_workaround_1_smc_end[];
100 static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
101 const char *hyp_vecs_end)
103 void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
106 for (i = 0; i < SZ_2K; i += 0x80)
107 memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
109 __flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
112 static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
113 const char *hyp_vecs_start,
114 const char *hyp_vecs_end)
116 static DEFINE_SPINLOCK(bp_lock);
120 for_each_possible_cpu(cpu) {
121 if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
122 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
128 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
129 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
130 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
133 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
134 __this_cpu_write(bp_hardening_data.fn, fn);
135 spin_unlock(&bp_lock);
138 #define __smccc_workaround_1_smc_start NULL
139 #define __smccc_workaround_1_smc_end NULL
141 static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
142 const char *hyp_vecs_start,
143 const char *hyp_vecs_end)
145 __this_cpu_write(bp_hardening_data.fn, fn);
147 #endif /* CONFIG_KVM_INDIRECT_VECTORS */
149 static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
150 bp_hardening_cb_t fn,
151 const char *hyp_vecs_start,
152 const char *hyp_vecs_end)
156 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
159 pfr0 = read_cpuid(ID_AA64PFR0_EL1);
160 if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
163 __install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
166 #include <uapi/linux/psci.h>
167 #include <linux/arm-smccc.h>
168 #include <linux/psci.h>
170 static void call_smc_arch_workaround_1(void)
172 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
175 static void call_hvc_arch_workaround_1(void)
177 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
180 static void qcom_link_stack_sanitization(void)
184 asm volatile("mov %0, x30 \n"
193 enable_smccc_arch_workaround_1(const struct arm64_cpu_capabilities *entry)
195 bp_hardening_cb_t cb;
196 void *smccc_start, *smccc_end;
197 struct arm_smccc_res res;
198 u32 midr = read_cpuid_id();
200 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
203 if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
206 switch (psci_ops.conduit) {
207 case PSCI_CONDUIT_HVC:
208 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
209 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
212 cb = call_hvc_arch_workaround_1;
213 /* This is a guest, no need to patch KVM vectors */
218 case PSCI_CONDUIT_SMC:
219 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
220 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
223 cb = call_smc_arch_workaround_1;
224 smccc_start = __smccc_workaround_1_smc_start;
225 smccc_end = __smccc_workaround_1_smc_end;
232 if (((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR) ||
233 ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1))
234 cb = qcom_link_stack_sanitization;
236 install_bp_hardening_cb(entry, cb, smccc_start, smccc_end);
240 #endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
242 #ifdef CONFIG_ARM64_SSBD
243 DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
245 int ssbd_state __read_mostly = ARM64_SSBD_KERNEL;
247 static const struct ssbd_options {
251 { "force-on", ARM64_SSBD_FORCE_ENABLE, },
252 { "force-off", ARM64_SSBD_FORCE_DISABLE, },
253 { "kernel", ARM64_SSBD_KERNEL, },
256 static int __init ssbd_cfg(char *buf)
263 for (i = 0; i < ARRAY_SIZE(ssbd_options); i++) {
264 int len = strlen(ssbd_options[i].str);
266 if (strncmp(buf, ssbd_options[i].str, len))
269 ssbd_state = ssbd_options[i].state;
275 early_param("ssbd", ssbd_cfg);
277 void __init arm64_update_smccc_conduit(struct alt_instr *alt,
278 __le32 *origptr, __le32 *updptr,
283 BUG_ON(nr_inst != 1);
285 switch (psci_ops.conduit) {
286 case PSCI_CONDUIT_HVC:
287 insn = aarch64_insn_get_hvc_value();
289 case PSCI_CONDUIT_SMC:
290 insn = aarch64_insn_get_smc_value();
296 *updptr = cpu_to_le32(insn);
299 void __init arm64_enable_wa2_handling(struct alt_instr *alt,
300 __le32 *origptr, __le32 *updptr,
303 BUG_ON(nr_inst != 1);
305 * Only allow mitigation on EL1 entry/exit and guest
306 * ARCH_WORKAROUND_2 handling if the SSBD state allows it to
309 if (arm64_get_ssbd_state() == ARM64_SSBD_KERNEL)
310 *updptr = cpu_to_le32(aarch64_insn_gen_nop());
313 void arm64_set_ssbd_mitigation(bool state)
315 switch (psci_ops.conduit) {
316 case PSCI_CONDUIT_HVC:
317 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
320 case PSCI_CONDUIT_SMC:
321 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
330 static bool has_ssbd_mitigation(const struct arm64_cpu_capabilities *entry,
333 struct arm_smccc_res res;
334 bool required = true;
337 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
339 if (psci_ops.smccc_version == SMCCC_VERSION_1_0) {
340 ssbd_state = ARM64_SSBD_UNKNOWN;
344 switch (psci_ops.conduit) {
345 case PSCI_CONDUIT_HVC:
346 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
347 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
350 case PSCI_CONDUIT_SMC:
351 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
352 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
356 ssbd_state = ARM64_SSBD_UNKNOWN;
363 case SMCCC_RET_NOT_SUPPORTED:
364 ssbd_state = ARM64_SSBD_UNKNOWN;
367 case SMCCC_RET_NOT_REQUIRED:
368 pr_info_once("%s mitigation not required\n", entry->desc);
369 ssbd_state = ARM64_SSBD_MITIGATED;
372 case SMCCC_RET_SUCCESS:
376 case 1: /* Mitigation not required on this CPU */
385 switch (ssbd_state) {
386 case ARM64_SSBD_FORCE_DISABLE:
387 pr_info_once("%s disabled from command-line\n", entry->desc);
388 arm64_set_ssbd_mitigation(false);
392 case ARM64_SSBD_KERNEL:
394 __this_cpu_write(arm64_ssbd_callback_required, 1);
395 arm64_set_ssbd_mitigation(true);
399 case ARM64_SSBD_FORCE_ENABLE:
400 pr_info_once("%s forced from command-line\n", entry->desc);
401 arm64_set_ssbd_mitigation(true);
412 #endif /* CONFIG_ARM64_SSBD */
414 #define CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
415 .matches = is_affected_midr_range, \
416 .midr_range = MIDR_RANGE(model, v_min, r_min, v_max, r_max)
418 #define CAP_MIDR_ALL_VERSIONS(model) \
419 .matches = is_affected_midr_range, \
420 .midr_range = MIDR_ALL_VERSIONS(model)
422 #define MIDR_FIXED(rev, revidr_mask) \
423 .fixed_revs = (struct arm64_midr_revidr[]){{ (rev), (revidr_mask) }, {}}
425 #define ERRATA_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
426 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
427 CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max)
429 #define CAP_MIDR_RANGE_LIST(list) \
430 .matches = is_affected_midr_range_list, \
431 .midr_range_list = list
433 /* Errata affecting a range of revisions of given model variant */
434 #define ERRATA_MIDR_REV_RANGE(m, var, r_min, r_max) \
435 ERRATA_MIDR_RANGE(m, var, r_min, var, r_max)
437 /* Errata affecting a single variant/revision of a model */
438 #define ERRATA_MIDR_REV(model, var, rev) \
439 ERRATA_MIDR_RANGE(model, var, rev, var, rev)
441 /* Errata affecting all variants/revisions of a given a model */
442 #define ERRATA_MIDR_ALL_VERSIONS(model) \
443 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
444 CAP_MIDR_ALL_VERSIONS(model)
446 /* Errata affecting a list of midr ranges, with same work around */
447 #define ERRATA_MIDR_RANGE_LIST(midr_list) \
448 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
449 CAP_MIDR_RANGE_LIST(midr_list)
452 * Generic helper for handling capabilties with multiple (match,enable) pairs
453 * of call backs, sharing the same capability bit.
454 * Iterate over each entry to see if at least one matches.
456 static bool __maybe_unused
457 multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry, int scope)
459 const struct arm64_cpu_capabilities *caps;
461 for (caps = entry->match_list; caps->matches; caps++)
462 if (caps->matches(caps, scope))
469 * Take appropriate action for all matching entries in the shared capability
472 static void __maybe_unused
473 multi_entry_cap_cpu_enable(const struct arm64_cpu_capabilities *entry)
475 const struct arm64_cpu_capabilities *caps;
477 for (caps = entry->match_list; caps->matches; caps++)
478 if (caps->matches(caps, SCOPE_LOCAL_CPU) &&
480 caps->cpu_enable(caps);
483 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
486 * List of CPUs where we need to issue a psci call to
487 * harden the branch predictor.
489 static const struct midr_range arm64_bp_harden_smccc_cpus[] = {
490 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
491 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
492 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
493 MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
494 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
495 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
496 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR_V1),
497 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR),
498 MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER),
504 #ifdef CONFIG_HARDEN_EL2_VECTORS
506 static const struct midr_range arm64_harden_el2_vectors[] = {
507 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
508 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
514 const struct arm64_cpu_capabilities arm64_errata[] = {
515 #if defined(CONFIG_ARM64_ERRATUM_826319) || \
516 defined(CONFIG_ARM64_ERRATUM_827319) || \
517 defined(CONFIG_ARM64_ERRATUM_824069)
519 /* Cortex-A53 r0p[012] */
520 .desc = "ARM errata 826319, 827319, 824069",
521 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
522 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 2),
523 .cpu_enable = cpu_enable_cache_maint_trap,
526 #ifdef CONFIG_ARM64_ERRATUM_819472
528 /* Cortex-A53 r0p[01] */
529 .desc = "ARM errata 819472",
530 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
531 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 1),
532 .cpu_enable = cpu_enable_cache_maint_trap,
535 #ifdef CONFIG_ARM64_ERRATUM_832075
537 /* Cortex-A57 r0p0 - r1p2 */
538 .desc = "ARM erratum 832075",
539 .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
540 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
545 #ifdef CONFIG_ARM64_ERRATUM_834220
547 /* Cortex-A57 r0p0 - r1p2 */
548 .desc = "ARM erratum 834220",
549 .capability = ARM64_WORKAROUND_834220,
550 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
555 #ifdef CONFIG_ARM64_ERRATUM_843419
557 /* Cortex-A53 r0p[01234] */
558 .desc = "ARM erratum 843419",
559 .capability = ARM64_WORKAROUND_843419,
560 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
561 MIDR_FIXED(0x4, BIT(8)),
564 #ifdef CONFIG_ARM64_ERRATUM_845719
566 /* Cortex-A53 r0p[01234] */
567 .desc = "ARM erratum 845719",
568 .capability = ARM64_WORKAROUND_845719,
569 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
572 #ifdef CONFIG_CAVIUM_ERRATUM_23154
574 /* Cavium ThunderX, pass 1.x */
575 .desc = "Cavium erratum 23154",
576 .capability = ARM64_WORKAROUND_CAVIUM_23154,
577 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX, 0, 0, 1),
580 #ifdef CONFIG_CAVIUM_ERRATUM_27456
582 /* Cavium ThunderX, T88 pass 1.x - 2.1 */
583 .desc = "Cavium erratum 27456",
584 .capability = ARM64_WORKAROUND_CAVIUM_27456,
585 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
590 /* Cavium ThunderX, T81 pass 1.0 */
591 .desc = "Cavium erratum 27456",
592 .capability = ARM64_WORKAROUND_CAVIUM_27456,
593 ERRATA_MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
596 #ifdef CONFIG_CAVIUM_ERRATUM_30115
598 /* Cavium ThunderX, T88 pass 1.x - 2.2 */
599 .desc = "Cavium erratum 30115",
600 .capability = ARM64_WORKAROUND_CAVIUM_30115,
601 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
606 /* Cavium ThunderX, T81 pass 1.0 - 1.2 */
607 .desc = "Cavium erratum 30115",
608 .capability = ARM64_WORKAROUND_CAVIUM_30115,
609 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX_81XX, 0, 0, 2),
612 /* Cavium ThunderX, T83 pass 1.0 */
613 .desc = "Cavium erratum 30115",
614 .capability = ARM64_WORKAROUND_CAVIUM_30115,
615 ERRATA_MIDR_REV(MIDR_THUNDERX_83XX, 0, 0),
619 .desc = "Mismatched cache line size",
620 .capability = ARM64_MISMATCHED_CACHE_LINE_SIZE,
621 .matches = has_mismatched_cache_type,
622 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
623 .cpu_enable = cpu_enable_trap_ctr_access,
626 .desc = "Mismatched cache type",
627 .capability = ARM64_MISMATCHED_CACHE_TYPE,
628 .matches = has_mismatched_cache_type,
629 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
630 .cpu_enable = cpu_enable_trap_ctr_access,
632 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
634 .desc = "Qualcomm Technologies Falkor erratum 1003",
635 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
636 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
639 .desc = "Qualcomm Technologies Kryo erratum 1003",
640 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
641 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
642 .midr_range.model = MIDR_QCOM_KRYO,
643 .matches = is_kryo_midr,
646 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
648 .desc = "Qualcomm Technologies Falkor erratum 1009",
649 .capability = ARM64_WORKAROUND_REPEAT_TLBI,
650 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
653 #ifdef CONFIG_ARM64_ERRATUM_858921
655 /* Cortex-A73 all versions */
656 .desc = "ARM erratum 858921",
657 .capability = ARM64_WORKAROUND_858921,
658 ERRATA_MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
661 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
663 .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
664 .cpu_enable = enable_smccc_arch_workaround_1,
665 ERRATA_MIDR_RANGE_LIST(arm64_bp_harden_smccc_cpus),
668 #ifdef CONFIG_HARDEN_EL2_VECTORS
670 .desc = "EL2 vector hardening",
671 .capability = ARM64_HARDEN_EL2_VECTORS,
672 ERRATA_MIDR_RANGE_LIST(arm64_harden_el2_vectors),
675 #ifdef CONFIG_ARM64_SSBD
677 .desc = "Speculative Store Bypass Disable",
678 .capability = ARM64_SSBD,
679 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
680 .matches = has_ssbd_mitigation,