aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/time/timecounter.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/kernel/time/timecounter.c b/kernel/time/timecounter.c
index 59a1ec3a57cb..4687b3104bae 100644
--- a/kernel/time/timecounter.c
+++ b/kernel/time/timecounter.c
@@ -25,6 +25,8 @@ void timecounter_init(struct timecounter *tc,
25 tc->cc = cc; 25 tc->cc = cc;
26 tc->cycle_last = cc->read(cc); 26 tc->cycle_last = cc->read(cc);
27 tc->nsec = start_tstamp; 27 tc->nsec = start_tstamp;
28 tc->mask = (1ULL << cc->shift) - 1;
29 tc->frac = 0;
28} 30}
29EXPORT_SYMBOL_GPL(timecounter_init); 31EXPORT_SYMBOL_GPL(timecounter_init);
30 32
@@ -51,7 +53,8 @@ static u64 timecounter_read_delta(struct timecounter *tc)
51 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; 53 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
52 54
53 /* convert to nanoseconds: */ 55 /* convert to nanoseconds: */
54 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta); 56 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta,
57 tc->mask, &tc->frac);
55 58
56 /* update time stamp of timecounter_read_delta() call: */ 59 /* update time stamp of timecounter_read_delta() call: */
57 tc->cycle_last = cycle_now; 60 tc->cycle_last = cycle_now;
@@ -72,22 +75,36 @@ u64 timecounter_read(struct timecounter *tc)
72} 75}
73EXPORT_SYMBOL_GPL(timecounter_read); 76EXPORT_SYMBOL_GPL(timecounter_read);
74 77
78/*
79 * This is like cyclecounter_cyc2ns(), but it is used for computing a
80 * time previous to the time stored in the cycle counter.
81 */
82static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
83 cycle_t cycles, u64 mask, u64 frac)
84{
85 u64 ns = (u64) cycles;
86
87 ns = ((ns * cc->mult) - frac) >> cc->shift;
88
89 return ns;
90}
91
75u64 timecounter_cyc2time(struct timecounter *tc, 92u64 timecounter_cyc2time(struct timecounter *tc,
76 cycle_t cycle_tstamp) 93 cycle_t cycle_tstamp)
77{ 94{
78 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; 95 u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
79 u64 nsec; 96 u64 nsec = tc->nsec, frac = tc->frac;
80 97
81 /* 98 /*
82 * Instead of always treating cycle_tstamp as more recent 99 * Instead of always treating cycle_tstamp as more recent
83 * than tc->cycle_last, detect when it is too far in the 100 * than tc->cycle_last, detect when it is too far in the
84 * future and treat it as old time stamp instead. 101 * future and treat it as old time stamp instead.
85 */ 102 */
86 if (cycle_delta > tc->cc->mask / 2) { 103 if (delta > tc->cc->mask / 2) {
87 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; 104 delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
88 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta); 105 nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
89 } else { 106 } else {
90 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec; 107 nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
91 } 108 }
92 109
93 return nsec; 110 return nsec;