Skip to contentSkip to conversation

clocksource/drivers/sun4i: Wait for pending reload before CTRL write

PATCH In review Ondřej Jirman 3 participants 7 messages Jul 25 – Aug 12

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

PATCH 1/1 clocksource/drivers/sun4i: Wait for pending reload before CTRL write

From: Ondrej Jirman <[email protected]>

The A13 manual says of TMR0_RELOAD: "After the bit is set, it can not be written again before it's cleared automatically." Both time_stop() and time_start() read-modify-write the control register, so a reload still in flight gets rewritten and the write can be dropped, leaving the timer unarmed. In oneshot mode this happens on every tick, and the tick eventually stops for good.

Wait for the reload to clear first. All control register updates go through sun4i_clkevt_time_stop().

Without this my Allwinner A13 based Pocketbook Touch Lux 3 stops scheduling processes during boot or within a few seconds of executing userspace. Only sysrq+t over serial port works at this stage, which is how I discovered the root cause.

Fixes 7e14183469d8 ("clocksource: sun4i: Fix bug when switching from periodic to oneshot modes")

---
BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

 drivers/clocksource/timer-sun4i.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/clocksource/timer-sun4i.c +25 -1
index 7bdcc60ad43c..2e7457f671c4 100644
@@ -39,6 +39,9 @@
 
 #define TIMER_SYNC_TICKS	3
 
+/* The reload bit clears a couple of source clock cycles after it is set. */
+#define TIMER_RELOAD_MAX_POLL	100
+
 /*
  * When we disable a timer, we need to wait at least for 2 cycles of
  * the timer source clock. We will use for that the clocksource timer
@@ -53,9 +56,30 @@ static void sun4i_clkevt_sync(void __iomem *base)
 		cpu_relax();
 }
 
+/*
+ * The control register must not be written while a reload is still in
+ * flight, or the write can be dropped.
+ */
+static int sun4i_clkevt_wait_reload(void __iomem *base, u8 timer)
+{
+	int i;
+
+	for (i = 0; i < TIMER_RELOAD_MAX_POLL; i++) {
+		if (!(readl(base + TIMER_CTL_REG(timer)) & TIMER_CTL_RELOAD))
+			return 0;
+		cpu_relax();
+	}
+
+	return -ETIME;
+}
+
 static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
 {
-	u32 val = readl(base + TIMER_CTL_REG(timer));
+	u32 val;
+
+	sun4i_clkevt_wait_reload(base, timer);
+
+	val = readl(base + TIMER_CTL_REG(timer));
 	writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer));
 	sun4i_clkevt_sync(base);
 }
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same hardware limitation where writing to the control register while the RELOAD bit is set causes the write to be dropped is unaddressed in timer-sun5i.c. --

--- Patch [1]: [PATCH] clocksource/drivers/sun4i: Wait for pending reload before CTRL write --- commit 936f66d7fd8614d6358ab1a1c7241c4675c9fff4
Author: Ondrej Jirman <[email protected]>

clocksource/drivers/sun4i: Wait for pending reload before CTRL write

This commit fixes a bug in the sun4i timer driver where writes to the control register are silently dropped if the RELOAD bit is set. It introduces a polling mechanism to wait for the reload bit to clear before modifying the register.

Quoted 3 lines

BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

[Severity: High]
This isn't a bug introduced by this patch, but as you suspected, the exact same hardware limitation is unaddressed in drivers/clocksource/timer-sun5i.c.

Looking at sun5i_clkevt_time_stop(), the unprotected read-modify-write of the control register occurs here:

drivers/clocksource/timer-sun5i.c:sun5i_clkevt_time_stop() {
    ...
    writel(val & ~TIMER_CTL_ENABLE,
           ce->base + TIMER_CTL_REG(timer, offset));
    ...
}

If rapid back-to-back scheduling of high-resolution timers causes this to be called while the RELOAD bit from a prior time_start() is still set, wouldn't the write be dropped, failing to clear the ENABLE bit?

Since this can permanently stop clock events and cause the system to freeze by halting process scheduling, should a similar wait mechanism be applied to the sun5i timer to protect devices that use it as a clockevent device?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Well, that's what I stated in the original patch comment. I don't have the HW to test. I can send a blind fix, or whoever cares and has SoC that uses the other timer driver that may have the same issue, can do it.

