From d96ed13cbde3237b826c6ddc4a2930f061022bfa Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Mon, 9 Dec 2024 13:59:06 +0800 Subject: [PATCH] risc-v: Accelerate schedule by percpu Fetch TCB info in percpu scratch register for better performance. Signed-off-by: Huang Qi --- arch/risc-v/include/irq.h | 23 +++++++++++++++++++++++ arch/risc-v/src/common/riscv_cpustart.c | 9 ++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/risc-v/include/irq.h b/arch/risc-v/include/irq.h index 544784cc60..b45f7ff951 100644 --- a/arch/risc-v/include/irq.h +++ b/arch/risc-v/include/irq.h @@ -703,6 +703,29 @@ irqstate_t up_irq_enable(void); int up_cpu_index(void) noinstrument_function; #endif /* CONFIG_ARCH_HAVE_MULTICPU */ +/**************************************************************************** + * Schedule acceleration macros + ****************************************************************************/ + +/* If RISCV_PERCPU_SCRATCH is not enabled, we can use the scratch register + * to store the current task pointer. + */ + +#ifndef CONFIG_RISCV_PERCPU_SCRATCH +#define up_this_task() \ + ({ \ + struct tcb_s *t; \ + t = (struct tcb_s *)READ_CSR(CSR_SCRATCH); \ + t; \ + }) + +#define up_update_task(t) WRITE_CSR(CSR_SCRATCH, (uintptr_t)t) +#else +/* TODO: Implement up_this_task()/up_update_task() if RISCV_PERCPU_SCRATCH + * enabled. + */ +#endif + /**************************************************************************** * Name: up_this_cpu * diff --git a/arch/risc-v/src/common/riscv_cpustart.c b/arch/risc-v/src/common/riscv_cpustart.c index 5b046ca326..242d7d7878 100644 --- a/arch/risc-v/src/common/riscv_cpustart.c +++ b/arch/risc-v/src/common/riscv_cpustart.c @@ -37,6 +37,8 @@ #include #include +#include + #include "sched/sched.h" #include "init/init.h" #include "riscv_internal.h" @@ -69,6 +71,8 @@ void riscv_cpu_boot(int cpu) { + struct tcb_s *tcb; + /* Clear IPI for CPU(cpu) */ riscv_ipi_clear(cpu); @@ -100,8 +104,9 @@ void riscv_cpu_boot(int cpu) _info("CPU%d Started\n", this_cpu()); + tcb = current_task(this_cpu()); + #ifdef CONFIG_STACK_COLORATION - struct tcb_s *tcb = this_task(); /* If stack debug is enabled, then fill the stack with a * recognizable value that we can use later to test for high @@ -111,6 +116,8 @@ void riscv_cpu_boot(int cpu) riscv_stack_color(tcb->stack_alloc_ptr, 0); #endif + up_update_task(tcb); + /* TODO: Setup FPU */ /* Clear machine software interrupt for CPU(cpu) */