* [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT
@ 2026-12-05 5:33 Matt Turner
2026-12-05 20:38 ` [PATCH 1/2] alpha: Use rpcc when WTINT is configured but not implemented Matt Turner
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Matt Turner @ 2026-12-05 5:33 UTC (permalink / raw)
To: Richard Henderson, Magnus Lindholm; +Cc: linux-alpha, linux-kernel, Matt Turner
Alpha registers the rpcc clocksource only on uniprocessor machines and
only without CONFIG_ALPHA_WTINT, which most SMP configurations enable.
Every SMP machine other than QEMU therefore runs on the jiffies
clocksource.
Patch 1 makes the WTINT restriction a runtime decision. Not all PALcode
implements WTINT; where it does not, do_entIF() already turns the call
into a no-op, so the counter never stops and rpcc is safe to use. The
Marvel firmware's Tru64 PALcode is one of these.
Patch 2 lifts the uniprocessor restriction by measuring a per-CPU offset
between the CPUs' cycle counters, as proposed for parisc's cr16 [1].
Firmware zeroes each counter separately when the CPU starts and only
PALcode can write it, so the counters cannot be aligned directly.
Tested on an ES47 (Marvel) with two 1.3 GHz EV7s, CONFIG_ALPHA_WTINT=y:
rpcc: CPU1 offset 607373602 cycles (reference CPU0, round trip 963 cycles)
clocksource: Switched to clocksource rpcc
Two threads pinned to different CPUs, passing CLOCK_MONOTONIC readings
back and forth, saw no backward step in 3 million round trips, both
right after boot and after 21 minutes.
A draft that remeasured the offsets every minute saw them move by at
most 275 cycles over 20 minutes, with no trend, so the series measures
only once, at boot.
If the parisc patch lands too, the measurement could move into common
code; I'm happy to do that as a follow-up.
[1] https://patch.msgid.link/20261204-parisc-cr16-smp-v2-1-1e37fc62aae2@gmail.com
---
Matt Turner (2):
alpha: Use rpcc when WTINT is configured but not implemented
alpha: Synchronize rpcc across CPUs on SMP
arch/alpha/include/asm/hwrpb.h | 1 +
arch/alpha/kernel/proto.h | 1 +
arch/alpha/kernel/time.c | 235 ++++++++++++++++++++++++++++++++++++++---
arch/alpha/kernel/traps.c | 4 +
4 files changed, 227 insertions(+), 14 deletions(-)
---
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
change-id: 20261204-alpha-rpcc-smp-5929dd41d2a7
Best regards,
--
Matt Turner <mattst88@gmail.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] alpha: Use rpcc when WTINT is configured but not implemented
2026-12-05 5:33 [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Matt Turner
@ 2026-12-05 20:38 ` Matt Turner
2026-12-05 20:38 ` [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP Matt Turner
2026-12-06 0:32 ` [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Magnus Lindholm
2 siblings, 0 replies; 5+ messages in thread
From: Matt Turner @ 2026-12-05 20:38 UTC (permalink / raw)
To: Richard Henderson, Magnus Lindholm; +Cc: linux-alpha, linux-kernel
With CONFIG_ALPHA_WTINT, which most SMP configurations enable, the
rpcc clocksource is compiled out: CALL_PAL WTINT may slow or stop the
cycle counter while the CPU waits for an interrupt. But not every PALcode
implements WTINT. Where it does not, the call raises an OPCDEC fault
and do_entIF() patches it into a no-op, so the counter keeps running
and the kernel is left on the jiffies clocksource for no reason.
The Tru64 PALcode in the Marvel (ES47, GS1280) firmware is one of
these: its WTINT entry is the same stub as its unassigned CALL_PAL
functions, and on an ES47 the call traps.
Always build the rpcc clocksource and record in do_entIF() that WTINT
had to be emulated. Register the clocksource from a device_initcall()
instead of time_init(), which runs with interrupts disabled, and before
that call WTINT once with interrupts enabled: where it is implemented
it returns at the next interrupt, and where it is not, the emulation
says so.
Signed-off-by: Matt Turner <mattst88@gmail.com>
---
arch/alpha/kernel/proto.h | 1 +
arch/alpha/kernel/time.c | 40 ++++++++++++++++++++++++++++++++--------
arch/alpha/kernel/traps.c | 4 ++++
3 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/arch/alpha/kernel/proto.h b/arch/alpha/kernel/proto.h
index 7ece3b635e27..0226aa8281f0 100644
--- a/arch/alpha/kernel/proto.h
+++ b/arch/alpha/kernel/proto.h
@@ -190,6 +190,7 @@ extern void do_entDbg(struct pt_regs *);
struct allregs;
extern void do_entUna(void *, unsigned long, unsigned long, struct allregs *);
extern void do_entUnaUser(void __user *, unsigned long, unsigned long, struct pt_regs *);
+extern bool alpha_wtint_missing;
/* irq_alpha.c */
extern void notrace lockdep_on_restore(unsigned long ps, unsigned long ip);
diff --git a/arch/alpha/kernel/time.c b/arch/alpha/kernel/time.c
index 4d01c392ab14..7a5aa0ce5ac5 100644
--- a/arch/alpha/kernel/time.c
+++ b/arch/alpha/kernel/time.c
@@ -246,7 +246,6 @@ common_init_rtc(void)
}
\f
-#ifndef CONFIG_ALPHA_WTINT
/*
* The RPCC as a clocksource primitive.
*
@@ -258,7 +257,7 @@ common_init_rtc(void)
*
* When using the WTINT PALcall, the RPCC may shift to a lower frequency,
* or stop altogether, while waiting for the interrupt. Therefore we cannot
- * use this method when WTINT is in use.
+ * use this method when the PALcode implements WTINT.
*/
static u64 read_rpcc(struct clocksource *cs)
@@ -273,7 +272,8 @@ static struct clocksource clocksource_rpcc = {
.mask = CLOCKSOURCE_MASK(32),
.flags = CLOCK_SOURCE_IS_CONTINUOUS
};
-#endif /* ALPHA_WTINT */
+
+static unsigned long rpcc_freq __initdata;
\f
/* Validate a computed cycle counter result against the known bounds for
@@ -434,17 +434,41 @@ time_init(void)
"and unable to estimate a proper value!\n");
}
- /* See above for restrictions on using clocksource_rpcc. */
-#ifndef CONFIG_ALPHA_WTINT
- if (hwrpb->nr_processors == 1)
- clocksource_register_hz(&clocksource_rpcc, cycle_freq);
-#endif
+ /* Registered by init_rpcc_clocksource(). */
+ rpcc_freq = cycle_freq;
/* Startup the timer source. */
alpha_mv.init_rtc();
init_rtc_clockevent();
}
+/*
+ * Registered from an initcall rather than time_init(), which runs with
+ * interrupts disabled, too early to probe for WTINT.
+ */
+static int __init init_rpcc_clocksource(void)
+{
+ /* QEMU uses qemu_cs; time_init() leaves rpcc_freq unset. */
+ if (alpha_using_qemu)
+ return 0;
+
+ /* See above for restrictions on using clocksource_rpcc. */
+ if (hwrpb->nr_processors != 1)
+ return 0;
+
+ if (IS_ENABLED(CONFIG_ALPHA_WTINT)) {
+ /* Returns at the next interrupt, or traps if unimplemented. */
+ wtint(0);
+ if (!alpha_wtint_missing)
+ return 0;
+ }
+
+ clocksource_register_hz(&clocksource_rpcc, rpcc_freq);
+
+ return 0;
+}
+device_initcall(init_rpcc_clocksource);
+
/* Initialize the clock_event_device for secondary cpus. */
#ifdef CONFIG_SMP
void __init
diff --git a/arch/alpha/kernel/traps.c b/arch/alpha/kernel/traps.c
index db6277b3bed0..b335928c8314 100644
--- a/arch/alpha/kernel/traps.c
+++ b/arch/alpha/kernel/traps.c
@@ -196,6 +196,9 @@ do_entArith(unsigned long summary, unsigned long write_mask,
send_sig_fault_trapno(SIGFPE, si_code, (void __user *) regs->pc, 0, current);
}
+/* Set once do_entIF() has had to emulate CALL_PAL WTINT. */
+bool alpha_wtint_missing;
+
asmlinkage void
do_entIF(unsigned long type, struct pt_regs *regs)
{
@@ -234,6 +237,7 @@ do_entIF(unsigned long type, struct pt_regs *regs)
*pinsn = 0x47e01400; /* mov 0,$0 */
imb();
regs->r0 = 0;
+ alpha_wtint_missing = true;
return;
}
}
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP
2026-12-05 5:33 [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Matt Turner
2026-12-05 20:38 ` [PATCH 1/2] alpha: Use rpcc when WTINT is configured but not implemented Matt Turner
@ 2026-12-05 20:38 ` Matt Turner
2026-12-06 0:30 ` Magnus Lindholm
2026-12-06 0:32 ` [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Magnus Lindholm
2 siblings, 1 reply; 5+ messages in thread
From: Matt Turner @ 2026-12-05 20:38 UTC (permalink / raw)
To: Richard Henderson, Magnus Lindholm; +Cc: linux-alpha, linux-kernel
The rpcc clocksource is registered only when the HWRPB lists a single
processor, because the CPUs' cycle counters are not synchronized. SMP
machines therefore fall back to the jiffies clocksource, with a
resolution of one tick.
The counters cannot be synchronized directly. Firmware zeroes each CPU's
counter separately, in PALcode reset initialization when that CPU starts,
and only PALcode can write CC_CTL. The Marvel firmware's Tru64 PALcode
never writes it and offers no CSERVE function that does; Tru64 itself
never corrects the counters, because it does not use them to keep time.
On an ES47 with two 1.3 GHz EV7s the counters differ by about half a
second.
Instead, keep a per-CPU offset that the clocksource read adds to the
counter, the way the parisc cr16 patch under review does, and measure
each CPU's offset against a reference CPU before the clocksource is
registered. The measurement is a ping-pong over a shared cache line with
interrupts disabled on both CPUs: the target CPU reads its counter, asks
the reference CPU for a timestamp, and reads its counter again when the
reply arrives. The reply is assumed to have been taken halfway through
the round trip, and the round with the shortest round trip out of 64
wins. The counters are 32 bits wide, so all of this is modulo 2^32.
Measure once, at boot. On the ES47, measurements repeated every minute
for 20 minutes differed by at most 275 cycles with no trend, so the
counters do not drift apart. Alpha has no CPU hotplug, so no CPU needs
measuring later.
An offset cannot correct counters that run at different rates. From
revision 13 of the HWRPB, each CPU's slot can give its own cycle counter
frequency (Tru64 reads it there); keep the jiffies clocksource if any two
differ.
Signed-off-by: Matt Turner <mattst88@gmail.com>
---
arch/alpha/include/asm/hwrpb.h | 1 +
arch/alpha/kernel/time.c | 203 +++++++++++++++++++++++++++++++++++++++--
2 files changed, 194 insertions(+), 10 deletions(-)
diff --git a/arch/alpha/include/asm/hwrpb.h b/arch/alpha/include/asm/hwrpb.h
index db831cf8de10..5469c97b9733 100644
--- a/arch/alpha/include/asm/hwrpb.h
+++ b/arch/alpha/include/asm/hwrpb.h
@@ -114,6 +114,7 @@ struct percpu_struct {
unsigned long console_data_log_pa;
unsigned long console_data_log_length;
unsigned long bcache_info;
+ unsigned long cycle_freq; /* HWRPB revision > 12 */
};
struct procdesc_struct {
diff --git a/arch/alpha/kernel/time.c b/arch/alpha/kernel/time.c
index 7a5aa0ce5ac5..fd797e42d243 100644
--- a/arch/alpha/kernel/time.c
+++ b/arch/alpha/kernel/time.c
@@ -44,6 +44,8 @@
#include <linux/timex.h>
#include <linux/clocksource.h>
#include <linux/clockchips.h>
+#include <linux/percpu.h>
+#include <linux/smp.h>
#include "proto.h"
#include "irq_impl.h"
@@ -249,20 +251,200 @@ common_init_rtc(void)
/*
* The RPCC as a clocksource primitive.
*
- * While we have free-running timecounters running on all CPUs, and we make
- * a half-hearted attempt in init_rtc_rpcc_info to sync the timecounter
- * with the wall clock, that initialization isn't kept up-to-date across
- * different time counters in SMP mode. Therefore we can only use this
- * method when there's only one CPU enabled.
+ * Firmware zeroes each CPU's cycle counter separately, when the CPU starts,
+ * and only PALcode can write it, so on SMP the counters of different CPUs
+ * disagree. Keep a per-CPU offset that aligns each CPU's counter with a
+ * reference CPU's, measured once before the clocksource is registered.
+ * Only the clocksource applies it.
*
* When using the WTINT PALcall, the RPCC may shift to a lower frequency,
* or stop altogether, while waiting for the interrupt. Therefore we cannot
* use this method when the PALcode implements WTINT.
*/
+#ifdef CONFIG_SMP
+static DEFINE_PER_CPU(u32, rpcc_offset);
+
+static inline u32 rpcc_this_cpu_offset(void)
+{
+ return __this_cpu_read(rpcc_offset);
+}
+
+#define RPCC_SYNC_ROUNDS 64
+
+/*
+ * The target CPU asks for a timestamp by setting @go, and the reference CPU
+ * replies with its adjusted counter in @ref_time (never 0). The target takes
+ * the reply as having been read halfway through its round trip and keeps the
+ * round with the shortest round trip.
+ *
+ * Each flag is set with a release store and polled with an acquire load, so
+ * that a CPU clearing @go or @ref_time for the next round, or writing its
+ * new offset before @done, is seen to do so before the flag it then sets.
+ *
+ * The counters are 32 bits wide; all arithmetic on them is modulo 2^32.
+ */
+static struct {
+ unsigned long go;
+ unsigned long ref_time;
+ unsigned long done;
+ unsigned long rtt;
+} rpcc_sync ____cacheline_aligned_in_smp;
+
+static void __init rpcc_sync_target(void *unused)
+{
+ u32 t0, t1, tm, rtt;
+ u32 best_rtt = U32_MAX, best_mid = 0, best_tm = 0;
+ int i;
+
+ for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
+ t0 = rpcc();
+ /* Orders the clearing of @ref_time before the request. */
+ smp_store_release(&rpcc_sync.go, 1);
+ /* Pairs with the release of @ref_time in rpcc_sync_cpu(). */
+ while (!(tm = smp_load_acquire(&rpcc_sync.ref_time)))
+ cpu_relax();
+ /* Do not let the read of the counter pass the load above. */
+ mb();
+ t1 = rpcc();
+ WRITE_ONCE(rpcc_sync.ref_time, 0);
+
+ rtt = t1 - t0;
+ if (rtt < best_rtt) {
+ best_rtt = rtt;
+ best_mid = t0 + rtt / 2;
+ best_tm = tm;
+ }
+ }
+
+ rpcc_sync.rtt = best_rtt;
+ __this_cpu_write(rpcc_offset, best_tm - best_mid);
+
+ /* Publishes the results above; pairs with rpcc_sync_cpu(). */
+ smp_store_release(&rpcc_sync.done, 1);
+}
+
+static void __init rpcc_sync_wait(unsigned long *flag)
+{
+ /* Pairs with the release of @flag in rpcc_sync_target(). */
+ while (!smp_load_acquire(flag))
+ cpu_relax();
+}
+
+/* Align @cpu's counter with this CPU's. Called with migration disabled. */
+static void __init rpcc_sync_cpu(unsigned int cpu)
+{
+ unsigned long flags;
+ u32 tm;
+ int i;
+
+ rpcc_sync.go = 0;
+ rpcc_sync.ref_time = 0;
+ rpcc_sync.done = 0;
+
+ preempt_disable();
+ if (smp_call_function_single(cpu, rpcc_sync_target, NULL, 0)) {
+ preempt_enable();
+ return;
+ }
+
+ /*
+ * Wait for the first request with interrupts enabled: the target CPU
+ * may be waiting for this one to answer an IPI before it can run
+ * rpcc_sync_target(). That only lengthens the first round trip.
+ */
+ rpcc_sync_wait(&rpcc_sync.go);
+ local_irq_save(flags);
+ for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
+ if (i)
+ rpcc_sync_wait(&rpcc_sync.go);
+ WRITE_ONCE(rpcc_sync.go, 0);
+ /* Do not let the read of the counter pass the load above. */
+ mb();
+ tm = rpcc() + rpcc_this_cpu_offset();
+ /* Orders the clearing of @go before the reply. */
+ smp_store_release(&rpcc_sync.ref_time, tm ?: 1);
+ }
+ local_irq_restore(flags);
+
+ rpcc_sync_wait(&rpcc_sync.done);
+ preempt_enable();
+
+ pr_info("rpcc: CPU%u offset %d cycles (reference CPU%u, round trip %lu cycles)\n",
+ cpu, (s32)per_cpu(rpcc_offset, cpu), smp_processor_id(),
+ rpcc_sync.rtt);
+}
+
+/*
+ * An offset corrects only counters that run at the same rate. From HWRPB
+ * revision 13 on, each CPU's slot may give its own cycle counter frequency.
+ */
+static bool __init rpcc_same_freq(void)
+{
+ unsigned long freq = 0;
+ unsigned int cpu;
+
+ if (hwrpb->revision <= 12)
+ return true;
+
+ for_each_online_cpu(cpu) {
+ struct percpu_struct *slot = (struct percpu_struct *)
+ ((char *)hwrpb + hwrpb->processor_offset
+ + cpu * hwrpb->processor_size);
+
+ if (!slot->cycle_freq)
+ continue;
+ if (freq && slot->cycle_freq != freq) {
+ pr_info("rpcc: CPU%u counts at %lu Hz, not %lu Hz\n",
+ cpu, slot->cycle_freq, freq);
+ return false;
+ }
+ freq = slot->cycle_freq;
+ }
+
+ return true;
+}
+
+static bool __init rpcc_sync_init(void)
+{
+ unsigned int cpu;
+
+ if (num_online_cpus() == 1)
+ return true;
+
+ if (!rpcc_same_freq())
+ return false;
+
+ migrate_disable();
+ for_each_online_cpu(cpu) {
+ if (cpu != smp_processor_id())
+ rpcc_sync_cpu(cpu);
+ }
+ migrate_enable();
+
+ return true;
+}
+#else
+static inline u32 rpcc_this_cpu_offset(void)
+{
+ return 0;
+}
+
+static inline bool rpcc_sync_init(void)
+{
+ return true;
+}
+#endif /* CONFIG_SMP */
+
static u64 read_rpcc(struct clocksource *cs)
{
- return rpcc();
+ u32 now;
+
+ preempt_disable_notrace();
+ now = rpcc() + rpcc_this_cpu_offset();
+ preempt_enable_notrace();
+
+ return now;
}
static struct clocksource clocksource_rpcc = {
@@ -444,7 +626,8 @@ time_init(void)
/*
* Registered from an initcall rather than time_init(), which runs with
- * interrupts disabled, too early to probe for WTINT.
+ * interrupts disabled, too early to probe for WTINT, and before the
+ * secondary CPUs are up to have their counters measured.
*/
static int __init init_rpcc_clocksource(void)
{
@@ -453,9 +636,6 @@ static int __init init_rpcc_clocksource(void)
return 0;
/* See above for restrictions on using clocksource_rpcc. */
- if (hwrpb->nr_processors != 1)
- return 0;
-
if (IS_ENABLED(CONFIG_ALPHA_WTINT)) {
/* Returns at the next interrupt, or traps if unimplemented. */
wtint(0);
@@ -463,6 +643,9 @@ static int __init init_rpcc_clocksource(void)
return 0;
}
+ if (!rpcc_sync_init())
+ return 0;
+
clocksource_register_hz(&clocksource_rpcc, rpcc_freq);
return 0;
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP
2026-12-05 20:38 ` [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP Matt Turner
@ 2026-12-06 0:30 ` Magnus Lindholm
0 siblings, 0 replies; 5+ messages in thread
From: Magnus Lindholm @ 2026-12-06 0:30 UTC (permalink / raw)
To: Matt Turner; +Cc: Richard Henderson, linux-alpha, linux-kernel
Hi Matt,
On Sat, Dec 5, 2026 at 9:38 PM Matt Turner <mattst88@gmail.com> wrote:
>
> The rpcc clocksource is registered only when the HWRPB lists a single
> processor, because the CPUs' cycle counters are not synchronized. SMP
> machines therefore fall back to the jiffies clocksource, with a
> resolution of one tick.
>
> The counters cannot be synchronized directly. Firmware zeroes each CPU's
> counter separately, in PALcode reset initialization when that CPU starts,
> and only PALcode can write CC_CTL. The Marvel firmware's Tru64 PALcode
> never writes it and offers no CSERVE function that does; Tru64 itself
> never corrects the counters, because it does not use them to keep time.
> On an ES47 with two 1.3 GHz EV7s the counters differ by about half a
> second.
>
> Instead, keep a per-CPU offset that the clocksource read adds to the
> counter, the way the parisc cr16 patch under review does, and measure
> each CPU's offset against a reference CPU before the clocksource is
> registered. The measurement is a ping-pong over a shared cache line with
> interrupts disabled on both CPUs: the target CPU reads its counter, asks
> the reference CPU for a timestamp, and reads its counter again when the
> reply arrives. The reply is assumed to have been taken halfway through
> the round trip, and the round with the shortest round trip out of 64
> wins. The counters are 32 bits wide, so all of this is modulo 2^32.
>
> Measure once, at boot. On the ES47, measurements repeated every minute
> for 20 minutes differed by at most 275 cycles with no trend, so the
> counters do not drift apart. Alpha has no CPU hotplug, so no CPU needs
> measuring later.
>
> An offset cannot correct counters that run at different rates. From
> revision 13 of the HWRPB, each CPU's slot can give its own cycle counter
> frequency (Tru64 reads it there); keep the jiffies clocksource if any two
> differ.
>
> Signed-off-by: Matt Turner <mattst88@gmail.com>
> ---
> arch/alpha/include/asm/hwrpb.h | 1 +
> arch/alpha/kernel/time.c | 203 +++++++++++++++++++++++++++++++++++++++--
> 2 files changed, 194 insertions(+), 10 deletions(-)
>
> diff --git a/arch/alpha/include/asm/hwrpb.h b/arch/alpha/include/asm/hwrpb.h
> index db831cf8de10..5469c97b9733 100644
> --- a/arch/alpha/include/asm/hwrpb.h
> +++ b/arch/alpha/include/asm/hwrpb.h
> @@ -114,6 +114,7 @@ struct percpu_struct {
> unsigned long console_data_log_pa;
> unsigned long console_data_log_length;
> unsigned long bcache_info;
> + unsigned long cycle_freq; /* HWRPB revision > 12 */
> };
>
> struct procdesc_struct {
> diff --git a/arch/alpha/kernel/time.c b/arch/alpha/kernel/time.c
> index 7a5aa0ce5ac5..fd797e42d243 100644
> --- a/arch/alpha/kernel/time.c
> +++ b/arch/alpha/kernel/time.c
> @@ -44,6 +44,8 @@
> #include <linux/timex.h>
> #include <linux/clocksource.h>
> #include <linux/clockchips.h>
> +#include <linux/percpu.h>
> +#include <linux/smp.h>
>
> #include "proto.h"
> #include "irq_impl.h"
> @@ -249,20 +251,200 @@ common_init_rtc(void)
> /*
> * The RPCC as a clocksource primitive.
> *
> - * While we have free-running timecounters running on all CPUs, and we make
> - * a half-hearted attempt in init_rtc_rpcc_info to sync the timecounter
> - * with the wall clock, that initialization isn't kept up-to-date across
> - * different time counters in SMP mode. Therefore we can only use this
> - * method when there's only one CPU enabled.
> + * Firmware zeroes each CPU's cycle counter separately, when the CPU starts,
> + * and only PALcode can write it, so on SMP the counters of different CPUs
> + * disagree. Keep a per-CPU offset that aligns each CPU's counter with a
> + * reference CPU's, measured once before the clocksource is registered.
> + * Only the clocksource applies it.
> *
> * When using the WTINT PALcall, the RPCC may shift to a lower frequency,
> * or stop altogether, while waiting for the interrupt. Therefore we cannot
> * use this method when the PALcode implements WTINT.
> */
>
> +#ifdef CONFIG_SMP
> +static DEFINE_PER_CPU(u32, rpcc_offset);
> +
> +static inline u32 rpcc_this_cpu_offset(void)
> +{
> + return __this_cpu_read(rpcc_offset);
> +}
> +
> +#define RPCC_SYNC_ROUNDS 64
> +
> +/*
> + * The target CPU asks for a timestamp by setting @go, and the reference CPU
> + * replies with its adjusted counter in @ref_time (never 0). The target takes
> + * the reply as having been read halfway through its round trip and keeps the
> + * round with the shortest round trip.
> + *
> + * Each flag is set with a release store and polled with an acquire load, so
> + * that a CPU clearing @go or @ref_time for the next round, or writing its
> + * new offset before @done, is seen to do so before the flag it then sets.
> + *
> + * The counters are 32 bits wide; all arithmetic on them is modulo 2^32.
> + */
> +static struct {
> + unsigned long go;
> + unsigned long ref_time;
> + unsigned long done;
> + unsigned long rtt;
> +} rpcc_sync ____cacheline_aligned_in_smp;
> +
> +static void __init rpcc_sync_target(void *unused)
> +{
> + u32 t0, t1, tm, rtt;
> + u32 best_rtt = U32_MAX, best_mid = 0, best_tm = 0;
> + int i;
> +
> + for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
> + t0 = rpcc();
> + /* Orders the clearing of @ref_time before the request. */
> + smp_store_release(&rpcc_sync.go, 1);
> + /* Pairs with the release of @ref_time in rpcc_sync_cpu(). */
> + while (!(tm = smp_load_acquire(&rpcc_sync.ref_time)))
> + cpu_relax();
> + /* Do not let the read of the counter pass the load above. */
> + mb();
> + t1 = rpcc();
> + WRITE_ONCE(rpcc_sync.ref_time, 0);
> +
> + rtt = t1 - t0;
> + if (rtt < best_rtt) {
> + best_rtt = rtt;
> + best_mid = t0 + rtt / 2;
> + best_tm = tm;
> + }
> + }
> +
> + rpcc_sync.rtt = best_rtt;
> + __this_cpu_write(rpcc_offset, best_tm - best_mid);
> +
> + /* Publishes the results above; pairs with rpcc_sync_cpu(). */
> + smp_store_release(&rpcc_sync.done, 1);
> +}
> +
> +static void __init rpcc_sync_wait(unsigned long *flag)
> +{
> + /* Pairs with the release of @flag in rpcc_sync_target(). */
> + while (!smp_load_acquire(flag))
> + cpu_relax();
> +}
> +
> +/* Align @cpu's counter with this CPU's. Called with migration disabled. */
> +static void __init rpcc_sync_cpu(unsigned int cpu)
> +{
> + unsigned long flags;
> + u32 tm;
> + int i;
> +
> + rpcc_sync.go = 0;
> + rpcc_sync.ref_time = 0;
> + rpcc_sync.done = 0;
> +
> + preempt_disable();
> + if (smp_call_function_single(cpu, rpcc_sync_target, NULL, 0)) {
> + preempt_enable();
> + return;
> + }
> +
> + /*
> + * Wait for the first request with interrupts enabled: the target CPU
> + * may be waiting for this one to answer an IPI before it can run
> + * rpcc_sync_target(). That only lengthens the first round trip.
> + */
> + rpcc_sync_wait(&rpcc_sync.go);
> + local_irq_save(flags);
> + for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
> + if (i)
> + rpcc_sync_wait(&rpcc_sync.go);
> + WRITE_ONCE(rpcc_sync.go, 0);
> + /* Do not let the read of the counter pass the load above. */
> + mb();
> + tm = rpcc() + rpcc_this_cpu_offset();
> + /* Orders the clearing of @go before the reply. */
> + smp_store_release(&rpcc_sync.ref_time, tm ?: 1);
> + }
> + local_irq_restore(flags);
> +
> + rpcc_sync_wait(&rpcc_sync.done);
> + preempt_enable();
> +
> + pr_info("rpcc: CPU%u offset %d cycles (reference CPU%u, round trip %lu cycles)\n",
> + cpu, (s32)per_cpu(rpcc_offset, cpu), smp_processor_id(),
> + rpcc_sync.rtt);
> +}
> +
> +/*
> + * An offset corrects only counters that run at the same rate. From HWRPB
> + * revision 13 on, each CPU's slot may give its own cycle counter frequency.
> + */
> +static bool __init rpcc_same_freq(void)
> +{
> + unsigned long freq = 0;
> + unsigned int cpu;
> +
> + if (hwrpb->revision <= 12)
> + return true;
> +
> + for_each_online_cpu(cpu) {
> + struct percpu_struct *slot = (struct percpu_struct *)
> + ((char *)hwrpb + hwrpb->processor_offset
> + + cpu * hwrpb->processor_size);
> +
> + if (!slot->cycle_freq)
> + continue;
Table 26-4 of the Alpha Architecture Reference Manual says a zero
per-CPU frequency means to use HWRPB+112. If one CPU inherits that
frequency and another reports a different one explicitly, skipping
the zero entry makes rpcc_same_freq() accept the pair.
Could we compare slot->cycle_freq ?: hwrpb->cycle_freq for each
online CPU instead? A fixed offset can't correct that rate difference.
Thanks,
Magnus
> + if (freq && slot->cycle_freq != freq) {
> + pr_info("rpcc: CPU%u counts at %lu Hz, not %lu Hz\n",
> + cpu, slot->cycle_freq, freq);
> + return false;
> + }
> + freq = slot->cycle_freq;
> + }
> +
> + return true;
> +}
> +
> +static bool __init rpcc_sync_init(void)
> +{
> + unsigned int cpu;
> +
> + if (num_online_cpus() == 1)
> + return true;
> +
> + if (!rpcc_same_freq())
> + return false;
> +
> + migrate_disable();
> + for_each_online_cpu(cpu) {
> + if (cpu != smp_processor_id())
> + rpcc_sync_cpu(cpu);
> + }
> + migrate_enable();
> +
> + return true;
> +}
> +#else
> +static inline u32 rpcc_this_cpu_offset(void)
> +{
> + return 0;
> +}
> +
> +static inline bool rpcc_sync_init(void)
> +{
> + return true;
> +}
> +#endif /* CONFIG_SMP */
> +
> static u64 read_rpcc(struct clocksource *cs)
> {
> - return rpcc();
> + u32 now;
> +
> + preempt_disable_notrace();
> + now = rpcc() + rpcc_this_cpu_offset();
> + preempt_enable_notrace();
> +
> + return now;
> }
>
> static struct clocksource clocksource_rpcc = {
> @@ -444,7 +626,8 @@ time_init(void)
>
> /*
> * Registered from an initcall rather than time_init(), which runs with
> - * interrupts disabled, too early to probe for WTINT.
> + * interrupts disabled, too early to probe for WTINT, and before the
> + * secondary CPUs are up to have their counters measured.
> */
> static int __init init_rpcc_clocksource(void)
> {
> @@ -453,9 +636,6 @@ static int __init init_rpcc_clocksource(void)
> return 0;
>
> /* See above for restrictions on using clocksource_rpcc. */
> - if (hwrpb->nr_processors != 1)
> - return 0;
> -
> if (IS_ENABLED(CONFIG_ALPHA_WTINT)) {
> /* Returns at the next interrupt, or traps if unimplemented. */
> wtint(0);
> @@ -463,6 +643,9 @@ static int __init init_rpcc_clocksource(void)
> return 0;
> }
>
> + if (!rpcc_sync_init())
> + return 0;
> +
> clocksource_register_hz(&clocksource_rpcc, rpcc_freq);
>
> return 0;
>
> --
> 2.56.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT
2026-12-05 5:33 [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Matt Turner
2026-12-05 20:38 ` [PATCH 1/2] alpha: Use rpcc when WTINT is configured but not implemented Matt Turner
2026-12-05 20:38 ` [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP Matt Turner
@ 2026-12-06 0:32 ` Magnus Lindholm
2 siblings, 0 replies; 5+ messages in thread
From: Magnus Lindholm @ 2026-12-06 0:32 UTC (permalink / raw)
To: Matt Turner; +Cc: Richard Henderson, linux-alpha, linux-kernel
Hi Matt,
On Sat, Dec 5, 2026 at 6:33 AM Matt Turner <mattst88@gmail.com> wrote:
>
> Alpha registers the rpcc clocksource only on uniprocessor machines and
> only without CONFIG_ALPHA_WTINT, which most SMP configurations enable.
> Every SMP machine other than QEMU therefore runs on the jiffies
> clocksource.
>
> Patch 1 makes the WTINT restriction a runtime decision. Not all PALcode
> implements WTINT; where it does not, do_entIF() already turns the call
> into a no-op, so the counter never stops and rpcc is safe to use. The
> Marvel firmware's Tru64 PALcode is one of these.
>
> Patch 2 lifts the uniprocessor restriction by measuring a per-CPU offset
> between the CPUs' cycle counters, as proposed for parisc's cr16 [1].
> Firmware zeroes each counter separately when the CPU starts and only
> PALcode can write it, so the counters cannot be aligned directly.
>
> Tested on an ES47 (Marvel) with two 1.3 GHz EV7s, CONFIG_ALPHA_WTINT=y:
>
> rpcc: CPU1 offset 607373602 cycles (reference CPU0, round trip 963 cycles)
> clocksource: Switched to clocksource rpcc
>
> Two threads pinned to different CPUs, passing CLOCK_MONOTONIC readings
> back and forth, saw no backward step in 3 million round trips, both
> right after boot and after 21 minutes.
>
> A draft that remeasured the offsets every minute saw them move by at
> most 275 cycles over 20 minutes, with no trend, so the series measures
> only once, at boot.
>
> If the parisc patch lands too, the measurement could move into common
> code; I'm happy to do that as a follow-up.
>
> [1] https://patch.msgid.link/20261204-parisc-cr16-smp-v2-1-1e37fc62aae2@gmail.com
>
> ---
> Matt Turner (2):
> alpha: Use rpcc when WTINT is configured but not implemented
> alpha: Synchronize rpcc across CPUs on SMP
>
> arch/alpha/include/asm/hwrpb.h | 1 +
> arch/alpha/kernel/proto.h | 1 +
> arch/alpha/kernel/time.c | 235 ++++++++++++++++++++++++++++++++++++++---
> arch/alpha/kernel/traps.c | 4 +
> 4 files changed, 227 insertions(+), 14 deletions(-)
> ---
> base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
> change-id: 20261204-alpha-rpcc-smp-5929dd41d2a7
>
> Best regards,
> --
> Matt Turner <mattst88@gmail.com>
>
When you spin v2, could you base it on my for-next? The generic-entry
changes there moved the WTINT handling into handle_entIF(), so 1/2
needs a small context fix in traps.c. With that resolved, 2/2 applies
cleanly.
Thanks,
Magnus
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-06 0:33 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05 5:33 [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Matt Turner
2026-12-05 20:38 ` [PATCH 1/2] alpha: Use rpcc when WTINT is configured but not implemented Matt Turner
2026-12-05 20:38 ` [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP Matt Turner
2026-12-06 0:30 ` Magnus Lindholm
2026-12-06 0:32 ` [PATCH 0/2] alpha: Use rpcc as the clocksource on SMP and without WTINT Magnus Lindholm
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®