I'll do whatever the real maintainers suggest. ;)

	.o
Quoted 45 lines · On Sat, Jul 25, 2026 at 11:43:38AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same hardware limitation where writing to the control register while the RELOAD bit is set causes the write to be dropped is unaddressed in timer-sun5i.c. --

--- Patch [1]: [PATCH] clocksource/drivers/sun4i: Wait for pending reload before CTRL write --- commit 936f66d7fd8614d6358ab1a1c7241c4675c9fff4
Author: Ondrej Jirman <[email protected]>

clocksource/drivers/sun4i: Wait for pending reload before CTRL write

This commit fixes a bug in the sun4i timer driver where writes to the control register are silently dropped if the RELOAD bit is set. It introduces a polling mechanism to wait for the reload bit to clear before modifying the register.

Quoted 3 lines

BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

[Severity: High]
This isn't a bug introduced by this patch, but as you suspected, the exact same hardware limitation is unaddressed in drivers/clocksource/timer-sun5i.c.

Looking at sun5i_clkevt_time_stop(), the unprotected read-modify-write of the control register occurs here:

drivers/clocksource/timer-sun5i.c:sun5i_clkevt_time_stop() {
    ...
    writel(val & ~TIMER_CTL_ENABLE,
           ce->base + TIMER_CTL_REG(timer, offset));
    ...
}

If rapid back-to-back scheduling of high-resolution timers causes this to be called while the RELOAD bit from a prior time_start() is still set, wouldn't the write be dropped, failing to clear the ENABLE bit?

Since this can permanently stop clock events and cause the system to freeze by halting process scheduling, should a similar wait mechanism be applied to the sun5i timer to protect devices that use it as a clockevent device?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Ondřej Jirman
Quoted 48 lines · Ondřej Jirman

From: Ondrej Jirman <[email protected]>

The A13 manual says of TMR0_RELOAD: "After the bit is set, it can not be written again before it's cleared automatically." Both time_stop() and time_start() read-modify-write the control register, so a reload still in flight gets rewritten and the write can be dropped, leaving the timer unarmed. In oneshot mode this happens on every tick, and the tick eventually stops for good.

Wait for the reload to clear first. All control register updates go through sun4i_clkevt_time_stop().

Without this my Allwinner A13 based Pocketbook Touch Lux 3 stops scheduling processes during boot or within a few seconds of executing userspace. Only sysrq+t over serial port works at this stage, which is how I discovered the root cause.

Fixes 7e14183469d8 ("clocksource: sun4i: Fix bug when switching from periodic to oneshot modes")

---
BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

  drivers/clocksource/timer-sun4i.c | 26 +++++++++++++++++++++++++-
  1 file changed, 25 insertions(+), 1 deletion(-)
drivers/clocksource/timer-sun4i.c +6 -0
index 7bdcc60ad43c..2e7457f671c4 100644
@@ -39,6 +39,9 @@
 
  #define TIMER_SYNC_TICKS	3
 
+/* The reload bit clears a couple of source clock cycles after it is set. */
+#define TIMER_RELOAD_MAX_POLL	100
+
  /*
   * When we disable a timer, we need to wait at least for 2 cycles of
   * the timer source clock. We will use for that the clocksource timer
@@ -53,9 +56,30 @@ static void sun4i_clkevt_sync(void __iomem *base)
  		cpu_relax();
  }
 
+/*
+ * The control register must not be written while a reload is still in
+ * flight, or the write can be dropped.

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 71

Quoted 10 lines
drivers/clocksource/timer-sun4i.c +10 -0
+ */
+static int sun4i_clkevt_wait_reload(void __iomem *base, u8 timer)
+{
+	int i;
+
+	for (i = 0; i < TIMER_RELOAD_MAX_POLL; i++) {
+		if (!(readl(base + TIMER_CTL_REG(timer)) & TIMER_CTL_RELOAD))
+			return 0;
+		cpu_relax();
+	}

The loop looks correct but I'm wondering why not use the helper:

ret = readl_poll_timeout_atomic(base + TIMER_CTL_REG(timer), val,

				!(val & TIMER_CTL_RELOAD), 0, timeout);

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 80

Quoted 9 lines
drivers/clocksource/timer-sun4i.c +6 -1
+	return -ETIME;
+}
+
  static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
  {
-	u32 val = readl(base + TIMER_CTL_REG(timer));
+	u32 val;
+
+	sun4i_clkevt_wait_reload(base, timer);

If it fails ?

Quoted 5 lines
drivers/clocksource/timer-sun4i.c +2 -0
+
+	val = readl(base + TIMER_CTL_REG(timer));
  	writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer));
  	sun4i_clkevt_sync(base);
  }
