blob: 2b112dd5a948b58799697fb15d2da0ae6351407b [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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
33struct mono_time {
34 long microseconds;
35};
36
37struct rela_time {
38 long microseconds;
39};
40
41/* Obtain the current monotonic time. The assumption is that the time counts
42 * up from the value 0 with value 0 being the point when the timer was
43 * initialized. Additionally, the timer is assumed to only be valid for the
44 * duration of the boot.
45 *
46 * Note that any implementations of timer_monotonic_get()
47 * need to ensure its timesource does not roll over within 10 secs. The reason
48 * is that the time between calls to timer_monotonic_get() may be on order
49 * of 10 seconds. */
50void timer_monotonic_get(struct mono_time *mt);
51
52/* Add microseconds to an absoute time. */
53static inline void mono_time_add_usecs(struct mono_time *mt, long us)
54{
55 mt->microseconds += us;
56}
57
58/* Add milliseconds to an absoute time. */
59static inline void mono_time_add_msecs(struct mono_time *mt, long ms)
60{
61 mono_time_add_usecs(mt, ms * USECS_PER_MSEC);
62}
63
64static inline void mono_time_add_rela_time(struct mono_time *mt,
65 const struct rela_time *t)
66{
67 mono_time_add_usecs(mt, t->microseconds);
68}
69
70/* Compare two absoluted times: Return -1, 0, or 1 if t1 is <, =, or > t2,
71 * respectively. */
72static inline int mono_time_cmp(const struct mono_time *t1,
73 const struct mono_time *t2)
74{
75 if (t1->microseconds == t2->microseconds)
76 return 0;
77
78 if (t1->microseconds < t2->microseconds)
79 return -1;
80
81 return 1;
82}
83
84static inline int rela_time_cmp(const struct rela_time *t1,
85 const struct rela_time *t2)
86{
87 if (t1->microseconds == t2->microseconds)
88 return 0;
89
90 if (t1->microseconds < t2->microseconds)
91 return -1;
92
93 return 1;
94}
95
96/* Iniitalize a rela_time structure. */
97static inline struct rela_time rela_time_init_usecs(long us)
98{
99 struct rela_time t;
100 t.microseconds = us;
101 return t;
102}
103
104/* Return time difference between t1 and t2. i.e. t2 - t1. */
105static struct rela_time mono_time_diff(const struct mono_time *t1,
106 const struct mono_time *t2)
107{
108 return rela_time_init_usecs(t2->microseconds - t1->microseconds);
109}
110
111/* Return true if t1 after t2 */
112static inline int mono_time_after(const struct mono_time *t1,
113 const struct mono_time *t2)
114{
115 return mono_time_cmp(t1, t2) > 0;
116}
117
118/* Return true if t1 before t2. */
119static inline int mono_time_before(const struct mono_time *t1,
120 const struct mono_time *t2)
121{
122 return mono_time_cmp(t1, t2) < 0;
123}
124
125/* Return the difference between now and t. */
126static inline struct rela_time current_time_from(const struct mono_time *t)
127{
128 struct mono_time now;
129
130 timer_monotonic_get(&now);
131 return mono_time_diff(t, &now);
132
133}
134
135static inline long rela_time_in_microseconds(const struct rela_time *rt)
136{
137 return rt->microseconds;
138}
139
140#endif /* TIMER_H */