Skip to contentSkip to conversation

riscv: Access time mmio instead of rdtime.

PATCH In review Aleksandar Rikalo 5 participants 5 messages Dec 27, 2024 – Apr 10, 2025

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

PATCH 1/1 riscv: Access time mmio instead of rdtime.

Allows for riscv-clock to pick up mmio address for faster rdtime access via clock-reg (u64) property on boot cpu node.

Add RISCV_TIME_MMIO config.

---
 arch/riscv/Kconfig                | 11 ++++++
 arch/riscv/include/asm/timex.h    | 59 ++++++++++++++++++++-----------
 drivers/clocksource/timer-riscv.c | 22 ++++++++++++
 3 files changed, 72 insertions(+), 20 deletions(-)
arch/riscv/Kconfig +11 -0
index d4a7ca0388c0..0f4243e4bc1b 100644
@@ -716,6 +716,17 @@ config RISCV_ISA_ZACAS
 
 	  If you don't know what to do here, say Y.
 
+config RISCV_TIME_MMIO
+	bool "Time mmio support"
+	depends on !RISCV_M_MODE
+	help
+	   Access time mmio instead of rdtime.
+	   Allows for riscv-clock to pick up mmio address for faster rdtime
+	   access via clock-reg (u64) property on boot cpu node.
+	   Some CPUs trap on rdtime or reading the time CSR register.
+
+	   If you don't know what to do here, say N.
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y
arch/riscv/include/asm/timex.h +39 -20

92 lines not on this page · raw message

drivers/clocksource/timer-riscv.c +22 -0
index 48ce50c5f5e6..1f6be676d6c7 100644
@@ -32,6 +32,13 @@
 static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
 static bool riscv_timer_cannot_wake_cpu;
 
+#if defined(CONFIG_RISCV_TIME_MMIO)
+EXPORT_SYMBOL(riscv_time_mmio_available);
+DEFINE_STATIC_KEY_FALSE_RO(riscv_time_mmio_available);
+u64 __iomem *riscv_time_val __ro_after_init;
+EXPORT_SYMBOL(riscv_time_val);
+#endif
+
 static void riscv_clock_event_stop(void)
 {
 	if (static_branch_likely(&riscv_sstc_available)) {
@@ -203,6 +210,9 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 	int cpuid, error;
 	unsigned long hartid;
 	struct device_node *child;
+#if defined(CONFIG_RISCV_TIME_MMIO)
+	u64 mmio_addr;
+#endif
 
 	error = riscv_of_processor_hartid(n, &hartid);
 	if (error < 0) {
@@ -220,6 +230,18 @@ static int __init riscv_timer_init_dt(struct device_node *n)
 	if (cpuid != smp_processor_id())
 		return 0;
 
+#if defined(CONFIG_RISCV_TIME_MMIO)
+	if (!of_property_read_u64(n, "clock-reg", &mmio_addr)) {
+		riscv_time_val = ioremap((long)mmio_addr, 8);
+		if (riscv_time_val) {
+			pr_info("Using mmio time register at 0x%llx\n", mmio_addr);
+			static_branch_enable(&riscv_time_mmio_available);
+		} else {
+			pr_warn("Unable to use mmio time at 0x%llx\n", mmio_addr);
+		}
+	}
+#endif
+
 	child = of_find_compatible_node(NULL, NULL, "riscv,timer");
 	if (child) {
 		riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
Signature · 1 line
2.25.1
#1 lore raw

drivers/clocksource/timer-riscv.c static int __init riscv_timer_init_dt(struct device_node *n) · line 234

Quoted 2 lines · Aleksandar Rikalo
drivers/clocksource/timer-riscv.c +2 -0
+#if defined(CONFIG_RISCV_TIME_MMIO)
+	if (!of_property_read_u64(n, "clock-reg", &mmio_addr)) {

This is a non-standard, undocumented property. reg & reg-names are what you should be using here.

Cheers,
Conor.

Quoted 9 lines
drivers/clocksource/timer-riscv.c +9 -0
+		riscv_time_val = ioremap((long)mmio_addr, 8);
+		if (riscv_time_val) {
+			pr_info("Using mmio time register at 0x%llx\n", mmio_addr);
+			static_branch_enable(&riscv_time_mmio_available);
+		} else {
+			pr_warn("Unable to use mmio time at 0x%llx\n", mmio_addr);
+		}
+	}
+#endif
maintainer
#2 lore raw
Quoted 6 lines · Conor Dooley

drivers/clocksource/timer-riscv.c static int __init riscv_timer_init_dt(struct device_node *n) · line 234

Quoted 2 lines · Aleksandar Rikalo
drivers/clocksource/timer-riscv.c +2 -0
+#if defined(CONFIG_RISCV_TIME_MMIO)
+	if (!of_property_read_u64(n, "clock-reg", &mmio_addr)) {

This is a non-standard, undocumented property. reg & reg-names are what you should be using here.

We were talking some in the patchwork meeting. If this system has a CLINT then it's probably best to refactor the timers such that MMIO CLINT access is independent from M-mode (as there's some patches floating around to make M-mode use rdtime on systems that implement rdtime).

Quoted 9 lines
Quoted 9 lines
drivers/clocksource/timer-riscv.c +9 -0
+		riscv_time_val = ioremap((long)mmio_addr, 8);
+		if (riscv_time_val) {
+			pr_info("Using mmio time register at 0x%llx\n", mmio_addr);
+			static_branch_enable(&riscv_time_mmio_available);
+		} else {
+			pr_warn("Unable to use mmio time at 0x%llx\n", mmio_addr);
+		}
+	}
+#endif
#3 lore raw

HTEC Public

Thanks a lot for your feedback. We will address those comments in the v2 for the change.

Best,
Djordje

________________________________________
From: Palmer Dabbelt <[email protected]>
Sent: Wednesday, February 5, 2025 4:31 PM
To: Conor Dooley <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; Paul Walmsley <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Djordje Todorovic <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] riscv: Access time mmio instead of rdtime.

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 6 lines · Conor Dooley

drivers/clocksource/timer-riscv.c static int __init riscv_timer_init_dt(struct device_node *n) · line 234

Quoted 2 lines · Aleksandar Rikalo
drivers/clocksource/timer-riscv.c +2 -0
+#if defined(CONFIG_RISCV_TIME_MMIO)
+    if (!of_property_read_u64(n, "clock-reg", &mmio_addr)) {

This is a non-standard, undocumented property. reg & reg-names are what you should be using here.

We were talking some in the patchwork meeting. If this system has a CLINT then it's probably best to refactor the timers such that MMIO CLINT access is independent from M-mode (as there's some patches floating around to make M-mode use rdtime on systems that implement rdtime).

Quoted 9 lines
Quoted 9 lines
drivers/clocksource/timer-riscv.c +9 -0
+            riscv_time_val = ioremap((long)mmio_addr, 8);
+            if (riscv_time_val) {
+                    pr_info("Using mmio time register at 0x%llx\n", mmio_addr);
+                    static_branch_enable(&riscv_time_mmio_available);
+            } else {
+                    pr_warn("Unable to use mmio time at 0x%llx\n", mmio_addr);
+            }
+    }
+#endif