1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMEKEEPING_H
3 #define _LINUX_TIMEKEEPING_H
5 #include <linux/errno.h>
7 /* Included from linux/ktime.h */
9 void timekeeping_init(void);
10 extern int timekeeping_suspended;
12 /* Architecture timer tick functions: */
13 extern void update_process_times(int user);
14 extern void xtime_update(unsigned long ticks);
17 * Get and set timeofday
19 extern void do_gettimeofday(struct timeval *tv);
20 extern int do_settimeofday64(const struct timespec64 *ts);
21 extern int do_sys_settimeofday64(const struct timespec64 *tv,
22 const struct timezone *tz);
24 * Kernel time accessors
26 unsigned long get_seconds(void);
27 struct timespec64 current_kernel_time64(void);
28 /* does not take xtime_lock */
29 struct timespec __current_kernel_time(void);
31 static inline struct timespec current_kernel_time(void)
33 struct timespec64 now = current_kernel_time64();
35 return timespec64_to_timespec(now);
39 * timespec based interfaces
41 struct timespec64 get_monotonic_coarse64(void);
42 extern void getrawmonotonic64(struct timespec64 *ts);
43 extern void ktime_get_ts64(struct timespec64 *ts);
44 extern time64_t ktime_get_seconds(void);
45 extern time64_t ktime_get_real_seconds(void);
47 extern int __getnstimeofday64(struct timespec64 *tv);
48 extern void getnstimeofday64(struct timespec64 *tv);
49 extern void getboottime64(struct timespec64 *ts);
51 #if BITS_PER_LONG == 64
53 * Deprecated. Use do_settimeofday64().
55 static inline int do_settimeofday(const struct timespec *ts)
57 return do_settimeofday64(ts);
60 static inline int __getnstimeofday(struct timespec *ts)
62 return __getnstimeofday64(ts);
65 static inline void getnstimeofday(struct timespec *ts)
70 static inline void ktime_get_ts(struct timespec *ts)
75 static inline void ktime_get_real_ts(struct timespec *ts)
80 static inline void getrawmonotonic(struct timespec *ts)
82 getrawmonotonic64(ts);
85 static inline struct timespec get_monotonic_coarse(void)
87 return get_monotonic_coarse64();
90 static inline void getboottime(struct timespec *ts)
92 return getboottime64(ts);
96 * Deprecated. Use do_settimeofday64().
98 static inline int do_settimeofday(const struct timespec *ts)
100 struct timespec64 ts64;
102 ts64 = timespec_to_timespec64(*ts);
103 return do_settimeofday64(&ts64);
106 static inline int __getnstimeofday(struct timespec *ts)
108 struct timespec64 ts64;
109 int ret = __getnstimeofday64(&ts64);
111 *ts = timespec64_to_timespec(ts64);
115 static inline void getnstimeofday(struct timespec *ts)
117 struct timespec64 ts64;
119 getnstimeofday64(&ts64);
120 *ts = timespec64_to_timespec(ts64);
123 static inline void ktime_get_ts(struct timespec *ts)
125 struct timespec64 ts64;
127 ktime_get_ts64(&ts64);
128 *ts = timespec64_to_timespec(ts64);
131 static inline void ktime_get_real_ts(struct timespec *ts)
133 struct timespec64 ts64;
135 getnstimeofday64(&ts64);
136 *ts = timespec64_to_timespec(ts64);
139 static inline void getrawmonotonic(struct timespec *ts)
141 struct timespec64 ts64;
143 getrawmonotonic64(&ts64);
144 *ts = timespec64_to_timespec(ts64);
147 static inline struct timespec get_monotonic_coarse(void)
149 return timespec64_to_timespec(get_monotonic_coarse64());
152 static inline void getboottime(struct timespec *ts)
154 struct timespec64 ts64;
156 getboottime64(&ts64);
157 *ts = timespec64_to_timespec(ts64);
161 #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
164 * ktime_t based interfaces
174 extern ktime_t ktime_get(void);
175 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
176 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
177 extern ktime_t ktime_get_raw(void);
178 extern u32 ktime_get_resolution_ns(void);
181 * ktime_get_real - get the real (wall-) time in ktime_t format
183 static inline ktime_t ktime_get_real(void)
185 return ktime_get_with_offset(TK_OFFS_REAL);
189 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
191 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
192 * time spent in suspend.
194 static inline ktime_t ktime_get_boottime(void)
196 return ktime_get_with_offset(TK_OFFS_BOOT);
200 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
202 static inline ktime_t ktime_get_clocktai(void)
204 return ktime_get_with_offset(TK_OFFS_TAI);
208 * ktime_mono_to_real - Convert monotonic time to clock realtime
210 static inline ktime_t ktime_mono_to_real(ktime_t mono)
212 return ktime_mono_to_any(mono, TK_OFFS_REAL);
215 static inline u64 ktime_get_ns(void)
217 return ktime_to_ns(ktime_get());
220 static inline u64 ktime_get_real_ns(void)
222 return ktime_to_ns(ktime_get_real());
225 static inline u64 ktime_get_boot_ns(void)
227 return ktime_to_ns(ktime_get_boottime());
230 static inline u64 ktime_get_tai_ns(void)
232 return ktime_to_ns(ktime_get_clocktai());
235 static inline u64 ktime_get_raw_ns(void)
237 return ktime_to_ns(ktime_get_raw());
240 extern u64 ktime_get_mono_fast_ns(void);
241 extern u64 ktime_get_raw_fast_ns(void);
242 extern u64 ktime_get_boot_fast_ns(void);
245 * Timespec interfaces utilizing the ktime based ones
247 static inline void get_monotonic_boottime(struct timespec *ts)
249 *ts = ktime_to_timespec(ktime_get_boottime());
252 static inline void get_monotonic_boottime64(struct timespec64 *ts)
254 *ts = ktime_to_timespec64(ktime_get_boottime());
257 static inline void timekeeping_clocktai(struct timespec *ts)
259 *ts = ktime_to_timespec(ktime_get_clocktai());
262 static inline void timekeeping_clocktai64(struct timespec64 *ts)
264 *ts = ktime_to_timespec64(ktime_get_clocktai());
270 extern bool timekeeping_rtc_skipsuspend(void);
271 extern bool timekeeping_rtc_skipresume(void);
273 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
278 extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
279 struct timespec64 *ts_real);
282 * struct system_time_snapshot - simultaneous raw/real time capture with
284 * @cycles: Clocksource counter value to produce the system times
285 * @real: Realtime system time
286 * @raw: Monotonic raw system time
287 * @clock_was_set_seq: The sequence number of clock was set events
288 * @cs_was_changed_seq: The sequence number of clocksource change events
290 struct system_time_snapshot {
294 unsigned int clock_was_set_seq;
295 u8 cs_was_changed_seq;
299 * struct system_device_crosststamp - system/device cross-timestamp
300 * (syncronized capture)
301 * @device: Device time
302 * @sys_realtime: Realtime simultaneous with device time
303 * @sys_monoraw: Monotonic raw simultaneous with device time
305 struct system_device_crosststamp {
307 ktime_t sys_realtime;
312 * struct system_counterval_t - system counter value with the pointer to the
313 * corresponding clocksource
314 * @cycles: System counter value
315 * @cs: Clocksource corresponding to system counter value. Used by
316 * timekeeping code to verify comparibility of two cycle values
318 struct system_counterval_t {
320 struct clocksource *cs;
324 * Get cross timestamp between system clock and device clock
326 extern int get_device_system_crosststamp(
327 int (*get_time_fn)(ktime_t *device_time,
328 struct system_counterval_t *system_counterval,
331 struct system_time_snapshot *history,
332 struct system_device_crosststamp *xtstamp);
335 * Simultaneously snapshot realtime and monotonic raw clocks
337 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
340 * Persistent clock related interfaces
342 extern int persistent_clock_is_local;
344 extern void read_persistent_clock(struct timespec *ts);
345 extern void read_persistent_clock64(struct timespec64 *ts);
346 extern void read_boot_clock64(struct timespec64 *ts);
347 extern int update_persistent_clock(struct timespec now);
348 extern int update_persistent_clock64(struct timespec64 now);