arm64: cpufeature: Add an early command-line cpufeature override facility
authorMarc Zyngier <maz@kernel.org>
Mon, 8 Feb 2021 09:57:22 +0000 (09:57 +0000)
committerWill Deacon <will@kernel.org>
Tue, 9 Feb 2021 13:50:52 +0000 (13:50 +0000)
In order to be able to override CPU features at boot time,
let's add a command line parser that matches options of the
form "cpureg.feature=value", and store the corresponding
value into the override val/mask pair.

No features are currently defined, so no expected change in
functionality.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Acked-by: David Brazdil <dbrazdil@google.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/20210208095732.3267263-14-maz@kernel.org
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/kernel/Makefile
arch/arm64/kernel/head.S
arch/arm64/kernel/idreg-override.c [new file with mode: 0644]

index 86364ab..2262f03 100644 (file)
@@ -17,7 +17,7 @@ obj-y                 := debug-monitors.o entry.o irq.o fpsimd.o              \
                           return_address.o cpuinfo.o cpu_errata.o              \
                           cpufeature.o alternative.o cacheinfo.o               \
                           smp.o smp_spin_table.o topology.o smccc-call.o       \
-                          syscall.o proton-pack.o
+                          syscall.o proton-pack.o idreg-override.o
 
 targets                        += efi-entry.o
 
index 070d1c6..e7f12bf 100644 (file)
@@ -438,6 +438,7 @@ SYM_FUNC_START_LOCAL(__primary_switched)
 #endif
        mov     x0, x21                         // pass FDT address in x0
        bl      early_fdt_map                   // Try mapping the FDT early
+       bl      init_feature_override           // Parse cpu feature overrides
 #ifdef CONFIG_RANDOMIZE_BASE
        tst     x23, ~(MIN_KIMG_ALIGN - 1)      // already running randomized?
        b.ne    0f
diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c
new file mode 100644 (file)
index 0000000..3a347b4
--- /dev/null
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Early cpufeature override framework
+ *
+ * Copyright (C) 2020 Google LLC
+ * Author: Marc Zyngier <maz@kernel.org>
+ */
+
+#include <linux/ctype.h>
+#include <linux/kernel.h>
+#include <linux/libfdt.h>
+
+#include <asm/cacheflush.h>
+#include <asm/setup.h>
+
+#define FTR_DESC_NAME_LEN      20
+#define FTR_DESC_FIELD_LEN     10
+
+struct ftr_set_desc {
+       char                            name[FTR_DESC_NAME_LEN];
+       struct arm64_ftr_override       *override;
+       struct {
+               char                    name[FTR_DESC_FIELD_LEN];
+               u8                      shift;
+       }                               fields[];
+};
+
+static const struct ftr_set_desc * const regs[] __initconst = {
+};
+
+static int __init find_field(const char *cmdline,
+                            const struct ftr_set_desc *reg, int f, u64 *v)
+{
+       char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
+       int len;
+
+       len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
+                      reg->name, reg->fields[f].name);
+
+       if (!parameqn(cmdline, opt, len))
+               return -1;
+
+       return kstrtou64(cmdline + len, 0, v);
+}
+
+static void __init match_options(const char *cmdline)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(regs); i++) {
+               int f;
+
+               if (!regs[i]->override)
+                       continue;
+
+               for (f = 0; strlen(regs[i]->fields[f].name); f++) {
+                       u64 shift = regs[i]->fields[f].shift;
+                       u64 mask = 0xfUL << shift;
+                       u64 v;
+
+                       if (find_field(cmdline, regs[i], f, &v))
+                               continue;
+
+                       regs[i]->override->val  &= ~mask;
+                       regs[i]->override->val  |= (v << shift) & mask;
+                       regs[i]->override->mask |= mask;
+
+                       return;
+               }
+       }
+}
+
+static __init void __parse_cmdline(const char *cmdline)
+{
+       do {
+               char buf[256];
+               size_t len;
+               int i;
+
+               cmdline = skip_spaces(cmdline);
+
+               for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
+               if (!len)
+                       return;
+
+               len = min(len, ARRAY_SIZE(buf) - 1);
+               strncpy(buf, cmdline, len);
+               buf[len] = 0;
+
+               if (strcmp(buf, "--") == 0)
+                       return;
+
+               cmdline += len;
+
+               match_options(buf);
+
+       } while (1);
+}
+
+static __init void parse_cmdline(void)
+{
+       if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
+               const u8 *prop;
+               void *fdt;
+               int node;
+
+               fdt = get_early_fdt_ptr();
+               if (!fdt)
+                       goto out;
+
+               node = fdt_path_offset(fdt, "/chosen");
+               if (node < 0)
+                       goto out;
+
+               prop = fdt_getprop(fdt, node, "bootargs", NULL);
+               if (!prop)
+                       goto out;
+
+               __parse_cmdline(prop);
+
+               if (!IS_ENABLED(CONFIG_CMDLINE_EXTEND))
+                       return;
+       }
+
+out:
+       __parse_cmdline(CONFIG_CMDLINE);
+}
+
+/* Keep checkers quiet */
+void init_feature_override(void);
+
+asmlinkage void __init init_feature_override(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(regs); i++) {
+               if (regs[i]->override) {
+                       regs[i]->override->val  = 0;
+                       regs[i]->override->mask = 0;
+               }
+       }
+
+       parse_cmdline();
+
+       for (i = 0; i < ARRAY_SIZE(regs); i++) {
+               if (regs[i]->override)
+                       __flush_dcache_area(regs[i]->override,
+                                           sizeof(*regs[i]->override));
+       }
+}