timecompare.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2009 Intel Corporation.
  3. * Author: Patrick Ohly <patrick.ohly@intel.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/timecompare.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/math64.h>
  23. /*
  24. * fixed point arithmetic scale factor for skew
  25. *
  26. * Usually one would measure skew in ppb (parts per billion, 1e9), but
  27. * using a factor of 2 simplifies the math.
  28. */
  29. #define TIMECOMPARE_SKEW_RESOLUTION (((s64)1)<<30)
  30. ktime_t timecompare_transform(struct timecompare *sync,
  31. u64 source_tstamp)
  32. {
  33. u64 nsec;
  34. nsec = source_tstamp + sync->offset;
  35. nsec += (s64)(source_tstamp - sync->last_update) * sync->skew /
  36. TIMECOMPARE_SKEW_RESOLUTION;
  37. return ns_to_ktime(nsec);
  38. }
  39. EXPORT_SYMBOL_GPL(timecompare_transform);
  40. int timecompare_offset(struct timecompare *sync,
  41. s64 *offset,
  42. u64 *source_tstamp)
  43. {
  44. u64 start_source = 0, end_source = 0;
  45. struct {
  46. s64 offset;
  47. s64 duration_target;
  48. } buffer[10], sample, *samples;
  49. int counter = 0, i;
  50. int used;
  51. int index;
  52. int num_samples = sync->num_samples;
  53. if (num_samples > sizeof(buffer)/sizeof(buffer[0])) {
  54. samples = kmalloc(sizeof(*samples) * num_samples, GFP_ATOMIC);
  55. if (!samples) {
  56. samples = buffer;
  57. num_samples = sizeof(buffer)/sizeof(buffer[0]);
  58. }
  59. } else {
  60. samples = buffer;
  61. }
  62. /* run until we have enough valid samples, but do not try forever */
  63. i = 0;
  64. counter = 0;
  65. while (1) {
  66. u64 ts;
  67. ktime_t start, end;
  68. start = sync->target();
  69. ts = timecounter_read(sync->source);
  70. end = sync->target();
  71. if (!i)
  72. start_source = ts;
  73. /* ignore negative durations */
  74. sample.duration_target = ktime_to_ns(ktime_sub(end, start));
  75. if (sample.duration_target >= 0) {
  76. /*
  77. * assume symetric delay to and from source:
  78. * average target time corresponds to measured
  79. * source time
  80. */
  81. sample.offset =
  82. (ktime_to_ns(end) + ktime_to_ns(start)) / 2 -
  83. ts;
  84. /* simple insertion sort based on duration */
  85. index = counter - 1;
  86. while (index >= 0) {
  87. if (samples[index].duration_target <
  88. sample.duration_target)
  89. break;
  90. samples[index + 1] = samples[index];
  91. index--;
  92. }
  93. samples[index + 1] = sample;
  94. counter++;
  95. }
  96. i++;
  97. if (counter >= num_samples || i >= 100000) {
  98. end_source = ts;
  99. break;
  100. }
  101. }
  102. *source_tstamp = (end_source + start_source) / 2;
  103. /* remove outliers by only using 75% of the samples */
  104. used = counter * 3 / 4;
  105. if (!used)
  106. used = counter;
  107. if (used) {
  108. /* calculate average */
  109. s64 off = 0;
  110. for (index = 0; index < used; index++)
  111. off += samples[index].offset;
  112. *offset = div_s64(off, used);
  113. }
  114. if (samples && samples != buffer)
  115. kfree(samples);
  116. return used;
  117. }
  118. EXPORT_SYMBOL_GPL(timecompare_offset);
  119. void __timecompare_update(struct timecompare *sync,
  120. u64 source_tstamp)
  121. {
  122. s64 offset;
  123. u64 average_time;
  124. if (!timecompare_offset(sync, &offset, &average_time))
  125. return;
  126. if (!sync->last_update) {
  127. sync->last_update = average_time;
  128. sync->offset = offset;
  129. sync->skew = 0;
  130. } else {
  131. s64 delta_nsec = average_time - sync->last_update;
  132. /* avoid division by negative or small deltas */
  133. if (delta_nsec >= 10000) {
  134. s64 delta_offset_nsec = offset - sync->offset;
  135. s64 skew; /* delta_offset_nsec *
  136. TIMECOMPARE_SKEW_RESOLUTION /
  137. delta_nsec */
  138. u64 divisor;
  139. /* div_s64() is limited to 32 bit divisor */
  140. skew = delta_offset_nsec * TIMECOMPARE_SKEW_RESOLUTION;
  141. divisor = delta_nsec;
  142. while (unlikely(divisor >= ((s64)1) << 32)) {
  143. /* divide both by 2; beware, right shift
  144. of negative value has undefined
  145. behavior and can only be used for
  146. the positive divisor */
  147. skew = div_s64(skew, 2);
  148. divisor >>= 1;
  149. }
  150. skew = div_s64(skew, divisor);
  151. /*
  152. * Calculate new overall skew as 4/16 the
  153. * old value and 12/16 the new one. This is
  154. * a rather arbitrary tradeoff between
  155. * only using the latest measurement (0/16 and
  156. * 16/16) and even more weight on past measurements.
  157. */
  158. #define TIMECOMPARE_NEW_SKEW_PER_16 12
  159. sync->skew =
  160. div_s64((16 - TIMECOMPARE_NEW_SKEW_PER_16) *
  161. sync->skew +
  162. TIMECOMPARE_NEW_SKEW_PER_16 * skew,
  163. 16);
  164. sync->last_update = average_time;
  165. sync->offset = offset;
  166. }
  167. }
  168. }
  169. EXPORT_SYMBOL_GPL(__timecompare_update);