Skip to contentSkip to conversation

clocksource/drivers/timer-npcm7xx: Fix timer1 warning, add sched_clock

PATCH New William A. Kennington III 1 participant 3 messages Nov 28

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

cover clocksource/drivers/timer-npcm7xx: Fix timer1 warning, add sched_clock

Two fixes for drivers/clocksource/timer-npcm7xx.c found while bringing up Linux 7.3 on an NPCM730 BMC:

1. of_clk_get(np, 1) returns ERR_PTR(-ENOENT) when only one clock is present in DT, which is the case for every in-tree device tree, so boot always logs a spurious warning for the optional timer1 clock. Ignore -ENOENT, add the missing newline, and check clk_prepare_enable() errors.
2. Register the 24-bit free-running timer1 counter with sched_clock_register() instead of falling back to jiffies, which also enables CONFIG_IRQ_TIME_ACCOUNTING at runtime.

Tested on an NPCM730 BMC: the timer1 -ENOENT boot warning is gone, sched_clock registers at 80 ns resolution with microsecond dmesg timestamps, and /proc/stat reports live IRQ time under TCP load. WPCM450, which shares the driver, was not tested.

William A. Kennington III (2):
clocksource/drivers/timer-npcm7xx: Fix spurious timer1 clock warning clocksource/drivers/timer-npcm7xx: Register timer1 as sched_clock

 drivers/clocksource/timer-npcm7xx.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
#1 lore raw

PATCH 1/2 clocksource/drivers/timer-npcm7xx: Fix spurious timer1 clock warning

of_clk_get(np, 1) returns ERR_PTR(-ENOENT), not NULL, when only one clock is listed in the device tree. No in-tree device tree lists a second timer clock, so the existing if (clk) check always logs a warning on boot:

/ahb/apb/timer@8000: Failed to get clock for timer1: -ENOENT

Ignore -ENOENT since the second clock is optional, add the missing trailing newline, and check clk_prepare_enable() for errors.

Fixes db78539fc95c ("clocksource/drivers/timer-npcm7xx: Enable timer 1 clock before use")
---
 drivers/clocksource/timer-npcm7xx.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/clocksource/timer-npcm7xx.c +9 -5
index 9af30af5f989..c7377f30e95a 100644
@@ -202,11 +202,15 @@ static int __init npcm7xx_timer_init(struct device_node *np)
 
 	/* Enable the clock for timer1, if it exists */
 	clk = of_clk_get(np, 1);
-	if (clk) {
-		if (!IS_ERR(clk))
-			clk_prepare_enable(clk);
-		else
-			pr_warn("%pOF: Failed to get clock for timer1: %pe", np, clk);
+	if (!IS_ERR(clk)) {
+		ret = clk_prepare_enable(clk);
+		if (ret) {
+			pr_warn("%pOF: Failed to enable clock for timer1: %d\n",
+				np, ret);
+			clk_put(clk);
+		}
+	} else if (PTR_ERR(clk) != -ENOENT) {
+		pr_warn("%pOF: Failed to get clock for timer1: %pe\n", np, clk);
 	}
 
 	npcm7xx_clocksource_init();
#2 lore raw

PATCH 2/2 clocksource/drivers/timer-npcm7xx: Register timer1 as sched_clock

In reply to William A. Kennington III

timer-npcm7xx starts timer1 as a 24-bit free-running down-counter and registers it as a clocksource, but never calls sched_clock_register(). sched_clock() then falls back to jiffies, which leaves CONFIG_IRQ_TIME_ACCOUNTING disabled at runtime and gives coarse scheduler and printk timestamps.

Register timer1 with sched_clock_register() using an inverted readl_relaxed() callback matching clocksource_mmio_readl_down(). The counter is the one the clocksource already reads, so its 24-bit wrap (about 1.3 s at 12.5 MHz) is not a new constraint.

Tested on an NPCM730 BMC, where sched_clock now has 80 ns resolution. The driver also binds nuvoton,wpcm450-timer, which was not tested.

---
 drivers/clocksource/timer-npcm7xx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clocksource/timer-npcm7xx.c +9 -0
index c7377f30e95a..f4b828d7c183 100644
@@ -14,6 +14,7 @@
 #include <linux/clk.h>
 #include <linux/io.h>
 #include <linux/clockchips.h>
+#include <linux/sched_clock.h>
 #include <linux/of_irq.h>
 #include <linux/of_address.h>
 #include "timer-of.h"
@@ -166,6 +167,11 @@ static void __init npcm7xx_clockevents_init(void)
 					0x1, NPCM7XX_Tx_MAX_CNT);
 }
 
+static u64 notrace npcm7xx_read_sched_clock(void)
+{
+	return ~(u64)readl_relaxed(npcm7xx_to.of_base.base + NPCM7XX_REG_TDR1);
+}
+
 static void __init npcm7xx_clocksource_init(void)
 {
 	u32 val;
@@ -184,6 +190,9 @@ static void __init npcm7xx_clocksource_init(void)
 				"npcm7xx-timer1", timer_of_rate(&npcm7xx_to),
 				200, (unsigned int)NPCM7XX_Tx_TDR_MASK_BITS,
 				clocksource_mmio_readl_down);
+	sched_clock_register(npcm7xx_read_sched_clock,
+			     NPCM7XX_Tx_TDR_MASK_BITS,
+			     timer_of_rate(&npcm7xx_to));
 }
 
 static int __init npcm7xx_timer_init(struct device_node *np)