blob: 85a2e3d6263c27c0d037b7149b3a5fd58e7d4b35 [file] [log] [blame]
Russell Kinge388b802018-05-10 13:09:54 +01001// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/smp.h>
4
Russell Kingf5fe12b2018-05-14 14:20:21 +01005#include <asm/cp15.h>
6#include <asm/cputype.h>
7#include <asm/system_misc.h>
8
9#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
10DEFINE_PER_CPU(harden_branch_predictor_fn_t, harden_branch_predictor_fn);
11
12static void harden_branch_predictor_bpiall(void)
13{
14 write_sysreg(0, BPIALL);
15}
16
17static void harden_branch_predictor_iciallu(void)
18{
19 write_sysreg(0, ICIALLU);
20}
21
22static void cpu_v7_spectre_init(void)
23{
24 const char *spectre_v2_method = NULL;
25 int cpu = smp_processor_id();
26
27 if (per_cpu(harden_branch_predictor_fn, cpu))
28 return;
29
30 switch (read_cpuid_part()) {
31 case ARM_CPU_PART_CORTEX_A8:
32 case ARM_CPU_PART_CORTEX_A9:
33 case ARM_CPU_PART_CORTEX_A12:
34 case ARM_CPU_PART_CORTEX_A17:
35 case ARM_CPU_PART_CORTEX_A73:
36 case ARM_CPU_PART_CORTEX_A75:
37 per_cpu(harden_branch_predictor_fn, cpu) =
38 harden_branch_predictor_bpiall;
39 spectre_v2_method = "BPIALL";
40 break;
41
42 case ARM_CPU_PART_CORTEX_A15:
43 case ARM_CPU_PART_BRAHMA_B15:
44 per_cpu(harden_branch_predictor_fn, cpu) =
45 harden_branch_predictor_iciallu;
46 spectre_v2_method = "ICIALLU";
47 break;
48 }
49 if (spectre_v2_method)
50 pr_info("CPU%u: Spectre v2: using %s workaround\n",
51 smp_processor_id(), spectre_v2_method);
52}
53#else
54static void cpu_v7_spectre_init(void)
55{
56}
57#endif
58
59static __maybe_unused bool cpu_v7_check_auxcr_set(bool *warned,
Russell Kinge388b802018-05-10 13:09:54 +010060 u32 mask, const char *msg)
61{
62 u32 aux_cr;
63
64 asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (aux_cr));
65
66 if ((aux_cr & mask) != mask) {
67 if (!*warned)
68 pr_err("CPU%u: %s", smp_processor_id(), msg);
69 *warned = true;
Russell Kingf5fe12b2018-05-14 14:20:21 +010070 return false;
Russell Kinge388b802018-05-10 13:09:54 +010071 }
Russell Kingf5fe12b2018-05-14 14:20:21 +010072 return true;
Russell Kinge388b802018-05-10 13:09:54 +010073}
74
75static DEFINE_PER_CPU(bool, spectre_warned);
76
Russell Kingf5fe12b2018-05-14 14:20:21 +010077static bool check_spectre_auxcr(bool *warned, u32 bit)
Russell Kinge388b802018-05-10 13:09:54 +010078{
Russell Kingf5fe12b2018-05-14 14:20:21 +010079 return IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR) &&
Russell Kinge388b802018-05-10 13:09:54 +010080 cpu_v7_check_auxcr_set(warned, bit,
81 "Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable\n");
82}
83
84void cpu_v7_ca8_ibe(void)
85{
Russell Kingf5fe12b2018-05-14 14:20:21 +010086 if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(6)))
87 cpu_v7_spectre_init();
Russell Kinge388b802018-05-10 13:09:54 +010088}
89
90void cpu_v7_ca15_ibe(void)
91{
Russell Kingf5fe12b2018-05-14 14:20:21 +010092 if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(0)))
93 cpu_v7_spectre_init();
94}
95
96void cpu_v7_bugs_init(void)
97{
98 cpu_v7_spectre_init();
Russell Kinge388b802018-05-10 13:09:54 +010099}