Skip to contentSkip to conversation

*** Port dovetail 6.6.y to Loongarch***

PATCH In review Zheng Zhang 2 participants 9 messages Oct 16, 2024 – Oct 23, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover *** Port dovetail 6.6.y to Loongarch***

Hi all!

This patches is base on 6.6.y-dovetail-rebase aiming to port dovetail to Loongarch. Most of architecture related code has already been ported, except ipi part, which I will add in the later patches. This patch is tested under qemu-9.1.0 with irq torture test in single core.

If the community is interested in porting Dovetail to LoongArch, I am very willing to continue improving the current work and maintaining it in the future :^)

Zheng Zhang (6):
dovetail: Add dovetail and irq_pipeline option to Loongarch's Kconfig
dovetail: Port Architecture bits related code to Loongarch
dovetail: Adapt Loongarch's clocksource
dovetail: Add place holders for smp/ipi related functions
dovetail: Disable irq in mm and fpu's critical area
dovetail: Add more irq-chip support

 arch/loongarch/Kconfig                    |   3 +
 arch/loongarch/include/asm/dovetail.h     |  44 +++++++
 arch/loongarch/include/asm/irq.h          |   2 +
 arch/loongarch/include/asm/irq_pipeline.h | 145 ++++++++++++++++++++++
 arch/loongarch/include/asm/irqflags.h     |  28 +++--
 arch/loongarch/include/asm/mmu_context.h  |  31 ++++-
 arch/loongarch/include/asm/ptrace.h       |   3 +-
 arch/loongarch/include/asm/syscall.h      |   6 +
 arch/loongarch/include/asm/thread_info.h  |  17 +++
 arch/loongarch/kernel/Makefile            |   2 +
 arch/loongarch/kernel/alternative.c       |   4 +-
 arch/loongarch/kernel/asm-offsets.c       |   1 +
 arch/loongarch/kernel/idle.c              |   2 +
 arch/loongarch/kernel/irq.c               |  23 ++++
 arch/loongarch/kernel/irq_pipeline.c      |  34 +++++
 arch/loongarch/kernel/perf_event.c        |   8 +-
 arch/loongarch/kernel/smp.c               |  28 ++++-
 arch/loongarch/kernel/time.c              |  10 +-
 arch/loongarch/kernel/traps.c             |  52 ++++++--
 arch/loongarch/mm/fault.c                 |   4 +-
 arch/loongarch/mm/tlb.c                   |  17 +--
 drivers/irqchip/irq-loongarch-cpu.c       |   1 +
 drivers/irqchip/irq-loongson-pch-lpc.c    |   2 +-
 23 files changed, 412 insertions(+), 55 deletions(-)
 create mode 100644 arch/loongarch/include/asm/dovetail.h
 create mode 100644 arch/loongarch/include/asm/irq_pipeline.h
 create mode 100644 arch/loongarch/kernel/irq_pipeline.c
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/6 dovetail: Add dovetail and irq_pipeline option to Loongarch's Kconfig

---
 arch/loongarch/Kconfig | 3 +++
 1 file changed, 3 insertions(+)
arch/loongarch/Kconfig +3 -0
index e14396a2ddcb..11f9a9b73920 100644
@@ -130,6 +130,8 @@ config LOONGARCH
 	select HAVE_KPROBES_ON_FTRACE
 	select HAVE_KRETPROBES
 	select HAVE_MOD_ARCH_SPECIFIC
+	select HAVE_IRQ_PIPELINE
+	select HAVE_DOVETAIL
 	select HAVE_NMI
 	select HAVE_PCI
 	select HAVE_PERF_EVENTS
@@ -266,6 +268,7 @@ config AS_HAS_LBT_EXTENSION
 menu "Kernel type and options"
 
 source "kernel/Kconfig.hz"
+source "kernel/Kconfig.dovetail"
 
 choice
 	prompt "Page Table Layout"
Signature · 1 line
2.47.0
#2 lore raw

PATCH 2/6 dovetail: Port Architecture bits related code to Loongarch

In reply to Zheng Zhang

1. Modify the irq entry code to adapt the generic interrupt management provide by irq pipeline.

2. Interrupt mask virtualization arch-specific code is implemented to provide real and virtual interrupt control for inband and oob stage.

---
 arch/loongarch/include/asm/dovetail.h     |  44 +++++++
 arch/loongarch/include/asm/irq.h          |   2 +
 arch/loongarch/include/asm/irq_pipeline.h | 145 ++++++++++++++++++++++
 arch/loongarch/include/asm/irqflags.h     |  28 +++--
 arch/loongarch/include/asm/ptrace.h       |   3 +-
 arch/loongarch/include/asm/syscall.h      |   6 +
 arch/loongarch/include/asm/thread_info.h  |  17 +++
 arch/loongarch/kernel/Makefile            |   2 +
 arch/loongarch/kernel/asm-offsets.c       |   1 +
 arch/loongarch/kernel/irq.c               |  23 ++++
 arch/loongarch/kernel/irq_pipeline.c      |  34 +++++
 arch/loongarch/kernel/time.c              |   4 +
 arch/loongarch/kernel/traps.c             |  13 +-
 13 files changed, 305 insertions(+), 17 deletions(-)
 create mode 100644 arch/loongarch/include/asm/dovetail.h
 create mode 100644 arch/loongarch/include/asm/irq_pipeline.h
 create mode 100644 arch/loongarch/kernel/irq_pipeline.c
