Skip to contentSkip to conversation

m68k: delay: Fix rounding of short udelay() and ndelay()

PATCH In review Graeme Harker 2 participants 6 messages Nov 13 – Nov 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 m68k: delay: Fix rounding of short udelay() and ndelay()

__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.

Assisted-by Claude:claude-opus-5.5
Assisted-by Claude:claude-fable-5.2

---
The 68030 is a homebrew board whose support is not upstream. The timings above were taken on v7.3-rc1 with the multiply written as xloops * (loops_per_jiffy * HZ). The form sent here, with the factor of 4 moved to xloops, gives the same loop count whenever neither product overflows (compared on the host for loops_per_jiffy from 16896 to 170000000 and every delay the table uses); it has been booted under QEMU (virt, 770 BogoMIPS) but the table has not been re-measured with it.

Without the factor of 4 the product would overflow above 8590 BogoMIPS, which a fast QEMU host is within reach of; the old code had no such limit in practice.

Build-tested on m68k master (v7.4-rc2) with multi_defconfig, sun3_defconfig, virt_defconfig and m5208evb_defconfig (the CPU_HAS_NO_MULDIV64 side).

 arch/m68k/include/asm/delay.h | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
arch/m68k/include/asm/delay.h +24 -2
index 751712f8beea..2d6b18a23f4e 100644
@@ -57,14 +57,24 @@ extern void __bad_udelay(void);
 
 #else
 
+/*
+ * xloops is the delay in units of 2**-32 s.  Multiply by loops per second
+ * and keep the high word, so the result is truncated by less than one loop;
+ * __delay() runs one more loop than asked, so the delay is never short.
+ * Scaling by loops_per_jiffy first and by HZ afterwards would truncate in
+ * steps of HZ loops, turning any delay below HZ loops into __delay(0).
+ *
+ * As on x86, a factor of 4 is moved from loops per second to xloops, so
+ * that loops_per_jiffy * HZ may be up to 2**34.
+ */
 static inline void __xdelay(unsigned long xloops)
 {
 	unsigned long tmp;
 
 	__asm__ ("mulul %2,%0:%1"
 		: "=d" (xloops), "=d" (tmp)
-		: "d" (xloops), "1" (loops_per_jiffy));
-	__delay(xloops * HZ);
+		: "d" (xloops * 4), "1" (loops_per_jiffy * (HZ / 4)));
+	__delay(xloops);
 }
 
 /*
@@ -113,6 +123,7 @@ static inline void __udelay(unsigned long usecs)
  */
 #define	HZSCALE		(268435456 / (1000000 / HZ))
 
+#ifdef CONFIG_CPU_HAS_NO_MULDIV64
 static inline void ndelay(unsigned long nsec)
 {
 	__delay(DIV_ROUND_UP(nsec *
@@ -120,6 +131,17 @@ static inline void ndelay(unsigned long nsec)
 			       (loops_per_jiffy >> 11)) >> 6),
 			     1000));
 }
+#else
+/*
+ * The shifts above round loops_per_jiffy down to a multiple of 2048, which
+ * on a slow CPU loses most of it.  With a 64-bit multiply, use __xdelay():
+ * 5 is 2**32 / 10**9 rounded up, as on x86.
+ */
+static inline void ndelay(unsigned long nsec)
+{
+	__xdelay(nsec * 5);
+}
+#endif
 #define ndelay(n) ndelay(n)
 
 #endif /* defined(_M68K_DELAY_H) */

base-commit: 8c9ddebb300c719362a52a2d465f44e7d055778d
prerequisite-patch-id: d8f836f1e047af08e6fff01d411b44e442086214

Signature · 1 line
2.53.0
#1 lore raw
Quoted 28 lines · Graeme Harker

__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
Quoted 5 lines

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.

---

arch/m68k/mac/config.c +48 -0
index b745cf20a71f..bc1da6ee014a 100644
@@ -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
  */
#2 lore raw

Hi Finn,

Thanks for testing it on real hardware, and with a better method than mine.

Quoted 1 line

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.

Quoted 1 line

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? 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.

Graeme

#3 lore raw
Quoted 9 lines · Graeme Harker
Quoted 2 lines

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.

Quoted 11 lines
Quoted 1 line

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.

Quoted 1 line

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.

Quoted 11 lines

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.

---

arch/m68k/mac/config.c +77 -0
index b745cf20a71f..66e80aa230ff 100644
@@ -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
  */
#4 lore raw

Hi Finn,

Thanks for running it again, and with both interrupt settings.

Quoted 1 line

test_ndelay(40) 191E-09 -> 17.0E-09

I think this one has wrapped the timer as well. 400000 calls only fit in T2's 83.7 ms if each takes less than 209 ns, and 17 ns is less than one clock on the LC040. With one wrap it would be 226 ns, which is longer than before, as ndelay(400) now is. 40000 calls should show it.

Quoted 2 lines

test_ndelay(40000) 38.7E-06 -> 46.5E-06
test_ndelay(400000) 366E-06 -> 456E-06

These show a real problem with the patch: long ndelay()s now run about 15% long. The 5 I took from x86 is 2^32 / 10^9 rounded up, but 2^32 / 10^9 is 4.295, so every ndelay() asks for 16.4% more loops than it should. Combined with your udelay(1000) of 979 us (2% short), that predicts 453 us for ndelay(400000), and you measured 456 us. The old code was short instead, from rounding loops_per_jiffy down to a multiple of 2048.

I can't explain the old ndelay(4000) of 6.03 us without knowing loops_per_jiffy. What does your machine calibrate to?

