Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 1 | /* |
| 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 Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 14 | */ |
| 15 | #ifndef TIMER_H |
| 16 | #define TIMER_H |
| 17 | |
Julius Werner | 5132570 | 2018-11-02 14:48:24 -0700 | [diff] [blame] | 18 | #include <types.h> |
| 19 | |
Lin Huang | da6b1bc | 2017-11-07 15:38:24 +0800 | [diff] [blame] | 20 | #define NSECS_PER_SEC 1000000000 |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 21 | #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 | |
| 32 | struct mono_time { |
| 33 | long microseconds; |
| 34 | }; |
| 35 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 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 { |
| 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 Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 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. |
| 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. */ |
| 56 | void timer_monotonic_get(struct mono_time *mt); |
| 57 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 58 | /* Returns 1 if callbacks still present in the queue. 0 if no timers left. */ |
| 59 | int timers_run(void); |
| 60 | |
| 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); |
| 64 | |
Gabe Black | 6ccc45d | 2013-08-09 00:48:06 -0700 | [diff] [blame] | 65 | /* Set an absolute time to a number of microseconds. */ |
| 66 | static 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. */ |
| 72 | static inline void mono_time_set_msecs(struct mono_time *mt, long ms) |
| 73 | { |
| 74 | mt->microseconds = ms * USECS_PER_MSEC; |
| 75 | } |
| 76 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 77 | /* Add microseconds to an absolute time. */ |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 78 | static inline void mono_time_add_usecs(struct mono_time *mt, long us) |
| 79 | { |
| 80 | mt->microseconds += us; |
| 81 | } |
| 82 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 83 | /* Add milliseconds to an absolute time. */ |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 84 | static 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 Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 89 | /* 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] | 90 | * respectively. */ |
| 91 | static inline int mono_time_cmp(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 | 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 Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 103 | /* Return true if t1 after t2 */ |
| 104 | static inline int mono_time_after(const struct mono_time *t1, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 105 | const struct mono_time *t2) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 106 | { |
| 107 | return mono_time_cmp(t1, t2) > 0; |
| 108 | } |
| 109 | |
| 110 | /* Return true if t1 before t2. */ |
| 111 | static inline int mono_time_before(const struct mono_time *t1, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 112 | const struct mono_time *t2) |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 113 | { |
| 114 | return mono_time_cmp(t1, t2) < 0; |
| 115 | } |
| 116 | |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 117 | /* Return time difference between t1 and t2. i.e. t2 - t1. */ |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 118 | static inline long mono_time_diff_microseconds(const struct mono_time *t1, |
| 119 | const struct mono_time *t2) |
| 120 | { |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 121 | return t2->microseconds - t1->microseconds; |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 124 | struct stopwatch { |
| 125 | struct mono_time start; |
| 126 | struct mono_time current; |
| 127 | struct mono_time expires; |
| 128 | }; |
| 129 | |
| 130 | static inline void stopwatch_init(struct stopwatch *sw) |
| 131 | { |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame] | 132 | if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 133 | timer_monotonic_get(&sw->start); |
| 134 | else |
| 135 | sw->start.microseconds = 0; |
| 136 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 137 | sw->current = sw->expires = sw->start; |
| 138 | } |
| 139 | |
| 140 | static 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 | |
| 146 | static 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 | */ |
| 154 | static inline void stopwatch_tick(struct stopwatch *sw) |
| 155 | { |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame] | 156 | if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 157 | timer_monotonic_get(&sw->current); |
| 158 | else |
| 159 | sw->current.microseconds = 0; |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Tick and check the stopwatch for expiration. Returns non-zero on exipration. |
| 164 | */ |
| 165 | static 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äfer | 109ba28 | 2017-09-19 15:19:53 +0200 | [diff] [blame] | 172 | * Tick and check the stopwatch as long as it has not expired. |
| 173 | */ |
| 174 | static inline void stopwatch_wait_until_expired(struct stopwatch *sw) |
| 175 | { |
| 176 | while (!stopwatch_expired(sw)) |
| 177 | ; |
| 178 | } |
| 179 | |
| 180 | /* |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 181 | * Return number of microseconds since starting the stopwatch. |
| 182 | */ |
| 183 | static 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 | |
| 195 | static inline long stopwatch_duration_msecs(struct stopwatch *sw) |
| 196 | { |
| 197 | return stopwatch_duration_usecs(sw) / USECS_PER_MSEC; |
| 198 | } |
| 199 | |
Julius Werner | 5132570 | 2018-11-02 14:48:24 -0700 | [diff] [blame] | 200 | /* |
| 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 Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 232 | #endif /* TIMER_H */ |