blob: 0105e32320a8f7eb0c1fab60a84d117e64844297 [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
18#define USECS_PER_SEC 1000000
19#define MSECS_PER_SEC 1000
20#define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC)
21
22/* The time structures are defined to be a representation of the time since
23 * coreboot started executing one of its stages. The reason for using structures
24 * is to allow for changes in the future. The structures' details are exposed
25 * so that the compiler can allocate space on the stack and use in other
26 * structures. In other words, accessing any field within this structure
27 * outside of the core timer code is not supported. */
28
29struct mono_time {
30 long microseconds;
31};
32
Aaron Durbin340ca912013-04-30 09:58:12 -050033/* A timeout_callback structure is used for the book keeping for scheduling
34 * work in the future. When a callback is called the structure can be
35 * re-used for scheduling as it is not being tracked by the core timer
36 * library any more. */
37struct timeout_callback {
38 void *priv;
39 void (*callback)(struct timeout_callback *tocb);
40 /* Not for public use. The timer library uses the fields below. */
41 struct mono_time expiration;
42};
43
Aaron Durbina4217912013-04-29 22:31:51 -050044/* Obtain the current monotonic time. The assumption is that the time counts
45 * up from the value 0 with value 0 being the point when the timer was
46 * initialized. Additionally, the timer is assumed to only be valid for the
47 * duration of the boot.
48 *
49 * Note that any implementations of timer_monotonic_get()
50 * need to ensure its timesource does not roll over within 10 secs. The reason
51 * is that the time between calls to timer_monotonic_get() may be on order
52 * of 10 seconds. */
53void timer_monotonic_get(struct mono_time *mt);
54
Aaron Durbin340ca912013-04-30 09:58:12 -050055/* Returns 1 if callbacks still present in the queue. 0 if no timers left. */
56int timers_run(void);
57
58/* Schedule a callback to be ran microseconds from time of invocation.
59 * 0 returned on success, < 0 on error. */
60int timer_sched_callback(struct timeout_callback *tocb, unsigned long us);
61
Gabe Black6ccc45d2013-08-09 00:48:06 -070062/* Set an absolute time to a number of microseconds. */
63static inline void mono_time_set_usecs(struct mono_time *mt, long us)
64{
65 mt->microseconds = us;
66}
67
68/* Set an absolute time to a number of milliseconds. */
69static inline void mono_time_set_msecs(struct mono_time *mt, long ms)
70{
71 mt->microseconds = ms * USECS_PER_MSEC;
72}
73
Martin Roth0cb07e32013-07-09 21:46:01 -060074/* Add microseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050075static inline void mono_time_add_usecs(struct mono_time *mt, long us)
76{
77 mt->microseconds += us;
78}
79
Martin Roth0cb07e32013-07-09 21:46:01 -060080/* Add milliseconds to an absolute time. */
Aaron Durbina4217912013-04-29 22:31:51 -050081static inline void mono_time_add_msecs(struct mono_time *mt, long ms)
82{
83 mono_time_add_usecs(mt, ms * USECS_PER_MSEC);
84}
85
Martin Roth0cb07e32013-07-09 21:46:01 -060086/* Compare two absolute times: Return -1, 0, or 1 if t1 is <, =, or > t2,
Aaron Durbina4217912013-04-29 22:31:51 -050087 * respectively. */
88static inline int mono_time_cmp(const struct mono_time *t1,
89 const struct mono_time *t2)
90{
91 if (t1->microseconds == t2->microseconds)
92 return 0;
93
94 if (t1->microseconds < t2->microseconds)
95 return -1;
96
97 return 1;
98}
99
Aaron Durbina4217912013-04-29 22:31:51 -0500100/* Return true if t1 after t2 */
101static inline int mono_time_after(const struct mono_time *t1,
102 const struct mono_time *t2)
103{
104 return mono_time_cmp(t1, t2) > 0;
105}
106
107/* Return true if t1 before t2. */
108static inline int mono_time_before(const struct mono_time *t1,
109 const struct mono_time *t2)
110{
111 return mono_time_cmp(t1, t2) < 0;
112}
113
Aaron Durbina7a39172014-09-24 09:56:18 -0500114/* Return time difference between t1 and t2. i.e. t2 - t1. */
David Hendricks040f25b2013-05-03 12:28:11 -0700115static inline long mono_time_diff_microseconds(const struct mono_time *t1,
116 const struct mono_time *t2)
117{
Aaron Durbina7a39172014-09-24 09:56:18 -0500118 return t2->microseconds - t1->microseconds;
David Hendricks040f25b2013-05-03 12:28:11 -0700119}
120
Aaron Durbinf65153e2014-09-23 16:28:43 -0500121struct stopwatch {
122 struct mono_time start;
123 struct mono_time current;
124 struct mono_time expires;
125};
126
127static inline void stopwatch_init(struct stopwatch *sw)
128{
Paul Menzelbe706462015-10-20 22:27:05 +0200129 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
130 timer_monotonic_get(&sw->start);
131 else
132 sw->start.microseconds = 0;
133
Aaron Durbinf65153e2014-09-23 16:28:43 -0500134 sw->current = sw->expires = sw->start;
135}
136
137static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
138{
139 stopwatch_init(sw);
140 mono_time_add_usecs(&sw->expires, us);
141}
142
143static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
144{
145 stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms);
146}
147
148/*
149 * Tick the stopwatch to collect the current time.
150 */
151static inline void stopwatch_tick(struct stopwatch *sw)
152{
Paul Menzelbe706462015-10-20 22:27:05 +0200153 if (IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
154 timer_monotonic_get(&sw->current);
155 else
156 sw->current.microseconds = 0;
Aaron Durbinf65153e2014-09-23 16:28:43 -0500157}
158
159/*
160 * Tick and check the stopwatch for expiration. Returns non-zero on exipration.
161 */
162static inline int stopwatch_expired(struct stopwatch *sw)
163{
164 stopwatch_tick(sw);
165 return !mono_time_before(&sw->current, &sw->expires);
166}
167
168/*
169 * Return number of microseconds since starting the stopwatch.
170 */
171static inline long stopwatch_duration_usecs(struct stopwatch *sw)
172{
173 /*
174 * If the stopwatch hasn't been ticked (current == start) tick
175 * the stopwatch to gather the accumulated time.
176 */
177 if (!mono_time_cmp(&sw->start, &sw->current))
178 stopwatch_tick(sw);
179
180 return mono_time_diff_microseconds(&sw->start, &sw->current);
181}
182
183static inline long stopwatch_duration_msecs(struct stopwatch *sw)
184{
185 return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
186}
187
Aaron Durbina4217912013-04-29 22:31:51 -0500188#endif /* TIMER_H */