timecompare.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <patrick.ohly@intel.com>
  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. #include <linux/clocksource.h>
  50. #include <linux/ktime.h>
  51. /**
  52. * struct timecompare - stores state and configuration for the two clocks
  53. *
  54. * Initialize to zero, then set source/target/num_samples.
  55. *
  56. * Transformation between source time and target time is done with:
  57. * target_time = source_time + offset +
  58. * (source_time - last_update) * skew /
  59. * TIMECOMPARE_SKEW_RESOLUTION
  60. *
  61. * @source: used to get source time stamps via timecounter_read()
  62. * @target: function returning target time (for example, ktime_get
  63. * for monotonic time, or ktime_get_real for wall clock)
  64. * @num_samples: number of times that source time and target time are to
  65. * be compared when determining their offset
  66. * @offset: (target time - source time) at the time of the last update
  67. * @skew: average (target time - source time) / delta source time *
  68. * TIMECOMPARE_SKEW_RESOLUTION
  69. * @last_update: last source time stamp when time offset was measured
  70. */
  71. struct timecompare {
  72. struct timecounter *source;
  73. ktime_t (*target)(void);
  74. int num_samples;
  75. s64 offset;
  76. s64 skew;
  77. u64 last_update;
  78. };
  79. /**
  80. * timecompare_transform - transform source time stamp into target time base
  81. * @sync: context for time sync
  82. * @source_tstamp: the result of timecounter_read() or
  83. * timecounter_cyc2time()
  84. */
  85. extern ktime_t timecompare_transform(struct timecompare *sync,
  86. u64 source_tstamp);
  87. /**
  88. * timecompare_offset - measure current (target time - source time) offset
  89. * @sync: context for time sync
  90. * @offset: average offset during sample period returned here
  91. * @source_tstamp: average source time during sample period returned here
  92. *
  93. * Returns number of samples used. Might be zero (= no result) in the
  94. * unlikely case that target time was monotonically decreasing for all
  95. * samples (= broken).
  96. */
  97. extern int timecompare_offset(struct timecompare *sync,
  98. s64 *offset,
  99. u64 *source_tstamp);
  100. extern void __timecompare_update(struct timecompare *sync,
  101. u64 source_tstamp);
  102. /**
  103. * timecompare_update - update offset and skew by measuring current offset
  104. * @sync: context for time sync
  105. * @source_tstamp: the result of timecounter_read() or
  106. * timecounter_cyc2time(), pass zero to force update
  107. *
  108. * Updates are only done at most once per second.
  109. */
  110. static inline void timecompare_update(struct timecompare *sync,
  111. u64 source_tstamp)
  112. {
  113. if (!source_tstamp ||
  114. (s64)(source_tstamp - sync->last_update) >= NSEC_PER_SEC)
  115. __timecompare_update(sync, source_tstamp);
  116. }
  117. #endif /* _LINUX_TIMECOMPARE_H */