From cadaabcc720bb20d6d604c4792acb29072d2882d Mon Sep 17 00:00:00 2001 From: Mitchell Horne Date: Thu, 25 May 2023 14:07:49 -0300 Subject: [PATCH] riscv timer: use stimecmp CSR when available The Sstc extension defines a new stimecmp CSR, allowing supervisor software to set the timer, rather than just read it. When supported, using this avoids the frequent trips through the SBI every time the CPU's timer expires. Reviewed by: jhb MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40241 --- sys/riscv/riscv/timer.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sys/riscv/riscv/timer.c b/sys/riscv/riscv/timer.c index 8f01ef2a720..d23d88d2c0e 100644 --- a/sys/riscv/riscv/timer.c +++ b/sys/riscv/riscv/timer.c @@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include @@ -84,6 +85,16 @@ get_timecount(void) return (rdtime()); } +static inline void +set_timecmp(uint64_t timecmp) +{ + + if (has_sstc) + csr_write(stimecmp, timecmp); + else + sbi_set_timer(timecmp); +} + static u_int riscv_timer_tc_get_timecount(struct timecounter *tc __unused) { @@ -107,7 +118,7 @@ riscv_timer_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period) if (first != 0) { counts = ((uint32_t)et->et_frequency * first) >> 32; - sbi_set_timer(get_timecount() + counts); + set_timecmp(get_timecount() + counts); csr_set(sie, SIE_STIE); return (0);