Skip to contentSkip to conversation

clocksource/drivers/timer-clint: Add T-Head C9xx clint

PATCH In review Jesse Taube 2 participants 4 messages Jun 23, 2025 – Jul 15, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 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 continous 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. Add riscv_csr_time_available static key for this purpose.

---
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

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 | 82 +++++++++++++++++++++++++++++--
 2 files changed, 89 insertions(+), 5 deletions(-)
arch/riscv/include/asm/timex.h +11 -1
index a06697846e69..d78075351bf4 100644
@@ -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_cycles
 
 static 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 +78 -4

149 lines not on this page · raw message

Signature · 1 line
2.49.0
#1 lore raw
Quoted 51 lines · Jesse Taube

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 continous 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. Add riscv_csr_time_available static key for this purpose.

---
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

Quoted 4 lines

Posible future work:
- Check for SSTC support and use stimercmp
- SSTC depends on smode which cpufeature doesnt check for.
---

I would like to have Anup Patel opinion on these changes

-- Daniel

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#2 lore raw
In reply to Jesse Taube
Quoted 31 lines · Jesse Taube

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 continous 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. Add riscv_csr_time_available static key for this purpose.

---
Treat this as a completely new patch, as it is mostly rewritten.

[ ... ]

Quoted 5 lines

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

[ ... ]

Any comments for these changes from the arch maintainer ?

Thanks

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#3 lore raw
Quoted 29 lines · Daniel Lezcano
Quoted 27 lines · Jesse Taube

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 continous 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. Add riscv_csr_time_available static key for this purpose.

Quoted 6 lines
Quoted 4 lines

---
Treat this as a completely new patch, as it is mostly rewritten.

[ ... ]

Yes, Jisheng Zhang was the original author, but the patch serves the same goal. I used their code as a starting point, so I kept the sign-off and commit description.

Quoted 10 lines
Quoted 6 lines

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

[ ... ]

Any comments for these changes from the arch maintainer ?

I don't understand? Are you asking Jisheng Zhang?

Quoted 11 lines

Thanks

--

<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

Quoted 6 lines

drivers/clocksource/timer-clint.c static int clint_clock_next_event(unsigned long delta, · line 163

Quoted 6 lines
drivers/clocksource/timer-clint.c +6 -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 = clint_get_cycles64() + delta;

This line should be:
u64 val = csr_get_cycles64() + delta;
I messed up when splitting csr_get_cycles64 and clint_get_cycles64

Thanks,
Jesse Taube

Quoted 7 lines
Quoted 7 lines
drivers/clocksource/timer-clint.c +7 -0
+
+ csr_set(CSR_IE, IE_TIE);
+ writel_relaxed(val, r);
+ writel_relaxed(val >> 32, r + 4);
+ return 0;
+}
+