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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 18 | */ |
| 19 | #ifndef TIMER_H |
| 20 | #define TIMER_H |
| 21 | |
| 22 | #define USECS_PER_SEC 1000000 |
| 23 | #define MSECS_PER_SEC 1000 |
| 24 | #define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC) |
| 25 | |
| 26 | /* The time structures are defined to be a representation of the time since |
| 27 | * coreboot started executing one of its stages. The reason for using structures |
| 28 | * is to allow for changes in the future. The structures' details are exposed |
| 29 | * so that the compiler can allocate space on the stack and use in other |
| 30 | * structures. In other words, accessing any field within this structure |
| 31 | * outside of the core timer code is not supported. */ |
| 32 | |
| 33 | struct mono_time { |
| 34 | long microseconds; |
| 35 | }; |
| 36 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 37 | /* A timeout_callback structure is used for the book keeping for scheduling |
| 38 | * work in the future. When a callback is called the structure can be |
| 39 | * re-used for scheduling as it is not being tracked by the core timer |
| 40 | * library any more. */ |
| 41 | struct timeout_callback { |
| 42 | void *priv; |
| 43 | void (*callback)(struct timeout_callback *tocb); |
| 44 | /* Not for public use. The timer library uses the fields below. */ |
| 45 | struct mono_time expiration; |
| 46 | }; |
| 47 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 48 | /* Obtain the current monotonic time. The assumption is that the time counts |
| 49 | * up from the value 0 with value 0 being the point when the timer was |
| 50 | * initialized. Additionally, the timer is assumed to only be valid for the |
| 51 | * duration of the boot. |
| 52 | * |
| 53 | * Note that any implementations of timer_monotonic_get() |
| 54 | * need to ensure its timesource does not roll over within 10 secs. The reason |
| 55 | * is that the time between calls to timer_monotonic_get() may be on order |
| 56 | * of 10 seconds. */ |
| 57 | void timer_monotonic_get(struct mono_time *mt); |
| 58 | |
Aaron Durbin | 340ca91 | 2013-04-30 09:58:12 -0500 | [diff] [blame] | 59 | /* Returns 1 if callbacks still present in the queue. 0 if no timers left. */ |
| 60 | int timers_run(void); |
| 61 | |
| 62 | /* Schedule a callback to be ran microseconds from time of invocation. |
| 63 | * 0 returned on success, < 0 on error. */ |
| 64 | int timer_sched_callback(struct timeout_callback *tocb, unsigned long us); |
| 65 | |
Gabe Black | 6ccc45d | 2013-08-09 00:48:06 -0700 | [diff] [blame] | 66 | /* Set an absolute time to a number of microseconds. */ |
| 67 | static inline void mono_time_set_usecs(struct mono_time *mt, long us) |
| 68 | { |
| 69 | mt->microseconds = us; |
| 70 | } |
| 71 | |
| 72 | /* Set an absolute time to a number of milliseconds. */ |
| 73 | static inline void mono_time_set_msecs(struct mono_time *mt, long ms) |
| 74 | { |
| 75 | mt->microseconds = ms * USECS_PER_MSEC; |
| 76 | } |
| 77 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 78 | /* Add microseconds to an absolute time. */ |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 79 | static inline void mono_time_add_usecs(struct mono_time *mt, long us) |
| 80 | { |
| 81 | mt->microseconds += us; |
| 82 | } |
| 83 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 84 | /* Add milliseconds to an absolute time. */ |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 85 | static inline void mono_time_add_msecs(struct mono_time *mt, long ms) |
| 86 | { |
| 87 | mono_time_add_usecs(mt, ms * USECS_PER_MSEC); |
| 88 | } |
| 89 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 90 | /* 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] | 91 | * respectively. */ |
| 92 | static inline int mono_time_cmp(const struct mono_time *t1, |
| 93 | const struct mono_time *t2) |
| 94 | { |
| 95 | if (t1->microseconds == t2->microseconds) |
| 96 | return 0; |
| 97 | |
| 98 | if (t1->microseconds < t2->microseconds) |
| 99 | return -1; |
| 100 | |
| 101 | return 1; |
| 102 | } |
| 103 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 104 | /* Return true if t1 after t2 */ |
| 105 | static inline int mono_time_after(const struct mono_time *t1, |
| 106 | const struct mono_time *t2) |
| 107 | { |
| 108 | return mono_time_cmp(t1, t2) > 0; |
| 109 | } |
| 110 | |
| 111 | /* Return true if t1 before t2. */ |
| 112 | static inline int mono_time_before(const struct mono_time *t1, |
| 113 | const struct mono_time *t2) |
| 114 | { |
| 115 | return mono_time_cmp(t1, t2) < 0; |
| 116 | } |
| 117 | |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 118 | /* Return time difference between t1 and t2. i.e. t2 - t1. */ |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 119 | static inline long mono_time_diff_microseconds(const struct mono_time *t1, |
| 120 | const struct mono_time *t2) |
| 121 | { |
Aaron Durbin | a7a3917 | 2014-09-24 09:56:18 -0500 | [diff] [blame] | 122 | return t2->microseconds - t1->microseconds; |
David Hendricks | 040f25b | 2013-05-03 12:28:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 125 | struct stopwatch { |
| 126 | struct mono_time start; |
| 127 | struct mono_time current; |
| 128 | struct mono_time expires; |
| 129 | }; |
| 130 | |
| 131 | static inline void stopwatch_init(struct stopwatch *sw) |
| 132 | { |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame^] | 133 | if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 134 | timer_monotonic_get(&sw->start); |
| 135 | else |
| 136 | sw->start.microseconds = 0; |
| 137 | |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 138 | sw->current = sw->expires = sw->start; |
| 139 | } |
| 140 | |
| 141 | static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, long us) |
| 142 | { |
| 143 | stopwatch_init(sw); |
| 144 | mono_time_add_usecs(&sw->expires, us); |
| 145 | } |
| 146 | |
| 147 | static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms) |
| 148 | { |
| 149 | stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Tick the stopwatch to collect the current time. |
| 154 | */ |
| 155 | static inline void stopwatch_tick(struct stopwatch *sw) |
| 156 | { |
Paul Menzel | be70646 | 2015-10-20 22:27:05 +0200 | [diff] [blame^] | 157 | if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 158 | timer_monotonic_get(&sw->current); |
| 159 | else |
| 160 | sw->current.microseconds = 0; |
Aaron Durbin | f65153e | 2014-09-23 16:28:43 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Tick and check the stopwatch for expiration. Returns non-zero on exipration. |
| 165 | */ |
| 166 | static inline int stopwatch_expired(struct stopwatch *sw) |
| 167 | { |
| 168 | stopwatch_tick(sw); |
| 169 | return !mono_time_before(&sw->current, &sw->expires); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Return number of microseconds since starting the stopwatch. |
| 174 | */ |
| 175 | static inline long stopwatch_duration_usecs(struct stopwatch *sw) |
| 176 | { |
| 177 | /* |
| 178 | * If the stopwatch hasn't been ticked (current == start) tick |
| 179 | * the stopwatch to gather the accumulated time. |
| 180 | */ |
| 181 | if (!mono_time_cmp(&sw->start, &sw->current)) |
| 182 | stopwatch_tick(sw); |
| 183 | |
| 184 | return mono_time_diff_microseconds(&sw->start, &sw->current); |
| 185 | } |
| 186 | |
| 187 | static inline long stopwatch_duration_msecs(struct stopwatch *sw) |
| 188 | { |
| 189 | return stopwatch_duration_usecs(sw) / USECS_PER_MSEC; |
| 190 | } |
| 191 | |
Aaron Durbin | a421791 | 2013-04-29 22:31:51 -0500 | [diff] [blame] | 192 | #endif /* TIMER_H */ |