#4 lore raw

Hello Daniel,

Quoted 59 lines · Daniel Lezcano
Quoted 45 lines · Ondřej Jirman

From: Ondrej Jirman <[email protected]>

The A13 manual says of TMR0_RELOAD: "After the bit is set, it can not be written again before it's cleared automatically." Both time_stop() and time_start() read-modify-write the control register, so a reload still in flight gets rewritten and the write can be dropped, leaving the timer unarmed. In oneshot mode this happens on every tick, and the tick eventually stops for good.

Wait for the reload to clear first. All control register updates go through sun4i_clkevt_time_stop().

Without this my Allwinner A13 based Pocketbook Touch Lux 3 stops scheduling processes during boot or within a few seconds of executing userspace. Only sysrq+t over serial port works at this stage, which is how I discovered the root cause.

Fixes 7e14183469d8 ("clocksource: sun4i: Fix bug when switching from periodic to oneshot modes")

---
BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

  drivers/clocksource/timer-sun4i.c | 26 +++++++++++++++++++++++++-
  1 file changed, 25 insertions(+), 1 deletion(-)
drivers/clocksource/timer-sun4i.c +6 -0
index 7bdcc60ad43c..2e7457f671c4 100644
@@ -39,6 +39,9 @@
  #define TIMER_SYNC_TICKS	3
+/* The reload bit clears a couple of source clock cycles after it is set. */
+#define TIMER_RELOAD_MAX_POLL	100
+
  /*
   * When we disable a timer, we need to wait at least for 2 cycles of
   * the timer source clock. We will use for that the clocksource timer
@@ -53,9 +56,30 @@ static void sun4i_clkevt_sync(void __iomem *base)
  		cpu_relax();
  }
+/*
+ * The control register must not be written while a reload is still in
+ * flight, or the write can be dropped.

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 70

Quoted 10 lines
drivers/clocksource/timer-sun4i.c +10 -0
+ */
+static int sun4i_clkevt_wait_reload(void __iomem *base, u8 timer)
+{
+	int i;
+
+	for (i = 0; i < TIMER_RELOAD_MAX_POLL; i++) {
+		if (!(readl(base + TIMER_CTL_REG(timer)) & TIMER_CTL_RELOAD))
+			return 0;
+		cpu_relax();
+	}

The loop looks correct but I'm wondering why not use the helper:

Quoted 3 lines

ret = readl_poll_timeout_atomic(base + TIMER_CTL_REG(timer), val,

				!(val & TIMER_CTL_RELOAD), 0, timeout);

I can. The only difference would be that this would loop up to 1000 * timeout times, instead of 100 times in case reload bit never returns to 0. But that never happens anyway.

Quoted 11 lines

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 80

Quoted 9 lines
drivers/clocksource/timer-sun4i.c +6 -1
+	return -ETIME;
+}
+
  static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
  {
-	u32 val = readl(base + TIMER_CTL_REG(timer));
+	u32 val;
+
+	sun4i_clkevt_wait_reload(base, timer);

If it fails ?

There's not much to do, since sun4i_clkevt_time_stop() does not return anything anyway and it's used across the file quite extensively. Well, the file is small, but sun4i_clkevt_time_stop is called in every function exported via timer_of struct... :)

I can rewrite all these functions to report all errors to timer core, but in practice reload always resets very quickly, and a condition where the loop even loops happens once every few seconds or so. I doubt anything but return 0 would ever happen anyway. I'd certainly notice, because it would again cause instant lockup of my e-reader. Maybe I can just return void from sun4i_clkevt_wait_reload() and not pretend there are errors to report, instead?

Best regards,

	o.
Quoted 6 lines
Quoted 5 lines
drivers/clocksource/timer-sun4i.c +2 -0
+
+	val = readl(base + TIMER_CTL_REG(timer));
  	writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer));
  	sun4i_clkevt_sync(base);
  }
#5 lore raw
Quoted 62 lines · Ondřej Jirman