arch/loongarch/include/asm/dovetail.h new file +44 -0
new file mode 100644
index 000000000000..da2ca9704b3b
@@ -0,0 +1,44 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018 Philippe Gerum  <[email protected]>.
+ * Copyright (C) 2023 Hao Miao <[email protected]>.
+ * Copyright (C) 2024 Zhang Zheng <[email protected]>.
+ */
+#ifndef _ASM_LOONGARCH64_DOVETAIL_H
+#define _ASM_LOONGARCH64_DOVETAIL_H
+
+/* LOONGARCH64 traps */
+#define LOONGARCH64_TRAP_FPE		0	/* Delayed fp exception */
+#define LOONGARCH64_TRAP_BP		1	/* Break point */
+#define LOONGARCH64_TRAP_WATCH		2	/* Watch point */
+#define LOONGARCH64_TRAP_RI		3	/* Reserved instruction */
+#define LOONGARCH64_TRAP_FPU		4	/* FPU access */
+#define LOONGARCH64_TRAP_LSX		5	/* LAX access */
+#define LOONGARCH64_TRAP_LASX		6	/* LASX access */
+#define LOONGARCH64_TRAP_LBT		7	/* LBT access */
+#define LOONGARCH64_TRAP_RESERVED	8	/* Reserved exception */
+#define LOONGARCH64_TRAP_ADE		9	/* Wrong memory address access */
+#define LOONGARCH64_TRAP_ALE		10	/* Unaligned memory address access */
+#define LOONGARCH64_TRAP_PAGEFAULT	11	/* Page fault */
+
+#ifdef CONFIG_DOVETAIL
+
+static inline void arch_dovetail_exec_prepare(void)
+{ }
+
+static inline void arch_dovetail_switch_prepare(bool leave_inband)
+{ }
+
+static inline void arch_dovetail_switch_finish(bool enter_inband)
+{
+	extern void restore_fp_current_oob(void);
+	restore_fp_current_oob();
+}
+
+#define arch_dovetail_is_syscall(__nr)	\
+	((__nr) == __NR_prctl)
+
+#endif
+
+#endif /* _ASM_LOONGARCH64_DOVETAIL_H */
arch/loongarch/include/asm/irq.h +2 -0
index 218b4da0ea90..8a8dbe1f3856 100644
@@ -121,4 +121,6 @@ extern irqreturn_t loongson_ipi_interrupt(int irq, void *dev);
 
 #include <asm-generic/irq.h>
 
+int handle_arch_irq_pipelined(struct pt_regs *regs);
+
 #endif /* _ASM_IRQ_H */
arch/loongarch/include/asm/irq_pipeline.h new file +145 -0

145 lines not on this page · raw message

arch/loongarch/include/asm/irqflags.h +15 -13
index 319a8c616f1f..f3c2ea057f72 100644
@@ -10,8 +10,9 @@
 #include <linux/compiler.h>
 #include <linux/stringify.h>
 #include <asm/loongarch.h>
+#include <asm/irq_pipeline.h>
 
-static inline void arch_local_irq_enable(void)
+static inline void native_irq_enable(void)
 {
 	u32 flags = CSR_CRMD_IE;
 	__asm__ __volatile__(
@@ -21,7 +22,7 @@ static inline void arch_local_irq_enable(void)
 		: "memory");
 }
 
-static inline void arch_local_irq_disable(void)
+static inline void native_irq_disable(void)
 {
 	u32 flags = 0;
 	__asm__ __volatile__(
@@ -31,7 +32,14 @@ static inline void arch_local_irq_disable(void)
 		: "memory");
 }
 
