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