blob: 0f49ec67c98efad79e6c01923c7ec1415af70a0a [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbina4217912013-04-29 22:31:51 -05002#ifndef TIMER_H
3#define TIMER_H
4
Julius Werner51325702018-11-02 14:48:24 -07005#include <types.h>
6
Lin Huangda6b1bc2017-11-07 15:38:24 +08007#define NSECS_PER_SEC 1000000000
Aaron Durbina4217912013-04-29 22:31:51 -05008#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
19struct mono_time {
Rob Barnesb11f9f72022-06-24 10:28:31 -060020 uint64_t microseconds;
Aaron Durbina4217912013-04-29 22:31:51 -050021};
22
Aaron Durbin340ca912013-04-30 09:58:12 -050023/* 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. */
27struct 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 Durbina4217912013-04-29 22:31:51 -050034/* 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. */
43void timer_monotonic_get(struct mono_time *mt);
44
Aaron Durbin340ca912013-04-30 09:58:12 -050045/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
46int 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 Barnesd522f382022-09-12 06:31:47 -060050int timer_sched_callback(struct timeout_callback *tocb, uint64_t us);
Aaron Durbin340ca912013-04-30 09:58:12 -050051
Gabe Black6ccc45d2013-08-09 00:48:06 -070052/* Set an absolute time to a number of microseconds. */
Rob Barnesd522f382022-09-12 06:31:47 -060053static inline void mono_time_set_usecs(struct mono_time *mt, uint64_t us)
Gabe Black6ccc45d2013-08-09 00:48:06 -070054{
55 mt->microseconds = us;
56}
57
58/* Set an absolute time to a number of milliseconds. */
Rob Barnesd522f382022-09-12 06:31:47 -060059static inline void mono_time_set_msecs(struct mono_time *mt, uint64_t ms)
Gabe Black6ccc45d2013-08-09 00:48:06 -070060{
61 mt->microseconds = ms * USECS_PER_MSEC;
62}
63
Martin Roth0cb07e32013-07-09 21:46:01 -060064/* Add microseconds to an absolute time. */
Rob Barnesd522f382022-09-12 06:31:47 -060065static inline void mono_time_add_usecs(struct mono_time *mt, int64_t us)
Aaron Durbina4217912013-04-29 22:31:51 -050066{
67 mt->microseconds += us;
68}
69
Martin Roth0cb07e32013-07-09 21:46:01 -060070/* Add milliseconds to an absolute time. */
Rob Barnesd522f382022-09-12 06:31:47 -060071static inline void mono_time_add_msecs(struct mono_time *mt, int64_t ms)
Aaron Durbina4217912013-04-29 22:31:51 -050072{
73 mono_time_add_usecs(mt, ms * USECS_PER_MSEC);
74}
75
Martin Roth0cb07e32013-07-09 21:46:01 -060076/* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
Aaron Durbina4217912013-04-29 22:31:51 -050077 * respectively. */
78static inline int mono_time_cmp(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -080079 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -050080{
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 Durbina4217912013-04-29 22:31:51 -050090/* Return true if t1 after t2 */
91static inline int mono_time_after(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -080092 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -050093{
94 return mono_time_cmp(t1, t2) > 0;
95}
96
97/* Return true if t1 before t2. */
98static inline int mono_time_before(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -080099 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -0500100{
101 return mono_time_cmp(t1, t2) < 0;
102}
103
Aaron Durbina7a39172014-09-24 09:56:18 -0500104/* Return time difference between t1 and t2. i.e. t2 - t1. */
Rob Barnesd522f382022-09-12 06:31:47 -0600105static inline int64_t mono_time_diff_microseconds(const struct mono_time *t1,
106 const struct mono_time *t2)
David Hendricks040f25b2013-05-03 12:28:11 -0700107{
Aaron Durbina7a39172014-09-24 09:56:18 -0500108 return t2->microseconds - t1->microseconds;
David Hendricks040f25b2013-05-03 12:28:11 -0700109}
110
Aaron Durbinf65153e2014-09-23 16:28:43 -0500111struct stopwatch {
112 struct mono_time start;
113 struct mono_time current;
114 struct mono_time expires;
115};
116
117static inline void stopwatch_init(struct stopwatch *sw)
118{
Julius Wernercd49cce2019-03-05 16:53:33 -0800119 if (CONFIG(HAVE_MONOTONIC_TIMER))
Paul Menzelbe706462015-10-20 22:27:05 +0200120 timer_monotonic_get(&sw->start);
121 else
122 sw->start.microseconds = 0;
123
Aaron Durbinf65153e2014-09-23 16:28:43 -0500124 sw->current = sw->expires = sw->start;
125}
126
Rob Barnesd522f382022-09-12 06:31:47 -0600127static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, uint64_t us)
Aaron Durbinf65153e2014-09-23 16:28:43 -0500128{
129 stopwatch_init(sw);
130 mono_time_add_usecs(&sw->expires, us);
131}
132
Rob Barnesd522f382022-09-12 06:31:47 -0600133static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, uint64_t ms)
Aaron Durbinf65153e2014-09-23 16:28:43 -0500134{
135 stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms);
136}
137
138/*
139 * Tick the stopwatch to collect the current time.
140 */
141static inline void stopwatch_tick(struct stopwatch *sw)
142{
Julius Wernercd49cce2019-03-05 16:53:33 -0800143 if (CONFIG(HAVE_MONOTONIC_TIMER))
Paul Menzelbe706462015-10-20 22:27:05 +0200144 timer_monotonic_get(&sw->current);
145 else
146 sw->current.microseconds = 0;
Aaron Durbinf65153e2014-09-23 16:28:43 -0500147}
148
149/*
Elyes HAOUAS5f73e222020-01-15 21:13:45 +0100150 * Tick and check the stopwatch for expiration. Returns non-zero on expiration.
Aaron Durbinf65153e2014-09-23 16:28:43 -0500151 */
152static 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äfer109ba282017-09-19 15:19:53 +0200159 * Tick and check the stopwatch as long as it has not expired.
160 */
161static inline void stopwatch_wait_until_expired(struct stopwatch *sw)
162{
163 while (!stopwatch_expired(sw))
164 ;
165}
166
167/*
Aaron Durbinf65153e2014-09-23 16:28:43 -0500168 * Return number of microseconds since starting the stopwatch.
169 */
Rob Barnesd522f382022-09-12 06:31:47 -0600170static inline int64_t stopwatch_duration_usecs(struct stopwatch *sw)
Aaron Durbinf65153e2014-09-23 16:28:43 -0500171{
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 Barnesd522f382022-09-12 06:31:47 -0600182static inline int64_t stopwatch_duration_msecs(struct stopwatch *sw)
Aaron Durbinf65153e2014-09-23 16:28:43 -0500183{
184 return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
185}
186
Julius Werner51325702018-11-02 14:48:24 -0700187/*
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 Barnesd522f382022-09-12 06:31:47 -0600200 int64_t __ret = 0; \
Julius Werner51325702018-11-02 14:48:24 -0700201 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 Durbina4217912013-04-29 22:31:51 -0500219#endif /* TIMER_H */