Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
timecompare.h
Go to the documentation of this file.
1 /*
2  * Utility code which helps transforming between two different time
3  * bases, called "source" and "target" time in this code.
4  *
5  * Source time has to be provided via the timecounter API while target
6  * time is accessed via a function callback whose prototype
7  * intentionally matches ktime_get() and ktime_get_real(). These
8  * interfaces where chosen like this so that the code serves its
9  * initial purpose without additional glue code.
10  *
11  * This purpose is synchronizing a hardware clock in a NIC with system
12  * time, in order to implement the Precision Time Protocol (PTP,
13  * IEEE1588) with more accurate hardware assisted time stamping. In
14  * that context only synchronization against system time (=
15  * ktime_get_real()) is currently needed. But this utility code might
16  * become useful in other situations, which is why it was written as
17  * general purpose utility code.
18  *
19  * The source timecounter is assumed to return monotonically
20  * increasing time (but this code does its best to compensate if that
21  * is not the case) whereas target time may jump.
22  *
23  * The target time corresponding to a source time is determined by
24  * reading target time, reading source time, reading target time
25  * again, then assuming that average target time corresponds to source
26  * time. In other words, the assumption is that reading the source
27  * time is slow and involves equal time for sending the request and
28  * receiving the reply, whereas reading target time is assumed to be
29  * fast.
30  *
31  * Copyright (C) 2009 Intel Corporation.
32  * Author: Patrick Ohly <[email protected]>
33  *
34  * This program is free software; you can redistribute it and/or modify it
35  * under the terms and conditions of the GNU General Public License,
36  * version 2, as published by the Free Software Foundation.
37  *
38  * This program is distributed in the hope it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40  * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
41  * more details.
42  *
43  * You should have received a copy of the GNU General Public License along with
44  * this program; if not, write to the Free Software Foundation, Inc.,
45  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
46  */
47 #ifndef _LINUX_TIMECOMPARE_H
48 #define _LINUX_TIMECOMPARE_H
49 
50 #include <linux/clocksource.h>
51 #include <linux/ktime.h>
52 
73 struct timecompare {
77 
81 };
82 
90  u64 source_tstamp);
91 
102 extern int timecompare_offset(struct timecompare *sync,
103  s64 *offset,
104  u64 *source_tstamp);
105 
106 extern void __timecompare_update(struct timecompare *sync,
107  u64 source_tstamp);
108 
117 static inline void timecompare_update(struct timecompare *sync,
118  u64 source_tstamp)
119 {
120  if (!source_tstamp ||
121  (s64)(source_tstamp - sync->last_update) >= NSEC_PER_SEC)
122  __timecompare_update(sync, source_tstamp);
123 }
124 
125 #endif /* _LINUX_TIMECOMPARE_H */