1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2d7b4202eSJohn Stultz /*
3d7b4202eSJohn Stultz  * You SHOULD NOT be including this unless you're vsyscall
4d7b4202eSJohn Stultz  * handling code or timekeeping internal code!
5d7b4202eSJohn Stultz  */
6d7b4202eSJohn Stultz 
7d7b4202eSJohn Stultz #ifndef _LINUX_TIMEKEEPER_INTERNAL_H
8d7b4202eSJohn Stultz #define _LINUX_TIMEKEEPER_INTERNAL_H
9d7b4202eSJohn Stultz 
10d7b4202eSJohn Stultz #include <linux/clocksource.h>
11d7b4202eSJohn Stultz #include <linux/jiffies.h>
12d7b4202eSJohn Stultz #include <linux/time.h>
13d7b4202eSJohn Stultz 
14d28ede83SThomas Gleixner /**
15d28ede83SThomas Gleixner  * struct tk_read_base - base structure for timekeeping readout
16d28ede83SThomas Gleixner  * @clock:	Current clocksource used for timekeeping.
17d28ede83SThomas Gleixner  * @mask:	Bitmask for two's complement subtraction of non 64bit clocks
18d28ede83SThomas Gleixner  * @cycle_last: @clock cycle value at last update
19876e7881SPeter Zijlstra  * @mult:	(NTP adjusted) multiplier for scaled math conversion
20d28ede83SThomas Gleixner  * @shift:	Shift value for scaled math conversion
21d28ede83SThomas Gleixner  * @xtime_nsec: Shifted (fractional) nano seconds offset for readout
22876e7881SPeter Zijlstra  * @base:	ktime_t (nanoseconds) base time for readout
234c3711d7SThomas Gleixner  * @base_real:	Nanoseconds base value for clock REALTIME readout
243fdb14fdSThomas Gleixner  *
25d28ede83SThomas Gleixner  * This struct has size 56 byte on 64 bit. Together with a seqcount it
26d28ede83SThomas Gleixner  * occupies a single 64byte cache line.
27d28ede83SThomas Gleixner  *
28d28ede83SThomas Gleixner  * The struct is separate from struct timekeeper as it is also used
296860d28cSThomas Gleixner  * for the fast NMI safe accessors.
304c3711d7SThomas Gleixner  *
314c3711d7SThomas Gleixner  * @base_real is for the fast NMI safe accessor to allow reading clock
324c3711d7SThomas Gleixner  * realtime from any context.
33d28ede83SThomas Gleixner  */
34d28ede83SThomas Gleixner struct tk_read_base {
35d28ede83SThomas Gleixner 	struct clocksource	*clock;
36a5a1d1c2SThomas Gleixner 	u64			mask;
37a5a1d1c2SThomas Gleixner 	u64			cycle_last;
38d28ede83SThomas Gleixner 	u32			mult;
39d28ede83SThomas Gleixner 	u32			shift;
40d28ede83SThomas Gleixner 	u64			xtime_nsec;
41876e7881SPeter Zijlstra 	ktime_t			base;
424c3711d7SThomas Gleixner 	u64			base_real;
43d28ede83SThomas Gleixner };
44d28ede83SThomas Gleixner 
45d28ede83SThomas Gleixner /**
46d28ede83SThomas Gleixner  * struct timekeeper - Structure holding internal timekeeping values.
47876e7881SPeter Zijlstra  * @tkr_mono:			The readout base structure for CLOCK_MONOTONIC
48d28ede83SThomas Gleixner  * @xtime_sec:			Current CLOCK_REALTIME time in seconds
499e3680b1SHeena Sirwani  * @ktime_sec:			Current CLOCK_MONOTONIC time in seconds
50d28ede83SThomas Gleixner  * @wall_to_monotonic:		CLOCK_REALTIME to CLOCK_MONOTONIC offset
51d28ede83SThomas Gleixner  * @offs_real:			Offset clock monotonic -> clock realtime
52d28ede83SThomas Gleixner  * @offs_boot:			Offset clock monotonic -> clock boottime
53d28ede83SThomas Gleixner  * @offs_tai:			Offset clock monotonic -> clock tai
54*b71f9804SThomas Gleixner  * @coarse_nsec:		The nanoseconds part for coarse time getters
556860d28cSThomas Gleixner  * @tkr_raw:			The readout base structure for CLOCK_MONOTONIC_RAW
566860d28cSThomas Gleixner  * @raw_sec:			CLOCK_MONOTONIC_RAW  time in seconds
57868a3e91SThomas Gleixner  * @clock_was_set_seq:		The sequence number of clock was set events
582c756febSChristopher S. Hall  * @cs_was_changed_seq:		The sequence number of clocksource change events
59b99328a6SThomas Gleixner  * @monotonic_to_boot:		CLOCK_MONOTONIC to CLOCK_BOOTTIME offset
60d28ede83SThomas Gleixner  * @cycle_interval:		Number of clock cycles in one NTP interval
61d28ede83SThomas Gleixner  * @xtime_interval:		Number of clock shifted nano seconds in one NTP
62d28ede83SThomas Gleixner  *				interval.
63d28ede83SThomas Gleixner  * @xtime_remainder:		Shifted nano seconds left over when rounding
64d28ede83SThomas Gleixner  *				@cycle_interval
653d88d56cSJohn Stultz  * @raw_interval:		Shifted raw nano seconds accumulated per NTP interval.
666860d28cSThomas Gleixner  * @next_leap_ktime:		CLOCK_MONOTONIC time value of a pending leap-second
676860d28cSThomas Gleixner  * @ntp_tick:			The ntp_tick_length() value currently being
686860d28cSThomas Gleixner  *				used. This cached copy ensures we consistently
696860d28cSThomas Gleixner  *				apply the tick length for an entire tick, as
706860d28cSThomas Gleixner  *				ntp_tick_length may change mid-tick, and we don't
716860d28cSThomas Gleixner  *				want to apply that new value to the tick in
726860d28cSThomas Gleixner  *				progress.
73d28ede83SThomas Gleixner  * @ntp_error:			Difference between accumulated time and NTP time in ntp
74d28ede83SThomas Gleixner  *				shifted nano seconds.
75d28ede83SThomas Gleixner  * @ntp_error_shift:		Shift conversion between clock shifted nano seconds and
76d28ede83SThomas Gleixner  *				ntp shifted nano seconds.
776860d28cSThomas Gleixner  * @ntp_err_mult:		Multiplication factor for scaled math conversion
786860d28cSThomas Gleixner  * @skip_second_overflow:	Flag used to avoid updating NTP twice with same second
79*b71f9804SThomas Gleixner  * @tai_offset:			The current UTC to TAI offset in seconds
80d28ede83SThomas Gleixner  *
81d28ede83SThomas Gleixner  * Note: For timespec(64) based interfaces wall_to_monotonic is what
82bd7c8ff9SAnna-Maria Behnsen  * we need to add to xtime (or xtime corrected for sub jiffy times)
83d28ede83SThomas Gleixner  * to get to monotonic time.  Monotonic is pegged at zero at system
84d28ede83SThomas Gleixner  * boot time, so wall_to_monotonic will be negative, however, we will
85d28ede83SThomas Gleixner  * ALWAYS keep the tv_nsec part positive so we can use the usual
86d28ede83SThomas Gleixner  * normalization.
873fdb14fdSThomas Gleixner  *
883fdb14fdSThomas Gleixner  * wall_to_monotonic is moved after resume from suspend for the
89d28ede83SThomas Gleixner  * monotonic time not to jump. We need to add total_sleep_time to
90d28ede83SThomas Gleixner  * wall_to_monotonic to get the real boot based time offset.
913fdb14fdSThomas Gleixner  *
92d28ede83SThomas Gleixner  * wall_to_monotonic is no longer the boot time, getboottime must be
933fdb14fdSThomas Gleixner  * used instead.
94b99328a6SThomas Gleixner  *
95b99328a6SThomas Gleixner  * @monotonic_to_boottime is a timespec64 representation of @offs_boot to
96b99328a6SThomas Gleixner  * accelerate the VDSO update for CLOCK_BOOTTIME.
976860d28cSThomas Gleixner  *
986860d28cSThomas Gleixner  * The cacheline ordering of the structure is optimized for in kernel usage of
996860d28cSThomas Gleixner  * the ktime_get() and ktime_get_ts64() family of time accessors. Struct
1006860d28cSThomas Gleixner  * timekeeper is prepended in the core timekeeping code with a sequence count,
1016860d28cSThomas Gleixner  * which results in the following cacheline layout:
1026860d28cSThomas Gleixner  *
1036860d28cSThomas Gleixner  * 0:	seqcount, tkr_mono
104*b71f9804SThomas Gleixner  * 1:	xtime_sec ... coarse_nsec
1056860d28cSThomas Gleixner  * 2:	tkr_raw, raw_sec
1066860d28cSThomas Gleixner  * 3,4: Internal variables
1076860d28cSThomas Gleixner  *
1086860d28cSThomas Gleixner  * Cacheline 0,1 contain the data which is used for accessing
1096860d28cSThomas Gleixner  * CLOCK_MONOTONIC/REALTIME/BOOTTIME/TAI, while cacheline 2 contains the
1106860d28cSThomas Gleixner  * data for accessing CLOCK_MONOTONIC_RAW.  Cacheline 3,4 are internal
1116860d28cSThomas Gleixner  * variables which are only accessed during timekeeper updates once per
1126860d28cSThomas Gleixner  * tick.
1133fdb14fdSThomas Gleixner  */
114d7b4202eSJohn Stultz struct timekeeper {
1156860d28cSThomas Gleixner 	/* Cacheline 0 (together with prepended seqcount of timekeeper core): */
116876e7881SPeter Zijlstra 	struct tk_read_base	tkr_mono;
1176860d28cSThomas Gleixner 
1186860d28cSThomas Gleixner 	/* Cacheline 1: */
1193fdb14fdSThomas Gleixner 	u64			xtime_sec;
1209e3680b1SHeena Sirwani 	unsigned long		ktime_sec;
1213fdb14fdSThomas Gleixner 	struct timespec64	wall_to_monotonic;
1223fdb14fdSThomas Gleixner 	ktime_t			offs_real;
1233fdb14fdSThomas Gleixner 	ktime_t			offs_boot;
1243fdb14fdSThomas Gleixner 	ktime_t			offs_tai;
125*b71f9804SThomas Gleixner 	u32			coarse_nsec;
1266860d28cSThomas Gleixner 
1276860d28cSThomas Gleixner 	/* Cacheline 2: */
1286860d28cSThomas Gleixner 	struct tk_read_base	tkr_raw;
1296860d28cSThomas Gleixner 	u64			raw_sec;
1306860d28cSThomas Gleixner 
1316860d28cSThomas Gleixner 	/* Cachline 3 and 4 (timekeeping internal variables): */
132868a3e91SThomas Gleixner 	unsigned int		clock_was_set_seq;
1332c756febSChristopher S. Hall 	u8			cs_was_changed_seq;
1346860d28cSThomas Gleixner 
135b99328a6SThomas Gleixner 	struct timespec64	monotonic_to_boot;
1363fdb14fdSThomas Gleixner 
137a5a1d1c2SThomas Gleixner 	u64			cycle_interval;
138d7b4202eSJohn Stultz 	u64			xtime_interval;
139d7b4202eSJohn Stultz 	s64			xtime_remainder;
1403d88d56cSJohn Stultz 	u64			raw_interval;
1416860d28cSThomas Gleixner 
1426860d28cSThomas Gleixner 	ktime_t			next_leap_ktime;
143375f45b5SJohn Stultz 	u64			ntp_tick;
1443fdb14fdSThomas Gleixner 	s64			ntp_error;
1453fdb14fdSThomas Gleixner 	u32			ntp_error_shift;
146dc491596SJohn Stultz 	u32			ntp_err_mult;
14778b98e3cSMiroslav Lichvar 	u32			skip_second_overflow;
148*b71f9804SThomas Gleixner 	s32			tai_offset;
149d7b4202eSJohn Stultz };
150189374aeSJohn Stultz 
151576094b7SJohn Stultz #ifdef CONFIG_GENERIC_TIME_VSYSCALL
152576094b7SJohn Stultz 
153576094b7SJohn Stultz extern void update_vsyscall(struct timekeeper *tk);
154576094b7SJohn Stultz extern void update_vsyscall_tz(void);
155576094b7SJohn Stultz 
156189374aeSJohn Stultz #else
157576094b7SJohn Stultz 
update_vsyscall(struct timekeeper * tk)158576094b7SJohn Stultz static inline void update_vsyscall(struct timekeeper *tk)
159189374aeSJohn Stultz {
160189374aeSJohn Stultz }
update_vsyscall_tz(void)161189374aeSJohn Stultz static inline void update_vsyscall_tz(void)
162189374aeSJohn Stultz {
163189374aeSJohn Stultz }
164189374aeSJohn Stultz #endif
165189374aeSJohn Stultz 
166d7b4202eSJohn Stultz #endif /* _LINUX_TIMEKEEPER_INTERNAL_H */
167