Loading...
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
19#include <linux/types.h>
20#include <asm/cpu.h>
21#include <asm/cputype.h>
22#include <asm/cpufeature.h>
23
24static bool __maybe_unused
25is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
26{
27 const struct arm64_midr_revidr *fix;
28 u32 midr = read_cpuid_id(), revidr;
29
30 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
31 if (!is_midr_in_range(midr, &entry->midr_range))
32 return false;
33
34 midr &= MIDR_REVISION_MASK | MIDR_VARIANT_MASK;
35 revidr = read_cpuid(REVIDR_EL1);
36 for (fix = entry->fixed_revs; fix && fix->revidr_mask; fix++)
37 if (midr == fix->midr_rv && (revidr & fix->revidr_mask))
38 return false;
39
40 return true;
41}
42
43static bool __maybe_unused
44is_affected_midr_range_list(const struct arm64_cpu_capabilities *entry,
45 int scope)
46{
47 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
48 return is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list);
49}
50
51static bool __maybe_unused
52is_kryo_midr(const struct arm64_cpu_capabilities *entry, int scope)
53{
54 u32 model;
55
56 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
57
58 model = read_cpuid_id();
59 model &= MIDR_IMPLEMENTOR_MASK | (0xf00 << MIDR_PARTNUM_SHIFT) |
60 MIDR_ARCHITECTURE_MASK;
61
62 return model == entry->midr_range.model;
63}
64
65static bool
66has_mismatched_cache_line_size(const struct arm64_cpu_capabilities *entry,
67 int scope)
68{
69 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
70 return (read_cpuid_cachetype() & arm64_ftr_reg_ctrel0.strict_mask) !=
71 (arm64_ftr_reg_ctrel0.sys_val & arm64_ftr_reg_ctrel0.strict_mask);
72}
73
74static void
75cpu_enable_trap_ctr_access(const struct arm64_cpu_capabilities *__unused)
76{
77 /* Clear SCTLR_EL1.UCT */
78 config_sctlr_el1(SCTLR_EL1_UCT, 0);
79}
80
81atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
82
83#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
84#include <asm/mmu_context.h>
85#include <asm/cacheflush.h>
86
87DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
88
89#ifdef CONFIG_KVM_INDIRECT_VECTORS
90extern char __smccc_workaround_1_smc_start[];
91extern char __smccc_workaround_1_smc_end[];
92
93static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
94 const char *hyp_vecs_end)
95{
96 void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
97 int i;
98
99 for (i = 0; i < SZ_2K; i += 0x80)
100 memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
101
102 flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
103}
104
105static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
106 const char *hyp_vecs_start,
107 const char *hyp_vecs_end)
108{
109 static DEFINE_SPINLOCK(bp_lock);
110 int cpu, slot = -1;
111
112 spin_lock(&bp_lock);
113 for_each_possible_cpu(cpu) {
114 if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
115 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
116 break;
117 }
118 }
119
120 if (slot == -1) {
121 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
122 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
123 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
124 }
125
126 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
127 __this_cpu_write(bp_hardening_data.fn, fn);
128 spin_unlock(&bp_lock);
129}
130#else
131#define __smccc_workaround_1_smc_start NULL
132#define __smccc_workaround_1_smc_end NULL
133
134static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
135 const char *hyp_vecs_start,
136 const char *hyp_vecs_end)
137{
138 __this_cpu_write(bp_hardening_data.fn, fn);
139}
140#endif /* CONFIG_KVM_INDIRECT_VECTORS */
141
142static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
143 bp_hardening_cb_t fn,
144 const char *hyp_vecs_start,
145 const char *hyp_vecs_end)
146{
147 u64 pfr0;
148
149 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
150 return;
151
152 pfr0 = read_cpuid(ID_AA64PFR0_EL1);
153 if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
154 return;
155
156 __install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
157}
158
159#include <uapi/linux/psci.h>
160#include <linux/arm-smccc.h>
161#include <linux/psci.h>
162
163static void call_smc_arch_workaround_1(void)
164{
165 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
166}
167
168static void call_hvc_arch_workaround_1(void)
169{
170 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
171}
172
173static void qcom_link_stack_sanitization(void)
174{
175 u64 tmp;
176
177 asm volatile("mov %0, x30 \n"
178 ".rept 16 \n"
179 "bl . + 4 \n"
180 ".endr \n"
181 "mov x30, %0 \n"
182 : "=&r" (tmp));
183}
184
185static void
186enable_smccc_arch_workaround_1(const struct arm64_cpu_capabilities *entry)
187{
188 bp_hardening_cb_t cb;
189 void *smccc_start, *smccc_end;
190 struct arm_smccc_res res;
191 u32 midr = read_cpuid_id();
192
193 if (!entry->matches(entry, SCOPE_LOCAL_CPU))
194 return;
195
196 if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
197 return;
198
199 switch (psci_ops.conduit) {
200 case PSCI_CONDUIT_HVC:
201 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
202 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
203 if ((int)res.a0 < 0)
204 return;
205 cb = call_hvc_arch_workaround_1;
206 /* This is a guest, no need to patch KVM vectors */
207 smccc_start = NULL;
208 smccc_end = NULL;
209 break;
210
211 case PSCI_CONDUIT_SMC:
212 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
213 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
214 if ((int)res.a0 < 0)
215 return;
216 cb = call_smc_arch_workaround_1;
217 smccc_start = __smccc_workaround_1_smc_start;
218 smccc_end = __smccc_workaround_1_smc_end;
219 break;
220
221 default:
222 return;
223 }
224
225 if (((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR) ||
226 ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1))
227 cb = qcom_link_stack_sanitization;
228
229 install_bp_hardening_cb(entry, cb, smccc_start, smccc_end);
230
231 return;
232}
233#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
234
235#define CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
236 .matches = is_affected_midr_range, \
237 .midr_range = MIDR_RANGE(model, v_min, r_min, v_max, r_max)
238
239#define CAP_MIDR_ALL_VERSIONS(model) \
240 .matches = is_affected_midr_range, \
241 .midr_range = MIDR_ALL_VERSIONS(model)
242
243#define MIDR_FIXED(rev, revidr_mask) \
244 .fixed_revs = (struct arm64_midr_revidr[]){{ (rev), (revidr_mask) }, {}}
245
246#define ERRATA_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
247 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
248 CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max)
249
250#define CAP_MIDR_RANGE_LIST(list) \
251 .matches = is_affected_midr_range_list, \
252 .midr_range_list = list
253
254/* Errata affecting a range of revisions of given model variant */
255#define ERRATA_MIDR_REV_RANGE(m, var, r_min, r_max) \
256 ERRATA_MIDR_RANGE(m, var, r_min, var, r_max)
257
258/* Errata affecting a single variant/revision of a model */
259#define ERRATA_MIDR_REV(model, var, rev) \
260 ERRATA_MIDR_RANGE(model, var, rev, var, rev)
261
262/* Errata affecting all variants/revisions of a given a model */
263#define ERRATA_MIDR_ALL_VERSIONS(model) \
264 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
265 CAP_MIDR_ALL_VERSIONS(model)
266
267/* Errata affecting a list of midr ranges, with same work around */
268#define ERRATA_MIDR_RANGE_LIST(midr_list) \
269 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
270 CAP_MIDR_RANGE_LIST(midr_list)
271
272/*
273 * Generic helper for handling capabilties with multiple (match,enable) pairs
274 * of call backs, sharing the same capability bit.
275 * Iterate over each entry to see if at least one matches.
276 */
277static bool __maybe_unused
278multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry, int scope)
279{
280 const struct arm64_cpu_capabilities *caps;
281
282 for (caps = entry->match_list; caps->matches; caps++)
283 if (caps->matches(caps, scope))
284 return true;
285
286 return false;
287}
288
289/*
290 * Take appropriate action for all matching entries in the shared capability
291 * entry.
292 */
293static void __maybe_unused
294multi_entry_cap_cpu_enable(const struct arm64_cpu_capabilities *entry)
295{
296 const struct arm64_cpu_capabilities *caps;
297
298 for (caps = entry->match_list; caps->matches; caps++)
299 if (caps->matches(caps, SCOPE_LOCAL_CPU) &&
300 caps->cpu_enable)
301 caps->cpu_enable(caps);
302}
303
304#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
305
306/*
307 * List of CPUs where we need to issue a psci call to
308 * harden the branch predictor.
309 */
310static const struct midr_range arm64_bp_harden_smccc_cpus[] = {
311 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
312 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
313 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
314 MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
315 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
316 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
317 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR_V1),
318 MIDR_ALL_VERSIONS(MIDR_QCOM_FALKOR),
319 MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER),
320 {},
321};
322
323#endif
324
325#ifdef CONFIG_HARDEN_EL2_VECTORS
326
327static const struct midr_range arm64_harden_el2_vectors[] = {
328 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
329 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
330 {},
331};
332
333#endif
334
335const struct arm64_cpu_capabilities arm64_errata[] = {
336#if defined(CONFIG_ARM64_ERRATUM_826319) || \
337 defined(CONFIG_ARM64_ERRATUM_827319) || \
338 defined(CONFIG_ARM64_ERRATUM_824069)
339 {
340 /* Cortex-A53 r0p[012] */
341 .desc = "ARM errata 826319, 827319, 824069",
342 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
343 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 2),
344 .cpu_enable = cpu_enable_cache_maint_trap,
345 },
346#endif
347#ifdef CONFIG_ARM64_ERRATUM_819472
348 {
349 /* Cortex-A53 r0p[01] */
350 .desc = "ARM errata 819472",
351 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
352 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 1),
353 .cpu_enable = cpu_enable_cache_maint_trap,
354 },
355#endif
356#ifdef CONFIG_ARM64_ERRATUM_832075
357 {
358 /* Cortex-A57 r0p0 - r1p2 */
359 .desc = "ARM erratum 832075",
360 .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
361 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
362 0, 0,
363 1, 2),
364 },
365#endif
366#ifdef CONFIG_ARM64_ERRATUM_834220
367 {
368 /* Cortex-A57 r0p0 - r1p2 */
369 .desc = "ARM erratum 834220",
370 .capability = ARM64_WORKAROUND_834220,
371 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
372 0, 0,
373 1, 2),
374 },
375#endif
376#ifdef CONFIG_ARM64_ERRATUM_843419
377 {
378 /* Cortex-A53 r0p[01234] */
379 .desc = "ARM erratum 843419",
380 .capability = ARM64_WORKAROUND_843419,
381 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
382 MIDR_FIXED(0x4, BIT(8)),
383 },
384#endif
385#ifdef CONFIG_ARM64_ERRATUM_845719
386 {
387 /* Cortex-A53 r0p[01234] */
388 .desc = "ARM erratum 845719",
389 .capability = ARM64_WORKAROUND_845719,
390 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
391 },
392#endif
393#ifdef CONFIG_CAVIUM_ERRATUM_23154
394 {
395 /* Cavium ThunderX, pass 1.x */
396 .desc = "Cavium erratum 23154",
397 .capability = ARM64_WORKAROUND_CAVIUM_23154,
398 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX, 0, 0, 1),
399 },
400#endif
401#ifdef CONFIG_CAVIUM_ERRATUM_27456
402 {
403 /* Cavium ThunderX, T88 pass 1.x - 2.1 */
404 .desc = "Cavium erratum 27456",
405 .capability = ARM64_WORKAROUND_CAVIUM_27456,
406 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
407 0, 0,
408 1, 1),
409 },
410 {
411 /* Cavium ThunderX, T81 pass 1.0 */
412 .desc = "Cavium erratum 27456",
413 .capability = ARM64_WORKAROUND_CAVIUM_27456,
414 ERRATA_MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
415 },
416#endif
417#ifdef CONFIG_CAVIUM_ERRATUM_30115
418 {
419 /* Cavium ThunderX, T88 pass 1.x - 2.2 */
420 .desc = "Cavium erratum 30115",
421 .capability = ARM64_WORKAROUND_CAVIUM_30115,
422 ERRATA_MIDR_RANGE(MIDR_THUNDERX,
423 0, 0,
424 1, 2),
425 },
426 {
427 /* Cavium ThunderX, T81 pass 1.0 - 1.2 */
428 .desc = "Cavium erratum 30115",
429 .capability = ARM64_WORKAROUND_CAVIUM_30115,
430 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX_81XX, 0, 0, 2),
431 },
432 {
433 /* Cavium ThunderX, T83 pass 1.0 */
434 .desc = "Cavium erratum 30115",
435 .capability = ARM64_WORKAROUND_CAVIUM_30115,
436 ERRATA_MIDR_REV(MIDR_THUNDERX_83XX, 0, 0),
437 },
438#endif
439 {
440 .desc = "Mismatched cache line size",
441 .capability = ARM64_MISMATCHED_CACHE_LINE_SIZE,
442 .matches = has_mismatched_cache_line_size,
443 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
444 .cpu_enable = cpu_enable_trap_ctr_access,
445 },
446#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
447 {
448 .desc = "Qualcomm Technologies Falkor erratum 1003",
449 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
450 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
451 },
452 {
453 .desc = "Qualcomm Technologies Kryo erratum 1003",
454 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
455 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
456 .midr_range.model = MIDR_QCOM_KRYO,
457 .matches = is_kryo_midr,
458 },
459#endif
460#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
461 {
462 .desc = "Qualcomm Technologies Falkor erratum 1009",
463 .capability = ARM64_WORKAROUND_REPEAT_TLBI,
464 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
465 },
466#endif
467#ifdef CONFIG_ARM64_ERRATUM_858921
468 {
469 /* Cortex-A73 all versions */
470 .desc = "ARM erratum 858921",
471 .capability = ARM64_WORKAROUND_858921,
472 ERRATA_MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
473 },
474#endif
475#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
476 {
477 .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
478 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
479 .cpu_enable = enable_smccc_arch_workaround_1,
480 ERRATA_MIDR_RANGE_LIST(arm64_bp_harden_smccc_cpus),
481 },
482#endif
483#ifdef CONFIG_HARDEN_EL2_VECTORS
484 {
485 .desc = "EL2 vector hardening",
486 .capability = ARM64_HARDEN_EL2_VECTORS,
487 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
488 ERRATA_MIDR_RANGE_LIST(arm64_harden_el2_vectors),
489 },
490#endif
491 {
492 }
493};
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Contains CPU specific errata definitions
4 *
5 * Copyright (C) 2014 ARM Ltd.
6 */
7
8#include <linux/arm-smccc.h>
9#include <linux/psci.h>
10#include <linux/types.h>
11#include <linux/cpu.h>
12#include <asm/cpu.h>
13#include <asm/cputype.h>
14#include <asm/cpufeature.h>
15#include <asm/smp_plat.h>
16
17static bool __maybe_unused
18is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
19{
20 const struct arm64_midr_revidr *fix;
21 u32 midr = read_cpuid_id(), revidr;
22
23 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
24 if (!is_midr_in_range(midr, &entry->midr_range))
25 return false;
26
27 midr &= MIDR_REVISION_MASK | MIDR_VARIANT_MASK;
28 revidr = read_cpuid(REVIDR_EL1);
29 for (fix = entry->fixed_revs; fix && fix->revidr_mask; fix++)
30 if (midr == fix->midr_rv && (revidr & fix->revidr_mask))
31 return false;
32
33 return true;
34}
35
36static bool __maybe_unused
37is_affected_midr_range_list(const struct arm64_cpu_capabilities *entry,
38 int scope)
39{
40 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
41 return is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list);
42}
43
44static bool __maybe_unused
45is_kryo_midr(const struct arm64_cpu_capabilities *entry, int scope)
46{
47 u32 model;
48
49 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
50
51 model = read_cpuid_id();
52 model &= MIDR_IMPLEMENTOR_MASK | (0xf00 << MIDR_PARTNUM_SHIFT) |
53 MIDR_ARCHITECTURE_MASK;
54
55 return model == entry->midr_range.model;
56}
57
58static bool
59has_mismatched_cache_type(const struct arm64_cpu_capabilities *entry,
60 int scope)
61{
62 u64 mask = arm64_ftr_reg_ctrel0.strict_mask;
63 u64 sys = arm64_ftr_reg_ctrel0.sys_val & mask;
64 u64 ctr_raw, ctr_real;
65
66 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
67
68 /*
69 * We want to make sure that all the CPUs in the system expose
70 * a consistent CTR_EL0 to make sure that applications behaves
71 * correctly with migration.
72 *
73 * If a CPU has CTR_EL0.IDC but does not advertise it via CTR_EL0 :
74 *
75 * 1) It is safe if the system doesn't support IDC, as CPU anyway
76 * reports IDC = 0, consistent with the rest.
77 *
78 * 2) If the system has IDC, it is still safe as we trap CTR_EL0
79 * access on this CPU via the ARM64_HAS_CACHE_IDC capability.
80 *
81 * So, we need to make sure either the raw CTR_EL0 or the effective
82 * CTR_EL0 matches the system's copy to allow a secondary CPU to boot.
83 */
84 ctr_raw = read_cpuid_cachetype() & mask;
85 ctr_real = read_cpuid_effective_cachetype() & mask;
86
87 return (ctr_real != sys) && (ctr_raw != sys);
88}
89
90static void
91cpu_enable_trap_ctr_access(const struct arm64_cpu_capabilities *__unused)
92{
93 u64 mask = arm64_ftr_reg_ctrel0.strict_mask;
94
95 /* Trap CTR_EL0 access on this CPU, only if it has a mismatch */
96 if ((read_cpuid_cachetype() & mask) !=
97 (arm64_ftr_reg_ctrel0.sys_val & mask))
98 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
99}
100
101atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
102
103#include <asm/mmu_context.h>
104#include <asm/cacheflush.h>
105
106DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
107
108#ifdef CONFIG_KVM_INDIRECT_VECTORS
109extern char __smccc_workaround_1_smc_start[];
110extern char __smccc_workaround_1_smc_end[];
111
112static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
113 const char *hyp_vecs_end)
114{
115 void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
116 int i;
117
118 for (i = 0; i < SZ_2K; i += 0x80)
119 memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
120
121 __flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
122}
123
124static void install_bp_hardening_cb(bp_hardening_cb_t fn,
125 const char *hyp_vecs_start,
126 const char *hyp_vecs_end)
127{
128 static DEFINE_RAW_SPINLOCK(bp_lock);
129 int cpu, slot = -1;
130
131 /*
132 * detect_harden_bp_fw() passes NULL for the hyp_vecs start/end if
133 * we're a guest. Skip the hyp-vectors work.
134 */
135 if (!hyp_vecs_start) {
136 __this_cpu_write(bp_hardening_data.fn, fn);
137 return;
138 }
139
140 raw_spin_lock(&bp_lock);
141 for_each_possible_cpu(cpu) {
142 if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
143 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
144 break;
145 }
146 }
147
148 if (slot == -1) {
149 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
150 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
151 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
152 }
153
154 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
155 __this_cpu_write(bp_hardening_data.fn, fn);
156 raw_spin_unlock(&bp_lock);
157}
158#else
159#define __smccc_workaround_1_smc_start NULL
160#define __smccc_workaround_1_smc_end NULL
161
162static void install_bp_hardening_cb(bp_hardening_cb_t fn,
163 const char *hyp_vecs_start,
164 const char *hyp_vecs_end)
165{
166 __this_cpu_write(bp_hardening_data.fn, fn);
167}
168#endif /* CONFIG_KVM_INDIRECT_VECTORS */
169
170#include <uapi/linux/psci.h>
171#include <linux/arm-smccc.h>
172#include <linux/psci.h>
173
174static void call_smc_arch_workaround_1(void)
175{
176 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
177}
178
179static void call_hvc_arch_workaround_1(void)
180{
181 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
182}
183
184static void qcom_link_stack_sanitization(void)
185{
186 u64 tmp;
187
188 asm volatile("mov %0, x30 \n"
189 ".rept 16 \n"
190 "bl . + 4 \n"
191 ".endr \n"
192 "mov x30, %0 \n"
193 : "=&r" (tmp));
194}
195
196static bool __nospectre_v2;
197static int __init parse_nospectre_v2(char *str)
198{
199 __nospectre_v2 = true;
200 return 0;
201}
202early_param("nospectre_v2", parse_nospectre_v2);
203
204/*
205 * -1: No workaround
206 * 0: No workaround required
207 * 1: Workaround installed
208 */
209static int detect_harden_bp_fw(void)
210{
211 bp_hardening_cb_t cb;
212 void *smccc_start, *smccc_end;
213 struct arm_smccc_res res;
214 u32 midr = read_cpuid_id();
215
216 if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
217 return -1;
218
219 switch (psci_ops.conduit) {
220 case PSCI_CONDUIT_HVC:
221 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
222 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
223 switch ((int)res.a0) {
224 case 1:
225 /* Firmware says we're just fine */
226 return 0;
227 case 0:
228 cb = call_hvc_arch_workaround_1;
229 /* This is a guest, no need to patch KVM vectors */
230 smccc_start = NULL;
231 smccc_end = NULL;
232 break;
233 default:
234 return -1;
235 }
236 break;
237
238 case PSCI_CONDUIT_SMC:
239 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
240 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
241 switch ((int)res.a0) {
242 case 1:
243 /* Firmware says we're just fine */
244 return 0;
245 case 0:
246 cb = call_smc_arch_workaround_1;
247 smccc_start = __smccc_workaround_1_smc_start;
248 smccc_end = __smccc_workaround_1_smc_end;
249 break;
250 default:
251 return -1;
252 }
253 break;
254
255 default:
256 return -1;
257 }
258
259 if (((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR) ||
260 ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1))
261 cb = qcom_link_stack_sanitization;
262
263 if (IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR))
264 install_bp_hardening_cb(cb, smccc_start, smccc_end);
265
266 return 1;
267}
268
269DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
270
271int ssbd_state __read_mostly = ARM64_SSBD_KERNEL;
272static bool __ssb_safe = true;
273
274static const struct ssbd_options {
275 const char *str;
276 int state;
277} ssbd_options[] = {
278 { "force-on", ARM64_SSBD_FORCE_ENABLE, },
279 { "force-off", ARM64_SSBD_FORCE_DISABLE, },
280 { "kernel", ARM64_SSBD_KERNEL, },
281};
282
283static int __init ssbd_cfg(char *buf)
284{
285 int i;
286
287 if (!buf || !buf[0])
288 return -EINVAL;
289
290 for (i = 0; i < ARRAY_SIZE(ssbd_options); i++) {
291 int len = strlen(ssbd_options[i].str);
292
293 if (strncmp(buf, ssbd_options[i].str, len))
294 continue;
295
296 ssbd_state = ssbd_options[i].state;
297 return 0;
298 }
299
300 return -EINVAL;
301}
302early_param("ssbd", ssbd_cfg);
303
304void __init arm64_update_smccc_conduit(struct alt_instr *alt,
305 __le32 *origptr, __le32 *updptr,
306 int nr_inst)
307{
308 u32 insn;
309
310 BUG_ON(nr_inst != 1);
311
312 switch (psci_ops.conduit) {
313 case PSCI_CONDUIT_HVC:
314 insn = aarch64_insn_get_hvc_value();
315 break;
316 case PSCI_CONDUIT_SMC:
317 insn = aarch64_insn_get_smc_value();
318 break;
319 default:
320 return;
321 }
322
323 *updptr = cpu_to_le32(insn);
324}
325
326void __init arm64_enable_wa2_handling(struct alt_instr *alt,
327 __le32 *origptr, __le32 *updptr,
328 int nr_inst)
329{
330 BUG_ON(nr_inst != 1);
331 /*
332 * Only allow mitigation on EL1 entry/exit and guest
333 * ARCH_WORKAROUND_2 handling if the SSBD state allows it to
334 * be flipped.
335 */
336 if (arm64_get_ssbd_state() == ARM64_SSBD_KERNEL)
337 *updptr = cpu_to_le32(aarch64_insn_gen_nop());
338}
339
340void arm64_set_ssbd_mitigation(bool state)
341{
342 if (!IS_ENABLED(CONFIG_ARM64_SSBD)) {
343 pr_info_once("SSBD disabled by kernel configuration\n");
344 return;
345 }
346
347 if (this_cpu_has_cap(ARM64_SSBS)) {
348 if (state)
349 asm volatile(SET_PSTATE_SSBS(0));
350 else
351 asm volatile(SET_PSTATE_SSBS(1));
352 return;
353 }
354
355 switch (psci_ops.conduit) {
356 case PSCI_CONDUIT_HVC:
357 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
358 break;
359
360 case PSCI_CONDUIT_SMC:
361 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
362 break;
363
364 default:
365 WARN_ON_ONCE(1);
366 break;
367 }
368}
369
370static bool has_ssbd_mitigation(const struct arm64_cpu_capabilities *entry,
371 int scope)
372{
373 struct arm_smccc_res res;
374 bool required = true;
375 s32 val;
376 bool this_cpu_safe = false;
377
378 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
379
380 if (cpu_mitigations_off())
381 ssbd_state = ARM64_SSBD_FORCE_DISABLE;
382
383 /* delay setting __ssb_safe until we get a firmware response */
384 if (is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list))
385 this_cpu_safe = true;
386
387 if (this_cpu_has_cap(ARM64_SSBS)) {
388 if (!this_cpu_safe)
389 __ssb_safe = false;
390 required = false;
391 goto out_printmsg;
392 }
393
394 if (psci_ops.smccc_version == SMCCC_VERSION_1_0) {
395 ssbd_state = ARM64_SSBD_UNKNOWN;
396 if (!this_cpu_safe)
397 __ssb_safe = false;
398 return false;
399 }
400
401 switch (psci_ops.conduit) {
402 case PSCI_CONDUIT_HVC:
403 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
404 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
405 break;
406
407 case PSCI_CONDUIT_SMC:
408 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
409 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
410 break;
411
412 default:
413 ssbd_state = ARM64_SSBD_UNKNOWN;
414 if (!this_cpu_safe)
415 __ssb_safe = false;
416 return false;
417 }
418
419 val = (s32)res.a0;
420
421 switch (val) {
422 case SMCCC_RET_NOT_SUPPORTED:
423 ssbd_state = ARM64_SSBD_UNKNOWN;
424 if (!this_cpu_safe)
425 __ssb_safe = false;
426 return false;
427
428 /* machines with mixed mitigation requirements must not return this */
429 case SMCCC_RET_NOT_REQUIRED:
430 pr_info_once("%s mitigation not required\n", entry->desc);
431 ssbd_state = ARM64_SSBD_MITIGATED;
432 return false;
433
434 case SMCCC_RET_SUCCESS:
435 __ssb_safe = false;
436 required = true;
437 break;
438
439 case 1: /* Mitigation not required on this CPU */
440 required = false;
441 break;
442
443 default:
444 WARN_ON(1);
445 if (!this_cpu_safe)
446 __ssb_safe = false;
447 return false;
448 }
449
450 switch (ssbd_state) {
451 case ARM64_SSBD_FORCE_DISABLE:
452 arm64_set_ssbd_mitigation(false);
453 required = false;
454 break;
455
456 case ARM64_SSBD_KERNEL:
457 if (required) {
458 __this_cpu_write(arm64_ssbd_callback_required, 1);
459 arm64_set_ssbd_mitigation(true);
460 }
461 break;
462
463 case ARM64_SSBD_FORCE_ENABLE:
464 arm64_set_ssbd_mitigation(true);
465 required = true;
466 break;
467
468 default:
469 WARN_ON(1);
470 break;
471 }
472
473out_printmsg:
474 switch (ssbd_state) {
475 case ARM64_SSBD_FORCE_DISABLE:
476 pr_info_once("%s disabled from command-line\n", entry->desc);
477 break;
478
479 case ARM64_SSBD_FORCE_ENABLE:
480 pr_info_once("%s forced from command-line\n", entry->desc);
481 break;
482 }
483
484 return required;
485}
486
487/* known invulnerable cores */
488static const struct midr_range arm64_ssb_cpus[] = {
489 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
490 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
491 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
492 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
493 {},
494};
495
496#ifdef CONFIG_ARM64_ERRATUM_1463225
497DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
498
499static bool
500has_cortex_a76_erratum_1463225(const struct arm64_cpu_capabilities *entry,
501 int scope)
502{
503 u32 midr = read_cpuid_id();
504 /* Cortex-A76 r0p0 - r3p1 */
505 struct midr_range range = MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 3, 1);
506
507 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
508 return is_midr_in_range(midr, &range) && is_kernel_in_hyp_mode();
509}
510#endif
511
512static void __maybe_unused
513cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
514{
515 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCI, 0);
516}
517
518#define CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
519 .matches = is_affected_midr_range, \
520 .midr_range = MIDR_RANGE(model, v_min, r_min, v_max, r_max)
521
522#define CAP_MIDR_ALL_VERSIONS(model) \
523 .matches = is_affected_midr_range, \
524 .midr_range = MIDR_ALL_VERSIONS(model)
525
526#define MIDR_FIXED(rev, revidr_mask) \
527 .fixed_revs = (struct arm64_midr_revidr[]){{ (rev), (revidr_mask) }, {}}
528
529#define ERRATA_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
530 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
531 CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max)
532
533#define CAP_MIDR_RANGE_LIST(list) \
534 .matches = is_affected_midr_range_list, \
535 .midr_range_list = list
536
537/* Errata affecting a range of revisions of given model variant */
538#define ERRATA_MIDR_REV_RANGE(m, var, r_min, r_max) \
539 ERRATA_MIDR_RANGE(m, var, r_min, var, r_max)
540
541/* Errata affecting a single variant/revision of a model */
542#define ERRATA_MIDR_REV(model, var, rev) \
543 ERRATA_MIDR_RANGE(model, var, rev, var, rev)
544
545/* Errata affecting all variants/revisions of a given a model */
546#define ERRATA_MIDR_ALL_VERSIONS(model) \
547 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
548 CAP_MIDR_ALL_VERSIONS(model)
549
550/* Errata affecting a list of midr ranges, with same work around */
551#define ERRATA_MIDR_RANGE_LIST(midr_list) \
552 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
553 CAP_MIDR_RANGE_LIST(midr_list)
554
555/* Track overall mitigation state. We are only mitigated if all cores are ok */
556static bool __hardenbp_enab = true;
557static bool __spectrev2_safe = true;
558
559int get_spectre_v2_workaround_state(void)
560{
561 if (__spectrev2_safe)
562 return ARM64_BP_HARDEN_NOT_REQUIRED;
563
564 if (!__hardenbp_enab)
565 return ARM64_BP_HARDEN_UNKNOWN;
566
567 return ARM64_BP_HARDEN_WA_NEEDED;
568}
569
570/*
571 * List of CPUs that do not need any Spectre-v2 mitigation at all.
572 */
573static const struct midr_range spectre_v2_safe_list[] = {
574 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
575 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
576 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
577 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
578 { /* sentinel */ }
579};
580
581/*
582 * Track overall bp hardening for all heterogeneous cores in the machine.
583 * We are only considered "safe" if all booted cores are known safe.
584 */
585static bool __maybe_unused
586check_branch_predictor(const struct arm64_cpu_capabilities *entry, int scope)
587{
588 int need_wa;
589
590 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
591
592 /* If the CPU has CSV2 set, we're safe */
593 if (cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64PFR0_EL1),
594 ID_AA64PFR0_CSV2_SHIFT))
595 return false;
596
597 /* Alternatively, we have a list of unaffected CPUs */
598 if (is_midr_in_range_list(read_cpuid_id(), spectre_v2_safe_list))
599 return false;
600
601 /* Fallback to firmware detection */
602 need_wa = detect_harden_bp_fw();
603 if (!need_wa)
604 return false;
605
606 __spectrev2_safe = false;
607
608 if (!IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR)) {
609 pr_warn_once("spectrev2 mitigation disabled by kernel configuration\n");
610 __hardenbp_enab = false;
611 return false;
612 }
613
614 /* forced off */
615 if (__nospectre_v2 || cpu_mitigations_off()) {
616 pr_info_once("spectrev2 mitigation disabled by command line option\n");
617 __hardenbp_enab = false;
618 return false;
619 }
620
621 if (need_wa < 0) {
622 pr_warn_once("ARM_SMCCC_ARCH_WORKAROUND_1 missing from firmware\n");
623 __hardenbp_enab = false;
624 }
625
626 return (need_wa > 0);
627}
628
629static const __maybe_unused struct midr_range tx2_family_cpus[] = {
630 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
631 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
632 {},
633};
634
635static bool __maybe_unused
636needs_tx2_tvm_workaround(const struct arm64_cpu_capabilities *entry,
637 int scope)
638{
639 int i;
640
641 if (!is_affected_midr_range_list(entry, scope) ||
642 !is_hyp_mode_available())
643 return false;
644
645 for_each_possible_cpu(i) {
646 if (MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0) != 0)
647 return true;
648 }
649
650 return false;
651}
652
653#ifdef CONFIG_HARDEN_EL2_VECTORS
654
655static const struct midr_range arm64_harden_el2_vectors[] = {
656 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
657 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
658 {},
659};
660
661#endif
662
663#ifdef CONFIG_ARM64_WORKAROUND_REPEAT_TLBI
664static const struct arm64_cpu_capabilities arm64_repeat_tlbi_list[] = {
665#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
666 {
667 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0)
668 },
669 {
670 .midr_range.model = MIDR_QCOM_KRYO,
671 .matches = is_kryo_midr,
672 },
673#endif
674#ifdef CONFIG_ARM64_ERRATUM_1286807
675 {
676 ERRATA_MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 3, 0),
677 },
678#endif
679 {},
680};
681#endif
682
683#ifdef CONFIG_CAVIUM_ERRATUM_27456
684const struct midr_range cavium_erratum_27456_cpus[] = {
685 /* Cavium ThunderX, T88 pass 1.x - 2.1 */
686 MIDR_RANGE(MIDR_THUNDERX, 0, 0, 1, 1),
687 /* Cavium ThunderX, T81 pass 1.0 */
688 MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
689 {},
690};
691#endif
692
693#ifdef CONFIG_CAVIUM_ERRATUM_30115
694static const struct midr_range cavium_erratum_30115_cpus[] = {
695 /* Cavium ThunderX, T88 pass 1.x - 2.2 */
696 MIDR_RANGE(MIDR_THUNDERX, 0, 0, 1, 2),
697 /* Cavium ThunderX, T81 pass 1.0 - 1.2 */
698 MIDR_REV_RANGE(MIDR_THUNDERX_81XX, 0, 0, 2),
699 /* Cavium ThunderX, T83 pass 1.0 */
700 MIDR_REV(MIDR_THUNDERX_83XX, 0, 0),
701 {},
702};
703#endif
704
705#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
706static const struct arm64_cpu_capabilities qcom_erratum_1003_list[] = {
707 {
708 ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
709 },
710 {
711 .midr_range.model = MIDR_QCOM_KRYO,
712 .matches = is_kryo_midr,
713 },
714 {},
715};
716#endif
717
718#ifdef CONFIG_ARM64_WORKAROUND_CLEAN_CACHE
719static const struct midr_range workaround_clean_cache[] = {
720#if defined(CONFIG_ARM64_ERRATUM_826319) || \
721 defined(CONFIG_ARM64_ERRATUM_827319) || \
722 defined(CONFIG_ARM64_ERRATUM_824069)
723 /* Cortex-A53 r0p[012]: ARM errata 826319, 827319, 824069 */
724 MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 2),
725#endif
726#ifdef CONFIG_ARM64_ERRATUM_819472
727 /* Cortex-A53 r0p[01] : ARM errata 819472 */
728 MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 1),
729#endif
730 {},
731};
732#endif
733
734#ifdef CONFIG_ARM64_ERRATUM_1418040
735/*
736 * - 1188873 affects r0p0 to r2p0
737 * - 1418040 affects r0p0 to r3p1
738 */
739static const struct midr_range erratum_1418040_list[] = {
740 /* Cortex-A76 r0p0 to r3p1 */
741 MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 3, 1),
742 /* Neoverse-N1 r0p0 to r3p1 */
743 MIDR_RANGE(MIDR_NEOVERSE_N1, 0, 0, 3, 1),
744 {},
745};
746#endif
747
748#ifdef CONFIG_ARM64_ERRATUM_845719
749static const struct midr_range erratum_845719_list[] = {
750 /* Cortex-A53 r0p[01234] */
751 MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
752 /* Brahma-B53 r0p[0] */
753 MIDR_REV(MIDR_BRAHMA_B53, 0, 0),
754 {},
755};
756#endif
757
758#ifdef CONFIG_ARM64_ERRATUM_843419
759static const struct arm64_cpu_capabilities erratum_843419_list[] = {
760 {
761 /* Cortex-A53 r0p[01234] */
762 .matches = is_affected_midr_range,
763 ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
764 MIDR_FIXED(0x4, BIT(8)),
765 },
766 {
767 /* Brahma-B53 r0p[0] */
768 .matches = is_affected_midr_range,
769 ERRATA_MIDR_REV(MIDR_BRAHMA_B53, 0, 0),
770 },
771 {},
772};
773#endif
774
775const struct arm64_cpu_capabilities arm64_errata[] = {
776#ifdef CONFIG_ARM64_WORKAROUND_CLEAN_CACHE
777 {
778 .desc = "ARM errata 826319, 827319, 824069, 819472",
779 .capability = ARM64_WORKAROUND_CLEAN_CACHE,
780 ERRATA_MIDR_RANGE_LIST(workaround_clean_cache),
781 .cpu_enable = cpu_enable_cache_maint_trap,
782 },
783#endif
784#ifdef CONFIG_ARM64_ERRATUM_832075
785 {
786 /* Cortex-A57 r0p0 - r1p2 */
787 .desc = "ARM erratum 832075",
788 .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
789 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
790 0, 0,
791 1, 2),
792 },
793#endif
794#ifdef CONFIG_ARM64_ERRATUM_834220
795 {
796 /* Cortex-A57 r0p0 - r1p2 */
797 .desc = "ARM erratum 834220",
798 .capability = ARM64_WORKAROUND_834220,
799 ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
800 0, 0,
801 1, 2),
802 },
803#endif
804#ifdef CONFIG_ARM64_ERRATUM_843419
805 {
806 .desc = "ARM erratum 843419",
807 .capability = ARM64_WORKAROUND_843419,
808 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
809 .matches = cpucap_multi_entry_cap_matches,
810 .match_list = erratum_843419_list,
811 },
812#endif
813#ifdef CONFIG_ARM64_ERRATUM_845719
814 {
815 .desc = "ARM erratum 845719",
816 .capability = ARM64_WORKAROUND_845719,
817 ERRATA_MIDR_RANGE_LIST(erratum_845719_list),
818 },
819#endif
820#ifdef CONFIG_CAVIUM_ERRATUM_23154
821 {
822 /* Cavium ThunderX, pass 1.x */
823 .desc = "Cavium erratum 23154",
824 .capability = ARM64_WORKAROUND_CAVIUM_23154,
825 ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX, 0, 0, 1),
826 },
827#endif
828#ifdef CONFIG_CAVIUM_ERRATUM_27456
829 {
830 .desc = "Cavium erratum 27456",
831 .capability = ARM64_WORKAROUND_CAVIUM_27456,
832 ERRATA_MIDR_RANGE_LIST(cavium_erratum_27456_cpus),
833 },
834#endif
835#ifdef CONFIG_CAVIUM_ERRATUM_30115
836 {
837 .desc = "Cavium erratum 30115",
838 .capability = ARM64_WORKAROUND_CAVIUM_30115,
839 ERRATA_MIDR_RANGE_LIST(cavium_erratum_30115_cpus),
840 },
841#endif
842 {
843 .desc = "Mismatched cache type (CTR_EL0)",
844 .capability = ARM64_MISMATCHED_CACHE_TYPE,
845 .matches = has_mismatched_cache_type,
846 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
847 .cpu_enable = cpu_enable_trap_ctr_access,
848 },
849#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
850 {
851 .desc = "Qualcomm Technologies Falkor/Kryo erratum 1003",
852 .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
853 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
854 .matches = cpucap_multi_entry_cap_matches,
855 .match_list = qcom_erratum_1003_list,
856 },
857#endif
858#ifdef CONFIG_ARM64_WORKAROUND_REPEAT_TLBI
859 {
860 .desc = "Qualcomm erratum 1009, ARM erratum 1286807",
861 .capability = ARM64_WORKAROUND_REPEAT_TLBI,
862 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
863 .matches = cpucap_multi_entry_cap_matches,
864 .match_list = arm64_repeat_tlbi_list,
865 },
866#endif
867#ifdef CONFIG_ARM64_ERRATUM_858921
868 {
869 /* Cortex-A73 all versions */
870 .desc = "ARM erratum 858921",
871 .capability = ARM64_WORKAROUND_858921,
872 ERRATA_MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
873 },
874#endif
875 {
876 .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
877 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
878 .matches = check_branch_predictor,
879 },
880#ifdef CONFIG_HARDEN_EL2_VECTORS
881 {
882 .desc = "EL2 vector hardening",
883 .capability = ARM64_HARDEN_EL2_VECTORS,
884 ERRATA_MIDR_RANGE_LIST(arm64_harden_el2_vectors),
885 },
886#endif
887 {
888 .desc = "Speculative Store Bypass Disable",
889 .capability = ARM64_SSBD,
890 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
891 .matches = has_ssbd_mitigation,
892 .midr_range_list = arm64_ssb_cpus,
893 },
894#ifdef CONFIG_ARM64_ERRATUM_1418040
895 {
896 .desc = "ARM erratum 1418040",
897 .capability = ARM64_WORKAROUND_1418040,
898 ERRATA_MIDR_RANGE_LIST(erratum_1418040_list),
899 },
900#endif
901#ifdef CONFIG_ARM64_ERRATUM_1165522
902 {
903 /* Cortex-A76 r0p0 to r2p0 */
904 .desc = "ARM erratum 1165522",
905 .capability = ARM64_WORKAROUND_1165522,
906 ERRATA_MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 2, 0),
907 },
908#endif
909#ifdef CONFIG_ARM64_ERRATUM_1463225
910 {
911 .desc = "ARM erratum 1463225",
912 .capability = ARM64_WORKAROUND_1463225,
913 .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
914 .matches = has_cortex_a76_erratum_1463225,
915 },
916#endif
917#ifdef CONFIG_CAVIUM_TX2_ERRATUM_219
918 {
919 .desc = "Cavium ThunderX2 erratum 219 (KVM guest sysreg trapping)",
920 .capability = ARM64_WORKAROUND_CAVIUM_TX2_219_TVM,
921 ERRATA_MIDR_RANGE_LIST(tx2_family_cpus),
922 .matches = needs_tx2_tvm_workaround,
923 },
924 {
925 .desc = "Cavium ThunderX2 erratum 219 (PRFM removal)",
926 .capability = ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM,
927 ERRATA_MIDR_RANGE_LIST(tx2_family_cpus),
928 },
929#endif
930 {
931 }
932};
933
934ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr,
935 char *buf)
936{
937 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
938}
939
940ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr,
941 char *buf)
942{
943 switch (get_spectre_v2_workaround_state()) {
944 case ARM64_BP_HARDEN_NOT_REQUIRED:
945 return sprintf(buf, "Not affected\n");
946 case ARM64_BP_HARDEN_WA_NEEDED:
947 return sprintf(buf, "Mitigation: Branch predictor hardening\n");
948 case ARM64_BP_HARDEN_UNKNOWN:
949 default:
950 return sprintf(buf, "Vulnerable\n");
951 }
952}
953
954ssize_t cpu_show_spec_store_bypass(struct device *dev,
955 struct device_attribute *attr, char *buf)
956{
957 if (__ssb_safe)
958 return sprintf(buf, "Not affected\n");
959
960 switch (ssbd_state) {
961 case ARM64_SSBD_KERNEL:
962 case ARM64_SSBD_FORCE_ENABLE:
963 if (IS_ENABLED(CONFIG_ARM64_SSBD))
964 return sprintf(buf,
965 "Mitigation: Speculative Store Bypass disabled via prctl\n");
966 }
967
968 return sprintf(buf, "Vulnerable\n");
969}