Skip to content

Commit 04816c1

Browse files
committed
LoongArch: Add debugfs entries to switch SFB/TSO state
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>
1 parent 26c0a2d commit 04816c1

File tree

4 files changed

+186
-7
lines changed

4 files changed

+186
-7
lines changed

arch/loongarch/include/asm/loongarch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
#define CPUCFG3_SPW_HG_HF BIT(11)
109109
#define CPUCFG3_RVA BIT(12)
110110
#define CPUCFG3_RVAMAX GENMASK(16, 13)
111+
#define CPUCFG3_ALDORDER_CAP BIT(18) /* All address load ordered, capability */
112+
#define CPUCFG3_ASTORDER_CAP BIT(19) /* All address store ordered, capability */
113+
#define CPUCFG3_ALDORDER_STA BIT(20) /* All address load ordered, status */
114+
#define CPUCFG3_ASTORDER_STA BIT(21) /* All address store ordered, status */
115+
#define CPUCFG3_SLDORDER_CAP BIT(22) /* Same address load ordered, capability */
116+
#define CPUCFG3_SLDORDER_STA BIT(23) /* Same address load ordered, status */
111117

112118
#define LOONGARCH_CPUCFG4 0x4
113119
#define CPUCFG4_CCFREQ GENMASK(31, 0)
@@ -565,6 +571,15 @@
565571

566572
/* Implement dependent */
567573
#define LOONGARCH_CSR_IMPCTL1 0x80 /* Loongson config1 */
574+
#define CSR_LDSTORDER_SHIFT 28
575+
#define CSR_LDSTORDER_WIDTH 3
576+
#define CSR_LDSTORDER_MASK (_ULCAST_(0x7) << CSR_LDSTORDER_SHIFT)
577+
#define CSR_LDSTORDER_NLD_NST (_ULCAST_(0x0) << CSR_LDSTORDER_SHIFT) /* 000 = No Load No Store */
578+
#define CSR_LDSTORDER_ALD_NST (_ULCAST_(0x1) << CSR_LDSTORDER_SHIFT) /* 001 = All Load No Store */
579+
#define CSR_LDSTORDER_SLD_NST (_ULCAST_(0x3) << CSR_LDSTORDER_SHIFT) /* 011 = Same Load No Store */
580+
#define CSR_LDSTORDER_NLD_AST (_ULCAST_(0x4) << CSR_LDSTORDER_SHIFT) /* 100 = No Load All Store */
581+
#define CSR_LDSTORDER_ALD_AST (_ULCAST_(0x5) << CSR_LDSTORDER_SHIFT) /* 101 = All Load All Store */
582+
#define CSR_LDSTORDER_SLD_AST (_ULCAST_(0x7) << CSR_LDSTORDER_SHIFT) /* 111 = Same Load All Store */
568583
#define CSR_MISPEC_SHIFT 20
569584
#define CSR_MISPEC_WIDTH 8
570585
#define CSR_MISPEC (_ULCAST_(0xff) << CSR_MISPEC_SHIFT)

arch/loongarch/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extra-y := vmlinux.lds
1010
obj-y += head.o cpu-probe.o cacheinfo.o env.o setup.o entry.o genex.o \
1111
traps.o irq.o idle.o process.o dma.o mem.o reset.o switch.o \
1212
elf.o syscall.o signal.o time.o topology.o inst.o ptrace.o vdso.o \
13-
alternative.o unwind.o
13+
alternative.o kdebugfs.o unwind.o
1414

1515
obj-$(CONFIG_ACPI) += acpi.o
1616
obj-$(CONFIG_EFI) += efi.o

