riscv: sbi: Add support to test timer extension
Subsystems (3)
v6 cover riscv: sbi: Add support to test timer extension
This patch series adds support for testing the timer extension as defined in the RISC-V SBI specification. The first 2 patches add infrastructural support for handling interrupts, the next 2 patches add some helper routines that can be used by SBI extension tests, while the last patch adds the actual test for the timer extension.
v6:
- Addressed all of Andrew's comments on v5.
v5:
- Addressed all of Andrew's comments on v4.
- Updated the test to check if sip.STIP is cleared for both cases of setting the timer to -1 and masking the timer irq as per the spec.
- Updated the test to check if sie.STIE is writable for the mask irq test.
v4:
- Addressed all of Andrew's comments on v3.
v3:
- Addressed all of Andrew's comments on v2.
- Added 2 new patches to add sbi_probe and the delay and timer routines.
v2:
- Addressed all of the previous comments from Andrew.
- Updated the test to get the timer frequency value from the device tree and allow the test parameters to be specified in microseconds instead of cycles.
Andrew Jones (1):
riscv: Extend exception handling support for interrupts
James Raphael Tiovalen (4):
riscv: Update exception cause list
riscv: Add method to probe for SBI extensions
riscv: Add some delay and timer routines
riscv: sbi: Add test for timer extension
riscv/Makefile | 2 + lib/riscv/asm/csr.h | 21 +++++++ lib/riscv/asm/delay.h | 16 +++++ lib/riscv/asm/processor.h | 15 ++++- lib/riscv/asm/sbi.h | 6 ++ lib/riscv/asm/setup.h | 1 + lib/riscv/asm/timer.h | 24 +++++++ lib/riscv/delay.c | 21 +++++++ lib/riscv/processor.c | 27 ++++++-- lib/riscv/sbi.c | 13 ++++ lib/riscv/setup.c | 4 ++ lib/riscv/timer.c | 28 +++++++++ riscv/sbi.c | 127 ++++++++++++++++++++++++++++++++++++++ 13 files changed, 300 insertions(+), 5 deletions(-) create mode 100644 lib/riscv/asm/delay.h create mode 100644 lib/riscv/asm/timer.h create mode 100644 lib/riscv/delay.c create mode 100644 lib/riscv/timer.c
--
2.43.0
PATCH v6 1/5 riscv: Extend exception handling support for interrupts
From: Andrew Jones <[email protected]>
Add install_irq_handler() to enable tests to install interrupt handlers. Also add local_irq_enable() and local_irq_disable() to respectively enable and disable IRQs via the sstatus.SIE bit.
--- lib/riscv/asm/csr.h | 2 ++ lib/riscv/asm/processor.h | 13 +++++++++++++ lib/riscv/processor.c | 27 +++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-)
lib/riscv/asm/csr.h +2 -0
@@ -11,6 +11,8 @@#define CSR_STVAL 0x143#define CSR_SATP 0x180 +#define SR_SIE _AC(0x00000002, UL) +/* Exception cause high bit - is an interrupt if set */ #define CAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1))
lib/riscv/asm/processor.h +13 -0
@@ -5,6 +5,7 @@#include <asm/ptrace.h> #define EXCEPTION_CAUSE_MAX 16 +#define INTERRUPT_CAUSE_MAX 16typedef void (*exception_fn )(struct pt_regs *);
@@ -13,6 +14,7 @@ struct thread_info {unsigned long hartid;unsigned long isa[1 ];exception_fn exception_handlers[EXCEPTION_CAUSE_MAX ]; +exception_fn interrupt_handlers[INTERRUPT_CAUSE_MAX ]; };static inline struct thread_info *current_thread_info (void )
@@ -20,7 +22,18 @@ static inline struct thread_info *current_thread_info(void)return (struct thread_info *)csr_read (CSR_SSCRATCH ); } +static inline void local_irq_enable (void ) +{ +csr_set (CSR_SSTATUS ,SR_SIE ); +} + +static inline void local_irq_disable (void ) +{ +csr_clear (CSR_SSTATUS ,SR_SIE ); +} +void install_exception_handler (unsigned long cause,void (*handler)(struct pt_regs *)); +void install_irq_handler (unsigned long cause,void (*handler)(struct pt_regs *));void do_handle_exception (struct pt_regs *regs);void thread_info_init (void );
lib/riscv/processor.c +23 -4
@@ -36,10 +36,21 @@ void do_handle_exception(struct pt_regs *regs) {struct thread_info *info =current_thread_info (); -assert (regs->cause <EXCEPTION_CAUSE_MAX ); -if (info->exception_handlers[regs->cause]) { - info->exception_handlers[regs->cause](regs); -return ; +if (regs->cause &CAUSE_IRQ_FLAG ) { +unsigned long irq_cause = regs->cause & ~CAUSE_IRQ_FLAG ; + +assert (irq_cause <INTERRUPT_CAUSE_MAX ); +if (info->interrupt_handlers[irq_cause]) { + info->interrupt_handlers[irq_cause](regs); +return ; + } + }else { +assert (regs->cause <EXCEPTION_CAUSE_MAX ); + +if (info->exception_handlers[regs->cause]) { + info->exception_handlers[regs->cause](regs); +return ; + } }show_regs (regs);
@@ -47,6 +58,14 @@ void do_handle_exception(struct pt_regs *regs)abort (); } +void install_irq_handler (unsigned long cause,void (*handler)(struct pt_regs *)) +{ +struct thread_info *info =current_thread_info (); + +assert (cause <INTERRUPT_CAUSE_MAX ); + info->interrupt_handlers[cause] = handler; +} +void install_exception_handler (unsigned long cause,void (*handler)(struct pt_regs *)) {struct thread_info *info =current_thread_info ();
Signature · 1 line
2.43.0
PATCH v6 2/5 riscv: Update exception cause list
In reply to James Raphael TiovalenUpdate the list of exception and interrupt causes to follow the latest RISC-V privileged ISA specification (version 20240411 section 18.6.1).
--- lib/riscv/asm/csr.h | 10 ++++++++++ lib/riscv/asm/processor.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-)
lib/riscv/asm/csr.h +10 -0
@@ -36,6 +36,16 @@#define EXC_VIRTUAL_INST_FAULT 22#define EXC_STORE_GUEST_PAGE_FAULT 23 +/* Interrupt causes */ +#define IRQ_S_SOFT 1 +#define IRQ_VS_SOFT 2 +#define IRQ_S_TIMER 5 +#define IRQ_VS_TIMER 6 +#define IRQ_S_EXT 9 +#define IRQ_VS_EXT 10 +#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13 +#ifndef __ASSEMBLY__#define csr_swap (csr, val) \
lib/riscv/asm/processor.h +1 -1
@@ -4,7 +4,7 @@#include <asm/csr.h> #include <asm/ptrace.h> -#define EXCEPTION_CAUSE_MAX 16 +#define EXCEPTION_CAUSE_MAX 24#define INTERRUPT_CAUSE_MAX 16typedef void (*exception_fn )(struct pt_regs *);
Signature · 1 line
2.43.0
PATCH v6 3/5 riscv: Add method to probe for SBI extensions
In reply to James Raphael TiovalenAdd a sbi_probe helper method that can be used by SBI extension tests to check if a given extension is available.
--- lib/riscv/asm/sbi.h | 1 + lib/riscv/sbi.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
lib/riscv/asm/sbi.h +1 -0
@@ -49,6 +49,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,void sbi_shutdown (void );struct sbiret sbi_hart_start (unsigned long hartid,unsigned long entry,unsigned long sp); +long sbi_probe (int ext);#endif /* !__ASSEMBLY__ */ #endif /* _ASMRISCV_SBI_H_ */
lib/riscv/sbi.c +13 -0
@@ -38,3 +38,16 @@ struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned {return sbi_ecall (SBI_EXT_HSM ,SBI_EXT_HSM_HART_START , hartid, entry, sp,0 ,0 ,0 ); } + +long sbi_probe (int ext) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_GET_SPEC_VERSION ,0 ,0 ,0 ,0 ,0 ,0 ); +assert (!ret.error && ret.value >=2 ); + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_PROBE_EXT , ext,0 ,0 ,0 ,0 ,0 ); +assert (!ret.error); + +return ret.value; +}
Signature · 1 line
2.43.0
PATCH v6 4/5 riscv: Add some delay and timer routines
In reply to James Raphael TiovalenAdd a delay method that would allow tests to wait for some specified number of cycles. Also add a conversion helper method between microseconds and cycles. This conversion is done by using the timebase frequency, which is obtained during setup via the device tree.
--- riscv/Makefile | 2 ++ lib/riscv/asm/csr.h | 1 + lib/riscv/asm/delay.h | 16 ++++++++++++++++ lib/riscv/asm/setup.h | 1 + lib/riscv/asm/timer.h | 14 ++++++++++++++ lib/riscv/delay.c | 21 +++++++++++++++++++++ lib/riscv/setup.c | 4 ++++ lib/riscv/timer.c | 28 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+) create mode 100644 lib/riscv/asm/delay.h create mode 100644 lib/riscv/asm/timer.h create mode 100644 lib/riscv/delay.c create mode 100644 lib/riscv/timer.c
riscv/Makefile +2 -0
@@ -30,6 +30,7 @@ cflatobjs += lib/memregions.o cflatobjs += lib/on-cpus.o cflatobjs += lib/vmalloc.o cflatobjs += lib/riscv/bitops.o +cflatobjs += lib/riscv/delay.o cflatobjs += lib/riscv/io.o cflatobjs += lib/riscv/isa.o cflatobjs += lib/riscv/mmu.o
@@ -38,6 +39,7 @@ cflatobjs += lib/riscv/sbi.o cflatobjs += lib/riscv/setup.o cflatobjs += lib/riscv/smp.o cflatobjs += lib/riscv/stack.o +cflatobjs += lib/riscv/timer.o ifeq ($(ARCH),riscv32) cflatobjs += lib/ldiv32.o endif
lib/riscv/asm/csr.h +1 -0
@@ -10,6 +10,7 @@#define CSR_SCAUSE 0x142#define CSR_STVAL 0x143#define CSR_SATP 0x180 +#define CSR_TIME 0xc01#define SR_SIE _AC(0x00000002, UL)
lib/riscv/asm/delay.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_DELAY_H_ +#define _ASMRISCV_DELAY_H_ + +#include <libcflat.h> +#include <asm/setup.h> + +extern void delay (uint64_t cycles); +extern void udelay (unsigned long usecs); + +static inline uint64_t usec_to_cycles (uint64_t usec) +{ +return (timebase_frequency * usec) /1000000 ; +} + +#endif /* _ASMRISCV_DELAY_H_ */
lib/riscv/asm/setup.h +1 -0
@@ -7,6 +7,7 @@#define NR_CPUS 16extern struct thread_info cpus[NR_CPUS ];extern int nr_cpus; +extern uint64_t timebase_frequency;int hartid_to_cpu (unsigned long hartid);void io_init (void );
lib/riscv/asm/timer.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_TIMER_H_ +#define _ASMRISCV_TIMER_H_ + +#include <asm/csr.h> + +extern void timer_get_frequency (void ); + +static inline uint64_t timer_get_cycles (void ) +{ +return csr_read (CSR_TIME ); +} + +#endif /* _ASMRISCV_TIMER_H_ */
lib/riscv/delay.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2024, James Raphael Tiovalen <[email protected]> +*/ +#include <libcflat.h> +#include <asm/barrier.h> +#include <asm/delay.h> +#include <asm/timer.h> + +void delay (uint64_t cycles) +{ +uint64_t start =timer_get_cycles (); + +while ((timer_get_cycles () - start) < cycles) +cpu_relax (); +} + +void udelay (unsigned long usecs) +{ +delay (usec_to_cycles ((uint64_t )usecs)); +}
lib/riscv/setup.c +4 -0
@@ -20,6 +20,7 @@#include <asm/page.h> #include <asm/processor.h> #include <asm/setup.h> +#include <asm/timer.h> #define VA_BASE ((phys_addr_t)3 * SZ_1G)#if __riscv_xlen ==64
@@ -38,6 +39,7 @@ u32 initrd_size;struct thread_info cpus[NR_CPUS ];int nr_cpus; +uint64_t timebase_frequency;static struct mem_region riscv_mem_regions[NR_MEM_REGIONS +1 ];
@@ -199,6 +201,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)mem_init (PAGE_ALIGN (__pa (freemem)));cpu_init (); +timer_get_frequency ();thread_info_init ();io_init ();
@@ -264,6 +267,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) }cpu_init (); +timer_get_frequency ();thread_info_init ();io_init ();initrd_setup ();
lib/riscv/timer.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2024, James Raphael Tiovalen <[email protected]> +*/ +#include <libcflat.h> +#include <devicetree.h> +#include <asm/setup.h> +#include <asm/timer.h> + +void timer_get_frequency (void ) +{ +const struct fdt_property *prop; +u32 *data; +int cpus, len; + +assert_msg (dt_available (),"ACPI not yet supported" ); + +const void *fdt =dt_fdt (); + + cpus =fdt_path_offset (fdt,"/cpus" ); +assert (cpus >=0 ); + + prop =fdt_get_property (fdt, cpus,"timebase-frequency" , &len); +assert (prop !=NULL && len ==4 ); + + data = (u32 *)prop->data; + timebase_frequency =fdt32_to_cpu (*data); +}
Signature · 1 line
2.43.0
PATCH v6 5/5 riscv: sbi: Add test for timer extension
In reply to James Raphael TiovalenAdd a test for the set_timer function of the time extension. The test checks that:
- The time extension is available
- The installed timer interrupt handler is called
- The timer interrupt is received within a reasonable time interval
- The timer interrupt pending bit is cleared after the set_timer SBI call is made
- The timer interrupt can be cleared either by requesting a timer interrupt infinitely far into the future or by masking the timer interrupt
The timer interrupt delay can be set using the TIMER_DELAY environment variable in microseconds. The default delay value is 200 milliseconds. Since the interrupt can arrive a little later than the specified delay, allow some margin of error. This margin of error can be specified via the TIMER_MARGIN environment variable in microseconds. The default margin of error is 200 milliseconds.
--- lib/riscv/asm/csr.h | 8 +++ lib/riscv/asm/sbi.h | 5 ++ lib/riscv/asm/timer.h | 10 ++++ riscv/sbi.c | 127 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+)
lib/riscv/asm/csr.h +8 -0
@@ -4,11 +4,15 @@#include <linux/const.h> #define CSR_SSTATUS 0x100 +#define CSR_SIE 0x104#define CSR_STVEC 0x105#define CSR_SSCRATCH 0x140#define CSR_SEPC 0x141#define CSR_SCAUSE 0x142#define CSR_STVAL 0x143 +#define CSR_SIP 0x144 +#define CSR_STIMECMP 0x14d +#define CSR_STIMECMPH 0x15d#define CSR_SATP 0x180#define CSR_TIME 0xc01
@@ -47,6 +51,10 @@#define IRQ_S_GEXT 12#define IRQ_PMU_OVF 13 +#define IE_TIE (_AC(0x1, UL) << IRQ_S_TIMER) + +#define IP_TIP IE_TIE +#ifndef __ASSEMBLY__#define csr_swap (csr, val) \
lib/riscv/asm/sbi.h +5 -0
@@ -16,6 +16,7 @@enum sbi_ext_id {SBI_EXT_BASE =0x10 , +SBI_EXT_TIME =0x54494d45 ,SBI_EXT_HSM =0x48534d ,SBI_EXT_SRST =0x53525354 , };
@@ -37,6 +38,10 @@ enum sbi_ext_hsm_fid { SBI_EXT_HSM_HART_SUSPEND, }; +enum sbi_ext_time_fid { +SBI_EXT_TIME_SET_TIMER =0 , +}; +struct sbiret {long error;long value;
lib/riscv/asm/timer.h +10 -0
@@ -11,4 +11,14 @@ static inline uint64_t timer_get_cycles(void) return csr_read(CSR_TIME); } +static inline void timer_irq_enable (void ) +{ +csr_set (CSR_SIE ,IE_TIE ); +} + +static inline void timer_irq_disable (void ) +{ +csr_clear (CSR_SIE ,IE_TIE ); +} +#endif /* _ASMRISCV_TIMER_H_ */
riscv/sbi.c +127 -0
152 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [kvm-unit-tests PATCH v6 5/5] riscv: sbi: Add test for timer extension
Quoted 10 lines · James Raphael Tiovalen
Add a test for the
set_timerfunction of the time extension. The test checks that:
- The time extension is available
- The installed timer interrupt handler is called
- The timer interrupt is received within a reasonable time interval
- The timer interrupt pending bit is cleared after theset_timerSBI call is made
- The timer interrupt can be cleared either by requesting a timer interrupt infinitely far into the future or by masking the timer interrupt
I've modified the "or by masking the timer interrupt" test to work as I'd expect it to based on a bit more thought about what the SBI TIME extension spec is trying to say (we should clarify the spec with the PR you've written). I also added a test for ensuring a timer is pending immediately when setting the time with a value of zero. All tests pass.
I also moved some code around a bit and a couple other minor cleanups.
And (drum roll, please), it's now merged!
Thanks,
drew
Re: [kvm-unit-tests PATCH v6 5/5] riscv: sbi: Add test for timer extension
Quoted 13 lines · On Fri, Aug 2, 2024 at 4:36?PM Andrew Jones <[email protected]> wrote:
Quoted 10 lines · James Raphael Tiovalen
Add a test for the
set_timerfunction of the time extension. The test checks that:
- The time extension is available
- The installed timer interrupt handler is called
- The timer interrupt is received within a reasonable time interval
- The timer interrupt pending bit is cleared after theset_timerSBI call is made
- The timer interrupt can be cleared either by requesting a timer interrupt infinitely far into the future or by masking the timer interrupt
Quoted 6 lines
I've modified the "or by masking the timer interrupt" test to work as I'd expect it to based on a bit more thought about what the SBI TIME extension spec is trying to say (we should clarify the spec with the PR you've written). I also added a test for ensuring a timer is pending immediately when setting the time with a value of zero. All tests pass.
Sure, I will reopen the clarification PR and work on it.
Quoted 3 lines
I also moved some code around a bit and a couple other minor cleanups.
And (drum roll, please), it's now merged!
Awesome, thanks Andrew!
Quoted 3 lines
Thanks,
drew
Best regards,
James Raphael Tiovalen