aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/clocksource.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/clocksource.c')
-rw-r--r--kernel/time/clocksource.c261
1 files changed, 87 insertions, 174 deletions
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index ba3e502c955a..15facb1b9c60 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -32,82 +32,7 @@
32#include <linux/kthread.h> 32#include <linux/kthread.h>
33 33
34#include "tick-internal.h" 34#include "tick-internal.h"
35 35#include "timekeeping_internal.h"
36void timecounter_init(struct timecounter *tc,
37 const struct cyclecounter *cc,
38 u64 start_tstamp)
39{
40 tc->cc = cc;
41 tc->cycle_last = cc->read(cc);
42 tc->nsec = start_tstamp;
43}
44EXPORT_SYMBOL_GPL(timecounter_init);
45
46/**
47 * timecounter_read_delta - get nanoseconds since last call of this function
48 * @tc: Pointer to time counter
49 *
50 * When the underlying cycle counter runs over, this will be handled
51 * correctly as long as it does not run over more than once between
52 * calls.
53 *
54 * The first call to this function for a new time counter initializes
55 * the time tracking and returns an undefined result.
56 */
57static u64 timecounter_read_delta(struct timecounter *tc)
58{
59 cycle_t cycle_now, cycle_delta;
60 u64 ns_offset;
61
62 /* read cycle counter: */
63 cycle_now = tc->cc->read(tc->cc);
64
65 /* calculate the delta since the last timecounter_read_delta(): */
66 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
67
68 /* convert to nanoseconds: */
69 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
70
71 /* update time stamp of timecounter_read_delta() call: */
72 tc->cycle_last = cycle_now;
73
74 return ns_offset;
75}
76
77u64 timecounter_read(struct timecounter *tc)
78{
79 u64 nsec;
80
81 /* increment time by nanoseconds since last call */
82 nsec = timecounter_read_delta(tc);
83 nsec += tc->nsec;
84 tc->nsec = nsec;
85
86 return nsec;
87}
88EXPORT_SYMBOL_GPL(timecounter_read);
89
90u64 timecounter_cyc2time(struct timecounter *tc,
91 cycle_t cycle_tstamp)
92{
93 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
94 u64 nsec;
95
96 /*
97 * Instead of always treating cycle_tstamp as more recent
98 * than tc->cycle_last, detect when it is too far in the
99 * future and treat it as old time stamp instead.
100 */
101 if (cycle_delta > tc->cc->mask / 2) {
102 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
103 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
104 } else {
105 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
106 }
107
108 return nsec;
109}
110EXPORT_SYMBOL_GPL(timecounter_cyc2time);
111 36
112/** 37/**
113 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks 38 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
@@ -217,13 +142,6 @@ static void __clocksource_unstable(struct clocksource *cs)
217 schedule_work(&watchdog_work); 142 schedule_work(&watchdog_work);
218} 143}
219 144
220static void clocksource_unstable(struct clocksource *cs, int64_t delta)
221{
222 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
223 cs->name, delta);
224 __clocksource_unstable(cs);
225}
226
227/** 145/**
228 * clocksource_mark_unstable - mark clocksource unstable via watchdog 146 * clocksource_mark_unstable - mark clocksource unstable via watchdog
229 * @cs: clocksource to be marked unstable 147 * @cs: clocksource to be marked unstable
@@ -249,7 +167,7 @@ void clocksource_mark_unstable(struct clocksource *cs)
249static void clocksource_watchdog(unsigned long data) 167static void clocksource_watchdog(unsigned long data)
250{ 168{
251 struct clocksource *cs; 169 struct clocksource *cs;
252 cycle_t csnow, wdnow; 170 cycle_t csnow, wdnow, cslast, wdlast, delta;
253 int64_t wd_nsec, cs_nsec; 171 int64_t wd_nsec, cs_nsec;
254 int next_cpu, reset_pending; 172 int next_cpu, reset_pending;
255 173
@@ -282,11 +200,14 @@ static void clocksource_watchdog(unsigned long data)
282 continue; 200 continue;
283 } 201 }
284 202
285 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask, 203 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
286 watchdog->mult, watchdog->shift); 204 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
205 watchdog->shift);
287 206
288 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) & 207 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
289 cs->mask, cs->mult, cs->shift); 208 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
209 wdlast = cs->wd_last; /* save these in case we print them */
210 cslast = cs->cs_last;
290 cs->cs_last = csnow; 211 cs->cs_last = csnow;
291 cs->wd_last = wdnow; 212 cs->wd_last = wdnow;
292 213
@@ -295,7 +216,12 @@ static void clocksource_watchdog(unsigned long data)
295 216
296 /* Check the deviation from the watchdog clocksource. */ 217 /* Check the deviation from the watchdog clocksource. */
297 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) { 218 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
298 clocksource_unstable(cs, cs_nsec - wd_nsec); 219 pr_warn("timekeeping watchdog: Marking clocksource '%s' as unstable, because the skew is too large:\n", cs->name);
220 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
221 watchdog->name, wdnow, wdlast, watchdog->mask);
222 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
223 cs->name, csnow, cslast, cs->mask);
224 __clocksource_unstable(cs);
299 continue; 225 continue;
300 } 226 }
301 227
@@ -543,26 +469,25 @@ static u32 clocksource_max_adjustment(struct clocksource *cs)
543 * @shift: cycle to nanosecond divisor (power of two) 469 * @shift: cycle to nanosecond divisor (power of two)
544 * @maxadj: maximum adjustment value to mult (~11%) 470 * @maxadj: maximum adjustment value to mult (~11%)
545 * @mask: bitmask for two's complement subtraction of non 64 bit counters 471 * @mask: bitmask for two's complement subtraction of non 64 bit counters
472 * @max_cyc: maximum cycle value before potential overflow (does not include
473 * any safety margin)
474 *
475 * NOTE: This function includes a safety margin of 50%, in other words, we
476 * return half the number of nanoseconds the hardware counter can technically
477 * cover. This is done so that we can potentially detect problems caused by
478 * delayed timers or bad hardware, which might result in time intervals that
479 * are larger then what the math used can handle without overflows.
546 */ 480 */
547u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask) 481u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
548{ 482{
549 u64 max_nsecs, max_cycles; 483 u64 max_nsecs, max_cycles;
550 484
551 /* 485 /*
552 * Calculate the maximum number of cycles that we can pass to the 486 * Calculate the maximum number of cycles that we can pass to the
553 * cyc2ns function without overflowing a 64-bit signed result. The 487 * cyc2ns() function without overflowing a 64-bit result.
554 * maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
555 * which is equivalent to the below.
556 * max_cycles < (2^63)/(mult + maxadj)
557 * max_cycles < 2^(log2((2^63)/(mult + maxadj)))
558 * max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
559 * max_cycles < 2^(63 - log2(mult + maxadj))
560 * max_cycles < 1 << (63 - log2(mult + maxadj))
561 * Please note that we add 1 to the result of the log2 to account for
562 * any rounding errors, ensure the above inequality is satisfied and
563 * no overflow will occur.
564 */ 488 */
565 max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1)); 489 max_cycles = ULLONG_MAX;
490 do_div(max_cycles, mult+maxadj);
566 491
567 /* 492 /*
568 * The actual maximum number of cycles we can defer the clocksource is 493 * The actual maximum number of cycles we can defer the clocksource is
@@ -573,27 +498,26 @@ u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
573 max_cycles = min(max_cycles, mask); 498 max_cycles = min(max_cycles, mask);
574 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift); 499 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
575 500
501 /* return the max_cycles value as well if requested */
502 if (max_cyc)
503 *max_cyc = max_cycles;
504
505 /* Return 50% of the actual maximum, so we can detect bad values */
506 max_nsecs >>= 1;
507
576 return max_nsecs; 508 return max_nsecs;
577} 509}
578 510
579/** 511/**
580 * clocksource_max_deferment - Returns max time the clocksource can be deferred 512 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
581 * @cs: Pointer to clocksource 513 * @cs: Pointer to clocksource to be updated
582 * 514 *
583 */ 515 */
584static u64 clocksource_max_deferment(struct clocksource *cs) 516static inline void clocksource_update_max_deferment(struct clocksource *cs)
585{ 517{
586 u64 max_nsecs; 518 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
587 519 cs->maxadj, cs->mask,
588 max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj, 520 &cs->max_cycles);
589 cs->mask);
590 /*
591 * To ensure that the clocksource does not wrap whilst we are idle,
592 * limit the time the clocksource can be deferred by 12.5%. Please
593 * note a margin of 12.5% is used because this can be computed with
594 * a shift, versus say 10% which would require division.
595 */
596 return max_nsecs - (max_nsecs >> 3);
597} 521}
598 522
599#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET 523#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
@@ -722,7 +646,7 @@ static void clocksource_enqueue(struct clocksource *cs)
722} 646}
723 647
724/** 648/**
725 * __clocksource_updatefreq_scale - Used update clocksource with new freq 649 * __clocksource_update_freq_scale - Used update clocksource with new freq
726 * @cs: clocksource to be registered 650 * @cs: clocksource to be registered
727 * @scale: Scale factor multiplied against freq to get clocksource hz 651 * @scale: Scale factor multiplied against freq to get clocksource hz
728 * @freq: clocksource frequency (cycles per second) divided by scale 652 * @freq: clocksource frequency (cycles per second) divided by scale
@@ -730,48 +654,64 @@ static void clocksource_enqueue(struct clocksource *cs)
730 * This should only be called from the clocksource->enable() method. 654 * This should only be called from the clocksource->enable() method.
731 * 655 *
732 * This *SHOULD NOT* be called directly! Please use the 656 * This *SHOULD NOT* be called directly! Please use the
733 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions. 657 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
658 * functions.
734 */ 659 */
735void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) 660void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
736{ 661{
737 u64 sec; 662 u64 sec;
663
738 /* 664 /*
739 * Calc the maximum number of seconds which we can run before 665 * Default clocksources are *special* and self-define their mult/shift.
740 * wrapping around. For clocksources which have a mask > 32bit 666 * But, you're not special, so you should specify a freq value.
741 * we need to limit the max sleep time to have a good
742 * conversion precision. 10 minutes is still a reasonable
743 * amount. That results in a shift value of 24 for a
744 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
745 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
746 * margin as we do in clocksource_max_deferment()
747 */ 667 */
748 sec = (cs->mask - (cs->mask >> 3)); 668 if (freq) {
749 do_div(sec, freq); 669 /*
750 do_div(sec, scale); 670 * Calc the maximum number of seconds which we can run before
751 if (!sec) 671 * wrapping around. For clocksources which have a mask > 32-bit
752 sec = 1; 672 * we need to limit the max sleep time to have a good
753 else if (sec > 600 && cs->mask > UINT_MAX) 673 * conversion precision. 10 minutes is still a reasonable
754 sec = 600; 674 * amount. That results in a shift value of 24 for a
755 675 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
756 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 676 * ~ 0.06ppm granularity for NTP.
757 NSEC_PER_SEC / scale, sec * scale); 677 */
758 678 sec = cs->mask;
679 do_div(sec, freq);
680 do_div(sec, scale);
681 if (!sec)
682 sec = 1;
683 else if (sec > 600 && cs->mask > UINT_MAX)
684 sec = 600;
685
686 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
687 NSEC_PER_SEC / scale, sec * scale);
688 }
759 /* 689 /*
760 * for clocksources that have large mults, to avoid overflow. 690 * Ensure clocksources that have large 'mult' values don't overflow
761 * Since mult may be adjusted by ntp, add an safety extra margin 691 * when adjusted.
762 *
763 */ 692 */
764 cs->maxadj = clocksource_max_adjustment(cs); 693 cs->maxadj = clocksource_max_adjustment(cs);
765 while ((cs->mult + cs->maxadj < cs->mult) 694 while (freq && ((cs->mult + cs->maxadj < cs->mult)
766 || (cs->mult - cs->maxadj > cs->mult)) { 695 || (cs->mult - cs->maxadj > cs->mult))) {
767 cs->mult >>= 1; 696 cs->mult >>= 1;
768 cs->shift--; 697 cs->shift--;
769 cs->maxadj = clocksource_max_adjustment(cs); 698 cs->maxadj = clocksource_max_adjustment(cs);
770 } 699 }
771 700
772 cs->max_idle_ns = clocksource_max_deferment(cs); 701 /*
702 * Only warn for *special* clocksources that self-define
703 * their mult/shift values and don't specify a freq.
704 */
705 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
706 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
707 cs->name);
708
709 clocksource_update_max_deferment(cs);
710
711 pr_info("clocksource %s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
712 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
773} 713}
774EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); 714EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
775 715
776/** 716/**
777 * __clocksource_register_scale - Used to install new clocksources 717 * __clocksource_register_scale - Used to install new clocksources
@@ -788,9 +728,9 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
788{ 728{
789 729
790 /* Initialize mult/shift and max_idle_ns */ 730 /* Initialize mult/shift and max_idle_ns */
791 __clocksource_updatefreq_scale(cs, scale, freq); 731 __clocksource_update_freq_scale(cs, scale, freq);
792 732
793 /* Add clocksource to the clcoksource list */ 733 /* Add clocksource to the clocksource list */
794 mutex_lock(&clocksource_mutex); 734 mutex_lock(&clocksource_mutex);
795 clocksource_enqueue(cs); 735 clocksource_enqueue(cs);
796 clocksource_enqueue_watchdog(cs); 736 clocksource_enqueue_watchdog(cs);
@@ -800,33 +740,6 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
800} 740}
801EXPORT_SYMBOL_GPL(__clocksource_register_scale); 741EXPORT_SYMBOL_GPL(__clocksource_register_scale);
802 742
803
804/**
805 * clocksource_register - Used to install new clocksources
806 * @cs: clocksource to be registered
807 *
808 * Returns -EBUSY if registration fails, zero otherwise.
809 */
810int clocksource_register(struct clocksource *cs)
811{
812 /* calculate max adjustment for given mult/shift */
813 cs->maxadj = clocksource_max_adjustment(cs);
814 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
815 "Clocksource %s might overflow on 11%% adjustment\n",
816 cs->name);
817
818 /* calculate max idle time permitted for this clocksource */
819 cs->max_idle_ns = clocksource_max_deferment(cs);
820
821 mutex_lock(&clocksource_mutex);
822 clocksource_enqueue(cs);
823 clocksource_enqueue_watchdog(cs);
824 clocksource_select();
825 mutex_unlock(&clocksource_mutex);
826 return 0;
827}
828EXPORT_SYMBOL(clocksource_register);
829
830static void __clocksource_change_rating(struct clocksource *cs, int rating) 743static void __clocksource_change_rating(struct clocksource *cs, int rating)
831{ 744{
832 list_del(&cs->list); 745 list_del(&cs->list);