7cca82639606f6fc1fc176b220887eb8fa7fda63
[platform/kernel/linux-starfive.git] / arch / arm64 / kernel / idreg-override.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Early cpufeature override framework
4  *
5  * Copyright (C) 2020 Google LLC
6  * Author: Marc Zyngier <maz@kernel.org>
7  */
8
9 #include <linux/ctype.h>
10 #include <linux/kernel.h>
11 #include <linux/libfdt.h>
12
13 #include <asm/cacheflush.h>
14 #include <asm/cpufeature.h>
15 #include <asm/setup.h>
16
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
21
22 static u64 __boot_status __initdata;
23
24 struct ftr_set_desc {
25         char                            name[FTR_DESC_NAME_LEN];
26         struct arm64_ftr_override       *override;
27         struct {
28                 char                    name[FTR_DESC_FIELD_LEN];
29                 u8                      shift;
30                 u8                      width;
31                 bool                    (*filter)(u64 val);
32         }                               fields[];
33 };
34
35 #define FIELD(n, s, f)  { .name = n, .shift = s, .width = 4, .filter = f }
36
37 static bool __init mmfr1_vh_filter(u64 val)
38 {
39         /*
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.
44          */
45         return !(__boot_status == (BOOT_CPU_FLAG_E2H | BOOT_CPU_MODE_EL2) &&
46                  val == 0);
47 }
48
49 static const struct ftr_set_desc mmfr1 __initconst = {
50         .name           = "id_aa64mmfr1",
51         .override       = &id_aa64mmfr1_override,
52         .fields         = {
53                 FIELD("vh", ID_AA64MMFR1_VHE_SHIFT, mmfr1_vh_filter),
54                 {}
55         },
56 };
57
58 static bool __init pfr0_sve_filter(u64 val)
59 {
60         /*
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.
64          */
65         if (!val) {
66                 id_aa64zfr0_override.val = 0;
67                 id_aa64zfr0_override.mask = GENMASK(63, 0);
68         }
69
70         return true;
71 }
72
73 static const struct ftr_set_desc pfr0 __initconst = {
74         .name           = "id_aa64pfr0",
75         .override       = &id_aa64pfr0_override,
76         .fields         = {
77                 FIELD("sve", ID_AA64PFR0_SVE_SHIFT, pfr0_sve_filter),
78                 {}
79         },
80 };
81
82 static bool __init pfr1_sme_filter(u64 val)
83 {
84         /*
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.
88          */
89         if (!val) {
90                 id_aa64smfr0_override.val = 0;
91                 id_aa64smfr0_override.mask = GENMASK(63, 0);
92         }
93
94         return true;
95 }
96
97 static const struct ftr_set_desc pfr1 __initconst = {
98         .name           = "id_aa64pfr1",
99         .override       = &id_aa64pfr1_override,
100         .fields         = {
101                 FIELD("bt", ID_AA64PFR1_BT_SHIFT, NULL),
102                 FIELD("mte", ID_AA64PFR1_MTE_SHIFT, NULL),
103                 FIELD("sme", ID_AA64PFR1_SME_SHIFT, pfr1_sme_filter),
104                 {}
105         },
106 };
107
108 static const struct ftr_set_desc isar1 __initconst = {
109         .name           = "id_aa64isar1",
110         .override       = &id_aa64isar1_override,
111         .fields         = {
112                 FIELD("gpi", ID_AA64ISAR1_GPI_SHIFT, NULL),
113                 FIELD("gpa", ID_AA64ISAR1_GPA_SHIFT, NULL),
114                 FIELD("api", ID_AA64ISAR1_API_SHIFT, NULL),
115                 FIELD("apa", ID_AA64ISAR1_APA_SHIFT, NULL),
116                 {}
117         },
118 };
119
120 static const struct ftr_set_desc isar2 __initconst = {
121         .name           = "id_aa64isar2",
122         .override       = &id_aa64isar2_override,
123         .fields         = {
124                 FIELD("gpa3", ID_AA64ISAR2_GPA3_SHIFT, NULL),
125                 FIELD("apa3", ID_AA64ISAR2_APA3_SHIFT, NULL),
126                 {}
127         },
128 };
129
130 extern struct arm64_ftr_override kaslr_feature_override;
131
132 static const struct ftr_set_desc kaslr __initconst = {
133         .name           = "kaslr",
134 #ifdef CONFIG_RANDOMIZE_BASE
135         .override       = &kaslr_feature_override,
136 #endif
137         .fields         = {
138                 FIELD("disabled", 0, NULL),
139                 {}
140         },
141 };
142
143 static const struct ftr_set_desc * const regs[] __initconst = {
144         &mmfr1,
145         &pfr0,
146         &pfr1,
147         &isar1,
148         &isar2,
149         &kaslr,
150 };
151
152 static const struct {
153         char    alias[FTR_ALIAS_NAME_LEN];
154         char    feature[FTR_ALIAS_OPTION_LEN];
155 } aliases[] __initconst = {
156         { "kvm-arm.mode=nvhe",          "id_aa64mmfr1.vh=0" },
157         { "kvm-arm.mode=protected",     "id_aa64mmfr1.vh=0" },
158         { "arm64.nosve",                "id_aa64pfr0.sve=0 id_aa64pfr1.sme=0" },
159         { "arm64.nosme",                "id_aa64pfr1.sme=0" },
160         { "arm64.nobti",                "id_aa64pfr1.bt=0" },
161         { "arm64.nopauth",
162           "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 "
163           "id_aa64isar1.api=0 id_aa64isar1.apa=0 "
164           "id_aa64isar2.gpa3=0 id_aa64isar2.apa3=0"        },
165         { "arm64.nomte",                "id_aa64pfr1.mte=0" },
166         { "nokaslr",                    "kaslr.disabled=1" },
167 };
168
169 static int __init find_field(const char *cmdline,
170                              const struct ftr_set_desc *reg, int f, u64 *v)
171 {
172         char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
173         int len;
174
175         len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
176                        reg->name, reg->fields[f].name);
177
178         if (!parameqn(cmdline, opt, len))
179                 return -1;
180
181         return kstrtou64(cmdline + len, 0, v);
182 }
183
184 static void __init match_options(const char *cmdline)
185 {
186         int i;
187
188         for (i = 0; i < ARRAY_SIZE(regs); i++) {
189                 int f;
190
191                 if (!regs[i]->override)
192                         continue;
193
194                 for (f = 0; strlen(regs[i]->fields[f].name); f++) {
195                         u64 shift = regs[i]->fields[f].shift;
196                         u64 width = regs[i]->fields[f].width ?: 4;
197                         u64 mask = GENMASK_ULL(shift + width - 1, shift);
198                         u64 v;
199
200                         if (find_field(cmdline, regs[i], f, &v))
201                                 continue;
202
203                         /*
204                          * If an override gets filtered out, advertise
205                          * it by setting the value to the all-ones while
206                          * clearing the mask... Yes, this is fragile.
207                          */
208                         if (regs[i]->fields[f].filter &&
209                             !regs[i]->fields[f].filter(v)) {
210                                 regs[i]->override->val  |= mask;
211                                 regs[i]->override->mask &= ~mask;
212                                 continue;
213                         }
214
215                         regs[i]->override->val  &= ~mask;
216                         regs[i]->override->val  |= (v << shift) & mask;
217                         regs[i]->override->mask |= mask;
218
219                         return;
220                 }
221         }
222 }
223
224 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
225 {
226         do {
227                 char buf[256];
228                 size_t len;
229                 int i;
230
231                 cmdline = skip_spaces(cmdline);
232
233                 for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
234                 if (!len)
235                         return;
236
237                 len = min(len, ARRAY_SIZE(buf) - 1);
238                 strncpy(buf, cmdline, len);
239                 buf[len] = 0;
240
241                 if (strcmp(buf, "--") == 0)
242                         return;
243
244                 cmdline += len;
245
246                 match_options(buf);
247
248                 for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++)
249                         if (parameq(buf, aliases[i].alias))
250                                 __parse_cmdline(aliases[i].feature, false);
251         } while (1);
252 }
253
254 static __init const u8 *get_bootargs_cmdline(const void *fdt)
255 {
256         const u8 *prop;
257         int node;
258
259         node = fdt_path_offset(fdt, "/chosen");
260         if (node < 0)
261                 return NULL;
262
263         prop = fdt_getprop(fdt, node, "bootargs", NULL);
264         if (!prop)
265                 return NULL;
266
267         return strlen(prop) ? prop : NULL;
268 }
269
270 static __init void parse_cmdline(const void *fdt)
271 {
272         const u8 *prop = get_bootargs_cmdline(fdt);
273
274         if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop)
275                 __parse_cmdline(CONFIG_CMDLINE, true);
276
277         if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop)
278                 __parse_cmdline(prop, true);
279 }
280
281 /* Keep checkers quiet */
282 void init_feature_override(const void *fdt, u64 boot_status);
283
284 asmlinkage void __init init_feature_override(const void *fdt, u64 boot_status)
285 {
286         int i;
287
288         for (i = 0; i < ARRAY_SIZE(regs); i++) {
289                 if (regs[i]->override) {
290                         regs[i]->override->val  = 0;
291                         regs[i]->override->mask = 0;
292                 }
293         }
294
295         __boot_status = boot_status;
296
297         parse_cmdline(fdt);
298
299         for (i = 0; i < ARRAY_SIZE(regs); i++) {
300                 if (regs[i]->override)
301                         dcache_clean_inval_poc((unsigned long)regs[i]->override,
302                                             (unsigned long)regs[i]->override +
303                                             sizeof(*regs[i]->override));
304         }
305 }