Skip to contentSkip to conversation

clocksource/drivers/timer-goldfish: Fix bugs and add DT support

PATCH New Kuan-Wei Chiu 1 participant 9 messages Nov 5

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

cover clocksource/drivers/timer-goldfish: Fix bugs and add DT support

The goldfish timer driver was previously only used and tested on single core m68k virt, but the same device is also present on other qemu virt machines such as riscv and or1k, where running on SMP exposes several issues.

Fix the concurrency, oneshot, and registration bugs in the driver, add device tree probing support so it can be used across these platforms, and add a MAINTAINERS entry.

Tested on qemu m68k virt and on qemu riscv64 virt with 4 SMP cores. On riscv64, testing was done by passing clocksource=goldfish_timer on the kernel command line and locally bumping the clock_event_device rating above the architected timer to verify both clocksource and oneshot clockevent operation.

Kuan-Wei Chiu (8):
clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock clocksource/drivers/timer-goldfish: Fix oneshot mode spurious
interrupt
clocksource/drivers/timer-goldfish: Fix resource leaks in init clocksource/drivers/timer-goldfish: Set CLOCK_SOURCE_IS_CONTINUOUS
flag
clocksource/drivers/timer-goldfish: Drop redundant max_idle_ns setting clocksource/drivers/timer-goldfish: Set clock_event_device cpumask clocksource/drivers/timer-goldfish: Add device tree probing support
MAINTAINERS: Add entry for goldfish timer driver

 MAINTAINERS                          |   6 ++
 drivers/clocksource/Kconfig          |   5 +-
 drivers/clocksource/timer-goldfish.c | 113 +++++++++++++++++++++------
 3 files changed, 99 insertions(+), 25 deletions(-)
Signature · 1 line
2.56.0.398.g882cbad275-goog
#1 lore raw

PATCH 1/8 clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock

Reading TIMER_TIME_LOW latches the upper 32 bits of the current time into TIMER_TIME_HIGH. Because goldfish_timer_read() reads these two registers without synchronization, a concurrent reader from an interrupt or another CPU can read TIMER_TIME_LOW in between and overwrite the latched high bits.

Protect the register read sequence with a raw spinlock.

Fixes c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
---
 drivers/clocksource/timer-goldfish.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/clocksource/timer-goldfish.c +6 -0
index 3fe12c7ee7b7..cc02b012ff76 100644
@@ -6,6 +6,7 @@
 #include <linux/clockchips.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
 #include <linux/goldfish.h>
 #include <clocksource/timer-goldfish.h>
 
@@ -14,6 +15,7 @@ struct goldfish_timer {
 	struct clock_event_device ced;
 	struct resource res;
 	void __iomem *base;
+	raw_spinlock_t lock;
 };
 
 static struct goldfish_timer *ced_to_gf(struct clock_event_device *ced)