-static inline unsigned long arch_local_irq_save(void)
+static inline void native_irq_sync(void)
+{
+	native_irq_enable();
+	barrier();
+	native_irq_disable();
+}
+
+static inline unsigned long native_irq_save(void)
 {
 	u32 flags = 0;
 	__asm__ __volatile__(
@@ -42,7 +50,7 @@ static inline unsigned long arch_local_irq_save(void)
 	return flags;
 }
 
-static inline void arch_local_irq_restore(unsigned long flags)
+static inline void native_irq_restore(unsigned long flags)
 {
 	__asm__ __volatile__(
 		"csrxchg %[val], %[mask], %[reg]\n\t"
@@ -51,7 +59,7 @@ static inline void arch_local_irq_restore(unsigned long flags)
 		: "memory");
 }
 
-static inline unsigned long arch_local_save_flags(void)
+static inline unsigned long native_save_flags(void)
 {
 	u32 flags;
 	__asm__ __volatile__(
@@ -62,16 +70,10 @@ static inline unsigned long arch_local_save_flags(void)
 	return flags;
 }
 
-static inline int arch_irqs_disabled_flags(unsigned long flags)
-{
-	return !(flags & CSR_CRMD_IE);
-}
 
-static inline int arch_irqs_disabled(void)
+static inline int native_irqs_disabled(void)
 {
-	return arch_irqs_disabled_flags(arch_local_save_flags());
+	return native_irqs_disabled_flags(native_save_flags());
 }
 
 #endif /* #ifndef __ASSEMBLY__ */
-
-#endif /* _ASM_IRQFLAGS_H */
arch/loongarch/include/asm/ptrace.h +1 -2
index f3ddaed9ef7f..86ec684e51bd 100644
@@ -6,7 +6,6 @@
 #define _ASM_PTRACE_H
 
 #include <asm/page.h>
-#include <asm/irqflags.h>
 #include <asm/thread_info.h>
 #include <uapi/asm/ptrace.h>
 
@@ -35,7 +34,7 @@ struct pt_regs {
 
 static inline int regs_irqs_disabled(struct pt_regs *regs)
 {
-	return arch_irqs_disabled_flags(regs->csr_prmd);
+	return !(regs->csr_prmd & CSR_CRMD_IE);
 }
 
 static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
arch/loongarch/include/asm/syscall.h +6 -0
index e286dc58476e..2fcc6f72e94b 100644
@@ -61,6 +61,12 @@ static inline void syscall_get_arguments(struct task_struct *task,
 	memcpy(&args[1], &regs->regs[5], 5 * sizeof(long));
 }
 
+static inline unsigned long syscall_get_arg0(struct task_struct *task,
+					     struct pt_regs *regs)
+{
+	return regs->orig_a0;
+}
+
 static inline int syscall_get_arch(struct task_struct *task)
 {
 	return AUDIT_ARCH_LOONGARCH64;
arch/loongarch/include/asm/thread_info.h +17 -0
index 8cb653d49a54..4e544d6237b8 100644
@@ -12,6 +12,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <dovetail/thread_info.h>
 #include <asm/processor.h>
 
 /*
@@ -24,12 +25,14 @@
 struct thread_info {
 	struct task_struct	*task;		/* main task structure */
 	unsigned long		flags;		/* low level flags */
+	unsigned long		local_flags;	/* local (synchronous) flags */
 	unsigned long		tp_value;	/* thread pointer */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;	/* 0 => preemptible, <0 => BUG */
 	struct pt_regs		*regs;
 	unsigned long		syscall;	/* syscall number */
 	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
+	struct oob_thread_state	oob_state;
 };
 
 /*
@@ -40,9 +43,11 @@ struct thread_info {
 	.task		= &tsk,			\
 	.flags		= _TIF_FIXADE,		\
 	.cpu		= 0,			\
+ 	.local_flags	= 0,	\
 	.preempt_count	= INIT_PREEMPT_COUNT,	\
 }
 
+#define ti_local_flags(__ti)	((__ti)->local_flags)
 /* How to get the thread information struct from C. */
 register struct thread_info *__current_thread_info __asm__("$tp");
 
@@ -86,6 +91,8 @@ register unsigned long current_stack_pointer __asm__("$sp");
 #define TIF_LASX_CTX_LIVE	18	/* LASX context must be preserved */
 #define TIF_USEDLBT		19	/* LBT was used by this task this quantum (SMP) */
 #define TIF_LBT_CTX_LIVE	20	/* LBT context must be preserved */
+#define TIF_RETUSER		21	/* INBAND_TASK_RETUSER is pending */
+#define TIF_MAYDAY		22	/* Emergency trap pending */
 
 #define _TIF_SIGPENDING		(1<<TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1<<TIF_NEED_RESCHED)
@@ -105,6 +112,16 @@ register unsigned long current_stack_pointer __asm__("$sp");
 #define _TIF_LASX_CTX_LIVE	(1<<TIF_LASX_CTX_LIVE)
 #define _TIF_USEDLBT		(1<<TIF_USEDLBT)
 #define _TIF_LBT_CTX_LIVE	(1<<TIF_LBT_CTX_LIVE)
+#define _TIF_RETUSER		(1 << TIF_RETUSER)
+#define _TIF_SINGLESTEP         (1<<TIF_SINGLESTEP)
+
+/*
+ * Local (synchronous) thread flags.
+ */
+#define _TLF_OOB		0x0001
+#define _TLF_DOVETAIL		0x0002
+#define _TLF_OFFSTAGE		0x0004
+#define _TLF_OOBTRAP		0x0008
 
 #endif /* __KERNEL__ */
 #endif /* _ASM_THREAD_INFO_H */
arch/loongarch/kernel/Makefile +2 -0
index 4fcc168f0732..c1086fcf8c0b 100644
@@ -57,6 +57,8 @@ obj-$(CONFIG_MAGIC_SYSRQ)	+= sysrq.o
 
 obj-$(CONFIG_RELOCATABLE)	+= relocate.o
 
+obj-$(CONFIG_IRQ_PIPELINE) += irq_pipeline.o
+
 obj-$(CONFIG_KEXEC)		+= machine_kexec.o relocate_kernel.o
 obj-$(CONFIG_CRASH_DUMP)	+= crash_dump.o
 
arch/loongarch/kernel/asm-offsets.c +1 -0
index 8da0726777ed..02c4279f7ffb 100644
@@ -81,6 +81,7 @@ void output_thread_info_defines(void)
 	COMMENT("LoongArch thread_info offsets.");
 	OFFSET(TI_TASK, thread_info, task);
 	OFFSET(TI_FLAGS, thread_info, flags);
+	OFFSET(TI_LOCAL_FLAGS, thread_info, local_flags);
 	OFFSET(TI_TP_VALUE, thread_info, tp_value);
 	OFFSET(TI_CPU, thread_info, cpu);
 	OFFSET(TI_PRE_COUNT, thread_info, preempt_count);
arch/loongarch/kernel/irq.c +23 -0
index 883e5066ae44..f9e868cf8e4c 100644
@@ -16,6 +16,7 @@
 #include <linux/seq_file.h>
 #include <linux/kallsyms.h>
 #include <linux/uaccess.h>
+#include <linux/irq_pipeline.h>
 
 #include <asm/irq.h>
 #include <asm/loongson.h>
@@ -135,3 +136,25 @@ void __init init_IRQ(void)
 
 	set_csr_ecfg(ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 | ECFGF_IPI | ECFGF_PMC);
 }
+
+DEFINE_PER_CPU(int, irq_nesting);
+
+#ifdef CONFIG_IRQ_PIPELINE
+asmlinkage int
+handle_arch_irq_pipelined(struct pt_regs *regs)
+{
+	if (this_cpu_inc_return(irq_nesting) == 1) {
+		handle_irq_pipelined(regs);
+		this_cpu_dec(irq_nesting);
+		return running_inband() && !arch_irqs_disabled();
+	}
+
+	return handle_irq_pipelined(regs);
+}
+#else
+asmlinkage int
+handle_arch_irq_pipelined(struct pt_regs *regs)
+{
+	return handle_irq_pipelined(regs);
+}
+#endif
arch/loongarch/kernel/irq_pipeline.c new file +34 -0
new file mode 100644
index 000000000000..25352e50dc55
@@ -0,0 +1,34 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018 Philippe Gerum  <[email protected]>.
+ * Copyright (C) 2023 Hao Miao <[email protected]>.
+ * Copyright (C) 2024 Zhang Zheng <[email protected]>.
+ *
+ */
+
+#include <linux/irq.h>
+#include <linux/irq_pipeline.h>
+
+void arch_do_IRQ_pipelined(struct irq_desc *desc)
+{
+	struct pt_regs *regs = raw_cpu_ptr(&irq_pipeline.tick_regs);
+	struct pt_regs *old_regs = set_irq_regs(regs);
+
+	irq_enter();
+	handle_irq_desc(desc);
+	irq_exit();
+
+	set_irq_regs(old_regs);
+}
+
+
+void __init arch_irq_pipeline_init(void)
+{
+	/* no per-arch init. */
+}
+
+inline void arch_handle_irq_pipelined(struct pt_regs *regs)
+{
+	handle_arch_irq(regs);
+}
arch/loongarch/kernel/time.c +4 -0
index 3064af94db9c..a03b62aae83c 100644
@@ -179,6 +179,10 @@ int constant_clockevent_init(void)
 	if (request_irq(irq, constant_timer_interrupt, IRQF_PERCPU | IRQF_TIMER, "timer", NULL))
 		pr_err("Failed to request irq %d (timer)\n", irq);
 
+#ifdef CONFIG_IRQ_PIPELINE
+	irq_set_percpu_devid(irq);
+#endif
+
 	lpj_fine = get_loops_per_jiffy();
 	pr_info("Constant clock event device register\n");
 
arch/loongarch/kernel/traps.c +11 -2
index aebfc3733a76..f51371d1790e 100644
@@ -1053,8 +1053,13 @@ asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
 
 	cpu = smp_processor_id();
 
-	if (on_irq_stack(cpu, sp))
-		handle_loongarch_irq(regs);
+	if (on_irq_stack(cpu, sp)) {
+#ifdef CONFIG_IRQ_PIPELINE
+	handle_arch_irq_pipelined(regs);
+#else
+	handle_arch_irq(regs);
+#endif
+	}
 	else {
 		stack = per_cpu(irq_stack, cpu) + IRQ_STACK_START;
 
@@ -1065,7 +1070,11 @@ asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
 		"move	$s0, $sp		\n" /* Preserve sp */
 		"move	$sp, %[stk]		\n" /* Switch stack */
 		"move	$a0, %[regs]		\n"
+#ifdef CONFIG_IRQ_PIPELINE
+		"bl	handle_arch_irq_pipelined		\n"
+#else
 		"bl	handle_loongarch_irq	\n"
+#endif
 		"move	$sp, $s0		\n" /* Restore sp */
 		: /* No outputs */
 		: [stk] "r" (stack), [regs] "r" (regs)
Signature · 1 line
2.47.0
#3 lore raw

PATCH 3/6 dovetail: Adapt Loongarch's clocksource

In reply to Zheng Zhang

1. Adapting clock chip devices for proxying.

---
 arch/loongarch/kernel/time.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/loongarch/kernel/time.c +3 -3
index a03b62aae83c..c320e7e7489b 100644
@@ -37,7 +37,7 @@ static irqreturn_t constant_timer_interrupt(int irq, void *data)
 	/* Clear Timer Interrupt */
 	write_csr_tintclear(CSR_TINTCLR_TI);
 	cd = &per_cpu(constant_clockevent_device, cpu);
-	cd->event_handler(cd);
+	clockevents_handle_event(cd);
 
 	return IRQ_HANDLED;
 }
@@ -155,8 +155,8 @@ int constant_clockevent_init(void)
 	cd = &per_cpu(constant_clockevent_device, cpu);
 
 	cd->name = "Constant";
-	cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_PERCPU;
-
+	cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_PERCPU
+		| CLOCK_EVT_FEAT_PIPELINE;
 	cd->irq = irq;
 	cd->rating = 320;
 	cd->cpumask = cpumask_of(cpu);
Signature · 1 line
2.47.0
#4 lore raw

PATCH 4/6 dovetail: Add place holders for smp/ipi related functions

In reply to Zheng Zhang

ipi interrupts in loongarch is a bit triky when dovetail needs irq_desc for each oob ipi. Loongarch multiplex ipi through a iocsr register, which is similar to arm64's way to multiplex ipi through a PER_CPU veriable. The only problem is that there is only one irq_desc for ipi in Loongarch, and there is no way to let oob irq use the rest of the irq_desc. So one way is to multiplex all inband/oob ipi through iocsr register. But I didn't implement this yet.

---
 arch/loongarch/kernel/smp.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/loongarch/kernel/smp.c +23 -0
index ef35c871244f..fb2113688842 100644
@@ -698,3 +698,26 @@ void flush_tlb_one(unsigned long vaddr)
 	on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
 }
 EXPORT_SYMBOL(flush_tlb_one);
+
+// FIXME: ipi irq handling
+// not doing any thing yet
+
+int ipi_irq_base __read_mostly = 12;
+static void __smp_cross_call(const struct cpumask *target, unsigned int ipinr)
+{
+}
+
+void irq_send_oob_ipi(unsigned int irq,
+		const struct cpumask *cpumask)
+{
+	unsigned int sgi = irq - ipi_irq_base;
+
+	/**if (WARN_ON(irq_pipeline_debug() &&*/
+	/**      (sgi < OOB_IPI_OFFSET ||*/
+	/**       sgi >= OOB_IPI_OFFSET + OOB_NR_IPI)))*/
+	/**  return;*/
+
+	/* Out-of-band IPI (SGI1-2). */
+	__smp_cross_call(cpumask, sgi);
+}
+EXPORT_SYMBOL_GPL(irq_send_oob_ipi);
Signature · 1 line
2.47.0
#5 lore raw

PATCH 5/6 dovetail: Disable irq in mm and fpu's critical area

In reply to Zheng Zhang

Disable hardware interrupt in functhion like switch_mm.

---
 arch/loongarch/include/asm/mmu_context.h | 31 +++++++++++++++----
 arch/loongarch/kernel/alternative.c      |  4 +--
 arch/loongarch/kernel/idle.c             |  2 ++
 arch/loongarch/kernel/perf_event.c       |  8 ++---
 arch/loongarch/kernel/smp.c              |  5 +--
 arch/loongarch/kernel/traps.c            | 39 ++++++++++++++++++------
 arch/loongarch/mm/fault.c                |  4 +--
 arch/loongarch/mm/tlb.c                  | 17 ++++++-----
 8 files changed, 76 insertions(+), 34 deletions(-)
arch/loongarch/include/asm/mmu_context.h +25 -6
index 9f97c3453b9c..583a32228518 100644
@@ -12,6 +12,7 @@
 #include <linux/mm_types.h>
 #include <linux/smp.h>
 #include <linux/slab.h>
+#include <linux/irq_pipeline.h>
 
 #include <asm/cacheflush.h>
 #include <asm/tlbflush.h>
@@ -99,14 +100,14 @@ static inline void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *
 
 #define switch_mm_irqs_off switch_mm_irqs_off
 
-static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
+static inline void do_switch_mm(struct mm_struct *prev, struct mm_struct *next,
 			     struct task_struct *tsk)
 {
 	unsigned long flags;
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 	switch_mm_irqs_off(prev, next, tsk);
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 /*
@@ -117,9 +118,27 @@ static inline void destroy_context(struct mm_struct *mm)
 {
 }
 
-#define activate_mm(prev, next)	switch_mm(prev, next, current)
+static inline void
+switch_mm(struct mm_struct *prev, struct mm_struct *next,
+	  struct task_struct *tsk)
+{
+	unsigned long flags;
+
+	protect_inband_mm(flags);
+	do_switch_mm(prev, next, tsk);
+	unprotect_inband_mm(flags);
+}
+
+#define activate_mm(prev, next)	do_switch_mm(prev, next, current)
 #define deactivate_mm(task, mm)	do { } while (0)
 
+static inline void
+switch_oob_mm(struct mm_struct *prev, struct mm_struct *next,
+	      struct task_struct *tsk)
+{
+	do_switch_mm(prev, next, tsk);
+}
+
 /*
  * If mm is currently active, we can't really drop it.
  * Instead, we will get a new one for it.
@@ -130,7 +149,7 @@ drop_mmu_context(struct mm_struct *mm, unsigned int cpu)
 	int asid;
 	unsigned long flags;
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 
 	asid = read_csr_asid() & cpu_asid_mask(&current_cpu_data);
 
@@ -146,7 +165,7 @@ drop_mmu_context(struct mm_struct *mm, unsigned int cpu)
 	cpu_context(cpu, mm) = 0;
 	cpumask_clear_cpu(cpu, mm_cpumask(mm));
 out:
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 #endif /* _ASM_MMU_CONTEXT_H */
arch/loongarch/kernel/alternative.c +2 -2
index 4ad13847e962..0e24a482c4eb 100644
@@ -160,12 +160,12 @@ static void *__init_or_module text_poke_early(union loongarch_instruction *insn,
 	int i;
 	unsigned long flags;
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 
 	for (i = 0; i < nr; i++)
 		insn[i].word = buf[i].word;
 
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 
 	wbflush();
 	flush_icache_range((unsigned long)insn, (unsigned long)(insn + nr));
arch/loongarch/kernel/idle.c +2 -0
index 0b5dd2faeb90..5f2778287066 100644
@@ -4,6 +4,7 @@
  *
  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  */
+#include "asm-generic/irq_pipeline.h"
 #include <linux/cpu.h>
 #include <linux/irqflags.h>
 #include <asm/cpu.h>
@@ -11,6 +12,7 @@
 
 void __cpuidle arch_cpu_idle(void)
 {
+	hard_local_irq_enable();
 	raw_local_irq_enable();
 	__arch_cpu_idle(); /* idle instruction needs irq enabled */
 	raw_local_irq_disable();
arch/loongarch/kernel/perf_event.c +4 -4
index 0491bf453cd4..c7f99e28e644 100644
@@ -289,11 +289,11 @@ static void loongarch_pmu_disable_event(int idx)
 
 	WARN_ON(idx < 0 || idx >= loongarch_pmu.num_counters);
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 	cpuc->saved_ctrl[idx] = loongarch_pmu_read_control(idx) &
 		~M_PERFCTL_COUNT_EVENT_WHENEVER;
 	loongarch_pmu_write_control(idx, cpuc->saved_ctrl[idx]);
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 static int loongarch_pmu_event_set_period(struct perf_event *event,
@@ -826,14 +826,14 @@ static void pause_local_counters(void)
 	int ctr = loongarch_pmu.num_counters;
 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 	do {
 		ctr--;
 		cpuc->saved_ctrl[ctr] = loongarch_pmu_read_control(ctr);
 		loongarch_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
 					 ~M_PERFCTL_COUNT_EVENT_WHENEVER);
 	} while (ctr > 0);
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 static void resume_local_counters(void)
arch/loongarch/kernel/smp.c +3 -2
index fb2113688842..c455d8c8318c 100644
@@ -8,6 +8,7 @@
  * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
  * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
  */
+#include "linux/kern_levels.h"
 #include <linux/acpi.h>
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
@@ -301,10 +302,10 @@ int loongson_cpu_disable(void)
 #endif
 	set_cpu_online(cpu, false);
 	calculate_cpu_foreign_map();
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 	irq_migrate_all_off_this_cpu();
 	clear_csr_ecfg(ECFG0_IM);
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 	local_flush_tlb_all();
 
 	return 0;
arch/loongarch/kernel/traps.c +29 -10
index f51371d1790e..664323aa5db3 100644
@@ -484,13 +484,14 @@ asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
 	void __user *fault_addr;
 	irqentry_state_t state = irqentry_enter(regs);
 
+	oob_trap_notify(LOONGARCH64_TRAP_FPE, regs);
 	if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
 		       SIGFPE) == NOTIFY_STOP)
 		goto out;
 
 	/* Clear FCSR.Cause before enabling interrupts */
 	write_fcsr(LOONGARCH_FCSR0, fcsr & ~mask_fcsr_x(fcsr));
-	local_irq_enable();
+	hard_local_irq_enable();
 
 	die_if_kernel("FP exception in kernel code", regs);
 
@@ -501,7 +502,8 @@ asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
 	process_fpemu_return(sig, fault_addr, fcsr);
 
 out:
-	local_irq_disable();
+	hard_local_irq_disable();
+	oob_trap_unwind(LOONGARCH64_TRAP_FPE, regs);
 	irqentry_exit(regs, state);
 }
 
@@ -676,8 +678,10 @@ asmlinkage void noinstr do_bp(struct pt_regs *regs)
 	unsigned long era = exception_era(regs);
 	irqentry_state_t state = irqentry_enter(regs);
 
+
+	oob_trap_notify(LOONGARCH64_TRAP_BP, regs);
 	if (regs->csr_prmd & CSR_PRMD_PIE)
-		local_irq_enable();
+		hard_local_irq_enable();
 
 	if (__get_inst(&opcode, (u32 *)era, user))
 		goto out_sigsegv;
@@ -743,7 +747,9 @@ asmlinkage void noinstr do_bp(struct pt_regs *regs)
 
 out:
 	if (regs->csr_prmd & CSR_PRMD_PIE)
-		local_irq_disable();
+		hard_local_irq_disable();
+
+	oob_trap_unwind(LOONGARCH64_TRAP_BP, regs);
 
 	irqentry_exit(regs, state);
 	return;
@@ -813,7 +819,7 @@ asmlinkage void noinstr do_ri(struct pt_regs *regs)
 	unsigned int __user *era = (unsigned int __user *)exception_era(regs);
 	irqentry_state_t state = irqentry_enter(regs);
 
-	local_irq_enable();
+	hard_local_irq_enable();
 	current->thread.trap_nr = read_csr_excode();
 
 	if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
@@ -830,7 +836,7 @@ asmlinkage void noinstr do_ri(struct pt_regs *regs)
 	force_sig(status);
 
 out:
-	local_irq_disable();
+	hard_local_irq_disable();
 	irqentry_exit(regs, state);
 }
 
