1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMEKEEPING_H
3 #define _LINUX_TIMEKEEPING_H
4 
5 #include <linux/errno.h>
6 #include <linux/clocksource_ids.h>
7 #include <linux/ktime.h>
8 
9 /* Included from linux/ktime.h */
10 
11 void timekeeping_init(void);
12 extern int timekeeping_suspended;
13 
14 /* Architecture timer tick functions: */
15 extern void legacy_timer_tick(unsigned long ticks);
16 
17 /*
18  * Get and set timeofday
19  */
20 extern int do_settimeofday64(const struct timespec64 *ts);
21 extern int do_sys_settimeofday64(const struct timespec64 *tv,
22 				 const struct timezone *tz);
23 
24 /*
25  * ktime_get() family: read the current time in a multitude of ways,
26  *
27  * The default time reference is CLOCK_MONOTONIC, starting at
28  * boot time but not counting the time spent in suspend.
29  * For other references, use the functions with "real", "clocktai",
30  * "boottime" and "raw" suffixes.
31  *
32  * To get the time in a different format, use the ones wit
33  * "ns", "ts64" and "seconds" suffix.
34  *
35  * See Documentation/core-api/timekeeping.rst for more details.
36  */
37 
38 
39 /*
40  * timespec64 based interfaces
41  */
42 extern void ktime_get_raw_ts64(struct timespec64 *ts);
43 extern void ktime_get_ts64(struct timespec64 *ts);
44 extern void ktime_get_real_ts64(struct timespec64 *tv);
45 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
46 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
47 
48 void getboottime64(struct timespec64 *ts);
49 
50 /*
51  * time64_t base interfaces
52  */
53 extern time64_t ktime_get_seconds(void);
54 extern time64_t __ktime_get_real_seconds(void);
55 extern time64_t ktime_get_real_seconds(void);
56 
57 /*
58  * ktime_t based interfaces
59  */
60 
61 enum tk_offsets {
62 	TK_OFFS_REAL,
63 	TK_OFFS_BOOT,
64 	TK_OFFS_TAI,
65 	TK_OFFS_MAX,
66 };
67 
68 extern ktime_t ktime_get(void);
69 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
70 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
71 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
72 extern ktime_t ktime_get_raw(void);
73 extern u32 ktime_get_resolution_ns(void);
74 
75 /**
76  * ktime_get_real - get the real (wall-) time in ktime_t format
77  */
ktime_get_real(void)78 static inline ktime_t ktime_get_real(void)
79 {
80 	return ktime_get_with_offset(TK_OFFS_REAL);
81 }
82 
ktime_get_coarse_real(void)83 static inline ktime_t ktime_get_coarse_real(void)
84 {
85 	return ktime_get_coarse_with_offset(TK_OFFS_REAL);
86 }
87 
88 /**
89  * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
90  *
91  * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
92  * time spent in suspend.
93  */
ktime_get_boottime(void)94 static inline ktime_t ktime_get_boottime(void)
95 {
96 	return ktime_get_with_offset(TK_OFFS_BOOT);
97 }
98 
ktime_get_coarse_boottime(void)99 static inline ktime_t ktime_get_coarse_boottime(void)
100 {
101 	return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
102 }
103 
104 /**
105  * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
106  */
ktime_get_clocktai(void)107 static inline ktime_t ktime_get_clocktai(void)
108 {
109 	return ktime_get_with_offset(TK_OFFS_TAI);
110 }
111 
ktime_get_coarse_clocktai(void)112 static inline ktime_t ktime_get_coarse_clocktai(void)
113 {
114 	return ktime_get_coarse_with_offset(TK_OFFS_TAI);
115 }
116 
ktime_get_coarse(void)117 static inline ktime_t ktime_get_coarse(void)
118 {
119 	struct timespec64 ts;
120 
121 	ktime_get_coarse_ts64(&ts);
122 	return timespec64_to_ktime(ts);
123 }
124 
ktime_get_coarse_ns(void)125 static inline u64 ktime_get_coarse_ns(void)
126 {
127 	return ktime_to_ns(ktime_get_coarse());
128 }
129 
ktime_get_coarse_real_ns(void)130 static inline u64 ktime_get_coarse_real_ns(void)
131 {
132 	return ktime_to_ns(ktime_get_coarse_real());
133 }
134 
ktime_get_coarse_boottime_ns(void)135 static inline u64 ktime_get_coarse_boottime_ns(void)
136 {
137 	return ktime_to_ns(ktime_get_coarse_boottime());
138 }
139 
ktime_get_coarse_clocktai_ns(void)140 static inline u64 ktime_get_coarse_clocktai_ns(void)
141 {
142 	return ktime_to_ns(ktime_get_coarse_clocktai());
143 }
144 
145 /**
146  * ktime_mono_to_real - Convert monotonic time to clock realtime
147  */
ktime_mono_to_real(ktime_t mono)148 static inline ktime_t ktime_mono_to_real(ktime_t mono)
149 {
150 	return ktime_mono_to_any(mono, TK_OFFS_REAL);
151 }
152 
ktime_get_ns(void)153 static inline u64 ktime_get_ns(void)
154 {
155 	return ktime_to_ns(ktime_get());
156 }
157 
ktime_get_real_ns(void)158 static inline u64 ktime_get_real_ns(void)
159 {
160 	return ktime_to_ns(ktime_get_real());
161 }
162 
ktime_get_boottime_ns(void)163 static inline u64 ktime_get_boottime_ns(void)
164 {
165 	return ktime_to_ns(ktime_get_boottime());
166 }
167 
ktime_get_clocktai_ns(void)168 static inline u64 ktime_get_clocktai_ns(void)
169 {
170 	return ktime_to_ns(ktime_get_clocktai());
171 }
172 
ktime_get_raw_ns(void)173 static inline u64 ktime_get_raw_ns(void)
174 {
175 	return ktime_to_ns(ktime_get_raw());
176 }
177 
178 extern u64 ktime_get_mono_fast_ns(void);
179 extern u64 ktime_get_raw_fast_ns(void);
180 extern u64 ktime_get_boot_fast_ns(void);
181 extern u64 ktime_get_tai_fast_ns(void);
182 extern u64 ktime_get_real_fast_ns(void);
183 
184 /*
185  * timespec64/time64_t interfaces utilizing the ktime based ones
186  * for API completeness, these could be implemented more efficiently
187  * if needed.
188  */
ktime_get_boottime_ts64(struct timespec64 * ts)189 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
190 {
191 	*ts = ktime_to_timespec64(ktime_get_boottime());
192 }
193 
ktime_get_coarse_boottime_ts64(struct timespec64 * ts)194 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
195 {
196 	*ts = ktime_to_timespec64(ktime_get_coarse_boottime());
197 }
198 
ktime_get_boottime_seconds(void)199 static inline time64_t ktime_get_boottime_seconds(void)
200 {
201 	return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
202 }
203 
ktime_get_clocktai_ts64(struct timespec64 * ts)204 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
205 {
206 	*ts = ktime_to_timespec64(ktime_get_clocktai());
207 }
208 
ktime_get_coarse_clocktai_ts64(struct timespec64 * ts)209 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
210 {
211 	*ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
212 }
213 
ktime_get_clocktai_seconds(void)214 static inline time64_t ktime_get_clocktai_seconds(void)
215 {
216 	return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
217 }
218 
219 /*
220  * RTC specific
221  */
222 extern bool timekeeping_rtc_skipsuspend(void);
223 extern bool timekeeping_rtc_skipresume(void);
224 
225 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
226 
227 /*
228  * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
229  * @mono:	Monotonic timestamp
230  * @boot:	Boottime timestamp
231  * @real:	Realtime timestamp
232  */
233 struct ktime_timestamps {
234 	u64		mono;
235 	u64		boot;
236 	u64		real;
237 };
238 
239 /**
240  * struct system_time_snapshot - simultaneous raw/real time capture with
241  *				 counter value
242  * @cycles:	Clocksource counter value to produce the system times
243  * @real:	Realtime system time
244  * @raw:	Monotonic raw system time
245  * @clock_was_set_seq:	The sequence number of clock was set events
246  * @cs_was_changed_seq:	The sequence number of clocksource change events
247  */
248 struct system_time_snapshot {
249 	u64			cycles;
250 	ktime_t			real;
251 	ktime_t			raw;
252 	enum clocksource_ids	cs_id;
253 	unsigned int		clock_was_set_seq;
254 	u8			cs_was_changed_seq;
255 };
256 
257 /**
258  * struct system_device_crosststamp - system/device cross-timestamp
259  *				      (synchronized capture)
260  * @device:		Device time
261  * @sys_realtime:	Realtime simultaneous with device time
262  * @sys_monoraw:	Monotonic raw simultaneous with device time
263  */
264 struct system_device_crosststamp {
265 	ktime_t device;
266 	ktime_t sys_realtime;
267 	ktime_t sys_monoraw;
268 };
269 
270 /**
271  * struct system_counterval_t - system counter value with the pointer to the
272  *				corresponding clocksource
273  * @cycles:	System counter value
274  * @cs:		Clocksource corresponding to system counter value. Used by
275  *		timekeeping code to verify comparibility of two cycle values
276  */
277 struct system_counterval_t {
278 	u64			cycles;
279 	struct clocksource	*cs;
280 };
281 
282 /*
283  * Get cross timestamp between system clock and device clock
284  */
285 extern int get_device_system_crosststamp(
286 			int (*get_time_fn)(ktime_t *device_time,
287 				struct system_counterval_t *system_counterval,
288 				void *ctx),
289 			void *ctx,
290 			struct system_time_snapshot *history,
291 			struct system_device_crosststamp *xtstamp);
292 
293 /*
294  * Simultaneously snapshot realtime and monotonic raw clocks
295  */
296 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
297 
298 /* NMI safe mono/boot/realtime timestamps */
299 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
300 
301 /*
302  * Persistent clock related interfaces
303  */
304 extern int persistent_clock_is_local;
305 
306 extern void read_persistent_clock64(struct timespec64 *ts);
307 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
308 					  struct timespec64 *boot_offset);
309 #ifdef CONFIG_GENERIC_CMOS_UPDATE
310 extern int update_persistent_clock64(struct timespec64 now);
311 #endif
312 
313 #endif
314