ntp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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. */
  9. #include <linux/capability.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/workqueue.h>
  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>
  18. #include <linux/module.h>
  19. #include <linux/rtc.h>
  20. #include <linux/audit.h>
  21. #include "ntp_internal.h"
  22. #include "timekeeping_internal.h"
  23. /*
  24. * NTP timekeeping variables:
  25. *
  26. * Note: All of the NTP state is protected by the timekeeping locks.
  27. */
  28. /* USER_HZ period (usecs): */
  29. unsigned long tick_usec = USER_TICK_USEC;
  30. /* SHIFTED_HZ period (nsecs): */
  31. unsigned long tick_nsec;
  32. static u64 tick_length;
  33. static u64 tick_length_base;
  34. #define SECS_PER_DAY 86400
  35. #define MAX_TICKADJ 500LL /* usecs */
  36. #define MAX_TICKADJ_SCALED \
  37. (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
  38. #define MAX_TAI_OFFSET 100000
  39. /*
  40. * phase-lock loop variables
  41. */
  42. /*
  43. * clock synchronization status
  44. *
  45. * (TIME_ERROR prevents overwriting the CMOS clock)
  46. */
  47. static int time_state = TIME_OK;
  48. /* clock status bits: */
  49. static int time_status = STA_UNSYNC;
  50. /* time adjustment (nsecs): */
  51. static s64 time_offset;
  52. /* pll time constant: */
  53. static long time_constant = 2;
  54. /* maximum error (usecs): */
  55. static long time_maxerror = NTP_PHASE_LIMIT;
  56. /* estimated error (usecs): */
  57. static long time_esterror = NTP_PHASE_LIMIT;
  58. /* frequency offset (scaled nsecs/secs): */
  59. static s64 time_freq;
  60. /* time at last adjustment (secs): */
  61. static time64_t time_reftime;
  62. static long time_adjust;
  63. /* constant (boot-param configurable) NTP tick adjustment (upscaled) */
  64. static s64 ntp_tick_adj;
  65. /* second value of the next pending leapsecond, or TIME64_MAX if no leap */
  66. static time64_t ntp_next_leap_sec = TIME64_MAX;
  67. #ifdef CONFIG_NTP_PPS
  68. /*
  69. * The following variables are used when a pulse-per-second (PPS) signal
  70. * is available. They establish the engineering parameters of the clock
  71. * discipline loop when controlled by the PPS signal.
  72. */
  73. #define PPS_VALID 10 /* PPS signal watchdog max (s) */
  74. #define PPS_POPCORN 4 /* popcorn spike threshold (shift) */
  75. #define PPS_INTMIN 2 /* min freq interval (s) (shift) */
  76. #define PPS_INTMAX 8 /* max freq interval (s) (shift) */
  77. #define PPS_INTCOUNT 4 /* number of consecutive good intervals to
  78. increase pps_shift or consecutive bad
  79. intervals to decrease it */
  80. #define PPS_MAXWANDER 100000 /* max PPS freq wander (ns/s) */
  81. static int pps_valid; /* signal watchdog counter */
  82. static long pps_tf[3]; /* phase median filter */
  83. static long pps_jitter; /* current jitter (ns) */
  84. static struct timespec64 pps_fbase; /* beginning of the last freq interval */
  85. static int pps_shift; /* current interval duration (s) (shift) */
  86. static int pps_intcnt; /* interval counter */
  87. static s64 pps_freq; /* frequency offset (scaled ns/s) */
  88. static long pps_stabil; /* current stability (scaled ns/s) */
  89. /*
  90. * PPS signal quality monitors
  91. */
  92. static long pps_calcnt; /* calibration intervals */
  93. static long pps_jitcnt; /* jitter limit exceeded */
  94. static long pps_stbcnt; /* stability limit exceeded */
  95. static long pps_errcnt; /* calibration errors */
  96. /* PPS kernel consumer compensates the whole phase error immediately.
  97. * Otherwise, reduce the offset by a fixed factor times the time constant.
  98. */
  99. static inline s64 ntp_offset_chunk(s64 offset)
  100. {
  101. if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
  102. return offset;
  103. else
  104. return shift_right(offset, SHIFT_PLL + time_constant);
  105. }
  106. static inline void pps_reset_freq_interval(void)
  107. {
  108. /* the PPS calibration interval may end
  109. surprisingly early */
  110. pps_shift = PPS_INTMIN;
  111. pps_intcnt = 0;
  112. }
  113. /**
  114. * pps_clear - Clears the PPS state variables
  115. */
  116. static inline void pps_clear(void)
  117. {
  118. pps_reset_freq_interval();
  119. pps_tf[0] = 0;
  120. pps_tf[1] = 0;
  121. pps_tf[2] = 0;
  122. pps_fbase.tv_sec = pps_fbase.tv_nsec = 0;
  123. pps_freq = 0;
  124. }
  125. /* Decrease pps_valid to indicate that another second has passed since
  126. * the last PPS signal. When it reaches 0, indicate that PPS signal is
  127. * missing.
  128. */
  129. static inline void pps_dec_valid(void)
  130. {
  131. if (pps_valid > 0)
  132. pps_valid--;
  133. else {
  134. time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
  135. STA_PPSWANDER | STA_PPSERROR);
  136. pps_clear();
  137. }
  138. }
  139. static inline void pps_set_freq(s64 freq)
  140. {
  141. pps_freq = freq;
  142. }
  143. static inline int is_error_status(int status)
  144. {
  145. return (status & (STA_UNSYNC|STA_CLOCKERR))
  146. /* PPS signal lost when either PPS time or
  147. * PPS frequency synchronization requested
  148. */
  149. || ((status & (STA_PPSFREQ|STA_PPSTIME))
  150. && !(status & STA_PPSSIGNAL))
  151. /* PPS jitter exceeded when
  152. * PPS time synchronization requested */
  153. || ((status & (STA_PPSTIME|STA_PPSJITTER))
  154. == (STA_PPSTIME|STA_PPSJITTER))
  155. /* PPS wander exceeded or calibration error when
  156. * PPS frequency synchronization requested
  157. */
  158. || ((status & STA_PPSFREQ)
  159. && (status & (STA_PPSWANDER|STA_PPSERROR)));
  160. }
  161. static inline void pps_fill_timex(struct __kernel_timex *txc)
  162. {
  163. txc->ppsfreq = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) *
  164. PPM_SCALE_INV, NTP_SCALE_SHIFT);
  165. txc->jitter = pps_jitter;
  166. if (!(time_status & STA_NANO))
  167. txc->jitter = pps_jitter / NSEC_PER_USEC;
  168. txc->shift = pps_shift;
  169. txc->stabil = pps_stabil;
  170. txc->jitcnt = pps_jitcnt;
  171. txc->calcnt = pps_calcnt;
  172. txc->errcnt = pps_errcnt;
  173. txc->stbcnt = pps_stbcnt;
  174. }
  175. #else /* !CONFIG_NTP_PPS */
  176. static inline s64 ntp_offset_chunk(s64 offset)
  177. {
  178. return shift_right(offset, SHIFT_PLL + time_constant);
  179. }
  180. static inline void pps_reset_freq_interval(void) {}
  181. static inline void pps_clear(void) {}
  182. static inline void pps_dec_valid(void) {}
  183. static inline void pps_set_freq(s64 freq) {}
  184. static inline int is_error_status(int status)
  185. {
  186. return status & (STA_UNSYNC|STA_CLOCKERR);
  187. }
  188. static inline void pps_fill_timex(struct __kernel_timex *txc)
  189. {
  190. /* PPS is not implemented, so these are zero */
  191. txc->ppsfreq = 0;
  192. txc->jitter = 0;
  193. txc->shift = 0;
  194. txc->stabil = 0;
  195. txc->jitcnt = 0;
  196. txc->calcnt = 0;
  197. txc->errcnt = 0;
  198. txc->stbcnt = 0;
  199. }
  200. #endif /* CONFIG_NTP_PPS */
  201. /**
  202. * ntp_synced - Returns 1 if the NTP status is not UNSYNC
  203. *
  204. */
  205. static inline int ntp_synced(void)
  206. {
  207. return !(time_status & STA_UNSYNC);
  208. }
  209. /*
  210. * NTP methods:
  211. */
  212. /*
  213. * Update (tick_length, tick_length_base, tick_nsec), based
  214. * on (tick_usec, ntp_tick_adj, time_freq):
  215. */
  216. static void ntp_update_frequency(void)
  217. {
  218. u64 second_length;
  219. u64 new_base;
  220. second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
  221. << NTP_SCALE_SHIFT;
  222. second_length += ntp_tick_adj;
  223. second_length += time_freq;
  224. tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
  225. new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
  226. /*
  227. * Don't wait for the next second_overflow, apply
  228. * the change to the tick length immediately:
  229. */
  230. tick_length += new_base - tick_length_base;
  231. tick_length_base = new_base;
  232. }
  233. static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
  234. {
  235. time_status &= ~STA_MODE;
  236. if (secs < MINSEC)
  237. return 0;
  238. if (!(time_status & STA_FLL) && (secs <= MAXSEC))
  239. return 0;
  240. time_status |= STA_MODE;
  241. return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
  242. }
  243. static void ntp_update_offset(long offset)
  244. {
  245. s64 freq_adj;
  246. s64 offset64;
  247. long secs;
  248. if (!(time_status & STA_PLL))
  249. return;
  250. if (!(time_status & STA_NANO)) {
  251. /* Make sure the multiplication below won't overflow */
  252. offset = clamp(offset, -USEC_PER_SEC, USEC_PER_SEC);
  253. offset *= NSEC_PER_USEC;
  254. }
  255. /*
  256. * Scale the phase adjustment and
  257. * clamp to the operating range.
  258. */
  259. offset = clamp(offset, -MAXPHASE, MAXPHASE);
  260. /*
  261. * Select how the frequency is to be controlled
  262. * and in which mode (PLL or FLL).
  263. */
  264. secs = (long)(__ktime_get_real_seconds() - time_reftime);
  265. if (unlikely(time_status & STA_FREQHOLD))
  266. secs = 0;
  267. time_reftime = __ktime_get_real_seconds();
  268. offset64 = offset;
  269. freq_adj = ntp_update_offset_fll(offset64, secs);
  270. /*
  271. * Clamp update interval to reduce PLL gain with low
  272. * sampling rate (e.g. intermittent network connection)
  273. * to avoid instability.
  274. */
  275. if (unlikely(secs > 1 << (SHIFT_PLL + 1 + time_constant)))
  276. secs = 1 << (SHIFT_PLL + 1 + time_constant);
  277. freq_adj += (offset64 * secs) <<
  278. (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
  279. freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
  280. time_freq = max(freq_adj, -MAXFREQ_SCALED);
  281. time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
  282. }
  283. /**
  284. * ntp_clear - Clears the NTP state variables
  285. */
  286. void ntp_clear(void)
  287. {
  288. time_adjust = 0; /* stop active adjtime() */
  289. time_status |= STA_UNSYNC;
  290. time_maxerror = NTP_PHASE_LIMIT;
  291. time_esterror = NTP_PHASE_LIMIT;
  292. ntp_update_frequency();
  293. tick_length = tick_length_base;
  294. time_offset = 0;
  295. ntp_next_leap_sec = TIME64_MAX;
  296. /* Clear PPS state variables */
  297. pps_clear();
  298. }
  299. u64 ntp_tick_length(void)
  300. {
  301. return tick_length;
  302. }
  303. /**
  304. * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
  305. *
  306. * Provides the time of the next leapsecond against CLOCK_REALTIME in
  307. * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending.
  308. */
  309. ktime_t ntp_get_next_leap(void)
  310. {
  311. ktime_t ret;
  312. if ((time_state == TIME_INS) && (time_status & STA_INS))
  313. return ktime_set(ntp_next_leap_sec, 0);
  314. ret = KTIME_MAX;
  315. return ret;
  316. }
  317. /*
  318. * this routine handles the overflow of the microsecond field
  319. *
  320. * The tricky bits of code to handle the accurate clock support
  321. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  322. * They were originally developed for SUN and DEC kernels.
  323. * All the kudos should go to Dave for this stuff.
  324. *
  325. * Also handles leap second processing, and returns leap offset
  326. */
  327. int second_overflow(time64_t secs)
  328. {
  329. s64 delta;
  330. int leap = 0;
  331. s32 rem;
  332. /*
  333. * Leap second processing. If in leap-insert state at the end of the
  334. * day, the system clock is set back one second; if in leap-delete
  335. * state, the system clock is set ahead one second.
  336. */
  337. switch (time_state) {
  338. case TIME_OK:
  339. if (time_status & STA_INS) {
  340. time_state = TIME_INS;
  341. div_s64_rem(secs, SECS_PER_DAY, &rem);
  342. ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
  343. } else if (time_status & STA_DEL) {
  344. time_state = TIME_DEL;
  345. div_s64_rem(secs + 1, SECS_PER_DAY, &rem);
  346. ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
  347. }
  348. break;
  349. case TIME_INS:
  350. if (!(time_status & STA_INS)) {
  351. ntp_next_leap_sec = TIME64_MAX;
  352. time_state = TIME_OK;
  353. } else if (secs == ntp_next_leap_sec) {
  354. leap = -1;
  355. time_state = TIME_OOP;
  356. printk(KERN_NOTICE
  357. "Clock: inserting leap second 23:59:60 UTC\n");
  358. }
  359. break;
  360. case TIME_DEL:
  361. if (!(time_status & STA_DEL)) {
  362. ntp_next_leap_sec = TIME64_MAX;
  363. time_state = TIME_OK;
  364. } else if (secs == ntp_next_leap_sec) {
  365. leap = 1;
  366. ntp_next_leap_sec = TIME64_MAX;
  367. time_state = TIME_WAIT;
  368. printk(KERN_NOTICE
  369. "Clock: deleting leap second 23:59:59 UTC\n");
  370. }
  371. break;
  372. case TIME_OOP:
  373. ntp_next_leap_sec = TIME64_MAX;
  374. time_state = TIME_WAIT;
  375. break;
  376. case TIME_WAIT:
  377. if (!(time_status & (STA_INS | STA_DEL)))
  378. time_state = TIME_OK;
  379. break;
  380. }
  381. /* Bump the maxerror field */
  382. time_maxerror += MAXFREQ / NSEC_PER_USEC;
  383. if (time_maxerror > NTP_PHASE_LIMIT) {
  384. time_maxerror = NTP_PHASE_LIMIT;
  385. time_status |= STA_UNSYNC;
  386. }
  387. /* Compute the phase adjustment for the next second */
  388. tick_length = tick_length_base;
  389. delta = ntp_offset_chunk(time_offset);
  390. time_offset -= delta;
  391. tick_length += delta;
  392. /* Check PPS signal */
  393. pps_dec_valid();
  394. if (!time_adjust)
  395. goto out;
  396. if (time_adjust > MAX_TICKADJ) {
  397. time_adjust -= MAX_TICKADJ;
  398. tick_length += MAX_TICKADJ_SCALED;
  399. goto out;
  400. }
  401. if (time_adjust < -MAX_TICKADJ) {
  402. time_adjust += MAX_TICKADJ;
  403. tick_length -= MAX_TICKADJ_SCALED;
  404. goto out;
  405. }
  406. tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
  407. << NTP_SCALE_SHIFT;
  408. time_adjust = 0;
  409. out:
  410. return leap;
  411. }
  412. static void sync_hw_clock(struct work_struct *work);
  413. static DECLARE_DELAYED_WORK(sync_work, sync_hw_clock);
  414. static void sched_sync_hw_clock(struct timespec64 now,
  415. unsigned long target_nsec, bool fail)
  416. {
  417. struct timespec64 next;
  418. ktime_get_real_ts64(&next);
  419. if (!fail)
  420. next.tv_sec = 659;
  421. else {
  422. /*
  423. * Try again as soon as possible. Delaying long periods
  424. * decreases the accuracy of the work queue timer. Due to this
  425. * the algorithm is very likely to require a short-sleep retry
  426. * after the above long sleep to synchronize ts_nsec.
  427. */
  428. next.tv_sec = 0;
  429. }
  430. /* Compute the needed delay that will get to tv_nsec == target_nsec */
  431. next.tv_nsec = target_nsec - next.tv_nsec;
  432. if (next.tv_nsec <= 0)
  433. next.tv_nsec += NSEC_PER_SEC;
  434. if (next.tv_nsec >= NSEC_PER_SEC) {
  435. next.tv_sec++;
  436. next.tv_nsec -= NSEC_PER_SEC;
  437. }
  438. queue_delayed_work(system_power_efficient_wq, &sync_work,
  439. timespec64_to_jiffies(&next));
  440. }
  441. static void sync_rtc_clock(void)
  442. {
  443. unsigned long target_nsec;
  444. struct timespec64 adjust, now;
  445. int rc;
  446. if (!IS_ENABLED(CONFIG_RTC_SYSTOHC))
  447. return;
  448. ktime_get_real_ts64(&now);
  449. adjust = now;
  450. if (persistent_clock_is_local)
  451. adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
  452. /*
  453. * The current RTC in use will provide the target_nsec it wants to be
  454. * called at, and does rtc_tv_nsec_ok internally.
  455. */
  456. rc = rtc_set_ntp_time(adjust, &target_nsec);
  457. if (rc == -ENODEV)
  458. return;
  459. sched_sync_hw_clock(now, target_nsec, rc);
  460. }
  461. #ifdef CONFIG_GENERIC_CMOS_UPDATE
  462. int __weak update_persistent_clock64(struct timespec64 now64)
  463. {
  464. return -ENODEV;
  465. }
  466. #endif
  467. static bool sync_cmos_clock(void)
  468. {
  469. static bool no_cmos;
  470. struct timespec64 now;
  471. struct timespec64 adjust;
  472. int rc = -EPROTO;
  473. long target_nsec = NSEC_PER_SEC / 2;
  474. if (!IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE))
  475. return false;
  476. if (no_cmos)
  477. return false;
  478. /*
  479. * Historically update_persistent_clock64() has followed x86
  480. * semantics, which match the MC146818A/etc RTC. This RTC will store
  481. * 'adjust' and then in .5s it will advance once second.
  482. *
  483. * Architectures are strongly encouraged to use rtclib and not
  484. * implement this legacy API.
  485. */
  486. ktime_get_real_ts64(&now);
  487. if (rtc_tv_nsec_ok(-1 * target_nsec, &adjust, &now)) {
  488. if (persistent_clock_is_local)
  489. adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
  490. rc = update_persistent_clock64(adjust);
  491. /*
  492. * The machine does not support update_persistent_clock64 even
  493. * though it defines CONFIG_GENERIC_CMOS_UPDATE.
  494. */
  495. if (rc == -ENODEV) {
  496. no_cmos = true;
  497. return false;
  498. }
  499. }
  500. sched_sync_hw_clock(now, target_nsec, rc);
  501. return true;
  502. }
  503. /*
  504. * If we have an externally synchronized Linux clock, then update RTC clock
  505. * accordingly every ~11 minutes. Generally RTCs can only store second
  506. * precision, but many RTCs will adjust the phase of their second tick to
  507. * match the moment of update. This infrastructure arranges to call to the RTC
  508. * set at the correct moment to phase synchronize the RTC second tick over
  509. * with the kernel clock.
  510. */
  511. static void sync_hw_clock(struct work_struct *work)
  512. {
  513. if (!ntp_synced())
  514. return;
  515. if (sync_cmos_clock())
  516. return;
  517. sync_rtc_clock();
  518. }
  519. void ntp_notify_cmos_timer(void)
  520. {
  521. if (!ntp_synced())
  522. return;
  523. if (IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE) ||
  524. IS_ENABLED(CONFIG_RTC_SYSTOHC))
  525. queue_delayed_work(system_power_efficient_wq, &sync_work, 0);
  526. }
  527. /*
  528. * Propagate a new txc->status value into the NTP state:
  529. */
  530. static inline void process_adj_status(const struct __kernel_timex *txc)
  531. {
  532. if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
  533. time_state = TIME_OK;
  534. time_status = STA_UNSYNC;
  535. ntp_next_leap_sec = TIME64_MAX;
  536. /* restart PPS frequency calibration */
  537. pps_reset_freq_interval();
  538. }
  539. /*
  540. * If we turn on PLL adjustments then reset the
  541. * reference time to current time.
  542. */
  543. if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
  544. time_reftime = __ktime_get_real_seconds();
  545. /* only set allowed bits */
  546. time_status &= STA_RONLY;
  547. time_status |= txc->status & ~STA_RONLY;
  548. }
  549. static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
  550. s32 *time_tai)
  551. {
  552. if (txc->modes & ADJ_STATUS)
  553. process_adj_status(txc);
  554. if (txc->modes & ADJ_NANO)
  555. time_status |= STA_NANO;
  556. if (txc->modes & ADJ_MICRO)
  557. time_status &= ~STA_NANO;
  558. if (txc->modes & ADJ_FREQUENCY) {
  559. time_freq = txc->freq * PPM_SCALE;
  560. time_freq = min(time_freq, MAXFREQ_SCALED);
  561. time_freq = max(time_freq, -MAXFREQ_SCALED);
  562. /* update pps_freq */
  563. pps_set_freq(time_freq);
  564. }
  565. if (txc->modes & ADJ_MAXERROR)
  566. time_maxerror = txc->maxerror;
  567. if (txc->modes & ADJ_ESTERROR)
  568. time_esterror = txc->esterror;
  569. if (txc->modes & ADJ_TIMECONST) {
  570. time_constant = txc->constant;
  571. if (!(time_status & STA_NANO))
  572. time_constant += 4;
  573. time_constant = min(time_constant, (long)MAXTC);
  574. time_constant = max(time_constant, 0l);
  575. }
  576. if (txc->modes & ADJ_TAI &&
  577. txc->constant >= 0 && txc->constant <= MAX_TAI_OFFSET)
  578. *time_tai = txc->constant;
  579. if (txc->modes & ADJ_OFFSET)
  580. ntp_update_offset(txc->offset);
  581. if (txc->modes & ADJ_TICK)
  582. tick_usec = txc->tick;
  583. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  584. ntp_update_frequency();
  585. }
  586. /*
  587. * adjtimex mainly allows reading (and writing, if superuser) of
  588. * kernel time-keeping variables. used by xntpd.
  589. */
  590. int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
  591. s32 *time_tai, struct audit_ntp_data *ad)
  592. {
  593. int result;
  594. if (txc->modes & ADJ_ADJTIME) {
  595. long save_adjust = time_adjust;
  596. if (!(txc->modes & ADJ_OFFSET_READONLY)) {
  597. /* adjtime() is independent from ntp_adjtime() */
  598. time_adjust = txc->offset;
  599. ntp_update_frequency();
  600. audit_ntp_set_old(ad, AUDIT_NTP_ADJUST, save_adjust);
  601. audit_ntp_set_new(ad, AUDIT_NTP_ADJUST, time_adjust);
  602. }
  603. txc->offset = save_adjust;
  604. } else {
  605. /* If there are input parameters, then process them: */
  606. if (txc->modes) {
  607. audit_ntp_set_old(ad, AUDIT_NTP_OFFSET, time_offset);
  608. audit_ntp_set_old(ad, AUDIT_NTP_FREQ, time_freq);
  609. audit_ntp_set_old(ad, AUDIT_NTP_STATUS, time_status);
  610. audit_ntp_set_old(ad, AUDIT_NTP_TAI, *time_tai);
  611. audit_ntp_set_old(ad, AUDIT_NTP_TICK, tick_usec);
  612. process_adjtimex_modes(txc, time_tai);
  613. audit_ntp_set_new(ad, AUDIT_NTP_OFFSET, time_offset);
  614. audit_ntp_set_new(ad, AUDIT_NTP_FREQ, time_freq);
  615. audit_ntp_set_new(ad, AUDIT_NTP_STATUS, time_status);
  616. audit_ntp_set_new(ad, AUDIT_NTP_TAI, *time_tai);
  617. audit_ntp_set_new(ad, AUDIT_NTP_TICK, tick_usec);
  618. }
  619. txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
  620. NTP_SCALE_SHIFT);
  621. if (!(time_status & STA_NANO))
  622. txc->offset = (u32)txc->offset / NSEC_PER_USEC;
  623. }
  624. result = time_state; /* mostly `TIME_OK' */
  625. /* check for errors */
  626. if (is_error_status(time_status))
  627. result = TIME_ERROR;
  628. txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
  629. PPM_SCALE_INV, NTP_SCALE_SHIFT);
  630. txc->maxerror = time_maxerror;
  631. txc->esterror = time_esterror;
  632. txc->status = time_status;
  633. txc->constant = time_constant;
  634. txc->precision = 1;
  635. txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
  636. txc->tick = tick_usec;
  637. txc->tai = *time_tai;
  638. /* fill PPS status fields */
  639. pps_fill_timex(txc);
  640. txc->time.tv_sec = ts->tv_sec;
  641. txc->time.tv_usec = ts->tv_nsec;
  642. if (!(time_status & STA_NANO))
  643. txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
  644. /* Handle leapsec adjustments */
  645. if (unlikely(ts->tv_sec >= ntp_next_leap_sec)) {
  646. if ((time_state == TIME_INS) && (time_status & STA_INS)) {
  647. result = TIME_OOP;
  648. txc->tai++;
  649. txc->time.tv_sec--;
  650. }
  651. if ((time_state == TIME_DEL) && (time_status & STA_DEL)) {
  652. result = TIME_WAIT;
  653. txc->tai--;
  654. txc->time.tv_sec++;
  655. }
  656. if ((time_state == TIME_OOP) &&
  657. (ts->tv_sec == ntp_next_leap_sec)) {
  658. result = TIME_WAIT;
  659. }
  660. }
  661. return result;
  662. }
  663. #ifdef CONFIG_NTP_PPS
  664. /* actually struct pps_normtime is good old struct timespec, but it is
  665. * semantically different (and it is the reason why it was invented):
  666. * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ]
  667. * while timespec.tv_nsec has a range of [0, NSEC_PER_SEC) */
  668. struct pps_normtime {
  669. s64 sec; /* seconds */
  670. long nsec; /* nanoseconds */
  671. };
  672. /* normalize the timestamp so that nsec is in the
  673. ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval */
  674. static inline struct pps_normtime pps_normalize_ts(struct timespec64 ts)
  675. {
  676. struct pps_normtime norm = {
  677. .sec = ts.tv_sec,
  678. .nsec = ts.tv_nsec
  679. };
  680. if (norm.nsec > (NSEC_PER_SEC >> 1)) {
  681. norm.nsec -= NSEC_PER_SEC;
  682. norm.sec++;
  683. }
  684. return norm;
  685. }
  686. /* get current phase correction and jitter */
  687. static inline long pps_phase_filter_get(long *jitter)
  688. {
  689. *jitter = pps_tf[0] - pps_tf[1];
  690. if (*jitter < 0)
  691. *jitter = -*jitter;
  692. /* TODO: test various filters */
  693. return pps_tf[0];
  694. }
  695. /* add the sample to the phase filter */
  696. static inline void pps_phase_filter_add(long err)
  697. {
  698. pps_tf[2] = pps_tf[1];
  699. pps_tf[1] = pps_tf[0];
  700. pps_tf[0] = err;
  701. }
  702. /* decrease frequency calibration interval length.
  703. * It is halved after four consecutive unstable intervals.
  704. */
  705. static inline void pps_dec_freq_interval(void)
  706. {
  707. if (--pps_intcnt <= -PPS_INTCOUNT) {
  708. pps_intcnt = -PPS_INTCOUNT;
  709. if (pps_shift > PPS_INTMIN) {
  710. pps_shift--;
  711. pps_intcnt = 0;
  712. }
  713. }
  714. }
  715. /* increase frequency calibration interval length.
  716. * It is doubled after four consecutive stable intervals.
  717. */
  718. static inline void pps_inc_freq_interval(void)
  719. {
  720. if (++pps_intcnt >= PPS_INTCOUNT) {
  721. pps_intcnt = PPS_INTCOUNT;
  722. if (pps_shift < PPS_INTMAX) {
  723. pps_shift++;
  724. pps_intcnt = 0;
  725. }
  726. }
  727. }
  728. /* update clock frequency based on MONOTONIC_RAW clock PPS signal
  729. * timestamps
  730. *
  731. * At the end of the calibration interval the difference between the
  732. * first and last MONOTONIC_RAW clock timestamps divided by the length
  733. * of the interval becomes the frequency update. If the interval was
  734. * too long, the data are discarded.
  735. * Returns the difference between old and new frequency values.
  736. */
  737. static long hardpps_update_freq(struct pps_normtime freq_norm)
  738. {
  739. long delta, delta_mod;
  740. s64 ftemp;
  741. /* check if the frequency interval was too long */
  742. if (freq_norm.sec > (2 << pps_shift)) {
  743. time_status |= STA_PPSERROR;
  744. pps_errcnt++;
  745. pps_dec_freq_interval();
  746. printk_deferred(KERN_ERR
  747. "hardpps: PPSERROR: interval too long - %lld s\n",
  748. freq_norm.sec);
  749. return 0;
  750. }
  751. /* here the raw frequency offset and wander (stability) is
  752. * calculated. If the wander is less than the wander threshold
  753. * the interval is increased; otherwise it is decreased.
  754. */
  755. ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT,
  756. freq_norm.sec);
  757. delta = shift_right(ftemp - pps_freq, NTP_SCALE_SHIFT);
  758. pps_freq = ftemp;
  759. if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
  760. printk_deferred(KERN_WARNING
  761. "hardpps: PPSWANDER: change=%ld\n", delta);
  762. time_status |= STA_PPSWANDER;
  763. pps_stbcnt++;
  764. pps_dec_freq_interval();
  765. } else { /* good sample */
  766. pps_inc_freq_interval();
  767. }
  768. /* the stability metric is calculated as the average of recent
  769. * frequency changes, but is used only for performance
  770. * monitoring
  771. */
  772. delta_mod = delta;
  773. if (delta_mod < 0)
  774. delta_mod = -delta_mod;
  775. pps_stabil += (div_s64(((s64)delta_mod) <<
  776. (NTP_SCALE_SHIFT - SHIFT_USEC),
  777. NSEC_PER_USEC) - pps_stabil) >> PPS_INTMIN;
  778. /* if enabled, the system clock frequency is updated */
  779. if ((time_status & STA_PPSFREQ) != 0 &&
  780. (time_status & STA_FREQHOLD) == 0) {
  781. time_freq = pps_freq;
  782. ntp_update_frequency();
  783. }
  784. return delta;
  785. }
  786. /* correct REALTIME clock phase error against PPS signal */
  787. static void hardpps_update_phase(long error)
  788. {
  789. long correction = -error;
  790. long jitter;
  791. /* add the sample to the median filter */
  792. pps_phase_filter_add(correction);
  793. correction = pps_phase_filter_get(&jitter);
  794. /* Nominal jitter is due to PPS signal noise. If it exceeds the
  795. * threshold, the sample is discarded; otherwise, if so enabled,
  796. * the time offset is updated.
  797. */
  798. if (jitter > (pps_jitter << PPS_POPCORN)) {
  799. printk_deferred(KERN_WARNING
  800. "hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
  801. jitter, (pps_jitter << PPS_POPCORN));
  802. time_status |= STA_PPSJITTER;
  803. pps_jitcnt++;
  804. } else if (time_status & STA_PPSTIME) {
  805. /* correct the time using the phase offset */
  806. time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT,
  807. NTP_INTERVAL_FREQ);
  808. /* cancel running adjtime() */
  809. time_adjust = 0;
  810. }
  811. /* update jitter */
  812. pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN;
  813. }
  814. /*
  815. * __hardpps() - discipline CPU clock oscillator to external PPS signal
  816. *
  817. * This routine is called at each PPS signal arrival in order to
  818. * discipline the CPU clock oscillator to the PPS signal. It takes two
  819. * parameters: REALTIME and MONOTONIC_RAW clock timestamps. The former
  820. * is used to correct clock phase error and the latter is used to
  821. * correct the frequency.
  822. *
  823. * This code is based on David Mills's reference nanokernel
  824. * implementation. It was mostly rewritten but keeps the same idea.
  825. */
  826. void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
  827. {
  828. struct pps_normtime pts_norm, freq_norm;
  829. pts_norm = pps_normalize_ts(*phase_ts);
  830. /* clear the error bits, they will be set again if needed */
  831. time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
  832. /* indicate signal presence */
  833. time_status |= STA_PPSSIGNAL;
  834. pps_valid = PPS_VALID;
  835. /* when called for the first time,
  836. * just start the frequency interval */
  837. if (unlikely(pps_fbase.tv_sec == 0)) {
  838. pps_fbase = *raw_ts;
  839. return;
  840. }
  841. /* ok, now we have a base for frequency calculation */
  842. freq_norm = pps_normalize_ts(timespec64_sub(*raw_ts, pps_fbase));
  843. /* check that the signal is in the range
  844. * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it */
  845. if ((freq_norm.sec == 0) ||
  846. (freq_norm.nsec > MAXFREQ * freq_norm.sec) ||
  847. (freq_norm.nsec < -MAXFREQ * freq_norm.sec)) {
  848. time_status |= STA_PPSJITTER;
  849. /* restart the frequency calibration interval */
  850. pps_fbase = *raw_ts;
  851. printk_deferred(KERN_ERR "hardpps: PPSJITTER: bad pulse\n");
  852. return;
  853. }
  854. /* signal is ok */
  855. /* check if the current frequency interval is finished */
  856. if (freq_norm.sec >= (1 << pps_shift)) {
  857. pps_calcnt++;
  858. /* restart the frequency calibration interval */
  859. pps_fbase = *raw_ts;
  860. hardpps_update_freq(freq_norm);
  861. }
  862. hardpps_update_phase(pts_norm.nsec);
  863. }
  864. #endif /* CONFIG_NTP_PPS */
  865. static int __init ntp_tick_adj_setup(char *str)
  866. {
  867. int rc = kstrtos64(str, 0, &ntp_tick_adj);
  868. if (rc)
  869. return rc;
  870. ntp_tick_adj <<= NTP_SCALE_SHIFT;
  871. return 1;
  872. }
  873. __setup("ntp_tick_adj=", ntp_tick_adj_setup);
  874. void __init ntp_init(void)
  875. {
  876. ntp_clear();
  877. }