@@ -848,6 +854,19 @@ static void init_restore_fp(void)
 	BUG_ON(!is_fp_enabled());
 }
 
+#ifdef CONFIG_DOVETAIL
+void restore_fp_current_oob(void)
+{
+	unsigned long __flags;
+
+	if (current->mm && used_math() && !is_fpu_owner()) {
+		__flags = hard_preempt_disable();
+		own_fpu_inatomic(1);
+		hard_preempt_enable(__flags);
+	}
+}
+#endif
+
 static void init_restore_lsx(void)
 {
 	enable_lsx();
@@ -905,7 +924,7 @@ asmlinkage void noinstr do_fpu(struct pt_regs *regs)
 {
 	irqentry_state_t state = irqentry_enter(regs);
 
-	local_irq_enable();
+	hard_local_irq_enable();
 	die_if_kernel("do_fpu invoked from kernel context!", regs);
 	BUG_ON(is_lsx_enabled());
 	BUG_ON(is_lasx_enabled());
@@ -914,7 +933,7 @@ asmlinkage void noinstr do_fpu(struct pt_regs *regs)
 	init_restore_fp();
 	preempt_enable();
 
-	local_irq_disable();
+	hard_local_irq_disable();
 	irqentry_exit(regs, state);
 }
 
@@ -922,7 +941,7 @@ asmlinkage void noinstr do_lsx(struct pt_regs *regs)
 {
 	irqentry_state_t state = irqentry_enter(regs);
 
-	local_irq_enable();
+	hard_local_irq_enable();
 	if (!cpu_has_lsx) {
 		force_sig(SIGILL);
 		goto out;
@@ -936,7 +955,7 @@ asmlinkage void noinstr do_lsx(struct pt_regs *regs)
 	preempt_enable();
 
 out:
-	local_irq_disable();
+	hard_local_irq_disable();
 	irqentry_exit(regs, state);
 }
 
arch/loongarch/mm/fault.c +2 -2
index 1fc2f6813ea0..9d34556dd34c 100644
@@ -260,11 +260,11 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
 
 	/* Enable interrupt if enabled in parent context */
 	if (likely(regs->csr_prmd & CSR_PRMD_PIE))
-		local_irq_enable();
+		hard_local_irq_enable();
 
 	__do_page_fault(regs, write, address);
 
-	local_irq_disable();
+	hard_local_irq_disable();
 
 	irqentry_exit(regs, state);
 }
arch/loongarch/mm/tlb.c +9 -8
index 2c0a411f23aa..ee47e07e12d6 100644
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  */
+#include "asm-generic/irq_pipeline.h"
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/smp.h>
@@ -62,7 +63,7 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
 	if (asid_valid(mm, cpu)) {
 		unsigned long size, flags;
 
-		local_irq_save(flags);
+		flags = hard_local_irq_save();
 		start = round_down(start, PAGE_SIZE << 1);
 		end = round_up(end, PAGE_SIZE << 1);
 		size = (end - start) >> (PAGE_SHIFT + 1);
@@ -78,7 +79,7 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
 		} else {
 			drop_mmu_context(mm, cpu);
 		}
-		local_irq_restore(flags);
+		hard_local_irq_restore(flags);
 	} else {
 		cpumask_clear_cpu(cpu, mm_cpumask(mm));
 	}
