From a421791db815fb2e2da9b1ce4bec78c97665b62f Mon Sep 17 00:00:00 2001 From: Aaron Durbin Date: Mon, 29 Apr 2013 22:31:51 -0500 Subject: coreboot: introduce monotonic timer API The notion of a monotonic timer is introduced. Along with it are helper functions and other types for comparing times. This is just the framework where it is the responsibility of the chipset/board to provide the implementation of timer_monotonic_get(). The reason structs are used instead of native types is to allow for future changes to the data structure without chaning all the call sites. Change-Id: Ie56b9ab9dedb0da69dea86ef87ca744004eb1ae3 Signed-off-by: Aaron Durbin Reviewed-on: http://review.coreboot.org/3152 Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich --- src/include/timer.h | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/include/timer.h (limited to 'src/include') diff --git a/src/include/timer.h b/src/include/timer.h new file mode 100644 index 0000000000..2b112dd5a9 --- /dev/null +++ b/src/include/timer.h @@ -0,0 +1,140 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2013 Google, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef TIMER_H +#define TIMER_H + +#define USECS_PER_SEC 1000000 +#define MSECS_PER_SEC 1000 +#define USECS_PER_MSEC (USECS_PER_SEC / MSECS_PER_SEC) + +/* The time structures are defined to be a representation of the time since + * coreboot started executing one of its stages. The reason for using structures + * is to allow for changes in the future. The structures' details are exposed + * so that the compiler can allocate space on the stack and use in other + * structures. In other words, accessing any field within this structure + * outside of the core timer code is not supported. */ + +struct mono_time { + long microseconds; +}; + +struct rela_time { + long microseconds; +}; + +/* Obtain the current monotonic time. The assumption is that the time counts + * up from the value 0 with value 0 being the point when the timer was + * initialized. Additionally, the timer is assumed to only be valid for the + * duration of the boot. + * + * Note that any implementations of timer_monotonic_get() + * need to ensure its timesource does not roll over within 10 secs. The reason + * is that the time between calls to timer_monotonic_get() may be on order + * of 10 seconds. */ +void timer_monotonic_get(struct mono_time *mt); + +/* Add microseconds to an absoute time. */ +static inline void mono_time_add_usecs(struct mono_time *mt, long us) +{ + mt->microseconds += us; +} + +/* Add milliseconds to an absoute time. */ +static inline void mono_time_add_msecs(struct mono_time *mt, long ms) +{ + mono_time_add_usecs(mt, ms * USECS_PER_MSEC); +} + +static inline void mono_time_add_rela_time(struct mono_time *mt, + const struct rela_time *t) +{ + mono_time_add_usecs(mt, t->microseconds); +} + +/* Compare two absoluted times: Return -1, 0, or 1 if t1 is <, =, or > t2, + * respectively. */ +static inline int mono_time_cmp(const struct mono_time *t1, + const struct mono_time *t2) +{ + if (t1->microseconds == t2->microseconds) + return 0; + + if (t1->microseconds < t2->microseconds) + return -1; + + return 1; +} + +static inline int rela_time_cmp(const struct rela_time *t1, + const struct rela_time *t2) +{ + if (t1->microseconds == t2->microseconds) + return 0; + + if (t1->microseconds < t2->microseconds) + return -1; + + return 1; +} + +/* Iniitalize a rela_time structure. */ +static inline struct rela_time rela_time_init_usecs(long us) +{ + struct rela_time t; + t.microseconds = us; + return t; +} + +/* Return time difference between t1 and t2. i.e. t2 - t1. */ +static struct rela_time mono_time_diff(const struct mono_time *t1, + const struct mono_time *t2) +{ + return rela_time_init_usecs(t2->microseconds - t1->microseconds); +} + +/* Return true if t1 after t2 */ +static inline int mono_time_after(const struct mono_time *t1, + const struct mono_time *t2) +{ + return mono_time_cmp(t1, t2) > 0; +} + +/* Return true if t1 before t2. */ +static inline int mono_time_before(const struct mono_time *t1, + const struct mono_time *t2) +{ + return mono_time_cmp(t1, t2) < 0; +} + +/* Return the difference between now and t. */ +static inline struct rela_time current_time_from(const struct mono_time *t) +{ + struct mono_time now; + + timer_monotonic_get(&now); + return mono_time_diff(t, &now); + +} + +static inline long rela_time_in_microseconds(const struct rela_time *rt) +{ + return rt->microseconds; +} + +#endif /* TIMER_H */ -- cgit v1.2.3