blob: d3afb19646d02591b613c8ce4315417a7ed9c14a [file] [log] [blame]
Aaron Durbina4217912013-04-29 22:31:51 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbina4217912013-04-29 22:31:51 -050014 */
15#ifndef TIMER_H
16#define TIMER_H
17
Julius Werner51325702018-11-02 14:48:24 -070018#include <types.h>
19
Lin Huangda6b1bc2017-11-07 15:38:24 +080020#define NSECS_PER_SEC 1000000000
Aaron Durbina4217912013-04-29 22:31:51 -050021#define USECS_PER_SEC 1000000
22#define MSECS_PER_SEC 1000
23#define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC)
24
25/* The time structures are defined to be a representation of the time since
26 * coreboot started executing one of its stages. The reason for using structures
27 * is to allow for changes in the future. The structures' details are exposed
28 * so that the compiler can allocate space on the stack and use in other
29 * structures. In other words, accessing any field within this structure
30 * outside of the core timer code is not supported. */
31
32struct mono_time {
33 long microseconds;
34};
35
Aaron Durbin340ca912013-04-30 09:58:12 -050036/* A timeout_callback structure is used for the book keeping for scheduling
37 * work in the future. When a callback is called the structure can be
38 * re-used for scheduling as it is not being tracked by the core timer
39 * library any more. */
40struct timeout_callback {
41 void *priv;
42 void (*callback)(struct timeout_callback *tocb);
43 /* Not for public use. The timer library uses the fields below. */
44 struct mono_time expiration;
45};
46
Aaron Durbina4217912013-04-29 22:31:51 -050047/* Obtain the current monotonic time. The assumption is that the time counts
48 * up from the value 0 with value 0 being the point when the timer was
49 * initialized. Additionally, the timer is assumed to only be valid for the
50 * duration of the boot.
51 *
52 * Note that any implementations of timer_monotonic_get()
53 * need to ensure its timesource does not roll over within 10 secs. The reason
54 * is that the time between calls to timer_monotonic_get() may be on order
55 * of 10 seconds. */
56void timer_monotonic_get(struct mono_time *mt);
57
Aaron Durbin340ca912013-04-30 09:58:12 -050058/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
59int timers_run(void);
60
61/* Schedule a callback to be ran microseconds from time of invocation.
62 * 0 returned on success, < 0 on error. */
63int timer_sched_callback(struct timeout_callback *tocb, unsigned long us);
64
Gabe Black6ccc45d2013-08-09 00:48:06 -070065/* Set an absolute time to a number of microseconds. */
66static inline void mono_time_set_usecs(struct mono_time *mt, long us)
67{
68 mt->microseconds = us;
69}
70
71/* Set an absolute time to a number of milliseconds. */
72static inline void mono_time_set_msecs(struct mono_time *mt, long ms)
73{
74 mt->microseconds = ms * USECS_PER_MSEC;
75}
76
Martin Roth0cb07e32013-07-09 21:46:01 -060077/* Add microseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050078static inline void mono_time_add_usecs(struct mono_time *mt, long us)
79{
80 mt->microseconds += us;
81}
82
Martin Roth0cb07e32013-07-09 21:46:01 -060083/* Add milliseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050084static inline void mono_time_add_msecs(struct mono_time *mt, long ms)
85{
86 mono_time_add_usecs(mt, ms * USECS_PER_MSEC);
87}
88
Martin Roth0cb07e32013-07-09 21:46:01 -060089/* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
Aaron Durbina4217912013-04-29 22:31:51 -050090 * respectively. */
91static inline int mono_time_cmp(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 if (t1->microseconds == t2->microseconds)
95 return 0;
96
97 if (t1->microseconds < t2->microseconds)
98 return -1;
99
100 return 1;
101}
102
Aaron Durbina4217912013-04-29 22:31:51 -0500103/* Return true if t1 after t2 */
104static inline int mono_time_after(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -0800105 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -0500106{
107 return mono_time_cmp(t1, t2) > 0;
108}
109
110/* Return true if t1 before t2. */
111static inline int mono_time_before(const struct mono_time *t1,
Lee Leahy708fc272017-03-07 12:18:53 -0800112 const struct mono_time *t2)
Aaron Durbina4217912013-04-29 22:31:51 -0500113{
114 return mono_time_cmp(t1, t2) < 0;
115}
116
Aaron Durbina7a39172014-09-24 09:56:18 -0500117/* Return time difference between t1 and t2. i.e. t2 - t1. */
David Hendricks040f25b2013-05-03 12:28:11 -0700118static inline long mono_time_diff_microseconds(const struct mono_time *t1,
119 const struct mono_time *t2)
120{
Aaron Durbina7a39172014-09-24 09:56:18 -0500121 return t2->microseconds - t1->microseconds;
David Hendricks040f25b2013-05-03 12:28:11 -0700122}
123
Aaron Durbinf65153e2014-09-23 16:28:43 -0500124struct stopwatch {
125 struct mono_time start;
126 struct mono_time current;
127 struct mono_time expires;
128};
129
130static inline void stopwatch_init(struct stopwatch *sw)
131{
Paul Menzelbe706462015-10-20 22:27:05 +0200132 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
133 timer_monotonic_get(&sw->start);
134 else
135 sw->start.microseconds = 0;
136
Aaron Durbinf65153e2014-09-23 16:28:43 -0500137 sw->current = sw->expires = sw->start;
138}
139
140static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
141{
142 stopwatch_init(sw);
143 mono_time_add_usecs(&sw->expires, us);
144}
145
146static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
147{
148 stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms);
149}
150
151/*
152 * Tick the stopwatch to collect the current time.
153 */
154static inline void stopwatch_tick(struct stopwatch *sw)
155{
Paul Menzelbe706462015-10-20 22:27:05 +0200156 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
157 timer_monotonic_get(&sw->current);
158 else
159 sw->current.microseconds = 0;
Aaron Durbinf65153e2014-09-23 16:28:43 -0500160}
161
162/*
163 * Tick and check the stopwatch for expiration. Returns non-zero on exipration.
164 */
165static inline int stopwatch_expired(struct stopwatch *sw)
166{
167 stopwatch_tick(sw);
168 return !mono_time_before(&sw->current, &sw->expires);
169}
170
171/*
Jonathan Neuschäfer109ba282017-09-19 15:19:53 +0200172 * Tick and check the stopwatch as long as it has not expired.
173 */
174static inline void stopwatch_wait_until_expired(struct stopwatch *sw)
175{
176 while (!stopwatch_expired(sw))
177 ;
178}
179
180/*
Aaron Durbinf65153e2014-09-23 16:28:43 -0500181 * Return number of microseconds since starting the stopwatch.
182 */
183static inline long stopwatch_duration_usecs(struct stopwatch *sw)
184{
185 /*
186 * If the stopwatch hasn't been ticked (current == start) tick
187 * the stopwatch to gather the accumulated time.
188 */
189 if (!mono_time_cmp(&sw->start, &sw->current))
190 stopwatch_tick(sw);
191
192 return mono_time_diff_microseconds(&sw->start, &sw->current);
193}
194
195static inline long stopwatch_duration_msecs(struct stopwatch *sw)
196{
197 return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
198}
199
Julius Werner51325702018-11-02 14:48:24 -0700200/*
201 * Helper macro to wait until a condition becomes true or a timeout elapses.
202 *
203 * condition: a C expression to wait for
204 * timeout: timeout, in microseconds
205 *
206 * Returns:
207 * 0 if the condition still evaluates to false after the timeout elapsed,
208 * >0 if the condition evaluates to true. The return value is the amount of
209 * microseconds waited (at least 1).
210 */
211#define wait_us(timeout_us, condition) \
212({ \
213 long __ret = 0; \
214 struct stopwatch __sw; \
215 stopwatch_init_usecs_expire(&__sw, timeout_us); \
216 do { \
217 if (condition) { \
218 stopwatch_tick(&__sw); \
219 __ret = stopwatch_duration_usecs(&__sw); \
220 if (!__ret) /* make sure it evaluates to true */\
221 __ret = 1; \
222 break; \
223 } \
224 } while (!stopwatch_expired(&__sw)); \
225 __ret; \
226})
227
228#define wait_ms(timeout_ms, condition) \
229 DIV_ROUND_UP(wait_us((timeout_ms) * USECS_PER_MSEC, condition), \
230 USECS_PER_MSEC)
231
Aaron Durbina4217912013-04-29 22:31:51 -0500232#endif /* TIMER_H */