blob: 691d6fbab572f4d9ce22de2e45d1e570327e49cf [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.
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 Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Aaron Durbina4217912013-04-29 22:31:51 -050018 */
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
33struct mono_time {
34 long microseconds;
35};
36
Aaron Durbin340ca912013-04-30 09:58:12 -050037/* 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. */
41struct 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 Durbina4217912013-04-29 22:31:51 -050048/* 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. */
57void timer_monotonic_get(struct mono_time *mt);
58
Aaron Durbin340ca912013-04-30 09:58:12 -050059/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
60int timers_run(void);
61
62/* Schedule a callback to be ran microseconds from time of invocation.
63 * 0 returned on success, < 0 on error. */
64int timer_sched_callback(struct timeout_callback *tocb, unsigned long us);
65
Gabe Black6ccc45d2013-08-09 00:48:06 -070066/* Set an absolute time to a number of microseconds. */
67static 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. */
73static inline void mono_time_set_msecs(struct mono_time *mt, long ms)
74{
75 mt->microseconds = ms * USECS_PER_MSEC;
76}
77
Martin Roth0cb07e32013-07-09 21:46:01 -060078/* Add microseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050079static inline void mono_time_add_usecs(struct mono_time *mt, long us)
80{
81 mt->microseconds += us;
82}
83
Martin Roth0cb07e32013-07-09 21:46:01 -060084/* Add milliseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050085static 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 Roth0cb07e32013-07-09 21:46:01 -060090/* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
Aaron Durbina4217912013-04-29 22:31:51 -050091 * respectively. */
92static 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 Durbina4217912013-04-29 22:31:51 -0500104/* Return true if t1 after t2 */
105static 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. */
112static 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 Durbina7a39172014-09-24 09:56:18 -0500118/* Return time difference between t1 and t2. i.e. t2 - t1. */
David Hendricks040f25b2013-05-03 12:28:11 -0700119static inline long mono_time_diff_microseconds(const struct mono_time *t1,
120 const struct mono_time *t2)
121{
Aaron Durbina7a39172014-09-24 09:56:18 -0500122 return t2->microseconds - t1->microseconds;
David Hendricks040f25b2013-05-03 12:28:11 -0700123}
124
Aaron Durbinf65153e2014-09-23 16:28:43 -0500125struct stopwatch {
126 struct mono_time start;
127 struct mono_time current;
128 struct mono_time expires;
129};
130
131static inline void stopwatch_init(struct stopwatch *sw)
132{
Paul Menzelbe706462015-10-20 22:27:05 +0200133 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
134 timer_monotonic_get(&sw->start);
135 else
136 sw->start.microseconds = 0;
137
Aaron Durbinf65153e2014-09-23 16:28:43 -0500138 sw->current = sw->expires = sw->start;
139}
140
141static 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
147static 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 */
155static inline void stopwatch_tick(struct stopwatch *sw)
156{
Paul Menzelbe706462015-10-20 22:27:05 +0200157 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
158 timer_monotonic_get(&sw->current);
159 else
160 sw->current.microseconds = 0;
Aaron Durbinf65153e2014-09-23 16:28:43 -0500161}
162
163/*
164 * Tick and check the stopwatch for expiration. Returns non-zero on exipration.
165 */
166static 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 */
175static 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
187static inline long stopwatch_duration_msecs(struct stopwatch *sw)
188{
189 return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
190}
191
Aaron Durbina4217912013-04-29 22:31:51 -0500192#endif /* TIMER_H */