Hello Daniel,

Quoted 59 lines · Daniel Lezcano
Quoted 45 lines · Ondřej Jirman

From: Ondrej Jirman <[email protected]>

The A13 manual says of TMR0_RELOAD: "After the bit is set, it can not be written again before it's cleared automatically." Both time_stop() and time_start() read-modify-write the control register, so a reload still in flight gets rewritten and the write can be dropped, leaving the timer unarmed. In oneshot mode this happens on every tick, and the tick eventually stops for good.

Wait for the reload to clear first. All control register updates go through sun4i_clkevt_time_stop().

Without this my Allwinner A13 based Pocketbook Touch Lux 3 stops scheduling processes during boot or within a few seconds of executing userspace. Only sysrq+t over serial port works at this stage, which is how I discovered the root cause.

Fixes 7e14183469d8 ("clocksource: sun4i: Fix bug when switching from periodic to oneshot modes")

---
BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

   drivers/clocksource/timer-sun4i.c | 26 +++++++++++++++++++++++++-
   1 file changed, 25 insertions(+), 1 deletion(-)
drivers/clocksource/timer-sun4i.c +6 -0
index 7bdcc60ad43c..2e7457f671c4 100644
@@ -39,6 +39,9 @@
   #define TIMER_SYNC_TICKS	3
+/* The reload bit clears a couple of source clock cycles after it is set. */
+#define TIMER_RELOAD_MAX_POLL	100
+
   /*
    * When we disable a timer, we need to wait at least for 2 cycles of
    * the timer source clock. We will use for that the clocksource timer
@@ -53,9 +56,30 @@ static void sun4i_clkevt_sync(void __iomem *base)
   		cpu_relax();
   }
+/*
+ * The control register must not be written while a reload is still in
+ * flight, or the write can be dropped.

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 70

Quoted 10 lines
drivers/clocksource/timer-sun4i.c +10 -0
+ */
+static int sun4i_clkevt_wait_reload(void __iomem *base, u8 timer)
+{
+	int i;
+
+	for (i = 0; i < TIMER_RELOAD_MAX_POLL; i++) {
+		if (!(readl(base + TIMER_CTL_REG(timer)) & TIMER_CTL_RELOAD))
+			return 0;
+		cpu_relax();
+	}

The loop looks correct but I'm wondering why not use the helper:

Quoted 7 lines
Quoted 3 lines

ret = readl_poll_timeout_atomic(base + TIMER_CTL_REG(timer), val,

				!(val & TIMER_CTL_RELOAD), 0, timeout);

I can. The only difference would be that this would loop up to 1000 * timeout times, instead of 100 times in case reload bit never returns to 0. But that never happens anyway.

Sorry I don't get the 1000 * timeout

Does the documentation says anything about the duration before the register is cleared ?

Quoted 17 lines
Quoted 11 lines

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 80

Quoted 9 lines
drivers/clocksource/timer-sun4i.c +6 -1
+	return -ETIME;
+}
+
   static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
   {
-	u32 val = readl(base + TIMER_CTL_REG(timer));
+	u32 val;
+
+	sun4i_clkevt_wait_reload(base, timer);

If it fails ?

There's not much to do, since sun4i_clkevt_time_stop() does not return anything anyway and it's used across the file quite extensively. Well, the file is small, but sun4i_clkevt_time_stop is called in every function exported via timer_of struct... :)

Quoted 7 lines

I can rewrite all these functions to report all errors to timer core, but in practice reload always resets very quickly, and a condition where the loop even loops happens once every few seconds or so. I doubt anything but return 0 would ever happen anyway. I'd certainly notice, because it would again cause instant lockup of my e-reader. Maybe I can just return void from sun4i_clkevt_wait_reload() and not pretend there are errors to report, instead?

IMO, returning void is fine along with a message, may be a WARN as it is really, AFAIU, not supposed to happen. It will be easy for anyone to find out where the issue is coming from

#6 lore raw
Quoted 67 lines · Daniel Lezcano
Quoted 66 lines · Ondřej Jirman

Hello Daniel,

Quoted 62 lines · Daniel Lezcano
Quoted 45 lines · Ondřej Jirman

From: Ondrej Jirman <[email protected]>

