1 // SPDX-License-Identifier: GPL-2.0
3 * Early cpufeature override framework
5 * Copyright (C) 2020 Google LLC
6 * Author: Marc Zyngier <maz@kernel.org>
9 #include <linux/ctype.h>
10 #include <linux/kernel.h>
11 #include <linux/libfdt.h>
13 #include <asm/cacheflush.h>
14 #include <asm/cpufeature.h>
15 #include <asm/setup.h>
17 #define FTR_DESC_NAME_LEN 20
18 #define FTR_DESC_FIELD_LEN 10
19 #define FTR_ALIAS_NAME_LEN 30
20 #define FTR_ALIAS_OPTION_LEN 116
22 static u64 __boot_status __initdata;
25 char name[FTR_DESC_NAME_LEN];
26 struct arm64_ftr_override *override;
28 char name[FTR_DESC_FIELD_LEN];
31 bool (*filter)(u64 val);
35 #define FIELD(n, s, f) { .name = n, .shift = s, .width = 4, .filter = f }
37 static bool __init mmfr1_vh_filter(u64 val)
40 * If we ever reach this point while running VHE, we're
41 * guaranteed to be on one of these funky, VHE-stuck CPUs. If
42 * the user was trying to force nVHE on us, proceed with
43 * attitude adjustment.
45 return !(__boot_status == (BOOT_CPU_FLAG_E2H | BOOT_CPU_MODE_EL2) &&
49 static const struct ftr_set_desc mmfr1 __initconst = {
50 .name = "id_aa64mmfr1",
51 .override = &id_aa64mmfr1_override,
53 FIELD("vh", ID_AA64MMFR1_EL1_VH_SHIFT, mmfr1_vh_filter),
58 static bool __init pfr0_sve_filter(u64 val)
61 * Disabling SVE also means disabling all the features that
62 * are associated with it. The easiest way to do it is just to
63 * override id_aa64zfr0_el1 to be 0.
66 id_aa64zfr0_override.val = 0;
67 id_aa64zfr0_override.mask = GENMASK(63, 0);
73 static const struct ftr_set_desc pfr0 __initconst = {
74 .name = "id_aa64pfr0",
75 .override = &id_aa64pfr0_override,
77 FIELD("sve", ID_AA64PFR0_EL1_SVE_SHIFT, pfr0_sve_filter),
82 static bool __init pfr1_sme_filter(u64 val)
85 * Similarly to SVE, disabling SME also means disabling all
86 * the features that are associated with it. Just set
87 * id_aa64smfr0_el1 to 0 and don't look back.
90 id_aa64smfr0_override.val = 0;
91 id_aa64smfr0_override.mask = GENMASK(63, 0);
97 static const struct ftr_set_desc pfr1 __initconst = {
98 .name = "id_aa64pfr1",
99 .override = &id_aa64pfr1_override,
101 FIELD("bt", ID_AA64PFR1_EL1_BT_SHIFT, NULL ),
102 FIELD("mte", ID_AA64PFR1_EL1_MTE_SHIFT, NULL),
103 FIELD("sme", ID_AA64PFR1_EL1_SME_SHIFT, pfr1_sme_filter),
108 static const struct ftr_set_desc isar1 __initconst = {
109 .name = "id_aa64isar1",
110 .override = &id_aa64isar1_override,
112 FIELD("gpi", ID_AA64ISAR1_EL1_GPI_SHIFT, NULL),
113 FIELD("gpa", ID_AA64ISAR1_EL1_GPA_SHIFT, NULL),
114 FIELD("api", ID_AA64ISAR1_EL1_API_SHIFT, NULL),
115 FIELD("apa", ID_AA64ISAR1_EL1_APA_SHIFT, NULL),
120 static const struct ftr_set_desc isar2 __initconst = {
121 .name = "id_aa64isar2",
122 .override = &id_aa64isar2_override,
124 FIELD("gpa3", ID_AA64ISAR2_EL1_GPA3_SHIFT, NULL),
125 FIELD("apa3", ID_AA64ISAR2_EL1_APA3_SHIFT, NULL),
126 FIELD("mops", ID_AA64ISAR2_EL1_MOPS_SHIFT, NULL),
131 static const struct ftr_set_desc smfr0 __initconst = {
132 .name = "id_aa64smfr0",
133 .override = &id_aa64smfr0_override,
135 FIELD("smever", ID_AA64SMFR0_EL1_SMEver_SHIFT, NULL),
136 /* FA64 is a one bit field... :-/ */
137 { "fa64", ID_AA64SMFR0_EL1_FA64_SHIFT, 1, },
142 static bool __init hvhe_filter(u64 val)
144 u64 mmfr1 = read_sysreg(id_aa64mmfr1_el1);
147 lower_32_bits(__boot_status) == BOOT_CPU_MODE_EL2 &&
148 cpuid_feature_extract_unsigned_field(mmfr1,
149 ID_AA64MMFR1_EL1_VH_SHIFT));
152 static const struct ftr_set_desc sw_features __initconst = {
154 .override = &arm64_sw_feature_override,
156 FIELD("nokaslr", ARM64_SW_FEATURE_OVERRIDE_NOKASLR, NULL),
157 FIELD("hvhe", ARM64_SW_FEATURE_OVERRIDE_HVHE, hvhe_filter),
162 static const struct ftr_set_desc * const regs[] __initconst = {
172 static const struct {
173 char alias[FTR_ALIAS_NAME_LEN];
174 char feature[FTR_ALIAS_OPTION_LEN];
175 } aliases[] __initconst = {
176 { "kvm-arm.mode=nvhe", "id_aa64mmfr1.vh=0" },
177 { "kvm-arm.mode=protected", "id_aa64mmfr1.vh=0" },
178 { "arm64.nosve", "id_aa64pfr0.sve=0" },
179 { "arm64.nosme", "id_aa64pfr1.sme=0" },
180 { "arm64.nobti", "id_aa64pfr1.bt=0" },
182 "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 "
183 "id_aa64isar1.api=0 id_aa64isar1.apa=0 "
184 "id_aa64isar2.gpa3=0 id_aa64isar2.apa3=0" },
185 { "arm64.nomops", "id_aa64isar2.mops=0" },
186 { "arm64.nomte", "id_aa64pfr1.mte=0" },
187 { "nokaslr", "arm64_sw.nokaslr=1" },
190 static int __init parse_nokaslr(char *unused)
192 /* nokaslr param handling is done by early cpufeature code */
195 early_param("nokaslr", parse_nokaslr);
197 static int __init find_field(const char *cmdline,
198 const struct ftr_set_desc *reg, int f, u64 *v)
200 char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
203 len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
204 reg->name, reg->fields[f].name);
206 if (!parameqn(cmdline, opt, len))
209 return kstrtou64(cmdline + len, 0, v);
212 static void __init match_options(const char *cmdline)
216 for (i = 0; i < ARRAY_SIZE(regs); i++) {
219 if (!regs[i]->override)
222 for (f = 0; strlen(regs[i]->fields[f].name); f++) {
223 u64 shift = regs[i]->fields[f].shift;
224 u64 width = regs[i]->fields[f].width ?: 4;
225 u64 mask = GENMASK_ULL(shift + width - 1, shift);
228 if (find_field(cmdline, regs[i], f, &v))
232 * If an override gets filtered out, advertise
233 * it by setting the value to the all-ones while
234 * clearing the mask... Yes, this is fragile.
236 if (regs[i]->fields[f].filter &&
237 !regs[i]->fields[f].filter(v)) {
238 regs[i]->override->val |= mask;
239 regs[i]->override->mask &= ~mask;
243 regs[i]->override->val &= ~mask;
244 regs[i]->override->val |= (v << shift) & mask;
245 regs[i]->override->mask |= mask;
252 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
259 cmdline = skip_spaces(cmdline);
261 for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
265 len = min(len, ARRAY_SIZE(buf) - 1);
266 memcpy(buf, cmdline, len);
269 if (strcmp(buf, "--") == 0)
276 for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++)
277 if (parameq(buf, aliases[i].alias))
278 __parse_cmdline(aliases[i].feature, false);
282 static __init const u8 *get_bootargs_cmdline(void)
288 fdt = get_early_fdt_ptr();
292 node = fdt_path_offset(fdt, "/chosen");
296 prop = fdt_getprop(fdt, node, "bootargs", NULL);
300 return strlen(prop) ? prop : NULL;
303 static __init void parse_cmdline(void)
305 const u8 *prop = get_bootargs_cmdline();
307 if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop)
308 __parse_cmdline(CONFIG_CMDLINE, true);
310 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop)
311 __parse_cmdline(prop, true);
314 /* Keep checkers quiet */
315 void init_feature_override(u64 boot_status);
317 asmlinkage void __init init_feature_override(u64 boot_status)
321 for (i = 0; i < ARRAY_SIZE(regs); i++) {
322 if (regs[i]->override) {
323 regs[i]->override->val = 0;
324 regs[i]->override->mask = 0;
328 __boot_status = boot_status;
332 for (i = 0; i < ARRAY_SIZE(regs); i++) {
333 if (regs[i]->override)
334 dcache_clean_inval_poc((unsigned long)regs[i]->override,
335 (unsigned long)regs[i]->override +
336 sizeof(*regs[i]->override));