@@ -88,7 +89,7 @@ void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
 	unsigned long size, flags;
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 	size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
 	size = (size + 1) >> 1;
 	if (size <= (current_cpu_data.tlbsizestlbsets ?
@@ -106,7 +107,7 @@ void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
 	} else {
 		local_flush_tlb_kernel();
 	}
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
@@ -141,7 +142,7 @@ static void __update_hugetlb(struct vm_area_struct *vma, unsigned long address,
 	unsigned long lo;
 	unsigned long flags;
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 
 	address &= (PAGE_MASK << 1);
 	write_csr_entryhi(address);
@@ -158,7 +159,7 @@ static void __update_hugetlb(struct vm_area_struct *vma, unsigned long address,
 		tlb_write_indexed();
 	write_csr_pagesize(PS_DEFAULT_SIZE);
 
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 #endif
 }
 
@@ -181,7 +182,7 @@ void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t *ptep
 		return;
 	}
 
-	local_irq_save(flags);
+	flags = hard_local_irq_save();
 
 	if ((unsigned long)ptep & sizeof(pte_t))
 		ptep--;
@@ -198,7 +199,7 @@ void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t *ptep
 	else
 		tlb_write_indexed();
 
-	local_irq_restore(flags);
+	hard_local_irq_restore(flags);
 }
 
 static void setup_ptwalker(void)
