timecompare.c 4.8 KB

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