1
2
3
4
5#include <linux/export.h>
6#include <linux/timecounter.h>
7
8void timecounter_init(struct timecounter *tc,
9 const struct cyclecounter *cc,
10 u64 start_tstamp)
11{
12 tc->cc = cc;
13 tc->cycle_last = cc->read(cc);
14 tc->nsec = start_tstamp;
15 tc->mask = (1ULL << cc->shift) - 1;
16 tc->frac = 0;
17}
18EXPORT_SYMBOL_GPL(timecounter_init);
19
20
21
22
23
24
25
26
27
28
29
30
31static u64 timecounter_read_delta(struct timecounter *tc)
32{
33 u64 cycle_now, cycle_delta;
34 u64 ns_offset;
35
36
37 cycle_now = tc->cc->read(tc->cc);
38
39
40 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
41
42
43 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta,
44 tc->mask, &tc->frac);
45
46
47 tc->cycle_last = cycle_now;
48
49 return ns_offset;
50}
51
52u64 timecounter_read(struct timecounter *tc)
53{
54 u64 nsec;
55
56
57 nsec = timecounter_read_delta(tc);
58 nsec += tc->nsec;
59 tc->nsec = nsec;
60
61 return nsec;
62}
63EXPORT_SYMBOL_GPL(timecounter_read);
64
65
66
67
68
69static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
70 u64 cycles, u64 mask, u64 frac)
71{
72 u64 ns = (u64) cycles;
73
74 ns = ((ns * cc->mult) - frac) >> cc->shift;
75
76 return ns;
77}
78
79u64 timecounter_cyc2time(struct timecounter *tc,
80 u64 cycle_tstamp)
81{
82 u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
83 u64 nsec = tc->nsec, frac = tc->frac;
84
85
86
87
88
89
90 if (delta > tc->cc->mask / 2) {
91 delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
92 nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
93 } else {
94 nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
95 }
96
97 return nsec;
98}
99EXPORT_SYMBOL_GPL(timecounter_cyc2time);
100