xref: /linux-6.15/include/linux/timekeeping.h (revision ece97f3a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMEKEEPING_H
3 #define _LINUX_TIMEKEEPING_H
4 
5 #include <linux/errno.h>
6 
7 /* Included from linux/ktime.h */
8 
9 void timekeeping_init(void);
10 extern int timekeeping_suspended;
11 
12 /* Architecture timer tick functions: */
13 extern void update_process_times(int user);
14 extern void xtime_update(unsigned long ticks);
15 
16 /*
17  * Get and set timeofday
18  */
19 extern int do_settimeofday64(const struct timespec64 *ts);
20 extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 				 const struct timezone *tz);
22 /*
23  * Kernel time accessors
24  */
25 struct timespec64 current_kernel_time64(void);
26 
27 /*
28  * timespec64 based interfaces
29  */
30 struct timespec64 get_monotonic_coarse64(void);
31 extern void getrawmonotonic64(struct timespec64 *ts);
32 extern void ktime_get_ts64(struct timespec64 *ts);
33 extern time64_t ktime_get_seconds(void);
34 extern time64_t __ktime_get_real_seconds(void);
35 extern time64_t ktime_get_real_seconds(void);
36 extern void ktime_get_active_ts64(struct timespec64 *ts);
37 
38 extern int __getnstimeofday64(struct timespec64 *tv);
39 extern void getnstimeofday64(struct timespec64 *tv);
40 extern void getboottime64(struct timespec64 *ts);
41 
42 #define ktime_get_real_ts64(ts)		getnstimeofday64(ts)
43 
44 /* Clock BOOTTIME compatibility wrappers */
45 static inline void get_monotonic_boottime64(struct timespec64 *ts)
46 {
47 	ktime_get_ts64(ts);
48 }
49 
50 /*
51  * ktime_t based interfaces
52  */
53 enum tk_offsets {
54 	TK_OFFS_REAL,
55 	TK_OFFS_TAI,
56 	TK_OFFS_MAX,
57 };
58 
59 extern ktime_t ktime_get(void);
60 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
61 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
62 extern ktime_t ktime_get_raw(void);
63 extern u32 ktime_get_resolution_ns(void);
64 
65 /* Clock BOOTTIME compatibility wrappers */
66 static inline ktime_t ktime_get_boottime(void) { return ktime_get(); }
67 static inline u64 ktime_get_boot_ns(void) { return ktime_get(); }
68 
69 /**
70  * ktime_get_real - get the real (wall-) time in ktime_t format
71  */
72 static inline ktime_t ktime_get_real(void)
73 {
74 	return ktime_get_with_offset(TK_OFFS_REAL);
75 }
76 
77 /**
78  * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
79  */
80 static inline ktime_t ktime_get_clocktai(void)
81 {
82 	return ktime_get_with_offset(TK_OFFS_TAI);
83 }
84 
85 /**
86  * ktime_mono_to_real - Convert monotonic time to clock realtime
87  */
88 static inline ktime_t ktime_mono_to_real(ktime_t mono)
89 {
90 	return ktime_mono_to_any(mono, TK_OFFS_REAL);
91 }
92 
93 static inline u64 ktime_get_ns(void)
94 {
95 	return ktime_to_ns(ktime_get());
96 }
97 
98 static inline u64 ktime_get_real_ns(void)
99 {
100 	return ktime_to_ns(ktime_get_real());
101 }
102 
103 static inline u64 ktime_get_tai_ns(void)
104 {
105 	return ktime_to_ns(ktime_get_clocktai());
106 }
107 
108 static inline u64 ktime_get_raw_ns(void)
109 {
110 	return ktime_to_ns(ktime_get_raw());
111 }
112 
113 extern u64 ktime_get_mono_fast_ns(void);
114 extern u64 ktime_get_raw_fast_ns(void);
115 extern u64 ktime_get_real_fast_ns(void);
116 
117 /*
118  * timespec64 interfaces utilizing the ktime based ones
119  */
120 static inline void timekeeping_clocktai64(struct timespec64 *ts)
121 {
122 	*ts = ktime_to_timespec64(ktime_get_clocktai());
123 }
124 
125 /*
126  * RTC specific
127  */
128 extern bool timekeeping_rtc_skipsuspend(void);
129 extern bool timekeeping_rtc_skipresume(void);
130 
131 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
132 
133 /*
134  * struct system_time_snapshot - simultaneous raw/real time capture with
135  *	counter value
136  * @cycles:	Clocksource counter value to produce the system times
137  * @real:	Realtime system time
138  * @raw:	Monotonic raw system time
139  * @clock_was_set_seq:	The sequence number of clock was set events
140  * @cs_was_changed_seq:	The sequence number of clocksource change events
141  */
142 struct system_time_snapshot {
143 	u64		cycles;
144 	ktime_t		real;
145 	ktime_t		raw;
146 	unsigned int	clock_was_set_seq;
147 	u8		cs_was_changed_seq;
148 };
149 
150 /*
151  * struct system_device_crosststamp - system/device cross-timestamp
152  *	(syncronized capture)
153  * @device:		Device time
154  * @sys_realtime:	Realtime simultaneous with device time
155  * @sys_monoraw:	Monotonic raw simultaneous with device time
156  */
157 struct system_device_crosststamp {
158 	ktime_t device;
159 	ktime_t sys_realtime;
160 	ktime_t sys_monoraw;
161 };
162 
163 /*
164  * struct system_counterval_t - system counter value with the pointer to the
165  *	corresponding clocksource
166  * @cycles:	System counter value
167  * @cs:		Clocksource corresponding to system counter value. Used by
168  *	timekeeping code to verify comparibility of two cycle values
169  */
170 struct system_counterval_t {
171 	u64			cycles;
172 	struct clocksource	*cs;
173 };
174 
175 /*
176  * Get cross timestamp between system clock and device clock
177  */
178 extern int get_device_system_crosststamp(
179 			int (*get_time_fn)(ktime_t *device_time,
180 				struct system_counterval_t *system_counterval,
181 				void *ctx),
182 			void *ctx,
183 			struct system_time_snapshot *history,
184 			struct system_device_crosststamp *xtstamp);
185 
186 /*
187  * Simultaneously snapshot realtime and monotonic raw clocks
188  */
189 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
190 
191 /*
192  * Persistent clock related interfaces
193  */
194 extern int persistent_clock_is_local;
195 
196 extern void read_persistent_clock64(struct timespec64 *ts);
197 extern void read_boot_clock64(struct timespec64 *ts);
198 extern int update_persistent_clock64(struct timespec64 now);
199 
200 
201 #endif
202