From: Marc Gonzalez <marc_gonzalez@sigmadesigns.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alan Cox <gnomes@lxorguk.ukuu.org.uk>,
LKML <linux-kernel@vger.kernel.org>,
Linux ARM <linux-arm-kernel@lists.infradead.org>,
Steven Rostedt <rostedt@goodmis.org>,
Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Peter Zijlstra <peterz@infradead.org>,
John Stultz <john.stultz@linaro.org>,
Douglas Anderson <dianders@chromium.org>,
Nicolas Pitre <nico@linaro.org>,
Mark Rutland <mark.rutland@arm.com>,
Will Deacon <will.deacon@arm.com>,
Jonathan Austin <jonathan.austin@arm.com>,
Arnd Bergmann <arnd@arndb.de>, Kevin Hilman <khilman@kernel.org>,
Russell King <linux@arm.linux.org.uk>,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@codeaurora.org>,
Boris Brezillon <boris.brezillon@free-electrons.com>,
Thibaud Cornic <thibaud_cornic@sigmadesigns.com>,
Mason <slash.tmp@free.fr>
Subject: Re: [RFC] Improving udelay/ndelay on platforms where that is possible
Date: Wed, 15 Nov 2017 13:51:54 +0100 [thread overview]
Message-ID: <11393e07-b042-180c-3bcd-484bf51eada6@sigmadesigns.com> (raw)
In-Reply-To: <e03436d1-b140-3f29-59a6-3b1eb6bf9da4@free.fr>
On 01/11/2017 20:38, Marc Gonzalez wrote:
> OK, I'll just send my patch, and then crawl back under my rock.
Linus,
As promised, the patch is provided below. And as promised, I will
no longer bring this up on LKML.
FWIW, I have checked that the computed value matches the expected
value for all HZ and delay_us, and for a few clock frequencies,
using the following program:
$ cat delays.c
#include <stdio.h>
#define MEGA 1000000u
typedef unsigned int uint;
typedef unsigned long long u64;
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
static const uint HZ_tab[] = { 100, 250, 300, 1000 };
static void check_cycle_count(uint freq, uint HZ, uint delay_us)
{
uint UDELAY_MULT = (2147 * HZ) + (483648 * HZ / MEGA);
uint lpj = DIV_ROUND_UP(freq, HZ);
uint computed = ((u64)lpj * delay_us * UDELAY_MULT >> 31) + 1;
uint expected = DIV_ROUND_UP((u64)delay_us * freq, MEGA);
if (computed != expected)
printf("freq=%u HZ=%u delay_us=%u comp=%u exp=%u\n", freq, HZ, delay_us, computed, expected);
}
int main(void)
{
uint idx, delay_us, freq;
for (freq = 3*MEGA; freq <= 100*MEGA; freq += 3*MEGA)
for (idx = 0; idx < sizeof HZ_tab / sizeof *HZ_tab; ++idx)
for (delay_us = 1; delay_us <= 2000; ++delay_us)
check_cycle_count(freq, HZ_tab[idx], delay_us);
return 0;
}
-- >8 --
Subject: [PATCH] ARM: Tweak clock-based udelay implementation
In 9f8197980d87a ("delay: Add explanation of udelay() inaccuracy")
Russell pointed out that loop-based delays may return early.
On the arm platform, delays may be either loop-based or clock-based.
This patch tweaks the clock-based implementation so that udelay(N)
is guaranteed to spin at least N microseconds.
Signed-off-by: Marc Gonzalez <marc_gonzalez@sigmadesigns.com>
---
arch/arm/lib/delay.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/arch/arm/lib/delay.c b/arch/arm/lib/delay.c
index 2cef11884857..0a25712077ec 100644
--- a/arch/arm/lib/delay.c
+++ b/arch/arm/lib/delay.c
@@ -58,15 +58,15 @@ static void __timer_delay(unsigned long cycles)
{
cycles_t start = get_cycles();
- while ((get_cycles() - start) < cycles)
+ while ((get_cycles() - start) <= cycles)
cpu_relax();
}
static void __timer_const_udelay(unsigned long xloops)
{
- unsigned long long loops = xloops;
- loops *= arm_delay_ops.ticks_per_jiffy;
- __timer_delay(loops >> UDELAY_SHIFT);
+ u64 tmp = (u64)xloops * arm_delay_ops.ticks_per_jiffy;
+ unsigned long cycles = tmp >> UDELAY_SHIFT;
+ __timer_delay(cycles + 1); /* Round up in 1 instruction */
}
static void __timer_udelay(unsigned long usecs)
@@ -92,7 +92,7 @@ void __init register_current_timer_delay(const struct delay_timer *timer)
if (!delay_calibrated && (!delay_res || (res < delay_res))) {
pr_info("Switching to timer-based delay loop, resolution %lluns\n", res);
delay_timer = timer;
- lpj_fine = timer->freq / HZ;
+ lpj_fine = DIV_ROUND_UP(timer->freq, HZ);
delay_res = res;
/* cpufreq may scale loops_per_jiffy, so keep a private copy */
--
2.15.0
next prev parent reply other threads:[~2017-11-15 12:53 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-31 16:15 [RFC] Improving udelay/ndelay on platforms where that is possible Marc Gonzalez
2017-10-31 16:44 ` Linus Torvalds
2017-10-31 16:56 ` Russell King - ARM Linux
2017-10-31 17:45 ` Linus Torvalds
2017-10-31 17:58 ` Linus Torvalds
2017-11-01 0:23 ` Doug Anderson
2017-11-01 9:26 ` Russell King - ARM Linux
2017-11-01 15:53 ` Doug Anderson
2017-12-07 12:31 ` Pavel Machek
2017-11-01 19:28 ` Marc Gonzalez
2017-11-01 20:30 ` Russell King - ARM Linux
2017-10-31 16:46 ` Russell King - ARM Linux
2017-11-01 17:53 ` Alan Cox
2017-11-01 19:03 ` Marc Gonzalez
2017-11-01 19:09 ` Linus Torvalds
2017-11-01 19:17 ` Linus Torvalds
2017-11-01 19:38 ` Marc Gonzalez
2017-11-15 12:51 ` Marc Gonzalez [this message]
2017-11-15 13:13 ` Russell King - ARM Linux
2017-11-16 15:26 ` Marc Gonzalez
2017-11-16 15:36 ` Russell King - ARM Linux
2017-11-16 15:47 ` Marc Gonzalez
2017-11-16 16:08 ` Nicolas Pitre
2017-11-16 16:26 ` Marc Gonzalez
2017-11-16 16:32 ` Russell King - ARM Linux
2017-11-16 16:42 ` Marc Gonzalez
2017-11-16 17:05 ` Russell King - ARM Linux
2017-11-16 21:05 ` Marc Gonzalez
2017-11-16 22:15 ` Doug Anderson
2017-11-16 23:22 ` Russell King - ARM Linux
2017-11-20 17:38 ` Doug Anderson
2017-11-20 18:31 ` Russell King - ARM Linux
2017-11-16 16:47 ` Nicolas Pitre
2017-11-16 16:51 ` Marc Gonzalez
2017-11-16 17:00 ` Nicolas Pitre
2017-12-07 12:43 ` Pavel Machek
2017-11-15 18:45 ` Doug Anderson
2017-11-01 19:36 ` Alan Cox
2017-11-01 19:39 ` Thomas Gleixner
2017-11-01 19:48 ` Baruch Siach
2017-11-02 16:12 ` Boris Brezillon
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=11393e07-b042-180c-3bcd-484bf51eada6@sigmadesigns.com \
--to=marc_gonzalez@sigmadesigns.com \
--cc=arnd@arndb.de \
--cc=boris.brezillon@free-electrons.com \
--cc=dianders@chromium.org \
--cc=gnomes@lxorguk.ukuu.org.uk \
--cc=john.stultz@linaro.org \
--cc=jonathan.austin@arm.com \
--cc=khilman@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=mark.rutland@arm.com \
--cc=mingo@kernel.org \
--cc=mturquette@baylibre.com \
--cc=nico@linaro.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=sboyd@codeaurora.org \
--cc=slash.tmp@free.fr \
--cc=tglx@linutronix.de \
--cc=thibaud_cornic@sigmadesigns.com \
--cc=torvalds@linux-foundation.org \
--cc=will.deacon@arm.com \
/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;
as well as URLs for NNTP newsgroup(s).