Skip to content

Commit 9f7a8ff

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Prefer sstc extension if available
RISC-V ISA has sstc extension which allows updating the next clock event via a CSR (stimecmp) instead of an SBI call. This should happen dynamically if sstc extension is available. Otherwise, it will fallback to SBI call to maintain backward compatibility. Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Guo Ren <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 464b018 commit 9f7a8ff

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

drivers/clocksource/timer-riscv.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* either be read from the "time" and "timeh" CSRs, and can use the SBI to
88
* setup events, or directly accessed using MMIO registers.
99
*/
10+
11+
#define pr_fmt(fmt) "riscv-timer: " fmt
12+
1013
#include <linux/clocksource.h>
1114
#include <linux/clockchips.h>
1215
#include <linux/cpu.h>
@@ -20,14 +23,28 @@
2023
#include <linux/of_irq.h>
2124
#include <clocksource/timer-riscv.h>
2225
#include <asm/smp.h>
26+
#include <asm/hwcap.h>
2327
#include <asm/sbi.h>
2428
#include <asm/timex.h>
2529

30+
static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
31+
2632
static int riscv_clock_next_event(unsigned long delta,
2733
struct clock_event_device *ce)
2834
{
35+
u64 next_tval = get_cycles64() + delta;
36+
2937
csr_set(CSR_IE, IE_TIE);
30-
sbi_set_timer(get_cycles64() + delta);
38+
if (static_branch_likely(&riscv_sstc_available)) {
39+
#if defined(CONFIG_32BIT)
40+
csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
41+
csr_write(CSR_STIMECMPH, next_tval >> 32);
42+
#else
43+
csr_write(CSR_STIMECMP, next_tval);
44+
#endif
45+
} else
46+
sbi_set_timer(next_tval);
47+
3148
return 0;
3249
}
3350

@@ -165,6 +182,12 @@ static int __init riscv_timer_init_dt(struct device_node *n)
165182
if (error)
166183
pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
167184
error);
185+
186+
if (riscv_isa_extension_available(NULL, SSTC)) {
187+
pr_info("Timer interrupt in S-mode is available via sstc extension\n");
188+
static_branch_enable(&riscv_sstc_available);
189+
}
190+
168191
return error;
169192
}
170193

0 commit comments

Comments
 (0)