arch/loongarch/kernel/kdebugfs.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/init.h>
3+
#include <linux/export.h>
4+
#include <linux/debugfs.h>
5+
#include <linux/kstrtox.h>
6+
#include <asm/loongarch.h>
7+
8+
struct dentry *arch_debugfs_dir;
9+
EXPORT_SYMBOL(arch_debugfs_dir);
10+
11+
static int sfb_state, tso_state;
12+
13+
static void set_sfb_state(void *info)
14+
{
15+
int val = *(int *)info << CSR_STFILL_SHIFT;
16+
17+
csr_xchg32(val, CSR_STFILL, LOONGARCH_CSR_IMPCTL1);
18+
}
19+
20+
static ssize_t sfb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
21+
{
22+
int s, state;
23+
char str[32];
24+
25+
state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_STFILL) >> CSR_STFILL_SHIFT;
26+
27+
s = snprintf(str, sizeof(str), "Boot State: %x\nCurrent State: %x\n", sfb_state, state);
28+
29+
if (*ppos >= s)
30+
return 0;
31+
32+
s -= *ppos;
33+
s = min_t(u32, s, count);
34+
35+
if (copy_to_user(buf, &str[*ppos], s))
36+
return -EFAULT;
37+
38+
*ppos += s;
39+
40+
return s;
41+
}
42+
43+
static ssize_t sfb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
44+
{
45+
int state;
46+
47+
if (kstrtoint_from_user(buf, count, 10, &state))
48+
return -EFAULT;
49+
50+
switch (state) {
51+
case 0: case 1:
52+
on_each_cpu(set_sfb_state, &state, 1);
53+
break;
54+
default:
55+
return -EINVAL;
56+
}
57+
58+
return count;
59+
}
60+
61+
static const struct file_operations sfb_fops = {
62+
.read = sfb_read,
63+
.write = sfb_write,
64+
.open = simple_open,
65+
.llseek = default_llseek
66+
};
67+
68+
#define LDSTORDER_NLD_NST 0x0 /* 000 = No Load No Store */
69+
#define LDSTORDER_ALD_NST 0x1 /* 001 = All Load No Store */
70+
#define LDSTORDER_SLD_NST 0x3 /* 011 = Same Load No Store */
71+
#define LDSTORDER_NLD_AST 0x4 /* 100 = No Load All Store */
72+
#define LDSTORDER_ALD_AST 0x5 /* 101 = All Load All Store */
73+
#define LDSTORDER_SLD_AST 0x7 /* 111 = Same Load All Store */
74+
75+
static char *tso_hints[] = {
76+
"No Load No Store",
77+
"All Load No Store",
78+
"Invalid Config",
79+
"Same Load No Store",
80+
"No Load All Store",
81+
"All Load All Store",
82+
"Invalid Config",
83+
"Same Load All Store"
84+
};
85+
86+
static void set_tso_state(void *info)
87+
{
88+
int val = *(int *)info << CSR_LDSTORDER_SHIFT;
89+
90+
csr_xchg32(val, CSR_LDSTORDER_MASK, LOONGARCH_CSR_IMPCTL1);
91+
}
92+
93+
static ssize_t tso_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
94+
{
95+
int s, state;
96+
char str[240];
97+
98+
state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_LDSTORDER_MASK) >> CSR_LDSTORDER_SHIFT;
99+
100+
s = snprintf(str, sizeof(str), "Boot State: %d (%s)\n"
101+
"Current State: %d (%s)\n\n"
102+
"Available States:\n"
103+
"0 (%s)\t" "1 (%s)\t" "3 (%s)\n"
104+
"4 (%s)\t" "5 (%s)\t" "7 (%s)\n",
105+
tso_state, tso_hints[tso_state], state, tso_hints[state],
106+
tso_hints[0], tso_hints[1], tso_hints[3], tso_hints[4], tso_hints[5], tso_hints[7]);
107+
108+
if (*ppos >= s)
109+
return 0;
110+
111+
s -= *ppos;
112+
s = min_t(u32, s, count);
113+
114+
if (copy_to_user(buf, &str[*ppos], s))
115+
return -EFAULT;
116+
117+
*ppos += s;
118+
119+
return s;
120+
}
121+
122+
static ssize_t tso_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
123+
{
124+
int state;
125+
126+
if (kstrtoint_from_user(buf, count, 10, &state))
127+
return -EFAULT;
128+
129+
switch (state) {
130+
case 0: case 1: case 3:
131+
case 4: case 5: case 7:
132+
on_each_cpu(set_tso_state, &state, 1);
133+
break;
134+
default:
135+
return -EINVAL;
136+
}
137+
138+
return count;
139+
}
140+
141+
static const struct file_operations tso_fops = {
142+
.read = tso_read,
143+
.write = tso_write,
144+
.open = simple_open,
145+
.llseek = default_llseek
146+
};
147+
148+
static int __init arch_kdebugfs_init(void)
149+
{
150+
unsigned int config = read_cpucfg(LOONGARCH_CPUCFG3);
151+
152+
arch_debugfs_dir = debugfs_create_dir("loongarch", NULL);
153+
154+
if (config & CPUCFG3_SFB) {
155+
debugfs_create_file("sfb_state", S_IRUGO | S_IWUSR,
156+
arch_debugfs_dir, &sfb_state, &sfb_fops);
157+
sfb_state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_STFILL) >> CSR_STFILL_SHIFT;
158+
}
159+
160+
if (config & (CPUCFG3_ALDORDER_CAP | CPUCFG3_ASTORDER_CAP)) {
161+
debugfs_create_file("tso_state", S_IRUGO | S_IWUSR,
162+
arch_debugfs_dir, &tso_state, &tso_fops);
163+
tso_state = (csr_read32(LOONGARCH_CSR_IMPCTL1) & CSR_LDSTORDER_MASK) >> CSR_LDSTORDER_SHIFT;
164+
}
165+
166+
return 0;
167+
}
168+
postcore_initcall(arch_kdebugfs_init);

arch/loongarch/kernel/unaligned.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,10 @@ void emulate_load_store_insn(struct pt_regs *regs, void __user *addr, unsigned i
482482
#ifdef CONFIG_DEBUG_FS
483483
static int __init debugfs_unaligned(void)
484484
{
485-
struct dentry *d;
486-
487-
d = debugfs_create_dir("loongarch", NULL);
488-
489485
debugfs_create_u32("unaligned_instructions_user",
490-
S_IRUGO, d, &unaligned_instructions_user);
486+
S_IRUGO, arch_debugfs_dir, &unaligned_instructions_user);
491487
debugfs_create_u32("unaligned_instructions_kernel",
492-
S_IRUGO, d, &unaligned_instructions_kernel);
488+
S_IRUGO, arch_debugfs_dir, &unaligned_instructions_kernel);
493489

494490
return 0;
495491
}

0 commit comments

Comments
 (0)