1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMEKEEPING_H
3 #define _LINUX_TIMEKEEPING_H
5 #include <linux/errno.h>
6 #include <linux/clocksource_ids.h>
7 #include <linux/ktime.h>
9 /* Included from linux/ktime.h */
11 void timekeeping_init(void);
12 extern int timekeeping_suspended;
14 /* Architecture timer tick functions: */
15 extern void legacy_timer_tick(unsigned long ticks);
18 * Get and set timeofday
20 extern int do_settimeofday64(const struct timespec64 *ts);
21 extern int do_sys_settimeofday64(const struct timespec64 *tv,
22 const struct timezone *tz);
25 * ktime_get() family - read the current time in a multitude of ways.
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.
32 * To get the time in a different format, use the ones with
33 * "ns", "ts64" and "seconds" suffix.
35 * See Documentation/core-api/timekeeping.rst for more details.
40 * timespec64 based interfaces
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);
48 void getboottime64(struct timespec64 *ts);
51 * time64_t base interfaces
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);
58 * ktime_t based interfaces
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);
76 * ktime_get_real - get the real (wall-) time in ktime_t format
78 * Returns: real (wall) time in ktime_t format
80 static inline ktime_t ktime_get_real(void)
82 return ktime_get_with_offset(TK_OFFS_REAL);
85 static inline ktime_t ktime_get_coarse_real(void)
87 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
91 * ktime_get_boottime - Get monotonic time since boot in ktime_t format
93 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
94 * time spent in suspend.
96 * Returns: monotonic time since boot in ktime_t format
98 static inline ktime_t ktime_get_boottime(void)
100 return ktime_get_with_offset(TK_OFFS_BOOT);
103 static inline ktime_t ktime_get_coarse_boottime(void)
105 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
109 * ktime_get_clocktai - Get the TAI time of day in ktime_t format
111 * Returns: the TAI time of day in ktime_t format
113 static inline ktime_t ktime_get_clocktai(void)
115 return ktime_get_with_offset(TK_OFFS_TAI);
118 static inline ktime_t ktime_get_coarse_clocktai(void)
120 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
123 static inline ktime_t ktime_get_coarse(void)
125 struct timespec64 ts;
127 ktime_get_coarse_ts64(&ts);
128 return timespec64_to_ktime(ts);
131 static inline u64 ktime_get_coarse_ns(void)
133 return ktime_to_ns(ktime_get_coarse());
136 static inline u64 ktime_get_coarse_real_ns(void)
138 return ktime_to_ns(ktime_get_coarse_real());
141 static inline u64 ktime_get_coarse_boottime_ns(void)
143 return ktime_to_ns(ktime_get_coarse_boottime());
146 static inline u64 ktime_get_coarse_clocktai_ns(void)
148 return ktime_to_ns(ktime_get_coarse_clocktai());
152 * ktime_mono_to_real - Convert monotonic time to clock realtime
153 * @mono: monotonic time to convert
155 * Returns: time converted to realtime clock
157 static inline ktime_t ktime_mono_to_real(ktime_t mono)
159 return ktime_mono_to_any(mono, TK_OFFS_REAL);
163 * ktime_get_ns - Get the current time in nanoseconds
165 * Returns: current time converted to nanoseconds
167 static inline u64 ktime_get_ns(void)
169 return ktime_to_ns(ktime_get());
173 * ktime_get_real_ns - Get the current real/wall time in nanoseconds
175 * Returns: current real time converted to nanoseconds
177 static inline u64 ktime_get_real_ns(void)
179 return ktime_to_ns(ktime_get_real());
183 * ktime_get_boottime_ns - Get the monotonic time since boot in nanoseconds
185 * Returns: current boottime converted to nanoseconds
187 static inline u64 ktime_get_boottime_ns(void)
189 return ktime_to_ns(ktime_get_boottime());
193 * ktime_get_clocktai_ns - Get the current TAI time of day in nanoseconds
195 * Returns: current TAI time converted to nanoseconds
197 static inline u64 ktime_get_clocktai_ns(void)
199 return ktime_to_ns(ktime_get_clocktai());
203 * ktime_get_raw_ns - Get the raw monotonic time in nanoseconds
205 * Returns: current raw monotonic time converted to nanoseconds
207 static inline u64 ktime_get_raw_ns(void)
209 return ktime_to_ns(ktime_get_raw());
212 extern u64 ktime_get_mono_fast_ns(void);
213 extern u64 ktime_get_raw_fast_ns(void);
214 extern u64 ktime_get_boot_fast_ns(void);
215 extern u64 ktime_get_tai_fast_ns(void);
216 extern u64 ktime_get_real_fast_ns(void);
219 * timespec64/time64_t interfaces utilizing the ktime based ones
220 * for API completeness, these could be implemented more efficiently
223 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
225 *ts = ktime_to_timespec64(ktime_get_boottime());
228 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
230 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
233 static inline time64_t ktime_get_boottime_seconds(void)
235 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
238 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
240 *ts = ktime_to_timespec64(ktime_get_clocktai());
243 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
245 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
248 static inline time64_t ktime_get_clocktai_seconds(void)
250 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
256 extern bool timekeeping_rtc_skipsuspend(void);
257 extern bool timekeeping_rtc_skipresume(void);
259 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
262 * struct ktime_timestamps - Simultaneous mono/boot/real timestamps
263 * @mono: Monotonic timestamp
264 * @boot: Boottime timestamp
265 * @real: Realtime timestamp
267 struct ktime_timestamps {
274 * struct system_time_snapshot - simultaneous raw/real time capture with
276 * @cycles: Clocksource counter value to produce the system times
277 * @real: Realtime system time
278 * @raw: Monotonic raw system time
279 * @cs_id: Clocksource ID
280 * @clock_was_set_seq: The sequence number of clock-was-set events
281 * @cs_was_changed_seq: The sequence number of clocksource change events
283 struct system_time_snapshot {
287 enum clocksource_ids cs_id;
288 unsigned int clock_was_set_seq;
289 u8 cs_was_changed_seq;
293 * struct system_device_crosststamp - system/device cross-timestamp
294 * (synchronized capture)
295 * @device: Device time
296 * @sys_realtime: Realtime simultaneous with device time
297 * @sys_monoraw: Monotonic raw simultaneous with device time
299 struct system_device_crosststamp {
301 ktime_t sys_realtime;
306 * struct system_counterval_t - system counter value with the ID of the
307 * corresponding clocksource
308 * @cycles: System counter value
309 * @cs_id: Clocksource ID corresponding to system counter value. Used by
310 * timekeeping code to verify comparability of two cycle values.
311 * The default ID, CSID_GENERIC, does not identify a specific
314 struct system_counterval_t {
316 enum clocksource_ids cs_id;
320 * Get cross timestamp between system clock and device clock
322 extern int get_device_system_crosststamp(
323 int (*get_time_fn)(ktime_t *device_time,
324 struct system_counterval_t *system_counterval,
327 struct system_time_snapshot *history,
328 struct system_device_crosststamp *xtstamp);
331 * Simultaneously snapshot realtime and monotonic raw clocks
333 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
335 /* NMI safe mono/boot/realtime timestamps */
336 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
339 * Persistent clock related interfaces
341 extern int persistent_clock_is_local;
343 extern void read_persistent_clock64(struct timespec64 *ts);
344 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
345 struct timespec64 *boot_offset);
346 #ifdef CONFIG_GENERIC_CMOS_UPDATE
347 extern int update_persistent_clock64(struct timespec64 now);