Re: [PATCH] m68k: delay: Fix rounding of short udelay() and ndelay()

From: Finn Thain

Date: Wed Nov 18 2026 - 04:52:47 EST



On Fri, 13 Nov 2026, Graeme Harker wrote:

> __xdelay() takes the high word of xloops * loops_per_jiffy and only then
> multiplies by HZ, so the loop count is truncated to a multiple of HZ.
> On a 16 MHz 68030 (loops_per_jiffy 17792, HZ 100) every udelay() below
> 57 us becomes __delay(0): measured on such a machine, udelay(10),
> udelay(20) and udelay(50) all take the same 8 us, the call overhead,
> and udelay(100) takes 67 us.
>
> Multiply by loops per second instead and keep the high word, which
> truncates by less than one loop; __delay() runs one more loop than it
> is given, so the delay is never short. As on x86, a factor of 4 is
> moved from loops per second to xloops, so that the product
> loops_per_jiffy * HZ only overflows above 2**34 loops per second
> (34000 BogoMIPS).
>
> ndelay() works out loops per microsecond from loops_per_jiffy rounded
> down to a multiple of 2048, which on this CPU is 1 instead of 1.78.
> With a 64-bit multiply available, use __xdelay() as udelay() does,
> with 5 (2**32 / 10**9 rounded up) as on x86. The CPU_HAS_NO_MULDIV64
> variants are unchanged.
>
> Measured on the 68030 (1 s of each, timed by jiffies, interrupts on),
> time per call before -> after:
>
> udelay(10) 8.0 us -> 18.2 us
> udelay(20) 8.0 us -> 29.0 us
> udelay(50) 8.5 us -> 61.5 us
> udelay(100) 67 us -> 114 us
> udelay(1000) 1020 us -> 1070 us
> ndelay(400) 1.8 us -> 7.9 us
>
> About 8 us of each is the call and the 64-bit multiply on this CPU.
> ndelay() now pays for that multiply too, so very short ndelay()s cost
> more; they are no longer short when loops_per_jiffy is small.

It's hard to measure anything accurately with interrupts enabled,
without CPU isolation, NO_HZ, HIGH_RES_TIMERS, GENERIC_CLOCKEVENTS etc.

(BTW, I notice that m68k and RISCPC are the last remaining users of
LEGACY_TIMER_TICK...)

Which clocksource driver are you using for jiffies?

I did some measurements with your patch on a Motorola 68040 (Mac LC 630).
This build does not set CONFIG_CPU_HAS_NO_MULDIV64.

My method was a bit different than yours (code is given below): nominal
40ms of each call, measured with a VIA timer, with interrupts disabled.
(The jiffies clocksource on this platform uses the same VIA clock.)

Here's what I measured:

udelay(10) 9.94E-06 -> 10.3E-06 seconds
udelay(20) 19.0E-06 -> 20.1E-06 seconds
udelay(50) 46.2E-06 -> 49.4E-06 seconds
udelay(100) 91.6E-06 -> 98.3E-06 seconds
udelay(1000) 972E-06 -> 978E-06 seconds
ndelay(400) 536E-09 -> 239E-09 seconds

You were right about the delays being short, and you've certainly improved
upon that for udelay(). But ndelay() appears to have regressed in my test.

There is a lot of overhead in my ndelay() measurement so I'm surprised to
see it measure low. I would have expected the opposite. Maybe my method
is just not accurate enough.

Thanks for working on this.

---

diff --git a/arch/m68k/mac/config.c b/arch/m68k/mac/config.c
index b745cf20a71f..bc1da6ee014a 100644
--- a/arch/m68k/mac/config.c
+++ b/arch/m68k/mac/config.c
@@ -65,6 +65,54 @@ static void __init mac_sched_init(void)
via_init_clock();
}

+#define GET_VIA2_TIMER2() \
+({ \
+ u8 low, high; \
+ do { \
+ high = via1[vT2CH]; \
+ low = via1[vT2CL]; \
+ } while (via1[vT2CH] != high); \
+ 1 + low + (high << 8); \
+})
+
+#define DELAY_LOOP(n, cmd) ({ \
+ u32 i, I = n / 40; \
+ u16 t1, t2; \
+ int d; \
+ t1 = GET_VIA2_TIMER2(); \
+ for (i = 0; i < I; ++i) { \
+ cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; \
+ cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; \
+ cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; \
+ cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; cmd; \
+ } \
+ t2 = GET_VIA2_TIMER2(); \
+ d = t1 - t2; \
+ printk("%10d invocations, %20s, %10d timer2 cycles\n", I * 40 , #cmd, d); \
+ })
+
+static int __init measure_delays(void)
+{
+ unsigned long flags;
+
+ via1[vACR] &= ~0x20; // free running T2 with no port B output
+
+ // Given eclk is 783360 Hz, the 16 bit counter can measure up to 80 ms.
+
+ local_irq_save(flags);
+ DELAY_LOOP(4000, udelay(10));
+ DELAY_LOOP(2000, udelay(20));
+ DELAY_LOOP(800, udelay(50));
+ DELAY_LOOP(400, udelay(100));
+ DELAY_LOOP(40, udelay(1000));
+ DELAY_LOOP(100000, ndelay(400));
+ local_irq_restore(flags);
+
+ return 0;
+}
+
+subsys_initcall(measure_delays);
+
/*
* Parse a Macintosh-specific record in the bootinfo
*/