Signature · 1 line
2.47.0
#6 lore raw

PATCH 6/6 dovetail: Add more irq-chip support

In reply to Zheng Zhang
---
 drivers/irqchip/irq-loongarch-cpu.c    | 1 +
 drivers/irqchip/irq-loongson-pch-lpc.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/irqchip/irq-loongarch-cpu.c +1 -0
index 9d8f2c406043..411f93bba480 100644
@@ -65,6 +65,7 @@ static struct irq_chip cpu_irq_controller = {
 	.name		= "CPUINTC",
 	.irq_mask	= mask_loongarch_irq,
 	.irq_unmask	= unmask_loongarch_irq,
+	.flags      = IRQCHIP_PIPELINE_SAFE,
 };
 
 static void handle_cpu_irq(struct pt_regs *regs)
drivers/irqchip/irq-loongson-pch-lpc.c +1 -1
index 9b35492fb6be..fc11be962186 100644
@@ -97,7 +97,7 @@ static const struct irq_chip pch_lpc_irq_chip = {
 	.irq_unmask		= lpc_irq_unmask,
 	.irq_ack		= lpc_irq_ack,
 	.irq_set_type		= lpc_irq_set_type,
-	.flags			= IRQCHIP_SKIP_SET_WAKE,
+	.flags			= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_PIPELINE_SAFE,
 };
 
 static void lpc_irq_dispatch(struct irq_desc *desc)
