2 * Copyright (C) 2009 Intel Corporation.
3 * Author: Patrick Ohly <patrick.ohly@intel.com>
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.
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.
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.
20 #include <linux/timecompare.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/math64.h>
26 * fixed point arithmetic scale factor for skew
28 * Usually one would measure skew in ppb (parts per billion, 1e9), but
29 * using a factor of 2 simplifies the math.
31 #define TIMECOMPARE_SKEW_RESOLUTION (((s64)1)<<30)
33 ktime_t timecompare_transform(struct timecompare *sync,
38 nsec = source_tstamp + sync->offset;
39 nsec += (s64)(source_tstamp - sync->last_update) * sync->skew /
40 TIMECOMPARE_SKEW_RESOLUTION;
42 return ns_to_ktime(nsec);
44 EXPORT_SYMBOL_GPL(timecompare_transform);
46 int timecompare_offset(struct timecompare *sync,
50 u64 start_source = 0, end_source = 0;
54 } buffer[10], sample, *samples;
58 int num_samples = sync->num_samples;
60 if (num_samples > sizeof(buffer)/sizeof(buffer[0])) {
61 samples = kmalloc(sizeof(*samples) * num_samples, GFP_ATOMIC);
64 num_samples = sizeof(buffer)/sizeof(buffer[0]);
70 /* run until we have enough valid samples, but do not try forever */
77 start = sync->target();
78 ts = timecounter_read(sync->source);
84 /* ignore negative durations */
85 sample.duration_target = ktime_to_ns(ktime_sub(end, start));
86 if (sample.duration_target >= 0) {
88 * assume symetric delay to and from source:
89 * average target time corresponds to measured
93 (ktime_to_ns(end) + ktime_to_ns(start)) / 2 -
96 /* simple insertion sort based on duration */
99 if (samples[index].duration_target <
100 sample.duration_target)
102 samples[index + 1] = samples[index];
105 samples[index + 1] = sample;
110 if (counter >= num_samples || i >= 100000) {
116 *source_tstamp = (end_source + start_source) / 2;
118 /* remove outliers by only using 75% of the samples */
119 used = counter * 3 / 4;
123 /* calculate average */
125 for (index = 0; index < used; index++)
126 off += samples[index].offset;
127 *offset = div_s64(off, used);
130 if (samples && samples != buffer)
135 EXPORT_SYMBOL_GPL(timecompare_offset);
137 void __timecompare_update(struct timecompare *sync,
143 if (!timecompare_offset(sync, &offset, &average_time))
146 if (!sync->last_update) {
147 sync->last_update = average_time;
148 sync->offset = offset;
151 s64 delta_nsec = average_time - sync->last_update;
153 /* avoid division by negative or small deltas */
154 if (delta_nsec >= 10000) {
155 s64 delta_offset_nsec = offset - sync->offset;
156 s64 skew; /* delta_offset_nsec *
157 TIMECOMPARE_SKEW_RESOLUTION /
161 /* div_s64() is limited to 32 bit divisor */
162 skew = delta_offset_nsec * TIMECOMPARE_SKEW_RESOLUTION;
163 divisor = delta_nsec;
164 while (unlikely(divisor >= ((s64)1) << 32)) {
165 /* divide both by 2; beware, right shift
166 of negative value has undefined
167 behavior and can only be used for
168 the positive divisor */
169 skew = div_s64(skew, 2);
172 skew = div_s64(skew, divisor);
175 * Calculate new overall skew as 4/16 the
176 * old value and 12/16 the new one. This is
177 * a rather arbitrary tradeoff between
178 * only using the latest measurement (0/16 and
179 * 16/16) and even more weight on past measurements.
181 #define TIMECOMPARE_NEW_SKEW_PER_16 12
183 div_s64((16 - TIMECOMPARE_NEW_SKEW_PER_16) *
185 TIMECOMPARE_NEW_SKEW_PER_16 * skew,
187 sync->last_update = average_time;
188 sync->offset = offset;
192 EXPORT_SYMBOL_GPL(__timecompare_update);