The A13 manual says of TMR0_RELOAD: "After the bit is set, it can not be written again before it's cleared automatically." Both time_stop() and time_start() read-modify-write the control register, so a reload still in flight gets rewritten and the write can be dropped, leaving the timer unarmed. In oneshot mode this happens on every tick, and the tick eventually stops for good.

Wait for the reload to clear first. All control register updates go through sun4i_clkevt_time_stop().

Without this my Allwinner A13 based Pocketbook Touch Lux 3 stops scheduling processes during boot or within a few seconds of executing userspace. Only sysrq+t over serial port works at this stage, which is how I discovered the root cause.

Fixes 7e14183469d8 ("clocksource: sun4i: Fix bug when switching from periodic to oneshot modes")

---
BTW, similar issue will likely be also in timer-sun5i.c but I don't have any device that would exercise it. And it's possible that this triggers more easily with CONFIG_HZ=1000 which I use.

   drivers/clocksource/timer-sun4i.c | 26 +++++++++++++++++++++++++-
   1 file changed, 25 insertions(+), 1 deletion(-)
drivers/clocksource/timer-sun4i.c +6 -0
index 7bdcc60ad43c..2e7457f671c4 100644
@@ -39,6 +39,9 @@
   #define TIMER_SYNC_TICKS	3
+/* The reload bit clears a couple of source clock cycles after it is set. */
+#define TIMER_RELOAD_MAX_POLL	100
+
   /*
    * When we disable a timer, we need to wait at least for 2 cycles of
    * the timer source clock. We will use for that the clocksource timer
@@ -53,9 +56,30 @@ static void sun4i_clkevt_sync(void __iomem *base)
   		cpu_relax();
   }
+/*
+ * The control register must not be written while a reload is still in
+ * flight, or the write can be dropped.

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 70

Quoted 10 lines
drivers/clocksource/timer-sun4i.c +10 -0
+ */
+static int sun4i_clkevt_wait_reload(void __iomem *base, u8 timer)
+{
+	int i;
+
+	for (i = 0; i < TIMER_RELOAD_MAX_POLL; i++) {
+		if (!(readl(base + TIMER_CTL_REG(timer)) & TIMER_CTL_RELOAD))
+			return 0;
+		cpu_relax();
+	}

The loop looks correct but I'm wondering why not use the helper:

ret = readl_poll_timeout_atomic(base + TIMER_CTL_REG(timer), val,

				!(val & TIMER_CTL_RELOAD), 0, timeout);
Quoted 5 lines
Quoted 3 lines

I can. The only difference would be that this would loop up to 1000 * timeout times, instead of 100 times in case reload bit never returns to 0. But that never happens anyway.

Sorry I don't get the 1000 * timeout

If you look at implementation of readl_poll_timeout_atomic() the timeout parameter means that readl_poll_timeout_atomic() will do at most timeout * 1000 thight loops, so minimum timeout is 1000 loops.

Quoted 2 lines

Does the documentation says anything about the duration before the register is cleared ?

Not directly, but for start after stop, manual says to wait at least 2 Tcycles (input clock cycles to the timer).

Regards,

	o.
Quoted 28 lines
Quoted 17 lines
Quoted 11 lines

drivers/clocksource/timer-sun4i.c static void sun4i_clkevt_sync(void __iomem *base) · line 80

Quoted 9 lines
drivers/clocksource/timer-sun4i.c +6 -1
+	return -ETIME;
+}
+
   static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
   {
-	u32 val = readl(base + TIMER_CTL_REG(timer));
+	u32 val;
+
+	sun4i_clkevt_wait_reload(base, timer);

If it fails ?

There's not much to do, since sun4i_clkevt_time_stop() does not return anything anyway and it's used across the file quite extensively. Well, the file is small, but sun4i_clkevt_time_stop is called in every function exported via timer_of struct... :)

Quoted 7 lines

I can rewrite all these functions to report all errors to timer core, but in practice reload always resets very quickly, and a condition where the loop even loops happens once every few seconds or so. I doubt anything but return 0 would ever happen anyway. I'd certainly notice, because it would again cause instant lockup of my e-reader. Maybe I can just return void from sun4i_clkevt_wait_reload() and not pretend there are errors to report, instead?

IMO, returning void is fine along with a message, may be a WARN as it is really, AFAIU, not supposed to happen. It will be easy for anyone to find out where the issue is coming from