Signature · 1 line
2.47.0
#7 lore raw
In reply to Zheng Zhang
Quoted 40 lines · Zheng Zhang

Hi all!

This patches is base on 6.6.y-dovetail-rebase aiming to port dovetail to Loongarch. Most of architecture related code has already been ported, except ipi part, which I will add in the later patches. This patch is tested under qemu-9.1.0 with irq torture test in single core.

If the community is interested in porting Dovetail to LoongArch, I am very willing to continue improving the current work and maintaining it in the future :^)

Zheng Zhang (6):
dovetail: Add dovetail and irq_pipeline option to Loongarch's Kconfig
dovetail: Port Architecture bits related code to Loongarch
dovetail: Adapt Loongarch's clocksource
dovetail: Add place holders for smp/ipi related functions
dovetail: Disable irq in mm and fpu's critical area
dovetail: Add more irq-chip support

 arch/loongarch/Kconfig                    |   3 +
 arch/loongarch/include/asm/dovetail.h     |  44 +++++++
 arch/loongarch/include/asm/irq.h          |   2 +
 arch/loongarch/include/asm/irq_pipeline.h | 145 ++++++++++++++++++++++
 arch/loongarch/include/asm/irqflags.h     |  28 +++--
 arch/loongarch/include/asm/mmu_context.h  |  31 ++++-
 arch/loongarch/include/asm/ptrace.h       |   3 +-
 arch/loongarch/include/asm/syscall.h      |   6 +
 arch/loongarch/include/asm/thread_info.h  |  17 +++
 arch/loongarch/kernel/Makefile            |   2 +
 arch/loongarch/kernel/alternative.c       |   4 +-
 arch/loongarch/kernel/asm-offsets.c       |   1 +
 arch/loongarch/kernel/idle.c              |   2 +
 arch/loongarch/kernel/irq.c               |  23 ++++
 arch/loongarch/kernel/irq_pipeline.c      |  34 +++++
 arch/loongarch/kernel/perf_event.c        |   8 +-
 arch/loongarch/kernel/smp.c               |  28 ++++-
 arch/loongarch/kernel/time.c              |  10 +-
 arch/loongarch/kernel/traps.c             |  52 ++++++--
