From: Finn Thain <fthain@linux-m68k.org>
To: Graeme Harker <graeme.harker@gmail.com>
Cc: geert@linux-m68k.org, linux-m68k@lists.linux-m68k.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] m68k: delay: Fix rounding of short udelay() and ndelay()
Date: Wed, 18 Nov 2026 20:52:16 +1100 (AEDT) [thread overview]
Message-ID: <ae768ed3-a517-3223-5c86-a1599ff6c8e5@linux-m68k.org> (raw)
In-Reply-To: <20261113143832.168233-1-graeme.harker@gmail.com>
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
*/
prev parent reply other threads:[~2026-11-18 9:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 14:38 Graeme Harker
2026-11-18 9:52 ` Finn Thain [this message]
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=ae768ed3-a517-3223-5c86-a1599ff6c8e5@linux-m68k.org \
--to=fthain@linux-m68k.org \
--cc=geert@linux-m68k.org \
--cc=graeme.harker@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.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®