timecompare.c 4.8 KB

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