Quoted 9 lines
 arch/loongarch/mm/fault.c                 |   4 +-
 arch/loongarch/mm/tlb.c                   |  17 +--
 drivers/irqchip/irq-loongarch-cpu.c       |   1 +
 drivers/irqchip/irq-loongson-pch-lpc.c    |   2 +-
 23 files changed, 412 insertions(+), 55 deletions(-)
 create mode 100644 arch/loongarch/include/asm/dovetail.h
 create mode 100644 arch/loongarch/include/asm/irq_pipeline.h
 create mode 100644 arch/loongarch/kernel/irq_pipeline.c

Thanks a lot for this contribution! I leave the detailed review to others ;). Is your plan to build EVL support on top?

We will eventually have to look at the testability of this, just like we will have for RISC-V. QEMU is a convenient starting point, but eventually there will be a need for real hardware as well, then also for building test images and finally running them on real HW as part of regular development jobs. But one after the other.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
author
#8 lore raw

Thanks for your kindly reply. Consider the missing IPI part may affect EVL
unit test result, so I didn't patch them yet. So I'm currently working on
port the IPI part, and plane to patch EVL just after it.

Build and test the image on real hardware is definitely consider as a finally goal for me. After patch and test EVL on QEMU, I then will choose a proper piece of hardware to test the image.

Zheng

Quoted 66 lines · Jan Kiszka <[email protected]> 于 2024年10月21日周一 19:55写道:
Quoted 40 lines · Zheng Zhang

Hi all!

This patches is base on 6.6.y-dovetail-rebase aiming to port dovetail to Loongarch. Most of architecture related code has already been ported, except ipi part, which I will add in the later patches. This patch is tested under qemu-9.1.0 with irq torture test in single core.

If the community is interested in porting Dovetail to LoongArch, I am very willing to continue improving the current work and maintaining it in the future :^)

Zheng Zhang (6):
dovetail: Add dovetail and irq_pipeline option to Loongarch's Kconfig
dovetail: Port Architecture bits related code to Loongarch
dovetail: Adapt Loongarch's clocksource
dovetail: Add place holders for smp/ipi related functions
dovetail: Disable irq in mm and fpu's critical area
dovetail: Add more irq-chip support

 arch/loongarch/Kconfig                    |   3 +
 arch/loongarch/include/asm/dovetail.h     |  44 +++++++
 arch/loongarch/include/asm/irq.h          |   2 +
 arch/loongarch/include/asm/irq_pipeline.h | 145 ++++++++++++++++++++++
 arch/loongarch/include/asm/irqflags.h     |  28 +++--
 arch/loongarch/include/asm/mmu_context.h  |  31 ++++-
 arch/loongarch/include/asm/ptrace.h       |   3 +-
 arch/loongarch/include/asm/syscall.h      |   6 +
 arch/loongarch/include/asm/thread_info.h  |  17 +++
 arch/loongarch/kernel/Makefile            |   2 +
 arch/loongarch/kernel/alternative.c       |   4 +-
 arch/loongarch/kernel/asm-offsets.c       |   1 +
 arch/loongarch/kernel/idle.c              |   2 +
 arch/loongarch/kernel/irq.c               |  23 ++++
 arch/loongarch/kernel/irq_pipeline.c      |  34 +++++
 arch/loongarch/kernel/perf_event.c        |   8 +-
 arch/loongarch/kernel/smp.c               |  28 ++++-
 arch/loongarch/kernel/time.c              |  10 +-
 arch/loongarch/kernel/traps.c             |  52 ++++++--
Quoted 9 lines
 arch/loongarch/mm/fault.c                 |   4 +-
 arch/loongarch/mm/tlb.c                   |  17 +--
 drivers/irqchip/irq-loongarch-cpu.c       |   1 +
 drivers/irqchip/irq-loongson-pch-lpc.c    |   2 +-
 23 files changed, 412 insertions(+), 55 deletions(-)
 create mode 100644 arch/loongarch/include/asm/dovetail.h
 create mode 100644 arch/loongarch/include/asm/irq_pipeline.h
 create mode 100644 arch/loongarch/kernel/irq_pipeline.c

Thanks a lot for this contribution! I leave the detailed review to others ;). Is your plan to build EVL support on top?

We will eventually have to look at the testability of this, just like we will have for RISC-V. QEMU is a convenient starting point, but eventually there will be a need for real hardware as well, then also for building test images and finally running them on real HW as part of regular development jobs. But one after the other.

Jan

--
Siemens AG, Technology
Linux Expert Center