2 * This file is part of the coreboot project.
4 * Copyright (C) 2013 Google, Inc.
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.
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.
20 #define NSECS_PER_SEC 1000000000
21 #define USECS_PER_SEC 1000000
22 #define MSECS_PER_SEC 1000
23 #define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC)
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. */
36 /* 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. */
40 struct timeout_callback
{
42 void (*callback
)(struct timeout_callback
*tocb
);
43 /* Not for public use. The timer library uses the fields below. */
44 struct mono_time expiration
;
47 /* 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.
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
56 void timer_monotonic_get(struct mono_time
*mt
);
58 /* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
61 /* Schedule a callback to be ran microseconds from time of invocation.
62 * 0 returned on success, < 0 on error. */
63 int timer_sched_callback(struct timeout_callback
*tocb
, unsigned long us
);
65 /* Set an absolute time to a number of microseconds. */
66 static inline void mono_time_set_usecs(struct mono_time
*mt
, long us
)
68 mt
->microseconds
= us
;
71 /* Set an absolute time to a number of milliseconds. */
72 static inline void mono_time_set_msecs(struct mono_time
*mt
, long ms
)
74 mt
->microseconds
= ms
* USECS_PER_MSEC
;
77 /* Add microseconds to an absolute time. */
78 static inline void mono_time_add_usecs(struct mono_time
*mt
, long us
)
80 mt
->microseconds
+= us
;
83 /* Add milliseconds to an absolute time. */
84 static inline void mono_time_add_msecs(struct mono_time
*mt
, long ms
)
86 mono_time_add_usecs(mt
, ms
* USECS_PER_MSEC
);
89 /* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
91 static inline int mono_time_cmp(const struct mono_time
*t1
,
92 const struct mono_time
*t2
)
94 if (t1
->microseconds
== t2
->microseconds
)
97 if (t1
->microseconds
< t2
->microseconds
)
103 /* Return true if t1 after t2 */
104 static inline int mono_time_after(const struct mono_time
*t1
,
105 const struct mono_time
*t2
)
107 return mono_time_cmp(t1
, t2
) > 0;
110 /* Return true if t1 before t2. */
111 static inline int mono_time_before(const struct mono_time
*t1
,
112 const struct mono_time
*t2
)
114 return mono_time_cmp(t1
, t2
) < 0;
117 /* Return time difference between t1 and t2. i.e. t2 - t1. */
118 static inline long mono_time_diff_microseconds(const struct mono_time
*t1
,
119 const struct mono_time
*t2
)
121 return t2
->microseconds
- t1
->microseconds
;
125 struct mono_time start
;
126 struct mono_time current
;
127 struct mono_time expires
;
130 static inline void stopwatch_init(struct stopwatch
*sw
)
132 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER
))
133 timer_monotonic_get(&sw
->start
);
135 sw
->start
.microseconds
= 0;
137 sw
->current
= sw
->expires
= sw
->start
;
140 static inline void stopwatch_init_usecs_expire(struct stopwatch
*sw
, long us
)
143 mono_time_add_usecs(&sw
->expires
, us
);
146 static inline void stopwatch_init_msecs_expire(struct stopwatch
*sw
, long ms
)
148 stopwatch_init_usecs_expire(sw
, USECS_PER_MSEC
* ms
);
152 * Tick the stopwatch to collect the current time.
154 static inline void stopwatch_tick(struct stopwatch
*sw
)
156 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER
))
157 timer_monotonic_get(&sw
->current
);
159 sw
->current
.microseconds
= 0;
163 * Tick and check the stopwatch for expiration. Returns non-zero on exipration.
165 static inline int stopwatch_expired(struct stopwatch
*sw
)
168 return !mono_time_before(&sw
->current
, &sw
->expires
);
172 * Tick and check the stopwatch as long as it has not expired.
174 static inline void stopwatch_wait_until_expired(struct stopwatch
*sw
)
176 while (!stopwatch_expired(sw
))
181 * Return number of microseconds since starting the stopwatch.
183 static inline long stopwatch_duration_usecs(struct stopwatch
*sw
)
186 * If the stopwatch hasn't been ticked (current == start) tick
187 * the stopwatch to gather the accumulated time.
189 if (!mono_time_cmp(&sw
->start
, &sw
->current
))
192 return mono_time_diff_microseconds(&sw
->start
, &sw
->current
);
195 static inline long stopwatch_duration_msecs(struct stopwatch
*sw
)
197 return stopwatch_duration_usecs(sw
) / USECS_PER_MSEC
;
201 * Helper macro to wait until a condition becomes true or a timeout elapses.
203 * condition: a C expression to wait for
204 * timeout: timeout, in microseconds
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).
211 #define wait_us(timeout_us, condition) \
214 struct stopwatch __sw; \
215 stopwatch_init_usecs_expire(&__sw, timeout_us); \
218 stopwatch_tick(&__sw); \
219 __ret = stopwatch_duration_usecs(&__sw); \
220 if (!__ret) /* make sure it evaluates to true */\
224 } while (!stopwatch_expired(&__sw)); \
228 #define wait_ms(timeout_ms, condition) \
229 DIV_ROUND_UP(wait_us((timeout_ms) * USECS_PER_MSEC, condition), \