From: "clocksource-bot for Enric Balletbo i Serra" <tip-bot2@linutronix.de>
To: linux-kernel@vger.kernel.org
Cc: Enric Balletbo i Serra <eballetb@redhat.com>,
Daniel Lezcano <daniel.lezcano@kernel.org>,
Khristine Andreea Barbulescu
<khristineandreea.barbulescu@oss.nxp.com>
Subject: [clocksource: timers/drivers/next] clocksource/drivers/nxp-stm: Fix overshoot detection in set_next_event
Date: Tue, 01 Dec 2026 10:28:09 -0000 [thread overview]
Message-ID: <179612088997.1190409.1331824630064367447.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20261105-nxp-stm-fixes-v3-2-8129b912e74b@redhat.com>
The following commit has been merged into the timers/drivers/next branch of clocksource:
Commit-ID: 73cf416dfa871feb9621564eae62315482e836ba
Gitweb: https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//73cf416dfa871feb9621564eae62315482e836ba
Author: Enric Balletbo i Serra <eballetb@redhat.com>
AuthorDate: Thu, 05 Nov 2026 09:18:33 +01:00
Committer: Daniel Lezcano <daniel.lezcano@kernel.org>
CommitterDate: Tue, 01 Dec 2026 11:11:07 +01:00
clocksource/drivers/nxp-stm: Fix overshoot detection in set_next_event
The existing overshoot check effectively compares two counter readings
and only triggers on a counter wraparound between reads; the normal
case where the counter advances past the programmed value during the
read/write sequence is not detected.
Replace it with the approach of hpet_clkevt_set_next_event(): read the
counter after the comparator write and return -ETIME when less than
STM_MIN_CYCLES remain until the match. Derive the minimum programming
delta from that margin. See the cover letter for the measurement
procedure and constant derivation.
Fixes: cec32ac75827 ("clocksource/drivers/nxp-timer: Add the System Timer Module for the s32gx platforms")
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Enric Balletbo i Serra <eballetb@redhat.com>
Signed-off-by: Daniel Lezcano <daniel.lezcano@kernel.org>
Tested-by: Khristine Andreea Barbulescu <khristineandreea.barbulescu@oss.nxp.com>
Link: https://patch.msgid.link/20261105-nxp-stm-fixes-v3-2-8129b912e74b@redhat.com
---
drivers/clocksource/timer-nxp-stm.c | 53 ++++++++++++++++++----------
1 file changed, 35 insertions(+), 18 deletions(-)
diff --git a/drivers/clocksource/timer-nxp-stm.c b/drivers/clocksource/timer-nxp-stm.c
index 87ae0ae..36b0a11 100644
--- a/drivers/clocksource/timer-nxp-stm.c
+++ b/drivers/clocksource/timer-nxp-stm.c
@@ -20,7 +20,6 @@
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/sched_clock.h>
-#include <linux/units.h>
#define STM_CR(__base) (__base)
@@ -52,6 +51,17 @@
#define STM_ENABLE_MASK (STM_CR_FRZ | STM_CR_TEN)
+/*
+ * The comparator fires only on an exact match with the counter.
+ * STM_MIN_CYCLES is the post-write overshoot margin used by
+ * nxp_stm_clockevent_set_next_event(), following the same approach
+ * as hpet_clkevt_set_next_event(). STM_MIN_PROG_DELTA is the
+ * minimum programming delta derived from it. Both values are
+ * empirically sized from measurements on S32G.
+ */
+#define STM_MIN_CYCLES 512
+#define STM_MIN_PROG_DELTA (STM_MIN_CYCLES * 3)
+
struct stm_timer {
void __iomem *base;
unsigned long rate;
@@ -244,6 +254,7 @@ static int nxp_stm_clockevent_set_next_event(unsigned long delta, struct clock_e
{
struct stm_timer *stm_timer = ced_to_stm(ced);
u32 val;
+ s32 res;
nxp_stm_clockevent_disable(stm_timer);
@@ -254,16 +265,26 @@ static int nxp_stm_clockevent_set_next_event(unsigned long delta, struct clock_e
writel(val, STM_CMP0(stm_timer->base));
/*
- * The counter is shared across the channels and can not be
- * stopped while we are setting the next event. If the delta
- * is very small it is possible the counter increases above
- * the computed 'val'. The min_delta value specified when
- * registering the clockevent will prevent that. The second
- * case is if the counter wraps while we compute the 'val' and
- * before writing the comparator register. We read the counter,
- * check if we are back in time and abort the timer with -ETIME.
+ * The comparator generates an interrupt only on an exact match
+ * with the counter. If the counter reaches the programmed value
+ * before the write to the comparator takes effect, the match is
+ * missed and the next interrupt only fires after a full 32-bit
+ * counter wraparound (~32.2s at 133 MHz).
+ *
+ * The counter read below targets the same module as the
+ * comparator write, so it can only complete once the write has
+ * reached the device, and the signed difference between the
+ * programmed value and the counter reflects the hardware state at
+ * that point. If less than STM_MIN_CYCLES remain to the match, the
+ * write might not have made it into the comparator in time;
+ * return -ETIME so the clockevents core retries with a larger
+ * delta.
+ *
+ * This is the same approach as hpet_clkevt_set_next_event().
*/
- if (val > nxp_stm_clockevent_read_counter(stm_timer) + delta)
+ res = (s32)(val - nxp_stm_clockevent_read_counter(stm_timer));
+
+ if (res < STM_MIN_CYCLES)
return -ETIME;
nxp_stm_clockevent_enable(stm_timer);
@@ -333,16 +354,12 @@ static int nxp_stm_clockevent_starting_cpu(unsigned int cpu)
return ret;
/*
- * The timings measurement show reading the counter register
- * and writing to the comparator register takes as a maximum
- * value 1100 ns at 133MHz rate frequency. The timer must be
- * set above this value and to be secure we set the minimum
- * value equal to 2000ns, so 2us.
- *
- * minimum ticks = (rate / MICRO) * 2
+ * The minimum programming delta absorbs the measured worst case
+ * read/write/read round trip and the overshoot margin, see the
+ * comment on STM_MIN_CYCLES.
*/
clockevents_config_and_register(&stm_timer->ced, stm_timer->rate,
- (stm_timer->rate / MICRO) * 2, 0x7FFFFFFFU);
+ STM_MIN_PROG_DELTA, 0x7FFFFFFFU);
return 0;
}
next prev parent reply other threads:[~2026-12-01 10:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 8:18 [PATCH v3 0/3] Fix NXP STM clockevent bugs causing lockups Enric Balletbo i Serra
2026-11-05 8:18 ` [PATCH v3 1/3] clocksource/drivers/nxp-stm: Fix max_delta overflow on 64-bit platforms Enric Balletbo i Serra
2026-12-01 10:28 ` [clocksource: timers/drivers/next] " clocksource-bot for Enric Balletbo i Serra
2026-11-05 8:18 ` [PATCH v3 2/3] clocksource/drivers/nxp-stm: Fix overshoot detection in set_next_event Enric Balletbo i Serra
2026-12-01 10:28 ` clocksource-bot for Enric Balletbo i Serra [this message]
2026-11-05 8:18 ` [PATCH v3 3/3] clocksource/drivers/nxp-stm: Arm the compare channel from the state callbacks Enric Balletbo i Serra
2026-12-01 10:28 ` [clocksource: timers/drivers/next] " clocksource-bot for Enric Balletbo i Serra
2026-11-06 8:42 ` [PATCH v3 0/3] Fix NXP STM clockevent bugs causing lockups Khristine Andreea Barbulescu
2026-12-01 9:15 ` Enric Balletbo i Serra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179612088997.1190409.1331824630064367447.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=daniel.lezcano@kernel.org \
--cc=eballetb@redhat.com \
--cc=khristineandreea.barbulescu@oss.nxp.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®