clocksource/drivers/timer-clint: Add ZICNTR support
Subsystems (2)
PATCH v8 1/2 clocksource/drivers/timer-clint: Add ZICNTR support
ZICNTR allows the CPU time to be read using CSRs, which used to be the default. If ZICNTR is supported prefer using CSRs over MMIO. The clint MMIO is covered in the RISC-V ACLINT spec [1], which has not been ratified yet, where as the ZICNTR spec is ratified. Using the CSRs also used to be the default, but was changed in Commit d5be89a8d118 ("RISC-V: Resurrect the MMIO timer implementation for M-mode systems") due to K210 not implementing rdtime in M-mode.
[1]: https://github.com/riscvarchive/riscv-aclint
---
Treat this as a completely new patch, as it is mostly rewritten. Original:
https://lore.kernel.org/all/[email protected]/
V3 -> V4:
- Add riscv,csr-clint
- Allow using of CSRs in S mode
- Change if return else return to if return return
- Change static_branch_likely to static_branch_unlikely
- Fix 32-bit clint_get_cycles64 csr_available check being inverted
- Fix is_c900_clint being uninitialized
V4 -> V5:
- Remove riscv,csr-clint
- Remove riscv_csr_time_available
- Replace all static_branch_unlikely(&riscv_csr_time_available) with riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)
V5 -> V6:
- Add csr_clock_next_event
- Add csr_get_cycles64 to sched_clock_register
- Break get_cycles64 into csr_get_cycles64 and clint_get_cycles64
- Change default behavior to use CSR time register
V6 -> V7:
- Split csr and c900 parts into different commits
Posible future work:
- Check for SSTC support and use stimercmp
- SSTC depends on smode which cpufeature doesnt check for.
--- arch/riscv/include/asm/timex.h | 12 ++++++- drivers/clocksource/timer-clint.c | 57 ++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-)
arch/riscv/include/asm/timex.h +11 -1
@@ -17,17 +17,26 @@ typedef unsigned long cycles_t;#ifdef CONFIG_64BIT static inline cycles_t get_cycles (void ) { +if (riscv_has_extension_likely (RISCV_ISA_EXT_ZICNTR )) +return csr_read (CSR_TIME ); +return readq_relaxed (clint_time_val); }#else /* !CONFIG_64BIT */ static inline u32 get_cycles (void ) { +if (riscv_has_extension_likely (RISCV_ISA_EXT_ZICNTR )) +return csr_read (CSR_TIME ); +return readl_relaxed (((u32 *)clint_time_val)); }#define get_cycles get_cyclesstatic inline u32 get_cycles_hi (void ) { +if (riscv_has_extension_likely (RISCV_ISA_EXT_ZICNTR )) +return csr_read (CSR_TIMEH ); +return readl_relaxed (((u32 *)clint_time_val) +1 ); }#define get_cycles_hi get_cycles_hi
@@ -40,7 +49,8 @@ static inline u32 get_cycles_hi(void)*/ static inline unsigned long random_get_entropy (void ) { -if (unlikely (clint_time_val ==NULL )) +if (!riscv_has_extension_likely (RISCV_ISA_EXT_ZICNTR ) && + (unlikely (clint_time_val ==NULL )))return random_get_entropy_fallback ();return get_cycles (); }
drivers/clocksource/timer-clint.c +53 -4
114 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v8 2/2 clocksource/drivers/timer-clint: Add T-Head C9xx clint
From: Jisheng Zhang <[email protected]>
To use the T-HEAD C9xx clint in RISCV-M NOMMU env, we need to take care two points:
1.The mtimecmp in T-Head C9xx clint only supports 32bit read/write, implement such support.
2. As pointed out by commit ca7810aecdba ("lib: utils/timer: mtimer:
add a quirk for lacking mtime register") of opensbi:
"T-Head developers surely have a different understanding of time CSR and CLINT's mtime register with SiFive ones, that they did not implement the mtime register at all -- as shown in openC906 source code, their time CSR value is just exposed at the top of their processor IP block and expects an external continuous counter, which makes it not overrideable, and thus mtime register is not implemented, even not for reading. However, if CLINTEE is not enabled in T-Head's MXSTATUS extended CSR, these systems still rely on the mtimecmp registers to generate timer interrupts. This makes it necessary to implement T-Head C9xx CLINT support in OpenSBI MTIMER driver, which skips implementing reading mtime register and falls back to default code that reads time CSR."
So, we need to fall back to read time CSR instead of mtime register using RISCV_ISA_EXT_ZICNTR.
---
Treat this as a completely new patch, as it is mostly rewritten. Original:
https://lore.kernel.org/all/[email protected]/
V6 -> V7:
- New commit
- Split csr and c900 parts into different commits
- c900_clint_clock_next_event use csr_get_cycles64 over accedental use of clint_get_cycles64
--- drivers/clocksource/timer-clint.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
drivers/clocksource/timer-clint.c +25 -0
@@ -39,6 +39,7 @@ static u64 __iomem *clint_timer_cmp;static u64 __iomem *clint_timer_val;static unsigned long clint_timer_freq;static unsigned int clint_timer_irq; +static bool is_c900_clint;#ifdef CONFIG_RISCV_M_MODE u64 __iomem *clint_time_val;
@@ -154,6 +155,19 @@ static int clint_clock_next_event(unsigned long delta,return 0 ; } +static int c900_clint_clock_next_event (unsigned long delta, +struct clock_event_device *ce) +{ +void __iomem *r = clint_timer_cmp + +cpuid_to_hartid_map (smp_processor_id ()); +u64 val =csr_get_cycles64 () + delta; + +csr_set (CSR_IE ,IE_TIE ); +writel_relaxed (val, r); +writel_relaxed (val >>32 , r +4 ); +return 0 ; +} + static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = { .name ="clint_clockevent" , .features =CLOCK_EVT_FEAT_ONESHOT ,
@@ -168,6 +182,9 @@ static int clint_timer_starting_cpu(unsigned int cpu)if (!riscv_has_extension_likely (RISCV_ISA_EXT_ZICNTR )) ce->set_next_event = clint_clock_next_event; +if (is_c900_clint) + ce->set_next_event = c900_clint_clock_next_event; + ce->cpumask =cpumask_of (cpu);clockevents_config_and_register (ce, clint_timer_freq,100 ,ULONG_MAX );
@@ -319,8 +336,16 @@ static int __init clint_timer_init(struct device_node *np)static int __init clint_timer_init_dt (struct device_node *np) { + is_c900_clint = false; +return clint_timer_init (np); +} + +static int __init c900_clint_timer_init_dt (struct device_node *np) +{ + is_c900_clint = true;return clint_timer_init (np); }TIMER_OF_DECLARE (clint_timer,"riscv,clint0" , clint_timer_init_dt);TIMER_OF_DECLARE (clint_timer1,"sifive,clint0" , clint_timer_init_dt); +TIMER_OF_DECLARE (clint_timer2,"thead,c900-clint" , c900_clint_timer_init_dt);
Signature · 1 line
2.54.0
Re: [PATCH v8 2/2] clocksource/drivers/timer-clint: Add T-Head C9xx clint
Quoted 10 lines · Jesse Taube
To use the T-HEAD C9xx clint in RISCV-M NOMMU env, we need to take care two points:
1.The mtimecmp in T-Head C9xx clint only supports 32bit read/write, implement such support.
2. As pointed out by commit
ca7810aecdba("lib: utils/timer: mtimer:
add a quirk for lacking mtime register") of opensbi:[...]
Signature · 1 line
- Charlie
Quoted 4 lines
ZICNTR allows the CPU time to be read using CSRs, which used to be the default. If ZICNTR is supported prefer using CSRs over MMIO. The clint MMIO is covered in the RISC-V ACLINT spec [1], which has not been ratified yet, where as the ZICNTR spec is ratified.
It's actually stronger than that, the ACLINT spec has been rejected:
https://riscv.atlassian.net/browse/RVS-3645
https://riscv.atlassian.net/browse/RVS-4913
Quoted 6 lines
Using the CSRs also used to be the default, but was changed in Commit
d5be89a8d118("RISC-V: Resurrect the MMIO timer implementation for M-mode systems") due to K210 not implementing rdtime in M-mode.[1]: https://github.com/riscvarchive/riscv-aclint
Signed-off-by Jesse Taube <[email protected]>
Signature · 1 line
- Charlie