I can see three ways to go, and I'd like to know which you and Geert would prefer:

1. Keep 5, as x86 does. Delays are never short, but up to 16% long.

2. Use a closer constant, e.g. __xdelay(nsec * 4 + ((nsec * 1209) >> 12)), which is 4.29517, within 0.005% of 2^32 / 10^9 and still never short. It costs another multiply and a shift on every call, and the second term overflows for delays above 3.5 ms, which ndelay() should not be asked for anyway.

3. Leave ndelay() as it is and fix only udelay().

Option 2 seems the most accurate, but ndelay() is mostly used for short delays, and for those the call overhead you measured matters more than the constant.

Graeme

#5 lore raw
Quoted 7 lines · Graeme Harker
Quoted 1 line

test_ndelay(40) 191E-09 -> 17.0E-09

I think this one has wrapped the timer as well. 400000 calls only fit in T2's 83.7 ms if each takes less than 209 ns, and 17 ns is less than one clock on the LC040. With one wrap it would be 226 ns, which is longer than before, as ndelay(400) now is. 40000 calls should show it.

Well, the problem with 40 ns x 40000 calls (or 1.6 ms) is that such a short interval will usually encounter no tick interrupt, and the measurement would be erratic (if interrupts were enabled).

The measurements below were made with interrupts disabled so add 4% if you want to account for the tick interrupt.

test_udelay(10)	    10.8E-06  ->  11.8E-06
test_udelay(20)	    19.7E-06  ->  21.5E-06
test_udelay(40)	    37.8E-06  ->  41.1E-06
test_udelay(100)    92.3E-06  ->  99.7E-06
test_udelay(200)     192E-06  ->   198E-06
test_udelay(400)     392E-06  ->   393E-06
test_udelay(1000)    973E-06  ->   980E-06
test_ndelay(20)	     401E-09  ->   612E-09
test_ndelay(40)	     401E-09  ->  1.06E-06
test_ndelay(100)    2.49E-06  ->  1.17E-06
test_ndelay(200)    2.55E-06  ->  1.22E-06
test_ndelay(400)    2.75E-06  ->  1.50E-06
test_ndelay(1000)   3.30E-06  ->  2.14E-06
test_ndelay(2000)   4.24E-06  ->  3.33E-06
test_ndelay(4000)   6.07E-06  ->  5.63E-06
test_ndelay(10000)  11.6E-06  ->  12.5E-06
test_ndelay(20000)  20.8E-06  ->  23.9E-06
test_ndelay(40000)  39.2E-06  ->  46.9E-06
Quoted 6 lines
Quoted 2 lines

test_ndelay(40000) 38.7E-06 -> 46.5E-06
test_ndelay(400000) 366E-06 -> 456E-06

These show a real problem with the patch: long ndelay()s now run about 15% long.

Quoted 9 lines

The 5 I took from x86 is 2^32 / 10^9 rounded up, but 2^32 / 10^9 is 4.295, so every ndelay() asks for 16.4% more loops than it should. Combined with your udelay(1000) of 979 us (2% short), that predicts 453 us for ndelay(400000), and you measured 456 us. The old code was short instead, from rounding loops_per_jiffy down to a multiple of 2048.

I can't explain the old ndelay(4000) of 6.03 us without knowing loops_per_jiffy. What does your machine calibrate to?

It logs "Calibrating delay loop... 21.55 BogoMIPS lpj=107776)".

Quoted 11 lines

I can see three ways to go, and I'd like to know which you and Geert would prefer:

1. Keep 5, as x86 does. Delays are never short, but up to 16% long.

2. Use a closer constant, e.g. __xdelay(nsec * 4 + ((nsec * 1209) >> 12)), which is 4.29517, within 0.005% of 2^32 / 10^9 and still never short. It costs another multiply and a shift on every call, and the second term overflows for delays above 3.5 ms, which ndelay() should not be asked for anyway.

Quoted 6 lines

3. Leave ndelay() as it is and fix only udelay().

Option 2 seems the most accurate, but ndelay() is mostly used for short delays, and for those the call overhead you measured matters more than the constant.

I note that the API documentation warns callers that "ndelay(), udelay() and mdelay() may return early for several reasons". So I think I'll defer to the arch maintainers on this one.

---

arch/m68k/mac/config.c +84 -0
index b745cf20a71f..9e3547698619 100644
@@ -65,6 +65,90 @@ 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(1000, test_udelay(40));
+	DELAY_LOOP( 400, test_udelay(100));
+	DELAY_LOOP( 200, test_udelay(200));
+	DELAY_LOOP( 100, test_udelay(400));
+	DELAY_LOOP(  40, test_udelay(1000));
+	// Loops of 1.6ms of ndelay() calls:
+	DELAY_LOOP(80000, test_ndelay(20));
+	DELAY_LOOP(40000, test_ndelay(40));
+	DELAY_LOOP(16000, test_ndelay(100));
+	DELAY_LOOP( 8000, test_ndelay(200));
+	DELAY_LOOP( 4000, test_ndelay(400));
+	DELAY_LOOP( 1600, test_ndelay(1000));
+	DELAY_LOOP(  800, test_ndelay(2000));
+	DELAY_LOOP(  400, test_ndelay(4000));
+	DELAY_LOOP(  160, test_ndelay(10000));
+	DELAY_LOOP(   80, test_ndelay(20000));
+	DELAY_LOOP(   40, test_ndelay(40000));
+}
+
+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);
+
+	return 0;
+}
+
+subsys_initcall(measure_delays_init);
+
 /*
  * Parse a Macintosh-specific record in the bootinfo
  */