From b283b949b6024787070278badf11c1557cff1ba5 Mon Sep 17 00:00:00 2001 From: Inochi Amaoto Date: Sun, 28 Apr 2024 18:20:50 +0800 Subject: [PATCH] arch/risc-v: implement standard ecall interface for nuttsbi NuttSBI have a simple ecall interface for the kernel, which make it hard to add new SBI call for NuttSBI. So implement standard ecall interface for NuttSBI and make life easier. Signed-off-by: Inochi Amaoto --- arch/risc-v/src/common/riscv_internal.h | 13 +++ arch/risc-v/src/common/supervisor/riscv_sbi.c | 26 +---- arch/risc-v/src/nuttsbi/sbi_mcall.c | 30 +++--- arch/risc-v/src/nuttsbi/sbi_mcall.h | 101 +----------------- 4 files changed, 37 insertions(+), 133 deletions(-) diff --git a/arch/risc-v/src/common/riscv_internal.h b/arch/risc-v/src/common/riscv_internal.h index 9bf3725e67..4a813f4f4d 100644 --- a/arch/risc-v/src/common/riscv_internal.h +++ b/arch/risc-v/src/common/riscv_internal.h @@ -168,6 +168,19 @@ #endif +/* SBI Extension IDs */ + +#define SBI_EXT_HSM 0x48534D +#define SBI_EXT_TIME 0x54494D45 + +/* SBI function IDs for TIME extension */ + +#define SBI_EXT_TIME_SET_TIMER 0x0 + +/* SBI function IDs for HSM extension */ + +#define SBI_EXT_HSM_HART_START 0x0 + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/arch/risc-v/src/common/supervisor/riscv_sbi.c b/arch/risc-v/src/common/supervisor/riscv_sbi.c index 75f8c34fe5..dbe14fe836 100644 --- a/arch/risc-v/src/common/supervisor/riscv_sbi.c +++ b/arch/risc-v/src/common/supervisor/riscv_sbi.c @@ -22,19 +22,6 @@ * Pre-processor Definitions ****************************************************************************/ -/* SBI Extension IDs */ - -#define SBI_EXT_HSM 0x48534D -#define SBI_EXT_TIME 0x54494D45 - -/* SBI function IDs for TIME extension */ - -#define SBI_EXT_TIME_SET_TIMER 0x0 - -/* SBI function IDs for HSM extension */ - -#define SBI_EXT_HSM_HART_START 0x0 - /**************************************************************************** * Included Files ****************************************************************************/ @@ -54,7 +41,6 @@ * Private Functions ****************************************************************************/ -#ifndef CONFIG_NUTTSBI static inline uintptr_t sbi_ecall(unsigned int extid, unsigned int fid, uintptr_t parm0, uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, @@ -79,7 +65,6 @@ static inline uintptr_t sbi_ecall(unsigned int extid, unsigned int fid, return r1; } -#endif /* CONFIG_NUTTSBI */ /**************************************************************************** * Public Functions @@ -98,9 +83,6 @@ static inline uintptr_t sbi_ecall(unsigned int extid, unsigned int fid, void riscv_sbi_set_timer(uint64_t stime_value) { -#ifdef CONFIG_NUTTSBI - sbi_mcall_set_timer(stime_value); -#else #ifdef CONFIG_ARCH_RV64 sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, 0, 0, 0, 0, 0); @@ -108,7 +90,6 @@ void riscv_sbi_set_timer(uint64_t stime_value) sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, stime_value >> 32, 0, 0, 0, 0); #endif -#endif } /**************************************************************************** @@ -125,9 +106,9 @@ void riscv_sbi_set_timer(uint64_t stime_value) uint64_t riscv_sbi_get_time(void) { #ifdef CONFIG_NUTTSBI - return sbi_mcall_get_time(); -#else -#ifdef CONFIG_ARCH_RV64 + return sbi_ecall(SBI_EXT_FIRMWARE, SBI_EXT_FIRMWARE_GET_MTIME, 0, 0, + 0, 0, 0, 0); +#elif defined(CONFIG_ARCH_RV64) return READ_CSR(CSR_TIME); #else uint32_t hi; @@ -142,7 +123,6 @@ uint64_t riscv_sbi_get_time(void) return (((uint64_t) hi) << 32) | lo; #endif -#endif } #ifndef CONFIG_NUTTSBI diff --git a/arch/risc-v/src/nuttsbi/sbi_mcall.c b/arch/risc-v/src/nuttsbi/sbi_mcall.c index d69fdeb752..772726769f 100644 --- a/arch/risc-v/src/nuttsbi/sbi_mcall.c +++ b/arch/risc-v/src/nuttsbi/sbi_mcall.c @@ -51,9 +51,25 @@ void sbi_mcall_handle(uintptr_t *regs) { /* Check the environment call number */ - switch (regs[REG_A0]) + switch (regs[REG_A7]) { - case MCALL_GET_TIMER: + case SBI_EXT_TIME: + switch (regs[REG_A6]) + { + case SBI_EXT_TIME_SET_TIMER: +#ifdef CONFIG_ARCH_RV64 + sbi_set_mtimecmp(regs[REG_A0]); +#else + sbi_set_mtimecmp(regs[REG_A0] + ((uint64_t)regs[REG_A1] << 32)); +#endif + CLEAR_CSR(CSR_MIP, MIP_STIP); + SET_CSR(CSR_MIE, MIE_MTIE); + break; + default: + break; + } + break; + case SBI_EXT_FIRMWARE: #ifdef CONFIG_ARCH_RV64 regs[REG_A0] = sbi_get_mtime(); #else @@ -65,16 +81,6 @@ void sbi_mcall_handle(uintptr_t *regs) #endif break; - case MCALL_SET_TIMER: -#ifdef CONFIG_ARCH_RV64 - sbi_set_mtimecmp(regs[REG_A1]); -#else - sbi_set_mtimecmp(regs[REG_A1] + ((uint64_t)regs[REG_A2] << 32)); -#endif - CLEAR_CSR(CSR_MIP, MIP_STIP); - SET_CSR(CSR_MIE, MIE_MTIE); - break; - default: break; } diff --git a/arch/risc-v/src/nuttsbi/sbi_mcall.h b/arch/risc-v/src/nuttsbi/sbi_mcall.h index 95154a2eac..f641233947 100644 --- a/arch/risc-v/src/nuttsbi/sbi_mcall.h +++ b/arch/risc-v/src/nuttsbi/sbi_mcall.h @@ -30,105 +30,10 @@ #include /**************************************************************************** - * Public Types + * Pre-processor Definitions ****************************************************************************/ -enum mcall_num_e -{ - MCALL_INVALID, - MCALL_GET_TIMER, - MCALL_SET_TIMER, - MCALL_LAST -}; - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Name: mcall0 - * - * Description: - * Generate an environment call to machine mode - * - * Input Parameters: - * nbr - Environment call number - * - ****************************************************************************/ - -static inline uintptr_t mcall0(unsigned int nbr) -{ - register long r0 asm("a0") = (long)(nbr); - - asm volatile - ( - "ecall" - :: "r"(r0) - : "memory" - ); - - asm volatile("nop" : "=r"(r0)); - - return r0; -} - -/**************************************************************************** - * Name: mcall1 - * - * Description: - * Generate an environment call to machine mode - * - * Input Parameters: - * nbr - Environment call number - * parm1 - Argument for ecall - * - ****************************************************************************/ - -static inline uintptr_t mcall1(unsigned int nbr, uintptr_t parm1) -{ - register long r0 asm("a0") = (long)(nbr); - register long r1 asm("a1") = (long)(parm1); - - asm volatile - ( - "ecall" - :: "r"(r0), "r"(r1) - : "memory" - ); - - asm volatile("nop" : "=r"(r0)); - - return r0; -} - -/**************************************************************************** - * Name: sbi_mcall_get_time - * - * Description: - * Read the current system time (mtime) - * - * Returned Value: - * time - Value of mtime - * - ****************************************************************************/ - -#define sbi_mcall_get_time() \ - mcall0(MCALL_GET_TIMER) - -/**************************************************************************** - * Name: sbi_mcall_set_timer - * - * Description: - * Set new compare match value for timer. Time is in absolute time, so - * user must either obtain system time and calculate the offset, or keep - * the old compare match value in memory - * - * Input Parameters: - * stime_value - Absolute time for next compare match event - * - ****************************************************************************/ - -#define sbi_mcall_set_timer(stime_value) \ - (void)mcall1(MCALL_SET_TIMER, stime_value) +#define SBI_EXT_FIRMWARE 0x0A000000 +#define SBI_EXT_FIRMWARE_GET_MTIME 0x0 #endif /* __ARCH_RISC_V_SRC_NUTTSBI_SBI_MCALL_H */