summaryrefslogtreecommitdiffstats
path: root/udelay.c
diff options
context:
space:
mode:
authorNico Huber <nico.h@gmx.de>2012-11-05 21:46:33 +0100
committerNico Huber <nico.h@gmx.de>2017-06-22 11:14:15 +0000
commit8624e8cfa88ebd17ecf3bfd55c8dc1a799f47573 (patch)
treef61a16e60f193b5e3f13a377a81d1308b1b4f0ad /udelay.c
parent2d62572d1dd0c37eb626fd8faa17b26690f20b15 (diff)
downloadflashrom-8624e8cfa88ebd17ecf3bfd55c8dc1a799f47573.tar.gz
flashrom-8624e8cfa88ebd17ecf3bfd55c8dc1a799f47573.tar.bz2
flashrom-8624e8cfa88ebd17ecf3bfd55c8dc1a799f47573.zip
udelay: Use clock_gettime() if available and precise
Instead of calibrating our busy loop against a coarse clock, check if a precise clock is available and loop against that. The former is unre- liable by definition on any modern system that may dynamically reclock the processor. v2: Apparently _POSIX_MONOTONIC_CLOCK being defined only means that the library knows about CLOCK_MONOTONIC. So check for its support at runtime and fall back to CLOCK_REALTIME if it's missing. TEST=Manually added a 10s loop and compared to real time. Run on Linux RPi3, Linux x86 and my original use case Linux in VirtualBox (Linux host). Change-Id: I85ad359823875237ada9cd027af3017d62e9a235 Signed-off-by: Nico Huber <nico.h@gmx.de> Reviewed-on: https://review.coreboot.org/19391 Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org> Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Reviewed-by: David Hendricks <david.hendricks@gmail.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'udelay.c')
-rw-r--r--udelay.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/udelay.c b/udelay.c
index 7c6961d13..3a44c1659 100644
--- a/udelay.c
+++ b/udelay.c
@@ -21,13 +21,64 @@
#ifndef __LIBPAYLOAD__
+#include <stdbool.h>
#include <unistd.h>
+#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <limits.h>
#include "flash.h"
+static bool use_clock_gettime = false;
+
+#if HAVE_CLOCK_GETTIME == 1
+
+#ifdef _POSIX_MONOTONIC_CLOCK
+static clockid_t clock_id = CLOCK_MONOTONIC;
+#else
+static clockid_t clock_id = CLOCK_REALTIME;
+#endif
+
+static void clock_usec_delay(int usecs)
+{
+ struct timespec now;
+ clock_gettime(clock_id, &now);
+
+ const long end_nsec = now.tv_nsec + usecs * 1000L;
+ const struct timespec end = {
+ end_nsec / (1000 * 1000 * 1000) + now.tv_sec,
+ end_nsec % (1000 * 1000 * 1000)
+ };
+ do {
+ clock_gettime(clock_id, &now);
+ } while (now.tv_sec < end.tv_sec || (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
+}
+
+static int clock_check_res(void)
+{
+ struct timespec res;
+ if (!clock_getres(clock_id, &res)) {
+ if (res.tv_sec == 0 && res.tv_nsec <= 100) {
+ msg_pinfo("Using clock_gettime for delay loops (clk_id: %d, resolution: %ldns).\n",
+ (int)clock_id, res.tv_nsec);
+ use_clock_gettime = true;
+ return 1;
+ }
+ } else if (clock_id != CLOCK_REALTIME && errno == EINVAL) {
+ /* Try again with CLOCK_REALTIME. */
+ clock_id = CLOCK_REALTIME;
+ return clock_check_res();
+ }
+ return 0;
+}
+#else
+
+static inline void clock_usec_delay(int usecs) {}
+static inline int clock_check_res(void) { return 0; }
+
+#endif /* HAVE_CLOCK_GETTIME == 1 */
+
/* loops per microsecond */
static unsigned long micro = 1;
@@ -87,6 +138,9 @@ static unsigned long measure_delay(unsigned int usecs)
void myusec_calibrate_delay(void)
{
+ if (clock_check_res())
+ return;
+
unsigned long count = 1000;
unsigned long timeusec, resolution;
int i, tries = 0;
@@ -189,6 +243,8 @@ void internal_delay(unsigned int usecs)
/* If the delay is >1 s, use internal_sleep because timing does not need to be so precise. */
if (usecs > 1000000) {
internal_sleep(usecs);
+ } else if (use_clock_gettime) {
+ clock_usec_delay(usecs);
} else {
myusec_delay(usecs);
}