]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
LoongArch: Add debugfs entries to switch SFB/TSO state
authorHuacai Chen <chenhuacai@loongson.cn>
Sun, 26 Jan 2025 13:49:59 +0000 (21:49 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Sun, 26 Jan 2025 13:49:59 +0000 (21:49 +0800)
We need to switch SFB (Store Fill Buffer) and TSO (Total Store Order)
state at runtime to debug memory management and KVM virtualization, so
add two debugfs entries "sfb_state" and "tso_state" under the directory
/sys/kernel/debug/loongarch.

Query SFB:
cat /sys/kernel/debug/loongarch/sfb_state

Enable SFB:
echo 1 > /sys/kernel/debug/loongarch/sfb_state

Disable SFB:
echo 0 > /sys/kernel/debug/loongarch/sfb_state

Query TSO:
cat /sys/kernel/debug/loongarch/tso_state

Switch TSO:
echo [TSO] > /sys/kernel/debug/loongarch/tso_state

Available [TSO] states:
0 (No Load No Store)    1 (All Load No Store)   3 (Same Load No Store)
4 (No Load All Store)   5 (All Load All Store)  7 (Same Load All Store)

Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/include/asm/loongarch.h
arch/loongarch/kernel/Makefile
arch/loongarch/kernel/kdebugfs.c [new file with mode: 0644]
arch/loongarch/kernel/unaligned.c

index a3cc4f8d4c4a0b0521ef2b76c87fa57eca417d75..15c24563617604f98d2378f10fb34ebc8b70b591 100644 (file)
 #define  CPUCFG3_SPW_HG_HF             BIT(11)
 #define  CPUCFG3_RVA                   BIT(12)
 #define  CPUCFG3_RVAMAX                        GENMASK(16, 13)
+#define  CPUCFG3_ALDORDER_CAP          BIT(18) /* All address load ordered, capability */
+#define  CPUCFG3_ASTORDER_CAP          BIT(19) /* All address store ordered, capability */
+#define  CPUCFG3_ALDORDER_STA          BIT(20) /* All address load ordered, status */
+#define  CPUCFG3_ASTORDER_STA          BIT(21) /* All address store ordered, status */
+#define  CPUCFG3_SLDORDER_CAP          BIT(22) /* Same address load ordered, capability */
+#define  CPUCFG3_SLDORDER_STA          BIT(23) /* Same address load ordered, status */
 
 #define LOONGARCH_CPUCFG4              0x4
 #define  CPUCFG4_CCFREQ                        GENMASK(31, 0)
 
 /* Implement dependent */
 #define LOONGARCH_CSR_IMPCTL1          0x80    /* Loongson config1 */
+#define  CSR_LDSTORDER_SHIFT           28
+#define  CSR_LDSTORDER_WIDTH           3
+#define  CSR_LDSTORDER_MASK            (_ULCAST_(0x7) << CSR_LDSTORDER_SHIFT)
+#define  CSR_LDSTORDER_NLD_NST         (_ULCAST_(0x0) << CSR_LDSTORDER_SHIFT) /* 000 = No Load No Store */
+#define  CSR_LDSTORDER_ALD_NST         (_ULCAST_(0x1) << CSR_LDSTORDER_SHIFT) /* 001 = All Load No Store */
+#define  CSR_LDSTORDER_SLD_NST         (_ULCAST_(0x3) << CSR_LDSTORDER_SHIFT) /* 011 = Same Load No Store */
+#define  CSR_LDSTORDER_NLD_AST         (_ULCAST_(0x4) << CSR_LDSTORDER_SHIFT) /* 100 = No Load All Store */
+#define  CSR_LDSTORDER_ALD_AST         (_ULCAST_(0x5) << CSR_LDSTORDER_SHIFT) /* 101 = All Load All Store */
+#define  CSR_LDSTORDER_SLD_AST         (_ULCAST_(0x7) << CSR_LDSTORDER_SHIFT) /* 111 = Same Load All Store */
 #define  CSR_MISPEC_SHIFT              20
 #define  CSR_MISPEC_WIDTH              8
 #define  CSR_MISPEC                    (_ULCAST_(0xff) << CSR_MISPEC_SHIFT)
index 9497968ee158ff90196b29a71d1c0630f96162e9..4853e8b04c6fbead5c58cade6d77df65f073e537 100644 (file)
@@ -10,7 +10,7 @@ extra-y               := vmlinux.lds
 obj-y          += head.o cpu-probe.o cacheinfo.o env.o setup.o entry.o genex.o \
                   traps.o irq.o idle.o process.o dma.o mem.o reset.o switch.o \
                   elf.o syscall.o signal.o time.o topology.o inst.o ptrace.o vdso.o \
-                  alternative.o unwind.o
+                  alternative.o kdebugfs.o unwind.o
 
 obj-$(CONFIG_ACPI)             += acpi.o
 obj-$(CONFIG_EFI)              += efi.o
diff --git a/arch/loongarch/kernel/kdebugfs.c b/arch/loongarch/kernel/kdebugfs.c
new file mode 100644 (file)
index 0000000..80cf647
--- /dev/null
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/init.h>
+#include <linux/export.h>
+#include <linux/debugfs.h>
+#include <linux/kstrtox.h>
+#include <asm/loongarch.h>
+
+struct dentry *arch_debugfs_dir;
+EXPORT_SYMBOL(arch_debugfs_dir);
+
+static int sfb_state, tso_state;
+
+static void set_sfb_state(void *info)
+{
+       int val = *(int *)info << CSR_STFILL_SHIFT;
+
+       csr_xchg32(val, CSR_STFILL, LOONGARCH_CSR_IMPCTL1);
+}
+
+static ssize_t sfb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+       int s, state;
+       char str[32];
+
+       state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_STFILL) >> CSR_STFILL_SHIFT;
+
+       s = snprintf(str, sizeof(str), "Boot State: %x\nCurrent State: %x\n", sfb_state, state);
+
+       if (*ppos >= s)
+               return 0;
+
+       s -= *ppos;
+       s = min_t(u32, s, count);
+
+       if (copy_to_user(buf, &str[*ppos], s))
+               return -EFAULT;
+
+       *ppos += s;
+
+       return s;
+}
+
+static ssize_t sfb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
+{
+       int state;
+
+       if (kstrtoint_from_user(buf, count, 10, &state))
+               return -EFAULT;
+
+       switch (state) {
+       case 0: case 1:
+               on_each_cpu(set_sfb_state, &state, 1);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return count;
+}
+
+static const struct file_operations sfb_fops = {
+       .read = sfb_read,
+       .write = sfb_write,
+       .open = simple_open,
+       .llseek = default_llseek
+};
+
+#define LDSTORDER_NLD_NST        0x0 /* 000 = No Load No Store */
+#define LDSTORDER_ALD_NST        0x1 /* 001 = All Load No Store */
+#define LDSTORDER_SLD_NST        0x3 /* 011 = Same Load No Store */
+#define LDSTORDER_NLD_AST        0x4 /* 100 = No Load All Store */
+#define LDSTORDER_ALD_AST        0x5 /* 101 = All Load All Store */
+#define LDSTORDER_SLD_AST        0x7 /* 111 = Same Load All Store */
+
+static char *tso_hints[] = {
+       "No Load No Store",
+       "All Load No Store",
+       "Invalid Config",
+       "Same Load No Store",
+       "No Load All Store",
+       "All Load All Store",
+       "Invalid Config",
+       "Same Load All Store"
+};
+
+static void set_tso_state(void *info)
+{
+       int val = *(int *)info << CSR_LDSTORDER_SHIFT;
+
+       csr_xchg32(val, CSR_LDSTORDER_MASK, LOONGARCH_CSR_IMPCTL1);
+}
+
+static ssize_t tso_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+       int s, state;
+       char str[240];
+
+       state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_LDSTORDER_MASK) >> CSR_LDSTORDER_SHIFT;
+
+       s = snprintf(str, sizeof(str), "Boot State: %d (%s)\n"
+                                      "Current State: %d (%s)\n\n"
+                                      "Available States:\n"
+                                      "0 (%s)\t" "1 (%s)\t" "3 (%s)\n"
+                                      "4 (%s)\t" "5 (%s)\t" "7 (%s)\n",
+                                      tso_state, tso_hints[tso_state], state, tso_hints[state],
+                                      tso_hints[0], tso_hints[1], tso_hints[3], tso_hints[4], tso_hints[5], tso_hints[7]);
+
+       if (*ppos >= s)
+               return 0;
+
+       s -= *ppos;
+       s = min_t(u32, s, count);
+
+       if (copy_to_user(buf, &str[*ppos], s))
+               return -EFAULT;
+
+       *ppos += s;
+
+       return s;
+}
+
+static ssize_t tso_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
+{
+       int state;
+
+       if (kstrtoint_from_user(buf, count, 10, &state))
+               return -EFAULT;
+
+       switch (state) {
+       case 0: case 1: case 3:
+       case 4: case 5: case 7:
+               on_each_cpu(set_tso_state, &state, 1);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return count;
+}
+
+static const struct file_operations tso_fops = {
+       .read = tso_read,
+       .write = tso_write,
+       .open = simple_open,
+       .llseek = default_llseek
+};
+
+static int __init arch_kdebugfs_init(void)
+{
+       unsigned int config = read_cpucfg(LOONGARCH_CPUCFG3);
+
+       arch_debugfs_dir = debugfs_create_dir("loongarch", NULL);
+
+       if (config & CPUCFG3_SFB) {
+               debugfs_create_file("sfb_state", S_IRUGO | S_IWUSR,
+                           arch_debugfs_dir, &sfb_state, &sfb_fops);
+               sfb_state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_STFILL) >> CSR_STFILL_SHIFT;
+       }
+
+       if (config & (CPUCFG3_ALDORDER_CAP | CPUCFG3_ASTORDER_CAP)) {
+               debugfs_create_file("tso_state", S_IRUGO | S_IWUSR,
+                           arch_debugfs_dir, &tso_state, &tso_fops);
+               tso_state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_LDSTORDER_MASK) >> CSR_LDSTORDER_SHIFT;
+       }
+
+       return 0;
+}
+postcore_initcall(arch_kdebugfs_init);
index 3abf163dda056b681494a74916570866a6f1ec7b..487be604b96ae5b9d840130809e467a39a5b7fdc 100644 (file)
@@ -482,14 +482,10 @@ sigbus:
 #ifdef CONFIG_DEBUG_FS
 static int __init debugfs_unaligned(void)
 {
-       struct dentry *d;
-
-       d = debugfs_create_dir("loongarch", NULL);
-
        debugfs_create_u32("unaligned_instructions_user",
-                               S_IRUGO, d, &unaligned_instructions_user);
+                               S_IRUGO, arch_debugfs_dir, &unaligned_instructions_user);
        debugfs_create_u32("unaligned_instructions_kernel",
-                               S_IRUGO, d, &unaligned_instructions_kernel);
+                               S_IRUGO, arch_debugfs_dir, &unaligned_instructions_kernel);
 
        return 0;
 }