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 legacy_timer_tick(unsigned long ticks);
16 * Get and set timeofday
18 extern int do_settimeofday64(const struct timespec64 *ts);
19 extern int do_sys_settimeofday64(const struct timespec64 *tv,
20 const struct timezone *tz);
23 * ktime_get() family: read the current time in a multitude of ways,
25 * The default time reference is CLOCK_MONOTONIC, starting at
26 * boot time but not counting the time spent in suspend.
27 * For other references, use the functions with "real", "clocktai",
28 * "boottime" and "raw" suffixes.
30 * To get the time in a different format, use the ones wit
31 * "ns", "ts64" and "seconds" suffix.
33 * See Documentation/core-api/timekeeping.rst for more details.
38 * timespec64 based interfaces
40 extern void ktime_get_raw_ts64(struct timespec64 *ts);
41 extern void ktime_get_ts64(struct timespec64 *ts);
42 extern void ktime_get_real_ts64(struct timespec64 *tv);
43 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
44 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
46 void getboottime64(struct timespec64 *ts);
49 * time64_t base interfaces
51 extern time64_t ktime_get_seconds(void);
52 extern time64_t __ktime_get_real_seconds(void);
53 extern time64_t ktime_get_real_seconds(void);
56 * ktime_t based interfaces
66 extern ktime_t ktime_get(void);
67 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
68 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
69 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
70 extern ktime_t ktime_get_raw(void);
71 extern u32 ktime_get_resolution_ns(void);
74 * ktime_get_real - get the real (wall-) time in ktime_t format
76 static inline ktime_t ktime_get_real(void)
78 return ktime_get_with_offset(TK_OFFS_REAL);
81 static inline ktime_t ktime_get_coarse_real(void)
83 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
87 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
89 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
90 * time spent in suspend.
92 static inline ktime_t ktime_get_boottime(void)
94 return ktime_get_with_offset(TK_OFFS_BOOT);
97 static inline ktime_t ktime_get_coarse_boottime(void)
99 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
103 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
105 static inline ktime_t ktime_get_clocktai(void)
107 return ktime_get_with_offset(TK_OFFS_TAI);
110 static inline ktime_t ktime_get_coarse_clocktai(void)
112 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
115 static inline ktime_t ktime_get_coarse(void)
117 struct timespec64 ts;
119 ktime_get_coarse_ts64(&ts);
120 return timespec64_to_ktime(ts);
123 static inline u64 ktime_get_coarse_ns(void)
125 return ktime_to_ns(ktime_get_coarse());
128 static inline u64 ktime_get_coarse_real_ns(void)
130 return ktime_to_ns(ktime_get_coarse_real());
133 static inline u64 ktime_get_coarse_boottime_ns(void)
135 return ktime_to_ns(ktime_get_coarse_boottime());
138 static inline u64 ktime_get_coarse_clocktai_ns(void)
140 return ktime_to_ns(ktime_get_coarse_clocktai());
144 * ktime_mono_to_real - Convert monotonic time to clock realtime
146 static inline ktime_t ktime_mono_to_real(ktime_t mono)
148 return ktime_mono_to_any(mono, TK_OFFS_REAL);
151 static inline u64 ktime_get_ns(void)
153 return ktime_to_ns(ktime_get());
156 static inline u64 ktime_get_real_ns(void)
158 return ktime_to_ns(ktime_get_real());
161 static inline u64 ktime_get_boottime_ns(void)
163 return ktime_to_ns(ktime_get_boottime());
166 static inline u64 ktime_get_clocktai_ns(void)
168 return ktime_to_ns(ktime_get_clocktai());
171 static inline u64 ktime_get_raw_ns(void)
173 return ktime_to_ns(ktime_get_raw());
176 extern u64 ktime_get_mono_fast_ns(void);
177 extern u64 ktime_get_raw_fast_ns(void);
178 extern u64 ktime_get_boot_fast_ns(void);
179 extern u64 ktime_get_real_fast_ns(void);
182 * timespec64/time64_t interfaces utilizing the ktime based ones
183 * for API completeness, these could be implemented more efficiently
186 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
188 *ts = ktime_to_timespec64(ktime_get_boottime());
191 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
193 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
196 static inline time64_t ktime_get_boottime_seconds(void)
198 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
201 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
203 *ts = ktime_to_timespec64(ktime_get_clocktai());
206 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
208 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
211 static inline time64_t ktime_get_clocktai_seconds(void)
213 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
219 extern bool timekeeping_rtc_skipsuspend(void);
220 extern bool timekeeping_rtc_skipresume(void);
222 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
225 * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
226 * @mono: Monotonic timestamp
227 * @boot: Boottime timestamp
228 * @real: Realtime timestamp
230 struct ktime_timestamps {
237 * struct system_time_snapshot - simultaneous raw/real time capture with
239 * @cycles: Clocksource counter value to produce the system times
240 * @real: Realtime system time
241 * @raw: Monotonic raw system time
242 * @clock_was_set_seq: The sequence number of clock was set events
243 * @cs_was_changed_seq: The sequence number of clocksource change events
245 struct system_time_snapshot {
249 unsigned int clock_was_set_seq;
250 u8 cs_was_changed_seq;
254 * struct system_device_crosststamp - system/device cross-timestamp
255 * (synchronized capture)
256 * @device: Device time
257 * @sys_realtime: Realtime simultaneous with device time
258 * @sys_monoraw: Monotonic raw simultaneous with device time
260 struct system_device_crosststamp {
262 ktime_t sys_realtime;
267 * struct system_counterval_t - system counter value with the pointer to the
268 * corresponding clocksource
269 * @cycles: System counter value
270 * @cs: Clocksource corresponding to system counter value. Used by
271 * timekeeping code to verify comparibility of two cycle values
273 struct system_counterval_t {
275 struct clocksource *cs;
279 * Get cross timestamp between system clock and device clock
281 extern int get_device_system_crosststamp(
282 int (*get_time_fn)(ktime_t *device_time,
283 struct system_counterval_t *system_counterval,
286 struct system_time_snapshot *history,
287 struct system_device_crosststamp *xtstamp);
290 * Simultaneously snapshot realtime and monotonic raw clocks
292 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
294 /* NMI safe mono/boot/realtime timestamps */
295 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
298 * Persistent clock related interfaces
300 extern int persistent_clock_is_local;
302 extern void read_persistent_clock64(struct timespec64 *ts);
303 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
304 struct timespec64 *boot_offset);
305 #ifdef CONFIG_GENERIC_CMOS_UPDATE
306 extern int update_persistent_clock64(struct timespec64 now);