@@ -30,6 +32,7 @@ static u64 goldfish_timer_read(struct clocksource *cs)
 {
 	struct goldfish_timer *timerdrv = cs_to_gf(cs);
 	void __iomem *base = timerdrv->base;
+	unsigned long flags;
 	u32 time_low, time_high;
 	u64 ticks;
 
@@ -37,8 +40,10 @@ static u64 goldfish_timer_read(struct clocksource *cs)
 	 * time_low: get low bits of current time and update time_high
 	 * time_high: get high bits of time at last time_low read
 	 */
+	raw_spin_lock_irqsave(&timerdrv->lock, flags);
 	time_low = gf_ioread32(base + TIMER_TIME_LOW);
 	time_high = gf_ioread32(base + TIMER_TIME_HIGH);
+	raw_spin_unlock_irqrestore(&timerdrv->lock, flags);
 
 	ticks = ((u64)time_high << 32) | time_low;
 
@@ -107,6 +112,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 		return -ENOMEM;
 
 	timerdrv->base = base;
+	raw_spin_lock_init(&timerdrv->lock);
 
 	timerdrv->ced = (struct clock_event_device){
 		.name			= "goldfish_timer",
Signature · 1 line
2.56.0.398.g882cbad275-goog
#2 lore raw

PATCH 2/8 clocksource/drivers/timer-goldfish: Fix oneshot mode spurious interrupt

In reply to Kuan-Wei Chiu

goldfish_timer_set_oneshot() writes 0 to TIMER_ALARM_HIGH and TIMER_ALARM_LOW. Writing TIMER_ALARM_LOW arms the alarm for timestamp 0, which immediately triggers a spurious interrupt when TIMER_IRQ_ENABLED is set. Meanwhile, goldfish_timer_shutdown() disables interrupts without disarming the alarm.

Use TIMER_CLEAR_ALARM to disarm the alarm in both functions.

Fixes c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
---
 drivers/clocksource/timer-goldfish.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clocksource/timer-goldfish.c +2 -2
index cc02b012ff76..b1dc8b388470 100644
@@ -55,8 +55,7 @@ static int goldfish_timer_set_oneshot(struct clock_event_device *evt)
 	struct goldfish_timer *timerdrv = ced_to_gf(evt);
 	void __iomem *base = timerdrv->base;
 
-	gf_iowrite32(0, base + TIMER_ALARM_HIGH);
-	gf_iowrite32(0, base + TIMER_ALARM_LOW);
+	gf_iowrite32(1, base + TIMER_CLEAR_ALARM);
 	gf_iowrite32(1, base + TIMER_IRQ_ENABLED);
 
 	return 0;
@@ -68,6 +67,7 @@ static int goldfish_timer_shutdown(struct clock_event_device *evt)
 	void __iomem *base = timerdrv->base;
 
 	gf_iowrite32(0, base + TIMER_IRQ_ENABLED);
+	gf_iowrite32(1, base + TIMER_CLEAR_ALARM);
 
 	return 0;
 }
Signature · 1 line
2.56.0.398.g882cbad275-goog
#3 lore raw

PATCH 3/8 clocksource/drivers/timer-goldfish: Fix resource leaks in init

In reply to Kuan-Wei Chiu

goldfish_timer_init() ignores the return value of clocksource_register_hz() and fails to release allocated resources when initialization fails.

Check the return value of clocksource_register_hz() and clean up allocated resources on error.

Fixes c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
---
 drivers/clocksource/timer-goldfish.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/clocksource/timer-goldfish.c +15 -3
index b1dc8b388470..8c312c3a64a6 100644
@@ -131,7 +131,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 	ret = request_resource(&iomem_resource, &timerdrv->res);
 	if (ret) {
 		pr_err("Cannot allocate '%s' resource\n", timerdrv->res.name);
-		return ret;
+		goto err_free;
 	}
 
 	timerdrv->cs = (struct clocksource){
@@ -143,17 +143,29 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 		.max_idle_ns	= LONG_MAX,
 	};
 
-	clocksource_register_hz(&timerdrv->cs, NSEC_PER_SEC);
+	ret = clocksource_register_hz(&timerdrv->cs, NSEC_PER_SEC);
+	if (ret) {
+		pr_err("Couldn't register goldfish-timer clocksource\n");
+		goto err_release_res;
+	}
 
 	ret = request_irq(irq, goldfish_timer_irq, IRQF_TIMER,
 			  "goldfish_timer", timerdrv);
 	if (ret) {
 		pr_err("Couldn't register goldfish-timer interrupt\n");
-		return ret;
+		goto err_unregister_cs;
 	}
 
 	clockevents_config_and_register(&timerdrv->ced, NSEC_PER_SEC,
 					1, 0xffffffff);
 
 	return 0;
+
+err_unregister_cs:
+	clocksource_unregister(&timerdrv->cs);
+err_release_res:
+	release_resource(&timerdrv->res);
+err_free:
+	kfree(timerdrv);
+	return ret;
 }
Signature · 1 line
2.56.0.398.g882cbad275-goog
#4 lore raw

PATCH 4/8 clocksource/drivers/timer-goldfish: Set CLOCK_SOURCE_IS_CONTINUOUS flag

In reply to Kuan-Wei Chiu

The goldfish timer is a free-running counter, but timerdrv->cs.flags is set to 0. Without CLOCK_SOURCE_IS_CONTINUOUS, the clocksource core does not mark it as valid for high-resolution timers, preventing the kernel from switching to it when oneshot mode is active:

  [    0.751173] clocksource: Override clocksource goldfish_timer is not currently HRT compatible - deferring

Set .flags to CLOCK_SOURCE_IS_CONTINUOUS.

Fixes c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
---
 drivers/clocksource/timer-goldfish.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clocksource/timer-goldfish.c +1 -1
index 8c312c3a64a6..88253bd1f569 100644
@@ -139,7 +139,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 		.rating		= 400,
 		.read		= goldfish_timer_read,
 		.mask		= CLOCKSOURCE_MASK(64),
-		.flags		= 0,
+		.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
 		.max_idle_ns	= LONG_MAX,
 	};
 
Signature · 1 line
2.56.0.398.g882cbad275-goog
#5 lore raw

