aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time
diff options
context:
space:
mode:
authorJason Gunthorpe <jgunthorpe@obsidianresearch.com>2017-10-13 13:54:33 -0400
committerJohn Stultz <john.stultz@linaro.org>2017-10-30 18:03:24 -0400
commit0f295b0650c90362b4111f46d7f9149a0a4191be (patch)
tree4589b6e58d710ac1f2967317305c6c8fc28903d3 /kernel/time
parent9e66317d3c92ddaab330c125dfe9d06eee268aff (diff)
rtc: Allow rtc drivers to specify the tv_nsec value for ntp
ntp is currently hardwired to try and call the rtc set when wall clock tv_nsec is 0.5 seconds. This historical behaviour works well with certain PC RTCs, but is not universal to all rtc hardware. Change how this works by introducing the driver specific concept of set_offset_nsec, the delay between current wall clock time and the target time to set (with a 0 tv_nsecs). For x86-style CMOS set_offset_nsec should be -0.5 s which causes the last second to be written 0.5 s after it has started. For compat with the old rtc_set_ntp_time, the value is defaulted to + 0.5 s, which causes the next second to be written 0.5s before it starts, as things were before this patch. Testing shows many non-x86 RTCs would like set_offset_nsec ~= 0, so ultimately each RTC driver should set the set_offset_nsec according to its needs, and non x86 architectures should stop using update_persistent_clock64 in order to access this feature. Future patches will revise the drivers as needed. Since CMOS and RTC now have very different handling they are split into two dedicated code paths, sharing the support code, and ifdefs are replaced with IS_ENABLED. Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@kernel.org> Cc: Miroslav Lichvar <mlichvar@redhat.com> Cc: Richard Cochran <richardcochran@gmail.com> Cc: Prarit Bhargava <prarit@redhat.com> Cc: Stephen Boyd <stephen.boyd@linaro.org> Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com> Signed-off-by: John Stultz <john.stultz@linaro.org>
Diffstat (limited to 'kernel/time')
-rw-r--r--kernel/time/ntp.c166
1 files changed, 113 insertions, 53 deletions
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index edf19cc53140..bc19de1a0683 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -492,6 +492,67 @@ out:
492 return leap; 492 return leap;
493} 493}
494 494
495static void sync_hw_clock(struct work_struct *work);
496static DECLARE_DELAYED_WORK(sync_work, sync_hw_clock);
497
498static void sched_sync_hw_clock(struct timespec64 now,
499 unsigned long target_nsec, bool fail)
500
501{
502 struct timespec64 next;
503
504 getnstimeofday64(&next);
505 if (!fail)
506 next.tv_sec = 659;
507 else {
508 /*
509 * Try again as soon as possible. Delaying long periods
510 * decreases the accuracy of the work queue timer. Due to this
511 * the algorithm is very likely to require a short-sleep retry
512 * after the above long sleep to synchronize ts_nsec.
513 */
514 next.tv_sec = 0;
515 }
516
517 /* Compute the needed delay that will get to tv_nsec == target_nsec */
518 next.tv_nsec = target_nsec - next.tv_nsec;
519 if (next.tv_nsec <= 0)
520 next.tv_nsec += NSEC_PER_SEC;
521 if (next.tv_nsec >= NSEC_PER_SEC) {
522 next.tv_sec++;
523 next.tv_nsec -= NSEC_PER_SEC;
524 }
525
526 queue_delayed_work(system_power_efficient_wq, &sync_work,
527 timespec64_to_jiffies(&next));
528}
529
530static void sync_rtc_clock(void)
531{
532 unsigned long target_nsec;
533 struct timespec64 adjust, now;
534 int rc;
535
536 if (!IS_ENABLED(CONFIG_RTC_SYSTOHC))
537 return;
538
539 getnstimeofday64(&now);
540
541 adjust = now;
542 if (persistent_clock_is_local)
543 adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
544
545 /*
546 * The current RTC in use will provide the target_nsec it wants to be
547 * called at, and does rtc_tv_nsec_ok internally.
548 */
549 rc = rtc_set_ntp_time(adjust, &target_nsec);
550 if (rc == -ENODEV)
551 return;
552
553 sched_sync_hw_clock(now, target_nsec, rc);
554}
555
495#ifdef CONFIG_GENERIC_CMOS_UPDATE 556#ifdef CONFIG_GENERIC_CMOS_UPDATE
496int __weak update_persistent_clock(struct timespec now) 557int __weak update_persistent_clock(struct timespec now)
497{ 558{
@@ -507,76 +568,75 @@ int __weak update_persistent_clock64(struct timespec64 now64)
507} 568}
508#endif 569#endif
509 570
510#if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) 571static bool sync_cmos_clock(void)
511static void sync_cmos_clock(struct work_struct *work);
512
513static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
514
515static void sync_cmos_clock(struct work_struct *work)
516{ 572{
573 static bool no_cmos;
517 struct timespec64 now; 574 struct timespec64 now;
518 struct timespec64 next; 575 struct timespec64 adjust;
519 int fail = 1; 576 int rc = -EPROTO;
577 long target_nsec = NSEC_PER_SEC / 2;
578
579 if (!IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE))
580 return false;
581
582 if (no_cmos)
583 return false;
520 584
521 /* 585 /*
522 * If we have an externally synchronized Linux clock, then update 586 * Historically update_persistent_clock64() has followed x86
523 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be 587 * semantics, which match the MC146818A/etc RTC. This RTC will store
524 * called as close as possible to 500 ms before the new second starts. 588 * 'adjust' and then in .5s it will advance once second.
525 * This code is run on a timer. If the clock is set, that timer 589 *
526 * may not expire at the correct time. Thus, we adjust... 590 * Architectures are strongly encouraged to use rtclib and not
527 * We want the clock to be within a couple of ticks from the target. 591 * implement this legacy API.
528 */ 592 */
529 if (!ntp_synced()) {
530 /*
531 * Not synced, exit, do not restart a timer (if one is
532 * running, let it run out).
533 */
534 return;
535 }
536
537 getnstimeofday64(&now); 593 getnstimeofday64(&now);
538 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec * 5) { 594 if (rtc_tv_nsec_ok(-1 * target_nsec, &adjust, &now)) {
539 struct timespec64 adjust = now;
540
541 fail = -ENODEV;
542 if (persistent_clock_is_local) 595 if (persistent_clock_is_local)
543 adjust.tv_sec -= (sys_tz.tz_minuteswest * 60); 596 adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
544#ifdef CONFIG_GENERIC_CMOS_UPDATE 597 rc = update_persistent_clock64(adjust);
545 fail = update_persistent_clock64(adjust); 598 /*
546#endif 599 * The machine does not support update_persistent_clock64 even
547 600 * though it defines CONFIG_GENERIC_CMOS_UPDATE.
548#ifdef CONFIG_RTC_SYSTOHC 601 */
549 if (fail == -ENODEV) 602 if (rc == -ENODEV) {
550 fail = rtc_set_ntp_time(adjust); 603 no_cmos = true;
551#endif 604 return false;
605 }
552 } 606 }
553 607
554 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2); 608 sched_sync_hw_clock(now, target_nsec, rc);
555 if (next.tv_nsec <= 0) 609 return true;
556 next.tv_nsec += NSEC_PER_SEC; 610}
557 611
558 if (!fail || fail == -ENODEV) 612/*
559 next.tv_sec = 659; 613 * If we have an externally synchronized Linux clock, then update RTC clock
560 else 614 * accordingly every ~11 minutes. Generally RTCs can only store second
561 next.tv_sec = 0; 615 * precision, but many RTCs will adjust the phase of their second tick to
616 * match the moment of update. This infrastructure arranges to call to the RTC
617 * set at the correct moment to phase synchronize the RTC second tick over
618 * with the kernel clock.
619 */
620static void sync_hw_clock(struct work_struct *work)
621{
622 if (!ntp_synced())
623 return;
562 624
563 if (next.tv_nsec >= NSEC_PER_SEC) { 625 if (sync_cmos_clock())
564 next.tv_sec++; 626 return;
565 next.tv_nsec -= NSEC_PER_SEC; 627
566 } 628 sync_rtc_clock();
567 queue_delayed_work(system_power_efficient_wq,
568 &sync_cmos_work, timespec64_to_jiffies(&next));
569} 629}
570 630
571void ntp_notify_cmos_timer(void) 631void ntp_notify_cmos_timer(void)
572{ 632{
573 queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0); 633 if (!ntp_synced())
574} 634 return;
575
576#else
577void ntp_notify_cmos_timer(void) { }
578#endif
579 635
636 if (IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE) ||
637 IS_ENABLED(CONFIG_RTC_SYSTOHC))
638 queue_delayed_work(system_power_efficient_wq, &sync_work, 0);
639}
580 640
581/* 641/*
582 * Propagate a new txc->status value into the NTP state: 642 * Propagate a new txc->status value into the NTP state: