mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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: Fri, 20 Nov 2026 15:34:46 +1100 (AEDT)	[thread overview]
Message-ID: <5783b448-b34a-58a5-0bc0-98b1a806f182@linux-m68k.org> (raw)
In-Reply-To: <20261118210008.927764-1-graeme.harker@gmail.com>


On Wed, 18 Nov 2026, Graeme Harker wrote:

> 
> > Which clocksource driver are you using for jiffies?
> 
> The jiffies clocksource itself; the board has no sub-tick clocksource 
> yet. The tick is a periodic clock_event_device on the DUART's 
> counter/timer (GENERIC_CLOCKEVENTS, not LEGACY_TIMER_TICK), and my 
> figures were 1 s of each call timed by jiffies with interrupts on, so 
> they include the tick's own cost. Yours are the better numbers.
> 

Maybe. But this clock oscillator is running at some frequency which I 
haven't actually measured. Also, I'm measuring wallclock time, which may 
be inappropriate if the udelay() interval is derived from loops_per_jiffy.

> > ndelay(400) 536E-09 -> 239E-09 seconds
> 
> I think the patched ndelay() count has wrapped your 16-bit timer. The 
> patched ndelay(400) asks __delay() for at least 466 ns of calibrated 
> loops whatever loops_per_jiffy is (8000 * loops_per_jiffy * 25 / 2^32 
> loops, plus the one __delay() adds), and it now does a mulu.l on every 
> call, which the old code did not (its arithmetic is plain C, so GCC can 
> hoist it out of your unrolled loop). If a call takes about 1.08 us, 
> 100000 calls take 107.6 ms, more than the 83.7 ms that T2 can count at 
> 783360 Hz, and what is left after one wrap is 23.9 ms, or 239 ns a call.
> 
> Could you try ndelay(400) with 40000 invocations instead of 100000?

Here's what I measured with interrupts disabled:

test_udelay(10)	     11.0E-06 -> 11.7E-06
test_udelay(20)	     19.7E-06 -> 21.5E-06
test_udelay(50)	     47.0E-06 -> 50.8E-06
test_udelay(100)     92.3E-06 -> 99.7E-06
test_udelay(1000)     973E-06 ->  979E-06
test_ndelay(40)	      191E-09 -> 17.0E-09
test_ndelay(400)      673E-09 -> 1.49E-06
test_ndelay(4000)    6.03E-06 -> 5.58E-06
test_ndelay(40000)   38.7E-06 -> 46.5E-06
test_ndelay(400000)   366E-06 ->  456E-06

ndelay(400) and ndelay(40) seem to be anomalous. I don't know why.

> If I'm right it should come out at about 1.08 us. That would mean it is 
> longer than before, not shorter. As the commit message says, ndelay() 
> now pays for the 64-bit multiply, so very short ndelay()s cost more. If 
> that cost matters on the 040, I could keep the old ndelay() and only fix 
> udelay().
> 
> Your udelay(1000) at 978 us is still 2 % short with interrupts off. I 
> believe that is calibrate_delay() running with the tick enabled, so the 
> tick's time is counted as loop time. That is separate from this patch, 
> and I have left it alone.
> 

Here's the same thing, but interrupts were enabled this time:

test_udelay(10)      11.4E-06 -> 12.2E-06
test_udelay(20)      20.5E-06 -> 22.3E-06
test_udelay(50)      48.8E-06 -> 52.7E-06
test_udelay(100)     96.1E-06 ->  103E-06
test_udelay(1000)    1.01E-03 -> 1.02E-03
test_ndelay(40)      77.5E-09 -> 57.1E-09
test_ndelay(400)      776E-09 -> 1.55E-06
test_ndelay(4000)    6.23E-06 -> 5.77E-06
test_ndelay(40000)   40.7E-06 -> 48.5E-06
test_ndelay(400000)   384E-06 ->  476E-06

In general, delays are about 4% longer when interrupts are enabled. I 
think that's just the overhead associated with a bunch of VIA chip 
accesses every 10 ms. Other platforms should fare better.

---

diff --git a/arch/m68k/mac/config.c b/arch/m68k/mac/config.c
index b745cf20a71f..66e80aa230ff 100644
--- a/arch/m68k/mac/config.c
+++ b/arch/m68k/mac/config.c
@@ -65,6 +65,83 @@ static void __init mac_sched_init(void)
 	via_init_clock();
 }
 
+#define GET_VIA1_TIMER2() \
+({ \
+	unsigned long flags; \
+	local_irq_save(flags); \
+	u8 high = via1[vT2CH]; \
+	u8 low = via1[vT2CL]; \
+	torn = via1[vT2CH] != high; \
+	local_irq_restore(flags); \
+	1 + low + (high << 8); \
+})
+
+#define DELAY_LOOP(n, arg) \
+do { \
+	int d, i, I = n / 40; \
+	u16 t1, t2; \
+	bool torn; \
+	do { t1 = GET_VIA1_TIMER2(); } while (torn); \
+	for (i = 0; i < I; ++i) { \
+		arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+		arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+		arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+		arg; arg; arg; arg; arg; arg; arg; arg; arg; arg; \
+	} \
+	t2 = GET_VIA1_TIMER2(); \
+	d = t1 - t2; \
+	if (d < 0) d += 0x10000; \
+	if (torn) pr_info("torn\n"); \
+	else pr_info("%10d, %20s, %10d\n", I * 40 , #arg, d); \
+} while (0)
+
+static inline void test_udelay(unsigned long t)
+{
+	volatile unsigned long vt = t;
+
+	udelay(vt);
+}
+
+static inline void test_ndelay(unsigned long t)
+{
+	volatile unsigned long vt = t;
+
+	ndelay(vt);
+}
+
+static void measure_delays(void)
+{
+	// Given ECLK is 783360 Hz, the 16 bit counter can measure up to 80 ms.
+	// Loops of 40ms of udelay() calls:
+	DELAY_LOOP(4000, test_udelay(10));
+	DELAY_LOOP(2000, test_udelay(20));
+	DELAY_LOOP( 800, test_udelay(50));
+	DELAY_LOOP( 400, test_udelay(100));
+	DELAY_LOOP(  40, test_udelay(1000));
+	// Loops of 16ms of ndelay() calls:
+	DELAY_LOOP(400000, test_ndelay(40));
+	DELAY_LOOP( 40000, test_ndelay(400));
+	DELAY_LOOP(  4000, test_ndelay(4000));
+	DELAY_LOOP(   400, test_ndelay(40000));
+	DELAY_LOOP(    40, test_ndelay(400000));
+}
+
+static int __init measure_delays_init(void)
+{
+	unsigned long flags;
+
+	via1[vACR] &= ~0x20; // free running T2 with no port B output
+
+	local_irq_save(flags);
+	measure_delays();
+	local_irq_restore(flags);
+	measure_delays();
+
+	return 0;
+}
+
+subsys_initcall(measure_delays_init);
+
 /*
  * Parse a Macintosh-specific record in the bootinfo
  */

      reply	other threads:[~2026-11-20  4:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 14:38 Graeme Harker
2026-11-18  9:52 ` Finn Thain
2026-11-18 21:00   ` Graeme Harker
2026-11-20  4:34     ` 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=5783b448-b34a-58a5-0bc0-98b1a806f182@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®