PATCH 5/8 clocksource/drivers/timer-goldfish: Drop redundant max_idle_ns setting

In reply to Kuan-Wei Chiu

clocksource_register_hz() automatically calculates cs->max_idle_ns, so the explicit max_idle_ns assignment in goldfish_timer_init() is redundant and overwritten during registration.

Remove the redundant assignment.

---
 drivers/clocksource/timer-goldfish.c | 1 -
 1 file changed, 1 deletion(-)
drivers/clocksource/timer-goldfish.c +0 -1
index 88253bd1f569..bac84e0f9b7b 100644
@@ -140,7 +140,6 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 		.read		= goldfish_timer_read,
 		.mask		= CLOCKSOURCE_MASK(64),
 		.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
-		.max_idle_ns	= LONG_MAX,
 	};
 
 	ret = clocksource_register_hz(&timerdrv->cs, NSEC_PER_SEC);
Signature · 1 line
2.56.0.398.g882cbad275-goog
#6 lore raw

PATCH 6/8 clocksource/drivers/timer-goldfish: Set clock_event_device cpumask

In reply to Kuan-Wei Chiu

Leaving timerdrv->ced.cpumask uninitialized triggers a warning in clockevents_register_device() when num_possible_cpus() > 1.

Initialize .cpumask to cpu_possible_mask.

Fixes c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
---
 drivers/clocksource/timer-goldfish.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clocksource/timer-goldfish.c +1 -0
index bac84e0f9b7b..208b80c46193 100644
@@ -117,6 +117,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
 	timerdrv->ced = (struct clock_event_device){
 		.name			= "goldfish_timer",
 		.features		= CLOCK_EVT_FEAT_ONESHOT,
+		.cpumask		= cpu_possible_mask,
 		.set_state_shutdown	= goldfish_timer_shutdown,
 		.set_state_oneshot      = goldfish_timer_set_oneshot,
 		.set_next_event		= goldfish_timer_next_event,
Signature · 1 line
2.56.0.398.g882cbad275-goog
#7 lore raw

PATCH 7/8 clocksource/drivers/timer-goldfish: Add device tree probing support

In reply to Kuan-Wei Chiu

The driver currently only supports board-level initialization via goldfish_timer_init() on m68k virt.

Register a platform driver matching "google,goldfish-rtc" so device tree platforms can also probe the timer. Since DT platforms typically expose a single goldfish-rtc device that cannot be bound by both drivers at once, depend on M68K || !RTC_DRV_GOLDFISH in Kconfig to avoid conflicting with rtc-goldfish.

---
 drivers/clocksource/Kconfig          |  5 +-
 drivers/clocksource/timer-goldfish.c | 93 +++++++++++++++++++++-------
 2 files changed, 74 insertions(+), 24 deletions(-)
drivers/clocksource/Kconfig +3 -2
index da83483296d5..3a94feb377a5 100644
@@ -760,8 +760,9 @@ config MICROCHIP_PIT64B
 
 config GOLDFISH_TIMER
 	bool "Clocksource using goldfish-rtc"
-	depends on M68K || COMPILE_TEST
-	depends on RTC_DRV_GOLDFISH
+	depends on M68K || OF || COMPILE_TEST
+	depends on M68K || !RTC_DRV_GOLDFISH
+	depends on HAS_IOMEM
 	help
 	  Support for the timer/counter of goldfish-rtc
 
drivers/clocksource/timer-goldfish.c +71 -22

136 lines not on this page · raw message

Signature · 1 line
2.56.0.398.g882cbad275-goog
#8 lore raw

PATCH 8/8 MAINTAINERS: Add entry for goldfish timer driver

In reply to Kuan-Wei Chiu

There is currently no MAINTAINERS entry for the goldfish timer driver. Add one and list myself as maintainer, as I already maintain the same driver in U-Boot [1].

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 93abbb315065..13688df0e060 100644
@@ -2078,6 +2078,12 @@ M:	Jiaxun Yang <[email protected]>
 S:	Supported
 F:	drivers/rtc/rtc-goldfish.c
 
+ANDROID GOLDFISH TIMER DRIVER
+M:	Kuan-Wei Chiu <[email protected]>
+S:	Maintained
+F:	drivers/clocksource/timer-goldfish.c
+F:	include/clocksource/timer-goldfish.h
+
 ANLOGIC DR1V90 CRU DRIVER
 M:	Junhui Liu <[email protected]>
 S:	Maintained
Signature · 1 line
2.56.0.398.g882cbad275-goog