xref: /linux/include/linux/timekeeper_internal.h (revision c1fe867b5bf9c57ab7856486d342720e2b205eed)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * You SHOULD NOT be including this unless you're vsyscall
4  * handling code or timekeeping internal code!
5  */
6 
7 #ifndef _LINUX_TIMEKEEPER_INTERNAL_H
8 #define _LINUX_TIMEKEEPER_INTERNAL_H
9 
10 #include <linux/clocksource.h>
11 #include <linux/jiffies.h>
12 #include <linux/time.h>
13 
14 /**
15  * timekeeper_ids - IDs for various time keepers in the kernel
16  * @TIMEKEEPER_CORE:		The central core timekeeper managing system time
17  * @TIMEKEEPER_AUX_FIRST:	The first AUX timekeeper
18  * @TIMEKEEPER_AUX_LAST:	The last AUX timekeeper
19  * @TIMEKEEPERS_MAX:		The maximum number of timekeepers managed
20  */
21 enum timekeeper_ids {
22 	TIMEKEEPER_CORE,
23 #ifdef CONFIG_POSIX_AUX_CLOCKS
24 	TIMEKEEPER_AUX_FIRST,
25 	TIMEKEEPER_AUX_LAST = TIMEKEEPER_AUX_FIRST + MAX_AUX_CLOCKS - 1,
26 #endif
27 	TIMEKEEPERS_MAX,
28 };
29 
30 /**
31  * struct tk_read_base - base structure for timekeeping readout
32  * @clock:	Current clocksource used for timekeeping.
33  * @mask:	Bitmask for two's complement subtraction of non 64bit clocks
34  * @cycle_last: @clock cycle value at last update
35  * @mult:	(NTP adjusted) multiplier for scaled math conversion
36  * @shift:	Shift value for scaled math conversion
37  * @xtime_nsec: Shifted (fractional) nano seconds offset for readout
38  * @base:	ktime_t (nanoseconds) base time for readout
39  * @base_real:	Nanoseconds base value for clock REALTIME readout
40  *
41  * This struct has size 56 byte on 64 bit. Together with a seqcount it
42  * occupies a single 64byte cache line.
43  *
44  * The struct is separate from struct timekeeper as it is also used
45  * for the fast NMI safe accessors.
46  *
47  * @base_real is for the fast NMI safe accessor to allow reading clock
48  * realtime from any context.
49  */
50 struct tk_read_base {
51 	struct clocksource	*clock;
52 	u64			mask;
53 	u64			cycle_last;
54 	u32			mult;
55 	u32			shift;
56 	u64			xtime_nsec;
57 	ktime_t			base;
58 	u64			base_real;
59 };
60 
61 /**
62  * struct timekeeper - Structure holding internal timekeeping values.
63  * @tkr_mono:			The readout base structure for CLOCK_MONOTONIC
64  * @xtime_sec:			Current CLOCK_REALTIME time in seconds
65  * @ktime_sec:			Current CLOCK_MONOTONIC time in seconds
66  * @wall_to_monotonic:		CLOCK_REALTIME to CLOCK_MONOTONIC offset
67  * @offs_real:			Offset clock monotonic -> clock realtime
68  * @offs_boot:			Offset clock monotonic -> clock boottime
69  * @offs_tai:			Offset clock monotonic -> clock tai
70  * @offs_aux:			Offset clock monotonic -> clock AUX
71  * @coarse_nsec:		The nanoseconds part for coarse time getters
72  * @id:				The timekeeper ID
73  * @tkr_raw:			The readout base structure for CLOCK_MONOTONIC_RAW
74  * @raw_sec:			CLOCK_MONOTONIC_RAW  time in seconds
75  * @cs_id:			The ID of the current clocksource
76  * @cs_ns_to_cyc_mult:		Multiplicator for nanoseconds to cycles conversion
77  * @cs_ns_to_cyc_shift:		Shift value for nanoseconds to cycles conversion
78  * @cs_ns_to_cyc_maxns:		Maximum nanoseconds to cyles conversion range
79  * @clock_was_set_seq:		The sequence number of clock was set events
80  * @cs_was_changed_seq:		The sequence number of clocksource change events
81  * @clock_valid:		Indicator for valid clock
82  * @monotonic_to_boot:		CLOCK_MONOTONIC to CLOCK_BOOTTIME offset
83  * @monotonic_to_aux:		CLOCK_MONOTONIC to CLOCK_AUX offset
84  * @cycle_interval:		Number of clock cycles in one NTP interval
85  * @xtime_interval:		Number of clock shifted nano seconds in one NTP
86  *				interval.
87  * @xtime_remainder:		Shifted nano seconds left over when rounding
88  *				@cycle_interval
89  * @raw_interval:		Shifted raw nano seconds accumulated per NTP interval.
90  * @next_leap_ktime:		CLOCK_MONOTONIC time value of a pending leap-second
91  * @ntp_tick:			The ntp_tick_length() value currently being
92  *				used. This cached copy ensures we consistently
93  *				apply the tick length for an entire tick, as
94  *				ntp_tick_length may change mid-tick, and we don't
95  *				want to apply that new value to the tick in
96  *				progress.
97  * @ntp_error:			Difference between accumulated time and NTP time in ntp
98  *				shifted nano seconds.
99  * @ntp_error_shift:		Shift conversion between clock shifted nano seconds and
100  *				ntp shifted nano seconds.
101  * @ntp_err_mult:		Multiplication factor for scaled math conversion
102  * @skip_second_overflow:	Flag used to avoid updating NTP twice with same second
103  * @tai_offset:			The current UTC to TAI offset in seconds
104  *
105  * Note: For timespec(64) based interfaces wall_to_monotonic is what
106  * we need to add to xtime (or xtime corrected for sub jiffy times)
107  * to get to monotonic time.  Monotonic is pegged at zero at system
108  * boot time, so wall_to_monotonic will be negative, however, we will
109  * ALWAYS keep the tv_nsec part positive so we can use the usual
110  * normalization.
111  *
112  * wall_to_monotonic is moved after resume from suspend for the
113  * monotonic time not to jump. We need to add total_sleep_time to
114  * wall_to_monotonic to get the real boot based time offset.
115  *
116  * wall_to_monotonic is no longer the boot time, getboottime must be
117  * used instead.
118  *
119  * @monotonic_to_boottime is a timespec64 representation of @offs_boot to
120  * accelerate the VDSO update for CLOCK_BOOTTIME.
121  *
122  * @offs_aux is used by the auxiliary timekeepers which do not utilize any
123  * of the regular timekeeper offset fields.
124  *
125  * @monotonic_to_aux is a timespec64 representation of @offs_aux to
126  * accelerate the VDSO update for CLOCK_AUX.
127  *
128  * The cacheline ordering of the structure is optimized for in kernel usage of
129  * the ktime_get() and ktime_get_ts64() family of time accessors. Struct
130  * timekeeper is prepended in the core timekeeping code with a sequence count,
131  * which results in the following cacheline layout:
132  *
133  * 0:	seqcount, tkr_mono
134  * 1:	xtime_sec ... id
135  * 2:	tkr_raw, raw_sec
136  * 3,4: Internal variables
137  *
138  * Cacheline 0,1 contain the data which is used for accessing
139  * CLOCK_MONOTONIC/REALTIME/BOOTTIME/TAI, while cacheline 2 contains the
140  * data for accessing CLOCK_MONOTONIC_RAW.  Cacheline 3,4 are internal
141  * variables which are only accessed during timekeeper updates once per
142  * tick.
143  */
144 struct timekeeper {
145 	/* Cacheline 0 (together with prepended seqcount of timekeeper core): */
146 	struct tk_read_base	tkr_mono;
147 
148 	/* Cacheline 1: */
149 	u64			xtime_sec;
150 	unsigned long		ktime_sec;
151 	struct timespec64	wall_to_monotonic;
152 	ktime_t			offs_real;
153 	ktime_t			offs_boot;
154 	union {
155 		ktime_t		offs_tai;
156 		ktime_t		offs_aux;
157 	};
158 	u32			coarse_nsec;
159 	enum timekeeper_ids	id;
160 
161 	/* Cacheline 2: */
162 	struct tk_read_base	tkr_raw;
163 	u64			raw_sec;
164 
165 	/* Cachline 3 and 4 (timekeeping internal variables): */
166 	enum clocksource_ids	cs_id;
167 	u32			cs_ns_to_cyc_mult;
168 	u32			cs_ns_to_cyc_shift;
169 	u64			cs_ns_to_cyc_maxns;
170 	unsigned int		clock_was_set_seq;
171 	u8			cs_was_changed_seq;
172 	u8			clock_valid;
173 
174 	union {
175 		struct timespec64	monotonic_to_boot;
176 		struct timespec64	monotonic_to_aux;
177 	};
178 
179 	u64			cycle_interval;
180 	u64			xtime_interval;
181 	s64			xtime_remainder;
182 	u64			raw_interval;
183 
184 	ktime_t			next_leap_ktime;
185 	u64			ntp_tick;
186 	s64			ntp_error;
187 	u32			ntp_error_shift;
188 	u32			ntp_err_mult;
189 	u32			skip_second_overflow;
190 	s32			tai_offset;
191 };
192 
193 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
194 
195 extern void update_vsyscall(struct timekeeper *tk);
196 extern void update_vsyscall_tz(void);
197 
198 #else
199 
200 static inline void update_vsyscall(struct timekeeper *tk)
201 {
202 }
203 static inline void update_vsyscall_tz(void)
204 {
205 }
206 #endif
207 
208 #if defined(CONFIG_GENERIC_GETTIMEOFDAY) && defined(CONFIG_POSIX_AUX_CLOCKS)
209 extern void vdso_time_update_aux(struct timekeeper *tk);
210 #else
211 static inline void vdso_time_update_aux(struct timekeeper *tk) { }
212 #endif
213 
214 #endif /* _LINUX_TIMEKEEPER_INTERNAL_H */
215