]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
4c7ee8de | 2 | /* |
4c7ee8de JS |
3 | * NTP state machine interfaces and logic. |
4 | * | |
5 | * This code was mainly moved from kernel/timer.c and kernel/time.c | |
6 | * Please see those files for relevant copyright info and historical | |
7 | * changelogs. | |
8 | */ | |
aa0ac365 | 9 | #include <linux/capability.h> |
7dffa3c6 | 10 | #include <linux/clocksource.h> |
eb3f938f | 11 | #include <linux/workqueue.h> |
53bbfa9e IM |
12 | #include <linux/hrtimer.h> |
13 | #include <linux/jiffies.h> | |
14 | #include <linux/math64.h> | |
15 | #include <linux/timex.h> | |
16 | #include <linux/time.h> | |
17 | #include <linux/mm.h> | |
025b40ab | 18 | #include <linux/module.h> |
023f333a | 19 | #include <linux/rtc.h> |
7e8eda73 | 20 | #include <linux/audit.h> |
4c7ee8de | 21 | |
aa6f9c59 | 22 | #include "ntp_internal.h" |
0af86465 D |
23 | #include "timekeeping_internal.h" |
24 | ||
68f66f97 TG |
25 | /** |
26 | * struct ntp_data - Structure holding all NTP related state | |
27 | * @tick_usec: USER_HZ period in microseconds | |
ec93ec22 TG |
28 | * @tick_length: Adjusted tick length |
29 | * @tick_length_base: Base value for @tick_length | |
bee18a23 TG |
30 | * @time_state: State of the clock synchronization |
31 | * @time_status: Clock status bits | |
d5143554 TG |
32 | * @time_offset: Time adjustment in nanoseconds |
33 | * @time_constant: PLL time constant | |
7891cf29 TG |
34 | * @time_maxerror: Maximum error in microseconds holding the NTP sync distance |
35 | * (NTP dispersion + delay / 2) | |
36 | * @time_esterror: Estimated error in microseconds holding NTP dispersion | |
a076b214 | 37 | * |
68f66f97 | 38 | * Protected by the timekeeping locks. |
b0ee7556 | 39 | */ |
68f66f97 TG |
40 | struct ntp_data { |
41 | unsigned long tick_usec; | |
ec93ec22 TG |
42 | u64 tick_length; |
43 | u64 tick_length_base; | |
bee18a23 TG |
44 | int time_state; |
45 | int time_status; | |
d5143554 TG |
46 | s64 time_offset; |
47 | long time_constant; | |
7891cf29 TG |
48 | long time_maxerror; |
49 | long time_esterror; | |
68f66f97 | 50 | }; |
b0ee7556 | 51 | |
68f66f97 TG |
52 | static struct ntp_data tk_ntp_data = { |
53 | .tick_usec = USER_TICK_USEC, | |
bee18a23 TG |
54 | .time_state = TIME_OK, |
55 | .time_status = STA_UNSYNC, | |
d5143554 | 56 | .time_constant = 2, |
7891cf29 TG |
57 | .time_maxerror = NTP_PHASE_LIMIT, |
58 | .time_esterror = NTP_PHASE_LIMIT, | |
68f66f97 | 59 | }; |
53bbfa9e | 60 | |
90bf361c | 61 | #define SECS_PER_DAY 86400 |
bbd12676 | 62 | #define MAX_TICKADJ 500LL /* usecs */ |
53bbfa9e | 63 | #define MAX_TICKADJ_SCALED \ |
bbd12676 | 64 | (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ) |
d897a4ab | 65 | #define MAX_TAI_OFFSET 100000 |
4c7ee8de | 66 | |
53bbfa9e IM |
67 | /* frequency offset (scaled nsecs/secs): */ |
68 | static s64 time_freq; | |
69 | ||
70 | /* time at last adjustment (secs): */ | |
0af86465 | 71 | static time64_t time_reftime; |
53bbfa9e | 72 | |
e1292ba1 | 73 | static long time_adjust; |
53bbfa9e | 74 | |
069569e0 IM |
75 | /* constant (boot-param configurable) NTP tick adjustment (upscaled) */ |
76 | static s64 ntp_tick_adj; | |
53bbfa9e | 77 | |
833f32d7 JS |
78 | /* second value of the next pending leapsecond, or TIME64_MAX if no leap */ |
79 | static time64_t ntp_next_leap_sec = TIME64_MAX; | |
80 | ||
025b40ab AG |
81 | #ifdef CONFIG_NTP_PPS |
82 | ||
83 | /* | |
84 | * The following variables are used when a pulse-per-second (PPS) signal | |
85 | * is available. They establish the engineering parameters of the clock | |
86 | * discipline loop when controlled by the PPS signal. | |
87 | */ | |
88 | #define PPS_VALID 10 /* PPS signal watchdog max (s) */ | |
89 | #define PPS_POPCORN 4 /* popcorn spike threshold (shift) */ | |
90 | #define PPS_INTMIN 2 /* min freq interval (s) (shift) */ | |
91 | #define PPS_INTMAX 8 /* max freq interval (s) (shift) */ | |
92 | #define PPS_INTCOUNT 4 /* number of consecutive good intervals to | |
93 | increase pps_shift or consecutive bad | |
94 | intervals to decrease it */ | |
95 | #define PPS_MAXWANDER 100000 /* max PPS freq wander (ns/s) */ | |
96 | ||
97 | static int pps_valid; /* signal watchdog counter */ | |
98 | static long pps_tf[3]; /* phase median filter */ | |
99 | static long pps_jitter; /* current jitter (ns) */ | |
7ec88e4b | 100 | static struct timespec64 pps_fbase; /* beginning of the last freq interval */ |
025b40ab AG |
101 | static int pps_shift; /* current interval duration (s) (shift) */ |
102 | static int pps_intcnt; /* interval counter */ | |
103 | static s64 pps_freq; /* frequency offset (scaled ns/s) */ | |
104 | static long pps_stabil; /* current stability (scaled ns/s) */ | |
105 | ||
106 | /* | |
107 | * PPS signal quality monitors | |
108 | */ | |
109 | static long pps_calcnt; /* calibration intervals */ | |
110 | static long pps_jitcnt; /* jitter limit exceeded */ | |
111 | static long pps_stbcnt; /* stability limit exceeded */ | |
112 | static long pps_errcnt; /* calibration errors */ | |
113 | ||
114 | ||
a0581cdb TG |
115 | /* |
116 | * PPS kernel consumer compensates the whole phase error immediately. | |
025b40ab AG |
117 | * Otherwise, reduce the offset by a fixed factor times the time constant. |
118 | */ | |
bee18a23 | 119 | static inline s64 ntp_offset_chunk(struct ntp_data *ntpdata, s64 offset) |
025b40ab | 120 | { |
bee18a23 | 121 | if (ntpdata->time_status & STA_PPSTIME && ntpdata->time_status & STA_PPSSIGNAL) |
025b40ab AG |
122 | return offset; |
123 | else | |
d5143554 | 124 | return shift_right(offset, SHIFT_PLL + ntpdata->time_constant); |
025b40ab AG |
125 | } |
126 | ||
127 | static inline void pps_reset_freq_interval(void) | |
128 | { | |
a0581cdb | 129 | /* The PPS calibration interval may end surprisingly early */ |
025b40ab AG |
130 | pps_shift = PPS_INTMIN; |
131 | pps_intcnt = 0; | |
132 | } | |
133 | ||
134 | /** | |
135 | * pps_clear - Clears the PPS state variables | |
025b40ab AG |
136 | */ |
137 | static inline void pps_clear(void) | |
138 | { | |
139 | pps_reset_freq_interval(); | |
140 | pps_tf[0] = 0; | |
141 | pps_tf[1] = 0; | |
142 | pps_tf[2] = 0; | |
143 | pps_fbase.tv_sec = pps_fbase.tv_nsec = 0; | |
144 | pps_freq = 0; | |
145 | } | |
146 | ||
a0581cdb TG |
147 | /* |
148 | * Decrease pps_valid to indicate that another second has passed since the | |
149 | * last PPS signal. When it reaches 0, indicate that PPS signal is missing. | |
025b40ab | 150 | */ |
bee18a23 | 151 | static inline void pps_dec_valid(struct ntp_data *ntpdata) |
025b40ab AG |
152 | { |
153 | if (pps_valid > 0) | |
154 | pps_valid--; | |
155 | else { | |
bee18a23 TG |
156 | ntpdata->time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER | |
157 | STA_PPSWANDER | STA_PPSERROR); | |
025b40ab AG |
158 | pps_clear(); |
159 | } | |
160 | } | |
161 | ||
162 | static inline void pps_set_freq(s64 freq) | |
163 | { | |
164 | pps_freq = freq; | |
165 | } | |
166 | ||
48c3c65f | 167 | static inline bool is_error_status(int status) |
025b40ab | 168 | { |
ea54bca3 | 169 | return (status & (STA_UNSYNC|STA_CLOCKERR)) |
a0581cdb TG |
170 | /* |
171 | * PPS signal lost when either PPS time or PPS frequency | |
172 | * synchronization requested | |
025b40ab | 173 | */ |
ea54bca3 GS |
174 | || ((status & (STA_PPSFREQ|STA_PPSTIME)) |
175 | && !(status & STA_PPSSIGNAL)) | |
a0581cdb TG |
176 | /* |
177 | * PPS jitter exceeded when PPS time synchronization | |
178 | * requested | |
179 | */ | |
ea54bca3 | 180 | || ((status & (STA_PPSTIME|STA_PPSJITTER)) |
025b40ab | 181 | == (STA_PPSTIME|STA_PPSJITTER)) |
a0581cdb TG |
182 | /* |
183 | * PPS wander exceeded or calibration error when PPS | |
184 | * frequency synchronization requested | |
025b40ab | 185 | */ |
ea54bca3 GS |
186 | || ((status & STA_PPSFREQ) |
187 | && (status & (STA_PPSWANDER|STA_PPSERROR))); | |
025b40ab AG |
188 | } |
189 | ||
bee18a23 | 190 | static inline void pps_fill_timex(struct ntp_data *ntpdata, struct __kernel_timex *txc) |
025b40ab AG |
191 | { |
192 | txc->ppsfreq = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) * | |
193 | PPM_SCALE_INV, NTP_SCALE_SHIFT); | |
194 | txc->jitter = pps_jitter; | |
bee18a23 | 195 | if (!(ntpdata->time_status & STA_NANO)) |
ead25417 | 196 | txc->jitter = pps_jitter / NSEC_PER_USEC; |
025b40ab AG |
197 | txc->shift = pps_shift; |
198 | txc->stabil = pps_stabil; | |
199 | txc->jitcnt = pps_jitcnt; | |
200 | txc->calcnt = pps_calcnt; | |
201 | txc->errcnt = pps_errcnt; | |
202 | txc->stbcnt = pps_stbcnt; | |
203 | } | |
204 | ||
205 | #else /* !CONFIG_NTP_PPS */ | |
206 | ||
d5143554 | 207 | static inline s64 ntp_offset_chunk(struct ntp_data *ntpdata, s64 offset) |
025b40ab | 208 | { |
d5143554 | 209 | return shift_right(offset, SHIFT_PLL + ntpdata->time_constant); |
025b40ab AG |
210 | } |
211 | ||
212 | static inline void pps_reset_freq_interval(void) {} | |
213 | static inline void pps_clear(void) {} | |
bee18a23 | 214 | static inline void pps_dec_valid(struct ntp_data *ntpdata) {} |
025b40ab AG |
215 | static inline void pps_set_freq(s64 freq) {} |
216 | ||
48c3c65f | 217 | static inline bool is_error_status(int status) |
025b40ab AG |
218 | { |
219 | return status & (STA_UNSYNC|STA_CLOCKERR); | |
220 | } | |
221 | ||
bee18a23 | 222 | static inline void pps_fill_timex(struct ntp_data *ntpdata, struct __kernel_timex *txc) |
025b40ab AG |
223 | { |
224 | /* PPS is not implemented, so these are zero */ | |
225 | txc->ppsfreq = 0; | |
226 | txc->jitter = 0; | |
227 | txc->shift = 0; | |
228 | txc->stabil = 0; | |
229 | txc->jitcnt = 0; | |
230 | txc->calcnt = 0; | |
231 | txc->errcnt = 0; | |
232 | txc->stbcnt = 0; | |
233 | } | |
234 | ||
235 | #endif /* CONFIG_NTP_PPS */ | |
236 | ||
9ce616aa | 237 | /* |
a849a027 TG |
238 | * Update tick_length and tick_length_base, based on tick_usec, ntp_tick_adj and |
239 | * time_freq: | |
9ce616aa | 240 | */ |
68f66f97 | 241 | static void ntp_update_frequency(struct ntp_data *ntpdata) |
70bc42f9 | 242 | { |
68f66f97 | 243 | u64 second_length, new_base, tick_usec = (u64)ntpdata->tick_usec; |
9ce616aa | 244 | |
68f66f97 | 245 | second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << NTP_SCALE_SHIFT; |
9ce616aa | 246 | |
069569e0 | 247 | second_length += ntp_tick_adj; |
9ce616aa | 248 | second_length += time_freq; |
70bc42f9 | 249 | |
bc26c31d | 250 | new_base = div_u64(second_length, NTP_INTERVAL_FREQ); |
fdcedf7b JS |
251 | |
252 | /* | |
a0581cdb TG |
253 | * Don't wait for the next second_overflow, apply the change to the |
254 | * tick length immediately: | |
fdcedf7b | 255 | */ |
ec93ec22 TG |
256 | ntpdata->tick_length += new_base - ntpdata->tick_length_base; |
257 | ntpdata->tick_length_base = new_base; | |
70bc42f9 AB |
258 | } |
259 | ||
bee18a23 | 260 | static inline s64 ntp_update_offset_fll(struct ntp_data *ntpdata, s64 offset64, long secs) |
f939890b | 261 | { |
bee18a23 | 262 | ntpdata->time_status &= ~STA_MODE; |
f939890b IM |
263 | |
264 | if (secs < MINSEC) | |
478b7aab | 265 | return 0; |
f939890b | 266 | |
bee18a23 | 267 | if (!(ntpdata->time_status & STA_FLL) && (secs <= MAXSEC)) |
478b7aab | 268 | return 0; |
f939890b | 269 | |
bee18a23 | 270 | ntpdata->time_status |= STA_MODE; |
f939890b | 271 | |
a078c6d0 | 272 | return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs); |
f939890b IM |
273 | } |
274 | ||
bee18a23 | 275 | static void ntp_update_offset(struct ntp_data *ntpdata, long offset) |
ee9851b2 | 276 | { |
136bccbc TG |
277 | s64 freq_adj, offset64; |
278 | long secs, real_secs; | |
ee9851b2 | 279 | |
bee18a23 | 280 | if (!(ntpdata->time_status & STA_PLL)) |
ee9851b2 RZ |
281 | return; |
282 | ||
bee18a23 | 283 | if (!(ntpdata->time_status & STA_NANO)) { |
52d189f1 SL |
284 | /* Make sure the multiplication below won't overflow */ |
285 | offset = clamp(offset, -USEC_PER_SEC, USEC_PER_SEC); | |
9f14f669 | 286 | offset *= NSEC_PER_USEC; |
52d189f1 | 287 | } |
ee9851b2 | 288 | |
a0581cdb | 289 | /* Scale the phase adjustment and clamp to the operating range. */ |
52d189f1 | 290 | offset = clamp(offset, -MAXPHASE, MAXPHASE); |
ee9851b2 RZ |
291 | |
292 | /* | |
293 | * Select how the frequency is to be controlled | |
294 | * and in which mode (PLL or FLL). | |
295 | */ | |
136bccbc TG |
296 | real_secs = __ktime_get_real_seconds(); |
297 | secs = (long)(real_secs - time_reftime); | |
bee18a23 | 298 | if (unlikely(ntpdata->time_status & STA_FREQHOLD)) |
c7986acb IM |
299 | secs = 0; |
300 | ||
136bccbc | 301 | time_reftime = real_secs; |
ee9851b2 | 302 | |
f939890b | 303 | offset64 = offset; |
bee18a23 | 304 | freq_adj = ntp_update_offset_fll(ntpdata, offset64, secs); |
f939890b | 305 | |
8af3c153 ML |
306 | /* |
307 | * Clamp update interval to reduce PLL gain with low | |
308 | * sampling rate (e.g. intermittent network connection) | |
309 | * to avoid instability. | |
310 | */ | |
d5143554 TG |
311 | if (unlikely(secs > 1 << (SHIFT_PLL + 1 + ntpdata->time_constant))) |
312 | secs = 1 << (SHIFT_PLL + 1 + ntpdata->time_constant); | |
8af3c153 ML |
313 | |
314 | freq_adj += (offset64 * secs) << | |
d5143554 | 315 | (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + ntpdata->time_constant)); |
f939890b IM |
316 | |
317 | freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED); | |
318 | ||
319 | time_freq = max(freq_adj, -MAXFREQ_SCALED); | |
320 | ||
d5143554 | 321 | ntpdata->time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ); |
ee9851b2 RZ |
322 | } |
323 | ||
68f66f97 | 324 | static void __ntp_clear(struct ntp_data *ntpdata) |
b0ee7556 | 325 | { |
a0581cdb | 326 | /* Stop active adjtime() */ |
bee18a23 TG |
327 | time_adjust = 0; |
328 | ntpdata->time_status |= STA_UNSYNC; | |
7891cf29 TG |
329 | ntpdata->time_maxerror = NTP_PHASE_LIMIT; |
330 | ntpdata->time_esterror = NTP_PHASE_LIMIT; | |
b0ee7556 | 331 | |
68f66f97 | 332 | ntp_update_frequency(ntpdata); |
b0ee7556 | 333 | |
ec93ec22 | 334 | ntpdata->tick_length = ntpdata->tick_length_base; |
d5143554 | 335 | ntpdata->time_offset = 0; |
025b40ab | 336 | |
833f32d7 | 337 | ntp_next_leap_sec = TIME64_MAX; |
025b40ab AG |
338 | /* Clear PPS state variables */ |
339 | pps_clear(); | |
b0ee7556 RZ |
340 | } |
341 | ||
68f66f97 TG |
342 | /** |
343 | * ntp_clear - Clears the NTP state variables | |
344 | */ | |
345 | void ntp_clear(void) | |
346 | { | |
347 | __ntp_clear(&tk_ntp_data); | |
348 | } | |
349 | ||
ea7cf49a JS |
350 | |
351 | u64 ntp_tick_length(void) | |
352 | { | |
ec93ec22 | 353 | return tk_ntp_data.tick_length; |
ea7cf49a JS |
354 | } |
355 | ||
833f32d7 JS |
356 | /** |
357 | * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t | |
358 | * | |
359 | * Provides the time of the next leapsecond against CLOCK_REALTIME in | |
360 | * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending. | |
361 | */ | |
362 | ktime_t ntp_get_next_leap(void) | |
363 | { | |
bee18a23 | 364 | struct ntp_data *ntpdata = &tk_ntp_data; |
833f32d7 JS |
365 | ktime_t ret; |
366 | ||
bee18a23 | 367 | if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS)) |
833f32d7 | 368 | return ktime_set(ntp_next_leap_sec, 0); |
2456e855 | 369 | ret = KTIME_MAX; |
833f32d7 JS |
370 | return ret; |
371 | } | |
ea7cf49a | 372 | |
4c7ee8de | 373 | /* |
a0581cdb | 374 | * This routine handles the overflow of the microsecond field |
6b43ae8a JS |
375 | * |
376 | * The tricky bits of code to handle the accurate clock support | |
377 | * were provided by Dave Mills ([email protected]) of NTP fame. | |
378 | * They were originally developed for SUN and DEC kernels. | |
379 | * All the kudos should go to Dave for this stuff. | |
380 | * | |
381 | * Also handles leap second processing, and returns leap offset | |
4c7ee8de | 382 | */ |
c7963487 | 383 | int second_overflow(time64_t secs) |
4c7ee8de | 384 | { |
ec93ec22 | 385 | struct ntp_data *ntpdata = &tk_ntp_data; |
6b43ae8a | 386 | s64 delta; |
bd331268 | 387 | int leap = 0; |
c7963487 | 388 | s32 rem; |
6b43ae8a JS |
389 | |
390 | /* | |
391 | * Leap second processing. If in leap-insert state at the end of the | |
392 | * day, the system clock is set back one second; if in leap-delete | |
393 | * state, the system clock is set ahead one second. | |
394 | */ | |
bee18a23 | 395 | switch (ntpdata->time_state) { |
4c7ee8de | 396 | case TIME_OK: |
bee18a23 TG |
397 | if (ntpdata->time_status & STA_INS) { |
398 | ntpdata->time_state = TIME_INS; | |
c7963487 D |
399 | div_s64_rem(secs, SECS_PER_DAY, &rem); |
400 | ntp_next_leap_sec = secs + SECS_PER_DAY - rem; | |
bee18a23 TG |
401 | } else if (ntpdata->time_status & STA_DEL) { |
402 | ntpdata->time_state = TIME_DEL; | |
c7963487 D |
403 | div_s64_rem(secs + 1, SECS_PER_DAY, &rem); |
404 | ntp_next_leap_sec = secs + SECS_PER_DAY - rem; | |
833f32d7 | 405 | } |
4c7ee8de JS |
406 | break; |
407 | case TIME_INS: | |
bee18a23 | 408 | if (!(ntpdata->time_status & STA_INS)) { |
833f32d7 | 409 | ntp_next_leap_sec = TIME64_MAX; |
bee18a23 | 410 | ntpdata->time_state = TIME_OK; |
c7963487 | 411 | } else if (secs == ntp_next_leap_sec) { |
6b43ae8a | 412 | leap = -1; |
bee18a23 | 413 | ntpdata->time_state = TIME_OOP; |
38007dc0 | 414 | pr_notice("Clock: inserting leap second 23:59:60 UTC\n"); |
6b43ae8a | 415 | } |
4c7ee8de JS |
416 | break; |
417 | case TIME_DEL: | |
bee18a23 | 418 | if (!(ntpdata->time_status & STA_DEL)) { |
833f32d7 | 419 | ntp_next_leap_sec = TIME64_MAX; |
bee18a23 | 420 | ntpdata->time_state = TIME_OK; |
c7963487 | 421 | } else if (secs == ntp_next_leap_sec) { |
6b43ae8a | 422 | leap = 1; |
833f32d7 | 423 | ntp_next_leap_sec = TIME64_MAX; |
bee18a23 | 424 | ntpdata->time_state = TIME_WAIT; |
38007dc0 | 425 | pr_notice("Clock: deleting leap second 23:59:59 UTC\n"); |
6b43ae8a | 426 | } |
4c7ee8de JS |
427 | break; |
428 | case TIME_OOP: | |
833f32d7 | 429 | ntp_next_leap_sec = TIME64_MAX; |
bee18a23 | 430 | ntpdata->time_state = TIME_WAIT; |
6b43ae8a | 431 | break; |
4c7ee8de | 432 | case TIME_WAIT: |
bee18a23 TG |
433 | if (!(ntpdata->time_status & (STA_INS | STA_DEL))) |
434 | ntpdata->time_state = TIME_OK; | |
7dffa3c6 RZ |
435 | break; |
436 | } | |
bd331268 | 437 | |
7dffa3c6 | 438 | /* Bump the maxerror field */ |
7891cf29 TG |
439 | ntpdata->time_maxerror += MAXFREQ / NSEC_PER_USEC; |
440 | if (ntpdata->time_maxerror > NTP_PHASE_LIMIT) { | |
441 | ntpdata->time_maxerror = NTP_PHASE_LIMIT; | |
bee18a23 | 442 | ntpdata->time_status |= STA_UNSYNC; |
4c7ee8de JS |
443 | } |
444 | ||
025b40ab | 445 | /* Compute the phase adjustment for the next second */ |
ec93ec22 | 446 | ntpdata->tick_length = ntpdata->tick_length_base; |
39854fe8 | 447 | |
d5143554 TG |
448 | delta = ntp_offset_chunk(ntpdata, ntpdata->time_offset); |
449 | ntpdata->time_offset -= delta; | |
ec93ec22 | 450 | ntpdata->tick_length += delta; |
4c7ee8de | 451 | |
025b40ab | 452 | /* Check PPS signal */ |
bee18a23 | 453 | pps_dec_valid(ntpdata); |
025b40ab | 454 | |
3c972c24 | 455 | if (!time_adjust) |
bd331268 | 456 | goto out; |
3c972c24 IM |
457 | |
458 | if (time_adjust > MAX_TICKADJ) { | |
459 | time_adjust -= MAX_TICKADJ; | |
ec93ec22 | 460 | ntpdata->tick_length += MAX_TICKADJ_SCALED; |
bd331268 | 461 | goto out; |
4c7ee8de | 462 | } |
3c972c24 IM |
463 | |
464 | if (time_adjust < -MAX_TICKADJ) { | |
465 | time_adjust += MAX_TICKADJ; | |
ec93ec22 | 466 | ntpdata->tick_length -= MAX_TICKADJ_SCALED; |
bd331268 | 467 | goto out; |
3c972c24 IM |
468 | } |
469 | ||
ec93ec22 TG |
470 | ntpdata->tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ) |
471 | << NTP_SCALE_SHIFT; | |
3c972c24 | 472 | time_adjust = 0; |
6b43ae8a | 473 | |
bd331268 | 474 | out: |
6b43ae8a | 475 | return leap; |
4c7ee8de JS |
476 | } |
477 | ||
c9e6189f | 478 | #if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) |
0f295b06 | 479 | static void sync_hw_clock(struct work_struct *work); |
c9e6189f TG |
480 | static DECLARE_WORK(sync_work, sync_hw_clock); |
481 | static struct hrtimer sync_hrtimer; | |
e3fab2f3 | 482 | #define SYNC_PERIOD_NS (11ULL * 60 * NSEC_PER_SEC) |
0f295b06 | 483 | |
c9e6189f | 484 | static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer) |
0f295b06 | 485 | { |
24c242ec | 486 | queue_work(system_freezable_power_efficient_wq, &sync_work); |
0f295b06 | 487 | |
c9e6189f TG |
488 | return HRTIMER_NORESTART; |
489 | } | |
0f295b06 | 490 | |
c9e6189f TG |
491 | static void sched_sync_hw_clock(unsigned long offset_nsec, bool retry) |
492 | { | |
493 | ktime_t exp = ktime_set(ktime_get_real_seconds(), 0); | |
494 | ||
495 | if (retry) | |
e3fab2f3 | 496 | exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec); |
c9e6189f TG |
497 | else |
498 | exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec); | |
0f295b06 | 499 | |
c9e6189f | 500 | hrtimer_start(&sync_hrtimer, exp, HRTIMER_MODE_ABS); |
0f295b06 JG |
501 | } |
502 | ||
33e62e83 | 503 | /* |
69eca258 TG |
504 | * Check whether @now is correct versus the required time to update the RTC |
505 | * and calculate the value which needs to be written to the RTC so that the | |
506 | * next seconds increment of the RTC after the write is aligned with the next | |
507 | * seconds increment of clock REALTIME. | |
33e62e83 | 508 | * |
69eca258 | 509 | * tsched t1 write(t2.tv_sec - 1sec)) t2 RTC increments seconds |
33e62e83 | 510 | * |
69eca258 TG |
511 | * t2.tv_nsec == 0 |
512 | * tsched = t2 - set_offset_nsec | |
513 | * newval = t2 - NSEC_PER_SEC | |
514 | * | |
515 | * ==> neval = tsched + set_offset_nsec - NSEC_PER_SEC | |
516 | * | |
517 | * As the execution of this code is not guaranteed to happen exactly at | |
518 | * tsched this allows it to happen within a fuzzy region: | |
519 | * | |
520 | * abs(now - tsched) < FUZZ | |
521 | * | |
522 | * If @now is not inside the allowed window the function returns false. | |
33e62e83 | 523 | */ |
69eca258 | 524 | static inline bool rtc_tv_nsec_ok(unsigned long set_offset_nsec, |
33e62e83 TG |
525 | struct timespec64 *to_set, |
526 | const struct timespec64 *now) | |
527 | { | |
4bf07f65 | 528 | /* Allowed error in tv_nsec, arbitrarily set to 5 jiffies in ns. */ |
33e62e83 | 529 | const unsigned long TIME_SET_NSEC_FUZZ = TICK_NSEC * 5; |
69eca258 | 530 | struct timespec64 delay = {.tv_sec = -1, |
33e62e83 TG |
531 | .tv_nsec = set_offset_nsec}; |
532 | ||
533 | *to_set = timespec64_add(*now, delay); | |
534 | ||
535 | if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) { | |
536 | to_set->tv_nsec = 0; | |
537 | return true; | |
538 | } | |
539 | ||
540 | if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) { | |
541 | to_set->tv_sec++; | |
542 | to_set->tv_nsec = 0; | |
543 | return true; | |
544 | } | |
545 | return false; | |
546 | } | |
547 | ||
76e87d96 TG |
548 | #ifdef CONFIG_GENERIC_CMOS_UPDATE |
549 | int __weak update_persistent_clock64(struct timespec64 now64) | |
550 | { | |
551 | return -ENODEV; | |
552 | } | |
553 | #else | |
554 | static inline int update_persistent_clock64(struct timespec64 now64) | |
555 | { | |
556 | return -ENODEV; | |
557 | } | |
558 | #endif | |
559 | ||
33e62e83 | 560 | #ifdef CONFIG_RTC_SYSTOHC |
76e87d96 TG |
561 | /* Save NTP synchronized time to the RTC */ |
562 | static int update_rtc(struct timespec64 *to_set, unsigned long *offset_nsec) | |
33e62e83 TG |
563 | { |
564 | struct rtc_device *rtc; | |
565 | struct rtc_time tm; | |
33e62e83 | 566 | int err = -ENODEV; |
33e62e83 TG |
567 | |
568 | rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE); | |
569 | if (!rtc) | |
76e87d96 | 570 | return -ENODEV; |
33e62e83 TG |
571 | |
572 | if (!rtc->ops || !rtc->ops->set_time) | |
573 | goto out_close; | |
574 | ||
76e87d96 TG |
575 | /* First call might not have the correct offset */ |
576 | if (*offset_nsec == rtc->set_offset_nsec) { | |
577 | rtc_time64_to_tm(to_set->tv_sec, &tm); | |
578 | err = rtc_set_time(rtc, &tm); | |
579 | } else { | |
580 | /* Store the update offset and let the caller try again */ | |
581 | *offset_nsec = rtc->set_offset_nsec; | |
582 | err = -EAGAIN; | |
33e62e83 | 583 | } |
33e62e83 TG |
584 | out_close: |
585 | rtc_class_close(rtc); | |
33e62e83 TG |
586 | return err; |
587 | } | |
33e62e83 | 588 | #else |
76e87d96 | 589 | static inline int update_rtc(struct timespec64 *to_set, unsigned long *offset_nsec) |
3c00a1fe | 590 | { |
92661788 | 591 | return -ENODEV; |
3c00a1fe XP |
592 | } |
593 | #endif | |
594 | ||
48c3c65f TG |
595 | /** |
596 | * ntp_synced - Tells whether the NTP status is not UNSYNC | |
597 | * Returns: true if not UNSYNC, false otherwise | |
598 | */ | |
599 | static inline bool ntp_synced(void) | |
600 | { | |
bee18a23 | 601 | return !(tk_ntp_data.time_status & STA_UNSYNC); |
48c3c65f TG |
602 | } |
603 | ||
0f295b06 JG |
604 | /* |
605 | * If we have an externally synchronized Linux clock, then update RTC clock | |
606 | * accordingly every ~11 minutes. Generally RTCs can only store second | |
607 | * precision, but many RTCs will adjust the phase of their second tick to | |
608 | * match the moment of update. This infrastructure arranges to call to the RTC | |
609 | * set at the correct moment to phase synchronize the RTC second tick over | |
610 | * with the kernel clock. | |
611 | */ | |
612 | static void sync_hw_clock(struct work_struct *work) | |
613 | { | |
76e87d96 TG |
614 | /* |
615 | * The default synchronization offset is 500ms for the deprecated | |
616 | * update_persistent_clock64() under the assumption that it uses | |
617 | * the infamous CMOS clock (MC146818). | |
618 | */ | |
619 | static unsigned long offset_nsec = NSEC_PER_SEC / 2; | |
620 | struct timespec64 now, to_set; | |
621 | int res = -EAGAIN; | |
622 | ||
c9e6189f TG |
623 | /* |
624 | * Don't update if STA_UNSYNC is set and if ntp_notify_cmos_timer() | |
625 | * managed to schedule the work between the timer firing and the | |
626 | * work being able to rearm the timer. Wait for the timer to expire. | |
627 | */ | |
628 | if (!ntp_synced() || hrtimer_is_queued(&sync_hrtimer)) | |
0f295b06 | 629 | return; |
82644459 | 630 | |
76e87d96 TG |
631 | ktime_get_real_ts64(&now); |
632 | /* If @now is not in the allowed window, try again */ | |
633 | if (!rtc_tv_nsec_ok(offset_nsec, &to_set, &now)) | |
634 | goto rearm; | |
635 | ||
636 | /* Take timezone adjusted RTCs into account */ | |
637 | if (persistent_clock_is_local) | |
638 | to_set.tv_sec -= (sys_tz.tz_minuteswest * 60); | |
639 | ||
640 | /* Try the legacy RTC first. */ | |
641 | res = update_persistent_clock64(to_set); | |
642 | if (res != -ENODEV) | |
643 | goto rearm; | |
0f295b06 | 644 | |
76e87d96 TG |
645 | /* Try the RTC class */ |
646 | res = update_rtc(&to_set, &offset_nsec); | |
647 | if (res == -ENODEV) | |
648 | return; | |
649 | rearm: | |
650 | sched_sync_hw_clock(offset_nsec, res != 0); | |
82644459 TG |
651 | } |
652 | ||
35b603f8 | 653 | void ntp_notify_cmos_timer(bool offset_set) |
4c7ee8de | 654 | { |
35b603f8 BR |
655 | /* |
656 | * If the time jumped (using ADJ_SETOFFSET) cancels sync timer, | |
657 | * which may have been running if the time was synchronized | |
658 | * prior to the ADJ_SETOFFSET call. | |
659 | */ | |
660 | if (offset_set) | |
661 | hrtimer_cancel(&sync_hrtimer); | |
662 | ||
c9e6189f TG |
663 | /* |
664 | * When the work is currently executed but has not yet the timer | |
665 | * rearmed this queues the work immediately again. No big issue, | |
666 | * just a pointless work scheduled. | |
667 | */ | |
668 | if (ntp_synced() && !hrtimer_is_queued(&sync_hrtimer)) | |
24c242ec | 669 | queue_work(system_freezable_power_efficient_wq, &sync_work); |
c9e6189f | 670 | } |
82644459 | 671 | |
c9e6189f TG |
672 | static void __init ntp_init_cmos_sync(void) |
673 | { | |
674 | hrtimer_init(&sync_hrtimer, CLOCK_REALTIME, HRTIMER_MODE_ABS); | |
675 | sync_hrtimer.function = sync_timer_callback; | |
0f295b06 | 676 | } |
c9e6189f TG |
677 | #else /* CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) */ |
678 | static inline void __init ntp_init_cmos_sync(void) { } | |
679 | #endif /* !CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) */ | |
80f22571 IM |
680 | |
681 | /* | |
682 | * Propagate a new txc->status value into the NTP state: | |
683 | */ | |
bee18a23 | 684 | static inline void process_adj_status(struct ntp_data *ntpdata, const struct __kernel_timex *txc) |
80f22571 | 685 | { |
bee18a23 TG |
686 | if ((ntpdata->time_status & STA_PLL) && !(txc->status & STA_PLL)) { |
687 | ntpdata->time_state = TIME_OK; | |
688 | ntpdata->time_status = STA_UNSYNC; | |
833f32d7 | 689 | ntp_next_leap_sec = TIME64_MAX; |
a0581cdb | 690 | /* Restart PPS frequency calibration */ |
025b40ab | 691 | pps_reset_freq_interval(); |
80f22571 | 692 | } |
80f22571 IM |
693 | |
694 | /* | |
695 | * If we turn on PLL adjustments then reset the | |
696 | * reference time to current time. | |
697 | */ | |
bee18a23 | 698 | if (!(ntpdata->time_status & STA_PLL) && (txc->status & STA_PLL)) |
0af86465 | 699 | time_reftime = __ktime_get_real_seconds(); |
80f22571 | 700 | |
bee18a23 TG |
701 | /* only set allowed bits */ |
702 | ntpdata->time_status &= STA_RONLY; | |
703 | ntpdata->time_status |= txc->status & ~STA_RONLY; | |
80f22571 | 704 | } |
cd5398be | 705 | |
68f66f97 | 706 | static inline void process_adjtimex_modes(struct ntp_data *ntpdata, const struct __kernel_timex *txc, |
ead25417 | 707 | s32 *time_tai) |
80f22571 IM |
708 | { |
709 | if (txc->modes & ADJ_STATUS) | |
bee18a23 | 710 | process_adj_status(ntpdata, txc); |
80f22571 IM |
711 | |
712 | if (txc->modes & ADJ_NANO) | |
bee18a23 | 713 | ntpdata->time_status |= STA_NANO; |
e9629165 | 714 | |
80f22571 | 715 | if (txc->modes & ADJ_MICRO) |
bee18a23 | 716 | ntpdata->time_status &= ~STA_NANO; |
80f22571 IM |
717 | |
718 | if (txc->modes & ADJ_FREQUENCY) { | |
2b9d1496 | 719 | time_freq = txc->freq * PPM_SCALE; |
80f22571 IM |
720 | time_freq = min(time_freq, MAXFREQ_SCALED); |
721 | time_freq = max(time_freq, -MAXFREQ_SCALED); | |
a0581cdb | 722 | /* Update pps_freq */ |
025b40ab | 723 | pps_set_freq(time_freq); |
80f22571 IM |
724 | } |
725 | ||
726 | if (txc->modes & ADJ_MAXERROR) | |
7891cf29 | 727 | ntpdata->time_maxerror = clamp(txc->maxerror, 0, NTP_PHASE_LIMIT); |
e9629165 | 728 | |
80f22571 | 729 | if (txc->modes & ADJ_ESTERROR) |
7891cf29 | 730 | ntpdata->time_esterror = clamp(txc->esterror, 0, NTP_PHASE_LIMIT); |
80f22571 IM |
731 | |
732 | if (txc->modes & ADJ_TIMECONST) { | |
d5143554 | 733 | ntpdata->time_constant = clamp(txc->constant, 0, MAXTC); |
bee18a23 | 734 | if (!(ntpdata->time_status & STA_NANO)) |
d5143554 TG |
735 | ntpdata->time_constant += 4; |
736 | ntpdata->time_constant = clamp(ntpdata->time_constant, 0, MAXTC); | |
80f22571 IM |
737 | } |
738 | ||
bee18a23 | 739 | if (txc->modes & ADJ_TAI && txc->constant >= 0 && txc->constant <= MAX_TAI_OFFSET) |
cc244dda | 740 | *time_tai = txc->constant; |
80f22571 IM |
741 | |
742 | if (txc->modes & ADJ_OFFSET) | |
bee18a23 | 743 | ntp_update_offset(ntpdata, txc->offset); |
e9629165 | 744 | |
80f22571 | 745 | if (txc->modes & ADJ_TICK) |
68f66f97 | 746 | ntpdata->tick_usec = txc->tick; |
80f22571 IM |
747 | |
748 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) | |
68f66f97 | 749 | ntp_update_frequency(ntpdata); |
80f22571 IM |
750 | } |
751 | ||
ad460967 | 752 | /* |
a0581cdb | 753 | * adjtimex() mainly allows reading (and writing, if superuser) of |
ad460967 JS |
754 | * kernel time-keeping variables. used by xntpd. |
755 | */ | |
ead25417 | 756 | int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, |
7e8eda73 | 757 | s32 *time_tai, struct audit_ntp_data *ad) |
ad460967 | 758 | { |
68f66f97 | 759 | struct ntp_data *ntpdata = &tk_ntp_data; |
ad460967 JS |
760 | int result; |
761 | ||
916c7a85 RZ |
762 | if (txc->modes & ADJ_ADJTIME) { |
763 | long save_adjust = time_adjust; | |
764 | ||
765 | if (!(txc->modes & ADJ_OFFSET_READONLY)) { | |
766 | /* adjtime() is independent from ntp_adjtime() */ | |
767 | time_adjust = txc->offset; | |
68f66f97 | 768 | ntp_update_frequency(ntpdata); |
7e8eda73 OM |
769 | |
770 | audit_ntp_set_old(ad, AUDIT_NTP_ADJUST, save_adjust); | |
771 | audit_ntp_set_new(ad, AUDIT_NTP_ADJUST, time_adjust); | |
916c7a85 RZ |
772 | } |
773 | txc->offset = save_adjust; | |
e9629165 | 774 | } else { |
e9629165 | 775 | /* If there are input parameters, then process them: */ |
7e8eda73 | 776 | if (txc->modes) { |
d5143554 | 777 | audit_ntp_set_old(ad, AUDIT_NTP_OFFSET, ntpdata->time_offset); |
7e8eda73 | 778 | audit_ntp_set_old(ad, AUDIT_NTP_FREQ, time_freq); |
bee18a23 | 779 | audit_ntp_set_old(ad, AUDIT_NTP_STATUS, ntpdata->time_status); |
7e8eda73 | 780 | audit_ntp_set_old(ad, AUDIT_NTP_TAI, *time_tai); |
68f66f97 | 781 | audit_ntp_set_old(ad, AUDIT_NTP_TICK, ntpdata->tick_usec); |
7e8eda73 | 782 | |
68f66f97 | 783 | process_adjtimex_modes(ntpdata, txc, time_tai); |
eea83d89 | 784 | |
d5143554 | 785 | audit_ntp_set_new(ad, AUDIT_NTP_OFFSET, ntpdata->time_offset); |
7e8eda73 | 786 | audit_ntp_set_new(ad, AUDIT_NTP_FREQ, time_freq); |
bee18a23 | 787 | audit_ntp_set_new(ad, AUDIT_NTP_STATUS, ntpdata->time_status); |
7e8eda73 | 788 | audit_ntp_set_new(ad, AUDIT_NTP_TAI, *time_tai); |
68f66f97 | 789 | audit_ntp_set_new(ad, AUDIT_NTP_TICK, ntpdata->tick_usec); |
7e8eda73 OM |
790 | } |
791 | ||
d5143554 | 792 | txc->offset = shift_right(ntpdata->time_offset * NTP_INTERVAL_FREQ, NTP_SCALE_SHIFT); |
bee18a23 | 793 | if (!(ntpdata->time_status & STA_NANO)) |
ead25417 | 794 | txc->offset = (u32)txc->offset / NSEC_PER_USEC; |
e9629165 | 795 | } |
916c7a85 | 796 | |
bee18a23 TG |
797 | result = ntpdata->time_state; |
798 | if (is_error_status(ntpdata->time_status)) | |
4c7ee8de JS |
799 | result = TIME_ERROR; |
800 | ||
d40e944c | 801 | txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) * |
2b9d1496 | 802 | PPM_SCALE_INV, NTP_SCALE_SHIFT); |
7891cf29 TG |
803 | txc->maxerror = ntpdata->time_maxerror; |
804 | txc->esterror = ntpdata->time_esterror; | |
bee18a23 | 805 | txc->status = ntpdata->time_status; |
d5143554 | 806 | txc->constant = ntpdata->time_constant; |
70bc42f9 | 807 | txc->precision = 1; |
074b3b87 | 808 | txc->tolerance = MAXFREQ_SCALED / PPM_SCALE; |
68f66f97 | 809 | txc->tick = ntpdata->tick_usec; |
87ace39b | 810 | txc->tai = *time_tai; |
4c7ee8de | 811 | |
a0581cdb | 812 | /* Fill PPS status fields */ |
bee18a23 | 813 | pps_fill_timex(ntpdata, txc); |
e9629165 | 814 | |
2f584134 | 815 | txc->time.tv_sec = ts->tv_sec; |
87ace39b | 816 | txc->time.tv_usec = ts->tv_nsec; |
bee18a23 | 817 | if (!(ntpdata->time_status & STA_NANO)) |
ead25417 | 818 | txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC; |
ee9851b2 | 819 | |
96efdcf2 JS |
820 | /* Handle leapsec adjustments */ |
821 | if (unlikely(ts->tv_sec >= ntp_next_leap_sec)) { | |
bee18a23 | 822 | if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS)) { |
96efdcf2 JS |
823 | result = TIME_OOP; |
824 | txc->tai++; | |
825 | txc->time.tv_sec--; | |
826 | } | |
bee18a23 | 827 | if ((ntpdata->time_state == TIME_DEL) && (ntpdata->time_status & STA_DEL)) { |
96efdcf2 JS |
828 | result = TIME_WAIT; |
829 | txc->tai--; | |
830 | txc->time.tv_sec++; | |
831 | } | |
bee18a23 | 832 | if ((ntpdata->time_state == TIME_OOP) && (ts->tv_sec == ntp_next_leap_sec)) |
96efdcf2 | 833 | result = TIME_WAIT; |
96efdcf2 JS |
834 | } |
835 | ||
ee9851b2 | 836 | return result; |
4c7ee8de | 837 | } |
10a398d0 | 838 | |
025b40ab AG |
839 | #ifdef CONFIG_NTP_PPS |
840 | ||
a0581cdb TG |
841 | /* |
842 | * struct pps_normtime is basically a struct timespec, but it is | |
025b40ab AG |
843 | * semantically different (and it is the reason why it was invented): |
844 | * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] | |
a0581cdb TG |
845 | * while timespec.tv_nsec has a range of [0, NSEC_PER_SEC) |
846 | */ | |
025b40ab | 847 | struct pps_normtime { |
7ec88e4b | 848 | s64 sec; /* seconds */ |
025b40ab AG |
849 | long nsec; /* nanoseconds */ |
850 | }; | |
851 | ||
a0581cdb TG |
852 | /* |
853 | * Normalize the timestamp so that nsec is in the | |
854 | * [ -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval | |
855 | */ | |
7ec88e4b | 856 | static inline struct pps_normtime pps_normalize_ts(struct timespec64 ts) |
025b40ab AG |
857 | { |
858 | struct pps_normtime norm = { | |
859 | .sec = ts.tv_sec, | |
860 | .nsec = ts.tv_nsec | |
861 | }; | |
862 | ||
863 | if (norm.nsec > (NSEC_PER_SEC >> 1)) { | |
864 | norm.nsec -= NSEC_PER_SEC; | |
865 | norm.sec++; | |
866 | } | |
867 | ||
868 | return norm; | |
869 | } | |
870 | ||
a0581cdb | 871 | /* Get current phase correction and jitter */ |
025b40ab AG |
872 | static inline long pps_phase_filter_get(long *jitter) |
873 | { | |
874 | *jitter = pps_tf[0] - pps_tf[1]; | |
875 | if (*jitter < 0) | |
876 | *jitter = -*jitter; | |
877 | ||
878 | /* TODO: test various filters */ | |
879 | return pps_tf[0]; | |
880 | } | |
881 | ||
a0581cdb | 882 | /* Add the sample to the phase filter */ |
025b40ab AG |
883 | static inline void pps_phase_filter_add(long err) |
884 | { | |
885 | pps_tf[2] = pps_tf[1]; | |
886 | pps_tf[1] = pps_tf[0]; | |
887 | pps_tf[0] = err; | |
888 | } | |
889 | ||
a0581cdb TG |
890 | /* |
891 | * Decrease frequency calibration interval length. It is halved after four | |
892 | * consecutive unstable intervals. | |
025b40ab AG |
893 | */ |
894 | static inline void pps_dec_freq_interval(void) | |
895 | { | |
896 | if (--pps_intcnt <= -PPS_INTCOUNT) { | |
897 | pps_intcnt = -PPS_INTCOUNT; | |
898 | if (pps_shift > PPS_INTMIN) { | |
899 | pps_shift--; | |
900 | pps_intcnt = 0; | |
901 | } | |
902 | } | |
903 | } | |
904 | ||
a0581cdb TG |
905 | /* |
906 | * Increase frequency calibration interval length. It is doubled after | |
907 | * four consecutive stable intervals. | |
025b40ab AG |
908 | */ |
909 | static inline void pps_inc_freq_interval(void) | |
910 | { | |
911 | if (++pps_intcnt >= PPS_INTCOUNT) { | |
912 | pps_intcnt = PPS_INTCOUNT; | |
913 | if (pps_shift < PPS_INTMAX) { | |
914 | pps_shift++; | |
915 | pps_intcnt = 0; | |
916 | } | |
917 | } | |
918 | } | |
919 | ||
a0581cdb TG |
920 | /* |
921 | * Update clock frequency based on MONOTONIC_RAW clock PPS signal | |
025b40ab AG |
922 | * timestamps |
923 | * | |
924 | * At the end of the calibration interval the difference between the | |
925 | * first and last MONOTONIC_RAW clock timestamps divided by the length | |
926 | * of the interval becomes the frequency update. If the interval was | |
927 | * too long, the data are discarded. | |
928 | * Returns the difference between old and new frequency values. | |
929 | */ | |
68f66f97 | 930 | static long hardpps_update_freq(struct ntp_data *ntpdata, struct pps_normtime freq_norm) |
025b40ab AG |
931 | { |
932 | long delta, delta_mod; | |
933 | s64 ftemp; | |
934 | ||
a0581cdb | 935 | /* Check if the frequency interval was too long */ |
025b40ab | 936 | if (freq_norm.sec > (2 << pps_shift)) { |
bee18a23 | 937 | ntpdata->time_status |= STA_PPSERROR; |
025b40ab AG |
938 | pps_errcnt++; |
939 | pps_dec_freq_interval(); | |
38007dc0 AMB |
940 | printk_deferred(KERN_ERR "hardpps: PPSERROR: interval too long - %lld s\n", |
941 | freq_norm.sec); | |
025b40ab AG |
942 | return 0; |
943 | } | |
944 | ||
a0581cdb TG |
945 | /* |
946 | * Here the raw frequency offset and wander (stability) is | |
947 | * calculated. If the wander is less than the wander threshold the | |
948 | * interval is increased; otherwise it is decreased. | |
025b40ab AG |
949 | */ |
950 | ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT, | |
951 | freq_norm.sec); | |
952 | delta = shift_right(ftemp - pps_freq, NTP_SCALE_SHIFT); | |
953 | pps_freq = ftemp; | |
954 | if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) { | |
38007dc0 | 955 | printk_deferred(KERN_WARNING "hardpps: PPSWANDER: change=%ld\n", delta); |
bee18a23 | 956 | ntpdata->time_status |= STA_PPSWANDER; |
025b40ab AG |
957 | pps_stbcnt++; |
958 | pps_dec_freq_interval(); | |
a0581cdb TG |
959 | } else { |
960 | /* Good sample */ | |
025b40ab AG |
961 | pps_inc_freq_interval(); |
962 | } | |
963 | ||
a0581cdb TG |
964 | /* |
965 | * The stability metric is calculated as the average of recent | |
966 | * frequency changes, but is used only for performance monitoring | |
025b40ab AG |
967 | */ |
968 | delta_mod = delta; | |
969 | if (delta_mod < 0) | |
970 | delta_mod = -delta_mod; | |
38007dc0 AMB |
971 | pps_stabil += (div_s64(((s64)delta_mod) << (NTP_SCALE_SHIFT - SHIFT_USEC), |
972 | NSEC_PER_USEC) - pps_stabil) >> PPS_INTMIN; | |
025b40ab | 973 | |
a0581cdb | 974 | /* If enabled, the system clock frequency is updated */ |
bee18a23 | 975 | if ((ntpdata->time_status & STA_PPSFREQ) && !(ntpdata->time_status & STA_FREQHOLD)) { |
025b40ab | 976 | time_freq = pps_freq; |
68f66f97 | 977 | ntp_update_frequency(ntpdata); |
025b40ab AG |
978 | } |
979 | ||
980 | return delta; | |
981 | } | |
982 | ||
a0581cdb | 983 | /* Correct REALTIME clock phase error against PPS signal */ |
bee18a23 | 984 | static void hardpps_update_phase(struct ntp_data *ntpdata, long error) |
025b40ab AG |
985 | { |
986 | long correction = -error; | |
987 | long jitter; | |
988 | ||
a0581cdb | 989 | /* Add the sample to the median filter */ |
025b40ab AG |
990 | pps_phase_filter_add(correction); |
991 | correction = pps_phase_filter_get(&jitter); | |
992 | ||
a0581cdb TG |
993 | /* |
994 | * Nominal jitter is due to PPS signal noise. If it exceeds the | |
025b40ab AG |
995 | * threshold, the sample is discarded; otherwise, if so enabled, |
996 | * the time offset is updated. | |
997 | */ | |
998 | if (jitter > (pps_jitter << PPS_POPCORN)) { | |
38007dc0 | 999 | printk_deferred(KERN_WARNING "hardpps: PPSJITTER: jitter=%ld, limit=%ld\n", |
6d9bcb62 | 1000 | jitter, (pps_jitter << PPS_POPCORN)); |
bee18a23 | 1001 | ntpdata->time_status |= STA_PPSJITTER; |
025b40ab | 1002 | pps_jitcnt++; |
bee18a23 | 1003 | } else if (ntpdata->time_status & STA_PPSTIME) { |
a0581cdb | 1004 | /* Correct the time using the phase offset */ |
d5143554 TG |
1005 | ntpdata->time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT, |
1006 | NTP_INTERVAL_FREQ); | |
a0581cdb | 1007 | /* Cancel running adjtime() */ |
025b40ab AG |
1008 | time_adjust = 0; |
1009 | } | |
a0581cdb | 1010 | /* Update jitter */ |
025b40ab AG |
1011 | pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN; |
1012 | } | |
1013 | ||
1014 | /* | |
aa6f9c59 | 1015 | * __hardpps() - discipline CPU clock oscillator to external PPS signal |
025b40ab AG |
1016 | * |
1017 | * This routine is called at each PPS signal arrival in order to | |
1018 | * discipline the CPU clock oscillator to the PPS signal. It takes two | |
1019 | * parameters: REALTIME and MONOTONIC_RAW clock timestamps. The former | |
1020 | * is used to correct clock phase error and the latter is used to | |
1021 | * correct the frequency. | |
1022 | * | |
1023 | * This code is based on David Mills's reference nanokernel | |
1024 | * implementation. It was mostly rewritten but keeps the same idea. | |
1025 | */ | |
7ec88e4b | 1026 | void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts) |
025b40ab AG |
1027 | { |
1028 | struct pps_normtime pts_norm, freq_norm; | |
68f66f97 | 1029 | struct ntp_data *ntpdata = &tk_ntp_data; |
025b40ab AG |
1030 | |
1031 | pts_norm = pps_normalize_ts(*phase_ts); | |
1032 | ||
a0581cdb | 1033 | /* Clear the error bits, they will be set again if needed */ |
bee18a23 | 1034 | ntpdata->time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR); |
025b40ab | 1035 | |
bee18a23 TG |
1036 | /* indicate signal presence */ |
1037 | ntpdata->time_status |= STA_PPSSIGNAL; | |
025b40ab AG |
1038 | pps_valid = PPS_VALID; |
1039 | ||
a0581cdb TG |
1040 | /* |
1041 | * When called for the first time, just start the frequency | |
1042 | * interval | |
1043 | */ | |
025b40ab AG |
1044 | if (unlikely(pps_fbase.tv_sec == 0)) { |
1045 | pps_fbase = *raw_ts; | |
025b40ab AG |
1046 | return; |
1047 | } | |
1048 | ||
a0581cdb | 1049 | /* Ok, now we have a base for frequency calculation */ |
7ec88e4b | 1050 | freq_norm = pps_normalize_ts(timespec64_sub(*raw_ts, pps_fbase)); |
025b40ab | 1051 | |
a0581cdb TG |
1052 | /* |
1053 | * Check that the signal is in the range | |
1054 | * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it | |
1055 | */ | |
38007dc0 AMB |
1056 | if ((freq_norm.sec == 0) || (freq_norm.nsec > MAXFREQ * freq_norm.sec) || |
1057 | (freq_norm.nsec < -MAXFREQ * freq_norm.sec)) { | |
bee18a23 | 1058 | ntpdata->time_status |= STA_PPSJITTER; |
a0581cdb | 1059 | /* Restart the frequency calibration interval */ |
025b40ab | 1060 | pps_fbase = *raw_ts; |
6d9bcb62 | 1061 | printk_deferred(KERN_ERR "hardpps: PPSJITTER: bad pulse\n"); |
025b40ab AG |
1062 | return; |
1063 | } | |
1064 | ||
a0581cdb | 1065 | /* Signal is ok. Check if the current frequency interval is finished */ |
025b40ab AG |
1066 | if (freq_norm.sec >= (1 << pps_shift)) { |
1067 | pps_calcnt++; | |
a0581cdb | 1068 | /* Restart the frequency calibration interval */ |
025b40ab | 1069 | pps_fbase = *raw_ts; |
68f66f97 | 1070 | hardpps_update_freq(ntpdata, freq_norm); |
025b40ab AG |
1071 | } |
1072 | ||
bee18a23 | 1073 | hardpps_update_phase(ntpdata, pts_norm.nsec); |
025b40ab | 1074 | |
025b40ab | 1075 | } |
025b40ab AG |
1076 | #endif /* CONFIG_NTP_PPS */ |
1077 | ||
10a398d0 RZ |
1078 | static int __init ntp_tick_adj_setup(char *str) |
1079 | { | |
86b2dcd4 | 1080 | int rc = kstrtos64(str, 0, &ntp_tick_adj); |
cdafb93f FF |
1081 | if (rc) |
1082 | return rc; | |
069569e0 | 1083 | |
86b2dcd4 | 1084 | ntp_tick_adj <<= NTP_SCALE_SHIFT; |
10a398d0 RZ |
1085 | return 1; |
1086 | } | |
1087 | ||
1088 | __setup("ntp_tick_adj=", ntp_tick_adj_setup); | |
7dffa3c6 RZ |
1089 | |
1090 | void __init ntp_init(void) | |
1091 | { | |
1092 | ntp_clear(); | |
c9e6189f | 1093 | ntp_init_cmos_sync(); |
7dffa3c6 | 1094 | } |