diff --git a/arch/common/timing_info_bench.c b/arch/common/timing_info_bench.c index 1dbc3e78400..2ff0deaa128 100644 --- a/arch/common/timing_info_bench.c +++ b/arch/common/timing_info_bench.c @@ -76,6 +76,14 @@ u64_t __common_var_swap_end_time; << 16 | \ (IORD_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE))) \ - ((u32_t)val)) + + +#elif CONFIG_RISCV32 +#define TIMING_INFO_PRE_READ() +#define TIMING_INFO_OS_GET_TIME() (k_cycle_get_32()) +#define TIMING_INFO_GET_TIMER_VALUE() (k_cycle_get_32()) +#define SUBTRACT_CLOCK_CYCLES(val) ((u32_t)val) + #else /* Default case */ #error "Benchmarks have not been implemented for this architecture" diff --git a/arch/riscv32/core/isr.S b/arch/riscv32/core/isr.S index 852432cf56e..8ea1eed8bee 100644 --- a/arch/riscv32/core/isr.S +++ b/arch/riscv32/core/isr.S @@ -100,6 +100,9 @@ SECTION_FUNC(exception.entry, __irq_wrapper) sw a6, __NANO_ESF_a6_OFFSET(sp) sw a7, __NANO_ESF_a7_OFFSET(sp) +#ifdef CONFIG_EXECUTION_BENCHMARKING + call read_timer_start_of_isr +#endif /* Save MEPC register */ csrr t0, mepc sw t0, __NANO_ESF_mepc_OFFSET(sp) @@ -261,6 +264,9 @@ call_irq: /* Load ISR function address in register t1 */ lw t1, 0x04(t0) +#ifdef CONFIG_EXECUTION_BENCHMARKING + call read_timer_end_of_isr +#endif /* Call ISR function */ jalr ra, t1 @@ -364,6 +370,53 @@ reschedule: lw s10, _thread_offset_to_s10(t1) lw s11, _thread_offset_to_s11(t1) +#ifdef CONFIG_EXECUTION_BENCHMARKING + addi sp, sp, -__NANO_ESF_SIZEOF + + sw ra, __NANO_ESF_ra_OFFSET(sp) + sw gp, __NANO_ESF_gp_OFFSET(sp) + sw tp, __NANO_ESF_tp_OFFSET(sp) + sw t0, __NANO_ESF_t0_OFFSET(sp) + sw t1, __NANO_ESF_t1_OFFSET(sp) + sw t2, __NANO_ESF_t2_OFFSET(sp) + sw t3, __NANO_ESF_t3_OFFSET(sp) + sw t4, __NANO_ESF_t4_OFFSET(sp) + sw t5, __NANO_ESF_t5_OFFSET(sp) + sw t6, __NANO_ESF_t6_OFFSET(sp) + sw a0, __NANO_ESF_a0_OFFSET(sp) + sw a1, __NANO_ESF_a1_OFFSET(sp) + sw a2, __NANO_ESF_a2_OFFSET(sp) + sw a3, __NANO_ESF_a3_OFFSET(sp) + sw a4, __NANO_ESF_a4_OFFSET(sp) + sw a5, __NANO_ESF_a5_OFFSET(sp) + sw a6, __NANO_ESF_a6_OFFSET(sp) + sw a7, __NANO_ESF_a7_OFFSET(sp) + + call read_timer_end_of_swap + + lw ra, __NANO_ESF_ra_OFFSET(sp) + lw gp, __NANO_ESF_gp_OFFSET(sp) + lw tp, __NANO_ESF_tp_OFFSET(sp) + lw t0, __NANO_ESF_t0_OFFSET(sp) + lw t1, __NANO_ESF_t1_OFFSET(sp) + lw t2, __NANO_ESF_t2_OFFSET(sp) + lw t3, __NANO_ESF_t3_OFFSET(sp) + lw t4, __NANO_ESF_t4_OFFSET(sp) + lw t5, __NANO_ESF_t5_OFFSET(sp) + lw t6, __NANO_ESF_t6_OFFSET(sp) + lw a0, __NANO_ESF_a0_OFFSET(sp) + lw a1, __NANO_ESF_a1_OFFSET(sp) + lw a2, __NANO_ESF_a2_OFFSET(sp) + lw a3, __NANO_ESF_a3_OFFSET(sp) + lw a4, __NANO_ESF_a4_OFFSET(sp) + lw a5, __NANO_ESF_a5_OFFSET(sp) + lw a6, __NANO_ESF_a6_OFFSET(sp) + lw a7, __NANO_ESF_a7_OFFSET(sp) + + /* Release stack space */ + addi sp, sp, __NANO_ESF_SIZEOF +#endif + no_reschedule: #ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE /* Restore context at SOC level */ diff --git a/arch/riscv32/core/swap.S b/arch/riscv32/core/swap.S index 3a0a571efa4..ed18cd6d6fe 100644 --- a/arch/riscv32/core/swap.S +++ b/arch/riscv32/core/swap.S @@ -23,6 +23,52 @@ GTEXT(_thread_entry_wrapper) SECTION_FUNC(exception.other, __swap) /* Make a system call to perform context switch */ +#ifdef CONFIG_EXECUTION_BENCHMARKING + addi sp, sp, -__NANO_ESF_SIZEOF + + sw ra, __NANO_ESF_ra_OFFSET(sp) + sw gp, __NANO_ESF_gp_OFFSET(sp) + sw tp, __NANO_ESF_tp_OFFSET(sp) + sw t0, __NANO_ESF_t0_OFFSET(sp) + sw t1, __NANO_ESF_t1_OFFSET(sp) + sw t2, __NANO_ESF_t2_OFFSET(sp) + sw t3, __NANO_ESF_t3_OFFSET(sp) + sw t4, __NANO_ESF_t4_OFFSET(sp) + sw t5, __NANO_ESF_t5_OFFSET(sp) + sw t6, __NANO_ESF_t6_OFFSET(sp) + sw a0, __NANO_ESF_a0_OFFSET(sp) + sw a1, __NANO_ESF_a1_OFFSET(sp) + sw a2, __NANO_ESF_a2_OFFSET(sp) + sw a3, __NANO_ESF_a3_OFFSET(sp) + sw a4, __NANO_ESF_a4_OFFSET(sp) + sw a5, __NANO_ESF_a5_OFFSET(sp) + sw a6, __NANO_ESF_a6_OFFSET(sp) + sw a7, __NANO_ESF_a7_OFFSET(sp) + + call read_timer_start_of_swap + + lw ra, __NANO_ESF_ra_OFFSET(sp) + lw gp, __NANO_ESF_gp_OFFSET(sp) + lw tp, __NANO_ESF_tp_OFFSET(sp) + lw t0, __NANO_ESF_t0_OFFSET(sp) + lw t1, __NANO_ESF_t1_OFFSET(sp) + lw t2, __NANO_ESF_t2_OFFSET(sp) + lw t3, __NANO_ESF_t3_OFFSET(sp) + lw t4, __NANO_ESF_t4_OFFSET(sp) + lw t5, __NANO_ESF_t5_OFFSET(sp) + lw t6, __NANO_ESF_t6_OFFSET(sp) + lw a0, __NANO_ESF_a0_OFFSET(sp) + lw a1, __NANO_ESF_a1_OFFSET(sp) + lw a2, __NANO_ESF_a2_OFFSET(sp) + lw a3, __NANO_ESF_a3_OFFSET(sp) + lw a4, __NANO_ESF_a4_OFFSET(sp) + lw a5, __NANO_ESF_a5_OFFSET(sp) + lw a6, __NANO_ESF_a6_OFFSET(sp) + lw a7, __NANO_ESF_a7_OFFSET(sp) + + /* Release stack space */ + addi sp, sp, __NANO_ESF_SIZEOF +#endif ecall /* diff --git a/drivers/timer/riscv_machine_timer.c b/drivers/timer/riscv_machine_timer.c index 05e6219b039..6b9f5949f61 100644 --- a/drivers/timer/riscv_machine_timer.c +++ b/drivers/timer/riscv_machine_timer.c @@ -64,11 +64,20 @@ static ALWAYS_INLINE void riscv_machine_rearm_timer(void) static void riscv_machine_timer_irq_handler(void *unused) { ARG_UNUSED(unused); +#ifdef CONFIG_EXECUTION_BENCHMARKING + extern void read_timer_start_of_tick_handler(void); + read_timer_start_of_tick_handler(); +#endif _sys_clock_tick_announce(); /* Rearm timer */ riscv_machine_rearm_timer(); + +#ifdef CONFIG_EXECUTION_BENCHMARKING + extern void read_timer_end_of_tick_handler(void); + read_timer_end_of_tick_handler(); +#endif } #ifdef CONFIG_TICKLESS_IDLE diff --git a/tests/benchmarks/timing_info/src/timing_info.h b/tests/benchmarks/timing_info/src/timing_info.h index 834e5791478..5516e343b8a 100644 --- a/tests/benchmarks/timing_info/src/timing_info.h +++ b/tests/benchmarks/timing_info/src/timing_info.h @@ -86,6 +86,14 @@ << 16 | \ (IORD_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE))) \ - ((u32_t)val)) + + +#elif CONFIG_RISCV32 +#define TIMING_INFO_PRE_READ() +#define TIMING_INFO_OS_GET_TIME() (k_cycle_get_32()) +#define TIMING_INFO_GET_TIMER_VALUE() (k_cycle_get_32()) +#define SUBTRACT_CLOCK_CYCLES(val) ((u32_t)val) + #endif /* CONFIG_NRF_RTC_TIMER */ /******************************************************************************/ diff --git a/tests/benchmarks/timing_info/testcase.yaml b/tests/benchmarks/timing_info/testcase.yaml index fc2306047ec..a75775cb1e5 100644 --- a/tests/benchmarks/timing_info/testcase.yaml +++ b/tests/benchmarks/timing_info/testcase.yaml @@ -1,6 +1,6 @@ tests: benchmark.timing.default_kernel: - arch_whitelist: x86 arm arc xtensa nios2 + arch_whitelist: x86 arm arc xtensa nios2 riscv32 tags: benchmark benchmark.timing.userspace: filter: CONFIG_ARCH_HAS_USERSPACE