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 int do_settimeofday64(const struct timespec64 *ts);
20 extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
23 * Kernel time accessors
25 struct timespec64 current_kernel_time64(void);
28 * timespec64 based interfaces
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 void ktime_get_real_ts64(struct timespec64 *tv);
34 extern time64_t ktime_get_seconds(void);
35 extern time64_t __ktime_get_real_seconds(void);
36 extern time64_t ktime_get_real_seconds(void);
38 extern void getboottime64(struct timespec64 *ts);
42 * ktime_t based interfaces
52 extern ktime_t ktime_get(void);
53 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
54 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
55 extern ktime_t ktime_get_raw(void);
56 extern u32 ktime_get_resolution_ns(void);
59 * ktime_get_real - get the real (wall-) time in ktime_t format
61 static inline ktime_t ktime_get_real(void)
63 return ktime_get_with_offset(TK_OFFS_REAL);
67 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
69 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
70 * time spent in suspend.
72 static inline ktime_t ktime_get_boottime(void)
74 return ktime_get_with_offset(TK_OFFS_BOOT);
78 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
80 static inline ktime_t ktime_get_clocktai(void)
82 return ktime_get_with_offset(TK_OFFS_TAI);
86 * ktime_mono_to_real - Convert monotonic time to clock realtime
88 static inline ktime_t ktime_mono_to_real(ktime_t mono)
90 return ktime_mono_to_any(mono, TK_OFFS_REAL);
93 static inline u64 ktime_get_ns(void)
95 return ktime_to_ns(ktime_get());
98 static inline u64 ktime_get_real_ns(void)
100 return ktime_to_ns(ktime_get_real());
103 static inline u64 ktime_get_boot_ns(void)
105 return ktime_to_ns(ktime_get_boottime());
108 static inline u64 ktime_get_tai_ns(void)
110 return ktime_to_ns(ktime_get_clocktai());
113 static inline u64 ktime_get_raw_ns(void)
115 return ktime_to_ns(ktime_get_raw());
118 extern u64 ktime_get_mono_fast_ns(void);
119 extern u64 ktime_get_raw_fast_ns(void);
120 extern u64 ktime_get_boot_fast_ns(void);
121 extern u64 ktime_get_real_fast_ns(void);
124 * timespec64 interfaces utilizing the ktime based ones
126 static inline void get_monotonic_boottime64(struct timespec64 *ts)
128 *ts = ktime_to_timespec64(ktime_get_boottime());
131 static inline void timekeeping_clocktai64(struct timespec64 *ts)
133 *ts = ktime_to_timespec64(ktime_get_clocktai());
139 extern bool timekeeping_rtc_skipsuspend(void);
140 extern bool timekeeping_rtc_skipresume(void);
142 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
145 * struct system_time_snapshot - simultaneous raw/real time capture with
147 * @cycles: Clocksource counter value to produce the system times
148 * @real: Realtime system time
149 * @raw: Monotonic raw system time
150 * @clock_was_set_seq: The sequence number of clock was set events
151 * @cs_was_changed_seq: The sequence number of clocksource change events
153 struct system_time_snapshot {
157 unsigned int clock_was_set_seq;
158 u8 cs_was_changed_seq;
162 * struct system_device_crosststamp - system/device cross-timestamp
163 * (syncronized capture)
164 * @device: Device time
165 * @sys_realtime: Realtime simultaneous with device time
166 * @sys_monoraw: Monotonic raw simultaneous with device time
168 struct system_device_crosststamp {
170 ktime_t sys_realtime;
175 * struct system_counterval_t - system counter value with the pointer to the
176 * corresponding clocksource
177 * @cycles: System counter value
178 * @cs: Clocksource corresponding to system counter value. Used by
179 * timekeeping code to verify comparibility of two cycle values
181 struct system_counterval_t {
183 struct clocksource *cs;
187 * Get cross timestamp between system clock and device clock
189 extern int get_device_system_crosststamp(
190 int (*get_time_fn)(ktime_t *device_time,
191 struct system_counterval_t *system_counterval,
194 struct system_time_snapshot *history,
195 struct system_device_crosststamp *xtstamp);
198 * Simultaneously snapshot realtime and monotonic raw clocks
200 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
203 * Persistent clock related interfaces
205 extern int persistent_clock_is_local;
207 extern void read_persistent_clock64(struct timespec64 *ts);
208 extern void read_boot_clock64(struct timespec64 *ts);
209 extern int update_persistent_clock64(struct timespec64 now);
212 * deprecated aliases, don't use in new code
214 #define getnstimeofday64(ts) ktime_get_real_ts64(ts)