1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
274d23cc7SRichard Cochran /*
374d23cc7SRichard Cochran * linux/include/linux/timecounter.h
474d23cc7SRichard Cochran *
574d23cc7SRichard Cochran * based on code that migrated away from
674d23cc7SRichard Cochran * linux/include/linux/clocksource.h
774d23cc7SRichard Cochran */
874d23cc7SRichard Cochran #ifndef _LINUX_TIMECOUNTER_H
974d23cc7SRichard Cochran #define _LINUX_TIMECOUNTER_H
1074d23cc7SRichard Cochran
1174d23cc7SRichard Cochran #include <linux/types.h>
1274d23cc7SRichard Cochran
131891172aSRichard Cochran /* simplify initialization of mask field */
14a5a1d1c2SThomas Gleixner #define CYCLECOUNTER_MASK(bits) (u64)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
151891172aSRichard Cochran
1674d23cc7SRichard Cochran /**
1774d23cc7SRichard Cochran * struct cyclecounter - hardware abstraction for a free running counter
1874d23cc7SRichard Cochran * Provides completely state-free accessors to the underlying hardware.
1974d23cc7SRichard Cochran * Depending on which hardware it reads, the cycle counter may wrap
2074d23cc7SRichard Cochran * around quickly. Locking rules (if necessary) have to be defined
2174d23cc7SRichard Cochran * by the implementor and user of specific instances of this API.
2274d23cc7SRichard Cochran *
2374d23cc7SRichard Cochran * @read: returns the current cycle value
2474d23cc7SRichard Cochran * @mask: bitmask for two's complement
25*aa7cbefeSRandy Dunlap * subtraction of non-64-bit counters,
261891172aSRichard Cochran * see CYCLECOUNTER_MASK() helper macro
2774d23cc7SRichard Cochran * @mult: cycle to nanosecond multiplier
2874d23cc7SRichard Cochran * @shift: cycle to nanosecond divisor (power of two)
2974d23cc7SRichard Cochran */
3074d23cc7SRichard Cochran struct cyclecounter {
31a5a1d1c2SThomas Gleixner u64 (*read)(const struct cyclecounter *cc);
32a5a1d1c2SThomas Gleixner u64 mask;
3374d23cc7SRichard Cochran u32 mult;
3474d23cc7SRichard Cochran u32 shift;
3574d23cc7SRichard Cochran };
3674d23cc7SRichard Cochran
3774d23cc7SRichard Cochran /**
38*aa7cbefeSRandy Dunlap * struct timecounter - layer above a &struct cyclecounter which counts nanoseconds
3974d23cc7SRichard Cochran * Contains the state needed by timecounter_read() to detect
4074d23cc7SRichard Cochran * cycle counter wrap around. Initialize with
4174d23cc7SRichard Cochran * timecounter_init(). Also used to convert cycle counts into the
4274d23cc7SRichard Cochran * corresponding nanosecond counts with timecounter_cyc2time(). Users
4374d23cc7SRichard Cochran * of this code are responsible for initializing the underlying
4474d23cc7SRichard Cochran * cycle counter hardware, locking issues and reading the time
4574d23cc7SRichard Cochran * more often than the cycle counter wraps around. The nanosecond
4674d23cc7SRichard Cochran * counter will only wrap around after ~585 years.
4774d23cc7SRichard Cochran *
4874d23cc7SRichard Cochran * @cc: the cycle counter used by this instance
4974d23cc7SRichard Cochran * @cycle_last: most recent cycle counter value seen by
5074d23cc7SRichard Cochran * timecounter_read()
5174d23cc7SRichard Cochran * @nsec: continuously increasing count
522eebdde6SRichard Cochran * @mask: bit mask for maintaining the 'frac' field
532eebdde6SRichard Cochran * @frac: accumulated fractional nanoseconds
5474d23cc7SRichard Cochran */
5574d23cc7SRichard Cochran struct timecounter {
5674d23cc7SRichard Cochran const struct cyclecounter *cc;
57a5a1d1c2SThomas Gleixner u64 cycle_last;
5874d23cc7SRichard Cochran u64 nsec;
592eebdde6SRichard Cochran u64 mask;
602eebdde6SRichard Cochran u64 frac;
6174d23cc7SRichard Cochran };
6274d23cc7SRichard Cochran
6374d23cc7SRichard Cochran /**
6474d23cc7SRichard Cochran * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
6574d23cc7SRichard Cochran * @cc: Pointer to cycle counter.
6674d23cc7SRichard Cochran * @cycles: Cycles
672eebdde6SRichard Cochran * @mask: bit mask for maintaining the 'frac' field
682eebdde6SRichard Cochran * @frac: pointer to storage for the fractional nanoseconds.
69*aa7cbefeSRandy Dunlap *
70*aa7cbefeSRandy Dunlap * Returns: cycle counter cycles converted to nanoseconds
7174d23cc7SRichard Cochran */
cyclecounter_cyc2ns(const struct cyclecounter * cc,u64 cycles,u64 mask,u64 * frac)7274d23cc7SRichard Cochran static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
73a5a1d1c2SThomas Gleixner u64 cycles, u64 mask, u64 *frac)
7474d23cc7SRichard Cochran {
752eebdde6SRichard Cochran u64 ns = (u64) cycles;
762eebdde6SRichard Cochran
772eebdde6SRichard Cochran ns = (ns * cc->mult) + *frac;
782eebdde6SRichard Cochran *frac = ns & mask;
792eebdde6SRichard Cochran return ns >> cc->shift;
8074d23cc7SRichard Cochran }
8174d23cc7SRichard Cochran
8274d23cc7SRichard Cochran /**
83796c1efdSRichard Cochran * timecounter_adjtime - Shifts the time of the clock.
84*aa7cbefeSRandy Dunlap * @tc: The &struct timecounter to adjust
85796c1efdSRichard Cochran * @delta: Desired change in nanoseconds.
86796c1efdSRichard Cochran */
timecounter_adjtime(struct timecounter * tc,s64 delta)87796c1efdSRichard Cochran static inline void timecounter_adjtime(struct timecounter *tc, s64 delta)
88796c1efdSRichard Cochran {
89796c1efdSRichard Cochran tc->nsec += delta;
90796c1efdSRichard Cochran }
91796c1efdSRichard Cochran
92796c1efdSRichard Cochran /**
9374d23cc7SRichard Cochran * timecounter_init - initialize a time counter
9474d23cc7SRichard Cochran * @tc: Pointer to time counter which is to be initialized/reset
9574d23cc7SRichard Cochran * @cc: A cycle counter, ready to be used.
9674d23cc7SRichard Cochran * @start_tstamp: Arbitrary initial time stamp.
9774d23cc7SRichard Cochran *
9874d23cc7SRichard Cochran * After this call the current cycle register (roughly) corresponds to
9974d23cc7SRichard Cochran * the initial time stamp. Every call to timecounter_read() increments
10074d23cc7SRichard Cochran * the time stamp counter by the number of elapsed nanoseconds.
10174d23cc7SRichard Cochran */
10274d23cc7SRichard Cochran extern void timecounter_init(struct timecounter *tc,
10374d23cc7SRichard Cochran const struct cyclecounter *cc,
10474d23cc7SRichard Cochran u64 start_tstamp);
10574d23cc7SRichard Cochran
10674d23cc7SRichard Cochran /**
10774d23cc7SRichard Cochran * timecounter_read - return nanoseconds elapsed since timecounter_init()
10874d23cc7SRichard Cochran * plus the initial time stamp
10974d23cc7SRichard Cochran * @tc: Pointer to time counter.
11074d23cc7SRichard Cochran *
11174d23cc7SRichard Cochran * In other words, keeps track of time since the same epoch as
11274d23cc7SRichard Cochran * the function which generated the initial time stamp.
113*aa7cbefeSRandy Dunlap *
114*aa7cbefeSRandy Dunlap * Returns: nanoseconds since the initial time stamp
11574d23cc7SRichard Cochran */
11674d23cc7SRichard Cochran extern u64 timecounter_read(struct timecounter *tc);
11774d23cc7SRichard Cochran
11874d23cc7SRichard Cochran /**
11974d23cc7SRichard Cochran * timecounter_cyc2time - convert a cycle counter to same
12074d23cc7SRichard Cochran * time base as values returned by
12174d23cc7SRichard Cochran * timecounter_read()
12274d23cc7SRichard Cochran * @tc: Pointer to time counter.
12374d23cc7SRichard Cochran * @cycle_tstamp: a value returned by tc->cc->read()
12474d23cc7SRichard Cochran *
12574d23cc7SRichard Cochran * Cycle counts that are converted correctly as long as they
12674d23cc7SRichard Cochran * fall into the interval [-1/2 max cycle count, +1/2 max cycle count],
12774d23cc7SRichard Cochran * with "max cycle count" == cs->mask+1.
12874d23cc7SRichard Cochran *
12974d23cc7SRichard Cochran * This allows conversion of cycle counter values which were generated
13074d23cc7SRichard Cochran * in the past.
131*aa7cbefeSRandy Dunlap *
132*aa7cbefeSRandy Dunlap * Returns: cycle counter converted to nanoseconds since the initial time stamp
13374d23cc7SRichard Cochran */
13407ff4aedSMarc Kleine-Budde extern u64 timecounter_cyc2time(const struct timecounter *tc,
135a5a1d1c2SThomas Gleixner u64 cycle_tstamp);
13674d23cc7SRichard Cochran
13774d23cc7SRichard Cochran #endif
138