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>
24 #include <linux/kernel.h>
27 * fixed point arithmetic scale factor for skew
29 * Usually one would measure skew in ppb (parts per billion, 1e9), but
30 * using a factor of 2 simplifies the math.
32 #define TIMECOMPARE_SKEW_RESOLUTION (((s64)1)<<30)
34 ktime_t
timecompare_transform(struct timecompare
*sync
,
39 nsec
= source_tstamp
+ sync
->offset
;
40 nsec
+= (s64
)(source_tstamp
- sync
->last_update
) * sync
->skew
/
41 TIMECOMPARE_SKEW_RESOLUTION
;
43 return ns_to_ktime(nsec
);
45 EXPORT_SYMBOL_GPL(timecompare_transform
);
47 int timecompare_offset(struct timecompare
*sync
,
51 u64 start_source
= 0, end_source
= 0;
55 } buffer
[10], sample
, *samples
;
59 int num_samples
= sync
->num_samples
;
61 if (num_samples
> ARRAY_SIZE(buffer
)) {
62 samples
= kmalloc(sizeof(*samples
) * num_samples
, GFP_ATOMIC
);
65 num_samples
= ARRAY_SIZE(buffer
);
71 /* run until we have enough valid samples, but do not try forever */
78 start
= sync
->target();
79 ts
= timecounter_read(sync
->source
);
85 /* ignore negative durations */
86 sample
.duration_target
= ktime_to_ns(ktime_sub(end
, start
));
87 if (sample
.duration_target
>= 0) {
89 * assume symetric delay to and from source:
90 * average target time corresponds to measured
94 (ktime_to_ns(end
) + ktime_to_ns(start
)) / 2 -
97 /* simple insertion sort based on duration */
100 if (samples
[index
].duration_target
<
101 sample
.duration_target
)
103 samples
[index
+ 1] = samples
[index
];
106 samples
[index
+ 1] = sample
;
111 if (counter
>= num_samples
|| i
>= 100000) {
117 *source_tstamp
= (end_source
+ start_source
) / 2;
119 /* remove outliers by only using 75% of the samples */
120 used
= counter
* 3 / 4;
124 /* calculate average */
126 for (index
= 0; index
< used
; index
++)
127 off
+= samples
[index
].offset
;
128 *offset
= div_s64(off
, used
);
131 if (samples
&& samples
!= buffer
)
136 EXPORT_SYMBOL_GPL(timecompare_offset
);
138 void __timecompare_update(struct timecompare
*sync
,
144 if (!timecompare_offset(sync
, &offset
, &average_time
))
147 if (!sync
->last_update
) {
148 sync
->last_update
= average_time
;
149 sync
->offset
= offset
;
152 s64 delta_nsec
= average_time
- sync
->last_update
;
154 /* avoid division by negative or small deltas */
155 if (delta_nsec
>= 10000) {
156 s64 delta_offset_nsec
= offset
- sync
->offset
;
157 s64 skew
; /* delta_offset_nsec *
158 TIMECOMPARE_SKEW_RESOLUTION /
162 /* div_s64() is limited to 32 bit divisor */
163 skew
= delta_offset_nsec
* TIMECOMPARE_SKEW_RESOLUTION
;
164 divisor
= delta_nsec
;
165 while (unlikely(divisor
>= ((s64
)1) << 32)) {
166 /* divide both by 2; beware, right shift
167 of negative value has undefined
168 behavior and can only be used for
169 the positive divisor */
170 skew
= div_s64(skew
, 2);
173 skew
= div_s64(skew
, divisor
);
176 * Calculate new overall skew as 4/16 the
177 * old value and 12/16 the new one. This is
178 * a rather arbitrary tradeoff between
179 * only using the latest measurement (0/16 and
180 * 16/16) and even more weight on past measurements.
182 #define TIMECOMPARE_NEW_SKEW_PER_16 12
184 div_s64((16 - TIMECOMPARE_NEW_SKEW_PER_16
) *
186 TIMECOMPARE_NEW_SKEW_PER_16
* skew
,
188 sync
->last_update
= average_time
;
189 sync
->offset
= offset
;
193 EXPORT_SYMBOL_GPL(__timecompare_update
);