blob: d890c9146fb0b00559d0be42857744a6d1ffc13e [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbina4217912013-04-29 22:31:51 -05003#ifndef TIMER_H
4#define TIMER_H
5
Julius Werner51325702018-11-02 14:48:24 -07006#include <types.h>
7
Lin Huangda6b1bc2017-11-07 15:38:24 +08008#define NSECS_PER_SEC 1000000000
Aaron Durbina4217912013-04-29 22:31:51 -05009#define USECS_PER_SEC 1000000
10#define MSECS_PER_SEC 1000
11#define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC)
12
13/* The time structures are defined to be a representation of the time since
14 * coreboot started executing one of its stages. The reason for using structures
15 * is to allow for changes in the future. The structures' details are exposed
16 * so that the compiler can allocate space on the stack and use in other
17 * structures. In other words, accessing any field within this structure
18 * outside of the core timer code is not supported. */
19
20struct mono_time {
21 long microseconds;
22};
23
Aaron Durbin340ca912013-04-30 09:58:12 -050024/* A timeout_callback structure is used for the book keeping for scheduling
25 * work in the future. When a callback is called the structure can be
26 * re-used for scheduling as it is not being tracked by the core timer
27 * library any more. */
28struct timeout_callback {
29 void *priv;
30 void (*callback)(struct timeout_callback *tocb);
31 /* Not for public use. The timer library uses the fields below. */
32 struct mono_time expiration;
33};
34
Aaron Durbina4217912013-04-29 22:31:51 -050035/* Obtain the current monotonic time. The assumption is that the time counts
36 * up from the value 0 with value 0 being the point when the timer was
37 * initialized. Additionally, the timer is assumed to only be valid for the
38 * duration of the boot.
39 *
40 * Note that any implementations of timer_monotonic_get()
41 * need to ensure its timesource does not roll over within 10 secs. The reason
42 * is that the time between calls to timer_monotonic_get() may be on order
43 * of 10 seconds. */
44void timer_monotonic_get(struct mono_time *mt);
45
Aaron Durbin340ca912013-04-30 09:58:12 -050046/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
47int timers_run(void);
48
49/* Schedule a callback to be ran microseconds from time of invocation.
50 * 0 returned on success, < 0 on error. */
51int timer_sched_callback(struct timeout_callback *tocb, unsigned long us);
52
Gabe Black6ccc45d2013-08-09 00:48:06 -070053/* Set an absolute time to a number of microseconds. */
54static inline void mono_time_set_usecs(struct mono_time *mt, long us)
55{
56 mt->microseconds = us;
57}
58
59/* Set an absolute time to a number of milliseconds. */
60static inline void mono_time_set_msecs(struct mono_time *mt, long ms)
61{
62 mt->microseconds = ms * USECS_PER_MSEC;
63}
64
Martin Roth0cb07e32013-07-09 21:46:01 -060065/* Add microseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050066static inline void mono_time_add_usecs(struct mono_time *mt, long us)
67{
68 mt->microseconds += us;
69}
70
Martin Roth0cb07e32013-07-09 21:46:01 -060071/* Add milliseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050072static inline void mono_time_add_msecs(struct mono_time *mt, long ms)
73{
74 mono_time_add_usecs(mt, ms * USECS_PER_MSEC);
75}
76
Martin Roth0cb07e32013-07-09 21:46:01 -060077/* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
Aaron Durbina4217912013-04-29 22:31:51 -050078 * respectively. */
79static inline int mono_time_cmp(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -080080 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -050081{
82 if (t1->microseconds == t2->microseconds)
83 return 0;
84
85 if (t1->microseconds < t2->microseconds)
86 return -1;
87
88 return 1;
89}
90
Aaron Durbina4217912013-04-29 22:31:51 -050091/* Return true if t1 after t2 */
92static inline int mono_time_after(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -080093 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -050094{
95 return mono_time_cmp(t1, t2) > 0;
96}
97
98/* Return true if t1 before t2. */
99static inline int mono_time_before(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -0800100 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -0500101{
102 return mono_time_cmp(t1, t2) < 0;
103}
104
Aaron Durbina7a39172014-09-24 09:56:18 -0500105/* Return time difference between t1 and t2. i.e. t2 - t1. */
David Hendricks040f25b2013-05-03 12:28:11 -0700106static inline long mono_time_diff_microseconds(const struct mono_time *t1,
107 const struct mono_time *t2)
108{
Aaron Durbina7a39172014-09-24 09:56:18 -0500109 return t2->microseconds - t1->microseconds;
David Hendricks040f25b2013-05-03 12:28:11 -0700110}
111
Aaron Durbinf65153e2014-09-23 16:28:43 -0500112struct stopwatch {
113 struct mono_time start;
114 struct mono_time current;
115 struct mono_time expires;
116};
117
118static inline void stopwatch_init(struct stopwatch *sw)
119{
Julius Wernercd49cce2019-03-05 16:53:33 -0800120 if (CONFIG(HAVE_MONOTONIC_TIMER))
Paul Menzelbe706462015-10-20 22:27:05 +0200121 timer_monotonic_get(&sw->start);
122 else
123 sw->start.microseconds = 0;
124
Aaron Durbinf65153e2014-09-23 16:28:43 -0500125 sw->current = sw->expires = sw->start;
126}
127
128static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
129{
130 stopwatch_init(sw);
131 mono_time_add_usecs(&sw->expires, us);
132}
133
134static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
135{
136 stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms);
137}
138
139/*
140 * Tick the stopwatch to collect the current time.
141 */
142static inline void stopwatch_tick(struct stopwatch *sw)
143{
Julius Wernercd49cce2019-03-05 16:53:33 -0800144 if (CONFIG(HAVE_MONOTONIC_TIMER))
Paul Menzelbe706462015-10-20 22:27:05 +0200145 timer_monotonic_get(&sw->current);
146 else
147 sw->current.microseconds = 0;
Aaron Durbinf65153e2014-09-23 16:28:43 -0500148}
149
150/*
Elyes HAOUAS5f73e222020-01-15 21:13:45 +0100151 * Tick and check the stopwatch for expiration. Returns non-zero on expiration.
Aaron Durbinf65153e2014-09-23 16:28:43 -0500152 */
153static inline int stopwatch_expired(struct stopwatch *sw)
154{
155 stopwatch_tick(sw);
156 return !mono_time_before(&sw->current, &sw->expires);
157}
158
159/*
Jonathan Neuschäfer109ba282017-09-19 15:19:53 +0200160 * Tick and check the stopwatch as long as it has not expired.
161 */
162static inline void stopwatch_wait_until_expired(struct stopwatch *sw)
163{
164 while (!stopwatch_expired(sw))
165 ;
166}
167
168/*
Aaron Durbinf65153e2014-09-23 16:28:43 -0500169 * Return number of microseconds since starting the stopwatch.
170 */
171static inline long stopwatch_duration_usecs(struct stopwatch *sw)
172{
173 /*
174 * If the stopwatch hasn't been ticked (current == start) tick
175 * the stopwatch to gather the accumulated time.
176 */
177 if (!mono_time_cmp(&sw->start, &sw->current))
178 stopwatch_tick(sw);
179
180 return mono_time_diff_microseconds(&sw->start, &sw->current);
181}
182
183static inline long stopwatch_duration_msecs(struct stopwatch *sw)
184{
185 return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
186}
187
Julius Werner51325702018-11-02 14:48:24 -0700188/*
189 * Helper macro to wait until a condition becomes true or a timeout elapses.
190 *
191 * condition: a C expression to wait for
192 * timeout: timeout, in microseconds
193 *
194 * Returns:
195 * 0 if the condition still evaluates to false after the timeout elapsed,
196 * >0 if the condition evaluates to true. The return value is the amount of
197 * microseconds waited (at least 1).
198 */
199#define wait_us(timeout_us, condition) \
200({ \
201 long __ret = 0; \
202 struct stopwatch __sw; \
203 stopwatch_init_usecs_expire(&__sw, timeout_us); \
204 do { \
205 if (condition) { \
206 stopwatch_tick(&__sw); \
207 __ret = stopwatch_duration_usecs(&__sw); \
208 if (!__ret) /* make sure it evaluates to true */\
209 __ret = 1; \
210 break; \
211 } \
212 } while (!stopwatch_expired(&__sw)); \
213 __ret; \
214})
215
216#define wait_ms(timeout_ms, condition) \
217 DIV_ROUND_UP(wait_us((timeout_ms) * USECS_PER_MSEC, condition), \
218 USECS_PER_MSEC)
219
Aaron Durbina4217912013-04-29 22:31:51 -0500220#endif /* TIMER_H */