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);
24 * timespec64 based interfaces
26 extern void ktime_get_raw_ts64(struct timespec64 *ts);
27 extern void ktime_get_ts64(struct timespec64 *ts);
28 extern void ktime_get_real_ts64(struct timespec64 *tv);
29 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
30 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
32 void getboottime64(struct timespec64 *ts);
35 * time64_t base interfaces
37 extern time64_t ktime_get_seconds(void);
38 extern time64_t __ktime_get_real_seconds(void);
39 extern time64_t ktime_get_real_seconds(void);
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_get_coarse_with_offset(enum tk_offsets offs);
55 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
56 extern ktime_t ktime_get_raw(void);
57 extern u32 ktime_get_resolution_ns(void);
60 * ktime_get_real - get the real (wall-) time in ktime_t format
62 static inline ktime_t ktime_get_real(void)
64 return ktime_get_with_offset(TK_OFFS_REAL);
67 static inline ktime_t ktime_get_coarse_real(void)
69 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
73 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
75 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
76 * time spent in suspend.
78 static inline ktime_t ktime_get_boottime(void)
80 return ktime_get_with_offset(TK_OFFS_BOOT);
83 static inline ktime_t ktime_get_coarse_boottime(void)
85 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
89 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
91 static inline ktime_t ktime_get_clocktai(void)
93 return ktime_get_with_offset(TK_OFFS_TAI);
96 static inline ktime_t ktime_get_coarse_clocktai(void)
98 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
102 * ktime_mono_to_real - Convert monotonic time to clock realtime
104 static inline ktime_t ktime_mono_to_real(ktime_t mono)
106 return ktime_mono_to_any(mono, TK_OFFS_REAL);
109 static inline u64 ktime_get_ns(void)
111 return ktime_to_ns(ktime_get());
114 static inline u64 ktime_get_real_ns(void)
116 return ktime_to_ns(ktime_get_real());
119 static inline u64 ktime_get_boot_ns(void)
121 return ktime_to_ns(ktime_get_boottime());
124 static inline u64 ktime_get_tai_ns(void)
126 return ktime_to_ns(ktime_get_clocktai());
129 static inline u64 ktime_get_raw_ns(void)
131 return ktime_to_ns(ktime_get_raw());
134 extern u64 ktime_get_mono_fast_ns(void);
135 extern u64 ktime_get_raw_fast_ns(void);
136 extern u64 ktime_get_boot_fast_ns(void);
137 extern u64 ktime_get_real_fast_ns(void);
140 * timespec64/time64_t interfaces utilizing the ktime based ones
141 * for API completeness, these could be implemented more efficiently
144 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
146 *ts = ktime_to_timespec64(ktime_get_boottime());
149 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
151 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
154 static inline time64_t ktime_get_boottime_seconds(void)
156 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
159 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
161 *ts = ktime_to_timespec64(ktime_get_clocktai());
164 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
166 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
169 static inline time64_t ktime_get_clocktai_seconds(void)
171 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
177 extern bool timekeeping_rtc_skipsuspend(void);
178 extern bool timekeeping_rtc_skipresume(void);
180 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
183 * struct system_time_snapshot - simultaneous raw/real time capture with
185 * @cycles: Clocksource counter value to produce the system times
186 * @real: Realtime system time
187 * @raw: Monotonic raw system time
188 * @clock_was_set_seq: The sequence number of clock was set events
189 * @cs_was_changed_seq: The sequence number of clocksource change events
191 struct system_time_snapshot {
195 unsigned int clock_was_set_seq;
196 u8 cs_was_changed_seq;
200 * struct system_device_crosststamp - system/device cross-timestamp
201 * (syncronized capture)
202 * @device: Device time
203 * @sys_realtime: Realtime simultaneous with device time
204 * @sys_monoraw: Monotonic raw simultaneous with device time
206 struct system_device_crosststamp {
208 ktime_t sys_realtime;
213 * struct system_counterval_t - system counter value with the pointer to the
214 * corresponding clocksource
215 * @cycles: System counter value
216 * @cs: Clocksource corresponding to system counter value. Used by
217 * timekeeping code to verify comparibility of two cycle values
219 struct system_counterval_t {
221 struct clocksource *cs;
225 * Get cross timestamp between system clock and device clock
227 extern int get_device_system_crosststamp(
228 int (*get_time_fn)(ktime_t *device_time,
229 struct system_counterval_t *system_counterval,
232 struct system_time_snapshot *history,
233 struct system_device_crosststamp *xtstamp);
236 * Simultaneously snapshot realtime and monotonic raw clocks
238 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
241 * Persistent clock related interfaces
243 extern int persistent_clock_is_local;
245 extern void read_persistent_clock64(struct timespec64 *ts);
246 extern void read_boot_clock64(struct timespec64 *ts);
247 extern int update_persistent_clock64(struct timespec64 now);
250 * deprecated aliases, don't use in new code
252 #define getnstimeofday64(ts) ktime_get_real_ts64(ts)
253 #define get_monotonic_boottime64(ts) ktime_get_boottime_ts64(ts)
254 #define getrawmonotonic64(ts) ktime_get_raw_ts64(ts)
255 #define timekeeping_clocktai64(ts) ktime_get_clocktai_ts64(ts)
257 static inline struct timespec64 current_kernel_time64(void)
259 struct timespec64 ts;
261 ktime_get_coarse_real_ts64(&ts);
266 static inline struct timespec64 get_monotonic_coarse64(void)
268 struct timespec64 ts;
270 ktime_get_coarse_ts64(&ts);