Debian Codegraph @

platform/udelay.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
6 * Copyright (C) 2024 Google LLC
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19 #ifndef __LIBPAYLOAD__
20
21 #include "platform/udelay.h"
22
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <stdlib.h>
29 #include <limits.h>
30 #include "flash.h"
31 #include "programmer.h"
32
33 #if HAVE_CLOCK_GETTIME == 1
34
35 static void clock_usec_delay(int usecs)
36 {
37 static clockid_t clock_id =
38 #ifdef _POSIX_MONOTONIC_CLOCK
39 CLOCK_MONOTONIC;
40 #else
41 CLOCK_REALTIME;
42 #endif
43
44 struct timespec now;
45 if (clock_gettime(clock_id, &now)) {
46 /* Fall back to realtime clock if monotonic doesn't work */
47 if (clock_id != CLOCK_REALTIME && errno == EINVAL) {
48 clock_id = CLOCK_REALTIME;
49 clock_gettime(clock_id, &now);
50 }
51 }
52
53 const long end_nsec = now.tv_nsec + usecs * 1000L;
54 const struct timespec end = {
55 end_nsec / (1000 * 1000 * 1000) + now.tv_sec,
56 end_nsec % (1000 * 1000 * 1000)
57 };
58 do {
59 clock_gettime(clock_id, &now);
60 } while (now.tv_sec < end.tv_sec || (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
61 }
62
63 #else
64
65 static void clock_usec_delay(unsigned int usecs) {
66 struct timeval start, end;
67 unsigned long elapsed = 0;
68
69 gettimeofday(&start, NULL);
70
71 while (elapsed < usecs) {
72 gettimeofday(&end, NULL);
73 elapsed = 1000000 * (end.tv_sec - start.tv_sec) +
74 (end.tv_usec - start.tv_usec);
75 /* Protect against time going forward too much. */
76 if ((end.tv_sec > start.tv_sec) &&
77 ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1))
78 elapsed = 0;
79 /* Protect against time going backwards during leap seconds. */
80 if ((end.tv_sec < start.tv_sec) || (elapsed > LONG_MAX))
81 elapsed = 0;
82 }
83 }
84
85 #endif /* HAVE_CLOCK_GETTIME == 1 */
86
87 /* Not very precise sleep. */
88 void internal_sleep(unsigned int usecs)
89 {
90 #if IS_WINDOWS
91 Sleep((usecs + 999) / 1000);
92 #else
93 nanosleep(&(struct timespec){usecs / 1000000, (usecs * 1000) % 1000000000UL}, NULL);
94 #endif
95 }
96
97 static const unsigned min_sleep = CONFIG_DELAY_MINIMUM_SLEEP_US;
98
99 /* Precise delay. */
100 void default_delay(unsigned int usecs)
101 {
102 if (usecs < min_sleep) {
103 clock_usec_delay(usecs);
104 } else {
105 internal_sleep(usecs);
106 }
107 }
108
109 #else
110 #include <libpayload.h>
111
112 void default_delay(unsigned int usecs)
113 {
114 udelay(usecs);
115 }
116 #endif