From: Marc Zyngier <maz@kernel.org>
To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Thomas Gleixner <tglx@kernel.org>,
Ben Horgan <ben.horgan@arm.com>,
Daniel Lezcano <daniel.lezcano@linaro.org>
Subject: [PATCH v3 5/8] clocksource/drivers/arm_arch_timer: Turn arch_timer_read_counter into a function
Date: Sun, 2 Aug 2026 17:53:24 +0100 [thread overview]
Message-ID: <20260802165327.385217-6-maz@kernel.org> (raw)
In-Reply-To: <20260802165327.385217-1-maz@kernel.org>
Now that we control the workarounds applied to the counters with
a static key, we can also do the same thing for the selection
between virtual and physical counters, as this selection is
done on the boot CPU, where we have the freedom to flip static
keys.
With this, there is no need for arch_timer_read_counter to be
a function pointer, and we can directly promote it to an actual
function.
While we're at it, apply the same static key treatment to the raw_*
accessors.
Signed-off-by: Marc Zyngier <maz@kernel.org>
---
drivers/clocksource/arm_arch_timer.c | 38 +++++++++++++++++-----------
include/clocksource/arm_arch_timer.h | 2 +-
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c
index 5085e87706f99..747f51d9225c2 100644
--- a/drivers/clocksource/arm_arch_timer.c
+++ b/drivers/clocksource/arm_arch_timer.c
@@ -134,9 +134,21 @@ static notrace u64 arch_counter_get_cntvct(void)
* to exist on arm64. arm doesn't use this before DT is probed so even
* if we don't have the cp15 accessors we won't have a problem.
*/
-u64 (*arch_timer_read_counter)(void) __ro_after_init = arch_counter_get_cntvct;
+static DEFINE_STATIC_KEY_TRUE(arch_counter_is_virtual);
+
+u64 notrace arch_timer_read_counter(void)
+{
+ return static_branch_likely(&arch_counter_is_virtual) ?
+ arch_counter_get_cntvct() : arch_counter_get_cntpct();
+}
EXPORT_SYMBOL_GPL(arch_timer_read_counter);
+static u64 noinstr raw_arch_timer_read_counter(void)
+{
+ return static_branch_likely(&arch_counter_is_virtual) ?
+ raw_counter_get_cntvct() : raw_counter_get_cntpct();
+}
+
static u64 arch_counter_read(struct clocksource *cs)
{
return arch_timer_read_counter();
@@ -940,22 +952,19 @@ struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
static void __init arch_counter_register(void)
{
- u64 (*scr)(void);
- u64 (*rd)(void);
u64 start_count;
int width;
- if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
- arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI ||
- arch_timer_uses_ppi == ARCH_TIMER_HYP_VIRT_PPI) {
- rd = arch_counter_get_cntvct;
- scr = raw_counter_get_cntvct;
- } else {
- rd = arch_counter_get_cntpct;
- scr = raw_counter_get_cntpct;
+ switch (arch_timer_uses_ppi) {
+ case ARCH_TIMER_PHYS_SECURE_PPI:
+ case ARCH_TIMER_PHYS_NONSECURE_PPI:
+ case ARCH_TIMER_HYP_PPI:
+ static_branch_disable(&arch_counter_is_virtual);
+ break;
+ default:
+ break;
}
- arch_timer_read_counter = rd;
clocksource_counter.vdso_clock_mode = vdso_default;
width = arch_counter_get_width();
@@ -971,15 +980,14 @@ static void __init arch_counter_register(void)
timecounter_init(&arch_timer_kvm_info.timecounter,
&cyclecounter, start_count);
- sched_clock_register(scr, width, arch_timer_rate);
+ sched_clock_register(raw_arch_timer_read_counter, width, arch_timer_rate);
}
bool read_sched_clock_is_arch_counter(const struct clock_read_data *crd)
{
u64 (*rd)(void) = crd->read_sched_clock;
- return (rd == raw_counter_get_cntvct ||
- rd == raw_counter_get_cntpct);
+ return (rd == raw_arch_timer_read_counter);
}
static void arch_timer_stop(struct clock_event_device *clk)
diff --git a/include/clocksource/arm_arch_timer.h b/include/clocksource/arm_arch_timer.h
index 84cdbaffcfb5e..8deb815ed0a08 100644
--- a/include/clocksource/arm_arch_timer.h
+++ b/include/clocksource/arm_arch_timer.h
@@ -89,7 +89,7 @@ struct clock_read_data;
#ifdef CONFIG_ARM_ARCH_TIMER
extern u32 arch_timer_get_rate(void);
-extern u64 (*arch_timer_read_counter)(void);
+extern u64 arch_timer_read_counter(void);
extern struct arch_timer_kvm_info *arch_timer_get_kvm_info(void);
extern bool arch_timer_evtstrm_available(void);
extern bool read_sched_clock_is_arch_counter(const struct clock_read_data *);
--
2.47.3
next prev parent reply other threads:[~2026-08-02 16:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 16:53 [PATCH v3 0/8] arm64: arch_timer: Improve errata handling Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 1/8] clocksource/drivers/arm_arch_timer: Add read_sched_clock_is_arch_counter() predicate Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 2/8] clocksource/drivers/arm_arch_timer: Add a static key indicating the need for a runtime workaround Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 3/8] clocksource/drivers/arm_arch_timer: Convert counter accessors to a static key alternative Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 4/8] clocksource/drivers/arm_arch_timer: Drop the arch_counter_get_cnt{p,v}ct_stable() accessors Marc Zyngier
2026-08-02 16:53 ` Marc Zyngier [this message]
2026-08-02 16:53 ` [PATCH v3 6/8] clocksource/drivers/arm_arch_timer: Add command-line control over the counter errata management Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 7/8] clocksource/drivers/arm_arch_timer: Expose a direct accessor for the virtual counter Marc Zyngier
2026-08-02 16:53 ` [PATCH v3 8/8] arm64: Convert __delay_cycles() to arch_timer_read_vcounter() Marc Zyngier
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=20260802165327.385217-6-maz@kernel.org \
--to=maz@kernel.org \
--cc=ben.horgan@arm.com \
--cc=catalin.marinas@arm.com \
--cc=daniel.lezcano@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=tglx@kernel.org \
--cc=will@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