Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 2 | #ifndef TIMER_H |
| 3 | #define TIMER_H |
| 4 | |
Julius Werner | 5132570 | 2018-11-02 14:48:24 -0700 | [diff] [blame] | 5 | #include <types.h> |
| 6 | |
Lin Huang | da6b1bc | 2017-11-07 15:38:24 +0800 | [diff] [blame] | 7 | #define NSECS_PER_SEC 1000000000 |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 8 | #define USECS_PER_SEC 1000000 |
| 9 | #define MSECS_PER_SEC 1000 |
| 10 | #define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC) |
| 11 | |
| 12 | /* The time structures are defined to be a representation of the time since |
| 13 | * coreboot started executing one of its stages. The reason for using structures |
| 14 | * is to allow for changes in the future. The structures' details are exposed |
| 15 | * so that the compiler can allocate space on the stack and use in other |
| 16 | * structures. In other words, accessing any field within this structure |
| 17 | * outside of the core timer code is not supported. */ |
| 18 | |
| 19 | struct mono_time { |
Rob Barnes | b11f9f7 | 2022-06-24 10:28:31 -0600 | [diff] [blame] | 20 | uint64_t microseconds; |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 21 | }; |
| 22 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 23 | /* A timeout_callback structure is used for the book keeping for scheduling |
| 24 | * work in the future. When a callback is called the structure can be |
| 25 | * re-used for scheduling as it is not being tracked by the core timer |
| 26 | * library any more. */ |
| 27 | struct timeout_callback { |
| 28 | void *priv; |
| 29 | void (*callback)(struct timeout_callback *tocb); |
| 30 | /* Not for public use. The timer library uses the fields below. */ |
| 31 | struct mono_time expiration; |
| 32 | }; |
| 33 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 34 | /* Obtain the current monotonic time. The assumption is that the time counts |
| 35 | * up from the value 0 with value 0 being the point when the timer was |
| 36 | * initialized. Additionally, the timer is assumed to only be valid for the |
| 37 | * duration of the boot. |
| 38 | * |
| 39 | * Note that any implementations of timer_monotonic_get() |
| 40 | * need to ensure its timesource does not roll over within 10 secs. The reason |
| 41 | * is that the time between calls to timer_monotonic_get() may be on order |
| 42 | * of 10 seconds. */ |
| 43 | void timer_monotonic_get(struct mono_time *mt); |
| 44 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 45 | /* Returns 1 if callbacks still present in the queue. 0 if no timers left. */ |
| 46 | int timers_run(void); |
| 47 | |
| 48 | /* Schedule a callback to be ran microseconds from time of invocation. |
| 49 | * 0 returned on success, < 0 on error. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 50 | int timer_sched_callback(struct timeout_callback *tocb, uint64_t us); |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 51 | |
Gabe Black | 6ccc45d | 2013-08-09 00:48:06 -0700 | [diff] [blame] | 52 | /* Set an absolute time to a number of microseconds. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 53 | static inline void mono_time_set_usecs(struct mono_time *mt, uint64_t us) |
Gabe Black | 6ccc45d | 2013-08-09 00:48:06 -0700 | [diff] [blame] | 54 | { |
| 55 | mt->microseconds = us; |
| 56 | } |
| 57 | |
| 58 | /* Set an absolute time to a number of milliseconds. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 59 | static inline void mono_time_set_msecs(struct mono_time *mt, uint64_t ms) |
Gabe Black | 6ccc45d | 2013-08-09 00:48:06 -0700 | [diff] [blame] | 60 | { |
| 61 | mt->microseconds = ms * USECS_PER_MSEC; |
| 62 | } |
| 63 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 64 | /* Add microseconds to an absolute time. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 65 | static inline void mono_time_add_usecs(struct mono_time *mt, int64_t us) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 66 | { |
| 67 | mt->microseconds += us; |
| 68 | } |
| 69 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 70 | /* Add milliseconds to an absolute time. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 71 | static inline void mono_time_add_msecs(struct mono_time *mt, int64_t ms) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 72 | { |
| 73 | mono_time_add_usecs(mt, ms * USECS_PER_MSEC); |
| 74 | } |
| 75 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 76 | /* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2, |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 77 | * respectively. */ |
| 78 | static inline int mono_time_cmp(const struct mono_time *t1, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 79 | const struct mono_time *t2) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 80 | { |
| 81 | if (t1->microseconds == t2->microseconds) |
| 82 | return 0; |
| 83 | |
| 84 | if (t1->microseconds < t2->microseconds) |
| 85 | return -1; |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 90 | /* Return true if t1 after t2 */ |
| 91 | static inline int mono_time_after(const struct mono_time *t1, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 92 | const struct mono_time *t2) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 93 | { |
| 94 | return mono_time_cmp(t1, t2) > 0; |
| 95 | } |
| 96 | |
| 97 | /* Return true if t1 before t2. */ |
| 98 | static inline int mono_time_before(const struct mono_time *t1, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 99 | const struct mono_time *t2) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 100 | { |
| 101 | return mono_time_cmp(t1, t2) < 0; |
| 102 | } |
| 103 | |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 104 | /* Return time difference between t1 and t2. i.e. t2 - t1. */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 105 | static inline int64_t mono_time_diff_microseconds(const struct mono_time *t1, |
| 106 | const struct mono_time *t2) |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 107 | { |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 108 | return t2->microseconds - t1->microseconds; |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 111 | struct stopwatch { |
| 112 | struct mono_time start; |
| 113 | struct mono_time current; |
| 114 | struct mono_time expires; |
| 115 | }; |
| 116 | |
| 117 | static inline void stopwatch_init(struct stopwatch *sw) |
| 118 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 119 | if (CONFIG(HAVE_MONOTONIC_TIMER)) |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame] | 120 | timer_monotonic_get(&sw->start); |
| 121 | else |
| 122 | sw->start.microseconds = 0; |
| 123 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 124 | sw->current = sw->expires = sw->start; |
| 125 | } |
| 126 | |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 127 | static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, uint64_t us) |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 128 | { |
| 129 | stopwatch_init(sw); |
| 130 | mono_time_add_usecs(&sw->expires, us); |
| 131 | } |
| 132 | |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 133 | static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, uint64_t ms) |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 134 | { |
| 135 | stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Tick the stopwatch to collect the current time. |
| 140 | */ |
| 141 | static inline void stopwatch_tick(struct stopwatch *sw) |
| 142 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 143 | if (CONFIG(HAVE_MONOTONIC_TIMER)) |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame] | 144 | timer_monotonic_get(&sw->current); |
| 145 | else |
| 146 | sw->current.microseconds = 0; |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
Elyes HAOUAS | 5f73e22 | 2020-01-15 21:13:45 +0100 | [diff] [blame] | 150 | * Tick and check the stopwatch for expiration. Returns non-zero on expiration. |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 151 | */ |
| 152 | static inline int stopwatch_expired(struct stopwatch *sw) |
| 153 | { |
| 154 | stopwatch_tick(sw); |
| 155 | return !mono_time_before(&sw->current, &sw->expires); |
| 156 | } |
| 157 | |
| 158 | /* |
Jonathan Neuschäfer | 109ba28 | 2017-09-19 15:19:53 +0200 | [diff] [blame] | 159 | * Tick and check the stopwatch as long as it has not expired. |
| 160 | */ |
| 161 | static inline void stopwatch_wait_until_expired(struct stopwatch *sw) |
| 162 | { |
| 163 | while (!stopwatch_expired(sw)) |
| 164 | ; |
| 165 | } |
| 166 | |
| 167 | /* |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 168 | * Return number of microseconds since starting the stopwatch. |
| 169 | */ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 170 | static inline int64_t stopwatch_duration_usecs(struct stopwatch *sw) |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 171 | { |
| 172 | /* |
| 173 | * If the stopwatch hasn't been ticked (current == start) tick |
| 174 | * the stopwatch to gather the accumulated time. |
| 175 | */ |
| 176 | if (!mono_time_cmp(&sw->start, &sw->current)) |
| 177 | stopwatch_tick(sw); |
| 178 | |
| 179 | return mono_time_diff_microseconds(&sw->start, &sw->current); |
| 180 | } |
| 181 | |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 182 | static inline int64_t stopwatch_duration_msecs(struct stopwatch *sw) |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 183 | { |
| 184 | return stopwatch_duration_usecs(sw) / USECS_PER_MSEC; |
| 185 | } |
| 186 | |
Julius Werner | 5132570 | 2018-11-02 14:48:24 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Helper macro to wait until a condition becomes true or a timeout elapses. |
| 189 | * |
| 190 | * condition: a C expression to wait for |
| 191 | * timeout: timeout, in microseconds |
| 192 | * |
| 193 | * Returns: |
| 194 | * 0 if the condition still evaluates to false after the timeout elapsed, |
| 195 | * >0 if the condition evaluates to true. The return value is the amount of |
| 196 | * microseconds waited (at least 1). |
| 197 | */ |
| 198 | #define wait_us(timeout_us, condition) \ |
| 199 | ({ \ |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 200 | int64_t __ret = 0; \ |
Julius Werner | 5132570 | 2018-11-02 14:48:24 -0700 | [diff] [blame] | 201 | struct stopwatch __sw; \ |
| 202 | stopwatch_init_usecs_expire(&__sw, timeout_us); \ |
| 203 | do { \ |
| 204 | if (condition) { \ |
| 205 | stopwatch_tick(&__sw); \ |
| 206 | __ret = stopwatch_duration_usecs(&__sw); \ |
| 207 | if (!__ret) /* make sure it evaluates to true */\ |
| 208 | __ret = 1; \ |
| 209 | break; \ |
| 210 | } \ |
| 211 | } while (!stopwatch_expired(&__sw)); \ |
| 212 | __ret; \ |
| 213 | }) |
| 214 | |
| 215 | #define wait_ms(timeout_ms, condition) \ |
| 216 | DIV_ROUND_UP(wait_us((timeout_ms) * USECS_PER_MSEC, condition), \ |
| 217 | USECS_PER_MSEC) |
| 218 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 219 | #endif /* TIMER_H */ |